bind_addr.c 14 KB

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