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 = kcalloc(IWPM_MAPINFO_HASH_SIZE, 60 sizeof(struct hlist_head), 61 GFP_KERNEL); 62 if (!iwpm_hash_bucket) { 63 ret = -ENOMEM; 64 goto init_exit; 65 } 66 iwpm_reminfo_bucket = kcalloc(IWPM_REMINFO_HASH_SIZE, 67 sizeof(struct hlist_head), 68 GFP_KERNEL); 69 if (!iwpm_reminfo_bucket) { 70 kfree(iwpm_hash_bucket); 71 ret = -ENOMEM; 72 goto init_exit; 73 } 74 } 75 atomic_inc(&iwpm_admin.refcount); 76 init_exit: 77 mutex_unlock(&iwpm_admin_lock); 78 if (!ret) { 79 iwpm_set_valid(nl_client, 1); 80 iwpm_set_registration(nl_client, IWPM_REG_UNDEF); 81 pr_debug("%s: Mapinfo and reminfo tables are created\n", 82 __func__); 83 } 84 return ret; 85 } 86 87 static void free_hash_bucket(void); 88 static void free_reminfo_bucket(void); 89 90 int iwpm_exit(u8 nl_client) 91 { 92 93 if (!iwpm_valid_client(nl_client)) 94 return -EINVAL; 95 mutex_lock(&iwpm_admin_lock); 96 if (atomic_read(&iwpm_admin.refcount) == 0) { 97 mutex_unlock(&iwpm_admin_lock); 98 pr_err("%s Incorrect usage - negative refcount\n", __func__); 99 return -EINVAL; 100 } 101 if (atomic_dec_and_test(&iwpm_admin.refcount)) { 102 free_hash_bucket(); 103 free_reminfo_bucket(); 104 pr_debug("%s: Resources are destroyed\n", __func__); 105 } 106 mutex_unlock(&iwpm_admin_lock); 107 iwpm_set_valid(nl_client, 0); 108 iwpm_set_registration(nl_client, IWPM_REG_UNDEF); 109 return 0; 110 } 111 112 static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage *, 113 struct sockaddr_storage *); 114 115 int iwpm_create_mapinfo(struct sockaddr_storage *local_sockaddr, 116 struct sockaddr_storage *mapped_sockaddr, 117 u8 nl_client) 118 { 119 struct hlist_head *hash_bucket_head = NULL; 120 struct iwpm_mapping_info *map_info; 121 unsigned long flags; 122 int ret = -EINVAL; 123 124 if (!iwpm_valid_client(nl_client)) 125 return ret; 126 map_info = kzalloc(sizeof(struct iwpm_mapping_info), GFP_KERNEL); 127 if (!map_info) 128 return -ENOMEM; 129 130 memcpy(&map_info->local_sockaddr, local_sockaddr, 131 sizeof(struct sockaddr_storage)); 132 memcpy(&map_info->mapped_sockaddr, mapped_sockaddr, 133 sizeof(struct sockaddr_storage)); 134 map_info->nl_client = nl_client; 135 136 spin_lock_irqsave(&iwpm_mapinfo_lock, flags); 137 if (iwpm_hash_bucket) { 138 hash_bucket_head = get_mapinfo_hash_bucket( 139 &map_info->local_sockaddr, 140 &map_info->mapped_sockaddr); 141 if (hash_bucket_head) { 142 hlist_add_head(&map_info->hlist_node, hash_bucket_head); 143 ret = 0; 144 } 145 } 146 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags); 147 148 if (!hash_bucket_head) 149 kfree(map_info); 150 return ret; 151 } 152 153 int 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 } 183 remove_mapinfo_exit: 184 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags); 185 return ret; 186 } 187 188 static void free_hash_bucket(void) 189 { 190 struct hlist_node *tmp_hlist_node; 191 struct iwpm_mapping_info *map_info; 192 unsigned long flags; 193 int i; 194 195 /* remove all the mapinfo data from the list */ 196 spin_lock_irqsave(&iwpm_mapinfo_lock, flags); 197 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) { 198 hlist_for_each_entry_safe(map_info, tmp_hlist_node, 199 &iwpm_hash_bucket[i], hlist_node) { 200 201 hlist_del_init(&map_info->hlist_node); 202 kfree(map_info); 203 } 204 } 205 /* free the hash list */ 206 kfree(iwpm_hash_bucket); 207 iwpm_hash_bucket = NULL; 208 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags); 209 } 210 211 static void free_reminfo_bucket(void) 212 { 213 struct hlist_node *tmp_hlist_node; 214 struct iwpm_remote_info *rem_info; 215 unsigned long flags; 216 int i; 217 218 /* remove all the remote info from the list */ 219 spin_lock_irqsave(&iwpm_reminfo_lock, flags); 220 for (i = 0; i < IWPM_REMINFO_HASH_SIZE; i++) { 221 hlist_for_each_entry_safe(rem_info, tmp_hlist_node, 222 &iwpm_reminfo_bucket[i], hlist_node) { 223 224 hlist_del_init(&rem_info->hlist_node); 225 kfree(rem_info); 226 } 227 } 228 /* free the hash list */ 229 kfree(iwpm_reminfo_bucket); 230 iwpm_reminfo_bucket = NULL; 231 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags); 232 } 233 234 static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage *, 235 struct sockaddr_storage *); 236 237 void iwpm_add_remote_info(struct iwpm_remote_info *rem_info) 238 { 239 struct hlist_head *hash_bucket_head; 240 unsigned long flags; 241 242 spin_lock_irqsave(&iwpm_reminfo_lock, flags); 243 if (iwpm_reminfo_bucket) { 244 hash_bucket_head = get_reminfo_hash_bucket( 245 &rem_info->mapped_loc_sockaddr, 246 &rem_info->mapped_rem_sockaddr); 247 if (hash_bucket_head) 248 hlist_add_head(&rem_info->hlist_node, hash_bucket_head); 249 } 250 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags); 251 } 252 253 int iwpm_get_remote_info(struct sockaddr_storage *mapped_loc_addr, 254 struct sockaddr_storage *mapped_rem_addr, 255 struct sockaddr_storage *remote_addr, 256 u8 nl_client) 257 { 258 struct hlist_node *tmp_hlist_node; 259 struct hlist_head *hash_bucket_head; 260 struct iwpm_remote_info *rem_info = NULL; 261 unsigned long flags; 262 int ret = -EINVAL; 263 264 if (!iwpm_valid_client(nl_client)) { 265 pr_info("%s: Invalid client = %d\n", __func__, nl_client); 266 return ret; 267 } 268 spin_lock_irqsave(&iwpm_reminfo_lock, flags); 269 if (iwpm_reminfo_bucket) { 270 hash_bucket_head = get_reminfo_hash_bucket( 271 mapped_loc_addr, 272 mapped_rem_addr); 273 if (!hash_bucket_head) 274 goto get_remote_info_exit; 275 hlist_for_each_entry_safe(rem_info, tmp_hlist_node, 276 hash_bucket_head, hlist_node) { 277 278 if (!iwpm_compare_sockaddr(&rem_info->mapped_loc_sockaddr, 279 mapped_loc_addr) && 280 !iwpm_compare_sockaddr(&rem_info->mapped_rem_sockaddr, 281 mapped_rem_addr)) { 282 283 memcpy(remote_addr, &rem_info->remote_sockaddr, 284 sizeof(struct sockaddr_storage)); 285 iwpm_print_sockaddr(remote_addr, 286 "get_remote_info: Remote sockaddr:"); 287 288 hlist_del_init(&rem_info->hlist_node); 289 kfree(rem_info); 290 ret = 0; 291 break; 292 } 293 } 294 } 295 get_remote_info_exit: 296 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags); 297 return ret; 298 } 299 300 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq, 301 u8 nl_client, gfp_t gfp) 302 { 303 struct iwpm_nlmsg_request *nlmsg_request = NULL; 304 unsigned long flags; 305 306 nlmsg_request = kzalloc(sizeof(struct iwpm_nlmsg_request), gfp); 307 if (!nlmsg_request) 308 return NULL; 309 310 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 311 list_add_tail(&nlmsg_request->inprocess_list, &iwpm_nlmsg_req_list); 312 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 313 314 kref_init(&nlmsg_request->kref); 315 kref_get(&nlmsg_request->kref); 316 nlmsg_request->nlmsg_seq = nlmsg_seq; 317 nlmsg_request->nl_client = nl_client; 318 nlmsg_request->request_done = 0; 319 nlmsg_request->err_code = 0; 320 sema_init(&nlmsg_request->sem, 1); 321 down(&nlmsg_request->sem); 322 return nlmsg_request; 323 } 324 325 void iwpm_free_nlmsg_request(struct kref *kref) 326 { 327 struct iwpm_nlmsg_request *nlmsg_request; 328 unsigned long flags; 329 330 nlmsg_request = container_of(kref, struct iwpm_nlmsg_request, kref); 331 332 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 333 list_del_init(&nlmsg_request->inprocess_list); 334 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 335 336 if (!nlmsg_request->request_done) 337 pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n", 338 __func__, nlmsg_request->nlmsg_seq); 339 kfree(nlmsg_request); 340 } 341 342 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq) 343 { 344 struct iwpm_nlmsg_request *nlmsg_request; 345 struct iwpm_nlmsg_request *found_request = NULL; 346 unsigned long flags; 347 348 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 349 list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list, 350 inprocess_list) { 351 if (nlmsg_request->nlmsg_seq == echo_seq) { 352 found_request = nlmsg_request; 353 kref_get(&nlmsg_request->kref); 354 break; 355 } 356 } 357 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 358 return found_request; 359 } 360 361 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request) 362 { 363 int ret; 364 365 ret = down_timeout(&nlmsg_request->sem, IWPM_NL_TIMEOUT); 366 if (ret) { 367 ret = -EINVAL; 368 pr_info("%s: Timeout %d sec for netlink request (seq = %u)\n", 369 __func__, (IWPM_NL_TIMEOUT/HZ), nlmsg_request->nlmsg_seq); 370 } else { 371 ret = nlmsg_request->err_code; 372 } 373 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request); 374 return ret; 375 } 376 377 int iwpm_get_nlmsg_seq(void) 378 { 379 return atomic_inc_return(&iwpm_admin.nlmsg_seq); 380 } 381 382 int iwpm_valid_client(u8 nl_client) 383 { 384 return iwpm_admin.client_list[nl_client]; 385 } 386 387 void iwpm_set_valid(u8 nl_client, int valid) 388 { 389 iwpm_admin.client_list[nl_client] = valid; 390 } 391 392 /* valid client */ 393 u32 iwpm_get_registration(u8 nl_client) 394 { 395 return iwpm_admin.reg_list[nl_client]; 396 } 397 398 /* valid client */ 399 void iwpm_set_registration(u8 nl_client, u32 reg) 400 { 401 iwpm_admin.reg_list[nl_client] = reg; 402 } 403 404 /* valid client */ 405 u32 iwpm_check_registration(u8 nl_client, u32 reg) 406 { 407 return (iwpm_get_registration(nl_client) & reg); 408 } 409 410 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr, 411 struct sockaddr_storage *b_sockaddr) 412 { 413 if (a_sockaddr->ss_family != b_sockaddr->ss_family) 414 return 1; 415 if (a_sockaddr->ss_family == AF_INET) { 416 struct sockaddr_in *a4_sockaddr = 417 (struct sockaddr_in *)a_sockaddr; 418 struct sockaddr_in *b4_sockaddr = 419 (struct sockaddr_in *)b_sockaddr; 420 if (!memcmp(&a4_sockaddr->sin_addr, 421 &b4_sockaddr->sin_addr, sizeof(struct in_addr)) 422 && a4_sockaddr->sin_port == b4_sockaddr->sin_port) 423 return 0; 424 425 } else if (a_sockaddr->ss_family == AF_INET6) { 426 struct sockaddr_in6 *a6_sockaddr = 427 (struct sockaddr_in6 *)a_sockaddr; 428 struct sockaddr_in6 *b6_sockaddr = 429 (struct sockaddr_in6 *)b_sockaddr; 430 if (!memcmp(&a6_sockaddr->sin6_addr, 431 &b6_sockaddr->sin6_addr, sizeof(struct in6_addr)) 432 && a6_sockaddr->sin6_port == b6_sockaddr->sin6_port) 433 return 0; 434 435 } else { 436 pr_err("%s: Invalid sockaddr family\n", __func__); 437 } 438 return 1; 439 } 440 441 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh, 442 int nl_client) 443 { 444 struct sk_buff *skb = NULL; 445 446 skb = dev_alloc_skb(IWPM_MSG_SIZE); 447 if (!skb) 448 goto create_nlmsg_exit; 449 450 if (!(ibnl_put_msg(skb, nlh, 0, 0, nl_client, nl_op, 451 NLM_F_REQUEST))) { 452 pr_warn("%s: Unable to put the nlmsg header\n", __func__); 453 dev_kfree_skb(skb); 454 skb = NULL; 455 } 456 create_nlmsg_exit: 457 return skb; 458 } 459 460 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max, 461 const struct nla_policy *nlmsg_policy, 462 struct nlattr *nltb[], const char *msg_type) 463 { 464 int nlh_len = 0; 465 int ret; 466 const char *err_str = ""; 467 468 ret = nlmsg_validate(cb->nlh, nlh_len, policy_max - 1, nlmsg_policy, 469 NULL); 470 if (ret) { 471 err_str = "Invalid attribute"; 472 goto parse_nlmsg_error; 473 } 474 ret = nlmsg_parse(cb->nlh, nlh_len, nltb, policy_max - 1, 475 nlmsg_policy, NULL); 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; 486 parse_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 492 void 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 517 static 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 524 static 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 531 static 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 555 static 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 568 static 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 581 static 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 605 nlmsg_end(skb, nlh); 606 607 ret = rdma_nl_unicast(skb, iwpm_pid); 608 if (ret) { 609 skb = NULL; 610 err_str = "Unable to send a nlmsg"; 611 goto mapinfo_num_error; 612 } 613 pr_debug("%s: Sent mapping number = %d\n", __func__, mapping_num); 614 return 0; 615 mapinfo_num_error: 616 pr_info("%s: %s\n", __func__, err_str); 617 if (skb) 618 dev_kfree_skb(skb); 619 return ret; 620 } 621 622 static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid) 623 { 624 struct nlmsghdr *nlh = NULL; 625 int ret = 0; 626 627 if (!skb) 628 return ret; 629 if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client, 630 RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) { 631 pr_warn("%s Unable to put NLMSG_DONE\n", __func__); 632 dev_kfree_skb(skb); 633 return -ENOMEM; 634 } 635 nlh->nlmsg_type = NLMSG_DONE; 636 ret = rdma_nl_unicast(skb, iwpm_pid); 637 if (ret) 638 pr_warn("%s Unable to send a nlmsg\n", __func__); 639 return ret; 640 } 641 642 int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid) 643 { 644 struct iwpm_mapping_info *map_info; 645 struct sk_buff *skb = NULL; 646 struct nlmsghdr *nlh; 647 int skb_num = 0, mapping_num = 0; 648 int i = 0, nlmsg_bytes = 0; 649 unsigned long flags; 650 const char *err_str = ""; 651 int ret; 652 653 skb = dev_alloc_skb(NLMSG_GOODSIZE); 654 if (!skb) { 655 ret = -ENOMEM; 656 err_str = "Unable to allocate skb"; 657 goto send_mapping_info_exit; 658 } 659 skb_num++; 660 spin_lock_irqsave(&iwpm_mapinfo_lock, flags); 661 ret = -EINVAL; 662 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) { 663 hlist_for_each_entry(map_info, &iwpm_hash_bucket[i], 664 hlist_node) { 665 if (map_info->nl_client != nl_client) 666 continue; 667 nlh = NULL; 668 if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client, 669 RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) { 670 ret = -ENOMEM; 671 err_str = "Unable to put the nlmsg header"; 672 goto send_mapping_info_unlock; 673 } 674 err_str = "Unable to put attribute of the nlmsg"; 675 ret = ibnl_put_attr(skb, nlh, 676 sizeof(struct sockaddr_storage), 677 &map_info->local_sockaddr, 678 IWPM_NLA_MAPINFO_LOCAL_ADDR); 679 if (ret) 680 goto send_mapping_info_unlock; 681 682 ret = ibnl_put_attr(skb, nlh, 683 sizeof(struct sockaddr_storage), 684 &map_info->mapped_sockaddr, 685 IWPM_NLA_MAPINFO_MAPPED_ADDR); 686 if (ret) 687 goto send_mapping_info_unlock; 688 689 nlmsg_end(skb, nlh); 690 691 iwpm_print_sockaddr(&map_info->local_sockaddr, 692 "send_mapping_info: Local sockaddr:"); 693 iwpm_print_sockaddr(&map_info->mapped_sockaddr, 694 "send_mapping_info: Mapped local sockaddr:"); 695 mapping_num++; 696 nlmsg_bytes += nlh->nlmsg_len; 697 698 /* check if all mappings can fit in one skb */ 699 if (NLMSG_GOODSIZE - nlmsg_bytes < nlh->nlmsg_len * 2) { 700 /* and leave room for NLMSG_DONE */ 701 nlmsg_bytes = 0; 702 skb_num++; 703 spin_unlock_irqrestore(&iwpm_mapinfo_lock, 704 flags); 705 /* send the skb */ 706 ret = send_nlmsg_done(skb, nl_client, iwpm_pid); 707 skb = NULL; 708 if (ret) { 709 err_str = "Unable to send map info"; 710 goto send_mapping_info_exit; 711 } 712 if (skb_num == IWPM_MAPINFO_SKB_COUNT) { 713 ret = -ENOMEM; 714 err_str = "Insufficient skbs for map info"; 715 goto send_mapping_info_exit; 716 } 717 skb = dev_alloc_skb(NLMSG_GOODSIZE); 718 if (!skb) { 719 ret = -ENOMEM; 720 err_str = "Unable to allocate skb"; 721 goto send_mapping_info_exit; 722 } 723 spin_lock_irqsave(&iwpm_mapinfo_lock, flags); 724 } 725 } 726 } 727 send_mapping_info_unlock: 728 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags); 729 send_mapping_info_exit: 730 if (ret) { 731 pr_warn("%s: %s (ret = %d)\n", __func__, err_str, ret); 732 if (skb) 733 dev_kfree_skb(skb); 734 return ret; 735 } 736 send_nlmsg_done(skb, nl_client, iwpm_pid); 737 return send_mapinfo_num(mapping_num, nl_client, iwpm_pid); 738 } 739 740 int iwpm_mapinfo_available(void) 741 { 742 unsigned long flags; 743 int full_bucket = 0, i = 0; 744 745 spin_lock_irqsave(&iwpm_mapinfo_lock, flags); 746 if (iwpm_hash_bucket) { 747 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) { 748 if (!hlist_empty(&iwpm_hash_bucket[i])) { 749 full_bucket = 1; 750 break; 751 } 752 } 753 } 754 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags); 755 return full_bucket; 756 } 757