Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2014 Intel Corporation. All rights reserved.
0003  * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
0004  *
0005  * This software is available to you under a choice of one of two
0006  * licenses.  You may choose to be licensed under the terms of the GNU
0007  * General Public License (GPL) Version 2, available from the file
0008  * COPYING in the main directory of this source tree, or the
0009  * OpenIB.org BSD license below:
0010  *
0011  *     Redistribution and use in source and binary forms, with or
0012  *     without modification, are permitted provided that the following
0013  *     conditions are met:
0014  *
0015  *      - Redistributions of source code must retain the above
0016  *    copyright notice, this list of conditions and the following
0017  *    disclaimer.
0018  *
0019  *      - Redistributions in binary form must reproduce the above
0020  *    copyright notice, this list of conditions and the following
0021  *    disclaimer in the documentation and/or other materials
0022  *    provided with the distribution.
0023  *
0024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0026  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0028  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0029  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0030  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0031  * SOFTWARE.
0032  */
0033 #ifndef _IWPM_UTIL_H
0034 #define _IWPM_UTIL_H
0035 
0036 #include <linux/io.h>
0037 #include <linux/in.h>
0038 #include <linux/in6.h>
0039 #include <linux/spinlock.h>
0040 #include <linux/kernel.h>
0041 #include <linux/netdevice.h>
0042 #include <linux/delay.h>
0043 #include <linux/workqueue.h>
0044 #include <linux/mutex.h>
0045 #include <linux/jhash.h>
0046 #include <linux/kref.h>
0047 #include <net/netlink.h>
0048 #include <linux/errno.h>
0049 #include <rdma/iw_portmap.h>
0050 #include <rdma/rdma_netlink.h>
0051 
0052 
0053 #define IWPM_NL_RETRANS     3
0054 #define IWPM_NL_TIMEOUT     (10*HZ)
0055 #define IWPM_MAPINFO_SKB_COUNT  20
0056 
0057 #define IWPM_PID_UNDEFINED     -1
0058 #define IWPM_PID_UNAVAILABLE   -2
0059 
0060 #define IWPM_REG_UNDEF          0x01
0061 #define IWPM_REG_VALID          0x02
0062 #define IWPM_REG_INCOMPL        0x04
0063 
0064 struct iwpm_nlmsg_request {
0065     struct list_head    inprocess_list;
0066     __u32               nlmsg_seq;
0067     void                *req_buffer;
0068     u8              nl_client;
0069     u8                  request_done;
0070     u16                 err_code;
0071     struct semaphore    sem;
0072     struct kref         kref;
0073 };
0074 
0075 struct iwpm_mapping_info {
0076     struct hlist_node hlist_node;
0077     struct sockaddr_storage local_sockaddr;
0078     struct sockaddr_storage mapped_sockaddr;
0079     u8     nl_client;
0080     u32    map_flags;
0081 };
0082 
0083 struct iwpm_remote_info {
0084     struct hlist_node hlist_node;
0085     struct sockaddr_storage remote_sockaddr;
0086     struct sockaddr_storage mapped_loc_sockaddr;
0087     struct sockaddr_storage mapped_rem_sockaddr;
0088     u8     nl_client;
0089 };
0090 
0091 struct iwpm_admin_data {
0092     atomic_t nlmsg_seq;
0093     u32      reg_list[RDMA_NL_NUM_CLIENTS];
0094 };
0095 
0096 /**
0097  * iwpm_get_nlmsg_request - Allocate and initialize netlink message request
0098  * @nlmsg_seq: Sequence number of the netlink message
0099  * @nl_client: The index of the netlink client
0100  * @gfp: Indicates how the memory for the request should be allocated
0101  *
0102  * Returns the newly allocated netlink request object if successful,
0103  * otherwise returns NULL
0104  */
0105 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
0106                         u8 nl_client, gfp_t gfp);
0107 
0108 /**
0109  * iwpm_free_nlmsg_request - Deallocate netlink message request
0110  * @kref: Holds reference of netlink message request
0111  */
0112 void iwpm_free_nlmsg_request(struct kref *kref);
0113 
0114 /**
0115  * iwpm_find_nlmsg_request - Find netlink message request in the request list
0116  * @echo_seq: Sequence number of the netlink request to find
0117  *
0118  * Returns the found netlink message request,
0119  * if not found, returns NULL
0120  */
0121 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq);
0122 
0123 /**
0124  * iwpm_wait_complete_req - Block while servicing the netlink request
0125  * @nlmsg_request: Netlink message request to service
0126  *
0127  * Wakes up, after the request is completed or expired
0128  * Returns 0 if the request is complete without error
0129  */
0130 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request);
0131 
0132 /**
0133  * iwpm_get_nlmsg_seq - Get the sequence number for a netlink
0134  *          message to send to the port mapper
0135  *
0136  * Returns the sequence number for the netlink message.
0137  */
0138 int iwpm_get_nlmsg_seq(void);
0139 
0140 /**
0141  * iwpm_add_remote_info - Add remote address info of the connecting peer
0142  *                    to the remote info hash table
0143  * @reminfo: The remote info to be added
0144  */
0145 void iwpm_add_remote_info(struct iwpm_remote_info *reminfo);
0146 
0147 /**
0148  * iwpm_check_registration - Check if the client registration
0149  *                matches the given one
0150  * @nl_client: The index of the netlink client
0151  * @reg: The given registration type to compare with
0152  *
0153  * Call iwpm_register_pid() to register a client
0154  * Returns true if the client registration matches reg,
0155  * otherwise returns false
0156  */
0157 u32 iwpm_check_registration(u8 nl_client, u32 reg);
0158 
0159 /**
0160  * iwpm_set_registration - Set the client registration
0161  * @nl_client: The index of the netlink client
0162  * @reg: Registration type to set
0163  */
0164 void iwpm_set_registration(u8 nl_client, u32 reg);
0165 
0166 /**
0167  * iwpm_get_registration - Get the client registration
0168  * @nl_client: The index of the netlink client
0169  *
0170  * Returns the client registration type
0171  */
0172 u32 iwpm_get_registration(u8 nl_client);
0173 
0174 /**
0175  * iwpm_send_mapinfo - Send local and mapped IPv4/IPv6 address info of
0176  *                     a client to the user space port mapper
0177  * @nl_client: The index of the netlink client
0178  * @iwpm_pid: The pid of the user space port mapper
0179  *
0180  * If successful, returns the number of sent mapping info records
0181  */
0182 int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid);
0183 
0184 /**
0185  * iwpm_mapinfo_available - Check if any mapping info records is available
0186  *                  in the hash table
0187  *
0188  * Returns 1 if mapping information is available, otherwise returns 0
0189  */
0190 int iwpm_mapinfo_available(void);
0191 
0192 /**
0193  * iwpm_compare_sockaddr - Compare two sockaddr storage structs
0194  * @a_sockaddr: first sockaddr to compare
0195  * @b_sockaddr: second sockaddr to compare
0196  *
0197  * Return: 0 if they are holding the same ip/tcp address info,
0198  * otherwise returns 1
0199  */
0200 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
0201             struct sockaddr_storage *b_sockaddr);
0202 
0203 /**
0204  * iwpm_validate_nlmsg_attr - Check for NULL netlink attributes
0205  * @nltb: Holds address of each netlink message attributes
0206  * @nla_count: Number of netlink message attributes
0207  *
0208  * Returns error if any of the nla_count attributes is NULL
0209  */
0210 static inline int iwpm_validate_nlmsg_attr(struct nlattr *nltb[],
0211                        int nla_count)
0212 {
0213     int i;
0214     for (i = 1; i < nla_count; i++) {
0215         if (!nltb[i])
0216             return -EINVAL;
0217     }
0218     return 0;
0219 }
0220 
0221 /**
0222  * iwpm_create_nlmsg - Allocate skb and form a netlink message
0223  * @nl_op: Netlink message opcode
0224  * @nlh: Holds address of the netlink message header in skb
0225  * @nl_client: The index of the netlink client
0226  *
0227  * Returns the newly allcated skb, or NULL if the tailroom of the skb
0228  * is insufficient to store the message header and payload
0229  */
0230 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
0231                     int nl_client);
0232 
0233 /**
0234  * iwpm_parse_nlmsg - Validate and parse the received netlink message
0235  * @cb: Netlink callback structure
0236  * @policy_max: Maximum attribute type to be expected
0237  * @nlmsg_policy: Validation policy
0238  * @nltb: Array to store policy_max parsed elements
0239  * @msg_type: Type of netlink message
0240  *
0241  * Returns 0 on success or a negative error code
0242  */
0243 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
0244                 const struct nla_policy *nlmsg_policy,
0245                 struct nlattr *nltb[], const char *msg_type);
0246 
0247 /**
0248  * iwpm_print_sockaddr - Print IPv4/IPv6 address and TCP port
0249  * @sockaddr: Socket address to print
0250  * @msg: Message to print
0251  */
0252 void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg);
0253 
0254 /**
0255  * iwpm_send_hello - Send hello response to iwpmd
0256  *
0257  * @nl_client: The index of the netlink client
0258  * @iwpm_pid: The pid of the user space port mapper
0259  * @abi_version: The kernel's abi_version
0260  *
0261  * Returns 0 on success or a negative error code
0262  */
0263 int iwpm_send_hello(u8 nl_client, int iwpm_pid, u16 abi_version);
0264 extern u16 iwpm_ulib_version;
0265 #endif