iwpm_util.c 23 KB

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