1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
7 * Copyright (c) 2001-2002 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
10 * This file is part of the SCTP kernel implementation
12 * These functions interface with the sockets layer to implement the
13 * SCTP Extensions for the Sockets API.
15 * Note that the descriptions from the specification are USER level
16 * functions--this file is the functions which populate the struct proto
17 * for SCTP which is the BOTTOM of the sockets interface.
19 * Please send any bug reports or fixes you make to the
21 * lksctp developers <linux-sctp@vger.kernel.org>
23 * Written or modified by:
24 * La Monte H.P. Yarroll <piggy@acm.org>
25 * Narasimha Budihal <narsi@refcode.org>
26 * Karl Knutson <karl@athena.chicago.il.us>
27 * Jon Grimm <jgrimm@us.ibm.com>
28 * Xingang Guo <xingang.guo@intel.com>
29 * Daisy Chang <daisyc@us.ibm.com>
30 * Sridhar Samudrala <samudrala@us.ibm.com>
31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32 * Ardelle Fan <ardelle.fan@intel.com>
33 * Ryan Layer <rmlayer@us.ibm.com>
34 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35 * Kevin Gao <kevin.gao@intel.com>
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <crypto/hash.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/time.h>
45 #include <linux/sched/signal.h>
47 #include <linux/capability.h>
48 #include <linux/fcntl.h>
49 #include <linux/poll.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/file.h>
53 #include <linux/compat.h>
54 #include <linux/rhashtable.h>
58 #include <net/route.h>
60 #include <net/inet_common.h>
61 #include <net/busy_poll.h>
63 #include <linux/socket.h> /* for sa_family_t */
64 #include <linux/export.h>
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
68 #include <net/sctp/stream_sched.h>
70 /* Forward declarations for internal helper functions. */
71 static bool sctp_writeable(struct sock *sk);
72 static void sctp_wfree(struct sk_buff *skb);
73 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
75 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
76 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77 static int sctp_wait_for_accept(struct sock *sk, long timeo);
78 static void sctp_wait_for_close(struct sock *sk, long timeo);
79 static void sctp_destruct_sock(struct sock *sk);
80 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81 union sctp_addr *addr, int len);
82 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86 static int sctp_send_asconf(struct sctp_association *asoc,
87 struct sctp_chunk *chunk);
88 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89 static int sctp_autobind(struct sock *sk);
90 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91 struct sctp_association *assoc,
92 enum sctp_socket_type type);
94 static unsigned long sctp_memory_pressure;
95 static atomic_long_t sctp_memory_allocated;
96 struct percpu_counter sctp_sockets_allocated;
98 static void sctp_enter_memory_pressure(struct sock *sk)
100 sctp_memory_pressure = 1;
104 /* Get the sndbuf space available at the time on the association. */
105 static inline int sctp_wspace(struct sctp_association *asoc)
107 struct sock *sk = asoc->base.sk;
109 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110 : sk_stream_wspace(sk);
113 /* Increment the used sndbuf space count of the corresponding association by
114 * the size of the outgoing data chunk.
115 * Also, set the skb destructor for sndbuf accounting later.
117 * Since it is always 1-1 between chunk and skb, and also a new skb is always
118 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119 * destructor in the data chunk skb for the purpose of the sndbuf space
122 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
124 struct sctp_association *asoc = chunk->asoc;
125 struct sock *sk = asoc->base.sk;
127 /* The sndbuf space is tracked per association. */
128 sctp_association_hold(asoc);
131 sctp_auth_shkey_hold(chunk->shkey);
133 skb_set_owner_w(chunk->skb, sk);
135 chunk->skb->destructor = sctp_wfree;
136 /* Save the chunk pointer in skb for sctp_wfree to use later. */
137 skb_shinfo(chunk->skb)->destructor_arg = chunk;
139 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
140 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
142 sk_mem_charge(sk, chunk->skb->truesize);
145 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
147 skb_orphan(chunk->skb);
150 #define traverse_and_process() \
153 if (msg == prev_msg) \
155 list_for_each_entry(c, &msg->chunks, frag_list) { \
156 if ((clear && asoc->base.sk == c->skb->sk) || \
157 (!clear && asoc->base.sk != c->skb->sk)) \
163 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
165 void (*cb)(struct sctp_chunk *))
168 struct sctp_datamsg *msg, *prev_msg = NULL;
169 struct sctp_outq *q = &asoc->outqueue;
170 struct sctp_chunk *chunk, *c;
171 struct sctp_transport *t;
173 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
175 traverse_and_process();
177 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
178 traverse_and_process();
180 list_for_each_entry(chunk, &q->sacked, transmitted_list)
181 traverse_and_process();
183 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
184 traverse_and_process();
186 list_for_each_entry(chunk, &q->out_chunk_list, list)
187 traverse_and_process();
190 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191 void (*cb)(struct sk_buff *, struct sock *))
194 struct sk_buff *skb, *tmp;
196 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
199 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
206 /* Verify that this is a valid address. */
207 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
212 /* Verify basic sockaddr. */
213 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
217 /* Is this a valid SCTP address? */
218 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
221 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
227 /* Look up the association by its id. If this is not a UDP-style
228 * socket, the ID field is always ignored.
230 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
232 struct sctp_association *asoc = NULL;
234 /* If this is not a UDP-style socket, assoc id should be ignored. */
235 if (!sctp_style(sk, UDP)) {
236 /* Return NULL if the socket state is not ESTABLISHED. It
237 * could be a TCP-style listening socket or a socket which
238 * hasn't yet called connect() to establish an association.
240 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
243 /* Get the first and the only association from the list. */
244 if (!list_empty(&sctp_sk(sk)->ep->asocs))
245 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246 struct sctp_association, asocs);
250 /* Otherwise this is a UDP-style socket. */
251 if (id <= SCTP_ALL_ASSOC)
254 spin_lock_bh(&sctp_assocs_id_lock);
255 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
256 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
258 spin_unlock_bh(&sctp_assocs_id_lock);
263 /* Look up the transport from an address and an assoc id. If both address and
264 * id are specified, the associations matching the address and the id should be
267 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
268 struct sockaddr_storage *addr,
271 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
272 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
273 union sctp_addr *laddr = (union sctp_addr *)addr;
274 struct sctp_transport *transport;
276 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
279 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
286 id_asoc = sctp_id2assoc(sk, id);
287 if (id_asoc && (id_asoc != addr_asoc))
290 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
291 (union sctp_addr *)addr);
296 /* API 3.1.2 bind() - UDP Style Syntax
297 * The syntax of bind() is,
299 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
301 * sd - the socket descriptor returned by socket().
302 * addr - the address structure (struct sockaddr_in or struct
303 * sockaddr_in6 [RFC 2553]),
304 * addr_len - the size of the address structure.
306 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
312 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
315 /* Disallow binding twice. */
316 if (!sctp_sk(sk)->ep->base.bind_addr.port)
317 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
327 static int sctp_get_port_local(struct sock *, union sctp_addr *);
329 /* Verify this is a valid sockaddr. */
330 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
331 union sctp_addr *addr, int len)
335 /* Check minimum size. */
336 if (len < sizeof (struct sockaddr))
339 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
342 if (addr->sa.sa_family == AF_INET6) {
343 if (len < SIN6_LEN_RFC2133)
345 /* V4 mapped address are really of AF_INET family */
346 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
347 !opt->pf->af_supported(AF_INET, opt))
351 /* If we get this far, af is valid. */
352 af = sctp_get_af_specific(addr->sa.sa_family);
354 if (len < af->sockaddr_len)
360 /* Bind a local address either to an endpoint or to an association. */
361 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
363 struct net *net = sock_net(sk);
364 struct sctp_sock *sp = sctp_sk(sk);
365 struct sctp_endpoint *ep = sp->ep;
366 struct sctp_bind_addr *bp = &ep->base.bind_addr;
371 /* Common sockaddr verification. */
372 af = sctp_sockaddr_af(sp, addr, len);
374 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
375 __func__, sk, addr, len);
379 snum = ntohs(addr->v4.sin_port);
381 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
382 __func__, sk, &addr->sa, bp->port, snum, len);
384 /* PF specific bind() address verification. */
385 if (!sp->pf->bind_verify(sp, addr))
386 return -EADDRNOTAVAIL;
388 /* We must either be unbound, or bind to the same port.
389 * It's OK to allow 0 ports if we are already bound.
390 * We'll just inhert an already bound port in this case
395 else if (snum != bp->port) {
396 pr_debug("%s: new port %d doesn't match existing port "
397 "%d\n", __func__, snum, bp->port);
402 if (snum && inet_port_requires_bind_service(net, snum) &&
403 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
406 /* See if the address matches any of the addresses we may have
407 * already bound before checking against other endpoints.
409 if (sctp_bind_addr_match(bp, addr, sp))
412 /* Make sure we are allowed to bind here.
413 * The function sctp_get_port_local() does duplicate address
416 addr->v4.sin_port = htons(snum);
417 if (sctp_get_port_local(sk, addr))
420 /* Refresh ephemeral port. */
422 bp->port = inet_sk(sk)->inet_num;
424 /* Add the address to the bind address list.
425 * Use GFP_ATOMIC since BHs will be disabled.
427 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
428 SCTP_ADDR_SRC, GFP_ATOMIC);
434 /* Copy back into socket for getsockname() use. */
435 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
436 sp->pf->to_sk_saddr(addr, sk);
441 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
443 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
444 * at any one time. If a sender, after sending an ASCONF chunk, decides
445 * it needs to transfer another ASCONF Chunk, it MUST wait until the
446 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
447 * subsequent ASCONF. Note this restriction binds each side, so at any
448 * time two ASCONF may be in-transit on any given association (one sent
449 * from each endpoint).
451 static int sctp_send_asconf(struct sctp_association *asoc,
452 struct sctp_chunk *chunk)
456 /* If there is an outstanding ASCONF chunk, queue it for later
459 if (asoc->addip_last_asconf) {
460 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
464 /* Hold the chunk until an ASCONF_ACK is received. */
465 sctp_chunk_hold(chunk);
466 retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
468 sctp_chunk_free(chunk);
470 asoc->addip_last_asconf = chunk;
476 /* Add a list of addresses as bind addresses to local endpoint or
479 * Basically run through each address specified in the addrs/addrcnt
480 * array/length pair, determine if it is IPv6 or IPv4 and call
481 * sctp_do_bind() on it.
483 * If any of them fails, then the operation will be reversed and the
484 * ones that were added will be removed.
486 * Only sctp_setsockopt_bindx() is supposed to call this function.
488 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
493 struct sockaddr *sa_addr;
496 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
500 for (cnt = 0; cnt < addrcnt; cnt++) {
501 /* The list may contain either IPv4 or IPv6 address;
502 * determine the address length for walking thru the list.
505 af = sctp_get_af_specific(sa_addr->sa_family);
511 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
514 addr_buf += af->sockaddr_len;
518 /* Failed. Cleanup the ones that have been added */
520 sctp_bindx_rem(sk, addrs, cnt);
528 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
529 * associations that are part of the endpoint indicating that a list of local
530 * addresses are added to the endpoint.
532 * If any of the addresses is already in the bind address list of the
533 * association, we do not send the chunk for that association. But it will not
534 * affect other associations.
536 * Only sctp_setsockopt_bindx() is supposed to call this function.
538 static int sctp_send_asconf_add_ip(struct sock *sk,
539 struct sockaddr *addrs,
542 struct sctp_sock *sp;
543 struct sctp_endpoint *ep;
544 struct sctp_association *asoc;
545 struct sctp_bind_addr *bp;
546 struct sctp_chunk *chunk;
547 struct sctp_sockaddr_entry *laddr;
548 union sctp_addr *addr;
549 union sctp_addr saveaddr;
559 if (!ep->asconf_enable)
562 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
563 __func__, sk, addrs, addrcnt);
565 list_for_each_entry(asoc, &ep->asocs, asocs) {
566 if (!asoc->peer.asconf_capable)
569 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
572 if (!sctp_state(asoc, ESTABLISHED))
575 /* Check if any address in the packed array of addresses is
576 * in the bind address list of the association. If so,
577 * do not send the asconf chunk to its peer, but continue with
578 * other associations.
581 for (i = 0; i < addrcnt; i++) {
583 af = sctp_get_af_specific(addr->v4.sin_family);
589 if (sctp_assoc_lookup_laddr(asoc, addr))
592 addr_buf += af->sockaddr_len;
597 /* Use the first valid address in bind addr list of
598 * association as Address Parameter of ASCONF CHUNK.
600 bp = &asoc->base.bind_addr;
601 p = bp->address_list.next;
602 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
603 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
604 addrcnt, SCTP_PARAM_ADD_IP);
610 /* Add the new addresses to the bind address list with
611 * use_as_src set to 0.
614 for (i = 0; i < addrcnt; i++) {
616 af = sctp_get_af_specific(addr->v4.sin_family);
617 memcpy(&saveaddr, addr, af->sockaddr_len);
618 retval = sctp_add_bind_addr(bp, &saveaddr,
620 SCTP_ADDR_NEW, GFP_ATOMIC);
621 addr_buf += af->sockaddr_len;
623 if (asoc->src_out_of_asoc_ok) {
624 struct sctp_transport *trans;
626 list_for_each_entry(trans,
627 &asoc->peer.transport_addr_list, transports) {
628 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
629 2*asoc->pathmtu, 4380));
630 trans->ssthresh = asoc->peer.i.a_rwnd;
631 trans->rto = asoc->rto_initial;
632 sctp_max_rto(asoc, trans);
633 trans->rtt = trans->srtt = trans->rttvar = 0;
634 /* Clear the source and route cache */
635 sctp_transport_route(trans, NULL,
636 sctp_sk(asoc->base.sk));
639 retval = sctp_send_asconf(asoc, chunk);
646 /* Remove a list of addresses from bind addresses list. Do not remove the
649 * Basically run through each address specified in the addrs/addrcnt
650 * array/length pair, determine if it is IPv6 or IPv4 and call
651 * sctp_del_bind() on it.
653 * If any of them fails, then the operation will be reversed and the
654 * ones that were removed will be added back.
656 * At least one address has to be left; if only one address is
657 * available, the operation will return -EBUSY.
659 * Only sctp_setsockopt_bindx() is supposed to call this function.
661 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
663 struct sctp_sock *sp = sctp_sk(sk);
664 struct sctp_endpoint *ep = sp->ep;
666 struct sctp_bind_addr *bp = &ep->base.bind_addr;
669 union sctp_addr *sa_addr;
672 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
673 __func__, sk, addrs, addrcnt);
676 for (cnt = 0; cnt < addrcnt; cnt++) {
677 /* If the bind address list is empty or if there is only one
678 * bind address, there is nothing more to be removed (we need
679 * at least one address here).
681 if (list_empty(&bp->address_list) ||
682 (sctp_list_single_entry(&bp->address_list))) {
688 af = sctp_get_af_specific(sa_addr->sa.sa_family);
694 if (!af->addr_valid(sa_addr, sp, NULL)) {
695 retval = -EADDRNOTAVAIL;
699 if (sa_addr->v4.sin_port &&
700 sa_addr->v4.sin_port != htons(bp->port)) {
705 if (!sa_addr->v4.sin_port)
706 sa_addr->v4.sin_port = htons(bp->port);
708 /* FIXME - There is probably a need to check if sk->sk_saddr and
709 * sk->sk_rcv_addr are currently set to one of the addresses to
710 * be removed. This is something which needs to be looked into
711 * when we are fixing the outstanding issues with multi-homing
712 * socket routing and failover schemes. Refer to comments in
713 * sctp_do_bind(). -daisy
715 retval = sctp_del_bind_addr(bp, sa_addr);
717 addr_buf += af->sockaddr_len;
720 /* Failed. Add the ones that has been removed back */
722 sctp_bindx_add(sk, addrs, cnt);
730 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
731 * the associations that are part of the endpoint indicating that a list of
732 * local addresses are removed from the endpoint.
734 * If any of the addresses is already in the bind address list of the
735 * association, we do not send the chunk for that association. But it will not
736 * affect other associations.
738 * Only sctp_setsockopt_bindx() is supposed to call this function.
740 static int sctp_send_asconf_del_ip(struct sock *sk,
741 struct sockaddr *addrs,
744 struct sctp_sock *sp;
745 struct sctp_endpoint *ep;
746 struct sctp_association *asoc;
747 struct sctp_transport *transport;
748 struct sctp_bind_addr *bp;
749 struct sctp_chunk *chunk;
750 union sctp_addr *laddr;
753 struct sctp_sockaddr_entry *saddr;
762 if (!ep->asconf_enable)
765 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
766 __func__, sk, addrs, addrcnt);
768 list_for_each_entry(asoc, &ep->asocs, asocs) {
770 if (!asoc->peer.asconf_capable)
773 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
776 if (!sctp_state(asoc, ESTABLISHED))
779 /* Check if any address in the packed array of addresses is
780 * not present in the bind address list of the association.
781 * If so, do not send the asconf chunk to its peer, but
782 * continue with other associations.
785 for (i = 0; i < addrcnt; i++) {
787 af = sctp_get_af_specific(laddr->v4.sin_family);
793 if (!sctp_assoc_lookup_laddr(asoc, laddr))
796 addr_buf += af->sockaddr_len;
801 /* Find one address in the association's bind address list
802 * that is not in the packed array of addresses. This is to
803 * make sure that we do not delete all the addresses in the
806 bp = &asoc->base.bind_addr;
807 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
809 if ((laddr == NULL) && (addrcnt == 1)) {
810 if (asoc->asconf_addr_del_pending)
812 asoc->asconf_addr_del_pending =
813 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
814 if (asoc->asconf_addr_del_pending == NULL) {
818 asoc->asconf_addr_del_pending->sa.sa_family =
820 asoc->asconf_addr_del_pending->v4.sin_port =
822 if (addrs->sa_family == AF_INET) {
823 struct sockaddr_in *sin;
825 sin = (struct sockaddr_in *)addrs;
826 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
827 } else if (addrs->sa_family == AF_INET6) {
828 struct sockaddr_in6 *sin6;
830 sin6 = (struct sockaddr_in6 *)addrs;
831 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
834 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
835 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
836 asoc->asconf_addr_del_pending);
838 asoc->src_out_of_asoc_ok = 1;
846 /* We do not need RCU protection throughout this loop
847 * because this is done under a socket lock from the
850 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
858 /* Reset use_as_src flag for the addresses in the bind address
859 * list that are to be deleted.
862 for (i = 0; i < addrcnt; i++) {
864 af = sctp_get_af_specific(laddr->v4.sin_family);
865 list_for_each_entry(saddr, &bp->address_list, list) {
866 if (sctp_cmp_addr_exact(&saddr->a, laddr))
867 saddr->state = SCTP_ADDR_DEL;
869 addr_buf += af->sockaddr_len;
872 /* Update the route and saddr entries for all the transports
873 * as some of the addresses in the bind address list are
874 * about to be deleted and cannot be used as source addresses.
876 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
878 sctp_transport_route(transport, NULL,
879 sctp_sk(asoc->base.sk));
883 /* We don't need to transmit ASCONF */
885 retval = sctp_send_asconf(asoc, chunk);
891 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
892 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
894 struct sock *sk = sctp_opt2sk(sp);
895 union sctp_addr *addr;
898 /* It is safe to write port space in caller. */
900 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
901 af = sctp_get_af_specific(addr->sa.sa_family);
904 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
907 if (addrw->state == SCTP_ADDR_NEW)
908 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
910 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
913 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
916 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
919 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
920 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
923 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
924 * Section 3.1.2 for this usage.
926 * addrs is a pointer to an array of one or more socket addresses. Each
927 * address is contained in its appropriate structure (i.e. struct
928 * sockaddr_in or struct sockaddr_in6) the family of the address type
929 * must be used to distinguish the address length (note that this
930 * representation is termed a "packed array" of addresses). The caller
931 * specifies the number of addresses in the array with addrcnt.
933 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
934 * -1, and sets errno to the appropriate error code.
936 * For SCTP, the port given in each socket address must be the same, or
937 * sctp_bindx() will fail, setting errno to EINVAL.
939 * The flags parameter is formed from the bitwise OR of zero or more of
940 * the following currently defined flags:
942 * SCTP_BINDX_ADD_ADDR
944 * SCTP_BINDX_REM_ADDR
946 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
947 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
948 * addresses from the association. The two flags are mutually exclusive;
949 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
950 * not remove all addresses from an association; sctp_bindx() will
951 * reject such an attempt with EINVAL.
953 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
954 * additional addresses with an endpoint after calling bind(). Or use
955 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
956 * socket is associated with so that no new association accepted will be
957 * associated with those addresses. If the endpoint supports dynamic
958 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
959 * endpoint to send the appropriate message to the peer to change the
960 * peers address lists.
962 * Adding and removing addresses from a connected association is
963 * optional functionality. Implementations that do not support this
964 * functionality should return EOPNOTSUPP.
966 * Basically do nothing but copying the addresses from user to kernel
967 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
968 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
971 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
974 * sk The sk of the socket
975 * addrs The pointer to the addresses
976 * addrssize Size of the addrs buffer
977 * op Operation to perform (add or remove, see the flags of
980 * Returns 0 if ok, <0 errno code on error.
982 static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs,
983 int addrs_size, int op)
988 struct sockaddr *sa_addr;
989 void *addr_buf = addrs;
992 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
993 __func__, sk, addr_buf, addrs_size, op);
995 if (unlikely(addrs_size <= 0))
998 /* Walk through the addrs buffer and count the number of addresses. */
999 while (walk_size < addrs_size) {
1000 if (walk_size + sizeof(sa_family_t) > addrs_size)
1004 af = sctp_get_af_specific(sa_addr->sa_family);
1006 /* If the address family is not supported or if this address
1007 * causes the address buffer to overflow return EINVAL.
1009 if (!af || (walk_size + af->sockaddr_len) > addrs_size)
1012 addr_buf += af->sockaddr_len;
1013 walk_size += af->sockaddr_len;
1018 case SCTP_BINDX_ADD_ADDR:
1019 /* Allow security module to validate bindx addresses. */
1020 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1024 err = sctp_bindx_add(sk, addrs, addrcnt);
1027 return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
1028 case SCTP_BINDX_REM_ADDR:
1029 err = sctp_bindx_rem(sk, addrs, addrcnt);
1032 return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
1039 static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
1045 err = sctp_setsockopt_bindx(sk, addrs, addrlen, SCTP_BINDX_ADD_ADDR);
1050 static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
1051 const union sctp_addr *daddr,
1052 const struct sctp_initmsg *init,
1053 struct sctp_transport **tp)
1055 struct sctp_association *asoc;
1056 struct sock *sk = ep->base.sk;
1057 struct net *net = sock_net(sk);
1058 enum sctp_scope scope;
1061 if (sctp_endpoint_is_peeled_off(ep, daddr))
1062 return -EADDRNOTAVAIL;
1064 if (!ep->base.bind_addr.port) {
1065 if (sctp_autobind(sk))
1068 if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
1069 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1073 scope = sctp_scope(daddr);
1074 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1078 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1082 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1091 if (init->sinit_num_ostreams) {
1092 __u16 outcnt = init->sinit_num_ostreams;
1094 asoc->c.sinit_num_ostreams = outcnt;
1095 /* outcnt has been changed, need to re-init stream */
1096 err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
1101 if (init->sinit_max_instreams)
1102 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1104 if (init->sinit_max_attempts)
1105 asoc->max_init_attempts = init->sinit_max_attempts;
1107 if (init->sinit_max_init_timeo)
1108 asoc->max_init_timeo =
1109 msecs_to_jiffies(init->sinit_max_init_timeo);
1113 sctp_association_free(asoc);
1117 static int sctp_connect_add_peer(struct sctp_association *asoc,
1118 union sctp_addr *daddr, int addr_len)
1120 struct sctp_endpoint *ep = asoc->ep;
1121 struct sctp_association *old;
1122 struct sctp_transport *t;
1125 err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
1129 old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
1130 if (old && old != asoc)
1131 return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1134 if (sctp_endpoint_is_peeled_off(ep, daddr))
1135 return -EADDRNOTAVAIL;
1137 t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1144 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1146 * Common routine for handling connect() and sctp_connectx().
1147 * Connect will come in with just a single address.
1149 static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
1150 int addrs_size, int flags, sctp_assoc_t *assoc_id)
1152 struct sctp_sock *sp = sctp_sk(sk);
1153 struct sctp_endpoint *ep = sp->ep;
1154 struct sctp_transport *transport;
1155 struct sctp_association *asoc;
1156 void *addr_buf = kaddrs;
1157 union sctp_addr *daddr;
1162 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1163 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
1167 af = sctp_get_af_specific(daddr->sa.sa_family);
1168 if (!af || af->sockaddr_len > addrs_size)
1171 err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
1175 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1177 return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1180 err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
1183 asoc = transport->asoc;
1185 addr_buf += af->sockaddr_len;
1186 walk_size = af->sockaddr_len;
1187 while (walk_size < addrs_size) {
1189 if (walk_size + sizeof(sa_family_t) > addrs_size)
1193 af = sctp_get_af_specific(daddr->sa.sa_family);
1194 if (!af || af->sockaddr_len + walk_size > addrs_size)
1197 if (asoc->peer.port != ntohs(daddr->v4.sin_port))
1200 err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
1204 addr_buf += af->sockaddr_len;
1205 walk_size += af->sockaddr_len;
1208 /* In case the user of sctp_connectx() wants an association
1209 * id back, assign one now.
1212 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1217 err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
1221 /* Initialize sk's dport and daddr for getpeername() */
1222 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1223 sp->pf->to_sk_daddr(daddr, sk);
1227 *assoc_id = asoc->assoc_id;
1229 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1230 return sctp_wait_for_connect(asoc, &timeo);
1233 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1234 __func__, asoc, kaddrs, err);
1235 sctp_association_free(asoc);
1239 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1242 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1243 * sctp_assoc_t *asoc);
1245 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1246 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1247 * or IPv6 addresses.
1249 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1250 * Section 3.1.2 for this usage.
1252 * addrs is a pointer to an array of one or more socket addresses. Each
1253 * address is contained in its appropriate structure (i.e. struct
1254 * sockaddr_in or struct sockaddr_in6) the family of the address type
1255 * must be used to distengish the address length (note that this
1256 * representation is termed a "packed array" of addresses). The caller
1257 * specifies the number of addresses in the array with addrcnt.
1259 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1260 * the association id of the new association. On failure, sctp_connectx()
1261 * returns -1, and sets errno to the appropriate error code. The assoc_id
1262 * is not touched by the kernel.
1264 * For SCTP, the port given in each socket address must be the same, or
1265 * sctp_connectx() will fail, setting errno to EINVAL.
1267 * An application can use sctp_connectx to initiate an association with
1268 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1269 * allows a caller to specify multiple addresses at which a peer can be
1270 * reached. The way the SCTP stack uses the list of addresses to set up
1271 * the association is implementation dependent. This function only
1272 * specifies that the stack will try to make use of all the addresses in
1273 * the list when needed.
1275 * Note that the list of addresses passed in is only used for setting up
1276 * the association. It does not necessarily equal the set of addresses
1277 * the peer uses for the resulting association. If the caller wants to
1278 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1279 * retrieve them after the association has been set up.
1281 * Basically do nothing but copying the addresses from user to kernel
1282 * land and invoking either sctp_connectx(). This is used for tunneling
1283 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1285 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1288 * sk The sk of the socket
1289 * addrs The pointer to the addresses in user land
1290 * addrssize Size of the addrs buffer
1292 * Returns >=0 if ok, <0 errno code on error.
1294 static int __sctp_setsockopt_connectx(struct sock *sk,
1295 struct sockaddr __user *addrs,
1297 sctp_assoc_t *assoc_id)
1299 struct sockaddr *kaddrs;
1300 int err = 0, flags = 0;
1302 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1303 __func__, sk, addrs, addrs_size);
1305 /* make sure the 1st addr's sa_family is accessible later */
1306 if (unlikely(addrs_size < sizeof(sa_family_t)))
1309 kaddrs = memdup_user(addrs, addrs_size);
1311 return PTR_ERR(kaddrs);
1313 /* Allow security module to validate connectx addresses. */
1314 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1315 (struct sockaddr *)kaddrs,
1320 /* in-kernel sockets don't generally have a file allocated to them
1321 * if all they do is call sock_create_kern().
1323 if (sk->sk_socket->file)
1324 flags = sk->sk_socket->file->f_flags;
1326 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1335 * This is an older interface. It's kept for backward compatibility
1336 * to the option that doesn't provide association id.
1338 static int sctp_setsockopt_connectx_old(struct sock *sk,
1339 struct sockaddr __user *addrs,
1342 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1346 * New interface for the API. The since the API is done with a socket
1347 * option, to make it simple we feed back the association id is as a return
1348 * indication to the call. Error is always negative and association id is
1351 static int sctp_setsockopt_connectx(struct sock *sk,
1352 struct sockaddr __user *addrs,
1355 sctp_assoc_t assoc_id = 0;
1358 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1367 * New (hopefully final) interface for the API.
1368 * We use the sctp_getaddrs_old structure so that use-space library
1369 * can avoid any unnecessary allocations. The only different part
1370 * is that we store the actual length of the address buffer into the
1371 * addrs_num structure member. That way we can re-use the existing
1374 #ifdef CONFIG_COMPAT
1375 struct compat_sctp_getaddrs_old {
1376 sctp_assoc_t assoc_id;
1378 compat_uptr_t addrs; /* struct sockaddr * */
1382 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1383 char __user *optval,
1386 struct sctp_getaddrs_old param;
1387 sctp_assoc_t assoc_id = 0;
1390 #ifdef CONFIG_COMPAT
1391 if (in_compat_syscall()) {
1392 struct compat_sctp_getaddrs_old param32;
1394 if (len < sizeof(param32))
1396 if (copy_from_user(¶m32, optval, sizeof(param32)))
1399 param.assoc_id = param32.assoc_id;
1400 param.addr_num = param32.addr_num;
1401 param.addrs = compat_ptr(param32.addrs);
1405 if (len < sizeof(param))
1407 if (copy_from_user(¶m, optval, sizeof(param)))
1411 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1412 param.addrs, param.addr_num,
1414 if (err == 0 || err == -EINPROGRESS) {
1415 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1417 if (put_user(sizeof(assoc_id), optlen))
1424 /* API 3.1.4 close() - UDP Style Syntax
1425 * Applications use close() to perform graceful shutdown (as described in
1426 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1427 * by a UDP-style socket.
1431 * ret = close(int sd);
1433 * sd - the socket descriptor of the associations to be closed.
1435 * To gracefully shutdown a specific association represented by the
1436 * UDP-style socket, an application should use the sendmsg() call,
1437 * passing no user data, but including the appropriate flag in the
1438 * ancillary data (see Section xxxx).
1440 * If sd in the close() call is a branched-off socket representing only
1441 * one association, the shutdown is performed on that association only.
1443 * 4.1.6 close() - TCP Style Syntax
1445 * Applications use close() to gracefully close down an association.
1449 * int close(int sd);
1451 * sd - the socket descriptor of the association to be closed.
1453 * After an application calls close() on a socket descriptor, no further
1454 * socket operations will succeed on that descriptor.
1456 * API 7.1.4 SO_LINGER
1458 * An application using the TCP-style socket can use this option to
1459 * perform the SCTP ABORT primitive. The linger option structure is:
1462 * int l_onoff; // option on/off
1463 * int l_linger; // linger time
1466 * To enable the option, set l_onoff to 1. If the l_linger value is set
1467 * to 0, calling close() is the same as the ABORT primitive. If the
1468 * value is set to a negative value, the setsockopt() call will return
1469 * an error. If the value is set to a positive value linger_time, the
1470 * close() can be blocked for at most linger_time ms. If the graceful
1471 * shutdown phase does not finish during this period, close() will
1472 * return but the graceful shutdown phase continues in the system.
1474 static void sctp_close(struct sock *sk, long timeout)
1476 struct net *net = sock_net(sk);
1477 struct sctp_endpoint *ep;
1478 struct sctp_association *asoc;
1479 struct list_head *pos, *temp;
1480 unsigned int data_was_unread;
1482 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1484 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1485 sk->sk_shutdown = SHUTDOWN_MASK;
1486 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1488 ep = sctp_sk(sk)->ep;
1490 /* Clean up any skbs sitting on the receive queue. */
1491 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1492 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1494 /* Walk all associations on an endpoint. */
1495 list_for_each_safe(pos, temp, &ep->asocs) {
1496 asoc = list_entry(pos, struct sctp_association, asocs);
1498 if (sctp_style(sk, TCP)) {
1499 /* A closed association can still be in the list if
1500 * it belongs to a TCP-style listening socket that is
1501 * not yet accepted. If so, free it. If not, send an
1502 * ABORT or SHUTDOWN based on the linger options.
1504 if (sctp_state(asoc, CLOSED)) {
1505 sctp_association_free(asoc);
1510 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1511 !skb_queue_empty(&asoc->ulpq.reasm) ||
1512 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1513 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1514 struct sctp_chunk *chunk;
1516 chunk = sctp_make_abort_user(asoc, NULL, 0);
1517 sctp_primitive_ABORT(net, asoc, chunk);
1519 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1522 /* On a TCP-style socket, block for at most linger_time if set. */
1523 if (sctp_style(sk, TCP) && timeout)
1524 sctp_wait_for_close(sk, timeout);
1526 /* This will run the backlog queue. */
1529 /* Supposedly, no process has access to the socket, but
1530 * the net layers still may.
1531 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1532 * held and that should be grabbed before socket lock.
1534 spin_lock_bh(&net->sctp.addr_wq_lock);
1535 bh_lock_sock_nested(sk);
1537 /* Hold the sock, since sk_common_release() will put sock_put()
1538 * and we have just a little more cleanup.
1541 sk_common_release(sk);
1544 spin_unlock_bh(&net->sctp.addr_wq_lock);
1548 SCTP_DBG_OBJCNT_DEC(sock);
1551 /* Handle EPIPE error. */
1552 static int sctp_error(struct sock *sk, int flags, int err)
1555 err = sock_error(sk) ? : -EPIPE;
1556 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1557 send_sig(SIGPIPE, current, 0);
1561 /* API 3.1.3 sendmsg() - UDP Style Syntax
1563 * An application uses sendmsg() and recvmsg() calls to transmit data to
1564 * and receive data from its peer.
1566 * ssize_t sendmsg(int socket, const struct msghdr *message,
1569 * socket - the socket descriptor of the endpoint.
1570 * message - pointer to the msghdr structure which contains a single
1571 * user message and possibly some ancillary data.
1573 * See Section 5 for complete description of the data
1576 * flags - flags sent or received with the user message, see Section
1577 * 5 for complete description of the flags.
1579 * Note: This function could use a rewrite especially when explicit
1580 * connect support comes in.
1582 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1584 static int sctp_msghdr_parse(const struct msghdr *msg,
1585 struct sctp_cmsgs *cmsgs);
1587 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1588 struct sctp_sndrcvinfo *srinfo,
1589 const struct msghdr *msg, size_t msg_len)
1594 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1597 if (msg_len > sk->sk_sndbuf)
1600 memset(cmsgs, 0, sizeof(*cmsgs));
1601 err = sctp_msghdr_parse(msg, cmsgs);
1603 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1607 memset(srinfo, 0, sizeof(*srinfo));
1608 if (cmsgs->srinfo) {
1609 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1610 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1611 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1612 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1613 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1614 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1618 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1619 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1620 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1621 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1622 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1625 if (cmsgs->prinfo) {
1626 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1627 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1628 cmsgs->prinfo->pr_policy);
1631 sflags = srinfo->sinfo_flags;
1632 if (!sflags && msg_len)
1635 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1638 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1639 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1642 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1648 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1649 struct sctp_cmsgs *cmsgs,
1650 union sctp_addr *daddr,
1651 struct sctp_transport **tp)
1653 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1654 struct sctp_association *asoc;
1655 struct cmsghdr *cmsg;
1656 __be32 flowinfo = 0;
1662 if (sflags & (SCTP_EOF | SCTP_ABORT))
1665 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1666 sctp_sstate(sk, CLOSING)))
1667 return -EADDRNOTAVAIL;
1669 /* Label connection socket for first association 1-to-many
1670 * style for client sequence socket()->sendmsg(). This
1671 * needs to be done before sctp_assoc_add_peer() as that will
1672 * set up the initial packet that needs to account for any
1673 * security ip options (CIPSO/CALIPSO) added to the packet.
1675 af = sctp_get_af_specific(daddr->sa.sa_family);
1678 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1679 (struct sockaddr *)daddr,
1684 err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
1689 if (!cmsgs->addrs_msg)
1692 if (daddr->sa.sa_family == AF_INET6)
1693 flowinfo = daddr->v6.sin6_flowinfo;
1695 /* sendv addr list parse */
1696 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1697 union sctp_addr _daddr;
1700 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1701 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1702 cmsg->cmsg_type != SCTP_DSTADDRV6))
1706 memset(daddr, 0, sizeof(*daddr));
1707 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1708 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1709 if (dlen < sizeof(struct in_addr)) {
1714 dlen = sizeof(struct in_addr);
1715 daddr->v4.sin_family = AF_INET;
1716 daddr->v4.sin_port = htons(asoc->peer.port);
1717 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1719 if (dlen < sizeof(struct in6_addr)) {
1724 dlen = sizeof(struct in6_addr);
1725 daddr->v6.sin6_flowinfo = flowinfo;
1726 daddr->v6.sin6_family = AF_INET6;
1727 daddr->v6.sin6_port = htons(asoc->peer.port);
1728 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1731 err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
1739 sctp_association_free(asoc);
1743 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1744 __u16 sflags, struct msghdr *msg,
1747 struct sock *sk = asoc->base.sk;
1748 struct net *net = sock_net(sk);
1750 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1753 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1754 !sctp_state(asoc, ESTABLISHED))
1757 if (sflags & SCTP_EOF) {
1758 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1759 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1764 if (sflags & SCTP_ABORT) {
1765 struct sctp_chunk *chunk;
1767 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1771 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1772 sctp_primitive_ABORT(net, asoc, chunk);
1773 iov_iter_revert(&msg->msg_iter, msg_len);
1781 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1782 struct msghdr *msg, size_t msg_len,
1783 struct sctp_transport *transport,
1784 struct sctp_sndrcvinfo *sinfo)
1786 struct sock *sk = asoc->base.sk;
1787 struct sctp_sock *sp = sctp_sk(sk);
1788 struct net *net = sock_net(sk);
1789 struct sctp_datamsg *datamsg;
1790 bool wait_connect = false;
1791 struct sctp_chunk *chunk;
1795 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1800 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1801 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1806 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1811 if (asoc->pmtu_pending) {
1812 if (sp->param_flags & SPP_PMTUD_ENABLE)
1813 sctp_assoc_sync_pmtu(asoc);
1814 asoc->pmtu_pending = 0;
1817 if (sctp_wspace(asoc) < (int)msg_len)
1818 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1820 if (sk_under_memory_pressure(sk))
1823 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1824 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1825 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1830 if (sctp_state(asoc, CLOSED)) {
1831 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1835 if (asoc->ep->intl_enable) {
1836 timeo = sock_sndtimeo(sk, 0);
1837 err = sctp_wait_for_connect(asoc, &timeo);
1843 wait_connect = true;
1846 pr_debug("%s: we associated primitively\n", __func__);
1849 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1850 if (IS_ERR(datamsg)) {
1851 err = PTR_ERR(datamsg);
1855 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1857 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1858 sctp_chunk_hold(chunk);
1859 sctp_set_owner_w(chunk);
1860 chunk->transport = transport;
1863 err = sctp_primitive_SEND(net, asoc, datamsg);
1865 sctp_datamsg_free(datamsg);
1869 pr_debug("%s: we sent primitively\n", __func__);
1871 sctp_datamsg_put(datamsg);
1873 if (unlikely(wait_connect)) {
1874 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1875 sctp_wait_for_connect(asoc, &timeo);
1884 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1885 const struct msghdr *msg,
1886 struct sctp_cmsgs *cmsgs)
1888 union sctp_addr *daddr = NULL;
1891 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1892 int len = msg->msg_namelen;
1894 if (len > sizeof(*daddr))
1895 len = sizeof(*daddr);
1897 daddr = (union sctp_addr *)msg->msg_name;
1899 err = sctp_verify_addr(sk, daddr, len);
1901 return ERR_PTR(err);
1907 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1908 struct sctp_sndrcvinfo *sinfo,
1909 struct sctp_cmsgs *cmsgs)
1911 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1912 sinfo->sinfo_stream = asoc->default_stream;
1913 sinfo->sinfo_ppid = asoc->default_ppid;
1914 sinfo->sinfo_context = asoc->default_context;
1915 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1918 sinfo->sinfo_flags = asoc->default_flags;
1921 if (!cmsgs->srinfo && !cmsgs->prinfo)
1922 sinfo->sinfo_timetolive = asoc->default_timetolive;
1924 if (cmsgs->authinfo) {
1925 /* Reuse sinfo_tsn to indicate that authinfo was set and
1926 * sinfo_ssn to save the keyid on tx path.
1928 sinfo->sinfo_tsn = 1;
1929 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1933 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1935 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1936 struct sctp_transport *transport = NULL;
1937 struct sctp_sndrcvinfo _sinfo, *sinfo;
1938 struct sctp_association *asoc, *tmp;
1939 struct sctp_cmsgs cmsgs;
1940 union sctp_addr *daddr;
1945 /* Parse and get snd_info */
1946 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1951 sflags = sinfo->sinfo_flags;
1953 /* Get daddr from msg */
1954 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1955 if (IS_ERR(daddr)) {
1956 err = PTR_ERR(daddr);
1962 /* SCTP_SENDALL process */
1963 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
1964 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
1965 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1972 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1974 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
1979 iov_iter_revert(&msg->msg_iter, err);
1985 /* Get and check or create asoc */
1987 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1989 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1994 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
1999 asoc = transport->asoc;
2003 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2006 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2012 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2017 /* Update snd_info with the asoc */
2018 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2020 /* Send msg to the asoc */
2021 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2022 if (err < 0 && err != -ESRCH && new)
2023 sctp_association_free(asoc);
2028 return sctp_error(sk, msg->msg_flags, err);
2031 /* This is an extended version of skb_pull() that removes the data from the
2032 * start of a skb even when data is spread across the list of skb's in the
2033 * frag_list. len specifies the total amount of data that needs to be removed.
2034 * when 'len' bytes could be removed from the skb, it returns 0.
2035 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2036 * could not be removed.
2038 static int sctp_skb_pull(struct sk_buff *skb, int len)
2040 struct sk_buff *list;
2041 int skb_len = skb_headlen(skb);
2044 if (len <= skb_len) {
2045 __skb_pull(skb, len);
2049 __skb_pull(skb, skb_len);
2051 skb_walk_frags(skb, list) {
2052 rlen = sctp_skb_pull(list, len);
2053 skb->len -= (len-rlen);
2054 skb->data_len -= (len-rlen);
2065 /* API 3.1.3 recvmsg() - UDP Style Syntax
2067 * ssize_t recvmsg(int socket, struct msghdr *message,
2070 * socket - the socket descriptor of the endpoint.
2071 * message - pointer to the msghdr structure which contains a single
2072 * user message and possibly some ancillary data.
2074 * See Section 5 for complete description of the data
2077 * flags - flags sent or received with the user message, see Section
2078 * 5 for complete description of the flags.
2080 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2081 int noblock, int flags, int *addr_len)
2083 struct sctp_ulpevent *event = NULL;
2084 struct sctp_sock *sp = sctp_sk(sk);
2085 struct sk_buff *skb, *head_skb;
2090 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2091 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2096 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2097 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2102 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2106 /* Get the total length of the skb including any skb's in the
2115 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2117 event = sctp_skb2event(skb);
2122 if (event->chunk && event->chunk->head_skb)
2123 head_skb = event->chunk->head_skb;
2126 sock_recv_ts_and_drops(msg, sk, head_skb);
2127 if (sctp_ulpevent_is_notification(event)) {
2128 msg->msg_flags |= MSG_NOTIFICATION;
2129 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2131 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2134 /* Check if we allow SCTP_NXTINFO. */
2135 if (sp->recvnxtinfo)
2136 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2137 /* Check if we allow SCTP_RCVINFO. */
2138 if (sp->recvrcvinfo)
2139 sctp_ulpevent_read_rcvinfo(event, msg);
2140 /* Check if we allow SCTP_SNDRCVINFO. */
2141 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2142 sctp_ulpevent_read_sndrcvinfo(event, msg);
2146 /* If skb's length exceeds the user's buffer, update the skb and
2147 * push it back to the receive_queue so that the next call to
2148 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2150 if (skb_len > copied) {
2151 msg->msg_flags &= ~MSG_EOR;
2152 if (flags & MSG_PEEK)
2154 sctp_skb_pull(skb, copied);
2155 skb_queue_head(&sk->sk_receive_queue, skb);
2157 /* When only partial message is copied to the user, increase
2158 * rwnd by that amount. If all the data in the skb is read,
2159 * rwnd is updated when the event is freed.
2161 if (!sctp_ulpevent_is_notification(event))
2162 sctp_assoc_rwnd_increase(event->asoc, copied);
2164 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2165 (event->msg_flags & MSG_EOR))
2166 msg->msg_flags |= MSG_EOR;
2168 msg->msg_flags &= ~MSG_EOR;
2171 if (flags & MSG_PEEK) {
2172 /* Release the skb reference acquired after peeking the skb in
2173 * sctp_skb_recv_datagram().
2177 /* Free the event which includes releasing the reference to
2178 * the owner of the skb, freeing the skb and updating the
2181 sctp_ulpevent_free(event);
2188 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2190 * This option is a on/off flag. If enabled no SCTP message
2191 * fragmentation will be performed. Instead if a message being sent
2192 * exceeds the current PMTU size, the message will NOT be sent and
2193 * instead a error will be indicated to the user.
2195 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2196 char __user *optval,
2197 unsigned int optlen)
2201 if (optlen < sizeof(int))
2204 if (get_user(val, (int __user *)optval))
2207 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2212 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2213 unsigned int optlen)
2215 struct sctp_event_subscribe subscribe;
2216 __u8 *sn_type = (__u8 *)&subscribe;
2217 struct sctp_sock *sp = sctp_sk(sk);
2218 struct sctp_association *asoc;
2221 if (optlen > sizeof(struct sctp_event_subscribe))
2224 if (copy_from_user(&subscribe, optval, optlen))
2227 for (i = 0; i < optlen; i++)
2228 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2231 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2232 asoc->subscribe = sctp_sk(sk)->subscribe;
2234 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2235 * if there is no data to be sent or retransmit, the stack will
2236 * immediately send up this notification.
2238 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2239 struct sctp_ulpevent *event;
2241 asoc = sctp_id2assoc(sk, 0);
2242 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2243 event = sctp_ulpevent_make_sender_dry_event(asoc,
2244 GFP_USER | __GFP_NOWARN);
2248 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2255 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2257 * This socket option is applicable to the UDP-style socket only. When
2258 * set it will cause associations that are idle for more than the
2259 * specified number of seconds to automatically close. An association
2260 * being idle is defined an association that has NOT sent or received
2261 * user data. The special value of '0' indicates that no automatic
2262 * close of any associations should be performed. The option expects an
2263 * integer defining the number of seconds of idle time before an
2264 * association is closed.
2266 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2267 unsigned int optlen)
2269 struct sctp_sock *sp = sctp_sk(sk);
2270 struct net *net = sock_net(sk);
2272 /* Applicable to UDP-style socket only */
2273 if (sctp_style(sk, TCP))
2275 if (optlen != sizeof(int))
2277 if (copy_from_user(&sp->autoclose, optval, optlen))
2280 if (sp->autoclose > net->sctp.max_autoclose)
2281 sp->autoclose = net->sctp.max_autoclose;
2286 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2288 * Applications can enable or disable heartbeats for any peer address of
2289 * an association, modify an address's heartbeat interval, force a
2290 * heartbeat to be sent immediately, and adjust the address's maximum
2291 * number of retransmissions sent before an address is considered
2292 * unreachable. The following structure is used to access and modify an
2293 * address's parameters:
2295 * struct sctp_paddrparams {
2296 * sctp_assoc_t spp_assoc_id;
2297 * struct sockaddr_storage spp_address;
2298 * uint32_t spp_hbinterval;
2299 * uint16_t spp_pathmaxrxt;
2300 * uint32_t spp_pathmtu;
2301 * uint32_t spp_sackdelay;
2302 * uint32_t spp_flags;
2303 * uint32_t spp_ipv6_flowlabel;
2307 * spp_assoc_id - (one-to-many style socket) This is filled in the
2308 * application, and identifies the association for
2310 * spp_address - This specifies which address is of interest.
2311 * spp_hbinterval - This contains the value of the heartbeat interval,
2312 * in milliseconds. If a value of zero
2313 * is present in this field then no changes are to
2314 * be made to this parameter.
2315 * spp_pathmaxrxt - This contains the maximum number of
2316 * retransmissions before this address shall be
2317 * considered unreachable. If a value of zero
2318 * is present in this field then no changes are to
2319 * be made to this parameter.
2320 * spp_pathmtu - When Path MTU discovery is disabled the value
2321 * specified here will be the "fixed" path mtu.
2322 * Note that if the spp_address field is empty
2323 * then all associations on this address will
2324 * have this fixed path mtu set upon them.
2326 * spp_sackdelay - When delayed sack is enabled, this value specifies
2327 * the number of milliseconds that sacks will be delayed
2328 * for. This value will apply to all addresses of an
2329 * association if the spp_address field is empty. Note
2330 * also, that if delayed sack is enabled and this
2331 * value is set to 0, no change is made to the last
2332 * recorded delayed sack timer value.
2334 * spp_flags - These flags are used to control various features
2335 * on an association. The flag field may contain
2336 * zero or more of the following options.
2338 * SPP_HB_ENABLE - Enable heartbeats on the
2339 * specified address. Note that if the address
2340 * field is empty all addresses for the association
2341 * have heartbeats enabled upon them.
2343 * SPP_HB_DISABLE - Disable heartbeats on the
2344 * speicifed address. Note that if the address
2345 * field is empty all addresses for the association
2346 * will have their heartbeats disabled. Note also
2347 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2348 * mutually exclusive, only one of these two should
2349 * be specified. Enabling both fields will have
2350 * undetermined results.
2352 * SPP_HB_DEMAND - Request a user initiated heartbeat
2353 * to be made immediately.
2355 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2356 * heartbeat delayis to be set to the value of 0
2359 * SPP_PMTUD_ENABLE - This field will enable PMTU
2360 * discovery upon the specified address. Note that
2361 * if the address feild is empty then all addresses
2362 * on the association are effected.
2364 * SPP_PMTUD_DISABLE - This field will disable PMTU
2365 * discovery upon the specified address. Note that
2366 * if the address feild is empty then all addresses
2367 * on the association are effected. Not also that
2368 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2369 * exclusive. Enabling both will have undetermined
2372 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2373 * on delayed sack. The time specified in spp_sackdelay
2374 * is used to specify the sack delay for this address. Note
2375 * that if spp_address is empty then all addresses will
2376 * enable delayed sack and take on the sack delay
2377 * value specified in spp_sackdelay.
2378 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2379 * off delayed sack. If the spp_address field is blank then
2380 * delayed sack is disabled for the entire association. Note
2381 * also that this field is mutually exclusive to
2382 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2385 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2386 * setting of the IPV6 flow label value. The value is
2387 * contained in the spp_ipv6_flowlabel field.
2388 * Upon retrieval, this flag will be set to indicate that
2389 * the spp_ipv6_flowlabel field has a valid value returned.
2390 * If a specific destination address is set (in the
2391 * spp_address field), then the value returned is that of
2392 * the address. If just an association is specified (and
2393 * no address), then the association's default flow label
2394 * is returned. If neither an association nor a destination
2395 * is specified, then the socket's default flow label is
2396 * returned. For non-IPv6 sockets, this flag will be left
2399 * SPP_DSCP: Setting this flag enables the setting of the
2400 * Differentiated Services Code Point (DSCP) value
2401 * associated with either the association or a specific
2402 * address. The value is obtained in the spp_dscp field.
2403 * Upon retrieval, this flag will be set to indicate that
2404 * the spp_dscp field has a valid value returned. If a
2405 * specific destination address is set when called (in the
2406 * spp_address field), then that specific destination
2407 * address's DSCP value is returned. If just an association
2408 * is specified, then the association's default DSCP is
2409 * returned. If neither an association nor a destination is
2410 * specified, then the socket's default DSCP is returned.
2412 * spp_ipv6_flowlabel
2413 * - This field is used in conjunction with the
2414 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2415 * The 20 least significant bits are used for the flow
2416 * label. This setting has precedence over any IPv6-layer
2419 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2420 * and contains the DSCP. The 6 most significant bits are
2421 * used for the DSCP. This setting has precedence over any
2422 * IPv4- or IPv6- layer setting.
2424 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2425 struct sctp_transport *trans,
2426 struct sctp_association *asoc,
2427 struct sctp_sock *sp,
2430 int sackdelay_change)
2434 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2435 error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
2436 trans->asoc, trans);
2441 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2442 * this field is ignored. Note also that a value of zero indicates
2443 * the current setting should be left unchanged.
2445 if (params->spp_flags & SPP_HB_ENABLE) {
2447 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2448 * set. This lets us use 0 value when this flag
2451 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2452 params->spp_hbinterval = 0;
2454 if (params->spp_hbinterval ||
2455 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2458 msecs_to_jiffies(params->spp_hbinterval);
2461 msecs_to_jiffies(params->spp_hbinterval);
2463 sp->hbinterval = params->spp_hbinterval;
2470 trans->param_flags =
2471 (trans->param_flags & ~SPP_HB) | hb_change;
2474 (asoc->param_flags & ~SPP_HB) | hb_change;
2477 (sp->param_flags & ~SPP_HB) | hb_change;
2481 /* When Path MTU discovery is disabled the value specified here will
2482 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2483 * include the flag SPP_PMTUD_DISABLE for this field to have any
2486 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2488 trans->pathmtu = params->spp_pathmtu;
2489 sctp_assoc_sync_pmtu(asoc);
2491 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2493 sp->pathmtu = params->spp_pathmtu;
2499 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2500 (params->spp_flags & SPP_PMTUD_ENABLE);
2501 trans->param_flags =
2502 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2504 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2505 sctp_assoc_sync_pmtu(asoc);
2509 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2512 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2516 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2517 * value of this field is ignored. Note also that a value of zero
2518 * indicates the current setting should be left unchanged.
2520 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2523 msecs_to_jiffies(params->spp_sackdelay);
2526 msecs_to_jiffies(params->spp_sackdelay);
2528 sp->sackdelay = params->spp_sackdelay;
2532 if (sackdelay_change) {
2534 trans->param_flags =
2535 (trans->param_flags & ~SPP_SACKDELAY) |
2539 (asoc->param_flags & ~SPP_SACKDELAY) |
2543 (sp->param_flags & ~SPP_SACKDELAY) |
2548 /* Note that a value of zero indicates the current setting should be
2551 if (params->spp_pathmaxrxt) {
2553 trans->pathmaxrxt = params->spp_pathmaxrxt;
2555 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2557 sp->pathmaxrxt = params->spp_pathmaxrxt;
2561 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2563 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2564 trans->flowlabel = params->spp_ipv6_flowlabel &
2565 SCTP_FLOWLABEL_VAL_MASK;
2566 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2569 struct sctp_transport *t;
2571 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2573 if (t->ipaddr.sa.sa_family != AF_INET6)
2575 t->flowlabel = params->spp_ipv6_flowlabel &
2576 SCTP_FLOWLABEL_VAL_MASK;
2577 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2579 asoc->flowlabel = params->spp_ipv6_flowlabel &
2580 SCTP_FLOWLABEL_VAL_MASK;
2581 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2582 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2583 sp->flowlabel = params->spp_ipv6_flowlabel &
2584 SCTP_FLOWLABEL_VAL_MASK;
2585 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2589 if (params->spp_flags & SPP_DSCP) {
2591 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2592 trans->dscp |= SCTP_DSCP_SET_MASK;
2594 struct sctp_transport *t;
2596 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2598 t->dscp = params->spp_dscp &
2600 t->dscp |= SCTP_DSCP_SET_MASK;
2602 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2603 asoc->dscp |= SCTP_DSCP_SET_MASK;
2605 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2606 sp->dscp |= SCTP_DSCP_SET_MASK;
2613 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2614 char __user *optval,
2615 unsigned int optlen)
2617 struct sctp_paddrparams params;
2618 struct sctp_transport *trans = NULL;
2619 struct sctp_association *asoc = NULL;
2620 struct sctp_sock *sp = sctp_sk(sk);
2622 int hb_change, pmtud_change, sackdelay_change;
2624 if (optlen == sizeof(params)) {
2625 if (copy_from_user(¶ms, optval, optlen))
2627 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2628 spp_ipv6_flowlabel), 4)) {
2629 if (copy_from_user(¶ms, optval, optlen))
2631 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2637 /* Validate flags and value parameters. */
2638 hb_change = params.spp_flags & SPP_HB;
2639 pmtud_change = params.spp_flags & SPP_PMTUD;
2640 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2642 if (hb_change == SPP_HB ||
2643 pmtud_change == SPP_PMTUD ||
2644 sackdelay_change == SPP_SACKDELAY ||
2645 params.spp_sackdelay > 500 ||
2646 (params.spp_pathmtu &&
2647 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2650 /* If an address other than INADDR_ANY is specified, and
2651 * no transport is found, then the request is invalid.
2653 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2654 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2655 params.spp_assoc_id);
2660 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2661 * socket is a one to many style socket, and an association
2662 * was not found, then the id was invalid.
2664 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2665 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2666 sctp_style(sk, UDP))
2669 /* Heartbeat demand can only be sent on a transport or
2670 * association, but not a socket.
2672 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2675 /* Process parameters. */
2676 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2677 hb_change, pmtud_change,
2683 /* If changes are for association, also apply parameters to each
2686 if (!trans && asoc) {
2687 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2689 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2690 hb_change, pmtud_change,
2698 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2700 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2703 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2705 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2708 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2709 struct sctp_association *asoc)
2711 struct sctp_transport *trans;
2713 if (params->sack_delay) {
2714 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2716 sctp_spp_sackdelay_enable(asoc->param_flags);
2718 if (params->sack_freq == 1) {
2720 sctp_spp_sackdelay_disable(asoc->param_flags);
2721 } else if (params->sack_freq > 1) {
2722 asoc->sackfreq = params->sack_freq;
2724 sctp_spp_sackdelay_enable(asoc->param_flags);
2727 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2729 if (params->sack_delay) {
2730 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2731 trans->param_flags =
2732 sctp_spp_sackdelay_enable(trans->param_flags);
2734 if (params->sack_freq == 1) {
2735 trans->param_flags =
2736 sctp_spp_sackdelay_disable(trans->param_flags);
2737 } else if (params->sack_freq > 1) {
2738 trans->sackfreq = params->sack_freq;
2739 trans->param_flags =
2740 sctp_spp_sackdelay_enable(trans->param_flags);
2746 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2748 * This option will effect the way delayed acks are performed. This
2749 * option allows you to get or set the delayed ack time, in
2750 * milliseconds. It also allows changing the delayed ack frequency.
2751 * Changing the frequency to 1 disables the delayed sack algorithm. If
2752 * the assoc_id is 0, then this sets or gets the endpoints default
2753 * values. If the assoc_id field is non-zero, then the set or get
2754 * effects the specified association for the one to many model (the
2755 * assoc_id field is ignored by the one to one model). Note that if
2756 * sack_delay or sack_freq are 0 when setting this option, then the
2757 * current values will remain unchanged.
2759 * struct sctp_sack_info {
2760 * sctp_assoc_t sack_assoc_id;
2761 * uint32_t sack_delay;
2762 * uint32_t sack_freq;
2765 * sack_assoc_id - This parameter, indicates which association the user
2766 * is performing an action upon. Note that if this field's value is
2767 * zero then the endpoints default value is changed (effecting future
2768 * associations only).
2770 * sack_delay - This parameter contains the number of milliseconds that
2771 * the user is requesting the delayed ACK timer be set to. Note that
2772 * this value is defined in the standard to be between 200 and 500
2775 * sack_freq - This parameter contains the number of packets that must
2776 * be received before a sack is sent without waiting for the delay
2777 * timer to expire. The default value for this is 2, setting this
2778 * value to 1 will disable the delayed sack algorithm.
2781 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2782 char __user *optval, unsigned int optlen)
2784 struct sctp_sock *sp = sctp_sk(sk);
2785 struct sctp_association *asoc;
2786 struct sctp_sack_info params;
2788 if (optlen == sizeof(struct sctp_sack_info)) {
2789 if (copy_from_user(¶ms, optval, optlen))
2792 if (params.sack_delay == 0 && params.sack_freq == 0)
2794 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2795 pr_warn_ratelimited(DEPRECATED
2797 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2798 "Use struct sctp_sack_info instead\n",
2799 current->comm, task_pid_nr(current));
2800 if (copy_from_user(¶ms, optval, optlen))
2803 if (params.sack_delay == 0)
2804 params.sack_freq = 1;
2806 params.sack_freq = 0;
2810 /* Validate value parameter. */
2811 if (params.sack_delay > 500)
2814 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2815 * socket is a one to many style socket, and an association
2816 * was not found, then the id was invalid.
2818 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2819 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2820 sctp_style(sk, UDP))
2824 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2829 if (sctp_style(sk, TCP))
2830 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2832 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2833 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2834 if (params.sack_delay) {
2835 sp->sackdelay = params.sack_delay;
2837 sctp_spp_sackdelay_enable(sp->param_flags);
2839 if (params.sack_freq == 1) {
2841 sctp_spp_sackdelay_disable(sp->param_flags);
2842 } else if (params.sack_freq > 1) {
2843 sp->sackfreq = params.sack_freq;
2845 sctp_spp_sackdelay_enable(sp->param_flags);
2849 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2850 params.sack_assoc_id == SCTP_ALL_ASSOC)
2851 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2852 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2857 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2859 * Applications can specify protocol parameters for the default association
2860 * initialization. The option name argument to setsockopt() and getsockopt()
2863 * Setting initialization parameters is effective only on an unconnected
2864 * socket (for UDP-style sockets only future associations are effected
2865 * by the change). With TCP-style sockets, this option is inherited by
2866 * sockets derived from a listener socket.
2868 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2870 struct sctp_initmsg sinit;
2871 struct sctp_sock *sp = sctp_sk(sk);
2873 if (optlen != sizeof(struct sctp_initmsg))
2875 if (copy_from_user(&sinit, optval, optlen))
2878 if (sinit.sinit_num_ostreams)
2879 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2880 if (sinit.sinit_max_instreams)
2881 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2882 if (sinit.sinit_max_attempts)
2883 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2884 if (sinit.sinit_max_init_timeo)
2885 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2891 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2893 * Applications that wish to use the sendto() system call may wish to
2894 * specify a default set of parameters that would normally be supplied
2895 * through the inclusion of ancillary data. This socket option allows
2896 * such an application to set the default sctp_sndrcvinfo structure.
2897 * The application that wishes to use this socket option simply passes
2898 * in to this call the sctp_sndrcvinfo structure defined in Section
2899 * 5.2.2) The input parameters accepted by this call include
2900 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2901 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2902 * to this call if the caller is using the UDP model.
2904 static int sctp_setsockopt_default_send_param(struct sock *sk,
2905 char __user *optval,
2906 unsigned int optlen)
2908 struct sctp_sock *sp = sctp_sk(sk);
2909 struct sctp_association *asoc;
2910 struct sctp_sndrcvinfo info;
2912 if (optlen != sizeof(info))
2914 if (copy_from_user(&info, optval, optlen))
2916 if (info.sinfo_flags &
2917 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2918 SCTP_ABORT | SCTP_EOF))
2921 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2922 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
2923 sctp_style(sk, UDP))
2927 asoc->default_stream = info.sinfo_stream;
2928 asoc->default_flags = info.sinfo_flags;
2929 asoc->default_ppid = info.sinfo_ppid;
2930 asoc->default_context = info.sinfo_context;
2931 asoc->default_timetolive = info.sinfo_timetolive;
2936 if (sctp_style(sk, TCP))
2937 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2939 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2940 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2941 sp->default_stream = info.sinfo_stream;
2942 sp->default_flags = info.sinfo_flags;
2943 sp->default_ppid = info.sinfo_ppid;
2944 sp->default_context = info.sinfo_context;
2945 sp->default_timetolive = info.sinfo_timetolive;
2948 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2949 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2950 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2951 asoc->default_stream = info.sinfo_stream;
2952 asoc->default_flags = info.sinfo_flags;
2953 asoc->default_ppid = info.sinfo_ppid;
2954 asoc->default_context = info.sinfo_context;
2955 asoc->default_timetolive = info.sinfo_timetolive;
2962 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2963 * (SCTP_DEFAULT_SNDINFO)
2965 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2966 char __user *optval,
2967 unsigned int optlen)
2969 struct sctp_sock *sp = sctp_sk(sk);
2970 struct sctp_association *asoc;
2971 struct sctp_sndinfo info;
2973 if (optlen != sizeof(info))
2975 if (copy_from_user(&info, optval, optlen))
2977 if (info.snd_flags &
2978 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2979 SCTP_ABORT | SCTP_EOF))
2982 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2983 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
2984 sctp_style(sk, UDP))
2988 asoc->default_stream = info.snd_sid;
2989 asoc->default_flags = info.snd_flags;
2990 asoc->default_ppid = info.snd_ppid;
2991 asoc->default_context = info.snd_context;
2996 if (sctp_style(sk, TCP))
2997 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
2999 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3000 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3001 sp->default_stream = info.snd_sid;
3002 sp->default_flags = info.snd_flags;
3003 sp->default_ppid = info.snd_ppid;
3004 sp->default_context = info.snd_context;
3007 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3008 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3009 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3010 asoc->default_stream = info.snd_sid;
3011 asoc->default_flags = info.snd_flags;
3012 asoc->default_ppid = info.snd_ppid;
3013 asoc->default_context = info.snd_context;
3020 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3022 * Requests that the local SCTP stack use the enclosed peer address as
3023 * the association primary. The enclosed address must be one of the
3024 * association peer's addresses.
3026 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3027 unsigned int optlen)
3029 struct sctp_prim prim;
3030 struct sctp_transport *trans;
3034 if (optlen != sizeof(struct sctp_prim))
3037 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3040 /* Allow security module to validate address but need address len. */
3041 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3045 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3046 (struct sockaddr *)&prim.ssp_addr,
3051 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3055 sctp_assoc_set_primary(trans->asoc, trans);
3061 * 7.1.5 SCTP_NODELAY
3063 * Turn on/off any Nagle-like algorithm. This means that packets are
3064 * generally sent as soon as possible and no unnecessary delays are
3065 * introduced, at the cost of more packets in the network. Expects an
3066 * integer boolean flag.
3068 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3069 unsigned int optlen)
3073 if (optlen < sizeof(int))
3075 if (get_user(val, (int __user *)optval))
3078 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3084 * 7.1.1 SCTP_RTOINFO
3086 * The protocol parameters used to initialize and bound retransmission
3087 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3088 * and modify these parameters.
3089 * All parameters are time values, in milliseconds. A value of 0, when
3090 * modifying the parameters, indicates that the current value should not
3094 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3096 struct sctp_rtoinfo rtoinfo;
3097 struct sctp_association *asoc;
3098 unsigned long rto_min, rto_max;
3099 struct sctp_sock *sp = sctp_sk(sk);
3101 if (optlen != sizeof (struct sctp_rtoinfo))
3104 if (copy_from_user(&rtoinfo, optval, optlen))
3107 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3109 /* Set the values to the specific association */
3110 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3111 sctp_style(sk, UDP))
3114 rto_max = rtoinfo.srto_max;
3115 rto_min = rtoinfo.srto_min;
3118 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3120 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3123 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3125 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3127 if (rto_min > rto_max)
3131 if (rtoinfo.srto_initial != 0)
3133 msecs_to_jiffies(rtoinfo.srto_initial);
3134 asoc->rto_max = rto_max;
3135 asoc->rto_min = rto_min;
3137 /* If there is no association or the association-id = 0
3138 * set the values to the endpoint.
3140 if (rtoinfo.srto_initial != 0)
3141 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3142 sp->rtoinfo.srto_max = rto_max;
3143 sp->rtoinfo.srto_min = rto_min;
3151 * 7.1.2 SCTP_ASSOCINFO
3153 * This option is used to tune the maximum retransmission attempts
3154 * of the association.
3155 * Returns an error if the new association retransmission value is
3156 * greater than the sum of the retransmission value of the peer.
3157 * See [SCTP] for more information.
3160 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3163 struct sctp_assocparams assocparams;
3164 struct sctp_association *asoc;
3166 if (optlen != sizeof(struct sctp_assocparams))
3168 if (copy_from_user(&assocparams, optval, optlen))
3171 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3173 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3174 sctp_style(sk, UDP))
3177 /* Set the values to the specific association */
3179 if (assocparams.sasoc_asocmaxrxt != 0) {
3182 struct sctp_transport *peer_addr;
3184 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3186 path_sum += peer_addr->pathmaxrxt;
3190 /* Only validate asocmaxrxt if we have more than
3191 * one path/transport. We do this because path
3192 * retransmissions are only counted when we have more
3196 assocparams.sasoc_asocmaxrxt > path_sum)
3199 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3202 if (assocparams.sasoc_cookie_life != 0)
3203 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3205 /* Set the values to the endpoint */
3206 struct sctp_sock *sp = sctp_sk(sk);
3208 if (assocparams.sasoc_asocmaxrxt != 0)
3209 sp->assocparams.sasoc_asocmaxrxt =
3210 assocparams.sasoc_asocmaxrxt;
3211 if (assocparams.sasoc_cookie_life != 0)
3212 sp->assocparams.sasoc_cookie_life =
3213 assocparams.sasoc_cookie_life;
3219 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3221 * This socket option is a boolean flag which turns on or off mapped V4
3222 * addresses. If this option is turned on and the socket is type
3223 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3224 * If this option is turned off, then no mapping will be done of V4
3225 * addresses and a user will receive both PF_INET6 and PF_INET type
3226 * addresses on the socket.
3228 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3231 struct sctp_sock *sp = sctp_sk(sk);
3233 if (optlen < sizeof(int))
3235 if (get_user(val, (int __user *)optval))
3246 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3247 * This option will get or set the maximum size to put in any outgoing
3248 * SCTP DATA chunk. If a message is larger than this size it will be
3249 * fragmented by SCTP into the specified size. Note that the underlying
3250 * SCTP implementation may fragment into smaller sized chunks when the
3251 * PMTU of the underlying association is smaller than the value set by
3252 * the user. The default value for this option is '0' which indicates
3253 * the user is NOT limiting fragmentation and only the PMTU will effect
3254 * SCTP's choice of DATA chunk size. Note also that values set larger
3255 * than the maximum size of an IP datagram will effectively let SCTP
3256 * control fragmentation (i.e. the same as setting this option to 0).
3258 * The following structure is used to access and modify this parameter:
3260 * struct sctp_assoc_value {
3261 * sctp_assoc_t assoc_id;
3262 * uint32_t assoc_value;
3265 * assoc_id: This parameter is ignored for one-to-one style sockets.
3266 * For one-to-many style sockets this parameter indicates which
3267 * association the user is performing an action upon. Note that if
3268 * this field's value is zero then the endpoints default value is
3269 * changed (effecting future associations only).
3270 * assoc_value: This parameter specifies the maximum size in bytes.
3272 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3274 struct sctp_sock *sp = sctp_sk(sk);
3275 struct sctp_assoc_value params;
3276 struct sctp_association *asoc;
3279 if (optlen == sizeof(int)) {
3280 pr_warn_ratelimited(DEPRECATED
3282 "Use of int in maxseg socket option.\n"
3283 "Use struct sctp_assoc_value instead\n",
3284 current->comm, task_pid_nr(current));
3285 if (copy_from_user(&val, optval, optlen))
3287 params.assoc_id = SCTP_FUTURE_ASSOC;
3288 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3289 if (copy_from_user(¶ms, optval, optlen))
3291 val = params.assoc_value;
3296 asoc = sctp_id2assoc(sk, params.assoc_id);
3297 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3298 sctp_style(sk, UDP))
3302 int min_len, max_len;
3303 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3304 sizeof(struct sctp_data_chunk);
3306 min_len = sctp_min_frag_point(sp, datasize);
3307 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3309 if (val < min_len || val > max_len)
3314 asoc->user_frag = val;
3315 sctp_assoc_update_frag_point(asoc);
3317 sp->user_frag = val;
3325 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3327 * Requests that the peer mark the enclosed address as the association
3328 * primary. The enclosed address must be one of the association's
3329 * locally bound addresses. The following structure is used to make a
3330 * set primary request:
3332 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3333 unsigned int optlen)
3335 struct sctp_sock *sp;
3336 struct sctp_association *asoc = NULL;
3337 struct sctp_setpeerprim prim;
3338 struct sctp_chunk *chunk;
3344 if (!sp->ep->asconf_enable)
3347 if (optlen != sizeof(struct sctp_setpeerprim))
3350 if (copy_from_user(&prim, optval, optlen))
3353 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3357 if (!asoc->peer.asconf_capable)
3360 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3363 if (!sctp_state(asoc, ESTABLISHED))
3366 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3370 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3371 return -EADDRNOTAVAIL;
3373 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3374 return -EADDRNOTAVAIL;
3376 /* Allow security module to validate address. */
3377 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3378 (struct sockaddr *)&prim.sspp_addr,
3383 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3384 chunk = sctp_make_asconf_set_prim(asoc,
3385 (union sctp_addr *)&prim.sspp_addr);
3389 err = sctp_send_asconf(asoc, chunk);
3391 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3396 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3397 unsigned int optlen)
3399 struct sctp_setadaptation adaptation;
3401 if (optlen != sizeof(struct sctp_setadaptation))
3403 if (copy_from_user(&adaptation, optval, optlen))
3406 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3412 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3414 * The context field in the sctp_sndrcvinfo structure is normally only
3415 * used when a failed message is retrieved holding the value that was
3416 * sent down on the actual send call. This option allows the setting of
3417 * a default context on an association basis that will be received on
3418 * reading messages from the peer. This is especially helpful in the
3419 * one-2-many model for an application to keep some reference to an
3420 * internal state machine that is processing messages on the
3421 * association. Note that the setting of this value only effects
3422 * received messages from the peer and does not effect the value that is
3423 * saved with outbound messages.
3425 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3426 unsigned int optlen)
3428 struct sctp_sock *sp = sctp_sk(sk);
3429 struct sctp_assoc_value params;
3430 struct sctp_association *asoc;
3432 if (optlen != sizeof(struct sctp_assoc_value))
3434 if (copy_from_user(¶ms, optval, optlen))
3437 asoc = sctp_id2assoc(sk, params.assoc_id);
3438 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3439 sctp_style(sk, UDP))
3443 asoc->default_rcv_context = params.assoc_value;
3448 if (sctp_style(sk, TCP))
3449 params.assoc_id = SCTP_FUTURE_ASSOC;
3451 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3452 params.assoc_id == SCTP_ALL_ASSOC)
3453 sp->default_rcv_context = params.assoc_value;
3455 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3456 params.assoc_id == SCTP_ALL_ASSOC)
3457 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3458 asoc->default_rcv_context = params.assoc_value;
3464 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3466 * This options will at a minimum specify if the implementation is doing
3467 * fragmented interleave. Fragmented interleave, for a one to many
3468 * socket, is when subsequent calls to receive a message may return
3469 * parts of messages from different associations. Some implementations
3470 * may allow you to turn this value on or off. If so, when turned off,
3471 * no fragment interleave will occur (which will cause a head of line
3472 * blocking amongst multiple associations sharing the same one to many
3473 * socket). When this option is turned on, then each receive call may
3474 * come from a different association (thus the user must receive data
3475 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3476 * association each receive belongs to.
3478 * This option takes a boolean value. A non-zero value indicates that
3479 * fragmented interleave is on. A value of zero indicates that
3480 * fragmented interleave is off.
3482 * Note that it is important that an implementation that allows this
3483 * option to be turned on, have it off by default. Otherwise an unaware
3484 * application using the one to many model may become confused and act
3487 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3488 char __user *optval,
3489 unsigned int optlen)
3493 if (optlen != sizeof(int))
3495 if (get_user(val, (int __user *)optval))
3498 sctp_sk(sk)->frag_interleave = !!val;
3500 if (!sctp_sk(sk)->frag_interleave)
3501 sctp_sk(sk)->ep->intl_enable = 0;
3507 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3508 * (SCTP_PARTIAL_DELIVERY_POINT)
3510 * This option will set or get the SCTP partial delivery point. This
3511 * point is the size of a message where the partial delivery API will be
3512 * invoked to help free up rwnd space for the peer. Setting this to a
3513 * lower value will cause partial deliveries to happen more often. The
3514 * calls argument is an integer that sets or gets the partial delivery
3515 * point. Note also that the call will fail if the user attempts to set
3516 * this value larger than the socket receive buffer size.
3518 * Note that any single message having a length smaller than or equal to
3519 * the SCTP partial delivery point will be delivered in one single read
3520 * call as long as the user provided buffer is large enough to hold the
3523 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3524 char __user *optval,
3525 unsigned int optlen)
3529 if (optlen != sizeof(u32))
3531 if (get_user(val, (int __user *)optval))
3534 /* Note: We double the receive buffer from what the user sets
3535 * it to be, also initial rwnd is based on rcvbuf/2.
3537 if (val > (sk->sk_rcvbuf >> 1))
3540 sctp_sk(sk)->pd_point = val;
3542 return 0; /* is this the right error code? */
3546 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3548 * This option will allow a user to change the maximum burst of packets
3549 * that can be emitted by this association. Note that the default value
3550 * is 4, and some implementations may restrict this setting so that it
3551 * can only be lowered.
3553 * NOTE: This text doesn't seem right. Do this on a socket basis with
3554 * future associations inheriting the socket value.
3556 static int sctp_setsockopt_maxburst(struct sock *sk,
3557 char __user *optval,
3558 unsigned int optlen)
3560 struct sctp_sock *sp = sctp_sk(sk);
3561 struct sctp_assoc_value params;
3562 struct sctp_association *asoc;
3564 if (optlen == sizeof(int)) {
3565 pr_warn_ratelimited(DEPRECATED
3567 "Use of int in max_burst socket option deprecated.\n"
3568 "Use struct sctp_assoc_value instead\n",
3569 current->comm, task_pid_nr(current));
3570 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3572 params.assoc_id = SCTP_FUTURE_ASSOC;
3573 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3574 if (copy_from_user(¶ms, optval, optlen))
3579 asoc = sctp_id2assoc(sk, params.assoc_id);
3580 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3581 sctp_style(sk, UDP))
3585 asoc->max_burst = params.assoc_value;
3590 if (sctp_style(sk, TCP))
3591 params.assoc_id = SCTP_FUTURE_ASSOC;
3593 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3594 params.assoc_id == SCTP_ALL_ASSOC)
3595 sp->max_burst = params.assoc_value;
3597 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3598 params.assoc_id == SCTP_ALL_ASSOC)
3599 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3600 asoc->max_burst = params.assoc_value;
3606 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3608 * This set option adds a chunk type that the user is requesting to be
3609 * received only in an authenticated way. Changes to the list of chunks
3610 * will only effect future associations on the socket.
3612 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3613 char __user *optval,
3614 unsigned int optlen)
3616 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3617 struct sctp_authchunk val;
3619 if (!ep->auth_enable)
3622 if (optlen != sizeof(struct sctp_authchunk))
3624 if (copy_from_user(&val, optval, optlen))
3627 switch (val.sauth_chunk) {
3629 case SCTP_CID_INIT_ACK:
3630 case SCTP_CID_SHUTDOWN_COMPLETE:
3635 /* add this chunk id to the endpoint */
3636 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3640 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3642 * This option gets or sets the list of HMAC algorithms that the local
3643 * endpoint requires the peer to use.
3645 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3646 char __user *optval,
3647 unsigned int optlen)
3649 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3650 struct sctp_hmacalgo *hmacs;
3654 if (!ep->auth_enable)
3657 if (optlen < sizeof(struct sctp_hmacalgo))
3659 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3660 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3662 hmacs = memdup_user(optval, optlen);
3664 return PTR_ERR(hmacs);
3666 idents = hmacs->shmac_num_idents;
3667 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3668 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3673 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3680 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3682 * This option will set a shared secret key which is used to build an
3683 * association shared key.
3685 static int sctp_setsockopt_auth_key(struct sock *sk,
3686 char __user *optval,
3687 unsigned int optlen)
3689 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3690 struct sctp_authkey *authkey;
3691 struct sctp_association *asoc;
3694 if (optlen <= sizeof(struct sctp_authkey))
3696 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3699 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3701 authkey = memdup_user(optval, optlen);
3702 if (IS_ERR(authkey))
3703 return PTR_ERR(authkey);
3705 if (authkey->sca_keylength > optlen - sizeof(*authkey))
3708 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3709 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3710 sctp_style(sk, UDP))
3714 ret = sctp_auth_set_key(ep, asoc, authkey);
3718 if (sctp_style(sk, TCP))
3719 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3721 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3722 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3723 ret = sctp_auth_set_key(ep, asoc, authkey);
3730 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3731 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3732 list_for_each_entry(asoc, &ep->asocs, asocs) {
3733 int res = sctp_auth_set_key(ep, asoc, authkey);
3746 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3748 * This option will get or set the active shared key to be used to build
3749 * the association shared key.
3751 static int sctp_setsockopt_active_key(struct sock *sk,
3752 char __user *optval,
3753 unsigned int optlen)
3755 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3756 struct sctp_association *asoc;
3757 struct sctp_authkeyid val;
3760 if (optlen != sizeof(struct sctp_authkeyid))
3762 if (copy_from_user(&val, optval, optlen))
3765 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3766 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3767 sctp_style(sk, UDP))
3771 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3773 if (sctp_style(sk, TCP))
3774 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3776 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3777 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3778 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3783 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3784 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3785 list_for_each_entry(asoc, &ep->asocs, asocs) {
3786 int res = sctp_auth_set_active_key(ep, asoc,
3787 val.scact_keynumber);
3798 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3800 * This set option will delete a shared secret key from use.
3802 static int sctp_setsockopt_del_key(struct sock *sk,
3803 char __user *optval,
3804 unsigned int optlen)
3806 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3807 struct sctp_association *asoc;
3808 struct sctp_authkeyid val;
3811 if (optlen != sizeof(struct sctp_authkeyid))
3813 if (copy_from_user(&val, optval, optlen))
3816 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3817 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3818 sctp_style(sk, UDP))
3822 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3824 if (sctp_style(sk, TCP))
3825 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3827 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3828 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3829 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3834 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3835 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3836 list_for_each_entry(asoc, &ep->asocs, asocs) {
3837 int res = sctp_auth_del_key_id(ep, asoc,
3838 val.scact_keynumber);
3849 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3851 * This set option will deactivate a shared secret key.
3853 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3854 unsigned int optlen)
3856 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3857 struct sctp_association *asoc;
3858 struct sctp_authkeyid val;
3861 if (optlen != sizeof(struct sctp_authkeyid))
3863 if (copy_from_user(&val, optval, optlen))
3866 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3867 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3868 sctp_style(sk, UDP))
3872 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3874 if (sctp_style(sk, TCP))
3875 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3877 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3878 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3879 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3884 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3885 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3886 list_for_each_entry(asoc, &ep->asocs, asocs) {
3887 int res = sctp_auth_deact_key_id(ep, asoc,
3888 val.scact_keynumber);
3899 * 8.1.23 SCTP_AUTO_ASCONF
3901 * This option will enable or disable the use of the automatic generation of
3902 * ASCONF chunks to add and delete addresses to an existing association. Note
3903 * that this option has two caveats namely: a) it only affects sockets that
3904 * are bound to all addresses available to the SCTP stack, and b) the system
3905 * administrator may have an overriding control that turns the ASCONF feature
3906 * off no matter what setting the socket option may have.
3907 * This option expects an integer boolean flag, where a non-zero value turns on
3908 * the option, and a zero value turns off the option.
3909 * Note. In this implementation, socket operation overrides default parameter
3910 * being set by sysctl as well as FreeBSD implementation
3912 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3913 unsigned int optlen)
3916 struct sctp_sock *sp = sctp_sk(sk);
3918 if (optlen < sizeof(int))
3920 if (get_user(val, (int __user *)optval))
3922 if (!sctp_is_ep_boundall(sk) && val)
3924 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3927 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3928 if (val == 0 && sp->do_auto_asconf) {
3929 list_del(&sp->auto_asconf_list);
3930 sp->do_auto_asconf = 0;
3931 } else if (val && !sp->do_auto_asconf) {
3932 list_add_tail(&sp->auto_asconf_list,
3933 &sock_net(sk)->sctp.auto_asconf_splist);
3934 sp->do_auto_asconf = 1;
3936 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3941 * SCTP_PEER_ADDR_THLDS
3943 * This option allows us to alter the partially failed threshold for one or all
3944 * transports in an association. See Section 6.1 of:
3945 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3947 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3948 char __user *optval,
3949 unsigned int optlen, bool v2)
3951 struct sctp_paddrthlds_v2 val;
3952 struct sctp_transport *trans;
3953 struct sctp_association *asoc;
3956 len = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
3959 if (copy_from_user(&val, optval, len))
3962 if (v2 && val.spt_pathpfthld > val.spt_pathcpthld)
3965 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3966 trans = sctp_addr_id2transport(sk, &val.spt_address,
3971 if (val.spt_pathmaxrxt)
3972 trans->pathmaxrxt = val.spt_pathmaxrxt;
3974 trans->ps_retrans = val.spt_pathcpthld;
3975 trans->pf_retrans = val.spt_pathpfthld;
3980 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3981 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
3982 sctp_style(sk, UDP))
3986 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3988 if (val.spt_pathmaxrxt)
3989 trans->pathmaxrxt = val.spt_pathmaxrxt;
3991 trans->ps_retrans = val.spt_pathcpthld;
3992 trans->pf_retrans = val.spt_pathpfthld;
3995 if (val.spt_pathmaxrxt)
3996 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3998 asoc->ps_retrans = val.spt_pathcpthld;
3999 asoc->pf_retrans = val.spt_pathpfthld;
4001 struct sctp_sock *sp = sctp_sk(sk);
4003 if (val.spt_pathmaxrxt)
4004 sp->pathmaxrxt = val.spt_pathmaxrxt;
4006 sp->ps_retrans = val.spt_pathcpthld;
4007 sp->pf_retrans = val.spt_pathpfthld;
4013 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4014 char __user *optval,
4015 unsigned int optlen)
4019 if (optlen < sizeof(int))
4021 if (get_user(val, (int __user *) optval))
4024 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4029 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4030 char __user *optval,
4031 unsigned int optlen)
4035 if (optlen < sizeof(int))
4037 if (get_user(val, (int __user *) optval))
4040 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4045 static int sctp_setsockopt_pr_supported(struct sock *sk,
4046 char __user *optval,
4047 unsigned int optlen)
4049 struct sctp_assoc_value params;
4050 struct sctp_association *asoc;
4052 if (optlen != sizeof(params))
4055 if (copy_from_user(¶ms, optval, optlen))
4058 asoc = sctp_id2assoc(sk, params.assoc_id);
4059 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4060 sctp_style(sk, UDP))
4063 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4068 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4069 char __user *optval,
4070 unsigned int optlen)
4072 struct sctp_sock *sp = sctp_sk(sk);
4073 struct sctp_default_prinfo info;
4074 struct sctp_association *asoc;
4075 int retval = -EINVAL;
4077 if (optlen != sizeof(info))
4080 if (copy_from_user(&info, optval, sizeof(info))) {
4085 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4088 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4091 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4092 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4093 sctp_style(sk, UDP))
4099 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4100 asoc->default_timetolive = info.pr_value;
4104 if (sctp_style(sk, TCP))
4105 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4107 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4108 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4109 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4110 sp->default_timetolive = info.pr_value;
4113 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4114 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4115 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4116 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4117 asoc->default_timetolive = info.pr_value;
4125 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4126 char __user *optval,
4127 unsigned int optlen)
4129 struct sctp_assoc_value params;
4130 struct sctp_association *asoc;
4131 int retval = -EINVAL;
4133 if (optlen != sizeof(params))
4136 if (copy_from_user(¶ms, optval, optlen)) {
4141 asoc = sctp_id2assoc(sk, params.assoc_id);
4142 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4143 sctp_style(sk, UDP))
4146 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4154 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4155 char __user *optval,
4156 unsigned int optlen)
4158 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4159 struct sctp_assoc_value params;
4160 struct sctp_association *asoc;
4161 int retval = -EINVAL;
4163 if (optlen != sizeof(params))
4166 if (copy_from_user(¶ms, optval, optlen)) {
4171 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4174 asoc = sctp_id2assoc(sk, params.assoc_id);
4175 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4176 sctp_style(sk, UDP))
4182 asoc->strreset_enable = params.assoc_value;
4186 if (sctp_style(sk, TCP))
4187 params.assoc_id = SCTP_FUTURE_ASSOC;
4189 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4190 params.assoc_id == SCTP_ALL_ASSOC)
4191 ep->strreset_enable = params.assoc_value;
4193 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4194 params.assoc_id == SCTP_ALL_ASSOC)
4195 list_for_each_entry(asoc, &ep->asocs, asocs)
4196 asoc->strreset_enable = params.assoc_value;
4202 static int sctp_setsockopt_reset_streams(struct sock *sk,
4203 char __user *optval,
4204 unsigned int optlen)
4206 struct sctp_reset_streams *params;
4207 struct sctp_association *asoc;
4208 int retval = -EINVAL;
4210 if (optlen < sizeof(*params))
4212 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4213 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4214 sizeof(__u16) * sizeof(*params));
4216 params = memdup_user(optval, optlen);
4218 return PTR_ERR(params);
4220 if (params->srs_number_streams * sizeof(__u16) >
4221 optlen - sizeof(*params))
4224 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4228 retval = sctp_send_reset_streams(asoc, params);
4235 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4236 char __user *optval,
4237 unsigned int optlen)
4239 struct sctp_association *asoc;
4240 sctp_assoc_t associd;
4241 int retval = -EINVAL;
4243 if (optlen != sizeof(associd))
4246 if (copy_from_user(&associd, optval, optlen)) {
4251 asoc = sctp_id2assoc(sk, associd);
4255 retval = sctp_send_reset_assoc(asoc);
4261 static int sctp_setsockopt_add_streams(struct sock *sk,
4262 char __user *optval,
4263 unsigned int optlen)
4265 struct sctp_association *asoc;
4266 struct sctp_add_streams params;
4267 int retval = -EINVAL;
4269 if (optlen != sizeof(params))
4272 if (copy_from_user(¶ms, optval, optlen)) {
4277 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4281 retval = sctp_send_add_streams(asoc, ¶ms);
4287 static int sctp_setsockopt_scheduler(struct sock *sk,
4288 char __user *optval,
4289 unsigned int optlen)
4291 struct sctp_sock *sp = sctp_sk(sk);
4292 struct sctp_association *asoc;
4293 struct sctp_assoc_value params;
4296 if (optlen < sizeof(params))
4299 optlen = sizeof(params);
4300 if (copy_from_user(¶ms, optval, optlen))
4303 if (params.assoc_value > SCTP_SS_MAX)
4306 asoc = sctp_id2assoc(sk, params.assoc_id);
4307 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4308 sctp_style(sk, UDP))
4312 return sctp_sched_set_sched(asoc, params.assoc_value);
4314 if (sctp_style(sk, TCP))
4315 params.assoc_id = SCTP_FUTURE_ASSOC;
4317 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4318 params.assoc_id == SCTP_ALL_ASSOC)
4319 sp->default_ss = params.assoc_value;
4321 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4322 params.assoc_id == SCTP_ALL_ASSOC) {
4323 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4324 int ret = sctp_sched_set_sched(asoc,
4325 params.assoc_value);
4335 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4336 char __user *optval,
4337 unsigned int optlen)
4339 struct sctp_stream_value params;
4340 struct sctp_association *asoc;
4341 int retval = -EINVAL;
4343 if (optlen < sizeof(params))
4346 optlen = sizeof(params);
4347 if (copy_from_user(¶ms, optval, optlen)) {
4352 asoc = sctp_id2assoc(sk, params.assoc_id);
4353 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4354 sctp_style(sk, UDP))
4358 retval = sctp_sched_set_value(asoc, params.stream_id,
4359 params.stream_value, GFP_KERNEL);
4365 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4366 int ret = sctp_sched_set_value(asoc, params.stream_id,
4367 params.stream_value, GFP_KERNEL);
4368 if (ret && !retval) /* try to return the 1st error. */
4376 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4377 char __user *optval,
4378 unsigned int optlen)
4380 struct sctp_sock *sp = sctp_sk(sk);
4381 struct sctp_assoc_value params;
4382 struct sctp_association *asoc;
4383 int retval = -EINVAL;
4385 if (optlen < sizeof(params))
4388 optlen = sizeof(params);
4389 if (copy_from_user(¶ms, optval, optlen)) {
4394 asoc = sctp_id2assoc(sk, params.assoc_id);
4395 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4396 sctp_style(sk, UDP))
4399 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4404 sp->ep->intl_enable = !!params.assoc_value;
4412 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4413 unsigned int optlen)
4417 if (!sctp_style(sk, TCP))
4420 if (sctp_sk(sk)->ep->base.bind_addr.port)
4423 if (optlen < sizeof(int))
4426 if (get_user(val, (int __user *)optval))
4429 sctp_sk(sk)->reuse = !!val;
4434 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4435 struct sctp_association *asoc)
4437 struct sctp_ulpevent *event;
4439 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4441 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4442 if (sctp_outq_is_empty(&asoc->outqueue)) {
4443 event = sctp_ulpevent_make_sender_dry_event(asoc,
4444 GFP_USER | __GFP_NOWARN);
4448 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4455 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4456 unsigned int optlen)
4458 struct sctp_sock *sp = sctp_sk(sk);
4459 struct sctp_association *asoc;
4460 struct sctp_event param;
4463 if (optlen < sizeof(param))
4466 optlen = sizeof(param);
4467 if (copy_from_user(¶m, optval, optlen))
4470 if (param.se_type < SCTP_SN_TYPE_BASE ||
4471 param.se_type > SCTP_SN_TYPE_MAX)
4474 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4475 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4476 sctp_style(sk, UDP))
4480 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4482 if (sctp_style(sk, TCP))
4483 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4485 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4486 param.se_assoc_id == SCTP_ALL_ASSOC)
4487 sctp_ulpevent_type_set(&sp->subscribe,
4488 param.se_type, param.se_on);
4490 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4491 param.se_assoc_id == SCTP_ALL_ASSOC) {
4492 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4493 int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4503 static int sctp_setsockopt_asconf_supported(struct sock *sk,
4504 char __user *optval,
4505 unsigned int optlen)
4507 struct sctp_assoc_value params;
4508 struct sctp_association *asoc;
4509 struct sctp_endpoint *ep;
4510 int retval = -EINVAL;
4512 if (optlen != sizeof(params))
4515 if (copy_from_user(¶ms, optval, optlen)) {
4520 asoc = sctp_id2assoc(sk, params.assoc_id);
4521 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4522 sctp_style(sk, UDP))
4525 ep = sctp_sk(sk)->ep;
4526 ep->asconf_enable = !!params.assoc_value;
4528 if (ep->asconf_enable && ep->auth_enable) {
4529 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4530 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4539 static int sctp_setsockopt_auth_supported(struct sock *sk,
4540 char __user *optval,
4541 unsigned int optlen)
4543 struct sctp_assoc_value params;
4544 struct sctp_association *asoc;
4545 struct sctp_endpoint *ep;
4546 int retval = -EINVAL;
4548 if (optlen != sizeof(params))
4551 if (copy_from_user(¶ms, optval, optlen)) {
4556 asoc = sctp_id2assoc(sk, params.assoc_id);
4557 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4558 sctp_style(sk, UDP))
4561 ep = sctp_sk(sk)->ep;
4562 if (params.assoc_value) {
4563 retval = sctp_auth_init(ep, GFP_KERNEL);
4566 if (ep->asconf_enable) {
4567 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4568 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4572 ep->auth_enable = !!params.assoc_value;
4579 static int sctp_setsockopt_ecn_supported(struct sock *sk,
4580 char __user *optval,
4581 unsigned int optlen)
4583 struct sctp_assoc_value params;
4584 struct sctp_association *asoc;
4585 int retval = -EINVAL;
4587 if (optlen != sizeof(params))
4590 if (copy_from_user(¶ms, optval, optlen)) {
4595 asoc = sctp_id2assoc(sk, params.assoc_id);
4596 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4597 sctp_style(sk, UDP))
4600 sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
4607 static int sctp_setsockopt_pf_expose(struct sock *sk,
4608 char __user *optval,
4609 unsigned int optlen)
4611 struct sctp_assoc_value params;
4612 struct sctp_association *asoc;
4613 int retval = -EINVAL;
4615 if (optlen != sizeof(params))
4618 if (copy_from_user(¶ms, optval, optlen)) {
4623 if (params.assoc_value > SCTP_PF_EXPOSE_MAX)
4626 asoc = sctp_id2assoc(sk, params.assoc_id);
4627 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4628 sctp_style(sk, UDP))
4632 asoc->pf_expose = params.assoc_value;
4634 sctp_sk(sk)->pf_expose = params.assoc_value;
4641 /* API 6.2 setsockopt(), getsockopt()
4643 * Applications use setsockopt() and getsockopt() to set or retrieve
4644 * socket options. Socket options are used to change the default
4645 * behavior of sockets calls. They are described in Section 7.
4649 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4650 * int __user *optlen);
4651 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4654 * sd - the socket descript.
4655 * level - set to IPPROTO_SCTP for all SCTP options.
4656 * optname - the option name.
4657 * optval - the buffer to store the value of the option.
4658 * optlen - the size of the buffer.
4660 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4661 char __user *optval, unsigned int optlen)
4666 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4668 /* I can hardly begin to describe how wrong this is. This is
4669 * so broken as to be worse than useless. The API draft
4670 * REALLY is NOT helpful here... I am not convinced that the
4671 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4672 * are at all well-founded.
4674 if (level != SOL_SCTP) {
4675 struct sctp_af *af = sctp_sk(sk)->pf->af;
4676 retval = af->setsockopt(sk, level, optname, optval, optlen);
4681 kopt = memdup_user(optval, optlen);
4683 return PTR_ERR(kopt);
4689 case SCTP_SOCKOPT_BINDX_ADD:
4690 /* 'optlen' is the size of the addresses buffer. */
4691 retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4692 SCTP_BINDX_ADD_ADDR);
4695 case SCTP_SOCKOPT_BINDX_REM:
4696 /* 'optlen' is the size of the addresses buffer. */
4697 retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4698 SCTP_BINDX_REM_ADDR);
4701 case SCTP_SOCKOPT_CONNECTX_OLD:
4702 /* 'optlen' is the size of the addresses buffer. */
4703 retval = sctp_setsockopt_connectx_old(sk,
4704 (struct sockaddr __user *)optval,
4708 case SCTP_SOCKOPT_CONNECTX:
4709 /* 'optlen' is the size of the addresses buffer. */
4710 retval = sctp_setsockopt_connectx(sk,
4711 (struct sockaddr __user *)optval,
4715 case SCTP_DISABLE_FRAGMENTS:
4716 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4720 retval = sctp_setsockopt_events(sk, optval, optlen);
4723 case SCTP_AUTOCLOSE:
4724 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4727 case SCTP_PEER_ADDR_PARAMS:
4728 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4731 case SCTP_DELAYED_SACK:
4732 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4734 case SCTP_PARTIAL_DELIVERY_POINT:
4735 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4739 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4741 case SCTP_DEFAULT_SEND_PARAM:
4742 retval = sctp_setsockopt_default_send_param(sk, optval,
4745 case SCTP_DEFAULT_SNDINFO:
4746 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4748 case SCTP_PRIMARY_ADDR:
4749 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4751 case SCTP_SET_PEER_PRIMARY_ADDR:
4752 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4755 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4758 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4760 case SCTP_ASSOCINFO:
4761 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4763 case SCTP_I_WANT_MAPPED_V4_ADDR:
4764 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4767 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4769 case SCTP_ADAPTATION_LAYER:
4770 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4773 retval = sctp_setsockopt_context(sk, optval, optlen);
4775 case SCTP_FRAGMENT_INTERLEAVE:
4776 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4778 case SCTP_MAX_BURST:
4779 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4781 case SCTP_AUTH_CHUNK:
4782 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4784 case SCTP_HMAC_IDENT:
4785 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4788 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4790 case SCTP_AUTH_ACTIVE_KEY:
4791 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4793 case SCTP_AUTH_DELETE_KEY:
4794 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4796 case SCTP_AUTH_DEACTIVATE_KEY:
4797 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4799 case SCTP_AUTO_ASCONF:
4800 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4802 case SCTP_PEER_ADDR_THLDS:
4803 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
4806 case SCTP_PEER_ADDR_THLDS_V2:
4807 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
4810 case SCTP_RECVRCVINFO:
4811 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4813 case SCTP_RECVNXTINFO:
4814 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4816 case SCTP_PR_SUPPORTED:
4817 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4819 case SCTP_DEFAULT_PRINFO:
4820 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4822 case SCTP_RECONFIG_SUPPORTED:
4823 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4825 case SCTP_ENABLE_STREAM_RESET:
4826 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4828 case SCTP_RESET_STREAMS:
4829 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4831 case SCTP_RESET_ASSOC:
4832 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4834 case SCTP_ADD_STREAMS:
4835 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4837 case SCTP_STREAM_SCHEDULER:
4838 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4840 case SCTP_STREAM_SCHEDULER_VALUE:
4841 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4843 case SCTP_INTERLEAVING_SUPPORTED:
4844 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4847 case SCTP_REUSE_PORT:
4848 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4851 retval = sctp_setsockopt_event(sk, optval, optlen);
4853 case SCTP_ASCONF_SUPPORTED:
4854 retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
4856 case SCTP_AUTH_SUPPORTED:
4857 retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
4859 case SCTP_ECN_SUPPORTED:
4860 retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
4862 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
4863 retval = sctp_setsockopt_pf_expose(sk, optval, optlen);
4866 retval = -ENOPROTOOPT;
4877 /* API 3.1.6 connect() - UDP Style Syntax
4879 * An application may use the connect() call in the UDP model to initiate an
4880 * association without sending data.
4884 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4886 * sd: the socket descriptor to have a new association added to.
4888 * nam: the address structure (either struct sockaddr_in or struct
4889 * sockaddr_in6 defined in RFC2553 [7]).
4891 * len: the size of the address.
4893 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4894 int addr_len, int flags)
4900 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4903 /* Validate addr_len before calling common connect/connectx routine. */
4904 af = sctp_get_af_specific(addr->sa_family);
4905 if (af && addr_len >= af->sockaddr_len)
4906 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4912 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4913 int addr_len, int flags)
4915 if (addr_len < sizeof(uaddr->sa_family))
4918 if (uaddr->sa_family == AF_UNSPEC)
4921 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4924 /* FIXME: Write comments. */
4925 static int sctp_disconnect(struct sock *sk, int flags)
4927 return -EOPNOTSUPP; /* STUB */
4930 /* 4.1.4 accept() - TCP Style Syntax
4932 * Applications use accept() call to remove an established SCTP
4933 * association from the accept queue of the endpoint. A new socket
4934 * descriptor will be returned from accept() to represent the newly
4935 * formed association.
4937 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4939 struct sctp_sock *sp;
4940 struct sctp_endpoint *ep;
4941 struct sock *newsk = NULL;
4942 struct sctp_association *asoc;
4951 if (!sctp_style(sk, TCP)) {
4952 error = -EOPNOTSUPP;
4956 if (!sctp_sstate(sk, LISTENING)) {
4961 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4963 error = sctp_wait_for_accept(sk, timeo);
4967 /* We treat the list of associations on the endpoint as the accept
4968 * queue and pick the first association on the list.
4970 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4972 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4978 /* Populate the fields of the newsk from the oldsk and migrate the
4979 * asoc to the newsk.
4981 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4983 sk_common_release(newsk);
4993 /* The SCTP ioctl handler. */
4994 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
5001 * SEQPACKET-style sockets in LISTENING state are valid, for
5002 * SCTP, so only discard TCP-style sockets in LISTENING state.
5004 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5009 struct sk_buff *skb;
5010 unsigned int amount = 0;
5012 skb = skb_peek(&sk->sk_receive_queue);
5015 * We will only return the amount of this packet since
5016 * that is all that will be read.
5020 rc = put_user(amount, (int __user *)arg);
5032 /* This is the function which gets called during socket creation to
5033 * initialized the SCTP-specific portion of the sock.
5034 * The sock structure should already be zero-filled memory.
5036 static int sctp_init_sock(struct sock *sk)
5038 struct net *net = sock_net(sk);
5039 struct sctp_sock *sp;
5041 pr_debug("%s: sk:%p\n", __func__, sk);
5045 /* Initialize the SCTP per socket area. */
5046 switch (sk->sk_type) {
5047 case SOCK_SEQPACKET:
5048 sp->type = SCTP_SOCKET_UDP;
5051 sp->type = SCTP_SOCKET_TCP;
5054 return -ESOCKTNOSUPPORT;
5057 sk->sk_gso_type = SKB_GSO_SCTP;
5059 /* Initialize default send parameters. These parameters can be
5060 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
5062 sp->default_stream = 0;
5063 sp->default_ppid = 0;
5064 sp->default_flags = 0;
5065 sp->default_context = 0;
5066 sp->default_timetolive = 0;
5068 sp->default_rcv_context = 0;
5069 sp->max_burst = net->sctp.max_burst;
5071 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5073 /* Initialize default setup parameters. These parameters
5074 * can be modified with the SCTP_INITMSG socket option or
5075 * overridden by the SCTP_INIT CMSG.
5077 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5078 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
5079 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5080 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5082 /* Initialize default RTO related parameters. These parameters can
5083 * be modified for with the SCTP_RTOINFO socket option.
5085 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5086 sp->rtoinfo.srto_max = net->sctp.rto_max;
5087 sp->rtoinfo.srto_min = net->sctp.rto_min;
5089 /* Initialize default association related parameters. These parameters
5090 * can be modified with the SCTP_ASSOCINFO socket option.
5092 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5093 sp->assocparams.sasoc_number_peer_destinations = 0;
5094 sp->assocparams.sasoc_peer_rwnd = 0;
5095 sp->assocparams.sasoc_local_rwnd = 0;
5096 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5098 /* Initialize default event subscriptions. By default, all the
5103 /* Default Peer Address Parameters. These defaults can
5104 * be modified via SCTP_PEER_ADDR_PARAMS
5106 sp->hbinterval = net->sctp.hb_interval;
5107 sp->pathmaxrxt = net->sctp.max_retrans_path;
5108 sp->pf_retrans = net->sctp.pf_retrans;
5109 sp->ps_retrans = net->sctp.ps_retrans;
5110 sp->pf_expose = net->sctp.pf_expose;
5111 sp->pathmtu = 0; /* allow default discovery */
5112 sp->sackdelay = net->sctp.sack_timeout;
5114 sp->param_flags = SPP_HB_ENABLE |
5116 SPP_SACKDELAY_ENABLE;
5117 sp->default_ss = SCTP_SS_DEFAULT;
5119 /* If enabled no SCTP message fragmentation will be performed.
5120 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5122 sp->disable_fragments = 0;
5124 /* Enable Nagle algorithm by default. */
5127 sp->recvrcvinfo = 0;
5128 sp->recvnxtinfo = 0;
5130 /* Enable by default. */
5133 /* Auto-close idle associations after the configured
5134 * number of seconds. A value of 0 disables this
5135 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5136 * for UDP-style sockets only.
5140 /* User specified fragmentation limit. */
5143 sp->adaptation_ind = 0;
5145 sp->pf = sctp_get_pf_specific(sk->sk_family);
5147 /* Control variables for partial data delivery. */
5148 atomic_set(&sp->pd_mode, 0);
5149 skb_queue_head_init(&sp->pd_lobby);
5150 sp->frag_interleave = 0;
5152 /* Create a per socket endpoint structure. Even if we
5153 * change the data structure relationships, this may still
5154 * be useful for storing pre-connect address information.
5156 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5162 sk->sk_destruct = sctp_destruct_sock;
5164 SCTP_DBG_OBJCNT_INC(sock);
5167 sk_sockets_allocated_inc(sk);
5168 sock_prot_inuse_add(net, sk->sk_prot, 1);
5170 /* Nothing can fail after this block, otherwise
5171 * sctp_destroy_sock() will be called without addr_wq_lock held
5173 if (net->sctp.default_auto_asconf) {
5174 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
5175 list_add_tail(&sp->auto_asconf_list,
5176 &net->sctp.auto_asconf_splist);
5177 sp->do_auto_asconf = 1;
5178 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5180 sp->do_auto_asconf = 0;
5188 /* Cleanup any SCTP per socket resources. Must be called with
5189 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5191 static void sctp_destroy_sock(struct sock *sk)
5193 struct sctp_sock *sp;
5195 pr_debug("%s: sk:%p\n", __func__, sk);
5197 /* Release our hold on the endpoint. */
5199 /* This could happen during socket init, thus we bail out
5200 * early, since the rest of the below is not setup either.
5205 if (sp->do_auto_asconf) {
5206 sp->do_auto_asconf = 0;
5207 list_del(&sp->auto_asconf_list);
5209 sctp_endpoint_free(sp->ep);
5211 sk_sockets_allocated_dec(sk);
5212 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5216 /* Triggered when there are no references on the socket anymore */
5217 static void sctp_destruct_sock(struct sock *sk)
5219 struct sctp_sock *sp = sctp_sk(sk);
5221 /* Free up the HMAC transform. */
5222 crypto_free_shash(sp->hmac);
5224 inet_sock_destruct(sk);
5227 /* API 4.1.7 shutdown() - TCP Style Syntax
5228 * int shutdown(int socket, int how);
5230 * sd - the socket descriptor of the association to be closed.
5231 * how - Specifies the type of shutdown. The values are
5234 * Disables further receive operations. No SCTP
5235 * protocol action is taken.
5237 * Disables further send operations, and initiates
5238 * the SCTP shutdown sequence.
5240 * Disables further send and receive operations
5241 * and initiates the SCTP shutdown sequence.
5243 static void sctp_shutdown(struct sock *sk, int how)
5245 struct net *net = sock_net(sk);
5246 struct sctp_endpoint *ep;
5248 if (!sctp_style(sk, TCP))
5251 ep = sctp_sk(sk)->ep;
5252 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5253 struct sctp_association *asoc;
5255 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5256 asoc = list_entry(ep->asocs.next,
5257 struct sctp_association, asocs);
5258 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5262 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5263 struct sctp_info *info)
5265 struct sctp_transport *prim;
5266 struct list_head *pos;
5269 memset(info, 0, sizeof(*info));
5271 struct sctp_sock *sp = sctp_sk(sk);
5273 info->sctpi_s_autoclose = sp->autoclose;
5274 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5275 info->sctpi_s_pd_point = sp->pd_point;
5276 info->sctpi_s_nodelay = sp->nodelay;
5277 info->sctpi_s_disable_fragments = sp->disable_fragments;
5278 info->sctpi_s_v4mapped = sp->v4mapped;
5279 info->sctpi_s_frag_interleave = sp->frag_interleave;
5280 info->sctpi_s_type = sp->type;
5285 info->sctpi_tag = asoc->c.my_vtag;
5286 info->sctpi_state = asoc->state;
5287 info->sctpi_rwnd = asoc->a_rwnd;
5288 info->sctpi_unackdata = asoc->unack_data;
5289 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5290 info->sctpi_instrms = asoc->stream.incnt;
5291 info->sctpi_outstrms = asoc->stream.outcnt;
5292 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5293 info->sctpi_inqueue++;
5294 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5295 info->sctpi_outqueue++;
5296 info->sctpi_overall_error = asoc->overall_error_count;
5297 info->sctpi_max_burst = asoc->max_burst;
5298 info->sctpi_maxseg = asoc->frag_point;
5299 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5300 info->sctpi_peer_tag = asoc->c.peer_vtag;
5302 mask = asoc->peer.ecn_capable << 1;
5303 mask = (mask | asoc->peer.ipv4_address) << 1;
5304 mask = (mask | asoc->peer.ipv6_address) << 1;
5305 mask = (mask | asoc->peer.hostname_address) << 1;
5306 mask = (mask | asoc->peer.asconf_capable) << 1;
5307 mask = (mask | asoc->peer.prsctp_capable) << 1;
5308 mask = (mask | asoc->peer.auth_capable);
5309 info->sctpi_peer_capable = mask;
5310 mask = asoc->peer.sack_needed << 1;
5311 mask = (mask | asoc->peer.sack_generation) << 1;
5312 mask = (mask | asoc->peer.zero_window_announced);
5313 info->sctpi_peer_sack = mask;
5315 info->sctpi_isacks = asoc->stats.isacks;
5316 info->sctpi_osacks = asoc->stats.osacks;
5317 info->sctpi_opackets = asoc->stats.opackets;
5318 info->sctpi_ipackets = asoc->stats.ipackets;
5319 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5320 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5321 info->sctpi_idupchunks = asoc->stats.idupchunks;
5322 info->sctpi_gapcnt = asoc->stats.gapcnt;
5323 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5324 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5325 info->sctpi_oodchunks = asoc->stats.oodchunks;
5326 info->sctpi_iodchunks = asoc->stats.iodchunks;
5327 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5328 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5330 prim = asoc->peer.primary_path;
5331 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5332 info->sctpi_p_state = prim->state;
5333 info->sctpi_p_cwnd = prim->cwnd;
5334 info->sctpi_p_srtt = prim->srtt;
5335 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5336 info->sctpi_p_hbinterval = prim->hbinterval;
5337 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5338 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5339 info->sctpi_p_ssthresh = prim->ssthresh;
5340 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5341 info->sctpi_p_flight_size = prim->flight_size;
5342 info->sctpi_p_error = prim->error_count;
5346 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5348 /* use callback to avoid exporting the core structure */
5349 void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
5351 rhltable_walk_enter(&sctp_transport_hashtable, iter);
5353 rhashtable_walk_start(iter);
5356 void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
5358 rhashtable_walk_stop(iter);
5359 rhashtable_walk_exit(iter);
5362 struct sctp_transport *sctp_transport_get_next(struct net *net,
5363 struct rhashtable_iter *iter)
5365 struct sctp_transport *t;
5367 t = rhashtable_walk_next(iter);
5368 for (; t; t = rhashtable_walk_next(iter)) {
5370 if (PTR_ERR(t) == -EAGAIN)
5375 if (!sctp_transport_hold(t))
5378 if (net_eq(t->asoc->base.net, net) &&
5379 t->asoc->peer.primary_path == t)
5382 sctp_transport_put(t);
5388 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5389 struct rhashtable_iter *iter,
5392 struct sctp_transport *t;
5395 return SEQ_START_TOKEN;
5397 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5400 sctp_transport_put(t);
5406 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5410 struct sctp_ep_common *epb;
5411 struct sctp_hashbucket *head;
5413 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5415 read_lock_bh(&head->lock);
5416 sctp_for_each_hentry(epb, &head->chain) {
5417 err = cb(sctp_ep(epb), p);
5421 read_unlock_bh(&head->lock);
5426 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5428 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5430 const union sctp_addr *laddr,
5431 const union sctp_addr *paddr, void *p)
5433 struct sctp_transport *transport;
5437 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5442 err = cb(transport, p);
5443 sctp_transport_put(transport);
5447 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5449 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5450 int (*cb_done)(struct sctp_transport *, void *),
5451 struct net *net, int *pos, void *p) {
5452 struct rhashtable_iter hti;
5453 struct sctp_transport *tsp;
5458 sctp_transport_walk_start(&hti);
5460 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5461 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5466 sctp_transport_put(tsp);
5468 sctp_transport_walk_stop(&hti);
5471 if (cb_done && !cb_done(tsp, p)) {
5473 sctp_transport_put(tsp);
5476 sctp_transport_put(tsp);
5481 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5483 /* 7.2.1 Association Status (SCTP_STATUS)
5485 * Applications can retrieve current status information about an
5486 * association, including association state, peer receiver window size,
5487 * number of unacked data chunks, and number of data chunks pending
5488 * receipt. This information is read-only.
5490 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5491 char __user *optval,
5494 struct sctp_status status;
5495 struct sctp_association *asoc = NULL;
5496 struct sctp_transport *transport;
5497 sctp_assoc_t associd;
5500 if (len < sizeof(status)) {
5505 len = sizeof(status);
5506 if (copy_from_user(&status, optval, len)) {
5511 associd = status.sstat_assoc_id;
5512 asoc = sctp_id2assoc(sk, associd);
5518 transport = asoc->peer.primary_path;
5520 status.sstat_assoc_id = sctp_assoc2id(asoc);
5521 status.sstat_state = sctp_assoc_to_state(asoc);
5522 status.sstat_rwnd = asoc->peer.rwnd;
5523 status.sstat_unackdata = asoc->unack_data;
5525 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5526 status.sstat_instrms = asoc->stream.incnt;
5527 status.sstat_outstrms = asoc->stream.outcnt;
5528 status.sstat_fragmentation_point = asoc->frag_point;
5529 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5530 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5531 transport->af_specific->sockaddr_len);
5532 /* Map ipv4 address into v4-mapped-on-v6 address. */
5533 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5534 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5535 status.sstat_primary.spinfo_state = transport->state;
5536 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5537 status.sstat_primary.spinfo_srtt = transport->srtt;
5538 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5539 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5541 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5542 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5544 if (put_user(len, optlen)) {
5549 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5550 __func__, len, status.sstat_state, status.sstat_rwnd,
5551 status.sstat_assoc_id);
5553 if (copy_to_user(optval, &status, len)) {
5563 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5565 * Applications can retrieve information about a specific peer address
5566 * of an association, including its reachability state, congestion
5567 * window, and retransmission timer values. This information is
5570 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5571 char __user *optval,
5574 struct sctp_paddrinfo pinfo;
5575 struct sctp_transport *transport;
5578 if (len < sizeof(pinfo)) {
5583 len = sizeof(pinfo);
5584 if (copy_from_user(&pinfo, optval, len)) {
5589 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5590 pinfo.spinfo_assoc_id);
5596 if (transport->state == SCTP_PF &&
5597 transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
5602 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5603 pinfo.spinfo_state = transport->state;
5604 pinfo.spinfo_cwnd = transport->cwnd;
5605 pinfo.spinfo_srtt = transport->srtt;
5606 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5607 pinfo.spinfo_mtu = transport->pathmtu;
5609 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5610 pinfo.spinfo_state = SCTP_ACTIVE;
5612 if (put_user(len, optlen)) {
5617 if (copy_to_user(optval, &pinfo, len)) {
5626 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5628 * This option is a on/off flag. If enabled no SCTP message
5629 * fragmentation will be performed. Instead if a message being sent
5630 * exceeds the current PMTU size, the message will NOT be sent and
5631 * instead a error will be indicated to the user.
5633 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5634 char __user *optval, int __user *optlen)
5638 if (len < sizeof(int))
5642 val = (sctp_sk(sk)->disable_fragments == 1);
5643 if (put_user(len, optlen))
5645 if (copy_to_user(optval, &val, len))
5650 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5652 * This socket option is used to specify various notifications and
5653 * ancillary data the user wishes to receive.
5655 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5658 struct sctp_event_subscribe subscribe;
5659 __u8 *sn_type = (__u8 *)&subscribe;
5664 if (len > sizeof(struct sctp_event_subscribe))
5665 len = sizeof(struct sctp_event_subscribe);
5666 if (put_user(len, optlen))
5669 for (i = 0; i < len; i++)
5670 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5671 SCTP_SN_TYPE_BASE + i);
5673 if (copy_to_user(optval, &subscribe, len))
5679 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5681 * This socket option is applicable to the UDP-style socket only. When
5682 * set it will cause associations that are idle for more than the
5683 * specified number of seconds to automatically close. An association
5684 * being idle is defined an association that has NOT sent or received
5685 * user data. The special value of '0' indicates that no automatic
5686 * close of any associations should be performed. The option expects an
5687 * integer defining the number of seconds of idle time before an
5688 * association is closed.
5690 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5692 /* Applicable to UDP-style socket only */
5693 if (sctp_style(sk, TCP))
5695 if (len < sizeof(int))
5698 if (put_user(len, optlen))
5700 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5705 /* Helper routine to branch off an association to a new socket. */
5706 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5708 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5709 struct sctp_sock *sp = sctp_sk(sk);
5710 struct socket *sock;
5713 /* Do not peel off from one netns to another one. */
5714 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5720 /* An association cannot be branched off from an already peeled-off
5721 * socket, nor is this supported for tcp style sockets.
5723 if (!sctp_style(sk, UDP))
5726 /* Create a new socket. */
5727 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5731 sctp_copy_sock(sock->sk, sk, asoc);
5733 /* Make peeled-off sockets more like 1-1 accepted sockets.
5734 * Set the daddr and initialize id to something more random and also
5735 * copy over any ip options.
5737 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5738 sp->pf->copy_ip_options(sk, sock->sk);
5740 /* Populate the fields of the newsk from the oldsk and migrate the
5741 * asoc to the newsk.
5743 err = sctp_sock_migrate(sk, sock->sk, asoc,
5744 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5754 EXPORT_SYMBOL(sctp_do_peeloff);
5756 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5757 struct file **newfile, unsigned flags)
5759 struct socket *newsock;
5762 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5766 /* Map the socket to an unused fd that can be returned to the user. */
5767 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5769 sock_release(newsock);
5773 *newfile = sock_alloc_file(newsock, 0, NULL);
5774 if (IS_ERR(*newfile)) {
5775 put_unused_fd(retval);
5776 retval = PTR_ERR(*newfile);
5781 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5784 peeloff->sd = retval;
5786 if (flags & SOCK_NONBLOCK)
5787 (*newfile)->f_flags |= O_NONBLOCK;
5792 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5794 sctp_peeloff_arg_t peeloff;
5795 struct file *newfile = NULL;
5798 if (len < sizeof(sctp_peeloff_arg_t))
5800 len = sizeof(sctp_peeloff_arg_t);
5801 if (copy_from_user(&peeloff, optval, len))
5804 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5808 /* Return the fd mapped to the new socket. */
5809 if (put_user(len, optlen)) {
5811 put_unused_fd(retval);
5815 if (copy_to_user(optval, &peeloff, len)) {
5817 put_unused_fd(retval);
5820 fd_install(retval, newfile);
5825 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5826 char __user *optval, int __user *optlen)
5828 sctp_peeloff_flags_arg_t peeloff;
5829 struct file *newfile = NULL;
5832 if (len < sizeof(sctp_peeloff_flags_arg_t))
5834 len = sizeof(sctp_peeloff_flags_arg_t);
5835 if (copy_from_user(&peeloff, optval, len))
5838 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5839 &newfile, peeloff.flags);
5843 /* Return the fd mapped to the new socket. */
5844 if (put_user(len, optlen)) {
5846 put_unused_fd(retval);
5850 if (copy_to_user(optval, &peeloff, len)) {
5852 put_unused_fd(retval);
5855 fd_install(retval, newfile);
5860 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5862 * Applications can enable or disable heartbeats for any peer address of
5863 * an association, modify an address's heartbeat interval, force a
5864 * heartbeat to be sent immediately, and adjust the address's maximum
5865 * number of retransmissions sent before an address is considered
5866 * unreachable. The following structure is used to access and modify an
5867 * address's parameters:
5869 * struct sctp_paddrparams {
5870 * sctp_assoc_t spp_assoc_id;
5871 * struct sockaddr_storage spp_address;
5872 * uint32_t spp_hbinterval;
5873 * uint16_t spp_pathmaxrxt;
5874 * uint32_t spp_pathmtu;
5875 * uint32_t spp_sackdelay;
5876 * uint32_t spp_flags;
5879 * spp_assoc_id - (one-to-many style socket) This is filled in the
5880 * application, and identifies the association for
5882 * spp_address - This specifies which address is of interest.
5883 * spp_hbinterval - This contains the value of the heartbeat interval,
5884 * in milliseconds. If a value of zero
5885 * is present in this field then no changes are to
5886 * be made to this parameter.
5887 * spp_pathmaxrxt - This contains the maximum number of
5888 * retransmissions before this address shall be
5889 * considered unreachable. If a value of zero
5890 * is present in this field then no changes are to
5891 * be made to this parameter.
5892 * spp_pathmtu - When Path MTU discovery is disabled the value
5893 * specified here will be the "fixed" path mtu.
5894 * Note that if the spp_address field is empty
5895 * then all associations on this address will
5896 * have this fixed path mtu set upon them.
5898 * spp_sackdelay - When delayed sack is enabled, this value specifies
5899 * the number of milliseconds that sacks will be delayed
5900 * for. This value will apply to all addresses of an
5901 * association if the spp_address field is empty. Note
5902 * also, that if delayed sack is enabled and this
5903 * value is set to 0, no change is made to the last
5904 * recorded delayed sack timer value.
5906 * spp_flags - These flags are used to control various features
5907 * on an association. The flag field may contain
5908 * zero or more of the following options.
5910 * SPP_HB_ENABLE - Enable heartbeats on the
5911 * specified address. Note that if the address
5912 * field is empty all addresses for the association
5913 * have heartbeats enabled upon them.
5915 * SPP_HB_DISABLE - Disable heartbeats on the
5916 * speicifed address. Note that if the address
5917 * field is empty all addresses for the association
5918 * will have their heartbeats disabled. Note also
5919 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5920 * mutually exclusive, only one of these two should
5921 * be specified. Enabling both fields will have
5922 * undetermined results.
5924 * SPP_HB_DEMAND - Request a user initiated heartbeat
5925 * to be made immediately.
5927 * SPP_PMTUD_ENABLE - This field will enable PMTU
5928 * discovery upon the specified address. Note that
5929 * if the address feild is empty then all addresses
5930 * on the association are effected.
5932 * SPP_PMTUD_DISABLE - This field will disable PMTU
5933 * discovery upon the specified address. Note that
5934 * if the address feild is empty then all addresses
5935 * on the association are effected. Not also that
5936 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5937 * exclusive. Enabling both will have undetermined
5940 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5941 * on delayed sack. The time specified in spp_sackdelay
5942 * is used to specify the sack delay for this address. Note
5943 * that if spp_address is empty then all addresses will
5944 * enable delayed sack and take on the sack delay
5945 * value specified in spp_sackdelay.
5946 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5947 * off delayed sack. If the spp_address field is blank then
5948 * delayed sack is disabled for the entire association. Note
5949 * also that this field is mutually exclusive to
5950 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5953 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5954 * setting of the IPV6 flow label value. The value is
5955 * contained in the spp_ipv6_flowlabel field.
5956 * Upon retrieval, this flag will be set to indicate that
5957 * the spp_ipv6_flowlabel field has a valid value returned.
5958 * If a specific destination address is set (in the
5959 * spp_address field), then the value returned is that of
5960 * the address. If just an association is specified (and
5961 * no address), then the association's default flow label
5962 * is returned. If neither an association nor a destination
5963 * is specified, then the socket's default flow label is
5964 * returned. For non-IPv6 sockets, this flag will be left
5967 * SPP_DSCP: Setting this flag enables the setting of the
5968 * Differentiated Services Code Point (DSCP) value
5969 * associated with either the association or a specific
5970 * address. The value is obtained in the spp_dscp field.
5971 * Upon retrieval, this flag will be set to indicate that
5972 * the spp_dscp field has a valid value returned. If a
5973 * specific destination address is set when called (in the
5974 * spp_address field), then that specific destination
5975 * address's DSCP value is returned. If just an association
5976 * is specified, then the association's default DSCP is
5977 * returned. If neither an association nor a destination is
5978 * specified, then the socket's default DSCP is returned.
5980 * spp_ipv6_flowlabel
5981 * - This field is used in conjunction with the
5982 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5983 * The 20 least significant bits are used for the flow
5984 * label. This setting has precedence over any IPv6-layer
5987 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5988 * and contains the DSCP. The 6 most significant bits are
5989 * used for the DSCP. This setting has precedence over any
5990 * IPv4- or IPv6- layer setting.
5992 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5993 char __user *optval, int __user *optlen)
5995 struct sctp_paddrparams params;
5996 struct sctp_transport *trans = NULL;
5997 struct sctp_association *asoc = NULL;
5998 struct sctp_sock *sp = sctp_sk(sk);
6000 if (len >= sizeof(params))
6001 len = sizeof(params);
6002 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
6003 spp_ipv6_flowlabel), 4))
6004 len = ALIGN(offsetof(struct sctp_paddrparams,
6005 spp_ipv6_flowlabel), 4);
6009 if (copy_from_user(¶ms, optval, len))
6012 /* If an address other than INADDR_ANY is specified, and
6013 * no transport is found, then the request is invalid.
6015 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
6016 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
6017 params.spp_assoc_id);
6019 pr_debug("%s: failed no transport\n", __func__);
6024 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
6025 * socket is a one to many style socket, and an association
6026 * was not found, then the id was invalid.
6028 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
6029 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
6030 sctp_style(sk, UDP)) {
6031 pr_debug("%s: failed no association\n", __func__);
6036 /* Fetch transport values. */
6037 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
6038 params.spp_pathmtu = trans->pathmtu;
6039 params.spp_pathmaxrxt = trans->pathmaxrxt;
6040 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
6042 /*draft-11 doesn't say what to return in spp_flags*/
6043 params.spp_flags = trans->param_flags;
6044 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6045 params.spp_ipv6_flowlabel = trans->flowlabel &
6046 SCTP_FLOWLABEL_VAL_MASK;
6047 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6049 if (trans->dscp & SCTP_DSCP_SET_MASK) {
6050 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
6051 params.spp_flags |= SPP_DSCP;
6054 /* Fetch association values. */
6055 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
6056 params.spp_pathmtu = asoc->pathmtu;
6057 params.spp_pathmaxrxt = asoc->pathmaxrxt;
6058 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
6060 /*draft-11 doesn't say what to return in spp_flags*/
6061 params.spp_flags = asoc->param_flags;
6062 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6063 params.spp_ipv6_flowlabel = asoc->flowlabel &
6064 SCTP_FLOWLABEL_VAL_MASK;
6065 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6067 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
6068 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6069 params.spp_flags |= SPP_DSCP;
6072 /* Fetch socket values. */
6073 params.spp_hbinterval = sp->hbinterval;
6074 params.spp_pathmtu = sp->pathmtu;
6075 params.spp_sackdelay = sp->sackdelay;
6076 params.spp_pathmaxrxt = sp->pathmaxrxt;
6078 /*draft-11 doesn't say what to return in spp_flags*/
6079 params.spp_flags = sp->param_flags;
6080 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6081 params.spp_ipv6_flowlabel = sp->flowlabel &
6082 SCTP_FLOWLABEL_VAL_MASK;
6083 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6085 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6086 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6087 params.spp_flags |= SPP_DSCP;
6091 if (copy_to_user(optval, ¶ms, len))
6094 if (put_user(len, optlen))
6101 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6103 * This option will effect the way delayed acks are performed. This
6104 * option allows you to get or set the delayed ack time, in
6105 * milliseconds. It also allows changing the delayed ack frequency.
6106 * Changing the frequency to 1 disables the delayed sack algorithm. If
6107 * the assoc_id is 0, then this sets or gets the endpoints default
6108 * values. If the assoc_id field is non-zero, then the set or get
6109 * effects the specified association for the one to many model (the
6110 * assoc_id field is ignored by the one to one model). Note that if
6111 * sack_delay or sack_freq are 0 when setting this option, then the
6112 * current values will remain unchanged.
6114 * struct sctp_sack_info {
6115 * sctp_assoc_t sack_assoc_id;
6116 * uint32_t sack_delay;
6117 * uint32_t sack_freq;
6120 * sack_assoc_id - This parameter, indicates which association the user
6121 * is performing an action upon. Note that if this field's value is
6122 * zero then the endpoints default value is changed (effecting future
6123 * associations only).
6125 * sack_delay - This parameter contains the number of milliseconds that
6126 * the user is requesting the delayed ACK timer be set to. Note that
6127 * this value is defined in the standard to be between 200 and 500
6130 * sack_freq - This parameter contains the number of packets that must
6131 * be received before a sack is sent without waiting for the delay
6132 * timer to expire. The default value for this is 2, setting this
6133 * value to 1 will disable the delayed sack algorithm.
6135 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6136 char __user *optval,
6139 struct sctp_sack_info params;
6140 struct sctp_association *asoc = NULL;
6141 struct sctp_sock *sp = sctp_sk(sk);
6143 if (len >= sizeof(struct sctp_sack_info)) {
6144 len = sizeof(struct sctp_sack_info);
6146 if (copy_from_user(¶ms, optval, len))
6148 } else if (len == sizeof(struct sctp_assoc_value)) {
6149 pr_warn_ratelimited(DEPRECATED
6151 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6152 "Use struct sctp_sack_info instead\n",
6153 current->comm, task_pid_nr(current));
6154 if (copy_from_user(¶ms, optval, len))
6159 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6160 * socket is a one to many style socket, and an association
6161 * was not found, then the id was invalid.
6163 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6164 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6165 sctp_style(sk, UDP))
6169 /* Fetch association values. */
6170 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6171 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6172 params.sack_freq = asoc->sackfreq;
6175 params.sack_delay = 0;
6176 params.sack_freq = 1;
6179 /* Fetch socket values. */
6180 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6181 params.sack_delay = sp->sackdelay;
6182 params.sack_freq = sp->sackfreq;
6184 params.sack_delay = 0;
6185 params.sack_freq = 1;
6189 if (copy_to_user(optval, ¶ms, len))
6192 if (put_user(len, optlen))
6198 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6200 * Applications can specify protocol parameters for the default association
6201 * initialization. The option name argument to setsockopt() and getsockopt()
6204 * Setting initialization parameters is effective only on an unconnected
6205 * socket (for UDP-style sockets only future associations are effected
6206 * by the change). With TCP-style sockets, this option is inherited by
6207 * sockets derived from a listener socket.
6209 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6211 if (len < sizeof(struct sctp_initmsg))
6213 len = sizeof(struct sctp_initmsg);
6214 if (put_user(len, optlen))
6216 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6222 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6223 char __user *optval, int __user *optlen)
6225 struct sctp_association *asoc;
6227 struct sctp_getaddrs getaddrs;
6228 struct sctp_transport *from;
6230 union sctp_addr temp;
6231 struct sctp_sock *sp = sctp_sk(sk);
6236 if (len < sizeof(struct sctp_getaddrs))
6239 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6242 /* For UDP-style sockets, id specifies the association to query. */
6243 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6247 to = optval + offsetof(struct sctp_getaddrs, addrs);
6248 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6250 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6252 memcpy(&temp, &from->ipaddr, sizeof(temp));
6253 addrlen = sctp_get_pf_specific(sk->sk_family)
6254 ->addr_to_user(sp, &temp);
6255 if (space_left < addrlen)
6257 if (copy_to_user(to, &temp, addrlen))
6261 space_left -= addrlen;
6264 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6266 bytes_copied = ((char __user *)to) - optval;
6267 if (put_user(bytes_copied, optlen))
6273 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6274 size_t space_left, int *bytes_copied)
6276 struct sctp_sockaddr_entry *addr;
6277 union sctp_addr temp;
6280 struct net *net = sock_net(sk);
6283 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6287 if ((PF_INET == sk->sk_family) &&
6288 (AF_INET6 == addr->a.sa.sa_family))
6290 if ((PF_INET6 == sk->sk_family) &&
6291 inet_v6_ipv6only(sk) &&
6292 (AF_INET == addr->a.sa.sa_family))
6294 memcpy(&temp, &addr->a, sizeof(temp));
6295 if (!temp.v4.sin_port)
6296 temp.v4.sin_port = htons(port);
6298 addrlen = sctp_get_pf_specific(sk->sk_family)
6299 ->addr_to_user(sctp_sk(sk), &temp);
6301 if (space_left < addrlen) {
6305 memcpy(to, &temp, addrlen);
6309 space_left -= addrlen;
6310 *bytes_copied += addrlen;
6318 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6319 char __user *optval, int __user *optlen)
6321 struct sctp_bind_addr *bp;
6322 struct sctp_association *asoc;
6324 struct sctp_getaddrs getaddrs;
6325 struct sctp_sockaddr_entry *addr;
6327 union sctp_addr temp;
6328 struct sctp_sock *sp = sctp_sk(sk);
6332 int bytes_copied = 0;
6336 if (len < sizeof(struct sctp_getaddrs))
6339 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6343 * For UDP-style sockets, id specifies the association to query.
6344 * If the id field is set to the value '0' then the locally bound
6345 * addresses are returned without regard to any particular
6348 if (0 == getaddrs.assoc_id) {
6349 bp = &sctp_sk(sk)->ep->base.bind_addr;
6351 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6354 bp = &asoc->base.bind_addr;
6357 to = optval + offsetof(struct sctp_getaddrs, addrs);
6358 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6360 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6364 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6365 * addresses from the global local address list.
6367 if (sctp_list_single_entry(&bp->address_list)) {
6368 addr = list_entry(bp->address_list.next,
6369 struct sctp_sockaddr_entry, list);
6370 if (sctp_is_any(sk, &addr->a)) {
6371 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6372 space_left, &bytes_copied);
6382 /* Protection on the bound address list is not needed since
6383 * in the socket option context we hold a socket lock and
6384 * thus the bound address list can't change.
6386 list_for_each_entry(addr, &bp->address_list, list) {
6387 memcpy(&temp, &addr->a, sizeof(temp));
6388 addrlen = sctp_get_pf_specific(sk->sk_family)
6389 ->addr_to_user(sp, &temp);
6390 if (space_left < addrlen) {
6391 err = -ENOMEM; /*fixme: right error?*/
6394 memcpy(buf, &temp, addrlen);
6396 bytes_copied += addrlen;
6398 space_left -= addrlen;
6402 if (copy_to_user(to, addrs, bytes_copied)) {
6406 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6410 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6411 * but we can't change it anymore.
6413 if (put_user(bytes_copied, optlen))
6420 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6422 * Requests that the local SCTP stack use the enclosed peer address as
6423 * the association primary. The enclosed address must be one of the
6424 * association peer's addresses.
6426 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6427 char __user *optval, int __user *optlen)
6429 struct sctp_prim prim;
6430 struct sctp_association *asoc;
6431 struct sctp_sock *sp = sctp_sk(sk);
6433 if (len < sizeof(struct sctp_prim))
6436 len = sizeof(struct sctp_prim);
6438 if (copy_from_user(&prim, optval, len))
6441 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6445 if (!asoc->peer.primary_path)
6448 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6449 asoc->peer.primary_path->af_specific->sockaddr_len);
6451 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6452 (union sctp_addr *)&prim.ssp_addr);
6454 if (put_user(len, optlen))
6456 if (copy_to_user(optval, &prim, len))
6463 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6465 * Requests that the local endpoint set the specified Adaptation Layer
6466 * Indication parameter for all future INIT and INIT-ACK exchanges.
6468 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6469 char __user *optval, int __user *optlen)
6471 struct sctp_setadaptation adaptation;
6473 if (len < sizeof(struct sctp_setadaptation))
6476 len = sizeof(struct sctp_setadaptation);
6478 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6480 if (put_user(len, optlen))
6482 if (copy_to_user(optval, &adaptation, len))
6490 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6492 * Applications that wish to use the sendto() system call may wish to
6493 * specify a default set of parameters that would normally be supplied
6494 * through the inclusion of ancillary data. This socket option allows
6495 * such an application to set the default sctp_sndrcvinfo structure.
6498 * The application that wishes to use this socket option simply passes
6499 * in to this call the sctp_sndrcvinfo structure defined in Section
6500 * 5.2.2) The input parameters accepted by this call include
6501 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6502 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6503 * to this call if the caller is using the UDP model.
6505 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6507 static int sctp_getsockopt_default_send_param(struct sock *sk,
6508 int len, char __user *optval,
6511 struct sctp_sock *sp = sctp_sk(sk);
6512 struct sctp_association *asoc;
6513 struct sctp_sndrcvinfo info;
6515 if (len < sizeof(info))
6520 if (copy_from_user(&info, optval, len))
6523 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6524 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6525 sctp_style(sk, UDP))
6529 info.sinfo_stream = asoc->default_stream;
6530 info.sinfo_flags = asoc->default_flags;
6531 info.sinfo_ppid = asoc->default_ppid;
6532 info.sinfo_context = asoc->default_context;
6533 info.sinfo_timetolive = asoc->default_timetolive;
6535 info.sinfo_stream = sp->default_stream;
6536 info.sinfo_flags = sp->default_flags;
6537 info.sinfo_ppid = sp->default_ppid;
6538 info.sinfo_context = sp->default_context;
6539 info.sinfo_timetolive = sp->default_timetolive;
6542 if (put_user(len, optlen))
6544 if (copy_to_user(optval, &info, len))
6550 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6551 * (SCTP_DEFAULT_SNDINFO)
6553 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6554 char __user *optval,
6557 struct sctp_sock *sp = sctp_sk(sk);
6558 struct sctp_association *asoc;
6559 struct sctp_sndinfo info;
6561 if (len < sizeof(info))
6566 if (copy_from_user(&info, optval, len))
6569 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6570 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6571 sctp_style(sk, UDP))
6575 info.snd_sid = asoc->default_stream;
6576 info.snd_flags = asoc->default_flags;
6577 info.snd_ppid = asoc->default_ppid;
6578 info.snd_context = asoc->default_context;
6580 info.snd_sid = sp->default_stream;
6581 info.snd_flags = sp->default_flags;
6582 info.snd_ppid = sp->default_ppid;
6583 info.snd_context = sp->default_context;
6586 if (put_user(len, optlen))
6588 if (copy_to_user(optval, &info, len))
6596 * 7.1.5 SCTP_NODELAY
6598 * Turn on/off any Nagle-like algorithm. This means that packets are
6599 * generally sent as soon as possible and no unnecessary delays are
6600 * introduced, at the cost of more packets in the network. Expects an
6601 * integer boolean flag.
6604 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6605 char __user *optval, int __user *optlen)
6609 if (len < sizeof(int))
6613 val = (sctp_sk(sk)->nodelay == 1);
6614 if (put_user(len, optlen))
6616 if (copy_to_user(optval, &val, len))
6623 * 7.1.1 SCTP_RTOINFO
6625 * The protocol parameters used to initialize and bound retransmission
6626 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6627 * and modify these parameters.
6628 * All parameters are time values, in milliseconds. A value of 0, when
6629 * modifying the parameters, indicates that the current value should not
6633 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6634 char __user *optval,
6635 int __user *optlen) {
6636 struct sctp_rtoinfo rtoinfo;
6637 struct sctp_association *asoc;
6639 if (len < sizeof (struct sctp_rtoinfo))
6642 len = sizeof(struct sctp_rtoinfo);
6644 if (copy_from_user(&rtoinfo, optval, len))
6647 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6649 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6650 sctp_style(sk, UDP))
6653 /* Values corresponding to the specific association. */
6655 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6656 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6657 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6659 /* Values corresponding to the endpoint. */
6660 struct sctp_sock *sp = sctp_sk(sk);
6662 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6663 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6664 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6667 if (put_user(len, optlen))
6670 if (copy_to_user(optval, &rtoinfo, len))
6678 * 7.1.2 SCTP_ASSOCINFO
6680 * This option is used to tune the maximum retransmission attempts
6681 * of the association.
6682 * Returns an error if the new association retransmission value is
6683 * greater than the sum of the retransmission value of the peer.
6684 * See [SCTP] for more information.
6687 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6688 char __user *optval,
6692 struct sctp_assocparams assocparams;
6693 struct sctp_association *asoc;
6694 struct list_head *pos;
6697 if (len < sizeof (struct sctp_assocparams))
6700 len = sizeof(struct sctp_assocparams);
6702 if (copy_from_user(&assocparams, optval, len))
6705 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6707 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6708 sctp_style(sk, UDP))
6711 /* Values correspoinding to the specific association */
6713 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6714 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6715 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6716 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6718 list_for_each(pos, &asoc->peer.transport_addr_list) {
6722 assocparams.sasoc_number_peer_destinations = cnt;
6724 /* Values corresponding to the endpoint */
6725 struct sctp_sock *sp = sctp_sk(sk);
6727 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6728 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6729 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6730 assocparams.sasoc_cookie_life =
6731 sp->assocparams.sasoc_cookie_life;
6732 assocparams.sasoc_number_peer_destinations =
6734 sasoc_number_peer_destinations;
6737 if (put_user(len, optlen))
6740 if (copy_to_user(optval, &assocparams, len))
6747 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6749 * This socket option is a boolean flag which turns on or off mapped V4
6750 * addresses. If this option is turned on and the socket is type
6751 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6752 * If this option is turned off, then no mapping will be done of V4
6753 * addresses and a user will receive both PF_INET6 and PF_INET type
6754 * addresses on the socket.
6756 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6757 char __user *optval, int __user *optlen)
6760 struct sctp_sock *sp = sctp_sk(sk);
6762 if (len < sizeof(int))
6767 if (put_user(len, optlen))
6769 if (copy_to_user(optval, &val, len))
6776 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6777 * (chapter and verse is quoted at sctp_setsockopt_context())
6779 static int sctp_getsockopt_context(struct sock *sk, int len,
6780 char __user *optval, int __user *optlen)
6782 struct sctp_assoc_value params;
6783 struct sctp_association *asoc;
6785 if (len < sizeof(struct sctp_assoc_value))
6788 len = sizeof(struct sctp_assoc_value);
6790 if (copy_from_user(¶ms, optval, len))
6793 asoc = sctp_id2assoc(sk, params.assoc_id);
6794 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6795 sctp_style(sk, UDP))
6798 params.assoc_value = asoc ? asoc->default_rcv_context
6799 : sctp_sk(sk)->default_rcv_context;
6801 if (put_user(len, optlen))
6803 if (copy_to_user(optval, ¶ms, len))
6810 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6811 * This option will get or set the maximum size to put in any outgoing
6812 * SCTP DATA chunk. If a message is larger than this size it will be
6813 * fragmented by SCTP into the specified size. Note that the underlying
6814 * SCTP implementation may fragment into smaller sized chunks when the
6815 * PMTU of the underlying association is smaller than the value set by
6816 * the user. The default value for this option is '0' which indicates
6817 * the user is NOT limiting fragmentation and only the PMTU will effect
6818 * SCTP's choice of DATA chunk size. Note also that values set larger
6819 * than the maximum size of an IP datagram will effectively let SCTP
6820 * control fragmentation (i.e. the same as setting this option to 0).
6822 * The following structure is used to access and modify this parameter:
6824 * struct sctp_assoc_value {
6825 * sctp_assoc_t assoc_id;
6826 * uint32_t assoc_value;
6829 * assoc_id: This parameter is ignored for one-to-one style sockets.
6830 * For one-to-many style sockets this parameter indicates which
6831 * association the user is performing an action upon. Note that if
6832 * this field's value is zero then the endpoints default value is
6833 * changed (effecting future associations only).
6834 * assoc_value: This parameter specifies the maximum size in bytes.
6836 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6837 char __user *optval, int __user *optlen)
6839 struct sctp_assoc_value params;
6840 struct sctp_association *asoc;
6842 if (len == sizeof(int)) {
6843 pr_warn_ratelimited(DEPRECATED
6845 "Use of int in maxseg socket option.\n"
6846 "Use struct sctp_assoc_value instead\n",
6847 current->comm, task_pid_nr(current));
6848 params.assoc_id = SCTP_FUTURE_ASSOC;
6849 } else if (len >= sizeof(struct sctp_assoc_value)) {
6850 len = sizeof(struct sctp_assoc_value);
6851 if (copy_from_user(¶ms, optval, len))
6856 asoc = sctp_id2assoc(sk, params.assoc_id);
6857 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6858 sctp_style(sk, UDP))
6862 params.assoc_value = asoc->frag_point;
6864 params.assoc_value = sctp_sk(sk)->user_frag;
6866 if (put_user(len, optlen))
6868 if (len == sizeof(int)) {
6869 if (copy_to_user(optval, ¶ms.assoc_value, len))
6872 if (copy_to_user(optval, ¶ms, len))
6880 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6881 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6883 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6884 char __user *optval, int __user *optlen)
6888 if (len < sizeof(int))
6893 val = sctp_sk(sk)->frag_interleave;
6894 if (put_user(len, optlen))
6896 if (copy_to_user(optval, &val, len))
6903 * 7.1.25. Set or Get the sctp partial delivery point
6904 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6906 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6907 char __user *optval,
6912 if (len < sizeof(u32))
6917 val = sctp_sk(sk)->pd_point;
6918 if (put_user(len, optlen))
6920 if (copy_to_user(optval, &val, len))
6927 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6928 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6930 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6931 char __user *optval,
6934 struct sctp_assoc_value params;
6935 struct sctp_association *asoc;
6937 if (len == sizeof(int)) {
6938 pr_warn_ratelimited(DEPRECATED
6940 "Use of int in max_burst socket option.\n"
6941 "Use struct sctp_assoc_value instead\n",
6942 current->comm, task_pid_nr(current));
6943 params.assoc_id = SCTP_FUTURE_ASSOC;
6944 } else if (len >= sizeof(struct sctp_assoc_value)) {
6945 len = sizeof(struct sctp_assoc_value);
6946 if (copy_from_user(¶ms, optval, len))
6951 asoc = sctp_id2assoc(sk, params.assoc_id);
6952 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6953 sctp_style(sk, UDP))
6956 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6958 if (len == sizeof(int)) {
6959 if (copy_to_user(optval, ¶ms.assoc_value, len))
6962 if (copy_to_user(optval, ¶ms, len))
6970 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6971 char __user *optval, int __user *optlen)
6973 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6974 struct sctp_hmacalgo __user *p = (void __user *)optval;
6975 struct sctp_hmac_algo_param *hmacs;
6980 if (!ep->auth_enable)
6983 hmacs = ep->auth_hmacs_list;
6984 data_len = ntohs(hmacs->param_hdr.length) -
6985 sizeof(struct sctp_paramhdr);
6987 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6990 len = sizeof(struct sctp_hmacalgo) + data_len;
6991 num_idents = data_len / sizeof(u16);
6993 if (put_user(len, optlen))
6995 if (put_user(num_idents, &p->shmac_num_idents))
6997 for (i = 0; i < num_idents; i++) {
6998 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
7000 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
7006 static int sctp_getsockopt_active_key(struct sock *sk, int len,
7007 char __user *optval, int __user *optlen)
7009 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7010 struct sctp_authkeyid val;
7011 struct sctp_association *asoc;
7013 if (len < sizeof(struct sctp_authkeyid))
7016 len = sizeof(struct sctp_authkeyid);
7017 if (copy_from_user(&val, optval, len))
7020 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
7021 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
7025 if (!asoc->peer.auth_capable)
7027 val.scact_keynumber = asoc->active_key_id;
7029 if (!ep->auth_enable)
7031 val.scact_keynumber = ep->active_key_id;
7034 if (put_user(len, optlen))
7036 if (copy_to_user(optval, &val, len))
7042 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
7043 char __user *optval, int __user *optlen)
7045 struct sctp_authchunks __user *p = (void __user *)optval;
7046 struct sctp_authchunks val;
7047 struct sctp_association *asoc;
7048 struct sctp_chunks_param *ch;
7052 if (len < sizeof(struct sctp_authchunks))
7055 if (copy_from_user(&val, optval, sizeof(val)))
7058 to = p->gauth_chunks;
7059 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7063 if (!asoc->peer.auth_capable)
7066 ch = asoc->peer.peer_chunks;
7070 /* See if the user provided enough room for all the data */
7071 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7072 if (len < num_chunks)
7075 if (copy_to_user(to, ch->chunks, num_chunks))
7078 len = sizeof(struct sctp_authchunks) + num_chunks;
7079 if (put_user(len, optlen))
7081 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7086 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
7087 char __user *optval, int __user *optlen)
7089 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7090 struct sctp_authchunks __user *p = (void __user *)optval;
7091 struct sctp_authchunks val;
7092 struct sctp_association *asoc;
7093 struct sctp_chunks_param *ch;
7097 if (len < sizeof(struct sctp_authchunks))
7100 if (copy_from_user(&val, optval, sizeof(val)))
7103 to = p->gauth_chunks;
7104 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7105 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7106 sctp_style(sk, UDP))
7110 if (!asoc->peer.auth_capable)
7112 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
7114 if (!ep->auth_enable)
7116 ch = ep->auth_chunk_list;
7121 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7122 if (len < sizeof(struct sctp_authchunks) + num_chunks)
7125 if (copy_to_user(to, ch->chunks, num_chunks))
7128 len = sizeof(struct sctp_authchunks) + num_chunks;
7129 if (put_user(len, optlen))
7131 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7138 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7139 * This option gets the current number of associations that are attached
7140 * to a one-to-many style socket. The option value is an uint32_t.
7142 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7143 char __user *optval, int __user *optlen)
7145 struct sctp_sock *sp = sctp_sk(sk);
7146 struct sctp_association *asoc;
7149 if (sctp_style(sk, TCP))
7152 if (len < sizeof(u32))
7157 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7161 if (put_user(len, optlen))
7163 if (copy_to_user(optval, &val, len))
7170 * 8.1.23 SCTP_AUTO_ASCONF
7171 * See the corresponding setsockopt entry as description
7173 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7174 char __user *optval, int __user *optlen)
7178 if (len < sizeof(int))
7182 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7184 if (put_user(len, optlen))
7186 if (copy_to_user(optval, &val, len))
7192 * 8.2.6. Get the Current Identifiers of Associations
7193 * (SCTP_GET_ASSOC_ID_LIST)
7195 * This option gets the current list of SCTP association identifiers of
7196 * the SCTP associations handled by a one-to-many style socket.
7198 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7199 char __user *optval, int __user *optlen)
7201 struct sctp_sock *sp = sctp_sk(sk);
7202 struct sctp_association *asoc;
7203 struct sctp_assoc_ids *ids;
7206 if (sctp_style(sk, TCP))
7209 if (len < sizeof(struct sctp_assoc_ids))
7212 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7216 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7219 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7221 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7225 ids->gaids_number_of_ids = num;
7227 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7228 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7231 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7241 * SCTP_PEER_ADDR_THLDS
7243 * This option allows us to fetch the partially failed threshold for one or all
7244 * transports in an association. See Section 6.1 of:
7245 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7247 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7248 char __user *optval, int len,
7249 int __user *optlen, bool v2)
7251 struct sctp_paddrthlds_v2 val;
7252 struct sctp_transport *trans;
7253 struct sctp_association *asoc;
7256 min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
7260 if (copy_from_user(&val, optval, len))
7263 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7264 trans = sctp_addr_id2transport(sk, &val.spt_address,
7269 val.spt_pathmaxrxt = trans->pathmaxrxt;
7270 val.spt_pathpfthld = trans->pf_retrans;
7271 val.spt_pathcpthld = trans->ps_retrans;
7276 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7277 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7278 sctp_style(sk, UDP))
7282 val.spt_pathpfthld = asoc->pf_retrans;
7283 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7284 val.spt_pathcpthld = asoc->ps_retrans;
7286 struct sctp_sock *sp = sctp_sk(sk);
7288 val.spt_pathpfthld = sp->pf_retrans;
7289 val.spt_pathmaxrxt = sp->pathmaxrxt;
7290 val.spt_pathcpthld = sp->ps_retrans;
7294 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7301 * SCTP_GET_ASSOC_STATS
7303 * This option retrieves local per endpoint statistics. It is modeled
7304 * after OpenSolaris' implementation
7306 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7307 char __user *optval,
7310 struct sctp_assoc_stats sas;
7311 struct sctp_association *asoc = NULL;
7313 /* User must provide at least the assoc id */
7314 if (len < sizeof(sctp_assoc_t))
7317 /* Allow the struct to grow and fill in as much as possible */
7318 len = min_t(size_t, len, sizeof(sas));
7320 if (copy_from_user(&sas, optval, len))
7323 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7327 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7328 sas.sas_gapcnt = asoc->stats.gapcnt;
7329 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7330 sas.sas_osacks = asoc->stats.osacks;
7331 sas.sas_isacks = asoc->stats.isacks;
7332 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7333 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7334 sas.sas_oodchunks = asoc->stats.oodchunks;
7335 sas.sas_iodchunks = asoc->stats.iodchunks;
7336 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7337 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7338 sas.sas_idupchunks = asoc->stats.idupchunks;
7339 sas.sas_opackets = asoc->stats.opackets;
7340 sas.sas_ipackets = asoc->stats.ipackets;
7342 /* New high max rto observed, will return 0 if not a single
7343 * RTO update took place. obs_rto_ipaddr will be bogus
7346 sas.sas_maxrto = asoc->stats.max_obs_rto;
7347 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7348 sizeof(struct sockaddr_storage));
7350 /* Mark beginning of a new observation period */
7351 asoc->stats.max_obs_rto = asoc->rto_min;
7353 if (put_user(len, optlen))
7356 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7358 if (copy_to_user(optval, &sas, len))
7364 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7365 char __user *optval,
7370 if (len < sizeof(int))
7374 if (sctp_sk(sk)->recvrcvinfo)
7376 if (put_user(len, optlen))
7378 if (copy_to_user(optval, &val, len))
7384 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7385 char __user *optval,
7390 if (len < sizeof(int))
7394 if (sctp_sk(sk)->recvnxtinfo)
7396 if (put_user(len, optlen))
7398 if (copy_to_user(optval, &val, len))
7404 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7405 char __user *optval,
7408 struct sctp_assoc_value params;
7409 struct sctp_association *asoc;
7410 int retval = -EFAULT;
7412 if (len < sizeof(params)) {
7417 len = sizeof(params);
7418 if (copy_from_user(¶ms, optval, len))
7421 asoc = sctp_id2assoc(sk, params.assoc_id);
7422 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7423 sctp_style(sk, UDP)) {
7428 params.assoc_value = asoc ? asoc->peer.prsctp_capable
7429 : sctp_sk(sk)->ep->prsctp_enable;
7431 if (put_user(len, optlen))
7434 if (copy_to_user(optval, ¶ms, len))
7443 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7444 char __user *optval,
7447 struct sctp_default_prinfo info;
7448 struct sctp_association *asoc;
7449 int retval = -EFAULT;
7451 if (len < sizeof(info)) {
7457 if (copy_from_user(&info, optval, len))
7460 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7461 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7462 sctp_style(sk, UDP)) {
7468 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7469 info.pr_value = asoc->default_timetolive;
7471 struct sctp_sock *sp = sctp_sk(sk);
7473 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7474 info.pr_value = sp->default_timetolive;
7477 if (put_user(len, optlen))
7480 if (copy_to_user(optval, &info, len))
7489 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7490 char __user *optval,
7493 struct sctp_prstatus params;
7494 struct sctp_association *asoc;
7496 int retval = -EINVAL;
7498 if (len < sizeof(params))
7501 len = sizeof(params);
7502 if (copy_from_user(¶ms, optval, len)) {
7507 policy = params.sprstat_policy;
7508 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7509 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7512 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7516 if (policy == SCTP_PR_SCTP_ALL) {
7517 params.sprstat_abandoned_unsent = 0;
7518 params.sprstat_abandoned_sent = 0;
7519 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7520 params.sprstat_abandoned_unsent +=
7521 asoc->abandoned_unsent[policy];
7522 params.sprstat_abandoned_sent +=
7523 asoc->abandoned_sent[policy];
7526 params.sprstat_abandoned_unsent =
7527 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7528 params.sprstat_abandoned_sent =
7529 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7532 if (put_user(len, optlen)) {
7537 if (copy_to_user(optval, ¶ms, len)) {
7548 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7549 char __user *optval,
7552 struct sctp_stream_out_ext *streamoute;
7553 struct sctp_association *asoc;
7554 struct sctp_prstatus params;
7555 int retval = -EINVAL;
7558 if (len < sizeof(params))
7561 len = sizeof(params);
7562 if (copy_from_user(¶ms, optval, len)) {
7567 policy = params.sprstat_policy;
7568 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7569 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7572 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7573 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7576 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7578 /* Not allocated yet, means all stats are 0 */
7579 params.sprstat_abandoned_unsent = 0;
7580 params.sprstat_abandoned_sent = 0;
7585 if (policy == SCTP_PR_SCTP_ALL) {
7586 params.sprstat_abandoned_unsent = 0;
7587 params.sprstat_abandoned_sent = 0;
7588 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7589 params.sprstat_abandoned_unsent +=
7590 streamoute->abandoned_unsent[policy];
7591 params.sprstat_abandoned_sent +=
7592 streamoute->abandoned_sent[policy];
7595 params.sprstat_abandoned_unsent =
7596 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7597 params.sprstat_abandoned_sent =
7598 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7601 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7612 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7613 char __user *optval,
7616 struct sctp_assoc_value params;
7617 struct sctp_association *asoc;
7618 int retval = -EFAULT;
7620 if (len < sizeof(params)) {
7625 len = sizeof(params);
7626 if (copy_from_user(¶ms, optval, len))
7629 asoc = sctp_id2assoc(sk, params.assoc_id);
7630 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7631 sctp_style(sk, UDP)) {
7636 params.assoc_value = asoc ? asoc->peer.reconf_capable
7637 : sctp_sk(sk)->ep->reconf_enable;
7639 if (put_user(len, optlen))
7642 if (copy_to_user(optval, ¶ms, len))
7651 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7652 char __user *optval,
7655 struct sctp_assoc_value params;
7656 struct sctp_association *asoc;
7657 int retval = -EFAULT;
7659 if (len < sizeof(params)) {
7664 len = sizeof(params);
7665 if (copy_from_user(¶ms, optval, len))
7668 asoc = sctp_id2assoc(sk, params.assoc_id);
7669 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7670 sctp_style(sk, UDP)) {
7675 params.assoc_value = asoc ? asoc->strreset_enable
7676 : sctp_sk(sk)->ep->strreset_enable;
7678 if (put_user(len, optlen))
7681 if (copy_to_user(optval, ¶ms, len))
7690 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7691 char __user *optval,
7694 struct sctp_assoc_value params;
7695 struct sctp_association *asoc;
7696 int retval = -EFAULT;
7698 if (len < sizeof(params)) {
7703 len = sizeof(params);
7704 if (copy_from_user(¶ms, optval, len))
7707 asoc = sctp_id2assoc(sk, params.assoc_id);
7708 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7709 sctp_style(sk, UDP)) {
7714 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7715 : sctp_sk(sk)->default_ss;
7717 if (put_user(len, optlen))
7720 if (copy_to_user(optval, ¶ms, len))
7729 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7730 char __user *optval,
7733 struct sctp_stream_value params;
7734 struct sctp_association *asoc;
7735 int retval = -EFAULT;
7737 if (len < sizeof(params)) {
7742 len = sizeof(params);
7743 if (copy_from_user(¶ms, optval, len))
7746 asoc = sctp_id2assoc(sk, params.assoc_id);
7752 retval = sctp_sched_get_value(asoc, params.stream_id,
7753 ¶ms.stream_value);
7757 if (put_user(len, optlen)) {
7762 if (copy_to_user(optval, ¶ms, len)) {
7771 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7772 char __user *optval,
7775 struct sctp_assoc_value params;
7776 struct sctp_association *asoc;
7777 int retval = -EFAULT;
7779 if (len < sizeof(params)) {
7784 len = sizeof(params);
7785 if (copy_from_user(¶ms, optval, len))
7788 asoc = sctp_id2assoc(sk, params.assoc_id);
7789 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7790 sctp_style(sk, UDP)) {
7795 params.assoc_value = asoc ? asoc->peer.intl_capable
7796 : sctp_sk(sk)->ep->intl_enable;
7798 if (put_user(len, optlen))
7801 if (copy_to_user(optval, ¶ms, len))
7810 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7811 char __user *optval,
7816 if (len < sizeof(int))
7820 val = sctp_sk(sk)->reuse;
7821 if (put_user(len, optlen))
7824 if (copy_to_user(optval, &val, len))
7830 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7833 struct sctp_association *asoc;
7834 struct sctp_event param;
7837 if (len < sizeof(param))
7840 len = sizeof(param);
7841 if (copy_from_user(¶m, optval, len))
7844 if (param.se_type < SCTP_SN_TYPE_BASE ||
7845 param.se_type > SCTP_SN_TYPE_MAX)
7848 asoc = sctp_id2assoc(sk, param.se_assoc_id);
7849 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7850 sctp_style(sk, UDP))
7853 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7854 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7856 if (put_user(len, optlen))
7859 if (copy_to_user(optval, ¶m, len))
7865 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
7866 char __user *optval,
7869 struct sctp_assoc_value params;
7870 struct sctp_association *asoc;
7871 int retval = -EFAULT;
7873 if (len < sizeof(params)) {
7878 len = sizeof(params);
7879 if (copy_from_user(¶ms, optval, len))
7882 asoc = sctp_id2assoc(sk, params.assoc_id);
7883 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7884 sctp_style(sk, UDP)) {
7889 params.assoc_value = asoc ? asoc->peer.asconf_capable
7890 : sctp_sk(sk)->ep->asconf_enable;
7892 if (put_user(len, optlen))
7895 if (copy_to_user(optval, ¶ms, len))
7904 static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
7905 char __user *optval,
7908 struct sctp_assoc_value params;
7909 struct sctp_association *asoc;
7910 int retval = -EFAULT;
7912 if (len < sizeof(params)) {
7917 len = sizeof(params);
7918 if (copy_from_user(¶ms, optval, len))
7921 asoc = sctp_id2assoc(sk, params.assoc_id);
7922 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7923 sctp_style(sk, UDP)) {
7928 params.assoc_value = asoc ? asoc->peer.auth_capable
7929 : sctp_sk(sk)->ep->auth_enable;
7931 if (put_user(len, optlen))
7934 if (copy_to_user(optval, ¶ms, len))
7943 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
7944 char __user *optval,
7947 struct sctp_assoc_value params;
7948 struct sctp_association *asoc;
7949 int retval = -EFAULT;
7951 if (len < sizeof(params)) {
7956 len = sizeof(params);
7957 if (copy_from_user(¶ms, optval, len))
7960 asoc = sctp_id2assoc(sk, params.assoc_id);
7961 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7962 sctp_style(sk, UDP)) {
7967 params.assoc_value = asoc ? asoc->peer.ecn_capable
7968 : sctp_sk(sk)->ep->ecn_enable;
7970 if (put_user(len, optlen))
7973 if (copy_to_user(optval, ¶ms, len))
7982 static int sctp_getsockopt_pf_expose(struct sock *sk, int len,
7983 char __user *optval,
7986 struct sctp_assoc_value params;
7987 struct sctp_association *asoc;
7988 int retval = -EFAULT;
7990 if (len < sizeof(params)) {
7995 len = sizeof(params);
7996 if (copy_from_user(¶ms, optval, len))
7999 asoc = sctp_id2assoc(sk, params.assoc_id);
8000 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
8001 sctp_style(sk, UDP)) {
8006 params.assoc_value = asoc ? asoc->pf_expose
8007 : sctp_sk(sk)->pf_expose;
8009 if (put_user(len, optlen))
8012 if (copy_to_user(optval, ¶ms, len))
8021 static int sctp_getsockopt(struct sock *sk, int level, int optname,
8022 char __user *optval, int __user *optlen)
8027 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
8029 /* I can hardly begin to describe how wrong this is. This is
8030 * so broken as to be worse than useless. The API draft
8031 * REALLY is NOT helpful here... I am not convinced that the
8032 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
8033 * are at all well-founded.
8035 if (level != SOL_SCTP) {
8036 struct sctp_af *af = sctp_sk(sk)->pf->af;
8038 retval = af->getsockopt(sk, level, optname, optval, optlen);
8042 if (get_user(len, optlen))
8052 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
8054 case SCTP_DISABLE_FRAGMENTS:
8055 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
8059 retval = sctp_getsockopt_events(sk, len, optval, optlen);
8061 case SCTP_AUTOCLOSE:
8062 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
8064 case SCTP_SOCKOPT_PEELOFF:
8065 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
8067 case SCTP_SOCKOPT_PEELOFF_FLAGS:
8068 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
8070 case SCTP_PEER_ADDR_PARAMS:
8071 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
8074 case SCTP_DELAYED_SACK:
8075 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
8079 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
8081 case SCTP_GET_PEER_ADDRS:
8082 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
8085 case SCTP_GET_LOCAL_ADDRS:
8086 retval = sctp_getsockopt_local_addrs(sk, len, optval,
8089 case SCTP_SOCKOPT_CONNECTX3:
8090 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
8092 case SCTP_DEFAULT_SEND_PARAM:
8093 retval = sctp_getsockopt_default_send_param(sk, len,
8096 case SCTP_DEFAULT_SNDINFO:
8097 retval = sctp_getsockopt_default_sndinfo(sk, len,
8100 case SCTP_PRIMARY_ADDR:
8101 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
8104 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
8107 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
8109 case SCTP_ASSOCINFO:
8110 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
8112 case SCTP_I_WANT_MAPPED_V4_ADDR:
8113 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
8116 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
8118 case SCTP_GET_PEER_ADDR_INFO:
8119 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
8122 case SCTP_ADAPTATION_LAYER:
8123 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
8127 retval = sctp_getsockopt_context(sk, len, optval, optlen);
8129 case SCTP_FRAGMENT_INTERLEAVE:
8130 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
8133 case SCTP_PARTIAL_DELIVERY_POINT:
8134 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
8137 case SCTP_MAX_BURST:
8138 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
8141 case SCTP_AUTH_CHUNK:
8142 case SCTP_AUTH_DELETE_KEY:
8143 case SCTP_AUTH_DEACTIVATE_KEY:
8144 retval = -EOPNOTSUPP;
8146 case SCTP_HMAC_IDENT:
8147 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
8149 case SCTP_AUTH_ACTIVE_KEY:
8150 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
8152 case SCTP_PEER_AUTH_CHUNKS:
8153 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
8156 case SCTP_LOCAL_AUTH_CHUNKS:
8157 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
8160 case SCTP_GET_ASSOC_NUMBER:
8161 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
8163 case SCTP_GET_ASSOC_ID_LIST:
8164 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
8166 case SCTP_AUTO_ASCONF:
8167 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
8169 case SCTP_PEER_ADDR_THLDS:
8170 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
8173 case SCTP_PEER_ADDR_THLDS_V2:
8174 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
8177 case SCTP_GET_ASSOC_STATS:
8178 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
8180 case SCTP_RECVRCVINFO:
8181 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
8183 case SCTP_RECVNXTINFO:
8184 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
8186 case SCTP_PR_SUPPORTED:
8187 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
8189 case SCTP_DEFAULT_PRINFO:
8190 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
8193 case SCTP_PR_ASSOC_STATUS:
8194 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
8197 case SCTP_PR_STREAM_STATUS:
8198 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
8201 case SCTP_RECONFIG_SUPPORTED:
8202 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
8205 case SCTP_ENABLE_STREAM_RESET:
8206 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
8209 case SCTP_STREAM_SCHEDULER:
8210 retval = sctp_getsockopt_scheduler(sk, len, optval,
8213 case SCTP_STREAM_SCHEDULER_VALUE:
8214 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
8217 case SCTP_INTERLEAVING_SUPPORTED:
8218 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
8221 case SCTP_REUSE_PORT:
8222 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
8225 retval = sctp_getsockopt_event(sk, len, optval, optlen);
8227 case SCTP_ASCONF_SUPPORTED:
8228 retval = sctp_getsockopt_asconf_supported(sk, len, optval,
8231 case SCTP_AUTH_SUPPORTED:
8232 retval = sctp_getsockopt_auth_supported(sk, len, optval,
8235 case SCTP_ECN_SUPPORTED:
8236 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
8238 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
8239 retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen);
8242 retval = -ENOPROTOOPT;
8250 static int sctp_hash(struct sock *sk)
8256 static void sctp_unhash(struct sock *sk)
8261 /* Check if port is acceptable. Possibly find first available port.
8263 * The port hash table (contained in the 'global' SCTP protocol storage
8264 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
8265 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
8266 * list (the list number is the port number hashed out, so as you
8267 * would expect from a hash function, all the ports in a given list have
8268 * such a number that hashes out to the same list number; you were
8269 * expecting that, right?); so each list has a set of ports, with a
8270 * link to the socket (struct sock) that uses it, the port number and
8271 * a fastreuse flag (FIXME: NPI ipg).
8273 static struct sctp_bind_bucket *sctp_bucket_create(
8274 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8276 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8278 struct sctp_sock *sp = sctp_sk(sk);
8279 bool reuse = (sk->sk_reuse || sp->reuse);
8280 struct sctp_bind_hashbucket *head; /* hash list */
8281 struct net *net = sock_net(sk);
8282 kuid_t uid = sock_i_uid(sk);
8283 struct sctp_bind_bucket *pp;
8284 unsigned short snum;
8287 snum = ntohs(addr->v4.sin_port);
8289 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8294 /* Search for an available port. */
8295 int low, high, remaining, index;
8298 inet_get_local_port_range(net, &low, &high);
8299 remaining = (high - low) + 1;
8300 rover = prandom_u32() % remaining + low;
8304 if ((rover < low) || (rover > high))
8306 if (inet_is_local_reserved_port(net, rover))
8308 index = sctp_phashfn(net, rover);
8309 head = &sctp_port_hashtable[index];
8310 spin_lock(&head->lock);
8311 sctp_for_each_hentry(pp, &head->chain)
8312 if ((pp->port == rover) &&
8313 net_eq(net, pp->net))
8317 spin_unlock(&head->lock);
8318 } while (--remaining > 0);
8320 /* Exhausted local port range during search? */
8325 /* OK, here is the one we will use. HEAD (the port
8326 * hash table list entry) is non-NULL and we hold it's
8331 /* We are given an specific port number; we verify
8332 * that it is not being used. If it is used, we will
8333 * exahust the search in the hash list corresponding
8334 * to the port number (snum) - we detect that with the
8335 * port iterator, pp being NULL.
8337 head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
8338 spin_lock(&head->lock);
8339 sctp_for_each_hentry(pp, &head->chain) {
8340 if ((pp->port == snum) && net_eq(pp->net, net))
8347 if (!hlist_empty(&pp->owner)) {
8348 /* We had a port hash table hit - there is an
8349 * available port (pp != NULL) and it is being
8350 * used by other socket (pp->owner not empty); that other
8351 * socket is going to be sk2.
8355 pr_debug("%s: found a possible match\n", __func__);
8357 if ((pp->fastreuse && reuse &&
8358 sk->sk_state != SCTP_SS_LISTENING) ||
8359 (pp->fastreuseport && sk->sk_reuseport &&
8360 uid_eq(pp->fastuid, uid)))
8363 /* Run through the list of sockets bound to the port
8364 * (pp->port) [via the pointers bind_next and
8365 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8366 * we get the endpoint they describe and run through
8367 * the endpoint's list of IP (v4 or v6) addresses,
8368 * comparing each of the addresses with the address of
8369 * the socket sk. If we find a match, then that means
8370 * that this port/socket (sk) combination are already
8373 sk_for_each_bound(sk2, &pp->owner) {
8374 struct sctp_sock *sp2 = sctp_sk(sk2);
8375 struct sctp_endpoint *ep2 = sp2->ep;
8378 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8379 sk2->sk_state != SCTP_SS_LISTENING) ||
8380 (sk->sk_reuseport && sk2->sk_reuseport &&
8381 uid_eq(uid, sock_i_uid(sk2))))
8384 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8391 pr_debug("%s: found a match\n", __func__);
8394 /* If there was a hash table miss, create a new port. */
8396 if (!pp && !(pp = sctp_bucket_create(head, net, snum)))
8399 /* In either case (hit or miss), make sure fastreuse is 1 only
8400 * if sk->sk_reuse is too (that is, if the caller requested
8401 * SO_REUSEADDR on this socket -sk-).
8403 if (hlist_empty(&pp->owner)) {
8404 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8409 if (sk->sk_reuseport) {
8410 pp->fastreuseport = 1;
8413 pp->fastreuseport = 0;
8416 if (pp->fastreuse &&
8417 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8420 if (pp->fastreuseport &&
8421 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8422 pp->fastreuseport = 0;
8425 /* We are set, so fill up all the data in the hash table
8426 * entry, tie the socket list information with the rest of the
8427 * sockets FIXME: Blurry, NPI (ipg).
8430 if (!sp->bind_hash) {
8431 inet_sk(sk)->inet_num = snum;
8432 sk_add_bind_node(sk, &pp->owner);
8438 spin_unlock(&head->lock);
8445 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8446 * port is requested.
8448 static int sctp_get_port(struct sock *sk, unsigned short snum)
8450 union sctp_addr addr;
8451 struct sctp_af *af = sctp_sk(sk)->pf->af;
8453 /* Set up a dummy address struct from the sk. */
8454 af->from_sk(&addr, sk);
8455 addr.v4.sin_port = htons(snum);
8457 /* Note: sk->sk_num gets filled in if ephemeral port request. */
8458 return sctp_get_port_local(sk, &addr);
8462 * Move a socket to LISTENING state.
8464 static int sctp_listen_start(struct sock *sk, int backlog)
8466 struct sctp_sock *sp = sctp_sk(sk);
8467 struct sctp_endpoint *ep = sp->ep;
8468 struct crypto_shash *tfm = NULL;
8471 /* Allocate HMAC for generating cookie. */
8472 if (!sp->hmac && sp->sctp_hmac_alg) {
8473 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8474 tfm = crypto_alloc_shash(alg, 0, 0);
8476 net_info_ratelimited("failed to load transform for %s: %ld\n",
8477 sp->sctp_hmac_alg, PTR_ERR(tfm));
8480 sctp_sk(sk)->hmac = tfm;
8484 * If a bind() or sctp_bindx() is not called prior to a listen()
8485 * call that allows new associations to be accepted, the system
8486 * picks an ephemeral port and will choose an address set equivalent
8487 * to binding with a wildcard address.
8489 * This is not currently spelled out in the SCTP sockets
8490 * extensions draft, but follows the practice as seen in TCP
8494 inet_sk_set_state(sk, SCTP_SS_LISTENING);
8495 if (!ep->base.bind_addr.port) {
8496 if (sctp_autobind(sk))
8499 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8500 inet_sk_set_state(sk, SCTP_SS_CLOSED);
8505 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
8506 return sctp_hash_endpoint(ep);
8510 * 4.1.3 / 5.1.3 listen()
8512 * By default, new associations are not accepted for UDP style sockets.
8513 * An application uses listen() to mark a socket as being able to
8514 * accept new associations.
8516 * On TCP style sockets, applications use listen() to ready the SCTP
8517 * endpoint for accepting inbound associations.
8519 * On both types of endpoints a backlog of '0' disables listening.
8521 * Move a socket to LISTENING state.
8523 int sctp_inet_listen(struct socket *sock, int backlog)
8525 struct sock *sk = sock->sk;
8526 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8529 if (unlikely(backlog < 0))
8534 /* Peeled-off sockets are not allowed to listen(). */
8535 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8538 if (sock->state != SS_UNCONNECTED)
8541 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8544 /* If backlog is zero, disable listening. */
8546 if (sctp_sstate(sk, CLOSED))
8550 sctp_unhash_endpoint(ep);
8551 sk->sk_state = SCTP_SS_CLOSED;
8552 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8553 sctp_sk(sk)->bind_hash->fastreuse = 1;
8557 /* If we are already listening, just update the backlog */
8558 if (sctp_sstate(sk, LISTENING))
8559 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
8561 err = sctp_listen_start(sk, backlog);
8573 * This function is done by modeling the current datagram_poll() and the
8574 * tcp_poll(). Note that, based on these implementations, we don't
8575 * lock the socket in this function, even though it seems that,
8576 * ideally, locking or some other mechanisms can be used to ensure
8577 * the integrity of the counters (sndbuf and wmem_alloc) used
8578 * in this place. We assume that we don't need locks either until proven
8581 * Another thing to note is that we include the Async I/O support
8582 * here, again, by modeling the current TCP/UDP code. We don't have
8583 * a good way to test with it yet.
8585 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8587 struct sock *sk = sock->sk;
8588 struct sctp_sock *sp = sctp_sk(sk);
8591 poll_wait(file, sk_sleep(sk), wait);
8593 sock_rps_record_flow(sk);
8595 /* A TCP-style listening socket becomes readable when the accept queue
8598 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8599 return (!list_empty(&sp->ep->asocs)) ?
8600 (EPOLLIN | EPOLLRDNORM) : 0;
8604 /* Is there any exceptional events? */
8605 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8607 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8608 if (sk->sk_shutdown & RCV_SHUTDOWN)
8609 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8610 if (sk->sk_shutdown == SHUTDOWN_MASK)
8613 /* Is it readable? Reconsider this code with TCP-style support. */
8614 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8615 mask |= EPOLLIN | EPOLLRDNORM;
8617 /* The association is either gone or not ready. */
8618 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8621 /* Is it writable? */
8622 if (sctp_writeable(sk)) {
8623 mask |= EPOLLOUT | EPOLLWRNORM;
8625 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8627 * Since the socket is not locked, the buffer
8628 * might be made available after the writeable check and
8629 * before the bit is set. This could cause a lost I/O
8630 * signal. tcp_poll() has a race breaker for this race
8631 * condition. Based on their implementation, we put
8632 * in the following code to cover it as well.
8634 if (sctp_writeable(sk))
8635 mask |= EPOLLOUT | EPOLLWRNORM;
8640 /********************************************************************
8641 * 2nd Level Abstractions
8642 ********************************************************************/
8644 static struct sctp_bind_bucket *sctp_bucket_create(
8645 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8647 struct sctp_bind_bucket *pp;
8649 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8651 SCTP_DBG_OBJCNT_INC(bind_bucket);
8654 INIT_HLIST_HEAD(&pp->owner);
8656 hlist_add_head(&pp->node, &head->chain);
8661 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8662 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8664 if (pp && hlist_empty(&pp->owner)) {
8665 __hlist_del(&pp->node);
8666 kmem_cache_free(sctp_bucket_cachep, pp);
8667 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8671 /* Release this socket's reference to a local port. */
8672 static inline void __sctp_put_port(struct sock *sk)
8674 struct sctp_bind_hashbucket *head =
8675 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8676 inet_sk(sk)->inet_num)];
8677 struct sctp_bind_bucket *pp;
8679 spin_lock(&head->lock);
8680 pp = sctp_sk(sk)->bind_hash;
8681 __sk_del_bind_node(sk);
8682 sctp_sk(sk)->bind_hash = NULL;
8683 inet_sk(sk)->inet_num = 0;
8684 sctp_bucket_destroy(pp);
8685 spin_unlock(&head->lock);
8688 void sctp_put_port(struct sock *sk)
8691 __sctp_put_port(sk);
8696 * The system picks an ephemeral port and choose an address set equivalent
8697 * to binding with a wildcard address.
8698 * One of those addresses will be the primary address for the association.
8699 * This automatically enables the multihoming capability of SCTP.
8701 static int sctp_autobind(struct sock *sk)
8703 union sctp_addr autoaddr;
8707 /* Initialize a local sockaddr structure to INADDR_ANY. */
8708 af = sctp_sk(sk)->pf->af;
8710 port = htons(inet_sk(sk)->inet_num);
8711 af->inaddr_any(&autoaddr, port);
8713 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8716 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8719 * 4.2 The cmsghdr Structure *
8721 * When ancillary data is sent or received, any number of ancillary data
8722 * objects can be specified by the msg_control and msg_controllen members of
8723 * the msghdr structure, because each object is preceded by
8724 * a cmsghdr structure defining the object's length (the cmsg_len member).
8725 * Historically Berkeley-derived implementations have passed only one object
8726 * at a time, but this API allows multiple objects to be
8727 * passed in a single call to sendmsg() or recvmsg(). The following example
8728 * shows two ancillary data objects in a control buffer.
8730 * |<--------------------------- msg_controllen -------------------------->|
8733 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8735 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8738 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8740 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8743 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8744 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8746 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8748 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8755 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8757 struct msghdr *my_msg = (struct msghdr *)msg;
8758 struct cmsghdr *cmsg;
8760 for_each_cmsghdr(cmsg, my_msg) {
8761 if (!CMSG_OK(my_msg, cmsg))
8764 /* Should we parse this header or ignore? */
8765 if (cmsg->cmsg_level != IPPROTO_SCTP)
8768 /* Strictly check lengths following example in SCM code. */
8769 switch (cmsg->cmsg_type) {
8771 /* SCTP Socket API Extension
8772 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8774 * This cmsghdr structure provides information for
8775 * initializing new SCTP associations with sendmsg().
8776 * The SCTP_INITMSG socket option uses this same data
8777 * structure. This structure is not used for
8780 * cmsg_level cmsg_type cmsg_data[]
8781 * ------------ ------------ ----------------------
8782 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8784 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8787 cmsgs->init = CMSG_DATA(cmsg);
8791 /* SCTP Socket API Extension
8792 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8794 * This cmsghdr structure specifies SCTP options for
8795 * sendmsg() and describes SCTP header information
8796 * about a received message through recvmsg().
8798 * cmsg_level cmsg_type cmsg_data[]
8799 * ------------ ------------ ----------------------
8800 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8802 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8805 cmsgs->srinfo = CMSG_DATA(cmsg);
8807 if (cmsgs->srinfo->sinfo_flags &
8808 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8809 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8810 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8815 /* SCTP Socket API Extension
8816 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8818 * This cmsghdr structure specifies SCTP options for
8819 * sendmsg(). This structure and SCTP_RCVINFO replaces
8820 * SCTP_SNDRCV which has been deprecated.
8822 * cmsg_level cmsg_type cmsg_data[]
8823 * ------------ ------------ ---------------------
8824 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8826 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8829 cmsgs->sinfo = CMSG_DATA(cmsg);
8831 if (cmsgs->sinfo->snd_flags &
8832 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8833 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8834 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8838 /* SCTP Socket API Extension
8839 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8841 * This cmsghdr structure specifies SCTP options for sendmsg().
8843 * cmsg_level cmsg_type cmsg_data[]
8844 * ------------ ------------ ---------------------
8845 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8847 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8850 cmsgs->prinfo = CMSG_DATA(cmsg);
8851 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8854 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8855 cmsgs->prinfo->pr_value = 0;
8858 /* SCTP Socket API Extension
8859 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8861 * This cmsghdr structure specifies SCTP options for sendmsg().
8863 * cmsg_level cmsg_type cmsg_data[]
8864 * ------------ ------------ ---------------------
8865 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8867 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8870 cmsgs->authinfo = CMSG_DATA(cmsg);
8872 case SCTP_DSTADDRV4:
8873 case SCTP_DSTADDRV6:
8874 /* SCTP Socket API Extension
8875 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8877 * This cmsghdr structure specifies SCTP options for sendmsg().
8879 * cmsg_level cmsg_type cmsg_data[]
8880 * ------------ ------------ ---------------------
8881 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8882 * ------------ ------------ ---------------------
8883 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8885 cmsgs->addrs_msg = my_msg;
8896 * Wait for a packet..
8897 * Note: This function is the same function as in core/datagram.c
8898 * with a few modifications to make lksctp work.
8900 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8905 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8907 /* Socket errors? */
8908 error = sock_error(sk);
8912 if (!skb_queue_empty(&sk->sk_receive_queue))
8915 /* Socket shut down? */
8916 if (sk->sk_shutdown & RCV_SHUTDOWN)
8919 /* Sequenced packets can come disconnected. If so we report the
8924 /* Is there a good reason to think that we may receive some data? */
8925 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8928 /* Handle signals. */
8929 if (signal_pending(current))
8932 /* Let another process have a go. Since we are going to sleep
8933 * anyway. Note: This may cause odd behaviors if the message
8934 * does not fit in the user's buffer, but this seems to be the
8935 * only way to honor MSG_DONTWAIT realistically.
8938 *timeo_p = schedule_timeout(*timeo_p);
8942 finish_wait(sk_sleep(sk), &wait);
8946 error = sock_intr_errno(*timeo_p);
8949 finish_wait(sk_sleep(sk), &wait);
8954 /* Receive a datagram.
8955 * Note: This is pretty much the same routine as in core/datagram.c
8956 * with a few changes to make lksctp work.
8958 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8959 int noblock, int *err)
8962 struct sk_buff *skb;
8965 timeo = sock_rcvtimeo(sk, noblock);
8967 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8968 MAX_SCHEDULE_TIMEOUT);
8971 /* Again only user level code calls this function,
8972 * so nothing interrupt level
8973 * will suddenly eat the receive_queue.
8975 * Look at current nfs client by the way...
8976 * However, this function was correct in any case. 8)
8978 if (flags & MSG_PEEK) {
8979 skb = skb_peek(&sk->sk_receive_queue);
8981 refcount_inc(&skb->users);
8983 skb = __skb_dequeue(&sk->sk_receive_queue);
8989 /* Caller is allowed not to check sk->sk_err before calling. */
8990 error = sock_error(sk);
8994 if (sk->sk_shutdown & RCV_SHUTDOWN)
8997 if (sk_can_busy_loop(sk)) {
8998 sk_busy_loop(sk, noblock);
9000 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
9004 /* User doesn't want to wait. */
9008 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
9017 /* If sndbuf has changed, wake up per association sndbuf waiters. */
9018 static void __sctp_write_space(struct sctp_association *asoc)
9020 struct sock *sk = asoc->base.sk;
9022 if (sctp_wspace(asoc) <= 0)
9025 if (waitqueue_active(&asoc->wait))
9026 wake_up_interruptible(&asoc->wait);
9028 if (sctp_writeable(sk)) {
9029 struct socket_wq *wq;
9032 wq = rcu_dereference(sk->sk_wq);
9034 if (waitqueue_active(&wq->wait))
9035 wake_up_interruptible(&wq->wait);
9037 /* Note that we try to include the Async I/O support
9038 * here by modeling from the current TCP/UDP code.
9039 * We have not tested with it yet.
9041 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
9042 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
9048 static void sctp_wake_up_waiters(struct sock *sk,
9049 struct sctp_association *asoc)
9051 struct sctp_association *tmp = asoc;
9053 /* We do accounting for the sndbuf space per association,
9054 * so we only need to wake our own association.
9056 if (asoc->ep->sndbuf_policy)
9057 return __sctp_write_space(asoc);
9059 /* If association goes down and is just flushing its
9060 * outq, then just normally notify others.
9062 if (asoc->base.dead)
9063 return sctp_write_space(sk);
9065 /* Accounting for the sndbuf space is per socket, so we
9066 * need to wake up others, try to be fair and in case of
9067 * other associations, let them have a go first instead
9068 * of just doing a sctp_write_space() call.
9070 * Note that we reach sctp_wake_up_waiters() only when
9071 * associations free up queued chunks, thus we are under
9072 * lock and the list of associations on a socket is
9073 * guaranteed not to change.
9075 for (tmp = list_next_entry(tmp, asocs); 1;
9076 tmp = list_next_entry(tmp, asocs)) {
9077 /* Manually skip the head element. */
9078 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
9080 /* Wake up association. */
9081 __sctp_write_space(tmp);
9082 /* We've reached the end. */
9088 /* Do accounting for the sndbuf space.
9089 * Decrement the used sndbuf space of the corresponding association by the
9090 * data size which was just transmitted(freed).
9092 static void sctp_wfree(struct sk_buff *skb)
9094 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
9095 struct sctp_association *asoc = chunk->asoc;
9096 struct sock *sk = asoc->base.sk;
9098 sk_mem_uncharge(sk, skb->truesize);
9099 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
9100 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
9101 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
9102 &sk->sk_wmem_alloc));
9105 struct sctp_shared_key *shkey = chunk->shkey;
9107 /* refcnt == 2 and !list_empty mean after this release, it's
9108 * not being used anywhere, and it's time to notify userland
9109 * that this shkey can be freed if it's been deactivated.
9111 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
9112 refcount_read(&shkey->refcnt) == 2) {
9113 struct sctp_ulpevent *ev;
9115 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
9119 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
9121 sctp_auth_shkey_release(chunk->shkey);
9125 sctp_wake_up_waiters(sk, asoc);
9127 sctp_association_put(asoc);
9130 /* Do accounting for the receive space on the socket.
9131 * Accounting for the association is done in ulpevent.c
9132 * We set this as a destructor for the cloned data skbs so that
9133 * accounting is done at the correct time.
9135 void sctp_sock_rfree(struct sk_buff *skb)
9137 struct sock *sk = skb->sk;
9138 struct sctp_ulpevent *event = sctp_skb2event(skb);
9140 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
9143 * Mimic the behavior of sock_rfree
9145 sk_mem_uncharge(sk, event->rmem_len);
9149 /* Helper function to wait for space in the sndbuf. */
9150 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
9153 struct sock *sk = asoc->base.sk;
9154 long current_timeo = *timeo_p;
9158 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
9161 /* Increment the association's refcnt. */
9162 sctp_association_hold(asoc);
9164 /* Wait on the association specific sndbuf space. */
9166 prepare_to_wait_exclusive(&asoc->wait, &wait,
9167 TASK_INTERRUPTIBLE);
9168 if (asoc->base.dead)
9172 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
9174 if (signal_pending(current))
9175 goto do_interrupted;
9176 if (sk_under_memory_pressure(sk))
9178 if ((int)msg_len <= sctp_wspace(asoc) &&
9179 sk_wmem_schedule(sk, msg_len))
9182 /* Let another process have a go. Since we are going
9186 current_timeo = schedule_timeout(current_timeo);
9188 if (sk != asoc->base.sk)
9191 *timeo_p = current_timeo;
9195 finish_wait(&asoc->wait, &wait);
9197 /* Release the association's refcnt. */
9198 sctp_association_put(asoc);
9211 err = sock_intr_errno(*timeo_p);
9219 void sctp_data_ready(struct sock *sk)
9221 struct socket_wq *wq;
9224 wq = rcu_dereference(sk->sk_wq);
9225 if (skwq_has_sleeper(wq))
9226 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
9227 EPOLLRDNORM | EPOLLRDBAND);
9228 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9232 /* If socket sndbuf has changed, wake up all per association waiters. */
9233 void sctp_write_space(struct sock *sk)
9235 struct sctp_association *asoc;
9237 /* Wake up the tasks in each wait queue. */
9238 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
9239 __sctp_write_space(asoc);
9243 /* Is there any sndbuf space available on the socket?
9245 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
9246 * associations on the same socket. For a UDP-style socket with
9247 * multiple associations, it is possible for it to be "unwriteable"
9248 * prematurely. I assume that this is acceptable because
9249 * a premature "unwriteable" is better than an accidental "writeable" which
9250 * would cause an unwanted block under certain circumstances. For the 1-1
9251 * UDP-style sockets or TCP-style sockets, this code should work.
9254 static bool sctp_writeable(struct sock *sk)
9256 return sk->sk_sndbuf > sk->sk_wmem_queued;
9259 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
9260 * returns immediately with EINPROGRESS.
9262 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9264 struct sock *sk = asoc->base.sk;
9266 long current_timeo = *timeo_p;
9269 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9271 /* Increment the association's refcnt. */
9272 sctp_association_hold(asoc);
9275 prepare_to_wait_exclusive(&asoc->wait, &wait,
9276 TASK_INTERRUPTIBLE);
9279 if (sk->sk_shutdown & RCV_SHUTDOWN)
9281 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9284 if (signal_pending(current))
9285 goto do_interrupted;
9287 if (sctp_state(asoc, ESTABLISHED))
9290 /* Let another process have a go. Since we are going
9294 current_timeo = schedule_timeout(current_timeo);
9297 *timeo_p = current_timeo;
9301 finish_wait(&asoc->wait, &wait);
9303 /* Release the association's refcnt. */
9304 sctp_association_put(asoc);
9309 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9312 err = -ECONNREFUSED;
9316 err = sock_intr_errno(*timeo_p);
9324 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9326 struct sctp_endpoint *ep;
9330 ep = sctp_sk(sk)->ep;
9334 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9335 TASK_INTERRUPTIBLE);
9337 if (list_empty(&ep->asocs)) {
9339 timeo = schedule_timeout(timeo);
9344 if (!sctp_sstate(sk, LISTENING))
9348 if (!list_empty(&ep->asocs))
9351 err = sock_intr_errno(timeo);
9352 if (signal_pending(current))
9360 finish_wait(sk_sleep(sk), &wait);
9365 static void sctp_wait_for_close(struct sock *sk, long timeout)
9370 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9371 if (list_empty(&sctp_sk(sk)->ep->asocs))
9374 timeout = schedule_timeout(timeout);
9376 } while (!signal_pending(current) && timeout);
9378 finish_wait(sk_sleep(sk), &wait);
9381 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9383 struct sk_buff *frag;
9388 /* Don't forget the fragments. */
9389 skb_walk_frags(skb, frag)
9390 sctp_skb_set_owner_r_frag(frag, sk);
9393 sctp_skb_set_owner_r(skb, sk);
9396 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9397 struct sctp_association *asoc)
9399 struct inet_sock *inet = inet_sk(sk);
9400 struct inet_sock *newinet;
9401 struct sctp_sock *sp = sctp_sk(sk);
9402 struct sctp_endpoint *ep = sp->ep;
9404 newsk->sk_type = sk->sk_type;
9405 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9406 newsk->sk_flags = sk->sk_flags;
9407 newsk->sk_tsflags = sk->sk_tsflags;
9408 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9409 newsk->sk_no_check_rx = sk->sk_no_check_rx;
9410 newsk->sk_reuse = sk->sk_reuse;
9411 sctp_sk(newsk)->reuse = sp->reuse;
9413 newsk->sk_shutdown = sk->sk_shutdown;
9414 newsk->sk_destruct = sctp_destruct_sock;
9415 newsk->sk_family = sk->sk_family;
9416 newsk->sk_protocol = IPPROTO_SCTP;
9417 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9418 newsk->sk_sndbuf = sk->sk_sndbuf;
9419 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9420 newsk->sk_lingertime = sk->sk_lingertime;
9421 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9422 newsk->sk_sndtimeo = sk->sk_sndtimeo;
9423 newsk->sk_rxhash = sk->sk_rxhash;
9425 newinet = inet_sk(newsk);
9427 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9428 * getsockname() and getpeername()
9430 newinet->inet_sport = inet->inet_sport;
9431 newinet->inet_saddr = inet->inet_saddr;
9432 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9433 newinet->inet_dport = htons(asoc->peer.port);
9434 newinet->pmtudisc = inet->pmtudisc;
9435 newinet->inet_id = prandom_u32();
9437 newinet->uc_ttl = inet->uc_ttl;
9438 newinet->mc_loop = 1;
9439 newinet->mc_ttl = 1;
9440 newinet->mc_index = 0;
9441 newinet->mc_list = NULL;
9443 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9444 net_enable_timestamp();
9446 /* Set newsk security attributes from orginal sk and connection
9447 * security attribute from ep.
9449 security_sctp_sk_clone(ep, sk, newsk);
9452 static inline void sctp_copy_descendant(struct sock *sk_to,
9453 const struct sock *sk_from)
9455 int ancestor_size = sizeof(struct inet_sock) +
9456 sizeof(struct sctp_sock) -
9457 offsetof(struct sctp_sock, pd_lobby);
9459 if (sk_from->sk_family == PF_INET6)
9460 ancestor_size += sizeof(struct ipv6_pinfo);
9462 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9465 /* Populate the fields of the newsk from the oldsk and migrate the assoc
9466 * and its messages to the newsk.
9468 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9469 struct sctp_association *assoc,
9470 enum sctp_socket_type type)
9472 struct sctp_sock *oldsp = sctp_sk(oldsk);
9473 struct sctp_sock *newsp = sctp_sk(newsk);
9474 struct sctp_bind_bucket *pp; /* hash list port iterator */
9475 struct sctp_endpoint *newep = newsp->ep;
9476 struct sk_buff *skb, *tmp;
9477 struct sctp_ulpevent *event;
9478 struct sctp_bind_hashbucket *head;
9481 /* Migrate socket buffer sizes and all the socket level options to the
9484 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9485 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9486 /* Brute force copy old sctp opt. */
9487 sctp_copy_descendant(newsk, oldsk);
9489 /* Restore the ep value that was overwritten with the above structure
9495 /* Hook this new socket in to the bind_hash list. */
9496 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9497 inet_sk(oldsk)->inet_num)];
9498 spin_lock_bh(&head->lock);
9499 pp = sctp_sk(oldsk)->bind_hash;
9500 sk_add_bind_node(newsk, &pp->owner);
9501 sctp_sk(newsk)->bind_hash = pp;
9502 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9503 spin_unlock_bh(&head->lock);
9505 /* Copy the bind_addr list from the original endpoint to the new
9506 * endpoint so that we can handle restarts properly
9508 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9509 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9513 /* New ep's auth_hmacs should be set if old ep's is set, in case
9514 * that net->sctp.auth_enable has been changed to 0 by users and
9515 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9517 if (oldsp->ep->auth_hmacs) {
9518 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9523 /* Move any messages in the old socket's receive queue that are for the
9524 * peeled off association to the new socket's receive queue.
9526 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9527 event = sctp_skb2event(skb);
9528 if (event->asoc == assoc) {
9529 __skb_unlink(skb, &oldsk->sk_receive_queue);
9530 __skb_queue_tail(&newsk->sk_receive_queue, skb);
9531 sctp_skb_set_owner_r_frag(skb, newsk);
9535 /* Clean up any messages pending delivery due to partial
9536 * delivery. Three cases:
9537 * 1) No partial deliver; no work.
9538 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9539 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9541 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9543 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9544 struct sk_buff_head *queue;
9546 /* Decide which queue to move pd_lobby skbs to. */
9547 if (assoc->ulpq.pd_mode) {
9548 queue = &newsp->pd_lobby;
9550 queue = &newsk->sk_receive_queue;
9552 /* Walk through the pd_lobby, looking for skbs that
9553 * need moved to the new socket.
9555 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9556 event = sctp_skb2event(skb);
9557 if (event->asoc == assoc) {
9558 __skb_unlink(skb, &oldsp->pd_lobby);
9559 __skb_queue_tail(queue, skb);
9560 sctp_skb_set_owner_r_frag(skb, newsk);
9564 /* Clear up any skbs waiting for the partial
9565 * delivery to finish.
9567 if (assoc->ulpq.pd_mode)
9568 sctp_clear_pd(oldsk, NULL);
9572 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9574 /* Set the type of socket to indicate that it is peeled off from the
9575 * original UDP-style socket or created with the accept() call on a
9576 * TCP-style socket..
9580 /* Mark the new socket "in-use" by the user so that any packets
9581 * that may arrive on the association after we've moved it are
9582 * queued to the backlog. This prevents a potential race between
9583 * backlog processing on the old socket and new-packet processing
9584 * on the new socket.
9586 * The caller has just allocated newsk so we can guarantee that other
9587 * paths won't try to lock it and then oldsk.
9589 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9590 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
9591 sctp_assoc_migrate(assoc, newsk);
9592 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
9594 /* If the association on the newsk is already closed before accept()
9595 * is called, set RCV_SHUTDOWN flag.
9597 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9598 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9599 newsk->sk_shutdown |= RCV_SHUTDOWN;
9601 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9604 release_sock(newsk);
9610 /* This proto struct describes the ULP interface for SCTP. */
9611 struct proto sctp_prot = {
9613 .owner = THIS_MODULE,
9614 .close = sctp_close,
9615 .disconnect = sctp_disconnect,
9616 .accept = sctp_accept,
9617 .ioctl = sctp_ioctl,
9618 .init = sctp_init_sock,
9619 .destroy = sctp_destroy_sock,
9620 .shutdown = sctp_shutdown,
9621 .setsockopt = sctp_setsockopt,
9622 .getsockopt = sctp_getsockopt,
9623 .sendmsg = sctp_sendmsg,
9624 .recvmsg = sctp_recvmsg,
9626 .bind_add = sctp_bind_add,
9627 .backlog_rcv = sctp_backlog_rcv,
9629 .unhash = sctp_unhash,
9630 .no_autobind = true,
9631 .obj_size = sizeof(struct sctp_sock),
9632 .useroffset = offsetof(struct sctp_sock, subscribe),
9633 .usersize = offsetof(struct sctp_sock, initmsg) -
9634 offsetof(struct sctp_sock, subscribe) +
9635 sizeof_field(struct sctp_sock, initmsg),
9636 .sysctl_mem = sysctl_sctp_mem,
9637 .sysctl_rmem = sysctl_sctp_rmem,
9638 .sysctl_wmem = sysctl_sctp_wmem,
9639 .memory_pressure = &sctp_memory_pressure,
9640 .enter_memory_pressure = sctp_enter_memory_pressure,
9641 .memory_allocated = &sctp_memory_allocated,
9642 .sockets_allocated = &sctp_sockets_allocated,
9645 #if IS_ENABLED(CONFIG_IPV6)
9647 #include <net/transp_v6.h>
9648 static void sctp_v6_destroy_sock(struct sock *sk)
9650 sctp_destroy_sock(sk);
9651 inet6_destroy_sock(sk);
9654 struct proto sctpv6_prot = {
9656 .owner = THIS_MODULE,
9657 .close = sctp_close,
9658 .disconnect = sctp_disconnect,
9659 .accept = sctp_accept,
9660 .ioctl = sctp_ioctl,
9661 .init = sctp_init_sock,
9662 .destroy = sctp_v6_destroy_sock,
9663 .shutdown = sctp_shutdown,
9664 .setsockopt = sctp_setsockopt,
9665 .getsockopt = sctp_getsockopt,
9666 .sendmsg = sctp_sendmsg,
9667 .recvmsg = sctp_recvmsg,
9669 .bind_add = sctp_bind_add,
9670 .backlog_rcv = sctp_backlog_rcv,
9672 .unhash = sctp_unhash,
9673 .no_autobind = true,
9674 .obj_size = sizeof(struct sctp6_sock),
9675 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9676 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9677 offsetof(struct sctp6_sock, sctp.subscribe) +
9678 sizeof_field(struct sctp6_sock, sctp.initmsg),
9679 .sysctl_mem = sysctl_sctp_mem,
9680 .sysctl_rmem = sysctl_sctp_rmem,
9681 .sysctl_wmem = sysctl_sctp_wmem,
9682 .memory_pressure = &sctp_memory_pressure,
9683 .enter_memory_pressure = sctp_enter_memory_pressure,
9684 .memory_allocated = &sctp_memory_allocated,
9685 .sockets_allocated = &sctp_sockets_allocated,
9687 #endif /* IS_ENABLED(CONFIG_IPV6) */