iwpm_msg.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright (c) 2014 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2014 Chelsio, Inc. 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. static const char iwpm_ulib_name[IWPM_ULIBNAME_SIZE] = "iWarpPortMapperUser";
  35. u16 iwpm_ulib_version = IWPM_UABI_VERSION_MIN;
  36. static int iwpm_user_pid = IWPM_PID_UNDEFINED;
  37. static atomic_t echo_nlmsg_seq;
  38. /**
  39. * iwpm_valid_pid - Check if the userspace iwarp port mapper pid is valid
  40. *
  41. * Returns true if the pid is greater than zero, otherwise returns false
  42. */
  43. int iwpm_valid_pid(void)
  44. {
  45. return iwpm_user_pid > 0;
  46. }
  47. /**
  48. * iwpm_register_pid - Send a netlink query to userspace
  49. * to get the iwarp port mapper pid
  50. * @pm_msg: Contains driver info to send to the userspace port mapper
  51. * @nl_client: The index of the netlink client
  52. *
  53. * nlmsg attributes:
  54. * [IWPM_NLA_REG_PID_SEQ]
  55. * [IWPM_NLA_REG_IF_NAME]
  56. * [IWPM_NLA_REG_IBDEV_NAME]
  57. * [IWPM_NLA_REG_ULIB_NAME]
  58. */
  59. int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
  60. {
  61. struct sk_buff *skb = NULL;
  62. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  63. struct nlmsghdr *nlh;
  64. u32 msg_seq;
  65. const char *err_str = "";
  66. int ret = -EINVAL;
  67. if (iwpm_check_registration(nl_client, IWPM_REG_VALID) ||
  68. iwpm_user_pid == IWPM_PID_UNAVAILABLE)
  69. return 0;
  70. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_REG_PID, &nlh, nl_client);
  71. if (!skb) {
  72. err_str = "Unable to create a nlmsg";
  73. goto pid_query_error;
  74. }
  75. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  76. nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq, nl_client, GFP_KERNEL);
  77. if (!nlmsg_request) {
  78. err_str = "Unable to allocate netlink request";
  79. goto pid_query_error;
  80. }
  81. msg_seq = atomic_read(&echo_nlmsg_seq);
  82. /* fill in the pid request message */
  83. err_str = "Unable to put attribute of the nlmsg";
  84. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq, IWPM_NLA_REG_PID_SEQ);
  85. if (ret)
  86. goto pid_query_error;
  87. ret = ibnl_put_attr(skb, nlh, IFNAMSIZ,
  88. pm_msg->if_name, IWPM_NLA_REG_IF_NAME);
  89. if (ret)
  90. goto pid_query_error;
  91. ret = ibnl_put_attr(skb, nlh, IWPM_DEVNAME_SIZE,
  92. pm_msg->dev_name, IWPM_NLA_REG_IBDEV_NAME);
  93. if (ret)
  94. goto pid_query_error;
  95. ret = ibnl_put_attr(skb, nlh, IWPM_ULIBNAME_SIZE,
  96. (char *)iwpm_ulib_name, IWPM_NLA_REG_ULIB_NAME);
  97. if (ret)
  98. goto pid_query_error;
  99. nlmsg_end(skb, nlh);
  100. pr_debug("%s: Multicasting a nlmsg (dev = %s ifname = %s iwpm = %s)\n",
  101. __func__, pm_msg->dev_name, pm_msg->if_name, iwpm_ulib_name);
  102. ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_IWPM, GFP_KERNEL);
  103. if (ret) {
  104. skb = NULL; /* skb is freed in the netlink send-op handling */
  105. iwpm_user_pid = IWPM_PID_UNAVAILABLE;
  106. err_str = "Unable to send a nlmsg";
  107. goto pid_query_error;
  108. }
  109. nlmsg_request->req_buffer = pm_msg;
  110. ret = iwpm_wait_complete_req(nlmsg_request);
  111. return ret;
  112. pid_query_error:
  113. pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
  114. dev_kfree_skb(skb);
  115. if (nlmsg_request)
  116. iwpm_free_nlmsg_request(&nlmsg_request->kref);
  117. return ret;
  118. }
  119. /**
  120. * iwpm_add_mapping - Send a netlink add mapping request to
  121. * the userspace port mapper
  122. * @pm_msg: Contains the local ip/tcp address info to send
  123. * @nl_client: The index of the netlink client
  124. *
  125. * nlmsg attributes:
  126. * [IWPM_NLA_MANAGE_MAPPING_SEQ]
  127. * [IWPM_NLA_MANAGE_ADDR]
  128. * [IWPM_NLA_MANAGE_FLAGS]
  129. *
  130. * If the request is successful, the pm_msg stores
  131. * the port mapper response (mapped address info)
  132. */
  133. int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
  134. {
  135. struct sk_buff *skb = NULL;
  136. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  137. struct nlmsghdr *nlh;
  138. u32 msg_seq;
  139. const char *err_str = "";
  140. int ret = -EINVAL;
  141. if (!iwpm_valid_pid())
  142. return 0;
  143. if (!iwpm_check_registration(nl_client, IWPM_REG_VALID)) {
  144. err_str = "Unregistered port mapper client";
  145. goto add_mapping_error;
  146. }
  147. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_ADD_MAPPING, &nlh, nl_client);
  148. if (!skb) {
  149. err_str = "Unable to create a nlmsg";
  150. goto add_mapping_error;
  151. }
  152. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  153. nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq, nl_client, GFP_KERNEL);
  154. if (!nlmsg_request) {
  155. err_str = "Unable to allocate netlink request";
  156. goto add_mapping_error;
  157. }
  158. msg_seq = atomic_read(&echo_nlmsg_seq);
  159. /* fill in the add mapping message */
  160. err_str = "Unable to put attribute of the nlmsg";
  161. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq,
  162. IWPM_NLA_MANAGE_MAPPING_SEQ);
  163. if (ret)
  164. goto add_mapping_error;
  165. ret = ibnl_put_attr(skb, nlh, sizeof(struct sockaddr_storage),
  166. &pm_msg->loc_addr, IWPM_NLA_MANAGE_ADDR);
  167. if (ret)
  168. goto add_mapping_error;
  169. /* If flags are required and we're not V4, then return a quiet error */
  170. if (pm_msg->flags && iwpm_ulib_version == IWPM_UABI_VERSION_MIN) {
  171. ret = -EINVAL;
  172. goto add_mapping_error_nowarn;
  173. }
  174. if (iwpm_ulib_version > IWPM_UABI_VERSION_MIN) {
  175. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &pm_msg->flags,
  176. IWPM_NLA_MANAGE_FLAGS);
  177. if (ret)
  178. goto add_mapping_error;
  179. }
  180. nlmsg_end(skb, nlh);
  181. nlmsg_request->req_buffer = pm_msg;
  182. ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
  183. if (ret) {
  184. skb = NULL; /* skb is freed in the netlink send-op handling */
  185. iwpm_user_pid = IWPM_PID_UNDEFINED;
  186. err_str = "Unable to send a nlmsg";
  187. goto add_mapping_error;
  188. }
  189. ret = iwpm_wait_complete_req(nlmsg_request);
  190. return ret;
  191. add_mapping_error:
  192. pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
  193. add_mapping_error_nowarn:
  194. dev_kfree_skb(skb);
  195. if (nlmsg_request)
  196. iwpm_free_nlmsg_request(&nlmsg_request->kref);
  197. return ret;
  198. }
  199. /**
  200. * iwpm_add_and_query_mapping - Process the port mapper response to
  201. * iwpm_add_and_query_mapping request
  202. * @pm_msg: Contains the local ip/tcp address info to send
  203. * @nl_client: The index of the netlink client
  204. *
  205. * nlmsg attributes:
  206. * [IWPM_NLA_QUERY_MAPPING_SEQ]
  207. * [IWPM_NLA_QUERY_LOCAL_ADDR]
  208. * [IWPM_NLA_QUERY_REMOTE_ADDR]
  209. * [IWPM_NLA_QUERY_FLAGS]
  210. */
  211. int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
  212. {
  213. struct sk_buff *skb = NULL;
  214. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  215. struct nlmsghdr *nlh;
  216. u32 msg_seq;
  217. const char *err_str = "";
  218. int ret = -EINVAL;
  219. if (!iwpm_valid_pid())
  220. return 0;
  221. if (!iwpm_check_registration(nl_client, IWPM_REG_VALID)) {
  222. err_str = "Unregistered port mapper client";
  223. goto query_mapping_error;
  224. }
  225. ret = -ENOMEM;
  226. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_QUERY_MAPPING, &nlh, nl_client);
  227. if (!skb) {
  228. err_str = "Unable to create a nlmsg";
  229. goto query_mapping_error;
  230. }
  231. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  232. nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq,
  233. nl_client, GFP_KERNEL);
  234. if (!nlmsg_request) {
  235. err_str = "Unable to allocate netlink request";
  236. goto query_mapping_error;
  237. }
  238. msg_seq = atomic_read(&echo_nlmsg_seq);
  239. /* fill in the query message */
  240. err_str = "Unable to put attribute of the nlmsg";
  241. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq,
  242. IWPM_NLA_QUERY_MAPPING_SEQ);
  243. if (ret)
  244. goto query_mapping_error;
  245. ret = ibnl_put_attr(skb, nlh, sizeof(struct sockaddr_storage),
  246. &pm_msg->loc_addr, IWPM_NLA_QUERY_LOCAL_ADDR);
  247. if (ret)
  248. goto query_mapping_error;
  249. ret = ibnl_put_attr(skb, nlh, sizeof(struct sockaddr_storage),
  250. &pm_msg->rem_addr, IWPM_NLA_QUERY_REMOTE_ADDR);
  251. if (ret)
  252. goto query_mapping_error;
  253. /* If flags are required and we're not V4, then return a quite error */
  254. if (pm_msg->flags && iwpm_ulib_version == IWPM_UABI_VERSION_MIN) {
  255. ret = -EINVAL;
  256. goto query_mapping_error_nowarn;
  257. }
  258. if (iwpm_ulib_version > IWPM_UABI_VERSION_MIN) {
  259. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &pm_msg->flags,
  260. IWPM_NLA_QUERY_FLAGS);
  261. if (ret)
  262. goto query_mapping_error;
  263. }
  264. nlmsg_end(skb, nlh);
  265. nlmsg_request->req_buffer = pm_msg;
  266. ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
  267. if (ret) {
  268. skb = NULL; /* skb is freed in the netlink send-op handling */
  269. err_str = "Unable to send a nlmsg";
  270. goto query_mapping_error;
  271. }
  272. ret = iwpm_wait_complete_req(nlmsg_request);
  273. return ret;
  274. query_mapping_error:
  275. pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
  276. query_mapping_error_nowarn:
  277. dev_kfree_skb(skb);
  278. if (nlmsg_request)
  279. iwpm_free_nlmsg_request(&nlmsg_request->kref);
  280. return ret;
  281. }
  282. /**
  283. * iwpm_remove_mapping - Send a netlink remove mapping request
  284. * to the userspace port mapper
  285. *
  286. * @local_addr: Local ip/tcp address to remove
  287. * @nl_client: The index of the netlink client
  288. *
  289. * nlmsg attributes:
  290. * [IWPM_NLA_MANAGE_MAPPING_SEQ]
  291. * [IWPM_NLA_MANAGE_ADDR]
  292. */
  293. int iwpm_remove_mapping(struct sockaddr_storage *local_addr, u8 nl_client)
  294. {
  295. struct sk_buff *skb = NULL;
  296. struct nlmsghdr *nlh;
  297. u32 msg_seq;
  298. const char *err_str = "";
  299. int ret = -EINVAL;
  300. if (!iwpm_valid_pid())
  301. return 0;
  302. if (iwpm_check_registration(nl_client, IWPM_REG_UNDEF)) {
  303. err_str = "Unregistered port mapper client";
  304. goto remove_mapping_error;
  305. }
  306. skb = iwpm_create_nlmsg(RDMA_NL_IWPM_REMOVE_MAPPING, &nlh, nl_client);
  307. if (!skb) {
  308. ret = -ENOMEM;
  309. err_str = "Unable to create a nlmsg";
  310. goto remove_mapping_error;
  311. }
  312. msg_seq = atomic_read(&echo_nlmsg_seq);
  313. nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
  314. err_str = "Unable to put attribute of the nlmsg";
  315. ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq,
  316. IWPM_NLA_MANAGE_MAPPING_SEQ);
  317. if (ret)
  318. goto remove_mapping_error;
  319. ret = ibnl_put_attr(skb, nlh, sizeof(struct sockaddr_storage),
  320. local_addr, IWPM_NLA_MANAGE_ADDR);
  321. if (ret)
  322. goto remove_mapping_error;
  323. nlmsg_end(skb, nlh);
  324. ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
  325. if (ret) {
  326. skb = NULL; /* skb is freed in the netlink send-op handling */
  327. iwpm_user_pid = IWPM_PID_UNDEFINED;
  328. err_str = "Unable to send a nlmsg";
  329. goto remove_mapping_error;
  330. }
  331. iwpm_print_sockaddr(local_addr,
  332. "remove_mapping: Local sockaddr:");
  333. return 0;
  334. remove_mapping_error:
  335. pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
  336. if (skb)
  337. dev_kfree_skb_any(skb);
  338. return ret;
  339. }
  340. /* netlink attribute policy for the received response to register pid request */
  341. static const struct nla_policy resp_reg_policy[IWPM_NLA_RREG_PID_MAX] = {
  342. [IWPM_NLA_RREG_PID_SEQ] = { .type = NLA_U32 },
  343. [IWPM_NLA_RREG_IBDEV_NAME] = { .type = NLA_STRING,
  344. .len = IWPM_DEVNAME_SIZE - 1 },
  345. [IWPM_NLA_RREG_ULIB_NAME] = { .type = NLA_STRING,
  346. .len = IWPM_ULIBNAME_SIZE - 1 },
  347. [IWPM_NLA_RREG_ULIB_VER] = { .type = NLA_U16 },
  348. [IWPM_NLA_RREG_PID_ERR] = { .type = NLA_U16 }
  349. };
  350. /**
  351. * iwpm_register_pid_cb - Process the port mapper response to
  352. * iwpm_register_pid query
  353. * @skb: The socket buffer
  354. * @cb: Contains the received message (payload and netlink header)
  355. *
  356. * If successful, the function receives the userspace port mapper pid
  357. * which is used in future communication with the port mapper
  358. */
  359. int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
  360. {
  361. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  362. struct nlattr *nltb[IWPM_NLA_RREG_PID_MAX];
  363. struct iwpm_dev_data *pm_msg;
  364. char *dev_name, *iwpm_name;
  365. u32 msg_seq;
  366. u8 nl_client;
  367. u16 iwpm_version;
  368. const char *msg_type = "Register Pid response";
  369. if (iwpm_parse_nlmsg(cb, IWPM_NLA_RREG_PID_MAX,
  370. resp_reg_policy, nltb, msg_type))
  371. return -EINVAL;
  372. msg_seq = nla_get_u32(nltb[IWPM_NLA_RREG_PID_SEQ]);
  373. nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
  374. if (!nlmsg_request) {
  375. pr_info("%s: Could not find a matching request (seq = %u)\n",
  376. __func__, msg_seq);
  377. return -EINVAL;
  378. }
  379. pm_msg = nlmsg_request->req_buffer;
  380. nl_client = nlmsg_request->nl_client;
  381. dev_name = (char *)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]);
  382. iwpm_name = (char *)nla_data(nltb[IWPM_NLA_RREG_ULIB_NAME]);
  383. iwpm_version = nla_get_u16(nltb[IWPM_NLA_RREG_ULIB_VER]);
  384. /* check device name, ulib name and version */
  385. if (strcmp(pm_msg->dev_name, dev_name) ||
  386. strcmp(iwpm_ulib_name, iwpm_name) ||
  387. iwpm_version < IWPM_UABI_VERSION_MIN) {
  388. pr_info("%s: Incorrect info (dev = %s name = %s version = %u)\n",
  389. __func__, dev_name, iwpm_name, iwpm_version);
  390. nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
  391. goto register_pid_response_exit;
  392. }
  393. iwpm_user_pid = cb->nlh->nlmsg_pid;
  394. iwpm_ulib_version = iwpm_version;
  395. if (iwpm_ulib_version < IWPM_UABI_VERSION)
  396. pr_warn_once("%s: Down level iwpmd/pid %d. Continuing...",
  397. __func__, iwpm_user_pid);
  398. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  399. pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n",
  400. __func__, iwpm_user_pid);
  401. iwpm_set_registration(nl_client, IWPM_REG_VALID);
  402. register_pid_response_exit:
  403. nlmsg_request->request_done = 1;
  404. /* always for found nlmsg_request */
  405. kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
  406. barrier();
  407. up(&nlmsg_request->sem);
  408. return 0;
  409. }
  410. /* netlink attribute policy for the received response to add mapping request */
  411. static const struct nla_policy resp_add_policy[IWPM_NLA_RMANAGE_MAPPING_MAX] = {
  412. [IWPM_NLA_RMANAGE_MAPPING_SEQ] = { .type = NLA_U32 },
  413. [IWPM_NLA_RMANAGE_ADDR] = {
  414. .len = sizeof(struct sockaddr_storage) },
  415. [IWPM_NLA_RMANAGE_MAPPED_LOC_ADDR] = {
  416. .len = sizeof(struct sockaddr_storage) },
  417. [IWPM_NLA_RMANAGE_MAPPING_ERR] = { .type = NLA_U16 }
  418. };
  419. /**
  420. * iwpm_add_mapping_cb - Process the port mapper response to
  421. * iwpm_add_mapping request
  422. * @skb: The socket buffer
  423. * @cb: Contains the received message (payload and netlink header)
  424. */
  425. int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb)
  426. {
  427. struct iwpm_sa_data *pm_msg;
  428. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  429. struct nlattr *nltb[IWPM_NLA_RMANAGE_MAPPING_MAX];
  430. struct sockaddr_storage *local_sockaddr;
  431. struct sockaddr_storage *mapped_sockaddr;
  432. const char *msg_type;
  433. u32 msg_seq;
  434. msg_type = "Add Mapping response";
  435. if (iwpm_parse_nlmsg(cb, IWPM_NLA_RMANAGE_MAPPING_MAX,
  436. resp_add_policy, nltb, msg_type))
  437. return -EINVAL;
  438. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  439. msg_seq = nla_get_u32(nltb[IWPM_NLA_RMANAGE_MAPPING_SEQ]);
  440. nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
  441. if (!nlmsg_request) {
  442. pr_info("%s: Could not find a matching request (seq = %u)\n",
  443. __func__, msg_seq);
  444. return -EINVAL;
  445. }
  446. pm_msg = nlmsg_request->req_buffer;
  447. local_sockaddr = (struct sockaddr_storage *)
  448. nla_data(nltb[IWPM_NLA_RMANAGE_ADDR]);
  449. mapped_sockaddr = (struct sockaddr_storage *)
  450. nla_data(nltb[IWPM_NLA_RMANAGE_MAPPED_LOC_ADDR]);
  451. if (iwpm_compare_sockaddr(local_sockaddr, &pm_msg->loc_addr)) {
  452. nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
  453. goto add_mapping_response_exit;
  454. }
  455. if (mapped_sockaddr->ss_family != local_sockaddr->ss_family) {
  456. pr_info("%s: Sockaddr family doesn't match the requested one\n",
  457. __func__);
  458. nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
  459. goto add_mapping_response_exit;
  460. }
  461. memcpy(&pm_msg->mapped_loc_addr, mapped_sockaddr,
  462. sizeof(*mapped_sockaddr));
  463. iwpm_print_sockaddr(&pm_msg->loc_addr,
  464. "add_mapping: Local sockaddr:");
  465. iwpm_print_sockaddr(&pm_msg->mapped_loc_addr,
  466. "add_mapping: Mapped local sockaddr:");
  467. add_mapping_response_exit:
  468. nlmsg_request->request_done = 1;
  469. /* always for found request */
  470. kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
  471. barrier();
  472. up(&nlmsg_request->sem);
  473. return 0;
  474. }
  475. /* netlink attribute policy for the response to add and query mapping request
  476. * and response with remote address info
  477. */
  478. static const struct nla_policy resp_query_policy[IWPM_NLA_RQUERY_MAPPING_MAX] = {
  479. [IWPM_NLA_RQUERY_MAPPING_SEQ] = { .type = NLA_U32 },
  480. [IWPM_NLA_RQUERY_LOCAL_ADDR] = {
  481. .len = sizeof(struct sockaddr_storage) },
  482. [IWPM_NLA_RQUERY_REMOTE_ADDR] = {
  483. .len = sizeof(struct sockaddr_storage) },
  484. [IWPM_NLA_RQUERY_MAPPED_LOC_ADDR] = {
  485. .len = sizeof(struct sockaddr_storage) },
  486. [IWPM_NLA_RQUERY_MAPPED_REM_ADDR] = {
  487. .len = sizeof(struct sockaddr_storage) },
  488. [IWPM_NLA_RQUERY_MAPPING_ERR] = { .type = NLA_U16 }
  489. };
  490. /**
  491. * iwpm_add_and_query_mapping_cb - Process the port mapper response to
  492. * iwpm_add_and_query_mapping request
  493. * @skb: The socket buffer
  494. * @cb: Contains the received message (payload and netlink header)
  495. */
  496. int iwpm_add_and_query_mapping_cb(struct sk_buff *skb,
  497. struct netlink_callback *cb)
  498. {
  499. struct iwpm_sa_data *pm_msg;
  500. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  501. struct nlattr *nltb[IWPM_NLA_RQUERY_MAPPING_MAX];
  502. struct sockaddr_storage *local_sockaddr, *remote_sockaddr;
  503. struct sockaddr_storage *mapped_loc_sockaddr, *mapped_rem_sockaddr;
  504. const char *msg_type;
  505. u32 msg_seq;
  506. u16 err_code;
  507. msg_type = "Query Mapping response";
  508. if (iwpm_parse_nlmsg(cb, IWPM_NLA_RQUERY_MAPPING_MAX,
  509. resp_query_policy, nltb, msg_type))
  510. return -EINVAL;
  511. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  512. msg_seq = nla_get_u32(nltb[IWPM_NLA_RQUERY_MAPPING_SEQ]);
  513. nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
  514. if (!nlmsg_request) {
  515. pr_info("%s: Could not find a matching request (seq = %u)\n",
  516. __func__, msg_seq);
  517. return -EINVAL;
  518. }
  519. pm_msg = nlmsg_request->req_buffer;
  520. local_sockaddr = (struct sockaddr_storage *)
  521. nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
  522. remote_sockaddr = (struct sockaddr_storage *)
  523. nla_data(nltb[IWPM_NLA_RQUERY_REMOTE_ADDR]);
  524. mapped_loc_sockaddr = (struct sockaddr_storage *)
  525. nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_LOC_ADDR]);
  526. mapped_rem_sockaddr = (struct sockaddr_storage *)
  527. nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_REM_ADDR]);
  528. err_code = nla_get_u16(nltb[IWPM_NLA_RQUERY_MAPPING_ERR]);
  529. if (err_code == IWPM_REMOTE_QUERY_REJECT) {
  530. pr_info("%s: Received a Reject (pid = %u, echo seq = %u)\n",
  531. __func__, cb->nlh->nlmsg_pid, msg_seq);
  532. nlmsg_request->err_code = IWPM_REMOTE_QUERY_REJECT;
  533. }
  534. if (iwpm_compare_sockaddr(local_sockaddr, &pm_msg->loc_addr) ||
  535. iwpm_compare_sockaddr(remote_sockaddr, &pm_msg->rem_addr)) {
  536. pr_info("%s: Incorrect local sockaddr\n", __func__);
  537. nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
  538. goto query_mapping_response_exit;
  539. }
  540. if (mapped_loc_sockaddr->ss_family != local_sockaddr->ss_family ||
  541. mapped_rem_sockaddr->ss_family != remote_sockaddr->ss_family) {
  542. pr_info("%s: Sockaddr family doesn't match the requested one\n",
  543. __func__);
  544. nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
  545. goto query_mapping_response_exit;
  546. }
  547. memcpy(&pm_msg->mapped_loc_addr, mapped_loc_sockaddr,
  548. sizeof(*mapped_loc_sockaddr));
  549. memcpy(&pm_msg->mapped_rem_addr, mapped_rem_sockaddr,
  550. sizeof(*mapped_rem_sockaddr));
  551. iwpm_print_sockaddr(&pm_msg->loc_addr,
  552. "query_mapping: Local sockaddr:");
  553. iwpm_print_sockaddr(&pm_msg->mapped_loc_addr,
  554. "query_mapping: Mapped local sockaddr:");
  555. iwpm_print_sockaddr(&pm_msg->rem_addr,
  556. "query_mapping: Remote sockaddr:");
  557. iwpm_print_sockaddr(&pm_msg->mapped_rem_addr,
  558. "query_mapping: Mapped remote sockaddr:");
  559. query_mapping_response_exit:
  560. nlmsg_request->request_done = 1;
  561. /* always for found request */
  562. kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
  563. barrier();
  564. up(&nlmsg_request->sem);
  565. return 0;
  566. }
  567. /**
  568. * iwpm_remote_info_cb - Process remote connecting peer address info, which
  569. * the port mapper has received from the connecting peer
  570. * @skb: The socket buffer
  571. * @cb: Contains the received message (payload and netlink header)
  572. *
  573. * Stores the IPv4/IPv6 address info in a hash table
  574. */
  575. int iwpm_remote_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
  576. {
  577. struct nlattr *nltb[IWPM_NLA_RQUERY_MAPPING_MAX];
  578. struct sockaddr_storage *local_sockaddr, *remote_sockaddr;
  579. struct sockaddr_storage *mapped_loc_sockaddr, *mapped_rem_sockaddr;
  580. struct iwpm_remote_info *rem_info;
  581. const char *msg_type;
  582. u8 nl_client;
  583. int ret = -EINVAL;
  584. msg_type = "Remote Mapping info";
  585. if (iwpm_parse_nlmsg(cb, IWPM_NLA_RQUERY_MAPPING_MAX,
  586. resp_query_policy, nltb, msg_type))
  587. return ret;
  588. nl_client = RDMA_NL_GET_CLIENT(cb->nlh->nlmsg_type);
  589. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  590. local_sockaddr = (struct sockaddr_storage *)
  591. nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
  592. remote_sockaddr = (struct sockaddr_storage *)
  593. nla_data(nltb[IWPM_NLA_RQUERY_REMOTE_ADDR]);
  594. mapped_loc_sockaddr = (struct sockaddr_storage *)
  595. nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_LOC_ADDR]);
  596. mapped_rem_sockaddr = (struct sockaddr_storage *)
  597. nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_REM_ADDR]);
  598. if (mapped_loc_sockaddr->ss_family != local_sockaddr->ss_family ||
  599. mapped_rem_sockaddr->ss_family != remote_sockaddr->ss_family) {
  600. pr_info("%s: Sockaddr family doesn't match the requested one\n",
  601. __func__);
  602. return ret;
  603. }
  604. rem_info = kzalloc(sizeof(struct iwpm_remote_info), GFP_ATOMIC);
  605. if (!rem_info) {
  606. ret = -ENOMEM;
  607. return ret;
  608. }
  609. memcpy(&rem_info->mapped_loc_sockaddr, mapped_loc_sockaddr,
  610. sizeof(struct sockaddr_storage));
  611. memcpy(&rem_info->remote_sockaddr, remote_sockaddr,
  612. sizeof(struct sockaddr_storage));
  613. memcpy(&rem_info->mapped_rem_sockaddr, mapped_rem_sockaddr,
  614. sizeof(struct sockaddr_storage));
  615. rem_info->nl_client = nl_client;
  616. iwpm_add_remote_info(rem_info);
  617. iwpm_print_sockaddr(local_sockaddr,
  618. "remote_info: Local sockaddr:");
  619. iwpm_print_sockaddr(mapped_loc_sockaddr,
  620. "remote_info: Mapped local sockaddr:");
  621. iwpm_print_sockaddr(remote_sockaddr,
  622. "remote_info: Remote sockaddr:");
  623. iwpm_print_sockaddr(mapped_rem_sockaddr,
  624. "remote_info: Mapped remote sockaddr:");
  625. return ret;
  626. }
  627. /* netlink attribute policy for the received request for mapping info */
  628. static const struct nla_policy resp_mapinfo_policy[IWPM_NLA_MAPINFO_REQ_MAX] = {
  629. [IWPM_NLA_MAPINFO_ULIB_NAME] = { .type = NLA_STRING,
  630. .len = IWPM_ULIBNAME_SIZE - 1 },
  631. [IWPM_NLA_MAPINFO_ULIB_VER] = { .type = NLA_U16 }
  632. };
  633. /**
  634. * iwpm_mapping_info_cb - Process a notification that the userspace
  635. * port mapper daemon is started
  636. * @skb: The socket buffer
  637. * @cb: Contains the received message (payload and netlink header)
  638. *
  639. * Using the received port mapper pid, send all the local mapping
  640. * info records to the userspace port mapper
  641. */
  642. int iwpm_mapping_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
  643. {
  644. struct nlattr *nltb[IWPM_NLA_MAPINFO_REQ_MAX];
  645. const char *msg_type = "Mapping Info response";
  646. u8 nl_client;
  647. char *iwpm_name;
  648. u16 iwpm_version;
  649. int ret = -EINVAL;
  650. if (iwpm_parse_nlmsg(cb, IWPM_NLA_MAPINFO_REQ_MAX,
  651. resp_mapinfo_policy, nltb, msg_type)) {
  652. pr_info("%s: Unable to parse nlmsg\n", __func__);
  653. return ret;
  654. }
  655. iwpm_name = (char *)nla_data(nltb[IWPM_NLA_MAPINFO_ULIB_NAME]);
  656. iwpm_version = nla_get_u16(nltb[IWPM_NLA_MAPINFO_ULIB_VER]);
  657. if (strcmp(iwpm_ulib_name, iwpm_name) ||
  658. iwpm_version < IWPM_UABI_VERSION_MIN) {
  659. pr_info("%s: Invalid port mapper name = %s version = %u\n",
  660. __func__, iwpm_name, iwpm_version);
  661. return ret;
  662. }
  663. nl_client = RDMA_NL_GET_CLIENT(cb->nlh->nlmsg_type);
  664. iwpm_set_registration(nl_client, IWPM_REG_INCOMPL);
  665. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  666. iwpm_user_pid = cb->nlh->nlmsg_pid;
  667. if (iwpm_ulib_version < IWPM_UABI_VERSION)
  668. pr_warn_once("%s: Down level iwpmd/pid %d. Continuing...",
  669. __func__, iwpm_user_pid);
  670. if (!iwpm_mapinfo_available())
  671. return 0;
  672. pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n",
  673. __func__, iwpm_user_pid);
  674. ret = iwpm_send_mapinfo(nl_client, iwpm_user_pid);
  675. return ret;
  676. }
  677. /* netlink attribute policy for the received mapping info ack */
  678. static const struct nla_policy ack_mapinfo_policy[IWPM_NLA_MAPINFO_NUM_MAX] = {
  679. [IWPM_NLA_MAPINFO_SEQ] = { .type = NLA_U32 },
  680. [IWPM_NLA_MAPINFO_SEND_NUM] = { .type = NLA_U32 },
  681. [IWPM_NLA_MAPINFO_ACK_NUM] = { .type = NLA_U32 }
  682. };
  683. /**
  684. * iwpm_ack_mapping_info_cb - Process the port mapper ack for
  685. * the provided local mapping info records
  686. * @skb: The socket buffer
  687. * @cb: Contains the received message (payload and netlink header)
  688. */
  689. int iwpm_ack_mapping_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
  690. {
  691. struct nlattr *nltb[IWPM_NLA_MAPINFO_NUM_MAX];
  692. u32 mapinfo_send, mapinfo_ack;
  693. const char *msg_type = "Mapping Info Ack";
  694. if (iwpm_parse_nlmsg(cb, IWPM_NLA_MAPINFO_NUM_MAX,
  695. ack_mapinfo_policy, nltb, msg_type))
  696. return -EINVAL;
  697. mapinfo_send = nla_get_u32(nltb[IWPM_NLA_MAPINFO_SEND_NUM]);
  698. mapinfo_ack = nla_get_u32(nltb[IWPM_NLA_MAPINFO_ACK_NUM]);
  699. if (mapinfo_ack != mapinfo_send)
  700. pr_info("%s: Invalid mapinfo number (sent = %u ack-ed = %u)\n",
  701. __func__, mapinfo_send, mapinfo_ack);
  702. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  703. return 0;
  704. }
  705. /* netlink attribute policy for the received port mapper error message */
  706. static const struct nla_policy map_error_policy[IWPM_NLA_ERR_MAX] = {
  707. [IWPM_NLA_ERR_SEQ] = { .type = NLA_U32 },
  708. [IWPM_NLA_ERR_CODE] = { .type = NLA_U16 },
  709. };
  710. /**
  711. * iwpm_mapping_error_cb - Process port mapper notification for error
  712. *
  713. * @skb: The socket buffer
  714. * @cb: Contains the received message (payload and netlink header)
  715. */
  716. int iwpm_mapping_error_cb(struct sk_buff *skb, struct netlink_callback *cb)
  717. {
  718. struct iwpm_nlmsg_request *nlmsg_request = NULL;
  719. int nl_client = RDMA_NL_GET_CLIENT(cb->nlh->nlmsg_type);
  720. struct nlattr *nltb[IWPM_NLA_ERR_MAX];
  721. u32 msg_seq;
  722. u16 err_code;
  723. const char *msg_type = "Mapping Error Msg";
  724. if (iwpm_parse_nlmsg(cb, IWPM_NLA_ERR_MAX,
  725. map_error_policy, nltb, msg_type))
  726. return -EINVAL;
  727. msg_seq = nla_get_u32(nltb[IWPM_NLA_ERR_SEQ]);
  728. err_code = nla_get_u16(nltb[IWPM_NLA_ERR_CODE]);
  729. pr_info("%s: Received msg seq = %u err code = %u client = %d\n",
  730. __func__, msg_seq, err_code, nl_client);
  731. /* look for nlmsg_request */
  732. nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
  733. if (!nlmsg_request) {
  734. /* not all errors have associated requests */
  735. pr_debug("Could not find matching req (seq = %u)\n", msg_seq);
  736. return 0;
  737. }
  738. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  739. nlmsg_request->err_code = err_code;
  740. nlmsg_request->request_done = 1;
  741. /* always for found request */
  742. kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
  743. barrier();
  744. up(&nlmsg_request->sem);
  745. return 0;
  746. }
  747. /* netlink attribute policy for the received hello request */
  748. static const struct nla_policy hello_policy[IWPM_NLA_HELLO_MAX] = {
  749. [IWPM_NLA_HELLO_ABI_VERSION] = { .type = NLA_U16 }
  750. };
  751. /**
  752. * iwpm_hello_cb - Process a hello message from iwpmd
  753. *
  754. * @skb: The socket buffer
  755. * @cb: Contains the received message (payload and netlink header)
  756. *
  757. * Using the received port mapper pid, send the kernel's abi_version
  758. * after adjusting it to support the iwpmd version.
  759. */
  760. int iwpm_hello_cb(struct sk_buff *skb, struct netlink_callback *cb)
  761. {
  762. struct nlattr *nltb[IWPM_NLA_HELLO_MAX];
  763. const char *msg_type = "Hello request";
  764. u8 nl_client;
  765. u16 abi_version;
  766. int ret = -EINVAL;
  767. if (iwpm_parse_nlmsg(cb, IWPM_NLA_HELLO_MAX, hello_policy, nltb,
  768. msg_type)) {
  769. pr_info("%s: Unable to parse nlmsg\n", __func__);
  770. return ret;
  771. }
  772. abi_version = nla_get_u16(nltb[IWPM_NLA_HELLO_ABI_VERSION]);
  773. nl_client = RDMA_NL_GET_CLIENT(cb->nlh->nlmsg_type);
  774. iwpm_set_registration(nl_client, IWPM_REG_INCOMPL);
  775. atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
  776. iwpm_ulib_version = min_t(u16, IWPM_UABI_VERSION, abi_version);
  777. pr_debug("Using ABI version %u\n", iwpm_ulib_version);
  778. iwpm_user_pid = cb->nlh->nlmsg_pid;
  779. ret = iwpm_send_hello(nl_client, iwpm_user_pid, iwpm_ulib_version);
  780. return ret;
  781. }