bind_addr.c 14 KB

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