PageRenderTime 336ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 3ms

/net/sctp/socket.c

http://github.com/mirrors/linux
C | 9687 lines | 6253 code | 1457 blank | 1977 comment | 1474 complexity | 32df4213cbcfd876d7520045e797b19b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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_DSTADDRV4) {
  1475. if (dlen < sizeof(struct in_addr)) {
  1476. err = -EINVAL;
  1477. goto free;
  1478. }
  1479. dlen = sizeof(struct in_addr);
  1480. daddr->v4.sin_family = AF_INET;
  1481. daddr->v4.sin_port = htons(asoc->peer.port);
  1482. memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
  1483. } else {
  1484. if (dlen < sizeof(struct in6_addr)) {
  1485. err = -EINVAL;
  1486. goto free;
  1487. }
  1488. dlen = sizeof(struct in6_addr);
  1489. daddr->v6.sin6_flowinfo = flowinfo;
  1490. daddr->v6.sin6_family = AF_INET6;
  1491. daddr->v6.sin6_port = htons(asoc->peer.port);
  1492. memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
  1493. }
  1494. err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
  1495. if (err)
  1496. goto free;
  1497. }
  1498. return 0;
  1499. free:
  1500. sctp_association_free(asoc);
  1501. return err;
  1502. }
  1503. static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
  1504. __u16 sflags, struct msghdr *msg,
  1505. size_t msg_len)
  1506. {
  1507. struct sock *sk = asoc->base.sk;
  1508. struct net *net = sock_net(sk);
  1509. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
  1510. return -EPIPE;
  1511. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
  1512. !sctp_state(asoc, ESTABLISHED))
  1513. return 0;
  1514. if (sflags & SCTP_EOF) {
  1515. pr_debug("%s: shutting down association:%p\n", __func__, asoc);
  1516. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1517. return 0;
  1518. }
  1519. if (sflags & SCTP_ABORT) {
  1520. struct sctp_chunk *chunk;
  1521. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1522. if (!chunk)
  1523. return -ENOMEM;
  1524. pr_debug("%s: aborting association:%p\n", __func__, asoc);
  1525. sctp_primitive_ABORT(net, asoc, chunk);
  1526. iov_iter_revert(&msg->msg_iter, msg_len);
  1527. return 0;
  1528. }
  1529. return 1;
  1530. }
  1531. static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
  1532. struct msghdr *msg, size_t msg_len,
  1533. struct sctp_transport *transport,
  1534. struct sctp_sndrcvinfo *sinfo)
  1535. {
  1536. struct sock *sk = asoc->base.sk;
  1537. struct sctp_sock *sp = sctp_sk(sk);
  1538. struct net *net = sock_net(sk);
  1539. struct sctp_datamsg *datamsg;
  1540. bool wait_connect = false;
  1541. struct sctp_chunk *chunk;
  1542. long timeo;
  1543. int err;
  1544. if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
  1545. err = -EINVAL;
  1546. goto err;
  1547. }
  1548. if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
  1549. err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
  1550. if (err)
  1551. goto err;
  1552. }
  1553. if (sp->disable_fragments && msg_len > asoc->frag_point) {
  1554. err = -EMSGSIZE;
  1555. goto err;
  1556. }
  1557. if (asoc->pmtu_pending) {
  1558. if (sp->param_flags & SPP_PMTUD_ENABLE)
  1559. sctp_assoc_sync_pmtu(asoc);
  1560. asoc->pmtu_pending = 0;
  1561. }
  1562. if (sctp_wspace(asoc) < (int)msg_len)
  1563. sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
  1564. if (sk_under_memory_pressure(sk))
  1565. sk_mem_reclaim(sk);
  1566. if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
  1567. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1568. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1569. if (err)
  1570. goto err;
  1571. }
  1572. if (sctp_state(asoc, CLOSED)) {
  1573. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1574. if (err)
  1575. goto err;
  1576. if (asoc->ep->intl_enable) {
  1577. timeo = sock_sndtimeo(sk, 0);
  1578. err = sctp_wait_for_connect(asoc, &timeo);
  1579. if (err) {
  1580. err = -ESRCH;
  1581. goto err;
  1582. }
  1583. } else {
  1584. wait_connect = true;
  1585. }
  1586. pr_debug("%s: we associated primitively\n", __func__);
  1587. }
  1588. datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
  1589. if (IS_ERR(datamsg)) {
  1590. err = PTR_ERR(datamsg);
  1591. goto err;
  1592. }
  1593. asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
  1594. list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
  1595. sctp_chunk_hold(chunk);
  1596. sctp_set_owner_w(chunk);
  1597. chunk->transport = transport;
  1598. }
  1599. err = sctp_primitive_SEND(net, asoc, datamsg);
  1600. if (err) {
  1601. sctp_datamsg_free(datamsg);
  1602. goto err;
  1603. }
  1604. pr_debug("%s: we sent primitively\n", __func__);
  1605. sctp_datamsg_put(datamsg);
  1606. if (unlikely(wait_connect)) {
  1607. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1608. sctp_wait_for_connect(asoc, &timeo);
  1609. }
  1610. err = msg_len;
  1611. err:
  1612. return err;
  1613. }
  1614. static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
  1615. const struct msghdr *msg,
  1616. struct sctp_cmsgs *cmsgs)
  1617. {
  1618. union sctp_addr *daddr = NULL;
  1619. int err;
  1620. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1621. int len = msg->msg_namelen;
  1622. if (len > sizeof(*daddr))
  1623. len = sizeof(*daddr);
  1624. daddr = (union sctp_addr *)msg->msg_name;
  1625. err = sctp_verify_addr(sk, daddr, len);
  1626. if (err)
  1627. return ERR_PTR(err);
  1628. }
  1629. return daddr;
  1630. }
  1631. static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
  1632. struct sctp_sndrcvinfo *sinfo,
  1633. struct sctp_cmsgs *cmsgs)
  1634. {
  1635. if (!cmsgs->srinfo && !cmsgs->sinfo) {
  1636. sinfo->sinfo_stream = asoc->default_stream;
  1637. sinfo->sinfo_ppid = asoc->default_ppid;
  1638. sinfo->sinfo_context = asoc->default_context;
  1639. sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
  1640. if (!cmsgs->prinfo)
  1641. sinfo->sinfo_flags = asoc->default_flags;
  1642. }
  1643. if (!cmsgs->srinfo && !cmsgs->prinfo)
  1644. sinfo->sinfo_timetolive = asoc->default_timetolive;
  1645. if (cmsgs->authinfo) {
  1646. /* Reuse sinfo_tsn to indicate that authinfo was set and
  1647. * sinfo_ssn to save the keyid on tx path.
  1648. */
  1649. sinfo->sinfo_tsn = 1;
  1650. sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
  1651. }
  1652. }
  1653. static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
  1654. {
  1655. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1656. struct sctp_transport *transport = NULL;
  1657. struct sctp_sndrcvinfo _sinfo, *sinfo;
  1658. struct sctp_association *asoc, *tmp;
  1659. struct sctp_cmsgs cmsgs;
  1660. union sctp_addr *daddr;
  1661. bool new = false;
  1662. __u16 sflags;
  1663. int err;
  1664. /* Parse and get snd_info */
  1665. err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
  1666. if (err)
  1667. goto out;
  1668. sinfo = &_sinfo;
  1669. sflags = sinfo->sinfo_flags;
  1670. /* Get daddr from msg */
  1671. daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
  1672. if (IS_ERR(daddr)) {
  1673. err = PTR_ERR(daddr);
  1674. goto out;
  1675. }
  1676. lock_sock(sk);
  1677. /* SCTP_SENDALL process */
  1678. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
  1679. list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
  1680. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1681. msg_len);
  1682. if (err == 0)
  1683. continue;
  1684. if (err < 0)
  1685. goto out_unlock;
  1686. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1687. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
  1688. NULL, sinfo);
  1689. if (err < 0)
  1690. goto out_unlock;
  1691. iov_iter_revert(&msg->msg_iter, err);
  1692. }
  1693. goto out_unlock;
  1694. }
  1695. /* Get and check or create asoc */
  1696. if (daddr) {
  1697. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1698. if (asoc) {
  1699. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1700. msg_len);
  1701. if (err <= 0)
  1702. goto out_unlock;
  1703. } else {
  1704. err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
  1705. &transport);
  1706. if (err)
  1707. goto out_unlock;
  1708. asoc = transport->asoc;
  1709. new = true;
  1710. }
  1711. if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
  1712. transport = NULL;
  1713. } else {
  1714. asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
  1715. if (!asoc) {
  1716. err = -EPIPE;
  1717. goto out_unlock;
  1718. }
  1719. err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
  1720. if (err <= 0)
  1721. goto out_unlock;
  1722. }
  1723. /* Update snd_info with the asoc */
  1724. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1725. /* Send msg to the asoc */
  1726. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
  1727. if (err < 0 && err != -ESRCH && new)
  1728. sctp_association_free(asoc);
  1729. out_unlock:
  1730. release_sock(sk);
  1731. out:
  1732. return sctp_error(sk, msg->msg_flags, err);
  1733. }
  1734. /* This is an extended version of skb_pull() that removes the data from the
  1735. * start of a skb even when data is spread across the list of skb's in the
  1736. * frag_list. len specifies the total amount of data that needs to be removed.
  1737. * when 'len' bytes could be removed from the skb, it returns 0.
  1738. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1739. * could not be removed.
  1740. */
  1741. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1742. {
  1743. struct sk_buff *list;
  1744. int skb_len = skb_headlen(skb);
  1745. int rlen;
  1746. if (len <= skb_len) {
  1747. __skb_pull(skb, len);
  1748. return 0;
  1749. }
  1750. len -= skb_len;
  1751. __skb_pull(skb, skb_len);
  1752. skb_walk_frags(skb, list) {
  1753. rlen = sctp_skb_pull(list, len);
  1754. skb->len -= (len-rlen);
  1755. skb->data_len -= (len-rlen);
  1756. if (!rlen)
  1757. return 0;
  1758. len = rlen;
  1759. }
  1760. return len;
  1761. }
  1762. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1763. *
  1764. * ssize_t recvmsg(int socket, struct msghdr *message,
  1765. * int flags);
  1766. *
  1767. * socket - the socket descriptor of the endpoint.
  1768. * message - pointer to the msghdr structure which contains a single
  1769. * user message and possibly some ancillary data.
  1770. *
  1771. * See Section 5 for complete description of the data
  1772. * structures.
  1773. *
  1774. * flags - flags sent or received with the user message, see Section
  1775. * 5 for complete description of the flags.
  1776. */
  1777. static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  1778. int noblock, int flags, int *addr_len)
  1779. {
  1780. struct sctp_ulpevent *event = NULL;
  1781. struct sctp_sock *sp = sctp_sk(sk);
  1782. struct sk_buff *skb, *head_skb;
  1783. int copied;
  1784. int err = 0;
  1785. int skb_len;
  1786. pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
  1787. "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
  1788. addr_len);
  1789. lock_sock(sk);
  1790. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
  1791. !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
  1792. err = -ENOTCONN;
  1793. goto out;
  1794. }
  1795. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1796. if (!skb)
  1797. goto out;
  1798. /* Get the total length of the skb including any skb's in the
  1799. * frag_list.
  1800. */
  1801. skb_len = skb->len;
  1802. copied = skb_len;
  1803. if (copied > len)
  1804. copied = len;
  1805. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  1806. event = sctp_skb2event(skb);
  1807. if (err)
  1808. goto out_free;
  1809. if (event->chunk && event->chunk->head_skb)
  1810. head_skb = event->chunk->head_skb;
  1811. else
  1812. head_skb = skb;
  1813. sock_recv_ts_and_drops(msg, sk, head_skb);
  1814. if (sctp_ulpevent_is_notification(event)) {
  1815. msg->msg_flags |= MSG_NOTIFICATION;
  1816. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1817. } else {
  1818. sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
  1819. }
  1820. /* Check if we allow SCTP_NXTINFO. */
  1821. if (sp->recvnxtinfo)
  1822. sctp_ulpevent_read_nxtinfo(event, msg, sk);
  1823. /* Check if we allow SCTP_RCVINFO. */
  1824. if (sp->recvrcvinfo)
  1825. sctp_ulpevent_read_rcvinfo(event, msg);
  1826. /* Check if we allow SCTP_SNDRCVINFO. */
  1827. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
  1828. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1829. err = copied;
  1830. /* If skb's length exceeds the user's buffer, update the skb and
  1831. * push it back to the receive_queue so that the next call to
  1832. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1833. */
  1834. if (skb_len > copied) {
  1835. msg->msg_flags &= ~MSG_EOR;
  1836. if (flags & MSG_PEEK)
  1837. goto out_free;
  1838. sctp_skb_pull(skb, copied);
  1839. skb_queue_head(&sk->sk_receive_queue, skb);
  1840. /* When only partial message is copied to the user, increase
  1841. * rwnd by that amount. If all the data in the skb is read,
  1842. * rwnd is updated when the event is freed.
  1843. */
  1844. if (!sctp_ulpevent_is_notification(event))
  1845. sctp_assoc_rwnd_increase(event->asoc, copied);
  1846. goto out;
  1847. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1848. (event->msg_flags & MSG_EOR))
  1849. msg->msg_flags |= MSG_EOR;
  1850. else
  1851. msg->msg_flags &= ~MSG_EOR;
  1852. out_free:
  1853. if (flags & MSG_PEEK) {
  1854. /* Release the skb reference acquired after peeking the skb in
  1855. * sctp_skb_recv_datagram().
  1856. */
  1857. kfree_skb(skb);
  1858. } else {
  1859. /* Free the event which includes releasing the reference to
  1860. * the owner of the skb, freeing the skb and updating the
  1861. * rwnd.
  1862. */
  1863. sctp_ulpevent_free(event);
  1864. }
  1865. out:
  1866. release_sock(sk);
  1867. return err;
  1868. }
  1869. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1870. *
  1871. * This option is a on/off flag. If enabled no SCTP message
  1872. * fragmentation will be performed. Instead if a message being sent
  1873. * exceeds the current PMTU size, the message will NOT be sent and
  1874. * instead a error will be indicated to the user.
  1875. */
  1876. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1877. char __user *optval,
  1878. unsigned int optlen)
  1879. {
  1880. int val;
  1881. if (optlen < sizeof(int))
  1882. return -EINVAL;
  1883. if (get_user(val, (int __user *)optval))
  1884. return -EFAULT;
  1885. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1886. return 0;
  1887. }
  1888. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1889. unsigned int optlen)
  1890. {
  1891. struct sctp_event_subscribe subscribe;
  1892. __u8 *sn_type = (__u8 *)&subscribe;
  1893. struct sctp_sock *sp = sctp_sk(sk);
  1894. struct sctp_association *asoc;
  1895. int i;
  1896. if (optlen > sizeof(struct sctp_event_subscribe))
  1897. return -EINVAL;
  1898. if (copy_from_user(&subscribe, optval, optlen))
  1899. return -EFAULT;
  1900. for (i = 0; i < optlen; i++)
  1901. sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
  1902. sn_type[i]);
  1903. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  1904. asoc->subscribe = sctp_sk(sk)->subscribe;
  1905. /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
  1906. * if there is no data to be sent or retransmit, the stack will
  1907. * immediately send up this notification.
  1908. */
  1909. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
  1910. struct sctp_ulpevent *event;
  1911. asoc = sctp_id2assoc(sk, 0);
  1912. if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
  1913. event = sctp_ulpevent_make_sender_dry_event(asoc,
  1914. GFP_USER | __GFP_NOWARN);
  1915. if (!event)
  1916. return -ENOMEM;
  1917. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  1918. }
  1919. }
  1920. return 0;
  1921. }
  1922. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1923. *
  1924. * This socket option is applicable to the UDP-style socket only. When
  1925. * set it will cause associations that are idle for more than the
  1926. * specified number of seconds to automatically close. An association
  1927. * being idle is defined an association that has NOT sent or received
  1928. * user data. The special value of '0' indicates that no automatic
  1929. * close of any associations should be performed. The option expects an
  1930. * integer defining the number of seconds of idle time before an
  1931. * association is closed.
  1932. */
  1933. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1934. unsigned int optlen)
  1935. {
  1936. struct sctp_sock *sp = sctp_sk(sk);
  1937. struct net *net = sock_net(sk);
  1938. /* Applicable to UDP-style socket only */
  1939. if (sctp_style(sk, TCP))
  1940. return -EOPNOTSUPP;
  1941. if (optlen != sizeof(int))
  1942. return -EINVAL;
  1943. if (copy_from_user(&sp->autoclose, optval, optlen))
  1944. return -EFAULT;
  1945. if (sp->autoclose > net->sctp.max_autoclose)
  1946. sp->autoclose = net->sctp.max_autoclose;
  1947. return 0;
  1948. }
  1949. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1950. *
  1951. * Applications can enable or disable heartbeats for any peer address of
  1952. * an association, modify an address's heartbeat interval, force a
  1953. * heartbeat to be sent immediately, and adjust the address's maximum
  1954. * number of retransmissions sent before an address is considered
  1955. * unreachable. The following structure is used to access and modify an
  1956. * address's parameters:
  1957. *
  1958. * struct sctp_paddrparams {
  1959. * sctp_assoc_t spp_assoc_id;
  1960. * struct sockaddr_storage spp_address;
  1961. * uint32_t spp_hbinterval;
  1962. * uint16_t spp_pathmaxrxt;
  1963. * uint32_t spp_pathmtu;
  1964. * uint32_t spp_sackdelay;
  1965. * uint32_t spp_flags;
  1966. * uint32_t spp_ipv6_flowlabel;
  1967. * uint8_t spp_dscp;
  1968. * };
  1969. *
  1970. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1971. * application, and identifies the association for
  1972. * this query.
  1973. * spp_address - This specifies which address is of interest.
  1974. * spp_hbinterval - This contains the value of the heartbeat interval,
  1975. * in milliseconds. If a value of zero
  1976. * is present in this field then no changes are to
  1977. * be made to this parameter.
  1978. * spp_pathmaxrxt - This contains the maximum number of
  1979. * retransmissions before this address shall be
  1980. * considered unreachable. If a value of zero
  1981. * is present in this field then no changes are to
  1982. * be made to this parameter.
  1983. * spp_pathmtu - When Path MTU discovery is disabled the value
  1984. * specified here will be the "fixed" path mtu.
  1985. * Note that if the spp_address field is empty
  1986. * then all associations on this address will
  1987. * have this fixed path mtu set upon them.
  1988. *
  1989. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1990. * the number of milliseconds that sacks will be delayed
  1991. * for. This value will apply to all addresses of an
  1992. * association if the spp_address field is empty. Note
  1993. * also, that if delayed sack is enabled and this
  1994. * value is set to 0, no change is made to the last
  1995. * recorded delayed sack timer value.
  1996. *
  1997. * spp_flags - These flags are used to control various features
  1998. * on an association. The flag field may contain
  1999. * zero or more of the following options.
  2000. *
  2001. * SPP_HB_ENABLE - Enable heartbeats on the
  2002. * specified address. Note that if the address
  2003. * field is empty all addresses for the association
  2004. * have heartbeats enabled upon them.
  2005. *
  2006. * SPP_HB_DISABLE - Disable heartbeats on the
  2007. * speicifed address. Note that if the address
  2008. * field is empty all addresses for the association
  2009. * will have their heartbeats disabled. Note also
  2010. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  2011. * mutually exclusive, only one of these two should
  2012. * be specified. Enabling both fields will have
  2013. * undetermined results.
  2014. *
  2015. * SPP_HB_DEMAND - Request a user initiated heartbeat
  2016. * to be made immediately.
  2017. *
  2018. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  2019. * heartbeat delayis to be set to the value of 0
  2020. * milliseconds.
  2021. *
  2022. * SPP_PMTUD_ENABLE - This field will enable PMTU
  2023. * discovery upon the specified address. Note that
  2024. * if the address feild is empty then all addresses
  2025. * on the association are effected.
  2026. *
  2027. * SPP_PMTUD_DISABLE - This field will disable PMTU
  2028. * discovery upon the specified address. Note that
  2029. * if the address feild is empty then all addresses
  2030. * on the association are effected. Not also that
  2031. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  2032. * exclusive. Enabling both will have undetermined
  2033. * results.
  2034. *
  2035. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  2036. * on delayed sack. The time specified in spp_sackdelay
  2037. * is used to specify the sack delay for this address. Note
  2038. * that if spp_address is empty then all addresses will
  2039. * enable delayed sack and take on the sack delay
  2040. * value specified in spp_sackdelay.
  2041. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  2042. * off delayed sack. If the spp_address field is blank then
  2043. * delayed sack is disabled for the entire association. Note
  2044. * also that this field is mutually exclusive to
  2045. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  2046. * results.
  2047. *
  2048. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  2049. * setting of the IPV6 flow label value. The value is
  2050. * contained in the spp_ipv6_flowlabel field.
  2051. * Upon retrieval, this flag will be set to indicate that
  2052. * the spp_ipv6_flowlabel field has a valid value returned.
  2053. * If a specific destination address is set (in the
  2054. * spp_address field), then the value returned is that of
  2055. * the address. If just an association is specified (and
  2056. * no address), then the association's default flow label
  2057. * is returned. If neither an association nor a destination
  2058. * is specified, then the socket's default flow label is
  2059. * returned. For non-IPv6 sockets, this flag will be left
  2060. * cleared.
  2061. *
  2062. * SPP_DSCP: Setting this flag enables the setting of the
  2063. * Differentiated Services Code Point (DSCP) value
  2064. * associated with either the association or a specific
  2065. * address. The value is obtained in the spp_dscp field.
  2066. * Upon retrieval, this flag will be set to indicate that
  2067. * the spp_dscp field has a valid value returned. If a
  2068. * specific destination address is set when called (in the
  2069. * spp_address field), then that specific destination
  2070. * address's DSCP value is returned. If just an association
  2071. * is specified, then the association's default DSCP is
  2072. * returned. If neither an association nor a destination is
  2073. * specified, then the socket's default DSCP is returned.
  2074. *
  2075. * spp_ipv6_flowlabel
  2076. * - This field is used in conjunction with the
  2077. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  2078. * The 20 least significant bits are used for the flow
  2079. * label. This setting has precedence over any IPv6-layer
  2080. * setting.
  2081. *
  2082. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  2083. * and contains the DSCP. The 6 most significant bits are
  2084. * used for the DSCP. This setting has precedence over any
  2085. * IPv4- or IPv6- layer setting.
  2086. */
  2087. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  2088. struct sctp_transport *trans,
  2089. struct sctp_association *asoc,
  2090. struct sctp_sock *sp,
  2091. int hb_change,
  2092. int pmtud_change,
  2093. int sackdelay_change)
  2094. {
  2095. int error;
  2096. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  2097. error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
  2098. trans->asoc, trans);
  2099. if (error)
  2100. return error;
  2101. }
  2102. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  2103. * this field is ignored. Note also that a value of zero indicates
  2104. * the current setting should be left unchanged.
  2105. */
  2106. if (params->spp_flags & SPP_HB_ENABLE) {
  2107. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  2108. * set. This lets us use 0 value when this flag
  2109. * is set.
  2110. */
  2111. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  2112. params->spp_hbinterval = 0;
  2113. if (params->spp_hbinterval ||
  2114. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  2115. if (trans) {
  2116. trans->hbinterval =
  2117. msecs_to_jiffies(params->spp_hbinterval);
  2118. } else if (asoc) {
  2119. asoc->hbinterval =
  2120. msecs_to_jiffies(params->spp_hbinterval);
  2121. } else {
  2122. sp->hbinterval = params->spp_hbinterval;
  2123. }
  2124. }
  2125. }
  2126. if (hb_change) {
  2127. if (trans) {
  2128. trans->param_flags =
  2129. (trans->param_flags & ~SPP_HB) | hb_change;
  2130. } else if (asoc) {
  2131. asoc->param_flags =
  2132. (asoc->param_flags & ~SPP_HB) | hb_change;
  2133. } else {
  2134. sp->param_flags =
  2135. (sp->param_flags & ~SPP_HB) | hb_change;
  2136. }
  2137. }
  2138. /* When Path MTU discovery is disabled the value specified here will
  2139. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2140. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2141. * effect).
  2142. */
  2143. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2144. if (trans) {
  2145. trans->pathmtu = params->spp_pathmtu;
  2146. sctp_assoc_sync_pmtu(asoc);
  2147. } else if (asoc) {
  2148. sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
  2149. } else {
  2150. sp->pathmtu = params->spp_pathmtu;
  2151. }
  2152. }
  2153. if (pmtud_change) {
  2154. if (trans) {
  2155. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2156. (params->spp_flags & SPP_PMTUD_ENABLE);
  2157. trans->param_flags =
  2158. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2159. if (update) {
  2160. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2161. sctp_assoc_sync_pmtu(asoc);
  2162. }
  2163. } else if (asoc) {
  2164. asoc->param_flags =
  2165. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2166. } else {
  2167. sp->param_flags =
  2168. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2169. }
  2170. }
  2171. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2172. * value of this field is ignored. Note also that a value of zero
  2173. * indicates the current setting should be left unchanged.
  2174. */
  2175. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2176. if (trans) {
  2177. trans->sackdelay =
  2178. msecs_to_jiffies(params->spp_sackdelay);
  2179. } else if (asoc) {
  2180. asoc->sackdelay =
  2181. msecs_to_jiffies(params->spp_sackdelay);
  2182. } else {
  2183. sp->sackdelay = params->spp_sackdelay;
  2184. }
  2185. }
  2186. if (sackdelay_change) {
  2187. if (trans) {
  2188. trans->param_flags =
  2189. (trans->param_flags & ~SPP_SACKDELAY) |
  2190. sackdelay_change;
  2191. } else if (asoc) {
  2192. asoc->param_flags =
  2193. (asoc->param_flags & ~SPP_SACKDELAY) |
  2194. sackdelay_change;
  2195. } else {
  2196. sp->param_flags =
  2197. (sp->param_flags & ~SPP_SACKDELAY) |
  2198. sackdelay_change;
  2199. }
  2200. }
  2201. /* Note that a value of zero indicates the current setting should be
  2202. left unchanged.
  2203. */
  2204. if (params->spp_pathmaxrxt) {
  2205. if (trans) {
  2206. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2207. } else if (asoc) {
  2208. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2209. } else {
  2210. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2211. }
  2212. }
  2213. if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
  2214. if (trans) {
  2215. if (trans->ipaddr.sa.sa_family == AF_INET6) {
  2216. trans->flowlabel = params->spp_ipv6_flowlabel &
  2217. SCTP_FLOWLABEL_VAL_MASK;
  2218. trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2219. }
  2220. } else if (asoc) {
  2221. struct sctp_transport *t;
  2222. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2223. transports) {
  2224. if (t->ipaddr.sa.sa_family != AF_INET6)
  2225. continue;
  2226. t->flowlabel = params->spp_ipv6_flowlabel &
  2227. SCTP_FLOWLABEL_VAL_MASK;
  2228. t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2229. }
  2230. asoc->flowlabel = params->spp_ipv6_flowlabel &
  2231. SCTP_FLOWLABEL_VAL_MASK;
  2232. asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2233. } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
  2234. sp->flowlabel = params->spp_ipv6_flowlabel &
  2235. SCTP_FLOWLABEL_VAL_MASK;
  2236. sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2237. }
  2238. }
  2239. if (params->spp_flags & SPP_DSCP) {
  2240. if (trans) {
  2241. trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2242. trans->dscp |= SCTP_DSCP_SET_MASK;
  2243. } else if (asoc) {
  2244. struct sctp_transport *t;
  2245. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2246. transports) {
  2247. t->dscp = params->spp_dscp &
  2248. SCTP_DSCP_VAL_MASK;
  2249. t->dscp |= SCTP_DSCP_SET_MASK;
  2250. }
  2251. asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2252. asoc->dscp |= SCTP_DSCP_SET_MASK;
  2253. } else {
  2254. sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2255. sp->dscp |= SCTP_DSCP_SET_MASK;
  2256. }
  2257. }
  2258. return 0;
  2259. }
  2260. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2261. char __user *optval,
  2262. unsigned int optlen)
  2263. {
  2264. struct sctp_paddrparams params;
  2265. struct sctp_transport *trans = NULL;
  2266. struct sctp_association *asoc = NULL;
  2267. struct sctp_sock *sp = sctp_sk(sk);
  2268. int error;
  2269. int hb_change, pmtud_change, sackdelay_change;
  2270. if (optlen == sizeof(params)) {
  2271. if (copy_from_user(&params, optval, optlen))
  2272. return -EFAULT;
  2273. } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
  2274. spp_ipv6_flowlabel), 4)) {
  2275. if (copy_from_user(&params, optval, optlen))
  2276. return -EFAULT;
  2277. if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
  2278. return -EINVAL;
  2279. } else {
  2280. return -EINVAL;
  2281. }
  2282. /* Validate flags and value parameters. */
  2283. hb_change = params.spp_flags & SPP_HB;
  2284. pmtud_change = params.spp_flags & SPP_PMTUD;
  2285. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  2286. if (hb_change == SPP_HB ||
  2287. pmtud_change == SPP_PMTUD ||
  2288. sackdelay_change == SPP_SACKDELAY ||
  2289. params.spp_sackdelay > 500 ||
  2290. (params.spp_pathmtu &&
  2291. params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2292. return -EINVAL;
  2293. /* If an address other than INADDR_ANY is specified, and
  2294. * no transport is found, then the request is invalid.
  2295. */
  2296. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  2297. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2298. params.spp_assoc_id);
  2299. if (!trans)
  2300. return -EINVAL;
  2301. }
  2302. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  2303. * socket is a one to many style socket, and an association
  2304. * was not found, then the id was invalid.
  2305. */
  2306. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  2307. if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
  2308. sctp_style(sk, UDP))
  2309. return -EINVAL;
  2310. /* Heartbeat demand can only be sent on a transport or
  2311. * association, but not a socket.
  2312. */
  2313. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2314. return -EINVAL;
  2315. /* Process parameters. */
  2316. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2317. hb_change, pmtud_change,
  2318. sackdelay_change);
  2319. if (error)
  2320. return error;
  2321. /* If changes are for association, also apply parameters to each
  2322. * transport.
  2323. */
  2324. if (!trans && asoc) {
  2325. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2326. transports) {
  2327. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2328. hb_change, pmtud_change,
  2329. sackdelay_change);
  2330. }
  2331. }
  2332. return 0;
  2333. }
  2334. static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
  2335. {
  2336. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
  2337. }
  2338. static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
  2339. {
  2340. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
  2341. }
  2342. static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
  2343. struct sctp_association *asoc)
  2344. {
  2345. struct sctp_transport *trans;
  2346. if (params->sack_delay) {
  2347. asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
  2348. asoc->param_flags =
  2349. sctp_spp_sackdelay_enable(asoc->param_flags);
  2350. }
  2351. if (params->sack_freq == 1) {
  2352. asoc->param_flags =
  2353. sctp_spp_sackdelay_disable(asoc->param_flags);
  2354. } else if (params->sack_freq > 1) {
  2355. asoc->sackfreq = params->sack_freq;
  2356. asoc->param_flags =
  2357. sctp_spp_sackdelay_enable(asoc->param_flags);
  2358. }
  2359. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2360. transports) {
  2361. if (params->sack_delay) {
  2362. trans->sackdelay = msecs_to_jiffies(params->sack_delay);
  2363. trans->param_flags =
  2364. sctp_spp_sackdelay_enable(trans->param_flags);
  2365. }
  2366. if (params->sack_freq == 1) {
  2367. trans->param_flags =
  2368. sctp_spp_sackdelay_disable(trans->param_flags);
  2369. } else if (params->sack_freq > 1) {
  2370. trans->sackfreq = params->sack_freq;
  2371. trans->param_flags =
  2372. sctp_spp_sackdelay_enable(trans->param_flags);
  2373. }
  2374. }
  2375. }
  2376. /*
  2377. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2378. *
  2379. * This option will effect the way delayed acks are performed. This
  2380. * option allows you to get or set the delayed ack time, in
  2381. * milliseconds. It also allows changing the delayed ack frequency.
  2382. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2383. * the assoc_id is 0, then this sets or gets the endpoints default
  2384. * values. If the assoc_id field is non-zero, then the set or get
  2385. * effects the specified association for the one to many model (the
  2386. * assoc_id field is ignored by the one to one model). Note that if
  2387. * sack_delay or sack_freq are 0 when setting this option, then the
  2388. * current values will remain unchanged.
  2389. *
  2390. * struct sctp_sack_info {
  2391. * sctp_assoc_t sack_assoc_id;
  2392. * uint32_t sack_delay;
  2393. * uint32_t sack_freq;
  2394. * };
  2395. *
  2396. * sack_assoc_id - This parameter, indicates which association the user
  2397. * is performing an action upon. Note that if this field's value is
  2398. * zero then the endpoints default value is changed (effecting future
  2399. * associations only).
  2400. *
  2401. * sack_delay - This parameter contains the number of milliseconds that
  2402. * the user is requesting the delayed ACK timer be set to. Note that
  2403. * this value is defined in the standard to be between 200 and 500
  2404. * milliseconds.
  2405. *
  2406. * sack_freq - This parameter contains the number of packets that must
  2407. * be received before a sack is sent without waiting for the delay
  2408. * timer to expire. The default value for this is 2, setting this
  2409. * value to 1 will disable the delayed sack algorithm.
  2410. */
  2411. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2412. char __user *optval, unsigned int optlen)
  2413. {
  2414. struct sctp_sock *sp = sctp_sk(sk);
  2415. struct sctp_association *asoc;
  2416. struct sctp_sack_info params;
  2417. if (optlen == sizeof(struct sctp_sack_info)) {
  2418. if (copy_from_user(&params, optval, optlen))
  2419. return -EFAULT;
  2420. if (params.sack_delay == 0 && params.sack_freq == 0)
  2421. return 0;
  2422. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2423. pr_warn_ratelimited(DEPRECATED
  2424. "%s (pid %d) "
  2425. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  2426. "Use struct sctp_sack_info instead\n",
  2427. current->comm, task_pid_nr(current));
  2428. if (copy_from_user(&params, optval, optlen))
  2429. return -EFAULT;
  2430. if (params.sack_delay == 0)
  2431. params.sack_freq = 1;
  2432. else
  2433. params.sack_freq = 0;
  2434. } else
  2435. return -EINVAL;
  2436. /* Validate value parameter. */
  2437. if (params.sack_delay > 500)
  2438. return -EINVAL;
  2439. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  2440. * socket is a one to many style socket, and an association
  2441. * was not found, then the id was invalid.
  2442. */
  2443. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  2444. if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
  2445. sctp_style(sk, UDP))
  2446. return -EINVAL;
  2447. if (asoc) {
  2448. sctp_apply_asoc_delayed_ack(&params, asoc);
  2449. return 0;
  2450. }
  2451. if (sctp_style(sk, TCP))
  2452. params.sack_assoc_id = SCTP_FUTURE_ASSOC;
  2453. if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
  2454. params.sack_assoc_id == SCTP_ALL_ASSOC) {
  2455. if (params.sack_delay) {
  2456. sp->sackdelay = params.sack_delay;
  2457. sp->param_flags =
  2458. sctp_spp_sackdelay_enable(sp->param_flags);
  2459. }
  2460. if (params.sack_freq == 1) {
  2461. sp->param_flags =
  2462. sctp_spp_sackdelay_disable(sp->param_flags);
  2463. } else if (params.sack_freq > 1) {
  2464. sp->sackfreq = params.sack_freq;
  2465. sp->param_flags =
  2466. sctp_spp_sackdelay_enable(sp->param_flags);
  2467. }
  2468. }
  2469. if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
  2470. params.sack_assoc_id == SCTP_ALL_ASSOC)
  2471. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2472. sctp_apply_asoc_delayed_ack(&params, asoc);
  2473. return 0;
  2474. }
  2475. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2476. *
  2477. * Applications can specify protocol parameters for the default association
  2478. * initialization. The option name argument to setsockopt() and getsockopt()
  2479. * is SCTP_INITMSG.
  2480. *
  2481. * Setting initialization parameters is effective only on an unconnected
  2482. * socket (for UDP-style sockets only future associations are effected
  2483. * by the change). With TCP-style sockets, this option is inherited by
  2484. * sockets derived from a listener socket.
  2485. */
  2486. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
  2487. {
  2488. struct sctp_initmsg sinit;
  2489. struct sctp_sock *sp = sctp_sk(sk);
  2490. if (optlen != sizeof(struct sctp_initmsg))
  2491. return -EINVAL;
  2492. if (copy_from_user(&sinit, optval, optlen))
  2493. return -EFAULT;
  2494. if (sinit.sinit_num_ostreams)
  2495. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2496. if (sinit.sinit_max_instreams)
  2497. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2498. if (sinit.sinit_max_attempts)
  2499. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2500. if (sinit.sinit_max_init_timeo)
  2501. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2502. return 0;
  2503. }
  2504. /*
  2505. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2506. *
  2507. * Applications that wish to use the sendto() system call may wish to
  2508. * specify a default set of parameters that would normally be supplied
  2509. * through the inclusion of ancillary data. This socket option allows
  2510. * such an application to set the default sctp_sndrcvinfo structure.
  2511. * The application that wishes to use this socket option simply passes
  2512. * in to this call the sctp_sndrcvinfo structure defined in Section
  2513. * 5.2.2) The input parameters accepted by this call include
  2514. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2515. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2516. * to this call if the caller is using the UDP model.
  2517. */
  2518. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2519. char __user *optval,
  2520. unsigned int optlen)
  2521. {
  2522. struct sctp_sock *sp = sctp_sk(sk);
  2523. struct sctp_association *asoc;
  2524. struct sctp_sndrcvinfo info;
  2525. if (optlen != sizeof(info))
  2526. return -EINVAL;
  2527. if (copy_from_user(&info, optval, optlen))
  2528. return -EFAULT;
  2529. if (info.sinfo_flags &
  2530. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2531. SCTP_ABORT | SCTP_EOF))
  2532. return -EINVAL;
  2533. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2534. if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
  2535. sctp_style(sk, UDP))
  2536. return -EINVAL;
  2537. if (asoc) {
  2538. asoc->default_stream = info.sinfo_stream;
  2539. asoc->default_flags = info.sinfo_flags;
  2540. asoc->default_ppid = info.sinfo_ppid;
  2541. asoc->default_context = info.sinfo_context;
  2542. asoc->default_timetolive = info.sinfo_timetolive;
  2543. return 0;
  2544. }
  2545. if (sctp_style(sk, TCP))
  2546. info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
  2547. if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
  2548. info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2549. sp->default_stream = info.sinfo_stream;
  2550. sp->default_flags = info.sinfo_flags;
  2551. sp->default_ppid = info.sinfo_ppid;
  2552. sp->default_context = info.sinfo_context;
  2553. sp->default_timetolive = info.sinfo_timetolive;
  2554. }
  2555. if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
  2556. info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2557. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2558. asoc->default_stream = info.sinfo_stream;
  2559. asoc->default_flags = info.sinfo_flags;
  2560. asoc->default_ppid = info.sinfo_ppid;
  2561. asoc->default_context = info.sinfo_context;
  2562. asoc->default_timetolive = info.sinfo_timetolive;
  2563. }
  2564. }
  2565. return 0;
  2566. }
  2567. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  2568. * (SCTP_DEFAULT_SNDINFO)
  2569. */
  2570. static int sctp_setsockopt_default_sndinfo(struct sock *sk,
  2571. char __user *optval,
  2572. unsigned int optlen)
  2573. {
  2574. struct sctp_sock *sp = sctp_sk(sk);
  2575. struct sctp_association *asoc;
  2576. struct sctp_sndinfo info;
  2577. if (optlen != sizeof(info))
  2578. return -EINVAL;
  2579. if (copy_from_user(&info, optval, optlen))
  2580. return -EFAULT;
  2581. if (info.snd_flags &
  2582. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2583. SCTP_ABORT | SCTP_EOF))
  2584. return -EINVAL;
  2585. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  2586. if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
  2587. sctp_style(sk, UDP))
  2588. return -EINVAL;
  2589. if (asoc) {
  2590. asoc->default_stream = info.snd_sid;
  2591. asoc->default_flags = info.snd_flags;
  2592. asoc->default_ppid = info.snd_ppid;
  2593. asoc->default_context = info.snd_context;
  2594. return 0;
  2595. }
  2596. if (sctp_style(sk, TCP))
  2597. info.snd_assoc_id = SCTP_FUTURE_ASSOC;
  2598. if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
  2599. info.snd_assoc_id == SCTP_ALL_ASSOC) {
  2600. sp->default_stream = info.snd_sid;
  2601. sp->default_flags = info.snd_flags;
  2602. sp->default_ppid = info.snd_ppid;
  2603. sp->default_context = info.snd_context;
  2604. }
  2605. if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
  2606. info.snd_assoc_id == SCTP_ALL_ASSOC) {
  2607. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2608. asoc->default_stream = info.snd_sid;
  2609. asoc->default_flags = info.snd_flags;
  2610. asoc->default_ppid = info.snd_ppid;
  2611. asoc->default_context = info.snd_context;
  2612. }
  2613. }
  2614. return 0;
  2615. }
  2616. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2617. *
  2618. * Requests that the local SCTP stack use the enclosed peer address as
  2619. * the association primary. The enclosed address must be one of the
  2620. * association peer's addresses.
  2621. */
  2622. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2623. unsigned int optlen)
  2624. {
  2625. struct sctp_prim prim;
  2626. struct sctp_transport *trans;
  2627. struct sctp_af *af;
  2628. int err;
  2629. if (optlen != sizeof(struct sctp_prim))
  2630. return -EINVAL;
  2631. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2632. return -EFAULT;
  2633. /* Allow security module to validate address but need address len. */
  2634. af = sctp_get_af_specific(prim.ssp_addr.ss_family);
  2635. if (!af)
  2636. return -EINVAL;
  2637. err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
  2638. (struct sockaddr *)&prim.ssp_addr,
  2639. af->sockaddr_len);
  2640. if (err)
  2641. return err;
  2642. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2643. if (!trans)
  2644. return -EINVAL;
  2645. sctp_assoc_set_primary(trans->asoc, trans);
  2646. return 0;
  2647. }
  2648. /*
  2649. * 7.1.5 SCTP_NODELAY
  2650. *
  2651. * Turn on/off any Nagle-like algorithm. This means that packets are
  2652. * generally sent as soon as possible and no unnecessary delays are
  2653. * introduced, at the cost of more packets in the network. Expects an
  2654. * integer boolean flag.
  2655. */
  2656. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2657. unsigned int optlen)
  2658. {
  2659. int val;
  2660. if (optlen < sizeof(int))
  2661. return -EINVAL;
  2662. if (get_user(val, (int __user *)optval))
  2663. return -EFAULT;
  2664. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2665. return 0;
  2666. }
  2667. /*
  2668. *
  2669. * 7.1.1 SCTP_RTOINFO
  2670. *
  2671. * The protocol parameters used to initialize and bound retransmission
  2672. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2673. * and modify these parameters.
  2674. * All parameters are time values, in milliseconds. A value of 0, when
  2675. * modifying the parameters, indicates that the current value should not
  2676. * be changed.
  2677. *
  2678. */
  2679. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2680. {
  2681. struct sctp_rtoinfo rtoinfo;
  2682. struct sctp_association *asoc;
  2683. unsigned long rto_min, rto_max;
  2684. struct sctp_sock *sp = sctp_sk(sk);
  2685. if (optlen != sizeof (struct sctp_rtoinfo))
  2686. return -EINVAL;
  2687. if (copy_from_user(&rtoinfo, optval, optlen))
  2688. return -EFAULT;
  2689. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2690. /* Set the values to the specific association */
  2691. if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
  2692. sctp_style(sk, UDP))
  2693. return -EINVAL;
  2694. rto_max = rtoinfo.srto_max;
  2695. rto_min = rtoinfo.srto_min;
  2696. if (rto_max)
  2697. rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
  2698. else
  2699. rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
  2700. if (rto_min)
  2701. rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
  2702. else
  2703. rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
  2704. if (rto_min > rto_max)
  2705. return -EINVAL;
  2706. if (asoc) {
  2707. if (rtoinfo.srto_initial != 0)
  2708. asoc->rto_initial =
  2709. msecs_to_jiffies(rtoinfo.srto_initial);
  2710. asoc->rto_max = rto_max;
  2711. asoc->rto_min = rto_min;
  2712. } else {
  2713. /* If there is no association or the association-id = 0
  2714. * set the values to the endpoint.
  2715. */
  2716. if (rtoinfo.srto_initial != 0)
  2717. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2718. sp->rtoinfo.srto_max = rto_max;
  2719. sp->rtoinfo.srto_min = rto_min;
  2720. }
  2721. return 0;
  2722. }
  2723. /*
  2724. *
  2725. * 7.1.2 SCTP_ASSOCINFO
  2726. *
  2727. * This option is used to tune the maximum retransmission attempts
  2728. * of the association.
  2729. * Returns an error if the new association retransmission value is
  2730. * greater than the sum of the retransmission value of the peer.
  2731. * See [SCTP] for more information.
  2732. *
  2733. */
  2734. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2735. {
  2736. struct sctp_assocparams assocparams;
  2737. struct sctp_association *asoc;
  2738. if (optlen != sizeof(struct sctp_assocparams))
  2739. return -EINVAL;
  2740. if (copy_from_user(&assocparams, optval, optlen))
  2741. return -EFAULT;
  2742. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2743. if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  2744. sctp_style(sk, UDP))
  2745. return -EINVAL;
  2746. /* Set the values to the specific association */
  2747. if (asoc) {
  2748. if (assocparams.sasoc_asocmaxrxt != 0) {
  2749. __u32 path_sum = 0;
  2750. int paths = 0;
  2751. struct sctp_transport *peer_addr;
  2752. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2753. transports) {
  2754. path_sum += peer_addr->pathmaxrxt;
  2755. paths++;
  2756. }
  2757. /* Only validate asocmaxrxt if we have more than
  2758. * one path/transport. We do this because path
  2759. * retransmissions are only counted when we have more
  2760. * then one path.
  2761. */
  2762. if (paths > 1 &&
  2763. assocparams.sasoc_asocmaxrxt > path_sum)
  2764. return -EINVAL;
  2765. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2766. }
  2767. if (assocparams.sasoc_cookie_life != 0)
  2768. asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
  2769. } else {
  2770. /* Set the values to the endpoint */
  2771. struct sctp_sock *sp = sctp_sk(sk);
  2772. if (assocparams.sasoc_asocmaxrxt != 0)
  2773. sp->assocparams.sasoc_asocmaxrxt =
  2774. assocparams.sasoc_asocmaxrxt;
  2775. if (assocparams.sasoc_cookie_life != 0)
  2776. sp->assocparams.sasoc_cookie_life =
  2777. assocparams.sasoc_cookie_life;
  2778. }
  2779. return 0;
  2780. }
  2781. /*
  2782. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2783. *
  2784. * This socket option is a boolean flag which turns on or off mapped V4
  2785. * addresses. If this option is turned on and the socket is type
  2786. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2787. * If this option is turned off, then no mapping will be done of V4
  2788. * addresses and a user will receive both PF_INET6 and PF_INET type
  2789. * addresses on the socket.
  2790. */
  2791. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
  2792. {
  2793. int val;
  2794. struct sctp_sock *sp = sctp_sk(sk);
  2795. if (optlen < sizeof(int))
  2796. return -EINVAL;
  2797. if (get_user(val, (int __user *)optval))
  2798. return -EFAULT;
  2799. if (val)
  2800. sp->v4mapped = 1;
  2801. else
  2802. sp->v4mapped = 0;
  2803. return 0;
  2804. }
  2805. /*
  2806. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2807. * This option will get or set the maximum size to put in any outgoing
  2808. * SCTP DATA chunk. If a message is larger than this size it will be
  2809. * fragmented by SCTP into the specified size. Note that the underlying
  2810. * SCTP implementation may fragment into smaller sized chunks when the
  2811. * PMTU of the underlying association is smaller than the value set by
  2812. * the user. The default value for this option is '0' which indicates
  2813. * the user is NOT limiting fragmentation and only the PMTU will effect
  2814. * SCTP's choice of DATA chunk size. Note also that values set larger
  2815. * than the maximum size of an IP datagram will effectively let SCTP
  2816. * control fragmentation (i.e. the same as setting this option to 0).
  2817. *
  2818. * The following structure is used to access and modify this parameter:
  2819. *
  2820. * struct sctp_assoc_value {
  2821. * sctp_assoc_t assoc_id;
  2822. * uint32_t assoc_value;
  2823. * };
  2824. *
  2825. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2826. * For one-to-many style sockets this parameter indicates which
  2827. * association the user is performing an action upon. Note that if
  2828. * this field's value is zero then the endpoints default value is
  2829. * changed (effecting future associations only).
  2830. * assoc_value: This parameter specifies the maximum size in bytes.
  2831. */
  2832. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
  2833. {
  2834. struct sctp_sock *sp = sctp_sk(sk);
  2835. struct sctp_assoc_value params;
  2836. struct sctp_association *asoc;
  2837. int val;
  2838. if (optlen == sizeof(int)) {
  2839. pr_warn_ratelimited(DEPRECATED
  2840. "%s (pid %d) "
  2841. "Use of int in maxseg socket option.\n"
  2842. "Use struct sctp_assoc_value instead\n",
  2843. current->comm, task_pid_nr(current));
  2844. if (copy_from_user(&val, optval, optlen))
  2845. return -EFAULT;
  2846. params.assoc_id = SCTP_FUTURE_ASSOC;
  2847. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2848. if (copy_from_user(&params, optval, optlen))
  2849. return -EFAULT;
  2850. val = params.assoc_value;
  2851. } else {
  2852. return -EINVAL;
  2853. }
  2854. asoc = sctp_id2assoc(sk, params.assoc_id);
  2855. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  2856. sctp_style(sk, UDP))
  2857. return -EINVAL;
  2858. if (val) {
  2859. int min_len, max_len;
  2860. __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
  2861. sizeof(struct sctp_data_chunk);
  2862. min_len = sctp_min_frag_point(sp, datasize);
  2863. max_len = SCTP_MAX_CHUNK_LEN - datasize;
  2864. if (val < min_len || val > max_len)
  2865. return -EINVAL;
  2866. }
  2867. if (asoc) {
  2868. asoc->user_frag = val;
  2869. sctp_assoc_update_frag_point(asoc);
  2870. } else {
  2871. sp->user_frag = val;
  2872. }
  2873. return 0;
  2874. }
  2875. /*
  2876. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2877. *
  2878. * Requests that the peer mark the enclosed address as the association
  2879. * primary. The enclosed address must be one of the association's
  2880. * locally bound addresses. The following structure is used to make a
  2881. * set primary request:
  2882. */
  2883. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2884. unsigned int optlen)
  2885. {
  2886. struct sctp_sock *sp;
  2887. struct sctp_association *asoc = NULL;
  2888. struct sctp_setpeerprim prim;
  2889. struct sctp_chunk *chunk;
  2890. struct sctp_af *af;
  2891. int err;
  2892. sp = sctp_sk(sk);
  2893. if (!sp->ep->asconf_enable)
  2894. return -EPERM;
  2895. if (optlen != sizeof(struct sctp_setpeerprim))
  2896. return -EINVAL;
  2897. if (copy_from_user(&prim, optval, optlen))
  2898. return -EFAULT;
  2899. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2900. if (!asoc)
  2901. return -EINVAL;
  2902. if (!asoc->peer.asconf_capable)
  2903. return -EPERM;
  2904. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2905. return -EPERM;
  2906. if (!sctp_state(asoc, ESTABLISHED))
  2907. return -ENOTCONN;
  2908. af = sctp_get_af_specific(prim.sspp_addr.ss_family);
  2909. if (!af)
  2910. return -EINVAL;
  2911. if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
  2912. return -EADDRNOTAVAIL;
  2913. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2914. return -EADDRNOTAVAIL;
  2915. /* Allow security module to validate address. */
  2916. err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
  2917. (struct sockaddr *)&prim.sspp_addr,
  2918. af->sockaddr_len);
  2919. if (err)
  2920. return err;
  2921. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2922. chunk = sctp_make_asconf_set_prim(asoc,
  2923. (union sctp_addr *)&prim.sspp_addr);
  2924. if (!chunk)
  2925. return -ENOMEM;
  2926. err = sctp_send_asconf(asoc, chunk);
  2927. pr_debug("%s: we set peer primary addr primitively\n", __func__);
  2928. return err;
  2929. }
  2930. static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
  2931. unsigned int optlen)
  2932. {
  2933. struct sctp_setadaptation adaptation;
  2934. if (optlen != sizeof(struct sctp_setadaptation))
  2935. return -EINVAL;
  2936. if (copy_from_user(&adaptation, optval, optlen))
  2937. return -EFAULT;
  2938. sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
  2939. return 0;
  2940. }
  2941. /*
  2942. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2943. *
  2944. * The context field in the sctp_sndrcvinfo structure is normally only
  2945. * used when a failed message is retrieved holding the value that was
  2946. * sent down on the actual send call. This option allows the setting of
  2947. * a default context on an association basis that will be received on
  2948. * reading messages from the peer. This is especially helpful in the
  2949. * one-2-many model for an application to keep some reference to an
  2950. * internal state machine that is processing messages on the
  2951. * association. Note that the setting of this value only effects
  2952. * received messages from the peer and does not effect the value that is
  2953. * saved with outbound messages.
  2954. */
  2955. static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
  2956. unsigned int optlen)
  2957. {
  2958. struct sctp_sock *sp = sctp_sk(sk);
  2959. struct sctp_assoc_value params;
  2960. struct sctp_association *asoc;
  2961. if (optlen != sizeof(struct sctp_assoc_value))
  2962. return -EINVAL;
  2963. if (copy_from_user(&params, optval, optlen))
  2964. return -EFAULT;
  2965. asoc = sctp_id2assoc(sk, params.assoc_id);
  2966. if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
  2967. sctp_style(sk, UDP))
  2968. return -EINVAL;
  2969. if (asoc) {
  2970. asoc->default_rcv_context = params.assoc_value;
  2971. return 0;
  2972. }
  2973. if (sctp_style(sk, TCP))
  2974. params.assoc_id = SCTP_FUTURE_ASSOC;
  2975. if (params.assoc_id == SCTP_FUTURE_ASSOC ||
  2976. params.assoc_id == SCTP_ALL_ASSOC)
  2977. sp->default_rcv_context = params.assoc_value;
  2978. if (params.assoc_id == SCTP_CURRENT_ASSOC ||
  2979. params.assoc_id == SCTP_ALL_ASSOC)
  2980. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2981. asoc->default_rcv_context = params.assoc_value;
  2982. return 0;
  2983. }
  2984. /*
  2985. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2986. *
  2987. * This options will at a minimum specify if the implementation is doing
  2988. * fragmented interleave. Fragmented interleave, for a one to many
  2989. * socket, is when subsequent calls to receive a message may return
  2990. * parts of messages from different associations. Some implementations
  2991. * may allow you to turn this value on or off. If so, when turned off,
  2992. * no fragment interleave will occur (which will cause a head of line
  2993. * blocking amongst multiple associations sharing the same one to many
  2994. * socket). When this option is turned on, then each receive call may
  2995. * come from a different association (thus the user must receive data
  2996. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2997. * association each receive belongs to.
  2998. *
  2999. * This option takes a boolean value. A non-zero value indicates that
  3000. * fragmented interleave is on. A value of zero indicates that
  3001. * fragmented interleave is off.
  3002. *
  3003. * Note that it is important that an implementation that allows this
  3004. * option to be turned on, have it off by default. Otherwise an unaware
  3005. * application using the one to many model may become confused and act
  3006. * incorrectly.
  3007. */
  3008. static int sctp_setsockopt_fragment_interleave(struct sock *sk,
  3009. char __user *optval,
  3010. unsigned int optlen)
  3011. {
  3012. int val;
  3013. if (optlen != sizeof(int))
  3014. return -EINVAL;
  3015. if (get_user(val, (int __user *)optval))
  3016. return -EFAULT;
  3017. sctp_sk(sk)->frag_interleave = !!val;
  3018. if (!sctp_sk(sk)->frag_interleave)
  3019. sctp_sk(sk)->ep->intl_enable = 0;
  3020. return 0;
  3021. }
  3022. /*
  3023. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  3024. * (SCTP_PARTIAL_DELIVERY_POINT)
  3025. *
  3026. * This option will set or get the SCTP partial delivery point. This
  3027. * point is the size of a message where the partial delivery API will be
  3028. * invoked to help free up rwnd space for the peer. Setting this to a
  3029. * lower value will cause partial deliveries to happen more often. The
  3030. * calls argument is an integer that sets or gets the partial delivery
  3031. * point. Note also that the call will fail if the user attempts to set
  3032. * this value larger than the socket receive buffer size.
  3033. *
  3034. * Note that any single message having a length smaller than or equal to
  3035. * the SCTP partial delivery point will be delivered in one single read
  3036. * call as long as the user provided buffer is large enough to hold the
  3037. * message.
  3038. */
  3039. static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
  3040. char __user *optval,
  3041. unsigned int optlen)
  3042. {
  3043. u32 val;
  3044. if (optlen != sizeof(u32))
  3045. return -EINVAL;
  3046. if (get_user(val, (int __user *)optval))
  3047. return -EFAULT;
  3048. /* Note: We double the receive buffer from what the user sets
  3049. * it to be, also initial rwnd is based on rcvbuf/2.
  3050. */
  3051. if (val > (sk->sk_rcvbuf >> 1))
  3052. return -EINVAL;
  3053. sctp_sk(sk)->pd_point = val;
  3054. return 0; /* is this the right error code? */
  3055. }
  3056. /*
  3057. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  3058. *
  3059. * This option will allow a user to change the maximum burst of packets
  3060. * that can be emitted by this association. Note that the default value
  3061. * is 4, and some implementations may restrict this setting so that it
  3062. * can only be lowered.
  3063. *
  3064. * NOTE: This text doesn't seem right. Do this on a socket basis with
  3065. * future associations inheriting the socket value.
  3066. */
  3067. static int sctp_setsockopt_maxburst(struct sock *sk,
  3068. char __user *optval,
  3069. unsigned int optlen)
  3070. {
  3071. struct sctp_sock *sp = sctp_sk(sk);
  3072. struct sctp_assoc_value params;
  3073. struct sctp_association *asoc;
  3074. if (optlen == sizeof(int)) {
  3075. pr_warn_ratelimited(DEPRECATED
  3076. "%s (pid %d) "
  3077. "Use of int in max_burst socket option deprecated.\n"
  3078. "Use struct sctp_assoc_value instead\n",
  3079. current->comm, task_pid_nr(current));
  3080. if (copy_from_user(&params.assoc_value, optval, optlen))
  3081. return -EFAULT;
  3082. params.assoc_id = SCTP_FUTURE_ASSOC;
  3083. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  3084. if (copy_from_user(&params, optval, optlen))
  3085. return -EFAULT;
  3086. } else
  3087. return -EINVAL;
  3088. asoc = sctp_id2assoc(sk, params.assoc_id);
  3089. if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
  3090. sctp_style(sk, UDP))
  3091. return -EINVAL;
  3092. if (asoc) {
  3093. asoc->max_burst = params.assoc_value;
  3094. return 0;
  3095. }
  3096. if (sctp_style(sk, TCP))
  3097. params.assoc_id = SCTP_FUTURE_ASSOC;
  3098. if (params.assoc_id == SCTP_FUTURE_ASSOC ||
  3099. params.assoc_id == SCTP_ALL_ASSOC)
  3100. sp->max_burst = params.assoc_value;
  3101. if (params.assoc_id == SCTP_CURRENT_ASSOC ||
  3102. params.assoc_id == SCTP_ALL_ASSOC)
  3103. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  3104. asoc->max_burst = params.assoc_value;
  3105. return 0;
  3106. }
  3107. /*
  3108. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  3109. *
  3110. * This set option adds a chunk type that the user is requesting to be
  3111. * received only in an authenticated way. Changes to the list of chunks
  3112. * will only effect future associations on the socket.
  3113. */
  3114. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  3115. char __user *optval,
  3116. unsigned int optlen)
  3117. {
  3118. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3119. struct sctp_authchunk val;
  3120. if (!ep->auth_enable)
  3121. return -EACCES;
  3122. if (optlen != sizeof(struct sctp_authchunk))
  3123. return -EINVAL;
  3124. if (copy_from_user(&val, optval, optlen))
  3125. return -EFAULT;
  3126. switch (val.sauth_chunk) {
  3127. case SCTP_CID_INIT:
  3128. case SCTP_CID_INIT_ACK:
  3129. case SCTP_CID_SHUTDOWN_COMPLETE:
  3130. case SCTP_CID_AUTH:
  3131. return -EINVAL;
  3132. }
  3133. /* add this chunk id to the endpoint */
  3134. return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
  3135. }
  3136. /*
  3137. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  3138. *
  3139. * This option gets or sets the list of HMAC algorithms that the local
  3140. * endpoint requires the peer to use.
  3141. */
  3142. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  3143. char __user *optval,
  3144. unsigned int optlen)
  3145. {
  3146. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3147. struct sctp_hmacalgo *hmacs;
  3148. u32 idents;
  3149. int err;
  3150. if (!ep->auth_enable)
  3151. return -EACCES;
  3152. if (optlen < sizeof(struct sctp_hmacalgo))
  3153. return -EINVAL;
  3154. optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
  3155. SCTP_AUTH_NUM_HMACS * sizeof(u16));
  3156. hmacs = memdup_user(optval, optlen);
  3157. if (IS_ERR(hmacs))
  3158. return PTR_ERR(hmacs);
  3159. idents = hmacs->shmac_num_idents;
  3160. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  3161. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
  3162. err = -EINVAL;
  3163. goto out;
  3164. }
  3165. err = sctp_auth_ep_set_hmacs(ep, hmacs);
  3166. out:
  3167. kfree(hmacs);
  3168. return err;
  3169. }
  3170. /*
  3171. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  3172. *
  3173. * This option will set a shared secret key which is used to build an
  3174. * association shared key.
  3175. */
  3176. static int sctp_setsockopt_auth_key(struct sock *sk,
  3177. char __user *optval,
  3178. unsigned int optlen)
  3179. {
  3180. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3181. struct sctp_authkey *authkey;
  3182. struct sctp_association *asoc;
  3183. int ret = -EINVAL;
  3184. if (optlen <= sizeof(struct sctp_authkey))
  3185. return -EINVAL;
  3186. /* authkey->sca_keylength is u16, so optlen can't be bigger than
  3187. * this.
  3188. */
  3189. optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
  3190. authkey = memdup_user(optval, optlen);
  3191. if (IS_ERR(authkey))
  3192. return PTR_ERR(authkey);
  3193. if (authkey->sca_keylength > optlen - sizeof(*authkey))
  3194. goto out;
  3195. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  3196. if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
  3197. sctp_style(sk, UDP))
  3198. goto out;
  3199. if (asoc) {
  3200. ret = sctp_auth_set_key(ep, asoc, authkey);
  3201. goto out;
  3202. }
  3203. if (sctp_style(sk, TCP))
  3204. authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
  3205. if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
  3206. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3207. ret = sctp_auth_set_key(ep, asoc, authkey);
  3208. if (ret)
  3209. goto out;
  3210. }
  3211. ret = 0;
  3212. if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
  3213. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3214. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3215. int res = sctp_auth_set_key(ep, asoc, authkey);
  3216. if (res && !ret)
  3217. ret = res;
  3218. }
  3219. }
  3220. out:
  3221. kzfree(authkey);
  3222. return ret;
  3223. }
  3224. /*
  3225. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  3226. *
  3227. * This option will get or set the active shared key to be used to build
  3228. * the association shared key.
  3229. */
  3230. static int sctp_setsockopt_active_key(struct sock *sk,
  3231. char __user *optval,
  3232. unsigned int optlen)
  3233. {
  3234. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3235. struct sctp_association *asoc;
  3236. struct sctp_authkeyid val;
  3237. int ret = 0;
  3238. if (optlen != sizeof(struct sctp_authkeyid))
  3239. return -EINVAL;
  3240. if (copy_from_user(&val, optval, optlen))
  3241. return -EFAULT;
  3242. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  3243. if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
  3244. sctp_style(sk, UDP))
  3245. return -EINVAL;
  3246. if (asoc)
  3247. return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
  3248. if (sctp_style(sk, TCP))
  3249. val.scact_assoc_id = SCTP_FUTURE_ASSOC;
  3250. if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3251. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3252. ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
  3253. if (ret)
  3254. return ret;
  3255. }
  3256. if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3257. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3258. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3259. int res = sctp_auth_set_active_key(ep, asoc,
  3260. val.scact_keynumber);
  3261. if (res && !ret)
  3262. ret = res;
  3263. }
  3264. }
  3265. return ret;
  3266. }
  3267. /*
  3268. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  3269. *
  3270. * This set option will delete a shared secret key from use.
  3271. */
  3272. static int sctp_setsockopt_del_key(struct sock *sk,
  3273. char __user *optval,
  3274. unsigned int optlen)
  3275. {
  3276. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3277. struct sctp_association *asoc;
  3278. struct sctp_authkeyid val;
  3279. int ret = 0;
  3280. if (optlen != sizeof(struct sctp_authkeyid))
  3281. return -EINVAL;
  3282. if (copy_from_user(&val, optval, optlen))
  3283. return -EFAULT;
  3284. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  3285. if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
  3286. sctp_style(sk, UDP))
  3287. return -EINVAL;
  3288. if (asoc)
  3289. return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
  3290. if (sctp_style(sk, TCP))
  3291. val.scact_assoc_id = SCTP_FUTURE_ASSOC;
  3292. if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3293. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3294. ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
  3295. if (ret)
  3296. return ret;
  3297. }
  3298. if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3299. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3300. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3301. int res = sctp_auth_del_key_id(ep, asoc,
  3302. val.scact_keynumber);
  3303. if (res && !ret)
  3304. ret = res;
  3305. }
  3306. }
  3307. return ret;
  3308. }
  3309. /*
  3310. * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
  3311. *
  3312. * This set option will deactivate a shared secret key.
  3313. */
  3314. static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
  3315. unsigned int optlen)
  3316. {
  3317. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3318. struct sctp_association *asoc;
  3319. struct sctp_authkeyid val;
  3320. int ret = 0;
  3321. if (optlen != sizeof(struct sctp_authkeyid))
  3322. return -EINVAL;
  3323. if (copy_from_user(&val, optval, optlen))
  3324. return -EFAULT;
  3325. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  3326. if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
  3327. sctp_style(sk, UDP))
  3328. return -EINVAL;
  3329. if (asoc)
  3330. return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
  3331. if (sctp_style(sk, TCP))
  3332. val.scact_assoc_id = SCTP_FUTURE_ASSOC;
  3333. if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3334. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3335. ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
  3336. if (ret)
  3337. return ret;
  3338. }
  3339. if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3340. val.scact_assoc_id == SCTP_ALL_ASSOC) {
  3341. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3342. int res = sctp_auth_deact_key_id(ep, asoc,
  3343. val.scact_keynumber);
  3344. if (res && !ret)
  3345. ret = res;
  3346. }
  3347. }
  3348. return ret;
  3349. }
  3350. /*
  3351. * 8.1.23 SCTP_AUTO_ASCONF
  3352. *
  3353. * This option will enable or disable the use of the automatic generation of
  3354. * ASCONF chunks to add and delete addresses to an existing association. Note
  3355. * that this option has two caveats namely: a) it only affects sockets that
  3356. * are bound to all addresses available to the SCTP stack, and b) the system
  3357. * administrator may have an overriding control that turns the ASCONF feature
  3358. * off no matter what setting the socket option may have.
  3359. * This option expects an integer boolean flag, where a non-zero value turns on
  3360. * the option, and a zero value turns off the option.
  3361. * Note. In this implementation, socket operation overrides default parameter
  3362. * being set by sysctl as well as FreeBSD implementation
  3363. */
  3364. static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
  3365. unsigned int optlen)
  3366. {
  3367. int val;
  3368. struct sctp_sock *sp = sctp_sk(sk);
  3369. if (optlen < sizeof(int))
  3370. return -EINVAL;
  3371. if (get_user(val, (int __user *)optval))
  3372. return -EFAULT;
  3373. if (!sctp_is_ep_boundall(sk) && val)
  3374. return -EINVAL;
  3375. if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
  3376. return 0;
  3377. spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3378. if (val == 0 && sp->do_auto_asconf) {
  3379. list_del(&sp->auto_asconf_list);
  3380. sp->do_auto_asconf = 0;
  3381. } else if (val && !sp->do_auto_asconf) {
  3382. list_add_tail(&sp->auto_asconf_list,
  3383. &sock_net(sk)->sctp.auto_asconf_splist);
  3384. sp->do_auto_asconf = 1;
  3385. }
  3386. spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3387. return 0;
  3388. }
  3389. /*
  3390. * SCTP_PEER_ADDR_THLDS
  3391. *
  3392. * This option allows us to alter the partially failed threshold for one or all
  3393. * transports in an association. See Section 6.1 of:
  3394. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3395. */
  3396. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3397. char __user *optval,
  3398. unsigned int optlen, bool v2)
  3399. {
  3400. struct sctp_paddrthlds_v2 val;
  3401. struct sctp_transport *trans;
  3402. struct sctp_association *asoc;
  3403. int len;
  3404. len = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
  3405. if (optlen < len)
  3406. return -EINVAL;
  3407. if (copy_from_user(&val, optval, len))
  3408. return -EFAULT;
  3409. if (v2 && val.spt_pathpfthld > val.spt_pathcpthld)
  3410. return -EINVAL;
  3411. if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  3412. trans = sctp_addr_id2transport(sk, &val.spt_address,
  3413. val.spt_assoc_id);
  3414. if (!trans)
  3415. return -ENOENT;
  3416. if (val.spt_pathmaxrxt)
  3417. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3418. if (v2)
  3419. trans->ps_retrans = val.spt_pathcpthld;
  3420. trans->pf_retrans = val.spt_pathpfthld;
  3421. return 0;
  3422. }
  3423. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  3424. if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
  3425. sctp_style(sk, UDP))
  3426. return -EINVAL;
  3427. if (asoc) {
  3428. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3429. transports) {
  3430. if (val.spt_pathmaxrxt)
  3431. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3432. if (v2)
  3433. trans->ps_retrans = val.spt_pathcpthld;
  3434. trans->pf_retrans = val.spt_pathpfthld;
  3435. }
  3436. if (val.spt_pathmaxrxt)
  3437. asoc->pathmaxrxt = val.spt_pathmaxrxt;
  3438. if (v2)
  3439. asoc->ps_retrans = val.spt_pathcpthld;
  3440. asoc->pf_retrans = val.spt_pathpfthld;
  3441. } else {
  3442. struct sctp_sock *sp = sctp_sk(sk);
  3443. if (val.spt_pathmaxrxt)
  3444. sp->pathmaxrxt = val.spt_pathmaxrxt;
  3445. if (v2)
  3446. sp->ps_retrans = val.spt_pathcpthld;
  3447. sp->pf_retrans = val.spt_pathpfthld;
  3448. }
  3449. return 0;
  3450. }
  3451. static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
  3452. char __user *optval,
  3453. unsigned int optlen)
  3454. {
  3455. int val;
  3456. if (optlen < sizeof(int))
  3457. return -EINVAL;
  3458. if (get_user(val, (int __user *) optval))
  3459. return -EFAULT;
  3460. sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
  3461. return 0;
  3462. }
  3463. static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
  3464. char __user *optval,
  3465. unsigned int optlen)
  3466. {
  3467. int val;
  3468. if (optlen < sizeof(int))
  3469. return -EINVAL;
  3470. if (get_user(val, (int __user *) optval))
  3471. return -EFAULT;
  3472. sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
  3473. return 0;
  3474. }
  3475. static int sctp_setsockopt_pr_supported(struct sock *sk,
  3476. char __user *optval,
  3477. unsigned int optlen)
  3478. {
  3479. struct sctp_assoc_value params;
  3480. struct sctp_association *asoc;
  3481. if (optlen != sizeof(params))
  3482. return -EINVAL;
  3483. if (copy_from_user(&params, optval, optlen))
  3484. return -EFAULT;
  3485. asoc = sctp_id2assoc(sk, params.assoc_id);
  3486. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3487. sctp_style(sk, UDP))
  3488. return -EINVAL;
  3489. sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
  3490. return 0;
  3491. }
  3492. static int sctp_setsockopt_default_prinfo(struct sock *sk,
  3493. char __user *optval,
  3494. unsigned int optlen)
  3495. {
  3496. struct sctp_sock *sp = sctp_sk(sk);
  3497. struct sctp_default_prinfo info;
  3498. struct sctp_association *asoc;
  3499. int retval = -EINVAL;
  3500. if (optlen != sizeof(info))
  3501. goto out;
  3502. if (copy_from_user(&info, optval, sizeof(info))) {
  3503. retval = -EFAULT;
  3504. goto out;
  3505. }
  3506. if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
  3507. goto out;
  3508. if (info.pr_policy == SCTP_PR_SCTP_NONE)
  3509. info.pr_value = 0;
  3510. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  3511. if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
  3512. sctp_style(sk, UDP))
  3513. goto out;
  3514. retval = 0;
  3515. if (asoc) {
  3516. SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
  3517. asoc->default_timetolive = info.pr_value;
  3518. goto out;
  3519. }
  3520. if (sctp_style(sk, TCP))
  3521. info.pr_assoc_id = SCTP_FUTURE_ASSOC;
  3522. if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
  3523. info.pr_assoc_id == SCTP_ALL_ASSOC) {
  3524. SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
  3525. sp->default_timetolive = info.pr_value;
  3526. }
  3527. if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
  3528. info.pr_assoc_id == SCTP_ALL_ASSOC) {
  3529. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3530. SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
  3531. asoc->default_timetolive = info.pr_value;
  3532. }
  3533. }
  3534. out:
  3535. return retval;
  3536. }
  3537. static int sctp_setsockopt_reconfig_supported(struct sock *sk,
  3538. char __user *optval,
  3539. unsigned int optlen)
  3540. {
  3541. struct sctp_assoc_value params;
  3542. struct sctp_association *asoc;
  3543. int retval = -EINVAL;
  3544. if (optlen != sizeof(params))
  3545. goto out;
  3546. if (copy_from_user(&params, optval, optlen)) {
  3547. retval = -EFAULT;
  3548. goto out;
  3549. }
  3550. asoc = sctp_id2assoc(sk, params.assoc_id);
  3551. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3552. sctp_style(sk, UDP))
  3553. goto out;
  3554. sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
  3555. retval = 0;
  3556. out:
  3557. return retval;
  3558. }
  3559. static int sctp_setsockopt_enable_strreset(struct sock *sk,
  3560. char __user *optval,
  3561. unsigned int optlen)
  3562. {
  3563. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3564. struct sctp_assoc_value params;
  3565. struct sctp_association *asoc;
  3566. int retval = -EINVAL;
  3567. if (optlen != sizeof(params))
  3568. goto out;
  3569. if (copy_from_user(&params, optval, optlen)) {
  3570. retval = -EFAULT;
  3571. goto out;
  3572. }
  3573. if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
  3574. goto out;
  3575. asoc = sctp_id2assoc(sk, params.assoc_id);
  3576. if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
  3577. sctp_style(sk, UDP))
  3578. goto out;
  3579. retval = 0;
  3580. if (asoc) {
  3581. asoc->strreset_enable = params.assoc_value;
  3582. goto out;
  3583. }
  3584. if (sctp_style(sk, TCP))
  3585. params.assoc_id = SCTP_FUTURE_ASSOC;
  3586. if (params.assoc_id == SCTP_FUTURE_ASSOC ||
  3587. params.assoc_id == SCTP_ALL_ASSOC)
  3588. ep->strreset_enable = params.assoc_value;
  3589. if (params.assoc_id == SCTP_CURRENT_ASSOC ||
  3590. params.assoc_id == SCTP_ALL_ASSOC)
  3591. list_for_each_entry(asoc, &ep->asocs, asocs)
  3592. asoc->strreset_enable = params.assoc_value;
  3593. out:
  3594. return retval;
  3595. }
  3596. static int sctp_setsockopt_reset_streams(struct sock *sk,
  3597. char __user *optval,
  3598. unsigned int optlen)
  3599. {
  3600. struct sctp_reset_streams *params;
  3601. struct sctp_association *asoc;
  3602. int retval = -EINVAL;
  3603. if (optlen < sizeof(*params))
  3604. return -EINVAL;
  3605. /* srs_number_streams is u16, so optlen can't be bigger than this. */
  3606. optlen = min_t(unsigned int, optlen, USHRT_MAX +
  3607. sizeof(__u16) * sizeof(*params));
  3608. params = memdup_user(optval, optlen);
  3609. if (IS_ERR(params))
  3610. return PTR_ERR(params);
  3611. if (params->srs_number_streams * sizeof(__u16) >
  3612. optlen - sizeof(*params))
  3613. goto out;
  3614. asoc = sctp_id2assoc(sk, params->srs_assoc_id);
  3615. if (!asoc)
  3616. goto out;
  3617. retval = sctp_send_reset_streams(asoc, params);
  3618. out:
  3619. kfree(params);
  3620. return retval;
  3621. }
  3622. static int sctp_setsockopt_reset_assoc(struct sock *sk,
  3623. char __user *optval,
  3624. unsigned int optlen)
  3625. {
  3626. struct sctp_association *asoc;
  3627. sctp_assoc_t associd;
  3628. int retval = -EINVAL;
  3629. if (optlen != sizeof(associd))
  3630. goto out;
  3631. if (copy_from_user(&associd, optval, optlen)) {
  3632. retval = -EFAULT;
  3633. goto out;
  3634. }
  3635. asoc = sctp_id2assoc(sk, associd);
  3636. if (!asoc)
  3637. goto out;
  3638. retval = sctp_send_reset_assoc(asoc);
  3639. out:
  3640. return retval;
  3641. }
  3642. static int sctp_setsockopt_add_streams(struct sock *sk,
  3643. char __user *optval,
  3644. unsigned int optlen)
  3645. {
  3646. struct sctp_association *asoc;
  3647. struct sctp_add_streams params;
  3648. int retval = -EINVAL;
  3649. if (optlen != sizeof(params))
  3650. goto out;
  3651. if (copy_from_user(&params, optval, optlen)) {
  3652. retval = -EFAULT;
  3653. goto out;
  3654. }
  3655. asoc = sctp_id2assoc(sk, params.sas_assoc_id);
  3656. if (!asoc)
  3657. goto out;
  3658. retval = sctp_send_add_streams(asoc, &params);
  3659. out:
  3660. return retval;
  3661. }
  3662. static int sctp_setsockopt_scheduler(struct sock *sk,
  3663. char __user *optval,
  3664. unsigned int optlen)
  3665. {
  3666. struct sctp_sock *sp = sctp_sk(sk);
  3667. struct sctp_association *asoc;
  3668. struct sctp_assoc_value params;
  3669. int retval = 0;
  3670. if (optlen < sizeof(params))
  3671. return -EINVAL;
  3672. optlen = sizeof(params);
  3673. if (copy_from_user(&params, optval, optlen))
  3674. return -EFAULT;
  3675. if (params.assoc_value > SCTP_SS_MAX)
  3676. return -EINVAL;
  3677. asoc = sctp_id2assoc(sk, params.assoc_id);
  3678. if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
  3679. sctp_style(sk, UDP))
  3680. return -EINVAL;
  3681. if (asoc)
  3682. return sctp_sched_set_sched(asoc, params.assoc_value);
  3683. if (sctp_style(sk, TCP))
  3684. params.assoc_id = SCTP_FUTURE_ASSOC;
  3685. if (params.assoc_id == SCTP_FUTURE_ASSOC ||
  3686. params.assoc_id == SCTP_ALL_ASSOC)
  3687. sp->default_ss = params.assoc_value;
  3688. if (params.assoc_id == SCTP_CURRENT_ASSOC ||
  3689. params.assoc_id == SCTP_ALL_ASSOC) {
  3690. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3691. int ret = sctp_sched_set_sched(asoc,
  3692. params.assoc_value);
  3693. if (ret && !retval)
  3694. retval = ret;
  3695. }
  3696. }
  3697. return retval;
  3698. }
  3699. static int sctp_setsockopt_scheduler_value(struct sock *sk,
  3700. char __user *optval,
  3701. unsigned int optlen)
  3702. {
  3703. struct sctp_stream_value params;
  3704. struct sctp_association *asoc;
  3705. int retval = -EINVAL;
  3706. if (optlen < sizeof(params))
  3707. goto out;
  3708. optlen = sizeof(params);
  3709. if (copy_from_user(&params, optval, optlen)) {
  3710. retval = -EFAULT;
  3711. goto out;
  3712. }
  3713. asoc = sctp_id2assoc(sk, params.assoc_id);
  3714. if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
  3715. sctp_style(sk, UDP))
  3716. goto out;
  3717. if (asoc) {
  3718. retval = sctp_sched_set_value(asoc, params.stream_id,
  3719. params.stream_value, GFP_KERNEL);
  3720. goto out;
  3721. }
  3722. retval = 0;
  3723. list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
  3724. int ret = sctp_sched_set_value(asoc, params.stream_id,
  3725. params.stream_value, GFP_KERNEL);
  3726. if (ret && !retval) /* try to return the 1st error. */
  3727. retval = ret;
  3728. }
  3729. out:
  3730. return retval;
  3731. }
  3732. static int sctp_setsockopt_interleaving_supported(struct sock *sk,
  3733. char __user *optval,
  3734. unsigned int optlen)
  3735. {
  3736. struct sctp_sock *sp = sctp_sk(sk);
  3737. struct sctp_assoc_value params;
  3738. struct sctp_association *asoc;
  3739. int retval = -EINVAL;
  3740. if (optlen < sizeof(params))
  3741. goto out;
  3742. optlen = sizeof(params);
  3743. if (copy_from_user(&params, optval, optlen)) {
  3744. retval = -EFAULT;
  3745. goto out;
  3746. }
  3747. asoc = sctp_id2assoc(sk, params.assoc_id);
  3748. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3749. sctp_style(sk, UDP))
  3750. goto out;
  3751. if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
  3752. retval = -EPERM;
  3753. goto out;
  3754. }
  3755. sp->ep->intl_enable = !!params.assoc_value;
  3756. retval = 0;
  3757. out:
  3758. return retval;
  3759. }
  3760. static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
  3761. unsigned int optlen)
  3762. {
  3763. int val;
  3764. if (!sctp_style(sk, TCP))
  3765. return -EOPNOTSUPP;
  3766. if (sctp_sk(sk)->ep->base.bind_addr.port)
  3767. return -EFAULT;
  3768. if (optlen < sizeof(int))
  3769. return -EINVAL;
  3770. if (get_user(val, (int __user *)optval))
  3771. return -EFAULT;
  3772. sctp_sk(sk)->reuse = !!val;
  3773. return 0;
  3774. }
  3775. static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
  3776. struct sctp_association *asoc)
  3777. {
  3778. struct sctp_ulpevent *event;
  3779. sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
  3780. if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
  3781. if (sctp_outq_is_empty(&asoc->outqueue)) {
  3782. event = sctp_ulpevent_make_sender_dry_event(asoc,
  3783. GFP_USER | __GFP_NOWARN);
  3784. if (!event)
  3785. return -ENOMEM;
  3786. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  3787. }
  3788. }
  3789. return 0;
  3790. }
  3791. static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
  3792. unsigned int optlen)
  3793. {
  3794. struct sctp_sock *sp = sctp_sk(sk);
  3795. struct sctp_association *asoc;
  3796. struct sctp_event param;
  3797. int retval = 0;
  3798. if (optlen < sizeof(param))
  3799. return -EINVAL;
  3800. optlen = sizeof(param);
  3801. if (copy_from_user(&param, optval, optlen))
  3802. return -EFAULT;
  3803. if (param.se_type < SCTP_SN_TYPE_BASE ||
  3804. param.se_type > SCTP_SN_TYPE_MAX)
  3805. return -EINVAL;
  3806. asoc = sctp_id2assoc(sk, param.se_assoc_id);
  3807. if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
  3808. sctp_style(sk, UDP))
  3809. return -EINVAL;
  3810. if (asoc)
  3811. return sctp_assoc_ulpevent_type_set(&param, asoc);
  3812. if (sctp_style(sk, TCP))
  3813. param.se_assoc_id = SCTP_FUTURE_ASSOC;
  3814. if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
  3815. param.se_assoc_id == SCTP_ALL_ASSOC)
  3816. sctp_ulpevent_type_set(&sp->subscribe,
  3817. param.se_type, param.se_on);
  3818. if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
  3819. param.se_assoc_id == SCTP_ALL_ASSOC) {
  3820. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3821. int ret = sctp_assoc_ulpevent_type_set(&param, asoc);
  3822. if (ret && !retval)
  3823. retval = ret;
  3824. }
  3825. }
  3826. return retval;
  3827. }
  3828. static int sctp_setsockopt_asconf_supported(struct sock *sk,
  3829. char __user *optval,
  3830. unsigned int optlen)
  3831. {
  3832. struct sctp_assoc_value params;
  3833. struct sctp_association *asoc;
  3834. struct sctp_endpoint *ep;
  3835. int retval = -EINVAL;
  3836. if (optlen != sizeof(params))
  3837. goto out;
  3838. if (copy_from_user(&params, optval, optlen)) {
  3839. retval = -EFAULT;
  3840. goto out;
  3841. }
  3842. asoc = sctp_id2assoc(sk, params.assoc_id);
  3843. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3844. sctp_style(sk, UDP))
  3845. goto out;
  3846. ep = sctp_sk(sk)->ep;
  3847. ep->asconf_enable = !!params.assoc_value;
  3848. if (ep->asconf_enable && ep->auth_enable) {
  3849. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3850. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3851. }
  3852. retval = 0;
  3853. out:
  3854. return retval;
  3855. }
  3856. static int sctp_setsockopt_auth_supported(struct sock *sk,
  3857. char __user *optval,
  3858. unsigned int optlen)
  3859. {
  3860. struct sctp_assoc_value params;
  3861. struct sctp_association *asoc;
  3862. struct sctp_endpoint *ep;
  3863. int retval = -EINVAL;
  3864. if (optlen != sizeof(params))
  3865. goto out;
  3866. if (copy_from_user(&params, optval, optlen)) {
  3867. retval = -EFAULT;
  3868. goto out;
  3869. }
  3870. asoc = sctp_id2assoc(sk, params.assoc_id);
  3871. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3872. sctp_style(sk, UDP))
  3873. goto out;
  3874. ep = sctp_sk(sk)->ep;
  3875. if (params.assoc_value) {
  3876. retval = sctp_auth_init(ep, GFP_KERNEL);
  3877. if (retval)
  3878. goto out;
  3879. if (ep->asconf_enable) {
  3880. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3881. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3882. }
  3883. }
  3884. ep->auth_enable = !!params.assoc_value;
  3885. retval = 0;
  3886. out:
  3887. return retval;
  3888. }
  3889. static int sctp_setsockopt_ecn_supported(struct sock *sk,
  3890. char __user *optval,
  3891. unsigned int optlen)
  3892. {
  3893. struct sctp_assoc_value params;
  3894. struct sctp_association *asoc;
  3895. int retval = -EINVAL;
  3896. if (optlen != sizeof(params))
  3897. goto out;
  3898. if (copy_from_user(&params, optval, optlen)) {
  3899. retval = -EFAULT;
  3900. goto out;
  3901. }
  3902. asoc = sctp_id2assoc(sk, params.assoc_id);
  3903. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3904. sctp_style(sk, UDP))
  3905. goto out;
  3906. sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
  3907. retval = 0;
  3908. out:
  3909. return retval;
  3910. }
  3911. static int sctp_setsockopt_pf_expose(struct sock *sk,
  3912. char __user *optval,
  3913. unsigned int optlen)
  3914. {
  3915. struct sctp_assoc_value params;
  3916. struct sctp_association *asoc;
  3917. int retval = -EINVAL;
  3918. if (optlen != sizeof(params))
  3919. goto out;
  3920. if (copy_from_user(&params, optval, optlen)) {
  3921. retval = -EFAULT;
  3922. goto out;
  3923. }
  3924. if (params.assoc_value > SCTP_PF_EXPOSE_MAX)
  3925. goto out;
  3926. asoc = sctp_id2assoc(sk, params.assoc_id);
  3927. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  3928. sctp_style(sk, UDP))
  3929. goto out;
  3930. if (asoc)
  3931. asoc->pf_expose = params.assoc_value;
  3932. else
  3933. sctp_sk(sk)->pf_expose = params.assoc_value;
  3934. retval = 0;
  3935. out:
  3936. return retval;
  3937. }
  3938. /* API 6.2 setsockopt(), getsockopt()
  3939. *
  3940. * Applications use setsockopt() and getsockopt() to set or retrieve
  3941. * socket options. Socket options are used to change the default
  3942. * behavior of sockets calls. They are described in Section 7.
  3943. *
  3944. * The syntax is:
  3945. *
  3946. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3947. * int __user *optlen);
  3948. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3949. * int optlen);
  3950. *
  3951. * sd - the socket descript.
  3952. * level - set to IPPROTO_SCTP for all SCTP options.
  3953. * optname - the option name.
  3954. * optval - the buffer to store the value of the option.
  3955. * optlen - the size of the buffer.
  3956. */
  3957. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3958. char __user *optval, unsigned int optlen)
  3959. {
  3960. int retval = 0;
  3961. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3962. /* I can hardly begin to describe how wrong this is. This is
  3963. * so broken as to be worse than useless. The API draft
  3964. * REALLY is NOT helpful here... I am not convinced that the
  3965. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3966. * are at all well-founded.
  3967. */
  3968. if (level != SOL_SCTP) {
  3969. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3970. retval = af->setsockopt(sk, level, optname, optval, optlen);
  3971. goto out_nounlock;
  3972. }
  3973. lock_sock(sk);
  3974. switch (optname) {
  3975. case SCTP_SOCKOPT_BINDX_ADD:
  3976. /* 'optlen' is the size of the addresses buffer. */
  3977. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3978. optlen, SCTP_BINDX_ADD_ADDR);
  3979. break;
  3980. case SCTP_SOCKOPT_BINDX_REM:
  3981. /* 'optlen' is the size of the addresses buffer. */
  3982. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3983. optlen, SCTP_BINDX_REM_ADDR);
  3984. break;
  3985. case SCTP_SOCKOPT_CONNECTX_OLD:
  3986. /* 'optlen' is the size of the addresses buffer. */
  3987. retval = sctp_setsockopt_connectx_old(sk,
  3988. (struct sockaddr __user *)optval,
  3989. optlen);
  3990. break;
  3991. case SCTP_SOCKOPT_CONNECTX:
  3992. /* 'optlen' is the size of the addresses buffer. */
  3993. retval = sctp_setsockopt_connectx(sk,
  3994. (struct sockaddr __user *)optval,
  3995. optlen);
  3996. break;
  3997. case SCTP_DISABLE_FRAGMENTS:
  3998. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  3999. break;
  4000. case SCTP_EVENTS:
  4001. retval = sctp_setsockopt_events(sk, optval, optlen);
  4002. break;
  4003. case SCTP_AUTOCLOSE:
  4004. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  4005. break;
  4006. case SCTP_PEER_ADDR_PARAMS:
  4007. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  4008. break;
  4009. case SCTP_DELAYED_SACK:
  4010. retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
  4011. break;
  4012. case SCTP_PARTIAL_DELIVERY_POINT:
  4013. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  4014. break;
  4015. case SCTP_INITMSG:
  4016. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  4017. break;
  4018. case SCTP_DEFAULT_SEND_PARAM:
  4019. retval = sctp_setsockopt_default_send_param(sk, optval,
  4020. optlen);
  4021. break;
  4022. case SCTP_DEFAULT_SNDINFO:
  4023. retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
  4024. break;
  4025. case SCTP_PRIMARY_ADDR:
  4026. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  4027. break;
  4028. case SCTP_SET_PEER_PRIMARY_ADDR:
  4029. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  4030. break;
  4031. case SCTP_NODELAY:
  4032. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  4033. break;
  4034. case SCTP_RTOINFO:
  4035. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  4036. break;
  4037. case SCTP_ASSOCINFO:
  4038. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  4039. break;
  4040. case SCTP_I_WANT_MAPPED_V4_ADDR:
  4041. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  4042. break;
  4043. case SCTP_MAXSEG:
  4044. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  4045. break;
  4046. case SCTP_ADAPTATION_LAYER:
  4047. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  4048. break;
  4049. case SCTP_CONTEXT:
  4050. retval = sctp_setsockopt_context(sk, optval, optlen);
  4051. break;
  4052. case SCTP_FRAGMENT_INTERLEAVE:
  4053. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  4054. break;
  4055. case SCTP_MAX_BURST:
  4056. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  4057. break;
  4058. case SCTP_AUTH_CHUNK:
  4059. retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
  4060. break;
  4061. case SCTP_HMAC_IDENT:
  4062. retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
  4063. break;
  4064. case SCTP_AUTH_KEY:
  4065. retval = sctp_setsockopt_auth_key(sk, optval, optlen);
  4066. break;
  4067. case SCTP_AUTH_ACTIVE_KEY:
  4068. retval = sctp_setsockopt_active_key(sk, optval, optlen);
  4069. break;
  4070. case SCTP_AUTH_DELETE_KEY:
  4071. retval = sctp_setsockopt_del_key(sk, optval, optlen);
  4072. break;
  4073. case SCTP_AUTH_DEACTIVATE_KEY:
  4074. retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
  4075. break;
  4076. case SCTP_AUTO_ASCONF:
  4077. retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
  4078. break;
  4079. case SCTP_PEER_ADDR_THLDS:
  4080. retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
  4081. false);
  4082. break;
  4083. case SCTP_PEER_ADDR_THLDS_V2:
  4084. retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
  4085. true);
  4086. break;
  4087. case SCTP_RECVRCVINFO:
  4088. retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
  4089. break;
  4090. case SCTP_RECVNXTINFO:
  4091. retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
  4092. break;
  4093. case SCTP_PR_SUPPORTED:
  4094. retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
  4095. break;
  4096. case SCTP_DEFAULT_PRINFO:
  4097. retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
  4098. break;
  4099. case SCTP_RECONFIG_SUPPORTED:
  4100. retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
  4101. break;
  4102. case SCTP_ENABLE_STREAM_RESET:
  4103. retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
  4104. break;
  4105. case SCTP_RESET_STREAMS:
  4106. retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
  4107. break;
  4108. case SCTP_RESET_ASSOC:
  4109. retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
  4110. break;
  4111. case SCTP_ADD_STREAMS:
  4112. retval = sctp_setsockopt_add_streams(sk, optval, optlen);
  4113. break;
  4114. case SCTP_STREAM_SCHEDULER:
  4115. retval = sctp_setsockopt_scheduler(sk, optval, optlen);
  4116. break;
  4117. case SCTP_STREAM_SCHEDULER_VALUE:
  4118. retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
  4119. break;
  4120. case SCTP_INTERLEAVING_SUPPORTED:
  4121. retval = sctp_setsockopt_interleaving_supported(sk, optval,
  4122. optlen);
  4123. break;
  4124. case SCTP_REUSE_PORT:
  4125. retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
  4126. break;
  4127. case SCTP_EVENT:
  4128. retval = sctp_setsockopt_event(sk, optval, optlen);
  4129. break;
  4130. case SCTP_ASCONF_SUPPORTED:
  4131. retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
  4132. break;
  4133. case SCTP_AUTH_SUPPORTED:
  4134. retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
  4135. break;
  4136. case SCTP_ECN_SUPPORTED:
  4137. retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
  4138. break;
  4139. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  4140. retval = sctp_setsockopt_pf_expose(sk, optval, optlen);
  4141. break;
  4142. default:
  4143. retval = -ENOPROTOOPT;
  4144. break;
  4145. }
  4146. release_sock(sk);
  4147. out_nounlock:
  4148. return retval;
  4149. }
  4150. /* API 3.1.6 connect() - UDP Style Syntax
  4151. *
  4152. * An application may use the connect() call in the UDP model to initiate an
  4153. * association without sending data.
  4154. *
  4155. * The syntax is:
  4156. *
  4157. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  4158. *
  4159. * sd: the socket descriptor to have a new association added to.
  4160. *
  4161. * nam: the address structure (either struct sockaddr_in or struct
  4162. * sockaddr_in6 defined in RFC2553 [7]).
  4163. *
  4164. * len: the size of the address.
  4165. */
  4166. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  4167. int addr_len, int flags)
  4168. {
  4169. struct sctp_af *af;
  4170. int err = -EINVAL;
  4171. lock_sock(sk);
  4172. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  4173. addr, addr_len);
  4174. /* Validate addr_len before calling common connect/connectx routine. */
  4175. af = sctp_get_af_specific(addr->sa_family);
  4176. if (af && addr_len >= af->sockaddr_len)
  4177. err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
  4178. release_sock(sk);
  4179. return err;
  4180. }
  4181. int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
  4182. int addr_len, int flags)
  4183. {
  4184. if (addr_len < sizeof(uaddr->sa_family))
  4185. return -EINVAL;
  4186. if (uaddr->sa_family == AF_UNSPEC)
  4187. return -EOPNOTSUPP;
  4188. return sctp_connect(sock->sk, uaddr, addr_len, flags);
  4189. }
  4190. /* FIXME: Write comments. */
  4191. static int sctp_disconnect(struct sock *sk, int flags)
  4192. {
  4193. return -EOPNOTSUPP; /* STUB */
  4194. }
  4195. /* 4.1.4 accept() - TCP Style Syntax
  4196. *
  4197. * Applications use accept() call to remove an established SCTP
  4198. * association from the accept queue of the endpoint. A new socket
  4199. * descriptor will be returned from accept() to represent the newly
  4200. * formed association.
  4201. */
  4202. static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
  4203. {
  4204. struct sctp_sock *sp;
  4205. struct sctp_endpoint *ep;
  4206. struct sock *newsk = NULL;
  4207. struct sctp_association *asoc;
  4208. long timeo;
  4209. int error = 0;
  4210. lock_sock(sk);
  4211. sp = sctp_sk(sk);
  4212. ep = sp->ep;
  4213. if (!sctp_style(sk, TCP)) {
  4214. error = -EOPNOTSUPP;
  4215. goto out;
  4216. }
  4217. if (!sctp_sstate(sk, LISTENING)) {
  4218. error = -EINVAL;
  4219. goto out;
  4220. }
  4221. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  4222. error = sctp_wait_for_accept(sk, timeo);
  4223. if (error)
  4224. goto out;
  4225. /* We treat the list of associations on the endpoint as the accept
  4226. * queue and pick the first association on the list.
  4227. */
  4228. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  4229. newsk = sp->pf->create_accept_sk(sk, asoc, kern);
  4230. if (!newsk) {
  4231. error = -ENOMEM;
  4232. goto out;
  4233. }
  4234. /* Populate the fields of the newsk from the oldsk and migrate the
  4235. * asoc to the newsk.
  4236. */
  4237. error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  4238. if (error) {
  4239. sk_common_release(newsk);
  4240. newsk = NULL;
  4241. }
  4242. out:
  4243. release_sock(sk);
  4244. *err = error;
  4245. return newsk;
  4246. }
  4247. /* The SCTP ioctl handler. */
  4248. static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  4249. {
  4250. int rc = -ENOTCONN;
  4251. lock_sock(sk);
  4252. /*
  4253. * SEQPACKET-style sockets in LISTENING state are valid, for
  4254. * SCTP, so only discard TCP-style sockets in LISTENING state.
  4255. */
  4256. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4257. goto out;
  4258. switch (cmd) {
  4259. case SIOCINQ: {
  4260. struct sk_buff *skb;
  4261. unsigned int amount = 0;
  4262. skb = skb_peek(&sk->sk_receive_queue);
  4263. if (skb != NULL) {
  4264. /*
  4265. * We will only return the amount of this packet since
  4266. * that is all that will be read.
  4267. */
  4268. amount = skb->len;
  4269. }
  4270. rc = put_user(amount, (int __user *)arg);
  4271. break;
  4272. }
  4273. default:
  4274. rc = -ENOIOCTLCMD;
  4275. break;
  4276. }
  4277. out:
  4278. release_sock(sk);
  4279. return rc;
  4280. }
  4281. /* This is the function which gets called during socket creation to
  4282. * initialized the SCTP-specific portion of the sock.
  4283. * The sock structure should already be zero-filled memory.
  4284. */
  4285. static int sctp_init_sock(struct sock *sk)
  4286. {
  4287. struct net *net = sock_net(sk);
  4288. struct sctp_sock *sp;
  4289. pr_debug("%s: sk:%p\n", __func__, sk);
  4290. sp = sctp_sk(sk);
  4291. /* Initialize the SCTP per socket area. */
  4292. switch (sk->sk_type) {
  4293. case SOCK_SEQPACKET:
  4294. sp->type = SCTP_SOCKET_UDP;
  4295. break;
  4296. case SOCK_STREAM:
  4297. sp->type = SCTP_SOCKET_TCP;
  4298. break;
  4299. default:
  4300. return -ESOCKTNOSUPPORT;
  4301. }
  4302. sk->sk_gso_type = SKB_GSO_SCTP;
  4303. /* Initialize default send parameters. These parameters can be
  4304. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  4305. */
  4306. sp->default_stream = 0;
  4307. sp->default_ppid = 0;
  4308. sp->default_flags = 0;
  4309. sp->default_context = 0;
  4310. sp->default_timetolive = 0;
  4311. sp->default_rcv_context = 0;
  4312. sp->max_burst = net->sctp.max_burst;
  4313. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  4314. /* Initialize default setup parameters. These parameters
  4315. * can be modified with the SCTP_INITMSG socket option or
  4316. * overridden by the SCTP_INIT CMSG.
  4317. */
  4318. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  4319. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  4320. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  4321. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  4322. /* Initialize default RTO related parameters. These parameters can
  4323. * be modified for with the SCTP_RTOINFO socket option.
  4324. */
  4325. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  4326. sp->rtoinfo.srto_max = net->sctp.rto_max;
  4327. sp->rtoinfo.srto_min = net->sctp.rto_min;
  4328. /* Initialize default association related parameters. These parameters
  4329. * can be modified with the SCTP_ASSOCINFO socket option.
  4330. */
  4331. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  4332. sp->assocparams.sasoc_number_peer_destinations = 0;
  4333. sp->assocparams.sasoc_peer_rwnd = 0;
  4334. sp->assocparams.sasoc_local_rwnd = 0;
  4335. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  4336. /* Initialize default event subscriptions. By default, all the
  4337. * options are off.
  4338. */
  4339. sp->subscribe = 0;
  4340. /* Default Peer Address Parameters. These defaults can
  4341. * be modified via SCTP_PEER_ADDR_PARAMS
  4342. */
  4343. sp->hbinterval = net->sctp.hb_interval;
  4344. sp->pathmaxrxt = net->sctp.max_retrans_path;
  4345. sp->pf_retrans = net->sctp.pf_retrans;
  4346. sp->ps_retrans = net->sctp.ps_retrans;
  4347. sp->pf_expose = net->sctp.pf_expose;
  4348. sp->pathmtu = 0; /* allow default discovery */
  4349. sp->sackdelay = net->sctp.sack_timeout;
  4350. sp->sackfreq = 2;
  4351. sp->param_flags = SPP_HB_ENABLE |
  4352. SPP_PMTUD_ENABLE |
  4353. SPP_SACKDELAY_ENABLE;
  4354. sp->default_ss = SCTP_SS_DEFAULT;
  4355. /* If enabled no SCTP message fragmentation will be performed.
  4356. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  4357. */
  4358. sp->disable_fragments = 0;
  4359. /* Enable Nagle algorithm by default. */
  4360. sp->nodelay = 0;
  4361. sp->recvrcvinfo = 0;
  4362. sp->recvnxtinfo = 0;
  4363. /* Enable by default. */
  4364. sp->v4mapped = 1;
  4365. /* Auto-close idle associations after the configured
  4366. * number of seconds. A value of 0 disables this
  4367. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  4368. * for UDP-style sockets only.
  4369. */
  4370. sp->autoclose = 0;
  4371. /* User specified fragmentation limit. */
  4372. sp->user_frag = 0;
  4373. sp->adaptation_ind = 0;
  4374. sp->pf = sctp_get_pf_specific(sk->sk_family);
  4375. /* Control variables for partial data delivery. */
  4376. atomic_set(&sp->pd_mode, 0);
  4377. skb_queue_head_init(&sp->pd_lobby);
  4378. sp->frag_interleave = 0;
  4379. /* Create a per socket endpoint structure. Even if we
  4380. * change the data structure relationships, this may still
  4381. * be useful for storing pre-connect address information.
  4382. */
  4383. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  4384. if (!sp->ep)
  4385. return -ENOMEM;
  4386. sp->hmac = NULL;
  4387. sk->sk_destruct = sctp_destruct_sock;
  4388. SCTP_DBG_OBJCNT_INC(sock);
  4389. local_bh_disable();
  4390. sk_sockets_allocated_inc(sk);
  4391. sock_prot_inuse_add(net, sk->sk_prot, 1);
  4392. /* Nothing can fail after this block, otherwise
  4393. * sctp_destroy_sock() will be called without addr_wq_lock held
  4394. */
  4395. if (net->sctp.default_auto_asconf) {
  4396. spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
  4397. list_add_tail(&sp->auto_asconf_list,
  4398. &net->sctp.auto_asconf_splist);
  4399. sp->do_auto_asconf = 1;
  4400. spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
  4401. } else {
  4402. sp->do_auto_asconf = 0;
  4403. }
  4404. local_bh_enable();
  4405. return 0;
  4406. }
  4407. /* Cleanup any SCTP per socket resources. Must be called with
  4408. * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
  4409. */
  4410. static void sctp_destroy_sock(struct sock *sk)
  4411. {
  4412. struct sctp_sock *sp;
  4413. pr_debug("%s: sk:%p\n", __func__, sk);
  4414. /* Release our hold on the endpoint. */
  4415. sp = sctp_sk(sk);
  4416. /* This could happen during socket init, thus we bail out
  4417. * early, since the rest of the below is not setup either.
  4418. */
  4419. if (sp->ep == NULL)
  4420. return;
  4421. if (sp->do_auto_asconf) {
  4422. sp->do_auto_asconf = 0;
  4423. list_del(&sp->auto_asconf_list);
  4424. }
  4425. sctp_endpoint_free(sp->ep);
  4426. local_bh_disable();
  4427. sk_sockets_allocated_dec(sk);
  4428. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  4429. local_bh_enable();
  4430. }
  4431. /* Triggered when there are no references on the socket anymore */
  4432. static void sctp_destruct_sock(struct sock *sk)
  4433. {
  4434. struct sctp_sock *sp = sctp_sk(sk);
  4435. /* Free up the HMAC transform. */
  4436. crypto_free_shash(sp->hmac);
  4437. inet_sock_destruct(sk);
  4438. }
  4439. /* API 4.1.7 shutdown() - TCP Style Syntax
  4440. * int shutdown(int socket, int how);
  4441. *
  4442. * sd - the socket descriptor of the association to be closed.
  4443. * how - Specifies the type of shutdown. The values are
  4444. * as follows:
  4445. * SHUT_RD
  4446. * Disables further receive operations. No SCTP
  4447. * protocol action is taken.
  4448. * SHUT_WR
  4449. * Disables further send operations, and initiates
  4450. * the SCTP shutdown sequence.
  4451. * SHUT_RDWR
  4452. * Disables further send and receive operations
  4453. * and initiates the SCTP shutdown sequence.
  4454. */
  4455. static void sctp_shutdown(struct sock *sk, int how)
  4456. {
  4457. struct net *net = sock_net(sk);
  4458. struct sctp_endpoint *ep;
  4459. if (!sctp_style(sk, TCP))
  4460. return;
  4461. ep = sctp_sk(sk)->ep;
  4462. if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
  4463. struct sctp_association *asoc;
  4464. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  4465. asoc = list_entry(ep->asocs.next,
  4466. struct sctp_association, asocs);
  4467. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  4468. }
  4469. }
  4470. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  4471. struct sctp_info *info)
  4472. {
  4473. struct sctp_transport *prim;
  4474. struct list_head *pos;
  4475. int mask;
  4476. memset(info, 0, sizeof(*info));
  4477. if (!asoc) {
  4478. struct sctp_sock *sp = sctp_sk(sk);
  4479. info->sctpi_s_autoclose = sp->autoclose;
  4480. info->sctpi_s_adaptation_ind = sp->adaptation_ind;
  4481. info->sctpi_s_pd_point = sp->pd_point;
  4482. info->sctpi_s_nodelay = sp->nodelay;
  4483. info->sctpi_s_disable_fragments = sp->disable_fragments;
  4484. info->sctpi_s_v4mapped = sp->v4mapped;
  4485. info->sctpi_s_frag_interleave = sp->frag_interleave;
  4486. info->sctpi_s_type = sp->type;
  4487. return 0;
  4488. }
  4489. info->sctpi_tag = asoc->c.my_vtag;
  4490. info->sctpi_state = asoc->state;
  4491. info->sctpi_rwnd = asoc->a_rwnd;
  4492. info->sctpi_unackdata = asoc->unack_data;
  4493. info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4494. info->sctpi_instrms = asoc->stream.incnt;
  4495. info->sctpi_outstrms = asoc->stream.outcnt;
  4496. list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
  4497. info->sctpi_inqueue++;
  4498. list_for_each(pos, &asoc->outqueue.out_chunk_list)
  4499. info->sctpi_outqueue++;
  4500. info->sctpi_overall_error = asoc->overall_error_count;
  4501. info->sctpi_max_burst = asoc->max_burst;
  4502. info->sctpi_maxseg = asoc->frag_point;
  4503. info->sctpi_peer_rwnd = asoc->peer.rwnd;
  4504. info->sctpi_peer_tag = asoc->c.peer_vtag;
  4505. mask = asoc->peer.ecn_capable << 1;
  4506. mask = (mask | asoc->peer.ipv4_address) << 1;
  4507. mask = (mask | asoc->peer.ipv6_address) << 1;
  4508. mask = (mask | asoc->peer.hostname_address) << 1;
  4509. mask = (mask | asoc->peer.asconf_capable) << 1;
  4510. mask = (mask | asoc->peer.prsctp_capable) << 1;
  4511. mask = (mask | asoc->peer.auth_capable);
  4512. info->sctpi_peer_capable = mask;
  4513. mask = asoc->peer.sack_needed << 1;
  4514. mask = (mask | asoc->peer.sack_generation) << 1;
  4515. mask = (mask | asoc->peer.zero_window_announced);
  4516. info->sctpi_peer_sack = mask;
  4517. info->sctpi_isacks = asoc->stats.isacks;
  4518. info->sctpi_osacks = asoc->stats.osacks;
  4519. info->sctpi_opackets = asoc->stats.opackets;
  4520. info->sctpi_ipackets = asoc->stats.ipackets;
  4521. info->sctpi_rtxchunks = asoc->stats.rtxchunks;
  4522. info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
  4523. info->sctpi_idupchunks = asoc->stats.idupchunks;
  4524. info->sctpi_gapcnt = asoc->stats.gapcnt;
  4525. info->sctpi_ouodchunks = asoc->stats.ouodchunks;
  4526. info->sctpi_iuodchunks = asoc->stats.iuodchunks;
  4527. info->sctpi_oodchunks = asoc->stats.oodchunks;
  4528. info->sctpi_iodchunks = asoc->stats.iodchunks;
  4529. info->sctpi_octrlchunks = asoc->stats.octrlchunks;
  4530. info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
  4531. prim = asoc->peer.primary_path;
  4532. memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
  4533. info->sctpi_p_state = prim->state;
  4534. info->sctpi_p_cwnd = prim->cwnd;
  4535. info->sctpi_p_srtt = prim->srtt;
  4536. info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
  4537. info->sctpi_p_hbinterval = prim->hbinterval;
  4538. info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
  4539. info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
  4540. info->sctpi_p_ssthresh = prim->ssthresh;
  4541. info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
  4542. info->sctpi_p_flight_size = prim->flight_size;
  4543. info->sctpi_p_error = prim->error_count;
  4544. return 0;
  4545. }
  4546. EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
  4547. /* use callback to avoid exporting the core structure */
  4548. void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
  4549. {
  4550. rhltable_walk_enter(&sctp_transport_hashtable, iter);
  4551. rhashtable_walk_start(iter);
  4552. }
  4553. void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
  4554. {
  4555. rhashtable_walk_stop(iter);
  4556. rhashtable_walk_exit(iter);
  4557. }
  4558. struct sctp_transport *sctp_transport_get_next(struct net *net,
  4559. struct rhashtable_iter *iter)
  4560. {
  4561. struct sctp_transport *t;
  4562. t = rhashtable_walk_next(iter);
  4563. for (; t; t = rhashtable_walk_next(iter)) {
  4564. if (IS_ERR(t)) {
  4565. if (PTR_ERR(t) == -EAGAIN)
  4566. continue;
  4567. break;
  4568. }
  4569. if (!sctp_transport_hold(t))
  4570. continue;
  4571. if (net_eq(t->asoc->base.net, net) &&
  4572. t->asoc->peer.primary_path == t)
  4573. break;
  4574. sctp_transport_put(t);
  4575. }
  4576. return t;
  4577. }
  4578. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  4579. struct rhashtable_iter *iter,
  4580. int pos)
  4581. {
  4582. struct sctp_transport *t;
  4583. if (!pos)
  4584. return SEQ_START_TOKEN;
  4585. while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
  4586. if (!--pos)
  4587. break;
  4588. sctp_transport_put(t);
  4589. }
  4590. return t;
  4591. }
  4592. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
  4593. void *p) {
  4594. int err = 0;
  4595. int hash = 0;
  4596. struct sctp_ep_common *epb;
  4597. struct sctp_hashbucket *head;
  4598. for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
  4599. hash++, head++) {
  4600. read_lock_bh(&head->lock);
  4601. sctp_for_each_hentry(epb, &head->chain) {
  4602. err = cb(sctp_ep(epb), p);
  4603. if (err)
  4604. break;
  4605. }
  4606. read_unlock_bh(&head->lock);
  4607. }
  4608. return err;
  4609. }
  4610. EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
  4611. int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
  4612. struct net *net,
  4613. const union sctp_addr *laddr,
  4614. const union sctp_addr *paddr, void *p)
  4615. {
  4616. struct sctp_transport *transport;
  4617. int err;
  4618. rcu_read_lock();
  4619. transport = sctp_addrs_lookup_transport(net, laddr, paddr);
  4620. rcu_read_unlock();
  4621. if (!transport)
  4622. return -ENOENT;
  4623. err = cb(transport, p);
  4624. sctp_transport_put(transport);
  4625. return err;
  4626. }
  4627. EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
  4628. int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
  4629. int (*cb_done)(struct sctp_transport *, void *),
  4630. struct net *net, int *pos, void *p) {
  4631. struct rhashtable_iter hti;
  4632. struct sctp_transport *tsp;
  4633. int ret;
  4634. again:
  4635. ret = 0;
  4636. sctp_transport_walk_start(&hti);
  4637. tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
  4638. for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
  4639. ret = cb(tsp, p);
  4640. if (ret)
  4641. break;
  4642. (*pos)++;
  4643. sctp_transport_put(tsp);
  4644. }
  4645. sctp_transport_walk_stop(&hti);
  4646. if (ret) {
  4647. if (cb_done && !cb_done(tsp, p)) {
  4648. (*pos)++;
  4649. sctp_transport_put(tsp);
  4650. goto again;
  4651. }
  4652. sctp_transport_put(tsp);
  4653. }
  4654. return ret;
  4655. }
  4656. EXPORT_SYMBOL_GPL(sctp_for_each_transport);
  4657. /* 7.2.1 Association Status (SCTP_STATUS)
  4658. * Applications can retrieve current status information about an
  4659. * association, including association state, peer receiver window size,
  4660. * number of unacked data chunks, and number of data chunks pending
  4661. * receipt. This information is read-only.
  4662. */
  4663. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  4664. char __user *optval,
  4665. int __user *optlen)
  4666. {
  4667. struct sctp_status status;
  4668. struct sctp_association *asoc = NULL;
  4669. struct sctp_transport *transport;
  4670. sctp_assoc_t associd;
  4671. int retval = 0;
  4672. if (len < sizeof(status)) {
  4673. retval = -EINVAL;
  4674. goto out;
  4675. }
  4676. len = sizeof(status);
  4677. if (copy_from_user(&status, optval, len)) {
  4678. retval = -EFAULT;
  4679. goto out;
  4680. }
  4681. associd = status.sstat_assoc_id;
  4682. asoc = sctp_id2assoc(sk, associd);
  4683. if (!asoc) {
  4684. retval = -EINVAL;
  4685. goto out;
  4686. }
  4687. transport = asoc->peer.primary_path;
  4688. status.sstat_assoc_id = sctp_assoc2id(asoc);
  4689. status.sstat_state = sctp_assoc_to_state(asoc);
  4690. status.sstat_rwnd = asoc->peer.rwnd;
  4691. status.sstat_unackdata = asoc->unack_data;
  4692. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4693. status.sstat_instrms = asoc->stream.incnt;
  4694. status.sstat_outstrms = asoc->stream.outcnt;
  4695. status.sstat_fragmentation_point = asoc->frag_point;
  4696. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4697. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  4698. transport->af_specific->sockaddr_len);
  4699. /* Map ipv4 address into v4-mapped-on-v6 address. */
  4700. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  4701. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  4702. status.sstat_primary.spinfo_state = transport->state;
  4703. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  4704. status.sstat_primary.spinfo_srtt = transport->srtt;
  4705. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  4706. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  4707. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  4708. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  4709. if (put_user(len, optlen)) {
  4710. retval = -EFAULT;
  4711. goto out;
  4712. }
  4713. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  4714. __func__, len, status.sstat_state, status.sstat_rwnd,
  4715. status.sstat_assoc_id);
  4716. if (copy_to_user(optval, &status, len)) {
  4717. retval = -EFAULT;
  4718. goto out;
  4719. }
  4720. out:
  4721. return retval;
  4722. }
  4723. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  4724. *
  4725. * Applications can retrieve information about a specific peer address
  4726. * of an association, including its reachability state, congestion
  4727. * window, and retransmission timer values. This information is
  4728. * read-only.
  4729. */
  4730. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  4731. char __user *optval,
  4732. int __user *optlen)
  4733. {
  4734. struct sctp_paddrinfo pinfo;
  4735. struct sctp_transport *transport;
  4736. int retval = 0;
  4737. if (len < sizeof(pinfo)) {
  4738. retval = -EINVAL;
  4739. goto out;
  4740. }
  4741. len = sizeof(pinfo);
  4742. if (copy_from_user(&pinfo, optval, len)) {
  4743. retval = -EFAULT;
  4744. goto out;
  4745. }
  4746. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  4747. pinfo.spinfo_assoc_id);
  4748. if (!transport) {
  4749. retval = -EINVAL;
  4750. goto out;
  4751. }
  4752. if (transport->state == SCTP_PF &&
  4753. transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
  4754. retval = -EACCES;
  4755. goto out;
  4756. }
  4757. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4758. pinfo.spinfo_state = transport->state;
  4759. pinfo.spinfo_cwnd = transport->cwnd;
  4760. pinfo.spinfo_srtt = transport->srtt;
  4761. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  4762. pinfo.spinfo_mtu = transport->pathmtu;
  4763. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  4764. pinfo.spinfo_state = SCTP_ACTIVE;
  4765. if (put_user(len, optlen)) {
  4766. retval = -EFAULT;
  4767. goto out;
  4768. }
  4769. if (copy_to_user(optval, &pinfo, len)) {
  4770. retval = -EFAULT;
  4771. goto out;
  4772. }
  4773. out:
  4774. return retval;
  4775. }
  4776. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  4777. *
  4778. * This option is a on/off flag. If enabled no SCTP message
  4779. * fragmentation will be performed. Instead if a message being sent
  4780. * exceeds the current PMTU size, the message will NOT be sent and
  4781. * instead a error will be indicated to the user.
  4782. */
  4783. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  4784. char __user *optval, int __user *optlen)
  4785. {
  4786. int val;
  4787. if (len < sizeof(int))
  4788. return -EINVAL;
  4789. len = sizeof(int);
  4790. val = (sctp_sk(sk)->disable_fragments == 1);
  4791. if (put_user(len, optlen))
  4792. return -EFAULT;
  4793. if (copy_to_user(optval, &val, len))
  4794. return -EFAULT;
  4795. return 0;
  4796. }
  4797. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  4798. *
  4799. * This socket option is used to specify various notifications and
  4800. * ancillary data the user wishes to receive.
  4801. */
  4802. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  4803. int __user *optlen)
  4804. {
  4805. struct sctp_event_subscribe subscribe;
  4806. __u8 *sn_type = (__u8 *)&subscribe;
  4807. int i;
  4808. if (len == 0)
  4809. return -EINVAL;
  4810. if (len > sizeof(struct sctp_event_subscribe))
  4811. len = sizeof(struct sctp_event_subscribe);
  4812. if (put_user(len, optlen))
  4813. return -EFAULT;
  4814. for (i = 0; i < len; i++)
  4815. sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
  4816. SCTP_SN_TYPE_BASE + i);
  4817. if (copy_to_user(optval, &subscribe, len))
  4818. return -EFAULT;
  4819. return 0;
  4820. }
  4821. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  4822. *
  4823. * This socket option is applicable to the UDP-style socket only. When
  4824. * set it will cause associations that are idle for more than the
  4825. * specified number of seconds to automatically close. An association
  4826. * being idle is defined an association that has NOT sent or received
  4827. * user data. The special value of '0' indicates that no automatic
  4828. * close of any associations should be performed. The option expects an
  4829. * integer defining the number of seconds of idle time before an
  4830. * association is closed.
  4831. */
  4832. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4833. {
  4834. /* Applicable to UDP-style socket only */
  4835. if (sctp_style(sk, TCP))
  4836. return -EOPNOTSUPP;
  4837. if (len < sizeof(int))
  4838. return -EINVAL;
  4839. len = sizeof(int);
  4840. if (put_user(len, optlen))
  4841. return -EFAULT;
  4842. if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
  4843. return -EFAULT;
  4844. return 0;
  4845. }
  4846. /* Helper routine to branch off an association to a new socket. */
  4847. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
  4848. {
  4849. struct sctp_association *asoc = sctp_id2assoc(sk, id);
  4850. struct sctp_sock *sp = sctp_sk(sk);
  4851. struct socket *sock;
  4852. int err = 0;
  4853. /* Do not peel off from one netns to another one. */
  4854. if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
  4855. return -EINVAL;
  4856. if (!asoc)
  4857. return -EINVAL;
  4858. /* An association cannot be branched off from an already peeled-off
  4859. * socket, nor is this supported for tcp style sockets.
  4860. */
  4861. if (!sctp_style(sk, UDP))
  4862. return -EINVAL;
  4863. /* Create a new socket. */
  4864. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  4865. if (err < 0)
  4866. return err;
  4867. sctp_copy_sock(sock->sk, sk, asoc);
  4868. /* Make peeled-off sockets more like 1-1 accepted sockets.
  4869. * Set the daddr and initialize id to something more random and also
  4870. * copy over any ip options.
  4871. */
  4872. sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
  4873. sp->pf->copy_ip_options(sk, sock->sk);
  4874. /* Populate the fields of the newsk from the oldsk and migrate the
  4875. * asoc to the newsk.
  4876. */
  4877. err = sctp_sock_migrate(sk, sock->sk, asoc,
  4878. SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  4879. if (err) {
  4880. sock_release(sock);
  4881. sock = NULL;
  4882. }
  4883. *sockp = sock;
  4884. return err;
  4885. }
  4886. EXPORT_SYMBOL(sctp_do_peeloff);
  4887. static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
  4888. struct file **newfile, unsigned flags)
  4889. {
  4890. struct socket *newsock;
  4891. int retval;
  4892. retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
  4893. if (retval < 0)
  4894. goto out;
  4895. /* Map the socket to an unused fd that can be returned to the user. */
  4896. retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
  4897. if (retval < 0) {
  4898. sock_release(newsock);
  4899. goto out;
  4900. }
  4901. *newfile = sock_alloc_file(newsock, 0, NULL);
  4902. if (IS_ERR(*newfile)) {
  4903. put_unused_fd(retval);
  4904. retval = PTR_ERR(*newfile);
  4905. *newfile = NULL;
  4906. return retval;
  4907. }
  4908. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  4909. retval);
  4910. peeloff->sd = retval;
  4911. if (flags & SOCK_NONBLOCK)
  4912. (*newfile)->f_flags |= O_NONBLOCK;
  4913. out:
  4914. return retval;
  4915. }
  4916. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4917. {
  4918. sctp_peeloff_arg_t peeloff;
  4919. struct file *newfile = NULL;
  4920. int retval = 0;
  4921. if (len < sizeof(sctp_peeloff_arg_t))
  4922. return -EINVAL;
  4923. len = sizeof(sctp_peeloff_arg_t);
  4924. if (copy_from_user(&peeloff, optval, len))
  4925. return -EFAULT;
  4926. retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
  4927. if (retval < 0)
  4928. goto out;
  4929. /* Return the fd mapped to the new socket. */
  4930. if (put_user(len, optlen)) {
  4931. fput(newfile);
  4932. put_unused_fd(retval);
  4933. return -EFAULT;
  4934. }
  4935. if (copy_to_user(optval, &peeloff, len)) {
  4936. fput(newfile);
  4937. put_unused_fd(retval);
  4938. return -EFAULT;
  4939. }
  4940. fd_install(retval, newfile);
  4941. out:
  4942. return retval;
  4943. }
  4944. static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
  4945. char __user *optval, int __user *optlen)
  4946. {
  4947. sctp_peeloff_flags_arg_t peeloff;
  4948. struct file *newfile = NULL;
  4949. int retval = 0;
  4950. if (len < sizeof(sctp_peeloff_flags_arg_t))
  4951. return -EINVAL;
  4952. len = sizeof(sctp_peeloff_flags_arg_t);
  4953. if (copy_from_user(&peeloff, optval, len))
  4954. return -EFAULT;
  4955. retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
  4956. &newfile, peeloff.flags);
  4957. if (retval < 0)
  4958. goto out;
  4959. /* Return the fd mapped to the new socket. */
  4960. if (put_user(len, optlen)) {
  4961. fput(newfile);
  4962. put_unused_fd(retval);
  4963. return -EFAULT;
  4964. }
  4965. if (copy_to_user(optval, &peeloff, len)) {
  4966. fput(newfile);
  4967. put_unused_fd(retval);
  4968. return -EFAULT;
  4969. }
  4970. fd_install(retval, newfile);
  4971. out:
  4972. return retval;
  4973. }
  4974. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  4975. *
  4976. * Applications can enable or disable heartbeats for any peer address of
  4977. * an association, modify an address's heartbeat interval, force a
  4978. * heartbeat to be sent immediately, and adjust the address's maximum
  4979. * number of retransmissions sent before an address is considered
  4980. * unreachable. The following structure is used to access and modify an
  4981. * address's parameters:
  4982. *
  4983. * struct sctp_paddrparams {
  4984. * sctp_assoc_t spp_assoc_id;
  4985. * struct sockaddr_storage spp_address;
  4986. * uint32_t spp_hbinterval;
  4987. * uint16_t spp_pathmaxrxt;
  4988. * uint32_t spp_pathmtu;
  4989. * uint32_t spp_sackdelay;
  4990. * uint32_t spp_flags;
  4991. * };
  4992. *
  4993. * spp_assoc_id - (one-to-many style socket) This is filled in the
  4994. * application, and identifies the association for
  4995. * this query.
  4996. * spp_address - This specifies which address is of interest.
  4997. * spp_hbinterval - This contains the value of the heartbeat interval,
  4998. * in milliseconds. If a value of zero
  4999. * is present in this field then no changes are to
  5000. * be made to this parameter.
  5001. * spp_pathmaxrxt - This contains the maximum number of
  5002. * retransmissions before this address shall be
  5003. * considered unreachable. If a value of zero
  5004. * is present in this field then no changes are to
  5005. * be made to this parameter.
  5006. * spp_pathmtu - When Path MTU discovery is disabled the value
  5007. * specified here will be the "fixed" path mtu.
  5008. * Note that if the spp_address field is empty
  5009. * then all associations on this address will
  5010. * have this fixed path mtu set upon them.
  5011. *
  5012. * spp_sackdelay - When delayed sack is enabled, this value specifies
  5013. * the number of milliseconds that sacks will be delayed
  5014. * for. This value will apply to all addresses of an
  5015. * association if the spp_address field is empty. Note
  5016. * also, that if delayed sack is enabled and this
  5017. * value is set to 0, no change is made to the last
  5018. * recorded delayed sack timer value.
  5019. *
  5020. * spp_flags - These flags are used to control various features
  5021. * on an association. The flag field may contain
  5022. * zero or more of the following options.
  5023. *
  5024. * SPP_HB_ENABLE - Enable heartbeats on the
  5025. * specified address. Note that if the address
  5026. * field is empty all addresses for the association
  5027. * have heartbeats enabled upon them.
  5028. *
  5029. * SPP_HB_DISABLE - Disable heartbeats on the
  5030. * speicifed address. Note that if the address
  5031. * field is empty all addresses for the association
  5032. * will have their heartbeats disabled. Note also
  5033. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  5034. * mutually exclusive, only one of these two should
  5035. * be specified. Enabling both fields will have
  5036. * undetermined results.
  5037. *
  5038. * SPP_HB_DEMAND - Request a user initiated heartbeat
  5039. * to be made immediately.
  5040. *
  5041. * SPP_PMTUD_ENABLE - This field will enable PMTU
  5042. * discovery upon the specified address. Note that
  5043. * if the address feild is empty then all addresses
  5044. * on the association are effected.
  5045. *
  5046. * SPP_PMTUD_DISABLE - This field will disable PMTU
  5047. * discovery upon the specified address. Note that
  5048. * if the address feild is empty then all addresses
  5049. * on the association are effected. Not also that
  5050. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  5051. * exclusive. Enabling both will have undetermined
  5052. * results.
  5053. *
  5054. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  5055. * on delayed sack. The time specified in spp_sackdelay
  5056. * is used to specify the sack delay for this address. Note
  5057. * that if spp_address is empty then all addresses will
  5058. * enable delayed sack and take on the sack delay
  5059. * value specified in spp_sackdelay.
  5060. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  5061. * off delayed sack. If the spp_address field is blank then
  5062. * delayed sack is disabled for the entire association. Note
  5063. * also that this field is mutually exclusive to
  5064. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  5065. * results.
  5066. *
  5067. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  5068. * setting of the IPV6 flow label value. The value is
  5069. * contained in the spp_ipv6_flowlabel field.
  5070. * Upon retrieval, this flag will be set to indicate that
  5071. * the spp_ipv6_flowlabel field has a valid value returned.
  5072. * If a specific destination address is set (in the
  5073. * spp_address field), then the value returned is that of
  5074. * the address. If just an association is specified (and
  5075. * no address), then the association's default flow label
  5076. * is returned. If neither an association nor a destination
  5077. * is specified, then the socket's default flow label is
  5078. * returned. For non-IPv6 sockets, this flag will be left
  5079. * cleared.
  5080. *
  5081. * SPP_DSCP: Setting this flag enables the setting of the
  5082. * Differentiated Services Code Point (DSCP) value
  5083. * associated with either the association or a specific
  5084. * address. The value is obtained in the spp_dscp field.
  5085. * Upon retrieval, this flag will be set to indicate that
  5086. * the spp_dscp field has a valid value returned. If a
  5087. * specific destination address is set when called (in the
  5088. * spp_address field), then that specific destination
  5089. * address's DSCP value is returned. If just an association
  5090. * is specified, then the association's default DSCP is
  5091. * returned. If neither an association nor a destination is
  5092. * specified, then the socket's default DSCP is returned.
  5093. *
  5094. * spp_ipv6_flowlabel
  5095. * - This field is used in conjunction with the
  5096. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  5097. * The 20 least significant bits are used for the flow
  5098. * label. This setting has precedence over any IPv6-layer
  5099. * setting.
  5100. *
  5101. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  5102. * and contains the DSCP. The 6 most significant bits are
  5103. * used for the DSCP. This setting has precedence over any
  5104. * IPv4- or IPv6- layer setting.
  5105. */
  5106. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  5107. char __user *optval, int __user *optlen)
  5108. {
  5109. struct sctp_paddrparams params;
  5110. struct sctp_transport *trans = NULL;
  5111. struct sctp_association *asoc = NULL;
  5112. struct sctp_sock *sp = sctp_sk(sk);
  5113. if (len >= sizeof(params))
  5114. len = sizeof(params);
  5115. else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
  5116. spp_ipv6_flowlabel), 4))
  5117. len = ALIGN(offsetof(struct sctp_paddrparams,
  5118. spp_ipv6_flowlabel), 4);
  5119. else
  5120. return -EINVAL;
  5121. if (copy_from_user(&params, optval, len))
  5122. return -EFAULT;
  5123. /* If an address other than INADDR_ANY is specified, and
  5124. * no transport is found, then the request is invalid.
  5125. */
  5126. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  5127. trans = sctp_addr_id2transport(sk, &params.spp_address,
  5128. params.spp_assoc_id);
  5129. if (!trans) {
  5130. pr_debug("%s: failed no transport\n", __func__);
  5131. return -EINVAL;
  5132. }
  5133. }
  5134. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  5135. * socket is a one to many style socket, and an association
  5136. * was not found, then the id was invalid.
  5137. */
  5138. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  5139. if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
  5140. sctp_style(sk, UDP)) {
  5141. pr_debug("%s: failed no association\n", __func__);
  5142. return -EINVAL;
  5143. }
  5144. if (trans) {
  5145. /* Fetch transport values. */
  5146. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  5147. params.spp_pathmtu = trans->pathmtu;
  5148. params.spp_pathmaxrxt = trans->pathmaxrxt;
  5149. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  5150. /*draft-11 doesn't say what to return in spp_flags*/
  5151. params.spp_flags = trans->param_flags;
  5152. if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5153. params.spp_ipv6_flowlabel = trans->flowlabel &
  5154. SCTP_FLOWLABEL_VAL_MASK;
  5155. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5156. }
  5157. if (trans->dscp & SCTP_DSCP_SET_MASK) {
  5158. params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
  5159. params.spp_flags |= SPP_DSCP;
  5160. }
  5161. } else if (asoc) {
  5162. /* Fetch association values. */
  5163. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  5164. params.spp_pathmtu = asoc->pathmtu;
  5165. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  5166. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  5167. /*draft-11 doesn't say what to return in spp_flags*/
  5168. params.spp_flags = asoc->param_flags;
  5169. if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5170. params.spp_ipv6_flowlabel = asoc->flowlabel &
  5171. SCTP_FLOWLABEL_VAL_MASK;
  5172. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5173. }
  5174. if (asoc->dscp & SCTP_DSCP_SET_MASK) {
  5175. params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
  5176. params.spp_flags |= SPP_DSCP;
  5177. }
  5178. } else {
  5179. /* Fetch socket values. */
  5180. params.spp_hbinterval = sp->hbinterval;
  5181. params.spp_pathmtu = sp->pathmtu;
  5182. params.spp_sackdelay = sp->sackdelay;
  5183. params.spp_pathmaxrxt = sp->pathmaxrxt;
  5184. /*draft-11 doesn't say what to return in spp_flags*/
  5185. params.spp_flags = sp->param_flags;
  5186. if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5187. params.spp_ipv6_flowlabel = sp->flowlabel &
  5188. SCTP_FLOWLABEL_VAL_MASK;
  5189. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5190. }
  5191. if (sp->dscp & SCTP_DSCP_SET_MASK) {
  5192. params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
  5193. params.spp_flags |= SPP_DSCP;
  5194. }
  5195. }
  5196. if (copy_to_user(optval, &params, len))
  5197. return -EFAULT;
  5198. if (put_user(len, optlen))
  5199. return -EFAULT;
  5200. return 0;
  5201. }
  5202. /*
  5203. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  5204. *
  5205. * This option will effect the way delayed acks are performed. This
  5206. * option allows you to get or set the delayed ack time, in
  5207. * milliseconds. It also allows changing the delayed ack frequency.
  5208. * Changing the frequency to 1 disables the delayed sack algorithm. If
  5209. * the assoc_id is 0, then this sets or gets the endpoints default
  5210. * values. If the assoc_id field is non-zero, then the set or get
  5211. * effects the specified association for the one to many model (the
  5212. * assoc_id field is ignored by the one to one model). Note that if
  5213. * sack_delay or sack_freq are 0 when setting this option, then the
  5214. * current values will remain unchanged.
  5215. *
  5216. * struct sctp_sack_info {
  5217. * sctp_assoc_t sack_assoc_id;
  5218. * uint32_t sack_delay;
  5219. * uint32_t sack_freq;
  5220. * };
  5221. *
  5222. * sack_assoc_id - This parameter, indicates which association the user
  5223. * is performing an action upon. Note that if this field's value is
  5224. * zero then the endpoints default value is changed (effecting future
  5225. * associations only).
  5226. *
  5227. * sack_delay - This parameter contains the number of milliseconds that
  5228. * the user is requesting the delayed ACK timer be set to. Note that
  5229. * this value is defined in the standard to be between 200 and 500
  5230. * milliseconds.
  5231. *
  5232. * sack_freq - This parameter contains the number of packets that must
  5233. * be received before a sack is sent without waiting for the delay
  5234. * timer to expire. The default value for this is 2, setting this
  5235. * value to 1 will disable the delayed sack algorithm.
  5236. */
  5237. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  5238. char __user *optval,
  5239. int __user *optlen)
  5240. {
  5241. struct sctp_sack_info params;
  5242. struct sctp_association *asoc = NULL;
  5243. struct sctp_sock *sp = sctp_sk(sk);
  5244. if (len >= sizeof(struct sctp_sack_info)) {
  5245. len = sizeof(struct sctp_sack_info);
  5246. if (copy_from_user(&params, optval, len))
  5247. return -EFAULT;
  5248. } else if (len == sizeof(struct sctp_assoc_value)) {
  5249. pr_warn_ratelimited(DEPRECATED
  5250. "%s (pid %d) "
  5251. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  5252. "Use struct sctp_sack_info instead\n",
  5253. current->comm, task_pid_nr(current));
  5254. if (copy_from_user(&params, optval, len))
  5255. return -EFAULT;
  5256. } else
  5257. return -EINVAL;
  5258. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  5259. * socket is a one to many style socket, and an association
  5260. * was not found, then the id was invalid.
  5261. */
  5262. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  5263. if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
  5264. sctp_style(sk, UDP))
  5265. return -EINVAL;
  5266. if (asoc) {
  5267. /* Fetch association values. */
  5268. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  5269. params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
  5270. params.sack_freq = asoc->sackfreq;
  5271. } else {
  5272. params.sack_delay = 0;
  5273. params.sack_freq = 1;
  5274. }
  5275. } else {
  5276. /* Fetch socket values. */
  5277. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  5278. params.sack_delay = sp->sackdelay;
  5279. params.sack_freq = sp->sackfreq;
  5280. } else {
  5281. params.sack_delay = 0;
  5282. params.sack_freq = 1;
  5283. }
  5284. }
  5285. if (copy_to_user(optval, &params, len))
  5286. return -EFAULT;
  5287. if (put_user(len, optlen))
  5288. return -EFAULT;
  5289. return 0;
  5290. }
  5291. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  5292. *
  5293. * Applications can specify protocol parameters for the default association
  5294. * initialization. The option name argument to setsockopt() and getsockopt()
  5295. * is SCTP_INITMSG.
  5296. *
  5297. * Setting initialization parameters is effective only on an unconnected
  5298. * socket (for UDP-style sockets only future associations are effected
  5299. * by the change). With TCP-style sockets, this option is inherited by
  5300. * sockets derived from a listener socket.
  5301. */
  5302. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  5303. {
  5304. if (len < sizeof(struct sctp_initmsg))
  5305. return -EINVAL;
  5306. len = sizeof(struct sctp_initmsg);
  5307. if (put_user(len, optlen))
  5308. return -EFAULT;
  5309. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  5310. return -EFAULT;
  5311. return 0;
  5312. }
  5313. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  5314. char __user *optval, int __user *optlen)
  5315. {
  5316. struct sctp_association *asoc;
  5317. int cnt = 0;
  5318. struct sctp_getaddrs getaddrs;
  5319. struct sctp_transport *from;
  5320. void __user *to;
  5321. union sctp_addr temp;
  5322. struct sctp_sock *sp = sctp_sk(sk);
  5323. int addrlen;
  5324. size_t space_left;
  5325. int bytes_copied;
  5326. if (len < sizeof(struct sctp_getaddrs))
  5327. return -EINVAL;
  5328. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5329. return -EFAULT;
  5330. /* For UDP-style sockets, id specifies the association to query. */
  5331. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5332. if (!asoc)
  5333. return -EINVAL;
  5334. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5335. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5336. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  5337. transports) {
  5338. memcpy(&temp, &from->ipaddr, sizeof(temp));
  5339. addrlen = sctp_get_pf_specific(sk->sk_family)
  5340. ->addr_to_user(sp, &temp);
  5341. if (space_left < addrlen)
  5342. return -ENOMEM;
  5343. if (copy_to_user(to, &temp, addrlen))
  5344. return -EFAULT;
  5345. to += addrlen;
  5346. cnt++;
  5347. space_left -= addrlen;
  5348. }
  5349. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  5350. return -EFAULT;
  5351. bytes_copied = ((char __user *)to) - optval;
  5352. if (put_user(bytes_copied, optlen))
  5353. return -EFAULT;
  5354. return 0;
  5355. }
  5356. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  5357. size_t space_left, int *bytes_copied)
  5358. {
  5359. struct sctp_sockaddr_entry *addr;
  5360. union sctp_addr temp;
  5361. int cnt = 0;
  5362. int addrlen;
  5363. struct net *net = sock_net(sk);
  5364. rcu_read_lock();
  5365. list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
  5366. if (!addr->valid)
  5367. continue;
  5368. if ((PF_INET == sk->sk_family) &&
  5369. (AF_INET6 == addr->a.sa.sa_family))
  5370. continue;
  5371. if ((PF_INET6 == sk->sk_family) &&
  5372. inet_v6_ipv6only(sk) &&
  5373. (AF_INET == addr->a.sa.sa_family))
  5374. continue;
  5375. memcpy(&temp, &addr->a, sizeof(temp));
  5376. if (!temp.v4.sin_port)
  5377. temp.v4.sin_port = htons(port);
  5378. addrlen = sctp_get_pf_specific(sk->sk_family)
  5379. ->addr_to_user(sctp_sk(sk), &temp);
  5380. if (space_left < addrlen) {
  5381. cnt = -ENOMEM;
  5382. break;
  5383. }
  5384. memcpy(to, &temp, addrlen);
  5385. to += addrlen;
  5386. cnt++;
  5387. space_left -= addrlen;
  5388. *bytes_copied += addrlen;
  5389. }
  5390. rcu_read_unlock();
  5391. return cnt;
  5392. }
  5393. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  5394. char __user *optval, int __user *optlen)
  5395. {
  5396. struct sctp_bind_addr *bp;
  5397. struct sctp_association *asoc;
  5398. int cnt = 0;
  5399. struct sctp_getaddrs getaddrs;
  5400. struct sctp_sockaddr_entry *addr;
  5401. void __user *to;
  5402. union sctp_addr temp;
  5403. struct sctp_sock *sp = sctp_sk(sk);
  5404. int addrlen;
  5405. int err = 0;
  5406. size_t space_left;
  5407. int bytes_copied = 0;
  5408. void *addrs;
  5409. void *buf;
  5410. if (len < sizeof(struct sctp_getaddrs))
  5411. return -EINVAL;
  5412. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5413. return -EFAULT;
  5414. /*
  5415. * For UDP-style sockets, id specifies the association to query.
  5416. * If the id field is set to the value '0' then the locally bound
  5417. * addresses are returned without regard to any particular
  5418. * association.
  5419. */
  5420. if (0 == getaddrs.assoc_id) {
  5421. bp = &sctp_sk(sk)->ep->base.bind_addr;
  5422. } else {
  5423. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5424. if (!asoc)
  5425. return -EINVAL;
  5426. bp = &asoc->base.bind_addr;
  5427. }
  5428. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5429. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5430. addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
  5431. if (!addrs)
  5432. return -ENOMEM;
  5433. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  5434. * addresses from the global local address list.
  5435. */
  5436. if (sctp_list_single_entry(&bp->address_list)) {
  5437. addr = list_entry(bp->address_list.next,
  5438. struct sctp_sockaddr_entry, list);
  5439. if (sctp_is_any(sk, &addr->a)) {
  5440. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  5441. space_left, &bytes_copied);
  5442. if (cnt < 0) {
  5443. err = cnt;
  5444. goto out;
  5445. }
  5446. goto copy_getaddrs;
  5447. }
  5448. }
  5449. buf = addrs;
  5450. /* Protection on the bound address list is not needed since
  5451. * in the socket option context we hold a socket lock and
  5452. * thus the bound address list can't change.
  5453. */
  5454. list_for_each_entry(addr, &bp->address_list, list) {
  5455. memcpy(&temp, &addr->a, sizeof(temp));
  5456. addrlen = sctp_get_pf_specific(sk->sk_family)
  5457. ->addr_to_user(sp, &temp);
  5458. if (space_left < addrlen) {
  5459. err = -ENOMEM; /*fixme: right error?*/
  5460. goto out;
  5461. }
  5462. memcpy(buf, &temp, addrlen);
  5463. buf += addrlen;
  5464. bytes_copied += addrlen;
  5465. cnt++;
  5466. space_left -= addrlen;
  5467. }
  5468. copy_getaddrs:
  5469. if (copy_to_user(to, addrs, bytes_copied)) {
  5470. err = -EFAULT;
  5471. goto out;
  5472. }
  5473. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  5474. err = -EFAULT;
  5475. goto out;
  5476. }
  5477. /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
  5478. * but we can't change it anymore.
  5479. */
  5480. if (put_user(bytes_copied, optlen))
  5481. err = -EFAULT;
  5482. out:
  5483. kfree(addrs);
  5484. return err;
  5485. }
  5486. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  5487. *
  5488. * Requests that the local SCTP stack use the enclosed peer address as
  5489. * the association primary. The enclosed address must be one of the
  5490. * association peer's addresses.
  5491. */
  5492. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  5493. char __user *optval, int __user *optlen)
  5494. {
  5495. struct sctp_prim prim;
  5496. struct sctp_association *asoc;
  5497. struct sctp_sock *sp = sctp_sk(sk);
  5498. if (len < sizeof(struct sctp_prim))
  5499. return -EINVAL;
  5500. len = sizeof(struct sctp_prim);
  5501. if (copy_from_user(&prim, optval, len))
  5502. return -EFAULT;
  5503. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  5504. if (!asoc)
  5505. return -EINVAL;
  5506. if (!asoc->peer.primary_path)
  5507. return -ENOTCONN;
  5508. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  5509. asoc->peer.primary_path->af_specific->sockaddr_len);
  5510. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
  5511. (union sctp_addr *)&prim.ssp_addr);
  5512. if (put_user(len, optlen))
  5513. return -EFAULT;
  5514. if (copy_to_user(optval, &prim, len))
  5515. return -EFAULT;
  5516. return 0;
  5517. }
  5518. /*
  5519. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  5520. *
  5521. * Requests that the local endpoint set the specified Adaptation Layer
  5522. * Indication parameter for all future INIT and INIT-ACK exchanges.
  5523. */
  5524. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  5525. char __user *optval, int __user *optlen)
  5526. {
  5527. struct sctp_setadaptation adaptation;
  5528. if (len < sizeof(struct sctp_setadaptation))
  5529. return -EINVAL;
  5530. len = sizeof(struct sctp_setadaptation);
  5531. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  5532. if (put_user(len, optlen))
  5533. return -EFAULT;
  5534. if (copy_to_user(optval, &adaptation, len))
  5535. return -EFAULT;
  5536. return 0;
  5537. }
  5538. /*
  5539. *
  5540. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  5541. *
  5542. * Applications that wish to use the sendto() system call may wish to
  5543. * specify a default set of parameters that would normally be supplied
  5544. * through the inclusion of ancillary data. This socket option allows
  5545. * such an application to set the default sctp_sndrcvinfo structure.
  5546. * The application that wishes to use this socket option simply passes
  5547. * in to this call the sctp_sndrcvinfo structure defined in Section
  5548. * 5.2.2) The input parameters accepted by this call include
  5549. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  5550. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  5551. * to this call if the caller is using the UDP model.
  5552. *
  5553. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  5554. */
  5555. static int sctp_getsockopt_default_send_param(struct sock *sk,
  5556. int len, char __user *optval,
  5557. int __user *optlen)
  5558. {
  5559. struct sctp_sock *sp = sctp_sk(sk);
  5560. struct sctp_association *asoc;
  5561. struct sctp_sndrcvinfo info;
  5562. if (len < sizeof(info))
  5563. return -EINVAL;
  5564. len = sizeof(info);
  5565. if (copy_from_user(&info, optval, len))
  5566. return -EFAULT;
  5567. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  5568. if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
  5569. sctp_style(sk, UDP))
  5570. return -EINVAL;
  5571. if (asoc) {
  5572. info.sinfo_stream = asoc->default_stream;
  5573. info.sinfo_flags = asoc->default_flags;
  5574. info.sinfo_ppid = asoc->default_ppid;
  5575. info.sinfo_context = asoc->default_context;
  5576. info.sinfo_timetolive = asoc->default_timetolive;
  5577. } else {
  5578. info.sinfo_stream = sp->default_stream;
  5579. info.sinfo_flags = sp->default_flags;
  5580. info.sinfo_ppid = sp->default_ppid;
  5581. info.sinfo_context = sp->default_context;
  5582. info.sinfo_timetolive = sp->default_timetolive;
  5583. }
  5584. if (put_user(len, optlen))
  5585. return -EFAULT;
  5586. if (copy_to_user(optval, &info, len))
  5587. return -EFAULT;
  5588. return 0;
  5589. }
  5590. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  5591. * (SCTP_DEFAULT_SNDINFO)
  5592. */
  5593. static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
  5594. char __user *optval,
  5595. int __user *optlen)
  5596. {
  5597. struct sctp_sock *sp = sctp_sk(sk);
  5598. struct sctp_association *asoc;
  5599. struct sctp_sndinfo info;
  5600. if (len < sizeof(info))
  5601. return -EINVAL;
  5602. len = sizeof(info);
  5603. if (copy_from_user(&info, optval, len))
  5604. return -EFAULT;
  5605. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  5606. if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
  5607. sctp_style(sk, UDP))
  5608. return -EINVAL;
  5609. if (asoc) {
  5610. info.snd_sid = asoc->default_stream;
  5611. info.snd_flags = asoc->default_flags;
  5612. info.snd_ppid = asoc->default_ppid;
  5613. info.snd_context = asoc->default_context;
  5614. } else {
  5615. info.snd_sid = sp->default_stream;
  5616. info.snd_flags = sp->default_flags;
  5617. info.snd_ppid = sp->default_ppid;
  5618. info.snd_context = sp->default_context;
  5619. }
  5620. if (put_user(len, optlen))
  5621. return -EFAULT;
  5622. if (copy_to_user(optval, &info, len))
  5623. return -EFAULT;
  5624. return 0;
  5625. }
  5626. /*
  5627. *
  5628. * 7.1.5 SCTP_NODELAY
  5629. *
  5630. * Turn on/off any Nagle-like algorithm. This means that packets are
  5631. * generally sent as soon as possible and no unnecessary delays are
  5632. * introduced, at the cost of more packets in the network. Expects an
  5633. * integer boolean flag.
  5634. */
  5635. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  5636. char __user *optval, int __user *optlen)
  5637. {
  5638. int val;
  5639. if (len < sizeof(int))
  5640. return -EINVAL;
  5641. len = sizeof(int);
  5642. val = (sctp_sk(sk)->nodelay == 1);
  5643. if (put_user(len, optlen))
  5644. return -EFAULT;
  5645. if (copy_to_user(optval, &val, len))
  5646. return -EFAULT;
  5647. return 0;
  5648. }
  5649. /*
  5650. *
  5651. * 7.1.1 SCTP_RTOINFO
  5652. *
  5653. * The protocol parameters used to initialize and bound retransmission
  5654. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  5655. * and modify these parameters.
  5656. * All parameters are time values, in milliseconds. A value of 0, when
  5657. * modifying the parameters, indicates that the current value should not
  5658. * be changed.
  5659. *
  5660. */
  5661. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  5662. char __user *optval,
  5663. int __user *optlen) {
  5664. struct sctp_rtoinfo rtoinfo;
  5665. struct sctp_association *asoc;
  5666. if (len < sizeof (struct sctp_rtoinfo))
  5667. return -EINVAL;
  5668. len = sizeof(struct sctp_rtoinfo);
  5669. if (copy_from_user(&rtoinfo, optval, len))
  5670. return -EFAULT;
  5671. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  5672. if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
  5673. sctp_style(sk, UDP))
  5674. return -EINVAL;
  5675. /* Values corresponding to the specific association. */
  5676. if (asoc) {
  5677. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  5678. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  5679. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  5680. } else {
  5681. /* Values corresponding to the endpoint. */
  5682. struct sctp_sock *sp = sctp_sk(sk);
  5683. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  5684. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  5685. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  5686. }
  5687. if (put_user(len, optlen))
  5688. return -EFAULT;
  5689. if (copy_to_user(optval, &rtoinfo, len))
  5690. return -EFAULT;
  5691. return 0;
  5692. }
  5693. /*
  5694. *
  5695. * 7.1.2 SCTP_ASSOCINFO
  5696. *
  5697. * This option is used to tune the maximum retransmission attempts
  5698. * of the association.
  5699. * Returns an error if the new association retransmission value is
  5700. * greater than the sum of the retransmission value of the peer.
  5701. * See [SCTP] for more information.
  5702. *
  5703. */
  5704. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  5705. char __user *optval,
  5706. int __user *optlen)
  5707. {
  5708. struct sctp_assocparams assocparams;
  5709. struct sctp_association *asoc;
  5710. struct list_head *pos;
  5711. int cnt = 0;
  5712. if (len < sizeof (struct sctp_assocparams))
  5713. return -EINVAL;
  5714. len = sizeof(struct sctp_assocparams);
  5715. if (copy_from_user(&assocparams, optval, len))
  5716. return -EFAULT;
  5717. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  5718. if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  5719. sctp_style(sk, UDP))
  5720. return -EINVAL;
  5721. /* Values correspoinding to the specific association */
  5722. if (asoc) {
  5723. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  5724. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  5725. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  5726. assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
  5727. list_for_each(pos, &asoc->peer.transport_addr_list) {
  5728. cnt++;
  5729. }
  5730. assocparams.sasoc_number_peer_destinations = cnt;
  5731. } else {
  5732. /* Values corresponding to the endpoint */
  5733. struct sctp_sock *sp = sctp_sk(sk);
  5734. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  5735. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  5736. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  5737. assocparams.sasoc_cookie_life =
  5738. sp->assocparams.sasoc_cookie_life;
  5739. assocparams.sasoc_number_peer_destinations =
  5740. sp->assocparams.
  5741. sasoc_number_peer_destinations;
  5742. }
  5743. if (put_user(len, optlen))
  5744. return -EFAULT;
  5745. if (copy_to_user(optval, &assocparams, len))
  5746. return -EFAULT;
  5747. return 0;
  5748. }
  5749. /*
  5750. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  5751. *
  5752. * This socket option is a boolean flag which turns on or off mapped V4
  5753. * addresses. If this option is turned on and the socket is type
  5754. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  5755. * If this option is turned off, then no mapping will be done of V4
  5756. * addresses and a user will receive both PF_INET6 and PF_INET type
  5757. * addresses on the socket.
  5758. */
  5759. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  5760. char __user *optval, int __user *optlen)
  5761. {
  5762. int val;
  5763. struct sctp_sock *sp = sctp_sk(sk);
  5764. if (len < sizeof(int))
  5765. return -EINVAL;
  5766. len = sizeof(int);
  5767. val = sp->v4mapped;
  5768. if (put_user(len, optlen))
  5769. return -EFAULT;
  5770. if (copy_to_user(optval, &val, len))
  5771. return -EFAULT;
  5772. return 0;
  5773. }
  5774. /*
  5775. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  5776. * (chapter and verse is quoted at sctp_setsockopt_context())
  5777. */
  5778. static int sctp_getsockopt_context(struct sock *sk, int len,
  5779. char __user *optval, int __user *optlen)
  5780. {
  5781. struct sctp_assoc_value params;
  5782. struct sctp_association *asoc;
  5783. if (len < sizeof(struct sctp_assoc_value))
  5784. return -EINVAL;
  5785. len = sizeof(struct sctp_assoc_value);
  5786. if (copy_from_user(&params, optval, len))
  5787. return -EFAULT;
  5788. asoc = sctp_id2assoc(sk, params.assoc_id);
  5789. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5790. sctp_style(sk, UDP))
  5791. return -EINVAL;
  5792. params.assoc_value = asoc ? asoc->default_rcv_context
  5793. : sctp_sk(sk)->default_rcv_context;
  5794. if (put_user(len, optlen))
  5795. return -EFAULT;
  5796. if (copy_to_user(optval, &params, len))
  5797. return -EFAULT;
  5798. return 0;
  5799. }
  5800. /*
  5801. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  5802. * This option will get or set the maximum size to put in any outgoing
  5803. * SCTP DATA chunk. If a message is larger than this size it will be
  5804. * fragmented by SCTP into the specified size. Note that the underlying
  5805. * SCTP implementation may fragment into smaller sized chunks when the
  5806. * PMTU of the underlying association is smaller than the value set by
  5807. * the user. The default value for this option is '0' which indicates
  5808. * the user is NOT limiting fragmentation and only the PMTU will effect
  5809. * SCTP's choice of DATA chunk size. Note also that values set larger
  5810. * than the maximum size of an IP datagram will effectively let SCTP
  5811. * control fragmentation (i.e. the same as setting this option to 0).
  5812. *
  5813. * The following structure is used to access and modify this parameter:
  5814. *
  5815. * struct sctp_assoc_value {
  5816. * sctp_assoc_t assoc_id;
  5817. * uint32_t assoc_value;
  5818. * };
  5819. *
  5820. * assoc_id: This parameter is ignored for one-to-one style sockets.
  5821. * For one-to-many style sockets this parameter indicates which
  5822. * association the user is performing an action upon. Note that if
  5823. * this field's value is zero then the endpoints default value is
  5824. * changed (effecting future associations only).
  5825. * assoc_value: This parameter specifies the maximum size in bytes.
  5826. */
  5827. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  5828. char __user *optval, int __user *optlen)
  5829. {
  5830. struct sctp_assoc_value params;
  5831. struct sctp_association *asoc;
  5832. if (len == sizeof(int)) {
  5833. pr_warn_ratelimited(DEPRECATED
  5834. "%s (pid %d) "
  5835. "Use of int in maxseg socket option.\n"
  5836. "Use struct sctp_assoc_value instead\n",
  5837. current->comm, task_pid_nr(current));
  5838. params.assoc_id = SCTP_FUTURE_ASSOC;
  5839. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5840. len = sizeof(struct sctp_assoc_value);
  5841. if (copy_from_user(&params, optval, len))
  5842. return -EFAULT;
  5843. } else
  5844. return -EINVAL;
  5845. asoc = sctp_id2assoc(sk, params.assoc_id);
  5846. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5847. sctp_style(sk, UDP))
  5848. return -EINVAL;
  5849. if (asoc)
  5850. params.assoc_value = asoc->frag_point;
  5851. else
  5852. params.assoc_value = sctp_sk(sk)->user_frag;
  5853. if (put_user(len, optlen))
  5854. return -EFAULT;
  5855. if (len == sizeof(int)) {
  5856. if (copy_to_user(optval, &params.assoc_value, len))
  5857. return -EFAULT;
  5858. } else {
  5859. if (copy_to_user(optval, &params, len))
  5860. return -EFAULT;
  5861. }
  5862. return 0;
  5863. }
  5864. /*
  5865. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  5866. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  5867. */
  5868. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  5869. char __user *optval, int __user *optlen)
  5870. {
  5871. int val;
  5872. if (len < sizeof(int))
  5873. return -EINVAL;
  5874. len = sizeof(int);
  5875. val = sctp_sk(sk)->frag_interleave;
  5876. if (put_user(len, optlen))
  5877. return -EFAULT;
  5878. if (copy_to_user(optval, &val, len))
  5879. return -EFAULT;
  5880. return 0;
  5881. }
  5882. /*
  5883. * 7.1.25. Set or Get the sctp partial delivery point
  5884. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  5885. */
  5886. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  5887. char __user *optval,
  5888. int __user *optlen)
  5889. {
  5890. u32 val;
  5891. if (len < sizeof(u32))
  5892. return -EINVAL;
  5893. len = sizeof(u32);
  5894. val = sctp_sk(sk)->pd_point;
  5895. if (put_user(len, optlen))
  5896. return -EFAULT;
  5897. if (copy_to_user(optval, &val, len))
  5898. return -EFAULT;
  5899. return 0;
  5900. }
  5901. /*
  5902. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  5903. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  5904. */
  5905. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  5906. char __user *optval,
  5907. int __user *optlen)
  5908. {
  5909. struct sctp_assoc_value params;
  5910. struct sctp_association *asoc;
  5911. if (len == sizeof(int)) {
  5912. pr_warn_ratelimited(DEPRECATED
  5913. "%s (pid %d) "
  5914. "Use of int in max_burst socket option.\n"
  5915. "Use struct sctp_assoc_value instead\n",
  5916. current->comm, task_pid_nr(current));
  5917. params.assoc_id = SCTP_FUTURE_ASSOC;
  5918. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5919. len = sizeof(struct sctp_assoc_value);
  5920. if (copy_from_user(&params, optval, len))
  5921. return -EFAULT;
  5922. } else
  5923. return -EINVAL;
  5924. asoc = sctp_id2assoc(sk, params.assoc_id);
  5925. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5926. sctp_style(sk, UDP))
  5927. return -EINVAL;
  5928. params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
  5929. if (len == sizeof(int)) {
  5930. if (copy_to_user(optval, &params.assoc_value, len))
  5931. return -EFAULT;
  5932. } else {
  5933. if (copy_to_user(optval, &params, len))
  5934. return -EFAULT;
  5935. }
  5936. return 0;
  5937. }
  5938. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  5939. char __user *optval, int __user *optlen)
  5940. {
  5941. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5942. struct sctp_hmacalgo __user *p = (void __user *)optval;
  5943. struct sctp_hmac_algo_param *hmacs;
  5944. __u16 data_len = 0;
  5945. u32 num_idents;
  5946. int i;
  5947. if (!ep->auth_enable)
  5948. return -EACCES;
  5949. hmacs = ep->auth_hmacs_list;
  5950. data_len = ntohs(hmacs->param_hdr.length) -
  5951. sizeof(struct sctp_paramhdr);
  5952. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  5953. return -EINVAL;
  5954. len = sizeof(struct sctp_hmacalgo) + data_len;
  5955. num_idents = data_len / sizeof(u16);
  5956. if (put_user(len, optlen))
  5957. return -EFAULT;
  5958. if (put_user(num_idents, &p->shmac_num_idents))
  5959. return -EFAULT;
  5960. for (i = 0; i < num_idents; i++) {
  5961. __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
  5962. if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
  5963. return -EFAULT;
  5964. }
  5965. return 0;
  5966. }
  5967. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  5968. char __user *optval, int __user *optlen)
  5969. {
  5970. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5971. struct sctp_authkeyid val;
  5972. struct sctp_association *asoc;
  5973. if (len < sizeof(struct sctp_authkeyid))
  5974. return -EINVAL;
  5975. len = sizeof(struct sctp_authkeyid);
  5976. if (copy_from_user(&val, optval, len))
  5977. return -EFAULT;
  5978. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  5979. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  5980. return -EINVAL;
  5981. if (asoc) {
  5982. if (!asoc->peer.auth_capable)
  5983. return -EACCES;
  5984. val.scact_keynumber = asoc->active_key_id;
  5985. } else {
  5986. if (!ep->auth_enable)
  5987. return -EACCES;
  5988. val.scact_keynumber = ep->active_key_id;
  5989. }
  5990. if (put_user(len, optlen))
  5991. return -EFAULT;
  5992. if (copy_to_user(optval, &val, len))
  5993. return -EFAULT;
  5994. return 0;
  5995. }
  5996. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  5997. char __user *optval, int __user *optlen)
  5998. {
  5999. struct sctp_authchunks __user *p = (void __user *)optval;
  6000. struct sctp_authchunks val;
  6001. struct sctp_association *asoc;
  6002. struct sctp_chunks_param *ch;
  6003. u32 num_chunks = 0;
  6004. char __user *to;
  6005. if (len < sizeof(struct sctp_authchunks))
  6006. return -EINVAL;
  6007. if (copy_from_user(&val, optval, sizeof(val)))
  6008. return -EFAULT;
  6009. to = p->gauth_chunks;
  6010. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  6011. if (!asoc)
  6012. return -EINVAL;
  6013. if (!asoc->peer.auth_capable)
  6014. return -EACCES;
  6015. ch = asoc->peer.peer_chunks;
  6016. if (!ch)
  6017. goto num;
  6018. /* See if the user provided enough room for all the data */
  6019. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  6020. if (len < num_chunks)
  6021. return -EINVAL;
  6022. if (copy_to_user(to, ch->chunks, num_chunks))
  6023. return -EFAULT;
  6024. num:
  6025. len = sizeof(struct sctp_authchunks) + num_chunks;
  6026. if (put_user(len, optlen))
  6027. return -EFAULT;
  6028. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  6029. return -EFAULT;
  6030. return 0;
  6031. }
  6032. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  6033. char __user *optval, int __user *optlen)
  6034. {
  6035. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  6036. struct sctp_authchunks __user *p = (void __user *)optval;
  6037. struct sctp_authchunks val;
  6038. struct sctp_association *asoc;
  6039. struct sctp_chunks_param *ch;
  6040. u32 num_chunks = 0;
  6041. char __user *to;
  6042. if (len < sizeof(struct sctp_authchunks))
  6043. return -EINVAL;
  6044. if (copy_from_user(&val, optval, sizeof(val)))
  6045. return -EFAULT;
  6046. to = p->gauth_chunks;
  6047. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  6048. if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
  6049. sctp_style(sk, UDP))
  6050. return -EINVAL;
  6051. if (asoc) {
  6052. if (!asoc->peer.auth_capable)
  6053. return -EACCES;
  6054. ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
  6055. } else {
  6056. if (!ep->auth_enable)
  6057. return -EACCES;
  6058. ch = ep->auth_chunk_list;
  6059. }
  6060. if (!ch)
  6061. goto num;
  6062. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  6063. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  6064. return -EINVAL;
  6065. if (copy_to_user(to, ch->chunks, num_chunks))
  6066. return -EFAULT;
  6067. num:
  6068. len = sizeof(struct sctp_authchunks) + num_chunks;
  6069. if (put_user(len, optlen))
  6070. return -EFAULT;
  6071. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  6072. return -EFAULT;
  6073. return 0;
  6074. }
  6075. /*
  6076. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  6077. * This option gets the current number of associations that are attached
  6078. * to a one-to-many style socket. The option value is an uint32_t.
  6079. */
  6080. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  6081. char __user *optval, int __user *optlen)
  6082. {
  6083. struct sctp_sock *sp = sctp_sk(sk);
  6084. struct sctp_association *asoc;
  6085. u32 val = 0;
  6086. if (sctp_style(sk, TCP))
  6087. return -EOPNOTSUPP;
  6088. if (len < sizeof(u32))
  6089. return -EINVAL;
  6090. len = sizeof(u32);
  6091. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6092. val++;
  6093. }
  6094. if (put_user(len, optlen))
  6095. return -EFAULT;
  6096. if (copy_to_user(optval, &val, len))
  6097. return -EFAULT;
  6098. return 0;
  6099. }
  6100. /*
  6101. * 8.1.23 SCTP_AUTO_ASCONF
  6102. * See the corresponding setsockopt entry as description
  6103. */
  6104. static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
  6105. char __user *optval, int __user *optlen)
  6106. {
  6107. int val = 0;
  6108. if (len < sizeof(int))
  6109. return -EINVAL;
  6110. len = sizeof(int);
  6111. if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
  6112. val = 1;
  6113. if (put_user(len, optlen))
  6114. return -EFAULT;
  6115. if (copy_to_user(optval, &val, len))
  6116. return -EFAULT;
  6117. return 0;
  6118. }
  6119. /*
  6120. * 8.2.6. Get the Current Identifiers of Associations
  6121. * (SCTP_GET_ASSOC_ID_LIST)
  6122. *
  6123. * This option gets the current list of SCTP association identifiers of
  6124. * the SCTP associations handled by a one-to-many style socket.
  6125. */
  6126. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  6127. char __user *optval, int __user *optlen)
  6128. {
  6129. struct sctp_sock *sp = sctp_sk(sk);
  6130. struct sctp_association *asoc;
  6131. struct sctp_assoc_ids *ids;
  6132. u32 num = 0;
  6133. if (sctp_style(sk, TCP))
  6134. return -EOPNOTSUPP;
  6135. if (len < sizeof(struct sctp_assoc_ids))
  6136. return -EINVAL;
  6137. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6138. num++;
  6139. }
  6140. if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
  6141. return -EINVAL;
  6142. len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
  6143. ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
  6144. if (unlikely(!ids))
  6145. return -ENOMEM;
  6146. ids->gaids_number_of_ids = num;
  6147. num = 0;
  6148. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6149. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  6150. }
  6151. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  6152. kfree(ids);
  6153. return -EFAULT;
  6154. }
  6155. kfree(ids);
  6156. return 0;
  6157. }
  6158. /*
  6159. * SCTP_PEER_ADDR_THLDS
  6160. *
  6161. * This option allows us to fetch the partially failed threshold for one or all
  6162. * transports in an association. See Section 6.1 of:
  6163. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  6164. */
  6165. static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
  6166. char __user *optval, int len,
  6167. int __user *optlen, bool v2)
  6168. {
  6169. struct sctp_paddrthlds_v2 val;
  6170. struct sctp_transport *trans;
  6171. struct sctp_association *asoc;
  6172. int min;
  6173. min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
  6174. if (len < min)
  6175. return -EINVAL;
  6176. len = min;
  6177. if (copy_from_user(&val, optval, len))
  6178. return -EFAULT;
  6179. if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  6180. trans = sctp_addr_id2transport(sk, &val.spt_address,
  6181. val.spt_assoc_id);
  6182. if (!trans)
  6183. return -ENOENT;
  6184. val.spt_pathmaxrxt = trans->pathmaxrxt;
  6185. val.spt_pathpfthld = trans->pf_retrans;
  6186. val.spt_pathcpthld = trans->ps_retrans;
  6187. goto out;
  6188. }
  6189. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  6190. if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
  6191. sctp_style(sk, UDP))
  6192. return -EINVAL;
  6193. if (asoc) {
  6194. val.spt_pathpfthld = asoc->pf_retrans;
  6195. val.spt_pathmaxrxt = asoc->pathmaxrxt;
  6196. val.spt_pathcpthld = asoc->ps_retrans;
  6197. } else {
  6198. struct sctp_sock *sp = sctp_sk(sk);
  6199. val.spt_pathpfthld = sp->pf_retrans;
  6200. val.spt_pathmaxrxt = sp->pathmaxrxt;
  6201. val.spt_pathcpthld = sp->ps_retrans;
  6202. }
  6203. out:
  6204. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  6205. return -EFAULT;
  6206. return 0;
  6207. }
  6208. /*
  6209. * SCTP_GET_ASSOC_STATS
  6210. *
  6211. * This option retrieves local per endpoint statistics. It is modeled
  6212. * after OpenSolaris' implementation
  6213. */
  6214. static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
  6215. char __user *optval,
  6216. int __user *optlen)
  6217. {
  6218. struct sctp_assoc_stats sas;
  6219. struct sctp_association *asoc = NULL;
  6220. /* User must provide at least the assoc id */
  6221. if (len < sizeof(sctp_assoc_t))
  6222. return -EINVAL;
  6223. /* Allow the struct to grow and fill in as much as possible */
  6224. len = min_t(size_t, len, sizeof(sas));
  6225. if (copy_from_user(&sas, optval, len))
  6226. return -EFAULT;
  6227. asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
  6228. if (!asoc)
  6229. return -EINVAL;
  6230. sas.sas_rtxchunks = asoc->stats.rtxchunks;
  6231. sas.sas_gapcnt = asoc->stats.gapcnt;
  6232. sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
  6233. sas.sas_osacks = asoc->stats.osacks;
  6234. sas.sas_isacks = asoc->stats.isacks;
  6235. sas.sas_octrlchunks = asoc->stats.octrlchunks;
  6236. sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
  6237. sas.sas_oodchunks = asoc->stats.oodchunks;
  6238. sas.sas_iodchunks = asoc->stats.iodchunks;
  6239. sas.sas_ouodchunks = asoc->stats.ouodchunks;
  6240. sas.sas_iuodchunks = asoc->stats.iuodchunks;
  6241. sas.sas_idupchunks = asoc->stats.idupchunks;
  6242. sas.sas_opackets = asoc->stats.opackets;
  6243. sas.sas_ipackets = asoc->stats.ipackets;
  6244. /* New high max rto observed, will return 0 if not a single
  6245. * RTO update took place. obs_rto_ipaddr will be bogus
  6246. * in such a case
  6247. */
  6248. sas.sas_maxrto = asoc->stats.max_obs_rto;
  6249. memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
  6250. sizeof(struct sockaddr_storage));
  6251. /* Mark beginning of a new observation period */
  6252. asoc->stats.max_obs_rto = asoc->rto_min;
  6253. if (put_user(len, optlen))
  6254. return -EFAULT;
  6255. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  6256. if (copy_to_user(optval, &sas, len))
  6257. return -EFAULT;
  6258. return 0;
  6259. }
  6260. static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
  6261. char __user *optval,
  6262. int __user *optlen)
  6263. {
  6264. int val = 0;
  6265. if (len < sizeof(int))
  6266. return -EINVAL;
  6267. len = sizeof(int);
  6268. if (sctp_sk(sk)->recvrcvinfo)
  6269. val = 1;
  6270. if (put_user(len, optlen))
  6271. return -EFAULT;
  6272. if (copy_to_user(optval, &val, len))
  6273. return -EFAULT;
  6274. return 0;
  6275. }
  6276. static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
  6277. char __user *optval,
  6278. int __user *optlen)
  6279. {
  6280. int val = 0;
  6281. if (len < sizeof(int))
  6282. return -EINVAL;
  6283. len = sizeof(int);
  6284. if (sctp_sk(sk)->recvnxtinfo)
  6285. val = 1;
  6286. if (put_user(len, optlen))
  6287. return -EFAULT;
  6288. if (copy_to_user(optval, &val, len))
  6289. return -EFAULT;
  6290. return 0;
  6291. }
  6292. static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
  6293. char __user *optval,
  6294. int __user *optlen)
  6295. {
  6296. struct sctp_assoc_value params;
  6297. struct sctp_association *asoc;
  6298. int retval = -EFAULT;
  6299. if (len < sizeof(params)) {
  6300. retval = -EINVAL;
  6301. goto out;
  6302. }
  6303. len = sizeof(params);
  6304. if (copy_from_user(&params, optval, len))
  6305. goto out;
  6306. asoc = sctp_id2assoc(sk, params.assoc_id);
  6307. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6308. sctp_style(sk, UDP)) {
  6309. retval = -EINVAL;
  6310. goto out;
  6311. }
  6312. params.assoc_value = asoc ? asoc->peer.prsctp_capable
  6313. : sctp_sk(sk)->ep->prsctp_enable;
  6314. if (put_user(len, optlen))
  6315. goto out;
  6316. if (copy_to_user(optval, &params, len))
  6317. goto out;
  6318. retval = 0;
  6319. out:
  6320. return retval;
  6321. }
  6322. static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
  6323. char __user *optval,
  6324. int __user *optlen)
  6325. {
  6326. struct sctp_default_prinfo info;
  6327. struct sctp_association *asoc;
  6328. int retval = -EFAULT;
  6329. if (len < sizeof(info)) {
  6330. retval = -EINVAL;
  6331. goto out;
  6332. }
  6333. len = sizeof(info);
  6334. if (copy_from_user(&info, optval, len))
  6335. goto out;
  6336. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  6337. if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
  6338. sctp_style(sk, UDP)) {
  6339. retval = -EINVAL;
  6340. goto out;
  6341. }
  6342. if (asoc) {
  6343. info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
  6344. info.pr_value = asoc->default_timetolive;
  6345. } else {
  6346. struct sctp_sock *sp = sctp_sk(sk);
  6347. info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
  6348. info.pr_value = sp->default_timetolive;
  6349. }
  6350. if (put_user(len, optlen))
  6351. goto out;
  6352. if (copy_to_user(optval, &info, len))
  6353. goto out;
  6354. retval = 0;
  6355. out:
  6356. return retval;
  6357. }
  6358. static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
  6359. char __user *optval,
  6360. int __user *optlen)
  6361. {
  6362. struct sctp_prstatus params;
  6363. struct sctp_association *asoc;
  6364. int policy;
  6365. int retval = -EINVAL;
  6366. if (len < sizeof(params))
  6367. goto out;
  6368. len = sizeof(params);
  6369. if (copy_from_user(&params, optval, len)) {
  6370. retval = -EFAULT;
  6371. goto out;
  6372. }
  6373. policy = params.sprstat_policy;
  6374. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6375. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6376. goto out;
  6377. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6378. if (!asoc)
  6379. goto out;
  6380. if (policy == SCTP_PR_SCTP_ALL) {
  6381. params.sprstat_abandoned_unsent = 0;
  6382. params.sprstat_abandoned_sent = 0;
  6383. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6384. params.sprstat_abandoned_unsent +=
  6385. asoc->abandoned_unsent[policy];
  6386. params.sprstat_abandoned_sent +=
  6387. asoc->abandoned_sent[policy];
  6388. }
  6389. } else {
  6390. params.sprstat_abandoned_unsent =
  6391. asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6392. params.sprstat_abandoned_sent =
  6393. asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6394. }
  6395. if (put_user(len, optlen)) {
  6396. retval = -EFAULT;
  6397. goto out;
  6398. }
  6399. if (copy_to_user(optval, &params, len)) {
  6400. retval = -EFAULT;
  6401. goto out;
  6402. }
  6403. retval = 0;
  6404. out:
  6405. return retval;
  6406. }
  6407. static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
  6408. char __user *optval,
  6409. int __user *optlen)
  6410. {
  6411. struct sctp_stream_out_ext *streamoute;
  6412. struct sctp_association *asoc;
  6413. struct sctp_prstatus params;
  6414. int retval = -EINVAL;
  6415. int policy;
  6416. if (len < sizeof(params))
  6417. goto out;
  6418. len = sizeof(params);
  6419. if (copy_from_user(&params, optval, len)) {
  6420. retval = -EFAULT;
  6421. goto out;
  6422. }
  6423. policy = params.sprstat_policy;
  6424. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6425. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6426. goto out;
  6427. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6428. if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
  6429. goto out;
  6430. streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
  6431. if (!streamoute) {
  6432. /* Not allocated yet, means all stats are 0 */
  6433. params.sprstat_abandoned_unsent = 0;
  6434. params.sprstat_abandoned_sent = 0;
  6435. retval = 0;
  6436. goto out;
  6437. }
  6438. if (policy == SCTP_PR_SCTP_ALL) {
  6439. params.sprstat_abandoned_unsent = 0;
  6440. params.sprstat_abandoned_sent = 0;
  6441. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6442. params.sprstat_abandoned_unsent +=
  6443. streamoute->abandoned_unsent[policy];
  6444. params.sprstat_abandoned_sent +=
  6445. streamoute->abandoned_sent[policy];
  6446. }
  6447. } else {
  6448. params.sprstat_abandoned_unsent =
  6449. streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6450. params.sprstat_abandoned_sent =
  6451. streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6452. }
  6453. if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
  6454. retval = -EFAULT;
  6455. goto out;
  6456. }
  6457. retval = 0;
  6458. out:
  6459. return retval;
  6460. }
  6461. static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
  6462. char __user *optval,
  6463. int __user *optlen)
  6464. {
  6465. struct sctp_assoc_value params;
  6466. struct sctp_association *asoc;
  6467. int retval = -EFAULT;
  6468. if (len < sizeof(params)) {
  6469. retval = -EINVAL;
  6470. goto out;
  6471. }
  6472. len = sizeof(params);
  6473. if (copy_from_user(&params, optval, len))
  6474. goto out;
  6475. asoc = sctp_id2assoc(sk, params.assoc_id);
  6476. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6477. sctp_style(sk, UDP)) {
  6478. retval = -EINVAL;
  6479. goto out;
  6480. }
  6481. params.assoc_value = asoc ? asoc->peer.reconf_capable
  6482. : sctp_sk(sk)->ep->reconf_enable;
  6483. if (put_user(len, optlen))
  6484. goto out;
  6485. if (copy_to_user(optval, &params, len))
  6486. goto out;
  6487. retval = 0;
  6488. out:
  6489. return retval;
  6490. }
  6491. static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
  6492. char __user *optval,
  6493. int __user *optlen)
  6494. {
  6495. struct sctp_assoc_value params;
  6496. struct sctp_association *asoc;
  6497. int retval = -EFAULT;
  6498. if (len < sizeof(params)) {
  6499. retval = -EINVAL;
  6500. goto out;
  6501. }
  6502. len = sizeof(params);
  6503. if (copy_from_user(&params, optval, len))
  6504. goto out;
  6505. asoc = sctp_id2assoc(sk, params.assoc_id);
  6506. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6507. sctp_style(sk, UDP)) {
  6508. retval = -EINVAL;
  6509. goto out;
  6510. }
  6511. params.assoc_value = asoc ? asoc->strreset_enable
  6512. : sctp_sk(sk)->ep->strreset_enable;
  6513. if (put_user(len, optlen))
  6514. goto out;
  6515. if (copy_to_user(optval, &params, len))
  6516. goto out;
  6517. retval = 0;
  6518. out:
  6519. return retval;
  6520. }
  6521. static int sctp_getsockopt_scheduler(struct sock *sk, int len,
  6522. char __user *optval,
  6523. int __user *optlen)
  6524. {
  6525. struct sctp_assoc_value params;
  6526. struct sctp_association *asoc;
  6527. int retval = -EFAULT;
  6528. if (len < sizeof(params)) {
  6529. retval = -EINVAL;
  6530. goto out;
  6531. }
  6532. len = sizeof(params);
  6533. if (copy_from_user(&params, optval, len))
  6534. goto out;
  6535. asoc = sctp_id2assoc(sk, params.assoc_id);
  6536. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6537. sctp_style(sk, UDP)) {
  6538. retval = -EINVAL;
  6539. goto out;
  6540. }
  6541. params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
  6542. : sctp_sk(sk)->default_ss;
  6543. if (put_user(len, optlen))
  6544. goto out;
  6545. if (copy_to_user(optval, &params, len))
  6546. goto out;
  6547. retval = 0;
  6548. out:
  6549. return retval;
  6550. }
  6551. static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
  6552. char __user *optval,
  6553. int __user *optlen)
  6554. {
  6555. struct sctp_stream_value params;
  6556. struct sctp_association *asoc;
  6557. int retval = -EFAULT;
  6558. if (len < sizeof(params)) {
  6559. retval = -EINVAL;
  6560. goto out;
  6561. }
  6562. len = sizeof(params);
  6563. if (copy_from_user(&params, optval, len))
  6564. goto out;
  6565. asoc = sctp_id2assoc(sk, params.assoc_id);
  6566. if (!asoc) {
  6567. retval = -EINVAL;
  6568. goto out;
  6569. }
  6570. retval = sctp_sched_get_value(asoc, params.stream_id,
  6571. &params.stream_value);
  6572. if (retval)
  6573. goto out;
  6574. if (put_user(len, optlen)) {
  6575. retval = -EFAULT;
  6576. goto out;
  6577. }
  6578. if (copy_to_user(optval, &params, len)) {
  6579. retval = -EFAULT;
  6580. goto out;
  6581. }
  6582. out:
  6583. return retval;
  6584. }
  6585. static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
  6586. char __user *optval,
  6587. int __user *optlen)
  6588. {
  6589. struct sctp_assoc_value params;
  6590. struct sctp_association *asoc;
  6591. int retval = -EFAULT;
  6592. if (len < sizeof(params)) {
  6593. retval = -EINVAL;
  6594. goto out;
  6595. }
  6596. len = sizeof(params);
  6597. if (copy_from_user(&params, optval, len))
  6598. goto out;
  6599. asoc = sctp_id2assoc(sk, params.assoc_id);
  6600. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6601. sctp_style(sk, UDP)) {
  6602. retval = -EINVAL;
  6603. goto out;
  6604. }
  6605. params.assoc_value = asoc ? asoc->peer.intl_capable
  6606. : sctp_sk(sk)->ep->intl_enable;
  6607. if (put_user(len, optlen))
  6608. goto out;
  6609. if (copy_to_user(optval, &params, len))
  6610. goto out;
  6611. retval = 0;
  6612. out:
  6613. return retval;
  6614. }
  6615. static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
  6616. char __user *optval,
  6617. int __user *optlen)
  6618. {
  6619. int val;
  6620. if (len < sizeof(int))
  6621. return -EINVAL;
  6622. len = sizeof(int);
  6623. val = sctp_sk(sk)->reuse;
  6624. if (put_user(len, optlen))
  6625. return -EFAULT;
  6626. if (copy_to_user(optval, &val, len))
  6627. return -EFAULT;
  6628. return 0;
  6629. }
  6630. static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
  6631. int __user *optlen)
  6632. {
  6633. struct sctp_association *asoc;
  6634. struct sctp_event param;
  6635. __u16 subscribe;
  6636. if (len < sizeof(param))
  6637. return -EINVAL;
  6638. len = sizeof(param);
  6639. if (copy_from_user(&param, optval, len))
  6640. return -EFAULT;
  6641. if (param.se_type < SCTP_SN_TYPE_BASE ||
  6642. param.se_type > SCTP_SN_TYPE_MAX)
  6643. return -EINVAL;
  6644. asoc = sctp_id2assoc(sk, param.se_assoc_id);
  6645. if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
  6646. sctp_style(sk, UDP))
  6647. return -EINVAL;
  6648. subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
  6649. param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
  6650. if (put_user(len, optlen))
  6651. return -EFAULT;
  6652. if (copy_to_user(optval, &param, len))
  6653. return -EFAULT;
  6654. return 0;
  6655. }
  6656. static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
  6657. char __user *optval,
  6658. int __user *optlen)
  6659. {
  6660. struct sctp_assoc_value params;
  6661. struct sctp_association *asoc;
  6662. int retval = -EFAULT;
  6663. if (len < sizeof(params)) {
  6664. retval = -EINVAL;
  6665. goto out;
  6666. }
  6667. len = sizeof(params);
  6668. if (copy_from_user(&params, optval, len))
  6669. goto out;
  6670. asoc = sctp_id2assoc(sk, params.assoc_id);
  6671. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6672. sctp_style(sk, UDP)) {
  6673. retval = -EINVAL;
  6674. goto out;
  6675. }
  6676. params.assoc_value = asoc ? asoc->peer.asconf_capable
  6677. : sctp_sk(sk)->ep->asconf_enable;
  6678. if (put_user(len, optlen))
  6679. goto out;
  6680. if (copy_to_user(optval, &params, len))
  6681. goto out;
  6682. retval = 0;
  6683. out:
  6684. return retval;
  6685. }
  6686. static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
  6687. char __user *optval,
  6688. int __user *optlen)
  6689. {
  6690. struct sctp_assoc_value params;
  6691. struct sctp_association *asoc;
  6692. int retval = -EFAULT;
  6693. if (len < sizeof(params)) {
  6694. retval = -EINVAL;
  6695. goto out;
  6696. }
  6697. len = sizeof(params);
  6698. if (copy_from_user(&params, optval, len))
  6699. goto out;
  6700. asoc = sctp_id2assoc(sk, params.assoc_id);
  6701. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6702. sctp_style(sk, UDP)) {
  6703. retval = -EINVAL;
  6704. goto out;
  6705. }
  6706. params.assoc_value = asoc ? asoc->peer.auth_capable
  6707. : sctp_sk(sk)->ep->auth_enable;
  6708. if (put_user(len, optlen))
  6709. goto out;
  6710. if (copy_to_user(optval, &params, len))
  6711. goto out;
  6712. retval = 0;
  6713. out:
  6714. return retval;
  6715. }
  6716. static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
  6717. char __user *optval,
  6718. int __user *optlen)
  6719. {
  6720. struct sctp_assoc_value params;
  6721. struct sctp_association *asoc;
  6722. int retval = -EFAULT;
  6723. if (len < sizeof(params)) {
  6724. retval = -EINVAL;
  6725. goto out;
  6726. }
  6727. len = sizeof(params);
  6728. if (copy_from_user(&params, optval, len))
  6729. goto out;
  6730. asoc = sctp_id2assoc(sk, params.assoc_id);
  6731. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6732. sctp_style(sk, UDP)) {
  6733. retval = -EINVAL;
  6734. goto out;
  6735. }
  6736. params.assoc_value = asoc ? asoc->peer.ecn_capable
  6737. : sctp_sk(sk)->ep->ecn_enable;
  6738. if (put_user(len, optlen))
  6739. goto out;
  6740. if (copy_to_user(optval, &params, len))
  6741. goto out;
  6742. retval = 0;
  6743. out:
  6744. return retval;
  6745. }
  6746. static int sctp_getsockopt_pf_expose(struct sock *sk, int len,
  6747. char __user *optval,
  6748. int __user *optlen)
  6749. {
  6750. struct sctp_assoc_value params;
  6751. struct sctp_association *asoc;
  6752. int retval = -EFAULT;
  6753. if (len < sizeof(params)) {
  6754. retval = -EINVAL;
  6755. goto out;
  6756. }
  6757. len = sizeof(params);
  6758. if (copy_from_user(&params, optval, len))
  6759. goto out;
  6760. asoc = sctp_id2assoc(sk, params.assoc_id);
  6761. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6762. sctp_style(sk, UDP)) {
  6763. retval = -EINVAL;
  6764. goto out;
  6765. }
  6766. params.assoc_value = asoc ? asoc->pf_expose
  6767. : sctp_sk(sk)->pf_expose;
  6768. if (put_user(len, optlen))
  6769. goto out;
  6770. if (copy_to_user(optval, &params, len))
  6771. goto out;
  6772. retval = 0;
  6773. out:
  6774. return retval;
  6775. }
  6776. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  6777. char __user *optval, int __user *optlen)
  6778. {
  6779. int retval = 0;
  6780. int len;
  6781. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  6782. /* I can hardly begin to describe how wrong this is. This is
  6783. * so broken as to be worse than useless. The API draft
  6784. * REALLY is NOT helpful here... I am not convinced that the
  6785. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  6786. * are at all well-founded.
  6787. */
  6788. if (level != SOL_SCTP) {
  6789. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6790. retval = af->getsockopt(sk, level, optname, optval, optlen);
  6791. return retval;
  6792. }
  6793. if (get_user(len, optlen))
  6794. return -EFAULT;
  6795. if (len < 0)
  6796. return -EINVAL;
  6797. lock_sock(sk);
  6798. switch (optname) {
  6799. case SCTP_STATUS:
  6800. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  6801. break;
  6802. case SCTP_DISABLE_FRAGMENTS:
  6803. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  6804. optlen);
  6805. break;
  6806. case SCTP_EVENTS:
  6807. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  6808. break;
  6809. case SCTP_AUTOCLOSE:
  6810. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  6811. break;
  6812. case SCTP_SOCKOPT_PEELOFF:
  6813. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  6814. break;
  6815. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  6816. retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
  6817. break;
  6818. case SCTP_PEER_ADDR_PARAMS:
  6819. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  6820. optlen);
  6821. break;
  6822. case SCTP_DELAYED_SACK:
  6823. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  6824. optlen);
  6825. break;
  6826. case SCTP_INITMSG:
  6827. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  6828. break;
  6829. case SCTP_GET_PEER_ADDRS:
  6830. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  6831. optlen);
  6832. break;
  6833. case SCTP_GET_LOCAL_ADDRS:
  6834. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  6835. optlen);
  6836. break;
  6837. case SCTP_SOCKOPT_CONNECTX3:
  6838. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  6839. break;
  6840. case SCTP_DEFAULT_SEND_PARAM:
  6841. retval = sctp_getsockopt_default_send_param(sk, len,
  6842. optval, optlen);
  6843. break;
  6844. case SCTP_DEFAULT_SNDINFO:
  6845. retval = sctp_getsockopt_default_sndinfo(sk, len,
  6846. optval, optlen);
  6847. break;
  6848. case SCTP_PRIMARY_ADDR:
  6849. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  6850. break;
  6851. case SCTP_NODELAY:
  6852. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  6853. break;
  6854. case SCTP_RTOINFO:
  6855. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  6856. break;
  6857. case SCTP_ASSOCINFO:
  6858. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  6859. break;
  6860. case SCTP_I_WANT_MAPPED_V4_ADDR:
  6861. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  6862. break;
  6863. case SCTP_MAXSEG:
  6864. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  6865. break;
  6866. case SCTP_GET_PEER_ADDR_INFO:
  6867. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  6868. optlen);
  6869. break;
  6870. case SCTP_ADAPTATION_LAYER:
  6871. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  6872. optlen);
  6873. break;
  6874. case SCTP_CONTEXT:
  6875. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  6876. break;
  6877. case SCTP_FRAGMENT_INTERLEAVE:
  6878. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  6879. optlen);
  6880. break;
  6881. case SCTP_PARTIAL_DELIVERY_POINT:
  6882. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  6883. optlen);
  6884. break;
  6885. case SCTP_MAX_BURST:
  6886. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  6887. break;
  6888. case SCTP_AUTH_KEY:
  6889. case SCTP_AUTH_CHUNK:
  6890. case SCTP_AUTH_DELETE_KEY:
  6891. case SCTP_AUTH_DEACTIVATE_KEY:
  6892. retval = -EOPNOTSUPP;
  6893. break;
  6894. case SCTP_HMAC_IDENT:
  6895. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  6896. break;
  6897. case SCTP_AUTH_ACTIVE_KEY:
  6898. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  6899. break;
  6900. case SCTP_PEER_AUTH_CHUNKS:
  6901. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  6902. optlen);
  6903. break;
  6904. case SCTP_LOCAL_AUTH_CHUNKS:
  6905. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  6906. optlen);
  6907. break;
  6908. case SCTP_GET_ASSOC_NUMBER:
  6909. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  6910. break;
  6911. case SCTP_GET_ASSOC_ID_LIST:
  6912. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  6913. break;
  6914. case SCTP_AUTO_ASCONF:
  6915. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  6916. break;
  6917. case SCTP_PEER_ADDR_THLDS:
  6918. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6919. optlen, false);
  6920. break;
  6921. case SCTP_PEER_ADDR_THLDS_V2:
  6922. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6923. optlen, true);
  6924. break;
  6925. case SCTP_GET_ASSOC_STATS:
  6926. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  6927. break;
  6928. case SCTP_RECVRCVINFO:
  6929. retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
  6930. break;
  6931. case SCTP_RECVNXTINFO:
  6932. retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
  6933. break;
  6934. case SCTP_PR_SUPPORTED:
  6935. retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
  6936. break;
  6937. case SCTP_DEFAULT_PRINFO:
  6938. retval = sctp_getsockopt_default_prinfo(sk, len, optval,
  6939. optlen);
  6940. break;
  6941. case SCTP_PR_ASSOC_STATUS:
  6942. retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
  6943. optlen);
  6944. break;
  6945. case SCTP_PR_STREAM_STATUS:
  6946. retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
  6947. optlen);
  6948. break;
  6949. case SCTP_RECONFIG_SUPPORTED:
  6950. retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
  6951. optlen);
  6952. break;
  6953. case SCTP_ENABLE_STREAM_RESET:
  6954. retval = sctp_getsockopt_enable_strreset(sk, len, optval,
  6955. optlen);
  6956. break;
  6957. case SCTP_STREAM_SCHEDULER:
  6958. retval = sctp_getsockopt_scheduler(sk, len, optval,
  6959. optlen);
  6960. break;
  6961. case SCTP_STREAM_SCHEDULER_VALUE:
  6962. retval = sctp_getsockopt_scheduler_value(sk, len, optval,
  6963. optlen);
  6964. break;
  6965. case SCTP_INTERLEAVING_SUPPORTED:
  6966. retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
  6967. optlen);
  6968. break;
  6969. case SCTP_REUSE_PORT:
  6970. retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
  6971. break;
  6972. case SCTP_EVENT:
  6973. retval = sctp_getsockopt_event(sk, len, optval, optlen);
  6974. break;
  6975. case SCTP_ASCONF_SUPPORTED:
  6976. retval = sctp_getsockopt_asconf_supported(sk, len, optval,
  6977. optlen);
  6978. break;
  6979. case SCTP_AUTH_SUPPORTED:
  6980. retval = sctp_getsockopt_auth_supported(sk, len, optval,
  6981. optlen);
  6982. break;
  6983. case SCTP_ECN_SUPPORTED:
  6984. retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
  6985. break;
  6986. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  6987. retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen);
  6988. break;
  6989. default:
  6990. retval = -ENOPROTOOPT;
  6991. break;
  6992. }
  6993. release_sock(sk);
  6994. return retval;
  6995. }
  6996. static int sctp_hash(struct sock *sk)
  6997. {
  6998. /* STUB */
  6999. return 0;
  7000. }
  7001. static void sctp_unhash(struct sock *sk)
  7002. {
  7003. /* STUB */
  7004. }
  7005. /* Check if port is acceptable. Possibly find first available port.
  7006. *
  7007. * The port hash table (contained in the 'global' SCTP protocol storage
  7008. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  7009. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  7010. * list (the list number is the port number hashed out, so as you
  7011. * would expect from a hash function, all the ports in a given list have
  7012. * such a number that hashes out to the same list number; you were
  7013. * expecting that, right?); so each list has a set of ports, with a
  7014. * link to the socket (struct sock) that uses it, the port number and
  7015. * a fastreuse flag (FIXME: NPI ipg).
  7016. */
  7017. static struct sctp_bind_bucket *sctp_bucket_create(
  7018. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  7019. static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  7020. {
  7021. struct sctp_sock *sp = sctp_sk(sk);
  7022. bool reuse = (sk->sk_reuse || sp->reuse);
  7023. struct sctp_bind_hashbucket *head; /* hash list */
  7024. struct net *net = sock_net(sk);
  7025. kuid_t uid = sock_i_uid(sk);
  7026. struct sctp_bind_bucket *pp;
  7027. unsigned short snum;
  7028. int ret;
  7029. snum = ntohs(addr->v4.sin_port);
  7030. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  7031. local_bh_disable();
  7032. if (snum == 0) {
  7033. /* Search for an available port. */
  7034. int low, high, remaining, index;
  7035. unsigned int rover;
  7036. inet_get_local_port_range(net, &low, &high);
  7037. remaining = (high - low) + 1;
  7038. rover = prandom_u32() % remaining + low;
  7039. do {
  7040. rover++;
  7041. if ((rover < low) || (rover > high))
  7042. rover = low;
  7043. if (inet_is_local_reserved_port(net, rover))
  7044. continue;
  7045. index = sctp_phashfn(net, rover);
  7046. head = &sctp_port_hashtable[index];
  7047. spin_lock(&head->lock);
  7048. sctp_for_each_hentry(pp, &head->chain)
  7049. if ((pp->port == rover) &&
  7050. net_eq(net, pp->net))
  7051. goto next;
  7052. break;
  7053. next:
  7054. spin_unlock(&head->lock);
  7055. } while (--remaining > 0);
  7056. /* Exhausted local port range during search? */
  7057. ret = 1;
  7058. if (remaining <= 0)
  7059. goto fail;
  7060. /* OK, here is the one we will use. HEAD (the port
  7061. * hash table list entry) is non-NULL and we hold it's
  7062. * mutex.
  7063. */
  7064. snum = rover;
  7065. } else {
  7066. /* We are given an specific port number; we verify
  7067. * that it is not being used. If it is used, we will
  7068. * exahust the search in the hash list corresponding
  7069. * to the port number (snum) - we detect that with the
  7070. * port iterator, pp being NULL.
  7071. */
  7072. head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
  7073. spin_lock(&head->lock);
  7074. sctp_for_each_hentry(pp, &head->chain) {
  7075. if ((pp->port == snum) && net_eq(pp->net, net))
  7076. goto pp_found;
  7077. }
  7078. }
  7079. pp = NULL;
  7080. goto pp_not_found;
  7081. pp_found:
  7082. if (!hlist_empty(&pp->owner)) {
  7083. /* We had a port hash table hit - there is an
  7084. * available port (pp != NULL) and it is being
  7085. * used by other socket (pp->owner not empty); that other
  7086. * socket is going to be sk2.
  7087. */
  7088. struct sock *sk2;
  7089. pr_debug("%s: found a possible match\n", __func__);
  7090. if ((pp->fastreuse && reuse &&
  7091. sk->sk_state != SCTP_SS_LISTENING) ||
  7092. (pp->fastreuseport && sk->sk_reuseport &&
  7093. uid_eq(pp->fastuid, uid)))
  7094. goto success;
  7095. /* Run through the list of sockets bound to the port
  7096. * (pp->port) [via the pointers bind_next and
  7097. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  7098. * we get the endpoint they describe and run through
  7099. * the endpoint's list of IP (v4 or v6) addresses,
  7100. * comparing each of the addresses with the address of
  7101. * the socket sk. If we find a match, then that means
  7102. * that this port/socket (sk) combination are already
  7103. * in an endpoint.
  7104. */
  7105. sk_for_each_bound(sk2, &pp->owner) {
  7106. struct sctp_sock *sp2 = sctp_sk(sk2);
  7107. struct sctp_endpoint *ep2 = sp2->ep;
  7108. if (sk == sk2 ||
  7109. (reuse && (sk2->sk_reuse || sp2->reuse) &&
  7110. sk2->sk_state != SCTP_SS_LISTENING) ||
  7111. (sk->sk_reuseport && sk2->sk_reuseport &&
  7112. uid_eq(uid, sock_i_uid(sk2))))
  7113. continue;
  7114. if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
  7115. addr, sp2, sp)) {
  7116. ret = 1;
  7117. goto fail_unlock;
  7118. }
  7119. }
  7120. pr_debug("%s: found a match\n", __func__);
  7121. }
  7122. pp_not_found:
  7123. /* If there was a hash table miss, create a new port. */
  7124. ret = 1;
  7125. if (!pp && !(pp = sctp_bucket_create(head, net, snum)))
  7126. goto fail_unlock;
  7127. /* In either case (hit or miss), make sure fastreuse is 1 only
  7128. * if sk->sk_reuse is too (that is, if the caller requested
  7129. * SO_REUSEADDR on this socket -sk-).
  7130. */
  7131. if (hlist_empty(&pp->owner)) {
  7132. if (reuse && sk->sk_state != SCTP_SS_LISTENING)
  7133. pp->fastreuse = 1;
  7134. else
  7135. pp->fastreuse = 0;
  7136. if (sk->sk_reuseport) {
  7137. pp->fastreuseport = 1;
  7138. pp->fastuid = uid;
  7139. } else {
  7140. pp->fastreuseport = 0;
  7141. }
  7142. } else {
  7143. if (pp->fastreuse &&
  7144. (!reuse || sk->sk_state == SCTP_SS_LISTENING))
  7145. pp->fastreuse = 0;
  7146. if (pp->fastreuseport &&
  7147. (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
  7148. pp->fastreuseport = 0;
  7149. }
  7150. /* We are set, so fill up all the data in the hash table
  7151. * entry, tie the socket list information with the rest of the
  7152. * sockets FIXME: Blurry, NPI (ipg).
  7153. */
  7154. success:
  7155. if (!sp->bind_hash) {
  7156. inet_sk(sk)->inet_num = snum;
  7157. sk_add_bind_node(sk, &pp->owner);
  7158. sp->bind_hash = pp;
  7159. }
  7160. ret = 0;
  7161. fail_unlock:
  7162. spin_unlock(&head->lock);
  7163. fail:
  7164. local_bh_enable();
  7165. return ret;
  7166. }
  7167. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  7168. * port is requested.
  7169. */
  7170. static int sctp_get_port(struct sock *sk, unsigned short snum)
  7171. {
  7172. union sctp_addr addr;
  7173. struct sctp_af *af = sctp_sk(sk)->pf->af;
  7174. /* Set up a dummy address struct from the sk. */
  7175. af->from_sk(&addr, sk);
  7176. addr.v4.sin_port = htons(snum);
  7177. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  7178. return sctp_get_port_local(sk, &addr);
  7179. }
  7180. /*
  7181. * Move a socket to LISTENING state.
  7182. */
  7183. static int sctp_listen_start(struct sock *sk, int backlog)
  7184. {
  7185. struct sctp_sock *sp = sctp_sk(sk);
  7186. struct sctp_endpoint *ep = sp->ep;
  7187. struct crypto_shash *tfm = NULL;
  7188. char alg[32];
  7189. /* Allocate HMAC for generating cookie. */
  7190. if (!sp->hmac && sp->sctp_hmac_alg) {
  7191. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  7192. tfm = crypto_alloc_shash(alg, 0, 0);
  7193. if (IS_ERR(tfm)) {
  7194. net_info_ratelimited("failed to load transform for %s: %ld\n",
  7195. sp->sctp_hmac_alg, PTR_ERR(tfm));
  7196. return -ENOSYS;
  7197. }
  7198. sctp_sk(sk)->hmac = tfm;
  7199. }
  7200. /*
  7201. * If a bind() or sctp_bindx() is not called prior to a listen()
  7202. * call that allows new associations to be accepted, the system
  7203. * picks an ephemeral port and will choose an address set equivalent
  7204. * to binding with a wildcard address.
  7205. *
  7206. * This is not currently spelled out in the SCTP sockets
  7207. * extensions draft, but follows the practice as seen in TCP
  7208. * sockets.
  7209. *
  7210. */
  7211. inet_sk_set_state(sk, SCTP_SS_LISTENING);
  7212. if (!ep->base.bind_addr.port) {
  7213. if (sctp_autobind(sk))
  7214. return -EAGAIN;
  7215. } else {
  7216. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  7217. inet_sk_set_state(sk, SCTP_SS_CLOSED);
  7218. return -EADDRINUSE;
  7219. }
  7220. }
  7221. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7222. return sctp_hash_endpoint(ep);
  7223. }
  7224. /*
  7225. * 4.1.3 / 5.1.3 listen()
  7226. *
  7227. * By default, new associations are not accepted for UDP style sockets.
  7228. * An application uses listen() to mark a socket as being able to
  7229. * accept new associations.
  7230. *
  7231. * On TCP style sockets, applications use listen() to ready the SCTP
  7232. * endpoint for accepting inbound associations.
  7233. *
  7234. * On both types of endpoints a backlog of '0' disables listening.
  7235. *
  7236. * Move a socket to LISTENING state.
  7237. */
  7238. int sctp_inet_listen(struct socket *sock, int backlog)
  7239. {
  7240. struct sock *sk = sock->sk;
  7241. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  7242. int err = -EINVAL;
  7243. if (unlikely(backlog < 0))
  7244. return err;
  7245. lock_sock(sk);
  7246. /* Peeled-off sockets are not allowed to listen(). */
  7247. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  7248. goto out;
  7249. if (sock->state != SS_UNCONNECTED)
  7250. goto out;
  7251. if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
  7252. goto out;
  7253. /* If backlog is zero, disable listening. */
  7254. if (!backlog) {
  7255. if (sctp_sstate(sk, CLOSED))
  7256. goto out;
  7257. err = 0;
  7258. sctp_unhash_endpoint(ep);
  7259. sk->sk_state = SCTP_SS_CLOSED;
  7260. if (sk->sk_reuse || sctp_sk(sk)->reuse)
  7261. sctp_sk(sk)->bind_hash->fastreuse = 1;
  7262. goto out;
  7263. }
  7264. /* If we are already listening, just update the backlog */
  7265. if (sctp_sstate(sk, LISTENING))
  7266. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7267. else {
  7268. err = sctp_listen_start(sk, backlog);
  7269. if (err)
  7270. goto out;
  7271. }
  7272. err = 0;
  7273. out:
  7274. release_sock(sk);
  7275. return err;
  7276. }
  7277. /*
  7278. * This function is done by modeling the current datagram_poll() and the
  7279. * tcp_poll(). Note that, based on these implementations, we don't
  7280. * lock the socket in this function, even though it seems that,
  7281. * ideally, locking or some other mechanisms can be used to ensure
  7282. * the integrity of the counters (sndbuf and wmem_alloc) used
  7283. * in this place. We assume that we don't need locks either until proven
  7284. * otherwise.
  7285. *
  7286. * Another thing to note is that we include the Async I/O support
  7287. * here, again, by modeling the current TCP/UDP code. We don't have
  7288. * a good way to test with it yet.
  7289. */
  7290. __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  7291. {
  7292. struct sock *sk = sock->sk;
  7293. struct sctp_sock *sp = sctp_sk(sk);
  7294. __poll_t mask;
  7295. poll_wait(file, sk_sleep(sk), wait);
  7296. sock_rps_record_flow(sk);
  7297. /* A TCP-style listening socket becomes readable when the accept queue
  7298. * is not empty.
  7299. */
  7300. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  7301. return (!list_empty(&sp->ep->asocs)) ?
  7302. (EPOLLIN | EPOLLRDNORM) : 0;
  7303. mask = 0;
  7304. /* Is there any exceptional events? */
  7305. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  7306. mask |= EPOLLERR |
  7307. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  7308. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7309. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  7310. if (sk->sk_shutdown == SHUTDOWN_MASK)
  7311. mask |= EPOLLHUP;
  7312. /* Is it readable? Reconsider this code with TCP-style support. */
  7313. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  7314. mask |= EPOLLIN | EPOLLRDNORM;
  7315. /* The association is either gone or not ready. */
  7316. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  7317. return mask;
  7318. /* Is it writable? */
  7319. if (sctp_writeable(sk)) {
  7320. mask |= EPOLLOUT | EPOLLWRNORM;
  7321. } else {
  7322. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  7323. /*
  7324. * Since the socket is not locked, the buffer
  7325. * might be made available after the writeable check and
  7326. * before the bit is set. This could cause a lost I/O
  7327. * signal. tcp_poll() has a race breaker for this race
  7328. * condition. Based on their implementation, we put
  7329. * in the following code to cover it as well.
  7330. */
  7331. if (sctp_writeable(sk))
  7332. mask |= EPOLLOUT | EPOLLWRNORM;
  7333. }
  7334. return mask;
  7335. }
  7336. /********************************************************************
  7337. * 2nd Level Abstractions
  7338. ********************************************************************/
  7339. static struct sctp_bind_bucket *sctp_bucket_create(
  7340. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  7341. {
  7342. struct sctp_bind_bucket *pp;
  7343. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  7344. if (pp) {
  7345. SCTP_DBG_OBJCNT_INC(bind_bucket);
  7346. pp->port = snum;
  7347. pp->fastreuse = 0;
  7348. INIT_HLIST_HEAD(&pp->owner);
  7349. pp->net = net;
  7350. hlist_add_head(&pp->node, &head->chain);
  7351. }
  7352. return pp;
  7353. }
  7354. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  7355. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  7356. {
  7357. if (pp && hlist_empty(&pp->owner)) {
  7358. __hlist_del(&pp->node);
  7359. kmem_cache_free(sctp_bucket_cachep, pp);
  7360. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  7361. }
  7362. }
  7363. /* Release this socket's reference to a local port. */
  7364. static inline void __sctp_put_port(struct sock *sk)
  7365. {
  7366. struct sctp_bind_hashbucket *head =
  7367. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  7368. inet_sk(sk)->inet_num)];
  7369. struct sctp_bind_bucket *pp;
  7370. spin_lock(&head->lock);
  7371. pp = sctp_sk(sk)->bind_hash;
  7372. __sk_del_bind_node(sk);
  7373. sctp_sk(sk)->bind_hash = NULL;
  7374. inet_sk(sk)->inet_num = 0;
  7375. sctp_bucket_destroy(pp);
  7376. spin_unlock(&head->lock);
  7377. }
  7378. void sctp_put_port(struct sock *sk)
  7379. {
  7380. local_bh_disable();
  7381. __sctp_put_port(sk);
  7382. local_bh_enable();
  7383. }
  7384. /*
  7385. * The system picks an ephemeral port and choose an address set equivalent
  7386. * to binding with a wildcard address.
  7387. * One of those addresses will be the primary address for the association.
  7388. * This automatically enables the multihoming capability of SCTP.
  7389. */
  7390. static int sctp_autobind(struct sock *sk)
  7391. {
  7392. union sctp_addr autoaddr;
  7393. struct sctp_af *af;
  7394. __be16 port;
  7395. /* Initialize a local sockaddr structure to INADDR_ANY. */
  7396. af = sctp_sk(sk)->pf->af;
  7397. port = htons(inet_sk(sk)->inet_num);
  7398. af->inaddr_any(&autoaddr, port);
  7399. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  7400. }
  7401. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  7402. *
  7403. * From RFC 2292
  7404. * 4.2 The cmsghdr Structure *
  7405. *
  7406. * When ancillary data is sent or received, any number of ancillary data
  7407. * objects can be specified by the msg_control and msg_controllen members of
  7408. * the msghdr structure, because each object is preceded by
  7409. * a cmsghdr structure defining the object's length (the cmsg_len member).
  7410. * Historically Berkeley-derived implementations have passed only one object
  7411. * at a time, but this API allows multiple objects to be
  7412. * passed in a single call to sendmsg() or recvmsg(). The following example
  7413. * shows two ancillary data objects in a control buffer.
  7414. *
  7415. * |<--------------------------- msg_controllen -------------------------->|
  7416. * | |
  7417. *
  7418. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  7419. *
  7420. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  7421. * | | |
  7422. *
  7423. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  7424. *
  7425. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  7426. * | | | | |
  7427. *
  7428. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7429. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  7430. *
  7431. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  7432. *
  7433. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7434. * ^
  7435. * |
  7436. *
  7437. * msg_control
  7438. * points here
  7439. */
  7440. static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
  7441. {
  7442. struct msghdr *my_msg = (struct msghdr *)msg;
  7443. struct cmsghdr *cmsg;
  7444. for_each_cmsghdr(cmsg, my_msg) {
  7445. if (!CMSG_OK(my_msg, cmsg))
  7446. return -EINVAL;
  7447. /* Should we parse this header or ignore? */
  7448. if (cmsg->cmsg_level != IPPROTO_SCTP)
  7449. continue;
  7450. /* Strictly check lengths following example in SCM code. */
  7451. switch (cmsg->cmsg_type) {
  7452. case SCTP_INIT:
  7453. /* SCTP Socket API Extension
  7454. * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
  7455. *
  7456. * This cmsghdr structure provides information for
  7457. * initializing new SCTP associations with sendmsg().
  7458. * The SCTP_INITMSG socket option uses this same data
  7459. * structure. This structure is not used for
  7460. * recvmsg().
  7461. *
  7462. * cmsg_level cmsg_type cmsg_data[]
  7463. * ------------ ------------ ----------------------
  7464. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  7465. */
  7466. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
  7467. return -EINVAL;
  7468. cmsgs->init = CMSG_DATA(cmsg);
  7469. break;
  7470. case SCTP_SNDRCV:
  7471. /* SCTP Socket API Extension
  7472. * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
  7473. *
  7474. * This cmsghdr structure specifies SCTP options for
  7475. * sendmsg() and describes SCTP header information
  7476. * about a received message through recvmsg().
  7477. *
  7478. * cmsg_level cmsg_type cmsg_data[]
  7479. * ------------ ------------ ----------------------
  7480. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  7481. */
  7482. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  7483. return -EINVAL;
  7484. cmsgs->srinfo = CMSG_DATA(cmsg);
  7485. if (cmsgs->srinfo->sinfo_flags &
  7486. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7487. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7488. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7489. return -EINVAL;
  7490. break;
  7491. case SCTP_SNDINFO:
  7492. /* SCTP Socket API Extension
  7493. * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
  7494. *
  7495. * This cmsghdr structure specifies SCTP options for
  7496. * sendmsg(). This structure and SCTP_RCVINFO replaces
  7497. * SCTP_SNDRCV which has been deprecated.
  7498. *
  7499. * cmsg_level cmsg_type cmsg_data[]
  7500. * ------------ ------------ ---------------------
  7501. * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
  7502. */
  7503. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
  7504. return -EINVAL;
  7505. cmsgs->sinfo = CMSG_DATA(cmsg);
  7506. if (cmsgs->sinfo->snd_flags &
  7507. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7508. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7509. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7510. return -EINVAL;
  7511. break;
  7512. case SCTP_PRINFO:
  7513. /* SCTP Socket API Extension
  7514. * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
  7515. *
  7516. * This cmsghdr structure specifies SCTP options for sendmsg().
  7517. *
  7518. * cmsg_level cmsg_type cmsg_data[]
  7519. * ------------ ------------ ---------------------
  7520. * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
  7521. */
  7522. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
  7523. return -EINVAL;
  7524. cmsgs->prinfo = CMSG_DATA(cmsg);
  7525. if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
  7526. return -EINVAL;
  7527. if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
  7528. cmsgs->prinfo->pr_value = 0;
  7529. break;
  7530. case SCTP_AUTHINFO:
  7531. /* SCTP Socket API Extension
  7532. * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
  7533. *
  7534. * This cmsghdr structure specifies SCTP options for sendmsg().
  7535. *
  7536. * cmsg_level cmsg_type cmsg_data[]
  7537. * ------------ ------------ ---------------------
  7538. * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
  7539. */
  7540. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
  7541. return -EINVAL;
  7542. cmsgs->authinfo = CMSG_DATA(cmsg);
  7543. break;
  7544. case SCTP_DSTADDRV4:
  7545. case SCTP_DSTADDRV6:
  7546. /* SCTP Socket API Extension
  7547. * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
  7548. *
  7549. * This cmsghdr structure specifies SCTP options for sendmsg().
  7550. *
  7551. * cmsg_level cmsg_type cmsg_data[]
  7552. * ------------ ------------ ---------------------
  7553. * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
  7554. * ------------ ------------ ---------------------
  7555. * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
  7556. */
  7557. cmsgs->addrs_msg = my_msg;
  7558. break;
  7559. default:
  7560. return -EINVAL;
  7561. }
  7562. }
  7563. return 0;
  7564. }
  7565. /*
  7566. * Wait for a packet..
  7567. * Note: This function is the same function as in core/datagram.c
  7568. * with a few modifications to make lksctp work.
  7569. */
  7570. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  7571. {
  7572. int error;
  7573. DEFINE_WAIT(wait);
  7574. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7575. /* Socket errors? */
  7576. error = sock_error(sk);
  7577. if (error)
  7578. goto out;
  7579. if (!skb_queue_empty(&sk->sk_receive_queue))
  7580. goto ready;
  7581. /* Socket shut down? */
  7582. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7583. goto out;
  7584. /* Sequenced packets can come disconnected. If so we report the
  7585. * problem.
  7586. */
  7587. error = -ENOTCONN;
  7588. /* Is there a good reason to think that we may receive some data? */
  7589. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  7590. goto out;
  7591. /* Handle signals. */
  7592. if (signal_pending(current))
  7593. goto interrupted;
  7594. /* Let another process have a go. Since we are going to sleep
  7595. * anyway. Note: This may cause odd behaviors if the message
  7596. * does not fit in the user's buffer, but this seems to be the
  7597. * only way to honor MSG_DONTWAIT realistically.
  7598. */
  7599. release_sock(sk);
  7600. *timeo_p = schedule_timeout(*timeo_p);
  7601. lock_sock(sk);
  7602. ready:
  7603. finish_wait(sk_sleep(sk), &wait);
  7604. return 0;
  7605. interrupted:
  7606. error = sock_intr_errno(*timeo_p);
  7607. out:
  7608. finish_wait(sk_sleep(sk), &wait);
  7609. *err = error;
  7610. return error;
  7611. }
  7612. /* Receive a datagram.
  7613. * Note: This is pretty much the same routine as in core/datagram.c
  7614. * with a few changes to make lksctp work.
  7615. */
  7616. struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  7617. int noblock, int *err)
  7618. {
  7619. int error;
  7620. struct sk_buff *skb;
  7621. long timeo;
  7622. timeo = sock_rcvtimeo(sk, noblock);
  7623. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  7624. MAX_SCHEDULE_TIMEOUT);
  7625. do {
  7626. /* Again only user level code calls this function,
  7627. * so nothing interrupt level
  7628. * will suddenly eat the receive_queue.
  7629. *
  7630. * Look at current nfs client by the way...
  7631. * However, this function was correct in any case. 8)
  7632. */
  7633. if (flags & MSG_PEEK) {
  7634. skb = skb_peek(&sk->sk_receive_queue);
  7635. if (skb)
  7636. refcount_inc(&skb->users);
  7637. } else {
  7638. skb = __skb_dequeue(&sk->sk_receive_queue);
  7639. }
  7640. if (skb)
  7641. return skb;
  7642. /* Caller is allowed not to check sk->sk_err before calling. */
  7643. error = sock_error(sk);
  7644. if (error)
  7645. goto no_packet;
  7646. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7647. break;
  7648. if (sk_can_busy_loop(sk)) {
  7649. sk_busy_loop(sk, noblock);
  7650. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  7651. continue;
  7652. }
  7653. /* User doesn't want to wait. */
  7654. error = -EAGAIN;
  7655. if (!timeo)
  7656. goto no_packet;
  7657. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  7658. return NULL;
  7659. no_packet:
  7660. *err = error;
  7661. return NULL;
  7662. }
  7663. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  7664. static void __sctp_write_space(struct sctp_association *asoc)
  7665. {
  7666. struct sock *sk = asoc->base.sk;
  7667. if (sctp_wspace(asoc) <= 0)
  7668. return;
  7669. if (waitqueue_active(&asoc->wait))
  7670. wake_up_interruptible(&asoc->wait);
  7671. if (sctp_writeable(sk)) {
  7672. struct socket_wq *wq;
  7673. rcu_read_lock();
  7674. wq = rcu_dereference(sk->sk_wq);
  7675. if (wq) {
  7676. if (waitqueue_active(&wq->wait))
  7677. wake_up_interruptible(&wq->wait);
  7678. /* Note that we try to include the Async I/O support
  7679. * here by modeling from the current TCP/UDP code.
  7680. * We have not tested with it yet.
  7681. */
  7682. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  7683. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  7684. }
  7685. rcu_read_unlock();
  7686. }
  7687. }
  7688. static void sctp_wake_up_waiters(struct sock *sk,
  7689. struct sctp_association *asoc)
  7690. {
  7691. struct sctp_association *tmp = asoc;
  7692. /* We do accounting for the sndbuf space per association,
  7693. * so we only need to wake our own association.
  7694. */
  7695. if (asoc->ep->sndbuf_policy)
  7696. return __sctp_write_space(asoc);
  7697. /* If association goes down and is just flushing its
  7698. * outq, then just normally notify others.
  7699. */
  7700. if (asoc->base.dead)
  7701. return sctp_write_space(sk);
  7702. /* Accounting for the sndbuf space is per socket, so we
  7703. * need to wake up others, try to be fair and in case of
  7704. * other associations, let them have a go first instead
  7705. * of just doing a sctp_write_space() call.
  7706. *
  7707. * Note that we reach sctp_wake_up_waiters() only when
  7708. * associations free up queued chunks, thus we are under
  7709. * lock and the list of associations on a socket is
  7710. * guaranteed not to change.
  7711. */
  7712. for (tmp = list_next_entry(tmp, asocs); 1;
  7713. tmp = list_next_entry(tmp, asocs)) {
  7714. /* Manually skip the head element. */
  7715. if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
  7716. continue;
  7717. /* Wake up association. */
  7718. __sctp_write_space(tmp);
  7719. /* We've reached the end. */
  7720. if (tmp == asoc)
  7721. break;
  7722. }
  7723. }
  7724. /* Do accounting for the sndbuf space.
  7725. * Decrement the used sndbuf space of the corresponding association by the
  7726. * data size which was just transmitted(freed).
  7727. */
  7728. static void sctp_wfree(struct sk_buff *skb)
  7729. {
  7730. struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
  7731. struct sctp_association *asoc = chunk->asoc;
  7732. struct sock *sk = asoc->base.sk;
  7733. sk_mem_uncharge(sk, skb->truesize);
  7734. sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
  7735. asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
  7736. WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
  7737. &sk->sk_wmem_alloc));
  7738. if (chunk->shkey) {
  7739. struct sctp_shared_key *shkey = chunk->shkey;
  7740. /* refcnt == 2 and !list_empty mean after this release, it's
  7741. * not being used anywhere, and it's time to notify userland
  7742. * that this shkey can be freed if it's been deactivated.
  7743. */
  7744. if (shkey->deactivated && !list_empty(&shkey->key_list) &&
  7745. refcount_read(&shkey->refcnt) == 2) {
  7746. struct sctp_ulpevent *ev;
  7747. ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
  7748. SCTP_AUTH_FREE_KEY,
  7749. GFP_KERNEL);
  7750. if (ev)
  7751. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  7752. }
  7753. sctp_auth_shkey_release(chunk->shkey);
  7754. }
  7755. sock_wfree(skb);
  7756. sctp_wake_up_waiters(sk, asoc);
  7757. sctp_association_put(asoc);
  7758. }
  7759. /* Do accounting for the receive space on the socket.
  7760. * Accounting for the association is done in ulpevent.c
  7761. * We set this as a destructor for the cloned data skbs so that
  7762. * accounting is done at the correct time.
  7763. */
  7764. void sctp_sock_rfree(struct sk_buff *skb)
  7765. {
  7766. struct sock *sk = skb->sk;
  7767. struct sctp_ulpevent *event = sctp_skb2event(skb);
  7768. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  7769. /*
  7770. * Mimic the behavior of sock_rfree
  7771. */
  7772. sk_mem_uncharge(sk, event->rmem_len);
  7773. }
  7774. /* Helper function to wait for space in the sndbuf. */
  7775. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  7776. size_t msg_len)
  7777. {
  7778. struct sock *sk = asoc->base.sk;
  7779. long current_timeo = *timeo_p;
  7780. DEFINE_WAIT(wait);
  7781. int err = 0;
  7782. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  7783. *timeo_p, msg_len);
  7784. /* Increment the association's refcnt. */
  7785. sctp_association_hold(asoc);
  7786. /* Wait on the association specific sndbuf space. */
  7787. for (;;) {
  7788. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7789. TASK_INTERRUPTIBLE);
  7790. if (asoc->base.dead)
  7791. goto do_dead;
  7792. if (!*timeo_p)
  7793. goto do_nonblock;
  7794. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
  7795. goto do_error;
  7796. if (signal_pending(current))
  7797. goto do_interrupted;
  7798. if (sk_under_memory_pressure(sk))
  7799. sk_mem_reclaim(sk);
  7800. if ((int)msg_len <= sctp_wspace(asoc) &&
  7801. sk_wmem_schedule(sk, msg_len))
  7802. break;
  7803. /* Let another process have a go. Since we are going
  7804. * to sleep anyway.
  7805. */
  7806. release_sock(sk);
  7807. current_timeo = schedule_timeout(current_timeo);
  7808. lock_sock(sk);
  7809. if (sk != asoc->base.sk)
  7810. goto do_error;
  7811. *timeo_p = current_timeo;
  7812. }
  7813. out:
  7814. finish_wait(&asoc->wait, &wait);
  7815. /* Release the association's refcnt. */
  7816. sctp_association_put(asoc);
  7817. return err;
  7818. do_dead:
  7819. err = -ESRCH;
  7820. goto out;
  7821. do_error:
  7822. err = -EPIPE;
  7823. goto out;
  7824. do_interrupted:
  7825. err = sock_intr_errno(*timeo_p);
  7826. goto out;
  7827. do_nonblock:
  7828. err = -EAGAIN;
  7829. goto out;
  7830. }
  7831. void sctp_data_ready(struct sock *sk)
  7832. {
  7833. struct socket_wq *wq;
  7834. rcu_read_lock();
  7835. wq = rcu_dereference(sk->sk_wq);
  7836. if (skwq_has_sleeper(wq))
  7837. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  7838. EPOLLRDNORM | EPOLLRDBAND);
  7839. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  7840. rcu_read_unlock();
  7841. }
  7842. /* If socket sndbuf has changed, wake up all per association waiters. */
  7843. void sctp_write_space(struct sock *sk)
  7844. {
  7845. struct sctp_association *asoc;
  7846. /* Wake up the tasks in each wait queue. */
  7847. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  7848. __sctp_write_space(asoc);
  7849. }
  7850. }
  7851. /* Is there any sndbuf space available on the socket?
  7852. *
  7853. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  7854. * associations on the same socket. For a UDP-style socket with
  7855. * multiple associations, it is possible for it to be "unwriteable"
  7856. * prematurely. I assume that this is acceptable because
  7857. * a premature "unwriteable" is better than an accidental "writeable" which
  7858. * would cause an unwanted block under certain circumstances. For the 1-1
  7859. * UDP-style sockets or TCP-style sockets, this code should work.
  7860. * - Daisy
  7861. */
  7862. static bool sctp_writeable(struct sock *sk)
  7863. {
  7864. return sk->sk_sndbuf > sk->sk_wmem_queued;
  7865. }
  7866. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  7867. * returns immediately with EINPROGRESS.
  7868. */
  7869. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  7870. {
  7871. struct sock *sk = asoc->base.sk;
  7872. int err = 0;
  7873. long current_timeo = *timeo_p;
  7874. DEFINE_WAIT(wait);
  7875. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  7876. /* Increment the association's refcnt. */
  7877. sctp_association_hold(asoc);
  7878. for (;;) {
  7879. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7880. TASK_INTERRUPTIBLE);
  7881. if (!*timeo_p)
  7882. goto do_nonblock;
  7883. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7884. break;
  7885. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  7886. asoc->base.dead)
  7887. goto do_error;
  7888. if (signal_pending(current))
  7889. goto do_interrupted;
  7890. if (sctp_state(asoc, ESTABLISHED))
  7891. break;
  7892. /* Let another process have a go. Since we are going
  7893. * to sleep anyway.
  7894. */
  7895. release_sock(sk);
  7896. current_timeo = schedule_timeout(current_timeo);
  7897. lock_sock(sk);
  7898. *timeo_p = current_timeo;
  7899. }
  7900. out:
  7901. finish_wait(&asoc->wait, &wait);
  7902. /* Release the association's refcnt. */
  7903. sctp_association_put(asoc);
  7904. return err;
  7905. do_error:
  7906. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  7907. err = -ETIMEDOUT;
  7908. else
  7909. err = -ECONNREFUSED;
  7910. goto out;
  7911. do_interrupted:
  7912. err = sock_intr_errno(*timeo_p);
  7913. goto out;
  7914. do_nonblock:
  7915. err = -EINPROGRESS;
  7916. goto out;
  7917. }
  7918. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  7919. {
  7920. struct sctp_endpoint *ep;
  7921. int err = 0;
  7922. DEFINE_WAIT(wait);
  7923. ep = sctp_sk(sk)->ep;
  7924. for (;;) {
  7925. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  7926. TASK_INTERRUPTIBLE);
  7927. if (list_empty(&ep->asocs)) {
  7928. release_sock(sk);
  7929. timeo = schedule_timeout(timeo);
  7930. lock_sock(sk);
  7931. }
  7932. err = -EINVAL;
  7933. if (!sctp_sstate(sk, LISTENING))
  7934. break;
  7935. err = 0;
  7936. if (!list_empty(&ep->asocs))
  7937. break;
  7938. err = sock_intr_errno(timeo);
  7939. if (signal_pending(current))
  7940. break;
  7941. err = -EAGAIN;
  7942. if (!timeo)
  7943. break;
  7944. }
  7945. finish_wait(sk_sleep(sk), &wait);
  7946. return err;
  7947. }
  7948. static void sctp_wait_for_close(struct sock *sk, long timeout)
  7949. {
  7950. DEFINE_WAIT(wait);
  7951. do {
  7952. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7953. if (list_empty(&sctp_sk(sk)->ep->asocs))
  7954. break;
  7955. release_sock(sk);
  7956. timeout = schedule_timeout(timeout);
  7957. lock_sock(sk);
  7958. } while (!signal_pending(current) && timeout);
  7959. finish_wait(sk_sleep(sk), &wait);
  7960. }
  7961. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  7962. {
  7963. struct sk_buff *frag;
  7964. if (!skb->data_len)
  7965. goto done;
  7966. /* Don't forget the fragments. */
  7967. skb_walk_frags(skb, frag)
  7968. sctp_skb_set_owner_r_frag(frag, sk);
  7969. done:
  7970. sctp_skb_set_owner_r(skb, sk);
  7971. }
  7972. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  7973. struct sctp_association *asoc)
  7974. {
  7975. struct inet_sock *inet = inet_sk(sk);
  7976. struct inet_sock *newinet;
  7977. struct sctp_sock *sp = sctp_sk(sk);
  7978. struct sctp_endpoint *ep = sp->ep;
  7979. newsk->sk_type = sk->sk_type;
  7980. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  7981. newsk->sk_flags = sk->sk_flags;
  7982. newsk->sk_tsflags = sk->sk_tsflags;
  7983. newsk->sk_no_check_tx = sk->sk_no_check_tx;
  7984. newsk->sk_no_check_rx = sk->sk_no_check_rx;
  7985. newsk->sk_reuse = sk->sk_reuse;
  7986. sctp_sk(newsk)->reuse = sp->reuse;
  7987. newsk->sk_shutdown = sk->sk_shutdown;
  7988. newsk->sk_destruct = sctp_destruct_sock;
  7989. newsk->sk_family = sk->sk_family;
  7990. newsk->sk_protocol = IPPROTO_SCTP;
  7991. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  7992. newsk->sk_sndbuf = sk->sk_sndbuf;
  7993. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  7994. newsk->sk_lingertime = sk->sk_lingertime;
  7995. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  7996. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  7997. newsk->sk_rxhash = sk->sk_rxhash;
  7998. newinet = inet_sk(newsk);
  7999. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  8000. * getsockname() and getpeername()
  8001. */
  8002. newinet->inet_sport = inet->inet_sport;
  8003. newinet->inet_saddr = inet->inet_saddr;
  8004. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  8005. newinet->inet_dport = htons(asoc->peer.port);
  8006. newinet->pmtudisc = inet->pmtudisc;
  8007. newinet->inet_id = prandom_u32();
  8008. newinet->uc_ttl = inet->uc_ttl;
  8009. newinet->mc_loop = 1;
  8010. newinet->mc_ttl = 1;
  8011. newinet->mc_index = 0;
  8012. newinet->mc_list = NULL;
  8013. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  8014. net_enable_timestamp();
  8015. /* Set newsk security attributes from orginal sk and connection
  8016. * security attribute from ep.
  8017. */
  8018. security_sctp_sk_clone(ep, sk, newsk);
  8019. }
  8020. static inline void sctp_copy_descendant(struct sock *sk_to,
  8021. const struct sock *sk_from)
  8022. {
  8023. int ancestor_size = sizeof(struct inet_sock) +
  8024. sizeof(struct sctp_sock) -
  8025. offsetof(struct sctp_sock, pd_lobby);
  8026. if (sk_from->sk_family == PF_INET6)
  8027. ancestor_size += sizeof(struct ipv6_pinfo);
  8028. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  8029. }
  8030. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  8031. * and its messages to the newsk.
  8032. */
  8033. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  8034. struct sctp_association *assoc,
  8035. enum sctp_socket_type type)
  8036. {
  8037. struct sctp_sock *oldsp = sctp_sk(oldsk);
  8038. struct sctp_sock *newsp = sctp_sk(newsk);
  8039. struct sctp_bind_bucket *pp; /* hash list port iterator */
  8040. struct sctp_endpoint *newep = newsp->ep;
  8041. struct sk_buff *skb, *tmp;
  8042. struct sctp_ulpevent *event;
  8043. struct sctp_bind_hashbucket *head;
  8044. int err;
  8045. /* Migrate socket buffer sizes and all the socket level options to the
  8046. * new socket.
  8047. */
  8048. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  8049. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  8050. /* Brute force copy old sctp opt. */
  8051. sctp_copy_descendant(newsk, oldsk);
  8052. /* Restore the ep value that was overwritten with the above structure
  8053. * copy.
  8054. */
  8055. newsp->ep = newep;
  8056. newsp->hmac = NULL;
  8057. /* Hook this new socket in to the bind_hash list. */
  8058. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  8059. inet_sk(oldsk)->inet_num)];
  8060. spin_lock_bh(&head->lock);
  8061. pp = sctp_sk(oldsk)->bind_hash;
  8062. sk_add_bind_node(newsk, &pp->owner);
  8063. sctp_sk(newsk)->bind_hash = pp;
  8064. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  8065. spin_unlock_bh(&head->lock);
  8066. /* Copy the bind_addr list from the original endpoint to the new
  8067. * endpoint so that we can handle restarts properly
  8068. */
  8069. err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  8070. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  8071. if (err)
  8072. return err;
  8073. /* New ep's auth_hmacs should be set if old ep's is set, in case
  8074. * that net->sctp.auth_enable has been changed to 0 by users and
  8075. * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
  8076. */
  8077. if (oldsp->ep->auth_hmacs) {
  8078. err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
  8079. if (err)
  8080. return err;
  8081. }
  8082. /* Move any messages in the old socket's receive queue that are for the
  8083. * peeled off association to the new socket's receive queue.
  8084. */
  8085. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  8086. event = sctp_skb2event(skb);
  8087. if (event->asoc == assoc) {
  8088. __skb_unlink(skb, &oldsk->sk_receive_queue);
  8089. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  8090. sctp_skb_set_owner_r_frag(skb, newsk);
  8091. }
  8092. }
  8093. /* Clean up any messages pending delivery due to partial
  8094. * delivery. Three cases:
  8095. * 1) No partial deliver; no work.
  8096. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  8097. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  8098. */
  8099. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  8100. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  8101. struct sk_buff_head *queue;
  8102. /* Decide which queue to move pd_lobby skbs to. */
  8103. if (assoc->ulpq.pd_mode) {
  8104. queue = &newsp->pd_lobby;
  8105. } else
  8106. queue = &newsk->sk_receive_queue;
  8107. /* Walk through the pd_lobby, looking for skbs that
  8108. * need moved to the new socket.
  8109. */
  8110. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  8111. event = sctp_skb2event(skb);
  8112. if (event->asoc == assoc) {
  8113. __skb_unlink(skb, &oldsp->pd_lobby);
  8114. __skb_queue_tail(queue, skb);
  8115. sctp_skb_set_owner_r_frag(skb, newsk);
  8116. }
  8117. }
  8118. /* Clear up any skbs waiting for the partial
  8119. * delivery to finish.
  8120. */
  8121. if (assoc->ulpq.pd_mode)
  8122. sctp_clear_pd(oldsk, NULL);
  8123. }
  8124. sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
  8125. /* Set the type of socket to indicate that it is peeled off from the
  8126. * original UDP-style socket or created with the accept() call on a
  8127. * TCP-style socket..
  8128. */
  8129. newsp->type = type;
  8130. /* Mark the new socket "in-use" by the user so that any packets
  8131. * that may arrive on the association after we've moved it are
  8132. * queued to the backlog. This prevents a potential race between
  8133. * backlog processing on the old socket and new-packet processing
  8134. * on the new socket.
  8135. *
  8136. * The caller has just allocated newsk so we can guarantee that other
  8137. * paths won't try to lock it and then oldsk.
  8138. */
  8139. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  8140. sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
  8141. sctp_assoc_migrate(assoc, newsk);
  8142. sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
  8143. /* If the association on the newsk is already closed before accept()
  8144. * is called, set RCV_SHUTDOWN flag.
  8145. */
  8146. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
  8147. inet_sk_set_state(newsk, SCTP_SS_CLOSED);
  8148. newsk->sk_shutdown |= RCV_SHUTDOWN;
  8149. } else {
  8150. inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
  8151. }
  8152. release_sock(newsk);
  8153. return 0;
  8154. }
  8155. /* This proto struct describes the ULP interface for SCTP. */
  8156. struct proto sctp_prot = {
  8157. .name = "SCTP",
  8158. .owner = THIS_MODULE,
  8159. .close = sctp_close,
  8160. .disconnect = sctp_disconnect,
  8161. .accept = sctp_accept,
  8162. .ioctl = sctp_ioctl,
  8163. .init = sctp_init_sock,
  8164. .destroy = sctp_destroy_sock,
  8165. .shutdown = sctp_shutdown,
  8166. .setsockopt = sctp_setsockopt,
  8167. .getsockopt = sctp_getsockopt,
  8168. .sendmsg = sctp_sendmsg,
  8169. .recvmsg = sctp_recvmsg,
  8170. .bind = sctp_bind,
  8171. .backlog_rcv = sctp_backlog_rcv,
  8172. .hash = sctp_hash,
  8173. .unhash = sctp_unhash,
  8174. .no_autobind = true,
  8175. .obj_size = sizeof(struct sctp_sock),
  8176. .useroffset = offsetof(struct sctp_sock, subscribe),
  8177. .usersize = offsetof(struct sctp_sock, initmsg) -
  8178. offsetof(struct sctp_sock, subscribe) +
  8179. sizeof_field(struct sctp_sock, initmsg),
  8180. .sysctl_mem = sysctl_sctp_mem,
  8181. .sysctl_rmem = sysctl_sctp_rmem,
  8182. .sysctl_wmem = sysctl_sctp_wmem,
  8183. .memory_pressure = &sctp_memory_pressure,
  8184. .enter_memory_pressure = sctp_enter_memory_pressure,
  8185. .memory_allocated = &sctp_memory_allocated,
  8186. .sockets_allocated = &sctp_sockets_allocated,
  8187. };
  8188. #if IS_ENABLED(CONFIG_IPV6)
  8189. #include <net/transp_v6.h>
  8190. static void sctp_v6_destroy_sock(struct sock *sk)
  8191. {
  8192. sctp_destroy_sock(sk);
  8193. inet6_destroy_sock(sk);
  8194. }
  8195. struct proto sctpv6_prot = {
  8196. .name = "SCTPv6",
  8197. .owner = THIS_MODULE,
  8198. .close = sctp_close,
  8199. .disconnect = sctp_disconnect,
  8200. .accept = sctp_accept,
  8201. .ioctl = sctp_ioctl,
  8202. .init = sctp_init_sock,
  8203. .destroy = sctp_v6_destroy_sock,
  8204. .shutdown = sctp_shutdown,
  8205. .setsockopt = sctp_setsockopt,
  8206. .getsockopt = sctp_getsockopt,
  8207. .sendmsg = sctp_sendmsg,
  8208. .recvmsg = sctp_recvmsg,
  8209. .bind = sctp_bind,
  8210. .backlog_rcv = sctp_backlog_rcv,
  8211. .hash = sctp_hash,
  8212. .unhash = sctp_unhash,
  8213. .no_autobind = true,
  8214. .obj_size = sizeof(struct sctp6_sock),
  8215. .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
  8216. .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
  8217. offsetof(struct sctp6_sock, sctp.subscribe) +
  8218. sizeof_field(struct sctp6_sock, sctp.initmsg),
  8219. .sysctl_mem = sysctl_sctp_mem,
  8220. .sysctl_rmem = sysctl_sctp_rmem,
  8221. .sysctl_wmem = sysctl_sctp_wmem,
  8222. .memory_pressure = &sctp_memory_pressure,
  8223. .enter_memory_pressure = sctp_enter_memory_pressure,
  8224. .memory_allocated = &sctp_memory_allocated,
  8225. .sockets_allocated = &sctp_sockets_allocated,
  8226. };
  8227. #endif /* IS_ENABLED(CONFIG_IPV6) */