bind_addr.c 14 KB

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