1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * This SCTP implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * This SCTP implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
34 * Please send any bug reports or fixes you make to the
36 * lksctp developers <linux-sctp@vger.kernel.org>
38 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 #include <crypto/hash.h>
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/wait.h>
59 #include <linux/time.h>
60 #include <linux/sched/signal.h>
62 #include <linux/capability.h>
63 #include <linux/fcntl.h>
64 #include <linux/poll.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/file.h>
68 #include <linux/compat.h>
69 #include <linux/rhashtable.h>
73 #include <net/route.h>
75 #include <net/inet_common.h>
76 #include <net/busy_poll.h>
78 #include <linux/socket.h> /* for sa_family_t */
79 #include <linux/export.h>
81 #include <net/sctp/sctp.h>
82 #include <net/sctp/sm.h>
83 #include <net/sctp/stream_sched.h>
85 /* Forward declarations for internal helper functions. */
86 static bool sctp_writeable(struct sock *sk);
87 static void sctp_wfree(struct sk_buff *skb);
88 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
90 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
91 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92 static int sctp_wait_for_accept(struct sock *sk, long timeo);
93 static void sctp_wait_for_close(struct sock *sk, long timeo);
94 static void sctp_destruct_sock(struct sock *sk);
95 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104 static int sctp_autobind(struct sock *sk);
105 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
106 struct sctp_association *assoc,
107 enum sctp_socket_type type);
109 static unsigned long sctp_memory_pressure;
110 static atomic_long_t sctp_memory_allocated;
111 struct percpu_counter sctp_sockets_allocated;
113 static void sctp_enter_memory_pressure(struct sock *sk)
115 sctp_memory_pressure = 1;
119 /* Get the sndbuf space available at the time on the association. */
120 static inline int sctp_wspace(struct sctp_association *asoc)
122 struct sock *sk = asoc->base.sk;
124 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
125 : sk_stream_wspace(sk);
128 /* Increment the used sndbuf space count of the corresponding association by
129 * the size of the outgoing data chunk.
130 * Also, set the skb destructor for sndbuf accounting later.
132 * Since it is always 1-1 between chunk and skb, and also a new skb is always
133 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
134 * destructor in the data chunk skb for the purpose of the sndbuf space
137 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
139 struct sctp_association *asoc = chunk->asoc;
140 struct sock *sk = asoc->base.sk;
142 /* The sndbuf space is tracked per association. */
143 sctp_association_hold(asoc);
146 sctp_auth_shkey_hold(chunk->shkey);
148 skb_set_owner_w(chunk->skb, sk);
150 chunk->skb->destructor = sctp_wfree;
151 /* Save the chunk pointer in skb for sctp_wfree to use later. */
152 skb_shinfo(chunk->skb)->destructor_arg = chunk;
154 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
155 sizeof(struct sk_buff) +
156 sizeof(struct sctp_chunk);
158 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
159 sk->sk_wmem_queued += chunk->skb->truesize;
160 sk_mem_charge(sk, chunk->skb->truesize);
163 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
165 skb_orphan(chunk->skb);
168 #define traverse_and_process() \
171 if (msg == prev_msg) \
173 list_for_each_entry(c, &msg->chunks, frag_list) { \
174 if ((clear && asoc->base.sk == c->skb->sk) || \
175 (!clear && asoc->base.sk != c->skb->sk)) \
181 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
183 void (*cb)(struct sctp_chunk *))
186 struct sctp_datamsg *msg, *prev_msg = NULL;
187 struct sctp_outq *q = &asoc->outqueue;
188 struct sctp_chunk *chunk, *c;
189 struct sctp_transport *t;
191 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
192 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
193 traverse_and_process();
195 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
196 traverse_and_process();
198 list_for_each_entry(chunk, &q->sacked, transmitted_list)
199 traverse_and_process();
201 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
202 traverse_and_process();
204 list_for_each_entry(chunk, &q->out_chunk_list, list)
205 traverse_and_process();
208 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
209 void (*cb)(struct sk_buff *, struct sock *))
212 struct sk_buff *skb, *tmp;
214 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
217 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
220 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
224 /* Verify that this is a valid address. */
225 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
230 /* Verify basic sockaddr. */
231 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
235 /* Is this a valid SCTP address? */
236 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
239 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
245 /* Look up the association by its id. If this is not a UDP-style
246 * socket, the ID field is always ignored.
248 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
250 struct sctp_association *asoc = NULL;
252 /* If this is not a UDP-style socket, assoc id should be ignored. */
253 if (!sctp_style(sk, UDP)) {
254 /* Return NULL if the socket state is not ESTABLISHED. It
255 * could be a TCP-style listening socket or a socket which
256 * hasn't yet called connect() to establish an association.
258 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
261 /* Get the first and the only association from the list. */
262 if (!list_empty(&sctp_sk(sk)->ep->asocs))
263 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
264 struct sctp_association, asocs);
268 /* Otherwise this is a UDP-style socket. */
269 if (!id || (id == (sctp_assoc_t)-1))
272 spin_lock_bh(&sctp_assocs_id_lock);
273 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
274 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
276 spin_unlock_bh(&sctp_assocs_id_lock);
281 /* Look up the transport from an address and an assoc id. If both address and
282 * id are specified, the associations matching the address and the id should be
285 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
286 struct sockaddr_storage *addr,
289 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
290 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
291 union sctp_addr *laddr = (union sctp_addr *)addr;
292 struct sctp_transport *transport;
294 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
297 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
304 id_asoc = sctp_id2assoc(sk, id);
305 if (id_asoc && (id_asoc != addr_asoc))
308 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
309 (union sctp_addr *)addr);
314 /* API 3.1.2 bind() - UDP Style Syntax
315 * The syntax of bind() is,
317 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
319 * sd - the socket descriptor returned by socket().
320 * addr - the address structure (struct sockaddr_in or struct
321 * sockaddr_in6 [RFC 2553]),
322 * addr_len - the size of the address structure.
324 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
330 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
333 /* Disallow binding twice. */
334 if (!sctp_sk(sk)->ep->base.bind_addr.port)
335 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
345 static long sctp_get_port_local(struct sock *, union sctp_addr *);
347 /* Verify this is a valid sockaddr. */
348 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
349 union sctp_addr *addr, int len)
353 /* Check minimum size. */
354 if (len < sizeof (struct sockaddr))
357 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
360 if (addr->sa.sa_family == AF_INET6) {
361 if (len < SIN6_LEN_RFC2133)
363 /* V4 mapped address are really of AF_INET family */
364 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
365 !opt->pf->af_supported(AF_INET, opt))
369 /* If we get this far, af is valid. */
370 af = sctp_get_af_specific(addr->sa.sa_family);
372 if (len < af->sockaddr_len)
378 static void sctp_auto_asconf_init(struct sctp_sock *sp)
380 struct net *net = sock_net(&sp->inet.sk);
382 if (net->sctp.default_auto_asconf) {
383 spin_lock(&net->sctp.addr_wq_lock);
384 list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
385 spin_unlock(&net->sctp.addr_wq_lock);
386 sp->do_auto_asconf = 1;
390 /* Bind a local address either to an endpoint or to an association. */
391 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
393 struct net *net = sock_net(sk);
394 struct sctp_sock *sp = sctp_sk(sk);
395 struct sctp_endpoint *ep = sp->ep;
396 struct sctp_bind_addr *bp = &ep->base.bind_addr;
401 /* Common sockaddr verification. */
402 af = sctp_sockaddr_af(sp, addr, len);
404 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
405 __func__, sk, addr, len);
409 snum = ntohs(addr->v4.sin_port);
411 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
412 __func__, sk, &addr->sa, bp->port, snum, len);
414 /* PF specific bind() address verification. */
415 if (!sp->pf->bind_verify(sp, addr))
416 return -EADDRNOTAVAIL;
418 /* We must either be unbound, or bind to the same port.
419 * It's OK to allow 0 ports if we are already bound.
420 * We'll just inhert an already bound port in this case
425 else if (snum != bp->port) {
426 pr_debug("%s: new port %d doesn't match existing port "
427 "%d\n", __func__, snum, bp->port);
432 if (snum && snum < inet_prot_sock(net) &&
433 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
436 /* See if the address matches any of the addresses we may have
437 * already bound before checking against other endpoints.
439 if (sctp_bind_addr_match(bp, addr, sp))
442 /* Make sure we are allowed to bind here.
443 * The function sctp_get_port_local() does duplicate address
446 addr->v4.sin_port = htons(snum);
447 if ((ret = sctp_get_port_local(sk, addr))) {
451 /* Refresh ephemeral port. */
453 bp->port = inet_sk(sk)->inet_num;
454 sctp_auto_asconf_init(sp);
457 /* Add the address to the bind address list.
458 * Use GFP_ATOMIC since BHs will be disabled.
460 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
461 SCTP_ADDR_SRC, GFP_ATOMIC);
463 /* Copy back into socket for getsockname() use. */
465 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
466 sp->pf->to_sk_saddr(addr, sk);
472 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
474 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
475 * at any one time. If a sender, after sending an ASCONF chunk, decides
476 * it needs to transfer another ASCONF Chunk, it MUST wait until the
477 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
478 * subsequent ASCONF. Note this restriction binds each side, so at any
479 * time two ASCONF may be in-transit on any given association (one sent
480 * from each endpoint).
482 static int sctp_send_asconf(struct sctp_association *asoc,
483 struct sctp_chunk *chunk)
485 struct net *net = sock_net(asoc->base.sk);
488 /* If there is an outstanding ASCONF chunk, queue it for later
491 if (asoc->addip_last_asconf) {
492 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
496 /* Hold the chunk until an ASCONF_ACK is received. */
497 sctp_chunk_hold(chunk);
498 retval = sctp_primitive_ASCONF(net, asoc, chunk);
500 sctp_chunk_free(chunk);
502 asoc->addip_last_asconf = chunk;
508 /* Add a list of addresses as bind addresses to local endpoint or
511 * Basically run through each address specified in the addrs/addrcnt
512 * array/length pair, determine if it is IPv6 or IPv4 and call
513 * sctp_do_bind() on it.
515 * If any of them fails, then the operation will be reversed and the
516 * ones that were added will be removed.
518 * Only sctp_setsockopt_bindx() is supposed to call this function.
520 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
525 struct sockaddr *sa_addr;
528 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
532 for (cnt = 0; cnt < addrcnt; cnt++) {
533 /* The list may contain either IPv4 or IPv6 address;
534 * determine the address length for walking thru the list.
537 af = sctp_get_af_specific(sa_addr->sa_family);
543 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
546 addr_buf += af->sockaddr_len;
550 /* Failed. Cleanup the ones that have been added */
552 sctp_bindx_rem(sk, addrs, cnt);
560 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
561 * associations that are part of the endpoint indicating that a list of local
562 * addresses are added to the endpoint.
564 * If any of the addresses is already in the bind address list of the
565 * association, we do not send the chunk for that association. But it will not
566 * affect other associations.
568 * Only sctp_setsockopt_bindx() is supposed to call this function.
570 static int sctp_send_asconf_add_ip(struct sock *sk,
571 struct sockaddr *addrs,
574 struct net *net = sock_net(sk);
575 struct sctp_sock *sp;
576 struct sctp_endpoint *ep;
577 struct sctp_association *asoc;
578 struct sctp_bind_addr *bp;
579 struct sctp_chunk *chunk;
580 struct sctp_sockaddr_entry *laddr;
581 union sctp_addr *addr;
582 union sctp_addr saveaddr;
589 if (!net->sctp.addip_enable)
595 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
596 __func__, sk, addrs, addrcnt);
598 list_for_each_entry(asoc, &ep->asocs, asocs) {
599 if (!asoc->peer.asconf_capable)
602 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
605 if (!sctp_state(asoc, ESTABLISHED))
608 /* Check if any address in the packed array of addresses is
609 * in the bind address list of the association. If so,
610 * do not send the asconf chunk to its peer, but continue with
611 * other associations.
614 for (i = 0; i < addrcnt; i++) {
616 af = sctp_get_af_specific(addr->v4.sin_family);
622 if (sctp_assoc_lookup_laddr(asoc, addr))
625 addr_buf += af->sockaddr_len;
630 /* Use the first valid address in bind addr list of
631 * association as Address Parameter of ASCONF CHUNK.
633 bp = &asoc->base.bind_addr;
634 p = bp->address_list.next;
635 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
636 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
637 addrcnt, SCTP_PARAM_ADD_IP);
643 /* Add the new addresses to the bind address list with
644 * use_as_src set to 0.
647 for (i = 0; i < addrcnt; i++) {
649 af = sctp_get_af_specific(addr->v4.sin_family);
650 memcpy(&saveaddr, addr, af->sockaddr_len);
651 retval = sctp_add_bind_addr(bp, &saveaddr,
653 SCTP_ADDR_NEW, GFP_ATOMIC);
654 addr_buf += af->sockaddr_len;
656 if (asoc->src_out_of_asoc_ok) {
657 struct sctp_transport *trans;
659 list_for_each_entry(trans,
660 &asoc->peer.transport_addr_list, transports) {
661 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
662 2*asoc->pathmtu, 4380));
663 trans->ssthresh = asoc->peer.i.a_rwnd;
664 trans->rto = asoc->rto_initial;
665 sctp_max_rto(asoc, trans);
666 trans->rtt = trans->srtt = trans->rttvar = 0;
667 /* Clear the source and route cache */
668 sctp_transport_route(trans, NULL,
669 sctp_sk(asoc->base.sk));
672 retval = sctp_send_asconf(asoc, chunk);
679 /* Remove a list of addresses from bind addresses list. Do not remove the
682 * Basically run through each address specified in the addrs/addrcnt
683 * array/length pair, determine if it is IPv6 or IPv4 and call
684 * sctp_del_bind() on it.
686 * If any of them fails, then the operation will be reversed and the
687 * ones that were removed will be added back.
689 * At least one address has to be left; if only one address is
690 * available, the operation will return -EBUSY.
692 * Only sctp_setsockopt_bindx() is supposed to call this function.
694 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
696 struct sctp_sock *sp = sctp_sk(sk);
697 struct sctp_endpoint *ep = sp->ep;
699 struct sctp_bind_addr *bp = &ep->base.bind_addr;
702 union sctp_addr *sa_addr;
705 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
706 __func__, sk, addrs, addrcnt);
709 for (cnt = 0; cnt < addrcnt; cnt++) {
710 /* If the bind address list is empty or if there is only one
711 * bind address, there is nothing more to be removed (we need
712 * at least one address here).
714 if (list_empty(&bp->address_list) ||
715 (sctp_list_single_entry(&bp->address_list))) {
721 af = sctp_get_af_specific(sa_addr->sa.sa_family);
727 if (!af->addr_valid(sa_addr, sp, NULL)) {
728 retval = -EADDRNOTAVAIL;
732 if (sa_addr->v4.sin_port &&
733 sa_addr->v4.sin_port != htons(bp->port)) {
738 if (!sa_addr->v4.sin_port)
739 sa_addr->v4.sin_port = htons(bp->port);
741 /* FIXME - There is probably a need to check if sk->sk_saddr and
742 * sk->sk_rcv_addr are currently set to one of the addresses to
743 * be removed. This is something which needs to be looked into
744 * when we are fixing the outstanding issues with multi-homing
745 * socket routing and failover schemes. Refer to comments in
746 * sctp_do_bind(). -daisy
748 retval = sctp_del_bind_addr(bp, sa_addr);
750 addr_buf += af->sockaddr_len;
753 /* Failed. Add the ones that has been removed back */
755 sctp_bindx_add(sk, addrs, cnt);
763 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
764 * the associations that are part of the endpoint indicating that a list of
765 * local addresses are removed from the endpoint.
767 * If any of the addresses is already in the bind address list of the
768 * association, we do not send the chunk for that association. But it will not
769 * affect other associations.
771 * Only sctp_setsockopt_bindx() is supposed to call this function.
773 static int sctp_send_asconf_del_ip(struct sock *sk,
774 struct sockaddr *addrs,
777 struct net *net = sock_net(sk);
778 struct sctp_sock *sp;
779 struct sctp_endpoint *ep;
780 struct sctp_association *asoc;
781 struct sctp_transport *transport;
782 struct sctp_bind_addr *bp;
783 struct sctp_chunk *chunk;
784 union sctp_addr *laddr;
787 struct sctp_sockaddr_entry *saddr;
793 if (!net->sctp.addip_enable)
799 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
800 __func__, sk, addrs, addrcnt);
802 list_for_each_entry(asoc, &ep->asocs, asocs) {
804 if (!asoc->peer.asconf_capable)
807 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
810 if (!sctp_state(asoc, ESTABLISHED))
813 /* Check if any address in the packed array of addresses is
814 * not present in the bind address list of the association.
815 * If so, do not send the asconf chunk to its peer, but
816 * continue with other associations.
819 for (i = 0; i < addrcnt; i++) {
821 af = sctp_get_af_specific(laddr->v4.sin_family);
827 if (!sctp_assoc_lookup_laddr(asoc, laddr))
830 addr_buf += af->sockaddr_len;
835 /* Find one address in the association's bind address list
836 * that is not in the packed array of addresses. This is to
837 * make sure that we do not delete all the addresses in the
840 bp = &asoc->base.bind_addr;
841 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
843 if ((laddr == NULL) && (addrcnt == 1)) {
844 if (asoc->asconf_addr_del_pending)
846 asoc->asconf_addr_del_pending =
847 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
848 if (asoc->asconf_addr_del_pending == NULL) {
852 asoc->asconf_addr_del_pending->sa.sa_family =
854 asoc->asconf_addr_del_pending->v4.sin_port =
856 if (addrs->sa_family == AF_INET) {
857 struct sockaddr_in *sin;
859 sin = (struct sockaddr_in *)addrs;
860 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
861 } else if (addrs->sa_family == AF_INET6) {
862 struct sockaddr_in6 *sin6;
864 sin6 = (struct sockaddr_in6 *)addrs;
865 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
868 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
869 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
870 asoc->asconf_addr_del_pending);
872 asoc->src_out_of_asoc_ok = 1;
880 /* We do not need RCU protection throughout this loop
881 * because this is done under a socket lock from the
884 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
892 /* Reset use_as_src flag for the addresses in the bind address
893 * list that are to be deleted.
896 for (i = 0; i < addrcnt; i++) {
898 af = sctp_get_af_specific(laddr->v4.sin_family);
899 list_for_each_entry(saddr, &bp->address_list, list) {
900 if (sctp_cmp_addr_exact(&saddr->a, laddr))
901 saddr->state = SCTP_ADDR_DEL;
903 addr_buf += af->sockaddr_len;
906 /* Update the route and saddr entries for all the transports
907 * as some of the addresses in the bind address list are
908 * about to be deleted and cannot be used as source addresses.
910 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
912 sctp_transport_route(transport, NULL,
913 sctp_sk(asoc->base.sk));
917 /* We don't need to transmit ASCONF */
919 retval = sctp_send_asconf(asoc, chunk);
925 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
926 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
928 struct sock *sk = sctp_opt2sk(sp);
929 union sctp_addr *addr;
932 /* It is safe to write port space in caller. */
934 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
935 af = sctp_get_af_specific(addr->sa.sa_family);
938 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
941 if (addrw->state == SCTP_ADDR_NEW)
942 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
944 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
947 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
950 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
953 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
954 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
957 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
958 * Section 3.1.2 for this usage.
960 * addrs is a pointer to an array of one or more socket addresses. Each
961 * address is contained in its appropriate structure (i.e. struct
962 * sockaddr_in or struct sockaddr_in6) the family of the address type
963 * must be used to distinguish the address length (note that this
964 * representation is termed a "packed array" of addresses). The caller
965 * specifies the number of addresses in the array with addrcnt.
967 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
968 * -1, and sets errno to the appropriate error code.
970 * For SCTP, the port given in each socket address must be the same, or
971 * sctp_bindx() will fail, setting errno to EINVAL.
973 * The flags parameter is formed from the bitwise OR of zero or more of
974 * the following currently defined flags:
976 * SCTP_BINDX_ADD_ADDR
978 * SCTP_BINDX_REM_ADDR
980 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
981 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
982 * addresses from the association. The two flags are mutually exclusive;
983 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
984 * not remove all addresses from an association; sctp_bindx() will
985 * reject such an attempt with EINVAL.
987 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
988 * additional addresses with an endpoint after calling bind(). Or use
989 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
990 * socket is associated with so that no new association accepted will be
991 * associated with those addresses. If the endpoint supports dynamic
992 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
993 * endpoint to send the appropriate message to the peer to change the
994 * peers address lists.
996 * Adding and removing addresses from a connected association is
997 * optional functionality. Implementations that do not support this
998 * functionality should return EOPNOTSUPP.
1000 * Basically do nothing but copying the addresses from user to kernel
1001 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
1002 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
1005 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1008 * sk The sk of the socket
1009 * addrs The pointer to the addresses in user land
1010 * addrssize Size of the addrs buffer
1011 * op Operation to perform (add or remove, see the flags of
1014 * Returns 0 if ok, <0 errno code on error.
1016 static int sctp_setsockopt_bindx(struct sock *sk,
1017 struct sockaddr __user *addrs,
1018 int addrs_size, int op)
1020 struct sockaddr *kaddrs;
1024 struct sockaddr *sa_addr;
1028 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1029 __func__, sk, addrs, addrs_size, op);
1031 if (unlikely(addrs_size <= 0))
1034 kaddrs = memdup_user(addrs, addrs_size);
1035 if (unlikely(IS_ERR(kaddrs)))
1036 return PTR_ERR(kaddrs);
1038 /* Walk through the addrs buffer and count the number of addresses. */
1040 while (walk_size < addrs_size) {
1041 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1047 af = sctp_get_af_specific(sa_addr->sa_family);
1049 /* If the address family is not supported or if this address
1050 * causes the address buffer to overflow return EINVAL.
1052 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1057 addr_buf += af->sockaddr_len;
1058 walk_size += af->sockaddr_len;
1063 case SCTP_BINDX_ADD_ADDR:
1064 /* Allow security module to validate bindx addresses. */
1065 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1066 (struct sockaddr *)kaddrs,
1070 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1073 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1076 case SCTP_BINDX_REM_ADDR:
1077 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1080 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1094 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1096 * Common routine for handling connect() and sctp_connectx().
1097 * Connect will come in with just a single address.
1099 static int __sctp_connect(struct sock *sk,
1100 struct sockaddr *kaddrs,
1101 int addrs_size, int flags,
1102 sctp_assoc_t *assoc_id)
1104 struct net *net = sock_net(sk);
1105 struct sctp_sock *sp;
1106 struct sctp_endpoint *ep;
1107 struct sctp_association *asoc = NULL;
1108 struct sctp_association *asoc2;
1109 struct sctp_transport *transport;
1111 enum sctp_scope scope;
1116 union sctp_addr *sa_addr = NULL;
1118 unsigned short port;
1123 /* connect() cannot be done on a socket that is already in ESTABLISHED
1124 * state - UDP-style peeled off socket or a TCP-style socket that
1125 * is already connected.
1126 * It cannot be done even on a TCP-style listening socket.
1128 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1129 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1134 /* Walk through the addrs buffer and count the number of addresses. */
1136 while (walk_size < addrs_size) {
1139 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1145 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1147 /* If the address family is not supported or if this address
1148 * causes the address buffer to overflow return EINVAL.
1150 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1155 port = ntohs(sa_addr->v4.sin_port);
1157 /* Save current address so we can work with it */
1158 memcpy(&to, sa_addr, af->sockaddr_len);
1160 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1164 /* Make sure the destination port is correctly set
1167 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1172 /* Check if there already is a matching association on the
1173 * endpoint (other than the one created here).
1175 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1176 if (asoc2 && asoc2 != asoc) {
1177 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1184 /* If we could not find a matching association on the endpoint,
1185 * make sure that there is no peeled-off association matching
1186 * the peer address even on another socket.
1188 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1189 err = -EADDRNOTAVAIL;
1194 /* If a bind() or sctp_bindx() is not called prior to
1195 * an sctp_connectx() call, the system picks an
1196 * ephemeral port and will choose an address set
1197 * equivalent to binding with a wildcard address.
1199 if (!ep->base.bind_addr.port) {
1200 if (sctp_autobind(sk)) {
1206 * If an unprivileged user inherits a 1-many
1207 * style socket with open associations on a
1208 * privileged port, it MAY be permitted to
1209 * accept new associations, but it SHOULD NOT
1210 * be permitted to open new associations.
1212 if (ep->base.bind_addr.port <
1213 inet_prot_sock(net) &&
1214 !ns_capable(net->user_ns,
1215 CAP_NET_BIND_SERVICE)) {
1221 scope = sctp_scope(&to);
1222 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1228 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1236 /* Prime the peer's transport structures. */
1237 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1245 addr_buf += af->sockaddr_len;
1246 walk_size += af->sockaddr_len;
1249 /* In case the user of sctp_connectx() wants an association
1250 * id back, assign one now.
1253 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1258 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1263 /* Initialize sk's dport and daddr for getpeername() */
1264 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1265 sp->pf->to_sk_daddr(sa_addr, sk);
1268 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1271 *assoc_id = asoc->assoc_id;
1273 err = sctp_wait_for_connect(asoc, &timeo);
1274 /* Note: the asoc may be freed after the return of
1275 * sctp_wait_for_connect.
1278 /* Don't free association on exit. */
1282 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1283 __func__, asoc, kaddrs, err);
1286 /* sctp_primitive_ASSOCIATE may have added this association
1287 * To the hash table, try to unhash it, just in case, its a noop
1288 * if it wasn't hashed so we're safe
1290 sctp_association_free(asoc);
1295 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1298 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1299 * sctp_assoc_t *asoc);
1301 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1302 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1303 * or IPv6 addresses.
1305 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1306 * Section 3.1.2 for this usage.
1308 * addrs is a pointer to an array of one or more socket addresses. Each
1309 * address is contained in its appropriate structure (i.e. struct
1310 * sockaddr_in or struct sockaddr_in6) the family of the address type
1311 * must be used to distengish the address length (note that this
1312 * representation is termed a "packed array" of addresses). The caller
1313 * specifies the number of addresses in the array with addrcnt.
1315 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1316 * the association id of the new association. On failure, sctp_connectx()
1317 * returns -1, and sets errno to the appropriate error code. The assoc_id
1318 * is not touched by the kernel.
1320 * For SCTP, the port given in each socket address must be the same, or
1321 * sctp_connectx() will fail, setting errno to EINVAL.
1323 * An application can use sctp_connectx to initiate an association with
1324 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1325 * allows a caller to specify multiple addresses at which a peer can be
1326 * reached. The way the SCTP stack uses the list of addresses to set up
1327 * the association is implementation dependent. This function only
1328 * specifies that the stack will try to make use of all the addresses in
1329 * the list when needed.
1331 * Note that the list of addresses passed in is only used for setting up
1332 * the association. It does not necessarily equal the set of addresses
1333 * the peer uses for the resulting association. If the caller wants to
1334 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1335 * retrieve them after the association has been set up.
1337 * Basically do nothing but copying the addresses from user to kernel
1338 * land and invoking either sctp_connectx(). This is used for tunneling
1339 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1341 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1344 * sk The sk of the socket
1345 * addrs The pointer to the addresses in user land
1346 * addrssize Size of the addrs buffer
1348 * Returns >=0 if ok, <0 errno code on error.
1350 static int __sctp_setsockopt_connectx(struct sock *sk,
1351 struct sockaddr __user *addrs,
1353 sctp_assoc_t *assoc_id)
1355 struct sockaddr *kaddrs;
1356 int err = 0, flags = 0;
1358 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1359 __func__, sk, addrs, addrs_size);
1361 if (unlikely(addrs_size <= 0))
1364 kaddrs = memdup_user(addrs, addrs_size);
1365 if (unlikely(IS_ERR(kaddrs)))
1366 return PTR_ERR(kaddrs);
1368 /* Allow security module to validate connectx addresses. */
1369 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1370 (struct sockaddr *)kaddrs,
1375 /* in-kernel sockets don't generally have a file allocated to them
1376 * if all they do is call sock_create_kern().
1378 if (sk->sk_socket->file)
1379 flags = sk->sk_socket->file->f_flags;
1381 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1390 * This is an older interface. It's kept for backward compatibility
1391 * to the option that doesn't provide association id.
1393 static int sctp_setsockopt_connectx_old(struct sock *sk,
1394 struct sockaddr __user *addrs,
1397 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1401 * New interface for the API. The since the API is done with a socket
1402 * option, to make it simple we feed back the association id is as a return
1403 * indication to the call. Error is always negative and association id is
1406 static int sctp_setsockopt_connectx(struct sock *sk,
1407 struct sockaddr __user *addrs,
1410 sctp_assoc_t assoc_id = 0;
1413 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1422 * New (hopefully final) interface for the API.
1423 * We use the sctp_getaddrs_old structure so that use-space library
1424 * can avoid any unnecessary allocations. The only different part
1425 * is that we store the actual length of the address buffer into the
1426 * addrs_num structure member. That way we can re-use the existing
1429 #ifdef CONFIG_COMPAT
1430 struct compat_sctp_getaddrs_old {
1431 sctp_assoc_t assoc_id;
1433 compat_uptr_t addrs; /* struct sockaddr * */
1437 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1438 char __user *optval,
1441 struct sctp_getaddrs_old param;
1442 sctp_assoc_t assoc_id = 0;
1445 #ifdef CONFIG_COMPAT
1446 if (in_compat_syscall()) {
1447 struct compat_sctp_getaddrs_old param32;
1449 if (len < sizeof(param32))
1451 if (copy_from_user(¶m32, optval, sizeof(param32)))
1454 param.assoc_id = param32.assoc_id;
1455 param.addr_num = param32.addr_num;
1456 param.addrs = compat_ptr(param32.addrs);
1460 if (len < sizeof(param))
1462 if (copy_from_user(¶m, optval, sizeof(param)))
1466 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1467 param.addrs, param.addr_num,
1469 if (err == 0 || err == -EINPROGRESS) {
1470 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1472 if (put_user(sizeof(assoc_id), optlen))
1479 /* API 3.1.4 close() - UDP Style Syntax
1480 * Applications use close() to perform graceful shutdown (as described in
1481 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1482 * by a UDP-style socket.
1486 * ret = close(int sd);
1488 * sd - the socket descriptor of the associations to be closed.
1490 * To gracefully shutdown a specific association represented by the
1491 * UDP-style socket, an application should use the sendmsg() call,
1492 * passing no user data, but including the appropriate flag in the
1493 * ancillary data (see Section xxxx).
1495 * If sd in the close() call is a branched-off socket representing only
1496 * one association, the shutdown is performed on that association only.
1498 * 4.1.6 close() - TCP Style Syntax
1500 * Applications use close() to gracefully close down an association.
1504 * int close(int sd);
1506 * sd - the socket descriptor of the association to be closed.
1508 * After an application calls close() on a socket descriptor, no further
1509 * socket operations will succeed on that descriptor.
1511 * API 7.1.4 SO_LINGER
1513 * An application using the TCP-style socket can use this option to
1514 * perform the SCTP ABORT primitive. The linger option structure is:
1517 * int l_onoff; // option on/off
1518 * int l_linger; // linger time
1521 * To enable the option, set l_onoff to 1. If the l_linger value is set
1522 * to 0, calling close() is the same as the ABORT primitive. If the
1523 * value is set to a negative value, the setsockopt() call will return
1524 * an error. If the value is set to a positive value linger_time, the
1525 * close() can be blocked for at most linger_time ms. If the graceful
1526 * shutdown phase does not finish during this period, close() will
1527 * return but the graceful shutdown phase continues in the system.
1529 static void sctp_close(struct sock *sk, long timeout)
1531 struct net *net = sock_net(sk);
1532 struct sctp_endpoint *ep;
1533 struct sctp_association *asoc;
1534 struct list_head *pos, *temp;
1535 unsigned int data_was_unread;
1537 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1539 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1540 sk->sk_shutdown = SHUTDOWN_MASK;
1541 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1543 ep = sctp_sk(sk)->ep;
1545 /* Clean up any skbs sitting on the receive queue. */
1546 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1547 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1549 /* Walk all associations on an endpoint. */
1550 list_for_each_safe(pos, temp, &ep->asocs) {
1551 asoc = list_entry(pos, struct sctp_association, asocs);
1553 if (sctp_style(sk, TCP)) {
1554 /* A closed association can still be in the list if
1555 * it belongs to a TCP-style listening socket that is
1556 * not yet accepted. If so, free it. If not, send an
1557 * ABORT or SHUTDOWN based on the linger options.
1559 if (sctp_state(asoc, CLOSED)) {
1560 sctp_association_free(asoc);
1565 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1566 !skb_queue_empty(&asoc->ulpq.reasm) ||
1567 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1568 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1569 struct sctp_chunk *chunk;
1571 chunk = sctp_make_abort_user(asoc, NULL, 0);
1572 sctp_primitive_ABORT(net, asoc, chunk);
1574 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1577 /* On a TCP-style socket, block for at most linger_time if set. */
1578 if (sctp_style(sk, TCP) && timeout)
1579 sctp_wait_for_close(sk, timeout);
1581 /* This will run the backlog queue. */
1584 /* Supposedly, no process has access to the socket, but
1585 * the net layers still may.
1586 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1587 * held and that should be grabbed before socket lock.
1589 spin_lock_bh(&net->sctp.addr_wq_lock);
1590 bh_lock_sock_nested(sk);
1592 /* Hold the sock, since sk_common_release() will put sock_put()
1593 * and we have just a little more cleanup.
1596 sk_common_release(sk);
1599 spin_unlock_bh(&net->sctp.addr_wq_lock);
1603 SCTP_DBG_OBJCNT_DEC(sock);
1606 /* Handle EPIPE error. */
1607 static int sctp_error(struct sock *sk, int flags, int err)
1610 err = sock_error(sk) ? : -EPIPE;
1611 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1612 send_sig(SIGPIPE, current, 0);
1616 /* API 3.1.3 sendmsg() - UDP Style Syntax
1618 * An application uses sendmsg() and recvmsg() calls to transmit data to
1619 * and receive data from its peer.
1621 * ssize_t sendmsg(int socket, const struct msghdr *message,
1624 * socket - the socket descriptor of the endpoint.
1625 * message - pointer to the msghdr structure which contains a single
1626 * user message and possibly some ancillary data.
1628 * See Section 5 for complete description of the data
1631 * flags - flags sent or received with the user message, see Section
1632 * 5 for complete description of the flags.
1634 * Note: This function could use a rewrite especially when explicit
1635 * connect support comes in.
1637 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1639 static int sctp_msghdr_parse(const struct msghdr *msg,
1640 struct sctp_cmsgs *cmsgs);
1642 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1643 struct sctp_sndrcvinfo *srinfo,
1644 const struct msghdr *msg, size_t msg_len)
1649 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1652 if (msg_len > sk->sk_sndbuf)
1655 memset(cmsgs, 0, sizeof(*cmsgs));
1656 err = sctp_msghdr_parse(msg, cmsgs);
1658 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1662 memset(srinfo, 0, sizeof(*srinfo));
1663 if (cmsgs->srinfo) {
1664 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1665 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1666 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1667 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1668 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1669 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1673 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1674 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1675 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1676 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1677 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1680 if (cmsgs->prinfo) {
1681 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1682 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1683 cmsgs->prinfo->pr_policy);
1686 sflags = srinfo->sinfo_flags;
1687 if (!sflags && msg_len)
1690 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1693 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1694 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1697 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1703 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1704 struct sctp_cmsgs *cmsgs,
1705 union sctp_addr *daddr,
1706 struct sctp_transport **tp)
1708 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1709 struct net *net = sock_net(sk);
1710 struct sctp_association *asoc;
1711 enum sctp_scope scope;
1712 struct cmsghdr *cmsg;
1713 __be32 flowinfo = 0;
1719 if (sflags & (SCTP_EOF | SCTP_ABORT))
1722 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1723 sctp_sstate(sk, CLOSING)))
1724 return -EADDRNOTAVAIL;
1726 if (sctp_endpoint_is_peeled_off(ep, daddr))
1727 return -EADDRNOTAVAIL;
1729 if (!ep->base.bind_addr.port) {
1730 if (sctp_autobind(sk))
1733 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1734 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1738 scope = sctp_scope(daddr);
1740 /* Label connection socket for first association 1-to-many
1741 * style for client sequence socket()->sendmsg(). This
1742 * needs to be done before sctp_assoc_add_peer() as that will
1743 * set up the initial packet that needs to account for any
1744 * security ip options (CIPSO/CALIPSO) added to the packet.
1746 af = sctp_get_af_specific(daddr->sa.sa_family);
1749 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1750 (struct sockaddr *)daddr,
1755 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1759 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1765 struct sctp_initmsg *init = cmsgs->init;
1767 if (init->sinit_num_ostreams) {
1768 __u16 outcnt = init->sinit_num_ostreams;
1770 asoc->c.sinit_num_ostreams = outcnt;
1771 /* outcnt has been changed, need to re-init stream */
1772 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1778 if (init->sinit_max_instreams)
1779 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1781 if (init->sinit_max_attempts)
1782 asoc->max_init_attempts = init->sinit_max_attempts;
1784 if (init->sinit_max_init_timeo)
1785 asoc->max_init_timeo =
1786 msecs_to_jiffies(init->sinit_max_init_timeo);
1789 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1795 if (!cmsgs->addrs_msg)
1798 if (daddr->sa.sa_family == AF_INET6)
1799 flowinfo = daddr->v6.sin6_flowinfo;
1801 /* sendv addr list parse */
1802 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1803 struct sctp_transport *transport;
1804 struct sctp_association *old;
1805 union sctp_addr _daddr;
1808 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1809 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1810 cmsg->cmsg_type != SCTP_DSTADDRV6))
1814 memset(daddr, 0, sizeof(*daddr));
1815 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1816 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1817 if (dlen < sizeof(struct in_addr)) {
1822 dlen = sizeof(struct in_addr);
1823 daddr->v4.sin_family = AF_INET;
1824 daddr->v4.sin_port = htons(asoc->peer.port);
1825 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1827 if (dlen < sizeof(struct in6_addr)) {
1832 dlen = sizeof(struct in6_addr);
1833 daddr->v6.sin6_flowinfo = flowinfo;
1834 daddr->v6.sin6_family = AF_INET6;
1835 daddr->v6.sin6_port = htons(asoc->peer.port);
1836 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1838 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1842 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1843 if (old && old != asoc) {
1844 if (old->state >= SCTP_STATE_ESTABLISHED)
1851 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1852 err = -EADDRNOTAVAIL;
1856 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1867 sctp_association_free(asoc);
1871 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1872 __u16 sflags, struct msghdr *msg,
1875 struct sock *sk = asoc->base.sk;
1876 struct net *net = sock_net(sk);
1878 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1881 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1882 !sctp_state(asoc, ESTABLISHED))
1885 if (sflags & SCTP_EOF) {
1886 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1887 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1892 if (sflags & SCTP_ABORT) {
1893 struct sctp_chunk *chunk;
1895 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1899 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1900 sctp_primitive_ABORT(net, asoc, chunk);
1901 iov_iter_revert(&msg->msg_iter, msg_len);
1909 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1910 struct msghdr *msg, size_t msg_len,
1911 struct sctp_transport *transport,
1912 struct sctp_sndrcvinfo *sinfo)
1914 struct sock *sk = asoc->base.sk;
1915 struct sctp_sock *sp = sctp_sk(sk);
1916 struct net *net = sock_net(sk);
1917 struct sctp_datamsg *datamsg;
1918 bool wait_connect = false;
1919 struct sctp_chunk *chunk;
1923 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1928 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1929 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1934 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1939 if (asoc->pmtu_pending) {
1940 if (sp->param_flags & SPP_PMTUD_ENABLE)
1941 sctp_assoc_sync_pmtu(asoc);
1942 asoc->pmtu_pending = 0;
1945 if (sctp_wspace(asoc) < (int)msg_len)
1946 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1948 if (sk_under_memory_pressure(sk))
1951 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1952 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1953 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1958 if (sctp_state(asoc, CLOSED)) {
1959 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1963 if (sp->strm_interleave) {
1964 timeo = sock_sndtimeo(sk, 0);
1965 err = sctp_wait_for_connect(asoc, &timeo);
1971 wait_connect = true;
1974 pr_debug("%s: we associated primitively\n", __func__);
1977 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1978 if (IS_ERR(datamsg)) {
1979 err = PTR_ERR(datamsg);
1983 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1985 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1986 sctp_chunk_hold(chunk);
1987 sctp_set_owner_w(chunk);
1988 chunk->transport = transport;
1991 err = sctp_primitive_SEND(net, asoc, datamsg);
1993 sctp_datamsg_free(datamsg);
1997 pr_debug("%s: we sent primitively\n", __func__);
1999 sctp_datamsg_put(datamsg);
2001 if (unlikely(wait_connect)) {
2002 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
2003 sctp_wait_for_connect(asoc, &timeo);
2012 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
2013 const struct msghdr *msg,
2014 struct sctp_cmsgs *cmsgs)
2016 union sctp_addr *daddr = NULL;
2019 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
2020 int len = msg->msg_namelen;
2022 if (len > sizeof(*daddr))
2023 len = sizeof(*daddr);
2025 daddr = (union sctp_addr *)msg->msg_name;
2027 err = sctp_verify_addr(sk, daddr, len);
2029 return ERR_PTR(err);
2035 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2036 struct sctp_sndrcvinfo *sinfo,
2037 struct sctp_cmsgs *cmsgs)
2039 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2040 sinfo->sinfo_stream = asoc->default_stream;
2041 sinfo->sinfo_ppid = asoc->default_ppid;
2042 sinfo->sinfo_context = asoc->default_context;
2043 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
2046 sinfo->sinfo_flags = asoc->default_flags;
2049 if (!cmsgs->srinfo && !cmsgs->prinfo)
2050 sinfo->sinfo_timetolive = asoc->default_timetolive;
2052 if (cmsgs->authinfo) {
2053 /* Reuse sinfo_tsn to indicate that authinfo was set and
2054 * sinfo_ssn to save the keyid on tx path.
2056 sinfo->sinfo_tsn = 1;
2057 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2061 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2063 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2064 struct sctp_transport *transport = NULL;
2065 struct sctp_sndrcvinfo _sinfo, *sinfo;
2066 struct sctp_association *asoc, *tmp;
2067 struct sctp_cmsgs cmsgs;
2068 union sctp_addr *daddr;
2073 /* Parse and get snd_info */
2074 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2079 sflags = sinfo->sinfo_flags;
2081 /* Get daddr from msg */
2082 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2083 if (IS_ERR(daddr)) {
2084 err = PTR_ERR(daddr);
2090 /* SCTP_SENDALL process */
2091 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2092 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
2093 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2100 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2102 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2107 iov_iter_revert(&msg->msg_iter, err);
2113 /* Get and check or create asoc */
2115 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2117 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2122 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2127 asoc = transport->asoc;
2131 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2134 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2140 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2145 /* Update snd_info with the asoc */
2146 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2148 /* Send msg to the asoc */
2149 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2150 if (err < 0 && err != -ESRCH && new)
2151 sctp_association_free(asoc);
2156 return sctp_error(sk, msg->msg_flags, err);
2159 /* This is an extended version of skb_pull() that removes the data from the
2160 * start of a skb even when data is spread across the list of skb's in the
2161 * frag_list. len specifies the total amount of data that needs to be removed.
2162 * when 'len' bytes could be removed from the skb, it returns 0.
2163 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2164 * could not be removed.
2166 static int sctp_skb_pull(struct sk_buff *skb, int len)
2168 struct sk_buff *list;
2169 int skb_len = skb_headlen(skb);
2172 if (len <= skb_len) {
2173 __skb_pull(skb, len);
2177 __skb_pull(skb, skb_len);
2179 skb_walk_frags(skb, list) {
2180 rlen = sctp_skb_pull(list, len);
2181 skb->len -= (len-rlen);
2182 skb->data_len -= (len-rlen);
2193 /* API 3.1.3 recvmsg() - UDP Style Syntax
2195 * ssize_t recvmsg(int socket, struct msghdr *message,
2198 * socket - the socket descriptor of the endpoint.
2199 * message - pointer to the msghdr structure which contains a single
2200 * user message and possibly some ancillary data.
2202 * See Section 5 for complete description of the data
2205 * flags - flags sent or received with the user message, see Section
2206 * 5 for complete description of the flags.
2208 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2209 int noblock, int flags, int *addr_len)
2211 struct sctp_ulpevent *event = NULL;
2212 struct sctp_sock *sp = sctp_sk(sk);
2213 struct sk_buff *skb, *head_skb;
2218 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2219 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2224 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2225 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2230 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2234 /* Get the total length of the skb including any skb's in the
2243 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2245 event = sctp_skb2event(skb);
2250 if (event->chunk && event->chunk->head_skb)
2251 head_skb = event->chunk->head_skb;
2254 sock_recv_ts_and_drops(msg, sk, head_skb);
2255 if (sctp_ulpevent_is_notification(event)) {
2256 msg->msg_flags |= MSG_NOTIFICATION;
2257 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2259 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2262 /* Check if we allow SCTP_NXTINFO. */
2263 if (sp->recvnxtinfo)
2264 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2265 /* Check if we allow SCTP_RCVINFO. */
2266 if (sp->recvrcvinfo)
2267 sctp_ulpevent_read_rcvinfo(event, msg);
2268 /* Check if we allow SCTP_SNDRCVINFO. */
2269 if (sp->subscribe.sctp_data_io_event)
2270 sctp_ulpevent_read_sndrcvinfo(event, msg);
2274 /* If skb's length exceeds the user's buffer, update the skb and
2275 * push it back to the receive_queue so that the next call to
2276 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2278 if (skb_len > copied) {
2279 msg->msg_flags &= ~MSG_EOR;
2280 if (flags & MSG_PEEK)
2282 sctp_skb_pull(skb, copied);
2283 skb_queue_head(&sk->sk_receive_queue, skb);
2285 /* When only partial message is copied to the user, increase
2286 * rwnd by that amount. If all the data in the skb is read,
2287 * rwnd is updated when the event is freed.
2289 if (!sctp_ulpevent_is_notification(event))
2290 sctp_assoc_rwnd_increase(event->asoc, copied);
2292 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2293 (event->msg_flags & MSG_EOR))
2294 msg->msg_flags |= MSG_EOR;
2296 msg->msg_flags &= ~MSG_EOR;
2299 if (flags & MSG_PEEK) {
2300 /* Release the skb reference acquired after peeking the skb in
2301 * sctp_skb_recv_datagram().
2305 /* Free the event which includes releasing the reference to
2306 * the owner of the skb, freeing the skb and updating the
2309 sctp_ulpevent_free(event);
2316 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2318 * This option is a on/off flag. If enabled no SCTP message
2319 * fragmentation will be performed. Instead if a message being sent
2320 * exceeds the current PMTU size, the message will NOT be sent and
2321 * instead a error will be indicated to the user.
2323 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2324 char __user *optval,
2325 unsigned int optlen)
2329 if (optlen < sizeof(int))
2332 if (get_user(val, (int __user *)optval))
2335 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2340 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2341 unsigned int optlen)
2343 struct sctp_association *asoc;
2344 struct sctp_ulpevent *event;
2346 if (optlen > sizeof(struct sctp_event_subscribe))
2348 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2351 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2352 * if there is no data to be sent or retransmit, the stack will
2353 * immediately send up this notification.
2355 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2356 &sctp_sk(sk)->subscribe)) {
2357 asoc = sctp_id2assoc(sk, 0);
2359 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2360 event = sctp_ulpevent_make_sender_dry_event(asoc,
2361 GFP_USER | __GFP_NOWARN);
2365 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2372 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2374 * This socket option is applicable to the UDP-style socket only. When
2375 * set it will cause associations that are idle for more than the
2376 * specified number of seconds to automatically close. An association
2377 * being idle is defined an association that has NOT sent or received
2378 * user data. The special value of '0' indicates that no automatic
2379 * close of any associations should be performed. The option expects an
2380 * integer defining the number of seconds of idle time before an
2381 * association is closed.
2383 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2384 unsigned int optlen)
2386 struct sctp_sock *sp = sctp_sk(sk);
2387 struct net *net = sock_net(sk);
2389 /* Applicable to UDP-style socket only */
2390 if (sctp_style(sk, TCP))
2392 if (optlen != sizeof(int))
2394 if (copy_from_user(&sp->autoclose, optval, optlen))
2397 if (sp->autoclose > net->sctp.max_autoclose)
2398 sp->autoclose = net->sctp.max_autoclose;
2403 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2405 * Applications can enable or disable heartbeats for any peer address of
2406 * an association, modify an address's heartbeat interval, force a
2407 * heartbeat to be sent immediately, and adjust the address's maximum
2408 * number of retransmissions sent before an address is considered
2409 * unreachable. The following structure is used to access and modify an
2410 * address's parameters:
2412 * struct sctp_paddrparams {
2413 * sctp_assoc_t spp_assoc_id;
2414 * struct sockaddr_storage spp_address;
2415 * uint32_t spp_hbinterval;
2416 * uint16_t spp_pathmaxrxt;
2417 * uint32_t spp_pathmtu;
2418 * uint32_t spp_sackdelay;
2419 * uint32_t spp_flags;
2420 * uint32_t spp_ipv6_flowlabel;
2424 * spp_assoc_id - (one-to-many style socket) This is filled in the
2425 * application, and identifies the association for
2427 * spp_address - This specifies which address is of interest.
2428 * spp_hbinterval - This contains the value of the heartbeat interval,
2429 * in milliseconds. If a value of zero
2430 * is present in this field then no changes are to
2431 * be made to this parameter.
2432 * spp_pathmaxrxt - This contains the maximum number of
2433 * retransmissions before this address shall be
2434 * considered unreachable. If a value of zero
2435 * is present in this field then no changes are to
2436 * be made to this parameter.
2437 * spp_pathmtu - When Path MTU discovery is disabled the value
2438 * specified here will be the "fixed" path mtu.
2439 * Note that if the spp_address field is empty
2440 * then all associations on this address will
2441 * have this fixed path mtu set upon them.
2443 * spp_sackdelay - When delayed sack is enabled, this value specifies
2444 * the number of milliseconds that sacks will be delayed
2445 * for. This value will apply to all addresses of an
2446 * association if the spp_address field is empty. Note
2447 * also, that if delayed sack is enabled and this
2448 * value is set to 0, no change is made to the last
2449 * recorded delayed sack timer value.
2451 * spp_flags - These flags are used to control various features
2452 * on an association. The flag field may contain
2453 * zero or more of the following options.
2455 * SPP_HB_ENABLE - Enable heartbeats on the
2456 * specified address. Note that if the address
2457 * field is empty all addresses for the association
2458 * have heartbeats enabled upon them.
2460 * SPP_HB_DISABLE - Disable heartbeats on the
2461 * speicifed address. Note that if the address
2462 * field is empty all addresses for the association
2463 * will have their heartbeats disabled. Note also
2464 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2465 * mutually exclusive, only one of these two should
2466 * be specified. Enabling both fields will have
2467 * undetermined results.
2469 * SPP_HB_DEMAND - Request a user initiated heartbeat
2470 * to be made immediately.
2472 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2473 * heartbeat delayis to be set to the value of 0
2476 * SPP_PMTUD_ENABLE - This field will enable PMTU
2477 * discovery upon the specified address. Note that
2478 * if the address feild is empty then all addresses
2479 * on the association are effected.
2481 * SPP_PMTUD_DISABLE - This field will disable PMTU
2482 * discovery upon the specified address. Note that
2483 * if the address feild is empty then all addresses
2484 * on the association are effected. Not also that
2485 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2486 * exclusive. Enabling both will have undetermined
2489 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2490 * on delayed sack. The time specified in spp_sackdelay
2491 * is used to specify the sack delay for this address. Note
2492 * that if spp_address is empty then all addresses will
2493 * enable delayed sack and take on the sack delay
2494 * value specified in spp_sackdelay.
2495 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2496 * off delayed sack. If the spp_address field is blank then
2497 * delayed sack is disabled for the entire association. Note
2498 * also that this field is mutually exclusive to
2499 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2502 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2503 * setting of the IPV6 flow label value. The value is
2504 * contained in the spp_ipv6_flowlabel field.
2505 * Upon retrieval, this flag will be set to indicate that
2506 * the spp_ipv6_flowlabel field has a valid value returned.
2507 * If a specific destination address is set (in the
2508 * spp_address field), then the value returned is that of
2509 * the address. If just an association is specified (and
2510 * no address), then the association's default flow label
2511 * is returned. If neither an association nor a destination
2512 * is specified, then the socket's default flow label is
2513 * returned. For non-IPv6 sockets, this flag will be left
2516 * SPP_DSCP: Setting this flag enables the setting of the
2517 * Differentiated Services Code Point (DSCP) value
2518 * associated with either the association or a specific
2519 * address. The value is obtained in the spp_dscp field.
2520 * Upon retrieval, this flag will be set to indicate that
2521 * the spp_dscp field has a valid value returned. If a
2522 * specific destination address is set when called (in the
2523 * spp_address field), then that specific destination
2524 * address's DSCP value is returned. If just an association
2525 * is specified, then the association's default DSCP is
2526 * returned. If neither an association nor a destination is
2527 * specified, then the socket's default DSCP is returned.
2529 * spp_ipv6_flowlabel
2530 * - This field is used in conjunction with the
2531 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2532 * The 20 least significant bits are used for the flow
2533 * label. This setting has precedence over any IPv6-layer
2536 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2537 * and contains the DSCP. The 6 most significant bits are
2538 * used for the DSCP. This setting has precedence over any
2539 * IPv4- or IPv6- layer setting.
2541 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2542 struct sctp_transport *trans,
2543 struct sctp_association *asoc,
2544 struct sctp_sock *sp,
2547 int sackdelay_change)
2551 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2552 struct net *net = sock_net(trans->asoc->base.sk);
2554 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2559 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2560 * this field is ignored. Note also that a value of zero indicates
2561 * the current setting should be left unchanged.
2563 if (params->spp_flags & SPP_HB_ENABLE) {
2565 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2566 * set. This lets us use 0 value when this flag
2569 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2570 params->spp_hbinterval = 0;
2572 if (params->spp_hbinterval ||
2573 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2576 msecs_to_jiffies(params->spp_hbinterval);
2579 msecs_to_jiffies(params->spp_hbinterval);
2581 sp->hbinterval = params->spp_hbinterval;
2588 trans->param_flags =
2589 (trans->param_flags & ~SPP_HB) | hb_change;
2592 (asoc->param_flags & ~SPP_HB) | hb_change;
2595 (sp->param_flags & ~SPP_HB) | hb_change;
2599 /* When Path MTU discovery is disabled the value specified here will
2600 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2601 * include the flag SPP_PMTUD_DISABLE for this field to have any
2604 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2606 trans->pathmtu = params->spp_pathmtu;
2607 sctp_assoc_sync_pmtu(asoc);
2609 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2611 sp->pathmtu = params->spp_pathmtu;
2617 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2618 (params->spp_flags & SPP_PMTUD_ENABLE);
2619 trans->param_flags =
2620 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2622 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2623 sctp_assoc_sync_pmtu(asoc);
2627 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2630 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2634 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2635 * value of this field is ignored. Note also that a value of zero
2636 * indicates the current setting should be left unchanged.
2638 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2641 msecs_to_jiffies(params->spp_sackdelay);
2644 msecs_to_jiffies(params->spp_sackdelay);
2646 sp->sackdelay = params->spp_sackdelay;
2650 if (sackdelay_change) {
2652 trans->param_flags =
2653 (trans->param_flags & ~SPP_SACKDELAY) |
2657 (asoc->param_flags & ~SPP_SACKDELAY) |
2661 (sp->param_flags & ~SPP_SACKDELAY) |
2666 /* Note that a value of zero indicates the current setting should be
2669 if (params->spp_pathmaxrxt) {
2671 trans->pathmaxrxt = params->spp_pathmaxrxt;
2673 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2675 sp->pathmaxrxt = params->spp_pathmaxrxt;
2679 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2681 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2682 trans->flowlabel = params->spp_ipv6_flowlabel &
2683 SCTP_FLOWLABEL_VAL_MASK;
2684 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2687 struct sctp_transport *t;
2689 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2691 if (t->ipaddr.sa.sa_family != AF_INET6)
2693 t->flowlabel = params->spp_ipv6_flowlabel &
2694 SCTP_FLOWLABEL_VAL_MASK;
2695 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2697 asoc->flowlabel = params->spp_ipv6_flowlabel &
2698 SCTP_FLOWLABEL_VAL_MASK;
2699 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2700 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2701 sp->flowlabel = params->spp_ipv6_flowlabel &
2702 SCTP_FLOWLABEL_VAL_MASK;
2703 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2707 if (params->spp_flags & SPP_DSCP) {
2709 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2710 trans->dscp |= SCTP_DSCP_SET_MASK;
2712 struct sctp_transport *t;
2714 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2716 t->dscp = params->spp_dscp &
2718 t->dscp |= SCTP_DSCP_SET_MASK;
2720 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2721 asoc->dscp |= SCTP_DSCP_SET_MASK;
2723 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2724 sp->dscp |= SCTP_DSCP_SET_MASK;
2731 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2732 char __user *optval,
2733 unsigned int optlen)
2735 struct sctp_paddrparams params;
2736 struct sctp_transport *trans = NULL;
2737 struct sctp_association *asoc = NULL;
2738 struct sctp_sock *sp = sctp_sk(sk);
2740 int hb_change, pmtud_change, sackdelay_change;
2742 if (optlen == sizeof(params)) {
2743 if (copy_from_user(¶ms, optval, optlen))
2745 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2746 spp_ipv6_flowlabel), 4)) {
2747 if (copy_from_user(¶ms, optval, optlen))
2749 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2755 /* Validate flags and value parameters. */
2756 hb_change = params.spp_flags & SPP_HB;
2757 pmtud_change = params.spp_flags & SPP_PMTUD;
2758 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2760 if (hb_change == SPP_HB ||
2761 pmtud_change == SPP_PMTUD ||
2762 sackdelay_change == SPP_SACKDELAY ||
2763 params.spp_sackdelay > 500 ||
2764 (params.spp_pathmtu &&
2765 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2768 /* If an address other than INADDR_ANY is specified, and
2769 * no transport is found, then the request is invalid.
2771 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2772 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2773 params.spp_assoc_id);
2778 /* Get association, if assoc_id != 0 and the socket is a one
2779 * to many style socket, and an association was not found, then
2780 * the id was invalid.
2782 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2783 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2786 /* Heartbeat demand can only be sent on a transport or
2787 * association, but not a socket.
2789 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2792 /* Process parameters. */
2793 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2794 hb_change, pmtud_change,
2800 /* If changes are for association, also apply parameters to each
2803 if (!trans && asoc) {
2804 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2806 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2807 hb_change, pmtud_change,
2815 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2817 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2820 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2822 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2826 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2828 * This option will effect the way delayed acks are performed. This
2829 * option allows you to get or set the delayed ack time, in
2830 * milliseconds. It also allows changing the delayed ack frequency.
2831 * Changing the frequency to 1 disables the delayed sack algorithm. If
2832 * the assoc_id is 0, then this sets or gets the endpoints default
2833 * values. If the assoc_id field is non-zero, then the set or get
2834 * effects the specified association for the one to many model (the
2835 * assoc_id field is ignored by the one to one model). Note that if
2836 * sack_delay or sack_freq are 0 when setting this option, then the
2837 * current values will remain unchanged.
2839 * struct sctp_sack_info {
2840 * sctp_assoc_t sack_assoc_id;
2841 * uint32_t sack_delay;
2842 * uint32_t sack_freq;
2845 * sack_assoc_id - This parameter, indicates which association the user
2846 * is performing an action upon. Note that if this field's value is
2847 * zero then the endpoints default value is changed (effecting future
2848 * associations only).
2850 * sack_delay - This parameter contains the number of milliseconds that
2851 * the user is requesting the delayed ACK timer be set to. Note that
2852 * this value is defined in the standard to be between 200 and 500
2855 * sack_freq - This parameter contains the number of packets that must
2856 * be received before a sack is sent without waiting for the delay
2857 * timer to expire. The default value for this is 2, setting this
2858 * value to 1 will disable the delayed sack algorithm.
2861 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2862 char __user *optval, unsigned int optlen)
2864 struct sctp_sack_info params;
2865 struct sctp_transport *trans = NULL;
2866 struct sctp_association *asoc = NULL;
2867 struct sctp_sock *sp = sctp_sk(sk);
2869 if (optlen == sizeof(struct sctp_sack_info)) {
2870 if (copy_from_user(¶ms, optval, optlen))
2873 if (params.sack_delay == 0 && params.sack_freq == 0)
2875 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2876 pr_warn_ratelimited(DEPRECATED
2878 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2879 "Use struct sctp_sack_info instead\n",
2880 current->comm, task_pid_nr(current));
2881 if (copy_from_user(¶ms, optval, optlen))
2884 if (params.sack_delay == 0)
2885 params.sack_freq = 1;
2887 params.sack_freq = 0;
2891 /* Validate value parameter. */
2892 if (params.sack_delay > 500)
2895 /* Get association, if sack_assoc_id != 0 and the socket is a one
2896 * to many style socket, and an association was not found, then
2897 * the id was invalid.
2899 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2900 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2903 if (params.sack_delay) {
2906 msecs_to_jiffies(params.sack_delay);
2908 sctp_spp_sackdelay_enable(asoc->param_flags);
2910 sp->sackdelay = params.sack_delay;
2912 sctp_spp_sackdelay_enable(sp->param_flags);
2916 if (params.sack_freq == 1) {
2919 sctp_spp_sackdelay_disable(asoc->param_flags);
2922 sctp_spp_sackdelay_disable(sp->param_flags);
2924 } else if (params.sack_freq > 1) {
2926 asoc->sackfreq = params.sack_freq;
2928 sctp_spp_sackdelay_enable(asoc->param_flags);
2930 sp->sackfreq = params.sack_freq;
2932 sctp_spp_sackdelay_enable(sp->param_flags);
2936 /* If change is for association, also apply to each transport. */
2938 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2940 if (params.sack_delay) {
2942 msecs_to_jiffies(params.sack_delay);
2943 trans->param_flags =
2944 sctp_spp_sackdelay_enable(trans->param_flags);
2946 if (params.sack_freq == 1) {
2947 trans->param_flags =
2948 sctp_spp_sackdelay_disable(trans->param_flags);
2949 } else if (params.sack_freq > 1) {
2950 trans->sackfreq = params.sack_freq;
2951 trans->param_flags =
2952 sctp_spp_sackdelay_enable(trans->param_flags);
2960 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2962 * Applications can specify protocol parameters for the default association
2963 * initialization. The option name argument to setsockopt() and getsockopt()
2966 * Setting initialization parameters is effective only on an unconnected
2967 * socket (for UDP-style sockets only future associations are effected
2968 * by the change). With TCP-style sockets, this option is inherited by
2969 * sockets derived from a listener socket.
2971 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2973 struct sctp_initmsg sinit;
2974 struct sctp_sock *sp = sctp_sk(sk);
2976 if (optlen != sizeof(struct sctp_initmsg))
2978 if (copy_from_user(&sinit, optval, optlen))
2981 if (sinit.sinit_num_ostreams)
2982 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2983 if (sinit.sinit_max_instreams)
2984 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2985 if (sinit.sinit_max_attempts)
2986 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2987 if (sinit.sinit_max_init_timeo)
2988 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2994 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2996 * Applications that wish to use the sendto() system call may wish to
2997 * specify a default set of parameters that would normally be supplied
2998 * through the inclusion of ancillary data. This socket option allows
2999 * such an application to set the default sctp_sndrcvinfo structure.
3000 * The application that wishes to use this socket option simply passes
3001 * in to this call the sctp_sndrcvinfo structure defined in Section
3002 * 5.2.2) The input parameters accepted by this call include
3003 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3004 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3005 * to this call if the caller is using the UDP model.
3007 static int sctp_setsockopt_default_send_param(struct sock *sk,
3008 char __user *optval,
3009 unsigned int optlen)
3011 struct sctp_sock *sp = sctp_sk(sk);
3012 struct sctp_association *asoc;
3013 struct sctp_sndrcvinfo info;
3015 if (optlen != sizeof(info))
3017 if (copy_from_user(&info, optval, optlen))
3019 if (info.sinfo_flags &
3020 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3021 SCTP_ABORT | SCTP_EOF))
3024 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3025 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3028 asoc->default_stream = info.sinfo_stream;
3029 asoc->default_flags = info.sinfo_flags;
3030 asoc->default_ppid = info.sinfo_ppid;
3031 asoc->default_context = info.sinfo_context;
3032 asoc->default_timetolive = info.sinfo_timetolive;
3034 sp->default_stream = info.sinfo_stream;
3035 sp->default_flags = info.sinfo_flags;
3036 sp->default_ppid = info.sinfo_ppid;
3037 sp->default_context = info.sinfo_context;
3038 sp->default_timetolive = info.sinfo_timetolive;
3044 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3045 * (SCTP_DEFAULT_SNDINFO)
3047 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3048 char __user *optval,
3049 unsigned int optlen)
3051 struct sctp_sock *sp = sctp_sk(sk);
3052 struct sctp_association *asoc;
3053 struct sctp_sndinfo info;
3055 if (optlen != sizeof(info))
3057 if (copy_from_user(&info, optval, optlen))
3059 if (info.snd_flags &
3060 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3061 SCTP_ABORT | SCTP_EOF))
3064 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3065 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
3068 asoc->default_stream = info.snd_sid;
3069 asoc->default_flags = info.snd_flags;
3070 asoc->default_ppid = info.snd_ppid;
3071 asoc->default_context = info.snd_context;
3073 sp->default_stream = info.snd_sid;
3074 sp->default_flags = info.snd_flags;
3075 sp->default_ppid = info.snd_ppid;
3076 sp->default_context = info.snd_context;
3082 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3084 * Requests that the local SCTP stack use the enclosed peer address as
3085 * the association primary. The enclosed address must be one of the
3086 * association peer's addresses.
3088 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3089 unsigned int optlen)
3091 struct sctp_prim prim;
3092 struct sctp_transport *trans;
3096 if (optlen != sizeof(struct sctp_prim))
3099 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3102 /* Allow security module to validate address but need address len. */
3103 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3107 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3108 (struct sockaddr *)&prim.ssp_addr,
3113 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3117 sctp_assoc_set_primary(trans->asoc, trans);
3123 * 7.1.5 SCTP_NODELAY
3125 * Turn on/off any Nagle-like algorithm. This means that packets are
3126 * generally sent as soon as possible and no unnecessary delays are
3127 * introduced, at the cost of more packets in the network. Expects an
3128 * integer boolean flag.
3130 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3131 unsigned int optlen)
3135 if (optlen < sizeof(int))
3137 if (get_user(val, (int __user *)optval))
3140 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3146 * 7.1.1 SCTP_RTOINFO
3148 * The protocol parameters used to initialize and bound retransmission
3149 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3150 * and modify these parameters.
3151 * All parameters are time values, in milliseconds. A value of 0, when
3152 * modifying the parameters, indicates that the current value should not
3156 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3158 struct sctp_rtoinfo rtoinfo;
3159 struct sctp_association *asoc;
3160 unsigned long rto_min, rto_max;
3161 struct sctp_sock *sp = sctp_sk(sk);
3163 if (optlen != sizeof (struct sctp_rtoinfo))
3166 if (copy_from_user(&rtoinfo, optval, optlen))
3169 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3171 /* Set the values to the specific association */
3172 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3175 rto_max = rtoinfo.srto_max;
3176 rto_min = rtoinfo.srto_min;
3179 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3181 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3184 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3186 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3188 if (rto_min > rto_max)
3192 if (rtoinfo.srto_initial != 0)
3194 msecs_to_jiffies(rtoinfo.srto_initial);
3195 asoc->rto_max = rto_max;
3196 asoc->rto_min = rto_min;
3198 /* If there is no association or the association-id = 0
3199 * set the values to the endpoint.
3201 if (rtoinfo.srto_initial != 0)
3202 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3203 sp->rtoinfo.srto_max = rto_max;
3204 sp->rtoinfo.srto_min = rto_min;
3212 * 7.1.2 SCTP_ASSOCINFO
3214 * This option is used to tune the maximum retransmission attempts
3215 * of the association.
3216 * Returns an error if the new association retransmission value is
3217 * greater than the sum of the retransmission value of the peer.
3218 * See [SCTP] for more information.
3221 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3224 struct sctp_assocparams assocparams;
3225 struct sctp_association *asoc;
3227 if (optlen != sizeof(struct sctp_assocparams))
3229 if (copy_from_user(&assocparams, optval, optlen))
3232 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3234 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3237 /* Set the values to the specific association */
3239 if (assocparams.sasoc_asocmaxrxt != 0) {
3242 struct sctp_transport *peer_addr;
3244 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3246 path_sum += peer_addr->pathmaxrxt;
3250 /* Only validate asocmaxrxt if we have more than
3251 * one path/transport. We do this because path
3252 * retransmissions are only counted when we have more
3256 assocparams.sasoc_asocmaxrxt > path_sum)
3259 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3262 if (assocparams.sasoc_cookie_life != 0)
3263 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3265 /* Set the values to the endpoint */
3266 struct sctp_sock *sp = sctp_sk(sk);
3268 if (assocparams.sasoc_asocmaxrxt != 0)
3269 sp->assocparams.sasoc_asocmaxrxt =
3270 assocparams.sasoc_asocmaxrxt;
3271 if (assocparams.sasoc_cookie_life != 0)
3272 sp->assocparams.sasoc_cookie_life =
3273 assocparams.sasoc_cookie_life;
3279 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3281 * This socket option is a boolean flag which turns on or off mapped V4
3282 * addresses. If this option is turned on and the socket is type
3283 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3284 * If this option is turned off, then no mapping will be done of V4
3285 * addresses and a user will receive both PF_INET6 and PF_INET type
3286 * addresses on the socket.
3288 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3291 struct sctp_sock *sp = sctp_sk(sk);
3293 if (optlen < sizeof(int))
3295 if (get_user(val, (int __user *)optval))
3306 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3307 * This option will get or set the maximum size to put in any outgoing
3308 * SCTP DATA chunk. If a message is larger than this size it will be
3309 * fragmented by SCTP into the specified size. Note that the underlying
3310 * SCTP implementation may fragment into smaller sized chunks when the
3311 * PMTU of the underlying association is smaller than the value set by
3312 * the user. The default value for this option is '0' which indicates
3313 * the user is NOT limiting fragmentation and only the PMTU will effect
3314 * SCTP's choice of DATA chunk size. Note also that values set larger
3315 * than the maximum size of an IP datagram will effectively let SCTP
3316 * control fragmentation (i.e. the same as setting this option to 0).
3318 * The following structure is used to access and modify this parameter:
3320 * struct sctp_assoc_value {
3321 * sctp_assoc_t assoc_id;
3322 * uint32_t assoc_value;
3325 * assoc_id: This parameter is ignored for one-to-one style sockets.
3326 * For one-to-many style sockets this parameter indicates which
3327 * association the user is performing an action upon. Note that if
3328 * this field's value is zero then the endpoints default value is
3329 * changed (effecting future associations only).
3330 * assoc_value: This parameter specifies the maximum size in bytes.
3332 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3334 struct sctp_sock *sp = sctp_sk(sk);
3335 struct sctp_assoc_value params;
3336 struct sctp_association *asoc;
3339 if (optlen == sizeof(int)) {
3340 pr_warn_ratelimited(DEPRECATED
3342 "Use of int in maxseg socket option.\n"
3343 "Use struct sctp_assoc_value instead\n",
3344 current->comm, task_pid_nr(current));
3345 if (copy_from_user(&val, optval, optlen))
3347 params.assoc_id = 0;
3348 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3349 if (copy_from_user(¶ms, optval, optlen))
3351 val = params.assoc_value;
3356 asoc = sctp_id2assoc(sk, params.assoc_id);
3359 int min_len, max_len;
3360 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3361 sizeof(struct sctp_data_chunk);
3363 min_len = sctp_min_frag_point(sp, datasize);
3364 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3366 if (val < min_len || val > max_len)
3371 asoc->user_frag = val;
3372 sctp_assoc_update_frag_point(asoc);
3374 if (params.assoc_id && sctp_style(sk, UDP))
3376 sp->user_frag = val;
3384 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3386 * Requests that the peer mark the enclosed address as the association
3387 * primary. The enclosed address must be one of the association's
3388 * locally bound addresses. The following structure is used to make a
3389 * set primary request:
3391 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3392 unsigned int optlen)
3394 struct net *net = sock_net(sk);
3395 struct sctp_sock *sp;
3396 struct sctp_association *asoc = NULL;
3397 struct sctp_setpeerprim prim;
3398 struct sctp_chunk *chunk;
3404 if (!net->sctp.addip_enable)
3407 if (optlen != sizeof(struct sctp_setpeerprim))
3410 if (copy_from_user(&prim, optval, optlen))
3413 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3417 if (!asoc->peer.asconf_capable)
3420 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3423 if (!sctp_state(asoc, ESTABLISHED))
3426 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3430 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3431 return -EADDRNOTAVAIL;
3433 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3434 return -EADDRNOTAVAIL;
3436 /* Allow security module to validate address. */
3437 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3438 (struct sockaddr *)&prim.sspp_addr,
3443 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3444 chunk = sctp_make_asconf_set_prim(asoc,
3445 (union sctp_addr *)&prim.sspp_addr);
3449 err = sctp_send_asconf(asoc, chunk);
3451 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3456 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3457 unsigned int optlen)
3459 struct sctp_setadaptation adaptation;
3461 if (optlen != sizeof(struct sctp_setadaptation))
3463 if (copy_from_user(&adaptation, optval, optlen))
3466 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3472 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3474 * The context field in the sctp_sndrcvinfo structure is normally only
3475 * used when a failed message is retrieved holding the value that was
3476 * sent down on the actual send call. This option allows the setting of
3477 * a default context on an association basis that will be received on
3478 * reading messages from the peer. This is especially helpful in the
3479 * one-2-many model for an application to keep some reference to an
3480 * internal state machine that is processing messages on the
3481 * association. Note that the setting of this value only effects
3482 * received messages from the peer and does not effect the value that is
3483 * saved with outbound messages.
3485 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3486 unsigned int optlen)
3488 struct sctp_assoc_value params;
3489 struct sctp_sock *sp;
3490 struct sctp_association *asoc;
3492 if (optlen != sizeof(struct sctp_assoc_value))
3494 if (copy_from_user(¶ms, optval, optlen))
3499 if (params.assoc_id != 0) {
3500 asoc = sctp_id2assoc(sk, params.assoc_id);
3503 asoc->default_rcv_context = params.assoc_value;
3505 sp->default_rcv_context = params.assoc_value;
3512 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3514 * This options will at a minimum specify if the implementation is doing
3515 * fragmented interleave. Fragmented interleave, for a one to many
3516 * socket, is when subsequent calls to receive a message may return
3517 * parts of messages from different associations. Some implementations
3518 * may allow you to turn this value on or off. If so, when turned off,
3519 * no fragment interleave will occur (which will cause a head of line
3520 * blocking amongst multiple associations sharing the same one to many
3521 * socket). When this option is turned on, then each receive call may
3522 * come from a different association (thus the user must receive data
3523 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3524 * association each receive belongs to.
3526 * This option takes a boolean value. A non-zero value indicates that
3527 * fragmented interleave is on. A value of zero indicates that
3528 * fragmented interleave is off.
3530 * Note that it is important that an implementation that allows this
3531 * option to be turned on, have it off by default. Otherwise an unaware
3532 * application using the one to many model may become confused and act
3535 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3536 char __user *optval,
3537 unsigned int optlen)
3541 if (optlen != sizeof(int))
3543 if (get_user(val, (int __user *)optval))
3546 sctp_sk(sk)->frag_interleave = !!val;
3548 if (!sctp_sk(sk)->frag_interleave)
3549 sctp_sk(sk)->strm_interleave = 0;
3555 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3556 * (SCTP_PARTIAL_DELIVERY_POINT)
3558 * This option will set or get the SCTP partial delivery point. This
3559 * point is the size of a message where the partial delivery API will be
3560 * invoked to help free up rwnd space for the peer. Setting this to a
3561 * lower value will cause partial deliveries to happen more often. The
3562 * calls argument is an integer that sets or gets the partial delivery
3563 * point. Note also that the call will fail if the user attempts to set
3564 * this value larger than the socket receive buffer size.
3566 * Note that any single message having a length smaller than or equal to
3567 * the SCTP partial delivery point will be delivered in one single read
3568 * call as long as the user provided buffer is large enough to hold the
3571 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3572 char __user *optval,
3573 unsigned int optlen)
3577 if (optlen != sizeof(u32))
3579 if (get_user(val, (int __user *)optval))
3582 /* Note: We double the receive buffer from what the user sets
3583 * it to be, also initial rwnd is based on rcvbuf/2.
3585 if (val > (sk->sk_rcvbuf >> 1))
3588 sctp_sk(sk)->pd_point = val;
3590 return 0; /* is this the right error code? */
3594 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3596 * This option will allow a user to change the maximum burst of packets
3597 * that can be emitted by this association. Note that the default value
3598 * is 4, and some implementations may restrict this setting so that it
3599 * can only be lowered.
3601 * NOTE: This text doesn't seem right. Do this on a socket basis with
3602 * future associations inheriting the socket value.
3604 static int sctp_setsockopt_maxburst(struct sock *sk,
3605 char __user *optval,
3606 unsigned int optlen)
3608 struct sctp_assoc_value params;
3609 struct sctp_sock *sp;
3610 struct sctp_association *asoc;
3614 if (optlen == sizeof(int)) {
3615 pr_warn_ratelimited(DEPRECATED
3617 "Use of int in max_burst socket option deprecated.\n"
3618 "Use struct sctp_assoc_value instead\n",
3619 current->comm, task_pid_nr(current));
3620 if (copy_from_user(&val, optval, optlen))
3622 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3623 if (copy_from_user(¶ms, optval, optlen))
3625 val = params.assoc_value;
3626 assoc_id = params.assoc_id;
3632 if (assoc_id != 0) {
3633 asoc = sctp_id2assoc(sk, assoc_id);
3636 asoc->max_burst = val;
3638 sp->max_burst = val;
3644 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3646 * This set option adds a chunk type that the user is requesting to be
3647 * received only in an authenticated way. Changes to the list of chunks
3648 * will only effect future associations on the socket.
3650 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3651 char __user *optval,
3652 unsigned int optlen)
3654 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3655 struct sctp_authchunk val;
3657 if (!ep->auth_enable)
3660 if (optlen != sizeof(struct sctp_authchunk))
3662 if (copy_from_user(&val, optval, optlen))
3665 switch (val.sauth_chunk) {
3667 case SCTP_CID_INIT_ACK:
3668 case SCTP_CID_SHUTDOWN_COMPLETE:
3673 /* add this chunk id to the endpoint */
3674 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3678 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3680 * This option gets or sets the list of HMAC algorithms that the local
3681 * endpoint requires the peer to use.
3683 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3684 char __user *optval,
3685 unsigned int optlen)
3687 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3688 struct sctp_hmacalgo *hmacs;
3692 if (!ep->auth_enable)
3695 if (optlen < sizeof(struct sctp_hmacalgo))
3697 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3698 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3700 hmacs = memdup_user(optval, optlen);
3702 return PTR_ERR(hmacs);
3704 idents = hmacs->shmac_num_idents;
3705 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3706 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3711 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3718 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3720 * This option will set a shared secret key which is used to build an
3721 * association shared key.
3723 static int sctp_setsockopt_auth_key(struct sock *sk,
3724 char __user *optval,
3725 unsigned int optlen)
3727 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3728 struct sctp_authkey *authkey;
3729 struct sctp_association *asoc;
3732 if (!ep->auth_enable)
3735 if (optlen <= sizeof(struct sctp_authkey))
3737 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3740 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3741 sizeof(struct sctp_authkey));
3743 authkey = memdup_user(optval, optlen);
3744 if (IS_ERR(authkey))
3745 return PTR_ERR(authkey);
3747 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3752 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3753 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3758 ret = sctp_auth_set_key(ep, asoc, authkey);
3765 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3767 * This option will get or set the active shared key to be used to build
3768 * the association shared key.
3770 static int sctp_setsockopt_active_key(struct sock *sk,
3771 char __user *optval,
3772 unsigned int optlen)
3774 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3775 struct sctp_authkeyid val;
3776 struct sctp_association *asoc;
3778 if (!ep->auth_enable)
3781 if (optlen != sizeof(struct sctp_authkeyid))
3783 if (copy_from_user(&val, optval, optlen))
3786 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3787 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3790 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3794 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3796 * This set option will delete a shared secret key from use.
3798 static int sctp_setsockopt_del_key(struct sock *sk,
3799 char __user *optval,
3800 unsigned int optlen)
3802 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3803 struct sctp_authkeyid val;
3804 struct sctp_association *asoc;
3806 if (!ep->auth_enable)
3809 if (optlen != sizeof(struct sctp_authkeyid))
3811 if (copy_from_user(&val, optval, optlen))
3814 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3815 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3818 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3823 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3825 * This set option will deactivate a shared secret key.
3827 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3828 unsigned int optlen)
3830 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3831 struct sctp_authkeyid val;
3832 struct sctp_association *asoc;
3834 if (!ep->auth_enable)
3837 if (optlen != sizeof(struct sctp_authkeyid))
3839 if (copy_from_user(&val, optval, optlen))
3842 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3843 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3846 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3850 * 8.1.23 SCTP_AUTO_ASCONF
3852 * This option will enable or disable the use of the automatic generation of
3853 * ASCONF chunks to add and delete addresses to an existing association. Note
3854 * that this option has two caveats namely: a) it only affects sockets that
3855 * are bound to all addresses available to the SCTP stack, and b) the system
3856 * administrator may have an overriding control that turns the ASCONF feature
3857 * off no matter what setting the socket option may have.
3858 * This option expects an integer boolean flag, where a non-zero value turns on
3859 * the option, and a zero value turns off the option.
3860 * Note. In this implementation, socket operation overrides default parameter
3861 * being set by sysctl as well as FreeBSD implementation
3863 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3864 unsigned int optlen)
3867 struct sctp_sock *sp = sctp_sk(sk);
3869 if (optlen < sizeof(int))
3871 if (get_user(val, (int __user *)optval))
3873 if (!sctp_is_ep_boundall(sk) && val)
3875 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3878 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3879 if (val == 0 && sp->do_auto_asconf) {
3880 list_del(&sp->auto_asconf_list);
3881 sp->do_auto_asconf = 0;
3882 } else if (val && !sp->do_auto_asconf) {
3883 list_add_tail(&sp->auto_asconf_list,
3884 &sock_net(sk)->sctp.auto_asconf_splist);
3885 sp->do_auto_asconf = 1;
3887 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3892 * SCTP_PEER_ADDR_THLDS
3894 * This option allows us to alter the partially failed threshold for one or all
3895 * transports in an association. See Section 6.1 of:
3896 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3898 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3899 char __user *optval,
3900 unsigned int optlen)
3902 struct sctp_paddrthlds val;
3903 struct sctp_transport *trans;
3904 struct sctp_association *asoc;
3906 if (optlen < sizeof(struct sctp_paddrthlds))
3908 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3909 sizeof(struct sctp_paddrthlds)))
3913 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3914 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3917 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3919 if (val.spt_pathmaxrxt)
3920 trans->pathmaxrxt = val.spt_pathmaxrxt;
3921 trans->pf_retrans = val.spt_pathpfthld;
3924 if (val.spt_pathmaxrxt)
3925 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3926 asoc->pf_retrans = val.spt_pathpfthld;
3928 trans = sctp_addr_id2transport(sk, &val.spt_address,
3933 if (val.spt_pathmaxrxt)
3934 trans->pathmaxrxt = val.spt_pathmaxrxt;
3935 trans->pf_retrans = val.spt_pathpfthld;
3941 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3942 char __user *optval,
3943 unsigned int optlen)
3947 if (optlen < sizeof(int))
3949 if (get_user(val, (int __user *) optval))
3952 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3957 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3958 char __user *optval,
3959 unsigned int optlen)
3963 if (optlen < sizeof(int))
3965 if (get_user(val, (int __user *) optval))
3968 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3973 static int sctp_setsockopt_pr_supported(struct sock *sk,
3974 char __user *optval,
3975 unsigned int optlen)
3977 struct sctp_assoc_value params;
3979 if (optlen != sizeof(params))
3982 if (copy_from_user(¶ms, optval, optlen))
3985 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
3990 static int sctp_setsockopt_default_prinfo(struct sock *sk,
3991 char __user *optval,
3992 unsigned int optlen)
3994 struct sctp_default_prinfo info;
3995 struct sctp_association *asoc;
3996 int retval = -EINVAL;
3998 if (optlen != sizeof(info))
4001 if (copy_from_user(&info, optval, sizeof(info))) {
4006 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4009 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4012 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4014 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4015 asoc->default_timetolive = info.pr_value;
4016 } else if (!info.pr_assoc_id) {
4017 struct sctp_sock *sp = sctp_sk(sk);
4019 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4020 sp->default_timetolive = info.pr_value;
4031 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4032 char __user *optval,
4033 unsigned int optlen)
4035 struct sctp_assoc_value params;
4036 struct sctp_association *asoc;
4037 int retval = -EINVAL;
4039 if (optlen != sizeof(params))
4042 if (copy_from_user(¶ms, optval, optlen)) {
4047 asoc = sctp_id2assoc(sk, params.assoc_id);
4049 asoc->reconf_enable = !!params.assoc_value;
4050 } else if (!params.assoc_id) {
4051 struct sctp_sock *sp = sctp_sk(sk);
4053 sp->ep->reconf_enable = !!params.assoc_value;
4064 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4065 char __user *optval,
4066 unsigned int optlen)
4068 struct sctp_assoc_value params;
4069 struct sctp_association *asoc;
4070 int retval = -EINVAL;
4072 if (optlen != sizeof(params))
4075 if (copy_from_user(¶ms, optval, optlen)) {
4080 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4083 asoc = sctp_id2assoc(sk, params.assoc_id);
4085 asoc->strreset_enable = params.assoc_value;
4086 } else if (!params.assoc_id) {
4087 struct sctp_sock *sp = sctp_sk(sk);
4089 sp->ep->strreset_enable = params.assoc_value;
4100 static int sctp_setsockopt_reset_streams(struct sock *sk,
4101 char __user *optval,
4102 unsigned int optlen)
4104 struct sctp_reset_streams *params;
4105 struct sctp_association *asoc;
4106 int retval = -EINVAL;
4108 if (optlen < sizeof(*params))
4110 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4111 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4112 sizeof(__u16) * sizeof(*params));
4114 params = memdup_user(optval, optlen);
4116 return PTR_ERR(params);
4118 if (params->srs_number_streams * sizeof(__u16) >
4119 optlen - sizeof(*params))
4122 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4126 retval = sctp_send_reset_streams(asoc, params);
4133 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4134 char __user *optval,
4135 unsigned int optlen)
4137 struct sctp_association *asoc;
4138 sctp_assoc_t associd;
4139 int retval = -EINVAL;
4141 if (optlen != sizeof(associd))
4144 if (copy_from_user(&associd, optval, optlen)) {
4149 asoc = sctp_id2assoc(sk, associd);
4153 retval = sctp_send_reset_assoc(asoc);
4159 static int sctp_setsockopt_add_streams(struct sock *sk,
4160 char __user *optval,
4161 unsigned int optlen)
4163 struct sctp_association *asoc;
4164 struct sctp_add_streams params;
4165 int retval = -EINVAL;
4167 if (optlen != sizeof(params))
4170 if (copy_from_user(¶ms, optval, optlen)) {
4175 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4179 retval = sctp_send_add_streams(asoc, ¶ms);
4185 static int sctp_setsockopt_scheduler(struct sock *sk,
4186 char __user *optval,
4187 unsigned int optlen)
4189 struct sctp_association *asoc;
4190 struct sctp_assoc_value params;
4191 int retval = -EINVAL;
4193 if (optlen < sizeof(params))
4196 optlen = sizeof(params);
4197 if (copy_from_user(¶ms, optval, optlen)) {
4202 if (params.assoc_value > SCTP_SS_MAX)
4205 asoc = sctp_id2assoc(sk, params.assoc_id);
4209 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4215 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4216 char __user *optval,
4217 unsigned int optlen)
4219 struct sctp_association *asoc;
4220 struct sctp_stream_value params;
4221 int retval = -EINVAL;
4223 if (optlen < sizeof(params))
4226 optlen = sizeof(params);
4227 if (copy_from_user(¶ms, optval, optlen)) {
4232 asoc = sctp_id2assoc(sk, params.assoc_id);
4236 retval = sctp_sched_set_value(asoc, params.stream_id,
4237 params.stream_value, GFP_KERNEL);
4243 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4244 char __user *optval,
4245 unsigned int optlen)
4247 struct sctp_sock *sp = sctp_sk(sk);
4248 struct net *net = sock_net(sk);
4249 struct sctp_assoc_value params;
4250 int retval = -EINVAL;
4252 if (optlen < sizeof(params))
4255 optlen = sizeof(params);
4256 if (copy_from_user(¶ms, optval, optlen)) {
4261 if (params.assoc_id)
4264 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4269 sp->strm_interleave = !!params.assoc_value;
4277 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4278 unsigned int optlen)
4282 if (!sctp_style(sk, TCP))
4285 if (sctp_sk(sk)->ep->base.bind_addr.port)
4288 if (optlen < sizeof(int))
4291 if (get_user(val, (int __user *)optval))
4294 sctp_sk(sk)->reuse = !!val;
4299 /* API 6.2 setsockopt(), getsockopt()
4301 * Applications use setsockopt() and getsockopt() to set or retrieve
4302 * socket options. Socket options are used to change the default
4303 * behavior of sockets calls. They are described in Section 7.
4307 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4308 * int __user *optlen);
4309 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4312 * sd - the socket descript.
4313 * level - set to IPPROTO_SCTP for all SCTP options.
4314 * optname - the option name.
4315 * optval - the buffer to store the value of the option.
4316 * optlen - the size of the buffer.
4318 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4319 char __user *optval, unsigned int optlen)
4323 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4325 /* I can hardly begin to describe how wrong this is. This is
4326 * so broken as to be worse than useless. The API draft
4327 * REALLY is NOT helpful here... I am not convinced that the
4328 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4329 * are at all well-founded.
4331 if (level != SOL_SCTP) {
4332 struct sctp_af *af = sctp_sk(sk)->pf->af;
4333 retval = af->setsockopt(sk, level, optname, optval, optlen);
4340 case SCTP_SOCKOPT_BINDX_ADD:
4341 /* 'optlen' is the size of the addresses buffer. */
4342 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4343 optlen, SCTP_BINDX_ADD_ADDR);
4346 case SCTP_SOCKOPT_BINDX_REM:
4347 /* 'optlen' is the size of the addresses buffer. */
4348 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4349 optlen, SCTP_BINDX_REM_ADDR);
4352 case SCTP_SOCKOPT_CONNECTX_OLD:
4353 /* 'optlen' is the size of the addresses buffer. */
4354 retval = sctp_setsockopt_connectx_old(sk,
4355 (struct sockaddr __user *)optval,
4359 case SCTP_SOCKOPT_CONNECTX:
4360 /* 'optlen' is the size of the addresses buffer. */
4361 retval = sctp_setsockopt_connectx(sk,
4362 (struct sockaddr __user *)optval,
4366 case SCTP_DISABLE_FRAGMENTS:
4367 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4371 retval = sctp_setsockopt_events(sk, optval, optlen);
4374 case SCTP_AUTOCLOSE:
4375 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4378 case SCTP_PEER_ADDR_PARAMS:
4379 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4382 case SCTP_DELAYED_SACK:
4383 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4385 case SCTP_PARTIAL_DELIVERY_POINT:
4386 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4390 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4392 case SCTP_DEFAULT_SEND_PARAM:
4393 retval = sctp_setsockopt_default_send_param(sk, optval,
4396 case SCTP_DEFAULT_SNDINFO:
4397 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4399 case SCTP_PRIMARY_ADDR:
4400 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4402 case SCTP_SET_PEER_PRIMARY_ADDR:
4403 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4406 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4409 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4411 case SCTP_ASSOCINFO:
4412 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4414 case SCTP_I_WANT_MAPPED_V4_ADDR:
4415 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4418 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4420 case SCTP_ADAPTATION_LAYER:
4421 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4424 retval = sctp_setsockopt_context(sk, optval, optlen);
4426 case SCTP_FRAGMENT_INTERLEAVE:
4427 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4429 case SCTP_MAX_BURST:
4430 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4432 case SCTP_AUTH_CHUNK:
4433 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4435 case SCTP_HMAC_IDENT:
4436 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4439 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4441 case SCTP_AUTH_ACTIVE_KEY:
4442 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4444 case SCTP_AUTH_DELETE_KEY:
4445 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4447 case SCTP_AUTH_DEACTIVATE_KEY:
4448 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4450 case SCTP_AUTO_ASCONF:
4451 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4453 case SCTP_PEER_ADDR_THLDS:
4454 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4456 case SCTP_RECVRCVINFO:
4457 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4459 case SCTP_RECVNXTINFO:
4460 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4462 case SCTP_PR_SUPPORTED:
4463 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4465 case SCTP_DEFAULT_PRINFO:
4466 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4468 case SCTP_RECONFIG_SUPPORTED:
4469 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4471 case SCTP_ENABLE_STREAM_RESET:
4472 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4474 case SCTP_RESET_STREAMS:
4475 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4477 case SCTP_RESET_ASSOC:
4478 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4480 case SCTP_ADD_STREAMS:
4481 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4483 case SCTP_STREAM_SCHEDULER:
4484 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4486 case SCTP_STREAM_SCHEDULER_VALUE:
4487 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4489 case SCTP_INTERLEAVING_SUPPORTED:
4490 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4493 case SCTP_REUSE_PORT:
4494 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4497 retval = -ENOPROTOOPT;
4507 /* API 3.1.6 connect() - UDP Style Syntax
4509 * An application may use the connect() call in the UDP model to initiate an
4510 * association without sending data.
4514 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4516 * sd: the socket descriptor to have a new association added to.
4518 * nam: the address structure (either struct sockaddr_in or struct
4519 * sockaddr_in6 defined in RFC2553 [7]).
4521 * len: the size of the address.
4523 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4524 int addr_len, int flags)
4531 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4534 /* Validate addr_len before calling common connect/connectx routine. */
4535 af = sctp_get_af_specific(addr->sa_family);
4536 if (af && addr_len >= af->sockaddr_len)
4537 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4543 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4544 int addr_len, int flags)
4546 if (addr_len < sizeof(uaddr->sa_family))
4549 if (uaddr->sa_family == AF_UNSPEC)
4552 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4555 /* FIXME: Write comments. */
4556 static int sctp_disconnect(struct sock *sk, int flags)
4558 return -EOPNOTSUPP; /* STUB */
4561 /* 4.1.4 accept() - TCP Style Syntax
4563 * Applications use accept() call to remove an established SCTP
4564 * association from the accept queue of the endpoint. A new socket
4565 * descriptor will be returned from accept() to represent the newly
4566 * formed association.
4568 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4570 struct sctp_sock *sp;
4571 struct sctp_endpoint *ep;
4572 struct sock *newsk = NULL;
4573 struct sctp_association *asoc;
4582 if (!sctp_style(sk, TCP)) {
4583 error = -EOPNOTSUPP;
4587 if (!sctp_sstate(sk, LISTENING)) {
4592 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4594 error = sctp_wait_for_accept(sk, timeo);
4598 /* We treat the list of associations on the endpoint as the accept
4599 * queue and pick the first association on the list.
4601 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4603 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4609 /* Populate the fields of the newsk from the oldsk and migrate the
4610 * asoc to the newsk.
4612 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4620 /* The SCTP ioctl handler. */
4621 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4628 * SEQPACKET-style sockets in LISTENING state are valid, for
4629 * SCTP, so only discard TCP-style sockets in LISTENING state.
4631 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4636 struct sk_buff *skb;
4637 unsigned int amount = 0;
4639 skb = skb_peek(&sk->sk_receive_queue);
4642 * We will only return the amount of this packet since
4643 * that is all that will be read.
4647 rc = put_user(amount, (int __user *)arg);
4659 /* This is the function which gets called during socket creation to
4660 * initialized the SCTP-specific portion of the sock.
4661 * The sock structure should already be zero-filled memory.
4663 static int sctp_init_sock(struct sock *sk)
4665 struct net *net = sock_net(sk);
4666 struct sctp_sock *sp;
4668 pr_debug("%s: sk:%p\n", __func__, sk);
4672 /* Initialize the SCTP per socket area. */
4673 switch (sk->sk_type) {
4674 case SOCK_SEQPACKET:
4675 sp->type = SCTP_SOCKET_UDP;
4678 sp->type = SCTP_SOCKET_TCP;
4681 return -ESOCKTNOSUPPORT;
4684 sk->sk_gso_type = SKB_GSO_SCTP;
4686 /* Initialize default send parameters. These parameters can be
4687 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4689 sp->default_stream = 0;
4690 sp->default_ppid = 0;
4691 sp->default_flags = 0;
4692 sp->default_context = 0;
4693 sp->default_timetolive = 0;
4695 sp->default_rcv_context = 0;
4696 sp->max_burst = net->sctp.max_burst;
4698 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4700 /* Initialize default setup parameters. These parameters
4701 * can be modified with the SCTP_INITMSG socket option or
4702 * overridden by the SCTP_INIT CMSG.
4704 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4705 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
4706 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4707 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
4709 /* Initialize default RTO related parameters. These parameters can
4710 * be modified for with the SCTP_RTOINFO socket option.
4712 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4713 sp->rtoinfo.srto_max = net->sctp.rto_max;
4714 sp->rtoinfo.srto_min = net->sctp.rto_min;
4716 /* Initialize default association related parameters. These parameters
4717 * can be modified with the SCTP_ASSOCINFO socket option.
4719 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
4720 sp->assocparams.sasoc_number_peer_destinations = 0;
4721 sp->assocparams.sasoc_peer_rwnd = 0;
4722 sp->assocparams.sasoc_local_rwnd = 0;
4723 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
4725 /* Initialize default event subscriptions. By default, all the
4728 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4730 /* Default Peer Address Parameters. These defaults can
4731 * be modified via SCTP_PEER_ADDR_PARAMS
4733 sp->hbinterval = net->sctp.hb_interval;
4734 sp->pathmaxrxt = net->sctp.max_retrans_path;
4735 sp->pathmtu = 0; /* allow default discovery */
4736 sp->sackdelay = net->sctp.sack_timeout;
4738 sp->param_flags = SPP_HB_ENABLE |
4740 SPP_SACKDELAY_ENABLE;
4742 /* If enabled no SCTP message fragmentation will be performed.
4743 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4745 sp->disable_fragments = 0;
4747 /* Enable Nagle algorithm by default. */
4750 sp->recvrcvinfo = 0;
4751 sp->recvnxtinfo = 0;
4753 /* Enable by default. */
4756 /* Auto-close idle associations after the configured
4757 * number of seconds. A value of 0 disables this
4758 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4759 * for UDP-style sockets only.
4763 /* User specified fragmentation limit. */
4766 sp->adaptation_ind = 0;
4768 sp->pf = sctp_get_pf_specific(sk->sk_family);
4770 /* Control variables for partial data delivery. */
4771 atomic_set(&sp->pd_mode, 0);
4772 skb_queue_head_init(&sp->pd_lobby);
4773 sp->frag_interleave = 0;
4775 /* Create a per socket endpoint structure. Even if we
4776 * change the data structure relationships, this may still
4777 * be useful for storing pre-connect address information.
4779 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4785 sk->sk_destruct = sctp_destruct_sock;
4787 SCTP_DBG_OBJCNT_INC(sock);
4790 sk_sockets_allocated_inc(sk);
4791 sock_prot_inuse_add(net, sk->sk_prot, 1);
4798 /* Cleanup any SCTP per socket resources. Must be called with
4799 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4801 static void sctp_destroy_sock(struct sock *sk)
4803 struct sctp_sock *sp;
4805 pr_debug("%s: sk:%p\n", __func__, sk);
4807 /* Release our hold on the endpoint. */
4809 /* This could happen during socket init, thus we bail out
4810 * early, since the rest of the below is not setup either.
4815 if (sp->do_auto_asconf) {
4816 sp->do_auto_asconf = 0;
4817 list_del(&sp->auto_asconf_list);
4819 sctp_endpoint_free(sp->ep);
4821 sk_sockets_allocated_dec(sk);
4822 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4826 /* Triggered when there are no references on the socket anymore */
4827 static void sctp_destruct_sock(struct sock *sk)
4829 struct sctp_sock *sp = sctp_sk(sk);
4831 /* Free up the HMAC transform. */
4832 crypto_free_shash(sp->hmac);
4834 inet_sock_destruct(sk);
4837 /* API 4.1.7 shutdown() - TCP Style Syntax
4838 * int shutdown(int socket, int how);
4840 * sd - the socket descriptor of the association to be closed.
4841 * how - Specifies the type of shutdown. The values are
4844 * Disables further receive operations. No SCTP
4845 * protocol action is taken.
4847 * Disables further send operations, and initiates
4848 * the SCTP shutdown sequence.
4850 * Disables further send and receive operations
4851 * and initiates the SCTP shutdown sequence.
4853 static void sctp_shutdown(struct sock *sk, int how)
4855 struct net *net = sock_net(sk);
4856 struct sctp_endpoint *ep;
4858 if (!sctp_style(sk, TCP))
4861 ep = sctp_sk(sk)->ep;
4862 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4863 struct sctp_association *asoc;
4865 inet_sk_set_state(sk, SCTP_SS_CLOSING);
4866 asoc = list_entry(ep->asocs.next,
4867 struct sctp_association, asocs);
4868 sctp_primitive_SHUTDOWN(net, asoc, NULL);
4872 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4873 struct sctp_info *info)
4875 struct sctp_transport *prim;
4876 struct list_head *pos;
4879 memset(info, 0, sizeof(*info));
4881 struct sctp_sock *sp = sctp_sk(sk);
4883 info->sctpi_s_autoclose = sp->autoclose;
4884 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4885 info->sctpi_s_pd_point = sp->pd_point;
4886 info->sctpi_s_nodelay = sp->nodelay;
4887 info->sctpi_s_disable_fragments = sp->disable_fragments;
4888 info->sctpi_s_v4mapped = sp->v4mapped;
4889 info->sctpi_s_frag_interleave = sp->frag_interleave;
4890 info->sctpi_s_type = sp->type;
4895 info->sctpi_tag = asoc->c.my_vtag;
4896 info->sctpi_state = asoc->state;
4897 info->sctpi_rwnd = asoc->a_rwnd;
4898 info->sctpi_unackdata = asoc->unack_data;
4899 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4900 info->sctpi_instrms = asoc->stream.incnt;
4901 info->sctpi_outstrms = asoc->stream.outcnt;
4902 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4903 info->sctpi_inqueue++;
4904 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4905 info->sctpi_outqueue++;
4906 info->sctpi_overall_error = asoc->overall_error_count;
4907 info->sctpi_max_burst = asoc->max_burst;
4908 info->sctpi_maxseg = asoc->frag_point;
4909 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4910 info->sctpi_peer_tag = asoc->c.peer_vtag;
4912 mask = asoc->peer.ecn_capable << 1;
4913 mask = (mask | asoc->peer.ipv4_address) << 1;
4914 mask = (mask | asoc->peer.ipv6_address) << 1;
4915 mask = (mask | asoc->peer.hostname_address) << 1;
4916 mask = (mask | asoc->peer.asconf_capable) << 1;
4917 mask = (mask | asoc->peer.prsctp_capable) << 1;
4918 mask = (mask | asoc->peer.auth_capable);
4919 info->sctpi_peer_capable = mask;
4920 mask = asoc->peer.sack_needed << 1;
4921 mask = (mask | asoc->peer.sack_generation) << 1;
4922 mask = (mask | asoc->peer.zero_window_announced);
4923 info->sctpi_peer_sack = mask;
4925 info->sctpi_isacks = asoc->stats.isacks;
4926 info->sctpi_osacks = asoc->stats.osacks;
4927 info->sctpi_opackets = asoc->stats.opackets;
4928 info->sctpi_ipackets = asoc->stats.ipackets;
4929 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4930 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4931 info->sctpi_idupchunks = asoc->stats.idupchunks;
4932 info->sctpi_gapcnt = asoc->stats.gapcnt;
4933 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4934 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4935 info->sctpi_oodchunks = asoc->stats.oodchunks;
4936 info->sctpi_iodchunks = asoc->stats.iodchunks;
4937 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4938 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4940 prim = asoc->peer.primary_path;
4941 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
4942 info->sctpi_p_state = prim->state;
4943 info->sctpi_p_cwnd = prim->cwnd;
4944 info->sctpi_p_srtt = prim->srtt;
4945 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4946 info->sctpi_p_hbinterval = prim->hbinterval;
4947 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4948 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4949 info->sctpi_p_ssthresh = prim->ssthresh;
4950 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4951 info->sctpi_p_flight_size = prim->flight_size;
4952 info->sctpi_p_error = prim->error_count;
4956 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4958 /* use callback to avoid exporting the core structure */
4959 void sctp_transport_walk_start(struct rhashtable_iter *iter)
4961 rhltable_walk_enter(&sctp_transport_hashtable, iter);
4963 rhashtable_walk_start(iter);
4966 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4968 rhashtable_walk_stop(iter);
4969 rhashtable_walk_exit(iter);
4972 struct sctp_transport *sctp_transport_get_next(struct net *net,
4973 struct rhashtable_iter *iter)
4975 struct sctp_transport *t;
4977 t = rhashtable_walk_next(iter);
4978 for (; t; t = rhashtable_walk_next(iter)) {
4980 if (PTR_ERR(t) == -EAGAIN)
4985 if (!sctp_transport_hold(t))
4988 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4989 t->asoc->peer.primary_path == t)
4992 sctp_transport_put(t);
4998 struct sctp_transport *sctp_transport_get_idx(struct net *net,
4999 struct rhashtable_iter *iter,
5002 struct sctp_transport *t;
5005 return SEQ_START_TOKEN;
5007 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5010 sctp_transport_put(t);
5016 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5020 struct sctp_ep_common *epb;
5021 struct sctp_hashbucket *head;
5023 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5025 read_lock_bh(&head->lock);
5026 sctp_for_each_hentry(epb, &head->chain) {
5027 err = cb(sctp_ep(epb), p);
5031 read_unlock_bh(&head->lock);
5036 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5038 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5040 const union sctp_addr *laddr,
5041 const union sctp_addr *paddr, void *p)
5043 struct sctp_transport *transport;
5047 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5052 err = cb(transport, p);
5053 sctp_transport_put(transport);
5057 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5059 int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
5060 struct net *net, int *pos, void *p)
5062 struct rhashtable_iter hti;
5063 struct sctp_transport *tsp;
5064 struct sctp_endpoint *ep;
5069 sctp_transport_walk_start(&hti);
5071 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5072 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5074 if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
5075 ret = cb(ep, tsp, p);
5078 sctp_endpoint_put(ep);
5081 sctp_transport_put(tsp);
5083 sctp_transport_walk_stop(&hti);
5086 if (cb_done && !cb_done(ep, tsp, p)) {
5088 sctp_endpoint_put(ep);
5089 sctp_transport_put(tsp);
5092 sctp_endpoint_put(ep);
5093 sctp_transport_put(tsp);
5098 EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
5100 /* 7.2.1 Association Status (SCTP_STATUS)
5102 * Applications can retrieve current status information about an
5103 * association, including association state, peer receiver window size,
5104 * number of unacked data chunks, and number of data chunks pending
5105 * receipt. This information is read-only.
5107 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5108 char __user *optval,
5111 struct sctp_status status;
5112 struct sctp_association *asoc = NULL;
5113 struct sctp_transport *transport;
5114 sctp_assoc_t associd;
5117 if (len < sizeof(status)) {
5122 len = sizeof(status);
5123 if (copy_from_user(&status, optval, len)) {
5128 associd = status.sstat_assoc_id;
5129 asoc = sctp_id2assoc(sk, associd);
5135 transport = asoc->peer.primary_path;
5137 status.sstat_assoc_id = sctp_assoc2id(asoc);
5138 status.sstat_state = sctp_assoc_to_state(asoc);
5139 status.sstat_rwnd = asoc->peer.rwnd;
5140 status.sstat_unackdata = asoc->unack_data;
5142 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5143 status.sstat_instrms = asoc->stream.incnt;
5144 status.sstat_outstrms = asoc->stream.outcnt;
5145 status.sstat_fragmentation_point = asoc->frag_point;
5146 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5147 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5148 transport->af_specific->sockaddr_len);
5149 /* Map ipv4 address into v4-mapped-on-v6 address. */
5150 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5151 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5152 status.sstat_primary.spinfo_state = transport->state;
5153 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5154 status.sstat_primary.spinfo_srtt = transport->srtt;
5155 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5156 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5158 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5159 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5161 if (put_user(len, optlen)) {
5166 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5167 __func__, len, status.sstat_state, status.sstat_rwnd,
5168 status.sstat_assoc_id);
5170 if (copy_to_user(optval, &status, len)) {
5180 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5182 * Applications can retrieve information about a specific peer address
5183 * of an association, including its reachability state, congestion
5184 * window, and retransmission timer values. This information is
5187 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5188 char __user *optval,
5191 struct sctp_paddrinfo pinfo;
5192 struct sctp_transport *transport;
5195 if (len < sizeof(pinfo)) {
5200 len = sizeof(pinfo);
5201 if (copy_from_user(&pinfo, optval, len)) {
5206 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5207 pinfo.spinfo_assoc_id);
5211 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5212 pinfo.spinfo_state = transport->state;
5213 pinfo.spinfo_cwnd = transport->cwnd;
5214 pinfo.spinfo_srtt = transport->srtt;
5215 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5216 pinfo.spinfo_mtu = transport->pathmtu;
5218 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5219 pinfo.spinfo_state = SCTP_ACTIVE;
5221 if (put_user(len, optlen)) {
5226 if (copy_to_user(optval, &pinfo, len)) {
5235 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5237 * This option is a on/off flag. If enabled no SCTP message
5238 * fragmentation will be performed. Instead if a message being sent
5239 * exceeds the current PMTU size, the message will NOT be sent and
5240 * instead a error will be indicated to the user.
5242 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5243 char __user *optval, int __user *optlen)
5247 if (len < sizeof(int))
5251 val = (sctp_sk(sk)->disable_fragments == 1);
5252 if (put_user(len, optlen))
5254 if (copy_to_user(optval, &val, len))
5259 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5261 * This socket option is used to specify various notifications and
5262 * ancillary data the user wishes to receive.
5264 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5269 if (len > sizeof(struct sctp_event_subscribe))
5270 len = sizeof(struct sctp_event_subscribe);
5271 if (put_user(len, optlen))
5273 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5278 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5280 * This socket option is applicable to the UDP-style socket only. When
5281 * set it will cause associations that are idle for more than the
5282 * specified number of seconds to automatically close. An association
5283 * being idle is defined an association that has NOT sent or received
5284 * user data. The special value of '0' indicates that no automatic
5285 * close of any associations should be performed. The option expects an
5286 * integer defining the number of seconds of idle time before an
5287 * association is closed.
5289 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5291 /* Applicable to UDP-style socket only */
5292 if (sctp_style(sk, TCP))
5294 if (len < sizeof(int))
5297 if (put_user(len, optlen))
5299 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5304 /* Helper routine to branch off an association to a new socket. */
5305 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5307 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5308 struct sctp_sock *sp = sctp_sk(sk);
5309 struct socket *sock;
5312 /* Do not peel off from one netns to another one. */
5313 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5319 /* An association cannot be branched off from an already peeled-off
5320 * socket, nor is this supported for tcp style sockets.
5322 if (!sctp_style(sk, UDP))
5325 /* Create a new socket. */
5326 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5330 sctp_copy_sock(sock->sk, sk, asoc);
5332 /* Make peeled-off sockets more like 1-1 accepted sockets.
5333 * Set the daddr and initialize id to something more random and also
5334 * copy over any ip options.
5336 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
5337 sp->pf->copy_ip_options(sk, sock->sk);
5339 /* Populate the fields of the newsk from the oldsk and migrate the
5340 * asoc to the newsk.
5342 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5348 EXPORT_SYMBOL(sctp_do_peeloff);
5350 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5351 struct file **newfile, unsigned flags)
5353 struct socket *newsock;
5356 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5360 /* Map the socket to an unused fd that can be returned to the user. */
5361 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5363 sock_release(newsock);
5367 *newfile = sock_alloc_file(newsock, 0, NULL);
5368 if (IS_ERR(*newfile)) {
5369 put_unused_fd(retval);
5370 retval = PTR_ERR(*newfile);
5375 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5378 peeloff->sd = retval;
5380 if (flags & SOCK_NONBLOCK)
5381 (*newfile)->f_flags |= O_NONBLOCK;
5386 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5388 sctp_peeloff_arg_t peeloff;
5389 struct file *newfile = NULL;
5392 if (len < sizeof(sctp_peeloff_arg_t))
5394 len = sizeof(sctp_peeloff_arg_t);
5395 if (copy_from_user(&peeloff, optval, len))
5398 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5402 /* Return the fd mapped to the new socket. */
5403 if (put_user(len, optlen)) {
5405 put_unused_fd(retval);
5409 if (copy_to_user(optval, &peeloff, len)) {
5411 put_unused_fd(retval);
5414 fd_install(retval, newfile);
5419 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5420 char __user *optval, int __user *optlen)
5422 sctp_peeloff_flags_arg_t peeloff;
5423 struct file *newfile = NULL;
5426 if (len < sizeof(sctp_peeloff_flags_arg_t))
5428 len = sizeof(sctp_peeloff_flags_arg_t);
5429 if (copy_from_user(&peeloff, optval, len))
5432 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5433 &newfile, peeloff.flags);
5437 /* Return the fd mapped to the new socket. */
5438 if (put_user(len, optlen)) {
5440 put_unused_fd(retval);
5444 if (copy_to_user(optval, &peeloff, len)) {
5446 put_unused_fd(retval);
5449 fd_install(retval, newfile);
5454 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5456 * Applications can enable or disable heartbeats for any peer address of
5457 * an association, modify an address's heartbeat interval, force a
5458 * heartbeat to be sent immediately, and adjust the address's maximum
5459 * number of retransmissions sent before an address is considered
5460 * unreachable. The following structure is used to access and modify an
5461 * address's parameters:
5463 * struct sctp_paddrparams {
5464 * sctp_assoc_t spp_assoc_id;
5465 * struct sockaddr_storage spp_address;
5466 * uint32_t spp_hbinterval;
5467 * uint16_t spp_pathmaxrxt;
5468 * uint32_t spp_pathmtu;
5469 * uint32_t spp_sackdelay;
5470 * uint32_t spp_flags;
5473 * spp_assoc_id - (one-to-many style socket) This is filled in the
5474 * application, and identifies the association for
5476 * spp_address - This specifies which address is of interest.
5477 * spp_hbinterval - This contains the value of the heartbeat interval,
5478 * in milliseconds. If a value of zero
5479 * is present in this field then no changes are to
5480 * be made to this parameter.
5481 * spp_pathmaxrxt - This contains the maximum number of
5482 * retransmissions before this address shall be
5483 * considered unreachable. If a value of zero
5484 * is present in this field then no changes are to
5485 * be made to this parameter.
5486 * spp_pathmtu - When Path MTU discovery is disabled the value
5487 * specified here will be the "fixed" path mtu.
5488 * Note that if the spp_address field is empty
5489 * then all associations on this address will
5490 * have this fixed path mtu set upon them.
5492 * spp_sackdelay - When delayed sack is enabled, this value specifies
5493 * the number of milliseconds that sacks will be delayed
5494 * for. This value will apply to all addresses of an
5495 * association if the spp_address field is empty. Note
5496 * also, that if delayed sack is enabled and this
5497 * value is set to 0, no change is made to the last
5498 * recorded delayed sack timer value.
5500 * spp_flags - These flags are used to control various features
5501 * on an association. The flag field may contain
5502 * zero or more of the following options.
5504 * SPP_HB_ENABLE - Enable heartbeats on the
5505 * specified address. Note that if the address
5506 * field is empty all addresses for the association
5507 * have heartbeats enabled upon them.
5509 * SPP_HB_DISABLE - Disable heartbeats on the
5510 * speicifed address. Note that if the address
5511 * field is empty all addresses for the association
5512 * will have their heartbeats disabled. Note also
5513 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5514 * mutually exclusive, only one of these two should
5515 * be specified. Enabling both fields will have
5516 * undetermined results.
5518 * SPP_HB_DEMAND - Request a user initiated heartbeat
5519 * to be made immediately.
5521 * SPP_PMTUD_ENABLE - This field will enable PMTU
5522 * discovery upon the specified address. Note that
5523 * if the address feild is empty then all addresses
5524 * on the association are effected.
5526 * SPP_PMTUD_DISABLE - This field will disable PMTU
5527 * discovery upon the specified address. Note that
5528 * if the address feild is empty then all addresses
5529 * on the association are effected. Not also that
5530 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5531 * exclusive. Enabling both will have undetermined
5534 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5535 * on delayed sack. The time specified in spp_sackdelay
5536 * is used to specify the sack delay for this address. Note
5537 * that if spp_address is empty then all addresses will
5538 * enable delayed sack and take on the sack delay
5539 * value specified in spp_sackdelay.
5540 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5541 * off delayed sack. If the spp_address field is blank then
5542 * delayed sack is disabled for the entire association. Note
5543 * also that this field is mutually exclusive to
5544 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5547 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5548 * setting of the IPV6 flow label value. The value is
5549 * contained in the spp_ipv6_flowlabel field.
5550 * Upon retrieval, this flag will be set to indicate that
5551 * the spp_ipv6_flowlabel field has a valid value returned.
5552 * If a specific destination address is set (in the
5553 * spp_address field), then the value returned is that of
5554 * the address. If just an association is specified (and
5555 * no address), then the association's default flow label
5556 * is returned. If neither an association nor a destination
5557 * is specified, then the socket's default flow label is
5558 * returned. For non-IPv6 sockets, this flag will be left
5561 * SPP_DSCP: Setting this flag enables the setting of the
5562 * Differentiated Services Code Point (DSCP) value
5563 * associated with either the association or a specific
5564 * address. The value is obtained in the spp_dscp field.
5565 * Upon retrieval, this flag will be set to indicate that
5566 * the spp_dscp field has a valid value returned. If a
5567 * specific destination address is set when called (in the
5568 * spp_address field), then that specific destination
5569 * address's DSCP value is returned. If just an association
5570 * is specified, then the association's default DSCP is
5571 * returned. If neither an association nor a destination is
5572 * specified, then the socket's default DSCP is returned.
5574 * spp_ipv6_flowlabel
5575 * - This field is used in conjunction with the
5576 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5577 * The 20 least significant bits are used for the flow
5578 * label. This setting has precedence over any IPv6-layer
5581 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5582 * and contains the DSCP. The 6 most significant bits are
5583 * used for the DSCP. This setting has precedence over any
5584 * IPv4- or IPv6- layer setting.
5586 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5587 char __user *optval, int __user *optlen)
5589 struct sctp_paddrparams params;
5590 struct sctp_transport *trans = NULL;
5591 struct sctp_association *asoc = NULL;
5592 struct sctp_sock *sp = sctp_sk(sk);
5594 if (len >= sizeof(params))
5595 len = sizeof(params);
5596 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5597 spp_ipv6_flowlabel), 4))
5598 len = ALIGN(offsetof(struct sctp_paddrparams,
5599 spp_ipv6_flowlabel), 4);
5603 if (copy_from_user(¶ms, optval, len))
5606 /* If an address other than INADDR_ANY is specified, and
5607 * no transport is found, then the request is invalid.
5609 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5610 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5611 params.spp_assoc_id);
5613 pr_debug("%s: failed no transport\n", __func__);
5618 /* Get association, if assoc_id != 0 and the socket is a one
5619 * to many style socket, and an association was not found, then
5620 * the id was invalid.
5622 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5623 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
5624 pr_debug("%s: failed no association\n", __func__);
5629 /* Fetch transport values. */
5630 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5631 params.spp_pathmtu = trans->pathmtu;
5632 params.spp_pathmaxrxt = trans->pathmaxrxt;
5633 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5635 /*draft-11 doesn't say what to return in spp_flags*/
5636 params.spp_flags = trans->param_flags;
5637 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5638 params.spp_ipv6_flowlabel = trans->flowlabel &
5639 SCTP_FLOWLABEL_VAL_MASK;
5640 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5642 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5643 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5644 params.spp_flags |= SPP_DSCP;
5647 /* Fetch association values. */
5648 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5649 params.spp_pathmtu = asoc->pathmtu;
5650 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5651 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5653 /*draft-11 doesn't say what to return in spp_flags*/
5654 params.spp_flags = asoc->param_flags;
5655 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5656 params.spp_ipv6_flowlabel = asoc->flowlabel &
5657 SCTP_FLOWLABEL_VAL_MASK;
5658 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5660 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5661 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5662 params.spp_flags |= SPP_DSCP;
5665 /* Fetch socket values. */
5666 params.spp_hbinterval = sp->hbinterval;
5667 params.spp_pathmtu = sp->pathmtu;
5668 params.spp_sackdelay = sp->sackdelay;
5669 params.spp_pathmaxrxt = sp->pathmaxrxt;
5671 /*draft-11 doesn't say what to return in spp_flags*/
5672 params.spp_flags = sp->param_flags;
5673 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5674 params.spp_ipv6_flowlabel = sp->flowlabel &
5675 SCTP_FLOWLABEL_VAL_MASK;
5676 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5678 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5679 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5680 params.spp_flags |= SPP_DSCP;
5684 if (copy_to_user(optval, ¶ms, len))
5687 if (put_user(len, optlen))
5694 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5696 * This option will effect the way delayed acks are performed. This
5697 * option allows you to get or set the delayed ack time, in
5698 * milliseconds. It also allows changing the delayed ack frequency.
5699 * Changing the frequency to 1 disables the delayed sack algorithm. If
5700 * the assoc_id is 0, then this sets or gets the endpoints default
5701 * values. If the assoc_id field is non-zero, then the set or get
5702 * effects the specified association for the one to many model (the
5703 * assoc_id field is ignored by the one to one model). Note that if
5704 * sack_delay or sack_freq are 0 when setting this option, then the
5705 * current values will remain unchanged.
5707 * struct sctp_sack_info {
5708 * sctp_assoc_t sack_assoc_id;
5709 * uint32_t sack_delay;
5710 * uint32_t sack_freq;
5713 * sack_assoc_id - This parameter, indicates which association the user
5714 * is performing an action upon. Note that if this field's value is
5715 * zero then the endpoints default value is changed (effecting future
5716 * associations only).
5718 * sack_delay - This parameter contains the number of milliseconds that
5719 * the user is requesting the delayed ACK timer be set to. Note that
5720 * this value is defined in the standard to be between 200 and 500
5723 * sack_freq - This parameter contains the number of packets that must
5724 * be received before a sack is sent without waiting for the delay
5725 * timer to expire. The default value for this is 2, setting this
5726 * value to 1 will disable the delayed sack algorithm.
5728 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5729 char __user *optval,
5732 struct sctp_sack_info params;
5733 struct sctp_association *asoc = NULL;
5734 struct sctp_sock *sp = sctp_sk(sk);
5736 if (len >= sizeof(struct sctp_sack_info)) {
5737 len = sizeof(struct sctp_sack_info);
5739 if (copy_from_user(¶ms, optval, len))
5741 } else if (len == sizeof(struct sctp_assoc_value)) {
5742 pr_warn_ratelimited(DEPRECATED
5744 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5745 "Use struct sctp_sack_info instead\n",
5746 current->comm, task_pid_nr(current));
5747 if (copy_from_user(¶ms, optval, len))
5752 /* Get association, if sack_assoc_id != 0 and the socket is a one
5753 * to many style socket, and an association was not found, then
5754 * the id was invalid.
5756 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5757 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
5761 /* Fetch association values. */
5762 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5763 params.sack_delay = jiffies_to_msecs(
5765 params.sack_freq = asoc->sackfreq;
5768 params.sack_delay = 0;
5769 params.sack_freq = 1;
5772 /* Fetch socket values. */
5773 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5774 params.sack_delay = sp->sackdelay;
5775 params.sack_freq = sp->sackfreq;
5777 params.sack_delay = 0;
5778 params.sack_freq = 1;
5782 if (copy_to_user(optval, ¶ms, len))
5785 if (put_user(len, optlen))
5791 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5793 * Applications can specify protocol parameters for the default association
5794 * initialization. The option name argument to setsockopt() and getsockopt()
5797 * Setting initialization parameters is effective only on an unconnected
5798 * socket (for UDP-style sockets only future associations are effected
5799 * by the change). With TCP-style sockets, this option is inherited by
5800 * sockets derived from a listener socket.
5802 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5804 if (len < sizeof(struct sctp_initmsg))
5806 len = sizeof(struct sctp_initmsg);
5807 if (put_user(len, optlen))
5809 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5815 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5816 char __user *optval, int __user *optlen)
5818 struct sctp_association *asoc;
5820 struct sctp_getaddrs getaddrs;
5821 struct sctp_transport *from;
5823 union sctp_addr temp;
5824 struct sctp_sock *sp = sctp_sk(sk);
5829 if (len < sizeof(struct sctp_getaddrs))
5832 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5835 /* For UDP-style sockets, id specifies the association to query. */
5836 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5840 to = optval + offsetof(struct sctp_getaddrs, addrs);
5841 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5843 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5845 memcpy(&temp, &from->ipaddr, sizeof(temp));
5846 addrlen = sctp_get_pf_specific(sk->sk_family)
5847 ->addr_to_user(sp, &temp);
5848 if (space_left < addrlen)
5850 if (copy_to_user(to, &temp, addrlen))
5854 space_left -= addrlen;
5857 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5859 bytes_copied = ((char __user *)to) - optval;
5860 if (put_user(bytes_copied, optlen))
5866 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5867 size_t space_left, int *bytes_copied)
5869 struct sctp_sockaddr_entry *addr;
5870 union sctp_addr temp;
5873 struct net *net = sock_net(sk);
5876 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
5880 if ((PF_INET == sk->sk_family) &&
5881 (AF_INET6 == addr->a.sa.sa_family))
5883 if ((PF_INET6 == sk->sk_family) &&
5884 inet_v6_ipv6only(sk) &&
5885 (AF_INET == addr->a.sa.sa_family))
5887 memcpy(&temp, &addr->a, sizeof(temp));
5888 if (!temp.v4.sin_port)
5889 temp.v4.sin_port = htons(port);
5891 addrlen = sctp_get_pf_specific(sk->sk_family)
5892 ->addr_to_user(sctp_sk(sk), &temp);
5894 if (space_left < addrlen) {
5898 memcpy(to, &temp, addrlen);
5902 space_left -= addrlen;
5903 *bytes_copied += addrlen;
5911 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5912 char __user *optval, int __user *optlen)
5914 struct sctp_bind_addr *bp;
5915 struct sctp_association *asoc;
5917 struct sctp_getaddrs getaddrs;
5918 struct sctp_sockaddr_entry *addr;
5920 union sctp_addr temp;
5921 struct sctp_sock *sp = sctp_sk(sk);
5925 int bytes_copied = 0;
5929 if (len < sizeof(struct sctp_getaddrs))
5932 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5936 * For UDP-style sockets, id specifies the association to query.
5937 * If the id field is set to the value '0' then the locally bound
5938 * addresses are returned without regard to any particular
5941 if (0 == getaddrs.assoc_id) {
5942 bp = &sctp_sk(sk)->ep->base.bind_addr;
5944 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5947 bp = &asoc->base.bind_addr;
5950 to = optval + offsetof(struct sctp_getaddrs, addrs);
5951 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5953 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
5957 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5958 * addresses from the global local address list.
5960 if (sctp_list_single_entry(&bp->address_list)) {
5961 addr = list_entry(bp->address_list.next,
5962 struct sctp_sockaddr_entry, list);
5963 if (sctp_is_any(sk, &addr->a)) {
5964 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5965 space_left, &bytes_copied);
5975 /* Protection on the bound address list is not needed since
5976 * in the socket option context we hold a socket lock and
5977 * thus the bound address list can't change.
5979 list_for_each_entry(addr, &bp->address_list, list) {
5980 memcpy(&temp, &addr->a, sizeof(temp));
5981 addrlen = sctp_get_pf_specific(sk->sk_family)
5982 ->addr_to_user(sp, &temp);
5983 if (space_left < addrlen) {
5984 err = -ENOMEM; /*fixme: right error?*/
5987 memcpy(buf, &temp, addrlen);
5989 bytes_copied += addrlen;
5991 space_left -= addrlen;
5995 if (copy_to_user(to, addrs, bytes_copied)) {
5999 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6003 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6004 * but we can't change it anymore.
6006 if (put_user(bytes_copied, optlen))
6013 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6015 * Requests that the local SCTP stack use the enclosed peer address as
6016 * the association primary. The enclosed address must be one of the
6017 * association peer's addresses.
6019 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6020 char __user *optval, int __user *optlen)
6022 struct sctp_prim prim;
6023 struct sctp_association *asoc;
6024 struct sctp_sock *sp = sctp_sk(sk);
6026 if (len < sizeof(struct sctp_prim))
6029 len = sizeof(struct sctp_prim);
6031 if (copy_from_user(&prim, optval, len))
6034 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6038 if (!asoc->peer.primary_path)
6041 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6042 asoc->peer.primary_path->af_specific->sockaddr_len);
6044 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6045 (union sctp_addr *)&prim.ssp_addr);
6047 if (put_user(len, optlen))
6049 if (copy_to_user(optval, &prim, len))
6056 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6058 * Requests that the local endpoint set the specified Adaptation Layer
6059 * Indication parameter for all future INIT and INIT-ACK exchanges.
6061 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6062 char __user *optval, int __user *optlen)
6064 struct sctp_setadaptation adaptation;
6066 if (len < sizeof(struct sctp_setadaptation))
6069 len = sizeof(struct sctp_setadaptation);
6071 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6073 if (put_user(len, optlen))
6075 if (copy_to_user(optval, &adaptation, len))
6083 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6085 * Applications that wish to use the sendto() system call may wish to
6086 * specify a default set of parameters that would normally be supplied
6087 * through the inclusion of ancillary data. This socket option allows
6088 * such an application to set the default sctp_sndrcvinfo structure.
6091 * The application that wishes to use this socket option simply passes
6092 * in to this call the sctp_sndrcvinfo structure defined in Section
6093 * 5.2.2) The input parameters accepted by this call include
6094 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6095 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6096 * to this call if the caller is using the UDP model.
6098 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6100 static int sctp_getsockopt_default_send_param(struct sock *sk,
6101 int len, char __user *optval,
6104 struct sctp_sock *sp = sctp_sk(sk);
6105 struct sctp_association *asoc;
6106 struct sctp_sndrcvinfo info;
6108 if (len < sizeof(info))
6113 if (copy_from_user(&info, optval, len))
6116 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6117 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6120 info.sinfo_stream = asoc->default_stream;
6121 info.sinfo_flags = asoc->default_flags;
6122 info.sinfo_ppid = asoc->default_ppid;
6123 info.sinfo_context = asoc->default_context;
6124 info.sinfo_timetolive = asoc->default_timetolive;
6126 info.sinfo_stream = sp->default_stream;
6127 info.sinfo_flags = sp->default_flags;
6128 info.sinfo_ppid = sp->default_ppid;
6129 info.sinfo_context = sp->default_context;
6130 info.sinfo_timetolive = sp->default_timetolive;
6133 if (put_user(len, optlen))
6135 if (copy_to_user(optval, &info, len))
6141 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6142 * (SCTP_DEFAULT_SNDINFO)
6144 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6145 char __user *optval,
6148 struct sctp_sock *sp = sctp_sk(sk);
6149 struct sctp_association *asoc;
6150 struct sctp_sndinfo info;
6152 if (len < sizeof(info))
6157 if (copy_from_user(&info, optval, len))
6160 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6161 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6164 info.snd_sid = asoc->default_stream;
6165 info.snd_flags = asoc->default_flags;
6166 info.snd_ppid = asoc->default_ppid;
6167 info.snd_context = asoc->default_context;
6169 info.snd_sid = sp->default_stream;
6170 info.snd_flags = sp->default_flags;
6171 info.snd_ppid = sp->default_ppid;
6172 info.snd_context = sp->default_context;
6175 if (put_user(len, optlen))
6177 if (copy_to_user(optval, &info, len))
6185 * 7.1.5 SCTP_NODELAY
6187 * Turn on/off any Nagle-like algorithm. This means that packets are
6188 * generally sent as soon as possible and no unnecessary delays are
6189 * introduced, at the cost of more packets in the network. Expects an
6190 * integer boolean flag.
6193 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6194 char __user *optval, int __user *optlen)
6198 if (len < sizeof(int))
6202 val = (sctp_sk(sk)->nodelay == 1);
6203 if (put_user(len, optlen))
6205 if (copy_to_user(optval, &val, len))
6212 * 7.1.1 SCTP_RTOINFO
6214 * The protocol parameters used to initialize and bound retransmission
6215 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6216 * and modify these parameters.
6217 * All parameters are time values, in milliseconds. A value of 0, when
6218 * modifying the parameters, indicates that the current value should not
6222 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6223 char __user *optval,
6224 int __user *optlen) {
6225 struct sctp_rtoinfo rtoinfo;
6226 struct sctp_association *asoc;
6228 if (len < sizeof (struct sctp_rtoinfo))
6231 len = sizeof(struct sctp_rtoinfo);
6233 if (copy_from_user(&rtoinfo, optval, len))
6236 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6238 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6241 /* Values corresponding to the specific association. */
6243 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6244 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6245 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6247 /* Values corresponding to the endpoint. */
6248 struct sctp_sock *sp = sctp_sk(sk);
6250 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6251 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6252 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6255 if (put_user(len, optlen))
6258 if (copy_to_user(optval, &rtoinfo, len))
6266 * 7.1.2 SCTP_ASSOCINFO
6268 * This option is used to tune the maximum retransmission attempts
6269 * of the association.
6270 * Returns an error if the new association retransmission value is
6271 * greater than the sum of the retransmission value of the peer.
6272 * See [SCTP] for more information.
6275 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6276 char __user *optval,
6280 struct sctp_assocparams assocparams;
6281 struct sctp_association *asoc;
6282 struct list_head *pos;
6285 if (len < sizeof (struct sctp_assocparams))
6288 len = sizeof(struct sctp_assocparams);
6290 if (copy_from_user(&assocparams, optval, len))
6293 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6295 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6298 /* Values correspoinding to the specific association */
6300 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6301 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6302 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6303 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6305 list_for_each(pos, &asoc->peer.transport_addr_list) {
6309 assocparams.sasoc_number_peer_destinations = cnt;
6311 /* Values corresponding to the endpoint */
6312 struct sctp_sock *sp = sctp_sk(sk);
6314 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6315 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6316 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6317 assocparams.sasoc_cookie_life =
6318 sp->assocparams.sasoc_cookie_life;
6319 assocparams.sasoc_number_peer_destinations =
6321 sasoc_number_peer_destinations;
6324 if (put_user(len, optlen))
6327 if (copy_to_user(optval, &assocparams, len))
6334 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6336 * This socket option is a boolean flag which turns on or off mapped V4
6337 * addresses. If this option is turned on and the socket is type
6338 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6339 * If this option is turned off, then no mapping will be done of V4
6340 * addresses and a user will receive both PF_INET6 and PF_INET type
6341 * addresses on the socket.
6343 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6344 char __user *optval, int __user *optlen)
6347 struct sctp_sock *sp = sctp_sk(sk);
6349 if (len < sizeof(int))
6354 if (put_user(len, optlen))
6356 if (copy_to_user(optval, &val, len))
6363 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6364 * (chapter and verse is quoted at sctp_setsockopt_context())
6366 static int sctp_getsockopt_context(struct sock *sk, int len,
6367 char __user *optval, int __user *optlen)
6369 struct sctp_assoc_value params;
6370 struct sctp_sock *sp;
6371 struct sctp_association *asoc;
6373 if (len < sizeof(struct sctp_assoc_value))
6376 len = sizeof(struct sctp_assoc_value);
6378 if (copy_from_user(¶ms, optval, len))
6383 if (params.assoc_id != 0) {
6384 asoc = sctp_id2assoc(sk, params.assoc_id);
6387 params.assoc_value = asoc->default_rcv_context;
6389 params.assoc_value = sp->default_rcv_context;
6392 if (put_user(len, optlen))
6394 if (copy_to_user(optval, ¶ms, len))
6401 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6402 * This option will get or set the maximum size to put in any outgoing
6403 * SCTP DATA chunk. If a message is larger than this size it will be
6404 * fragmented by SCTP into the specified size. Note that the underlying
6405 * SCTP implementation may fragment into smaller sized chunks when the
6406 * PMTU of the underlying association is smaller than the value set by
6407 * the user. The default value for this option is '0' which indicates
6408 * the user is NOT limiting fragmentation and only the PMTU will effect
6409 * SCTP's choice of DATA chunk size. Note also that values set larger
6410 * than the maximum size of an IP datagram will effectively let SCTP
6411 * control fragmentation (i.e. the same as setting this option to 0).
6413 * The following structure is used to access and modify this parameter:
6415 * struct sctp_assoc_value {
6416 * sctp_assoc_t assoc_id;
6417 * uint32_t assoc_value;
6420 * assoc_id: This parameter is ignored for one-to-one style sockets.
6421 * For one-to-many style sockets this parameter indicates which
6422 * association the user is performing an action upon. Note that if
6423 * this field's value is zero then the endpoints default value is
6424 * changed (effecting future associations only).
6425 * assoc_value: This parameter specifies the maximum size in bytes.
6427 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6428 char __user *optval, int __user *optlen)
6430 struct sctp_assoc_value params;
6431 struct sctp_association *asoc;
6433 if (len == sizeof(int)) {
6434 pr_warn_ratelimited(DEPRECATED
6436 "Use of int in maxseg socket option.\n"
6437 "Use struct sctp_assoc_value instead\n",
6438 current->comm, task_pid_nr(current));
6439 params.assoc_id = 0;
6440 } else if (len >= sizeof(struct sctp_assoc_value)) {
6441 len = sizeof(struct sctp_assoc_value);
6442 if (copy_from_user(¶ms, optval, len))
6447 asoc = sctp_id2assoc(sk, params.assoc_id);
6448 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6452 params.assoc_value = asoc->frag_point;
6454 params.assoc_value = sctp_sk(sk)->user_frag;
6456 if (put_user(len, optlen))
6458 if (len == sizeof(int)) {
6459 if (copy_to_user(optval, ¶ms.assoc_value, len))
6462 if (copy_to_user(optval, ¶ms, len))
6470 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6471 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6473 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6474 char __user *optval, int __user *optlen)
6478 if (len < sizeof(int))
6483 val = sctp_sk(sk)->frag_interleave;
6484 if (put_user(len, optlen))
6486 if (copy_to_user(optval, &val, len))
6493 * 7.1.25. Set or Get the sctp partial delivery point
6494 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6496 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6497 char __user *optval,
6502 if (len < sizeof(u32))
6507 val = sctp_sk(sk)->pd_point;
6508 if (put_user(len, optlen))
6510 if (copy_to_user(optval, &val, len))
6517 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6518 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6520 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6521 char __user *optval,
6524 struct sctp_assoc_value params;
6525 struct sctp_sock *sp;
6526 struct sctp_association *asoc;
6528 if (len == sizeof(int)) {
6529 pr_warn_ratelimited(DEPRECATED
6531 "Use of int in max_burst socket option.\n"
6532 "Use struct sctp_assoc_value instead\n",
6533 current->comm, task_pid_nr(current));
6534 params.assoc_id = 0;
6535 } else if (len >= sizeof(struct sctp_assoc_value)) {
6536 len = sizeof(struct sctp_assoc_value);
6537 if (copy_from_user(¶ms, optval, len))
6544 if (params.assoc_id != 0) {
6545 asoc = sctp_id2assoc(sk, params.assoc_id);
6548 params.assoc_value = asoc->max_burst;
6550 params.assoc_value = sp->max_burst;
6552 if (len == sizeof(int)) {
6553 if (copy_to_user(optval, ¶ms.assoc_value, len))
6556 if (copy_to_user(optval, ¶ms, len))
6564 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6565 char __user *optval, int __user *optlen)
6567 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6568 struct sctp_hmacalgo __user *p = (void __user *)optval;
6569 struct sctp_hmac_algo_param *hmacs;
6574 if (!ep->auth_enable)
6577 hmacs = ep->auth_hmacs_list;
6578 data_len = ntohs(hmacs->param_hdr.length) -
6579 sizeof(struct sctp_paramhdr);
6581 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6584 len = sizeof(struct sctp_hmacalgo) + data_len;
6585 num_idents = data_len / sizeof(u16);
6587 if (put_user(len, optlen))
6589 if (put_user(num_idents, &p->shmac_num_idents))
6591 for (i = 0; i < num_idents; i++) {
6592 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6594 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6600 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6601 char __user *optval, int __user *optlen)
6603 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6604 struct sctp_authkeyid val;
6605 struct sctp_association *asoc;
6607 if (!ep->auth_enable)
6610 if (len < sizeof(struct sctp_authkeyid))
6613 len = sizeof(struct sctp_authkeyid);
6614 if (copy_from_user(&val, optval, len))
6617 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6618 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6622 val.scact_keynumber = asoc->active_key_id;
6624 val.scact_keynumber = ep->active_key_id;
6626 if (put_user(len, optlen))
6628 if (copy_to_user(optval, &val, len))
6634 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6635 char __user *optval, int __user *optlen)
6637 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6638 struct sctp_authchunks __user *p = (void __user *)optval;
6639 struct sctp_authchunks val;
6640 struct sctp_association *asoc;
6641 struct sctp_chunks_param *ch;
6645 if (!ep->auth_enable)
6648 if (len < sizeof(struct sctp_authchunks))
6651 if (copy_from_user(&val, optval, sizeof(val)))
6654 to = p->gauth_chunks;
6655 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6659 ch = asoc->peer.peer_chunks;
6663 /* See if the user provided enough room for all the data */
6664 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6665 if (len < num_chunks)
6668 if (copy_to_user(to, ch->chunks, num_chunks))
6671 len = sizeof(struct sctp_authchunks) + num_chunks;
6672 if (put_user(len, optlen))
6674 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6679 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6680 char __user *optval, int __user *optlen)
6682 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6683 struct sctp_authchunks __user *p = (void __user *)optval;
6684 struct sctp_authchunks val;
6685 struct sctp_association *asoc;
6686 struct sctp_chunks_param *ch;
6690 if (!ep->auth_enable)
6693 if (len < sizeof(struct sctp_authchunks))
6696 if (copy_from_user(&val, optval, sizeof(val)))
6699 to = p->gauth_chunks;
6700 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6701 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6705 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6707 ch = ep->auth_chunk_list;
6712 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6713 if (len < sizeof(struct sctp_authchunks) + num_chunks)
6716 if (copy_to_user(to, ch->chunks, num_chunks))
6719 len = sizeof(struct sctp_authchunks) + num_chunks;
6720 if (put_user(len, optlen))
6722 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6729 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6730 * This option gets the current number of associations that are attached
6731 * to a one-to-many style socket. The option value is an uint32_t.
6733 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6734 char __user *optval, int __user *optlen)
6736 struct sctp_sock *sp = sctp_sk(sk);
6737 struct sctp_association *asoc;
6740 if (sctp_style(sk, TCP))
6743 if (len < sizeof(u32))
6748 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6752 if (put_user(len, optlen))
6754 if (copy_to_user(optval, &val, len))
6761 * 8.1.23 SCTP_AUTO_ASCONF
6762 * See the corresponding setsockopt entry as description
6764 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6765 char __user *optval, int __user *optlen)
6769 if (len < sizeof(int))
6773 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6775 if (put_user(len, optlen))
6777 if (copy_to_user(optval, &val, len))
6783 * 8.2.6. Get the Current Identifiers of Associations
6784 * (SCTP_GET_ASSOC_ID_LIST)
6786 * This option gets the current list of SCTP association identifiers of
6787 * the SCTP associations handled by a one-to-many style socket.
6789 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6790 char __user *optval, int __user *optlen)
6792 struct sctp_sock *sp = sctp_sk(sk);
6793 struct sctp_association *asoc;
6794 struct sctp_assoc_ids *ids;
6797 if (sctp_style(sk, TCP))
6800 if (len < sizeof(struct sctp_assoc_ids))
6803 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6807 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6810 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6812 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
6816 ids->gaids_number_of_ids = num;
6818 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6819 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6822 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6832 * SCTP_PEER_ADDR_THLDS
6834 * This option allows us to fetch the partially failed threshold for one or all
6835 * transports in an association. See Section 6.1 of:
6836 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6838 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6839 char __user *optval,
6843 struct sctp_paddrthlds val;
6844 struct sctp_transport *trans;
6845 struct sctp_association *asoc;
6847 if (len < sizeof(struct sctp_paddrthlds))
6849 len = sizeof(struct sctp_paddrthlds);
6850 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6853 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6854 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6858 val.spt_pathpfthld = asoc->pf_retrans;
6859 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6861 trans = sctp_addr_id2transport(sk, &val.spt_address,
6866 val.spt_pathmaxrxt = trans->pathmaxrxt;
6867 val.spt_pathpfthld = trans->pf_retrans;
6870 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6877 * SCTP_GET_ASSOC_STATS
6879 * This option retrieves local per endpoint statistics. It is modeled
6880 * after OpenSolaris' implementation
6882 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6883 char __user *optval,
6886 struct sctp_assoc_stats sas;
6887 struct sctp_association *asoc = NULL;
6889 /* User must provide at least the assoc id */
6890 if (len < sizeof(sctp_assoc_t))
6893 /* Allow the struct to grow and fill in as much as possible */
6894 len = min_t(size_t, len, sizeof(sas));
6896 if (copy_from_user(&sas, optval, len))
6899 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6903 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6904 sas.sas_gapcnt = asoc->stats.gapcnt;
6905 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6906 sas.sas_osacks = asoc->stats.osacks;
6907 sas.sas_isacks = asoc->stats.isacks;
6908 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6909 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6910 sas.sas_oodchunks = asoc->stats.oodchunks;
6911 sas.sas_iodchunks = asoc->stats.iodchunks;
6912 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6913 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6914 sas.sas_idupchunks = asoc->stats.idupchunks;
6915 sas.sas_opackets = asoc->stats.opackets;
6916 sas.sas_ipackets = asoc->stats.ipackets;
6918 /* New high max rto observed, will return 0 if not a single
6919 * RTO update took place. obs_rto_ipaddr will be bogus
6922 sas.sas_maxrto = asoc->stats.max_obs_rto;
6923 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6924 sizeof(struct sockaddr_storage));
6926 /* Mark beginning of a new observation period */
6927 asoc->stats.max_obs_rto = asoc->rto_min;
6929 if (put_user(len, optlen))
6932 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
6934 if (copy_to_user(optval, &sas, len))
6940 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6941 char __user *optval,
6946 if (len < sizeof(int))
6950 if (sctp_sk(sk)->recvrcvinfo)
6952 if (put_user(len, optlen))
6954 if (copy_to_user(optval, &val, len))
6960 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6961 char __user *optval,
6966 if (len < sizeof(int))
6970 if (sctp_sk(sk)->recvnxtinfo)
6972 if (put_user(len, optlen))
6974 if (copy_to_user(optval, &val, len))
6980 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6981 char __user *optval,
6984 struct sctp_assoc_value params;
6985 struct sctp_association *asoc;
6986 int retval = -EFAULT;
6988 if (len < sizeof(params)) {
6993 len = sizeof(params);
6994 if (copy_from_user(¶ms, optval, len))
6997 asoc = sctp_id2assoc(sk, params.assoc_id);
6999 params.assoc_value = asoc->prsctp_enable;
7000 } else if (!params.assoc_id) {
7001 struct sctp_sock *sp = sctp_sk(sk);
7003 params.assoc_value = sp->ep->prsctp_enable;
7009 if (put_user(len, optlen))
7012 if (copy_to_user(optval, ¶ms, len))
7021 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7022 char __user *optval,
7025 struct sctp_default_prinfo info;
7026 struct sctp_association *asoc;
7027 int retval = -EFAULT;
7029 if (len < sizeof(info)) {
7035 if (copy_from_user(&info, optval, len))
7038 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7040 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7041 info.pr_value = asoc->default_timetolive;
7042 } else if (!info.pr_assoc_id) {
7043 struct sctp_sock *sp = sctp_sk(sk);
7045 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7046 info.pr_value = sp->default_timetolive;
7052 if (put_user(len, optlen))
7055 if (copy_to_user(optval, &info, len))
7064 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7065 char __user *optval,
7068 struct sctp_prstatus params;
7069 struct sctp_association *asoc;
7071 int retval = -EINVAL;
7073 if (len < sizeof(params))
7076 len = sizeof(params);
7077 if (copy_from_user(¶ms, optval, len)) {
7082 policy = params.sprstat_policy;
7083 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7084 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7087 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7091 if (policy == SCTP_PR_SCTP_ALL) {
7092 params.sprstat_abandoned_unsent = 0;
7093 params.sprstat_abandoned_sent = 0;
7094 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7095 params.sprstat_abandoned_unsent +=
7096 asoc->abandoned_unsent[policy];
7097 params.sprstat_abandoned_sent +=
7098 asoc->abandoned_sent[policy];
7101 params.sprstat_abandoned_unsent =
7102 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7103 params.sprstat_abandoned_sent =
7104 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7107 if (put_user(len, optlen)) {
7112 if (copy_to_user(optval, ¶ms, len)) {
7123 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7124 char __user *optval,
7127 struct sctp_stream_out_ext *streamoute;
7128 struct sctp_association *asoc;
7129 struct sctp_prstatus params;
7130 int retval = -EINVAL;
7133 if (len < sizeof(params))
7136 len = sizeof(params);
7137 if (copy_from_user(¶ms, optval, len)) {
7142 policy = params.sprstat_policy;
7143 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7144 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7147 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7148 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7151 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7153 /* Not allocated yet, means all stats are 0 */
7154 params.sprstat_abandoned_unsent = 0;
7155 params.sprstat_abandoned_sent = 0;
7160 if (policy == SCTP_PR_SCTP_ALL) {
7161 params.sprstat_abandoned_unsent = 0;
7162 params.sprstat_abandoned_sent = 0;
7163 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7164 params.sprstat_abandoned_unsent +=
7165 streamoute->abandoned_unsent[policy];
7166 params.sprstat_abandoned_sent +=
7167 streamoute->abandoned_sent[policy];
7170 params.sprstat_abandoned_unsent =
7171 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7172 params.sprstat_abandoned_sent =
7173 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7176 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7187 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7188 char __user *optval,
7191 struct sctp_assoc_value params;
7192 struct sctp_association *asoc;
7193 int retval = -EFAULT;
7195 if (len < sizeof(params)) {
7200 len = sizeof(params);
7201 if (copy_from_user(¶ms, optval, len))
7204 asoc = sctp_id2assoc(sk, params.assoc_id);
7206 params.assoc_value = asoc->reconf_enable;
7207 } else if (!params.assoc_id) {
7208 struct sctp_sock *sp = sctp_sk(sk);
7210 params.assoc_value = sp->ep->reconf_enable;
7216 if (put_user(len, optlen))
7219 if (copy_to_user(optval, ¶ms, len))
7228 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7229 char __user *optval,
7232 struct sctp_assoc_value params;
7233 struct sctp_association *asoc;
7234 int retval = -EFAULT;
7236 if (len < sizeof(params)) {
7241 len = sizeof(params);
7242 if (copy_from_user(¶ms, optval, len))
7245 asoc = sctp_id2assoc(sk, params.assoc_id);
7247 params.assoc_value = asoc->strreset_enable;
7248 } else if (!params.assoc_id) {
7249 struct sctp_sock *sp = sctp_sk(sk);
7251 params.assoc_value = sp->ep->strreset_enable;
7257 if (put_user(len, optlen))
7260 if (copy_to_user(optval, ¶ms, len))
7269 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7270 char __user *optval,
7273 struct sctp_assoc_value params;
7274 struct sctp_association *asoc;
7275 int retval = -EFAULT;
7277 if (len < sizeof(params)) {
7282 len = sizeof(params);
7283 if (copy_from_user(¶ms, optval, len))
7286 asoc = sctp_id2assoc(sk, params.assoc_id);
7292 params.assoc_value = sctp_sched_get_sched(asoc);
7294 if (put_user(len, optlen))
7297 if (copy_to_user(optval, ¶ms, len))
7306 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7307 char __user *optval,
7310 struct sctp_stream_value params;
7311 struct sctp_association *asoc;
7312 int retval = -EFAULT;
7314 if (len < sizeof(params)) {
7319 len = sizeof(params);
7320 if (copy_from_user(¶ms, optval, len))
7323 asoc = sctp_id2assoc(sk, params.assoc_id);
7329 retval = sctp_sched_get_value(asoc, params.stream_id,
7330 ¶ms.stream_value);
7334 if (put_user(len, optlen)) {
7339 if (copy_to_user(optval, ¶ms, len)) {
7348 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7349 char __user *optval,
7352 struct sctp_assoc_value params;
7353 struct sctp_association *asoc;
7354 int retval = -EFAULT;
7356 if (len < sizeof(params)) {
7361 len = sizeof(params);
7362 if (copy_from_user(¶ms, optval, len))
7365 asoc = sctp_id2assoc(sk, params.assoc_id);
7367 params.assoc_value = asoc->intl_enable;
7368 } else if (!params.assoc_id) {
7369 struct sctp_sock *sp = sctp_sk(sk);
7371 params.assoc_value = sp->strm_interleave;
7377 if (put_user(len, optlen))
7380 if (copy_to_user(optval, ¶ms, len))
7389 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7390 char __user *optval,
7395 if (len < sizeof(int))
7399 val = sctp_sk(sk)->reuse;
7400 if (put_user(len, optlen))
7403 if (copy_to_user(optval, &val, len))
7409 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7410 char __user *optval, int __user *optlen)
7415 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7417 /* I can hardly begin to describe how wrong this is. This is
7418 * so broken as to be worse than useless. The API draft
7419 * REALLY is NOT helpful here... I am not convinced that the
7420 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7421 * are at all well-founded.
7423 if (level != SOL_SCTP) {
7424 struct sctp_af *af = sctp_sk(sk)->pf->af;
7426 retval = af->getsockopt(sk, level, optname, optval, optlen);
7430 if (get_user(len, optlen))
7440 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7442 case SCTP_DISABLE_FRAGMENTS:
7443 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7447 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7449 case SCTP_AUTOCLOSE:
7450 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7452 case SCTP_SOCKOPT_PEELOFF:
7453 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7455 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7456 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7458 case SCTP_PEER_ADDR_PARAMS:
7459 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7462 case SCTP_DELAYED_SACK:
7463 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7467 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7469 case SCTP_GET_PEER_ADDRS:
7470 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7473 case SCTP_GET_LOCAL_ADDRS:
7474 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7477 case SCTP_SOCKOPT_CONNECTX3:
7478 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7480 case SCTP_DEFAULT_SEND_PARAM:
7481 retval = sctp_getsockopt_default_send_param(sk, len,
7484 case SCTP_DEFAULT_SNDINFO:
7485 retval = sctp_getsockopt_default_sndinfo(sk, len,
7488 case SCTP_PRIMARY_ADDR:
7489 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7492 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7495 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7497 case SCTP_ASSOCINFO:
7498 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7500 case SCTP_I_WANT_MAPPED_V4_ADDR:
7501 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7504 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7506 case SCTP_GET_PEER_ADDR_INFO:
7507 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7510 case SCTP_ADAPTATION_LAYER:
7511 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7515 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7517 case SCTP_FRAGMENT_INTERLEAVE:
7518 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7521 case SCTP_PARTIAL_DELIVERY_POINT:
7522 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7525 case SCTP_MAX_BURST:
7526 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7529 case SCTP_AUTH_CHUNK:
7530 case SCTP_AUTH_DELETE_KEY:
7531 case SCTP_AUTH_DEACTIVATE_KEY:
7532 retval = -EOPNOTSUPP;
7534 case SCTP_HMAC_IDENT:
7535 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7537 case SCTP_AUTH_ACTIVE_KEY:
7538 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7540 case SCTP_PEER_AUTH_CHUNKS:
7541 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7544 case SCTP_LOCAL_AUTH_CHUNKS:
7545 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7548 case SCTP_GET_ASSOC_NUMBER:
7549 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7551 case SCTP_GET_ASSOC_ID_LIST:
7552 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7554 case SCTP_AUTO_ASCONF:
7555 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7557 case SCTP_PEER_ADDR_THLDS:
7558 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7560 case SCTP_GET_ASSOC_STATS:
7561 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7563 case SCTP_RECVRCVINFO:
7564 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7566 case SCTP_RECVNXTINFO:
7567 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7569 case SCTP_PR_SUPPORTED:
7570 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7572 case SCTP_DEFAULT_PRINFO:
7573 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7576 case SCTP_PR_ASSOC_STATUS:
7577 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7580 case SCTP_PR_STREAM_STATUS:
7581 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7584 case SCTP_RECONFIG_SUPPORTED:
7585 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7588 case SCTP_ENABLE_STREAM_RESET:
7589 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7592 case SCTP_STREAM_SCHEDULER:
7593 retval = sctp_getsockopt_scheduler(sk, len, optval,
7596 case SCTP_STREAM_SCHEDULER_VALUE:
7597 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7600 case SCTP_INTERLEAVING_SUPPORTED:
7601 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7604 case SCTP_REUSE_PORT:
7605 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7608 retval = -ENOPROTOOPT;
7616 static int sctp_hash(struct sock *sk)
7622 static void sctp_unhash(struct sock *sk)
7627 /* Check if port is acceptable. Possibly find first available port.
7629 * The port hash table (contained in the 'global' SCTP protocol storage
7630 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7631 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7632 * list (the list number is the port number hashed out, so as you
7633 * would expect from a hash function, all the ports in a given list have
7634 * such a number that hashes out to the same list number; you were
7635 * expecting that, right?); so each list has a set of ports, with a
7636 * link to the socket (struct sock) that uses it, the port number and
7637 * a fastreuse flag (FIXME: NPI ipg).
7639 static struct sctp_bind_bucket *sctp_bucket_create(
7640 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
7642 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7644 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
7645 struct sctp_bind_hashbucket *head; /* hash list */
7646 struct sctp_bind_bucket *pp;
7647 unsigned short snum;
7650 snum = ntohs(addr->v4.sin_port);
7652 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7655 /* Search for an available port. */
7656 int low, high, remaining, index;
7658 struct net *net = sock_net(sk);
7660 inet_get_local_port_range(net, &low, &high);
7661 remaining = (high - low) + 1;
7662 rover = prandom_u32() % remaining + low;
7666 if ((rover < low) || (rover > high))
7668 if (inet_is_local_reserved_port(net, rover))
7670 index = sctp_phashfn(sock_net(sk), rover);
7671 head = &sctp_port_hashtable[index];
7672 spin_lock_bh(&head->lock);
7673 sctp_for_each_hentry(pp, &head->chain)
7674 if ((pp->port == rover) &&
7675 net_eq(sock_net(sk), pp->net))
7679 spin_unlock_bh(&head->lock);
7681 } while (--remaining > 0);
7683 /* Exhausted local port range during search? */
7688 /* OK, here is the one we will use. HEAD (the port
7689 * hash table list entry) is non-NULL and we hold it's
7694 /* We are given an specific port number; we verify
7695 * that it is not being used. If it is used, we will
7696 * exahust the search in the hash list corresponding
7697 * to the port number (snum) - we detect that with the
7698 * port iterator, pp being NULL.
7700 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
7701 spin_lock_bh(&head->lock);
7702 sctp_for_each_hentry(pp, &head->chain) {
7703 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
7710 if (!hlist_empty(&pp->owner)) {
7711 /* We had a port hash table hit - there is an
7712 * available port (pp != NULL) and it is being
7713 * used by other socket (pp->owner not empty); that other
7714 * socket is going to be sk2.
7718 pr_debug("%s: found a possible match\n", __func__);
7720 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
7723 /* Run through the list of sockets bound to the port
7724 * (pp->port) [via the pointers bind_next and
7725 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7726 * we get the endpoint they describe and run through
7727 * the endpoint's list of IP (v4 or v6) addresses,
7728 * comparing each of the addresses with the address of
7729 * the socket sk. If we find a match, then that means
7730 * that this port/socket (sk) combination are already
7733 sk_for_each_bound(sk2, &pp->owner) {
7734 struct sctp_endpoint *ep2;
7735 ep2 = sctp_sk(sk2)->ep;
7738 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
7739 sk2->sk_state != SCTP_SS_LISTENING))
7742 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7743 sctp_sk(sk2), sctp_sk(sk))) {
7749 pr_debug("%s: found a match\n", __func__);
7752 /* If there was a hash table miss, create a new port. */
7754 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
7757 /* In either case (hit or miss), make sure fastreuse is 1 only
7758 * if sk->sk_reuse is too (that is, if the caller requested
7759 * SO_REUSEADDR on this socket -sk-).
7761 if (hlist_empty(&pp->owner)) {
7762 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
7766 } else if (pp->fastreuse &&
7767 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
7770 /* We are set, so fill up all the data in the hash table
7771 * entry, tie the socket list information with the rest of the
7772 * sockets FIXME: Blurry, NPI (ipg).
7775 if (!sctp_sk(sk)->bind_hash) {
7776 inet_sk(sk)->inet_num = snum;
7777 sk_add_bind_node(sk, &pp->owner);
7778 sctp_sk(sk)->bind_hash = pp;
7783 spin_unlock_bh(&head->lock);
7787 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7788 * port is requested.
7790 static int sctp_get_port(struct sock *sk, unsigned short snum)
7792 union sctp_addr addr;
7793 struct sctp_af *af = sctp_sk(sk)->pf->af;
7795 /* Set up a dummy address struct from the sk. */
7796 af->from_sk(&addr, sk);
7797 addr.v4.sin_port = htons(snum);
7799 /* Note: sk->sk_num gets filled in if ephemeral port request. */
7800 return !!sctp_get_port_local(sk, &addr);
7804 * Move a socket to LISTENING state.
7806 static int sctp_listen_start(struct sock *sk, int backlog)
7808 struct sctp_sock *sp = sctp_sk(sk);
7809 struct sctp_endpoint *ep = sp->ep;
7810 struct crypto_shash *tfm = NULL;
7813 /* Allocate HMAC for generating cookie. */
7814 if (!sp->hmac && sp->sctp_hmac_alg) {
7815 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
7816 tfm = crypto_alloc_shash(alg, 0, 0);
7818 net_info_ratelimited("failed to load transform for %s: %ld\n",
7819 sp->sctp_hmac_alg, PTR_ERR(tfm));
7822 sctp_sk(sk)->hmac = tfm;
7826 * If a bind() or sctp_bindx() is not called prior to a listen()
7827 * call that allows new associations to be accepted, the system
7828 * picks an ephemeral port and will choose an address set equivalent
7829 * to binding with a wildcard address.
7831 * This is not currently spelled out in the SCTP sockets
7832 * extensions draft, but follows the practice as seen in TCP
7836 inet_sk_set_state(sk, SCTP_SS_LISTENING);
7837 if (!ep->base.bind_addr.port) {
7838 if (sctp_autobind(sk))
7841 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
7842 inet_sk_set_state(sk, SCTP_SS_CLOSED);
7847 sk->sk_max_ack_backlog = backlog;
7848 sctp_hash_endpoint(ep);
7853 * 4.1.3 / 5.1.3 listen()
7855 * By default, new associations are not accepted for UDP style sockets.
7856 * An application uses listen() to mark a socket as being able to
7857 * accept new associations.
7859 * On TCP style sockets, applications use listen() to ready the SCTP
7860 * endpoint for accepting inbound associations.
7862 * On both types of endpoints a backlog of '0' disables listening.
7864 * Move a socket to LISTENING state.
7866 int sctp_inet_listen(struct socket *sock, int backlog)
7868 struct sock *sk = sock->sk;
7869 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7872 if (unlikely(backlog < 0))
7877 /* Peeled-off sockets are not allowed to listen(). */
7878 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7881 if (sock->state != SS_UNCONNECTED)
7884 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7887 /* If backlog is zero, disable listening. */
7889 if (sctp_sstate(sk, CLOSED))
7893 sctp_unhash_endpoint(ep);
7894 sk->sk_state = SCTP_SS_CLOSED;
7895 if (sk->sk_reuse || sctp_sk(sk)->reuse)
7896 sctp_sk(sk)->bind_hash->fastreuse = 1;
7900 /* If we are already listening, just update the backlog */
7901 if (sctp_sstate(sk, LISTENING))
7902 sk->sk_max_ack_backlog = backlog;
7904 err = sctp_listen_start(sk, backlog);
7916 * This function is done by modeling the current datagram_poll() and the
7917 * tcp_poll(). Note that, based on these implementations, we don't
7918 * lock the socket in this function, even though it seems that,
7919 * ideally, locking or some other mechanisms can be used to ensure
7920 * the integrity of the counters (sndbuf and wmem_alloc) used
7921 * in this place. We assume that we don't need locks either until proven
7924 * Another thing to note is that we include the Async I/O support
7925 * here, again, by modeling the current TCP/UDP code. We don't have
7926 * a good way to test with it yet.
7928 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7930 struct sock *sk = sock->sk;
7931 struct sctp_sock *sp = sctp_sk(sk);
7934 poll_wait(file, sk_sleep(sk), wait);
7936 sock_rps_record_flow(sk);
7938 /* A TCP-style listening socket becomes readable when the accept queue
7941 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7942 return (!list_empty(&sp->ep->asocs)) ?
7943 (EPOLLIN | EPOLLRDNORM) : 0;
7947 /* Is there any exceptional events? */
7948 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
7950 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
7951 if (sk->sk_shutdown & RCV_SHUTDOWN)
7952 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
7953 if (sk->sk_shutdown == SHUTDOWN_MASK)
7956 /* Is it readable? Reconsider this code with TCP-style support. */
7957 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
7958 mask |= EPOLLIN | EPOLLRDNORM;
7960 /* The association is either gone or not ready. */
7961 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7964 /* Is it writable? */
7965 if (sctp_writeable(sk)) {
7966 mask |= EPOLLOUT | EPOLLWRNORM;
7968 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
7970 * Since the socket is not locked, the buffer
7971 * might be made available after the writeable check and
7972 * before the bit is set. This could cause a lost I/O
7973 * signal. tcp_poll() has a race breaker for this race
7974 * condition. Based on their implementation, we put
7975 * in the following code to cover it as well.
7977 if (sctp_writeable(sk))
7978 mask |= EPOLLOUT | EPOLLWRNORM;
7983 /********************************************************************
7984 * 2nd Level Abstractions
7985 ********************************************************************/
7987 static struct sctp_bind_bucket *sctp_bucket_create(
7988 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
7990 struct sctp_bind_bucket *pp;
7992 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
7994 SCTP_DBG_OBJCNT_INC(bind_bucket);
7997 INIT_HLIST_HEAD(&pp->owner);
7999 hlist_add_head(&pp->node, &head->chain);
8004 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8005 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8007 if (pp && hlist_empty(&pp->owner)) {
8008 __hlist_del(&pp->node);
8009 kmem_cache_free(sctp_bucket_cachep, pp);
8010 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8014 /* Release this socket's reference to a local port. */
8015 static inline void __sctp_put_port(struct sock *sk)
8017 struct sctp_bind_hashbucket *head =
8018 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8019 inet_sk(sk)->inet_num)];
8020 struct sctp_bind_bucket *pp;
8022 spin_lock(&head->lock);
8023 pp = sctp_sk(sk)->bind_hash;
8024 __sk_del_bind_node(sk);
8025 sctp_sk(sk)->bind_hash = NULL;
8026 inet_sk(sk)->inet_num = 0;
8027 sctp_bucket_destroy(pp);
8028 spin_unlock(&head->lock);
8031 void sctp_put_port(struct sock *sk)
8034 __sctp_put_port(sk);
8039 * The system picks an ephemeral port and choose an address set equivalent
8040 * to binding with a wildcard address.
8041 * One of those addresses will be the primary address for the association.
8042 * This automatically enables the multihoming capability of SCTP.
8044 static int sctp_autobind(struct sock *sk)
8046 union sctp_addr autoaddr;
8050 /* Initialize a local sockaddr structure to INADDR_ANY. */
8051 af = sctp_sk(sk)->pf->af;
8053 port = htons(inet_sk(sk)->inet_num);
8054 af->inaddr_any(&autoaddr, port);
8056 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8059 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8062 * 4.2 The cmsghdr Structure *
8064 * When ancillary data is sent or received, any number of ancillary data
8065 * objects can be specified by the msg_control and msg_controllen members of
8066 * the msghdr structure, because each object is preceded by
8067 * a cmsghdr structure defining the object's length (the cmsg_len member).
8068 * Historically Berkeley-derived implementations have passed only one object
8069 * at a time, but this API allows multiple objects to be
8070 * passed in a single call to sendmsg() or recvmsg(). The following example
8071 * shows two ancillary data objects in a control buffer.
8073 * |<--------------------------- msg_controllen -------------------------->|
8076 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8078 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8081 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8083 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8086 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8087 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8089 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8091 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8098 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8100 struct msghdr *my_msg = (struct msghdr *)msg;
8101 struct cmsghdr *cmsg;
8103 for_each_cmsghdr(cmsg, my_msg) {
8104 if (!CMSG_OK(my_msg, cmsg))
8107 /* Should we parse this header or ignore? */
8108 if (cmsg->cmsg_level != IPPROTO_SCTP)
8111 /* Strictly check lengths following example in SCM code. */
8112 switch (cmsg->cmsg_type) {
8114 /* SCTP Socket API Extension
8115 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8117 * This cmsghdr structure provides information for
8118 * initializing new SCTP associations with sendmsg().
8119 * The SCTP_INITMSG socket option uses this same data
8120 * structure. This structure is not used for
8123 * cmsg_level cmsg_type cmsg_data[]
8124 * ------------ ------------ ----------------------
8125 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8127 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8130 cmsgs->init = CMSG_DATA(cmsg);
8134 /* SCTP Socket API Extension
8135 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8137 * This cmsghdr structure specifies SCTP options for
8138 * sendmsg() and describes SCTP header information
8139 * about a received message through recvmsg().
8141 * cmsg_level cmsg_type cmsg_data[]
8142 * ------------ ------------ ----------------------
8143 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8145 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8148 cmsgs->srinfo = CMSG_DATA(cmsg);
8150 if (cmsgs->srinfo->sinfo_flags &
8151 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8152 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8153 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8158 /* SCTP Socket API Extension
8159 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8161 * This cmsghdr structure specifies SCTP options for
8162 * sendmsg(). This structure and SCTP_RCVINFO replaces
8163 * SCTP_SNDRCV which has been deprecated.
8165 * cmsg_level cmsg_type cmsg_data[]
8166 * ------------ ------------ ---------------------
8167 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8169 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8172 cmsgs->sinfo = CMSG_DATA(cmsg);
8174 if (cmsgs->sinfo->snd_flags &
8175 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8176 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8177 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8181 /* SCTP Socket API Extension
8182 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8184 * This cmsghdr structure specifies SCTP options for sendmsg().
8186 * cmsg_level cmsg_type cmsg_data[]
8187 * ------------ ------------ ---------------------
8188 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8190 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8193 cmsgs->prinfo = CMSG_DATA(cmsg);
8194 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8197 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8198 cmsgs->prinfo->pr_value = 0;
8201 /* SCTP Socket API Extension
8202 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8204 * This cmsghdr structure specifies SCTP options for sendmsg().
8206 * cmsg_level cmsg_type cmsg_data[]
8207 * ------------ ------------ ---------------------
8208 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8210 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8213 cmsgs->authinfo = CMSG_DATA(cmsg);
8215 case SCTP_DSTADDRV4:
8216 case SCTP_DSTADDRV6:
8217 /* SCTP Socket API Extension
8218 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8220 * This cmsghdr structure specifies SCTP options for sendmsg().
8222 * cmsg_level cmsg_type cmsg_data[]
8223 * ------------ ------------ ---------------------
8224 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8225 * ------------ ------------ ---------------------
8226 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8228 cmsgs->addrs_msg = my_msg;
8239 * Wait for a packet..
8240 * Note: This function is the same function as in core/datagram.c
8241 * with a few modifications to make lksctp work.
8243 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8248 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8250 /* Socket errors? */
8251 error = sock_error(sk);
8255 if (!skb_queue_empty(&sk->sk_receive_queue))
8258 /* Socket shut down? */
8259 if (sk->sk_shutdown & RCV_SHUTDOWN)
8262 /* Sequenced packets can come disconnected. If so we report the
8267 /* Is there a good reason to think that we may receive some data? */
8268 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8271 /* Handle signals. */
8272 if (signal_pending(current))
8275 /* Let another process have a go. Since we are going to sleep
8276 * anyway. Note: This may cause odd behaviors if the message
8277 * does not fit in the user's buffer, but this seems to be the
8278 * only way to honor MSG_DONTWAIT realistically.
8281 *timeo_p = schedule_timeout(*timeo_p);
8285 finish_wait(sk_sleep(sk), &wait);
8289 error = sock_intr_errno(*timeo_p);
8292 finish_wait(sk_sleep(sk), &wait);
8297 /* Receive a datagram.
8298 * Note: This is pretty much the same routine as in core/datagram.c
8299 * with a few changes to make lksctp work.
8301 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8302 int noblock, int *err)
8305 struct sk_buff *skb;
8308 timeo = sock_rcvtimeo(sk, noblock);
8310 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8311 MAX_SCHEDULE_TIMEOUT);
8314 /* Again only user level code calls this function,
8315 * so nothing interrupt level
8316 * will suddenly eat the receive_queue.
8318 * Look at current nfs client by the way...
8319 * However, this function was correct in any case. 8)
8321 if (flags & MSG_PEEK) {
8322 skb = skb_peek(&sk->sk_receive_queue);
8324 refcount_inc(&skb->users);
8326 skb = __skb_dequeue(&sk->sk_receive_queue);
8332 /* Caller is allowed not to check sk->sk_err before calling. */
8333 error = sock_error(sk);
8337 if (sk->sk_shutdown & RCV_SHUTDOWN)
8340 if (sk_can_busy_loop(sk)) {
8341 sk_busy_loop(sk, noblock);
8343 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8347 /* User doesn't want to wait. */
8351 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8360 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8361 static void __sctp_write_space(struct sctp_association *asoc)
8363 struct sock *sk = asoc->base.sk;
8365 if (sctp_wspace(asoc) <= 0)
8368 if (waitqueue_active(&asoc->wait))
8369 wake_up_interruptible(&asoc->wait);
8371 if (sctp_writeable(sk)) {
8372 struct socket_wq *wq;
8375 wq = rcu_dereference(sk->sk_wq);
8377 if (waitqueue_active(&wq->wait))
8378 wake_up_interruptible(&wq->wait);
8380 /* Note that we try to include the Async I/O support
8381 * here by modeling from the current TCP/UDP code.
8382 * We have not tested with it yet.
8384 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8385 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8391 static void sctp_wake_up_waiters(struct sock *sk,
8392 struct sctp_association *asoc)
8394 struct sctp_association *tmp = asoc;
8396 /* We do accounting for the sndbuf space per association,
8397 * so we only need to wake our own association.
8399 if (asoc->ep->sndbuf_policy)
8400 return __sctp_write_space(asoc);
8402 /* If association goes down and is just flushing its
8403 * outq, then just normally notify others.
8405 if (asoc->base.dead)
8406 return sctp_write_space(sk);
8408 /* Accounting for the sndbuf space is per socket, so we
8409 * need to wake up others, try to be fair and in case of
8410 * other associations, let them have a go first instead
8411 * of just doing a sctp_write_space() call.
8413 * Note that we reach sctp_wake_up_waiters() only when
8414 * associations free up queued chunks, thus we are under
8415 * lock and the list of associations on a socket is
8416 * guaranteed not to change.
8418 for (tmp = list_next_entry(tmp, asocs); 1;
8419 tmp = list_next_entry(tmp, asocs)) {
8420 /* Manually skip the head element. */
8421 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8423 /* Wake up association. */
8424 __sctp_write_space(tmp);
8425 /* We've reached the end. */
8431 /* Do accounting for the sndbuf space.
8432 * Decrement the used sndbuf space of the corresponding association by the
8433 * data size which was just transmitted(freed).
8435 static void sctp_wfree(struct sk_buff *skb)
8437 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8438 struct sctp_association *asoc = chunk->asoc;
8439 struct sock *sk = asoc->base.sk;
8441 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8442 sizeof(struct sk_buff) +
8443 sizeof(struct sctp_chunk);
8445 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
8448 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
8450 sk->sk_wmem_queued -= skb->truesize;
8451 sk_mem_uncharge(sk, skb->truesize);
8454 struct sctp_shared_key *shkey = chunk->shkey;
8456 /* refcnt == 2 and !list_empty mean after this release, it's
8457 * not being used anywhere, and it's time to notify userland
8458 * that this shkey can be freed if it's been deactivated.
8460 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8461 refcount_read(&shkey->refcnt) == 2) {
8462 struct sctp_ulpevent *ev;
8464 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8468 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8470 sctp_auth_shkey_release(chunk->shkey);
8474 sctp_wake_up_waiters(sk, asoc);
8476 sctp_association_put(asoc);
8479 /* Do accounting for the receive space on the socket.
8480 * Accounting for the association is done in ulpevent.c
8481 * We set this as a destructor for the cloned data skbs so that
8482 * accounting is done at the correct time.
8484 void sctp_sock_rfree(struct sk_buff *skb)
8486 struct sock *sk = skb->sk;
8487 struct sctp_ulpevent *event = sctp_skb2event(skb);
8489 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8492 * Mimic the behavior of sock_rfree
8494 sk_mem_uncharge(sk, event->rmem_len);
8498 /* Helper function to wait for space in the sndbuf. */
8499 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8502 struct sock *sk = asoc->base.sk;
8503 long current_timeo = *timeo_p;
8507 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8510 /* Increment the association's refcnt. */
8511 sctp_association_hold(asoc);
8513 /* Wait on the association specific sndbuf space. */
8515 prepare_to_wait_exclusive(&asoc->wait, &wait,
8516 TASK_INTERRUPTIBLE);
8517 if (asoc->base.dead)
8521 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8523 if (signal_pending(current))
8524 goto do_interrupted;
8525 if (sk_under_memory_pressure(sk))
8527 if ((int)msg_len <= sctp_wspace(asoc) &&
8528 sk_wmem_schedule(sk, msg_len))
8531 /* Let another process have a go. Since we are going
8535 current_timeo = schedule_timeout(current_timeo);
8537 if (sk != asoc->base.sk)
8540 *timeo_p = current_timeo;
8544 finish_wait(&asoc->wait, &wait);
8546 /* Release the association's refcnt. */
8547 sctp_association_put(asoc);
8560 err = sock_intr_errno(*timeo_p);
8568 void sctp_data_ready(struct sock *sk)
8570 struct socket_wq *wq;
8573 wq = rcu_dereference(sk->sk_wq);
8574 if (skwq_has_sleeper(wq))
8575 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8576 EPOLLRDNORM | EPOLLRDBAND);
8577 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8581 /* If socket sndbuf has changed, wake up all per association waiters. */
8582 void sctp_write_space(struct sock *sk)
8584 struct sctp_association *asoc;
8586 /* Wake up the tasks in each wait queue. */
8587 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8588 __sctp_write_space(asoc);
8592 /* Is there any sndbuf space available on the socket?
8594 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8595 * associations on the same socket. For a UDP-style socket with
8596 * multiple associations, it is possible for it to be "unwriteable"
8597 * prematurely. I assume that this is acceptable because
8598 * a premature "unwriteable" is better than an accidental "writeable" which
8599 * would cause an unwanted block under certain circumstances. For the 1-1
8600 * UDP-style sockets or TCP-style sockets, this code should work.
8603 static bool sctp_writeable(struct sock *sk)
8605 return sk->sk_sndbuf > sk->sk_wmem_queued;
8608 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8609 * returns immediately with EINPROGRESS.
8611 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8613 struct sock *sk = asoc->base.sk;
8615 long current_timeo = *timeo_p;
8618 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8620 /* Increment the association's refcnt. */
8621 sctp_association_hold(asoc);
8624 prepare_to_wait_exclusive(&asoc->wait, &wait,
8625 TASK_INTERRUPTIBLE);
8628 if (sk->sk_shutdown & RCV_SHUTDOWN)
8630 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8633 if (signal_pending(current))
8634 goto do_interrupted;
8636 if (sctp_state(asoc, ESTABLISHED))
8639 /* Let another process have a go. Since we are going
8643 current_timeo = schedule_timeout(current_timeo);
8646 *timeo_p = current_timeo;
8650 finish_wait(&asoc->wait, &wait);
8652 /* Release the association's refcnt. */
8653 sctp_association_put(asoc);
8658 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
8661 err = -ECONNREFUSED;
8665 err = sock_intr_errno(*timeo_p);
8673 static int sctp_wait_for_accept(struct sock *sk, long timeo)
8675 struct sctp_endpoint *ep;
8679 ep = sctp_sk(sk)->ep;
8683 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
8684 TASK_INTERRUPTIBLE);
8686 if (list_empty(&ep->asocs)) {
8688 timeo = schedule_timeout(timeo);
8693 if (!sctp_sstate(sk, LISTENING))
8697 if (!list_empty(&ep->asocs))
8700 err = sock_intr_errno(timeo);
8701 if (signal_pending(current))
8709 finish_wait(sk_sleep(sk), &wait);
8714 static void sctp_wait_for_close(struct sock *sk, long timeout)
8719 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8720 if (list_empty(&sctp_sk(sk)->ep->asocs))
8723 timeout = schedule_timeout(timeout);
8725 } while (!signal_pending(current) && timeout);
8727 finish_wait(sk_sleep(sk), &wait);
8730 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8732 struct sk_buff *frag;
8737 /* Don't forget the fragments. */
8738 skb_walk_frags(skb, frag)
8739 sctp_skb_set_owner_r_frag(frag, sk);
8742 sctp_skb_set_owner_r(skb, sk);
8745 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8746 struct sctp_association *asoc)
8748 struct inet_sock *inet = inet_sk(sk);
8749 struct inet_sock *newinet;
8750 struct sctp_sock *sp = sctp_sk(sk);
8751 struct sctp_endpoint *ep = sp->ep;
8753 newsk->sk_type = sk->sk_type;
8754 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8755 newsk->sk_flags = sk->sk_flags;
8756 newsk->sk_tsflags = sk->sk_tsflags;
8757 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8758 newsk->sk_no_check_rx = sk->sk_no_check_rx;
8759 newsk->sk_reuse = sk->sk_reuse;
8760 sctp_sk(newsk)->reuse = sp->reuse;
8762 newsk->sk_shutdown = sk->sk_shutdown;
8763 newsk->sk_destruct = sctp_destruct_sock;
8764 newsk->sk_family = sk->sk_family;
8765 newsk->sk_protocol = IPPROTO_SCTP;
8766 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8767 newsk->sk_sndbuf = sk->sk_sndbuf;
8768 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8769 newsk->sk_lingertime = sk->sk_lingertime;
8770 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8771 newsk->sk_sndtimeo = sk->sk_sndtimeo;
8772 newsk->sk_rxhash = sk->sk_rxhash;
8774 newinet = inet_sk(newsk);
8776 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8777 * getsockname() and getpeername()
8779 newinet->inet_sport = inet->inet_sport;
8780 newinet->inet_saddr = inet->inet_saddr;
8781 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8782 newinet->inet_dport = htons(asoc->peer.port);
8783 newinet->pmtudisc = inet->pmtudisc;
8784 newinet->inet_id = prandom_u32();
8786 newinet->uc_ttl = inet->uc_ttl;
8787 newinet->mc_loop = 1;
8788 newinet->mc_ttl = 1;
8789 newinet->mc_index = 0;
8790 newinet->mc_list = NULL;
8792 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8793 net_enable_timestamp();
8795 /* Set newsk security attributes from orginal sk and connection
8796 * security attribute from ep.
8798 security_sctp_sk_clone(ep, sk, newsk);
8801 static inline void sctp_copy_descendant(struct sock *sk_to,
8802 const struct sock *sk_from)
8804 int ancestor_size = sizeof(struct inet_sock) +
8805 sizeof(struct sctp_sock) -
8806 offsetof(struct sctp_sock, auto_asconf_list);
8808 if (sk_from->sk_family == PF_INET6)
8809 ancestor_size += sizeof(struct ipv6_pinfo);
8811 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8814 /* Populate the fields of the newsk from the oldsk and migrate the assoc
8815 * and its messages to the newsk.
8817 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8818 struct sctp_association *assoc,
8819 enum sctp_socket_type type)
8821 struct sctp_sock *oldsp = sctp_sk(oldsk);
8822 struct sctp_sock *newsp = sctp_sk(newsk);
8823 struct sctp_bind_bucket *pp; /* hash list port iterator */
8824 struct sctp_endpoint *newep = newsp->ep;
8825 struct sk_buff *skb, *tmp;
8826 struct sctp_ulpevent *event;
8827 struct sctp_bind_hashbucket *head;
8829 /* Migrate socket buffer sizes and all the socket level options to the
8832 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8833 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8834 /* Brute force copy old sctp opt. */
8835 sctp_copy_descendant(newsk, oldsk);
8837 /* Restore the ep value that was overwritten with the above structure
8843 /* Hook this new socket in to the bind_hash list. */
8844 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8845 inet_sk(oldsk)->inet_num)];
8846 spin_lock_bh(&head->lock);
8847 pp = sctp_sk(oldsk)->bind_hash;
8848 sk_add_bind_node(newsk, &pp->owner);
8849 sctp_sk(newsk)->bind_hash = pp;
8850 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
8851 spin_unlock_bh(&head->lock);
8853 /* Copy the bind_addr list from the original endpoint to the new
8854 * endpoint so that we can handle restarts properly
8856 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8857 &oldsp->ep->base.bind_addr, GFP_KERNEL);
8859 sctp_auto_asconf_init(newsp);
8861 /* Move any messages in the old socket's receive queue that are for the
8862 * peeled off association to the new socket's receive queue.
8864 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8865 event = sctp_skb2event(skb);
8866 if (event->asoc == assoc) {
8867 __skb_unlink(skb, &oldsk->sk_receive_queue);
8868 __skb_queue_tail(&newsk->sk_receive_queue, skb);
8869 sctp_skb_set_owner_r_frag(skb, newsk);
8873 /* Clean up any messages pending delivery due to partial
8874 * delivery. Three cases:
8875 * 1) No partial deliver; no work.
8876 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8877 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8879 skb_queue_head_init(&newsp->pd_lobby);
8880 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
8882 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
8883 struct sk_buff_head *queue;
8885 /* Decide which queue to move pd_lobby skbs to. */
8886 if (assoc->ulpq.pd_mode) {
8887 queue = &newsp->pd_lobby;
8889 queue = &newsk->sk_receive_queue;
8891 /* Walk through the pd_lobby, looking for skbs that
8892 * need moved to the new socket.
8894 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8895 event = sctp_skb2event(skb);
8896 if (event->asoc == assoc) {
8897 __skb_unlink(skb, &oldsp->pd_lobby);
8898 __skb_queue_tail(queue, skb);
8899 sctp_skb_set_owner_r_frag(skb, newsk);
8903 /* Clear up any skbs waiting for the partial
8904 * delivery to finish.
8906 if (assoc->ulpq.pd_mode)
8907 sctp_clear_pd(oldsk, NULL);
8911 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
8913 /* Set the type of socket to indicate that it is peeled off from the
8914 * original UDP-style socket or created with the accept() call on a
8915 * TCP-style socket..
8919 /* Mark the new socket "in-use" by the user so that any packets
8920 * that may arrive on the association after we've moved it are
8921 * queued to the backlog. This prevents a potential race between
8922 * backlog processing on the old socket and new-packet processing
8923 * on the new socket.
8925 * The caller has just allocated newsk so we can guarantee that other
8926 * paths won't try to lock it and then oldsk.
8928 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
8929 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
8930 sctp_assoc_migrate(assoc, newsk);
8931 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
8933 /* If the association on the newsk is already closed before accept()
8934 * is called, set RCV_SHUTDOWN flag.
8936 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
8937 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
8938 newsk->sk_shutdown |= RCV_SHUTDOWN;
8940 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
8943 release_sock(newsk);
8947 /* This proto struct describes the ULP interface for SCTP. */
8948 struct proto sctp_prot = {
8950 .owner = THIS_MODULE,
8951 .close = sctp_close,
8952 .disconnect = sctp_disconnect,
8953 .accept = sctp_accept,
8954 .ioctl = sctp_ioctl,
8955 .init = sctp_init_sock,
8956 .destroy = sctp_destroy_sock,
8957 .shutdown = sctp_shutdown,
8958 .setsockopt = sctp_setsockopt,
8959 .getsockopt = sctp_getsockopt,
8960 .sendmsg = sctp_sendmsg,
8961 .recvmsg = sctp_recvmsg,
8963 .backlog_rcv = sctp_backlog_rcv,
8965 .unhash = sctp_unhash,
8966 .no_autobind = true,
8967 .obj_size = sizeof(struct sctp_sock),
8968 .useroffset = offsetof(struct sctp_sock, subscribe),
8969 .usersize = offsetof(struct sctp_sock, initmsg) -
8970 offsetof(struct sctp_sock, subscribe) +
8971 sizeof_field(struct sctp_sock, initmsg),
8972 .sysctl_mem = sysctl_sctp_mem,
8973 .sysctl_rmem = sysctl_sctp_rmem,
8974 .sysctl_wmem = sysctl_sctp_wmem,
8975 .memory_pressure = &sctp_memory_pressure,
8976 .enter_memory_pressure = sctp_enter_memory_pressure,
8977 .memory_allocated = &sctp_memory_allocated,
8978 .sockets_allocated = &sctp_sockets_allocated,
8981 #if IS_ENABLED(CONFIG_IPV6)
8983 #include <net/transp_v6.h>
8984 static void sctp_v6_destroy_sock(struct sock *sk)
8986 sctp_destroy_sock(sk);
8987 inet6_destroy_sock(sk);
8990 struct proto sctpv6_prot = {
8992 .owner = THIS_MODULE,
8993 .close = sctp_close,
8994 .disconnect = sctp_disconnect,
8995 .accept = sctp_accept,
8996 .ioctl = sctp_ioctl,
8997 .init = sctp_init_sock,
8998 .destroy = sctp_v6_destroy_sock,
8999 .shutdown = sctp_shutdown,
9000 .setsockopt = sctp_setsockopt,
9001 .getsockopt = sctp_getsockopt,
9002 .sendmsg = sctp_sendmsg,
9003 .recvmsg = sctp_recvmsg,
9005 .backlog_rcv = sctp_backlog_rcv,
9007 .unhash = sctp_unhash,
9008 .no_autobind = true,
9009 .obj_size = sizeof(struct sctp6_sock),
9010 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9011 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9012 offsetof(struct sctp6_sock, sctp.subscribe) +
9013 sizeof_field(struct sctp6_sock, sctp.initmsg),
9014 .sysctl_mem = sysctl_sctp_mem,
9015 .sysctl_rmem = sysctl_sctp_rmem,
9016 .sysctl_wmem = sysctl_sctp_wmem,
9017 .memory_pressure = &sctp_memory_pressure,
9018 .enter_memory_pressure = sctp_enter_memory_pressure,
9019 .memory_allocated = &sctp_memory_allocated,
9020 .sockets_allocated = &sctp_sockets_allocated,
9022 #endif /* IS_ENABLED(CONFIG_IPV6) */