bind_addr.c 11 KB

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