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 if (iwpm_valid_client(nl_client)) 58 return -EINVAL; 59 mutex_lock(&iwpm_admin_lock); 60 if (atomic_read(&iwpm_admin.refcount) == 0) { 61 iwpm_hash_bucket = kzalloc(IWPM_MAPINFO_HASH_SIZE * 62 sizeof(struct hlist_head), GFP_KERNEL); 63 if (!iwpm_hash_bucket) { 64 ret = -ENOMEM; 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 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 EXPORT_SYMBOL(iwpm_init); 87 88 static void free_hash_bucket(void); 89 static void free_reminfo_bucket(void); 90 91 int 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 iwpm_set_registration(nl_client, IWPM_REG_UNDEF); 110 return 0; 111 } 112 EXPORT_SYMBOL(iwpm_exit); 113 114 static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage *, 115 struct sockaddr_storage *); 116 117 int iwpm_create_mapinfo(struct sockaddr_storage *local_sockaddr, 118 struct sockaddr_storage *mapped_sockaddr, 119 u8 nl_client) 120 { 121 struct hlist_head *hash_bucket_head; 122 struct iwpm_mapping_info *map_info; 123 unsigned long flags; 124 int ret = -EINVAL; 125 126 if (!iwpm_valid_client(nl_client)) 127 return ret; 128 map_info = kzalloc(sizeof(struct iwpm_mapping_info), GFP_KERNEL); 129 if (!map_info) 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 } 151 EXPORT_SYMBOL(iwpm_create_mapinfo); 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 EXPORT_SYMBOL(iwpm_remove_mapinfo); 188 189 static 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 212 static 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 235 static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage *, 236 struct sockaddr_storage *); 237 238 void 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 254 int 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 } 296 get_remote_info_exit: 297 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags); 298 return ret; 299 } 300 EXPORT_SYMBOL(iwpm_get_remote_info); 301 302 struct 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 return NULL; 311 312 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 313 list_add_tail(&nlmsg_request->inprocess_list, &iwpm_nlmsg_req_list); 314 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 315 316 kref_init(&nlmsg_request->kref); 317 kref_get(&nlmsg_request->kref); 318 nlmsg_request->nlmsg_seq = nlmsg_seq; 319 nlmsg_request->nl_client = nl_client; 320 nlmsg_request->request_done = 0; 321 nlmsg_request->err_code = 0; 322 sema_init(&nlmsg_request->sem, 1); 323 down(&nlmsg_request->sem); 324 return nlmsg_request; 325 } 326 327 void iwpm_free_nlmsg_request(struct kref *kref) 328 { 329 struct iwpm_nlmsg_request *nlmsg_request; 330 unsigned long flags; 331 332 nlmsg_request = container_of(kref, struct iwpm_nlmsg_request, kref); 333 334 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 335 list_del_init(&nlmsg_request->inprocess_list); 336 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 337 338 if (!nlmsg_request->request_done) 339 pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n", 340 __func__, nlmsg_request->nlmsg_seq); 341 kfree(nlmsg_request); 342 } 343 344 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq) 345 { 346 struct iwpm_nlmsg_request *nlmsg_request; 347 struct iwpm_nlmsg_request *found_request = NULL; 348 unsigned long flags; 349 350 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags); 351 list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list, 352 inprocess_list) { 353 if (nlmsg_request->nlmsg_seq == echo_seq) { 354 found_request = nlmsg_request; 355 kref_get(&nlmsg_request->kref); 356 break; 357 } 358 } 359 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags); 360 return found_request; 361 } 362 363 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request) 364 { 365 int ret; 366 367 ret = down_timeout(&nlmsg_request->sem, IWPM_NL_TIMEOUT); 368 if (ret) { 369 ret = -EINVAL; 370 pr_info("%s: Timeout %d sec for netlink request (seq = %u)\n", 371 __func__, (IWPM_NL_TIMEOUT/HZ), nlmsg_request->nlmsg_seq); 372 } else { 373 ret = nlmsg_request->err_code; 374 } 375 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request); 376 return ret; 377 } 378 379 int iwpm_get_nlmsg_seq(void) 380 { 381 return atomic_inc_return(&iwpm_admin.nlmsg_seq); 382 } 383 384 int iwpm_valid_client(u8 nl_client) 385 { 386 if (nl_client >= RDMA_NL_NUM_CLIENTS) 387 return 0; 388 return iwpm_admin.client_list[nl_client]; 389 } 390 391 void iwpm_set_valid(u8 nl_client, int valid) 392 { 393 if (nl_client >= RDMA_NL_NUM_CLIENTS) 394 return; 395 iwpm_admin.client_list[nl_client] = valid; 396 } 397 398 /* valid client */ 399 u32 iwpm_get_registration(u8 nl_client) 400 { 401 return iwpm_admin.reg_list[nl_client]; 402 } 403 404 /* valid client */ 405 void iwpm_set_registration(u8 nl_client, u32 reg) 406 { 407 iwpm_admin.reg_list[nl_client] = reg; 408 } 409 410 /* valid client */ 411 u32 iwpm_check_registration(u8 nl_client, u32 reg) 412 { 413 return (iwpm_get_registration(nl_client) & reg); 414 } 415 416 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr, 417 struct sockaddr_storage *b_sockaddr) 418 { 419 if (a_sockaddr->ss_family != b_sockaddr->ss_family) 420 return 1; 421 if (a_sockaddr->ss_family == AF_INET) { 422 struct sockaddr_in *a4_sockaddr = 423 (struct sockaddr_in *)a_sockaddr; 424 struct sockaddr_in *b4_sockaddr = 425 (struct sockaddr_in *)b_sockaddr; 426 if (!memcmp(&a4_sockaddr->sin_addr, 427 &b4_sockaddr->sin_addr, sizeof(struct in_addr)) 428 && a4_sockaddr->sin_port == b4_sockaddr->sin_port) 429 return 0; 430 431 } else if (a_sockaddr->ss_family == AF_INET6) { 432 struct sockaddr_in6 *a6_sockaddr = 433 (struct sockaddr_in6 *)a_sockaddr; 434 struct sockaddr_in6 *b6_sockaddr = 435 (struct sockaddr_in6 *)b_sockaddr; 436 if (!memcmp(&a6_sockaddr->sin6_addr, 437 &b6_sockaddr->sin6_addr, sizeof(struct in6_addr)) 438 && a6_sockaddr->sin6_port == b6_sockaddr->sin6_port) 439 return 0; 440 441 } else { 442 pr_err("%s: Invalid sockaddr family\n", __func__); 443 } 444 return 1; 445 } 446 447 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh, 448 int nl_client) 449 { 450 struct sk_buff *skb = NULL; 451 452 skb = dev_alloc_skb(IWPM_MSG_SIZE); 453 if (!skb) { 454 pr_err("%s Unable to allocate skb\n", __func__); 455 goto create_nlmsg_exit; 456 } 457 if (!(ibnl_put_msg(skb, nlh, 0, 0, nl_client, nl_op, 458 NLM_F_REQUEST))) { 459 pr_warn("%s: Unable to put the nlmsg header\n", __func__); 460 dev_kfree_skb(skb); 461 skb = NULL; 462 } 463 create_nlmsg_exit: 464 return skb; 465 } 466 467 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max, 468 const struct nla_policy *nlmsg_policy, 469 struct nlattr *nltb[], const char *msg_type) 470 { 471 int nlh_len = 0; 472 int ret; 473 const char *err_str = ""; 474 475 ret = nlmsg_validate(cb->nlh, nlh_len, policy_max - 1, nlmsg_policy, 476 NULL); 477 if (ret) { 478 err_str = "Invalid attribute"; 479 goto parse_nlmsg_error; 480 } 481 ret = nlmsg_parse(cb->nlh, nlh_len, nltb, policy_max - 1, 482 nlmsg_policy, NULL); 483 if (ret) { 484 err_str = "Unable to parse the nlmsg"; 485 goto parse_nlmsg_error; 486 } 487 ret = iwpm_validate_nlmsg_attr(nltb, policy_max); 488 if (ret) { 489 err_str = "Invalid NULL attribute"; 490 goto parse_nlmsg_error; 491 } 492 return 0; 493 parse_nlmsg_error: 494 pr_warn("%s: %s (msg type %s ret = %d)\n", 495 __func__, err_str, msg_type, ret); 496 return ret; 497 } 498 499 void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg) 500 { 501 struct sockaddr_in6 *sockaddr_v6; 502 struct sockaddr_in *sockaddr_v4; 503 504 switch (sockaddr->ss_family) { 505 case AF_INET: 506 sockaddr_v4 = (struct sockaddr_in *)sockaddr; 507 pr_debug("%s IPV4 %pI4: %u(0x%04X)\n", 508 msg, &sockaddr_v4->sin_addr, 509 ntohs(sockaddr_v4->sin_port), 510 ntohs(sockaddr_v4->sin_port)); 511 break; 512 case AF_INET6: 513 sockaddr_v6 = (struct sockaddr_in6 *)sockaddr; 514 pr_debug("%s IPV6 %pI6: %u(0x%04X)\n", 515 msg, &sockaddr_v6->sin6_addr, 516 ntohs(sockaddr_v6->sin6_port), 517 ntohs(sockaddr_v6->sin6_port)); 518 break; 519 default: 520 break; 521 } 522 } 523 524 static u32 iwpm_ipv6_jhash(struct sockaddr_in6 *ipv6_sockaddr) 525 { 526 u32 ipv6_hash = jhash(&ipv6_sockaddr->sin6_addr, sizeof(struct in6_addr), 0); 527 u32 hash = jhash_2words(ipv6_hash, (__force u32) ipv6_sockaddr->sin6_port, 0); 528 return hash; 529 } 530 531 static u32 iwpm_ipv4_jhash(struct sockaddr_in *ipv4_sockaddr) 532 { 533 u32 ipv4_hash = jhash(&ipv4_sockaddr->sin_addr, sizeof(struct in_addr), 0); 534 u32 hash = jhash_2words(ipv4_hash, (__force u32) ipv4_sockaddr->sin_port, 0); 535 return hash; 536 } 537 538 static int get_hash_bucket(struct sockaddr_storage *a_sockaddr, 539 struct sockaddr_storage *b_sockaddr, u32 *hash) 540 { 541 u32 a_hash, b_hash; 542 543 if (a_sockaddr->ss_family == AF_INET) { 544 a_hash = iwpm_ipv4_jhash((struct sockaddr_in *) a_sockaddr); 545 b_hash = iwpm_ipv4_jhash((struct sockaddr_in *) b_sockaddr); 546 547 } else if (a_sockaddr->ss_family == AF_INET6) { 548 a_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) a_sockaddr); 549 b_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) b_sockaddr); 550 } else { 551 pr_err("%s: Invalid sockaddr family\n", __func__); 552 return -EINVAL; 553 } 554 555 if (a_hash == b_hash) /* if port mapper isn't available */ 556 *hash = a_hash; 557 else 558 *hash = jhash_2words(a_hash, b_hash, 0); 559 return 0; 560 } 561 562 static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage 563 *local_sockaddr, struct sockaddr_storage 564 *mapped_sockaddr) 565 { 566 u32 hash; 567 int ret; 568 569 ret = get_hash_bucket(local_sockaddr, mapped_sockaddr, &hash); 570 if (ret) 571 return NULL; 572 return &iwpm_hash_bucket[hash & IWPM_MAPINFO_HASH_MASK]; 573 } 574 575 static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage 576 *mapped_loc_sockaddr, struct sockaddr_storage 577 *mapped_rem_sockaddr) 578 { 579 u32 hash; 580 int ret; 581 582 ret = get_hash_bucket(mapped_loc_sockaddr, mapped_rem_sockaddr, &hash); 583 if (ret) 584 return NULL; 585 return &iwpm_reminfo_bucket[hash & IWPM_REMINFO_HASH_MASK]; 586 } 587 588 static int send_mapinfo_num(u32 mapping_num, u8 nl_client, int iwpm_pid) 589 { 590 struct sk_buff *skb = NULL; 591 struct nlmsghdr *nlh; 592 u32 msg_seq; 593 const char *err_str = ""; 594 int ret = -EINVAL; 595 596 skb = iwpm_create_nlmsg(RDMA_NL_IWPM_MAPINFO_NUM, &nlh, nl_client); 597 if (!skb) { 598 err_str = "Unable to create a nlmsg"; 599 goto mapinfo_num_error; 600 } 601 nlh->nlmsg_seq = iwpm_get_nlmsg_seq(); 602 msg_seq = 0; 603 err_str = "Unable to put attribute of mapinfo number nlmsg"; 604 ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq, IWPM_NLA_MAPINFO_SEQ); 605 if (ret) 606 goto mapinfo_num_error; 607 ret = ibnl_put_attr(skb, nlh, sizeof(u32), 608 &mapping_num, IWPM_NLA_MAPINFO_SEND_NUM); 609 if (ret) 610 goto mapinfo_num_error; 611 ret = ibnl_unicast(skb, nlh, iwpm_pid); 612 if (ret) { 613 skb = NULL; 614 err_str = "Unable to send a nlmsg"; 615 goto mapinfo_num_error; 616 } 617 pr_debug("%s: Sent mapping number = %d\n", __func__, mapping_num); 618 return 0; 619 mapinfo_num_error: 620 pr_info("%s: %s\n", __func__, err_str); 621 if (skb) 622 dev_kfree_skb(skb); 623 return ret; 624 } 625 626 static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid) 627 { 628 struct nlmsghdr *nlh = NULL; 629 int ret = 0; 630 631 if (!skb) 632 return ret; 633 if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client, 634 RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) { 635 pr_warn("%s Unable to put NLMSG_DONE\n", __func__); 636 dev_kfree_skb(skb); 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