bind_addr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. retval = -EINVAL;
  250. sctp_bind_addr_clean(bp);
  251. break;
  252. }
  253. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  254. if (sctp_bind_addr_state(bp, &addr) != -1)
  255. goto next;
  256. retval = sctp_add_bind_addr(bp, &addr, sizeof(addr),
  257. SCTP_ADDR_SRC, gfp);
  258. if (retval) {
  259. /* Can't finish building the list, clean up. */
  260. sctp_bind_addr_clean(bp);
  261. break;
  262. }
  263. next:
  264. len = ntohs(param->length);
  265. addrs_len -= len;
  266. raw_addr_list += len;
  267. }
  268. return retval;
  269. }
  270. /********************************************************************
  271. * 2nd Level Abstractions
  272. ********************************************************************/
  273. /* Does this contain a specified address? Allow wildcarding. */
  274. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  275. const union sctp_addr *addr,
  276. struct sctp_sock *opt)
  277. {
  278. struct sctp_sockaddr_entry *laddr;
  279. int match = 0;
  280. rcu_read_lock();
  281. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  282. if (!laddr->valid)
  283. continue;
  284. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  285. match = 1;
  286. break;
  287. }
  288. }
  289. rcu_read_unlock();
  290. return match;
  291. }
  292. /* Does the address 'addr' conflict with any addresses in
  293. * the bp.
  294. */
  295. int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
  296. const union sctp_addr *addr,
  297. struct sctp_sock *bp_sp,
  298. struct sctp_sock *addr_sp)
  299. {
  300. struct sctp_sockaddr_entry *laddr;
  301. int conflict = 0;
  302. struct sctp_sock *sp;
  303. /* Pick the IPv6 socket as the basis of comparison
  304. * since it's usually a superset of the IPv4.
  305. * If there is no IPv6 socket, then default to bind_addr.
  306. */
  307. if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
  308. sp = bp_sp;
  309. else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
  310. sp = addr_sp;
  311. else
  312. sp = bp_sp;
  313. rcu_read_lock();
  314. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  315. if (!laddr->valid)
  316. continue;
  317. conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
  318. if (conflict)
  319. break;
  320. }
  321. rcu_read_unlock();
  322. return conflict;
  323. }
  324. /* Get the state of the entry in the bind_addr_list */
  325. int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
  326. const union sctp_addr *addr)
  327. {
  328. struct sctp_sockaddr_entry *laddr;
  329. struct sctp_af *af;
  330. int state = -1;
  331. af = sctp_get_af_specific(addr->sa.sa_family);
  332. if (unlikely(!af))
  333. return state;
  334. rcu_read_lock();
  335. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  336. if (!laddr->valid)
  337. continue;
  338. if (af->cmp_addr(&laddr->a, addr)) {
  339. state = laddr->state;
  340. break;
  341. }
  342. }
  343. rcu_read_unlock();
  344. return state;
  345. }
  346. /* Find the first address in the bind address list that is not present in
  347. * the addrs packed array.
  348. */
  349. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  350. const union sctp_addr *addrs,
  351. int addrcnt,
  352. struct sctp_sock *opt)
  353. {
  354. struct sctp_sockaddr_entry *laddr;
  355. union sctp_addr *addr;
  356. void *addr_buf;
  357. struct sctp_af *af;
  358. int i;
  359. /* This is only called sctp_send_asconf_del_ip() and we hold
  360. * the socket lock in that code patch, so that address list
  361. * can't change.
  362. */
  363. list_for_each_entry(laddr, &bp->address_list, list) {
  364. addr_buf = (union sctp_addr *)addrs;
  365. for (i = 0; i < addrcnt; i++) {
  366. addr = addr_buf;
  367. af = sctp_get_af_specific(addr->v4.sin_family);
  368. if (!af)
  369. break;
  370. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  371. break;
  372. addr_buf += af->sockaddr_len;
  373. }
  374. if (i == addrcnt)
  375. return &laddr->a;
  376. }
  377. return NULL;
  378. }
  379. /* Copy out addresses from the global local address list. */
  380. static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
  381. union sctp_addr *addr, enum sctp_scope scope,
  382. gfp_t gfp, int flags)
  383. {
  384. int error = 0;
  385. if (sctp_is_any(NULL, addr)) {
  386. error = sctp_copy_local_addr_list(net, dest, scope, gfp, flags);
  387. } else if (sctp_in_scope(net, addr, scope)) {
  388. /* Now that the address is in scope, check to see if
  389. * the address type is supported by local sock as
  390. * well as the remote peer.
  391. */
  392. if ((((AF_INET == addr->sa.sa_family) &&
  393. (flags & SCTP_ADDR4_PEERSUPP))) ||
  394. (((AF_INET6 == addr->sa.sa_family) &&
  395. (flags & SCTP_ADDR6_ALLOWED) &&
  396. (flags & SCTP_ADDR6_PEERSUPP))))
  397. error = sctp_add_bind_addr(dest, addr, sizeof(*addr),
  398. SCTP_ADDR_SRC, gfp);
  399. }
  400. return error;
  401. }
  402. /* Is this a wildcard address? */
  403. int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
  404. {
  405. unsigned short fam = 0;
  406. struct sctp_af *af;
  407. /* Try to get the right address family */
  408. if (addr->sa.sa_family != AF_UNSPEC)
  409. fam = addr->sa.sa_family;
  410. else if (sk)
  411. fam = sk->sk_family;
  412. af = sctp_get_af_specific(fam);
  413. if (!af)
  414. return 0;
  415. return af->is_any(addr);
  416. }
  417. /* Is 'addr' valid for 'scope'? */
  418. int sctp_in_scope(struct net *net, const union sctp_addr *addr,
  419. enum sctp_scope scope)
  420. {
  421. enum sctp_scope addr_scope = sctp_scope(addr);
  422. /* The unusable SCTP addresses will not be considered with
  423. * any defined scopes.
  424. */
  425. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  426. return 0;
  427. /*
  428. * For INIT and INIT-ACK address list, let L be the level of
  429. * of requested destination address, sender and receiver
  430. * SHOULD include all of its addresses with level greater
  431. * than or equal to L.
  432. *
  433. * Address scoping can be selectively controlled via sysctl
  434. * option
  435. */
  436. switch (net->sctp.scope_policy) {
  437. case SCTP_SCOPE_POLICY_DISABLE:
  438. return 1;
  439. case SCTP_SCOPE_POLICY_ENABLE:
  440. if (addr_scope <= scope)
  441. return 1;
  442. break;
  443. case SCTP_SCOPE_POLICY_PRIVATE:
  444. if (addr_scope <= scope || SCTP_SCOPE_PRIVATE == addr_scope)
  445. return 1;
  446. break;
  447. case SCTP_SCOPE_POLICY_LINK:
  448. if (addr_scope <= scope || SCTP_SCOPE_LINK == addr_scope)
  449. return 1;
  450. break;
  451. default:
  452. break;
  453. }
  454. return 0;
  455. }
  456. int sctp_is_ep_boundall(struct sock *sk)
  457. {
  458. struct sctp_bind_addr *bp;
  459. struct sctp_sockaddr_entry *addr;
  460. bp = &sctp_sk(sk)->ep->base.bind_addr;
  461. if (sctp_list_single_entry(&bp->address_list)) {
  462. addr = list_entry(bp->address_list.next,
  463. struct sctp_sockaddr_entry, list);
  464. if (sctp_is_any(sk, &addr->a))
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. /********************************************************************
  470. * 3rd Level Abstractions
  471. ********************************************************************/
  472. /* What is the scope of 'addr'? */
  473. enum sctp_scope sctp_scope(const union sctp_addr *addr)
  474. {
  475. struct sctp_af *af;
  476. af = sctp_get_af_specific(addr->sa.sa_family);
  477. if (!af)
  478. return SCTP_SCOPE_UNUSABLE;
  479. return af->scope((union sctp_addr *)addr);
  480. }