bind_addr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* SCTP kernel 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 implementation.
  8. *
  9. * A collection class to handle the storage of transport addresses.
  10. *
  11. * This SCTP 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. * This SCTP 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. 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_entry(addr, &src->address_list, list) {
  71. error = sctp_copy_one_addr(dest, &addr->a, scope,
  72. gfp, flags);
  73. if (error < 0)
  74. goto out;
  75. }
  76. /* If there are no addresses matching the scope and
  77. * this is global scope, try to get a link scope address, with
  78. * the assumption that we must be sitting behind a NAT.
  79. */
  80. if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
  81. list_for_each_entry(addr, &src->address_list, list) {
  82. error = sctp_copy_one_addr(dest, &addr->a,
  83. SCTP_SCOPE_LINK, gfp,
  84. flags);
  85. if (error < 0)
  86. goto out;
  87. }
  88. }
  89. out:
  90. if (error)
  91. sctp_bind_addr_clean(dest);
  92. return error;
  93. }
  94. /* Exactly duplicate the address lists. This is necessary when doing
  95. * peer-offs and accepts. We don't want to put all the current system
  96. * addresses into the endpoint. That's useless. But we do want duplicat
  97. * the list of bound addresses that the older endpoint used.
  98. */
  99. int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
  100. const struct sctp_bind_addr *src,
  101. gfp_t gfp)
  102. {
  103. struct sctp_sockaddr_entry *addr;
  104. int error = 0;
  105. /* All addresses share the same port. */
  106. dest->port = src->port;
  107. list_for_each_entry(addr, &src->address_list, list) {
  108. error = sctp_add_bind_addr(dest, &addr->a, 1, gfp);
  109. if (error < 0)
  110. break;
  111. }
  112. return error;
  113. }
  114. /* Initialize the SCTP_bind_addr structure for either an endpoint or
  115. * an association.
  116. */
  117. void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
  118. {
  119. bp->malloced = 0;
  120. INIT_LIST_HEAD(&bp->address_list);
  121. bp->port = port;
  122. }
  123. /* Dispose of the address list. */
  124. static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
  125. {
  126. struct sctp_sockaddr_entry *addr;
  127. struct list_head *pos, *temp;
  128. /* Empty the bind address list. */
  129. list_for_each_safe(pos, temp, &bp->address_list) {
  130. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  131. list_del(pos);
  132. kfree(addr);
  133. SCTP_DBG_OBJCNT_DEC(addr);
  134. }
  135. }
  136. /* Dispose of an SCTP_bind_addr structure */
  137. void sctp_bind_addr_free(struct sctp_bind_addr *bp)
  138. {
  139. /* Empty the bind address list. */
  140. sctp_bind_addr_clean(bp);
  141. if (bp->malloced) {
  142. kfree(bp);
  143. SCTP_DBG_OBJCNT_DEC(bind_addr);
  144. }
  145. }
  146. /* Add an address to the bind address list in the SCTP_bind_addr structure. */
  147. int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
  148. __u8 addr_state, gfp_t gfp)
  149. {
  150. struct sctp_sockaddr_entry *addr;
  151. /* Add the address to the bind address list. */
  152. addr = t_new(struct sctp_sockaddr_entry, gfp);
  153. if (!addr)
  154. return -ENOMEM;
  155. memcpy(&addr->a, new, sizeof(*new));
  156. /* Fix up the port if it has not yet been set.
  157. * Both v4 and v6 have the port at the same offset.
  158. */
  159. if (!addr->a.v4.sin_port)
  160. addr->a.v4.sin_port = htons(bp->port);
  161. addr->state = addr_state;
  162. addr->valid = 1;
  163. INIT_LIST_HEAD(&addr->list);
  164. INIT_RCU_HEAD(&addr->rcu);
  165. /* We always hold a socket lock when calling this function,
  166. * and that acts as a writer synchronizing lock.
  167. */
  168. list_add_tail_rcu(&addr->list, &bp->address_list);
  169. SCTP_DBG_OBJCNT_INC(addr);
  170. return 0;
  171. }
  172. /* Delete an address from the bind address list in the SCTP_bind_addr
  173. * structure.
  174. */
  175. int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
  176. {
  177. struct sctp_sockaddr_entry *addr, *temp;
  178. int found = 0;
  179. /* We hold the socket lock when calling this function,
  180. * and that acts as a writer synchronizing lock.
  181. */
  182. list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
  183. if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
  184. /* Found the exact match. */
  185. found = 1;
  186. addr->valid = 0;
  187. list_del_rcu(&addr->list);
  188. break;
  189. }
  190. }
  191. if (found) {
  192. call_rcu(&addr->rcu, sctp_local_addr_free);
  193. SCTP_DBG_OBJCNT_DEC(addr);
  194. return 0;
  195. }
  196. return -EINVAL;
  197. }
  198. /* Create a network byte-order representation of all the addresses
  199. * formated as SCTP parameters.
  200. *
  201. * The second argument is the return value for the length.
  202. */
  203. union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
  204. int *addrs_len,
  205. gfp_t gfp)
  206. {
  207. union sctp_params addrparms;
  208. union sctp_params retval;
  209. int addrparms_len;
  210. union sctp_addr_param rawaddr;
  211. int len;
  212. struct sctp_sockaddr_entry *addr;
  213. struct list_head *pos;
  214. struct sctp_af *af;
  215. addrparms_len = 0;
  216. len = 0;
  217. /* Allocate enough memory at once. */
  218. list_for_each(pos, &bp->address_list) {
  219. len += sizeof(union sctp_addr_param);
  220. }
  221. /* Don't even bother embedding an address if there
  222. * is only one.
  223. */
  224. if (len == sizeof(union sctp_addr_param)) {
  225. retval.v = NULL;
  226. goto end_raw;
  227. }
  228. retval.v = kmalloc(len, gfp);
  229. if (!retval.v)
  230. goto end_raw;
  231. addrparms = retval;
  232. list_for_each_entry(addr, &bp->address_list, list) {
  233. af = sctp_get_af_specific(addr->a.v4.sin_family);
  234. len = af->to_addr_param(&addr->a, &rawaddr);
  235. memcpy(addrparms.v, &rawaddr, len);
  236. addrparms.v += len;
  237. addrparms_len += len;
  238. }
  239. end_raw:
  240. *addrs_len = addrparms_len;
  241. return retval;
  242. }
  243. /*
  244. * Create an address list out of the raw address list format (IPv4 and IPv6
  245. * address parameters).
  246. */
  247. int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
  248. int addrs_len, __u16 port, gfp_t gfp)
  249. {
  250. union sctp_addr_param *rawaddr;
  251. struct sctp_paramhdr *param;
  252. union sctp_addr addr;
  253. int retval = 0;
  254. int len;
  255. struct sctp_af *af;
  256. /* Convert the raw address to standard address format */
  257. while (addrs_len) {
  258. param = (struct sctp_paramhdr *)raw_addr_list;
  259. rawaddr = (union sctp_addr_param *)raw_addr_list;
  260. af = sctp_get_af_specific(param_type2af(param->type));
  261. if (unlikely(!af)) {
  262. retval = -EINVAL;
  263. sctp_bind_addr_clean(bp);
  264. break;
  265. }
  266. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  267. retval = sctp_add_bind_addr(bp, &addr, SCTP_ADDR_SRC, gfp);
  268. if (retval) {
  269. /* Can't finish building the list, clean up. */
  270. sctp_bind_addr_clean(bp);
  271. break;
  272. }
  273. len = ntohs(param->length);
  274. addrs_len -= len;
  275. raw_addr_list += len;
  276. }
  277. return retval;
  278. }
  279. /********************************************************************
  280. * 2nd Level Abstractions
  281. ********************************************************************/
  282. /* Does this contain a specified address? Allow wildcarding. */
  283. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  284. const union sctp_addr *addr,
  285. struct sctp_sock *opt)
  286. {
  287. struct sctp_sockaddr_entry *laddr;
  288. int match = 0;
  289. rcu_read_lock();
  290. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  291. if (!laddr->valid)
  292. continue;
  293. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  294. match = 1;
  295. break;
  296. }
  297. }
  298. rcu_read_unlock();
  299. return match;
  300. }
  301. /* Does the address 'addr' conflict with any addresses in
  302. * the bp.
  303. */
  304. int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
  305. const union sctp_addr *addr,
  306. struct sctp_sock *bp_sp,
  307. struct sctp_sock *addr_sp)
  308. {
  309. struct sctp_sockaddr_entry *laddr;
  310. int conflict = 0;
  311. struct sctp_sock *sp;
  312. /* Pick the IPv6 socket as the basis of comparison
  313. * since it's usually a superset of the IPv4.
  314. * If there is no IPv6 socket, then default to bind_addr.
  315. */
  316. if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
  317. sp = bp_sp;
  318. else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
  319. sp = addr_sp;
  320. else
  321. sp = bp_sp;
  322. rcu_read_lock();
  323. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  324. if (!laddr->valid)
  325. continue;
  326. conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
  327. if (conflict)
  328. break;
  329. }
  330. rcu_read_unlock();
  331. return conflict;
  332. }
  333. /* Get the state of the entry in the bind_addr_list */
  334. int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
  335. const union sctp_addr *addr)
  336. {
  337. struct sctp_sockaddr_entry *laddr;
  338. struct sctp_af *af;
  339. int state = -1;
  340. af = sctp_get_af_specific(addr->sa.sa_family);
  341. if (unlikely(!af))
  342. return state;
  343. rcu_read_lock();
  344. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  345. if (!laddr->valid)
  346. continue;
  347. if (af->cmp_addr(&laddr->a, addr)) {
  348. state = laddr->state;
  349. break;
  350. }
  351. }
  352. rcu_read_unlock();
  353. return state;
  354. }
  355. /* Find the first address in the bind address list that is not present in
  356. * the addrs packed array.
  357. */
  358. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  359. const union sctp_addr *addrs,
  360. int addrcnt,
  361. struct sctp_sock *opt)
  362. {
  363. struct sctp_sockaddr_entry *laddr;
  364. union sctp_addr *addr;
  365. void *addr_buf;
  366. struct sctp_af *af;
  367. int i;
  368. /* This is only called sctp_send_asconf_del_ip() and we hold
  369. * the socket lock in that code patch, so that address list
  370. * can't change.
  371. */
  372. list_for_each_entry(laddr, &bp->address_list, list) {
  373. addr_buf = (union sctp_addr *)addrs;
  374. for (i = 0; i < addrcnt; i++) {
  375. addr = (union sctp_addr *)addr_buf;
  376. af = sctp_get_af_specific(addr->v4.sin_family);
  377. if (!af)
  378. break;
  379. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  380. break;
  381. addr_buf += af->sockaddr_len;
  382. }
  383. if (i == addrcnt)
  384. return &laddr->a;
  385. }
  386. return NULL;
  387. }
  388. /* Copy out addresses from the global local address list. */
  389. static int sctp_copy_one_addr(struct sctp_bind_addr *dest,
  390. union sctp_addr *addr,
  391. sctp_scope_t scope, gfp_t gfp,
  392. int flags)
  393. {
  394. int error = 0;
  395. if (sctp_is_any(NULL, addr)) {
  396. error = sctp_copy_local_addr_list(dest, scope, gfp, flags);
  397. } else if (sctp_in_scope(addr, scope)) {
  398. /* Now that the address is in scope, check to see if
  399. * the address type is supported by local sock as
  400. * well as the remote peer.
  401. */
  402. if ((((AF_INET == addr->sa.sa_family) &&
  403. (flags & SCTP_ADDR4_PEERSUPP))) ||
  404. (((AF_INET6 == addr->sa.sa_family) &&
  405. (flags & SCTP_ADDR6_ALLOWED) &&
  406. (flags & SCTP_ADDR6_PEERSUPP))))
  407. error = sctp_add_bind_addr(dest, addr, SCTP_ADDR_SRC,
  408. gfp);
  409. }
  410. return error;
  411. }
  412. /* Is this a wildcard address? */
  413. int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
  414. {
  415. unsigned short fam = 0;
  416. struct sctp_af *af;
  417. /* Try to get the right address family */
  418. if (addr->sa.sa_family != AF_UNSPEC)
  419. fam = addr->sa.sa_family;
  420. else if (sk)
  421. fam = sk->sk_family;
  422. af = sctp_get_af_specific(fam);
  423. if (!af)
  424. return 0;
  425. return af->is_any(addr);
  426. }
  427. /* Is 'addr' valid for 'scope'? */
  428. int sctp_in_scope(const union sctp_addr *addr, sctp_scope_t scope)
  429. {
  430. sctp_scope_t addr_scope = sctp_scope(addr);
  431. /* The unusable SCTP addresses will not be considered with
  432. * any defined scopes.
  433. */
  434. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  435. return 0;
  436. /*
  437. * For INIT and INIT-ACK address list, let L be the level of
  438. * of requested destination address, sender and receiver
  439. * SHOULD include all of its addresses with level greater
  440. * than or equal to L.
  441. */
  442. if (addr_scope <= scope)
  443. return 1;
  444. return 0;
  445. }
  446. /********************************************************************
  447. * 3rd Level Abstractions
  448. ********************************************************************/
  449. /* What is the scope of 'addr'? */
  450. sctp_scope_t sctp_scope(const union sctp_addr *addr)
  451. {
  452. struct sctp_af *af;
  453. af = sctp_get_af_specific(addr->sa.sa_family);
  454. if (!af)
  455. return SCTP_SCOPE_UNUSABLE;
  456. return af->scope((union sctp_addr *)addr);
  457. }