bind_addr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2003
  3. * Copyright (c) Cisco 1999,2000
  4. * Copyright (c) Motorola 1999,2000,2001
  5. * Copyright (c) La Monte H.P. Yarroll 2001
  6. *
  7. * This file is part of the SCTP kernel reference implementation.
  8. *
  9. * A collection class to handle the storage of transport addresses.
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * La Monte H.P. Yarroll <piggy@acm.org>
  37. * Karl Knutson <karl@athena.chicago.il.us>
  38. * Jon Grimm <jgrimm@us.ibm.com>
  39. * Daisy Chang <daisyc@us.ibm.com>
  40. *
  41. * Any bugs reported given to us we will try to fix... any fixes shared will
  42. * be incorporated into the next SCTP release.
  43. */
  44. #include <linux/types.h>
  45. #include <linux/in.h>
  46. #include <net/sock.h>
  47. #include <net/ipv6.h>
  48. #include <net/if_inet6.h>
  49. #include <net/sctp/sctp.h>
  50. #include <net/sctp/sm.h>
  51. /* Forward declarations for internal helpers. */
  52. static int sctp_copy_one_addr(struct sctp_bind_addr *, union sctp_addr *,
  53. sctp_scope_t scope, gfp_t gfp,
  54. int flags);
  55. static void sctp_bind_addr_clean(struct sctp_bind_addr *);
  56. /* First Level Abstractions. */
  57. /* Copy 'src' to 'dest' taking 'scope' into account. Omit addresses
  58. * in 'src' which have a broader scope than 'scope'.
  59. */
  60. int sctp_bind_addr_copy(struct sctp_bind_addr *dest,
  61. const struct sctp_bind_addr *src,
  62. sctp_scope_t scope, gfp_t gfp,
  63. int flags)
  64. {
  65. struct sctp_sockaddr_entry *addr;
  66. struct list_head *pos;
  67. int error = 0;
  68. /* All addresses share the same port. */
  69. dest->port = src->port;
  70. /* Extract the addresses which are relevant for this scope. */
  71. list_for_each(pos, &src->address_list) {
  72. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  73. error = sctp_copy_one_addr(dest, &addr->a, scope,
  74. gfp, flags);
  75. if (error < 0)
  76. goto out;
  77. }
  78. /* If there are no addresses matching the scope and
  79. * this is global scope, try to get a link scope address, with
  80. * the assumption that we must be sitting behind a NAT.
  81. */
  82. if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
  83. list_for_each(pos, &src->address_list) {
  84. addr = list_entry(pos, struct sctp_sockaddr_entry,
  85. list);
  86. error = sctp_copy_one_addr(dest, &addr->a,
  87. SCTP_SCOPE_LINK, gfp,
  88. flags);
  89. if (error < 0)
  90. goto out;
  91. }
  92. }
  93. out:
  94. if (error)
  95. sctp_bind_addr_clean(dest);
  96. return error;
  97. }
  98. /* Initialize the SCTP_bind_addr structure for either an endpoint or
  99. * an association.
  100. */
  101. void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
  102. {
  103. bp->malloced = 0;
  104. INIT_LIST_HEAD(&bp->address_list);
  105. bp->port = port;
  106. }
  107. /* Dispose of the address list. */
  108. static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
  109. {
  110. struct sctp_sockaddr_entry *addr;
  111. struct list_head *pos, *temp;
  112. /* Empty the bind address list. */
  113. list_for_each_safe(pos, temp, &bp->address_list) {
  114. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  115. list_del(pos);
  116. kfree(addr);
  117. SCTP_DBG_OBJCNT_DEC(addr);
  118. }
  119. }
  120. /* Dispose of an SCTP_bind_addr structure */
  121. void sctp_bind_addr_free(struct sctp_bind_addr *bp)
  122. {
  123. /* Empty the bind address list. */
  124. sctp_bind_addr_clean(bp);
  125. if (bp->malloced) {
  126. kfree(bp);
  127. SCTP_DBG_OBJCNT_DEC(bind_addr);
  128. }
  129. }
  130. /* Add an address to the bind address list in the SCTP_bind_addr structure. */
  131. int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
  132. __u8 use_as_src, gfp_t gfp)
  133. {
  134. struct sctp_sockaddr_entry *addr;
  135. /* Add the address to the bind address list. */
  136. addr = t_new(struct sctp_sockaddr_entry, gfp);
  137. if (!addr)
  138. return -ENOMEM;
  139. memcpy(&addr->a, new, sizeof(*new));
  140. /* Fix up the port if it has not yet been set.
  141. * Both v4 and v6 have the port at the same offset.
  142. */
  143. if (!addr->a.v4.sin_port)
  144. addr->a.v4.sin_port = htons(bp->port);
  145. addr->use_as_src = use_as_src;
  146. addr->valid = 1;
  147. INIT_LIST_HEAD(&addr->list);
  148. INIT_RCU_HEAD(&addr->rcu);
  149. /* We always hold a socket lock when calling this function,
  150. * and that acts as a writer synchronizing lock.
  151. */
  152. list_add_tail_rcu(&addr->list, &bp->address_list);
  153. SCTP_DBG_OBJCNT_INC(addr);
  154. return 0;
  155. }
  156. /* Delete an address from the bind address list in the SCTP_bind_addr
  157. * structure.
  158. */
  159. int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr,
  160. void fastcall (*rcu_call)(struct rcu_head *head,
  161. void (*func)(struct rcu_head *head)))
  162. {
  163. struct sctp_sockaddr_entry *addr, *temp;
  164. /* We hold the socket lock when calling this function,
  165. * and that acts as a writer synchronizing lock.
  166. */
  167. list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
  168. if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
  169. /* Found the exact match. */
  170. addr->valid = 0;
  171. list_del_rcu(&addr->list);
  172. break;
  173. }
  174. }
  175. /* Call the rcu callback provided in the args. This function is
  176. * called by both BH packet processing and user side socket option
  177. * processing, but it works on different lists in those 2 contexts.
  178. * Each context provides it's own callback, whether call_rcu_bh()
  179. * or call_rcu(), to make sure that we wait for an appropriate time.
  180. */
  181. if (addr && !addr->valid) {
  182. rcu_call(&addr->rcu, sctp_local_addr_free);
  183. SCTP_DBG_OBJCNT_DEC(addr);
  184. }
  185. return -EINVAL;
  186. }
  187. /* Create a network byte-order representation of all the addresses
  188. * formated as SCTP parameters.
  189. *
  190. * The second argument is the return value for the length.
  191. */
  192. union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
  193. int *addrs_len,
  194. gfp_t gfp)
  195. {
  196. union sctp_params addrparms;
  197. union sctp_params retval;
  198. int addrparms_len;
  199. union sctp_addr_param rawaddr;
  200. int len;
  201. struct sctp_sockaddr_entry *addr;
  202. struct list_head *pos;
  203. struct sctp_af *af;
  204. addrparms_len = 0;
  205. len = 0;
  206. /* Allocate enough memory at once. */
  207. list_for_each(pos, &bp->address_list) {
  208. len += sizeof(union sctp_addr_param);
  209. }
  210. /* Don't even bother embedding an address if there
  211. * is only one.
  212. */
  213. if (len == sizeof(union sctp_addr_param)) {
  214. retval.v = NULL;
  215. goto end_raw;
  216. }
  217. retval.v = kmalloc(len, gfp);
  218. if (!retval.v)
  219. goto end_raw;
  220. addrparms = retval;
  221. list_for_each(pos, &bp->address_list) {
  222. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  223. af = sctp_get_af_specific(addr->a.v4.sin_family);
  224. len = af->to_addr_param(&addr->a, &rawaddr);
  225. memcpy(addrparms.v, &rawaddr, len);
  226. addrparms.v += len;
  227. addrparms_len += len;
  228. }
  229. end_raw:
  230. *addrs_len = addrparms_len;
  231. return retval;
  232. }
  233. /*
  234. * Create an address list out of the raw address list format (IPv4 and IPv6
  235. * address parameters).
  236. */
  237. int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
  238. int addrs_len, __u16 port, gfp_t gfp)
  239. {
  240. union sctp_addr_param *rawaddr;
  241. struct sctp_paramhdr *param;
  242. union sctp_addr addr;
  243. int retval = 0;
  244. int len;
  245. struct sctp_af *af;
  246. /* Convert the raw address to standard address format */
  247. while (addrs_len) {
  248. param = (struct sctp_paramhdr *)raw_addr_list;
  249. rawaddr = (union sctp_addr_param *)raw_addr_list;
  250. af = sctp_get_af_specific(param_type2af(param->type));
  251. if (unlikely(!af)) {
  252. retval = -EINVAL;
  253. sctp_bind_addr_clean(bp);
  254. break;
  255. }
  256. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  257. retval = sctp_add_bind_addr(bp, &addr, 1, gfp);
  258. if (retval) {
  259. /* Can't finish building the list, clean up. */
  260. sctp_bind_addr_clean(bp);
  261. break;
  262. }
  263. len = ntohs(param->length);
  264. addrs_len -= len;
  265. raw_addr_list += len;
  266. }
  267. return retval;
  268. }
  269. /********************************************************************
  270. * 2nd Level Abstractions
  271. ********************************************************************/
  272. /* Does this contain a specified address? Allow wildcarding. */
  273. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  274. const union sctp_addr *addr,
  275. struct sctp_sock *opt)
  276. {
  277. struct sctp_sockaddr_entry *laddr;
  278. int match = 0;
  279. rcu_read_lock();
  280. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  281. if (!laddr->valid)
  282. continue;
  283. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  284. match = 1;
  285. break;
  286. }
  287. }
  288. rcu_read_unlock();
  289. return match;
  290. }
  291. /* Find the first address in the bind address list that is not present in
  292. * the addrs packed array.
  293. */
  294. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  295. const union sctp_addr *addrs,
  296. int addrcnt,
  297. struct sctp_sock *opt)
  298. {
  299. struct sctp_sockaddr_entry *laddr;
  300. union sctp_addr *addr;
  301. void *addr_buf;
  302. struct sctp_af *af;
  303. int i;
  304. /* This is only called sctp_send_asconf_del_ip() and we hold
  305. * the socket lock in that code patch, so that address list
  306. * can't change.
  307. */
  308. list_for_each_entry(laddr, &bp->address_list, list) {
  309. addr_buf = (union sctp_addr *)addrs;
  310. for (i = 0; i < addrcnt; i++) {
  311. addr = (union sctp_addr *)addr_buf;
  312. af = sctp_get_af_specific(addr->v4.sin_family);
  313. if (!af)
  314. break;
  315. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  316. break;
  317. addr_buf += af->sockaddr_len;
  318. }
  319. if (i == addrcnt)
  320. return &laddr->a;
  321. }
  322. return NULL;
  323. }
  324. /* Copy out addresses from the global local address list. */
  325. static int sctp_copy_one_addr(struct sctp_bind_addr *dest,
  326. union sctp_addr *addr,
  327. sctp_scope_t scope, gfp_t gfp,
  328. int flags)
  329. {
  330. int error = 0;
  331. if (sctp_is_any(addr)) {
  332. error = sctp_copy_local_addr_list(dest, scope, gfp, flags);
  333. } else if (sctp_in_scope(addr, scope)) {
  334. /* Now that the address is in scope, check to see if
  335. * the address type is supported by local sock as
  336. * well as the remote peer.
  337. */
  338. if ((((AF_INET == addr->sa.sa_family) &&
  339. (flags & SCTP_ADDR4_PEERSUPP))) ||
  340. (((AF_INET6 == addr->sa.sa_family) &&
  341. (flags & SCTP_ADDR6_ALLOWED) &&
  342. (flags & SCTP_ADDR6_PEERSUPP))))
  343. error = sctp_add_bind_addr(dest, addr, 1, gfp);
  344. }
  345. return error;
  346. }
  347. /* Is this a wildcard address? */
  348. int sctp_is_any(const union sctp_addr *addr)
  349. {
  350. struct sctp_af *af = sctp_get_af_specific(addr->sa.sa_family);
  351. if (!af)
  352. return 0;
  353. return af->is_any(addr);
  354. }
  355. /* Is 'addr' valid for 'scope'? */
  356. int sctp_in_scope(const union sctp_addr *addr, sctp_scope_t scope)
  357. {
  358. sctp_scope_t addr_scope = sctp_scope(addr);
  359. /* The unusable SCTP addresses will not be considered with
  360. * any defined scopes.
  361. */
  362. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  363. return 0;
  364. /*
  365. * For INIT and INIT-ACK address list, let L be the level of
  366. * of requested destination address, sender and receiver
  367. * SHOULD include all of its addresses with level greater
  368. * than or equal to L.
  369. */
  370. if (addr_scope <= scope)
  371. return 1;
  372. return 0;
  373. }
  374. /********************************************************************
  375. * 3rd Level Abstractions
  376. ********************************************************************/
  377. /* What is the scope of 'addr'? */
  378. sctp_scope_t sctp_scope(const union sctp_addr *addr)
  379. {
  380. struct sctp_af *af;
  381. af = sctp_get_af_specific(addr->sa.sa_family);
  382. if (!af)
  383. return SCTP_SCOPE_UNUSABLE;
  384. return af->scope((union sctp_addr *)addr);
  385. }