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