iwpm_util.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
  3. * Copyright (c) 2014 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include "iwpm_util.h"
  34. #define IWPM_MAPINFO_HASH_SIZE 512
  35. #define IWPM_MAPINFO_HASH_MASK (IWPM_MAPINFO_HASH_SIZE - 1)
  36. #define IWPM_REMINFO_HASH_SIZE 64
  37. #define IWPM_REMINFO_HASH_MASK (IWPM_REMINFO_HASH_SIZE - 1)
  38. #define IWPM_MSG_SIZE 512
  39. static LIST_HEAD(iwpm_nlmsg_req_list);
  40. static DEFINE_SPINLOCK(iwpm_nlmsg_req_lock);
  41. static struct hlist_head *iwpm_hash_bucket;
  42. static DEFINE_SPINLOCK(iwpm_mapinfo_lock);
  43. static struct hlist_head *iwpm_reminfo_bucket;
  44. static DEFINE_SPINLOCK(iwpm_reminfo_lock);
  45. static struct iwpm_admin_data iwpm_admin;
  46. /**
  47. * iwpm_init - Allocate resources for the iwarp port mapper
  48. * @nl_client: The index of the netlink client
  49. *
  50. * Should be called when network interface goes up.
  51. */
  52. int iwpm_init(u8 nl_client)
  53. {
  54. iwpm_hash_bucket = kzalloc_objs(struct hlist_head,
  55. IWPM_MAPINFO_HASH_SIZE);
  56. if (!iwpm_hash_bucket)
  57. return -ENOMEM;
  58. iwpm_reminfo_bucket = kzalloc_objs(struct hlist_head,
  59. IWPM_REMINFO_HASH_SIZE);
  60. if (!iwpm_reminfo_bucket) {
  61. kfree(iwpm_hash_bucket);
  62. return -ENOMEM;
  63. }
  64. iwpm_set_registration(nl_client, IWPM_REG_UNDEF);
  65. pr_debug("%s: Mapinfo and reminfo tables are created\n", __func__);
  66. return 0;
  67. }
  68. static void free_hash_bucket(void);
  69. static void free_reminfo_bucket(void);
  70. /**
  71. * iwpm_exit - Deallocate resources for the iwarp port mapper
  72. * @nl_client: The index of the netlink client
  73. *
  74. * Should be called when network interface goes down.
  75. */
  76. int iwpm_exit(u8 nl_client)
  77. {
  78. free_hash_bucket();
  79. free_reminfo_bucket();
  80. pr_debug("%s: Resources are destroyed\n", __func__);
  81. iwpm_set_registration(nl_client, IWPM_REG_UNDEF);
  82. return 0;
  83. }
  84. static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage *,
  85. struct sockaddr_storage *);
  86. /**
  87. * iwpm_create_mapinfo - Store local and mapped IPv4/IPv6 address
  88. * info in a hash table
  89. * @local_sockaddr: Local ip/tcp address
  90. * @mapped_sockaddr: Mapped local ip/tcp address
  91. * @nl_client: The index of the netlink client
  92. * @map_flags: IWPM mapping flags
  93. */
  94. int iwpm_create_mapinfo(struct sockaddr_storage *local_sockaddr,
  95. struct sockaddr_storage *mapped_sockaddr,
  96. u8 nl_client, u32 map_flags)
  97. {
  98. struct hlist_head *hash_bucket_head = NULL;
  99. struct iwpm_mapping_info *map_info;
  100. unsigned long flags;
  101. int ret = -EINVAL;
  102. map_info = kzalloc_obj(struct iwpm_mapping_info);
  103. if (!map_info)
  104. return -ENOMEM;
  105. memcpy(&map_info->local_sockaddr, local_sockaddr,
  106. sizeof(struct sockaddr_storage));
  107. memcpy(&map_info->mapped_sockaddr, mapped_sockaddr,
  108. sizeof(struct sockaddr_storage));
  109. map_info->nl_client = nl_client;
  110. map_info->map_flags = map_flags;
  111. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  112. if (iwpm_hash_bucket) {
  113. hash_bucket_head = get_mapinfo_hash_bucket(
  114. &map_info->local_sockaddr,
  115. &map_info->mapped_sockaddr);
  116. if (hash_bucket_head) {
  117. hlist_add_head(&map_info->hlist_node, hash_bucket_head);
  118. ret = 0;
  119. }
  120. }
  121. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  122. if (!hash_bucket_head)
  123. kfree(map_info);
  124. return ret;
  125. }
  126. /**
  127. * iwpm_remove_mapinfo - Remove local and mapped IPv4/IPv6 address
  128. * info from the hash table
  129. * @local_sockaddr: Local ip/tcp address
  130. * @mapped_local_addr: Mapped local ip/tcp address
  131. *
  132. * Returns err code if mapping info is not found in the hash table,
  133. * otherwise returns 0
  134. */
  135. int iwpm_remove_mapinfo(struct sockaddr_storage *local_sockaddr,
  136. struct sockaddr_storage *mapped_local_addr)
  137. {
  138. struct hlist_node *tmp_hlist_node;
  139. struct hlist_head *hash_bucket_head;
  140. struct iwpm_mapping_info *map_info = NULL;
  141. unsigned long flags;
  142. int ret = -EINVAL;
  143. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  144. if (iwpm_hash_bucket) {
  145. hash_bucket_head = get_mapinfo_hash_bucket(
  146. local_sockaddr,
  147. mapped_local_addr);
  148. if (!hash_bucket_head)
  149. goto remove_mapinfo_exit;
  150. hlist_for_each_entry_safe(map_info, tmp_hlist_node,
  151. hash_bucket_head, hlist_node) {
  152. if (!iwpm_compare_sockaddr(&map_info->mapped_sockaddr,
  153. mapped_local_addr)) {
  154. hlist_del_init(&map_info->hlist_node);
  155. kfree(map_info);
  156. ret = 0;
  157. break;
  158. }
  159. }
  160. }
  161. remove_mapinfo_exit:
  162. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  163. return ret;
  164. }
  165. static void free_hash_bucket(void)
  166. {
  167. struct hlist_node *tmp_hlist_node;
  168. struct iwpm_mapping_info *map_info;
  169. unsigned long flags;
  170. int i;
  171. /* remove all the mapinfo data from the list */
  172. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  173. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  174. hlist_for_each_entry_safe(map_info, tmp_hlist_node,
  175. &iwpm_hash_bucket[i], hlist_node) {
  176. hlist_del_init(&map_info->hlist_node);
  177. kfree(map_info);
  178. }
  179. }
  180. /* free the hash list */
  181. kfree(iwpm_hash_bucket);
  182. iwpm_hash_bucket = NULL;
  183. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  184. }
  185. static void free_reminfo_bucket(void)
  186. {
  187. struct hlist_node *tmp_hlist_node;
  188. struct iwpm_remote_info *rem_info;
  189. unsigned long flags;
  190. int i;
  191. /* remove all the remote info from the list */
  192. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  193. for (i = 0; i < IWPM_REMINFO_HASH_SIZE; i++) {
  194. hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
  195. &iwpm_reminfo_bucket[i], hlist_node) {
  196. hlist_del_init(&rem_info->hlist_node);
  197. kfree(rem_info);
  198. }
  199. }
  200. /* free the hash list */
  201. kfree(iwpm_reminfo_bucket);
  202. iwpm_reminfo_bucket = NULL;
  203. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  204. }
  205. static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage *,
  206. struct sockaddr_storage *);
  207. void iwpm_add_remote_info(struct iwpm_remote_info *rem_info)
  208. {
  209. struct hlist_head *hash_bucket_head;
  210. unsigned long flags;
  211. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  212. if (iwpm_reminfo_bucket) {
  213. hash_bucket_head = get_reminfo_hash_bucket(
  214. &rem_info->mapped_loc_sockaddr,
  215. &rem_info->mapped_rem_sockaddr);
  216. if (hash_bucket_head)
  217. hlist_add_head(&rem_info->hlist_node, hash_bucket_head);
  218. }
  219. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  220. }
  221. /**
  222. * iwpm_get_remote_info - Get the remote connecting peer address info
  223. *
  224. * @mapped_loc_addr: Mapped local address of the listening peer
  225. * @mapped_rem_addr: Mapped remote address of the connecting peer
  226. * @remote_addr: To store the remote address of the connecting peer
  227. * @nl_client: The index of the netlink client
  228. *
  229. * The remote address info is retrieved and provided to the client in
  230. * the remote_addr. After that it is removed from the hash table
  231. */
  232. int iwpm_get_remote_info(struct sockaddr_storage *mapped_loc_addr,
  233. struct sockaddr_storage *mapped_rem_addr,
  234. struct sockaddr_storage *remote_addr,
  235. u8 nl_client)
  236. {
  237. struct hlist_node *tmp_hlist_node;
  238. struct hlist_head *hash_bucket_head;
  239. struct iwpm_remote_info *rem_info = NULL;
  240. unsigned long flags;
  241. int ret = -EINVAL;
  242. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  243. if (iwpm_reminfo_bucket) {
  244. hash_bucket_head = get_reminfo_hash_bucket(
  245. mapped_loc_addr,
  246. mapped_rem_addr);
  247. if (!hash_bucket_head)
  248. goto get_remote_info_exit;
  249. hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
  250. hash_bucket_head, hlist_node) {
  251. if (!iwpm_compare_sockaddr(&rem_info->mapped_loc_sockaddr,
  252. mapped_loc_addr) &&
  253. !iwpm_compare_sockaddr(&rem_info->mapped_rem_sockaddr,
  254. mapped_rem_addr)) {
  255. memcpy(remote_addr, &rem_info->remote_sockaddr,
  256. sizeof(struct sockaddr_storage));
  257. iwpm_print_sockaddr(remote_addr,
  258. "get_remote_info: Remote sockaddr:");
  259. hlist_del_init(&rem_info->hlist_node);
  260. kfree(rem_info);
  261. ret = 0;
  262. break;
  263. }
  264. }
  265. }
  266. get_remote_info_exit:
  267. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  268. return ret;
  269. }
  270. struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
  271. u8 nl_client, gfp_t gfp)
  272. {
  273. struct iwpm_nlmsg_request *nlmsg_request;
  274. unsigned long flags;
  275. nlmsg_request = kzalloc_obj(struct iwpm_nlmsg_request, gfp);
  276. if (!nlmsg_request)
  277. return NULL;
  278. spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
  279. list_add_tail(&nlmsg_request->inprocess_list, &iwpm_nlmsg_req_list);
  280. spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
  281. kref_init(&nlmsg_request->kref);
  282. kref_get(&nlmsg_request->kref);
  283. nlmsg_request->nlmsg_seq = nlmsg_seq;
  284. nlmsg_request->nl_client = nl_client;
  285. nlmsg_request->request_done = 0;
  286. nlmsg_request->err_code = 0;
  287. sema_init(&nlmsg_request->sem, 1);
  288. down(&nlmsg_request->sem);
  289. return nlmsg_request;
  290. }
  291. void iwpm_free_nlmsg_request(struct kref *kref)
  292. {
  293. struct iwpm_nlmsg_request *nlmsg_request;
  294. unsigned long flags;
  295. nlmsg_request = container_of(kref, struct iwpm_nlmsg_request, kref);
  296. spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
  297. list_del_init(&nlmsg_request->inprocess_list);
  298. spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
  299. if (!nlmsg_request->request_done)
  300. pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n",
  301. __func__, nlmsg_request->nlmsg_seq);
  302. kfree(nlmsg_request);
  303. }
  304. struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq)
  305. {
  306. struct iwpm_nlmsg_request *nlmsg_request;
  307. struct iwpm_nlmsg_request *found_request = NULL;
  308. unsigned long flags;
  309. spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
  310. list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list,
  311. inprocess_list) {
  312. if (nlmsg_request->nlmsg_seq == echo_seq) {
  313. found_request = nlmsg_request;
  314. kref_get(&nlmsg_request->kref);
  315. break;
  316. }
  317. }
  318. spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
  319. return found_request;
  320. }
  321. int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request)
  322. {
  323. int ret;
  324. ret = down_timeout(&nlmsg_request->sem, IWPM_NL_TIMEOUT);
  325. if (ret) {
  326. ret = -EINVAL;
  327. pr_info("%s: Timeout %d sec for netlink request (seq = %u)\n",
  328. __func__, (IWPM_NL_TIMEOUT/HZ), nlmsg_request->nlmsg_seq);
  329. } else {
  330. ret = nlmsg_request->err_code;
  331. }
  332. kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
  333. return ret;
  334. }
  335. int iwpm_get_nlmsg_seq(void)
  336. {
  337. return atomic_inc_return(&iwpm_admin.nlmsg_seq);
  338. }
  339. /* valid client */
  340. u32 iwpm_get_registration(u8 nl_client)
  341. {
  342. return iwpm_admin.reg_list[nl_client];
  343. }
  344. /* valid client */
  345. void iwpm_set_registration(u8 nl_client, u32 reg)
  346. {
  347. iwpm_admin.reg_list[nl_client] = reg;
  348. }
  349. /* valid client */
  350. u32 iwpm_check_registration(u8 nl_client, u32 reg)
  351. {
  352. return (iwpm_get_registration(nl_client) & reg);
  353. }
  354. int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
  355. struct sockaddr_storage *b_sockaddr)
  356. {
  357. if (a_sockaddr->ss_family != b_sockaddr->ss_family)
  358. return 1;
  359. if (a_sockaddr->ss_family == AF_INET) {
  360. struct sockaddr_in *a4_sockaddr =
  361. (struct sockaddr_in *)a_sockaddr;
  362. struct sockaddr_in *b4_sockaddr =
  363. (struct sockaddr_in *)b_sockaddr;
  364. if (!memcmp(&a4_sockaddr->sin_addr,
  365. &b4_sockaddr->sin_addr, sizeof(struct in_addr))
  366. && a4_sockaddr->sin_port == b4_sockaddr->sin_port)
  367. return 0;
  368. } else if (a_sockaddr->ss_family == AF_INET6) {
  369. struct sockaddr_in6 *a6_sockaddr =
  370. (struct sockaddr_in6 *)a_sockaddr;
  371. struct sockaddr_in6 *b6_sockaddr =
  372. (struct sockaddr_in6 *)b_sockaddr;
  373. if (!memcmp(&a6_sockaddr->sin6_addr,
  374. &b6_sockaddr->sin6_addr, sizeof(struct in6_addr))
  375. && a6_sockaddr->sin6_port == b6_sockaddr->sin6_port)
  376. return 0;
  377. } else {
  378. pr_err("%s: Invalid sockaddr family\n", __func__);
  379. }
  380. return 1;
  381. }
  382. struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
  383. int nl_client)
  384. {
  385. struct sk_buff *skb = NULL;
  386. skb = dev_alloc_skb(IWPM_MSG_SIZE);
  387. if (!skb)
  388. goto create_nlmsg_exit;
  389. if (!(ibnl_put_msg(skb, nlh, 0, 0, nl_client, nl_op,
  390. NLM_F_REQUEST))) {
  391. pr_warn("%s: Unable to put the nlmsg header\n", __func__);
  392. dev_kfree_skb(skb);
  393. skb = NULL;
  394. }
  395. create_nlmsg_exit:
  396. return skb;
  397. }
  398. int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
  399. const struct nla_policy *nlmsg_policy,
  400. struct nlattr *nltb[], const char *msg_type)
  401. {
  402. int nlh_len = 0;
  403. int ret;
  404. const char *err_str = "";
  405. ret = nlmsg_validate_deprecated(cb->nlh, nlh_len, policy_max - 1,
  406. nlmsg_policy, NULL);
  407. if (ret) {
  408. err_str = "Invalid attribute";
  409. goto parse_nlmsg_error;
  410. }
  411. ret = nlmsg_parse_deprecated(cb->nlh, nlh_len, nltb, policy_max - 1,
  412. nlmsg_policy, NULL);
  413. if (ret) {
  414. err_str = "Unable to parse the nlmsg";
  415. goto parse_nlmsg_error;
  416. }
  417. ret = iwpm_validate_nlmsg_attr(nltb, policy_max);
  418. if (ret) {
  419. err_str = "Invalid NULL attribute";
  420. goto parse_nlmsg_error;
  421. }
  422. return 0;
  423. parse_nlmsg_error:
  424. pr_warn("%s: %s (msg type %s ret = %d)\n",
  425. __func__, err_str, msg_type, ret);
  426. return ret;
  427. }
  428. void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg)
  429. {
  430. struct sockaddr_in6 *sockaddr_v6;
  431. struct sockaddr_in *sockaddr_v4;
  432. switch (sockaddr->ss_family) {
  433. case AF_INET:
  434. sockaddr_v4 = (struct sockaddr_in *)sockaddr;
  435. pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
  436. msg, &sockaddr_v4->sin_addr,
  437. ntohs(sockaddr_v4->sin_port),
  438. ntohs(sockaddr_v4->sin_port));
  439. break;
  440. case AF_INET6:
  441. sockaddr_v6 = (struct sockaddr_in6 *)sockaddr;
  442. pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
  443. msg, &sockaddr_v6->sin6_addr,
  444. ntohs(sockaddr_v6->sin6_port),
  445. ntohs(sockaddr_v6->sin6_port));
  446. break;
  447. default:
  448. break;
  449. }
  450. }
  451. static u32 iwpm_ipv6_jhash(struct sockaddr_in6 *ipv6_sockaddr)
  452. {
  453. u32 ipv6_hash = jhash(&ipv6_sockaddr->sin6_addr, sizeof(struct in6_addr), 0);
  454. u32 hash = jhash_2words(ipv6_hash, (__force u32) ipv6_sockaddr->sin6_port, 0);
  455. return hash;
  456. }
  457. static u32 iwpm_ipv4_jhash(struct sockaddr_in *ipv4_sockaddr)
  458. {
  459. u32 ipv4_hash = jhash(&ipv4_sockaddr->sin_addr, sizeof(struct in_addr), 0);
  460. u32 hash = jhash_2words(ipv4_hash, (__force u32) ipv4_sockaddr->sin_port, 0);
  461. return hash;
  462. }
  463. static int get_hash_bucket(struct sockaddr_storage *a_sockaddr,
  464. struct sockaddr_storage *b_sockaddr, u32 *hash)
  465. {
  466. u32 a_hash, b_hash;
  467. if (a_sockaddr->ss_family == AF_INET) {
  468. a_hash = iwpm_ipv4_jhash((struct sockaddr_in *) a_sockaddr);
  469. b_hash = iwpm_ipv4_jhash((struct sockaddr_in *) b_sockaddr);
  470. } else if (a_sockaddr->ss_family == AF_INET6) {
  471. a_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) a_sockaddr);
  472. b_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) b_sockaddr);
  473. } else {
  474. pr_err("%s: Invalid sockaddr family\n", __func__);
  475. return -EINVAL;
  476. }
  477. if (a_hash == b_hash) /* if port mapper isn't available */
  478. *hash = a_hash;
  479. else
  480. *hash = jhash_2words(a_hash, b_hash, 0);
  481. return 0;
  482. }
  483. static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage
  484. *local_sockaddr, struct sockaddr_storage
  485. *mapped_sockaddr)
  486. {
  487. u32 hash;
  488. int ret;
  489. ret = get_hash_bucket(local_sockaddr, mapped_sockaddr, &hash);
  490. if (ret)
  491. return NULL;
  492. return &iwpm_hash_bucket[hash & IWPM_MAPINFO_HASH_MASK];
  493. }
  494. static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage
  495. *mapped_loc_sockaddr, struct sockaddr_storage
  496. *mapped_rem_sockaddr)
  497. {
  498. u32 hash;
  499. int ret;
  500. ret = get_hash_bucket(mapped_loc_sockaddr, mapped_rem_sockaddr, &hash);
  501. if (ret)
  502. return NULL;
  503. return &iwpm_reminfo_bucket[hash & IWPM_REMINFO_HASH_MASK];
  504. }
  505. static int send_mapinfo_num(u32 mapping_num, u8 nl_client, int iwpm_pid)
  506. {
  507. struct sk_buff *skb = NULL;
  508. struct nlmsghdr *nlh;
  509. u32 msg_seq;
  510. const char *err_str = "";
  511. int ret = -EINVAL;
  512. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_MAPINFO_NUM, &nlh, nl_client);
  513. if (!skb) {
  514. err_str = "Unable to create a nlmsg";
  515. goto mapinfo_num_error;
  516. }
  517. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  518. msg_seq = 0;
  519. err_str = "Unable to put attribute of mapinfo number nlmsg";
  520. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq, IWPM_NLA_MAPINFO_SEQ);
  521. if (ret)
  522. goto mapinfo_num_error;
  523. ret = ibnl_put_attr(skb, nlh, sizeof(u32),
  524. &mapping_num, IWPM_NLA_MAPINFO_SEND_NUM);
  525. if (ret)
  526. goto mapinfo_num_error;
  527. nlmsg_end(skb, nlh);
  528. ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
  529. if (ret) {
  530. skb = NULL;
  531. err_str = "Unable to send a nlmsg";
  532. goto mapinfo_num_error;
  533. }
  534. pr_debug("%s: Sent mapping number = %u\n", __func__, mapping_num);
  535. return 0;
  536. mapinfo_num_error:
  537. pr_info("%s: %s\n", __func__, err_str);
  538. dev_kfree_skb(skb);
  539. return ret;
  540. }
  541. static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid)
  542. {
  543. struct nlmsghdr *nlh = NULL;
  544. int ret = 0;
  545. if (!skb)
  546. return ret;
  547. if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
  548. RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
  549. pr_warn("%s Unable to put NLMSG_DONE\n", __func__);
  550. dev_kfree_skb(skb);
  551. return -ENOMEM;
  552. }
  553. nlh->nlmsg_type = NLMSG_DONE;
  554. ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
  555. if (ret)
  556. pr_warn("%s Unable to send a nlmsg\n", __func__);
  557. return ret;
  558. }
  559. int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid)
  560. {
  561. struct iwpm_mapping_info *map_info;
  562. struct sk_buff *skb = NULL;
  563. struct nlmsghdr *nlh;
  564. int skb_num = 0, mapping_num = 0;
  565. int i = 0, nlmsg_bytes = 0;
  566. unsigned long flags;
  567. const char *err_str = "";
  568. int ret;
  569. skb = dev_alloc_skb(NLMSG_GOODSIZE);
  570. if (!skb) {
  571. ret = -ENOMEM;
  572. err_str = "Unable to allocate skb";
  573. goto send_mapping_info_exit;
  574. }
  575. skb_num++;
  576. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  577. ret = -EINVAL;
  578. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  579. hlist_for_each_entry(map_info, &iwpm_hash_bucket[i],
  580. hlist_node) {
  581. if (map_info->nl_client != nl_client)
  582. continue;
  583. nlh = NULL;
  584. if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
  585. RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
  586. ret = -ENOMEM;
  587. err_str = "Unable to put the nlmsg header";
  588. goto send_mapping_info_unlock;
  589. }
  590. err_str = "Unable to put attribute of the nlmsg";
  591. ret = ibnl_put_attr(skb, nlh,
  592. sizeof(struct sockaddr_storage),
  593. &map_info->local_sockaddr,
  594. IWPM_NLA_MAPINFO_LOCAL_ADDR);
  595. if (ret)
  596. goto send_mapping_info_unlock;
  597. ret = ibnl_put_attr(skb, nlh,
  598. sizeof(struct sockaddr_storage),
  599. &map_info->mapped_sockaddr,
  600. IWPM_NLA_MAPINFO_MAPPED_ADDR);
  601. if (ret)
  602. goto send_mapping_info_unlock;
  603. if (iwpm_ulib_version > IWPM_UABI_VERSION_MIN) {
  604. ret = ibnl_put_attr(skb, nlh, sizeof(u32),
  605. &map_info->map_flags,
  606. IWPM_NLA_MAPINFO_FLAGS);
  607. if (ret)
  608. goto send_mapping_info_unlock;
  609. }
  610. nlmsg_end(skb, nlh);
  611. iwpm_print_sockaddr(&map_info->local_sockaddr,
  612. "send_mapping_info: Local sockaddr:");
  613. iwpm_print_sockaddr(&map_info->mapped_sockaddr,
  614. "send_mapping_info: Mapped local sockaddr:");
  615. mapping_num++;
  616. nlmsg_bytes += nlh->nlmsg_len;
  617. /* check if all mappings can fit in one skb */
  618. if (NLMSG_GOODSIZE - nlmsg_bytes < nlh->nlmsg_len * 2) {
  619. /* and leave room for NLMSG_DONE */
  620. nlmsg_bytes = 0;
  621. skb_num++;
  622. spin_unlock_irqrestore(&iwpm_mapinfo_lock,
  623. flags);
  624. /* send the skb */
  625. ret = send_nlmsg_done(skb, nl_client, iwpm_pid);
  626. skb = NULL;
  627. if (ret) {
  628. err_str = "Unable to send map info";
  629. goto send_mapping_info_exit;
  630. }
  631. if (skb_num == IWPM_MAPINFO_SKB_COUNT) {
  632. ret = -ENOMEM;
  633. err_str = "Insufficient skbs for map info";
  634. goto send_mapping_info_exit;
  635. }
  636. skb = dev_alloc_skb(NLMSG_GOODSIZE);
  637. if (!skb) {
  638. ret = -ENOMEM;
  639. err_str = "Unable to allocate skb";
  640. goto send_mapping_info_exit;
  641. }
  642. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  643. }
  644. }
  645. }
  646. send_mapping_info_unlock:
  647. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  648. send_mapping_info_exit:
  649. if (ret) {
  650. pr_warn("%s: %s (ret = %d)\n", __func__, err_str, ret);
  651. dev_kfree_skb(skb);
  652. return ret;
  653. }
  654. send_nlmsg_done(skb, nl_client, iwpm_pid);
  655. return send_mapinfo_num(mapping_num, nl_client, iwpm_pid);
  656. }
  657. int iwpm_mapinfo_available(void)
  658. {
  659. unsigned long flags;
  660. int full_bucket = 0, i = 0;
  661. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  662. if (iwpm_hash_bucket) {
  663. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  664. if (!hlist_empty(&iwpm_hash_bucket[i])) {
  665. full_bucket = 1;
  666. break;
  667. }
  668. }
  669. }
  670. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  671. return full_bucket;
  672. }
  673. int iwpm_send_hello(u8 nl_client, int iwpm_pid, u16 abi_version)
  674. {
  675. struct sk_buff *skb = NULL;
  676. struct nlmsghdr *nlh;
  677. const char *err_str;
  678. int ret = -EINVAL;
  679. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_HELLO, &nlh, nl_client);
  680. if (!skb) {
  681. err_str = "Unable to create a nlmsg";
  682. goto hello_num_error;
  683. }
  684. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  685. err_str = "Unable to put attribute of abi_version into nlmsg";
  686. ret = ibnl_put_attr(skb, nlh, sizeof(u16), &abi_version,
  687. IWPM_NLA_HELLO_ABI_VERSION);
  688. if (ret)
  689. goto hello_num_error;
  690. nlmsg_end(skb, nlh);
  691. ret = rdma_nl_unicast(&init_net, skb, iwpm_pid);
  692. if (ret) {
  693. skb = NULL;
  694. err_str = "Unable to send a nlmsg";
  695. goto hello_num_error;
  696. }
  697. pr_debug("%s: Sent hello abi_version = %u\n", __func__, abi_version);
  698. return 0;
  699. hello_num_error:
  700. pr_info("%s: %s\n", __func__, err_str);
  701. dev_kfree_skb(skb);
  702. return ret;
  703. }