bind_addr.c 14 KB

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