iwpm_util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. static LIST_HEAD(iwpm_nlmsg_req_list);
  39. static DEFINE_SPINLOCK(iwpm_nlmsg_req_lock);
  40. static struct hlist_head *iwpm_hash_bucket;
  41. static DEFINE_SPINLOCK(iwpm_mapinfo_lock);
  42. static struct hlist_head *iwpm_reminfo_bucket;
  43. static DEFINE_SPINLOCK(iwpm_reminfo_lock);
  44. static DEFINE_MUTEX(iwpm_admin_lock);
  45. static struct iwpm_admin_data iwpm_admin;
  46. int iwpm_init(u8 nl_client)
  47. {
  48. int ret = 0;
  49. if (iwpm_valid_client(nl_client))
  50. return -EINVAL;
  51. mutex_lock(&iwpm_admin_lock);
  52. if (atomic_read(&iwpm_admin.refcount) == 0) {
  53. iwpm_hash_bucket = kzalloc(IWPM_MAPINFO_HASH_SIZE *
  54. sizeof(struct hlist_head), GFP_KERNEL);
  55. if (!iwpm_hash_bucket) {
  56. ret = -ENOMEM;
  57. pr_err("%s Unable to create mapinfo hash table\n", __func__);
  58. goto init_exit;
  59. }
  60. iwpm_reminfo_bucket = kzalloc(IWPM_REMINFO_HASH_SIZE *
  61. sizeof(struct hlist_head), GFP_KERNEL);
  62. if (!iwpm_reminfo_bucket) {
  63. kfree(iwpm_hash_bucket);
  64. ret = -ENOMEM;
  65. pr_err("%s Unable to create reminfo hash table\n", __func__);
  66. goto init_exit;
  67. }
  68. }
  69. atomic_inc(&iwpm_admin.refcount);
  70. init_exit:
  71. mutex_unlock(&iwpm_admin_lock);
  72. if (!ret) {
  73. iwpm_set_valid(nl_client, 1);
  74. pr_debug("%s: Mapinfo and reminfo tables are created\n",
  75. __func__);
  76. }
  77. return ret;
  78. }
  79. EXPORT_SYMBOL(iwpm_init);
  80. static void free_hash_bucket(void);
  81. static void free_reminfo_bucket(void);
  82. int iwpm_exit(u8 nl_client)
  83. {
  84. if (!iwpm_valid_client(nl_client))
  85. return -EINVAL;
  86. mutex_lock(&iwpm_admin_lock);
  87. if (atomic_read(&iwpm_admin.refcount) == 0) {
  88. mutex_unlock(&iwpm_admin_lock);
  89. pr_err("%s Incorrect usage - negative refcount\n", __func__);
  90. return -EINVAL;
  91. }
  92. if (atomic_dec_and_test(&iwpm_admin.refcount)) {
  93. free_hash_bucket();
  94. free_reminfo_bucket();
  95. pr_debug("%s: Resources are destroyed\n", __func__);
  96. }
  97. mutex_unlock(&iwpm_admin_lock);
  98. iwpm_set_valid(nl_client, 0);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(iwpm_exit);
  102. static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage *,
  103. struct sockaddr_storage *);
  104. int iwpm_create_mapinfo(struct sockaddr_storage *local_sockaddr,
  105. struct sockaddr_storage *mapped_sockaddr,
  106. u8 nl_client)
  107. {
  108. struct hlist_head *hash_bucket_head;
  109. struct iwpm_mapping_info *map_info;
  110. unsigned long flags;
  111. int ret = -EINVAL;
  112. if (!iwpm_valid_client(nl_client))
  113. return ret;
  114. map_info = kzalloc(sizeof(struct iwpm_mapping_info), GFP_KERNEL);
  115. if (!map_info) {
  116. pr_err("%s: Unable to allocate a mapping info\n", __func__);
  117. return -ENOMEM;
  118. }
  119. memcpy(&map_info->local_sockaddr, local_sockaddr,
  120. sizeof(struct sockaddr_storage));
  121. memcpy(&map_info->mapped_sockaddr, mapped_sockaddr,
  122. sizeof(struct sockaddr_storage));
  123. map_info->nl_client = nl_client;
  124. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  125. if (iwpm_hash_bucket) {
  126. hash_bucket_head = get_mapinfo_hash_bucket(
  127. &map_info->local_sockaddr,
  128. &map_info->mapped_sockaddr);
  129. if (hash_bucket_head) {
  130. hlist_add_head(&map_info->hlist_node, hash_bucket_head);
  131. ret = 0;
  132. }
  133. }
  134. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  135. return ret;
  136. }
  137. EXPORT_SYMBOL(iwpm_create_mapinfo);
  138. int iwpm_remove_mapinfo(struct sockaddr_storage *local_sockaddr,
  139. struct sockaddr_storage *mapped_local_addr)
  140. {
  141. struct hlist_node *tmp_hlist_node;
  142. struct hlist_head *hash_bucket_head;
  143. struct iwpm_mapping_info *map_info = NULL;
  144. unsigned long flags;
  145. int ret = -EINVAL;
  146. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  147. if (iwpm_hash_bucket) {
  148. hash_bucket_head = get_mapinfo_hash_bucket(
  149. local_sockaddr,
  150. mapped_local_addr);
  151. if (!hash_bucket_head)
  152. goto remove_mapinfo_exit;
  153. hlist_for_each_entry_safe(map_info, tmp_hlist_node,
  154. hash_bucket_head, hlist_node) {
  155. if (!iwpm_compare_sockaddr(&map_info->mapped_sockaddr,
  156. mapped_local_addr)) {
  157. hlist_del_init(&map_info->hlist_node);
  158. kfree(map_info);
  159. ret = 0;
  160. break;
  161. }
  162. }
  163. }
  164. remove_mapinfo_exit:
  165. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(iwpm_remove_mapinfo);
  169. static void free_hash_bucket(void)
  170. {
  171. struct hlist_node *tmp_hlist_node;
  172. struct iwpm_mapping_info *map_info;
  173. unsigned long flags;
  174. int i;
  175. /* remove all the mapinfo data from the list */
  176. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  177. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  178. hlist_for_each_entry_safe(map_info, tmp_hlist_node,
  179. &iwpm_hash_bucket[i], hlist_node) {
  180. hlist_del_init(&map_info->hlist_node);
  181. kfree(map_info);
  182. }
  183. }
  184. /* free the hash list */
  185. kfree(iwpm_hash_bucket);
  186. iwpm_hash_bucket = NULL;
  187. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  188. }
  189. static void free_reminfo_bucket(void)
  190. {
  191. struct hlist_node *tmp_hlist_node;
  192. struct iwpm_remote_info *rem_info;
  193. unsigned long flags;
  194. int i;
  195. /* remove all the remote info from the list */
  196. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  197. for (i = 0; i < IWPM_REMINFO_HASH_SIZE; i++) {
  198. hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
  199. &iwpm_reminfo_bucket[i], hlist_node) {
  200. hlist_del_init(&rem_info->hlist_node);
  201. kfree(rem_info);
  202. }
  203. }
  204. /* free the hash list */
  205. kfree(iwpm_reminfo_bucket);
  206. iwpm_reminfo_bucket = NULL;
  207. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  208. }
  209. static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage *,
  210. struct sockaddr_storage *);
  211. void iwpm_add_remote_info(struct iwpm_remote_info *rem_info)
  212. {
  213. struct hlist_head *hash_bucket_head;
  214. unsigned long flags;
  215. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  216. if (iwpm_reminfo_bucket) {
  217. hash_bucket_head = get_reminfo_hash_bucket(
  218. &rem_info->mapped_loc_sockaddr,
  219. &rem_info->mapped_rem_sockaddr);
  220. if (hash_bucket_head)
  221. hlist_add_head(&rem_info->hlist_node, hash_bucket_head);
  222. }
  223. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  224. }
  225. int iwpm_get_remote_info(struct sockaddr_storage *mapped_loc_addr,
  226. struct sockaddr_storage *mapped_rem_addr,
  227. struct sockaddr_storage *remote_addr,
  228. u8 nl_client)
  229. {
  230. struct hlist_node *tmp_hlist_node;
  231. struct hlist_head *hash_bucket_head;
  232. struct iwpm_remote_info *rem_info = NULL;
  233. unsigned long flags;
  234. int ret = -EINVAL;
  235. if (!iwpm_valid_client(nl_client)) {
  236. pr_info("%s: Invalid client = %d\n", __func__, nl_client);
  237. return ret;
  238. }
  239. spin_lock_irqsave(&iwpm_reminfo_lock, flags);
  240. if (iwpm_reminfo_bucket) {
  241. hash_bucket_head = get_reminfo_hash_bucket(
  242. mapped_loc_addr,
  243. mapped_rem_addr);
  244. if (!hash_bucket_head)
  245. goto get_remote_info_exit;
  246. hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
  247. hash_bucket_head, hlist_node) {
  248. if (!iwpm_compare_sockaddr(&rem_info->mapped_loc_sockaddr,
  249. mapped_loc_addr) &&
  250. !iwpm_compare_sockaddr(&rem_info->mapped_rem_sockaddr,
  251. mapped_rem_addr)) {
  252. memcpy(remote_addr, &rem_info->remote_sockaddr,
  253. sizeof(struct sockaddr_storage));
  254. iwpm_print_sockaddr(remote_addr,
  255. "get_remote_info: Remote sockaddr:");
  256. hlist_del_init(&rem_info->hlist_node);
  257. kfree(rem_info);
  258. ret = 0;
  259. break;
  260. }
  261. }
  262. }
  263. get_remote_info_exit:
  264. spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
  265. return ret;
  266. }
  267. EXPORT_SYMBOL(iwpm_get_remote_info);
  268. struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
  269. u8 nl_client, gfp_t gfp)
  270. {
  271. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  272. unsigned long flags;
  273. nlmsg_request = kzalloc(sizeof(struct iwpm_nlmsg_request), gfp);
  274. if (!nlmsg_request) {
  275. pr_err("%s Unable to allocate a nlmsg_request\n", __func__);
  276. return NULL;
  277. }
  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. return nlmsg_request;
  288. }
  289. void iwpm_free_nlmsg_request(struct kref *kref)
  290. {
  291. struct iwpm_nlmsg_request *nlmsg_request;
  292. unsigned long flags;
  293. nlmsg_request = container_of(kref, struct iwpm_nlmsg_request, kref);
  294. spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
  295. list_del_init(&nlmsg_request->inprocess_list);
  296. spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
  297. if (!nlmsg_request->request_done)
  298. pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n",
  299. __func__, nlmsg_request->nlmsg_seq);
  300. kfree(nlmsg_request);
  301. }
  302. struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq)
  303. {
  304. struct iwpm_nlmsg_request *nlmsg_request;
  305. struct iwpm_nlmsg_request *found_request = NULL;
  306. unsigned long flags;
  307. spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
  308. list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list,
  309. inprocess_list) {
  310. if (nlmsg_request->nlmsg_seq == echo_seq) {
  311. found_request = nlmsg_request;
  312. kref_get(&nlmsg_request->kref);
  313. break;
  314. }
  315. }
  316. spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
  317. return found_request;
  318. }
  319. int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request)
  320. {
  321. int ret;
  322. init_waitqueue_head(&nlmsg_request->waitq);
  323. ret = wait_event_timeout(nlmsg_request->waitq,
  324. (nlmsg_request->request_done != 0), 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. int iwpm_valid_client(u8 nl_client)
  340. {
  341. if (nl_client >= RDMA_NL_NUM_CLIENTS)
  342. return 0;
  343. return iwpm_admin.client_list[nl_client];
  344. }
  345. void iwpm_set_valid(u8 nl_client, int valid)
  346. {
  347. if (nl_client >= RDMA_NL_NUM_CLIENTS)
  348. return;
  349. iwpm_admin.client_list[nl_client] = valid;
  350. }
  351. /* valid client */
  352. int iwpm_registered_client(u8 nl_client)
  353. {
  354. return iwpm_admin.reg_list[nl_client];
  355. }
  356. /* valid client */
  357. void iwpm_set_registered(u8 nl_client, int reg)
  358. {
  359. iwpm_admin.reg_list[nl_client] = reg;
  360. }
  361. int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
  362. struct sockaddr_storage *b_sockaddr)
  363. {
  364. if (a_sockaddr->ss_family != b_sockaddr->ss_family)
  365. return 1;
  366. if (a_sockaddr->ss_family == AF_INET) {
  367. struct sockaddr_in *a4_sockaddr =
  368. (struct sockaddr_in *)a_sockaddr;
  369. struct sockaddr_in *b4_sockaddr =
  370. (struct sockaddr_in *)b_sockaddr;
  371. if (!memcmp(&a4_sockaddr->sin_addr,
  372. &b4_sockaddr->sin_addr, sizeof(struct in_addr))
  373. && a4_sockaddr->sin_port == b4_sockaddr->sin_port)
  374. return 0;
  375. } else if (a_sockaddr->ss_family == AF_INET6) {
  376. struct sockaddr_in6 *a6_sockaddr =
  377. (struct sockaddr_in6 *)a_sockaddr;
  378. struct sockaddr_in6 *b6_sockaddr =
  379. (struct sockaddr_in6 *)b_sockaddr;
  380. if (!memcmp(&a6_sockaddr->sin6_addr,
  381. &b6_sockaddr->sin6_addr, sizeof(struct in6_addr))
  382. && a6_sockaddr->sin6_port == b6_sockaddr->sin6_port)
  383. return 0;
  384. } else {
  385. pr_err("%s: Invalid sockaddr family\n", __func__);
  386. }
  387. return 1;
  388. }
  389. struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
  390. int nl_client)
  391. {
  392. struct sk_buff *skb = NULL;
  393. skb = dev_alloc_skb(NLMSG_GOODSIZE);
  394. if (!skb) {
  395. pr_err("%s Unable to allocate skb\n", __func__);
  396. goto create_nlmsg_exit;
  397. }
  398. if (!(ibnl_put_msg(skb, nlh, 0, 0, nl_client, nl_op,
  399. NLM_F_REQUEST))) {
  400. pr_warn("%s: Unable to put the nlmsg header\n", __func__);
  401. dev_kfree_skb(skb);
  402. skb = NULL;
  403. }
  404. create_nlmsg_exit:
  405. return skb;
  406. }
  407. int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
  408. const struct nla_policy *nlmsg_policy,
  409. struct nlattr *nltb[], const char *msg_type)
  410. {
  411. int nlh_len = 0;
  412. int ret;
  413. const char *err_str = "";
  414. ret = nlmsg_validate(cb->nlh, nlh_len, policy_max-1, nlmsg_policy);
  415. if (ret) {
  416. err_str = "Invalid attribute";
  417. goto parse_nlmsg_error;
  418. }
  419. ret = nlmsg_parse(cb->nlh, nlh_len, nltb, policy_max-1, nlmsg_policy);
  420. if (ret) {
  421. err_str = "Unable to parse the nlmsg";
  422. goto parse_nlmsg_error;
  423. }
  424. ret = iwpm_validate_nlmsg_attr(nltb, policy_max);
  425. if (ret) {
  426. err_str = "Invalid NULL attribute";
  427. goto parse_nlmsg_error;
  428. }
  429. return 0;
  430. parse_nlmsg_error:
  431. pr_warn("%s: %s (msg type %s ret = %d)\n",
  432. __func__, err_str, msg_type, ret);
  433. return ret;
  434. }
  435. void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg)
  436. {
  437. struct sockaddr_in6 *sockaddr_v6;
  438. struct sockaddr_in *sockaddr_v4;
  439. switch (sockaddr->ss_family) {
  440. case AF_INET:
  441. sockaddr_v4 = (struct sockaddr_in *)sockaddr;
  442. pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
  443. msg, &sockaddr_v4->sin_addr,
  444. ntohs(sockaddr_v4->sin_port),
  445. ntohs(sockaddr_v4->sin_port));
  446. break;
  447. case AF_INET6:
  448. sockaddr_v6 = (struct sockaddr_in6 *)sockaddr;
  449. pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
  450. msg, &sockaddr_v6->sin6_addr,
  451. ntohs(sockaddr_v6->sin6_port),
  452. ntohs(sockaddr_v6->sin6_port));
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. static u32 iwpm_ipv6_jhash(struct sockaddr_in6 *ipv6_sockaddr)
  459. {
  460. u32 ipv6_hash = jhash(&ipv6_sockaddr->sin6_addr, sizeof(struct in6_addr), 0);
  461. u32 hash = jhash_2words(ipv6_hash, (__force u32) ipv6_sockaddr->sin6_port, 0);
  462. return hash;
  463. }
  464. static u32 iwpm_ipv4_jhash(struct sockaddr_in *ipv4_sockaddr)
  465. {
  466. u32 ipv4_hash = jhash(&ipv4_sockaddr->sin_addr, sizeof(struct in_addr), 0);
  467. u32 hash = jhash_2words(ipv4_hash, (__force u32) ipv4_sockaddr->sin_port, 0);
  468. return hash;
  469. }
  470. static int get_hash_bucket(struct sockaddr_storage *a_sockaddr,
  471. struct sockaddr_storage *b_sockaddr, u32 *hash)
  472. {
  473. u32 a_hash, b_hash;
  474. if (a_sockaddr->ss_family == AF_INET) {
  475. a_hash = iwpm_ipv4_jhash((struct sockaddr_in *) a_sockaddr);
  476. b_hash = iwpm_ipv4_jhash((struct sockaddr_in *) b_sockaddr);
  477. } else if (a_sockaddr->ss_family == AF_INET6) {
  478. a_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) a_sockaddr);
  479. b_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) b_sockaddr);
  480. } else {
  481. pr_err("%s: Invalid sockaddr family\n", __func__);
  482. return -EINVAL;
  483. }
  484. if (a_hash == b_hash) /* if port mapper isn't available */
  485. *hash = a_hash;
  486. else
  487. *hash = jhash_2words(a_hash, b_hash, 0);
  488. return 0;
  489. }
  490. static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage
  491. *local_sockaddr, struct sockaddr_storage
  492. *mapped_sockaddr)
  493. {
  494. u32 hash;
  495. int ret;
  496. ret = get_hash_bucket(local_sockaddr, mapped_sockaddr, &hash);
  497. if (ret)
  498. return NULL;
  499. return &iwpm_hash_bucket[hash & IWPM_MAPINFO_HASH_MASK];
  500. }
  501. static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage
  502. *mapped_loc_sockaddr, struct sockaddr_storage
  503. *mapped_rem_sockaddr)
  504. {
  505. u32 hash;
  506. int ret;
  507. ret = get_hash_bucket(mapped_loc_sockaddr, mapped_rem_sockaddr, &hash);
  508. if (ret)
  509. return NULL;
  510. return &iwpm_reminfo_bucket[hash & IWPM_REMINFO_HASH_MASK];
  511. }
  512. static int send_mapinfo_num(u32 mapping_num, u8 nl_client, int iwpm_pid)
  513. {
  514. struct sk_buff *skb = NULL;
  515. struct nlmsghdr *nlh;
  516. u32 msg_seq;
  517. const char *err_str = "";
  518. int ret = -EINVAL;
  519. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_MAPINFO_NUM, &nlh, nl_client);
  520. if (!skb) {
  521. err_str = "Unable to create a nlmsg";
  522. goto mapinfo_num_error;
  523. }
  524. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  525. msg_seq = 0;
  526. err_str = "Unable to put attribute of mapinfo number nlmsg";
  527. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq, IWPM_NLA_MAPINFO_SEQ);
  528. if (ret)
  529. goto mapinfo_num_error;
  530. ret = ibnl_put_attr(skb, nlh, sizeof(u32),
  531. &mapping_num, IWPM_NLA_MAPINFO_SEND_NUM);
  532. if (ret)
  533. goto mapinfo_num_error;
  534. ret = ibnl_unicast(skb, nlh, iwpm_pid);
  535. if (ret) {
  536. skb = NULL;
  537. err_str = "Unable to send a nlmsg";
  538. goto mapinfo_num_error;
  539. }
  540. pr_debug("%s: Sent mapping number = %d\n", __func__, mapping_num);
  541. return 0;
  542. mapinfo_num_error:
  543. pr_info("%s: %s\n", __func__, err_str);
  544. if (skb)
  545. dev_kfree_skb(skb);
  546. return ret;
  547. }
  548. static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid)
  549. {
  550. struct nlmsghdr *nlh = NULL;
  551. int ret = 0;
  552. if (!skb)
  553. return ret;
  554. if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
  555. RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
  556. pr_warn("%s Unable to put NLMSG_DONE\n", __func__);
  557. return -ENOMEM;
  558. }
  559. nlh->nlmsg_type = NLMSG_DONE;
  560. ret = ibnl_unicast(skb, (struct nlmsghdr *)skb->data, iwpm_pid);
  561. if (ret)
  562. pr_warn("%s Unable to send a nlmsg\n", __func__);
  563. return ret;
  564. }
  565. int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid)
  566. {
  567. struct iwpm_mapping_info *map_info;
  568. struct sk_buff *skb = NULL;
  569. struct nlmsghdr *nlh;
  570. int skb_num = 0, mapping_num = 0;
  571. int i = 0, nlmsg_bytes = 0;
  572. unsigned long flags;
  573. const char *err_str = "";
  574. int ret;
  575. skb = dev_alloc_skb(NLMSG_GOODSIZE);
  576. if (!skb) {
  577. ret = -ENOMEM;
  578. err_str = "Unable to allocate skb";
  579. goto send_mapping_info_exit;
  580. }
  581. skb_num++;
  582. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  583. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  584. hlist_for_each_entry(map_info, &iwpm_hash_bucket[i],
  585. hlist_node) {
  586. if (map_info->nl_client != nl_client)
  587. continue;
  588. nlh = NULL;
  589. if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
  590. RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
  591. ret = -ENOMEM;
  592. err_str = "Unable to put the nlmsg header";
  593. goto send_mapping_info_unlock;
  594. }
  595. err_str = "Unable to put attribute of the nlmsg";
  596. ret = ibnl_put_attr(skb, nlh,
  597. sizeof(struct sockaddr_storage),
  598. &map_info->local_sockaddr,
  599. IWPM_NLA_MAPINFO_LOCAL_ADDR);
  600. if (ret)
  601. goto send_mapping_info_unlock;
  602. ret = ibnl_put_attr(skb, nlh,
  603. sizeof(struct sockaddr_storage),
  604. &map_info->mapped_sockaddr,
  605. IWPM_NLA_MAPINFO_MAPPED_ADDR);
  606. if (ret)
  607. goto send_mapping_info_unlock;
  608. iwpm_print_sockaddr(&map_info->local_sockaddr,
  609. "send_mapping_info: Local sockaddr:");
  610. iwpm_print_sockaddr(&map_info->mapped_sockaddr,
  611. "send_mapping_info: Mapped local sockaddr:");
  612. mapping_num++;
  613. nlmsg_bytes += nlh->nlmsg_len;
  614. /* check if all mappings can fit in one skb */
  615. if (NLMSG_GOODSIZE - nlmsg_bytes < nlh->nlmsg_len * 2) {
  616. /* and leave room for NLMSG_DONE */
  617. nlmsg_bytes = 0;
  618. skb_num++;
  619. spin_unlock_irqrestore(&iwpm_mapinfo_lock,
  620. flags);
  621. /* send the skb */
  622. ret = send_nlmsg_done(skb, nl_client, iwpm_pid);
  623. skb = NULL;
  624. if (ret) {
  625. err_str = "Unable to send map info";
  626. goto send_mapping_info_exit;
  627. }
  628. if (skb_num == IWPM_MAPINFO_SKB_COUNT) {
  629. ret = -ENOMEM;
  630. err_str = "Insufficient skbs for map info";
  631. goto send_mapping_info_exit;
  632. }
  633. skb = dev_alloc_skb(NLMSG_GOODSIZE);
  634. if (!skb) {
  635. ret = -ENOMEM;
  636. err_str = "Unable to allocate skb";
  637. goto send_mapping_info_exit;
  638. }
  639. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  640. }
  641. }
  642. }
  643. send_mapping_info_unlock:
  644. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  645. send_mapping_info_exit:
  646. if (ret) {
  647. pr_warn("%s: %s (ret = %d)\n", __func__, err_str, ret);
  648. if (skb)
  649. dev_kfree_skb(skb);
  650. return ret;
  651. }
  652. send_nlmsg_done(skb, nl_client, iwpm_pid);
  653. return send_mapinfo_num(mapping_num, nl_client, iwpm_pid);
  654. }
  655. int iwpm_mapinfo_available(void)
  656. {
  657. unsigned long flags;
  658. int full_bucket = 0, i = 0;
  659. spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
  660. if (iwpm_hash_bucket) {
  661. for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
  662. if (!hlist_empty(&iwpm_hash_bucket[i])) {
  663. full_bucket = 1;
  664. break;
  665. }
  666. }
  667. }
  668. spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
  669. return full_bucket;
  670. }