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, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
62 #include <linux/types.h>
63 #include <linux/kernel.h>
64 #include <linux/wait.h>
65 #include <linux/time.h>
67 #include <linux/capability.h>
68 #include <linux/fcntl.h>
69 #include <linux/poll.h>
70 #include <linux/init.h>
71 #include <linux/crypto.h>
72 #include <linux/slab.h>
73 #include <linux/file.h>
77 #include <net/route.h>
79 #include <net/inet_common.h>
81 #include <linux/socket.h> /* for sa_family_t */
82 #include <linux/export.h>
84 #include <net/sctp/sctp.h>
85 #include <net/sctp/sm.h>
87 /* WARNING: Please do not remove the SCTP_STATIC attribute to
88 * any of the functions below as they are used to export functions
89 * used by a project regression testsuite.
92 /* Forward declarations for internal helper functions. */
93 static int sctp_writeable(struct sock *sk);
94 static void sctp_wfree(struct sk_buff *skb);
95 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
97 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
98 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
99 static int sctp_wait_for_accept(struct sock *sk, long timeo);
100 static void sctp_wait_for_close(struct sock *sk, long timeo);
101 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
102 union sctp_addr *addr, int len);
103 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
104 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
105 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
106 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
107 static int sctp_send_asconf(struct sctp_association *asoc,
108 struct sctp_chunk *chunk);
109 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
110 static int sctp_autobind(struct sock *sk);
111 static void sctp_sock_migrate(struct sock *, struct sock *,
112 struct sctp_association *, sctp_socket_type_t);
113 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
115 extern struct kmem_cache *sctp_bucket_cachep;
116 extern long sysctl_sctp_mem[3];
117 extern int sysctl_sctp_rmem[3];
118 extern int sysctl_sctp_wmem[3];
120 static int sctp_memory_pressure;
121 static atomic_long_t sctp_memory_allocated;
122 struct percpu_counter sctp_sockets_allocated;
124 static void sctp_enter_memory_pressure(struct sock *sk)
126 sctp_memory_pressure = 1;
130 /* Get the sndbuf space available at the time on the association. */
131 static inline int sctp_wspace(struct sctp_association *asoc)
135 if (asoc->ep->sndbuf_policy)
136 amt = asoc->sndbuf_used;
138 amt = sk_wmem_alloc_get(asoc->base.sk);
140 if (amt >= asoc->base.sk->sk_sndbuf) {
141 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
144 amt = sk_stream_wspace(asoc->base.sk);
149 amt = asoc->base.sk->sk_sndbuf - amt;
154 /* Increment the used sndbuf space count of the corresponding association by
155 * the size of the outgoing data chunk.
156 * Also, set the skb destructor for sndbuf accounting later.
158 * Since it is always 1-1 between chunk and skb, and also a new skb is always
159 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
160 * destructor in the data chunk skb for the purpose of the sndbuf space
163 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
165 struct sctp_association *asoc = chunk->asoc;
166 struct sock *sk = asoc->base.sk;
168 /* The sndbuf space is tracked per association. */
169 sctp_association_hold(asoc);
171 skb_set_owner_w(chunk->skb, sk);
173 chunk->skb->destructor = sctp_wfree;
174 /* Save the chunk pointer in skb for sctp_wfree to use later. */
175 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
177 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
178 sizeof(struct sk_buff) +
179 sizeof(struct sctp_chunk);
181 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
182 sk->sk_wmem_queued += chunk->skb->truesize;
183 sk_mem_charge(sk, chunk->skb->truesize);
186 /* Verify that this is a valid address. */
187 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
192 /* Verify basic sockaddr. */
193 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
197 /* Is this a valid SCTP address? */
198 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
201 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
207 /* Look up the association by its id. If this is not a UDP-style
208 * socket, the ID field is always ignored.
210 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
212 struct sctp_association *asoc = NULL;
214 /* If this is not a UDP-style socket, assoc id should be ignored. */
215 if (!sctp_style(sk, UDP)) {
216 /* Return NULL if the socket state is not ESTABLISHED. It
217 * could be a TCP-style listening socket or a socket which
218 * hasn't yet called connect() to establish an association.
220 if (!sctp_sstate(sk, ESTABLISHED))
223 /* Get the first and the only association from the list. */
224 if (!list_empty(&sctp_sk(sk)->ep->asocs))
225 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
226 struct sctp_association, asocs);
230 /* Otherwise this is a UDP-style socket. */
231 if (!id || (id == (sctp_assoc_t)-1))
234 spin_lock_bh(&sctp_assocs_id_lock);
235 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
236 spin_unlock_bh(&sctp_assocs_id_lock);
238 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
244 /* Look up the transport from an address and an assoc id. If both address and
245 * id are specified, the associations matching the address and the id should be
248 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
249 struct sockaddr_storage *addr,
252 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
253 struct sctp_transport *transport;
254 union sctp_addr *laddr = (union sctp_addr *)addr;
256 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
263 id_asoc = sctp_id2assoc(sk, id);
264 if (id_asoc && (id_asoc != addr_asoc))
267 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
268 (union sctp_addr *)addr);
273 /* API 3.1.2 bind() - UDP Style Syntax
274 * The syntax of bind() is,
276 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
278 * sd - the socket descriptor returned by socket().
279 * addr - the address structure (struct sockaddr_in or struct
280 * sockaddr_in6 [RFC 2553]),
281 * addr_len - the size of the address structure.
283 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
289 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
292 /* Disallow binding twice. */
293 if (!sctp_sk(sk)->ep->base.bind_addr.port)
294 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
299 sctp_release_sock(sk);
304 static long sctp_get_port_local(struct sock *, union sctp_addr *);
306 /* Verify this is a valid sockaddr. */
307 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
308 union sctp_addr *addr, int len)
312 /* Check minimum size. */
313 if (len < sizeof (struct sockaddr))
316 /* V4 mapped address are really of AF_INET family */
317 if (addr->sa.sa_family == AF_INET6 &&
318 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
319 if (!opt->pf->af_supported(AF_INET, opt))
322 /* Does this PF support this AF? */
323 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
327 /* If we get this far, af is valid. */
328 af = sctp_get_af_specific(addr->sa.sa_family);
330 if (len < af->sockaddr_len)
336 /* Bind a local address either to an endpoint or to an association. */
337 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
339 struct sctp_sock *sp = sctp_sk(sk);
340 struct sctp_endpoint *ep = sp->ep;
341 struct sctp_bind_addr *bp = &ep->base.bind_addr;
346 /* Common sockaddr verification. */
347 af = sctp_sockaddr_af(sp, addr, len);
349 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
354 snum = ntohs(addr->v4.sin_port);
356 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
357 ", port: %d, new port: %d, len: %d)\n",
363 /* PF specific bind() address verification. */
364 if (!sp->pf->bind_verify(sp, addr))
365 return -EADDRNOTAVAIL;
367 /* We must either be unbound, or bind to the same port.
368 * It's OK to allow 0 ports if we are already bound.
369 * We'll just inhert an already bound port in this case
374 else if (snum != bp->port) {
375 SCTP_DEBUG_PRINTK("sctp_do_bind:"
376 " New port %d does not match existing port "
377 "%d.\n", snum, bp->port);
382 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
385 /* See if the address matches any of the addresses we may have
386 * already bound before checking against other endpoints.
388 if (sctp_bind_addr_match(bp, addr, sp))
391 /* Make sure we are allowed to bind here.
392 * The function sctp_get_port_local() does duplicate address
395 addr->v4.sin_port = htons(snum);
396 if ((ret = sctp_get_port_local(sk, addr))) {
400 /* Refresh ephemeral port. */
402 bp->port = inet_sk(sk)->inet_num;
404 /* Add the address to the bind address list.
405 * Use GFP_ATOMIC since BHs will be disabled.
407 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
409 /* Copy back into socket for getsockname() use. */
411 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
412 af->to_sk_saddr(addr, sk);
418 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
420 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
421 * at any one time. If a sender, after sending an ASCONF chunk, decides
422 * it needs to transfer another ASCONF Chunk, it MUST wait until the
423 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
424 * subsequent ASCONF. Note this restriction binds each side, so at any
425 * time two ASCONF may be in-transit on any given association (one sent
426 * from each endpoint).
428 static int sctp_send_asconf(struct sctp_association *asoc,
429 struct sctp_chunk *chunk)
431 struct net *net = sock_net(asoc->base.sk);
434 /* If there is an outstanding ASCONF chunk, queue it for later
437 if (asoc->addip_last_asconf) {
438 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
442 /* Hold the chunk until an ASCONF_ACK is received. */
443 sctp_chunk_hold(chunk);
444 retval = sctp_primitive_ASCONF(net, asoc, chunk);
446 sctp_chunk_free(chunk);
448 asoc->addip_last_asconf = chunk;
454 /* Add a list of addresses as bind addresses to local endpoint or
457 * Basically run through each address specified in the addrs/addrcnt
458 * array/length pair, determine if it is IPv6 or IPv4 and call
459 * sctp_do_bind() on it.
461 * If any of them fails, then the operation will be reversed and the
462 * ones that were added will be removed.
464 * Only sctp_setsockopt_bindx() is supposed to call this function.
466 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
471 struct sockaddr *sa_addr;
474 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
478 for (cnt = 0; cnt < addrcnt; cnt++) {
479 /* The list may contain either IPv4 or IPv6 address;
480 * determine the address length for walking thru the list.
483 af = sctp_get_af_specific(sa_addr->sa_family);
489 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
492 addr_buf += af->sockaddr_len;
496 /* Failed. Cleanup the ones that have been added */
498 sctp_bindx_rem(sk, addrs, cnt);
506 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
507 * associations that are part of the endpoint indicating that a list of local
508 * addresses are added to the endpoint.
510 * If any of the addresses is already in the bind address list of the
511 * association, we do not send the chunk for that association. But it will not
512 * affect other associations.
514 * Only sctp_setsockopt_bindx() is supposed to call this function.
516 static int sctp_send_asconf_add_ip(struct sock *sk,
517 struct sockaddr *addrs,
520 struct net *net = sock_net(sk);
521 struct sctp_sock *sp;
522 struct sctp_endpoint *ep;
523 struct sctp_association *asoc;
524 struct sctp_bind_addr *bp;
525 struct sctp_chunk *chunk;
526 struct sctp_sockaddr_entry *laddr;
527 union sctp_addr *addr;
528 union sctp_addr saveaddr;
535 if (!net->sctp.addip_enable)
541 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
542 __func__, sk, addrs, addrcnt);
544 list_for_each_entry(asoc, &ep->asocs, asocs) {
546 if (!asoc->peer.asconf_capable)
549 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
552 if (!sctp_state(asoc, ESTABLISHED))
555 /* Check if any address in the packed array of addresses is
556 * in the bind address list of the association. If so,
557 * do not send the asconf chunk to its peer, but continue with
558 * other associations.
561 for (i = 0; i < addrcnt; i++) {
563 af = sctp_get_af_specific(addr->v4.sin_family);
569 if (sctp_assoc_lookup_laddr(asoc, addr))
572 addr_buf += af->sockaddr_len;
577 /* Use the first valid address in bind addr list of
578 * association as Address Parameter of ASCONF CHUNK.
580 bp = &asoc->base.bind_addr;
581 p = bp->address_list.next;
582 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
583 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
584 addrcnt, SCTP_PARAM_ADD_IP);
590 /* Add the new addresses to the bind address list with
591 * use_as_src set to 0.
594 for (i = 0; i < addrcnt; i++) {
596 af = sctp_get_af_specific(addr->v4.sin_family);
597 memcpy(&saveaddr, addr, af->sockaddr_len);
598 retval = sctp_add_bind_addr(bp, &saveaddr,
599 SCTP_ADDR_NEW, GFP_ATOMIC);
600 addr_buf += af->sockaddr_len;
602 if (asoc->src_out_of_asoc_ok) {
603 struct sctp_transport *trans;
605 list_for_each_entry(trans,
606 &asoc->peer.transport_addr_list, transports) {
607 /* Clear the source and route cache */
608 dst_release(trans->dst);
609 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
610 2*asoc->pathmtu, 4380));
611 trans->ssthresh = asoc->peer.i.a_rwnd;
612 trans->rto = asoc->rto_initial;
613 trans->rtt = trans->srtt = trans->rttvar = 0;
614 sctp_transport_route(trans, NULL,
615 sctp_sk(asoc->base.sk));
618 retval = sctp_send_asconf(asoc, chunk);
625 /* Remove a list of addresses from bind addresses list. Do not remove the
628 * Basically run through each address specified in the addrs/addrcnt
629 * array/length pair, determine if it is IPv6 or IPv4 and call
630 * sctp_del_bind() on it.
632 * If any of them fails, then the operation will be reversed and the
633 * ones that were removed will be added back.
635 * At least one address has to be left; if only one address is
636 * available, the operation will return -EBUSY.
638 * Only sctp_setsockopt_bindx() is supposed to call this function.
640 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
642 struct sctp_sock *sp = sctp_sk(sk);
643 struct sctp_endpoint *ep = sp->ep;
645 struct sctp_bind_addr *bp = &ep->base.bind_addr;
648 union sctp_addr *sa_addr;
651 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
655 for (cnt = 0; cnt < addrcnt; cnt++) {
656 /* If the bind address list is empty or if there is only one
657 * bind address, there is nothing more to be removed (we need
658 * at least one address here).
660 if (list_empty(&bp->address_list) ||
661 (sctp_list_single_entry(&bp->address_list))) {
667 af = sctp_get_af_specific(sa_addr->sa.sa_family);
673 if (!af->addr_valid(sa_addr, sp, NULL)) {
674 retval = -EADDRNOTAVAIL;
678 if (sa_addr->v4.sin_port &&
679 sa_addr->v4.sin_port != htons(bp->port)) {
684 if (!sa_addr->v4.sin_port)
685 sa_addr->v4.sin_port = htons(bp->port);
687 /* FIXME - There is probably a need to check if sk->sk_saddr and
688 * sk->sk_rcv_addr are currently set to one of the addresses to
689 * be removed. This is something which needs to be looked into
690 * when we are fixing the outstanding issues with multi-homing
691 * socket routing and failover schemes. Refer to comments in
692 * sctp_do_bind(). -daisy
694 retval = sctp_del_bind_addr(bp, sa_addr);
696 addr_buf += af->sockaddr_len;
699 /* Failed. Add the ones that has been removed back */
701 sctp_bindx_add(sk, addrs, cnt);
709 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
710 * the associations that are part of the endpoint indicating that a list of
711 * local addresses are removed from the endpoint.
713 * If any of the addresses is already in the bind address list of the
714 * association, we do not send the chunk for that association. But it will not
715 * affect other associations.
717 * Only sctp_setsockopt_bindx() is supposed to call this function.
719 static int sctp_send_asconf_del_ip(struct sock *sk,
720 struct sockaddr *addrs,
723 struct net *net = sock_net(sk);
724 struct sctp_sock *sp;
725 struct sctp_endpoint *ep;
726 struct sctp_association *asoc;
727 struct sctp_transport *transport;
728 struct sctp_bind_addr *bp;
729 struct sctp_chunk *chunk;
730 union sctp_addr *laddr;
733 struct sctp_sockaddr_entry *saddr;
739 if (!net->sctp.addip_enable)
745 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
746 __func__, sk, addrs, addrcnt);
748 list_for_each_entry(asoc, &ep->asocs, asocs) {
750 if (!asoc->peer.asconf_capable)
753 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
756 if (!sctp_state(asoc, ESTABLISHED))
759 /* Check if any address in the packed array of addresses is
760 * not present in the bind address list of the association.
761 * If so, do not send the asconf chunk to its peer, but
762 * continue with other associations.
765 for (i = 0; i < addrcnt; i++) {
767 af = sctp_get_af_specific(laddr->v4.sin_family);
773 if (!sctp_assoc_lookup_laddr(asoc, laddr))
776 addr_buf += af->sockaddr_len;
781 /* Find one address in the association's bind address list
782 * that is not in the packed array of addresses. This is to
783 * make sure that we do not delete all the addresses in the
786 bp = &asoc->base.bind_addr;
787 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
789 if ((laddr == NULL) && (addrcnt == 1)) {
790 if (asoc->asconf_addr_del_pending)
792 asoc->asconf_addr_del_pending =
793 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
794 if (asoc->asconf_addr_del_pending == NULL) {
798 asoc->asconf_addr_del_pending->sa.sa_family =
800 asoc->asconf_addr_del_pending->v4.sin_port =
802 if (addrs->sa_family == AF_INET) {
803 struct sockaddr_in *sin;
805 sin = (struct sockaddr_in *)addrs;
806 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
807 } else if (addrs->sa_family == AF_INET6) {
808 struct sockaddr_in6 *sin6;
810 sin6 = (struct sockaddr_in6 *)addrs;
811 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
813 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
814 " at %p\n", asoc, asoc->asconf_addr_del_pending,
815 asoc->asconf_addr_del_pending);
816 asoc->src_out_of_asoc_ok = 1;
821 /* We do not need RCU protection throughout this loop
822 * because this is done under a socket lock from the
825 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
833 /* Reset use_as_src flag for the addresses in the bind address
834 * list that are to be deleted.
837 for (i = 0; i < addrcnt; i++) {
839 af = sctp_get_af_specific(laddr->v4.sin_family);
840 list_for_each_entry(saddr, &bp->address_list, list) {
841 if (sctp_cmp_addr_exact(&saddr->a, laddr))
842 saddr->state = SCTP_ADDR_DEL;
844 addr_buf += af->sockaddr_len;
847 /* Update the route and saddr entries for all the transports
848 * as some of the addresses in the bind address list are
849 * about to be deleted and cannot be used as source addresses.
851 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
853 dst_release(transport->dst);
854 sctp_transport_route(transport, NULL,
855 sctp_sk(asoc->base.sk));
859 /* We don't need to transmit ASCONF */
861 retval = sctp_send_asconf(asoc, chunk);
867 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
868 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
870 struct sock *sk = sctp_opt2sk(sp);
871 union sctp_addr *addr;
874 /* It is safe to write port space in caller. */
876 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
877 af = sctp_get_af_specific(addr->sa.sa_family);
880 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
883 if (addrw->state == SCTP_ADDR_NEW)
884 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
886 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
889 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
892 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
895 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
896 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
899 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
900 * Section 3.1.2 for this usage.
902 * addrs is a pointer to an array of one or more socket addresses. Each
903 * address is contained in its appropriate structure (i.e. struct
904 * sockaddr_in or struct sockaddr_in6) the family of the address type
905 * must be used to distinguish the address length (note that this
906 * representation is termed a "packed array" of addresses). The caller
907 * specifies the number of addresses in the array with addrcnt.
909 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
910 * -1, and sets errno to the appropriate error code.
912 * For SCTP, the port given in each socket address must be the same, or
913 * sctp_bindx() will fail, setting errno to EINVAL.
915 * The flags parameter is formed from the bitwise OR of zero or more of
916 * the following currently defined flags:
918 * SCTP_BINDX_ADD_ADDR
920 * SCTP_BINDX_REM_ADDR
922 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
923 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
924 * addresses from the association. The two flags are mutually exclusive;
925 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
926 * not remove all addresses from an association; sctp_bindx() will
927 * reject such an attempt with EINVAL.
929 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
930 * additional addresses with an endpoint after calling bind(). Or use
931 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
932 * socket is associated with so that no new association accepted will be
933 * associated with those addresses. If the endpoint supports dynamic
934 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
935 * endpoint to send the appropriate message to the peer to change the
936 * peers address lists.
938 * Adding and removing addresses from a connected association is
939 * optional functionality. Implementations that do not support this
940 * functionality should return EOPNOTSUPP.
942 * Basically do nothing but copying the addresses from user to kernel
943 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
944 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
947 * We don't use copy_from_user() for optimization: we first do the
948 * sanity checks (buffer size -fast- and access check-healthy
949 * pointer); if all of those succeed, then we can alloc the memory
950 * (expensive operation) needed to copy the data to kernel. Then we do
951 * the copying without checking the user space area
952 * (__copy_from_user()).
954 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
957 * sk The sk of the socket
958 * addrs The pointer to the addresses in user land
959 * addrssize Size of the addrs buffer
960 * op Operation to perform (add or remove, see the flags of
963 * Returns 0 if ok, <0 errno code on error.
965 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
966 struct sockaddr __user *addrs,
967 int addrs_size, int op)
969 struct sockaddr *kaddrs;
973 struct sockaddr *sa_addr;
977 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
978 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
980 if (unlikely(addrs_size <= 0))
983 /* Check the user passed a healthy pointer. */
984 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
987 /* Alloc space for the address array in kernel memory. */
988 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
989 if (unlikely(!kaddrs))
992 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
997 /* Walk through the addrs buffer and count the number of addresses. */
999 while (walk_size < addrs_size) {
1000 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1006 af = sctp_get_af_specific(sa_addr->sa_family);
1008 /* If the address family is not supported or if this address
1009 * causes the address buffer to overflow return EINVAL.
1011 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1016 addr_buf += af->sockaddr_len;
1017 walk_size += af->sockaddr_len;
1022 case SCTP_BINDX_ADD_ADDR:
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,
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;
1070 union sctp_addr *sa_addr = NULL;
1072 unsigned short port;
1073 unsigned int f_flags = 0;
1078 /* connect() cannot be done on a socket that is already in ESTABLISHED
1079 * state - UDP-style peeled off socket or a TCP-style socket that
1080 * is already connected.
1081 * It cannot be done even on a TCP-style listening socket.
1083 if (sctp_sstate(sk, ESTABLISHED) ||
1084 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1089 /* Walk through the addrs buffer and count the number of addresses. */
1091 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)
1124 /* Check if there already is a matching association on the
1125 * endpoint (other than the one created here).
1127 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1128 if (asoc2 && asoc2 != asoc) {
1129 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1136 /* If we could not find a matching association on the endpoint,
1137 * make sure that there is no peeled-off association matching
1138 * the peer address even on another socket.
1140 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1141 err = -EADDRNOTAVAIL;
1146 /* If a bind() or sctp_bindx() is not called prior to
1147 * an sctp_connectx() call, the system picks an
1148 * ephemeral port and will choose an address set
1149 * equivalent to binding with a wildcard address.
1151 if (!ep->base.bind_addr.port) {
1152 if (sctp_autobind(sk)) {
1158 * If an unprivileged user inherits a 1-many
1159 * style socket with open associations on a
1160 * privileged port, it MAY be permitted to
1161 * accept new associations, but it SHOULD NOT
1162 * be permitted to open new associations.
1164 if (ep->base.bind_addr.port < PROT_SOCK &&
1165 !capable(CAP_NET_BIND_SERVICE)) {
1171 scope = sctp_scope(&to);
1172 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1178 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1186 /* Prime the peer's transport structures. */
1187 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1195 addr_buf += af->sockaddr_len;
1196 walk_size += af->sockaddr_len;
1199 /* In case the user of sctp_connectx() wants an association
1200 * id back, assign one now.
1203 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1208 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1213 /* Initialize sk's dport and daddr for getpeername() */
1214 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1215 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1216 af->to_sk_daddr(sa_addr, sk);
1219 /* in-kernel sockets don't generally have a file allocated to them
1220 * if all they do is call sock_create_kern().
1222 if (sk->sk_socket->file)
1223 f_flags = sk->sk_socket->file->f_flags;
1225 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1227 err = sctp_wait_for_connect(asoc, &timeo);
1228 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
1229 *assoc_id = asoc->assoc_id;
1231 /* Don't free association on exit. */
1236 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1237 " kaddrs: %p err: %d\n",
1240 /* sctp_primitive_ASSOCIATE may have added this association
1241 * To the hash table, try to unhash it, just in case, its a noop
1242 * if it wasn't hashed so we're safe
1244 sctp_unhash_established(asoc);
1245 sctp_association_free(asoc);
1250 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1253 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1254 * sctp_assoc_t *asoc);
1256 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1257 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1258 * or IPv6 addresses.
1260 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1261 * Section 3.1.2 for this usage.
1263 * addrs is a pointer to an array of one or more socket addresses. Each
1264 * address is contained in its appropriate structure (i.e. struct
1265 * sockaddr_in or struct sockaddr_in6) the family of the address type
1266 * must be used to distengish the address length (note that this
1267 * representation is termed a "packed array" of addresses). The caller
1268 * specifies the number of addresses in the array with addrcnt.
1270 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1271 * the association id of the new association. On failure, sctp_connectx()
1272 * returns -1, and sets errno to the appropriate error code. The assoc_id
1273 * is not touched by the kernel.
1275 * For SCTP, the port given in each socket address must be the same, or
1276 * sctp_connectx() will fail, setting errno to EINVAL.
1278 * An application can use sctp_connectx to initiate an association with
1279 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1280 * allows a caller to specify multiple addresses at which a peer can be
1281 * reached. The way the SCTP stack uses the list of addresses to set up
1282 * the association is implementation dependent. This function only
1283 * specifies that the stack will try to make use of all the addresses in
1284 * the list when needed.
1286 * Note that the list of addresses passed in is only used for setting up
1287 * the association. It does not necessarily equal the set of addresses
1288 * the peer uses for the resulting association. If the caller wants to
1289 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1290 * retrieve them after the association has been set up.
1292 * Basically do nothing but copying the addresses from user to kernel
1293 * land and invoking either sctp_connectx(). This is used for tunneling
1294 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1296 * We don't use copy_from_user() for optimization: we first do the
1297 * sanity checks (buffer size -fast- and access check-healthy
1298 * pointer); if all of those succeed, then we can alloc the memory
1299 * (expensive operation) needed to copy the data to kernel. Then we do
1300 * the copying without checking the user space area
1301 * (__copy_from_user()).
1303 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1306 * sk The sk of the socket
1307 * addrs The pointer to the addresses in user land
1308 * addrssize Size of the addrs buffer
1310 * Returns >=0 if ok, <0 errno code on error.
1312 SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
1313 struct sockaddr __user *addrs,
1315 sctp_assoc_t *assoc_id)
1318 struct sockaddr *kaddrs;
1320 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1321 __func__, sk, addrs, addrs_size);
1323 if (unlikely(addrs_size <= 0))
1326 /* Check the user passed a healthy pointer. */
1327 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1330 /* Alloc space for the address array in kernel memory. */
1331 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1332 if (unlikely(!kaddrs))
1335 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1338 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1347 * This is an older interface. It's kept for backward compatibility
1348 * to the option that doesn't provide association id.
1350 SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1351 struct sockaddr __user *addrs,
1354 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1358 * New interface for the API. The since the API is done with a socket
1359 * option, to make it simple we feed back the association id is as a return
1360 * indication to the call. Error is always negative and association id is
1363 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1364 struct sockaddr __user *addrs,
1367 sctp_assoc_t assoc_id = 0;
1370 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1379 * New (hopefully final) interface for the API.
1380 * We use the sctp_getaddrs_old structure so that use-space library
1381 * can avoid any unnecessary allocations. The only defferent part
1382 * is that we store the actual length of the address buffer into the
1383 * addrs_num structure member. That way we can re-use the existing
1386 SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1387 char __user *optval,
1390 struct sctp_getaddrs_old param;
1391 sctp_assoc_t assoc_id = 0;
1394 if (len < sizeof(param))
1397 if (copy_from_user(¶m, optval, sizeof(param)))
1400 err = __sctp_setsockopt_connectx(sk,
1401 (struct sockaddr __user *)param.addrs,
1402 param.addr_num, &assoc_id);
1404 if (err == 0 || err == -EINPROGRESS) {
1405 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1407 if (put_user(sizeof(assoc_id), optlen))
1414 /* API 3.1.4 close() - UDP Style Syntax
1415 * Applications use close() to perform graceful shutdown (as described in
1416 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1417 * by a UDP-style socket.
1421 * ret = close(int sd);
1423 * sd - the socket descriptor of the associations to be closed.
1425 * To gracefully shutdown a specific association represented by the
1426 * UDP-style socket, an application should use the sendmsg() call,
1427 * passing no user data, but including the appropriate flag in the
1428 * ancillary data (see Section xxxx).
1430 * If sd in the close() call is a branched-off socket representing only
1431 * one association, the shutdown is performed on that association only.
1433 * 4.1.6 close() - TCP Style Syntax
1435 * Applications use close() to gracefully close down an association.
1439 * int close(int sd);
1441 * sd - the socket descriptor of the association to be closed.
1443 * After an application calls close() on a socket descriptor, no further
1444 * socket operations will succeed on that descriptor.
1446 * API 7.1.4 SO_LINGER
1448 * An application using the TCP-style socket can use this option to
1449 * perform the SCTP ABORT primitive. The linger option structure is:
1452 * int l_onoff; // option on/off
1453 * int l_linger; // linger time
1456 * To enable the option, set l_onoff to 1. If the l_linger value is set
1457 * to 0, calling close() is the same as the ABORT primitive. If the
1458 * value is set to a negative value, the setsockopt() call will return
1459 * an error. If the value is set to a positive value linger_time, the
1460 * close() can be blocked for at most linger_time ms. If the graceful
1461 * shutdown phase does not finish during this period, close() will
1462 * return but the graceful shutdown phase continues in the system.
1464 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1466 struct net *net = sock_net(sk);
1467 struct sctp_endpoint *ep;
1468 struct sctp_association *asoc;
1469 struct list_head *pos, *temp;
1470 unsigned int data_was_unread;
1472 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1475 sk->sk_shutdown = SHUTDOWN_MASK;
1476 sk->sk_state = SCTP_SS_CLOSING;
1478 ep = sctp_sk(sk)->ep;
1480 /* Clean up any skbs sitting on the receive queue. */
1481 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1482 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1484 /* Walk all associations on an endpoint. */
1485 list_for_each_safe(pos, temp, &ep->asocs) {
1486 asoc = list_entry(pos, struct sctp_association, asocs);
1488 if (sctp_style(sk, TCP)) {
1489 /* A closed association can still be in the list if
1490 * it belongs to a TCP-style listening socket that is
1491 * not yet accepted. If so, free it. If not, send an
1492 * ABORT or SHUTDOWN based on the linger options.
1494 if (sctp_state(asoc, CLOSED)) {
1495 sctp_unhash_established(asoc);
1496 sctp_association_free(asoc);
1501 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1502 !skb_queue_empty(&asoc->ulpq.reasm) ||
1503 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1504 struct sctp_chunk *chunk;
1506 chunk = sctp_make_abort_user(asoc, NULL, 0);
1508 sctp_primitive_ABORT(net, asoc, chunk);
1510 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1513 /* On a TCP-style socket, block for at most linger_time if set. */
1514 if (sctp_style(sk, TCP) && timeout)
1515 sctp_wait_for_close(sk, timeout);
1517 /* This will run the backlog queue. */
1518 sctp_release_sock(sk);
1520 /* Supposedly, no process has access to the socket, but
1521 * the net layers still may.
1523 sctp_local_bh_disable();
1524 sctp_bh_lock_sock(sk);
1526 /* Hold the sock, since sk_common_release() will put sock_put()
1527 * and we have just a little more cleanup.
1530 sk_common_release(sk);
1532 sctp_bh_unlock_sock(sk);
1533 sctp_local_bh_enable();
1537 SCTP_DBG_OBJCNT_DEC(sock);
1540 /* Handle EPIPE error. */
1541 static int sctp_error(struct sock *sk, int flags, int err)
1544 err = sock_error(sk) ? : -EPIPE;
1545 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1546 send_sig(SIGPIPE, current, 0);
1550 /* API 3.1.3 sendmsg() - UDP Style Syntax
1552 * An application uses sendmsg() and recvmsg() calls to transmit data to
1553 * and receive data from its peer.
1555 * ssize_t sendmsg(int socket, const struct msghdr *message,
1558 * socket - the socket descriptor of the endpoint.
1559 * message - pointer to the msghdr structure which contains a single
1560 * user message and possibly some ancillary data.
1562 * See Section 5 for complete description of the data
1565 * flags - flags sent or received with the user message, see Section
1566 * 5 for complete description of the flags.
1568 * Note: This function could use a rewrite especially when explicit
1569 * connect support comes in.
1571 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1573 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1575 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1576 struct msghdr *msg, size_t msg_len)
1578 struct net *net = sock_net(sk);
1579 struct sctp_sock *sp;
1580 struct sctp_endpoint *ep;
1581 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1582 struct sctp_transport *transport, *chunk_tp;
1583 struct sctp_chunk *chunk;
1585 struct sockaddr *msg_name = NULL;
1586 struct sctp_sndrcvinfo default_sinfo;
1587 struct sctp_sndrcvinfo *sinfo;
1588 struct sctp_initmsg *sinit;
1589 sctp_assoc_t associd = 0;
1590 sctp_cmsgs_t cmsgs = { NULL };
1594 __u16 sinfo_flags = 0;
1595 struct sctp_datamsg *datamsg;
1596 int msg_flags = msg->msg_flags;
1598 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1605 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1607 /* We cannot send a message over a TCP-style listening socket. */
1608 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1613 /* Parse out the SCTP CMSGs. */
1614 err = sctp_msghdr_parse(msg, &cmsgs);
1617 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1621 /* Fetch the destination address for this packet. This
1622 * address only selects the association--it is not necessarily
1623 * the address we will send to.
1624 * For a peeled-off socket, msg_name is ignored.
1626 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1627 int msg_namelen = msg->msg_namelen;
1629 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1634 if (msg_namelen > sizeof(to))
1635 msg_namelen = sizeof(to);
1636 memcpy(&to, msg->msg_name, msg_namelen);
1637 msg_name = msg->msg_name;
1643 /* Did the user specify SNDRCVINFO? */
1645 sinfo_flags = sinfo->sinfo_flags;
1646 associd = sinfo->sinfo_assoc_id;
1649 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1650 msg_len, sinfo_flags);
1652 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1653 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1658 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1659 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1660 * If SCTP_ABORT is set, the message length could be non zero with
1661 * the msg_iov set to the user abort reason.
1663 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1664 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1669 /* If SCTP_ADDR_OVER is set, there must be an address
1670 * specified in msg_name.
1672 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1679 SCTP_DEBUG_PRINTK("About to look up association.\n");
1683 /* If a msg_name has been specified, assume this is to be used. */
1685 /* Look for a matching association on the endpoint. */
1686 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1688 /* If we could not find a matching association on the
1689 * endpoint, make sure that it is not a TCP-style
1690 * socket that already has an association or there is
1691 * no peeled-off association on another socket.
1693 if ((sctp_style(sk, TCP) &&
1694 sctp_sstate(sk, ESTABLISHED)) ||
1695 sctp_endpoint_is_peeled_off(ep, &to)) {
1696 err = -EADDRNOTAVAIL;
1701 asoc = sctp_id2assoc(sk, associd);
1709 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1711 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1712 * socket that has an association in CLOSED state. This can
1713 * happen when an accepted socket has an association that is
1716 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1721 if (sinfo_flags & SCTP_EOF) {
1722 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1724 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1728 if (sinfo_flags & SCTP_ABORT) {
1730 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1736 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1737 sctp_primitive_ABORT(net, asoc, chunk);
1743 /* Do we need to create the association? */
1745 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1747 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1752 /* Check for invalid stream against the stream counts,
1753 * either the default or the user specified stream counts.
1756 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1757 /* Check against the defaults. */
1758 if (sinfo->sinfo_stream >=
1759 sp->initmsg.sinit_num_ostreams) {
1764 /* Check against the requested. */
1765 if (sinfo->sinfo_stream >=
1766 sinit->sinit_num_ostreams) {
1774 * API 3.1.2 bind() - UDP Style Syntax
1775 * If a bind() or sctp_bindx() is not called prior to a
1776 * sendmsg() call that initiates a new association, the
1777 * system picks an ephemeral port and will choose an address
1778 * set equivalent to binding with a wildcard address.
1780 if (!ep->base.bind_addr.port) {
1781 if (sctp_autobind(sk)) {
1787 * If an unprivileged user inherits a one-to-many
1788 * style socket with open associations on a privileged
1789 * port, it MAY be permitted to accept new associations,
1790 * but it SHOULD NOT be permitted to open new
1793 if (ep->base.bind_addr.port < PROT_SOCK &&
1794 !capable(CAP_NET_BIND_SERVICE)) {
1800 scope = sctp_scope(&to);
1801 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1807 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1813 /* If the SCTP_INIT ancillary data is specified, set all
1814 * the association init values accordingly.
1817 if (sinit->sinit_num_ostreams) {
1818 asoc->c.sinit_num_ostreams =
1819 sinit->sinit_num_ostreams;
1821 if (sinit->sinit_max_instreams) {
1822 asoc->c.sinit_max_instreams =
1823 sinit->sinit_max_instreams;
1825 if (sinit->sinit_max_attempts) {
1826 asoc->max_init_attempts
1827 = sinit->sinit_max_attempts;
1829 if (sinit->sinit_max_init_timeo) {
1830 asoc->max_init_timeo =
1831 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1835 /* Prime the peer's transport structures. */
1836 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1843 /* ASSERT: we have a valid association at this point. */
1844 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1847 /* If the user didn't specify SNDRCVINFO, make up one with
1850 memset(&default_sinfo, 0, sizeof(default_sinfo));
1851 default_sinfo.sinfo_stream = asoc->default_stream;
1852 default_sinfo.sinfo_flags = asoc->default_flags;
1853 default_sinfo.sinfo_ppid = asoc->default_ppid;
1854 default_sinfo.sinfo_context = asoc->default_context;
1855 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1856 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1857 sinfo = &default_sinfo;
1860 /* API 7.1.7, the sndbuf size per association bounds the
1861 * maximum size of data that can be sent in a single send call.
1863 if (msg_len > sk->sk_sndbuf) {
1868 if (asoc->pmtu_pending)
1869 sctp_assoc_pending_pmtu(sk, asoc);
1871 /* If fragmentation is disabled and the message length exceeds the
1872 * association fragmentation point, return EMSGSIZE. The I-D
1873 * does not specify what this error is, but this looks like
1876 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1881 /* Check for invalid stream. */
1882 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1887 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1888 if (!sctp_wspace(asoc)) {
1889 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1894 /* If an address is passed with the sendto/sendmsg call, it is used
1895 * to override the primary destination address in the TCP model, or
1896 * when SCTP_ADDR_OVER flag is set in the UDP model.
1898 if ((sctp_style(sk, TCP) && msg_name) ||
1899 (sinfo_flags & SCTP_ADDR_OVER)) {
1900 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1908 /* Auto-connect, if we aren't connected already. */
1909 if (sctp_state(asoc, CLOSED)) {
1910 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1913 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1916 /* Break the message into multiple chunks of maximum size. */
1917 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1923 /* Now send the (possibly) fragmented message. */
1924 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1925 sctp_chunk_hold(chunk);
1927 /* Do accounting for the write space. */
1928 sctp_set_owner_w(chunk);
1930 chunk->transport = chunk_tp;
1933 /* Send it to the lower layers. Note: all chunks
1934 * must either fail or succeed. The lower layer
1935 * works that way today. Keep it that way or this
1938 err = sctp_primitive_SEND(net, asoc, datamsg);
1939 /* Did the lower layer accept the chunk? */
1941 sctp_datamsg_free(datamsg);
1943 sctp_datamsg_put(datamsg);
1945 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1952 /* If we are already past ASSOCIATE, the lower
1953 * layers are responsible for association cleanup.
1959 sctp_unhash_established(asoc);
1960 sctp_association_free(asoc);
1963 sctp_release_sock(sk);
1966 return sctp_error(sk, msg_flags, err);
1973 err = sock_error(sk);
1983 /* This is an extended version of skb_pull() that removes the data from the
1984 * start of a skb even when data is spread across the list of skb's in the
1985 * frag_list. len specifies the total amount of data that needs to be removed.
1986 * when 'len' bytes could be removed from the skb, it returns 0.
1987 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1988 * could not be removed.
1990 static int sctp_skb_pull(struct sk_buff *skb, int len)
1992 struct sk_buff *list;
1993 int skb_len = skb_headlen(skb);
1996 if (len <= skb_len) {
1997 __skb_pull(skb, len);
2001 __skb_pull(skb, skb_len);
2003 skb_walk_frags(skb, list) {
2004 rlen = sctp_skb_pull(list, len);
2005 skb->len -= (len-rlen);
2006 skb->data_len -= (len-rlen);
2017 /* API 3.1.3 recvmsg() - UDP Style Syntax
2019 * ssize_t recvmsg(int socket, struct msghdr *message,
2022 * socket - the socket descriptor of the endpoint.
2023 * message - pointer to the msghdr structure which contains a single
2024 * user message and possibly some ancillary data.
2026 * See Section 5 for complete description of the data
2029 * flags - flags sent or received with the user message, see Section
2030 * 5 for complete description of the flags.
2032 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2034 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2035 struct msghdr *msg, size_t len, int noblock,
2036 int flags, int *addr_len)
2038 struct sctp_ulpevent *event = NULL;
2039 struct sctp_sock *sp = sctp_sk(sk);
2040 struct sk_buff *skb;
2045 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2046 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2047 "len", len, "knoblauch", noblock,
2048 "flags", flags, "addr_len", addr_len);
2052 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2057 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2061 /* Get the total length of the skb including any skb's in the
2070 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2072 event = sctp_skb2event(skb);
2077 sock_recv_ts_and_drops(msg, sk, skb);
2078 if (sctp_ulpevent_is_notification(event)) {
2079 msg->msg_flags |= MSG_NOTIFICATION;
2080 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2082 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2085 /* Check if we allow SCTP_SNDRCVINFO. */
2086 if (sp->subscribe.sctp_data_io_event)
2087 sctp_ulpevent_read_sndrcvinfo(event, msg);
2089 /* FIXME: we should be calling IP/IPv6 layers. */
2090 if (sk->sk_protinfo.af_inet.cmsg_flags)
2091 ip_cmsg_recv(msg, skb);
2096 /* If skb's length exceeds the user's buffer, update the skb and
2097 * push it back to the receive_queue so that the next call to
2098 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2100 if (skb_len > copied) {
2101 msg->msg_flags &= ~MSG_EOR;
2102 if (flags & MSG_PEEK)
2104 sctp_skb_pull(skb, copied);
2105 skb_queue_head(&sk->sk_receive_queue, skb);
2107 /* When only partial message is copied to the user, increase
2108 * rwnd by that amount. If all the data in the skb is read,
2109 * rwnd is updated when the event is freed.
2111 if (!sctp_ulpevent_is_notification(event))
2112 sctp_assoc_rwnd_increase(event->asoc, copied);
2114 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2115 (event->msg_flags & MSG_EOR))
2116 msg->msg_flags |= MSG_EOR;
2118 msg->msg_flags &= ~MSG_EOR;
2121 if (flags & MSG_PEEK) {
2122 /* Release the skb reference acquired after peeking the skb in
2123 * sctp_skb_recv_datagram().
2127 /* Free the event which includes releasing the reference to
2128 * the owner of the skb, freeing the skb and updating the
2131 sctp_ulpevent_free(event);
2134 sctp_release_sock(sk);
2138 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2140 * This option is a on/off flag. If enabled no SCTP message
2141 * fragmentation will be performed. Instead if a message being sent
2142 * exceeds the current PMTU size, the message will NOT be sent and
2143 * instead a error will be indicated to the user.
2145 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2146 char __user *optval,
2147 unsigned int optlen)
2151 if (optlen < sizeof(int))
2154 if (get_user(val, (int __user *)optval))
2157 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2162 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2163 unsigned int optlen)
2165 struct sctp_association *asoc;
2166 struct sctp_ulpevent *event;
2168 if (optlen > sizeof(struct sctp_event_subscribe))
2170 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2174 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2175 * if there is no data to be sent or retransmit, the stack will
2176 * immediately send up this notification.
2178 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2179 &sctp_sk(sk)->subscribe)) {
2180 asoc = sctp_id2assoc(sk, 0);
2182 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2183 event = sctp_ulpevent_make_sender_dry_event(asoc,
2188 sctp_ulpq_tail_event(&asoc->ulpq, event);
2195 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2197 * This socket option is applicable to the UDP-style socket only. When
2198 * set it will cause associations that are idle for more than the
2199 * specified number of seconds to automatically close. An association
2200 * being idle is defined an association that has NOT sent or received
2201 * user data. The special value of '0' indicates that no automatic
2202 * close of any associations should be performed. The option expects an
2203 * integer defining the number of seconds of idle time before an
2204 * association is closed.
2206 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2207 unsigned int optlen)
2209 struct sctp_sock *sp = sctp_sk(sk);
2211 /* Applicable to UDP-style socket only */
2212 if (sctp_style(sk, TCP))
2214 if (optlen != sizeof(int))
2216 if (copy_from_user(&sp->autoclose, optval, optlen))
2222 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2224 * Applications can enable or disable heartbeats for any peer address of
2225 * an association, modify an address's heartbeat interval, force a
2226 * heartbeat to be sent immediately, and adjust the address's maximum
2227 * number of retransmissions sent before an address is considered
2228 * unreachable. The following structure is used to access and modify an
2229 * address's parameters:
2231 * struct sctp_paddrparams {
2232 * sctp_assoc_t spp_assoc_id;
2233 * struct sockaddr_storage spp_address;
2234 * uint32_t spp_hbinterval;
2235 * uint16_t spp_pathmaxrxt;
2236 * uint32_t spp_pathmtu;
2237 * uint32_t spp_sackdelay;
2238 * uint32_t spp_flags;
2241 * spp_assoc_id - (one-to-many style socket) This is filled in the
2242 * application, and identifies the association for
2244 * spp_address - This specifies which address is of interest.
2245 * spp_hbinterval - This contains the value of the heartbeat interval,
2246 * in milliseconds. If a value of zero
2247 * is present in this field then no changes are to
2248 * be made to this parameter.
2249 * spp_pathmaxrxt - This contains the maximum number of
2250 * retransmissions before this address shall be
2251 * considered unreachable. If a value of zero
2252 * is present in this field then no changes are to
2253 * be made to this parameter.
2254 * spp_pathmtu - When Path MTU discovery is disabled the value
2255 * specified here will be the "fixed" path mtu.
2256 * Note that if the spp_address field is empty
2257 * then all associations on this address will
2258 * have this fixed path mtu set upon them.
2260 * spp_sackdelay - When delayed sack is enabled, this value specifies
2261 * the number of milliseconds that sacks will be delayed
2262 * for. This value will apply to all addresses of an
2263 * association if the spp_address field is empty. Note
2264 * also, that if delayed sack is enabled and this
2265 * value is set to 0, no change is made to the last
2266 * recorded delayed sack timer value.
2268 * spp_flags - These flags are used to control various features
2269 * on an association. The flag field may contain
2270 * zero or more of the following options.
2272 * SPP_HB_ENABLE - Enable heartbeats on the
2273 * specified address. Note that if the address
2274 * field is empty all addresses for the association
2275 * have heartbeats enabled upon them.
2277 * SPP_HB_DISABLE - Disable heartbeats on the
2278 * speicifed address. Note that if the address
2279 * field is empty all addresses for the association
2280 * will have their heartbeats disabled. Note also
2281 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2282 * mutually exclusive, only one of these two should
2283 * be specified. Enabling both fields will have
2284 * undetermined results.
2286 * SPP_HB_DEMAND - Request a user initiated heartbeat
2287 * to be made immediately.
2289 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2290 * heartbeat delayis to be set to the value of 0
2293 * SPP_PMTUD_ENABLE - This field will enable PMTU
2294 * discovery upon the specified address. Note that
2295 * if the address feild is empty then all addresses
2296 * on the association are effected.
2298 * SPP_PMTUD_DISABLE - This field will disable PMTU
2299 * discovery upon the specified address. Note that
2300 * if the address feild is empty then all addresses
2301 * on the association are effected. Not also that
2302 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2303 * exclusive. Enabling both will have undetermined
2306 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2307 * on delayed sack. The time specified in spp_sackdelay
2308 * is used to specify the sack delay for this address. Note
2309 * that if spp_address is empty then all addresses will
2310 * enable delayed sack and take on the sack delay
2311 * value specified in spp_sackdelay.
2312 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2313 * off delayed sack. If the spp_address field is blank then
2314 * delayed sack is disabled for the entire association. Note
2315 * also that this field is mutually exclusive to
2316 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2319 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2320 struct sctp_transport *trans,
2321 struct sctp_association *asoc,
2322 struct sctp_sock *sp,
2325 int sackdelay_change)
2329 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2330 struct net *net = sock_net(trans->asoc->base.sk);
2332 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2337 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2338 * this field is ignored. Note also that a value of zero indicates
2339 * the current setting should be left unchanged.
2341 if (params->spp_flags & SPP_HB_ENABLE) {
2343 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2344 * set. This lets us use 0 value when this flag
2347 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2348 params->spp_hbinterval = 0;
2350 if (params->spp_hbinterval ||
2351 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2354 msecs_to_jiffies(params->spp_hbinterval);
2357 msecs_to_jiffies(params->spp_hbinterval);
2359 sp->hbinterval = params->spp_hbinterval;
2366 trans->param_flags =
2367 (trans->param_flags & ~SPP_HB) | hb_change;
2370 (asoc->param_flags & ~SPP_HB) | hb_change;
2373 (sp->param_flags & ~SPP_HB) | hb_change;
2377 /* When Path MTU discovery is disabled the value specified here will
2378 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2379 * include the flag SPP_PMTUD_DISABLE for this field to have any
2382 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2384 trans->pathmtu = params->spp_pathmtu;
2385 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
2387 asoc->pathmtu = params->spp_pathmtu;
2388 sctp_frag_point(asoc, params->spp_pathmtu);
2390 sp->pathmtu = params->spp_pathmtu;
2396 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2397 (params->spp_flags & SPP_PMTUD_ENABLE);
2398 trans->param_flags =
2399 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2401 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2402 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
2406 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2409 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2413 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2414 * value of this field is ignored. Note also that a value of zero
2415 * indicates the current setting should be left unchanged.
2417 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2420 msecs_to_jiffies(params->spp_sackdelay);
2423 msecs_to_jiffies(params->spp_sackdelay);
2425 sp->sackdelay = params->spp_sackdelay;
2429 if (sackdelay_change) {
2431 trans->param_flags =
2432 (trans->param_flags & ~SPP_SACKDELAY) |
2436 (asoc->param_flags & ~SPP_SACKDELAY) |
2440 (sp->param_flags & ~SPP_SACKDELAY) |
2445 /* Note that a value of zero indicates the current setting should be
2448 if (params->spp_pathmaxrxt) {
2450 trans->pathmaxrxt = params->spp_pathmaxrxt;
2452 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2454 sp->pathmaxrxt = params->spp_pathmaxrxt;
2461 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2462 char __user *optval,
2463 unsigned int optlen)
2465 struct sctp_paddrparams params;
2466 struct sctp_transport *trans = NULL;
2467 struct sctp_association *asoc = NULL;
2468 struct sctp_sock *sp = sctp_sk(sk);
2470 int hb_change, pmtud_change, sackdelay_change;
2472 if (optlen != sizeof(struct sctp_paddrparams))
2475 if (copy_from_user(¶ms, optval, optlen))
2478 /* Validate flags and value parameters. */
2479 hb_change = params.spp_flags & SPP_HB;
2480 pmtud_change = params.spp_flags & SPP_PMTUD;
2481 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2483 if (hb_change == SPP_HB ||
2484 pmtud_change == SPP_PMTUD ||
2485 sackdelay_change == SPP_SACKDELAY ||
2486 params.spp_sackdelay > 500 ||
2487 (params.spp_pathmtu &&
2488 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2491 /* If an address other than INADDR_ANY is specified, and
2492 * no transport is found, then the request is invalid.
2494 if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) {
2495 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2496 params.spp_assoc_id);
2501 /* Get association, if assoc_id != 0 and the socket is a one
2502 * to many style socket, and an association was not found, then
2503 * the id was invalid.
2505 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2506 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2509 /* Heartbeat demand can only be sent on a transport or
2510 * association, but not a socket.
2512 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2515 /* Process parameters. */
2516 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2517 hb_change, pmtud_change,
2523 /* If changes are for association, also apply parameters to each
2526 if (!trans && asoc) {
2527 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2529 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2530 hb_change, pmtud_change,
2539 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2541 * This option will effect the way delayed acks are performed. This
2542 * option allows you to get or set the delayed ack time, in
2543 * milliseconds. It also allows changing the delayed ack frequency.
2544 * Changing the frequency to 1 disables the delayed sack algorithm. If
2545 * the assoc_id is 0, then this sets or gets the endpoints default
2546 * values. If the assoc_id field is non-zero, then the set or get
2547 * effects the specified association for the one to many model (the
2548 * assoc_id field is ignored by the one to one model). Note that if
2549 * sack_delay or sack_freq are 0 when setting this option, then the
2550 * current values will remain unchanged.
2552 * struct sctp_sack_info {
2553 * sctp_assoc_t sack_assoc_id;
2554 * uint32_t sack_delay;
2555 * uint32_t sack_freq;
2558 * sack_assoc_id - This parameter, indicates which association the user
2559 * is performing an action upon. Note that if this field's value is
2560 * zero then the endpoints default value is changed (effecting future
2561 * associations only).
2563 * sack_delay - This parameter contains the number of milliseconds that
2564 * the user is requesting the delayed ACK timer be set to. Note that
2565 * this value is defined in the standard to be between 200 and 500
2568 * sack_freq - This parameter contains the number of packets that must
2569 * be received before a sack is sent without waiting for the delay
2570 * timer to expire. The default value for this is 2, setting this
2571 * value to 1 will disable the delayed sack algorithm.
2574 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2575 char __user *optval, unsigned int optlen)
2577 struct sctp_sack_info params;
2578 struct sctp_transport *trans = NULL;
2579 struct sctp_association *asoc = NULL;
2580 struct sctp_sock *sp = sctp_sk(sk);
2582 if (optlen == sizeof(struct sctp_sack_info)) {
2583 if (copy_from_user(¶ms, optval, optlen))
2586 if (params.sack_delay == 0 && params.sack_freq == 0)
2588 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2589 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2590 pr_warn("Use struct sctp_sack_info instead\n");
2591 if (copy_from_user(¶ms, optval, optlen))
2594 if (params.sack_delay == 0)
2595 params.sack_freq = 1;
2597 params.sack_freq = 0;
2601 /* Validate value parameter. */
2602 if (params.sack_delay > 500)
2605 /* Get association, if sack_assoc_id != 0 and the socket is a one
2606 * to many style socket, and an association was not found, then
2607 * the id was invalid.
2609 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2610 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2613 if (params.sack_delay) {
2616 msecs_to_jiffies(params.sack_delay);
2618 (asoc->param_flags & ~SPP_SACKDELAY) |
2619 SPP_SACKDELAY_ENABLE;
2621 sp->sackdelay = params.sack_delay;
2623 (sp->param_flags & ~SPP_SACKDELAY) |
2624 SPP_SACKDELAY_ENABLE;
2628 if (params.sack_freq == 1) {
2631 (asoc->param_flags & ~SPP_SACKDELAY) |
2632 SPP_SACKDELAY_DISABLE;
2635 (sp->param_flags & ~SPP_SACKDELAY) |
2636 SPP_SACKDELAY_DISABLE;
2638 } else if (params.sack_freq > 1) {
2640 asoc->sackfreq = params.sack_freq;
2642 (asoc->param_flags & ~SPP_SACKDELAY) |
2643 SPP_SACKDELAY_ENABLE;
2645 sp->sackfreq = params.sack_freq;
2647 (sp->param_flags & ~SPP_SACKDELAY) |
2648 SPP_SACKDELAY_ENABLE;
2652 /* If change is for association, also apply to each transport. */
2654 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2656 if (params.sack_delay) {
2658 msecs_to_jiffies(params.sack_delay);
2659 trans->param_flags =
2660 (trans->param_flags & ~SPP_SACKDELAY) |
2661 SPP_SACKDELAY_ENABLE;
2663 if (params.sack_freq == 1) {
2664 trans->param_flags =
2665 (trans->param_flags & ~SPP_SACKDELAY) |
2666 SPP_SACKDELAY_DISABLE;
2667 } else if (params.sack_freq > 1) {
2668 trans->sackfreq = params.sack_freq;
2669 trans->param_flags =
2670 (trans->param_flags & ~SPP_SACKDELAY) |
2671 SPP_SACKDELAY_ENABLE;
2679 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2681 * Applications can specify protocol parameters for the default association
2682 * initialization. The option name argument to setsockopt() and getsockopt()
2685 * Setting initialization parameters is effective only on an unconnected
2686 * socket (for UDP-style sockets only future associations are effected
2687 * by the change). With TCP-style sockets, this option is inherited by
2688 * sockets derived from a listener socket.
2690 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2692 struct sctp_initmsg sinit;
2693 struct sctp_sock *sp = sctp_sk(sk);
2695 if (optlen != sizeof(struct sctp_initmsg))
2697 if (copy_from_user(&sinit, optval, optlen))
2700 if (sinit.sinit_num_ostreams)
2701 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2702 if (sinit.sinit_max_instreams)
2703 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2704 if (sinit.sinit_max_attempts)
2705 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2706 if (sinit.sinit_max_init_timeo)
2707 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2713 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2715 * Applications that wish to use the sendto() system call may wish to
2716 * specify a default set of parameters that would normally be supplied
2717 * through the inclusion of ancillary data. This socket option allows
2718 * such an application to set the default sctp_sndrcvinfo structure.
2719 * The application that wishes to use this socket option simply passes
2720 * in to this call the sctp_sndrcvinfo structure defined in Section
2721 * 5.2.2) The input parameters accepted by this call include
2722 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2723 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2724 * to this call if the caller is using the UDP model.
2726 static int sctp_setsockopt_default_send_param(struct sock *sk,
2727 char __user *optval,
2728 unsigned int optlen)
2730 struct sctp_sndrcvinfo info;
2731 struct sctp_association *asoc;
2732 struct sctp_sock *sp = sctp_sk(sk);
2734 if (optlen != sizeof(struct sctp_sndrcvinfo))
2736 if (copy_from_user(&info, optval, optlen))
2739 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2740 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2744 asoc->default_stream = info.sinfo_stream;
2745 asoc->default_flags = info.sinfo_flags;
2746 asoc->default_ppid = info.sinfo_ppid;
2747 asoc->default_context = info.sinfo_context;
2748 asoc->default_timetolive = info.sinfo_timetolive;
2750 sp->default_stream = info.sinfo_stream;
2751 sp->default_flags = info.sinfo_flags;
2752 sp->default_ppid = info.sinfo_ppid;
2753 sp->default_context = info.sinfo_context;
2754 sp->default_timetolive = info.sinfo_timetolive;
2760 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2762 * Requests that the local SCTP stack use the enclosed peer address as
2763 * the association primary. The enclosed address must be one of the
2764 * association peer's addresses.
2766 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2767 unsigned int optlen)
2769 struct sctp_prim prim;
2770 struct sctp_transport *trans;
2772 if (optlen != sizeof(struct sctp_prim))
2775 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2778 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2782 sctp_assoc_set_primary(trans->asoc, trans);
2788 * 7.1.5 SCTP_NODELAY
2790 * Turn on/off any Nagle-like algorithm. This means that packets are
2791 * generally sent as soon as possible and no unnecessary delays are
2792 * introduced, at the cost of more packets in the network. Expects an
2793 * integer boolean flag.
2795 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2796 unsigned int optlen)
2800 if (optlen < sizeof(int))
2802 if (get_user(val, (int __user *)optval))
2805 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2811 * 7.1.1 SCTP_RTOINFO
2813 * The protocol parameters used to initialize and bound retransmission
2814 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2815 * and modify these parameters.
2816 * All parameters are time values, in milliseconds. A value of 0, when
2817 * modifying the parameters, indicates that the current value should not
2821 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2823 struct sctp_rtoinfo rtoinfo;
2824 struct sctp_association *asoc;
2826 if (optlen != sizeof (struct sctp_rtoinfo))
2829 if (copy_from_user(&rtoinfo, optval, optlen))
2832 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2834 /* Set the values to the specific association */
2835 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2839 if (rtoinfo.srto_initial != 0)
2841 msecs_to_jiffies(rtoinfo.srto_initial);
2842 if (rtoinfo.srto_max != 0)
2843 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2844 if (rtoinfo.srto_min != 0)
2845 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2847 /* If there is no association or the association-id = 0
2848 * set the values to the endpoint.
2850 struct sctp_sock *sp = sctp_sk(sk);
2852 if (rtoinfo.srto_initial != 0)
2853 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2854 if (rtoinfo.srto_max != 0)
2855 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2856 if (rtoinfo.srto_min != 0)
2857 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2865 * 7.1.2 SCTP_ASSOCINFO
2867 * This option is used to tune the maximum retransmission attempts
2868 * of the association.
2869 * Returns an error if the new association retransmission value is
2870 * greater than the sum of the retransmission value of the peer.
2871 * See [SCTP] for more information.
2874 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
2877 struct sctp_assocparams assocparams;
2878 struct sctp_association *asoc;
2880 if (optlen != sizeof(struct sctp_assocparams))
2882 if (copy_from_user(&assocparams, optval, optlen))
2885 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2887 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2890 /* Set the values to the specific association */
2892 if (assocparams.sasoc_asocmaxrxt != 0) {
2895 struct sctp_transport *peer_addr;
2897 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2899 path_sum += peer_addr->pathmaxrxt;
2903 /* Only validate asocmaxrxt if we have more than
2904 * one path/transport. We do this because path
2905 * retransmissions are only counted when we have more
2909 assocparams.sasoc_asocmaxrxt > path_sum)
2912 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2915 if (assocparams.sasoc_cookie_life != 0) {
2916 asoc->cookie_life.tv_sec =
2917 assocparams.sasoc_cookie_life / 1000;
2918 asoc->cookie_life.tv_usec =
2919 (assocparams.sasoc_cookie_life % 1000)
2923 /* Set the values to the endpoint */
2924 struct sctp_sock *sp = sctp_sk(sk);
2926 if (assocparams.sasoc_asocmaxrxt != 0)
2927 sp->assocparams.sasoc_asocmaxrxt =
2928 assocparams.sasoc_asocmaxrxt;
2929 if (assocparams.sasoc_cookie_life != 0)
2930 sp->assocparams.sasoc_cookie_life =
2931 assocparams.sasoc_cookie_life;
2937 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2939 * This socket option is a boolean flag which turns on or off mapped V4
2940 * addresses. If this option is turned on and the socket is type
2941 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2942 * If this option is turned off, then no mapping will be done of V4
2943 * addresses and a user will receive both PF_INET6 and PF_INET type
2944 * addresses on the socket.
2946 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
2949 struct sctp_sock *sp = sctp_sk(sk);
2951 if (optlen < sizeof(int))
2953 if (get_user(val, (int __user *)optval))
2964 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2965 * This option will get or set the maximum size to put in any outgoing
2966 * SCTP DATA chunk. If a message is larger than this size it will be
2967 * fragmented by SCTP into the specified size. Note that the underlying
2968 * SCTP implementation may fragment into smaller sized chunks when the
2969 * PMTU of the underlying association is smaller than the value set by
2970 * the user. The default value for this option is '0' which indicates
2971 * the user is NOT limiting fragmentation and only the PMTU will effect
2972 * SCTP's choice of DATA chunk size. Note also that values set larger
2973 * than the maximum size of an IP datagram will effectively let SCTP
2974 * control fragmentation (i.e. the same as setting this option to 0).
2976 * The following structure is used to access and modify this parameter:
2978 * struct sctp_assoc_value {
2979 * sctp_assoc_t assoc_id;
2980 * uint32_t assoc_value;
2983 * assoc_id: This parameter is ignored for one-to-one style sockets.
2984 * For one-to-many style sockets this parameter indicates which
2985 * association the user is performing an action upon. Note that if
2986 * this field's value is zero then the endpoints default value is
2987 * changed (effecting future associations only).
2988 * assoc_value: This parameter specifies the maximum size in bytes.
2990 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
2992 struct sctp_assoc_value params;
2993 struct sctp_association *asoc;
2994 struct sctp_sock *sp = sctp_sk(sk);
2997 if (optlen == sizeof(int)) {
2998 pr_warn("Use of int in maxseg socket option deprecated\n");
2999 pr_warn("Use struct sctp_assoc_value instead\n");
3000 if (copy_from_user(&val, optval, optlen))
3002 params.assoc_id = 0;
3003 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3004 if (copy_from_user(¶ms, optval, optlen))
3006 val = params.assoc_value;
3010 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
3013 asoc = sctp_id2assoc(sk, params.assoc_id);
3014 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3019 val = asoc->pathmtu;
3020 val -= sp->pf->af->net_header_len;
3021 val -= sizeof(struct sctphdr) +
3022 sizeof(struct sctp_data_chunk);
3024 asoc->user_frag = val;
3025 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
3027 sp->user_frag = val;
3035 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3037 * Requests that the peer mark the enclosed address as the association
3038 * primary. The enclosed address must be one of the association's
3039 * locally bound addresses. The following structure is used to make a
3040 * set primary request:
3042 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3043 unsigned int optlen)
3045 struct net *net = sock_net(sk);
3046 struct sctp_sock *sp;
3047 struct sctp_association *asoc = NULL;
3048 struct sctp_setpeerprim prim;
3049 struct sctp_chunk *chunk;
3055 if (!net->sctp.addip_enable)
3058 if (optlen != sizeof(struct sctp_setpeerprim))
3061 if (copy_from_user(&prim, optval, optlen))
3064 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3068 if (!asoc->peer.asconf_capable)
3071 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3074 if (!sctp_state(asoc, ESTABLISHED))
3077 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3081 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3082 return -EADDRNOTAVAIL;
3084 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3085 return -EADDRNOTAVAIL;
3087 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3088 chunk = sctp_make_asconf_set_prim(asoc,
3089 (union sctp_addr *)&prim.sspp_addr);
3093 err = sctp_send_asconf(asoc, chunk);
3095 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3100 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3101 unsigned int optlen)
3103 struct sctp_setadaptation adaptation;
3105 if (optlen != sizeof(struct sctp_setadaptation))
3107 if (copy_from_user(&adaptation, optval, optlen))
3110 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3116 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3118 * The context field in the sctp_sndrcvinfo structure is normally only
3119 * used when a failed message is retrieved holding the value that was
3120 * sent down on the actual send call. This option allows the setting of
3121 * a default context on an association basis that will be received on
3122 * reading messages from the peer. This is especially helpful in the
3123 * one-2-many model for an application to keep some reference to an
3124 * internal state machine that is processing messages on the
3125 * association. Note that the setting of this value only effects
3126 * received messages from the peer and does not effect the value that is
3127 * saved with outbound messages.
3129 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3130 unsigned int optlen)
3132 struct sctp_assoc_value params;
3133 struct sctp_sock *sp;
3134 struct sctp_association *asoc;
3136 if (optlen != sizeof(struct sctp_assoc_value))
3138 if (copy_from_user(¶ms, optval, optlen))
3143 if (params.assoc_id != 0) {
3144 asoc = sctp_id2assoc(sk, params.assoc_id);
3147 asoc->default_rcv_context = params.assoc_value;
3149 sp->default_rcv_context = params.assoc_value;
3156 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3158 * This options will at a minimum specify if the implementation is doing
3159 * fragmented interleave. Fragmented interleave, for a one to many
3160 * socket, is when subsequent calls to receive a message may return
3161 * parts of messages from different associations. Some implementations
3162 * may allow you to turn this value on or off. If so, when turned off,
3163 * no fragment interleave will occur (which will cause a head of line
3164 * blocking amongst multiple associations sharing the same one to many
3165 * socket). When this option is turned on, then each receive call may
3166 * come from a different association (thus the user must receive data
3167 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3168 * association each receive belongs to.
3170 * This option takes a boolean value. A non-zero value indicates that
3171 * fragmented interleave is on. A value of zero indicates that
3172 * fragmented interleave is off.
3174 * Note that it is important that an implementation that allows this
3175 * option to be turned on, have it off by default. Otherwise an unaware
3176 * application using the one to many model may become confused and act
3179 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3180 char __user *optval,
3181 unsigned int optlen)
3185 if (optlen != sizeof(int))
3187 if (get_user(val, (int __user *)optval))
3190 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3196 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3197 * (SCTP_PARTIAL_DELIVERY_POINT)
3199 * This option will set or get the SCTP partial delivery point. This
3200 * point is the size of a message where the partial delivery API will be
3201 * invoked to help free up rwnd space for the peer. Setting this to a
3202 * lower value will cause partial deliveries to happen more often. The
3203 * calls argument is an integer that sets or gets the partial delivery
3204 * point. Note also that the call will fail if the user attempts to set
3205 * this value larger than the socket receive buffer size.
3207 * Note that any single message having a length smaller than or equal to
3208 * the SCTP partial delivery point will be delivered in one single read
3209 * call as long as the user provided buffer is large enough to hold the
3212 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3213 char __user *optval,
3214 unsigned int optlen)
3218 if (optlen != sizeof(u32))
3220 if (get_user(val, (int __user *)optval))
3223 /* Note: We double the receive buffer from what the user sets
3224 * it to be, also initial rwnd is based on rcvbuf/2.
3226 if (val > (sk->sk_rcvbuf >> 1))
3229 sctp_sk(sk)->pd_point = val;
3231 return 0; /* is this the right error code? */
3235 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3237 * This option will allow a user to change the maximum burst of packets
3238 * that can be emitted by this association. Note that the default value
3239 * is 4, and some implementations may restrict this setting so that it
3240 * can only be lowered.
3242 * NOTE: This text doesn't seem right. Do this on a socket basis with
3243 * future associations inheriting the socket value.
3245 static int sctp_setsockopt_maxburst(struct sock *sk,
3246 char __user *optval,
3247 unsigned int optlen)
3249 struct sctp_assoc_value params;
3250 struct sctp_sock *sp;
3251 struct sctp_association *asoc;
3255 if (optlen == sizeof(int)) {
3256 pr_warn("Use of int in max_burst socket option deprecated\n");
3257 pr_warn("Use struct sctp_assoc_value instead\n");
3258 if (copy_from_user(&val, optval, optlen))
3260 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3261 if (copy_from_user(¶ms, optval, optlen))
3263 val = params.assoc_value;
3264 assoc_id = params.assoc_id;
3270 if (assoc_id != 0) {
3271 asoc = sctp_id2assoc(sk, assoc_id);
3274 asoc->max_burst = val;
3276 sp->max_burst = val;
3282 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3284 * This set option adds a chunk type that the user is requesting to be
3285 * received only in an authenticated way. Changes to the list of chunks
3286 * will only effect future associations on the socket.
3288 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3289 char __user *optval,
3290 unsigned int optlen)
3292 struct net *net = sock_net(sk);
3293 struct sctp_authchunk val;
3295 if (!net->sctp.auth_enable)
3298 if (optlen != sizeof(struct sctp_authchunk))
3300 if (copy_from_user(&val, optval, optlen))
3303 switch (val.sauth_chunk) {
3305 case SCTP_CID_INIT_ACK:
3306 case SCTP_CID_SHUTDOWN_COMPLETE:
3311 /* add this chunk id to the endpoint */
3312 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3316 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3318 * This option gets or sets the list of HMAC algorithms that the local
3319 * endpoint requires the peer to use.
3321 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3322 char __user *optval,
3323 unsigned int optlen)
3325 struct net *net = sock_net(sk);
3326 struct sctp_hmacalgo *hmacs;
3330 if (!net->sctp.auth_enable)
3333 if (optlen < sizeof(struct sctp_hmacalgo))
3336 hmacs= memdup_user(optval, optlen);
3338 return PTR_ERR(hmacs);
3340 idents = hmacs->shmac_num_idents;
3341 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3342 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3347 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3354 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3356 * This option will set a shared secret key which is used to build an
3357 * association shared key.
3359 static int sctp_setsockopt_auth_key(struct sock *sk,
3360 char __user *optval,
3361 unsigned int optlen)
3363 struct net *net = sock_net(sk);
3364 struct sctp_authkey *authkey;
3365 struct sctp_association *asoc;
3368 if (!net->sctp.auth_enable)
3371 if (optlen <= sizeof(struct sctp_authkey))
3374 authkey= memdup_user(optval, optlen);
3375 if (IS_ERR(authkey))
3376 return PTR_ERR(authkey);
3378 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3383 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3384 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3389 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3396 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3398 * This option will get or set the active shared key to be used to build
3399 * the association shared key.
3401 static int sctp_setsockopt_active_key(struct sock *sk,
3402 char __user *optval,
3403 unsigned int optlen)
3405 struct net *net = sock_net(sk);
3406 struct sctp_authkeyid val;
3407 struct sctp_association *asoc;
3409 if (!net->sctp.auth_enable)
3412 if (optlen != sizeof(struct sctp_authkeyid))
3414 if (copy_from_user(&val, optval, optlen))
3417 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3418 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3421 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3422 val.scact_keynumber);
3426 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3428 * This set option will delete a shared secret key from use.
3430 static int sctp_setsockopt_del_key(struct sock *sk,
3431 char __user *optval,
3432 unsigned int optlen)
3434 struct net *net = sock_net(sk);
3435 struct sctp_authkeyid val;
3436 struct sctp_association *asoc;
3438 if (!net->sctp.auth_enable)
3441 if (optlen != sizeof(struct sctp_authkeyid))
3443 if (copy_from_user(&val, optval, optlen))
3446 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3447 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3450 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3451 val.scact_keynumber);
3456 * 8.1.23 SCTP_AUTO_ASCONF
3458 * This option will enable or disable the use of the automatic generation of
3459 * ASCONF chunks to add and delete addresses to an existing association. Note
3460 * that this option has two caveats namely: a) it only affects sockets that
3461 * are bound to all addresses available to the SCTP stack, and b) the system
3462 * administrator may have an overriding control that turns the ASCONF feature
3463 * off no matter what setting the socket option may have.
3464 * This option expects an integer boolean flag, where a non-zero value turns on
3465 * the option, and a zero value turns off the option.
3466 * Note. In this implementation, socket operation overrides default parameter
3467 * being set by sysctl as well as FreeBSD implementation
3469 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3470 unsigned int optlen)
3473 struct sctp_sock *sp = sctp_sk(sk);
3475 if (optlen < sizeof(int))
3477 if (get_user(val, (int __user *)optval))
3479 if (!sctp_is_ep_boundall(sk) && val)
3481 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3484 if (val == 0 && sp->do_auto_asconf) {
3485 list_del(&sp->auto_asconf_list);
3486 sp->do_auto_asconf = 0;
3487 } else if (val && !sp->do_auto_asconf) {
3488 list_add_tail(&sp->auto_asconf_list,
3489 &sock_net(sk)->sctp.auto_asconf_splist);
3490 sp->do_auto_asconf = 1;
3497 * SCTP_PEER_ADDR_THLDS
3499 * This option allows us to alter the partially failed threshold for one or all
3500 * transports in an association. See Section 6.1 of:
3501 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3503 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3504 char __user *optval,
3505 unsigned int optlen)
3507 struct sctp_paddrthlds val;
3508 struct sctp_transport *trans;
3509 struct sctp_association *asoc;
3511 if (optlen < sizeof(struct sctp_paddrthlds))
3513 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3514 sizeof(struct sctp_paddrthlds)))
3518 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3519 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3522 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3524 if (val.spt_pathmaxrxt)
3525 trans->pathmaxrxt = val.spt_pathmaxrxt;
3526 trans->pf_retrans = val.spt_pathpfthld;
3529 if (val.spt_pathmaxrxt)
3530 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3531 asoc->pf_retrans = val.spt_pathpfthld;
3533 trans = sctp_addr_id2transport(sk, &val.spt_address,
3538 if (val.spt_pathmaxrxt)
3539 trans->pathmaxrxt = val.spt_pathmaxrxt;
3540 trans->pf_retrans = val.spt_pathpfthld;
3546 /* API 6.2 setsockopt(), getsockopt()
3548 * Applications use setsockopt() and getsockopt() to set or retrieve
3549 * socket options. Socket options are used to change the default
3550 * behavior of sockets calls. They are described in Section 7.
3554 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3555 * int __user *optlen);
3556 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3559 * sd - the socket descript.
3560 * level - set to IPPROTO_SCTP for all SCTP options.
3561 * optname - the option name.
3562 * optval - the buffer to store the value of the option.
3563 * optlen - the size of the buffer.
3565 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
3566 char __user *optval, unsigned int optlen)
3570 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3573 /* I can hardly begin to describe how wrong this is. This is
3574 * so broken as to be worse than useless. The API draft
3575 * REALLY is NOT helpful here... I am not convinced that the
3576 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3577 * are at all well-founded.
3579 if (level != SOL_SCTP) {
3580 struct sctp_af *af = sctp_sk(sk)->pf->af;
3581 retval = af->setsockopt(sk, level, optname, optval, optlen);
3588 case SCTP_SOCKOPT_BINDX_ADD:
3589 /* 'optlen' is the size of the addresses buffer. */
3590 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3591 optlen, SCTP_BINDX_ADD_ADDR);
3594 case SCTP_SOCKOPT_BINDX_REM:
3595 /* 'optlen' is the size of the addresses buffer. */
3596 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3597 optlen, SCTP_BINDX_REM_ADDR);
3600 case SCTP_SOCKOPT_CONNECTX_OLD:
3601 /* 'optlen' is the size of the addresses buffer. */
3602 retval = sctp_setsockopt_connectx_old(sk,
3603 (struct sockaddr __user *)optval,
3607 case SCTP_SOCKOPT_CONNECTX:
3608 /* 'optlen' is the size of the addresses buffer. */
3609 retval = sctp_setsockopt_connectx(sk,
3610 (struct sockaddr __user *)optval,
3614 case SCTP_DISABLE_FRAGMENTS:
3615 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3619 retval = sctp_setsockopt_events(sk, optval, optlen);
3622 case SCTP_AUTOCLOSE:
3623 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3626 case SCTP_PEER_ADDR_PARAMS:
3627 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3630 case SCTP_DELAYED_SACK:
3631 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
3633 case SCTP_PARTIAL_DELIVERY_POINT:
3634 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3638 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3640 case SCTP_DEFAULT_SEND_PARAM:
3641 retval = sctp_setsockopt_default_send_param(sk, optval,
3644 case SCTP_PRIMARY_ADDR:
3645 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3647 case SCTP_SET_PEER_PRIMARY_ADDR:
3648 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3651 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3654 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3656 case SCTP_ASSOCINFO:
3657 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3659 case SCTP_I_WANT_MAPPED_V4_ADDR:
3660 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3663 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3665 case SCTP_ADAPTATION_LAYER:
3666 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3669 retval = sctp_setsockopt_context(sk, optval, optlen);
3671 case SCTP_FRAGMENT_INTERLEAVE:
3672 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3674 case SCTP_MAX_BURST:
3675 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3677 case SCTP_AUTH_CHUNK:
3678 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3680 case SCTP_HMAC_IDENT:
3681 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3684 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3686 case SCTP_AUTH_ACTIVE_KEY:
3687 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3689 case SCTP_AUTH_DELETE_KEY:
3690 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3692 case SCTP_AUTO_ASCONF:
3693 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3695 case SCTP_PEER_ADDR_THLDS:
3696 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3699 retval = -ENOPROTOOPT;
3703 sctp_release_sock(sk);
3709 /* API 3.1.6 connect() - UDP Style Syntax
3711 * An application may use the connect() call in the UDP model to initiate an
3712 * association without sending data.
3716 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3718 * sd: the socket descriptor to have a new association added to.
3720 * nam: the address structure (either struct sockaddr_in or struct
3721 * sockaddr_in6 defined in RFC2553 [7]).
3723 * len: the size of the address.
3725 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3733 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3734 __func__, sk, addr, addr_len);
3736 /* Validate addr_len before calling common connect/connectx routine. */
3737 af = sctp_get_af_specific(addr->sa_family);
3738 if (!af || addr_len < af->sockaddr_len) {
3741 /* Pass correct addr len to common routine (so it knows there
3742 * is only one address being passed.
3744 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
3747 sctp_release_sock(sk);
3751 /* FIXME: Write comments. */
3752 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3754 return -EOPNOTSUPP; /* STUB */
3757 /* 4.1.4 accept() - TCP Style Syntax
3759 * Applications use accept() call to remove an established SCTP
3760 * association from the accept queue of the endpoint. A new socket
3761 * descriptor will be returned from accept() to represent the newly
3762 * formed association.
3764 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3766 struct sctp_sock *sp;
3767 struct sctp_endpoint *ep;
3768 struct sock *newsk = NULL;
3769 struct sctp_association *asoc;
3778 if (!sctp_style(sk, TCP)) {
3779 error = -EOPNOTSUPP;
3783 if (!sctp_sstate(sk, LISTENING)) {
3788 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3790 error = sctp_wait_for_accept(sk, timeo);
3794 /* We treat the list of associations on the endpoint as the accept
3795 * queue and pick the first association on the list.
3797 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3799 newsk = sp->pf->create_accept_sk(sk, asoc);
3805 /* Populate the fields of the newsk from the oldsk and migrate the
3806 * asoc to the newsk.
3808 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3811 sctp_release_sock(sk);
3816 /* The SCTP ioctl handler. */
3817 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3824 * SEQPACKET-style sockets in LISTENING state are valid, for
3825 * SCTP, so only discard TCP-style sockets in LISTENING state.
3827 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3832 struct sk_buff *skb;
3833 unsigned int amount = 0;
3835 skb = skb_peek(&sk->sk_receive_queue);
3838 * We will only return the amount of this packet since
3839 * that is all that will be read.
3843 rc = put_user(amount, (int __user *)arg);
3851 sctp_release_sock(sk);
3855 /* This is the function which gets called during socket creation to
3856 * initialized the SCTP-specific portion of the sock.
3857 * The sock structure should already be zero-filled memory.
3859 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3861 struct net *net = sock_net(sk);
3862 struct sctp_endpoint *ep;
3863 struct sctp_sock *sp;
3865 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3869 /* Initialize the SCTP per socket area. */
3870 switch (sk->sk_type) {
3871 case SOCK_SEQPACKET:
3872 sp->type = SCTP_SOCKET_UDP;
3875 sp->type = SCTP_SOCKET_TCP;
3878 return -ESOCKTNOSUPPORT;
3881 /* Initialize default send parameters. These parameters can be
3882 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3884 sp->default_stream = 0;
3885 sp->default_ppid = 0;
3886 sp->default_flags = 0;
3887 sp->default_context = 0;
3888 sp->default_timetolive = 0;
3890 sp->default_rcv_context = 0;
3891 sp->max_burst = net->sctp.max_burst;
3893 /* Initialize default setup parameters. These parameters
3894 * can be modified with the SCTP_INITMSG socket option or
3895 * overridden by the SCTP_INIT CMSG.
3897 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3898 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3899 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
3900 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
3902 /* Initialize default RTO related parameters. These parameters can
3903 * be modified for with the SCTP_RTOINFO socket option.
3905 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
3906 sp->rtoinfo.srto_max = net->sctp.rto_max;
3907 sp->rtoinfo.srto_min = net->sctp.rto_min;
3909 /* Initialize default association related parameters. These parameters
3910 * can be modified with the SCTP_ASSOCINFO socket option.
3912 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
3913 sp->assocparams.sasoc_number_peer_destinations = 0;
3914 sp->assocparams.sasoc_peer_rwnd = 0;
3915 sp->assocparams.sasoc_local_rwnd = 0;
3916 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
3918 /* Initialize default event subscriptions. By default, all the
3921 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3923 /* Default Peer Address Parameters. These defaults can
3924 * be modified via SCTP_PEER_ADDR_PARAMS
3926 sp->hbinterval = net->sctp.hb_interval;
3927 sp->pathmaxrxt = net->sctp.max_retrans_path;
3928 sp->pathmtu = 0; // allow default discovery
3929 sp->sackdelay = net->sctp.sack_timeout;
3931 sp->param_flags = SPP_HB_ENABLE |
3933 SPP_SACKDELAY_ENABLE;
3935 /* If enabled no SCTP message fragmentation will be performed.
3936 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3938 sp->disable_fragments = 0;
3940 /* Enable Nagle algorithm by default. */
3943 /* Enable by default. */
3946 /* Auto-close idle associations after the configured
3947 * number of seconds. A value of 0 disables this
3948 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3949 * for UDP-style sockets only.
3953 /* User specified fragmentation limit. */
3956 sp->adaptation_ind = 0;
3958 sp->pf = sctp_get_pf_specific(sk->sk_family);
3960 /* Control variables for partial data delivery. */
3961 atomic_set(&sp->pd_mode, 0);
3962 skb_queue_head_init(&sp->pd_lobby);
3963 sp->frag_interleave = 0;
3965 /* Create a per socket endpoint structure. Even if we
3966 * change the data structure relationships, this may still
3967 * be useful for storing pre-connect address information.
3969 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3976 SCTP_DBG_OBJCNT_INC(sock);
3979 percpu_counter_inc(&sctp_sockets_allocated);
3980 sock_prot_inuse_add(net, sk->sk_prot, 1);
3981 if (net->sctp.default_auto_asconf) {
3982 list_add_tail(&sp->auto_asconf_list,
3983 &net->sctp.auto_asconf_splist);
3984 sp->do_auto_asconf = 1;
3986 sp->do_auto_asconf = 0;
3992 /* Cleanup any SCTP per socket resources. */
3993 SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
3995 struct sctp_sock *sp;
3997 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3999 /* Release our hold on the endpoint. */
4001 if (sp->do_auto_asconf) {
4002 sp->do_auto_asconf = 0;
4003 list_del(&sp->auto_asconf_list);
4005 sctp_endpoint_free(sp->ep);
4007 percpu_counter_dec(&sctp_sockets_allocated);
4008 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4012 /* API 4.1.7 shutdown() - TCP Style Syntax
4013 * int shutdown(int socket, int how);
4015 * sd - the socket descriptor of the association to be closed.
4016 * how - Specifies the type of shutdown. The values are
4019 * Disables further receive operations. No SCTP
4020 * protocol action is taken.
4022 * Disables further send operations, and initiates
4023 * the SCTP shutdown sequence.
4025 * Disables further send and receive operations
4026 * and initiates the SCTP shutdown sequence.
4028 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4030 struct net *net = sock_net(sk);
4031 struct sctp_endpoint *ep;
4032 struct sctp_association *asoc;
4034 if (!sctp_style(sk, TCP))
4037 if (how & SEND_SHUTDOWN) {
4038 ep = sctp_sk(sk)->ep;
4039 if (!list_empty(&ep->asocs)) {
4040 asoc = list_entry(ep->asocs.next,
4041 struct sctp_association, asocs);
4042 sctp_primitive_SHUTDOWN(net, asoc, NULL);
4047 /* 7.2.1 Association Status (SCTP_STATUS)
4049 * Applications can retrieve current status information about an
4050 * association, including association state, peer receiver window size,
4051 * number of unacked data chunks, and number of data chunks pending
4052 * receipt. This information is read-only.
4054 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4055 char __user *optval,
4058 struct sctp_status status;
4059 struct sctp_association *asoc = NULL;
4060 struct sctp_transport *transport;
4061 sctp_assoc_t associd;
4064 if (len < sizeof(status)) {
4069 len = sizeof(status);
4070 if (copy_from_user(&status, optval, len)) {
4075 associd = status.sstat_assoc_id;
4076 asoc = sctp_id2assoc(sk, associd);
4082 transport = asoc->peer.primary_path;
4084 status.sstat_assoc_id = sctp_assoc2id(asoc);
4085 status.sstat_state = asoc->state;
4086 status.sstat_rwnd = asoc->peer.rwnd;
4087 status.sstat_unackdata = asoc->unack_data;
4089 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4090 status.sstat_instrms = asoc->c.sinit_max_instreams;
4091 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4092 status.sstat_fragmentation_point = asoc->frag_point;
4093 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4094 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4095 transport->af_specific->sockaddr_len);
4096 /* Map ipv4 address into v4-mapped-on-v6 address. */
4097 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4098 (union sctp_addr *)&status.sstat_primary.spinfo_address);
4099 status.sstat_primary.spinfo_state = transport->state;
4100 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4101 status.sstat_primary.spinfo_srtt = transport->srtt;
4102 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
4103 status.sstat_primary.spinfo_mtu = transport->pathmtu;
4105 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4106 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4108 if (put_user(len, optlen)) {
4113 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4114 len, status.sstat_state, status.sstat_rwnd,
4115 status.sstat_assoc_id);
4117 if (copy_to_user(optval, &status, len)) {
4127 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4129 * Applications can retrieve information about a specific peer address
4130 * of an association, including its reachability state, congestion
4131 * window, and retransmission timer values. This information is
4134 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4135 char __user *optval,
4138 struct sctp_paddrinfo pinfo;
4139 struct sctp_transport *transport;
4142 if (len < sizeof(pinfo)) {
4147 len = sizeof(pinfo);
4148 if (copy_from_user(&pinfo, optval, len)) {
4153 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4154 pinfo.spinfo_assoc_id);
4158 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
4159 pinfo.spinfo_state = transport->state;
4160 pinfo.spinfo_cwnd = transport->cwnd;
4161 pinfo.spinfo_srtt = transport->srtt;
4162 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
4163 pinfo.spinfo_mtu = transport->pathmtu;
4165 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4166 pinfo.spinfo_state = SCTP_ACTIVE;
4168 if (put_user(len, optlen)) {
4173 if (copy_to_user(optval, &pinfo, len)) {
4182 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4184 * This option is a on/off flag. If enabled no SCTP message
4185 * fragmentation will be performed. Instead if a message being sent
4186 * exceeds the current PMTU size, the message will NOT be sent and
4187 * instead a error will be indicated to the user.
4189 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4190 char __user *optval, int __user *optlen)
4194 if (len < sizeof(int))
4198 val = (sctp_sk(sk)->disable_fragments == 1);
4199 if (put_user(len, optlen))
4201 if (copy_to_user(optval, &val, len))
4206 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4208 * This socket option is used to specify various notifications and
4209 * ancillary data the user wishes to receive.
4211 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4216 if (len > sizeof(struct sctp_event_subscribe))
4217 len = sizeof(struct sctp_event_subscribe);
4218 if (put_user(len, optlen))
4220 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4225 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4227 * This socket option is applicable to the UDP-style socket only. When
4228 * set it will cause associations that are idle for more than the
4229 * specified number of seconds to automatically close. An association
4230 * being idle is defined an association that has NOT sent or received
4231 * user data. The special value of '0' indicates that no automatic
4232 * close of any associations should be performed. The option expects an
4233 * integer defining the number of seconds of idle time before an
4234 * association is closed.
4236 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4238 /* Applicable to UDP-style socket only */
4239 if (sctp_style(sk, TCP))
4241 if (len < sizeof(int))
4244 if (put_user(len, optlen))
4246 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
4251 /* Helper routine to branch off an association to a new socket. */
4252 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
4254 struct sctp_association *asoc = sctp_id2assoc(sk, id);
4255 struct socket *sock;
4262 /* An association cannot be branched off from an already peeled-off
4263 * socket, nor is this supported for tcp style sockets.
4265 if (!sctp_style(sk, UDP))
4268 /* Create a new socket. */
4269 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4273 sctp_copy_sock(sock->sk, sk, asoc);
4275 /* Make peeled-off sockets more like 1-1 accepted sockets.
4276 * Set the daddr and initialize id to something more random
4278 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4279 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
4281 /* Populate the fields of the newsk from the oldsk and migrate the
4282 * asoc to the newsk.
4284 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
4290 EXPORT_SYMBOL(sctp_do_peeloff);
4292 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4294 sctp_peeloff_arg_t peeloff;
4295 struct socket *newsock;
4296 struct file *newfile;
4299 if (len < sizeof(sctp_peeloff_arg_t))
4301 len = sizeof(sctp_peeloff_arg_t);
4302 if (copy_from_user(&peeloff, optval, len))
4305 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
4309 /* Map the socket to an unused fd that can be returned to the user. */
4310 retval = get_unused_fd();
4312 sock_release(newsock);
4316 newfile = sock_alloc_file(newsock, 0, NULL);
4317 if (unlikely(IS_ERR(newfile))) {
4318 put_unused_fd(retval);
4319 sock_release(newsock);
4320 return PTR_ERR(newfile);
4323 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4324 __func__, sk, newsock->sk, retval);
4326 /* Return the fd mapped to the new socket. */
4327 if (put_user(len, optlen)) {
4329 put_unused_fd(retval);
4332 peeloff.sd = retval;
4333 if (copy_to_user(optval, &peeloff, len)) {
4335 put_unused_fd(retval);
4338 fd_install(retval, newfile);
4343 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4345 * Applications can enable or disable heartbeats for any peer address of
4346 * an association, modify an address's heartbeat interval, force a
4347 * heartbeat to be sent immediately, and adjust the address's maximum
4348 * number of retransmissions sent before an address is considered
4349 * unreachable. The following structure is used to access and modify an
4350 * address's parameters:
4352 * struct sctp_paddrparams {
4353 * sctp_assoc_t spp_assoc_id;
4354 * struct sockaddr_storage spp_address;
4355 * uint32_t spp_hbinterval;
4356 * uint16_t spp_pathmaxrxt;
4357 * uint32_t spp_pathmtu;
4358 * uint32_t spp_sackdelay;
4359 * uint32_t spp_flags;
4362 * spp_assoc_id - (one-to-many style socket) This is filled in the
4363 * application, and identifies the association for
4365 * spp_address - This specifies which address is of interest.
4366 * spp_hbinterval - This contains the value of the heartbeat interval,
4367 * in milliseconds. If a value of zero
4368 * is present in this field then no changes are to
4369 * be made to this parameter.
4370 * spp_pathmaxrxt - This contains the maximum number of
4371 * retransmissions before this address shall be
4372 * considered unreachable. If a value of zero
4373 * is present in this field then no changes are to
4374 * be made to this parameter.
4375 * spp_pathmtu - When Path MTU discovery is disabled the value
4376 * specified here will be the "fixed" path mtu.
4377 * Note that if the spp_address field is empty
4378 * then all associations on this address will
4379 * have this fixed path mtu set upon them.
4381 * spp_sackdelay - When delayed sack is enabled, this value specifies
4382 * the number of milliseconds that sacks will be delayed
4383 * for. This value will apply to all addresses of an
4384 * association if the spp_address field is empty. Note
4385 * also, that if delayed sack is enabled and this
4386 * value is set to 0, no change is made to the last
4387 * recorded delayed sack timer value.
4389 * spp_flags - These flags are used to control various features
4390 * on an association. The flag field may contain
4391 * zero or more of the following options.
4393 * SPP_HB_ENABLE - Enable heartbeats on the
4394 * specified address. Note that if the address
4395 * field is empty all addresses for the association
4396 * have heartbeats enabled upon them.
4398 * SPP_HB_DISABLE - Disable heartbeats on the
4399 * speicifed address. Note that if the address
4400 * field is empty all addresses for the association
4401 * will have their heartbeats disabled. Note also
4402 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4403 * mutually exclusive, only one of these two should
4404 * be specified. Enabling both fields will have
4405 * undetermined results.
4407 * SPP_HB_DEMAND - Request a user initiated heartbeat
4408 * to be made immediately.
4410 * SPP_PMTUD_ENABLE - This field will enable PMTU
4411 * discovery upon the specified address. Note that
4412 * if the address feild is empty then all addresses
4413 * on the association are effected.
4415 * SPP_PMTUD_DISABLE - This field will disable PMTU
4416 * discovery upon the specified address. Note that
4417 * if the address feild is empty then all addresses
4418 * on the association are effected. Not also that
4419 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4420 * exclusive. Enabling both will have undetermined
4423 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4424 * on delayed sack. The time specified in spp_sackdelay
4425 * is used to specify the sack delay for this address. Note
4426 * that if spp_address is empty then all addresses will
4427 * enable delayed sack and take on the sack delay
4428 * value specified in spp_sackdelay.
4429 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4430 * off delayed sack. If the spp_address field is blank then
4431 * delayed sack is disabled for the entire association. Note
4432 * also that this field is mutually exclusive to
4433 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4436 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
4437 char __user *optval, int __user *optlen)
4439 struct sctp_paddrparams params;
4440 struct sctp_transport *trans = NULL;
4441 struct sctp_association *asoc = NULL;
4442 struct sctp_sock *sp = sctp_sk(sk);
4444 if (len < sizeof(struct sctp_paddrparams))
4446 len = sizeof(struct sctp_paddrparams);
4447 if (copy_from_user(¶ms, optval, len))
4450 /* If an address other than INADDR_ANY is specified, and
4451 * no transport is found, then the request is invalid.
4453 if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) {
4454 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
4455 params.spp_assoc_id);
4457 SCTP_DEBUG_PRINTK("Failed no transport\n");
4462 /* Get association, if assoc_id != 0 and the socket is a one
4463 * to many style socket, and an association was not found, then
4464 * the id was invalid.
4466 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4467 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4468 SCTP_DEBUG_PRINTK("Failed no association\n");
4473 /* Fetch transport values. */
4474 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4475 params.spp_pathmtu = trans->pathmtu;
4476 params.spp_pathmaxrxt = trans->pathmaxrxt;
4477 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
4479 /*draft-11 doesn't say what to return in spp_flags*/
4480 params.spp_flags = trans->param_flags;
4482 /* Fetch association values. */
4483 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4484 params.spp_pathmtu = asoc->pathmtu;
4485 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4486 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
4488 /*draft-11 doesn't say what to return in spp_flags*/
4489 params.spp_flags = asoc->param_flags;
4491 /* Fetch socket values. */
4492 params.spp_hbinterval = sp->hbinterval;
4493 params.spp_pathmtu = sp->pathmtu;
4494 params.spp_sackdelay = sp->sackdelay;
4495 params.spp_pathmaxrxt = sp->pathmaxrxt;
4497 /*draft-11 doesn't say what to return in spp_flags*/
4498 params.spp_flags = sp->param_flags;
4501 if (copy_to_user(optval, ¶ms, len))
4504 if (put_user(len, optlen))
4511 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
4513 * This option will effect the way delayed acks are performed. This
4514 * option allows you to get or set the delayed ack time, in
4515 * milliseconds. It also allows changing the delayed ack frequency.
4516 * Changing the frequency to 1 disables the delayed sack algorithm. If
4517 * the assoc_id is 0, then this sets or gets the endpoints default
4518 * values. If the assoc_id field is non-zero, then the set or get
4519 * effects the specified association for the one to many model (the
4520 * assoc_id field is ignored by the one to one model). Note that if
4521 * sack_delay or sack_freq are 0 when setting this option, then the
4522 * current values will remain unchanged.
4524 * struct sctp_sack_info {
4525 * sctp_assoc_t sack_assoc_id;
4526 * uint32_t sack_delay;
4527 * uint32_t sack_freq;
4530 * sack_assoc_id - This parameter, indicates which association the user
4531 * is performing an action upon. Note that if this field's value is
4532 * zero then the endpoints default value is changed (effecting future
4533 * associations only).
4535 * sack_delay - This parameter contains the number of milliseconds that
4536 * the user is requesting the delayed ACK timer be set to. Note that
4537 * this value is defined in the standard to be between 200 and 500
4540 * sack_freq - This parameter contains the number of packets that must
4541 * be received before a sack is sent without waiting for the delay
4542 * timer to expire. The default value for this is 2, setting this
4543 * value to 1 will disable the delayed sack algorithm.
4545 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
4546 char __user *optval,
4549 struct sctp_sack_info params;
4550 struct sctp_association *asoc = NULL;
4551 struct sctp_sock *sp = sctp_sk(sk);
4553 if (len >= sizeof(struct sctp_sack_info)) {
4554 len = sizeof(struct sctp_sack_info);
4556 if (copy_from_user(¶ms, optval, len))
4558 } else if (len == sizeof(struct sctp_assoc_value)) {
4559 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4560 pr_warn("Use struct sctp_sack_info instead\n");
4561 if (copy_from_user(¶ms, optval, len))
4566 /* Get association, if sack_assoc_id != 0 and the socket is a one
4567 * to many style socket, and an association was not found, then
4568 * the id was invalid.
4570 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4571 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
4575 /* Fetch association values. */
4576 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4577 params.sack_delay = jiffies_to_msecs(
4579 params.sack_freq = asoc->sackfreq;
4582 params.sack_delay = 0;
4583 params.sack_freq = 1;
4586 /* Fetch socket values. */
4587 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4588 params.sack_delay = sp->sackdelay;
4589 params.sack_freq = sp->sackfreq;
4591 params.sack_delay = 0;
4592 params.sack_freq = 1;
4596 if (copy_to_user(optval, ¶ms, len))
4599 if (put_user(len, optlen))
4605 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4607 * Applications can specify protocol parameters for the default association
4608 * initialization. The option name argument to setsockopt() and getsockopt()
4611 * Setting initialization parameters is effective only on an unconnected
4612 * socket (for UDP-style sockets only future associations are effected
4613 * by the change). With TCP-style sockets, this option is inherited by
4614 * sockets derived from a listener socket.
4616 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4618 if (len < sizeof(struct sctp_initmsg))
4620 len = sizeof(struct sctp_initmsg);
4621 if (put_user(len, optlen))
4623 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4629 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4630 char __user *optval, int __user *optlen)
4632 struct sctp_association *asoc;
4634 struct sctp_getaddrs getaddrs;
4635 struct sctp_transport *from;
4637 union sctp_addr temp;
4638 struct sctp_sock *sp = sctp_sk(sk);
4643 if (len < sizeof(struct sctp_getaddrs))
4646 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4649 /* For UDP-style sockets, id specifies the association to query. */
4650 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4654 to = optval + offsetof(struct sctp_getaddrs,addrs);
4655 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4657 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4659 memcpy(&temp, &from->ipaddr, sizeof(temp));
4660 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4661 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4662 if (space_left < addrlen)
4664 if (copy_to_user(to, &temp, addrlen))
4668 space_left -= addrlen;
4671 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4673 bytes_copied = ((char __user *)to) - optval;
4674 if (put_user(bytes_copied, optlen))
4680 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4681 size_t space_left, int *bytes_copied)
4683 struct sctp_sockaddr_entry *addr;
4684 union sctp_addr temp;
4687 struct net *net = sock_net(sk);
4690 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
4694 if ((PF_INET == sk->sk_family) &&
4695 (AF_INET6 == addr->a.sa.sa_family))
4697 if ((PF_INET6 == sk->sk_family) &&
4698 inet_v6_ipv6only(sk) &&
4699 (AF_INET == addr->a.sa.sa_family))
4701 memcpy(&temp, &addr->a, sizeof(temp));
4702 if (!temp.v4.sin_port)
4703 temp.v4.sin_port = htons(port);
4705 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4707 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4708 if (space_left < addrlen) {
4712 memcpy(to, &temp, addrlen);
4716 space_left -= addrlen;
4717 *bytes_copied += addrlen;
4725 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4726 char __user *optval, int __user *optlen)
4728 struct sctp_bind_addr *bp;
4729 struct sctp_association *asoc;
4731 struct sctp_getaddrs getaddrs;
4732 struct sctp_sockaddr_entry *addr;
4734 union sctp_addr temp;
4735 struct sctp_sock *sp = sctp_sk(sk);
4739 int bytes_copied = 0;
4743 if (len < sizeof(struct sctp_getaddrs))
4746 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4750 * For UDP-style sockets, id specifies the association to query.
4751 * If the id field is set to the value '0' then the locally bound
4752 * addresses are returned without regard to any particular
4755 if (0 == getaddrs.assoc_id) {
4756 bp = &sctp_sk(sk)->ep->base.bind_addr;
4758 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4761 bp = &asoc->base.bind_addr;
4764 to = optval + offsetof(struct sctp_getaddrs,addrs);
4765 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4767 addrs = kmalloc(space_left, GFP_KERNEL);
4771 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4772 * addresses from the global local address list.
4774 if (sctp_list_single_entry(&bp->address_list)) {
4775 addr = list_entry(bp->address_list.next,
4776 struct sctp_sockaddr_entry, list);
4777 if (sctp_is_any(sk, &addr->a)) {
4778 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4779 space_left, &bytes_copied);
4789 /* Protection on the bound address list is not needed since
4790 * in the socket option context we hold a socket lock and
4791 * thus the bound address list can't change.
4793 list_for_each_entry(addr, &bp->address_list, list) {
4794 memcpy(&temp, &addr->a, sizeof(temp));
4795 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4796 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4797 if (space_left < addrlen) {
4798 err = -ENOMEM; /*fixme: right error?*/
4801 memcpy(buf, &temp, addrlen);
4803 bytes_copied += addrlen;
4805 space_left -= addrlen;
4809 if (copy_to_user(to, addrs, bytes_copied)) {
4813 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4817 if (put_user(bytes_copied, optlen))
4824 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4826 * Requests that the local SCTP stack use the enclosed peer address as
4827 * the association primary. The enclosed address must be one of the
4828 * association peer's addresses.
4830 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4831 char __user *optval, int __user *optlen)
4833 struct sctp_prim prim;
4834 struct sctp_association *asoc;
4835 struct sctp_sock *sp = sctp_sk(sk);
4837 if (len < sizeof(struct sctp_prim))
4840 len = sizeof(struct sctp_prim);
4842 if (copy_from_user(&prim, optval, len))
4845 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4849 if (!asoc->peer.primary_path)
4852 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4853 asoc->peer.primary_path->af_specific->sockaddr_len);
4855 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4856 (union sctp_addr *)&prim.ssp_addr);
4858 if (put_user(len, optlen))
4860 if (copy_to_user(optval, &prim, len))
4867 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4869 * Requests that the local endpoint set the specified Adaptation Layer
4870 * Indication parameter for all future INIT and INIT-ACK exchanges.
4872 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4873 char __user *optval, int __user *optlen)
4875 struct sctp_setadaptation adaptation;
4877 if (len < sizeof(struct sctp_setadaptation))
4880 len = sizeof(struct sctp_setadaptation);
4882 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4884 if (put_user(len, optlen))
4886 if (copy_to_user(optval, &adaptation, len))
4894 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4896 * Applications that wish to use the sendto() system call may wish to
4897 * specify a default set of parameters that would normally be supplied
4898 * through the inclusion of ancillary data. This socket option allows
4899 * such an application to set the default sctp_sndrcvinfo structure.
4902 * The application that wishes to use this socket option simply passes
4903 * in to this call the sctp_sndrcvinfo structure defined in Section
4904 * 5.2.2) The input parameters accepted by this call include
4905 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4906 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4907 * to this call if the caller is using the UDP model.
4909 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4911 static int sctp_getsockopt_default_send_param(struct sock *sk,
4912 int len, char __user *optval,
4915 struct sctp_sndrcvinfo info;
4916 struct sctp_association *asoc;
4917 struct sctp_sock *sp = sctp_sk(sk);
4919 if (len < sizeof(struct sctp_sndrcvinfo))
4922 len = sizeof(struct sctp_sndrcvinfo);
4924 if (copy_from_user(&info, optval, len))
4927 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4928 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4932 info.sinfo_stream = asoc->default_stream;
4933 info.sinfo_flags = asoc->default_flags;
4934 info.sinfo_ppid = asoc->default_ppid;
4935 info.sinfo_context = asoc->default_context;
4936 info.sinfo_timetolive = asoc->default_timetolive;
4938 info.sinfo_stream = sp->default_stream;
4939 info.sinfo_flags = sp->default_flags;
4940 info.sinfo_ppid = sp->default_ppid;
4941 info.sinfo_context = sp->default_context;
4942 info.sinfo_timetolive = sp->default_timetolive;
4945 if (put_user(len, optlen))
4947 if (copy_to_user(optval, &info, len))
4955 * 7.1.5 SCTP_NODELAY
4957 * Turn on/off any Nagle-like algorithm. This means that packets are
4958 * generally sent as soon as possible and no unnecessary delays are
4959 * introduced, at the cost of more packets in the network. Expects an
4960 * integer boolean flag.
4963 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4964 char __user *optval, int __user *optlen)
4968 if (len < sizeof(int))
4972 val = (sctp_sk(sk)->nodelay == 1);
4973 if (put_user(len, optlen))
4975 if (copy_to_user(optval, &val, len))
4982 * 7.1.1 SCTP_RTOINFO
4984 * The protocol parameters used to initialize and bound retransmission
4985 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4986 * and modify these parameters.
4987 * All parameters are time values, in milliseconds. A value of 0, when
4988 * modifying the parameters, indicates that the current value should not
4992 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4993 char __user *optval,
4994 int __user *optlen) {
4995 struct sctp_rtoinfo rtoinfo;
4996 struct sctp_association *asoc;
4998 if (len < sizeof (struct sctp_rtoinfo))
5001 len = sizeof(struct sctp_rtoinfo);
5003 if (copy_from_user(&rtoinfo, optval, len))
5006 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5008 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5011 /* Values corresponding to the specific association. */
5013 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5014 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5015 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5017 /* Values corresponding to the endpoint. */
5018 struct sctp_sock *sp = sctp_sk(sk);
5020 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5021 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5022 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5025 if (put_user(len, optlen))
5028 if (copy_to_user(optval, &rtoinfo, len))
5036 * 7.1.2 SCTP_ASSOCINFO
5038 * This option is used to tune the maximum retransmission attempts
5039 * of the association.
5040 * Returns an error if the new association retransmission value is
5041 * greater than the sum of the retransmission value of the peer.
5042 * See [SCTP] for more information.
5045 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5046 char __user *optval,
5050 struct sctp_assocparams assocparams;
5051 struct sctp_association *asoc;
5052 struct list_head *pos;
5055 if (len < sizeof (struct sctp_assocparams))
5058 len = sizeof(struct sctp_assocparams);
5060 if (copy_from_user(&assocparams, optval, len))
5063 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5065 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5068 /* Values correspoinding to the specific association */
5070 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5071 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5072 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5073 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5075 (asoc->cookie_life.tv_usec
5078 list_for_each(pos, &asoc->peer.transport_addr_list) {
5082 assocparams.sasoc_number_peer_destinations = cnt;
5084 /* Values corresponding to the endpoint */
5085 struct sctp_sock *sp = sctp_sk(sk);
5087 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5088 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5089 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5090 assocparams.sasoc_cookie_life =
5091 sp->assocparams.sasoc_cookie_life;
5092 assocparams.sasoc_number_peer_destinations =
5094 sasoc_number_peer_destinations;
5097 if (put_user(len, optlen))
5100 if (copy_to_user(optval, &assocparams, len))
5107 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5109 * This socket option is a boolean flag which turns on or off mapped V4
5110 * addresses. If this option is turned on and the socket is type
5111 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5112 * If this option is turned off, then no mapping will be done of V4
5113 * addresses and a user will receive both PF_INET6 and PF_INET type
5114 * addresses on the socket.
5116 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5117 char __user *optval, int __user *optlen)
5120 struct sctp_sock *sp = sctp_sk(sk);
5122 if (len < sizeof(int))
5127 if (put_user(len, optlen))
5129 if (copy_to_user(optval, &val, len))
5136 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5137 * (chapter and verse is quoted at sctp_setsockopt_context())
5139 static int sctp_getsockopt_context(struct sock *sk, int len,
5140 char __user *optval, int __user *optlen)
5142 struct sctp_assoc_value params;
5143 struct sctp_sock *sp;
5144 struct sctp_association *asoc;
5146 if (len < sizeof(struct sctp_assoc_value))
5149 len = sizeof(struct sctp_assoc_value);
5151 if (copy_from_user(¶ms, optval, len))
5156 if (params.assoc_id != 0) {
5157 asoc = sctp_id2assoc(sk, params.assoc_id);
5160 params.assoc_value = asoc->default_rcv_context;
5162 params.assoc_value = sp->default_rcv_context;
5165 if (put_user(len, optlen))
5167 if (copy_to_user(optval, ¶ms, len))
5174 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5175 * This option will get or set the maximum size to put in any outgoing
5176 * SCTP DATA chunk. If a message is larger than this size it will be
5177 * fragmented by SCTP into the specified size. Note that the underlying
5178 * SCTP implementation may fragment into smaller sized chunks when the
5179 * PMTU of the underlying association is smaller than the value set by
5180 * the user. The default value for this option is '0' which indicates
5181 * the user is NOT limiting fragmentation and only the PMTU will effect
5182 * SCTP's choice of DATA chunk size. Note also that values set larger
5183 * than the maximum size of an IP datagram will effectively let SCTP
5184 * control fragmentation (i.e. the same as setting this option to 0).
5186 * The following structure is used to access and modify this parameter:
5188 * struct sctp_assoc_value {
5189 * sctp_assoc_t assoc_id;
5190 * uint32_t assoc_value;
5193 * assoc_id: This parameter is ignored for one-to-one style sockets.
5194 * For one-to-many style sockets this parameter indicates which
5195 * association the user is performing an action upon. Note that if
5196 * this field's value is zero then the endpoints default value is
5197 * changed (effecting future associations only).
5198 * assoc_value: This parameter specifies the maximum size in bytes.
5200 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5201 char __user *optval, int __user *optlen)
5203 struct sctp_assoc_value params;
5204 struct sctp_association *asoc;
5206 if (len == sizeof(int)) {
5207 pr_warn("Use of int in maxseg socket option deprecated\n");
5208 pr_warn("Use struct sctp_assoc_value instead\n");
5209 params.assoc_id = 0;
5210 } else if (len >= sizeof(struct sctp_assoc_value)) {
5211 len = sizeof(struct sctp_assoc_value);
5212 if (copy_from_user(¶ms, optval, sizeof(params)))
5217 asoc = sctp_id2assoc(sk, params.assoc_id);
5218 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5222 params.assoc_value = asoc->frag_point;
5224 params.assoc_value = sctp_sk(sk)->user_frag;
5226 if (put_user(len, optlen))
5228 if (len == sizeof(int)) {
5229 if (copy_to_user(optval, ¶ms.assoc_value, len))
5232 if (copy_to_user(optval, ¶ms, len))
5240 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5241 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5243 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5244 char __user *optval, int __user *optlen)
5248 if (len < sizeof(int))
5253 val = sctp_sk(sk)->frag_interleave;
5254 if (put_user(len, optlen))
5256 if (copy_to_user(optval, &val, len))
5263 * 7.1.25. Set or Get the sctp partial delivery point
5264 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5266 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5267 char __user *optval,
5272 if (len < sizeof(u32))
5277 val = sctp_sk(sk)->pd_point;
5278 if (put_user(len, optlen))
5280 if (copy_to_user(optval, &val, len))
5287 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5288 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5290 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5291 char __user *optval,
5294 struct sctp_assoc_value params;
5295 struct sctp_sock *sp;
5296 struct sctp_association *asoc;
5298 if (len == sizeof(int)) {
5299 pr_warn("Use of int in max_burst socket option deprecated\n");
5300 pr_warn("Use struct sctp_assoc_value instead\n");
5301 params.assoc_id = 0;
5302 } else if (len >= sizeof(struct sctp_assoc_value)) {
5303 len = sizeof(struct sctp_assoc_value);
5304 if (copy_from_user(¶ms, optval, len))
5311 if (params.assoc_id != 0) {
5312 asoc = sctp_id2assoc(sk, params.assoc_id);
5315 params.assoc_value = asoc->max_burst;
5317 params.assoc_value = sp->max_burst;
5319 if (len == sizeof(int)) {
5320 if (copy_to_user(optval, ¶ms.assoc_value, len))
5323 if (copy_to_user(optval, ¶ms, len))
5331 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5332 char __user *optval, int __user *optlen)
5334 struct net *net = sock_net(sk);
5335 struct sctp_hmacalgo __user *p = (void __user *)optval;
5336 struct sctp_hmac_algo_param *hmacs;
5340 if (!net->sctp.auth_enable)
5343 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5344 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
5346 if (len < sizeof(struct sctp_hmacalgo) + data_len)
5349 len = sizeof(struct sctp_hmacalgo) + data_len;
5350 num_idents = data_len / sizeof(u16);
5352 if (put_user(len, optlen))
5354 if (put_user(num_idents, &p->shmac_num_idents))
5356 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5361 static int sctp_getsockopt_active_key(struct sock *sk, int len,
5362 char __user *optval, int __user *optlen)
5364 struct net *net = sock_net(sk);
5365 struct sctp_authkeyid val;
5366 struct sctp_association *asoc;
5368 if (!net->sctp.auth_enable)
5371 if (len < sizeof(struct sctp_authkeyid))
5373 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5376 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5377 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5381 val.scact_keynumber = asoc->active_key_id;
5383 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5385 len = sizeof(struct sctp_authkeyid);
5386 if (put_user(len, optlen))
5388 if (copy_to_user(optval, &val, len))
5394 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5395 char __user *optval, int __user *optlen)
5397 struct net *net = sock_net(sk);
5398 struct sctp_authchunks __user *p = (void __user *)optval;
5399 struct sctp_authchunks val;
5400 struct sctp_association *asoc;
5401 struct sctp_chunks_param *ch;
5405 if (!net->sctp.auth_enable)
5408 if (len < sizeof(struct sctp_authchunks))
5411 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5414 to = p->gauth_chunks;
5415 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5419 ch = asoc->peer.peer_chunks;
5423 /* See if the user provided enough room for all the data */
5424 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5425 if (len < num_chunks)
5428 if (copy_to_user(to, ch->chunks, num_chunks))
5431 len = sizeof(struct sctp_authchunks) + num_chunks;
5432 if (put_user(len, optlen)) return -EFAULT;
5433 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5438 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5439 char __user *optval, int __user *optlen)
5441 struct net *net = sock_net(sk);
5442 struct sctp_authchunks __user *p = (void __user *)optval;
5443 struct sctp_authchunks val;
5444 struct sctp_association *asoc;
5445 struct sctp_chunks_param *ch;
5449 if (!net->sctp.auth_enable)
5452 if (len < sizeof(struct sctp_authchunks))
5455 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5458 to = p->gauth_chunks;
5459 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5460 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5464 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5466 ch = sctp_sk(sk)->ep->auth_chunk_list;
5471 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5472 if (len < sizeof(struct sctp_authchunks) + num_chunks)
5475 if (copy_to_user(to, ch->chunks, num_chunks))
5478 len = sizeof(struct sctp_authchunks) + num_chunks;
5479 if (put_user(len, optlen))
5481 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5488 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5489 * This option gets the current number of associations that are attached
5490 * to a one-to-many style socket. The option value is an uint32_t.
5492 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5493 char __user *optval, int __user *optlen)
5495 struct sctp_sock *sp = sctp_sk(sk);
5496 struct sctp_association *asoc;
5499 if (sctp_style(sk, TCP))
5502 if (len < sizeof(u32))
5507 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5511 if (put_user(len, optlen))
5513 if (copy_to_user(optval, &val, len))
5520 * 8.1.23 SCTP_AUTO_ASCONF
5521 * See the corresponding setsockopt entry as description
5523 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
5524 char __user *optval, int __user *optlen)
5528 if (len < sizeof(int))
5532 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
5534 if (put_user(len, optlen))
5536 if (copy_to_user(optval, &val, len))
5542 * 8.2.6. Get the Current Identifiers of Associations
5543 * (SCTP_GET_ASSOC_ID_LIST)
5545 * This option gets the current list of SCTP association identifiers of
5546 * the SCTP associations handled by a one-to-many style socket.
5548 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
5549 char __user *optval, int __user *optlen)
5551 struct sctp_sock *sp = sctp_sk(sk);
5552 struct sctp_association *asoc;
5553 struct sctp_assoc_ids *ids;
5556 if (sctp_style(sk, TCP))
5559 if (len < sizeof(struct sctp_assoc_ids))
5562 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5566 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
5569 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
5571 ids = kmalloc(len, GFP_KERNEL);
5575 ids->gaids_number_of_ids = num;
5577 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5578 ids->gaids_assoc_id[num++] = asoc->assoc_id;
5581 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
5591 * SCTP_PEER_ADDR_THLDS
5593 * This option allows us to fetch the partially failed threshold for one or all
5594 * transports in an association. See Section 6.1 of:
5595 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
5597 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
5598 char __user *optval,
5602 struct sctp_paddrthlds val;
5603 struct sctp_transport *trans;
5604 struct sctp_association *asoc;
5606 if (len < sizeof(struct sctp_paddrthlds))
5608 len = sizeof(struct sctp_paddrthlds);
5609 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
5612 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
5613 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
5617 val.spt_pathpfthld = asoc->pf_retrans;
5618 val.spt_pathmaxrxt = asoc->pathmaxrxt;
5620 trans = sctp_addr_id2transport(sk, &val.spt_address,
5625 val.spt_pathmaxrxt = trans->pathmaxrxt;
5626 val.spt_pathpfthld = trans->pf_retrans;
5629 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
5635 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5636 char __user *optval, int __user *optlen)
5641 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5644 /* I can hardly begin to describe how wrong this is. This is
5645 * so broken as to be worse than useless. The API draft
5646 * REALLY is NOT helpful here... I am not convinced that the
5647 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5648 * are at all well-founded.
5650 if (level != SOL_SCTP) {
5651 struct sctp_af *af = sctp_sk(sk)->pf->af;
5653 retval = af->getsockopt(sk, level, optname, optval, optlen);
5657 if (get_user(len, optlen))
5664 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5666 case SCTP_DISABLE_FRAGMENTS:
5667 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5671 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5673 case SCTP_AUTOCLOSE:
5674 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5676 case SCTP_SOCKOPT_PEELOFF:
5677 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5679 case SCTP_PEER_ADDR_PARAMS:
5680 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5683 case SCTP_DELAYED_SACK:
5684 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
5688 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5690 case SCTP_GET_PEER_ADDRS:
5691 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5694 case SCTP_GET_LOCAL_ADDRS:
5695 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5698 case SCTP_SOCKOPT_CONNECTX3:
5699 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5701 case SCTP_DEFAULT_SEND_PARAM:
5702 retval = sctp_getsockopt_default_send_param(sk, len,
5705 case SCTP_PRIMARY_ADDR:
5706 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5709 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5712 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5714 case SCTP_ASSOCINFO:
5715 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5717 case SCTP_I_WANT_MAPPED_V4_ADDR:
5718 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5721 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5723 case SCTP_GET_PEER_ADDR_INFO:
5724 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5727 case SCTP_ADAPTATION_LAYER:
5728 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
5732 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5734 case SCTP_FRAGMENT_INTERLEAVE:
5735 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5738 case SCTP_PARTIAL_DELIVERY_POINT:
5739 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5742 case SCTP_MAX_BURST:
5743 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5746 case SCTP_AUTH_CHUNK:
5747 case SCTP_AUTH_DELETE_KEY:
5748 retval = -EOPNOTSUPP;
5750 case SCTP_HMAC_IDENT:
5751 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5753 case SCTP_AUTH_ACTIVE_KEY:
5754 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5756 case SCTP_PEER_AUTH_CHUNKS:
5757 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5760 case SCTP_LOCAL_AUTH_CHUNKS:
5761 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5764 case SCTP_GET_ASSOC_NUMBER:
5765 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5767 case SCTP_GET_ASSOC_ID_LIST:
5768 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5770 case SCTP_AUTO_ASCONF:
5771 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5773 case SCTP_PEER_ADDR_THLDS:
5774 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
5777 retval = -ENOPROTOOPT;
5781 sctp_release_sock(sk);
5785 static void sctp_hash(struct sock *sk)
5790 static void sctp_unhash(struct sock *sk)
5795 /* Check if port is acceptable. Possibly find first available port.
5797 * The port hash table (contained in the 'global' SCTP protocol storage
5798 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5799 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5800 * list (the list number is the port number hashed out, so as you
5801 * would expect from a hash function, all the ports in a given list have
5802 * such a number that hashes out to the same list number; you were
5803 * expecting that, right?); so each list has a set of ports, with a
5804 * link to the socket (struct sock) that uses it, the port number and
5805 * a fastreuse flag (FIXME: NPI ipg).
5807 static struct sctp_bind_bucket *sctp_bucket_create(
5808 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
5810 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5812 struct sctp_bind_hashbucket *head; /* hash list */
5813 struct sctp_bind_bucket *pp; /* hash list port iterator */
5814 struct hlist_node *node;
5815 unsigned short snum;
5818 snum = ntohs(addr->v4.sin_port);
5820 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5821 sctp_local_bh_disable();
5824 /* Search for an available port. */
5825 int low, high, remaining, index;
5828 inet_get_local_port_range(&low, &high);
5829 remaining = (high - low) + 1;
5830 rover = net_random() % remaining + low;
5834 if ((rover < low) || (rover > high))
5836 if (inet_is_reserved_local_port(rover))
5838 index = sctp_phashfn(sock_net(sk), rover);
5839 head = &sctp_port_hashtable[index];
5840 sctp_spin_lock(&head->lock);
5841 sctp_for_each_hentry(pp, node, &head->chain)
5842 if ((pp->port == rover) &&
5843 net_eq(sock_net(sk), pp->net))
5847 sctp_spin_unlock(&head->lock);
5848 } while (--remaining > 0);
5850 /* Exhausted local port range during search? */
5855 /* OK, here is the one we will use. HEAD (the port
5856 * hash table list entry) is non-NULL and we hold it's
5861 /* We are given an specific port number; we verify
5862 * that it is not being used. If it is used, we will
5863 * exahust the search in the hash list corresponding
5864 * to the port number (snum) - we detect that with the
5865 * port iterator, pp being NULL.
5867 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
5868 sctp_spin_lock(&head->lock);
5869 sctp_for_each_hentry(pp, node, &head->chain) {
5870 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
5877 if (!hlist_empty(&pp->owner)) {
5878 /* We had a port hash table hit - there is an
5879 * available port (pp != NULL) and it is being
5880 * used by other socket (pp->owner not empty); that other
5881 * socket is going to be sk2.
5883 int reuse = sk->sk_reuse;
5886 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5887 if (pp->fastreuse && sk->sk_reuse &&
5888 sk->sk_state != SCTP_SS_LISTENING)
5891 /* Run through the list of sockets bound to the port
5892 * (pp->port) [via the pointers bind_next and
5893 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5894 * we get the endpoint they describe and run through
5895 * the endpoint's list of IP (v4 or v6) addresses,
5896 * comparing each of the addresses with the address of
5897 * the socket sk. If we find a match, then that means
5898 * that this port/socket (sk) combination are already
5901 sk_for_each_bound(sk2, node, &pp->owner) {
5902 struct sctp_endpoint *ep2;
5903 ep2 = sctp_sk(sk2)->ep;
5906 (reuse && sk2->sk_reuse &&
5907 sk2->sk_state != SCTP_SS_LISTENING))
5910 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5911 sctp_sk(sk2), sctp_sk(sk))) {
5916 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5919 /* If there was a hash table miss, create a new port. */
5921 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
5924 /* In either case (hit or miss), make sure fastreuse is 1 only
5925 * if sk->sk_reuse is too (that is, if the caller requested
5926 * SO_REUSEADDR on this socket -sk-).
5928 if (hlist_empty(&pp->owner)) {
5929 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5933 } else if (pp->fastreuse &&
5934 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5937 /* We are set, so fill up all the data in the hash table
5938 * entry, tie the socket list information with the rest of the
5939 * sockets FIXME: Blurry, NPI (ipg).
5942 if (!sctp_sk(sk)->bind_hash) {
5943 inet_sk(sk)->inet_num = snum;
5944 sk_add_bind_node(sk, &pp->owner);
5945 sctp_sk(sk)->bind_hash = pp;
5950 sctp_spin_unlock(&head->lock);
5953 sctp_local_bh_enable();
5957 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5958 * port is requested.
5960 static int sctp_get_port(struct sock *sk, unsigned short snum)
5963 union sctp_addr addr;
5964 struct sctp_af *af = sctp_sk(sk)->pf->af;
5966 /* Set up a dummy address struct from the sk. */
5967 af->from_sk(&addr, sk);
5968 addr.v4.sin_port = htons(snum);
5970 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5971 ret = sctp_get_port_local(sk, &addr);
5977 * Move a socket to LISTENING state.
5979 SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
5981 struct sctp_sock *sp = sctp_sk(sk);
5982 struct sctp_endpoint *ep = sp->ep;
5983 struct crypto_hash *tfm = NULL;
5985 /* Allocate HMAC for generating cookie. */
5986 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
5987 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5989 net_info_ratelimited("failed to load transform for %s: %ld\n",
5990 sctp_hmac_alg, PTR_ERR(tfm));
5993 sctp_sk(sk)->hmac = tfm;
5997 * If a bind() or sctp_bindx() is not called prior to a listen()
5998 * call that allows new associations to be accepted, the system
5999 * picks an ephemeral port and will choose an address set equivalent
6000 * to binding with a wildcard address.
6002 * This is not currently spelled out in the SCTP sockets
6003 * extensions draft, but follows the practice as seen in TCP
6007 sk->sk_state = SCTP_SS_LISTENING;
6008 if (!ep->base.bind_addr.port) {
6009 if (sctp_autobind(sk))
6012 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
6013 sk->sk_state = SCTP_SS_CLOSED;
6018 sk->sk_max_ack_backlog = backlog;
6019 sctp_hash_endpoint(ep);
6024 * 4.1.3 / 5.1.3 listen()
6026 * By default, new associations are not accepted for UDP style sockets.
6027 * An application uses listen() to mark a socket as being able to
6028 * accept new associations.
6030 * On TCP style sockets, applications use listen() to ready the SCTP
6031 * endpoint for accepting inbound associations.
6033 * On both types of endpoints a backlog of '0' disables listening.
6035 * Move a socket to LISTENING state.
6037 int sctp_inet_listen(struct socket *sock, int backlog)
6039 struct sock *sk = sock->sk;
6040 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6043 if (unlikely(backlog < 0))
6048 /* Peeled-off sockets are not allowed to listen(). */
6049 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6052 if (sock->state != SS_UNCONNECTED)
6055 /* If backlog is zero, disable listening. */
6057 if (sctp_sstate(sk, CLOSED))
6061 sctp_unhash_endpoint(ep);
6062 sk->sk_state = SCTP_SS_CLOSED;
6064 sctp_sk(sk)->bind_hash->fastreuse = 1;
6068 /* If we are already listening, just update the backlog */
6069 if (sctp_sstate(sk, LISTENING))
6070 sk->sk_max_ack_backlog = backlog;
6072 err = sctp_listen_start(sk, backlog);
6079 sctp_release_sock(sk);
6084 * This function is done by modeling the current datagram_poll() and the
6085 * tcp_poll(). Note that, based on these implementations, we don't
6086 * lock the socket in this function, even though it seems that,
6087 * ideally, locking or some other mechanisms can be used to ensure
6088 * the integrity of the counters (sndbuf and wmem_alloc) used
6089 * in this place. We assume that we don't need locks either until proven
6092 * Another thing to note is that we include the Async I/O support
6093 * here, again, by modeling the current TCP/UDP code. We don't have
6094 * a good way to test with it yet.
6096 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6098 struct sock *sk = sock->sk;
6099 struct sctp_sock *sp = sctp_sk(sk);
6102 poll_wait(file, sk_sleep(sk), wait);
6104 /* A TCP-style listening socket becomes readable when the accept queue
6107 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6108 return (!list_empty(&sp->ep->asocs)) ?
6109 (POLLIN | POLLRDNORM) : 0;
6113 /* Is there any exceptional events? */
6114 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
6116 if (sk->sk_shutdown & RCV_SHUTDOWN)
6117 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
6118 if (sk->sk_shutdown == SHUTDOWN_MASK)
6121 /* Is it readable? Reconsider this code with TCP-style support. */
6122 if (!skb_queue_empty(&sk->sk_receive_queue))
6123 mask |= POLLIN | POLLRDNORM;
6125 /* The association is either gone or not ready. */
6126 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6129 /* Is it writable? */
6130 if (sctp_writeable(sk)) {
6131 mask |= POLLOUT | POLLWRNORM;
6133 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
6135 * Since the socket is not locked, the buffer
6136 * might be made available after the writeable check and
6137 * before the bit is set. This could cause a lost I/O
6138 * signal. tcp_poll() has a race breaker for this race
6139 * condition. Based on their implementation, we put
6140 * in the following code to cover it as well.
6142 if (sctp_writeable(sk))
6143 mask |= POLLOUT | POLLWRNORM;
6148 /********************************************************************
6149 * 2nd Level Abstractions
6150 ********************************************************************/
6152 static struct sctp_bind_bucket *sctp_bucket_create(
6153 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
6155 struct sctp_bind_bucket *pp;
6157 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
6159 SCTP_DBG_OBJCNT_INC(bind_bucket);
6162 INIT_HLIST_HEAD(&pp->owner);
6164 hlist_add_head(&pp->node, &head->chain);
6169 /* Caller must hold hashbucket lock for this tb with local BH disabled */
6170 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
6172 if (pp && hlist_empty(&pp->owner)) {
6173 __hlist_del(&pp->node);
6174 kmem_cache_free(sctp_bucket_cachep, pp);
6175 SCTP_DBG_OBJCNT_DEC(bind_bucket);
6179 /* Release this socket's reference to a local port. */
6180 static inline void __sctp_put_port(struct sock *sk)
6182 struct sctp_bind_hashbucket *head =
6183 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
6184 inet_sk(sk)->inet_num)];
6185 struct sctp_bind_bucket *pp;
6187 sctp_spin_lock(&head->lock);
6188 pp = sctp_sk(sk)->bind_hash;
6189 __sk_del_bind_node(sk);
6190 sctp_sk(sk)->bind_hash = NULL;
6191 inet_sk(sk)->inet_num = 0;
6192 sctp_bucket_destroy(pp);
6193 sctp_spin_unlock(&head->lock);
6196 void sctp_put_port(struct sock *sk)
6198 sctp_local_bh_disable();
6199 __sctp_put_port(sk);
6200 sctp_local_bh_enable();
6204 * The system picks an ephemeral port and choose an address set equivalent
6205 * to binding with a wildcard address.
6206 * One of those addresses will be the primary address for the association.
6207 * This automatically enables the multihoming capability of SCTP.
6209 static int sctp_autobind(struct sock *sk)
6211 union sctp_addr autoaddr;
6215 /* Initialize a local sockaddr structure to INADDR_ANY. */
6216 af = sctp_sk(sk)->pf->af;
6218 port = htons(inet_sk(sk)->inet_num);
6219 af->inaddr_any(&autoaddr, port);
6221 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
6224 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
6227 * 4.2 The cmsghdr Structure *
6229 * When ancillary data is sent or received, any number of ancillary data
6230 * objects can be specified by the msg_control and msg_controllen members of
6231 * the msghdr structure, because each object is preceded by
6232 * a cmsghdr structure defining the object's length (the cmsg_len member).
6233 * Historically Berkeley-derived implementations have passed only one object
6234 * at a time, but this API allows multiple objects to be
6235 * passed in a single call to sendmsg() or recvmsg(). The following example
6236 * shows two ancillary data objects in a control buffer.
6238 * |<--------------------------- msg_controllen -------------------------->|
6241 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
6243 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
6246 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
6248 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
6251 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6252 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
6254 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
6256 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6263 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
6264 sctp_cmsgs_t *cmsgs)
6266 struct cmsghdr *cmsg;
6267 struct msghdr *my_msg = (struct msghdr *)msg;
6269 for (cmsg = CMSG_FIRSTHDR(msg);
6271 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
6272 if (!CMSG_OK(my_msg, cmsg))
6275 /* Should we parse this header or ignore? */
6276 if (cmsg->cmsg_level != IPPROTO_SCTP)
6279 /* Strictly check lengths following example in SCM code. */
6280 switch (cmsg->cmsg_type) {
6282 /* SCTP Socket API Extension
6283 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
6285 * This cmsghdr structure provides information for
6286 * initializing new SCTP associations with sendmsg().
6287 * The SCTP_INITMSG socket option uses this same data
6288 * structure. This structure is not used for
6291 * cmsg_level cmsg_type cmsg_data[]
6292 * ------------ ------------ ----------------------
6293 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
6295 if (cmsg->cmsg_len !=
6296 CMSG_LEN(sizeof(struct sctp_initmsg)))
6298 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
6302 /* SCTP Socket API Extension
6303 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
6305 * This cmsghdr structure specifies SCTP options for
6306 * sendmsg() and describes SCTP header information
6307 * about a received message through recvmsg().
6309 * cmsg_level cmsg_type cmsg_data[]
6310 * ------------ ------------ ----------------------
6311 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
6313 if (cmsg->cmsg_len !=
6314 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
6318 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
6320 /* Minimally, validate the sinfo_flags. */
6321 if (cmsgs->info->sinfo_flags &
6322 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6323 SCTP_ABORT | SCTP_EOF))
6335 * Wait for a packet..
6336 * Note: This function is the same function as in core/datagram.c
6337 * with a few modifications to make lksctp work.
6339 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6344 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6346 /* Socket errors? */
6347 error = sock_error(sk);
6351 if (!skb_queue_empty(&sk->sk_receive_queue))
6354 /* Socket shut down? */
6355 if (sk->sk_shutdown & RCV_SHUTDOWN)
6358 /* Sequenced packets can come disconnected. If so we report the
6363 /* Is there a good reason to think that we may receive some data? */
6364 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6367 /* Handle signals. */
6368 if (signal_pending(current))
6371 /* Let another process have a go. Since we are going to sleep
6372 * anyway. Note: This may cause odd behaviors if the message
6373 * does not fit in the user's buffer, but this seems to be the
6374 * only way to honor MSG_DONTWAIT realistically.
6376 sctp_release_sock(sk);
6377 *timeo_p = schedule_timeout(*timeo_p);
6381 finish_wait(sk_sleep(sk), &wait);
6385 error = sock_intr_errno(*timeo_p);
6388 finish_wait(sk_sleep(sk), &wait);
6393 /* Receive a datagram.
6394 * Note: This is pretty much the same routine as in core/datagram.c
6395 * with a few changes to make lksctp work.
6397 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6398 int noblock, int *err)
6401 struct sk_buff *skb;
6404 timeo = sock_rcvtimeo(sk, noblock);
6406 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6407 timeo, MAX_SCHEDULE_TIMEOUT);
6410 /* Again only user level code calls this function,
6411 * so nothing interrupt level
6412 * will suddenly eat the receive_queue.
6414 * Look at current nfs client by the way...
6415 * However, this function was correct in any case. 8)
6417 if (flags & MSG_PEEK) {
6418 spin_lock_bh(&sk->sk_receive_queue.lock);
6419 skb = skb_peek(&sk->sk_receive_queue);
6421 atomic_inc(&skb->users);
6422 spin_unlock_bh(&sk->sk_receive_queue.lock);
6424 skb = skb_dequeue(&sk->sk_receive_queue);
6430 /* Caller is allowed not to check sk->sk_err before calling. */
6431 error = sock_error(sk);
6435 if (sk->sk_shutdown & RCV_SHUTDOWN)
6438 /* User doesn't want to wait. */
6442 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6451 /* If sndbuf has changed, wake up per association sndbuf waiters. */
6452 static void __sctp_write_space(struct sctp_association *asoc)
6454 struct sock *sk = asoc->base.sk;
6455 struct socket *sock = sk->sk_socket;
6457 if ((sctp_wspace(asoc) > 0) && sock) {
6458 if (waitqueue_active(&asoc->wait))
6459 wake_up_interruptible(&asoc->wait);
6461 if (sctp_writeable(sk)) {
6462 wait_queue_head_t *wq = sk_sleep(sk);
6464 if (wq && waitqueue_active(wq))
6465 wake_up_interruptible(wq);
6467 /* Note that we try to include the Async I/O support
6468 * here by modeling from the current TCP/UDP code.
6469 * We have not tested with it yet.
6471 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
6472 sock_wake_async(sock,
6473 SOCK_WAKE_SPACE, POLL_OUT);
6478 /* Do accounting for the sndbuf space.
6479 * Decrement the used sndbuf space of the corresponding association by the
6480 * data size which was just transmitted(freed).
6482 static void sctp_wfree(struct sk_buff *skb)
6484 struct sctp_association *asoc;
6485 struct sctp_chunk *chunk;
6488 /* Get the saved chunk pointer. */
6489 chunk = *((struct sctp_chunk **)(skb->cb));
6492 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6493 sizeof(struct sk_buff) +
6494 sizeof(struct sctp_chunk);
6496 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6499 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
6501 sk->sk_wmem_queued -= skb->truesize;
6502 sk_mem_uncharge(sk, skb->truesize);
6505 __sctp_write_space(asoc);
6507 sctp_association_put(asoc);
6510 /* Do accounting for the receive space on the socket.
6511 * Accounting for the association is done in ulpevent.c
6512 * We set this as a destructor for the cloned data skbs so that
6513 * accounting is done at the correct time.
6515 void sctp_sock_rfree(struct sk_buff *skb)
6517 struct sock *sk = skb->sk;
6518 struct sctp_ulpevent *event = sctp_skb2event(skb);
6520 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
6523 * Mimic the behavior of sock_rfree
6525 sk_mem_uncharge(sk, event->rmem_len);
6529 /* Helper function to wait for space in the sndbuf. */
6530 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6533 struct sock *sk = asoc->base.sk;
6535 long current_timeo = *timeo_p;
6538 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
6539 asoc, (long)(*timeo_p), msg_len);
6541 /* Increment the association's refcnt. */
6542 sctp_association_hold(asoc);
6544 /* Wait on the association specific sndbuf space. */
6546 prepare_to_wait_exclusive(&asoc->wait, &wait,
6547 TASK_INTERRUPTIBLE);
6550 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6553 if (signal_pending(current))
6554 goto do_interrupted;
6555 if (msg_len <= sctp_wspace(asoc))
6558 /* Let another process have a go. Since we are going
6561 sctp_release_sock(sk);
6562 current_timeo = schedule_timeout(current_timeo);
6563 BUG_ON(sk != asoc->base.sk);
6566 *timeo_p = current_timeo;
6570 finish_wait(&asoc->wait, &wait);
6572 /* Release the association's refcnt. */
6573 sctp_association_put(asoc);
6582 err = sock_intr_errno(*timeo_p);
6590 void sctp_data_ready(struct sock *sk, int len)
6592 struct socket_wq *wq;
6595 wq = rcu_dereference(sk->sk_wq);
6596 if (wq_has_sleeper(wq))
6597 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
6598 POLLRDNORM | POLLRDBAND);
6599 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
6603 /* If socket sndbuf has changed, wake up all per association waiters. */
6604 void sctp_write_space(struct sock *sk)
6606 struct sctp_association *asoc;
6608 /* Wake up the tasks in each wait queue. */
6609 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
6610 __sctp_write_space(asoc);
6614 /* Is there any sndbuf space available on the socket?
6616 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
6617 * associations on the same socket. For a UDP-style socket with
6618 * multiple associations, it is possible for it to be "unwriteable"
6619 * prematurely. I assume that this is acceptable because
6620 * a premature "unwriteable" is better than an accidental "writeable" which
6621 * would cause an unwanted block under certain circumstances. For the 1-1
6622 * UDP-style sockets or TCP-style sockets, this code should work.
6625 static int sctp_writeable(struct sock *sk)
6629 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
6635 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6636 * returns immediately with EINPROGRESS.
6638 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6640 struct sock *sk = asoc->base.sk;
6642 long current_timeo = *timeo_p;
6645 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
6648 /* Increment the association's refcnt. */
6649 sctp_association_hold(asoc);
6652 prepare_to_wait_exclusive(&asoc->wait, &wait,
6653 TASK_INTERRUPTIBLE);
6656 if (sk->sk_shutdown & RCV_SHUTDOWN)
6658 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6661 if (signal_pending(current))
6662 goto do_interrupted;
6664 if (sctp_state(asoc, ESTABLISHED))
6667 /* Let another process have a go. Since we are going
6670 sctp_release_sock(sk);
6671 current_timeo = schedule_timeout(current_timeo);
6674 *timeo_p = current_timeo;
6678 finish_wait(&asoc->wait, &wait);
6680 /* Release the association's refcnt. */
6681 sctp_association_put(asoc);
6686 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
6689 err = -ECONNREFUSED;
6693 err = sock_intr_errno(*timeo_p);
6701 static int sctp_wait_for_accept(struct sock *sk, long timeo)
6703 struct sctp_endpoint *ep;
6707 ep = sctp_sk(sk)->ep;
6711 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
6712 TASK_INTERRUPTIBLE);
6714 if (list_empty(&ep->asocs)) {
6715 sctp_release_sock(sk);
6716 timeo = schedule_timeout(timeo);
6721 if (!sctp_sstate(sk, LISTENING))
6725 if (!list_empty(&ep->asocs))
6728 err = sock_intr_errno(timeo);
6729 if (signal_pending(current))
6737 finish_wait(sk_sleep(sk), &wait);
6742 static void sctp_wait_for_close(struct sock *sk, long timeout)
6747 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6748 if (list_empty(&sctp_sk(sk)->ep->asocs))
6750 sctp_release_sock(sk);
6751 timeout = schedule_timeout(timeout);
6753 } while (!signal_pending(current) && timeout);
6755 finish_wait(sk_sleep(sk), &wait);
6758 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6760 struct sk_buff *frag;
6765 /* Don't forget the fragments. */
6766 skb_walk_frags(skb, frag)
6767 sctp_skb_set_owner_r_frag(frag, sk);
6770 sctp_skb_set_owner_r(skb, sk);
6773 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6774 struct sctp_association *asoc)
6776 struct inet_sock *inet = inet_sk(sk);
6777 struct inet_sock *newinet;
6779 newsk->sk_type = sk->sk_type;
6780 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6781 newsk->sk_flags = sk->sk_flags;
6782 newsk->sk_no_check = sk->sk_no_check;
6783 newsk->sk_reuse = sk->sk_reuse;
6785 newsk->sk_shutdown = sk->sk_shutdown;
6786 newsk->sk_destruct = inet_sock_destruct;
6787 newsk->sk_family = sk->sk_family;
6788 newsk->sk_protocol = IPPROTO_SCTP;
6789 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6790 newsk->sk_sndbuf = sk->sk_sndbuf;
6791 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6792 newsk->sk_lingertime = sk->sk_lingertime;
6793 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6794 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6796 newinet = inet_sk(newsk);
6798 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6799 * getsockname() and getpeername()
6801 newinet->inet_sport = inet->inet_sport;
6802 newinet->inet_saddr = inet->inet_saddr;
6803 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6804 newinet->inet_dport = htons(asoc->peer.port);
6805 newinet->pmtudisc = inet->pmtudisc;
6806 newinet->inet_id = asoc->next_tsn ^ jiffies;
6808 newinet->uc_ttl = inet->uc_ttl;
6809 newinet->mc_loop = 1;
6810 newinet->mc_ttl = 1;
6811 newinet->mc_index = 0;
6812 newinet->mc_list = NULL;
6815 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6816 * and its messages to the newsk.
6818 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6819 struct sctp_association *assoc,
6820 sctp_socket_type_t type)
6822 struct sctp_sock *oldsp = sctp_sk(oldsk);
6823 struct sctp_sock *newsp = sctp_sk(newsk);
6824 struct sctp_bind_bucket *pp; /* hash list port iterator */
6825 struct sctp_endpoint *newep = newsp->ep;
6826 struct sk_buff *skb, *tmp;
6827 struct sctp_ulpevent *event;
6828 struct sctp_bind_hashbucket *head;
6829 struct list_head tmplist;
6831 /* Migrate socket buffer sizes and all the socket level options to the
6834 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6835 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6836 /* Brute force copy old sctp opt. */
6837 if (oldsp->do_auto_asconf) {
6838 memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist));
6839 inet_sk_copy_descendant(newsk, oldsk);
6840 memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist));
6842 inet_sk_copy_descendant(newsk, oldsk);
6844 /* Restore the ep value that was overwritten with the above structure
6850 /* Hook this new socket in to the bind_hash list. */
6851 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
6852 inet_sk(oldsk)->inet_num)];
6853 sctp_local_bh_disable();
6854 sctp_spin_lock(&head->lock);
6855 pp = sctp_sk(oldsk)->bind_hash;
6856 sk_add_bind_node(newsk, &pp->owner);
6857 sctp_sk(newsk)->bind_hash = pp;
6858 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
6859 sctp_spin_unlock(&head->lock);
6860 sctp_local_bh_enable();
6862 /* Copy the bind_addr list from the original endpoint to the new
6863 * endpoint so that we can handle restarts properly
6865 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6866 &oldsp->ep->base.bind_addr, GFP_KERNEL);
6868 /* Move any messages in the old socket's receive queue that are for the
6869 * peeled off association to the new socket's receive queue.
6871 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6872 event = sctp_skb2event(skb);
6873 if (event->asoc == assoc) {
6874 __skb_unlink(skb, &oldsk->sk_receive_queue);
6875 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6876 sctp_skb_set_owner_r_frag(skb, newsk);
6880 /* Clean up any messages pending delivery due to partial
6881 * delivery. Three cases:
6882 * 1) No partial deliver; no work.
6883 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6884 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6886 skb_queue_head_init(&newsp->pd_lobby);
6887 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6889 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6890 struct sk_buff_head *queue;
6892 /* Decide which queue to move pd_lobby skbs to. */
6893 if (assoc->ulpq.pd_mode) {
6894 queue = &newsp->pd_lobby;
6896 queue = &newsk->sk_receive_queue;
6898 /* Walk through the pd_lobby, looking for skbs that
6899 * need moved to the new socket.
6901 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6902 event = sctp_skb2event(skb);
6903 if (event->asoc == assoc) {
6904 __skb_unlink(skb, &oldsp->pd_lobby);
6905 __skb_queue_tail(queue, skb);
6906 sctp_skb_set_owner_r_frag(skb, newsk);
6910 /* Clear up any skbs waiting for the partial
6911 * delivery to finish.
6913 if (assoc->ulpq.pd_mode)
6914 sctp_clear_pd(oldsk, NULL);
6918 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
6919 sctp_skb_set_owner_r_frag(skb, newsk);
6921 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
6922 sctp_skb_set_owner_r_frag(skb, newsk);
6924 /* Set the type of socket to indicate that it is peeled off from the
6925 * original UDP-style socket or created with the accept() call on a
6926 * TCP-style socket..
6930 /* Mark the new socket "in-use" by the user so that any packets
6931 * that may arrive on the association after we've moved it are
6932 * queued to the backlog. This prevents a potential race between
6933 * backlog processing on the old socket and new-packet processing
6934 * on the new socket.
6936 * The caller has just allocated newsk so we can guarantee that other
6937 * paths won't try to lock it and then oldsk.
6939 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6940 sctp_assoc_migrate(assoc, newsk);
6942 /* If the association on the newsk is already closed before accept()
6943 * is called, set RCV_SHUTDOWN flag.
6945 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6946 newsk->sk_shutdown |= RCV_SHUTDOWN;
6948 newsk->sk_state = SCTP_SS_ESTABLISHED;
6949 sctp_release_sock(newsk);
6953 /* This proto struct describes the ULP interface for SCTP. */
6954 struct proto sctp_prot = {
6956 .owner = THIS_MODULE,
6957 .close = sctp_close,
6958 .connect = sctp_connect,
6959 .disconnect = sctp_disconnect,
6960 .accept = sctp_accept,
6961 .ioctl = sctp_ioctl,
6962 .init = sctp_init_sock,
6963 .destroy = sctp_destroy_sock,
6964 .shutdown = sctp_shutdown,
6965 .setsockopt = sctp_setsockopt,
6966 .getsockopt = sctp_getsockopt,
6967 .sendmsg = sctp_sendmsg,
6968 .recvmsg = sctp_recvmsg,
6970 .backlog_rcv = sctp_backlog_rcv,
6972 .unhash = sctp_unhash,
6973 .get_port = sctp_get_port,
6974 .obj_size = sizeof(struct sctp_sock),
6975 .sysctl_mem = sysctl_sctp_mem,
6976 .sysctl_rmem = sysctl_sctp_rmem,
6977 .sysctl_wmem = sysctl_sctp_wmem,
6978 .memory_pressure = &sctp_memory_pressure,
6979 .enter_memory_pressure = sctp_enter_memory_pressure,
6980 .memory_allocated = &sctp_memory_allocated,
6981 .sockets_allocated = &sctp_sockets_allocated,
6984 #if IS_ENABLED(CONFIG_IPV6)
6986 struct proto sctpv6_prot = {
6988 .owner = THIS_MODULE,
6989 .close = sctp_close,
6990 .connect = sctp_connect,
6991 .disconnect = sctp_disconnect,
6992 .accept = sctp_accept,
6993 .ioctl = sctp_ioctl,
6994 .init = sctp_init_sock,
6995 .destroy = sctp_destroy_sock,
6996 .shutdown = sctp_shutdown,
6997 .setsockopt = sctp_setsockopt,
6998 .getsockopt = sctp_getsockopt,
6999 .sendmsg = sctp_sendmsg,
7000 .recvmsg = sctp_recvmsg,
7002 .backlog_rcv = sctp_backlog_rcv,
7004 .unhash = sctp_unhash,
7005 .get_port = sctp_get_port,
7006 .obj_size = sizeof(struct sctp6_sock),
7007 .sysctl_mem = sysctl_sctp_mem,
7008 .sysctl_rmem = sysctl_sctp_rmem,
7009 .sysctl_wmem = sysctl_sctp_wmem,
7010 .memory_pressure = &sctp_memory_pressure,
7011 .enter_memory_pressure = sctp_enter_memory_pressure,
7012 .memory_allocated = &sctp_memory_allocated,
7013 .sockets_allocated = &sctp_sockets_allocated,
7015 #endif /* IS_ENABLED(CONFIG_IPV6) */