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