bind_addr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 *net, struct sctp_bind_addr *dest,
  47. union sctp_addr *addr, enum sctp_scope scope,
  48. gfp_t gfp, 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. enum sctp_scope 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, sizeof(addr->a),
  103. 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. int new_size, __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, min_t(size_t, sizeof(*new), new_size));
  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. !af->from_addr_param(&addr, rawaddr, htons(port), 0)) {
  250. retval = -EINVAL;
  251. goto out_err;
  252. }
  253. if (sctp_bind_addr_state(bp, &addr) != -1)
  254. goto next;
  255. retval = sctp_add_bind_addr(bp, &addr, sizeof(addr),
  256. SCTP_ADDR_SRC, gfp);
  257. if (retval)
  258. /* Can't finish building the list, clean up. */
  259. goto out_err;
  260. next:
  261. len = ntohs(param->length);
  262. addrs_len -= len;
  263. raw_addr_list += len;
  264. }
  265. return retval;
  266. out_err:
  267. if (retval)
  268. sctp_bind_addr_clean(bp);
  269. return retval;
  270. }
  271. /********************************************************************
  272. * 2nd Level Abstractions
  273. ********************************************************************/
  274. /* Does this contain a specified address? Allow wildcarding. */
  275. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  276. const union sctp_addr *addr,
  277. struct sctp_sock *opt)
  278. {
  279. struct sctp_sockaddr_entry *laddr;
  280. int match = 0;
  281. rcu_read_lock();
  282. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  283. if (!laddr->valid)
  284. continue;
  285. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  286. match = 1;
  287. break;
  288. }
  289. }
  290. rcu_read_unlock();
  291. return match;
  292. }
  293. /* Does the address 'addr' conflict with any addresses in
  294. * the bp.
  295. */
  296. int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
  297. const union sctp_addr *addr,
  298. struct sctp_sock *bp_sp,
  299. struct sctp_sock *addr_sp)
  300. {
  301. struct sctp_sockaddr_entry *laddr;
  302. int conflict = 0;
  303. struct sctp_sock *sp;
  304. /* Pick the IPv6 socket as the basis of comparison
  305. * since it's usually a superset of the IPv4.
  306. * If there is no IPv6 socket, then default to bind_addr.
  307. */
  308. if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
  309. sp = bp_sp;
  310. else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
  311. sp = addr_sp;
  312. else
  313. sp = bp_sp;
  314. rcu_read_lock();
  315. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  316. if (!laddr->valid)
  317. continue;
  318. conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
  319. if (conflict)
  320. break;
  321. }
  322. rcu_read_unlock();
  323. return conflict;
  324. }
  325. /* Get the state of the entry in the bind_addr_list */
  326. int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
  327. const union sctp_addr *addr)
  328. {
  329. struct sctp_sockaddr_entry *laddr;
  330. struct sctp_af *af;
  331. int state = -1;
  332. af = sctp_get_af_specific(addr->sa.sa_family);
  333. if (unlikely(!af))
  334. return state;
  335. rcu_read_lock();
  336. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  337. if (!laddr->valid)
  338. continue;
  339. if (af->cmp_addr(&laddr->a, addr)) {
  340. state = laddr->state;
  341. break;
  342. }
  343. }
  344. rcu_read_unlock();
  345. return state;
  346. }
  347. /* Find the first address in the bind address list that is not present in
  348. * the addrs packed array.
  349. */
  350. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  351. const union sctp_addr *addrs,
  352. int addrcnt,
  353. struct sctp_sock *opt)
  354. {
  355. struct sctp_sockaddr_entry *laddr;
  356. union sctp_addr *addr;
  357. void *addr_buf;
  358. struct sctp_af *af;
  359. int i;
  360. /* This is only called sctp_send_asconf_del_ip() and we hold
  361. * the socket lock in that code patch, so that address list
  362. * can't change.
  363. */
  364. list_for_each_entry(laddr, &bp->address_list, list) {
  365. addr_buf = (union sctp_addr *)addrs;
  366. for (i = 0; i < addrcnt; i++) {
  367. addr = addr_buf;
  368. af = sctp_get_af_specific(addr->v4.sin_family);
  369. if (!af)
  370. break;
  371. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  372. break;
  373. addr_buf += af->sockaddr_len;
  374. }
  375. if (i == addrcnt)
  376. return &laddr->a;
  377. }
  378. return NULL;
  379. }
  380. /* Copy out addresses from the global local address list. */
  381. static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
  382. union sctp_addr *addr, enum sctp_scope scope,
  383. gfp_t gfp, int flags)
  384. {
  385. int error = 0;
  386. if (sctp_is_any(NULL, addr)) {
  387. error = sctp_copy_local_addr_list(net, dest, scope, gfp, flags);
  388. } else if (sctp_in_scope(net, addr, scope)) {
  389. /* Now that the address is in scope, check to see if
  390. * the address type is supported by local sock as
  391. * well as the remote peer.
  392. */
  393. if ((((AF_INET == addr->sa.sa_family) &&
  394. (flags & SCTP_ADDR4_ALLOWED) &&
  395. (flags & SCTP_ADDR4_PEERSUPP))) ||
  396. (((AF_INET6 == addr->sa.sa_family) &&
  397. (flags & SCTP_ADDR6_ALLOWED) &&
  398. (flags & SCTP_ADDR6_PEERSUPP))))
  399. error = sctp_add_bind_addr(dest, addr, sizeof(*addr),
  400. SCTP_ADDR_SRC, gfp);
  401. }
  402. return error;
  403. }
  404. /* Is this a wildcard address? */
  405. int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
  406. {
  407. unsigned short fam = 0;
  408. struct sctp_af *af;
  409. /* Try to get the right address family */
  410. if (addr->sa.sa_family != AF_UNSPEC)
  411. fam = addr->sa.sa_family;
  412. else if (sk)
  413. fam = sk->sk_family;
  414. af = sctp_get_af_specific(fam);
  415. if (!af)
  416. return 0;
  417. return af->is_any(addr);
  418. }
  419. /* Is 'addr' valid for 'scope'? */
  420. int sctp_in_scope(struct net *net, const union sctp_addr *addr,
  421. enum sctp_scope scope)
  422. {
  423. enum sctp_scope 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. enum sctp_scope 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. }