Linux Audio

Check our new training course

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