1 /* SCTP kernel reference 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 reference 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 * The SCTP reference 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 * The SCTP reference 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
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
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 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
73 #include <net/route.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 extern kmem_cache_t *sctp_bucket_cachep;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
115 struct sock *sk = asoc->base.sk;
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
149 skb_set_owner_w(chunk->skb, sk);
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
162 /* Verify that this is a valid address. */
163 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
168 /* Verify basic sockaddr. */
169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
173 /* Is this a valid SCTP address? */
174 if (!af->addr_valid(addr, sctp_sk(sk)))
177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
183 /* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
186 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
188 struct sctp_association *asoc = NULL;
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk, UDP)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
196 if (!sctp_sstate(sk, ESTABLISHED))
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk)->ep->asocs))
201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
202 struct sctp_association, asocs);
206 /* Otherwise this is a UDP-style socket. */
207 if (!id || (id == (sctp_assoc_t)-1))
210 spin_lock_bh(&sctp_assocs_id_lock);
211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
212 spin_unlock_bh(&sctp_assocs_id_lock);
214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
220 /* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
224 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
225 struct sockaddr_storage *addr,
228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
229 struct sctp_transport *transport;
230 union sctp_addr *laddr = (union sctp_addr *)addr;
232 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
233 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
234 (union sctp_addr *)addr,
236 laddr->v4.sin_port = htons(laddr->v4.sin_port);
241 id_asoc = sctp_id2assoc(sk, id);
242 if (id_asoc && (id_asoc != addr_asoc))
245 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
246 (union sctp_addr *)addr);
251 /* API 3.1.2 bind() - UDP Style Syntax
252 * The syntax of bind() is,
254 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
256 * sd - the socket descriptor returned by socket().
257 * addr - the address structure (struct sockaddr_in or struct
258 * sockaddr_in6 [RFC 2553]),
259 * addr_len - the size of the address structure.
261 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
267 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
270 /* Disallow binding twice. */
271 if (!sctp_sk(sk)->ep->base.bind_addr.port)
272 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
277 sctp_release_sock(sk);
282 static long sctp_get_port_local(struct sock *, union sctp_addr *);
284 /* Verify this is a valid sockaddr. */
285 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
286 union sctp_addr *addr, int len)
290 /* Check minimum size. */
291 if (len < sizeof (struct sockaddr))
294 /* Does this PF support this AF? */
295 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
298 /* If we get this far, af is valid. */
299 af = sctp_get_af_specific(addr->sa.sa_family);
301 if (len < af->sockaddr_len)
307 /* Bind a local address either to an endpoint or to an association. */
308 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
310 struct sctp_sock *sp = sctp_sk(sk);
311 struct sctp_endpoint *ep = sp->ep;
312 struct sctp_bind_addr *bp = &ep->base.bind_addr;
317 /* Common sockaddr verification. */
318 af = sctp_sockaddr_af(sp, addr, len);
320 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
325 snum = ntohs(addr->v4.sin_port);
327 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
328 ", port: %d, new port: %d, len: %d)\n",
334 /* PF specific bind() address verification. */
335 if (!sp->pf->bind_verify(sp, addr))
336 return -EADDRNOTAVAIL;
338 /* We must either be unbound, or bind to the same port. */
339 if (bp->port && (snum != bp->port)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum, bp->port);
346 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
353 if ((ret = sctp_get_port_local(sk, addr))) {
354 if (ret == (long) sk) {
355 /* This endpoint has a conflicting address. */
362 /* Refresh ephemeral port. */
364 bp->port = inet_sk(sk)->num;
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep->base.addr_lock);
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr->v4.sin_port = ntohs(addr->v4.sin_port);
372 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
373 addr->v4.sin_port = htons(addr->v4.sin_port);
374 sctp_write_unlock(&ep->base.addr_lock);
375 sctp_local_bh_enable();
377 /* Copy back into socket for getsockname() use. */
379 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
380 af->to_sk_saddr(addr, sk);
386 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
389 * at any one time. If a sender, after sending an ASCONF chunk, decides
390 * it needs to transfer another ASCONF Chunk, it MUST wait until the
391 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
392 * subsequent ASCONF. Note this restriction binds each side, so at any
393 * time two ASCONF may be in-transit on any given association (one sent
394 * from each endpoint).
396 static int sctp_send_asconf(struct sctp_association *asoc,
397 struct sctp_chunk *chunk)
401 /* If there is an outstanding ASCONF chunk, queue it for later
404 if (asoc->addip_last_asconf) {
405 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
409 /* Hold the chunk until an ASCONF_ACK is received. */
410 sctp_chunk_hold(chunk);
411 retval = sctp_primitive_ASCONF(asoc, chunk);
413 sctp_chunk_free(chunk);
415 asoc->addip_last_asconf = chunk;
421 /* Add a list of addresses as bind addresses to local endpoint or
424 * Basically run through each address specified in the addrs/addrcnt
425 * array/length pair, determine if it is IPv6 or IPv4 and call
426 * sctp_do_bind() on it.
428 * If any of them fails, then the operation will be reversed and the
429 * ones that were added will be removed.
431 * Only sctp_setsockopt_bindx() is supposed to call this function.
433 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
438 struct sockaddr *sa_addr;
441 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
445 for (cnt = 0; cnt < addrcnt; cnt++) {
446 /* The list may contain either IPv4 or IPv6 address;
447 * determine the address length for walking thru the list.
449 sa_addr = (struct sockaddr *)addr_buf;
450 af = sctp_get_af_specific(sa_addr->sa_family);
456 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
459 addr_buf += af->sockaddr_len;
463 /* Failed. Cleanup the ones that have been added */
465 sctp_bindx_rem(sk, addrs, cnt);
473 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
474 * associations that are part of the endpoint indicating that a list of local
475 * addresses are added to the endpoint.
477 * If any of the addresses is already in the bind address list of the
478 * association, we do not send the chunk for that association. But it will not
479 * affect other associations.
481 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 static int sctp_send_asconf_add_ip(struct sock *sk,
484 struct sockaddr *addrs,
487 struct sctp_sock *sp;
488 struct sctp_endpoint *ep;
489 struct sctp_association *asoc;
490 struct sctp_bind_addr *bp;
491 struct sctp_chunk *chunk;
492 struct sctp_sockaddr_entry *laddr;
493 union sctp_addr *addr;
496 struct list_head *pos;
501 if (!sctp_addip_enable)
507 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
508 __FUNCTION__, sk, addrs, addrcnt);
510 list_for_each(pos, &ep->asocs) {
511 asoc = list_entry(pos, struct sctp_association, asocs);
513 if (!asoc->peer.asconf_capable)
516 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
519 if (!sctp_state(asoc, ESTABLISHED))
522 /* Check if any address in the packed array of addresses is
523 * in the bind address list of the association. If so,
524 * do not send the asconf chunk to its peer, but continue with
525 * other associations.
528 for (i = 0; i < addrcnt; i++) {
529 addr = (union sctp_addr *)addr_buf;
530 af = sctp_get_af_specific(addr->v4.sin_family);
536 if (sctp_assoc_lookup_laddr(asoc, addr))
539 addr_buf += af->sockaddr_len;
544 /* Use the first address in bind addr list of association as
545 * Address Parameter of ASCONF CHUNK.
547 sctp_read_lock(&asoc->base.addr_lock);
548 bp = &asoc->base.bind_addr;
549 p = bp->address_list.next;
550 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
551 sctp_read_unlock(&asoc->base.addr_lock);
553 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
554 addrcnt, SCTP_PARAM_ADD_IP);
560 retval = sctp_send_asconf(asoc, chunk);
562 /* FIXME: After sending the add address ASCONF chunk, we
563 * cannot append the address to the association's binding
564 * address list, because the new address may be used as the
565 * source of a message sent to the peer before the ASCONF
566 * chunk is received by the peer. So we should wait until
567 * ASCONF_ACK is received.
575 /* Remove a list of addresses from bind addresses list. Do not remove the
578 * Basically run through each address specified in the addrs/addrcnt
579 * array/length pair, determine if it is IPv6 or IPv4 and call
580 * sctp_del_bind() on it.
582 * If any of them fails, then the operation will be reversed and the
583 * ones that were removed will be added back.
585 * At least one address has to be left; if only one address is
586 * available, the operation will return -EBUSY.
588 * Only sctp_setsockopt_bindx() is supposed to call this function.
590 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
592 struct sctp_sock *sp = sctp_sk(sk);
593 struct sctp_endpoint *ep = sp->ep;
595 struct sctp_bind_addr *bp = &ep->base.bind_addr;
597 union sctp_addr saveaddr;
599 struct sockaddr *sa_addr;
602 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
606 for (cnt = 0; cnt < addrcnt; cnt++) {
607 /* If the bind address list is empty or if there is only one
608 * bind address, there is nothing more to be removed (we need
609 * at least one address here).
611 if (list_empty(&bp->address_list) ||
612 (sctp_list_single_entry(&bp->address_list))) {
617 /* The list may contain either IPv4 or IPv6 address;
618 * determine the address length to copy the address to
621 sa_addr = (struct sockaddr *)addr_buf;
622 af = sctp_get_af_specific(sa_addr->sa_family);
627 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
628 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
629 if (saveaddr.v4.sin_port != bp->port) {
634 /* FIXME - There is probably a need to check if sk->sk_saddr and
635 * sk->sk_rcv_addr are currently set to one of the addresses to
636 * be removed. This is something which needs to be looked into
637 * when we are fixing the outstanding issues with multi-homing
638 * socket routing and failover schemes. Refer to comments in
639 * sctp_do_bind(). -daisy
641 sctp_local_bh_disable();
642 sctp_write_lock(&ep->base.addr_lock);
644 retval = sctp_del_bind_addr(bp, &saveaddr);
646 sctp_write_unlock(&ep->base.addr_lock);
647 sctp_local_bh_enable();
649 addr_buf += af->sockaddr_len;
652 /* Failed. Add the ones that has been removed back */
654 sctp_bindx_add(sk, addrs, cnt);
662 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
663 * the associations that are part of the endpoint indicating that a list of
664 * local addresses are removed from the endpoint.
666 * If any of the addresses is already in the bind address list of the
667 * association, we do not send the chunk for that association. But it will not
668 * affect other associations.
670 * Only sctp_setsockopt_bindx() is supposed to call this function.
672 static int sctp_send_asconf_del_ip(struct sock *sk,
673 struct sockaddr *addrs,
676 struct sctp_sock *sp;
677 struct sctp_endpoint *ep;
678 struct sctp_association *asoc;
679 struct sctp_bind_addr *bp;
680 struct sctp_chunk *chunk;
681 union sctp_addr *laddr;
684 struct list_head *pos;
688 if (!sctp_addip_enable)
694 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
695 __FUNCTION__, sk, addrs, addrcnt);
697 list_for_each(pos, &ep->asocs) {
698 asoc = list_entry(pos, struct sctp_association, asocs);
700 if (!asoc->peer.asconf_capable)
703 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
706 if (!sctp_state(asoc, ESTABLISHED))
709 /* Check if any address in the packed array of addresses is
710 * not present in the bind address list of the association.
711 * If so, do not send the asconf chunk to its peer, but
712 * continue with other associations.
715 for (i = 0; i < addrcnt; i++) {
716 laddr = (union sctp_addr *)addr_buf;
717 af = sctp_get_af_specific(laddr->v4.sin_family);
723 if (!sctp_assoc_lookup_laddr(asoc, laddr))
726 addr_buf += af->sockaddr_len;
731 /* Find one address in the association's bind address list
732 * that is not in the packed array of addresses. This is to
733 * make sure that we do not delete all the addresses in the
736 sctp_read_lock(&asoc->base.addr_lock);
737 bp = &asoc->base.bind_addr;
738 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
740 sctp_read_unlock(&asoc->base.addr_lock);
744 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
751 retval = sctp_send_asconf(asoc, chunk);
753 /* FIXME: After sending the delete address ASCONF chunk, we
754 * cannot remove the addresses from the association's bind
755 * address list, because there maybe some packet send to
756 * the delete addresses, so we should wait until ASCONF_ACK
757 * packet is received.
764 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
767 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
770 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
771 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
774 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
775 * Section 3.1.2 for this usage.
777 * addrs is a pointer to an array of one or more socket addresses. Each
778 * address is contained in its appropriate structure (i.e. struct
779 * sockaddr_in or struct sockaddr_in6) the family of the address type
780 * must be used to distengish the address length (note that this
781 * representation is termed a "packed array" of addresses). The caller
782 * specifies the number of addresses in the array with addrcnt.
784 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
785 * -1, and sets errno to the appropriate error code.
787 * For SCTP, the port given in each socket address must be the same, or
788 * sctp_bindx() will fail, setting errno to EINVAL.
790 * The flags parameter is formed from the bitwise OR of zero or more of
791 * the following currently defined flags:
793 * SCTP_BINDX_ADD_ADDR
795 * SCTP_BINDX_REM_ADDR
797 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
798 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
799 * addresses from the association. The two flags are mutually exclusive;
800 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
801 * not remove all addresses from an association; sctp_bindx() will
802 * reject such an attempt with EINVAL.
804 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
805 * additional addresses with an endpoint after calling bind(). Or use
806 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
807 * socket is associated with so that no new association accepted will be
808 * associated with those addresses. If the endpoint supports dynamic
809 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
810 * endpoint to send the appropriate message to the peer to change the
811 * peers address lists.
813 * Adding and removing addresses from a connected association is
814 * optional functionality. Implementations that do not support this
815 * functionality should return EOPNOTSUPP.
817 * Basically do nothing but copying the addresses from user to kernel
818 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
819 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
822 * We don't use copy_from_user() for optimization: we first do the
823 * sanity checks (buffer size -fast- and access check-healthy
824 * pointer); if all of those succeed, then we can alloc the memory
825 * (expensive operation) needed to copy the data to kernel. Then we do
826 * the copying without checking the user space area
827 * (__copy_from_user()).
829 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
832 * sk The sk of the socket
833 * addrs The pointer to the addresses in user land
834 * addrssize Size of the addrs buffer
835 * op Operation to perform (add or remove, see the flags of
838 * Returns 0 if ok, <0 errno code on error.
840 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
841 struct sockaddr __user *addrs,
842 int addrs_size, int op)
844 struct sockaddr *kaddrs;
848 struct sockaddr *sa_addr;
852 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
853 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
855 if (unlikely(addrs_size <= 0))
858 /* Check the user passed a healthy pointer. */
859 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
862 /* Alloc space for the address array in kernel memory. */
863 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
864 if (unlikely(!kaddrs))
867 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
872 /* Walk through the addrs buffer and count the number of addresses. */
874 while (walk_size < addrs_size) {
875 sa_addr = (struct sockaddr *)addr_buf;
876 af = sctp_get_af_specific(sa_addr->sa_family);
878 /* If the address family is not supported or if this address
879 * causes the address buffer to overflow return EINVAL.
881 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
886 addr_buf += af->sockaddr_len;
887 walk_size += af->sockaddr_len;
892 case SCTP_BINDX_ADD_ADDR:
893 err = sctp_bindx_add(sk, kaddrs, addrcnt);
896 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
899 case SCTP_BINDX_REM_ADDR:
900 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
903 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
917 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
919 * Common routine for handling connect() and sctp_connectx().
920 * Connect will come in with just a single address.
922 static int __sctp_connect(struct sock* sk,
923 struct sockaddr *kaddrs,
926 struct sctp_sock *sp;
927 struct sctp_endpoint *ep;
928 struct sctp_association *asoc = NULL;
929 struct sctp_association *asoc2;
930 struct sctp_transport *transport;
938 struct sockaddr *sa_addr;
944 /* connect() cannot be done on a socket that is already in ESTABLISHED
945 * state - UDP-style peeled off socket or a TCP-style socket that
946 * is already connected.
947 * It cannot be done even on a TCP-style listening socket.
949 if (sctp_sstate(sk, ESTABLISHED) ||
950 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
955 /* Walk through the addrs buffer and count the number of addresses. */
957 while (walk_size < addrs_size) {
958 sa_addr = (struct sockaddr *)addr_buf;
959 af = sctp_get_af_specific(sa_addr->sa_family);
961 /* If the address family is not supported or if this address
962 * causes the address buffer to overflow return EINVAL.
964 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
969 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
974 memcpy(&to, sa_addr, af->sockaddr_len);
975 to.v4.sin_port = ntohs(to.v4.sin_port);
977 /* Check if there already is a matching association on the
978 * endpoint (other than the one created here).
980 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
981 if (asoc2 && asoc2 != asoc) {
982 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
989 /* If we could not find a matching association on the endpoint,
990 * make sure that there is no peeled-off association matching
991 * the peer address even on another socket.
993 if (sctp_endpoint_is_peeled_off(ep, &to)) {
994 err = -EADDRNOTAVAIL;
999 /* If a bind() or sctp_bindx() is not called prior to
1000 * an sctp_connectx() call, the system picks an
1001 * ephemeral port and will choose an address set
1002 * equivalent to binding with a wildcard address.
1004 if (!ep->base.bind_addr.port) {
1005 if (sctp_autobind(sk)) {
1011 * If an unprivileged user inherits a 1-many
1012 * style socket with open associations on a
1013 * privileged port, it MAY be permitted to
1014 * accept new associations, but it SHOULD NOT
1015 * be permitted to open new associations.
1017 if (ep->base.bind_addr.port < PROT_SOCK &&
1018 !capable(CAP_NET_BIND_SERVICE)) {
1024 scope = sctp_scope(&to);
1025 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1032 /* Prime the peer's transport structures. */
1033 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1041 addr_buf += af->sockaddr_len;
1042 walk_size += af->sockaddr_len;
1045 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1050 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1055 /* Initialize sk's dport and daddr for getpeername() */
1056 inet_sk(sk)->dport = htons(asoc->peer.port);
1057 af = sctp_get_af_specific(to.sa.sa_family);
1058 af->to_sk_daddr(&to, sk);
1060 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1061 err = sctp_wait_for_connect(asoc, &timeo);
1063 /* Don't free association on exit. */
1068 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1069 " kaddrs: %p err: %d\n",
1072 sctp_association_free(asoc);
1076 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1079 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1081 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1082 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1083 * or IPv6 addresses.
1085 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1086 * Section 3.1.2 for this usage.
1088 * addrs is a pointer to an array of one or more socket addresses. Each
1089 * address is contained in its appropriate structure (i.e. struct
1090 * sockaddr_in or struct sockaddr_in6) the family of the address type
1091 * must be used to distengish the address length (note that this
1092 * representation is termed a "packed array" of addresses). The caller
1093 * specifies the number of addresses in the array with addrcnt.
1095 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1096 * -1, and sets errno to the appropriate error code.
1098 * For SCTP, the port given in each socket address must be the same, or
1099 * sctp_connectx() will fail, setting errno to EINVAL.
1101 * An application can use sctp_connectx to initiate an association with
1102 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1103 * allows a caller to specify multiple addresses at which a peer can be
1104 * reached. The way the SCTP stack uses the list of addresses to set up
1105 * the association is implementation dependant. This function only
1106 * specifies that the stack will try to make use of all the addresses in
1107 * the list when needed.
1109 * Note that the list of addresses passed in is only used for setting up
1110 * the association. It does not necessarily equal the set of addresses
1111 * the peer uses for the resulting association. If the caller wants to
1112 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1113 * retrieve them after the association has been set up.
1115 * Basically do nothing but copying the addresses from user to kernel
1116 * land and invoking either sctp_connectx(). This is used for tunneling
1117 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1119 * We don't use copy_from_user() for optimization: we first do the
1120 * sanity checks (buffer size -fast- and access check-healthy
1121 * pointer); if all of those succeed, then we can alloc the memory
1122 * (expensive operation) needed to copy the data to kernel. Then we do
1123 * the copying without checking the user space area
1124 * (__copy_from_user()).
1126 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1129 * sk The sk of the socket
1130 * addrs The pointer to the addresses in user land
1131 * addrssize Size of the addrs buffer
1133 * Returns 0 if ok, <0 errno code on error.
1135 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1136 struct sockaddr __user *addrs,
1140 struct sockaddr *kaddrs;
1142 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1143 __FUNCTION__, sk, addrs, addrs_size);
1145 if (unlikely(addrs_size <= 0))
1148 /* Check the user passed a healthy pointer. */
1149 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1152 /* Alloc space for the address array in kernel memory. */
1153 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
1154 if (unlikely(!kaddrs))
1157 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1160 err = __sctp_connect(sk, kaddrs, addrs_size);
1167 /* API 3.1.4 close() - UDP Style Syntax
1168 * Applications use close() to perform graceful shutdown (as described in
1169 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1170 * by a UDP-style socket.
1174 * ret = close(int sd);
1176 * sd - the socket descriptor of the associations to be closed.
1178 * To gracefully shutdown a specific association represented by the
1179 * UDP-style socket, an application should use the sendmsg() call,
1180 * passing no user data, but including the appropriate flag in the
1181 * ancillary data (see Section xxxx).
1183 * If sd in the close() call is a branched-off socket representing only
1184 * one association, the shutdown is performed on that association only.
1186 * 4.1.6 close() - TCP Style Syntax
1188 * Applications use close() to gracefully close down an association.
1192 * int close(int sd);
1194 * sd - the socket descriptor of the association to be closed.
1196 * After an application calls close() on a socket descriptor, no further
1197 * socket operations will succeed on that descriptor.
1199 * API 7.1.4 SO_LINGER
1201 * An application using the TCP-style socket can use this option to
1202 * perform the SCTP ABORT primitive. The linger option structure is:
1205 * int l_onoff; // option on/off
1206 * int l_linger; // linger time
1209 * To enable the option, set l_onoff to 1. If the l_linger value is set
1210 * to 0, calling close() is the same as the ABORT primitive. If the
1211 * value is set to a negative value, the setsockopt() call will return
1212 * an error. If the value is set to a positive value linger_time, the
1213 * close() can be blocked for at most linger_time ms. If the graceful
1214 * shutdown phase does not finish during this period, close() will
1215 * return but the graceful shutdown phase continues in the system.
1217 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1219 struct sctp_endpoint *ep;
1220 struct sctp_association *asoc;
1221 struct list_head *pos, *temp;
1223 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1226 sk->sk_shutdown = SHUTDOWN_MASK;
1228 ep = sctp_sk(sk)->ep;
1230 /* Walk all associations on a socket, not on an endpoint. */
1231 list_for_each_safe(pos, temp, &ep->asocs) {
1232 asoc = list_entry(pos, struct sctp_association, asocs);
1234 if (sctp_style(sk, TCP)) {
1235 /* A closed association can still be in the list if
1236 * it belongs to a TCP-style listening socket that is
1237 * not yet accepted. If so, free it. If not, send an
1238 * ABORT or SHUTDOWN based on the linger options.
1240 if (sctp_state(asoc, CLOSED)) {
1241 sctp_unhash_established(asoc);
1242 sctp_association_free(asoc);
1244 } else if (sock_flag(sk, SOCK_LINGER) &&
1246 sctp_primitive_ABORT(asoc, NULL);
1248 sctp_primitive_SHUTDOWN(asoc, NULL);
1250 sctp_primitive_SHUTDOWN(asoc, NULL);
1253 /* Clean up any skbs sitting on the receive queue. */
1254 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1255 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1257 /* On a TCP-style socket, block for at most linger_time if set. */
1258 if (sctp_style(sk, TCP) && timeout)
1259 sctp_wait_for_close(sk, timeout);
1261 /* This will run the backlog queue. */
1262 sctp_release_sock(sk);
1264 /* Supposedly, no process has access to the socket, but
1265 * the net layers still may.
1267 sctp_local_bh_disable();
1268 sctp_bh_lock_sock(sk);
1270 /* Hold the sock, since sk_common_release() will put sock_put()
1271 * and we have just a little more cleanup.
1274 sk_common_release(sk);
1276 sctp_bh_unlock_sock(sk);
1277 sctp_local_bh_enable();
1281 SCTP_DBG_OBJCNT_DEC(sock);
1284 /* Handle EPIPE error. */
1285 static int sctp_error(struct sock *sk, int flags, int err)
1288 err = sock_error(sk) ? : -EPIPE;
1289 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1290 send_sig(SIGPIPE, current, 0);
1294 /* API 3.1.3 sendmsg() - UDP Style Syntax
1296 * An application uses sendmsg() and recvmsg() calls to transmit data to
1297 * and receive data from its peer.
1299 * ssize_t sendmsg(int socket, const struct msghdr *message,
1302 * socket - the socket descriptor of the endpoint.
1303 * message - pointer to the msghdr structure which contains a single
1304 * user message and possibly some ancillary data.
1306 * See Section 5 for complete description of the data
1309 * flags - flags sent or received with the user message, see Section
1310 * 5 for complete description of the flags.
1312 * Note: This function could use a rewrite especially when explicit
1313 * connect support comes in.
1315 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1317 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1319 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1320 struct msghdr *msg, size_t msg_len)
1322 struct sctp_sock *sp;
1323 struct sctp_endpoint *ep;
1324 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1325 struct sctp_transport *transport, *chunk_tp;
1326 struct sctp_chunk *chunk;
1328 struct sockaddr *msg_name = NULL;
1329 struct sctp_sndrcvinfo default_sinfo = { 0 };
1330 struct sctp_sndrcvinfo *sinfo;
1331 struct sctp_initmsg *sinit;
1332 sctp_assoc_t associd = 0;
1333 sctp_cmsgs_t cmsgs = { NULL };
1337 __u16 sinfo_flags = 0;
1338 struct sctp_datamsg *datamsg;
1339 struct list_head *pos;
1340 int msg_flags = msg->msg_flags;
1342 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1349 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1351 /* We cannot send a message over a TCP-style listening socket. */
1352 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1357 /* Parse out the SCTP CMSGs. */
1358 err = sctp_msghdr_parse(msg, &cmsgs);
1361 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1365 /* Fetch the destination address for this packet. This
1366 * address only selects the association--it is not necessarily
1367 * the address we will send to.
1368 * For a peeled-off socket, msg_name is ignored.
1370 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1371 int msg_namelen = msg->msg_namelen;
1373 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1378 if (msg_namelen > sizeof(to))
1379 msg_namelen = sizeof(to);
1380 memcpy(&to, msg->msg_name, msg_namelen);
1381 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1383 to.v4.sin_addr.s_addr, to.v4.sin_port);
1385 to.v4.sin_port = ntohs(to.v4.sin_port);
1386 msg_name = msg->msg_name;
1392 /* Did the user specify SNDRCVINFO? */
1394 sinfo_flags = sinfo->sinfo_flags;
1395 associd = sinfo->sinfo_assoc_id;
1398 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1399 msg_len, sinfo_flags);
1401 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1402 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1407 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1408 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1409 * If SCTP_ABORT is set, the message length could be non zero with
1410 * the msg_iov set to the user abort reason.
1412 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1413 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1418 /* If SCTP_ADDR_OVER is set, there must be an address
1419 * specified in msg_name.
1421 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1428 SCTP_DEBUG_PRINTK("About to look up association.\n");
1432 /* If a msg_name has been specified, assume this is to be used. */
1434 /* Look for a matching association on the endpoint. */
1435 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1437 /* If we could not find a matching association on the
1438 * endpoint, make sure that it is not a TCP-style
1439 * socket that already has an association or there is
1440 * no peeled-off association on another socket.
1442 if ((sctp_style(sk, TCP) &&
1443 sctp_sstate(sk, ESTABLISHED)) ||
1444 sctp_endpoint_is_peeled_off(ep, &to)) {
1445 err = -EADDRNOTAVAIL;
1450 asoc = sctp_id2assoc(sk, associd);
1458 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1460 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1461 * socket that has an association in CLOSED state. This can
1462 * happen when an accepted socket has an association that is
1465 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1470 if (sinfo_flags & SCTP_EOF) {
1471 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1473 sctp_primitive_SHUTDOWN(asoc, NULL);
1477 if (sinfo_flags & SCTP_ABORT) {
1478 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1479 sctp_primitive_ABORT(asoc, msg);
1485 /* Do we need to create the association? */
1487 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1489 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1494 /* Check for invalid stream against the stream counts,
1495 * either the default or the user specified stream counts.
1498 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1499 /* Check against the defaults. */
1500 if (sinfo->sinfo_stream >=
1501 sp->initmsg.sinit_num_ostreams) {
1506 /* Check against the requested. */
1507 if (sinfo->sinfo_stream >=
1508 sinit->sinit_num_ostreams) {
1516 * API 3.1.2 bind() - UDP Style Syntax
1517 * If a bind() or sctp_bindx() is not called prior to a
1518 * sendmsg() call that initiates a new association, the
1519 * system picks an ephemeral port and will choose an address
1520 * set equivalent to binding with a wildcard address.
1522 if (!ep->base.bind_addr.port) {
1523 if (sctp_autobind(sk)) {
1529 * If an unprivileged user inherits a one-to-many
1530 * style socket with open associations on a privileged
1531 * port, it MAY be permitted to accept new associations,
1532 * but it SHOULD NOT be permitted to open new
1535 if (ep->base.bind_addr.port < PROT_SOCK &&
1536 !capable(CAP_NET_BIND_SERVICE)) {
1542 scope = sctp_scope(&to);
1543 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1550 /* If the SCTP_INIT ancillary data is specified, set all
1551 * the association init values accordingly.
1554 if (sinit->sinit_num_ostreams) {
1555 asoc->c.sinit_num_ostreams =
1556 sinit->sinit_num_ostreams;
1558 if (sinit->sinit_max_instreams) {
1559 asoc->c.sinit_max_instreams =
1560 sinit->sinit_max_instreams;
1562 if (sinit->sinit_max_attempts) {
1563 asoc->max_init_attempts
1564 = sinit->sinit_max_attempts;
1566 if (sinit->sinit_max_init_timeo) {
1567 asoc->max_init_timeo =
1568 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1572 /* Prime the peer's transport structures. */
1573 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1578 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1585 /* ASSERT: we have a valid association at this point. */
1586 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1589 /* If the user didn't specify SNDRCVINFO, make up one with
1592 default_sinfo.sinfo_stream = asoc->default_stream;
1593 default_sinfo.sinfo_flags = asoc->default_flags;
1594 default_sinfo.sinfo_ppid = asoc->default_ppid;
1595 default_sinfo.sinfo_context = asoc->default_context;
1596 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1597 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1598 sinfo = &default_sinfo;
1601 /* API 7.1.7, the sndbuf size per association bounds the
1602 * maximum size of data that can be sent in a single send call.
1604 if (msg_len > sk->sk_sndbuf) {
1609 /* If fragmentation is disabled and the message length exceeds the
1610 * association fragmentation point, return EMSGSIZE. The I-D
1611 * does not specify what this error is, but this looks like
1614 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1620 /* Check for invalid stream. */
1621 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1627 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1628 if (!sctp_wspace(asoc)) {
1629 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1634 /* If an address is passed with the sendto/sendmsg call, it is used
1635 * to override the primary destination address in the TCP model, or
1636 * when SCTP_ADDR_OVER flag is set in the UDP model.
1638 if ((sctp_style(sk, TCP) && msg_name) ||
1639 (sinfo_flags & SCTP_ADDR_OVER)) {
1640 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1648 /* Auto-connect, if we aren't connected already. */
1649 if (sctp_state(asoc, CLOSED)) {
1650 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1653 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1656 /* Break the message into multiple chunks of maximum size. */
1657 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1663 /* Now send the (possibly) fragmented message. */
1664 list_for_each(pos, &datamsg->chunks) {
1665 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1666 sctp_datamsg_track(chunk);
1668 /* Do accounting for the write space. */
1669 sctp_set_owner_w(chunk);
1671 chunk->transport = chunk_tp;
1673 /* Send it to the lower layers. Note: all chunks
1674 * must either fail or succeed. The lower layer
1675 * works that way today. Keep it that way or this
1678 err = sctp_primitive_SEND(asoc, chunk);
1679 /* Did the lower layer accept the chunk? */
1681 sctp_chunk_free(chunk);
1682 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1685 sctp_datamsg_free(datamsg);
1691 /* If we are already past ASSOCIATE, the lower
1692 * layers are responsible for association cleanup.
1698 sctp_association_free(asoc);
1700 sctp_release_sock(sk);
1703 return sctp_error(sk, msg_flags, err);
1710 err = sock_error(sk);
1720 /* This is an extended version of skb_pull() that removes the data from the
1721 * start of a skb even when data is spread across the list of skb's in the
1722 * frag_list. len specifies the total amount of data that needs to be removed.
1723 * when 'len' bytes could be removed from the skb, it returns 0.
1724 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1725 * could not be removed.
1727 static int sctp_skb_pull(struct sk_buff *skb, int len)
1729 struct sk_buff *list;
1730 int skb_len = skb_headlen(skb);
1733 if (len <= skb_len) {
1734 __skb_pull(skb, len);
1738 __skb_pull(skb, skb_len);
1740 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1741 rlen = sctp_skb_pull(list, len);
1742 skb->len -= (len-rlen);
1743 skb->data_len -= (len-rlen);
1754 /* API 3.1.3 recvmsg() - UDP Style Syntax
1756 * ssize_t recvmsg(int socket, struct msghdr *message,
1759 * socket - the socket descriptor of the endpoint.
1760 * message - pointer to the msghdr structure which contains a single
1761 * user message and possibly some ancillary data.
1763 * See Section 5 for complete description of the data
1766 * flags - flags sent or received with the user message, see Section
1767 * 5 for complete description of the flags.
1769 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1771 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1772 struct msghdr *msg, size_t len, int noblock,
1773 int flags, int *addr_len)
1775 struct sctp_ulpevent *event = NULL;
1776 struct sctp_sock *sp = sctp_sk(sk);
1777 struct sk_buff *skb;
1782 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1783 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1784 "len", len, "knoblauch", noblock,
1785 "flags", flags, "addr_len", addr_len);
1789 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1794 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1798 /* Get the total length of the skb including any skb's in the
1807 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1809 event = sctp_skb2event(skb);
1814 sock_recv_timestamp(msg, sk, skb);
1815 if (sctp_ulpevent_is_notification(event)) {
1816 msg->msg_flags |= MSG_NOTIFICATION;
1817 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1819 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1822 /* Check if we allow SCTP_SNDRCVINFO. */
1823 if (sp->subscribe.sctp_data_io_event)
1824 sctp_ulpevent_read_sndrcvinfo(event, msg);
1826 /* FIXME: we should be calling IP/IPv6 layers. */
1827 if (sk->sk_protinfo.af_inet.cmsg_flags)
1828 ip_cmsg_recv(msg, skb);
1833 /* If skb's length exceeds the user's buffer, update the skb and
1834 * push it back to the receive_queue so that the next call to
1835 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1837 if (skb_len > copied) {
1838 msg->msg_flags &= ~MSG_EOR;
1839 if (flags & MSG_PEEK)
1841 sctp_skb_pull(skb, copied);
1842 skb_queue_head(&sk->sk_receive_queue, skb);
1844 /* When only partial message is copied to the user, increase
1845 * rwnd by that amount. If all the data in the skb is read,
1846 * rwnd is updated when the event is freed.
1848 sctp_assoc_rwnd_increase(event->asoc, copied);
1850 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1851 (event->msg_flags & MSG_EOR))
1852 msg->msg_flags |= MSG_EOR;
1854 msg->msg_flags &= ~MSG_EOR;
1857 if (flags & MSG_PEEK) {
1858 /* Release the skb reference acquired after peeking the skb in
1859 * sctp_skb_recv_datagram().
1863 /* Free the event which includes releasing the reference to
1864 * the owner of the skb, freeing the skb and updating the
1867 sctp_ulpevent_free(event);
1870 sctp_release_sock(sk);
1874 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1876 * This option is a on/off flag. If enabled no SCTP message
1877 * fragmentation will be performed. Instead if a message being sent
1878 * exceeds the current PMTU size, the message will NOT be sent and
1879 * instead a error will be indicated to the user.
1881 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1882 char __user *optval, int optlen)
1886 if (optlen < sizeof(int))
1889 if (get_user(val, (int __user *)optval))
1892 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1897 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1900 if (optlen != sizeof(struct sctp_event_subscribe))
1902 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1907 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1909 * This socket option is applicable to the UDP-style socket only. When
1910 * set it will cause associations that are idle for more than the
1911 * specified number of seconds to automatically close. An association
1912 * being idle is defined an association that has NOT sent or received
1913 * user data. The special value of '0' indicates that no automatic
1914 * close of any associations should be performed. The option expects an
1915 * integer defining the number of seconds of idle time before an
1916 * association is closed.
1918 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1921 struct sctp_sock *sp = sctp_sk(sk);
1923 /* Applicable to UDP-style socket only */
1924 if (sctp_style(sk, TCP))
1926 if (optlen != sizeof(int))
1928 if (copy_from_user(&sp->autoclose, optval, optlen))
1934 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1936 * Applications can enable or disable heartbeats for any peer address of
1937 * an association, modify an address's heartbeat interval, force a
1938 * heartbeat to be sent immediately, and adjust the address's maximum
1939 * number of retransmissions sent before an address is considered
1940 * unreachable. The following structure is used to access and modify an
1941 * address's parameters:
1943 * struct sctp_paddrparams {
1944 * sctp_assoc_t spp_assoc_id;
1945 * struct sockaddr_storage spp_address;
1946 * uint32_t spp_hbinterval;
1947 * uint16_t spp_pathmaxrxt;
1948 * uint32_t spp_pathmtu;
1949 * uint32_t spp_sackdelay;
1950 * uint32_t spp_flags;
1953 * spp_assoc_id - (one-to-many style socket) This is filled in the
1954 * application, and identifies the association for
1956 * spp_address - This specifies which address is of interest.
1957 * spp_hbinterval - This contains the value of the heartbeat interval,
1958 * in milliseconds. If a value of zero
1959 * is present in this field then no changes are to
1960 * be made to this parameter.
1961 * spp_pathmaxrxt - This contains the maximum number of
1962 * retransmissions before this address shall be
1963 * considered unreachable. If a value of zero
1964 * is present in this field then no changes are to
1965 * be made to this parameter.
1966 * spp_pathmtu - When Path MTU discovery is disabled the value
1967 * specified here will be the "fixed" path mtu.
1968 * Note that if the spp_address field is empty
1969 * then all associations on this address will
1970 * have this fixed path mtu set upon them.
1972 * spp_sackdelay - When delayed sack is enabled, this value specifies
1973 * the number of milliseconds that sacks will be delayed
1974 * for. This value will apply to all addresses of an
1975 * association if the spp_address field is empty. Note
1976 * also, that if delayed sack is enabled and this
1977 * value is set to 0, no change is made to the last
1978 * recorded delayed sack timer value.
1980 * spp_flags - These flags are used to control various features
1981 * on an association. The flag field may contain
1982 * zero or more of the following options.
1984 * SPP_HB_ENABLE - Enable heartbeats on the
1985 * specified address. Note that if the address
1986 * field is empty all addresses for the association
1987 * have heartbeats enabled upon them.
1989 * SPP_HB_DISABLE - Disable heartbeats on the
1990 * speicifed address. Note that if the address
1991 * field is empty all addresses for the association
1992 * will have their heartbeats disabled. Note also
1993 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
1994 * mutually exclusive, only one of these two should
1995 * be specified. Enabling both fields will have
1996 * undetermined results.
1998 * SPP_HB_DEMAND - Request a user initiated heartbeat
1999 * to be made immediately.
2001 * SPP_PMTUD_ENABLE - This field will enable PMTU
2002 * discovery upon the specified address. Note that
2003 * if the address feild is empty then all addresses
2004 * on the association are effected.
2006 * SPP_PMTUD_DISABLE - This field will disable PMTU
2007 * discovery upon the specified address. Note that
2008 * if the address feild is empty then all addresses
2009 * on the association are effected. Not also that
2010 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2011 * exclusive. Enabling both will have undetermined
2014 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2015 * on delayed sack. The time specified in spp_sackdelay
2016 * is used to specify the sack delay for this address. Note
2017 * that if spp_address is empty then all addresses will
2018 * enable delayed sack and take on the sack delay
2019 * value specified in spp_sackdelay.
2020 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2021 * off delayed sack. If the spp_address field is blank then
2022 * delayed sack is disabled for the entire association. Note
2023 * also that this field is mutually exclusive to
2024 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2027 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2028 struct sctp_transport *trans,
2029 struct sctp_association *asoc,
2030 struct sctp_sock *sp,
2033 int sackdelay_change)
2037 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2038 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2043 if (params->spp_hbinterval) {
2045 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2047 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2049 sp->hbinterval = params->spp_hbinterval;
2055 trans->param_flags =
2056 (trans->param_flags & ~SPP_HB) | hb_change;
2059 (asoc->param_flags & ~SPP_HB) | hb_change;
2062 (sp->param_flags & ~SPP_HB) | hb_change;
2066 if (params->spp_pathmtu) {
2068 trans->pathmtu = params->spp_pathmtu;
2069 sctp_assoc_sync_pmtu(asoc);
2071 asoc->pathmtu = params->spp_pathmtu;
2072 sctp_frag_point(sp, params->spp_pathmtu);
2074 sp->pathmtu = params->spp_pathmtu;
2080 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2081 (params->spp_flags & SPP_PMTUD_ENABLE);
2082 trans->param_flags =
2083 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2085 sctp_transport_pmtu(trans);
2086 sctp_assoc_sync_pmtu(asoc);
2090 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2093 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2097 if (params->spp_sackdelay) {
2100 msecs_to_jiffies(params->spp_sackdelay);
2103 msecs_to_jiffies(params->spp_sackdelay);
2105 sp->sackdelay = params->spp_sackdelay;
2109 if (sackdelay_change) {
2111 trans->param_flags =
2112 (trans->param_flags & ~SPP_SACKDELAY) |
2116 (asoc->param_flags & ~SPP_SACKDELAY) |
2120 (sp->param_flags & ~SPP_SACKDELAY) |
2125 if (params->spp_pathmaxrxt) {
2127 trans->pathmaxrxt = params->spp_pathmaxrxt;
2129 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2131 sp->pathmaxrxt = params->spp_pathmaxrxt;
2138 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2139 char __user *optval, int optlen)
2141 struct sctp_paddrparams params;
2142 struct sctp_transport *trans = NULL;
2143 struct sctp_association *asoc = NULL;
2144 struct sctp_sock *sp = sctp_sk(sk);
2146 int hb_change, pmtud_change, sackdelay_change;
2148 if (optlen != sizeof(struct sctp_paddrparams))
2151 if (copy_from_user(¶ms, optval, optlen))
2154 /* Validate flags and value parameters. */
2155 hb_change = params.spp_flags & SPP_HB;
2156 pmtud_change = params.spp_flags & SPP_PMTUD;
2157 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2159 if (hb_change == SPP_HB ||
2160 pmtud_change == SPP_PMTUD ||
2161 sackdelay_change == SPP_SACKDELAY ||
2162 params.spp_sackdelay > 500 ||
2164 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2167 /* If an address other than INADDR_ANY is specified, and
2168 * no transport is found, then the request is invalid.
2170 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
2171 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2172 params.spp_assoc_id);
2177 /* Get association, if assoc_id != 0 and the socket is a one
2178 * to many style socket, and an association was not found, then
2179 * the id was invalid.
2181 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2182 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2185 /* Heartbeat demand can only be sent on a transport or
2186 * association, but not a socket.
2188 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2191 /* Process parameters. */
2192 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2193 hb_change, pmtud_change,
2199 /* If changes are for association, also apply parameters to each
2202 if (!trans && asoc) {
2203 struct list_head *pos;
2205 list_for_each(pos, &asoc->peer.transport_addr_list) {
2206 trans = list_entry(pos, struct sctp_transport,
2208 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2209 hb_change, pmtud_change,
2217 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2219 * This options will get or set the delayed ack timer. The time is set
2220 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2221 * endpoints default delayed ack timer value. If the assoc_id field is
2222 * non-zero, then the set or get effects the specified association.
2224 * struct sctp_assoc_value {
2225 * sctp_assoc_t assoc_id;
2226 * uint32_t assoc_value;
2229 * assoc_id - This parameter, indicates which association the
2230 * user is preforming an action upon. Note that if
2231 * this field's value is zero then the endpoints
2232 * default value is changed (effecting future
2233 * associations only).
2235 * assoc_value - This parameter contains the number of milliseconds
2236 * that the user is requesting the delayed ACK timer
2237 * be set to. Note that this value is defined in
2238 * the standard to be between 200 and 500 milliseconds.
2240 * Note: a value of zero will leave the value alone,
2241 * but disable SACK delay. A non-zero value will also
2242 * enable SACK delay.
2245 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2246 char __user *optval, int optlen)
2248 struct sctp_assoc_value params;
2249 struct sctp_transport *trans = NULL;
2250 struct sctp_association *asoc = NULL;
2251 struct sctp_sock *sp = sctp_sk(sk);
2253 if (optlen != sizeof(struct sctp_assoc_value))
2256 if (copy_from_user(¶ms, optval, optlen))
2259 /* Validate value parameter. */
2260 if (params.assoc_value > 500)
2263 /* Get association, if assoc_id != 0 and the socket is a one
2264 * to many style socket, and an association was not found, then
2265 * the id was invalid.
2267 asoc = sctp_id2assoc(sk, params.assoc_id);
2268 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2271 if (params.assoc_value) {
2274 msecs_to_jiffies(params.assoc_value);
2276 (asoc->param_flags & ~SPP_SACKDELAY) |
2277 SPP_SACKDELAY_ENABLE;
2279 sp->sackdelay = params.assoc_value;
2281 (sp->param_flags & ~SPP_SACKDELAY) |
2282 SPP_SACKDELAY_ENABLE;
2287 (asoc->param_flags & ~SPP_SACKDELAY) |
2288 SPP_SACKDELAY_DISABLE;
2291 (sp->param_flags & ~SPP_SACKDELAY) |
2292 SPP_SACKDELAY_DISABLE;
2296 /* If change is for association, also apply to each transport. */
2298 struct list_head *pos;
2300 list_for_each(pos, &asoc->peer.transport_addr_list) {
2301 trans = list_entry(pos, struct sctp_transport,
2303 if (params.assoc_value) {
2305 msecs_to_jiffies(params.assoc_value);
2306 trans->param_flags =
2307 (trans->param_flags & ~SPP_SACKDELAY) |
2308 SPP_SACKDELAY_ENABLE;
2310 trans->param_flags =
2311 (trans->param_flags & ~SPP_SACKDELAY) |
2312 SPP_SACKDELAY_DISABLE;
2320 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2322 * Applications can specify protocol parameters for the default association
2323 * initialization. The option name argument to setsockopt() and getsockopt()
2326 * Setting initialization parameters is effective only on an unconnected
2327 * socket (for UDP-style sockets only future associations are effected
2328 * by the change). With TCP-style sockets, this option is inherited by
2329 * sockets derived from a listener socket.
2331 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2333 struct sctp_initmsg sinit;
2334 struct sctp_sock *sp = sctp_sk(sk);
2336 if (optlen != sizeof(struct sctp_initmsg))
2338 if (copy_from_user(&sinit, optval, optlen))
2341 if (sinit.sinit_num_ostreams)
2342 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2343 if (sinit.sinit_max_instreams)
2344 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2345 if (sinit.sinit_max_attempts)
2346 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2347 if (sinit.sinit_max_init_timeo)
2348 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2354 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2356 * Applications that wish to use the sendto() system call may wish to
2357 * specify a default set of parameters that would normally be supplied
2358 * through the inclusion of ancillary data. This socket option allows
2359 * such an application to set the default sctp_sndrcvinfo structure.
2360 * The application that wishes to use this socket option simply passes
2361 * in to this call the sctp_sndrcvinfo structure defined in Section
2362 * 5.2.2) The input parameters accepted by this call include
2363 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2364 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2365 * to this call if the caller is using the UDP model.
2367 static int sctp_setsockopt_default_send_param(struct sock *sk,
2368 char __user *optval, int optlen)
2370 struct sctp_sndrcvinfo info;
2371 struct sctp_association *asoc;
2372 struct sctp_sock *sp = sctp_sk(sk);
2374 if (optlen != sizeof(struct sctp_sndrcvinfo))
2376 if (copy_from_user(&info, optval, optlen))
2379 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2380 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2384 asoc->default_stream = info.sinfo_stream;
2385 asoc->default_flags = info.sinfo_flags;
2386 asoc->default_ppid = info.sinfo_ppid;
2387 asoc->default_context = info.sinfo_context;
2388 asoc->default_timetolive = info.sinfo_timetolive;
2390 sp->default_stream = info.sinfo_stream;
2391 sp->default_flags = info.sinfo_flags;
2392 sp->default_ppid = info.sinfo_ppid;
2393 sp->default_context = info.sinfo_context;
2394 sp->default_timetolive = info.sinfo_timetolive;
2400 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2402 * Requests that the local SCTP stack use the enclosed peer address as
2403 * the association primary. The enclosed address must be one of the
2404 * association peer's addresses.
2406 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2409 struct sctp_prim prim;
2410 struct sctp_transport *trans;
2412 if (optlen != sizeof(struct sctp_prim))
2415 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2418 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2422 sctp_assoc_set_primary(trans->asoc, trans);
2428 * 7.1.5 SCTP_NODELAY
2430 * Turn on/off any Nagle-like algorithm. This means that packets are
2431 * generally sent as soon as possible and no unnecessary delays are
2432 * introduced, at the cost of more packets in the network. Expects an
2433 * integer boolean flag.
2435 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2440 if (optlen < sizeof(int))
2442 if (get_user(val, (int __user *)optval))
2445 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2451 * 7.1.1 SCTP_RTOINFO
2453 * The protocol parameters used to initialize and bound retransmission
2454 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2455 * and modify these parameters.
2456 * All parameters are time values, in milliseconds. A value of 0, when
2457 * modifying the parameters, indicates that the current value should not
2461 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2462 struct sctp_rtoinfo rtoinfo;
2463 struct sctp_association *asoc;
2465 if (optlen != sizeof (struct sctp_rtoinfo))
2468 if (copy_from_user(&rtoinfo, optval, optlen))
2471 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2473 /* Set the values to the specific association */
2474 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2478 if (rtoinfo.srto_initial != 0)
2480 msecs_to_jiffies(rtoinfo.srto_initial);
2481 if (rtoinfo.srto_max != 0)
2482 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2483 if (rtoinfo.srto_min != 0)
2484 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2486 /* If there is no association or the association-id = 0
2487 * set the values to the endpoint.
2489 struct sctp_sock *sp = sctp_sk(sk);
2491 if (rtoinfo.srto_initial != 0)
2492 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2493 if (rtoinfo.srto_max != 0)
2494 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2495 if (rtoinfo.srto_min != 0)
2496 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2504 * 7.1.2 SCTP_ASSOCINFO
2506 * This option is used to tune the the maximum retransmission attempts
2507 * of the association.
2508 * Returns an error if the new association retransmission value is
2509 * greater than the sum of the retransmission value of the peer.
2510 * See [SCTP] for more information.
2513 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2516 struct sctp_assocparams assocparams;
2517 struct sctp_association *asoc;
2519 if (optlen != sizeof(struct sctp_assocparams))
2521 if (copy_from_user(&assocparams, optval, optlen))
2524 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2526 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2529 /* Set the values to the specific association */
2531 if (assocparams.sasoc_asocmaxrxt != 0)
2532 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2533 if (assocparams.sasoc_cookie_life != 0) {
2534 asoc->cookie_life.tv_sec =
2535 assocparams.sasoc_cookie_life / 1000;
2536 asoc->cookie_life.tv_usec =
2537 (assocparams.sasoc_cookie_life % 1000)
2541 /* Set the values to the endpoint */
2542 struct sctp_sock *sp = sctp_sk(sk);
2544 if (assocparams.sasoc_asocmaxrxt != 0)
2545 sp->assocparams.sasoc_asocmaxrxt =
2546 assocparams.sasoc_asocmaxrxt;
2547 if (assocparams.sasoc_cookie_life != 0)
2548 sp->assocparams.sasoc_cookie_life =
2549 assocparams.sasoc_cookie_life;
2555 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2557 * This socket option is a boolean flag which turns on or off mapped V4
2558 * addresses. If this option is turned on and the socket is type
2559 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2560 * If this option is turned off, then no mapping will be done of V4
2561 * addresses and a user will receive both PF_INET6 and PF_INET type
2562 * addresses on the socket.
2564 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2567 struct sctp_sock *sp = sctp_sk(sk);
2569 if (optlen < sizeof(int))
2571 if (get_user(val, (int __user *)optval))
2582 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2584 * This socket option specifies the maximum size to put in any outgoing
2585 * SCTP chunk. If a message is larger than this size it will be
2586 * fragmented by SCTP into the specified size. Note that the underlying
2587 * SCTP implementation may fragment into smaller sized chunks when the
2588 * PMTU of the underlying association is smaller than the value set by
2591 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2593 struct sctp_association *asoc;
2594 struct list_head *pos;
2595 struct sctp_sock *sp = sctp_sk(sk);
2598 if (optlen < sizeof(int))
2600 if (get_user(val, (int __user *)optval))
2602 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2604 sp->user_frag = val;
2606 /* Update the frag_point of the existing associations. */
2607 list_for_each(pos, &(sp->ep->asocs)) {
2608 asoc = list_entry(pos, struct sctp_association, asocs);
2609 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2617 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2619 * Requests that the peer mark the enclosed address as the association
2620 * primary. The enclosed address must be one of the association's
2621 * locally bound addresses. The following structure is used to make a
2622 * set primary request:
2624 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2627 struct sctp_sock *sp;
2628 struct sctp_endpoint *ep;
2629 struct sctp_association *asoc = NULL;
2630 struct sctp_setpeerprim prim;
2631 struct sctp_chunk *chunk;
2637 if (!sctp_addip_enable)
2640 if (optlen != sizeof(struct sctp_setpeerprim))
2643 if (copy_from_user(&prim, optval, optlen))
2646 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2650 if (!asoc->peer.asconf_capable)
2653 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2656 if (!sctp_state(asoc, ESTABLISHED))
2659 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2660 return -EADDRNOTAVAIL;
2662 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2663 chunk = sctp_make_asconf_set_prim(asoc,
2664 (union sctp_addr *)&prim.sspp_addr);
2668 err = sctp_send_asconf(asoc, chunk);
2670 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2675 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2678 struct sctp_setadaption adaption;
2680 if (optlen != sizeof(struct sctp_setadaption))
2682 if (copy_from_user(&adaption, optval, optlen))
2685 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2690 /* API 6.2 setsockopt(), getsockopt()
2692 * Applications use setsockopt() and getsockopt() to set or retrieve
2693 * socket options. Socket options are used to change the default
2694 * behavior of sockets calls. They are described in Section 7.
2698 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2699 * int __user *optlen);
2700 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2703 * sd - the socket descript.
2704 * level - set to IPPROTO_SCTP for all SCTP options.
2705 * optname - the option name.
2706 * optval - the buffer to store the value of the option.
2707 * optlen - the size of the buffer.
2709 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2710 char __user *optval, int optlen)
2714 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2717 /* I can hardly begin to describe how wrong this is. This is
2718 * so broken as to be worse than useless. The API draft
2719 * REALLY is NOT helpful here... I am not convinced that the
2720 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2721 * are at all well-founded.
2723 if (level != SOL_SCTP) {
2724 struct sctp_af *af = sctp_sk(sk)->pf->af;
2725 retval = af->setsockopt(sk, level, optname, optval, optlen);
2732 case SCTP_SOCKOPT_BINDX_ADD:
2733 /* 'optlen' is the size of the addresses buffer. */
2734 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2735 optlen, SCTP_BINDX_ADD_ADDR);
2738 case SCTP_SOCKOPT_BINDX_REM:
2739 /* 'optlen' is the size of the addresses buffer. */
2740 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2741 optlen, SCTP_BINDX_REM_ADDR);
2744 case SCTP_SOCKOPT_CONNECTX:
2745 /* 'optlen' is the size of the addresses buffer. */
2746 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2750 case SCTP_DISABLE_FRAGMENTS:
2751 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2755 retval = sctp_setsockopt_events(sk, optval, optlen);
2758 case SCTP_AUTOCLOSE:
2759 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2762 case SCTP_PEER_ADDR_PARAMS:
2763 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2766 case SCTP_DELAYED_ACK_TIME:
2767 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2771 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2773 case SCTP_DEFAULT_SEND_PARAM:
2774 retval = sctp_setsockopt_default_send_param(sk, optval,
2777 case SCTP_PRIMARY_ADDR:
2778 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2780 case SCTP_SET_PEER_PRIMARY_ADDR:
2781 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2784 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2787 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2789 case SCTP_ASSOCINFO:
2790 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2792 case SCTP_I_WANT_MAPPED_V4_ADDR:
2793 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2796 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2798 case SCTP_ADAPTION_LAYER:
2799 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2803 retval = -ENOPROTOOPT;
2807 sctp_release_sock(sk);
2813 /* API 3.1.6 connect() - UDP Style Syntax
2815 * An application may use the connect() call in the UDP model to initiate an
2816 * association without sending data.
2820 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2822 * sd: the socket descriptor to have a new association added to.
2824 * nam: the address structure (either struct sockaddr_in or struct
2825 * sockaddr_in6 defined in RFC2553 [7]).
2827 * len: the size of the address.
2829 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2837 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2838 __FUNCTION__, sk, addr, addr_len);
2840 /* Validate addr_len before calling common connect/connectx routine. */
2841 af = sctp_get_af_specific(addr->sa_family);
2842 if (!af || addr_len < af->sockaddr_len) {
2845 /* Pass correct addr len to common routine (so it knows there
2846 * is only one address being passed.
2848 err = __sctp_connect(sk, addr, af->sockaddr_len);
2851 sctp_release_sock(sk);
2855 /* FIXME: Write comments. */
2856 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2858 return -EOPNOTSUPP; /* STUB */
2861 /* 4.1.4 accept() - TCP Style Syntax
2863 * Applications use accept() call to remove an established SCTP
2864 * association from the accept queue of the endpoint. A new socket
2865 * descriptor will be returned from accept() to represent the newly
2866 * formed association.
2868 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2870 struct sctp_sock *sp;
2871 struct sctp_endpoint *ep;
2872 struct sock *newsk = NULL;
2873 struct sctp_association *asoc;
2882 if (!sctp_style(sk, TCP)) {
2883 error = -EOPNOTSUPP;
2887 if (!sctp_sstate(sk, LISTENING)) {
2892 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2894 error = sctp_wait_for_accept(sk, timeo);
2898 /* We treat the list of associations on the endpoint as the accept
2899 * queue and pick the first association on the list.
2901 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2903 newsk = sp->pf->create_accept_sk(sk, asoc);
2909 /* Populate the fields of the newsk from the oldsk and migrate the
2910 * asoc to the newsk.
2912 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2915 sctp_release_sock(sk);
2920 /* The SCTP ioctl handler. */
2921 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2923 return -ENOIOCTLCMD;
2926 /* This is the function which gets called during socket creation to
2927 * initialized the SCTP-specific portion of the sock.
2928 * The sock structure should already be zero-filled memory.
2930 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2932 struct sctp_endpoint *ep;
2933 struct sctp_sock *sp;
2935 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2939 /* Initialize the SCTP per socket area. */
2940 switch (sk->sk_type) {
2941 case SOCK_SEQPACKET:
2942 sp->type = SCTP_SOCKET_UDP;
2945 sp->type = SCTP_SOCKET_TCP;
2948 return -ESOCKTNOSUPPORT;
2951 /* Initialize default send parameters. These parameters can be
2952 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2954 sp->default_stream = 0;
2955 sp->default_ppid = 0;
2956 sp->default_flags = 0;
2957 sp->default_context = 0;
2958 sp->default_timetolive = 0;
2960 /* Initialize default setup parameters. These parameters
2961 * can be modified with the SCTP_INITMSG socket option or
2962 * overridden by the SCTP_INIT CMSG.
2964 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2965 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2966 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2967 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2969 /* Initialize default RTO related parameters. These parameters can
2970 * be modified for with the SCTP_RTOINFO socket option.
2972 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2973 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2974 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2976 /* Initialize default association related parameters. These parameters
2977 * can be modified with the SCTP_ASSOCINFO socket option.
2979 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2980 sp->assocparams.sasoc_number_peer_destinations = 0;
2981 sp->assocparams.sasoc_peer_rwnd = 0;
2982 sp->assocparams.sasoc_local_rwnd = 0;
2983 sp->assocparams.sasoc_cookie_life =
2984 jiffies_to_msecs(sctp_valid_cookie_life);
2986 /* Initialize default event subscriptions. By default, all the
2989 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2991 /* Default Peer Address Parameters. These defaults can
2992 * be modified via SCTP_PEER_ADDR_PARAMS
2994 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
2995 sp->pathmaxrxt = sctp_max_retrans_path;
2996 sp->pathmtu = 0; // allow default discovery
2997 sp->sackdelay = sctp_sack_timeout;
2998 sp->param_flags = SPP_HB_ENABLE |
3000 SPP_SACKDELAY_ENABLE;
3002 /* If enabled no SCTP message fragmentation will be performed.
3003 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3005 sp->disable_fragments = 0;
3007 /* Turn on/off any Nagle-like algorithm. */
3010 /* Enable by default. */
3013 /* Auto-close idle associations after the configured
3014 * number of seconds. A value of 0 disables this
3015 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3016 * for UDP-style sockets only.
3020 /* User specified fragmentation limit. */
3023 sp->adaption_ind = 0;
3025 sp->pf = sctp_get_pf_specific(sk->sk_family);
3027 /* Control variables for partial data delivery. */
3029 skb_queue_head_init(&sp->pd_lobby);
3031 /* Create a per socket endpoint structure. Even if we
3032 * change the data structure relationships, this may still
3033 * be useful for storing pre-connect address information.
3035 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3042 SCTP_DBG_OBJCNT_INC(sock);
3046 /* Cleanup any SCTP per socket resources. */
3047 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3049 struct sctp_endpoint *ep;
3051 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3053 /* Release our hold on the endpoint. */
3054 ep = sctp_sk(sk)->ep;
3055 sctp_endpoint_free(ep);
3060 /* API 4.1.7 shutdown() - TCP Style Syntax
3061 * int shutdown(int socket, int how);
3063 * sd - the socket descriptor of the association to be closed.
3064 * how - Specifies the type of shutdown. The values are
3067 * Disables further receive operations. No SCTP
3068 * protocol action is taken.
3070 * Disables further send operations, and initiates
3071 * the SCTP shutdown sequence.
3073 * Disables further send and receive operations
3074 * and initiates the SCTP shutdown sequence.
3076 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3078 struct sctp_endpoint *ep;
3079 struct sctp_association *asoc;
3081 if (!sctp_style(sk, TCP))
3084 if (how & SEND_SHUTDOWN) {
3085 ep = sctp_sk(sk)->ep;
3086 if (!list_empty(&ep->asocs)) {
3087 asoc = list_entry(ep->asocs.next,
3088 struct sctp_association, asocs);
3089 sctp_primitive_SHUTDOWN(asoc, NULL);
3094 /* 7.2.1 Association Status (SCTP_STATUS)
3096 * Applications can retrieve current status information about an
3097 * association, including association state, peer receiver window size,
3098 * number of unacked data chunks, and number of data chunks pending
3099 * receipt. This information is read-only.
3101 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3102 char __user *optval,
3105 struct sctp_status status;
3106 struct sctp_association *asoc = NULL;
3107 struct sctp_transport *transport;
3108 sctp_assoc_t associd;
3111 if (len != sizeof(status)) {
3116 if (copy_from_user(&status, optval, sizeof(status))) {
3121 associd = status.sstat_assoc_id;
3122 asoc = sctp_id2assoc(sk, associd);
3128 transport = asoc->peer.primary_path;
3130 status.sstat_assoc_id = sctp_assoc2id(asoc);
3131 status.sstat_state = asoc->state;
3132 status.sstat_rwnd = asoc->peer.rwnd;
3133 status.sstat_unackdata = asoc->unack_data;
3135 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3136 status.sstat_instrms = asoc->c.sinit_max_instreams;
3137 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3138 status.sstat_fragmentation_point = asoc->frag_point;
3139 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3140 memcpy(&status.sstat_primary.spinfo_address,
3141 &(transport->ipaddr), sizeof(union sctp_addr));
3142 /* Map ipv4 address into v4-mapped-on-v6 address. */
3143 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3144 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3145 status.sstat_primary.spinfo_state = transport->state;
3146 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3147 status.sstat_primary.spinfo_srtt = transport->srtt;
3148 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3149 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3151 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3152 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3154 if (put_user(len, optlen)) {
3159 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3160 len, status.sstat_state, status.sstat_rwnd,
3161 status.sstat_assoc_id);
3163 if (copy_to_user(optval, &status, len)) {
3173 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3175 * Applications can retrieve information about a specific peer address
3176 * of an association, including its reachability state, congestion
3177 * window, and retransmission timer values. This information is
3180 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3181 char __user *optval,
3184 struct sctp_paddrinfo pinfo;
3185 struct sctp_transport *transport;
3188 if (len != sizeof(pinfo)) {
3193 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3198 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3199 pinfo.spinfo_assoc_id);
3203 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3204 pinfo.spinfo_state = transport->state;
3205 pinfo.spinfo_cwnd = transport->cwnd;
3206 pinfo.spinfo_srtt = transport->srtt;
3207 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3208 pinfo.spinfo_mtu = transport->pathmtu;
3210 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3211 pinfo.spinfo_state = SCTP_ACTIVE;
3213 if (put_user(len, optlen)) {
3218 if (copy_to_user(optval, &pinfo, len)) {
3227 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3229 * This option is a on/off flag. If enabled no SCTP message
3230 * fragmentation will be performed. Instead if a message being sent
3231 * exceeds the current PMTU size, the message will NOT be sent and
3232 * instead a error will be indicated to the user.
3234 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3235 char __user *optval, int __user *optlen)
3239 if (len < sizeof(int))
3243 val = (sctp_sk(sk)->disable_fragments == 1);
3244 if (put_user(len, optlen))
3246 if (copy_to_user(optval, &val, len))
3251 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3253 * This socket option is used to specify various notifications and
3254 * ancillary data the user wishes to receive.
3256 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3259 if (len != sizeof(struct sctp_event_subscribe))
3261 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3266 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3268 * This socket option is applicable to the UDP-style socket only. When
3269 * set it will cause associations that are idle for more than the
3270 * specified number of seconds to automatically close. An association
3271 * being idle is defined an association that has NOT sent or received
3272 * user data. The special value of '0' indicates that no automatic
3273 * close of any associations should be performed. The option expects an
3274 * integer defining the number of seconds of idle time before an
3275 * association is closed.
3277 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3279 /* Applicable to UDP-style socket only */
3280 if (sctp_style(sk, TCP))
3282 if (len != sizeof(int))
3284 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3289 /* Helper routine to branch off an association to a new socket. */
3290 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3291 struct socket **sockp)
3293 struct sock *sk = asoc->base.sk;
3294 struct socket *sock;
3297 /* An association cannot be branched off from an already peeled-off
3298 * socket, nor is this supported for tcp style sockets.
3300 if (!sctp_style(sk, UDP))
3303 /* Create a new socket. */
3304 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3308 /* Populate the fields of the newsk from the oldsk and migrate the
3309 * asoc to the newsk.
3311 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3317 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3319 sctp_peeloff_arg_t peeloff;
3320 struct socket *newsock;
3322 struct sctp_association *asoc;
3324 if (len != sizeof(sctp_peeloff_arg_t))
3326 if (copy_from_user(&peeloff, optval, len))
3329 asoc = sctp_id2assoc(sk, peeloff.associd);
3335 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3337 retval = sctp_do_peeloff(asoc, &newsock);
3341 /* Map the socket to an unused fd that can be returned to the user. */
3342 retval = sock_map_fd(newsock);
3344 sock_release(newsock);
3348 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3349 __FUNCTION__, sk, asoc, newsock->sk, retval);
3351 /* Return the fd mapped to the new socket. */
3352 peeloff.sd = retval;
3353 if (copy_to_user(optval, &peeloff, len))
3360 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3362 * Applications can enable or disable heartbeats for any peer address of
3363 * an association, modify an address's heartbeat interval, force a
3364 * heartbeat to be sent immediately, and adjust the address's maximum
3365 * number of retransmissions sent before an address is considered
3366 * unreachable. The following structure is used to access and modify an
3367 * address's parameters:
3369 * struct sctp_paddrparams {
3370 * sctp_assoc_t spp_assoc_id;
3371 * struct sockaddr_storage spp_address;
3372 * uint32_t spp_hbinterval;
3373 * uint16_t spp_pathmaxrxt;
3374 * uint32_t spp_pathmtu;
3375 * uint32_t spp_sackdelay;
3376 * uint32_t spp_flags;
3379 * spp_assoc_id - (one-to-many style socket) This is filled in the
3380 * application, and identifies the association for
3382 * spp_address - This specifies which address is of interest.
3383 * spp_hbinterval - This contains the value of the heartbeat interval,
3384 * in milliseconds. If a value of zero
3385 * is present in this field then no changes are to
3386 * be made to this parameter.
3387 * spp_pathmaxrxt - This contains the maximum number of
3388 * retransmissions before this address shall be
3389 * considered unreachable. If a value of zero
3390 * is present in this field then no changes are to
3391 * be made to this parameter.
3392 * spp_pathmtu - When Path MTU discovery is disabled the value
3393 * specified here will be the "fixed" path mtu.
3394 * Note that if the spp_address field is empty
3395 * then all associations on this address will
3396 * have this fixed path mtu set upon them.
3398 * spp_sackdelay - When delayed sack is enabled, this value specifies
3399 * the number of milliseconds that sacks will be delayed
3400 * for. This value will apply to all addresses of an
3401 * association if the spp_address field is empty. Note
3402 * also, that if delayed sack is enabled and this
3403 * value is set to 0, no change is made to the last
3404 * recorded delayed sack timer value.
3406 * spp_flags - These flags are used to control various features
3407 * on an association. The flag field may contain
3408 * zero or more of the following options.
3410 * SPP_HB_ENABLE - Enable heartbeats on the
3411 * specified address. Note that if the address
3412 * field is empty all addresses for the association
3413 * have heartbeats enabled upon them.
3415 * SPP_HB_DISABLE - Disable heartbeats on the
3416 * speicifed address. Note that if the address
3417 * field is empty all addresses for the association
3418 * will have their heartbeats disabled. Note also
3419 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3420 * mutually exclusive, only one of these two should
3421 * be specified. Enabling both fields will have
3422 * undetermined results.
3424 * SPP_HB_DEMAND - Request a user initiated heartbeat
3425 * to be made immediately.
3427 * SPP_PMTUD_ENABLE - This field will enable PMTU
3428 * discovery upon the specified address. Note that
3429 * if the address feild is empty then all addresses
3430 * on the association are effected.
3432 * SPP_PMTUD_DISABLE - This field will disable PMTU
3433 * discovery upon the specified address. Note that
3434 * if the address feild is empty then all addresses
3435 * on the association are effected. Not also that
3436 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3437 * exclusive. Enabling both will have undetermined
3440 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3441 * on delayed sack. The time specified in spp_sackdelay
3442 * is used to specify the sack delay for this address. Note
3443 * that if spp_address is empty then all addresses will
3444 * enable delayed sack and take on the sack delay
3445 * value specified in spp_sackdelay.
3446 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3447 * off delayed sack. If the spp_address field is blank then
3448 * delayed sack is disabled for the entire association. Note
3449 * also that this field is mutually exclusive to
3450 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3453 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3454 char __user *optval, int __user *optlen)
3456 struct sctp_paddrparams params;
3457 struct sctp_transport *trans = NULL;
3458 struct sctp_association *asoc = NULL;
3459 struct sctp_sock *sp = sctp_sk(sk);
3461 if (len != sizeof(struct sctp_paddrparams))
3464 if (copy_from_user(¶ms, optval, len))
3467 /* If an address other than INADDR_ANY is specified, and
3468 * no transport is found, then the request is invalid.
3470 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
3471 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
3472 params.spp_assoc_id);
3474 SCTP_DEBUG_PRINTK("Failed no transport\n");
3479 /* Get association, if assoc_id != 0 and the socket is a one
3480 * to many style socket, and an association was not found, then
3481 * the id was invalid.
3483 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3484 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3485 SCTP_DEBUG_PRINTK("Failed no association\n");
3490 /* Fetch transport values. */
3491 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3492 params.spp_pathmtu = trans->pathmtu;
3493 params.spp_pathmaxrxt = trans->pathmaxrxt;
3494 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3496 /*draft-11 doesn't say what to return in spp_flags*/
3497 params.spp_flags = trans->param_flags;
3499 /* Fetch association values. */
3500 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3501 params.spp_pathmtu = asoc->pathmtu;
3502 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3503 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3505 /*draft-11 doesn't say what to return in spp_flags*/
3506 params.spp_flags = asoc->param_flags;
3508 /* Fetch socket values. */
3509 params.spp_hbinterval = sp->hbinterval;
3510 params.spp_pathmtu = sp->pathmtu;
3511 params.spp_sackdelay = sp->sackdelay;
3512 params.spp_pathmaxrxt = sp->pathmaxrxt;
3514 /*draft-11 doesn't say what to return in spp_flags*/
3515 params.spp_flags = sp->param_flags;
3518 if (copy_to_user(optval, ¶ms, len))
3521 if (put_user(len, optlen))
3527 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3529 * This options will get or set the delayed ack timer. The time is set
3530 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3531 * endpoints default delayed ack timer value. If the assoc_id field is
3532 * non-zero, then the set or get effects the specified association.
3534 * struct sctp_assoc_value {
3535 * sctp_assoc_t assoc_id;
3536 * uint32_t assoc_value;
3539 * assoc_id - This parameter, indicates which association the
3540 * user is preforming an action upon. Note that if
3541 * this field's value is zero then the endpoints
3542 * default value is changed (effecting future
3543 * associations only).
3545 * assoc_value - This parameter contains the number of milliseconds
3546 * that the user is requesting the delayed ACK timer
3547 * be set to. Note that this value is defined in
3548 * the standard to be between 200 and 500 milliseconds.
3550 * Note: a value of zero will leave the value alone,
3551 * but disable SACK delay. A non-zero value will also
3552 * enable SACK delay.
3554 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3555 char __user *optval,
3558 struct sctp_assoc_value params;
3559 struct sctp_association *asoc = NULL;
3560 struct sctp_sock *sp = sctp_sk(sk);
3562 if (len != sizeof(struct sctp_assoc_value))
3565 if (copy_from_user(¶ms, optval, len))
3568 /* Get association, if assoc_id != 0 and the socket is a one
3569 * to many style socket, and an association was not found, then
3570 * the id was invalid.
3572 asoc = sctp_id2assoc(sk, params.assoc_id);
3573 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3577 /* Fetch association values. */
3578 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3579 params.assoc_value = jiffies_to_msecs(
3582 params.assoc_value = 0;
3584 /* Fetch socket values. */
3585 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3586 params.assoc_value = sp->sackdelay;
3588 params.assoc_value = 0;
3591 if (copy_to_user(optval, ¶ms, len))
3594 if (put_user(len, optlen))
3600 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3602 * Applications can specify protocol parameters for the default association
3603 * initialization. The option name argument to setsockopt() and getsockopt()
3606 * Setting initialization parameters is effective only on an unconnected
3607 * socket (for UDP-style sockets only future associations are effected
3608 * by the change). With TCP-style sockets, this option is inherited by
3609 * sockets derived from a listener socket.
3611 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3613 if (len != sizeof(struct sctp_initmsg))
3615 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3620 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3621 char __user *optval,
3625 struct sctp_association *asoc;
3626 struct list_head *pos;
3629 if (len != sizeof(sctp_assoc_t))
3632 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3635 /* For UDP-style sockets, id specifies the association to query. */
3636 asoc = sctp_id2assoc(sk, id);
3640 list_for_each(pos, &asoc->peer.transport_addr_list) {
3648 * Old API for getting list of peer addresses. Does not work for 32-bit
3649 * programs running on a 64-bit kernel
3651 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3652 char __user *optval,
3655 struct sctp_association *asoc;
3656 struct list_head *pos;
3658 struct sctp_getaddrs_old getaddrs;
3659 struct sctp_transport *from;
3661 union sctp_addr temp;
3662 struct sctp_sock *sp = sctp_sk(sk);
3665 if (len != sizeof(struct sctp_getaddrs_old))
3668 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3671 if (getaddrs.addr_num <= 0) return -EINVAL;
3673 /* For UDP-style sockets, id specifies the association to query. */
3674 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3678 to = (void __user *)getaddrs.addrs;
3679 list_for_each(pos, &asoc->peer.transport_addr_list) {
3680 from = list_entry(pos, struct sctp_transport, transports);
3681 memcpy(&temp, &from->ipaddr, sizeof(temp));
3682 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3683 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3684 temp.v4.sin_port = htons(temp.v4.sin_port);
3685 if (copy_to_user(to, &temp, addrlen))
3689 if (cnt >= getaddrs.addr_num) break;
3691 getaddrs.addr_num = cnt;
3692 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3698 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3699 char __user *optval, int __user *optlen)
3701 struct sctp_association *asoc;
3702 struct list_head *pos;
3704 struct sctp_getaddrs getaddrs;
3705 struct sctp_transport *from;
3707 union sctp_addr temp;
3708 struct sctp_sock *sp = sctp_sk(sk);
3713 if (len < sizeof(struct sctp_getaddrs))
3716 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3719 /* For UDP-style sockets, id specifies the association to query. */
3720 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3724 to = optval + offsetof(struct sctp_getaddrs,addrs);
3725 space_left = len - sizeof(struct sctp_getaddrs) -
3726 offsetof(struct sctp_getaddrs,addrs);
3728 list_for_each(pos, &asoc->peer.transport_addr_list) {
3729 from = list_entry(pos, struct sctp_transport, transports);
3730 memcpy(&temp, &from->ipaddr, sizeof(temp));
3731 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3732 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3733 if(space_left < addrlen)
3735 temp.v4.sin_port = htons(temp.v4.sin_port);
3736 if (copy_to_user(to, &temp, addrlen))
3740 space_left -= addrlen;
3743 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3745 bytes_copied = ((char __user *)to) - optval;
3746 if (put_user(bytes_copied, optlen))
3752 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3753 char __user *optval,
3757 struct sctp_bind_addr *bp;
3758 struct sctp_association *asoc;
3759 struct list_head *pos;
3760 struct sctp_sockaddr_entry *addr;
3761 rwlock_t *addr_lock;
3762 unsigned long flags;
3765 if (len != sizeof(sctp_assoc_t))
3768 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3772 * For UDP-style sockets, id specifies the association to query.
3773 * If the id field is set to the value '0' then the locally bound
3774 * addresses are returned without regard to any particular
3778 bp = &sctp_sk(sk)->ep->base.bind_addr;
3779 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3781 asoc = sctp_id2assoc(sk, id);
3784 bp = &asoc->base.bind_addr;
3785 addr_lock = &asoc->base.addr_lock;
3788 sctp_read_lock(addr_lock);
3790 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3791 * addresses from the global local address list.
3793 if (sctp_list_single_entry(&bp->address_list)) {
3794 addr = list_entry(bp->address_list.next,
3795 struct sctp_sockaddr_entry, list);
3796 if (sctp_is_any(&addr->a)) {
3797 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3798 list_for_each(pos, &sctp_local_addr_list) {
3799 addr = list_entry(pos,
3800 struct sctp_sockaddr_entry,
3802 if ((PF_INET == sk->sk_family) &&
3803 (AF_INET6 == addr->a.sa.sa_family))
3807 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3815 list_for_each(pos, &bp->address_list) {
3820 sctp_read_unlock(addr_lock);
3824 /* Helper function that copies local addresses to user and returns the number
3825 * of addresses copied.
3827 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3830 struct list_head *pos;
3831 struct sctp_sockaddr_entry *addr;
3832 unsigned long flags;
3833 union sctp_addr temp;
3837 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3838 list_for_each(pos, &sctp_local_addr_list) {
3839 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3840 if ((PF_INET == sk->sk_family) &&
3841 (AF_INET6 == addr->a.sa.sa_family))
3843 memcpy(&temp, &addr->a, sizeof(temp));
3844 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3846 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3847 temp.v4.sin_port = htons(port);
3848 if (copy_to_user(to, &temp, addrlen)) {
3849 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3855 if (cnt >= max_addrs) break;
3857 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3862 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3863 void __user **to, size_t space_left)
3865 struct list_head *pos;
3866 struct sctp_sockaddr_entry *addr;
3867 unsigned long flags;
3868 union sctp_addr temp;
3872 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3873 list_for_each(pos, &sctp_local_addr_list) {
3874 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3875 if ((PF_INET == sk->sk_family) &&
3876 (AF_INET6 == addr->a.sa.sa_family))
3878 memcpy(&temp, &addr->a, sizeof(temp));
3879 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3881 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3882 if(space_left<addrlen)
3884 temp.v4.sin_port = htons(port);
3885 if (copy_to_user(*to, &temp, addrlen)) {
3886 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3892 space_left -= addrlen;
3894 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3899 /* Old API for getting list of local addresses. Does not work for 32-bit
3900 * programs running on a 64-bit kernel
3902 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3903 char __user *optval, int __user *optlen)
3905 struct sctp_bind_addr *bp;
3906 struct sctp_association *asoc;
3907 struct list_head *pos;
3909 struct sctp_getaddrs_old getaddrs;
3910 struct sctp_sockaddr_entry *addr;
3912 union sctp_addr temp;
3913 struct sctp_sock *sp = sctp_sk(sk);
3915 rwlock_t *addr_lock;
3918 if (len != sizeof(struct sctp_getaddrs_old))
3921 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3924 if (getaddrs.addr_num <= 0) return -EINVAL;
3926 * For UDP-style sockets, id specifies the association to query.
3927 * If the id field is set to the value '0' then the locally bound
3928 * addresses are returned without regard to any particular
3931 if (0 == getaddrs.assoc_id) {
3932 bp = &sctp_sk(sk)->ep->base.bind_addr;
3933 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3935 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3938 bp = &asoc->base.bind_addr;
3939 addr_lock = &asoc->base.addr_lock;
3942 to = getaddrs.addrs;
3944 sctp_read_lock(addr_lock);
3946 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3947 * addresses from the global local address list.
3949 if (sctp_list_single_entry(&bp->address_list)) {
3950 addr = list_entry(bp->address_list.next,
3951 struct sctp_sockaddr_entry, list);
3952 if (sctp_is_any(&addr->a)) {
3953 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3964 list_for_each(pos, &bp->address_list) {
3965 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3966 memcpy(&temp, &addr->a, sizeof(temp));
3967 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3968 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3969 temp.v4.sin_port = htons(temp.v4.sin_port);
3970 if (copy_to_user(to, &temp, addrlen)) {
3976 if (cnt >= getaddrs.addr_num) break;
3980 getaddrs.addr_num = cnt;
3981 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3985 sctp_read_unlock(addr_lock);
3989 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3990 char __user *optval, int __user *optlen)
3992 struct sctp_bind_addr *bp;
3993 struct sctp_association *asoc;
3994 struct list_head *pos;
3996 struct sctp_getaddrs getaddrs;
3997 struct sctp_sockaddr_entry *addr;
3999 union sctp_addr temp;
4000 struct sctp_sock *sp = sctp_sk(sk);
4002 rwlock_t *addr_lock;
4007 if (len <= sizeof(struct sctp_getaddrs))
4010 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4014 * For UDP-style sockets, id specifies the association to query.
4015 * If the id field is set to the value '0' then the locally bound
4016 * addresses are returned without regard to any particular
4019 if (0 == getaddrs.assoc_id) {
4020 bp = &sctp_sk(sk)->ep->base.bind_addr;
4021 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4023 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4026 bp = &asoc->base.bind_addr;
4027 addr_lock = &asoc->base.addr_lock;
4030 to = optval + offsetof(struct sctp_getaddrs,addrs);
4031 space_left = len - sizeof(struct sctp_getaddrs) -
4032 offsetof(struct sctp_getaddrs,addrs);
4034 sctp_read_lock(addr_lock);
4036 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4037 * addresses from the global local address list.
4039 if (sctp_list_single_entry(&bp->address_list)) {
4040 addr = list_entry(bp->address_list.next,
4041 struct sctp_sockaddr_entry, list);
4042 if (sctp_is_any(&addr->a)) {
4043 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4053 list_for_each(pos, &bp->address_list) {
4054 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4055 memcpy(&temp, &addr->a, sizeof(temp));
4056 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4057 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4058 if(space_left < addrlen)
4059 return -ENOMEM; /*fixme: right error?*/
4060 temp.v4.sin_port = htons(temp.v4.sin_port);
4061 if (copy_to_user(to, &temp, addrlen)) {
4067 space_left -= addrlen;
4071 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4073 bytes_copied = ((char __user *)to) - optval;
4074 if (put_user(bytes_copied, optlen))
4078 sctp_read_unlock(addr_lock);
4082 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4084 * Requests that the local SCTP stack use the enclosed peer address as
4085 * the association primary. The enclosed address must be one of the
4086 * association peer's addresses.
4088 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4089 char __user *optval, int __user *optlen)
4091 struct sctp_prim prim;
4092 struct sctp_association *asoc;
4093 struct sctp_sock *sp = sctp_sk(sk);
4095 if (len != sizeof(struct sctp_prim))
4098 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4101 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4105 if (!asoc->peer.primary_path)
4108 asoc->peer.primary_path->ipaddr.v4.sin_port =
4109 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4110 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4111 sizeof(union sctp_addr));
4112 asoc->peer.primary_path->ipaddr.v4.sin_port =
4113 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4115 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4116 (union sctp_addr *)&prim.ssp_addr);
4118 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4125 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4127 * Requests that the local endpoint set the specified Adaption Layer
4128 * Indication parameter for all future INIT and INIT-ACK exchanges.
4130 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4131 char __user *optval, int __user *optlen)
4133 struct sctp_setadaption adaption;
4135 if (len != sizeof(struct sctp_setadaption))
4138 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4139 if (copy_to_user(optval, &adaption, len))
4147 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4149 * Applications that wish to use the sendto() system call may wish to
4150 * specify a default set of parameters that would normally be supplied
4151 * through the inclusion of ancillary data. This socket option allows
4152 * such an application to set the default sctp_sndrcvinfo structure.
4155 * The application that wishes to use this socket option simply passes
4156 * in to this call the sctp_sndrcvinfo structure defined in Section
4157 * 5.2.2) The input parameters accepted by this call include
4158 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4159 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4160 * to this call if the caller is using the UDP model.
4162 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4164 static int sctp_getsockopt_default_send_param(struct sock *sk,
4165 int len, char __user *optval,
4168 struct sctp_sndrcvinfo info;
4169 struct sctp_association *asoc;
4170 struct sctp_sock *sp = sctp_sk(sk);
4172 if (len != sizeof(struct sctp_sndrcvinfo))
4174 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4177 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4178 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4182 info.sinfo_stream = asoc->default_stream;
4183 info.sinfo_flags = asoc->default_flags;
4184 info.sinfo_ppid = asoc->default_ppid;
4185 info.sinfo_context = asoc->default_context;
4186 info.sinfo_timetolive = asoc->default_timetolive;
4188 info.sinfo_stream = sp->default_stream;
4189 info.sinfo_flags = sp->default_flags;
4190 info.sinfo_ppid = sp->default_ppid;
4191 info.sinfo_context = sp->default_context;
4192 info.sinfo_timetolive = sp->default_timetolive;
4195 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4203 * 7.1.5 SCTP_NODELAY
4205 * Turn on/off any Nagle-like algorithm. This means that packets are
4206 * generally sent as soon as possible and no unnecessary delays are
4207 * introduced, at the cost of more packets in the network. Expects an
4208 * integer boolean flag.
4211 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4212 char __user *optval, int __user *optlen)
4216 if (len < sizeof(int))
4220 val = (sctp_sk(sk)->nodelay == 1);
4221 if (put_user(len, optlen))
4223 if (copy_to_user(optval, &val, len))
4230 * 7.1.1 SCTP_RTOINFO
4232 * The protocol parameters used to initialize and bound retransmission
4233 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4234 * and modify these parameters.
4235 * All parameters are time values, in milliseconds. A value of 0, when
4236 * modifying the parameters, indicates that the current value should not
4240 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4241 char __user *optval,
4242 int __user *optlen) {
4243 struct sctp_rtoinfo rtoinfo;
4244 struct sctp_association *asoc;
4246 if (len != sizeof (struct sctp_rtoinfo))
4249 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4252 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4254 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4257 /* Values corresponding to the specific association. */
4259 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4260 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4261 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4263 /* Values corresponding to the endpoint. */
4264 struct sctp_sock *sp = sctp_sk(sk);
4266 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4267 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4268 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4271 if (put_user(len, optlen))
4274 if (copy_to_user(optval, &rtoinfo, len))
4282 * 7.1.2 SCTP_ASSOCINFO
4284 * This option is used to tune the the maximum retransmission attempts
4285 * of the association.
4286 * Returns an error if the new association retransmission value is
4287 * greater than the sum of the retransmission value of the peer.
4288 * See [SCTP] for more information.
4291 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4292 char __user *optval,
4296 struct sctp_assocparams assocparams;
4297 struct sctp_association *asoc;
4298 struct list_head *pos;
4301 if (len != sizeof (struct sctp_assocparams))
4304 if (copy_from_user(&assocparams, optval,
4305 sizeof (struct sctp_assocparams)))
4308 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4310 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4313 /* Values correspoinding to the specific association */
4315 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4316 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4317 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4318 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4320 (asoc->cookie_life.tv_usec
4323 list_for_each(pos, &asoc->peer.transport_addr_list) {
4327 assocparams.sasoc_number_peer_destinations = cnt;
4329 /* Values corresponding to the endpoint */
4330 struct sctp_sock *sp = sctp_sk(sk);
4332 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4333 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4334 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4335 assocparams.sasoc_cookie_life =
4336 sp->assocparams.sasoc_cookie_life;
4337 assocparams.sasoc_number_peer_destinations =
4339 sasoc_number_peer_destinations;
4342 if (put_user(len, optlen))
4345 if (copy_to_user(optval, &assocparams, len))
4352 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4354 * This socket option is a boolean flag which turns on or off mapped V4
4355 * addresses. If this option is turned on and the socket is type
4356 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4357 * If this option is turned off, then no mapping will be done of V4
4358 * addresses and a user will receive both PF_INET6 and PF_INET type
4359 * addresses on the socket.
4361 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4362 char __user *optval, int __user *optlen)
4365 struct sctp_sock *sp = sctp_sk(sk);
4367 if (len < sizeof(int))
4372 if (put_user(len, optlen))
4374 if (copy_to_user(optval, &val, len))
4381 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4383 * This socket option specifies the maximum size to put in any outgoing
4384 * SCTP chunk. If a message is larger than this size it will be
4385 * fragmented by SCTP into the specified size. Note that the underlying
4386 * SCTP implementation may fragment into smaller sized chunks when the
4387 * PMTU of the underlying association is smaller than the value set by
4390 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4391 char __user *optval, int __user *optlen)
4395 if (len < sizeof(int))
4400 val = sctp_sk(sk)->user_frag;
4401 if (put_user(len, optlen))
4403 if (copy_to_user(optval, &val, len))
4409 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4410 char __user *optval, int __user *optlen)
4415 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4418 /* I can hardly begin to describe how wrong this is. This is
4419 * so broken as to be worse than useless. The API draft
4420 * REALLY is NOT helpful here... I am not convinced that the
4421 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4422 * are at all well-founded.
4424 if (level != SOL_SCTP) {
4425 struct sctp_af *af = sctp_sk(sk)->pf->af;
4427 retval = af->getsockopt(sk, level, optname, optval, optlen);
4431 if (get_user(len, optlen))
4438 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4440 case SCTP_DISABLE_FRAGMENTS:
4441 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4445 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4447 case SCTP_AUTOCLOSE:
4448 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4450 case SCTP_SOCKOPT_PEELOFF:
4451 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4453 case SCTP_PEER_ADDR_PARAMS:
4454 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4457 case SCTP_DELAYED_ACK_TIME:
4458 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4462 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4464 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4465 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4468 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4469 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4472 case SCTP_GET_PEER_ADDRS_OLD:
4473 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4476 case SCTP_GET_LOCAL_ADDRS_OLD:
4477 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4480 case SCTP_GET_PEER_ADDRS:
4481 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4484 case SCTP_GET_LOCAL_ADDRS:
4485 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4488 case SCTP_DEFAULT_SEND_PARAM:
4489 retval = sctp_getsockopt_default_send_param(sk, len,
4492 case SCTP_PRIMARY_ADDR:
4493 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4496 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4499 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4501 case SCTP_ASSOCINFO:
4502 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4504 case SCTP_I_WANT_MAPPED_V4_ADDR:
4505 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4508 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4510 case SCTP_GET_PEER_ADDR_INFO:
4511 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4514 case SCTP_ADAPTION_LAYER:
4515 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4519 retval = -ENOPROTOOPT;
4523 sctp_release_sock(sk);
4527 static void sctp_hash(struct sock *sk)
4532 static void sctp_unhash(struct sock *sk)
4537 /* Check if port is acceptable. Possibly find first available port.
4539 * The port hash table (contained in the 'global' SCTP protocol storage
4540 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4541 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4542 * list (the list number is the port number hashed out, so as you
4543 * would expect from a hash function, all the ports in a given list have
4544 * such a number that hashes out to the same list number; you were
4545 * expecting that, right?); so each list has a set of ports, with a
4546 * link to the socket (struct sock) that uses it, the port number and
4547 * a fastreuse flag (FIXME: NPI ipg).
4549 static struct sctp_bind_bucket *sctp_bucket_create(
4550 struct sctp_bind_hashbucket *head, unsigned short snum);
4552 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4554 struct sctp_bind_hashbucket *head; /* hash list */
4555 struct sctp_bind_bucket *pp; /* hash list port iterator */
4556 unsigned short snum;
4559 /* NOTE: Remember to put this back to net order. */
4560 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4561 snum = addr->v4.sin_port;
4563 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4564 sctp_local_bh_disable();
4567 /* Search for an available port.
4569 * 'sctp_port_rover' was the last port assigned, so
4570 * we start to search from 'sctp_port_rover +
4571 * 1'. What we do is first check if port 'rover' is
4572 * already in the hash table; if not, we use that; if
4573 * it is, we try next.
4575 int low = sysctl_local_port_range[0];
4576 int high = sysctl_local_port_range[1];
4577 int remaining = (high - low) + 1;
4581 sctp_spin_lock(&sctp_port_alloc_lock);
4582 rover = sctp_port_rover;
4585 if ((rover < low) || (rover > high))
4587 index = sctp_phashfn(rover);
4588 head = &sctp_port_hashtable[index];
4589 sctp_spin_lock(&head->lock);
4590 for (pp = head->chain; pp; pp = pp->next)
4591 if (pp->port == rover)
4595 sctp_spin_unlock(&head->lock);
4596 } while (--remaining > 0);
4597 sctp_port_rover = rover;
4598 sctp_spin_unlock(&sctp_port_alloc_lock);
4600 /* Exhausted local port range during search? */
4605 /* OK, here is the one we will use. HEAD (the port
4606 * hash table list entry) is non-NULL and we hold it's
4611 /* We are given an specific port number; we verify
4612 * that it is not being used. If it is used, we will
4613 * exahust the search in the hash list corresponding
4614 * to the port number (snum) - we detect that with the
4615 * port iterator, pp being NULL.
4617 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4618 sctp_spin_lock(&head->lock);
4619 for (pp = head->chain; pp; pp = pp->next) {
4620 if (pp->port == snum)
4627 if (!hlist_empty(&pp->owner)) {
4628 /* We had a port hash table hit - there is an
4629 * available port (pp != NULL) and it is being
4630 * used by other socket (pp->owner not empty); that other
4631 * socket is going to be sk2.
4633 int reuse = sk->sk_reuse;
4635 struct hlist_node *node;
4637 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4638 if (pp->fastreuse && sk->sk_reuse)
4641 /* Run through the list of sockets bound to the port
4642 * (pp->port) [via the pointers bind_next and
4643 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4644 * we get the endpoint they describe and run through
4645 * the endpoint's list of IP (v4 or v6) addresses,
4646 * comparing each of the addresses with the address of
4647 * the socket sk. If we find a match, then that means
4648 * that this port/socket (sk) combination are already
4651 sk_for_each_bound(sk2, node, &pp->owner) {
4652 struct sctp_endpoint *ep2;
4653 ep2 = sctp_sk(sk2)->ep;
4655 if (reuse && sk2->sk_reuse)
4658 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4664 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4667 /* If there was a hash table miss, create a new port. */
4669 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4672 /* In either case (hit or miss), make sure fastreuse is 1 only
4673 * if sk->sk_reuse is too (that is, if the caller requested
4674 * SO_REUSEADDR on this socket -sk-).
4676 if (hlist_empty(&pp->owner))
4677 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4678 else if (pp->fastreuse && !sk->sk_reuse)
4681 /* We are set, so fill up all the data in the hash table
4682 * entry, tie the socket list information with the rest of the
4683 * sockets FIXME: Blurry, NPI (ipg).
4686 inet_sk(sk)->num = snum;
4687 if (!sctp_sk(sk)->bind_hash) {
4688 sk_add_bind_node(sk, &pp->owner);
4689 sctp_sk(sk)->bind_hash = pp;
4694 sctp_spin_unlock(&head->lock);
4697 sctp_local_bh_enable();
4698 addr->v4.sin_port = htons(addr->v4.sin_port);
4702 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4703 * port is requested.
4705 static int sctp_get_port(struct sock *sk, unsigned short snum)
4708 union sctp_addr addr;
4709 struct sctp_af *af = sctp_sk(sk)->pf->af;
4711 /* Set up a dummy address struct from the sk. */
4712 af->from_sk(&addr, sk);
4713 addr.v4.sin_port = htons(snum);
4715 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4716 ret = sctp_get_port_local(sk, &addr);
4718 return (ret ? 1 : 0);
4722 * 3.1.3 listen() - UDP Style Syntax
4724 * By default, new associations are not accepted for UDP style sockets.
4725 * An application uses listen() to mark a socket as being able to
4726 * accept new associations.
4728 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4730 struct sctp_sock *sp = sctp_sk(sk);
4731 struct sctp_endpoint *ep = sp->ep;
4733 /* Only UDP style sockets that are not peeled off are allowed to
4736 if (!sctp_style(sk, UDP))
4739 /* If backlog is zero, disable listening. */
4741 if (sctp_sstate(sk, CLOSED))
4744 sctp_unhash_endpoint(ep);
4745 sk->sk_state = SCTP_SS_CLOSED;
4748 /* Return if we are already listening. */
4749 if (sctp_sstate(sk, LISTENING))
4753 * If a bind() or sctp_bindx() is not called prior to a listen()
4754 * call that allows new associations to be accepted, the system
4755 * picks an ephemeral port and will choose an address set equivalent
4756 * to binding with a wildcard address.
4758 * This is not currently spelled out in the SCTP sockets
4759 * extensions draft, but follows the practice as seen in TCP
4762 if (!ep->base.bind_addr.port) {
4763 if (sctp_autobind(sk))
4766 sk->sk_state = SCTP_SS_LISTENING;
4767 sctp_hash_endpoint(ep);
4772 * 4.1.3 listen() - TCP Style Syntax
4774 * Applications uses listen() to ready the SCTP endpoint for accepting
4775 * inbound associations.
4777 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4779 struct sctp_sock *sp = sctp_sk(sk);
4780 struct sctp_endpoint *ep = sp->ep;
4782 /* If backlog is zero, disable listening. */
4784 if (sctp_sstate(sk, CLOSED))
4787 sctp_unhash_endpoint(ep);
4788 sk->sk_state = SCTP_SS_CLOSED;
4791 if (sctp_sstate(sk, LISTENING))
4795 * If a bind() or sctp_bindx() is not called prior to a listen()
4796 * call that allows new associations to be accepted, the system
4797 * picks an ephemeral port and will choose an address set equivalent
4798 * to binding with a wildcard address.
4800 * This is not currently spelled out in the SCTP sockets
4801 * extensions draft, but follows the practice as seen in TCP
4804 if (!ep->base.bind_addr.port) {
4805 if (sctp_autobind(sk))
4808 sk->sk_state = SCTP_SS_LISTENING;
4809 sk->sk_max_ack_backlog = backlog;
4810 sctp_hash_endpoint(ep);
4815 * Move a socket to LISTENING state.
4817 int sctp_inet_listen(struct socket *sock, int backlog)
4819 struct sock *sk = sock->sk;
4820 struct crypto_tfm *tfm=NULL;
4823 if (unlikely(backlog < 0))
4828 if (sock->state != SS_UNCONNECTED)
4831 /* Allocate HMAC for generating cookie. */
4832 if (sctp_hmac_alg) {
4833 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4840 switch (sock->type) {
4841 case SOCK_SEQPACKET:
4842 err = sctp_seqpacket_listen(sk, backlog);
4845 err = sctp_stream_listen(sk, backlog);
4853 /* Store away the transform reference. */
4854 sctp_sk(sk)->hmac = tfm;
4856 sctp_release_sock(sk);
4859 sctp_crypto_free_tfm(tfm);
4864 * This function is done by modeling the current datagram_poll() and the
4865 * tcp_poll(). Note that, based on these implementations, we don't
4866 * lock the socket in this function, even though it seems that,
4867 * ideally, locking or some other mechanisms can be used to ensure
4868 * the integrity of the counters (sndbuf and wmem_alloc) used
4869 * in this place. We assume that we don't need locks either until proven
4872 * Another thing to note is that we include the Async I/O support
4873 * here, again, by modeling the current TCP/UDP code. We don't have
4874 * a good way to test with it yet.
4876 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4878 struct sock *sk = sock->sk;
4879 struct sctp_sock *sp = sctp_sk(sk);
4882 poll_wait(file, sk->sk_sleep, wait);
4884 /* A TCP-style listening socket becomes readable when the accept queue
4887 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4888 return (!list_empty(&sp->ep->asocs)) ?
4889 (POLLIN | POLLRDNORM) : 0;
4893 /* Is there any exceptional events? */
4894 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4896 if (sk->sk_shutdown == SHUTDOWN_MASK)
4899 /* Is it readable? Reconsider this code with TCP-style support. */
4900 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4901 (sk->sk_shutdown & RCV_SHUTDOWN))
4902 mask |= POLLIN | POLLRDNORM;
4904 /* The association is either gone or not ready. */
4905 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4908 /* Is it writable? */
4909 if (sctp_writeable(sk)) {
4910 mask |= POLLOUT | POLLWRNORM;
4912 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4914 * Since the socket is not locked, the buffer
4915 * might be made available after the writeable check and
4916 * before the bit is set. This could cause a lost I/O
4917 * signal. tcp_poll() has a race breaker for this race
4918 * condition. Based on their implementation, we put
4919 * in the following code to cover it as well.
4921 if (sctp_writeable(sk))
4922 mask |= POLLOUT | POLLWRNORM;
4927 /********************************************************************
4928 * 2nd Level Abstractions
4929 ********************************************************************/
4931 static struct sctp_bind_bucket *sctp_bucket_create(
4932 struct sctp_bind_hashbucket *head, unsigned short snum)
4934 struct sctp_bind_bucket *pp;
4936 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4937 SCTP_DBG_OBJCNT_INC(bind_bucket);
4941 INIT_HLIST_HEAD(&pp->owner);
4942 if ((pp->next = head->chain) != NULL)
4943 pp->next->pprev = &pp->next;
4945 pp->pprev = &head->chain;
4950 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4951 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4953 if (hlist_empty(&pp->owner)) {
4955 pp->next->pprev = pp->pprev;
4956 *(pp->pprev) = pp->next;
4957 kmem_cache_free(sctp_bucket_cachep, pp);
4958 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4962 /* Release this socket's reference to a local port. */
4963 static inline void __sctp_put_port(struct sock *sk)
4965 struct sctp_bind_hashbucket *head =
4966 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4967 struct sctp_bind_bucket *pp;
4969 sctp_spin_lock(&head->lock);
4970 pp = sctp_sk(sk)->bind_hash;
4971 __sk_del_bind_node(sk);
4972 sctp_sk(sk)->bind_hash = NULL;
4973 inet_sk(sk)->num = 0;
4974 sctp_bucket_destroy(pp);
4975 sctp_spin_unlock(&head->lock);
4978 void sctp_put_port(struct sock *sk)
4980 sctp_local_bh_disable();
4981 __sctp_put_port(sk);
4982 sctp_local_bh_enable();
4986 * The system picks an ephemeral port and choose an address set equivalent
4987 * to binding with a wildcard address.
4988 * One of those addresses will be the primary address for the association.
4989 * This automatically enables the multihoming capability of SCTP.
4991 static int sctp_autobind(struct sock *sk)
4993 union sctp_addr autoaddr;
4995 unsigned short port;
4997 /* Initialize a local sockaddr structure to INADDR_ANY. */
4998 af = sctp_sk(sk)->pf->af;
5000 port = htons(inet_sk(sk)->num);
5001 af->inaddr_any(&autoaddr, port);
5003 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5006 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5009 * 4.2 The cmsghdr Structure *
5011 * When ancillary data is sent or received, any number of ancillary data
5012 * objects can be specified by the msg_control and msg_controllen members of
5013 * the msghdr structure, because each object is preceded by
5014 * a cmsghdr structure defining the object's length (the cmsg_len member).
5015 * Historically Berkeley-derived implementations have passed only one object
5016 * at a time, but this API allows multiple objects to be
5017 * passed in a single call to sendmsg() or recvmsg(). The following example
5018 * shows two ancillary data objects in a control buffer.
5020 * |<--------------------------- msg_controllen -------------------------->|
5023 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5025 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5028 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5030 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5033 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5034 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5036 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5038 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5045 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5046 sctp_cmsgs_t *cmsgs)
5048 struct cmsghdr *cmsg;
5050 for (cmsg = CMSG_FIRSTHDR(msg);
5052 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5053 if (!CMSG_OK(msg, cmsg))
5056 /* Should we parse this header or ignore? */
5057 if (cmsg->cmsg_level != IPPROTO_SCTP)
5060 /* Strictly check lengths following example in SCM code. */
5061 switch (cmsg->cmsg_type) {
5063 /* SCTP Socket API Extension
5064 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5066 * This cmsghdr structure provides information for
5067 * initializing new SCTP associations with sendmsg().
5068 * The SCTP_INITMSG socket option uses this same data
5069 * structure. This structure is not used for
5072 * cmsg_level cmsg_type cmsg_data[]
5073 * ------------ ------------ ----------------------
5074 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5076 if (cmsg->cmsg_len !=
5077 CMSG_LEN(sizeof(struct sctp_initmsg)))
5079 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5083 /* SCTP Socket API Extension
5084 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5086 * This cmsghdr structure specifies SCTP options for
5087 * sendmsg() and describes SCTP header information
5088 * about a received message through recvmsg().
5090 * cmsg_level cmsg_type cmsg_data[]
5091 * ------------ ------------ ----------------------
5092 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5094 if (cmsg->cmsg_len !=
5095 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5099 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5101 /* Minimally, validate the sinfo_flags. */
5102 if (cmsgs->info->sinfo_flags &
5103 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5104 SCTP_ABORT | SCTP_EOF))
5116 * Wait for a packet..
5117 * Note: This function is the same function as in core/datagram.c
5118 * with a few modifications to make lksctp work.
5120 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5125 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5127 /* Socket errors? */
5128 error = sock_error(sk);
5132 if (!skb_queue_empty(&sk->sk_receive_queue))
5135 /* Socket shut down? */
5136 if (sk->sk_shutdown & RCV_SHUTDOWN)
5139 /* Sequenced packets can come disconnected. If so we report the
5144 /* Is there a good reason to think that we may receive some data? */
5145 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5148 /* Handle signals. */
5149 if (signal_pending(current))
5152 /* Let another process have a go. Since we are going to sleep
5153 * anyway. Note: This may cause odd behaviors if the message
5154 * does not fit in the user's buffer, but this seems to be the
5155 * only way to honor MSG_DONTWAIT realistically.
5157 sctp_release_sock(sk);
5158 *timeo_p = schedule_timeout(*timeo_p);
5162 finish_wait(sk->sk_sleep, &wait);
5166 error = sock_intr_errno(*timeo_p);
5169 finish_wait(sk->sk_sleep, &wait);
5174 /* Receive a datagram.
5175 * Note: This is pretty much the same routine as in core/datagram.c
5176 * with a few changes to make lksctp work.
5178 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5179 int noblock, int *err)
5182 struct sk_buff *skb;
5185 timeo = sock_rcvtimeo(sk, noblock);
5187 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5188 timeo, MAX_SCHEDULE_TIMEOUT);
5191 /* Again only user level code calls this function,
5192 * so nothing interrupt level
5193 * will suddenly eat the receive_queue.
5195 * Look at current nfs client by the way...
5196 * However, this function was corrent in any case. 8)
5198 if (flags & MSG_PEEK) {
5199 spin_lock_bh(&sk->sk_receive_queue.lock);
5200 skb = skb_peek(&sk->sk_receive_queue);
5202 atomic_inc(&skb->users);
5203 spin_unlock_bh(&sk->sk_receive_queue.lock);
5205 skb = skb_dequeue(&sk->sk_receive_queue);
5211 /* Caller is allowed not to check sk->sk_err before calling. */
5212 error = sock_error(sk);
5216 if (sk->sk_shutdown & RCV_SHUTDOWN)
5219 /* User doesn't want to wait. */
5223 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5232 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5233 static void __sctp_write_space(struct sctp_association *asoc)
5235 struct sock *sk = asoc->base.sk;
5236 struct socket *sock = sk->sk_socket;
5238 if ((sctp_wspace(asoc) > 0) && sock) {
5239 if (waitqueue_active(&asoc->wait))
5240 wake_up_interruptible(&asoc->wait);
5242 if (sctp_writeable(sk)) {
5243 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5244 wake_up_interruptible(sk->sk_sleep);
5246 /* Note that we try to include the Async I/O support
5247 * here by modeling from the current TCP/UDP code.
5248 * We have not tested with it yet.
5250 if (sock->fasync_list &&
5251 !(sk->sk_shutdown & SEND_SHUTDOWN))
5252 sock_wake_async(sock, 2, POLL_OUT);
5257 /* Do accounting for the sndbuf space.
5258 * Decrement the used sndbuf space of the corresponding association by the
5259 * data size which was just transmitted(freed).
5261 static void sctp_wfree(struct sk_buff *skb)
5263 struct sctp_association *asoc;
5264 struct sctp_chunk *chunk;
5267 /* Get the saved chunk pointer. */
5268 chunk = *((struct sctp_chunk **)(skb->cb));
5271 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5272 sizeof(struct sk_buff) +
5273 sizeof(struct sctp_chunk);
5275 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5278 __sctp_write_space(asoc);
5280 sctp_association_put(asoc);
5283 /* Helper function to wait for space in the sndbuf. */
5284 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5287 struct sock *sk = asoc->base.sk;
5289 long current_timeo = *timeo_p;
5292 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5293 asoc, (long)(*timeo_p), msg_len);
5295 /* Increment the association's refcnt. */
5296 sctp_association_hold(asoc);
5298 /* Wait on the association specific sndbuf space. */
5300 prepare_to_wait_exclusive(&asoc->wait, &wait,
5301 TASK_INTERRUPTIBLE);
5304 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5307 if (signal_pending(current))
5308 goto do_interrupted;
5309 if (msg_len <= sctp_wspace(asoc))
5312 /* Let another process have a go. Since we are going
5315 sctp_release_sock(sk);
5316 current_timeo = schedule_timeout(current_timeo);
5319 *timeo_p = current_timeo;
5323 finish_wait(&asoc->wait, &wait);
5325 /* Release the association's refcnt. */
5326 sctp_association_put(asoc);
5335 err = sock_intr_errno(*timeo_p);
5343 /* If socket sndbuf has changed, wake up all per association waiters. */
5344 void sctp_write_space(struct sock *sk)
5346 struct sctp_association *asoc;
5347 struct list_head *pos;
5349 /* Wake up the tasks in each wait queue. */
5350 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5351 asoc = list_entry(pos, struct sctp_association, asocs);
5352 __sctp_write_space(asoc);
5356 /* Is there any sndbuf space available on the socket?
5358 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5359 * associations on the same socket. For a UDP-style socket with
5360 * multiple associations, it is possible for it to be "unwriteable"
5361 * prematurely. I assume that this is acceptable because
5362 * a premature "unwriteable" is better than an accidental "writeable" which
5363 * would cause an unwanted block under certain circumstances. For the 1-1
5364 * UDP-style sockets or TCP-style sockets, this code should work.
5367 static int sctp_writeable(struct sock *sk)
5371 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5377 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5378 * returns immediately with EINPROGRESS.
5380 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5382 struct sock *sk = asoc->base.sk;
5384 long current_timeo = *timeo_p;
5387 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5390 /* Increment the association's refcnt. */
5391 sctp_association_hold(asoc);
5394 prepare_to_wait_exclusive(&asoc->wait, &wait,
5395 TASK_INTERRUPTIBLE);
5398 if (sk->sk_shutdown & RCV_SHUTDOWN)
5400 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5403 if (signal_pending(current))
5404 goto do_interrupted;
5406 if (sctp_state(asoc, ESTABLISHED))
5409 /* Let another process have a go. Since we are going
5412 sctp_release_sock(sk);
5413 current_timeo = schedule_timeout(current_timeo);
5416 *timeo_p = current_timeo;
5420 finish_wait(&asoc->wait, &wait);
5422 /* Release the association's refcnt. */
5423 sctp_association_put(asoc);
5428 if (asoc->init_err_counter + 1 >= asoc->max_init_attempts)
5431 err = -ECONNREFUSED;
5435 err = sock_intr_errno(*timeo_p);
5443 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5445 struct sctp_endpoint *ep;
5449 ep = sctp_sk(sk)->ep;
5453 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5454 TASK_INTERRUPTIBLE);
5456 if (list_empty(&ep->asocs)) {
5457 sctp_release_sock(sk);
5458 timeo = schedule_timeout(timeo);
5463 if (!sctp_sstate(sk, LISTENING))
5467 if (!list_empty(&ep->asocs))
5470 err = sock_intr_errno(timeo);
5471 if (signal_pending(current))
5479 finish_wait(sk->sk_sleep, &wait);
5484 void sctp_wait_for_close(struct sock *sk, long timeout)
5489 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5490 if (list_empty(&sctp_sk(sk)->ep->asocs))
5492 sctp_release_sock(sk);
5493 timeout = schedule_timeout(timeout);
5495 } while (!signal_pending(current) && timeout);
5497 finish_wait(sk->sk_sleep, &wait);
5500 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5501 * and its messages to the newsk.
5503 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5504 struct sctp_association *assoc,
5505 sctp_socket_type_t type)
5507 struct sctp_sock *oldsp = sctp_sk(oldsk);
5508 struct sctp_sock *newsp = sctp_sk(newsk);
5509 struct sctp_bind_bucket *pp; /* hash list port iterator */
5510 struct sctp_endpoint *newep = newsp->ep;
5511 struct sk_buff *skb, *tmp;
5512 struct sctp_ulpevent *event;
5515 /* Migrate socket buffer sizes and all the socket level options to the
5518 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5519 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5520 /* Brute force copy old sctp opt. */
5521 inet_sk_copy_descendant(newsk, oldsk);
5523 /* Restore the ep value that was overwritten with the above structure
5529 /* Hook this new socket in to the bind_hash list. */
5530 pp = sctp_sk(oldsk)->bind_hash;
5531 sk_add_bind_node(newsk, &pp->owner);
5532 sctp_sk(newsk)->bind_hash = pp;
5533 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5535 /* Copy the bind_addr list from the original endpoint to the new
5536 * endpoint so that we can handle restarts properly
5538 if (assoc->peer.ipv4_address)
5539 flags |= SCTP_ADDR4_PEERSUPP;
5540 if (assoc->peer.ipv6_address)
5541 flags |= SCTP_ADDR6_PEERSUPP;
5542 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5543 &oldsp->ep->base.bind_addr,
5544 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5546 /* Move any messages in the old socket's receive queue that are for the
5547 * peeled off association to the new socket's receive queue.
5549 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5550 event = sctp_skb2event(skb);
5551 if (event->asoc == assoc) {
5553 __skb_unlink(skb, &oldsk->sk_receive_queue);
5554 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5555 skb_set_owner_r(skb, newsk);
5559 /* Clean up any messages pending delivery due to partial
5560 * delivery. Three cases:
5561 * 1) No partial deliver; no work.
5562 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5563 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5565 skb_queue_head_init(&newsp->pd_lobby);
5566 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5568 if (sctp_sk(oldsk)->pd_mode) {
5569 struct sk_buff_head *queue;
5571 /* Decide which queue to move pd_lobby skbs to. */
5572 if (assoc->ulpq.pd_mode) {
5573 queue = &newsp->pd_lobby;
5575 queue = &newsk->sk_receive_queue;
5577 /* Walk through the pd_lobby, looking for skbs that
5578 * need moved to the new socket.
5580 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5581 event = sctp_skb2event(skb);
5582 if (event->asoc == assoc) {
5584 __skb_unlink(skb, &oldsp->pd_lobby);
5585 __skb_queue_tail(queue, skb);
5586 skb_set_owner_r(skb, newsk);
5590 /* Clear up any skbs waiting for the partial
5591 * delivery to finish.
5593 if (assoc->ulpq.pd_mode)
5594 sctp_clear_pd(oldsk);
5598 /* Set the type of socket to indicate that it is peeled off from the
5599 * original UDP-style socket or created with the accept() call on a
5600 * TCP-style socket..
5604 /* Migrate the association to the new socket. */
5605 sctp_assoc_migrate(assoc, newsk);
5607 /* If the association on the newsk is already closed before accept()
5608 * is called, set RCV_SHUTDOWN flag.
5610 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5611 newsk->sk_shutdown |= RCV_SHUTDOWN;
5613 newsk->sk_state = SCTP_SS_ESTABLISHED;
5616 /* This proto struct describes the ULP interface for SCTP. */
5617 struct proto sctp_prot = {
5619 .owner = THIS_MODULE,
5620 .close = sctp_close,
5621 .connect = sctp_connect,
5622 .disconnect = sctp_disconnect,
5623 .accept = sctp_accept,
5624 .ioctl = sctp_ioctl,
5625 .init = sctp_init_sock,
5626 .destroy = sctp_destroy_sock,
5627 .shutdown = sctp_shutdown,
5628 .setsockopt = sctp_setsockopt,
5629 .getsockopt = sctp_getsockopt,
5630 .sendmsg = sctp_sendmsg,
5631 .recvmsg = sctp_recvmsg,
5633 .backlog_rcv = sctp_backlog_rcv,
5635 .unhash = sctp_unhash,
5636 .get_port = sctp_get_port,
5637 .obj_size = sizeof(struct sctp_sock),
5640 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5641 struct proto sctpv6_prot = {
5643 .owner = THIS_MODULE,
5644 .close = sctp_close,
5645 .connect = sctp_connect,
5646 .disconnect = sctp_disconnect,
5647 .accept = sctp_accept,
5648 .ioctl = sctp_ioctl,
5649 .init = sctp_init_sock,
5650 .destroy = sctp_destroy_sock,
5651 .shutdown = sctp_shutdown,
5652 .setsockopt = sctp_setsockopt,
5653 .getsockopt = sctp_getsockopt,
5654 .sendmsg = sctp_sendmsg,
5655 .recvmsg = sctp_recvmsg,
5657 .backlog_rcv = sctp_backlog_rcv,
5659 .unhash = sctp_unhash,
5660 .get_port = sctp_get_port,
5661 .obj_size = sizeof(struct sctp6_sock),
5663 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */