1 /*
2  * Copyright (c) 2014 Intel Corporation. All rights reserved.
3  * Copyright (c) 2014 Chelsio, Inc. 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 #ifndef _IWPM_UTIL_H
34 #define _IWPM_UTIL_H
35 
36 #include <linux/module.h>
37 #include <linux/io.h>
38 #include <linux/in.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>
52 
53 
54 #define IWPM_NL_RETRANS		3
55 #define IWPM_NL_TIMEOUT		(10*HZ)
56 #define IWPM_MAPINFO_SKB_COUNT	20
57 
58 #define IWPM_PID_UNDEFINED     -1
59 #define IWPM_PID_UNAVAILABLE   -2
60 
61 #define IWPM_REG_UNDEF          0x01
62 #define IWPM_REG_VALID          0x02
63 #define IWPM_REG_INCOMPL        0x04
64 
65 struct iwpm_nlmsg_request {
66 	struct list_head    inprocess_list;
67 	__u32               nlmsg_seq;
68 	void                *req_buffer;
69 	u8	            nl_client;
70 	u8                  request_done;
71 	u16                 err_code;
72 	struct semaphore    sem;
73 	struct kref         kref;
74 };
75 
76 struct iwpm_mapping_info {
77 	struct hlist_node hlist_node;
78 	struct sockaddr_storage local_sockaddr;
79 	struct sockaddr_storage mapped_sockaddr;
80 	u8     nl_client;
81 	u32    map_flags;
82 };
83 
84 struct iwpm_remote_info {
85 	struct hlist_node hlist_node;
86 	struct sockaddr_storage remote_sockaddr;
87 	struct sockaddr_storage mapped_loc_sockaddr;
88 	struct sockaddr_storage mapped_rem_sockaddr;
89 	u8     nl_client;
90 };
91 
92 struct iwpm_admin_data {
93 	atomic_t refcount;
94 	atomic_t nlmsg_seq;
95 	int      client_list[RDMA_NL_NUM_CLIENTS];
96 	u32      reg_list[RDMA_NL_NUM_CLIENTS];
97 };
98 
99 /**
100  * iwpm_get_nlmsg_request - Allocate and initialize netlink message request
101  * @nlmsg_seq: Sequence number of the netlink message
102  * @nl_client: The index of the netlink client
103  * @gfp: Indicates how the memory for the request should be allocated
104  *
105  * Returns the newly allocated netlink request object if successful,
106  * otherwise returns NULL
107  */
108 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
109 						u8 nl_client, gfp_t gfp);
110 
111 /**
112  * iwpm_free_nlmsg_request - Deallocate netlink message request
113  * @kref: Holds reference of netlink message request
114  */
115 void iwpm_free_nlmsg_request(struct kref *kref);
116 
117 /**
118  * iwpm_find_nlmsg_request - Find netlink message request in the request list
119  * @echo_seq: Sequence number of the netlink request to find
120  *
121  * Returns the found netlink message request,
122  * if not found, returns NULL
123  */
124 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq);
125 
126 /**
127  * iwpm_wait_complete_req - Block while servicing the netlink request
128  * @nlmsg_request: Netlink message request to service
129  *
130  * Wakes up, after the request is completed or expired
131  * Returns 0 if the request is complete without error
132  */
133 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request);
134 
135 /**
136  * iwpm_get_nlmsg_seq - Get the sequence number for a netlink
137  *			message to send to the port mapper
138  *
139  * Returns the sequence number for the netlink message.
140  */
141 int iwpm_get_nlmsg_seq(void);
142 
143 /**
144  * iwpm_add_reminfo - Add remote address info of the connecting peer
145  *                    to the remote info hash table
146  * @reminfo: The remote info to be added
147  */
148 void iwpm_add_remote_info(struct iwpm_remote_info *reminfo);
149 
150 /**
151  * iwpm_valid_client - Check if the port mapper client is valid
152  * @nl_client: The index of the netlink client
153  *
154  * Valid clients need to call iwpm_init() before using
155  * the port mapper
156  */
157 int iwpm_valid_client(u8 nl_client);
158 
159 /**
160  * iwpm_set_valid - Set the port mapper client to valid or not
161  * @nl_client: The index of the netlink client
162  * @valid: 1 if valid or 0 if invalid
163  */
164 void iwpm_set_valid(u8 nl_client, int valid);
165 
166 /**
167  * iwpm_check_registration - Check if the client registration
168  *			      matches the given one
169  * @nl_client: The index of the netlink client
170  * @reg: The given registration type to compare with
171  *
172  * Call iwpm_register_pid() to register a client
173  * Returns true if the client registration matches reg,
174  * otherwise returns false
175  */
176 u32 iwpm_check_registration(u8 nl_client, u32 reg);
177 
178 /**
179  * iwpm_set_registration - Set the client registration
180  * @nl_client: The index of the netlink client
181  * @reg: Registration type to set
182  */
183 void iwpm_set_registration(u8 nl_client, u32 reg);
184 
185 /**
186  * iwpm_get_registration
187  * @nl_client: The index of the netlink client
188  *
189  * Returns the client registration type
190  */
191 u32 iwpm_get_registration(u8 nl_client);
192 
193 /**
194  * iwpm_send_mapinfo - Send local and mapped IPv4/IPv6 address info of
195  *                     a client to the user space port mapper
196  * @nl_client: The index of the netlink client
197  * @iwpm_pid: The pid of the user space port mapper
198  *
199  * If successful, returns the number of sent mapping info records
200  */
201 int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid);
202 
203 /**
204  * iwpm_mapinfo_available - Check if any mapping info records is available
205  *		            in the hash table
206  *
207  * Returns 1 if mapping information is available, otherwise returns 0
208  */
209 int iwpm_mapinfo_available(void);
210 
211 /**
212  * iwpm_compare_sockaddr - Compare two sockaddr storage structs
213  *
214  * Returns 0 if they are holding the same ip/tcp address info,
215  * otherwise returns 1
216  */
217 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
218 			struct sockaddr_storage *b_sockaddr);
219 
220 /**
221  * iwpm_validate_nlmsg_attr - Check for NULL netlink attributes
222  * @nltb: Holds address of each netlink message attributes
223  * @nla_count: Number of netlink message attributes
224  *
225  * Returns error if any of the nla_count attributes is NULL
226  */
227 static inline int iwpm_validate_nlmsg_attr(struct nlattr *nltb[],
228 					   int nla_count)
229 {
230 	int i;
231 	for (i = 1; i < nla_count; i++) {
232 		if (!nltb[i])
233 			return -EINVAL;
234 	}
235 	return 0;
236 }
237 
238 /**
239  * iwpm_create_nlmsg - Allocate skb and form a netlink message
240  * @nl_op: Netlink message opcode
241  * @nlh: Holds address of the netlink message header in skb
242  * @nl_client: The index of the netlink client
243  *
244  * Returns the newly allcated skb, or NULL if the tailroom of the skb
245  * is insufficient to store the message header and payload
246  */
247 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
248 					int nl_client);
249 
250 /**
251  * iwpm_parse_nlmsg - Validate and parse the received netlink message
252  * @cb: Netlink callback structure
253  * @policy_max: Maximum attribute type to be expected
254  * @nlmsg_policy: Validation policy
255  * @nltb: Array to store policy_max parsed elements
256  * @msg_type: Type of netlink message
257  *
258  * Returns 0 on success or a negative error code
259  */
260 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
261 				const struct nla_policy *nlmsg_policy,
262 				struct nlattr *nltb[], const char *msg_type);
263 
264 /**
265  * iwpm_print_sockaddr - Print IPv4/IPv6 address and TCP port
266  * @sockaddr: Socket address to print
267  * @msg: Message to print
268  */
269 void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg);
270 
271 /**
272  * iwpm_send_hello - Send hello response to iwpmd
273  *
274  * @nl_client: The index of the netlink client
275  * @abi_version: The kernel's abi_version
276  *
277  * Returns 0 on success or a negative error code
278  */
279 int iwpm_send_hello(u8 nl_client, int iwpm_pid, u16 abi_version);
280 extern u16 iwpm_ulib_version;
281 #endif
282