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,
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, transmitted_list)
194 list_for_each_entry(chunk, &q->sacked, transmitted_list)
197 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
200 list_for_each_entry(chunk, &q->out_chunk_list, list)
204 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
205 void (*cb)(struct sk_buff *, struct sock *))
208 struct sk_buff *skb, *tmp;
210 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
213 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
220 /* Verify that this is a valid address. */
221 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
226 /* Verify basic sockaddr. */
227 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
231 /* Is this a valid SCTP address? */
232 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
235 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
241 /* Look up the association by its id. If this is not a UDP-style
242 * socket, the ID field is always ignored.
244 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
246 struct sctp_association *asoc = NULL;
248 /* If this is not a UDP-style socket, assoc id should be ignored. */
249 if (!sctp_style(sk, UDP)) {
250 /* Return NULL if the socket state is not ESTABLISHED. It
251 * could be a TCP-style listening socket or a socket which
252 * hasn't yet called connect() to establish an association.
254 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
257 /* Get the first and the only association from the list. */
258 if (!list_empty(&sctp_sk(sk)->ep->asocs))
259 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
260 struct sctp_association, asocs);
264 /* Otherwise this is a UDP-style socket. */
265 if (!id || (id == (sctp_assoc_t)-1))
268 spin_lock_bh(&sctp_assocs_id_lock);
269 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
270 spin_unlock_bh(&sctp_assocs_id_lock);
272 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
278 /* Look up the transport from an address and an assoc id. If both address and
279 * id are specified, the associations matching the address and the id should be
282 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
283 struct sockaddr_storage *addr,
286 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
287 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
288 union sctp_addr *laddr = (union sctp_addr *)addr;
289 struct sctp_transport *transport;
291 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
294 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
301 id_asoc = sctp_id2assoc(sk, id);
302 if (id_asoc && (id_asoc != addr_asoc))
305 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
306 (union sctp_addr *)addr);
311 /* API 3.1.2 bind() - UDP Style Syntax
312 * The syntax of bind() is,
314 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
316 * sd - the socket descriptor returned by socket().
317 * addr - the address structure (struct sockaddr_in or struct
318 * sockaddr_in6 [RFC 2553]),
319 * addr_len - the size of the address structure.
321 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
327 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
330 /* Disallow binding twice. */
331 if (!sctp_sk(sk)->ep->base.bind_addr.port)
332 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
342 static long sctp_get_port_local(struct sock *, union sctp_addr *);
344 /* Verify this is a valid sockaddr. */
345 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
346 union sctp_addr *addr, int len)
350 /* Check minimum size. */
351 if (len < sizeof (struct sockaddr))
354 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
357 /* V4 mapped address are really of AF_INET family */
358 if (addr->sa.sa_family == AF_INET6 &&
359 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
360 !opt->pf->af_supported(AF_INET, opt))
363 /* If we get this far, af is valid. */
364 af = sctp_get_af_specific(addr->sa.sa_family);
366 if (len < af->sockaddr_len)
372 /* Bind a local address either to an endpoint or to an association. */
373 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
375 struct net *net = sock_net(sk);
376 struct sctp_sock *sp = sctp_sk(sk);
377 struct sctp_endpoint *ep = sp->ep;
378 struct sctp_bind_addr *bp = &ep->base.bind_addr;
383 /* Common sockaddr verification. */
384 af = sctp_sockaddr_af(sp, addr, len);
386 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387 __func__, sk, addr, len);
391 snum = ntohs(addr->v4.sin_port);
393 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394 __func__, sk, &addr->sa, bp->port, snum, len);
396 /* PF specific bind() address verification. */
397 if (!sp->pf->bind_verify(sp, addr))
398 return -EADDRNOTAVAIL;
400 /* We must either be unbound, or bind to the same port.
401 * It's OK to allow 0 ports if we are already bound.
402 * We'll just inhert an already bound port in this case
407 else if (snum != bp->port) {
408 pr_debug("%s: new port %d doesn't match existing port "
409 "%d\n", __func__, snum, bp->port);
414 if (snum && snum < inet_prot_sock(net) &&
415 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
418 /* See if the address matches any of the addresses we may have
419 * already bound before checking against other endpoints.
421 if (sctp_bind_addr_match(bp, addr, sp))
424 /* Make sure we are allowed to bind here.
425 * The function sctp_get_port_local() does duplicate address
428 addr->v4.sin_port = htons(snum);
429 if ((ret = sctp_get_port_local(sk, addr))) {
433 /* Refresh ephemeral port. */
435 bp->port = inet_sk(sk)->inet_num;
437 /* Add the address to the bind address list.
438 * Use GFP_ATOMIC since BHs will be disabled.
440 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
441 SCTP_ADDR_SRC, GFP_ATOMIC);
443 /* Copy back into socket for getsockname() use. */
445 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
446 sp->pf->to_sk_saddr(addr, sk);
452 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
454 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
455 * at any one time. If a sender, after sending an ASCONF chunk, decides
456 * it needs to transfer another ASCONF Chunk, it MUST wait until the
457 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
458 * subsequent ASCONF. Note this restriction binds each side, so at any
459 * time two ASCONF may be in-transit on any given association (one sent
460 * from each endpoint).
462 static int sctp_send_asconf(struct sctp_association *asoc,
463 struct sctp_chunk *chunk)
465 struct net *net = sock_net(asoc->base.sk);
468 /* If there is an outstanding ASCONF chunk, queue it for later
471 if (asoc->addip_last_asconf) {
472 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
476 /* Hold the chunk until an ASCONF_ACK is received. */
477 sctp_chunk_hold(chunk);
478 retval = sctp_primitive_ASCONF(net, asoc, chunk);
480 sctp_chunk_free(chunk);
482 asoc->addip_last_asconf = chunk;
488 /* Add a list of addresses as bind addresses to local endpoint or
491 * Basically run through each address specified in the addrs/addrcnt
492 * array/length pair, determine if it is IPv6 or IPv4 and call
493 * sctp_do_bind() on it.
495 * If any of them fails, then the operation will be reversed and the
496 * ones that were added will be removed.
498 * Only sctp_setsockopt_bindx() is supposed to call this function.
500 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
505 struct sockaddr *sa_addr;
508 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
512 for (cnt = 0; cnt < addrcnt; cnt++) {
513 /* The list may contain either IPv4 or IPv6 address;
514 * determine the address length for walking thru the list.
517 af = sctp_get_af_specific(sa_addr->sa_family);
523 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
526 addr_buf += af->sockaddr_len;
530 /* Failed. Cleanup the ones that have been added */
532 sctp_bindx_rem(sk, addrs, cnt);
540 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
541 * associations that are part of the endpoint indicating that a list of local
542 * addresses are added to the endpoint.
544 * If any of the addresses is already in the bind address list of the
545 * association, we do not send the chunk for that association. But it will not
546 * affect other associations.
548 * Only sctp_setsockopt_bindx() is supposed to call this function.
550 static int sctp_send_asconf_add_ip(struct sock *sk,
551 struct sockaddr *addrs,
554 struct net *net = sock_net(sk);
555 struct sctp_sock *sp;
556 struct sctp_endpoint *ep;
557 struct sctp_association *asoc;
558 struct sctp_bind_addr *bp;
559 struct sctp_chunk *chunk;
560 struct sctp_sockaddr_entry *laddr;
561 union sctp_addr *addr;
562 union sctp_addr saveaddr;
569 if (!net->sctp.addip_enable)
575 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
576 __func__, sk, addrs, addrcnt);
578 list_for_each_entry(asoc, &ep->asocs, asocs) {
579 if (!asoc->peer.asconf_capable)
582 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
585 if (!sctp_state(asoc, ESTABLISHED))
588 /* Check if any address in the packed array of addresses is
589 * in the bind address list of the association. If so,
590 * do not send the asconf chunk to its peer, but continue with
591 * other associations.
594 for (i = 0; i < addrcnt; i++) {
596 af = sctp_get_af_specific(addr->v4.sin_family);
602 if (sctp_assoc_lookup_laddr(asoc, addr))
605 addr_buf += af->sockaddr_len;
610 /* Use the first valid address in bind addr list of
611 * association as Address Parameter of ASCONF CHUNK.
613 bp = &asoc->base.bind_addr;
614 p = bp->address_list.next;
615 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
616 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
617 addrcnt, SCTP_PARAM_ADD_IP);
623 /* Add the new addresses to the bind address list with
624 * use_as_src set to 0.
627 for (i = 0; i < addrcnt; i++) {
629 af = sctp_get_af_specific(addr->v4.sin_family);
630 memcpy(&saveaddr, addr, af->sockaddr_len);
631 retval = sctp_add_bind_addr(bp, &saveaddr,
633 SCTP_ADDR_NEW, GFP_ATOMIC);
634 addr_buf += af->sockaddr_len;
636 if (asoc->src_out_of_asoc_ok) {
637 struct sctp_transport *trans;
639 list_for_each_entry(trans,
640 &asoc->peer.transport_addr_list, transports) {
641 /* Clear the source and route cache */
642 sctp_transport_dst_release(trans);
643 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
644 2*asoc->pathmtu, 4380));
645 trans->ssthresh = asoc->peer.i.a_rwnd;
646 trans->rto = asoc->rto_initial;
647 sctp_max_rto(asoc, trans);
648 trans->rtt = trans->srtt = trans->rttvar = 0;
649 sctp_transport_route(trans, NULL,
650 sctp_sk(asoc->base.sk));
653 retval = sctp_send_asconf(asoc, chunk);
660 /* Remove a list of addresses from bind addresses list. Do not remove the
663 * Basically run through each address specified in the addrs/addrcnt
664 * array/length pair, determine if it is IPv6 or IPv4 and call
665 * sctp_del_bind() on it.
667 * If any of them fails, then the operation will be reversed and the
668 * ones that were removed will be added back.
670 * At least one address has to be left; if only one address is
671 * available, the operation will return -EBUSY.
673 * Only sctp_setsockopt_bindx() is supposed to call this function.
675 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
677 struct sctp_sock *sp = sctp_sk(sk);
678 struct sctp_endpoint *ep = sp->ep;
680 struct sctp_bind_addr *bp = &ep->base.bind_addr;
683 union sctp_addr *sa_addr;
686 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
687 __func__, sk, addrs, addrcnt);
690 for (cnt = 0; cnt < addrcnt; cnt++) {
691 /* If the bind address list is empty or if there is only one
692 * bind address, there is nothing more to be removed (we need
693 * at least one address here).
695 if (list_empty(&bp->address_list) ||
696 (sctp_list_single_entry(&bp->address_list))) {
702 af = sctp_get_af_specific(sa_addr->sa.sa_family);
708 if (!af->addr_valid(sa_addr, sp, NULL)) {
709 retval = -EADDRNOTAVAIL;
713 if (sa_addr->v4.sin_port &&
714 sa_addr->v4.sin_port != htons(bp->port)) {
719 if (!sa_addr->v4.sin_port)
720 sa_addr->v4.sin_port = htons(bp->port);
722 /* FIXME - There is probably a need to check if sk->sk_saddr and
723 * sk->sk_rcv_addr are currently set to one of the addresses to
724 * be removed. This is something which needs to be looked into
725 * when we are fixing the outstanding issues with multi-homing
726 * socket routing and failover schemes. Refer to comments in
727 * sctp_do_bind(). -daisy
729 retval = sctp_del_bind_addr(bp, sa_addr);
731 addr_buf += af->sockaddr_len;
734 /* Failed. Add the ones that has been removed back */
736 sctp_bindx_add(sk, addrs, cnt);
744 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
745 * the associations that are part of the endpoint indicating that a list of
746 * local addresses are removed from the endpoint.
748 * If any of the addresses is already in the bind address list of the
749 * association, we do not send the chunk for that association. But it will not
750 * affect other associations.
752 * Only sctp_setsockopt_bindx() is supposed to call this function.
754 static int sctp_send_asconf_del_ip(struct sock *sk,
755 struct sockaddr *addrs,
758 struct net *net = sock_net(sk);
759 struct sctp_sock *sp;
760 struct sctp_endpoint *ep;
761 struct sctp_association *asoc;
762 struct sctp_transport *transport;
763 struct sctp_bind_addr *bp;
764 struct sctp_chunk *chunk;
765 union sctp_addr *laddr;
768 struct sctp_sockaddr_entry *saddr;
774 if (!net->sctp.addip_enable)
780 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
781 __func__, sk, addrs, addrcnt);
783 list_for_each_entry(asoc, &ep->asocs, asocs) {
785 if (!asoc->peer.asconf_capable)
788 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
791 if (!sctp_state(asoc, ESTABLISHED))
794 /* Check if any address in the packed array of addresses is
795 * not present in the bind address list of the association.
796 * If so, do not send the asconf chunk to its peer, but
797 * continue with other associations.
800 for (i = 0; i < addrcnt; i++) {
802 af = sctp_get_af_specific(laddr->v4.sin_family);
808 if (!sctp_assoc_lookup_laddr(asoc, laddr))
811 addr_buf += af->sockaddr_len;
816 /* Find one address in the association's bind address list
817 * that is not in the packed array of addresses. This is to
818 * make sure that we do not delete all the addresses in the
821 bp = &asoc->base.bind_addr;
822 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
824 if ((laddr == NULL) && (addrcnt == 1)) {
825 if (asoc->asconf_addr_del_pending)
827 asoc->asconf_addr_del_pending =
828 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
829 if (asoc->asconf_addr_del_pending == NULL) {
833 asoc->asconf_addr_del_pending->sa.sa_family =
835 asoc->asconf_addr_del_pending->v4.sin_port =
837 if (addrs->sa_family == AF_INET) {
838 struct sockaddr_in *sin;
840 sin = (struct sockaddr_in *)addrs;
841 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
842 } else if (addrs->sa_family == AF_INET6) {
843 struct sockaddr_in6 *sin6;
845 sin6 = (struct sockaddr_in6 *)addrs;
846 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
849 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
850 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
851 asoc->asconf_addr_del_pending);
853 asoc->src_out_of_asoc_ok = 1;
861 /* We do not need RCU protection throughout this loop
862 * because this is done under a socket lock from the
865 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
873 /* Reset use_as_src flag for the addresses in the bind address
874 * list that are to be deleted.
877 for (i = 0; i < addrcnt; i++) {
879 af = sctp_get_af_specific(laddr->v4.sin_family);
880 list_for_each_entry(saddr, &bp->address_list, list) {
881 if (sctp_cmp_addr_exact(&saddr->a, laddr))
882 saddr->state = SCTP_ADDR_DEL;
884 addr_buf += af->sockaddr_len;
887 /* Update the route and saddr entries for all the transports
888 * as some of the addresses in the bind address list are
889 * about to be deleted and cannot be used as source addresses.
891 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
893 sctp_transport_dst_release(transport);
894 sctp_transport_route(transport, NULL,
895 sctp_sk(asoc->base.sk));
899 /* We don't need to transmit ASCONF */
901 retval = sctp_send_asconf(asoc, chunk);
907 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
908 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
910 struct sock *sk = sctp_opt2sk(sp);
911 union sctp_addr *addr;
914 /* It is safe to write port space in caller. */
916 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
917 af = sctp_get_af_specific(addr->sa.sa_family);
920 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
923 if (addrw->state == SCTP_ADDR_NEW)
924 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
926 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
929 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
932 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
935 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
936 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
939 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
940 * Section 3.1.2 for this usage.
942 * addrs is a pointer to an array of one or more socket addresses. Each
943 * address is contained in its appropriate structure (i.e. struct
944 * sockaddr_in or struct sockaddr_in6) the family of the address type
945 * must be used to distinguish the address length (note that this
946 * representation is termed a "packed array" of addresses). The caller
947 * specifies the number of addresses in the array with addrcnt.
949 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
950 * -1, and sets errno to the appropriate error code.
952 * For SCTP, the port given in each socket address must be the same, or
953 * sctp_bindx() will fail, setting errno to EINVAL.
955 * The flags parameter is formed from the bitwise OR of zero or more of
956 * the following currently defined flags:
958 * SCTP_BINDX_ADD_ADDR
960 * SCTP_BINDX_REM_ADDR
962 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
963 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
964 * addresses from the association. The two flags are mutually exclusive;
965 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
966 * not remove all addresses from an association; sctp_bindx() will
967 * reject such an attempt with EINVAL.
969 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
970 * additional addresses with an endpoint after calling bind(). Or use
971 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
972 * socket is associated with so that no new association accepted will be
973 * associated with those addresses. If the endpoint supports dynamic
974 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
975 * endpoint to send the appropriate message to the peer to change the
976 * peers address lists.
978 * Adding and removing addresses from a connected association is
979 * optional functionality. Implementations that do not support this
980 * functionality should return EOPNOTSUPP.
982 * Basically do nothing but copying the addresses from user to kernel
983 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
984 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
987 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
990 * sk The sk of the socket
991 * addrs The pointer to the addresses in user land
992 * addrssize Size of the addrs buffer
993 * op Operation to perform (add or remove, see the flags of
996 * Returns 0 if ok, <0 errno code on error.
998 static int sctp_setsockopt_bindx(struct sock *sk,
999 struct sockaddr __user *addrs,
1000 int addrs_size, int op)
1002 struct sockaddr *kaddrs;
1006 struct sockaddr *sa_addr;
1010 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1011 __func__, sk, addrs, addrs_size, op);
1013 if (unlikely(addrs_size <= 0))
1016 kaddrs = vmemdup_user(addrs, addrs_size);
1017 if (unlikely(IS_ERR(kaddrs)))
1018 return PTR_ERR(kaddrs);
1020 /* Walk through the addrs buffer and count the number of addresses. */
1022 while (walk_size < addrs_size) {
1023 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1029 af = sctp_get_af_specific(sa_addr->sa_family);
1031 /* If the address family is not supported or if this address
1032 * causes the address buffer to overflow return EINVAL.
1034 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1039 addr_buf += af->sockaddr_len;
1040 walk_size += af->sockaddr_len;
1045 case SCTP_BINDX_ADD_ADDR:
1046 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1049 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1052 case SCTP_BINDX_REM_ADDR:
1053 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1056 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1070 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1072 * Common routine for handling connect() and sctp_connectx().
1073 * Connect will come in with just a single address.
1075 static int __sctp_connect(struct sock *sk,
1076 struct sockaddr *kaddrs,
1078 sctp_assoc_t *assoc_id)
1080 struct net *net = sock_net(sk);
1081 struct sctp_sock *sp;
1082 struct sctp_endpoint *ep;
1083 struct sctp_association *asoc = NULL;
1084 struct sctp_association *asoc2;
1085 struct sctp_transport *transport;
1087 enum sctp_scope scope;
1092 union sctp_addr *sa_addr = NULL;
1094 unsigned short port;
1095 unsigned int f_flags = 0;
1100 /* connect() cannot be done on a socket that is already in ESTABLISHED
1101 * state - UDP-style peeled off socket or a TCP-style socket that
1102 * is already connected.
1103 * It cannot be done even on a TCP-style listening socket.
1105 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1106 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1111 /* Walk through the addrs buffer and count the number of addresses. */
1113 while (walk_size < addrs_size) {
1116 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1122 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1124 /* If the address family is not supported or if this address
1125 * causes the address buffer to overflow return EINVAL.
1127 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1132 port = ntohs(sa_addr->v4.sin_port);
1134 /* Save current address so we can work with it */
1135 memcpy(&to, sa_addr, af->sockaddr_len);
1137 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1141 /* Make sure the destination port is correctly set
1144 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1149 /* Check if there already is a matching association on the
1150 * endpoint (other than the one created here).
1152 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1153 if (asoc2 && asoc2 != asoc) {
1154 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1161 /* If we could not find a matching association on the endpoint,
1162 * make sure that there is no peeled-off association matching
1163 * the peer address even on another socket.
1165 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1166 err = -EADDRNOTAVAIL;
1171 /* If a bind() or sctp_bindx() is not called prior to
1172 * an sctp_connectx() call, the system picks an
1173 * ephemeral port and will choose an address set
1174 * equivalent to binding with a wildcard address.
1176 if (!ep->base.bind_addr.port) {
1177 if (sctp_autobind(sk)) {
1183 * If an unprivileged user inherits a 1-many
1184 * style socket with open associations on a
1185 * privileged port, it MAY be permitted to
1186 * accept new associations, but it SHOULD NOT
1187 * be permitted to open new associations.
1189 if (ep->base.bind_addr.port <
1190 inet_prot_sock(net) &&
1191 !ns_capable(net->user_ns,
1192 CAP_NET_BIND_SERVICE)) {
1198 scope = sctp_scope(&to);
1199 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1205 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1213 /* Prime the peer's transport structures. */
1214 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1222 addr_buf += af->sockaddr_len;
1223 walk_size += af->sockaddr_len;
1226 /* In case the user of sctp_connectx() wants an association
1227 * id back, assign one now.
1230 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1235 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1240 /* Initialize sk's dport and daddr for getpeername() */
1241 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1242 sp->pf->to_sk_daddr(sa_addr, sk);
1245 /* in-kernel sockets don't generally have a file allocated to them
1246 * if all they do is call sock_create_kern().
1248 if (sk->sk_socket->file)
1249 f_flags = sk->sk_socket->file->f_flags;
1251 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1254 *assoc_id = asoc->assoc_id;
1255 err = sctp_wait_for_connect(asoc, &timeo);
1256 /* Note: the asoc may be freed after the return of
1257 * sctp_wait_for_connect.
1260 /* Don't free association on exit. */
1264 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1265 __func__, asoc, kaddrs, err);
1268 /* sctp_primitive_ASSOCIATE may have added this association
1269 * To the hash table, try to unhash it, just in case, its a noop
1270 * if it wasn't hashed so we're safe
1272 sctp_association_free(asoc);
1277 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1280 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1281 * sctp_assoc_t *asoc);
1283 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1284 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1285 * or IPv6 addresses.
1287 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1288 * Section 3.1.2 for this usage.
1290 * addrs is a pointer to an array of one or more socket addresses. Each
1291 * address is contained in its appropriate structure (i.e. struct
1292 * sockaddr_in or struct sockaddr_in6) the family of the address type
1293 * must be used to distengish the address length (note that this
1294 * representation is termed a "packed array" of addresses). The caller
1295 * specifies the number of addresses in the array with addrcnt.
1297 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1298 * the association id of the new association. On failure, sctp_connectx()
1299 * returns -1, and sets errno to the appropriate error code. The assoc_id
1300 * is not touched by the kernel.
1302 * For SCTP, the port given in each socket address must be the same, or
1303 * sctp_connectx() will fail, setting errno to EINVAL.
1305 * An application can use sctp_connectx to initiate an association with
1306 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1307 * allows a caller to specify multiple addresses at which a peer can be
1308 * reached. The way the SCTP stack uses the list of addresses to set up
1309 * the association is implementation dependent. This function only
1310 * specifies that the stack will try to make use of all the addresses in
1311 * the list when needed.
1313 * Note that the list of addresses passed in is only used for setting up
1314 * the association. It does not necessarily equal the set of addresses
1315 * the peer uses for the resulting association. If the caller wants to
1316 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1317 * retrieve them after the association has been set up.
1319 * Basically do nothing but copying the addresses from user to kernel
1320 * land and invoking either sctp_connectx(). This is used for tunneling
1321 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1323 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1326 * sk The sk of the socket
1327 * addrs The pointer to the addresses in user land
1328 * addrssize Size of the addrs buffer
1330 * Returns >=0 if ok, <0 errno code on error.
1332 static int __sctp_setsockopt_connectx(struct sock *sk,
1333 struct sockaddr __user *addrs,
1335 sctp_assoc_t *assoc_id)
1337 struct sockaddr *kaddrs;
1340 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1341 __func__, sk, addrs, addrs_size);
1343 if (unlikely(addrs_size <= 0))
1346 kaddrs = vmemdup_user(addrs, addrs_size);
1347 if (unlikely(IS_ERR(kaddrs)))
1348 return PTR_ERR(kaddrs);
1350 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1357 * This is an older interface. It's kept for backward compatibility
1358 * to the option that doesn't provide association id.
1360 static int sctp_setsockopt_connectx_old(struct sock *sk,
1361 struct sockaddr __user *addrs,
1364 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1368 * New interface for the API. The since the API is done with a socket
1369 * option, to make it simple we feed back the association id is as a return
1370 * indication to the call. Error is always negative and association id is
1373 static int sctp_setsockopt_connectx(struct sock *sk,
1374 struct sockaddr __user *addrs,
1377 sctp_assoc_t assoc_id = 0;
1380 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1389 * New (hopefully final) interface for the API.
1390 * We use the sctp_getaddrs_old structure so that use-space library
1391 * can avoid any unnecessary allocations. The only different part
1392 * is that we store the actual length of the address buffer into the
1393 * addrs_num structure member. That way we can re-use the existing
1396 #ifdef CONFIG_COMPAT
1397 struct compat_sctp_getaddrs_old {
1398 sctp_assoc_t assoc_id;
1400 compat_uptr_t addrs; /* struct sockaddr * */
1404 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1405 char __user *optval,
1408 struct sctp_getaddrs_old param;
1409 sctp_assoc_t assoc_id = 0;
1412 #ifdef CONFIG_COMPAT
1413 if (in_compat_syscall()) {
1414 struct compat_sctp_getaddrs_old param32;
1416 if (len < sizeof(param32))
1418 if (copy_from_user(¶m32, optval, sizeof(param32)))
1421 param.assoc_id = param32.assoc_id;
1422 param.addr_num = param32.addr_num;
1423 param.addrs = compat_ptr(param32.addrs);
1427 if (len < sizeof(param))
1429 if (copy_from_user(¶m, optval, sizeof(param)))
1433 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1434 param.addrs, param.addr_num,
1436 if (err == 0 || err == -EINPROGRESS) {
1437 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1439 if (put_user(sizeof(assoc_id), optlen))
1446 /* API 3.1.4 close() - UDP Style Syntax
1447 * Applications use close() to perform graceful shutdown (as described in
1448 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1449 * by a UDP-style socket.
1453 * ret = close(int sd);
1455 * sd - the socket descriptor of the associations to be closed.
1457 * To gracefully shutdown a specific association represented by the
1458 * UDP-style socket, an application should use the sendmsg() call,
1459 * passing no user data, but including the appropriate flag in the
1460 * ancillary data (see Section xxxx).
1462 * If sd in the close() call is a branched-off socket representing only
1463 * one association, the shutdown is performed on that association only.
1465 * 4.1.6 close() - TCP Style Syntax
1467 * Applications use close() to gracefully close down an association.
1471 * int close(int sd);
1473 * sd - the socket descriptor of the association to be closed.
1475 * After an application calls close() on a socket descriptor, no further
1476 * socket operations will succeed on that descriptor.
1478 * API 7.1.4 SO_LINGER
1480 * An application using the TCP-style socket can use this option to
1481 * perform the SCTP ABORT primitive. The linger option structure is:
1484 * int l_onoff; // option on/off
1485 * int l_linger; // linger time
1488 * To enable the option, set l_onoff to 1. If the l_linger value is set
1489 * to 0, calling close() is the same as the ABORT primitive. If the
1490 * value is set to a negative value, the setsockopt() call will return
1491 * an error. If the value is set to a positive value linger_time, the
1492 * close() can be blocked for at most linger_time ms. If the graceful
1493 * shutdown phase does not finish during this period, close() will
1494 * return but the graceful shutdown phase continues in the system.
1496 static void sctp_close(struct sock *sk, long timeout)
1498 struct net *net = sock_net(sk);
1499 struct sctp_endpoint *ep;
1500 struct sctp_association *asoc;
1501 struct list_head *pos, *temp;
1502 unsigned int data_was_unread;
1504 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1506 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1507 sk->sk_shutdown = SHUTDOWN_MASK;
1508 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1510 ep = sctp_sk(sk)->ep;
1512 /* Clean up any skbs sitting on the receive queue. */
1513 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1514 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1516 /* Walk all associations on an endpoint. */
1517 list_for_each_safe(pos, temp, &ep->asocs) {
1518 asoc = list_entry(pos, struct sctp_association, asocs);
1520 if (sctp_style(sk, TCP)) {
1521 /* A closed association can still be in the list if
1522 * it belongs to a TCP-style listening socket that is
1523 * not yet accepted. If so, free it. If not, send an
1524 * ABORT or SHUTDOWN based on the linger options.
1526 if (sctp_state(asoc, CLOSED)) {
1527 sctp_association_free(asoc);
1532 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1533 !skb_queue_empty(&asoc->ulpq.reasm) ||
1534 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1535 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1536 struct sctp_chunk *chunk;
1538 chunk = sctp_make_abort_user(asoc, NULL, 0);
1539 sctp_primitive_ABORT(net, asoc, chunk);
1541 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1544 /* On a TCP-style socket, block for at most linger_time if set. */
1545 if (sctp_style(sk, TCP) && timeout)
1546 sctp_wait_for_close(sk, timeout);
1548 /* This will run the backlog queue. */
1551 /* Supposedly, no process has access to the socket, but
1552 * the net layers still may.
1553 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1554 * held and that should be grabbed before socket lock.
1556 spin_lock_bh(&net->sctp.addr_wq_lock);
1557 bh_lock_sock_nested(sk);
1559 /* Hold the sock, since sk_common_release() will put sock_put()
1560 * and we have just a little more cleanup.
1563 sk_common_release(sk);
1566 spin_unlock_bh(&net->sctp.addr_wq_lock);
1570 SCTP_DBG_OBJCNT_DEC(sock);
1573 /* Handle EPIPE error. */
1574 static int sctp_error(struct sock *sk, int flags, int err)
1577 err = sock_error(sk) ? : -EPIPE;
1578 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1579 send_sig(SIGPIPE, current, 0);
1583 /* API 3.1.3 sendmsg() - UDP Style Syntax
1585 * An application uses sendmsg() and recvmsg() calls to transmit data to
1586 * and receive data from its peer.
1588 * ssize_t sendmsg(int socket, const struct msghdr *message,
1591 * socket - the socket descriptor of the endpoint.
1592 * message - pointer to the msghdr structure which contains a single
1593 * user message and possibly some ancillary data.
1595 * See Section 5 for complete description of the data
1598 * flags - flags sent or received with the user message, see Section
1599 * 5 for complete description of the flags.
1601 * Note: This function could use a rewrite especially when explicit
1602 * connect support comes in.
1604 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1606 static int sctp_msghdr_parse(const struct msghdr *msg,
1607 struct sctp_cmsgs *cmsgs);
1609 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1610 struct sctp_sndrcvinfo *srinfo,
1611 const struct msghdr *msg, size_t msg_len)
1616 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1619 if (msg_len > sk->sk_sndbuf)
1622 memset(cmsgs, 0, sizeof(*cmsgs));
1623 err = sctp_msghdr_parse(msg, cmsgs);
1625 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1629 memset(srinfo, 0, sizeof(*srinfo));
1630 if (cmsgs->srinfo) {
1631 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1632 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1633 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1634 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1635 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1636 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1640 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1641 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1642 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1643 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1644 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1647 sflags = srinfo->sinfo_flags;
1648 if (!sflags && msg_len)
1651 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1654 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1655 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1658 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1664 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1665 struct sctp_cmsgs *cmsgs,
1666 union sctp_addr *daddr,
1667 struct sctp_transport **tp)
1669 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1670 struct net *net = sock_net(sk);
1671 struct sctp_association *asoc;
1672 enum sctp_scope scope;
1677 if (sflags & (SCTP_EOF | SCTP_ABORT))
1680 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1681 sctp_sstate(sk, CLOSING)))
1682 return -EADDRNOTAVAIL;
1684 if (sctp_endpoint_is_peeled_off(ep, daddr))
1685 return -EADDRNOTAVAIL;
1687 if (!ep->base.bind_addr.port) {
1688 if (sctp_autobind(sk))
1691 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1692 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1696 scope = sctp_scope(daddr);
1698 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1702 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1708 struct sctp_initmsg *init = cmsgs->init;
1710 if (init->sinit_num_ostreams) {
1711 __u16 outcnt = init->sinit_num_ostreams;
1713 asoc->c.sinit_num_ostreams = outcnt;
1714 /* outcnt has been changed, need to re-init stream */
1715 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1721 if (init->sinit_max_instreams)
1722 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1724 if (init->sinit_max_attempts)
1725 asoc->max_init_attempts = init->sinit_max_attempts;
1727 if (init->sinit_max_init_timeo)
1728 asoc->max_init_timeo =
1729 msecs_to_jiffies(init->sinit_max_init_timeo);
1732 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1741 sctp_association_free(asoc);
1745 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1746 __u16 sflags, struct msghdr *msg,
1749 struct sock *sk = asoc->base.sk;
1750 struct net *net = sock_net(sk);
1752 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1755 if (sflags & SCTP_EOF) {
1756 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1757 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1762 if (sflags & SCTP_ABORT) {
1763 struct sctp_chunk *chunk;
1765 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1769 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1770 sctp_primitive_ABORT(net, asoc, chunk);
1778 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1779 struct msghdr *msg, size_t msg_len,
1780 struct sctp_transport *transport,
1781 struct sctp_sndrcvinfo *sinfo)
1783 struct sock *sk = asoc->base.sk;
1784 struct net *net = sock_net(sk);
1785 struct sctp_datamsg *datamsg;
1786 bool wait_connect = false;
1787 struct sctp_chunk *chunk;
1791 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1796 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1797 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1802 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1807 if (sctp_state(asoc, CLOSED)) {
1808 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1812 if (sctp_sk(sk)->strm_interleave) {
1813 timeo = sock_sndtimeo(sk, 0);
1814 err = sctp_wait_for_connect(asoc, &timeo);
1818 wait_connect = true;
1821 pr_debug("%s: we associated primitively\n", __func__);
1824 if (asoc->pmtu_pending)
1825 sctp_assoc_pending_pmtu(asoc);
1827 if (sctp_wspace(asoc) < msg_len)
1828 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1830 if (!sctp_wspace(asoc)) {
1831 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1832 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1837 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1838 if (IS_ERR(datamsg)) {
1839 err = PTR_ERR(datamsg);
1843 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1845 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1846 sctp_chunk_hold(chunk);
1847 sctp_set_owner_w(chunk);
1848 chunk->transport = transport;
1851 err = sctp_primitive_SEND(net, asoc, datamsg);
1853 sctp_datamsg_free(datamsg);
1857 pr_debug("%s: we sent primitively\n", __func__);
1859 sctp_datamsg_put(datamsg);
1861 if (unlikely(wait_connect)) {
1862 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1863 sctp_wait_for_connect(asoc, &timeo);
1872 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1873 const struct msghdr *msg,
1874 struct sctp_cmsgs *cmsgs)
1876 union sctp_addr *daddr = NULL;
1879 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1880 int len = msg->msg_namelen;
1882 if (len > sizeof(*daddr))
1883 len = sizeof(*daddr);
1885 daddr = (union sctp_addr *)msg->msg_name;
1887 err = sctp_verify_addr(sk, daddr, len);
1889 return ERR_PTR(err);
1895 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1896 struct sctp_sndrcvinfo *sinfo,
1897 struct sctp_cmsgs *cmsgs)
1899 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1900 sinfo->sinfo_stream = asoc->default_stream;
1901 sinfo->sinfo_ppid = asoc->default_ppid;
1902 sinfo->sinfo_context = asoc->default_context;
1903 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1907 sinfo->sinfo_timetolive = asoc->default_timetolive;
1910 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1912 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1913 struct sctp_association *new_asoc = NULL, *asoc = NULL;
1914 struct sctp_transport *transport, *chunk_tp;
1915 struct sctp_sndrcvinfo _sinfo, *sinfo;
1916 sctp_assoc_t associd = 0;
1917 struct sctp_cmsgs cmsgs = { NULL };
1918 __u16 sinfo_flags = 0;
1919 union sctp_addr *daddr;
1922 /* Parse and get snd_info */
1923 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1928 sinfo_flags = sinfo->sinfo_flags;
1930 /* Get daddr from msg */
1931 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1932 if (IS_ERR(daddr)) {
1933 err = PTR_ERR(daddr);
1939 /* If a msg_name has been specified, assume this is to be used. */
1941 /* Look for a matching association on the endpoint. */
1942 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1944 asoc = sctp_id2assoc(sk, associd);
1952 err = sctp_sendmsg_check_sflags(asoc, sinfo_flags, msg,
1958 /* Do we need to create the association? */
1960 err = sctp_sendmsg_new_asoc(sk, sinfo_flags, &cmsgs, daddr,
1965 asoc = transport->asoc;
1969 /* Update snd_info with the asoc */
1970 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1972 /* If an address is passed with the sendto/sendmsg call, it is used
1973 * to override the primary destination address in the TCP model, or
1974 * when SCTP_ADDR_OVER flag is set in the UDP model.
1976 if ((sctp_style(sk, TCP) && daddr) ||
1977 (sinfo_flags & SCTP_ADDR_OVER)) {
1978 chunk_tp = sctp_assoc_lookup_paddr(asoc, daddr);
1986 /* Send msg to the asoc */
1987 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, chunk_tp, sinfo);
1990 if (err < 0 && err != -ESRCH && new_asoc)
1991 sctp_association_free(asoc);
1995 return sctp_error(sk, msg->msg_flags, err);
1998 /* This is an extended version of skb_pull() that removes the data from the
1999 * start of a skb even when data is spread across the list of skb's in the
2000 * frag_list. len specifies the total amount of data that needs to be removed.
2001 * when 'len' bytes could be removed from the skb, it returns 0.
2002 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2003 * could not be removed.
2005 static int sctp_skb_pull(struct sk_buff *skb, int len)
2007 struct sk_buff *list;
2008 int skb_len = skb_headlen(skb);
2011 if (len <= skb_len) {
2012 __skb_pull(skb, len);
2016 __skb_pull(skb, skb_len);
2018 skb_walk_frags(skb, list) {
2019 rlen = sctp_skb_pull(list, len);
2020 skb->len -= (len-rlen);
2021 skb->data_len -= (len-rlen);
2032 /* API 3.1.3 recvmsg() - UDP Style Syntax
2034 * ssize_t recvmsg(int socket, struct msghdr *message,
2037 * socket - the socket descriptor of the endpoint.
2038 * message - pointer to the msghdr structure which contains a single
2039 * user message and possibly some ancillary data.
2041 * See Section 5 for complete description of the data
2044 * flags - flags sent or received with the user message, see Section
2045 * 5 for complete description of the flags.
2047 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2048 int noblock, int flags, int *addr_len)
2050 struct sctp_ulpevent *event = NULL;
2051 struct sctp_sock *sp = sctp_sk(sk);
2052 struct sk_buff *skb, *head_skb;
2057 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2058 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2063 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2064 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2069 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2073 /* Get the total length of the skb including any skb's in the
2082 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2084 event = sctp_skb2event(skb);
2089 if (event->chunk && event->chunk->head_skb)
2090 head_skb = event->chunk->head_skb;
2093 sock_recv_ts_and_drops(msg, sk, head_skb);
2094 if (sctp_ulpevent_is_notification(event)) {
2095 msg->msg_flags |= MSG_NOTIFICATION;
2096 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2098 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2101 /* Check if we allow SCTP_NXTINFO. */
2102 if (sp->recvnxtinfo)
2103 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2104 /* Check if we allow SCTP_RCVINFO. */
2105 if (sp->recvrcvinfo)
2106 sctp_ulpevent_read_rcvinfo(event, msg);
2107 /* Check if we allow SCTP_SNDRCVINFO. */
2108 if (sp->subscribe.sctp_data_io_event)
2109 sctp_ulpevent_read_sndrcvinfo(event, msg);
2113 /* If skb's length exceeds the user's buffer, update the skb and
2114 * push it back to the receive_queue so that the next call to
2115 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2117 if (skb_len > copied) {
2118 msg->msg_flags &= ~MSG_EOR;
2119 if (flags & MSG_PEEK)
2121 sctp_skb_pull(skb, copied);
2122 skb_queue_head(&sk->sk_receive_queue, skb);
2124 /* When only partial message is copied to the user, increase
2125 * rwnd by that amount. If all the data in the skb is read,
2126 * rwnd is updated when the event is freed.
2128 if (!sctp_ulpevent_is_notification(event))
2129 sctp_assoc_rwnd_increase(event->asoc, copied);
2131 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2132 (event->msg_flags & MSG_EOR))
2133 msg->msg_flags |= MSG_EOR;
2135 msg->msg_flags &= ~MSG_EOR;
2138 if (flags & MSG_PEEK) {
2139 /* Release the skb reference acquired after peeking the skb in
2140 * sctp_skb_recv_datagram().
2144 /* Free the event which includes releasing the reference to
2145 * the owner of the skb, freeing the skb and updating the
2148 sctp_ulpevent_free(event);
2155 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2157 * This option is a on/off flag. If enabled no SCTP message
2158 * fragmentation will be performed. Instead if a message being sent
2159 * exceeds the current PMTU size, the message will NOT be sent and
2160 * instead a error will be indicated to the user.
2162 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2163 char __user *optval,
2164 unsigned int optlen)
2168 if (optlen < sizeof(int))
2171 if (get_user(val, (int __user *)optval))
2174 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2179 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2180 unsigned int optlen)
2182 struct sctp_association *asoc;
2183 struct sctp_ulpevent *event;
2185 if (optlen > sizeof(struct sctp_event_subscribe))
2187 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2190 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2191 * if there is no data to be sent or retransmit, the stack will
2192 * immediately send up this notification.
2194 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2195 &sctp_sk(sk)->subscribe)) {
2196 asoc = sctp_id2assoc(sk, 0);
2198 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2199 event = sctp_ulpevent_make_sender_dry_event(asoc,
2200 GFP_USER | __GFP_NOWARN);
2204 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2211 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2213 * This socket option is applicable to the UDP-style socket only. When
2214 * set it will cause associations that are idle for more than the
2215 * specified number of seconds to automatically close. An association
2216 * being idle is defined an association that has NOT sent or received
2217 * user data. The special value of '0' indicates that no automatic
2218 * close of any associations should be performed. The option expects an
2219 * integer defining the number of seconds of idle time before an
2220 * association is closed.
2222 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2223 unsigned int optlen)
2225 struct sctp_sock *sp = sctp_sk(sk);
2226 struct net *net = sock_net(sk);
2228 /* Applicable to UDP-style socket only */
2229 if (sctp_style(sk, TCP))
2231 if (optlen != sizeof(int))
2233 if (copy_from_user(&sp->autoclose, optval, optlen))
2236 if (sp->autoclose > net->sctp.max_autoclose)
2237 sp->autoclose = net->sctp.max_autoclose;
2242 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2244 * Applications can enable or disable heartbeats for any peer address of
2245 * an association, modify an address's heartbeat interval, force a
2246 * heartbeat to be sent immediately, and adjust the address's maximum
2247 * number of retransmissions sent before an address is considered
2248 * unreachable. The following structure is used to access and modify an
2249 * address's parameters:
2251 * struct sctp_paddrparams {
2252 * sctp_assoc_t spp_assoc_id;
2253 * struct sockaddr_storage spp_address;
2254 * uint32_t spp_hbinterval;
2255 * uint16_t spp_pathmaxrxt;
2256 * uint32_t spp_pathmtu;
2257 * uint32_t spp_sackdelay;
2258 * uint32_t spp_flags;
2261 * spp_assoc_id - (one-to-many style socket) This is filled in the
2262 * application, and identifies the association for
2264 * spp_address - This specifies which address is of interest.
2265 * spp_hbinterval - This contains the value of the heartbeat interval,
2266 * in milliseconds. If a value of zero
2267 * is present in this field then no changes are to
2268 * be made to this parameter.
2269 * spp_pathmaxrxt - This contains the maximum number of
2270 * retransmissions before this address shall be
2271 * considered unreachable. If a value of zero
2272 * is present in this field then no changes are to
2273 * be made to this parameter.
2274 * spp_pathmtu - When Path MTU discovery is disabled the value
2275 * specified here will be the "fixed" path mtu.
2276 * Note that if the spp_address field is empty
2277 * then all associations on this address will
2278 * have this fixed path mtu set upon them.
2280 * spp_sackdelay - When delayed sack is enabled, this value specifies
2281 * the number of milliseconds that sacks will be delayed
2282 * for. This value will apply to all addresses of an
2283 * association if the spp_address field is empty. Note
2284 * also, that if delayed sack is enabled and this
2285 * value is set to 0, no change is made to the last
2286 * recorded delayed sack timer value.
2288 * spp_flags - These flags are used to control various features
2289 * on an association. The flag field may contain
2290 * zero or more of the following options.
2292 * SPP_HB_ENABLE - Enable heartbeats on the
2293 * specified address. Note that if the address
2294 * field is empty all addresses for the association
2295 * have heartbeats enabled upon them.
2297 * SPP_HB_DISABLE - Disable heartbeats on the
2298 * speicifed address. Note that if the address
2299 * field is empty all addresses for the association
2300 * will have their heartbeats disabled. Note also
2301 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2302 * mutually exclusive, only one of these two should
2303 * be specified. Enabling both fields will have
2304 * undetermined results.
2306 * SPP_HB_DEMAND - Request a user initiated heartbeat
2307 * to be made immediately.
2309 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2310 * heartbeat delayis to be set to the value of 0
2313 * SPP_PMTUD_ENABLE - This field will enable PMTU
2314 * discovery upon the specified address. Note that
2315 * if the address feild is empty then all addresses
2316 * on the association are effected.
2318 * SPP_PMTUD_DISABLE - This field will disable PMTU
2319 * discovery upon the specified address. Note that
2320 * if the address feild is empty then all addresses
2321 * on the association are effected. Not also that
2322 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2323 * exclusive. Enabling both will have undetermined
2326 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2327 * on delayed sack. The time specified in spp_sackdelay
2328 * is used to specify the sack delay for this address. Note
2329 * that if spp_address is empty then all addresses will
2330 * enable delayed sack and take on the sack delay
2331 * value specified in spp_sackdelay.
2332 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2333 * off delayed sack. If the spp_address field is blank then
2334 * delayed sack is disabled for the entire association. Note
2335 * also that this field is mutually exclusive to
2336 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2339 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2340 struct sctp_transport *trans,
2341 struct sctp_association *asoc,
2342 struct sctp_sock *sp,
2345 int sackdelay_change)
2349 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2350 struct net *net = sock_net(trans->asoc->base.sk);
2352 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2357 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2358 * this field is ignored. Note also that a value of zero indicates
2359 * the current setting should be left unchanged.
2361 if (params->spp_flags & SPP_HB_ENABLE) {
2363 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2364 * set. This lets us use 0 value when this flag
2367 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2368 params->spp_hbinterval = 0;
2370 if (params->spp_hbinterval ||
2371 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2374 msecs_to_jiffies(params->spp_hbinterval);
2377 msecs_to_jiffies(params->spp_hbinterval);
2379 sp->hbinterval = params->spp_hbinterval;
2386 trans->param_flags =
2387 (trans->param_flags & ~SPP_HB) | hb_change;
2390 (asoc->param_flags & ~SPP_HB) | hb_change;
2393 (sp->param_flags & ~SPP_HB) | hb_change;
2397 /* When Path MTU discovery is disabled the value specified here will
2398 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2399 * include the flag SPP_PMTUD_DISABLE for this field to have any
2402 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2404 trans->pathmtu = params->spp_pathmtu;
2405 sctp_assoc_sync_pmtu(asoc);
2407 asoc->pathmtu = params->spp_pathmtu;
2409 sp->pathmtu = params->spp_pathmtu;
2415 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2416 (params->spp_flags & SPP_PMTUD_ENABLE);
2417 trans->param_flags =
2418 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2420 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2421 sctp_assoc_sync_pmtu(asoc);
2425 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2428 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2432 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2433 * value of this field is ignored. Note also that a value of zero
2434 * indicates the current setting should be left unchanged.
2436 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2439 msecs_to_jiffies(params->spp_sackdelay);
2442 msecs_to_jiffies(params->spp_sackdelay);
2444 sp->sackdelay = params->spp_sackdelay;
2448 if (sackdelay_change) {
2450 trans->param_flags =
2451 (trans->param_flags & ~SPP_SACKDELAY) |
2455 (asoc->param_flags & ~SPP_SACKDELAY) |
2459 (sp->param_flags & ~SPP_SACKDELAY) |
2464 /* Note that a value of zero indicates the current setting should be
2467 if (params->spp_pathmaxrxt) {
2469 trans->pathmaxrxt = params->spp_pathmaxrxt;
2471 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2473 sp->pathmaxrxt = params->spp_pathmaxrxt;
2480 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2481 char __user *optval,
2482 unsigned int optlen)
2484 struct sctp_paddrparams params;
2485 struct sctp_transport *trans = NULL;
2486 struct sctp_association *asoc = NULL;
2487 struct sctp_sock *sp = sctp_sk(sk);
2489 int hb_change, pmtud_change, sackdelay_change;
2491 if (optlen != sizeof(struct sctp_paddrparams))
2494 if (copy_from_user(¶ms, optval, optlen))
2497 /* Validate flags and value parameters. */
2498 hb_change = params.spp_flags & SPP_HB;
2499 pmtud_change = params.spp_flags & SPP_PMTUD;
2500 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2502 if (hb_change == SPP_HB ||
2503 pmtud_change == SPP_PMTUD ||
2504 sackdelay_change == SPP_SACKDELAY ||
2505 params.spp_sackdelay > 500 ||
2506 (params.spp_pathmtu &&
2507 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2510 /* If an address other than INADDR_ANY is specified, and
2511 * no transport is found, then the request is invalid.
2513 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2514 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2515 params.spp_assoc_id);
2520 /* Get association, if assoc_id != 0 and the socket is a one
2521 * to many style socket, and an association was not found, then
2522 * the id was invalid.
2524 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2525 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2528 /* Heartbeat demand can only be sent on a transport or
2529 * association, but not a socket.
2531 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2534 /* Process parameters. */
2535 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2536 hb_change, pmtud_change,
2542 /* If changes are for association, also apply parameters to each
2545 if (!trans && asoc) {
2546 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2548 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2549 hb_change, pmtud_change,
2557 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2559 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2562 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2564 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2568 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2570 * This option will effect the way delayed acks are performed. This
2571 * option allows you to get or set the delayed ack time, in
2572 * milliseconds. It also allows changing the delayed ack frequency.
2573 * Changing the frequency to 1 disables the delayed sack algorithm. If
2574 * the assoc_id is 0, then this sets or gets the endpoints default
2575 * values. If the assoc_id field is non-zero, then the set or get
2576 * effects the specified association for the one to many model (the
2577 * assoc_id field is ignored by the one to one model). Note that if
2578 * sack_delay or sack_freq are 0 when setting this option, then the
2579 * current values will remain unchanged.
2581 * struct sctp_sack_info {
2582 * sctp_assoc_t sack_assoc_id;
2583 * uint32_t sack_delay;
2584 * uint32_t sack_freq;
2587 * sack_assoc_id - This parameter, indicates which association the user
2588 * is performing an action upon. Note that if this field's value is
2589 * zero then the endpoints default value is changed (effecting future
2590 * associations only).
2592 * sack_delay - This parameter contains the number of milliseconds that
2593 * the user is requesting the delayed ACK timer be set to. Note that
2594 * this value is defined in the standard to be between 200 and 500
2597 * sack_freq - This parameter contains the number of packets that must
2598 * be received before a sack is sent without waiting for the delay
2599 * timer to expire. The default value for this is 2, setting this
2600 * value to 1 will disable the delayed sack algorithm.
2603 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2604 char __user *optval, unsigned int optlen)
2606 struct sctp_sack_info params;
2607 struct sctp_transport *trans = NULL;
2608 struct sctp_association *asoc = NULL;
2609 struct sctp_sock *sp = sctp_sk(sk);
2611 if (optlen == sizeof(struct sctp_sack_info)) {
2612 if (copy_from_user(¶ms, optval, optlen))
2615 if (params.sack_delay == 0 && params.sack_freq == 0)
2617 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2618 pr_warn_ratelimited(DEPRECATED
2620 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2621 "Use struct sctp_sack_info instead\n",
2622 current->comm, task_pid_nr(current));
2623 if (copy_from_user(¶ms, optval, optlen))
2626 if (params.sack_delay == 0)
2627 params.sack_freq = 1;
2629 params.sack_freq = 0;
2633 /* Validate value parameter. */
2634 if (params.sack_delay > 500)
2637 /* Get association, if sack_assoc_id != 0 and the socket is a one
2638 * to many style socket, and an association was not found, then
2639 * the id was invalid.
2641 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2642 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2645 if (params.sack_delay) {
2648 msecs_to_jiffies(params.sack_delay);
2650 sctp_spp_sackdelay_enable(asoc->param_flags);
2652 sp->sackdelay = params.sack_delay;
2654 sctp_spp_sackdelay_enable(sp->param_flags);
2658 if (params.sack_freq == 1) {
2661 sctp_spp_sackdelay_disable(asoc->param_flags);
2664 sctp_spp_sackdelay_disable(sp->param_flags);
2666 } else if (params.sack_freq > 1) {
2668 asoc->sackfreq = params.sack_freq;
2670 sctp_spp_sackdelay_enable(asoc->param_flags);
2672 sp->sackfreq = params.sack_freq;
2674 sctp_spp_sackdelay_enable(sp->param_flags);
2678 /* If change is for association, also apply to each transport. */
2680 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2682 if (params.sack_delay) {
2684 msecs_to_jiffies(params.sack_delay);
2685 trans->param_flags =
2686 sctp_spp_sackdelay_enable(trans->param_flags);
2688 if (params.sack_freq == 1) {
2689 trans->param_flags =
2690 sctp_spp_sackdelay_disable(trans->param_flags);
2691 } else if (params.sack_freq > 1) {
2692 trans->sackfreq = params.sack_freq;
2693 trans->param_flags =
2694 sctp_spp_sackdelay_enable(trans->param_flags);
2702 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2704 * Applications can specify protocol parameters for the default association
2705 * initialization. The option name argument to setsockopt() and getsockopt()
2708 * Setting initialization parameters is effective only on an unconnected
2709 * socket (for UDP-style sockets only future associations are effected
2710 * by the change). With TCP-style sockets, this option is inherited by
2711 * sockets derived from a listener socket.
2713 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2715 struct sctp_initmsg sinit;
2716 struct sctp_sock *sp = sctp_sk(sk);
2718 if (optlen != sizeof(struct sctp_initmsg))
2720 if (copy_from_user(&sinit, optval, optlen))
2723 if (sinit.sinit_num_ostreams)
2724 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2725 if (sinit.sinit_max_instreams)
2726 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2727 if (sinit.sinit_max_attempts)
2728 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2729 if (sinit.sinit_max_init_timeo)
2730 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2736 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2738 * Applications that wish to use the sendto() system call may wish to
2739 * specify a default set of parameters that would normally be supplied
2740 * through the inclusion of ancillary data. This socket option allows
2741 * such an application to set the default sctp_sndrcvinfo structure.
2742 * The application that wishes to use this socket option simply passes
2743 * in to this call the sctp_sndrcvinfo structure defined in Section
2744 * 5.2.2) The input parameters accepted by this call include
2745 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2746 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2747 * to this call if the caller is using the UDP model.
2749 static int sctp_setsockopt_default_send_param(struct sock *sk,
2750 char __user *optval,
2751 unsigned int optlen)
2753 struct sctp_sock *sp = sctp_sk(sk);
2754 struct sctp_association *asoc;
2755 struct sctp_sndrcvinfo info;
2757 if (optlen != sizeof(info))
2759 if (copy_from_user(&info, optval, optlen))
2761 if (info.sinfo_flags &
2762 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2763 SCTP_ABORT | SCTP_EOF))
2766 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2767 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2770 asoc->default_stream = info.sinfo_stream;
2771 asoc->default_flags = info.sinfo_flags;
2772 asoc->default_ppid = info.sinfo_ppid;
2773 asoc->default_context = info.sinfo_context;
2774 asoc->default_timetolive = info.sinfo_timetolive;
2776 sp->default_stream = info.sinfo_stream;
2777 sp->default_flags = info.sinfo_flags;
2778 sp->default_ppid = info.sinfo_ppid;
2779 sp->default_context = info.sinfo_context;
2780 sp->default_timetolive = info.sinfo_timetolive;
2786 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2787 * (SCTP_DEFAULT_SNDINFO)
2789 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2790 char __user *optval,
2791 unsigned int optlen)
2793 struct sctp_sock *sp = sctp_sk(sk);
2794 struct sctp_association *asoc;
2795 struct sctp_sndinfo info;
2797 if (optlen != sizeof(info))
2799 if (copy_from_user(&info, optval, optlen))
2801 if (info.snd_flags &
2802 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2803 SCTP_ABORT | SCTP_EOF))
2806 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2807 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2810 asoc->default_stream = info.snd_sid;
2811 asoc->default_flags = info.snd_flags;
2812 asoc->default_ppid = info.snd_ppid;
2813 asoc->default_context = info.snd_context;
2815 sp->default_stream = info.snd_sid;
2816 sp->default_flags = info.snd_flags;
2817 sp->default_ppid = info.snd_ppid;
2818 sp->default_context = info.snd_context;
2824 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2826 * Requests that the local SCTP stack use the enclosed peer address as
2827 * the association primary. The enclosed address must be one of the
2828 * association peer's addresses.
2830 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2831 unsigned int optlen)
2833 struct sctp_prim prim;
2834 struct sctp_transport *trans;
2836 if (optlen != sizeof(struct sctp_prim))
2839 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2842 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2846 sctp_assoc_set_primary(trans->asoc, trans);
2852 * 7.1.5 SCTP_NODELAY
2854 * Turn on/off any Nagle-like algorithm. This means that packets are
2855 * generally sent as soon as possible and no unnecessary delays are
2856 * introduced, at the cost of more packets in the network. Expects an
2857 * integer boolean flag.
2859 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2860 unsigned int optlen)
2864 if (optlen < sizeof(int))
2866 if (get_user(val, (int __user *)optval))
2869 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2875 * 7.1.1 SCTP_RTOINFO
2877 * The protocol parameters used to initialize and bound retransmission
2878 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2879 * and modify these parameters.
2880 * All parameters are time values, in milliseconds. A value of 0, when
2881 * modifying the parameters, indicates that the current value should not
2885 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2887 struct sctp_rtoinfo rtoinfo;
2888 struct sctp_association *asoc;
2889 unsigned long rto_min, rto_max;
2890 struct sctp_sock *sp = sctp_sk(sk);
2892 if (optlen != sizeof (struct sctp_rtoinfo))
2895 if (copy_from_user(&rtoinfo, optval, optlen))
2898 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2900 /* Set the values to the specific association */
2901 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2904 rto_max = rtoinfo.srto_max;
2905 rto_min = rtoinfo.srto_min;
2908 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2910 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2913 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2915 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2917 if (rto_min > rto_max)
2921 if (rtoinfo.srto_initial != 0)
2923 msecs_to_jiffies(rtoinfo.srto_initial);
2924 asoc->rto_max = rto_max;
2925 asoc->rto_min = rto_min;
2927 /* If there is no association or the association-id = 0
2928 * set the values to the endpoint.
2930 if (rtoinfo.srto_initial != 0)
2931 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2932 sp->rtoinfo.srto_max = rto_max;
2933 sp->rtoinfo.srto_min = rto_min;
2941 * 7.1.2 SCTP_ASSOCINFO
2943 * This option is used to tune the maximum retransmission attempts
2944 * of the association.
2945 * Returns an error if the new association retransmission value is
2946 * greater than the sum of the retransmission value of the peer.
2947 * See [SCTP] for more information.
2950 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
2953 struct sctp_assocparams assocparams;
2954 struct sctp_association *asoc;
2956 if (optlen != sizeof(struct sctp_assocparams))
2958 if (copy_from_user(&assocparams, optval, optlen))
2961 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2963 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2966 /* Set the values to the specific association */
2968 if (assocparams.sasoc_asocmaxrxt != 0) {
2971 struct sctp_transport *peer_addr;
2973 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2975 path_sum += peer_addr->pathmaxrxt;
2979 /* Only validate asocmaxrxt if we have more than
2980 * one path/transport. We do this because path
2981 * retransmissions are only counted when we have more
2985 assocparams.sasoc_asocmaxrxt > path_sum)
2988 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2991 if (assocparams.sasoc_cookie_life != 0)
2992 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
2994 /* Set the values to the endpoint */
2995 struct sctp_sock *sp = sctp_sk(sk);
2997 if (assocparams.sasoc_asocmaxrxt != 0)
2998 sp->assocparams.sasoc_asocmaxrxt =
2999 assocparams.sasoc_asocmaxrxt;
3000 if (assocparams.sasoc_cookie_life != 0)
3001 sp->assocparams.sasoc_cookie_life =
3002 assocparams.sasoc_cookie_life;
3008 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3010 * This socket option is a boolean flag which turns on or off mapped V4
3011 * addresses. If this option is turned on and the socket is type
3012 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3013 * If this option is turned off, then no mapping will be done of V4
3014 * addresses and a user will receive both PF_INET6 and PF_INET type
3015 * addresses on the socket.
3017 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3020 struct sctp_sock *sp = sctp_sk(sk);
3022 if (optlen < sizeof(int))
3024 if (get_user(val, (int __user *)optval))
3035 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3036 * This option will get or set the maximum size to put in any outgoing
3037 * SCTP DATA chunk. If a message is larger than this size it will be
3038 * fragmented by SCTP into the specified size. Note that the underlying
3039 * SCTP implementation may fragment into smaller sized chunks when the
3040 * PMTU of the underlying association is smaller than the value set by
3041 * the user. The default value for this option is '0' which indicates
3042 * the user is NOT limiting fragmentation and only the PMTU will effect
3043 * SCTP's choice of DATA chunk size. Note also that values set larger
3044 * than the maximum size of an IP datagram will effectively let SCTP
3045 * control fragmentation (i.e. the same as setting this option to 0).
3047 * The following structure is used to access and modify this parameter:
3049 * struct sctp_assoc_value {
3050 * sctp_assoc_t assoc_id;
3051 * uint32_t assoc_value;
3054 * assoc_id: This parameter is ignored for one-to-one style sockets.
3055 * For one-to-many style sockets this parameter indicates which
3056 * association the user is performing an action upon. Note that if
3057 * this field's value is zero then the endpoints default value is
3058 * changed (effecting future associations only).
3059 * assoc_value: This parameter specifies the maximum size in bytes.
3061 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3063 struct sctp_sock *sp = sctp_sk(sk);
3064 struct sctp_assoc_value params;
3065 struct sctp_association *asoc;
3068 if (optlen == sizeof(int)) {
3069 pr_warn_ratelimited(DEPRECATED
3071 "Use of int in maxseg socket option.\n"
3072 "Use struct sctp_assoc_value instead\n",
3073 current->comm, task_pid_nr(current));
3074 if (copy_from_user(&val, optval, optlen))
3076 params.assoc_id = 0;
3077 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3078 if (copy_from_user(¶ms, optval, optlen))
3080 val = params.assoc_value;
3086 int min_len, max_len;
3088 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3089 min_len -= sizeof(struct sctphdr) +
3090 sizeof(struct sctp_data_chunk);
3092 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3094 if (val < min_len || val > max_len)
3098 asoc = sctp_id2assoc(sk, params.assoc_id);
3101 val = asoc->pathmtu - sp->pf->af->net_header_len;
3102 val -= sizeof(struct sctphdr) +
3103 sctp_datachk_len(&asoc->stream);
3105 asoc->user_frag = val;
3106 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
3108 if (params.assoc_id && sctp_style(sk, UDP))
3110 sp->user_frag = val;
3118 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3120 * Requests that the peer mark the enclosed address as the association
3121 * primary. The enclosed address must be one of the association's
3122 * locally bound addresses. The following structure is used to make a
3123 * set primary request:
3125 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3126 unsigned int optlen)
3128 struct net *net = sock_net(sk);
3129 struct sctp_sock *sp;
3130 struct sctp_association *asoc = NULL;
3131 struct sctp_setpeerprim prim;
3132 struct sctp_chunk *chunk;
3138 if (!net->sctp.addip_enable)
3141 if (optlen != sizeof(struct sctp_setpeerprim))
3144 if (copy_from_user(&prim, optval, optlen))
3147 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3151 if (!asoc->peer.asconf_capable)
3154 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3157 if (!sctp_state(asoc, ESTABLISHED))
3160 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3164 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3165 return -EADDRNOTAVAIL;
3167 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3168 return -EADDRNOTAVAIL;
3170 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3171 chunk = sctp_make_asconf_set_prim(asoc,
3172 (union sctp_addr *)&prim.sspp_addr);
3176 err = sctp_send_asconf(asoc, chunk);
3178 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3183 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3184 unsigned int optlen)
3186 struct sctp_setadaptation adaptation;
3188 if (optlen != sizeof(struct sctp_setadaptation))
3190 if (copy_from_user(&adaptation, optval, optlen))
3193 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3199 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3201 * The context field in the sctp_sndrcvinfo structure is normally only
3202 * used when a failed message is retrieved holding the value that was
3203 * sent down on the actual send call. This option allows the setting of
3204 * a default context on an association basis that will be received on
3205 * reading messages from the peer. This is especially helpful in the
3206 * one-2-many model for an application to keep some reference to an
3207 * internal state machine that is processing messages on the
3208 * association. Note that the setting of this value only effects
3209 * received messages from the peer and does not effect the value that is
3210 * saved with outbound messages.
3212 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3213 unsigned int optlen)
3215 struct sctp_assoc_value params;
3216 struct sctp_sock *sp;
3217 struct sctp_association *asoc;
3219 if (optlen != sizeof(struct sctp_assoc_value))
3221 if (copy_from_user(¶ms, optval, optlen))
3226 if (params.assoc_id != 0) {
3227 asoc = sctp_id2assoc(sk, params.assoc_id);
3230 asoc->default_rcv_context = params.assoc_value;
3232 sp->default_rcv_context = params.assoc_value;
3239 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3241 * This options will at a minimum specify if the implementation is doing
3242 * fragmented interleave. Fragmented interleave, for a one to many
3243 * socket, is when subsequent calls to receive a message may return
3244 * parts of messages from different associations. Some implementations
3245 * may allow you to turn this value on or off. If so, when turned off,
3246 * no fragment interleave will occur (which will cause a head of line
3247 * blocking amongst multiple associations sharing the same one to many
3248 * socket). When this option is turned on, then each receive call may
3249 * come from a different association (thus the user must receive data
3250 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3251 * association each receive belongs to.
3253 * This option takes a boolean value. A non-zero value indicates that
3254 * fragmented interleave is on. A value of zero indicates that
3255 * fragmented interleave is off.
3257 * Note that it is important that an implementation that allows this
3258 * option to be turned on, have it off by default. Otherwise an unaware
3259 * application using the one to many model may become confused and act
3262 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3263 char __user *optval,
3264 unsigned int optlen)
3268 if (optlen != sizeof(int))
3270 if (get_user(val, (int __user *)optval))
3273 sctp_sk(sk)->frag_interleave = !!val;
3275 if (!sctp_sk(sk)->frag_interleave)
3276 sctp_sk(sk)->strm_interleave = 0;
3282 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3283 * (SCTP_PARTIAL_DELIVERY_POINT)
3285 * This option will set or get the SCTP partial delivery point. This
3286 * point is the size of a message where the partial delivery API will be
3287 * invoked to help free up rwnd space for the peer. Setting this to a
3288 * lower value will cause partial deliveries to happen more often. The
3289 * calls argument is an integer that sets or gets the partial delivery
3290 * point. Note also that the call will fail if the user attempts to set
3291 * this value larger than the socket receive buffer size.
3293 * Note that any single message having a length smaller than or equal to
3294 * the SCTP partial delivery point will be delivered in one single read
3295 * call as long as the user provided buffer is large enough to hold the
3298 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3299 char __user *optval,
3300 unsigned int optlen)
3304 if (optlen != sizeof(u32))
3306 if (get_user(val, (int __user *)optval))
3309 /* Note: We double the receive buffer from what the user sets
3310 * it to be, also initial rwnd is based on rcvbuf/2.
3312 if (val > (sk->sk_rcvbuf >> 1))
3315 sctp_sk(sk)->pd_point = val;
3317 return 0; /* is this the right error code? */
3321 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3323 * This option will allow a user to change the maximum burst of packets
3324 * that can be emitted by this association. Note that the default value
3325 * is 4, and some implementations may restrict this setting so that it
3326 * can only be lowered.
3328 * NOTE: This text doesn't seem right. Do this on a socket basis with
3329 * future associations inheriting the socket value.
3331 static int sctp_setsockopt_maxburst(struct sock *sk,
3332 char __user *optval,
3333 unsigned int optlen)
3335 struct sctp_assoc_value params;
3336 struct sctp_sock *sp;
3337 struct sctp_association *asoc;
3341 if (optlen == sizeof(int)) {
3342 pr_warn_ratelimited(DEPRECATED
3344 "Use of int in max_burst socket option deprecated.\n"
3345 "Use struct sctp_assoc_value instead\n",
3346 current->comm, task_pid_nr(current));
3347 if (copy_from_user(&val, optval, optlen))
3349 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3350 if (copy_from_user(¶ms, optval, optlen))
3352 val = params.assoc_value;
3353 assoc_id = params.assoc_id;
3359 if (assoc_id != 0) {
3360 asoc = sctp_id2assoc(sk, assoc_id);
3363 asoc->max_burst = val;
3365 sp->max_burst = val;
3371 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3373 * This set option adds a chunk type that the user is requesting to be
3374 * received only in an authenticated way. Changes to the list of chunks
3375 * will only effect future associations on the socket.
3377 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3378 char __user *optval,
3379 unsigned int optlen)
3381 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3382 struct sctp_authchunk val;
3384 if (!ep->auth_enable)
3387 if (optlen != sizeof(struct sctp_authchunk))
3389 if (copy_from_user(&val, optval, optlen))
3392 switch (val.sauth_chunk) {
3394 case SCTP_CID_INIT_ACK:
3395 case SCTP_CID_SHUTDOWN_COMPLETE:
3400 /* add this chunk id to the endpoint */
3401 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3405 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3407 * This option gets or sets the list of HMAC algorithms that the local
3408 * endpoint requires the peer to use.
3410 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3411 char __user *optval,
3412 unsigned int optlen)
3414 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3415 struct sctp_hmacalgo *hmacs;
3419 if (!ep->auth_enable)
3422 if (optlen < sizeof(struct sctp_hmacalgo))
3424 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3425 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3427 hmacs = memdup_user(optval, optlen);
3429 return PTR_ERR(hmacs);
3431 idents = hmacs->shmac_num_idents;
3432 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3433 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3438 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3445 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3447 * This option will set a shared secret key which is used to build an
3448 * association shared key.
3450 static int sctp_setsockopt_auth_key(struct sock *sk,
3451 char __user *optval,
3452 unsigned int optlen)
3454 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3455 struct sctp_authkey *authkey;
3456 struct sctp_association *asoc;
3459 if (!ep->auth_enable)
3462 if (optlen <= sizeof(struct sctp_authkey))
3464 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3467 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3468 sizeof(struct sctp_authkey));
3470 authkey = memdup_user(optval, optlen);
3471 if (IS_ERR(authkey))
3472 return PTR_ERR(authkey);
3474 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3479 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3480 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3485 ret = sctp_auth_set_key(ep, asoc, authkey);
3492 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3494 * This option will get or set the active shared key to be used to build
3495 * the association shared key.
3497 static int sctp_setsockopt_active_key(struct sock *sk,
3498 char __user *optval,
3499 unsigned int optlen)
3501 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3502 struct sctp_authkeyid val;
3503 struct sctp_association *asoc;
3505 if (!ep->auth_enable)
3508 if (optlen != sizeof(struct sctp_authkeyid))
3510 if (copy_from_user(&val, optval, optlen))
3513 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3514 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3517 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3521 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3523 * This set option will delete a shared secret key from use.
3525 static int sctp_setsockopt_del_key(struct sock *sk,
3526 char __user *optval,
3527 unsigned int optlen)
3529 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3530 struct sctp_authkeyid val;
3531 struct sctp_association *asoc;
3533 if (!ep->auth_enable)
3536 if (optlen != sizeof(struct sctp_authkeyid))
3538 if (copy_from_user(&val, optval, optlen))
3541 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3542 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3545 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3550 * 8.1.23 SCTP_AUTO_ASCONF
3552 * This option will enable or disable the use of the automatic generation of
3553 * ASCONF chunks to add and delete addresses to an existing association. Note
3554 * that this option has two caveats namely: a) it only affects sockets that
3555 * are bound to all addresses available to the SCTP stack, and b) the system
3556 * administrator may have an overriding control that turns the ASCONF feature
3557 * off no matter what setting the socket option may have.
3558 * This option expects an integer boolean flag, where a non-zero value turns on
3559 * the option, and a zero value turns off the option.
3560 * Note. In this implementation, socket operation overrides default parameter
3561 * being set by sysctl as well as FreeBSD implementation
3563 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3564 unsigned int optlen)
3567 struct sctp_sock *sp = sctp_sk(sk);
3569 if (optlen < sizeof(int))
3571 if (get_user(val, (int __user *)optval))
3573 if (!sctp_is_ep_boundall(sk) && val)
3575 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3578 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3579 if (val == 0 && sp->do_auto_asconf) {
3580 list_del(&sp->auto_asconf_list);
3581 sp->do_auto_asconf = 0;
3582 } else if (val && !sp->do_auto_asconf) {
3583 list_add_tail(&sp->auto_asconf_list,
3584 &sock_net(sk)->sctp.auto_asconf_splist);
3585 sp->do_auto_asconf = 1;
3587 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3592 * SCTP_PEER_ADDR_THLDS
3594 * This option allows us to alter the partially failed threshold for one or all
3595 * transports in an association. See Section 6.1 of:
3596 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3598 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3599 char __user *optval,
3600 unsigned int optlen)
3602 struct sctp_paddrthlds val;
3603 struct sctp_transport *trans;
3604 struct sctp_association *asoc;
3606 if (optlen < sizeof(struct sctp_paddrthlds))
3608 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3609 sizeof(struct sctp_paddrthlds)))
3613 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3614 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3617 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3619 if (val.spt_pathmaxrxt)
3620 trans->pathmaxrxt = val.spt_pathmaxrxt;
3621 trans->pf_retrans = val.spt_pathpfthld;
3624 if (val.spt_pathmaxrxt)
3625 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3626 asoc->pf_retrans = val.spt_pathpfthld;
3628 trans = sctp_addr_id2transport(sk, &val.spt_address,
3633 if (val.spt_pathmaxrxt)
3634 trans->pathmaxrxt = val.spt_pathmaxrxt;
3635 trans->pf_retrans = val.spt_pathpfthld;
3641 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3642 char __user *optval,
3643 unsigned int optlen)
3647 if (optlen < sizeof(int))
3649 if (get_user(val, (int __user *) optval))
3652 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3657 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3658 char __user *optval,
3659 unsigned int optlen)
3663 if (optlen < sizeof(int))
3665 if (get_user(val, (int __user *) optval))
3668 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3673 static int sctp_setsockopt_pr_supported(struct sock *sk,
3674 char __user *optval,
3675 unsigned int optlen)
3677 struct sctp_assoc_value params;
3678 struct sctp_association *asoc;
3679 int retval = -EINVAL;
3681 if (optlen != sizeof(params))
3684 if (copy_from_user(¶ms, optval, optlen)) {
3689 asoc = sctp_id2assoc(sk, params.assoc_id);
3691 asoc->prsctp_enable = !!params.assoc_value;
3692 } else if (!params.assoc_id) {
3693 struct sctp_sock *sp = sctp_sk(sk);
3695 sp->ep->prsctp_enable = !!params.assoc_value;
3706 static int sctp_setsockopt_default_prinfo(struct sock *sk,
3707 char __user *optval,
3708 unsigned int optlen)
3710 struct sctp_default_prinfo info;
3711 struct sctp_association *asoc;
3712 int retval = -EINVAL;
3714 if (optlen != sizeof(info))
3717 if (copy_from_user(&info, optval, sizeof(info))) {
3722 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3725 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3728 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3730 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3731 asoc->default_timetolive = info.pr_value;
3732 } else if (!info.pr_assoc_id) {
3733 struct sctp_sock *sp = sctp_sk(sk);
3735 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3736 sp->default_timetolive = info.pr_value;
3747 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3748 char __user *optval,
3749 unsigned int optlen)
3751 struct sctp_assoc_value params;
3752 struct sctp_association *asoc;
3753 int retval = -EINVAL;
3755 if (optlen != sizeof(params))
3758 if (copy_from_user(¶ms, optval, optlen)) {
3763 asoc = sctp_id2assoc(sk, params.assoc_id);
3765 asoc->reconf_enable = !!params.assoc_value;
3766 } else if (!params.assoc_id) {
3767 struct sctp_sock *sp = sctp_sk(sk);
3769 sp->ep->reconf_enable = !!params.assoc_value;
3780 static int sctp_setsockopt_enable_strreset(struct sock *sk,
3781 char __user *optval,
3782 unsigned int optlen)
3784 struct sctp_assoc_value params;
3785 struct sctp_association *asoc;
3786 int retval = -EINVAL;
3788 if (optlen != sizeof(params))
3791 if (copy_from_user(¶ms, optval, optlen)) {
3796 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3799 asoc = sctp_id2assoc(sk, params.assoc_id);
3801 asoc->strreset_enable = params.assoc_value;
3802 } else if (!params.assoc_id) {
3803 struct sctp_sock *sp = sctp_sk(sk);
3805 sp->ep->strreset_enable = params.assoc_value;
3816 static int sctp_setsockopt_reset_streams(struct sock *sk,
3817 char __user *optval,
3818 unsigned int optlen)
3820 struct sctp_reset_streams *params;
3821 struct sctp_association *asoc;
3822 int retval = -EINVAL;
3824 if (optlen < sizeof(*params))
3826 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3827 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3828 sizeof(__u16) * sizeof(*params));
3830 params = memdup_user(optval, optlen);
3832 return PTR_ERR(params);
3834 if (params->srs_number_streams * sizeof(__u16) >
3835 optlen - sizeof(*params))
3838 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3842 retval = sctp_send_reset_streams(asoc, params);
3849 static int sctp_setsockopt_reset_assoc(struct sock *sk,
3850 char __user *optval,
3851 unsigned int optlen)
3853 struct sctp_association *asoc;
3854 sctp_assoc_t associd;
3855 int retval = -EINVAL;
3857 if (optlen != sizeof(associd))
3860 if (copy_from_user(&associd, optval, optlen)) {
3865 asoc = sctp_id2assoc(sk, associd);
3869 retval = sctp_send_reset_assoc(asoc);
3875 static int sctp_setsockopt_add_streams(struct sock *sk,
3876 char __user *optval,
3877 unsigned int optlen)
3879 struct sctp_association *asoc;
3880 struct sctp_add_streams params;
3881 int retval = -EINVAL;
3883 if (optlen != sizeof(params))
3886 if (copy_from_user(¶ms, optval, optlen)) {
3891 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3895 retval = sctp_send_add_streams(asoc, ¶ms);
3901 static int sctp_setsockopt_scheduler(struct sock *sk,
3902 char __user *optval,
3903 unsigned int optlen)
3905 struct sctp_association *asoc;
3906 struct sctp_assoc_value params;
3907 int retval = -EINVAL;
3909 if (optlen < sizeof(params))
3912 optlen = sizeof(params);
3913 if (copy_from_user(¶ms, optval, optlen)) {
3918 if (params.assoc_value > SCTP_SS_MAX)
3921 asoc = sctp_id2assoc(sk, params.assoc_id);
3925 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3931 static int sctp_setsockopt_scheduler_value(struct sock *sk,
3932 char __user *optval,
3933 unsigned int optlen)
3935 struct sctp_association *asoc;
3936 struct sctp_stream_value params;
3937 int retval = -EINVAL;
3939 if (optlen < sizeof(params))
3942 optlen = sizeof(params);
3943 if (copy_from_user(¶ms, optval, optlen)) {
3948 asoc = sctp_id2assoc(sk, params.assoc_id);
3952 retval = sctp_sched_set_value(asoc, params.stream_id,
3953 params.stream_value, GFP_KERNEL);
3959 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
3960 char __user *optval,
3961 unsigned int optlen)
3963 struct sctp_sock *sp = sctp_sk(sk);
3964 struct net *net = sock_net(sk);
3965 struct sctp_assoc_value params;
3966 int retval = -EINVAL;
3968 if (optlen < sizeof(params))
3971 optlen = sizeof(params);
3972 if (copy_from_user(¶ms, optval, optlen)) {
3977 if (params.assoc_id)
3980 if (!net->sctp.intl_enable || !sp->frag_interleave) {
3985 sp->strm_interleave = !!params.assoc_value;
3993 /* API 6.2 setsockopt(), getsockopt()
3995 * Applications use setsockopt() and getsockopt() to set or retrieve
3996 * socket options. Socket options are used to change the default
3997 * behavior of sockets calls. They are described in Section 7.
4001 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4002 * int __user *optlen);
4003 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4006 * sd - the socket descript.
4007 * level - set to IPPROTO_SCTP for all SCTP options.
4008 * optname - the option name.
4009 * optval - the buffer to store the value of the option.
4010 * optlen - the size of the buffer.
4012 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4013 char __user *optval, unsigned int optlen)
4017 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4019 /* I can hardly begin to describe how wrong this is. This is
4020 * so broken as to be worse than useless. The API draft
4021 * REALLY is NOT helpful here... I am not convinced that the
4022 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4023 * are at all well-founded.
4025 if (level != SOL_SCTP) {
4026 struct sctp_af *af = sctp_sk(sk)->pf->af;
4027 retval = af->setsockopt(sk, level, optname, optval, optlen);
4034 case SCTP_SOCKOPT_BINDX_ADD:
4035 /* 'optlen' is the size of the addresses buffer. */
4036 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4037 optlen, SCTP_BINDX_ADD_ADDR);
4040 case SCTP_SOCKOPT_BINDX_REM:
4041 /* 'optlen' is the size of the addresses buffer. */
4042 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4043 optlen, SCTP_BINDX_REM_ADDR);
4046 case SCTP_SOCKOPT_CONNECTX_OLD:
4047 /* 'optlen' is the size of the addresses buffer. */
4048 retval = sctp_setsockopt_connectx_old(sk,
4049 (struct sockaddr __user *)optval,
4053 case SCTP_SOCKOPT_CONNECTX:
4054 /* 'optlen' is the size of the addresses buffer. */
4055 retval = sctp_setsockopt_connectx(sk,
4056 (struct sockaddr __user *)optval,
4060 case SCTP_DISABLE_FRAGMENTS:
4061 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4065 retval = sctp_setsockopt_events(sk, optval, optlen);
4068 case SCTP_AUTOCLOSE:
4069 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4072 case SCTP_PEER_ADDR_PARAMS:
4073 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4076 case SCTP_DELAYED_SACK:
4077 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4079 case SCTP_PARTIAL_DELIVERY_POINT:
4080 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4084 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4086 case SCTP_DEFAULT_SEND_PARAM:
4087 retval = sctp_setsockopt_default_send_param(sk, optval,
4090 case SCTP_DEFAULT_SNDINFO:
4091 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4093 case SCTP_PRIMARY_ADDR:
4094 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4096 case SCTP_SET_PEER_PRIMARY_ADDR:
4097 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4100 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4103 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4105 case SCTP_ASSOCINFO:
4106 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4108 case SCTP_I_WANT_MAPPED_V4_ADDR:
4109 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4112 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4114 case SCTP_ADAPTATION_LAYER:
4115 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4118 retval = sctp_setsockopt_context(sk, optval, optlen);
4120 case SCTP_FRAGMENT_INTERLEAVE:
4121 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4123 case SCTP_MAX_BURST:
4124 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4126 case SCTP_AUTH_CHUNK:
4127 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4129 case SCTP_HMAC_IDENT:
4130 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4133 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4135 case SCTP_AUTH_ACTIVE_KEY:
4136 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4138 case SCTP_AUTH_DELETE_KEY:
4139 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4141 case SCTP_AUTO_ASCONF:
4142 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4144 case SCTP_PEER_ADDR_THLDS:
4145 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4147 case SCTP_RECVRCVINFO:
4148 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4150 case SCTP_RECVNXTINFO:
4151 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4153 case SCTP_PR_SUPPORTED:
4154 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4156 case SCTP_DEFAULT_PRINFO:
4157 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4159 case SCTP_RECONFIG_SUPPORTED:
4160 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4162 case SCTP_ENABLE_STREAM_RESET:
4163 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4165 case SCTP_RESET_STREAMS:
4166 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4168 case SCTP_RESET_ASSOC:
4169 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4171 case SCTP_ADD_STREAMS:
4172 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4174 case SCTP_STREAM_SCHEDULER:
4175 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4177 case SCTP_STREAM_SCHEDULER_VALUE:
4178 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4180 case SCTP_INTERLEAVING_SUPPORTED:
4181 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4185 retval = -ENOPROTOOPT;
4195 /* API 3.1.6 connect() - UDP Style Syntax
4197 * An application may use the connect() call in the UDP model to initiate an
4198 * association without sending data.
4202 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4204 * sd: the socket descriptor to have a new association added to.
4206 * nam: the address structure (either struct sockaddr_in or struct
4207 * sockaddr_in6 defined in RFC2553 [7]).
4209 * len: the size of the address.
4211 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4219 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4222 /* Validate addr_len before calling common connect/connectx routine. */
4223 af = sctp_get_af_specific(addr->sa_family);
4224 if (!af || addr_len < af->sockaddr_len) {
4227 /* Pass correct addr len to common routine (so it knows there
4228 * is only one address being passed.
4230 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
4237 /* FIXME: Write comments. */
4238 static int sctp_disconnect(struct sock *sk, int flags)
4240 return -EOPNOTSUPP; /* STUB */
4243 /* 4.1.4 accept() - TCP Style Syntax
4245 * Applications use accept() call to remove an established SCTP
4246 * association from the accept queue of the endpoint. A new socket
4247 * descriptor will be returned from accept() to represent the newly
4248 * formed association.
4250 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4252 struct sctp_sock *sp;
4253 struct sctp_endpoint *ep;
4254 struct sock *newsk = NULL;
4255 struct sctp_association *asoc;
4264 if (!sctp_style(sk, TCP)) {
4265 error = -EOPNOTSUPP;
4269 if (!sctp_sstate(sk, LISTENING)) {
4274 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4276 error = sctp_wait_for_accept(sk, timeo);
4280 /* We treat the list of associations on the endpoint as the accept
4281 * queue and pick the first association on the list.
4283 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4285 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4291 /* Populate the fields of the newsk from the oldsk and migrate the
4292 * asoc to the newsk.
4294 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4302 /* The SCTP ioctl handler. */
4303 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4310 * SEQPACKET-style sockets in LISTENING state are valid, for
4311 * SCTP, so only discard TCP-style sockets in LISTENING state.
4313 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4318 struct sk_buff *skb;
4319 unsigned int amount = 0;
4321 skb = skb_peek(&sk->sk_receive_queue);
4324 * We will only return the amount of this packet since
4325 * that is all that will be read.
4329 rc = put_user(amount, (int __user *)arg);
4341 /* This is the function which gets called during socket creation to
4342 * initialized the SCTP-specific portion of the sock.
4343 * The sock structure should already be zero-filled memory.
4345 static int sctp_init_sock(struct sock *sk)
4347 struct net *net = sock_net(sk);
4348 struct sctp_sock *sp;
4350 pr_debug("%s: sk:%p\n", __func__, sk);
4354 /* Initialize the SCTP per socket area. */
4355 switch (sk->sk_type) {
4356 case SOCK_SEQPACKET:
4357 sp->type = SCTP_SOCKET_UDP;
4360 sp->type = SCTP_SOCKET_TCP;
4363 return -ESOCKTNOSUPPORT;
4366 sk->sk_gso_type = SKB_GSO_SCTP;
4368 /* Initialize default send parameters. These parameters can be
4369 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4371 sp->default_stream = 0;
4372 sp->default_ppid = 0;
4373 sp->default_flags = 0;
4374 sp->default_context = 0;
4375 sp->default_timetolive = 0;
4377 sp->default_rcv_context = 0;
4378 sp->max_burst = net->sctp.max_burst;
4380 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4382 /* Initialize default setup parameters. These parameters
4383 * can be modified with the SCTP_INITMSG socket option or
4384 * overridden by the SCTP_INIT CMSG.
4386 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4387 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
4388 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4389 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
4391 /* Initialize default RTO related parameters. These parameters can
4392 * be modified for with the SCTP_RTOINFO socket option.
4394 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4395 sp->rtoinfo.srto_max = net->sctp.rto_max;
4396 sp->rtoinfo.srto_min = net->sctp.rto_min;
4398 /* Initialize default association related parameters. These parameters
4399 * can be modified with the SCTP_ASSOCINFO socket option.
4401 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
4402 sp->assocparams.sasoc_number_peer_destinations = 0;
4403 sp->assocparams.sasoc_peer_rwnd = 0;
4404 sp->assocparams.sasoc_local_rwnd = 0;
4405 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
4407 /* Initialize default event subscriptions. By default, all the
4410 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4412 /* Default Peer Address Parameters. These defaults can
4413 * be modified via SCTP_PEER_ADDR_PARAMS
4415 sp->hbinterval = net->sctp.hb_interval;
4416 sp->pathmaxrxt = net->sctp.max_retrans_path;
4417 sp->pathmtu = 0; /* allow default discovery */
4418 sp->sackdelay = net->sctp.sack_timeout;
4420 sp->param_flags = SPP_HB_ENABLE |
4422 SPP_SACKDELAY_ENABLE;
4424 /* If enabled no SCTP message fragmentation will be performed.
4425 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4427 sp->disable_fragments = 0;
4429 /* Enable Nagle algorithm by default. */
4432 sp->recvrcvinfo = 0;
4433 sp->recvnxtinfo = 0;
4435 /* Enable by default. */
4438 /* Auto-close idle associations after the configured
4439 * number of seconds. A value of 0 disables this
4440 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4441 * for UDP-style sockets only.
4445 /* User specified fragmentation limit. */
4448 sp->adaptation_ind = 0;
4450 sp->pf = sctp_get_pf_specific(sk->sk_family);
4452 /* Control variables for partial data delivery. */
4453 atomic_set(&sp->pd_mode, 0);
4454 skb_queue_head_init(&sp->pd_lobby);
4455 sp->frag_interleave = 0;
4457 /* Create a per socket endpoint structure. Even if we
4458 * change the data structure relationships, this may still
4459 * be useful for storing pre-connect address information.
4461 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4467 sk->sk_destruct = sctp_destruct_sock;
4469 SCTP_DBG_OBJCNT_INC(sock);
4472 sk_sockets_allocated_inc(sk);
4473 sock_prot_inuse_add(net, sk->sk_prot, 1);
4475 /* Nothing can fail after this block, otherwise
4476 * sctp_destroy_sock() will be called without addr_wq_lock held
4478 if (net->sctp.default_auto_asconf) {
4479 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
4480 list_add_tail(&sp->auto_asconf_list,
4481 &net->sctp.auto_asconf_splist);
4482 sp->do_auto_asconf = 1;
4483 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4485 sp->do_auto_asconf = 0;
4493 /* Cleanup any SCTP per socket resources. Must be called with
4494 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4496 static void sctp_destroy_sock(struct sock *sk)
4498 struct sctp_sock *sp;
4500 pr_debug("%s: sk:%p\n", __func__, sk);
4502 /* Release our hold on the endpoint. */
4504 /* This could happen during socket init, thus we bail out
4505 * early, since the rest of the below is not setup either.
4510 if (sp->do_auto_asconf) {
4511 sp->do_auto_asconf = 0;
4512 list_del(&sp->auto_asconf_list);
4514 sctp_endpoint_free(sp->ep);
4516 sk_sockets_allocated_dec(sk);
4517 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4521 /* Triggered when there are no references on the socket anymore */
4522 static void sctp_destruct_sock(struct sock *sk)
4524 struct sctp_sock *sp = sctp_sk(sk);
4526 /* Free up the HMAC transform. */
4527 crypto_free_shash(sp->hmac);
4529 inet_sock_destruct(sk);
4532 /* API 4.1.7 shutdown() - TCP Style Syntax
4533 * int shutdown(int socket, int how);
4535 * sd - the socket descriptor of the association to be closed.
4536 * how - Specifies the type of shutdown. The values are
4539 * Disables further receive operations. No SCTP
4540 * protocol action is taken.
4542 * Disables further send operations, and initiates
4543 * the SCTP shutdown sequence.
4545 * Disables further send and receive operations
4546 * and initiates the SCTP shutdown sequence.
4548 static void sctp_shutdown(struct sock *sk, int how)
4550 struct net *net = sock_net(sk);
4551 struct sctp_endpoint *ep;
4553 if (!sctp_style(sk, TCP))
4556 ep = sctp_sk(sk)->ep;
4557 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4558 struct sctp_association *asoc;
4560 inet_sk_set_state(sk, SCTP_SS_CLOSING);
4561 asoc = list_entry(ep->asocs.next,
4562 struct sctp_association, asocs);
4563 sctp_primitive_SHUTDOWN(net, asoc, NULL);
4567 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4568 struct sctp_info *info)
4570 struct sctp_transport *prim;
4571 struct list_head *pos;
4574 memset(info, 0, sizeof(*info));
4576 struct sctp_sock *sp = sctp_sk(sk);
4578 info->sctpi_s_autoclose = sp->autoclose;
4579 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4580 info->sctpi_s_pd_point = sp->pd_point;
4581 info->sctpi_s_nodelay = sp->nodelay;
4582 info->sctpi_s_disable_fragments = sp->disable_fragments;
4583 info->sctpi_s_v4mapped = sp->v4mapped;
4584 info->sctpi_s_frag_interleave = sp->frag_interleave;
4585 info->sctpi_s_type = sp->type;
4590 info->sctpi_tag = asoc->c.my_vtag;
4591 info->sctpi_state = asoc->state;
4592 info->sctpi_rwnd = asoc->a_rwnd;
4593 info->sctpi_unackdata = asoc->unack_data;
4594 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4595 info->sctpi_instrms = asoc->stream.incnt;
4596 info->sctpi_outstrms = asoc->stream.outcnt;
4597 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4598 info->sctpi_inqueue++;
4599 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4600 info->sctpi_outqueue++;
4601 info->sctpi_overall_error = asoc->overall_error_count;
4602 info->sctpi_max_burst = asoc->max_burst;
4603 info->sctpi_maxseg = asoc->frag_point;
4604 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4605 info->sctpi_peer_tag = asoc->c.peer_vtag;
4607 mask = asoc->peer.ecn_capable << 1;
4608 mask = (mask | asoc->peer.ipv4_address) << 1;
4609 mask = (mask | asoc->peer.ipv6_address) << 1;
4610 mask = (mask | asoc->peer.hostname_address) << 1;
4611 mask = (mask | asoc->peer.asconf_capable) << 1;
4612 mask = (mask | asoc->peer.prsctp_capable) << 1;
4613 mask = (mask | asoc->peer.auth_capable);
4614 info->sctpi_peer_capable = mask;
4615 mask = asoc->peer.sack_needed << 1;
4616 mask = (mask | asoc->peer.sack_generation) << 1;
4617 mask = (mask | asoc->peer.zero_window_announced);
4618 info->sctpi_peer_sack = mask;
4620 info->sctpi_isacks = asoc->stats.isacks;
4621 info->sctpi_osacks = asoc->stats.osacks;
4622 info->sctpi_opackets = asoc->stats.opackets;
4623 info->sctpi_ipackets = asoc->stats.ipackets;
4624 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4625 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4626 info->sctpi_idupchunks = asoc->stats.idupchunks;
4627 info->sctpi_gapcnt = asoc->stats.gapcnt;
4628 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4629 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4630 info->sctpi_oodchunks = asoc->stats.oodchunks;
4631 info->sctpi_iodchunks = asoc->stats.iodchunks;
4632 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4633 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4635 prim = asoc->peer.primary_path;
4636 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
4637 info->sctpi_p_state = prim->state;
4638 info->sctpi_p_cwnd = prim->cwnd;
4639 info->sctpi_p_srtt = prim->srtt;
4640 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4641 info->sctpi_p_hbinterval = prim->hbinterval;
4642 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4643 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4644 info->sctpi_p_ssthresh = prim->ssthresh;
4645 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4646 info->sctpi_p_flight_size = prim->flight_size;
4647 info->sctpi_p_error = prim->error_count;
4651 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4653 /* use callback to avoid exporting the core structure */
4654 void sctp_transport_walk_start(struct rhashtable_iter *iter)
4656 rhltable_walk_enter(&sctp_transport_hashtable, iter);
4658 rhashtable_walk_start(iter);
4661 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4663 rhashtable_walk_stop(iter);
4664 rhashtable_walk_exit(iter);
4667 struct sctp_transport *sctp_transport_get_next(struct net *net,
4668 struct rhashtable_iter *iter)
4670 struct sctp_transport *t;
4672 t = rhashtable_walk_next(iter);
4673 for (; t; t = rhashtable_walk_next(iter)) {
4675 if (PTR_ERR(t) == -EAGAIN)
4680 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4681 t->asoc->peer.primary_path == t)
4688 struct sctp_transport *sctp_transport_get_idx(struct net *net,
4689 struct rhashtable_iter *iter,
4692 void *obj = SEQ_START_TOKEN;
4694 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4701 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4705 struct sctp_ep_common *epb;
4706 struct sctp_hashbucket *head;
4708 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4710 read_lock_bh(&head->lock);
4711 sctp_for_each_hentry(epb, &head->chain) {
4712 err = cb(sctp_ep(epb), p);
4716 read_unlock_bh(&head->lock);
4721 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4723 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4725 const union sctp_addr *laddr,
4726 const union sctp_addr *paddr, void *p)
4728 struct sctp_transport *transport;
4732 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
4737 err = cb(transport, p);
4738 sctp_transport_put(transport);
4742 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4744 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
4745 int (*cb_done)(struct sctp_transport *, void *),
4746 struct net *net, int *pos, void *p) {
4747 struct rhashtable_iter hti;
4748 struct sctp_transport *tsp;
4753 sctp_transport_walk_start(&hti);
4755 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4756 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4757 if (!sctp_transport_hold(tsp))
4763 sctp_transport_put(tsp);
4765 sctp_transport_walk_stop(&hti);
4768 if (cb_done && !cb_done(tsp, p)) {
4770 sctp_transport_put(tsp);
4773 sctp_transport_put(tsp);
4778 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4780 /* 7.2.1 Association Status (SCTP_STATUS)
4782 * Applications can retrieve current status information about an
4783 * association, including association state, peer receiver window size,
4784 * number of unacked data chunks, and number of data chunks pending
4785 * receipt. This information is read-only.
4787 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4788 char __user *optval,
4791 struct sctp_status status;
4792 struct sctp_association *asoc = NULL;
4793 struct sctp_transport *transport;
4794 sctp_assoc_t associd;
4797 if (len < sizeof(status)) {
4802 len = sizeof(status);
4803 if (copy_from_user(&status, optval, len)) {
4808 associd = status.sstat_assoc_id;
4809 asoc = sctp_id2assoc(sk, associd);
4815 transport = asoc->peer.primary_path;
4817 status.sstat_assoc_id = sctp_assoc2id(asoc);
4818 status.sstat_state = sctp_assoc_to_state(asoc);
4819 status.sstat_rwnd = asoc->peer.rwnd;
4820 status.sstat_unackdata = asoc->unack_data;
4822 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4823 status.sstat_instrms = asoc->stream.incnt;
4824 status.sstat_outstrms = asoc->stream.outcnt;
4825 status.sstat_fragmentation_point = asoc->frag_point;
4826 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4827 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4828 transport->af_specific->sockaddr_len);
4829 /* Map ipv4 address into v4-mapped-on-v6 address. */
4830 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
4831 (union sctp_addr *)&status.sstat_primary.spinfo_address);
4832 status.sstat_primary.spinfo_state = transport->state;
4833 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4834 status.sstat_primary.spinfo_srtt = transport->srtt;
4835 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
4836 status.sstat_primary.spinfo_mtu = transport->pathmtu;
4838 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4839 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4841 if (put_user(len, optlen)) {
4846 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4847 __func__, len, status.sstat_state, status.sstat_rwnd,
4848 status.sstat_assoc_id);
4850 if (copy_to_user(optval, &status, len)) {
4860 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4862 * Applications can retrieve information about a specific peer address
4863 * of an association, including its reachability state, congestion
4864 * window, and retransmission timer values. This information is
4867 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4868 char __user *optval,
4871 struct sctp_paddrinfo pinfo;
4872 struct sctp_transport *transport;
4875 if (len < sizeof(pinfo)) {
4880 len = sizeof(pinfo);
4881 if (copy_from_user(&pinfo, optval, len)) {
4886 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4887 pinfo.spinfo_assoc_id);
4891 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4892 pinfo.spinfo_state = transport->state;
4893 pinfo.spinfo_cwnd = transport->cwnd;
4894 pinfo.spinfo_srtt = transport->srtt;
4895 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
4896 pinfo.spinfo_mtu = transport->pathmtu;
4898 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4899 pinfo.spinfo_state = SCTP_ACTIVE;
4901 if (put_user(len, optlen)) {
4906 if (copy_to_user(optval, &pinfo, len)) {
4915 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4917 * This option is a on/off flag. If enabled no SCTP message
4918 * fragmentation will be performed. Instead if a message being sent
4919 * exceeds the current PMTU size, the message will NOT be sent and
4920 * instead a error will be indicated to the user.
4922 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4923 char __user *optval, int __user *optlen)
4927 if (len < sizeof(int))
4931 val = (sctp_sk(sk)->disable_fragments == 1);
4932 if (put_user(len, optlen))
4934 if (copy_to_user(optval, &val, len))
4939 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4941 * This socket option is used to specify various notifications and
4942 * ancillary data the user wishes to receive.
4944 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4949 if (len > sizeof(struct sctp_event_subscribe))
4950 len = sizeof(struct sctp_event_subscribe);
4951 if (put_user(len, optlen))
4953 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4958 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4960 * This socket option is applicable to the UDP-style socket only. When
4961 * set it will cause associations that are idle for more than the
4962 * specified number of seconds to automatically close. An association
4963 * being idle is defined an association that has NOT sent or received
4964 * user data. The special value of '0' indicates that no automatic
4965 * close of any associations should be performed. The option expects an
4966 * integer defining the number of seconds of idle time before an
4967 * association is closed.
4969 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4971 /* Applicable to UDP-style socket only */
4972 if (sctp_style(sk, TCP))
4974 if (len < sizeof(int))
4977 if (put_user(len, optlen))
4979 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
4984 /* Helper routine to branch off an association to a new socket. */
4985 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
4987 struct sctp_association *asoc = sctp_id2assoc(sk, id);
4988 struct sctp_sock *sp = sctp_sk(sk);
4989 struct socket *sock;
4992 /* Do not peel off from one netns to another one. */
4993 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
4999 /* An association cannot be branched off from an already peeled-off
5000 * socket, nor is this supported for tcp style sockets.
5002 if (!sctp_style(sk, UDP))
5005 /* Create a new socket. */
5006 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5010 sctp_copy_sock(sock->sk, sk, asoc);
5012 /* Make peeled-off sockets more like 1-1 accepted sockets.
5013 * Set the daddr and initialize id to something more random
5015 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5017 /* Populate the fields of the newsk from the oldsk and migrate the
5018 * asoc to the newsk.
5020 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5026 EXPORT_SYMBOL(sctp_do_peeloff);
5028 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5029 struct file **newfile, unsigned flags)
5031 struct socket *newsock;
5034 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5038 /* Map the socket to an unused fd that can be returned to the user. */
5039 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5041 sock_release(newsock);
5045 *newfile = sock_alloc_file(newsock, 0, NULL);
5046 if (IS_ERR(*newfile)) {
5047 put_unused_fd(retval);
5048 retval = PTR_ERR(*newfile);
5053 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5056 peeloff->sd = retval;
5058 if (flags & SOCK_NONBLOCK)
5059 (*newfile)->f_flags |= O_NONBLOCK;
5064 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5066 sctp_peeloff_arg_t peeloff;
5067 struct file *newfile = NULL;
5070 if (len < sizeof(sctp_peeloff_arg_t))
5072 len = sizeof(sctp_peeloff_arg_t);
5073 if (copy_from_user(&peeloff, optval, len))
5076 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5080 /* Return the fd mapped to the new socket. */
5081 if (put_user(len, optlen)) {
5083 put_unused_fd(retval);
5087 if (copy_to_user(optval, &peeloff, len)) {
5089 put_unused_fd(retval);
5092 fd_install(retval, newfile);
5097 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5098 char __user *optval, int __user *optlen)
5100 sctp_peeloff_flags_arg_t peeloff;
5101 struct file *newfile = NULL;
5104 if (len < sizeof(sctp_peeloff_flags_arg_t))
5106 len = sizeof(sctp_peeloff_flags_arg_t);
5107 if (copy_from_user(&peeloff, optval, len))
5110 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5111 &newfile, peeloff.flags);
5115 /* Return the fd mapped to the new socket. */
5116 if (put_user(len, optlen)) {
5118 put_unused_fd(retval);
5122 if (copy_to_user(optval, &peeloff, len)) {
5124 put_unused_fd(retval);
5127 fd_install(retval, newfile);
5132 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5134 * Applications can enable or disable heartbeats for any peer address of
5135 * an association, modify an address's heartbeat interval, force a
5136 * heartbeat to be sent immediately, and adjust the address's maximum
5137 * number of retransmissions sent before an address is considered
5138 * unreachable. The following structure is used to access and modify an
5139 * address's parameters:
5141 * struct sctp_paddrparams {
5142 * sctp_assoc_t spp_assoc_id;
5143 * struct sockaddr_storage spp_address;
5144 * uint32_t spp_hbinterval;
5145 * uint16_t spp_pathmaxrxt;
5146 * uint32_t spp_pathmtu;
5147 * uint32_t spp_sackdelay;
5148 * uint32_t spp_flags;
5151 * spp_assoc_id - (one-to-many style socket) This is filled in the
5152 * application, and identifies the association for
5154 * spp_address - This specifies which address is of interest.
5155 * spp_hbinterval - This contains the value of the heartbeat interval,
5156 * in milliseconds. If a value of zero
5157 * is present in this field then no changes are to
5158 * be made to this parameter.
5159 * spp_pathmaxrxt - This contains the maximum number of
5160 * retransmissions before this address shall be
5161 * considered unreachable. If a value of zero
5162 * is present in this field then no changes are to
5163 * be made to this parameter.
5164 * spp_pathmtu - When Path MTU discovery is disabled the value
5165 * specified here will be the "fixed" path mtu.
5166 * Note that if the spp_address field is empty
5167 * then all associations on this address will
5168 * have this fixed path mtu set upon them.
5170 * spp_sackdelay - When delayed sack is enabled, this value specifies
5171 * the number of milliseconds that sacks will be delayed
5172 * for. This value will apply to all addresses of an
5173 * association if the spp_address field is empty. Note
5174 * also, that if delayed sack is enabled and this
5175 * value is set to 0, no change is made to the last
5176 * recorded delayed sack timer value.
5178 * spp_flags - These flags are used to control various features
5179 * on an association. The flag field may contain
5180 * zero or more of the following options.
5182 * SPP_HB_ENABLE - Enable heartbeats on the
5183 * specified address. Note that if the address
5184 * field is empty all addresses for the association
5185 * have heartbeats enabled upon them.
5187 * SPP_HB_DISABLE - Disable heartbeats on the
5188 * speicifed address. Note that if the address
5189 * field is empty all addresses for the association
5190 * will have their heartbeats disabled. Note also
5191 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5192 * mutually exclusive, only one of these two should
5193 * be specified. Enabling both fields will have
5194 * undetermined results.
5196 * SPP_HB_DEMAND - Request a user initiated heartbeat
5197 * to be made immediately.
5199 * SPP_PMTUD_ENABLE - This field will enable PMTU
5200 * discovery upon the specified address. Note that
5201 * if the address feild is empty then all addresses
5202 * on the association are effected.
5204 * SPP_PMTUD_DISABLE - This field will disable PMTU
5205 * discovery upon the specified address. Note that
5206 * if the address feild is empty then all addresses
5207 * on the association are effected. Not also that
5208 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5209 * exclusive. Enabling both will have undetermined
5212 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5213 * on delayed sack. The time specified in spp_sackdelay
5214 * is used to specify the sack delay for this address. Note
5215 * that if spp_address is empty then all addresses will
5216 * enable delayed sack and take on the sack delay
5217 * value specified in spp_sackdelay.
5218 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5219 * off delayed sack. If the spp_address field is blank then
5220 * delayed sack is disabled for the entire association. Note
5221 * also that this field is mutually exclusive to
5222 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5225 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5226 char __user *optval, int __user *optlen)
5228 struct sctp_paddrparams params;
5229 struct sctp_transport *trans = NULL;
5230 struct sctp_association *asoc = NULL;
5231 struct sctp_sock *sp = sctp_sk(sk);
5233 if (len < sizeof(struct sctp_paddrparams))
5235 len = sizeof(struct sctp_paddrparams);
5236 if (copy_from_user(¶ms, optval, len))
5239 /* If an address other than INADDR_ANY is specified, and
5240 * no transport is found, then the request is invalid.
5242 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5243 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5244 params.spp_assoc_id);
5246 pr_debug("%s: failed no transport\n", __func__);
5251 /* Get association, if assoc_id != 0 and the socket is a one
5252 * to many style socket, and an association was not found, then
5253 * the id was invalid.
5255 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5256 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
5257 pr_debug("%s: failed no association\n", __func__);
5262 /* Fetch transport values. */
5263 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5264 params.spp_pathmtu = trans->pathmtu;
5265 params.spp_pathmaxrxt = trans->pathmaxrxt;
5266 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5268 /*draft-11 doesn't say what to return in spp_flags*/
5269 params.spp_flags = trans->param_flags;
5271 /* Fetch association values. */
5272 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5273 params.spp_pathmtu = asoc->pathmtu;
5274 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5275 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5277 /*draft-11 doesn't say what to return in spp_flags*/
5278 params.spp_flags = asoc->param_flags;
5280 /* Fetch socket values. */
5281 params.spp_hbinterval = sp->hbinterval;
5282 params.spp_pathmtu = sp->pathmtu;
5283 params.spp_sackdelay = sp->sackdelay;
5284 params.spp_pathmaxrxt = sp->pathmaxrxt;
5286 /*draft-11 doesn't say what to return in spp_flags*/
5287 params.spp_flags = sp->param_flags;
5290 if (copy_to_user(optval, ¶ms, len))
5293 if (put_user(len, optlen))
5300 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5302 * This option will effect the way delayed acks are performed. This
5303 * option allows you to get or set the delayed ack time, in
5304 * milliseconds. It also allows changing the delayed ack frequency.
5305 * Changing the frequency to 1 disables the delayed sack algorithm. If
5306 * the assoc_id is 0, then this sets or gets the endpoints default
5307 * values. If the assoc_id field is non-zero, then the set or get
5308 * effects the specified association for the one to many model (the
5309 * assoc_id field is ignored by the one to one model). Note that if
5310 * sack_delay or sack_freq are 0 when setting this option, then the
5311 * current values will remain unchanged.
5313 * struct sctp_sack_info {
5314 * sctp_assoc_t sack_assoc_id;
5315 * uint32_t sack_delay;
5316 * uint32_t sack_freq;
5319 * sack_assoc_id - This parameter, indicates which association the user
5320 * is performing an action upon. Note that if this field's value is
5321 * zero then the endpoints default value is changed (effecting future
5322 * associations only).
5324 * sack_delay - This parameter contains the number of milliseconds that
5325 * the user is requesting the delayed ACK timer be set to. Note that
5326 * this value is defined in the standard to be between 200 and 500
5329 * sack_freq - This parameter contains the number of packets that must
5330 * be received before a sack is sent without waiting for the delay
5331 * timer to expire. The default value for this is 2, setting this
5332 * value to 1 will disable the delayed sack algorithm.
5334 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5335 char __user *optval,
5338 struct sctp_sack_info params;
5339 struct sctp_association *asoc = NULL;
5340 struct sctp_sock *sp = sctp_sk(sk);
5342 if (len >= sizeof(struct sctp_sack_info)) {
5343 len = sizeof(struct sctp_sack_info);
5345 if (copy_from_user(¶ms, optval, len))
5347 } else if (len == sizeof(struct sctp_assoc_value)) {
5348 pr_warn_ratelimited(DEPRECATED
5350 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5351 "Use struct sctp_sack_info instead\n",
5352 current->comm, task_pid_nr(current));
5353 if (copy_from_user(¶ms, optval, len))
5358 /* Get association, if sack_assoc_id != 0 and the socket is a one
5359 * to many style socket, and an association was not found, then
5360 * the id was invalid.
5362 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5363 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
5367 /* Fetch association values. */
5368 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5369 params.sack_delay = jiffies_to_msecs(
5371 params.sack_freq = asoc->sackfreq;
5374 params.sack_delay = 0;
5375 params.sack_freq = 1;
5378 /* Fetch socket values. */
5379 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5380 params.sack_delay = sp->sackdelay;
5381 params.sack_freq = sp->sackfreq;
5383 params.sack_delay = 0;
5384 params.sack_freq = 1;
5388 if (copy_to_user(optval, ¶ms, len))
5391 if (put_user(len, optlen))
5397 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5399 * Applications can specify protocol parameters for the default association
5400 * initialization. The option name argument to setsockopt() and getsockopt()
5403 * Setting initialization parameters is effective only on an unconnected
5404 * socket (for UDP-style sockets only future associations are effected
5405 * by the change). With TCP-style sockets, this option is inherited by
5406 * sockets derived from a listener socket.
5408 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5410 if (len < sizeof(struct sctp_initmsg))
5412 len = sizeof(struct sctp_initmsg);
5413 if (put_user(len, optlen))
5415 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5421 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5422 char __user *optval, int __user *optlen)
5424 struct sctp_association *asoc;
5426 struct sctp_getaddrs getaddrs;
5427 struct sctp_transport *from;
5429 union sctp_addr temp;
5430 struct sctp_sock *sp = sctp_sk(sk);
5435 if (len < sizeof(struct sctp_getaddrs))
5438 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5441 /* For UDP-style sockets, id specifies the association to query. */
5442 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5446 to = optval + offsetof(struct sctp_getaddrs, addrs);
5447 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5449 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5451 memcpy(&temp, &from->ipaddr, sizeof(temp));
5452 addrlen = sctp_get_pf_specific(sk->sk_family)
5453 ->addr_to_user(sp, &temp);
5454 if (space_left < addrlen)
5456 if (copy_to_user(to, &temp, addrlen))
5460 space_left -= addrlen;
5463 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5465 bytes_copied = ((char __user *)to) - optval;
5466 if (put_user(bytes_copied, optlen))
5472 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5473 size_t space_left, int *bytes_copied)
5475 struct sctp_sockaddr_entry *addr;
5476 union sctp_addr temp;
5479 struct net *net = sock_net(sk);
5482 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
5486 if ((PF_INET == sk->sk_family) &&
5487 (AF_INET6 == addr->a.sa.sa_family))
5489 if ((PF_INET6 == sk->sk_family) &&
5490 inet_v6_ipv6only(sk) &&
5491 (AF_INET == addr->a.sa.sa_family))
5493 memcpy(&temp, &addr->a, sizeof(temp));
5494 if (!temp.v4.sin_port)
5495 temp.v4.sin_port = htons(port);
5497 addrlen = sctp_get_pf_specific(sk->sk_family)
5498 ->addr_to_user(sctp_sk(sk), &temp);
5500 if (space_left < addrlen) {
5504 memcpy(to, &temp, addrlen);
5508 space_left -= addrlen;
5509 *bytes_copied += addrlen;
5517 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5518 char __user *optval, int __user *optlen)
5520 struct sctp_bind_addr *bp;
5521 struct sctp_association *asoc;
5523 struct sctp_getaddrs getaddrs;
5524 struct sctp_sockaddr_entry *addr;
5526 union sctp_addr temp;
5527 struct sctp_sock *sp = sctp_sk(sk);
5531 int bytes_copied = 0;
5535 if (len < sizeof(struct sctp_getaddrs))
5538 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5542 * For UDP-style sockets, id specifies the association to query.
5543 * If the id field is set to the value '0' then the locally bound
5544 * addresses are returned without regard to any particular
5547 if (0 == getaddrs.assoc_id) {
5548 bp = &sctp_sk(sk)->ep->base.bind_addr;
5550 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5553 bp = &asoc->base.bind_addr;
5556 to = optval + offsetof(struct sctp_getaddrs, addrs);
5557 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5559 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
5563 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5564 * addresses from the global local address list.
5566 if (sctp_list_single_entry(&bp->address_list)) {
5567 addr = list_entry(bp->address_list.next,
5568 struct sctp_sockaddr_entry, list);
5569 if (sctp_is_any(sk, &addr->a)) {
5570 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5571 space_left, &bytes_copied);
5581 /* Protection on the bound address list is not needed since
5582 * in the socket option context we hold a socket lock and
5583 * thus the bound address list can't change.
5585 list_for_each_entry(addr, &bp->address_list, list) {
5586 memcpy(&temp, &addr->a, sizeof(temp));
5587 addrlen = sctp_get_pf_specific(sk->sk_family)
5588 ->addr_to_user(sp, &temp);
5589 if (space_left < addrlen) {
5590 err = -ENOMEM; /*fixme: right error?*/
5593 memcpy(buf, &temp, addrlen);
5595 bytes_copied += addrlen;
5597 space_left -= addrlen;
5601 if (copy_to_user(to, addrs, bytes_copied)) {
5605 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5609 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5610 * but we can't change it anymore.
5612 if (put_user(bytes_copied, optlen))
5619 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5621 * Requests that the local SCTP stack use the enclosed peer address as
5622 * the association primary. The enclosed address must be one of the
5623 * association peer's addresses.
5625 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5626 char __user *optval, int __user *optlen)
5628 struct sctp_prim prim;
5629 struct sctp_association *asoc;
5630 struct sctp_sock *sp = sctp_sk(sk);
5632 if (len < sizeof(struct sctp_prim))
5635 len = sizeof(struct sctp_prim);
5637 if (copy_from_user(&prim, optval, len))
5640 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5644 if (!asoc->peer.primary_path)
5647 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5648 asoc->peer.primary_path->af_specific->sockaddr_len);
5650 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
5651 (union sctp_addr *)&prim.ssp_addr);
5653 if (put_user(len, optlen))
5655 if (copy_to_user(optval, &prim, len))
5662 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
5664 * Requests that the local endpoint set the specified Adaptation Layer
5665 * Indication parameter for all future INIT and INIT-ACK exchanges.
5667 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
5668 char __user *optval, int __user *optlen)
5670 struct sctp_setadaptation adaptation;
5672 if (len < sizeof(struct sctp_setadaptation))
5675 len = sizeof(struct sctp_setadaptation);
5677 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
5679 if (put_user(len, optlen))
5681 if (copy_to_user(optval, &adaptation, len))
5689 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5691 * Applications that wish to use the sendto() system call may wish to
5692 * specify a default set of parameters that would normally be supplied
5693 * through the inclusion of ancillary data. This socket option allows
5694 * such an application to set the default sctp_sndrcvinfo structure.
5697 * The application that wishes to use this socket option simply passes
5698 * in to this call the sctp_sndrcvinfo structure defined in Section
5699 * 5.2.2) The input parameters accepted by this call include
5700 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5701 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5702 * to this call if the caller is using the UDP model.
5704 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5706 static int sctp_getsockopt_default_send_param(struct sock *sk,
5707 int len, char __user *optval,
5710 struct sctp_sock *sp = sctp_sk(sk);
5711 struct sctp_association *asoc;
5712 struct sctp_sndrcvinfo info;
5714 if (len < sizeof(info))
5719 if (copy_from_user(&info, optval, len))
5722 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5723 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5726 info.sinfo_stream = asoc->default_stream;
5727 info.sinfo_flags = asoc->default_flags;
5728 info.sinfo_ppid = asoc->default_ppid;
5729 info.sinfo_context = asoc->default_context;
5730 info.sinfo_timetolive = asoc->default_timetolive;
5732 info.sinfo_stream = sp->default_stream;
5733 info.sinfo_flags = sp->default_flags;
5734 info.sinfo_ppid = sp->default_ppid;
5735 info.sinfo_context = sp->default_context;
5736 info.sinfo_timetolive = sp->default_timetolive;
5739 if (put_user(len, optlen))
5741 if (copy_to_user(optval, &info, len))
5747 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5748 * (SCTP_DEFAULT_SNDINFO)
5750 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5751 char __user *optval,
5754 struct sctp_sock *sp = sctp_sk(sk);
5755 struct sctp_association *asoc;
5756 struct sctp_sndinfo info;
5758 if (len < sizeof(info))
5763 if (copy_from_user(&info, optval, len))
5766 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5767 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5770 info.snd_sid = asoc->default_stream;
5771 info.snd_flags = asoc->default_flags;
5772 info.snd_ppid = asoc->default_ppid;
5773 info.snd_context = asoc->default_context;
5775 info.snd_sid = sp->default_stream;
5776 info.snd_flags = sp->default_flags;
5777 info.snd_ppid = sp->default_ppid;
5778 info.snd_context = sp->default_context;
5781 if (put_user(len, optlen))
5783 if (copy_to_user(optval, &info, len))
5791 * 7.1.5 SCTP_NODELAY
5793 * Turn on/off any Nagle-like algorithm. This means that packets are
5794 * generally sent as soon as possible and no unnecessary delays are
5795 * introduced, at the cost of more packets in the network. Expects an
5796 * integer boolean flag.
5799 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5800 char __user *optval, int __user *optlen)
5804 if (len < sizeof(int))
5808 val = (sctp_sk(sk)->nodelay == 1);
5809 if (put_user(len, optlen))
5811 if (copy_to_user(optval, &val, len))
5818 * 7.1.1 SCTP_RTOINFO
5820 * The protocol parameters used to initialize and bound retransmission
5821 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5822 * and modify these parameters.
5823 * All parameters are time values, in milliseconds. A value of 0, when
5824 * modifying the parameters, indicates that the current value should not
5828 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5829 char __user *optval,
5830 int __user *optlen) {
5831 struct sctp_rtoinfo rtoinfo;
5832 struct sctp_association *asoc;
5834 if (len < sizeof (struct sctp_rtoinfo))
5837 len = sizeof(struct sctp_rtoinfo);
5839 if (copy_from_user(&rtoinfo, optval, len))
5842 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5844 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5847 /* Values corresponding to the specific association. */
5849 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5850 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5851 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5853 /* Values corresponding to the endpoint. */
5854 struct sctp_sock *sp = sctp_sk(sk);
5856 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5857 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5858 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5861 if (put_user(len, optlen))
5864 if (copy_to_user(optval, &rtoinfo, len))
5872 * 7.1.2 SCTP_ASSOCINFO
5874 * This option is used to tune the maximum retransmission attempts
5875 * of the association.
5876 * Returns an error if the new association retransmission value is
5877 * greater than the sum of the retransmission value of the peer.
5878 * See [SCTP] for more information.
5881 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5882 char __user *optval,
5886 struct sctp_assocparams assocparams;
5887 struct sctp_association *asoc;
5888 struct list_head *pos;
5891 if (len < sizeof (struct sctp_assocparams))
5894 len = sizeof(struct sctp_assocparams);
5896 if (copy_from_user(&assocparams, optval, len))
5899 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5901 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5904 /* Values correspoinding to the specific association */
5906 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5907 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5908 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5909 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
5911 list_for_each(pos, &asoc->peer.transport_addr_list) {
5915 assocparams.sasoc_number_peer_destinations = cnt;
5917 /* Values corresponding to the endpoint */
5918 struct sctp_sock *sp = sctp_sk(sk);
5920 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5921 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5922 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5923 assocparams.sasoc_cookie_life =
5924 sp->assocparams.sasoc_cookie_life;
5925 assocparams.sasoc_number_peer_destinations =
5927 sasoc_number_peer_destinations;
5930 if (put_user(len, optlen))
5933 if (copy_to_user(optval, &assocparams, len))
5940 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5942 * This socket option is a boolean flag which turns on or off mapped V4
5943 * addresses. If this option is turned on and the socket is type
5944 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5945 * If this option is turned off, then no mapping will be done of V4
5946 * addresses and a user will receive both PF_INET6 and PF_INET type
5947 * addresses on the socket.
5949 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5950 char __user *optval, int __user *optlen)
5953 struct sctp_sock *sp = sctp_sk(sk);
5955 if (len < sizeof(int))
5960 if (put_user(len, optlen))
5962 if (copy_to_user(optval, &val, len))
5969 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5970 * (chapter and verse is quoted at sctp_setsockopt_context())
5972 static int sctp_getsockopt_context(struct sock *sk, int len,
5973 char __user *optval, int __user *optlen)
5975 struct sctp_assoc_value params;
5976 struct sctp_sock *sp;
5977 struct sctp_association *asoc;
5979 if (len < sizeof(struct sctp_assoc_value))
5982 len = sizeof(struct sctp_assoc_value);
5984 if (copy_from_user(¶ms, optval, len))
5989 if (params.assoc_id != 0) {
5990 asoc = sctp_id2assoc(sk, params.assoc_id);
5993 params.assoc_value = asoc->default_rcv_context;
5995 params.assoc_value = sp->default_rcv_context;
5998 if (put_user(len, optlen))
6000 if (copy_to_user(optval, ¶ms, len))
6007 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6008 * This option will get or set the maximum size to put in any outgoing
6009 * SCTP DATA chunk. If a message is larger than this size it will be
6010 * fragmented by SCTP into the specified size. Note that the underlying
6011 * SCTP implementation may fragment into smaller sized chunks when the
6012 * PMTU of the underlying association is smaller than the value set by
6013 * the user. The default value for this option is '0' which indicates
6014 * the user is NOT limiting fragmentation and only the PMTU will effect
6015 * SCTP's choice of DATA chunk size. Note also that values set larger
6016 * than the maximum size of an IP datagram will effectively let SCTP
6017 * control fragmentation (i.e. the same as setting this option to 0).
6019 * The following structure is used to access and modify this parameter:
6021 * struct sctp_assoc_value {
6022 * sctp_assoc_t assoc_id;
6023 * uint32_t assoc_value;
6026 * assoc_id: This parameter is ignored for one-to-one style sockets.
6027 * For one-to-many style sockets this parameter indicates which
6028 * association the user is performing an action upon. Note that if
6029 * this field's value is zero then the endpoints default value is
6030 * changed (effecting future associations only).
6031 * assoc_value: This parameter specifies the maximum size in bytes.
6033 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6034 char __user *optval, int __user *optlen)
6036 struct sctp_assoc_value params;
6037 struct sctp_association *asoc;
6039 if (len == sizeof(int)) {
6040 pr_warn_ratelimited(DEPRECATED
6042 "Use of int in maxseg socket option.\n"
6043 "Use struct sctp_assoc_value instead\n",
6044 current->comm, task_pid_nr(current));
6045 params.assoc_id = 0;
6046 } else if (len >= sizeof(struct sctp_assoc_value)) {
6047 len = sizeof(struct sctp_assoc_value);
6048 if (copy_from_user(¶ms, optval, len))
6053 asoc = sctp_id2assoc(sk, params.assoc_id);
6054 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6058 params.assoc_value = asoc->frag_point;
6060 params.assoc_value = sctp_sk(sk)->user_frag;
6062 if (put_user(len, optlen))
6064 if (len == sizeof(int)) {
6065 if (copy_to_user(optval, ¶ms.assoc_value, len))
6068 if (copy_to_user(optval, ¶ms, len))
6076 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6077 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6079 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6080 char __user *optval, int __user *optlen)
6084 if (len < sizeof(int))
6089 val = sctp_sk(sk)->frag_interleave;
6090 if (put_user(len, optlen))
6092 if (copy_to_user(optval, &val, len))
6099 * 7.1.25. Set or Get the sctp partial delivery point
6100 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6102 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6103 char __user *optval,
6108 if (len < sizeof(u32))
6113 val = sctp_sk(sk)->pd_point;
6114 if (put_user(len, optlen))
6116 if (copy_to_user(optval, &val, len))
6123 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6124 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6126 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6127 char __user *optval,
6130 struct sctp_assoc_value params;
6131 struct sctp_sock *sp;
6132 struct sctp_association *asoc;
6134 if (len == sizeof(int)) {
6135 pr_warn_ratelimited(DEPRECATED
6137 "Use of int in max_burst socket option.\n"
6138 "Use struct sctp_assoc_value instead\n",
6139 current->comm, task_pid_nr(current));
6140 params.assoc_id = 0;
6141 } else if (len >= sizeof(struct sctp_assoc_value)) {
6142 len = sizeof(struct sctp_assoc_value);
6143 if (copy_from_user(¶ms, optval, len))
6150 if (params.assoc_id != 0) {
6151 asoc = sctp_id2assoc(sk, params.assoc_id);
6154 params.assoc_value = asoc->max_burst;
6156 params.assoc_value = sp->max_burst;
6158 if (len == sizeof(int)) {
6159 if (copy_to_user(optval, ¶ms.assoc_value, len))
6162 if (copy_to_user(optval, ¶ms, len))
6170 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6171 char __user *optval, int __user *optlen)
6173 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6174 struct sctp_hmacalgo __user *p = (void __user *)optval;
6175 struct sctp_hmac_algo_param *hmacs;
6180 if (!ep->auth_enable)
6183 hmacs = ep->auth_hmacs_list;
6184 data_len = ntohs(hmacs->param_hdr.length) -
6185 sizeof(struct sctp_paramhdr);
6187 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6190 len = sizeof(struct sctp_hmacalgo) + data_len;
6191 num_idents = data_len / sizeof(u16);
6193 if (put_user(len, optlen))
6195 if (put_user(num_idents, &p->shmac_num_idents))
6197 for (i = 0; i < num_idents; i++) {
6198 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6200 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6206 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6207 char __user *optval, int __user *optlen)
6209 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6210 struct sctp_authkeyid val;
6211 struct sctp_association *asoc;
6213 if (!ep->auth_enable)
6216 if (len < sizeof(struct sctp_authkeyid))
6219 len = sizeof(struct sctp_authkeyid);
6220 if (copy_from_user(&val, optval, len))
6223 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6224 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6228 val.scact_keynumber = asoc->active_key_id;
6230 val.scact_keynumber = ep->active_key_id;
6232 if (put_user(len, optlen))
6234 if (copy_to_user(optval, &val, len))
6240 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6241 char __user *optval, int __user *optlen)
6243 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6244 struct sctp_authchunks __user *p = (void __user *)optval;
6245 struct sctp_authchunks val;
6246 struct sctp_association *asoc;
6247 struct sctp_chunks_param *ch;
6251 if (!ep->auth_enable)
6254 if (len < sizeof(struct sctp_authchunks))
6257 if (copy_from_user(&val, optval, sizeof(val)))
6260 to = p->gauth_chunks;
6261 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6265 ch = asoc->peer.peer_chunks;
6269 /* See if the user provided enough room for all the data */
6270 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6271 if (len < num_chunks)
6274 if (copy_to_user(to, ch->chunks, num_chunks))
6277 len = sizeof(struct sctp_authchunks) + num_chunks;
6278 if (put_user(len, optlen))
6280 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6285 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6286 char __user *optval, int __user *optlen)
6288 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6289 struct sctp_authchunks __user *p = (void __user *)optval;
6290 struct sctp_authchunks val;
6291 struct sctp_association *asoc;
6292 struct sctp_chunks_param *ch;
6296 if (!ep->auth_enable)
6299 if (len < sizeof(struct sctp_authchunks))
6302 if (copy_from_user(&val, optval, sizeof(val)))
6305 to = p->gauth_chunks;
6306 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6307 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6311 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6313 ch = ep->auth_chunk_list;
6318 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6319 if (len < sizeof(struct sctp_authchunks) + num_chunks)
6322 if (copy_to_user(to, ch->chunks, num_chunks))
6325 len = sizeof(struct sctp_authchunks) + num_chunks;
6326 if (put_user(len, optlen))
6328 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6335 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6336 * This option gets the current number of associations that are attached
6337 * to a one-to-many style socket. The option value is an uint32_t.
6339 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6340 char __user *optval, int __user *optlen)
6342 struct sctp_sock *sp = sctp_sk(sk);
6343 struct sctp_association *asoc;
6346 if (sctp_style(sk, TCP))
6349 if (len < sizeof(u32))
6354 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6358 if (put_user(len, optlen))
6360 if (copy_to_user(optval, &val, len))
6367 * 8.1.23 SCTP_AUTO_ASCONF
6368 * See the corresponding setsockopt entry as description
6370 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6371 char __user *optval, int __user *optlen)
6375 if (len < sizeof(int))
6379 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6381 if (put_user(len, optlen))
6383 if (copy_to_user(optval, &val, len))
6389 * 8.2.6. Get the Current Identifiers of Associations
6390 * (SCTP_GET_ASSOC_ID_LIST)
6392 * This option gets the current list of SCTP association identifiers of
6393 * the SCTP associations handled by a one-to-many style socket.
6395 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6396 char __user *optval, int __user *optlen)
6398 struct sctp_sock *sp = sctp_sk(sk);
6399 struct sctp_association *asoc;
6400 struct sctp_assoc_ids *ids;
6403 if (sctp_style(sk, TCP))
6406 if (len < sizeof(struct sctp_assoc_ids))
6409 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6413 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6416 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6418 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
6422 ids->gaids_number_of_ids = num;
6424 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6425 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6428 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6438 * SCTP_PEER_ADDR_THLDS
6440 * This option allows us to fetch the partially failed threshold for one or all
6441 * transports in an association. See Section 6.1 of:
6442 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6444 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6445 char __user *optval,
6449 struct sctp_paddrthlds val;
6450 struct sctp_transport *trans;
6451 struct sctp_association *asoc;
6453 if (len < sizeof(struct sctp_paddrthlds))
6455 len = sizeof(struct sctp_paddrthlds);
6456 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6459 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6460 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6464 val.spt_pathpfthld = asoc->pf_retrans;
6465 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6467 trans = sctp_addr_id2transport(sk, &val.spt_address,
6472 val.spt_pathmaxrxt = trans->pathmaxrxt;
6473 val.spt_pathpfthld = trans->pf_retrans;
6476 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6483 * SCTP_GET_ASSOC_STATS
6485 * This option retrieves local per endpoint statistics. It is modeled
6486 * after OpenSolaris' implementation
6488 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6489 char __user *optval,
6492 struct sctp_assoc_stats sas;
6493 struct sctp_association *asoc = NULL;
6495 /* User must provide at least the assoc id */
6496 if (len < sizeof(sctp_assoc_t))
6499 /* Allow the struct to grow and fill in as much as possible */
6500 len = min_t(size_t, len, sizeof(sas));
6502 if (copy_from_user(&sas, optval, len))
6505 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6509 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6510 sas.sas_gapcnt = asoc->stats.gapcnt;
6511 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6512 sas.sas_osacks = asoc->stats.osacks;
6513 sas.sas_isacks = asoc->stats.isacks;
6514 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6515 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6516 sas.sas_oodchunks = asoc->stats.oodchunks;
6517 sas.sas_iodchunks = asoc->stats.iodchunks;
6518 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6519 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6520 sas.sas_idupchunks = asoc->stats.idupchunks;
6521 sas.sas_opackets = asoc->stats.opackets;
6522 sas.sas_ipackets = asoc->stats.ipackets;
6524 /* New high max rto observed, will return 0 if not a single
6525 * RTO update took place. obs_rto_ipaddr will be bogus
6528 sas.sas_maxrto = asoc->stats.max_obs_rto;
6529 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6530 sizeof(struct sockaddr_storage));
6532 /* Mark beginning of a new observation period */
6533 asoc->stats.max_obs_rto = asoc->rto_min;
6535 if (put_user(len, optlen))
6538 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
6540 if (copy_to_user(optval, &sas, len))
6546 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6547 char __user *optval,
6552 if (len < sizeof(int))
6556 if (sctp_sk(sk)->recvrcvinfo)
6558 if (put_user(len, optlen))
6560 if (copy_to_user(optval, &val, len))
6566 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6567 char __user *optval,
6572 if (len < sizeof(int))
6576 if (sctp_sk(sk)->recvnxtinfo)
6578 if (put_user(len, optlen))
6580 if (copy_to_user(optval, &val, len))
6586 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6587 char __user *optval,
6590 struct sctp_assoc_value params;
6591 struct sctp_association *asoc;
6592 int retval = -EFAULT;
6594 if (len < sizeof(params)) {
6599 len = sizeof(params);
6600 if (copy_from_user(¶ms, optval, len))
6603 asoc = sctp_id2assoc(sk, params.assoc_id);
6605 params.assoc_value = asoc->prsctp_enable;
6606 } else if (!params.assoc_id) {
6607 struct sctp_sock *sp = sctp_sk(sk);
6609 params.assoc_value = sp->ep->prsctp_enable;
6615 if (put_user(len, optlen))
6618 if (copy_to_user(optval, ¶ms, len))
6627 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6628 char __user *optval,
6631 struct sctp_default_prinfo info;
6632 struct sctp_association *asoc;
6633 int retval = -EFAULT;
6635 if (len < sizeof(info)) {
6641 if (copy_from_user(&info, optval, len))
6644 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6646 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6647 info.pr_value = asoc->default_timetolive;
6648 } else if (!info.pr_assoc_id) {
6649 struct sctp_sock *sp = sctp_sk(sk);
6651 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6652 info.pr_value = sp->default_timetolive;
6658 if (put_user(len, optlen))
6661 if (copy_to_user(optval, &info, len))
6670 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6671 char __user *optval,
6674 struct sctp_prstatus params;
6675 struct sctp_association *asoc;
6677 int retval = -EINVAL;
6679 if (len < sizeof(params))
6682 len = sizeof(params);
6683 if (copy_from_user(¶ms, optval, len)) {
6688 policy = params.sprstat_policy;
6689 if (policy & ~SCTP_PR_SCTP_MASK)
6692 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6696 if (policy == SCTP_PR_SCTP_NONE) {
6697 params.sprstat_abandoned_unsent = 0;
6698 params.sprstat_abandoned_sent = 0;
6699 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6700 params.sprstat_abandoned_unsent +=
6701 asoc->abandoned_unsent[policy];
6702 params.sprstat_abandoned_sent +=
6703 asoc->abandoned_sent[policy];
6706 params.sprstat_abandoned_unsent =
6707 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6708 params.sprstat_abandoned_sent =
6709 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6712 if (put_user(len, optlen)) {
6717 if (copy_to_user(optval, ¶ms, len)) {
6728 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6729 char __user *optval,
6732 struct sctp_stream_out_ext *streamoute;
6733 struct sctp_association *asoc;
6734 struct sctp_prstatus params;
6735 int retval = -EINVAL;
6738 if (len < sizeof(params))
6741 len = sizeof(params);
6742 if (copy_from_user(¶ms, optval, len)) {
6747 policy = params.sprstat_policy;
6748 if (policy & ~SCTP_PR_SCTP_MASK)
6751 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6752 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
6755 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6757 /* Not allocated yet, means all stats are 0 */
6758 params.sprstat_abandoned_unsent = 0;
6759 params.sprstat_abandoned_sent = 0;
6764 if (policy == SCTP_PR_SCTP_NONE) {
6765 params.sprstat_abandoned_unsent = 0;
6766 params.sprstat_abandoned_sent = 0;
6767 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6768 params.sprstat_abandoned_unsent +=
6769 streamoute->abandoned_unsent[policy];
6770 params.sprstat_abandoned_sent +=
6771 streamoute->abandoned_sent[policy];
6774 params.sprstat_abandoned_unsent =
6775 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6776 params.sprstat_abandoned_sent =
6777 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
6780 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
6791 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6792 char __user *optval,
6795 struct sctp_assoc_value params;
6796 struct sctp_association *asoc;
6797 int retval = -EFAULT;
6799 if (len < sizeof(params)) {
6804 len = sizeof(params);
6805 if (copy_from_user(¶ms, optval, len))
6808 asoc = sctp_id2assoc(sk, params.assoc_id);
6810 params.assoc_value = asoc->reconf_enable;
6811 } else if (!params.assoc_id) {
6812 struct sctp_sock *sp = sctp_sk(sk);
6814 params.assoc_value = sp->ep->reconf_enable;
6820 if (put_user(len, optlen))
6823 if (copy_to_user(optval, ¶ms, len))
6832 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6833 char __user *optval,
6836 struct sctp_assoc_value params;
6837 struct sctp_association *asoc;
6838 int retval = -EFAULT;
6840 if (len < sizeof(params)) {
6845 len = sizeof(params);
6846 if (copy_from_user(¶ms, optval, len))
6849 asoc = sctp_id2assoc(sk, params.assoc_id);
6851 params.assoc_value = asoc->strreset_enable;
6852 } else if (!params.assoc_id) {
6853 struct sctp_sock *sp = sctp_sk(sk);
6855 params.assoc_value = sp->ep->strreset_enable;
6861 if (put_user(len, optlen))
6864 if (copy_to_user(optval, ¶ms, len))
6873 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6874 char __user *optval,
6877 struct sctp_assoc_value params;
6878 struct sctp_association *asoc;
6879 int retval = -EFAULT;
6881 if (len < sizeof(params)) {
6886 len = sizeof(params);
6887 if (copy_from_user(¶ms, optval, len))
6890 asoc = sctp_id2assoc(sk, params.assoc_id);
6896 params.assoc_value = sctp_sched_get_sched(asoc);
6898 if (put_user(len, optlen))
6901 if (copy_to_user(optval, ¶ms, len))
6910 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6911 char __user *optval,
6914 struct sctp_stream_value params;
6915 struct sctp_association *asoc;
6916 int retval = -EFAULT;
6918 if (len < sizeof(params)) {
6923 len = sizeof(params);
6924 if (copy_from_user(¶ms, optval, len))
6927 asoc = sctp_id2assoc(sk, params.assoc_id);
6933 retval = sctp_sched_get_value(asoc, params.stream_id,
6934 ¶ms.stream_value);
6938 if (put_user(len, optlen)) {
6943 if (copy_to_user(optval, ¶ms, len)) {
6952 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
6953 char __user *optval,
6956 struct sctp_assoc_value params;
6957 struct sctp_association *asoc;
6958 int retval = -EFAULT;
6960 if (len < sizeof(params)) {
6965 len = sizeof(params);
6966 if (copy_from_user(¶ms, optval, len))
6969 asoc = sctp_id2assoc(sk, params.assoc_id);
6971 params.assoc_value = asoc->intl_enable;
6972 } else if (!params.assoc_id) {
6973 struct sctp_sock *sp = sctp_sk(sk);
6975 params.assoc_value = sp->strm_interleave;
6981 if (put_user(len, optlen))
6984 if (copy_to_user(optval, ¶ms, len))
6993 static int sctp_getsockopt(struct sock *sk, int level, int optname,
6994 char __user *optval, int __user *optlen)
6999 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7001 /* I can hardly begin to describe how wrong this is. This is
7002 * so broken as to be worse than useless. The API draft
7003 * REALLY is NOT helpful here... I am not convinced that the
7004 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7005 * are at all well-founded.
7007 if (level != SOL_SCTP) {
7008 struct sctp_af *af = sctp_sk(sk)->pf->af;
7010 retval = af->getsockopt(sk, level, optname, optval, optlen);
7014 if (get_user(len, optlen))
7024 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7026 case SCTP_DISABLE_FRAGMENTS:
7027 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7031 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7033 case SCTP_AUTOCLOSE:
7034 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7036 case SCTP_SOCKOPT_PEELOFF:
7037 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7039 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7040 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7042 case SCTP_PEER_ADDR_PARAMS:
7043 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7046 case SCTP_DELAYED_SACK:
7047 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7051 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7053 case SCTP_GET_PEER_ADDRS:
7054 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7057 case SCTP_GET_LOCAL_ADDRS:
7058 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7061 case SCTP_SOCKOPT_CONNECTX3:
7062 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7064 case SCTP_DEFAULT_SEND_PARAM:
7065 retval = sctp_getsockopt_default_send_param(sk, len,
7068 case SCTP_DEFAULT_SNDINFO:
7069 retval = sctp_getsockopt_default_sndinfo(sk, len,
7072 case SCTP_PRIMARY_ADDR:
7073 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7076 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7079 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7081 case SCTP_ASSOCINFO:
7082 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7084 case SCTP_I_WANT_MAPPED_V4_ADDR:
7085 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7088 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7090 case SCTP_GET_PEER_ADDR_INFO:
7091 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7094 case SCTP_ADAPTATION_LAYER:
7095 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7099 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7101 case SCTP_FRAGMENT_INTERLEAVE:
7102 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7105 case SCTP_PARTIAL_DELIVERY_POINT:
7106 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7109 case SCTP_MAX_BURST:
7110 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7113 case SCTP_AUTH_CHUNK:
7114 case SCTP_AUTH_DELETE_KEY:
7115 retval = -EOPNOTSUPP;
7117 case SCTP_HMAC_IDENT:
7118 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7120 case SCTP_AUTH_ACTIVE_KEY:
7121 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7123 case SCTP_PEER_AUTH_CHUNKS:
7124 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7127 case SCTP_LOCAL_AUTH_CHUNKS:
7128 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7131 case SCTP_GET_ASSOC_NUMBER:
7132 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7134 case SCTP_GET_ASSOC_ID_LIST:
7135 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7137 case SCTP_AUTO_ASCONF:
7138 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7140 case SCTP_PEER_ADDR_THLDS:
7141 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7143 case SCTP_GET_ASSOC_STATS:
7144 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7146 case SCTP_RECVRCVINFO:
7147 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7149 case SCTP_RECVNXTINFO:
7150 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7152 case SCTP_PR_SUPPORTED:
7153 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7155 case SCTP_DEFAULT_PRINFO:
7156 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7159 case SCTP_PR_ASSOC_STATUS:
7160 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7163 case SCTP_PR_STREAM_STATUS:
7164 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7167 case SCTP_RECONFIG_SUPPORTED:
7168 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7171 case SCTP_ENABLE_STREAM_RESET:
7172 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7175 case SCTP_STREAM_SCHEDULER:
7176 retval = sctp_getsockopt_scheduler(sk, len, optval,
7179 case SCTP_STREAM_SCHEDULER_VALUE:
7180 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7183 case SCTP_INTERLEAVING_SUPPORTED:
7184 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7188 retval = -ENOPROTOOPT;
7196 static int sctp_hash(struct sock *sk)
7202 static void sctp_unhash(struct sock *sk)
7207 /* Check if port is acceptable. Possibly find first available port.
7209 * The port hash table (contained in the 'global' SCTP protocol storage
7210 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7211 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7212 * list (the list number is the port number hashed out, so as you
7213 * would expect from a hash function, all the ports in a given list have
7214 * such a number that hashes out to the same list number; you were
7215 * expecting that, right?); so each list has a set of ports, with a
7216 * link to the socket (struct sock) that uses it, the port number and
7217 * a fastreuse flag (FIXME: NPI ipg).
7219 static struct sctp_bind_bucket *sctp_bucket_create(
7220 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
7222 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7224 struct sctp_bind_hashbucket *head; /* hash list */
7225 struct sctp_bind_bucket *pp;
7226 unsigned short snum;
7229 snum = ntohs(addr->v4.sin_port);
7231 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7236 /* Search for an available port. */
7237 int low, high, remaining, index;
7239 struct net *net = sock_net(sk);
7241 inet_get_local_port_range(net, &low, &high);
7242 remaining = (high - low) + 1;
7243 rover = prandom_u32() % remaining + low;
7247 if ((rover < low) || (rover > high))
7249 if (inet_is_local_reserved_port(net, rover))
7251 index = sctp_phashfn(sock_net(sk), rover);
7252 head = &sctp_port_hashtable[index];
7253 spin_lock(&head->lock);
7254 sctp_for_each_hentry(pp, &head->chain)
7255 if ((pp->port == rover) &&
7256 net_eq(sock_net(sk), pp->net))
7260 spin_unlock(&head->lock);
7261 } while (--remaining > 0);
7263 /* Exhausted local port range during search? */
7268 /* OK, here is the one we will use. HEAD (the port
7269 * hash table list entry) is non-NULL and we hold it's
7274 /* We are given an specific port number; we verify
7275 * that it is not being used. If it is used, we will
7276 * exahust the search in the hash list corresponding
7277 * to the port number (snum) - we detect that with the
7278 * port iterator, pp being NULL.
7280 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
7281 spin_lock(&head->lock);
7282 sctp_for_each_hentry(pp, &head->chain) {
7283 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
7290 if (!hlist_empty(&pp->owner)) {
7291 /* We had a port hash table hit - there is an
7292 * available port (pp != NULL) and it is being
7293 * used by other socket (pp->owner not empty); that other
7294 * socket is going to be sk2.
7296 int reuse = sk->sk_reuse;
7299 pr_debug("%s: found a possible match\n", __func__);
7301 if (pp->fastreuse && sk->sk_reuse &&
7302 sk->sk_state != SCTP_SS_LISTENING)
7305 /* Run through the list of sockets bound to the port
7306 * (pp->port) [via the pointers bind_next and
7307 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7308 * we get the endpoint they describe and run through
7309 * the endpoint's list of IP (v4 or v6) addresses,
7310 * comparing each of the addresses with the address of
7311 * the socket sk. If we find a match, then that means
7312 * that this port/socket (sk) combination are already
7315 sk_for_each_bound(sk2, &pp->owner) {
7316 struct sctp_endpoint *ep2;
7317 ep2 = sctp_sk(sk2)->ep;
7320 (reuse && sk2->sk_reuse &&
7321 sk2->sk_state != SCTP_SS_LISTENING))
7324 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7325 sctp_sk(sk2), sctp_sk(sk))) {
7331 pr_debug("%s: found a match\n", __func__);
7334 /* If there was a hash table miss, create a new port. */
7336 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
7339 /* In either case (hit or miss), make sure fastreuse is 1 only
7340 * if sk->sk_reuse is too (that is, if the caller requested
7341 * SO_REUSEADDR on this socket -sk-).
7343 if (hlist_empty(&pp->owner)) {
7344 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7348 } else if (pp->fastreuse &&
7349 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
7352 /* We are set, so fill up all the data in the hash table
7353 * entry, tie the socket list information with the rest of the
7354 * sockets FIXME: Blurry, NPI (ipg).
7357 if (!sctp_sk(sk)->bind_hash) {
7358 inet_sk(sk)->inet_num = snum;
7359 sk_add_bind_node(sk, &pp->owner);
7360 sctp_sk(sk)->bind_hash = pp;
7365 spin_unlock(&head->lock);
7372 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7373 * port is requested.
7375 static int sctp_get_port(struct sock *sk, unsigned short snum)
7377 union sctp_addr addr;
7378 struct sctp_af *af = sctp_sk(sk)->pf->af;
7380 /* Set up a dummy address struct from the sk. */
7381 af->from_sk(&addr, sk);
7382 addr.v4.sin_port = htons(snum);
7384 /* Note: sk->sk_num gets filled in if ephemeral port request. */
7385 return !!sctp_get_port_local(sk, &addr);
7389 * Move a socket to LISTENING state.
7391 static int sctp_listen_start(struct sock *sk, int backlog)
7393 struct sctp_sock *sp = sctp_sk(sk);
7394 struct sctp_endpoint *ep = sp->ep;
7395 struct crypto_shash *tfm = NULL;
7398 /* Allocate HMAC for generating cookie. */
7399 if (!sp->hmac && sp->sctp_hmac_alg) {
7400 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
7401 tfm = crypto_alloc_shash(alg, 0, 0);
7403 net_info_ratelimited("failed to load transform for %s: %ld\n",
7404 sp->sctp_hmac_alg, PTR_ERR(tfm));
7407 sctp_sk(sk)->hmac = tfm;
7411 * If a bind() or sctp_bindx() is not called prior to a listen()
7412 * call that allows new associations to be accepted, the system
7413 * picks an ephemeral port and will choose an address set equivalent
7414 * to binding with a wildcard address.
7416 * This is not currently spelled out in the SCTP sockets
7417 * extensions draft, but follows the practice as seen in TCP
7421 inet_sk_set_state(sk, SCTP_SS_LISTENING);
7422 if (!ep->base.bind_addr.port) {
7423 if (sctp_autobind(sk))
7426 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
7427 inet_sk_set_state(sk, SCTP_SS_CLOSED);
7432 sk->sk_max_ack_backlog = backlog;
7433 sctp_hash_endpoint(ep);
7438 * 4.1.3 / 5.1.3 listen()
7440 * By default, new associations are not accepted for UDP style sockets.
7441 * An application uses listen() to mark a socket as being able to
7442 * accept new associations.
7444 * On TCP style sockets, applications use listen() to ready the SCTP
7445 * endpoint for accepting inbound associations.
7447 * On both types of endpoints a backlog of '0' disables listening.
7449 * Move a socket to LISTENING state.
7451 int sctp_inet_listen(struct socket *sock, int backlog)
7453 struct sock *sk = sock->sk;
7454 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7457 if (unlikely(backlog < 0))
7462 /* Peeled-off sockets are not allowed to listen(). */
7463 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7466 if (sock->state != SS_UNCONNECTED)
7469 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7472 /* If backlog is zero, disable listening. */
7474 if (sctp_sstate(sk, CLOSED))
7478 sctp_unhash_endpoint(ep);
7479 sk->sk_state = SCTP_SS_CLOSED;
7481 sctp_sk(sk)->bind_hash->fastreuse = 1;
7485 /* If we are already listening, just update the backlog */
7486 if (sctp_sstate(sk, LISTENING))
7487 sk->sk_max_ack_backlog = backlog;
7489 err = sctp_listen_start(sk, backlog);
7501 * This function is done by modeling the current datagram_poll() and the
7502 * tcp_poll(). Note that, based on these implementations, we don't
7503 * lock the socket in this function, even though it seems that,
7504 * ideally, locking or some other mechanisms can be used to ensure
7505 * the integrity of the counters (sndbuf and wmem_alloc) used
7506 * in this place. We assume that we don't need locks either until proven
7509 * Another thing to note is that we include the Async I/O support
7510 * here, again, by modeling the current TCP/UDP code. We don't have
7511 * a good way to test with it yet.
7513 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7515 struct sock *sk = sock->sk;
7516 struct sctp_sock *sp = sctp_sk(sk);
7519 poll_wait(file, sk_sleep(sk), wait);
7521 sock_rps_record_flow(sk);
7523 /* A TCP-style listening socket becomes readable when the accept queue
7526 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7527 return (!list_empty(&sp->ep->asocs)) ?
7528 (EPOLLIN | EPOLLRDNORM) : 0;
7532 /* Is there any exceptional events? */
7533 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
7535 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
7536 if (sk->sk_shutdown & RCV_SHUTDOWN)
7537 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
7538 if (sk->sk_shutdown == SHUTDOWN_MASK)
7541 /* Is it readable? Reconsider this code with TCP-style support. */
7542 if (!skb_queue_empty(&sk->sk_receive_queue))
7543 mask |= EPOLLIN | EPOLLRDNORM;
7545 /* The association is either gone or not ready. */
7546 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7549 /* Is it writable? */
7550 if (sctp_writeable(sk)) {
7551 mask |= EPOLLOUT | EPOLLWRNORM;
7553 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
7555 * Since the socket is not locked, the buffer
7556 * might be made available after the writeable check and
7557 * before the bit is set. This could cause a lost I/O
7558 * signal. tcp_poll() has a race breaker for this race
7559 * condition. Based on their implementation, we put
7560 * in the following code to cover it as well.
7562 if (sctp_writeable(sk))
7563 mask |= EPOLLOUT | EPOLLWRNORM;
7568 /********************************************************************
7569 * 2nd Level Abstractions
7570 ********************************************************************/
7572 static struct sctp_bind_bucket *sctp_bucket_create(
7573 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
7575 struct sctp_bind_bucket *pp;
7577 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
7579 SCTP_DBG_OBJCNT_INC(bind_bucket);
7582 INIT_HLIST_HEAD(&pp->owner);
7584 hlist_add_head(&pp->node, &head->chain);
7589 /* Caller must hold hashbucket lock for this tb with local BH disabled */
7590 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7592 if (pp && hlist_empty(&pp->owner)) {
7593 __hlist_del(&pp->node);
7594 kmem_cache_free(sctp_bucket_cachep, pp);
7595 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7599 /* Release this socket's reference to a local port. */
7600 static inline void __sctp_put_port(struct sock *sk)
7602 struct sctp_bind_hashbucket *head =
7603 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7604 inet_sk(sk)->inet_num)];
7605 struct sctp_bind_bucket *pp;
7607 spin_lock(&head->lock);
7608 pp = sctp_sk(sk)->bind_hash;
7609 __sk_del_bind_node(sk);
7610 sctp_sk(sk)->bind_hash = NULL;
7611 inet_sk(sk)->inet_num = 0;
7612 sctp_bucket_destroy(pp);
7613 spin_unlock(&head->lock);
7616 void sctp_put_port(struct sock *sk)
7619 __sctp_put_port(sk);
7624 * The system picks an ephemeral port and choose an address set equivalent
7625 * to binding with a wildcard address.
7626 * One of those addresses will be the primary address for the association.
7627 * This automatically enables the multihoming capability of SCTP.
7629 static int sctp_autobind(struct sock *sk)
7631 union sctp_addr autoaddr;
7635 /* Initialize a local sockaddr structure to INADDR_ANY. */
7636 af = sctp_sk(sk)->pf->af;
7638 port = htons(inet_sk(sk)->inet_num);
7639 af->inaddr_any(&autoaddr, port);
7641 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7644 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7647 * 4.2 The cmsghdr Structure *
7649 * When ancillary data is sent or received, any number of ancillary data
7650 * objects can be specified by the msg_control and msg_controllen members of
7651 * the msghdr structure, because each object is preceded by
7652 * a cmsghdr structure defining the object's length (the cmsg_len member).
7653 * Historically Berkeley-derived implementations have passed only one object
7654 * at a time, but this API allows multiple objects to be
7655 * passed in a single call to sendmsg() or recvmsg(). The following example
7656 * shows two ancillary data objects in a control buffer.
7658 * |<--------------------------- msg_controllen -------------------------->|
7661 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7663 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7666 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7668 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7671 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7672 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7674 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7676 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7683 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
7685 struct msghdr *my_msg = (struct msghdr *)msg;
7686 struct cmsghdr *cmsg;
7688 for_each_cmsghdr(cmsg, my_msg) {
7689 if (!CMSG_OK(my_msg, cmsg))
7692 /* Should we parse this header or ignore? */
7693 if (cmsg->cmsg_level != IPPROTO_SCTP)
7696 /* Strictly check lengths following example in SCM code. */
7697 switch (cmsg->cmsg_type) {
7699 /* SCTP Socket API Extension
7700 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
7702 * This cmsghdr structure provides information for
7703 * initializing new SCTP associations with sendmsg().
7704 * The SCTP_INITMSG socket option uses this same data
7705 * structure. This structure is not used for
7708 * cmsg_level cmsg_type cmsg_data[]
7709 * ------------ ------------ ----------------------
7710 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7712 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
7715 cmsgs->init = CMSG_DATA(cmsg);
7719 /* SCTP Socket API Extension
7720 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
7722 * This cmsghdr structure specifies SCTP options for
7723 * sendmsg() and describes SCTP header information
7724 * about a received message through recvmsg().
7726 * cmsg_level cmsg_type cmsg_data[]
7727 * ------------ ------------ ----------------------
7728 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7730 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
7733 cmsgs->srinfo = CMSG_DATA(cmsg);
7735 if (cmsgs->srinfo->sinfo_flags &
7736 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
7737 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
7738 SCTP_ABORT | SCTP_EOF))
7743 /* SCTP Socket API Extension
7744 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7746 * This cmsghdr structure specifies SCTP options for
7747 * sendmsg(). This structure and SCTP_RCVINFO replaces
7748 * SCTP_SNDRCV which has been deprecated.
7750 * cmsg_level cmsg_type cmsg_data[]
7751 * ------------ ------------ ---------------------
7752 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7754 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7757 cmsgs->sinfo = CMSG_DATA(cmsg);
7759 if (cmsgs->sinfo->snd_flags &
7760 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
7761 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
7762 SCTP_ABORT | SCTP_EOF))
7774 * Wait for a packet..
7775 * Note: This function is the same function as in core/datagram.c
7776 * with a few modifications to make lksctp work.
7778 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
7783 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
7785 /* Socket errors? */
7786 error = sock_error(sk);
7790 if (!skb_queue_empty(&sk->sk_receive_queue))
7793 /* Socket shut down? */
7794 if (sk->sk_shutdown & RCV_SHUTDOWN)
7797 /* Sequenced packets can come disconnected. If so we report the
7802 /* Is there a good reason to think that we may receive some data? */
7803 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7806 /* Handle signals. */
7807 if (signal_pending(current))
7810 /* Let another process have a go. Since we are going to sleep
7811 * anyway. Note: This may cause odd behaviors if the message
7812 * does not fit in the user's buffer, but this seems to be the
7813 * only way to honor MSG_DONTWAIT realistically.
7816 *timeo_p = schedule_timeout(*timeo_p);
7820 finish_wait(sk_sleep(sk), &wait);
7824 error = sock_intr_errno(*timeo_p);
7827 finish_wait(sk_sleep(sk), &wait);
7832 /* Receive a datagram.
7833 * Note: This is pretty much the same routine as in core/datagram.c
7834 * with a few changes to make lksctp work.
7836 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7837 int noblock, int *err)
7840 struct sk_buff *skb;
7843 timeo = sock_rcvtimeo(sk, noblock);
7845 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7846 MAX_SCHEDULE_TIMEOUT);
7849 /* Again only user level code calls this function,
7850 * so nothing interrupt level
7851 * will suddenly eat the receive_queue.
7853 * Look at current nfs client by the way...
7854 * However, this function was correct in any case. 8)
7856 if (flags & MSG_PEEK) {
7857 skb = skb_peek(&sk->sk_receive_queue);
7859 refcount_inc(&skb->users);
7861 skb = __skb_dequeue(&sk->sk_receive_queue);
7867 /* Caller is allowed not to check sk->sk_err before calling. */
7868 error = sock_error(sk);
7872 if (sk->sk_shutdown & RCV_SHUTDOWN)
7875 if (sk_can_busy_loop(sk)) {
7876 sk_busy_loop(sk, noblock);
7878 if (!skb_queue_empty(&sk->sk_receive_queue))
7882 /* User doesn't want to wait. */
7886 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7895 /* If sndbuf has changed, wake up per association sndbuf waiters. */
7896 static void __sctp_write_space(struct sctp_association *asoc)
7898 struct sock *sk = asoc->base.sk;
7900 if (sctp_wspace(asoc) <= 0)
7903 if (waitqueue_active(&asoc->wait))
7904 wake_up_interruptible(&asoc->wait);
7906 if (sctp_writeable(sk)) {
7907 struct socket_wq *wq;
7910 wq = rcu_dereference(sk->sk_wq);
7912 if (waitqueue_active(&wq->wait))
7913 wake_up_interruptible(&wq->wait);
7915 /* Note that we try to include the Async I/O support
7916 * here by modeling from the current TCP/UDP code.
7917 * We have not tested with it yet.
7919 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
7920 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
7926 static void sctp_wake_up_waiters(struct sock *sk,
7927 struct sctp_association *asoc)
7929 struct sctp_association *tmp = asoc;
7931 /* We do accounting for the sndbuf space per association,
7932 * so we only need to wake our own association.
7934 if (asoc->ep->sndbuf_policy)
7935 return __sctp_write_space(asoc);
7937 /* If association goes down and is just flushing its
7938 * outq, then just normally notify others.
7940 if (asoc->base.dead)
7941 return sctp_write_space(sk);
7943 /* Accounting for the sndbuf space is per socket, so we
7944 * need to wake up others, try to be fair and in case of
7945 * other associations, let them have a go first instead
7946 * of just doing a sctp_write_space() call.
7948 * Note that we reach sctp_wake_up_waiters() only when
7949 * associations free up queued chunks, thus we are under
7950 * lock and the list of associations on a socket is
7951 * guaranteed not to change.
7953 for (tmp = list_next_entry(tmp, asocs); 1;
7954 tmp = list_next_entry(tmp, asocs)) {
7955 /* Manually skip the head element. */
7956 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7958 /* Wake up association. */
7959 __sctp_write_space(tmp);
7960 /* We've reached the end. */
7966 /* Do accounting for the sndbuf space.
7967 * Decrement the used sndbuf space of the corresponding association by the
7968 * data size which was just transmitted(freed).
7970 static void sctp_wfree(struct sk_buff *skb)
7972 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7973 struct sctp_association *asoc = chunk->asoc;
7974 struct sock *sk = asoc->base.sk;
7976 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7977 sizeof(struct sk_buff) +
7978 sizeof(struct sctp_chunk);
7980 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
7983 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
7985 sk->sk_wmem_queued -= skb->truesize;
7986 sk_mem_uncharge(sk, skb->truesize);
7989 sctp_wake_up_waiters(sk, asoc);
7991 sctp_association_put(asoc);
7994 /* Do accounting for the receive space on the socket.
7995 * Accounting for the association is done in ulpevent.c
7996 * We set this as a destructor for the cloned data skbs so that
7997 * accounting is done at the correct time.
7999 void sctp_sock_rfree(struct sk_buff *skb)
8001 struct sock *sk = skb->sk;
8002 struct sctp_ulpevent *event = sctp_skb2event(skb);
8004 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8007 * Mimic the behavior of sock_rfree
8009 sk_mem_uncharge(sk, event->rmem_len);
8013 /* Helper function to wait for space in the sndbuf. */
8014 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8017 struct sock *sk = asoc->base.sk;
8018 long current_timeo = *timeo_p;
8022 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8025 /* Increment the association's refcnt. */
8026 sctp_association_hold(asoc);
8028 /* Wait on the association specific sndbuf space. */
8030 prepare_to_wait_exclusive(&asoc->wait, &wait,
8031 TASK_INTERRUPTIBLE);
8032 if (asoc->base.dead)
8036 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8038 if (signal_pending(current))
8039 goto do_interrupted;
8040 if (msg_len <= sctp_wspace(asoc))
8043 /* Let another process have a go. Since we are going
8047 current_timeo = schedule_timeout(current_timeo);
8049 if (sk != asoc->base.sk)
8052 *timeo_p = current_timeo;
8056 finish_wait(&asoc->wait, &wait);
8058 /* Release the association's refcnt. */
8059 sctp_association_put(asoc);
8072 err = sock_intr_errno(*timeo_p);
8080 void sctp_data_ready(struct sock *sk)
8082 struct socket_wq *wq;
8085 wq = rcu_dereference(sk->sk_wq);
8086 if (skwq_has_sleeper(wq))
8087 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8088 EPOLLRDNORM | EPOLLRDBAND);
8089 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8093 /* If socket sndbuf has changed, wake up all per association waiters. */
8094 void sctp_write_space(struct sock *sk)
8096 struct sctp_association *asoc;
8098 /* Wake up the tasks in each wait queue. */
8099 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8100 __sctp_write_space(asoc);
8104 /* Is there any sndbuf space available on the socket?
8106 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8107 * associations on the same socket. For a UDP-style socket with
8108 * multiple associations, it is possible for it to be "unwriteable"
8109 * prematurely. I assume that this is acceptable because
8110 * a premature "unwriteable" is better than an accidental "writeable" which
8111 * would cause an unwanted block under certain circumstances. For the 1-1
8112 * UDP-style sockets or TCP-style sockets, this code should work.
8115 static int sctp_writeable(struct sock *sk)
8119 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
8125 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8126 * returns immediately with EINPROGRESS.
8128 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8130 struct sock *sk = asoc->base.sk;
8132 long current_timeo = *timeo_p;
8135 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8137 /* Increment the association's refcnt. */
8138 sctp_association_hold(asoc);
8141 prepare_to_wait_exclusive(&asoc->wait, &wait,
8142 TASK_INTERRUPTIBLE);
8145 if (sk->sk_shutdown & RCV_SHUTDOWN)
8147 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8150 if (signal_pending(current))
8151 goto do_interrupted;
8153 if (sctp_state(asoc, ESTABLISHED))
8156 /* Let another process have a go. Since we are going
8160 current_timeo = schedule_timeout(current_timeo);
8163 *timeo_p = current_timeo;
8167 finish_wait(&asoc->wait, &wait);
8169 /* Release the association's refcnt. */
8170 sctp_association_put(asoc);
8175 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
8178 err = -ECONNREFUSED;
8182 err = sock_intr_errno(*timeo_p);
8190 static int sctp_wait_for_accept(struct sock *sk, long timeo)
8192 struct sctp_endpoint *ep;
8196 ep = sctp_sk(sk)->ep;
8200 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
8201 TASK_INTERRUPTIBLE);
8203 if (list_empty(&ep->asocs)) {
8205 timeo = schedule_timeout(timeo);
8210 if (!sctp_sstate(sk, LISTENING))
8214 if (!list_empty(&ep->asocs))
8217 err = sock_intr_errno(timeo);
8218 if (signal_pending(current))
8226 finish_wait(sk_sleep(sk), &wait);
8231 static void sctp_wait_for_close(struct sock *sk, long timeout)
8236 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8237 if (list_empty(&sctp_sk(sk)->ep->asocs))
8240 timeout = schedule_timeout(timeout);
8242 } while (!signal_pending(current) && timeout);
8244 finish_wait(sk_sleep(sk), &wait);
8247 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8249 struct sk_buff *frag;
8254 /* Don't forget the fragments. */
8255 skb_walk_frags(skb, frag)
8256 sctp_skb_set_owner_r_frag(frag, sk);
8259 sctp_skb_set_owner_r(skb, sk);
8262 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8263 struct sctp_association *asoc)
8265 struct inet_sock *inet = inet_sk(sk);
8266 struct inet_sock *newinet;
8268 newsk->sk_type = sk->sk_type;
8269 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8270 newsk->sk_flags = sk->sk_flags;
8271 newsk->sk_tsflags = sk->sk_tsflags;
8272 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8273 newsk->sk_no_check_rx = sk->sk_no_check_rx;
8274 newsk->sk_reuse = sk->sk_reuse;
8276 newsk->sk_shutdown = sk->sk_shutdown;
8277 newsk->sk_destruct = sctp_destruct_sock;
8278 newsk->sk_family = sk->sk_family;
8279 newsk->sk_protocol = IPPROTO_SCTP;
8280 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8281 newsk->sk_sndbuf = sk->sk_sndbuf;
8282 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8283 newsk->sk_lingertime = sk->sk_lingertime;
8284 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8285 newsk->sk_sndtimeo = sk->sk_sndtimeo;
8286 newsk->sk_rxhash = sk->sk_rxhash;
8288 newinet = inet_sk(newsk);
8290 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8291 * getsockname() and getpeername()
8293 newinet->inet_sport = inet->inet_sport;
8294 newinet->inet_saddr = inet->inet_saddr;
8295 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8296 newinet->inet_dport = htons(asoc->peer.port);
8297 newinet->pmtudisc = inet->pmtudisc;
8298 newinet->inet_id = asoc->next_tsn ^ jiffies;
8300 newinet->uc_ttl = inet->uc_ttl;
8301 newinet->mc_loop = 1;
8302 newinet->mc_ttl = 1;
8303 newinet->mc_index = 0;
8304 newinet->mc_list = NULL;
8306 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8307 net_enable_timestamp();
8309 security_sk_clone(sk, newsk);
8312 static inline void sctp_copy_descendant(struct sock *sk_to,
8313 const struct sock *sk_from)
8315 int ancestor_size = sizeof(struct inet_sock) +
8316 sizeof(struct sctp_sock) -
8317 offsetof(struct sctp_sock, auto_asconf_list);
8319 if (sk_from->sk_family == PF_INET6)
8320 ancestor_size += sizeof(struct ipv6_pinfo);
8322 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8325 /* Populate the fields of the newsk from the oldsk and migrate the assoc
8326 * and its messages to the newsk.
8328 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8329 struct sctp_association *assoc,
8330 enum sctp_socket_type type)
8332 struct sctp_sock *oldsp = sctp_sk(oldsk);
8333 struct sctp_sock *newsp = sctp_sk(newsk);
8334 struct sctp_bind_bucket *pp; /* hash list port iterator */
8335 struct sctp_endpoint *newep = newsp->ep;
8336 struct sk_buff *skb, *tmp;
8337 struct sctp_ulpevent *event;
8338 struct sctp_bind_hashbucket *head;
8340 /* Migrate socket buffer sizes and all the socket level options to the
8343 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8344 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8345 /* Brute force copy old sctp opt. */
8346 sctp_copy_descendant(newsk, oldsk);
8348 /* Restore the ep value that was overwritten with the above structure
8354 /* Hook this new socket in to the bind_hash list. */
8355 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8356 inet_sk(oldsk)->inet_num)];
8357 spin_lock_bh(&head->lock);
8358 pp = sctp_sk(oldsk)->bind_hash;
8359 sk_add_bind_node(newsk, &pp->owner);
8360 sctp_sk(newsk)->bind_hash = pp;
8361 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
8362 spin_unlock_bh(&head->lock);
8364 /* Copy the bind_addr list from the original endpoint to the new
8365 * endpoint so that we can handle restarts properly
8367 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8368 &oldsp->ep->base.bind_addr, GFP_KERNEL);
8370 /* Move any messages in the old socket's receive queue that are for the
8371 * peeled off association to the new socket's receive queue.
8373 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8374 event = sctp_skb2event(skb);
8375 if (event->asoc == assoc) {
8376 __skb_unlink(skb, &oldsk->sk_receive_queue);
8377 __skb_queue_tail(&newsk->sk_receive_queue, skb);
8378 sctp_skb_set_owner_r_frag(skb, newsk);
8382 /* Clean up any messages pending delivery due to partial
8383 * delivery. Three cases:
8384 * 1) No partial deliver; no work.
8385 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8386 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8388 skb_queue_head_init(&newsp->pd_lobby);
8389 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
8391 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
8392 struct sk_buff_head *queue;
8394 /* Decide which queue to move pd_lobby skbs to. */
8395 if (assoc->ulpq.pd_mode) {
8396 queue = &newsp->pd_lobby;
8398 queue = &newsk->sk_receive_queue;
8400 /* Walk through the pd_lobby, looking for skbs that
8401 * need moved to the new socket.
8403 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8404 event = sctp_skb2event(skb);
8405 if (event->asoc == assoc) {
8406 __skb_unlink(skb, &oldsp->pd_lobby);
8407 __skb_queue_tail(queue, skb);
8408 sctp_skb_set_owner_r_frag(skb, newsk);
8412 /* Clear up any skbs waiting for the partial
8413 * delivery to finish.
8415 if (assoc->ulpq.pd_mode)
8416 sctp_clear_pd(oldsk, NULL);
8420 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
8422 /* Set the type of socket to indicate that it is peeled off from the
8423 * original UDP-style socket or created with the accept() call on a
8424 * TCP-style socket..
8428 /* Mark the new socket "in-use" by the user so that any packets
8429 * that may arrive on the association after we've moved it are
8430 * queued to the backlog. This prevents a potential race between
8431 * backlog processing on the old socket and new-packet processing
8432 * on the new socket.
8434 * The caller has just allocated newsk so we can guarantee that other
8435 * paths won't try to lock it and then oldsk.
8437 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
8438 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
8439 sctp_assoc_migrate(assoc, newsk);
8440 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
8442 /* If the association on the newsk is already closed before accept()
8443 * is called, set RCV_SHUTDOWN flag.
8445 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
8446 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
8447 newsk->sk_shutdown |= RCV_SHUTDOWN;
8449 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
8452 release_sock(newsk);
8456 /* This proto struct describes the ULP interface for SCTP. */
8457 struct proto sctp_prot = {
8459 .owner = THIS_MODULE,
8460 .close = sctp_close,
8461 .connect = sctp_connect,
8462 .disconnect = sctp_disconnect,
8463 .accept = sctp_accept,
8464 .ioctl = sctp_ioctl,
8465 .init = sctp_init_sock,
8466 .destroy = sctp_destroy_sock,
8467 .shutdown = sctp_shutdown,
8468 .setsockopt = sctp_setsockopt,
8469 .getsockopt = sctp_getsockopt,
8470 .sendmsg = sctp_sendmsg,
8471 .recvmsg = sctp_recvmsg,
8473 .backlog_rcv = sctp_backlog_rcv,
8475 .unhash = sctp_unhash,
8476 .get_port = sctp_get_port,
8477 .obj_size = sizeof(struct sctp_sock),
8478 .useroffset = offsetof(struct sctp_sock, subscribe),
8479 .usersize = offsetof(struct sctp_sock, initmsg) -
8480 offsetof(struct sctp_sock, subscribe) +
8481 sizeof_field(struct sctp_sock, initmsg),
8482 .sysctl_mem = sysctl_sctp_mem,
8483 .sysctl_rmem = sysctl_sctp_rmem,
8484 .sysctl_wmem = sysctl_sctp_wmem,
8485 .memory_pressure = &sctp_memory_pressure,
8486 .enter_memory_pressure = sctp_enter_memory_pressure,
8487 .memory_allocated = &sctp_memory_allocated,
8488 .sockets_allocated = &sctp_sockets_allocated,
8491 #if IS_ENABLED(CONFIG_IPV6)
8493 #include <net/transp_v6.h>
8494 static void sctp_v6_destroy_sock(struct sock *sk)
8496 sctp_destroy_sock(sk);
8497 inet6_destroy_sock(sk);
8500 struct proto sctpv6_prot = {
8502 .owner = THIS_MODULE,
8503 .close = sctp_close,
8504 .connect = sctp_connect,
8505 .disconnect = sctp_disconnect,
8506 .accept = sctp_accept,
8507 .ioctl = sctp_ioctl,
8508 .init = sctp_init_sock,
8509 .destroy = sctp_v6_destroy_sock,
8510 .shutdown = sctp_shutdown,
8511 .setsockopt = sctp_setsockopt,
8512 .getsockopt = sctp_getsockopt,
8513 .sendmsg = sctp_sendmsg,
8514 .recvmsg = sctp_recvmsg,
8516 .backlog_rcv = sctp_backlog_rcv,
8518 .unhash = sctp_unhash,
8519 .get_port = sctp_get_port,
8520 .obj_size = sizeof(struct sctp6_sock),
8521 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8522 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8523 offsetof(struct sctp6_sock, sctp.subscribe) +
8524 sizeof_field(struct sctp6_sock, sctp.initmsg),
8525 .sysctl_mem = sysctl_sctp_mem,
8526 .sysctl_rmem = sysctl_sctp_rmem,
8527 .sysctl_wmem = sysctl_sctp_wmem,
8528 .memory_pressure = &sctp_memory_pressure,
8529 .enter_memory_pressure = sctp_enter_memory_pressure,
8530 .memory_allocated = &sctp_memory_allocated,
8531 .sockets_allocated = &sctp_sockets_allocated,
8533 #endif /* IS_ENABLED(CONFIG_IPV6) */