1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
7 * Copyright (c) 2001-2002 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
10 * This file is part of the SCTP kernel implementation
12 * These functions interface with the sockets layer to implement the
13 * SCTP Extensions for the Sockets API.
15 * Note that the descriptions from the specification are USER level
16 * functions--this file is the functions which populate the struct proto
17 * for SCTP which is the BOTTOM of the sockets interface.
19 * Please send any bug reports or fixes you make to the
21 * lksctp developers <linux-sctp@vger.kernel.org>
23 * Written or modified by:
24 * La Monte H.P. Yarroll <piggy@acm.org>
25 * Narasimha Budihal <narsi@refcode.org>
26 * Karl Knutson <karl@athena.chicago.il.us>
27 * Jon Grimm <jgrimm@us.ibm.com>
28 * Xingang Guo <xingang.guo@intel.com>
29 * Daisy Chang <daisyc@us.ibm.com>
30 * Sridhar Samudrala <samudrala@us.ibm.com>
31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32 * Ardelle Fan <ardelle.fan@intel.com>
33 * Ryan Layer <rmlayer@us.ibm.com>
34 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35 * Kevin Gao <kevin.gao@intel.com>
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <crypto/hash.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/time.h>
45 #include <linux/sched/signal.h>
47 #include <linux/capability.h>
48 #include <linux/fcntl.h>
49 #include <linux/poll.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/file.h>
53 #include <linux/compat.h>
54 #include <linux/rhashtable.h>
58 #include <net/route.h>
60 #include <net/inet_common.h>
61 #include <net/busy_poll.h>
63 #include <linux/socket.h> /* for sa_family_t */
64 #include <linux/export.h>
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
68 #include <net/sctp/stream_sched.h>
70 /* Forward declarations for internal helper functions. */
71 static bool sctp_writeable(struct sock *sk);
72 static void sctp_wfree(struct sk_buff *skb);
73 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
75 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
76 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77 static int sctp_wait_for_accept(struct sock *sk, long timeo);
78 static void sctp_wait_for_close(struct sock *sk, long timeo);
79 static void sctp_destruct_sock(struct sock *sk);
80 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81 union sctp_addr *addr, int len);
82 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86 static int sctp_send_asconf(struct sctp_association *asoc,
87 struct sctp_chunk *chunk);
88 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89 static int sctp_autobind(struct sock *sk);
90 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91 struct sctp_association *assoc,
92 enum sctp_socket_type type);
94 static unsigned long sctp_memory_pressure;
95 static atomic_long_t sctp_memory_allocated;
96 struct percpu_counter sctp_sockets_allocated;
98 static void sctp_enter_memory_pressure(struct sock *sk)
100 sctp_memory_pressure = 1;
104 /* Get the sndbuf space available at the time on the association. */
105 static inline int sctp_wspace(struct sctp_association *asoc)
107 struct sock *sk = asoc->base.sk;
109 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110 : sk_stream_wspace(sk);
113 /* Increment the used sndbuf space count of the corresponding association by
114 * the size of the outgoing data chunk.
115 * Also, set the skb destructor for sndbuf accounting later.
117 * Since it is always 1-1 between chunk and skb, and also a new skb is always
118 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119 * destructor in the data chunk skb for the purpose of the sndbuf space
122 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
124 struct sctp_association *asoc = chunk->asoc;
125 struct sock *sk = asoc->base.sk;
127 /* The sndbuf space is tracked per association. */
128 sctp_association_hold(asoc);
131 sctp_auth_shkey_hold(chunk->shkey);
133 skb_set_owner_w(chunk->skb, sk);
135 chunk->skb->destructor = sctp_wfree;
136 /* Save the chunk pointer in skb for sctp_wfree to use later. */
137 skb_shinfo(chunk->skb)->destructor_arg = chunk;
139 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
140 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
142 sk_mem_charge(sk, chunk->skb->truesize);
145 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
147 skb_orphan(chunk->skb);
150 #define traverse_and_process() \
153 if (msg == prev_msg) \
155 list_for_each_entry(c, &msg->chunks, frag_list) { \
156 if ((clear && asoc->base.sk == c->skb->sk) || \
157 (!clear && asoc->base.sk != c->skb->sk)) \
163 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
165 void (*cb)(struct sctp_chunk *))
168 struct sctp_datamsg *msg, *prev_msg = NULL;
169 struct sctp_outq *q = &asoc->outqueue;
170 struct sctp_chunk *chunk, *c;
171 struct sctp_transport *t;
173 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
175 traverse_and_process();
177 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
178 traverse_and_process();
180 list_for_each_entry(chunk, &q->sacked, transmitted_list)
181 traverse_and_process();
183 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
184 traverse_and_process();
186 list_for_each_entry(chunk, &q->out_chunk_list, list)
187 traverse_and_process();
190 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191 void (*cb)(struct sk_buff *, struct sock *))
194 struct sk_buff *skb, *tmp;
196 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
199 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
206 /* Verify that this is a valid address. */
207 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
212 /* Verify basic sockaddr. */
213 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
217 /* Is this a valid SCTP address? */
218 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
221 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
227 /* Look up the association by its id. If this is not a UDP-style
228 * socket, the ID field is always ignored.
230 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
232 struct sctp_association *asoc = NULL;
234 /* If this is not a UDP-style socket, assoc id should be ignored. */
235 if (!sctp_style(sk, UDP)) {
236 /* Return NULL if the socket state is not ESTABLISHED. It
237 * could be a TCP-style listening socket or a socket which
238 * hasn't yet called connect() to establish an association.
240 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
243 /* Get the first and the only association from the list. */
244 if (!list_empty(&sctp_sk(sk)->ep->asocs))
245 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246 struct sctp_association, asocs);
250 /* Otherwise this is a UDP-style socket. */
251 if (id <= SCTP_ALL_ASSOC)
254 spin_lock_bh(&sctp_assocs_id_lock);
255 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
256 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
258 spin_unlock_bh(&sctp_assocs_id_lock);
263 /* Look up the transport from an address and an assoc id. If both address and
264 * id are specified, the associations matching the address and the id should be
267 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
268 struct sockaddr_storage *addr,
271 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
272 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
273 union sctp_addr *laddr = (union sctp_addr *)addr;
274 struct sctp_transport *transport;
276 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
279 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
286 id_asoc = sctp_id2assoc(sk, id);
287 if (id_asoc && (id_asoc != addr_asoc))
290 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
291 (union sctp_addr *)addr);
296 /* API 3.1.2 bind() - UDP Style Syntax
297 * The syntax of bind() is,
299 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
301 * sd - the socket descriptor returned by socket().
302 * addr - the address structure (struct sockaddr_in or struct
303 * sockaddr_in6 [RFC 2553]),
304 * addr_len - the size of the address structure.
306 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
312 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
315 /* Disallow binding twice. */
316 if (!sctp_sk(sk)->ep->base.bind_addr.port)
317 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
327 static int sctp_get_port_local(struct sock *, union sctp_addr *);
329 /* Verify this is a valid sockaddr. */
330 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
331 union sctp_addr *addr, int len)
335 /* Check minimum size. */
336 if (len < sizeof (struct sockaddr))
339 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
342 if (addr->sa.sa_family == AF_INET6) {
343 if (len < SIN6_LEN_RFC2133)
345 /* V4 mapped address are really of AF_INET family */
346 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
347 !opt->pf->af_supported(AF_INET, opt))
351 /* If we get this far, af is valid. */
352 af = sctp_get_af_specific(addr->sa.sa_family);
354 if (len < af->sockaddr_len)
360 static void sctp_auto_asconf_init(struct sctp_sock *sp)
362 struct net *net = sock_net(&sp->inet.sk);
364 if (net->sctp.default_auto_asconf) {
365 spin_lock(&net->sctp.addr_wq_lock);
366 list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
367 spin_unlock(&net->sctp.addr_wq_lock);
368 sp->do_auto_asconf = 1;
372 /* Bind a local address either to an endpoint or to an association. */
373 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
375 struct net *net = sock_net(sk);
376 struct sctp_sock *sp = sctp_sk(sk);
377 struct sctp_endpoint *ep = sp->ep;
378 struct sctp_bind_addr *bp = &ep->base.bind_addr;
383 /* Common sockaddr verification. */
384 af = sctp_sockaddr_af(sp, addr, len);
386 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387 __func__, sk, addr, len);
391 snum = ntohs(addr->v4.sin_port);
393 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394 __func__, sk, &addr->sa, bp->port, snum, len);
396 /* PF specific bind() address verification. */
397 if (!sp->pf->bind_verify(sp, addr))
398 return -EADDRNOTAVAIL;
400 /* We must either be unbound, or bind to the same port.
401 * It's OK to allow 0 ports if we are already bound.
402 * We'll just inhert an already bound port in this case
407 else if (snum != bp->port) {
408 pr_debug("%s: new port %d doesn't match existing port "
409 "%d\n", __func__, snum, bp->port);
414 if (snum && snum < inet_prot_sock(net) &&
415 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
418 /* See if the address matches any of the addresses we may have
419 * already bound before checking against other endpoints.
421 if (sctp_bind_addr_match(bp, addr, sp))
424 /* Make sure we are allowed to bind here.
425 * The function sctp_get_port_local() does duplicate address
428 addr->v4.sin_port = htons(snum);
429 if (sctp_get_port_local(sk, addr))
432 /* Refresh ephemeral port. */
434 bp->port = inet_sk(sk)->inet_num;
435 sctp_auto_asconf_init(sp);
438 /* Add the address to the bind address list.
439 * Use GFP_ATOMIC since BHs will be disabled.
441 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
442 SCTP_ADDR_SRC, GFP_ATOMIC);
448 /* Copy back into socket for getsockname() use. */
449 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
450 sp->pf->to_sk_saddr(addr, sk);
455 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
457 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
458 * at any one time. If a sender, after sending an ASCONF chunk, decides
459 * it needs to transfer another ASCONF Chunk, it MUST wait until the
460 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
461 * subsequent ASCONF. Note this restriction binds each side, so at any
462 * time two ASCONF may be in-transit on any given association (one sent
463 * from each endpoint).
465 static int sctp_send_asconf(struct sctp_association *asoc,
466 struct sctp_chunk *chunk)
468 struct net *net = sock_net(asoc->base.sk);
471 /* If there is an outstanding ASCONF chunk, queue it for later
474 if (asoc->addip_last_asconf) {
475 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
479 /* Hold the chunk until an ASCONF_ACK is received. */
480 sctp_chunk_hold(chunk);
481 retval = sctp_primitive_ASCONF(net, asoc, chunk);
483 sctp_chunk_free(chunk);
485 asoc->addip_last_asconf = chunk;
491 /* Add a list of addresses as bind addresses to local endpoint or
494 * Basically run through each address specified in the addrs/addrcnt
495 * array/length pair, determine if it is IPv6 or IPv4 and call
496 * sctp_do_bind() on it.
498 * If any of them fails, then the operation will be reversed and the
499 * ones that were added will be removed.
501 * Only sctp_setsockopt_bindx() is supposed to call this function.
503 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
508 struct sockaddr *sa_addr;
511 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
515 for (cnt = 0; cnt < addrcnt; cnt++) {
516 /* The list may contain either IPv4 or IPv6 address;
517 * determine the address length for walking thru the list.
520 af = sctp_get_af_specific(sa_addr->sa_family);
526 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
529 addr_buf += af->sockaddr_len;
533 /* Failed. Cleanup the ones that have been added */
535 sctp_bindx_rem(sk, addrs, cnt);
543 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
544 * associations that are part of the endpoint indicating that a list of local
545 * addresses are added to the endpoint.
547 * If any of the addresses is already in the bind address list of the
548 * association, we do not send the chunk for that association. But it will not
549 * affect other associations.
551 * Only sctp_setsockopt_bindx() is supposed to call this function.
553 static int sctp_send_asconf_add_ip(struct sock *sk,
554 struct sockaddr *addrs,
557 struct sctp_sock *sp;
558 struct sctp_endpoint *ep;
559 struct sctp_association *asoc;
560 struct sctp_bind_addr *bp;
561 struct sctp_chunk *chunk;
562 struct sctp_sockaddr_entry *laddr;
563 union sctp_addr *addr;
564 union sctp_addr saveaddr;
574 if (!ep->asconf_enable)
577 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
578 __func__, sk, addrs, addrcnt);
580 list_for_each_entry(asoc, &ep->asocs, asocs) {
581 if (!asoc->peer.asconf_capable)
584 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
587 if (!sctp_state(asoc, ESTABLISHED))
590 /* Check if any address in the packed array of addresses is
591 * in the bind address list of the association. If so,
592 * do not send the asconf chunk to its peer, but continue with
593 * other associations.
596 for (i = 0; i < addrcnt; i++) {
598 af = sctp_get_af_specific(addr->v4.sin_family);
604 if (sctp_assoc_lookup_laddr(asoc, addr))
607 addr_buf += af->sockaddr_len;
612 /* Use the first valid address in bind addr list of
613 * association as Address Parameter of ASCONF CHUNK.
615 bp = &asoc->base.bind_addr;
616 p = bp->address_list.next;
617 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
618 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
619 addrcnt, SCTP_PARAM_ADD_IP);
625 /* Add the new addresses to the bind address list with
626 * use_as_src set to 0.
629 for (i = 0; i < addrcnt; i++) {
631 af = sctp_get_af_specific(addr->v4.sin_family);
632 memcpy(&saveaddr, addr, af->sockaddr_len);
633 retval = sctp_add_bind_addr(bp, &saveaddr,
635 SCTP_ADDR_NEW, GFP_ATOMIC);
636 addr_buf += af->sockaddr_len;
638 if (asoc->src_out_of_asoc_ok) {
639 struct sctp_transport *trans;
641 list_for_each_entry(trans,
642 &asoc->peer.transport_addr_list, transports) {
643 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
644 2*asoc->pathmtu, 4380));
645 trans->ssthresh = asoc->peer.i.a_rwnd;
646 trans->rto = asoc->rto_initial;
647 sctp_max_rto(asoc, trans);
648 trans->rtt = trans->srtt = trans->rttvar = 0;
649 /* Clear the source and route cache */
650 sctp_transport_route(trans, NULL,
651 sctp_sk(asoc->base.sk));
654 retval = sctp_send_asconf(asoc, chunk);
661 /* Remove a list of addresses from bind addresses list. Do not remove the
664 * Basically run through each address specified in the addrs/addrcnt
665 * array/length pair, determine if it is IPv6 or IPv4 and call
666 * sctp_del_bind() on it.
668 * If any of them fails, then the operation will be reversed and the
669 * ones that were removed will be added back.
671 * At least one address has to be left; if only one address is
672 * available, the operation will return -EBUSY.
674 * Only sctp_setsockopt_bindx() is supposed to call this function.
676 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
678 struct sctp_sock *sp = sctp_sk(sk);
679 struct sctp_endpoint *ep = sp->ep;
681 struct sctp_bind_addr *bp = &ep->base.bind_addr;
684 union sctp_addr *sa_addr;
687 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
688 __func__, sk, addrs, addrcnt);
691 for (cnt = 0; cnt < addrcnt; cnt++) {
692 /* If the bind address list is empty or if there is only one
693 * bind address, there is nothing more to be removed (we need
694 * at least one address here).
696 if (list_empty(&bp->address_list) ||
697 (sctp_list_single_entry(&bp->address_list))) {
703 af = sctp_get_af_specific(sa_addr->sa.sa_family);
709 if (!af->addr_valid(sa_addr, sp, NULL)) {
710 retval = -EADDRNOTAVAIL;
714 if (sa_addr->v4.sin_port &&
715 sa_addr->v4.sin_port != htons(bp->port)) {
720 if (!sa_addr->v4.sin_port)
721 sa_addr->v4.sin_port = htons(bp->port);
723 /* FIXME - There is probably a need to check if sk->sk_saddr and
724 * sk->sk_rcv_addr are currently set to one of the addresses to
725 * be removed. This is something which needs to be looked into
726 * when we are fixing the outstanding issues with multi-homing
727 * socket routing and failover schemes. Refer to comments in
728 * sctp_do_bind(). -daisy
730 retval = sctp_del_bind_addr(bp, sa_addr);
732 addr_buf += af->sockaddr_len;
735 /* Failed. Add the ones that has been removed back */
737 sctp_bindx_add(sk, addrs, cnt);
745 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
746 * the associations that are part of the endpoint indicating that a list of
747 * local addresses are removed from the endpoint.
749 * If any of the addresses is already in the bind address list of the
750 * association, we do not send the chunk for that association. But it will not
751 * affect other associations.
753 * Only sctp_setsockopt_bindx() is supposed to call this function.
755 static int sctp_send_asconf_del_ip(struct sock *sk,
756 struct sockaddr *addrs,
759 struct sctp_sock *sp;
760 struct sctp_endpoint *ep;
761 struct sctp_association *asoc;
762 struct sctp_transport *transport;
763 struct sctp_bind_addr *bp;
764 struct sctp_chunk *chunk;
765 union sctp_addr *laddr;
768 struct sctp_sockaddr_entry *saddr;
777 if (!ep->asconf_enable)
780 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
781 __func__, sk, addrs, addrcnt);
783 list_for_each_entry(asoc, &ep->asocs, asocs) {
785 if (!asoc->peer.asconf_capable)
788 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
791 if (!sctp_state(asoc, ESTABLISHED))
794 /* Check if any address in the packed array of addresses is
795 * not present in the bind address list of the association.
796 * If so, do not send the asconf chunk to its peer, but
797 * continue with other associations.
800 for (i = 0; i < addrcnt; i++) {
802 af = sctp_get_af_specific(laddr->v4.sin_family);
808 if (!sctp_assoc_lookup_laddr(asoc, laddr))
811 addr_buf += af->sockaddr_len;
816 /* Find one address in the association's bind address list
817 * that is not in the packed array of addresses. This is to
818 * make sure that we do not delete all the addresses in the
821 bp = &asoc->base.bind_addr;
822 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
824 if ((laddr == NULL) && (addrcnt == 1)) {
825 if (asoc->asconf_addr_del_pending)
827 asoc->asconf_addr_del_pending =
828 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
829 if (asoc->asconf_addr_del_pending == NULL) {
833 asoc->asconf_addr_del_pending->sa.sa_family =
835 asoc->asconf_addr_del_pending->v4.sin_port =
837 if (addrs->sa_family == AF_INET) {
838 struct sockaddr_in *sin;
840 sin = (struct sockaddr_in *)addrs;
841 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
842 } else if (addrs->sa_family == AF_INET6) {
843 struct sockaddr_in6 *sin6;
845 sin6 = (struct sockaddr_in6 *)addrs;
846 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
849 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
850 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
851 asoc->asconf_addr_del_pending);
853 asoc->src_out_of_asoc_ok = 1;
861 /* We do not need RCU protection throughout this loop
862 * because this is done under a socket lock from the
865 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
873 /* Reset use_as_src flag for the addresses in the bind address
874 * list that are to be deleted.
877 for (i = 0; i < addrcnt; i++) {
879 af = sctp_get_af_specific(laddr->v4.sin_family);
880 list_for_each_entry(saddr, &bp->address_list, list) {
881 if (sctp_cmp_addr_exact(&saddr->a, laddr))
882 saddr->state = SCTP_ADDR_DEL;
884 addr_buf += af->sockaddr_len;
887 /* Update the route and saddr entries for all the transports
888 * as some of the addresses in the bind address list are
889 * about to be deleted and cannot be used as source addresses.
891 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
893 sctp_transport_route(transport, NULL,
894 sctp_sk(asoc->base.sk));
898 /* We don't need to transmit ASCONF */
900 retval = sctp_send_asconf(asoc, chunk);
906 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
907 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
909 struct sock *sk = sctp_opt2sk(sp);
910 union sctp_addr *addr;
913 /* It is safe to write port space in caller. */
915 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
916 af = sctp_get_af_specific(addr->sa.sa_family);
919 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
922 if (addrw->state == SCTP_ADDR_NEW)
923 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
925 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
928 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
931 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
934 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
935 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
938 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
939 * Section 3.1.2 for this usage.
941 * addrs is a pointer to an array of one or more socket addresses. Each
942 * address is contained in its appropriate structure (i.e. struct
943 * sockaddr_in or struct sockaddr_in6) the family of the address type
944 * must be used to distinguish the address length (note that this
945 * representation is termed a "packed array" of addresses). The caller
946 * specifies the number of addresses in the array with addrcnt.
948 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
949 * -1, and sets errno to the appropriate error code.
951 * For SCTP, the port given in each socket address must be the same, or
952 * sctp_bindx() will fail, setting errno to EINVAL.
954 * The flags parameter is formed from the bitwise OR of zero or more of
955 * the following currently defined flags:
957 * SCTP_BINDX_ADD_ADDR
959 * SCTP_BINDX_REM_ADDR
961 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
962 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
963 * addresses from the association. The two flags are mutually exclusive;
964 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
965 * not remove all addresses from an association; sctp_bindx() will
966 * reject such an attempt with EINVAL.
968 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
969 * additional addresses with an endpoint after calling bind(). Or use
970 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
971 * socket is associated with so that no new association accepted will be
972 * associated with those addresses. If the endpoint supports dynamic
973 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
974 * endpoint to send the appropriate message to the peer to change the
975 * peers address lists.
977 * Adding and removing addresses from a connected association is
978 * optional functionality. Implementations that do not support this
979 * functionality should return EOPNOTSUPP.
981 * Basically do nothing but copying the addresses from user to kernel
982 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
983 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
986 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
989 * sk The sk of the socket
990 * addrs The pointer to the addresses in user land
991 * addrssize Size of the addrs buffer
992 * op Operation to perform (add or remove, see the flags of
995 * Returns 0 if ok, <0 errno code on error.
997 static int sctp_setsockopt_bindx(struct sock *sk,
998 struct sockaddr __user *addrs,
999 int addrs_size, int op)
1001 struct sockaddr *kaddrs;
1005 struct sockaddr *sa_addr;
1009 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1010 __func__, sk, addrs, addrs_size, op);
1012 if (unlikely(addrs_size <= 0))
1015 kaddrs = memdup_user(addrs, addrs_size);
1017 return PTR_ERR(kaddrs);
1019 /* Walk through the addrs buffer and count the number of addresses. */
1021 while (walk_size < addrs_size) {
1022 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1028 af = sctp_get_af_specific(sa_addr->sa_family);
1030 /* If the address family is not supported or if this address
1031 * causes the address buffer to overflow return EINVAL.
1033 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1038 addr_buf += af->sockaddr_len;
1039 walk_size += af->sockaddr_len;
1044 case SCTP_BINDX_ADD_ADDR:
1045 /* Allow security module to validate bindx addresses. */
1046 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1047 (struct sockaddr *)kaddrs,
1051 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1054 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1057 case SCTP_BINDX_REM_ADDR:
1058 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1061 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1075 static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
1076 const union sctp_addr *daddr,
1077 const struct sctp_initmsg *init,
1078 struct sctp_transport **tp)
1080 struct sctp_association *asoc;
1081 struct sock *sk = ep->base.sk;
1082 struct net *net = sock_net(sk);
1083 enum sctp_scope scope;
1086 if (sctp_endpoint_is_peeled_off(ep, daddr))
1087 return -EADDRNOTAVAIL;
1089 if (!ep->base.bind_addr.port) {
1090 if (sctp_autobind(sk))
1093 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1094 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1098 scope = sctp_scope(daddr);
1099 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1103 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1107 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1116 if (init->sinit_num_ostreams) {
1117 __u16 outcnt = init->sinit_num_ostreams;
1119 asoc->c.sinit_num_ostreams = outcnt;
1120 /* outcnt has been changed, need to re-init stream */
1121 err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
1126 if (init->sinit_max_instreams)
1127 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1129 if (init->sinit_max_attempts)
1130 asoc->max_init_attempts = init->sinit_max_attempts;
1132 if (init->sinit_max_init_timeo)
1133 asoc->max_init_timeo =
1134 msecs_to_jiffies(init->sinit_max_init_timeo);
1138 sctp_association_free(asoc);
1142 static int sctp_connect_add_peer(struct sctp_association *asoc,
1143 union sctp_addr *daddr, int addr_len)
1145 struct sctp_endpoint *ep = asoc->ep;
1146 struct sctp_association *old;
1147 struct sctp_transport *t;
1150 err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
1154 old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
1155 if (old && old != asoc)
1156 return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1159 if (sctp_endpoint_is_peeled_off(ep, daddr))
1160 return -EADDRNOTAVAIL;
1162 t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1169 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1171 * Common routine for handling connect() and sctp_connectx().
1172 * Connect will come in with just a single address.
1174 static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
1175 int addrs_size, int flags, sctp_assoc_t *assoc_id)
1177 struct sctp_sock *sp = sctp_sk(sk);
1178 struct sctp_endpoint *ep = sp->ep;
1179 struct sctp_transport *transport;
1180 struct sctp_association *asoc;
1181 void *addr_buf = kaddrs;
1182 union sctp_addr *daddr;
1187 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1188 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
1192 af = sctp_get_af_specific(daddr->sa.sa_family);
1193 if (!af || af->sockaddr_len > addrs_size)
1196 err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
1200 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1202 return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1205 err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
1208 asoc = transport->asoc;
1210 addr_buf += af->sockaddr_len;
1211 walk_size = af->sockaddr_len;
1212 while (walk_size < addrs_size) {
1214 if (walk_size + sizeof(sa_family_t) > addrs_size)
1218 af = sctp_get_af_specific(daddr->sa.sa_family);
1219 if (!af || af->sockaddr_len + walk_size > addrs_size)
1222 if (asoc->peer.port != ntohs(daddr->v4.sin_port))
1225 err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
1229 addr_buf += af->sockaddr_len;
1230 walk_size += af->sockaddr_len;
1233 /* In case the user of sctp_connectx() wants an association
1234 * id back, assign one now.
1237 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1242 err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
1246 /* Initialize sk's dport and daddr for getpeername() */
1247 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1248 sp->pf->to_sk_daddr(daddr, sk);
1252 *assoc_id = asoc->assoc_id;
1254 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1255 return sctp_wait_for_connect(asoc, &timeo);
1258 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1259 __func__, asoc, kaddrs, err);
1260 sctp_association_free(asoc);
1264 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1267 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1268 * sctp_assoc_t *asoc);
1270 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1271 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1272 * or IPv6 addresses.
1274 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1275 * Section 3.1.2 for this usage.
1277 * addrs is a pointer to an array of one or more socket addresses. Each
1278 * address is contained in its appropriate structure (i.e. struct
1279 * sockaddr_in or struct sockaddr_in6) the family of the address type
1280 * must be used to distengish the address length (note that this
1281 * representation is termed a "packed array" of addresses). The caller
1282 * specifies the number of addresses in the array with addrcnt.
1284 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1285 * the association id of the new association. On failure, sctp_connectx()
1286 * returns -1, and sets errno to the appropriate error code. The assoc_id
1287 * is not touched by the kernel.
1289 * For SCTP, the port given in each socket address must be the same, or
1290 * sctp_connectx() will fail, setting errno to EINVAL.
1292 * An application can use sctp_connectx to initiate an association with
1293 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1294 * allows a caller to specify multiple addresses at which a peer can be
1295 * reached. The way the SCTP stack uses the list of addresses to set up
1296 * the association is implementation dependent. This function only
1297 * specifies that the stack will try to make use of all the addresses in
1298 * the list when needed.
1300 * Note that the list of addresses passed in is only used for setting up
1301 * the association. It does not necessarily equal the set of addresses
1302 * the peer uses for the resulting association. If the caller wants to
1303 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1304 * retrieve them after the association has been set up.
1306 * Basically do nothing but copying the addresses from user to kernel
1307 * land and invoking either sctp_connectx(). This is used for tunneling
1308 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1310 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1313 * sk The sk of the socket
1314 * addrs The pointer to the addresses in user land
1315 * addrssize Size of the addrs buffer
1317 * Returns >=0 if ok, <0 errno code on error.
1319 static int __sctp_setsockopt_connectx(struct sock *sk,
1320 struct sockaddr __user *addrs,
1322 sctp_assoc_t *assoc_id)
1324 struct sockaddr *kaddrs;
1325 int err = 0, flags = 0;
1327 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1328 __func__, sk, addrs, addrs_size);
1330 /* make sure the 1st addr's sa_family is accessible later */
1331 if (unlikely(addrs_size < sizeof(sa_family_t)))
1334 kaddrs = memdup_user(addrs, addrs_size);
1336 return PTR_ERR(kaddrs) == -EFAULT ? -EINVAL : PTR_ERR(kaddrs);
1338 /* Allow security module to validate connectx addresses. */
1339 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1340 (struct sockaddr *)kaddrs,
1345 /* in-kernel sockets don't generally have a file allocated to them
1346 * if all they do is call sock_create_kern().
1348 if (sk->sk_socket->file)
1349 flags = sk->sk_socket->file->f_flags;
1351 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1360 * This is an older interface. It's kept for backward compatibility
1361 * to the option that doesn't provide association id.
1363 static int sctp_setsockopt_connectx_old(struct sock *sk,
1364 struct sockaddr __user *addrs,
1367 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1371 * New interface for the API. The since the API is done with a socket
1372 * option, to make it simple we feed back the association id is as a return
1373 * indication to the call. Error is always negative and association id is
1376 static int sctp_setsockopt_connectx(struct sock *sk,
1377 struct sockaddr __user *addrs,
1380 sctp_assoc_t assoc_id = 0;
1383 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1392 * New (hopefully final) interface for the API.
1393 * We use the sctp_getaddrs_old structure so that use-space library
1394 * can avoid any unnecessary allocations. The only different part
1395 * is that we store the actual length of the address buffer into the
1396 * addrs_num structure member. That way we can re-use the existing
1399 #ifdef CONFIG_COMPAT
1400 struct compat_sctp_getaddrs_old {
1401 sctp_assoc_t assoc_id;
1403 compat_uptr_t addrs; /* struct sockaddr * */
1407 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1408 char __user *optval,
1411 struct sctp_getaddrs_old param;
1412 sctp_assoc_t assoc_id = 0;
1415 #ifdef CONFIG_COMPAT
1416 if (in_compat_syscall()) {
1417 struct compat_sctp_getaddrs_old param32;
1419 if (len < sizeof(param32))
1421 if (copy_from_user(¶m32, optval, sizeof(param32)))
1424 param.assoc_id = param32.assoc_id;
1425 param.addr_num = param32.addr_num;
1426 param.addrs = compat_ptr(param32.addrs);
1430 if (len < sizeof(param))
1432 if (copy_from_user(¶m, optval, sizeof(param)))
1436 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1437 param.addrs, param.addr_num,
1439 if (err == 0 || err == -EINPROGRESS) {
1440 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1442 if (put_user(sizeof(assoc_id), optlen))
1449 /* API 3.1.4 close() - UDP Style Syntax
1450 * Applications use close() to perform graceful shutdown (as described in
1451 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1452 * by a UDP-style socket.
1456 * ret = close(int sd);
1458 * sd - the socket descriptor of the associations to be closed.
1460 * To gracefully shutdown a specific association represented by the
1461 * UDP-style socket, an application should use the sendmsg() call,
1462 * passing no user data, but including the appropriate flag in the
1463 * ancillary data (see Section xxxx).
1465 * If sd in the close() call is a branched-off socket representing only
1466 * one association, the shutdown is performed on that association only.
1468 * 4.1.6 close() - TCP Style Syntax
1470 * Applications use close() to gracefully close down an association.
1474 * int close(int sd);
1476 * sd - the socket descriptor of the association to be closed.
1478 * After an application calls close() on a socket descriptor, no further
1479 * socket operations will succeed on that descriptor.
1481 * API 7.1.4 SO_LINGER
1483 * An application using the TCP-style socket can use this option to
1484 * perform the SCTP ABORT primitive. The linger option structure is:
1487 * int l_onoff; // option on/off
1488 * int l_linger; // linger time
1491 * To enable the option, set l_onoff to 1. If the l_linger value is set
1492 * to 0, calling close() is the same as the ABORT primitive. If the
1493 * value is set to a negative value, the setsockopt() call will return
1494 * an error. If the value is set to a positive value linger_time, the
1495 * close() can be blocked for at most linger_time ms. If the graceful
1496 * shutdown phase does not finish during this period, close() will
1497 * return but the graceful shutdown phase continues in the system.
1499 static void sctp_close(struct sock *sk, long timeout)
1501 struct net *net = sock_net(sk);
1502 struct sctp_endpoint *ep;
1503 struct sctp_association *asoc;
1504 struct list_head *pos, *temp;
1505 unsigned int data_was_unread;
1507 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1509 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1510 sk->sk_shutdown = SHUTDOWN_MASK;
1511 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1513 ep = sctp_sk(sk)->ep;
1515 /* Clean up any skbs sitting on the receive queue. */
1516 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1517 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1519 /* Walk all associations on an endpoint. */
1520 list_for_each_safe(pos, temp, &ep->asocs) {
1521 asoc = list_entry(pos, struct sctp_association, asocs);
1523 if (sctp_style(sk, TCP)) {
1524 /* A closed association can still be in the list if
1525 * it belongs to a TCP-style listening socket that is
1526 * not yet accepted. If so, free it. If not, send an
1527 * ABORT or SHUTDOWN based on the linger options.
1529 if (sctp_state(asoc, CLOSED)) {
1530 sctp_association_free(asoc);
1535 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1536 !skb_queue_empty(&asoc->ulpq.reasm) ||
1537 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1538 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1539 struct sctp_chunk *chunk;
1541 chunk = sctp_make_abort_user(asoc, NULL, 0);
1542 sctp_primitive_ABORT(net, asoc, chunk);
1544 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1547 /* On a TCP-style socket, block for at most linger_time if set. */
1548 if (sctp_style(sk, TCP) && timeout)
1549 sctp_wait_for_close(sk, timeout);
1551 /* This will run the backlog queue. */
1554 /* Supposedly, no process has access to the socket, but
1555 * the net layers still may.
1556 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1557 * held and that should be grabbed before socket lock.
1559 spin_lock_bh(&net->sctp.addr_wq_lock);
1560 bh_lock_sock_nested(sk);
1562 /* Hold the sock, since sk_common_release() will put sock_put()
1563 * and we have just a little more cleanup.
1566 sk_common_release(sk);
1569 spin_unlock_bh(&net->sctp.addr_wq_lock);
1573 SCTP_DBG_OBJCNT_DEC(sock);
1576 /* Handle EPIPE error. */
1577 static int sctp_error(struct sock *sk, int flags, int err)
1580 err = sock_error(sk) ? : -EPIPE;
1581 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1582 send_sig(SIGPIPE, current, 0);
1586 /* API 3.1.3 sendmsg() - UDP Style Syntax
1588 * An application uses sendmsg() and recvmsg() calls to transmit data to
1589 * and receive data from its peer.
1591 * ssize_t sendmsg(int socket, const struct msghdr *message,
1594 * socket - the socket descriptor of the endpoint.
1595 * message - pointer to the msghdr structure which contains a single
1596 * user message and possibly some ancillary data.
1598 * See Section 5 for complete description of the data
1601 * flags - flags sent or received with the user message, see Section
1602 * 5 for complete description of the flags.
1604 * Note: This function could use a rewrite especially when explicit
1605 * connect support comes in.
1607 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1609 static int sctp_msghdr_parse(const struct msghdr *msg,
1610 struct sctp_cmsgs *cmsgs);
1612 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1613 struct sctp_sndrcvinfo *srinfo,
1614 const struct msghdr *msg, size_t msg_len)
1619 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1622 if (msg_len > sk->sk_sndbuf)
1625 memset(cmsgs, 0, sizeof(*cmsgs));
1626 err = sctp_msghdr_parse(msg, cmsgs);
1628 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1632 memset(srinfo, 0, sizeof(*srinfo));
1633 if (cmsgs->srinfo) {
1634 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1635 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1636 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1637 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1638 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1639 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1643 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1644 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1645 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1646 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1647 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1650 if (cmsgs->prinfo) {
1651 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1652 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1653 cmsgs->prinfo->pr_policy);
1656 sflags = srinfo->sinfo_flags;
1657 if (!sflags && msg_len)
1660 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1663 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1664 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1667 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1673 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1674 struct sctp_cmsgs *cmsgs,
1675 union sctp_addr *daddr,
1676 struct sctp_transport **tp)
1678 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1679 struct sctp_association *asoc;
1680 struct cmsghdr *cmsg;
1681 __be32 flowinfo = 0;
1687 if (sflags & (SCTP_EOF | SCTP_ABORT))
1690 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1691 sctp_sstate(sk, CLOSING)))
1692 return -EADDRNOTAVAIL;
1694 /* Label connection socket for first association 1-to-many
1695 * style for client sequence socket()->sendmsg(). This
1696 * needs to be done before sctp_assoc_add_peer() as that will
1697 * set up the initial packet that needs to account for any
1698 * security ip options (CIPSO/CALIPSO) added to the packet.
1700 af = sctp_get_af_specific(daddr->sa.sa_family);
1703 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1704 (struct sockaddr *)daddr,
1709 err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
1714 if (!cmsgs->addrs_msg)
1717 if (daddr->sa.sa_family == AF_INET6)
1718 flowinfo = daddr->v6.sin6_flowinfo;
1720 /* sendv addr list parse */
1721 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1722 union sctp_addr _daddr;
1725 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1726 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1727 cmsg->cmsg_type != SCTP_DSTADDRV6))
1731 memset(daddr, 0, sizeof(*daddr));
1732 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1733 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1734 if (dlen < sizeof(struct in_addr)) {
1739 dlen = sizeof(struct in_addr);
1740 daddr->v4.sin_family = AF_INET;
1741 daddr->v4.sin_port = htons(asoc->peer.port);
1742 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1744 if (dlen < sizeof(struct in6_addr)) {
1749 dlen = sizeof(struct in6_addr);
1750 daddr->v6.sin6_flowinfo = flowinfo;
1751 daddr->v6.sin6_family = AF_INET6;
1752 daddr->v6.sin6_port = htons(asoc->peer.port);
1753 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1756 err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
1764 sctp_association_free(asoc);
1768 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1769 __u16 sflags, struct msghdr *msg,
1772 struct sock *sk = asoc->base.sk;
1773 struct net *net = sock_net(sk);
1775 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1778 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1779 !sctp_state(asoc, ESTABLISHED))
1782 if (sflags & SCTP_EOF) {
1783 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1784 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1789 if (sflags & SCTP_ABORT) {
1790 struct sctp_chunk *chunk;
1792 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1796 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1797 sctp_primitive_ABORT(net, asoc, chunk);
1798 iov_iter_revert(&msg->msg_iter, msg_len);
1806 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1807 struct msghdr *msg, size_t msg_len,
1808 struct sctp_transport *transport,
1809 struct sctp_sndrcvinfo *sinfo)
1811 struct sock *sk = asoc->base.sk;
1812 struct sctp_sock *sp = sctp_sk(sk);
1813 struct net *net = sock_net(sk);
1814 struct sctp_datamsg *datamsg;
1815 bool wait_connect = false;
1816 struct sctp_chunk *chunk;
1820 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1825 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1826 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1831 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1836 if (asoc->pmtu_pending) {
1837 if (sp->param_flags & SPP_PMTUD_ENABLE)
1838 sctp_assoc_sync_pmtu(asoc);
1839 asoc->pmtu_pending = 0;
1842 if (sctp_wspace(asoc) < (int)msg_len)
1843 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1845 if (sk_under_memory_pressure(sk))
1848 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1849 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1850 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1855 if (sctp_state(asoc, CLOSED)) {
1856 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1860 if (asoc->ep->intl_enable) {
1861 timeo = sock_sndtimeo(sk, 0);
1862 err = sctp_wait_for_connect(asoc, &timeo);
1868 wait_connect = true;
1871 pr_debug("%s: we associated primitively\n", __func__);
1874 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1875 if (IS_ERR(datamsg)) {
1876 err = PTR_ERR(datamsg);
1880 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1882 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1883 sctp_chunk_hold(chunk);
1884 sctp_set_owner_w(chunk);
1885 chunk->transport = transport;
1888 err = sctp_primitive_SEND(net, asoc, datamsg);
1890 sctp_datamsg_free(datamsg);
1894 pr_debug("%s: we sent primitively\n", __func__);
1896 sctp_datamsg_put(datamsg);
1898 if (unlikely(wait_connect)) {
1899 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1900 sctp_wait_for_connect(asoc, &timeo);
1909 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1910 const struct msghdr *msg,
1911 struct sctp_cmsgs *cmsgs)
1913 union sctp_addr *daddr = NULL;
1916 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1917 int len = msg->msg_namelen;
1919 if (len > sizeof(*daddr))
1920 len = sizeof(*daddr);
1922 daddr = (union sctp_addr *)msg->msg_name;
1924 err = sctp_verify_addr(sk, daddr, len);
1926 return ERR_PTR(err);
1932 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1933 struct sctp_sndrcvinfo *sinfo,
1934 struct sctp_cmsgs *cmsgs)
1936 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1937 sinfo->sinfo_stream = asoc->default_stream;
1938 sinfo->sinfo_ppid = asoc->default_ppid;
1939 sinfo->sinfo_context = asoc->default_context;
1940 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1943 sinfo->sinfo_flags = asoc->default_flags;
1946 if (!cmsgs->srinfo && !cmsgs->prinfo)
1947 sinfo->sinfo_timetolive = asoc->default_timetolive;
1949 if (cmsgs->authinfo) {
1950 /* Reuse sinfo_tsn to indicate that authinfo was set and
1951 * sinfo_ssn to save the keyid on tx path.
1953 sinfo->sinfo_tsn = 1;
1954 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1958 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1960 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1961 struct sctp_transport *transport = NULL;
1962 struct sctp_sndrcvinfo _sinfo, *sinfo;
1963 struct sctp_association *asoc, *tmp;
1964 struct sctp_cmsgs cmsgs;
1965 union sctp_addr *daddr;
1970 /* Parse and get snd_info */
1971 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1976 sflags = sinfo->sinfo_flags;
1978 /* Get daddr from msg */
1979 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1980 if (IS_ERR(daddr)) {
1981 err = PTR_ERR(daddr);
1987 /* SCTP_SENDALL process */
1988 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
1989 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
1990 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1997 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1999 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2004 iov_iter_revert(&msg->msg_iter, err);
2010 /* Get and check or create asoc */
2012 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2014 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2019 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2024 asoc = transport->asoc;
2028 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2031 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2037 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2042 /* Update snd_info with the asoc */
2043 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2045 /* Send msg to the asoc */
2046 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2047 if (err < 0 && err != -ESRCH && new)
2048 sctp_association_free(asoc);
2053 return sctp_error(sk, msg->msg_flags, err);
2056 /* This is an extended version of skb_pull() that removes the data from the
2057 * start of a skb even when data is spread across the list of skb's in the
2058 * frag_list. len specifies the total amount of data that needs to be removed.
2059 * when 'len' bytes could be removed from the skb, it returns 0.
2060 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2061 * could not be removed.
2063 static int sctp_skb_pull(struct sk_buff *skb, int len)
2065 struct sk_buff *list;
2066 int skb_len = skb_headlen(skb);
2069 if (len <= skb_len) {
2070 __skb_pull(skb, len);
2074 __skb_pull(skb, skb_len);
2076 skb_walk_frags(skb, list) {
2077 rlen = sctp_skb_pull(list, len);
2078 skb->len -= (len-rlen);
2079 skb->data_len -= (len-rlen);
2090 /* API 3.1.3 recvmsg() - UDP Style Syntax
2092 * ssize_t recvmsg(int socket, struct msghdr *message,
2095 * socket - the socket descriptor of the endpoint.
2096 * message - pointer to the msghdr structure which contains a single
2097 * user message and possibly some ancillary data.
2099 * See Section 5 for complete description of the data
2102 * flags - flags sent or received with the user message, see Section
2103 * 5 for complete description of the flags.
2105 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2106 int noblock, int flags, int *addr_len)
2108 struct sctp_ulpevent *event = NULL;
2109 struct sctp_sock *sp = sctp_sk(sk);
2110 struct sk_buff *skb, *head_skb;
2115 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2116 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2121 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2122 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2127 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2131 /* Get the total length of the skb including any skb's in the
2140 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2142 event = sctp_skb2event(skb);
2147 if (event->chunk && event->chunk->head_skb)
2148 head_skb = event->chunk->head_skb;
2151 sock_recv_ts_and_drops(msg, sk, head_skb);
2152 if (sctp_ulpevent_is_notification(event)) {
2153 msg->msg_flags |= MSG_NOTIFICATION;
2154 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2156 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2159 /* Check if we allow SCTP_NXTINFO. */
2160 if (sp->recvnxtinfo)
2161 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2162 /* Check if we allow SCTP_RCVINFO. */
2163 if (sp->recvrcvinfo)
2164 sctp_ulpevent_read_rcvinfo(event, msg);
2165 /* Check if we allow SCTP_SNDRCVINFO. */
2166 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2167 sctp_ulpevent_read_sndrcvinfo(event, msg);
2171 /* If skb's length exceeds the user's buffer, update the skb and
2172 * push it back to the receive_queue so that the next call to
2173 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2175 if (skb_len > copied) {
2176 msg->msg_flags &= ~MSG_EOR;
2177 if (flags & MSG_PEEK)
2179 sctp_skb_pull(skb, copied);
2180 skb_queue_head(&sk->sk_receive_queue, skb);
2182 /* When only partial message is copied to the user, increase
2183 * rwnd by that amount. If all the data in the skb is read,
2184 * rwnd is updated when the event is freed.
2186 if (!sctp_ulpevent_is_notification(event))
2187 sctp_assoc_rwnd_increase(event->asoc, copied);
2189 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2190 (event->msg_flags & MSG_EOR))
2191 msg->msg_flags |= MSG_EOR;
2193 msg->msg_flags &= ~MSG_EOR;
2196 if (flags & MSG_PEEK) {
2197 /* Release the skb reference acquired after peeking the skb in
2198 * sctp_skb_recv_datagram().
2202 /* Free the event which includes releasing the reference to
2203 * the owner of the skb, freeing the skb and updating the
2206 sctp_ulpevent_free(event);
2213 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2215 * This option is a on/off flag. If enabled no SCTP message
2216 * fragmentation will be performed. Instead if a message being sent
2217 * exceeds the current PMTU size, the message will NOT be sent and
2218 * instead a error will be indicated to the user.
2220 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2221 char __user *optval,
2222 unsigned int optlen)
2226 if (optlen < sizeof(int))
2229 if (get_user(val, (int __user *)optval))
2232 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2237 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2238 unsigned int optlen)
2240 struct sctp_event_subscribe subscribe;
2241 __u8 *sn_type = (__u8 *)&subscribe;
2242 struct sctp_sock *sp = sctp_sk(sk);
2243 struct sctp_association *asoc;
2246 if (optlen > sizeof(struct sctp_event_subscribe))
2249 if (copy_from_user(&subscribe, optval, optlen))
2252 for (i = 0; i < optlen; i++)
2253 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2256 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2257 asoc->subscribe = sctp_sk(sk)->subscribe;
2259 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2260 * if there is no data to be sent or retransmit, the stack will
2261 * immediately send up this notification.
2263 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2264 struct sctp_ulpevent *event;
2266 asoc = sctp_id2assoc(sk, 0);
2267 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2268 event = sctp_ulpevent_make_sender_dry_event(asoc,
2269 GFP_USER | __GFP_NOWARN);
2273 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2280 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2282 * This socket option is applicable to the UDP-style socket only. When
2283 * set it will cause associations that are idle for more than the
2284 * specified number of seconds to automatically close. An association
2285 * being idle is defined an association that has NOT sent or received
2286 * user data. The special value of '0' indicates that no automatic
2287 * close of any associations should be performed. The option expects an
2288 * integer defining the number of seconds of idle time before an
2289 * association is closed.
2291 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2292 unsigned int optlen)
2294 struct sctp_sock *sp = sctp_sk(sk);
2295 struct net *net = sock_net(sk);
2297 /* Applicable to UDP-style socket only */
2298 if (sctp_style(sk, TCP))
2300 if (optlen != sizeof(int))
2302 if (copy_from_user(&sp->autoclose, optval, optlen))
2305 if (sp->autoclose > net->sctp.max_autoclose)
2306 sp->autoclose = net->sctp.max_autoclose;
2311 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2313 * Applications can enable or disable heartbeats for any peer address of
2314 * an association, modify an address's heartbeat interval, force a
2315 * heartbeat to be sent immediately, and adjust the address's maximum
2316 * number of retransmissions sent before an address is considered
2317 * unreachable. The following structure is used to access and modify an
2318 * address's parameters:
2320 * struct sctp_paddrparams {
2321 * sctp_assoc_t spp_assoc_id;
2322 * struct sockaddr_storage spp_address;
2323 * uint32_t spp_hbinterval;
2324 * uint16_t spp_pathmaxrxt;
2325 * uint32_t spp_pathmtu;
2326 * uint32_t spp_sackdelay;
2327 * uint32_t spp_flags;
2328 * uint32_t spp_ipv6_flowlabel;
2332 * spp_assoc_id - (one-to-many style socket) This is filled in the
2333 * application, and identifies the association for
2335 * spp_address - This specifies which address is of interest.
2336 * spp_hbinterval - This contains the value of the heartbeat interval,
2337 * in milliseconds. If a value of zero
2338 * is present in this field then no changes are to
2339 * be made to this parameter.
2340 * spp_pathmaxrxt - This contains the maximum number of
2341 * retransmissions before this address shall be
2342 * considered unreachable. If a value of zero
2343 * is present in this field then no changes are to
2344 * be made to this parameter.
2345 * spp_pathmtu - When Path MTU discovery is disabled the value
2346 * specified here will be the "fixed" path mtu.
2347 * Note that if the spp_address field is empty
2348 * then all associations on this address will
2349 * have this fixed path mtu set upon them.
2351 * spp_sackdelay - When delayed sack is enabled, this value specifies
2352 * the number of milliseconds that sacks will be delayed
2353 * for. This value will apply to all addresses of an
2354 * association if the spp_address field is empty. Note
2355 * also, that if delayed sack is enabled and this
2356 * value is set to 0, no change is made to the last
2357 * recorded delayed sack timer value.
2359 * spp_flags - These flags are used to control various features
2360 * on an association. The flag field may contain
2361 * zero or more of the following options.
2363 * SPP_HB_ENABLE - Enable heartbeats on the
2364 * specified address. Note that if the address
2365 * field is empty all addresses for the association
2366 * have heartbeats enabled upon them.
2368 * SPP_HB_DISABLE - Disable heartbeats on the
2369 * speicifed address. Note that if the address
2370 * field is empty all addresses for the association
2371 * will have their heartbeats disabled. Note also
2372 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2373 * mutually exclusive, only one of these two should
2374 * be specified. Enabling both fields will have
2375 * undetermined results.
2377 * SPP_HB_DEMAND - Request a user initiated heartbeat
2378 * to be made immediately.
2380 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2381 * heartbeat delayis to be set to the value of 0
2384 * SPP_PMTUD_ENABLE - This field will enable PMTU
2385 * discovery upon the specified address. Note that
2386 * if the address feild is empty then all addresses
2387 * on the association are effected.
2389 * SPP_PMTUD_DISABLE - This field will disable PMTU
2390 * discovery upon the specified address. Note that
2391 * if the address feild is empty then all addresses
2392 * on the association are effected. Not also that
2393 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2394 * exclusive. Enabling both will have undetermined
2397 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2398 * on delayed sack. The time specified in spp_sackdelay
2399 * is used to specify the sack delay for this address. Note
2400 * that if spp_address is empty then all addresses will
2401 * enable delayed sack and take on the sack delay
2402 * value specified in spp_sackdelay.
2403 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2404 * off delayed sack. If the spp_address field is blank then
2405 * delayed sack is disabled for the entire association. Note
2406 * also that this field is mutually exclusive to
2407 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2410 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2411 * setting of the IPV6 flow label value. The value is
2412 * contained in the spp_ipv6_flowlabel field.
2413 * Upon retrieval, this flag will be set to indicate that
2414 * the spp_ipv6_flowlabel field has a valid value returned.
2415 * If a specific destination address is set (in the
2416 * spp_address field), then the value returned is that of
2417 * the address. If just an association is specified (and
2418 * no address), then the association's default flow label
2419 * is returned. If neither an association nor a destination
2420 * is specified, then the socket's default flow label is
2421 * returned. For non-IPv6 sockets, this flag will be left
2424 * SPP_DSCP: Setting this flag enables the setting of the
2425 * Differentiated Services Code Point (DSCP) value
2426 * associated with either the association or a specific
2427 * address. The value is obtained in the spp_dscp field.
2428 * Upon retrieval, this flag will be set to indicate that
2429 * the spp_dscp field has a valid value returned. If a
2430 * specific destination address is set when called (in the
2431 * spp_address field), then that specific destination
2432 * address's DSCP value is returned. If just an association
2433 * is specified, then the association's default DSCP is
2434 * returned. If neither an association nor a destination is
2435 * specified, then the socket's default DSCP is returned.
2437 * spp_ipv6_flowlabel
2438 * - This field is used in conjunction with the
2439 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2440 * The 20 least significant bits are used for the flow
2441 * label. This setting has precedence over any IPv6-layer
2444 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2445 * and contains the DSCP. The 6 most significant bits are
2446 * used for the DSCP. This setting has precedence over any
2447 * IPv4- or IPv6- layer setting.
2449 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2450 struct sctp_transport *trans,
2451 struct sctp_association *asoc,
2452 struct sctp_sock *sp,
2455 int sackdelay_change)
2459 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2460 struct net *net = sock_net(trans->asoc->base.sk);
2462 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2467 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2468 * this field is ignored. Note also that a value of zero indicates
2469 * the current setting should be left unchanged.
2471 if (params->spp_flags & SPP_HB_ENABLE) {
2473 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2474 * set. This lets us use 0 value when this flag
2477 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2478 params->spp_hbinterval = 0;
2480 if (params->spp_hbinterval ||
2481 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2484 msecs_to_jiffies(params->spp_hbinterval);
2487 msecs_to_jiffies(params->spp_hbinterval);
2489 sp->hbinterval = params->spp_hbinterval;
2496 trans->param_flags =
2497 (trans->param_flags & ~SPP_HB) | hb_change;
2500 (asoc->param_flags & ~SPP_HB) | hb_change;
2503 (sp->param_flags & ~SPP_HB) | hb_change;
2507 /* When Path MTU discovery is disabled the value specified here will
2508 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2509 * include the flag SPP_PMTUD_DISABLE for this field to have any
2512 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2514 trans->pathmtu = params->spp_pathmtu;
2515 sctp_assoc_sync_pmtu(asoc);
2517 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2519 sp->pathmtu = params->spp_pathmtu;
2525 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2526 (params->spp_flags & SPP_PMTUD_ENABLE);
2527 trans->param_flags =
2528 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2530 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2531 sctp_assoc_sync_pmtu(asoc);
2535 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2538 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2542 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2543 * value of this field is ignored. Note also that a value of zero
2544 * indicates the current setting should be left unchanged.
2546 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2549 msecs_to_jiffies(params->spp_sackdelay);
2552 msecs_to_jiffies(params->spp_sackdelay);
2554 sp->sackdelay = params->spp_sackdelay;
2558 if (sackdelay_change) {
2560 trans->param_flags =
2561 (trans->param_flags & ~SPP_SACKDELAY) |
2565 (asoc->param_flags & ~SPP_SACKDELAY) |
2569 (sp->param_flags & ~SPP_SACKDELAY) |
2574 /* Note that a value of zero indicates the current setting should be
2577 if (params->spp_pathmaxrxt) {
2579 trans->pathmaxrxt = params->spp_pathmaxrxt;
2581 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2583 sp->pathmaxrxt = params->spp_pathmaxrxt;
2587 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2589 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2590 trans->flowlabel = params->spp_ipv6_flowlabel &
2591 SCTP_FLOWLABEL_VAL_MASK;
2592 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2595 struct sctp_transport *t;
2597 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2599 if (t->ipaddr.sa.sa_family != AF_INET6)
2601 t->flowlabel = params->spp_ipv6_flowlabel &
2602 SCTP_FLOWLABEL_VAL_MASK;
2603 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2605 asoc->flowlabel = params->spp_ipv6_flowlabel &
2606 SCTP_FLOWLABEL_VAL_MASK;
2607 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2608 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2609 sp->flowlabel = params->spp_ipv6_flowlabel &
2610 SCTP_FLOWLABEL_VAL_MASK;
2611 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2615 if (params->spp_flags & SPP_DSCP) {
2617 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2618 trans->dscp |= SCTP_DSCP_SET_MASK;
2620 struct sctp_transport *t;
2622 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2624 t->dscp = params->spp_dscp &
2626 t->dscp |= SCTP_DSCP_SET_MASK;
2628 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2629 asoc->dscp |= SCTP_DSCP_SET_MASK;
2631 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2632 sp->dscp |= SCTP_DSCP_SET_MASK;
2639 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2640 char __user *optval,
2641 unsigned int optlen)
2643 struct sctp_paddrparams params;
2644 struct sctp_transport *trans = NULL;
2645 struct sctp_association *asoc = NULL;
2646 struct sctp_sock *sp = sctp_sk(sk);
2648 int hb_change, pmtud_change, sackdelay_change;
2650 if (optlen == sizeof(params)) {
2651 if (copy_from_user(¶ms, optval, optlen))
2653 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2654 spp_ipv6_flowlabel), 4)) {
2655 if (copy_from_user(¶ms, optval, optlen))
2657 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2663 /* Validate flags and value parameters. */
2664 hb_change = params.spp_flags & SPP_HB;
2665 pmtud_change = params.spp_flags & SPP_PMTUD;
2666 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2668 if (hb_change == SPP_HB ||
2669 pmtud_change == SPP_PMTUD ||
2670 sackdelay_change == SPP_SACKDELAY ||
2671 params.spp_sackdelay > 500 ||
2672 (params.spp_pathmtu &&
2673 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2676 /* If an address other than INADDR_ANY is specified, and
2677 * no transport is found, then the request is invalid.
2679 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2680 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2681 params.spp_assoc_id);
2686 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2687 * socket is a one to many style socket, and an association
2688 * was not found, then the id was invalid.
2690 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2691 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2692 sctp_style(sk, UDP))
2695 /* Heartbeat demand can only be sent on a transport or
2696 * association, but not a socket.
2698 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2701 /* Process parameters. */
2702 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2703 hb_change, pmtud_change,
2709 /* If changes are for association, also apply parameters to each
2712 if (!trans && asoc) {
2713 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2715 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2716 hb_change, pmtud_change,
2724 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2726 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2729 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2731 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2734 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2735 struct sctp_association *asoc)
2737 struct sctp_transport *trans;
2739 if (params->sack_delay) {
2740 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2742 sctp_spp_sackdelay_enable(asoc->param_flags);
2744 if (params->sack_freq == 1) {
2746 sctp_spp_sackdelay_disable(asoc->param_flags);
2747 } else if (params->sack_freq > 1) {
2748 asoc->sackfreq = params->sack_freq;
2750 sctp_spp_sackdelay_enable(asoc->param_flags);
2753 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2755 if (params->sack_delay) {
2756 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2757 trans->param_flags =
2758 sctp_spp_sackdelay_enable(trans->param_flags);
2760 if (params->sack_freq == 1) {
2761 trans->param_flags =
2762 sctp_spp_sackdelay_disable(trans->param_flags);
2763 } else if (params->sack_freq > 1) {
2764 trans->sackfreq = params->sack_freq;
2765 trans->param_flags =
2766 sctp_spp_sackdelay_enable(trans->param_flags);
2772 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2774 * This option will effect the way delayed acks are performed. This
2775 * option allows you to get or set the delayed ack time, in
2776 * milliseconds. It also allows changing the delayed ack frequency.
2777 * Changing the frequency to 1 disables the delayed sack algorithm. If
2778 * the assoc_id is 0, then this sets or gets the endpoints default
2779 * values. If the assoc_id field is non-zero, then the set or get
2780 * effects the specified association for the one to many model (the
2781 * assoc_id field is ignored by the one to one model). Note that if
2782 * sack_delay or sack_freq are 0 when setting this option, then the
2783 * current values will remain unchanged.
2785 * struct sctp_sack_info {
2786 * sctp_assoc_t sack_assoc_id;
2787 * uint32_t sack_delay;
2788 * uint32_t sack_freq;
2791 * sack_assoc_id - This parameter, indicates which association the user
2792 * is performing an action upon. Note that if this field's value is
2793 * zero then the endpoints default value is changed (effecting future
2794 * associations only).
2796 * sack_delay - This parameter contains the number of milliseconds that
2797 * the user is requesting the delayed ACK timer be set to. Note that
2798 * this value is defined in the standard to be between 200 and 500
2801 * sack_freq - This parameter contains the number of packets that must
2802 * be received before a sack is sent without waiting for the delay
2803 * timer to expire. The default value for this is 2, setting this
2804 * value to 1 will disable the delayed sack algorithm.
2807 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2808 char __user *optval, unsigned int optlen)
2810 struct sctp_sock *sp = sctp_sk(sk);
2811 struct sctp_association *asoc;
2812 struct sctp_sack_info params;
2814 if (optlen == sizeof(struct sctp_sack_info)) {
2815 if (copy_from_user(¶ms, optval, optlen))
2818 if (params.sack_delay == 0 && params.sack_freq == 0)
2820 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2821 pr_warn_ratelimited(DEPRECATED
2823 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2824 "Use struct sctp_sack_info instead\n",
2825 current->comm, task_pid_nr(current));
2826 if (copy_from_user(¶ms, optval, optlen))
2829 if (params.sack_delay == 0)
2830 params.sack_freq = 1;
2832 params.sack_freq = 0;
2836 /* Validate value parameter. */
2837 if (params.sack_delay > 500)
2840 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2841 * socket is a one to many style socket, and an association
2842 * was not found, then the id was invalid.
2844 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2845 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2846 sctp_style(sk, UDP))
2850 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2855 if (sctp_style(sk, TCP))
2856 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2858 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2859 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2860 if (params.sack_delay) {
2861 sp->sackdelay = params.sack_delay;
2863 sctp_spp_sackdelay_enable(sp->param_flags);
2865 if (params.sack_freq == 1) {
2867 sctp_spp_sackdelay_disable(sp->param_flags);
2868 } else if (params.sack_freq > 1) {
2869 sp->sackfreq = params.sack_freq;
2871 sctp_spp_sackdelay_enable(sp->param_flags);
2875 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2876 params.sack_assoc_id == SCTP_ALL_ASSOC)
2877 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2878 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2883 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2885 * Applications can specify protocol parameters for the default association
2886 * initialization. The option name argument to setsockopt() and getsockopt()
2889 * Setting initialization parameters is effective only on an unconnected
2890 * socket (for UDP-style sockets only future associations are effected
2891 * by the change). With TCP-style sockets, this option is inherited by
2892 * sockets derived from a listener socket.
2894 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2896 struct sctp_initmsg sinit;
2897 struct sctp_sock *sp = sctp_sk(sk);
2899 if (optlen != sizeof(struct sctp_initmsg))
2901 if (copy_from_user(&sinit, optval, optlen))
2904 if (sinit.sinit_num_ostreams)
2905 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2906 if (sinit.sinit_max_instreams)
2907 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2908 if (sinit.sinit_max_attempts)
2909 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2910 if (sinit.sinit_max_init_timeo)
2911 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2917 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2919 * Applications that wish to use the sendto() system call may wish to
2920 * specify a default set of parameters that would normally be supplied
2921 * through the inclusion of ancillary data. This socket option allows
2922 * such an application to set the default sctp_sndrcvinfo structure.
2923 * The application that wishes to use this socket option simply passes
2924 * in to this call the sctp_sndrcvinfo structure defined in Section
2925 * 5.2.2) The input parameters accepted by this call include
2926 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2927 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2928 * to this call if the caller is using the UDP model.
2930 static int sctp_setsockopt_default_send_param(struct sock *sk,
2931 char __user *optval,
2932 unsigned int optlen)
2934 struct sctp_sock *sp = sctp_sk(sk);
2935 struct sctp_association *asoc;
2936 struct sctp_sndrcvinfo info;
2938 if (optlen != sizeof(info))
2940 if (copy_from_user(&info, optval, optlen))
2942 if (info.sinfo_flags &
2943 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2944 SCTP_ABORT | SCTP_EOF))
2947 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2948 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
2949 sctp_style(sk, UDP))
2953 asoc->default_stream = info.sinfo_stream;
2954 asoc->default_flags = info.sinfo_flags;
2955 asoc->default_ppid = info.sinfo_ppid;
2956 asoc->default_context = info.sinfo_context;
2957 asoc->default_timetolive = info.sinfo_timetolive;
2962 if (sctp_style(sk, TCP))
2963 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2965 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2966 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2967 sp->default_stream = info.sinfo_stream;
2968 sp->default_flags = info.sinfo_flags;
2969 sp->default_ppid = info.sinfo_ppid;
2970 sp->default_context = info.sinfo_context;
2971 sp->default_timetolive = info.sinfo_timetolive;
2974 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2975 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2976 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2977 asoc->default_stream = info.sinfo_stream;
2978 asoc->default_flags = info.sinfo_flags;
2979 asoc->default_ppid = info.sinfo_ppid;
2980 asoc->default_context = info.sinfo_context;
2981 asoc->default_timetolive = info.sinfo_timetolive;
2988 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2989 * (SCTP_DEFAULT_SNDINFO)
2991 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2992 char __user *optval,
2993 unsigned int optlen)
2995 struct sctp_sock *sp = sctp_sk(sk);
2996 struct sctp_association *asoc;
2997 struct sctp_sndinfo info;
2999 if (optlen != sizeof(info))
3001 if (copy_from_user(&info, optval, optlen))
3003 if (info.snd_flags &
3004 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3005 SCTP_ABORT | SCTP_EOF))
3008 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3009 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3010 sctp_style(sk, UDP))
3014 asoc->default_stream = info.snd_sid;
3015 asoc->default_flags = info.snd_flags;
3016 asoc->default_ppid = info.snd_ppid;
3017 asoc->default_context = info.snd_context;
3022 if (sctp_style(sk, TCP))
3023 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3025 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3026 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3027 sp->default_stream = info.snd_sid;
3028 sp->default_flags = info.snd_flags;
3029 sp->default_ppid = info.snd_ppid;
3030 sp->default_context = info.snd_context;
3033 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3034 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3035 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3036 asoc->default_stream = info.snd_sid;
3037 asoc->default_flags = info.snd_flags;
3038 asoc->default_ppid = info.snd_ppid;
3039 asoc->default_context = info.snd_context;
3046 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3048 * Requests that the local SCTP stack use the enclosed peer address as
3049 * the association primary. The enclosed address must be one of the
3050 * association peer's addresses.
3052 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3053 unsigned int optlen)
3055 struct sctp_prim prim;
3056 struct sctp_transport *trans;
3060 if (optlen != sizeof(struct sctp_prim))
3063 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3066 /* Allow security module to validate address but need address len. */
3067 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3071 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3072 (struct sockaddr *)&prim.ssp_addr,
3077 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3081 sctp_assoc_set_primary(trans->asoc, trans);
3087 * 7.1.5 SCTP_NODELAY
3089 * Turn on/off any Nagle-like algorithm. This means that packets are
3090 * generally sent as soon as possible and no unnecessary delays are
3091 * introduced, at the cost of more packets in the network. Expects an
3092 * integer boolean flag.
3094 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3095 unsigned int optlen)
3099 if (optlen < sizeof(int))
3101 if (get_user(val, (int __user *)optval))
3104 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3110 * 7.1.1 SCTP_RTOINFO
3112 * The protocol parameters used to initialize and bound retransmission
3113 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3114 * and modify these parameters.
3115 * All parameters are time values, in milliseconds. A value of 0, when
3116 * modifying the parameters, indicates that the current value should not
3120 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3122 struct sctp_rtoinfo rtoinfo;
3123 struct sctp_association *asoc;
3124 unsigned long rto_min, rto_max;
3125 struct sctp_sock *sp = sctp_sk(sk);
3127 if (optlen != sizeof (struct sctp_rtoinfo))
3130 if (copy_from_user(&rtoinfo, optval, optlen))
3133 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3135 /* Set the values to the specific association */
3136 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3137 sctp_style(sk, UDP))
3140 rto_max = rtoinfo.srto_max;
3141 rto_min = rtoinfo.srto_min;
3144 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3146 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3149 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3151 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3153 if (rto_min > rto_max)
3157 if (rtoinfo.srto_initial != 0)
3159 msecs_to_jiffies(rtoinfo.srto_initial);
3160 asoc->rto_max = rto_max;
3161 asoc->rto_min = rto_min;
3163 /* If there is no association or the association-id = 0
3164 * set the values to the endpoint.
3166 if (rtoinfo.srto_initial != 0)
3167 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3168 sp->rtoinfo.srto_max = rto_max;
3169 sp->rtoinfo.srto_min = rto_min;
3177 * 7.1.2 SCTP_ASSOCINFO
3179 * This option is used to tune the maximum retransmission attempts
3180 * of the association.
3181 * Returns an error if the new association retransmission value is
3182 * greater than the sum of the retransmission value of the peer.
3183 * See [SCTP] for more information.
3186 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3189 struct sctp_assocparams assocparams;
3190 struct sctp_association *asoc;
3192 if (optlen != sizeof(struct sctp_assocparams))
3194 if (copy_from_user(&assocparams, optval, optlen))
3197 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3199 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3200 sctp_style(sk, UDP))
3203 /* Set the values to the specific association */
3205 if (assocparams.sasoc_asocmaxrxt != 0) {
3208 struct sctp_transport *peer_addr;
3210 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3212 path_sum += peer_addr->pathmaxrxt;
3216 /* Only validate asocmaxrxt if we have more than
3217 * one path/transport. We do this because path
3218 * retransmissions are only counted when we have more
3222 assocparams.sasoc_asocmaxrxt > path_sum)
3225 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3228 if (assocparams.sasoc_cookie_life != 0)
3229 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3231 /* Set the values to the endpoint */
3232 struct sctp_sock *sp = sctp_sk(sk);
3234 if (assocparams.sasoc_asocmaxrxt != 0)
3235 sp->assocparams.sasoc_asocmaxrxt =
3236 assocparams.sasoc_asocmaxrxt;
3237 if (assocparams.sasoc_cookie_life != 0)
3238 sp->assocparams.sasoc_cookie_life =
3239 assocparams.sasoc_cookie_life;
3245 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3247 * This socket option is a boolean flag which turns on or off mapped V4
3248 * addresses. If this option is turned on and the socket is type
3249 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3250 * If this option is turned off, then no mapping will be done of V4
3251 * addresses and a user will receive both PF_INET6 and PF_INET type
3252 * addresses on the socket.
3254 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3257 struct sctp_sock *sp = sctp_sk(sk);
3259 if (optlen < sizeof(int))
3261 if (get_user(val, (int __user *)optval))
3272 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3273 * This option will get or set the maximum size to put in any outgoing
3274 * SCTP DATA chunk. If a message is larger than this size it will be
3275 * fragmented by SCTP into the specified size. Note that the underlying
3276 * SCTP implementation may fragment into smaller sized chunks when the
3277 * PMTU of the underlying association is smaller than the value set by
3278 * the user. The default value for this option is '0' which indicates
3279 * the user is NOT limiting fragmentation and only the PMTU will effect
3280 * SCTP's choice of DATA chunk size. Note also that values set larger
3281 * than the maximum size of an IP datagram will effectively let SCTP
3282 * control fragmentation (i.e. the same as setting this option to 0).
3284 * The following structure is used to access and modify this parameter:
3286 * struct sctp_assoc_value {
3287 * sctp_assoc_t assoc_id;
3288 * uint32_t assoc_value;
3291 * assoc_id: This parameter is ignored for one-to-one style sockets.
3292 * For one-to-many style sockets this parameter indicates which
3293 * association the user is performing an action upon. Note that if
3294 * this field's value is zero then the endpoints default value is
3295 * changed (effecting future associations only).
3296 * assoc_value: This parameter specifies the maximum size in bytes.
3298 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3300 struct sctp_sock *sp = sctp_sk(sk);
3301 struct sctp_assoc_value params;
3302 struct sctp_association *asoc;
3305 if (optlen == sizeof(int)) {
3306 pr_warn_ratelimited(DEPRECATED
3308 "Use of int in maxseg socket option.\n"
3309 "Use struct sctp_assoc_value instead\n",
3310 current->comm, task_pid_nr(current));
3311 if (copy_from_user(&val, optval, optlen))
3313 params.assoc_id = SCTP_FUTURE_ASSOC;
3314 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3315 if (copy_from_user(¶ms, optval, optlen))
3317 val = params.assoc_value;
3322 asoc = sctp_id2assoc(sk, params.assoc_id);
3323 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3324 sctp_style(sk, UDP))
3328 int min_len, max_len;
3329 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3330 sizeof(struct sctp_data_chunk);
3332 min_len = sctp_min_frag_point(sp, datasize);
3333 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3335 if (val < min_len || val > max_len)
3340 asoc->user_frag = val;
3341 sctp_assoc_update_frag_point(asoc);
3343 sp->user_frag = val;
3351 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3353 * Requests that the peer mark the enclosed address as the association
3354 * primary. The enclosed address must be one of the association's
3355 * locally bound addresses. The following structure is used to make a
3356 * set primary request:
3358 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3359 unsigned int optlen)
3361 struct sctp_sock *sp;
3362 struct sctp_association *asoc = NULL;
3363 struct sctp_setpeerprim prim;
3364 struct sctp_chunk *chunk;
3370 if (!sp->ep->asconf_enable)
3373 if (optlen != sizeof(struct sctp_setpeerprim))
3376 if (copy_from_user(&prim, optval, optlen))
3379 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3383 if (!asoc->peer.asconf_capable)
3386 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3389 if (!sctp_state(asoc, ESTABLISHED))
3392 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3396 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3397 return -EADDRNOTAVAIL;
3399 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3400 return -EADDRNOTAVAIL;
3402 /* Allow security module to validate address. */
3403 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3404 (struct sockaddr *)&prim.sspp_addr,
3409 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3410 chunk = sctp_make_asconf_set_prim(asoc,
3411 (union sctp_addr *)&prim.sspp_addr);
3415 err = sctp_send_asconf(asoc, chunk);
3417 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3422 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3423 unsigned int optlen)
3425 struct sctp_setadaptation adaptation;
3427 if (optlen != sizeof(struct sctp_setadaptation))
3429 if (copy_from_user(&adaptation, optval, optlen))
3432 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3438 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3440 * The context field in the sctp_sndrcvinfo structure is normally only
3441 * used when a failed message is retrieved holding the value that was
3442 * sent down on the actual send call. This option allows the setting of
3443 * a default context on an association basis that will be received on
3444 * reading messages from the peer. This is especially helpful in the
3445 * one-2-many model for an application to keep some reference to an
3446 * internal state machine that is processing messages on the
3447 * association. Note that the setting of this value only effects
3448 * received messages from the peer and does not effect the value that is
3449 * saved with outbound messages.
3451 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3452 unsigned int optlen)
3454 struct sctp_sock *sp = sctp_sk(sk);
3455 struct sctp_assoc_value params;
3456 struct sctp_association *asoc;
3458 if (optlen != sizeof(struct sctp_assoc_value))
3460 if (copy_from_user(¶ms, optval, optlen))
3463 asoc = sctp_id2assoc(sk, params.assoc_id);
3464 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3465 sctp_style(sk, UDP))
3469 asoc->default_rcv_context = params.assoc_value;
3474 if (sctp_style(sk, TCP))
3475 params.assoc_id = SCTP_FUTURE_ASSOC;
3477 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3478 params.assoc_id == SCTP_ALL_ASSOC)
3479 sp->default_rcv_context = params.assoc_value;
3481 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3482 params.assoc_id == SCTP_ALL_ASSOC)
3483 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3484 asoc->default_rcv_context = params.assoc_value;
3490 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3492 * This options will at a minimum specify if the implementation is doing
3493 * fragmented interleave. Fragmented interleave, for a one to many
3494 * socket, is when subsequent calls to receive a message may return
3495 * parts of messages from different associations. Some implementations
3496 * may allow you to turn this value on or off. If so, when turned off,
3497 * no fragment interleave will occur (which will cause a head of line
3498 * blocking amongst multiple associations sharing the same one to many
3499 * socket). When this option is turned on, then each receive call may
3500 * come from a different association (thus the user must receive data
3501 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3502 * association each receive belongs to.
3504 * This option takes a boolean value. A non-zero value indicates that
3505 * fragmented interleave is on. A value of zero indicates that
3506 * fragmented interleave is off.
3508 * Note that it is important that an implementation that allows this
3509 * option to be turned on, have it off by default. Otherwise an unaware
3510 * application using the one to many model may become confused and act
3513 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3514 char __user *optval,
3515 unsigned int optlen)
3519 if (optlen != sizeof(int))
3521 if (get_user(val, (int __user *)optval))
3524 sctp_sk(sk)->frag_interleave = !!val;
3526 if (!sctp_sk(sk)->frag_interleave)
3527 sctp_sk(sk)->ep->intl_enable = 0;
3533 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3534 * (SCTP_PARTIAL_DELIVERY_POINT)
3536 * This option will set or get the SCTP partial delivery point. This
3537 * point is the size of a message where the partial delivery API will be
3538 * invoked to help free up rwnd space for the peer. Setting this to a
3539 * lower value will cause partial deliveries to happen more often. The
3540 * calls argument is an integer that sets or gets the partial delivery
3541 * point. Note also that the call will fail if the user attempts to set
3542 * this value larger than the socket receive buffer size.
3544 * Note that any single message having a length smaller than or equal to
3545 * the SCTP partial delivery point will be delivered in one single read
3546 * call as long as the user provided buffer is large enough to hold the
3549 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3550 char __user *optval,
3551 unsigned int optlen)
3555 if (optlen != sizeof(u32))
3557 if (get_user(val, (int __user *)optval))
3560 /* Note: We double the receive buffer from what the user sets
3561 * it to be, also initial rwnd is based on rcvbuf/2.
3563 if (val > (sk->sk_rcvbuf >> 1))
3566 sctp_sk(sk)->pd_point = val;
3568 return 0; /* is this the right error code? */
3572 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3574 * This option will allow a user to change the maximum burst of packets
3575 * that can be emitted by this association. Note that the default value
3576 * is 4, and some implementations may restrict this setting so that it
3577 * can only be lowered.
3579 * NOTE: This text doesn't seem right. Do this on a socket basis with
3580 * future associations inheriting the socket value.
3582 static int sctp_setsockopt_maxburst(struct sock *sk,
3583 char __user *optval,
3584 unsigned int optlen)
3586 struct sctp_sock *sp = sctp_sk(sk);
3587 struct sctp_assoc_value params;
3588 struct sctp_association *asoc;
3590 if (optlen == sizeof(int)) {
3591 pr_warn_ratelimited(DEPRECATED
3593 "Use of int in max_burst socket option deprecated.\n"
3594 "Use struct sctp_assoc_value instead\n",
3595 current->comm, task_pid_nr(current));
3596 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3598 params.assoc_id = SCTP_FUTURE_ASSOC;
3599 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3600 if (copy_from_user(¶ms, optval, optlen))
3605 asoc = sctp_id2assoc(sk, params.assoc_id);
3606 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3607 sctp_style(sk, UDP))
3611 asoc->max_burst = params.assoc_value;
3616 if (sctp_style(sk, TCP))
3617 params.assoc_id = SCTP_FUTURE_ASSOC;
3619 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3620 params.assoc_id == SCTP_ALL_ASSOC)
3621 sp->max_burst = params.assoc_value;
3623 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3624 params.assoc_id == SCTP_ALL_ASSOC)
3625 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3626 asoc->max_burst = params.assoc_value;
3632 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3634 * This set option adds a chunk type that the user is requesting to be
3635 * received only in an authenticated way. Changes to the list of chunks
3636 * will only effect future associations on the socket.
3638 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3639 char __user *optval,
3640 unsigned int optlen)
3642 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3643 struct sctp_authchunk val;
3645 if (!ep->auth_enable)
3648 if (optlen != sizeof(struct sctp_authchunk))
3650 if (copy_from_user(&val, optval, optlen))
3653 switch (val.sauth_chunk) {
3655 case SCTP_CID_INIT_ACK:
3656 case SCTP_CID_SHUTDOWN_COMPLETE:
3661 /* add this chunk id to the endpoint */
3662 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3666 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3668 * This option gets or sets the list of HMAC algorithms that the local
3669 * endpoint requires the peer to use.
3671 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3672 char __user *optval,
3673 unsigned int optlen)
3675 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3676 struct sctp_hmacalgo *hmacs;
3680 if (!ep->auth_enable)
3683 if (optlen < sizeof(struct sctp_hmacalgo))
3685 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3686 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3688 hmacs = memdup_user(optval, optlen);
3690 return PTR_ERR(hmacs);
3692 idents = hmacs->shmac_num_idents;
3693 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3694 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3699 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3706 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3708 * This option will set a shared secret key which is used to build an
3709 * association shared key.
3711 static int sctp_setsockopt_auth_key(struct sock *sk,
3712 char __user *optval,
3713 unsigned int optlen)
3715 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3716 struct sctp_authkey *authkey;
3717 struct sctp_association *asoc;
3720 if (optlen <= sizeof(struct sctp_authkey))
3722 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3725 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3727 authkey = memdup_user(optval, optlen);
3728 if (IS_ERR(authkey))
3729 return PTR_ERR(authkey);
3731 if (authkey->sca_keylength > optlen - sizeof(*authkey))
3734 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3735 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3736 sctp_style(sk, UDP))
3740 ret = sctp_auth_set_key(ep, asoc, authkey);
3744 if (sctp_style(sk, TCP))
3745 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3747 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3748 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3749 ret = sctp_auth_set_key(ep, asoc, authkey);
3756 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3757 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3758 list_for_each_entry(asoc, &ep->asocs, asocs) {
3759 int res = sctp_auth_set_key(ep, asoc, authkey);
3772 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3774 * This option will get or set the active shared key to be used to build
3775 * the association shared key.
3777 static int sctp_setsockopt_active_key(struct sock *sk,
3778 char __user *optval,
3779 unsigned int optlen)
3781 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3782 struct sctp_association *asoc;
3783 struct sctp_authkeyid val;
3786 if (optlen != sizeof(struct sctp_authkeyid))
3788 if (copy_from_user(&val, optval, optlen))
3791 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3792 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3793 sctp_style(sk, UDP))
3797 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3799 if (sctp_style(sk, TCP))
3800 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3802 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3803 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3804 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3809 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3810 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3811 list_for_each_entry(asoc, &ep->asocs, asocs) {
3812 int res = sctp_auth_set_active_key(ep, asoc,
3813 val.scact_keynumber);
3824 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3826 * This set option will delete a shared secret key from use.
3828 static int sctp_setsockopt_del_key(struct sock *sk,
3829 char __user *optval,
3830 unsigned int optlen)
3832 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3833 struct sctp_association *asoc;
3834 struct sctp_authkeyid val;
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_ALL_ASSOC &&
3844 sctp_style(sk, UDP))
3848 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3850 if (sctp_style(sk, TCP))
3851 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3853 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3854 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3855 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3860 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3861 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3862 list_for_each_entry(asoc, &ep->asocs, asocs) {
3863 int res = sctp_auth_del_key_id(ep, asoc,
3864 val.scact_keynumber);
3875 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3877 * This set option will deactivate a shared secret key.
3879 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3880 unsigned int optlen)
3882 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3883 struct sctp_association *asoc;
3884 struct sctp_authkeyid val;
3887 if (optlen != sizeof(struct sctp_authkeyid))
3889 if (copy_from_user(&val, optval, optlen))
3892 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3893 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3894 sctp_style(sk, UDP))
3898 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3900 if (sctp_style(sk, TCP))
3901 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3903 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3904 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3905 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3910 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3911 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3912 list_for_each_entry(asoc, &ep->asocs, asocs) {
3913 int res = sctp_auth_deact_key_id(ep, asoc,
3914 val.scact_keynumber);
3925 * 8.1.23 SCTP_AUTO_ASCONF
3927 * This option will enable or disable the use of the automatic generation of
3928 * ASCONF chunks to add and delete addresses to an existing association. Note
3929 * that this option has two caveats namely: a) it only affects sockets that
3930 * are bound to all addresses available to the SCTP stack, and b) the system
3931 * administrator may have an overriding control that turns the ASCONF feature
3932 * off no matter what setting the socket option may have.
3933 * This option expects an integer boolean flag, where a non-zero value turns on
3934 * the option, and a zero value turns off the option.
3935 * Note. In this implementation, socket operation overrides default parameter
3936 * being set by sysctl as well as FreeBSD implementation
3938 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3939 unsigned int optlen)
3942 struct sctp_sock *sp = sctp_sk(sk);
3944 if (optlen < sizeof(int))
3946 if (get_user(val, (int __user *)optval))
3948 if (!sctp_is_ep_boundall(sk) && val)
3950 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3953 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3954 if (val == 0 && sp->do_auto_asconf) {
3955 list_del(&sp->auto_asconf_list);
3956 sp->do_auto_asconf = 0;
3957 } else if (val && !sp->do_auto_asconf) {
3958 list_add_tail(&sp->auto_asconf_list,
3959 &sock_net(sk)->sctp.auto_asconf_splist);
3960 sp->do_auto_asconf = 1;
3962 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3967 * SCTP_PEER_ADDR_THLDS
3969 * This option allows us to alter the partially failed threshold for one or all
3970 * transports in an association. See Section 6.1 of:
3971 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3973 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3974 char __user *optval,
3975 unsigned int optlen)
3977 struct sctp_paddrthlds val;
3978 struct sctp_transport *trans;
3979 struct sctp_association *asoc;
3981 if (optlen < sizeof(struct sctp_paddrthlds))
3983 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3984 sizeof(struct sctp_paddrthlds)))
3987 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3988 trans = sctp_addr_id2transport(sk, &val.spt_address,
3993 if (val.spt_pathmaxrxt)
3994 trans->pathmaxrxt = val.spt_pathmaxrxt;
3995 trans->pf_retrans = val.spt_pathpfthld;
4000 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4001 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4002 sctp_style(sk, UDP))
4006 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4008 if (val.spt_pathmaxrxt)
4009 trans->pathmaxrxt = val.spt_pathmaxrxt;
4010 trans->pf_retrans = val.spt_pathpfthld;
4013 if (val.spt_pathmaxrxt)
4014 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4015 asoc->pf_retrans = val.spt_pathpfthld;
4017 struct sctp_sock *sp = sctp_sk(sk);
4019 if (val.spt_pathmaxrxt)
4020 sp->pathmaxrxt = val.spt_pathmaxrxt;
4021 sp->pf_retrans = val.spt_pathpfthld;
4027 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4028 char __user *optval,
4029 unsigned int optlen)
4033 if (optlen < sizeof(int))
4035 if (get_user(val, (int __user *) optval))
4038 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4043 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4044 char __user *optval,
4045 unsigned int optlen)
4049 if (optlen < sizeof(int))
4051 if (get_user(val, (int __user *) optval))
4054 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4059 static int sctp_setsockopt_pr_supported(struct sock *sk,
4060 char __user *optval,
4061 unsigned int optlen)
4063 struct sctp_assoc_value params;
4064 struct sctp_association *asoc;
4066 if (optlen != sizeof(params))
4069 if (copy_from_user(¶ms, optval, optlen))
4072 asoc = sctp_id2assoc(sk, params.assoc_id);
4073 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4074 sctp_style(sk, UDP))
4077 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4082 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4083 char __user *optval,
4084 unsigned int optlen)
4086 struct sctp_sock *sp = sctp_sk(sk);
4087 struct sctp_default_prinfo info;
4088 struct sctp_association *asoc;
4089 int retval = -EINVAL;
4091 if (optlen != sizeof(info))
4094 if (copy_from_user(&info, optval, sizeof(info))) {
4099 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4102 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4105 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4106 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4107 sctp_style(sk, UDP))
4113 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4114 asoc->default_timetolive = info.pr_value;
4118 if (sctp_style(sk, TCP))
4119 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4121 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4122 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4123 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4124 sp->default_timetolive = info.pr_value;
4127 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4128 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4129 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4130 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4131 asoc->default_timetolive = info.pr_value;
4139 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4140 char __user *optval,
4141 unsigned int optlen)
4143 struct sctp_assoc_value params;
4144 struct sctp_association *asoc;
4145 int retval = -EINVAL;
4147 if (optlen != sizeof(params))
4150 if (copy_from_user(¶ms, optval, optlen)) {
4155 asoc = sctp_id2assoc(sk, params.assoc_id);
4156 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4157 sctp_style(sk, UDP))
4160 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4168 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4169 char __user *optval,
4170 unsigned int optlen)
4172 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4173 struct sctp_assoc_value params;
4174 struct sctp_association *asoc;
4175 int retval = -EINVAL;
4177 if (optlen != sizeof(params))
4180 if (copy_from_user(¶ms, optval, optlen)) {
4185 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4188 asoc = sctp_id2assoc(sk, params.assoc_id);
4189 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4190 sctp_style(sk, UDP))
4196 asoc->strreset_enable = params.assoc_value;
4200 if (sctp_style(sk, TCP))
4201 params.assoc_id = SCTP_FUTURE_ASSOC;
4203 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4204 params.assoc_id == SCTP_ALL_ASSOC)
4205 ep->strreset_enable = params.assoc_value;
4207 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4208 params.assoc_id == SCTP_ALL_ASSOC)
4209 list_for_each_entry(asoc, &ep->asocs, asocs)
4210 asoc->strreset_enable = params.assoc_value;
4216 static int sctp_setsockopt_reset_streams(struct sock *sk,
4217 char __user *optval,
4218 unsigned int optlen)
4220 struct sctp_reset_streams *params;
4221 struct sctp_association *asoc;
4222 int retval = -EINVAL;
4224 if (optlen < sizeof(*params))
4226 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4227 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4228 sizeof(__u16) * sizeof(*params));
4230 params = memdup_user(optval, optlen);
4232 return PTR_ERR(params);
4234 if (params->srs_number_streams * sizeof(__u16) >
4235 optlen - sizeof(*params))
4238 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4242 retval = sctp_send_reset_streams(asoc, params);
4249 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4250 char __user *optval,
4251 unsigned int optlen)
4253 struct sctp_association *asoc;
4254 sctp_assoc_t associd;
4255 int retval = -EINVAL;
4257 if (optlen != sizeof(associd))
4260 if (copy_from_user(&associd, optval, optlen)) {
4265 asoc = sctp_id2assoc(sk, associd);
4269 retval = sctp_send_reset_assoc(asoc);
4275 static int sctp_setsockopt_add_streams(struct sock *sk,
4276 char __user *optval,
4277 unsigned int optlen)
4279 struct sctp_association *asoc;
4280 struct sctp_add_streams params;
4281 int retval = -EINVAL;
4283 if (optlen != sizeof(params))
4286 if (copy_from_user(¶ms, optval, optlen)) {
4291 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4295 retval = sctp_send_add_streams(asoc, ¶ms);
4301 static int sctp_setsockopt_scheduler(struct sock *sk,
4302 char __user *optval,
4303 unsigned int optlen)
4305 struct sctp_sock *sp = sctp_sk(sk);
4306 struct sctp_association *asoc;
4307 struct sctp_assoc_value params;
4310 if (optlen < sizeof(params))
4313 optlen = sizeof(params);
4314 if (copy_from_user(¶ms, optval, optlen))
4317 if (params.assoc_value > SCTP_SS_MAX)
4320 asoc = sctp_id2assoc(sk, params.assoc_id);
4321 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4322 sctp_style(sk, UDP))
4326 return sctp_sched_set_sched(asoc, params.assoc_value);
4328 if (sctp_style(sk, TCP))
4329 params.assoc_id = SCTP_FUTURE_ASSOC;
4331 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4332 params.assoc_id == SCTP_ALL_ASSOC)
4333 sp->default_ss = params.assoc_value;
4335 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4336 params.assoc_id == SCTP_ALL_ASSOC) {
4337 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4338 int ret = sctp_sched_set_sched(asoc,
4339 params.assoc_value);
4349 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4350 char __user *optval,
4351 unsigned int optlen)
4353 struct sctp_stream_value params;
4354 struct sctp_association *asoc;
4355 int retval = -EINVAL;
4357 if (optlen < sizeof(params))
4360 optlen = sizeof(params);
4361 if (copy_from_user(¶ms, optval, optlen)) {
4366 asoc = sctp_id2assoc(sk, params.assoc_id);
4367 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4368 sctp_style(sk, UDP))
4372 retval = sctp_sched_set_value(asoc, params.stream_id,
4373 params.stream_value, GFP_KERNEL);
4379 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4380 int ret = sctp_sched_set_value(asoc, params.stream_id,
4381 params.stream_value, GFP_KERNEL);
4382 if (ret && !retval) /* try to return the 1st error. */
4390 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4391 char __user *optval,
4392 unsigned int optlen)
4394 struct sctp_sock *sp = sctp_sk(sk);
4395 struct sctp_assoc_value params;
4396 struct sctp_association *asoc;
4397 int retval = -EINVAL;
4399 if (optlen < sizeof(params))
4402 optlen = sizeof(params);
4403 if (copy_from_user(¶ms, optval, optlen)) {
4408 asoc = sctp_id2assoc(sk, params.assoc_id);
4409 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4410 sctp_style(sk, UDP))
4413 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4418 sp->ep->intl_enable = !!params.assoc_value;
4426 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4427 unsigned int optlen)
4431 if (!sctp_style(sk, TCP))
4434 if (sctp_sk(sk)->ep->base.bind_addr.port)
4437 if (optlen < sizeof(int))
4440 if (get_user(val, (int __user *)optval))
4443 sctp_sk(sk)->reuse = !!val;
4448 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4449 struct sctp_association *asoc)
4451 struct sctp_ulpevent *event;
4453 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4455 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4456 if (sctp_outq_is_empty(&asoc->outqueue)) {
4457 event = sctp_ulpevent_make_sender_dry_event(asoc,
4458 GFP_USER | __GFP_NOWARN);
4462 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4469 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4470 unsigned int optlen)
4472 struct sctp_sock *sp = sctp_sk(sk);
4473 struct sctp_association *asoc;
4474 struct sctp_event param;
4477 if (optlen < sizeof(param))
4480 optlen = sizeof(param);
4481 if (copy_from_user(¶m, optval, optlen))
4484 if (param.se_type < SCTP_SN_TYPE_BASE ||
4485 param.se_type > SCTP_SN_TYPE_MAX)
4488 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4489 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4490 sctp_style(sk, UDP))
4494 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4496 if (sctp_style(sk, TCP))
4497 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4499 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4500 param.se_assoc_id == SCTP_ALL_ASSOC)
4501 sctp_ulpevent_type_set(&sp->subscribe,
4502 param.se_type, param.se_on);
4504 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4505 param.se_assoc_id == SCTP_ALL_ASSOC) {
4506 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4507 int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4517 static int sctp_setsockopt_asconf_supported(struct sock *sk,
4518 char __user *optval,
4519 unsigned int optlen)
4521 struct sctp_assoc_value params;
4522 struct sctp_association *asoc;
4523 struct sctp_endpoint *ep;
4524 int retval = -EINVAL;
4526 if (optlen != sizeof(params))
4529 if (copy_from_user(¶ms, optval, optlen)) {
4534 asoc = sctp_id2assoc(sk, params.assoc_id);
4535 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4536 sctp_style(sk, UDP))
4539 ep = sctp_sk(sk)->ep;
4540 ep->asconf_enable = !!params.assoc_value;
4542 if (ep->asconf_enable && ep->auth_enable) {
4543 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4544 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4553 static int sctp_setsockopt_auth_supported(struct sock *sk,
4554 char __user *optval,
4555 unsigned int optlen)
4557 struct sctp_assoc_value params;
4558 struct sctp_association *asoc;
4559 struct sctp_endpoint *ep;
4560 int retval = -EINVAL;
4562 if (optlen != sizeof(params))
4565 if (copy_from_user(¶ms, optval, optlen)) {
4570 asoc = sctp_id2assoc(sk, params.assoc_id);
4571 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4572 sctp_style(sk, UDP))
4575 ep = sctp_sk(sk)->ep;
4576 if (params.assoc_value) {
4577 retval = sctp_auth_init(ep, GFP_KERNEL);
4580 if (ep->asconf_enable) {
4581 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4582 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4586 ep->auth_enable = !!params.assoc_value;
4593 static int sctp_setsockopt_ecn_supported(struct sock *sk,
4594 char __user *optval,
4595 unsigned int optlen)
4597 struct sctp_assoc_value params;
4598 struct sctp_association *asoc;
4599 int retval = -EINVAL;
4601 if (optlen != sizeof(params))
4604 if (copy_from_user(¶ms, optval, optlen)) {
4609 asoc = sctp_id2assoc(sk, params.assoc_id);
4610 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4611 sctp_style(sk, UDP))
4614 sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
4621 /* API 6.2 setsockopt(), getsockopt()
4623 * Applications use setsockopt() and getsockopt() to set or retrieve
4624 * socket options. Socket options are used to change the default
4625 * behavior of sockets calls. They are described in Section 7.
4629 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4630 * int __user *optlen);
4631 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4634 * sd - the socket descript.
4635 * level - set to IPPROTO_SCTP for all SCTP options.
4636 * optname - the option name.
4637 * optval - the buffer to store the value of the option.
4638 * optlen - the size of the buffer.
4640 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4641 char __user *optval, unsigned int optlen)
4645 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4647 /* I can hardly begin to describe how wrong this is. This is
4648 * so broken as to be worse than useless. The API draft
4649 * REALLY is NOT helpful here... I am not convinced that the
4650 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4651 * are at all well-founded.
4653 if (level != SOL_SCTP) {
4654 struct sctp_af *af = sctp_sk(sk)->pf->af;
4655 retval = af->setsockopt(sk, level, optname, optval, optlen);
4662 case SCTP_SOCKOPT_BINDX_ADD:
4663 /* 'optlen' is the size of the addresses buffer. */
4664 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4665 optlen, SCTP_BINDX_ADD_ADDR);
4668 case SCTP_SOCKOPT_BINDX_REM:
4669 /* 'optlen' is the size of the addresses buffer. */
4670 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4671 optlen, SCTP_BINDX_REM_ADDR);
4674 case SCTP_SOCKOPT_CONNECTX_OLD:
4675 /* 'optlen' is the size of the addresses buffer. */
4676 retval = sctp_setsockopt_connectx_old(sk,
4677 (struct sockaddr __user *)optval,
4681 case SCTP_SOCKOPT_CONNECTX:
4682 /* 'optlen' is the size of the addresses buffer. */
4683 retval = sctp_setsockopt_connectx(sk,
4684 (struct sockaddr __user *)optval,
4688 case SCTP_DISABLE_FRAGMENTS:
4689 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4693 retval = sctp_setsockopt_events(sk, optval, optlen);
4696 case SCTP_AUTOCLOSE:
4697 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4700 case SCTP_PEER_ADDR_PARAMS:
4701 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4704 case SCTP_DELAYED_SACK:
4705 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4707 case SCTP_PARTIAL_DELIVERY_POINT:
4708 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4712 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4714 case SCTP_DEFAULT_SEND_PARAM:
4715 retval = sctp_setsockopt_default_send_param(sk, optval,
4718 case SCTP_DEFAULT_SNDINFO:
4719 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4721 case SCTP_PRIMARY_ADDR:
4722 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4724 case SCTP_SET_PEER_PRIMARY_ADDR:
4725 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4728 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4731 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4733 case SCTP_ASSOCINFO:
4734 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4736 case SCTP_I_WANT_MAPPED_V4_ADDR:
4737 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4740 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4742 case SCTP_ADAPTATION_LAYER:
4743 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4746 retval = sctp_setsockopt_context(sk, optval, optlen);
4748 case SCTP_FRAGMENT_INTERLEAVE:
4749 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4751 case SCTP_MAX_BURST:
4752 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4754 case SCTP_AUTH_CHUNK:
4755 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4757 case SCTP_HMAC_IDENT:
4758 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4761 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4763 case SCTP_AUTH_ACTIVE_KEY:
4764 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4766 case SCTP_AUTH_DELETE_KEY:
4767 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4769 case SCTP_AUTH_DEACTIVATE_KEY:
4770 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4772 case SCTP_AUTO_ASCONF:
4773 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4775 case SCTP_PEER_ADDR_THLDS:
4776 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4778 case SCTP_RECVRCVINFO:
4779 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4781 case SCTP_RECVNXTINFO:
4782 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4784 case SCTP_PR_SUPPORTED:
4785 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4787 case SCTP_DEFAULT_PRINFO:
4788 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4790 case SCTP_RECONFIG_SUPPORTED:
4791 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4793 case SCTP_ENABLE_STREAM_RESET:
4794 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4796 case SCTP_RESET_STREAMS:
4797 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4799 case SCTP_RESET_ASSOC:
4800 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4802 case SCTP_ADD_STREAMS:
4803 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4805 case SCTP_STREAM_SCHEDULER:
4806 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4808 case SCTP_STREAM_SCHEDULER_VALUE:
4809 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4811 case SCTP_INTERLEAVING_SUPPORTED:
4812 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4815 case SCTP_REUSE_PORT:
4816 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4819 retval = sctp_setsockopt_event(sk, optval, optlen);
4821 case SCTP_ASCONF_SUPPORTED:
4822 retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
4824 case SCTP_AUTH_SUPPORTED:
4825 retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
4827 case SCTP_ECN_SUPPORTED:
4828 retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
4831 retval = -ENOPROTOOPT;
4841 /* API 3.1.6 connect() - UDP Style Syntax
4843 * An application may use the connect() call in the UDP model to initiate an
4844 * association without sending data.
4848 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4850 * sd: the socket descriptor to have a new association added to.
4852 * nam: the address structure (either struct sockaddr_in or struct
4853 * sockaddr_in6 defined in RFC2553 [7]).
4855 * len: the size of the address.
4857 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4858 int addr_len, int flags)
4864 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4867 /* Validate addr_len before calling common connect/connectx routine. */
4868 af = sctp_get_af_specific(addr->sa_family);
4869 if (af && addr_len >= af->sockaddr_len)
4870 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4876 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4877 int addr_len, int flags)
4879 if (addr_len < sizeof(uaddr->sa_family))
4882 if (uaddr->sa_family == AF_UNSPEC)
4885 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4888 /* FIXME: Write comments. */
4889 static int sctp_disconnect(struct sock *sk, int flags)
4891 return -EOPNOTSUPP; /* STUB */
4894 /* 4.1.4 accept() - TCP Style Syntax
4896 * Applications use accept() call to remove an established SCTP
4897 * association from the accept queue of the endpoint. A new socket
4898 * descriptor will be returned from accept() to represent the newly
4899 * formed association.
4901 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4903 struct sctp_sock *sp;
4904 struct sctp_endpoint *ep;
4905 struct sock *newsk = NULL;
4906 struct sctp_association *asoc;
4915 if (!sctp_style(sk, TCP)) {
4916 error = -EOPNOTSUPP;
4920 if (!sctp_sstate(sk, LISTENING)) {
4925 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4927 error = sctp_wait_for_accept(sk, timeo);
4931 /* We treat the list of associations on the endpoint as the accept
4932 * queue and pick the first association on the list.
4934 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4936 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4942 /* Populate the fields of the newsk from the oldsk and migrate the
4943 * asoc to the newsk.
4945 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4947 sk_common_release(newsk);
4957 /* The SCTP ioctl handler. */
4958 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4965 * SEQPACKET-style sockets in LISTENING state are valid, for
4966 * SCTP, so only discard TCP-style sockets in LISTENING state.
4968 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4973 struct sk_buff *skb;
4974 unsigned int amount = 0;
4976 skb = skb_peek(&sk->sk_receive_queue);
4979 * We will only return the amount of this packet since
4980 * that is all that will be read.
4984 rc = put_user(amount, (int __user *)arg);
4996 /* This is the function which gets called during socket creation to
4997 * initialized the SCTP-specific portion of the sock.
4998 * The sock structure should already be zero-filled memory.
5000 static int sctp_init_sock(struct sock *sk)
5002 struct net *net = sock_net(sk);
5003 struct sctp_sock *sp;
5005 pr_debug("%s: sk:%p\n", __func__, sk);
5009 /* Initialize the SCTP per socket area. */
5010 switch (sk->sk_type) {
5011 case SOCK_SEQPACKET:
5012 sp->type = SCTP_SOCKET_UDP;
5015 sp->type = SCTP_SOCKET_TCP;
5018 return -ESOCKTNOSUPPORT;
5021 sk->sk_gso_type = SKB_GSO_SCTP;
5023 /* Initialize default send parameters. These parameters can be
5024 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
5026 sp->default_stream = 0;
5027 sp->default_ppid = 0;
5028 sp->default_flags = 0;
5029 sp->default_context = 0;
5030 sp->default_timetolive = 0;
5032 sp->default_rcv_context = 0;
5033 sp->max_burst = net->sctp.max_burst;
5035 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5037 /* Initialize default setup parameters. These parameters
5038 * can be modified with the SCTP_INITMSG socket option or
5039 * overridden by the SCTP_INIT CMSG.
5041 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5042 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
5043 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5044 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5046 /* Initialize default RTO related parameters. These parameters can
5047 * be modified for with the SCTP_RTOINFO socket option.
5049 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5050 sp->rtoinfo.srto_max = net->sctp.rto_max;
5051 sp->rtoinfo.srto_min = net->sctp.rto_min;
5053 /* Initialize default association related parameters. These parameters
5054 * can be modified with the SCTP_ASSOCINFO socket option.
5056 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5057 sp->assocparams.sasoc_number_peer_destinations = 0;
5058 sp->assocparams.sasoc_peer_rwnd = 0;
5059 sp->assocparams.sasoc_local_rwnd = 0;
5060 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5062 /* Initialize default event subscriptions. By default, all the
5067 /* Default Peer Address Parameters. These defaults can
5068 * be modified via SCTP_PEER_ADDR_PARAMS
5070 sp->hbinterval = net->sctp.hb_interval;
5071 sp->pathmaxrxt = net->sctp.max_retrans_path;
5072 sp->pf_retrans = net->sctp.pf_retrans;
5073 sp->pathmtu = 0; /* allow default discovery */
5074 sp->sackdelay = net->sctp.sack_timeout;
5076 sp->param_flags = SPP_HB_ENABLE |
5078 SPP_SACKDELAY_ENABLE;
5079 sp->default_ss = SCTP_SS_DEFAULT;
5081 /* If enabled no SCTP message fragmentation will be performed.
5082 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5084 sp->disable_fragments = 0;
5086 /* Enable Nagle algorithm by default. */
5089 sp->recvrcvinfo = 0;
5090 sp->recvnxtinfo = 0;
5092 /* Enable by default. */
5095 /* Auto-close idle associations after the configured
5096 * number of seconds. A value of 0 disables this
5097 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5098 * for UDP-style sockets only.
5102 /* User specified fragmentation limit. */
5105 sp->adaptation_ind = 0;
5107 sp->pf = sctp_get_pf_specific(sk->sk_family);
5109 /* Control variables for partial data delivery. */
5110 atomic_set(&sp->pd_mode, 0);
5111 skb_queue_head_init(&sp->pd_lobby);
5112 sp->frag_interleave = 0;
5114 /* Create a per socket endpoint structure. Even if we
5115 * change the data structure relationships, this may still
5116 * be useful for storing pre-connect address information.
5118 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5124 sk->sk_destruct = sctp_destruct_sock;
5126 SCTP_DBG_OBJCNT_INC(sock);
5129 sk_sockets_allocated_inc(sk);
5130 sock_prot_inuse_add(net, sk->sk_prot, 1);
5137 /* Cleanup any SCTP per socket resources. Must be called with
5138 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5140 static void sctp_destroy_sock(struct sock *sk)
5142 struct sctp_sock *sp;
5144 pr_debug("%s: sk:%p\n", __func__, sk);
5146 /* Release our hold on the endpoint. */
5148 /* This could happen during socket init, thus we bail out
5149 * early, since the rest of the below is not setup either.
5154 if (sp->do_auto_asconf) {
5155 sp->do_auto_asconf = 0;
5156 list_del(&sp->auto_asconf_list);
5158 sctp_endpoint_free(sp->ep);
5160 sk_sockets_allocated_dec(sk);
5161 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5165 /* Triggered when there are no references on the socket anymore */
5166 static void sctp_destruct_sock(struct sock *sk)
5168 struct sctp_sock *sp = sctp_sk(sk);
5170 /* Free up the HMAC transform. */
5171 crypto_free_shash(sp->hmac);
5173 inet_sock_destruct(sk);
5176 /* API 4.1.7 shutdown() - TCP Style Syntax
5177 * int shutdown(int socket, int how);
5179 * sd - the socket descriptor of the association to be closed.
5180 * how - Specifies the type of shutdown. The values are
5183 * Disables further receive operations. No SCTP
5184 * protocol action is taken.
5186 * Disables further send operations, and initiates
5187 * the SCTP shutdown sequence.
5189 * Disables further send and receive operations
5190 * and initiates the SCTP shutdown sequence.
5192 static void sctp_shutdown(struct sock *sk, int how)
5194 struct net *net = sock_net(sk);
5195 struct sctp_endpoint *ep;
5197 if (!sctp_style(sk, TCP))
5200 ep = sctp_sk(sk)->ep;
5201 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5202 struct sctp_association *asoc;
5204 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5205 asoc = list_entry(ep->asocs.next,
5206 struct sctp_association, asocs);
5207 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5211 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5212 struct sctp_info *info)
5214 struct sctp_transport *prim;
5215 struct list_head *pos;
5218 memset(info, 0, sizeof(*info));
5220 struct sctp_sock *sp = sctp_sk(sk);
5222 info->sctpi_s_autoclose = sp->autoclose;
5223 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5224 info->sctpi_s_pd_point = sp->pd_point;
5225 info->sctpi_s_nodelay = sp->nodelay;
5226 info->sctpi_s_disable_fragments = sp->disable_fragments;
5227 info->sctpi_s_v4mapped = sp->v4mapped;
5228 info->sctpi_s_frag_interleave = sp->frag_interleave;
5229 info->sctpi_s_type = sp->type;
5234 info->sctpi_tag = asoc->c.my_vtag;
5235 info->sctpi_state = asoc->state;
5236 info->sctpi_rwnd = asoc->a_rwnd;
5237 info->sctpi_unackdata = asoc->unack_data;
5238 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5239 info->sctpi_instrms = asoc->stream.incnt;
5240 info->sctpi_outstrms = asoc->stream.outcnt;
5241 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5242 info->sctpi_inqueue++;
5243 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5244 info->sctpi_outqueue++;
5245 info->sctpi_overall_error = asoc->overall_error_count;
5246 info->sctpi_max_burst = asoc->max_burst;
5247 info->sctpi_maxseg = asoc->frag_point;
5248 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5249 info->sctpi_peer_tag = asoc->c.peer_vtag;
5251 mask = asoc->peer.ecn_capable << 1;
5252 mask = (mask | asoc->peer.ipv4_address) << 1;
5253 mask = (mask | asoc->peer.ipv6_address) << 1;
5254 mask = (mask | asoc->peer.hostname_address) << 1;
5255 mask = (mask | asoc->peer.asconf_capable) << 1;
5256 mask = (mask | asoc->peer.prsctp_capable) << 1;
5257 mask = (mask | asoc->peer.auth_capable);
5258 info->sctpi_peer_capable = mask;
5259 mask = asoc->peer.sack_needed << 1;
5260 mask = (mask | asoc->peer.sack_generation) << 1;
5261 mask = (mask | asoc->peer.zero_window_announced);
5262 info->sctpi_peer_sack = mask;
5264 info->sctpi_isacks = asoc->stats.isacks;
5265 info->sctpi_osacks = asoc->stats.osacks;
5266 info->sctpi_opackets = asoc->stats.opackets;
5267 info->sctpi_ipackets = asoc->stats.ipackets;
5268 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5269 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5270 info->sctpi_idupchunks = asoc->stats.idupchunks;
5271 info->sctpi_gapcnt = asoc->stats.gapcnt;
5272 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5273 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5274 info->sctpi_oodchunks = asoc->stats.oodchunks;
5275 info->sctpi_iodchunks = asoc->stats.iodchunks;
5276 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5277 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5279 prim = asoc->peer.primary_path;
5280 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5281 info->sctpi_p_state = prim->state;
5282 info->sctpi_p_cwnd = prim->cwnd;
5283 info->sctpi_p_srtt = prim->srtt;
5284 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5285 info->sctpi_p_hbinterval = prim->hbinterval;
5286 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5287 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5288 info->sctpi_p_ssthresh = prim->ssthresh;
5289 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5290 info->sctpi_p_flight_size = prim->flight_size;
5291 info->sctpi_p_error = prim->error_count;
5295 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5297 /* use callback to avoid exporting the core structure */
5298 void sctp_transport_walk_start(struct rhashtable_iter *iter)
5300 rhltable_walk_enter(&sctp_transport_hashtable, iter);
5302 rhashtable_walk_start(iter);
5305 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5307 rhashtable_walk_stop(iter);
5308 rhashtable_walk_exit(iter);
5311 struct sctp_transport *sctp_transport_get_next(struct net *net,
5312 struct rhashtable_iter *iter)
5314 struct sctp_transport *t;
5316 t = rhashtable_walk_next(iter);
5317 for (; t; t = rhashtable_walk_next(iter)) {
5319 if (PTR_ERR(t) == -EAGAIN)
5324 if (!sctp_transport_hold(t))
5327 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5328 t->asoc->peer.primary_path == t)
5331 sctp_transport_put(t);
5337 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5338 struct rhashtable_iter *iter,
5341 struct sctp_transport *t;
5344 return SEQ_START_TOKEN;
5346 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5349 sctp_transport_put(t);
5355 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5359 struct sctp_ep_common *epb;
5360 struct sctp_hashbucket *head;
5362 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5364 read_lock_bh(&head->lock);
5365 sctp_for_each_hentry(epb, &head->chain) {
5366 err = cb(sctp_ep(epb), p);
5370 read_unlock_bh(&head->lock);
5375 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5377 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5379 const union sctp_addr *laddr,
5380 const union sctp_addr *paddr, void *p)
5382 struct sctp_transport *transport;
5386 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5391 err = cb(transport, p);
5392 sctp_transport_put(transport);
5396 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5398 int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
5399 struct net *net, int *pos, void *p)
5401 struct rhashtable_iter hti;
5402 struct sctp_transport *tsp;
5403 struct sctp_endpoint *ep;
5408 sctp_transport_walk_start(&hti);
5410 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5411 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5413 if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
5414 ret = cb(ep, tsp, p);
5417 sctp_endpoint_put(ep);
5420 sctp_transport_put(tsp);
5422 sctp_transport_walk_stop(&hti);
5425 if (cb_done && !cb_done(ep, tsp, p)) {
5427 sctp_endpoint_put(ep);
5428 sctp_transport_put(tsp);
5431 sctp_endpoint_put(ep);
5432 sctp_transport_put(tsp);
5437 EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
5439 /* 7.2.1 Association Status (SCTP_STATUS)
5441 * Applications can retrieve current status information about an
5442 * association, including association state, peer receiver window size,
5443 * number of unacked data chunks, and number of data chunks pending
5444 * receipt. This information is read-only.
5446 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5447 char __user *optval,
5450 struct sctp_status status;
5451 struct sctp_association *asoc = NULL;
5452 struct sctp_transport *transport;
5453 sctp_assoc_t associd;
5456 if (len < sizeof(status)) {
5461 len = sizeof(status);
5462 if (copy_from_user(&status, optval, len)) {
5467 associd = status.sstat_assoc_id;
5468 asoc = sctp_id2assoc(sk, associd);
5474 transport = asoc->peer.primary_path;
5476 status.sstat_assoc_id = sctp_assoc2id(asoc);
5477 status.sstat_state = sctp_assoc_to_state(asoc);
5478 status.sstat_rwnd = asoc->peer.rwnd;
5479 status.sstat_unackdata = asoc->unack_data;
5481 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5482 status.sstat_instrms = asoc->stream.incnt;
5483 status.sstat_outstrms = asoc->stream.outcnt;
5484 status.sstat_fragmentation_point = asoc->frag_point;
5485 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5486 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5487 transport->af_specific->sockaddr_len);
5488 /* Map ipv4 address into v4-mapped-on-v6 address. */
5489 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5490 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5491 status.sstat_primary.spinfo_state = transport->state;
5492 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5493 status.sstat_primary.spinfo_srtt = transport->srtt;
5494 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5495 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5497 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5498 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5500 if (put_user(len, optlen)) {
5505 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5506 __func__, len, status.sstat_state, status.sstat_rwnd,
5507 status.sstat_assoc_id);
5509 if (copy_to_user(optval, &status, len)) {
5519 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5521 * Applications can retrieve information about a specific peer address
5522 * of an association, including its reachability state, congestion
5523 * window, and retransmission timer values. This information is
5526 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5527 char __user *optval,
5530 struct sctp_paddrinfo pinfo;
5531 struct sctp_transport *transport;
5534 if (len < sizeof(pinfo)) {
5539 len = sizeof(pinfo);
5540 if (copy_from_user(&pinfo, optval, len)) {
5545 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5546 pinfo.spinfo_assoc_id);
5550 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5551 pinfo.spinfo_state = transport->state;
5552 pinfo.spinfo_cwnd = transport->cwnd;
5553 pinfo.spinfo_srtt = transport->srtt;
5554 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5555 pinfo.spinfo_mtu = transport->pathmtu;
5557 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5558 pinfo.spinfo_state = SCTP_ACTIVE;
5560 if (put_user(len, optlen)) {
5565 if (copy_to_user(optval, &pinfo, len)) {
5574 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5576 * This option is a on/off flag. If enabled no SCTP message
5577 * fragmentation will be performed. Instead if a message being sent
5578 * exceeds the current PMTU size, the message will NOT be sent and
5579 * instead a error will be indicated to the user.
5581 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5582 char __user *optval, int __user *optlen)
5586 if (len < sizeof(int))
5590 val = (sctp_sk(sk)->disable_fragments == 1);
5591 if (put_user(len, optlen))
5593 if (copy_to_user(optval, &val, len))
5598 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5600 * This socket option is used to specify various notifications and
5601 * ancillary data the user wishes to receive.
5603 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5606 struct sctp_event_subscribe subscribe;
5607 __u8 *sn_type = (__u8 *)&subscribe;
5612 if (len > sizeof(struct sctp_event_subscribe))
5613 len = sizeof(struct sctp_event_subscribe);
5614 if (put_user(len, optlen))
5617 for (i = 0; i < len; i++)
5618 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5619 SCTP_SN_TYPE_BASE + i);
5621 if (copy_to_user(optval, &subscribe, len))
5627 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5629 * This socket option is applicable to the UDP-style socket only. When
5630 * set it will cause associations that are idle for more than the
5631 * specified number of seconds to automatically close. An association
5632 * being idle is defined an association that has NOT sent or received
5633 * user data. The special value of '0' indicates that no automatic
5634 * close of any associations should be performed. The option expects an
5635 * integer defining the number of seconds of idle time before an
5636 * association is closed.
5638 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5640 /* Applicable to UDP-style socket only */
5641 if (sctp_style(sk, TCP))
5643 if (len < sizeof(int))
5646 if (put_user(len, optlen))
5648 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5653 /* Helper routine to branch off an association to a new socket. */
5654 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5656 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5657 struct sctp_sock *sp = sctp_sk(sk);
5658 struct socket *sock;
5661 /* Do not peel off from one netns to another one. */
5662 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5668 /* An association cannot be branched off from an already peeled-off
5669 * socket, nor is this supported for tcp style sockets.
5671 if (!sctp_style(sk, UDP))
5674 /* Create a new socket. */
5675 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5679 sctp_copy_sock(sock->sk, sk, asoc);
5681 /* Make peeled-off sockets more like 1-1 accepted sockets.
5682 * Set the daddr and initialize id to something more random and also
5683 * copy over any ip options.
5685 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
5686 sp->pf->copy_ip_options(sk, sock->sk);
5688 /* Populate the fields of the newsk from the oldsk and migrate the
5689 * asoc to the newsk.
5691 err = sctp_sock_migrate(sk, sock->sk, asoc,
5692 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5702 EXPORT_SYMBOL(sctp_do_peeloff);
5704 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5705 struct file **newfile, unsigned flags)
5707 struct socket *newsock;
5710 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5714 /* Map the socket to an unused fd that can be returned to the user. */
5715 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5717 sock_release(newsock);
5721 *newfile = sock_alloc_file(newsock, 0, NULL);
5722 if (IS_ERR(*newfile)) {
5723 put_unused_fd(retval);
5724 retval = PTR_ERR(*newfile);
5729 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5732 peeloff->sd = retval;
5734 if (flags & SOCK_NONBLOCK)
5735 (*newfile)->f_flags |= O_NONBLOCK;
5740 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5742 sctp_peeloff_arg_t peeloff;
5743 struct file *newfile = NULL;
5746 if (len < sizeof(sctp_peeloff_arg_t))
5748 len = sizeof(sctp_peeloff_arg_t);
5749 if (copy_from_user(&peeloff, optval, len))
5752 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5756 /* Return the fd mapped to the new socket. */
5757 if (put_user(len, optlen)) {
5759 put_unused_fd(retval);
5763 if (copy_to_user(optval, &peeloff, len)) {
5765 put_unused_fd(retval);
5768 fd_install(retval, newfile);
5773 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5774 char __user *optval, int __user *optlen)
5776 sctp_peeloff_flags_arg_t peeloff;
5777 struct file *newfile = NULL;
5780 if (len < sizeof(sctp_peeloff_flags_arg_t))
5782 len = sizeof(sctp_peeloff_flags_arg_t);
5783 if (copy_from_user(&peeloff, optval, len))
5786 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5787 &newfile, peeloff.flags);
5791 /* Return the fd mapped to the new socket. */
5792 if (put_user(len, optlen)) {
5794 put_unused_fd(retval);
5798 if (copy_to_user(optval, &peeloff, len)) {
5800 put_unused_fd(retval);
5803 fd_install(retval, newfile);
5808 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5810 * Applications can enable or disable heartbeats for any peer address of
5811 * an association, modify an address's heartbeat interval, force a
5812 * heartbeat to be sent immediately, and adjust the address's maximum
5813 * number of retransmissions sent before an address is considered
5814 * unreachable. The following structure is used to access and modify an
5815 * address's parameters:
5817 * struct sctp_paddrparams {
5818 * sctp_assoc_t spp_assoc_id;
5819 * struct sockaddr_storage spp_address;
5820 * uint32_t spp_hbinterval;
5821 * uint16_t spp_pathmaxrxt;
5822 * uint32_t spp_pathmtu;
5823 * uint32_t spp_sackdelay;
5824 * uint32_t spp_flags;
5827 * spp_assoc_id - (one-to-many style socket) This is filled in the
5828 * application, and identifies the association for
5830 * spp_address - This specifies which address is of interest.
5831 * spp_hbinterval - This contains the value of the heartbeat interval,
5832 * in milliseconds. If a value of zero
5833 * is present in this field then no changes are to
5834 * be made to this parameter.
5835 * spp_pathmaxrxt - This contains the maximum number of
5836 * retransmissions before this address shall be
5837 * considered unreachable. If a value of zero
5838 * is present in this field then no changes are to
5839 * be made to this parameter.
5840 * spp_pathmtu - When Path MTU discovery is disabled the value
5841 * specified here will be the "fixed" path mtu.
5842 * Note that if the spp_address field is empty
5843 * then all associations on this address will
5844 * have this fixed path mtu set upon them.
5846 * spp_sackdelay - When delayed sack is enabled, this value specifies
5847 * the number of milliseconds that sacks will be delayed
5848 * for. This value will apply to all addresses of an
5849 * association if the spp_address field is empty. Note
5850 * also, that if delayed sack is enabled and this
5851 * value is set to 0, no change is made to the last
5852 * recorded delayed sack timer value.
5854 * spp_flags - These flags are used to control various features
5855 * on an association. The flag field may contain
5856 * zero or more of the following options.
5858 * SPP_HB_ENABLE - Enable heartbeats on the
5859 * specified address. Note that if the address
5860 * field is empty all addresses for the association
5861 * have heartbeats enabled upon them.
5863 * SPP_HB_DISABLE - Disable heartbeats on the
5864 * speicifed address. Note that if the address
5865 * field is empty all addresses for the association
5866 * will have their heartbeats disabled. Note also
5867 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5868 * mutually exclusive, only one of these two should
5869 * be specified. Enabling both fields will have
5870 * undetermined results.
5872 * SPP_HB_DEMAND - Request a user initiated heartbeat
5873 * to be made immediately.
5875 * SPP_PMTUD_ENABLE - This field will enable PMTU
5876 * discovery upon the specified address. Note that
5877 * if the address feild is empty then all addresses
5878 * on the association are effected.
5880 * SPP_PMTUD_DISABLE - This field will disable PMTU
5881 * discovery upon the specified address. Note that
5882 * if the address feild is empty then all addresses
5883 * on the association are effected. Not also that
5884 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5885 * exclusive. Enabling both will have undetermined
5888 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5889 * on delayed sack. The time specified in spp_sackdelay
5890 * is used to specify the sack delay for this address. Note
5891 * that if spp_address is empty then all addresses will
5892 * enable delayed sack and take on the sack delay
5893 * value specified in spp_sackdelay.
5894 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5895 * off delayed sack. If the spp_address field is blank then
5896 * delayed sack is disabled for the entire association. Note
5897 * also that this field is mutually exclusive to
5898 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5901 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5902 * setting of the IPV6 flow label value. The value is
5903 * contained in the spp_ipv6_flowlabel field.
5904 * Upon retrieval, this flag will be set to indicate that
5905 * the spp_ipv6_flowlabel field has a valid value returned.
5906 * If a specific destination address is set (in the
5907 * spp_address field), then the value returned is that of
5908 * the address. If just an association is specified (and
5909 * no address), then the association's default flow label
5910 * is returned. If neither an association nor a destination
5911 * is specified, then the socket's default flow label is
5912 * returned. For non-IPv6 sockets, this flag will be left
5915 * SPP_DSCP: Setting this flag enables the setting of the
5916 * Differentiated Services Code Point (DSCP) value
5917 * associated with either the association or a specific
5918 * address. The value is obtained in the spp_dscp field.
5919 * Upon retrieval, this flag will be set to indicate that
5920 * the spp_dscp field has a valid value returned. If a
5921 * specific destination address is set when called (in the
5922 * spp_address field), then that specific destination
5923 * address's DSCP value is returned. If just an association
5924 * is specified, then the association's default DSCP is
5925 * returned. If neither an association nor a destination is
5926 * specified, then the socket's default DSCP is returned.
5928 * spp_ipv6_flowlabel
5929 * - This field is used in conjunction with the
5930 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5931 * The 20 least significant bits are used for the flow
5932 * label. This setting has precedence over any IPv6-layer
5935 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5936 * and contains the DSCP. The 6 most significant bits are
5937 * used for the DSCP. This setting has precedence over any
5938 * IPv4- or IPv6- layer setting.
5940 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5941 char __user *optval, int __user *optlen)
5943 struct sctp_paddrparams params;
5944 struct sctp_transport *trans = NULL;
5945 struct sctp_association *asoc = NULL;
5946 struct sctp_sock *sp = sctp_sk(sk);
5948 if (len >= sizeof(params))
5949 len = sizeof(params);
5950 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5951 spp_ipv6_flowlabel), 4))
5952 len = ALIGN(offsetof(struct sctp_paddrparams,
5953 spp_ipv6_flowlabel), 4);
5957 if (copy_from_user(¶ms, optval, len))
5960 /* If an address other than INADDR_ANY is specified, and
5961 * no transport is found, then the request is invalid.
5963 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5964 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5965 params.spp_assoc_id);
5967 pr_debug("%s: failed no transport\n", __func__);
5972 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5973 * socket is a one to many style socket, and an association
5974 * was not found, then the id was invalid.
5976 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5977 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5978 sctp_style(sk, UDP)) {
5979 pr_debug("%s: failed no association\n", __func__);
5984 /* Fetch transport values. */
5985 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5986 params.spp_pathmtu = trans->pathmtu;
5987 params.spp_pathmaxrxt = trans->pathmaxrxt;
5988 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5990 /*draft-11 doesn't say what to return in spp_flags*/
5991 params.spp_flags = trans->param_flags;
5992 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5993 params.spp_ipv6_flowlabel = trans->flowlabel &
5994 SCTP_FLOWLABEL_VAL_MASK;
5995 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5997 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5998 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5999 params.spp_flags |= SPP_DSCP;
6002 /* Fetch association values. */
6003 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
6004 params.spp_pathmtu = asoc->pathmtu;
6005 params.spp_pathmaxrxt = asoc->pathmaxrxt;
6006 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
6008 /*draft-11 doesn't say what to return in spp_flags*/
6009 params.spp_flags = asoc->param_flags;
6010 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6011 params.spp_ipv6_flowlabel = asoc->flowlabel &
6012 SCTP_FLOWLABEL_VAL_MASK;
6013 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6015 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
6016 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6017 params.spp_flags |= SPP_DSCP;
6020 /* Fetch socket values. */
6021 params.spp_hbinterval = sp->hbinterval;
6022 params.spp_pathmtu = sp->pathmtu;
6023 params.spp_sackdelay = sp->sackdelay;
6024 params.spp_pathmaxrxt = sp->pathmaxrxt;
6026 /*draft-11 doesn't say what to return in spp_flags*/
6027 params.spp_flags = sp->param_flags;
6028 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6029 params.spp_ipv6_flowlabel = sp->flowlabel &
6030 SCTP_FLOWLABEL_VAL_MASK;
6031 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6033 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6034 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6035 params.spp_flags |= SPP_DSCP;
6039 if (copy_to_user(optval, ¶ms, len))
6042 if (put_user(len, optlen))
6049 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6051 * This option will effect the way delayed acks are performed. This
6052 * option allows you to get or set the delayed ack time, in
6053 * milliseconds. It also allows changing the delayed ack frequency.
6054 * Changing the frequency to 1 disables the delayed sack algorithm. If
6055 * the assoc_id is 0, then this sets or gets the endpoints default
6056 * values. If the assoc_id field is non-zero, then the set or get
6057 * effects the specified association for the one to many model (the
6058 * assoc_id field is ignored by the one to one model). Note that if
6059 * sack_delay or sack_freq are 0 when setting this option, then the
6060 * current values will remain unchanged.
6062 * struct sctp_sack_info {
6063 * sctp_assoc_t sack_assoc_id;
6064 * uint32_t sack_delay;
6065 * uint32_t sack_freq;
6068 * sack_assoc_id - This parameter, indicates which association the user
6069 * is performing an action upon. Note that if this field's value is
6070 * zero then the endpoints default value is changed (effecting future
6071 * associations only).
6073 * sack_delay - This parameter contains the number of milliseconds that
6074 * the user is requesting the delayed ACK timer be set to. Note that
6075 * this value is defined in the standard to be between 200 and 500
6078 * sack_freq - This parameter contains the number of packets that must
6079 * be received before a sack is sent without waiting for the delay
6080 * timer to expire. The default value for this is 2, setting this
6081 * value to 1 will disable the delayed sack algorithm.
6083 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6084 char __user *optval,
6087 struct sctp_sack_info params;
6088 struct sctp_association *asoc = NULL;
6089 struct sctp_sock *sp = sctp_sk(sk);
6091 if (len >= sizeof(struct sctp_sack_info)) {
6092 len = sizeof(struct sctp_sack_info);
6094 if (copy_from_user(¶ms, optval, len))
6096 } else if (len == sizeof(struct sctp_assoc_value)) {
6097 pr_warn_ratelimited(DEPRECATED
6099 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6100 "Use struct sctp_sack_info instead\n",
6101 current->comm, task_pid_nr(current));
6102 if (copy_from_user(¶ms, optval, len))
6107 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6108 * socket is a one to many style socket, and an association
6109 * was not found, then the id was invalid.
6111 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6112 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6113 sctp_style(sk, UDP))
6117 /* Fetch association values. */
6118 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6119 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6120 params.sack_freq = asoc->sackfreq;
6123 params.sack_delay = 0;
6124 params.sack_freq = 1;
6127 /* Fetch socket values. */
6128 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6129 params.sack_delay = sp->sackdelay;
6130 params.sack_freq = sp->sackfreq;
6132 params.sack_delay = 0;
6133 params.sack_freq = 1;
6137 if (copy_to_user(optval, ¶ms, len))
6140 if (put_user(len, optlen))
6146 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6148 * Applications can specify protocol parameters for the default association
6149 * initialization. The option name argument to setsockopt() and getsockopt()
6152 * Setting initialization parameters is effective only on an unconnected
6153 * socket (for UDP-style sockets only future associations are effected
6154 * by the change). With TCP-style sockets, this option is inherited by
6155 * sockets derived from a listener socket.
6157 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6159 if (len < sizeof(struct sctp_initmsg))
6161 len = sizeof(struct sctp_initmsg);
6162 if (put_user(len, optlen))
6164 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6170 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6171 char __user *optval, int __user *optlen)
6173 struct sctp_association *asoc;
6175 struct sctp_getaddrs getaddrs;
6176 struct sctp_transport *from;
6178 union sctp_addr temp;
6179 struct sctp_sock *sp = sctp_sk(sk);
6184 if (len < sizeof(struct sctp_getaddrs))
6187 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6190 /* For UDP-style sockets, id specifies the association to query. */
6191 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6195 to = optval + offsetof(struct sctp_getaddrs, addrs);
6196 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6198 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6200 memcpy(&temp, &from->ipaddr, sizeof(temp));
6201 addrlen = sctp_get_pf_specific(sk->sk_family)
6202 ->addr_to_user(sp, &temp);
6203 if (space_left < addrlen)
6205 if (copy_to_user(to, &temp, addrlen))
6209 space_left -= addrlen;
6212 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6214 bytes_copied = ((char __user *)to) - optval;
6215 if (put_user(bytes_copied, optlen))
6221 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6222 size_t space_left, int *bytes_copied)
6224 struct sctp_sockaddr_entry *addr;
6225 union sctp_addr temp;
6228 struct net *net = sock_net(sk);
6231 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6235 if ((PF_INET == sk->sk_family) &&
6236 (AF_INET6 == addr->a.sa.sa_family))
6238 if ((PF_INET6 == sk->sk_family) &&
6239 inet_v6_ipv6only(sk) &&
6240 (AF_INET == addr->a.sa.sa_family))
6242 memcpy(&temp, &addr->a, sizeof(temp));
6243 if (!temp.v4.sin_port)
6244 temp.v4.sin_port = htons(port);
6246 addrlen = sctp_get_pf_specific(sk->sk_family)
6247 ->addr_to_user(sctp_sk(sk), &temp);
6249 if (space_left < addrlen) {
6253 memcpy(to, &temp, addrlen);
6257 space_left -= addrlen;
6258 *bytes_copied += addrlen;
6266 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6267 char __user *optval, int __user *optlen)
6269 struct sctp_bind_addr *bp;
6270 struct sctp_association *asoc;
6272 struct sctp_getaddrs getaddrs;
6273 struct sctp_sockaddr_entry *addr;
6275 union sctp_addr temp;
6276 struct sctp_sock *sp = sctp_sk(sk);
6280 int bytes_copied = 0;
6284 if (len < sizeof(struct sctp_getaddrs))
6287 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6291 * For UDP-style sockets, id specifies the association to query.
6292 * If the id field is set to the value '0' then the locally bound
6293 * addresses are returned without regard to any particular
6296 if (0 == getaddrs.assoc_id) {
6297 bp = &sctp_sk(sk)->ep->base.bind_addr;
6299 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6302 bp = &asoc->base.bind_addr;
6305 to = optval + offsetof(struct sctp_getaddrs, addrs);
6306 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6308 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6312 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6313 * addresses from the global local address list.
6315 if (sctp_list_single_entry(&bp->address_list)) {
6316 addr = list_entry(bp->address_list.next,
6317 struct sctp_sockaddr_entry, list);
6318 if (sctp_is_any(sk, &addr->a)) {
6319 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6320 space_left, &bytes_copied);
6330 /* Protection on the bound address list is not needed since
6331 * in the socket option context we hold a socket lock and
6332 * thus the bound address list can't change.
6334 list_for_each_entry(addr, &bp->address_list, list) {
6335 memcpy(&temp, &addr->a, sizeof(temp));
6336 addrlen = sctp_get_pf_specific(sk->sk_family)
6337 ->addr_to_user(sp, &temp);
6338 if (space_left < addrlen) {
6339 err = -ENOMEM; /*fixme: right error?*/
6342 memcpy(buf, &temp, addrlen);
6344 bytes_copied += addrlen;
6346 space_left -= addrlen;
6350 if (copy_to_user(to, addrs, bytes_copied)) {
6354 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6358 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6359 * but we can't change it anymore.
6361 if (put_user(bytes_copied, optlen))
6368 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6370 * Requests that the local SCTP stack use the enclosed peer address as
6371 * the association primary. The enclosed address must be one of the
6372 * association peer's addresses.
6374 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6375 char __user *optval, int __user *optlen)
6377 struct sctp_prim prim;
6378 struct sctp_association *asoc;
6379 struct sctp_sock *sp = sctp_sk(sk);
6381 if (len < sizeof(struct sctp_prim))
6384 len = sizeof(struct sctp_prim);
6386 if (copy_from_user(&prim, optval, len))
6389 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6393 if (!asoc->peer.primary_path)
6396 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6397 asoc->peer.primary_path->af_specific->sockaddr_len);
6399 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6400 (union sctp_addr *)&prim.ssp_addr);
6402 if (put_user(len, optlen))
6404 if (copy_to_user(optval, &prim, len))
6411 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6413 * Requests that the local endpoint set the specified Adaptation Layer
6414 * Indication parameter for all future INIT and INIT-ACK exchanges.
6416 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6417 char __user *optval, int __user *optlen)
6419 struct sctp_setadaptation adaptation;
6421 if (len < sizeof(struct sctp_setadaptation))
6424 len = sizeof(struct sctp_setadaptation);
6426 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6428 if (put_user(len, optlen))
6430 if (copy_to_user(optval, &adaptation, len))
6438 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6440 * Applications that wish to use the sendto() system call may wish to
6441 * specify a default set of parameters that would normally be supplied
6442 * through the inclusion of ancillary data. This socket option allows
6443 * such an application to set the default sctp_sndrcvinfo structure.
6446 * The application that wishes to use this socket option simply passes
6447 * in to this call the sctp_sndrcvinfo structure defined in Section
6448 * 5.2.2) The input parameters accepted by this call include
6449 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6450 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6451 * to this call if the caller is using the UDP model.
6453 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6455 static int sctp_getsockopt_default_send_param(struct sock *sk,
6456 int len, char __user *optval,
6459 struct sctp_sock *sp = sctp_sk(sk);
6460 struct sctp_association *asoc;
6461 struct sctp_sndrcvinfo info;
6463 if (len < sizeof(info))
6468 if (copy_from_user(&info, optval, len))
6471 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6472 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6473 sctp_style(sk, UDP))
6477 info.sinfo_stream = asoc->default_stream;
6478 info.sinfo_flags = asoc->default_flags;
6479 info.sinfo_ppid = asoc->default_ppid;
6480 info.sinfo_context = asoc->default_context;
6481 info.sinfo_timetolive = asoc->default_timetolive;
6483 info.sinfo_stream = sp->default_stream;
6484 info.sinfo_flags = sp->default_flags;
6485 info.sinfo_ppid = sp->default_ppid;
6486 info.sinfo_context = sp->default_context;
6487 info.sinfo_timetolive = sp->default_timetolive;
6490 if (put_user(len, optlen))
6492 if (copy_to_user(optval, &info, len))
6498 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6499 * (SCTP_DEFAULT_SNDINFO)
6501 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6502 char __user *optval,
6505 struct sctp_sock *sp = sctp_sk(sk);
6506 struct sctp_association *asoc;
6507 struct sctp_sndinfo info;
6509 if (len < sizeof(info))
6514 if (copy_from_user(&info, optval, len))
6517 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6518 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6519 sctp_style(sk, UDP))
6523 info.snd_sid = asoc->default_stream;
6524 info.snd_flags = asoc->default_flags;
6525 info.snd_ppid = asoc->default_ppid;
6526 info.snd_context = asoc->default_context;
6528 info.snd_sid = sp->default_stream;
6529 info.snd_flags = sp->default_flags;
6530 info.snd_ppid = sp->default_ppid;
6531 info.snd_context = sp->default_context;
6534 if (put_user(len, optlen))
6536 if (copy_to_user(optval, &info, len))
6544 * 7.1.5 SCTP_NODELAY
6546 * Turn on/off any Nagle-like algorithm. This means that packets are
6547 * generally sent as soon as possible and no unnecessary delays are
6548 * introduced, at the cost of more packets in the network. Expects an
6549 * integer boolean flag.
6552 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6553 char __user *optval, int __user *optlen)
6557 if (len < sizeof(int))
6561 val = (sctp_sk(sk)->nodelay == 1);
6562 if (put_user(len, optlen))
6564 if (copy_to_user(optval, &val, len))
6571 * 7.1.1 SCTP_RTOINFO
6573 * The protocol parameters used to initialize and bound retransmission
6574 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6575 * and modify these parameters.
6576 * All parameters are time values, in milliseconds. A value of 0, when
6577 * modifying the parameters, indicates that the current value should not
6581 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6582 char __user *optval,
6583 int __user *optlen) {
6584 struct sctp_rtoinfo rtoinfo;
6585 struct sctp_association *asoc;
6587 if (len < sizeof (struct sctp_rtoinfo))
6590 len = sizeof(struct sctp_rtoinfo);
6592 if (copy_from_user(&rtoinfo, optval, len))
6595 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6597 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6598 sctp_style(sk, UDP))
6601 /* Values corresponding to the specific association. */
6603 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6604 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6605 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6607 /* Values corresponding to the endpoint. */
6608 struct sctp_sock *sp = sctp_sk(sk);
6610 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6611 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6612 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6615 if (put_user(len, optlen))
6618 if (copy_to_user(optval, &rtoinfo, len))
6626 * 7.1.2 SCTP_ASSOCINFO
6628 * This option is used to tune the maximum retransmission attempts
6629 * of the association.
6630 * Returns an error if the new association retransmission value is
6631 * greater than the sum of the retransmission value of the peer.
6632 * See [SCTP] for more information.
6635 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6636 char __user *optval,
6640 struct sctp_assocparams assocparams;
6641 struct sctp_association *asoc;
6642 struct list_head *pos;
6645 if (len < sizeof (struct sctp_assocparams))
6648 len = sizeof(struct sctp_assocparams);
6650 if (copy_from_user(&assocparams, optval, len))
6653 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6655 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6656 sctp_style(sk, UDP))
6659 /* Values correspoinding to the specific association */
6661 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6662 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6663 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6664 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6666 list_for_each(pos, &asoc->peer.transport_addr_list) {
6670 assocparams.sasoc_number_peer_destinations = cnt;
6672 /* Values corresponding to the endpoint */
6673 struct sctp_sock *sp = sctp_sk(sk);
6675 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6676 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6677 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6678 assocparams.sasoc_cookie_life =
6679 sp->assocparams.sasoc_cookie_life;
6680 assocparams.sasoc_number_peer_destinations =
6682 sasoc_number_peer_destinations;
6685 if (put_user(len, optlen))
6688 if (copy_to_user(optval, &assocparams, len))
6695 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6697 * This socket option is a boolean flag which turns on or off mapped V4
6698 * addresses. If this option is turned on and the socket is type
6699 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6700 * If this option is turned off, then no mapping will be done of V4
6701 * addresses and a user will receive both PF_INET6 and PF_INET type
6702 * addresses on the socket.
6704 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6705 char __user *optval, int __user *optlen)
6708 struct sctp_sock *sp = sctp_sk(sk);
6710 if (len < sizeof(int))
6715 if (put_user(len, optlen))
6717 if (copy_to_user(optval, &val, len))
6724 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6725 * (chapter and verse is quoted at sctp_setsockopt_context())
6727 static int sctp_getsockopt_context(struct sock *sk, int len,
6728 char __user *optval, int __user *optlen)
6730 struct sctp_assoc_value params;
6731 struct sctp_association *asoc;
6733 if (len < sizeof(struct sctp_assoc_value))
6736 len = sizeof(struct sctp_assoc_value);
6738 if (copy_from_user(¶ms, optval, len))
6741 asoc = sctp_id2assoc(sk, params.assoc_id);
6742 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6743 sctp_style(sk, UDP))
6746 params.assoc_value = asoc ? asoc->default_rcv_context
6747 : sctp_sk(sk)->default_rcv_context;
6749 if (put_user(len, optlen))
6751 if (copy_to_user(optval, ¶ms, len))
6758 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6759 * This option will get or set the maximum size to put in any outgoing
6760 * SCTP DATA chunk. If a message is larger than this size it will be
6761 * fragmented by SCTP into the specified size. Note that the underlying
6762 * SCTP implementation may fragment into smaller sized chunks when the
6763 * PMTU of the underlying association is smaller than the value set by
6764 * the user. The default value for this option is '0' which indicates
6765 * the user is NOT limiting fragmentation and only the PMTU will effect
6766 * SCTP's choice of DATA chunk size. Note also that values set larger
6767 * than the maximum size of an IP datagram will effectively let SCTP
6768 * control fragmentation (i.e. the same as setting this option to 0).
6770 * The following structure is used to access and modify this parameter:
6772 * struct sctp_assoc_value {
6773 * sctp_assoc_t assoc_id;
6774 * uint32_t assoc_value;
6777 * assoc_id: This parameter is ignored for one-to-one style sockets.
6778 * For one-to-many style sockets this parameter indicates which
6779 * association the user is performing an action upon. Note that if
6780 * this field's value is zero then the endpoints default value is
6781 * changed (effecting future associations only).
6782 * assoc_value: This parameter specifies the maximum size in bytes.
6784 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6785 char __user *optval, int __user *optlen)
6787 struct sctp_assoc_value params;
6788 struct sctp_association *asoc;
6790 if (len == sizeof(int)) {
6791 pr_warn_ratelimited(DEPRECATED
6793 "Use of int in maxseg socket option.\n"
6794 "Use struct sctp_assoc_value instead\n",
6795 current->comm, task_pid_nr(current));
6796 params.assoc_id = SCTP_FUTURE_ASSOC;
6797 } else if (len >= sizeof(struct sctp_assoc_value)) {
6798 len = sizeof(struct sctp_assoc_value);
6799 if (copy_from_user(¶ms, optval, len))
6804 asoc = sctp_id2assoc(sk, params.assoc_id);
6805 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6806 sctp_style(sk, UDP))
6810 params.assoc_value = asoc->frag_point;
6812 params.assoc_value = sctp_sk(sk)->user_frag;
6814 if (put_user(len, optlen))
6816 if (len == sizeof(int)) {
6817 if (copy_to_user(optval, ¶ms.assoc_value, len))
6820 if (copy_to_user(optval, ¶ms, len))
6828 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6829 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6831 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6832 char __user *optval, int __user *optlen)
6836 if (len < sizeof(int))
6841 val = sctp_sk(sk)->frag_interleave;
6842 if (put_user(len, optlen))
6844 if (copy_to_user(optval, &val, len))
6851 * 7.1.25. Set or Get the sctp partial delivery point
6852 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6854 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6855 char __user *optval,
6860 if (len < sizeof(u32))
6865 val = sctp_sk(sk)->pd_point;
6866 if (put_user(len, optlen))
6868 if (copy_to_user(optval, &val, len))
6875 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6876 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6878 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6879 char __user *optval,
6882 struct sctp_assoc_value params;
6883 struct sctp_association *asoc;
6885 if (len == sizeof(int)) {
6886 pr_warn_ratelimited(DEPRECATED
6888 "Use of int in max_burst socket option.\n"
6889 "Use struct sctp_assoc_value instead\n",
6890 current->comm, task_pid_nr(current));
6891 params.assoc_id = SCTP_FUTURE_ASSOC;
6892 } else if (len >= sizeof(struct sctp_assoc_value)) {
6893 len = sizeof(struct sctp_assoc_value);
6894 if (copy_from_user(¶ms, optval, len))
6899 asoc = sctp_id2assoc(sk, params.assoc_id);
6900 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6901 sctp_style(sk, UDP))
6904 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6906 if (len == sizeof(int)) {
6907 if (copy_to_user(optval, ¶ms.assoc_value, len))
6910 if (copy_to_user(optval, ¶ms, len))
6918 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6919 char __user *optval, int __user *optlen)
6921 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6922 struct sctp_hmacalgo __user *p = (void __user *)optval;
6923 struct sctp_hmac_algo_param *hmacs;
6928 if (!ep->auth_enable)
6931 hmacs = ep->auth_hmacs_list;
6932 data_len = ntohs(hmacs->param_hdr.length) -
6933 sizeof(struct sctp_paramhdr);
6935 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6938 len = sizeof(struct sctp_hmacalgo) + data_len;
6939 num_idents = data_len / sizeof(u16);
6941 if (put_user(len, optlen))
6943 if (put_user(num_idents, &p->shmac_num_idents))
6945 for (i = 0; i < num_idents; i++) {
6946 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6948 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6954 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6955 char __user *optval, int __user *optlen)
6957 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6958 struct sctp_authkeyid val;
6959 struct sctp_association *asoc;
6961 if (len < sizeof(struct sctp_authkeyid))
6964 len = sizeof(struct sctp_authkeyid);
6965 if (copy_from_user(&val, optval, len))
6968 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6969 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6973 if (!asoc->peer.auth_capable)
6975 val.scact_keynumber = asoc->active_key_id;
6977 if (!ep->auth_enable)
6979 val.scact_keynumber = ep->active_key_id;
6982 if (put_user(len, optlen))
6984 if (copy_to_user(optval, &val, len))
6990 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6991 char __user *optval, int __user *optlen)
6993 struct sctp_authchunks __user *p = (void __user *)optval;
6994 struct sctp_authchunks val;
6995 struct sctp_association *asoc;
6996 struct sctp_chunks_param *ch;
7000 if (len < sizeof(struct sctp_authchunks))
7003 if (copy_from_user(&val, optval, sizeof(val)))
7006 to = p->gauth_chunks;
7007 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7011 if (!asoc->peer.auth_capable)
7014 ch = asoc->peer.peer_chunks;
7018 /* See if the user provided enough room for all the data */
7019 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7020 if (len < num_chunks)
7023 if (copy_to_user(to, ch->chunks, num_chunks))
7026 len = sizeof(struct sctp_authchunks) + num_chunks;
7027 if (put_user(len, optlen))
7029 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7034 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
7035 char __user *optval, int __user *optlen)
7037 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7038 struct sctp_authchunks __user *p = (void __user *)optval;
7039 struct sctp_authchunks val;
7040 struct sctp_association *asoc;
7041 struct sctp_chunks_param *ch;
7045 if (len < sizeof(struct sctp_authchunks))
7048 if (copy_from_user(&val, optval, sizeof(val)))
7051 to = p->gauth_chunks;
7052 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7053 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7054 sctp_style(sk, UDP))
7058 if (!asoc->peer.auth_capable)
7060 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
7062 if (!ep->auth_enable)
7064 ch = ep->auth_chunk_list;
7069 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7070 if (len < sizeof(struct sctp_authchunks) + num_chunks)
7073 if (copy_to_user(to, ch->chunks, num_chunks))
7076 len = sizeof(struct sctp_authchunks) + num_chunks;
7077 if (put_user(len, optlen))
7079 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7086 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7087 * This option gets the current number of associations that are attached
7088 * to a one-to-many style socket. The option value is an uint32_t.
7090 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7091 char __user *optval, int __user *optlen)
7093 struct sctp_sock *sp = sctp_sk(sk);
7094 struct sctp_association *asoc;
7097 if (sctp_style(sk, TCP))
7100 if (len < sizeof(u32))
7105 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7109 if (put_user(len, optlen))
7111 if (copy_to_user(optval, &val, len))
7118 * 8.1.23 SCTP_AUTO_ASCONF
7119 * See the corresponding setsockopt entry as description
7121 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7122 char __user *optval, int __user *optlen)
7126 if (len < sizeof(int))
7130 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7132 if (put_user(len, optlen))
7134 if (copy_to_user(optval, &val, len))
7140 * 8.2.6. Get the Current Identifiers of Associations
7141 * (SCTP_GET_ASSOC_ID_LIST)
7143 * This option gets the current list of SCTP association identifiers of
7144 * the SCTP associations handled by a one-to-many style socket.
7146 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7147 char __user *optval, int __user *optlen)
7149 struct sctp_sock *sp = sctp_sk(sk);
7150 struct sctp_association *asoc;
7151 struct sctp_assoc_ids *ids;
7154 if (sctp_style(sk, TCP))
7157 if (len < sizeof(struct sctp_assoc_ids))
7160 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7164 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7167 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7169 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7173 ids->gaids_number_of_ids = num;
7175 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7176 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7179 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7189 * SCTP_PEER_ADDR_THLDS
7191 * This option allows us to fetch the partially failed threshold for one or all
7192 * transports in an association. See Section 6.1 of:
7193 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7195 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7196 char __user *optval,
7200 struct sctp_paddrthlds val;
7201 struct sctp_transport *trans;
7202 struct sctp_association *asoc;
7204 if (len < sizeof(struct sctp_paddrthlds))
7206 len = sizeof(struct sctp_paddrthlds);
7207 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7210 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7211 trans = sctp_addr_id2transport(sk, &val.spt_address,
7216 val.spt_pathmaxrxt = trans->pathmaxrxt;
7217 val.spt_pathpfthld = trans->pf_retrans;
7222 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7223 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7224 sctp_style(sk, UDP))
7228 val.spt_pathpfthld = asoc->pf_retrans;
7229 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7231 struct sctp_sock *sp = sctp_sk(sk);
7233 val.spt_pathpfthld = sp->pf_retrans;
7234 val.spt_pathmaxrxt = sp->pathmaxrxt;
7238 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7245 * SCTP_GET_ASSOC_STATS
7247 * This option retrieves local per endpoint statistics. It is modeled
7248 * after OpenSolaris' implementation
7250 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7251 char __user *optval,
7254 struct sctp_assoc_stats sas;
7255 struct sctp_association *asoc = NULL;
7257 /* User must provide at least the assoc id */
7258 if (len < sizeof(sctp_assoc_t))
7261 /* Allow the struct to grow and fill in as much as possible */
7262 len = min_t(size_t, len, sizeof(sas));
7264 if (copy_from_user(&sas, optval, len))
7267 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7271 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7272 sas.sas_gapcnt = asoc->stats.gapcnt;
7273 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7274 sas.sas_osacks = asoc->stats.osacks;
7275 sas.sas_isacks = asoc->stats.isacks;
7276 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7277 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7278 sas.sas_oodchunks = asoc->stats.oodchunks;
7279 sas.sas_iodchunks = asoc->stats.iodchunks;
7280 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7281 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7282 sas.sas_idupchunks = asoc->stats.idupchunks;
7283 sas.sas_opackets = asoc->stats.opackets;
7284 sas.sas_ipackets = asoc->stats.ipackets;
7286 /* New high max rto observed, will return 0 if not a single
7287 * RTO update took place. obs_rto_ipaddr will be bogus
7290 sas.sas_maxrto = asoc->stats.max_obs_rto;
7291 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7292 sizeof(struct sockaddr_storage));
7294 /* Mark beginning of a new observation period */
7295 asoc->stats.max_obs_rto = asoc->rto_min;
7297 if (put_user(len, optlen))
7300 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7302 if (copy_to_user(optval, &sas, len))
7308 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7309 char __user *optval,
7314 if (len < sizeof(int))
7318 if (sctp_sk(sk)->recvrcvinfo)
7320 if (put_user(len, optlen))
7322 if (copy_to_user(optval, &val, len))
7328 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7329 char __user *optval,
7334 if (len < sizeof(int))
7338 if (sctp_sk(sk)->recvnxtinfo)
7340 if (put_user(len, optlen))
7342 if (copy_to_user(optval, &val, len))
7348 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7349 char __user *optval,
7352 struct sctp_assoc_value params;
7353 struct sctp_association *asoc;
7354 int retval = -EFAULT;
7356 if (len < sizeof(params)) {
7361 len = sizeof(params);
7362 if (copy_from_user(¶ms, optval, len))
7365 asoc = sctp_id2assoc(sk, params.assoc_id);
7366 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7367 sctp_style(sk, UDP)) {
7372 params.assoc_value = asoc ? asoc->peer.prsctp_capable
7373 : sctp_sk(sk)->ep->prsctp_enable;
7375 if (put_user(len, optlen))
7378 if (copy_to_user(optval, ¶ms, len))
7387 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7388 char __user *optval,
7391 struct sctp_default_prinfo info;
7392 struct sctp_association *asoc;
7393 int retval = -EFAULT;
7395 if (len < sizeof(info)) {
7401 if (copy_from_user(&info, optval, len))
7404 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7405 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7406 sctp_style(sk, UDP)) {
7412 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7413 info.pr_value = asoc->default_timetolive;
7415 struct sctp_sock *sp = sctp_sk(sk);
7417 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7418 info.pr_value = sp->default_timetolive;
7421 if (put_user(len, optlen))
7424 if (copy_to_user(optval, &info, len))
7433 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7434 char __user *optval,
7437 struct sctp_prstatus params;
7438 struct sctp_association *asoc;
7440 int retval = -EINVAL;
7442 if (len < sizeof(params))
7445 len = sizeof(params);
7446 if (copy_from_user(¶ms, optval, len)) {
7451 policy = params.sprstat_policy;
7452 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7453 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7456 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7460 if (policy == SCTP_PR_SCTP_ALL) {
7461 params.sprstat_abandoned_unsent = 0;
7462 params.sprstat_abandoned_sent = 0;
7463 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7464 params.sprstat_abandoned_unsent +=
7465 asoc->abandoned_unsent[policy];
7466 params.sprstat_abandoned_sent +=
7467 asoc->abandoned_sent[policy];
7470 params.sprstat_abandoned_unsent =
7471 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7472 params.sprstat_abandoned_sent =
7473 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7476 if (put_user(len, optlen)) {
7481 if (copy_to_user(optval, ¶ms, len)) {
7492 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7493 char __user *optval,
7496 struct sctp_stream_out_ext *streamoute;
7497 struct sctp_association *asoc;
7498 struct sctp_prstatus params;
7499 int retval = -EINVAL;
7502 if (len < sizeof(params))
7505 len = sizeof(params);
7506 if (copy_from_user(¶ms, optval, len)) {
7511 policy = params.sprstat_policy;
7512 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7513 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7516 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7517 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7520 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7522 /* Not allocated yet, means all stats are 0 */
7523 params.sprstat_abandoned_unsent = 0;
7524 params.sprstat_abandoned_sent = 0;
7529 if (policy == SCTP_PR_SCTP_ALL) {
7530 params.sprstat_abandoned_unsent = 0;
7531 params.sprstat_abandoned_sent = 0;
7532 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7533 params.sprstat_abandoned_unsent +=
7534 streamoute->abandoned_unsent[policy];
7535 params.sprstat_abandoned_sent +=
7536 streamoute->abandoned_sent[policy];
7539 params.sprstat_abandoned_unsent =
7540 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7541 params.sprstat_abandoned_sent =
7542 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7545 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7556 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7557 char __user *optval,
7560 struct sctp_assoc_value params;
7561 struct sctp_association *asoc;
7562 int retval = -EFAULT;
7564 if (len < sizeof(params)) {
7569 len = sizeof(params);
7570 if (copy_from_user(¶ms, optval, len))
7573 asoc = sctp_id2assoc(sk, params.assoc_id);
7574 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7575 sctp_style(sk, UDP)) {
7580 params.assoc_value = asoc ? asoc->peer.reconf_capable
7581 : sctp_sk(sk)->ep->reconf_enable;
7583 if (put_user(len, optlen))
7586 if (copy_to_user(optval, ¶ms, len))
7595 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7596 char __user *optval,
7599 struct sctp_assoc_value params;
7600 struct sctp_association *asoc;
7601 int retval = -EFAULT;
7603 if (len < sizeof(params)) {
7608 len = sizeof(params);
7609 if (copy_from_user(¶ms, optval, len))
7612 asoc = sctp_id2assoc(sk, params.assoc_id);
7613 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7614 sctp_style(sk, UDP)) {
7619 params.assoc_value = asoc ? asoc->strreset_enable
7620 : sctp_sk(sk)->ep->strreset_enable;
7622 if (put_user(len, optlen))
7625 if (copy_to_user(optval, ¶ms, len))
7634 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7635 char __user *optval,
7638 struct sctp_assoc_value params;
7639 struct sctp_association *asoc;
7640 int retval = -EFAULT;
7642 if (len < sizeof(params)) {
7647 len = sizeof(params);
7648 if (copy_from_user(¶ms, optval, len))
7651 asoc = sctp_id2assoc(sk, params.assoc_id);
7652 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7653 sctp_style(sk, UDP)) {
7658 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7659 : sctp_sk(sk)->default_ss;
7661 if (put_user(len, optlen))
7664 if (copy_to_user(optval, ¶ms, len))
7673 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7674 char __user *optval,
7677 struct sctp_stream_value params;
7678 struct sctp_association *asoc;
7679 int retval = -EFAULT;
7681 if (len < sizeof(params)) {
7686 len = sizeof(params);
7687 if (copy_from_user(¶ms, optval, len))
7690 asoc = sctp_id2assoc(sk, params.assoc_id);
7696 retval = sctp_sched_get_value(asoc, params.stream_id,
7697 ¶ms.stream_value);
7701 if (put_user(len, optlen)) {
7706 if (copy_to_user(optval, ¶ms, len)) {
7715 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7716 char __user *optval,
7719 struct sctp_assoc_value params;
7720 struct sctp_association *asoc;
7721 int retval = -EFAULT;
7723 if (len < sizeof(params)) {
7728 len = sizeof(params);
7729 if (copy_from_user(¶ms, optval, len))
7732 asoc = sctp_id2assoc(sk, params.assoc_id);
7733 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7734 sctp_style(sk, UDP)) {
7739 params.assoc_value = asoc ? asoc->peer.intl_capable
7740 : sctp_sk(sk)->ep->intl_enable;
7742 if (put_user(len, optlen))
7745 if (copy_to_user(optval, ¶ms, len))
7754 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7755 char __user *optval,
7760 if (len < sizeof(int))
7764 val = sctp_sk(sk)->reuse;
7765 if (put_user(len, optlen))
7768 if (copy_to_user(optval, &val, len))
7774 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7777 struct sctp_association *asoc;
7778 struct sctp_event param;
7781 if (len < sizeof(param))
7784 len = sizeof(param);
7785 if (copy_from_user(¶m, optval, len))
7788 if (param.se_type < SCTP_SN_TYPE_BASE ||
7789 param.se_type > SCTP_SN_TYPE_MAX)
7792 asoc = sctp_id2assoc(sk, param.se_assoc_id);
7793 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7794 sctp_style(sk, UDP))
7797 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7798 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7800 if (put_user(len, optlen))
7803 if (copy_to_user(optval, ¶m, len))
7809 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
7810 char __user *optval,
7813 struct sctp_assoc_value params;
7814 struct sctp_association *asoc;
7815 int retval = -EFAULT;
7817 if (len < sizeof(params)) {
7822 len = sizeof(params);
7823 if (copy_from_user(¶ms, optval, len))
7826 asoc = sctp_id2assoc(sk, params.assoc_id);
7827 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7828 sctp_style(sk, UDP)) {
7833 params.assoc_value = asoc ? asoc->peer.asconf_capable
7834 : sctp_sk(sk)->ep->asconf_enable;
7836 if (put_user(len, optlen))
7839 if (copy_to_user(optval, ¶ms, len))
7848 static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
7849 char __user *optval,
7852 struct sctp_assoc_value params;
7853 struct sctp_association *asoc;
7854 int retval = -EFAULT;
7856 if (len < sizeof(params)) {
7861 len = sizeof(params);
7862 if (copy_from_user(¶ms, optval, len))
7865 asoc = sctp_id2assoc(sk, params.assoc_id);
7866 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7867 sctp_style(sk, UDP)) {
7872 params.assoc_value = asoc ? asoc->peer.auth_capable
7873 : sctp_sk(sk)->ep->auth_enable;
7875 if (put_user(len, optlen))
7878 if (copy_to_user(optval, ¶ms, len))
7887 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
7888 char __user *optval,
7891 struct sctp_assoc_value params;
7892 struct sctp_association *asoc;
7893 int retval = -EFAULT;
7895 if (len < sizeof(params)) {
7900 len = sizeof(params);
7901 if (copy_from_user(¶ms, optval, len))
7904 asoc = sctp_id2assoc(sk, params.assoc_id);
7905 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7906 sctp_style(sk, UDP)) {
7911 params.assoc_value = asoc ? asoc->peer.ecn_capable
7912 : sctp_sk(sk)->ep->ecn_enable;
7914 if (put_user(len, optlen))
7917 if (copy_to_user(optval, ¶ms, len))
7926 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7927 char __user *optval, int __user *optlen)
7932 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7934 /* I can hardly begin to describe how wrong this is. This is
7935 * so broken as to be worse than useless. The API draft
7936 * REALLY is NOT helpful here... I am not convinced that the
7937 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7938 * are at all well-founded.
7940 if (level != SOL_SCTP) {
7941 struct sctp_af *af = sctp_sk(sk)->pf->af;
7943 retval = af->getsockopt(sk, level, optname, optval, optlen);
7947 if (get_user(len, optlen))
7957 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7959 case SCTP_DISABLE_FRAGMENTS:
7960 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7964 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7966 case SCTP_AUTOCLOSE:
7967 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7969 case SCTP_SOCKOPT_PEELOFF:
7970 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7972 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7973 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7975 case SCTP_PEER_ADDR_PARAMS:
7976 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7979 case SCTP_DELAYED_SACK:
7980 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7984 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7986 case SCTP_GET_PEER_ADDRS:
7987 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7990 case SCTP_GET_LOCAL_ADDRS:
7991 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7994 case SCTP_SOCKOPT_CONNECTX3:
7995 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7997 case SCTP_DEFAULT_SEND_PARAM:
7998 retval = sctp_getsockopt_default_send_param(sk, len,
8001 case SCTP_DEFAULT_SNDINFO:
8002 retval = sctp_getsockopt_default_sndinfo(sk, len,
8005 case SCTP_PRIMARY_ADDR:
8006 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
8009 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
8012 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
8014 case SCTP_ASSOCINFO:
8015 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
8017 case SCTP_I_WANT_MAPPED_V4_ADDR:
8018 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
8021 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
8023 case SCTP_GET_PEER_ADDR_INFO:
8024 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
8027 case SCTP_ADAPTATION_LAYER:
8028 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
8032 retval = sctp_getsockopt_context(sk, len, optval, optlen);
8034 case SCTP_FRAGMENT_INTERLEAVE:
8035 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
8038 case SCTP_PARTIAL_DELIVERY_POINT:
8039 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
8042 case SCTP_MAX_BURST:
8043 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
8046 case SCTP_AUTH_CHUNK:
8047 case SCTP_AUTH_DELETE_KEY:
8048 case SCTP_AUTH_DEACTIVATE_KEY:
8049 retval = -EOPNOTSUPP;
8051 case SCTP_HMAC_IDENT:
8052 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
8054 case SCTP_AUTH_ACTIVE_KEY:
8055 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
8057 case SCTP_PEER_AUTH_CHUNKS:
8058 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
8061 case SCTP_LOCAL_AUTH_CHUNKS:
8062 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
8065 case SCTP_GET_ASSOC_NUMBER:
8066 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
8068 case SCTP_GET_ASSOC_ID_LIST:
8069 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
8071 case SCTP_AUTO_ASCONF:
8072 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
8074 case SCTP_PEER_ADDR_THLDS:
8075 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
8077 case SCTP_GET_ASSOC_STATS:
8078 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
8080 case SCTP_RECVRCVINFO:
8081 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
8083 case SCTP_RECVNXTINFO:
8084 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
8086 case SCTP_PR_SUPPORTED:
8087 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
8089 case SCTP_DEFAULT_PRINFO:
8090 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
8093 case SCTP_PR_ASSOC_STATUS:
8094 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
8097 case SCTP_PR_STREAM_STATUS:
8098 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
8101 case SCTP_RECONFIG_SUPPORTED:
8102 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
8105 case SCTP_ENABLE_STREAM_RESET:
8106 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
8109 case SCTP_STREAM_SCHEDULER:
8110 retval = sctp_getsockopt_scheduler(sk, len, optval,
8113 case SCTP_STREAM_SCHEDULER_VALUE:
8114 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
8117 case SCTP_INTERLEAVING_SUPPORTED:
8118 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
8121 case SCTP_REUSE_PORT:
8122 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
8125 retval = sctp_getsockopt_event(sk, len, optval, optlen);
8127 case SCTP_ASCONF_SUPPORTED:
8128 retval = sctp_getsockopt_asconf_supported(sk, len, optval,
8131 case SCTP_AUTH_SUPPORTED:
8132 retval = sctp_getsockopt_auth_supported(sk, len, optval,
8135 case SCTP_ECN_SUPPORTED:
8136 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
8139 retval = -ENOPROTOOPT;
8147 static int sctp_hash(struct sock *sk)
8153 static void sctp_unhash(struct sock *sk)
8158 /* Check if port is acceptable. Possibly find first available port.
8160 * The port hash table (contained in the 'global' SCTP protocol storage
8161 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
8162 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
8163 * list (the list number is the port number hashed out, so as you
8164 * would expect from a hash function, all the ports in a given list have
8165 * such a number that hashes out to the same list number; you were
8166 * expecting that, right?); so each list has a set of ports, with a
8167 * link to the socket (struct sock) that uses it, the port number and
8168 * a fastreuse flag (FIXME: NPI ipg).
8170 static struct sctp_bind_bucket *sctp_bucket_create(
8171 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8173 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8175 struct sctp_sock *sp = sctp_sk(sk);
8176 bool reuse = (sk->sk_reuse || sp->reuse);
8177 struct sctp_bind_hashbucket *head; /* hash list */
8178 kuid_t uid = sock_i_uid(sk);
8179 struct sctp_bind_bucket *pp;
8180 unsigned short snum;
8183 snum = ntohs(addr->v4.sin_port);
8185 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8188 /* Search for an available port. */
8189 int low, high, remaining, index;
8191 struct net *net = sock_net(sk);
8193 inet_get_local_port_range(net, &low, &high);
8194 remaining = (high - low) + 1;
8195 rover = prandom_u32() % remaining + low;
8199 if ((rover < low) || (rover > high))
8201 if (inet_is_local_reserved_port(net, rover))
8203 index = sctp_phashfn(sock_net(sk), rover);
8204 head = &sctp_port_hashtable[index];
8205 spin_lock_bh(&head->lock);
8206 sctp_for_each_hentry(pp, &head->chain)
8207 if ((pp->port == rover) &&
8208 net_eq(sock_net(sk), pp->net))
8212 spin_unlock_bh(&head->lock);
8214 } while (--remaining > 0);
8216 /* Exhausted local port range during search? */
8221 /* OK, here is the one we will use. HEAD (the port
8222 * hash table list entry) is non-NULL and we hold it's
8227 /* We are given an specific port number; we verify
8228 * that it is not being used. If it is used, we will
8229 * exahust the search in the hash list corresponding
8230 * to the port number (snum) - we detect that with the
8231 * port iterator, pp being NULL.
8233 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
8234 spin_lock_bh(&head->lock);
8235 sctp_for_each_hentry(pp, &head->chain) {
8236 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
8243 if (!hlist_empty(&pp->owner)) {
8244 /* We had a port hash table hit - there is an
8245 * available port (pp != NULL) and it is being
8246 * used by other socket (pp->owner not empty); that other
8247 * socket is going to be sk2.
8251 pr_debug("%s: found a possible match\n", __func__);
8253 if ((pp->fastreuse && reuse &&
8254 sk->sk_state != SCTP_SS_LISTENING) ||
8255 (pp->fastreuseport && sk->sk_reuseport &&
8256 uid_eq(pp->fastuid, uid)))
8259 /* Run through the list of sockets bound to the port
8260 * (pp->port) [via the pointers bind_next and
8261 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8262 * we get the endpoint they describe and run through
8263 * the endpoint's list of IP (v4 or v6) addresses,
8264 * comparing each of the addresses with the address of
8265 * the socket sk. If we find a match, then that means
8266 * that this port/socket (sk) combination are already
8269 sk_for_each_bound(sk2, &pp->owner) {
8270 struct sctp_sock *sp2 = sctp_sk(sk2);
8271 struct sctp_endpoint *ep2 = sp2->ep;
8274 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8275 sk2->sk_state != SCTP_SS_LISTENING) ||
8276 (sk->sk_reuseport && sk2->sk_reuseport &&
8277 uid_eq(uid, sock_i_uid(sk2))))
8280 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8287 pr_debug("%s: found a match\n", __func__);
8290 /* If there was a hash table miss, create a new port. */
8292 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
8295 /* In either case (hit or miss), make sure fastreuse is 1 only
8296 * if sk->sk_reuse is too (that is, if the caller requested
8297 * SO_REUSEADDR on this socket -sk-).
8299 if (hlist_empty(&pp->owner)) {
8300 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8305 if (sk->sk_reuseport) {
8306 pp->fastreuseport = 1;
8309 pp->fastreuseport = 0;
8312 if (pp->fastreuse &&
8313 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8316 if (pp->fastreuseport &&
8317 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8318 pp->fastreuseport = 0;
8321 /* We are set, so fill up all the data in the hash table
8322 * entry, tie the socket list information with the rest of the
8323 * sockets FIXME: Blurry, NPI (ipg).
8326 if (!sp->bind_hash) {
8327 inet_sk(sk)->inet_num = snum;
8328 sk_add_bind_node(sk, &pp->owner);
8334 spin_unlock_bh(&head->lock);
8338 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8339 * port is requested.
8341 static int sctp_get_port(struct sock *sk, unsigned short snum)
8343 union sctp_addr addr;
8344 struct sctp_af *af = sctp_sk(sk)->pf->af;
8346 /* Set up a dummy address struct from the sk. */
8347 af->from_sk(&addr, sk);
8348 addr.v4.sin_port = htons(snum);
8350 /* Note: sk->sk_num gets filled in if ephemeral port request. */
8351 return sctp_get_port_local(sk, &addr);
8355 * Move a socket to LISTENING state.
8357 static int sctp_listen_start(struct sock *sk, int backlog)
8359 struct sctp_sock *sp = sctp_sk(sk);
8360 struct sctp_endpoint *ep = sp->ep;
8361 struct crypto_shash *tfm = NULL;
8364 /* Allocate HMAC for generating cookie. */
8365 if (!sp->hmac && sp->sctp_hmac_alg) {
8366 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8367 tfm = crypto_alloc_shash(alg, 0, 0);
8369 net_info_ratelimited("failed to load transform for %s: %ld\n",
8370 sp->sctp_hmac_alg, PTR_ERR(tfm));
8373 sctp_sk(sk)->hmac = tfm;
8377 * If a bind() or sctp_bindx() is not called prior to a listen()
8378 * call that allows new associations to be accepted, the system
8379 * picks an ephemeral port and will choose an address set equivalent
8380 * to binding with a wildcard address.
8382 * This is not currently spelled out in the SCTP sockets
8383 * extensions draft, but follows the practice as seen in TCP
8387 inet_sk_set_state(sk, SCTP_SS_LISTENING);
8388 if (!ep->base.bind_addr.port) {
8389 if (sctp_autobind(sk))
8392 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8393 inet_sk_set_state(sk, SCTP_SS_CLOSED);
8398 sk->sk_max_ack_backlog = backlog;
8399 return sctp_hash_endpoint(ep);
8403 * 4.1.3 / 5.1.3 listen()
8405 * By default, new associations are not accepted for UDP style sockets.
8406 * An application uses listen() to mark a socket as being able to
8407 * accept new associations.
8409 * On TCP style sockets, applications use listen() to ready the SCTP
8410 * endpoint for accepting inbound associations.
8412 * On both types of endpoints a backlog of '0' disables listening.
8414 * Move a socket to LISTENING state.
8416 int sctp_inet_listen(struct socket *sock, int backlog)
8418 struct sock *sk = sock->sk;
8419 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8422 if (unlikely(backlog < 0))
8427 /* Peeled-off sockets are not allowed to listen(). */
8428 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8431 if (sock->state != SS_UNCONNECTED)
8434 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8437 /* If backlog is zero, disable listening. */
8439 if (sctp_sstate(sk, CLOSED))
8443 sctp_unhash_endpoint(ep);
8444 sk->sk_state = SCTP_SS_CLOSED;
8445 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8446 sctp_sk(sk)->bind_hash->fastreuse = 1;
8450 /* If we are already listening, just update the backlog */
8451 if (sctp_sstate(sk, LISTENING))
8452 sk->sk_max_ack_backlog = backlog;
8454 err = sctp_listen_start(sk, backlog);
8466 * This function is done by modeling the current datagram_poll() and the
8467 * tcp_poll(). Note that, based on these implementations, we don't
8468 * lock the socket in this function, even though it seems that,
8469 * ideally, locking or some other mechanisms can be used to ensure
8470 * the integrity of the counters (sndbuf and wmem_alloc) used
8471 * in this place. We assume that we don't need locks either until proven
8474 * Another thing to note is that we include the Async I/O support
8475 * here, again, by modeling the current TCP/UDP code. We don't have
8476 * a good way to test with it yet.
8478 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8480 struct sock *sk = sock->sk;
8481 struct sctp_sock *sp = sctp_sk(sk);
8484 poll_wait(file, sk_sleep(sk), wait);
8486 sock_rps_record_flow(sk);
8488 /* A TCP-style listening socket becomes readable when the accept queue
8491 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8492 return (!list_empty(&sp->ep->asocs)) ?
8493 (EPOLLIN | EPOLLRDNORM) : 0;
8497 /* Is there any exceptional events? */
8498 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8500 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8501 if (sk->sk_shutdown & RCV_SHUTDOWN)
8502 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8503 if (sk->sk_shutdown == SHUTDOWN_MASK)
8506 /* Is it readable? Reconsider this code with TCP-style support. */
8507 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8508 mask |= EPOLLIN | EPOLLRDNORM;
8510 /* The association is either gone or not ready. */
8511 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8514 /* Is it writable? */
8515 if (sctp_writeable(sk)) {
8516 mask |= EPOLLOUT | EPOLLWRNORM;
8518 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8520 * Since the socket is not locked, the buffer
8521 * might be made available after the writeable check and
8522 * before the bit is set. This could cause a lost I/O
8523 * signal. tcp_poll() has a race breaker for this race
8524 * condition. Based on their implementation, we put
8525 * in the following code to cover it as well.
8527 if (sctp_writeable(sk))
8528 mask |= EPOLLOUT | EPOLLWRNORM;
8533 /********************************************************************
8534 * 2nd Level Abstractions
8535 ********************************************************************/
8537 static struct sctp_bind_bucket *sctp_bucket_create(
8538 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8540 struct sctp_bind_bucket *pp;
8542 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8544 SCTP_DBG_OBJCNT_INC(bind_bucket);
8547 INIT_HLIST_HEAD(&pp->owner);
8549 hlist_add_head(&pp->node, &head->chain);
8554 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8555 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8557 if (pp && hlist_empty(&pp->owner)) {
8558 __hlist_del(&pp->node);
8559 kmem_cache_free(sctp_bucket_cachep, pp);
8560 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8564 /* Release this socket's reference to a local port. */
8565 static inline void __sctp_put_port(struct sock *sk)
8567 struct sctp_bind_hashbucket *head =
8568 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8569 inet_sk(sk)->inet_num)];
8570 struct sctp_bind_bucket *pp;
8572 spin_lock(&head->lock);
8573 pp = sctp_sk(sk)->bind_hash;
8574 __sk_del_bind_node(sk);
8575 sctp_sk(sk)->bind_hash = NULL;
8576 inet_sk(sk)->inet_num = 0;
8577 sctp_bucket_destroy(pp);
8578 spin_unlock(&head->lock);
8581 void sctp_put_port(struct sock *sk)
8584 __sctp_put_port(sk);
8589 * The system picks an ephemeral port and choose an address set equivalent
8590 * to binding with a wildcard address.
8591 * One of those addresses will be the primary address for the association.
8592 * This automatically enables the multihoming capability of SCTP.
8594 static int sctp_autobind(struct sock *sk)
8596 union sctp_addr autoaddr;
8600 /* Initialize a local sockaddr structure to INADDR_ANY. */
8601 af = sctp_sk(sk)->pf->af;
8603 port = htons(inet_sk(sk)->inet_num);
8604 af->inaddr_any(&autoaddr, port);
8606 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8609 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8612 * 4.2 The cmsghdr Structure *
8614 * When ancillary data is sent or received, any number of ancillary data
8615 * objects can be specified by the msg_control and msg_controllen members of
8616 * the msghdr structure, because each object is preceded by
8617 * a cmsghdr structure defining the object's length (the cmsg_len member).
8618 * Historically Berkeley-derived implementations have passed only one object
8619 * at a time, but this API allows multiple objects to be
8620 * passed in a single call to sendmsg() or recvmsg(). The following example
8621 * shows two ancillary data objects in a control buffer.
8623 * |<--------------------------- msg_controllen -------------------------->|
8626 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8628 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8631 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8633 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8636 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8637 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8639 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8641 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8648 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8650 struct msghdr *my_msg = (struct msghdr *)msg;
8651 struct cmsghdr *cmsg;
8653 for_each_cmsghdr(cmsg, my_msg) {
8654 if (!CMSG_OK(my_msg, cmsg))
8657 /* Should we parse this header or ignore? */
8658 if (cmsg->cmsg_level != IPPROTO_SCTP)
8661 /* Strictly check lengths following example in SCM code. */
8662 switch (cmsg->cmsg_type) {
8664 /* SCTP Socket API Extension
8665 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8667 * This cmsghdr structure provides information for
8668 * initializing new SCTP associations with sendmsg().
8669 * The SCTP_INITMSG socket option uses this same data
8670 * structure. This structure is not used for
8673 * cmsg_level cmsg_type cmsg_data[]
8674 * ------------ ------------ ----------------------
8675 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8677 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8680 cmsgs->init = CMSG_DATA(cmsg);
8684 /* SCTP Socket API Extension
8685 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8687 * This cmsghdr structure specifies SCTP options for
8688 * sendmsg() and describes SCTP header information
8689 * about a received message through recvmsg().
8691 * cmsg_level cmsg_type cmsg_data[]
8692 * ------------ ------------ ----------------------
8693 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8695 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8698 cmsgs->srinfo = CMSG_DATA(cmsg);
8700 if (cmsgs->srinfo->sinfo_flags &
8701 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8702 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8703 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8708 /* SCTP Socket API Extension
8709 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8711 * This cmsghdr structure specifies SCTP options for
8712 * sendmsg(). This structure and SCTP_RCVINFO replaces
8713 * SCTP_SNDRCV which has been deprecated.
8715 * cmsg_level cmsg_type cmsg_data[]
8716 * ------------ ------------ ---------------------
8717 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8719 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8722 cmsgs->sinfo = CMSG_DATA(cmsg);
8724 if (cmsgs->sinfo->snd_flags &
8725 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8726 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8727 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8731 /* SCTP Socket API Extension
8732 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8734 * This cmsghdr structure specifies SCTP options for sendmsg().
8736 * cmsg_level cmsg_type cmsg_data[]
8737 * ------------ ------------ ---------------------
8738 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8740 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8743 cmsgs->prinfo = CMSG_DATA(cmsg);
8744 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8747 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8748 cmsgs->prinfo->pr_value = 0;
8751 /* SCTP Socket API Extension
8752 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8754 * This cmsghdr structure specifies SCTP options for sendmsg().
8756 * cmsg_level cmsg_type cmsg_data[]
8757 * ------------ ------------ ---------------------
8758 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8760 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8763 cmsgs->authinfo = CMSG_DATA(cmsg);
8765 case SCTP_DSTADDRV4:
8766 case SCTP_DSTADDRV6:
8767 /* SCTP Socket API Extension
8768 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8770 * This cmsghdr structure specifies SCTP options for sendmsg().
8772 * cmsg_level cmsg_type cmsg_data[]
8773 * ------------ ------------ ---------------------
8774 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8775 * ------------ ------------ ---------------------
8776 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8778 cmsgs->addrs_msg = my_msg;
8789 * Wait for a packet..
8790 * Note: This function is the same function as in core/datagram.c
8791 * with a few modifications to make lksctp work.
8793 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8798 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8800 /* Socket errors? */
8801 error = sock_error(sk);
8805 if (!skb_queue_empty(&sk->sk_receive_queue))
8808 /* Socket shut down? */
8809 if (sk->sk_shutdown & RCV_SHUTDOWN)
8812 /* Sequenced packets can come disconnected. If so we report the
8817 /* Is there a good reason to think that we may receive some data? */
8818 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8821 /* Handle signals. */
8822 if (signal_pending(current))
8825 /* Let another process have a go. Since we are going to sleep
8826 * anyway. Note: This may cause odd behaviors if the message
8827 * does not fit in the user's buffer, but this seems to be the
8828 * only way to honor MSG_DONTWAIT realistically.
8831 *timeo_p = schedule_timeout(*timeo_p);
8835 finish_wait(sk_sleep(sk), &wait);
8839 error = sock_intr_errno(*timeo_p);
8842 finish_wait(sk_sleep(sk), &wait);
8847 /* Receive a datagram.
8848 * Note: This is pretty much the same routine as in core/datagram.c
8849 * with a few changes to make lksctp work.
8851 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8852 int noblock, int *err)
8855 struct sk_buff *skb;
8858 timeo = sock_rcvtimeo(sk, noblock);
8860 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8861 MAX_SCHEDULE_TIMEOUT);
8864 /* Again only user level code calls this function,
8865 * so nothing interrupt level
8866 * will suddenly eat the receive_queue.
8868 * Look at current nfs client by the way...
8869 * However, this function was correct in any case. 8)
8871 if (flags & MSG_PEEK) {
8872 skb = skb_peek(&sk->sk_receive_queue);
8874 refcount_inc(&skb->users);
8876 skb = __skb_dequeue(&sk->sk_receive_queue);
8882 /* Caller is allowed not to check sk->sk_err before calling. */
8883 error = sock_error(sk);
8887 if (sk->sk_shutdown & RCV_SHUTDOWN)
8890 if (sk_can_busy_loop(sk)) {
8891 sk_busy_loop(sk, noblock);
8893 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8897 /* User doesn't want to wait. */
8901 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8910 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8911 static void __sctp_write_space(struct sctp_association *asoc)
8913 struct sock *sk = asoc->base.sk;
8915 if (sctp_wspace(asoc) <= 0)
8918 if (waitqueue_active(&asoc->wait))
8919 wake_up_interruptible(&asoc->wait);
8921 if (sctp_writeable(sk)) {
8922 struct socket_wq *wq;
8925 wq = rcu_dereference(sk->sk_wq);
8927 if (waitqueue_active(&wq->wait))
8928 wake_up_interruptible(&wq->wait);
8930 /* Note that we try to include the Async I/O support
8931 * here by modeling from the current TCP/UDP code.
8932 * We have not tested with it yet.
8934 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8935 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8941 static void sctp_wake_up_waiters(struct sock *sk,
8942 struct sctp_association *asoc)
8944 struct sctp_association *tmp = asoc;
8946 /* We do accounting for the sndbuf space per association,
8947 * so we only need to wake our own association.
8949 if (asoc->ep->sndbuf_policy)
8950 return __sctp_write_space(asoc);
8952 /* If association goes down and is just flushing its
8953 * outq, then just normally notify others.
8955 if (asoc->base.dead)
8956 return sctp_write_space(sk);
8958 /* Accounting for the sndbuf space is per socket, so we
8959 * need to wake up others, try to be fair and in case of
8960 * other associations, let them have a go first instead
8961 * of just doing a sctp_write_space() call.
8963 * Note that we reach sctp_wake_up_waiters() only when
8964 * associations free up queued chunks, thus we are under
8965 * lock and the list of associations on a socket is
8966 * guaranteed not to change.
8968 for (tmp = list_next_entry(tmp, asocs); 1;
8969 tmp = list_next_entry(tmp, asocs)) {
8970 /* Manually skip the head element. */
8971 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8973 /* Wake up association. */
8974 __sctp_write_space(tmp);
8975 /* We've reached the end. */
8981 /* Do accounting for the sndbuf space.
8982 * Decrement the used sndbuf space of the corresponding association by the
8983 * data size which was just transmitted(freed).
8985 static void sctp_wfree(struct sk_buff *skb)
8987 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8988 struct sctp_association *asoc = chunk->asoc;
8989 struct sock *sk = asoc->base.sk;
8991 sk_mem_uncharge(sk, skb->truesize);
8992 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8993 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8994 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8995 &sk->sk_wmem_alloc));
8998 struct sctp_shared_key *shkey = chunk->shkey;
9000 /* refcnt == 2 and !list_empty mean after this release, it's
9001 * not being used anywhere, and it's time to notify userland
9002 * that this shkey can be freed if it's been deactivated.
9004 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
9005 refcount_read(&shkey->refcnt) == 2) {
9006 struct sctp_ulpevent *ev;
9008 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
9012 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
9014 sctp_auth_shkey_release(chunk->shkey);
9018 sctp_wake_up_waiters(sk, asoc);
9020 sctp_association_put(asoc);
9023 /* Do accounting for the receive space on the socket.
9024 * Accounting for the association is done in ulpevent.c
9025 * We set this as a destructor for the cloned data skbs so that
9026 * accounting is done at the correct time.
9028 void sctp_sock_rfree(struct sk_buff *skb)
9030 struct sock *sk = skb->sk;
9031 struct sctp_ulpevent *event = sctp_skb2event(skb);
9033 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
9036 * Mimic the behavior of sock_rfree
9038 sk_mem_uncharge(sk, event->rmem_len);
9042 /* Helper function to wait for space in the sndbuf. */
9043 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
9046 struct sock *sk = asoc->base.sk;
9047 long current_timeo = *timeo_p;
9051 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
9054 /* Increment the association's refcnt. */
9055 sctp_association_hold(asoc);
9057 /* Wait on the association specific sndbuf space. */
9059 prepare_to_wait_exclusive(&asoc->wait, &wait,
9060 TASK_INTERRUPTIBLE);
9061 if (asoc->base.dead)
9065 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
9067 if (signal_pending(current))
9068 goto do_interrupted;
9069 if (sk_under_memory_pressure(sk))
9071 if ((int)msg_len <= sctp_wspace(asoc) &&
9072 sk_wmem_schedule(sk, msg_len))
9075 /* Let another process have a go. Since we are going
9079 current_timeo = schedule_timeout(current_timeo);
9081 if (sk != asoc->base.sk)
9084 *timeo_p = current_timeo;
9088 finish_wait(&asoc->wait, &wait);
9090 /* Release the association's refcnt. */
9091 sctp_association_put(asoc);
9104 err = sock_intr_errno(*timeo_p);
9112 void sctp_data_ready(struct sock *sk)
9114 struct socket_wq *wq;
9117 wq = rcu_dereference(sk->sk_wq);
9118 if (skwq_has_sleeper(wq))
9119 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
9120 EPOLLRDNORM | EPOLLRDBAND);
9121 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9125 /* If socket sndbuf has changed, wake up all per association waiters. */
9126 void sctp_write_space(struct sock *sk)
9128 struct sctp_association *asoc;
9130 /* Wake up the tasks in each wait queue. */
9131 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
9132 __sctp_write_space(asoc);
9136 /* Is there any sndbuf space available on the socket?
9138 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
9139 * associations on the same socket. For a UDP-style socket with
9140 * multiple associations, it is possible for it to be "unwriteable"
9141 * prematurely. I assume that this is acceptable because
9142 * a premature "unwriteable" is better than an accidental "writeable" which
9143 * would cause an unwanted block under certain circumstances. For the 1-1
9144 * UDP-style sockets or TCP-style sockets, this code should work.
9147 static bool sctp_writeable(struct sock *sk)
9149 return sk->sk_sndbuf > sk->sk_wmem_queued;
9152 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
9153 * returns immediately with EINPROGRESS.
9155 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9157 struct sock *sk = asoc->base.sk;
9159 long current_timeo = *timeo_p;
9162 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9164 /* Increment the association's refcnt. */
9165 sctp_association_hold(asoc);
9168 prepare_to_wait_exclusive(&asoc->wait, &wait,
9169 TASK_INTERRUPTIBLE);
9172 if (sk->sk_shutdown & RCV_SHUTDOWN)
9174 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9177 if (signal_pending(current))
9178 goto do_interrupted;
9180 if (sctp_state(asoc, ESTABLISHED))
9183 /* Let another process have a go. Since we are going
9187 current_timeo = schedule_timeout(current_timeo);
9190 *timeo_p = current_timeo;
9194 finish_wait(&asoc->wait, &wait);
9196 /* Release the association's refcnt. */
9197 sctp_association_put(asoc);
9202 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9205 err = -ECONNREFUSED;
9209 err = sock_intr_errno(*timeo_p);
9217 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9219 struct sctp_endpoint *ep;
9223 ep = sctp_sk(sk)->ep;
9227 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9228 TASK_INTERRUPTIBLE);
9230 if (list_empty(&ep->asocs)) {
9232 timeo = schedule_timeout(timeo);
9237 if (!sctp_sstate(sk, LISTENING))
9241 if (!list_empty(&ep->asocs))
9244 err = sock_intr_errno(timeo);
9245 if (signal_pending(current))
9253 finish_wait(sk_sleep(sk), &wait);
9258 static void sctp_wait_for_close(struct sock *sk, long timeout)
9263 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9264 if (list_empty(&sctp_sk(sk)->ep->asocs))
9267 timeout = schedule_timeout(timeout);
9269 } while (!signal_pending(current) && timeout);
9271 finish_wait(sk_sleep(sk), &wait);
9274 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9276 struct sk_buff *frag;
9281 /* Don't forget the fragments. */
9282 skb_walk_frags(skb, frag)
9283 sctp_skb_set_owner_r_frag(frag, sk);
9286 sctp_skb_set_owner_r(skb, sk);
9289 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9290 struct sctp_association *asoc)
9292 struct inet_sock *inet = inet_sk(sk);
9293 struct inet_sock *newinet;
9294 struct sctp_sock *sp = sctp_sk(sk);
9295 struct sctp_endpoint *ep = sp->ep;
9297 newsk->sk_type = sk->sk_type;
9298 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9299 newsk->sk_flags = sk->sk_flags;
9300 newsk->sk_tsflags = sk->sk_tsflags;
9301 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9302 newsk->sk_no_check_rx = sk->sk_no_check_rx;
9303 newsk->sk_reuse = sk->sk_reuse;
9304 sctp_sk(newsk)->reuse = sp->reuse;
9306 newsk->sk_shutdown = sk->sk_shutdown;
9307 newsk->sk_destruct = sctp_destruct_sock;
9308 newsk->sk_family = sk->sk_family;
9309 newsk->sk_protocol = IPPROTO_SCTP;
9310 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9311 newsk->sk_sndbuf = sk->sk_sndbuf;
9312 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9313 newsk->sk_lingertime = sk->sk_lingertime;
9314 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9315 newsk->sk_sndtimeo = sk->sk_sndtimeo;
9316 newsk->sk_rxhash = sk->sk_rxhash;
9318 newinet = inet_sk(newsk);
9320 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9321 * getsockname() and getpeername()
9323 newinet->inet_sport = inet->inet_sport;
9324 newinet->inet_saddr = inet->inet_saddr;
9325 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9326 newinet->inet_dport = htons(asoc->peer.port);
9327 newinet->pmtudisc = inet->pmtudisc;
9328 newinet->inet_id = prandom_u32();
9330 newinet->uc_ttl = inet->uc_ttl;
9331 newinet->mc_loop = 1;
9332 newinet->mc_ttl = 1;
9333 newinet->mc_index = 0;
9334 newinet->mc_list = NULL;
9336 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9337 net_enable_timestamp();
9339 /* Set newsk security attributes from orginal sk and connection
9340 * security attribute from ep.
9342 security_sctp_sk_clone(ep, sk, newsk);
9345 static inline void sctp_copy_descendant(struct sock *sk_to,
9346 const struct sock *sk_from)
9348 size_t ancestor_size = sizeof(struct inet_sock);
9350 ancestor_size += sk_from->sk_prot->obj_size;
9351 ancestor_size -= offsetof(struct sctp_sock, pd_lobby);
9352 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9355 /* Populate the fields of the newsk from the oldsk and migrate the assoc
9356 * and its messages to the newsk.
9358 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9359 struct sctp_association *assoc,
9360 enum sctp_socket_type type)
9362 struct sctp_sock *oldsp = sctp_sk(oldsk);
9363 struct sctp_sock *newsp = sctp_sk(newsk);
9364 struct sctp_bind_bucket *pp; /* hash list port iterator */
9365 struct sctp_endpoint *newep = newsp->ep;
9366 struct sk_buff *skb, *tmp;
9367 struct sctp_ulpevent *event;
9368 struct sctp_bind_hashbucket *head;
9371 /* Migrate socket buffer sizes and all the socket level options to the
9374 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9375 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9376 /* Brute force copy old sctp opt. */
9377 sctp_copy_descendant(newsk, oldsk);
9379 /* Restore the ep value that was overwritten with the above structure
9385 /* Hook this new socket in to the bind_hash list. */
9386 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9387 inet_sk(oldsk)->inet_num)];
9388 spin_lock_bh(&head->lock);
9389 pp = sctp_sk(oldsk)->bind_hash;
9390 sk_add_bind_node(newsk, &pp->owner);
9391 sctp_sk(newsk)->bind_hash = pp;
9392 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9393 spin_unlock_bh(&head->lock);
9395 /* Copy the bind_addr list from the original endpoint to the new
9396 * endpoint so that we can handle restarts properly
9398 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9399 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9403 /* New ep's auth_hmacs should be set if old ep's is set, in case
9404 * that net->sctp.auth_enable has been changed to 0 by users and
9405 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9407 if (oldsp->ep->auth_hmacs) {
9408 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9413 sctp_auto_asconf_init(newsp);
9415 /* Move any messages in the old socket's receive queue that are for the
9416 * peeled off association to the new socket's receive queue.
9418 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9419 event = sctp_skb2event(skb);
9420 if (event->asoc == assoc) {
9421 __skb_unlink(skb, &oldsk->sk_receive_queue);
9422 __skb_queue_tail(&newsk->sk_receive_queue, skb);
9423 sctp_skb_set_owner_r_frag(skb, newsk);
9427 /* Clean up any messages pending delivery due to partial
9428 * delivery. Three cases:
9429 * 1) No partial deliver; no work.
9430 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9431 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9433 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9435 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9436 struct sk_buff_head *queue;
9438 /* Decide which queue to move pd_lobby skbs to. */
9439 if (assoc->ulpq.pd_mode) {
9440 queue = &newsp->pd_lobby;
9442 queue = &newsk->sk_receive_queue;
9444 /* Walk through the pd_lobby, looking for skbs that
9445 * need moved to the new socket.
9447 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9448 event = sctp_skb2event(skb);
9449 if (event->asoc == assoc) {
9450 __skb_unlink(skb, &oldsp->pd_lobby);
9451 __skb_queue_tail(queue, skb);
9452 sctp_skb_set_owner_r_frag(skb, newsk);
9456 /* Clear up any skbs waiting for the partial
9457 * delivery to finish.
9459 if (assoc->ulpq.pd_mode)
9460 sctp_clear_pd(oldsk, NULL);
9464 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9466 /* Set the type of socket to indicate that it is peeled off from the
9467 * original UDP-style socket or created with the accept() call on a
9468 * TCP-style socket..
9472 /* Mark the new socket "in-use" by the user so that any packets
9473 * that may arrive on the association after we've moved it are
9474 * queued to the backlog. This prevents a potential race between
9475 * backlog processing on the old socket and new-packet processing
9476 * on the new socket.
9478 * The caller has just allocated newsk so we can guarantee that other
9479 * paths won't try to lock it and then oldsk.
9481 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9482 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
9483 sctp_assoc_migrate(assoc, newsk);
9484 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
9486 /* If the association on the newsk is already closed before accept()
9487 * is called, set RCV_SHUTDOWN flag.
9489 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9490 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9491 newsk->sk_shutdown |= RCV_SHUTDOWN;
9493 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9496 release_sock(newsk);
9502 /* This proto struct describes the ULP interface for SCTP. */
9503 struct proto sctp_prot = {
9505 .owner = THIS_MODULE,
9506 .close = sctp_close,
9507 .disconnect = sctp_disconnect,
9508 .accept = sctp_accept,
9509 .ioctl = sctp_ioctl,
9510 .init = sctp_init_sock,
9511 .destroy = sctp_destroy_sock,
9512 .shutdown = sctp_shutdown,
9513 .setsockopt = sctp_setsockopt,
9514 .getsockopt = sctp_getsockopt,
9515 .sendmsg = sctp_sendmsg,
9516 .recvmsg = sctp_recvmsg,
9518 .backlog_rcv = sctp_backlog_rcv,
9520 .unhash = sctp_unhash,
9521 .no_autobind = true,
9522 .obj_size = sizeof(struct sctp_sock),
9523 .useroffset = offsetof(struct sctp_sock, subscribe),
9524 .usersize = offsetof(struct sctp_sock, initmsg) -
9525 offsetof(struct sctp_sock, subscribe) +
9526 sizeof_field(struct sctp_sock, initmsg),
9527 .sysctl_mem = sysctl_sctp_mem,
9528 .sysctl_rmem = sysctl_sctp_rmem,
9529 .sysctl_wmem = sysctl_sctp_wmem,
9530 .memory_pressure = &sctp_memory_pressure,
9531 .enter_memory_pressure = sctp_enter_memory_pressure,
9532 .memory_allocated = &sctp_memory_allocated,
9533 .sockets_allocated = &sctp_sockets_allocated,
9536 #if IS_ENABLED(CONFIG_IPV6)
9538 #include <net/transp_v6.h>
9539 static void sctp_v6_destroy_sock(struct sock *sk)
9541 sctp_destroy_sock(sk);
9542 inet6_destroy_sock(sk);
9545 struct proto sctpv6_prot = {
9547 .owner = THIS_MODULE,
9548 .close = sctp_close,
9549 .disconnect = sctp_disconnect,
9550 .accept = sctp_accept,
9551 .ioctl = sctp_ioctl,
9552 .init = sctp_init_sock,
9553 .destroy = sctp_v6_destroy_sock,
9554 .shutdown = sctp_shutdown,
9555 .setsockopt = sctp_setsockopt,
9556 .getsockopt = sctp_getsockopt,
9557 .sendmsg = sctp_sendmsg,
9558 .recvmsg = sctp_recvmsg,
9560 .backlog_rcv = sctp_backlog_rcv,
9562 .unhash = sctp_unhash,
9563 .no_autobind = true,
9564 .obj_size = sizeof(struct sctp6_sock),
9565 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9566 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9567 offsetof(struct sctp6_sock, sctp.subscribe) +
9568 sizeof_field(struct sctp6_sock, sctp.initmsg),
9569 .sysctl_mem = sysctl_sctp_mem,
9570 .sysctl_rmem = sysctl_sctp_rmem,
9571 .sysctl_wmem = sysctl_sctp_wmem,
9572 .memory_pressure = &sctp_memory_pressure,
9573 .enter_memory_pressure = sctp_enter_memory_pressure,
9574 .memory_allocated = &sctp_memory_allocated,
9575 .sockets_allocated = &sctp_sockets_allocated,
9577 #endif /* IS_ENABLED(CONFIG_IPV6) */