bind_addr.c 14 KB

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