bind_addr.c 14 KB

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