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_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5060 int (*cb_done)(struct sctp_transport *, void *),
5061 struct net *net, int *pos, void *p) {
5062 struct rhashtable_iter hti;
5063 struct sctp_transport *tsp;
5068 sctp_transport_walk_start(&hti);
5070 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5071 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5076 sctp_transport_put(tsp);
5078 sctp_transport_walk_stop(&hti);
5081 if (cb_done && !cb_done(tsp, p)) {
5083 sctp_transport_put(tsp);
5086 sctp_transport_put(tsp);
5091 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5093 /* 7.2.1 Association Status (SCTP_STATUS)
5095 * Applications can retrieve current status information about an
5096 * association, including association state, peer receiver window size,
5097 * number of unacked data chunks, and number of data chunks pending
5098 * receipt. This information is read-only.
5100 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5101 char __user *optval,
5104 struct sctp_status status;
5105 struct sctp_association *asoc = NULL;
5106 struct sctp_transport *transport;
5107 sctp_assoc_t associd;
5110 if (len < sizeof(status)) {
5115 len = sizeof(status);
5116 if (copy_from_user(&status, optval, len)) {
5121 associd = status.sstat_assoc_id;
5122 asoc = sctp_id2assoc(sk, associd);
5128 transport = asoc->peer.primary_path;
5130 status.sstat_assoc_id = sctp_assoc2id(asoc);
5131 status.sstat_state = sctp_assoc_to_state(asoc);
5132 status.sstat_rwnd = asoc->peer.rwnd;
5133 status.sstat_unackdata = asoc->unack_data;
5135 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5136 status.sstat_instrms = asoc->stream.incnt;
5137 status.sstat_outstrms = asoc->stream.outcnt;
5138 status.sstat_fragmentation_point = asoc->frag_point;
5139 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5140 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5141 transport->af_specific->sockaddr_len);
5142 /* Map ipv4 address into v4-mapped-on-v6 address. */
5143 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5144 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5145 status.sstat_primary.spinfo_state = transport->state;
5146 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5147 status.sstat_primary.spinfo_srtt = transport->srtt;
5148 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5149 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5151 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5152 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5154 if (put_user(len, optlen)) {
5159 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5160 __func__, len, status.sstat_state, status.sstat_rwnd,
5161 status.sstat_assoc_id);
5163 if (copy_to_user(optval, &status, len)) {
5173 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5175 * Applications can retrieve information about a specific peer address
5176 * of an association, including its reachability state, congestion
5177 * window, and retransmission timer values. This information is
5180 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5181 char __user *optval,
5184 struct sctp_paddrinfo pinfo;
5185 struct sctp_transport *transport;
5188 if (len < sizeof(pinfo)) {
5193 len = sizeof(pinfo);
5194 if (copy_from_user(&pinfo, optval, len)) {
5199 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5200 pinfo.spinfo_assoc_id);
5204 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5205 pinfo.spinfo_state = transport->state;
5206 pinfo.spinfo_cwnd = transport->cwnd;
5207 pinfo.spinfo_srtt = transport->srtt;
5208 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5209 pinfo.spinfo_mtu = transport->pathmtu;
5211 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5212 pinfo.spinfo_state = SCTP_ACTIVE;
5214 if (put_user(len, optlen)) {
5219 if (copy_to_user(optval, &pinfo, len)) {
5228 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5230 * This option is a on/off flag. If enabled no SCTP message
5231 * fragmentation will be performed. Instead if a message being sent
5232 * exceeds the current PMTU size, the message will NOT be sent and
5233 * instead a error will be indicated to the user.
5235 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5236 char __user *optval, int __user *optlen)
5240 if (len < sizeof(int))
5244 val = (sctp_sk(sk)->disable_fragments == 1);
5245 if (put_user(len, optlen))
5247 if (copy_to_user(optval, &val, len))
5252 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5254 * This socket option is used to specify various notifications and
5255 * ancillary data the user wishes to receive.
5257 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5262 if (len > sizeof(struct sctp_event_subscribe))
5263 len = sizeof(struct sctp_event_subscribe);
5264 if (put_user(len, optlen))
5266 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5271 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5273 * This socket option is applicable to the UDP-style socket only. When
5274 * set it will cause associations that are idle for more than the
5275 * specified number of seconds to automatically close. An association
5276 * being idle is defined an association that has NOT sent or received
5277 * user data. The special value of '0' indicates that no automatic
5278 * close of any associations should be performed. The option expects an
5279 * integer defining the number of seconds of idle time before an
5280 * association is closed.
5282 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5284 /* Applicable to UDP-style socket only */
5285 if (sctp_style(sk, TCP))
5287 if (len < sizeof(int))
5290 if (put_user(len, optlen))
5292 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5297 /* Helper routine to branch off an association to a new socket. */
5298 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5300 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5301 struct sctp_sock *sp = sctp_sk(sk);
5302 struct socket *sock;
5305 /* Do not peel off from one netns to another one. */
5306 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5312 /* An association cannot be branched off from an already peeled-off
5313 * socket, nor is this supported for tcp style sockets.
5315 if (!sctp_style(sk, UDP))
5318 /* Create a new socket. */
5319 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5323 sctp_copy_sock(sock->sk, sk, asoc);
5325 /* Make peeled-off sockets more like 1-1 accepted sockets.
5326 * Set the daddr and initialize id to something more random and also
5327 * copy over any ip options.
5329 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5330 sp->pf->copy_ip_options(sk, sock->sk);
5332 /* Populate the fields of the newsk from the oldsk and migrate the
5333 * asoc to the newsk.
5335 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5341 EXPORT_SYMBOL(sctp_do_peeloff);
5343 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5344 struct file **newfile, unsigned flags)
5346 struct socket *newsock;
5349 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5353 /* Map the socket to an unused fd that can be returned to the user. */
5354 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5356 sock_release(newsock);
5360 *newfile = sock_alloc_file(newsock, 0, NULL);
5361 if (IS_ERR(*newfile)) {
5362 put_unused_fd(retval);
5363 retval = PTR_ERR(*newfile);
5368 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5371 peeloff->sd = retval;
5373 if (flags & SOCK_NONBLOCK)
5374 (*newfile)->f_flags |= O_NONBLOCK;
5379 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5381 sctp_peeloff_arg_t peeloff;
5382 struct file *newfile = NULL;
5385 if (len < sizeof(sctp_peeloff_arg_t))
5387 len = sizeof(sctp_peeloff_arg_t);
5388 if (copy_from_user(&peeloff, optval, len))
5391 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5395 /* Return the fd mapped to the new socket. */
5396 if (put_user(len, optlen)) {
5398 put_unused_fd(retval);
5402 if (copy_to_user(optval, &peeloff, len)) {
5404 put_unused_fd(retval);
5407 fd_install(retval, newfile);
5412 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5413 char __user *optval, int __user *optlen)
5415 sctp_peeloff_flags_arg_t peeloff;
5416 struct file *newfile = NULL;
5419 if (len < sizeof(sctp_peeloff_flags_arg_t))
5421 len = sizeof(sctp_peeloff_flags_arg_t);
5422 if (copy_from_user(&peeloff, optval, len))
5425 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5426 &newfile, peeloff.flags);
5430 /* Return the fd mapped to the new socket. */
5431 if (put_user(len, optlen)) {
5433 put_unused_fd(retval);
5437 if (copy_to_user(optval, &peeloff, len)) {
5439 put_unused_fd(retval);
5442 fd_install(retval, newfile);
5447 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5449 * Applications can enable or disable heartbeats for any peer address of
5450 * an association, modify an address's heartbeat interval, force a
5451 * heartbeat to be sent immediately, and adjust the address's maximum
5452 * number of retransmissions sent before an address is considered
5453 * unreachable. The following structure is used to access and modify an
5454 * address's parameters:
5456 * struct sctp_paddrparams {
5457 * sctp_assoc_t spp_assoc_id;
5458 * struct sockaddr_storage spp_address;
5459 * uint32_t spp_hbinterval;
5460 * uint16_t spp_pathmaxrxt;
5461 * uint32_t spp_pathmtu;
5462 * uint32_t spp_sackdelay;
5463 * uint32_t spp_flags;
5466 * spp_assoc_id - (one-to-many style socket) This is filled in the
5467 * application, and identifies the association for
5469 * spp_address - This specifies which address is of interest.
5470 * spp_hbinterval - This contains the value of the heartbeat interval,
5471 * in milliseconds. If a value of zero
5472 * is present in this field then no changes are to
5473 * be made to this parameter.
5474 * spp_pathmaxrxt - This contains the maximum number of
5475 * retransmissions before this address shall be
5476 * considered unreachable. If a value of zero
5477 * is present in this field then no changes are to
5478 * be made to this parameter.
5479 * spp_pathmtu - When Path MTU discovery is disabled the value
5480 * specified here will be the "fixed" path mtu.
5481 * Note that if the spp_address field is empty
5482 * then all associations on this address will
5483 * have this fixed path mtu set upon them.
5485 * spp_sackdelay - When delayed sack is enabled, this value specifies
5486 * the number of milliseconds that sacks will be delayed
5487 * for. This value will apply to all addresses of an
5488 * association if the spp_address field is empty. Note
5489 * also, that if delayed sack is enabled and this
5490 * value is set to 0, no change is made to the last
5491 * recorded delayed sack timer value.
5493 * spp_flags - These flags are used to control various features
5494 * on an association. The flag field may contain
5495 * zero or more of the following options.
5497 * SPP_HB_ENABLE - Enable heartbeats on the
5498 * specified address. Note that if the address
5499 * field is empty all addresses for the association
5500 * have heartbeats enabled upon them.
5502 * SPP_HB_DISABLE - Disable heartbeats on the
5503 * speicifed address. Note that if the address
5504 * field is empty all addresses for the association
5505 * will have their heartbeats disabled. Note also
5506 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5507 * mutually exclusive, only one of these two should
5508 * be specified. Enabling both fields will have
5509 * undetermined results.
5511 * SPP_HB_DEMAND - Request a user initiated heartbeat
5512 * to be made immediately.
5514 * SPP_PMTUD_ENABLE - This field will enable PMTU
5515 * discovery upon the specified address. Note that
5516 * if the address feild is empty then all addresses
5517 * on the association are effected.
5519 * SPP_PMTUD_DISABLE - This field will disable PMTU
5520 * discovery upon the specified address. Note that
5521 * if the address feild is empty then all addresses
5522 * on the association are effected. Not also that
5523 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5524 * exclusive. Enabling both will have undetermined
5527 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5528 * on delayed sack. The time specified in spp_sackdelay
5529 * is used to specify the sack delay for this address. Note
5530 * that if spp_address is empty then all addresses will
5531 * enable delayed sack and take on the sack delay
5532 * value specified in spp_sackdelay.
5533 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5534 * off delayed sack. If the spp_address field is blank then
5535 * delayed sack is disabled for the entire association. Note
5536 * also that this field is mutually exclusive to
5537 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5540 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5541 * setting of the IPV6 flow label value. The value is
5542 * contained in the spp_ipv6_flowlabel field.
5543 * Upon retrieval, this flag will be set to indicate that
5544 * the spp_ipv6_flowlabel field has a valid value returned.
5545 * If a specific destination address is set (in the
5546 * spp_address field), then the value returned is that of
5547 * the address. If just an association is specified (and
5548 * no address), then the association's default flow label
5549 * is returned. If neither an association nor a destination
5550 * is specified, then the socket's default flow label is
5551 * returned. For non-IPv6 sockets, this flag will be left
5554 * SPP_DSCP: Setting this flag enables the setting of the
5555 * Differentiated Services Code Point (DSCP) value
5556 * associated with either the association or a specific
5557 * address. The value is obtained in the spp_dscp field.
5558 * Upon retrieval, this flag will be set to indicate that
5559 * the spp_dscp field has a valid value returned. If a
5560 * specific destination address is set when called (in the
5561 * spp_address field), then that specific destination
5562 * address's DSCP value is returned. If just an association
5563 * is specified, then the association's default DSCP is
5564 * returned. If neither an association nor a destination is
5565 * specified, then the socket's default DSCP is returned.
5567 * spp_ipv6_flowlabel
5568 * - This field is used in conjunction with the
5569 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5570 * The 20 least significant bits are used for the flow
5571 * label. This setting has precedence over any IPv6-layer
5574 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5575 * and contains the DSCP. The 6 most significant bits are
5576 * used for the DSCP. This setting has precedence over any
5577 * IPv4- or IPv6- layer setting.
5579 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5580 char __user *optval, int __user *optlen)
5582 struct sctp_paddrparams params;
5583 struct sctp_transport *trans = NULL;
5584 struct sctp_association *asoc = NULL;
5585 struct sctp_sock *sp = sctp_sk(sk);
5587 if (len >= sizeof(params))
5588 len = sizeof(params);
5589 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5590 spp_ipv6_flowlabel), 4))
5591 len = ALIGN(offsetof(struct sctp_paddrparams,
5592 spp_ipv6_flowlabel), 4);
5596 if (copy_from_user(¶ms, optval, len))
5599 /* If an address other than INADDR_ANY is specified, and
5600 * no transport is found, then the request is invalid.
5602 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5603 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5604 params.spp_assoc_id);
5606 pr_debug("%s: failed no transport\n", __func__);
5611 /* Get association, if assoc_id != 0 and the socket is a one
5612 * to many style socket, and an association was not found, then
5613 * the id was invalid.
5615 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5616 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
5617 pr_debug("%s: failed no association\n", __func__);
5622 /* Fetch transport values. */
5623 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5624 params.spp_pathmtu = trans->pathmtu;
5625 params.spp_pathmaxrxt = trans->pathmaxrxt;
5626 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5628 /*draft-11 doesn't say what to return in spp_flags*/
5629 params.spp_flags = trans->param_flags;
5630 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5631 params.spp_ipv6_flowlabel = trans->flowlabel &
5632 SCTP_FLOWLABEL_VAL_MASK;
5633 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5635 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5636 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5637 params.spp_flags |= SPP_DSCP;
5640 /* Fetch association values. */
5641 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5642 params.spp_pathmtu = asoc->pathmtu;
5643 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5644 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5646 /*draft-11 doesn't say what to return in spp_flags*/
5647 params.spp_flags = asoc->param_flags;
5648 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5649 params.spp_ipv6_flowlabel = asoc->flowlabel &
5650 SCTP_FLOWLABEL_VAL_MASK;
5651 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5653 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5654 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5655 params.spp_flags |= SPP_DSCP;
5658 /* Fetch socket values. */
5659 params.spp_hbinterval = sp->hbinterval;
5660 params.spp_pathmtu = sp->pathmtu;
5661 params.spp_sackdelay = sp->sackdelay;
5662 params.spp_pathmaxrxt = sp->pathmaxrxt;
5664 /*draft-11 doesn't say what to return in spp_flags*/
5665 params.spp_flags = sp->param_flags;
5666 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5667 params.spp_ipv6_flowlabel = sp->flowlabel &
5668 SCTP_FLOWLABEL_VAL_MASK;
5669 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5671 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5672 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5673 params.spp_flags |= SPP_DSCP;
5677 if (copy_to_user(optval, ¶ms, len))
5680 if (put_user(len, optlen))
5687 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5689 * This option will effect the way delayed acks are performed. This
5690 * option allows you to get or set the delayed ack time, in
5691 * milliseconds. It also allows changing the delayed ack frequency.
5692 * Changing the frequency to 1 disables the delayed sack algorithm. If
5693 * the assoc_id is 0, then this sets or gets the endpoints default
5694 * values. If the assoc_id field is non-zero, then the set or get
5695 * effects the specified association for the one to many model (the
5696 * assoc_id field is ignored by the one to one model). Note that if
5697 * sack_delay or sack_freq are 0 when setting this option, then the
5698 * current values will remain unchanged.
5700 * struct sctp_sack_info {
5701 * sctp_assoc_t sack_assoc_id;
5702 * uint32_t sack_delay;
5703 * uint32_t sack_freq;
5706 * sack_assoc_id - This parameter, indicates which association the user
5707 * is performing an action upon. Note that if this field's value is
5708 * zero then the endpoints default value is changed (effecting future
5709 * associations only).
5711 * sack_delay - This parameter contains the number of milliseconds that
5712 * the user is requesting the delayed ACK timer be set to. Note that
5713 * this value is defined in the standard to be between 200 and 500
5716 * sack_freq - This parameter contains the number of packets that must
5717 * be received before a sack is sent without waiting for the delay
5718 * timer to expire. The default value for this is 2, setting this
5719 * value to 1 will disable the delayed sack algorithm.
5721 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5722 char __user *optval,
5725 struct sctp_sack_info params;
5726 struct sctp_association *asoc = NULL;
5727 struct sctp_sock *sp = sctp_sk(sk);
5729 if (len >= sizeof(struct sctp_sack_info)) {
5730 len = sizeof(struct sctp_sack_info);
5732 if (copy_from_user(¶ms, optval, len))
5734 } else if (len == sizeof(struct sctp_assoc_value)) {
5735 pr_warn_ratelimited(DEPRECATED
5737 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5738 "Use struct sctp_sack_info instead\n",
5739 current->comm, task_pid_nr(current));
5740 if (copy_from_user(¶ms, optval, len))
5745 /* Get association, if sack_assoc_id != 0 and the socket is a one
5746 * to many style socket, and an association was not found, then
5747 * the id was invalid.
5749 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5750 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
5754 /* Fetch association values. */
5755 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5756 params.sack_delay = jiffies_to_msecs(
5758 params.sack_freq = asoc->sackfreq;
5761 params.sack_delay = 0;
5762 params.sack_freq = 1;
5765 /* Fetch socket values. */
5766 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5767 params.sack_delay = sp->sackdelay;
5768 params.sack_freq = sp->sackfreq;
5770 params.sack_delay = 0;
5771 params.sack_freq = 1;
5775 if (copy_to_user(optval, ¶ms, len))
5778 if (put_user(len, optlen))
5784 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5786 * Applications can specify protocol parameters for the default association
5787 * initialization. The option name argument to setsockopt() and getsockopt()
5790 * Setting initialization parameters is effective only on an unconnected
5791 * socket (for UDP-style sockets only future associations are effected
5792 * by the change). With TCP-style sockets, this option is inherited by
5793 * sockets derived from a listener socket.
5795 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5797 if (len < sizeof(struct sctp_initmsg))
5799 len = sizeof(struct sctp_initmsg);
5800 if (put_user(len, optlen))
5802 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5808 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5809 char __user *optval, int __user *optlen)
5811 struct sctp_association *asoc;
5813 struct sctp_getaddrs getaddrs;
5814 struct sctp_transport *from;
5816 union sctp_addr temp;
5817 struct sctp_sock *sp = sctp_sk(sk);
5822 if (len < sizeof(struct sctp_getaddrs))
5825 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5828 /* For UDP-style sockets, id specifies the association to query. */
5829 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5833 to = optval + offsetof(struct sctp_getaddrs, addrs);
5834 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5836 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5838 memcpy(&temp, &from->ipaddr, sizeof(temp));
5839 addrlen = sctp_get_pf_specific(sk->sk_family)
5840 ->addr_to_user(sp, &temp);
5841 if (space_left < addrlen)
5843 if (copy_to_user(to, &temp, addrlen))
5847 space_left -= addrlen;
5850 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5852 bytes_copied = ((char __user *)to) - optval;
5853 if (put_user(bytes_copied, optlen))
5859 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5860 size_t space_left, int *bytes_copied)
5862 struct sctp_sockaddr_entry *addr;
5863 union sctp_addr temp;
5866 struct net *net = sock_net(sk);
5869 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
5873 if ((PF_INET == sk->sk_family) &&
5874 (AF_INET6 == addr->a.sa.sa_family))
5876 if ((PF_INET6 == sk->sk_family) &&
5877 inet_v6_ipv6only(sk) &&
5878 (AF_INET == addr->a.sa.sa_family))
5880 memcpy(&temp, &addr->a, sizeof(temp));
5881 if (!temp.v4.sin_port)
5882 temp.v4.sin_port = htons(port);
5884 addrlen = sctp_get_pf_specific(sk->sk_family)
5885 ->addr_to_user(sctp_sk(sk), &temp);
5887 if (space_left < addrlen) {
5891 memcpy(to, &temp, addrlen);
5895 space_left -= addrlen;
5896 *bytes_copied += addrlen;
5904 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5905 char __user *optval, int __user *optlen)
5907 struct sctp_bind_addr *bp;
5908 struct sctp_association *asoc;
5910 struct sctp_getaddrs getaddrs;
5911 struct sctp_sockaddr_entry *addr;
5913 union sctp_addr temp;
5914 struct sctp_sock *sp = sctp_sk(sk);
5918 int bytes_copied = 0;
5922 if (len < sizeof(struct sctp_getaddrs))
5925 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5929 * For UDP-style sockets, id specifies the association to query.
5930 * If the id field is set to the value '0' then the locally bound
5931 * addresses are returned without regard to any particular
5934 if (0 == getaddrs.assoc_id) {
5935 bp = &sctp_sk(sk)->ep->base.bind_addr;
5937 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5940 bp = &asoc->base.bind_addr;
5943 to = optval + offsetof(struct sctp_getaddrs, addrs);
5944 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5946 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
5950 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5951 * addresses from the global local address list.
5953 if (sctp_list_single_entry(&bp->address_list)) {
5954 addr = list_entry(bp->address_list.next,
5955 struct sctp_sockaddr_entry, list);
5956 if (sctp_is_any(sk, &addr->a)) {
5957 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5958 space_left, &bytes_copied);
5968 /* Protection on the bound address list is not needed since
5969 * in the socket option context we hold a socket lock and
5970 * thus the bound address list can't change.
5972 list_for_each_entry(addr, &bp->address_list, list) {
5973 memcpy(&temp, &addr->a, sizeof(temp));
5974 addrlen = sctp_get_pf_specific(sk->sk_family)
5975 ->addr_to_user(sp, &temp);
5976 if (space_left < addrlen) {
5977 err = -ENOMEM; /*fixme: right error?*/
5980 memcpy(buf, &temp, addrlen);
5982 bytes_copied += addrlen;
5984 space_left -= addrlen;
5988 if (copy_to_user(to, addrs, bytes_copied)) {
5992 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5996 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5997 * but we can't change it anymore.
5999 if (put_user(bytes_copied, optlen))
6006 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6008 * Requests that the local SCTP stack use the enclosed peer address as
6009 * the association primary. The enclosed address must be one of the
6010 * association peer's addresses.
6012 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6013 char __user *optval, int __user *optlen)
6015 struct sctp_prim prim;
6016 struct sctp_association *asoc;
6017 struct sctp_sock *sp = sctp_sk(sk);
6019 if (len < sizeof(struct sctp_prim))
6022 len = sizeof(struct sctp_prim);
6024 if (copy_from_user(&prim, optval, len))
6027 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6031 if (!asoc->peer.primary_path)
6034 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6035 asoc->peer.primary_path->af_specific->sockaddr_len);
6037 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6038 (union sctp_addr *)&prim.ssp_addr);
6040 if (put_user(len, optlen))
6042 if (copy_to_user(optval, &prim, len))
6049 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6051 * Requests that the local endpoint set the specified Adaptation Layer
6052 * Indication parameter for all future INIT and INIT-ACK exchanges.
6054 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6055 char __user *optval, int __user *optlen)
6057 struct sctp_setadaptation adaptation;
6059 if (len < sizeof(struct sctp_setadaptation))
6062 len = sizeof(struct sctp_setadaptation);
6064 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6066 if (put_user(len, optlen))
6068 if (copy_to_user(optval, &adaptation, len))
6076 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6078 * Applications that wish to use the sendto() system call may wish to
6079 * specify a default set of parameters that would normally be supplied
6080 * through the inclusion of ancillary data. This socket option allows
6081 * such an application to set the default sctp_sndrcvinfo structure.
6084 * The application that wishes to use this socket option simply passes
6085 * in to this call the sctp_sndrcvinfo structure defined in Section
6086 * 5.2.2) The input parameters accepted by this call include
6087 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6088 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6089 * to this call if the caller is using the UDP model.
6091 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6093 static int sctp_getsockopt_default_send_param(struct sock *sk,
6094 int len, char __user *optval,
6097 struct sctp_sock *sp = sctp_sk(sk);
6098 struct sctp_association *asoc;
6099 struct sctp_sndrcvinfo info;
6101 if (len < sizeof(info))
6106 if (copy_from_user(&info, optval, len))
6109 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6110 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6113 info.sinfo_stream = asoc->default_stream;
6114 info.sinfo_flags = asoc->default_flags;
6115 info.sinfo_ppid = asoc->default_ppid;
6116 info.sinfo_context = asoc->default_context;
6117 info.sinfo_timetolive = asoc->default_timetolive;
6119 info.sinfo_stream = sp->default_stream;
6120 info.sinfo_flags = sp->default_flags;
6121 info.sinfo_ppid = sp->default_ppid;
6122 info.sinfo_context = sp->default_context;
6123 info.sinfo_timetolive = sp->default_timetolive;
6126 if (put_user(len, optlen))
6128 if (copy_to_user(optval, &info, len))
6134 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6135 * (SCTP_DEFAULT_SNDINFO)
6137 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6138 char __user *optval,
6141 struct sctp_sock *sp = sctp_sk(sk);
6142 struct sctp_association *asoc;
6143 struct sctp_sndinfo info;
6145 if (len < sizeof(info))
6150 if (copy_from_user(&info, optval, len))
6153 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6154 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6157 info.snd_sid = asoc->default_stream;
6158 info.snd_flags = asoc->default_flags;
6159 info.snd_ppid = asoc->default_ppid;
6160 info.snd_context = asoc->default_context;
6162 info.snd_sid = sp->default_stream;
6163 info.snd_flags = sp->default_flags;
6164 info.snd_ppid = sp->default_ppid;
6165 info.snd_context = sp->default_context;
6168 if (put_user(len, optlen))
6170 if (copy_to_user(optval, &info, len))
6178 * 7.1.5 SCTP_NODELAY
6180 * Turn on/off any Nagle-like algorithm. This means that packets are
6181 * generally sent as soon as possible and no unnecessary delays are
6182 * introduced, at the cost of more packets in the network. Expects an
6183 * integer boolean flag.
6186 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6187 char __user *optval, int __user *optlen)
6191 if (len < sizeof(int))
6195 val = (sctp_sk(sk)->nodelay == 1);
6196 if (put_user(len, optlen))
6198 if (copy_to_user(optval, &val, len))
6205 * 7.1.1 SCTP_RTOINFO
6207 * The protocol parameters used to initialize and bound retransmission
6208 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6209 * and modify these parameters.
6210 * All parameters are time values, in milliseconds. A value of 0, when
6211 * modifying the parameters, indicates that the current value should not
6215 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6216 char __user *optval,
6217 int __user *optlen) {
6218 struct sctp_rtoinfo rtoinfo;
6219 struct sctp_association *asoc;
6221 if (len < sizeof (struct sctp_rtoinfo))
6224 len = sizeof(struct sctp_rtoinfo);
6226 if (copy_from_user(&rtoinfo, optval, len))
6229 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6231 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6234 /* Values corresponding to the specific association. */
6236 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6237 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6238 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6240 /* Values corresponding to the endpoint. */
6241 struct sctp_sock *sp = sctp_sk(sk);
6243 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6244 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6245 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6248 if (put_user(len, optlen))
6251 if (copy_to_user(optval, &rtoinfo, len))
6259 * 7.1.2 SCTP_ASSOCINFO
6261 * This option is used to tune the maximum retransmission attempts
6262 * of the association.
6263 * Returns an error if the new association retransmission value is
6264 * greater than the sum of the retransmission value of the peer.
6265 * See [SCTP] for more information.
6268 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6269 char __user *optval,
6273 struct sctp_assocparams assocparams;
6274 struct sctp_association *asoc;
6275 struct list_head *pos;
6278 if (len < sizeof (struct sctp_assocparams))
6281 len = sizeof(struct sctp_assocparams);
6283 if (copy_from_user(&assocparams, optval, len))
6286 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6288 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6291 /* Values correspoinding to the specific association */
6293 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6294 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6295 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6296 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6298 list_for_each(pos, &asoc->peer.transport_addr_list) {
6302 assocparams.sasoc_number_peer_destinations = cnt;
6304 /* Values corresponding to the endpoint */
6305 struct sctp_sock *sp = sctp_sk(sk);
6307 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6308 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6309 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6310 assocparams.sasoc_cookie_life =
6311 sp->assocparams.sasoc_cookie_life;
6312 assocparams.sasoc_number_peer_destinations =
6314 sasoc_number_peer_destinations;
6317 if (put_user(len, optlen))
6320 if (copy_to_user(optval, &assocparams, len))
6327 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6329 * This socket option is a boolean flag which turns on or off mapped V4
6330 * addresses. If this option is turned on and the socket is type
6331 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6332 * If this option is turned off, then no mapping will be done of V4
6333 * addresses and a user will receive both PF_INET6 and PF_INET type
6334 * addresses on the socket.
6336 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6337 char __user *optval, int __user *optlen)
6340 struct sctp_sock *sp = sctp_sk(sk);
6342 if (len < sizeof(int))
6347 if (put_user(len, optlen))
6349 if (copy_to_user(optval, &val, len))
6356 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6357 * (chapter and verse is quoted at sctp_setsockopt_context())
6359 static int sctp_getsockopt_context(struct sock *sk, int len,
6360 char __user *optval, int __user *optlen)
6362 struct sctp_assoc_value params;
6363 struct sctp_sock *sp;
6364 struct sctp_association *asoc;
6366 if (len < sizeof(struct sctp_assoc_value))
6369 len = sizeof(struct sctp_assoc_value);
6371 if (copy_from_user(¶ms, optval, len))
6376 if (params.assoc_id != 0) {
6377 asoc = sctp_id2assoc(sk, params.assoc_id);
6380 params.assoc_value = asoc->default_rcv_context;
6382 params.assoc_value = sp->default_rcv_context;
6385 if (put_user(len, optlen))
6387 if (copy_to_user(optval, ¶ms, len))
6394 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6395 * This option will get or set the maximum size to put in any outgoing
6396 * SCTP DATA chunk. If a message is larger than this size it will be
6397 * fragmented by SCTP into the specified size. Note that the underlying
6398 * SCTP implementation may fragment into smaller sized chunks when the
6399 * PMTU of the underlying association is smaller than the value set by
6400 * the user. The default value for this option is '0' which indicates
6401 * the user is NOT limiting fragmentation and only the PMTU will effect
6402 * SCTP's choice of DATA chunk size. Note also that values set larger
6403 * than the maximum size of an IP datagram will effectively let SCTP
6404 * control fragmentation (i.e. the same as setting this option to 0).
6406 * The following structure is used to access and modify this parameter:
6408 * struct sctp_assoc_value {
6409 * sctp_assoc_t assoc_id;
6410 * uint32_t assoc_value;
6413 * assoc_id: This parameter is ignored for one-to-one style sockets.
6414 * For one-to-many style sockets this parameter indicates which
6415 * association the user is performing an action upon. Note that if
6416 * this field's value is zero then the endpoints default value is
6417 * changed (effecting future associations only).
6418 * assoc_value: This parameter specifies the maximum size in bytes.
6420 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6421 char __user *optval, int __user *optlen)
6423 struct sctp_assoc_value params;
6424 struct sctp_association *asoc;
6426 if (len == sizeof(int)) {
6427 pr_warn_ratelimited(DEPRECATED
6429 "Use of int in maxseg socket option.\n"
6430 "Use struct sctp_assoc_value instead\n",
6431 current->comm, task_pid_nr(current));
6432 params.assoc_id = 0;
6433 } else if (len >= sizeof(struct sctp_assoc_value)) {
6434 len = sizeof(struct sctp_assoc_value);
6435 if (copy_from_user(¶ms, optval, len))
6440 asoc = sctp_id2assoc(sk, params.assoc_id);
6441 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6445 params.assoc_value = asoc->frag_point;
6447 params.assoc_value = sctp_sk(sk)->user_frag;
6449 if (put_user(len, optlen))
6451 if (len == sizeof(int)) {
6452 if (copy_to_user(optval, ¶ms.assoc_value, len))
6455 if (copy_to_user(optval, ¶ms, len))
6463 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6464 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6466 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6467 char __user *optval, int __user *optlen)
6471 if (len < sizeof(int))
6476 val = sctp_sk(sk)->frag_interleave;
6477 if (put_user(len, optlen))
6479 if (copy_to_user(optval, &val, len))
6486 * 7.1.25. Set or Get the sctp partial delivery point
6487 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6489 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6490 char __user *optval,
6495 if (len < sizeof(u32))
6500 val = sctp_sk(sk)->pd_point;
6501 if (put_user(len, optlen))
6503 if (copy_to_user(optval, &val, len))
6510 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6511 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6513 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6514 char __user *optval,
6517 struct sctp_assoc_value params;
6518 struct sctp_sock *sp;
6519 struct sctp_association *asoc;
6521 if (len == sizeof(int)) {
6522 pr_warn_ratelimited(DEPRECATED
6524 "Use of int in max_burst socket option.\n"
6525 "Use struct sctp_assoc_value instead\n",
6526 current->comm, task_pid_nr(current));
6527 params.assoc_id = 0;
6528 } else if (len >= sizeof(struct sctp_assoc_value)) {
6529 len = sizeof(struct sctp_assoc_value);
6530 if (copy_from_user(¶ms, optval, len))
6537 if (params.assoc_id != 0) {
6538 asoc = sctp_id2assoc(sk, params.assoc_id);
6541 params.assoc_value = asoc->max_burst;
6543 params.assoc_value = sp->max_burst;
6545 if (len == sizeof(int)) {
6546 if (copy_to_user(optval, ¶ms.assoc_value, len))
6549 if (copy_to_user(optval, ¶ms, len))
6557 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6558 char __user *optval, int __user *optlen)
6560 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6561 struct sctp_hmacalgo __user *p = (void __user *)optval;
6562 struct sctp_hmac_algo_param *hmacs;
6567 if (!ep->auth_enable)
6570 hmacs = ep->auth_hmacs_list;
6571 data_len = ntohs(hmacs->param_hdr.length) -
6572 sizeof(struct sctp_paramhdr);
6574 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6577 len = sizeof(struct sctp_hmacalgo) + data_len;
6578 num_idents = data_len / sizeof(u16);
6580 if (put_user(len, optlen))
6582 if (put_user(num_idents, &p->shmac_num_idents))
6584 for (i = 0; i < num_idents; i++) {
6585 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6587 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6593 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6594 char __user *optval, int __user *optlen)
6596 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6597 struct sctp_authkeyid val;
6598 struct sctp_association *asoc;
6600 if (!ep->auth_enable)
6603 if (len < sizeof(struct sctp_authkeyid))
6606 len = sizeof(struct sctp_authkeyid);
6607 if (copy_from_user(&val, optval, len))
6610 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6611 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6615 val.scact_keynumber = asoc->active_key_id;
6617 val.scact_keynumber = ep->active_key_id;
6619 if (put_user(len, optlen))
6621 if (copy_to_user(optval, &val, len))
6627 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6628 char __user *optval, int __user *optlen)
6630 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6631 struct sctp_authchunks __user *p = (void __user *)optval;
6632 struct sctp_authchunks val;
6633 struct sctp_association *asoc;
6634 struct sctp_chunks_param *ch;
6638 if (!ep->auth_enable)
6641 if (len < sizeof(struct sctp_authchunks))
6644 if (copy_from_user(&val, optval, sizeof(val)))
6647 to = p->gauth_chunks;
6648 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6652 ch = asoc->peer.peer_chunks;
6656 /* See if the user provided enough room for all the data */
6657 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6658 if (len < num_chunks)
6661 if (copy_to_user(to, ch->chunks, num_chunks))
6664 len = sizeof(struct sctp_authchunks) + num_chunks;
6665 if (put_user(len, optlen))
6667 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6672 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6673 char __user *optval, int __user *optlen)
6675 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6676 struct sctp_authchunks __user *p = (void __user *)optval;
6677 struct sctp_authchunks val;
6678 struct sctp_association *asoc;
6679 struct sctp_chunks_param *ch;
6683 if (!ep->auth_enable)
6686 if (len < sizeof(struct sctp_authchunks))
6689 if (copy_from_user(&val, optval, sizeof(val)))
6692 to = p->gauth_chunks;
6693 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6694 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6698 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6700 ch = ep->auth_chunk_list;
6705 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6706 if (len < sizeof(struct sctp_authchunks) + num_chunks)
6709 if (copy_to_user(to, ch->chunks, num_chunks))
6712 len = sizeof(struct sctp_authchunks) + num_chunks;
6713 if (put_user(len, optlen))
6715 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6722 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6723 * This option gets the current number of associations that are attached
6724 * to a one-to-many style socket. The option value is an uint32_t.
6726 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6727 char __user *optval, int __user *optlen)
6729 struct sctp_sock *sp = sctp_sk(sk);
6730 struct sctp_association *asoc;
6733 if (sctp_style(sk, TCP))
6736 if (len < sizeof(u32))
6741 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6745 if (put_user(len, optlen))
6747 if (copy_to_user(optval, &val, len))
6754 * 8.1.23 SCTP_AUTO_ASCONF
6755 * See the corresponding setsockopt entry as description
6757 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6758 char __user *optval, int __user *optlen)
6762 if (len < sizeof(int))
6766 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6768 if (put_user(len, optlen))
6770 if (copy_to_user(optval, &val, len))
6776 * 8.2.6. Get the Current Identifiers of Associations
6777 * (SCTP_GET_ASSOC_ID_LIST)
6779 * This option gets the current list of SCTP association identifiers of
6780 * the SCTP associations handled by a one-to-many style socket.
6782 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6783 char __user *optval, int __user *optlen)
6785 struct sctp_sock *sp = sctp_sk(sk);
6786 struct sctp_association *asoc;
6787 struct sctp_assoc_ids *ids;
6790 if (sctp_style(sk, TCP))
6793 if (len < sizeof(struct sctp_assoc_ids))
6796 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6800 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6803 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6805 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
6809 ids->gaids_number_of_ids = num;
6811 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6812 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6815 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6825 * SCTP_PEER_ADDR_THLDS
6827 * This option allows us to fetch the partially failed threshold for one or all
6828 * transports in an association. See Section 6.1 of:
6829 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6831 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6832 char __user *optval,
6836 struct sctp_paddrthlds val;
6837 struct sctp_transport *trans;
6838 struct sctp_association *asoc;
6840 if (len < sizeof(struct sctp_paddrthlds))
6842 len = sizeof(struct sctp_paddrthlds);
6843 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6846 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6847 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6851 val.spt_pathpfthld = asoc->pf_retrans;
6852 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6854 trans = sctp_addr_id2transport(sk, &val.spt_address,
6859 val.spt_pathmaxrxt = trans->pathmaxrxt;
6860 val.spt_pathpfthld = trans->pf_retrans;
6863 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6870 * SCTP_GET_ASSOC_STATS
6872 * This option retrieves local per endpoint statistics. It is modeled
6873 * after OpenSolaris' implementation
6875 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6876 char __user *optval,
6879 struct sctp_assoc_stats sas;
6880 struct sctp_association *asoc = NULL;
6882 /* User must provide at least the assoc id */
6883 if (len < sizeof(sctp_assoc_t))
6886 /* Allow the struct to grow and fill in as much as possible */
6887 len = min_t(size_t, len, sizeof(sas));
6889 if (copy_from_user(&sas, optval, len))
6892 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6896 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6897 sas.sas_gapcnt = asoc->stats.gapcnt;
6898 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6899 sas.sas_osacks = asoc->stats.osacks;
6900 sas.sas_isacks = asoc->stats.isacks;
6901 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6902 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6903 sas.sas_oodchunks = asoc->stats.oodchunks;
6904 sas.sas_iodchunks = asoc->stats.iodchunks;
6905 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6906 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6907 sas.sas_idupchunks = asoc->stats.idupchunks;
6908 sas.sas_opackets = asoc->stats.opackets;
6909 sas.sas_ipackets = asoc->stats.ipackets;
6911 /* New high max rto observed, will return 0 if not a single
6912 * RTO update took place. obs_rto_ipaddr will be bogus
6915 sas.sas_maxrto = asoc->stats.max_obs_rto;
6916 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6917 sizeof(struct sockaddr_storage));
6919 /* Mark beginning of a new observation period */
6920 asoc->stats.max_obs_rto = asoc->rto_min;
6922 if (put_user(len, optlen))
6925 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
6927 if (copy_to_user(optval, &sas, len))
6933 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6934 char __user *optval,
6939 if (len < sizeof(int))
6943 if (sctp_sk(sk)->recvrcvinfo)
6945 if (put_user(len, optlen))
6947 if (copy_to_user(optval, &val, len))
6953 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6954 char __user *optval,
6959 if (len < sizeof(int))
6963 if (sctp_sk(sk)->recvnxtinfo)
6965 if (put_user(len, optlen))
6967 if (copy_to_user(optval, &val, len))
6973 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6974 char __user *optval,
6977 struct sctp_assoc_value params;
6978 struct sctp_association *asoc;
6979 int retval = -EFAULT;
6981 if (len < sizeof(params)) {
6986 len = sizeof(params);
6987 if (copy_from_user(¶ms, optval, len))
6990 asoc = sctp_id2assoc(sk, params.assoc_id);
6992 params.assoc_value = asoc->prsctp_enable;
6993 } else if (!params.assoc_id) {
6994 struct sctp_sock *sp = sctp_sk(sk);
6996 params.assoc_value = sp->ep->prsctp_enable;
7002 if (put_user(len, optlen))
7005 if (copy_to_user(optval, ¶ms, len))
7014 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7015 char __user *optval,
7018 struct sctp_default_prinfo info;
7019 struct sctp_association *asoc;
7020 int retval = -EFAULT;
7022 if (len < sizeof(info)) {
7028 if (copy_from_user(&info, optval, len))
7031 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7033 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7034 info.pr_value = asoc->default_timetolive;
7035 } else if (!info.pr_assoc_id) {
7036 struct sctp_sock *sp = sctp_sk(sk);
7038 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7039 info.pr_value = sp->default_timetolive;
7045 if (put_user(len, optlen))
7048 if (copy_to_user(optval, &info, len))
7057 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7058 char __user *optval,
7061 struct sctp_prstatus params;
7062 struct sctp_association *asoc;
7064 int retval = -EINVAL;
7066 if (len < sizeof(params))
7069 len = sizeof(params);
7070 if (copy_from_user(¶ms, optval, len)) {
7075 policy = params.sprstat_policy;
7076 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7077 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7080 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7084 if (policy == SCTP_PR_SCTP_ALL) {
7085 params.sprstat_abandoned_unsent = 0;
7086 params.sprstat_abandoned_sent = 0;
7087 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7088 params.sprstat_abandoned_unsent +=
7089 asoc->abandoned_unsent[policy];
7090 params.sprstat_abandoned_sent +=
7091 asoc->abandoned_sent[policy];
7094 params.sprstat_abandoned_unsent =
7095 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7096 params.sprstat_abandoned_sent =
7097 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7100 if (put_user(len, optlen)) {
7105 if (copy_to_user(optval, ¶ms, len)) {
7116 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7117 char __user *optval,
7120 struct sctp_stream_out_ext *streamoute;
7121 struct sctp_association *asoc;
7122 struct sctp_prstatus params;
7123 int retval = -EINVAL;
7126 if (len < sizeof(params))
7129 len = sizeof(params);
7130 if (copy_from_user(¶ms, optval, len)) {
7135 policy = params.sprstat_policy;
7136 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7137 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7140 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7141 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7144 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7146 /* Not allocated yet, means all stats are 0 */
7147 params.sprstat_abandoned_unsent = 0;
7148 params.sprstat_abandoned_sent = 0;
7153 if (policy == SCTP_PR_SCTP_ALL) {
7154 params.sprstat_abandoned_unsent = 0;
7155 params.sprstat_abandoned_sent = 0;
7156 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7157 params.sprstat_abandoned_unsent +=
7158 streamoute->abandoned_unsent[policy];
7159 params.sprstat_abandoned_sent +=
7160 streamoute->abandoned_sent[policy];
7163 params.sprstat_abandoned_unsent =
7164 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7165 params.sprstat_abandoned_sent =
7166 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7169 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7180 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7181 char __user *optval,
7184 struct sctp_assoc_value params;
7185 struct sctp_association *asoc;
7186 int retval = -EFAULT;
7188 if (len < sizeof(params)) {
7193 len = sizeof(params);
7194 if (copy_from_user(¶ms, optval, len))
7197 asoc = sctp_id2assoc(sk, params.assoc_id);
7199 params.assoc_value = asoc->reconf_enable;
7200 } else if (!params.assoc_id) {
7201 struct sctp_sock *sp = sctp_sk(sk);
7203 params.assoc_value = sp->ep->reconf_enable;
7209 if (put_user(len, optlen))
7212 if (copy_to_user(optval, ¶ms, len))
7221 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7222 char __user *optval,
7225 struct sctp_assoc_value params;
7226 struct sctp_association *asoc;
7227 int retval = -EFAULT;
7229 if (len < sizeof(params)) {
7234 len = sizeof(params);
7235 if (copy_from_user(¶ms, optval, len))
7238 asoc = sctp_id2assoc(sk, params.assoc_id);
7240 params.assoc_value = asoc->strreset_enable;
7241 } else if (!params.assoc_id) {
7242 struct sctp_sock *sp = sctp_sk(sk);
7244 params.assoc_value = sp->ep->strreset_enable;
7250 if (put_user(len, optlen))
7253 if (copy_to_user(optval, ¶ms, len))
7262 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7263 char __user *optval,
7266 struct sctp_assoc_value params;
7267 struct sctp_association *asoc;
7268 int retval = -EFAULT;
7270 if (len < sizeof(params)) {
7275 len = sizeof(params);
7276 if (copy_from_user(¶ms, optval, len))
7279 asoc = sctp_id2assoc(sk, params.assoc_id);
7285 params.assoc_value = sctp_sched_get_sched(asoc);
7287 if (put_user(len, optlen))
7290 if (copy_to_user(optval, ¶ms, len))
7299 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7300 char __user *optval,
7303 struct sctp_stream_value params;
7304 struct sctp_association *asoc;
7305 int retval = -EFAULT;
7307 if (len < sizeof(params)) {
7312 len = sizeof(params);
7313 if (copy_from_user(¶ms, optval, len))
7316 asoc = sctp_id2assoc(sk, params.assoc_id);
7322 retval = sctp_sched_get_value(asoc, params.stream_id,
7323 ¶ms.stream_value);
7327 if (put_user(len, optlen)) {
7332 if (copy_to_user(optval, ¶ms, len)) {
7341 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7342 char __user *optval,
7345 struct sctp_assoc_value params;
7346 struct sctp_association *asoc;
7347 int retval = -EFAULT;
7349 if (len < sizeof(params)) {
7354 len = sizeof(params);
7355 if (copy_from_user(¶ms, optval, len))
7358 asoc = sctp_id2assoc(sk, params.assoc_id);
7360 params.assoc_value = asoc->intl_enable;
7361 } else if (!params.assoc_id) {
7362 struct sctp_sock *sp = sctp_sk(sk);
7364 params.assoc_value = sp->strm_interleave;
7370 if (put_user(len, optlen))
7373 if (copy_to_user(optval, ¶ms, len))
7382 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7383 char __user *optval,
7388 if (len < sizeof(int))
7392 val = sctp_sk(sk)->reuse;
7393 if (put_user(len, optlen))
7396 if (copy_to_user(optval, &val, len))
7402 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7403 char __user *optval, int __user *optlen)
7408 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7410 /* I can hardly begin to describe how wrong this is. This is
7411 * so broken as to be worse than useless. The API draft
7412 * REALLY is NOT helpful here... I am not convinced that the
7413 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7414 * are at all well-founded.
7416 if (level != SOL_SCTP) {
7417 struct sctp_af *af = sctp_sk(sk)->pf->af;
7419 retval = af->getsockopt(sk, level, optname, optval, optlen);
7423 if (get_user(len, optlen))
7433 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7435 case SCTP_DISABLE_FRAGMENTS:
7436 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7440 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7442 case SCTP_AUTOCLOSE:
7443 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7445 case SCTP_SOCKOPT_PEELOFF:
7446 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7448 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7449 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7451 case SCTP_PEER_ADDR_PARAMS:
7452 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7455 case SCTP_DELAYED_SACK:
7456 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7460 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7462 case SCTP_GET_PEER_ADDRS:
7463 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7466 case SCTP_GET_LOCAL_ADDRS:
7467 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7470 case SCTP_SOCKOPT_CONNECTX3:
7471 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7473 case SCTP_DEFAULT_SEND_PARAM:
7474 retval = sctp_getsockopt_default_send_param(sk, len,
7477 case SCTP_DEFAULT_SNDINFO:
7478 retval = sctp_getsockopt_default_sndinfo(sk, len,
7481 case SCTP_PRIMARY_ADDR:
7482 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7485 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7488 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7490 case SCTP_ASSOCINFO:
7491 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7493 case SCTP_I_WANT_MAPPED_V4_ADDR:
7494 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7497 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7499 case SCTP_GET_PEER_ADDR_INFO:
7500 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7503 case SCTP_ADAPTATION_LAYER:
7504 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7508 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7510 case SCTP_FRAGMENT_INTERLEAVE:
7511 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7514 case SCTP_PARTIAL_DELIVERY_POINT:
7515 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7518 case SCTP_MAX_BURST:
7519 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7522 case SCTP_AUTH_CHUNK:
7523 case SCTP_AUTH_DELETE_KEY:
7524 case SCTP_AUTH_DEACTIVATE_KEY:
7525 retval = -EOPNOTSUPP;
7527 case SCTP_HMAC_IDENT:
7528 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7530 case SCTP_AUTH_ACTIVE_KEY:
7531 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7533 case SCTP_PEER_AUTH_CHUNKS:
7534 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7537 case SCTP_LOCAL_AUTH_CHUNKS:
7538 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7541 case SCTP_GET_ASSOC_NUMBER:
7542 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7544 case SCTP_GET_ASSOC_ID_LIST:
7545 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7547 case SCTP_AUTO_ASCONF:
7548 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7550 case SCTP_PEER_ADDR_THLDS:
7551 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7553 case SCTP_GET_ASSOC_STATS:
7554 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7556 case SCTP_RECVRCVINFO:
7557 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7559 case SCTP_RECVNXTINFO:
7560 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7562 case SCTP_PR_SUPPORTED:
7563 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7565 case SCTP_DEFAULT_PRINFO:
7566 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7569 case SCTP_PR_ASSOC_STATUS:
7570 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7573 case SCTP_PR_STREAM_STATUS:
7574 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7577 case SCTP_RECONFIG_SUPPORTED:
7578 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7581 case SCTP_ENABLE_STREAM_RESET:
7582 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7585 case SCTP_STREAM_SCHEDULER:
7586 retval = sctp_getsockopt_scheduler(sk, len, optval,
7589 case SCTP_STREAM_SCHEDULER_VALUE:
7590 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7593 case SCTP_INTERLEAVING_SUPPORTED:
7594 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7597 case SCTP_REUSE_PORT:
7598 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7601 retval = -ENOPROTOOPT;
7609 static int sctp_hash(struct sock *sk)
7615 static void sctp_unhash(struct sock *sk)
7620 /* Check if port is acceptable. Possibly find first available port.
7622 * The port hash table (contained in the 'global' SCTP protocol storage
7623 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7624 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7625 * list (the list number is the port number hashed out, so as you
7626 * would expect from a hash function, all the ports in a given list have
7627 * such a number that hashes out to the same list number; you were
7628 * expecting that, right?); so each list has a set of ports, with a
7629 * link to the socket (struct sock) that uses it, the port number and
7630 * a fastreuse flag (FIXME: NPI ipg).
7632 static struct sctp_bind_bucket *sctp_bucket_create(
7633 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
7635 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7637 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
7638 struct sctp_bind_hashbucket *head; /* hash list */
7639 struct sctp_bind_bucket *pp;
7640 unsigned short snum;
7643 snum = ntohs(addr->v4.sin_port);
7645 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7648 /* Search for an available port. */
7649 int low, high, remaining, index;
7651 struct net *net = sock_net(sk);
7653 inet_get_local_port_range(net, &low, &high);
7654 remaining = (high - low) + 1;
7655 rover = prandom_u32() % remaining + low;
7659 if ((rover < low) || (rover > high))
7661 if (inet_is_local_reserved_port(net, rover))
7663 index = sctp_phashfn(sock_net(sk), rover);
7664 head = &sctp_port_hashtable[index];
7665 spin_lock_bh(&head->lock);
7666 sctp_for_each_hentry(pp, &head->chain)
7667 if ((pp->port == rover) &&
7668 net_eq(sock_net(sk), pp->net))
7672 spin_unlock_bh(&head->lock);
7674 } while (--remaining > 0);
7676 /* Exhausted local port range during search? */
7681 /* OK, here is the one we will use. HEAD (the port
7682 * hash table list entry) is non-NULL and we hold it's
7687 /* We are given an specific port number; we verify
7688 * that it is not being used. If it is used, we will
7689 * exahust the search in the hash list corresponding
7690 * to the port number (snum) - we detect that with the
7691 * port iterator, pp being NULL.
7693 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
7694 spin_lock_bh(&head->lock);
7695 sctp_for_each_hentry(pp, &head->chain) {
7696 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
7703 if (!hlist_empty(&pp->owner)) {
7704 /* We had a port hash table hit - there is an
7705 * available port (pp != NULL) and it is being
7706 * used by other socket (pp->owner not empty); that other
7707 * socket is going to be sk2.
7711 pr_debug("%s: found a possible match\n", __func__);
7713 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
7716 /* Run through the list of sockets bound to the port
7717 * (pp->port) [via the pointers bind_next and
7718 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7719 * we get the endpoint they describe and run through
7720 * the endpoint's list of IP (v4 or v6) addresses,
7721 * comparing each of the addresses with the address of
7722 * the socket sk. If we find a match, then that means
7723 * that this port/socket (sk) combination are already
7726 sk_for_each_bound(sk2, &pp->owner) {
7727 struct sctp_endpoint *ep2;
7728 ep2 = sctp_sk(sk2)->ep;
7731 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
7732 sk2->sk_state != SCTP_SS_LISTENING))
7735 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7736 sctp_sk(sk2), sctp_sk(sk))) {
7742 pr_debug("%s: found a match\n", __func__);
7745 /* If there was a hash table miss, create a new port. */
7747 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
7750 /* In either case (hit or miss), make sure fastreuse is 1 only
7751 * if sk->sk_reuse is too (that is, if the caller requested
7752 * SO_REUSEADDR on this socket -sk-).
7754 if (hlist_empty(&pp->owner)) {
7755 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
7759 } else if (pp->fastreuse &&
7760 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
7763 /* We are set, so fill up all the data in the hash table
7764 * entry, tie the socket list information with the rest of the
7765 * sockets FIXME: Blurry, NPI (ipg).
7768 if (!sctp_sk(sk)->bind_hash) {
7769 inet_sk(sk)->inet_num = snum;
7770 sk_add_bind_node(sk, &pp->owner);
7771 sctp_sk(sk)->bind_hash = pp;
7776 spin_unlock_bh(&head->lock);
7780 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7781 * port is requested.
7783 static int sctp_get_port(struct sock *sk, unsigned short snum)
7785 union sctp_addr addr;
7786 struct sctp_af *af = sctp_sk(sk)->pf->af;
7788 /* Set up a dummy address struct from the sk. */
7789 af->from_sk(&addr, sk);
7790 addr.v4.sin_port = htons(snum);
7792 /* Note: sk->sk_num gets filled in if ephemeral port request. */
7793 return !!sctp_get_port_local(sk, &addr);
7797 * Move a socket to LISTENING state.
7799 static int sctp_listen_start(struct sock *sk, int backlog)
7801 struct sctp_sock *sp = sctp_sk(sk);
7802 struct sctp_endpoint *ep = sp->ep;
7803 struct crypto_shash *tfm = NULL;
7806 /* Allocate HMAC for generating cookie. */
7807 if (!sp->hmac && sp->sctp_hmac_alg) {
7808 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
7809 tfm = crypto_alloc_shash(alg, 0, 0);
7811 net_info_ratelimited("failed to load transform for %s: %ld\n",
7812 sp->sctp_hmac_alg, PTR_ERR(tfm));
7815 sctp_sk(sk)->hmac = tfm;
7819 * If a bind() or sctp_bindx() is not called prior to a listen()
7820 * call that allows new associations to be accepted, the system
7821 * picks an ephemeral port and will choose an address set equivalent
7822 * to binding with a wildcard address.
7824 * This is not currently spelled out in the SCTP sockets
7825 * extensions draft, but follows the practice as seen in TCP
7829 inet_sk_set_state(sk, SCTP_SS_LISTENING);
7830 if (!ep->base.bind_addr.port) {
7831 if (sctp_autobind(sk))
7834 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
7835 inet_sk_set_state(sk, SCTP_SS_CLOSED);
7840 sk->sk_max_ack_backlog = backlog;
7841 sctp_hash_endpoint(ep);
7846 * 4.1.3 / 5.1.3 listen()
7848 * By default, new associations are not accepted for UDP style sockets.
7849 * An application uses listen() to mark a socket as being able to
7850 * accept new associations.
7852 * On TCP style sockets, applications use listen() to ready the SCTP
7853 * endpoint for accepting inbound associations.
7855 * On both types of endpoints a backlog of '0' disables listening.
7857 * Move a socket to LISTENING state.
7859 int sctp_inet_listen(struct socket *sock, int backlog)
7861 struct sock *sk = sock->sk;
7862 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7865 if (unlikely(backlog < 0))
7870 /* Peeled-off sockets are not allowed to listen(). */
7871 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7874 if (sock->state != SS_UNCONNECTED)
7877 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7880 /* If backlog is zero, disable listening. */
7882 if (sctp_sstate(sk, CLOSED))
7886 sctp_unhash_endpoint(ep);
7887 sk->sk_state = SCTP_SS_CLOSED;
7888 if (sk->sk_reuse || sctp_sk(sk)->reuse)
7889 sctp_sk(sk)->bind_hash->fastreuse = 1;
7893 /* If we are already listening, just update the backlog */
7894 if (sctp_sstate(sk, LISTENING))
7895 sk->sk_max_ack_backlog = backlog;
7897 err = sctp_listen_start(sk, backlog);
7909 * This function is done by modeling the current datagram_poll() and the
7910 * tcp_poll(). Note that, based on these implementations, we don't
7911 * lock the socket in this function, even though it seems that,
7912 * ideally, locking or some other mechanisms can be used to ensure
7913 * the integrity of the counters (sndbuf and wmem_alloc) used
7914 * in this place. We assume that we don't need locks either until proven
7917 * Another thing to note is that we include the Async I/O support
7918 * here, again, by modeling the current TCP/UDP code. We don't have
7919 * a good way to test with it yet.
7921 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7923 struct sock *sk = sock->sk;
7924 struct sctp_sock *sp = sctp_sk(sk);
7927 poll_wait(file, sk_sleep(sk), wait);
7929 sock_rps_record_flow(sk);
7931 /* A TCP-style listening socket becomes readable when the accept queue
7934 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7935 return (!list_empty(&sp->ep->asocs)) ?
7936 (EPOLLIN | EPOLLRDNORM) : 0;
7940 /* Is there any exceptional events? */
7941 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
7943 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
7944 if (sk->sk_shutdown & RCV_SHUTDOWN)
7945 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
7946 if (sk->sk_shutdown == SHUTDOWN_MASK)
7949 /* Is it readable? Reconsider this code with TCP-style support. */
7950 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
7951 mask |= EPOLLIN | EPOLLRDNORM;
7953 /* The association is either gone or not ready. */
7954 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7957 /* Is it writable? */
7958 if (sctp_writeable(sk)) {
7959 mask |= EPOLLOUT | EPOLLWRNORM;
7961 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
7963 * Since the socket is not locked, the buffer
7964 * might be made available after the writeable check and
7965 * before the bit is set. This could cause a lost I/O
7966 * signal. tcp_poll() has a race breaker for this race
7967 * condition. Based on their implementation, we put
7968 * in the following code to cover it as well.
7970 if (sctp_writeable(sk))
7971 mask |= EPOLLOUT | EPOLLWRNORM;
7976 /********************************************************************
7977 * 2nd Level Abstractions
7978 ********************************************************************/
7980 static struct sctp_bind_bucket *sctp_bucket_create(
7981 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
7983 struct sctp_bind_bucket *pp;
7985 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
7987 SCTP_DBG_OBJCNT_INC(bind_bucket);
7990 INIT_HLIST_HEAD(&pp->owner);
7992 hlist_add_head(&pp->node, &head->chain);
7997 /* Caller must hold hashbucket lock for this tb with local BH disabled */
7998 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8000 if (pp && hlist_empty(&pp->owner)) {
8001 __hlist_del(&pp->node);
8002 kmem_cache_free(sctp_bucket_cachep, pp);
8003 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8007 /* Release this socket's reference to a local port. */
8008 static inline void __sctp_put_port(struct sock *sk)
8010 struct sctp_bind_hashbucket *head =
8011 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8012 inet_sk(sk)->inet_num)];
8013 struct sctp_bind_bucket *pp;
8015 spin_lock(&head->lock);
8016 pp = sctp_sk(sk)->bind_hash;
8017 __sk_del_bind_node(sk);
8018 sctp_sk(sk)->bind_hash = NULL;
8019 inet_sk(sk)->inet_num = 0;
8020 sctp_bucket_destroy(pp);
8021 spin_unlock(&head->lock);
8024 void sctp_put_port(struct sock *sk)
8027 __sctp_put_port(sk);
8032 * The system picks an ephemeral port and choose an address set equivalent
8033 * to binding with a wildcard address.
8034 * One of those addresses will be the primary address for the association.
8035 * This automatically enables the multihoming capability of SCTP.
8037 static int sctp_autobind(struct sock *sk)
8039 union sctp_addr autoaddr;
8043 /* Initialize a local sockaddr structure to INADDR_ANY. */
8044 af = sctp_sk(sk)->pf->af;
8046 port = htons(inet_sk(sk)->inet_num);
8047 af->inaddr_any(&autoaddr, port);
8049 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8052 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8055 * 4.2 The cmsghdr Structure *
8057 * When ancillary data is sent or received, any number of ancillary data
8058 * objects can be specified by the msg_control and msg_controllen members of
8059 * the msghdr structure, because each object is preceded by
8060 * a cmsghdr structure defining the object's length (the cmsg_len member).
8061 * Historically Berkeley-derived implementations have passed only one object
8062 * at a time, but this API allows multiple objects to be
8063 * passed in a single call to sendmsg() or recvmsg(). The following example
8064 * shows two ancillary data objects in a control buffer.
8066 * |<--------------------------- msg_controllen -------------------------->|
8069 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8071 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8074 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8076 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8079 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8080 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8082 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8084 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8091 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8093 struct msghdr *my_msg = (struct msghdr *)msg;
8094 struct cmsghdr *cmsg;
8096 for_each_cmsghdr(cmsg, my_msg) {
8097 if (!CMSG_OK(my_msg, cmsg))
8100 /* Should we parse this header or ignore? */
8101 if (cmsg->cmsg_level != IPPROTO_SCTP)
8104 /* Strictly check lengths following example in SCM code. */
8105 switch (cmsg->cmsg_type) {
8107 /* SCTP Socket API Extension
8108 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8110 * This cmsghdr structure provides information for
8111 * initializing new SCTP associations with sendmsg().
8112 * The SCTP_INITMSG socket option uses this same data
8113 * structure. This structure is not used for
8116 * cmsg_level cmsg_type cmsg_data[]
8117 * ------------ ------------ ----------------------
8118 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8120 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8123 cmsgs->init = CMSG_DATA(cmsg);
8127 /* SCTP Socket API Extension
8128 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8130 * This cmsghdr structure specifies SCTP options for
8131 * sendmsg() and describes SCTP header information
8132 * about a received message through recvmsg().
8134 * cmsg_level cmsg_type cmsg_data[]
8135 * ------------ ------------ ----------------------
8136 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8138 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8141 cmsgs->srinfo = CMSG_DATA(cmsg);
8143 if (cmsgs->srinfo->sinfo_flags &
8144 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8145 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8146 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8151 /* SCTP Socket API Extension
8152 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8154 * This cmsghdr structure specifies SCTP options for
8155 * sendmsg(). This structure and SCTP_RCVINFO replaces
8156 * SCTP_SNDRCV which has been deprecated.
8158 * cmsg_level cmsg_type cmsg_data[]
8159 * ------------ ------------ ---------------------
8160 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8162 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8165 cmsgs->sinfo = CMSG_DATA(cmsg);
8167 if (cmsgs->sinfo->snd_flags &
8168 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8169 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8170 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8174 /* SCTP Socket API Extension
8175 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8177 * This cmsghdr structure specifies SCTP options for sendmsg().
8179 * cmsg_level cmsg_type cmsg_data[]
8180 * ------------ ------------ ---------------------
8181 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8183 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8186 cmsgs->prinfo = CMSG_DATA(cmsg);
8187 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8190 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8191 cmsgs->prinfo->pr_value = 0;
8194 /* SCTP Socket API Extension
8195 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8197 * This cmsghdr structure specifies SCTP options for sendmsg().
8199 * cmsg_level cmsg_type cmsg_data[]
8200 * ------------ ------------ ---------------------
8201 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8203 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8206 cmsgs->authinfo = CMSG_DATA(cmsg);
8208 case SCTP_DSTADDRV4:
8209 case SCTP_DSTADDRV6:
8210 /* SCTP Socket API Extension
8211 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8213 * This cmsghdr structure specifies SCTP options for sendmsg().
8215 * cmsg_level cmsg_type cmsg_data[]
8216 * ------------ ------------ ---------------------
8217 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8218 * ------------ ------------ ---------------------
8219 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8221 cmsgs->addrs_msg = my_msg;
8232 * Wait for a packet..
8233 * Note: This function is the same function as in core/datagram.c
8234 * with a few modifications to make lksctp work.
8236 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8241 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8243 /* Socket errors? */
8244 error = sock_error(sk);
8248 if (!skb_queue_empty(&sk->sk_receive_queue))
8251 /* Socket shut down? */
8252 if (sk->sk_shutdown & RCV_SHUTDOWN)
8255 /* Sequenced packets can come disconnected. If so we report the
8260 /* Is there a good reason to think that we may receive some data? */
8261 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8264 /* Handle signals. */
8265 if (signal_pending(current))
8268 /* Let another process have a go. Since we are going to sleep
8269 * anyway. Note: This may cause odd behaviors if the message
8270 * does not fit in the user's buffer, but this seems to be the
8271 * only way to honor MSG_DONTWAIT realistically.
8274 *timeo_p = schedule_timeout(*timeo_p);
8278 finish_wait(sk_sleep(sk), &wait);
8282 error = sock_intr_errno(*timeo_p);
8285 finish_wait(sk_sleep(sk), &wait);
8290 /* Receive a datagram.
8291 * Note: This is pretty much the same routine as in core/datagram.c
8292 * with a few changes to make lksctp work.
8294 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8295 int noblock, int *err)
8298 struct sk_buff *skb;
8301 timeo = sock_rcvtimeo(sk, noblock);
8303 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8304 MAX_SCHEDULE_TIMEOUT);
8307 /* Again only user level code calls this function,
8308 * so nothing interrupt level
8309 * will suddenly eat the receive_queue.
8311 * Look at current nfs client by the way...
8312 * However, this function was correct in any case. 8)
8314 if (flags & MSG_PEEK) {
8315 skb = skb_peek(&sk->sk_receive_queue);
8317 refcount_inc(&skb->users);
8319 skb = __skb_dequeue(&sk->sk_receive_queue);
8325 /* Caller is allowed not to check sk->sk_err before calling. */
8326 error = sock_error(sk);
8330 if (sk->sk_shutdown & RCV_SHUTDOWN)
8333 if (sk_can_busy_loop(sk)) {
8334 sk_busy_loop(sk, noblock);
8336 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8340 /* User doesn't want to wait. */
8344 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8353 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8354 static void __sctp_write_space(struct sctp_association *asoc)
8356 struct sock *sk = asoc->base.sk;
8358 if (sctp_wspace(asoc) <= 0)
8361 if (waitqueue_active(&asoc->wait))
8362 wake_up_interruptible(&asoc->wait);
8364 if (sctp_writeable(sk)) {
8365 struct socket_wq *wq;
8368 wq = rcu_dereference(sk->sk_wq);
8370 if (waitqueue_active(&wq->wait))
8371 wake_up_interruptible(&wq->wait);
8373 /* Note that we try to include the Async I/O support
8374 * here by modeling from the current TCP/UDP code.
8375 * We have not tested with it yet.
8377 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8378 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8384 static void sctp_wake_up_waiters(struct sock *sk,
8385 struct sctp_association *asoc)
8387 struct sctp_association *tmp = asoc;
8389 /* We do accounting for the sndbuf space per association,
8390 * so we only need to wake our own association.
8392 if (asoc->ep->sndbuf_policy)
8393 return __sctp_write_space(asoc);
8395 /* If association goes down and is just flushing its
8396 * outq, then just normally notify others.
8398 if (asoc->base.dead)
8399 return sctp_write_space(sk);
8401 /* Accounting for the sndbuf space is per socket, so we
8402 * need to wake up others, try to be fair and in case of
8403 * other associations, let them have a go first instead
8404 * of just doing a sctp_write_space() call.
8406 * Note that we reach sctp_wake_up_waiters() only when
8407 * associations free up queued chunks, thus we are under
8408 * lock and the list of associations on a socket is
8409 * guaranteed not to change.
8411 for (tmp = list_next_entry(tmp, asocs); 1;
8412 tmp = list_next_entry(tmp, asocs)) {
8413 /* Manually skip the head element. */
8414 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8416 /* Wake up association. */
8417 __sctp_write_space(tmp);
8418 /* We've reached the end. */
8424 /* Do accounting for the sndbuf space.
8425 * Decrement the used sndbuf space of the corresponding association by the
8426 * data size which was just transmitted(freed).
8428 static void sctp_wfree(struct sk_buff *skb)
8430 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8431 struct sctp_association *asoc = chunk->asoc;
8432 struct sock *sk = asoc->base.sk;
8434 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8435 sizeof(struct sk_buff) +
8436 sizeof(struct sctp_chunk);
8438 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
8441 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
8443 sk->sk_wmem_queued -= skb->truesize;
8444 sk_mem_uncharge(sk, skb->truesize);
8447 struct sctp_shared_key *shkey = chunk->shkey;
8449 /* refcnt == 2 and !list_empty mean after this release, it's
8450 * not being used anywhere, and it's time to notify userland
8451 * that this shkey can be freed if it's been deactivated.
8453 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8454 refcount_read(&shkey->refcnt) == 2) {
8455 struct sctp_ulpevent *ev;
8457 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8461 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8463 sctp_auth_shkey_release(chunk->shkey);
8467 sctp_wake_up_waiters(sk, asoc);
8469 sctp_association_put(asoc);
8472 /* Do accounting for the receive space on the socket.
8473 * Accounting for the association is done in ulpevent.c
8474 * We set this as a destructor for the cloned data skbs so that
8475 * accounting is done at the correct time.
8477 void sctp_sock_rfree(struct sk_buff *skb)
8479 struct sock *sk = skb->sk;
8480 struct sctp_ulpevent *event = sctp_skb2event(skb);
8482 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8485 * Mimic the behavior of sock_rfree
8487 sk_mem_uncharge(sk, event->rmem_len);
8491 /* Helper function to wait for space in the sndbuf. */
8492 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8495 struct sock *sk = asoc->base.sk;
8496 long current_timeo = *timeo_p;
8500 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8503 /* Increment the association's refcnt. */
8504 sctp_association_hold(asoc);
8506 /* Wait on the association specific sndbuf space. */
8508 prepare_to_wait_exclusive(&asoc->wait, &wait,
8509 TASK_INTERRUPTIBLE);
8510 if (asoc->base.dead)
8514 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8516 if (signal_pending(current))
8517 goto do_interrupted;
8518 if (sk_under_memory_pressure(sk))
8520 if ((int)msg_len <= sctp_wspace(asoc) &&
8521 sk_wmem_schedule(sk, msg_len))
8524 /* Let another process have a go. Since we are going
8528 current_timeo = schedule_timeout(current_timeo);
8530 if (sk != asoc->base.sk)
8533 *timeo_p = current_timeo;
8537 finish_wait(&asoc->wait, &wait);
8539 /* Release the association's refcnt. */
8540 sctp_association_put(asoc);
8553 err = sock_intr_errno(*timeo_p);
8561 void sctp_data_ready(struct sock *sk)
8563 struct socket_wq *wq;
8566 wq = rcu_dereference(sk->sk_wq);
8567 if (skwq_has_sleeper(wq))
8568 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8569 EPOLLRDNORM | EPOLLRDBAND);
8570 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8574 /* If socket sndbuf has changed, wake up all per association waiters. */
8575 void sctp_write_space(struct sock *sk)
8577 struct sctp_association *asoc;
8579 /* Wake up the tasks in each wait queue. */
8580 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8581 __sctp_write_space(asoc);
8585 /* Is there any sndbuf space available on the socket?
8587 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8588 * associations on the same socket. For a UDP-style socket with
8589 * multiple associations, it is possible for it to be "unwriteable"
8590 * prematurely. I assume that this is acceptable because
8591 * a premature "unwriteable" is better than an accidental "writeable" which
8592 * would cause an unwanted block under certain circumstances. For the 1-1
8593 * UDP-style sockets or TCP-style sockets, this code should work.
8596 static bool sctp_writeable(struct sock *sk)
8598 return sk->sk_sndbuf > sk->sk_wmem_queued;
8601 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8602 * returns immediately with EINPROGRESS.
8604 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8606 struct sock *sk = asoc->base.sk;
8608 long current_timeo = *timeo_p;
8611 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8613 /* Increment the association's refcnt. */
8614 sctp_association_hold(asoc);
8617 prepare_to_wait_exclusive(&asoc->wait, &wait,
8618 TASK_INTERRUPTIBLE);
8621 if (sk->sk_shutdown & RCV_SHUTDOWN)
8623 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8626 if (signal_pending(current))
8627 goto do_interrupted;
8629 if (sctp_state(asoc, ESTABLISHED))
8632 /* Let another process have a go. Since we are going
8636 current_timeo = schedule_timeout(current_timeo);
8639 *timeo_p = current_timeo;
8643 finish_wait(&asoc->wait, &wait);
8645 /* Release the association's refcnt. */
8646 sctp_association_put(asoc);
8651 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
8654 err = -ECONNREFUSED;
8658 err = sock_intr_errno(*timeo_p);
8666 static int sctp_wait_for_accept(struct sock *sk, long timeo)
8668 struct sctp_endpoint *ep;
8672 ep = sctp_sk(sk)->ep;
8676 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
8677 TASK_INTERRUPTIBLE);
8679 if (list_empty(&ep->asocs)) {
8681 timeo = schedule_timeout(timeo);
8686 if (!sctp_sstate(sk, LISTENING))
8690 if (!list_empty(&ep->asocs))
8693 err = sock_intr_errno(timeo);
8694 if (signal_pending(current))
8702 finish_wait(sk_sleep(sk), &wait);
8707 static void sctp_wait_for_close(struct sock *sk, long timeout)
8712 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8713 if (list_empty(&sctp_sk(sk)->ep->asocs))
8716 timeout = schedule_timeout(timeout);
8718 } while (!signal_pending(current) && timeout);
8720 finish_wait(sk_sleep(sk), &wait);
8723 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8725 struct sk_buff *frag;
8730 /* Don't forget the fragments. */
8731 skb_walk_frags(skb, frag)
8732 sctp_skb_set_owner_r_frag(frag, sk);
8735 sctp_skb_set_owner_r(skb, sk);
8738 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8739 struct sctp_association *asoc)
8741 struct inet_sock *inet = inet_sk(sk);
8742 struct inet_sock *newinet;
8743 struct sctp_sock *sp = sctp_sk(sk);
8744 struct sctp_endpoint *ep = sp->ep;
8746 newsk->sk_type = sk->sk_type;
8747 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8748 newsk->sk_flags = sk->sk_flags;
8749 newsk->sk_tsflags = sk->sk_tsflags;
8750 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8751 newsk->sk_no_check_rx = sk->sk_no_check_rx;
8752 newsk->sk_reuse = sk->sk_reuse;
8753 sctp_sk(newsk)->reuse = sp->reuse;
8755 newsk->sk_shutdown = sk->sk_shutdown;
8756 newsk->sk_destruct = sctp_destruct_sock;
8757 newsk->sk_family = sk->sk_family;
8758 newsk->sk_protocol = IPPROTO_SCTP;
8759 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8760 newsk->sk_sndbuf = sk->sk_sndbuf;
8761 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8762 newsk->sk_lingertime = sk->sk_lingertime;
8763 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8764 newsk->sk_sndtimeo = sk->sk_sndtimeo;
8765 newsk->sk_rxhash = sk->sk_rxhash;
8767 newinet = inet_sk(newsk);
8769 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8770 * getsockname() and getpeername()
8772 newinet->inet_sport = inet->inet_sport;
8773 newinet->inet_saddr = inet->inet_saddr;
8774 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8775 newinet->inet_dport = htons(asoc->peer.port);
8776 newinet->pmtudisc = inet->pmtudisc;
8777 newinet->inet_id = prandom_u32();
8779 newinet->uc_ttl = inet->uc_ttl;
8780 newinet->mc_loop = 1;
8781 newinet->mc_ttl = 1;
8782 newinet->mc_index = 0;
8783 newinet->mc_list = NULL;
8785 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8786 net_enable_timestamp();
8788 /* Set newsk security attributes from orginal sk and connection
8789 * security attribute from ep.
8791 security_sctp_sk_clone(ep, sk, newsk);
8794 static inline void sctp_copy_descendant(struct sock *sk_to,
8795 const struct sock *sk_from)
8797 int ancestor_size = sizeof(struct inet_sock) +
8798 sizeof(struct sctp_sock) -
8799 offsetof(struct sctp_sock, auto_asconf_list);
8801 if (sk_from->sk_family == PF_INET6)
8802 ancestor_size += sizeof(struct ipv6_pinfo);
8804 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8807 /* Populate the fields of the newsk from the oldsk and migrate the assoc
8808 * and its messages to the newsk.
8810 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8811 struct sctp_association *assoc,
8812 enum sctp_socket_type type)
8814 struct sctp_sock *oldsp = sctp_sk(oldsk);
8815 struct sctp_sock *newsp = sctp_sk(newsk);
8816 struct sctp_bind_bucket *pp; /* hash list port iterator */
8817 struct sctp_endpoint *newep = newsp->ep;
8818 struct sk_buff *skb, *tmp;
8819 struct sctp_ulpevent *event;
8820 struct sctp_bind_hashbucket *head;
8822 /* Migrate socket buffer sizes and all the socket level options to the
8825 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8826 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8827 /* Brute force copy old sctp opt. */
8828 sctp_copy_descendant(newsk, oldsk);
8830 /* Restore the ep value that was overwritten with the above structure
8836 /* Hook this new socket in to the bind_hash list. */
8837 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8838 inet_sk(oldsk)->inet_num)];
8839 spin_lock_bh(&head->lock);
8840 pp = sctp_sk(oldsk)->bind_hash;
8841 sk_add_bind_node(newsk, &pp->owner);
8842 sctp_sk(newsk)->bind_hash = pp;
8843 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
8844 spin_unlock_bh(&head->lock);
8846 /* Copy the bind_addr list from the original endpoint to the new
8847 * endpoint so that we can handle restarts properly
8849 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8850 &oldsp->ep->base.bind_addr, GFP_KERNEL);
8852 sctp_auto_asconf_init(newsp);
8854 /* Move any messages in the old socket's receive queue that are for the
8855 * peeled off association to the new socket's receive queue.
8857 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8858 event = sctp_skb2event(skb);
8859 if (event->asoc == assoc) {
8860 __skb_unlink(skb, &oldsk->sk_receive_queue);
8861 __skb_queue_tail(&newsk->sk_receive_queue, skb);
8862 sctp_skb_set_owner_r_frag(skb, newsk);
8866 /* Clean up any messages pending delivery due to partial
8867 * delivery. Three cases:
8868 * 1) No partial deliver; no work.
8869 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8870 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8872 skb_queue_head_init(&newsp->pd_lobby);
8873 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
8875 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
8876 struct sk_buff_head *queue;
8878 /* Decide which queue to move pd_lobby skbs to. */
8879 if (assoc->ulpq.pd_mode) {
8880 queue = &newsp->pd_lobby;
8882 queue = &newsk->sk_receive_queue;
8884 /* Walk through the pd_lobby, looking for skbs that
8885 * need moved to the new socket.
8887 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8888 event = sctp_skb2event(skb);
8889 if (event->asoc == assoc) {
8890 __skb_unlink(skb, &oldsp->pd_lobby);
8891 __skb_queue_tail(queue, skb);
8892 sctp_skb_set_owner_r_frag(skb, newsk);
8896 /* Clear up any skbs waiting for the partial
8897 * delivery to finish.
8899 if (assoc->ulpq.pd_mode)
8900 sctp_clear_pd(oldsk, NULL);
8904 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
8906 /* Set the type of socket to indicate that it is peeled off from the
8907 * original UDP-style socket or created with the accept() call on a
8908 * TCP-style socket..
8912 /* Mark the new socket "in-use" by the user so that any packets
8913 * that may arrive on the association after we've moved it are
8914 * queued to the backlog. This prevents a potential race between
8915 * backlog processing on the old socket and new-packet processing
8916 * on the new socket.
8918 * The caller has just allocated newsk so we can guarantee that other
8919 * paths won't try to lock it and then oldsk.
8921 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
8922 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
8923 sctp_assoc_migrate(assoc, newsk);
8924 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
8926 /* If the association on the newsk is already closed before accept()
8927 * is called, set RCV_SHUTDOWN flag.
8929 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
8930 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
8931 newsk->sk_shutdown |= RCV_SHUTDOWN;
8933 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
8936 release_sock(newsk);
8940 /* This proto struct describes the ULP interface for SCTP. */
8941 struct proto sctp_prot = {
8943 .owner = THIS_MODULE,
8944 .close = sctp_close,
8945 .disconnect = sctp_disconnect,
8946 .accept = sctp_accept,
8947 .ioctl = sctp_ioctl,
8948 .init = sctp_init_sock,
8949 .destroy = sctp_destroy_sock,
8950 .shutdown = sctp_shutdown,
8951 .setsockopt = sctp_setsockopt,
8952 .getsockopt = sctp_getsockopt,
8953 .sendmsg = sctp_sendmsg,
8954 .recvmsg = sctp_recvmsg,
8956 .backlog_rcv = sctp_backlog_rcv,
8958 .unhash = sctp_unhash,
8959 .no_autobind = true,
8960 .obj_size = sizeof(struct sctp_sock),
8961 .useroffset = offsetof(struct sctp_sock, subscribe),
8962 .usersize = offsetof(struct sctp_sock, initmsg) -
8963 offsetof(struct sctp_sock, subscribe) +
8964 sizeof_field(struct sctp_sock, initmsg),
8965 .sysctl_mem = sysctl_sctp_mem,
8966 .sysctl_rmem = sysctl_sctp_rmem,
8967 .sysctl_wmem = sysctl_sctp_wmem,
8968 .memory_pressure = &sctp_memory_pressure,
8969 .enter_memory_pressure = sctp_enter_memory_pressure,
8970 .memory_allocated = &sctp_memory_allocated,
8971 .sockets_allocated = &sctp_sockets_allocated,
8974 #if IS_ENABLED(CONFIG_IPV6)
8976 #include <net/transp_v6.h>
8977 static void sctp_v6_destroy_sock(struct sock *sk)
8979 sctp_destroy_sock(sk);
8980 inet6_destroy_sock(sk);
8983 struct proto sctpv6_prot = {
8985 .owner = THIS_MODULE,
8986 .close = sctp_close,
8987 .disconnect = sctp_disconnect,
8988 .accept = sctp_accept,
8989 .ioctl = sctp_ioctl,
8990 .init = sctp_init_sock,
8991 .destroy = sctp_v6_destroy_sock,
8992 .shutdown = sctp_shutdown,
8993 .setsockopt = sctp_setsockopt,
8994 .getsockopt = sctp_getsockopt,
8995 .sendmsg = sctp_sendmsg,
8996 .recvmsg = sctp_recvmsg,
8998 .backlog_rcv = sctp_backlog_rcv,
9000 .unhash = sctp_unhash,
9001 .no_autobind = true,
9002 .obj_size = sizeof(struct sctp6_sock),
9003 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9004 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9005 offsetof(struct sctp6_sock, sctp.subscribe) +
9006 sizeof_field(struct sctp6_sock, sctp.initmsg),
9007 .sysctl_mem = sysctl_sctp_mem,
9008 .sysctl_rmem = sysctl_sctp_rmem,
9009 .sysctl_wmem = sysctl_sctp_wmem,
9010 .memory_pressure = &sctp_memory_pressure,
9011 .enter_memory_pressure = sctp_enter_memory_pressure,
9012 .memory_allocated = &sctp_memory_allocated,
9013 .sockets_allocated = &sctp_sockets_allocated,
9015 #endif /* IS_ENABLED(CONFIG_IPV6) */