bind_addr.c 14 KB

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