1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
7 * Copyright (c) 2001-2002 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
10 * This file is part of the SCTP kernel implementation
12 * These functions interface with the sockets layer to implement the
13 * SCTP Extensions for the Sockets API.
15 * Note that the descriptions from the specification are USER level
16 * functions--this file is the functions which populate the struct proto
17 * for SCTP which is the BOTTOM of the sockets interface.
19 * Please send any bug reports or fixes you make to the
21 * lksctp developers <linux-sctp@vger.kernel.org>
23 * Written or modified by:
24 * La Monte H.P. Yarroll <piggy@acm.org>
25 * Narasimha Budihal <narsi@refcode.org>
26 * Karl Knutson <karl@athena.chicago.il.us>
27 * Jon Grimm <jgrimm@us.ibm.com>
28 * Xingang Guo <xingang.guo@intel.com>
29 * Daisy Chang <daisyc@us.ibm.com>
30 * Sridhar Samudrala <samudrala@us.ibm.com>
31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32 * Ardelle Fan <ardelle.fan@intel.com>
33 * Ryan Layer <rmlayer@us.ibm.com>
34 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35 * Kevin Gao <kevin.gao@intel.com>
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #include <crypto/hash.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/time.h>
45 #include <linux/sched/signal.h>
47 #include <linux/capability.h>
48 #include <linux/fcntl.h>
49 #include <linux/poll.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/file.h>
53 #include <linux/compat.h>
54 #include <linux/rhashtable.h>
58 #include <net/route.h>
60 #include <net/inet_common.h>
61 #include <net/busy_poll.h>
63 #include <linux/socket.h> /* for sa_family_t */
64 #include <linux/export.h>
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
68 #include <net/sctp/stream_sched.h>
70 /* Forward declarations for internal helper functions. */
71 static bool sctp_writeable(struct sock *sk);
72 static void sctp_wfree(struct sk_buff *skb);
73 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
75 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
76 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77 static int sctp_wait_for_accept(struct sock *sk, long timeo);
78 static void sctp_wait_for_close(struct sock *sk, long timeo);
79 static void sctp_destruct_sock(struct sock *sk);
80 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81 union sctp_addr *addr, int len);
82 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86 static int sctp_send_asconf(struct sctp_association *asoc,
87 struct sctp_chunk *chunk);
88 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89 static int sctp_autobind(struct sock *sk);
90 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91 struct sctp_association *assoc,
92 enum sctp_socket_type type);
94 static unsigned long sctp_memory_pressure;
95 static atomic_long_t sctp_memory_allocated;
96 struct percpu_counter sctp_sockets_allocated;
98 static void sctp_enter_memory_pressure(struct sock *sk)
100 sctp_memory_pressure = 1;
104 /* Get the sndbuf space available at the time on the association. */
105 static inline int sctp_wspace(struct sctp_association *asoc)
107 struct sock *sk = asoc->base.sk;
109 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110 : sk_stream_wspace(sk);
113 /* Increment the used sndbuf space count of the corresponding association by
114 * the size of the outgoing data chunk.
115 * Also, set the skb destructor for sndbuf accounting later.
117 * Since it is always 1-1 between chunk and skb, and also a new skb is always
118 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119 * destructor in the data chunk skb for the purpose of the sndbuf space
122 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
124 struct sctp_association *asoc = chunk->asoc;
125 struct sock *sk = asoc->base.sk;
127 /* The sndbuf space is tracked per association. */
128 sctp_association_hold(asoc);
131 sctp_auth_shkey_hold(chunk->shkey);
133 skb_set_owner_w(chunk->skb, sk);
135 chunk->skb->destructor = sctp_wfree;
136 /* Save the chunk pointer in skb for sctp_wfree to use later. */
137 skb_shinfo(chunk->skb)->destructor_arg = chunk;
139 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
140 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
142 sk_mem_charge(sk, chunk->skb->truesize);
145 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
147 skb_orphan(chunk->skb);
150 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
151 void (*cb)(struct sctp_chunk *))
154 struct sctp_outq *q = &asoc->outqueue;
155 struct sctp_transport *t;
156 struct sctp_chunk *chunk;
158 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
159 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
162 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
165 list_for_each_entry(chunk, &q->sacked, transmitted_list)
168 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
171 list_for_each_entry(chunk, &q->out_chunk_list, list)
175 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
176 void (*cb)(struct sk_buff *, struct sock *))
179 struct sk_buff *skb, *tmp;
181 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
184 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
187 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
191 /* Verify that this is a valid address. */
192 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
197 /* Verify basic sockaddr. */
198 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
202 /* Is this a valid SCTP address? */
203 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
206 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
212 /* Look up the association by its id. If this is not a UDP-style
213 * socket, the ID field is always ignored.
215 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
217 struct sctp_association *asoc = NULL;
219 /* If this is not a UDP-style socket, assoc id should be ignored. */
220 if (!sctp_style(sk, UDP)) {
221 /* Return NULL if the socket state is not ESTABLISHED. It
222 * could be a TCP-style listening socket or a socket which
223 * hasn't yet called connect() to establish an association.
225 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
228 /* Get the first and the only association from the list. */
229 if (!list_empty(&sctp_sk(sk)->ep->asocs))
230 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
231 struct sctp_association, asocs);
235 /* Otherwise this is a UDP-style socket. */
236 if (id <= SCTP_ALL_ASSOC)
239 spin_lock_bh(&sctp_assocs_id_lock);
240 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
241 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
243 spin_unlock_bh(&sctp_assocs_id_lock);
248 /* Look up the transport from an address and an assoc id. If both address and
249 * id are specified, the associations matching the address and the id should be
252 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
253 struct sockaddr_storage *addr,
256 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
257 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
258 union sctp_addr *laddr = (union sctp_addr *)addr;
259 struct sctp_transport *transport;
261 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
264 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
271 id_asoc = sctp_id2assoc(sk, id);
272 if (id_asoc && (id_asoc != addr_asoc))
275 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
276 (union sctp_addr *)addr);
281 /* API 3.1.2 bind() - UDP Style Syntax
282 * The syntax of bind() is,
284 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
286 * sd - the socket descriptor returned by socket().
287 * addr - the address structure (struct sockaddr_in or struct
288 * sockaddr_in6 [RFC 2553]),
289 * addr_len - the size of the address structure.
291 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
297 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
300 /* Disallow binding twice. */
301 if (!sctp_sk(sk)->ep->base.bind_addr.port)
302 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
312 static int sctp_get_port_local(struct sock *, union sctp_addr *);
314 /* Verify this is a valid sockaddr. */
315 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
316 union sctp_addr *addr, int len)
320 /* Check minimum size. */
321 if (len < sizeof (struct sockaddr))
324 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
327 if (addr->sa.sa_family == AF_INET6) {
328 if (len < SIN6_LEN_RFC2133)
330 /* V4 mapped address are really of AF_INET family */
331 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
332 !opt->pf->af_supported(AF_INET, opt))
336 /* If we get this far, af is valid. */
337 af = sctp_get_af_specific(addr->sa.sa_family);
339 if (len < af->sockaddr_len)
345 /* Bind a local address either to an endpoint or to an association. */
346 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
348 struct net *net = sock_net(sk);
349 struct sctp_sock *sp = sctp_sk(sk);
350 struct sctp_endpoint *ep = sp->ep;
351 struct sctp_bind_addr *bp = &ep->base.bind_addr;
356 /* Common sockaddr verification. */
357 af = sctp_sockaddr_af(sp, addr, len);
359 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
360 __func__, sk, addr, len);
364 snum = ntohs(addr->v4.sin_port);
366 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
367 __func__, sk, &addr->sa, bp->port, snum, len);
369 /* PF specific bind() address verification. */
370 if (!sp->pf->bind_verify(sp, addr))
371 return -EADDRNOTAVAIL;
373 /* We must either be unbound, or bind to the same port.
374 * It's OK to allow 0 ports if we are already bound.
375 * We'll just inhert an already bound port in this case
380 else if (snum != bp->port) {
381 pr_debug("%s: new port %d doesn't match existing port "
382 "%d\n", __func__, snum, bp->port);
387 if (snum && snum < inet_prot_sock(net) &&
388 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
391 /* See if the address matches any of the addresses we may have
392 * already bound before checking against other endpoints.
394 if (sctp_bind_addr_match(bp, addr, sp))
397 /* Make sure we are allowed to bind here.
398 * The function sctp_get_port_local() does duplicate address
401 addr->v4.sin_port = htons(snum);
402 if (sctp_get_port_local(sk, addr))
405 /* Refresh ephemeral port. */
407 bp->port = inet_sk(sk)->inet_num;
409 /* Add the address to the bind address list.
410 * Use GFP_ATOMIC since BHs will be disabled.
412 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
413 SCTP_ADDR_SRC, GFP_ATOMIC);
419 /* Copy back into socket for getsockname() use. */
420 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
421 sp->pf->to_sk_saddr(addr, sk);
426 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
428 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
429 * at any one time. If a sender, after sending an ASCONF chunk, decides
430 * it needs to transfer another ASCONF Chunk, it MUST wait until the
431 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
432 * subsequent ASCONF. Note this restriction binds each side, so at any
433 * time two ASCONF may be in-transit on any given association (one sent
434 * from each endpoint).
436 static int sctp_send_asconf(struct sctp_association *asoc,
437 struct sctp_chunk *chunk)
439 struct net *net = sock_net(asoc->base.sk);
442 /* If there is an outstanding ASCONF chunk, queue it for later
445 if (asoc->addip_last_asconf) {
446 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
450 /* Hold the chunk until an ASCONF_ACK is received. */
451 sctp_chunk_hold(chunk);
452 retval = sctp_primitive_ASCONF(net, asoc, chunk);
454 sctp_chunk_free(chunk);
456 asoc->addip_last_asconf = chunk;
462 /* Add a list of addresses as bind addresses to local endpoint or
465 * Basically run through each address specified in the addrs/addrcnt
466 * array/length pair, determine if it is IPv6 or IPv4 and call
467 * sctp_do_bind() on it.
469 * If any of them fails, then the operation will be reversed and the
470 * ones that were added will be removed.
472 * Only sctp_setsockopt_bindx() is supposed to call this function.
474 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
479 struct sockaddr *sa_addr;
482 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
486 for (cnt = 0; cnt < addrcnt; cnt++) {
487 /* The list may contain either IPv4 or IPv6 address;
488 * determine the address length for walking thru the list.
491 af = sctp_get_af_specific(sa_addr->sa_family);
497 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
500 addr_buf += af->sockaddr_len;
504 /* Failed. Cleanup the ones that have been added */
506 sctp_bindx_rem(sk, addrs, cnt);
514 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
515 * associations that are part of the endpoint indicating that a list of local
516 * addresses are added to the endpoint.
518 * If any of the addresses is already in the bind address list of the
519 * association, we do not send the chunk for that association. But it will not
520 * affect other associations.
522 * Only sctp_setsockopt_bindx() is supposed to call this function.
524 static int sctp_send_asconf_add_ip(struct sock *sk,
525 struct sockaddr *addrs,
528 struct net *net = sock_net(sk);
529 struct sctp_sock *sp;
530 struct sctp_endpoint *ep;
531 struct sctp_association *asoc;
532 struct sctp_bind_addr *bp;
533 struct sctp_chunk *chunk;
534 struct sctp_sockaddr_entry *laddr;
535 union sctp_addr *addr;
536 union sctp_addr saveaddr;
543 if (!net->sctp.addip_enable)
549 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
550 __func__, sk, addrs, addrcnt);
552 list_for_each_entry(asoc, &ep->asocs, asocs) {
553 if (!asoc->peer.asconf_capable)
556 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
559 if (!sctp_state(asoc, ESTABLISHED))
562 /* Check if any address in the packed array of addresses is
563 * in the bind address list of the association. If so,
564 * do not send the asconf chunk to its peer, but continue with
565 * other associations.
568 for (i = 0; i < addrcnt; i++) {
570 af = sctp_get_af_specific(addr->v4.sin_family);
576 if (sctp_assoc_lookup_laddr(asoc, addr))
579 addr_buf += af->sockaddr_len;
584 /* Use the first valid address in bind addr list of
585 * association as Address Parameter of ASCONF CHUNK.
587 bp = &asoc->base.bind_addr;
588 p = bp->address_list.next;
589 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
590 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
591 addrcnt, SCTP_PARAM_ADD_IP);
597 /* Add the new addresses to the bind address list with
598 * use_as_src set to 0.
601 for (i = 0; i < addrcnt; i++) {
603 af = sctp_get_af_specific(addr->v4.sin_family);
604 memcpy(&saveaddr, addr, af->sockaddr_len);
605 retval = sctp_add_bind_addr(bp, &saveaddr,
607 SCTP_ADDR_NEW, GFP_ATOMIC);
608 addr_buf += af->sockaddr_len;
610 if (asoc->src_out_of_asoc_ok) {
611 struct sctp_transport *trans;
613 list_for_each_entry(trans,
614 &asoc->peer.transport_addr_list, transports) {
615 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
616 2*asoc->pathmtu, 4380));
617 trans->ssthresh = asoc->peer.i.a_rwnd;
618 trans->rto = asoc->rto_initial;
619 sctp_max_rto(asoc, trans);
620 trans->rtt = trans->srtt = trans->rttvar = 0;
621 /* Clear the source and route cache */
622 sctp_transport_route(trans, NULL,
623 sctp_sk(asoc->base.sk));
626 retval = sctp_send_asconf(asoc, chunk);
633 /* Remove a list of addresses from bind addresses list. Do not remove the
636 * Basically run through each address specified in the addrs/addrcnt
637 * array/length pair, determine if it is IPv6 or IPv4 and call
638 * sctp_del_bind() on it.
640 * If any of them fails, then the operation will be reversed and the
641 * ones that were removed will be added back.
643 * At least one address has to be left; if only one address is
644 * available, the operation will return -EBUSY.
646 * Only sctp_setsockopt_bindx() is supposed to call this function.
648 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
650 struct sctp_sock *sp = sctp_sk(sk);
651 struct sctp_endpoint *ep = sp->ep;
653 struct sctp_bind_addr *bp = &ep->base.bind_addr;
656 union sctp_addr *sa_addr;
659 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
660 __func__, sk, addrs, addrcnt);
663 for (cnt = 0; cnt < addrcnt; cnt++) {
664 /* If the bind address list is empty or if there is only one
665 * bind address, there is nothing more to be removed (we need
666 * at least one address here).
668 if (list_empty(&bp->address_list) ||
669 (sctp_list_single_entry(&bp->address_list))) {
675 af = sctp_get_af_specific(sa_addr->sa.sa_family);
681 if (!af->addr_valid(sa_addr, sp, NULL)) {
682 retval = -EADDRNOTAVAIL;
686 if (sa_addr->v4.sin_port &&
687 sa_addr->v4.sin_port != htons(bp->port)) {
692 if (!sa_addr->v4.sin_port)
693 sa_addr->v4.sin_port = htons(bp->port);
695 /* FIXME - There is probably a need to check if sk->sk_saddr and
696 * sk->sk_rcv_addr are currently set to one of the addresses to
697 * be removed. This is something which needs to be looked into
698 * when we are fixing the outstanding issues with multi-homing
699 * socket routing and failover schemes. Refer to comments in
700 * sctp_do_bind(). -daisy
702 retval = sctp_del_bind_addr(bp, sa_addr);
704 addr_buf += af->sockaddr_len;
707 /* Failed. Add the ones that has been removed back */
709 sctp_bindx_add(sk, addrs, cnt);
717 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
718 * the associations that are part of the endpoint indicating that a list of
719 * local addresses are removed from the endpoint.
721 * If any of the addresses is already in the bind address list of the
722 * association, we do not send the chunk for that association. But it will not
723 * affect other associations.
725 * Only sctp_setsockopt_bindx() is supposed to call this function.
727 static int sctp_send_asconf_del_ip(struct sock *sk,
728 struct sockaddr *addrs,
731 struct net *net = sock_net(sk);
732 struct sctp_sock *sp;
733 struct sctp_endpoint *ep;
734 struct sctp_association *asoc;
735 struct sctp_transport *transport;
736 struct sctp_bind_addr *bp;
737 struct sctp_chunk *chunk;
738 union sctp_addr *laddr;
741 struct sctp_sockaddr_entry *saddr;
747 if (!net->sctp.addip_enable)
753 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
754 __func__, sk, addrs, addrcnt);
756 list_for_each_entry(asoc, &ep->asocs, asocs) {
758 if (!asoc->peer.asconf_capable)
761 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
764 if (!sctp_state(asoc, ESTABLISHED))
767 /* Check if any address in the packed array of addresses is
768 * not present in the bind address list of the association.
769 * If so, do not send the asconf chunk to its peer, but
770 * continue with other associations.
773 for (i = 0; i < addrcnt; i++) {
775 af = sctp_get_af_specific(laddr->v4.sin_family);
781 if (!sctp_assoc_lookup_laddr(asoc, laddr))
784 addr_buf += af->sockaddr_len;
789 /* Find one address in the association's bind address list
790 * that is not in the packed array of addresses. This is to
791 * make sure that we do not delete all the addresses in the
794 bp = &asoc->base.bind_addr;
795 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
797 if ((laddr == NULL) && (addrcnt == 1)) {
798 if (asoc->asconf_addr_del_pending)
800 asoc->asconf_addr_del_pending =
801 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
802 if (asoc->asconf_addr_del_pending == NULL) {
806 asoc->asconf_addr_del_pending->sa.sa_family =
808 asoc->asconf_addr_del_pending->v4.sin_port =
810 if (addrs->sa_family == AF_INET) {
811 struct sockaddr_in *sin;
813 sin = (struct sockaddr_in *)addrs;
814 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
815 } else if (addrs->sa_family == AF_INET6) {
816 struct sockaddr_in6 *sin6;
818 sin6 = (struct sockaddr_in6 *)addrs;
819 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
822 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
823 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
824 asoc->asconf_addr_del_pending);
826 asoc->src_out_of_asoc_ok = 1;
834 /* We do not need RCU protection throughout this loop
835 * because this is done under a socket lock from the
838 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
846 /* Reset use_as_src flag for the addresses in the bind address
847 * list that are to be deleted.
850 for (i = 0; i < addrcnt; i++) {
852 af = sctp_get_af_specific(laddr->v4.sin_family);
853 list_for_each_entry(saddr, &bp->address_list, list) {
854 if (sctp_cmp_addr_exact(&saddr->a, laddr))
855 saddr->state = SCTP_ADDR_DEL;
857 addr_buf += af->sockaddr_len;
860 /* Update the route and saddr entries for all the transports
861 * as some of the addresses in the bind address list are
862 * about to be deleted and cannot be used as source addresses.
864 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
866 sctp_transport_route(transport, NULL,
867 sctp_sk(asoc->base.sk));
871 /* We don't need to transmit ASCONF */
873 retval = sctp_send_asconf(asoc, chunk);
879 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
880 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
882 struct sock *sk = sctp_opt2sk(sp);
883 union sctp_addr *addr;
886 /* It is safe to write port space in caller. */
888 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
889 af = sctp_get_af_specific(addr->sa.sa_family);
892 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
895 if (addrw->state == SCTP_ADDR_NEW)
896 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
898 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
901 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
904 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
907 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
908 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
911 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
912 * Section 3.1.2 for this usage.
914 * addrs is a pointer to an array of one or more socket addresses. Each
915 * address is contained in its appropriate structure (i.e. struct
916 * sockaddr_in or struct sockaddr_in6) the family of the address type
917 * must be used to distinguish the address length (note that this
918 * representation is termed a "packed array" of addresses). The caller
919 * specifies the number of addresses in the array with addrcnt.
921 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
922 * -1, and sets errno to the appropriate error code.
924 * For SCTP, the port given in each socket address must be the same, or
925 * sctp_bindx() will fail, setting errno to EINVAL.
927 * The flags parameter is formed from the bitwise OR of zero or more of
928 * the following currently defined flags:
930 * SCTP_BINDX_ADD_ADDR
932 * SCTP_BINDX_REM_ADDR
934 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
935 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
936 * addresses from the association. The two flags are mutually exclusive;
937 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
938 * not remove all addresses from an association; sctp_bindx() will
939 * reject such an attempt with EINVAL.
941 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
942 * additional addresses with an endpoint after calling bind(). Or use
943 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
944 * socket is associated with so that no new association accepted will be
945 * associated with those addresses. If the endpoint supports dynamic
946 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
947 * endpoint to send the appropriate message to the peer to change the
948 * peers address lists.
950 * Adding and removing addresses from a connected association is
951 * optional functionality. Implementations that do not support this
952 * functionality should return EOPNOTSUPP.
954 * Basically do nothing but copying the addresses from user to kernel
955 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
956 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
959 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
962 * sk The sk of the socket
963 * addrs The pointer to the addresses in user land
964 * addrssize Size of the addrs buffer
965 * op Operation to perform (add or remove, see the flags of
968 * Returns 0 if ok, <0 errno code on error.
970 static int sctp_setsockopt_bindx(struct sock *sk,
971 struct sockaddr __user *addrs,
972 int addrs_size, int op)
974 struct sockaddr *kaddrs;
978 struct sockaddr *sa_addr;
982 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
983 __func__, sk, addrs, addrs_size, op);
985 if (unlikely(addrs_size <= 0))
988 kaddrs = memdup_user(addrs, addrs_size);
990 return PTR_ERR(kaddrs);
992 /* Walk through the addrs buffer and count the number of addresses. */
994 while (walk_size < addrs_size) {
995 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1001 af = sctp_get_af_specific(sa_addr->sa_family);
1003 /* If the address family is not supported or if this address
1004 * causes the address buffer to overflow return EINVAL.
1006 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1011 addr_buf += af->sockaddr_len;
1012 walk_size += af->sockaddr_len;
1017 case SCTP_BINDX_ADD_ADDR:
1018 /* Allow security module to validate bindx addresses. */
1019 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1020 (struct sockaddr *)kaddrs,
1024 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1027 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1030 case SCTP_BINDX_REM_ADDR:
1031 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1034 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1048 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1050 * Common routine for handling connect() and sctp_connectx().
1051 * Connect will come in with just a single address.
1053 static int __sctp_connect(struct sock *sk,
1054 struct sockaddr *kaddrs,
1055 int addrs_size, int flags,
1056 sctp_assoc_t *assoc_id)
1058 struct net *net = sock_net(sk);
1059 struct sctp_sock *sp;
1060 struct sctp_endpoint *ep;
1061 struct sctp_association *asoc = NULL;
1062 struct sctp_association *asoc2;
1063 struct sctp_transport *transport;
1065 enum sctp_scope scope;
1070 union sctp_addr *sa_addr = NULL;
1072 unsigned short port;
1077 /* connect() cannot be done on a socket that is already in ESTABLISHED
1078 * state - UDP-style peeled off socket or a TCP-style socket that
1079 * is already connected.
1080 * It cannot be done even on a TCP-style listening socket.
1082 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1083 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1088 /* Walk through the addrs buffer and count the number of addresses. */
1090 while (walk_size < addrs_size) {
1093 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1099 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1101 /* If the address family is not supported or if this address
1102 * causes the address buffer to overflow return EINVAL.
1104 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1109 port = ntohs(sa_addr->v4.sin_port);
1111 /* Save current address so we can work with it */
1112 memcpy(&to, sa_addr, af->sockaddr_len);
1114 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1118 /* Make sure the destination port is correctly set
1121 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1126 /* Check if there already is a matching association on the
1127 * endpoint (other than the one created here).
1129 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1130 if (asoc2 && asoc2 != asoc) {
1131 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1138 /* If we could not find a matching association on the endpoint,
1139 * make sure that there is no peeled-off association matching
1140 * the peer address even on another socket.
1142 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1143 err = -EADDRNOTAVAIL;
1148 /* If a bind() or sctp_bindx() is not called prior to
1149 * an sctp_connectx() call, the system picks an
1150 * ephemeral port and will choose an address set
1151 * equivalent to binding with a wildcard address.
1153 if (!ep->base.bind_addr.port) {
1154 if (sctp_autobind(sk)) {
1160 * If an unprivileged user inherits a 1-many
1161 * style socket with open associations on a
1162 * privileged port, it MAY be permitted to
1163 * accept new associations, but it SHOULD NOT
1164 * be permitted to open new associations.
1166 if (ep->base.bind_addr.port <
1167 inet_prot_sock(net) &&
1168 !ns_capable(net->user_ns,
1169 CAP_NET_BIND_SERVICE)) {
1175 scope = sctp_scope(&to);
1176 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1182 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1190 /* Prime the peer's transport structures. */
1191 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1199 addr_buf += af->sockaddr_len;
1200 walk_size += af->sockaddr_len;
1203 /* In case the user of sctp_connectx() wants an association
1204 * id back, assign one now.
1207 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1212 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1217 /* Initialize sk's dport and daddr for getpeername() */
1218 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1219 sp->pf->to_sk_daddr(sa_addr, sk);
1222 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1225 *assoc_id = asoc->assoc_id;
1227 err = sctp_wait_for_connect(asoc, &timeo);
1228 /* Note: the asoc may be freed after the return of
1229 * sctp_wait_for_connect.
1232 /* Don't free association on exit. */
1236 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1237 __func__, asoc, kaddrs, err);
1240 /* sctp_primitive_ASSOCIATE may have added this association
1241 * To the hash table, try to unhash it, just in case, its a noop
1242 * if it wasn't hashed so we're safe
1244 sctp_association_free(asoc);
1249 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1252 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1253 * sctp_assoc_t *asoc);
1255 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1256 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1257 * or IPv6 addresses.
1259 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1260 * Section 3.1.2 for this usage.
1262 * addrs is a pointer to an array of one or more socket addresses. Each
1263 * address is contained in its appropriate structure (i.e. struct
1264 * sockaddr_in or struct sockaddr_in6) the family of the address type
1265 * must be used to distengish the address length (note that this
1266 * representation is termed a "packed array" of addresses). The caller
1267 * specifies the number of addresses in the array with addrcnt.
1269 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1270 * the association id of the new association. On failure, sctp_connectx()
1271 * returns -1, and sets errno to the appropriate error code. The assoc_id
1272 * is not touched by the kernel.
1274 * For SCTP, the port given in each socket address must be the same, or
1275 * sctp_connectx() will fail, setting errno to EINVAL.
1277 * An application can use sctp_connectx to initiate an association with
1278 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1279 * allows a caller to specify multiple addresses at which a peer can be
1280 * reached. The way the SCTP stack uses the list of addresses to set up
1281 * the association is implementation dependent. This function only
1282 * specifies that the stack will try to make use of all the addresses in
1283 * the list when needed.
1285 * Note that the list of addresses passed in is only used for setting up
1286 * the association. It does not necessarily equal the set of addresses
1287 * the peer uses for the resulting association. If the caller wants to
1288 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1289 * retrieve them after the association has been set up.
1291 * Basically do nothing but copying the addresses from user to kernel
1292 * land and invoking either sctp_connectx(). This is used for tunneling
1293 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1295 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1298 * sk The sk of the socket
1299 * addrs The pointer to the addresses in user land
1300 * addrssize Size of the addrs buffer
1302 * Returns >=0 if ok, <0 errno code on error.
1304 static int __sctp_setsockopt_connectx(struct sock *sk,
1305 struct sockaddr __user *addrs,
1307 sctp_assoc_t *assoc_id)
1309 struct sockaddr *kaddrs;
1310 int err = 0, flags = 0;
1312 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1313 __func__, sk, addrs, addrs_size);
1315 if (unlikely(addrs_size <= 0))
1318 kaddrs = memdup_user(addrs, addrs_size);
1320 return PTR_ERR(kaddrs);
1322 /* Allow security module to validate connectx addresses. */
1323 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1324 (struct sockaddr *)kaddrs,
1329 /* in-kernel sockets don't generally have a file allocated to them
1330 * if all they do is call sock_create_kern().
1332 if (sk->sk_socket->file)
1333 flags = sk->sk_socket->file->f_flags;
1335 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1344 * This is an older interface. It's kept for backward compatibility
1345 * to the option that doesn't provide association id.
1347 static int sctp_setsockopt_connectx_old(struct sock *sk,
1348 struct sockaddr __user *addrs,
1351 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1355 * New interface for the API. The since the API is done with a socket
1356 * option, to make it simple we feed back the association id is as a return
1357 * indication to the call. Error is always negative and association id is
1360 static int sctp_setsockopt_connectx(struct sock *sk,
1361 struct sockaddr __user *addrs,
1364 sctp_assoc_t assoc_id = 0;
1367 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1376 * New (hopefully final) interface for the API.
1377 * We use the sctp_getaddrs_old structure so that use-space library
1378 * can avoid any unnecessary allocations. The only different part
1379 * is that we store the actual length of the address buffer into the
1380 * addrs_num structure member. That way we can re-use the existing
1383 #ifdef CONFIG_COMPAT
1384 struct compat_sctp_getaddrs_old {
1385 sctp_assoc_t assoc_id;
1387 compat_uptr_t addrs; /* struct sockaddr * */
1391 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1392 char __user *optval,
1395 struct sctp_getaddrs_old param;
1396 sctp_assoc_t assoc_id = 0;
1399 #ifdef CONFIG_COMPAT
1400 if (in_compat_syscall()) {
1401 struct compat_sctp_getaddrs_old param32;
1403 if (len < sizeof(param32))
1405 if (copy_from_user(¶m32, optval, sizeof(param32)))
1408 param.assoc_id = param32.assoc_id;
1409 param.addr_num = param32.addr_num;
1410 param.addrs = compat_ptr(param32.addrs);
1414 if (len < sizeof(param))
1416 if (copy_from_user(¶m, optval, sizeof(param)))
1420 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1421 param.addrs, param.addr_num,
1423 if (err == 0 || err == -EINPROGRESS) {
1424 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1426 if (put_user(sizeof(assoc_id), optlen))
1433 /* API 3.1.4 close() - UDP Style Syntax
1434 * Applications use close() to perform graceful shutdown (as described in
1435 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1436 * by a UDP-style socket.
1440 * ret = close(int sd);
1442 * sd - the socket descriptor of the associations to be closed.
1444 * To gracefully shutdown a specific association represented by the
1445 * UDP-style socket, an application should use the sendmsg() call,
1446 * passing no user data, but including the appropriate flag in the
1447 * ancillary data (see Section xxxx).
1449 * If sd in the close() call is a branched-off socket representing only
1450 * one association, the shutdown is performed on that association only.
1452 * 4.1.6 close() - TCP Style Syntax
1454 * Applications use close() to gracefully close down an association.
1458 * int close(int sd);
1460 * sd - the socket descriptor of the association to be closed.
1462 * After an application calls close() on a socket descriptor, no further
1463 * socket operations will succeed on that descriptor.
1465 * API 7.1.4 SO_LINGER
1467 * An application using the TCP-style socket can use this option to
1468 * perform the SCTP ABORT primitive. The linger option structure is:
1471 * int l_onoff; // option on/off
1472 * int l_linger; // linger time
1475 * To enable the option, set l_onoff to 1. If the l_linger value is set
1476 * to 0, calling close() is the same as the ABORT primitive. If the
1477 * value is set to a negative value, the setsockopt() call will return
1478 * an error. If the value is set to a positive value linger_time, the
1479 * close() can be blocked for at most linger_time ms. If the graceful
1480 * shutdown phase does not finish during this period, close() will
1481 * return but the graceful shutdown phase continues in the system.
1483 static void sctp_close(struct sock *sk, long timeout)
1485 struct net *net = sock_net(sk);
1486 struct sctp_endpoint *ep;
1487 struct sctp_association *asoc;
1488 struct list_head *pos, *temp;
1489 unsigned int data_was_unread;
1491 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1493 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1494 sk->sk_shutdown = SHUTDOWN_MASK;
1495 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1497 ep = sctp_sk(sk)->ep;
1499 /* Clean up any skbs sitting on the receive queue. */
1500 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1501 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1503 /* Walk all associations on an endpoint. */
1504 list_for_each_safe(pos, temp, &ep->asocs) {
1505 asoc = list_entry(pos, struct sctp_association, asocs);
1507 if (sctp_style(sk, TCP)) {
1508 /* A closed association can still be in the list if
1509 * it belongs to a TCP-style listening socket that is
1510 * not yet accepted. If so, free it. If not, send an
1511 * ABORT or SHUTDOWN based on the linger options.
1513 if (sctp_state(asoc, CLOSED)) {
1514 sctp_association_free(asoc);
1519 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1520 !skb_queue_empty(&asoc->ulpq.reasm) ||
1521 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1522 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1523 struct sctp_chunk *chunk;
1525 chunk = sctp_make_abort_user(asoc, NULL, 0);
1526 sctp_primitive_ABORT(net, asoc, chunk);
1528 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1531 /* On a TCP-style socket, block for at most linger_time if set. */
1532 if (sctp_style(sk, TCP) && timeout)
1533 sctp_wait_for_close(sk, timeout);
1535 /* This will run the backlog queue. */
1538 /* Supposedly, no process has access to the socket, but
1539 * the net layers still may.
1540 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1541 * held and that should be grabbed before socket lock.
1543 spin_lock_bh(&net->sctp.addr_wq_lock);
1544 bh_lock_sock_nested(sk);
1546 /* Hold the sock, since sk_common_release() will put sock_put()
1547 * and we have just a little more cleanup.
1550 sk_common_release(sk);
1553 spin_unlock_bh(&net->sctp.addr_wq_lock);
1557 SCTP_DBG_OBJCNT_DEC(sock);
1560 /* Handle EPIPE error. */
1561 static int sctp_error(struct sock *sk, int flags, int err)
1564 err = sock_error(sk) ? : -EPIPE;
1565 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1566 send_sig(SIGPIPE, current, 0);
1570 /* API 3.1.3 sendmsg() - UDP Style Syntax
1572 * An application uses sendmsg() and recvmsg() calls to transmit data to
1573 * and receive data from its peer.
1575 * ssize_t sendmsg(int socket, const struct msghdr *message,
1578 * socket - the socket descriptor of the endpoint.
1579 * message - pointer to the msghdr structure which contains a single
1580 * user message and possibly some ancillary data.
1582 * See Section 5 for complete description of the data
1585 * flags - flags sent or received with the user message, see Section
1586 * 5 for complete description of the flags.
1588 * Note: This function could use a rewrite especially when explicit
1589 * connect support comes in.
1591 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1593 static int sctp_msghdr_parse(const struct msghdr *msg,
1594 struct sctp_cmsgs *cmsgs);
1596 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1597 struct sctp_sndrcvinfo *srinfo,
1598 const struct msghdr *msg, size_t msg_len)
1603 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1606 if (msg_len > sk->sk_sndbuf)
1609 memset(cmsgs, 0, sizeof(*cmsgs));
1610 err = sctp_msghdr_parse(msg, cmsgs);
1612 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1616 memset(srinfo, 0, sizeof(*srinfo));
1617 if (cmsgs->srinfo) {
1618 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1619 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1620 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1621 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1622 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1623 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1627 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1628 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1629 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1630 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1631 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1634 if (cmsgs->prinfo) {
1635 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1636 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1637 cmsgs->prinfo->pr_policy);
1640 sflags = srinfo->sinfo_flags;
1641 if (!sflags && msg_len)
1644 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1647 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1648 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1651 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1657 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1658 struct sctp_cmsgs *cmsgs,
1659 union sctp_addr *daddr,
1660 struct sctp_transport **tp)
1662 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1663 struct net *net = sock_net(sk);
1664 struct sctp_association *asoc;
1665 enum sctp_scope scope;
1666 struct cmsghdr *cmsg;
1667 __be32 flowinfo = 0;
1673 if (sflags & (SCTP_EOF | SCTP_ABORT))
1676 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1677 sctp_sstate(sk, CLOSING)))
1678 return -EADDRNOTAVAIL;
1680 if (sctp_endpoint_is_peeled_off(ep, daddr))
1681 return -EADDRNOTAVAIL;
1683 if (!ep->base.bind_addr.port) {
1684 if (sctp_autobind(sk))
1687 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1688 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1692 scope = sctp_scope(daddr);
1694 /* Label connection socket for first association 1-to-many
1695 * style for client sequence socket()->sendmsg(). This
1696 * needs to be done before sctp_assoc_add_peer() as that will
1697 * set up the initial packet that needs to account for any
1698 * security ip options (CIPSO/CALIPSO) added to the packet.
1700 af = sctp_get_af_specific(daddr->sa.sa_family);
1703 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1704 (struct sockaddr *)daddr,
1709 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1713 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1719 struct sctp_initmsg *init = cmsgs->init;
1721 if (init->sinit_num_ostreams) {
1722 __u16 outcnt = init->sinit_num_ostreams;
1724 asoc->c.sinit_num_ostreams = outcnt;
1725 /* outcnt has been changed, need to re-init stream */
1726 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1732 if (init->sinit_max_instreams)
1733 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1735 if (init->sinit_max_attempts)
1736 asoc->max_init_attempts = init->sinit_max_attempts;
1738 if (init->sinit_max_init_timeo)
1739 asoc->max_init_timeo =
1740 msecs_to_jiffies(init->sinit_max_init_timeo);
1743 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1749 if (!cmsgs->addrs_msg)
1752 if (daddr->sa.sa_family == AF_INET6)
1753 flowinfo = daddr->v6.sin6_flowinfo;
1755 /* sendv addr list parse */
1756 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1757 struct sctp_transport *transport;
1758 struct sctp_association *old;
1759 union sctp_addr _daddr;
1762 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1763 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1764 cmsg->cmsg_type != SCTP_DSTADDRV6))
1768 memset(daddr, 0, sizeof(*daddr));
1769 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1770 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1771 if (dlen < sizeof(struct in_addr)) {
1776 dlen = sizeof(struct in_addr);
1777 daddr->v4.sin_family = AF_INET;
1778 daddr->v4.sin_port = htons(asoc->peer.port);
1779 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1781 if (dlen < sizeof(struct in6_addr)) {
1786 dlen = sizeof(struct in6_addr);
1787 daddr->v6.sin6_flowinfo = flowinfo;
1788 daddr->v6.sin6_family = AF_INET6;
1789 daddr->v6.sin6_port = htons(asoc->peer.port);
1790 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1792 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1796 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1797 if (old && old != asoc) {
1798 if (old->state >= SCTP_STATE_ESTABLISHED)
1805 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1806 err = -EADDRNOTAVAIL;
1810 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1821 sctp_association_free(asoc);
1825 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1826 __u16 sflags, struct msghdr *msg,
1829 struct sock *sk = asoc->base.sk;
1830 struct net *net = sock_net(sk);
1832 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1835 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1836 !sctp_state(asoc, ESTABLISHED))
1839 if (sflags & SCTP_EOF) {
1840 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1841 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1846 if (sflags & SCTP_ABORT) {
1847 struct sctp_chunk *chunk;
1849 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1853 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1854 sctp_primitive_ABORT(net, asoc, chunk);
1855 iov_iter_revert(&msg->msg_iter, msg_len);
1863 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1864 struct msghdr *msg, size_t msg_len,
1865 struct sctp_transport *transport,
1866 struct sctp_sndrcvinfo *sinfo)
1868 struct sock *sk = asoc->base.sk;
1869 struct sctp_sock *sp = sctp_sk(sk);
1870 struct net *net = sock_net(sk);
1871 struct sctp_datamsg *datamsg;
1872 bool wait_connect = false;
1873 struct sctp_chunk *chunk;
1877 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1882 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1883 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1888 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1893 if (asoc->pmtu_pending) {
1894 if (sp->param_flags & SPP_PMTUD_ENABLE)
1895 sctp_assoc_sync_pmtu(asoc);
1896 asoc->pmtu_pending = 0;
1899 if (sctp_wspace(asoc) < (int)msg_len)
1900 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1902 if (sk_under_memory_pressure(sk))
1905 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1906 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1907 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1912 if (sctp_state(asoc, CLOSED)) {
1913 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1917 if (asoc->ep->intl_enable) {
1918 timeo = sock_sndtimeo(sk, 0);
1919 err = sctp_wait_for_connect(asoc, &timeo);
1925 wait_connect = true;
1928 pr_debug("%s: we associated primitively\n", __func__);
1931 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1932 if (IS_ERR(datamsg)) {
1933 err = PTR_ERR(datamsg);
1937 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1939 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1940 sctp_chunk_hold(chunk);
1941 sctp_set_owner_w(chunk);
1942 chunk->transport = transport;
1945 err = sctp_primitive_SEND(net, asoc, datamsg);
1947 sctp_datamsg_free(datamsg);
1951 pr_debug("%s: we sent primitively\n", __func__);
1953 sctp_datamsg_put(datamsg);
1955 if (unlikely(wait_connect)) {
1956 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1957 sctp_wait_for_connect(asoc, &timeo);
1966 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1967 const struct msghdr *msg,
1968 struct sctp_cmsgs *cmsgs)
1970 union sctp_addr *daddr = NULL;
1973 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1974 int len = msg->msg_namelen;
1976 if (len > sizeof(*daddr))
1977 len = sizeof(*daddr);
1979 daddr = (union sctp_addr *)msg->msg_name;
1981 err = sctp_verify_addr(sk, daddr, len);
1983 return ERR_PTR(err);
1989 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1990 struct sctp_sndrcvinfo *sinfo,
1991 struct sctp_cmsgs *cmsgs)
1993 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1994 sinfo->sinfo_stream = asoc->default_stream;
1995 sinfo->sinfo_ppid = asoc->default_ppid;
1996 sinfo->sinfo_context = asoc->default_context;
1997 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
2000 sinfo->sinfo_flags = asoc->default_flags;
2003 if (!cmsgs->srinfo && !cmsgs->prinfo)
2004 sinfo->sinfo_timetolive = asoc->default_timetolive;
2006 if (cmsgs->authinfo) {
2007 /* Reuse sinfo_tsn to indicate that authinfo was set and
2008 * sinfo_ssn to save the keyid on tx path.
2010 sinfo->sinfo_tsn = 1;
2011 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2015 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2017 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2018 struct sctp_transport *transport = NULL;
2019 struct sctp_sndrcvinfo _sinfo, *sinfo;
2020 struct sctp_association *asoc, *tmp;
2021 struct sctp_cmsgs cmsgs;
2022 union sctp_addr *daddr;
2027 /* Parse and get snd_info */
2028 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2033 sflags = sinfo->sinfo_flags;
2035 /* Get daddr from msg */
2036 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2037 if (IS_ERR(daddr)) {
2038 err = PTR_ERR(daddr);
2044 /* SCTP_SENDALL process */
2045 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2046 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
2047 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2054 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2056 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2061 iov_iter_revert(&msg->msg_iter, err);
2067 /* Get and check or create asoc */
2069 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2071 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2076 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2081 asoc = transport->asoc;
2085 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2088 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2094 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2099 /* Update snd_info with the asoc */
2100 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2102 /* Send msg to the asoc */
2103 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2104 if (err < 0 && err != -ESRCH && new)
2105 sctp_association_free(asoc);
2110 return sctp_error(sk, msg->msg_flags, err);
2113 /* This is an extended version of skb_pull() that removes the data from the
2114 * start of a skb even when data is spread across the list of skb's in the
2115 * frag_list. len specifies the total amount of data that needs to be removed.
2116 * when 'len' bytes could be removed from the skb, it returns 0.
2117 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2118 * could not be removed.
2120 static int sctp_skb_pull(struct sk_buff *skb, int len)
2122 struct sk_buff *list;
2123 int skb_len = skb_headlen(skb);
2126 if (len <= skb_len) {
2127 __skb_pull(skb, len);
2131 __skb_pull(skb, skb_len);
2133 skb_walk_frags(skb, list) {
2134 rlen = sctp_skb_pull(list, len);
2135 skb->len -= (len-rlen);
2136 skb->data_len -= (len-rlen);
2147 /* API 3.1.3 recvmsg() - UDP Style Syntax
2149 * ssize_t recvmsg(int socket, struct msghdr *message,
2152 * socket - the socket descriptor of the endpoint.
2153 * message - pointer to the msghdr structure which contains a single
2154 * user message and possibly some ancillary data.
2156 * See Section 5 for complete description of the data
2159 * flags - flags sent or received with the user message, see Section
2160 * 5 for complete description of the flags.
2162 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2163 int noblock, int flags, int *addr_len)
2165 struct sctp_ulpevent *event = NULL;
2166 struct sctp_sock *sp = sctp_sk(sk);
2167 struct sk_buff *skb, *head_skb;
2172 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2173 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2178 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2179 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2184 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2188 /* Get the total length of the skb including any skb's in the
2197 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2199 event = sctp_skb2event(skb);
2204 if (event->chunk && event->chunk->head_skb)
2205 head_skb = event->chunk->head_skb;
2208 sock_recv_ts_and_drops(msg, sk, head_skb);
2209 if (sctp_ulpevent_is_notification(event)) {
2210 msg->msg_flags |= MSG_NOTIFICATION;
2211 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2213 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2216 /* Check if we allow SCTP_NXTINFO. */
2217 if (sp->recvnxtinfo)
2218 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2219 /* Check if we allow SCTP_RCVINFO. */
2220 if (sp->recvrcvinfo)
2221 sctp_ulpevent_read_rcvinfo(event, msg);
2222 /* Check if we allow SCTP_SNDRCVINFO. */
2223 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2224 sctp_ulpevent_read_sndrcvinfo(event, msg);
2228 /* If skb's length exceeds the user's buffer, update the skb and
2229 * push it back to the receive_queue so that the next call to
2230 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2232 if (skb_len > copied) {
2233 msg->msg_flags &= ~MSG_EOR;
2234 if (flags & MSG_PEEK)
2236 sctp_skb_pull(skb, copied);
2237 skb_queue_head(&sk->sk_receive_queue, skb);
2239 /* When only partial message is copied to the user, increase
2240 * rwnd by that amount. If all the data in the skb is read,
2241 * rwnd is updated when the event is freed.
2243 if (!sctp_ulpevent_is_notification(event))
2244 sctp_assoc_rwnd_increase(event->asoc, copied);
2246 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2247 (event->msg_flags & MSG_EOR))
2248 msg->msg_flags |= MSG_EOR;
2250 msg->msg_flags &= ~MSG_EOR;
2253 if (flags & MSG_PEEK) {
2254 /* Release the skb reference acquired after peeking the skb in
2255 * sctp_skb_recv_datagram().
2259 /* Free the event which includes releasing the reference to
2260 * the owner of the skb, freeing the skb and updating the
2263 sctp_ulpevent_free(event);
2270 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2272 * This option is a on/off flag. If enabled no SCTP message
2273 * fragmentation will be performed. Instead if a message being sent
2274 * exceeds the current PMTU size, the message will NOT be sent and
2275 * instead a error will be indicated to the user.
2277 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2278 char __user *optval,
2279 unsigned int optlen)
2283 if (optlen < sizeof(int))
2286 if (get_user(val, (int __user *)optval))
2289 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2294 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2295 unsigned int optlen)
2297 struct sctp_event_subscribe subscribe;
2298 __u8 *sn_type = (__u8 *)&subscribe;
2299 struct sctp_sock *sp = sctp_sk(sk);
2300 struct sctp_association *asoc;
2303 if (optlen > sizeof(struct sctp_event_subscribe))
2306 if (copy_from_user(&subscribe, optval, optlen))
2309 for (i = 0; i < optlen; i++)
2310 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2313 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2314 asoc->subscribe = sctp_sk(sk)->subscribe;
2316 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2317 * if there is no data to be sent or retransmit, the stack will
2318 * immediately send up this notification.
2320 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2321 struct sctp_ulpevent *event;
2323 asoc = sctp_id2assoc(sk, 0);
2324 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2325 event = sctp_ulpevent_make_sender_dry_event(asoc,
2326 GFP_USER | __GFP_NOWARN);
2330 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2337 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2339 * This socket option is applicable to the UDP-style socket only. When
2340 * set it will cause associations that are idle for more than the
2341 * specified number of seconds to automatically close. An association
2342 * being idle is defined an association that has NOT sent or received
2343 * user data. The special value of '0' indicates that no automatic
2344 * close of any associations should be performed. The option expects an
2345 * integer defining the number of seconds of idle time before an
2346 * association is closed.
2348 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2349 unsigned int optlen)
2351 struct sctp_sock *sp = sctp_sk(sk);
2352 struct net *net = sock_net(sk);
2354 /* Applicable to UDP-style socket only */
2355 if (sctp_style(sk, TCP))
2357 if (optlen != sizeof(int))
2359 if (copy_from_user(&sp->autoclose, optval, optlen))
2362 if (sp->autoclose > net->sctp.max_autoclose)
2363 sp->autoclose = net->sctp.max_autoclose;
2368 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2370 * Applications can enable or disable heartbeats for any peer address of
2371 * an association, modify an address's heartbeat interval, force a
2372 * heartbeat to be sent immediately, and adjust the address's maximum
2373 * number of retransmissions sent before an address is considered
2374 * unreachable. The following structure is used to access and modify an
2375 * address's parameters:
2377 * struct sctp_paddrparams {
2378 * sctp_assoc_t spp_assoc_id;
2379 * struct sockaddr_storage spp_address;
2380 * uint32_t spp_hbinterval;
2381 * uint16_t spp_pathmaxrxt;
2382 * uint32_t spp_pathmtu;
2383 * uint32_t spp_sackdelay;
2384 * uint32_t spp_flags;
2385 * uint32_t spp_ipv6_flowlabel;
2389 * spp_assoc_id - (one-to-many style socket) This is filled in the
2390 * application, and identifies the association for
2392 * spp_address - This specifies which address is of interest.
2393 * spp_hbinterval - This contains the value of the heartbeat interval,
2394 * in milliseconds. If a value of zero
2395 * is present in this field then no changes are to
2396 * be made to this parameter.
2397 * spp_pathmaxrxt - This contains the maximum number of
2398 * retransmissions before this address shall be
2399 * considered unreachable. If a value of zero
2400 * is present in this field then no changes are to
2401 * be made to this parameter.
2402 * spp_pathmtu - When Path MTU discovery is disabled the value
2403 * specified here will be the "fixed" path mtu.
2404 * Note that if the spp_address field is empty
2405 * then all associations on this address will
2406 * have this fixed path mtu set upon them.
2408 * spp_sackdelay - When delayed sack is enabled, this value specifies
2409 * the number of milliseconds that sacks will be delayed
2410 * for. This value will apply to all addresses of an
2411 * association if the spp_address field is empty. Note
2412 * also, that if delayed sack is enabled and this
2413 * value is set to 0, no change is made to the last
2414 * recorded delayed sack timer value.
2416 * spp_flags - These flags are used to control various features
2417 * on an association. The flag field may contain
2418 * zero or more of the following options.
2420 * SPP_HB_ENABLE - Enable heartbeats on the
2421 * specified address. Note that if the address
2422 * field is empty all addresses for the association
2423 * have heartbeats enabled upon them.
2425 * SPP_HB_DISABLE - Disable heartbeats on the
2426 * speicifed address. Note that if the address
2427 * field is empty all addresses for the association
2428 * will have their heartbeats disabled. Note also
2429 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2430 * mutually exclusive, only one of these two should
2431 * be specified. Enabling both fields will have
2432 * undetermined results.
2434 * SPP_HB_DEMAND - Request a user initiated heartbeat
2435 * to be made immediately.
2437 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2438 * heartbeat delayis to be set to the value of 0
2441 * SPP_PMTUD_ENABLE - This field will enable PMTU
2442 * discovery upon the specified address. Note that
2443 * if the address feild is empty then all addresses
2444 * on the association are effected.
2446 * SPP_PMTUD_DISABLE - This field will disable PMTU
2447 * discovery upon the specified address. Note that
2448 * if the address feild is empty then all addresses
2449 * on the association are effected. Not also that
2450 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2451 * exclusive. Enabling both will have undetermined
2454 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2455 * on delayed sack. The time specified in spp_sackdelay
2456 * is used to specify the sack delay for this address. Note
2457 * that if spp_address is empty then all addresses will
2458 * enable delayed sack and take on the sack delay
2459 * value specified in spp_sackdelay.
2460 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2461 * off delayed sack. If the spp_address field is blank then
2462 * delayed sack is disabled for the entire association. Note
2463 * also that this field is mutually exclusive to
2464 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2467 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2468 * setting of the IPV6 flow label value. The value is
2469 * contained in the spp_ipv6_flowlabel field.
2470 * Upon retrieval, this flag will be set to indicate that
2471 * the spp_ipv6_flowlabel field has a valid value returned.
2472 * If a specific destination address is set (in the
2473 * spp_address field), then the value returned is that of
2474 * the address. If just an association is specified (and
2475 * no address), then the association's default flow label
2476 * is returned. If neither an association nor a destination
2477 * is specified, then the socket's default flow label is
2478 * returned. For non-IPv6 sockets, this flag will be left
2481 * SPP_DSCP: Setting this flag enables the setting of the
2482 * Differentiated Services Code Point (DSCP) value
2483 * associated with either the association or a specific
2484 * address. The value is obtained in the spp_dscp field.
2485 * Upon retrieval, this flag will be set to indicate that
2486 * the spp_dscp field has a valid value returned. If a
2487 * specific destination address is set when called (in the
2488 * spp_address field), then that specific destination
2489 * address's DSCP value is returned. If just an association
2490 * is specified, then the association's default DSCP is
2491 * returned. If neither an association nor a destination is
2492 * specified, then the socket's default DSCP is returned.
2494 * spp_ipv6_flowlabel
2495 * - This field is used in conjunction with the
2496 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2497 * The 20 least significant bits are used for the flow
2498 * label. This setting has precedence over any IPv6-layer
2501 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2502 * and contains the DSCP. The 6 most significant bits are
2503 * used for the DSCP. This setting has precedence over any
2504 * IPv4- or IPv6- layer setting.
2506 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2507 struct sctp_transport *trans,
2508 struct sctp_association *asoc,
2509 struct sctp_sock *sp,
2512 int sackdelay_change)
2516 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2517 struct net *net = sock_net(trans->asoc->base.sk);
2519 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2524 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2525 * this field is ignored. Note also that a value of zero indicates
2526 * the current setting should be left unchanged.
2528 if (params->spp_flags & SPP_HB_ENABLE) {
2530 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2531 * set. This lets us use 0 value when this flag
2534 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2535 params->spp_hbinterval = 0;
2537 if (params->spp_hbinterval ||
2538 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2541 msecs_to_jiffies(params->spp_hbinterval);
2544 msecs_to_jiffies(params->spp_hbinterval);
2546 sp->hbinterval = params->spp_hbinterval;
2553 trans->param_flags =
2554 (trans->param_flags & ~SPP_HB) | hb_change;
2557 (asoc->param_flags & ~SPP_HB) | hb_change;
2560 (sp->param_flags & ~SPP_HB) | hb_change;
2564 /* When Path MTU discovery is disabled the value specified here will
2565 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2566 * include the flag SPP_PMTUD_DISABLE for this field to have any
2569 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2571 trans->pathmtu = params->spp_pathmtu;
2572 sctp_assoc_sync_pmtu(asoc);
2574 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2576 sp->pathmtu = params->spp_pathmtu;
2582 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2583 (params->spp_flags & SPP_PMTUD_ENABLE);
2584 trans->param_flags =
2585 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2587 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2588 sctp_assoc_sync_pmtu(asoc);
2592 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2595 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2599 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2600 * value of this field is ignored. Note also that a value of zero
2601 * indicates the current setting should be left unchanged.
2603 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2606 msecs_to_jiffies(params->spp_sackdelay);
2609 msecs_to_jiffies(params->spp_sackdelay);
2611 sp->sackdelay = params->spp_sackdelay;
2615 if (sackdelay_change) {
2617 trans->param_flags =
2618 (trans->param_flags & ~SPP_SACKDELAY) |
2622 (asoc->param_flags & ~SPP_SACKDELAY) |
2626 (sp->param_flags & ~SPP_SACKDELAY) |
2631 /* Note that a value of zero indicates the current setting should be
2634 if (params->spp_pathmaxrxt) {
2636 trans->pathmaxrxt = params->spp_pathmaxrxt;
2638 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2640 sp->pathmaxrxt = params->spp_pathmaxrxt;
2644 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2646 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2647 trans->flowlabel = params->spp_ipv6_flowlabel &
2648 SCTP_FLOWLABEL_VAL_MASK;
2649 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2652 struct sctp_transport *t;
2654 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2656 if (t->ipaddr.sa.sa_family != AF_INET6)
2658 t->flowlabel = params->spp_ipv6_flowlabel &
2659 SCTP_FLOWLABEL_VAL_MASK;
2660 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2662 asoc->flowlabel = params->spp_ipv6_flowlabel &
2663 SCTP_FLOWLABEL_VAL_MASK;
2664 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2665 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2666 sp->flowlabel = params->spp_ipv6_flowlabel &
2667 SCTP_FLOWLABEL_VAL_MASK;
2668 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2672 if (params->spp_flags & SPP_DSCP) {
2674 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2675 trans->dscp |= SCTP_DSCP_SET_MASK;
2677 struct sctp_transport *t;
2679 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2681 t->dscp = params->spp_dscp &
2683 t->dscp |= SCTP_DSCP_SET_MASK;
2685 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2686 asoc->dscp |= SCTP_DSCP_SET_MASK;
2688 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2689 sp->dscp |= SCTP_DSCP_SET_MASK;
2696 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2697 char __user *optval,
2698 unsigned int optlen)
2700 struct sctp_paddrparams params;
2701 struct sctp_transport *trans = NULL;
2702 struct sctp_association *asoc = NULL;
2703 struct sctp_sock *sp = sctp_sk(sk);
2705 int hb_change, pmtud_change, sackdelay_change;
2707 if (optlen == sizeof(params)) {
2708 if (copy_from_user(¶ms, optval, optlen))
2710 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2711 spp_ipv6_flowlabel), 4)) {
2712 if (copy_from_user(¶ms, optval, optlen))
2714 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2720 /* Validate flags and value parameters. */
2721 hb_change = params.spp_flags & SPP_HB;
2722 pmtud_change = params.spp_flags & SPP_PMTUD;
2723 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2725 if (hb_change == SPP_HB ||
2726 pmtud_change == SPP_PMTUD ||
2727 sackdelay_change == SPP_SACKDELAY ||
2728 params.spp_sackdelay > 500 ||
2729 (params.spp_pathmtu &&
2730 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2733 /* If an address other than INADDR_ANY is specified, and
2734 * no transport is found, then the request is invalid.
2736 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2737 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2738 params.spp_assoc_id);
2743 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2744 * socket is a one to many style socket, and an association
2745 * was not found, then the id was invalid.
2747 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2748 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2749 sctp_style(sk, UDP))
2752 /* Heartbeat demand can only be sent on a transport or
2753 * association, but not a socket.
2755 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2758 /* Process parameters. */
2759 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2760 hb_change, pmtud_change,
2766 /* If changes are for association, also apply parameters to each
2769 if (!trans && asoc) {
2770 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2772 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2773 hb_change, pmtud_change,
2781 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2783 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2786 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2788 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2791 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2792 struct sctp_association *asoc)
2794 struct sctp_transport *trans;
2796 if (params->sack_delay) {
2797 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2799 sctp_spp_sackdelay_enable(asoc->param_flags);
2801 if (params->sack_freq == 1) {
2803 sctp_spp_sackdelay_disable(asoc->param_flags);
2804 } else if (params->sack_freq > 1) {
2805 asoc->sackfreq = params->sack_freq;
2807 sctp_spp_sackdelay_enable(asoc->param_flags);
2810 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2812 if (params->sack_delay) {
2813 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2814 trans->param_flags =
2815 sctp_spp_sackdelay_enable(trans->param_flags);
2817 if (params->sack_freq == 1) {
2818 trans->param_flags =
2819 sctp_spp_sackdelay_disable(trans->param_flags);
2820 } else if (params->sack_freq > 1) {
2821 trans->sackfreq = params->sack_freq;
2822 trans->param_flags =
2823 sctp_spp_sackdelay_enable(trans->param_flags);
2829 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2831 * This option will effect the way delayed acks are performed. This
2832 * option allows you to get or set the delayed ack time, in
2833 * milliseconds. It also allows changing the delayed ack frequency.
2834 * Changing the frequency to 1 disables the delayed sack algorithm. If
2835 * the assoc_id is 0, then this sets or gets the endpoints default
2836 * values. If the assoc_id field is non-zero, then the set or get
2837 * effects the specified association for the one to many model (the
2838 * assoc_id field is ignored by the one to one model). Note that if
2839 * sack_delay or sack_freq are 0 when setting this option, then the
2840 * current values will remain unchanged.
2842 * struct sctp_sack_info {
2843 * sctp_assoc_t sack_assoc_id;
2844 * uint32_t sack_delay;
2845 * uint32_t sack_freq;
2848 * sack_assoc_id - This parameter, indicates which association the user
2849 * is performing an action upon. Note that if this field's value is
2850 * zero then the endpoints default value is changed (effecting future
2851 * associations only).
2853 * sack_delay - This parameter contains the number of milliseconds that
2854 * the user is requesting the delayed ACK timer be set to. Note that
2855 * this value is defined in the standard to be between 200 and 500
2858 * sack_freq - This parameter contains the number of packets that must
2859 * be received before a sack is sent without waiting for the delay
2860 * timer to expire. The default value for this is 2, setting this
2861 * value to 1 will disable the delayed sack algorithm.
2864 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2865 char __user *optval, unsigned int optlen)
2867 struct sctp_sock *sp = sctp_sk(sk);
2868 struct sctp_association *asoc;
2869 struct sctp_sack_info params;
2871 if (optlen == sizeof(struct sctp_sack_info)) {
2872 if (copy_from_user(¶ms, optval, optlen))
2875 if (params.sack_delay == 0 && params.sack_freq == 0)
2877 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2878 pr_warn_ratelimited(DEPRECATED
2880 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2881 "Use struct sctp_sack_info instead\n",
2882 current->comm, task_pid_nr(current));
2883 if (copy_from_user(¶ms, optval, optlen))
2886 if (params.sack_delay == 0)
2887 params.sack_freq = 1;
2889 params.sack_freq = 0;
2893 /* Validate value parameter. */
2894 if (params.sack_delay > 500)
2897 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2898 * socket is a one to many style socket, and an association
2899 * was not found, then the id was invalid.
2901 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2902 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2903 sctp_style(sk, UDP))
2907 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2912 if (sctp_style(sk, TCP))
2913 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2915 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2916 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2917 if (params.sack_delay) {
2918 sp->sackdelay = params.sack_delay;
2920 sctp_spp_sackdelay_enable(sp->param_flags);
2922 if (params.sack_freq == 1) {
2924 sctp_spp_sackdelay_disable(sp->param_flags);
2925 } else if (params.sack_freq > 1) {
2926 sp->sackfreq = params.sack_freq;
2928 sctp_spp_sackdelay_enable(sp->param_flags);
2932 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2933 params.sack_assoc_id == SCTP_ALL_ASSOC)
2934 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2935 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2940 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2942 * Applications can specify protocol parameters for the default association
2943 * initialization. The option name argument to setsockopt() and getsockopt()
2946 * Setting initialization parameters is effective only on an unconnected
2947 * socket (for UDP-style sockets only future associations are effected
2948 * by the change). With TCP-style sockets, this option is inherited by
2949 * sockets derived from a listener socket.
2951 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2953 struct sctp_initmsg sinit;
2954 struct sctp_sock *sp = sctp_sk(sk);
2956 if (optlen != sizeof(struct sctp_initmsg))
2958 if (copy_from_user(&sinit, optval, optlen))
2961 if (sinit.sinit_num_ostreams)
2962 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2963 if (sinit.sinit_max_instreams)
2964 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2965 if (sinit.sinit_max_attempts)
2966 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2967 if (sinit.sinit_max_init_timeo)
2968 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2974 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2976 * Applications that wish to use the sendto() system call may wish to
2977 * specify a default set of parameters that would normally be supplied
2978 * through the inclusion of ancillary data. This socket option allows
2979 * such an application to set the default sctp_sndrcvinfo structure.
2980 * The application that wishes to use this socket option simply passes
2981 * in to this call the sctp_sndrcvinfo structure defined in Section
2982 * 5.2.2) The input parameters accepted by this call include
2983 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2984 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2985 * to this call if the caller is using the UDP model.
2987 static int sctp_setsockopt_default_send_param(struct sock *sk,
2988 char __user *optval,
2989 unsigned int optlen)
2991 struct sctp_sock *sp = sctp_sk(sk);
2992 struct sctp_association *asoc;
2993 struct sctp_sndrcvinfo info;
2995 if (optlen != sizeof(info))
2997 if (copy_from_user(&info, optval, optlen))
2999 if (info.sinfo_flags &
3000 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3001 SCTP_ABORT | SCTP_EOF))
3004 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3005 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
3006 sctp_style(sk, UDP))
3010 asoc->default_stream = info.sinfo_stream;
3011 asoc->default_flags = info.sinfo_flags;
3012 asoc->default_ppid = info.sinfo_ppid;
3013 asoc->default_context = info.sinfo_context;
3014 asoc->default_timetolive = info.sinfo_timetolive;
3019 if (sctp_style(sk, TCP))
3020 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
3022 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
3023 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3024 sp->default_stream = info.sinfo_stream;
3025 sp->default_flags = info.sinfo_flags;
3026 sp->default_ppid = info.sinfo_ppid;
3027 sp->default_context = info.sinfo_context;
3028 sp->default_timetolive = info.sinfo_timetolive;
3031 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
3032 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3033 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3034 asoc->default_stream = info.sinfo_stream;
3035 asoc->default_flags = info.sinfo_flags;
3036 asoc->default_ppid = info.sinfo_ppid;
3037 asoc->default_context = info.sinfo_context;
3038 asoc->default_timetolive = info.sinfo_timetolive;
3045 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3046 * (SCTP_DEFAULT_SNDINFO)
3048 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3049 char __user *optval,
3050 unsigned int optlen)
3052 struct sctp_sock *sp = sctp_sk(sk);
3053 struct sctp_association *asoc;
3054 struct sctp_sndinfo info;
3056 if (optlen != sizeof(info))
3058 if (copy_from_user(&info, optval, optlen))
3060 if (info.snd_flags &
3061 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3062 SCTP_ABORT | SCTP_EOF))
3065 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3066 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3067 sctp_style(sk, UDP))
3071 asoc->default_stream = info.snd_sid;
3072 asoc->default_flags = info.snd_flags;
3073 asoc->default_ppid = info.snd_ppid;
3074 asoc->default_context = info.snd_context;
3079 if (sctp_style(sk, TCP))
3080 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3082 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3083 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3084 sp->default_stream = info.snd_sid;
3085 sp->default_flags = info.snd_flags;
3086 sp->default_ppid = info.snd_ppid;
3087 sp->default_context = info.snd_context;
3090 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3091 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3092 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3093 asoc->default_stream = info.snd_sid;
3094 asoc->default_flags = info.snd_flags;
3095 asoc->default_ppid = info.snd_ppid;
3096 asoc->default_context = info.snd_context;
3103 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3105 * Requests that the local SCTP stack use the enclosed peer address as
3106 * the association primary. The enclosed address must be one of the
3107 * association peer's addresses.
3109 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3110 unsigned int optlen)
3112 struct sctp_prim prim;
3113 struct sctp_transport *trans;
3117 if (optlen != sizeof(struct sctp_prim))
3120 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3123 /* Allow security module to validate address but need address len. */
3124 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3128 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3129 (struct sockaddr *)&prim.ssp_addr,
3134 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3138 sctp_assoc_set_primary(trans->asoc, trans);
3144 * 7.1.5 SCTP_NODELAY
3146 * Turn on/off any Nagle-like algorithm. This means that packets are
3147 * generally sent as soon as possible and no unnecessary delays are
3148 * introduced, at the cost of more packets in the network. Expects an
3149 * integer boolean flag.
3151 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3152 unsigned int optlen)
3156 if (optlen < sizeof(int))
3158 if (get_user(val, (int __user *)optval))
3161 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3167 * 7.1.1 SCTP_RTOINFO
3169 * The protocol parameters used to initialize and bound retransmission
3170 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3171 * and modify these parameters.
3172 * All parameters are time values, in milliseconds. A value of 0, when
3173 * modifying the parameters, indicates that the current value should not
3177 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3179 struct sctp_rtoinfo rtoinfo;
3180 struct sctp_association *asoc;
3181 unsigned long rto_min, rto_max;
3182 struct sctp_sock *sp = sctp_sk(sk);
3184 if (optlen != sizeof (struct sctp_rtoinfo))
3187 if (copy_from_user(&rtoinfo, optval, optlen))
3190 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3192 /* Set the values to the specific association */
3193 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3194 sctp_style(sk, UDP))
3197 rto_max = rtoinfo.srto_max;
3198 rto_min = rtoinfo.srto_min;
3201 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3203 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3206 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3208 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3210 if (rto_min > rto_max)
3214 if (rtoinfo.srto_initial != 0)
3216 msecs_to_jiffies(rtoinfo.srto_initial);
3217 asoc->rto_max = rto_max;
3218 asoc->rto_min = rto_min;
3220 /* If there is no association or the association-id = 0
3221 * set the values to the endpoint.
3223 if (rtoinfo.srto_initial != 0)
3224 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3225 sp->rtoinfo.srto_max = rto_max;
3226 sp->rtoinfo.srto_min = rto_min;
3234 * 7.1.2 SCTP_ASSOCINFO
3236 * This option is used to tune the maximum retransmission attempts
3237 * of the association.
3238 * Returns an error if the new association retransmission value is
3239 * greater than the sum of the retransmission value of the peer.
3240 * See [SCTP] for more information.
3243 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3246 struct sctp_assocparams assocparams;
3247 struct sctp_association *asoc;
3249 if (optlen != sizeof(struct sctp_assocparams))
3251 if (copy_from_user(&assocparams, optval, optlen))
3254 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3256 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3257 sctp_style(sk, UDP))
3260 /* Set the values to the specific association */
3262 if (assocparams.sasoc_asocmaxrxt != 0) {
3265 struct sctp_transport *peer_addr;
3267 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3269 path_sum += peer_addr->pathmaxrxt;
3273 /* Only validate asocmaxrxt if we have more than
3274 * one path/transport. We do this because path
3275 * retransmissions are only counted when we have more
3279 assocparams.sasoc_asocmaxrxt > path_sum)
3282 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3285 if (assocparams.sasoc_cookie_life != 0)
3286 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3288 /* Set the values to the endpoint */
3289 struct sctp_sock *sp = sctp_sk(sk);
3291 if (assocparams.sasoc_asocmaxrxt != 0)
3292 sp->assocparams.sasoc_asocmaxrxt =
3293 assocparams.sasoc_asocmaxrxt;
3294 if (assocparams.sasoc_cookie_life != 0)
3295 sp->assocparams.sasoc_cookie_life =
3296 assocparams.sasoc_cookie_life;
3302 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3304 * This socket option is a boolean flag which turns on or off mapped V4
3305 * addresses. If this option is turned on and the socket is type
3306 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3307 * If this option is turned off, then no mapping will be done of V4
3308 * addresses and a user will receive both PF_INET6 and PF_INET type
3309 * addresses on the socket.
3311 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3314 struct sctp_sock *sp = sctp_sk(sk);
3316 if (optlen < sizeof(int))
3318 if (get_user(val, (int __user *)optval))
3329 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3330 * This option will get or set the maximum size to put in any outgoing
3331 * SCTP DATA chunk. If a message is larger than this size it will be
3332 * fragmented by SCTP into the specified size. Note that the underlying
3333 * SCTP implementation may fragment into smaller sized chunks when the
3334 * PMTU of the underlying association is smaller than the value set by
3335 * the user. The default value for this option is '0' which indicates
3336 * the user is NOT limiting fragmentation and only the PMTU will effect
3337 * SCTP's choice of DATA chunk size. Note also that values set larger
3338 * than the maximum size of an IP datagram will effectively let SCTP
3339 * control fragmentation (i.e. the same as setting this option to 0).
3341 * The following structure is used to access and modify this parameter:
3343 * struct sctp_assoc_value {
3344 * sctp_assoc_t assoc_id;
3345 * uint32_t assoc_value;
3348 * assoc_id: This parameter is ignored for one-to-one style sockets.
3349 * For one-to-many style sockets this parameter indicates which
3350 * association the user is performing an action upon. Note that if
3351 * this field's value is zero then the endpoints default value is
3352 * changed (effecting future associations only).
3353 * assoc_value: This parameter specifies the maximum size in bytes.
3355 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3357 struct sctp_sock *sp = sctp_sk(sk);
3358 struct sctp_assoc_value params;
3359 struct sctp_association *asoc;
3362 if (optlen == sizeof(int)) {
3363 pr_warn_ratelimited(DEPRECATED
3365 "Use of int in maxseg socket option.\n"
3366 "Use struct sctp_assoc_value instead\n",
3367 current->comm, task_pid_nr(current));
3368 if (copy_from_user(&val, optval, optlen))
3370 params.assoc_id = SCTP_FUTURE_ASSOC;
3371 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3372 if (copy_from_user(¶ms, optval, optlen))
3374 val = params.assoc_value;
3379 asoc = sctp_id2assoc(sk, params.assoc_id);
3380 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3381 sctp_style(sk, UDP))
3385 int min_len, max_len;
3386 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3387 sizeof(struct sctp_data_chunk);
3389 min_len = sctp_min_frag_point(sp, datasize);
3390 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3392 if (val < min_len || val > max_len)
3397 asoc->user_frag = val;
3398 sctp_assoc_update_frag_point(asoc);
3400 sp->user_frag = val;
3408 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3410 * Requests that the peer mark the enclosed address as the association
3411 * primary. The enclosed address must be one of the association's
3412 * locally bound addresses. The following structure is used to make a
3413 * set primary request:
3415 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3416 unsigned int optlen)
3418 struct net *net = sock_net(sk);
3419 struct sctp_sock *sp;
3420 struct sctp_association *asoc = NULL;
3421 struct sctp_setpeerprim prim;
3422 struct sctp_chunk *chunk;
3428 if (!net->sctp.addip_enable)
3431 if (optlen != sizeof(struct sctp_setpeerprim))
3434 if (copy_from_user(&prim, optval, optlen))
3437 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3441 if (!asoc->peer.asconf_capable)
3444 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3447 if (!sctp_state(asoc, ESTABLISHED))
3450 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3454 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3455 return -EADDRNOTAVAIL;
3457 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3458 return -EADDRNOTAVAIL;
3460 /* Allow security module to validate address. */
3461 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3462 (struct sockaddr *)&prim.sspp_addr,
3467 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3468 chunk = sctp_make_asconf_set_prim(asoc,
3469 (union sctp_addr *)&prim.sspp_addr);
3473 err = sctp_send_asconf(asoc, chunk);
3475 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3480 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3481 unsigned int optlen)
3483 struct sctp_setadaptation adaptation;
3485 if (optlen != sizeof(struct sctp_setadaptation))
3487 if (copy_from_user(&adaptation, optval, optlen))
3490 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3496 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3498 * The context field in the sctp_sndrcvinfo structure is normally only
3499 * used when a failed message is retrieved holding the value that was
3500 * sent down on the actual send call. This option allows the setting of
3501 * a default context on an association basis that will be received on
3502 * reading messages from the peer. This is especially helpful in the
3503 * one-2-many model for an application to keep some reference to an
3504 * internal state machine that is processing messages on the
3505 * association. Note that the setting of this value only effects
3506 * received messages from the peer and does not effect the value that is
3507 * saved with outbound messages.
3509 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3510 unsigned int optlen)
3512 struct sctp_sock *sp = sctp_sk(sk);
3513 struct sctp_assoc_value params;
3514 struct sctp_association *asoc;
3516 if (optlen != sizeof(struct sctp_assoc_value))
3518 if (copy_from_user(¶ms, optval, optlen))
3521 asoc = sctp_id2assoc(sk, params.assoc_id);
3522 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3523 sctp_style(sk, UDP))
3527 asoc->default_rcv_context = params.assoc_value;
3532 if (sctp_style(sk, TCP))
3533 params.assoc_id = SCTP_FUTURE_ASSOC;
3535 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3536 params.assoc_id == SCTP_ALL_ASSOC)
3537 sp->default_rcv_context = params.assoc_value;
3539 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3540 params.assoc_id == SCTP_ALL_ASSOC)
3541 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3542 asoc->default_rcv_context = params.assoc_value;
3548 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3550 * This options will at a minimum specify if the implementation is doing
3551 * fragmented interleave. Fragmented interleave, for a one to many
3552 * socket, is when subsequent calls to receive a message may return
3553 * parts of messages from different associations. Some implementations
3554 * may allow you to turn this value on or off. If so, when turned off,
3555 * no fragment interleave will occur (which will cause a head of line
3556 * blocking amongst multiple associations sharing the same one to many
3557 * socket). When this option is turned on, then each receive call may
3558 * come from a different association (thus the user must receive data
3559 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3560 * association each receive belongs to.
3562 * This option takes a boolean value. A non-zero value indicates that
3563 * fragmented interleave is on. A value of zero indicates that
3564 * fragmented interleave is off.
3566 * Note that it is important that an implementation that allows this
3567 * option to be turned on, have it off by default. Otherwise an unaware
3568 * application using the one to many model may become confused and act
3571 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3572 char __user *optval,
3573 unsigned int optlen)
3577 if (optlen != sizeof(int))
3579 if (get_user(val, (int __user *)optval))
3582 sctp_sk(sk)->frag_interleave = !!val;
3584 if (!sctp_sk(sk)->frag_interleave)
3585 sctp_sk(sk)->ep->intl_enable = 0;
3591 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3592 * (SCTP_PARTIAL_DELIVERY_POINT)
3594 * This option will set or get the SCTP partial delivery point. This
3595 * point is the size of a message where the partial delivery API will be
3596 * invoked to help free up rwnd space for the peer. Setting this to a
3597 * lower value will cause partial deliveries to happen more often. The
3598 * calls argument is an integer that sets or gets the partial delivery
3599 * point. Note also that the call will fail if the user attempts to set
3600 * this value larger than the socket receive buffer size.
3602 * Note that any single message having a length smaller than or equal to
3603 * the SCTP partial delivery point will be delivered in one single read
3604 * call as long as the user provided buffer is large enough to hold the
3607 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3608 char __user *optval,
3609 unsigned int optlen)
3613 if (optlen != sizeof(u32))
3615 if (get_user(val, (int __user *)optval))
3618 /* Note: We double the receive buffer from what the user sets
3619 * it to be, also initial rwnd is based on rcvbuf/2.
3621 if (val > (sk->sk_rcvbuf >> 1))
3624 sctp_sk(sk)->pd_point = val;
3626 return 0; /* is this the right error code? */
3630 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3632 * This option will allow a user to change the maximum burst of packets
3633 * that can be emitted by this association. Note that the default value
3634 * is 4, and some implementations may restrict this setting so that it
3635 * can only be lowered.
3637 * NOTE: This text doesn't seem right. Do this on a socket basis with
3638 * future associations inheriting the socket value.
3640 static int sctp_setsockopt_maxburst(struct sock *sk,
3641 char __user *optval,
3642 unsigned int optlen)
3644 struct sctp_sock *sp = sctp_sk(sk);
3645 struct sctp_assoc_value params;
3646 struct sctp_association *asoc;
3648 if (optlen == sizeof(int)) {
3649 pr_warn_ratelimited(DEPRECATED
3651 "Use of int in max_burst socket option deprecated.\n"
3652 "Use struct sctp_assoc_value instead\n",
3653 current->comm, task_pid_nr(current));
3654 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3656 params.assoc_id = SCTP_FUTURE_ASSOC;
3657 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3658 if (copy_from_user(¶ms, optval, optlen))
3663 asoc = sctp_id2assoc(sk, params.assoc_id);
3664 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3665 sctp_style(sk, UDP))
3669 asoc->max_burst = params.assoc_value;
3674 if (sctp_style(sk, TCP))
3675 params.assoc_id = SCTP_FUTURE_ASSOC;
3677 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3678 params.assoc_id == SCTP_ALL_ASSOC)
3679 sp->max_burst = params.assoc_value;
3681 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3682 params.assoc_id == SCTP_ALL_ASSOC)
3683 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3684 asoc->max_burst = params.assoc_value;
3690 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3692 * This set option adds a chunk type that the user is requesting to be
3693 * received only in an authenticated way. Changes to the list of chunks
3694 * will only effect future associations on the socket.
3696 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3697 char __user *optval,
3698 unsigned int optlen)
3700 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3701 struct sctp_authchunk val;
3703 if (!ep->auth_enable)
3706 if (optlen != sizeof(struct sctp_authchunk))
3708 if (copy_from_user(&val, optval, optlen))
3711 switch (val.sauth_chunk) {
3713 case SCTP_CID_INIT_ACK:
3714 case SCTP_CID_SHUTDOWN_COMPLETE:
3719 /* add this chunk id to the endpoint */
3720 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3724 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3726 * This option gets or sets the list of HMAC algorithms that the local
3727 * endpoint requires the peer to use.
3729 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3730 char __user *optval,
3731 unsigned int optlen)
3733 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3734 struct sctp_hmacalgo *hmacs;
3738 if (!ep->auth_enable)
3741 if (optlen < sizeof(struct sctp_hmacalgo))
3743 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3744 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3746 hmacs = memdup_user(optval, optlen);
3748 return PTR_ERR(hmacs);
3750 idents = hmacs->shmac_num_idents;
3751 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3752 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3757 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3764 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3766 * This option will set a shared secret key which is used to build an
3767 * association shared key.
3769 static int sctp_setsockopt_auth_key(struct sock *sk,
3770 char __user *optval,
3771 unsigned int optlen)
3773 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3774 struct sctp_authkey *authkey;
3775 struct sctp_association *asoc;
3778 if (!ep->auth_enable)
3781 if (optlen <= sizeof(struct sctp_authkey))
3783 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3786 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3788 authkey = memdup_user(optval, optlen);
3789 if (IS_ERR(authkey))
3790 return PTR_ERR(authkey);
3792 if (authkey->sca_keylength > optlen - sizeof(*authkey))
3795 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3796 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3797 sctp_style(sk, UDP))
3801 ret = sctp_auth_set_key(ep, asoc, authkey);
3805 if (sctp_style(sk, TCP))
3806 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3808 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3809 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3810 ret = sctp_auth_set_key(ep, asoc, authkey);
3817 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3818 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3819 list_for_each_entry(asoc, &ep->asocs, asocs) {
3820 int res = sctp_auth_set_key(ep, asoc, authkey);
3833 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3835 * This option will get or set the active shared key to be used to build
3836 * the association shared key.
3838 static int sctp_setsockopt_active_key(struct sock *sk,
3839 char __user *optval,
3840 unsigned int optlen)
3842 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3843 struct sctp_association *asoc;
3844 struct sctp_authkeyid val;
3847 if (!ep->auth_enable)
3850 if (optlen != sizeof(struct sctp_authkeyid))
3852 if (copy_from_user(&val, optval, optlen))
3855 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3856 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3857 sctp_style(sk, UDP))
3861 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3863 if (sctp_style(sk, TCP))
3864 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3866 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3867 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3868 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3873 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3874 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3875 list_for_each_entry(asoc, &ep->asocs, asocs) {
3876 int res = sctp_auth_set_active_key(ep, asoc,
3877 val.scact_keynumber);
3888 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3890 * This set option will delete a shared secret key from use.
3892 static int sctp_setsockopt_del_key(struct sock *sk,
3893 char __user *optval,
3894 unsigned int optlen)
3896 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3897 struct sctp_association *asoc;
3898 struct sctp_authkeyid val;
3901 if (!ep->auth_enable)
3904 if (optlen != sizeof(struct sctp_authkeyid))
3906 if (copy_from_user(&val, optval, optlen))
3909 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3910 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3911 sctp_style(sk, UDP))
3915 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3917 if (sctp_style(sk, TCP))
3918 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3920 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3921 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3922 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3927 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3928 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3929 list_for_each_entry(asoc, &ep->asocs, asocs) {
3930 int res = sctp_auth_del_key_id(ep, asoc,
3931 val.scact_keynumber);
3942 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3944 * This set option will deactivate a shared secret key.
3946 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3947 unsigned int optlen)
3949 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3950 struct sctp_association *asoc;
3951 struct sctp_authkeyid val;
3954 if (!ep->auth_enable)
3957 if (optlen != sizeof(struct sctp_authkeyid))
3959 if (copy_from_user(&val, optval, optlen))
3962 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3963 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3964 sctp_style(sk, UDP))
3968 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3970 if (sctp_style(sk, TCP))
3971 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3973 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3974 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3975 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3980 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3981 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3982 list_for_each_entry(asoc, &ep->asocs, asocs) {
3983 int res = sctp_auth_deact_key_id(ep, asoc,
3984 val.scact_keynumber);
3995 * 8.1.23 SCTP_AUTO_ASCONF
3997 * This option will enable or disable the use of the automatic generation of
3998 * ASCONF chunks to add and delete addresses to an existing association. Note
3999 * that this option has two caveats namely: a) it only affects sockets that
4000 * are bound to all addresses available to the SCTP stack, and b) the system
4001 * administrator may have an overriding control that turns the ASCONF feature
4002 * off no matter what setting the socket option may have.
4003 * This option expects an integer boolean flag, where a non-zero value turns on
4004 * the option, and a zero value turns off the option.
4005 * Note. In this implementation, socket operation overrides default parameter
4006 * being set by sysctl as well as FreeBSD implementation
4008 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
4009 unsigned int optlen)
4012 struct sctp_sock *sp = sctp_sk(sk);
4014 if (optlen < sizeof(int))
4016 if (get_user(val, (int __user *)optval))
4018 if (!sctp_is_ep_boundall(sk) && val)
4020 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
4023 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
4024 if (val == 0 && sp->do_auto_asconf) {
4025 list_del(&sp->auto_asconf_list);
4026 sp->do_auto_asconf = 0;
4027 } else if (val && !sp->do_auto_asconf) {
4028 list_add_tail(&sp->auto_asconf_list,
4029 &sock_net(sk)->sctp.auto_asconf_splist);
4030 sp->do_auto_asconf = 1;
4032 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
4037 * SCTP_PEER_ADDR_THLDS
4039 * This option allows us to alter the partially failed threshold for one or all
4040 * transports in an association. See Section 6.1 of:
4041 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
4043 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
4044 char __user *optval,
4045 unsigned int optlen)
4047 struct sctp_paddrthlds val;
4048 struct sctp_transport *trans;
4049 struct sctp_association *asoc;
4051 if (optlen < sizeof(struct sctp_paddrthlds))
4053 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
4054 sizeof(struct sctp_paddrthlds)))
4057 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
4058 trans = sctp_addr_id2transport(sk, &val.spt_address,
4063 if (val.spt_pathmaxrxt)
4064 trans->pathmaxrxt = val.spt_pathmaxrxt;
4065 trans->pf_retrans = val.spt_pathpfthld;
4070 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4071 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4072 sctp_style(sk, UDP))
4076 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4078 if (val.spt_pathmaxrxt)
4079 trans->pathmaxrxt = val.spt_pathmaxrxt;
4080 trans->pf_retrans = val.spt_pathpfthld;
4083 if (val.spt_pathmaxrxt)
4084 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4085 asoc->pf_retrans = val.spt_pathpfthld;
4087 struct sctp_sock *sp = sctp_sk(sk);
4089 if (val.spt_pathmaxrxt)
4090 sp->pathmaxrxt = val.spt_pathmaxrxt;
4091 sp->pf_retrans = val.spt_pathpfthld;
4097 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4098 char __user *optval,
4099 unsigned int optlen)
4103 if (optlen < sizeof(int))
4105 if (get_user(val, (int __user *) optval))
4108 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4113 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4114 char __user *optval,
4115 unsigned int optlen)
4119 if (optlen < sizeof(int))
4121 if (get_user(val, (int __user *) optval))
4124 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4129 static int sctp_setsockopt_pr_supported(struct sock *sk,
4130 char __user *optval,
4131 unsigned int optlen)
4133 struct sctp_assoc_value params;
4134 struct sctp_association *asoc;
4136 if (optlen != sizeof(params))
4139 if (copy_from_user(¶ms, optval, optlen))
4142 asoc = sctp_id2assoc(sk, params.assoc_id);
4143 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4144 sctp_style(sk, UDP))
4147 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4152 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4153 char __user *optval,
4154 unsigned int optlen)
4156 struct sctp_sock *sp = sctp_sk(sk);
4157 struct sctp_default_prinfo info;
4158 struct sctp_association *asoc;
4159 int retval = -EINVAL;
4161 if (optlen != sizeof(info))
4164 if (copy_from_user(&info, optval, sizeof(info))) {
4169 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4172 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4175 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4176 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4177 sctp_style(sk, UDP))
4183 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4184 asoc->default_timetolive = info.pr_value;
4188 if (sctp_style(sk, TCP))
4189 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4191 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4192 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4193 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4194 sp->default_timetolive = info.pr_value;
4197 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4198 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4199 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4200 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4201 asoc->default_timetolive = info.pr_value;
4209 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4210 char __user *optval,
4211 unsigned int optlen)
4213 struct sctp_assoc_value params;
4214 struct sctp_association *asoc;
4215 int retval = -EINVAL;
4217 if (optlen != sizeof(params))
4220 if (copy_from_user(¶ms, optval, optlen)) {
4225 asoc = sctp_id2assoc(sk, params.assoc_id);
4226 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4227 sctp_style(sk, UDP))
4230 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4238 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4239 char __user *optval,
4240 unsigned int optlen)
4242 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4243 struct sctp_assoc_value params;
4244 struct sctp_association *asoc;
4245 int retval = -EINVAL;
4247 if (optlen != sizeof(params))
4250 if (copy_from_user(¶ms, optval, optlen)) {
4255 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4258 asoc = sctp_id2assoc(sk, params.assoc_id);
4259 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4260 sctp_style(sk, UDP))
4266 asoc->strreset_enable = params.assoc_value;
4270 if (sctp_style(sk, TCP))
4271 params.assoc_id = SCTP_FUTURE_ASSOC;
4273 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4274 params.assoc_id == SCTP_ALL_ASSOC)
4275 ep->strreset_enable = params.assoc_value;
4277 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4278 params.assoc_id == SCTP_ALL_ASSOC)
4279 list_for_each_entry(asoc, &ep->asocs, asocs)
4280 asoc->strreset_enable = params.assoc_value;
4286 static int sctp_setsockopt_reset_streams(struct sock *sk,
4287 char __user *optval,
4288 unsigned int optlen)
4290 struct sctp_reset_streams *params;
4291 struct sctp_association *asoc;
4292 int retval = -EINVAL;
4294 if (optlen < sizeof(*params))
4296 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4297 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4298 sizeof(__u16) * sizeof(*params));
4300 params = memdup_user(optval, optlen);
4302 return PTR_ERR(params);
4304 if (params->srs_number_streams * sizeof(__u16) >
4305 optlen - sizeof(*params))
4308 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4312 retval = sctp_send_reset_streams(asoc, params);
4319 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4320 char __user *optval,
4321 unsigned int optlen)
4323 struct sctp_association *asoc;
4324 sctp_assoc_t associd;
4325 int retval = -EINVAL;
4327 if (optlen != sizeof(associd))
4330 if (copy_from_user(&associd, optval, optlen)) {
4335 asoc = sctp_id2assoc(sk, associd);
4339 retval = sctp_send_reset_assoc(asoc);
4345 static int sctp_setsockopt_add_streams(struct sock *sk,
4346 char __user *optval,
4347 unsigned int optlen)
4349 struct sctp_association *asoc;
4350 struct sctp_add_streams params;
4351 int retval = -EINVAL;
4353 if (optlen != sizeof(params))
4356 if (copy_from_user(¶ms, optval, optlen)) {
4361 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4365 retval = sctp_send_add_streams(asoc, ¶ms);
4371 static int sctp_setsockopt_scheduler(struct sock *sk,
4372 char __user *optval,
4373 unsigned int optlen)
4375 struct sctp_sock *sp = sctp_sk(sk);
4376 struct sctp_association *asoc;
4377 struct sctp_assoc_value params;
4380 if (optlen < sizeof(params))
4383 optlen = sizeof(params);
4384 if (copy_from_user(¶ms, optval, optlen))
4387 if (params.assoc_value > SCTP_SS_MAX)
4390 asoc = sctp_id2assoc(sk, params.assoc_id);
4391 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4392 sctp_style(sk, UDP))
4396 return sctp_sched_set_sched(asoc, params.assoc_value);
4398 if (sctp_style(sk, TCP))
4399 params.assoc_id = SCTP_FUTURE_ASSOC;
4401 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4402 params.assoc_id == SCTP_ALL_ASSOC)
4403 sp->default_ss = params.assoc_value;
4405 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4406 params.assoc_id == SCTP_ALL_ASSOC) {
4407 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4408 int ret = sctp_sched_set_sched(asoc,
4409 params.assoc_value);
4419 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4420 char __user *optval,
4421 unsigned int optlen)
4423 struct sctp_stream_value params;
4424 struct sctp_association *asoc;
4425 int retval = -EINVAL;
4427 if (optlen < sizeof(params))
4430 optlen = sizeof(params);
4431 if (copy_from_user(¶ms, optval, optlen)) {
4436 asoc = sctp_id2assoc(sk, params.assoc_id);
4437 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4438 sctp_style(sk, UDP))
4442 retval = sctp_sched_set_value(asoc, params.stream_id,
4443 params.stream_value, GFP_KERNEL);
4449 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4450 int ret = sctp_sched_set_value(asoc, params.stream_id,
4451 params.stream_value, GFP_KERNEL);
4452 if (ret && !retval) /* try to return the 1st error. */
4460 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4461 char __user *optval,
4462 unsigned int optlen)
4464 struct sctp_sock *sp = sctp_sk(sk);
4465 struct sctp_assoc_value params;
4466 struct sctp_association *asoc;
4467 int retval = -EINVAL;
4469 if (optlen < sizeof(params))
4472 optlen = sizeof(params);
4473 if (copy_from_user(¶ms, optval, optlen)) {
4478 asoc = sctp_id2assoc(sk, params.assoc_id);
4479 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4480 sctp_style(sk, UDP))
4483 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4488 sp->ep->intl_enable = !!params.assoc_value;
4496 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4497 unsigned int optlen)
4501 if (!sctp_style(sk, TCP))
4504 if (sctp_sk(sk)->ep->base.bind_addr.port)
4507 if (optlen < sizeof(int))
4510 if (get_user(val, (int __user *)optval))
4513 sctp_sk(sk)->reuse = !!val;
4518 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4519 struct sctp_association *asoc)
4521 struct sctp_ulpevent *event;
4523 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4525 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4526 if (sctp_outq_is_empty(&asoc->outqueue)) {
4527 event = sctp_ulpevent_make_sender_dry_event(asoc,
4528 GFP_USER | __GFP_NOWARN);
4532 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4539 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4540 unsigned int optlen)
4542 struct sctp_sock *sp = sctp_sk(sk);
4543 struct sctp_association *asoc;
4544 struct sctp_event param;
4547 if (optlen < sizeof(param))
4550 optlen = sizeof(param);
4551 if (copy_from_user(¶m, optval, optlen))
4554 if (param.se_type < SCTP_SN_TYPE_BASE ||
4555 param.se_type > SCTP_SN_TYPE_MAX)
4558 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4559 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4560 sctp_style(sk, UDP))
4564 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4566 if (sctp_style(sk, TCP))
4567 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4569 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4570 param.se_assoc_id == SCTP_ALL_ASSOC)
4571 sctp_ulpevent_type_set(&sp->subscribe,
4572 param.se_type, param.se_on);
4574 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4575 param.se_assoc_id == SCTP_ALL_ASSOC) {
4576 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4577 int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4587 /* API 6.2 setsockopt(), getsockopt()
4589 * Applications use setsockopt() and getsockopt() to set or retrieve
4590 * socket options. Socket options are used to change the default
4591 * behavior of sockets calls. They are described in Section 7.
4595 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4596 * int __user *optlen);
4597 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4600 * sd - the socket descript.
4601 * level - set to IPPROTO_SCTP for all SCTP options.
4602 * optname - the option name.
4603 * optval - the buffer to store the value of the option.
4604 * optlen - the size of the buffer.
4606 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4607 char __user *optval, unsigned int optlen)
4611 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4613 /* I can hardly begin to describe how wrong this is. This is
4614 * so broken as to be worse than useless. The API draft
4615 * REALLY is NOT helpful here... I am not convinced that the
4616 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4617 * are at all well-founded.
4619 if (level != SOL_SCTP) {
4620 struct sctp_af *af = sctp_sk(sk)->pf->af;
4621 retval = af->setsockopt(sk, level, optname, optval, optlen);
4628 case SCTP_SOCKOPT_BINDX_ADD:
4629 /* 'optlen' is the size of the addresses buffer. */
4630 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4631 optlen, SCTP_BINDX_ADD_ADDR);
4634 case SCTP_SOCKOPT_BINDX_REM:
4635 /* 'optlen' is the size of the addresses buffer. */
4636 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4637 optlen, SCTP_BINDX_REM_ADDR);
4640 case SCTP_SOCKOPT_CONNECTX_OLD:
4641 /* 'optlen' is the size of the addresses buffer. */
4642 retval = sctp_setsockopt_connectx_old(sk,
4643 (struct sockaddr __user *)optval,
4647 case SCTP_SOCKOPT_CONNECTX:
4648 /* 'optlen' is the size of the addresses buffer. */
4649 retval = sctp_setsockopt_connectx(sk,
4650 (struct sockaddr __user *)optval,
4654 case SCTP_DISABLE_FRAGMENTS:
4655 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4659 retval = sctp_setsockopt_events(sk, optval, optlen);
4662 case SCTP_AUTOCLOSE:
4663 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4666 case SCTP_PEER_ADDR_PARAMS:
4667 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4670 case SCTP_DELAYED_SACK:
4671 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4673 case SCTP_PARTIAL_DELIVERY_POINT:
4674 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4678 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4680 case SCTP_DEFAULT_SEND_PARAM:
4681 retval = sctp_setsockopt_default_send_param(sk, optval,
4684 case SCTP_DEFAULT_SNDINFO:
4685 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4687 case SCTP_PRIMARY_ADDR:
4688 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4690 case SCTP_SET_PEER_PRIMARY_ADDR:
4691 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4694 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4697 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4699 case SCTP_ASSOCINFO:
4700 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4702 case SCTP_I_WANT_MAPPED_V4_ADDR:
4703 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4706 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4708 case SCTP_ADAPTATION_LAYER:
4709 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4712 retval = sctp_setsockopt_context(sk, optval, optlen);
4714 case SCTP_FRAGMENT_INTERLEAVE:
4715 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4717 case SCTP_MAX_BURST:
4718 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4720 case SCTP_AUTH_CHUNK:
4721 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4723 case SCTP_HMAC_IDENT:
4724 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4727 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4729 case SCTP_AUTH_ACTIVE_KEY:
4730 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4732 case SCTP_AUTH_DELETE_KEY:
4733 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4735 case SCTP_AUTH_DEACTIVATE_KEY:
4736 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4738 case SCTP_AUTO_ASCONF:
4739 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4741 case SCTP_PEER_ADDR_THLDS:
4742 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4744 case SCTP_RECVRCVINFO:
4745 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4747 case SCTP_RECVNXTINFO:
4748 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4750 case SCTP_PR_SUPPORTED:
4751 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4753 case SCTP_DEFAULT_PRINFO:
4754 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4756 case SCTP_RECONFIG_SUPPORTED:
4757 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4759 case SCTP_ENABLE_STREAM_RESET:
4760 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4762 case SCTP_RESET_STREAMS:
4763 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4765 case SCTP_RESET_ASSOC:
4766 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4768 case SCTP_ADD_STREAMS:
4769 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4771 case SCTP_STREAM_SCHEDULER:
4772 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4774 case SCTP_STREAM_SCHEDULER_VALUE:
4775 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4777 case SCTP_INTERLEAVING_SUPPORTED:
4778 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4781 case SCTP_REUSE_PORT:
4782 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4785 retval = sctp_setsockopt_event(sk, optval, optlen);
4788 retval = -ENOPROTOOPT;
4798 /* API 3.1.6 connect() - UDP Style Syntax
4800 * An application may use the connect() call in the UDP model to initiate an
4801 * association without sending data.
4805 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4807 * sd: the socket descriptor to have a new association added to.
4809 * nam: the address structure (either struct sockaddr_in or struct
4810 * sockaddr_in6 defined in RFC2553 [7]).
4812 * len: the size of the address.
4814 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4815 int addr_len, int flags)
4821 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4824 /* Validate addr_len before calling common connect/connectx routine. */
4825 af = sctp_get_af_specific(addr->sa_family);
4826 if (af && addr_len >= af->sockaddr_len)
4827 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4833 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4834 int addr_len, int flags)
4836 if (addr_len < sizeof(uaddr->sa_family))
4839 if (uaddr->sa_family == AF_UNSPEC)
4842 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4845 /* FIXME: Write comments. */
4846 static int sctp_disconnect(struct sock *sk, int flags)
4848 return -EOPNOTSUPP; /* STUB */
4851 /* 4.1.4 accept() - TCP Style Syntax
4853 * Applications use accept() call to remove an established SCTP
4854 * association from the accept queue of the endpoint. A new socket
4855 * descriptor will be returned from accept() to represent the newly
4856 * formed association.
4858 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4860 struct sctp_sock *sp;
4861 struct sctp_endpoint *ep;
4862 struct sock *newsk = NULL;
4863 struct sctp_association *asoc;
4872 if (!sctp_style(sk, TCP)) {
4873 error = -EOPNOTSUPP;
4877 if (!sctp_sstate(sk, LISTENING)) {
4882 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4884 error = sctp_wait_for_accept(sk, timeo);
4888 /* We treat the list of associations on the endpoint as the accept
4889 * queue and pick the first association on the list.
4891 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4893 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4899 /* Populate the fields of the newsk from the oldsk and migrate the
4900 * asoc to the newsk.
4902 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4904 sk_common_release(newsk);
4914 /* The SCTP ioctl handler. */
4915 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4922 * SEQPACKET-style sockets in LISTENING state are valid, for
4923 * SCTP, so only discard TCP-style sockets in LISTENING state.
4925 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4930 struct sk_buff *skb;
4931 unsigned int amount = 0;
4933 skb = skb_peek(&sk->sk_receive_queue);
4936 * We will only return the amount of this packet since
4937 * that is all that will be read.
4941 rc = put_user(amount, (int __user *)arg);
4953 /* This is the function which gets called during socket creation to
4954 * initialized the SCTP-specific portion of the sock.
4955 * The sock structure should already be zero-filled memory.
4957 static int sctp_init_sock(struct sock *sk)
4959 struct net *net = sock_net(sk);
4960 struct sctp_sock *sp;
4962 pr_debug("%s: sk:%p\n", __func__, sk);
4966 /* Initialize the SCTP per socket area. */
4967 switch (sk->sk_type) {
4968 case SOCK_SEQPACKET:
4969 sp->type = SCTP_SOCKET_UDP;
4972 sp->type = SCTP_SOCKET_TCP;
4975 return -ESOCKTNOSUPPORT;
4978 sk->sk_gso_type = SKB_GSO_SCTP;
4980 /* Initialize default send parameters. These parameters can be
4981 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4983 sp->default_stream = 0;
4984 sp->default_ppid = 0;
4985 sp->default_flags = 0;
4986 sp->default_context = 0;
4987 sp->default_timetolive = 0;
4989 sp->default_rcv_context = 0;
4990 sp->max_burst = net->sctp.max_burst;
4992 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4994 /* Initialize default setup parameters. These parameters
4995 * can be modified with the SCTP_INITMSG socket option or
4996 * overridden by the SCTP_INIT CMSG.
4998 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4999 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
5000 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5001 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5003 /* Initialize default RTO related parameters. These parameters can
5004 * be modified for with the SCTP_RTOINFO socket option.
5006 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5007 sp->rtoinfo.srto_max = net->sctp.rto_max;
5008 sp->rtoinfo.srto_min = net->sctp.rto_min;
5010 /* Initialize default association related parameters. These parameters
5011 * can be modified with the SCTP_ASSOCINFO socket option.
5013 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5014 sp->assocparams.sasoc_number_peer_destinations = 0;
5015 sp->assocparams.sasoc_peer_rwnd = 0;
5016 sp->assocparams.sasoc_local_rwnd = 0;
5017 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5019 /* Initialize default event subscriptions. By default, all the
5024 /* Default Peer Address Parameters. These defaults can
5025 * be modified via SCTP_PEER_ADDR_PARAMS
5027 sp->hbinterval = net->sctp.hb_interval;
5028 sp->pathmaxrxt = net->sctp.max_retrans_path;
5029 sp->pf_retrans = net->sctp.pf_retrans;
5030 sp->pathmtu = 0; /* allow default discovery */
5031 sp->sackdelay = net->sctp.sack_timeout;
5033 sp->param_flags = SPP_HB_ENABLE |
5035 SPP_SACKDELAY_ENABLE;
5036 sp->default_ss = SCTP_SS_DEFAULT;
5038 /* If enabled no SCTP message fragmentation will be performed.
5039 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5041 sp->disable_fragments = 0;
5043 /* Enable Nagle algorithm by default. */
5046 sp->recvrcvinfo = 0;
5047 sp->recvnxtinfo = 0;
5049 /* Enable by default. */
5052 /* Auto-close idle associations after the configured
5053 * number of seconds. A value of 0 disables this
5054 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5055 * for UDP-style sockets only.
5059 /* User specified fragmentation limit. */
5062 sp->adaptation_ind = 0;
5064 sp->pf = sctp_get_pf_specific(sk->sk_family);
5066 /* Control variables for partial data delivery. */
5067 atomic_set(&sp->pd_mode, 0);
5068 skb_queue_head_init(&sp->pd_lobby);
5069 sp->frag_interleave = 0;
5071 /* Create a per socket endpoint structure. Even if we
5072 * change the data structure relationships, this may still
5073 * be useful for storing pre-connect address information.
5075 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5081 sk->sk_destruct = sctp_destruct_sock;
5083 SCTP_DBG_OBJCNT_INC(sock);
5086 sk_sockets_allocated_inc(sk);
5087 sock_prot_inuse_add(net, sk->sk_prot, 1);
5089 /* Nothing can fail after this block, otherwise
5090 * sctp_destroy_sock() will be called without addr_wq_lock held
5092 if (net->sctp.default_auto_asconf) {
5093 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
5094 list_add_tail(&sp->auto_asconf_list,
5095 &net->sctp.auto_asconf_splist);
5096 sp->do_auto_asconf = 1;
5097 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5099 sp->do_auto_asconf = 0;
5107 /* Cleanup any SCTP per socket resources. Must be called with
5108 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5110 static void sctp_destroy_sock(struct sock *sk)
5112 struct sctp_sock *sp;
5114 pr_debug("%s: sk:%p\n", __func__, sk);
5116 /* Release our hold on the endpoint. */
5118 /* This could happen during socket init, thus we bail out
5119 * early, since the rest of the below is not setup either.
5124 if (sp->do_auto_asconf) {
5125 sp->do_auto_asconf = 0;
5126 list_del(&sp->auto_asconf_list);
5128 sctp_endpoint_free(sp->ep);
5130 sk_sockets_allocated_dec(sk);
5131 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5135 /* Triggered when there are no references on the socket anymore */
5136 static void sctp_destruct_sock(struct sock *sk)
5138 struct sctp_sock *sp = sctp_sk(sk);
5140 /* Free up the HMAC transform. */
5141 crypto_free_shash(sp->hmac);
5143 inet_sock_destruct(sk);
5146 /* API 4.1.7 shutdown() - TCP Style Syntax
5147 * int shutdown(int socket, int how);
5149 * sd - the socket descriptor of the association to be closed.
5150 * how - Specifies the type of shutdown. The values are
5153 * Disables further receive operations. No SCTP
5154 * protocol action is taken.
5156 * Disables further send operations, and initiates
5157 * the SCTP shutdown sequence.
5159 * Disables further send and receive operations
5160 * and initiates the SCTP shutdown sequence.
5162 static void sctp_shutdown(struct sock *sk, int how)
5164 struct net *net = sock_net(sk);
5165 struct sctp_endpoint *ep;
5167 if (!sctp_style(sk, TCP))
5170 ep = sctp_sk(sk)->ep;
5171 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5172 struct sctp_association *asoc;
5174 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5175 asoc = list_entry(ep->asocs.next,
5176 struct sctp_association, asocs);
5177 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5181 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5182 struct sctp_info *info)
5184 struct sctp_transport *prim;
5185 struct list_head *pos;
5188 memset(info, 0, sizeof(*info));
5190 struct sctp_sock *sp = sctp_sk(sk);
5192 info->sctpi_s_autoclose = sp->autoclose;
5193 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5194 info->sctpi_s_pd_point = sp->pd_point;
5195 info->sctpi_s_nodelay = sp->nodelay;
5196 info->sctpi_s_disable_fragments = sp->disable_fragments;
5197 info->sctpi_s_v4mapped = sp->v4mapped;
5198 info->sctpi_s_frag_interleave = sp->frag_interleave;
5199 info->sctpi_s_type = sp->type;
5204 info->sctpi_tag = asoc->c.my_vtag;
5205 info->sctpi_state = asoc->state;
5206 info->sctpi_rwnd = asoc->a_rwnd;
5207 info->sctpi_unackdata = asoc->unack_data;
5208 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5209 info->sctpi_instrms = asoc->stream.incnt;
5210 info->sctpi_outstrms = asoc->stream.outcnt;
5211 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5212 info->sctpi_inqueue++;
5213 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5214 info->sctpi_outqueue++;
5215 info->sctpi_overall_error = asoc->overall_error_count;
5216 info->sctpi_max_burst = asoc->max_burst;
5217 info->sctpi_maxseg = asoc->frag_point;
5218 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5219 info->sctpi_peer_tag = asoc->c.peer_vtag;
5221 mask = asoc->peer.ecn_capable << 1;
5222 mask = (mask | asoc->peer.ipv4_address) << 1;
5223 mask = (mask | asoc->peer.ipv6_address) << 1;
5224 mask = (mask | asoc->peer.hostname_address) << 1;
5225 mask = (mask | asoc->peer.asconf_capable) << 1;
5226 mask = (mask | asoc->peer.prsctp_capable) << 1;
5227 mask = (mask | asoc->peer.auth_capable);
5228 info->sctpi_peer_capable = mask;
5229 mask = asoc->peer.sack_needed << 1;
5230 mask = (mask | asoc->peer.sack_generation) << 1;
5231 mask = (mask | asoc->peer.zero_window_announced);
5232 info->sctpi_peer_sack = mask;
5234 info->sctpi_isacks = asoc->stats.isacks;
5235 info->sctpi_osacks = asoc->stats.osacks;
5236 info->sctpi_opackets = asoc->stats.opackets;
5237 info->sctpi_ipackets = asoc->stats.ipackets;
5238 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5239 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5240 info->sctpi_idupchunks = asoc->stats.idupchunks;
5241 info->sctpi_gapcnt = asoc->stats.gapcnt;
5242 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5243 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5244 info->sctpi_oodchunks = asoc->stats.oodchunks;
5245 info->sctpi_iodchunks = asoc->stats.iodchunks;
5246 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5247 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5249 prim = asoc->peer.primary_path;
5250 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5251 info->sctpi_p_state = prim->state;
5252 info->sctpi_p_cwnd = prim->cwnd;
5253 info->sctpi_p_srtt = prim->srtt;
5254 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5255 info->sctpi_p_hbinterval = prim->hbinterval;
5256 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5257 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5258 info->sctpi_p_ssthresh = prim->ssthresh;
5259 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5260 info->sctpi_p_flight_size = prim->flight_size;
5261 info->sctpi_p_error = prim->error_count;
5265 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5267 /* use callback to avoid exporting the core structure */
5268 void sctp_transport_walk_start(struct rhashtable_iter *iter)
5270 rhltable_walk_enter(&sctp_transport_hashtable, iter);
5272 rhashtable_walk_start(iter);
5275 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5277 rhashtable_walk_stop(iter);
5278 rhashtable_walk_exit(iter);
5281 struct sctp_transport *sctp_transport_get_next(struct net *net,
5282 struct rhashtable_iter *iter)
5284 struct sctp_transport *t;
5286 t = rhashtable_walk_next(iter);
5287 for (; t; t = rhashtable_walk_next(iter)) {
5289 if (PTR_ERR(t) == -EAGAIN)
5294 if (!sctp_transport_hold(t))
5297 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5298 t->asoc->peer.primary_path == t)
5301 sctp_transport_put(t);
5307 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5308 struct rhashtable_iter *iter,
5311 struct sctp_transport *t;
5314 return SEQ_START_TOKEN;
5316 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5319 sctp_transport_put(t);
5325 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5329 struct sctp_ep_common *epb;
5330 struct sctp_hashbucket *head;
5332 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5334 read_lock_bh(&head->lock);
5335 sctp_for_each_hentry(epb, &head->chain) {
5336 err = cb(sctp_ep(epb), p);
5340 read_unlock_bh(&head->lock);
5345 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5347 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5349 const union sctp_addr *laddr,
5350 const union sctp_addr *paddr, void *p)
5352 struct sctp_transport *transport;
5356 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5361 err = cb(transport, p);
5362 sctp_transport_put(transport);
5366 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5368 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5369 int (*cb_done)(struct sctp_transport *, void *),
5370 struct net *net, int *pos, void *p) {
5371 struct rhashtable_iter hti;
5372 struct sctp_transport *tsp;
5377 sctp_transport_walk_start(&hti);
5379 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5380 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5385 sctp_transport_put(tsp);
5387 sctp_transport_walk_stop(&hti);
5390 if (cb_done && !cb_done(tsp, p)) {
5392 sctp_transport_put(tsp);
5395 sctp_transport_put(tsp);
5400 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5402 /* 7.2.1 Association Status (SCTP_STATUS)
5404 * Applications can retrieve current status information about an
5405 * association, including association state, peer receiver window size,
5406 * number of unacked data chunks, and number of data chunks pending
5407 * receipt. This information is read-only.
5409 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5410 char __user *optval,
5413 struct sctp_status status;
5414 struct sctp_association *asoc = NULL;
5415 struct sctp_transport *transport;
5416 sctp_assoc_t associd;
5419 if (len < sizeof(status)) {
5424 len = sizeof(status);
5425 if (copy_from_user(&status, optval, len)) {
5430 associd = status.sstat_assoc_id;
5431 asoc = sctp_id2assoc(sk, associd);
5437 transport = asoc->peer.primary_path;
5439 status.sstat_assoc_id = sctp_assoc2id(asoc);
5440 status.sstat_state = sctp_assoc_to_state(asoc);
5441 status.sstat_rwnd = asoc->peer.rwnd;
5442 status.sstat_unackdata = asoc->unack_data;
5444 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5445 status.sstat_instrms = asoc->stream.incnt;
5446 status.sstat_outstrms = asoc->stream.outcnt;
5447 status.sstat_fragmentation_point = asoc->frag_point;
5448 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5449 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5450 transport->af_specific->sockaddr_len);
5451 /* Map ipv4 address into v4-mapped-on-v6 address. */
5452 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5453 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5454 status.sstat_primary.spinfo_state = transport->state;
5455 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5456 status.sstat_primary.spinfo_srtt = transport->srtt;
5457 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5458 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5460 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5461 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5463 if (put_user(len, optlen)) {
5468 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5469 __func__, len, status.sstat_state, status.sstat_rwnd,
5470 status.sstat_assoc_id);
5472 if (copy_to_user(optval, &status, len)) {
5482 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5484 * Applications can retrieve information about a specific peer address
5485 * of an association, including its reachability state, congestion
5486 * window, and retransmission timer values. This information is
5489 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5490 char __user *optval,
5493 struct sctp_paddrinfo pinfo;
5494 struct sctp_transport *transport;
5497 if (len < sizeof(pinfo)) {
5502 len = sizeof(pinfo);
5503 if (copy_from_user(&pinfo, optval, len)) {
5508 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5509 pinfo.spinfo_assoc_id);
5513 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5514 pinfo.spinfo_state = transport->state;
5515 pinfo.spinfo_cwnd = transport->cwnd;
5516 pinfo.spinfo_srtt = transport->srtt;
5517 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5518 pinfo.spinfo_mtu = transport->pathmtu;
5520 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5521 pinfo.spinfo_state = SCTP_ACTIVE;
5523 if (put_user(len, optlen)) {
5528 if (copy_to_user(optval, &pinfo, len)) {
5537 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5539 * This option is a on/off flag. If enabled no SCTP message
5540 * fragmentation will be performed. Instead if a message being sent
5541 * exceeds the current PMTU size, the message will NOT be sent and
5542 * instead a error will be indicated to the user.
5544 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5545 char __user *optval, int __user *optlen)
5549 if (len < sizeof(int))
5553 val = (sctp_sk(sk)->disable_fragments == 1);
5554 if (put_user(len, optlen))
5556 if (copy_to_user(optval, &val, len))
5561 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5563 * This socket option is used to specify various notifications and
5564 * ancillary data the user wishes to receive.
5566 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5569 struct sctp_event_subscribe subscribe;
5570 __u8 *sn_type = (__u8 *)&subscribe;
5575 if (len > sizeof(struct sctp_event_subscribe))
5576 len = sizeof(struct sctp_event_subscribe);
5577 if (put_user(len, optlen))
5580 for (i = 0; i < len; i++)
5581 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5582 SCTP_SN_TYPE_BASE + i);
5584 if (copy_to_user(optval, &subscribe, len))
5590 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5592 * This socket option is applicable to the UDP-style socket only. When
5593 * set it will cause associations that are idle for more than the
5594 * specified number of seconds to automatically close. An association
5595 * being idle is defined an association that has NOT sent or received
5596 * user data. The special value of '0' indicates that no automatic
5597 * close of any associations should be performed. The option expects an
5598 * integer defining the number of seconds of idle time before an
5599 * association is closed.
5601 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5603 /* Applicable to UDP-style socket only */
5604 if (sctp_style(sk, TCP))
5606 if (len < sizeof(int))
5609 if (put_user(len, optlen))
5611 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5616 /* Helper routine to branch off an association to a new socket. */
5617 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5619 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5620 struct sctp_sock *sp = sctp_sk(sk);
5621 struct socket *sock;
5624 /* Do not peel off from one netns to another one. */
5625 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5631 /* An association cannot be branched off from an already peeled-off
5632 * socket, nor is this supported for tcp style sockets.
5634 if (!sctp_style(sk, UDP))
5637 /* Create a new socket. */
5638 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5642 sctp_copy_sock(sock->sk, sk, asoc);
5644 /* Make peeled-off sockets more like 1-1 accepted sockets.
5645 * Set the daddr and initialize id to something more random and also
5646 * copy over any ip options.
5648 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5649 sp->pf->copy_ip_options(sk, sock->sk);
5651 /* Populate the fields of the newsk from the oldsk and migrate the
5652 * asoc to the newsk.
5654 err = sctp_sock_migrate(sk, sock->sk, asoc,
5655 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5665 EXPORT_SYMBOL(sctp_do_peeloff);
5667 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5668 struct file **newfile, unsigned flags)
5670 struct socket *newsock;
5673 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5677 /* Map the socket to an unused fd that can be returned to the user. */
5678 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5680 sock_release(newsock);
5684 *newfile = sock_alloc_file(newsock, 0, NULL);
5685 if (IS_ERR(*newfile)) {
5686 put_unused_fd(retval);
5687 retval = PTR_ERR(*newfile);
5692 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5695 peeloff->sd = retval;
5697 if (flags & SOCK_NONBLOCK)
5698 (*newfile)->f_flags |= O_NONBLOCK;
5703 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5705 sctp_peeloff_arg_t peeloff;
5706 struct file *newfile = NULL;
5709 if (len < sizeof(sctp_peeloff_arg_t))
5711 len = sizeof(sctp_peeloff_arg_t);
5712 if (copy_from_user(&peeloff, optval, len))
5715 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5719 /* Return the fd mapped to the new socket. */
5720 if (put_user(len, optlen)) {
5722 put_unused_fd(retval);
5726 if (copy_to_user(optval, &peeloff, len)) {
5728 put_unused_fd(retval);
5731 fd_install(retval, newfile);
5736 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5737 char __user *optval, int __user *optlen)
5739 sctp_peeloff_flags_arg_t peeloff;
5740 struct file *newfile = NULL;
5743 if (len < sizeof(sctp_peeloff_flags_arg_t))
5745 len = sizeof(sctp_peeloff_flags_arg_t);
5746 if (copy_from_user(&peeloff, optval, len))
5749 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5750 &newfile, peeloff.flags);
5754 /* Return the fd mapped to the new socket. */
5755 if (put_user(len, optlen)) {
5757 put_unused_fd(retval);
5761 if (copy_to_user(optval, &peeloff, len)) {
5763 put_unused_fd(retval);
5766 fd_install(retval, newfile);
5771 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5773 * Applications can enable or disable heartbeats for any peer address of
5774 * an association, modify an address's heartbeat interval, force a
5775 * heartbeat to be sent immediately, and adjust the address's maximum
5776 * number of retransmissions sent before an address is considered
5777 * unreachable. The following structure is used to access and modify an
5778 * address's parameters:
5780 * struct sctp_paddrparams {
5781 * sctp_assoc_t spp_assoc_id;
5782 * struct sockaddr_storage spp_address;
5783 * uint32_t spp_hbinterval;
5784 * uint16_t spp_pathmaxrxt;
5785 * uint32_t spp_pathmtu;
5786 * uint32_t spp_sackdelay;
5787 * uint32_t spp_flags;
5790 * spp_assoc_id - (one-to-many style socket) This is filled in the
5791 * application, and identifies the association for
5793 * spp_address - This specifies which address is of interest.
5794 * spp_hbinterval - This contains the value of the heartbeat interval,
5795 * in milliseconds. If a value of zero
5796 * is present in this field then no changes are to
5797 * be made to this parameter.
5798 * spp_pathmaxrxt - This contains the maximum number of
5799 * retransmissions before this address shall be
5800 * considered unreachable. If a value of zero
5801 * is present in this field then no changes are to
5802 * be made to this parameter.
5803 * spp_pathmtu - When Path MTU discovery is disabled the value
5804 * specified here will be the "fixed" path mtu.
5805 * Note that if the spp_address field is empty
5806 * then all associations on this address will
5807 * have this fixed path mtu set upon them.
5809 * spp_sackdelay - When delayed sack is enabled, this value specifies
5810 * the number of milliseconds that sacks will be delayed
5811 * for. This value will apply to all addresses of an
5812 * association if the spp_address field is empty. Note
5813 * also, that if delayed sack is enabled and this
5814 * value is set to 0, no change is made to the last
5815 * recorded delayed sack timer value.
5817 * spp_flags - These flags are used to control various features
5818 * on an association. The flag field may contain
5819 * zero or more of the following options.
5821 * SPP_HB_ENABLE - Enable heartbeats on the
5822 * specified address. Note that if the address
5823 * field is empty all addresses for the association
5824 * have heartbeats enabled upon them.
5826 * SPP_HB_DISABLE - Disable heartbeats on the
5827 * speicifed address. Note that if the address
5828 * field is empty all addresses for the association
5829 * will have their heartbeats disabled. Note also
5830 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5831 * mutually exclusive, only one of these two should
5832 * be specified. Enabling both fields will have
5833 * undetermined results.
5835 * SPP_HB_DEMAND - Request a user initiated heartbeat
5836 * to be made immediately.
5838 * SPP_PMTUD_ENABLE - This field will enable PMTU
5839 * discovery upon the specified address. Note that
5840 * if the address feild is empty then all addresses
5841 * on the association are effected.
5843 * SPP_PMTUD_DISABLE - This field will disable PMTU
5844 * discovery upon the specified address. Note that
5845 * if the address feild is empty then all addresses
5846 * on the association are effected. Not also that
5847 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5848 * exclusive. Enabling both will have undetermined
5851 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5852 * on delayed sack. The time specified in spp_sackdelay
5853 * is used to specify the sack delay for this address. Note
5854 * that if spp_address is empty then all addresses will
5855 * enable delayed sack and take on the sack delay
5856 * value specified in spp_sackdelay.
5857 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5858 * off delayed sack. If the spp_address field is blank then
5859 * delayed sack is disabled for the entire association. Note
5860 * also that this field is mutually exclusive to
5861 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5864 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5865 * setting of the IPV6 flow label value. The value is
5866 * contained in the spp_ipv6_flowlabel field.
5867 * Upon retrieval, this flag will be set to indicate that
5868 * the spp_ipv6_flowlabel field has a valid value returned.
5869 * If a specific destination address is set (in the
5870 * spp_address field), then the value returned is that of
5871 * the address. If just an association is specified (and
5872 * no address), then the association's default flow label
5873 * is returned. If neither an association nor a destination
5874 * is specified, then the socket's default flow label is
5875 * returned. For non-IPv6 sockets, this flag will be left
5878 * SPP_DSCP: Setting this flag enables the setting of the
5879 * Differentiated Services Code Point (DSCP) value
5880 * associated with either the association or a specific
5881 * address. The value is obtained in the spp_dscp field.
5882 * Upon retrieval, this flag will be set to indicate that
5883 * the spp_dscp field has a valid value returned. If a
5884 * specific destination address is set when called (in the
5885 * spp_address field), then that specific destination
5886 * address's DSCP value is returned. If just an association
5887 * is specified, then the association's default DSCP is
5888 * returned. If neither an association nor a destination is
5889 * specified, then the socket's default DSCP is returned.
5891 * spp_ipv6_flowlabel
5892 * - This field is used in conjunction with the
5893 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5894 * The 20 least significant bits are used for the flow
5895 * label. This setting has precedence over any IPv6-layer
5898 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5899 * and contains the DSCP. The 6 most significant bits are
5900 * used for the DSCP. This setting has precedence over any
5901 * IPv4- or IPv6- layer setting.
5903 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5904 char __user *optval, int __user *optlen)
5906 struct sctp_paddrparams params;
5907 struct sctp_transport *trans = NULL;
5908 struct sctp_association *asoc = NULL;
5909 struct sctp_sock *sp = sctp_sk(sk);
5911 if (len >= sizeof(params))
5912 len = sizeof(params);
5913 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5914 spp_ipv6_flowlabel), 4))
5915 len = ALIGN(offsetof(struct sctp_paddrparams,
5916 spp_ipv6_flowlabel), 4);
5920 if (copy_from_user(¶ms, optval, len))
5923 /* If an address other than INADDR_ANY is specified, and
5924 * no transport is found, then the request is invalid.
5926 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5927 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5928 params.spp_assoc_id);
5930 pr_debug("%s: failed no transport\n", __func__);
5935 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5936 * socket is a one to many style socket, and an association
5937 * was not found, then the id was invalid.
5939 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5940 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5941 sctp_style(sk, UDP)) {
5942 pr_debug("%s: failed no association\n", __func__);
5947 /* Fetch transport values. */
5948 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5949 params.spp_pathmtu = trans->pathmtu;
5950 params.spp_pathmaxrxt = trans->pathmaxrxt;
5951 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5953 /*draft-11 doesn't say what to return in spp_flags*/
5954 params.spp_flags = trans->param_flags;
5955 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5956 params.spp_ipv6_flowlabel = trans->flowlabel &
5957 SCTP_FLOWLABEL_VAL_MASK;
5958 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5960 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5961 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5962 params.spp_flags |= SPP_DSCP;
5965 /* Fetch association values. */
5966 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5967 params.spp_pathmtu = asoc->pathmtu;
5968 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5969 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5971 /*draft-11 doesn't say what to return in spp_flags*/
5972 params.spp_flags = asoc->param_flags;
5973 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5974 params.spp_ipv6_flowlabel = asoc->flowlabel &
5975 SCTP_FLOWLABEL_VAL_MASK;
5976 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5978 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5979 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5980 params.spp_flags |= SPP_DSCP;
5983 /* Fetch socket values. */
5984 params.spp_hbinterval = sp->hbinterval;
5985 params.spp_pathmtu = sp->pathmtu;
5986 params.spp_sackdelay = sp->sackdelay;
5987 params.spp_pathmaxrxt = sp->pathmaxrxt;
5989 /*draft-11 doesn't say what to return in spp_flags*/
5990 params.spp_flags = sp->param_flags;
5991 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5992 params.spp_ipv6_flowlabel = sp->flowlabel &
5993 SCTP_FLOWLABEL_VAL_MASK;
5994 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5996 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5997 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5998 params.spp_flags |= SPP_DSCP;
6002 if (copy_to_user(optval, ¶ms, len))
6005 if (put_user(len, optlen))
6012 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6014 * This option will effect the way delayed acks are performed. This
6015 * option allows you to get or set the delayed ack time, in
6016 * milliseconds. It also allows changing the delayed ack frequency.
6017 * Changing the frequency to 1 disables the delayed sack algorithm. If
6018 * the assoc_id is 0, then this sets or gets the endpoints default
6019 * values. If the assoc_id field is non-zero, then the set or get
6020 * effects the specified association for the one to many model (the
6021 * assoc_id field is ignored by the one to one model). Note that if
6022 * sack_delay or sack_freq are 0 when setting this option, then the
6023 * current values will remain unchanged.
6025 * struct sctp_sack_info {
6026 * sctp_assoc_t sack_assoc_id;
6027 * uint32_t sack_delay;
6028 * uint32_t sack_freq;
6031 * sack_assoc_id - This parameter, indicates which association the user
6032 * is performing an action upon. Note that if this field's value is
6033 * zero then the endpoints default value is changed (effecting future
6034 * associations only).
6036 * sack_delay - This parameter contains the number of milliseconds that
6037 * the user is requesting the delayed ACK timer be set to. Note that
6038 * this value is defined in the standard to be between 200 and 500
6041 * sack_freq - This parameter contains the number of packets that must
6042 * be received before a sack is sent without waiting for the delay
6043 * timer to expire. The default value for this is 2, setting this
6044 * value to 1 will disable the delayed sack algorithm.
6046 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6047 char __user *optval,
6050 struct sctp_sack_info params;
6051 struct sctp_association *asoc = NULL;
6052 struct sctp_sock *sp = sctp_sk(sk);
6054 if (len >= sizeof(struct sctp_sack_info)) {
6055 len = sizeof(struct sctp_sack_info);
6057 if (copy_from_user(¶ms, optval, len))
6059 } else if (len == sizeof(struct sctp_assoc_value)) {
6060 pr_warn_ratelimited(DEPRECATED
6062 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6063 "Use struct sctp_sack_info instead\n",
6064 current->comm, task_pid_nr(current));
6065 if (copy_from_user(¶ms, optval, len))
6070 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6071 * socket is a one to many style socket, and an association
6072 * was not found, then the id was invalid.
6074 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6075 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6076 sctp_style(sk, UDP))
6080 /* Fetch association values. */
6081 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6082 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6083 params.sack_freq = asoc->sackfreq;
6086 params.sack_delay = 0;
6087 params.sack_freq = 1;
6090 /* Fetch socket values. */
6091 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6092 params.sack_delay = sp->sackdelay;
6093 params.sack_freq = sp->sackfreq;
6095 params.sack_delay = 0;
6096 params.sack_freq = 1;
6100 if (copy_to_user(optval, ¶ms, len))
6103 if (put_user(len, optlen))
6109 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6111 * Applications can specify protocol parameters for the default association
6112 * initialization. The option name argument to setsockopt() and getsockopt()
6115 * Setting initialization parameters is effective only on an unconnected
6116 * socket (for UDP-style sockets only future associations are effected
6117 * by the change). With TCP-style sockets, this option is inherited by
6118 * sockets derived from a listener socket.
6120 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6122 if (len < sizeof(struct sctp_initmsg))
6124 len = sizeof(struct sctp_initmsg);
6125 if (put_user(len, optlen))
6127 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6133 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6134 char __user *optval, int __user *optlen)
6136 struct sctp_association *asoc;
6138 struct sctp_getaddrs getaddrs;
6139 struct sctp_transport *from;
6141 union sctp_addr temp;
6142 struct sctp_sock *sp = sctp_sk(sk);
6147 if (len < sizeof(struct sctp_getaddrs))
6150 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6153 /* For UDP-style sockets, id specifies the association to query. */
6154 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6158 to = optval + offsetof(struct sctp_getaddrs, addrs);
6159 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6161 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6163 memcpy(&temp, &from->ipaddr, sizeof(temp));
6164 addrlen = sctp_get_pf_specific(sk->sk_family)
6165 ->addr_to_user(sp, &temp);
6166 if (space_left < addrlen)
6168 if (copy_to_user(to, &temp, addrlen))
6172 space_left -= addrlen;
6175 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6177 bytes_copied = ((char __user *)to) - optval;
6178 if (put_user(bytes_copied, optlen))
6184 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6185 size_t space_left, int *bytes_copied)
6187 struct sctp_sockaddr_entry *addr;
6188 union sctp_addr temp;
6191 struct net *net = sock_net(sk);
6194 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6198 if ((PF_INET == sk->sk_family) &&
6199 (AF_INET6 == addr->a.sa.sa_family))
6201 if ((PF_INET6 == sk->sk_family) &&
6202 inet_v6_ipv6only(sk) &&
6203 (AF_INET == addr->a.sa.sa_family))
6205 memcpy(&temp, &addr->a, sizeof(temp));
6206 if (!temp.v4.sin_port)
6207 temp.v4.sin_port = htons(port);
6209 addrlen = sctp_get_pf_specific(sk->sk_family)
6210 ->addr_to_user(sctp_sk(sk), &temp);
6212 if (space_left < addrlen) {
6216 memcpy(to, &temp, addrlen);
6220 space_left -= addrlen;
6221 *bytes_copied += addrlen;
6229 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6230 char __user *optval, int __user *optlen)
6232 struct sctp_bind_addr *bp;
6233 struct sctp_association *asoc;
6235 struct sctp_getaddrs getaddrs;
6236 struct sctp_sockaddr_entry *addr;
6238 union sctp_addr temp;
6239 struct sctp_sock *sp = sctp_sk(sk);
6243 int bytes_copied = 0;
6247 if (len < sizeof(struct sctp_getaddrs))
6250 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6254 * For UDP-style sockets, id specifies the association to query.
6255 * If the id field is set to the value '0' then the locally bound
6256 * addresses are returned without regard to any particular
6259 if (0 == getaddrs.assoc_id) {
6260 bp = &sctp_sk(sk)->ep->base.bind_addr;
6262 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6265 bp = &asoc->base.bind_addr;
6268 to = optval + offsetof(struct sctp_getaddrs, addrs);
6269 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6271 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6275 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6276 * addresses from the global local address list.
6278 if (sctp_list_single_entry(&bp->address_list)) {
6279 addr = list_entry(bp->address_list.next,
6280 struct sctp_sockaddr_entry, list);
6281 if (sctp_is_any(sk, &addr->a)) {
6282 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6283 space_left, &bytes_copied);
6293 /* Protection on the bound address list is not needed since
6294 * in the socket option context we hold a socket lock and
6295 * thus the bound address list can't change.
6297 list_for_each_entry(addr, &bp->address_list, list) {
6298 memcpy(&temp, &addr->a, sizeof(temp));
6299 addrlen = sctp_get_pf_specific(sk->sk_family)
6300 ->addr_to_user(sp, &temp);
6301 if (space_left < addrlen) {
6302 err = -ENOMEM; /*fixme: right error?*/
6305 memcpy(buf, &temp, addrlen);
6307 bytes_copied += addrlen;
6309 space_left -= addrlen;
6313 if (copy_to_user(to, addrs, bytes_copied)) {
6317 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6321 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6322 * but we can't change it anymore.
6324 if (put_user(bytes_copied, optlen))
6331 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6333 * Requests that the local SCTP stack use the enclosed peer address as
6334 * the association primary. The enclosed address must be one of the
6335 * association peer's addresses.
6337 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6338 char __user *optval, int __user *optlen)
6340 struct sctp_prim prim;
6341 struct sctp_association *asoc;
6342 struct sctp_sock *sp = sctp_sk(sk);
6344 if (len < sizeof(struct sctp_prim))
6347 len = sizeof(struct sctp_prim);
6349 if (copy_from_user(&prim, optval, len))
6352 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6356 if (!asoc->peer.primary_path)
6359 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6360 asoc->peer.primary_path->af_specific->sockaddr_len);
6362 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6363 (union sctp_addr *)&prim.ssp_addr);
6365 if (put_user(len, optlen))
6367 if (copy_to_user(optval, &prim, len))
6374 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6376 * Requests that the local endpoint set the specified Adaptation Layer
6377 * Indication parameter for all future INIT and INIT-ACK exchanges.
6379 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6380 char __user *optval, int __user *optlen)
6382 struct sctp_setadaptation adaptation;
6384 if (len < sizeof(struct sctp_setadaptation))
6387 len = sizeof(struct sctp_setadaptation);
6389 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6391 if (put_user(len, optlen))
6393 if (copy_to_user(optval, &adaptation, len))
6401 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6403 * Applications that wish to use the sendto() system call may wish to
6404 * specify a default set of parameters that would normally be supplied
6405 * through the inclusion of ancillary data. This socket option allows
6406 * such an application to set the default sctp_sndrcvinfo structure.
6409 * The application that wishes to use this socket option simply passes
6410 * in to this call the sctp_sndrcvinfo structure defined in Section
6411 * 5.2.2) The input parameters accepted by this call include
6412 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6413 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6414 * to this call if the caller is using the UDP model.
6416 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6418 static int sctp_getsockopt_default_send_param(struct sock *sk,
6419 int len, char __user *optval,
6422 struct sctp_sock *sp = sctp_sk(sk);
6423 struct sctp_association *asoc;
6424 struct sctp_sndrcvinfo info;
6426 if (len < sizeof(info))
6431 if (copy_from_user(&info, optval, len))
6434 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6435 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6436 sctp_style(sk, UDP))
6440 info.sinfo_stream = asoc->default_stream;
6441 info.sinfo_flags = asoc->default_flags;
6442 info.sinfo_ppid = asoc->default_ppid;
6443 info.sinfo_context = asoc->default_context;
6444 info.sinfo_timetolive = asoc->default_timetolive;
6446 info.sinfo_stream = sp->default_stream;
6447 info.sinfo_flags = sp->default_flags;
6448 info.sinfo_ppid = sp->default_ppid;
6449 info.sinfo_context = sp->default_context;
6450 info.sinfo_timetolive = sp->default_timetolive;
6453 if (put_user(len, optlen))
6455 if (copy_to_user(optval, &info, len))
6461 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6462 * (SCTP_DEFAULT_SNDINFO)
6464 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6465 char __user *optval,
6468 struct sctp_sock *sp = sctp_sk(sk);
6469 struct sctp_association *asoc;
6470 struct sctp_sndinfo info;
6472 if (len < sizeof(info))
6477 if (copy_from_user(&info, optval, len))
6480 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6481 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6482 sctp_style(sk, UDP))
6486 info.snd_sid = asoc->default_stream;
6487 info.snd_flags = asoc->default_flags;
6488 info.snd_ppid = asoc->default_ppid;
6489 info.snd_context = asoc->default_context;
6491 info.snd_sid = sp->default_stream;
6492 info.snd_flags = sp->default_flags;
6493 info.snd_ppid = sp->default_ppid;
6494 info.snd_context = sp->default_context;
6497 if (put_user(len, optlen))
6499 if (copy_to_user(optval, &info, len))
6507 * 7.1.5 SCTP_NODELAY
6509 * Turn on/off any Nagle-like algorithm. This means that packets are
6510 * generally sent as soon as possible and no unnecessary delays are
6511 * introduced, at the cost of more packets in the network. Expects an
6512 * integer boolean flag.
6515 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6516 char __user *optval, int __user *optlen)
6520 if (len < sizeof(int))
6524 val = (sctp_sk(sk)->nodelay == 1);
6525 if (put_user(len, optlen))
6527 if (copy_to_user(optval, &val, len))
6534 * 7.1.1 SCTP_RTOINFO
6536 * The protocol parameters used to initialize and bound retransmission
6537 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6538 * and modify these parameters.
6539 * All parameters are time values, in milliseconds. A value of 0, when
6540 * modifying the parameters, indicates that the current value should not
6544 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6545 char __user *optval,
6546 int __user *optlen) {
6547 struct sctp_rtoinfo rtoinfo;
6548 struct sctp_association *asoc;
6550 if (len < sizeof (struct sctp_rtoinfo))
6553 len = sizeof(struct sctp_rtoinfo);
6555 if (copy_from_user(&rtoinfo, optval, len))
6558 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6560 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6561 sctp_style(sk, UDP))
6564 /* Values corresponding to the specific association. */
6566 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6567 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6568 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6570 /* Values corresponding to the endpoint. */
6571 struct sctp_sock *sp = sctp_sk(sk);
6573 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6574 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6575 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6578 if (put_user(len, optlen))
6581 if (copy_to_user(optval, &rtoinfo, len))
6589 * 7.1.2 SCTP_ASSOCINFO
6591 * This option is used to tune the maximum retransmission attempts
6592 * of the association.
6593 * Returns an error if the new association retransmission value is
6594 * greater than the sum of the retransmission value of the peer.
6595 * See [SCTP] for more information.
6598 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6599 char __user *optval,
6603 struct sctp_assocparams assocparams;
6604 struct sctp_association *asoc;
6605 struct list_head *pos;
6608 if (len < sizeof (struct sctp_assocparams))
6611 len = sizeof(struct sctp_assocparams);
6613 if (copy_from_user(&assocparams, optval, len))
6616 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6618 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6619 sctp_style(sk, UDP))
6622 /* Values correspoinding to the specific association */
6624 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6625 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6626 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6627 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6629 list_for_each(pos, &asoc->peer.transport_addr_list) {
6633 assocparams.sasoc_number_peer_destinations = cnt;
6635 /* Values corresponding to the endpoint */
6636 struct sctp_sock *sp = sctp_sk(sk);
6638 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6639 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6640 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6641 assocparams.sasoc_cookie_life =
6642 sp->assocparams.sasoc_cookie_life;
6643 assocparams.sasoc_number_peer_destinations =
6645 sasoc_number_peer_destinations;
6648 if (put_user(len, optlen))
6651 if (copy_to_user(optval, &assocparams, len))
6658 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6660 * This socket option is a boolean flag which turns on or off mapped V4
6661 * addresses. If this option is turned on and the socket is type
6662 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6663 * If this option is turned off, then no mapping will be done of V4
6664 * addresses and a user will receive both PF_INET6 and PF_INET type
6665 * addresses on the socket.
6667 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6668 char __user *optval, int __user *optlen)
6671 struct sctp_sock *sp = sctp_sk(sk);
6673 if (len < sizeof(int))
6678 if (put_user(len, optlen))
6680 if (copy_to_user(optval, &val, len))
6687 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6688 * (chapter and verse is quoted at sctp_setsockopt_context())
6690 static int sctp_getsockopt_context(struct sock *sk, int len,
6691 char __user *optval, int __user *optlen)
6693 struct sctp_assoc_value params;
6694 struct sctp_association *asoc;
6696 if (len < sizeof(struct sctp_assoc_value))
6699 len = sizeof(struct sctp_assoc_value);
6701 if (copy_from_user(¶ms, optval, len))
6704 asoc = sctp_id2assoc(sk, params.assoc_id);
6705 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6706 sctp_style(sk, UDP))
6709 params.assoc_value = asoc ? asoc->default_rcv_context
6710 : sctp_sk(sk)->default_rcv_context;
6712 if (put_user(len, optlen))
6714 if (copy_to_user(optval, ¶ms, len))
6721 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6722 * This option will get or set the maximum size to put in any outgoing
6723 * SCTP DATA chunk. If a message is larger than this size it will be
6724 * fragmented by SCTP into the specified size. Note that the underlying
6725 * SCTP implementation may fragment into smaller sized chunks when the
6726 * PMTU of the underlying association is smaller than the value set by
6727 * the user. The default value for this option is '0' which indicates
6728 * the user is NOT limiting fragmentation and only the PMTU will effect
6729 * SCTP's choice of DATA chunk size. Note also that values set larger
6730 * than the maximum size of an IP datagram will effectively let SCTP
6731 * control fragmentation (i.e. the same as setting this option to 0).
6733 * The following structure is used to access and modify this parameter:
6735 * struct sctp_assoc_value {
6736 * sctp_assoc_t assoc_id;
6737 * uint32_t assoc_value;
6740 * assoc_id: This parameter is ignored for one-to-one style sockets.
6741 * For one-to-many style sockets this parameter indicates which
6742 * association the user is performing an action upon. Note that if
6743 * this field's value is zero then the endpoints default value is
6744 * changed (effecting future associations only).
6745 * assoc_value: This parameter specifies the maximum size in bytes.
6747 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6748 char __user *optval, int __user *optlen)
6750 struct sctp_assoc_value params;
6751 struct sctp_association *asoc;
6753 if (len == sizeof(int)) {
6754 pr_warn_ratelimited(DEPRECATED
6756 "Use of int in maxseg socket option.\n"
6757 "Use struct sctp_assoc_value instead\n",
6758 current->comm, task_pid_nr(current));
6759 params.assoc_id = SCTP_FUTURE_ASSOC;
6760 } else if (len >= sizeof(struct sctp_assoc_value)) {
6761 len = sizeof(struct sctp_assoc_value);
6762 if (copy_from_user(¶ms, optval, len))
6767 asoc = sctp_id2assoc(sk, params.assoc_id);
6768 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6769 sctp_style(sk, UDP))
6773 params.assoc_value = asoc->frag_point;
6775 params.assoc_value = sctp_sk(sk)->user_frag;
6777 if (put_user(len, optlen))
6779 if (len == sizeof(int)) {
6780 if (copy_to_user(optval, ¶ms.assoc_value, len))
6783 if (copy_to_user(optval, ¶ms, len))
6791 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6792 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6794 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6795 char __user *optval, int __user *optlen)
6799 if (len < sizeof(int))
6804 val = sctp_sk(sk)->frag_interleave;
6805 if (put_user(len, optlen))
6807 if (copy_to_user(optval, &val, len))
6814 * 7.1.25. Set or Get the sctp partial delivery point
6815 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6817 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6818 char __user *optval,
6823 if (len < sizeof(u32))
6828 val = sctp_sk(sk)->pd_point;
6829 if (put_user(len, optlen))
6831 if (copy_to_user(optval, &val, len))
6838 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6839 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6841 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6842 char __user *optval,
6845 struct sctp_assoc_value params;
6846 struct sctp_association *asoc;
6848 if (len == sizeof(int)) {
6849 pr_warn_ratelimited(DEPRECATED
6851 "Use of int in max_burst socket option.\n"
6852 "Use struct sctp_assoc_value instead\n",
6853 current->comm, task_pid_nr(current));
6854 params.assoc_id = SCTP_FUTURE_ASSOC;
6855 } else if (len >= sizeof(struct sctp_assoc_value)) {
6856 len = sizeof(struct sctp_assoc_value);
6857 if (copy_from_user(¶ms, optval, len))
6862 asoc = sctp_id2assoc(sk, params.assoc_id);
6863 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6864 sctp_style(sk, UDP))
6867 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6869 if (len == sizeof(int)) {
6870 if (copy_to_user(optval, ¶ms.assoc_value, len))
6873 if (copy_to_user(optval, ¶ms, len))
6881 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6882 char __user *optval, int __user *optlen)
6884 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6885 struct sctp_hmacalgo __user *p = (void __user *)optval;
6886 struct sctp_hmac_algo_param *hmacs;
6891 if (!ep->auth_enable)
6894 hmacs = ep->auth_hmacs_list;
6895 data_len = ntohs(hmacs->param_hdr.length) -
6896 sizeof(struct sctp_paramhdr);
6898 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6901 len = sizeof(struct sctp_hmacalgo) + data_len;
6902 num_idents = data_len / sizeof(u16);
6904 if (put_user(len, optlen))
6906 if (put_user(num_idents, &p->shmac_num_idents))
6908 for (i = 0; i < num_idents; i++) {
6909 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6911 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6917 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6918 char __user *optval, int __user *optlen)
6920 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6921 struct sctp_authkeyid val;
6922 struct sctp_association *asoc;
6924 if (!ep->auth_enable)
6927 if (len < sizeof(struct sctp_authkeyid))
6930 len = sizeof(struct sctp_authkeyid);
6931 if (copy_from_user(&val, optval, len))
6934 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6935 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6939 val.scact_keynumber = asoc->active_key_id;
6941 val.scact_keynumber = ep->active_key_id;
6943 if (put_user(len, optlen))
6945 if (copy_to_user(optval, &val, len))
6951 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6952 char __user *optval, int __user *optlen)
6954 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6955 struct sctp_authchunks __user *p = (void __user *)optval;
6956 struct sctp_authchunks val;
6957 struct sctp_association *asoc;
6958 struct sctp_chunks_param *ch;
6962 if (!ep->auth_enable)
6965 if (len < sizeof(struct sctp_authchunks))
6968 if (copy_from_user(&val, optval, sizeof(val)))
6971 to = p->gauth_chunks;
6972 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6976 ch = asoc->peer.peer_chunks;
6980 /* See if the user provided enough room for all the data */
6981 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6982 if (len < num_chunks)
6985 if (copy_to_user(to, ch->chunks, num_chunks))
6988 len = sizeof(struct sctp_authchunks) + num_chunks;
6989 if (put_user(len, optlen))
6991 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6996 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6997 char __user *optval, int __user *optlen)
6999 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7000 struct sctp_authchunks __user *p = (void __user *)optval;
7001 struct sctp_authchunks val;
7002 struct sctp_association *asoc;
7003 struct sctp_chunks_param *ch;
7007 if (!ep->auth_enable)
7010 if (len < sizeof(struct sctp_authchunks))
7013 if (copy_from_user(&val, optval, sizeof(val)))
7016 to = p->gauth_chunks;
7017 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7018 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7019 sctp_style(sk, UDP))
7022 ch = asoc ? (struct sctp_chunks_param *)asoc->c.auth_chunks
7023 : ep->auth_chunk_list;
7027 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7028 if (len < sizeof(struct sctp_authchunks) + num_chunks)
7031 if (copy_to_user(to, ch->chunks, num_chunks))
7034 len = sizeof(struct sctp_authchunks) + num_chunks;
7035 if (put_user(len, optlen))
7037 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7044 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7045 * This option gets the current number of associations that are attached
7046 * to a one-to-many style socket. The option value is an uint32_t.
7048 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7049 char __user *optval, int __user *optlen)
7051 struct sctp_sock *sp = sctp_sk(sk);
7052 struct sctp_association *asoc;
7055 if (sctp_style(sk, TCP))
7058 if (len < sizeof(u32))
7063 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7067 if (put_user(len, optlen))
7069 if (copy_to_user(optval, &val, len))
7076 * 8.1.23 SCTP_AUTO_ASCONF
7077 * See the corresponding setsockopt entry as description
7079 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7080 char __user *optval, int __user *optlen)
7084 if (len < sizeof(int))
7088 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7090 if (put_user(len, optlen))
7092 if (copy_to_user(optval, &val, len))
7098 * 8.2.6. Get the Current Identifiers of Associations
7099 * (SCTP_GET_ASSOC_ID_LIST)
7101 * This option gets the current list of SCTP association identifiers of
7102 * the SCTP associations handled by a one-to-many style socket.
7104 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7105 char __user *optval, int __user *optlen)
7107 struct sctp_sock *sp = sctp_sk(sk);
7108 struct sctp_association *asoc;
7109 struct sctp_assoc_ids *ids;
7112 if (sctp_style(sk, TCP))
7115 if (len < sizeof(struct sctp_assoc_ids))
7118 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7122 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7125 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7127 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7131 ids->gaids_number_of_ids = num;
7133 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7134 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7137 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7147 * SCTP_PEER_ADDR_THLDS
7149 * This option allows us to fetch the partially failed threshold for one or all
7150 * transports in an association. See Section 6.1 of:
7151 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7153 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7154 char __user *optval,
7158 struct sctp_paddrthlds val;
7159 struct sctp_transport *trans;
7160 struct sctp_association *asoc;
7162 if (len < sizeof(struct sctp_paddrthlds))
7164 len = sizeof(struct sctp_paddrthlds);
7165 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7168 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7169 trans = sctp_addr_id2transport(sk, &val.spt_address,
7174 val.spt_pathmaxrxt = trans->pathmaxrxt;
7175 val.spt_pathpfthld = trans->pf_retrans;
7180 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7181 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7182 sctp_style(sk, UDP))
7186 val.spt_pathpfthld = asoc->pf_retrans;
7187 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7189 struct sctp_sock *sp = sctp_sk(sk);
7191 val.spt_pathpfthld = sp->pf_retrans;
7192 val.spt_pathmaxrxt = sp->pathmaxrxt;
7196 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7203 * SCTP_GET_ASSOC_STATS
7205 * This option retrieves local per endpoint statistics. It is modeled
7206 * after OpenSolaris' implementation
7208 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7209 char __user *optval,
7212 struct sctp_assoc_stats sas;
7213 struct sctp_association *asoc = NULL;
7215 /* User must provide at least the assoc id */
7216 if (len < sizeof(sctp_assoc_t))
7219 /* Allow the struct to grow and fill in as much as possible */
7220 len = min_t(size_t, len, sizeof(sas));
7222 if (copy_from_user(&sas, optval, len))
7225 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7229 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7230 sas.sas_gapcnt = asoc->stats.gapcnt;
7231 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7232 sas.sas_osacks = asoc->stats.osacks;
7233 sas.sas_isacks = asoc->stats.isacks;
7234 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7235 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7236 sas.sas_oodchunks = asoc->stats.oodchunks;
7237 sas.sas_iodchunks = asoc->stats.iodchunks;
7238 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7239 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7240 sas.sas_idupchunks = asoc->stats.idupchunks;
7241 sas.sas_opackets = asoc->stats.opackets;
7242 sas.sas_ipackets = asoc->stats.ipackets;
7244 /* New high max rto observed, will return 0 if not a single
7245 * RTO update took place. obs_rto_ipaddr will be bogus
7248 sas.sas_maxrto = asoc->stats.max_obs_rto;
7249 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7250 sizeof(struct sockaddr_storage));
7252 /* Mark beginning of a new observation period */
7253 asoc->stats.max_obs_rto = asoc->rto_min;
7255 if (put_user(len, optlen))
7258 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7260 if (copy_to_user(optval, &sas, len))
7266 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7267 char __user *optval,
7272 if (len < sizeof(int))
7276 if (sctp_sk(sk)->recvrcvinfo)
7278 if (put_user(len, optlen))
7280 if (copy_to_user(optval, &val, len))
7286 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7287 char __user *optval,
7292 if (len < sizeof(int))
7296 if (sctp_sk(sk)->recvnxtinfo)
7298 if (put_user(len, optlen))
7300 if (copy_to_user(optval, &val, len))
7306 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7307 char __user *optval,
7310 struct sctp_assoc_value params;
7311 struct sctp_association *asoc;
7312 int retval = -EFAULT;
7314 if (len < sizeof(params)) {
7319 len = sizeof(params);
7320 if (copy_from_user(¶ms, optval, len))
7323 asoc = sctp_id2assoc(sk, params.assoc_id);
7324 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7325 sctp_style(sk, UDP)) {
7330 params.assoc_value = asoc ? asoc->peer.prsctp_capable
7331 : sctp_sk(sk)->ep->prsctp_enable;
7333 if (put_user(len, optlen))
7336 if (copy_to_user(optval, ¶ms, len))
7345 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7346 char __user *optval,
7349 struct sctp_default_prinfo info;
7350 struct sctp_association *asoc;
7351 int retval = -EFAULT;
7353 if (len < sizeof(info)) {
7359 if (copy_from_user(&info, optval, len))
7362 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7363 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7364 sctp_style(sk, UDP)) {
7370 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7371 info.pr_value = asoc->default_timetolive;
7373 struct sctp_sock *sp = sctp_sk(sk);
7375 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7376 info.pr_value = sp->default_timetolive;
7379 if (put_user(len, optlen))
7382 if (copy_to_user(optval, &info, len))
7391 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7392 char __user *optval,
7395 struct sctp_prstatus params;
7396 struct sctp_association *asoc;
7398 int retval = -EINVAL;
7400 if (len < sizeof(params))
7403 len = sizeof(params);
7404 if (copy_from_user(¶ms, optval, len)) {
7409 policy = params.sprstat_policy;
7410 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7411 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7414 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7418 if (policy == SCTP_PR_SCTP_ALL) {
7419 params.sprstat_abandoned_unsent = 0;
7420 params.sprstat_abandoned_sent = 0;
7421 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7422 params.sprstat_abandoned_unsent +=
7423 asoc->abandoned_unsent[policy];
7424 params.sprstat_abandoned_sent +=
7425 asoc->abandoned_sent[policy];
7428 params.sprstat_abandoned_unsent =
7429 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7430 params.sprstat_abandoned_sent =
7431 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7434 if (put_user(len, optlen)) {
7439 if (copy_to_user(optval, ¶ms, len)) {
7450 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7451 char __user *optval,
7454 struct sctp_stream_out_ext *streamoute;
7455 struct sctp_association *asoc;
7456 struct sctp_prstatus params;
7457 int retval = -EINVAL;
7460 if (len < sizeof(params))
7463 len = sizeof(params);
7464 if (copy_from_user(¶ms, optval, len)) {
7469 policy = params.sprstat_policy;
7470 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7471 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7474 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7475 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7478 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7480 /* Not allocated yet, means all stats are 0 */
7481 params.sprstat_abandoned_unsent = 0;
7482 params.sprstat_abandoned_sent = 0;
7487 if (policy == SCTP_PR_SCTP_ALL) {
7488 params.sprstat_abandoned_unsent = 0;
7489 params.sprstat_abandoned_sent = 0;
7490 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7491 params.sprstat_abandoned_unsent +=
7492 streamoute->abandoned_unsent[policy];
7493 params.sprstat_abandoned_sent +=
7494 streamoute->abandoned_sent[policy];
7497 params.sprstat_abandoned_unsent =
7498 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7499 params.sprstat_abandoned_sent =
7500 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7503 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7514 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7515 char __user *optval,
7518 struct sctp_assoc_value params;
7519 struct sctp_association *asoc;
7520 int retval = -EFAULT;
7522 if (len < sizeof(params)) {
7527 len = sizeof(params);
7528 if (copy_from_user(¶ms, optval, len))
7531 asoc = sctp_id2assoc(sk, params.assoc_id);
7532 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7533 sctp_style(sk, UDP)) {
7538 params.assoc_value = asoc ? asoc->peer.reconf_capable
7539 : sctp_sk(sk)->ep->reconf_enable;
7541 if (put_user(len, optlen))
7544 if (copy_to_user(optval, ¶ms, len))
7553 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7554 char __user *optval,
7557 struct sctp_assoc_value params;
7558 struct sctp_association *asoc;
7559 int retval = -EFAULT;
7561 if (len < sizeof(params)) {
7566 len = sizeof(params);
7567 if (copy_from_user(¶ms, optval, len))
7570 asoc = sctp_id2assoc(sk, params.assoc_id);
7571 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7572 sctp_style(sk, UDP)) {
7577 params.assoc_value = asoc ? asoc->strreset_enable
7578 : sctp_sk(sk)->ep->strreset_enable;
7580 if (put_user(len, optlen))
7583 if (copy_to_user(optval, ¶ms, len))
7592 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7593 char __user *optval,
7596 struct sctp_assoc_value params;
7597 struct sctp_association *asoc;
7598 int retval = -EFAULT;
7600 if (len < sizeof(params)) {
7605 len = sizeof(params);
7606 if (copy_from_user(¶ms, optval, len))
7609 asoc = sctp_id2assoc(sk, params.assoc_id);
7610 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7611 sctp_style(sk, UDP)) {
7616 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7617 : sctp_sk(sk)->default_ss;
7619 if (put_user(len, optlen))
7622 if (copy_to_user(optval, ¶ms, len))
7631 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7632 char __user *optval,
7635 struct sctp_stream_value params;
7636 struct sctp_association *asoc;
7637 int retval = -EFAULT;
7639 if (len < sizeof(params)) {
7644 len = sizeof(params);
7645 if (copy_from_user(¶ms, optval, len))
7648 asoc = sctp_id2assoc(sk, params.assoc_id);
7654 retval = sctp_sched_get_value(asoc, params.stream_id,
7655 ¶ms.stream_value);
7659 if (put_user(len, optlen)) {
7664 if (copy_to_user(optval, ¶ms, len)) {
7673 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7674 char __user *optval,
7677 struct sctp_assoc_value params;
7678 struct sctp_association *asoc;
7679 int retval = -EFAULT;
7681 if (len < sizeof(params)) {
7686 len = sizeof(params);
7687 if (copy_from_user(¶ms, optval, len))
7690 asoc = sctp_id2assoc(sk, params.assoc_id);
7691 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7692 sctp_style(sk, UDP)) {
7697 params.assoc_value = asoc ? asoc->peer.intl_capable
7698 : sctp_sk(sk)->ep->intl_enable;
7700 if (put_user(len, optlen))
7703 if (copy_to_user(optval, ¶ms, len))
7712 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7713 char __user *optval,
7718 if (len < sizeof(int))
7722 val = sctp_sk(sk)->reuse;
7723 if (put_user(len, optlen))
7726 if (copy_to_user(optval, &val, len))
7732 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7735 struct sctp_association *asoc;
7736 struct sctp_event param;
7739 if (len < sizeof(param))
7742 len = sizeof(param);
7743 if (copy_from_user(¶m, optval, len))
7746 if (param.se_type < SCTP_SN_TYPE_BASE ||
7747 param.se_type > SCTP_SN_TYPE_MAX)
7750 asoc = sctp_id2assoc(sk, param.se_assoc_id);
7751 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7752 sctp_style(sk, UDP))
7755 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7756 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7758 if (put_user(len, optlen))
7761 if (copy_to_user(optval, ¶m, len))
7767 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7768 char __user *optval, int __user *optlen)
7773 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7775 /* I can hardly begin to describe how wrong this is. This is
7776 * so broken as to be worse than useless. The API draft
7777 * REALLY is NOT helpful here... I am not convinced that the
7778 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7779 * are at all well-founded.
7781 if (level != SOL_SCTP) {
7782 struct sctp_af *af = sctp_sk(sk)->pf->af;
7784 retval = af->getsockopt(sk, level, optname, optval, optlen);
7788 if (get_user(len, optlen))
7798 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7800 case SCTP_DISABLE_FRAGMENTS:
7801 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7805 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7807 case SCTP_AUTOCLOSE:
7808 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7810 case SCTP_SOCKOPT_PEELOFF:
7811 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7813 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7814 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7816 case SCTP_PEER_ADDR_PARAMS:
7817 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7820 case SCTP_DELAYED_SACK:
7821 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7825 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7827 case SCTP_GET_PEER_ADDRS:
7828 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7831 case SCTP_GET_LOCAL_ADDRS:
7832 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7835 case SCTP_SOCKOPT_CONNECTX3:
7836 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7838 case SCTP_DEFAULT_SEND_PARAM:
7839 retval = sctp_getsockopt_default_send_param(sk, len,
7842 case SCTP_DEFAULT_SNDINFO:
7843 retval = sctp_getsockopt_default_sndinfo(sk, len,
7846 case SCTP_PRIMARY_ADDR:
7847 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7850 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7853 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7855 case SCTP_ASSOCINFO:
7856 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7858 case SCTP_I_WANT_MAPPED_V4_ADDR:
7859 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7862 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7864 case SCTP_GET_PEER_ADDR_INFO:
7865 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7868 case SCTP_ADAPTATION_LAYER:
7869 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7873 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7875 case SCTP_FRAGMENT_INTERLEAVE:
7876 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7879 case SCTP_PARTIAL_DELIVERY_POINT:
7880 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7883 case SCTP_MAX_BURST:
7884 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7887 case SCTP_AUTH_CHUNK:
7888 case SCTP_AUTH_DELETE_KEY:
7889 case SCTP_AUTH_DEACTIVATE_KEY:
7890 retval = -EOPNOTSUPP;
7892 case SCTP_HMAC_IDENT:
7893 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7895 case SCTP_AUTH_ACTIVE_KEY:
7896 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7898 case SCTP_PEER_AUTH_CHUNKS:
7899 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7902 case SCTP_LOCAL_AUTH_CHUNKS:
7903 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7906 case SCTP_GET_ASSOC_NUMBER:
7907 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7909 case SCTP_GET_ASSOC_ID_LIST:
7910 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7912 case SCTP_AUTO_ASCONF:
7913 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7915 case SCTP_PEER_ADDR_THLDS:
7916 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7918 case SCTP_GET_ASSOC_STATS:
7919 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7921 case SCTP_RECVRCVINFO:
7922 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7924 case SCTP_RECVNXTINFO:
7925 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7927 case SCTP_PR_SUPPORTED:
7928 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7930 case SCTP_DEFAULT_PRINFO:
7931 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7934 case SCTP_PR_ASSOC_STATUS:
7935 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7938 case SCTP_PR_STREAM_STATUS:
7939 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7942 case SCTP_RECONFIG_SUPPORTED:
7943 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7946 case SCTP_ENABLE_STREAM_RESET:
7947 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7950 case SCTP_STREAM_SCHEDULER:
7951 retval = sctp_getsockopt_scheduler(sk, len, optval,
7954 case SCTP_STREAM_SCHEDULER_VALUE:
7955 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7958 case SCTP_INTERLEAVING_SUPPORTED:
7959 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7962 case SCTP_REUSE_PORT:
7963 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7966 retval = sctp_getsockopt_event(sk, len, optval, optlen);
7969 retval = -ENOPROTOOPT;
7977 static int sctp_hash(struct sock *sk)
7983 static void sctp_unhash(struct sock *sk)
7988 /* Check if port is acceptable. Possibly find first available port.
7990 * The port hash table (contained in the 'global' SCTP protocol storage
7991 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7992 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7993 * list (the list number is the port number hashed out, so as you
7994 * would expect from a hash function, all the ports in a given list have
7995 * such a number that hashes out to the same list number; you were
7996 * expecting that, right?); so each list has a set of ports, with a
7997 * link to the socket (struct sock) that uses it, the port number and
7998 * a fastreuse flag (FIXME: NPI ipg).
8000 static struct sctp_bind_bucket *sctp_bucket_create(
8001 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8003 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8005 struct sctp_sock *sp = sctp_sk(sk);
8006 bool reuse = (sk->sk_reuse || sp->reuse);
8007 struct sctp_bind_hashbucket *head; /* hash list */
8008 kuid_t uid = sock_i_uid(sk);
8009 struct sctp_bind_bucket *pp;
8010 unsigned short snum;
8013 snum = ntohs(addr->v4.sin_port);
8015 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8020 /* Search for an available port. */
8021 int low, high, remaining, index;
8023 struct net *net = sock_net(sk);
8025 inet_get_local_port_range(net, &low, &high);
8026 remaining = (high - low) + 1;
8027 rover = prandom_u32() % remaining + low;
8031 if ((rover < low) || (rover > high))
8033 if (inet_is_local_reserved_port(net, rover))
8035 index = sctp_phashfn(sock_net(sk), rover);
8036 head = &sctp_port_hashtable[index];
8037 spin_lock(&head->lock);
8038 sctp_for_each_hentry(pp, &head->chain)
8039 if ((pp->port == rover) &&
8040 net_eq(sock_net(sk), pp->net))
8044 spin_unlock(&head->lock);
8045 } while (--remaining > 0);
8047 /* Exhausted local port range during search? */
8052 /* OK, here is the one we will use. HEAD (the port
8053 * hash table list entry) is non-NULL and we hold it's
8058 /* We are given an specific port number; we verify
8059 * that it is not being used. If it is used, we will
8060 * exahust the search in the hash list corresponding
8061 * to the port number (snum) - we detect that with the
8062 * port iterator, pp being NULL.
8064 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
8065 spin_lock(&head->lock);
8066 sctp_for_each_hentry(pp, &head->chain) {
8067 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
8074 if (!hlist_empty(&pp->owner)) {
8075 /* We had a port hash table hit - there is an
8076 * available port (pp != NULL) and it is being
8077 * used by other socket (pp->owner not empty); that other
8078 * socket is going to be sk2.
8082 pr_debug("%s: found a possible match\n", __func__);
8084 if ((pp->fastreuse && reuse &&
8085 sk->sk_state != SCTP_SS_LISTENING) ||
8086 (pp->fastreuseport && sk->sk_reuseport &&
8087 uid_eq(pp->fastuid, uid)))
8090 /* Run through the list of sockets bound to the port
8091 * (pp->port) [via the pointers bind_next and
8092 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8093 * we get the endpoint they describe and run through
8094 * the endpoint's list of IP (v4 or v6) addresses,
8095 * comparing each of the addresses with the address of
8096 * the socket sk. If we find a match, then that means
8097 * that this port/socket (sk) combination are already
8100 sk_for_each_bound(sk2, &pp->owner) {
8101 struct sctp_sock *sp2 = sctp_sk(sk2);
8102 struct sctp_endpoint *ep2 = sp2->ep;
8105 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8106 sk2->sk_state != SCTP_SS_LISTENING) ||
8107 (sk->sk_reuseport && sk2->sk_reuseport &&
8108 uid_eq(uid, sock_i_uid(sk2))))
8111 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8118 pr_debug("%s: found a match\n", __func__);
8121 /* If there was a hash table miss, create a new port. */
8123 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
8126 /* In either case (hit or miss), make sure fastreuse is 1 only
8127 * if sk->sk_reuse is too (that is, if the caller requested
8128 * SO_REUSEADDR on this socket -sk-).
8130 if (hlist_empty(&pp->owner)) {
8131 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8136 if (sk->sk_reuseport) {
8137 pp->fastreuseport = 1;
8140 pp->fastreuseport = 0;
8143 if (pp->fastreuse &&
8144 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8147 if (pp->fastreuseport &&
8148 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8149 pp->fastreuseport = 0;
8152 /* We are set, so fill up all the data in the hash table
8153 * entry, tie the socket list information with the rest of the
8154 * sockets FIXME: Blurry, NPI (ipg).
8157 if (!sp->bind_hash) {
8158 inet_sk(sk)->inet_num = snum;
8159 sk_add_bind_node(sk, &pp->owner);
8165 spin_unlock(&head->lock);
8172 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8173 * port is requested.
8175 static int sctp_get_port(struct sock *sk, unsigned short snum)
8177 union sctp_addr addr;
8178 struct sctp_af *af = sctp_sk(sk)->pf->af;
8180 /* Set up a dummy address struct from the sk. */
8181 af->from_sk(&addr, sk);
8182 addr.v4.sin_port = htons(snum);
8184 /* Note: sk->sk_num gets filled in if ephemeral port request. */
8185 return sctp_get_port_local(sk, &addr);
8189 * Move a socket to LISTENING state.
8191 static int sctp_listen_start(struct sock *sk, int backlog)
8193 struct sctp_sock *sp = sctp_sk(sk);
8194 struct sctp_endpoint *ep = sp->ep;
8195 struct crypto_shash *tfm = NULL;
8198 /* Allocate HMAC for generating cookie. */
8199 if (!sp->hmac && sp->sctp_hmac_alg) {
8200 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8201 tfm = crypto_alloc_shash(alg, 0, 0);
8203 net_info_ratelimited("failed to load transform for %s: %ld\n",
8204 sp->sctp_hmac_alg, PTR_ERR(tfm));
8207 sctp_sk(sk)->hmac = tfm;
8211 * If a bind() or sctp_bindx() is not called prior to a listen()
8212 * call that allows new associations to be accepted, the system
8213 * picks an ephemeral port and will choose an address set equivalent
8214 * to binding with a wildcard address.
8216 * This is not currently spelled out in the SCTP sockets
8217 * extensions draft, but follows the practice as seen in TCP
8221 inet_sk_set_state(sk, SCTP_SS_LISTENING);
8222 if (!ep->base.bind_addr.port) {
8223 if (sctp_autobind(sk))
8226 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8227 inet_sk_set_state(sk, SCTP_SS_CLOSED);
8232 sk->sk_max_ack_backlog = backlog;
8233 return sctp_hash_endpoint(ep);
8237 * 4.1.3 / 5.1.3 listen()
8239 * By default, new associations are not accepted for UDP style sockets.
8240 * An application uses listen() to mark a socket as being able to
8241 * accept new associations.
8243 * On TCP style sockets, applications use listen() to ready the SCTP
8244 * endpoint for accepting inbound associations.
8246 * On both types of endpoints a backlog of '0' disables listening.
8248 * Move a socket to LISTENING state.
8250 int sctp_inet_listen(struct socket *sock, int backlog)
8252 struct sock *sk = sock->sk;
8253 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8256 if (unlikely(backlog < 0))
8261 /* Peeled-off sockets are not allowed to listen(). */
8262 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8265 if (sock->state != SS_UNCONNECTED)
8268 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8271 /* If backlog is zero, disable listening. */
8273 if (sctp_sstate(sk, CLOSED))
8277 sctp_unhash_endpoint(ep);
8278 sk->sk_state = SCTP_SS_CLOSED;
8279 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8280 sctp_sk(sk)->bind_hash->fastreuse = 1;
8284 /* If we are already listening, just update the backlog */
8285 if (sctp_sstate(sk, LISTENING))
8286 sk->sk_max_ack_backlog = backlog;
8288 err = sctp_listen_start(sk, backlog);
8300 * This function is done by modeling the current datagram_poll() and the
8301 * tcp_poll(). Note that, based on these implementations, we don't
8302 * lock the socket in this function, even though it seems that,
8303 * ideally, locking or some other mechanisms can be used to ensure
8304 * the integrity of the counters (sndbuf and wmem_alloc) used
8305 * in this place. We assume that we don't need locks either until proven
8308 * Another thing to note is that we include the Async I/O support
8309 * here, again, by modeling the current TCP/UDP code. We don't have
8310 * a good way to test with it yet.
8312 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8314 struct sock *sk = sock->sk;
8315 struct sctp_sock *sp = sctp_sk(sk);
8318 poll_wait(file, sk_sleep(sk), wait);
8320 sock_rps_record_flow(sk);
8322 /* A TCP-style listening socket becomes readable when the accept queue
8325 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8326 return (!list_empty(&sp->ep->asocs)) ?
8327 (EPOLLIN | EPOLLRDNORM) : 0;
8331 /* Is there any exceptional events? */
8332 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8334 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8335 if (sk->sk_shutdown & RCV_SHUTDOWN)
8336 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8337 if (sk->sk_shutdown == SHUTDOWN_MASK)
8340 /* Is it readable? Reconsider this code with TCP-style support. */
8341 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8342 mask |= EPOLLIN | EPOLLRDNORM;
8344 /* The association is either gone or not ready. */
8345 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8348 /* Is it writable? */
8349 if (sctp_writeable(sk)) {
8350 mask |= EPOLLOUT | EPOLLWRNORM;
8352 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8354 * Since the socket is not locked, the buffer
8355 * might be made available after the writeable check and
8356 * before the bit is set. This could cause a lost I/O
8357 * signal. tcp_poll() has a race breaker for this race
8358 * condition. Based on their implementation, we put
8359 * in the following code to cover it as well.
8361 if (sctp_writeable(sk))
8362 mask |= EPOLLOUT | EPOLLWRNORM;
8367 /********************************************************************
8368 * 2nd Level Abstractions
8369 ********************************************************************/
8371 static struct sctp_bind_bucket *sctp_bucket_create(
8372 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8374 struct sctp_bind_bucket *pp;
8376 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8378 SCTP_DBG_OBJCNT_INC(bind_bucket);
8381 INIT_HLIST_HEAD(&pp->owner);
8383 hlist_add_head(&pp->node, &head->chain);
8388 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8389 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8391 if (pp && hlist_empty(&pp->owner)) {
8392 __hlist_del(&pp->node);
8393 kmem_cache_free(sctp_bucket_cachep, pp);
8394 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8398 /* Release this socket's reference to a local port. */
8399 static inline void __sctp_put_port(struct sock *sk)
8401 struct sctp_bind_hashbucket *head =
8402 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8403 inet_sk(sk)->inet_num)];
8404 struct sctp_bind_bucket *pp;
8406 spin_lock(&head->lock);
8407 pp = sctp_sk(sk)->bind_hash;
8408 __sk_del_bind_node(sk);
8409 sctp_sk(sk)->bind_hash = NULL;
8410 inet_sk(sk)->inet_num = 0;
8411 sctp_bucket_destroy(pp);
8412 spin_unlock(&head->lock);
8415 void sctp_put_port(struct sock *sk)
8418 __sctp_put_port(sk);
8423 * The system picks an ephemeral port and choose an address set equivalent
8424 * to binding with a wildcard address.
8425 * One of those addresses will be the primary address for the association.
8426 * This automatically enables the multihoming capability of SCTP.
8428 static int sctp_autobind(struct sock *sk)
8430 union sctp_addr autoaddr;
8434 /* Initialize a local sockaddr structure to INADDR_ANY. */
8435 af = sctp_sk(sk)->pf->af;
8437 port = htons(inet_sk(sk)->inet_num);
8438 af->inaddr_any(&autoaddr, port);
8440 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8443 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8446 * 4.2 The cmsghdr Structure *
8448 * When ancillary data is sent or received, any number of ancillary data
8449 * objects can be specified by the msg_control and msg_controllen members of
8450 * the msghdr structure, because each object is preceded by
8451 * a cmsghdr structure defining the object's length (the cmsg_len member).
8452 * Historically Berkeley-derived implementations have passed only one object
8453 * at a time, but this API allows multiple objects to be
8454 * passed in a single call to sendmsg() or recvmsg(). The following example
8455 * shows two ancillary data objects in a control buffer.
8457 * |<--------------------------- msg_controllen -------------------------->|
8460 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8462 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8465 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8467 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8470 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8471 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8473 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8475 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8482 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8484 struct msghdr *my_msg = (struct msghdr *)msg;
8485 struct cmsghdr *cmsg;
8487 for_each_cmsghdr(cmsg, my_msg) {
8488 if (!CMSG_OK(my_msg, cmsg))
8491 /* Should we parse this header or ignore? */
8492 if (cmsg->cmsg_level != IPPROTO_SCTP)
8495 /* Strictly check lengths following example in SCM code. */
8496 switch (cmsg->cmsg_type) {
8498 /* SCTP Socket API Extension
8499 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8501 * This cmsghdr structure provides information for
8502 * initializing new SCTP associations with sendmsg().
8503 * The SCTP_INITMSG socket option uses this same data
8504 * structure. This structure is not used for
8507 * cmsg_level cmsg_type cmsg_data[]
8508 * ------------ ------------ ----------------------
8509 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8511 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8514 cmsgs->init = CMSG_DATA(cmsg);
8518 /* SCTP Socket API Extension
8519 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8521 * This cmsghdr structure specifies SCTP options for
8522 * sendmsg() and describes SCTP header information
8523 * about a received message through recvmsg().
8525 * cmsg_level cmsg_type cmsg_data[]
8526 * ------------ ------------ ----------------------
8527 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8529 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8532 cmsgs->srinfo = CMSG_DATA(cmsg);
8534 if (cmsgs->srinfo->sinfo_flags &
8535 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8536 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8537 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8542 /* SCTP Socket API Extension
8543 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8545 * This cmsghdr structure specifies SCTP options for
8546 * sendmsg(). This structure and SCTP_RCVINFO replaces
8547 * SCTP_SNDRCV which has been deprecated.
8549 * cmsg_level cmsg_type cmsg_data[]
8550 * ------------ ------------ ---------------------
8551 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8553 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8556 cmsgs->sinfo = CMSG_DATA(cmsg);
8558 if (cmsgs->sinfo->snd_flags &
8559 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8560 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8561 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8565 /* SCTP Socket API Extension
8566 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8568 * This cmsghdr structure specifies SCTP options for sendmsg().
8570 * cmsg_level cmsg_type cmsg_data[]
8571 * ------------ ------------ ---------------------
8572 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8574 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8577 cmsgs->prinfo = CMSG_DATA(cmsg);
8578 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8581 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8582 cmsgs->prinfo->pr_value = 0;
8585 /* SCTP Socket API Extension
8586 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8588 * This cmsghdr structure specifies SCTP options for sendmsg().
8590 * cmsg_level cmsg_type cmsg_data[]
8591 * ------------ ------------ ---------------------
8592 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8594 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8597 cmsgs->authinfo = CMSG_DATA(cmsg);
8599 case SCTP_DSTADDRV4:
8600 case SCTP_DSTADDRV6:
8601 /* SCTP Socket API Extension
8602 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8604 * This cmsghdr structure specifies SCTP options for sendmsg().
8606 * cmsg_level cmsg_type cmsg_data[]
8607 * ------------ ------------ ---------------------
8608 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8609 * ------------ ------------ ---------------------
8610 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8612 cmsgs->addrs_msg = my_msg;
8623 * Wait for a packet..
8624 * Note: This function is the same function as in core/datagram.c
8625 * with a few modifications to make lksctp work.
8627 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8632 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8634 /* Socket errors? */
8635 error = sock_error(sk);
8639 if (!skb_queue_empty(&sk->sk_receive_queue))
8642 /* Socket shut down? */
8643 if (sk->sk_shutdown & RCV_SHUTDOWN)
8646 /* Sequenced packets can come disconnected. If so we report the
8651 /* Is there a good reason to think that we may receive some data? */
8652 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8655 /* Handle signals. */
8656 if (signal_pending(current))
8659 /* Let another process have a go. Since we are going to sleep
8660 * anyway. Note: This may cause odd behaviors if the message
8661 * does not fit in the user's buffer, but this seems to be the
8662 * only way to honor MSG_DONTWAIT realistically.
8665 *timeo_p = schedule_timeout(*timeo_p);
8669 finish_wait(sk_sleep(sk), &wait);
8673 error = sock_intr_errno(*timeo_p);
8676 finish_wait(sk_sleep(sk), &wait);
8681 /* Receive a datagram.
8682 * Note: This is pretty much the same routine as in core/datagram.c
8683 * with a few changes to make lksctp work.
8685 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8686 int noblock, int *err)
8689 struct sk_buff *skb;
8692 timeo = sock_rcvtimeo(sk, noblock);
8694 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8695 MAX_SCHEDULE_TIMEOUT);
8698 /* Again only user level code calls this function,
8699 * so nothing interrupt level
8700 * will suddenly eat the receive_queue.
8702 * Look at current nfs client by the way...
8703 * However, this function was correct in any case. 8)
8705 if (flags & MSG_PEEK) {
8706 skb = skb_peek(&sk->sk_receive_queue);
8708 refcount_inc(&skb->users);
8710 skb = __skb_dequeue(&sk->sk_receive_queue);
8716 /* Caller is allowed not to check sk->sk_err before calling. */
8717 error = sock_error(sk);
8721 if (sk->sk_shutdown & RCV_SHUTDOWN)
8724 if (sk_can_busy_loop(sk)) {
8725 sk_busy_loop(sk, noblock);
8727 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8731 /* User doesn't want to wait. */
8735 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8744 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8745 static void __sctp_write_space(struct sctp_association *asoc)
8747 struct sock *sk = asoc->base.sk;
8749 if (sctp_wspace(asoc) <= 0)
8752 if (waitqueue_active(&asoc->wait))
8753 wake_up_interruptible(&asoc->wait);
8755 if (sctp_writeable(sk)) {
8756 struct socket_wq *wq;
8759 wq = rcu_dereference(sk->sk_wq);
8761 if (waitqueue_active(&wq->wait))
8762 wake_up_interruptible(&wq->wait);
8764 /* Note that we try to include the Async I/O support
8765 * here by modeling from the current TCP/UDP code.
8766 * We have not tested with it yet.
8768 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8769 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8775 static void sctp_wake_up_waiters(struct sock *sk,
8776 struct sctp_association *asoc)
8778 struct sctp_association *tmp = asoc;
8780 /* We do accounting for the sndbuf space per association,
8781 * so we only need to wake our own association.
8783 if (asoc->ep->sndbuf_policy)
8784 return __sctp_write_space(asoc);
8786 /* If association goes down and is just flushing its
8787 * outq, then just normally notify others.
8789 if (asoc->base.dead)
8790 return sctp_write_space(sk);
8792 /* Accounting for the sndbuf space is per socket, so we
8793 * need to wake up others, try to be fair and in case of
8794 * other associations, let them have a go first instead
8795 * of just doing a sctp_write_space() call.
8797 * Note that we reach sctp_wake_up_waiters() only when
8798 * associations free up queued chunks, thus we are under
8799 * lock and the list of associations on a socket is
8800 * guaranteed not to change.
8802 for (tmp = list_next_entry(tmp, asocs); 1;
8803 tmp = list_next_entry(tmp, asocs)) {
8804 /* Manually skip the head element. */
8805 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8807 /* Wake up association. */
8808 __sctp_write_space(tmp);
8809 /* We've reached the end. */
8815 /* Do accounting for the sndbuf space.
8816 * Decrement the used sndbuf space of the corresponding association by the
8817 * data size which was just transmitted(freed).
8819 static void sctp_wfree(struct sk_buff *skb)
8821 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8822 struct sctp_association *asoc = chunk->asoc;
8823 struct sock *sk = asoc->base.sk;
8825 sk_mem_uncharge(sk, skb->truesize);
8826 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8827 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8828 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8829 &sk->sk_wmem_alloc));
8832 struct sctp_shared_key *shkey = chunk->shkey;
8834 /* refcnt == 2 and !list_empty mean after this release, it's
8835 * not being used anywhere, and it's time to notify userland
8836 * that this shkey can be freed if it's been deactivated.
8838 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8839 refcount_read(&shkey->refcnt) == 2) {
8840 struct sctp_ulpevent *ev;
8842 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8846 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8848 sctp_auth_shkey_release(chunk->shkey);
8852 sctp_wake_up_waiters(sk, asoc);
8854 sctp_association_put(asoc);
8857 /* Do accounting for the receive space on the socket.
8858 * Accounting for the association is done in ulpevent.c
8859 * We set this as a destructor for the cloned data skbs so that
8860 * accounting is done at the correct time.
8862 void sctp_sock_rfree(struct sk_buff *skb)
8864 struct sock *sk = skb->sk;
8865 struct sctp_ulpevent *event = sctp_skb2event(skb);
8867 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8870 * Mimic the behavior of sock_rfree
8872 sk_mem_uncharge(sk, event->rmem_len);
8876 /* Helper function to wait for space in the sndbuf. */
8877 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8880 struct sock *sk = asoc->base.sk;
8881 long current_timeo = *timeo_p;
8885 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8888 /* Increment the association's refcnt. */
8889 sctp_association_hold(asoc);
8891 /* Wait on the association specific sndbuf space. */
8893 prepare_to_wait_exclusive(&asoc->wait, &wait,
8894 TASK_INTERRUPTIBLE);
8895 if (asoc->base.dead)
8899 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8901 if (signal_pending(current))
8902 goto do_interrupted;
8903 if (sk_under_memory_pressure(sk))
8905 if ((int)msg_len <= sctp_wspace(asoc) &&
8906 sk_wmem_schedule(sk, msg_len))
8909 /* Let another process have a go. Since we are going
8913 current_timeo = schedule_timeout(current_timeo);
8915 if (sk != asoc->base.sk)
8918 *timeo_p = current_timeo;
8922 finish_wait(&asoc->wait, &wait);
8924 /* Release the association's refcnt. */
8925 sctp_association_put(asoc);
8938 err = sock_intr_errno(*timeo_p);
8946 void sctp_data_ready(struct sock *sk)
8948 struct socket_wq *wq;
8951 wq = rcu_dereference(sk->sk_wq);
8952 if (skwq_has_sleeper(wq))
8953 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8954 EPOLLRDNORM | EPOLLRDBAND);
8955 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8959 /* If socket sndbuf has changed, wake up all per association waiters. */
8960 void sctp_write_space(struct sock *sk)
8962 struct sctp_association *asoc;
8964 /* Wake up the tasks in each wait queue. */
8965 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8966 __sctp_write_space(asoc);
8970 /* Is there any sndbuf space available on the socket?
8972 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8973 * associations on the same socket. For a UDP-style socket with
8974 * multiple associations, it is possible for it to be "unwriteable"
8975 * prematurely. I assume that this is acceptable because
8976 * a premature "unwriteable" is better than an accidental "writeable" which
8977 * would cause an unwanted block under certain circumstances. For the 1-1
8978 * UDP-style sockets or TCP-style sockets, this code should work.
8981 static bool sctp_writeable(struct sock *sk)
8983 return sk->sk_sndbuf > sk->sk_wmem_queued;
8986 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8987 * returns immediately with EINPROGRESS.
8989 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8991 struct sock *sk = asoc->base.sk;
8993 long current_timeo = *timeo_p;
8996 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8998 /* Increment the association's refcnt. */
8999 sctp_association_hold(asoc);
9002 prepare_to_wait_exclusive(&asoc->wait, &wait,
9003 TASK_INTERRUPTIBLE);
9006 if (sk->sk_shutdown & RCV_SHUTDOWN)
9008 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9011 if (signal_pending(current))
9012 goto do_interrupted;
9014 if (sctp_state(asoc, ESTABLISHED))
9017 /* Let another process have a go. Since we are going
9021 current_timeo = schedule_timeout(current_timeo);
9024 *timeo_p = current_timeo;
9028 finish_wait(&asoc->wait, &wait);
9030 /* Release the association's refcnt. */
9031 sctp_association_put(asoc);
9036 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9039 err = -ECONNREFUSED;
9043 err = sock_intr_errno(*timeo_p);
9051 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9053 struct sctp_endpoint *ep;
9057 ep = sctp_sk(sk)->ep;
9061 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9062 TASK_INTERRUPTIBLE);
9064 if (list_empty(&ep->asocs)) {
9066 timeo = schedule_timeout(timeo);
9071 if (!sctp_sstate(sk, LISTENING))
9075 if (!list_empty(&ep->asocs))
9078 err = sock_intr_errno(timeo);
9079 if (signal_pending(current))
9087 finish_wait(sk_sleep(sk), &wait);
9092 static void sctp_wait_for_close(struct sock *sk, long timeout)
9097 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9098 if (list_empty(&sctp_sk(sk)->ep->asocs))
9101 timeout = schedule_timeout(timeout);
9103 } while (!signal_pending(current) && timeout);
9105 finish_wait(sk_sleep(sk), &wait);
9108 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9110 struct sk_buff *frag;
9115 /* Don't forget the fragments. */
9116 skb_walk_frags(skb, frag)
9117 sctp_skb_set_owner_r_frag(frag, sk);
9120 sctp_skb_set_owner_r(skb, sk);
9123 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9124 struct sctp_association *asoc)
9126 struct inet_sock *inet = inet_sk(sk);
9127 struct inet_sock *newinet;
9128 struct sctp_sock *sp = sctp_sk(sk);
9129 struct sctp_endpoint *ep = sp->ep;
9131 newsk->sk_type = sk->sk_type;
9132 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9133 newsk->sk_flags = sk->sk_flags;
9134 newsk->sk_tsflags = sk->sk_tsflags;
9135 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9136 newsk->sk_no_check_rx = sk->sk_no_check_rx;
9137 newsk->sk_reuse = sk->sk_reuse;
9138 sctp_sk(newsk)->reuse = sp->reuse;
9140 newsk->sk_shutdown = sk->sk_shutdown;
9141 newsk->sk_destruct = sctp_destruct_sock;
9142 newsk->sk_family = sk->sk_family;
9143 newsk->sk_protocol = IPPROTO_SCTP;
9144 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9145 newsk->sk_sndbuf = sk->sk_sndbuf;
9146 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9147 newsk->sk_lingertime = sk->sk_lingertime;
9148 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9149 newsk->sk_sndtimeo = sk->sk_sndtimeo;
9150 newsk->sk_rxhash = sk->sk_rxhash;
9152 newinet = inet_sk(newsk);
9154 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9155 * getsockname() and getpeername()
9157 newinet->inet_sport = inet->inet_sport;
9158 newinet->inet_saddr = inet->inet_saddr;
9159 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9160 newinet->inet_dport = htons(asoc->peer.port);
9161 newinet->pmtudisc = inet->pmtudisc;
9162 newinet->inet_id = prandom_u32();
9164 newinet->uc_ttl = inet->uc_ttl;
9165 newinet->mc_loop = 1;
9166 newinet->mc_ttl = 1;
9167 newinet->mc_index = 0;
9168 newinet->mc_list = NULL;
9170 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9171 net_enable_timestamp();
9173 /* Set newsk security attributes from orginal sk and connection
9174 * security attribute from ep.
9176 security_sctp_sk_clone(ep, sk, newsk);
9179 static inline void sctp_copy_descendant(struct sock *sk_to,
9180 const struct sock *sk_from)
9182 int ancestor_size = sizeof(struct inet_sock) +
9183 sizeof(struct sctp_sock) -
9184 offsetof(struct sctp_sock, pd_lobby);
9186 if (sk_from->sk_family == PF_INET6)
9187 ancestor_size += sizeof(struct ipv6_pinfo);
9189 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9192 /* Populate the fields of the newsk from the oldsk and migrate the assoc
9193 * and its messages to the newsk.
9195 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9196 struct sctp_association *assoc,
9197 enum sctp_socket_type type)
9199 struct sctp_sock *oldsp = sctp_sk(oldsk);
9200 struct sctp_sock *newsp = sctp_sk(newsk);
9201 struct sctp_bind_bucket *pp; /* hash list port iterator */
9202 struct sctp_endpoint *newep = newsp->ep;
9203 struct sk_buff *skb, *tmp;
9204 struct sctp_ulpevent *event;
9205 struct sctp_bind_hashbucket *head;
9208 /* Migrate socket buffer sizes and all the socket level options to the
9211 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9212 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9213 /* Brute force copy old sctp opt. */
9214 sctp_copy_descendant(newsk, oldsk);
9216 /* Restore the ep value that was overwritten with the above structure
9222 /* Hook this new socket in to the bind_hash list. */
9223 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9224 inet_sk(oldsk)->inet_num)];
9225 spin_lock_bh(&head->lock);
9226 pp = sctp_sk(oldsk)->bind_hash;
9227 sk_add_bind_node(newsk, &pp->owner);
9228 sctp_sk(newsk)->bind_hash = pp;
9229 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9230 spin_unlock_bh(&head->lock);
9232 /* Copy the bind_addr list from the original endpoint to the new
9233 * endpoint so that we can handle restarts properly
9235 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9236 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9240 /* New ep's auth_hmacs should be set if old ep's is set, in case
9241 * that net->sctp.auth_enable has been changed to 0 by users and
9242 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9244 if (oldsp->ep->auth_hmacs) {
9245 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9250 /* Move any messages in the old socket's receive queue that are for the
9251 * peeled off association to the new socket's receive queue.
9253 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9254 event = sctp_skb2event(skb);
9255 if (event->asoc == assoc) {
9256 __skb_unlink(skb, &oldsk->sk_receive_queue);
9257 __skb_queue_tail(&newsk->sk_receive_queue, skb);
9258 sctp_skb_set_owner_r_frag(skb, newsk);
9262 /* Clean up any messages pending delivery due to partial
9263 * delivery. Three cases:
9264 * 1) No partial deliver; no work.
9265 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9266 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9268 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9270 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9271 struct sk_buff_head *queue;
9273 /* Decide which queue to move pd_lobby skbs to. */
9274 if (assoc->ulpq.pd_mode) {
9275 queue = &newsp->pd_lobby;
9277 queue = &newsk->sk_receive_queue;
9279 /* Walk through the pd_lobby, looking for skbs that
9280 * need moved to the new socket.
9282 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9283 event = sctp_skb2event(skb);
9284 if (event->asoc == assoc) {
9285 __skb_unlink(skb, &oldsp->pd_lobby);
9286 __skb_queue_tail(queue, skb);
9287 sctp_skb_set_owner_r_frag(skb, newsk);
9291 /* Clear up any skbs waiting for the partial
9292 * delivery to finish.
9294 if (assoc->ulpq.pd_mode)
9295 sctp_clear_pd(oldsk, NULL);
9299 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9301 /* Set the type of socket to indicate that it is peeled off from the
9302 * original UDP-style socket or created with the accept() call on a
9303 * TCP-style socket..
9307 /* Mark the new socket "in-use" by the user so that any packets
9308 * that may arrive on the association after we've moved it are
9309 * queued to the backlog. This prevents a potential race between
9310 * backlog processing on the old socket and new-packet processing
9311 * on the new socket.
9313 * The caller has just allocated newsk so we can guarantee that other
9314 * paths won't try to lock it and then oldsk.
9316 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9317 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
9318 sctp_assoc_migrate(assoc, newsk);
9319 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
9321 /* If the association on the newsk is already closed before accept()
9322 * is called, set RCV_SHUTDOWN flag.
9324 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9325 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9326 newsk->sk_shutdown |= RCV_SHUTDOWN;
9328 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9331 release_sock(newsk);
9337 /* This proto struct describes the ULP interface for SCTP. */
9338 struct proto sctp_prot = {
9340 .owner = THIS_MODULE,
9341 .close = sctp_close,
9342 .disconnect = sctp_disconnect,
9343 .accept = sctp_accept,
9344 .ioctl = sctp_ioctl,
9345 .init = sctp_init_sock,
9346 .destroy = sctp_destroy_sock,
9347 .shutdown = sctp_shutdown,
9348 .setsockopt = sctp_setsockopt,
9349 .getsockopt = sctp_getsockopt,
9350 .sendmsg = sctp_sendmsg,
9351 .recvmsg = sctp_recvmsg,
9353 .backlog_rcv = sctp_backlog_rcv,
9355 .unhash = sctp_unhash,
9356 .no_autobind = true,
9357 .obj_size = sizeof(struct sctp_sock),
9358 .useroffset = offsetof(struct sctp_sock, subscribe),
9359 .usersize = offsetof(struct sctp_sock, initmsg) -
9360 offsetof(struct sctp_sock, subscribe) +
9361 sizeof_field(struct sctp_sock, initmsg),
9362 .sysctl_mem = sysctl_sctp_mem,
9363 .sysctl_rmem = sysctl_sctp_rmem,
9364 .sysctl_wmem = sysctl_sctp_wmem,
9365 .memory_pressure = &sctp_memory_pressure,
9366 .enter_memory_pressure = sctp_enter_memory_pressure,
9367 .memory_allocated = &sctp_memory_allocated,
9368 .sockets_allocated = &sctp_sockets_allocated,
9371 #if IS_ENABLED(CONFIG_IPV6)
9373 #include <net/transp_v6.h>
9374 static void sctp_v6_destroy_sock(struct sock *sk)
9376 sctp_destroy_sock(sk);
9377 inet6_destroy_sock(sk);
9380 struct proto sctpv6_prot = {
9382 .owner = THIS_MODULE,
9383 .close = sctp_close,
9384 .disconnect = sctp_disconnect,
9385 .accept = sctp_accept,
9386 .ioctl = sctp_ioctl,
9387 .init = sctp_init_sock,
9388 .destroy = sctp_v6_destroy_sock,
9389 .shutdown = sctp_shutdown,
9390 .setsockopt = sctp_setsockopt,
9391 .getsockopt = sctp_getsockopt,
9392 .sendmsg = sctp_sendmsg,
9393 .recvmsg = sctp_recvmsg,
9395 .backlog_rcv = sctp_backlog_rcv,
9397 .unhash = sctp_unhash,
9398 .no_autobind = true,
9399 .obj_size = sizeof(struct sctp6_sock),
9400 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9401 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9402 offsetof(struct sctp6_sock, sctp.subscribe) +
9403 sizeof_field(struct sctp6_sock, sctp.initmsg),
9404 .sysctl_mem = sysctl_sctp_mem,
9405 .sysctl_rmem = sysctl_sctp_rmem,
9406 .sysctl_wmem = sysctl_sctp_wmem,
9407 .memory_pressure = &sctp_memory_pressure,
9408 .enter_memory_pressure = sctp_enter_memory_pressure,
9409 .memory_allocated = &sctp_memory_allocated,
9410 .sockets_allocated = &sctp_sockets_allocated,
9412 #endif /* IS_ENABLED(CONFIG_IPV6) */