bind_addr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. {
  161. struct sctp_sockaddr_entry *addr, *temp;
  162. /* We hold the socket lock when calling this function,
  163. * and that acts as a writer synchronizing lock.
  164. */
  165. list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
  166. if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
  167. /* Found the exact match. */
  168. addr->valid = 0;
  169. list_del_rcu(&addr->list);
  170. break;
  171. }
  172. }
  173. if (addr && !addr->valid) {
  174. call_rcu(&addr->rcu, sctp_local_addr_free);
  175. SCTP_DBG_OBJCNT_DEC(addr);
  176. return 0;
  177. }
  178. return -EINVAL;
  179. }
  180. /* Create a network byte-order representation of all the addresses
  181. * formated as SCTP parameters.
  182. *
  183. * The second argument is the return value for the length.
  184. */
  185. union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
  186. int *addrs_len,
  187. gfp_t gfp)
  188. {
  189. union sctp_params addrparms;
  190. union sctp_params retval;
  191. int addrparms_len;
  192. union sctp_addr_param rawaddr;
  193. int len;
  194. struct sctp_sockaddr_entry *addr;
  195. struct list_head *pos;
  196. struct sctp_af *af;
  197. addrparms_len = 0;
  198. len = 0;
  199. /* Allocate enough memory at once. */
  200. list_for_each(pos, &bp->address_list) {
  201. len += sizeof(union sctp_addr_param);
  202. }
  203. /* Don't even bother embedding an address if there
  204. * is only one.
  205. */
  206. if (len == sizeof(union sctp_addr_param)) {
  207. retval.v = NULL;
  208. goto end_raw;
  209. }
  210. retval.v = kmalloc(len, gfp);
  211. if (!retval.v)
  212. goto end_raw;
  213. addrparms = retval;
  214. list_for_each(pos, &bp->address_list) {
  215. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  216. af = sctp_get_af_specific(addr->a.v4.sin_family);
  217. len = af->to_addr_param(&addr->a, &rawaddr);
  218. memcpy(addrparms.v, &rawaddr, len);
  219. addrparms.v += len;
  220. addrparms_len += len;
  221. }
  222. end_raw:
  223. *addrs_len = addrparms_len;
  224. return retval;
  225. }
  226. /*
  227. * Create an address list out of the raw address list format (IPv4 and IPv6
  228. * address parameters).
  229. */
  230. int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
  231. int addrs_len, __u16 port, gfp_t gfp)
  232. {
  233. union sctp_addr_param *rawaddr;
  234. struct sctp_paramhdr *param;
  235. union sctp_addr addr;
  236. int retval = 0;
  237. int len;
  238. struct sctp_af *af;
  239. /* Convert the raw address to standard address format */
  240. while (addrs_len) {
  241. param = (struct sctp_paramhdr *)raw_addr_list;
  242. rawaddr = (union sctp_addr_param *)raw_addr_list;
  243. af = sctp_get_af_specific(param_type2af(param->type));
  244. if (unlikely(!af)) {
  245. retval = -EINVAL;
  246. sctp_bind_addr_clean(bp);
  247. break;
  248. }
  249. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  250. retval = sctp_add_bind_addr(bp, &addr, 1, gfp);
  251. if (retval) {
  252. /* Can't finish building the list, clean up. */
  253. sctp_bind_addr_clean(bp);
  254. break;
  255. }
  256. len = ntohs(param->length);
  257. addrs_len -= len;
  258. raw_addr_list += len;
  259. }
  260. return retval;
  261. }
  262. /********************************************************************
  263. * 2nd Level Abstractions
  264. ********************************************************************/
  265. /* Does this contain a specified address? Allow wildcarding. */
  266. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  267. const union sctp_addr *addr,
  268. struct sctp_sock *opt)
  269. {
  270. struct sctp_sockaddr_entry *laddr;
  271. int match = 0;
  272. rcu_read_lock();
  273. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  274. if (!laddr->valid)
  275. continue;
  276. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  277. match = 1;
  278. break;
  279. }
  280. }
  281. rcu_read_unlock();
  282. return match;
  283. }
  284. /* Find the first address in the bind address list that is not present in
  285. * the addrs packed array.
  286. */
  287. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  288. const union sctp_addr *addrs,
  289. int addrcnt,
  290. struct sctp_sock *opt)
  291. {
  292. struct sctp_sockaddr_entry *laddr;
  293. union sctp_addr *addr;
  294. void *addr_buf;
  295. struct sctp_af *af;
  296. int i;
  297. /* This is only called sctp_send_asconf_del_ip() and we hold
  298. * the socket lock in that code patch, so that address list
  299. * can't change.
  300. */
  301. list_for_each_entry(laddr, &bp->address_list, list) {
  302. addr_buf = (union sctp_addr *)addrs;
  303. for (i = 0; i < addrcnt; i++) {
  304. addr = (union sctp_addr *)addr_buf;
  305. af = sctp_get_af_specific(addr->v4.sin_family);
  306. if (!af)
  307. break;
  308. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  309. break;
  310. addr_buf += af->sockaddr_len;
  311. }
  312. if (i == addrcnt)
  313. return &laddr->a;
  314. }
  315. return NULL;
  316. }
  317. /* Copy out addresses from the global local address list. */
  318. static int sctp_copy_one_addr(struct sctp_bind_addr *dest,
  319. union sctp_addr *addr,
  320. sctp_scope_t scope, gfp_t gfp,
  321. int flags)
  322. {
  323. int error = 0;
  324. if (sctp_is_any(addr)) {
  325. error = sctp_copy_local_addr_list(dest, scope, gfp, flags);
  326. } else if (sctp_in_scope(addr, scope)) {
  327. /* Now that the address is in scope, check to see if
  328. * the address type is supported by local sock as
  329. * well as the remote peer.
  330. */
  331. if ((((AF_INET == addr->sa.sa_family) &&
  332. (flags & SCTP_ADDR4_PEERSUPP))) ||
  333. (((AF_INET6 == addr->sa.sa_family) &&
  334. (flags & SCTP_ADDR6_ALLOWED) &&
  335. (flags & SCTP_ADDR6_PEERSUPP))))
  336. error = sctp_add_bind_addr(dest, addr, 1, gfp);
  337. }
  338. return error;
  339. }
  340. /* Is this a wildcard address? */
  341. int sctp_is_any(const union sctp_addr *addr)
  342. {
  343. struct sctp_af *af = sctp_get_af_specific(addr->sa.sa_family);
  344. if (!af)
  345. return 0;
  346. return af->is_any(addr);
  347. }
  348. /* Is 'addr' valid for 'scope'? */
  349. int sctp_in_scope(const union sctp_addr *addr, sctp_scope_t scope)
  350. {
  351. sctp_scope_t addr_scope = sctp_scope(addr);
  352. /* The unusable SCTP addresses will not be considered with
  353. * any defined scopes.
  354. */
  355. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  356. return 0;
  357. /*
  358. * For INIT and INIT-ACK address list, let L be the level of
  359. * of requested destination address, sender and receiver
  360. * SHOULD include all of its addresses with level greater
  361. * than or equal to L.
  362. */
  363. if (addr_scope <= scope)
  364. return 1;
  365. return 0;
  366. }
  367. /********************************************************************
  368. * 3rd Level Abstractions
  369. ********************************************************************/
  370. /* What is the scope of 'addr'? */
  371. sctp_scope_t sctp_scope(const union sctp_addr *addr)
  372. {
  373. struct sctp_af *af;
  374. af = sctp_get_af_specific(addr->sa.sa_family);
  375. if (!af)
  376. return SCTP_SCOPE_UNUSABLE;
  377. return af->scope((union sctp_addr *)addr);
  378. }