1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * This SCTP implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * This SCTP implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
34 * Please send any bug reports or fixes you make to the
38 * Written or modified by:
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 #include <crypto/hash.h>
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/wait.h>
59 #include <linux/time.h>
60 #include <linux/sched/signal.h>
62 #include <linux/capability.h>
63 #include <linux/fcntl.h>
64 #include <linux/poll.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/file.h>
68 #include <linux/compat.h>
72 #include <net/route.h>
74 #include <net/inet_common.h>
75 #include <net/busy_poll.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <linux/export.h>
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
82 #include <net/sctp/stream_sched.h>
84 /* Forward declarations for internal helper functions. */
85 static int sctp_writeable(struct sock *sk);
86 static void sctp_wfree(struct sk_buff *skb);
87 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
88 size_t msg_len, struct sock **orig_sk);
89 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
90 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91 static int sctp_wait_for_accept(struct sock *sk, long timeo);
92 static void sctp_wait_for_close(struct sock *sk, long timeo);
93 static void sctp_destruct_sock(struct sock *sk);
94 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103 static int sctp_autobind(struct sock *sk);
104 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
108 static unsigned long sctp_memory_pressure;
109 static atomic_long_t sctp_memory_allocated;
110 struct percpu_counter sctp_sockets_allocated;
112 static void sctp_enter_memory_pressure(struct sock *sk)
114 sctp_memory_pressure = 1;
118 /* Get the sndbuf space available at the time on the association. */
119 static inline int sctp_wspace(struct sctp_association *asoc)
123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
126 amt = sk_wmem_alloc_get(asoc->base.sk);
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
132 amt = sk_stream_wspace(asoc->base.sk);
137 amt = asoc->base.sk->sk_sndbuf - amt;
142 /* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
151 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
159 skb_set_owner_w(chunk->skb, sk);
161 chunk->skb->destructor = sctp_wfree;
162 /* Save the chunk pointer in skb for sctp_wfree to use later. */
163 skb_shinfo(chunk->skb)->destructor_arg = chunk;
165 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
166 sizeof(struct sk_buff) +
167 sizeof(struct sctp_chunk);
169 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
170 sk->sk_wmem_queued += chunk->skb->truesize;
171 sk_mem_charge(sk, chunk->skb->truesize);
174 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
176 skb_orphan(chunk->skb);
179 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
180 void (*cb)(struct sctp_chunk *))
183 struct sctp_outq *q = &asoc->outqueue;
184 struct sctp_transport *t;
185 struct sctp_chunk *chunk;
187 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
188 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
191 list_for_each_entry(chunk, &q->retransmit, list)
194 list_for_each_entry(chunk, &q->sacked, list)
197 list_for_each_entry(chunk, &q->abandoned, list)
200 list_for_each_entry(chunk, &q->out_chunk_list, list)
204 /* Verify that this is a valid address. */
205 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
210 /* Verify basic sockaddr. */
211 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
215 /* Is this a valid SCTP address? */
216 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
219 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
225 /* Look up the association by its id. If this is not a UDP-style
226 * socket, the ID field is always ignored.
228 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
230 struct sctp_association *asoc = NULL;
232 /* If this is not a UDP-style socket, assoc id should be ignored. */
233 if (!sctp_style(sk, UDP)) {
234 /* Return NULL if the socket state is not ESTABLISHED. It
235 * could be a TCP-style listening socket or a socket which
236 * hasn't yet called connect() to establish an association.
238 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
241 /* Get the first and the only association from the list. */
242 if (!list_empty(&sctp_sk(sk)->ep->asocs))
243 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
244 struct sctp_association, asocs);
248 /* Otherwise this is a UDP-style socket. */
249 if (!id || (id == (sctp_assoc_t)-1))
252 spin_lock_bh(&sctp_assocs_id_lock);
253 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
254 spin_unlock_bh(&sctp_assocs_id_lock);
256 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
262 /* Look up the transport from an address and an assoc id. If both address and
263 * id are specified, the associations matching the address and the id should be
266 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
267 struct sockaddr_storage *addr,
270 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
271 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
272 union sctp_addr *laddr = (union sctp_addr *)addr;
273 struct sctp_transport *transport;
275 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
278 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
285 id_asoc = sctp_id2assoc(sk, id);
286 if (id_asoc && (id_asoc != addr_asoc))
289 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
290 (union sctp_addr *)addr);
295 /* API 3.1.2 bind() - UDP Style Syntax
296 * The syntax of bind() is,
298 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
300 * sd - the socket descriptor returned by socket().
301 * addr - the address structure (struct sockaddr_in or struct
302 * sockaddr_in6 [RFC 2553]),
303 * addr_len - the size of the address structure.
305 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
311 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
314 /* Disallow binding twice. */
315 if (!sctp_sk(sk)->ep->base.bind_addr.port)
316 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
326 static long sctp_get_port_local(struct sock *, union sctp_addr *);
328 /* Verify this is a valid sockaddr. */
329 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
330 union sctp_addr *addr, int len)
334 /* Check minimum size. */
335 if (len < sizeof (struct sockaddr))
338 /* V4 mapped address are really of AF_INET family */
339 if (addr->sa.sa_family == AF_INET6 &&
340 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
341 if (!opt->pf->af_supported(AF_INET, opt))
344 /* Does this PF support this AF? */
345 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
349 /* If we get this far, af is valid. */
350 af = sctp_get_af_specific(addr->sa.sa_family);
352 if (len < af->sockaddr_len)
358 /* Bind a local address either to an endpoint or to an association. */
359 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
361 struct net *net = sock_net(sk);
362 struct sctp_sock *sp = sctp_sk(sk);
363 struct sctp_endpoint *ep = sp->ep;
364 struct sctp_bind_addr *bp = &ep->base.bind_addr;
369 /* Common sockaddr verification. */
370 af = sctp_sockaddr_af(sp, addr, len);
372 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
373 __func__, sk, addr, len);
377 snum = ntohs(addr->v4.sin_port);
379 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
380 __func__, sk, &addr->sa, bp->port, snum, len);
382 /* PF specific bind() address verification. */
383 if (!sp->pf->bind_verify(sp, addr))
384 return -EADDRNOTAVAIL;
386 /* We must either be unbound, or bind to the same port.
387 * It's OK to allow 0 ports if we are already bound.
388 * We'll just inhert an already bound port in this case
393 else if (snum != bp->port) {
394 pr_debug("%s: new port %d doesn't match existing port "
395 "%d\n", __func__, snum, bp->port);
400 if (snum && snum < inet_prot_sock(net) &&
401 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
404 /* See if the address matches any of the addresses we may have
405 * already bound before checking against other endpoints.
407 if (sctp_bind_addr_match(bp, addr, sp))
410 /* Make sure we are allowed to bind here.
411 * The function sctp_get_port_local() does duplicate address
414 addr->v4.sin_port = htons(snum);
415 if ((ret = sctp_get_port_local(sk, addr))) {
419 /* Refresh ephemeral port. */
421 bp->port = inet_sk(sk)->inet_num;
423 /* Add the address to the bind address list.
424 * Use GFP_ATOMIC since BHs will be disabled.
426 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
427 SCTP_ADDR_SRC, GFP_ATOMIC);
429 /* Copy back into socket for getsockname() use. */
431 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
432 sp->pf->to_sk_saddr(addr, sk);
438 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
440 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
441 * at any one time. If a sender, after sending an ASCONF chunk, decides
442 * it needs to transfer another ASCONF Chunk, it MUST wait until the
443 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
444 * subsequent ASCONF. Note this restriction binds each side, so at any
445 * time two ASCONF may be in-transit on any given association (one sent
446 * from each endpoint).
448 static int sctp_send_asconf(struct sctp_association *asoc,
449 struct sctp_chunk *chunk)
451 struct net *net = sock_net(asoc->base.sk);
454 /* If there is an outstanding ASCONF chunk, queue it for later
457 if (asoc->addip_last_asconf) {
458 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
462 /* Hold the chunk until an ASCONF_ACK is received. */
463 sctp_chunk_hold(chunk);
464 retval = sctp_primitive_ASCONF(net, asoc, chunk);
466 sctp_chunk_free(chunk);
468 asoc->addip_last_asconf = chunk;
474 /* Add a list of addresses as bind addresses to local endpoint or
477 * Basically run through each address specified in the addrs/addrcnt
478 * array/length pair, determine if it is IPv6 or IPv4 and call
479 * sctp_do_bind() on it.
481 * If any of them fails, then the operation will be reversed and the
482 * ones that were added will be removed.
484 * Only sctp_setsockopt_bindx() is supposed to call this function.
486 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
491 struct sockaddr *sa_addr;
494 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
498 for (cnt = 0; cnt < addrcnt; cnt++) {
499 /* The list may contain either IPv4 or IPv6 address;
500 * determine the address length for walking thru the list.
503 af = sctp_get_af_specific(sa_addr->sa_family);
509 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
512 addr_buf += af->sockaddr_len;
516 /* Failed. Cleanup the ones that have been added */
518 sctp_bindx_rem(sk, addrs, cnt);
526 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
527 * associations that are part of the endpoint indicating that a list of local
528 * addresses are added to the endpoint.
530 * If any of the addresses is already in the bind address list of the
531 * association, we do not send the chunk for that association. But it will not
532 * affect other associations.
534 * Only sctp_setsockopt_bindx() is supposed to call this function.
536 static int sctp_send_asconf_add_ip(struct sock *sk,
537 struct sockaddr *addrs,
540 struct net *net = sock_net(sk);
541 struct sctp_sock *sp;
542 struct sctp_endpoint *ep;
543 struct sctp_association *asoc;
544 struct sctp_bind_addr *bp;
545 struct sctp_chunk *chunk;
546 struct sctp_sockaddr_entry *laddr;
547 union sctp_addr *addr;
548 union sctp_addr saveaddr;
555 if (!net->sctp.addip_enable)
561 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
562 __func__, sk, addrs, addrcnt);
564 list_for_each_entry(asoc, &ep->asocs, asocs) {
565 if (!asoc->peer.asconf_capable)
568 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
571 if (!sctp_state(asoc, ESTABLISHED))
574 /* Check if any address in the packed array of addresses is
575 * in the bind address list of the association. If so,
576 * do not send the asconf chunk to its peer, but continue with
577 * other associations.
580 for (i = 0; i < addrcnt; i++) {
582 af = sctp_get_af_specific(addr->v4.sin_family);
588 if (sctp_assoc_lookup_laddr(asoc, addr))
591 addr_buf += af->sockaddr_len;
596 /* Use the first valid address in bind addr list of
597 * association as Address Parameter of ASCONF CHUNK.
599 bp = &asoc->base.bind_addr;
600 p = bp->address_list.next;
601 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
602 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
603 addrcnt, SCTP_PARAM_ADD_IP);
609 /* Add the new addresses to the bind address list with
610 * use_as_src set to 0.
613 for (i = 0; i < addrcnt; i++) {
615 af = sctp_get_af_specific(addr->v4.sin_family);
616 memcpy(&saveaddr, addr, af->sockaddr_len);
617 retval = sctp_add_bind_addr(bp, &saveaddr,
619 SCTP_ADDR_NEW, GFP_ATOMIC);
620 addr_buf += af->sockaddr_len;
622 if (asoc->src_out_of_asoc_ok) {
623 struct sctp_transport *trans;
625 list_for_each_entry(trans,
626 &asoc->peer.transport_addr_list, transports) {
627 /* Clear the source and route cache */
628 sctp_transport_dst_release(trans);
629 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
630 2*asoc->pathmtu, 4380));
631 trans->ssthresh = asoc->peer.i.a_rwnd;
632 trans->rto = asoc->rto_initial;
633 sctp_max_rto(asoc, trans);
634 trans->rtt = trans->srtt = trans->rttvar = 0;
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 net *net = sock_net(sk);
745 struct sctp_sock *sp;
746 struct sctp_endpoint *ep;
747 struct sctp_association *asoc;
748 struct sctp_transport *transport;
749 struct sctp_bind_addr *bp;
750 struct sctp_chunk *chunk;
751 union sctp_addr *laddr;
754 struct sctp_sockaddr_entry *saddr;
760 if (!net->sctp.addip_enable)
766 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
767 __func__, sk, addrs, addrcnt);
769 list_for_each_entry(asoc, &ep->asocs, asocs) {
771 if (!asoc->peer.asconf_capable)
774 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
777 if (!sctp_state(asoc, ESTABLISHED))
780 /* Check if any address in the packed array of addresses is
781 * not present in the bind address list of the association.
782 * If so, do not send the asconf chunk to its peer, but
783 * continue with other associations.
786 for (i = 0; i < addrcnt; i++) {
788 af = sctp_get_af_specific(laddr->v4.sin_family);
794 if (!sctp_assoc_lookup_laddr(asoc, laddr))
797 addr_buf += af->sockaddr_len;
802 /* Find one address in the association's bind address list
803 * that is not in the packed array of addresses. This is to
804 * make sure that we do not delete all the addresses in the
807 bp = &asoc->base.bind_addr;
808 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
810 if ((laddr == NULL) && (addrcnt == 1)) {
811 if (asoc->asconf_addr_del_pending)
813 asoc->asconf_addr_del_pending =
814 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
815 if (asoc->asconf_addr_del_pending == NULL) {
819 asoc->asconf_addr_del_pending->sa.sa_family =
821 asoc->asconf_addr_del_pending->v4.sin_port =
823 if (addrs->sa_family == AF_INET) {
824 struct sockaddr_in *sin;
826 sin = (struct sockaddr_in *)addrs;
827 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
828 } else if (addrs->sa_family == AF_INET6) {
829 struct sockaddr_in6 *sin6;
831 sin6 = (struct sockaddr_in6 *)addrs;
832 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
835 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
836 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
837 asoc->asconf_addr_del_pending);
839 asoc->src_out_of_asoc_ok = 1;
847 /* We do not need RCU protection throughout this loop
848 * because this is done under a socket lock from the
851 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
859 /* Reset use_as_src flag for the addresses in the bind address
860 * list that are to be deleted.
863 for (i = 0; i < addrcnt; i++) {
865 af = sctp_get_af_specific(laddr->v4.sin_family);
866 list_for_each_entry(saddr, &bp->address_list, list) {
867 if (sctp_cmp_addr_exact(&saddr->a, laddr))
868 saddr->state = SCTP_ADDR_DEL;
870 addr_buf += af->sockaddr_len;
873 /* Update the route and saddr entries for all the transports
874 * as some of the addresses in the bind address list are
875 * about to be deleted and cannot be used as source addresses.
877 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
879 sctp_transport_dst_release(transport);
880 sctp_transport_route(transport, NULL,
881 sctp_sk(asoc->base.sk));
885 /* We don't need to transmit ASCONF */
887 retval = sctp_send_asconf(asoc, chunk);
893 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
894 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
896 struct sock *sk = sctp_opt2sk(sp);
897 union sctp_addr *addr;
900 /* It is safe to write port space in caller. */
902 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
903 af = sctp_get_af_specific(addr->sa.sa_family);
906 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
909 if (addrw->state == SCTP_ADDR_NEW)
910 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
912 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
915 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
918 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
921 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
922 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
925 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
926 * Section 3.1.2 for this usage.
928 * addrs is a pointer to an array of one or more socket addresses. Each
929 * address is contained in its appropriate structure (i.e. struct
930 * sockaddr_in or struct sockaddr_in6) the family of the address type
931 * must be used to distinguish the address length (note that this
932 * representation is termed a "packed array" of addresses). The caller
933 * specifies the number of addresses in the array with addrcnt.
935 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
936 * -1, and sets errno to the appropriate error code.
938 * For SCTP, the port given in each socket address must be the same, or
939 * sctp_bindx() will fail, setting errno to EINVAL.
941 * The flags parameter is formed from the bitwise OR of zero or more of
942 * the following currently defined flags:
944 * SCTP_BINDX_ADD_ADDR
946 * SCTP_BINDX_REM_ADDR
948 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
949 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
950 * addresses from the association. The two flags are mutually exclusive;
951 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
952 * not remove all addresses from an association; sctp_bindx() will
953 * reject such an attempt with EINVAL.
955 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
956 * additional addresses with an endpoint after calling bind(). Or use
957 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
958 * socket is associated with so that no new association accepted will be
959 * associated with those addresses. If the endpoint supports dynamic
960 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
961 * endpoint to send the appropriate message to the peer to change the
962 * peers address lists.
964 * Adding and removing addresses from a connected association is
965 * optional functionality. Implementations that do not support this
966 * functionality should return EOPNOTSUPP.
968 * Basically do nothing but copying the addresses from user to kernel
969 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
970 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
973 * We don't use copy_from_user() for optimization: we first do the
974 * sanity checks (buffer size -fast- and access check-healthy
975 * pointer); if all of those succeed, then we can alloc the memory
976 * (expensive operation) needed to copy the data to kernel. Then we do
977 * the copying without checking the user space area
978 * (__copy_from_user()).
980 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
983 * sk The sk of the socket
984 * addrs The pointer to the addresses in user land
985 * addrssize Size of the addrs buffer
986 * op Operation to perform (add or remove, see the flags of
989 * Returns 0 if ok, <0 errno code on error.
991 static int sctp_setsockopt_bindx(struct sock *sk,
992 struct sockaddr __user *addrs,
993 int addrs_size, int op)
995 struct sockaddr *kaddrs;
999 struct sockaddr *sa_addr;
1003 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1004 __func__, sk, addrs, addrs_size, op);
1006 if (unlikely(addrs_size <= 0))
1009 /* Check the user passed a healthy pointer. */
1010 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1013 /* Alloc space for the address array in kernel memory. */
1014 kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN);
1015 if (unlikely(!kaddrs))
1018 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1023 /* Walk through the addrs buffer and count the number of addresses. */
1025 while (walk_size < addrs_size) {
1026 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1032 af = sctp_get_af_specific(sa_addr->sa_family);
1034 /* If the address family is not supported or if this address
1035 * causes the address buffer to overflow return EINVAL.
1037 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1048 case SCTP_BINDX_ADD_ADDR:
1049 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1052 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1055 case SCTP_BINDX_REM_ADDR:
1056 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1059 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1073 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1075 * Common routine for handling connect() and sctp_connectx().
1076 * Connect will come in with just a single address.
1078 static int __sctp_connect(struct sock *sk,
1079 struct sockaddr *kaddrs,
1081 sctp_assoc_t *assoc_id)
1083 struct net *net = sock_net(sk);
1084 struct sctp_sock *sp;
1085 struct sctp_endpoint *ep;
1086 struct sctp_association *asoc = NULL;
1087 struct sctp_association *asoc2;
1088 struct sctp_transport *transport;
1090 enum sctp_scope scope;
1095 union sctp_addr *sa_addr = NULL;
1097 unsigned short port;
1098 unsigned int f_flags = 0;
1103 /* connect() cannot be done on a socket that is already in ESTABLISHED
1104 * state - UDP-style peeled off socket or a TCP-style socket that
1105 * is already connected.
1106 * It cannot be done even on a TCP-style listening socket.
1108 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1109 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1114 /* Walk through the addrs buffer and count the number of addresses. */
1116 while (walk_size < addrs_size) {
1119 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1125 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1127 /* If the address family is not supported or if this address
1128 * causes the address buffer to overflow return EINVAL.
1130 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1135 port = ntohs(sa_addr->v4.sin_port);
1137 /* Save current address so we can work with it */
1138 memcpy(&to, sa_addr, af->sockaddr_len);
1140 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1144 /* Make sure the destination port is correctly set
1147 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1152 /* Check if there already is a matching association on the
1153 * endpoint (other than the one created here).
1155 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1156 if (asoc2 && asoc2 != asoc) {
1157 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1164 /* If we could not find a matching association on the endpoint,
1165 * make sure that there is no peeled-off association matching
1166 * the peer address even on another socket.
1168 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1169 err = -EADDRNOTAVAIL;
1174 /* If a bind() or sctp_bindx() is not called prior to
1175 * an sctp_connectx() call, the system picks an
1176 * ephemeral port and will choose an address set
1177 * equivalent to binding with a wildcard address.
1179 if (!ep->base.bind_addr.port) {
1180 if (sctp_autobind(sk)) {
1186 * If an unprivileged user inherits a 1-many
1187 * style socket with open associations on a
1188 * privileged port, it MAY be permitted to
1189 * accept new associations, but it SHOULD NOT
1190 * be permitted to open new associations.
1192 if (ep->base.bind_addr.port <
1193 inet_prot_sock(net) &&
1194 !ns_capable(net->user_ns,
1195 CAP_NET_BIND_SERVICE)) {
1201 scope = sctp_scope(&to);
1202 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1208 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1216 /* Prime the peer's transport structures. */
1217 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1225 addr_buf += af->sockaddr_len;
1226 walk_size += af->sockaddr_len;
1229 /* In case the user of sctp_connectx() wants an association
1230 * id back, assign one now.
1233 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1238 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1243 /* Initialize sk's dport and daddr for getpeername() */
1244 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1245 sp->pf->to_sk_daddr(sa_addr, sk);
1248 /* in-kernel sockets don't generally have a file allocated to them
1249 * if all they do is call sock_create_kern().
1251 if (sk->sk_socket->file)
1252 f_flags = sk->sk_socket->file->f_flags;
1254 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1257 *assoc_id = asoc->assoc_id;
1258 err = sctp_wait_for_connect(asoc, &timeo);
1259 /* Note: the asoc may be freed after the return of
1260 * sctp_wait_for_connect.
1263 /* Don't free association on exit. */
1267 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1268 __func__, asoc, kaddrs, err);
1271 /* sctp_primitive_ASSOCIATE may have added this association
1272 * To the hash table, try to unhash it, just in case, its a noop
1273 * if it wasn't hashed so we're safe
1275 sctp_association_free(asoc);
1280 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1283 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1284 * sctp_assoc_t *asoc);
1286 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1287 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1288 * or IPv6 addresses.
1290 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1291 * Section 3.1.2 for this usage.
1293 * addrs is a pointer to an array of one or more socket addresses. Each
1294 * address is contained in its appropriate structure (i.e. struct
1295 * sockaddr_in or struct sockaddr_in6) the family of the address type
1296 * must be used to distengish the address length (note that this
1297 * representation is termed a "packed array" of addresses). The caller
1298 * specifies the number of addresses in the array with addrcnt.
1300 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1301 * the association id of the new association. On failure, sctp_connectx()
1302 * returns -1, and sets errno to the appropriate error code. The assoc_id
1303 * is not touched by the kernel.
1305 * For SCTP, the port given in each socket address must be the same, or
1306 * sctp_connectx() will fail, setting errno to EINVAL.
1308 * An application can use sctp_connectx to initiate an association with
1309 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1310 * allows a caller to specify multiple addresses at which a peer can be
1311 * reached. The way the SCTP stack uses the list of addresses to set up
1312 * the association is implementation dependent. This function only
1313 * specifies that the stack will try to make use of all the addresses in
1314 * the list when needed.
1316 * Note that the list of addresses passed in is only used for setting up
1317 * the association. It does not necessarily equal the set of addresses
1318 * the peer uses for the resulting association. If the caller wants to
1319 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1320 * retrieve them after the association has been set up.
1322 * Basically do nothing but copying the addresses from user to kernel
1323 * land and invoking either sctp_connectx(). This is used for tunneling
1324 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1326 * We don't use copy_from_user() for optimization: we first do the
1327 * sanity checks (buffer size -fast- and access check-healthy
1328 * pointer); if all of those succeed, then we can alloc the memory
1329 * (expensive operation) needed to copy the data to kernel. Then we do
1330 * the copying without checking the user space area
1331 * (__copy_from_user()).
1333 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1336 * sk The sk of the socket
1337 * addrs The pointer to the addresses in user land
1338 * addrssize Size of the addrs buffer
1340 * Returns >=0 if ok, <0 errno code on error.
1342 static int __sctp_setsockopt_connectx(struct sock *sk,
1343 struct sockaddr __user *addrs,
1345 sctp_assoc_t *assoc_id)
1347 struct sockaddr *kaddrs;
1348 gfp_t gfp = GFP_KERNEL;
1351 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1352 __func__, sk, addrs, addrs_size);
1354 if (unlikely(addrs_size <= 0))
1357 /* Check the user passed a healthy pointer. */
1358 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1361 /* Alloc space for the address array in kernel memory. */
1362 if (sk->sk_socket->file)
1363 gfp = GFP_USER | __GFP_NOWARN;
1364 kaddrs = kmalloc(addrs_size, gfp);
1365 if (unlikely(!kaddrs))
1368 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1371 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1380 * This is an older interface. It's kept for backward compatibility
1381 * to the option that doesn't provide association id.
1383 static int sctp_setsockopt_connectx_old(struct sock *sk,
1384 struct sockaddr __user *addrs,
1387 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1391 * New interface for the API. The since the API is done with a socket
1392 * option, to make it simple we feed back the association id is as a return
1393 * indication to the call. Error is always negative and association id is
1396 static int sctp_setsockopt_connectx(struct sock *sk,
1397 struct sockaddr __user *addrs,
1400 sctp_assoc_t assoc_id = 0;
1403 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1412 * New (hopefully final) interface for the API.
1413 * We use the sctp_getaddrs_old structure so that use-space library
1414 * can avoid any unnecessary allocations. The only different part
1415 * is that we store the actual length of the address buffer into the
1416 * addrs_num structure member. That way we can re-use the existing
1419 #ifdef CONFIG_COMPAT
1420 struct compat_sctp_getaddrs_old {
1421 sctp_assoc_t assoc_id;
1423 compat_uptr_t addrs; /* struct sockaddr * */
1427 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1428 char __user *optval,
1431 struct sctp_getaddrs_old param;
1432 sctp_assoc_t assoc_id = 0;
1435 #ifdef CONFIG_COMPAT
1436 if (in_compat_syscall()) {
1437 struct compat_sctp_getaddrs_old param32;
1439 if (len < sizeof(param32))
1441 if (copy_from_user(¶m32, optval, sizeof(param32)))
1444 param.assoc_id = param32.assoc_id;
1445 param.addr_num = param32.addr_num;
1446 param.addrs = compat_ptr(param32.addrs);
1450 if (len < sizeof(param))
1452 if (copy_from_user(¶m, optval, sizeof(param)))
1456 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1457 param.addrs, param.addr_num,
1459 if (err == 0 || err == -EINPROGRESS) {
1460 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1462 if (put_user(sizeof(assoc_id), optlen))
1469 /* API 3.1.4 close() - UDP Style Syntax
1470 * Applications use close() to perform graceful shutdown (as described in
1471 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1472 * by a UDP-style socket.
1476 * ret = close(int sd);
1478 * sd - the socket descriptor of the associations to be closed.
1480 * To gracefully shutdown a specific association represented by the
1481 * UDP-style socket, an application should use the sendmsg() call,
1482 * passing no user data, but including the appropriate flag in the
1483 * ancillary data (see Section xxxx).
1485 * If sd in the close() call is a branched-off socket representing only
1486 * one association, the shutdown is performed on that association only.
1488 * 4.1.6 close() - TCP Style Syntax
1490 * Applications use close() to gracefully close down an association.
1494 * int close(int sd);
1496 * sd - the socket descriptor of the association to be closed.
1498 * After an application calls close() on a socket descriptor, no further
1499 * socket operations will succeed on that descriptor.
1501 * API 7.1.4 SO_LINGER
1503 * An application using the TCP-style socket can use this option to
1504 * perform the SCTP ABORT primitive. The linger option structure is:
1507 * int l_onoff; // option on/off
1508 * int l_linger; // linger time
1511 * To enable the option, set l_onoff to 1. If the l_linger value is set
1512 * to 0, calling close() is the same as the ABORT primitive. If the
1513 * value is set to a negative value, the setsockopt() call will return
1514 * an error. If the value is set to a positive value linger_time, the
1515 * close() can be blocked for at most linger_time ms. If the graceful
1516 * shutdown phase does not finish during this period, close() will
1517 * return but the graceful shutdown phase continues in the system.
1519 static void sctp_close(struct sock *sk, long timeout)
1521 struct net *net = sock_net(sk);
1522 struct sctp_endpoint *ep;
1523 struct sctp_association *asoc;
1524 struct list_head *pos, *temp;
1525 unsigned int data_was_unread;
1527 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1529 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1530 sk->sk_shutdown = SHUTDOWN_MASK;
1531 sk->sk_state = SCTP_SS_CLOSING;
1533 ep = sctp_sk(sk)->ep;
1535 /* Clean up any skbs sitting on the receive queue. */
1536 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1537 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1539 /* Walk all associations on an endpoint. */
1540 list_for_each_safe(pos, temp, &ep->asocs) {
1541 asoc = list_entry(pos, struct sctp_association, asocs);
1543 if (sctp_style(sk, TCP)) {
1544 /* A closed association can still be in the list if
1545 * it belongs to a TCP-style listening socket that is
1546 * not yet accepted. If so, free it. If not, send an
1547 * ABORT or SHUTDOWN based on the linger options.
1549 if (sctp_state(asoc, CLOSED)) {
1550 sctp_association_free(asoc);
1555 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1556 !skb_queue_empty(&asoc->ulpq.reasm) ||
1557 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1558 struct sctp_chunk *chunk;
1560 chunk = sctp_make_abort_user(asoc, NULL, 0);
1561 sctp_primitive_ABORT(net, asoc, chunk);
1563 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1566 /* On a TCP-style socket, block for at most linger_time if set. */
1567 if (sctp_style(sk, TCP) && timeout)
1568 sctp_wait_for_close(sk, timeout);
1570 /* This will run the backlog queue. */
1573 /* Supposedly, no process has access to the socket, but
1574 * the net layers still may.
1575 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1576 * held and that should be grabbed before socket lock.
1578 spin_lock_bh(&net->sctp.addr_wq_lock);
1579 bh_lock_sock_nested(sk);
1581 /* Hold the sock, since sk_common_release() will put sock_put()
1582 * and we have just a little more cleanup.
1585 sk_common_release(sk);
1588 spin_unlock_bh(&net->sctp.addr_wq_lock);
1592 SCTP_DBG_OBJCNT_DEC(sock);
1595 /* Handle EPIPE error. */
1596 static int sctp_error(struct sock *sk, int flags, int err)
1599 err = sock_error(sk) ? : -EPIPE;
1600 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1601 send_sig(SIGPIPE, current, 0);
1605 /* API 3.1.3 sendmsg() - UDP Style Syntax
1607 * An application uses sendmsg() and recvmsg() calls to transmit data to
1608 * and receive data from its peer.
1610 * ssize_t sendmsg(int socket, const struct msghdr *message,
1613 * socket - the socket descriptor of the endpoint.
1614 * message - pointer to the msghdr structure which contains a single
1615 * user message and possibly some ancillary data.
1617 * See Section 5 for complete description of the data
1620 * flags - flags sent or received with the user message, see Section
1621 * 5 for complete description of the flags.
1623 * Note: This function could use a rewrite especially when explicit
1624 * connect support comes in.
1626 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1628 static int sctp_msghdr_parse(const struct msghdr *msg,
1629 struct sctp_cmsgs *cmsgs);
1631 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1633 struct net *net = sock_net(sk);
1634 struct sctp_sock *sp;
1635 struct sctp_endpoint *ep;
1636 struct sctp_association *new_asoc = NULL, *asoc = NULL;
1637 struct sctp_transport *transport, *chunk_tp;
1638 struct sctp_chunk *chunk;
1640 struct sockaddr *msg_name = NULL;
1641 struct sctp_sndrcvinfo default_sinfo;
1642 struct sctp_sndrcvinfo *sinfo;
1643 struct sctp_initmsg *sinit;
1644 sctp_assoc_t associd = 0;
1645 struct sctp_cmsgs cmsgs = { NULL };
1646 enum sctp_scope scope;
1647 bool fill_sinfo_ttl = false, wait_connect = false;
1648 struct sctp_datamsg *datamsg;
1649 int msg_flags = msg->msg_flags;
1650 __u16 sinfo_flags = 0;
1658 pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
1661 /* We cannot send a message over a TCP-style listening socket. */
1662 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1667 /* Parse out the SCTP CMSGs. */
1668 err = sctp_msghdr_parse(msg, &cmsgs);
1670 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1674 /* Fetch the destination address for this packet. This
1675 * address only selects the association--it is not necessarily
1676 * the address we will send to.
1677 * For a peeled-off socket, msg_name is ignored.
1679 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1680 int msg_namelen = msg->msg_namelen;
1682 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1687 if (msg_namelen > sizeof(to))
1688 msg_namelen = sizeof(to);
1689 memcpy(&to, msg->msg_name, msg_namelen);
1690 msg_name = msg->msg_name;
1694 if (cmsgs.sinfo != NULL) {
1695 memset(&default_sinfo, 0, sizeof(default_sinfo));
1696 default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
1697 default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
1698 default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
1699 default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
1700 default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
1702 sinfo = &default_sinfo;
1703 fill_sinfo_ttl = true;
1705 sinfo = cmsgs.srinfo;
1707 /* Did the user specify SNDINFO/SNDRCVINFO? */
1709 sinfo_flags = sinfo->sinfo_flags;
1710 associd = sinfo->sinfo_assoc_id;
1713 pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
1714 msg_len, sinfo_flags);
1716 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1717 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1722 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1723 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1724 * If SCTP_ABORT is set, the message length could be non zero with
1725 * the msg_iov set to the user abort reason.
1727 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1728 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1733 /* If SCTP_ADDR_OVER is set, there must be an address
1734 * specified in msg_name.
1736 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1743 pr_debug("%s: about to look up association\n", __func__);
1747 /* If a msg_name has been specified, assume this is to be used. */
1749 /* Look for a matching association on the endpoint. */
1750 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1752 /* If we could not find a matching association on the
1753 * endpoint, make sure that it is not a TCP-style
1754 * socket that already has an association or there is
1755 * no peeled-off association on another socket.
1758 ((sctp_style(sk, TCP) &&
1759 (sctp_sstate(sk, ESTABLISHED) ||
1760 sctp_sstate(sk, CLOSING))) ||
1761 sctp_endpoint_is_peeled_off(ep, &to))) {
1762 err = -EADDRNOTAVAIL;
1766 asoc = sctp_id2assoc(sk, associd);
1774 pr_debug("%s: just looked up association:%p\n", __func__, asoc);
1776 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1777 * socket that has an association in CLOSED state. This can
1778 * happen when an accepted socket has an association that is
1781 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1786 if (sinfo_flags & SCTP_EOF) {
1787 pr_debug("%s: shutting down association:%p\n",
1790 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1794 if (sinfo_flags & SCTP_ABORT) {
1796 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1802 pr_debug("%s: aborting association:%p\n",
1805 sctp_primitive_ABORT(net, asoc, chunk);
1811 /* Do we need to create the association? */
1813 pr_debug("%s: there is no association yet\n", __func__);
1815 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1820 /* Check for invalid stream against the stream counts,
1821 * either the default or the user specified stream counts.
1824 if (!sinit || !sinit->sinit_num_ostreams) {
1825 /* Check against the defaults. */
1826 if (sinfo->sinfo_stream >=
1827 sp->initmsg.sinit_num_ostreams) {
1832 /* Check against the requested. */
1833 if (sinfo->sinfo_stream >=
1834 sinit->sinit_num_ostreams) {
1842 * API 3.1.2 bind() - UDP Style Syntax
1843 * If a bind() or sctp_bindx() is not called prior to a
1844 * sendmsg() call that initiates a new association, the
1845 * system picks an ephemeral port and will choose an address
1846 * set equivalent to binding with a wildcard address.
1848 if (!ep->base.bind_addr.port) {
1849 if (sctp_autobind(sk)) {
1855 * If an unprivileged user inherits a one-to-many
1856 * style socket with open associations on a privileged
1857 * port, it MAY be permitted to accept new associations,
1858 * but it SHOULD NOT be permitted to open new
1861 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1862 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
1868 scope = sctp_scope(&to);
1869 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1875 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1881 /* If the SCTP_INIT ancillary data is specified, set all
1882 * the association init values accordingly.
1885 if (sinit->sinit_num_ostreams) {
1886 asoc->c.sinit_num_ostreams =
1887 sinit->sinit_num_ostreams;
1889 if (sinit->sinit_max_instreams) {
1890 asoc->c.sinit_max_instreams =
1891 sinit->sinit_max_instreams;
1893 if (sinit->sinit_max_attempts) {
1894 asoc->max_init_attempts
1895 = sinit->sinit_max_attempts;
1897 if (sinit->sinit_max_init_timeo) {
1898 asoc->max_init_timeo =
1899 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1903 /* Prime the peer's transport structures. */
1904 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1911 /* ASSERT: we have a valid association at this point. */
1912 pr_debug("%s: we have a valid association\n", __func__);
1915 /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
1916 * one with some defaults.
1918 memset(&default_sinfo, 0, sizeof(default_sinfo));
1919 default_sinfo.sinfo_stream = asoc->default_stream;
1920 default_sinfo.sinfo_flags = asoc->default_flags;
1921 default_sinfo.sinfo_ppid = asoc->default_ppid;
1922 default_sinfo.sinfo_context = asoc->default_context;
1923 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1924 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1926 sinfo = &default_sinfo;
1927 } else if (fill_sinfo_ttl) {
1928 /* In case SNDINFO was specified, we still need to fill
1929 * it with a default ttl from the assoc here.
1931 sinfo->sinfo_timetolive = asoc->default_timetolive;
1934 /* API 7.1.7, the sndbuf size per association bounds the
1935 * maximum size of data that can be sent in a single send call.
1937 if (msg_len > sk->sk_sndbuf) {
1942 if (asoc->pmtu_pending)
1943 sctp_assoc_pending_pmtu(asoc);
1945 /* If fragmentation is disabled and the message length exceeds the
1946 * association fragmentation point, return EMSGSIZE. The I-D
1947 * does not specify what this error is, but this looks like
1950 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1955 /* Check for invalid stream. */
1956 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1961 /* Allocate sctp_stream_out_ext if not already done */
1962 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1963 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1968 if (sctp_wspace(asoc) < msg_len)
1969 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1971 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1972 if (!sctp_wspace(asoc)) {
1973 /* sk can be changed by peel off when waiting for buf. */
1974 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len, &sk);
1976 if (err == -ESRCH) {
1977 /* asoc is already dead. */
1985 /* If an address is passed with the sendto/sendmsg call, it is used
1986 * to override the primary destination address in the TCP model, or
1987 * when SCTP_ADDR_OVER flag is set in the UDP model.
1989 if ((sctp_style(sk, TCP) && msg_name) ||
1990 (sinfo_flags & SCTP_ADDR_OVER)) {
1991 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1999 /* Auto-connect, if we aren't connected already. */
2000 if (sctp_state(asoc, CLOSED)) {
2001 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
2005 wait_connect = true;
2006 pr_debug("%s: we associated primitively\n", __func__);
2009 /* Break the message into multiple chunks of maximum size. */
2010 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
2011 if (IS_ERR(datamsg)) {
2012 err = PTR_ERR(datamsg);
2015 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
2017 /* Now send the (possibly) fragmented message. */
2018 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
2019 sctp_chunk_hold(chunk);
2021 /* Do accounting for the write space. */
2022 sctp_set_owner_w(chunk);
2024 chunk->transport = chunk_tp;
2027 /* Send it to the lower layers. Note: all chunks
2028 * must either fail or succeed. The lower layer
2029 * works that way today. Keep it that way or this
2032 err = sctp_primitive_SEND(net, asoc, datamsg);
2033 /* Did the lower layer accept the chunk? */
2035 sctp_datamsg_free(datamsg);
2039 pr_debug("%s: we sent primitively\n", __func__);
2041 sctp_datamsg_put(datamsg);
2044 if (unlikely(wait_connect)) {
2045 timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT);
2046 sctp_wait_for_connect(asoc, &timeo);
2049 /* If we are already past ASSOCIATE, the lower
2050 * layers are responsible for association cleanup.
2056 sctp_association_free(asoc);
2061 return sctp_error(sk, msg_flags, err);
2068 err = sock_error(sk);
2078 /* This is an extended version of skb_pull() that removes the data from the
2079 * start of a skb even when data is spread across the list of skb's in the
2080 * frag_list. len specifies the total amount of data that needs to be removed.
2081 * when 'len' bytes could be removed from the skb, it returns 0.
2082 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2083 * could not be removed.
2085 static int sctp_skb_pull(struct sk_buff *skb, int len)
2087 struct sk_buff *list;
2088 int skb_len = skb_headlen(skb);
2091 if (len <= skb_len) {
2092 __skb_pull(skb, len);
2096 __skb_pull(skb, skb_len);
2098 skb_walk_frags(skb, list) {
2099 rlen = sctp_skb_pull(list, len);
2100 skb->len -= (len-rlen);
2101 skb->data_len -= (len-rlen);
2112 /* API 3.1.3 recvmsg() - UDP Style Syntax
2114 * ssize_t recvmsg(int socket, struct msghdr *message,
2117 * socket - the socket descriptor of the endpoint.
2118 * message - pointer to the msghdr structure which contains a single
2119 * user message and possibly some ancillary data.
2121 * See Section 5 for complete description of the data
2124 * flags - flags sent or received with the user message, see Section
2125 * 5 for complete description of the flags.
2127 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2128 int noblock, int flags, int *addr_len)
2130 struct sctp_ulpevent *event = NULL;
2131 struct sctp_sock *sp = sctp_sk(sk);
2132 struct sk_buff *skb, *head_skb;
2137 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2138 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2143 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2144 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2149 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2153 /* Get the total length of the skb including any skb's in the
2162 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2164 event = sctp_skb2event(skb);
2169 if (event->chunk && event->chunk->head_skb)
2170 head_skb = event->chunk->head_skb;
2173 sock_recv_ts_and_drops(msg, sk, head_skb);
2174 if (sctp_ulpevent_is_notification(event)) {
2175 msg->msg_flags |= MSG_NOTIFICATION;
2176 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2178 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2181 /* Check if we allow SCTP_NXTINFO. */
2182 if (sp->recvnxtinfo)
2183 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2184 /* Check if we allow SCTP_RCVINFO. */
2185 if (sp->recvrcvinfo)
2186 sctp_ulpevent_read_rcvinfo(event, msg);
2187 /* Check if we allow SCTP_SNDRCVINFO. */
2188 if (sp->subscribe.sctp_data_io_event)
2189 sctp_ulpevent_read_sndrcvinfo(event, msg);
2193 /* If skb's length exceeds the user's buffer, update the skb and
2194 * push it back to the receive_queue so that the next call to
2195 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2197 if (skb_len > copied) {
2198 msg->msg_flags &= ~MSG_EOR;
2199 if (flags & MSG_PEEK)
2201 sctp_skb_pull(skb, copied);
2202 skb_queue_head(&sk->sk_receive_queue, skb);
2204 /* When only partial message is copied to the user, increase
2205 * rwnd by that amount. If all the data in the skb is read,
2206 * rwnd is updated when the event is freed.
2208 if (!sctp_ulpevent_is_notification(event))
2209 sctp_assoc_rwnd_increase(event->asoc, copied);
2211 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2212 (event->msg_flags & MSG_EOR))
2213 msg->msg_flags |= MSG_EOR;
2215 msg->msg_flags &= ~MSG_EOR;
2218 if (flags & MSG_PEEK) {
2219 /* Release the skb reference acquired after peeking the skb in
2220 * sctp_skb_recv_datagram().
2224 /* Free the event which includes releasing the reference to
2225 * the owner of the skb, freeing the skb and updating the
2228 sctp_ulpevent_free(event);
2235 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2237 * This option is a on/off flag. If enabled no SCTP message
2238 * fragmentation will be performed. Instead if a message being sent
2239 * exceeds the current PMTU size, the message will NOT be sent and
2240 * instead a error will be indicated to the user.
2242 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2243 char __user *optval,
2244 unsigned int optlen)
2248 if (optlen < sizeof(int))
2251 if (get_user(val, (int __user *)optval))
2254 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2259 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2260 unsigned int optlen)
2262 struct sctp_association *asoc;
2263 struct sctp_ulpevent *event;
2265 if (optlen > sizeof(struct sctp_event_subscribe))
2267 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2270 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2271 * if there is no data to be sent or retransmit, the stack will
2272 * immediately send up this notification.
2274 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2275 &sctp_sk(sk)->subscribe)) {
2276 asoc = sctp_id2assoc(sk, 0);
2278 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2279 event = sctp_ulpevent_make_sender_dry_event(asoc,
2284 sctp_ulpq_tail_event(&asoc->ulpq, event);
2291 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2293 * This socket option is applicable to the UDP-style socket only. When
2294 * set it will cause associations that are idle for more than the
2295 * specified number of seconds to automatically close. An association
2296 * being idle is defined an association that has NOT sent or received
2297 * user data. The special value of '0' indicates that no automatic
2298 * close of any associations should be performed. The option expects an
2299 * integer defining the number of seconds of idle time before an
2300 * association is closed.
2302 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2303 unsigned int optlen)
2305 struct sctp_sock *sp = sctp_sk(sk);
2306 struct net *net = sock_net(sk);
2308 /* Applicable to UDP-style socket only */
2309 if (sctp_style(sk, TCP))
2311 if (optlen != sizeof(int))
2313 if (copy_from_user(&sp->autoclose, optval, optlen))
2316 if (sp->autoclose > net->sctp.max_autoclose)
2317 sp->autoclose = net->sctp.max_autoclose;
2322 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2324 * Applications can enable or disable heartbeats for any peer address of
2325 * an association, modify an address's heartbeat interval, force a
2326 * heartbeat to be sent immediately, and adjust the address's maximum
2327 * number of retransmissions sent before an address is considered
2328 * unreachable. The following structure is used to access and modify an
2329 * address's parameters:
2331 * struct sctp_paddrparams {
2332 * sctp_assoc_t spp_assoc_id;
2333 * struct sockaddr_storage spp_address;
2334 * uint32_t spp_hbinterval;
2335 * uint16_t spp_pathmaxrxt;
2336 * uint32_t spp_pathmtu;
2337 * uint32_t spp_sackdelay;
2338 * uint32_t spp_flags;
2341 * spp_assoc_id - (one-to-many style socket) This is filled in the
2342 * application, and identifies the association for
2344 * spp_address - This specifies which address is of interest.
2345 * spp_hbinterval - This contains the value of the heartbeat interval,
2346 * in milliseconds. If a value of zero
2347 * is present in this field then no changes are to
2348 * be made to this parameter.
2349 * spp_pathmaxrxt - This contains the maximum number of
2350 * retransmissions before this address shall be
2351 * considered unreachable. If a value of zero
2352 * is present in this field then no changes are to
2353 * be made to this parameter.
2354 * spp_pathmtu - When Path MTU discovery is disabled the value
2355 * specified here will be the "fixed" path mtu.
2356 * Note that if the spp_address field is empty
2357 * then all associations on this address will
2358 * have this fixed path mtu set upon them.
2360 * spp_sackdelay - When delayed sack is enabled, this value specifies
2361 * the number of milliseconds that sacks will be delayed
2362 * for. This value will apply to all addresses of an
2363 * association if the spp_address field is empty. Note
2364 * also, that if delayed sack is enabled and this
2365 * value is set to 0, no change is made to the last
2366 * recorded delayed sack timer value.
2368 * spp_flags - These flags are used to control various features
2369 * on an association. The flag field may contain
2370 * zero or more of the following options.
2372 * SPP_HB_ENABLE - Enable heartbeats on the
2373 * specified address. Note that if the address
2374 * field is empty all addresses for the association
2375 * have heartbeats enabled upon them.
2377 * SPP_HB_DISABLE - Disable heartbeats on the
2378 * speicifed address. Note that if the address
2379 * field is empty all addresses for the association
2380 * will have their heartbeats disabled. Note also
2381 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2382 * mutually exclusive, only one of these two should
2383 * be specified. Enabling both fields will have
2384 * undetermined results.
2386 * SPP_HB_DEMAND - Request a user initiated heartbeat
2387 * to be made immediately.
2389 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2390 * heartbeat delayis to be set to the value of 0
2393 * SPP_PMTUD_ENABLE - This field will enable PMTU
2394 * discovery upon the specified address. Note that
2395 * if the address feild is empty then all addresses
2396 * on the association are effected.
2398 * SPP_PMTUD_DISABLE - This field will disable PMTU
2399 * discovery upon the specified address. Note that
2400 * if the address feild is empty then all addresses
2401 * on the association are effected. Not also that
2402 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2403 * exclusive. Enabling both will have undetermined
2406 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2407 * on delayed sack. The time specified in spp_sackdelay
2408 * is used to specify the sack delay for this address. Note
2409 * that if spp_address is empty then all addresses will
2410 * enable delayed sack and take on the sack delay
2411 * value specified in spp_sackdelay.
2412 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2413 * off delayed sack. If the spp_address field is blank then
2414 * delayed sack is disabled for the entire association. Note
2415 * also that this field is mutually exclusive to
2416 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2419 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2420 struct sctp_transport *trans,
2421 struct sctp_association *asoc,
2422 struct sctp_sock *sp,
2425 int sackdelay_change)
2429 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2430 struct net *net = sock_net(trans->asoc->base.sk);
2432 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2437 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2438 * this field is ignored. Note also that a value of zero indicates
2439 * the current setting should be left unchanged.
2441 if (params->spp_flags & SPP_HB_ENABLE) {
2443 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2444 * set. This lets us use 0 value when this flag
2447 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2448 params->spp_hbinterval = 0;
2450 if (params->spp_hbinterval ||
2451 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2454 msecs_to_jiffies(params->spp_hbinterval);
2457 msecs_to_jiffies(params->spp_hbinterval);
2459 sp->hbinterval = params->spp_hbinterval;
2466 trans->param_flags =
2467 (trans->param_flags & ~SPP_HB) | hb_change;
2470 (asoc->param_flags & ~SPP_HB) | hb_change;
2473 (sp->param_flags & ~SPP_HB) | hb_change;
2477 /* When Path MTU discovery is disabled the value specified here will
2478 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2479 * include the flag SPP_PMTUD_DISABLE for this field to have any
2482 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2484 trans->pathmtu = params->spp_pathmtu;
2485 sctp_assoc_sync_pmtu(asoc);
2487 asoc->pathmtu = params->spp_pathmtu;
2489 sp->pathmtu = params->spp_pathmtu;
2495 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2496 (params->spp_flags & SPP_PMTUD_ENABLE);
2497 trans->param_flags =
2498 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2500 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2501 sctp_assoc_sync_pmtu(asoc);
2505 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2508 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2512 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2513 * value of this field is ignored. Note also that a value of zero
2514 * indicates the current setting should be left unchanged.
2516 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2519 msecs_to_jiffies(params->spp_sackdelay);
2522 msecs_to_jiffies(params->spp_sackdelay);
2524 sp->sackdelay = params->spp_sackdelay;
2528 if (sackdelay_change) {
2530 trans->param_flags =
2531 (trans->param_flags & ~SPP_SACKDELAY) |
2535 (asoc->param_flags & ~SPP_SACKDELAY) |
2539 (sp->param_flags & ~SPP_SACKDELAY) |
2544 /* Note that a value of zero indicates the current setting should be
2547 if (params->spp_pathmaxrxt) {
2549 trans->pathmaxrxt = params->spp_pathmaxrxt;
2551 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2553 sp->pathmaxrxt = params->spp_pathmaxrxt;
2560 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2561 char __user *optval,
2562 unsigned int optlen)
2564 struct sctp_paddrparams params;
2565 struct sctp_transport *trans = NULL;
2566 struct sctp_association *asoc = NULL;
2567 struct sctp_sock *sp = sctp_sk(sk);
2569 int hb_change, pmtud_change, sackdelay_change;
2571 if (optlen != sizeof(struct sctp_paddrparams))
2574 if (copy_from_user(¶ms, optval, optlen))
2577 /* Validate flags and value parameters. */
2578 hb_change = params.spp_flags & SPP_HB;
2579 pmtud_change = params.spp_flags & SPP_PMTUD;
2580 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2582 if (hb_change == SPP_HB ||
2583 pmtud_change == SPP_PMTUD ||
2584 sackdelay_change == SPP_SACKDELAY ||
2585 params.spp_sackdelay > 500 ||
2586 (params.spp_pathmtu &&
2587 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2590 /* If an address other than INADDR_ANY is specified, and
2591 * no transport is found, then the request is invalid.
2593 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2594 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2595 params.spp_assoc_id);
2600 /* Get association, if assoc_id != 0 and the socket is a one
2601 * to many style socket, and an association was not found, then
2602 * the id was invalid.
2604 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2605 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2608 /* Heartbeat demand can only be sent on a transport or
2609 * association, but not a socket.
2611 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2614 /* Process parameters. */
2615 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2616 hb_change, pmtud_change,
2622 /* If changes are for association, also apply parameters to each
2625 if (!trans && asoc) {
2626 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2628 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2629 hb_change, pmtud_change,
2637 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2639 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2642 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2644 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2648 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2650 * This option will effect the way delayed acks are performed. This
2651 * option allows you to get or set the delayed ack time, in
2652 * milliseconds. It also allows changing the delayed ack frequency.
2653 * Changing the frequency to 1 disables the delayed sack algorithm. If
2654 * the assoc_id is 0, then this sets or gets the endpoints default
2655 * values. If the assoc_id field is non-zero, then the set or get
2656 * effects the specified association for the one to many model (the
2657 * assoc_id field is ignored by the one to one model). Note that if
2658 * sack_delay or sack_freq are 0 when setting this option, then the
2659 * current values will remain unchanged.
2661 * struct sctp_sack_info {
2662 * sctp_assoc_t sack_assoc_id;
2663 * uint32_t sack_delay;
2664 * uint32_t sack_freq;
2667 * sack_assoc_id - This parameter, indicates which association the user
2668 * is performing an action upon. Note that if this field's value is
2669 * zero then the endpoints default value is changed (effecting future
2670 * associations only).
2672 * sack_delay - This parameter contains the number of milliseconds that
2673 * the user is requesting the delayed ACK timer be set to. Note that
2674 * this value is defined in the standard to be between 200 and 500
2677 * sack_freq - This parameter contains the number of packets that must
2678 * be received before a sack is sent without waiting for the delay
2679 * timer to expire. The default value for this is 2, setting this
2680 * value to 1 will disable the delayed sack algorithm.
2683 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2684 char __user *optval, unsigned int optlen)
2686 struct sctp_sack_info params;
2687 struct sctp_transport *trans = NULL;
2688 struct sctp_association *asoc = NULL;
2689 struct sctp_sock *sp = sctp_sk(sk);
2691 if (optlen == sizeof(struct sctp_sack_info)) {
2692 if (copy_from_user(¶ms, optval, optlen))
2695 if (params.sack_delay == 0 && params.sack_freq == 0)
2697 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2698 pr_warn_ratelimited(DEPRECATED
2700 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2701 "Use struct sctp_sack_info instead\n",
2702 current->comm, task_pid_nr(current));
2703 if (copy_from_user(¶ms, optval, optlen))
2706 if (params.sack_delay == 0)
2707 params.sack_freq = 1;
2709 params.sack_freq = 0;
2713 /* Validate value parameter. */
2714 if (params.sack_delay > 500)
2717 /* Get association, if sack_assoc_id != 0 and the socket is a one
2718 * to many style socket, and an association was not found, then
2719 * the id was invalid.
2721 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2722 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2725 if (params.sack_delay) {
2728 msecs_to_jiffies(params.sack_delay);
2730 sctp_spp_sackdelay_enable(asoc->param_flags);
2732 sp->sackdelay = params.sack_delay;
2734 sctp_spp_sackdelay_enable(sp->param_flags);
2738 if (params.sack_freq == 1) {
2741 sctp_spp_sackdelay_disable(asoc->param_flags);
2744 sctp_spp_sackdelay_disable(sp->param_flags);
2746 } else if (params.sack_freq > 1) {
2748 asoc->sackfreq = params.sack_freq;
2750 sctp_spp_sackdelay_enable(asoc->param_flags);
2752 sp->sackfreq = params.sack_freq;
2754 sctp_spp_sackdelay_enable(sp->param_flags);
2758 /* If change is for association, also apply to each transport. */
2760 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2762 if (params.sack_delay) {
2764 msecs_to_jiffies(params.sack_delay);
2765 trans->param_flags =
2766 sctp_spp_sackdelay_enable(trans->param_flags);
2768 if (params.sack_freq == 1) {
2769 trans->param_flags =
2770 sctp_spp_sackdelay_disable(trans->param_flags);
2771 } else if (params.sack_freq > 1) {
2772 trans->sackfreq = params.sack_freq;
2773 trans->param_flags =
2774 sctp_spp_sackdelay_enable(trans->param_flags);
2782 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2784 * Applications can specify protocol parameters for the default association
2785 * initialization. The option name argument to setsockopt() and getsockopt()
2788 * Setting initialization parameters is effective only on an unconnected
2789 * socket (for UDP-style sockets only future associations are effected
2790 * by the change). With TCP-style sockets, this option is inherited by
2791 * sockets derived from a listener socket.
2793 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2795 struct sctp_initmsg sinit;
2796 struct sctp_sock *sp = sctp_sk(sk);
2798 if (optlen != sizeof(struct sctp_initmsg))
2800 if (copy_from_user(&sinit, optval, optlen))
2803 if (sinit.sinit_num_ostreams)
2804 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2805 if (sinit.sinit_max_instreams)
2806 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2807 if (sinit.sinit_max_attempts)
2808 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2809 if (sinit.sinit_max_init_timeo)
2810 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2816 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2818 * Applications that wish to use the sendto() system call may wish to
2819 * specify a default set of parameters that would normally be supplied
2820 * through the inclusion of ancillary data. This socket option allows
2821 * such an application to set the default sctp_sndrcvinfo structure.
2822 * The application that wishes to use this socket option simply passes
2823 * in to this call the sctp_sndrcvinfo structure defined in Section
2824 * 5.2.2) The input parameters accepted by this call include
2825 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2826 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2827 * to this call if the caller is using the UDP model.
2829 static int sctp_setsockopt_default_send_param(struct sock *sk,
2830 char __user *optval,
2831 unsigned int optlen)
2833 struct sctp_sock *sp = sctp_sk(sk);
2834 struct sctp_association *asoc;
2835 struct sctp_sndrcvinfo info;
2837 if (optlen != sizeof(info))
2839 if (copy_from_user(&info, optval, optlen))
2841 if (info.sinfo_flags &
2842 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2843 SCTP_ABORT | SCTP_EOF))
2846 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2847 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2850 asoc->default_stream = info.sinfo_stream;
2851 asoc->default_flags = info.sinfo_flags;
2852 asoc->default_ppid = info.sinfo_ppid;
2853 asoc->default_context = info.sinfo_context;
2854 asoc->default_timetolive = info.sinfo_timetolive;
2856 sp->default_stream = info.sinfo_stream;
2857 sp->default_flags = info.sinfo_flags;
2858 sp->default_ppid = info.sinfo_ppid;
2859 sp->default_context = info.sinfo_context;
2860 sp->default_timetolive = info.sinfo_timetolive;
2866 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2867 * (SCTP_DEFAULT_SNDINFO)
2869 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2870 char __user *optval,
2871 unsigned int optlen)
2873 struct sctp_sock *sp = sctp_sk(sk);
2874 struct sctp_association *asoc;
2875 struct sctp_sndinfo info;
2877 if (optlen != sizeof(info))
2879 if (copy_from_user(&info, optval, optlen))
2881 if (info.snd_flags &
2882 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2883 SCTP_ABORT | SCTP_EOF))
2886 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2887 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2890 asoc->default_stream = info.snd_sid;
2891 asoc->default_flags = info.snd_flags;
2892 asoc->default_ppid = info.snd_ppid;
2893 asoc->default_context = info.snd_context;
2895 sp->default_stream = info.snd_sid;
2896 sp->default_flags = info.snd_flags;
2897 sp->default_ppid = info.snd_ppid;
2898 sp->default_context = info.snd_context;
2904 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2906 * Requests that the local SCTP stack use the enclosed peer address as
2907 * the association primary. The enclosed address must be one of the
2908 * association peer's addresses.
2910 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2911 unsigned int optlen)
2913 struct sctp_prim prim;
2914 struct sctp_transport *trans;
2916 if (optlen != sizeof(struct sctp_prim))
2919 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2922 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2926 sctp_assoc_set_primary(trans->asoc, trans);
2932 * 7.1.5 SCTP_NODELAY
2934 * Turn on/off any Nagle-like algorithm. This means that packets are
2935 * generally sent as soon as possible and no unnecessary delays are
2936 * introduced, at the cost of more packets in the network. Expects an
2937 * integer boolean flag.
2939 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2940 unsigned int optlen)
2944 if (optlen < sizeof(int))
2946 if (get_user(val, (int __user *)optval))
2949 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2955 * 7.1.1 SCTP_RTOINFO
2957 * The protocol parameters used to initialize and bound retransmission
2958 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2959 * and modify these parameters.
2960 * All parameters are time values, in milliseconds. A value of 0, when
2961 * modifying the parameters, indicates that the current value should not
2965 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2967 struct sctp_rtoinfo rtoinfo;
2968 struct sctp_association *asoc;
2969 unsigned long rto_min, rto_max;
2970 struct sctp_sock *sp = sctp_sk(sk);
2972 if (optlen != sizeof (struct sctp_rtoinfo))
2975 if (copy_from_user(&rtoinfo, optval, optlen))
2978 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2980 /* Set the values to the specific association */
2981 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2984 rto_max = rtoinfo.srto_max;
2985 rto_min = rtoinfo.srto_min;
2988 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2990 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2993 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2995 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2997 if (rto_min > rto_max)
3001 if (rtoinfo.srto_initial != 0)
3003 msecs_to_jiffies(rtoinfo.srto_initial);
3004 asoc->rto_max = rto_max;
3005 asoc->rto_min = rto_min;
3007 /* If there is no association or the association-id = 0
3008 * set the values to the endpoint.
3010 if (rtoinfo.srto_initial != 0)
3011 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3012 sp->rtoinfo.srto_max = rto_max;
3013 sp->rtoinfo.srto_min = rto_min;
3021 * 7.1.2 SCTP_ASSOCINFO
3023 * This option is used to tune the maximum retransmission attempts
3024 * of the association.
3025 * Returns an error if the new association retransmission value is
3026 * greater than the sum of the retransmission value of the peer.
3027 * See [SCTP] for more information.
3030 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3033 struct sctp_assocparams assocparams;
3034 struct sctp_association *asoc;
3036 if (optlen != sizeof(struct sctp_assocparams))
3038 if (copy_from_user(&assocparams, optval, optlen))
3041 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3043 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3046 /* Set the values to the specific association */
3048 if (assocparams.sasoc_asocmaxrxt != 0) {
3051 struct sctp_transport *peer_addr;
3053 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3055 path_sum += peer_addr->pathmaxrxt;
3059 /* Only validate asocmaxrxt if we have more than
3060 * one path/transport. We do this because path
3061 * retransmissions are only counted when we have more
3065 assocparams.sasoc_asocmaxrxt > path_sum)
3068 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3071 if (assocparams.sasoc_cookie_life != 0)
3072 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3074 /* Set the values to the endpoint */
3075 struct sctp_sock *sp = sctp_sk(sk);
3077 if (assocparams.sasoc_asocmaxrxt != 0)
3078 sp->assocparams.sasoc_asocmaxrxt =
3079 assocparams.sasoc_asocmaxrxt;
3080 if (assocparams.sasoc_cookie_life != 0)
3081 sp->assocparams.sasoc_cookie_life =
3082 assocparams.sasoc_cookie_life;
3088 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3090 * This socket option is a boolean flag which turns on or off mapped V4
3091 * addresses. If this option is turned on and the socket is type
3092 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3093 * If this option is turned off, then no mapping will be done of V4
3094 * addresses and a user will receive both PF_INET6 and PF_INET type
3095 * addresses on the socket.
3097 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3100 struct sctp_sock *sp = sctp_sk(sk);
3102 if (optlen < sizeof(int))
3104 if (get_user(val, (int __user *)optval))
3115 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3116 * This option will get or set the maximum size to put in any outgoing
3117 * SCTP DATA chunk. If a message is larger than this size it will be
3118 * fragmented by SCTP into the specified size. Note that the underlying
3119 * SCTP implementation may fragment into smaller sized chunks when the
3120 * PMTU of the underlying association is smaller than the value set by
3121 * the user. The default value for this option is '0' which indicates
3122 * the user is NOT limiting fragmentation and only the PMTU will effect
3123 * SCTP's choice of DATA chunk size. Note also that values set larger
3124 * than the maximum size of an IP datagram will effectively let SCTP
3125 * control fragmentation (i.e. the same as setting this option to 0).
3127 * The following structure is used to access and modify this parameter:
3129 * struct sctp_assoc_value {
3130 * sctp_assoc_t assoc_id;
3131 * uint32_t assoc_value;
3134 * assoc_id: This parameter is ignored for one-to-one style sockets.
3135 * For one-to-many style sockets this parameter indicates which
3136 * association the user is performing an action upon. Note that if
3137 * this field's value is zero then the endpoints default value is
3138 * changed (effecting future associations only).
3139 * assoc_value: This parameter specifies the maximum size in bytes.
3141 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3143 struct sctp_assoc_value params;
3144 struct sctp_association *asoc;
3145 struct sctp_sock *sp = sctp_sk(sk);
3148 if (optlen == sizeof(int)) {
3149 pr_warn_ratelimited(DEPRECATED
3151 "Use of int in maxseg socket option.\n"
3152 "Use struct sctp_assoc_value instead\n",
3153 current->comm, task_pid_nr(current));
3154 if (copy_from_user(&val, optval, optlen))
3156 params.assoc_id = 0;
3157 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3158 if (copy_from_user(¶ms, optval, optlen))
3160 val = params.assoc_value;
3164 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
3167 asoc = sctp_id2assoc(sk, params.assoc_id);
3168 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3173 val = asoc->pathmtu;
3174 val -= sp->pf->af->net_header_len;
3175 val -= sizeof(struct sctphdr) +
3176 sizeof(struct sctp_data_chunk);
3178 asoc->user_frag = val;
3179 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
3181 sp->user_frag = val;
3189 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3191 * Requests that the peer mark the enclosed address as the association
3192 * primary. The enclosed address must be one of the association's
3193 * locally bound addresses. The following structure is used to make a
3194 * set primary request:
3196 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3197 unsigned int optlen)
3199 struct net *net = sock_net(sk);
3200 struct sctp_sock *sp;
3201 struct sctp_association *asoc = NULL;
3202 struct sctp_setpeerprim prim;
3203 struct sctp_chunk *chunk;
3209 if (!net->sctp.addip_enable)
3212 if (optlen != sizeof(struct sctp_setpeerprim))
3215 if (copy_from_user(&prim, optval, optlen))
3218 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3222 if (!asoc->peer.asconf_capable)
3225 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3228 if (!sctp_state(asoc, ESTABLISHED))
3231 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3235 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3236 return -EADDRNOTAVAIL;
3238 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3239 return -EADDRNOTAVAIL;
3241 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3242 chunk = sctp_make_asconf_set_prim(asoc,
3243 (union sctp_addr *)&prim.sspp_addr);
3247 err = sctp_send_asconf(asoc, chunk);
3249 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3254 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3255 unsigned int optlen)
3257 struct sctp_setadaptation adaptation;
3259 if (optlen != sizeof(struct sctp_setadaptation))
3261 if (copy_from_user(&adaptation, optval, optlen))
3264 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3270 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3272 * The context field in the sctp_sndrcvinfo structure is normally only
3273 * used when a failed message is retrieved holding the value that was
3274 * sent down on the actual send call. This option allows the setting of
3275 * a default context on an association basis that will be received on
3276 * reading messages from the peer. This is especially helpful in the
3277 * one-2-many model for an application to keep some reference to an
3278 * internal state machine that is processing messages on the
3279 * association. Note that the setting of this value only effects
3280 * received messages from the peer and does not effect the value that is
3281 * saved with outbound messages.
3283 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3284 unsigned int optlen)
3286 struct sctp_assoc_value params;
3287 struct sctp_sock *sp;
3288 struct sctp_association *asoc;
3290 if (optlen != sizeof(struct sctp_assoc_value))
3292 if (copy_from_user(¶ms, optval, optlen))
3297 if (params.assoc_id != 0) {
3298 asoc = sctp_id2assoc(sk, params.assoc_id);
3301 asoc->default_rcv_context = params.assoc_value;
3303 sp->default_rcv_context = params.assoc_value;
3310 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3312 * This options will at a minimum specify if the implementation is doing
3313 * fragmented interleave. Fragmented interleave, for a one to many
3314 * socket, is when subsequent calls to receive a message may return
3315 * parts of messages from different associations. Some implementations
3316 * may allow you to turn this value on or off. If so, when turned off,
3317 * no fragment interleave will occur (which will cause a head of line
3318 * blocking amongst multiple associations sharing the same one to many
3319 * socket). When this option is turned on, then each receive call may
3320 * come from a different association (thus the user must receive data
3321 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3322 * association each receive belongs to.
3324 * This option takes a boolean value. A non-zero value indicates that
3325 * fragmented interleave is on. A value of zero indicates that
3326 * fragmented interleave is off.
3328 * Note that it is important that an implementation that allows this
3329 * option to be turned on, have it off by default. Otherwise an unaware
3330 * application using the one to many model may become confused and act
3333 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3334 char __user *optval,
3335 unsigned int optlen)
3339 if (optlen != sizeof(int))
3341 if (get_user(val, (int __user *)optval))
3344 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3350 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3351 * (SCTP_PARTIAL_DELIVERY_POINT)
3353 * This option will set or get the SCTP partial delivery point. This
3354 * point is the size of a message where the partial delivery API will be
3355 * invoked to help free up rwnd space for the peer. Setting this to a
3356 * lower value will cause partial deliveries to happen more often. The
3357 * calls argument is an integer that sets or gets the partial delivery
3358 * point. Note also that the call will fail if the user attempts to set
3359 * this value larger than the socket receive buffer size.
3361 * Note that any single message having a length smaller than or equal to
3362 * the SCTP partial delivery point will be delivered in one single read
3363 * call as long as the user provided buffer is large enough to hold the
3366 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3367 char __user *optval,
3368 unsigned int optlen)
3372 if (optlen != sizeof(u32))
3374 if (get_user(val, (int __user *)optval))
3377 /* Note: We double the receive buffer from what the user sets
3378 * it to be, also initial rwnd is based on rcvbuf/2.
3380 if (val > (sk->sk_rcvbuf >> 1))
3383 sctp_sk(sk)->pd_point = val;
3385 return 0; /* is this the right error code? */
3389 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3391 * This option will allow a user to change the maximum burst of packets
3392 * that can be emitted by this association. Note that the default value
3393 * is 4, and some implementations may restrict this setting so that it
3394 * can only be lowered.
3396 * NOTE: This text doesn't seem right. Do this on a socket basis with
3397 * future associations inheriting the socket value.
3399 static int sctp_setsockopt_maxburst(struct sock *sk,
3400 char __user *optval,
3401 unsigned int optlen)
3403 struct sctp_assoc_value params;
3404 struct sctp_sock *sp;
3405 struct sctp_association *asoc;
3409 if (optlen == sizeof(int)) {
3410 pr_warn_ratelimited(DEPRECATED
3412 "Use of int in max_burst socket option deprecated.\n"
3413 "Use struct sctp_assoc_value instead\n",
3414 current->comm, task_pid_nr(current));
3415 if (copy_from_user(&val, optval, optlen))
3417 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3418 if (copy_from_user(¶ms, optval, optlen))
3420 val = params.assoc_value;
3421 assoc_id = params.assoc_id;
3427 if (assoc_id != 0) {
3428 asoc = sctp_id2assoc(sk, assoc_id);
3431 asoc->max_burst = val;
3433 sp->max_burst = val;
3439 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3441 * This set option adds a chunk type that the user is requesting to be
3442 * received only in an authenticated way. Changes to the list of chunks
3443 * will only effect future associations on the socket.
3445 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3446 char __user *optval,
3447 unsigned int optlen)
3449 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3450 struct sctp_authchunk val;
3452 if (!ep->auth_enable)
3455 if (optlen != sizeof(struct sctp_authchunk))
3457 if (copy_from_user(&val, optval, optlen))
3460 switch (val.sauth_chunk) {
3462 case SCTP_CID_INIT_ACK:
3463 case SCTP_CID_SHUTDOWN_COMPLETE:
3468 /* add this chunk id to the endpoint */
3469 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3473 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3475 * This option gets or sets the list of HMAC algorithms that the local
3476 * endpoint requires the peer to use.
3478 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3479 char __user *optval,
3480 unsigned int optlen)
3482 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3483 struct sctp_hmacalgo *hmacs;
3487 if (!ep->auth_enable)
3490 if (optlen < sizeof(struct sctp_hmacalgo))
3493 hmacs = memdup_user(optval, optlen);
3495 return PTR_ERR(hmacs);
3497 idents = hmacs->shmac_num_idents;
3498 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3499 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3504 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3511 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3513 * This option will set a shared secret key which is used to build an
3514 * association shared key.
3516 static int sctp_setsockopt_auth_key(struct sock *sk,
3517 char __user *optval,
3518 unsigned int optlen)
3520 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3521 struct sctp_authkey *authkey;
3522 struct sctp_association *asoc;
3525 if (!ep->auth_enable)
3528 if (optlen <= sizeof(struct sctp_authkey))
3531 authkey = memdup_user(optval, optlen);
3532 if (IS_ERR(authkey))
3533 return PTR_ERR(authkey);
3535 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3540 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3541 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3546 ret = sctp_auth_set_key(ep, asoc, authkey);
3553 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3555 * This option will get or set the active shared key to be used to build
3556 * the association shared key.
3558 static int sctp_setsockopt_active_key(struct sock *sk,
3559 char __user *optval,
3560 unsigned int optlen)
3562 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3563 struct sctp_authkeyid val;
3564 struct sctp_association *asoc;
3566 if (!ep->auth_enable)
3569 if (optlen != sizeof(struct sctp_authkeyid))
3571 if (copy_from_user(&val, optval, optlen))
3574 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3575 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3578 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3582 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3584 * This set option will delete a shared secret key from use.
3586 static int sctp_setsockopt_del_key(struct sock *sk,
3587 char __user *optval,
3588 unsigned int optlen)
3590 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3591 struct sctp_authkeyid val;
3592 struct sctp_association *asoc;
3594 if (!ep->auth_enable)
3597 if (optlen != sizeof(struct sctp_authkeyid))
3599 if (copy_from_user(&val, optval, optlen))
3602 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3603 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3606 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3611 * 8.1.23 SCTP_AUTO_ASCONF
3613 * This option will enable or disable the use of the automatic generation of
3614 * ASCONF chunks to add and delete addresses to an existing association. Note
3615 * that this option has two caveats namely: a) it only affects sockets that
3616 * are bound to all addresses available to the SCTP stack, and b) the system
3617 * administrator may have an overriding control that turns the ASCONF feature
3618 * off no matter what setting the socket option may have.
3619 * This option expects an integer boolean flag, where a non-zero value turns on
3620 * the option, and a zero value turns off the option.
3621 * Note. In this implementation, socket operation overrides default parameter
3622 * being set by sysctl as well as FreeBSD implementation
3624 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3625 unsigned int optlen)
3628 struct sctp_sock *sp = sctp_sk(sk);
3630 if (optlen < sizeof(int))
3632 if (get_user(val, (int __user *)optval))
3634 if (!sctp_is_ep_boundall(sk) && val)
3636 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3639 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3640 if (val == 0 && sp->do_auto_asconf) {
3641 list_del(&sp->auto_asconf_list);
3642 sp->do_auto_asconf = 0;
3643 } else if (val && !sp->do_auto_asconf) {
3644 list_add_tail(&sp->auto_asconf_list,
3645 &sock_net(sk)->sctp.auto_asconf_splist);
3646 sp->do_auto_asconf = 1;
3648 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3653 * SCTP_PEER_ADDR_THLDS
3655 * This option allows us to alter the partially failed threshold for one or all
3656 * transports in an association. See Section 6.1 of:
3657 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3659 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3660 char __user *optval,
3661 unsigned int optlen)
3663 struct sctp_paddrthlds val;
3664 struct sctp_transport *trans;
3665 struct sctp_association *asoc;
3667 if (optlen < sizeof(struct sctp_paddrthlds))
3669 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3670 sizeof(struct sctp_paddrthlds)))
3674 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3675 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3678 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3680 if (val.spt_pathmaxrxt)
3681 trans->pathmaxrxt = val.spt_pathmaxrxt;
3682 trans->pf_retrans = val.spt_pathpfthld;
3685 if (val.spt_pathmaxrxt)
3686 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3687 asoc->pf_retrans = val.spt_pathpfthld;
3689 trans = sctp_addr_id2transport(sk, &val.spt_address,
3694 if (val.spt_pathmaxrxt)
3695 trans->pathmaxrxt = val.spt_pathmaxrxt;
3696 trans->pf_retrans = val.spt_pathpfthld;
3702 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3703 char __user *optval,
3704 unsigned int optlen)
3708 if (optlen < sizeof(int))
3710 if (get_user(val, (int __user *) optval))
3713 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3718 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3719 char __user *optval,
3720 unsigned int optlen)
3724 if (optlen < sizeof(int))
3726 if (get_user(val, (int __user *) optval))
3729 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3734 static int sctp_setsockopt_pr_supported(struct sock *sk,
3735 char __user *optval,
3736 unsigned int optlen)
3738 struct sctp_assoc_value params;
3739 struct sctp_association *asoc;
3740 int retval = -EINVAL;
3742 if (optlen != sizeof(params))
3745 if (copy_from_user(¶ms, optval, optlen)) {
3750 asoc = sctp_id2assoc(sk, params.assoc_id);
3752 asoc->prsctp_enable = !!params.assoc_value;
3753 } else if (!params.assoc_id) {
3754 struct sctp_sock *sp = sctp_sk(sk);
3756 sp->ep->prsctp_enable = !!params.assoc_value;
3767 static int sctp_setsockopt_default_prinfo(struct sock *sk,
3768 char __user *optval,
3769 unsigned int optlen)
3771 struct sctp_default_prinfo info;
3772 struct sctp_association *asoc;
3773 int retval = -EINVAL;
3775 if (optlen != sizeof(info))
3778 if (copy_from_user(&info, optval, sizeof(info))) {
3783 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3786 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3789 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3791 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3792 asoc->default_timetolive = info.pr_value;
3793 } else if (!info.pr_assoc_id) {
3794 struct sctp_sock *sp = sctp_sk(sk);
3796 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3797 sp->default_timetolive = info.pr_value;
3808 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3809 char __user *optval,
3810 unsigned int optlen)
3812 struct sctp_assoc_value params;
3813 struct sctp_association *asoc;
3814 int retval = -EINVAL;
3816 if (optlen != sizeof(params))
3819 if (copy_from_user(¶ms, optval, optlen)) {
3824 asoc = sctp_id2assoc(sk, params.assoc_id);
3826 asoc->reconf_enable = !!params.assoc_value;
3827 } else if (!params.assoc_id) {
3828 struct sctp_sock *sp = sctp_sk(sk);
3830 sp->ep->reconf_enable = !!params.assoc_value;
3841 static int sctp_setsockopt_enable_strreset(struct sock *sk,
3842 char __user *optval,
3843 unsigned int optlen)
3845 struct sctp_assoc_value params;
3846 struct sctp_association *asoc;
3847 int retval = -EINVAL;
3849 if (optlen != sizeof(params))
3852 if (copy_from_user(¶ms, optval, optlen)) {
3857 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3860 asoc = sctp_id2assoc(sk, params.assoc_id);
3862 asoc->strreset_enable = params.assoc_value;
3863 } else if (!params.assoc_id) {
3864 struct sctp_sock *sp = sctp_sk(sk);
3866 sp->ep->strreset_enable = params.assoc_value;
3877 static int sctp_setsockopt_reset_streams(struct sock *sk,
3878 char __user *optval,
3879 unsigned int optlen)
3881 struct sctp_reset_streams *params;
3882 struct sctp_association *asoc;
3883 int retval = -EINVAL;
3885 if (optlen < sizeof(struct sctp_reset_streams))
3888 params = memdup_user(optval, optlen);
3890 return PTR_ERR(params);
3892 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3896 retval = sctp_send_reset_streams(asoc, params);
3903 static int sctp_setsockopt_reset_assoc(struct sock *sk,
3904 char __user *optval,
3905 unsigned int optlen)
3907 struct sctp_association *asoc;
3908 sctp_assoc_t associd;
3909 int retval = -EINVAL;
3911 if (optlen != sizeof(associd))
3914 if (copy_from_user(&associd, optval, optlen)) {
3919 asoc = sctp_id2assoc(sk, associd);
3923 retval = sctp_send_reset_assoc(asoc);
3929 static int sctp_setsockopt_add_streams(struct sock *sk,
3930 char __user *optval,
3931 unsigned int optlen)
3933 struct sctp_association *asoc;
3934 struct sctp_add_streams params;
3935 int retval = -EINVAL;
3937 if (optlen != sizeof(params))
3940 if (copy_from_user(¶ms, optval, optlen)) {
3945 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3949 retval = sctp_send_add_streams(asoc, ¶ms);
3955 static int sctp_setsockopt_scheduler(struct sock *sk,
3956 char __user *optval,
3957 unsigned int optlen)
3959 struct sctp_association *asoc;
3960 struct sctp_assoc_value params;
3961 int retval = -EINVAL;
3963 if (optlen < sizeof(params))
3966 optlen = sizeof(params);
3967 if (copy_from_user(¶ms, optval, optlen)) {
3972 if (params.assoc_value > SCTP_SS_MAX)
3975 asoc = sctp_id2assoc(sk, params.assoc_id);
3979 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3985 static int sctp_setsockopt_scheduler_value(struct sock *sk,
3986 char __user *optval,
3987 unsigned int optlen)
3989 struct sctp_association *asoc;
3990 struct sctp_stream_value params;
3991 int retval = -EINVAL;
3993 if (optlen < sizeof(params))
3996 optlen = sizeof(params);
3997 if (copy_from_user(¶ms, optval, optlen)) {
4002 asoc = sctp_id2assoc(sk, params.assoc_id);
4006 retval = sctp_sched_set_value(asoc, params.stream_id,
4007 params.stream_value, GFP_KERNEL);
4013 /* API 6.2 setsockopt(), getsockopt()
4015 * Applications use setsockopt() and getsockopt() to set or retrieve
4016 * socket options. Socket options are used to change the default
4017 * behavior of sockets calls. They are described in Section 7.
4021 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4022 * int __user *optlen);
4023 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4026 * sd - the socket descript.
4027 * level - set to IPPROTO_SCTP for all SCTP options.
4028 * optname - the option name.
4029 * optval - the buffer to store the value of the option.
4030 * optlen - the size of the buffer.
4032 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4033 char __user *optval, unsigned int optlen)
4037 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4039 /* I can hardly begin to describe how wrong this is. This is
4040 * so broken as to be worse than useless. The API draft
4041 * REALLY is NOT helpful here... I am not convinced that the
4042 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4043 * are at all well-founded.
4045 if (level != SOL_SCTP) {
4046 struct sctp_af *af = sctp_sk(sk)->pf->af;
4047 retval = af->setsockopt(sk, level, optname, optval, optlen);
4054 case SCTP_SOCKOPT_BINDX_ADD:
4055 /* 'optlen' is the size of the addresses buffer. */
4056 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4057 optlen, SCTP_BINDX_ADD_ADDR);
4060 case SCTP_SOCKOPT_BINDX_REM:
4061 /* 'optlen' is the size of the addresses buffer. */
4062 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4063 optlen, SCTP_BINDX_REM_ADDR);
4066 case SCTP_SOCKOPT_CONNECTX_OLD:
4067 /* 'optlen' is the size of the addresses buffer. */
4068 retval = sctp_setsockopt_connectx_old(sk,
4069 (struct sockaddr __user *)optval,
4073 case SCTP_SOCKOPT_CONNECTX:
4074 /* 'optlen' is the size of the addresses buffer. */
4075 retval = sctp_setsockopt_connectx(sk,
4076 (struct sockaddr __user *)optval,
4080 case SCTP_DISABLE_FRAGMENTS:
4081 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4085 retval = sctp_setsockopt_events(sk, optval, optlen);
4088 case SCTP_AUTOCLOSE:
4089 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4092 case SCTP_PEER_ADDR_PARAMS:
4093 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4096 case SCTP_DELAYED_SACK:
4097 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4099 case SCTP_PARTIAL_DELIVERY_POINT:
4100 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4104 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4106 case SCTP_DEFAULT_SEND_PARAM:
4107 retval = sctp_setsockopt_default_send_param(sk, optval,
4110 case SCTP_DEFAULT_SNDINFO:
4111 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4113 case SCTP_PRIMARY_ADDR:
4114 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4116 case SCTP_SET_PEER_PRIMARY_ADDR:
4117 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4120 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4123 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4125 case SCTP_ASSOCINFO:
4126 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4128 case SCTP_I_WANT_MAPPED_V4_ADDR:
4129 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4132 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4134 case SCTP_ADAPTATION_LAYER:
4135 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4138 retval = sctp_setsockopt_context(sk, optval, optlen);
4140 case SCTP_FRAGMENT_INTERLEAVE:
4141 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4143 case SCTP_MAX_BURST:
4144 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4146 case SCTP_AUTH_CHUNK:
4147 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4149 case SCTP_HMAC_IDENT:
4150 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4153 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4155 case SCTP_AUTH_ACTIVE_KEY:
4156 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4158 case SCTP_AUTH_DELETE_KEY:
4159 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4161 case SCTP_AUTO_ASCONF:
4162 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4164 case SCTP_PEER_ADDR_THLDS:
4165 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4167 case SCTP_RECVRCVINFO:
4168 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4170 case SCTP_RECVNXTINFO:
4171 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4173 case SCTP_PR_SUPPORTED:
4174 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4176 case SCTP_DEFAULT_PRINFO:
4177 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4179 case SCTP_RECONFIG_SUPPORTED:
4180 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4182 case SCTP_ENABLE_STREAM_RESET:
4183 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4185 case SCTP_RESET_STREAMS:
4186 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4188 case SCTP_RESET_ASSOC:
4189 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4191 case SCTP_ADD_STREAMS:
4192 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4194 case SCTP_STREAM_SCHEDULER:
4195 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4197 case SCTP_STREAM_SCHEDULER_VALUE:
4198 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4201 retval = -ENOPROTOOPT;
4211 /* API 3.1.6 connect() - UDP Style Syntax
4213 * An application may use the connect() call in the UDP model to initiate an
4214 * association without sending data.
4218 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4220 * sd: the socket descriptor to have a new association added to.
4222 * nam: the address structure (either struct sockaddr_in or struct
4223 * sockaddr_in6 defined in RFC2553 [7]).
4225 * len: the size of the address.
4227 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4235 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4238 /* Validate addr_len before calling common connect/connectx routine. */
4239 af = sctp_get_af_specific(addr->sa_family);
4240 if (!af || addr_len < af->sockaddr_len) {
4243 /* Pass correct addr len to common routine (so it knows there
4244 * is only one address being passed.
4246 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
4253 /* FIXME: Write comments. */
4254 static int sctp_disconnect(struct sock *sk, int flags)
4256 return -EOPNOTSUPP; /* STUB */
4259 /* 4.1.4 accept() - TCP Style Syntax
4261 * Applications use accept() call to remove an established SCTP
4262 * association from the accept queue of the endpoint. A new socket
4263 * descriptor will be returned from accept() to represent the newly
4264 * formed association.
4266 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4268 struct sctp_sock *sp;
4269 struct sctp_endpoint *ep;
4270 struct sock *newsk = NULL;
4271 struct sctp_association *asoc;
4280 if (!sctp_style(sk, TCP)) {
4281 error = -EOPNOTSUPP;
4285 if (!sctp_sstate(sk, LISTENING)) {
4290 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4292 error = sctp_wait_for_accept(sk, timeo);
4296 /* We treat the list of associations on the endpoint as the accept
4297 * queue and pick the first association on the list.
4299 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4301 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4307 /* Populate the fields of the newsk from the oldsk and migrate the
4308 * asoc to the newsk.
4310 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4318 /* The SCTP ioctl handler. */
4319 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4326 * SEQPACKET-style sockets in LISTENING state are valid, for
4327 * SCTP, so only discard TCP-style sockets in LISTENING state.
4329 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4334 struct sk_buff *skb;
4335 unsigned int amount = 0;
4337 skb = skb_peek(&sk->sk_receive_queue);
4340 * We will only return the amount of this packet since
4341 * that is all that will be read.
4345 rc = put_user(amount, (int __user *)arg);
4357 /* This is the function which gets called during socket creation to
4358 * initialized the SCTP-specific portion of the sock.
4359 * The sock structure should already be zero-filled memory.
4361 static int sctp_init_sock(struct sock *sk)
4363 struct net *net = sock_net(sk);
4364 struct sctp_sock *sp;
4366 pr_debug("%s: sk:%p\n", __func__, sk);
4370 /* Initialize the SCTP per socket area. */
4371 switch (sk->sk_type) {
4372 case SOCK_SEQPACKET:
4373 sp->type = SCTP_SOCKET_UDP;
4376 sp->type = SCTP_SOCKET_TCP;
4379 return -ESOCKTNOSUPPORT;
4382 sk->sk_gso_type = SKB_GSO_SCTP;
4384 /* Initialize default send parameters. These parameters can be
4385 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4387 sp->default_stream = 0;
4388 sp->default_ppid = 0;
4389 sp->default_flags = 0;
4390 sp->default_context = 0;
4391 sp->default_timetolive = 0;
4393 sp->default_rcv_context = 0;
4394 sp->max_burst = net->sctp.max_burst;
4396 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4398 /* Initialize default setup parameters. These parameters
4399 * can be modified with the SCTP_INITMSG socket option or
4400 * overridden by the SCTP_INIT CMSG.
4402 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4403 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
4404 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4405 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
4407 /* Initialize default RTO related parameters. These parameters can
4408 * be modified for with the SCTP_RTOINFO socket option.
4410 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4411 sp->rtoinfo.srto_max = net->sctp.rto_max;
4412 sp->rtoinfo.srto_min = net->sctp.rto_min;
4414 /* Initialize default association related parameters. These parameters
4415 * can be modified with the SCTP_ASSOCINFO socket option.
4417 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
4418 sp->assocparams.sasoc_number_peer_destinations = 0;
4419 sp->assocparams.sasoc_peer_rwnd = 0;
4420 sp->assocparams.sasoc_local_rwnd = 0;
4421 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
4423 /* Initialize default event subscriptions. By default, all the
4426 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4428 /* Default Peer Address Parameters. These defaults can
4429 * be modified via SCTP_PEER_ADDR_PARAMS
4431 sp->hbinterval = net->sctp.hb_interval;
4432 sp->pathmaxrxt = net->sctp.max_retrans_path;
4433 sp->pathmtu = 0; /* allow default discovery */
4434 sp->sackdelay = net->sctp.sack_timeout;
4436 sp->param_flags = SPP_HB_ENABLE |
4438 SPP_SACKDELAY_ENABLE;
4440 /* If enabled no SCTP message fragmentation will be performed.
4441 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4443 sp->disable_fragments = 0;
4445 /* Enable Nagle algorithm by default. */
4448 sp->recvrcvinfo = 0;
4449 sp->recvnxtinfo = 0;
4451 /* Enable by default. */
4454 /* Auto-close idle associations after the configured
4455 * number of seconds. A value of 0 disables this
4456 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4457 * for UDP-style sockets only.
4461 /* User specified fragmentation limit. */
4464 sp->adaptation_ind = 0;
4466 sp->pf = sctp_get_pf_specific(sk->sk_family);
4468 /* Control variables for partial data delivery. */
4469 atomic_set(&sp->pd_mode, 0);
4470 skb_queue_head_init(&sp->pd_lobby);
4471 sp->frag_interleave = 0;
4473 /* Create a per socket endpoint structure. Even if we
4474 * change the data structure relationships, this may still
4475 * be useful for storing pre-connect address information.
4477 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4483 sk->sk_destruct = sctp_destruct_sock;
4485 SCTP_DBG_OBJCNT_INC(sock);
4488 percpu_counter_inc(&sctp_sockets_allocated);
4489 sock_prot_inuse_add(net, sk->sk_prot, 1);
4491 /* Nothing can fail after this block, otherwise
4492 * sctp_destroy_sock() will be called without addr_wq_lock held
4494 if (net->sctp.default_auto_asconf) {
4495 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
4496 list_add_tail(&sp->auto_asconf_list,
4497 &net->sctp.auto_asconf_splist);
4498 sp->do_auto_asconf = 1;
4499 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4501 sp->do_auto_asconf = 0;
4509 /* Cleanup any SCTP per socket resources. Must be called with
4510 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4512 static void sctp_destroy_sock(struct sock *sk)
4514 struct sctp_sock *sp;
4516 pr_debug("%s: sk:%p\n", __func__, sk);
4518 /* Release our hold on the endpoint. */
4520 /* This could happen during socket init, thus we bail out
4521 * early, since the rest of the below is not setup either.
4526 if (sp->do_auto_asconf) {
4527 sp->do_auto_asconf = 0;
4528 list_del(&sp->auto_asconf_list);
4530 sctp_endpoint_free(sp->ep);
4532 percpu_counter_dec(&sctp_sockets_allocated);
4533 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4537 /* Triggered when there are no references on the socket anymore */
4538 static void sctp_destruct_sock(struct sock *sk)
4540 struct sctp_sock *sp = sctp_sk(sk);
4542 /* Free up the HMAC transform. */
4543 crypto_free_shash(sp->hmac);
4545 inet_sock_destruct(sk);
4548 /* API 4.1.7 shutdown() - TCP Style Syntax
4549 * int shutdown(int socket, int how);
4551 * sd - the socket descriptor of the association to be closed.
4552 * how - Specifies the type of shutdown. The values are
4555 * Disables further receive operations. No SCTP
4556 * protocol action is taken.
4558 * Disables further send operations, and initiates
4559 * the SCTP shutdown sequence.
4561 * Disables further send and receive operations
4562 * and initiates the SCTP shutdown sequence.
4564 static void sctp_shutdown(struct sock *sk, int how)
4566 struct net *net = sock_net(sk);
4567 struct sctp_endpoint *ep;
4569 if (!sctp_style(sk, TCP))
4572 ep = sctp_sk(sk)->ep;
4573 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4574 struct sctp_association *asoc;
4576 sk->sk_state = SCTP_SS_CLOSING;
4577 asoc = list_entry(ep->asocs.next,
4578 struct sctp_association, asocs);
4579 sctp_primitive_SHUTDOWN(net, asoc, NULL);
4583 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4584 struct sctp_info *info)
4586 struct sctp_transport *prim;
4587 struct list_head *pos;
4590 memset(info, 0, sizeof(*info));
4592 struct sctp_sock *sp = sctp_sk(sk);
4594 info->sctpi_s_autoclose = sp->autoclose;
4595 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4596 info->sctpi_s_pd_point = sp->pd_point;
4597 info->sctpi_s_nodelay = sp->nodelay;
4598 info->sctpi_s_disable_fragments = sp->disable_fragments;
4599 info->sctpi_s_v4mapped = sp->v4mapped;
4600 info->sctpi_s_frag_interleave = sp->frag_interleave;
4601 info->sctpi_s_type = sp->type;
4606 info->sctpi_tag = asoc->c.my_vtag;
4607 info->sctpi_state = asoc->state;
4608 info->sctpi_rwnd = asoc->a_rwnd;
4609 info->sctpi_unackdata = asoc->unack_data;
4610 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4611 info->sctpi_instrms = asoc->stream.incnt;
4612 info->sctpi_outstrms = asoc->stream.outcnt;
4613 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4614 info->sctpi_inqueue++;
4615 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4616 info->sctpi_outqueue++;
4617 info->sctpi_overall_error = asoc->overall_error_count;
4618 info->sctpi_max_burst = asoc->max_burst;
4619 info->sctpi_maxseg = asoc->frag_point;
4620 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4621 info->sctpi_peer_tag = asoc->c.peer_vtag;
4623 mask = asoc->peer.ecn_capable << 1;
4624 mask = (mask | asoc->peer.ipv4_address) << 1;
4625 mask = (mask | asoc->peer.ipv6_address) << 1;
4626 mask = (mask | asoc->peer.hostname_address) << 1;
4627 mask = (mask | asoc->peer.asconf_capable) << 1;
4628 mask = (mask | asoc->peer.prsctp_capable) << 1;
4629 mask = (mask | asoc->peer.auth_capable);
4630 info->sctpi_peer_capable = mask;
4631 mask = asoc->peer.sack_needed << 1;
4632 mask = (mask | asoc->peer.sack_generation) << 1;
4633 mask = (mask | asoc->peer.zero_window_announced);
4634 info->sctpi_peer_sack = mask;
4636 info->sctpi_isacks = asoc->stats.isacks;
4637 info->sctpi_osacks = asoc->stats.osacks;
4638 info->sctpi_opackets = asoc->stats.opackets;
4639 info->sctpi_ipackets = asoc->stats.ipackets;
4640 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4641 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4642 info->sctpi_idupchunks = asoc->stats.idupchunks;
4643 info->sctpi_gapcnt = asoc->stats.gapcnt;
4644 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4645 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4646 info->sctpi_oodchunks = asoc->stats.oodchunks;
4647 info->sctpi_iodchunks = asoc->stats.iodchunks;
4648 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4649 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4651 prim = asoc->peer.primary_path;
4652 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
4653 info->sctpi_p_state = prim->state;
4654 info->sctpi_p_cwnd = prim->cwnd;
4655 info->sctpi_p_srtt = prim->srtt;
4656 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4657 info->sctpi_p_hbinterval = prim->hbinterval;
4658 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4659 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4660 info->sctpi_p_ssthresh = prim->ssthresh;
4661 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4662 info->sctpi_p_flight_size = prim->flight_size;
4663 info->sctpi_p_error = prim->error_count;
4667 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4669 /* use callback to avoid exporting the core structure */
4670 int sctp_transport_walk_start(struct rhashtable_iter *iter)
4674 rhltable_walk_enter(&sctp_transport_hashtable, iter);
4676 err = rhashtable_walk_start(iter);
4677 if (err && err != -EAGAIN) {
4678 rhashtable_walk_stop(iter);
4679 rhashtable_walk_exit(iter);
4686 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4688 rhashtable_walk_stop(iter);
4689 rhashtable_walk_exit(iter);
4692 struct sctp_transport *sctp_transport_get_next(struct net *net,
4693 struct rhashtable_iter *iter)
4695 struct sctp_transport *t;
4697 t = rhashtable_walk_next(iter);
4698 for (; t; t = rhashtable_walk_next(iter)) {
4700 if (PTR_ERR(t) == -EAGAIN)
4705 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4706 t->asoc->peer.primary_path == t)
4713 struct sctp_transport *sctp_transport_get_idx(struct net *net,
4714 struct rhashtable_iter *iter,
4717 void *obj = SEQ_START_TOKEN;
4719 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4726 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4730 struct sctp_ep_common *epb;
4731 struct sctp_hashbucket *head;
4733 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4735 read_lock_bh(&head->lock);
4736 sctp_for_each_hentry(epb, &head->chain) {
4737 err = cb(sctp_ep(epb), p);
4741 read_unlock_bh(&head->lock);
4746 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4748 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4750 const union sctp_addr *laddr,
4751 const union sctp_addr *paddr, void *p)
4753 struct sctp_transport *transport;
4757 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
4762 err = cb(transport, p);
4763 sctp_transport_put(transport);
4767 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4769 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
4770 int (*cb_done)(struct sctp_transport *, void *),
4771 struct net *net, int *pos, void *p) {
4772 struct rhashtable_iter hti;
4773 struct sctp_transport *tsp;
4777 ret = sctp_transport_walk_start(&hti);
4781 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4782 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4783 if (!sctp_transport_hold(tsp))
4789 sctp_transport_put(tsp);
4791 sctp_transport_walk_stop(&hti);
4794 if (cb_done && !cb_done(tsp, p)) {
4796 sctp_transport_put(tsp);
4799 sctp_transport_put(tsp);
4804 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4806 /* 7.2.1 Association Status (SCTP_STATUS)
4808 * Applications can retrieve current status information about an
4809 * association, including association state, peer receiver window size,
4810 * number of unacked data chunks, and number of data chunks pending
4811 * receipt. This information is read-only.
4813 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4814 char __user *optval,
4817 struct sctp_status status;
4818 struct sctp_association *asoc = NULL;
4819 struct sctp_transport *transport;
4820 sctp_assoc_t associd;
4823 if (len < sizeof(status)) {
4828 len = sizeof(status);
4829 if (copy_from_user(&status, optval, len)) {
4834 associd = status.sstat_assoc_id;
4835 asoc = sctp_id2assoc(sk, associd);
4841 transport = asoc->peer.primary_path;
4843 status.sstat_assoc_id = sctp_assoc2id(asoc);
4844 status.sstat_state = sctp_assoc_to_state(asoc);
4845 status.sstat_rwnd = asoc->peer.rwnd;
4846 status.sstat_unackdata = asoc->unack_data;
4848 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4849 status.sstat_instrms = asoc->stream.incnt;
4850 status.sstat_outstrms = asoc->stream.outcnt;
4851 status.sstat_fragmentation_point = asoc->frag_point;
4852 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4853 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4854 transport->af_specific->sockaddr_len);
4855 /* Map ipv4 address into v4-mapped-on-v6 address. */
4856 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
4857 (union sctp_addr *)&status.sstat_primary.spinfo_address);
4858 status.sstat_primary.spinfo_state = transport->state;
4859 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4860 status.sstat_primary.spinfo_srtt = transport->srtt;
4861 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
4862 status.sstat_primary.spinfo_mtu = transport->pathmtu;
4864 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4865 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4867 if (put_user(len, optlen)) {
4872 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4873 __func__, len, status.sstat_state, status.sstat_rwnd,
4874 status.sstat_assoc_id);
4876 if (copy_to_user(optval, &status, len)) {
4886 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4888 * Applications can retrieve information about a specific peer address
4889 * of an association, including its reachability state, congestion
4890 * window, and retransmission timer values. This information is
4893 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4894 char __user *optval,
4897 struct sctp_paddrinfo pinfo;
4898 struct sctp_transport *transport;
4901 if (len < sizeof(pinfo)) {
4906 len = sizeof(pinfo);
4907 if (copy_from_user(&pinfo, optval, len)) {
4912 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4913 pinfo.spinfo_assoc_id);
4917 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4918 pinfo.spinfo_state = transport->state;
4919 pinfo.spinfo_cwnd = transport->cwnd;
4920 pinfo.spinfo_srtt = transport->srtt;
4921 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
4922 pinfo.spinfo_mtu = transport->pathmtu;
4924 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4925 pinfo.spinfo_state = SCTP_ACTIVE;
4927 if (put_user(len, optlen)) {
4932 if (copy_to_user(optval, &pinfo, len)) {
4941 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4943 * This option is a on/off flag. If enabled no SCTP message
4944 * fragmentation will be performed. Instead if a message being sent
4945 * exceeds the current PMTU size, the message will NOT be sent and
4946 * instead a error will be indicated to the user.
4948 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4949 char __user *optval, int __user *optlen)
4953 if (len < sizeof(int))
4957 val = (sctp_sk(sk)->disable_fragments == 1);
4958 if (put_user(len, optlen))
4960 if (copy_to_user(optval, &val, len))
4965 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4967 * This socket option is used to specify various notifications and
4968 * ancillary data the user wishes to receive.
4970 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4975 if (len > sizeof(struct sctp_event_subscribe))
4976 len = sizeof(struct sctp_event_subscribe);
4977 if (put_user(len, optlen))
4979 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4984 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4986 * This socket option is applicable to the UDP-style socket only. When
4987 * set it will cause associations that are idle for more than the
4988 * specified number of seconds to automatically close. An association
4989 * being idle is defined an association that has NOT sent or received
4990 * user data. The special value of '0' indicates that no automatic
4991 * close of any associations should be performed. The option expects an
4992 * integer defining the number of seconds of idle time before an
4993 * association is closed.
4995 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4997 /* Applicable to UDP-style socket only */
4998 if (sctp_style(sk, TCP))
5000 if (len < sizeof(int))
5003 if (put_user(len, optlen))
5005 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
5010 /* Helper routine to branch off an association to a new socket. */
5011 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5013 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5014 struct sctp_sock *sp = sctp_sk(sk);
5015 struct socket *sock;
5018 /* Do not peel off from one netns to another one. */
5019 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5025 /* An association cannot be branched off from an already peeled-off
5026 * socket, nor is this supported for tcp style sockets.
5028 if (!sctp_style(sk, UDP))
5031 /* Create a new socket. */
5032 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5036 sctp_copy_sock(sock->sk, sk, asoc);
5038 /* Make peeled-off sockets more like 1-1 accepted sockets.
5039 * Set the daddr and initialize id to something more random
5041 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5043 /* Populate the fields of the newsk from the oldsk and migrate the
5044 * asoc to the newsk.
5046 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5052 EXPORT_SYMBOL(sctp_do_peeloff);
5054 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5055 struct file **newfile, unsigned flags)
5057 struct socket *newsock;
5060 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5064 /* Map the socket to an unused fd that can be returned to the user. */
5065 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5067 sock_release(newsock);
5071 *newfile = sock_alloc_file(newsock, 0, NULL);
5072 if (IS_ERR(*newfile)) {
5073 put_unused_fd(retval);
5074 sock_release(newsock);
5075 retval = PTR_ERR(*newfile);
5080 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5083 peeloff->sd = retval;
5085 if (flags & SOCK_NONBLOCK)
5086 (*newfile)->f_flags |= O_NONBLOCK;
5091 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5093 sctp_peeloff_arg_t peeloff;
5094 struct file *newfile = NULL;
5097 if (len < sizeof(sctp_peeloff_arg_t))
5099 len = sizeof(sctp_peeloff_arg_t);
5100 if (copy_from_user(&peeloff, optval, len))
5103 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5107 /* Return the fd mapped to the new socket. */
5108 if (put_user(len, optlen)) {
5110 put_unused_fd(retval);
5114 if (copy_to_user(optval, &peeloff, len)) {
5116 put_unused_fd(retval);
5119 fd_install(retval, newfile);
5124 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5125 char __user *optval, int __user *optlen)
5127 sctp_peeloff_flags_arg_t peeloff;
5128 struct file *newfile = NULL;
5131 if (len < sizeof(sctp_peeloff_flags_arg_t))
5133 len = sizeof(sctp_peeloff_flags_arg_t);
5134 if (copy_from_user(&peeloff, optval, len))
5137 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5138 &newfile, peeloff.flags);
5142 /* Return the fd mapped to the new socket. */
5143 if (put_user(len, optlen)) {
5145 put_unused_fd(retval);
5149 if (copy_to_user(optval, &peeloff, len)) {
5151 put_unused_fd(retval);
5154 fd_install(retval, newfile);
5159 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5161 * Applications can enable or disable heartbeats for any peer address of
5162 * an association, modify an address's heartbeat interval, force a
5163 * heartbeat to be sent immediately, and adjust the address's maximum
5164 * number of retransmissions sent before an address is considered
5165 * unreachable. The following structure is used to access and modify an
5166 * address's parameters:
5168 * struct sctp_paddrparams {
5169 * sctp_assoc_t spp_assoc_id;
5170 * struct sockaddr_storage spp_address;
5171 * uint32_t spp_hbinterval;
5172 * uint16_t spp_pathmaxrxt;
5173 * uint32_t spp_pathmtu;
5174 * uint32_t spp_sackdelay;
5175 * uint32_t spp_flags;
5178 * spp_assoc_id - (one-to-many style socket) This is filled in the
5179 * application, and identifies the association for
5181 * spp_address - This specifies which address is of interest.
5182 * spp_hbinterval - This contains the value of the heartbeat interval,
5183 * in milliseconds. If a value of zero
5184 * is present in this field then no changes are to
5185 * be made to this parameter.
5186 * spp_pathmaxrxt - This contains the maximum number of
5187 * retransmissions before this address shall be
5188 * considered unreachable. If a value of zero
5189 * is present in this field then no changes are to
5190 * be made to this parameter.
5191 * spp_pathmtu - When Path MTU discovery is disabled the value
5192 * specified here will be the "fixed" path mtu.
5193 * Note that if the spp_address field is empty
5194 * then all associations on this address will
5195 * have this fixed path mtu set upon them.
5197 * spp_sackdelay - When delayed sack is enabled, this value specifies
5198 * the number of milliseconds that sacks will be delayed
5199 * for. This value will apply to all addresses of an
5200 * association if the spp_address field is empty. Note
5201 * also, that if delayed sack is enabled and this
5202 * value is set to 0, no change is made to the last
5203 * recorded delayed sack timer value.
5205 * spp_flags - These flags are used to control various features
5206 * on an association. The flag field may contain
5207 * zero or more of the following options.
5209 * SPP_HB_ENABLE - Enable heartbeats on the
5210 * specified address. Note that if the address
5211 * field is empty all addresses for the association
5212 * have heartbeats enabled upon them.
5214 * SPP_HB_DISABLE - Disable heartbeats on the
5215 * speicifed address. Note that if the address
5216 * field is empty all addresses for the association
5217 * will have their heartbeats disabled. Note also
5218 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5219 * mutually exclusive, only one of these two should
5220 * be specified. Enabling both fields will have
5221 * undetermined results.
5223 * SPP_HB_DEMAND - Request a user initiated heartbeat
5224 * to be made immediately.
5226 * SPP_PMTUD_ENABLE - This field will enable PMTU
5227 * discovery upon the specified address. Note that
5228 * if the address feild is empty then all addresses
5229 * on the association are effected.
5231 * SPP_PMTUD_DISABLE - This field will disable PMTU
5232 * discovery upon the specified address. Note that
5233 * if the address feild is empty then all addresses
5234 * on the association are effected. Not also that
5235 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5236 * exclusive. Enabling both will have undetermined
5239 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5240 * on delayed sack. The time specified in spp_sackdelay
5241 * is used to specify the sack delay for this address. Note
5242 * that if spp_address is empty then all addresses will
5243 * enable delayed sack and take on the sack delay
5244 * value specified in spp_sackdelay.
5245 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5246 * off delayed sack. If the spp_address field is blank then
5247 * delayed sack is disabled for the entire association. Note
5248 * also that this field is mutually exclusive to
5249 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5252 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5253 char __user *optval, int __user *optlen)
5255 struct sctp_paddrparams params;
5256 struct sctp_transport *trans = NULL;
5257 struct sctp_association *asoc = NULL;
5258 struct sctp_sock *sp = sctp_sk(sk);
5260 if (len < sizeof(struct sctp_paddrparams))
5262 len = sizeof(struct sctp_paddrparams);
5263 if (copy_from_user(¶ms, optval, len))
5266 /* If an address other than INADDR_ANY is specified, and
5267 * no transport is found, then the request is invalid.
5269 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5270 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5271 params.spp_assoc_id);
5273 pr_debug("%s: failed no transport\n", __func__);
5278 /* Get association, if assoc_id != 0 and the socket is a one
5279 * to many style socket, and an association was not found, then
5280 * the id was invalid.
5282 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5283 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
5284 pr_debug("%s: failed no association\n", __func__);
5289 /* Fetch transport values. */
5290 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5291 params.spp_pathmtu = trans->pathmtu;
5292 params.spp_pathmaxrxt = trans->pathmaxrxt;
5293 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5295 /*draft-11 doesn't say what to return in spp_flags*/
5296 params.spp_flags = trans->param_flags;
5298 /* Fetch association values. */
5299 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5300 params.spp_pathmtu = asoc->pathmtu;
5301 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5302 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5304 /*draft-11 doesn't say what to return in spp_flags*/
5305 params.spp_flags = asoc->param_flags;
5307 /* Fetch socket values. */
5308 params.spp_hbinterval = sp->hbinterval;
5309 params.spp_pathmtu = sp->pathmtu;
5310 params.spp_sackdelay = sp->sackdelay;
5311 params.spp_pathmaxrxt = sp->pathmaxrxt;
5313 /*draft-11 doesn't say what to return in spp_flags*/
5314 params.spp_flags = sp->param_flags;
5317 if (copy_to_user(optval, ¶ms, len))
5320 if (put_user(len, optlen))
5327 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5329 * This option will effect the way delayed acks are performed. This
5330 * option allows you to get or set the delayed ack time, in
5331 * milliseconds. It also allows changing the delayed ack frequency.
5332 * Changing the frequency to 1 disables the delayed sack algorithm. If
5333 * the assoc_id is 0, then this sets or gets the endpoints default
5334 * values. If the assoc_id field is non-zero, then the set or get
5335 * effects the specified association for the one to many model (the
5336 * assoc_id field is ignored by the one to one model). Note that if
5337 * sack_delay or sack_freq are 0 when setting this option, then the
5338 * current values will remain unchanged.
5340 * struct sctp_sack_info {
5341 * sctp_assoc_t sack_assoc_id;
5342 * uint32_t sack_delay;
5343 * uint32_t sack_freq;
5346 * sack_assoc_id - This parameter, indicates which association the user
5347 * is performing an action upon. Note that if this field's value is
5348 * zero then the endpoints default value is changed (effecting future
5349 * associations only).
5351 * sack_delay - This parameter contains the number of milliseconds that
5352 * the user is requesting the delayed ACK timer be set to. Note that
5353 * this value is defined in the standard to be between 200 and 500
5356 * sack_freq - This parameter contains the number of packets that must
5357 * be received before a sack is sent without waiting for the delay
5358 * timer to expire. The default value for this is 2, setting this
5359 * value to 1 will disable the delayed sack algorithm.
5361 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5362 char __user *optval,
5365 struct sctp_sack_info params;
5366 struct sctp_association *asoc = NULL;
5367 struct sctp_sock *sp = sctp_sk(sk);
5369 if (len >= sizeof(struct sctp_sack_info)) {
5370 len = sizeof(struct sctp_sack_info);
5372 if (copy_from_user(¶ms, optval, len))
5374 } else if (len == sizeof(struct sctp_assoc_value)) {
5375 pr_warn_ratelimited(DEPRECATED
5377 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5378 "Use struct sctp_sack_info instead\n",
5379 current->comm, task_pid_nr(current));
5380 if (copy_from_user(¶ms, optval, len))
5385 /* Get association, if sack_assoc_id != 0 and the socket is a one
5386 * to many style socket, and an association was not found, then
5387 * the id was invalid.
5389 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5390 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
5394 /* Fetch association values. */
5395 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5396 params.sack_delay = jiffies_to_msecs(
5398 params.sack_freq = asoc->sackfreq;
5401 params.sack_delay = 0;
5402 params.sack_freq = 1;
5405 /* Fetch socket values. */
5406 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5407 params.sack_delay = sp->sackdelay;
5408 params.sack_freq = sp->sackfreq;
5410 params.sack_delay = 0;
5411 params.sack_freq = 1;
5415 if (copy_to_user(optval, ¶ms, len))
5418 if (put_user(len, optlen))
5424 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5426 * Applications can specify protocol parameters for the default association
5427 * initialization. The option name argument to setsockopt() and getsockopt()
5430 * Setting initialization parameters is effective only on an unconnected
5431 * socket (for UDP-style sockets only future associations are effected
5432 * by the change). With TCP-style sockets, this option is inherited by
5433 * sockets derived from a listener socket.
5435 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5437 if (len < sizeof(struct sctp_initmsg))
5439 len = sizeof(struct sctp_initmsg);
5440 if (put_user(len, optlen))
5442 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5448 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5449 char __user *optval, int __user *optlen)
5451 struct sctp_association *asoc;
5453 struct sctp_getaddrs getaddrs;
5454 struct sctp_transport *from;
5456 union sctp_addr temp;
5457 struct sctp_sock *sp = sctp_sk(sk);
5462 if (len < sizeof(struct sctp_getaddrs))
5465 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5468 /* For UDP-style sockets, id specifies the association to query. */
5469 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5473 to = optval + offsetof(struct sctp_getaddrs, addrs);
5474 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5476 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5478 memcpy(&temp, &from->ipaddr, sizeof(temp));
5479 addrlen = sctp_get_pf_specific(sk->sk_family)
5480 ->addr_to_user(sp, &temp);
5481 if (space_left < addrlen)
5483 if (copy_to_user(to, &temp, addrlen))
5487 space_left -= addrlen;
5490 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5492 bytes_copied = ((char __user *)to) - optval;
5493 if (put_user(bytes_copied, optlen))
5499 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5500 size_t space_left, int *bytes_copied)
5502 struct sctp_sockaddr_entry *addr;
5503 union sctp_addr temp;
5506 struct net *net = sock_net(sk);
5509 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
5513 if ((PF_INET == sk->sk_family) &&
5514 (AF_INET6 == addr->a.sa.sa_family))
5516 if ((PF_INET6 == sk->sk_family) &&
5517 inet_v6_ipv6only(sk) &&
5518 (AF_INET == addr->a.sa.sa_family))
5520 memcpy(&temp, &addr->a, sizeof(temp));
5521 if (!temp.v4.sin_port)
5522 temp.v4.sin_port = htons(port);
5524 addrlen = sctp_get_pf_specific(sk->sk_family)
5525 ->addr_to_user(sctp_sk(sk), &temp);
5527 if (space_left < addrlen) {
5531 memcpy(to, &temp, addrlen);
5535 space_left -= addrlen;
5536 *bytes_copied += addrlen;
5544 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5545 char __user *optval, int __user *optlen)
5547 struct sctp_bind_addr *bp;
5548 struct sctp_association *asoc;
5550 struct sctp_getaddrs getaddrs;
5551 struct sctp_sockaddr_entry *addr;
5553 union sctp_addr temp;
5554 struct sctp_sock *sp = sctp_sk(sk);
5558 int bytes_copied = 0;
5562 if (len < sizeof(struct sctp_getaddrs))
5565 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5569 * For UDP-style sockets, id specifies the association to query.
5570 * If the id field is set to the value '0' then the locally bound
5571 * addresses are returned without regard to any particular
5574 if (0 == getaddrs.assoc_id) {
5575 bp = &sctp_sk(sk)->ep->base.bind_addr;
5577 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5580 bp = &asoc->base.bind_addr;
5583 to = optval + offsetof(struct sctp_getaddrs, addrs);
5584 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5586 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
5590 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5591 * addresses from the global local address list.
5593 if (sctp_list_single_entry(&bp->address_list)) {
5594 addr = list_entry(bp->address_list.next,
5595 struct sctp_sockaddr_entry, list);
5596 if (sctp_is_any(sk, &addr->a)) {
5597 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5598 space_left, &bytes_copied);
5608 /* Protection on the bound address list is not needed since
5609 * in the socket option context we hold a socket lock and
5610 * thus the bound address list can't change.
5612 list_for_each_entry(addr, &bp->address_list, list) {
5613 memcpy(&temp, &addr->a, sizeof(temp));
5614 addrlen = sctp_get_pf_specific(sk->sk_family)
5615 ->addr_to_user(sp, &temp);
5616 if (space_left < addrlen) {
5617 err = -ENOMEM; /*fixme: right error?*/
5620 memcpy(buf, &temp, addrlen);
5622 bytes_copied += addrlen;
5624 space_left -= addrlen;
5628 if (copy_to_user(to, addrs, bytes_copied)) {
5632 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5636 if (put_user(bytes_copied, optlen))
5643 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5645 * Requests that the local SCTP stack use the enclosed peer address as
5646 * the association primary. The enclosed address must be one of the
5647 * association peer's addresses.
5649 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5650 char __user *optval, int __user *optlen)
5652 struct sctp_prim prim;
5653 struct sctp_association *asoc;
5654 struct sctp_sock *sp = sctp_sk(sk);
5656 if (len < sizeof(struct sctp_prim))
5659 len = sizeof(struct sctp_prim);
5661 if (copy_from_user(&prim, optval, len))
5664 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5668 if (!asoc->peer.primary_path)
5671 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5672 asoc->peer.primary_path->af_specific->sockaddr_len);
5674 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
5675 (union sctp_addr *)&prim.ssp_addr);
5677 if (put_user(len, optlen))
5679 if (copy_to_user(optval, &prim, len))
5686 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
5688 * Requests that the local endpoint set the specified Adaptation Layer
5689 * Indication parameter for all future INIT and INIT-ACK exchanges.
5691 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
5692 char __user *optval, int __user *optlen)
5694 struct sctp_setadaptation adaptation;
5696 if (len < sizeof(struct sctp_setadaptation))
5699 len = sizeof(struct sctp_setadaptation);
5701 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
5703 if (put_user(len, optlen))
5705 if (copy_to_user(optval, &adaptation, len))
5713 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5715 * Applications that wish to use the sendto() system call may wish to
5716 * specify a default set of parameters that would normally be supplied
5717 * through the inclusion of ancillary data. This socket option allows
5718 * such an application to set the default sctp_sndrcvinfo structure.
5721 * The application that wishes to use this socket option simply passes
5722 * in to this call the sctp_sndrcvinfo structure defined in Section
5723 * 5.2.2) The input parameters accepted by this call include
5724 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5725 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5726 * to this call if the caller is using the UDP model.
5728 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5730 static int sctp_getsockopt_default_send_param(struct sock *sk,
5731 int len, char __user *optval,
5734 struct sctp_sock *sp = sctp_sk(sk);
5735 struct sctp_association *asoc;
5736 struct sctp_sndrcvinfo info;
5738 if (len < sizeof(info))
5743 if (copy_from_user(&info, optval, len))
5746 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5747 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5750 info.sinfo_stream = asoc->default_stream;
5751 info.sinfo_flags = asoc->default_flags;
5752 info.sinfo_ppid = asoc->default_ppid;
5753 info.sinfo_context = asoc->default_context;
5754 info.sinfo_timetolive = asoc->default_timetolive;
5756 info.sinfo_stream = sp->default_stream;
5757 info.sinfo_flags = sp->default_flags;
5758 info.sinfo_ppid = sp->default_ppid;
5759 info.sinfo_context = sp->default_context;
5760 info.sinfo_timetolive = sp->default_timetolive;
5763 if (put_user(len, optlen))
5765 if (copy_to_user(optval, &info, len))
5771 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5772 * (SCTP_DEFAULT_SNDINFO)
5774 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5775 char __user *optval,
5778 struct sctp_sock *sp = sctp_sk(sk);
5779 struct sctp_association *asoc;
5780 struct sctp_sndinfo info;
5782 if (len < sizeof(info))
5787 if (copy_from_user(&info, optval, len))
5790 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5791 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5794 info.snd_sid = asoc->default_stream;
5795 info.snd_flags = asoc->default_flags;
5796 info.snd_ppid = asoc->default_ppid;
5797 info.snd_context = asoc->default_context;
5799 info.snd_sid = sp->default_stream;
5800 info.snd_flags = sp->default_flags;
5801 info.snd_ppid = sp->default_ppid;
5802 info.snd_context = sp->default_context;
5805 if (put_user(len, optlen))
5807 if (copy_to_user(optval, &info, len))
5815 * 7.1.5 SCTP_NODELAY
5817 * Turn on/off any Nagle-like algorithm. This means that packets are
5818 * generally sent as soon as possible and no unnecessary delays are
5819 * introduced, at the cost of more packets in the network. Expects an
5820 * integer boolean flag.
5823 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5824 char __user *optval, int __user *optlen)
5828 if (len < sizeof(int))
5832 val = (sctp_sk(sk)->nodelay == 1);
5833 if (put_user(len, optlen))
5835 if (copy_to_user(optval, &val, len))
5842 * 7.1.1 SCTP_RTOINFO
5844 * The protocol parameters used to initialize and bound retransmission
5845 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5846 * and modify these parameters.
5847 * All parameters are time values, in milliseconds. A value of 0, when
5848 * modifying the parameters, indicates that the current value should not
5852 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5853 char __user *optval,
5854 int __user *optlen) {
5855 struct sctp_rtoinfo rtoinfo;
5856 struct sctp_association *asoc;
5858 if (len < sizeof (struct sctp_rtoinfo))
5861 len = sizeof(struct sctp_rtoinfo);
5863 if (copy_from_user(&rtoinfo, optval, len))
5866 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5868 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5871 /* Values corresponding to the specific association. */
5873 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5874 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5875 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5877 /* Values corresponding to the endpoint. */
5878 struct sctp_sock *sp = sctp_sk(sk);
5880 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5881 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5882 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5885 if (put_user(len, optlen))
5888 if (copy_to_user(optval, &rtoinfo, len))
5896 * 7.1.2 SCTP_ASSOCINFO
5898 * This option is used to tune the maximum retransmission attempts
5899 * of the association.
5900 * Returns an error if the new association retransmission value is
5901 * greater than the sum of the retransmission value of the peer.
5902 * See [SCTP] for more information.
5905 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5906 char __user *optval,
5910 struct sctp_assocparams assocparams;
5911 struct sctp_association *asoc;
5912 struct list_head *pos;
5915 if (len < sizeof (struct sctp_assocparams))
5918 len = sizeof(struct sctp_assocparams);
5920 if (copy_from_user(&assocparams, optval, len))
5923 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5925 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5928 /* Values correspoinding to the specific association */
5930 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5931 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5932 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5933 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
5935 list_for_each(pos, &asoc->peer.transport_addr_list) {
5939 assocparams.sasoc_number_peer_destinations = cnt;
5941 /* Values corresponding to the endpoint */
5942 struct sctp_sock *sp = sctp_sk(sk);
5944 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5945 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5946 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5947 assocparams.sasoc_cookie_life =
5948 sp->assocparams.sasoc_cookie_life;
5949 assocparams.sasoc_number_peer_destinations =
5951 sasoc_number_peer_destinations;
5954 if (put_user(len, optlen))
5957 if (copy_to_user(optval, &assocparams, len))
5964 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5966 * This socket option is a boolean flag which turns on or off mapped V4
5967 * addresses. If this option is turned on and the socket is type
5968 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5969 * If this option is turned off, then no mapping will be done of V4
5970 * addresses and a user will receive both PF_INET6 and PF_INET type
5971 * addresses on the socket.
5973 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5974 char __user *optval, int __user *optlen)
5977 struct sctp_sock *sp = sctp_sk(sk);
5979 if (len < sizeof(int))
5984 if (put_user(len, optlen))
5986 if (copy_to_user(optval, &val, len))
5993 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5994 * (chapter and verse is quoted at sctp_setsockopt_context())
5996 static int sctp_getsockopt_context(struct sock *sk, int len,
5997 char __user *optval, int __user *optlen)
5999 struct sctp_assoc_value params;
6000 struct sctp_sock *sp;
6001 struct sctp_association *asoc;
6003 if (len < sizeof(struct sctp_assoc_value))
6006 len = sizeof(struct sctp_assoc_value);
6008 if (copy_from_user(¶ms, optval, len))
6013 if (params.assoc_id != 0) {
6014 asoc = sctp_id2assoc(sk, params.assoc_id);
6017 params.assoc_value = asoc->default_rcv_context;
6019 params.assoc_value = sp->default_rcv_context;
6022 if (put_user(len, optlen))
6024 if (copy_to_user(optval, ¶ms, len))
6031 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6032 * This option will get or set the maximum size to put in any outgoing
6033 * SCTP DATA chunk. If a message is larger than this size it will be
6034 * fragmented by SCTP into the specified size. Note that the underlying
6035 * SCTP implementation may fragment into smaller sized chunks when the
6036 * PMTU of the underlying association is smaller than the value set by
6037 * the user. The default value for this option is '0' which indicates
6038 * the user is NOT limiting fragmentation and only the PMTU will effect
6039 * SCTP's choice of DATA chunk size. Note also that values set larger
6040 * than the maximum size of an IP datagram will effectively let SCTP
6041 * control fragmentation (i.e. the same as setting this option to 0).
6043 * The following structure is used to access and modify this parameter:
6045 * struct sctp_assoc_value {
6046 * sctp_assoc_t assoc_id;
6047 * uint32_t assoc_value;
6050 * assoc_id: This parameter is ignored for one-to-one style sockets.
6051 * For one-to-many style sockets this parameter indicates which
6052 * association the user is performing an action upon. Note that if
6053 * this field's value is zero then the endpoints default value is
6054 * changed (effecting future associations only).
6055 * assoc_value: This parameter specifies the maximum size in bytes.
6057 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6058 char __user *optval, int __user *optlen)
6060 struct sctp_assoc_value params;
6061 struct sctp_association *asoc;
6063 if (len == sizeof(int)) {
6064 pr_warn_ratelimited(DEPRECATED
6066 "Use of int in maxseg socket option.\n"
6067 "Use struct sctp_assoc_value instead\n",
6068 current->comm, task_pid_nr(current));
6069 params.assoc_id = 0;
6070 } else if (len >= sizeof(struct sctp_assoc_value)) {
6071 len = sizeof(struct sctp_assoc_value);
6072 if (copy_from_user(¶ms, optval, sizeof(params)))
6077 asoc = sctp_id2assoc(sk, params.assoc_id);
6078 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6082 params.assoc_value = asoc->frag_point;
6084 params.assoc_value = sctp_sk(sk)->user_frag;
6086 if (put_user(len, optlen))
6088 if (len == sizeof(int)) {
6089 if (copy_to_user(optval, ¶ms.assoc_value, len))
6092 if (copy_to_user(optval, ¶ms, len))
6100 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6101 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6103 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6104 char __user *optval, int __user *optlen)
6108 if (len < sizeof(int))
6113 val = sctp_sk(sk)->frag_interleave;
6114 if (put_user(len, optlen))
6116 if (copy_to_user(optval, &val, len))
6123 * 7.1.25. Set or Get the sctp partial delivery point
6124 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6126 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6127 char __user *optval,
6132 if (len < sizeof(u32))
6137 val = sctp_sk(sk)->pd_point;
6138 if (put_user(len, optlen))
6140 if (copy_to_user(optval, &val, len))
6147 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6148 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6150 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6151 char __user *optval,
6154 struct sctp_assoc_value params;
6155 struct sctp_sock *sp;
6156 struct sctp_association *asoc;
6158 if (len == sizeof(int)) {
6159 pr_warn_ratelimited(DEPRECATED
6161 "Use of int in max_burst socket option.\n"
6162 "Use struct sctp_assoc_value instead\n",
6163 current->comm, task_pid_nr(current));
6164 params.assoc_id = 0;
6165 } else if (len >= sizeof(struct sctp_assoc_value)) {
6166 len = sizeof(struct sctp_assoc_value);
6167 if (copy_from_user(¶ms, optval, len))
6174 if (params.assoc_id != 0) {
6175 asoc = sctp_id2assoc(sk, params.assoc_id);
6178 params.assoc_value = asoc->max_burst;
6180 params.assoc_value = sp->max_burst;
6182 if (len == sizeof(int)) {
6183 if (copy_to_user(optval, ¶ms.assoc_value, len))
6186 if (copy_to_user(optval, ¶ms, len))
6194 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6195 char __user *optval, int __user *optlen)
6197 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6198 struct sctp_hmacalgo __user *p = (void __user *)optval;
6199 struct sctp_hmac_algo_param *hmacs;
6204 if (!ep->auth_enable)
6207 hmacs = ep->auth_hmacs_list;
6208 data_len = ntohs(hmacs->param_hdr.length) -
6209 sizeof(struct sctp_paramhdr);
6211 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6214 len = sizeof(struct sctp_hmacalgo) + data_len;
6215 num_idents = data_len / sizeof(u16);
6217 if (put_user(len, optlen))
6219 if (put_user(num_idents, &p->shmac_num_idents))
6221 for (i = 0; i < num_idents; i++) {
6222 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6224 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6230 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6231 char __user *optval, int __user *optlen)
6233 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6234 struct sctp_authkeyid val;
6235 struct sctp_association *asoc;
6237 if (!ep->auth_enable)
6240 if (len < sizeof(struct sctp_authkeyid))
6242 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
6245 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6246 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6250 val.scact_keynumber = asoc->active_key_id;
6252 val.scact_keynumber = ep->active_key_id;
6254 len = sizeof(struct sctp_authkeyid);
6255 if (put_user(len, optlen))
6257 if (copy_to_user(optval, &val, len))
6263 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6264 char __user *optval, int __user *optlen)
6266 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6267 struct sctp_authchunks __user *p = (void __user *)optval;
6268 struct sctp_authchunks val;
6269 struct sctp_association *asoc;
6270 struct sctp_chunks_param *ch;
6274 if (!ep->auth_enable)
6277 if (len < sizeof(struct sctp_authchunks))
6280 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
6283 to = p->gauth_chunks;
6284 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6288 ch = asoc->peer.peer_chunks;
6292 /* See if the user provided enough room for all the data */
6293 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6294 if (len < num_chunks)
6297 if (copy_to_user(to, ch->chunks, num_chunks))
6300 len = sizeof(struct sctp_authchunks) + num_chunks;
6301 if (put_user(len, optlen))
6303 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6308 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6309 char __user *optval, int __user *optlen)
6311 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6312 struct sctp_authchunks __user *p = (void __user *)optval;
6313 struct sctp_authchunks val;
6314 struct sctp_association *asoc;
6315 struct sctp_chunks_param *ch;
6319 if (!ep->auth_enable)
6322 if (len < sizeof(struct sctp_authchunks))
6325 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
6328 to = p->gauth_chunks;
6329 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6330 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6334 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6336 ch = ep->auth_chunk_list;
6341 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6342 if (len < sizeof(struct sctp_authchunks) + num_chunks)
6345 if (copy_to_user(to, ch->chunks, num_chunks))
6348 len = sizeof(struct sctp_authchunks) + num_chunks;
6349 if (put_user(len, optlen))
6351 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6358 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6359 * This option gets the current number of associations that are attached
6360 * to a one-to-many style socket. The option value is an uint32_t.
6362 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6363 char __user *optval, int __user *optlen)
6365 struct sctp_sock *sp = sctp_sk(sk);
6366 struct sctp_association *asoc;
6369 if (sctp_style(sk, TCP))
6372 if (len < sizeof(u32))
6377 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6381 if (put_user(len, optlen))
6383 if (copy_to_user(optval, &val, len))
6390 * 8.1.23 SCTP_AUTO_ASCONF
6391 * See the corresponding setsockopt entry as description
6393 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6394 char __user *optval, int __user *optlen)
6398 if (len < sizeof(int))
6402 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6404 if (put_user(len, optlen))
6406 if (copy_to_user(optval, &val, len))
6412 * 8.2.6. Get the Current Identifiers of Associations
6413 * (SCTP_GET_ASSOC_ID_LIST)
6415 * This option gets the current list of SCTP association identifiers of
6416 * the SCTP associations handled by a one-to-many style socket.
6418 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6419 char __user *optval, int __user *optlen)
6421 struct sctp_sock *sp = sctp_sk(sk);
6422 struct sctp_association *asoc;
6423 struct sctp_assoc_ids *ids;
6426 if (sctp_style(sk, TCP))
6429 if (len < sizeof(struct sctp_assoc_ids))
6432 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6436 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6439 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6441 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
6445 ids->gaids_number_of_ids = num;
6447 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6448 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6451 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6461 * SCTP_PEER_ADDR_THLDS
6463 * This option allows us to fetch the partially failed threshold for one or all
6464 * transports in an association. See Section 6.1 of:
6465 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6467 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6468 char __user *optval,
6472 struct sctp_paddrthlds val;
6473 struct sctp_transport *trans;
6474 struct sctp_association *asoc;
6476 if (len < sizeof(struct sctp_paddrthlds))
6478 len = sizeof(struct sctp_paddrthlds);
6479 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6482 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6483 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6487 val.spt_pathpfthld = asoc->pf_retrans;
6488 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6490 trans = sctp_addr_id2transport(sk, &val.spt_address,
6495 val.spt_pathmaxrxt = trans->pathmaxrxt;
6496 val.spt_pathpfthld = trans->pf_retrans;
6499 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6506 * SCTP_GET_ASSOC_STATS
6508 * This option retrieves local per endpoint statistics. It is modeled
6509 * after OpenSolaris' implementation
6511 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6512 char __user *optval,
6515 struct sctp_assoc_stats sas;
6516 struct sctp_association *asoc = NULL;
6518 /* User must provide at least the assoc id */
6519 if (len < sizeof(sctp_assoc_t))
6522 /* Allow the struct to grow and fill in as much as possible */
6523 len = min_t(size_t, len, sizeof(sas));
6525 if (copy_from_user(&sas, optval, len))
6528 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6532 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6533 sas.sas_gapcnt = asoc->stats.gapcnt;
6534 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6535 sas.sas_osacks = asoc->stats.osacks;
6536 sas.sas_isacks = asoc->stats.isacks;
6537 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6538 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6539 sas.sas_oodchunks = asoc->stats.oodchunks;
6540 sas.sas_iodchunks = asoc->stats.iodchunks;
6541 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6542 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6543 sas.sas_idupchunks = asoc->stats.idupchunks;
6544 sas.sas_opackets = asoc->stats.opackets;
6545 sas.sas_ipackets = asoc->stats.ipackets;
6547 /* New high max rto observed, will return 0 if not a single
6548 * RTO update took place. obs_rto_ipaddr will be bogus
6551 sas.sas_maxrto = asoc->stats.max_obs_rto;
6552 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6553 sizeof(struct sockaddr_storage));
6555 /* Mark beginning of a new observation period */
6556 asoc->stats.max_obs_rto = asoc->rto_min;
6558 if (put_user(len, optlen))
6561 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
6563 if (copy_to_user(optval, &sas, len))
6569 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6570 char __user *optval,
6575 if (len < sizeof(int))
6579 if (sctp_sk(sk)->recvrcvinfo)
6581 if (put_user(len, optlen))
6583 if (copy_to_user(optval, &val, len))
6589 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6590 char __user *optval,
6595 if (len < sizeof(int))
6599 if (sctp_sk(sk)->recvnxtinfo)
6601 if (put_user(len, optlen))
6603 if (copy_to_user(optval, &val, len))
6609 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6610 char __user *optval,
6613 struct sctp_assoc_value params;
6614 struct sctp_association *asoc;
6615 int retval = -EFAULT;
6617 if (len < sizeof(params)) {
6622 len = sizeof(params);
6623 if (copy_from_user(¶ms, optval, len))
6626 asoc = sctp_id2assoc(sk, params.assoc_id);
6628 params.assoc_value = asoc->prsctp_enable;
6629 } else if (!params.assoc_id) {
6630 struct sctp_sock *sp = sctp_sk(sk);
6632 params.assoc_value = sp->ep->prsctp_enable;
6638 if (put_user(len, optlen))
6641 if (copy_to_user(optval, ¶ms, len))
6650 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6651 char __user *optval,
6654 struct sctp_default_prinfo info;
6655 struct sctp_association *asoc;
6656 int retval = -EFAULT;
6658 if (len < sizeof(info)) {
6664 if (copy_from_user(&info, optval, len))
6667 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6669 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6670 info.pr_value = asoc->default_timetolive;
6671 } else if (!info.pr_assoc_id) {
6672 struct sctp_sock *sp = sctp_sk(sk);
6674 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6675 info.pr_value = sp->default_timetolive;
6681 if (put_user(len, optlen))
6684 if (copy_to_user(optval, &info, len))
6693 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6694 char __user *optval,
6697 struct sctp_prstatus params;
6698 struct sctp_association *asoc;
6700 int retval = -EINVAL;
6702 if (len < sizeof(params))
6705 len = sizeof(params);
6706 if (copy_from_user(¶ms, optval, len)) {
6711 policy = params.sprstat_policy;
6712 if (policy & ~SCTP_PR_SCTP_MASK)
6715 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6719 if (policy == SCTP_PR_SCTP_NONE) {
6720 params.sprstat_abandoned_unsent = 0;
6721 params.sprstat_abandoned_sent = 0;
6722 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6723 params.sprstat_abandoned_unsent +=
6724 asoc->abandoned_unsent[policy];
6725 params.sprstat_abandoned_sent +=
6726 asoc->abandoned_sent[policy];
6729 params.sprstat_abandoned_unsent =
6730 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6731 params.sprstat_abandoned_sent =
6732 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6735 if (put_user(len, optlen)) {
6740 if (copy_to_user(optval, ¶ms, len)) {
6751 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6752 char __user *optval,
6755 struct sctp_stream_out_ext *streamoute;
6756 struct sctp_association *asoc;
6757 struct sctp_prstatus params;
6758 int retval = -EINVAL;
6761 if (len < sizeof(params))
6764 len = sizeof(params);
6765 if (copy_from_user(¶ms, optval, len)) {
6770 policy = params.sprstat_policy;
6771 if (policy & ~SCTP_PR_SCTP_MASK)
6774 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6775 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
6778 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6780 /* Not allocated yet, means all stats are 0 */
6781 params.sprstat_abandoned_unsent = 0;
6782 params.sprstat_abandoned_sent = 0;
6787 if (policy == SCTP_PR_SCTP_NONE) {
6788 params.sprstat_abandoned_unsent = 0;
6789 params.sprstat_abandoned_sent = 0;
6790 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6791 params.sprstat_abandoned_unsent +=
6792 streamoute->abandoned_unsent[policy];
6793 params.sprstat_abandoned_sent +=
6794 streamoute->abandoned_sent[policy];
6797 params.sprstat_abandoned_unsent =
6798 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6799 params.sprstat_abandoned_sent =
6800 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
6803 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
6814 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6815 char __user *optval,
6818 struct sctp_assoc_value params;
6819 struct sctp_association *asoc;
6820 int retval = -EFAULT;
6822 if (len < sizeof(params)) {
6827 len = sizeof(params);
6828 if (copy_from_user(¶ms, optval, len))
6831 asoc = sctp_id2assoc(sk, params.assoc_id);
6833 params.assoc_value = asoc->reconf_enable;
6834 } else if (!params.assoc_id) {
6835 struct sctp_sock *sp = sctp_sk(sk);
6837 params.assoc_value = sp->ep->reconf_enable;
6843 if (put_user(len, optlen))
6846 if (copy_to_user(optval, ¶ms, len))
6855 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6856 char __user *optval,
6859 struct sctp_assoc_value params;
6860 struct sctp_association *asoc;
6861 int retval = -EFAULT;
6863 if (len < sizeof(params)) {
6868 len = sizeof(params);
6869 if (copy_from_user(¶ms, optval, len))
6872 asoc = sctp_id2assoc(sk, params.assoc_id);
6874 params.assoc_value = asoc->strreset_enable;
6875 } else if (!params.assoc_id) {
6876 struct sctp_sock *sp = sctp_sk(sk);
6878 params.assoc_value = sp->ep->strreset_enable;
6884 if (put_user(len, optlen))
6887 if (copy_to_user(optval, ¶ms, len))
6896 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6897 char __user *optval,
6900 struct sctp_assoc_value params;
6901 struct sctp_association *asoc;
6902 int retval = -EFAULT;
6904 if (len < sizeof(params)) {
6909 len = sizeof(params);
6910 if (copy_from_user(¶ms, optval, len))
6913 asoc = sctp_id2assoc(sk, params.assoc_id);
6919 params.assoc_value = sctp_sched_get_sched(asoc);
6921 if (put_user(len, optlen))
6924 if (copy_to_user(optval, ¶ms, len))
6933 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6934 char __user *optval,
6937 struct sctp_stream_value params;
6938 struct sctp_association *asoc;
6939 int retval = -EFAULT;
6941 if (len < sizeof(params)) {
6946 len = sizeof(params);
6947 if (copy_from_user(¶ms, optval, len))
6950 asoc = sctp_id2assoc(sk, params.assoc_id);
6956 retval = sctp_sched_get_value(asoc, params.stream_id,
6957 ¶ms.stream_value);
6961 if (put_user(len, optlen)) {
6966 if (copy_to_user(optval, ¶ms, len)) {
6975 static int sctp_getsockopt(struct sock *sk, int level, int optname,
6976 char __user *optval, int __user *optlen)
6981 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
6983 /* I can hardly begin to describe how wrong this is. This is
6984 * so broken as to be worse than useless. The API draft
6985 * REALLY is NOT helpful here... I am not convinced that the
6986 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
6987 * are at all well-founded.
6989 if (level != SOL_SCTP) {
6990 struct sctp_af *af = sctp_sk(sk)->pf->af;
6992 retval = af->getsockopt(sk, level, optname, optval, optlen);
6996 if (get_user(len, optlen))
7006 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7008 case SCTP_DISABLE_FRAGMENTS:
7009 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7013 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7015 case SCTP_AUTOCLOSE:
7016 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7018 case SCTP_SOCKOPT_PEELOFF:
7019 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7021 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7022 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7024 case SCTP_PEER_ADDR_PARAMS:
7025 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7028 case SCTP_DELAYED_SACK:
7029 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7033 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7035 case SCTP_GET_PEER_ADDRS:
7036 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7039 case SCTP_GET_LOCAL_ADDRS:
7040 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7043 case SCTP_SOCKOPT_CONNECTX3:
7044 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7046 case SCTP_DEFAULT_SEND_PARAM:
7047 retval = sctp_getsockopt_default_send_param(sk, len,
7050 case SCTP_DEFAULT_SNDINFO:
7051 retval = sctp_getsockopt_default_sndinfo(sk, len,
7054 case SCTP_PRIMARY_ADDR:
7055 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7058 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7061 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7063 case SCTP_ASSOCINFO:
7064 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7066 case SCTP_I_WANT_MAPPED_V4_ADDR:
7067 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7070 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7072 case SCTP_GET_PEER_ADDR_INFO:
7073 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7076 case SCTP_ADAPTATION_LAYER:
7077 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7081 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7083 case SCTP_FRAGMENT_INTERLEAVE:
7084 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7087 case SCTP_PARTIAL_DELIVERY_POINT:
7088 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7091 case SCTP_MAX_BURST:
7092 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7095 case SCTP_AUTH_CHUNK:
7096 case SCTP_AUTH_DELETE_KEY:
7097 retval = -EOPNOTSUPP;
7099 case SCTP_HMAC_IDENT:
7100 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7102 case SCTP_AUTH_ACTIVE_KEY:
7103 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7105 case SCTP_PEER_AUTH_CHUNKS:
7106 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7109 case SCTP_LOCAL_AUTH_CHUNKS:
7110 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7113 case SCTP_GET_ASSOC_NUMBER:
7114 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7116 case SCTP_GET_ASSOC_ID_LIST:
7117 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7119 case SCTP_AUTO_ASCONF:
7120 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7122 case SCTP_PEER_ADDR_THLDS:
7123 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7125 case SCTP_GET_ASSOC_STATS:
7126 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7128 case SCTP_RECVRCVINFO:
7129 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7131 case SCTP_RECVNXTINFO:
7132 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7134 case SCTP_PR_SUPPORTED:
7135 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7137 case SCTP_DEFAULT_PRINFO:
7138 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7141 case SCTP_PR_ASSOC_STATUS:
7142 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7145 case SCTP_PR_STREAM_STATUS:
7146 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7149 case SCTP_RECONFIG_SUPPORTED:
7150 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7153 case SCTP_ENABLE_STREAM_RESET:
7154 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7157 case SCTP_STREAM_SCHEDULER:
7158 retval = sctp_getsockopt_scheduler(sk, len, optval,
7161 case SCTP_STREAM_SCHEDULER_VALUE:
7162 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7166 retval = -ENOPROTOOPT;
7174 static int sctp_hash(struct sock *sk)
7180 static void sctp_unhash(struct sock *sk)
7185 /* Check if port is acceptable. Possibly find first available port.
7187 * The port hash table (contained in the 'global' SCTP protocol storage
7188 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7189 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7190 * list (the list number is the port number hashed out, so as you
7191 * would expect from a hash function, all the ports in a given list have
7192 * such a number that hashes out to the same list number; you were
7193 * expecting that, right?); so each list has a set of ports, with a
7194 * link to the socket (struct sock) that uses it, the port number and
7195 * a fastreuse flag (FIXME: NPI ipg).
7197 static struct sctp_bind_bucket *sctp_bucket_create(
7198 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
7200 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7202 struct sctp_bind_hashbucket *head; /* hash list */
7203 struct sctp_bind_bucket *pp;
7204 unsigned short snum;
7207 snum = ntohs(addr->v4.sin_port);
7209 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7214 /* Search for an available port. */
7215 int low, high, remaining, index;
7217 struct net *net = sock_net(sk);
7219 inet_get_local_port_range(net, &low, &high);
7220 remaining = (high - low) + 1;
7221 rover = prandom_u32() % remaining + low;
7225 if ((rover < low) || (rover > high))
7227 if (inet_is_local_reserved_port(net, rover))
7229 index = sctp_phashfn(sock_net(sk), rover);
7230 head = &sctp_port_hashtable[index];
7231 spin_lock(&head->lock);
7232 sctp_for_each_hentry(pp, &head->chain)
7233 if ((pp->port == rover) &&
7234 net_eq(sock_net(sk), pp->net))
7238 spin_unlock(&head->lock);
7239 } while (--remaining > 0);
7241 /* Exhausted local port range during search? */
7246 /* OK, here is the one we will use. HEAD (the port
7247 * hash table list entry) is non-NULL and we hold it's
7252 /* We are given an specific port number; we verify
7253 * that it is not being used. If it is used, we will
7254 * exahust the search in the hash list corresponding
7255 * to the port number (snum) - we detect that with the
7256 * port iterator, pp being NULL.
7258 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
7259 spin_lock(&head->lock);
7260 sctp_for_each_hentry(pp, &head->chain) {
7261 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
7268 if (!hlist_empty(&pp->owner)) {
7269 /* We had a port hash table hit - there is an
7270 * available port (pp != NULL) and it is being
7271 * used by other socket (pp->owner not empty); that other
7272 * socket is going to be sk2.
7274 int reuse = sk->sk_reuse;
7277 pr_debug("%s: found a possible match\n", __func__);
7279 if (pp->fastreuse && sk->sk_reuse &&
7280 sk->sk_state != SCTP_SS_LISTENING)
7283 /* Run through the list of sockets bound to the port
7284 * (pp->port) [via the pointers bind_next and
7285 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7286 * we get the endpoint they describe and run through
7287 * the endpoint's list of IP (v4 or v6) addresses,
7288 * comparing each of the addresses with the address of
7289 * the socket sk. If we find a match, then that means
7290 * that this port/socket (sk) combination are already
7293 sk_for_each_bound(sk2, &pp->owner) {
7294 struct sctp_endpoint *ep2;
7295 ep2 = sctp_sk(sk2)->ep;
7298 (reuse && sk2->sk_reuse &&
7299 sk2->sk_state != SCTP_SS_LISTENING))
7302 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7303 sctp_sk(sk2), sctp_sk(sk))) {
7309 pr_debug("%s: found a match\n", __func__);
7312 /* If there was a hash table miss, create a new port. */
7314 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
7317 /* In either case (hit or miss), make sure fastreuse is 1 only
7318 * if sk->sk_reuse is too (that is, if the caller requested
7319 * SO_REUSEADDR on this socket -sk-).
7321 if (hlist_empty(&pp->owner)) {
7322 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7326 } else if (pp->fastreuse &&
7327 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
7330 /* We are set, so fill up all the data in the hash table
7331 * entry, tie the socket list information with the rest of the
7332 * sockets FIXME: Blurry, NPI (ipg).
7335 if (!sctp_sk(sk)->bind_hash) {
7336 inet_sk(sk)->inet_num = snum;
7337 sk_add_bind_node(sk, &pp->owner);
7338 sctp_sk(sk)->bind_hash = pp;
7343 spin_unlock(&head->lock);
7350 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7351 * port is requested.
7353 static int sctp_get_port(struct sock *sk, unsigned short snum)
7355 union sctp_addr addr;
7356 struct sctp_af *af = sctp_sk(sk)->pf->af;
7358 /* Set up a dummy address struct from the sk. */
7359 af->from_sk(&addr, sk);
7360 addr.v4.sin_port = htons(snum);
7362 /* Note: sk->sk_num gets filled in if ephemeral port request. */
7363 return !!sctp_get_port_local(sk, &addr);
7367 * Move a socket to LISTENING state.
7369 static int sctp_listen_start(struct sock *sk, int backlog)
7371 struct sctp_sock *sp = sctp_sk(sk);
7372 struct sctp_endpoint *ep = sp->ep;
7373 struct crypto_shash *tfm = NULL;
7376 /* Allocate HMAC for generating cookie. */
7377 if (!sp->hmac && sp->sctp_hmac_alg) {
7378 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
7379 tfm = crypto_alloc_shash(alg, 0, 0);
7381 net_info_ratelimited("failed to load transform for %s: %ld\n",
7382 sp->sctp_hmac_alg, PTR_ERR(tfm));
7385 sctp_sk(sk)->hmac = tfm;
7389 * If a bind() or sctp_bindx() is not called prior to a listen()
7390 * call that allows new associations to be accepted, the system
7391 * picks an ephemeral port and will choose an address set equivalent
7392 * to binding with a wildcard address.
7394 * This is not currently spelled out in the SCTP sockets
7395 * extensions draft, but follows the practice as seen in TCP
7399 sk->sk_state = SCTP_SS_LISTENING;
7400 if (!ep->base.bind_addr.port) {
7401 if (sctp_autobind(sk))
7404 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
7405 sk->sk_state = SCTP_SS_CLOSED;
7410 sk->sk_max_ack_backlog = backlog;
7411 sctp_hash_endpoint(ep);
7416 * 4.1.3 / 5.1.3 listen()
7418 * By default, new associations are not accepted for UDP style sockets.
7419 * An application uses listen() to mark a socket as being able to
7420 * accept new associations.
7422 * On TCP style sockets, applications use listen() to ready the SCTP
7423 * endpoint for accepting inbound associations.
7425 * On both types of endpoints a backlog of '0' disables listening.
7427 * Move a socket to LISTENING state.
7429 int sctp_inet_listen(struct socket *sock, int backlog)
7431 struct sock *sk = sock->sk;
7432 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7435 if (unlikely(backlog < 0))
7440 /* Peeled-off sockets are not allowed to listen(). */
7441 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7444 if (sock->state != SS_UNCONNECTED)
7447 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7450 /* If backlog is zero, disable listening. */
7452 if (sctp_sstate(sk, CLOSED))
7456 sctp_unhash_endpoint(ep);
7457 sk->sk_state = SCTP_SS_CLOSED;
7459 sctp_sk(sk)->bind_hash->fastreuse = 1;
7463 /* If we are already listening, just update the backlog */
7464 if (sctp_sstate(sk, LISTENING))
7465 sk->sk_max_ack_backlog = backlog;
7467 err = sctp_listen_start(sk, backlog);
7479 * This function is done by modeling the current datagram_poll() and the
7480 * tcp_poll(). Note that, based on these implementations, we don't
7481 * lock the socket in this function, even though it seems that,
7482 * ideally, locking or some other mechanisms can be used to ensure
7483 * the integrity of the counters (sndbuf and wmem_alloc) used
7484 * in this place. We assume that we don't need locks either until proven
7487 * Another thing to note is that we include the Async I/O support
7488 * here, again, by modeling the current TCP/UDP code. We don't have
7489 * a good way to test with it yet.
7491 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7493 struct sock *sk = sock->sk;
7494 struct sctp_sock *sp = sctp_sk(sk);
7497 poll_wait(file, sk_sleep(sk), wait);
7499 sock_rps_record_flow(sk);
7501 /* A TCP-style listening socket becomes readable when the accept queue
7504 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7505 return (!list_empty(&sp->ep->asocs)) ?
7506 (POLLIN | POLLRDNORM) : 0;
7510 /* Is there any exceptional events? */
7511 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
7513 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
7514 if (sk->sk_shutdown & RCV_SHUTDOWN)
7515 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
7516 if (sk->sk_shutdown == SHUTDOWN_MASK)
7519 /* Is it readable? Reconsider this code with TCP-style support. */
7520 if (!skb_queue_empty(&sk->sk_receive_queue))
7521 mask |= POLLIN | POLLRDNORM;
7523 /* The association is either gone or not ready. */
7524 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7527 /* Is it writable? */
7528 if (sctp_writeable(sk)) {
7529 mask |= POLLOUT | POLLWRNORM;
7531 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
7533 * Since the socket is not locked, the buffer
7534 * might be made available after the writeable check and
7535 * before the bit is set. This could cause a lost I/O
7536 * signal. tcp_poll() has a race breaker for this race
7537 * condition. Based on their implementation, we put
7538 * in the following code to cover it as well.
7540 if (sctp_writeable(sk))
7541 mask |= POLLOUT | POLLWRNORM;
7546 /********************************************************************
7547 * 2nd Level Abstractions
7548 ********************************************************************/
7550 static struct sctp_bind_bucket *sctp_bucket_create(
7551 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
7553 struct sctp_bind_bucket *pp;
7555 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
7557 SCTP_DBG_OBJCNT_INC(bind_bucket);
7560 INIT_HLIST_HEAD(&pp->owner);
7562 hlist_add_head(&pp->node, &head->chain);
7567 /* Caller must hold hashbucket lock for this tb with local BH disabled */
7568 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7570 if (pp && hlist_empty(&pp->owner)) {
7571 __hlist_del(&pp->node);
7572 kmem_cache_free(sctp_bucket_cachep, pp);
7573 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7577 /* Release this socket's reference to a local port. */
7578 static inline void __sctp_put_port(struct sock *sk)
7580 struct sctp_bind_hashbucket *head =
7581 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7582 inet_sk(sk)->inet_num)];
7583 struct sctp_bind_bucket *pp;
7585 spin_lock(&head->lock);
7586 pp = sctp_sk(sk)->bind_hash;
7587 __sk_del_bind_node(sk);
7588 sctp_sk(sk)->bind_hash = NULL;
7589 inet_sk(sk)->inet_num = 0;
7590 sctp_bucket_destroy(pp);
7591 spin_unlock(&head->lock);
7594 void sctp_put_port(struct sock *sk)
7597 __sctp_put_port(sk);
7602 * The system picks an ephemeral port and choose an address set equivalent
7603 * to binding with a wildcard address.
7604 * One of those addresses will be the primary address for the association.
7605 * This automatically enables the multihoming capability of SCTP.
7607 static int sctp_autobind(struct sock *sk)
7609 union sctp_addr autoaddr;
7613 /* Initialize a local sockaddr structure to INADDR_ANY. */
7614 af = sctp_sk(sk)->pf->af;
7616 port = htons(inet_sk(sk)->inet_num);
7617 af->inaddr_any(&autoaddr, port);
7619 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7622 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7625 * 4.2 The cmsghdr Structure *
7627 * When ancillary data is sent or received, any number of ancillary data
7628 * objects can be specified by the msg_control and msg_controllen members of
7629 * the msghdr structure, because each object is preceded by
7630 * a cmsghdr structure defining the object's length (the cmsg_len member).
7631 * Historically Berkeley-derived implementations have passed only one object
7632 * at a time, but this API allows multiple objects to be
7633 * passed in a single call to sendmsg() or recvmsg(). The following example
7634 * shows two ancillary data objects in a control buffer.
7636 * |<--------------------------- msg_controllen -------------------------->|
7639 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7641 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7644 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7646 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7649 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7650 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7652 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7654 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7661 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
7663 struct msghdr *my_msg = (struct msghdr *)msg;
7664 struct cmsghdr *cmsg;
7666 for_each_cmsghdr(cmsg, my_msg) {
7667 if (!CMSG_OK(my_msg, cmsg))
7670 /* Should we parse this header or ignore? */
7671 if (cmsg->cmsg_level != IPPROTO_SCTP)
7674 /* Strictly check lengths following example in SCM code. */
7675 switch (cmsg->cmsg_type) {
7677 /* SCTP Socket API Extension
7678 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
7680 * This cmsghdr structure provides information for
7681 * initializing new SCTP associations with sendmsg().
7682 * The SCTP_INITMSG socket option uses this same data
7683 * structure. This structure is not used for
7686 * cmsg_level cmsg_type cmsg_data[]
7687 * ------------ ------------ ----------------------
7688 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7690 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
7693 cmsgs->init = CMSG_DATA(cmsg);
7697 /* SCTP Socket API Extension
7698 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
7700 * This cmsghdr structure specifies SCTP options for
7701 * sendmsg() and describes SCTP header information
7702 * about a received message through recvmsg().
7704 * cmsg_level cmsg_type cmsg_data[]
7705 * ------------ ------------ ----------------------
7706 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7708 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
7711 cmsgs->srinfo = CMSG_DATA(cmsg);
7713 if (cmsgs->srinfo->sinfo_flags &
7714 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
7715 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
7716 SCTP_ABORT | SCTP_EOF))
7721 /* SCTP Socket API Extension
7722 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7724 * This cmsghdr structure specifies SCTP options for
7725 * sendmsg(). This structure and SCTP_RCVINFO replaces
7726 * SCTP_SNDRCV which has been deprecated.
7728 * cmsg_level cmsg_type cmsg_data[]
7729 * ------------ ------------ ---------------------
7730 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7732 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7735 cmsgs->sinfo = CMSG_DATA(cmsg);
7737 if (cmsgs->sinfo->snd_flags &
7738 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
7739 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
7740 SCTP_ABORT | SCTP_EOF))
7752 * Wait for a packet..
7753 * Note: This function is the same function as in core/datagram.c
7754 * with a few modifications to make lksctp work.
7756 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
7761 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
7763 /* Socket errors? */
7764 error = sock_error(sk);
7768 if (!skb_queue_empty(&sk->sk_receive_queue))
7771 /* Socket shut down? */
7772 if (sk->sk_shutdown & RCV_SHUTDOWN)
7775 /* Sequenced packets can come disconnected. If so we report the
7780 /* Is there a good reason to think that we may receive some data? */
7781 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7784 /* Handle signals. */
7785 if (signal_pending(current))
7788 /* Let another process have a go. Since we are going to sleep
7789 * anyway. Note: This may cause odd behaviors if the message
7790 * does not fit in the user's buffer, but this seems to be the
7791 * only way to honor MSG_DONTWAIT realistically.
7794 *timeo_p = schedule_timeout(*timeo_p);
7798 finish_wait(sk_sleep(sk), &wait);
7802 error = sock_intr_errno(*timeo_p);
7805 finish_wait(sk_sleep(sk), &wait);
7810 /* Receive a datagram.
7811 * Note: This is pretty much the same routine as in core/datagram.c
7812 * with a few changes to make lksctp work.
7814 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7815 int noblock, int *err)
7818 struct sk_buff *skb;
7821 timeo = sock_rcvtimeo(sk, noblock);
7823 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7824 MAX_SCHEDULE_TIMEOUT);
7827 /* Again only user level code calls this function,
7828 * so nothing interrupt level
7829 * will suddenly eat the receive_queue.
7831 * Look at current nfs client by the way...
7832 * However, this function was correct in any case. 8)
7834 if (flags & MSG_PEEK) {
7835 skb = skb_peek(&sk->sk_receive_queue);
7837 refcount_inc(&skb->users);
7839 skb = __skb_dequeue(&sk->sk_receive_queue);
7845 /* Caller is allowed not to check sk->sk_err before calling. */
7846 error = sock_error(sk);
7850 if (sk->sk_shutdown & RCV_SHUTDOWN)
7853 if (sk_can_busy_loop(sk)) {
7854 sk_busy_loop(sk, noblock);
7856 if (!skb_queue_empty(&sk->sk_receive_queue))
7860 /* User doesn't want to wait. */
7864 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7873 /* If sndbuf has changed, wake up per association sndbuf waiters. */
7874 static void __sctp_write_space(struct sctp_association *asoc)
7876 struct sock *sk = asoc->base.sk;
7878 if (sctp_wspace(asoc) <= 0)
7881 if (waitqueue_active(&asoc->wait))
7882 wake_up_interruptible(&asoc->wait);
7884 if (sctp_writeable(sk)) {
7885 struct socket_wq *wq;
7888 wq = rcu_dereference(sk->sk_wq);
7890 if (waitqueue_active(&wq->wait))
7891 wake_up_interruptible(&wq->wait);
7893 /* Note that we try to include the Async I/O support
7894 * here by modeling from the current TCP/UDP code.
7895 * We have not tested with it yet.
7897 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
7898 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
7904 static void sctp_wake_up_waiters(struct sock *sk,
7905 struct sctp_association *asoc)
7907 struct sctp_association *tmp = asoc;
7909 /* We do accounting for the sndbuf space per association,
7910 * so we only need to wake our own association.
7912 if (asoc->ep->sndbuf_policy)
7913 return __sctp_write_space(asoc);
7915 /* If association goes down and is just flushing its
7916 * outq, then just normally notify others.
7918 if (asoc->base.dead)
7919 return sctp_write_space(sk);
7921 /* Accounting for the sndbuf space is per socket, so we
7922 * need to wake up others, try to be fair and in case of
7923 * other associations, let them have a go first instead
7924 * of just doing a sctp_write_space() call.
7926 * Note that we reach sctp_wake_up_waiters() only when
7927 * associations free up queued chunks, thus we are under
7928 * lock and the list of associations on a socket is
7929 * guaranteed not to change.
7931 for (tmp = list_next_entry(tmp, asocs); 1;
7932 tmp = list_next_entry(tmp, asocs)) {
7933 /* Manually skip the head element. */
7934 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7936 /* Wake up association. */
7937 __sctp_write_space(tmp);
7938 /* We've reached the end. */
7944 /* Do accounting for the sndbuf space.
7945 * Decrement the used sndbuf space of the corresponding association by the
7946 * data size which was just transmitted(freed).
7948 static void sctp_wfree(struct sk_buff *skb)
7950 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7951 struct sctp_association *asoc = chunk->asoc;
7952 struct sock *sk = asoc->base.sk;
7954 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7955 sizeof(struct sk_buff) +
7956 sizeof(struct sctp_chunk);
7958 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
7961 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
7963 sk->sk_wmem_queued -= skb->truesize;
7964 sk_mem_uncharge(sk, skb->truesize);
7967 sctp_wake_up_waiters(sk, asoc);
7969 sctp_association_put(asoc);
7972 /* Do accounting for the receive space on the socket.
7973 * Accounting for the association is done in ulpevent.c
7974 * We set this as a destructor for the cloned data skbs so that
7975 * accounting is done at the correct time.
7977 void sctp_sock_rfree(struct sk_buff *skb)
7979 struct sock *sk = skb->sk;
7980 struct sctp_ulpevent *event = sctp_skb2event(skb);
7982 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
7985 * Mimic the behavior of sock_rfree
7987 sk_mem_uncharge(sk, event->rmem_len);
7991 /* Helper function to wait for space in the sndbuf. */
7992 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
7993 size_t msg_len, struct sock **orig_sk)
7995 struct sock *sk = asoc->base.sk;
7997 long current_timeo = *timeo_p;
8000 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8003 /* Increment the association's refcnt. */
8004 sctp_association_hold(asoc);
8006 /* Wait on the association specific sndbuf space. */
8008 prepare_to_wait_exclusive(&asoc->wait, &wait,
8009 TASK_INTERRUPTIBLE);
8010 if (asoc->base.dead)
8014 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8016 if (signal_pending(current))
8017 goto do_interrupted;
8018 if (msg_len <= sctp_wspace(asoc))
8021 /* Let another process have a go. Since we are going
8025 current_timeo = schedule_timeout(current_timeo);
8027 if (sk != asoc->base.sk) {
8033 *timeo_p = current_timeo;
8038 finish_wait(&asoc->wait, &wait);
8040 /* Release the association's refcnt. */
8041 sctp_association_put(asoc);
8054 err = sock_intr_errno(*timeo_p);
8062 void sctp_data_ready(struct sock *sk)
8064 struct socket_wq *wq;
8067 wq = rcu_dereference(sk->sk_wq);
8068 if (skwq_has_sleeper(wq))
8069 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
8070 POLLRDNORM | POLLRDBAND);
8071 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8075 /* If socket sndbuf has changed, wake up all per association waiters. */
8076 void sctp_write_space(struct sock *sk)
8078 struct sctp_association *asoc;
8080 /* Wake up the tasks in each wait queue. */
8081 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8082 __sctp_write_space(asoc);
8086 /* Is there any sndbuf space available on the socket?
8088 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8089 * associations on the same socket. For a UDP-style socket with
8090 * multiple associations, it is possible for it to be "unwriteable"
8091 * prematurely. I assume that this is acceptable because
8092 * a premature "unwriteable" is better than an accidental "writeable" which
8093 * would cause an unwanted block under certain circumstances. For the 1-1
8094 * UDP-style sockets or TCP-style sockets, this code should work.
8097 static int sctp_writeable(struct sock *sk)
8101 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
8107 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8108 * returns immediately with EINPROGRESS.
8110 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8112 struct sock *sk = asoc->base.sk;
8114 long current_timeo = *timeo_p;
8117 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8119 /* Increment the association's refcnt. */
8120 sctp_association_hold(asoc);
8123 prepare_to_wait_exclusive(&asoc->wait, &wait,
8124 TASK_INTERRUPTIBLE);
8127 if (sk->sk_shutdown & RCV_SHUTDOWN)
8129 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8132 if (signal_pending(current))
8133 goto do_interrupted;
8135 if (sctp_state(asoc, ESTABLISHED))
8138 /* Let another process have a go. Since we are going
8142 current_timeo = schedule_timeout(current_timeo);
8145 *timeo_p = current_timeo;
8149 finish_wait(&asoc->wait, &wait);
8151 /* Release the association's refcnt. */
8152 sctp_association_put(asoc);
8157 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
8160 err = -ECONNREFUSED;
8164 err = sock_intr_errno(*timeo_p);
8172 static int sctp_wait_for_accept(struct sock *sk, long timeo)
8174 struct sctp_endpoint *ep;
8178 ep = sctp_sk(sk)->ep;
8182 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
8183 TASK_INTERRUPTIBLE);
8185 if (list_empty(&ep->asocs)) {
8187 timeo = schedule_timeout(timeo);
8192 if (!sctp_sstate(sk, LISTENING))
8196 if (!list_empty(&ep->asocs))
8199 err = sock_intr_errno(timeo);
8200 if (signal_pending(current))
8208 finish_wait(sk_sleep(sk), &wait);
8213 static void sctp_wait_for_close(struct sock *sk, long timeout)
8218 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8219 if (list_empty(&sctp_sk(sk)->ep->asocs))
8222 timeout = schedule_timeout(timeout);
8224 } while (!signal_pending(current) && timeout);
8226 finish_wait(sk_sleep(sk), &wait);
8229 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8231 struct sk_buff *frag;
8236 /* Don't forget the fragments. */
8237 skb_walk_frags(skb, frag)
8238 sctp_skb_set_owner_r_frag(frag, sk);
8241 sctp_skb_set_owner_r(skb, sk);
8244 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8245 struct sctp_association *asoc)
8247 struct inet_sock *inet = inet_sk(sk);
8248 struct inet_sock *newinet;
8250 newsk->sk_type = sk->sk_type;
8251 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8252 newsk->sk_flags = sk->sk_flags;
8253 newsk->sk_tsflags = sk->sk_tsflags;
8254 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8255 newsk->sk_no_check_rx = sk->sk_no_check_rx;
8256 newsk->sk_reuse = sk->sk_reuse;
8258 newsk->sk_shutdown = sk->sk_shutdown;
8259 newsk->sk_destruct = sctp_destruct_sock;
8260 newsk->sk_family = sk->sk_family;
8261 newsk->sk_protocol = IPPROTO_SCTP;
8262 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8263 newsk->sk_sndbuf = sk->sk_sndbuf;
8264 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8265 newsk->sk_lingertime = sk->sk_lingertime;
8266 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8267 newsk->sk_sndtimeo = sk->sk_sndtimeo;
8268 newsk->sk_rxhash = sk->sk_rxhash;
8270 newinet = inet_sk(newsk);
8272 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8273 * getsockname() and getpeername()
8275 newinet->inet_sport = inet->inet_sport;
8276 newinet->inet_saddr = inet->inet_saddr;
8277 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8278 newinet->inet_dport = htons(asoc->peer.port);
8279 newinet->pmtudisc = inet->pmtudisc;
8280 newinet->inet_id = asoc->next_tsn ^ jiffies;
8282 newinet->uc_ttl = inet->uc_ttl;
8283 newinet->mc_loop = 1;
8284 newinet->mc_ttl = 1;
8285 newinet->mc_index = 0;
8286 newinet->mc_list = NULL;
8288 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8289 net_enable_timestamp();
8291 security_sk_clone(sk, newsk);
8294 static inline void sctp_copy_descendant(struct sock *sk_to,
8295 const struct sock *sk_from)
8297 int ancestor_size = sizeof(struct inet_sock) +
8298 sizeof(struct sctp_sock) -
8299 offsetof(struct sctp_sock, auto_asconf_list);
8301 if (sk_from->sk_family == PF_INET6)
8302 ancestor_size += sizeof(struct ipv6_pinfo);
8304 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8307 /* Populate the fields of the newsk from the oldsk and migrate the assoc
8308 * and its messages to the newsk.
8310 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8311 struct sctp_association *assoc,
8312 enum sctp_socket_type type)
8314 struct sctp_sock *oldsp = sctp_sk(oldsk);
8315 struct sctp_sock *newsp = sctp_sk(newsk);
8316 struct sctp_bind_bucket *pp; /* hash list port iterator */
8317 struct sctp_endpoint *newep = newsp->ep;
8318 struct sk_buff *skb, *tmp;
8319 struct sctp_ulpevent *event;
8320 struct sctp_bind_hashbucket *head;
8322 /* Migrate socket buffer sizes and all the socket level options to the
8325 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8326 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8327 /* Brute force copy old sctp opt. */
8328 sctp_copy_descendant(newsk, oldsk);
8330 /* Restore the ep value that was overwritten with the above structure
8336 /* Hook this new socket in to the bind_hash list. */
8337 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8338 inet_sk(oldsk)->inet_num)];
8339 spin_lock_bh(&head->lock);
8340 pp = sctp_sk(oldsk)->bind_hash;
8341 sk_add_bind_node(newsk, &pp->owner);
8342 sctp_sk(newsk)->bind_hash = pp;
8343 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
8344 spin_unlock_bh(&head->lock);
8346 /* Copy the bind_addr list from the original endpoint to the new
8347 * endpoint so that we can handle restarts properly
8349 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8350 &oldsp->ep->base.bind_addr, GFP_KERNEL);
8352 /* Move any messages in the old socket's receive queue that are for the
8353 * peeled off association to the new socket's receive queue.
8355 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8356 event = sctp_skb2event(skb);
8357 if (event->asoc == assoc) {
8358 __skb_unlink(skb, &oldsk->sk_receive_queue);
8359 __skb_queue_tail(&newsk->sk_receive_queue, skb);
8360 sctp_skb_set_owner_r_frag(skb, newsk);
8364 /* Clean up any messages pending delivery due to partial
8365 * delivery. Three cases:
8366 * 1) No partial deliver; no work.
8367 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8368 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8370 skb_queue_head_init(&newsp->pd_lobby);
8371 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
8373 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
8374 struct sk_buff_head *queue;
8376 /* Decide which queue to move pd_lobby skbs to. */
8377 if (assoc->ulpq.pd_mode) {
8378 queue = &newsp->pd_lobby;
8380 queue = &newsk->sk_receive_queue;
8382 /* Walk through the pd_lobby, looking for skbs that
8383 * need moved to the new socket.
8385 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8386 event = sctp_skb2event(skb);
8387 if (event->asoc == assoc) {
8388 __skb_unlink(skb, &oldsp->pd_lobby);
8389 __skb_queue_tail(queue, skb);
8390 sctp_skb_set_owner_r_frag(skb, newsk);
8394 /* Clear up any skbs waiting for the partial
8395 * delivery to finish.
8397 if (assoc->ulpq.pd_mode)
8398 sctp_clear_pd(oldsk, NULL);
8402 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
8403 sctp_skb_set_owner_r_frag(skb, newsk);
8405 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
8406 sctp_skb_set_owner_r_frag(skb, newsk);
8408 /* Set the type of socket to indicate that it is peeled off from the
8409 * original UDP-style socket or created with the accept() call on a
8410 * TCP-style socket..
8414 /* Mark the new socket "in-use" by the user so that any packets
8415 * that may arrive on the association after we've moved it are
8416 * queued to the backlog. This prevents a potential race between
8417 * backlog processing on the old socket and new-packet processing
8418 * on the new socket.
8420 * The caller has just allocated newsk so we can guarantee that other
8421 * paths won't try to lock it and then oldsk.
8423 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
8424 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
8425 sctp_assoc_migrate(assoc, newsk);
8426 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
8428 /* If the association on the newsk is already closed before accept()
8429 * is called, set RCV_SHUTDOWN flag.
8431 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
8432 newsk->sk_state = SCTP_SS_CLOSED;
8433 newsk->sk_shutdown |= RCV_SHUTDOWN;
8435 newsk->sk_state = SCTP_SS_ESTABLISHED;
8438 release_sock(newsk);
8442 /* This proto struct describes the ULP interface for SCTP. */
8443 struct proto sctp_prot = {
8445 .owner = THIS_MODULE,
8446 .close = sctp_close,
8447 .connect = sctp_connect,
8448 .disconnect = sctp_disconnect,
8449 .accept = sctp_accept,
8450 .ioctl = sctp_ioctl,
8451 .init = sctp_init_sock,
8452 .destroy = sctp_destroy_sock,
8453 .shutdown = sctp_shutdown,
8454 .setsockopt = sctp_setsockopt,
8455 .getsockopt = sctp_getsockopt,
8456 .sendmsg = sctp_sendmsg,
8457 .recvmsg = sctp_recvmsg,
8459 .backlog_rcv = sctp_backlog_rcv,
8461 .unhash = sctp_unhash,
8462 .get_port = sctp_get_port,
8463 .obj_size = sizeof(struct sctp_sock),
8464 .sysctl_mem = sysctl_sctp_mem,
8465 .sysctl_rmem = sysctl_sctp_rmem,
8466 .sysctl_wmem = sysctl_sctp_wmem,
8467 .memory_pressure = &sctp_memory_pressure,
8468 .enter_memory_pressure = sctp_enter_memory_pressure,
8469 .memory_allocated = &sctp_memory_allocated,
8470 .sockets_allocated = &sctp_sockets_allocated,
8473 #if IS_ENABLED(CONFIG_IPV6)
8475 #include <net/transp_v6.h>
8476 static void sctp_v6_destroy_sock(struct sock *sk)
8478 sctp_destroy_sock(sk);
8479 inet6_destroy_sock(sk);
8482 struct proto sctpv6_prot = {
8484 .owner = THIS_MODULE,
8485 .close = sctp_close,
8486 .connect = sctp_connect,
8487 .disconnect = sctp_disconnect,
8488 .accept = sctp_accept,
8489 .ioctl = sctp_ioctl,
8490 .init = sctp_init_sock,
8491 .destroy = sctp_v6_destroy_sock,
8492 .shutdown = sctp_shutdown,
8493 .setsockopt = sctp_setsockopt,
8494 .getsockopt = sctp_getsockopt,
8495 .sendmsg = sctp_sendmsg,
8496 .recvmsg = sctp_recvmsg,
8498 .backlog_rcv = sctp_backlog_rcv,
8500 .unhash = sctp_unhash,
8501 .get_port = sctp_get_port,
8502 .obj_size = sizeof(struct sctp6_sock),
8503 .sysctl_mem = sysctl_sctp_mem,
8504 .sysctl_rmem = sysctl_sctp_rmem,
8505 .sysctl_wmem = sysctl_sctp_wmem,
8506 .memory_pressure = &sctp_memory_pressure,
8507 .enter_memory_pressure = sctp_enter_memory_pressure,
8508 .memory_allocated = &sctp_memory_allocated,
8509 .sockets_allocated = &sctp_sockets_allocated,
8511 #endif /* IS_ENABLED(CONFIG_IPV6) */