bind_addr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/sched.h>
  46. #include <linux/in.h>
  47. #include <net/sock.h>
  48. #include <net/ipv6.h>
  49. #include <net/if_inet6.h>
  50. #include <net/sctp/sctp.h>
  51. #include <net/sctp/sm.h>
  52. /* Forward declarations for internal helpers. */
  53. static int sctp_copy_one_addr(struct sctp_bind_addr *, union sctp_addr *,
  54. sctp_scope_t scope, gfp_t gfp,
  55. int flags);
  56. static void sctp_bind_addr_clean(struct sctp_bind_addr *);
  57. /* First Level Abstractions. */
  58. /* Copy 'src' to 'dest' taking 'scope' into account. Omit addresses
  59. * in 'src' which have a broader scope than 'scope'.
  60. */
  61. int sctp_bind_addr_copy(struct sctp_bind_addr *dest,
  62. const struct sctp_bind_addr *src,
  63. sctp_scope_t scope, gfp_t gfp,
  64. int flags)
  65. {
  66. struct sctp_sockaddr_entry *addr;
  67. struct list_head *pos;
  68. int error = 0;
  69. /* All addresses share the same port. */
  70. dest->port = src->port;
  71. /* Extract the addresses which are relevant for this scope. */
  72. list_for_each(pos, &src->address_list) {
  73. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  74. error = sctp_copy_one_addr(dest, &addr->a_h, scope,
  75. gfp, flags);
  76. if (error < 0)
  77. goto out;
  78. }
  79. /* If there are no addresses matching the scope and
  80. * this is global scope, try to get a link scope address, with
  81. * the assumption that we must be sitting behind a NAT.
  82. */
  83. if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
  84. list_for_each(pos, &src->address_list) {
  85. addr = list_entry(pos, struct sctp_sockaddr_entry,
  86. list);
  87. error = sctp_copy_one_addr(dest, &addr->a_h,
  88. SCTP_SCOPE_LINK, gfp,
  89. flags);
  90. if (error < 0)
  91. goto out;
  92. }
  93. }
  94. out:
  95. if (error)
  96. sctp_bind_addr_clean(dest);
  97. return error;
  98. }
  99. /* Initialize the SCTP_bind_addr structure for either an endpoint or
  100. * an association.
  101. */
  102. void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
  103. {
  104. bp->malloced = 0;
  105. INIT_LIST_HEAD(&bp->address_list);
  106. bp->port = port;
  107. }
  108. /* Dispose of the address list. */
  109. static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
  110. {
  111. struct sctp_sockaddr_entry *addr;
  112. struct list_head *pos, *temp;
  113. /* Empty the bind address list. */
  114. list_for_each_safe(pos, temp, &bp->address_list) {
  115. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  116. list_del(pos);
  117. kfree(addr);
  118. SCTP_DBG_OBJCNT_DEC(addr);
  119. }
  120. }
  121. /* Dispose of an SCTP_bind_addr structure */
  122. void sctp_bind_addr_free(struct sctp_bind_addr *bp)
  123. {
  124. /* Empty the bind address list. */
  125. sctp_bind_addr_clean(bp);
  126. if (bp->malloced) {
  127. kfree(bp);
  128. SCTP_DBG_OBJCNT_DEC(bind_addr);
  129. }
  130. }
  131. /* Add an address to the bind address list in the SCTP_bind_addr structure. */
  132. int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
  133. __u8 use_as_src, gfp_t gfp)
  134. {
  135. struct sctp_sockaddr_entry *addr;
  136. /* Add the address to the bind address list. */
  137. addr = t_new(struct sctp_sockaddr_entry, gfp);
  138. if (!addr)
  139. return -ENOMEM;
  140. memcpy(&addr->a, new, sizeof(*new));
  141. /* Fix up the port if it has not yet been set.
  142. * Both v4 and v6 have the port at the same offset.
  143. */
  144. if (!addr->a.v4.sin_port)
  145. addr->a.v4.sin_port = htons(bp->port);
  146. flip_to_h(&addr->a_h, &addr->a);
  147. addr->use_as_src = use_as_src;
  148. INIT_LIST_HEAD(&addr->list);
  149. list_add_tail(&addr->list, &bp->address_list);
  150. SCTP_DBG_OBJCNT_INC(addr);
  151. return 0;
  152. }
  153. /* Delete an address from the bind address list in the SCTP_bind_addr
  154. * structure.
  155. */
  156. int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
  157. {
  158. struct list_head *pos, *temp;
  159. struct sctp_sockaddr_entry *addr;
  160. list_for_each_safe(pos, temp, &bp->address_list) {
  161. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  162. if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
  163. /* Found the exact match. */
  164. list_del(pos);
  165. kfree(addr);
  166. SCTP_DBG_OBJCNT_DEC(addr);
  167. return 0;
  168. }
  169. }
  170. return -EINVAL;
  171. }
  172. /* Create a network byte-order representation of all the addresses
  173. * formated as SCTP parameters.
  174. *
  175. * The second argument is the return value for the length.
  176. */
  177. union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
  178. int *addrs_len,
  179. gfp_t gfp)
  180. {
  181. union sctp_params addrparms;
  182. union sctp_params retval;
  183. int addrparms_len;
  184. union sctp_addr_param rawaddr;
  185. int len;
  186. struct sctp_sockaddr_entry *addr;
  187. struct list_head *pos;
  188. struct sctp_af *af;
  189. addrparms_len = 0;
  190. len = 0;
  191. /* Allocate enough memory at once. */
  192. list_for_each(pos, &bp->address_list) {
  193. len += sizeof(union sctp_addr_param);
  194. }
  195. /* Don't even bother embedding an address if there
  196. * is only one.
  197. */
  198. if (len == sizeof(union sctp_addr_param)) {
  199. retval.v = NULL;
  200. goto end_raw;
  201. }
  202. retval.v = kmalloc(len, gfp);
  203. if (!retval.v)
  204. goto end_raw;
  205. addrparms = retval;
  206. list_for_each(pos, &bp->address_list) {
  207. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  208. af = sctp_get_af_specific(addr->a_h.v4.sin_family);
  209. len = af->to_addr_param(&addr->a_h, &rawaddr);
  210. memcpy(addrparms.v, &rawaddr, len);
  211. addrparms.v += len;
  212. addrparms_len += len;
  213. }
  214. end_raw:
  215. *addrs_len = addrparms_len;
  216. return retval;
  217. }
  218. /*
  219. * Create an address list out of the raw address list format (IPv4 and IPv6
  220. * address parameters).
  221. */
  222. int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
  223. int addrs_len, __u16 port, gfp_t gfp)
  224. {
  225. union sctp_addr_param *rawaddr;
  226. struct sctp_paramhdr *param;
  227. union sctp_addr addr;
  228. int retval = 0;
  229. int len;
  230. struct sctp_af *af;
  231. /* Convert the raw address to standard address format */
  232. while (addrs_len) {
  233. param = (struct sctp_paramhdr *)raw_addr_list;
  234. rawaddr = (union sctp_addr_param *)raw_addr_list;
  235. af = sctp_get_af_specific(param_type2af(param->type));
  236. if (unlikely(!af)) {
  237. retval = -EINVAL;
  238. sctp_bind_addr_clean(bp);
  239. break;
  240. }
  241. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  242. retval = sctp_add_bind_addr(bp, &addr, 1, gfp);
  243. if (retval) {
  244. /* Can't finish building the list, clean up. */
  245. sctp_bind_addr_clean(bp);
  246. break;
  247. }
  248. len = ntohs(param->length);
  249. addrs_len -= len;
  250. raw_addr_list += len;
  251. }
  252. return retval;
  253. }
  254. /********************************************************************
  255. * 2nd Level Abstractions
  256. ********************************************************************/
  257. /* Does this contain a specified address? Allow wildcarding. */
  258. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  259. const union sctp_addr *addr,
  260. struct sctp_sock *opt)
  261. {
  262. struct sctp_sockaddr_entry *laddr;
  263. struct list_head *pos;
  264. list_for_each(pos, &bp->address_list) {
  265. laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
  266. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. /* Find the first address in the bind address list that is not present in
  272. * the addrs packed array.
  273. */
  274. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  275. const union sctp_addr *addrs,
  276. int addrcnt,
  277. struct sctp_sock *opt)
  278. {
  279. struct sctp_sockaddr_entry *laddr;
  280. union sctp_addr *addr;
  281. void *addr_buf;
  282. struct sctp_af *af;
  283. struct list_head *pos;
  284. int i;
  285. list_for_each(pos, &bp->address_list) {
  286. laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
  287. addr_buf = (union sctp_addr *)addrs;
  288. for (i = 0; i < addrcnt; i++) {
  289. addr = (union sctp_addr *)addr_buf;
  290. af = sctp_get_af_specific(addr->v4.sin_family);
  291. if (!af)
  292. return NULL;
  293. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  294. break;
  295. addr_buf += af->sockaddr_len;
  296. }
  297. if (i == addrcnt)
  298. return &laddr->a_h;
  299. }
  300. return NULL;
  301. }
  302. /* Copy out addresses from the global local address list. */
  303. static int sctp_copy_one_addr(struct sctp_bind_addr *dest,
  304. union sctp_addr *addr,
  305. sctp_scope_t scope, gfp_t gfp,
  306. int flags)
  307. {
  308. int error = 0;
  309. union sctp_addr tmp;
  310. flip_to_n(&tmp, addr);
  311. if (sctp_is_any(addr)) {
  312. error = sctp_copy_local_addr_list(dest, scope, gfp, flags);
  313. } else if (sctp_in_scope(addr, scope)) {
  314. /* Now that the address is in scope, check to see if
  315. * the address type is supported by local sock as
  316. * well as the remote peer.
  317. */
  318. if ((((AF_INET == addr->sa.sa_family) &&
  319. (flags & SCTP_ADDR4_PEERSUPP))) ||
  320. (((AF_INET6 == addr->sa.sa_family) &&
  321. (flags & SCTP_ADDR6_ALLOWED) &&
  322. (flags & SCTP_ADDR6_PEERSUPP))))
  323. error = sctp_add_bind_addr(dest, &tmp, 1, gfp);
  324. }
  325. return error;
  326. }
  327. /* Is this a wildcard address? */
  328. int sctp_is_any(const union sctp_addr *addr)
  329. {
  330. struct sctp_af *af = sctp_get_af_specific(addr->sa.sa_family);
  331. if (!af)
  332. return 0;
  333. return af->is_any(addr);
  334. }
  335. /* Is 'addr' valid for 'scope'? */
  336. int sctp_in_scope(const union sctp_addr *addr, sctp_scope_t scope)
  337. {
  338. sctp_scope_t addr_scope = sctp_scope(addr);
  339. /* The unusable SCTP addresses will not be considered with
  340. * any defined scopes.
  341. */
  342. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  343. return 0;
  344. /*
  345. * For INIT and INIT-ACK address list, let L be the level of
  346. * of requested destination address, sender and receiver
  347. * SHOULD include all of its addresses with level greater
  348. * than or equal to L.
  349. */
  350. if (addr_scope <= scope)
  351. return 1;
  352. return 0;
  353. }
  354. /********************************************************************
  355. * 3rd Level Abstractions
  356. ********************************************************************/
  357. /* What is the scope of 'addr'? */
  358. sctp_scope_t sctp_scope(const union sctp_addr *addr)
  359. {
  360. struct sctp_af *af;
  361. af = sctp_get_af_specific(addr->sa.sa_family);
  362. if (!af)
  363. return SCTP_SCOPE_UNUSABLE;
  364. return af->scope((union sctp_addr *)addr);
  365. }