/net/sctp/socket.c

http://github.com/mirrors/linux · C · 9687 lines · 6253 code · 1457 blank · 1977 comment · 1474 complexity · 32df4213cbcfd876d7520045e797b19b MD5 · raw file

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001-2003 Intel Corp.
  7. * Copyright (c) 2001-2002 Nokia, Inc.
  8. * Copyright (c) 2001 La Monte H.P. Yarroll
  9. *
  10. * This file is part of the SCTP kernel implementation
  11. *
  12. * These functions interface with the sockets layer to implement the
  13. * SCTP Extensions for the Sockets API.
  14. *
  15. * Note that the descriptions from the specification are USER level
  16. * functions--this file is the functions which populate the struct proto
  17. * for SCTP which is the BOTTOM of the sockets interface.
  18. *
  19. * Please send any bug reports or fixes you make to the
  20. * email address(es):
  21. * lksctp developers <linux-sctp@vger.kernel.org>
  22. *
  23. * Written or modified by:
  24. * La Monte H.P. Yarroll <piggy@acm.org>
  25. * Narasimha Budihal <narsi@refcode.org>
  26. * Karl Knutson <karl@athena.chicago.il.us>
  27. * Jon Grimm <jgrimm@us.ibm.com>
  28. * Xingang Guo <xingang.guo@intel.com>
  29. * Daisy Chang <daisyc@us.ibm.com>
  30. * Sridhar Samudrala <samudrala@us.ibm.com>
  31. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  32. * Ardelle Fan <ardelle.fan@intel.com>
  33. * Ryan Layer <rmlayer@us.ibm.com>
  34. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  35. * Kevin Gao <kevin.gao@intel.com>
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <crypto/hash.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/wait.h>
  42. #include <linux/time.h>
  43. #include <linux/sched/signal.h>
  44. #include <linux/ip.h>
  45. #include <linux/capability.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/poll.h>
  48. #include <linux/init.h>
  49. #include <linux/slab.h>
  50. #include <linux/file.h>
  51. #include <linux/compat.h>
  52. #include <linux/rhashtable.h>
  53. #include <net/ip.h>
  54. #include <net/icmp.h>
  55. #include <net/route.h>
  56. #include <net/ipv6.h>
  57. #include <net/inet_common.h>
  58. #include <net/busy_poll.h>
  59. #include <linux/socket.h> /* for sa_family_t */
  60. #include <linux/export.h>
  61. #include <net/sock.h>
  62. #include <net/sctp/sctp.h>
  63. #include <net/sctp/sm.h>
  64. #include <net/sctp/stream_sched.h>
  65. /* Forward declarations for internal helper functions. */
  66. static bool sctp_writeable(struct sock *sk);
  67. static void sctp_wfree(struct sk_buff *skb);
  68. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  69. size_t msg_len);
  70. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
  71. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  72. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  73. static void sctp_wait_for_close(struct sock *sk, long timeo);
  74. static void sctp_destruct_sock(struct sock *sk);
  75. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  76. union sctp_addr *addr, int len);
  77. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  78. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  79. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  80. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  81. static int sctp_send_asconf(struct sctp_association *asoc,
  82. struct sctp_chunk *chunk);
  83. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  84. static int sctp_autobind(struct sock *sk);
  85. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  86. struct sctp_association *assoc,
  87. enum sctp_socket_type type);
  88. static unsigned long sctp_memory_pressure;
  89. static atomic_long_t sctp_memory_allocated;
  90. struct percpu_counter sctp_sockets_allocated;
  91. static void sctp_enter_memory_pressure(struct sock *sk)
  92. {
  93. sctp_memory_pressure = 1;
  94. }
  95. /* Get the sndbuf space available at the time on the association. */
  96. static inline int sctp_wspace(struct sctp_association *asoc)
  97. {
  98. struct sock *sk = asoc->base.sk;
  99. return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
  100. : sk_stream_wspace(sk);
  101. }
  102. /* Increment the used sndbuf space count of the corresponding association by
  103. * the size of the outgoing data chunk.
  104. * Also, set the skb destructor for sndbuf accounting later.
  105. *
  106. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  107. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  108. * destructor in the data chunk skb for the purpose of the sndbuf space
  109. * tracking.
  110. */
  111. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  112. {
  113. struct sctp_association *asoc = chunk->asoc;
  114. struct sock *sk = asoc->base.sk;
  115. /* The sndbuf space is tracked per association. */
  116. sctp_association_hold(asoc);
  117. if (chunk->shkey)
  118. sctp_auth_shkey_hold(chunk->shkey);
  119. skb_set_owner_w(chunk->skb, sk);
  120. chunk->skb->destructor = sctp_wfree;
  121. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  122. skb_shinfo(chunk->skb)->destructor_arg = chunk;
  123. refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  124. asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
  125. sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
  126. sk_mem_charge(sk, chunk->skb->truesize);
  127. }
  128. static void sctp_clear_owner_w(struct sctp_chunk *chunk)
  129. {
  130. skb_orphan(chunk->skb);
  131. }
  132. #define traverse_and_process() \
  133. do { \
  134. msg = chunk->msg; \
  135. if (msg == prev_msg) \
  136. continue; \
  137. list_for_each_entry(c, &msg->chunks, frag_list) { \
  138. if ((clear && asoc->base.sk == c->skb->sk) || \
  139. (!clear && asoc->base.sk != c->skb->sk)) \
  140. cb(c); \
  141. } \
  142. prev_msg = msg; \
  143. } while (0)
  144. static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
  145. bool clear,
  146. void (*cb)(struct sctp_chunk *))
  147. {
  148. struct sctp_datamsg *msg, *prev_msg = NULL;
  149. struct sctp_outq *q = &asoc->outqueue;
  150. struct sctp_chunk *chunk, *c;
  151. struct sctp_transport *t;
  152. list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
  153. list_for_each_entry(chunk, &t->transmitted, transmitted_list)
  154. traverse_and_process();
  155. list_for_each_entry(chunk, &q->retransmit, transmitted_list)
  156. traverse_and_process();
  157. list_for_each_entry(chunk, &q->sacked, transmitted_list)
  158. traverse_and_process();
  159. list_for_each_entry(chunk, &q->abandoned, transmitted_list)
  160. traverse_and_process();
  161. list_for_each_entry(chunk, &q->out_chunk_list, list)
  162. traverse_and_process();
  163. }
  164. static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
  165. void (*cb)(struct sk_buff *, struct sock *))
  166. {
  167. struct sk_buff *skb, *tmp;
  168. sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
  169. cb(skb, sk);
  170. sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
  171. cb(skb, sk);
  172. sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
  173. cb(skb, sk);
  174. }
  175. /* Verify that this is a valid address. */
  176. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  177. int len)
  178. {
  179. struct sctp_af *af;
  180. /* Verify basic sockaddr. */
  181. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  182. if (!af)
  183. return -EINVAL;
  184. /* Is this a valid SCTP address? */
  185. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  186. return -EINVAL;
  187. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  188. return -EINVAL;
  189. return 0;
  190. }
  191. /* Look up the association by its id. If this is not a UDP-style
  192. * socket, the ID field is always ignored.
  193. */
  194. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  195. {
  196. struct sctp_association *asoc = NULL;
  197. /* If this is not a UDP-style socket, assoc id should be ignored. */
  198. if (!sctp_style(sk, UDP)) {
  199. /* Return NULL if the socket state is not ESTABLISHED. It
  200. * could be a TCP-style listening socket or a socket which
  201. * hasn't yet called connect() to establish an association.
  202. */
  203. if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
  204. return NULL;
  205. /* Get the first and the only association from the list. */
  206. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  207. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  208. struct sctp_association, asocs);
  209. return asoc;
  210. }
  211. /* Otherwise this is a UDP-style socket. */
  212. if (id <= SCTP_ALL_ASSOC)
  213. return NULL;
  214. spin_lock_bh(&sctp_assocs_id_lock);
  215. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  216. if (asoc && (asoc->base.sk != sk || asoc->base.dead))
  217. asoc = NULL;
  218. spin_unlock_bh(&sctp_assocs_id_lock);
  219. return asoc;
  220. }
  221. /* Look up the transport from an address and an assoc id. If both address and
  222. * id are specified, the associations matching the address and the id should be
  223. * the same.
  224. */
  225. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  226. struct sockaddr_storage *addr,
  227. sctp_assoc_t id)
  228. {
  229. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  230. struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
  231. union sctp_addr *laddr = (union sctp_addr *)addr;
  232. struct sctp_transport *transport;
  233. if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
  234. return NULL;
  235. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  236. laddr,
  237. &transport);
  238. if (!addr_asoc)
  239. return NULL;
  240. id_asoc = sctp_id2assoc(sk, id);
  241. if (id_asoc && (id_asoc != addr_asoc))
  242. return NULL;
  243. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  244. (union sctp_addr *)addr);
  245. return transport;
  246. }
  247. /* API 3.1.2 bind() - UDP Style Syntax
  248. * The syntax of bind() is,
  249. *
  250. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  251. *
  252. * sd - the socket descriptor returned by socket().
  253. * addr - the address structure (struct sockaddr_in or struct
  254. * sockaddr_in6 [RFC 2553]),
  255. * addr_len - the size of the address structure.
  256. */
  257. static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  258. {
  259. int retval = 0;
  260. lock_sock(sk);
  261. pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
  262. addr, addr_len);
  263. /* Disallow binding twice. */
  264. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  265. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  266. addr_len);
  267. else
  268. retval = -EINVAL;
  269. release_sock(sk);
  270. return retval;
  271. }
  272. static int sctp_get_port_local(struct sock *, union sctp_addr *);
  273. /* Verify this is a valid sockaddr. */
  274. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  275. union sctp_addr *addr, int len)
  276. {
  277. struct sctp_af *af;
  278. /* Check minimum size. */
  279. if (len < sizeof (struct sockaddr))
  280. return NULL;
  281. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  282. return NULL;
  283. if (addr->sa.sa_family == AF_INET6) {
  284. if (len < SIN6_LEN_RFC2133)
  285. return NULL;
  286. /* V4 mapped address are really of AF_INET family */
  287. if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
  288. !opt->pf->af_supported(AF_INET, opt))
  289. return NULL;
  290. }
  291. /* If we get this far, af is valid. */
  292. af = sctp_get_af_specific(addr->sa.sa_family);
  293. if (len < af->sockaddr_len)
  294. return NULL;
  295. return af;
  296. }
  297. /* Bind a local address either to an endpoint or to an association. */
  298. static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  299. {
  300. struct net *net = sock_net(sk);
  301. struct sctp_sock *sp = sctp_sk(sk);
  302. struct sctp_endpoint *ep = sp->ep;
  303. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  304. struct sctp_af *af;
  305. unsigned short snum;
  306. int ret = 0;
  307. /* Common sockaddr verification. */
  308. af = sctp_sockaddr_af(sp, addr, len);
  309. if (!af) {
  310. pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
  311. __func__, sk, addr, len);
  312. return -EINVAL;
  313. }
  314. snum = ntohs(addr->v4.sin_port);
  315. pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
  316. __func__, sk, &addr->sa, bp->port, snum, len);
  317. /* PF specific bind() address verification. */
  318. if (!sp->pf->bind_verify(sp, addr))
  319. return -EADDRNOTAVAIL;
  320. /* We must either be unbound, or bind to the same port.
  321. * It's OK to allow 0 ports if we are already bound.
  322. * We'll just inhert an already bound port in this case
  323. */
  324. if (bp->port) {
  325. if (!snum)
  326. snum = bp->port;
  327. else if (snum != bp->port) {
  328. pr_debug("%s: new port %d doesn't match existing port "
  329. "%d\n", __func__, snum, bp->port);
  330. return -EINVAL;
  331. }
  332. }
  333. if (snum && inet_port_requires_bind_service(net, snum) &&
  334. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  335. return -EACCES;
  336. /* See if the address matches any of the addresses we may have
  337. * already bound before checking against other endpoints.
  338. */
  339. if (sctp_bind_addr_match(bp, addr, sp))
  340. return -EINVAL;
  341. /* Make sure we are allowed to bind here.
  342. * The function sctp_get_port_local() does duplicate address
  343. * detection.
  344. */
  345. addr->v4.sin_port = htons(snum);
  346. if (sctp_get_port_local(sk, addr))
  347. return -EADDRINUSE;
  348. /* Refresh ephemeral port. */
  349. if (!bp->port)
  350. bp->port = inet_sk(sk)->inet_num;
  351. /* Add the address to the bind address list.
  352. * Use GFP_ATOMIC since BHs will be disabled.
  353. */
  354. ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
  355. SCTP_ADDR_SRC, GFP_ATOMIC);
  356. if (ret) {
  357. sctp_put_port(sk);
  358. return ret;
  359. }
  360. /* Copy back into socket for getsockname() use. */
  361. inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
  362. sp->pf->to_sk_saddr(addr, sk);
  363. return ret;
  364. }
  365. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  366. *
  367. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  368. * at any one time. If a sender, after sending an ASCONF chunk, decides
  369. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  370. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  371. * subsequent ASCONF. Note this restriction binds each side, so at any
  372. * time two ASCONF may be in-transit on any given association (one sent
  373. * from each endpoint).
  374. */
  375. static int sctp_send_asconf(struct sctp_association *asoc,
  376. struct sctp_chunk *chunk)
  377. {
  378. int retval = 0;
  379. /* If there is an outstanding ASCONF chunk, queue it for later
  380. * transmission.
  381. */
  382. if (asoc->addip_last_asconf) {
  383. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  384. goto out;
  385. }
  386. /* Hold the chunk until an ASCONF_ACK is received. */
  387. sctp_chunk_hold(chunk);
  388. retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
  389. if (retval)
  390. sctp_chunk_free(chunk);
  391. else
  392. asoc->addip_last_asconf = chunk;
  393. out:
  394. return retval;
  395. }
  396. /* Add a list of addresses as bind addresses to local endpoint or
  397. * association.
  398. *
  399. * Basically run through each address specified in the addrs/addrcnt
  400. * array/length pair, determine if it is IPv6 or IPv4 and call
  401. * sctp_do_bind() on it.
  402. *
  403. * If any of them fails, then the operation will be reversed and the
  404. * ones that were added will be removed.
  405. *
  406. * Only sctp_setsockopt_bindx() is supposed to call this function.
  407. */
  408. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  409. {
  410. int cnt;
  411. int retval = 0;
  412. void *addr_buf;
  413. struct sockaddr *sa_addr;
  414. struct sctp_af *af;
  415. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
  416. addrs, addrcnt);
  417. addr_buf = addrs;
  418. for (cnt = 0; cnt < addrcnt; cnt++) {
  419. /* The list may contain either IPv4 or IPv6 address;
  420. * determine the address length for walking thru the list.
  421. */
  422. sa_addr = addr_buf;
  423. af = sctp_get_af_specific(sa_addr->sa_family);
  424. if (!af) {
  425. retval = -EINVAL;
  426. goto err_bindx_add;
  427. }
  428. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  429. af->sockaddr_len);
  430. addr_buf += af->sockaddr_len;
  431. err_bindx_add:
  432. if (retval < 0) {
  433. /* Failed. Cleanup the ones that have been added */
  434. if (cnt > 0)
  435. sctp_bindx_rem(sk, addrs, cnt);
  436. return retval;
  437. }
  438. }
  439. return retval;
  440. }
  441. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  442. * associations that are part of the endpoint indicating that a list of local
  443. * addresses are added to the endpoint.
  444. *
  445. * If any of the addresses is already in the bind address list of the
  446. * association, we do not send the chunk for that association. But it will not
  447. * affect other associations.
  448. *
  449. * Only sctp_setsockopt_bindx() is supposed to call this function.
  450. */
  451. static int sctp_send_asconf_add_ip(struct sock *sk,
  452. struct sockaddr *addrs,
  453. int addrcnt)
  454. {
  455. struct sctp_sock *sp;
  456. struct sctp_endpoint *ep;
  457. struct sctp_association *asoc;
  458. struct sctp_bind_addr *bp;
  459. struct sctp_chunk *chunk;
  460. struct sctp_sockaddr_entry *laddr;
  461. union sctp_addr *addr;
  462. union sctp_addr saveaddr;
  463. void *addr_buf;
  464. struct sctp_af *af;
  465. struct list_head *p;
  466. int i;
  467. int retval = 0;
  468. sp = sctp_sk(sk);
  469. ep = sp->ep;
  470. if (!ep->asconf_enable)
  471. return retval;
  472. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  473. __func__, sk, addrs, addrcnt);
  474. list_for_each_entry(asoc, &ep->asocs, asocs) {
  475. if (!asoc->peer.asconf_capable)
  476. continue;
  477. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  478. continue;
  479. if (!sctp_state(asoc, ESTABLISHED))
  480. continue;
  481. /* Check if any address in the packed array of addresses is
  482. * in the bind address list of the association. If so,
  483. * do not send the asconf chunk to its peer, but continue with
  484. * other associations.
  485. */
  486. addr_buf = addrs;
  487. for (i = 0; i < addrcnt; i++) {
  488. addr = addr_buf;
  489. af = sctp_get_af_specific(addr->v4.sin_family);
  490. if (!af) {
  491. retval = -EINVAL;
  492. goto out;
  493. }
  494. if (sctp_assoc_lookup_laddr(asoc, addr))
  495. break;
  496. addr_buf += af->sockaddr_len;
  497. }
  498. if (i < addrcnt)
  499. continue;
  500. /* Use the first valid address in bind addr list of
  501. * association as Address Parameter of ASCONF CHUNK.
  502. */
  503. bp = &asoc->base.bind_addr;
  504. p = bp->address_list.next;
  505. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  506. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  507. addrcnt, SCTP_PARAM_ADD_IP);
  508. if (!chunk) {
  509. retval = -ENOMEM;
  510. goto out;
  511. }
  512. /* Add the new addresses to the bind address list with
  513. * use_as_src set to 0.
  514. */
  515. addr_buf = addrs;
  516. for (i = 0; i < addrcnt; i++) {
  517. addr = addr_buf;
  518. af = sctp_get_af_specific(addr->v4.sin_family);
  519. memcpy(&saveaddr, addr, af->sockaddr_len);
  520. retval = sctp_add_bind_addr(bp, &saveaddr,
  521. sizeof(saveaddr),
  522. SCTP_ADDR_NEW, GFP_ATOMIC);
  523. addr_buf += af->sockaddr_len;
  524. }
  525. if (asoc->src_out_of_asoc_ok) {
  526. struct sctp_transport *trans;
  527. list_for_each_entry(trans,
  528. &asoc->peer.transport_addr_list, transports) {
  529. trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
  530. 2*asoc->pathmtu, 4380));
  531. trans->ssthresh = asoc->peer.i.a_rwnd;
  532. trans->rto = asoc->rto_initial;
  533. sctp_max_rto(asoc, trans);
  534. trans->rtt = trans->srtt = trans->rttvar = 0;
  535. /* Clear the source and route cache */
  536. sctp_transport_route(trans, NULL,
  537. sctp_sk(asoc->base.sk));
  538. }
  539. }
  540. retval = sctp_send_asconf(asoc, chunk);
  541. }
  542. out:
  543. return retval;
  544. }
  545. /* Remove a list of addresses from bind addresses list. Do not remove the
  546. * last address.
  547. *
  548. * Basically run through each address specified in the addrs/addrcnt
  549. * array/length pair, determine if it is IPv6 or IPv4 and call
  550. * sctp_del_bind() on it.
  551. *
  552. * If any of them fails, then the operation will be reversed and the
  553. * ones that were removed will be added back.
  554. *
  555. * At least one address has to be left; if only one address is
  556. * available, the operation will return -EBUSY.
  557. *
  558. * Only sctp_setsockopt_bindx() is supposed to call this function.
  559. */
  560. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  561. {
  562. struct sctp_sock *sp = sctp_sk(sk);
  563. struct sctp_endpoint *ep = sp->ep;
  564. int cnt;
  565. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  566. int retval = 0;
  567. void *addr_buf;
  568. union sctp_addr *sa_addr;
  569. struct sctp_af *af;
  570. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  571. __func__, sk, addrs, addrcnt);
  572. addr_buf = addrs;
  573. for (cnt = 0; cnt < addrcnt; cnt++) {
  574. /* If the bind address list is empty or if there is only one
  575. * bind address, there is nothing more to be removed (we need
  576. * at least one address here).
  577. */
  578. if (list_empty(&bp->address_list) ||
  579. (sctp_list_single_entry(&bp->address_list))) {
  580. retval = -EBUSY;
  581. goto err_bindx_rem;
  582. }
  583. sa_addr = addr_buf;
  584. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  585. if (!af) {
  586. retval = -EINVAL;
  587. goto err_bindx_rem;
  588. }
  589. if (!af->addr_valid(sa_addr, sp, NULL)) {
  590. retval = -EADDRNOTAVAIL;
  591. goto err_bindx_rem;
  592. }
  593. if (sa_addr->v4.sin_port &&
  594. sa_addr->v4.sin_port != htons(bp->port)) {
  595. retval = -EINVAL;
  596. goto err_bindx_rem;
  597. }
  598. if (!sa_addr->v4.sin_port)
  599. sa_addr->v4.sin_port = htons(bp->port);
  600. /* FIXME - There is probably a need to check if sk->sk_saddr and
  601. * sk->sk_rcv_addr are currently set to one of the addresses to
  602. * be removed. This is something which needs to be looked into
  603. * when we are fixing the outstanding issues with multi-homing
  604. * socket routing and failover schemes. Refer to comments in
  605. * sctp_do_bind(). -daisy
  606. */
  607. retval = sctp_del_bind_addr(bp, sa_addr);
  608. addr_buf += af->sockaddr_len;
  609. err_bindx_rem:
  610. if (retval < 0) {
  611. /* Failed. Add the ones that has been removed back */
  612. if (cnt > 0)
  613. sctp_bindx_add(sk, addrs, cnt);
  614. return retval;
  615. }
  616. }
  617. return retval;
  618. }
  619. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  620. * the associations that are part of the endpoint indicating that a list of
  621. * local addresses are removed from the endpoint.
  622. *
  623. * If any of the addresses is already in the bind address list of the
  624. * association, we do not send the chunk for that association. But it will not
  625. * affect other associations.
  626. *
  627. * Only sctp_setsockopt_bindx() is supposed to call this function.
  628. */
  629. static int sctp_send_asconf_del_ip(struct sock *sk,
  630. struct sockaddr *addrs,
  631. int addrcnt)
  632. {
  633. struct sctp_sock *sp;
  634. struct sctp_endpoint *ep;
  635. struct sctp_association *asoc;
  636. struct sctp_transport *transport;
  637. struct sctp_bind_addr *bp;
  638. struct sctp_chunk *chunk;
  639. union sctp_addr *laddr;
  640. void *addr_buf;
  641. struct sctp_af *af;
  642. struct sctp_sockaddr_entry *saddr;
  643. int i;
  644. int retval = 0;
  645. int stored = 0;
  646. chunk = NULL;
  647. sp = sctp_sk(sk);
  648. ep = sp->ep;
  649. if (!ep->asconf_enable)
  650. return retval;
  651. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  652. __func__, sk, addrs, addrcnt);
  653. list_for_each_entry(asoc, &ep->asocs, asocs) {
  654. if (!asoc->peer.asconf_capable)
  655. continue;
  656. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  657. continue;
  658. if (!sctp_state(asoc, ESTABLISHED))
  659. continue;
  660. /* Check if any address in the packed array of addresses is
  661. * not present in the bind address list of the association.
  662. * If so, do not send the asconf chunk to its peer, but
  663. * continue with other associations.
  664. */
  665. addr_buf = addrs;
  666. for (i = 0; i < addrcnt; i++) {
  667. laddr = addr_buf;
  668. af = sctp_get_af_specific(laddr->v4.sin_family);
  669. if (!af) {
  670. retval = -EINVAL;
  671. goto out;
  672. }
  673. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  674. break;
  675. addr_buf += af->sockaddr_len;
  676. }
  677. if (i < addrcnt)
  678. continue;
  679. /* Find one address in the association's bind address list
  680. * that is not in the packed array of addresses. This is to
  681. * make sure that we do not delete all the addresses in the
  682. * association.
  683. */
  684. bp = &asoc->base.bind_addr;
  685. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  686. addrcnt, sp);
  687. if ((laddr == NULL) && (addrcnt == 1)) {
  688. if (asoc->asconf_addr_del_pending)
  689. continue;
  690. asoc->asconf_addr_del_pending =
  691. kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
  692. if (asoc->asconf_addr_del_pending == NULL) {
  693. retval = -ENOMEM;
  694. goto out;
  695. }
  696. asoc->asconf_addr_del_pending->sa.sa_family =
  697. addrs->sa_family;
  698. asoc->asconf_addr_del_pending->v4.sin_port =
  699. htons(bp->port);
  700. if (addrs->sa_family == AF_INET) {
  701. struct sockaddr_in *sin;
  702. sin = (struct sockaddr_in *)addrs;
  703. asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
  704. } else if (addrs->sa_family == AF_INET6) {
  705. struct sockaddr_in6 *sin6;
  706. sin6 = (struct sockaddr_in6 *)addrs;
  707. asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
  708. }
  709. pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
  710. __func__, asoc, &asoc->asconf_addr_del_pending->sa,
  711. asoc->asconf_addr_del_pending);
  712. asoc->src_out_of_asoc_ok = 1;
  713. stored = 1;
  714. goto skip_mkasconf;
  715. }
  716. if (laddr == NULL)
  717. return -EINVAL;
  718. /* We do not need RCU protection throughout this loop
  719. * because this is done under a socket lock from the
  720. * setsockopt call.
  721. */
  722. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  723. SCTP_PARAM_DEL_IP);
  724. if (!chunk) {
  725. retval = -ENOMEM;
  726. goto out;
  727. }
  728. skip_mkasconf:
  729. /* Reset use_as_src flag for the addresses in the bind address
  730. * list that are to be deleted.
  731. */
  732. addr_buf = addrs;
  733. for (i = 0; i < addrcnt; i++) {
  734. laddr = addr_buf;
  735. af = sctp_get_af_specific(laddr->v4.sin_family);
  736. list_for_each_entry(saddr, &bp->address_list, list) {
  737. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  738. saddr->state = SCTP_ADDR_DEL;
  739. }
  740. addr_buf += af->sockaddr_len;
  741. }
  742. /* Update the route and saddr entries for all the transports
  743. * as some of the addresses in the bind address list are
  744. * about to be deleted and cannot be used as source addresses.
  745. */
  746. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  747. transports) {
  748. sctp_transport_route(transport, NULL,
  749. sctp_sk(asoc->base.sk));
  750. }
  751. if (stored)
  752. /* We don't need to transmit ASCONF */
  753. continue;
  754. retval = sctp_send_asconf(asoc, chunk);
  755. }
  756. out:
  757. return retval;
  758. }
  759. /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
  760. int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
  761. {
  762. struct sock *sk = sctp_opt2sk(sp);
  763. union sctp_addr *addr;
  764. struct sctp_af *af;
  765. /* It is safe to write port space in caller. */
  766. addr = &addrw->a;
  767. addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
  768. af = sctp_get_af_specific(addr->sa.sa_family);
  769. if (!af)
  770. return -EINVAL;
  771. if (sctp_verify_addr(sk, addr, af->sockaddr_len))
  772. return -EINVAL;
  773. if (addrw->state == SCTP_ADDR_NEW)
  774. return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
  775. else
  776. return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
  777. }
  778. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  779. *
  780. * API 8.1
  781. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  782. * int flags);
  783. *
  784. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  785. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  786. * or IPv6 addresses.
  787. *
  788. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  789. * Section 3.1.2 for this usage.
  790. *
  791. * addrs is a pointer to an array of one or more socket addresses. Each
  792. * address is contained in its appropriate structure (i.e. struct
  793. * sockaddr_in or struct sockaddr_in6) the family of the address type
  794. * must be used to distinguish the address length (note that this
  795. * representation is termed a "packed array" of addresses). The caller
  796. * specifies the number of addresses in the array with addrcnt.
  797. *
  798. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  799. * -1, and sets errno to the appropriate error code.
  800. *
  801. * For SCTP, the port given in each socket address must be the same, or
  802. * sctp_bindx() will fail, setting errno to EINVAL.
  803. *
  804. * The flags parameter is formed from the bitwise OR of zero or more of
  805. * the following currently defined flags:
  806. *
  807. * SCTP_BINDX_ADD_ADDR
  808. *
  809. * SCTP_BINDX_REM_ADDR
  810. *
  811. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  812. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  813. * addresses from the association. The two flags are mutually exclusive;
  814. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  815. * not remove all addresses from an association; sctp_bindx() will
  816. * reject such an attempt with EINVAL.
  817. *
  818. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  819. * additional addresses with an endpoint after calling bind(). Or use
  820. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  821. * socket is associated with so that no new association accepted will be
  822. * associated with those addresses. If the endpoint supports dynamic
  823. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  824. * endpoint to send the appropriate message to the peer to change the
  825. * peers address lists.
  826. *
  827. * Adding and removing addresses from a connected association is
  828. * optional functionality. Implementations that do not support this
  829. * functionality should return EOPNOTSUPP.
  830. *
  831. * Basically do nothing but copying the addresses from user to kernel
  832. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  833. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  834. * from userspace.
  835. *
  836. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  837. * it.
  838. *
  839. * sk The sk of the socket
  840. * addrs The pointer to the addresses in user land
  841. * addrssize Size of the addrs buffer
  842. * op Operation to perform (add or remove, see the flags of
  843. * sctp_bindx)
  844. *
  845. * Returns 0 if ok, <0 errno code on error.
  846. */
  847. static int sctp_setsockopt_bindx(struct sock *sk,
  848. struct sockaddr __user *addrs,
  849. int addrs_size, int op)
  850. {
  851. struct sockaddr *kaddrs;
  852. int err;
  853. int addrcnt = 0;
  854. int walk_size = 0;
  855. struct sockaddr *sa_addr;
  856. void *addr_buf;
  857. struct sctp_af *af;
  858. pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
  859. __func__, sk, addrs, addrs_size, op);
  860. if (unlikely(addrs_size <= 0))
  861. return -EINVAL;
  862. kaddrs = memdup_user(addrs, addrs_size);
  863. if (IS_ERR(kaddrs))
  864. return PTR_ERR(kaddrs);
  865. /* Walk through the addrs buffer and count the number of addresses. */
  866. addr_buf = kaddrs;
  867. while (walk_size < addrs_size) {
  868. if (walk_size + sizeof(sa_family_t) > addrs_size) {
  869. kfree(kaddrs);
  870. return -EINVAL;
  871. }
  872. sa_addr = addr_buf;
  873. af = sctp_get_af_specific(sa_addr->sa_family);
  874. /* If the address family is not supported or if this address
  875. * causes the address buffer to overflow return EINVAL.
  876. */
  877. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  878. kfree(kaddrs);
  879. return -EINVAL;
  880. }
  881. addrcnt++;
  882. addr_buf += af->sockaddr_len;
  883. walk_size += af->sockaddr_len;
  884. }
  885. /* Do the work. */
  886. switch (op) {
  887. case SCTP_BINDX_ADD_ADDR:
  888. /* Allow security module to validate bindx addresses. */
  889. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
  890. (struct sockaddr *)kaddrs,
  891. addrs_size);
  892. if (err)
  893. goto out;
  894. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  895. if (err)
  896. goto out;
  897. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  898. break;
  899. case SCTP_BINDX_REM_ADDR:
  900. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  901. if (err)
  902. goto out;
  903. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  904. break;
  905. default:
  906. err = -EINVAL;
  907. break;
  908. }
  909. out:
  910. kfree(kaddrs);
  911. return err;
  912. }
  913. static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
  914. const union sctp_addr *daddr,
  915. const struct sctp_initmsg *init,
  916. struct sctp_transport **tp)
  917. {
  918. struct sctp_association *asoc;
  919. struct sock *sk = ep->base.sk;
  920. struct net *net = sock_net(sk);
  921. enum sctp_scope scope;
  922. int err;
  923. if (sctp_endpoint_is_peeled_off(ep, daddr))
  924. return -EADDRNOTAVAIL;
  925. if (!ep->base.bind_addr.port) {
  926. if (sctp_autobind(sk))
  927. return -EAGAIN;
  928. } else {
  929. if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
  930. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  931. return -EACCES;
  932. }
  933. scope = sctp_scope(daddr);
  934. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  935. if (!asoc)
  936. return -ENOMEM;
  937. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
  938. if (err < 0)
  939. goto free;
  940. *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  941. if (!*tp) {
  942. err = -ENOMEM;
  943. goto free;
  944. }
  945. if (!init)
  946. return 0;
  947. if (init->sinit_num_ostreams) {
  948. __u16 outcnt = init->sinit_num_ostreams;
  949. asoc->c.sinit_num_ostreams = outcnt;
  950. /* outcnt has been changed, need to re-init stream */
  951. err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
  952. if (err)
  953. goto free;
  954. }
  955. if (init->sinit_max_instreams)
  956. asoc->c.sinit_max_instreams = init->sinit_max_instreams;
  957. if (init->sinit_max_attempts)
  958. asoc->max_init_attempts = init->sinit_max_attempts;
  959. if (init->sinit_max_init_timeo)
  960. asoc->max_init_timeo =
  961. msecs_to_jiffies(init->sinit_max_init_timeo);
  962. return 0;
  963. free:
  964. sctp_association_free(asoc);
  965. return err;
  966. }
  967. static int sctp_connect_add_peer(struct sctp_association *asoc,
  968. union sctp_addr *daddr, int addr_len)
  969. {
  970. struct sctp_endpoint *ep = asoc->ep;
  971. struct sctp_association *old;
  972. struct sctp_transport *t;
  973. int err;
  974. err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
  975. if (err)
  976. return err;
  977. old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
  978. if (old && old != asoc)
  979. return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  980. : -EALREADY;
  981. if (sctp_endpoint_is_peeled_off(ep, daddr))
  982. return -EADDRNOTAVAIL;
  983. t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  984. if (!t)
  985. return -ENOMEM;
  986. return 0;
  987. }
  988. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  989. *
  990. * Common routine for handling connect() and sctp_connectx().
  991. * Connect will come in with just a single address.
  992. */
  993. static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
  994. int addrs_size, int flags, sctp_assoc_t *assoc_id)
  995. {
  996. struct sctp_sock *sp = sctp_sk(sk);
  997. struct sctp_endpoint *ep = sp->ep;
  998. struct sctp_transport *transport;
  999. struct sctp_association *asoc;
  1000. void *addr_buf = kaddrs;
  1001. union sctp_addr *daddr;
  1002. struct sctp_af *af;
  1003. int walk_size, err;
  1004. long timeo;
  1005. if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
  1006. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
  1007. return -EISCONN;
  1008. daddr = addr_buf;
  1009. af = sctp_get_af_specific(daddr->sa.sa_family);
  1010. if (!af || af->sockaddr_len > addrs_size)
  1011. return -EINVAL;
  1012. err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
  1013. if (err)
  1014. return err;
  1015. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1016. if (asoc)
  1017. return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  1018. : -EALREADY;
  1019. err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
  1020. if (err)
  1021. return err;
  1022. asoc = transport->asoc;
  1023. addr_buf += af->sockaddr_len;
  1024. walk_size = af->sockaddr_len;
  1025. while (walk_size < addrs_size) {
  1026. err = -EINVAL;
  1027. if (walk_size + sizeof(sa_family_t) > addrs_size)
  1028. goto out_free;
  1029. daddr = addr_buf;
  1030. af = sctp_get_af_specific(daddr->sa.sa_family);
  1031. if (!af || af->sockaddr_len + walk_size > addrs_size)
  1032. goto out_free;
  1033. if (asoc->peer.port != ntohs(daddr->v4.sin_port))
  1034. goto out_free;
  1035. err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
  1036. if (err)
  1037. goto out_free;
  1038. addr_buf += af->sockaddr_len;
  1039. walk_size += af->sockaddr_len;
  1040. }
  1041. /* In case the user of sctp_connectx() wants an association
  1042. * id back, assign one now.
  1043. */
  1044. if (assoc_id) {
  1045. err = sctp_assoc_set_id(asoc, GFP_KERNEL);
  1046. if (err < 0)
  1047. goto out_free;
  1048. }
  1049. err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
  1050. if (err < 0)
  1051. goto out_free;
  1052. /* Initialize sk's dport and daddr for getpeername() */
  1053. inet_sk(sk)->inet_dport = htons(asoc->peer.port);
  1054. sp->pf->to_sk_daddr(daddr, sk);
  1055. sk->sk_err = 0;
  1056. if (assoc_id)
  1057. *assoc_id = asoc->assoc_id;
  1058. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1059. return sctp_wait_for_connect(asoc, &timeo);
  1060. out_free:
  1061. pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
  1062. __func__, asoc, kaddrs, err);
  1063. sctp_association_free(asoc);
  1064. return err;
  1065. }
  1066. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  1067. *
  1068. * API 8.9
  1069. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
  1070. * sctp_assoc_t *asoc);
  1071. *
  1072. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  1073. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1074. * or IPv6 addresses.
  1075. *
  1076. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1077. * Section 3.1.2 for this usage.
  1078. *
  1079. * addrs is a pointer to an array of one or more socket addresses. Each
  1080. * address is contained in its appropriate structure (i.e. struct
  1081. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1082. * must be used to distengish the address length (note that this
  1083. * representation is termed a "packed array" of addresses). The caller
  1084. * specifies the number of addresses in the array with addrcnt.
  1085. *
  1086. * On success, sctp_connectx() returns 0. It also sets the assoc_id to
  1087. * the association id of the new association. On failure, sctp_connectx()
  1088. * returns -1, and sets errno to the appropriate error code. The assoc_id
  1089. * is not touched by the kernel.
  1090. *
  1091. * For SCTP, the port given in each socket address must be the same, or
  1092. * sctp_connectx() will fail, setting errno to EINVAL.
  1093. *
  1094. * An application can use sctp_connectx to initiate an association with
  1095. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1096. * allows a caller to specify multiple addresses at which a peer can be
  1097. * reached. The way the SCTP stack uses the list of addresses to set up
  1098. * the association is implementation dependent. This function only
  1099. * specifies that the stack will try to make use of all the addresses in
  1100. * the list when needed.
  1101. *
  1102. * Note that the list of addresses passed in is only used for setting up
  1103. * the association. It does not necessarily equal the set of addresses
  1104. * the peer uses for the resulting association. If the caller wants to
  1105. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1106. * retrieve them after the association has been set up.
  1107. *
  1108. * Basically do nothing but copying the addresses from user to kernel
  1109. * land and invoking either sctp_connectx(). This is used for tunneling
  1110. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1111. *
  1112. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1113. * it.
  1114. *
  1115. * sk The sk of the socket
  1116. * addrs The pointer to the addresses in user land
  1117. * addrssize Size of the addrs buffer
  1118. *
  1119. * Returns >=0 if ok, <0 errno code on error.
  1120. */
  1121. static int __sctp_setsockopt_connectx(struct sock *sk,
  1122. struct sockaddr __user *addrs,
  1123. int addrs_size,
  1124. sctp_assoc_t *assoc_id)
  1125. {
  1126. struct sockaddr *kaddrs;
  1127. int err = 0, flags = 0;
  1128. pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
  1129. __func__, sk, addrs, addrs_size);
  1130. /* make sure the 1st addr's sa_family is accessible later */
  1131. if (unlikely(addrs_size < sizeof(sa_family_t)))
  1132. return -EINVAL;
  1133. kaddrs = memdup_user(addrs, addrs_size);
  1134. if (IS_ERR(kaddrs))
  1135. return PTR_ERR(kaddrs);
  1136. /* Allow security module to validate connectx addresses. */
  1137. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
  1138. (struct sockaddr *)kaddrs,
  1139. addrs_size);
  1140. if (err)
  1141. goto out_free;
  1142. /* in-kernel sockets don't generally have a file allocated to them
  1143. * if all they do is call sock_create_kern().
  1144. */
  1145. if (sk->sk_socket->file)
  1146. flags = sk->sk_socket->file->f_flags;
  1147. err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
  1148. out_free:
  1149. kfree(kaddrs);
  1150. return err;
  1151. }
  1152. /*
  1153. * This is an older interface. It's kept for backward compatibility
  1154. * to the option that doesn't provide association id.
  1155. */
  1156. static int sctp_setsockopt_connectx_old(struct sock *sk,
  1157. struct sockaddr __user *addrs,
  1158. int addrs_size)
  1159. {
  1160. return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
  1161. }
  1162. /*
  1163. * New interface for the API. The since the API is done with a socket
  1164. * option, to make it simple we feed back the association id is as a return
  1165. * indication to the call. Error is always negative and association id is
  1166. * always positive.
  1167. */
  1168. static int sctp_setsockopt_connectx(struct sock *sk,
  1169. struct sockaddr __user *addrs,
  1170. int addrs_size)
  1171. {
  1172. sctp_assoc_t assoc_id = 0;
  1173. int err = 0;
  1174. err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
  1175. if (err)
  1176. return err;
  1177. else
  1178. return assoc_id;
  1179. }
  1180. /*
  1181. * New (hopefully final) interface for the API.
  1182. * We use the sctp_getaddrs_old structure so that use-space library
  1183. * can avoid any unnecessary allocations. The only different part
  1184. * is that we store the actual length of the address buffer into the
  1185. * addrs_num structure member. That way we can re-use the existing
  1186. * code.
  1187. */
  1188. #ifdef CONFIG_COMPAT
  1189. struct compat_sctp_getaddrs_old {
  1190. sctp_assoc_t assoc_id;
  1191. s32 addr_num;
  1192. compat_uptr_t addrs; /* struct sockaddr * */
  1193. };
  1194. #endif
  1195. static int sctp_getsockopt_connectx3(struct sock *sk, int len,
  1196. char __user *optval,
  1197. int __user *optlen)
  1198. {
  1199. struct sctp_getaddrs_old param;
  1200. sctp_assoc_t assoc_id = 0;
  1201. int err = 0;
  1202. #ifdef CONFIG_COMPAT
  1203. if (in_compat_syscall()) {
  1204. struct compat_sctp_getaddrs_old param32;
  1205. if (len < sizeof(param32))
  1206. return -EINVAL;
  1207. if (copy_from_user(&param32, optval, sizeof(param32)))
  1208. return -EFAULT;
  1209. param.assoc_id = param32.assoc_id;
  1210. param.addr_num = param32.addr_num;
  1211. param.addrs = compat_ptr(param32.addrs);
  1212. } else
  1213. #endif
  1214. {
  1215. if (len < sizeof(param))
  1216. return -EINVAL;
  1217. if (copy_from_user(&param, optval, sizeof(param)))
  1218. return -EFAULT;
  1219. }
  1220. err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
  1221. param.addrs, param.addr_num,
  1222. &assoc_id);
  1223. if (err == 0 || err == -EINPROGRESS) {
  1224. if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
  1225. return -EFAULT;
  1226. if (put_user(sizeof(assoc_id), optlen))
  1227. return -EFAULT;
  1228. }
  1229. return err;
  1230. }
  1231. /* API 3.1.4 close() - UDP Style Syntax
  1232. * Applications use close() to perform graceful shutdown (as described in
  1233. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1234. * by a UDP-style socket.
  1235. *
  1236. * The syntax is
  1237. *
  1238. * ret = close(int sd);
  1239. *
  1240. * sd - the socket descriptor of the associations to be closed.
  1241. *
  1242. * To gracefully shutdown a specific association represented by the
  1243. * UDP-style socket, an application should use the sendmsg() call,
  1244. * passing no user data, but including the appropriate flag in the
  1245. * ancillary data (see Section xxxx).
  1246. *
  1247. * If sd in the close() call is a branched-off socket representing only
  1248. * one association, the shutdown is performed on that association only.
  1249. *
  1250. * 4.1.6 close() - TCP Style Syntax
  1251. *
  1252. * Applications use close() to gracefully close down an association.
  1253. *
  1254. * The syntax is:
  1255. *
  1256. * int close(int sd);
  1257. *
  1258. * sd - the socket descriptor of the association to be closed.
  1259. *
  1260. * After an application calls close() on a socket descriptor, no further
  1261. * socket operations will succeed on that descriptor.
  1262. *
  1263. * API 7.1.4 SO_LINGER
  1264. *
  1265. * An application using the TCP-style socket can use this option to
  1266. * perform the SCTP ABORT primitive. The linger option structure is:
  1267. *
  1268. * struct linger {
  1269. * int l_onoff; // option on/off
  1270. * int l_linger; // linger time
  1271. * };
  1272. *
  1273. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1274. * to 0, calling close() is the same as the ABORT primitive. If the
  1275. * value is set to a negative value, the setsockopt() call will return
  1276. * an error. If the value is set to a positive value linger_time, the
  1277. * close() can be blocked for at most linger_time ms. If the graceful
  1278. * shutdown phase does not finish during this period, close() will
  1279. * return but the graceful shutdown phase continues in the system.
  1280. */
  1281. static void sctp_close(struct sock *sk, long timeout)
  1282. {
  1283. struct net *net = sock_net(sk);
  1284. struct sctp_endpoint *ep;
  1285. struct sctp_association *asoc;
  1286. struct list_head *pos, *temp;
  1287. unsigned int data_was_unread;
  1288. pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
  1289. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  1290. sk->sk_shutdown = SHUTDOWN_MASK;
  1291. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  1292. ep = sctp_sk(sk)->ep;
  1293. /* Clean up any skbs sitting on the receive queue. */
  1294. data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1295. data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1296. /* Walk all associations on an endpoint. */
  1297. list_for_each_safe(pos, temp, &ep->asocs) {
  1298. asoc = list_entry(pos, struct sctp_association, asocs);
  1299. if (sctp_style(sk, TCP)) {
  1300. /* A closed association can still be in the list if
  1301. * it belongs to a TCP-style listening socket that is
  1302. * not yet accepted. If so, free it. If not, send an
  1303. * ABORT or SHUTDOWN based on the linger options.
  1304. */
  1305. if (sctp_state(asoc, CLOSED)) {
  1306. sctp_association_free(asoc);
  1307. continue;
  1308. }
  1309. }
  1310. if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
  1311. !skb_queue_empty(&asoc->ulpq.reasm) ||
  1312. !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
  1313. (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
  1314. struct sctp_chunk *chunk;
  1315. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1316. sctp_primitive_ABORT(net, asoc, chunk);
  1317. } else
  1318. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1319. }
  1320. /* On a TCP-style socket, block for at most linger_time if set. */
  1321. if (sctp_style(sk, TCP) && timeout)
  1322. sctp_wait_for_close(sk, timeout);
  1323. /* This will run the backlog queue. */
  1324. release_sock(sk);
  1325. /* Supposedly, no process has access to the socket, but
  1326. * the net layers still may.
  1327. * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
  1328. * held and that should be grabbed before socket lock.
  1329. */
  1330. spin_lock_bh(&net->sctp.addr_wq_lock);
  1331. bh_lock_sock_nested(sk);
  1332. /* Hold the sock, since sk_common_release() will put sock_put()
  1333. * and we have just a little more cleanup.
  1334. */
  1335. sock_hold(sk);
  1336. sk_common_release(sk);
  1337. bh_unlock_sock(sk);
  1338. spin_unlock_bh(&net->sctp.addr_wq_lock);
  1339. sock_put(sk);
  1340. SCTP_DBG_OBJCNT_DEC(sock);
  1341. }
  1342. /* Handle EPIPE error. */
  1343. static int sctp_error(struct sock *sk, int flags, int err)
  1344. {
  1345. if (err == -EPIPE)
  1346. err = sock_error(sk) ? : -EPIPE;
  1347. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1348. send_sig(SIGPIPE, current, 0);
  1349. return err;
  1350. }
  1351. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1352. *
  1353. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1354. * and receive data from its peer.
  1355. *
  1356. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1357. * int flags);
  1358. *
  1359. * socket - the socket descriptor of the endpoint.
  1360. * message - pointer to the msghdr structure which contains a single
  1361. * user message and possibly some ancillary data.
  1362. *
  1363. * See Section 5 for complete description of the data
  1364. * structures.
  1365. *
  1366. * flags - flags sent or received with the user message, see Section
  1367. * 5 for complete description of the flags.
  1368. *
  1369. * Note: This function could use a rewrite especially when explicit
  1370. * connect support comes in.
  1371. */
  1372. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1373. static int sctp_msghdr_parse(const struct msghdr *msg,
  1374. struct sctp_cmsgs *cmsgs);
  1375. static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
  1376. struct sctp_sndrcvinfo *srinfo,
  1377. const struct msghdr *msg, size_t msg_len)
  1378. {
  1379. __u16 sflags;
  1380. int err;
  1381. if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
  1382. return -EPIPE;
  1383. if (msg_len > sk->sk_sndbuf)
  1384. return -EMSGSIZE;
  1385. memset(cmsgs, 0, sizeof(*cmsgs));
  1386. err = sctp_msghdr_parse(msg, cmsgs);
  1387. if (err) {
  1388. pr_debug("%s: msghdr parse err:%x\n", __func__, err);
  1389. return err;
  1390. }
  1391. memset(srinfo, 0, sizeof(*srinfo));
  1392. if (cmsgs->srinfo) {
  1393. srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
  1394. srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
  1395. srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
  1396. srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
  1397. srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
  1398. srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
  1399. }
  1400. if (cmsgs->sinfo) {
  1401. srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
  1402. srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
  1403. srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
  1404. srinfo->sinfo_context = cmsgs->sinfo->snd_context;
  1405. srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
  1406. }
  1407. if (cmsgs->prinfo) {
  1408. srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
  1409. SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
  1410. cmsgs->prinfo->pr_policy);
  1411. }
  1412. sflags = srinfo->sinfo_flags;
  1413. if (!sflags && msg_len)
  1414. return 0;
  1415. if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
  1416. return -EINVAL;
  1417. if (((sflags & SCTP_EOF) && msg_len > 0) ||
  1418. (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
  1419. return -EINVAL;
  1420. if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
  1421. return -EINVAL;
  1422. return 0;
  1423. }
  1424. static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
  1425. struct sctp_cmsgs *cmsgs,
  1426. union sctp_addr *daddr,
  1427. struct sctp_transport **tp)
  1428. {
  1429. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1430. struct sctp_association *asoc;
  1431. struct cmsghdr *cmsg;
  1432. __be32 flowinfo = 0;
  1433. struct sctp_af *af;
  1434. int err;
  1435. *tp = NULL;
  1436. if (sflags & (SCTP_EOF | SCTP_ABORT))
  1437. return -EINVAL;
  1438. if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
  1439. sctp_sstate(sk, CLOSING)))
  1440. return -EADDRNOTAVAIL;
  1441. /* Label connection socket for first association 1-to-many
  1442. * style for client sequence socket()->sendmsg(). This
  1443. * needs to be done before sctp_assoc_add_peer() as that will
  1444. * set up the initial packet that needs to account for any
  1445. * security ip options (CIPSO/CALIPSO) added to the packet.
  1446. */
  1447. af = sctp_get_af_specific(daddr->sa.sa_family);
  1448. if (!af)
  1449. return -EINVAL;
  1450. err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
  1451. (struct sockaddr *)daddr,
  1452. af->sockaddr_len);
  1453. if (err < 0)
  1454. return err;
  1455. err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
  1456. if (err)
  1457. return err;
  1458. asoc = (*tp)->asoc;
  1459. if (!cmsgs->addrs_msg)
  1460. return 0;
  1461. if (daddr->sa.sa_family == AF_INET6)
  1462. flowinfo = daddr->v6.sin6_flowinfo;
  1463. /* sendv addr list parse */
  1464. for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
  1465. union sctp_addr _daddr;
  1466. int dlen;
  1467. if (cmsg->cmsg_level != IPPROTO_SCTP ||
  1468. (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
  1469. cmsg->cmsg_type != SCTP_DSTADDRV6))
  1470. continue;
  1471. daddr = &_daddr;
  1472. memset(daddr, 0, sizeof(*daddr));
  1473. dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1474. if (cmsg->cmsg_type == SCTP_DST