iwpm_util.h 8.1 KB

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