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