1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * This SCTP implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * This SCTP implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
34 * Please send any bug reports or fixes you make to the
36 * lksctp developers <linux-sctp@vger.kernel.org>
38 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 #include <crypto/hash.h>
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/wait.h>
59 #include <linux/time.h>
60 #include <linux/sched/signal.h>
62 #include <linux/capability.h>
63 #include <linux/fcntl.h>
64 #include <linux/poll.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/file.h>
68 #include <linux/compat.h>
72 #include <net/route.h>
74 #include <net/inet_common.h>
75 #include <net/busy_poll.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <linux/export.h>
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
82 #include <net/sctp/stream_sched.h>
84 /* Forward declarations for internal helper functions. */
85 static int sctp_writeable(struct sock *sk);
86 static void sctp_wfree(struct sk_buff *skb);
87 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
89 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
90 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91 static int sctp_wait_for_accept(struct sock *sk, long timeo);
92 static void sctp_wait_for_close(struct sock *sk, long timeo);
93 static void sctp_destruct_sock(struct sock *sk);
94 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103 static int sctp_autobind(struct sock *sk);
104 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
108 static unsigned long sctp_memory_pressure;
109 static atomic_long_t sctp_memory_allocated;
110 struct percpu_counter sctp_sockets_allocated;
112 static void sctp_enter_memory_pressure(struct sock *sk)
114 sctp_memory_pressure = 1;
118 /* Get the sndbuf space available at the time on the association. */
119 static inline int sctp_wspace(struct sctp_association *asoc)
123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
126 amt = sk_wmem_alloc_get(asoc->base.sk);
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
132 amt = sk_stream_wspace(asoc->base.sk);
137 amt = asoc->base.sk->sk_sndbuf - amt;
142 /* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
151 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
160 sctp_auth_shkey_hold(chunk->shkey);
162 skb_set_owner_w(chunk->skb, sk);
164 chunk->skb->destructor = sctp_wfree;
165 /* Save the chunk pointer in skb for sctp_wfree to use later. */
166 skb_shinfo(chunk->skb)->destructor_arg = chunk;
168 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
169 sizeof(struct sk_buff) +
170 sizeof(struct sctp_chunk);
172 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
173 sk->sk_wmem_queued += chunk->skb->truesize;
174 sk_mem_charge(sk, chunk->skb->truesize);
177 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
179 skb_orphan(chunk->skb);
182 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
183 void (*cb)(struct sctp_chunk *))
186 struct sctp_outq *q = &asoc->outqueue;
187 struct sctp_transport *t;
188 struct sctp_chunk *chunk;
190 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
191 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
194 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
197 list_for_each_entry(chunk, &q->sacked, transmitted_list)
200 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
203 list_for_each_entry(chunk, &q->out_chunk_list, list)
207 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
208 void (*cb)(struct sk_buff *, struct sock *))
211 struct sk_buff *skb, *tmp;
213 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
219 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
223 /* Verify that this is a valid address. */
224 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
229 /* Verify basic sockaddr. */
230 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
234 /* Is this a valid SCTP address? */
235 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
238 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
244 /* Look up the association by its id. If this is not a UDP-style
245 * socket, the ID field is always ignored.
247 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
249 struct sctp_association *asoc = NULL;
251 /* If this is not a UDP-style socket, assoc id should be ignored. */
252 if (!sctp_style(sk, UDP)) {
253 /* Return NULL if the socket state is not ESTABLISHED. It
254 * could be a TCP-style listening socket or a socket which
255 * hasn't yet called connect() to establish an association.
257 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
260 /* Get the first and the only association from the list. */
261 if (!list_empty(&sctp_sk(sk)->ep->asocs))
262 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
263 struct sctp_association, asocs);
267 /* Otherwise this is a UDP-style socket. */
268 if (!id || (id == (sctp_assoc_t)-1))
271 spin_lock_bh(&sctp_assocs_id_lock);
272 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
273 spin_unlock_bh(&sctp_assocs_id_lock);
275 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
281 /* Look up the transport from an address and an assoc id. If both address and
282 * id are specified, the associations matching the address and the id should be
285 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
286 struct sockaddr_storage *addr,
289 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
290 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
291 union sctp_addr *laddr = (union sctp_addr *)addr;
292 struct sctp_transport *transport;
294 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
297 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
304 id_asoc = sctp_id2assoc(sk, id);
305 if (id_asoc && (id_asoc != addr_asoc))
308 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
309 (union sctp_addr *)addr);
314 /* API 3.1.2 bind() - UDP Style Syntax
315 * The syntax of bind() is,
317 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
319 * sd - the socket descriptor returned by socket().
320 * addr - the address structure (struct sockaddr_in or struct
321 * sockaddr_in6 [RFC 2553]),
322 * addr_len - the size of the address structure.
324 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
330 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
333 /* Disallow binding twice. */
334 if (!sctp_sk(sk)->ep->base.bind_addr.port)
335 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
345 static long sctp_get_port_local(struct sock *, union sctp_addr *);
347 /* Verify this is a valid sockaddr. */
348 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
349 union sctp_addr *addr, int len)
353 /* Check minimum size. */
354 if (len < sizeof (struct sockaddr))
357 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
360 /* V4 mapped address are really of AF_INET family */
361 if (addr->sa.sa_family == AF_INET6 &&
362 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
363 !opt->pf->af_supported(AF_INET, opt))
366 /* If we get this far, af is valid. */
367 af = sctp_get_af_specific(addr->sa.sa_family);
369 if (len < af->sockaddr_len)
375 /* Bind a local address either to an endpoint or to an association. */
376 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
378 struct net *net = sock_net(sk);
379 struct sctp_sock *sp = sctp_sk(sk);
380 struct sctp_endpoint *ep = sp->ep;
381 struct sctp_bind_addr *bp = &ep->base.bind_addr;
386 /* Common sockaddr verification. */
387 af = sctp_sockaddr_af(sp, addr, len);
389 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
390 __func__, sk, addr, len);
394 snum = ntohs(addr->v4.sin_port);
396 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
397 __func__, sk, &addr->sa, bp->port, snum, len);
399 /* PF specific bind() address verification. */
400 if (!sp->pf->bind_verify(sp, addr))
401 return -EADDRNOTAVAIL;
403 /* We must either be unbound, or bind to the same port.
404 * It's OK to allow 0 ports if we are already bound.
405 * We'll just inhert an already bound port in this case
410 else if (snum != bp->port) {
411 pr_debug("%s: new port %d doesn't match existing port "
412 "%d\n", __func__, snum, bp->port);
417 if (snum && snum < inet_prot_sock(net) &&
418 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
421 /* See if the address matches any of the addresses we may have
422 * already bound before checking against other endpoints.
424 if (sctp_bind_addr_match(bp, addr, sp))
427 /* Make sure we are allowed to bind here.
428 * The function sctp_get_port_local() does duplicate address
431 addr->v4.sin_port = htons(snum);
432 if ((ret = sctp_get_port_local(sk, addr))) {
436 /* Refresh ephemeral port. */
438 bp->port = inet_sk(sk)->inet_num;
440 /* Add the address to the bind address list.
441 * Use GFP_ATOMIC since BHs will be disabled.
443 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
444 SCTP_ADDR_SRC, GFP_ATOMIC);
446 /* Copy back into socket for getsockname() use. */
448 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
449 sp->pf->to_sk_saddr(addr, sk);
455 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
457 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
458 * at any one time. If a sender, after sending an ASCONF chunk, decides
459 * it needs to transfer another ASCONF Chunk, it MUST wait until the
460 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
461 * subsequent ASCONF. Note this restriction binds each side, so at any
462 * time two ASCONF may be in-transit on any given association (one sent
463 * from each endpoint).
465 static int sctp_send_asconf(struct sctp_association *asoc,
466 struct sctp_chunk *chunk)
468 struct net *net = sock_net(asoc->base.sk);
471 /* If there is an outstanding ASCONF chunk, queue it for later
474 if (asoc->addip_last_asconf) {
475 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
479 /* Hold the chunk until an ASCONF_ACK is received. */
480 sctp_chunk_hold(chunk);
481 retval = sctp_primitive_ASCONF(net, asoc, chunk);
483 sctp_chunk_free(chunk);
485 asoc->addip_last_asconf = chunk;
491 /* Add a list of addresses as bind addresses to local endpoint or
494 * Basically run through each address specified in the addrs/addrcnt
495 * array/length pair, determine if it is IPv6 or IPv4 and call
496 * sctp_do_bind() on it.
498 * If any of them fails, then the operation will be reversed and the
499 * ones that were added will be removed.
501 * Only sctp_setsockopt_bindx() is supposed to call this function.
503 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
508 struct sockaddr *sa_addr;
511 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
515 for (cnt = 0; cnt < addrcnt; cnt++) {
516 /* The list may contain either IPv4 or IPv6 address;
517 * determine the address length for walking thru the list.
520 af = sctp_get_af_specific(sa_addr->sa_family);
526 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
529 addr_buf += af->sockaddr_len;
533 /* Failed. Cleanup the ones that have been added */
535 sctp_bindx_rem(sk, addrs, cnt);
543 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
544 * associations that are part of the endpoint indicating that a list of local
545 * addresses are added to the endpoint.
547 * If any of the addresses is already in the bind address list of the
548 * association, we do not send the chunk for that association. But it will not
549 * affect other associations.
551 * Only sctp_setsockopt_bindx() is supposed to call this function.
553 static int sctp_send_asconf_add_ip(struct sock *sk,
554 struct sockaddr *addrs,
557 struct net *net = sock_net(sk);
558 struct sctp_sock *sp;
559 struct sctp_endpoint *ep;
560 struct sctp_association *asoc;
561 struct sctp_bind_addr *bp;
562 struct sctp_chunk *chunk;
563 struct sctp_sockaddr_entry *laddr;
564 union sctp_addr *addr;
565 union sctp_addr saveaddr;
572 if (!net->sctp.addip_enable)
578 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
579 __func__, sk, addrs, addrcnt);
581 list_for_each_entry(asoc, &ep->asocs, asocs) {
582 if (!asoc->peer.asconf_capable)
585 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
588 if (!sctp_state(asoc, ESTABLISHED))
591 /* Check if any address in the packed array of addresses is
592 * in the bind address list of the association. If so,
593 * do not send the asconf chunk to its peer, but continue with
594 * other associations.
597 for (i = 0; i < addrcnt; i++) {
599 af = sctp_get_af_specific(addr->v4.sin_family);
605 if (sctp_assoc_lookup_laddr(asoc, addr))
608 addr_buf += af->sockaddr_len;
613 /* Use the first valid address in bind addr list of
614 * association as Address Parameter of ASCONF CHUNK.
616 bp = &asoc->base.bind_addr;
617 p = bp->address_list.next;
618 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
619 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
620 addrcnt, SCTP_PARAM_ADD_IP);
626 /* Add the new addresses to the bind address list with
627 * use_as_src set to 0.
630 for (i = 0; i < addrcnt; i++) {
632 af = sctp_get_af_specific(addr->v4.sin_family);
633 memcpy(&saveaddr, addr, af->sockaddr_len);
634 retval = sctp_add_bind_addr(bp, &saveaddr,
636 SCTP_ADDR_NEW, GFP_ATOMIC);
637 addr_buf += af->sockaddr_len;
639 if (asoc->src_out_of_asoc_ok) {
640 struct sctp_transport *trans;
642 list_for_each_entry(trans,
643 &asoc->peer.transport_addr_list, transports) {
644 /* Clear the source and route cache */
645 sctp_transport_dst_release(trans);
646 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
647 2*asoc->pathmtu, 4380));
648 trans->ssthresh = asoc->peer.i.a_rwnd;
649 trans->rto = asoc->rto_initial;
650 sctp_max_rto(asoc, trans);
651 trans->rtt = trans->srtt = trans->rttvar = 0;
652 sctp_transport_route(trans, NULL,
653 sctp_sk(asoc->base.sk));
656 retval = sctp_send_asconf(asoc, chunk);
663 /* Remove a list of addresses from bind addresses list. Do not remove the
666 * Basically run through each address specified in the addrs/addrcnt
667 * array/length pair, determine if it is IPv6 or IPv4 and call
668 * sctp_del_bind() on it.
670 * If any of them fails, then the operation will be reversed and the
671 * ones that were removed will be added back.
673 * At least one address has to be left; if only one address is
674 * available, the operation will return -EBUSY.
676 * Only sctp_setsockopt_bindx() is supposed to call this function.
678 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
680 struct sctp_sock *sp = sctp_sk(sk);
681 struct sctp_endpoint *ep = sp->ep;
683 struct sctp_bind_addr *bp = &ep->base.bind_addr;
686 union sctp_addr *sa_addr;
689 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
690 __func__, sk, addrs, addrcnt);
693 for (cnt = 0; cnt < addrcnt; cnt++) {
694 /* If the bind address list is empty or if there is only one
695 * bind address, there is nothing more to be removed (we need
696 * at least one address here).
698 if (list_empty(&bp->address_list) ||
699 (sctp_list_single_entry(&bp->address_list))) {
705 af = sctp_get_af_specific(sa_addr->sa.sa_family);
711 if (!af->addr_valid(sa_addr, sp, NULL)) {
712 retval = -EADDRNOTAVAIL;
716 if (sa_addr->v4.sin_port &&
717 sa_addr->v4.sin_port != htons(bp->port)) {
722 if (!sa_addr->v4.sin_port)
723 sa_addr->v4.sin_port = htons(bp->port);
725 /* FIXME - There is probably a need to check if sk->sk_saddr and
726 * sk->sk_rcv_addr are currently set to one of the addresses to
727 * be removed. This is something which needs to be looked into
728 * when we are fixing the outstanding issues with multi-homing
729 * socket routing and failover schemes. Refer to comments in
730 * sctp_do_bind(). -daisy
732 retval = sctp_del_bind_addr(bp, sa_addr);
734 addr_buf += af->sockaddr_len;
737 /* Failed. Add the ones that has been removed back */
739 sctp_bindx_add(sk, addrs, cnt);
747 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
748 * the associations that are part of the endpoint indicating that a list of
749 * local addresses are removed from the endpoint.
751 * If any of the addresses is already in the bind address list of the
752 * association, we do not send the chunk for that association. But it will not
753 * affect other associations.
755 * Only sctp_setsockopt_bindx() is supposed to call this function.
757 static int sctp_send_asconf_del_ip(struct sock *sk,
758 struct sockaddr *addrs,
761 struct net *net = sock_net(sk);
762 struct sctp_sock *sp;
763 struct sctp_endpoint *ep;
764 struct sctp_association *asoc;
765 struct sctp_transport *transport;
766 struct sctp_bind_addr *bp;
767 struct sctp_chunk *chunk;
768 union sctp_addr *laddr;
771 struct sctp_sockaddr_entry *saddr;
777 if (!net->sctp.addip_enable)
783 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
784 __func__, sk, addrs, addrcnt);
786 list_for_each_entry(asoc, &ep->asocs, asocs) {
788 if (!asoc->peer.asconf_capable)
791 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
794 if (!sctp_state(asoc, ESTABLISHED))
797 /* Check if any address in the packed array of addresses is
798 * not present in the bind address list of the association.
799 * If so, do not send the asconf chunk to its peer, but
800 * continue with other associations.
803 for (i = 0; i < addrcnt; i++) {
805 af = sctp_get_af_specific(laddr->v4.sin_family);
811 if (!sctp_assoc_lookup_laddr(asoc, laddr))
814 addr_buf += af->sockaddr_len;
819 /* Find one address in the association's bind address list
820 * that is not in the packed array of addresses. This is to
821 * make sure that we do not delete all the addresses in the
824 bp = &asoc->base.bind_addr;
825 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
827 if ((laddr == NULL) && (addrcnt == 1)) {
828 if (asoc->asconf_addr_del_pending)
830 asoc->asconf_addr_del_pending =
831 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
832 if (asoc->asconf_addr_del_pending == NULL) {
836 asoc->asconf_addr_del_pending->sa.sa_family =
838 asoc->asconf_addr_del_pending->v4.sin_port =
840 if (addrs->sa_family == AF_INET) {
841 struct sockaddr_in *sin;
843 sin = (struct sockaddr_in *)addrs;
844 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
845 } else if (addrs->sa_family == AF_INET6) {
846 struct sockaddr_in6 *sin6;
848 sin6 = (struct sockaddr_in6 *)addrs;
849 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
852 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
853 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
854 asoc->asconf_addr_del_pending);
856 asoc->src_out_of_asoc_ok = 1;
864 /* We do not need RCU protection throughout this loop
865 * because this is done under a socket lock from the
868 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
876 /* Reset use_as_src flag for the addresses in the bind address
877 * list that are to be deleted.
880 for (i = 0; i < addrcnt; i++) {
882 af = sctp_get_af_specific(laddr->v4.sin_family);
883 list_for_each_entry(saddr, &bp->address_list, list) {
884 if (sctp_cmp_addr_exact(&saddr->a, laddr))
885 saddr->state = SCTP_ADDR_DEL;
887 addr_buf += af->sockaddr_len;
890 /* Update the route and saddr entries for all the transports
891 * as some of the addresses in the bind address list are
892 * about to be deleted and cannot be used as source addresses.
894 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
896 sctp_transport_dst_release(transport);
897 sctp_transport_route(transport, NULL,
898 sctp_sk(asoc->base.sk));
902 /* We don't need to transmit ASCONF */
904 retval = sctp_send_asconf(asoc, chunk);
910 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
911 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
913 struct sock *sk = sctp_opt2sk(sp);
914 union sctp_addr *addr;
917 /* It is safe to write port space in caller. */
919 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
920 af = sctp_get_af_specific(addr->sa.sa_family);
923 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
926 if (addrw->state == SCTP_ADDR_NEW)
927 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
929 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
932 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
935 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
938 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
939 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
942 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
943 * Section 3.1.2 for this usage.
945 * addrs is a pointer to an array of one or more socket addresses. Each
946 * address is contained in its appropriate structure (i.e. struct
947 * sockaddr_in or struct sockaddr_in6) the family of the address type
948 * must be used to distinguish the address length (note that this
949 * representation is termed a "packed array" of addresses). The caller
950 * specifies the number of addresses in the array with addrcnt.
952 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
953 * -1, and sets errno to the appropriate error code.
955 * For SCTP, the port given in each socket address must be the same, or
956 * sctp_bindx() will fail, setting errno to EINVAL.
958 * The flags parameter is formed from the bitwise OR of zero or more of
959 * the following currently defined flags:
961 * SCTP_BINDX_ADD_ADDR
963 * SCTP_BINDX_REM_ADDR
965 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
966 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
967 * addresses from the association. The two flags are mutually exclusive;
968 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
969 * not remove all addresses from an association; sctp_bindx() will
970 * reject such an attempt with EINVAL.
972 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
973 * additional addresses with an endpoint after calling bind(). Or use
974 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
975 * socket is associated with so that no new association accepted will be
976 * associated with those addresses. If the endpoint supports dynamic
977 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
978 * endpoint to send the appropriate message to the peer to change the
979 * peers address lists.
981 * Adding and removing addresses from a connected association is
982 * optional functionality. Implementations that do not support this
983 * functionality should return EOPNOTSUPP.
985 * Basically do nothing but copying the addresses from user to kernel
986 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
987 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
990 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
993 * sk The sk of the socket
994 * addrs The pointer to the addresses in user land
995 * addrssize Size of the addrs buffer
996 * op Operation to perform (add or remove, see the flags of
999 * Returns 0 if ok, <0 errno code on error.
1001 static int sctp_setsockopt_bindx(struct sock *sk,
1002 struct sockaddr __user *addrs,
1003 int addrs_size, int op)
1005 struct sockaddr *kaddrs;
1009 struct sockaddr *sa_addr;
1013 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1014 __func__, sk, addrs, addrs_size, op);
1016 if (unlikely(addrs_size <= 0))
1019 kaddrs = vmemdup_user(addrs, addrs_size);
1020 if (unlikely(IS_ERR(kaddrs)))
1021 return PTR_ERR(kaddrs);
1023 /* Walk through the addrs buffer and count the number of addresses. */
1025 while (walk_size < addrs_size) {
1026 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1032 af = sctp_get_af_specific(sa_addr->sa_family);
1034 /* If the address family is not supported or if this address
1035 * causes the address buffer to overflow return EINVAL.
1037 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1048 case SCTP_BINDX_ADD_ADDR:
1049 /* Allow security module to validate bindx addresses. */
1050 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1051 (struct sockaddr *)kaddrs,
1055 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1058 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1061 case SCTP_BINDX_REM_ADDR:
1062 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1065 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1079 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1081 * Common routine for handling connect() and sctp_connectx().
1082 * Connect will come in with just a single address.
1084 static int __sctp_connect(struct sock *sk,
1085 struct sockaddr *kaddrs,
1087 sctp_assoc_t *assoc_id)
1089 struct net *net = sock_net(sk);
1090 struct sctp_sock *sp;
1091 struct sctp_endpoint *ep;
1092 struct sctp_association *asoc = NULL;
1093 struct sctp_association *asoc2;
1094 struct sctp_transport *transport;
1096 enum sctp_scope scope;
1101 union sctp_addr *sa_addr = NULL;
1103 unsigned short port;
1104 unsigned int f_flags = 0;
1109 /* connect() cannot be done on a socket that is already in ESTABLISHED
1110 * state - UDP-style peeled off socket or a TCP-style socket that
1111 * is already connected.
1112 * It cannot be done even on a TCP-style listening socket.
1114 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1115 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1120 /* Walk through the addrs buffer and count the number of addresses. */
1122 while (walk_size < addrs_size) {
1125 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1131 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1133 /* If the address family is not supported or if this address
1134 * causes the address buffer to overflow return EINVAL.
1136 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1141 port = ntohs(sa_addr->v4.sin_port);
1143 /* Save current address so we can work with it */
1144 memcpy(&to, sa_addr, af->sockaddr_len);
1146 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1150 /* Make sure the destination port is correctly set
1153 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1158 /* Check if there already is a matching association on the
1159 * endpoint (other than the one created here).
1161 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1162 if (asoc2 && asoc2 != asoc) {
1163 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1170 /* If we could not find a matching association on the endpoint,
1171 * make sure that there is no peeled-off association matching
1172 * the peer address even on another socket.
1174 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1175 err = -EADDRNOTAVAIL;
1180 /* If a bind() or sctp_bindx() is not called prior to
1181 * an sctp_connectx() call, the system picks an
1182 * ephemeral port and will choose an address set
1183 * equivalent to binding with a wildcard address.
1185 if (!ep->base.bind_addr.port) {
1186 if (sctp_autobind(sk)) {
1192 * If an unprivileged user inherits a 1-many
1193 * style socket with open associations on a
1194 * privileged port, it MAY be permitted to
1195 * accept new associations, but it SHOULD NOT
1196 * be permitted to open new associations.
1198 if (ep->base.bind_addr.port <
1199 inet_prot_sock(net) &&
1200 !ns_capable(net->user_ns,
1201 CAP_NET_BIND_SERVICE)) {
1207 scope = sctp_scope(&to);
1208 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1214 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1222 /* Prime the peer's transport structures. */
1223 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1231 addr_buf += af->sockaddr_len;
1232 walk_size += af->sockaddr_len;
1235 /* In case the user of sctp_connectx() wants an association
1236 * id back, assign one now.
1239 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1244 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1249 /* Initialize sk's dport and daddr for getpeername() */
1250 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1251 sp->pf->to_sk_daddr(sa_addr, sk);
1254 /* in-kernel sockets don't generally have a file allocated to them
1255 * if all they do is call sock_create_kern().
1257 if (sk->sk_socket->file)
1258 f_flags = sk->sk_socket->file->f_flags;
1260 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1263 *assoc_id = asoc->assoc_id;
1265 err = sctp_wait_for_connect(asoc, &timeo);
1266 /* Note: the asoc may be freed after the return of
1267 * sctp_wait_for_connect.
1270 /* Don't free association on exit. */
1274 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1275 __func__, asoc, kaddrs, err);
1278 /* sctp_primitive_ASSOCIATE may have added this association
1279 * To the hash table, try to unhash it, just in case, its a noop
1280 * if it wasn't hashed so we're safe
1282 sctp_association_free(asoc);
1287 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1290 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1291 * sctp_assoc_t *asoc);
1293 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1294 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1295 * or IPv6 addresses.
1297 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1298 * Section 3.1.2 for this usage.
1300 * addrs is a pointer to an array of one or more socket addresses. Each
1301 * address is contained in its appropriate structure (i.e. struct
1302 * sockaddr_in or struct sockaddr_in6) the family of the address type
1303 * must be used to distengish the address length (note that this
1304 * representation is termed a "packed array" of addresses). The caller
1305 * specifies the number of addresses in the array with addrcnt.
1307 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1308 * the association id of the new association. On failure, sctp_connectx()
1309 * returns -1, and sets errno to the appropriate error code. The assoc_id
1310 * is not touched by the kernel.
1312 * For SCTP, the port given in each socket address must be the same, or
1313 * sctp_connectx() will fail, setting errno to EINVAL.
1315 * An application can use sctp_connectx to initiate an association with
1316 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1317 * allows a caller to specify multiple addresses at which a peer can be
1318 * reached. The way the SCTP stack uses the list of addresses to set up
1319 * the association is implementation dependent. This function only
1320 * specifies that the stack will try to make use of all the addresses in
1321 * the list when needed.
1323 * Note that the list of addresses passed in is only used for setting up
1324 * the association. It does not necessarily equal the set of addresses
1325 * the peer uses for the resulting association. If the caller wants to
1326 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1327 * retrieve them after the association has been set up.
1329 * Basically do nothing but copying the addresses from user to kernel
1330 * land and invoking either sctp_connectx(). This is used for tunneling
1331 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1333 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1336 * sk The sk of the socket
1337 * addrs The pointer to the addresses in user land
1338 * addrssize Size of the addrs buffer
1340 * Returns >=0 if ok, <0 errno code on error.
1342 static int __sctp_setsockopt_connectx(struct sock *sk,
1343 struct sockaddr __user *addrs,
1345 sctp_assoc_t *assoc_id)
1347 struct sockaddr *kaddrs;
1350 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1351 __func__, sk, addrs, addrs_size);
1353 if (unlikely(addrs_size <= 0))
1356 kaddrs = vmemdup_user(addrs, addrs_size);
1357 if (unlikely(IS_ERR(kaddrs)))
1358 return PTR_ERR(kaddrs);
1360 /* Allow security module to validate connectx addresses. */
1361 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1362 (struct sockaddr *)kaddrs,
1367 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1376 * This is an older interface. It's kept for backward compatibility
1377 * to the option that doesn't provide association id.
1379 static int sctp_setsockopt_connectx_old(struct sock *sk,
1380 struct sockaddr __user *addrs,
1383 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1387 * New interface for the API. The since the API is done with a socket
1388 * option, to make it simple we feed back the association id is as a return
1389 * indication to the call. Error is always negative and association id is
1392 static int sctp_setsockopt_connectx(struct sock *sk,
1393 struct sockaddr __user *addrs,
1396 sctp_assoc_t assoc_id = 0;
1399 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1408 * New (hopefully final) interface for the API.
1409 * We use the sctp_getaddrs_old structure so that use-space library
1410 * can avoid any unnecessary allocations. The only different part
1411 * is that we store the actual length of the address buffer into the
1412 * addrs_num structure member. That way we can re-use the existing
1415 #ifdef CONFIG_COMPAT
1416 struct compat_sctp_getaddrs_old {
1417 sctp_assoc_t assoc_id;
1419 compat_uptr_t addrs; /* struct sockaddr * */
1423 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1424 char __user *optval,
1427 struct sctp_getaddrs_old param;
1428 sctp_assoc_t assoc_id = 0;
1431 #ifdef CONFIG_COMPAT
1432 if (in_compat_syscall()) {
1433 struct compat_sctp_getaddrs_old param32;
1435 if (len < sizeof(param32))
1437 if (copy_from_user(¶m32, optval, sizeof(param32)))
1440 param.assoc_id = param32.assoc_id;
1441 param.addr_num = param32.addr_num;
1442 param.addrs = compat_ptr(param32.addrs);
1446 if (len < sizeof(param))
1448 if (copy_from_user(¶m, optval, sizeof(param)))
1452 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1453 param.addrs, param.addr_num,
1455 if (err == 0 || err == -EINPROGRESS) {
1456 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1458 if (put_user(sizeof(assoc_id), optlen))
1465 /* API 3.1.4 close() - UDP Style Syntax
1466 * Applications use close() to perform graceful shutdown (as described in
1467 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1468 * by a UDP-style socket.
1472 * ret = close(int sd);
1474 * sd - the socket descriptor of the associations to be closed.
1476 * To gracefully shutdown a specific association represented by the
1477 * UDP-style socket, an application should use the sendmsg() call,
1478 * passing no user data, but including the appropriate flag in the
1479 * ancillary data (see Section xxxx).
1481 * If sd in the close() call is a branched-off socket representing only
1482 * one association, the shutdown is performed on that association only.
1484 * 4.1.6 close() - TCP Style Syntax
1486 * Applications use close() to gracefully close down an association.
1490 * int close(int sd);
1492 * sd - the socket descriptor of the association to be closed.
1494 * After an application calls close() on a socket descriptor, no further
1495 * socket operations will succeed on that descriptor.
1497 * API 7.1.4 SO_LINGER
1499 * An application using the TCP-style socket can use this option to
1500 * perform the SCTP ABORT primitive. The linger option structure is:
1503 * int l_onoff; // option on/off
1504 * int l_linger; // linger time
1507 * To enable the option, set l_onoff to 1. If the l_linger value is set
1508 * to 0, calling close() is the same as the ABORT primitive. If the
1509 * value is set to a negative value, the setsockopt() call will return
1510 * an error. If the value is set to a positive value linger_time, the
1511 * close() can be blocked for at most linger_time ms. If the graceful
1512 * shutdown phase does not finish during this period, close() will
1513 * return but the graceful shutdown phase continues in the system.
1515 static void sctp_close(struct sock *sk, long timeout)
1517 struct net *net = sock_net(sk);
1518 struct sctp_endpoint *ep;
1519 struct sctp_association *asoc;
1520 struct list_head *pos, *temp;
1521 unsigned int data_was_unread;
1523 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1525 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1526 sk->sk_shutdown = SHUTDOWN_MASK;
1527 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1529 ep = sctp_sk(sk)->ep;
1531 /* Clean up any skbs sitting on the receive queue. */
1532 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1533 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1535 /* Walk all associations on an endpoint. */
1536 list_for_each_safe(pos, temp, &ep->asocs) {
1537 asoc = list_entry(pos, struct sctp_association, asocs);
1539 if (sctp_style(sk, TCP)) {
1540 /* A closed association can still be in the list if
1541 * it belongs to a TCP-style listening socket that is
1542 * not yet accepted. If so, free it. If not, send an
1543 * ABORT or SHUTDOWN based on the linger options.
1545 if (sctp_state(asoc, CLOSED)) {
1546 sctp_association_free(asoc);
1551 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1552 !skb_queue_empty(&asoc->ulpq.reasm) ||
1553 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1554 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1555 struct sctp_chunk *chunk;
1557 chunk = sctp_make_abort_user(asoc, NULL, 0);
1558 sctp_primitive_ABORT(net, asoc, chunk);
1560 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1563 /* On a TCP-style socket, block for at most linger_time if set. */
1564 if (sctp_style(sk, TCP) && timeout)
1565 sctp_wait_for_close(sk, timeout);
1567 /* This will run the backlog queue. */
1570 /* Supposedly, no process has access to the socket, but
1571 * the net layers still may.
1572 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1573 * held and that should be grabbed before socket lock.
1575 spin_lock_bh(&net->sctp.addr_wq_lock);
1576 bh_lock_sock_nested(sk);
1578 /* Hold the sock, since sk_common_release() will put sock_put()
1579 * and we have just a little more cleanup.
1582 sk_common_release(sk);
1585 spin_unlock_bh(&net->sctp.addr_wq_lock);
1589 SCTP_DBG_OBJCNT_DEC(sock);
1592 /* Handle EPIPE error. */
1593 static int sctp_error(struct sock *sk, int flags, int err)
1596 err = sock_error(sk) ? : -EPIPE;
1597 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1598 send_sig(SIGPIPE, current, 0);
1602 /* API 3.1.3 sendmsg() - UDP Style Syntax
1604 * An application uses sendmsg() and recvmsg() calls to transmit data to
1605 * and receive data from its peer.
1607 * ssize_t sendmsg(int socket, const struct msghdr *message,
1610 * socket - the socket descriptor of the endpoint.
1611 * message - pointer to the msghdr structure which contains a single
1612 * user message and possibly some ancillary data.
1614 * See Section 5 for complete description of the data
1617 * flags - flags sent or received with the user message, see Section
1618 * 5 for complete description of the flags.
1620 * Note: This function could use a rewrite especially when explicit
1621 * connect support comes in.
1623 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1625 static int sctp_msghdr_parse(const struct msghdr *msg,
1626 struct sctp_cmsgs *cmsgs);
1628 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1629 struct sctp_sndrcvinfo *srinfo,
1630 const struct msghdr *msg, size_t msg_len)
1635 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1638 if (msg_len > sk->sk_sndbuf)
1641 memset(cmsgs, 0, sizeof(*cmsgs));
1642 err = sctp_msghdr_parse(msg, cmsgs);
1644 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1648 memset(srinfo, 0, sizeof(*srinfo));
1649 if (cmsgs->srinfo) {
1650 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1651 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1652 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1653 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1654 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1655 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1659 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1660 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1661 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1662 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1663 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1666 if (cmsgs->prinfo) {
1667 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1668 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1669 cmsgs->prinfo->pr_policy);
1672 sflags = srinfo->sinfo_flags;
1673 if (!sflags && msg_len)
1676 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1679 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1680 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1683 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1689 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1690 struct sctp_cmsgs *cmsgs,
1691 union sctp_addr *daddr,
1692 struct sctp_transport **tp)
1694 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1695 struct net *net = sock_net(sk);
1696 struct sctp_association *asoc;
1697 enum sctp_scope scope;
1698 struct cmsghdr *cmsg;
1704 if (sflags & (SCTP_EOF | SCTP_ABORT))
1707 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1708 sctp_sstate(sk, CLOSING)))
1709 return -EADDRNOTAVAIL;
1711 if (sctp_endpoint_is_peeled_off(ep, daddr))
1712 return -EADDRNOTAVAIL;
1714 if (!ep->base.bind_addr.port) {
1715 if (sctp_autobind(sk))
1718 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1719 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1723 scope = sctp_scope(daddr);
1725 /* Label connection socket for first association 1-to-many
1726 * style for client sequence socket()->sendmsg(). This
1727 * needs to be done before sctp_assoc_add_peer() as that will
1728 * set up the initial packet that needs to account for any
1729 * security ip options (CIPSO/CALIPSO) added to the packet.
1731 af = sctp_get_af_specific(daddr->sa.sa_family);
1734 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1735 (struct sockaddr *)daddr,
1740 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1744 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1750 struct sctp_initmsg *init = cmsgs->init;
1752 if (init->sinit_num_ostreams) {
1753 __u16 outcnt = init->sinit_num_ostreams;
1755 asoc->c.sinit_num_ostreams = outcnt;
1756 /* outcnt has been changed, need to re-init stream */
1757 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1763 if (init->sinit_max_instreams)
1764 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1766 if (init->sinit_max_attempts)
1767 asoc->max_init_attempts = init->sinit_max_attempts;
1769 if (init->sinit_max_init_timeo)
1770 asoc->max_init_timeo =
1771 msecs_to_jiffies(init->sinit_max_init_timeo);
1774 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1780 if (!cmsgs->addrs_msg)
1783 /* sendv addr list parse */
1784 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1785 struct sctp_transport *transport;
1786 struct sctp_association *old;
1787 union sctp_addr _daddr;
1790 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1791 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1792 cmsg->cmsg_type != SCTP_DSTADDRV6))
1796 memset(daddr, 0, sizeof(*daddr));
1797 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1798 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1799 if (dlen < sizeof(struct in_addr)) {
1804 dlen = sizeof(struct in_addr);
1805 daddr->v4.sin_family = AF_INET;
1806 daddr->v4.sin_port = htons(asoc->peer.port);
1807 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1809 if (dlen < sizeof(struct in6_addr)) {
1814 dlen = sizeof(struct in6_addr);
1815 daddr->v6.sin6_family = AF_INET6;
1816 daddr->v6.sin6_port = htons(asoc->peer.port);
1817 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1819 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1823 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1824 if (old && old != asoc) {
1825 if (old->state >= SCTP_STATE_ESTABLISHED)
1832 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1833 err = -EADDRNOTAVAIL;
1837 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1848 sctp_association_free(asoc);
1852 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1853 __u16 sflags, struct msghdr *msg,
1856 struct sock *sk = asoc->base.sk;
1857 struct net *net = sock_net(sk);
1859 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1862 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1863 !sctp_state(asoc, ESTABLISHED))
1866 if (sflags & SCTP_EOF) {
1867 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1868 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1873 if (sflags & SCTP_ABORT) {
1874 struct sctp_chunk *chunk;
1876 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1880 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1881 sctp_primitive_ABORT(net, asoc, chunk);
1889 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1890 struct msghdr *msg, size_t msg_len,
1891 struct sctp_transport *transport,
1892 struct sctp_sndrcvinfo *sinfo)
1894 struct sock *sk = asoc->base.sk;
1895 struct net *net = sock_net(sk);
1896 struct sctp_datamsg *datamsg;
1897 bool wait_connect = false;
1898 struct sctp_chunk *chunk;
1902 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1907 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1908 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1913 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1918 if (asoc->pmtu_pending)
1919 sctp_assoc_pending_pmtu(asoc);
1921 if (sctp_wspace(asoc) < msg_len)
1922 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1924 if (!sctp_wspace(asoc)) {
1925 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1926 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1931 if (sctp_state(asoc, CLOSED)) {
1932 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1936 if (sctp_sk(sk)->strm_interleave) {
1937 timeo = sock_sndtimeo(sk, 0);
1938 err = sctp_wait_for_connect(asoc, &timeo);
1942 wait_connect = true;
1945 pr_debug("%s: we associated primitively\n", __func__);
1948 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1949 if (IS_ERR(datamsg)) {
1950 err = PTR_ERR(datamsg);
1954 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1956 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1957 sctp_chunk_hold(chunk);
1958 sctp_set_owner_w(chunk);
1959 chunk->transport = transport;
1962 err = sctp_primitive_SEND(net, asoc, datamsg);
1964 sctp_datamsg_free(datamsg);
1968 pr_debug("%s: we sent primitively\n", __func__);
1970 sctp_datamsg_put(datamsg);
1972 if (unlikely(wait_connect)) {
1973 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1974 sctp_wait_for_connect(asoc, &timeo);
1983 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1984 const struct msghdr *msg,
1985 struct sctp_cmsgs *cmsgs)
1987 union sctp_addr *daddr = NULL;
1990 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1991 int len = msg->msg_namelen;
1993 if (len > sizeof(*daddr))
1994 len = sizeof(*daddr);
1996 daddr = (union sctp_addr *)msg->msg_name;
1998 err = sctp_verify_addr(sk, daddr, len);
2000 return ERR_PTR(err);
2006 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2007 struct sctp_sndrcvinfo *sinfo,
2008 struct sctp_cmsgs *cmsgs)
2010 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2011 sinfo->sinfo_stream = asoc->default_stream;
2012 sinfo->sinfo_ppid = asoc->default_ppid;
2013 sinfo->sinfo_context = asoc->default_context;
2014 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
2017 sinfo->sinfo_flags = asoc->default_flags;
2020 if (!cmsgs->srinfo && !cmsgs->prinfo)
2021 sinfo->sinfo_timetolive = asoc->default_timetolive;
2023 if (cmsgs->authinfo) {
2024 /* Reuse sinfo_tsn to indicate that authinfo was set and
2025 * sinfo_ssn to save the keyid on tx path.
2027 sinfo->sinfo_tsn = 1;
2028 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2032 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2034 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2035 struct sctp_transport *transport = NULL;
2036 struct sctp_sndrcvinfo _sinfo, *sinfo;
2037 struct sctp_association *asoc;
2038 struct sctp_cmsgs cmsgs;
2039 union sctp_addr *daddr;
2044 /* Parse and get snd_info */
2045 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2050 sflags = sinfo->sinfo_flags;
2052 /* Get daddr from msg */
2053 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2054 if (IS_ERR(daddr)) {
2055 err = PTR_ERR(daddr);
2061 /* SCTP_SENDALL process */
2062 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2063 list_for_each_entry(asoc, &ep->asocs, asocs) {
2064 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2071 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2073 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2078 iov_iter_revert(&msg->msg_iter, err);
2084 /* Get and check or create asoc */
2086 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2088 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2093 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2098 asoc = transport->asoc;
2102 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2105 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2111 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2116 /* Update snd_info with the asoc */
2117 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2119 /* Send msg to the asoc */
2120 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2121 if (err < 0 && err != -ESRCH && new)
2122 sctp_association_free(asoc);
2127 return sctp_error(sk, msg->msg_flags, err);
2130 /* This is an extended version of skb_pull() that removes the data from the
2131 * start of a skb even when data is spread across the list of skb's in the
2132 * frag_list. len specifies the total amount of data that needs to be removed.
2133 * when 'len' bytes could be removed from the skb, it returns 0.
2134 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2135 * could not be removed.
2137 static int sctp_skb_pull(struct sk_buff *skb, int len)
2139 struct sk_buff *list;
2140 int skb_len = skb_headlen(skb);
2143 if (len <= skb_len) {
2144 __skb_pull(skb, len);
2148 __skb_pull(skb, skb_len);
2150 skb_walk_frags(skb, list) {
2151 rlen = sctp_skb_pull(list, len);
2152 skb->len -= (len-rlen);
2153 skb->data_len -= (len-rlen);
2164 /* API 3.1.3 recvmsg() - UDP Style Syntax
2166 * ssize_t recvmsg(int socket, struct msghdr *message,
2169 * socket - the socket descriptor of the endpoint.
2170 * message - pointer to the msghdr structure which contains a single
2171 * user message and possibly some ancillary data.
2173 * See Section 5 for complete description of the data
2176 * flags - flags sent or received with the user message, see Section
2177 * 5 for complete description of the flags.
2179 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2180 int noblock, int flags, int *addr_len)
2182 struct sctp_ulpevent *event = NULL;
2183 struct sctp_sock *sp = sctp_sk(sk);
2184 struct sk_buff *skb, *head_skb;
2189 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2190 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2195 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2196 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2201 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2205 /* Get the total length of the skb including any skb's in the
2214 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2216 event = sctp_skb2event(skb);
2221 if (event->chunk && event->chunk->head_skb)
2222 head_skb = event->chunk->head_skb;
2225 sock_recv_ts_and_drops(msg, sk, head_skb);
2226 if (sctp_ulpevent_is_notification(event)) {
2227 msg->msg_flags |= MSG_NOTIFICATION;
2228 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2230 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2233 /* Check if we allow SCTP_NXTINFO. */
2234 if (sp->recvnxtinfo)
2235 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2236 /* Check if we allow SCTP_RCVINFO. */
2237 if (sp->recvrcvinfo)
2238 sctp_ulpevent_read_rcvinfo(event, msg);
2239 /* Check if we allow SCTP_SNDRCVINFO. */
2240 if (sp->subscribe.sctp_data_io_event)
2241 sctp_ulpevent_read_sndrcvinfo(event, msg);
2245 /* If skb's length exceeds the user's buffer, update the skb and
2246 * push it back to the receive_queue so that the next call to
2247 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2249 if (skb_len > copied) {
2250 msg->msg_flags &= ~MSG_EOR;
2251 if (flags & MSG_PEEK)
2253 sctp_skb_pull(skb, copied);
2254 skb_queue_head(&sk->sk_receive_queue, skb);
2256 /* When only partial message is copied to the user, increase
2257 * rwnd by that amount. If all the data in the skb is read,
2258 * rwnd is updated when the event is freed.
2260 if (!sctp_ulpevent_is_notification(event))
2261 sctp_assoc_rwnd_increase(event->asoc, copied);
2263 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2264 (event->msg_flags & MSG_EOR))
2265 msg->msg_flags |= MSG_EOR;
2267 msg->msg_flags &= ~MSG_EOR;
2270 if (flags & MSG_PEEK) {
2271 /* Release the skb reference acquired after peeking the skb in
2272 * sctp_skb_recv_datagram().
2276 /* Free the event which includes releasing the reference to
2277 * the owner of the skb, freeing the skb and updating the
2280 sctp_ulpevent_free(event);
2287 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2289 * This option is a on/off flag. If enabled no SCTP message
2290 * fragmentation will be performed. Instead if a message being sent
2291 * exceeds the current PMTU size, the message will NOT be sent and
2292 * instead a error will be indicated to the user.
2294 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2295 char __user *optval,
2296 unsigned int optlen)
2300 if (optlen < sizeof(int))
2303 if (get_user(val, (int __user *)optval))
2306 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2311 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2312 unsigned int optlen)
2314 struct sctp_association *asoc;
2315 struct sctp_ulpevent *event;
2317 if (optlen > sizeof(struct sctp_event_subscribe))
2319 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2322 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2323 * if there is no data to be sent or retransmit, the stack will
2324 * immediately send up this notification.
2326 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2327 &sctp_sk(sk)->subscribe)) {
2328 asoc = sctp_id2assoc(sk, 0);
2330 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2331 event = sctp_ulpevent_make_sender_dry_event(asoc,
2332 GFP_USER | __GFP_NOWARN);
2336 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2343 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2345 * This socket option is applicable to the UDP-style socket only. When
2346 * set it will cause associations that are idle for more than the
2347 * specified number of seconds to automatically close. An association
2348 * being idle is defined an association that has NOT sent or received
2349 * user data. The special value of '0' indicates that no automatic
2350 * close of any associations should be performed. The option expects an
2351 * integer defining the number of seconds of idle time before an
2352 * association is closed.
2354 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2355 unsigned int optlen)
2357 struct sctp_sock *sp = sctp_sk(sk);
2358 struct net *net = sock_net(sk);
2360 /* Applicable to UDP-style socket only */
2361 if (sctp_style(sk, TCP))
2363 if (optlen != sizeof(int))
2365 if (copy_from_user(&sp->autoclose, optval, optlen))
2368 if (sp->autoclose > net->sctp.max_autoclose)
2369 sp->autoclose = net->sctp.max_autoclose;
2374 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2376 * Applications can enable or disable heartbeats for any peer address of
2377 * an association, modify an address's heartbeat interval, force a
2378 * heartbeat to be sent immediately, and adjust the address's maximum
2379 * number of retransmissions sent before an address is considered
2380 * unreachable. The following structure is used to access and modify an
2381 * address's parameters:
2383 * struct sctp_paddrparams {
2384 * sctp_assoc_t spp_assoc_id;
2385 * struct sockaddr_storage spp_address;
2386 * uint32_t spp_hbinterval;
2387 * uint16_t spp_pathmaxrxt;
2388 * uint32_t spp_pathmtu;
2389 * uint32_t spp_sackdelay;
2390 * uint32_t spp_flags;
2393 * spp_assoc_id - (one-to-many style socket) This is filled in the
2394 * application, and identifies the association for
2396 * spp_address - This specifies which address is of interest.
2397 * spp_hbinterval - This contains the value of the heartbeat interval,
2398 * in milliseconds. If a value of zero
2399 * is present in this field then no changes are to
2400 * be made to this parameter.
2401 * spp_pathmaxrxt - This contains the maximum number of
2402 * retransmissions before this address shall be
2403 * considered unreachable. If a value of zero
2404 * is present in this field then no changes are to
2405 * be made to this parameter.
2406 * spp_pathmtu - When Path MTU discovery is disabled the value
2407 * specified here will be the "fixed" path mtu.
2408 * Note that if the spp_address field is empty
2409 * then all associations on this address will
2410 * have this fixed path mtu set upon them.
2412 * spp_sackdelay - When delayed sack is enabled, this value specifies
2413 * the number of milliseconds that sacks will be delayed
2414 * for. This value will apply to all addresses of an
2415 * association if the spp_address field is empty. Note
2416 * also, that if delayed sack is enabled and this
2417 * value is set to 0, no change is made to the last
2418 * recorded delayed sack timer value.
2420 * spp_flags - These flags are used to control various features
2421 * on an association. The flag field may contain
2422 * zero or more of the following options.
2424 * SPP_HB_ENABLE - Enable heartbeats on the
2425 * specified address. Note that if the address
2426 * field is empty all addresses for the association
2427 * have heartbeats enabled upon them.
2429 * SPP_HB_DISABLE - Disable heartbeats on the
2430 * speicifed address. Note that if the address
2431 * field is empty all addresses for the association
2432 * will have their heartbeats disabled. Note also
2433 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2434 * mutually exclusive, only one of these two should
2435 * be specified. Enabling both fields will have
2436 * undetermined results.
2438 * SPP_HB_DEMAND - Request a user initiated heartbeat
2439 * to be made immediately.
2441 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2442 * heartbeat delayis to be set to the value of 0
2445 * SPP_PMTUD_ENABLE - This field will enable 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.
2450 * SPP_PMTUD_DISABLE - This field will disable PMTU
2451 * discovery upon the specified address. Note that
2452 * if the address feild is empty then all addresses
2453 * on the association are effected. Not also that
2454 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2455 * exclusive. Enabling both will have undetermined
2458 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2459 * on delayed sack. The time specified in spp_sackdelay
2460 * is used to specify the sack delay for this address. Note
2461 * that if spp_address is empty then all addresses will
2462 * enable delayed sack and take on the sack delay
2463 * value specified in spp_sackdelay.
2464 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2465 * off delayed sack. If the spp_address field is blank then
2466 * delayed sack is disabled for the entire association. Note
2467 * also that this field is mutually exclusive to
2468 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2471 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2472 struct sctp_transport *trans,
2473 struct sctp_association *asoc,
2474 struct sctp_sock *sp,
2477 int sackdelay_change)
2481 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2482 struct net *net = sock_net(trans->asoc->base.sk);
2484 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2489 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2490 * this field is ignored. Note also that a value of zero indicates
2491 * the current setting should be left unchanged.
2493 if (params->spp_flags & SPP_HB_ENABLE) {
2495 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2496 * set. This lets us use 0 value when this flag
2499 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2500 params->spp_hbinterval = 0;
2502 if (params->spp_hbinterval ||
2503 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2506 msecs_to_jiffies(params->spp_hbinterval);
2509 msecs_to_jiffies(params->spp_hbinterval);
2511 sp->hbinterval = params->spp_hbinterval;
2518 trans->param_flags =
2519 (trans->param_flags & ~SPP_HB) | hb_change;
2522 (asoc->param_flags & ~SPP_HB) | hb_change;
2525 (sp->param_flags & ~SPP_HB) | hb_change;
2529 /* When Path MTU discovery is disabled the value specified here will
2530 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2531 * include the flag SPP_PMTUD_DISABLE for this field to have any
2534 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2536 trans->pathmtu = params->spp_pathmtu;
2537 sctp_assoc_sync_pmtu(asoc);
2539 asoc->pathmtu = params->spp_pathmtu;
2541 sp->pathmtu = params->spp_pathmtu;
2547 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2548 (params->spp_flags & SPP_PMTUD_ENABLE);
2549 trans->param_flags =
2550 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2552 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2553 sctp_assoc_sync_pmtu(asoc);
2557 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2560 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2564 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2565 * value of this field is ignored. Note also that a value of zero
2566 * indicates the current setting should be left unchanged.
2568 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2571 msecs_to_jiffies(params->spp_sackdelay);
2574 msecs_to_jiffies(params->spp_sackdelay);
2576 sp->sackdelay = params->spp_sackdelay;
2580 if (sackdelay_change) {
2582 trans->param_flags =
2583 (trans->param_flags & ~SPP_SACKDELAY) |
2587 (asoc->param_flags & ~SPP_SACKDELAY) |
2591 (sp->param_flags & ~SPP_SACKDELAY) |
2596 /* Note that a value of zero indicates the current setting should be
2599 if (params->spp_pathmaxrxt) {
2601 trans->pathmaxrxt = params->spp_pathmaxrxt;
2603 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2605 sp->pathmaxrxt = params->spp_pathmaxrxt;
2612 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2613 char __user *optval,
2614 unsigned int optlen)
2616 struct sctp_paddrparams params;
2617 struct sctp_transport *trans = NULL;
2618 struct sctp_association *asoc = NULL;
2619 struct sctp_sock *sp = sctp_sk(sk);
2621 int hb_change, pmtud_change, sackdelay_change;
2623 if (optlen != sizeof(struct sctp_paddrparams))
2626 if (copy_from_user(¶ms, optval, optlen))
2629 /* Validate flags and value parameters. */
2630 hb_change = params.spp_flags & SPP_HB;
2631 pmtud_change = params.spp_flags & SPP_PMTUD;
2632 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2634 if (hb_change == SPP_HB ||
2635 pmtud_change == SPP_PMTUD ||
2636 sackdelay_change == SPP_SACKDELAY ||
2637 params.spp_sackdelay > 500 ||
2638 (params.spp_pathmtu &&
2639 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2642 /* If an address other than INADDR_ANY is specified, and
2643 * no transport is found, then the request is invalid.
2645 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2646 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2647 params.spp_assoc_id);
2652 /* Get association, if assoc_id != 0 and the socket is a one
2653 * to many style socket, and an association was not found, then
2654 * the id was invalid.
2656 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2657 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2660 /* Heartbeat demand can only be sent on a transport or
2661 * association, but not a socket.
2663 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2666 /* Process parameters. */
2667 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2668 hb_change, pmtud_change,
2674 /* If changes are for association, also apply parameters to each
2677 if (!trans && asoc) {
2678 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2680 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2681 hb_change, pmtud_change,
2689 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2691 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2694 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2696 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2700 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2702 * This option will effect the way delayed acks are performed. This
2703 * option allows you to get or set the delayed ack time, in
2704 * milliseconds. It also allows changing the delayed ack frequency.
2705 * Changing the frequency to 1 disables the delayed sack algorithm. If
2706 * the assoc_id is 0, then this sets or gets the endpoints default
2707 * values. If the assoc_id field is non-zero, then the set or get
2708 * effects the specified association for the one to many model (the
2709 * assoc_id field is ignored by the one to one model). Note that if
2710 * sack_delay or sack_freq are 0 when setting this option, then the
2711 * current values will remain unchanged.
2713 * struct sctp_sack_info {
2714 * sctp_assoc_t sack_assoc_id;
2715 * uint32_t sack_delay;
2716 * uint32_t sack_freq;
2719 * sack_assoc_id - This parameter, indicates which association the user
2720 * is performing an action upon. Note that if this field's value is
2721 * zero then the endpoints default value is changed (effecting future
2722 * associations only).
2724 * sack_delay - This parameter contains the number of milliseconds that
2725 * the user is requesting the delayed ACK timer be set to. Note that
2726 * this value is defined in the standard to be between 200 and 500
2729 * sack_freq - This parameter contains the number of packets that must
2730 * be received before a sack is sent without waiting for the delay
2731 * timer to expire. The default value for this is 2, setting this
2732 * value to 1 will disable the delayed sack algorithm.
2735 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2736 char __user *optval, unsigned int optlen)
2738 struct sctp_sack_info params;
2739 struct sctp_transport *trans = NULL;
2740 struct sctp_association *asoc = NULL;
2741 struct sctp_sock *sp = sctp_sk(sk);
2743 if (optlen == sizeof(struct sctp_sack_info)) {
2744 if (copy_from_user(¶ms, optval, optlen))
2747 if (params.sack_delay == 0 && params.sack_freq == 0)
2749 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2750 pr_warn_ratelimited(DEPRECATED
2752 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2753 "Use struct sctp_sack_info instead\n",
2754 current->comm, task_pid_nr(current));
2755 if (copy_from_user(¶ms, optval, optlen))
2758 if (params.sack_delay == 0)
2759 params.sack_freq = 1;
2761 params.sack_freq = 0;
2765 /* Validate value parameter. */
2766 if (params.sack_delay > 500)
2769 /* Get association, if sack_assoc_id != 0 and the socket is a one
2770 * to many style socket, and an association was not found, then
2771 * the id was invalid.
2773 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2774 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2777 if (params.sack_delay) {
2780 msecs_to_jiffies(params.sack_delay);
2782 sctp_spp_sackdelay_enable(asoc->param_flags);
2784 sp->sackdelay = params.sack_delay;
2786 sctp_spp_sackdelay_enable(sp->param_flags);
2790 if (params.sack_freq == 1) {
2793 sctp_spp_sackdelay_disable(asoc->param_flags);
2796 sctp_spp_sackdelay_disable(sp->param_flags);
2798 } else if (params.sack_freq > 1) {
2800 asoc->sackfreq = params.sack_freq;
2802 sctp_spp_sackdelay_enable(asoc->param_flags);
2804 sp->sackfreq = params.sack_freq;
2806 sctp_spp_sackdelay_enable(sp->param_flags);
2810 /* If change is for association, also apply to each transport. */
2812 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2814 if (params.sack_delay) {
2816 msecs_to_jiffies(params.sack_delay);
2817 trans->param_flags =
2818 sctp_spp_sackdelay_enable(trans->param_flags);
2820 if (params.sack_freq == 1) {
2821 trans->param_flags =
2822 sctp_spp_sackdelay_disable(trans->param_flags);
2823 } else if (params.sack_freq > 1) {
2824 trans->sackfreq = params.sack_freq;
2825 trans->param_flags =
2826 sctp_spp_sackdelay_enable(trans->param_flags);
2834 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2836 * Applications can specify protocol parameters for the default association
2837 * initialization. The option name argument to setsockopt() and getsockopt()
2840 * Setting initialization parameters is effective only on an unconnected
2841 * socket (for UDP-style sockets only future associations are effected
2842 * by the change). With TCP-style sockets, this option is inherited by
2843 * sockets derived from a listener socket.
2845 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2847 struct sctp_initmsg sinit;
2848 struct sctp_sock *sp = sctp_sk(sk);
2850 if (optlen != sizeof(struct sctp_initmsg))
2852 if (copy_from_user(&sinit, optval, optlen))
2855 if (sinit.sinit_num_ostreams)
2856 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2857 if (sinit.sinit_max_instreams)
2858 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2859 if (sinit.sinit_max_attempts)
2860 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2861 if (sinit.sinit_max_init_timeo)
2862 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2868 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2870 * Applications that wish to use the sendto() system call may wish to
2871 * specify a default set of parameters that would normally be supplied
2872 * through the inclusion of ancillary data. This socket option allows
2873 * such an application to set the default sctp_sndrcvinfo structure.
2874 * The application that wishes to use this socket option simply passes
2875 * in to this call the sctp_sndrcvinfo structure defined in Section
2876 * 5.2.2) The input parameters accepted by this call include
2877 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2878 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2879 * to this call if the caller is using the UDP model.
2881 static int sctp_setsockopt_default_send_param(struct sock *sk,
2882 char __user *optval,
2883 unsigned int optlen)
2885 struct sctp_sock *sp = sctp_sk(sk);
2886 struct sctp_association *asoc;
2887 struct sctp_sndrcvinfo info;
2889 if (optlen != sizeof(info))
2891 if (copy_from_user(&info, optval, optlen))
2893 if (info.sinfo_flags &
2894 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2895 SCTP_ABORT | SCTP_EOF))
2898 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2899 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2902 asoc->default_stream = info.sinfo_stream;
2903 asoc->default_flags = info.sinfo_flags;
2904 asoc->default_ppid = info.sinfo_ppid;
2905 asoc->default_context = info.sinfo_context;
2906 asoc->default_timetolive = info.sinfo_timetolive;
2908 sp->default_stream = info.sinfo_stream;
2909 sp->default_flags = info.sinfo_flags;
2910 sp->default_ppid = info.sinfo_ppid;
2911 sp->default_context = info.sinfo_context;
2912 sp->default_timetolive = info.sinfo_timetolive;
2918 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2919 * (SCTP_DEFAULT_SNDINFO)
2921 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2922 char __user *optval,
2923 unsigned int optlen)
2925 struct sctp_sock *sp = sctp_sk(sk);
2926 struct sctp_association *asoc;
2927 struct sctp_sndinfo info;
2929 if (optlen != sizeof(info))
2931 if (copy_from_user(&info, optval, optlen))
2933 if (info.snd_flags &
2934 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2935 SCTP_ABORT | SCTP_EOF))
2938 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2939 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2942 asoc->default_stream = info.snd_sid;
2943 asoc->default_flags = info.snd_flags;
2944 asoc->default_ppid = info.snd_ppid;
2945 asoc->default_context = info.snd_context;
2947 sp->default_stream = info.snd_sid;
2948 sp->default_flags = info.snd_flags;
2949 sp->default_ppid = info.snd_ppid;
2950 sp->default_context = info.snd_context;
2956 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2958 * Requests that the local SCTP stack use the enclosed peer address as
2959 * the association primary. The enclosed address must be one of the
2960 * association peer's addresses.
2962 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2963 unsigned int optlen)
2965 struct sctp_prim prim;
2966 struct sctp_transport *trans;
2970 if (optlen != sizeof(struct sctp_prim))
2973 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2976 /* Allow security module to validate address but need address len. */
2977 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
2981 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
2982 (struct sockaddr *)&prim.ssp_addr,
2987 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2991 sctp_assoc_set_primary(trans->asoc, trans);
2997 * 7.1.5 SCTP_NODELAY
2999 * Turn on/off any Nagle-like algorithm. This means that packets are
3000 * generally sent as soon as possible and no unnecessary delays are
3001 * introduced, at the cost of more packets in the network. Expects an
3002 * integer boolean flag.
3004 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3005 unsigned int optlen)
3009 if (optlen < sizeof(int))
3011 if (get_user(val, (int __user *)optval))
3014 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3020 * 7.1.1 SCTP_RTOINFO
3022 * The protocol parameters used to initialize and bound retransmission
3023 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3024 * and modify these parameters.
3025 * All parameters are time values, in milliseconds. A value of 0, when
3026 * modifying the parameters, indicates that the current value should not
3030 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3032 struct sctp_rtoinfo rtoinfo;
3033 struct sctp_association *asoc;
3034 unsigned long rto_min, rto_max;
3035 struct sctp_sock *sp = sctp_sk(sk);
3037 if (optlen != sizeof (struct sctp_rtoinfo))
3040 if (copy_from_user(&rtoinfo, optval, optlen))
3043 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3045 /* Set the values to the specific association */
3046 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3049 rto_max = rtoinfo.srto_max;
3050 rto_min = rtoinfo.srto_min;
3053 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3055 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3058 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3060 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3062 if (rto_min > rto_max)
3066 if (rtoinfo.srto_initial != 0)
3068 msecs_to_jiffies(rtoinfo.srto_initial);
3069 asoc->rto_max = rto_max;
3070 asoc->rto_min = rto_min;
3072 /* If there is no association or the association-id = 0
3073 * set the values to the endpoint.
3075 if (rtoinfo.srto_initial != 0)
3076 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3077 sp->rtoinfo.srto_max = rto_max;
3078 sp->rtoinfo.srto_min = rto_min;
3086 * 7.1.2 SCTP_ASSOCINFO
3088 * This option is used to tune the maximum retransmission attempts
3089 * of the association.
3090 * Returns an error if the new association retransmission value is
3091 * greater than the sum of the retransmission value of the peer.
3092 * See [SCTP] for more information.
3095 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3098 struct sctp_assocparams assocparams;
3099 struct sctp_association *asoc;
3101 if (optlen != sizeof(struct sctp_assocparams))
3103 if (copy_from_user(&assocparams, optval, optlen))
3106 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3108 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3111 /* Set the values to the specific association */
3113 if (assocparams.sasoc_asocmaxrxt != 0) {
3116 struct sctp_transport *peer_addr;
3118 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3120 path_sum += peer_addr->pathmaxrxt;
3124 /* Only validate asocmaxrxt if we have more than
3125 * one path/transport. We do this because path
3126 * retransmissions are only counted when we have more
3130 assocparams.sasoc_asocmaxrxt > path_sum)
3133 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3136 if (assocparams.sasoc_cookie_life != 0)
3137 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3139 /* Set the values to the endpoint */
3140 struct sctp_sock *sp = sctp_sk(sk);
3142 if (assocparams.sasoc_asocmaxrxt != 0)
3143 sp->assocparams.sasoc_asocmaxrxt =
3144 assocparams.sasoc_asocmaxrxt;
3145 if (assocparams.sasoc_cookie_life != 0)
3146 sp->assocparams.sasoc_cookie_life =
3147 assocparams.sasoc_cookie_life;
3153 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3155 * This socket option is a boolean flag which turns on or off mapped V4
3156 * addresses. If this option is turned on and the socket is type
3157 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3158 * If this option is turned off, then no mapping will be done of V4
3159 * addresses and a user will receive both PF_INET6 and PF_INET type
3160 * addresses on the socket.
3162 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3165 struct sctp_sock *sp = sctp_sk(sk);
3167 if (optlen < sizeof(int))
3169 if (get_user(val, (int __user *)optval))
3180 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3181 * This option will get or set the maximum size to put in any outgoing
3182 * SCTP DATA chunk. If a message is larger than this size it will be
3183 * fragmented by SCTP into the specified size. Note that the underlying
3184 * SCTP implementation may fragment into smaller sized chunks when the
3185 * PMTU of the underlying association is smaller than the value set by
3186 * the user. The default value for this option is '0' which indicates
3187 * the user is NOT limiting fragmentation and only the PMTU will effect
3188 * SCTP's choice of DATA chunk size. Note also that values set larger
3189 * than the maximum size of an IP datagram will effectively let SCTP
3190 * control fragmentation (i.e. the same as setting this option to 0).
3192 * The following structure is used to access and modify this parameter:
3194 * struct sctp_assoc_value {
3195 * sctp_assoc_t assoc_id;
3196 * uint32_t assoc_value;
3199 * assoc_id: This parameter is ignored for one-to-one style sockets.
3200 * For one-to-many style sockets this parameter indicates which
3201 * association the user is performing an action upon. Note that if
3202 * this field's value is zero then the endpoints default value is
3203 * changed (effecting future associations only).
3204 * assoc_value: This parameter specifies the maximum size in bytes.
3206 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3208 struct sctp_sock *sp = sctp_sk(sk);
3209 struct sctp_af *af = sp->pf->af;
3210 struct sctp_assoc_value params;
3211 struct sctp_association *asoc;
3214 if (optlen == sizeof(int)) {
3215 pr_warn_ratelimited(DEPRECATED
3217 "Use of int in maxseg socket option.\n"
3218 "Use struct sctp_assoc_value instead\n",
3219 current->comm, task_pid_nr(current));
3220 if (copy_from_user(&val, optval, optlen))
3222 params.assoc_id = 0;
3223 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3224 if (copy_from_user(¶ms, optval, optlen))
3226 val = params.assoc_value;
3232 int min_len, max_len;
3234 min_len = SCTP_DEFAULT_MINSEGMENT - af->net_header_len;
3235 min_len -= af->ip_options_len(sk);
3236 min_len -= sizeof(struct sctphdr) +
3237 sizeof(struct sctp_data_chunk);
3239 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3241 if (val < min_len || val > max_len)
3245 asoc = sctp_id2assoc(sk, params.assoc_id);
3248 val = asoc->pathmtu - af->net_header_len;
3249 val -= af->ip_options_len(sk);
3250 val -= sizeof(struct sctphdr) +
3251 sctp_datachk_len(&asoc->stream);
3253 asoc->user_frag = val;
3254 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
3256 if (params.assoc_id && sctp_style(sk, UDP))
3258 sp->user_frag = val;
3266 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3268 * Requests that the peer mark the enclosed address as the association
3269 * primary. The enclosed address must be one of the association's
3270 * locally bound addresses. The following structure is used to make a
3271 * set primary request:
3273 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3274 unsigned int optlen)
3276 struct net *net = sock_net(sk);
3277 struct sctp_sock *sp;
3278 struct sctp_association *asoc = NULL;
3279 struct sctp_setpeerprim prim;
3280 struct sctp_chunk *chunk;
3286 if (!net->sctp.addip_enable)
3289 if (optlen != sizeof(struct sctp_setpeerprim))
3292 if (copy_from_user(&prim, optval, optlen))
3295 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3299 if (!asoc->peer.asconf_capable)
3302 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3305 if (!sctp_state(asoc, ESTABLISHED))
3308 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3312 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3313 return -EADDRNOTAVAIL;
3315 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3316 return -EADDRNOTAVAIL;
3318 /* Allow security module to validate address. */
3319 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3320 (struct sockaddr *)&prim.sspp_addr,
3325 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3326 chunk = sctp_make_asconf_set_prim(asoc,
3327 (union sctp_addr *)&prim.sspp_addr);
3331 err = sctp_send_asconf(asoc, chunk);