iwpm_util.c 21 KB

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