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