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
23 * Written or modified by:
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 long 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 ((ret = sctp_get_port_local(sk, addr))) {
406 /* Refresh ephemeral port. */
408 bp->port = inet_sk(sk)->inet_num;
410 /* Add the address to the bind address list.
411 * Use GFP_ATOMIC since BHs will be disabled.
413 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
414 SCTP_ADDR_SRC, GFP_ATOMIC);
416 /* Copy back into socket for getsockname() use. */
418 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
419 sp->pf->to_sk_saddr(addr, sk);
425 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
427 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
428 * at any one time. If a sender, after sending an ASCONF chunk, decides
429 * it needs to transfer another ASCONF Chunk, it MUST wait until the
430 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
431 * subsequent ASCONF. Note this restriction binds each side, so at any
432 * time two ASCONF may be in-transit on any given association (one sent
433 * from each endpoint).
435 static int sctp_send_asconf(struct sctp_association *asoc,
436 struct sctp_chunk *chunk)
438 struct net *net = sock_net(asoc->base.sk);
441 /* If there is an outstanding ASCONF chunk, queue it for later
444 if (asoc->addip_last_asconf) {
445 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
449 /* Hold the chunk until an ASCONF_ACK is received. */
450 sctp_chunk_hold(chunk);
451 retval = sctp_primitive_ASCONF(net, asoc, chunk);
453 sctp_chunk_free(chunk);
455 asoc->addip_last_asconf = chunk;
461 /* Add a list of addresses as bind addresses to local endpoint or
464 * Basically run through each address specified in the addrs/addrcnt
465 * array/length pair, determine if it is IPv6 or IPv4 and call
466 * sctp_do_bind() on it.
468 * If any of them fails, then the operation will be reversed and the
469 * ones that were added will be removed.
471 * Only sctp_setsockopt_bindx() is supposed to call this function.
473 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
478 struct sockaddr *sa_addr;
481 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
485 for (cnt = 0; cnt < addrcnt; cnt++) {
486 /* The list may contain either IPv4 or IPv6 address;
487 * determine the address length for walking thru the list.
490 af = sctp_get_af_specific(sa_addr->sa_family);
496 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
499 addr_buf += af->sockaddr_len;
503 /* Failed. Cleanup the ones that have been added */
505 sctp_bindx_rem(sk, addrs, cnt);
513 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
514 * associations that are part of the endpoint indicating that a list of local
515 * addresses are added to the endpoint.
517 * If any of the addresses is already in the bind address list of the
518 * association, we do not send the chunk for that association. But it will not
519 * affect other associations.
521 * Only sctp_setsockopt_bindx() is supposed to call this function.
523 static int sctp_send_asconf_add_ip(struct sock *sk,
524 struct sockaddr *addrs,
527 struct net *net = sock_net(sk);
528 struct sctp_sock *sp;
529 struct sctp_endpoint *ep;
530 struct sctp_association *asoc;
531 struct sctp_bind_addr *bp;
532 struct sctp_chunk *chunk;
533 struct sctp_sockaddr_entry *laddr;
534 union sctp_addr *addr;
535 union sctp_addr saveaddr;
542 if (!net->sctp.addip_enable)
548 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
549 __func__, sk, addrs, addrcnt);
551 list_for_each_entry(asoc, &ep->asocs, asocs) {
552 if (!asoc->peer.asconf_capable)
555 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
558 if (!sctp_state(asoc, ESTABLISHED))
561 /* Check if any address in the packed array of addresses is
562 * in the bind address list of the association. If so,
563 * do not send the asconf chunk to its peer, but continue with
564 * other associations.
567 for (i = 0; i < addrcnt; i++) {
569 af = sctp_get_af_specific(addr->v4.sin_family);
575 if (sctp_assoc_lookup_laddr(asoc, addr))
578 addr_buf += af->sockaddr_len;
583 /* Use the first valid address in bind addr list of
584 * association as Address Parameter of ASCONF CHUNK.
586 bp = &asoc->base.bind_addr;
587 p = bp->address_list.next;
588 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
589 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
590 addrcnt, SCTP_PARAM_ADD_IP);
596 /* Add the new addresses to the bind address list with
597 * use_as_src set to 0.
600 for (i = 0; i < addrcnt; i++) {
602 af = sctp_get_af_specific(addr->v4.sin_family);
603 memcpy(&saveaddr, addr, af->sockaddr_len);
604 retval = sctp_add_bind_addr(bp, &saveaddr,
606 SCTP_ADDR_NEW, GFP_ATOMIC);
607 addr_buf += af->sockaddr_len;
609 if (asoc->src_out_of_asoc_ok) {
610 struct sctp_transport *trans;
612 list_for_each_entry(trans,
613 &asoc->peer.transport_addr_list, transports) {
614 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
615 2*asoc->pathmtu, 4380));
616 trans->ssthresh = asoc->peer.i.a_rwnd;
617 trans->rto = asoc->rto_initial;
618 sctp_max_rto(asoc, trans);
619 trans->rtt = trans->srtt = trans->rttvar = 0;
620 /* Clear the source and route cache */
621 sctp_transport_route(trans, NULL,
622 sctp_sk(asoc->base.sk));
625 retval = sctp_send_asconf(asoc, chunk);
632 /* Remove a list of addresses from bind addresses list. Do not remove the
635 * Basically run through each address specified in the addrs/addrcnt
636 * array/length pair, determine if it is IPv6 or IPv4 and call
637 * sctp_del_bind() on it.
639 * If any of them fails, then the operation will be reversed and the
640 * ones that were removed will be added back.
642 * At least one address has to be left; if only one address is
643 * available, the operation will return -EBUSY.
645 * Only sctp_setsockopt_bindx() is supposed to call this function.
647 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
649 struct sctp_sock *sp = sctp_sk(sk);
650 struct sctp_endpoint *ep = sp->ep;
652 struct sctp_bind_addr *bp = &ep->base.bind_addr;
655 union sctp_addr *sa_addr;
658 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
659 __func__, sk, addrs, addrcnt);
662 for (cnt = 0; cnt < addrcnt; cnt++) {
663 /* If the bind address list is empty or if there is only one
664 * bind address, there is nothing more to be removed (we need
665 * at least one address here).
667 if (list_empty(&bp->address_list) ||
668 (sctp_list_single_entry(&bp->address_list))) {
674 af = sctp_get_af_specific(sa_addr->sa.sa_family);
680 if (!af->addr_valid(sa_addr, sp, NULL)) {
681 retval = -EADDRNOTAVAIL;
685 if (sa_addr->v4.sin_port &&
686 sa_addr->v4.sin_port != htons(bp->port)) {
691 if (!sa_addr->v4.sin_port)
692 sa_addr->v4.sin_port = htons(bp->port);
694 /* FIXME - There is probably a need to check if sk->sk_saddr and
695 * sk->sk_rcv_addr are currently set to one of the addresses to
696 * be removed. This is something which needs to be looked into
697 * when we are fixing the outstanding issues with multi-homing
698 * socket routing and failover schemes. Refer to comments in
699 * sctp_do_bind(). -daisy
701 retval = sctp_del_bind_addr(bp, sa_addr);
703 addr_buf += af->sockaddr_len;
706 /* Failed. Add the ones that has been removed back */
708 sctp_bindx_add(sk, addrs, cnt);
716 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
717 * the associations that are part of the endpoint indicating that a list of
718 * local addresses are removed from the endpoint.
720 * If any of the addresses is already in the bind address list of the
721 * association, we do not send the chunk for that association. But it will not
722 * affect other associations.
724 * Only sctp_setsockopt_bindx() is supposed to call this function.
726 static int sctp_send_asconf_del_ip(struct sock *sk,
727 struct sockaddr *addrs,
730 struct net *net = sock_net(sk);
731 struct sctp_sock *sp;
732 struct sctp_endpoint *ep;
733 struct sctp_association *asoc;
734 struct sctp_transport *transport;
735 struct sctp_bind_addr *bp;
736 struct sctp_chunk *chunk;
737 union sctp_addr *laddr;
740 struct sctp_sockaddr_entry *saddr;
746 if (!net->sctp.addip_enable)
752 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
753 __func__, sk, addrs, addrcnt);
755 list_for_each_entry(asoc, &ep->asocs, asocs) {
757 if (!asoc->peer.asconf_capable)
760 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
763 if (!sctp_state(asoc, ESTABLISHED))
766 /* Check if any address in the packed array of addresses is
767 * not present in the bind address list of the association.
768 * If so, do not send the asconf chunk to its peer, but
769 * continue with other associations.
772 for (i = 0; i < addrcnt; i++) {
774 af = sctp_get_af_specific(laddr->v4.sin_family);
780 if (!sctp_assoc_lookup_laddr(asoc, laddr))
783 addr_buf += af->sockaddr_len;
788 /* Find one address in the association's bind address list
789 * that is not in the packed array of addresses. This is to
790 * make sure that we do not delete all the addresses in the
793 bp = &asoc->base.bind_addr;
794 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
796 if ((laddr == NULL) && (addrcnt == 1)) {
797 if (asoc->asconf_addr_del_pending)
799 asoc->asconf_addr_del_pending =
800 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
801 if (asoc->asconf_addr_del_pending == NULL) {
805 asoc->asconf_addr_del_pending->sa.sa_family =
807 asoc->asconf_addr_del_pending->v4.sin_port =
809 if (addrs->sa_family == AF_INET) {
810 struct sockaddr_in *sin;
812 sin = (struct sockaddr_in *)addrs;
813 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
814 } else if (addrs->sa_family == AF_INET6) {
815 struct sockaddr_in6 *sin6;
817 sin6 = (struct sockaddr_in6 *)addrs;
818 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
821 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
822 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
823 asoc->asconf_addr_del_pending);
825 asoc->src_out_of_asoc_ok = 1;
833 /* We do not need RCU protection throughout this loop
834 * because this is done under a socket lock from the
837 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
845 /* Reset use_as_src flag for the addresses in the bind address
846 * list that are to be deleted.
849 for (i = 0; i < addrcnt; i++) {
851 af = sctp_get_af_specific(laddr->v4.sin_family);
852 list_for_each_entry(saddr, &bp->address_list, list) {
853 if (sctp_cmp_addr_exact(&saddr->a, laddr))
854 saddr->state = SCTP_ADDR_DEL;
856 addr_buf += af->sockaddr_len;
859 /* Update the route and saddr entries for all the transports
860 * as some of the addresses in the bind address list are
861 * about to be deleted and cannot be used as source addresses.
863 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
865 sctp_transport_route(transport, NULL,
866 sctp_sk(asoc->base.sk));
870 /* We don't need to transmit ASCONF */
872 retval = sctp_send_asconf(asoc, chunk);
878 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
879 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
881 struct sock *sk = sctp_opt2sk(sp);
882 union sctp_addr *addr;
885 /* It is safe to write port space in caller. */
887 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
888 af = sctp_get_af_specific(addr->sa.sa_family);
891 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
894 if (addrw->state == SCTP_ADDR_NEW)
895 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
897 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
900 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
903 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
906 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
907 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
910 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
911 * Section 3.1.2 for this usage.
913 * addrs is a pointer to an array of one or more socket addresses. Each
914 * address is contained in its appropriate structure (i.e. struct
915 * sockaddr_in or struct sockaddr_in6) the family of the address type
916 * must be used to distinguish the address length (note that this
917 * representation is termed a "packed array" of addresses). The caller
918 * specifies the number of addresses in the array with addrcnt.
920 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
921 * -1, and sets errno to the appropriate error code.
923 * For SCTP, the port given in each socket address must be the same, or
924 * sctp_bindx() will fail, setting errno to EINVAL.
926 * The flags parameter is formed from the bitwise OR of zero or more of
927 * the following currently defined flags:
929 * SCTP_BINDX_ADD_ADDR
931 * SCTP_BINDX_REM_ADDR
933 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
934 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
935 * addresses from the association. The two flags are mutually exclusive;
936 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
937 * not remove all addresses from an association; sctp_bindx() will
938 * reject such an attempt with EINVAL.
940 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
941 * additional addresses with an endpoint after calling bind(). Or use
942 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
943 * socket is associated with so that no new association accepted will be
944 * associated with those addresses. If the endpoint supports dynamic
945 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
946 * endpoint to send the appropriate message to the peer to change the
947 * peers address lists.
949 * Adding and removing addresses from a connected association is
950 * optional functionality. Implementations that do not support this
951 * functionality should return EOPNOTSUPP.
953 * Basically do nothing but copying the addresses from user to kernel
954 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
955 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
958 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
961 * sk The sk of the socket
962 * addrs The pointer to the addresses in user land
963 * addrssize Size of the addrs buffer
964 * op Operation to perform (add or remove, see the flags of
967 * Returns 0 if ok, <0 errno code on error.
969 static int sctp_setsockopt_bindx(struct sock *sk,
970 struct sockaddr __user *addrs,
971 int addrs_size, int op)
973 struct sockaddr *kaddrs;
977 struct sockaddr *sa_addr;
981 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
982 __func__, sk, addrs, addrs_size, op);
984 if (unlikely(addrs_size <= 0))
987 kaddrs = memdup_user(addrs, addrs_size);
988 if (unlikely(IS_ERR(kaddrs)))
989 return PTR_ERR(kaddrs);
991 /* Walk through the addrs buffer and count the number of addresses. */
993 while (walk_size < addrs_size) {
994 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1000 af = sctp_get_af_specific(sa_addr->sa_family);
1002 /* If the address family is not supported or if this address
1003 * causes the address buffer to overflow return EINVAL.
1005 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1010 addr_buf += af->sockaddr_len;
1011 walk_size += af->sockaddr_len;
1016 case SCTP_BINDX_ADD_ADDR:
1017 /* Allow security module to validate bindx addresses. */
1018 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1019 (struct sockaddr *)kaddrs,
1023 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1026 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1029 case SCTP_BINDX_REM_ADDR:
1030 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1033 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1047 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1049 * Common routine for handling connect() and sctp_connectx().
1050 * Connect will come in with just a single address.
1052 static int __sctp_connect(struct sock *sk,
1053 struct sockaddr *kaddrs,
1054 int addrs_size, int flags,
1055 sctp_assoc_t *assoc_id)
1057 struct net *net = sock_net(sk);
1058 struct sctp_sock *sp;
1059 struct sctp_endpoint *ep;
1060 struct sctp_association *asoc = NULL;
1061 struct sctp_association *asoc2;
1062 struct sctp_transport *transport;
1064 enum sctp_scope scope;
1069 union sctp_addr *sa_addr = NULL;
1071 unsigned short port;
1076 /* connect() cannot be done on a socket that is already in ESTABLISHED
1077 * state - UDP-style peeled off socket or a TCP-style socket that
1078 * is already connected.
1079 * It cannot be done even on a TCP-style listening socket.
1081 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1082 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1087 /* Walk through the addrs buffer and count the number of addresses. */
1089 while (walk_size < addrs_size) {
1092 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1098 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1100 /* If the address family is not supported or if this address
1101 * causes the address buffer to overflow return EINVAL.
1103 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1108 port = ntohs(sa_addr->v4.sin_port);
1110 /* Save current address so we can work with it */
1111 memcpy(&to, sa_addr, af->sockaddr_len);
1113 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1117 /* Make sure the destination port is correctly set
1120 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1125 /* Check if there already is a matching association on the
1126 * endpoint (other than the one created here).
1128 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1129 if (asoc2 && asoc2 != asoc) {
1130 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1137 /* If we could not find a matching association on the endpoint,
1138 * make sure that there is no peeled-off association matching
1139 * the peer address even on another socket.
1141 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1142 err = -EADDRNOTAVAIL;
1147 /* If a bind() or sctp_bindx() is not called prior to
1148 * an sctp_connectx() call, the system picks an
1149 * ephemeral port and will choose an address set
1150 * equivalent to binding with a wildcard address.
1152 if (!ep->base.bind_addr.port) {
1153 if (sctp_autobind(sk)) {
1159 * If an unprivileged user inherits a 1-many
1160 * style socket with open associations on a
1161 * privileged port, it MAY be permitted to
1162 * accept new associations, but it SHOULD NOT
1163 * be permitted to open new associations.
1165 if (ep->base.bind_addr.port <
1166 inet_prot_sock(net) &&
1167 !ns_capable(net->user_ns,
1168 CAP_NET_BIND_SERVICE)) {
1174 scope = sctp_scope(&to);
1175 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1181 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1189 /* Prime the peer's transport structures. */
1190 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1198 addr_buf += af->sockaddr_len;
1199 walk_size += af->sockaddr_len;
1202 /* In case the user of sctp_connectx() wants an association
1203 * id back, assign one now.
1206 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1211 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1216 /* Initialize sk's dport and daddr for getpeername() */
1217 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1218 sp->pf->to_sk_daddr(sa_addr, sk);
1221 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1224 *assoc_id = asoc->assoc_id;
1226 err = sctp_wait_for_connect(asoc, &timeo);
1227 /* Note: the asoc may be freed after the return of
1228 * sctp_wait_for_connect.
1231 /* Don't free association on exit. */
1235 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1236 __func__, asoc, kaddrs, err);
1239 /* sctp_primitive_ASSOCIATE may have added this association
1240 * To the hash table, try to unhash it, just in case, its a noop
1241 * if it wasn't hashed so we're safe
1243 sctp_association_free(asoc);
1248 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1251 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1252 * sctp_assoc_t *asoc);
1254 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1255 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1256 * or IPv6 addresses.
1258 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1259 * Section 3.1.2 for this usage.
1261 * addrs is a pointer to an array of one or more socket addresses. Each
1262 * address is contained in its appropriate structure (i.e. struct
1263 * sockaddr_in or struct sockaddr_in6) the family of the address type
1264 * must be used to distengish the address length (note that this
1265 * representation is termed a "packed array" of addresses). The caller
1266 * specifies the number of addresses in the array with addrcnt.
1268 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1269 * the association id of the new association. On failure, sctp_connectx()
1270 * returns -1, and sets errno to the appropriate error code. The assoc_id
1271 * is not touched by the kernel.
1273 * For SCTP, the port given in each socket address must be the same, or
1274 * sctp_connectx() will fail, setting errno to EINVAL.
1276 * An application can use sctp_connectx to initiate an association with
1277 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1278 * allows a caller to specify multiple addresses at which a peer can be
1279 * reached. The way the SCTP stack uses the list of addresses to set up
1280 * the association is implementation dependent. This function only
1281 * specifies that the stack will try to make use of all the addresses in
1282 * the list when needed.
1284 * Note that the list of addresses passed in is only used for setting up
1285 * the association. It does not necessarily equal the set of addresses
1286 * the peer uses for the resulting association. If the caller wants to
1287 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1288 * retrieve them after the association has been set up.
1290 * Basically do nothing but copying the addresses from user to kernel
1291 * land and invoking either sctp_connectx(). This is used for tunneling
1292 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1294 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1297 * sk The sk of the socket
1298 * addrs The pointer to the addresses in user land
1299 * addrssize Size of the addrs buffer
1301 * Returns >=0 if ok, <0 errno code on error.
1303 static int __sctp_setsockopt_connectx(struct sock *sk,
1304 struct sockaddr __user *addrs,
1306 sctp_assoc_t *assoc_id)
1308 struct sockaddr *kaddrs;
1309 int err = 0, flags = 0;
1311 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1312 __func__, sk, addrs, addrs_size);
1314 if (unlikely(addrs_size <= 0))
1317 kaddrs = memdup_user(addrs, addrs_size);
1318 if (unlikely(IS_ERR(kaddrs)))
1319 return PTR_ERR(kaddrs);
1321 /* Allow security module to validate connectx addresses. */
1322 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1323 (struct sockaddr *)kaddrs,
1328 /* in-kernel sockets don't generally have a file allocated to them
1329 * if all they do is call sock_create_kern().
1331 if (sk->sk_socket->file)
1332 flags = sk->sk_socket->file->f_flags;
1334 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1343 * This is an older interface. It's kept for backward compatibility
1344 * to the option that doesn't provide association id.
1346 static int sctp_setsockopt_connectx_old(struct sock *sk,
1347 struct sockaddr __user *addrs,
1350 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1354 * New interface for the API. The since the API is done with a socket
1355 * option, to make it simple we feed back the association id is as a return
1356 * indication to the call. Error is always negative and association id is
1359 static int sctp_setsockopt_connectx(struct sock *sk,
1360 struct sockaddr __user *addrs,
1363 sctp_assoc_t assoc_id = 0;
1366 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1375 * New (hopefully final) interface for the API.
1376 * We use the sctp_getaddrs_old structure so that use-space library
1377 * can avoid any unnecessary allocations. The only different part
1378 * is that we store the actual length of the address buffer into the
1379 * addrs_num structure member. That way we can re-use the existing
1382 #ifdef CONFIG_COMPAT
1383 struct compat_sctp_getaddrs_old {
1384 sctp_assoc_t assoc_id;
1386 compat_uptr_t addrs; /* struct sockaddr * */
1390 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1391 char __user *optval,
1394 struct sctp_getaddrs_old param;
1395 sctp_assoc_t assoc_id = 0;
1398 #ifdef CONFIG_COMPAT
1399 if (in_compat_syscall()) {
1400 struct compat_sctp_getaddrs_old param32;
1402 if (len < sizeof(param32))
1404 if (copy_from_user(¶m32, optval, sizeof(param32)))
1407 param.assoc_id = param32.assoc_id;
1408 param.addr_num = param32.addr_num;
1409 param.addrs = compat_ptr(param32.addrs);
1413 if (len < sizeof(param))
1415 if (copy_from_user(¶m, optval, sizeof(param)))
1419 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1420 param.addrs, param.addr_num,
1422 if (err == 0 || err == -EINPROGRESS) {
1423 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1425 if (put_user(sizeof(assoc_id), optlen))
1432 /* API 3.1.4 close() - UDP Style Syntax
1433 * Applications use close() to perform graceful shutdown (as described in
1434 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1435 * by a UDP-style socket.
1439 * ret = close(int sd);
1441 * sd - the socket descriptor of the associations to be closed.
1443 * To gracefully shutdown a specific association represented by the
1444 * UDP-style socket, an application should use the sendmsg() call,
1445 * passing no user data, but including the appropriate flag in the
1446 * ancillary data (see Section xxxx).
1448 * If sd in the close() call is a branched-off socket representing only
1449 * one association, the shutdown is performed on that association only.
1451 * 4.1.6 close() - TCP Style Syntax
1453 * Applications use close() to gracefully close down an association.
1457 * int close(int sd);
1459 * sd - the socket descriptor of the association to be closed.
1461 * After an application calls close() on a socket descriptor, no further
1462 * socket operations will succeed on that descriptor.
1464 * API 7.1.4 SO_LINGER
1466 * An application using the TCP-style socket can use this option to
1467 * perform the SCTP ABORT primitive. The linger option structure is:
1470 * int l_onoff; // option on/off
1471 * int l_linger; // linger time
1474 * To enable the option, set l_onoff to 1. If the l_linger value is set
1475 * to 0, calling close() is the same as the ABORT primitive. If the
1476 * value is set to a negative value, the setsockopt() call will return
1477 * an error. If the value is set to a positive value linger_time, the
1478 * close() can be blocked for at most linger_time ms. If the graceful
1479 * shutdown phase does not finish during this period, close() will
1480 * return but the graceful shutdown phase continues in the system.
1482 static void sctp_close(struct sock *sk, long timeout)
1484 struct net *net = sock_net(sk);
1485 struct sctp_endpoint *ep;
1486 struct sctp_association *asoc;
1487 struct list_head *pos, *temp;
1488 unsigned int data_was_unread;
1490 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1492 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1493 sk->sk_shutdown = SHUTDOWN_MASK;
1494 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1496 ep = sctp_sk(sk)->ep;
1498 /* Clean up any skbs sitting on the receive queue. */
1499 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1500 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1502 /* Walk all associations on an endpoint. */
1503 list_for_each_safe(pos, temp, &ep->asocs) {
1504 asoc = list_entry(pos, struct sctp_association, asocs);
1506 if (sctp_style(sk, TCP)) {
1507 /* A closed association can still be in the list if
1508 * it belongs to a TCP-style listening socket that is
1509 * not yet accepted. If so, free it. If not, send an
1510 * ABORT or SHUTDOWN based on the linger options.
1512 if (sctp_state(asoc, CLOSED)) {
1513 sctp_association_free(asoc);
1518 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1519 !skb_queue_empty(&asoc->ulpq.reasm) ||
1520 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1521 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1522 struct sctp_chunk *chunk;
1524 chunk = sctp_make_abort_user(asoc, NULL, 0);
1525 sctp_primitive_ABORT(net, asoc, chunk);
1527 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1530 /* On a TCP-style socket, block for at most linger_time if set. */
1531 if (sctp_style(sk, TCP) && timeout)
1532 sctp_wait_for_close(sk, timeout);
1534 /* This will run the backlog queue. */
1537 /* Supposedly, no process has access to the socket, but
1538 * the net layers still may.
1539 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1540 * held and that should be grabbed before socket lock.
1542 spin_lock_bh(&net->sctp.addr_wq_lock);
1543 bh_lock_sock_nested(sk);
1545 /* Hold the sock, since sk_common_release() will put sock_put()
1546 * and we have just a little more cleanup.
1549 sk_common_release(sk);
1552 spin_unlock_bh(&net->sctp.addr_wq_lock);
1556 SCTP_DBG_OBJCNT_DEC(sock);
1559 /* Handle EPIPE error. */
1560 static int sctp_error(struct sock *sk, int flags, int err)
1563 err = sock_error(sk) ? : -EPIPE;
1564 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1565 send_sig(SIGPIPE, current, 0);
1569 /* API 3.1.3 sendmsg() - UDP Style Syntax
1571 * An application uses sendmsg() and recvmsg() calls to transmit data to
1572 * and receive data from its peer.
1574 * ssize_t sendmsg(int socket, const struct msghdr *message,
1577 * socket - the socket descriptor of the endpoint.
1578 * message - pointer to the msghdr structure which contains a single
1579 * user message and possibly some ancillary data.
1581 * See Section 5 for complete description of the data
1584 * flags - flags sent or received with the user message, see Section
1585 * 5 for complete description of the flags.
1587 * Note: This function could use a rewrite especially when explicit
1588 * connect support comes in.
1590 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1592 static int sctp_msghdr_parse(const struct msghdr *msg,
1593 struct sctp_cmsgs *cmsgs);
1595 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1596 struct sctp_sndrcvinfo *srinfo,
1597 const struct msghdr *msg, size_t msg_len)
1602 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1605 if (msg_len > sk->sk_sndbuf)
1608 memset(cmsgs, 0, sizeof(*cmsgs));
1609 err = sctp_msghdr_parse(msg, cmsgs);
1611 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1615 memset(srinfo, 0, sizeof(*srinfo));
1616 if (cmsgs->srinfo) {
1617 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1618 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1619 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1620 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1621 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1622 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1626 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1627 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1628 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1629 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1630 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1633 if (cmsgs->prinfo) {
1634 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1635 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1636 cmsgs->prinfo->pr_policy);
1639 sflags = srinfo->sinfo_flags;
1640 if (!sflags && msg_len)
1643 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1646 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1647 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1650 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1656 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1657 struct sctp_cmsgs *cmsgs,
1658 union sctp_addr *daddr,
1659 struct sctp_transport **tp)
1661 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1662 struct net *net = sock_net(sk);
1663 struct sctp_association *asoc;
1664 enum sctp_scope scope;
1665 struct cmsghdr *cmsg;
1666 __be32 flowinfo = 0;
1672 if (sflags & (SCTP_EOF | SCTP_ABORT))
1675 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1676 sctp_sstate(sk, CLOSING)))
1677 return -EADDRNOTAVAIL;
1679 if (sctp_endpoint_is_peeled_off(ep, daddr))
1680 return -EADDRNOTAVAIL;
1682 if (!ep->base.bind_addr.port) {
1683 if (sctp_autobind(sk))
1686 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1687 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1691 scope = sctp_scope(daddr);
1693 /* Label connection socket for first association 1-to-many
1694 * style for client sequence socket()->sendmsg(). This
1695 * needs to be done before sctp_assoc_add_peer() as that will
1696 * set up the initial packet that needs to account for any
1697 * security ip options (CIPSO/CALIPSO) added to the packet.
1699 af = sctp_get_af_specific(daddr->sa.sa_family);
1702 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1703 (struct sockaddr *)daddr,
1708 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1712 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1718 struct sctp_initmsg *init = cmsgs->init;
1720 if (init->sinit_num_ostreams) {
1721 __u16 outcnt = init->sinit_num_ostreams;
1723 asoc->c.sinit_num_ostreams = outcnt;
1724 /* outcnt has been changed, need to re-init stream */
1725 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1731 if (init->sinit_max_instreams)
1732 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1734 if (init->sinit_max_attempts)
1735 asoc->max_init_attempts = init->sinit_max_attempts;
1737 if (init->sinit_max_init_timeo)
1738 asoc->max_init_timeo =
1739 msecs_to_jiffies(init->sinit_max_init_timeo);
1742 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1748 if (!cmsgs->addrs_msg)
1751 if (daddr->sa.sa_family == AF_INET6)
1752 flowinfo = daddr->v6.sin6_flowinfo;
1754 /* sendv addr list parse */
1755 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1756 struct sctp_transport *transport;
1757 struct sctp_association *old;
1758 union sctp_addr _daddr;
1761 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1762 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1763 cmsg->cmsg_type != SCTP_DSTADDRV6))
1767 memset(daddr, 0, sizeof(*daddr));
1768 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1769 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1770 if (dlen < sizeof(struct in_addr)) {
1775 dlen = sizeof(struct in_addr);
1776 daddr->v4.sin_family = AF_INET;
1777 daddr->v4.sin_port = htons(asoc->peer.port);
1778 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1780 if (dlen < sizeof(struct in6_addr)) {
1785 dlen = sizeof(struct in6_addr);
1786 daddr->v6.sin6_flowinfo = flowinfo;
1787 daddr->v6.sin6_family = AF_INET6;
1788 daddr->v6.sin6_port = htons(asoc->peer.port);
1789 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1791 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1795 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1796 if (old && old != asoc) {
1797 if (old->state >= SCTP_STATE_ESTABLISHED)
1804 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1805 err = -EADDRNOTAVAIL;
1809 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1820 sctp_association_free(asoc);
1824 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1825 __u16 sflags, struct msghdr *msg,
1828 struct sock *sk = asoc->base.sk;
1829 struct net *net = sock_net(sk);
1831 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1834 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1835 !sctp_state(asoc, ESTABLISHED))
1838 if (sflags & SCTP_EOF) {
1839 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1840 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1845 if (sflags & SCTP_ABORT) {
1846 struct sctp_chunk *chunk;
1848 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1852 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1853 sctp_primitive_ABORT(net, asoc, chunk);
1854 iov_iter_revert(&msg->msg_iter, msg_len);
1862 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1863 struct msghdr *msg, size_t msg_len,
1864 struct sctp_transport *transport,
1865 struct sctp_sndrcvinfo *sinfo)
1867 struct sock *sk = asoc->base.sk;
1868 struct sctp_sock *sp = sctp_sk(sk);
1869 struct net *net = sock_net(sk);
1870 struct sctp_datamsg *datamsg;
1871 bool wait_connect = false;
1872 struct sctp_chunk *chunk;
1876 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1881 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1882 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1887 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1892 if (asoc->pmtu_pending) {
1893 if (sp->param_flags & SPP_PMTUD_ENABLE)
1894 sctp_assoc_sync_pmtu(asoc);
1895 asoc->pmtu_pending = 0;
1898 if (sctp_wspace(asoc) < (int)msg_len)
1899 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1901 if (sk_under_memory_pressure(sk))
1904 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1905 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1906 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1911 if (sctp_state(asoc, CLOSED)) {
1912 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1916 if (sp->strm_interleave) {
1917 timeo = sock_sndtimeo(sk, 0);
1918 err = sctp_wait_for_connect(asoc, &timeo);
1924 wait_connect = true;
1927 pr_debug("%s: we associated primitively\n", __func__);
1930 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1931 if (IS_ERR(datamsg)) {
1932 err = PTR_ERR(datamsg);
1936 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1938 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1939 sctp_chunk_hold(chunk);
1940 sctp_set_owner_w(chunk);
1941 chunk->transport = transport;
1944 err = sctp_primitive_SEND(net, asoc, datamsg);
1946 sctp_datamsg_free(datamsg);
1950 pr_debug("%s: we sent primitively\n", __func__);
1952 sctp_datamsg_put(datamsg);
1954 if (unlikely(wait_connect)) {
1955 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1956 sctp_wait_for_connect(asoc, &timeo);
1965 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1966 const struct msghdr *msg,
1967 struct sctp_cmsgs *cmsgs)
1969 union sctp_addr *daddr = NULL;
1972 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1973 int len = msg->msg_namelen;
1975 if (len > sizeof(*daddr))
1976 len = sizeof(*daddr);
1978 daddr = (union sctp_addr *)msg->msg_name;
1980 err = sctp_verify_addr(sk, daddr, len);
1982 return ERR_PTR(err);
1988 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1989 struct sctp_sndrcvinfo *sinfo,
1990 struct sctp_cmsgs *cmsgs)
1992 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1993 sinfo->sinfo_stream = asoc->default_stream;
1994 sinfo->sinfo_ppid = asoc->default_ppid;
1995 sinfo->sinfo_context = asoc->default_context;
1996 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1999 sinfo->sinfo_flags = asoc->default_flags;
2002 if (!cmsgs->srinfo && !cmsgs->prinfo)
2003 sinfo->sinfo_timetolive = asoc->default_timetolive;
2005 if (cmsgs->authinfo) {
2006 /* Reuse sinfo_tsn to indicate that authinfo was set and
2007 * sinfo_ssn to save the keyid on tx path.
2009 sinfo->sinfo_tsn = 1;
2010 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2014 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2016 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2017 struct sctp_transport *transport = NULL;
2018 struct sctp_sndrcvinfo _sinfo, *sinfo;
2019 struct sctp_association *asoc, *tmp;
2020 struct sctp_cmsgs cmsgs;
2021 union sctp_addr *daddr;
2026 /* Parse and get snd_info */
2027 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2032 sflags = sinfo->sinfo_flags;
2034 /* Get daddr from msg */
2035 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2036 if (IS_ERR(daddr)) {
2037 err = PTR_ERR(daddr);
2043 /* SCTP_SENDALL process */
2044 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2045 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
2046 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2053 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2055 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2060 iov_iter_revert(&msg->msg_iter, err);
2066 /* Get and check or create asoc */
2068 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2070 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2075 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2080 asoc = transport->asoc;
2084 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2087 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2093 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2098 /* Update snd_info with the asoc */
2099 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2101 /* Send msg to the asoc */
2102 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2103 if (err < 0 && err != -ESRCH && new)
2104 sctp_association_free(asoc);
2109 return sctp_error(sk, msg->msg_flags, err);
2112 /* This is an extended version of skb_pull() that removes the data from the
2113 * start of a skb even when data is spread across the list of skb's in the
2114 * frag_list. len specifies the total amount of data that needs to be removed.
2115 * when 'len' bytes could be removed from the skb, it returns 0.
2116 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2117 * could not be removed.
2119 static int sctp_skb_pull(struct sk_buff *skb, int len)
2121 struct sk_buff *list;
2122 int skb_len = skb_headlen(skb);
2125 if (len <= skb_len) {
2126 __skb_pull(skb, len);
2130 __skb_pull(skb, skb_len);
2132 skb_walk_frags(skb, list) {
2133 rlen = sctp_skb_pull(list, len);
2134 skb->len -= (len-rlen);
2135 skb->data_len -= (len-rlen);
2146 /* API 3.1.3 recvmsg() - UDP Style Syntax
2148 * ssize_t recvmsg(int socket, struct msghdr *message,
2151 * socket - the socket descriptor of the endpoint.
2152 * message - pointer to the msghdr structure which contains a single
2153 * user message and possibly some ancillary data.
2155 * See Section 5 for complete description of the data
2158 * flags - flags sent or received with the user message, see Section
2159 * 5 for complete description of the flags.
2161 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2162 int noblock, int flags, int *addr_len)
2164 struct sctp_ulpevent *event = NULL;
2165 struct sctp_sock *sp = sctp_sk(sk);
2166 struct sk_buff *skb, *head_skb;
2171 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2172 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2177 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2178 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2183 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2187 /* Get the total length of the skb including any skb's in the
2196 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2198 event = sctp_skb2event(skb);
2203 if (event->chunk && event->chunk->head_skb)
2204 head_skb = event->chunk->head_skb;
2207 sock_recv_ts_and_drops(msg, sk, head_skb);
2208 if (sctp_ulpevent_is_notification(event)) {
2209 msg->msg_flags |= MSG_NOTIFICATION;
2210 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2212 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2215 /* Check if we allow SCTP_NXTINFO. */
2216 if (sp->recvnxtinfo)
2217 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2218 /* Check if we allow SCTP_RCVINFO. */
2219 if (sp->recvrcvinfo)
2220 sctp_ulpevent_read_rcvinfo(event, msg);
2221 /* Check if we allow SCTP_SNDRCVINFO. */
2222 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2223 sctp_ulpevent_read_sndrcvinfo(event, msg);
2227 /* If skb's length exceeds the user's buffer, update the skb and
2228 * push it back to the receive_queue so that the next call to
2229 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2231 if (skb_len > copied) {
2232 msg->msg_flags &= ~MSG_EOR;
2233 if (flags & MSG_PEEK)
2235 sctp_skb_pull(skb, copied);
2236 skb_queue_head(&sk->sk_receive_queue, skb);
2238 /* When only partial message is copied to the user, increase
2239 * rwnd by that amount. If all the data in the skb is read,
2240 * rwnd is updated when the event is freed.
2242 if (!sctp_ulpevent_is_notification(event))
2243 sctp_assoc_rwnd_increase(event->asoc, copied);
2245 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2246 (event->msg_flags & MSG_EOR))
2247 msg->msg_flags |= MSG_EOR;
2249 msg->msg_flags &= ~MSG_EOR;
2252 if (flags & MSG_PEEK) {
2253 /* Release the skb reference acquired after peeking the skb in
2254 * sctp_skb_recv_datagram().
2258 /* Free the event which includes releasing the reference to
2259 * the owner of the skb, freeing the skb and updating the
2262 sctp_ulpevent_free(event);
2269 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2271 * This option is a on/off flag. If enabled no SCTP message
2272 * fragmentation will be performed. Instead if a message being sent
2273 * exceeds the current PMTU size, the message will NOT be sent and
2274 * instead a error will be indicated to the user.
2276 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2277 char __user *optval,
2278 unsigned int optlen)
2282 if (optlen < sizeof(int))
2285 if (get_user(val, (int __user *)optval))
2288 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2293 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2294 unsigned int optlen)
2296 struct sctp_event_subscribe subscribe;
2297 __u8 *sn_type = (__u8 *)&subscribe;
2298 struct sctp_sock *sp = sctp_sk(sk);
2299 struct sctp_association *asoc;
2302 if (optlen > sizeof(struct sctp_event_subscribe))
2305 if (copy_from_user(&subscribe, optval, optlen))
2308 for (i = 0; i < optlen; i++)
2309 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2312 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2313 asoc->subscribe = sctp_sk(sk)->subscribe;
2315 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2316 * if there is no data to be sent or retransmit, the stack will
2317 * immediately send up this notification.
2319 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2320 struct sctp_ulpevent *event;
2322 asoc = sctp_id2assoc(sk, 0);
2323 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2324 event = sctp_ulpevent_make_sender_dry_event(asoc,
2325 GFP_USER | __GFP_NOWARN);
2329 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2336 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2338 * This socket option is applicable to the UDP-style socket only. When
2339 * set it will cause associations that are idle for more than the
2340 * specified number of seconds to automatically close. An association
2341 * being idle is defined an association that has NOT sent or received
2342 * user data. The special value of '0' indicates that no automatic
2343 * close of any associations should be performed. The option expects an
2344 * integer defining the number of seconds of idle time before an
2345 * association is closed.
2347 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2348 unsigned int optlen)
2350 struct sctp_sock *sp = sctp_sk(sk);
2351 struct net *net = sock_net(sk);
2353 /* Applicable to UDP-style socket only */
2354 if (sctp_style(sk, TCP))
2356 if (optlen != sizeof(int))
2358 if (copy_from_user(&sp->autoclose, optval, optlen))
2361 if (sp->autoclose > net->sctp.max_autoclose)
2362 sp->autoclose = net->sctp.max_autoclose;
2367 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2369 * Applications can enable or disable heartbeats for any peer address of
2370 * an association, modify an address's heartbeat interval, force a
2371 * heartbeat to be sent immediately, and adjust the address's maximum
2372 * number of retransmissions sent before an address is considered
2373 * unreachable. The following structure is used to access and modify an
2374 * address's parameters:
2376 * struct sctp_paddrparams {
2377 * sctp_assoc_t spp_assoc_id;
2378 * struct sockaddr_storage spp_address;
2379 * uint32_t spp_hbinterval;
2380 * uint16_t spp_pathmaxrxt;
2381 * uint32_t spp_pathmtu;
2382 * uint32_t spp_sackdelay;
2383 * uint32_t spp_flags;
2384 * uint32_t spp_ipv6_flowlabel;
2388 * spp_assoc_id - (one-to-many style socket) This is filled in the
2389 * application, and identifies the association for
2391 * spp_address - This specifies which address is of interest.
2392 * spp_hbinterval - This contains the value of the heartbeat interval,
2393 * in milliseconds. If a value of zero
2394 * is present in this field then no changes are to
2395 * be made to this parameter.
2396 * spp_pathmaxrxt - This contains the maximum number of
2397 * retransmissions before this address shall be
2398 * considered unreachable. If a value of zero
2399 * is present in this field then no changes are to
2400 * be made to this parameter.
2401 * spp_pathmtu - When Path MTU discovery is disabled the value
2402 * specified here will be the "fixed" path mtu.
2403 * Note that if the spp_address field is empty
2404 * then all associations on this address will
2405 * have this fixed path mtu set upon them.
2407 * spp_sackdelay - When delayed sack is enabled, this value specifies
2408 * the number of milliseconds that sacks will be delayed
2409 * for. This value will apply to all addresses of an
2410 * association if the spp_address field is empty. Note
2411 * also, that if delayed sack is enabled and this
2412 * value is set to 0, no change is made to the last
2413 * recorded delayed sack timer value.
2415 * spp_flags - These flags are used to control various features
2416 * on an association. The flag field may contain
2417 * zero or more of the following options.
2419 * SPP_HB_ENABLE - Enable heartbeats on the
2420 * specified address. Note that if the address
2421 * field is empty all addresses for the association
2422 * have heartbeats enabled upon them.
2424 * SPP_HB_DISABLE - Disable heartbeats on the
2425 * speicifed address. Note that if the address
2426 * field is empty all addresses for the association
2427 * will have their heartbeats disabled. Note also
2428 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2429 * mutually exclusive, only one of these two should
2430 * be specified. Enabling both fields will have
2431 * undetermined results.
2433 * SPP_HB_DEMAND - Request a user initiated heartbeat
2434 * to be made immediately.
2436 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2437 * heartbeat delayis to be set to the value of 0
2440 * SPP_PMTUD_ENABLE - This field will enable PMTU
2441 * discovery upon the specified address. Note that
2442 * if the address feild is empty then all addresses
2443 * on the association are effected.
2445 * SPP_PMTUD_DISABLE - This field will disable PMTU
2446 * discovery upon the specified address. Note that
2447 * if the address feild is empty then all addresses
2448 * on the association are effected. Not also that
2449 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2450 * exclusive. Enabling both will have undetermined
2453 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2454 * on delayed sack. The time specified in spp_sackdelay
2455 * is used to specify the sack delay for this address. Note
2456 * that if spp_address is empty then all addresses will
2457 * enable delayed sack and take on the sack delay
2458 * value specified in spp_sackdelay.
2459 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2460 * off delayed sack. If the spp_address field is blank then
2461 * delayed sack is disabled for the entire association. Note
2462 * also that this field is mutually exclusive to
2463 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2466 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2467 * setting of the IPV6 flow label value. The value is
2468 * contained in the spp_ipv6_flowlabel field.
2469 * Upon retrieval, this flag will be set to indicate that
2470 * the spp_ipv6_flowlabel field has a valid value returned.
2471 * If a specific destination address is set (in the
2472 * spp_address field), then the value returned is that of
2473 * the address. If just an association is specified (and
2474 * no address), then the association's default flow label
2475 * is returned. If neither an association nor a destination
2476 * is specified, then the socket's default flow label is
2477 * returned. For non-IPv6 sockets, this flag will be left
2480 * SPP_DSCP: Setting this flag enables the setting of the
2481 * Differentiated Services Code Point (DSCP) value
2482 * associated with either the association or a specific
2483 * address. The value is obtained in the spp_dscp field.
2484 * Upon retrieval, this flag will be set to indicate that
2485 * the spp_dscp field has a valid value returned. If a
2486 * specific destination address is set when called (in the
2487 * spp_address field), then that specific destination
2488 * address's DSCP value is returned. If just an association
2489 * is specified, then the association's default DSCP is
2490 * returned. If neither an association nor a destination is
2491 * specified, then the socket's default DSCP is returned.
2493 * spp_ipv6_flowlabel
2494 * - This field is used in conjunction with the
2495 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2496 * The 20 least significant bits are used for the flow
2497 * label. This setting has precedence over any IPv6-layer
2500 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2501 * and contains the DSCP. The 6 most significant bits are
2502 * used for the DSCP. This setting has precedence over any
2503 * IPv4- or IPv6- layer setting.
2505 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2506 struct sctp_transport *trans,
2507 struct sctp_association *asoc,
2508 struct sctp_sock *sp,
2511 int sackdelay_change)
2515 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2516 struct net *net = sock_net(trans->asoc->base.sk);
2518 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2523 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2524 * this field is ignored. Note also that a value of zero indicates
2525 * the current setting should be left unchanged.
2527 if (params->spp_flags & SPP_HB_ENABLE) {
2529 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2530 * set. This lets us use 0 value when this flag
2533 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2534 params->spp_hbinterval = 0;
2536 if (params->spp_hbinterval ||
2537 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2540 msecs_to_jiffies(params->spp_hbinterval);
2543 msecs_to_jiffies(params->spp_hbinterval);
2545 sp->hbinterval = params->spp_hbinterval;
2552 trans->param_flags =
2553 (trans->param_flags & ~SPP_HB) | hb_change;
2556 (asoc->param_flags & ~SPP_HB) | hb_change;
2559 (sp->param_flags & ~SPP_HB) | hb_change;
2563 /* When Path MTU discovery is disabled the value specified here will
2564 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2565 * include the flag SPP_PMTUD_DISABLE for this field to have any
2568 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2570 trans->pathmtu = params->spp_pathmtu;
2571 sctp_assoc_sync_pmtu(asoc);
2573 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2575 sp->pathmtu = params->spp_pathmtu;
2581 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2582 (params->spp_flags & SPP_PMTUD_ENABLE);
2583 trans->param_flags =
2584 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2586 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2587 sctp_assoc_sync_pmtu(asoc);
2591 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2594 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2598 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2599 * value of this field is ignored. Note also that a value of zero
2600 * indicates the current setting should be left unchanged.
2602 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2605 msecs_to_jiffies(params->spp_sackdelay);
2608 msecs_to_jiffies(params->spp_sackdelay);
2610 sp->sackdelay = params->spp_sackdelay;
2614 if (sackdelay_change) {
2616 trans->param_flags =
2617 (trans->param_flags & ~SPP_SACKDELAY) |
2621 (asoc->param_flags & ~SPP_SACKDELAY) |
2625 (sp->param_flags & ~SPP_SACKDELAY) |
2630 /* Note that a value of zero indicates the current setting should be
2633 if (params->spp_pathmaxrxt) {
2635 trans->pathmaxrxt = params->spp_pathmaxrxt;
2637 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2639 sp->pathmaxrxt = params->spp_pathmaxrxt;
2643 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2645 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2646 trans->flowlabel = params->spp_ipv6_flowlabel &
2647 SCTP_FLOWLABEL_VAL_MASK;
2648 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2651 struct sctp_transport *t;
2653 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2655 if (t->ipaddr.sa.sa_family != AF_INET6)
2657 t->flowlabel = params->spp_ipv6_flowlabel &
2658 SCTP_FLOWLABEL_VAL_MASK;
2659 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2661 asoc->flowlabel = params->spp_ipv6_flowlabel &
2662 SCTP_FLOWLABEL_VAL_MASK;
2663 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2664 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2665 sp->flowlabel = params->spp_ipv6_flowlabel &
2666 SCTP_FLOWLABEL_VAL_MASK;
2667 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2671 if (params->spp_flags & SPP_DSCP) {
2673 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2674 trans->dscp |= SCTP_DSCP_SET_MASK;
2676 struct sctp_transport *t;
2678 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2680 t->dscp = params->spp_dscp &
2682 t->dscp |= SCTP_DSCP_SET_MASK;
2684 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2685 asoc->dscp |= SCTP_DSCP_SET_MASK;
2687 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2688 sp->dscp |= SCTP_DSCP_SET_MASK;
2695 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2696 char __user *optval,
2697 unsigned int optlen)
2699 struct sctp_paddrparams params;
2700 struct sctp_transport *trans = NULL;
2701 struct sctp_association *asoc = NULL;
2702 struct sctp_sock *sp = sctp_sk(sk);
2704 int hb_change, pmtud_change, sackdelay_change;
2706 if (optlen == sizeof(params)) {
2707 if (copy_from_user(¶ms, optval, optlen))
2709 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2710 spp_ipv6_flowlabel), 4)) {
2711 if (copy_from_user(¶ms, optval, optlen))
2713 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2719 /* Validate flags and value parameters. */
2720 hb_change = params.spp_flags & SPP_HB;
2721 pmtud_change = params.spp_flags & SPP_PMTUD;
2722 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2724 if (hb_change == SPP_HB ||
2725 pmtud_change == SPP_PMTUD ||
2726 sackdelay_change == SPP_SACKDELAY ||
2727 params.spp_sackdelay > 500 ||
2728 (params.spp_pathmtu &&
2729 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2732 /* If an address other than INADDR_ANY is specified, and
2733 * no transport is found, then the request is invalid.
2735 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2736 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2737 params.spp_assoc_id);
2742 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2743 * socket is a one to many style socket, and an association
2744 * was not found, then the id was invalid.
2746 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2747 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2748 sctp_style(sk, UDP))
2751 /* Heartbeat demand can only be sent on a transport or
2752 * association, but not a socket.
2754 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2757 /* Process parameters. */
2758 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2759 hb_change, pmtud_change,
2765 /* If changes are for association, also apply parameters to each
2768 if (!trans && asoc) {
2769 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2771 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2772 hb_change, pmtud_change,
2780 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2782 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2785 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2787 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2790 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2791 struct sctp_association *asoc)
2793 struct sctp_transport *trans;
2795 if (params->sack_delay) {
2796 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2798 sctp_spp_sackdelay_enable(asoc->param_flags);
2800 if (params->sack_freq == 1) {
2802 sctp_spp_sackdelay_disable(asoc->param_flags);
2803 } else if (params->sack_freq > 1) {
2804 asoc->sackfreq = params->sack_freq;
2806 sctp_spp_sackdelay_enable(asoc->param_flags);
2809 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2811 if (params->sack_delay) {
2812 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2813 trans->param_flags =
2814 sctp_spp_sackdelay_enable(trans->param_flags);
2816 if (params->sack_freq == 1) {
2817 trans->param_flags =
2818 sctp_spp_sackdelay_disable(trans->param_flags);
2819 } else if (params->sack_freq > 1) {
2820 trans->sackfreq = params->sack_freq;
2821 trans->param_flags =
2822 sctp_spp_sackdelay_enable(trans->param_flags);
2828 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2830 * This option will effect the way delayed acks are performed. This
2831 * option allows you to get or set the delayed ack time, in
2832 * milliseconds. It also allows changing the delayed ack frequency.
2833 * Changing the frequency to 1 disables the delayed sack algorithm. If
2834 * the assoc_id is 0, then this sets or gets the endpoints default
2835 * values. If the assoc_id field is non-zero, then the set or get
2836 * effects the specified association for the one to many model (the
2837 * assoc_id field is ignored by the one to one model). Note that if
2838 * sack_delay or sack_freq are 0 when setting this option, then the
2839 * current values will remain unchanged.
2841 * struct sctp_sack_info {
2842 * sctp_assoc_t sack_assoc_id;
2843 * uint32_t sack_delay;
2844 * uint32_t sack_freq;
2847 * sack_assoc_id - This parameter, indicates which association the user
2848 * is performing an action upon. Note that if this field's value is
2849 * zero then the endpoints default value is changed (effecting future
2850 * associations only).
2852 * sack_delay - This parameter contains the number of milliseconds that
2853 * the user is requesting the delayed ACK timer be set to. Note that
2854 * this value is defined in the standard to be between 200 and 500
2857 * sack_freq - This parameter contains the number of packets that must
2858 * be received before a sack is sent without waiting for the delay
2859 * timer to expire. The default value for this is 2, setting this
2860 * value to 1 will disable the delayed sack algorithm.
2863 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2864 char __user *optval, unsigned int optlen)
2866 struct sctp_sock *sp = sctp_sk(sk);
2867 struct sctp_association *asoc;
2868 struct sctp_sack_info params;
2870 if (optlen == sizeof(struct sctp_sack_info)) {
2871 if (copy_from_user(¶ms, optval, optlen))
2874 if (params.sack_delay == 0 && params.sack_freq == 0)
2876 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2877 pr_warn_ratelimited(DEPRECATED
2879 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2880 "Use struct sctp_sack_info instead\n",
2881 current->comm, task_pid_nr(current));
2882 if (copy_from_user(¶ms, optval, optlen))
2885 if (params.sack_delay == 0)
2886 params.sack_freq = 1;
2888 params.sack_freq = 0;
2892 /* Validate value parameter. */
2893 if (params.sack_delay > 500)
2896 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2897 * socket is a one to many style socket, and an association
2898 * was not found, then the id was invalid.
2900 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2901 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2902 sctp_style(sk, UDP))
2906 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2911 if (sctp_style(sk, TCP))
2912 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2914 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2915 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2916 if (params.sack_delay) {
2917 sp->sackdelay = params.sack_delay;
2919 sctp_spp_sackdelay_enable(sp->param_flags);
2921 if (params.sack_freq == 1) {
2923 sctp_spp_sackdelay_disable(sp->param_flags);
2924 } else if (params.sack_freq > 1) {
2925 sp->sackfreq = params.sack_freq;
2927 sctp_spp_sackdelay_enable(sp->param_flags);
2931 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2932 params.sack_assoc_id == SCTP_ALL_ASSOC)
2933 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2934 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2939 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2941 * Applications can specify protocol parameters for the default association
2942 * initialization. The option name argument to setsockopt() and getsockopt()
2945 * Setting initialization parameters is effective only on an unconnected
2946 * socket (for UDP-style sockets only future associations are effected
2947 * by the change). With TCP-style sockets, this option is inherited by
2948 * sockets derived from a listener socket.
2950 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2952 struct sctp_initmsg sinit;
2953 struct sctp_sock *sp = sctp_sk(sk);
2955 if (optlen != sizeof(struct sctp_initmsg))
2957 if (copy_from_user(&sinit, optval, optlen))
2960 if (sinit.sinit_num_ostreams)
2961 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2962 if (sinit.sinit_max_instreams)
2963 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2964 if (sinit.sinit_max_attempts)
2965 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2966 if (sinit.sinit_max_init_timeo)
2967 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2973 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2975 * Applications that wish to use the sendto() system call may wish to
2976 * specify a default set of parameters that would normally be supplied
2977 * through the inclusion of ancillary data. This socket option allows
2978 * such an application to set the default sctp_sndrcvinfo structure.
2979 * The application that wishes to use this socket option simply passes
2980 * in to this call the sctp_sndrcvinfo structure defined in Section
2981 * 5.2.2) The input parameters accepted by this call include
2982 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2983 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2984 * to this call if the caller is using the UDP model.
2986 static int sctp_setsockopt_default_send_param(struct sock *sk,
2987 char __user *optval,
2988 unsigned int optlen)
2990 struct sctp_sock *sp = sctp_sk(sk);
2991 struct sctp_association *asoc;
2992 struct sctp_sndrcvinfo info;
2994 if (optlen != sizeof(info))
2996 if (copy_from_user(&info, optval, optlen))
2998 if (info.sinfo_flags &
2999 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3000 SCTP_ABORT | SCTP_EOF))
3003 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3004 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
3005 sctp_style(sk, UDP))
3009 asoc->default_stream = info.sinfo_stream;
3010 asoc->default_flags = info.sinfo_flags;
3011 asoc->default_ppid = info.sinfo_ppid;
3012 asoc->default_context = info.sinfo_context;
3013 asoc->default_timetolive = info.sinfo_timetolive;
3018 if (sctp_style(sk, TCP))
3019 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
3021 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
3022 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3023 sp->default_stream = info.sinfo_stream;
3024 sp->default_flags = info.sinfo_flags;
3025 sp->default_ppid = info.sinfo_ppid;
3026 sp->default_context = info.sinfo_context;
3027 sp->default_timetolive = info.sinfo_timetolive;
3030 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
3031 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3032 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3033 asoc->default_stream = info.sinfo_stream;
3034 asoc->default_flags = info.sinfo_flags;
3035 asoc->default_ppid = info.sinfo_ppid;
3036 asoc->default_context = info.sinfo_context;
3037 asoc->default_timetolive = info.sinfo_timetolive;
3044 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3045 * (SCTP_DEFAULT_SNDINFO)
3047 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3048 char __user *optval,
3049 unsigned int optlen)
3051 struct sctp_sock *sp = sctp_sk(sk);
3052 struct sctp_association *asoc;
3053 struct sctp_sndinfo info;
3055 if (optlen != sizeof(info))
3057 if (copy_from_user(&info, optval, optlen))
3059 if (info.snd_flags &
3060 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3061 SCTP_ABORT | SCTP_EOF))
3064 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3065 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3066 sctp_style(sk, UDP))
3070 asoc->default_stream = info.snd_sid;
3071 asoc->default_flags = info.snd_flags;
3072 asoc->default_ppid = info.snd_ppid;
3073 asoc->default_context = info.snd_context;
3078 if (sctp_style(sk, TCP))
3079 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3081 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3082 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3083 sp->default_stream = info.snd_sid;
3084 sp->default_flags = info.snd_flags;
3085 sp->default_ppid = info.snd_ppid;
3086 sp->default_context = info.snd_context;
3089 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3090 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3091 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3092 asoc->default_stream = info.snd_sid;
3093 asoc->default_flags = info.snd_flags;
3094 asoc->default_ppid = info.snd_ppid;
3095 asoc->default_context = info.snd_context;
3102 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3104 * Requests that the local SCTP stack use the enclosed peer address as
3105 * the association primary. The enclosed address must be one of the
3106 * association peer's addresses.
3108 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3109 unsigned int optlen)
3111 struct sctp_prim prim;
3112 struct sctp_transport *trans;
3116 if (optlen != sizeof(struct sctp_prim))
3119 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3122 /* Allow security module to validate address but need address len. */
3123 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3127 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3128 (struct sockaddr *)&prim.ssp_addr,
3133 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3137 sctp_assoc_set_primary(trans->asoc, trans);
3143 * 7.1.5 SCTP_NODELAY
3145 * Turn on/off any Nagle-like algorithm. This means that packets are
3146 * generally sent as soon as possible and no unnecessary delays are
3147 * introduced, at the cost of more packets in the network. Expects an
3148 * integer boolean flag.
3150 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3151 unsigned int optlen)
3155 if (optlen < sizeof(int))
3157 if (get_user(val, (int __user *)optval))
3160 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3166 * 7.1.1 SCTP_RTOINFO
3168 * The protocol parameters used to initialize and bound retransmission
3169 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3170 * and modify these parameters.
3171 * All parameters are time values, in milliseconds. A value of 0, when
3172 * modifying the parameters, indicates that the current value should not
3176 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3178 struct sctp_rtoinfo rtoinfo;
3179 struct sctp_association *asoc;
3180 unsigned long rto_min, rto_max;
3181 struct sctp_sock *sp = sctp_sk(sk);
3183 if (optlen != sizeof (struct sctp_rtoinfo))
3186 if (copy_from_user(&rtoinfo, optval, optlen))
3189 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3191 /* Set the values to the specific association */
3192 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3193 sctp_style(sk, UDP))
3196 rto_max = rtoinfo.srto_max;
3197 rto_min = rtoinfo.srto_min;
3200 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3202 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3205 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3207 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3209 if (rto_min > rto_max)
3213 if (rtoinfo.srto_initial != 0)
3215 msecs_to_jiffies(rtoinfo.srto_initial);
3216 asoc->rto_max = rto_max;
3217 asoc->rto_min = rto_min;
3219 /* If there is no association or the association-id = 0
3220 * set the values to the endpoint.
3222 if (rtoinfo.srto_initial != 0)
3223 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3224 sp->rtoinfo.srto_max = rto_max;
3225 sp->rtoinfo.srto_min = rto_min;
3233 * 7.1.2 SCTP_ASSOCINFO
3235 * This option is used to tune the maximum retransmission attempts
3236 * of the association.
3237 * Returns an error if the new association retransmission value is
3238 * greater than the sum of the retransmission value of the peer.
3239 * See [SCTP] for more information.
3242 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3245 struct sctp_assocparams assocparams;
3246 struct sctp_association *asoc;
3248 if (optlen != sizeof(struct sctp_assocparams))
3250 if (copy_from_user(&assocparams, optval, optlen))
3253 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3255 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3256 sctp_style(sk, UDP))
3259 /* Set the values to the specific association */
3261 if (assocparams.sasoc_asocmaxrxt != 0) {
3264 struct sctp_transport *peer_addr;
3266 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3268 path_sum += peer_addr->pathmaxrxt;
3272 /* Only validate asocmaxrxt if we have more than
3273 * one path/transport. We do this because path
3274 * retransmissions are only counted when we have more
3278 assocparams.sasoc_asocmaxrxt > path_sum)
3281 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3284 if (assocparams.sasoc_cookie_life != 0)
3285 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3287 /* Set the values to the endpoint */
3288 struct sctp_sock *sp = sctp_sk(sk);
3290 if (assocparams.sasoc_asocmaxrxt != 0)
3291 sp->assocparams.sasoc_asocmaxrxt =
3292 assocparams.sasoc_asocmaxrxt;
3293 if (assocparams.sasoc_cookie_life != 0)
3294 sp->assocparams.sasoc_cookie_life =
3295 assocparams.sasoc_cookie_life;
3301 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3303 * This socket option is a boolean flag which turns on or off mapped V4
3304 * addresses. If this option is turned on and the socket is type
3305 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3306 * If this option is turned off, then no mapping will be done of V4
3307 * addresses and a user will receive both PF_INET6 and PF_INET type
3308 * addresses on the socket.
3310 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3313 struct sctp_sock *sp = sctp_sk(sk);
3315 if (optlen < sizeof(int))
3317 if (get_user(val, (int __user *)optval))
3328 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3329 * This option will get or set the maximum size to put in any outgoing
3330 * SCTP DATA chunk. If a message is larger than this size it will be
3331 * fragmented by SCTP into the specified size. Note that the underlying
3332 * SCTP implementation may fragment into smaller sized chunks when the
3333 * PMTU of the underlying association is smaller than the value set by
3334 * the user. The default value for this option is '0' which indicates
3335 * the user is NOT limiting fragmentation and only the PMTU will effect
3336 * SCTP's choice of DATA chunk size. Note also that values set larger
3337 * than the maximum size of an IP datagram will effectively let SCTP
3338 * control fragmentation (i.e. the same as setting this option to 0).
3340 * The following structure is used to access and modify this parameter:
3342 * struct sctp_assoc_value {
3343 * sctp_assoc_t assoc_id;
3344 * uint32_t assoc_value;
3347 * assoc_id: This parameter is ignored for one-to-one style sockets.
3348 * For one-to-many style sockets this parameter indicates which
3349 * association the user is performing an action upon. Note that if
3350 * this field's value is zero then the endpoints default value is
3351 * changed (effecting future associations only).
3352 * assoc_value: This parameter specifies the maximum size in bytes.
3354 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3356 struct sctp_sock *sp = sctp_sk(sk);
3357 struct sctp_assoc_value params;
3358 struct sctp_association *asoc;
3361 if (optlen == sizeof(int)) {
3362 pr_warn_ratelimited(DEPRECATED
3364 "Use of int in maxseg socket option.\n"
3365 "Use struct sctp_assoc_value instead\n",
3366 current->comm, task_pid_nr(current));
3367 if (copy_from_user(&val, optval, optlen))
3369 params.assoc_id = SCTP_FUTURE_ASSOC;
3370 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3371 if (copy_from_user(¶ms, optval, optlen))
3373 val = params.assoc_value;
3378 asoc = sctp_id2assoc(sk, params.assoc_id);
3379 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3380 sctp_style(sk, UDP))
3384 int min_len, max_len;
3385 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3386 sizeof(struct sctp_data_chunk);
3388 min_len = sctp_min_frag_point(sp, datasize);
3389 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3391 if (val < min_len || val > max_len)
3396 asoc->user_frag = val;
3397 sctp_assoc_update_frag_point(asoc);
3399 sp->user_frag = val;
3407 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3409 * Requests that the peer mark the enclosed address as the association
3410 * primary. The enclosed address must be one of the association's
3411 * locally bound addresses. The following structure is used to make a
3412 * set primary request:
3414 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3415 unsigned int optlen)
3417 struct net *net = sock_net(sk);
3418 struct sctp_sock *sp;
3419 struct sctp_association *asoc = NULL;
3420 struct sctp_setpeerprim prim;
3421 struct sctp_chunk *chunk;
3427 if (!net->sctp.addip_enable)
3430 if (optlen != sizeof(struct sctp_setpeerprim))
3433 if (copy_from_user(&prim, optval, optlen))
3436 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3440 if (!asoc->peer.asconf_capable)
3443 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3446 if (!sctp_state(asoc, ESTABLISHED))
3449 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3453 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3454 return -EADDRNOTAVAIL;
3456 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3457 return -EADDRNOTAVAIL;
3459 /* Allow security module to validate address. */
3460 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3461 (struct sockaddr *)&prim.sspp_addr,
3466 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3467 chunk = sctp_make_asconf_set_prim(asoc,
3468 (union sctp_addr *)&prim.sspp_addr);
3472 err = sctp_send_asconf(asoc, chunk);
3474 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3479 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3480 unsigned int optlen)
3482 struct sctp_setadaptation adaptation;
3484 if (optlen != sizeof(struct sctp_setadaptation))
3486 if (copy_from_user(&adaptation, optval, optlen))
3489 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3495 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3497 * The context field in the sctp_sndrcvinfo structure is normally only
3498 * used when a failed message is retrieved holding the value that was
3499 * sent down on the actual send call. This option allows the setting of
3500 * a default context on an association basis that will be received on
3501 * reading messages from the peer. This is especially helpful in the
3502 * one-2-many model for an application to keep some reference to an
3503 * internal state machine that is processing messages on the
3504 * association. Note that the setting of this value only effects
3505 * received messages from the peer and does not effect the value that is
3506 * saved with outbound messages.
3508 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3509 unsigned int optlen)
3511 struct sctp_sock *sp = sctp_sk(sk);
3512 struct sctp_assoc_value params;
3513 struct sctp_association *asoc;
3515 if (optlen != sizeof(struct sctp_assoc_value))
3517 if (copy_from_user(¶ms, optval, optlen))
3520 asoc = sctp_id2assoc(sk, params.assoc_id);
3521 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3522 sctp_style(sk, UDP))
3526 asoc->default_rcv_context = params.assoc_value;
3531 if (sctp_style(sk, TCP))
3532 params.assoc_id = SCTP_FUTURE_ASSOC;
3534 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3535 params.assoc_id == SCTP_ALL_ASSOC)
3536 sp->default_rcv_context = params.assoc_value;
3538 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3539 params.assoc_id == SCTP_ALL_ASSOC)
3540 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3541 asoc->default_rcv_context = params.assoc_value;
3547 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3549 * This options will at a minimum specify if the implementation is doing
3550 * fragmented interleave. Fragmented interleave, for a one to many
3551 * socket, is when subsequent calls to receive a message may return
3552 * parts of messages from different associations. Some implementations
3553 * may allow you to turn this value on or off. If so, when turned off,
3554 * no fragment interleave will occur (which will cause a head of line
3555 * blocking amongst multiple associations sharing the same one to many
3556 * socket). When this option is turned on, then each receive call may
3557 * come from a different association (thus the user must receive data
3558 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3559 * association each receive belongs to.
3561 * This option takes a boolean value. A non-zero value indicates that
3562 * fragmented interleave is on. A value of zero indicates that
3563 * fragmented interleave is off.
3565 * Note that it is important that an implementation that allows this
3566 * option to be turned on, have it off by default. Otherwise an unaware
3567 * application using the one to many model may become confused and act
3570 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3571 char __user *optval,
3572 unsigned int optlen)
3576 if (optlen != sizeof(int))
3578 if (get_user(val, (int __user *)optval))
3581 sctp_sk(sk)->frag_interleave = !!val;
3583 if (!sctp_sk(sk)->frag_interleave)
3584 sctp_sk(sk)->strm_interleave = 0;
3590 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3591 * (SCTP_PARTIAL_DELIVERY_POINT)
3593 * This option will set or get the SCTP partial delivery point. This
3594 * point is the size of a message where the partial delivery API will be
3595 * invoked to help free up rwnd space for the peer. Setting this to a
3596 * lower value will cause partial deliveries to happen more often. The
3597 * calls argument is an integer that sets or gets the partial delivery
3598 * point. Note also that the call will fail if the user attempts to set
3599 * this value larger than the socket receive buffer size.
3601 * Note that any single message having a length smaller than or equal to
3602 * the SCTP partial delivery point will be delivered in one single read
3603 * call as long as the user provided buffer is large enough to hold the
3606 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3607 char __user *optval,
3608 unsigned int optlen)
3612 if (optlen != sizeof(u32))
3614 if (get_user(val, (int __user *)optval))
3617 /* Note: We double the receive buffer from what the user sets
3618 * it to be, also initial rwnd is based on rcvbuf/2.
3620 if (val > (sk->sk_rcvbuf >> 1))
3623 sctp_sk(sk)->pd_point = val;
3625 return 0; /* is this the right error code? */
3629 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3631 * This option will allow a user to change the maximum burst of packets
3632 * that can be emitted by this association. Note that the default value
3633 * is 4, and some implementations may restrict this setting so that it
3634 * can only be lowered.
3636 * NOTE: This text doesn't seem right. Do this on a socket basis with
3637 * future associations inheriting the socket value.
3639 static int sctp_setsockopt_maxburst(struct sock *sk,
3640 char __user *optval,
3641 unsigned int optlen)
3643 struct sctp_sock *sp = sctp_sk(sk);
3644 struct sctp_assoc_value params;
3645 struct sctp_association *asoc;
3647 if (optlen == sizeof(int)) {
3648 pr_warn_ratelimited(DEPRECATED
3650 "Use of int in max_burst socket option deprecated.\n"
3651 "Use struct sctp_assoc_value instead\n",
3652 current->comm, task_pid_nr(current));
3653 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3655 params.assoc_id = SCTP_FUTURE_ASSOC;
3656 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3657 if (copy_from_user(¶ms, optval, optlen))
3662 asoc = sctp_id2assoc(sk, params.assoc_id);
3663 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3664 sctp_style(sk, UDP))
3668 asoc->max_burst = params.assoc_value;
3673 if (sctp_style(sk, TCP))
3674 params.assoc_id = SCTP_FUTURE_ASSOC;
3676 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3677 params.assoc_id == SCTP_ALL_ASSOC)
3678 sp->max_burst = params.assoc_value;
3680 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3681 params.assoc_id == SCTP_ALL_ASSOC)
3682 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3683 asoc->max_burst = params.assoc_value;
3689 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3691 * This set option adds a chunk type that the user is requesting to be
3692 * received only in an authenticated way. Changes to the list of chunks
3693 * will only effect future associations on the socket.
3695 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3696 char __user *optval,
3697 unsigned int optlen)
3699 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3700 struct sctp_authchunk val;
3702 if (!ep->auth_enable)
3705 if (optlen != sizeof(struct sctp_authchunk))
3707 if (copy_from_user(&val, optval, optlen))
3710 switch (val.sauth_chunk) {
3712 case SCTP_CID_INIT_ACK:
3713 case SCTP_CID_SHUTDOWN_COMPLETE:
3718 /* add this chunk id to the endpoint */
3719 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3723 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3725 * This option gets or sets the list of HMAC algorithms that the local
3726 * endpoint requires the peer to use.
3728 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3729 char __user *optval,
3730 unsigned int optlen)
3732 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3733 struct sctp_hmacalgo *hmacs;
3737 if (!ep->auth_enable)
3740 if (optlen < sizeof(struct sctp_hmacalgo))
3742 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3743 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3745 hmacs = memdup_user(optval, optlen);
3747 return PTR_ERR(hmacs);
3749 idents = hmacs->shmac_num_idents;
3750 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3751 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3756 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3763 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3765 * This option will set a shared secret key which is used to build an
3766 * association shared key.
3768 static int sctp_setsockopt_auth_key(struct sock *sk,
3769 char __user *optval,
3770 unsigned int optlen)
3772 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3773 struct sctp_authkey *authkey;
3774 struct sctp_association *asoc;
3777 if (!ep->auth_enable)
3780 if (optlen <= sizeof(struct sctp_authkey))
3782 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3785 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3787 authkey = memdup_user(optval, optlen);
3788 if (IS_ERR(authkey))
3789 return PTR_ERR(authkey);
3791 if (authkey->sca_keylength > optlen - sizeof(*authkey))
3794 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3795 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3796 sctp_style(sk, UDP))
3800 ret = sctp_auth_set_key(ep, asoc, authkey);
3804 if (sctp_style(sk, TCP))
3805 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3807 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3808 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3809 ret = sctp_auth_set_key(ep, asoc, authkey);
3816 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3817 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3818 list_for_each_entry(asoc, &ep->asocs, asocs) {
3819 int res = sctp_auth_set_key(ep, asoc, authkey);
3832 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3834 * This option will get or set the active shared key to be used to build
3835 * the association shared key.
3837 static int sctp_setsockopt_active_key(struct sock *sk,
3838 char __user *optval,
3839 unsigned int optlen)
3841 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3842 struct sctp_association *asoc;
3843 struct sctp_authkeyid val;
3846 if (!ep->auth_enable)
3849 if (optlen != sizeof(struct sctp_authkeyid))
3851 if (copy_from_user(&val, optval, optlen))
3854 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3855 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3856 sctp_style(sk, UDP))
3860 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3862 if (sctp_style(sk, TCP))
3863 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3865 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3866 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3867 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3872 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3873 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3874 list_for_each_entry(asoc, &ep->asocs, asocs) {
3875 int res = sctp_auth_set_active_key(ep, asoc,
3876 val.scact_keynumber);
3887 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3889 * This set option will delete a shared secret key from use.
3891 static int sctp_setsockopt_del_key(struct sock *sk,
3892 char __user *optval,
3893 unsigned int optlen)
3895 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3896 struct sctp_association *asoc;
3897 struct sctp_authkeyid val;
3900 if (!ep->auth_enable)
3903 if (optlen != sizeof(struct sctp_authkeyid))
3905 if (copy_from_user(&val, optval, optlen))
3908 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3909 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3910 sctp_style(sk, UDP))
3914 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3916 if (sctp_style(sk, TCP))
3917 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3919 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3920 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3921 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3926 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3927 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3928 list_for_each_entry(asoc, &ep->asocs, asocs) {
3929 int res = sctp_auth_del_key_id(ep, asoc,
3930 val.scact_keynumber);
3941 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3943 * This set option will deactivate a shared secret key.
3945 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3946 unsigned int optlen)
3948 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3949 struct sctp_association *asoc;
3950 struct sctp_authkeyid val;
3953 if (!ep->auth_enable)
3956 if (optlen != sizeof(struct sctp_authkeyid))
3958 if (copy_from_user(&val, optval, optlen))
3961 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3962 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3963 sctp_style(sk, UDP))
3967 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3969 if (sctp_style(sk, TCP))
3970 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3972 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3973 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3974 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3979 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3980 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3981 list_for_each_entry(asoc, &ep->asocs, asocs) {
3982 int res = sctp_auth_deact_key_id(ep, asoc,
3983 val.scact_keynumber);
3994 * 8.1.23 SCTP_AUTO_ASCONF
3996 * This option will enable or disable the use of the automatic generation of
3997 * ASCONF chunks to add and delete addresses to an existing association. Note
3998 * that this option has two caveats namely: a) it only affects sockets that
3999 * are bound to all addresses available to the SCTP stack, and b) the system
4000 * administrator may have an overriding control that turns the ASCONF feature
4001 * off no matter what setting the socket option may have.
4002 * This option expects an integer boolean flag, where a non-zero value turns on
4003 * the option, and a zero value turns off the option.
4004 * Note. In this implementation, socket operation overrides default parameter
4005 * being set by sysctl as well as FreeBSD implementation
4007 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
4008 unsigned int optlen)
4011 struct sctp_sock *sp = sctp_sk(sk);
4013 if (optlen < sizeof(int))
4015 if (get_user(val, (int __user *)optval))
4017 if (!sctp_is_ep_boundall(sk) && val)
4019 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
4022 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
4023 if (val == 0 && sp->do_auto_asconf) {
4024 list_del(&sp->auto_asconf_list);
4025 sp->do_auto_asconf = 0;
4026 } else if (val && !sp->do_auto_asconf) {
4027 list_add_tail(&sp->auto_asconf_list,
4028 &sock_net(sk)->sctp.auto_asconf_splist);
4029 sp->do_auto_asconf = 1;
4031 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
4036 * SCTP_PEER_ADDR_THLDS
4038 * This option allows us to alter the partially failed threshold for one or all
4039 * transports in an association. See Section 6.1 of:
4040 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
4042 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
4043 char __user *optval,
4044 unsigned int optlen)
4046 struct sctp_paddrthlds val;
4047 struct sctp_transport *trans;
4048 struct sctp_association *asoc;
4050 if (optlen < sizeof(struct sctp_paddrthlds))
4052 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
4053 sizeof(struct sctp_paddrthlds)))
4056 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
4057 trans = sctp_addr_id2transport(sk, &val.spt_address,
4062 if (val.spt_pathmaxrxt)
4063 trans->pathmaxrxt = val.spt_pathmaxrxt;
4064 trans->pf_retrans = val.spt_pathpfthld;
4069 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4070 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4071 sctp_style(sk, UDP))
4075 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4077 if (val.spt_pathmaxrxt)
4078 trans->pathmaxrxt = val.spt_pathmaxrxt;
4079 trans->pf_retrans = val.spt_pathpfthld;
4082 if (val.spt_pathmaxrxt)
4083 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4084 asoc->pf_retrans = val.spt_pathpfthld;
4086 struct sctp_sock *sp = sctp_sk(sk);
4088 if (val.spt_pathmaxrxt)
4089 sp->pathmaxrxt = val.spt_pathmaxrxt;
4090 sp->pf_retrans = val.spt_pathpfthld;
4096 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4097 char __user *optval,
4098 unsigned int optlen)
4102 if (optlen < sizeof(int))
4104 if (get_user(val, (int __user *) optval))
4107 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4112 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4113 char __user *optval,
4114 unsigned int optlen)
4118 if (optlen < sizeof(int))
4120 if (get_user(val, (int __user *) optval))
4123 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4128 static int sctp_setsockopt_pr_supported(struct sock *sk,
4129 char __user *optval,
4130 unsigned int optlen)
4132 struct sctp_assoc_value params;
4133 struct sctp_association *asoc;
4135 if (optlen != sizeof(params))
4138 if (copy_from_user(¶ms, optval, optlen))
4141 asoc = sctp_id2assoc(sk, params.assoc_id);
4142 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4143 sctp_style(sk, UDP))
4146 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4151 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4152 char __user *optval,
4153 unsigned int optlen)
4155 struct sctp_sock *sp = sctp_sk(sk);
4156 struct sctp_default_prinfo info;
4157 struct sctp_association *asoc;
4158 int retval = -EINVAL;
4160 if (optlen != sizeof(info))
4163 if (copy_from_user(&info, optval, sizeof(info))) {
4168 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4171 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4174 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4175 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4176 sctp_style(sk, UDP))
4182 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4183 asoc->default_timetolive = info.pr_value;
4187 if (sctp_style(sk, TCP))
4188 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4190 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4191 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4192 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4193 sp->default_timetolive = info.pr_value;
4196 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4197 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4198 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4199 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4200 asoc->default_timetolive = info.pr_value;
4208 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4209 char __user *optval,
4210 unsigned int optlen)
4212 struct sctp_assoc_value params;
4213 struct sctp_association *asoc;
4214 int retval = -EINVAL;
4216 if (optlen != sizeof(params))
4219 if (copy_from_user(¶ms, optval, optlen)) {
4224 asoc = sctp_id2assoc(sk, params.assoc_id);
4225 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4226 sctp_style(sk, UDP))
4230 asoc->reconf_enable = !!params.assoc_value;
4232 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4240 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4241 char __user *optval,
4242 unsigned int optlen)
4244 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4245 struct sctp_assoc_value params;
4246 struct sctp_association *asoc;
4247 int retval = -EINVAL;
4249 if (optlen != sizeof(params))
4252 if (copy_from_user(¶ms, optval, optlen)) {
4257 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4260 asoc = sctp_id2assoc(sk, params.assoc_id);
4261 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4262 sctp_style(sk, UDP))
4268 asoc->strreset_enable = params.assoc_value;
4272 if (sctp_style(sk, TCP))
4273 params.assoc_id = SCTP_FUTURE_ASSOC;
4275 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4276 params.assoc_id == SCTP_ALL_ASSOC)
4277 ep->strreset_enable = params.assoc_value;
4279 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4280 params.assoc_id == SCTP_ALL_ASSOC)
4281 list_for_each_entry(asoc, &ep->asocs, asocs)
4282 asoc->strreset_enable = params.assoc_value;
4288 static int sctp_setsockopt_reset_streams(struct sock *sk,
4289 char __user *optval,
4290 unsigned int optlen)
4292 struct sctp_reset_streams *params;
4293 struct sctp_association *asoc;
4294 int retval = -EINVAL;
4296 if (optlen < sizeof(*params))
4298 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4299 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4300 sizeof(__u16) * sizeof(*params));
4302 params = memdup_user(optval, optlen);
4304 return PTR_ERR(params);
4306 if (params->srs_number_streams * sizeof(__u16) >
4307 optlen - sizeof(*params))
4310 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4314 retval = sctp_send_reset_streams(asoc, params);
4321 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4322 char __user *optval,
4323 unsigned int optlen)
4325 struct sctp_association *asoc;
4326 sctp_assoc_t associd;
4327 int retval = -EINVAL;
4329 if (optlen != sizeof(associd))
4332 if (copy_from_user(&associd, optval, optlen)) {
4337 asoc = sctp_id2assoc(sk, associd);
4341 retval = sctp_send_reset_assoc(asoc);
4347 static int sctp_setsockopt_add_streams(struct sock *sk,
4348 char __user *optval,
4349 unsigned int optlen)
4351 struct sctp_association *asoc;
4352 struct sctp_add_streams params;
4353 int retval = -EINVAL;
4355 if (optlen != sizeof(params))
4358 if (copy_from_user(¶ms, optval, optlen)) {
4363 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4367 retval = sctp_send_add_streams(asoc, ¶ms);
4373 static int sctp_setsockopt_scheduler(struct sock *sk,
4374 char __user *optval,
4375 unsigned int optlen)
4377 struct sctp_sock *sp = sctp_sk(sk);
4378 struct sctp_association *asoc;
4379 struct sctp_assoc_value params;
4382 if (optlen < sizeof(params))
4385 optlen = sizeof(params);
4386 if (copy_from_user(¶ms, optval, optlen))
4389 if (params.assoc_value > SCTP_SS_MAX)
4392 asoc = sctp_id2assoc(sk, params.assoc_id);
4393 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4394 sctp_style(sk, UDP))
4398 return sctp_sched_set_sched(asoc, params.assoc_value);
4400 if (sctp_style(sk, TCP))
4401 params.assoc_id = SCTP_FUTURE_ASSOC;
4403 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4404 params.assoc_id == SCTP_ALL_ASSOC)
4405 sp->default_ss = params.assoc_value;
4407 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4408 params.assoc_id == SCTP_ALL_ASSOC) {
4409 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4410 int ret = sctp_sched_set_sched(asoc,
4411 params.assoc_value);
4421 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4422 char __user *optval,
4423 unsigned int optlen)
4425 struct sctp_stream_value params;
4426 struct sctp_association *asoc;
4427 int retval = -EINVAL;
4429 if (optlen < sizeof(params))
4432 optlen = sizeof(params);
4433 if (copy_from_user(¶ms, optval, optlen)) {
4438 asoc = sctp_id2assoc(sk, params.assoc_id);
4439 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4440 sctp_style(sk, UDP))
4444 retval = sctp_sched_set_value(asoc, params.stream_id,
4445 params.stream_value, GFP_KERNEL);
4451 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4452 int ret = sctp_sched_set_value(asoc, params.stream_id,
4453 params.stream_value, GFP_KERNEL);
4454 if (ret && !retval) /* try to return the 1st error. */
4462 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4463 char __user *optval,
4464 unsigned int optlen)
4466 struct sctp_sock *sp = sctp_sk(sk);
4467 struct sctp_assoc_value params;
4468 struct sctp_association *asoc;
4469 int retval = -EINVAL;
4471 if (optlen < sizeof(params))
4474 optlen = sizeof(params);
4475 if (copy_from_user(¶ms, optval, optlen)) {
4480 asoc = sctp_id2assoc(sk, params.assoc_id);
4481 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4482 sctp_style(sk, UDP))
4485 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4490 sp->strm_interleave = !!params.assoc_value;
4498 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4499 unsigned int optlen)
4503 if (!sctp_style(sk, TCP))
4506 if (sctp_sk(sk)->ep->base.bind_addr.port)
4509 if (optlen < sizeof(int))
4512 if (get_user(val, (int __user *)optval))
4515 sctp_sk(sk)->reuse = !!val;
4520 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4521 struct sctp_association *asoc)
4523 struct sctp_ulpevent *event;
4525 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4527 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4528 if (sctp_outq_is_empty(&asoc->outqueue)) {
4529 event = sctp_ulpevent_make_sender_dry_event(asoc,
4530 GFP_USER | __GFP_NOWARN);
4534 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4541 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4542 unsigned int optlen)
4544 struct sctp_sock *sp = sctp_sk(sk);
4545 struct sctp_association *asoc;
4546 struct sctp_event param;
4549 if (optlen < sizeof(param))
4552 optlen = sizeof(param);
4553 if (copy_from_user(¶m, optval, optlen))
4556 if (param.se_type < SCTP_SN_TYPE_BASE ||
4557 param.se_type > SCTP_SN_TYPE_MAX)
4560 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4561 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4562 sctp_style(sk, UDP))
4566 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4568 if (sctp_style(sk, TCP))
4569 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4571 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4572 param.se_assoc_id == SCTP_ALL_ASSOC)
4573 sctp_ulpevent_type_set(&sp->subscribe,
4574 param.se_type, param.se_on);
4576 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4577 param.se_assoc_id == SCTP_ALL_ASSOC) {
4578 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4579 int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4589 /* API 6.2 setsockopt(), getsockopt()
4591 * Applications use setsockopt() and getsockopt() to set or retrieve
4592 * socket options. Socket options are used to change the default
4593 * behavior of sockets calls. They are described in Section 7.
4597 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4598 * int __user *optlen);
4599 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4602 * sd - the socket descript.
4603 * level - set to IPPROTO_SCTP for all SCTP options.
4604 * optname - the option name.
4605 * optval - the buffer to store the value of the option.
4606 * optlen - the size of the buffer.
4608 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4609 char __user *optval, unsigned int optlen)
4613 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4615 /* I can hardly begin to describe how wrong this is. This is
4616 * so broken as to be worse than useless. The API draft
4617 * REALLY is NOT helpful here... I am not convinced that the
4618 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4619 * are at all well-founded.
4621 if (level != SOL_SCTP) {
4622 struct sctp_af *af = sctp_sk(sk)->pf->af;
4623 retval = af->setsockopt(sk, level, optname, optval, optlen);
4630 case SCTP_SOCKOPT_BINDX_ADD:
4631 /* 'optlen' is the size of the addresses buffer. */
4632 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4633 optlen, SCTP_BINDX_ADD_ADDR);
4636 case SCTP_SOCKOPT_BINDX_REM:
4637 /* 'optlen' is the size of the addresses buffer. */
4638 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4639 optlen, SCTP_BINDX_REM_ADDR);
4642 case SCTP_SOCKOPT_CONNECTX_OLD:
4643 /* 'optlen' is the size of the addresses buffer. */
4644 retval = sctp_setsockopt_connectx_old(sk,
4645 (struct sockaddr __user *)optval,
4649 case SCTP_SOCKOPT_CONNECTX:
4650 /* 'optlen' is the size of the addresses buffer. */
4651 retval = sctp_setsockopt_connectx(sk,
4652 (struct sockaddr __user *)optval,
4656 case SCTP_DISABLE_FRAGMENTS:
4657 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4661 retval = sctp_setsockopt_events(sk, optval, optlen);
4664 case SCTP_AUTOCLOSE:
4665 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4668 case SCTP_PEER_ADDR_PARAMS:
4669 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4672 case SCTP_DELAYED_SACK:
4673 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4675 case SCTP_PARTIAL_DELIVERY_POINT:
4676 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4680 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4682 case SCTP_DEFAULT_SEND_PARAM:
4683 retval = sctp_setsockopt_default_send_param(sk, optval,
4686 case SCTP_DEFAULT_SNDINFO:
4687 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4689 case SCTP_PRIMARY_ADDR:
4690 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4692 case SCTP_SET_PEER_PRIMARY_ADDR:
4693 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4696 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4699 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4701 case SCTP_ASSOCINFO:
4702 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4704 case SCTP_I_WANT_MAPPED_V4_ADDR:
4705 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4708 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4710 case SCTP_ADAPTATION_LAYER:
4711 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4714 retval = sctp_setsockopt_context(sk, optval, optlen);
4716 case SCTP_FRAGMENT_INTERLEAVE:
4717 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4719 case SCTP_MAX_BURST:
4720 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4722 case SCTP_AUTH_CHUNK:
4723 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4725 case SCTP_HMAC_IDENT:
4726 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4729 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4731 case SCTP_AUTH_ACTIVE_KEY:
4732 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4734 case SCTP_AUTH_DELETE_KEY:
4735 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4737 case SCTP_AUTH_DEACTIVATE_KEY:
4738 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4740 case SCTP_AUTO_ASCONF:
4741 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4743 case SCTP_PEER_ADDR_THLDS:
4744 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4746 case SCTP_RECVRCVINFO:
4747 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4749 case SCTP_RECVNXTINFO:
4750 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4752 case SCTP_PR_SUPPORTED:
4753 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4755 case SCTP_DEFAULT_PRINFO:
4756 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4758 case SCTP_RECONFIG_SUPPORTED:
4759 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4761 case SCTP_ENABLE_STREAM_RESET:
4762 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4764 case SCTP_RESET_STREAMS:
4765 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4767 case SCTP_RESET_ASSOC:
4768 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4770 case SCTP_ADD_STREAMS:
4771 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4773 case SCTP_STREAM_SCHEDULER:
4774 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4776 case SCTP_STREAM_SCHEDULER_VALUE:
4777 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4779 case SCTP_INTERLEAVING_SUPPORTED:
4780 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4783 case SCTP_REUSE_PORT:
4784 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4787 retval = sctp_setsockopt_event(sk, optval, optlen);
4790 retval = -ENOPROTOOPT;
4800 /* API 3.1.6 connect() - UDP Style Syntax
4802 * An application may use the connect() call in the UDP model to initiate an
4803 * association without sending data.
4807 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4809 * sd: the socket descriptor to have a new association added to.
4811 * nam: the address structure (either struct sockaddr_in or struct
4812 * sockaddr_in6 defined in RFC2553 [7]).
4814 * len: the size of the address.
4816 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4817 int addr_len, int flags)
4819 struct inet_sock *inet = inet_sk(sk);
4825 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4828 /* We may need to bind the socket. */
4829 if (!inet->inet_num) {
4830 if (sk->sk_prot->get_port(sk, 0)) {
4834 inet->inet_sport = htons(inet->inet_num);
4837 /* Validate addr_len before calling common connect/connectx routine. */
4838 af = addr_len < offsetofend(struct sockaddr, sa_family) ? NULL :
4839 sctp_get_af_specific(addr->sa_family);
4840 if (!af || addr_len < af->sockaddr_len) {
4843 /* Pass correct addr len to common routine (so it knows there
4844 * is only one address being passed.
4846 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4853 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4854 int addr_len, int flags)
4856 if (addr_len < sizeof(uaddr->sa_family))
4859 if (uaddr->sa_family == AF_UNSPEC)
4862 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4865 /* FIXME: Write comments. */
4866 static int sctp_disconnect(struct sock *sk, int flags)
4868 return -EOPNOTSUPP; /* STUB */
4871 /* 4.1.4 accept() - TCP Style Syntax
4873 * Applications use accept() call to remove an established SCTP
4874 * association from the accept queue of the endpoint. A new socket
4875 * descriptor will be returned from accept() to represent the newly
4876 * formed association.
4878 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4880 struct sctp_sock *sp;
4881 struct sctp_endpoint *ep;
4882 struct sock *newsk = NULL;
4883 struct sctp_association *asoc;
4892 if (!sctp_style(sk, TCP)) {
4893 error = -EOPNOTSUPP;
4897 if (!sctp_sstate(sk, LISTENING)) {
4902 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4904 error = sctp_wait_for_accept(sk, timeo);
4908 /* We treat the list of associations on the endpoint as the accept
4909 * queue and pick the first association on the list.
4911 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4913 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4919 /* Populate the fields of the newsk from the oldsk and migrate the
4920 * asoc to the newsk.
4922 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4924 sk_common_release(newsk);
4934 /* The SCTP ioctl handler. */
4935 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4942 * SEQPACKET-style sockets in LISTENING state are valid, for
4943 * SCTP, so only discard TCP-style sockets in LISTENING state.
4945 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4950 struct sk_buff *skb;
4951 unsigned int amount = 0;
4953 skb = skb_peek(&sk->sk_receive_queue);
4956 * We will only return the amount of this packet since
4957 * that is all that will be read.
4961 rc = put_user(amount, (int __user *)arg);
4973 /* This is the function which gets called during socket creation to
4974 * initialized the SCTP-specific portion of the sock.
4975 * The sock structure should already be zero-filled memory.
4977 static int sctp_init_sock(struct sock *sk)
4979 struct net *net = sock_net(sk);
4980 struct sctp_sock *sp;
4982 pr_debug("%s: sk:%p\n", __func__, sk);
4986 /* Initialize the SCTP per socket area. */
4987 switch (sk->sk_type) {
4988 case SOCK_SEQPACKET:
4989 sp->type = SCTP_SOCKET_UDP;
4992 sp->type = SCTP_SOCKET_TCP;
4995 return -ESOCKTNOSUPPORT;
4998 sk->sk_gso_type = SKB_GSO_SCTP;
5000 /* Initialize default send parameters. These parameters can be
5001 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
5003 sp->default_stream = 0;
5004 sp->default_ppid = 0;
5005 sp->default_flags = 0;
5006 sp->default_context = 0;
5007 sp->default_timetolive = 0;
5009 sp->default_rcv_context = 0;
5010 sp->max_burst = net->sctp.max_burst;
5012 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5014 /* Initialize default setup parameters. These parameters
5015 * can be modified with the SCTP_INITMSG socket option or
5016 * overridden by the SCTP_INIT CMSG.
5018 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5019 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
5020 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5021 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5023 /* Initialize default RTO related parameters. These parameters can
5024 * be modified for with the SCTP_RTOINFO socket option.
5026 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5027 sp->rtoinfo.srto_max = net->sctp.rto_max;
5028 sp->rtoinfo.srto_min = net->sctp.rto_min;
5030 /* Initialize default association related parameters. These parameters
5031 * can be modified with the SCTP_ASSOCINFO socket option.
5033 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5034 sp->assocparams.sasoc_number_peer_destinations = 0;
5035 sp->assocparams.sasoc_peer_rwnd = 0;
5036 sp->assocparams.sasoc_local_rwnd = 0;
5037 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5039 /* Initialize default event subscriptions. By default, all the
5044 /* Default Peer Address Parameters. These defaults can
5045 * be modified via SCTP_PEER_ADDR_PARAMS
5047 sp->hbinterval = net->sctp.hb_interval;
5048 sp->pathmaxrxt = net->sctp.max_retrans_path;
5049 sp->pf_retrans = net->sctp.pf_retrans;
5050 sp->pathmtu = 0; /* allow default discovery */
5051 sp->sackdelay = net->sctp.sack_timeout;
5053 sp->param_flags = SPP_HB_ENABLE |
5055 SPP_SACKDELAY_ENABLE;
5056 sp->default_ss = SCTP_SS_DEFAULT;
5058 /* If enabled no SCTP message fragmentation will be performed.
5059 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5061 sp->disable_fragments = 0;
5063 /* Enable Nagle algorithm by default. */
5066 sp->recvrcvinfo = 0;
5067 sp->recvnxtinfo = 0;
5069 /* Enable by default. */
5072 /* Auto-close idle associations after the configured
5073 * number of seconds. A value of 0 disables this
5074 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5075 * for UDP-style sockets only.
5079 /* User specified fragmentation limit. */
5082 sp->adaptation_ind = 0;
5084 sp->pf = sctp_get_pf_specific(sk->sk_family);
5086 /* Control variables for partial data delivery. */
5087 atomic_set(&sp->pd_mode, 0);
5088 skb_queue_head_init(&sp->pd_lobby);
5089 sp->frag_interleave = 0;
5091 /* Create a per socket endpoint structure. Even if we
5092 * change the data structure relationships, this may still
5093 * be useful for storing pre-connect address information.
5095 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5101 sk->sk_destruct = sctp_destruct_sock;
5103 SCTP_DBG_OBJCNT_INC(sock);
5106 sk_sockets_allocated_inc(sk);
5107 sock_prot_inuse_add(net, sk->sk_prot, 1);
5109 /* Nothing can fail after this block, otherwise
5110 * sctp_destroy_sock() will be called without addr_wq_lock held
5112 if (net->sctp.default_auto_asconf) {
5113 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
5114 list_add_tail(&sp->auto_asconf_list,
5115 &net->sctp.auto_asconf_splist);
5116 sp->do_auto_asconf = 1;
5117 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5119 sp->do_auto_asconf = 0;
5127 /* Cleanup any SCTP per socket resources. Must be called with
5128 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5130 static void sctp_destroy_sock(struct sock *sk)
5132 struct sctp_sock *sp;
5134 pr_debug("%s: sk:%p\n", __func__, sk);
5136 /* Release our hold on the endpoint. */
5138 /* This could happen during socket init, thus we bail out
5139 * early, since the rest of the below is not setup either.
5144 if (sp->do_auto_asconf) {
5145 sp->do_auto_asconf = 0;
5146 list_del(&sp->auto_asconf_list);
5148 sctp_endpoint_free(sp->ep);
5150 sk_sockets_allocated_dec(sk);
5151 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5155 /* Triggered when there are no references on the socket anymore */
5156 static void sctp_destruct_sock(struct sock *sk)
5158 struct sctp_sock *sp = sctp_sk(sk);
5160 /* Free up the HMAC transform. */
5161 crypto_free_shash(sp->hmac);
5163 inet_sock_destruct(sk);
5166 /* API 4.1.7 shutdown() - TCP Style Syntax
5167 * int shutdown(int socket, int how);
5169 * sd - the socket descriptor of the association to be closed.
5170 * how - Specifies the type of shutdown. The values are
5173 * Disables further receive operations. No SCTP
5174 * protocol action is taken.
5176 * Disables further send operations, and initiates
5177 * the SCTP shutdown sequence.
5179 * Disables further send and receive operations
5180 * and initiates the SCTP shutdown sequence.
5182 static void sctp_shutdown(struct sock *sk, int how)
5184 struct net *net = sock_net(sk);
5185 struct sctp_endpoint *ep;
5187 if (!sctp_style(sk, TCP))
5190 ep = sctp_sk(sk)->ep;
5191 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5192 struct sctp_association *asoc;
5194 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5195 asoc = list_entry(ep->asocs.next,
5196 struct sctp_association, asocs);
5197 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5201 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5202 struct sctp_info *info)
5204 struct sctp_transport *prim;
5205 struct list_head *pos;
5208 memset(info, 0, sizeof(*info));
5210 struct sctp_sock *sp = sctp_sk(sk);
5212 info->sctpi_s_autoclose = sp->autoclose;
5213 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5214 info->sctpi_s_pd_point = sp->pd_point;
5215 info->sctpi_s_nodelay = sp->nodelay;
5216 info->sctpi_s_disable_fragments = sp->disable_fragments;
5217 info->sctpi_s_v4mapped = sp->v4mapped;
5218 info->sctpi_s_frag_interleave = sp->frag_interleave;
5219 info->sctpi_s_type = sp->type;
5224 info->sctpi_tag = asoc->c.my_vtag;
5225 info->sctpi_state = asoc->state;
5226 info->sctpi_rwnd = asoc->a_rwnd;
5227 info->sctpi_unackdata = asoc->unack_data;
5228 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5229 info->sctpi_instrms = asoc->stream.incnt;
5230 info->sctpi_outstrms = asoc->stream.outcnt;
5231 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5232 info->sctpi_inqueue++;
5233 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5234 info->sctpi_outqueue++;
5235 info->sctpi_overall_error = asoc->overall_error_count;
5236 info->sctpi_max_burst = asoc->max_burst;
5237 info->sctpi_maxseg = asoc->frag_point;
5238 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5239 info->sctpi_peer_tag = asoc->c.peer_vtag;
5241 mask = asoc->peer.ecn_capable << 1;
5242 mask = (mask | asoc->peer.ipv4_address) << 1;
5243 mask = (mask | asoc->peer.ipv6_address) << 1;
5244 mask = (mask | asoc->peer.hostname_address) << 1;
5245 mask = (mask | asoc->peer.asconf_capable) << 1;
5246 mask = (mask | asoc->peer.prsctp_capable) << 1;
5247 mask = (mask | asoc->peer.auth_capable);
5248 info->sctpi_peer_capable = mask;
5249 mask = asoc->peer.sack_needed << 1;
5250 mask = (mask | asoc->peer.sack_generation) << 1;
5251 mask = (mask | asoc->peer.zero_window_announced);
5252 info->sctpi_peer_sack = mask;
5254 info->sctpi_isacks = asoc->stats.isacks;
5255 info->sctpi_osacks = asoc->stats.osacks;
5256 info->sctpi_opackets = asoc->stats.opackets;
5257 info->sctpi_ipackets = asoc->stats.ipackets;
5258 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5259 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5260 info->sctpi_idupchunks = asoc->stats.idupchunks;
5261 info->sctpi_gapcnt = asoc->stats.gapcnt;
5262 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5263 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5264 info->sctpi_oodchunks = asoc->stats.oodchunks;
5265 info->sctpi_iodchunks = asoc->stats.iodchunks;
5266 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5267 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5269 prim = asoc->peer.primary_path;
5270 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5271 info->sctpi_p_state = prim->state;
5272 info->sctpi_p_cwnd = prim->cwnd;
5273 info->sctpi_p_srtt = prim->srtt;
5274 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5275 info->sctpi_p_hbinterval = prim->hbinterval;
5276 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5277 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5278 info->sctpi_p_ssthresh = prim->ssthresh;
5279 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5280 info->sctpi_p_flight_size = prim->flight_size;
5281 info->sctpi_p_error = prim->error_count;
5285 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5287 /* use callback to avoid exporting the core structure */
5288 void sctp_transport_walk_start(struct rhashtable_iter *iter)
5290 rhltable_walk_enter(&sctp_transport_hashtable, iter);
5292 rhashtable_walk_start(iter);
5295 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5297 rhashtable_walk_stop(iter);
5298 rhashtable_walk_exit(iter);
5301 struct sctp_transport *sctp_transport_get_next(struct net *net,
5302 struct rhashtable_iter *iter)
5304 struct sctp_transport *t;
5306 t = rhashtable_walk_next(iter);
5307 for (; t; t = rhashtable_walk_next(iter)) {
5309 if (PTR_ERR(t) == -EAGAIN)
5314 if (!sctp_transport_hold(t))
5317 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5318 t->asoc->peer.primary_path == t)
5321 sctp_transport_put(t);
5327 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5328 struct rhashtable_iter *iter,
5331 struct sctp_transport *t;
5334 return SEQ_START_TOKEN;
5336 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5339 sctp_transport_put(t);
5345 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5349 struct sctp_ep_common *epb;
5350 struct sctp_hashbucket *head;
5352 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5354 read_lock_bh(&head->lock);
5355 sctp_for_each_hentry(epb, &head->chain) {
5356 err = cb(sctp_ep(epb), p);
5360 read_unlock_bh(&head->lock);
5365 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5367 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5369 const union sctp_addr *laddr,
5370 const union sctp_addr *paddr, void *p)
5372 struct sctp_transport *transport;
5376 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5381 err = cb(transport, p);
5382 sctp_transport_put(transport);
5386 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5388 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5389 int (*cb_done)(struct sctp_transport *, void *),
5390 struct net *net, int *pos, void *p) {
5391 struct rhashtable_iter hti;
5392 struct sctp_transport *tsp;
5397 sctp_transport_walk_start(&hti);
5399 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5400 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5405 sctp_transport_put(tsp);
5407 sctp_transport_walk_stop(&hti);
5410 if (cb_done && !cb_done(tsp, p)) {
5412 sctp_transport_put(tsp);
5415 sctp_transport_put(tsp);
5420 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5422 /* 7.2.1 Association Status (SCTP_STATUS)
5424 * Applications can retrieve current status information about an
5425 * association, including association state, peer receiver window size,
5426 * number of unacked data chunks, and number of data chunks pending
5427 * receipt. This information is read-only.
5429 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5430 char __user *optval,
5433 struct sctp_status status;
5434 struct sctp_association *asoc = NULL;
5435 struct sctp_transport *transport;
5436 sctp_assoc_t associd;
5439 if (len < sizeof(status)) {
5444 len = sizeof(status);
5445 if (copy_from_user(&status, optval, len)) {
5450 associd = status.sstat_assoc_id;
5451 asoc = sctp_id2assoc(sk, associd);
5457 transport = asoc->peer.primary_path;
5459 status.sstat_assoc_id = sctp_assoc2id(asoc);
5460 status.sstat_state = sctp_assoc_to_state(asoc);
5461 status.sstat_rwnd = asoc->peer.rwnd;
5462 status.sstat_unackdata = asoc->unack_data;
5464 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5465 status.sstat_instrms = asoc->stream.incnt;
5466 status.sstat_outstrms = asoc->stream.outcnt;
5467 status.sstat_fragmentation_point = asoc->frag_point;
5468 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5469 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5470 transport->af_specific->sockaddr_len);
5471 /* Map ipv4 address into v4-mapped-on-v6 address. */
5472 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5473 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5474 status.sstat_primary.spinfo_state = transport->state;
5475 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5476 status.sstat_primary.spinfo_srtt = transport->srtt;
5477 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5478 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5480 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5481 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5483 if (put_user(len, optlen)) {
5488 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5489 __func__, len, status.sstat_state, status.sstat_rwnd,
5490 status.sstat_assoc_id);
5492 if (copy_to_user(optval, &status, len)) {
5502 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5504 * Applications can retrieve information about a specific peer address
5505 * of an association, including its reachability state, congestion
5506 * window, and retransmission timer values. This information is
5509 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5510 char __user *optval,
5513 struct sctp_paddrinfo pinfo;
5514 struct sctp_transport *transport;
5517 if (len < sizeof(pinfo)) {
5522 len = sizeof(pinfo);
5523 if (copy_from_user(&pinfo, optval, len)) {
5528 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5529 pinfo.spinfo_assoc_id);
5533 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5534 pinfo.spinfo_state = transport->state;
5535 pinfo.spinfo_cwnd = transport->cwnd;
5536 pinfo.spinfo_srtt = transport->srtt;
5537 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5538 pinfo.spinfo_mtu = transport->pathmtu;
5540 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5541 pinfo.spinfo_state = SCTP_ACTIVE;
5543 if (put_user(len, optlen)) {
5548 if (copy_to_user(optval, &pinfo, len)) {
5557 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5559 * This option is a on/off flag. If enabled no SCTP message
5560 * fragmentation will be performed. Instead if a message being sent
5561 * exceeds the current PMTU size, the message will NOT be sent and
5562 * instead a error will be indicated to the user.
5564 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5565 char __user *optval, int __user *optlen)
5569 if (len < sizeof(int))
5573 val = (sctp_sk(sk)->disable_fragments == 1);
5574 if (put_user(len, optlen))
5576 if (copy_to_user(optval, &val, len))
5581 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5583 * This socket option is used to specify various notifications and
5584 * ancillary data the user wishes to receive.
5586 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5589 struct sctp_event_subscribe subscribe;
5590 __u8 *sn_type = (__u8 *)&subscribe;
5595 if (len > sizeof(struct sctp_event_subscribe))
5596 len = sizeof(struct sctp_event_subscribe);
5597 if (put_user(len, optlen))
5600 for (i = 0; i < len; i++)
5601 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5602 SCTP_SN_TYPE_BASE + i);
5604 if (copy_to_user(optval, &subscribe, len))
5610 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5612 * This socket option is applicable to the UDP-style socket only. When
5613 * set it will cause associations that are idle for more than the
5614 * specified number of seconds to automatically close. An association
5615 * being idle is defined an association that has NOT sent or received
5616 * user data. The special value of '0' indicates that no automatic
5617 * close of any associations should be performed. The option expects an
5618 * integer defining the number of seconds of idle time before an
5619 * association is closed.
5621 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5623 /* Applicable to UDP-style socket only */
5624 if (sctp_style(sk, TCP))
5626 if (len < sizeof(int))
5629 if (put_user(len, optlen))
5631 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5636 /* Helper routine to branch off an association to a new socket. */
5637 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5639 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5640 struct sctp_sock *sp = sctp_sk(sk);
5641 struct socket *sock;
5644 /* Do not peel off from one netns to another one. */
5645 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5651 /* An association cannot be branched off from an already peeled-off
5652 * socket, nor is this supported for tcp style sockets.
5654 if (!sctp_style(sk, UDP))
5657 /* Create a new socket. */
5658 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5662 sctp_copy_sock(sock->sk, sk, asoc);
5664 /* Make peeled-off sockets more like 1-1 accepted sockets.
5665 * Set the daddr and initialize id to something more random and also
5666 * copy over any ip options.
5668 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5669 sp->pf->copy_ip_options(sk, sock->sk);
5671 /* Populate the fields of the newsk from the oldsk and migrate the
5672 * asoc to the newsk.
5674 err = sctp_sock_migrate(sk, sock->sk, asoc,
5675 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5685 EXPORT_SYMBOL(sctp_do_peeloff);
5687 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5688 struct file **newfile, unsigned flags)
5690 struct socket *newsock;
5693 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5697 /* Map the socket to an unused fd that can be returned to the user. */
5698 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5700 sock_release(newsock);
5704 *newfile = sock_alloc_file(newsock, 0, NULL);
5705 if (IS_ERR(*newfile)) {
5706 put_unused_fd(retval);
5707 retval = PTR_ERR(*newfile);
5712 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5715 peeloff->sd = retval;
5717 if (flags & SOCK_NONBLOCK)
5718 (*newfile)->f_flags |= O_NONBLOCK;
5723 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5725 sctp_peeloff_arg_t peeloff;
5726 struct file *newfile = NULL;
5729 if (len < sizeof(sctp_peeloff_arg_t))
5731 len = sizeof(sctp_peeloff_arg_t);
5732 if (copy_from_user(&peeloff, optval, len))
5735 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5739 /* Return the fd mapped to the new socket. */
5740 if (put_user(len, optlen)) {
5742 put_unused_fd(retval);
5746 if (copy_to_user(optval, &peeloff, len)) {
5748 put_unused_fd(retval);
5751 fd_install(retval, newfile);
5756 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5757 char __user *optval, int __user *optlen)
5759 sctp_peeloff_flags_arg_t peeloff;
5760 struct file *newfile = NULL;
5763 if (len < sizeof(sctp_peeloff_flags_arg_t))
5765 len = sizeof(sctp_peeloff_flags_arg_t);
5766 if (copy_from_user(&peeloff, optval, len))
5769 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5770 &newfile, peeloff.flags);
5774 /* Return the fd mapped to the new socket. */
5775 if (put_user(len, optlen)) {
5777 put_unused_fd(retval);
5781 if (copy_to_user(optval, &peeloff, len)) {
5783 put_unused_fd(retval);
5786 fd_install(retval, newfile);
5791 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5793 * Applications can enable or disable heartbeats for any peer address of
5794 * an association, modify an address's heartbeat interval, force a
5795 * heartbeat to be sent immediately, and adjust the address's maximum
5796 * number of retransmissions sent before an address is considered
5797 * unreachable. The following structure is used to access and modify an
5798 * address's parameters:
5800 * struct sctp_paddrparams {
5801 * sctp_assoc_t spp_assoc_id;
5802 * struct sockaddr_storage spp_address;
5803 * uint32_t spp_hbinterval;
5804 * uint16_t spp_pathmaxrxt;
5805 * uint32_t spp_pathmtu;
5806 * uint32_t spp_sackdelay;
5807 * uint32_t spp_flags;
5810 * spp_assoc_id - (one-to-many style socket) This is filled in the
5811 * application, and identifies the association for
5813 * spp_address - This specifies which address is of interest.
5814 * spp_hbinterval - This contains the value of the heartbeat interval,
5815 * in milliseconds. If a value of zero
5816 * is present in this field then no changes are to
5817 * be made to this parameter.
5818 * spp_pathmaxrxt - This contains the maximum number of
5819 * retransmissions before this address shall be
5820 * considered unreachable. If a value of zero
5821 * is present in this field then no changes are to
5822 * be made to this parameter.
5823 * spp_pathmtu - When Path MTU discovery is disabled the value
5824 * specified here will be the "fixed" path mtu.
5825 * Note that if the spp_address field is empty
5826 * then all associations on this address will
5827 * have this fixed path mtu set upon them.
5829 * spp_sackdelay - When delayed sack is enabled, this value specifies
5830 * the number of milliseconds that sacks will be delayed
5831 * for. This value will apply to all addresses of an
5832 * association if the spp_address field is empty. Note
5833 * also, that if delayed sack is enabled and this
5834 * value is set to 0, no change is made to the last
5835 * recorded delayed sack timer value.
5837 * spp_flags - These flags are used to control various features
5838 * on an association. The flag field may contain
5839 * zero or more of the following options.
5841 * SPP_HB_ENABLE - Enable heartbeats on the
5842 * specified address. Note that if the address
5843 * field is empty all addresses for the association
5844 * have heartbeats enabled upon them.
5846 * SPP_HB_DISABLE - Disable heartbeats on the
5847 * speicifed address. Note that if the address
5848 * field is empty all addresses for the association
5849 * will have their heartbeats disabled. Note also
5850 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5851 * mutually exclusive, only one of these two should
5852 * be specified. Enabling both fields will have
5853 * undetermined results.
5855 * SPP_HB_DEMAND - Request a user initiated heartbeat
5856 * to be made immediately.
5858 * SPP_PMTUD_ENABLE - This field will enable PMTU
5859 * discovery upon the specified address. Note that
5860 * if the address feild is empty then all addresses
5861 * on the association are effected.
5863 * SPP_PMTUD_DISABLE - This field will disable PMTU
5864 * discovery upon the specified address. Note that
5865 * if the address feild is empty then all addresses
5866 * on the association are effected. Not also that
5867 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5868 * exclusive. Enabling both will have undetermined
5871 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5872 * on delayed sack. The time specified in spp_sackdelay
5873 * is used to specify the sack delay for this address. Note
5874 * that if spp_address is empty then all addresses will
5875 * enable delayed sack and take on the sack delay
5876 * value specified in spp_sackdelay.
5877 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5878 * off delayed sack. If the spp_address field is blank then
5879 * delayed sack is disabled for the entire association. Note
5880 * also that this field is mutually exclusive to
5881 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5884 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5885 * setting of the IPV6 flow label value. The value is
5886 * contained in the spp_ipv6_flowlabel field.
5887 * Upon retrieval, this flag will be set to indicate that
5888 * the spp_ipv6_flowlabel field has a valid value returned.
5889 * If a specific destination address is set (in the
5890 * spp_address field), then the value returned is that of
5891 * the address. If just an association is specified (and
5892 * no address), then the association's default flow label
5893 * is returned. If neither an association nor a destination
5894 * is specified, then the socket's default flow label is
5895 * returned. For non-IPv6 sockets, this flag will be left
5898 * SPP_DSCP: Setting this flag enables the setting of the
5899 * Differentiated Services Code Point (DSCP) value
5900 * associated with either the association or a specific
5901 * address. The value is obtained in the spp_dscp field.
5902 * Upon retrieval, this flag will be set to indicate that
5903 * the spp_dscp field has a valid value returned. If a
5904 * specific destination address is set when called (in the
5905 * spp_address field), then that specific destination
5906 * address's DSCP value is returned. If just an association
5907 * is specified, then the association's default DSCP is
5908 * returned. If neither an association nor a destination is
5909 * specified, then the socket's default DSCP is returned.
5911 * spp_ipv6_flowlabel
5912 * - This field is used in conjunction with the
5913 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5914 * The 20 least significant bits are used for the flow
5915 * label. This setting has precedence over any IPv6-layer
5918 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5919 * and contains the DSCP. The 6 most significant bits are
5920 * used for the DSCP. This setting has precedence over any
5921 * IPv4- or IPv6- layer setting.
5923 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5924 char __user *optval, int __user *optlen)
5926 struct sctp_paddrparams params;
5927 struct sctp_transport *trans = NULL;
5928 struct sctp_association *asoc = NULL;
5929 struct sctp_sock *sp = sctp_sk(sk);
5931 if (len >= sizeof(params))
5932 len = sizeof(params);
5933 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5934 spp_ipv6_flowlabel), 4))
5935 len = ALIGN(offsetof(struct sctp_paddrparams,
5936 spp_ipv6_flowlabel), 4);
5940 if (copy_from_user(¶ms, optval, len))
5943 /* If an address other than INADDR_ANY is specified, and
5944 * no transport is found, then the request is invalid.
5946 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5947 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5948 params.spp_assoc_id);
5950 pr_debug("%s: failed no transport\n", __func__);
5955 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5956 * socket is a one to many style socket, and an association
5957 * was not found, then the id was invalid.
5959 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5960 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5961 sctp_style(sk, UDP)) {
5962 pr_debug("%s: failed no association\n", __func__);
5967 /* Fetch transport values. */
5968 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5969 params.spp_pathmtu = trans->pathmtu;
5970 params.spp_pathmaxrxt = trans->pathmaxrxt;
5971 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5973 /*draft-11 doesn't say what to return in spp_flags*/
5974 params.spp_flags = trans->param_flags;
5975 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5976 params.spp_ipv6_flowlabel = trans->flowlabel &
5977 SCTP_FLOWLABEL_VAL_MASK;
5978 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5980 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5981 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5982 params.spp_flags |= SPP_DSCP;
5985 /* Fetch association values. */
5986 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5987 params.spp_pathmtu = asoc->pathmtu;
5988 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5989 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5991 /*draft-11 doesn't say what to return in spp_flags*/
5992 params.spp_flags = asoc->param_flags;
5993 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5994 params.spp_ipv6_flowlabel = asoc->flowlabel &
5995 SCTP_FLOWLABEL_VAL_MASK;
5996 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5998 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5999 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6000 params.spp_flags |= SPP_DSCP;
6003 /* Fetch socket values. */
6004 params.spp_hbinterval = sp->hbinterval;
6005 params.spp_pathmtu = sp->pathmtu;
6006 params.spp_sackdelay = sp->sackdelay;
6007 params.spp_pathmaxrxt = sp->pathmaxrxt;
6009 /*draft-11 doesn't say what to return in spp_flags*/
6010 params.spp_flags = sp->param_flags;
6011 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6012 params.spp_ipv6_flowlabel = sp->flowlabel &
6013 SCTP_FLOWLABEL_VAL_MASK;
6014 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6016 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6017 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6018 params.spp_flags |= SPP_DSCP;
6022 if (copy_to_user(optval, ¶ms, len))
6025 if (put_user(len, optlen))
6032 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6034 * This option will effect the way delayed acks are performed. This
6035 * option allows you to get or set the delayed ack time, in
6036 * milliseconds. It also allows changing the delayed ack frequency.
6037 * Changing the frequency to 1 disables the delayed sack algorithm. If
6038 * the assoc_id is 0, then this sets or gets the endpoints default
6039 * values. If the assoc_id field is non-zero, then the set or get
6040 * effects the specified association for the one to many model (the
6041 * assoc_id field is ignored by the one to one model). Note that if
6042 * sack_delay or sack_freq are 0 when setting this option, then the
6043 * current values will remain unchanged.
6045 * struct sctp_sack_info {
6046 * sctp_assoc_t sack_assoc_id;
6047 * uint32_t sack_delay;
6048 * uint32_t sack_freq;
6051 * sack_assoc_id - This parameter, indicates which association the user
6052 * is performing an action upon. Note that if this field's value is
6053 * zero then the endpoints default value is changed (effecting future
6054 * associations only).
6056 * sack_delay - This parameter contains the number of milliseconds that
6057 * the user is requesting the delayed ACK timer be set to. Note that
6058 * this value is defined in the standard to be between 200 and 500
6061 * sack_freq - This parameter contains the number of packets that must
6062 * be received before a sack is sent without waiting for the delay
6063 * timer to expire. The default value for this is 2, setting this
6064 * value to 1 will disable the delayed sack algorithm.
6066 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6067 char __user *optval,
6070 struct sctp_sack_info params;
6071 struct sctp_association *asoc = NULL;
6072 struct sctp_sock *sp = sctp_sk(sk);
6074 if (len >= sizeof(struct sctp_sack_info)) {
6075 len = sizeof(struct sctp_sack_info);
6077 if (copy_from_user(¶ms, optval, len))
6079 } else if (len == sizeof(struct sctp_assoc_value)) {
6080 pr_warn_ratelimited(DEPRECATED
6082 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6083 "Use struct sctp_sack_info instead\n",
6084 current->comm, task_pid_nr(current));
6085 if (copy_from_user(¶ms, optval, len))
6090 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6091 * socket is a one to many style socket, and an association
6092 * was not found, then the id was invalid.
6094 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6095 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6096 sctp_style(sk, UDP))
6100 /* Fetch association values. */
6101 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6102 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6103 params.sack_freq = asoc->sackfreq;
6106 params.sack_delay = 0;
6107 params.sack_freq = 1;
6110 /* Fetch socket values. */
6111 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6112 params.sack_delay = sp->sackdelay;
6113 params.sack_freq = sp->sackfreq;
6115 params.sack_delay = 0;
6116 params.sack_freq = 1;
6120 if (copy_to_user(optval, ¶ms, len))
6123 if (put_user(len, optlen))
6129 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6131 * Applications can specify protocol parameters for the default association
6132 * initialization. The option name argument to setsockopt() and getsockopt()
6135 * Setting initialization parameters is effective only on an unconnected
6136 * socket (for UDP-style sockets only future associations are effected
6137 * by the change). With TCP-style sockets, this option is inherited by
6138 * sockets derived from a listener socket.
6140 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6142 if (len < sizeof(struct sctp_initmsg))
6144 len = sizeof(struct sctp_initmsg);
6145 if (put_user(len, optlen))
6147 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6153 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6154 char __user *optval, int __user *optlen)
6156 struct sctp_association *asoc;
6158 struct sctp_getaddrs getaddrs;
6159 struct sctp_transport *from;
6161 union sctp_addr temp;
6162 struct sctp_sock *sp = sctp_sk(sk);
6167 if (len < sizeof(struct sctp_getaddrs))
6170 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6173 /* For UDP-style sockets, id specifies the association to query. */
6174 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6178 to = optval + offsetof(struct sctp_getaddrs, addrs);
6179 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6181 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6183 memcpy(&temp, &from->ipaddr, sizeof(temp));
6184 addrlen = sctp_get_pf_specific(sk->sk_family)
6185 ->addr_to_user(sp, &temp);
6186 if (space_left < addrlen)
6188 if (copy_to_user(to, &temp, addrlen))
6192 space_left -= addrlen;
6195 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6197 bytes_copied = ((char __user *)to) - optval;
6198 if (put_user(bytes_copied, optlen))
6204 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6205 size_t space_left, int *bytes_copied)
6207 struct sctp_sockaddr_entry *addr;
6208 union sctp_addr temp;
6211 struct net *net = sock_net(sk);
6214 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6218 if ((PF_INET == sk->sk_family) &&
6219 (AF_INET6 == addr->a.sa.sa_family))
6221 if ((PF_INET6 == sk->sk_family) &&
6222 inet_v6_ipv6only(sk) &&
6223 (AF_INET == addr->a.sa.sa_family))
6225 memcpy(&temp, &addr->a, sizeof(temp));
6226 if (!temp.v4.sin_port)
6227 temp.v4.sin_port = htons(port);
6229 addrlen = sctp_get_pf_specific(sk->sk_family)
6230 ->addr_to_user(sctp_sk(sk), &temp);
6232 if (space_left < addrlen) {
6236 memcpy(to, &temp, addrlen);
6240 space_left -= addrlen;
6241 *bytes_copied += addrlen;
6249 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6250 char __user *optval, int __user *optlen)
6252 struct sctp_bind_addr *bp;
6253 struct sctp_association *asoc;
6255 struct sctp_getaddrs getaddrs;
6256 struct sctp_sockaddr_entry *addr;
6258 union sctp_addr temp;
6259 struct sctp_sock *sp = sctp_sk(sk);
6263 int bytes_copied = 0;
6267 if (len < sizeof(struct sctp_getaddrs))
6270 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6274 * For UDP-style sockets, id specifies the association to query.
6275 * If the id field is set to the value '0' then the locally bound
6276 * addresses are returned without regard to any particular
6279 if (0 == getaddrs.assoc_id) {
6280 bp = &sctp_sk(sk)->ep->base.bind_addr;
6282 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6285 bp = &asoc->base.bind_addr;
6288 to = optval + offsetof(struct sctp_getaddrs, addrs);
6289 space_left = len - offsetof(struct sctp_getaddrs, addrs);
6291 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6295 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6296 * addresses from the global local address list.
6298 if (sctp_list_single_entry(&bp->address_list)) {
6299 addr = list_entry(bp->address_list.next,
6300 struct sctp_sockaddr_entry, list);
6301 if (sctp_is_any(sk, &addr->a)) {
6302 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6303 space_left, &bytes_copied);
6313 /* Protection on the bound address list is not needed since
6314 * in the socket option context we hold a socket lock and
6315 * thus the bound address list can't change.
6317 list_for_each_entry(addr, &bp->address_list, list) {
6318 memcpy(&temp, &addr->a, sizeof(temp));
6319 addrlen = sctp_get_pf_specific(sk->sk_family)
6320 ->addr_to_user(sp, &temp);
6321 if (space_left < addrlen) {
6322 err = -ENOMEM; /*fixme: right error?*/
6325 memcpy(buf, &temp, addrlen);
6327 bytes_copied += addrlen;
6329 space_left -= addrlen;
6333 if (copy_to_user(to, addrs, bytes_copied)) {
6337 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6341 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6342 * but we can't change it anymore.
6344 if (put_user(bytes_copied, optlen))
6351 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6353 * Requests that the local SCTP stack use the enclosed peer address as
6354 * the association primary. The enclosed address must be one of the
6355 * association peer's addresses.
6357 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6358 char __user *optval, int __user *optlen)
6360 struct sctp_prim prim;
6361 struct sctp_association *asoc;
6362 struct sctp_sock *sp = sctp_sk(sk);
6364 if (len < sizeof(struct sctp_prim))
6367 len = sizeof(struct sctp_prim);
6369 if (copy_from_user(&prim, optval, len))
6372 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6376 if (!asoc->peer.primary_path)
6379 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6380 asoc->peer.primary_path->af_specific->sockaddr_len);
6382 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6383 (union sctp_addr *)&prim.ssp_addr);
6385 if (put_user(len, optlen))
6387 if (copy_to_user(optval, &prim, len))
6394 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6396 * Requests that the local endpoint set the specified Adaptation Layer
6397 * Indication parameter for all future INIT and INIT-ACK exchanges.
6399 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6400 char __user *optval, int __user *optlen)
6402 struct sctp_setadaptation adaptation;
6404 if (len < sizeof(struct sctp_setadaptation))
6407 len = sizeof(struct sctp_setadaptation);
6409 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6411 if (put_user(len, optlen))
6413 if (copy_to_user(optval, &adaptation, len))
6421 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6423 * Applications that wish to use the sendto() system call may wish to
6424 * specify a default set of parameters that would normally be supplied
6425 * through the inclusion of ancillary data. This socket option allows
6426 * such an application to set the default sctp_sndrcvinfo structure.
6429 * The application that wishes to use this socket option simply passes
6430 * in to this call the sctp_sndrcvinfo structure defined in Section
6431 * 5.2.2) The input parameters accepted by this call include
6432 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6433 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6434 * to this call if the caller is using the UDP model.
6436 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6438 static int sctp_getsockopt_default_send_param(struct sock *sk,
6439 int len, char __user *optval,
6442 struct sctp_sock *sp = sctp_sk(sk);
6443 struct sctp_association *asoc;
6444 struct sctp_sndrcvinfo info;
6446 if (len < sizeof(info))
6451 if (copy_from_user(&info, optval, len))
6454 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6455 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6456 sctp_style(sk, UDP))
6460 info.sinfo_stream = asoc->default_stream;
6461 info.sinfo_flags = asoc->default_flags;
6462 info.sinfo_ppid = asoc->default_ppid;
6463 info.sinfo_context = asoc->default_context;
6464 info.sinfo_timetolive = asoc->default_timetolive;
6466 info.sinfo_stream = sp->default_stream;
6467 info.sinfo_flags = sp->default_flags;
6468 info.sinfo_ppid = sp->default_ppid;
6469 info.sinfo_context = sp->default_context;
6470 info.sinfo_timetolive = sp->default_timetolive;
6473 if (put_user(len, optlen))
6475 if (copy_to_user(optval, &info, len))
6481 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6482 * (SCTP_DEFAULT_SNDINFO)
6484 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6485 char __user *optval,
6488 struct sctp_sock *sp = sctp_sk(sk);
6489 struct sctp_association *asoc;
6490 struct sctp_sndinfo info;
6492 if (len < sizeof(info))
6497 if (copy_from_user(&info, optval, len))
6500 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6501 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6502 sctp_style(sk, UDP))
6506 info.snd_sid = asoc->default_stream;
6507 info.snd_flags = asoc->default_flags;
6508 info.snd_ppid = asoc->default_ppid;
6509 info.snd_context = asoc->default_context;
6511 info.snd_sid = sp->default_stream;
6512 info.snd_flags = sp->default_flags;
6513 info.snd_ppid = sp->default_ppid;
6514 info.snd_context = sp->default_context;
6517 if (put_user(len, optlen))
6519 if (copy_to_user(optval, &info, len))
6527 * 7.1.5 SCTP_NODELAY
6529 * Turn on/off any Nagle-like algorithm. This means that packets are
6530 * generally sent as soon as possible and no unnecessary delays are
6531 * introduced, at the cost of more packets in the network. Expects an
6532 * integer boolean flag.
6535 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6536 char __user *optval, int __user *optlen)
6540 if (len < sizeof(int))
6544 val = (sctp_sk(sk)->nodelay == 1);
6545 if (put_user(len, optlen))
6547 if (copy_to_user(optval, &val, len))
6554 * 7.1.1 SCTP_RTOINFO
6556 * The protocol parameters used to initialize and bound retransmission
6557 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6558 * and modify these parameters.
6559 * All parameters are time values, in milliseconds. A value of 0, when
6560 * modifying the parameters, indicates that the current value should not
6564 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6565 char __user *optval,
6566 int __user *optlen) {
6567 struct sctp_rtoinfo rtoinfo;
6568 struct sctp_association *asoc;
6570 if (len < sizeof (struct sctp_rtoinfo))
6573 len = sizeof(struct sctp_rtoinfo);
6575 if (copy_from_user(&rtoinfo, optval, len))
6578 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6580 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6581 sctp_style(sk, UDP))
6584 /* Values corresponding to the specific association. */
6586 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6587 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6588 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6590 /* Values corresponding to the endpoint. */
6591 struct sctp_sock *sp = sctp_sk(sk);
6593 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6594 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6595 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6598 if (put_user(len, optlen))
6601 if (copy_to_user(optval, &rtoinfo, len))
6609 * 7.1.2 SCTP_ASSOCINFO
6611 * This option is used to tune the maximum retransmission attempts
6612 * of the association.
6613 * Returns an error if the new association retransmission value is
6614 * greater than the sum of the retransmission value of the peer.
6615 * See [SCTP] for more information.
6618 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6619 char __user *optval,
6623 struct sctp_assocparams assocparams;
6624 struct sctp_association *asoc;
6625 struct list_head *pos;
6628 if (len < sizeof (struct sctp_assocparams))
6631 len = sizeof(struct sctp_assocparams);
6633 if (copy_from_user(&assocparams, optval, len))
6636 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6638 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6639 sctp_style(sk, UDP))
6642 /* Values correspoinding to the specific association */
6644 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6645 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6646 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6647 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6649 list_for_each(pos, &asoc->peer.transport_addr_list) {
6653 assocparams.sasoc_number_peer_destinations = cnt;
6655 /* Values corresponding to the endpoint */
6656 struct sctp_sock *sp = sctp_sk(sk);
6658 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6659 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6660 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6661 assocparams.sasoc_cookie_life =
6662 sp->assocparams.sasoc_cookie_life;
6663 assocparams.sasoc_number_peer_destinations =
6665 sasoc_number_peer_destinations;
6668 if (put_user(len, optlen))
6671 if (copy_to_user(optval, &assocparams, len))
6678 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6680 * This socket option is a boolean flag which turns on or off mapped V4
6681 * addresses. If this option is turned on and the socket is type
6682 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6683 * If this option is turned off, then no mapping will be done of V4
6684 * addresses and a user will receive both PF_INET6 and PF_INET type
6685 * addresses on the socket.
6687 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6688 char __user *optval, int __user *optlen)
6691 struct sctp_sock *sp = sctp_sk(sk);
6693 if (len < sizeof(int))
6698 if (put_user(len, optlen))
6700 if (copy_to_user(optval, &val, len))
6707 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6708 * (chapter and verse is quoted at sctp_setsockopt_context())
6710 static int sctp_getsockopt_context(struct sock *sk, int len,
6711 char __user *optval, int __user *optlen)
6713 struct sctp_assoc_value params;
6714 struct sctp_association *asoc;
6716 if (len < sizeof(struct sctp_assoc_value))
6719 len = sizeof(struct sctp_assoc_value);
6721 if (copy_from_user(¶ms, optval, len))
6724 asoc = sctp_id2assoc(sk, params.assoc_id);
6725 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6726 sctp_style(sk, UDP))
6729 params.assoc_value = asoc ? asoc->default_rcv_context
6730 : sctp_sk(sk)->default_rcv_context;
6732 if (put_user(len, optlen))
6734 if (copy_to_user(optval, ¶ms, len))
6741 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6742 * This option will get or set the maximum size to put in any outgoing
6743 * SCTP DATA chunk. If a message is larger than this size it will be
6744 * fragmented by SCTP into the specified size. Note that the underlying
6745 * SCTP implementation may fragment into smaller sized chunks when the
6746 * PMTU of the underlying association is smaller than the value set by
6747 * the user. The default value for this option is '0' which indicates
6748 * the user is NOT limiting fragmentation and only the PMTU will effect
6749 * SCTP's choice of DATA chunk size. Note also that values set larger
6750 * than the maximum size of an IP datagram will effectively let SCTP
6751 * control fragmentation (i.e. the same as setting this option to 0).
6753 * The following structure is used to access and modify this parameter:
6755 * struct sctp_assoc_value {
6756 * sctp_assoc_t assoc_id;
6757 * uint32_t assoc_value;
6760 * assoc_id: This parameter is ignored for one-to-one style sockets.
6761 * For one-to-many style sockets this parameter indicates which
6762 * association the user is performing an action upon. Note that if
6763 * this field's value is zero then the endpoints default value is
6764 * changed (effecting future associations only).
6765 * assoc_value: This parameter specifies the maximum size in bytes.
6767 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6768 char __user *optval, int __user *optlen)
6770 struct sctp_assoc_value params;
6771 struct sctp_association *asoc;
6773 if (len == sizeof(int)) {
6774 pr_warn_ratelimited(DEPRECATED
6776 "Use of int in maxseg socket option.\n"
6777 "Use struct sctp_assoc_value instead\n",
6778 current->comm, task_pid_nr(current));
6779 params.assoc_id = SCTP_FUTURE_ASSOC;
6780 } else if (len >= sizeof(struct sctp_assoc_value)) {
6781 len = sizeof(struct sctp_assoc_value);
6782 if (copy_from_user(¶ms, optval, len))
6787 asoc = sctp_id2assoc(sk, params.assoc_id);
6788 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6789 sctp_style(sk, UDP))
6793 params.assoc_value = asoc->frag_point;
6795 params.assoc_value = sctp_sk(sk)->user_frag;
6797 if (put_user(len, optlen))
6799 if (len == sizeof(int)) {
6800 if (copy_to_user(optval, ¶ms.assoc_value, len))
6803 if (copy_to_user(optval, ¶ms, len))
6811 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6812 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6814 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6815 char __user *optval, int __user *optlen)
6819 if (len < sizeof(int))
6824 val = sctp_sk(sk)->frag_interleave;
6825 if (put_user(len, optlen))
6827 if (copy_to_user(optval, &val, len))
6834 * 7.1.25. Set or Get the sctp partial delivery point
6835 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6837 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6838 char __user *optval,
6843 if (len < sizeof(u32))
6848 val = sctp_sk(sk)->pd_point;
6849 if (put_user(len, optlen))
6851 if (copy_to_user(optval, &val, len))
6858 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6859 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6861 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6862 char __user *optval,
6865 struct sctp_assoc_value params;
6866 struct sctp_association *asoc;
6868 if (len == sizeof(int)) {
6869 pr_warn_ratelimited(DEPRECATED
6871 "Use of int in max_burst socket option.\n"
6872 "Use struct sctp_assoc_value instead\n",
6873 current->comm, task_pid_nr(current));
6874 params.assoc_id = SCTP_FUTURE_ASSOC;
6875 } else if (len >= sizeof(struct sctp_assoc_value)) {
6876 len = sizeof(struct sctp_assoc_value);
6877 if (copy_from_user(¶ms, optval, len))
6882 asoc = sctp_id2assoc(sk, params.assoc_id);
6883 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6884 sctp_style(sk, UDP))
6887 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6889 if (len == sizeof(int)) {
6890 if (copy_to_user(optval, ¶ms.assoc_value, len))
6893 if (copy_to_user(optval, ¶ms, len))
6901 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6902 char __user *optval, int __user *optlen)
6904 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6905 struct sctp_hmacalgo __user *p = (void __user *)optval;
6906 struct sctp_hmac_algo_param *hmacs;
6911 if (!ep->auth_enable)
6914 hmacs = ep->auth_hmacs_list;
6915 data_len = ntohs(hmacs->param_hdr.length) -
6916 sizeof(struct sctp_paramhdr);
6918 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6921 len = sizeof(struct sctp_hmacalgo) + data_len;
6922 num_idents = data_len / sizeof(u16);
6924 if (put_user(len, optlen))
6926 if (put_user(num_idents, &p->shmac_num_idents))
6928 for (i = 0; i < num_idents; i++) {
6929 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6931 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6937 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6938 char __user *optval, int __user *optlen)
6940 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6941 struct sctp_authkeyid val;
6942 struct sctp_association *asoc;
6944 if (!ep->auth_enable)
6947 if (len < sizeof(struct sctp_authkeyid))
6950 len = sizeof(struct sctp_authkeyid);
6951 if (copy_from_user(&val, optval, len))
6954 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6955 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6959 val.scact_keynumber = asoc->active_key_id;
6961 val.scact_keynumber = ep->active_key_id;
6963 if (put_user(len, optlen))
6965 if (copy_to_user(optval, &val, len))
6971 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6972 char __user *optval, int __user *optlen)
6974 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6975 struct sctp_authchunks __user *p = (void __user *)optval;
6976 struct sctp_authchunks val;
6977 struct sctp_association *asoc;
6978 struct sctp_chunks_param *ch;
6982 if (!ep->auth_enable)
6985 if (len < sizeof(struct sctp_authchunks))
6988 if (copy_from_user(&val, optval, sizeof(val)))
6991 to = p->gauth_chunks;
6992 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6996 ch = asoc->peer.peer_chunks;
7000 /* See if the user provided enough room for all the data */
7001 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7002 if (len < num_chunks)
7005 if (copy_to_user(to, ch->chunks, num_chunks))
7008 len = sizeof(struct sctp_authchunks) + num_chunks;
7009 if (put_user(len, optlen))
7011 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7016 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
7017 char __user *optval, int __user *optlen)
7019 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7020 struct sctp_authchunks __user *p = (void __user *)optval;
7021 struct sctp_authchunks val;
7022 struct sctp_association *asoc;
7023 struct sctp_chunks_param *ch;
7027 if (!ep->auth_enable)
7030 if (len < sizeof(struct sctp_authchunks))
7033 if (copy_from_user(&val, optval, sizeof(val)))
7036 to = p->gauth_chunks;
7037 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7038 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7039 sctp_style(sk, UDP))
7042 ch = asoc ? (struct sctp_chunks_param *)asoc->c.auth_chunks
7043 : ep->auth_chunk_list;
7047 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7048 if (len < sizeof(struct sctp_authchunks) + num_chunks)
7051 if (copy_to_user(to, ch->chunks, num_chunks))
7054 len = sizeof(struct sctp_authchunks) + num_chunks;
7055 if (put_user(len, optlen))
7057 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7064 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7065 * This option gets the current number of associations that are attached
7066 * to a one-to-many style socket. The option value is an uint32_t.
7068 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7069 char __user *optval, int __user *optlen)
7071 struct sctp_sock *sp = sctp_sk(sk);
7072 struct sctp_association *asoc;
7075 if (sctp_style(sk, TCP))
7078 if (len < sizeof(u32))
7083 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7087 if (put_user(len, optlen))
7089 if (copy_to_user(optval, &val, len))
7096 * 8.1.23 SCTP_AUTO_ASCONF
7097 * See the corresponding setsockopt entry as description
7099 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7100 char __user *optval, int __user *optlen)
7104 if (len < sizeof(int))
7108 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7110 if (put_user(len, optlen))
7112 if (copy_to_user(optval, &val, len))
7118 * 8.2.6. Get the Current Identifiers of Associations
7119 * (SCTP_GET_ASSOC_ID_LIST)
7121 * This option gets the current list of SCTP association identifiers of
7122 * the SCTP associations handled by a one-to-many style socket.
7124 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7125 char __user *optval, int __user *optlen)
7127 struct sctp_sock *sp = sctp_sk(sk);
7128 struct sctp_association *asoc;
7129 struct sctp_assoc_ids *ids;
7132 if (sctp_style(sk, TCP))
7135 if (len < sizeof(struct sctp_assoc_ids))
7138 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7142 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7145 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7147 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7151 ids->gaids_number_of_ids = num;
7153 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7154 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7157 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7167 * SCTP_PEER_ADDR_THLDS
7169 * This option allows us to fetch the partially failed threshold for one or all
7170 * transports in an association. See Section 6.1 of:
7171 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7173 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7174 char __user *optval,
7178 struct sctp_paddrthlds val;
7179 struct sctp_transport *trans;
7180 struct sctp_association *asoc;
7182 if (len < sizeof(struct sctp_paddrthlds))
7184 len = sizeof(struct sctp_paddrthlds);
7185 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7188 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7189 trans = sctp_addr_id2transport(sk, &val.spt_address,
7194 val.spt_pathmaxrxt = trans->pathmaxrxt;
7195 val.spt_pathpfthld = trans->pf_retrans;
7200 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7201 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7202 sctp_style(sk, UDP))
7206 val.spt_pathpfthld = asoc->pf_retrans;
7207 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7209 struct sctp_sock *sp = sctp_sk(sk);
7211 val.spt_pathpfthld = sp->pf_retrans;
7212 val.spt_pathmaxrxt = sp->pathmaxrxt;
7215 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7222 * SCTP_GET_ASSOC_STATS
7224 * This option retrieves local per endpoint statistics. It is modeled
7225 * after OpenSolaris' implementation
7227 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7228 char __user *optval,
7231 struct sctp_assoc_stats sas;
7232 struct sctp_association *asoc = NULL;
7234 /* User must provide at least the assoc id */
7235 if (len < sizeof(sctp_assoc_t))
7238 /* Allow the struct to grow and fill in as much as possible */
7239 len = min_t(size_t, len, sizeof(sas));
7241 if (copy_from_user(&sas, optval, len))
7244 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7248 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7249 sas.sas_gapcnt = asoc->stats.gapcnt;
7250 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7251 sas.sas_osacks = asoc->stats.osacks;
7252 sas.sas_isacks = asoc->stats.isacks;
7253 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7254 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7255 sas.sas_oodchunks = asoc->stats.oodchunks;
7256 sas.sas_iodchunks = asoc->stats.iodchunks;
7257 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7258 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7259 sas.sas_idupchunks = asoc->stats.idupchunks;
7260 sas.sas_opackets = asoc->stats.opackets;
7261 sas.sas_ipackets = asoc->stats.ipackets;
7263 /* New high max rto observed, will return 0 if not a single
7264 * RTO update took place. obs_rto_ipaddr will be bogus
7267 sas.sas_maxrto = asoc->stats.max_obs_rto;
7268 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7269 sizeof(struct sockaddr_storage));
7271 /* Mark beginning of a new observation period */
7272 asoc->stats.max_obs_rto = asoc->rto_min;
7274 if (put_user(len, optlen))
7277 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7279 if (copy_to_user(optval, &sas, len))
7285 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7286 char __user *optval,
7291 if (len < sizeof(int))
7295 if (sctp_sk(sk)->recvrcvinfo)
7297 if (put_user(len, optlen))
7299 if (copy_to_user(optval, &val, len))
7305 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7306 char __user *optval,
7311 if (len < sizeof(int))
7315 if (sctp_sk(sk)->recvnxtinfo)
7317 if (put_user(len, optlen))
7319 if (copy_to_user(optval, &val, len))
7325 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7326 char __user *optval,
7329 struct sctp_assoc_value params;
7330 struct sctp_association *asoc;
7331 int retval = -EFAULT;
7333 if (len < sizeof(params)) {
7338 len = sizeof(params);
7339 if (copy_from_user(¶ms, optval, len))
7342 asoc = sctp_id2assoc(sk, params.assoc_id);
7343 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7344 sctp_style(sk, UDP)) {
7349 params.assoc_value = asoc ? asoc->prsctp_enable
7350 : sctp_sk(sk)->ep->prsctp_enable;
7352 if (put_user(len, optlen))
7355 if (copy_to_user(optval, ¶ms, len))
7364 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7365 char __user *optval,
7368 struct sctp_default_prinfo info;
7369 struct sctp_association *asoc;
7370 int retval = -EFAULT;
7372 if (len < sizeof(info)) {
7378 if (copy_from_user(&info, optval, len))
7381 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7382 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7383 sctp_style(sk, UDP)) {
7389 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7390 info.pr_value = asoc->default_timetolive;
7392 struct sctp_sock *sp = sctp_sk(sk);
7394 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7395 info.pr_value = sp->default_timetolive;
7398 if (put_user(len, optlen))
7401 if (copy_to_user(optval, &info, len))
7410 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7411 char __user *optval,
7414 struct sctp_prstatus params;
7415 struct sctp_association *asoc;
7417 int retval = -EINVAL;
7419 if (len < sizeof(params))
7422 len = sizeof(params);
7423 if (copy_from_user(¶ms, optval, len)) {
7428 policy = params.sprstat_policy;
7429 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7430 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7433 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7437 if (policy == SCTP_PR_SCTP_ALL) {
7438 params.sprstat_abandoned_unsent = 0;
7439 params.sprstat_abandoned_sent = 0;
7440 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7441 params.sprstat_abandoned_unsent +=
7442 asoc->abandoned_unsent[policy];
7443 params.sprstat_abandoned_sent +=
7444 asoc->abandoned_sent[policy];
7447 params.sprstat_abandoned_unsent =
7448 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7449 params.sprstat_abandoned_sent =
7450 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7453 if (put_user(len, optlen)) {
7458 if (copy_to_user(optval, ¶ms, len)) {
7469 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7470 char __user *optval,
7473 struct sctp_stream_out_ext *streamoute;
7474 struct sctp_association *asoc;
7475 struct sctp_prstatus params;
7476 int retval = -EINVAL;
7479 if (len < sizeof(params))
7482 len = sizeof(params);
7483 if (copy_from_user(¶ms, optval, len)) {
7488 policy = params.sprstat_policy;
7489 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7490 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7493 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7494 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7497 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7499 /* Not allocated yet, means all stats are 0 */
7500 params.sprstat_abandoned_unsent = 0;
7501 params.sprstat_abandoned_sent = 0;
7506 if (policy == SCTP_PR_SCTP_ALL) {
7507 params.sprstat_abandoned_unsent = 0;
7508 params.sprstat_abandoned_sent = 0;
7509 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7510 params.sprstat_abandoned_unsent +=
7511 streamoute->abandoned_unsent[policy];
7512 params.sprstat_abandoned_sent +=
7513 streamoute->abandoned_sent[policy];
7516 params.sprstat_abandoned_unsent =
7517 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7518 params.sprstat_abandoned_sent =
7519 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7522 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7533 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7534 char __user *optval,
7537 struct sctp_assoc_value params;
7538 struct sctp_association *asoc;
7539 int retval = -EFAULT;
7541 if (len < sizeof(params)) {
7546 len = sizeof(params);
7547 if (copy_from_user(¶ms, optval, len))
7550 asoc = sctp_id2assoc(sk, params.assoc_id);
7551 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7552 sctp_style(sk, UDP)) {
7557 params.assoc_value = asoc ? asoc->reconf_enable
7558 : sctp_sk(sk)->ep->reconf_enable;
7560 if (put_user(len, optlen))
7563 if (copy_to_user(optval, ¶ms, len))
7572 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7573 char __user *optval,
7576 struct sctp_assoc_value params;
7577 struct sctp_association *asoc;
7578 int retval = -EFAULT;
7580 if (len < sizeof(params)) {
7585 len = sizeof(params);
7586 if (copy_from_user(¶ms, optval, len))
7589 asoc = sctp_id2assoc(sk, params.assoc_id);
7590 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7591 sctp_style(sk, UDP)) {
7596 params.assoc_value = asoc ? asoc->strreset_enable
7597 : sctp_sk(sk)->ep->strreset_enable;
7599 if (put_user(len, optlen))
7602 if (copy_to_user(optval, ¶ms, len))
7611 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7612 char __user *optval,
7615 struct sctp_assoc_value params;
7616 struct sctp_association *asoc;
7617 int retval = -EFAULT;
7619 if (len < sizeof(params)) {
7624 len = sizeof(params);
7625 if (copy_from_user(¶ms, optval, len))
7628 asoc = sctp_id2assoc(sk, params.assoc_id);
7629 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7630 sctp_style(sk, UDP)) {
7635 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7636 : sctp_sk(sk)->default_ss;
7638 if (put_user(len, optlen))
7641 if (copy_to_user(optval, ¶ms, len))
7650 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7651 char __user *optval,
7654 struct sctp_stream_value params;
7655 struct sctp_association *asoc;
7656 int retval = -EFAULT;
7658 if (len < sizeof(params)) {
7663 len = sizeof(params);
7664 if (copy_from_user(¶ms, optval, len))
7667 asoc = sctp_id2assoc(sk, params.assoc_id);
7673 retval = sctp_sched_get_value(asoc, params.stream_id,
7674 ¶ms.stream_value);
7678 if (put_user(len, optlen)) {
7683 if (copy_to_user(optval, ¶ms, len)) {
7692 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7693 char __user *optval,
7696 struct sctp_assoc_value params;
7697 struct sctp_association *asoc;
7698 int retval = -EFAULT;
7700 if (len < sizeof(params)) {
7705 len = sizeof(params);
7706 if (copy_from_user(¶ms, optval, len))
7709 asoc = sctp_id2assoc(sk, params.assoc_id);
7710 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7711 sctp_style(sk, UDP)) {
7716 params.assoc_value = asoc ? asoc->intl_enable
7717 : sctp_sk(sk)->strm_interleave;
7719 if (put_user(len, optlen))
7722 if (copy_to_user(optval, ¶ms, len))
7731 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7732 char __user *optval,
7737 if (len < sizeof(int))
7741 val = sctp_sk(sk)->reuse;
7742 if (put_user(len, optlen))
7745 if (copy_to_user(optval, &val, len))
7751 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7754 struct sctp_association *asoc;
7755 struct sctp_event param;
7758 if (len < sizeof(param))
7761 len = sizeof(param);
7762 if (copy_from_user(¶m, optval, len))
7765 if (param.se_type < SCTP_SN_TYPE_BASE ||
7766 param.se_type > SCTP_SN_TYPE_MAX)
7769 asoc = sctp_id2assoc(sk, param.se_assoc_id);
7770 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7771 sctp_style(sk, UDP))
7774 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7775 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7777 if (put_user(len, optlen))
7780 if (copy_to_user(optval, ¶m, len))
7786 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7787 char __user *optval, int __user *optlen)
7792 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7794 /* I can hardly begin to describe how wrong this is. This is
7795 * so broken as to be worse than useless. The API draft
7796 * REALLY is NOT helpful here... I am not convinced that the
7797 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7798 * are at all well-founded.
7800 if (level != SOL_SCTP) {
7801 struct sctp_af *af = sctp_sk(sk)->pf->af;
7803 retval = af->getsockopt(sk, level, optname, optval, optlen);
7807 if (get_user(len, optlen))
7817 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7819 case SCTP_DISABLE_FRAGMENTS:
7820 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7824 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7826 case SCTP_AUTOCLOSE:
7827 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7829 case SCTP_SOCKOPT_PEELOFF:
7830 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7832 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7833 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7835 case SCTP_PEER_ADDR_PARAMS:
7836 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7839 case SCTP_DELAYED_SACK:
7840 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7844 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7846 case SCTP_GET_PEER_ADDRS:
7847 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7850 case SCTP_GET_LOCAL_ADDRS:
7851 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7854 case SCTP_SOCKOPT_CONNECTX3:
7855 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7857 case SCTP_DEFAULT_SEND_PARAM:
7858 retval = sctp_getsockopt_default_send_param(sk, len,
7861 case SCTP_DEFAULT_SNDINFO:
7862 retval = sctp_getsockopt_default_sndinfo(sk, len,
7865 case SCTP_PRIMARY_ADDR:
7866 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7869 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7872 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7874 case SCTP_ASSOCINFO:
7875 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7877 case SCTP_I_WANT_MAPPED_V4_ADDR:
7878 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7881 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7883 case SCTP_GET_PEER_ADDR_INFO:
7884 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7887 case SCTP_ADAPTATION_LAYER:
7888 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7892 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7894 case SCTP_FRAGMENT_INTERLEAVE:
7895 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7898 case SCTP_PARTIAL_DELIVERY_POINT:
7899 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7902 case SCTP_MAX_BURST:
7903 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7906 case SCTP_AUTH_CHUNK:
7907 case SCTP_AUTH_DELETE_KEY:
7908 case SCTP_AUTH_DEACTIVATE_KEY:
7909 retval = -EOPNOTSUPP;
7911 case SCTP_HMAC_IDENT:
7912 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7914 case SCTP_AUTH_ACTIVE_KEY:
7915 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7917 case SCTP_PEER_AUTH_CHUNKS:
7918 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7921 case SCTP_LOCAL_AUTH_CHUNKS:
7922 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7925 case SCTP_GET_ASSOC_NUMBER:
7926 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7928 case SCTP_GET_ASSOC_ID_LIST:
7929 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7931 case SCTP_AUTO_ASCONF:
7932 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7934 case SCTP_PEER_ADDR_THLDS:
7935 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7937 case SCTP_GET_ASSOC_STATS:
7938 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7940 case SCTP_RECVRCVINFO:
7941 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7943 case SCTP_RECVNXTINFO:
7944 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7946 case SCTP_PR_SUPPORTED:
7947 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7949 case SCTP_DEFAULT_PRINFO:
7950 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7953 case SCTP_PR_ASSOC_STATUS:
7954 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7957 case SCTP_PR_STREAM_STATUS:
7958 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7961 case SCTP_RECONFIG_SUPPORTED:
7962 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7965 case SCTP_ENABLE_STREAM_RESET:
7966 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7969 case SCTP_STREAM_SCHEDULER:
7970 retval = sctp_getsockopt_scheduler(sk, len, optval,
7973 case SCTP_STREAM_SCHEDULER_VALUE:
7974 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7977 case SCTP_INTERLEAVING_SUPPORTED:
7978 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7981 case SCTP_REUSE_PORT:
7982 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7985 retval = sctp_getsockopt_event(sk, len, optval, optlen);
7988 retval = -ENOPROTOOPT;
7996 static int sctp_hash(struct sock *sk)
8002 static void sctp_unhash(struct sock *sk)
8007 /* Check if port is acceptable. Possibly find first available port.
8009 * The port hash table (contained in the 'global' SCTP protocol storage
8010 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
8011 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
8012 * list (the list number is the port number hashed out, so as you
8013 * would expect from a hash function, all the ports in a given list have
8014 * such a number that hashes out to the same list number; you were
8015 * expecting that, right?); so each list has a set of ports, with a
8016 * link to the socket (struct sock) that uses it, the port number and
8017 * a fastreuse flag (FIXME: NPI ipg).
8019 static struct sctp_bind_bucket *sctp_bucket_create(
8020 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8022 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8024 struct sctp_sock *sp = sctp_sk(sk);
8025 bool reuse = (sk->sk_reuse || sp->reuse);
8026 struct sctp_bind_hashbucket *head; /* hash list */
8027 kuid_t uid = sock_i_uid(sk);
8028 struct sctp_bind_bucket *pp;
8029 unsigned short snum;
8032 snum = ntohs(addr->v4.sin_port);
8034 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8039 /* Search for an available port. */
8040 int low, high, remaining, index;
8042 struct net *net = sock_net(sk);
8044 inet_get_local_port_range(net, &low, &high);
8045 remaining = (high - low) + 1;
8046 rover = prandom_u32() % remaining + low;
8050 if ((rover < low) || (rover > high))
8052 if (inet_is_local_reserved_port(net, rover))
8054 index = sctp_phashfn(sock_net(sk), rover);
8055 head = &sctp_port_hashtable[index];
8056 spin_lock(&head->lock);
8057 sctp_for_each_hentry(pp, &head->chain)
8058 if ((pp->port == rover) &&
8059 net_eq(sock_net(sk), pp->net))
8063 spin_unlock(&head->lock);
8064 } while (--remaining > 0);
8066 /* Exhausted local port range during search? */
8071 /* OK, here is the one we will use. HEAD (the port
8072 * hash table list entry) is non-NULL and we hold it's
8077 /* We are given an specific port number; we verify
8078 * that it is not being used. If it is used, we will
8079 * exahust the search in the hash list corresponding
8080 * to the port number (snum) - we detect that with the
8081 * port iterator, pp being NULL.
8083 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
8084 spin_lock(&head->lock);
8085 sctp_for_each_hentry(pp, &head->chain) {
8086 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
8093 if (!hlist_empty(&pp->owner)) {
8094 /* We had a port hash table hit - there is an
8095 * available port (pp != NULL) and it is being
8096 * used by other socket (pp->owner not empty); that other
8097 * socket is going to be sk2.
8101 pr_debug("%s: found a possible match\n", __func__);
8103 if ((pp->fastreuse && reuse &&
8104 sk->sk_state != SCTP_SS_LISTENING) ||
8105 (pp->fastreuseport && sk->sk_reuseport &&
8106 uid_eq(pp->fastuid, uid)))
8109 /* Run through the list of sockets bound to the port
8110 * (pp->port) [via the pointers bind_next and
8111 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8112 * we get the endpoint they describe and run through
8113 * the endpoint's list of IP (v4 or v6) addresses,
8114 * comparing each of the addresses with the address of
8115 * the socket sk. If we find a match, then that means
8116 * that this port/socket (sk) combination are already
8119 sk_for_each_bound(sk2, &pp->owner) {
8120 struct sctp_sock *sp2 = sctp_sk(sk2);
8121 struct sctp_endpoint *ep2 = sp2->ep;
8124 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8125 sk2->sk_state != SCTP_SS_LISTENING) ||
8126 (sk->sk_reuseport && sk2->sk_reuseport &&
8127 uid_eq(uid, sock_i_uid(sk2))))
8130 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8137 pr_debug("%s: found a match\n", __func__);
8140 /* If there was a hash table miss, create a new port. */
8142 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
8145 /* In either case (hit or miss), make sure fastreuse is 1 only
8146 * if sk->sk_reuse is too (that is, if the caller requested
8147 * SO_REUSEADDR on this socket -sk-).
8149 if (hlist_empty(&pp->owner)) {
8150 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8155 if (sk->sk_reuseport) {
8156 pp->fastreuseport = 1;
8159 pp->fastreuseport = 0;
8162 if (pp->fastreuse &&
8163 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8166 if (pp->fastreuseport &&
8167 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8168 pp->fastreuseport = 0;
8171 /* We are set, so fill up all the data in the hash table
8172 * entry, tie the socket list information with the rest of the
8173 * sockets FIXME: Blurry, NPI (ipg).
8176 if (!sp->bind_hash) {
8177 inet_sk(sk)->inet_num = snum;
8178 sk_add_bind_node(sk, &pp->owner);
8184 spin_unlock(&head->lock);
8191 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8192 * port is requested.
8194 static int sctp_get_port(struct sock *sk, unsigned short snum)
8196 union sctp_addr addr;
8197 struct sctp_af *af = sctp_sk(sk)->pf->af;
8199 /* Set up a dummy address struct from the sk. */
8200 af->from_sk(&addr, sk);
8201 addr.v4.sin_port = htons(snum);
8203 /* Note: sk->sk_num gets filled in if ephemeral port request. */
8204 return !!sctp_get_port_local(sk, &addr);
8208 * Move a socket to LISTENING state.
8210 static int sctp_listen_start(struct sock *sk, int backlog)
8212 struct sctp_sock *sp = sctp_sk(sk);
8213 struct sctp_endpoint *ep = sp->ep;
8214 struct crypto_shash *tfm = NULL;
8217 /* Allocate HMAC for generating cookie. */
8218 if (!sp->hmac && sp->sctp_hmac_alg) {
8219 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8220 tfm = crypto_alloc_shash(alg, 0, 0);
8222 net_info_ratelimited("failed to load transform for %s: %ld\n",
8223 sp->sctp_hmac_alg, PTR_ERR(tfm));
8226 sctp_sk(sk)->hmac = tfm;
8230 * If a bind() or sctp_bindx() is not called prior to a listen()
8231 * call that allows new associations to be accepted, the system
8232 * picks an ephemeral port and will choose an address set equivalent
8233 * to binding with a wildcard address.
8235 * This is not currently spelled out in the SCTP sockets
8236 * extensions draft, but follows the practice as seen in TCP
8240 inet_sk_set_state(sk, SCTP_SS_LISTENING);
8241 if (!ep->base.bind_addr.port) {
8242 if (sctp_autobind(sk))
8245 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8246 inet_sk_set_state(sk, SCTP_SS_CLOSED);
8251 sk->sk_max_ack_backlog = backlog;
8252 return sctp_hash_endpoint(ep);
8256 * 4.1.3 / 5.1.3 listen()
8258 * By default, new associations are not accepted for UDP style sockets.
8259 * An application uses listen() to mark a socket as being able to
8260 * accept new associations.
8262 * On TCP style sockets, applications use listen() to ready the SCTP
8263 * endpoint for accepting inbound associations.
8265 * On both types of endpoints a backlog of '0' disables listening.
8267 * Move a socket to LISTENING state.
8269 int sctp_inet_listen(struct socket *sock, int backlog)
8271 struct sock *sk = sock->sk;
8272 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8275 if (unlikely(backlog < 0))
8280 /* Peeled-off sockets are not allowed to listen(). */
8281 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8284 if (sock->state != SS_UNCONNECTED)
8287 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8290 /* If backlog is zero, disable listening. */
8292 if (sctp_sstate(sk, CLOSED))
8296 sctp_unhash_endpoint(ep);
8297 sk->sk_state = SCTP_SS_CLOSED;
8298 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8299 sctp_sk(sk)->bind_hash->fastreuse = 1;
8303 /* If we are already listening, just update the backlog */
8304 if (sctp_sstate(sk, LISTENING))
8305 sk->sk_max_ack_backlog = backlog;
8307 err = sctp_listen_start(sk, backlog);
8319 * This function is done by modeling the current datagram_poll() and the
8320 * tcp_poll(). Note that, based on these implementations, we don't
8321 * lock the socket in this function, even though it seems that,
8322 * ideally, locking or some other mechanisms can be used to ensure
8323 * the integrity of the counters (sndbuf and wmem_alloc) used
8324 * in this place. We assume that we don't need locks either until proven
8327 * Another thing to note is that we include the Async I/O support
8328 * here, again, by modeling the current TCP/UDP code. We don't have
8329 * a good way to test with it yet.
8331 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8333 struct sock *sk = sock->sk;
8334 struct sctp_sock *sp = sctp_sk(sk);
8337 poll_wait(file, sk_sleep(sk), wait);
8339 sock_rps_record_flow(sk);
8341 /* A TCP-style listening socket becomes readable when the accept queue
8344 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8345 return (!list_empty(&sp->ep->asocs)) ?
8346 (EPOLLIN | EPOLLRDNORM) : 0;
8350 /* Is there any exceptional events? */
8351 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
8353 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8354 if (sk->sk_shutdown & RCV_SHUTDOWN)
8355 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8356 if (sk->sk_shutdown == SHUTDOWN_MASK)
8359 /* Is it readable? Reconsider this code with TCP-style support. */
8360 if (!skb_queue_empty(&sk->sk_receive_queue))
8361 mask |= EPOLLIN | EPOLLRDNORM;
8363 /* The association is either gone or not ready. */
8364 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8367 /* Is it writable? */
8368 if (sctp_writeable(sk)) {
8369 mask |= EPOLLOUT | EPOLLWRNORM;
8371 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8373 * Since the socket is not locked, the buffer
8374 * might be made available after the writeable check and
8375 * before the bit is set. This could cause a lost I/O
8376 * signal. tcp_poll() has a race breaker for this race
8377 * condition. Based on their implementation, we put
8378 * in the following code to cover it as well.
8380 if (sctp_writeable(sk))
8381 mask |= EPOLLOUT | EPOLLWRNORM;
8386 /********************************************************************
8387 * 2nd Level Abstractions
8388 ********************************************************************/
8390 static struct sctp_bind_bucket *sctp_bucket_create(
8391 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8393 struct sctp_bind_bucket *pp;
8395 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8397 SCTP_DBG_OBJCNT_INC(bind_bucket);
8400 INIT_HLIST_HEAD(&pp->owner);
8402 hlist_add_head(&pp->node, &head->chain);
8407 /* Caller must hold hashbucket lock for this tb with local BH disabled */
8408 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8410 if (pp && hlist_empty(&pp->owner)) {
8411 __hlist_del(&pp->node);
8412 kmem_cache_free(sctp_bucket_cachep, pp);
8413 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8417 /* Release this socket's reference to a local port. */
8418 static inline void __sctp_put_port(struct sock *sk)
8420 struct sctp_bind_hashbucket *head =
8421 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8422 inet_sk(sk)->inet_num)];
8423 struct sctp_bind_bucket *pp;
8425 spin_lock(&head->lock);
8426 pp = sctp_sk(sk)->bind_hash;
8427 __sk_del_bind_node(sk);
8428 sctp_sk(sk)->bind_hash = NULL;
8429 inet_sk(sk)->inet_num = 0;
8430 sctp_bucket_destroy(pp);
8431 spin_unlock(&head->lock);
8434 void sctp_put_port(struct sock *sk)
8437 __sctp_put_port(sk);
8442 * The system picks an ephemeral port and choose an address set equivalent
8443 * to binding with a wildcard address.
8444 * One of those addresses will be the primary address for the association.
8445 * This automatically enables the multihoming capability of SCTP.
8447 static int sctp_autobind(struct sock *sk)
8449 union sctp_addr autoaddr;
8453 /* Initialize a local sockaddr structure to INADDR_ANY. */
8454 af = sctp_sk(sk)->pf->af;
8456 port = htons(inet_sk(sk)->inet_num);
8457 af->inaddr_any(&autoaddr, port);
8459 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8462 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8465 * 4.2 The cmsghdr Structure *
8467 * When ancillary data is sent or received, any number of ancillary data
8468 * objects can be specified by the msg_control and msg_controllen members of
8469 * the msghdr structure, because each object is preceded by
8470 * a cmsghdr structure defining the object's length (the cmsg_len member).
8471 * Historically Berkeley-derived implementations have passed only one object
8472 * at a time, but this API allows multiple objects to be
8473 * passed in a single call to sendmsg() or recvmsg(). The following example
8474 * shows two ancillary data objects in a control buffer.
8476 * |<--------------------------- msg_controllen -------------------------->|
8479 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8481 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8484 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8486 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8489 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8490 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8492 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8494 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8501 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8503 struct msghdr *my_msg = (struct msghdr *)msg;
8504 struct cmsghdr *cmsg;
8506 for_each_cmsghdr(cmsg, my_msg) {
8507 if (!CMSG_OK(my_msg, cmsg))
8510 /* Should we parse this header or ignore? */
8511 if (cmsg->cmsg_level != IPPROTO_SCTP)
8514 /* Strictly check lengths following example in SCM code. */
8515 switch (cmsg->cmsg_type) {
8517 /* SCTP Socket API Extension
8518 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8520 * This cmsghdr structure provides information for
8521 * initializing new SCTP associations with sendmsg().
8522 * The SCTP_INITMSG socket option uses this same data
8523 * structure. This structure is not used for
8526 * cmsg_level cmsg_type cmsg_data[]
8527 * ------------ ------------ ----------------------
8528 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8530 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8533 cmsgs->init = CMSG_DATA(cmsg);
8537 /* SCTP Socket API Extension
8538 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8540 * This cmsghdr structure specifies SCTP options for
8541 * sendmsg() and describes SCTP header information
8542 * about a received message through recvmsg().
8544 * cmsg_level cmsg_type cmsg_data[]
8545 * ------------ ------------ ----------------------
8546 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8548 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8551 cmsgs->srinfo = CMSG_DATA(cmsg);
8553 if (cmsgs->srinfo->sinfo_flags &
8554 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8555 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8556 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8561 /* SCTP Socket API Extension
8562 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8564 * This cmsghdr structure specifies SCTP options for
8565 * sendmsg(). This structure and SCTP_RCVINFO replaces
8566 * SCTP_SNDRCV which has been deprecated.
8568 * cmsg_level cmsg_type cmsg_data[]
8569 * ------------ ------------ ---------------------
8570 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8572 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8575 cmsgs->sinfo = CMSG_DATA(cmsg);
8577 if (cmsgs->sinfo->snd_flags &
8578 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8579 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8580 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8584 /* SCTP Socket API Extension
8585 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8587 * This cmsghdr structure specifies SCTP options for sendmsg().
8589 * cmsg_level cmsg_type cmsg_data[]
8590 * ------------ ------------ ---------------------
8591 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8593 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8596 cmsgs->prinfo = CMSG_DATA(cmsg);
8597 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8600 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8601 cmsgs->prinfo->pr_value = 0;
8604 /* SCTP Socket API Extension
8605 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8607 * This cmsghdr structure specifies SCTP options for sendmsg().
8609 * cmsg_level cmsg_type cmsg_data[]
8610 * ------------ ------------ ---------------------
8611 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8613 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8616 cmsgs->authinfo = CMSG_DATA(cmsg);
8618 case SCTP_DSTADDRV4:
8619 case SCTP_DSTADDRV6:
8620 /* SCTP Socket API Extension
8621 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8623 * This cmsghdr structure specifies SCTP options for sendmsg().
8625 * cmsg_level cmsg_type cmsg_data[]
8626 * ------------ ------------ ---------------------
8627 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8628 * ------------ ------------ ---------------------
8629 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8631 cmsgs->addrs_msg = my_msg;
8642 * Wait for a packet..
8643 * Note: This function is the same function as in core/datagram.c
8644 * with a few modifications to make lksctp work.
8646 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8651 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8653 /* Socket errors? */
8654 error = sock_error(sk);
8658 if (!skb_queue_empty(&sk->sk_receive_queue))
8661 /* Socket shut down? */
8662 if (sk->sk_shutdown & RCV_SHUTDOWN)
8665 /* Sequenced packets can come disconnected. If so we report the
8670 /* Is there a good reason to think that we may receive some data? */
8671 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8674 /* Handle signals. */
8675 if (signal_pending(current))
8678 /* Let another process have a go. Since we are going to sleep
8679 * anyway. Note: This may cause odd behaviors if the message
8680 * does not fit in the user's buffer, but this seems to be the
8681 * only way to honor MSG_DONTWAIT realistically.
8684 *timeo_p = schedule_timeout(*timeo_p);
8688 finish_wait(sk_sleep(sk), &wait);
8692 error = sock_intr_errno(*timeo_p);
8695 finish_wait(sk_sleep(sk), &wait);
8700 /* Receive a datagram.
8701 * Note: This is pretty much the same routine as in core/datagram.c
8702 * with a few changes to make lksctp work.
8704 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8705 int noblock, int *err)
8708 struct sk_buff *skb;
8711 timeo = sock_rcvtimeo(sk, noblock);
8713 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8714 MAX_SCHEDULE_TIMEOUT);
8717 /* Again only user level code calls this function,
8718 * so nothing interrupt level
8719 * will suddenly eat the receive_queue.
8721 * Look at current nfs client by the way...
8722 * However, this function was correct in any case. 8)
8724 if (flags & MSG_PEEK) {
8725 skb = skb_peek(&sk->sk_receive_queue);
8727 refcount_inc(&skb->users);
8729 skb = __skb_dequeue(&sk->sk_receive_queue);
8735 /* Caller is allowed not to check sk->sk_err before calling. */
8736 error = sock_error(sk);
8740 if (sk->sk_shutdown & RCV_SHUTDOWN)
8743 if (sk_can_busy_loop(sk)) {
8744 sk_busy_loop(sk, noblock);
8746 if (!skb_queue_empty(&sk->sk_receive_queue))
8750 /* User doesn't want to wait. */
8754 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8763 /* If sndbuf has changed, wake up per association sndbuf waiters. */
8764 static void __sctp_write_space(struct sctp_association *asoc)
8766 struct sock *sk = asoc->base.sk;
8768 if (sctp_wspace(asoc) <= 0)
8771 if (waitqueue_active(&asoc->wait))
8772 wake_up_interruptible(&asoc->wait);
8774 if (sctp_writeable(sk)) {
8775 struct socket_wq *wq;
8778 wq = rcu_dereference(sk->sk_wq);
8780 if (waitqueue_active(&wq->wait))
8781 wake_up_interruptible(&wq->wait);
8783 /* Note that we try to include the Async I/O support
8784 * here by modeling from the current TCP/UDP code.
8785 * We have not tested with it yet.
8787 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8788 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8794 static void sctp_wake_up_waiters(struct sock *sk,
8795 struct sctp_association *asoc)
8797 struct sctp_association *tmp = asoc;
8799 /* We do accounting for the sndbuf space per association,
8800 * so we only need to wake our own association.
8802 if (asoc->ep->sndbuf_policy)
8803 return __sctp_write_space(asoc);
8805 /* If association goes down and is just flushing its
8806 * outq, then just normally notify others.
8808 if (asoc->base.dead)
8809 return sctp_write_space(sk);
8811 /* Accounting for the sndbuf space is per socket, so we
8812 * need to wake up others, try to be fair and in case of
8813 * other associations, let them have a go first instead
8814 * of just doing a sctp_write_space() call.
8816 * Note that we reach sctp_wake_up_waiters() only when
8817 * associations free up queued chunks, thus we are under
8818 * lock and the list of associations on a socket is
8819 * guaranteed not to change.
8821 for (tmp = list_next_entry(tmp, asocs); 1;
8822 tmp = list_next_entry(tmp, asocs)) {
8823 /* Manually skip the head element. */
8824 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8826 /* Wake up association. */
8827 __sctp_write_space(tmp);
8828 /* We've reached the end. */
8834 /* Do accounting for the sndbuf space.
8835 * Decrement the used sndbuf space of the corresponding association by the
8836 * data size which was just transmitted(freed).
8838 static void sctp_wfree(struct sk_buff *skb)
8840 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8841 struct sctp_association *asoc = chunk->asoc;
8842 struct sock *sk = asoc->base.sk;
8844 sk_mem_uncharge(sk, skb->truesize);
8845 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8846 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8847 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8848 &sk->sk_wmem_alloc));
8851 struct sctp_shared_key *shkey = chunk->shkey;
8853 /* refcnt == 2 and !list_empty mean after this release, it's
8854 * not being used anywhere, and it's time to notify userland
8855 * that this shkey can be freed if it's been deactivated.
8857 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8858 refcount_read(&shkey->refcnt) == 2) {
8859 struct sctp_ulpevent *ev;
8861 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8865 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8867 sctp_auth_shkey_release(chunk->shkey);
8871 sctp_wake_up_waiters(sk, asoc);
8873 sctp_association_put(asoc);
8876 /* Do accounting for the receive space on the socket.
8877 * Accounting for the association is done in ulpevent.c
8878 * We set this as a destructor for the cloned data skbs so that
8879 * accounting is done at the correct time.
8881 void sctp_sock_rfree(struct sk_buff *skb)
8883 struct sock *sk = skb->sk;
8884 struct sctp_ulpevent *event = sctp_skb2event(skb);
8886 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8889 * Mimic the behavior of sock_rfree
8891 sk_mem_uncharge(sk, event->rmem_len);
8895 /* Helper function to wait for space in the sndbuf. */
8896 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8899 struct sock *sk = asoc->base.sk;
8900 long current_timeo = *timeo_p;
8904 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8907 /* Increment the association's refcnt. */
8908 sctp_association_hold(asoc);
8910 /* Wait on the association specific sndbuf space. */
8912 prepare_to_wait_exclusive(&asoc->wait, &wait,
8913 TASK_INTERRUPTIBLE);
8914 if (asoc->base.dead)
8918 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8920 if (signal_pending(current))
8921 goto do_interrupted;
8922 if (sk_under_memory_pressure(sk))
8924 if ((int)msg_len <= sctp_wspace(asoc) &&
8925 sk_wmem_schedule(sk, msg_len))
8928 /* Let another process have a go. Since we are going
8932 current_timeo = schedule_timeout(current_timeo);
8934 if (sk != asoc->base.sk)
8937 *timeo_p = current_timeo;
8941 finish_wait(&asoc->wait, &wait);
8943 /* Release the association's refcnt. */
8944 sctp_association_put(asoc);
8957 err = sock_intr_errno(*timeo_p);
8965 void sctp_data_ready(struct sock *sk)
8967 struct socket_wq *wq;
8970 wq = rcu_dereference(sk->sk_wq);
8971 if (skwq_has_sleeper(wq))
8972 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8973 EPOLLRDNORM | EPOLLRDBAND);
8974 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8978 /* If socket sndbuf has changed, wake up all per association waiters. */
8979 void sctp_write_space(struct sock *sk)
8981 struct sctp_association *asoc;
8983 /* Wake up the tasks in each wait queue. */
8984 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8985 __sctp_write_space(asoc);
8989 /* Is there any sndbuf space available on the socket?
8991 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8992 * associations on the same socket. For a UDP-style socket with
8993 * multiple associations, it is possible for it to be "unwriteable"
8994 * prematurely. I assume that this is acceptable because
8995 * a premature "unwriteable" is better than an accidental "writeable" which
8996 * would cause an unwanted block under certain circumstances. For the 1-1
8997 * UDP-style sockets or TCP-style sockets, this code should work.
9000 static bool sctp_writeable(struct sock *sk)
9002 return sk->sk_sndbuf > sk->sk_wmem_queued;
9005 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
9006 * returns immediately with EINPROGRESS.
9008 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9010 struct sock *sk = asoc->base.sk;
9012 long current_timeo = *timeo_p;
9015 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9017 /* Increment the association's refcnt. */
9018 sctp_association_hold(asoc);
9021 prepare_to_wait_exclusive(&asoc->wait, &wait,
9022 TASK_INTERRUPTIBLE);
9025 if (sk->sk_shutdown & RCV_SHUTDOWN)
9027 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9030 if (signal_pending(current))
9031 goto do_interrupted;
9033 if (sctp_state(asoc, ESTABLISHED))
9036 /* Let another process have a go. Since we are going
9040 current_timeo = schedule_timeout(current_timeo);
9043 *timeo_p = current_timeo;
9047 finish_wait(&asoc->wait, &wait);
9049 /* Release the association's refcnt. */
9050 sctp_association_put(asoc);
9055 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9058 err = -ECONNREFUSED;
9062 err = sock_intr_errno(*timeo_p);
9070 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9072 struct sctp_endpoint *ep;
9076 ep = sctp_sk(sk)->ep;
9080 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9081 TASK_INTERRUPTIBLE);
9083 if (list_empty(&ep->asocs)) {
9085 timeo = schedule_timeout(timeo);
9090 if (!sctp_sstate(sk, LISTENING))
9094 if (!list_empty(&ep->asocs))
9097 err = sock_intr_errno(timeo);
9098 if (signal_pending(current))
9106 finish_wait(sk_sleep(sk), &wait);
9111 static void sctp_wait_for_close(struct sock *sk, long timeout)
9116 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9117 if (list_empty(&sctp_sk(sk)->ep->asocs))
9120 timeout = schedule_timeout(timeout);
9122 } while (!signal_pending(current) && timeout);
9124 finish_wait(sk_sleep(sk), &wait);
9127 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9129 struct sk_buff *frag;
9134 /* Don't forget the fragments. */
9135 skb_walk_frags(skb, frag)
9136 sctp_skb_set_owner_r_frag(frag, sk);
9139 sctp_skb_set_owner_r(skb, sk);
9142 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9143 struct sctp_association *asoc)
9145 struct inet_sock *inet = inet_sk(sk);
9146 struct inet_sock *newinet;
9147 struct sctp_sock *sp = sctp_sk(sk);
9148 struct sctp_endpoint *ep = sp->ep;
9150 newsk->sk_type = sk->sk_type;
9151 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9152 newsk->sk_flags = sk->sk_flags;
9153 newsk->sk_tsflags = sk->sk_tsflags;
9154 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9155 newsk->sk_no_check_rx = sk->sk_no_check_rx;
9156 newsk->sk_reuse = sk->sk_reuse;
9157 sctp_sk(newsk)->reuse = sp->reuse;
9159 newsk->sk_shutdown = sk->sk_shutdown;
9160 newsk->sk_destruct = sctp_destruct_sock;
9161 newsk->sk_family = sk->sk_family;
9162 newsk->sk_protocol = IPPROTO_SCTP;
9163 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9164 newsk->sk_sndbuf = sk->sk_sndbuf;
9165 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9166 newsk->sk_lingertime = sk->sk_lingertime;
9167 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9168 newsk->sk_sndtimeo = sk->sk_sndtimeo;
9169 newsk->sk_rxhash = sk->sk_rxhash;
9171 newinet = inet_sk(newsk);
9173 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9174 * getsockname() and getpeername()
9176 newinet->inet_sport = inet->inet_sport;
9177 newinet->inet_saddr = inet->inet_saddr;
9178 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9179 newinet->inet_dport = htons(asoc->peer.port);
9180 newinet->pmtudisc = inet->pmtudisc;
9181 newinet->inet_id = asoc->next_tsn ^ jiffies;
9183 newinet->uc_ttl = inet->uc_ttl;
9184 newinet->mc_loop = 1;
9185 newinet->mc_ttl = 1;
9186 newinet->mc_index = 0;
9187 newinet->mc_list = NULL;
9189 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9190 net_enable_timestamp();
9192 /* Set newsk security attributes from orginal sk and connection
9193 * security attribute from ep.
9195 security_sctp_sk_clone(ep, sk, newsk);
9198 static inline void sctp_copy_descendant(struct sock *sk_to,
9199 const struct sock *sk_from)
9201 int ancestor_size = sizeof(struct inet_sock) +
9202 sizeof(struct sctp_sock) -
9203 offsetof(struct sctp_sock, pd_lobby);
9205 if (sk_from->sk_family == PF_INET6)
9206 ancestor_size += sizeof(struct ipv6_pinfo);
9208 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9211 /* Populate the fields of the newsk from the oldsk and migrate the assoc
9212 * and its messages to the newsk.
9214 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9215 struct sctp_association *assoc,
9216 enum sctp_socket_type type)
9218 struct sctp_sock *oldsp = sctp_sk(oldsk);
9219 struct sctp_sock *newsp = sctp_sk(newsk);
9220 struct sctp_bind_bucket *pp; /* hash list port iterator */
9221 struct sctp_endpoint *newep = newsp->ep;
9222 struct sk_buff *skb, *tmp;
9223 struct sctp_ulpevent *event;
9224 struct sctp_bind_hashbucket *head;
9227 /* Migrate socket buffer sizes and all the socket level options to the
9230 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9231 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9232 /* Brute force copy old sctp opt. */
9233 sctp_copy_descendant(newsk, oldsk);
9235 /* Restore the ep value that was overwritten with the above structure
9241 /* Hook this new socket in to the bind_hash list. */
9242 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9243 inet_sk(oldsk)->inet_num)];
9244 spin_lock_bh(&head->lock);
9245 pp = sctp_sk(oldsk)->bind_hash;
9246 sk_add_bind_node(newsk, &pp->owner);
9247 sctp_sk(newsk)->bind_hash = pp;
9248 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9249 spin_unlock_bh(&head->lock);
9251 /* Copy the bind_addr list from the original endpoint to the new
9252 * endpoint so that we can handle restarts properly
9254 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9255 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9259 /* New ep's auth_hmacs should be set if old ep's is set, in case
9260 * that net->sctp.auth_enable has been changed to 0 by users and
9261 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9263 if (oldsp->ep->auth_hmacs) {
9264 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9269 /* Move any messages in the old socket's receive queue that are for the
9270 * peeled off association to the new socket's receive queue.
9272 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9273 event = sctp_skb2event(skb);
9274 if (event->asoc == assoc) {
9275 __skb_unlink(skb, &oldsk->sk_receive_queue);
9276 __skb_queue_tail(&newsk->sk_receive_queue, skb);
9277 sctp_skb_set_owner_r_frag(skb, newsk);
9281 /* Clean up any messages pending delivery due to partial
9282 * delivery. Three cases:
9283 * 1) No partial deliver; no work.
9284 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9285 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9287 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9289 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9290 struct sk_buff_head *queue;
9292 /* Decide which queue to move pd_lobby skbs to. */
9293 if (assoc->ulpq.pd_mode) {
9294 queue = &newsp->pd_lobby;
9296 queue = &newsk->sk_receive_queue;
9298 /* Walk through the pd_lobby, looking for skbs that
9299 * need moved to the new socket.
9301 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9302 event = sctp_skb2event(skb);
9303 if (event->asoc == assoc) {
9304 __skb_unlink(skb, &oldsp->pd_lobby);
9305 __skb_queue_tail(queue, skb);
9306 sctp_skb_set_owner_r_frag(skb, newsk);
9310 /* Clear up any skbs waiting for the partial
9311 * delivery to finish.
9313 if (assoc->ulpq.pd_mode)
9314 sctp_clear_pd(oldsk, NULL);
9318 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9320 /* Set the type of socket to indicate that it is peeled off from the
9321 * original UDP-style socket or created with the accept() call on a
9322 * TCP-style socket..
9326 /* Mark the new socket "in-use" by the user so that any packets
9327 * that may arrive on the association after we've moved it are
9328 * queued to the backlog. This prevents a potential race between
9329 * backlog processing on the old socket and new-packet processing
9330 * on the new socket.
9332 * The caller has just allocated newsk so we can guarantee that other
9333 * paths won't try to lock it and then oldsk.
9335 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9336 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
9337 sctp_assoc_migrate(assoc, newsk);
9338 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
9340 /* If the association on the newsk is already closed before accept()
9341 * is called, set RCV_SHUTDOWN flag.
9343 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9344 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9345 newsk->sk_shutdown |= RCV_SHUTDOWN;
9347 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9350 release_sock(newsk);
9356 /* This proto struct describes the ULP interface for SCTP. */
9357 struct proto sctp_prot = {
9359 .owner = THIS_MODULE,
9360 .close = sctp_close,
9361 .disconnect = sctp_disconnect,
9362 .accept = sctp_accept,
9363 .ioctl = sctp_ioctl,
9364 .init = sctp_init_sock,
9365 .destroy = sctp_destroy_sock,
9366 .shutdown = sctp_shutdown,
9367 .setsockopt = sctp_setsockopt,
9368 .getsockopt = sctp_getsockopt,
9369 .sendmsg = sctp_sendmsg,
9370 .recvmsg = sctp_recvmsg,
9372 .backlog_rcv = sctp_backlog_rcv,
9374 .unhash = sctp_unhash,
9375 .get_port = sctp_get_port,
9376 .obj_size = sizeof(struct sctp_sock),
9377 .useroffset = offsetof(struct sctp_sock, subscribe),
9378 .usersize = offsetof(struct sctp_sock, initmsg) -
9379 offsetof(struct sctp_sock, subscribe) +
9380 sizeof_field(struct sctp_sock, initmsg),
9381 .sysctl_mem = sysctl_sctp_mem,
9382 .sysctl_rmem = sysctl_sctp_rmem,
9383 .sysctl_wmem = sysctl_sctp_wmem,
9384 .memory_pressure = &sctp_memory_pressure,
9385 .enter_memory_pressure = sctp_enter_memory_pressure,
9386 .memory_allocated = &sctp_memory_allocated,
9387 .sockets_allocated = &sctp_sockets_allocated,
9390 #if IS_ENABLED(CONFIG_IPV6)
9392 #include <net/transp_v6.h>
9393 static void sctp_v6_destroy_sock(struct sock *sk)
9395 sctp_destroy_sock(sk);
9396 inet6_destroy_sock(sk);
9399 struct proto sctpv6_prot = {
9401 .owner = THIS_MODULE,
9402 .close = sctp_close,
9403 .disconnect = sctp_disconnect,
9404 .accept = sctp_accept,
9405 .ioctl = sctp_ioctl,
9406 .init = sctp_init_sock,
9407 .destroy = sctp_v6_destroy_sock,
9408 .shutdown = sctp_shutdown,
9409 .setsockopt = sctp_setsockopt,
9410 .getsockopt = sctp_getsockopt,
9411 .sendmsg = sctp_sendmsg,
9412 .recvmsg = sctp_recvmsg,
9414 .backlog_rcv = sctp_backlog_rcv,
9416 .unhash = sctp_unhash,
9417 .get_port = sctp_get_port,
9418 .obj_size = sizeof(struct sctp6_sock),
9419 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9420 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9421 offsetof(struct sctp6_sock, sctp.subscribe) +
9422 sizeof_field(struct sctp6_sock, sctp.initmsg),
9423 .sysctl_mem = sysctl_sctp_mem,
9424 .sysctl_rmem = sysctl_sctp_rmem,
9425 .sysctl_wmem = sysctl_sctp_wmem,
9426 .memory_pressure = &sctp_memory_pressure,
9427 .enter_memory_pressure = sctp_enter_memory_pressure,
9428 .memory_allocated = &sctp_memory_allocated,
9429 .sockets_allocated = &sctp_sockets_allocated,
9431 #endif /* IS_ENABLED(CONFIG_IPV6) */