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