2 * Copyright (c) 2014 Intel Corporation. All rights reserved.
3 * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
36 #include <linux/module.h>
39 #include <linux/in6.h>
40 #include <linux/spinlock.h>
41 #include <linux/kernel.h>
42 #include <linux/netdevice.h>
43 #include <linux/delay.h>
44 #include <linux/workqueue.h>
45 #include <linux/mutex.h>
46 #include <linux/jhash.h>
47 #include <linux/kref.h>
48 #include <net/netlink.h>
49 #include <linux/errno.h>
50 #include <rdma/iw_portmap.h>
51 #include <rdma/rdma_netlink.h>
54 #define IWPM_NL_RETRANS 3
55 #define IWPM_NL_TIMEOUT (10*HZ)
56 #define IWPM_MAPINFO_SKB_COUNT 20
58 #define IWPM_PID_UNDEFINED -1
59 #define IWPM_PID_UNAVAILABLE -2
61 struct iwpm_nlmsg_request {
62 struct list_head inprocess_list;
68 wait_queue_head_t waitq;
72 struct iwpm_mapping_info {
73 struct hlist_node hlist_node;
74 struct sockaddr_storage local_sockaddr;
75 struct sockaddr_storage mapped_sockaddr;
79 struct iwpm_remote_info {
80 struct hlist_node hlist_node;
81 struct sockaddr_storage remote_sockaddr;
82 struct sockaddr_storage mapped_loc_sockaddr;
83 struct sockaddr_storage mapped_rem_sockaddr;
87 struct iwpm_admin_data {
90 int client_list[RDMA_NL_NUM_CLIENTS];
91 int reg_list[RDMA_NL_NUM_CLIENTS];
95 * iwpm_get_nlmsg_request - Allocate and initialize netlink message request
96 * @nlmsg_seq: Sequence number of the netlink message
97 * @nl_client: The index of the netlink client
98 * @gfp: Indicates how the memory for the request should be allocated
100 * Returns the newly allocated netlink request object if successful,
101 * otherwise returns NULL
103 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
104 u8 nl_client, gfp_t gfp);
107 * iwpm_free_nlmsg_request - Deallocate netlink message request
108 * @kref: Holds reference of netlink message request
110 void iwpm_free_nlmsg_request(struct kref *kref);
113 * iwpm_find_nlmsg_request - Find netlink message request in the request list
114 * @echo_seq: Sequence number of the netlink request to find
116 * Returns the found netlink message request,
117 * if not found, returns NULL
119 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq);
122 * iwpm_wait_complete_req - Block while servicing the netlink request
123 * @nlmsg_request: Netlink message request to service
125 * Wakes up, after the request is completed or expired
126 * Returns 0 if the request is complete without error
128 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request);
131 * iwpm_get_nlmsg_seq - Get the sequence number for a netlink
132 * message to send to the port mapper
134 * Returns the sequence number for the netlink message.
136 int iwpm_get_nlmsg_seq(void);
139 * iwpm_add_reminfo - Add remote address info of the connecting peer
140 * to the remote info hash table
141 * @reminfo: The remote info to be added
143 void iwpm_add_remote_info(struct iwpm_remote_info *reminfo);
146 * iwpm_valid_client - Check if the port mapper client is valid
147 * @nl_client: The index of the netlink client
149 * Valid clients need to call iwpm_init() before using
152 int iwpm_valid_client(u8 nl_client);
155 * iwpm_set_valid - Set the port mapper client to valid or not
156 * @nl_client: The index of the netlink client
157 * @valid: 1 if valid or 0 if invalid
159 void iwpm_set_valid(u8 nl_client, int valid);
162 * iwpm_registered_client - Check if the port mapper client is registered
163 * @nl_client: The index of the netlink client
165 * Call iwpm_register_pid() to register a client
167 int iwpm_registered_client(u8 nl_client);
170 * iwpm_set_registered - Set the port mapper client to registered or not
171 * @nl_client: The index of the netlink client
172 * @reg: 1 if registered or 0 if not
174 void iwpm_set_registered(u8 nl_client, int reg);
177 * iwpm_send_mapinfo - Send local and mapped IPv4/IPv6 address info of
178 * a client to the user space port mapper
179 * @nl_client: The index of the netlink client
180 * @iwpm_pid: The pid of the user space port mapper
182 * If successful, returns the number of sent mapping info records
184 int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid);
187 * iwpm_mapinfo_available - Check if any mapping info records is available
190 * Returns 1 if mapping information is available, otherwise returns 0
192 int iwpm_mapinfo_available(void);
195 * iwpm_compare_sockaddr - Compare two sockaddr storage structs
197 * Returns 0 if they are holding the same ip/tcp address info,
198 * otherwise returns 1
200 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
201 struct sockaddr_storage *b_sockaddr);
204 * iwpm_validate_nlmsg_attr - Check for NULL netlink attributes
205 * @nltb: Holds address of each netlink message attributes
206 * @nla_count: Number of netlink message attributes
208 * Returns error if any of the nla_count attributes is NULL
210 static inline int iwpm_validate_nlmsg_attr(struct nlattr *nltb[],
214 for (i = 1; i < nla_count; i++) {
222 * iwpm_create_nlmsg - Allocate skb and form a netlink message
223 * @nl_op: Netlink message opcode
224 * @nlh: Holds address of the netlink message header in skb
225 * @nl_client: The index of the netlink client
227 * Returns the newly allcated skb, or NULL if the tailroom of the skb
228 * is insufficient to store the message header and payload
230 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
234 * iwpm_parse_nlmsg - Validate and parse the received netlink message
235 * @cb: Netlink callback structure
236 * @policy_max: Maximum attribute type to be expected
237 * @nlmsg_policy: Validation policy
238 * @nltb: Array to store policy_max parsed elements
239 * @msg_type: Type of netlink message
241 * Returns 0 on success or a negative error code
243 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
244 const struct nla_policy *nlmsg_policy,
245 struct nlattr *nltb[], const char *msg_type);
248 * iwpm_print_sockaddr - Print IPv4/IPv6 address and TCP port
249 * @sockaddr: Socket address to print
250 * @msg: Message to print
252 void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg);