bind_addr.c 11 KB

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