forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-09 244b2c5ca8b14627e4a17755e5922221e121c771
kernel/drivers/infiniband/core/iwpm_msg.c
....@@ -34,18 +34,25 @@
3434 #include "iwpm_util.h"
3535
3636 static const char iwpm_ulib_name[IWPM_ULIBNAME_SIZE] = "iWarpPortMapperUser";
37
-static int iwpm_ulib_version = 3;
37
+u16 iwpm_ulib_version = IWPM_UABI_VERSION_MIN;
3838 static int iwpm_user_pid = IWPM_PID_UNDEFINED;
3939 static atomic_t echo_nlmsg_seq;
4040
41
+/**
42
+ * iwpm_valid_pid - Check if the userspace iwarp port mapper pid is valid
43
+ *
44
+ * Returns true if the pid is greater than zero, otherwise returns false
45
+ */
4146 int iwpm_valid_pid(void)
4247 {
4348 return iwpm_user_pid > 0;
4449 }
4550
46
-/*
47
- * iwpm_register_pid - Send a netlink query to user space
48
- * for the iwarp port mapper pid
51
+/**
52
+ * iwpm_register_pid - Send a netlink query to userspace
53
+ * to get the iwarp port mapper pid
54
+ * @pm_msg: Contains driver info to send to the userspace port mapper
55
+ * @nl_client: The index of the netlink client
4956 *
5057 * nlmsg attributes:
5158 * [IWPM_NLA_REG_PID_SEQ]
....@@ -105,7 +112,7 @@
105112 pr_debug("%s: Multicasting a nlmsg (dev = %s ifname = %s iwpm = %s)\n",
106113 __func__, pm_msg->dev_name, pm_msg->if_name, iwpm_ulib_name);
107114
108
- ret = rdma_nl_multicast(skb, RDMA_NL_GROUP_IWPM, GFP_KERNEL);
115
+ ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_IWPM, GFP_KERNEL);
109116 if (ret) {
110117 skb = NULL; /* skb is freed in the netlink send-op handling */
111118 iwpm_user_pid = IWPM_PID_UNAVAILABLE;
....@@ -117,19 +124,25 @@
117124 return ret;
118125 pid_query_error:
119126 pr_info("%s: %s (client = %d)\n", __func__, err_str, nl_client);
120
- if (skb)
121
- dev_kfree_skb(skb);
127
+ dev_kfree_skb(skb);
122128 if (nlmsg_request)
123129 iwpm_free_nlmsg_request(&nlmsg_request->kref);
124130 return ret;
125131 }
126132
127
-/*
128
- * iwpm_add_mapping - Send a netlink add mapping message
129
- * to the port mapper
133
+/**
134
+ * iwpm_add_mapping - Send a netlink add mapping request to
135
+ * the userspace port mapper
136
+ * @pm_msg: Contains the local ip/tcp address info to send
137
+ * @nl_client: The index of the netlink client
138
+ *
130139 * nlmsg attributes:
131140 * [IWPM_NLA_MANAGE_MAPPING_SEQ]
132141 * [IWPM_NLA_MANAGE_ADDR]
142
+ * [IWPM_NLA_MANAGE_FLAGS]
143
+ *
144
+ * If the request is successful, the pm_msg stores
145
+ * the port mapper response (mapped address info)
133146 */
134147 int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
135148 {
....@@ -173,10 +186,22 @@
173186 if (ret)
174187 goto add_mapping_error;
175188
189
+ /* If flags are required and we're not V4, then return a quiet error */
190
+ if (pm_msg->flags && iwpm_ulib_version == IWPM_UABI_VERSION_MIN) {
191
+ ret = -EINVAL;
192
+ goto add_mapping_error_nowarn;
193
+ }
194
+ if (iwpm_ulib_version > IWPM_UABI_VERSION_MIN) {
195
+ ret = ibnl_put_attr(skb, nlh, sizeof(u32), &pm_msg->flags,
196
+ IWPM_NLA_MANAGE_FLAGS);
197
+ if (ret)
198
+ goto add_mapping_error;
199
+ }
200
+
176201 nlmsg_end(skb, nlh);
177202 nlmsg_request->req_buffer = pm_msg;
178203
179
- ret = rdma_nl_unicast_wait(skb, iwpm_user_pid);
204
+ ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
180205 if (ret) {
181206 skb = NULL; /* skb is freed in the netlink send-op handling */
182207 iwpm_user_pid = IWPM_PID_UNDEFINED;
....@@ -187,20 +212,24 @@
187212 return ret;
188213 add_mapping_error:
189214 pr_info("%s: %s (client = %d)\n", __func__, err_str, nl_client);
190
- if (skb)
191
- dev_kfree_skb(skb);
215
+add_mapping_error_nowarn:
216
+ dev_kfree_skb(skb);
192217 if (nlmsg_request)
193218 iwpm_free_nlmsg_request(&nlmsg_request->kref);
194219 return ret;
195220 }
196221
197
-/*
198
- * iwpm_add_and_query_mapping - Send a netlink add and query
199
- * mapping message to the port mapper
222
+/**
223
+ * iwpm_add_and_query_mapping - Process the port mapper response to
224
+ * iwpm_add_and_query_mapping request
225
+ * @pm_msg: Contains the local ip/tcp address info to send
226
+ * @nl_client: The index of the netlink client
227
+ *
200228 * nlmsg attributes:
201229 * [IWPM_NLA_QUERY_MAPPING_SEQ]
202230 * [IWPM_NLA_QUERY_LOCAL_ADDR]
203231 * [IWPM_NLA_QUERY_REMOTE_ADDR]
232
+ * [IWPM_NLA_QUERY_FLAGS]
204233 */
205234 int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
206235 {
....@@ -251,10 +280,22 @@
251280 if (ret)
252281 goto query_mapping_error;
253282
283
+ /* If flags are required and we're not V4, then return a quite error */
284
+ if (pm_msg->flags && iwpm_ulib_version == IWPM_UABI_VERSION_MIN) {
285
+ ret = -EINVAL;
286
+ goto query_mapping_error_nowarn;
287
+ }
288
+ if (iwpm_ulib_version > IWPM_UABI_VERSION_MIN) {
289
+ ret = ibnl_put_attr(skb, nlh, sizeof(u32), &pm_msg->flags,
290
+ IWPM_NLA_QUERY_FLAGS);
291
+ if (ret)
292
+ goto query_mapping_error;
293
+ }
294
+
254295 nlmsg_end(skb, nlh);
255296 nlmsg_request->req_buffer = pm_msg;
256297
257
- ret = rdma_nl_unicast_wait(skb, iwpm_user_pid);
298
+ ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
258299 if (ret) {
259300 skb = NULL; /* skb is freed in the netlink send-op handling */
260301 err_str = "Unable to send a nlmsg";
....@@ -264,16 +305,20 @@
264305 return ret;
265306 query_mapping_error:
266307 pr_info("%s: %s (client = %d)\n", __func__, err_str, nl_client);
267
- if (skb)
268
- dev_kfree_skb(skb);
308
+query_mapping_error_nowarn:
309
+ dev_kfree_skb(skb);
269310 if (nlmsg_request)
270311 iwpm_free_nlmsg_request(&nlmsg_request->kref);
271312 return ret;
272313 }
273314
274
-/*
275
- * iwpm_remove_mapping - Send a netlink remove mapping message
276
- * to the port mapper
315
+/**
316
+ * iwpm_remove_mapping - Send a netlink remove mapping request
317
+ * to the userspace port mapper
318
+ *
319
+ * @local_addr: Local ip/tcp address to remove
320
+ * @nl_client: The index of the netlink client
321
+ *
277322 * nlmsg attributes:
278323 * [IWPM_NLA_MANAGE_MAPPING_SEQ]
279324 * [IWPM_NLA_MANAGE_ADDR]
....@@ -316,7 +361,7 @@
316361
317362 nlmsg_end(skb, nlh);
318363
319
- ret = rdma_nl_unicast_wait(skb, iwpm_user_pid);
364
+ ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
320365 if (ret) {
321366 skb = NULL; /* skb is freed in the netlink send-op handling */
322367 iwpm_user_pid = IWPM_PID_UNDEFINED;
....@@ -344,9 +389,14 @@
344389 [IWPM_NLA_RREG_PID_ERR] = { .type = NLA_U16 }
345390 };
346391
347
-/*
348
- * iwpm_register_pid_cb - Process a port mapper response to
349
- * iwpm_register_pid()
392
+/**
393
+ * iwpm_register_pid_cb - Process the port mapper response to
394
+ * iwpm_register_pid query
395
+ * @skb:
396
+ * @cb: Contains the received message (payload and netlink header)
397
+ *
398
+ * If successful, the function receives the userspace port mapper pid
399
+ * which is used in future communication with the port mapper
350400 */
351401 int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
352402 {
....@@ -379,7 +429,7 @@
379429 /* check device name, ulib name and version */
380430 if (strcmp(pm_msg->dev_name, dev_name) ||
381431 strcmp(iwpm_ulib_name, iwpm_name) ||
382
- iwpm_version != iwpm_ulib_version) {
432
+ iwpm_version < IWPM_UABI_VERSION_MIN) {
383433
384434 pr_info("%s: Incorrect info (dev = %s name = %s version = %d)\n",
385435 __func__, dev_name, iwpm_name, iwpm_version);
....@@ -387,6 +437,10 @@
387437 goto register_pid_response_exit;
388438 }
389439 iwpm_user_pid = cb->nlh->nlmsg_pid;
440
+ iwpm_ulib_version = iwpm_version;
441
+ if (iwpm_ulib_version < IWPM_UABI_VERSION)
442
+ pr_warn_once("%s: Down level iwpmd/pid %u. Continuing...",
443
+ __func__, iwpm_user_pid);
390444 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
391445 pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n",
392446 __func__, iwpm_user_pid);
....@@ -403,15 +457,19 @@
403457
404458 /* netlink attribute policy for the received response to add mapping request */
405459 static const struct nla_policy resp_add_policy[IWPM_NLA_RMANAGE_MAPPING_MAX] = {
406
- [IWPM_NLA_MANAGE_MAPPING_SEQ] = { .type = NLA_U32 },
407
- [IWPM_NLA_MANAGE_ADDR] = { .len = sizeof(struct sockaddr_storage) },
408
- [IWPM_NLA_MANAGE_MAPPED_LOC_ADDR] = { .len = sizeof(struct sockaddr_storage) },
409
- [IWPM_NLA_RMANAGE_MAPPING_ERR] = { .type = NLA_U16 }
460
+ [IWPM_NLA_RMANAGE_MAPPING_SEQ] = { .type = NLA_U32 },
461
+ [IWPM_NLA_RMANAGE_ADDR] = {
462
+ .len = sizeof(struct sockaddr_storage) },
463
+ [IWPM_NLA_RMANAGE_MAPPED_LOC_ADDR] = {
464
+ .len = sizeof(struct sockaddr_storage) },
465
+ [IWPM_NLA_RMANAGE_MAPPING_ERR] = { .type = NLA_U16 }
410466 };
411467
412
-/*
413
- * iwpm_add_mapping_cb - Process a port mapper response to
414
- * iwpm_add_mapping()
468
+/**
469
+ * iwpm_add_mapping_cb - Process the port mapper response to
470
+ * iwpm_add_mapping request
471
+ * @skb:
472
+ * @cb: Contains the received message (payload and netlink header)
415473 */
416474 int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb)
417475 {
....@@ -430,7 +488,7 @@
430488
431489 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
432490
433
- msg_seq = nla_get_u32(nltb[IWPM_NLA_MANAGE_MAPPING_SEQ]);
491
+ msg_seq = nla_get_u32(nltb[IWPM_NLA_RMANAGE_MAPPING_SEQ]);
434492 nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
435493 if (!nlmsg_request) {
436494 pr_info("%s: Could not find a matching request (seq = %u)\n",
....@@ -439,9 +497,9 @@
439497 }
440498 pm_msg = nlmsg_request->req_buffer;
441499 local_sockaddr = (struct sockaddr_storage *)
442
- nla_data(nltb[IWPM_NLA_MANAGE_ADDR]);
500
+ nla_data(nltb[IWPM_NLA_RMANAGE_ADDR]);
443501 mapped_sockaddr = (struct sockaddr_storage *)
444
- nla_data(nltb[IWPM_NLA_MANAGE_MAPPED_LOC_ADDR]);
502
+ nla_data(nltb[IWPM_NLA_RMANAGE_MAPPED_LOC_ADDR]);
445503
446504 if (iwpm_compare_sockaddr(local_sockaddr, &pm_msg->loc_addr)) {
447505 nlmsg_request->err_code = IWPM_USER_LIB_INFO_ERR;
....@@ -472,17 +530,23 @@
472530 /* netlink attribute policy for the response to add and query mapping request
473531 * and response with remote address info */
474532 static const struct nla_policy resp_query_policy[IWPM_NLA_RQUERY_MAPPING_MAX] = {
475
- [IWPM_NLA_QUERY_MAPPING_SEQ] = { .type = NLA_U32 },
476
- [IWPM_NLA_QUERY_LOCAL_ADDR] = { .len = sizeof(struct sockaddr_storage) },
477
- [IWPM_NLA_QUERY_REMOTE_ADDR] = { .len = sizeof(struct sockaddr_storage) },
478
- [IWPM_NLA_RQUERY_MAPPED_LOC_ADDR] = { .len = sizeof(struct sockaddr_storage) },
479
- [IWPM_NLA_RQUERY_MAPPED_REM_ADDR] = { .len = sizeof(struct sockaddr_storage) },
533
+ [IWPM_NLA_RQUERY_MAPPING_SEQ] = { .type = NLA_U32 },
534
+ [IWPM_NLA_RQUERY_LOCAL_ADDR] = {
535
+ .len = sizeof(struct sockaddr_storage) },
536
+ [IWPM_NLA_RQUERY_REMOTE_ADDR] = {
537
+ .len = sizeof(struct sockaddr_storage) },
538
+ [IWPM_NLA_RQUERY_MAPPED_LOC_ADDR] = {
539
+ .len = sizeof(struct sockaddr_storage) },
540
+ [IWPM_NLA_RQUERY_MAPPED_REM_ADDR] = {
541
+ .len = sizeof(struct sockaddr_storage) },
480542 [IWPM_NLA_RQUERY_MAPPING_ERR] = { .type = NLA_U16 }
481543 };
482544
483
-/*
484
- * iwpm_add_and_query_mapping_cb - Process a port mapper response to
485
- * iwpm_add_and_query_mapping()
545
+/**
546
+ * iwpm_add_and_query_mapping_cb - Process the port mapper response to
547
+ * iwpm_add_and_query_mapping request
548
+ * @skb:
549
+ * @cb: Contains the received message (payload and netlink header)
486550 */
487551 int iwpm_add_and_query_mapping_cb(struct sk_buff *skb,
488552 struct netlink_callback *cb)
....@@ -502,7 +566,7 @@
502566 return -EINVAL;
503567 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
504568
505
- msg_seq = nla_get_u32(nltb[IWPM_NLA_QUERY_MAPPING_SEQ]);
569
+ msg_seq = nla_get_u32(nltb[IWPM_NLA_RQUERY_MAPPING_SEQ]);
506570 nlmsg_request = iwpm_find_nlmsg_request(msg_seq);
507571 if (!nlmsg_request) {
508572 pr_info("%s: Could not find a matching request (seq = %u)\n",
....@@ -511,9 +575,9 @@
511575 }
512576 pm_msg = nlmsg_request->req_buffer;
513577 local_sockaddr = (struct sockaddr_storage *)
514
- nla_data(nltb[IWPM_NLA_QUERY_LOCAL_ADDR]);
578
+ nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
515579 remote_sockaddr = (struct sockaddr_storage *)
516
- nla_data(nltb[IWPM_NLA_QUERY_REMOTE_ADDR]);
580
+ nla_data(nltb[IWPM_NLA_RQUERY_REMOTE_ADDR]);
517581 mapped_loc_sockaddr = (struct sockaddr_storage *)
518582 nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_LOC_ADDR]);
519583 mapped_rem_sockaddr = (struct sockaddr_storage *)
....@@ -560,9 +624,13 @@
560624 return 0;
561625 }
562626
563
-/*
564
- * iwpm_remote_info_cb - Process a port mapper message, containing
565
- * the remote connecting peer address info
627
+/**
628
+ * iwpm_remote_info_cb - Process remote connecting peer address info, which
629
+ * the port mapper has received from the connecting peer
630
+ * @skb:
631
+ * @cb: Contains the received message (payload and netlink header)
632
+ *
633
+ * Stores the IPv4/IPv6 address info in a hash table
566634 */
567635 int iwpm_remote_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
568636 {
....@@ -588,9 +656,9 @@
588656 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
589657
590658 local_sockaddr = (struct sockaddr_storage *)
591
- nla_data(nltb[IWPM_NLA_QUERY_LOCAL_ADDR]);
659
+ nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
592660 remote_sockaddr = (struct sockaddr_storage *)
593
- nla_data(nltb[IWPM_NLA_QUERY_REMOTE_ADDR]);
661
+ nla_data(nltb[IWPM_NLA_RQUERY_REMOTE_ADDR]);
594662 mapped_loc_sockaddr = (struct sockaddr_storage *)
595663 nla_data(nltb[IWPM_NLA_RQUERY_MAPPED_LOC_ADDR]);
596664 mapped_rem_sockaddr = (struct sockaddr_storage *)
....@@ -635,8 +703,14 @@
635703 [IWPM_NLA_MAPINFO_ULIB_VER] = { .type = NLA_U16 }
636704 };
637705
638
-/*
639
- * iwpm_mapping_info_cb - Process a port mapper request for mapping info
706
+/**
707
+ * iwpm_mapping_info_cb - Process a notification that the userspace
708
+ * port mapper daemon is started
709
+ * @skb:
710
+ * @cb: Contains the received message (payload and netlink header)
711
+ *
712
+ * Using the received port mapper pid, send all the local mapping
713
+ * info records to the userspace port mapper
640714 */
641715 int iwpm_mapping_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
642716 {
....@@ -655,7 +729,7 @@
655729 iwpm_name = (char *)nla_data(nltb[IWPM_NLA_MAPINFO_ULIB_NAME]);
656730 iwpm_version = nla_get_u16(nltb[IWPM_NLA_MAPINFO_ULIB_VER]);
657731 if (strcmp(iwpm_ulib_name, iwpm_name) ||
658
- iwpm_version != iwpm_ulib_version) {
732
+ iwpm_version < IWPM_UABI_VERSION_MIN) {
659733 pr_info("%s: Invalid port mapper name = %s version = %d\n",
660734 __func__, iwpm_name, iwpm_version);
661735 return ret;
....@@ -669,6 +743,11 @@
669743 iwpm_set_registration(nl_client, IWPM_REG_INCOMPL);
670744 atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
671745 iwpm_user_pid = cb->nlh->nlmsg_pid;
746
+
747
+ if (iwpm_ulib_version < IWPM_UABI_VERSION)
748
+ pr_warn_once("%s: Down level iwpmd/pid %u. Continuing...",
749
+ __func__, iwpm_user_pid);
750
+
672751 if (!iwpm_mapinfo_available())
673752 return 0;
674753 pr_debug("%s: iWarp Port Mapper (pid = %d) is available!\n",
....@@ -684,9 +763,11 @@
684763 [IWPM_NLA_MAPINFO_ACK_NUM] = { .type = NLA_U32 }
685764 };
686765
687
-/*
688
- * iwpm_ack_mapping_info_cb - Process a port mapper ack for
689
- * the provided mapping info records
766
+/**
767
+ * iwpm_ack_mapping_info_cb - Process the port mapper ack for
768
+ * the provided local mapping info records
769
+ * @skb:
770
+ * @cb: Contains the received message (payload and netlink header)
690771 */
691772 int iwpm_ack_mapping_info_cb(struct sk_buff *skb, struct netlink_callback *cb)
692773 {
....@@ -712,8 +793,11 @@
712793 [IWPM_NLA_ERR_CODE] = { .type = NLA_U16 },
713794 };
714795
715
-/*
716
- * iwpm_mapping_error_cb - Process a port mapper error message
796
+/**
797
+ * iwpm_mapping_error_cb - Process port mapper notification for error
798
+ *
799
+ * @skb:
800
+ * @cb: Contains the received message (payload and netlink header)
717801 */
718802 int iwpm_mapping_error_cb(struct sk_buff *skb, struct netlink_callback *cb)
719803 {
....@@ -748,3 +832,46 @@
748832 up(&nlmsg_request->sem);
749833 return 0;
750834 }
835
+
836
+/* netlink attribute policy for the received hello request */
837
+static const struct nla_policy hello_policy[IWPM_NLA_HELLO_MAX] = {
838
+ [IWPM_NLA_HELLO_ABI_VERSION] = { .type = NLA_U16 }
839
+};
840
+
841
+/**
842
+ * iwpm_hello_cb - Process a hello message from iwpmd
843
+ *
844
+ * @skb:
845
+ * @cb: Contains the received message (payload and netlink header)
846
+ *
847
+ * Using the received port mapper pid, send the kernel's abi_version
848
+ * after adjusting it to support the iwpmd version.
849
+ */
850
+int iwpm_hello_cb(struct sk_buff *skb, struct netlink_callback *cb)
851
+{
852
+ struct nlattr *nltb[IWPM_NLA_HELLO_MAX];
853
+ const char *msg_type = "Hello request";
854
+ u8 nl_client;
855
+ u16 abi_version;
856
+ int ret = -EINVAL;
857
+
858
+ if (iwpm_parse_nlmsg(cb, IWPM_NLA_HELLO_MAX, hello_policy, nltb,
859
+ msg_type)) {
860
+ pr_info("%s: Unable to parse nlmsg\n", __func__);
861
+ return ret;
862
+ }
863
+ abi_version = nla_get_u16(nltb[IWPM_NLA_HELLO_ABI_VERSION]);
864
+ nl_client = RDMA_NL_GET_CLIENT(cb->nlh->nlmsg_type);
865
+ if (!iwpm_valid_client(nl_client)) {
866
+ pr_info("%s: Invalid port mapper client = %d\n",
867
+ __func__, nl_client);
868
+ return ret;
869
+ }
870
+ iwpm_set_registration(nl_client, IWPM_REG_INCOMPL);
871
+ atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
872
+ iwpm_ulib_version = min_t(u16, IWPM_UABI_VERSION, abi_version);
873
+ pr_debug("Using ABI version %u\n", iwpm_ulib_version);
874
+ iwpm_user_pid = cb->nlh->nlmsg_pid;
875
+ ret = iwpm_send_hello(nl_client, iwpm_user_pid, iwpm_ulib_version);
876
+ return ret;
877
+}