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(const 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 WRITE_ONCE(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_wmem_queued_add(sk, 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_bh(&net->sctp.addr_wq_lock);
366 list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
367 spin_unlock_bh(&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);
1853 if (unlikely(sinfo->sinfo_stream >= asoc->stream.outcnt)) {
1859 if (sctp_state(asoc, CLOSED)) {
1860 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1864 if (asoc->ep->intl_enable) {
1865 timeo = sock_sndtimeo(sk, 0);
1866 err = sctp_wait_for_connect(asoc, &timeo);
1872 wait_connect = true;
1875 pr_debug("%s: we associated primitively\n", __func__);
1878 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1879 if (IS_ERR(datamsg)) {
1880 err = PTR_ERR(datamsg);
1884 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1886 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1887 sctp_chunk_hold(chunk);
1888 sctp_set_owner_w(chunk);
1889 chunk->transport = transport;
1892 err = sctp_primitive_SEND(net, asoc, datamsg);
1894 sctp_datamsg_free(datamsg);
1898 pr_debug("%s: we sent primitively\n", __func__);
1900 sctp_datamsg_put(datamsg);
1902 if (unlikely(wait_connect)) {
1903 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1904 sctp_wait_for_connect(asoc, &timeo);
1913 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1914 const struct msghdr *msg,
1915 struct sctp_cmsgs *cmsgs)
1917 union sctp_addr *daddr = NULL;
1920 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1921 int len = msg->msg_namelen;
1923 if (len > sizeof(*daddr))
1924 len = sizeof(*daddr);
1926 daddr = (union sctp_addr *)msg->msg_name;
1928 err = sctp_verify_addr(sk, daddr, len);
1930 return ERR_PTR(err);
1936 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1937 struct sctp_sndrcvinfo *sinfo,
1938 struct sctp_cmsgs *cmsgs)
1940 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1941 sinfo->sinfo_stream = asoc->default_stream;
1942 sinfo->sinfo_ppid = asoc->default_ppid;
1943 sinfo->sinfo_context = asoc->default_context;
1944 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1947 sinfo->sinfo_flags = asoc->default_flags;
1950 if (!cmsgs->srinfo && !cmsgs->prinfo)
1951 sinfo->sinfo_timetolive = asoc->default_timetolive;
1953 if (cmsgs->authinfo) {
1954 /* Reuse sinfo_tsn to indicate that authinfo was set and
1955 * sinfo_ssn to save the keyid on tx path.
1957 sinfo->sinfo_tsn = 1;
1958 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1962 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1964 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1965 struct sctp_transport *transport = NULL;
1966 struct sctp_sndrcvinfo _sinfo, *sinfo;
1967 struct sctp_association *asoc, *tmp;
1968 struct sctp_cmsgs cmsgs;
1969 union sctp_addr *daddr;
1974 /* Parse and get snd_info */
1975 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1980 sflags = sinfo->sinfo_flags;
1982 /* Get daddr from msg */
1983 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1984 if (IS_ERR(daddr)) {
1985 err = PTR_ERR(daddr);
1991 /* SCTP_SENDALL process */
1992 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
1993 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
1994 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2001 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2003 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2008 iov_iter_revert(&msg->msg_iter, err);
2014 /* Get and check or create asoc */
2016 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2018 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2023 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2028 asoc = transport->asoc;
2032 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2035 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2041 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2046 /* Update snd_info with the asoc */
2047 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2049 /* Send msg to the asoc */
2050 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2051 if (err < 0 && err != -ESRCH && new)
2052 sctp_association_free(asoc);
2057 return sctp_error(sk, msg->msg_flags, err);
2060 /* This is an extended version of skb_pull() that removes the data from the
2061 * start of a skb even when data is spread across the list of skb's in the
2062 * frag_list. len specifies the total amount of data that needs to be removed.
2063 * when 'len' bytes could be removed from the skb, it returns 0.
2064 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2065 * could not be removed.
2067 static int sctp_skb_pull(struct sk_buff *skb, int len)
2069 struct sk_buff *list;
2070 int skb_len = skb_headlen(skb);
2073 if (len <= skb_len) {
2074 __skb_pull(skb, len);
2078 __skb_pull(skb, skb_len);
2080 skb_walk_frags(skb, list) {
2081 rlen = sctp_skb_pull(list, len);
2082 skb->len -= (len-rlen);
2083 skb->data_len -= (len-rlen);
2094 /* API 3.1.3 recvmsg() - UDP Style Syntax
2096 * ssize_t recvmsg(int socket, struct msghdr *message,
2099 * socket - the socket descriptor of the endpoint.
2100 * message - pointer to the msghdr structure which contains a single
2101 * user message and possibly some ancillary data.
2103 * See Section 5 for complete description of the data
2106 * flags - flags sent or received with the user message, see Section
2107 * 5 for complete description of the flags.
2109 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2110 int noblock, int flags, int *addr_len)
2112 struct sctp_ulpevent *event = NULL;
2113 struct sctp_sock *sp = sctp_sk(sk);
2114 struct sk_buff *skb, *head_skb;
2119 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2120 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2125 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2126 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2131 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2135 /* Get the total length of the skb including any skb's in the
2144 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2146 event = sctp_skb2event(skb);
2151 if (event->chunk && event->chunk->head_skb)
2152 head_skb = event->chunk->head_skb;
2155 sock_recv_ts_and_drops(msg, sk, head_skb);
2156 if (sctp_ulpevent_is_notification(event)) {
2157 msg->msg_flags |= MSG_NOTIFICATION;
2158 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2160 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2163 /* Check if we allow SCTP_NXTINFO. */
2164 if (sp->recvnxtinfo)
2165 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2166 /* Check if we allow SCTP_RCVINFO. */
2167 if (sp->recvrcvinfo)
2168 sctp_ulpevent_read_rcvinfo(event, msg);
2169 /* Check if we allow SCTP_SNDRCVINFO. */
2170 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2171 sctp_ulpevent_read_sndrcvinfo(event, msg);
2175 /* If skb's length exceeds the user's buffer, update the skb and
2176 * push it back to the receive_queue so that the next call to
2177 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2179 if (skb_len > copied) {
2180 msg->msg_flags &= ~MSG_EOR;
2181 if (flags & MSG_PEEK)
2183 sctp_skb_pull(skb, copied);
2184 skb_queue_head(&sk->sk_receive_queue, skb);
2186 /* When only partial message is copied to the user, increase
2187 * rwnd by that amount. If all the data in the skb is read,
2188 * rwnd is updated when the event is freed.
2190 if (!sctp_ulpevent_is_notification(event))
2191 sctp_assoc_rwnd_increase(event->asoc, copied);
2193 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2194 (event->msg_flags & MSG_EOR))
2195 msg->msg_flags |= MSG_EOR;
2197 msg->msg_flags &= ~MSG_EOR;
2200 if (flags & MSG_PEEK) {
2201 /* Release the skb reference acquired after peeking the skb in
2202 * sctp_skb_recv_datagram().
2206 /* Free the event which includes releasing the reference to
2207 * the owner of the skb, freeing the skb and updating the
2210 sctp_ulpevent_free(event);
2217 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2219 * This option is a on/off flag. If enabled no SCTP message
2220 * fragmentation will be performed. Instead if a message being sent
2221 * exceeds the current PMTU size, the message will NOT be sent and
2222 * instead a error will be indicated to the user.
2224 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2225 char __user *optval,
2226 unsigned int optlen)
2230 if (optlen < sizeof(int))
2233 if (get_user(val, (int __user *)optval))
2236 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2241 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2242 unsigned int optlen)
2244 struct sctp_event_subscribe subscribe;
2245 __u8 *sn_type = (__u8 *)&subscribe;
2246 struct sctp_sock *sp = sctp_sk(sk);
2247 struct sctp_association *asoc;
2250 if (optlen > sizeof(struct sctp_event_subscribe))
2253 if (copy_from_user(&subscribe, optval, optlen))
2256 for (i = 0; i < optlen; i++)
2257 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2260 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2261 asoc->subscribe = sctp_sk(sk)->subscribe;
2263 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2264 * if there is no data to be sent or retransmit, the stack will
2265 * immediately send up this notification.
2267 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2268 struct sctp_ulpevent *event;
2270 asoc = sctp_id2assoc(sk, 0);
2271 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2272 event = sctp_ulpevent_make_sender_dry_event(asoc,
2273 GFP_USER | __GFP_NOWARN);
2277 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2284 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2286 * This socket option is applicable to the UDP-style socket only. When
2287 * set it will cause associations that are idle for more than the
2288 * specified number of seconds to automatically close. An association
2289 * being idle is defined an association that has NOT sent or received
2290 * user data. The special value of '0' indicates that no automatic
2291 * close of any associations should be performed. The option expects an
2292 * integer defining the number of seconds of idle time before an
2293 * association is closed.
2295 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2296 unsigned int optlen)
2298 struct sctp_sock *sp = sctp_sk(sk);
2299 struct net *net = sock_net(sk);
2301 /* Applicable to UDP-style socket only */
2302 if (sctp_style(sk, TCP))
2304 if (optlen != sizeof(int))
2306 if (copy_from_user(&sp->autoclose, optval, optlen))
2309 if (sp->autoclose > net->sctp.max_autoclose)
2310 sp->autoclose = net->sctp.max_autoclose;
2315 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2317 * Applications can enable or disable heartbeats for any peer address of
2318 * an association, modify an address's heartbeat interval, force a
2319 * heartbeat to be sent immediately, and adjust the address's maximum
2320 * number of retransmissions sent before an address is considered
2321 * unreachable. The following structure is used to access and modify an
2322 * address's parameters:
2324 * struct sctp_paddrparams {
2325 * sctp_assoc_t spp_assoc_id;
2326 * struct sockaddr_storage spp_address;
2327 * uint32_t spp_hbinterval;
2328 * uint16_t spp_pathmaxrxt;
2329 * uint32_t spp_pathmtu;
2330 * uint32_t spp_sackdelay;
2331 * uint32_t spp_flags;
2332 * uint32_t spp_ipv6_flowlabel;
2336 * spp_assoc_id - (one-to-many style socket) This is filled in the
2337 * application, and identifies the association for
2339 * spp_address - This specifies which address is of interest.
2340 * spp_hbinterval - This contains the value of the heartbeat interval,
2341 * in milliseconds. If a value of zero
2342 * is present in this field then no changes are to
2343 * be made to this parameter.
2344 * spp_pathmaxrxt - This contains the maximum number of
2345 * retransmissions before this address shall be
2346 * considered unreachable. If a value of zero
2347 * is present in this field then no changes are to
2348 * be made to this parameter.
2349 * spp_pathmtu - When Path MTU discovery is disabled the value
2350 * specified here will be the "fixed" path mtu.
2351 * Note that if the spp_address field is empty
2352 * then all associations on this address will
2353 * have this fixed path mtu set upon them.
2355 * spp_sackdelay - When delayed sack is enabled, this value specifies
2356 * the number of milliseconds that sacks will be delayed
2357 * for. This value will apply to all addresses of an
2358 * association if the spp_address field is empty. Note
2359 * also, that if delayed sack is enabled and this
2360 * value is set to 0, no change is made to the last
2361 * recorded delayed sack timer value.
2363 * spp_flags - These flags are used to control various features
2364 * on an association. The flag field may contain
2365 * zero or more of the following options.
2367 * SPP_HB_ENABLE - Enable heartbeats on the
2368 * specified address. Note that if the address
2369 * field is empty all addresses for the association
2370 * have heartbeats enabled upon them.
2372 * SPP_HB_DISABLE - Disable heartbeats on the
2373 * speicifed address. Note that if the address
2374 * field is empty all addresses for the association
2375 * will have their heartbeats disabled. Note also
2376 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2377 * mutually exclusive, only one of these two should
2378 * be specified. Enabling both fields will have
2379 * undetermined results.
2381 * SPP_HB_DEMAND - Request a user initiated heartbeat
2382 * to be made immediately.
2384 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2385 * heartbeat delayis to be set to the value of 0
2388 * SPP_PMTUD_ENABLE - This field will enable PMTU
2389 * discovery upon the specified address. Note that
2390 * if the address feild is empty then all addresses
2391 * on the association are effected.
2393 * SPP_PMTUD_DISABLE - This field will disable PMTU
2394 * discovery upon the specified address. Note that
2395 * if the address feild is empty then all addresses
2396 * on the association are effected. Not also that
2397 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2398 * exclusive. Enabling both will have undetermined
2401 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2402 * on delayed sack. The time specified in spp_sackdelay
2403 * is used to specify the sack delay for this address. Note
2404 * that if spp_address is empty then all addresses will
2405 * enable delayed sack and take on the sack delay
2406 * value specified in spp_sackdelay.
2407 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2408 * off delayed sack. If the spp_address field is blank then
2409 * delayed sack is disabled for the entire association. Note
2410 * also that this field is mutually exclusive to
2411 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2414 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2415 * setting of the IPV6 flow label value. The value is
2416 * contained in the spp_ipv6_flowlabel field.
2417 * Upon retrieval, this flag will be set to indicate that
2418 * the spp_ipv6_flowlabel field has a valid value returned.
2419 * If a specific destination address is set (in the
2420 * spp_address field), then the value returned is that of
2421 * the address. If just an association is specified (and
2422 * no address), then the association's default flow label
2423 * is returned. If neither an association nor a destination
2424 * is specified, then the socket's default flow label is
2425 * returned. For non-IPv6 sockets, this flag will be left
2428 * SPP_DSCP: Setting this flag enables the setting of the
2429 * Differentiated Services Code Point (DSCP) value
2430 * associated with either the association or a specific
2431 * address. The value is obtained in the spp_dscp field.
2432 * Upon retrieval, this flag will be set to indicate that
2433 * the spp_dscp field has a valid value returned. If a
2434 * specific destination address is set when called (in the
2435 * spp_address field), then that specific destination
2436 * address's DSCP value is returned. If just an association
2437 * is specified, then the association's default DSCP is
2438 * returned. If neither an association nor a destination is
2439 * specified, then the socket's default DSCP is returned.
2441 * spp_ipv6_flowlabel
2442 * - This field is used in conjunction with the
2443 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2444 * The 20 least significant bits are used for the flow
2445 * label. This setting has precedence over any IPv6-layer
2448 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2449 * and contains the DSCP. The 6 most significant bits are
2450 * used for the DSCP. This setting has precedence over any
2451 * IPv4- or IPv6- layer setting.
2453 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2454 struct sctp_transport *trans,
2455 struct sctp_association *asoc,
2456 struct sctp_sock *sp,
2459 int sackdelay_change)
2463 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2464 struct net *net = sock_net(trans->asoc->base.sk);
2466 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2471 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2472 * this field is ignored. Note also that a value of zero indicates
2473 * the current setting should be left unchanged.
2475 if (params->spp_flags & SPP_HB_ENABLE) {
2477 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2478 * set. This lets us use 0 value when this flag
2481 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2482 params->spp_hbinterval = 0;
2484 if (params->spp_hbinterval ||
2485 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2488 msecs_to_jiffies(params->spp_hbinterval);
2491 msecs_to_jiffies(params->spp_hbinterval);
2493 sp->hbinterval = params->spp_hbinterval;
2500 trans->param_flags =
2501 (trans->param_flags & ~SPP_HB) | hb_change;
2504 (asoc->param_flags & ~SPP_HB) | hb_change;
2507 (sp->param_flags & ~SPP_HB) | hb_change;
2511 /* When Path MTU discovery is disabled the value specified here will
2512 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2513 * include the flag SPP_PMTUD_DISABLE for this field to have any
2516 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2518 trans->pathmtu = params->spp_pathmtu;
2519 sctp_assoc_sync_pmtu(asoc);
2521 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2523 sp->pathmtu = params->spp_pathmtu;
2529 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2530 (params->spp_flags & SPP_PMTUD_ENABLE);
2531 trans->param_flags =
2532 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2534 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2535 sctp_assoc_sync_pmtu(asoc);
2539 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2542 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2546 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2547 * value of this field is ignored. Note also that a value of zero
2548 * indicates the current setting should be left unchanged.
2550 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2553 msecs_to_jiffies(params->spp_sackdelay);
2556 msecs_to_jiffies(params->spp_sackdelay);
2558 sp->sackdelay = params->spp_sackdelay;
2562 if (sackdelay_change) {
2564 trans->param_flags =
2565 (trans->param_flags & ~SPP_SACKDELAY) |
2569 (asoc->param_flags & ~SPP_SACKDELAY) |
2573 (sp->param_flags & ~SPP_SACKDELAY) |
2578 /* Note that a value of zero indicates the current setting should be
2581 if (params->spp_pathmaxrxt) {
2583 trans->pathmaxrxt = params->spp_pathmaxrxt;
2585 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2587 sp->pathmaxrxt = params->spp_pathmaxrxt;
2591 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2593 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2594 trans->flowlabel = params->spp_ipv6_flowlabel &
2595 SCTP_FLOWLABEL_VAL_MASK;
2596 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2599 struct sctp_transport *t;
2601 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2603 if (t->ipaddr.sa.sa_family != AF_INET6)
2605 t->flowlabel = params->spp_ipv6_flowlabel &
2606 SCTP_FLOWLABEL_VAL_MASK;
2607 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2609 asoc->flowlabel = params->spp_ipv6_flowlabel &
2610 SCTP_FLOWLABEL_VAL_MASK;
2611 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2612 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2613 sp->flowlabel = params->spp_ipv6_flowlabel &
2614 SCTP_FLOWLABEL_VAL_MASK;
2615 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2619 if (params->spp_flags & SPP_DSCP) {
2621 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2622 trans->dscp |= SCTP_DSCP_SET_MASK;
2624 struct sctp_transport *t;
2626 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2628 t->dscp = params->spp_dscp &
2630 t->dscp |= SCTP_DSCP_SET_MASK;
2632 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2633 asoc->dscp |= SCTP_DSCP_SET_MASK;
2635 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2636 sp->dscp |= SCTP_DSCP_SET_MASK;
2643 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2644 char __user *optval,
2645 unsigned int optlen)
2647 struct sctp_paddrparams params;
2648 struct sctp_transport *trans = NULL;
2649 struct sctp_association *asoc = NULL;
2650 struct sctp_sock *sp = sctp_sk(sk);
2652 int hb_change, pmtud_change, sackdelay_change;
2654 if (optlen == sizeof(params)) {
2655 if (copy_from_user(¶ms, optval, optlen))
2657 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2658 spp_ipv6_flowlabel), 4)) {
2659 if (copy_from_user(¶ms, optval, optlen))
2661 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2667 /* Validate flags and value parameters. */
2668 hb_change = params.spp_flags & SPP_HB;
2669 pmtud_change = params.spp_flags & SPP_PMTUD;
2670 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2672 if (hb_change == SPP_HB ||
2673 pmtud_change == SPP_PMTUD ||
2674 sackdelay_change == SPP_SACKDELAY ||
2675 params.spp_sackdelay > 500 ||
2676 (params.spp_pathmtu &&
2677 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2680 /* If an address other than INADDR_ANY is specified, and
2681 * no transport is found, then the request is invalid.
2683 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2684 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2685 params.spp_assoc_id);
2690 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2691 * socket is a one to many style socket, and an association
2692 * was not found, then the id was invalid.
2694 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2695 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2696 sctp_style(sk, UDP))
2699 /* Heartbeat demand can only be sent on a transport or
2700 * association, but not a socket.
2702 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2705 /* Process parameters. */
2706 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2707 hb_change, pmtud_change,
2713 /* If changes are for association, also apply parameters to each
2716 if (!trans && asoc) {
2717 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2719 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2720 hb_change, pmtud_change,
2728 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2730 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2733 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2735 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2738 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2739 struct sctp_association *asoc)
2741 struct sctp_transport *trans;
2743 if (params->sack_delay) {
2744 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2746 sctp_spp_sackdelay_enable(asoc->param_flags);
2748 if (params->sack_freq == 1) {
2750 sctp_spp_sackdelay_disable(asoc->param_flags);
2751 } else if (params->sack_freq > 1) {
2752 asoc->sackfreq = params->sack_freq;
2754 sctp_spp_sackdelay_enable(asoc->param_flags);
2757 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2759 if (params->sack_delay) {
2760 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2761 trans->param_flags =
2762 sctp_spp_sackdelay_enable(trans->param_flags);
2764 if (params->sack_freq == 1) {
2765 trans->param_flags =
2766 sctp_spp_sackdelay_disable(trans->param_flags);
2767 } else if (params->sack_freq > 1) {
2768 trans->sackfreq = params->sack_freq;
2769 trans->param_flags =
2770 sctp_spp_sackdelay_enable(trans->param_flags);
2776 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2778 * This option will effect the way delayed acks are performed. This
2779 * option allows you to get or set the delayed ack time, in
2780 * milliseconds. It also allows changing the delayed ack frequency.
2781 * Changing the frequency to 1 disables the delayed sack algorithm. If
2782 * the assoc_id is 0, then this sets or gets the endpoints default
2783 * values. If the assoc_id field is non-zero, then the set or get
2784 * effects the specified association for the one to many model (the
2785 * assoc_id field is ignored by the one to one model). Note that if
2786 * sack_delay or sack_freq are 0 when setting this option, then the
2787 * current values will remain unchanged.
2789 * struct sctp_sack_info {
2790 * sctp_assoc_t sack_assoc_id;
2791 * uint32_t sack_delay;
2792 * uint32_t sack_freq;
2795 * sack_assoc_id - This parameter, indicates which association the user
2796 * is performing an action upon. Note that if this field's value is
2797 * zero then the endpoints default value is changed (effecting future
2798 * associations only).
2800 * sack_delay - This parameter contains the number of milliseconds that
2801 * the user is requesting the delayed ACK timer be set to. Note that
2802 * this value is defined in the standard to be between 200 and 500
2805 * sack_freq - This parameter contains the number of packets that must
2806 * be received before a sack is sent without waiting for the delay
2807 * timer to expire. The default value for this is 2, setting this
2808 * value to 1 will disable the delayed sack algorithm.
2811 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2812 char __user *optval, unsigned int optlen)
2814 struct sctp_sock *sp = sctp_sk(sk);
2815 struct sctp_association *asoc;
2816 struct sctp_sack_info params;
2818 if (optlen == sizeof(struct sctp_sack_info)) {
2819 if (copy_from_user(¶ms, optval, optlen))
2822 if (params.sack_delay == 0 && params.sack_freq == 0)
2824 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2825 pr_warn_ratelimited(DEPRECATED
2827 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2828 "Use struct sctp_sack_info instead\n",
2829 current->comm, task_pid_nr(current));
2830 if (copy_from_user(¶ms, optval, optlen))
2833 if (params.sack_delay == 0)
2834 params.sack_freq = 1;
2836 params.sack_freq = 0;
2840 /* Validate value parameter. */
2841 if (params.sack_delay > 500)
2844 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2845 * socket is a one to many style socket, and an association
2846 * was not found, then the id was invalid.
2848 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2849 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2850 sctp_style(sk, UDP))
2854 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2859 if (sctp_style(sk, TCP))
2860 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2862 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2863 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2864 if (params.sack_delay) {
2865 sp->sackdelay = params.sack_delay;
2867 sctp_spp_sackdelay_enable(sp->param_flags);
2869 if (params.sack_freq == 1) {
2871 sctp_spp_sackdelay_disable(sp->param_flags);
2872 } else if (params.sack_freq > 1) {
2873 sp->sackfreq = params.sack_freq;
2875 sctp_spp_sackdelay_enable(sp->param_flags);
2879 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2880 params.sack_assoc_id == SCTP_ALL_ASSOC)
2881 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2882 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2887 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2889 * Applications can specify protocol parameters for the default association
2890 * initialization. The option name argument to setsockopt() and getsockopt()
2893 * Setting initialization parameters is effective only on an unconnected
2894 * socket (for UDP-style sockets only future associations are effected
2895 * by the change). With TCP-style sockets, this option is inherited by
2896 * sockets derived from a listener socket.
2898 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2900 struct sctp_initmsg sinit;
2901 struct sctp_sock *sp = sctp_sk(sk);
2903 if (optlen != sizeof(struct sctp_initmsg))
2905 if (copy_from_user(&sinit, optval, optlen))
2908 if (sinit.sinit_num_ostreams)
2909 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2910 if (sinit.sinit_max_instreams)
2911 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2912 if (sinit.sinit_max_attempts)
2913 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2914 if (sinit.sinit_max_init_timeo)
2915 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2921 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2923 * Applications that wish to use the sendto() system call may wish to
2924 * specify a default set of parameters that would normally be supplied
2925 * through the inclusion of ancillary data. This socket option allows
2926 * such an application to set the default sctp_sndrcvinfo structure.
2927 * The application that wishes to use this socket option simply passes
2928 * in to this call the sctp_sndrcvinfo structure defined in Section
2929 * 5.2.2) The input parameters accepted by this call include
2930 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2931 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2932 * to this call if the caller is using the UDP model.
2934 static int sctp_setsockopt_default_send_param(struct sock *sk,
2935 char __user *optval,
2936 unsigned int optlen)
2938 struct sctp_sock *sp = sctp_sk(sk);
2939 struct sctp_association *asoc;
2940 struct sctp_sndrcvinfo info;
2942 if (optlen != sizeof(info))
2944 if (copy_from_user(&info, optval, optlen))
2946 if (info.sinfo_flags &
2947 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2948 SCTP_ABORT | SCTP_EOF))
2951 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2952 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
2953 sctp_style(sk, UDP))
2957 asoc->default_stream = info.sinfo_stream;
2958 asoc->default_flags = info.sinfo_flags;
2959 asoc->default_ppid = info.sinfo_ppid;
2960 asoc->default_context = info.sinfo_context;
2961 asoc->default_timetolive = info.sinfo_timetolive;
2966 if (sctp_style(sk, TCP))
2967 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2969 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2970 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2971 sp->default_stream = info.sinfo_stream;
2972 sp->default_flags = info.sinfo_flags;
2973 sp->default_ppid = info.sinfo_ppid;
2974 sp->default_context = info.sinfo_context;
2975 sp->default_timetolive = info.sinfo_timetolive;
2978 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2979 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2980 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2981 asoc->default_stream = info.sinfo_stream;
2982 asoc->default_flags = info.sinfo_flags;
2983 asoc->default_ppid = info.sinfo_ppid;
2984 asoc->default_context = info.sinfo_context;
2985 asoc->default_timetolive = info.sinfo_timetolive;
2992 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2993 * (SCTP_DEFAULT_SNDINFO)
2995 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2996 char __user *optval,
2997 unsigned int optlen)
2999 struct sctp_sock *sp = sctp_sk(sk);
3000 struct sctp_association *asoc;
3001 struct sctp_sndinfo info;
3003 if (optlen != sizeof(info))
3005 if (copy_from_user(&info, optval, optlen))
3007 if (info.snd_flags &
3008 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3009 SCTP_ABORT | SCTP_EOF))
3012 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3013 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3014 sctp_style(sk, UDP))
3018 asoc->default_stream = info.snd_sid;
3019 asoc->default_flags = info.snd_flags;
3020 asoc->default_ppid = info.snd_ppid;
3021 asoc->default_context = info.snd_context;
3026 if (sctp_style(sk, TCP))
3027 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3029 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3030 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3031 sp->default_stream = info.snd_sid;
3032 sp->default_flags = info.snd_flags;
3033 sp->default_ppid = info.snd_ppid;
3034 sp->default_context = info.snd_context;
3037 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3038 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3039 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3040 asoc->default_stream = info.snd_sid;
3041 asoc->default_flags = info.snd_flags;
3042 asoc->default_ppid = info.snd_ppid;
3043 asoc->default_context = info.snd_context;
3050 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3052 * Requests that the local SCTP stack use the enclosed peer address as
3053 * the association primary. The enclosed address must be one of the
3054 * association peer's addresses.
3056 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3057 unsigned int optlen)
3059 struct sctp_prim prim;
3060 struct sctp_transport *trans;
3064 if (optlen != sizeof(struct sctp_prim))
3067 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3070 /* Allow security module to validate address but need address len. */
3071 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3075 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3076 (struct sockaddr *)&prim.ssp_addr,
3081 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3085 sctp_assoc_set_primary(trans->asoc, trans);
3091 * 7.1.5 SCTP_NODELAY
3093 * Turn on/off any Nagle-like algorithm. This means that packets are
3094 * generally sent as soon as possible and no unnecessary delays are
3095 * introduced, at the cost of more packets in the network. Expects an
3096 * integer boolean flag.
3098 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3099 unsigned int optlen)
3103 if (optlen < sizeof(int))
3105 if (get_user(val, (int __user *)optval))
3108 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3114 * 7.1.1 SCTP_RTOINFO
3116 * The protocol parameters used to initialize and bound retransmission
3117 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3118 * and modify these parameters.
3119 * All parameters are time values, in milliseconds. A value of 0, when
3120 * modifying the parameters, indicates that the current value should not
3124 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3126 struct sctp_rtoinfo rtoinfo;
3127 struct sctp_association *asoc;
3128 unsigned long rto_min, rto_max;
3129 struct sctp_sock *sp = sctp_sk(sk);
3131 if (optlen != sizeof (struct sctp_rtoinfo))
3134 if (copy_from_user(&rtoinfo, optval, optlen))
3137 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3139 /* Set the values to the specific association */
3140 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3141 sctp_style(sk, UDP))
3144 rto_max = rtoinfo.srto_max;
3145 rto_min = rtoinfo.srto_min;
3148 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3150 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3153 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3155 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3157 if (rto_min > rto_max)
3161 if (rtoinfo.srto_initial != 0)
3163 msecs_to_jiffies(rtoinfo.srto_initial);
3164 asoc->rto_max = rto_max;
3165 asoc->rto_min = rto_min;
3167 /* If there is no association or the association-id = 0
3168 * set the values to the endpoint.
3170 if (rtoinfo.srto_initial != 0)
3171 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3172 sp->rtoinfo.srto_max = rto_max;
3173 sp->rtoinfo.srto_min = rto_min;
3181 * 7.1.2 SCTP_ASSOCINFO
3183 * This option is used to tune the maximum retransmission attempts
3184 * of the association.
3185 * Returns an error if the new association retransmission value is
3186 * greater than the sum of the retransmission value of the peer.
3187 * See [SCTP] for more information.
3190 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3193 struct sctp_assocparams assocparams;
3194 struct sctp_association *asoc;
3196 if (optlen != sizeof(struct sctp_assocparams))
3198 if (copy_from_user(&assocparams, optval, optlen))
3201 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3203 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3204 sctp_style(sk, UDP))
3207 /* Set the values to the specific association */
3209 if (assocparams.sasoc_asocmaxrxt != 0) {
3212 struct sctp_transport *peer_addr;
3214 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3216 path_sum += peer_addr->pathmaxrxt;
3220 /* Only validate asocmaxrxt if we have more than
3221 * one path/transport. We do this because path
3222 * retransmissions are only counted when we have more
3226 assocparams.sasoc_asocmaxrxt > path_sum)
3229 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3232 if (assocparams.sasoc_cookie_life != 0)
3233 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3235 /* Set the values to the endpoint */
3236 struct sctp_sock *sp = sctp_sk(sk);
3238 if (assocparams.sasoc_asocmaxrxt != 0)
3239 sp->assocparams.sasoc_asocmaxrxt =
3240 assocparams.sasoc_asocmaxrxt;
3241 if (assocparams.sasoc_cookie_life != 0)
3242 sp->assocparams.sasoc_cookie_life =
3243 assocparams.sasoc_cookie_life;
3249 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3251 * This socket option is a boolean flag which turns on or off mapped V4
3252 * addresses. If this option is turned on and the socket is type
3253 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3254 * If this option is turned off, then no mapping will be done of V4
3255 * addresses and a user will receive both PF_INET6 and PF_INET type
3256 * addresses on the socket.
3258 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3261 struct sctp_sock *sp = sctp_sk(sk);
3263 if (optlen < sizeof(int))
3265 if (get_user(val, (int __user *)optval))
3276 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3277 * This option will get or set the maximum size to put in any outgoing
3278 * SCTP DATA chunk. If a message is larger than this size it will be
3279 * fragmented by SCTP into the specified size. Note that the underlying
3280 * SCTP implementation may fragment into smaller sized chunks when the
3281 * PMTU of the underlying association is smaller than the value set by
3282 * the user. The default value for this option is '0' which indicates
3283 * the user is NOT limiting fragmentation and only the PMTU will effect
3284 * SCTP's choice of DATA chunk size. Note also that values set larger
3285 * than the maximum size of an IP datagram will effectively let SCTP
3286 * control fragmentation (i.e. the same as setting this option to 0).
3288 * The following structure is used to access and modify this parameter:
3290 * struct sctp_assoc_value {
3291 * sctp_assoc_t assoc_id;
3292 * uint32_t assoc_value;
3295 * assoc_id: This parameter is ignored for one-to-one style sockets.
3296 * For one-to-many style sockets this parameter indicates which
3297 * association the user is performing an action upon. Note that if
3298 * this field's value is zero then the endpoints default value is
3299 * changed (effecting future associations only).
3300 * assoc_value: This parameter specifies the maximum size in bytes.
3302 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3304 struct sctp_sock *sp = sctp_sk(sk);
3305 struct sctp_assoc_value params;
3306 struct sctp_association *asoc;
3309 if (optlen == sizeof(int)) {
3310 pr_warn_ratelimited(DEPRECATED
3312 "Use of int in maxseg socket option.\n"
3313 "Use struct sctp_assoc_value instead\n",
3314 current->comm, task_pid_nr(current));
3315 if (copy_from_user(&val, optval, optlen))
3317 params.assoc_id = SCTP_FUTURE_ASSOC;
3318 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3319 if (copy_from_user(¶ms, optval, optlen))
3321 val = params.assoc_value;
3326 asoc = sctp_id2assoc(sk, params.assoc_id);
3327 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3328 sctp_style(sk, UDP))
3332 int min_len, max_len;
3333 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3334 sizeof(struct sctp_data_chunk);
3336 min_len = sctp_min_frag_point(sp, datasize);
3337 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3339 if (val < min_len || val > max_len)
3344 asoc->user_frag = val;
3345 sctp_assoc_update_frag_point(asoc);
3347 sp->user_frag = val;
3355 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3357 * Requests that the peer mark the enclosed address as the association
3358 * primary. The enclosed address must be one of the association's
3359 * locally bound addresses. The following structure is used to make a
3360 * set primary request:
3362 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3363 unsigned int optlen)
3365 struct sctp_sock *sp;
3366 struct sctp_association *asoc = NULL;
3367 struct sctp_setpeerprim prim;
3368 struct sctp_chunk *chunk;
3374 if (!sp->ep->asconf_enable)
3377 if (optlen != sizeof(struct sctp_setpeerprim))
3380 if (copy_from_user(&prim, optval, optlen))
3383 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3387 if (!asoc->peer.asconf_capable)
3390 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3393 if (!sctp_state(asoc, ESTABLISHED))
3396 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3400 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3401 return -EADDRNOTAVAIL;
3403 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3404 return -EADDRNOTAVAIL;
3406 /* Allow security module to validate address. */
3407 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3408 (struct sockaddr *)&prim.sspp_addr,
3413 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3414 chunk = sctp_make_asconf_set_prim(asoc,
3415 (union sctp_addr *)&prim.sspp_addr);
3419 err = sctp_send_asconf(asoc, chunk);
3421 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3426 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3427 unsigned int optlen)
3429 struct sctp_setadaptation adaptation;
3431 if (optlen != sizeof(struct sctp_setadaptation))
3433 if (copy_from_user(&adaptation, optval, optlen))
3436 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3442 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3444 * The context field in the sctp_sndrcvinfo structure is normally only
3445 * used when a failed message is retrieved holding the value that was
3446 * sent down on the actual send call. This option allows the setting of
3447 * a default context on an association basis that will be received on
3448 * reading messages from the peer. This is especially helpful in the
3449 * one-2-many model for an application to keep some reference to an
3450 * internal state machine that is processing messages on the
3451 * association. Note that the setting of this value only effects
3452 * received messages from the peer and does not effect the value that is
3453 * saved with outbound messages.
3455 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3456 unsigned int optlen)
3458 struct sctp_sock *sp = sctp_sk(sk);
3459 struct sctp_assoc_value params;
3460 struct sctp_association *asoc;
3462 if (optlen != sizeof(struct sctp_assoc_value))
3464 if (copy_from_user(¶ms, optval, optlen))
3467 asoc = sctp_id2assoc(sk, params.assoc_id);
3468 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3469 sctp_style(sk, UDP))
3473 asoc->default_rcv_context = params.assoc_value;
3478 if (sctp_style(sk, TCP))
3479 params.assoc_id = SCTP_FUTURE_ASSOC;
3481 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3482 params.assoc_id == SCTP_ALL_ASSOC)
3483 sp->default_rcv_context = params.assoc_value;
3485 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3486 params.assoc_id == SCTP_ALL_ASSOC)
3487 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3488 asoc->default_rcv_context = params.assoc_value;
3494 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3496 * This options will at a minimum specify if the implementation is doing
3497 * fragmented interleave. Fragmented interleave, for a one to many
3498 * socket, is when subsequent calls to receive a message may return
3499 * parts of messages from different associations. Some implementations
3500 * may allow you to turn this value on or off. If so, when turned off,
3501 * no fragment interleave will occur (which will cause a head of line
3502 * blocking amongst multiple associations sharing the same one to many
3503 * socket). When this option is turned on, then each receive call may
3504 * come from a different association (thus the user must receive data
3505 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3506 * association each receive belongs to.
3508 * This option takes a boolean value. A non-zero value indicates that
3509 * fragmented interleave is on. A value of zero indicates that
3510 * fragmented interleave is off.
3512 * Note that it is important that an implementation that allows this
3513 * option to be turned on, have it off by default. Otherwise an unaware
3514 * application using the one to many model may become confused and act
3517 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3518 char __user *optval,
3519 unsigned int optlen)
3523 if (optlen != sizeof(int))
3525 if (get_user(val, (int __user *)optval))
3528 sctp_sk(sk)->frag_interleave = !!val;
3530 if (!sctp_sk(sk)->frag_interleave)
3531 sctp_sk(sk)->ep->intl_enable = 0;
3537 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3538 * (SCTP_PARTIAL_DELIVERY_POINT)
3540 * This option will set or get the SCTP partial delivery point. This
3541 * point is the size of a message where the partial delivery API will be
3542 * invoked to help free up rwnd space for the peer. Setting this to a
3543 * lower value will cause partial deliveries to happen more often. The
3544 * calls argument is an integer that sets or gets the partial delivery
3545 * point. Note also that the call will fail if the user attempts to set
3546 * this value larger than the socket receive buffer size.
3548 * Note that any single message having a length smaller than or equal to
3549 * the SCTP partial delivery point will be delivered in one single read
3550 * call as long as the user provided buffer is large enough to hold the
3553 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3554 char __user *optval,
3555 unsigned int optlen)
3559 if (optlen != sizeof(u32))
3561 if (get_user(val, (int __user *)optval))
3564 /* Note: We double the receive buffer from what the user sets
3565 * it to be, also initial rwnd is based on rcvbuf/2.
3567 if (val > (sk->sk_rcvbuf >> 1))
3570 sctp_sk(sk)->pd_point = val;
3572 return 0; /* is this the right error code? */
3576 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3578 * This option will allow a user to change the maximum burst of packets
3579 * that can be emitted by this association. Note that the default value
3580 * is 4, and some implementations may restrict this setting so that it
3581 * can only be lowered.
3583 * NOTE: This text doesn't seem right. Do this on a socket basis with
3584 * future associations inheriting the socket value.
3586 static int sctp_setsockopt_maxburst(struct sock *sk,
3587 char __user *optval,
3588 unsigned int optlen)
3590 struct sctp_sock *sp = sctp_sk(sk);
3591 struct sctp_assoc_value params;
3592 struct sctp_association *asoc;
3594 if (optlen == sizeof(int)) {
3595 pr_warn_ratelimited(DEPRECATED
3597 "Use of int in max_burst socket option deprecated.\n"
3598 "Use struct sctp_assoc_value instead\n",
3599 current->comm, task_pid_nr(current));
3600 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3602 params.assoc_id = SCTP_FUTURE_ASSOC;
3603 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3604 if (copy_from_user(¶ms, optval, optlen))
3609 asoc = sctp_id2assoc(sk, params.assoc_id);
3610 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3611 sctp_style(sk, UDP))
3615 asoc->max_burst = params.assoc_value;
3620 if (sctp_style(sk, TCP))
3621 params.assoc_id = SCTP_FUTURE_ASSOC;
3623 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3624 params.assoc_id == SCTP_ALL_ASSOC)
3625 sp->max_burst = params.assoc_value;
3627 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3628 params.assoc_id == SCTP_ALL_ASSOC)
3629 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3630 asoc->max_burst = params.assoc_value;
3636 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3638 * This set option adds a chunk type that the user is requesting to be
3639 * received only in an authenticated way. Changes to the list of chunks
3640 * will only effect future associations on the socket.
3642 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3643 char __user *optval,
3644 unsigned int optlen)
3646 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3647 struct sctp_authchunk val;
3649 if (!ep->auth_enable)
3652 if (optlen != sizeof(struct sctp_authchunk))
3654 if (copy_from_user(&val, optval, optlen))
3657 switch (val.sauth_chunk) {
3659 case SCTP_CID_INIT_ACK:
3660 case SCTP_CID_SHUTDOWN_COMPLETE:
3665 /* add this chunk id to the endpoint */
3666 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3670 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3672 * This option gets or sets the list of HMAC algorithms that the local
3673 * endpoint requires the peer to use.
3675 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3676 char __user *optval,
3677 unsigned int optlen)
3679 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3680 struct sctp_hmacalgo *hmacs;
3684 if (!ep->auth_enable)
3687 if (optlen < sizeof(struct sctp_hmacalgo))
3689 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3690 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3692 hmacs = memdup_user(optval, optlen);
3694 return PTR_ERR(hmacs);
3696 idents = hmacs->shmac_num_idents;
3697 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3698 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3703 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3710 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3712 * This option will set a shared secret key which is used to build an
3713 * association shared key.
3715 static int sctp_setsockopt_auth_key(struct sock *sk,
3716 char __user *optval,
3717 unsigned int optlen)
3719 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3720 struct sctp_authkey *authkey;
3721 struct sctp_association *asoc;
3724 if (optlen <= sizeof(struct sctp_authkey))
3726 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3729 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3731 authkey = memdup_user(optval, optlen);
3732 if (IS_ERR(authkey))
3733 return PTR_ERR(authkey);
3735 if (authkey->sca_keylength > optlen - sizeof(*authkey))
3738 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3739 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3740 sctp_style(sk, UDP))
3744 ret = sctp_auth_set_key(ep, asoc, authkey);
3748 if (sctp_style(sk, TCP))
3749 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3751 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3752 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3753 ret = sctp_auth_set_key(ep, asoc, authkey);
3760 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3761 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3762 list_for_each_entry(asoc, &ep->asocs, asocs) {
3763 int res = sctp_auth_set_key(ep, asoc, authkey);
3776 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3778 * This option will get or set the active shared key to be used to build
3779 * the association shared key.
3781 static int sctp_setsockopt_active_key(struct sock *sk,
3782 char __user *optval,
3783 unsigned int optlen)
3785 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3786 struct sctp_association *asoc;
3787 struct sctp_authkeyid val;
3790 if (optlen != sizeof(struct sctp_authkeyid))
3792 if (copy_from_user(&val, optval, optlen))
3795 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3796 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3797 sctp_style(sk, UDP))
3801 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3803 if (sctp_style(sk, TCP))
3804 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3806 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3807 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3808 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3813 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3814 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3815 list_for_each_entry(asoc, &ep->asocs, asocs) {
3816 int res = sctp_auth_set_active_key(ep, asoc,
3817 val.scact_keynumber);
3828 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3830 * This set option will delete a shared secret key from use.
3832 static int sctp_setsockopt_del_key(struct sock *sk,
3833 char __user *optval,
3834 unsigned int optlen)
3836 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3837 struct sctp_association *asoc;
3838 struct sctp_authkeyid val;
3841 if (optlen != sizeof(struct sctp_authkeyid))
3843 if (copy_from_user(&val, optval, optlen))
3846 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3847 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3848 sctp_style(sk, UDP))
3852 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3854 if (sctp_style(sk, TCP))
3855 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3857 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3858 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3859 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3864 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3865 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3866 list_for_each_entry(asoc, &ep->asocs, asocs) {
3867 int res = sctp_auth_del_key_id(ep, asoc,
3868 val.scact_keynumber);
3879 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3881 * This set option will deactivate a shared secret key.
3883 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3884 unsigned int optlen)
3886 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3887 struct sctp_association *asoc;
3888 struct sctp_authkeyid val;
3891 if (optlen != sizeof(struct sctp_authkeyid))
3893 if (copy_from_user(&val, optval, optlen))
3896 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3897 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3898 sctp_style(sk, UDP))
3902 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3904 if (sctp_style(sk, TCP))
3905 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3907 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3908 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3909 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3914 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3915 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3916 list_for_each_entry(asoc, &ep->asocs, asocs) {
3917 int res = sctp_auth_deact_key_id(ep, asoc,
3918 val.scact_keynumber);
3929 * 8.1.23 SCTP_AUTO_ASCONF
3931 * This option will enable or disable the use of the automatic generation of
3932 * ASCONF chunks to add and delete addresses to an existing association. Note
3933 * that this option has two caveats namely: a) it only affects sockets that
3934 * are bound to all addresses available to the SCTP stack, and b) the system
3935 * administrator may have an overriding control that turns the ASCONF feature
3936 * off no matter what setting the socket option may have.
3937 * This option expects an integer boolean flag, where a non-zero value turns on
3938 * the option, and a zero value turns off the option.
3939 * Note. In this implementation, socket operation overrides default parameter
3940 * being set by sysctl as well as FreeBSD implementation
3942 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3943 unsigned int optlen)
3946 struct sctp_sock *sp = sctp_sk(sk);
3948 if (optlen < sizeof(int))
3950 if (get_user(val, (int __user *)optval))
3952 if (!sctp_is_ep_boundall(sk) && val)
3954 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3957 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3958 if (val == 0 && sp->do_auto_asconf) {
3959 list_del(&sp->auto_asconf_list);
3960 sp->do_auto_asconf = 0;
3961 } else if (val && !sp->do_auto_asconf) {
3962 list_add_tail(&sp->auto_asconf_list,
3963 &sock_net(sk)->sctp.auto_asconf_splist);
3964 sp->do_auto_asconf = 1;
3966 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3971 * SCTP_PEER_ADDR_THLDS
3973 * This option allows us to alter the partially failed threshold for one or all
3974 * transports in an association. See Section 6.1 of:
3975 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3977 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3978 char __user *optval,
3979 unsigned int optlen)
3981 struct sctp_paddrthlds val;
3982 struct sctp_transport *trans;
3983 struct sctp_association *asoc;
3985 if (optlen < sizeof(struct sctp_paddrthlds))
3987 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3988 sizeof(struct sctp_paddrthlds)))
3991 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3992 trans = sctp_addr_id2transport(sk, &val.spt_address,
3997 if (val.spt_pathmaxrxt)
3998 trans->pathmaxrxt = val.spt_pathmaxrxt;
3999 trans->pf_retrans = val.spt_pathpfthld;
4004 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4005 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4006 sctp_style(sk, UDP))
4010 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4012 if (val.spt_pathmaxrxt)
4013 trans->pathmaxrxt = val.spt_pathmaxrxt;
4014 trans->pf_retrans = val.spt_pathpfthld;
4017 if (val.spt_pathmaxrxt)
4018 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4019 asoc->pf_retrans = val.spt_pathpfthld;
4021 struct sctp_sock *sp = sctp_sk(sk);
4023 if (val.spt_pathmaxrxt)
4024 sp->pathmaxrxt = val.spt_pathmaxrxt;
4025 sp->pf_retrans = val.spt_pathpfthld;
4031 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4032 char __user *optval,
4033 unsigned int optlen)
4037 if (optlen < sizeof(int))
4039 if (get_user(val, (int __user *) optval))
4042 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4047 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4048 char __user *optval,
4049 unsigned int optlen)
4053 if (optlen < sizeof(int))
4055 if (get_user(val, (int __user *) optval))
4058 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4063 static int sctp_setsockopt_pr_supported(struct sock *sk,
4064 char __user *optval,
4065 unsigned int optlen)
4067 struct sctp_assoc_value params;
4068 struct sctp_association *asoc;
4070 if (optlen != sizeof(params))
4073 if (copy_from_user(¶ms, optval, optlen))
4076 asoc = sctp_id2assoc(sk, params.assoc_id);
4077 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4078 sctp_style(sk, UDP))
4081 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4086 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4087 char __user *optval,
4088 unsigned int optlen)
4090 struct sctp_sock *sp = sctp_sk(sk);
4091 struct sctp_default_prinfo info;
4092 struct sctp_association *asoc;
4093 int retval = -EINVAL;
4095 if (optlen != sizeof(info))
4098 if (copy_from_user(&info, optval, sizeof(info))) {
4103 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4106 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4109 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4110 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4111 sctp_style(sk, UDP))
4117 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4118 asoc->default_timetolive = info.pr_value;
4122 if (sctp_style(sk, TCP))
4123 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4125 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4126 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4127 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4128 sp->default_timetolive = info.pr_value;
4131 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4132 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4133 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4134 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4135 asoc->default_timetolive = info.pr_value;
4143 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4144 char __user *optval,
4145 unsigned int optlen)
4147 struct sctp_assoc_value params;
4148 struct sctp_association *asoc;
4149 int retval = -EINVAL;
4151 if (optlen != sizeof(params))
4154 if (copy_from_user(¶ms, optval, optlen)) {
4159 asoc = sctp_id2assoc(sk, params.assoc_id);
4160 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4161 sctp_style(sk, UDP))
4164 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4172 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4173 char __user *optval,
4174 unsigned int optlen)
4176 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4177 struct sctp_assoc_value params;
4178 struct sctp_association *asoc;
4179 int retval = -EINVAL;
4181 if (optlen != sizeof(params))
4184 if (copy_from_user(¶ms, optval, optlen)) {
4189 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4192 asoc = sctp_id2assoc(sk, params.assoc_id);
4193 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4194 sctp_style(sk, UDP))
4200 asoc->strreset_enable = params.assoc_value;
4204 if (sctp_style(sk, TCP))
4205 params.assoc_id = SCTP_FUTURE_ASSOC;
4207 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4208 params.assoc_id == SCTP_ALL_ASSOC)
4209 ep->strreset_enable = params.assoc_value;
4211 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4212 params.assoc_id == SCTP_ALL_ASSOC)
4213 list_for_each_entry(asoc, &ep->asocs, asocs)
4214 asoc->strreset_enable = params.assoc_value;
4220 static int sctp_setsockopt_reset_streams(struct sock *sk,
4221 char __user *optval,
4222 unsigned int optlen)
4224 struct sctp_reset_streams *params;
4225 struct sctp_association *asoc;
4226 int retval = -EINVAL;
4228 if (optlen < sizeof(*params))
4230 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4231 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4232 sizeof(__u16) * sizeof(*params));
4234 params = memdup_user(optval, optlen);
4236 return PTR_ERR(params);
4238 if (params->srs_number_streams * sizeof(__u16) >
4239 optlen - sizeof(*params))
4242 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4246 retval = sctp_send_reset_streams(asoc, params);
4253 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4254 char __user *optval,
4255 unsigned int optlen)
4257 struct sctp_association *asoc;
4258 sctp_assoc_t associd;
4259 int retval = -EINVAL;
4261 if (optlen != sizeof(associd))
4264 if (copy_from_user(&associd, optval, optlen)) {
4269 asoc = sctp_id2assoc(sk, associd);
4273 retval = sctp_send_reset_assoc(asoc);
4279 static int sctp_setsockopt_add_streams(struct sock *sk,
4280 char __user *optval,
4281 unsigned int optlen)
4283 struct sctp_association *asoc;
4284 struct sctp_add_streams params;
4285 int retval = -EINVAL;
4287 if (optlen != sizeof(params))
4290 if (copy_from_user(¶ms, optval, optlen)) {
4295 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4299 retval = sctp_send_add_streams(asoc, ¶ms);
4305 static int sctp_setsockopt_scheduler(struct sock *sk,
4306 char __user *optval,
4307 unsigned int optlen)
4309 struct sctp_sock *sp = sctp_sk(sk);
4310 struct sctp_association *asoc;
4311 struct sctp_assoc_value params;
4314 if (optlen < sizeof(params))
4317 optlen = sizeof(params);
4318 if (copy_from_user(¶ms, optval, optlen))
4321 if (params.assoc_value > SCTP_SS_MAX)
4324 asoc = sctp_id2assoc(sk, params.assoc_id);
4325 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4326 sctp_style(sk, UDP))
4330 return sctp_sched_set_sched(asoc, params.assoc_value);
4332 if (sctp_style(sk, TCP))
4333 params.assoc_id = SCTP_FUTURE_ASSOC;
4335 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4336 params.assoc_id == SCTP_ALL_ASSOC)
4337 sp->default_ss = params.assoc_value;
4339 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4340 params.assoc_id == SCTP_ALL_ASSOC) {
4341 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4342 int ret = sctp_sched_set_sched(asoc,
4343 params.assoc_value);
4353 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4354 char __user *optval,
4355 unsigned int optlen)
4357 struct sctp_stream_value params;
4358 struct sctp_association *asoc;
4359 int retval = -EINVAL;
4361 if (optlen < sizeof(params))
4364 optlen = sizeof(params);
4365 if (copy_from_user(¶ms, optval, optlen)) {
4370 asoc = sctp_id2assoc(sk, params.assoc_id);
4371 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4372 sctp_style(sk, UDP))
4376 retval = sctp_sched_set_value(asoc, params.stream_id,
4377 params.stream_value, GFP_KERNEL);
4383 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4384 int ret = sctp_sched_set_value(asoc, params.stream_id,
4385 params.stream_value, GFP_KERNEL);
4386 if (ret && !retval) /* try to return the 1st error. */
4394 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4395 char __user *optval,
4396 unsigned int optlen)
4398 struct sctp_sock *sp = sctp_sk(sk);
4399 struct sctp_assoc_value params;
4400 struct sctp_association *asoc;
4401 int retval = -EINVAL;
4403 if (optlen < sizeof(params))
4406 optlen = sizeof(params);
4407 if (copy_from_user(¶ms, optval, optlen)) {
4412 asoc = sctp_id2assoc(sk, params.assoc_id);
4413 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4414 sctp_style(sk, UDP))
4417 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4422 sp->ep->intl_enable = !!params.assoc_value;
4430 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4431 unsigned int optlen)
4435 if (!sctp_style(sk, TCP))
4438 if (sctp_sk(sk)->ep->base.bind_addr.port)
4441 if (optlen < sizeof(int))
4444 if (get_user(val, (int __user *)optval))
4447 sctp_sk(sk)->reuse = !!val;
4452 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4453 struct sctp_association *asoc)
4455 struct sctp_ulpevent *event;
4457 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4459 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4460 if (sctp_outq_is_empty(&asoc->outqueue)) {
4461 event = sctp_ulpevent_make_sender_dry_event(asoc,
4462 GFP_USER | __GFP_NOWARN);
4466 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4473 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4474 unsigned int optlen)
4476 struct sctp_sock *sp = sctp_sk(sk);
4477 struct sctp_association *asoc;
4478 struct sctp_event param;
4481 if (optlen < sizeof(param))
4484 optlen = sizeof(param);
4485 if (copy_from_user(¶m, optval, optlen))
4488 if (param.se_type < SCTP_SN_TYPE_BASE ||
4489 param.se_type > SCTP_SN_TYPE_MAX)
4492 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4493 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4494 sctp_style(sk, UDP))
4498 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4500 if (sctp_style(sk, TCP))
4501 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4503 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4504 param.se_assoc_id == SCTP_ALL_ASSOC)
4505 sctp_ulpevent_type_set(&sp->subscribe,
4506 param.se_type, param.se_on);
4508 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4509 param.se_assoc_id == SCTP_ALL_ASSOC) {
4510 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4511 int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4521 static int sctp_setsockopt_asconf_supported(struct sock *sk,
4522 char __user *optval,
4523 unsigned int optlen)
4525 struct sctp_assoc_value params;
4526 struct sctp_association *asoc;
4527 struct sctp_endpoint *ep;
4528 int retval = -EINVAL;
4530 if (optlen != sizeof(params))
4533 if (copy_from_user(¶ms, optval, optlen)) {
4538 asoc = sctp_id2assoc(sk, params.assoc_id);
4539 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4540 sctp_style(sk, UDP))
4543 ep = sctp_sk(sk)->ep;
4544 ep->asconf_enable = !!params.assoc_value;
4546 if (ep->asconf_enable && ep->auth_enable) {
4547 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4548 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4557 static int sctp_setsockopt_auth_supported(struct sock *sk,
4558 char __user *optval,
4559 unsigned int optlen)
4561 struct sctp_assoc_value params;
4562 struct sctp_association *asoc;
4563 struct sctp_endpoint *ep;
4564 int retval = -EINVAL;
4566 if (optlen != sizeof(params))
4569 if (copy_from_user(¶ms, optval, optlen)) {
4574 asoc = sctp_id2assoc(sk, params.assoc_id);
4575 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4576 sctp_style(sk, UDP))
4579 ep = sctp_sk(sk)->ep;
4580 if (params.assoc_value) {
4581 retval = sctp_auth_init(ep, GFP_KERNEL);
4584 if (ep->asconf_enable) {
4585 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4586 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4590 ep->auth_enable = !!params.assoc_value;
4597 static int sctp_setsockopt_ecn_supported(struct sock *sk,
4598 char __user *optval,
4599 unsigned int optlen)
4601 struct sctp_assoc_value params;
4602 struct sctp_association *asoc;
4603 int retval = -EINVAL;
4605 if (optlen != sizeof(params))
4608 if (copy_from_user(¶ms, optval, optlen)) {
4613 asoc = sctp_id2assoc(sk, params.assoc_id);
4614 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4615 sctp_style(sk, UDP))
4618 sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
4625 /* API 6.2 setsockopt(), getsockopt()
4627 * Applications use setsockopt() and getsockopt() to set or retrieve
4628 * socket options. Socket options are used to change the default
4629 * behavior of sockets calls. They are described in Section 7.
4633 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4634 * int __user *optlen);
4635 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4638 * sd - the socket descript.
4639 * level - set to IPPROTO_SCTP for all SCTP options.
4640 * optname - the option name.
4641 * optval - the buffer to store the value of the option.
4642 * optlen - the size of the buffer.
4644 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4645 char __user *optval, unsigned int optlen)
4649 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4651 /* I can hardly begin to describe how wrong this is. This is
4652 * so broken as to be worse than useless. The API draft
4653 * REALLY is NOT helpful here... I am not convinced that the
4654 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4655 * are at all well-founded.
4657 if (level != SOL_SCTP) {
4658 struct sctp_af *af = sctp_sk(sk)->pf->af;
4659 retval = af->setsockopt(sk, level, optname, optval, optlen);
4666 case SCTP_SOCKOPT_BINDX_ADD:
4667 /* 'optlen' is the size of the addresses buffer. */
4668 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4669 optlen, SCTP_BINDX_ADD_ADDR);
4672 case SCTP_SOCKOPT_BINDX_REM:
4673 /* 'optlen' is the size of the addresses buffer. */
4674 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4675 optlen, SCTP_BINDX_REM_ADDR);
4678 case SCTP_SOCKOPT_CONNECTX_OLD:
4679 /* 'optlen' is the size of the addresses buffer. */
4680 retval = sctp_setsockopt_connectx_old(sk,
4681 (struct sockaddr __user *)optval,
4685 case SCTP_SOCKOPT_CONNECTX:
4686 /* 'optlen' is the size of the addresses buffer. */
4687 retval = sctp_setsockopt_connectx(sk,
4688 (struct sockaddr __user *)optval,
4692 case SCTP_DISABLE_FRAGMENTS:
4693 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4697 retval = sctp_setsockopt_events(sk, optval, optlen);
4700 case SCTP_AUTOCLOSE:
4701 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4704 case SCTP_PEER_ADDR_PARAMS:
4705 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4708 case SCTP_DELAYED_SACK:
4709 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4711 case SCTP_PARTIAL_DELIVERY_POINT:
4712 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4716 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4718 case SCTP_DEFAULT_SEND_PARAM:
4719 retval = sctp_setsockopt_default_send_param(sk, optval,
4722 case SCTP_DEFAULT_SNDINFO:
4723 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4725 case SCTP_PRIMARY_ADDR:
4726 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4728 case SCTP_SET_PEER_PRIMARY_ADDR:
4729 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4732 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4735 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4737 case SCTP_ASSOCINFO:
4738 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4740 case SCTP_I_WANT_MAPPED_V4_ADDR:
4741 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4744 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4746 case SCTP_ADAPTATION_LAYER:
4747 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4750 retval = sctp_setsockopt_context(sk, optval, optlen);
4752 case SCTP_FRAGMENT_INTERLEAVE:
4753 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4755 case SCTP_MAX_BURST:
4756 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4758 case SCTP_AUTH_CHUNK:
4759 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4761 case SCTP_HMAC_IDENT:
4762 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4765 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4767 case SCTP_AUTH_ACTIVE_KEY:
4768 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4770 case SCTP_AUTH_DELETE_KEY:
4771 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4773 case SCTP_AUTH_DEACTIVATE_KEY:
4774 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4776 case SCTP_AUTO_ASCONF:
4777 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4779 case SCTP_PEER_ADDR_THLDS:
4780 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4782 case SCTP_RECVRCVINFO:
4783 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4785 case SCTP_RECVNXTINFO:
4786 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4788 case SCTP_PR_SUPPORTED:
4789 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4791 case SCTP_DEFAULT_PRINFO:
4792 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4794 case SCTP_RECONFIG_SUPPORTED:
4795 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4797 case SCTP_ENABLE_STREAM_RESET:
4798 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4800 case SCTP_RESET_STREAMS:
4801 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4803 case SCTP_RESET_ASSOC:
4804 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4806 case SCTP_ADD_STREAMS:
4807 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4809 case SCTP_STREAM_SCHEDULER:
4810 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4812 case SCTP_STREAM_SCHEDULER_VALUE:
4813 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4815 case SCTP_INTERLEAVING_SUPPORTED:
4816 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4819 case SCTP_REUSE_PORT:
4820 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4823 retval = sctp_setsockopt_event(sk, optval, optlen);
4825 case SCTP_ASCONF_SUPPORTED:
4826 retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
4828 case SCTP_AUTH_SUPPORTED:
4829 retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
4831 case SCTP_ECN_SUPPORTED:
4832 retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
4835 retval = -ENOPROTOOPT;
4845 /* API 3.1.6 connect() - UDP Style Syntax
4847 * An application may use the connect() call in the UDP model to initiate an
4848 * association without sending data.
4852 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4854 * sd: the socket descriptor to have a new association added to.
4856 * nam: the address structure (either struct sockaddr_in or struct
4857 * sockaddr_in6 defined in RFC2553 [7]).
4859 * len: the size of the address.
4861 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4862 int addr_len, int flags)
4868 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4871 /* Validate addr_len before calling common connect/connectx routine. */
4872 af = sctp_get_af_specific(addr->sa_family);
4873 if (af && addr_len >= af->sockaddr_len)
4874 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4880 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4881 int addr_len, int flags)
4883 if (addr_len < sizeof(uaddr->sa_family))
4886 if (uaddr->sa_family == AF_UNSPEC)
4889 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4892 /* FIXME: Write comments. */
4893 static int sctp_disconnect(struct sock *sk, int flags)
4895 return -EOPNOTSUPP; /* STUB */
4898 /* 4.1.4 accept() - TCP Style Syntax
4900 * Applications use accept() call to remove an established SCTP
4901 * association from the accept queue of the endpoint. A new socket
4902 * descriptor will be returned from accept() to represent the newly
4903 * formed association.
4905 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4907 struct sctp_sock *sp;
4908 struct sctp_endpoint *ep;
4909 struct sock *newsk = NULL;
4910 struct sctp_association *asoc;
4919 if (!sctp_style(sk, TCP)) {
4920 error = -EOPNOTSUPP;
4924 if (!sctp_sstate(sk, LISTENING)) {
4929 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4931 error = sctp_wait_for_accept(sk, timeo);
4935 /* We treat the list of associations on the endpoint as the accept
4936 * queue and pick the first association on the list.
4938 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4940 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4946 /* Populate the fields of the newsk from the oldsk and migrate the
4947 * asoc to the newsk.
4949 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4951 sk_common_release(newsk);
4961 /* The SCTP ioctl handler. */
4962 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4969 * SEQPACKET-style sockets in LISTENING state are valid, for
4970 * SCTP, so only discard TCP-style sockets in LISTENING state.
4972 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4977 struct sk_buff *skb;
4978 unsigned int amount = 0;
4980 skb = skb_peek(&sk->sk_receive_queue);
4983 * We will only return the amount of this packet since
4984 * that is all that will be read.
4988 rc = put_user(amount, (int __user *)arg);
5000 /* This is the function which gets called during socket creation to
5001 * initialized the SCTP-specific portion of the sock.
5002 * The sock structure should already be zero-filled memory.
5004 static int sctp_init_sock(struct sock *sk)
5006 struct net *net = sock_net(sk);
5007 struct sctp_sock *sp;
5009 pr_debug("%s: sk:%p\n", __func__, sk);
5013 /* Initialize the SCTP per socket area. */
5014 switch (sk->sk_type) {
5015 case SOCK_SEQPACKET:
5016 sp->type = SCTP_SOCKET_UDP;
5019 sp->type = SCTP_SOCKET_TCP;
5022 return -ESOCKTNOSUPPORT;
5025 sk->sk_gso_type = SKB_GSO_SCTP;
5027 /* Initialize default send parameters. These parameters can be
5028 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
5030 sp->default_stream = 0;
5031 sp->default_ppid = 0;
5032 sp->default_flags = 0;
5033 sp->default_context = 0;
5034 sp->default_timetolive = 0;
5036 sp->default_rcv_context = 0;
5037 sp->max_burst = net->sctp.max_burst;
5039 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5041 /* Initialize default setup parameters. These parameters
5042 * can be modified with the SCTP_INITMSG socket option or
5043 * overridden by the SCTP_INIT CMSG.
5045 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5046 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
5047 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5048 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5050 /* Initialize default RTO related parameters. These parameters can
5051 * be modified for with the SCTP_RTOINFO socket option.
5053 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5054 sp->rtoinfo.srto_max = net->sctp.rto_max;
5055 sp->rtoinfo.srto_min = net->sctp.rto_min;
5057 /* Initialize default association related parameters. These parameters
5058 * can be modified with the SCTP_ASSOCINFO socket option.
5060 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5061 sp->assocparams.sasoc_number_peer_destinations = 0;
5062 sp->assocparams.sasoc_peer_rwnd = 0;
5063 sp->assocparams.sasoc_local_rwnd = 0;
5064 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5066 /* Initialize default event subscriptions. By default, all the
5071 /* Default Peer Address Parameters. These defaults can
5072 * be modified via SCTP_PEER_ADDR_PARAMS
5074 sp->hbinterval = net->sctp.hb_interval;
5075 sp->pathmaxrxt = net->sctp.max_retrans_path;
5076 sp->pf_retrans = net->sctp.pf_retrans;
5077 sp->pathmtu = 0; /* allow default discovery */
5078 sp->sackdelay = net->sctp.sack_timeout;
5080 sp->param_flags = SPP_HB_ENABLE |
5082 SPP_SACKDELAY_ENABLE;
5083 sp->default_ss = SCTP_SS_DEFAULT;
5085 /* If enabled no SCTP message fragmentation will be performed.
5086 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5088 sp->disable_fragments = 0;
5090 /* Enable Nagle algorithm by default. */
5093 sp->recvrcvinfo = 0;
5094 sp->recvnxtinfo = 0;
5096 /* Enable by default. */
5099 /* Auto-close idle associations after the configured
5100 * number of seconds. A value of 0 disables this
5101 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5102 * for UDP-style sockets only.
5106 /* User specified fragmentation limit. */
5109 sp->adaptation_ind = 0;
5111 sp->pf = sctp_get_pf_specific(sk->sk_family);
5113 /* Control variables for partial data delivery. */
5114 atomic_set(&sp->pd_mode, 0);
5115 skb_queue_head_init(&sp->pd_lobby);
5116 sp->frag_interleave = 0;
5118 /* Create a per socket endpoint structure. Even if we
5119 * change the data structure relationships, this may still
5120 * be useful for storing pre-connect address information.
5122 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5128 sk->sk_destruct = sctp_destruct_sock;
5130 SCTP_DBG_OBJCNT_INC(sock);
5133 sk_sockets_allocated_inc(sk);
5134 sock_prot_inuse_add(net, sk->sk_prot, 1);
5141 /* Cleanup any SCTP per socket resources. Must be called with
5142 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5144 static void sctp_destroy_sock(struct sock *sk)
5146 struct sctp_sock *sp;
5148 pr_debug("%s: sk:%p\n", __func__, sk);
5150 /* Release our hold on the endpoint. */
5152 /* This could happen during socket init, thus we bail out
5153 * early, since the rest of the below is not setup either.
5158 if (sp->do_auto_asconf) {
5159 sp->do_auto_asconf = 0;
5160 list_del(&sp->auto_asconf_list);
5162 sctp_endpoint_free(sp->ep);
5164 sk_sockets_allocated_dec(sk);
5165 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5169 /* Triggered when there are no references on the socket anymore */
5170 static void sctp_destruct_common(struct sock *sk)
5172 struct sctp_sock *sp = sctp_sk(sk);
5174 /* Free up the HMAC transform. */
5175 crypto_free_shash(sp->hmac);
5178 static void sctp_destruct_sock(struct sock *sk)
5180 sctp_destruct_common(sk);
5181 inet_sock_destruct(sk);
5184 /* API 4.1.7 shutdown() - TCP Style Syntax
5185 * int shutdown(int socket, int how);
5187 * sd - the socket descriptor of the association to be closed.
5188 * how - Specifies the type of shutdown. The values are
5191 * Disables further receive operations. No SCTP
5192 * protocol action is taken.
5194 * Disables further send operations, and initiates
5195 * the SCTP shutdown sequence.
5197 * Disables further send and receive operations
5198 * and initiates the SCTP shutdown sequence.
5200 static void sctp_shutdown(struct sock *sk, int how)
5202 struct net *net = sock_net(sk);
5203 struct sctp_endpoint *ep;
5205 if (!sctp_style(sk, TCP))
5208 ep = sctp_sk(sk)->ep;
5209 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5210 struct sctp_association *asoc;
5212 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5213 asoc = list_entry(ep->asocs.next,
5214 struct sctp_association, asocs);
5215 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5219 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5220 struct sctp_info *info)
5222 struct sctp_transport *prim;
5223 struct list_head *pos;
5226 memset(info, 0, sizeof(*info));
5228 struct sctp_sock *sp = sctp_sk(sk);
5230 info->sctpi_s_autoclose = sp->autoclose;
5231 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5232 info->sctpi_s_pd_point = sp->pd_point;
5233 info->sctpi_s_nodelay = sp->nodelay;
5234 info->sctpi_s_disable_fragments = sp->disable_fragments;
5235 info->sctpi_s_v4mapped = sp->v4mapped;
5236 info->sctpi_s_frag_interleave = sp->frag_interleave;
5237 info->sctpi_s_type = sp->type;
5242 info->sctpi_tag = asoc->c.my_vtag;
5243 info->sctpi_state = asoc->state;
5244 info->sctpi_rwnd = asoc->a_rwnd;
5245 info->sctpi_unackdata = asoc->unack_data;
5246 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5247 info->sctpi_instrms = asoc->stream.incnt;
5248 info->sctpi_outstrms = asoc->stream.outcnt;
5249 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5250 info->sctpi_inqueue++;
5251 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5252 info->sctpi_outqueue++;
5253 info->sctpi_overall_error = asoc->overall_error_count;
5254 info->sctpi_max_burst = asoc->max_burst;
5255 info->sctpi_maxseg = asoc->frag_point;
5256 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5257 info->sctpi_peer_tag = asoc->c.peer_vtag;
5259 mask = asoc->peer.ecn_capable << 1;
5260 mask = (mask | asoc->peer.ipv4_address) << 1;
5261 mask = (mask | asoc->peer.ipv6_address) << 1;
5262 mask = (mask | asoc->peer.hostname_address) << 1;
5263 mask = (mask | asoc->peer.asconf_capable) << 1;
5264 mask = (mask | asoc->peer.prsctp_capable) << 1;
5265 mask = (mask | asoc->peer.auth_capable);
5266 info->sctpi_peer_capable = mask;
5267 mask = asoc->peer.sack_needed << 1;
5268 mask = (mask | asoc->peer.sack_generation) << 1;
5269 mask = (mask | asoc->peer.zero_window_announced);
5270 info->sctpi_peer_sack = mask;
5272 info->sctpi_isacks = asoc->stats.isacks;
5273 info->sctpi_osacks = asoc->stats.osacks;
5274 info->sctpi_opackets = asoc->stats.opackets;
5275 info->sctpi_ipackets = asoc->stats.ipackets;
5276 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5277 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5278 info->sctpi_idupchunks = asoc->stats.idupchunks;
5279 info->sctpi_gapcnt = asoc->stats.gapcnt;
5280 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5281 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5282 info->sctpi_oodchunks = asoc->stats.oodchunks;
5283 info->sctpi_iodchunks = asoc->stats.iodchunks;
5284 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5285 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5287 prim = asoc->peer.primary_path;
5288 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5289 info->sctpi_p_state = prim->state;
5290 info->sctpi_p_cwnd = prim->cwnd;
5291 info->sctpi_p_srtt = prim->srtt;
5292 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5293 info->sctpi_p_hbinterval = prim->hbinterval;
5294 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5295 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5296 info->sctpi_p_ssthresh = prim->ssthresh;
5297 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5298 info->sctpi_p_flight_size = prim->flight_size;
5299 info->sctpi_p_error = prim->error_count;
5303 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5305 /* use callback to avoid exporting the core structure */
5306 void sctp_transport_walk_start(struct rhashtable_iter *iter)
5308 rhltable_walk_enter(&sctp_transport_hashtable, iter);
5310 rhashtable_walk_start(iter);
5313 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5315 rhashtable_walk_stop(iter);
5316 rhashtable_walk_exit(iter);
5319 struct sctp_transport *sctp_transport_get_next(struct net *net,
5320 struct rhashtable_iter *iter)
5322 struct sctp_transport *t;
5324 t = rhashtable_walk_next(iter);
5325 for (; t; t = rhashtable_walk_next(iter)) {
5327 if (PTR_ERR(t) == -EAGAIN)
5332 if (!sctp_transport_hold(t))
5335 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5336 t->asoc->peer.primary_path == t)
5339 sctp_transport_put(t);
5345 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5346 struct rhashtable_iter *iter,
5349 struct sctp_transport *t;
5352 return SEQ_START_TOKEN;
5354 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5357 sctp_transport_put(t);
5363 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5367 struct sctp_ep_common *epb;
5368 struct sctp_hashbucket *head;
5370 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5372 read_lock_bh(&head->lock);
5373 sctp_for_each_hentry(epb, &head->chain) {
5374 err = cb(sctp_ep(epb), p);
5378 read_unlock_bh(&head->lock);
5383 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5385 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5387 const union sctp_addr *laddr,
5388 const union sctp_addr *paddr, void *p)
5390 struct sctp_transport *transport;
5394 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5399 err = cb(transport, p);
5400 sctp_transport_put(transport);
5404 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5406 int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
5407 struct net *net, int *pos, void *p)
5409 struct rhashtable_iter hti;
5410 struct sctp_transport *tsp;
5411 struct sctp_endpoint *ep;
5416 sctp_transport_walk_start(&hti);
5418 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5419 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5421 if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
5422 ret = cb(ep, tsp, p);
5425 sctp_endpoint_put(ep);
5428 sctp_transport_put(tsp);
5430 sctp_transport_walk_stop(&hti);
5433 if (cb_done && !cb_done(ep, tsp, p)) {
5435 sctp_endpoint_put(ep);
5436 sctp_transport_put(tsp);
5439 sctp_endpoint_put(ep);
5440 sctp_transport_put(tsp);
5445 EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
5447 /* 7.2.1 Association Status (SCTP_STATUS)
5449 * Applications can retrieve current status information about an
5450 * association, including association state, peer receiver window size,
5451 * number of unacked data chunks, and number of data chunks pending
5452 * receipt. This information is read-only.
5454 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5455 char __user *optval,
5458 struct sctp_status status;
5459 struct sctp_association *asoc = NULL;
5460 struct sctp_transport *transport;
5461 sctp_assoc_t associd;
5464 if (len < sizeof(status)) {
5469 len = sizeof(status);
5470 if (copy_from_user(&status, optval, len)) {
5475 associd = status.sstat_assoc_id;
5476 asoc = sctp_id2assoc(sk, associd);
5482 transport = asoc->peer.primary_path;
5484 status.sstat_assoc_id = sctp_assoc2id(asoc);
5485 status.sstat_state = sctp_assoc_to_state(asoc);
5486 status.sstat_rwnd = asoc->peer.rwnd;
5487 status.sstat_unackdata = asoc->unack_data;
5489 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5490 status.sstat_instrms = asoc->stream.incnt;
5491 status.sstat_outstrms = asoc->stream.outcnt;
5492 status.sstat_fragmentation_point = asoc->frag_point;
5493 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5494 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5495 transport->af_specific->sockaddr_len);
5496 /* Map ipv4 address into v4-mapped-on-v6 address. */
5497 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5498 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5499 status.sstat_primary.spinfo_state = transport->state;
5500 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5501 status.sstat_primary.spinfo_srtt = transport->srtt;
5502 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5503 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5505 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5506 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5508 if (put_user(len, optlen)) {
5513 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5514 __func__, len, status.sstat_state, status.sstat_rwnd,
5515 status.sstat_assoc_id);
5517 if (copy_to_user(optval, &status, len)) {
5527 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5529 * Applications can retrieve information about a specific peer address
5530 * of an association, including its reachability state, congestion
5531 * window, and retransmission timer values. This information is
5534 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5535 char __user *optval,
5538 struct sctp_paddrinfo pinfo;
5539 struct sctp_transport *transport;
5542 if (len < sizeof(pinfo)) {
5547 len = sizeof(pinfo);
5548 if (copy_from_user(&pinfo, optval, len)) {
5553 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5554 pinfo.spinfo_assoc_id);
5558 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5559 pinfo.spinfo_state = transport->state;
5560 pinfo.spinfo_cwnd = transport->cwnd;
5561 pinfo.spinfo_srtt = transport->srtt;
5562 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5563 pinfo.spinfo_mtu = transport->pathmtu;
5565 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5566 pinfo.spinfo_state = SCTP_ACTIVE;
5568 if (put_user(len, optlen)) {
5573 if (copy_to_user(optval, &pinfo, len)) {
5582 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5584 * This option is a on/off flag. If enabled no SCTP message
5585 * fragmentation will be performed. Instead if a message being sent
5586 * exceeds the current PMTU size, the message will NOT be sent and
5587 * instead a error will be indicated to the user.
5589 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5590 char __user *optval, int __user *optlen)
5594 if (len < sizeof(int))
5598 val = (sctp_sk(sk)->disable_fragments == 1);
5599 if (put_user(len, optlen))
5601 if (copy_to_user(optval, &val, len))
5606 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5608 * This socket option is used to specify various notifications and
5609 * ancillary data the user wishes to receive.
5611 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5614 struct sctp_event_subscribe subscribe;
5615 __u8 *sn_type = (__u8 *)&subscribe;
5620 if (len > sizeof(struct sctp_event_subscribe))
5621 len = sizeof(struct sctp_event_subscribe);
5622 if (put_user(len, optlen))
5625 for (i = 0; i < len; i++)
5626 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5627 SCTP_SN_TYPE_BASE + i);
5629 if (copy_to_user(optval, &subscribe, len))
5635 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5637 * This socket option is applicable to the UDP-style socket only. When
5638 * set it will cause associations that are idle for more than the
5639 * specified number of seconds to automatically close. An association
5640 * being idle is defined an association that has NOT sent or received
5641 * user data. The special value of '0' indicates that no automatic
5642 * close of any associations should be performed. The option expects an
5643 * integer defining the number of seconds of idle time before an
5644 * association is closed.
5646 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5648 /* Applicable to UDP-style socket only */
5649 if (sctp_style(sk, TCP))
5651 if (len < sizeof(int))
5654 if (put_user(len, optlen))
5656 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5661 /* Helper routine to branch off an association to a new socket. */
5662 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5664 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5665 struct sctp_sock *sp = sctp_sk(sk);
5666 struct socket *sock;
5669 /* Do not peel off from one netns to another one. */
5670 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5676 /* An association cannot be branched off from an already peeled-off
5677 * socket, nor is this supported for tcp style sockets.
5679 if (!sctp_style(sk, UDP))
5682 /* Create a new socket. */
5683 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5687 sctp_copy_sock(sock->sk, sk, asoc);
5689 /* Make peeled-off sockets more like 1-1 accepted sockets.
5690 * Set the daddr and initialize id to something more random and also
5691 * copy over any ip options.
5693 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
5694 sp->pf->copy_ip_options(sk, sock->sk);
5696 /* Populate the fields of the newsk from the oldsk and migrate the
5697 * asoc to the newsk.
5699 err = sctp_sock_migrate(sk, sock->sk, asoc,
5700 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5710 EXPORT_SYMBOL(sctp_do_peeloff);
5712 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5713 struct file **newfile, unsigned flags)
5715 struct socket *newsock;
5718 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5722 /* Map the socket to an unused fd that can be returned to the user. */
5723 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5725 sock_release(newsock);
5729 *newfile = sock_alloc_file(newsock, 0, NULL);
5730 if (IS_ERR(*newfile)) {
5731 put_unused_fd(retval);
5732 retval = PTR_ERR(*newfile);
5737 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5740 peeloff->sd = retval;
5742 if (flags & SOCK_NONBLOCK)
5743 (*newfile)->f_flags |= O_NONBLOCK;
5748 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5750 sctp_peeloff_arg_t peeloff;
5751 struct file *newfile = NULL;
5754 if (len < sizeof(sctp_peeloff_arg_t))
5756 len = sizeof(sctp_peeloff_arg_t);
5757 if (copy_from_user(&peeloff, optval, len))
5760 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5764 /* Return the fd mapped to the new socket. */
5765 if (put_user(len, optlen)) {
5767 put_unused_fd(retval);
5771 if (copy_to_user(optval, &peeloff, len)) {
5773 put_unused_fd(retval);
5776 fd_install(retval, newfile);
5781 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5782 char __user *optval, int __user *optlen)
5784 sctp_peeloff_flags_arg_t peeloff;
5785 struct file *newfile = NULL;
5788 if (len < sizeof(sctp_peeloff_flags_arg_t))
5790 len = sizeof(sctp_peeloff_flags_arg_t);
5791 if (copy_from_user(&peeloff, optval, len))
5794 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5795 &newfile, peeloff.flags);
5799 /* Return the fd mapped to the new socket. */
5800 if (put_user(len, optlen)) {
5802 put_unused_fd(retval);
5806 if (copy_to_user(optval, &peeloff, len)) {
5808 put_unused_fd(retval);
5811 fd_install(retval, newfile);
5816 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5818 * Applications can enable or disable heartbeats for any peer address of
5819 * an association, modify an address's heartbeat interval, force a
5820 * heartbeat to be sent immediately, and adjust the address's maximum
5821 * number of retransmissions sent before an address is considered
5822 * unreachable. The following structure is used to access and modify an
5823 * address's parameters:
5825 * struct sctp_paddrparams {
5826 * sctp_assoc_t spp_assoc_id;
5827 * struct sockaddr_storage spp_address;
5828 * uint32_t spp_hbinterval;
5829 * uint16_t spp_pathmaxrxt;
5830 * uint32_t spp_pathmtu;
5831 * uint32_t spp_sackdelay;
5832 * uint32_t spp_flags;
5835 * spp_assoc_id - (one-to-many style socket) This is filled in the
5836 * application, and identifies the association for
5838 * spp_address - This specifies which address is of interest.
5839 * spp_hbinterval - This contains the value of the heartbeat interval,
5840 * in milliseconds. If a value of zero
5841 * is present in this field then no changes are to
5842 * be made to this parameter.
5843 * spp_pathmaxrxt - This contains the maximum number of
5844 * retransmissions before this address shall be
5845 * considered unreachable. If a value of zero
5846 * is present in this field then no changes are to
5847 * be made to this parameter.
5848 * spp_pathmtu - When Path MTU discovery is disabled the value
5849 * specified here will be the "fixed" path mtu.
5850 * Note that if the spp_address field is empty
5851 * then all associations on this address will
5852 * have this fixed path mtu set upon them.
5854 * spp_sackdelay - When delayed sack is enabled, this value specifies
5855 * the number of milliseconds that sacks will be delayed
5856 * for. This value will apply to all addresses of an
5857 * association if the spp_address field is empty. Note
5858 * also, that if delayed sack is enabled and this
5859 * value is set to 0, no change is made to the last
5860 * recorded delayed sack timer value.
5862 * spp_flags - These flags are used to control various features
5863 * on an association. The flag field may contain
5864 * zero or more of the following options.
5866 * SPP_HB_ENABLE - Enable heartbeats on the
5867 * specified address. Note that if the address
5868 * field is empty all addresses for the association
5869 * have heartbeats enabled upon them.
5871 * SPP_HB_DISABLE - Disable heartbeats on the
5872 * speicifed address. Note that if the address
5873 * field is empty all addresses for the association
5874 * will have their heartbeats disabled. Note also
5875 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5876 * mutually exclusive, only one of these two should
5877 * be specified. Enabling both fields will have
5878 * undetermined results.
5880 * SPP_HB_DEMAND - Request a user initiated heartbeat
5881 * to be made immediately.
5883 * SPP_PMTUD_ENABLE - This field will enable PMTU
5884 * discovery upon the specified address. Note that
5885 * if the address feild is empty then all addresses
5886 * on the association are effected.
5888 * SPP_PMTUD_DISABLE - This field will disable PMTU
5889 * discovery upon the specified address. Note that
5890 * if the address feild is empty then all addresses
5891 * on the association are effected. Not also that
5892 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5893 * exclusive. Enabling both will have undetermined
5896 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5897 * on delayed sack. The time specified in spp_sackdelay
5898 * is used to specify the sack delay for this address. Note
5899 * that if spp_address is empty then all addresses will
5900 * enable delayed sack and take on the sack delay
5901 * value specified in spp_sackdelay.
5902 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5903 * off delayed sack. If the spp_address field is blank then
5904 * delayed sack is disabled for the entire association. Note
5905 * also that this field is mutually exclusive to
5906 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5909 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5910 * setting of the IPV6 flow label value. The value is
5911 * contained in the spp_ipv6_flowlabel field.
5912 * Upon retrieval, this flag will be set to indicate that
5913 * the spp_ipv6_flowlabel field has a valid value returned.
5914 * If a specific destination address is set (in the
5915 * spp_address field), then the value returned is that of
5916 * the address. If just an association is specified (and
5917 * no address), then the association's default flow label
5918 * is returned. If neither an association nor a destination
5919 * is specified, then the socket's default flow label is
5920 * returned. For non-IPv6 sockets, this flag will be left
5923 * SPP_DSCP: Setting this flag enables the setting of the
5924 * Differentiated Services Code Point (DSCP) value
5925 * associated with either the association or a specific
5926 * address. The value is obtained in the spp_dscp field.
5927 * Upon retrieval, this flag will be set to indicate that
5928 * the spp_dscp field has a valid value returned. If a
5929 * specific destination address is set when called (in the
5930 * spp_address field), then that specific destination
5931 * address's DSCP value is returned. If just an association
5932 * is specified, then the association's default DSCP is
5933 * returned. If neither an association nor a destination is
5934 * specified, then the socket's default DSCP is returned.
5936 * spp_ipv6_flowlabel
5937 * - This field is used in conjunction with the
5938 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5939 * The 20 least significant bits are used for the flow
5940 * label. This setting has precedence over any IPv6-layer
5943 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5944 * and contains the DSCP. The 6 most significant bits are
5945 * used for the DSCP. This setting has precedence over any
5946 * IPv4- or IPv6- layer setting.
5948 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5949 char __user *optval, int __user *optlen)
5951 struct sctp_paddrparams params;
5952 struct sctp_transport *trans = NULL;
5953 struct sctp_association *asoc = NULL;
5954 struct sctp_sock *sp = sctp_sk(sk);
5956 if (len >= sizeof(params))
5957 len = sizeof(params);
5958 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5959 spp_ipv6_flowlabel), 4))
5960 len = ALIGN(offsetof(struct sctp_paddrparams,
5961 spp_ipv6_flowlabel), 4);
5965 if (copy_from_user(¶ms, optval, len))
5968 /* If an address other than INADDR_ANY is specified, and
5969 * no transport is found, then the request is invalid.
5971 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5972 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5973 params.spp_assoc_id);
5975 pr_debug("%s: failed no transport\n", __func__);
5980 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5981 * socket is a one to many style socket, and an association
5982 * was not found, then the id was invalid.
5984 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5985 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5986 sctp_style(sk, UDP)) {
5987 pr_debug("%s: failed no association\n", __func__);
5992 /* Fetch transport values. */
5993 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5994 params.spp_pathmtu = trans->pathmtu;
5995 params.spp_pathmaxrxt = trans->pathmaxrxt;
5996 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5998 /*draft-11 doesn't say what to return in spp_flags*/
5999 params.spp_flags = trans->param_flags;
6000 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6001 params.spp_ipv6_flowlabel = trans->flowlabel &
6002 SCTP_FLOWLABEL_VAL_MASK;
6003 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6005 if (trans->dscp & SCTP_DSCP_SET_MASK) {
6006 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
6007 params.spp_flags |= SPP_DSCP;
6010 /* Fetch association values. */
6011 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
6012 params.spp_pathmtu = asoc->pathmtu;
6013 params.spp_pathmaxrxt = asoc->pathmaxrxt;
6014 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
6016 /*draft-11 doesn't say what to return in spp_flags*/
6017 params.spp_flags = asoc->param_flags;
6018 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6019 params.spp_ipv6_flowlabel = asoc->flowlabel &
6020 SCTP_FLOWLABEL_VAL_MASK;
6021 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6023 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
6024 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6025 params.spp_flags |= SPP_DSCP;
6028 /* Fetch socket values. */
6029 params.spp_hbinterval = sp->hbinterval;
6030 params.spp_pathmtu = sp->pathmtu;
6031 params.spp_sackdelay = sp->sackdelay;
6032 params.spp_pathmaxrxt = sp->pathmaxrxt;
6034 /*draft-11 doesn't say what to return in spp_flags*/
6035 params.spp_flags = sp->param_flags;
6036 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6037 params.spp_ipv6_flowlabel = sp->flowlabel &
6038 SCTP_FLOWLABEL_VAL_MASK;
6039 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6041 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6042 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6043 params.spp_flags |= SPP_DSCP;
6047 if (copy_to_user(optval, ¶ms, len))
6050 if (put_user(len, optlen))
6057 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6059 * This option will effect the way delayed acks are performed. This
6060 * option allows you to get or set the delayed ack time, in
6061 * milliseconds. It also allows changing the delayed ack frequency.
6062 * Changing the frequency to 1 disables the delayed sack algorithm. If
6063 * the assoc_id is 0, then this sets or gets the endpoints default
6064 * values. If the assoc_id field is non-zero, then the set or get
6065 * effects the specified association for the one to many model (the
6066 * assoc_id field is ignored by the one to one model). Note that if
6067 * sack_delay or sack_freq are 0 when setting this option, then the
6068 * current values will remain unchanged.
6070 * struct sctp_sack_info {
6071 * sctp_assoc_t sack_assoc_id;
6072 * uint32_t sack_delay;
6073 * uint32_t sack_freq;
6076 * sack_assoc_id - This parameter, indicates which association the user
6077 * is performing an action upon. Note that if this field's value is
6078 * zero then the endpoints default value is changed (effecting future
6079 * associations only).
6081 * sack_delay - This parameter contains the number of milliseconds that
6082 * the user is requesting the delayed ACK timer be set to. Note that
6083 * this value is defined in the standard to be between 200 and 500
6086 * sack_freq - This parameter contains the number of packets that must
6087 * be received before a sack is sent without waiting for the delay
6088 * timer to expire. The default value for this is 2, setting this
6089 * value to 1 will disable the delayed sack algorithm.
6091 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6092 char __user *optval,
6095 struct sctp_sack_info params;
6096 struct sctp_association *asoc = NULL;
6097 struct sctp_sock *sp = sctp_sk(sk);
6099 if (len >= sizeof(struct sctp_sack_info)) {
6100 len = sizeof(struct sctp_sack_info);
6102 if (copy_from_user(¶ms, optval, len))
6104 } else if (len == sizeof(struct sctp_assoc_value)) {
6105 pr_warn_ratelimited(DEPRECATED
6107 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6108 "Use struct sctp_sack_info instead\n",
6109 current->comm, task_pid_nr(current));
6110 if (copy_from_user(¶ms, optval, len))
6115 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6116 * socket is a one to many style socket, and an association
6117 * was not found, then the id was invalid.
6119 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6120 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6121 sctp_style(sk, UDP))
6125 /* Fetch association values. */
6126 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6127 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6128 params.sack_freq = asoc->sackfreq;
6131 params.sack_delay = 0;
6132 params.sack_freq = 1;
6135 /* Fetch socket values. */
6136 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6137 params.sack_delay = sp->sackdelay;
6138 params.sack_freq = sp->sackfreq;
6140 params.sack_delay = 0;
6141 params.sack_freq = 1;
6145 if (copy_to_user(optval, ¶ms, len))
6148 if (put_user(len, optlen))
6154 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6156 * Applications can specify protocol parameters for the default association
6157 * initialization. The option name argument to setsockopt() and getsockopt()
6160 * Setting initialization parameters is effective only on an unconnected
6161 * socket (for UDP-style sockets only future associations are effected
6162 * by the change). With TCP-style sockets, this option is inherited by
6163 * sockets derived from a listener socket.
6165 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6167 if (len < sizeof(struct sctp_initmsg))
6169 len = sizeof(struct sctp_initmsg);
6170 if (put_user(len, optlen))
6172 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6178 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6179 char __user *optval, int __user *optlen)
6181 struct sctp_association *asoc;
6183 struct sctp_getaddrs getaddrs;
6184 struct sctp_transport *from;
6186 union sctp_addr temp;
6187 struct sctp_sock *sp = sctp_sk(sk);
6192 if (len < sizeof(struct sctp_getaddrs))
6195 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6198 /* For UDP-style sockets, id specifies the association to query. */
6199 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6203 to = optval + offsetof(struct sctp_getaddrs, addrs);
6204 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6206 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6208 memcpy(&temp, &from->ipaddr, sizeof(temp));
6209 addrlen = sctp_get_pf_specific(sk->sk_family)
6210 ->addr_to_user(sp, &temp);
6211 if (space_left < addrlen)
6213 if (copy_to_user(to, &temp, addrlen))
6217 space_left -= addrlen;
6220 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6222 bytes_copied = ((char __user *)to) - optval;
6223 if (put_user(bytes_copied, optlen))
6229 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6230 size_t space_left, int *bytes_copied)
6232 struct sctp_sockaddr_entry *addr;
6233 union sctp_addr temp;
6236 struct net *net = sock_net(sk);
6239 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6243 if ((PF_INET == sk->sk_family) &&
6244 (AF_INET6 == addr->a.sa.sa_family))
6246 if ((PF_INET6 == sk->sk_family) &&
6247 inet_v6_ipv6only(sk) &&
6248 (AF_INET == addr->a.sa.sa_family))
6250 memcpy(&temp, &addr->a, sizeof(temp));
6251 if (!temp.v4.sin_port)
6252 temp.v4.sin_port = htons(port);
6254 addrlen = sctp_get_pf_specific(sk->sk_family)
6255 ->addr_to_user(sctp_sk(sk), &temp);
6257 if (space_left < addrlen) {
6261 memcpy(to, &temp, addrlen);
6265 space_left -= addrlen;
6266 *bytes_copied += addrlen;
6274 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6275 char __user *optval, int __user *optlen)
6277 struct sctp_bind_addr *bp;
6278 struct sctp_association *asoc;
6280 struct sctp_getaddrs getaddrs;
6281 struct sctp_sockaddr_entry *addr;
6283 union sctp_addr temp;
6284 struct sctp_sock *sp = sctp_sk(sk);
6288 int bytes_copied = 0;
6292 if (len < sizeof(struct sctp_getaddrs))
6295 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6299 * For UDP-style sockets, id specifies the association to query.
6300 * If the id field is set to the value '0' then the locally bound
6301 * addresses are returned without regard to any particular
6304 if (0 == getaddrs.assoc_id) {
6305 bp = &sctp_sk(sk)->ep->base.bind_addr;
6307 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6310 bp = &asoc->base.bind_addr;
6313 to = optval + offsetof(struct sctp_getaddrs, addrs);
6314 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6316 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6320 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6321 * addresses from the global local address list.
6323 if (sctp_list_single_entry(&bp->address_list)) {
6324 addr = list_entry(bp->address_list.next,
6325 struct sctp_sockaddr_entry, list);
6326 if (sctp_is_any(sk, &addr->a)) {
6327 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6328 space_left, &bytes_copied);
6338 /* Protection on the bound address list is not needed since
6339 * in the socket option context we hold a socket lock and
6340 * thus the bound address list can't change.
6342 list_for_each_entry(addr, &bp->address_list, list) {
6343 memcpy(&temp, &addr->a, sizeof(temp));
6344 addrlen = sctp_get_pf_specific(sk->sk_family)
6345 ->addr_to_user(sp, &temp);
6346 if (space_left < addrlen) {
6347 err = -ENOMEM; /*fixme: right error?*/
6350 memcpy(buf, &temp, addrlen);
6352 bytes_copied += addrlen;
6354 space_left -= addrlen;
6358 if (copy_to_user(to, addrs, bytes_copied)) {
6362 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6366 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6367 * but we can't change it anymore.
6369 if (put_user(bytes_copied, optlen))
6376 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6378 * Requests that the local SCTP stack use the enclosed peer address as
6379 * the association primary. The enclosed address must be one of the
6380 * association peer's addresses.
6382 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6383 char __user *optval, int __user *optlen)
6385 struct sctp_prim prim;
6386 struct sctp_association *asoc;
6387 struct sctp_sock *sp = sctp_sk(sk);
6389 if (len < sizeof(struct sctp_prim))
6392 len = sizeof(struct sctp_prim);
6394 if (copy_from_user(&prim, optval, len))
6397 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6401 if (!asoc->peer.primary_path)
6404 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6405 asoc->peer.primary_path->af_specific->sockaddr_len);
6407 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6408 (union sctp_addr *)&prim.ssp_addr);
6410 if (put_user(len, optlen))
6412 if (copy_to_user(optval, &prim, len))
6419 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6421 * Requests that the local endpoint set the specified Adaptation Layer
6422 * Indication parameter for all future INIT and INIT-ACK exchanges.
6424 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6425 char __user *optval, int __user *optlen)
6427 struct sctp_setadaptation adaptation;
6429 if (len < sizeof(struct sctp_setadaptation))
6432 len = sizeof(struct sctp_setadaptation);
6434 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6436 if (put_user(len, optlen))
6438 if (copy_to_user(optval, &adaptation, len))
6446 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6448 * Applications that wish to use the sendto() system call may wish to
6449 * specify a default set of parameters that would normally be supplied
6450 * through the inclusion of ancillary data. This socket option allows
6451 * such an application to set the default sctp_sndrcvinfo structure.
6454 * The application that wishes to use this socket option simply passes
6455 * in to this call the sctp_sndrcvinfo structure defined in Section
6456 * 5.2.2) The input parameters accepted by this call include
6457 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6458 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6459 * to this call if the caller is using the UDP model.
6461 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6463 static int sctp_getsockopt_default_send_param(struct sock *sk,
6464 int len, char __user *optval,
6467 struct sctp_sock *sp = sctp_sk(sk);
6468 struct sctp_association *asoc;
6469 struct sctp_sndrcvinfo info;
6471 if (len < sizeof(info))
6476 if (copy_from_user(&info, optval, len))
6479 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6480 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6481 sctp_style(sk, UDP))
6485 info.sinfo_stream = asoc->default_stream;
6486 info.sinfo_flags = asoc->default_flags;
6487 info.sinfo_ppid = asoc->default_ppid;
6488 info.sinfo_context = asoc->default_context;
6489 info.sinfo_timetolive = asoc->default_timetolive;
6491 info.sinfo_stream = sp->default_stream;
6492 info.sinfo_flags = sp->default_flags;
6493 info.sinfo_ppid = sp->default_ppid;
6494 info.sinfo_context = sp->default_context;
6495 info.sinfo_timetolive = sp->default_timetolive;
6498 if (put_user(len, optlen))
6500 if (copy_to_user(optval, &info, len))
6506 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6507 * (SCTP_DEFAULT_SNDINFO)
6509 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6510 char __user *optval,
6513 struct sctp_sock *sp = sctp_sk(sk);
6514 struct sctp_association *asoc;
6515 struct sctp_sndinfo info;
6517 if (len < sizeof(info))
6522 if (copy_from_user(&info, optval, len))
6525 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6526 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6527 sctp_style(sk, UDP))
6531 info.snd_sid = asoc->default_stream;
6532 info.snd_flags = asoc->default_flags;
6533 info.snd_ppid = asoc->default_ppid;
6534 info.snd_context = asoc->default_context;
6536 info.snd_sid = sp->default_stream;
6537 info.snd_flags = sp->default_flags;
6538 info.snd_ppid = sp->default_ppid;
6539 info.snd_context = sp->default_context;
6542 if (put_user(len, optlen))
6544 if (copy_to_user(optval, &info, len))
6552 * 7.1.5 SCTP_NODELAY
6554 * Turn on/off any Nagle-like algorithm. This means that packets are
6555 * generally sent as soon as possible and no unnecessary delays are
6556 * introduced, at the cost of more packets in the network. Expects an
6557 * integer boolean flag.
6560 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6561 char __user *optval, int __user *optlen)
6565 if (len < sizeof(int))
6569 val = (sctp_sk(sk)->nodelay == 1);
6570 if (put_user(len, optlen))
6572 if (copy_to_user(optval, &val, len))
6579 * 7.1.1 SCTP_RTOINFO
6581 * The protocol parameters used to initialize and bound retransmission
6582 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6583 * and modify these parameters.
6584 * All parameters are time values, in milliseconds. A value of 0, when
6585 * modifying the parameters, indicates that the current value should not
6589 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6590 char __user *optval,
6591 int __user *optlen) {
6592 struct sctp_rtoinfo rtoinfo;
6593 struct sctp_association *asoc;
6595 if (len < sizeof (struct sctp_rtoinfo))
6598 len = sizeof(struct sctp_rtoinfo);
6600 if (copy_from_user(&rtoinfo, optval, len))
6603 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6605 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6606 sctp_style(sk, UDP))
6609 /* Values corresponding to the specific association. */
6611 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6612 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6613 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6615 /* Values corresponding to the endpoint. */
6616 struct sctp_sock *sp = sctp_sk(sk);
6618 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6619 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6620 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6623 if (put_user(len, optlen))
6626 if (copy_to_user(optval, &rtoinfo, len))
6634 * 7.1.2 SCTP_ASSOCINFO
6636 * This option is used to tune the maximum retransmission attempts
6637 * of the association.
6638 * Returns an error if the new association retransmission value is
6639 * greater than the sum of the retransmission value of the peer.
6640 * See [SCTP] for more information.
6643 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6644 char __user *optval,
6648 struct sctp_assocparams assocparams;
6649 struct sctp_association *asoc;
6650 struct list_head *pos;
6653 if (len < sizeof (struct sctp_assocparams))
6656 len = sizeof(struct sctp_assocparams);
6658 if (copy_from_user(&assocparams, optval, len))
6661 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6663 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6664 sctp_style(sk, UDP))
6667 /* Values correspoinding to the specific association */
6669 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6670 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6671 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6672 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6674 list_for_each(pos, &asoc->peer.transport_addr_list) {
6678 assocparams.sasoc_number_peer_destinations = cnt;
6680 /* Values corresponding to the endpoint */
6681 struct sctp_sock *sp = sctp_sk(sk);
6683 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6684 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6685 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6686 assocparams.sasoc_cookie_life =
6687 sp->assocparams.sasoc_cookie_life;
6688 assocparams.sasoc_number_peer_destinations =
6690 sasoc_number_peer_destinations;
6693 if (put_user(len, optlen))
6696 if (copy_to_user(optval, &assocparams, len))
6703 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6705 * This socket option is a boolean flag which turns on or off mapped V4
6706 * addresses. If this option is turned on and the socket is type
6707 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6708 * If this option is turned off, then no mapping will be done of V4
6709 * addresses and a user will receive both PF_INET6 and PF_INET type
6710 * addresses on the socket.
6712 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6713 char __user *optval, int __user *optlen)
6716 struct sctp_sock *sp = sctp_sk(sk);
6718 if (len < sizeof(int))
6723 if (put_user(len, optlen))
6725 if (copy_to_user(optval, &val, len))
6732 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6733 * (chapter and verse is quoted at sctp_setsockopt_context())
6735 static int sctp_getsockopt_context(struct sock *sk, int len,
6736 char __user *optval, int __user *optlen)
6738 struct sctp_assoc_value params;
6739 struct sctp_association *asoc;
6741 if (len < sizeof(struct sctp_assoc_value))
6744 len = sizeof(struct sctp_assoc_value);
6746 if (copy_from_user(¶ms, optval, len))
6749 asoc = sctp_id2assoc(sk, params.assoc_id);
6750 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6751 sctp_style(sk, UDP))
6754 params.assoc_value = asoc ? asoc->default_rcv_context
6755 : sctp_sk(sk)->default_rcv_context;
6757 if (put_user(len, optlen))
6759 if (copy_to_user(optval, ¶ms, len))
6766 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6767 * This option will get or set the maximum size to put in any outgoing
6768 * SCTP DATA chunk. If a message is larger than this size it will be
6769 * fragmented by SCTP into the specified size. Note that the underlying
6770 * SCTP implementation may fragment into smaller sized chunks when the
6771 * PMTU of the underlying association is smaller than the value set by
6772 * the user. The default value for this option is '0' which indicates
6773 * the user is NOT limiting fragmentation and only the PMTU will effect
6774 * SCTP's choice of DATA chunk size. Note also that values set larger
6775 * than the maximum size of an IP datagram will effectively let SCTP
6776 * control fragmentation (i.e. the same as setting this option to 0).
6778 * The following structure is used to access and modify this parameter:
6780 * struct sctp_assoc_value {
6781 * sctp_assoc_t assoc_id;
6782 * uint32_t assoc_value;
6785 * assoc_id: This parameter is ignored for one-to-one style sockets.
6786 * For one-to-many style sockets this parameter indicates which
6787 * association the user is performing an action upon. Note that if
6788 * this field's value is zero then the endpoints default value is
6789 * changed (effecting future associations only).
6790 * assoc_value: This parameter specifies the maximum size in bytes.
6792 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6793 char __user *optval, int __user *optlen)
6795 struct sctp_assoc_value params;
6796 struct sctp_association *asoc;
6798 if (len == sizeof(int)) {
6799 pr_warn_ratelimited(DEPRECATED
6801 "Use of int in maxseg socket option.\n"
6802 "Use struct sctp_assoc_value instead\n",
6803 current->comm, task_pid_nr(current));
6804 params.assoc_id = SCTP_FUTURE_ASSOC;
6805 } else if (len >= sizeof(struct sctp_assoc_value)) {
6806 len = sizeof(struct sctp_assoc_value);
6807 if (copy_from_user(¶ms, optval, len))
6812 asoc = sctp_id2assoc(sk, params.assoc_id);
6813 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6814 sctp_style(sk, UDP))
6818 params.assoc_value = asoc->frag_point;
6820 params.assoc_value = sctp_sk(sk)->user_frag;
6822 if (put_user(len, optlen))
6824 if (len == sizeof(int)) {
6825 if (copy_to_user(optval, ¶ms.assoc_value, len))
6828 if (copy_to_user(optval, ¶ms, len))
6836 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6837 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6839 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6840 char __user *optval, int __user *optlen)
6844 if (len < sizeof(int))
6849 val = sctp_sk(sk)->frag_interleave;
6850 if (put_user(len, optlen))
6852 if (copy_to_user(optval, &val, len))
6859 * 7.1.25. Set or Get the sctp partial delivery point
6860 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6862 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6863 char __user *optval,
6868 if (len < sizeof(u32))
6873 val = sctp_sk(sk)->pd_point;
6874 if (put_user(len, optlen))
6876 if (copy_to_user(optval, &val, len))
6883 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6884 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6886 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6887 char __user *optval,
6890 struct sctp_assoc_value params;
6891 struct sctp_association *asoc;
6893 if (len == sizeof(int)) {
6894 pr_warn_ratelimited(DEPRECATED
6896 "Use of int in max_burst socket option.\n"
6897 "Use struct sctp_assoc_value instead\n",
6898 current->comm, task_pid_nr(current));
6899 params.assoc_id = SCTP_FUTURE_ASSOC;
6900 } else if (len >= sizeof(struct sctp_assoc_value)) {
6901 len = sizeof(struct sctp_assoc_value);
6902 if (copy_from_user(¶ms, optval, len))
6907 asoc = sctp_id2assoc(sk, params.assoc_id);
6908 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6909 sctp_style(sk, UDP))
6912 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6914 if (len == sizeof(int)) {
6915 if (copy_to_user(optval, ¶ms.assoc_value, len))
6918 if (copy_to_user(optval, ¶ms, len))
6926 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6927 char __user *optval, int __user *optlen)
6929 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6930 struct sctp_hmacalgo __user *p = (void __user *)optval;
6931 struct sctp_hmac_algo_param *hmacs;
6936 if (!ep->auth_enable)
6939 hmacs = ep->auth_hmacs_list;
6940 data_len = ntohs(hmacs->param_hdr.length) -
6941 sizeof(struct sctp_paramhdr);
6943 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6946 len = sizeof(struct sctp_hmacalgo) + data_len;
6947 num_idents = data_len / sizeof(u16);
6949 if (put_user(len, optlen))
6951 if (put_user(num_idents, &p->shmac_num_idents))
6953 for (i = 0; i < num_idents; i++) {
6954 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6956 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6962 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6963 char __user *optval, int __user *optlen)
6965 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6966 struct sctp_authkeyid val;
6967 struct sctp_association *asoc;
6969 if (len < sizeof(struct sctp_authkeyid))
6972 len = sizeof(struct sctp_authkeyid);
6973 if (copy_from_user(&val, optval, len))
6976 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6977 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6981 if (!asoc->peer.auth_capable)
6983 val.scact_keynumber = asoc->active_key_id;
6985 if (!ep->auth_enable)
6987 val.scact_keynumber = ep->active_key_id;
6990 if (put_user(len, optlen))
6992 if (copy_to_user(optval, &val, len))
6998 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6999 char __user *optval, int __user *optlen)
7001 struct sctp_authchunks __user *p = (void __user *)optval;
7002 struct sctp_authchunks val;
7003 struct sctp_association *asoc;
7004 struct sctp_chunks_param *ch;
7008 if (len < sizeof(struct sctp_authchunks))
7011 if (copy_from_user(&val, optval, sizeof(val)))
7014 to = p->gauth_chunks;
7015 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7019 if (!asoc->peer.auth_capable)
7022 ch = asoc->peer.peer_chunks;
7026 /* See if the user provided enough room for all the data */
7027 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7028 if (len < num_chunks)
7031 if (copy_to_user(to, ch->chunks, num_chunks))
7034 len = sizeof(struct sctp_authchunks) + num_chunks;
7035 if (put_user(len, optlen))
7037 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7042 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
7043 char __user *optval, int __user *optlen)
7045 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7046 struct sctp_authchunks __user *p = (void __user *)optval;
7047 struct sctp_authchunks val;
7048 struct sctp_association *asoc;
7049 struct sctp_chunks_param *ch;
7053 if (len < sizeof(struct sctp_authchunks))
7056 if (copy_from_user(&val, optval, sizeof(val)))
7059 to = p->gauth_chunks;
7060 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7061 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7062 sctp_style(sk, UDP))
7066 if (!asoc->peer.auth_capable)
7068 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
7070 if (!ep->auth_enable)
7072 ch = ep->auth_chunk_list;
7077 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7078 if (len < sizeof(struct sctp_authchunks) + num_chunks)
7081 if (copy_to_user(to, ch->chunks, num_chunks))
7084 len = sizeof(struct sctp_authchunks) + num_chunks;
7085 if (put_user(len, optlen))
7087 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7094 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7095 * This option gets the current number of associations that are attached
7096 * to a one-to-many style socket. The option value is an uint32_t.
7098 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7099 char __user *optval, int __user *optlen)
7101 struct sctp_sock *sp = sctp_sk(sk);
7102 struct sctp_association *asoc;
7105 if (sctp_style(sk, TCP))
7108 if (len < sizeof(u32))
7113 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7117 if (put_user(len, optlen))
7119 if (copy_to_user(optval, &val, len))
7126 * 8.1.23 SCTP_AUTO_ASCONF
7127 * See the corresponding setsockopt entry as description
7129 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7130 char __user *optval, int __user *optlen)
7134 if (len < sizeof(int))
7138 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7140 if (put_user(len, optlen))
7142 if (copy_to_user(optval, &val, len))
7148 * 8.2.6. Get the Current Identifiers of Associations
7149 * (SCTP_GET_ASSOC_ID_LIST)
7151 * This option gets the current list of SCTP association identifiers of
7152 * the SCTP associations handled by a one-to-many style socket.
7154 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7155 char __user *optval, int __user *optlen)
7157 struct sctp_sock *sp = sctp_sk(sk);
7158 struct sctp_association *asoc;
7159 struct sctp_assoc_ids *ids;
7162 if (sctp_style(sk, TCP))
7165 if (len < sizeof(struct sctp_assoc_ids))
7168 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7172 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7175 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7177 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7181 ids->gaids_number_of_ids = num;
7183 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7184 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7187 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7197 * SCTP_PEER_ADDR_THLDS
7199 * This option allows us to fetch the partially failed threshold for one or all
7200 * transports in an association. See Section 6.1 of:
7201 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7203 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7204 char __user *optval,
7208 struct sctp_paddrthlds val;
7209 struct sctp_transport *trans;
7210 struct sctp_association *asoc;
7212 if (len < sizeof(struct sctp_paddrthlds))
7214 len = sizeof(struct sctp_paddrthlds);
7215 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7218 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7219 trans = sctp_addr_id2transport(sk, &val.spt_address,
7224 val.spt_pathmaxrxt = trans->pathmaxrxt;
7225 val.spt_pathpfthld = trans->pf_retrans;
7230 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7231 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7232 sctp_style(sk, UDP))
7236 val.spt_pathpfthld = asoc->pf_retrans;
7237 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7239 struct sctp_sock *sp = sctp_sk(sk);
7241 val.spt_pathpfthld = sp->pf_retrans;
7242 val.spt_pathmaxrxt = sp->pathmaxrxt;
7246 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7253 * SCTP_GET_ASSOC_STATS
7255 * This option retrieves local per endpoint statistics. It is modeled
7256 * after OpenSolaris' implementation
7258 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7259 char __user *optval,
7262 struct sctp_assoc_stats sas;
7263 struct sctp_association *asoc = NULL;
7265 /* User must provide at least the assoc id */
7266 if (len < sizeof(sctp_assoc_t))
7269 /* Allow the struct to grow and fill in as much as possible */
7270 len = min_t(size_t, len, sizeof(sas));
7272 if (copy_from_user(&sas, optval, len))
7275 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7279 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7280 sas.sas_gapcnt = asoc->stats.gapcnt;
7281 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7282 sas.sas_osacks = asoc->stats.osacks;
7283 sas.sas_isacks = asoc->stats.isacks;
7284 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7285 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7286 sas.sas_oodchunks = asoc->stats.oodchunks;
7287 sas.sas_iodchunks = asoc->stats.iodchunks;
7288 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7289 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7290 sas.sas_idupchunks = asoc->stats.idupchunks;
7291 sas.sas_opackets = asoc->stats.opackets;
7292 sas.sas_ipackets = asoc->stats.ipackets;
7294 /* New high max rto observed, will return 0 if not a single
7295 * RTO update took place. obs_rto_ipaddr will be bogus
7298 sas.sas_maxrto = asoc->stats.max_obs_rto;
7299 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7300 sizeof(struct sockaddr_storage));
7302 /* Mark beginning of a new observation period */
7303 asoc->stats.max_obs_rto = asoc->rto_min;
7305 if (put_user(len, optlen))
7308 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7310 if (copy_to_user(optval, &sas, len))
7316 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7317 char __user *optval,
7322 if (len < sizeof(int))
7326 if (sctp_sk(sk)->recvrcvinfo)
7328 if (put_user(len, optlen))
7330 if (copy_to_user(optval, &val, len))
7336 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7337 char __user *optval,
7342 if (len < sizeof(int))
7346 if (sctp_sk(sk)->recvnxtinfo)
7348 if (put_user(len, optlen))
7350 if (copy_to_user(optval, &val, len))
7356 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7357 char __user *optval,
7360 struct sctp_assoc_value params;
7361 struct sctp_association *asoc;
7362 int retval = -EFAULT;
7364 if (len < sizeof(params)) {
7369 len = sizeof(params);
7370 if (copy_from_user(¶ms, optval, len))
7373 asoc = sctp_id2assoc(sk, params.assoc_id);
7374 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7375 sctp_style(sk, UDP)) {
7380 params.assoc_value = asoc ? asoc->peer.prsctp_capable
7381 : sctp_sk(sk)->ep->prsctp_enable;
7383 if (put_user(len, optlen))
7386 if (copy_to_user(optval, ¶ms, len))
7395 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7396 char __user *optval,
7399 struct sctp_default_prinfo info;
7400 struct sctp_association *asoc;
7401 int retval = -EFAULT;
7403 if (len < sizeof(info)) {
7409 if (copy_from_user(&info, optval, len))
7412 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7413 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7414 sctp_style(sk, UDP)) {
7420 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7421 info.pr_value = asoc->default_timetolive;
7423 struct sctp_sock *sp = sctp_sk(sk);
7425 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7426 info.pr_value = sp->default_timetolive;
7429 if (put_user(len, optlen))
7432 if (copy_to_user(optval, &info, len))
7441 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7442 char __user *optval,
7445 struct sctp_prstatus params;
7446 struct sctp_association *asoc;
7448 int retval = -EINVAL;
7450 if (len < sizeof(params))
7453 len = sizeof(params);
7454 if (copy_from_user(¶ms, optval, len)) {
7459 policy = params.sprstat_policy;
7460 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7461 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7464 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7468 if (policy == SCTP_PR_SCTP_ALL) {
7469 params.sprstat_abandoned_unsent = 0;
7470 params.sprstat_abandoned_sent = 0;
7471 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7472 params.sprstat_abandoned_unsent +=
7473 asoc->abandoned_unsent[policy];
7474 params.sprstat_abandoned_sent +=
7475 asoc->abandoned_sent[policy];
7478 params.sprstat_abandoned_unsent =
7479 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7480 params.sprstat_abandoned_sent =
7481 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7484 if (put_user(len, optlen)) {
7489 if (copy_to_user(optval, ¶ms, len)) {
7500 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7501 char __user *optval,
7504 struct sctp_stream_out_ext *streamoute;
7505 struct sctp_association *asoc;
7506 struct sctp_prstatus params;
7507 int retval = -EINVAL;
7510 if (len < sizeof(params))
7513 len = sizeof(params);
7514 if (copy_from_user(¶ms, optval, len)) {
7519 policy = params.sprstat_policy;
7520 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7521 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7524 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7525 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7528 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7530 /* Not allocated yet, means all stats are 0 */
7531 params.sprstat_abandoned_unsent = 0;
7532 params.sprstat_abandoned_sent = 0;
7537 if (policy == SCTP_PR_SCTP_ALL) {
7538 params.sprstat_abandoned_unsent = 0;
7539 params.sprstat_abandoned_sent = 0;
7540 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7541 params.sprstat_abandoned_unsent +=
7542 streamoute->abandoned_unsent[policy];
7543 params.sprstat_abandoned_sent +=
7544 streamoute->abandoned_sent[policy];
7547 params.sprstat_abandoned_unsent =
7548 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7549 params.sprstat_abandoned_sent =
7550 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7553 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7564 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7565 char __user *optval,
7568 struct sctp_assoc_value params;
7569 struct sctp_association *asoc;
7570 int retval = -EFAULT;
7572 if (len < sizeof(params)) {
7577 len = sizeof(params);
7578 if (copy_from_user(¶ms, optval, len))
7581 asoc = sctp_id2assoc(sk, params.assoc_id);
7582 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7583 sctp_style(sk, UDP)) {
7588 params.assoc_value = asoc ? asoc->peer.reconf_capable
7589 : sctp_sk(sk)->ep->reconf_enable;
7591 if (put_user(len, optlen))
7594 if (copy_to_user(optval, ¶ms, len))
7603 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7604 char __user *optval,
7607 struct sctp_assoc_value params;
7608 struct sctp_association *asoc;
7609 int retval = -EFAULT;
7611 if (len < sizeof(params)) {
7616 len = sizeof(params);
7617 if (copy_from_user(¶ms, optval, len))
7620 asoc = sctp_id2assoc(sk, params.assoc_id);
7621 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7622 sctp_style(sk, UDP)) {
7627 params.assoc_value = asoc ? asoc->strreset_enable
7628 : sctp_sk(sk)->ep->strreset_enable;
7630 if (put_user(len, optlen))
7633 if (copy_to_user(optval, ¶ms, len))
7642 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7643 char __user *optval,
7646 struct sctp_assoc_value params;
7647 struct sctp_association *asoc;
7648 int retval = -EFAULT;
7650 if (len < sizeof(params)) {
7655 len = sizeof(params);
7656 if (copy_from_user(¶ms, optval, len))
7659 asoc = sctp_id2assoc(sk, params.assoc_id);
7660 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7661 sctp_style(sk, UDP)) {
7666 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7667 : sctp_sk(sk)->default_ss;
7669 if (put_user(len, optlen))
7672 if (copy_to_user(optval, ¶ms, len))
7681 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7682 char __user *optval,
7685 struct sctp_stream_value params;
7686 struct sctp_association *asoc;
7687 int retval = -EFAULT;
7689 if (len < sizeof(params)) {
7694 len = sizeof(params);
7695 if (copy_from_user(¶ms, optval, len))
7698 asoc = sctp_id2assoc(sk, params.assoc_id);
7704 retval = sctp_sched_get_value(asoc, params.stream_id,
7705 ¶ms.stream_value);
7709 if (put_user(len, optlen)) {
7714 if (copy_to_user(optval, ¶ms, len)) {
7723 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7724 char __user *optval,
7727 struct sctp_assoc_value params;
7728 struct sctp_association *asoc;
7729 int retval = -EFAULT;
7731 if (len < sizeof(params)) {
7736 len = sizeof(params);
7737 if (copy_from_user(¶ms, optval, len))
7740 asoc = sctp_id2assoc(sk, params.assoc_id);
7741 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7742 sctp_style(sk, UDP)) {
7747 params.assoc_value = asoc ? asoc->peer.intl_capable
7748 : sctp_sk(sk)->ep->intl_enable;
7750 if (put_user(len, optlen))
7753 if (copy_to_user(optval, ¶ms, len))
7762 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7763 char __user *optval,
7768 if (len < sizeof(int))
7772 val = sctp_sk(sk)->reuse;
7773 if (put_user(len, optlen))
7776 if (copy_to_user(optval, &val, len))
7782 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7785 struct sctp_association *asoc;
7786 struct sctp_event param;
7789 if (len < sizeof(param))
7792 len = sizeof(param);
7793 if (copy_from_user(¶m, optval, len))
7796 if (param.se_type < SCTP_SN_TYPE_BASE ||
7797 param.se_type > SCTP_SN_TYPE_MAX)
7800 asoc = sctp_id2assoc(sk, param.se_assoc_id);
7801 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7802 sctp_style(sk, UDP))
7805 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7806 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7808 if (put_user(len, optlen))
7811 if (copy_to_user(optval, ¶m, len))
7817 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
7818 char __user *optval,
7821 struct sctp_assoc_value params;
7822 struct sctp_association *asoc;
7823 int retval = -EFAULT;
7825 if (len < sizeof(params)) {
7830 len = sizeof(params);
7831 if (copy_from_user(¶ms, optval, len))
7834 asoc = sctp_id2assoc(sk, params.assoc_id);
7835 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7836 sctp_style(sk, UDP)) {
7841 params.assoc_value = asoc ? asoc->peer.asconf_capable
7842 : sctp_sk(sk)->ep->asconf_enable;
7844 if (put_user(len, optlen))
7847 if (copy_to_user(optval, ¶ms, len))
7856 static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
7857 char __user *optval,
7860 struct sctp_assoc_value params;
7861 struct sctp_association *asoc;
7862 int retval = -EFAULT;
7864 if (len < sizeof(params)) {
7869 len = sizeof(params);
7870 if (copy_from_user(¶ms, optval, len))
7873 asoc = sctp_id2assoc(sk, params.assoc_id);
7874 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7875 sctp_style(sk, UDP)) {
7880 params.assoc_value = asoc ? asoc->peer.auth_capable
7881 : sctp_sk(sk)->ep->auth_enable;
7883 if (put_user(len, optlen))
7886 if (copy_to_user(optval, ¶ms, len))
7895 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
7896 char __user *optval,
7899 struct sctp_assoc_value params;
7900 struct sctp_association *asoc;
7901 int retval = -EFAULT;
7903 if (len < sizeof(params)) {
7908 len = sizeof(params);
7909 if (copy_from_user(¶ms, optval, len))
7912 asoc = sctp_id2assoc(sk, params.assoc_id);
7913 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7914 sctp_style(sk, UDP)) {
7919 params.assoc_value = asoc ? asoc->peer.ecn_capable
7920 : sctp_sk(sk)->ep->ecn_enable;
7922 if (put_user(len, optlen))
7925 if (copy_to_user(optval, ¶ms, len))
7934 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7935 char __user *optval, int __user *optlen)
7940 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7942 /* I can hardly begin to describe how wrong this is. This is
7943 * so broken as to be worse than useless. The API draft
7944 * REALLY is NOT helpful here... I am not convinced that the
7945 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7946 * are at all well-founded.
7948 if (level != SOL_SCTP) {
7949 struct sctp_af *af = sctp_sk(sk)->pf->af;
7951 retval = af->getsockopt(sk, level, optname, optval, optlen);
7955 if (get_user(len, optlen))
7965 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7967 case SCTP_DISABLE_FRAGMENTS:
7968 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7972 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7974 case SCTP_AUTOCLOSE:
7975 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7977 case SCTP_SOCKOPT_PEELOFF:
7978 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7980 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7981 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7983 case SCTP_PEER_ADDR_PARAMS:
7984 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7987 case SCTP_DELAYED_SACK:
7988 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7992 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7994 case SCTP_GET_PEER_ADDRS:
7995 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7998 case SCTP_GET_LOCAL_ADDRS:
7999 retval = sctp_getsockopt_local_addrs(sk, len, optval,
8002 case SCTP_SOCKOPT_CONNECTX3:
8003 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
8005 case SCTP_DEFAULT_SEND_PARAM:
8006 retval = sctp_getsockopt_default_send_param(sk, len,
8009 case SCTP_DEFAULT_SNDINFO:
8010 retval = sctp_getsockopt_default_sndinfo(sk, len,
8013 case SCTP_PRIMARY_ADDR:
8014 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
8017 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
8020 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
8022 case SCTP_ASSOCINFO:
8023 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
8025 case SCTP_I_WANT_MAPPED_V4_ADDR:
8026 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
8029 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
8031 case SCTP_GET_PEER_ADDR_INFO:
8032 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
8035 case SCTP_ADAPTATION_LAYER:
8036 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
8040 retval = sctp_getsockopt_context(sk, len, optval, optlen);
8042 case SCTP_FRAGMENT_INTERLEAVE:
8043 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
8046 case SCTP_PARTIAL_DELIVERY_POINT:
8047 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
8050 case SCTP_MAX_BURST:
8051 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
8054 case SCTP_AUTH_CHUNK:
8055 case SCTP_AUTH_DELETE_KEY:
8056 case SCTP_AUTH_DEACTIVATE_KEY:
8057 retval = -EOPNOTSUPP;
8059 case SCTP_HMAC_IDENT:
8060 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
8062 case SCTP_AUTH_ACTIVE_KEY:
8063 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
8065 case SCTP_PEER_AUTH_CHUNKS:
8066 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
8069 case SCTP_LOCAL_AUTH_CHUNKS:
8070 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
8073 case SCTP_GET_ASSOC_NUMBER:
8074 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
8076 case SCTP_GET_ASSOC_ID_LIST:
8077 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
8079 case SCTP_AUTO_ASCONF:
8080 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
8082 case SCTP_PEER_ADDR_THLDS:
8083 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
8085 case SCTP_GET_ASSOC_STATS:
8086 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
8088 case SCTP_RECVRCVINFO:
8089 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
8091 case SCTP_RECVNXTINFO:
8092 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
8094 case SCTP_PR_SUPPORTED:
8095 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
8097 case SCTP_DEFAULT_PRINFO:
8098 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
8101 case SCTP_PR_ASSOC_STATUS:
8102 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
8105 case SCTP_PR_STREAM_STATUS:
8106 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
8109 case SCTP_RECONFIG_SUPPORTED:
8110 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
8113 case SCTP_ENABLE_STREAM_RESET:
8114 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
8117 case SCTP_STREAM_SCHEDULER:
8118 retval = sctp_getsockopt_scheduler(sk, len, optval,
8121 case SCTP_STREAM_SCHEDULER_VALUE:
8122 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
8125 case SCTP_INTERLEAVING_SUPPORTED:
8126 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
8129 case SCTP_REUSE_PORT:
8130 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
8133 retval = sctp_getsockopt_event(sk, len, optval, optlen);
8135 case SCTP_ASCONF_SUPPORTED:
8136 retval = sctp_getsockopt_asconf_supported(sk, len, optval,
8139 case SCTP_AUTH_SUPPORTED:
8140 retval = sctp_getsockopt_auth_supported(sk, len, optval,
8143 case SCTP_ECN_SUPPORTED:
8144 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
8147 retval = -ENOPROTOOPT;
8155 static int sctp_hash(struct sock *sk)
8161 static void sctp_unhash(struct sock *sk)
8166 /* Check if port is acceptable. Possibly find first available port.
8168 * The port hash table (contained in the 'global' SCTP protocol storage
8169 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
8170 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
8171 * list (the list number is the port number hashed out, so as you
8172 * would expect from a hash function, all the ports in a given list have
8173 * such a number that hashes out to the same list number; you were
8174 * expecting that, right?); so each list has a set of ports, with a
8175 * link to the socket (struct sock) that uses it, the port number and
8176 * a fastreuse flag (FIXME: NPI ipg).
8178 static struct sctp_bind_bucket *sctp_bucket_create(
8179 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8181 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8183 struct sctp_sock *sp = sctp_sk(sk);
8184 bool reuse = (sk->sk_reuse || sp->reuse);
8185 struct sctp_bind_hashbucket *head; /* hash list */
8186 kuid_t uid = sock_i_uid(sk);
8187 struct sctp_bind_bucket *pp;
8188 unsigned short snum;
8191 snum = ntohs(addr->v4.sin_port);
8193 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8196 /* Search for an available port. */
8197 int low, high, remaining, index;
8199 struct net *net = sock_net(sk);
8201 inet_get_local_port_range(net, &low, &high);
8202 remaining = (high - low) + 1;
8203 rover = prandom_u32() % remaining + low;
8207 if ((rover < low) || (rover > high))
8209 if (inet_is_local_reserved_port(net, rover))
8211 index = sctp_phashfn(sock_net(sk), rover);
8212 head = &sctp_port_hashtable[index];
8213 spin_lock_bh(&head->lock);
8214 sctp_for_each_hentry(pp, &head->chain)
8215 if ((pp->port == rover) &&
8216 net_eq(sock_net(sk), pp->net))
8220 spin_unlock_bh(&head->lock);
8222 } while (--remaining > 0);
8224 /* Exhausted local port range during search? */
8229 /* OK, here is the one we will use. HEAD (the port
8230 * hash table list entry) is non-NULL and we hold it's
8235 /* We are given an specific port number; we verify
8236 * that it is not being used. If it is used, we will
8237 * exahust the search in the hash list corresponding
8238 * to the port number (snum) - we detect that with the
8239 * port iterator, pp being NULL.
8241 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
8242 spin_lock_bh(&head->lock);
8243 sctp_for_each_hentry(pp, &head->chain) {
8244 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
8251 if (!hlist_empty(&pp->owner)) {
8252 /* We had a port hash table hit - there is an
8253 * available port (pp != NULL) and it is being
8254 * used by other socket (pp->owner not empty); that other
8255 * socket is going to be sk2.
8259 pr_debug("%s: found a possible match\n", __func__);
8261 if ((pp->fastreuse && reuse &&
8262 sk->sk_state != SCTP_SS_LISTENING) ||
8263 (pp->fastreuseport && sk->sk_reuseport &&
8264 uid_eq(pp->fastuid, uid)))
8267 /* Run through the list of sockets bound to the port
8268 * (pp->port) [via the pointers bind_next and
8269 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8270 * we get the endpoint they describe and run through
8271 * the endpoint's list of IP (v4 or v6) addresses,
8272 * comparing each of the addresses with the address of
8273 * the socket sk. If we find a match, then that means
8274 * that this port/socket (sk) combination are already
8277 sk_for_each_bound(sk2, &pp->owner) {
8278 struct sctp_sock *sp2 = sctp_sk(sk2);
8279 struct sctp_endpoint *ep2 = sp2->ep;
8282 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8283 sk2->sk_state != SCTP_SS_LISTENING) ||
8284 (sk->sk_reuseport && sk2->sk_reuseport &&
8285 uid_eq(uid, sock_i_uid(sk2))))
8288 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8295 pr_debug("%s: found a match\n", __func__);
8298 /* If there was a hash table miss, create a new port. */
8300 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
8303 /* In either case (hit or miss), make sure fastreuse is 1 only
8304 * if sk->sk_reuse is too (that is, if the caller requested
8305 * SO_REUSEADDR on this socket -sk-).
8307 if (hlist_empty(&pp->owner)) {
8308 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8313 if (sk->sk_reuseport) {
8314 pp->fastreuseport = 1;
8317 pp->fastreuseport = 0;
8320 if (pp->fastreuse &&
8321 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8324 if (pp->fastreuseport &&
8325 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8326 pp->fastreuseport = 0;
8329 /* We are set, so fill up all the data in the hash table
8330 * entry, tie the socket list information with the rest of the
8331 * sockets FIXME: Blurry, NPI (ipg).
8334 if (!sp->bind_hash) {
8335 inet_sk(sk)->inet_num = snum;
8336 sk_add_bind_node(sk, &pp->owner);
8342 spin_unlock_bh(&head->lock);
8346 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8347 * port is requested.
8349 static int sctp_get_port(struct sock *sk, unsigned short snum)
8351 union sctp_addr addr;
8352 struct sctp_af *af = sctp_sk(sk)->pf->af;
8354 /* Set up a dummy address struct from the sk. */
8355 af->from_sk(&addr, sk);
8356 addr.v4.sin_port = htons(snum);
8358 /* Note: sk->sk_num gets filled in if ephemeral port request. */
8359 return sctp_get_port_local(sk, &addr);
8363 * Move a socket to LISTENING state.
8365 static int sctp_listen_start(struct sock *sk, int backlog)
8367 struct sctp_sock *sp = sctp_sk(sk);
8368 struct sctp_endpoint *ep = sp->ep;
8369 struct crypto_shash *tfm = NULL;
8372 /* Allocate HMAC for generating cookie. */
8373 if (!sp->hmac && sp->sctp_hmac_alg) {
8374 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8375 tfm = crypto_alloc_shash(alg, 0, 0);
8377 net_info_ratelimited("failed to load transform for %s: %ld\n",
8378 sp->sctp_hmac_alg, PTR_ERR(tfm));
8381 sctp_sk(sk)->hmac = tfm;
8385 * If a bind() or sctp_bindx() is not called prior to a listen()
8386 * call that allows new associations to be accepted, the system
8387 * picks an ephemeral port and will choose an address set equivalent
8388 * to binding with a wildcard address.
8390 * This is not currently spelled out in the SCTP sockets
8391 * extensions draft, but follows the practice as seen in TCP
8395 inet_sk_set_state(sk, SCTP_SS_LISTENING);
8396 if (!ep->base.bind_addr.port) {
8397 if (sctp_autobind(sk))
8400 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8401 inet_sk_set_state(sk, SCTP_SS_CLOSED);
8406 sk->sk_max_ack_backlog = backlog;
8407 return sctp_hash_endpoint(ep);
8411 * 4.1.3 / 5.1.3 listen()
8413 * By default, new associations are not accepted for UDP style sockets.
8414 * An application uses listen() to mark a socket as being able to
8415 * accept new associations.
8417 * On TCP style sockets, applications use listen() to ready the SCTP
8418 * endpoint for accepting inbound associations.
8420 * On both types of endpoints a backlog of '0' disables listening.
8422 * Move a socket to LISTENING state.
8424 int sctp_inet_listen(struct socket *sock, int backlog)
8426 struct sock *sk = sock->sk;
8427 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8430 if (unlikely(backlog < 0))
8435 /* Peeled-off sockets are not allowed to listen(). */
8436 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8439 if (sock->state != SS_UNCONNECTED)
8442 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8445 /* If backlog is zero, disable listening. */
8447 if (sctp_sstate(sk, CLOSED))
8451 sctp_unhash_endpoint(ep);
8452 sk->sk_state = SCTP_SS_CLOSED;
8453 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8454 sctp_sk(sk)->bind_hash->fastreuse = 1;
8458 /* If we are already listening, just update the backlog */
8459 if (sctp_sstate(sk, LISTENING))
8460 sk->sk_max_ack_backlog = backlog;
8462 err = sctp_listen_start(sk, backlog);
8474 * This function is done by modeling the current datagram_poll() and the
8475 * tcp_poll(). Note that, based on these implementations, we don't
8476 * lock the socket in this function, even though it seems that,
8477 * ideally, locking or some other mechanisms can be used to ensure
8478 * the integrity of the counters (sndbuf and wmem_alloc) used
8479 * in this place. We assume that we don't need locks either until proven
8482 * Another thing to note is that we include the Async I/O support
8483 * here, again, by modeling the current TCP/UDP code. We don't have
8484 * a good way to test with it yet.
8486 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8488 struct sock *sk = sock->sk;
8489 struct sctp_sock *sp = sctp_sk(sk);
8492 poll_wait(file, sk_sleep(sk), wait);
8494 sock_rps_record_flow(sk);
8496 /* A TCP-style listening socket becomes readable when the accept queue
8499 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8500 return (!list_empty(&sp->ep->asocs)) ?
8501 (EPOLLIN | EPOLLRDNORM) : 0;
8505 /* Is there any exceptional events? */
8506 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8508 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8509 if (sk->sk_shutdown & RCV_SHUTDOWN)
8510 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8511 if (sk->sk_shutdown == SHUTDOWN_MASK)
8514 /* Is it readable? Reconsider this code with TCP-style support. */
8515 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8516 mask |= EPOLLIN | EPOLLRDNORM;
8518 /* The association is either gone or not ready. */
8519 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8522 /* Is it writable? */
8523 if (sctp_writeable(sk)) {
8524 mask |= EPOLLOUT | EPOLLWRNORM;
8526 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8528 * Since the socket is not locked, the buffer
8529 * might be made available after the writeable check and
8530 * before the bit is set. This could cause a lost I/O
8531 * signal. tcp_poll() has a race breaker for this race
8532 * condition. Based on their implementation, we put
8533 * in the following code to cover it as well.
8535 if (sctp_writeable(sk))
8536 mask |= EPOLLOUT | EPOLLWRNORM;
8541 /********************************************************************
8542 * 2nd Level Abstractions
8543 ********************************************************************/
8545 static struct sctp_bind_bucket *sctp_bucket_create(
8546 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8548 struct sctp_bind_bucket *pp;
8550 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8552 SCTP_DBG_OBJCNT_INC(bind_bucket);
8555 INIT_HLIST_HEAD(&pp->owner);
8557 hlist_add_head(&pp->node, &head->chain);
8562 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8563 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8565 if (pp && hlist_empty(&pp->owner)) {
8566 __hlist_del(&pp->node);
8567 kmem_cache_free(sctp_bucket_cachep, pp);
8568 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8572 /* Release this socket's reference to a local port. */
8573 static inline void __sctp_put_port(struct sock *sk)
8575 struct sctp_bind_hashbucket *head =
8576 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8577 inet_sk(sk)->inet_num)];
8578 struct sctp_bind_bucket *pp;
8580 spin_lock(&head->lock);
8581 pp = sctp_sk(sk)->bind_hash;
8582 __sk_del_bind_node(sk);
8583 sctp_sk(sk)->bind_hash = NULL;
8584 inet_sk(sk)->inet_num = 0;
8585 sctp_bucket_destroy(pp);
8586 spin_unlock(&head->lock);
8589 void sctp_put_port(struct sock *sk)
8592 __sctp_put_port(sk);
8597 * The system picks an ephemeral port and choose an address set equivalent
8598 * to binding with a wildcard address.
8599 * One of those addresses will be the primary address for the association.
8600 * This automatically enables the multihoming capability of SCTP.
8602 static int sctp_autobind(struct sock *sk)
8604 union sctp_addr autoaddr;
8608 /* Initialize a local sockaddr structure to INADDR_ANY. */
8609 af = sctp_sk(sk)->pf->af;
8611 port = htons(inet_sk(sk)->inet_num);
8612 af->inaddr_any(&autoaddr, port);
8614 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8617 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8620 * 4.2 The cmsghdr Structure *
8622 * When ancillary data is sent or received, any number of ancillary data
8623 * objects can be specified by the msg_control and msg_controllen members of
8624 * the msghdr structure, because each object is preceded by
8625 * a cmsghdr structure defining the object's length (the cmsg_len member).
8626 * Historically Berkeley-derived implementations have passed only one object
8627 * at a time, but this API allows multiple objects to be
8628 * passed in a single call to sendmsg() or recvmsg(). The following example
8629 * shows two ancillary data objects in a control buffer.
8631 * |<--------------------------- msg_controllen -------------------------->|
8634 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8636 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8639 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8641 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8644 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8645 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8647 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8649 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8656 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8658 struct msghdr *my_msg = (struct msghdr *)msg;
8659 struct cmsghdr *cmsg;
8661 for_each_cmsghdr(cmsg, my_msg) {
8662 if (!CMSG_OK(my_msg, cmsg))
8665 /* Should we parse this header or ignore? */
8666 if (cmsg->cmsg_level != IPPROTO_SCTP)
8669 /* Strictly check lengths following example in SCM code. */
8670 switch (cmsg->cmsg_type) {
8672 /* SCTP Socket API Extension
8673 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8675 * This cmsghdr structure provides information for
8676 * initializing new SCTP associations with sendmsg().
8677 * The SCTP_INITMSG socket option uses this same data
8678 * structure. This structure is not used for
8681 * cmsg_level cmsg_type cmsg_data[]
8682 * ------------ ------------ ----------------------
8683 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8685 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8688 cmsgs->init = CMSG_DATA(cmsg);
8692 /* SCTP Socket API Extension
8693 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8695 * This cmsghdr structure specifies SCTP options for
8696 * sendmsg() and describes SCTP header information
8697 * about a received message through recvmsg().
8699 * cmsg_level cmsg_type cmsg_data[]
8700 * ------------ ------------ ----------------------
8701 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8703 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8706 cmsgs->srinfo = CMSG_DATA(cmsg);
8708 if (cmsgs->srinfo->sinfo_flags &
8709 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8710 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8711 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8716 /* SCTP Socket API Extension
8717 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8719 * This cmsghdr structure specifies SCTP options for
8720 * sendmsg(). This structure and SCTP_RCVINFO replaces
8721 * SCTP_SNDRCV which has been deprecated.
8723 * cmsg_level cmsg_type cmsg_data[]
8724 * ------------ ------------ ---------------------
8725 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8727 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8730 cmsgs->sinfo = CMSG_DATA(cmsg);
8732 if (cmsgs->sinfo->snd_flags &
8733 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8734 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8735 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8739 /* SCTP Socket API Extension
8740 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8742 * This cmsghdr structure specifies SCTP options for sendmsg().
8744 * cmsg_level cmsg_type cmsg_data[]
8745 * ------------ ------------ ---------------------
8746 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8748 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8751 cmsgs->prinfo = CMSG_DATA(cmsg);
8752 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8755 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8756 cmsgs->prinfo->pr_value = 0;
8759 /* SCTP Socket API Extension
8760 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8762 * This cmsghdr structure specifies SCTP options for sendmsg().
8764 * cmsg_level cmsg_type cmsg_data[]
8765 * ------------ ------------ ---------------------
8766 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8768 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8771 cmsgs->authinfo = CMSG_DATA(cmsg);
8773 case SCTP_DSTADDRV4:
8774 case SCTP_DSTADDRV6:
8775 /* SCTP Socket API Extension
8776 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8778 * This cmsghdr structure specifies SCTP options for sendmsg().
8780 * cmsg_level cmsg_type cmsg_data[]
8781 * ------------ ------------ ---------------------
8782 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8783 * ------------ ------------ ---------------------
8784 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8786 cmsgs->addrs_msg = my_msg;
8797 * Wait for a packet..
8798 * Note: This function is the same function as in core/datagram.c
8799 * with a few modifications to make lksctp work.
8801 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8806 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8808 /* Socket errors? */
8809 error = sock_error(sk);
8813 if (!skb_queue_empty(&sk->sk_receive_queue))
8816 /* Socket shut down? */
8817 if (sk->sk_shutdown & RCV_SHUTDOWN)
8820 /* Sequenced packets can come disconnected. If so we report the
8825 /* Is there a good reason to think that we may receive some data? */
8826 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8829 /* Handle signals. */
8830 if (signal_pending(current))
8833 /* Let another process have a go. Since we are going to sleep
8834 * anyway. Note: This may cause odd behaviors if the message
8835 * does not fit in the user's buffer, but this seems to be the
8836 * only way to honor MSG_DONTWAIT realistically.
8839 *timeo_p = schedule_timeout(*timeo_p);
8843 finish_wait(sk_sleep(sk), &wait);
8847 error = sock_intr_errno(*timeo_p);
8850 finish_wait(sk_sleep(sk), &wait);
8855 /* Receive a datagram.
8856 * Note: This is pretty much the same routine as in core/datagram.c
8857 * with a few changes to make lksctp work.
8859 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8860 int noblock, int *err)
8863 struct sk_buff *skb;
8866 timeo = sock_rcvtimeo(sk, noblock);
8868 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8869 MAX_SCHEDULE_TIMEOUT);
8872 /* Again only user level code calls this function,
8873 * so nothing interrupt level
8874 * will suddenly eat the receive_queue.
8876 * Look at current nfs client by the way...
8877 * However, this function was correct in any case. 8)
8879 if (flags & MSG_PEEK) {
8880 skb = skb_peek(&sk->sk_receive_queue);
8882 refcount_inc(&skb->users);
8884 skb = __skb_dequeue(&sk->sk_receive_queue);
8890 /* Caller is allowed not to check sk->sk_err before calling. */
8891 error = sock_error(sk);
8895 if (sk->sk_shutdown & RCV_SHUTDOWN)
8898 if (sk_can_busy_loop(sk)) {
8899 sk_busy_loop(sk, noblock);
8901 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8905 /* User doesn't want to wait. */
8909 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8918 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8919 static void __sctp_write_space(struct sctp_association *asoc)
8921 struct sock *sk = asoc->base.sk;
8923 if (sctp_wspace(asoc) <= 0)
8926 if (waitqueue_active(&asoc->wait))
8927 wake_up_interruptible(&asoc->wait);
8929 if (sctp_writeable(sk)) {
8930 struct socket_wq *wq;
8933 wq = rcu_dereference(sk->sk_wq);
8935 if (waitqueue_active(&wq->wait))
8936 wake_up_interruptible(&wq->wait);
8938 /* Note that we try to include the Async I/O support
8939 * here by modeling from the current TCP/UDP code.
8940 * We have not tested with it yet.
8942 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8943 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8949 static void sctp_wake_up_waiters(struct sock *sk,
8950 struct sctp_association *asoc)
8952 struct sctp_association *tmp = asoc;
8954 /* We do accounting for the sndbuf space per association,
8955 * so we only need to wake our own association.
8957 if (asoc->ep->sndbuf_policy)
8958 return __sctp_write_space(asoc);
8960 /* If association goes down and is just flushing its
8961 * outq, then just normally notify others.
8963 if (asoc->base.dead)
8964 return sctp_write_space(sk);
8966 /* Accounting for the sndbuf space is per socket, so we
8967 * need to wake up others, try to be fair and in case of
8968 * other associations, let them have a go first instead
8969 * of just doing a sctp_write_space() call.
8971 * Note that we reach sctp_wake_up_waiters() only when
8972 * associations free up queued chunks, thus we are under
8973 * lock and the list of associations on a socket is
8974 * guaranteed not to change.
8976 for (tmp = list_next_entry(tmp, asocs); 1;
8977 tmp = list_next_entry(tmp, asocs)) {
8978 /* Manually skip the head element. */
8979 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8981 /* Wake up association. */
8982 __sctp_write_space(tmp);
8983 /* We've reached the end. */
8989 /* Do accounting for the sndbuf space.
8990 * Decrement the used sndbuf space of the corresponding association by the
8991 * data size which was just transmitted(freed).
8993 static void sctp_wfree(struct sk_buff *skb)
8995 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8996 struct sctp_association *asoc = chunk->asoc;
8997 struct sock *sk = asoc->base.sk;
8999 sk_mem_uncharge(sk, skb->truesize);
9000 sk_wmem_queued_add(sk, -(skb->truesize + sizeof(struct sctp_chunk)));
9001 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
9002 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
9003 &sk->sk_wmem_alloc));
9006 struct sctp_shared_key *shkey = chunk->shkey;
9008 /* refcnt == 2 and !list_empty mean after this release, it's
9009 * not being used anywhere, and it's time to notify userland
9010 * that this shkey can be freed if it's been deactivated.
9012 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
9013 refcount_read(&shkey->refcnt) == 2) {
9014 struct sctp_ulpevent *ev;
9016 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
9020 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
9022 sctp_auth_shkey_release(chunk->shkey);
9026 sctp_wake_up_waiters(sk, asoc);
9028 sctp_association_put(asoc);
9031 /* Do accounting for the receive space on the socket.
9032 * Accounting for the association is done in ulpevent.c
9033 * We set this as a destructor for the cloned data skbs so that
9034 * accounting is done at the correct time.
9036 void sctp_sock_rfree(struct sk_buff *skb)
9038 struct sock *sk = skb->sk;
9039 struct sctp_ulpevent *event = sctp_skb2event(skb);
9041 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
9044 * Mimic the behavior of sock_rfree
9046 sk_mem_uncharge(sk, event->rmem_len);
9050 /* Helper function to wait for space in the sndbuf. */
9051 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
9054 struct sock *sk = asoc->base.sk;
9055 long current_timeo = *timeo_p;
9059 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
9062 /* Increment the association's refcnt. */
9063 sctp_association_hold(asoc);
9065 /* Wait on the association specific sndbuf space. */
9067 prepare_to_wait_exclusive(&asoc->wait, &wait,
9068 TASK_INTERRUPTIBLE);
9069 if (asoc->base.dead)
9073 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
9075 if (signal_pending(current))
9076 goto do_interrupted;
9077 if (sk_under_memory_pressure(sk))
9079 if ((int)msg_len <= sctp_wspace(asoc) &&
9080 sk_wmem_schedule(sk, msg_len))
9083 /* Let another process have a go. Since we are going
9087 current_timeo = schedule_timeout(current_timeo);
9089 if (sk != asoc->base.sk)
9092 *timeo_p = current_timeo;
9096 finish_wait(&asoc->wait, &wait);
9098 /* Release the association's refcnt. */
9099 sctp_association_put(asoc);
9112 err = sock_intr_errno(*timeo_p);
9120 void sctp_data_ready(struct sock *sk)
9122 struct socket_wq *wq;
9125 wq = rcu_dereference(sk->sk_wq);
9126 if (skwq_has_sleeper(wq))
9127 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
9128 EPOLLRDNORM | EPOLLRDBAND);
9129 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9133 /* If socket sndbuf has changed, wake up all per association waiters. */
9134 void sctp_write_space(struct sock *sk)
9136 struct sctp_association *asoc;
9138 /* Wake up the tasks in each wait queue. */
9139 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
9140 __sctp_write_space(asoc);
9144 /* Is there any sndbuf space available on the socket?
9146 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
9147 * associations on the same socket. For a UDP-style socket with
9148 * multiple associations, it is possible for it to be "unwriteable"
9149 * prematurely. I assume that this is acceptable because
9150 * a premature "unwriteable" is better than an accidental "writeable" which
9151 * would cause an unwanted block under certain circumstances. For the 1-1
9152 * UDP-style sockets or TCP-style sockets, this code should work.
9155 static bool sctp_writeable(const struct sock *sk)
9157 return READ_ONCE(sk->sk_sndbuf) > READ_ONCE(sk->sk_wmem_queued);
9160 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
9161 * returns immediately with EINPROGRESS.
9163 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9165 struct sock *sk = asoc->base.sk;
9167 long current_timeo = *timeo_p;
9170 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9172 /* Increment the association's refcnt. */
9173 sctp_association_hold(asoc);
9176 prepare_to_wait_exclusive(&asoc->wait, &wait,
9177 TASK_INTERRUPTIBLE);
9180 if (sk->sk_shutdown & RCV_SHUTDOWN)
9182 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9185 if (signal_pending(current))
9186 goto do_interrupted;
9188 if (sctp_state(asoc, ESTABLISHED))
9191 /* Let another process have a go. Since we are going
9195 current_timeo = schedule_timeout(current_timeo);
9198 *timeo_p = current_timeo;
9202 finish_wait(&asoc->wait, &wait);
9204 /* Release the association's refcnt. */
9205 sctp_association_put(asoc);
9210 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9213 err = -ECONNREFUSED;
9217 err = sock_intr_errno(*timeo_p);
9225 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9227 struct sctp_endpoint *ep;
9231 ep = sctp_sk(sk)->ep;
9235 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9236 TASK_INTERRUPTIBLE);
9238 if (list_empty(&ep->asocs)) {
9240 timeo = schedule_timeout(timeo);
9245 if (!sctp_sstate(sk, LISTENING))
9249 if (!list_empty(&ep->asocs))
9252 err = sock_intr_errno(timeo);
9253 if (signal_pending(current))
9261 finish_wait(sk_sleep(sk), &wait);
9266 static void sctp_wait_for_close(struct sock *sk, long timeout)
9271 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9272 if (list_empty(&sctp_sk(sk)->ep->asocs))
9275 timeout = schedule_timeout(timeout);
9277 } while (!signal_pending(current) && timeout);
9279 finish_wait(sk_sleep(sk), &wait);
9282 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9284 struct sk_buff *frag;
9289 /* Don't forget the fragments. */
9290 skb_walk_frags(skb, frag)
9291 sctp_skb_set_owner_r_frag(frag, sk);
9294 sctp_skb_set_owner_r(skb, sk);
9297 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9298 struct sctp_association *asoc)
9300 struct inet_sock *inet = inet_sk(sk);
9301 struct inet_sock *newinet;
9302 struct sctp_sock *sp = sctp_sk(sk);
9303 struct sctp_endpoint *ep = sp->ep;
9305 newsk->sk_type = sk->sk_type;
9306 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9307 newsk->sk_flags = sk->sk_flags;
9308 newsk->sk_tsflags = sk->sk_tsflags;
9309 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9310 newsk->sk_no_check_rx = sk->sk_no_check_rx;
9311 newsk->sk_reuse = sk->sk_reuse;
9312 sctp_sk(newsk)->reuse = sp->reuse;
9314 newsk->sk_shutdown = sk->sk_shutdown;
9315 newsk->sk_destruct = sk->sk_destruct;
9316 newsk->sk_family = sk->sk_family;
9317 newsk->sk_protocol = IPPROTO_SCTP;
9318 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9319 newsk->sk_sndbuf = sk->sk_sndbuf;
9320 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9321 newsk->sk_lingertime = sk->sk_lingertime;
9322 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9323 newsk->sk_sndtimeo = sk->sk_sndtimeo;
9324 newsk->sk_rxhash = sk->sk_rxhash;
9326 newinet = inet_sk(newsk);
9328 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9329 * getsockname() and getpeername()
9331 newinet->inet_sport = inet->inet_sport;
9332 newinet->inet_saddr = inet->inet_saddr;
9333 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9334 newinet->inet_dport = htons(asoc->peer.port);
9335 newinet->pmtudisc = inet->pmtudisc;
9336 newinet->inet_id = prandom_u32();
9338 newinet->uc_ttl = inet->uc_ttl;
9339 newinet->mc_loop = 1;
9340 newinet->mc_ttl = 1;
9341 newinet->mc_index = 0;
9342 newinet->mc_list = NULL;
9344 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9345 net_enable_timestamp();
9347 /* Set newsk security attributes from orginal sk and connection
9348 * security attribute from ep.
9350 security_sctp_sk_clone(ep, sk, newsk);
9353 static inline void sctp_copy_descendant(struct sock *sk_to,
9354 const struct sock *sk_from)
9356 size_t ancestor_size = sizeof(struct inet_sock);
9358 ancestor_size += sk_from->sk_prot->obj_size;
9359 ancestor_size -= offsetof(struct sctp_sock, pd_lobby);
9360 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9363 /* Populate the fields of the newsk from the oldsk and migrate the assoc
9364 * and its messages to the newsk.
9366 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9367 struct sctp_association *assoc,
9368 enum sctp_socket_type type)
9370 struct sctp_sock *oldsp = sctp_sk(oldsk);
9371 struct sctp_sock *newsp = sctp_sk(newsk);
9372 struct sctp_bind_bucket *pp; /* hash list port iterator */
9373 struct sctp_endpoint *newep = newsp->ep;
9374 struct sk_buff *skb, *tmp;
9375 struct sctp_ulpevent *event;
9376 struct sctp_bind_hashbucket *head;
9379 /* Migrate socket buffer sizes and all the socket level options to the
9382 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9383 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9384 /* Brute force copy old sctp opt. */
9385 sctp_copy_descendant(newsk, oldsk);
9387 /* Restore the ep value that was overwritten with the above structure
9393 /* Hook this new socket in to the bind_hash list. */
9394 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9395 inet_sk(oldsk)->inet_num)];
9396 spin_lock_bh(&head->lock);
9397 pp = sctp_sk(oldsk)->bind_hash;
9398 sk_add_bind_node(newsk, &pp->owner);
9399 sctp_sk(newsk)->bind_hash = pp;
9400 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9401 spin_unlock_bh(&head->lock);
9403 /* Copy the bind_addr list from the original endpoint to the new
9404 * endpoint so that we can handle restarts properly
9406 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9407 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9411 /* New ep's auth_hmacs should be set if old ep's is set, in case
9412 * that net->sctp.auth_enable has been changed to 0 by users and
9413 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9415 if (oldsp->ep->auth_hmacs) {
9416 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9421 sctp_auto_asconf_init(newsp);
9423 /* Move any messages in the old socket's receive queue that are for the
9424 * peeled off association to the new socket's receive queue.
9426 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9427 event = sctp_skb2event(skb);
9428 if (event->asoc == assoc) {
9429 __skb_unlink(skb, &oldsk->sk_receive_queue);
9430 __skb_queue_tail(&newsk->sk_receive_queue, skb);
9431 sctp_skb_set_owner_r_frag(skb, newsk);
9435 /* Clean up any messages pending delivery due to partial
9436 * delivery. Three cases:
9437 * 1) No partial deliver; no work.
9438 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9439 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9441 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9443 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9444 struct sk_buff_head *queue;
9446 /* Decide which queue to move pd_lobby skbs to. */
9447 if (assoc->ulpq.pd_mode) {
9448 queue = &newsp->pd_lobby;
9450 queue = &newsk->sk_receive_queue;
9452 /* Walk through the pd_lobby, looking for skbs that
9453 * need moved to the new socket.
9455 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9456 event = sctp_skb2event(skb);
9457 if (event->asoc == assoc) {
9458 __skb_unlink(skb, &oldsp->pd_lobby);
9459 __skb_queue_tail(queue, skb);
9460 sctp_skb_set_owner_r_frag(skb, newsk);
9464 /* Clear up any skbs waiting for the partial
9465 * delivery to finish.
9467 if (assoc->ulpq.pd_mode)
9468 sctp_clear_pd(oldsk, NULL);
9472 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9474 /* Set the type of socket to indicate that it is peeled off from the
9475 * original UDP-style socket or created with the accept() call on a
9476 * TCP-style socket..
9480 /* Mark the new socket "in-use" by the user so that any packets
9481 * that may arrive on the association after we've moved it are
9482 * queued to the backlog. This prevents a potential race between
9483 * backlog processing on the old socket and new-packet processing
9484 * on the new socket.
9486 * The caller has just allocated newsk so we can guarantee that other
9487 * paths won't try to lock it and then oldsk.
9489 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9490 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
9491 sctp_assoc_migrate(assoc, newsk);
9492 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
9494 /* If the association on the newsk is already closed before accept()
9495 * is called, set RCV_SHUTDOWN flag.
9497 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9498 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9499 newsk->sk_shutdown |= RCV_SHUTDOWN;
9501 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9504 release_sock(newsk);
9510 /* This proto struct describes the ULP interface for SCTP. */
9511 struct proto sctp_prot = {
9513 .owner = THIS_MODULE,
9514 .close = sctp_close,
9515 .disconnect = sctp_disconnect,
9516 .accept = sctp_accept,
9517 .ioctl = sctp_ioctl,
9518 .init = sctp_init_sock,
9519 .destroy = sctp_destroy_sock,
9520 .shutdown = sctp_shutdown,
9521 .setsockopt = sctp_setsockopt,
9522 .getsockopt = sctp_getsockopt,
9523 .sendmsg = sctp_sendmsg,
9524 .recvmsg = sctp_recvmsg,
9526 .backlog_rcv = sctp_backlog_rcv,
9528 .unhash = sctp_unhash,
9529 .no_autobind = true,
9530 .obj_size = sizeof(struct sctp_sock),
9531 .useroffset = offsetof(struct sctp_sock, subscribe),
9532 .usersize = offsetof(struct sctp_sock, initmsg) -
9533 offsetof(struct sctp_sock, subscribe) +
9534 sizeof_field(struct sctp_sock, initmsg),
9535 .sysctl_mem = sysctl_sctp_mem,
9536 .sysctl_rmem = sysctl_sctp_rmem,
9537 .sysctl_wmem = sysctl_sctp_wmem,
9538 .memory_pressure = &sctp_memory_pressure,
9539 .enter_memory_pressure = sctp_enter_memory_pressure,
9540 .memory_allocated = &sctp_memory_allocated,
9541 .sockets_allocated = &sctp_sockets_allocated,
9544 #if IS_ENABLED(CONFIG_IPV6)
9546 static void sctp_v6_destruct_sock(struct sock *sk)
9548 sctp_destruct_common(sk);
9549 inet6_sock_destruct(sk);
9552 static int sctp_v6_init_sock(struct sock *sk)
9554 int ret = sctp_init_sock(sk);
9557 sk->sk_destruct = sctp_v6_destruct_sock;
9562 struct proto sctpv6_prot = {
9564 .owner = THIS_MODULE,
9565 .close = sctp_close,
9566 .disconnect = sctp_disconnect,
9567 .accept = sctp_accept,
9568 .ioctl = sctp_ioctl,
9569 .init = sctp_v6_init_sock,
9570 .destroy = sctp_destroy_sock,
9571 .shutdown = sctp_shutdown,
9572 .setsockopt = sctp_setsockopt,
9573 .getsockopt = sctp_getsockopt,
9574 .sendmsg = sctp_sendmsg,
9575 .recvmsg = sctp_recvmsg,
9577 .backlog_rcv = sctp_backlog_rcv,
9579 .unhash = sctp_unhash,
9580 .no_autobind = true,
9581 .obj_size = sizeof(struct sctp6_sock),
9582 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9583 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9584 offsetof(struct sctp6_sock, sctp.subscribe) +
9585 sizeof_field(struct sctp6_sock, sctp.initmsg),
9586 .sysctl_mem = sysctl_sctp_mem,
9587 .sysctl_rmem = sysctl_sctp_rmem,
9588 .sysctl_wmem = sysctl_sctp_wmem,
9589 .memory_pressure = &sctp_memory_pressure,
9590 .enter_memory_pressure = sctp_enter_memory_pressure,
9591 .memory_allocated = &sctp_memory_allocated,
9592 .sockets_allocated = &sctp_sockets_allocated,
9594 #endif /* IS_ENABLED(CONFIG_IPV6) */