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
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #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_opt *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 amt = sk->sk_sndbuf - asoc->sndbuf_used;
124 /* Increment the used sndbuf space count of the corresponding association by
125 * the size of the outgoing data chunk.
126 * Also, set the skb destructor for sndbuf accounting later.
128 * Since it is always 1-1 between chunk and skb, and also a new skb is always
129 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
130 * destructor in the data chunk skb for the purpose of the sndbuf space
133 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
135 struct sctp_association *asoc = chunk->asoc;
136 struct sock *sk = asoc->base.sk;
138 /* The sndbuf space is tracked per association. */
139 sctp_association_hold(asoc);
141 chunk->skb->destructor = sctp_wfree;
142 /* Save the chunk pointer in skb for sctp_wfree to use later. */
143 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
145 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk);
146 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk);
149 /* Verify that this is a valid address. */
150 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
155 /* Verify basic sockaddr. */
156 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
160 /* Is this a valid SCTP address? */
161 if (!af->addr_valid(addr, sctp_sk(sk)))
164 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
170 /* Look up the association by its id. If this is not a UDP-style
171 * socket, the ID field is always ignored.
173 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
175 struct sctp_association *asoc = NULL;
177 /* If this is not a UDP-style socket, assoc id should be ignored. */
178 if (!sctp_style(sk, UDP)) {
179 /* Return NULL if the socket state is not ESTABLISHED. It
180 * could be a TCP-style listening socket or a socket which
181 * hasn't yet called connect() to establish an association.
183 if (!sctp_sstate(sk, ESTABLISHED))
186 /* Get the first and the only association from the list. */
187 if (!list_empty(&sctp_sk(sk)->ep->asocs))
188 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
189 struct sctp_association, asocs);
193 /* Otherwise this is a UDP-style socket. */
194 if (!id || (id == (sctp_assoc_t)-1))
197 spin_lock_bh(&sctp_assocs_id_lock);
198 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
199 spin_unlock_bh(&sctp_assocs_id_lock);
201 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
207 /* Look up the transport from an address and an assoc id. If both address and
208 * id are specified, the associations matching the address and the id should be
211 struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
212 struct sockaddr_storage *addr,
215 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
216 struct sctp_transport *transport;
217 union sctp_addr *laddr = (union sctp_addr *)addr;
219 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
220 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
221 (union sctp_addr *)addr,
223 laddr->v4.sin_port = htons(laddr->v4.sin_port);
228 id_asoc = sctp_id2assoc(sk, id);
229 if (id_asoc && (id_asoc != addr_asoc))
232 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
233 (union sctp_addr *)addr);
238 /* API 3.1.2 bind() - UDP Style Syntax
239 * The syntax of bind() is,
241 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
243 * sd - the socket descriptor returned by socket().
244 * addr - the address structure (struct sockaddr_in or struct
245 * sockaddr_in6 [RFC 2553]),
246 * addr_len - the size of the address structure.
248 int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
254 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
255 sk, uaddr, addr_len);
257 /* Disallow binding twice. */
258 if (!sctp_sk(sk)->ep->base.bind_addr.port)
259 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
264 sctp_release_sock(sk);
269 static long sctp_get_port_local(struct sock *, union sctp_addr *);
271 /* Verify this is a valid sockaddr. */
272 static struct sctp_af *sctp_sockaddr_af(struct sctp_opt *opt,
273 union sctp_addr *addr, int len)
277 /* Check minimum size. */
278 if (len < sizeof (struct sockaddr))
281 /* Does this PF support this AF? */
282 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
285 /* If we get this far, af is valid. */
286 af = sctp_get_af_specific(addr->sa.sa_family);
288 if (len < af->sockaddr_len)
294 /* Bind a local address either to an endpoint or to an association. */
295 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
297 struct sctp_opt *sp = sctp_sk(sk);
298 struct sctp_endpoint *ep = sp->ep;
299 struct sctp_bind_addr *bp = &ep->base.bind_addr;
304 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
307 /* Common sockaddr verification. */
308 af = sctp_sockaddr_af(sp, addr, len);
312 /* PF specific bind() address verification. */
313 if (!sp->pf->bind_verify(sp, addr))
314 return -EADDRNOTAVAIL;
316 snum= ntohs(addr->v4.sin_port);
318 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
321 /* We must either be unbound, or bind to the same port. */
322 if (bp->port && (snum != bp->port)) {
323 SCTP_DEBUG_PRINTK("sctp_do_bind:"
324 " New port %d does not match existing port "
325 "%d.\n", snum, bp->port);
329 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
332 /* Make sure we are allowed to bind here.
333 * The function sctp_get_port_local() does duplicate address
336 if ((ret = sctp_get_port_local(sk, addr))) {
337 if (ret == (long) sk) {
338 /* This endpoint has a conflicting address. */
345 /* Refresh ephemeral port. */
347 snum = inet_sk(sk)->num;
349 /* Add the address to the bind address list. */
350 sctp_local_bh_disable();
351 sctp_write_lock(&ep->base.addr_lock);
353 /* Use GFP_ATOMIC since BHs are disabled. */
354 addr->v4.sin_port = ntohs(addr->v4.sin_port);
355 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
356 addr->v4.sin_port = htons(addr->v4.sin_port);
357 if (!ret && !bp->port)
359 sctp_write_unlock(&ep->base.addr_lock);
360 sctp_local_bh_enable();
362 /* Copy back into socket for getsockname() use. */
364 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
365 af->to_sk_saddr(addr, sk);
371 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
373 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
374 * at any one time. If a sender, after sending an ASCONF chunk, decides
375 * it needs to transfer another ASCONF Chunk, it MUST wait until the
376 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
377 * subsequent ASCONF. Note this restriction binds each side, so at any
378 * time two ASCONF may be in-transit on any given association (one sent
379 * from each endpoint).
381 static int sctp_send_asconf(struct sctp_association *asoc,
382 struct sctp_chunk *chunk)
386 /* If there is an outstanding ASCONF chunk, queue it for later
389 if (asoc->addip_last_asconf) {
390 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
394 /* Hold the chunk until an ASCONF_ACK is received. */
395 sctp_chunk_hold(chunk);
396 retval = sctp_primitive_ASCONF(asoc, chunk);
398 sctp_chunk_free(chunk);
400 asoc->addip_last_asconf = chunk;
406 /* Add a list of addresses as bind addresses to local endpoint or
409 * Basically run through each address specified in the addrs/addrcnt
410 * array/length pair, determine if it is IPv6 or IPv4 and call
411 * sctp_do_bind() on it.
413 * If any of them fails, then the operation will be reversed and the
414 * ones that were added will be removed.
416 * Only sctp_setsockopt_bindx() is supposed to call this function.
418 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
423 struct sockaddr *sa_addr;
426 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
430 for (cnt = 0; cnt < addrcnt; cnt++) {
431 /* The list may contain either IPv4 or IPv6 address;
432 * determine the address length for walking thru the list.
434 sa_addr = (struct sockaddr *)addr_buf;
435 af = sctp_get_af_specific(sa_addr->sa_family);
441 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
444 addr_buf += af->sockaddr_len;
448 /* Failed. Cleanup the ones that have been added */
450 sctp_bindx_rem(sk, addrs, cnt);
458 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
459 * associations that are part of the endpoint indicating that a list of local
460 * addresses are added to the endpoint.
462 * If any of the addresses is already in the bind address list of the
463 * association, we do not send the chunk for that association. But it will not
464 * affect other associations.
466 * Only sctp_setsockopt_bindx() is supposed to call this function.
468 static int sctp_send_asconf_add_ip(struct sock *sk,
469 struct sockaddr *addrs,
473 struct sctp_endpoint *ep;
474 struct sctp_association *asoc;
475 struct sctp_bind_addr *bp;
476 struct sctp_chunk *chunk;
477 struct sctp_sockaddr_entry *laddr;
478 union sctp_addr *addr;
481 struct list_head *pos;
486 if (!sctp_addip_enable)
492 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
493 __FUNCTION__, sk, addrs, addrcnt);
495 list_for_each(pos, &ep->asocs) {
496 asoc = list_entry(pos, struct sctp_association, asocs);
498 if (!asoc->peer.asconf_capable)
501 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
504 if (!sctp_state(asoc, ESTABLISHED))
507 /* Check if any address in the packed array of addresses is
508 * in the bind address list of the association. If so,
509 * do not send the asconf chunk to its peer, but continue with
510 * other associations.
513 for (i = 0; i < addrcnt; i++) {
514 addr = (union sctp_addr *)addr_buf;
515 af = sctp_get_af_specific(addr->v4.sin_family);
521 if (sctp_assoc_lookup_laddr(asoc, addr))
524 addr_buf += af->sockaddr_len;
529 /* Use the first address in bind addr list of association as
530 * Address Parameter of ASCONF CHUNK.
532 sctp_read_lock(&asoc->base.addr_lock);
533 bp = &asoc->base.bind_addr;
534 p = bp->address_list.next;
535 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
536 sctp_read_unlock(&asoc->base.addr_lock);
538 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
539 addrcnt, SCTP_PARAM_ADD_IP);
545 retval = sctp_send_asconf(asoc, chunk);
547 /* FIXME: After sending the add address ASCONF chunk, we
548 * cannot append the address to the association's binding
549 * address list, because the new address may be used as the
550 * source of a message sent to the peer before the ASCONF
551 * chunk is received by the peer. So we should wait until
552 * ASCONF_ACK is received.
560 /* Remove a list of addresses from bind addresses list. Do not remove the
563 * Basically run through each address specified in the addrs/addrcnt
564 * array/length pair, determine if it is IPv6 or IPv4 and call
565 * sctp_del_bind() on it.
567 * If any of them fails, then the operation will be reversed and the
568 * ones that were removed will be added back.
570 * At least one address has to be left; if only one address is
571 * available, the operation will return -EBUSY.
573 * Only sctp_setsockopt_bindx() is supposed to call this function.
575 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
577 struct sctp_opt *sp = sctp_sk(sk);
578 struct sctp_endpoint *ep = sp->ep;
580 struct sctp_bind_addr *bp = &ep->base.bind_addr;
582 union sctp_addr saveaddr;
584 struct sockaddr *sa_addr;
587 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
591 for (cnt = 0; cnt < addrcnt; cnt++) {
592 /* If the bind address list is empty or if there is only one
593 * bind address, there is nothing more to be removed (we need
594 * at least one address here).
596 if (list_empty(&bp->address_list) ||
597 (sctp_list_single_entry(&bp->address_list))) {
602 /* The list may contain either IPv4 or IPv6 address;
603 * determine the address length to copy the address to
606 sa_addr = (struct sockaddr *)addr_buf;
607 af = sctp_get_af_specific(sa_addr->sa_family);
612 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
613 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
614 if (saveaddr.v4.sin_port != bp->port) {
619 /* FIXME - There is probably a need to check if sk->sk_saddr and
620 * sk->sk_rcv_addr are currently set to one of the addresses to
621 * be removed. This is something which needs to be looked into
622 * when we are fixing the outstanding issues with multi-homing
623 * socket routing and failover schemes. Refer to comments in
624 * sctp_do_bind(). -daisy
626 sctp_local_bh_disable();
627 sctp_write_lock(&ep->base.addr_lock);
629 retval = sctp_del_bind_addr(bp, &saveaddr);
631 sctp_write_unlock(&ep->base.addr_lock);
632 sctp_local_bh_enable();
634 addr_buf += af->sockaddr_len;
637 /* Failed. Add the ones that has been removed back */
639 sctp_bindx_add(sk, addrs, cnt);
647 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
648 * the associations that are part of the endpoint indicating that a list of
649 * local addresses are removed from the endpoint.
651 * If any of the addresses is already in the bind address list of the
652 * association, we do not send the chunk for that association. But it will not
653 * affect other associations.
655 * Only sctp_setsockopt_bindx() is supposed to call this function.
657 static int sctp_send_asconf_del_ip(struct sock *sk,
658 struct sockaddr *addrs,
662 struct sctp_endpoint *ep;
663 struct sctp_association *asoc;
664 struct sctp_bind_addr *bp;
665 struct sctp_chunk *chunk;
666 union sctp_addr *laddr;
669 struct list_head *pos;
673 if (!sctp_addip_enable)
679 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
680 __FUNCTION__, sk, addrs, addrcnt);
682 list_for_each(pos, &ep->asocs) {
683 asoc = list_entry(pos, struct sctp_association, asocs);
685 if (!asoc->peer.asconf_capable)
688 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
691 if (!sctp_state(asoc, ESTABLISHED))
694 /* Check if any address in the packed array of addresses is
695 * not present in the bind address list of the association.
696 * If so, do not send the asconf chunk to its peer, but
697 * continue with other associations.
700 for (i = 0; i < addrcnt; i++) {
701 laddr = (union sctp_addr *)addr_buf;
702 af = sctp_get_af_specific(laddr->v4.sin_family);
708 if (!sctp_assoc_lookup_laddr(asoc, laddr))
711 addr_buf += af->sockaddr_len;
716 /* Find one address in the association's bind address list
717 * that is not in the packed array of addresses. This is to
718 * make sure that we do not delete all the addresses in the
721 sctp_read_lock(&asoc->base.addr_lock);
722 bp = &asoc->base.bind_addr;
723 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
725 sctp_read_unlock(&asoc->base.addr_lock);
729 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
736 retval = sctp_send_asconf(asoc, chunk);
738 /* FIXME: After sending the delete address ASCONF chunk, we
739 * cannot remove the addresses from the association's bind
740 * address list, because there maybe some packet send to
741 * the delete addresses, so we should wait until ASCONF_ACK
742 * packet is received.
749 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
752 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
755 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
756 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
759 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
760 * Section 3.1.2 for this usage.
762 * addrs is a pointer to an array of one or more socket addresses. Each
763 * address is contained in its appropriate structure (i.e. struct
764 * sockaddr_in or struct sockaddr_in6) the family of the address type
765 * must be used to distengish the address length (note that this
766 * representation is termed a "packed array" of addresses). The caller
767 * specifies the number of addresses in the array with addrcnt.
769 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
770 * -1, and sets errno to the appropriate error code.
772 * For SCTP, the port given in each socket address must be the same, or
773 * sctp_bindx() will fail, setting errno to EINVAL.
775 * The flags parameter is formed from the bitwise OR of zero or more of
776 * the following currently defined flags:
778 * SCTP_BINDX_ADD_ADDR
780 * SCTP_BINDX_REM_ADDR
782 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
783 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
784 * addresses from the association. The two flags are mutually exclusive;
785 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
786 * not remove all addresses from an association; sctp_bindx() will
787 * reject such an attempt with EINVAL.
789 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
790 * additional addresses with an endpoint after calling bind(). Or use
791 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
792 * socket is associated with so that no new association accepted will be
793 * associated with those addresses. If the endpoint supports dynamic
794 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
795 * endpoint to send the appropriate message to the peer to change the
796 * peers address lists.
798 * Adding and removing addresses from a connected association is
799 * optional functionality. Implementations that do not support this
800 * functionality should return EOPNOTSUPP.
802 * Basically do nothing but copying the addresses from user to kernel
803 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
804 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
806 * We don't use copy_from_user() for optimization: we first do the
807 * sanity checks (buffer size -fast- and access check-healthy
808 * pointer); if all of those succeed, then we can alloc the memory
809 * (expensive operation) needed to copy the data to kernel. Then we do
810 * the copying without checking the user space area
811 * (__copy_from_user()).
813 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
816 * sk The sk of the socket
817 * addrs The pointer to the addresses in user land
818 * addrssize Size of the addrs buffer
819 * op Operation to perform (add or remove, see the flags of
822 * Returns 0 if ok, <0 errno code on error.
824 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
825 struct sockaddr __user *addrs,
826 int addrs_size, int op)
828 struct sockaddr *kaddrs;
832 struct sockaddr *sa_addr;
836 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
837 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
839 if (unlikely(addrs_size <= 0))
842 /* Check the user passed a healthy pointer. */
843 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
846 /* Alloc space for the address array in kernel memory. */
847 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
848 if (unlikely(!kaddrs))
851 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
856 /* Walk through the addrs buffer and count the number of addresses. */
858 while (walk_size < addrs_size) {
859 sa_addr = (struct sockaddr *)addr_buf;
860 af = sctp_get_af_specific(sa_addr->sa_family);
862 /* If the address family is not supported or if this address
863 * causes the address buffer to overflow return EINVAL.
865 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
870 addr_buf += af->sockaddr_len;
871 walk_size += af->sockaddr_len;
876 case SCTP_BINDX_ADD_ADDR:
877 err = sctp_bindx_add(sk, kaddrs, addrcnt);
880 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
883 case SCTP_BINDX_REM_ADDR:
884 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
887 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
901 /* API 3.1.4 close() - UDP Style Syntax
902 * Applications use close() to perform graceful shutdown (as described in
903 * Section 10.1 of [SCTP]) on ALL the associations currently represented
904 * by a UDP-style socket.
908 * ret = close(int sd);
910 * sd - the socket descriptor of the associations to be closed.
912 * To gracefully shutdown a specific association represented by the
913 * UDP-style socket, an application should use the sendmsg() call,
914 * passing no user data, but including the appropriate flag in the
915 * ancillary data (see Section xxxx).
917 * If sd in the close() call is a branched-off socket representing only
918 * one association, the shutdown is performed on that association only.
920 * 4.1.6 close() - TCP Style Syntax
922 * Applications use close() to gracefully close down an association.
928 * sd - the socket descriptor of the association to be closed.
930 * After an application calls close() on a socket descriptor, no further
931 * socket operations will succeed on that descriptor.
933 * API 7.1.4 SO_LINGER
935 * An application using the TCP-style socket can use this option to
936 * perform the SCTP ABORT primitive. The linger option structure is:
939 * int l_onoff; // option on/off
940 * int l_linger; // linger time
943 * To enable the option, set l_onoff to 1. If the l_linger value is set
944 * to 0, calling close() is the same as the ABORT primitive. If the
945 * value is set to a negative value, the setsockopt() call will return
946 * an error. If the value is set to a positive value linger_time, the
947 * close() can be blocked for at most linger_time ms. If the graceful
948 * shutdown phase does not finish during this period, close() will
949 * return but the graceful shutdown phase continues in the system.
951 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
953 struct sctp_endpoint *ep;
954 struct sctp_association *asoc;
955 struct list_head *pos, *temp;
957 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
960 sk->sk_shutdown = SHUTDOWN_MASK;
962 ep = sctp_sk(sk)->ep;
964 /* Walk all associations on a socket, not on an endpoint. */
965 list_for_each_safe(pos, temp, &ep->asocs) {
966 asoc = list_entry(pos, struct sctp_association, asocs);
968 if (sctp_style(sk, TCP)) {
969 /* A closed association can still be in the list if
970 * it belongs to a TCP-style listening socket that is
971 * not yet accepted. If so, free it. If not, send an
972 * ABORT or SHUTDOWN based on the linger options.
974 if (sctp_state(asoc, CLOSED)) {
975 sctp_unhash_established(asoc);
976 sctp_association_free(asoc);
978 } else if (sock_flag(sk, SOCK_LINGER) &&
980 sctp_primitive_ABORT(asoc, NULL);
982 sctp_primitive_SHUTDOWN(asoc, NULL);
984 sctp_primitive_SHUTDOWN(asoc, NULL);
987 /* Clean up any skbs sitting on the receive queue. */
988 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
989 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
991 /* On a TCP-style socket, block for at most linger_time if set. */
992 if (sctp_style(sk, TCP) && timeout)
993 sctp_wait_for_close(sk, timeout);
995 /* This will run the backlog queue. */
996 sctp_release_sock(sk);
998 /* Supposedly, no process has access to the socket, but
999 * the net layers still may.
1001 sctp_local_bh_disable();
1002 sctp_bh_lock_sock(sk);
1004 /* Hold the sock, since sk_common_release() will put sock_put()
1005 * and we have just a little more cleanup.
1008 sk_common_release(sk);
1010 sctp_bh_unlock_sock(sk);
1011 sctp_local_bh_enable();
1015 SCTP_DBG_OBJCNT_DEC(sock);
1018 /* Handle EPIPE error. */
1019 static int sctp_error(struct sock *sk, int flags, int err)
1022 err = sock_error(sk) ? : -EPIPE;
1023 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1024 send_sig(SIGPIPE, current, 0);
1028 /* API 3.1.3 sendmsg() - UDP Style Syntax
1030 * An application uses sendmsg() and recvmsg() calls to transmit data to
1031 * and receive data from its peer.
1033 * ssize_t sendmsg(int socket, const struct msghdr *message,
1036 * socket - the socket descriptor of the endpoint.
1037 * message - pointer to the msghdr structure which contains a single
1038 * user message and possibly some ancillary data.
1040 * See Section 5 for complete description of the data
1043 * flags - flags sent or received with the user message, see Section
1044 * 5 for complete description of the flags.
1046 * Note: This function could use a rewrite especially when explicit
1047 * connect support comes in.
1049 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1051 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1053 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1054 struct msghdr *msg, size_t msg_len)
1056 struct sctp_opt *sp;
1057 struct sctp_endpoint *ep;
1058 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1059 struct sctp_transport *transport, *chunk_tp;
1060 struct sctp_chunk *chunk;
1062 struct sockaddr *msg_name = NULL;
1063 struct sctp_sndrcvinfo default_sinfo = { 0 };
1064 struct sctp_sndrcvinfo *sinfo;
1065 struct sctp_initmsg *sinit;
1066 sctp_assoc_t associd = 0;
1067 sctp_cmsgs_t cmsgs = { NULL };
1071 __u16 sinfo_flags = 0;
1072 struct sctp_datamsg *datamsg;
1073 struct list_head *pos;
1074 int msg_flags = msg->msg_flags;
1076 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1083 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1085 /* We cannot send a message over a TCP-style listening socket. */
1086 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1091 /* Parse out the SCTP CMSGs. */
1092 err = sctp_msghdr_parse(msg, &cmsgs);
1095 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1099 /* Fetch the destination address for this packet. This
1100 * address only selects the association--it is not necessarily
1101 * the address we will send to.
1102 * For a peeled-off socket, msg_name is ignored.
1104 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1105 int msg_namelen = msg->msg_namelen;
1107 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1112 if (msg_namelen > sizeof(to))
1113 msg_namelen = sizeof(to);
1114 memcpy(&to, msg->msg_name, msg_namelen);
1115 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1117 to.v4.sin_addr.s_addr, to.v4.sin_port);
1119 to.v4.sin_port = ntohs(to.v4.sin_port);
1120 msg_name = msg->msg_name;
1126 /* Did the user specify SNDRCVINFO? */
1128 sinfo_flags = sinfo->sinfo_flags;
1129 associd = sinfo->sinfo_assoc_id;
1132 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1133 msg_len, sinfo_flags);
1135 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1136 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1141 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1142 * length messages when MSG_EOF|MSG_ABORT is not set.
1143 * If MSG_ABORT is set, the message length could be non zero with
1144 * the msg_iov set to the user abort reason.
1146 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1147 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1152 /* If MSG_ADDR_OVER is set, there must be an address
1153 * specified in msg_name.
1155 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1162 SCTP_DEBUG_PRINTK("About to look up association.\n");
1166 /* If a msg_name has been specified, assume this is to be used. */
1168 /* Look for a matching association on the endpoint. */
1169 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1171 /* If we could not find a matching association on the
1172 * endpoint, make sure that it is not a TCP-style
1173 * socket that already has an association or there is
1174 * no peeled-off association on another socket.
1176 if ((sctp_style(sk, TCP) &&
1177 sctp_sstate(sk, ESTABLISHED)) ||
1178 sctp_endpoint_is_peeled_off(ep, &to)) {
1179 err = -EADDRNOTAVAIL;
1184 asoc = sctp_id2assoc(sk, associd);
1192 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1194 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1195 * socket that has an association in CLOSED state. This can
1196 * happen when an accepted socket has an association that is
1199 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1204 if (sinfo_flags & MSG_EOF) {
1205 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1207 sctp_primitive_SHUTDOWN(asoc, NULL);
1211 if (sinfo_flags & MSG_ABORT) {
1212 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1213 sctp_primitive_ABORT(asoc, msg);
1219 /* Do we need to create the association? */
1221 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1223 if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
1228 /* Check for invalid stream against the stream counts,
1229 * either the default or the user specified stream counts.
1232 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1233 /* Check against the defaults. */
1234 if (sinfo->sinfo_stream >=
1235 sp->initmsg.sinit_num_ostreams) {
1240 /* Check against the requested. */
1241 if (sinfo->sinfo_stream >=
1242 sinit->sinit_num_ostreams) {
1250 * API 3.1.2 bind() - UDP Style Syntax
1251 * If a bind() or sctp_bindx() is not called prior to a
1252 * sendmsg() call that initiates a new association, the
1253 * system picks an ephemeral port and will choose an address
1254 * set equivalent to binding with a wildcard address.
1256 if (!ep->base.bind_addr.port) {
1257 if (sctp_autobind(sk)) {
1263 scope = sctp_scope(&to);
1264 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1271 /* If the SCTP_INIT ancillary data is specified, set all
1272 * the association init values accordingly.
1275 if (sinit->sinit_num_ostreams) {
1276 asoc->c.sinit_num_ostreams =
1277 sinit->sinit_num_ostreams;
1279 if (sinit->sinit_max_instreams) {
1280 asoc->c.sinit_max_instreams =
1281 sinit->sinit_max_instreams;
1283 if (sinit->sinit_max_attempts) {
1284 asoc->max_init_attempts
1285 = sinit->sinit_max_attempts;
1287 if (sinit->sinit_max_init_timeo) {
1288 asoc->max_init_timeo =
1289 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1293 /* Prime the peer's transport structures. */
1294 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1299 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1306 /* ASSERT: we have a valid association at this point. */
1307 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1310 /* If the user didn't specify SNDRCVINFO, make up one with
1313 default_sinfo.sinfo_stream = asoc->default_stream;
1314 default_sinfo.sinfo_flags = asoc->default_flags;
1315 default_sinfo.sinfo_ppid = asoc->default_ppid;
1316 default_sinfo.sinfo_context = asoc->default_context;
1317 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1318 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1319 sinfo = &default_sinfo;
1322 /* API 7.1.7, the sndbuf size per association bounds the
1323 * maximum size of data that can be sent in a single send call.
1325 if (msg_len > sk->sk_sndbuf) {
1330 /* If fragmentation is disabled and the message length exceeds the
1331 * association fragmentation point, return EMSGSIZE. The I-D
1332 * does not specify what this error is, but this looks like
1335 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1341 /* Check for invalid stream. */
1342 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1348 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1349 if (!sctp_wspace(asoc)) {
1350 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1355 /* If an address is passed with the sendto/sendmsg call, it is used
1356 * to override the primary destination address in the TCP model, or
1357 * when MSG_ADDR_OVER flag is set in the UDP model.
1359 if ((sctp_style(sk, TCP) && msg_name) ||
1360 (sinfo_flags & MSG_ADDR_OVER)) {
1361 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1369 /* Auto-connect, if we aren't connected already. */
1370 if (sctp_state(asoc, CLOSED)) {
1371 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1374 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1377 /* Break the message into multiple chunks of maximum size. */
1378 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1384 /* Now send the (possibly) fragmented message. */
1385 list_for_each(pos, &datamsg->chunks) {
1386 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1387 sctp_datamsg_track(chunk);
1389 /* Do accounting for the write space. */
1390 sctp_set_owner_w(chunk);
1392 chunk->transport = chunk_tp;
1394 /* Send it to the lower layers. Note: all chunks
1395 * must either fail or succeed. The lower layer
1396 * works that way today. Keep it that way or this
1399 err = sctp_primitive_SEND(asoc, chunk);
1400 /* Did the lower layer accept the chunk? */
1402 sctp_chunk_free(chunk);
1403 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1406 sctp_datamsg_free(datamsg);
1412 /* If we are already past ASSOCIATE, the lower
1413 * layers are responsible for association cleanup.
1419 sctp_association_free(asoc);
1421 sctp_release_sock(sk);
1424 return sctp_error(sk, msg_flags, err);
1431 err = sock_error(sk);
1441 /* This is an extended version of skb_pull() that removes the data from the
1442 * start of a skb even when data is spread across the list of skb's in the
1443 * frag_list. len specifies the total amount of data that needs to be removed.
1444 * when 'len' bytes could be removed from the skb, it returns 0.
1445 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1446 * could not be removed.
1448 static int sctp_skb_pull(struct sk_buff *skb, int len)
1450 struct sk_buff *list;
1451 int skb_len = skb_headlen(skb);
1454 if (len <= skb_len) {
1455 __skb_pull(skb, len);
1459 __skb_pull(skb, skb_len);
1461 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1462 rlen = sctp_skb_pull(list, len);
1463 skb->len -= (len-rlen);
1464 skb->data_len -= (len-rlen);
1475 /* API 3.1.3 recvmsg() - UDP Style Syntax
1477 * ssize_t recvmsg(int socket, struct msghdr *message,
1480 * socket - the socket descriptor of the endpoint.
1481 * message - pointer to the msghdr structure which contains a single
1482 * user message and possibly some ancillary data.
1484 * See Section 5 for complete description of the data
1487 * flags - flags sent or received with the user message, see Section
1488 * 5 for complete description of the flags.
1490 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1492 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1493 struct msghdr *msg, size_t len, int noblock,
1494 int flags, int *addr_len)
1496 struct sctp_ulpevent *event = NULL;
1497 struct sctp_opt *sp = sctp_sk(sk);
1498 struct sk_buff *skb;
1503 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1504 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1505 "len", len, "knoblauch", noblock,
1506 "flags", flags, "addr_len", addr_len);
1510 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1515 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1519 /* Get the total length of the skb including any skb's in the
1528 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1530 event = sctp_skb2event(skb);
1535 sock_recv_timestamp(msg, sk, skb);
1536 if (sctp_ulpevent_is_notification(event)) {
1537 msg->msg_flags |= MSG_NOTIFICATION;
1538 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1540 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1543 /* Check if we allow SCTP_SNDRCVINFO. */
1544 if (sp->subscribe.sctp_data_io_event)
1545 sctp_ulpevent_read_sndrcvinfo(event, msg);
1547 /* FIXME: we should be calling IP/IPv6 layers. */
1548 if (sk->sk_protinfo.af_inet.cmsg_flags)
1549 ip_cmsg_recv(msg, skb);
1554 /* If skb's length exceeds the user's buffer, update the skb and
1555 * push it back to the receive_queue so that the next call to
1556 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1558 if (skb_len > copied) {
1559 msg->msg_flags &= ~MSG_EOR;
1560 if (flags & MSG_PEEK)
1562 sctp_skb_pull(skb, copied);
1563 skb_queue_head(&sk->sk_receive_queue, skb);
1565 /* When only partial message is copied to the user, increase
1566 * rwnd by that amount. If all the data in the skb is read,
1567 * rwnd is updated when the event is freed.
1569 sctp_assoc_rwnd_increase(event->asoc, copied);
1571 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1572 (event->msg_flags & MSG_EOR))
1573 msg->msg_flags |= MSG_EOR;
1575 msg->msg_flags &= ~MSG_EOR;
1578 if (flags & MSG_PEEK) {
1579 /* Release the skb reference acquired after peeking the skb in
1580 * sctp_skb_recv_datagram().
1584 /* Free the event which includes releasing the reference to
1585 * the owner of the skb, freeing the skb and updating the
1588 sctp_ulpevent_free(event);
1591 sctp_release_sock(sk);
1595 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1597 * This option is a on/off flag. If enabled no SCTP message
1598 * fragmentation will be performed. Instead if a message being sent
1599 * exceeds the current PMTU size, the message will NOT be sent and
1600 * instead a error will be indicated to the user.
1602 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1603 char __user *optval, int optlen)
1607 if (optlen < sizeof(int))
1610 if (get_user(val, (int __user *)optval))
1613 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1618 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1621 if (optlen != sizeof(struct sctp_event_subscribe))
1623 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1628 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1630 * This socket option is applicable to the UDP-style socket only. When
1631 * set it will cause associations that are idle for more than the
1632 * specified number of seconds to automatically close. An association
1633 * being idle is defined an association that has NOT sent or received
1634 * user data. The special value of '0' indicates that no automatic
1635 * close of any associations should be performed. The option expects an
1636 * integer defining the number of seconds of idle time before an
1637 * association is closed.
1639 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1642 struct sctp_opt *sp = sctp_sk(sk);
1644 /* Applicable to UDP-style socket only */
1645 if (sctp_style(sk, TCP))
1647 if (optlen != sizeof(int))
1649 if (copy_from_user(&sp->autoclose, optval, optlen))
1652 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1656 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1658 * Applications can enable or disable heartbeats for any peer address of
1659 * an association, modify an address's heartbeat interval, force a
1660 * heartbeat to be sent immediately, and adjust the address's maximum
1661 * number of retransmissions sent before an address is considered
1662 * unreachable. The following structure is used to access and modify an
1663 * address's parameters:
1665 * struct sctp_paddrparams {
1666 * sctp_assoc_t spp_assoc_id;
1667 * struct sockaddr_storage spp_address;
1668 * uint32_t spp_hbinterval;
1669 * uint16_t spp_pathmaxrxt;
1672 * spp_assoc_id - (UDP style socket) This is filled in the application,
1673 * and identifies the association for this query.
1674 * spp_address - This specifies which address is of interest.
1675 * spp_hbinterval - This contains the value of the heartbeat interval,
1676 * in milliseconds. A value of 0, when modifying the
1677 * parameter, specifies that the heartbeat on this
1678 * address should be disabled. A value of UINT32_MAX
1679 * (4294967295), when modifying the parameter,
1680 * specifies that a heartbeat should be sent
1681 * immediately to the peer address, and the current
1682 * interval should remain unchanged.
1683 * spp_pathmaxrxt - This contains the maximum number of
1684 * retransmissions before this address shall be
1685 * considered unreachable.
1687 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1688 char __user *optval, int optlen)
1690 struct sctp_paddrparams params;
1691 struct sctp_transport *trans;
1694 if (optlen != sizeof(struct sctp_paddrparams))
1696 if (copy_from_user(¶ms, optval, optlen))
1700 * API 7. Socket Options (setting the default value for the endpoint)
1701 * All options that support specific settings on an association by
1702 * filling in either an association id variable or a sockaddr_storage
1703 * SHOULD also support setting of the same value for the entire endpoint
1704 * (i.e. future associations). To accomplish this the following logic is
1705 * used when setting one of these options:
1707 * c) If neither the sockaddr_storage or association identification is
1708 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1709 * the association identification is 0, the settings are a default
1710 * and to be applied to the endpoint (all future associations).
1713 /* update default value for endpoint (all future associations) */
1714 if (!params.spp_assoc_id &&
1715 sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
1716 if (params.spp_hbinterval)
1717 sctp_sk(sk)->paddrparam.spp_hbinterval =
1718 params.spp_hbinterval;
1719 if (sctp_max_retrans_path)
1720 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
1721 params.spp_pathmaxrxt;
1725 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
1726 params.spp_assoc_id);
1730 /* Applications can enable or disable heartbeats for any peer address
1731 * of an association, modify an address's heartbeat interval, force a
1732 * heartbeat to be sent immediately, and adjust the address's maximum
1733 * number of retransmissions sent before an address is considered
1736 * The value of the heartbeat interval, in milliseconds. A value of
1737 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1738 * that a heartbeat should be sent immediately to the peer address,
1739 * and the current interval should remain unchanged.
1741 if (0xffffffff == params.spp_hbinterval) {
1742 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1746 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1747 * when modifying the parameter, specifies that the heartbeat on this
1748 * address should be disabled.
1750 if (params.spp_hbinterval) {
1751 trans->hb_allowed = 1;
1752 trans->hb_interval =
1753 msecs_to_jiffies(params.spp_hbinterval);
1755 trans->hb_allowed = 0;
1758 /* spp_pathmaxrxt contains the maximum number of retransmissions
1759 * before this address shall be considered unreachable.
1761 trans->error_threshold = params.spp_pathmaxrxt;
1766 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1768 * Applications can specify protocol parameters for the default association
1769 * initialization. The option name argument to setsockopt() and getsockopt()
1772 * Setting initialization parameters is effective only on an unconnected
1773 * socket (for UDP-style sockets only future associations are effected
1774 * by the change). With TCP-style sockets, this option is inherited by
1775 * sockets derived from a listener socket.
1777 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1779 struct sctp_initmsg sinit;
1780 struct sctp_opt *sp = sctp_sk(sk);
1782 if (optlen != sizeof(struct sctp_initmsg))
1784 if (copy_from_user(&sinit, optval, optlen))
1787 if (sinit.sinit_num_ostreams)
1788 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1789 if (sinit.sinit_max_instreams)
1790 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1791 if (sinit.sinit_max_attempts)
1792 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1793 if (sinit.sinit_max_init_timeo)
1794 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1800 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1802 * Applications that wish to use the sendto() system call may wish to
1803 * specify a default set of parameters that would normally be supplied
1804 * through the inclusion of ancillary data. This socket option allows
1805 * such an application to set the default sctp_sndrcvinfo structure.
1806 * The application that wishes to use this socket option simply passes
1807 * in to this call the sctp_sndrcvinfo structure defined in Section
1808 * 5.2.2) The input parameters accepted by this call include
1809 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1810 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1811 * to this call if the caller is using the UDP model.
1813 static int sctp_setsockopt_default_send_param(struct sock *sk,
1814 char __user *optval, int optlen)
1816 struct sctp_sndrcvinfo info;
1817 struct sctp_association *asoc;
1818 struct sctp_opt *sp = sctp_sk(sk);
1820 if (optlen != sizeof(struct sctp_sndrcvinfo))
1822 if (copy_from_user(&info, optval, optlen))
1825 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1826 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1830 asoc->default_stream = info.sinfo_stream;
1831 asoc->default_flags = info.sinfo_flags;
1832 asoc->default_ppid = info.sinfo_ppid;
1833 asoc->default_context = info.sinfo_context;
1834 asoc->default_timetolive = info.sinfo_timetolive;
1836 sp->default_stream = info.sinfo_stream;
1837 sp->default_flags = info.sinfo_flags;
1838 sp->default_ppid = info.sinfo_ppid;
1839 sp->default_context = info.sinfo_context;
1840 sp->default_timetolive = info.sinfo_timetolive;
1846 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1848 * Requests that the local SCTP stack use the enclosed peer address as
1849 * the association primary. The enclosed address must be one of the
1850 * association peer's addresses.
1852 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1855 struct sctp_prim prim;
1856 struct sctp_transport *trans;
1858 if (optlen != sizeof(struct sctp_prim))
1861 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1864 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1868 sctp_assoc_set_primary(trans->asoc, trans);
1874 * 7.1.5 SCTP_NODELAY
1876 * Turn on/off any Nagle-like algorithm. This means that packets are
1877 * generally sent as soon as possible and no unnecessary delays are
1878 * introduced, at the cost of more packets in the network. Expects an
1879 * integer boolean flag.
1881 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1886 if (optlen < sizeof(int))
1888 if (get_user(val, (int __user *)optval))
1891 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1897 * 7.1.1 SCTP_RTOINFO
1899 * The protocol parameters used to initialize and bound retransmission
1900 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1901 * and modify these parameters.
1902 * All parameters are time values, in milliseconds. A value of 0, when
1903 * modifying the parameters, indicates that the current value should not
1907 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1908 struct sctp_rtoinfo rtoinfo;
1909 struct sctp_association *asoc;
1911 if (optlen != sizeof (struct sctp_rtoinfo))
1914 if (copy_from_user(&rtoinfo, optval, optlen))
1917 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1919 /* Set the values to the specific association */
1920 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1924 if (rtoinfo.srto_initial != 0)
1926 msecs_to_jiffies(rtoinfo.srto_initial);
1927 if (rtoinfo.srto_max != 0)
1928 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1929 if (rtoinfo.srto_min != 0)
1930 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1932 /* If there is no association or the association-id = 0
1933 * set the values to the endpoint.
1935 struct sctp_opt *sp = sctp_sk(sk);
1937 if (rtoinfo.srto_initial != 0)
1938 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1939 if (rtoinfo.srto_max != 0)
1940 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1941 if (rtoinfo.srto_min != 0)
1942 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1950 * 7.1.2 SCTP_ASSOCINFO
1952 * This option is used to tune the the maximum retransmission attempts
1953 * of the association.
1954 * Returns an error if the new association retransmission value is
1955 * greater than the sum of the retransmission value of the peer.
1956 * See [SCTP] for more information.
1959 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1962 struct sctp_assocparams assocparams;
1963 struct sctp_association *asoc;
1965 if (optlen != sizeof(struct sctp_assocparams))
1967 if (copy_from_user(&assocparams, optval, optlen))
1970 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1972 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1975 /* Set the values to the specific association */
1977 if (assocparams.sasoc_asocmaxrxt != 0)
1978 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1979 if (assocparams.sasoc_cookie_life != 0) {
1980 asoc->cookie_life.tv_sec =
1981 assocparams.sasoc_cookie_life / 1000;
1982 asoc->cookie_life.tv_usec =
1983 (assocparams.sasoc_cookie_life % 1000)
1987 /* Set the values to the endpoint */
1988 struct sctp_opt *sp = sctp_sk(sk);
1990 if (assocparams.sasoc_asocmaxrxt != 0)
1991 sp->assocparams.sasoc_asocmaxrxt =
1992 assocparams.sasoc_asocmaxrxt;
1993 if (assocparams.sasoc_cookie_life != 0)
1994 sp->assocparams.sasoc_cookie_life =
1995 assocparams.sasoc_cookie_life;
2001 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2003 * This socket option is a boolean flag which turns on or off mapped V4
2004 * addresses. If this option is turned on and the socket is type
2005 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2006 * If this option is turned off, then no mapping will be done of V4
2007 * addresses and a user will receive both PF_INET6 and PF_INET type
2008 * addresses on the socket.
2010 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2013 struct sctp_opt *sp = sctp_sk(sk);
2015 if (optlen < sizeof(int))
2017 if (get_user(val, (int __user *)optval))
2028 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2030 * This socket option specifies the maximum size to put in any outgoing
2031 * SCTP chunk. If a message is larger than this size it will be
2032 * fragmented by SCTP into the specified size. Note that the underlying
2033 * SCTP implementation may fragment into smaller sized chunks when the
2034 * PMTU of the underlying association is smaller than the value set by
2037 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2039 struct sctp_association *asoc;
2040 struct list_head *pos;
2041 struct sctp_opt *sp = sctp_sk(sk);
2044 if (optlen < sizeof(int))
2046 if (get_user(val, (int __user *)optval))
2048 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
2050 sp->user_frag = val;
2053 /* Update the frag_point of the existing associations. */
2054 list_for_each(pos, &(sp->ep->asocs)) {
2055 asoc = list_entry(pos, struct sctp_association, asocs);
2056 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2065 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2067 * Requests that the peer mark the enclosed address as the association
2068 * primary. The enclosed address must be one of the association's
2069 * locally bound addresses. The following structure is used to make a
2070 * set primary request:
2072 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2075 struct sctp_opt *sp;
2076 struct sctp_endpoint *ep;
2077 struct sctp_association *asoc = NULL;
2078 struct sctp_setpeerprim prim;
2079 struct sctp_chunk *chunk;
2085 if (!sctp_addip_enable)
2088 if (optlen != sizeof(struct sctp_setpeerprim))
2091 if (copy_from_user(&prim, optval, optlen))
2094 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2098 if (!asoc->peer.asconf_capable)
2101 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2104 if (!sctp_state(asoc, ESTABLISHED))
2107 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2108 return -EADDRNOTAVAIL;
2110 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2111 chunk = sctp_make_asconf_set_prim(asoc,
2112 (union sctp_addr *)&prim.sspp_addr);
2116 err = sctp_send_asconf(asoc, chunk);
2118 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2123 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2128 if (optlen < sizeof(__u32))
2130 if (copy_from_user(&val, optval, sizeof(__u32)))
2133 sctp_sk(sk)->adaption_ind = val;
2138 /* API 6.2 setsockopt(), getsockopt()
2140 * Applications use setsockopt() and getsockopt() to set or retrieve
2141 * socket options. Socket options are used to change the default
2142 * behavior of sockets calls. They are described in Section 7.
2146 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2147 * int __user *optlen);
2148 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2151 * sd - the socket descript.
2152 * level - set to IPPROTO_SCTP for all SCTP options.
2153 * optname - the option name.
2154 * optval - the buffer to store the value of the option.
2155 * optlen - the size of the buffer.
2157 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2158 char __user *optval, int optlen)
2162 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2165 /* I can hardly begin to describe how wrong this is. This is
2166 * so broken as to be worse than useless. The API draft
2167 * REALLY is NOT helpful here... I am not convinced that the
2168 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2169 * are at all well-founded.
2171 if (level != SOL_SCTP) {
2172 struct sctp_af *af = sctp_sk(sk)->pf->af;
2173 retval = af->setsockopt(sk, level, optname, optval, optlen);
2180 case SCTP_SOCKOPT_BINDX_ADD:
2181 /* 'optlen' is the size of the addresses buffer. */
2182 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2183 optlen, SCTP_BINDX_ADD_ADDR);
2186 case SCTP_SOCKOPT_BINDX_REM:
2187 /* 'optlen' is the size of the addresses buffer. */
2188 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2189 optlen, SCTP_BINDX_REM_ADDR);
2192 case SCTP_DISABLE_FRAGMENTS:
2193 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2197 retval = sctp_setsockopt_events(sk, optval, optlen);
2200 case SCTP_AUTOCLOSE:
2201 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2204 case SCTP_PEER_ADDR_PARAMS:
2205 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2209 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2211 case SCTP_DEFAULT_SEND_PARAM:
2212 retval = sctp_setsockopt_default_send_param(sk, optval,
2215 case SCTP_PRIMARY_ADDR:
2216 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2218 case SCTP_SET_PEER_PRIMARY_ADDR:
2219 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2222 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2225 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2227 case SCTP_ASSOCINFO:
2228 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2230 case SCTP_I_WANT_MAPPED_V4_ADDR:
2231 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2234 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2236 case SCTP_ADAPTION_LAYER:
2237 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2241 retval = -ENOPROTOOPT;
2245 sctp_release_sock(sk);
2251 /* API 3.1.6 connect() - UDP Style Syntax
2253 * An application may use the connect() call in the UDP model to initiate an
2254 * association without sending data.
2258 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2260 * sd: the socket descriptor to have a new association added to.
2262 * nam: the address structure (either struct sockaddr_in or struct
2263 * sockaddr_in6 defined in RFC2553 [7]).
2265 * len: the size of the address.
2267 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2270 struct sctp_opt *sp;
2271 struct sctp_endpoint *ep;
2272 struct sctp_association *asoc;
2273 struct sctp_transport *transport;
2282 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2283 __FUNCTION__, sk, uaddr, addr_len);
2288 /* connect() cannot be done on a socket that is already in ESTABLISHED
2289 * state - UDP-style peeled off socket or a TCP-style socket that
2290 * is already connected.
2291 * It cannot be done even on a TCP-style listening socket.
2293 if (sctp_sstate(sk, ESTABLISHED) ||
2294 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2299 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2303 if (addr_len > sizeof(to))
2304 addr_len = sizeof(to);
2305 memcpy(&to, uaddr, addr_len);
2306 to.v4.sin_port = ntohs(to.v4.sin_port);
2308 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2310 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2317 /* If we could not find a matching association on the endpoint,
2318 * make sure that there is no peeled-off association matching the
2319 * peer address even on another socket.
2321 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2322 err = -EADDRNOTAVAIL;
2326 /* If a bind() or sctp_bindx() is not called prior to a connect()
2327 * call, the system picks an ephemeral port and will choose an address
2328 * set equivalent to binding with a wildcard address.
2330 if (!ep->base.bind_addr.port) {
2331 if (sctp_autobind(sk)) {
2337 scope = sctp_scope(&to);
2338 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2344 /* Prime the peer's transport structures. */
2345 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2347 sctp_association_free(asoc);
2350 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2352 sctp_association_free(asoc);
2356 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2358 sctp_association_free(asoc);
2362 /* Initialize sk's dport and daddr for getpeername() */
2363 inet_sk(sk)->dport = htons(asoc->peer.port);
2364 af = sctp_get_af_specific(to.sa.sa_family);
2365 af->to_sk_daddr(&to, sk);
2367 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2368 err = sctp_wait_for_connect(asoc, &timeo);
2371 sctp_release_sock(sk);
2376 /* FIXME: Write comments. */
2377 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2379 return -EOPNOTSUPP; /* STUB */
2382 /* 4.1.4 accept() - TCP Style Syntax
2384 * Applications use accept() call to remove an established SCTP
2385 * association from the accept queue of the endpoint. A new socket
2386 * descriptor will be returned from accept() to represent the newly
2387 * formed association.
2389 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2391 struct sctp_opt *sp;
2392 struct sctp_endpoint *ep;
2393 struct sock *newsk = NULL;
2394 struct sctp_association *asoc;
2403 if (!sctp_style(sk, TCP)) {
2404 error = -EOPNOTSUPP;
2408 if (!sctp_sstate(sk, LISTENING)) {
2413 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2415 error = sctp_wait_for_accept(sk, timeo);
2419 /* We treat the list of associations on the endpoint as the accept
2420 * queue and pick the first association on the list.
2422 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2424 newsk = sp->pf->create_accept_sk(sk, asoc);
2430 /* Populate the fields of the newsk from the oldsk and migrate the
2431 * asoc to the newsk.
2433 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2436 sctp_release_sock(sk);
2441 /* The SCTP ioctl handler. */
2442 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2444 return -ENOIOCTLCMD;
2447 /* This is the function which gets called during socket creation to
2448 * initialized the SCTP-specific portion of the sock.
2449 * The sock structure should already be zero-filled memory.
2451 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2453 struct sctp_endpoint *ep;
2454 struct sctp_opt *sp;
2456 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2460 /* Initialize the SCTP per socket area. */
2461 switch (sk->sk_type) {
2462 case SOCK_SEQPACKET:
2463 sp->type = SCTP_SOCKET_UDP;
2466 sp->type = SCTP_SOCKET_TCP;
2469 return -ESOCKTNOSUPPORT;
2472 /* Initialize default send parameters. These parameters can be
2473 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2475 sp->default_stream = 0;
2476 sp->default_ppid = 0;
2477 sp->default_flags = 0;
2478 sp->default_context = 0;
2479 sp->default_timetolive = 0;
2481 /* Initialize default setup parameters. These parameters
2482 * can be modified with the SCTP_INITMSG socket option or
2483 * overridden by the SCTP_INIT CMSG.
2485 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2486 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2487 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2488 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2490 /* Initialize default RTO related parameters. These parameters can
2491 * be modified for with the SCTP_RTOINFO socket option.
2493 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2494 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2495 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2497 /* Initialize default association related parameters. These parameters
2498 * can be modified with the SCTP_ASSOCINFO socket option.
2500 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2501 sp->assocparams.sasoc_number_peer_destinations = 0;
2502 sp->assocparams.sasoc_peer_rwnd = 0;
2503 sp->assocparams.sasoc_local_rwnd = 0;
2504 sp->assocparams.sasoc_cookie_life =
2505 jiffies_to_msecs(sctp_valid_cookie_life);
2507 /* Initialize default event subscriptions. By default, all the
2510 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2512 /* Default Peer Address Parameters. These defaults can
2513 * be modified via SCTP_PEER_ADDR_PARAMS
2515 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2516 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2518 /* If enabled no SCTP message fragmentation will be performed.
2519 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2521 sp->disable_fragments = 0;
2523 /* Turn on/off any Nagle-like algorithm. */
2526 /* Enable by default. */
2529 /* Auto-close idle associations after the configured
2530 * number of seconds. A value of 0 disables this
2531 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2532 * for UDP-style sockets only.
2536 /* User specified fragmentation limit. */
2539 sp->adaption_ind = 0;
2541 sp->pf = sctp_get_pf_specific(sk->sk_family);
2543 /* Control variables for partial data delivery. */
2545 skb_queue_head_init(&sp->pd_lobby);
2547 /* Create a per socket endpoint structure. Even if we
2548 * change the data structure relationships, this may still
2549 * be useful for storing pre-connect address information.
2551 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2558 SCTP_DBG_OBJCNT_INC(sock);
2562 /* Cleanup any SCTP per socket resources. */
2563 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2565 struct sctp_endpoint *ep;
2567 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2569 /* Release our hold on the endpoint. */
2570 ep = sctp_sk(sk)->ep;
2571 sctp_endpoint_free(ep);
2576 /* API 4.1.7 shutdown() - TCP Style Syntax
2577 * int shutdown(int socket, int how);
2579 * sd - the socket descriptor of the association to be closed.
2580 * how - Specifies the type of shutdown. The values are
2583 * Disables further receive operations. No SCTP
2584 * protocol action is taken.
2586 * Disables further send operations, and initiates
2587 * the SCTP shutdown sequence.
2589 * Disables further send and receive operations
2590 * and initiates the SCTP shutdown sequence.
2592 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2594 struct sctp_endpoint *ep;
2595 struct sctp_association *asoc;
2597 if (!sctp_style(sk, TCP))
2600 if (how & SEND_SHUTDOWN) {
2601 ep = sctp_sk(sk)->ep;
2602 if (!list_empty(&ep->asocs)) {
2603 asoc = list_entry(ep->asocs.next,
2604 struct sctp_association, asocs);
2605 sctp_primitive_SHUTDOWN(asoc, NULL);
2610 /* 7.2.1 Association Status (SCTP_STATUS)
2612 * Applications can retrieve current status information about an
2613 * association, including association state, peer receiver window size,
2614 * number of unacked data chunks, and number of data chunks pending
2615 * receipt. This information is read-only.
2617 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2618 char __user *optval,
2621 struct sctp_status status;
2622 struct sctp_association *asoc = NULL;
2623 struct sctp_transport *transport;
2624 sctp_assoc_t associd;
2627 if (len != sizeof(status)) {
2632 if (copy_from_user(&status, optval, sizeof(status))) {
2637 associd = status.sstat_assoc_id;
2638 asoc = sctp_id2assoc(sk, associd);
2644 transport = asoc->peer.primary_path;
2646 status.sstat_assoc_id = sctp_assoc2id(asoc);
2647 status.sstat_state = asoc->state;
2648 status.sstat_rwnd = asoc->peer.rwnd;
2649 status.sstat_unackdata = asoc->unack_data;
2651 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2652 status.sstat_instrms = asoc->c.sinit_max_instreams;
2653 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2654 status.sstat_fragmentation_point = asoc->frag_point;
2655 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2656 memcpy(&status.sstat_primary.spinfo_address,
2657 &(transport->ipaddr), sizeof(union sctp_addr));
2658 /* Map ipv4 address into v4-mapped-on-v6 address. */
2659 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2660 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2661 status.sstat_primary.spinfo_state = transport->active;
2662 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2663 status.sstat_primary.spinfo_srtt = transport->srtt;
2664 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2665 status.sstat_primary.spinfo_mtu = transport->pmtu;
2667 if (put_user(len, optlen)) {
2672 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
2673 len, status.sstat_state, status.sstat_rwnd,
2674 status.sstat_assoc_id);
2676 if (copy_to_user(optval, &status, len)) {
2686 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2688 * Applications can retrieve information about a specific peer address
2689 * of an association, including its reachability state, congestion
2690 * window, and retransmission timer values. This information is
2693 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2694 char __user *optval,
2697 struct sctp_paddrinfo pinfo;
2698 struct sctp_transport *transport;
2701 if (len != sizeof(pinfo)) {
2706 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2711 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2712 pinfo.spinfo_assoc_id);
2716 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2717 pinfo.spinfo_state = transport->active;
2718 pinfo.spinfo_cwnd = transport->cwnd;
2719 pinfo.spinfo_srtt = transport->srtt;
2720 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2721 pinfo.spinfo_mtu = transport->pmtu;
2723 if (put_user(len, optlen)) {
2728 if (copy_to_user(optval, &pinfo, len)) {
2737 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2739 * This option is a on/off flag. If enabled no SCTP message
2740 * fragmentation will be performed. Instead if a message being sent
2741 * exceeds the current PMTU size, the message will NOT be sent and
2742 * instead a error will be indicated to the user.
2744 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2745 char __user *optval, int __user *optlen)
2749 if (len < sizeof(int))
2753 val = (sctp_sk(sk)->disable_fragments == 1);
2754 if (put_user(len, optlen))
2756 if (copy_to_user(optval, &val, len))
2761 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2763 * This socket option is used to specify various notifications and
2764 * ancillary data the user wishes to receive.
2766 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2769 if (len != sizeof(struct sctp_event_subscribe))
2771 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2776 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2778 * This socket option is applicable to the UDP-style socket only. When
2779 * set it will cause associations that are idle for more than the
2780 * specified number of seconds to automatically close. An association
2781 * being idle is defined an association that has NOT sent or received
2782 * user data. The special value of '0' indicates that no automatic
2783 * close of any associations should be performed. The option expects an
2784 * integer defining the number of seconds of idle time before an
2785 * association is closed.
2787 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2789 /* Applicable to UDP-style socket only */
2790 if (sctp_style(sk, TCP))
2792 if (len != sizeof(int))
2794 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2799 /* Helper routine to branch off an association to a new socket. */
2800 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2801 struct socket **sockp)
2803 struct sock *sk = asoc->base.sk;
2804 struct socket *sock;
2807 /* An association cannot be branched off from an already peeled-off
2808 * socket, nor is this supported for tcp style sockets.
2810 if (!sctp_style(sk, UDP))
2813 /* Create a new socket. */
2814 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2818 /* Populate the fields of the newsk from the oldsk and migrate the
2819 * asoc to the newsk.
2821 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2827 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2829 sctp_peeloff_arg_t peeloff;
2830 struct socket *newsock;
2832 struct sctp_association *asoc;
2834 if (len != sizeof(sctp_peeloff_arg_t))
2836 if (copy_from_user(&peeloff, optval, len))
2839 asoc = sctp_id2assoc(sk, peeloff.associd);
2845 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2847 retval = sctp_do_peeloff(asoc, &newsock);
2851 /* Map the socket to an unused fd that can be returned to the user. */
2852 retval = sock_map_fd(newsock);
2854 sock_release(newsock);
2858 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2859 __FUNCTION__, sk, asoc, newsock->sk, retval);
2861 /* Return the fd mapped to the new socket. */
2862 peeloff.sd = retval;
2863 if (copy_to_user(optval, &peeloff, len))
2870 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2872 * Applications can enable or disable heartbeats for any peer address of
2873 * an association, modify an address's heartbeat interval, force a
2874 * heartbeat to be sent immediately, and adjust the address's maximum
2875 * number of retransmissions sent before an address is considered
2876 * unreachable. The following structure is used to access and modify an
2877 * address's parameters:
2879 * struct sctp_paddrparams {
2880 * sctp_assoc_t spp_assoc_id;
2881 * struct sockaddr_storage spp_address;
2882 * uint32_t spp_hbinterval;
2883 * uint16_t spp_pathmaxrxt;
2886 * spp_assoc_id - (UDP style socket) This is filled in the application,
2887 * and identifies the association for this query.
2888 * spp_address - This specifies which address is of interest.
2889 * spp_hbinterval - This contains the value of the heartbeat interval,
2890 * in milliseconds. A value of 0, when modifying the
2891 * parameter, specifies that the heartbeat on this
2892 * address should be disabled. A value of UINT32_MAX
2893 * (4294967295), when modifying the parameter,
2894 * specifies that a heartbeat should be sent
2895 * immediately to the peer address, and the current
2896 * interval should remain unchanged.
2897 * spp_pathmaxrxt - This contains the maximum number of
2898 * retransmissions before this address shall be
2899 * considered unreachable.
2901 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2902 char __user *optval, int __user *optlen)
2904 struct sctp_paddrparams params;
2905 struct sctp_transport *trans;
2907 if (len != sizeof(struct sctp_paddrparams))
2909 if (copy_from_user(¶ms, optval, len))
2912 /* If no association id is specified retrieve the default value
2913 * for the endpoint that will be used for all future associations
2915 if (!params.spp_assoc_id &&
2916 sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
2917 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
2918 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
2923 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2924 params.spp_assoc_id);
2928 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2929 * when modifying the parameter, specifies that the heartbeat on this
2930 * address should be disabled.
2932 if (!trans->hb_allowed)
2933 params.spp_hbinterval = 0;
2935 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2937 /* spp_pathmaxrxt contains the maximum number of retransmissions
2938 * before this address shall be considered unreachable.
2940 params.spp_pathmaxrxt = trans->error_threshold;
2943 if (copy_to_user(optval, ¶ms, len))
2946 if (put_user(len, optlen))
2952 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2954 * Applications can specify protocol parameters for the default association
2955 * initialization. The option name argument to setsockopt() and getsockopt()
2958 * Setting initialization parameters is effective only on an unconnected
2959 * socket (for UDP-style sockets only future associations are effected
2960 * by the change). With TCP-style sockets, this option is inherited by
2961 * sockets derived from a listener socket.
2963 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2965 if (len != sizeof(struct sctp_initmsg))
2967 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2972 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2973 char __user *optval, int __user *optlen)
2976 struct sctp_association *asoc;
2977 struct list_head *pos;
2980 if (len != sizeof(sctp_assoc_t))
2983 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2986 /* For UDP-style sockets, id specifies the association to query. */
2987 asoc = sctp_id2assoc(sk, id);
2991 list_for_each(pos, &asoc->peer.transport_addr_list) {
2998 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
2999 char __user *optval, int __user *optlen)
3001 struct sctp_association *asoc;
3002 struct list_head *pos;
3004 struct sctp_getaddrs getaddrs;
3005 struct sctp_transport *from;
3007 union sctp_addr temp;
3008 struct sctp_opt *sp = sctp_sk(sk);
3011 if (len != sizeof(struct sctp_getaddrs))
3014 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3017 if (getaddrs.addr_num <= 0) return -EINVAL;
3019 /* For UDP-style sockets, id specifies the association to query. */
3020 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3024 to = (void __user *)getaddrs.addrs;
3025 list_for_each(pos, &asoc->peer.transport_addr_list) {
3026 from = list_entry(pos, struct sctp_transport, transports);
3027 memcpy(&temp, &from->ipaddr, sizeof(temp));
3028 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3029 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3030 temp.v4.sin_port = htons(temp.v4.sin_port);
3031 if (copy_to_user(to, &temp, addrlen))
3035 if (cnt >= getaddrs.addr_num) break;
3037 getaddrs.addr_num = cnt;
3038 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3044 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
3045 char __user *optval,
3049 struct sctp_bind_addr *bp;
3050 struct sctp_association *asoc;
3051 struct list_head *pos;
3054 if (len != sizeof(sctp_assoc_t))
3057 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3061 * For UDP-style sockets, id specifies the association to query.
3062 * If the id field is set to the value '0' then the locally bound
3063 * addresses are returned without regard to any particular
3067 bp = &sctp_sk(sk)->ep->base.bind_addr;
3069 asoc = sctp_id2assoc(sk, id);
3072 bp = &asoc->base.bind_addr;
3075 list_for_each(pos, &bp->address_list) {
3082 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3083 char __user *optval, int __user *optlen)
3085 struct sctp_bind_addr *bp;
3086 struct sctp_association *asoc;
3087 struct list_head *pos;
3089 struct sctp_getaddrs getaddrs;
3090 struct sctp_sockaddr_entry *from;
3092 union sctp_addr temp;
3093 struct sctp_opt *sp = sctp_sk(sk);
3096 if (len != sizeof(struct sctp_getaddrs))
3099 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3102 if (getaddrs.addr_num <= 0) return -EINVAL;
3104 * For UDP-style sockets, id specifies the association to query.
3105 * If the id field is set to the value '0' then the locally bound
3106 * addresses are returned without regard to any particular
3109 if (0 == getaddrs.assoc_id) {
3110 bp = &sctp_sk(sk)->ep->base.bind_addr;
3112 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3115 bp = &asoc->base.bind_addr;
3118 to = getaddrs.addrs;
3119 list_for_each(pos, &bp->address_list) {
3120 from = list_entry(pos,
3121 struct sctp_sockaddr_entry,
3123 memcpy(&temp, &from->a, sizeof(temp));
3124 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3125 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3126 temp.v4.sin_port = htons(temp.v4.sin_port);
3127 if (copy_to_user(to, &temp, addrlen))
3131 if (cnt >= getaddrs.addr_num) break;
3133 getaddrs.addr_num = cnt;
3134 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3140 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3142 * Requests that the local SCTP stack use the enclosed peer address as
3143 * the association primary. The enclosed address must be one of the
3144 * association peer's addresses.
3146 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3147 char __user *optval, int __user *optlen)
3149 struct sctp_prim prim;
3150 struct sctp_association *asoc;
3151 struct sctp_opt *sp = sctp_sk(sk);
3153 if (len != sizeof(struct sctp_prim))
3156 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3159 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3163 if (!asoc->peer.primary_path)
3166 asoc->peer.primary_path->ipaddr.v4.sin_port =
3167 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3168 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3169 sizeof(union sctp_addr));
3170 asoc->peer.primary_path->ipaddr.v4.sin_port =
3171 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3173 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3174 (union sctp_addr *)&prim.ssp_addr);
3176 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3183 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
3185 * Requests that the local endpoint set the specified Adaption Layer
3186 * Indication parameter for all future INIT and INIT-ACK exchanges.
3188 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
3189 char __user *optval, int __user *optlen)
3193 if (len < sizeof(__u32))
3196 len = sizeof(__u32);
3197 val = sctp_sk(sk)->adaption_ind;
3198 if (put_user(len, optlen))
3200 if (copy_to_user(optval, &val, len))
3207 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3209 * Applications that wish to use the sendto() system call may wish to
3210 * specify a default set of parameters that would normally be supplied
3211 * through the inclusion of ancillary data. This socket option allows
3212 * such an application to set the default sctp_sndrcvinfo structure.
3215 * The application that wishes to use this socket option simply passes
3216 * in to this call the sctp_sndrcvinfo structure defined in Section
3217 * 5.2.2) The input parameters accepted by this call include
3218 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3219 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3220 * to this call if the caller is using the UDP model.
3222 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3224 static int sctp_getsockopt_default_send_param(struct sock *sk,
3225 int len, char __user *optval,
3228 struct sctp_sndrcvinfo info;
3229 struct sctp_association *asoc;
3230 struct sctp_opt *sp = sctp_sk(sk);
3232 if (len != sizeof(struct sctp_sndrcvinfo))
3234 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3237 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3238 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3242 info.sinfo_stream = asoc->default_stream;
3243 info.sinfo_flags = asoc->default_flags;
3244 info.sinfo_ppid = asoc->default_ppid;
3245 info.sinfo_context = asoc->default_context;
3246 info.sinfo_timetolive = asoc->default_timetolive;
3248 info.sinfo_stream = sp->default_stream;
3249 info.sinfo_flags = sp->default_flags;
3250 info.sinfo_ppid = sp->default_ppid;
3251 info.sinfo_context = sp->default_context;
3252 info.sinfo_timetolive = sp->default_timetolive;
3255 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3263 * 7.1.5 SCTP_NODELAY
3265 * Turn on/off any Nagle-like algorithm. This means that packets are
3266 * generally sent as soon as possible and no unnecessary delays are
3267 * introduced, at the cost of more packets in the network. Expects an
3268 * integer boolean flag.
3271 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3272 char __user *optval, int __user *optlen)
3276 if (len < sizeof(int))
3280 val = (sctp_sk(sk)->nodelay == 1);
3281 if (put_user(len, optlen))
3283 if (copy_to_user(optval, &val, len))
3290 * 7.1.1 SCTP_RTOINFO
3292 * The protocol parameters used to initialize and bound retransmission
3293 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3294 * and modify these parameters.
3295 * All parameters are time values, in milliseconds. A value of 0, when
3296 * modifying the parameters, indicates that the current value should not
3300 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3301 char __user *optval,
3302 int __user *optlen) {
3303 struct sctp_rtoinfo rtoinfo;
3304 struct sctp_association *asoc;
3306 if (len != sizeof (struct sctp_rtoinfo))
3309 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3312 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3314 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3317 /* Values corresponding to the specific association. */
3319 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3320 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3321 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3323 /* Values corresponding to the endpoint. */
3324 struct sctp_opt *sp = sctp_sk(sk);
3326 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3327 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3328 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3331 if (put_user(len, optlen))
3334 if (copy_to_user(optval, &rtoinfo, len))
3342 * 7.1.2 SCTP_ASSOCINFO
3344 * This option is used to tune the the maximum retransmission attempts
3345 * of the association.
3346 * Returns an error if the new association retransmission value is
3347 * greater than the sum of the retransmission value of the peer.
3348 * See [SCTP] for more information.
3351 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3352 char __user *optval,
3356 struct sctp_assocparams assocparams;
3357 struct sctp_association *asoc;
3358 struct list_head *pos;
3361 if (len != sizeof (struct sctp_assocparams))
3364 if (copy_from_user(&assocparams, optval,
3365 sizeof (struct sctp_assocparams)))
3368 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3370 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3373 /* Values correspoinding to the specific association */
3374 if (assocparams.sasoc_assoc_id != 0) {
3375 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3376 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3377 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3378 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3380 (asoc->cookie_life.tv_usec
3383 list_for_each(pos, &asoc->peer.transport_addr_list) {
3387 assocparams.sasoc_number_peer_destinations = cnt;
3389 /* Values corresponding to the endpoint */
3390 struct sctp_opt *sp = sctp_sk(sk);
3392 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3393 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3394 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3395 assocparams.sasoc_cookie_life =
3396 sp->assocparams.sasoc_cookie_life;
3397 assocparams.sasoc_number_peer_destinations =
3399 sasoc_number_peer_destinations;
3402 if (put_user(len, optlen))
3405 if (copy_to_user(optval, &assocparams, len))
3412 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3414 * This socket option is a boolean flag which turns on or off mapped V4
3415 * addresses. If this option is turned on and the socket is type
3416 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3417 * If this option is turned off, then no mapping will be done of V4
3418 * addresses and a user will receive both PF_INET6 and PF_INET type
3419 * addresses on the socket.
3421 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3422 char __user *optval, int __user *optlen)
3425 struct sctp_opt *sp = sctp_sk(sk);
3427 if (len < sizeof(int))
3432 if (put_user(len, optlen))
3434 if (copy_to_user(optval, &val, len))
3441 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3443 * This socket option specifies the maximum size to put in any outgoing
3444 * SCTP chunk. If a message is larger than this size it will be
3445 * fragmented by SCTP into the specified size. Note that the underlying
3446 * SCTP implementation may fragment into smaller sized chunks when the
3447 * PMTU of the underlying association is smaller than the value set by
3450 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3451 char __user *optval, int __user *optlen)
3455 if (len < sizeof(int))
3460 val = sctp_sk(sk)->user_frag;
3461 if (put_user(len, optlen))
3463 if (copy_to_user(optval, &val, len))
3469 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3470 char __user *optval, int __user *optlen)
3475 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3477 /* I can hardly begin to describe how wrong this is. This is
3478 * so broken as to be worse than useless. The API draft
3479 * REALLY is NOT helpful here... I am not convinced that the
3480 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3481 * are at all well-founded.
3483 if (level != SOL_SCTP) {
3484 struct sctp_af *af = sctp_sk(sk)->pf->af;
3486 retval = af->getsockopt(sk, level, optname, optval, optlen);
3490 if (get_user(len, optlen))
3497 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3499 case SCTP_DISABLE_FRAGMENTS:
3500 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3504 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3506 case SCTP_AUTOCLOSE:
3507 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3509 case SCTP_SOCKOPT_PEELOFF:
3510 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3512 case SCTP_PEER_ADDR_PARAMS:
3513 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3517 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3519 case SCTP_GET_PEER_ADDRS_NUM:
3520 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3523 case SCTP_GET_LOCAL_ADDRS_NUM:
3524 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3527 case SCTP_GET_PEER_ADDRS:
3528 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3531 case SCTP_GET_LOCAL_ADDRS:
3532 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3535 case SCTP_DEFAULT_SEND_PARAM:
3536 retval = sctp_getsockopt_default_send_param(sk, len,
3539 case SCTP_PRIMARY_ADDR:
3540 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3543 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3546 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3548 case SCTP_ASSOCINFO:
3549 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3551 case SCTP_I_WANT_MAPPED_V4_ADDR:
3552 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3555 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3557 case SCTP_GET_PEER_ADDR_INFO:
3558 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3561 case SCTP_ADAPTION_LAYER:
3562 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
3566 retval = -ENOPROTOOPT;
3570 sctp_release_sock(sk);
3574 static void sctp_hash(struct sock *sk)
3579 static void sctp_unhash(struct sock *sk)
3584 /* Check if port is acceptable. Possibly find first available port.
3586 * The port hash table (contained in the 'global' SCTP protocol storage
3587 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3588 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3589 * list (the list number is the port number hashed out, so as you
3590 * would expect from a hash function, all the ports in a given list have
3591 * such a number that hashes out to the same list number; you were
3592 * expecting that, right?); so each list has a set of ports, with a
3593 * link to the socket (struct sock) that uses it, the port number and
3594 * a fastreuse flag (FIXME: NPI ipg).
3596 static struct sctp_bind_bucket *sctp_bucket_create(
3597 struct sctp_bind_hashbucket *head, unsigned short snum);
3599 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3601 struct sctp_bind_hashbucket *head; /* hash list */
3602 struct sctp_bind_bucket *pp; /* hash list port iterator */
3603 unsigned short snum;
3606 /* NOTE: Remember to put this back to net order. */
3607 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3608 snum = addr->v4.sin_port;
3610 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3611 sctp_local_bh_disable();
3614 /* Search for an available port.
3616 * 'sctp_port_rover' was the last port assigned, so
3617 * we start to search from 'sctp_port_rover +
3618 * 1'. What we do is first check if port 'rover' is
3619 * already in the hash table; if not, we use that; if
3620 * it is, we try next.
3622 int low = sysctl_local_port_range[0];
3623 int high = sysctl_local_port_range[1];
3624 int remaining = (high - low) + 1;
3628 sctp_spin_lock(&sctp_port_alloc_lock);
3629 rover = sctp_port_rover;
3632 if ((rover < low) || (rover > high))
3634 index = sctp_phashfn(rover);
3635 head = &sctp_port_hashtable[index];
3636 sctp_spin_lock(&head->lock);
3637 for (pp = head->chain; pp; pp = pp->next)
3638 if (pp->port == rover)
3642 sctp_spin_unlock(&head->lock);
3643 } while (--remaining > 0);
3644 sctp_port_rover = rover;
3645 sctp_spin_unlock(&sctp_port_alloc_lock);
3647 /* Exhausted local port range during search? */
3652 /* OK, here is the one we will use. HEAD (the port
3653 * hash table list entry) is non-NULL and we hold it's
3658 /* We are given an specific port number; we verify
3659 * that it is not being used. If it is used, we will
3660 * exahust the search in the hash list corresponding
3661 * to the port number (snum) - we detect that with the
3662 * port iterator, pp being NULL.
3664 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3665 sctp_spin_lock(&head->lock);
3666 for (pp = head->chain; pp; pp = pp->next) {
3667 if (pp->port == snum)
3674 if (!hlist_empty(&pp->owner)) {
3675 /* We had a port hash table hit - there is an
3676 * available port (pp != NULL) and it is being
3677 * used by other socket (pp->owner not empty); that other
3678 * socket is going to be sk2.
3680 int reuse = sk->sk_reuse;
3682 struct hlist_node *node;
3684 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3685 if (pp->fastreuse && sk->sk_reuse)
3688 /* Run through the list of sockets bound to the port
3689 * (pp->port) [via the pointers bind_next and
3690 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3691 * we get the endpoint they describe and run through
3692 * the endpoint's list of IP (v4 or v6) addresses,
3693 * comparing each of the addresses with the address of
3694 * the socket sk. If we find a match, then that means
3695 * that this port/socket (sk) combination are already
3698 sk_for_each_bound(sk2, node, &pp->owner) {
3699 struct sctp_endpoint *ep2;
3700 ep2 = sctp_sk(sk2)->ep;
3702 if (reuse && sk2->sk_reuse)
3705 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3711 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3714 /* If there was a hash table miss, create a new port. */
3716 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3719 /* In either case (hit or miss), make sure fastreuse is 1 only
3720 * if sk->sk_reuse is too (that is, if the caller requested
3721 * SO_REUSEADDR on this socket -sk-).
3723 if (hlist_empty(&pp->owner))
3724 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3725 else if (pp->fastreuse && !sk->sk_reuse)
3728 /* We are set, so fill up all the data in the hash table
3729 * entry, tie the socket list information with the rest of the
3730 * sockets FIXME: Blurry, NPI (ipg).
3733 inet_sk(sk)->num = snum;
3734 if (!sctp_sk(sk)->bind_hash) {
3735 sk_add_bind_node(sk, &pp->owner);
3736 sctp_sk(sk)->bind_hash = pp;
3741 sctp_spin_unlock(&head->lock);
3744 sctp_local_bh_enable();
3745 addr->v4.sin_port = htons(addr->v4.sin_port);
3749 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3750 * port is requested.
3752 static int sctp_get_port(struct sock *sk, unsigned short snum)
3755 union sctp_addr addr;
3756 struct sctp_af *af = sctp_sk(sk)->pf->af;
3758 /* Set up a dummy address struct from the sk. */
3759 af->from_sk(&addr, sk);
3760 addr.v4.sin_port = htons(snum);
3762 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3763 ret = sctp_get_port_local(sk, &addr);
3765 return (ret ? 1 : 0);
3769 * 3.1.3 listen() - UDP Style Syntax
3771 * By default, new associations are not accepted for UDP style sockets.
3772 * An application uses listen() to mark a socket as being able to
3773 * accept new associations.
3775 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3777 struct sctp_opt *sp = sctp_sk(sk);
3778 struct sctp_endpoint *ep = sp->ep;
3780 /* Only UDP style sockets that are not peeled off are allowed to
3783 if (!sctp_style(sk, UDP))
3786 /* If backlog is zero, disable listening. */
3788 if (sctp_sstate(sk, CLOSED))
3791 sctp_unhash_endpoint(ep);
3792 sk->sk_state = SCTP_SS_CLOSED;
3795 /* Return if we are already listening. */
3796 if (sctp_sstate(sk, LISTENING))
3800 * If a bind() or sctp_bindx() is not called prior to a listen()
3801 * call that allows new associations to be accepted, the system
3802 * picks an ephemeral port and will choose an address set equivalent
3803 * to binding with a wildcard address.
3805 * This is not currently spelled out in the SCTP sockets
3806 * extensions draft, but follows the practice as seen in TCP
3809 if (!ep->base.bind_addr.port) {
3810 if (sctp_autobind(sk))
3813 sk->sk_state = SCTP_SS_LISTENING;
3814 sctp_hash_endpoint(ep);
3819 * 4.1.3 listen() - TCP Style Syntax
3821 * Applications uses listen() to ready the SCTP endpoint for accepting
3822 * inbound associations.
3824 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3826 struct sctp_opt *sp = sctp_sk(sk);
3827 struct sctp_endpoint *ep = sp->ep;
3829 /* If backlog is zero, disable listening. */
3831 if (sctp_sstate(sk, CLOSED))
3834 sctp_unhash_endpoint(ep);
3835 sk->sk_state = SCTP_SS_CLOSED;
3838 if (sctp_sstate(sk, LISTENING))
3842 * If a bind() or sctp_bindx() is not called prior to a listen()
3843 * call that allows new associations to be accepted, the system
3844 * picks an ephemeral port and will choose an address set equivalent
3845 * to binding with a wildcard address.
3847 * This is not currently spelled out in the SCTP sockets
3848 * extensions draft, but follows the practice as seen in TCP
3851 if (!ep->base.bind_addr.port) {
3852 if (sctp_autobind(sk))
3855 sk->sk_state = SCTP_SS_LISTENING;
3856 sk->sk_max_ack_backlog = backlog;
3857 sctp_hash_endpoint(ep);
3862 * Move a socket to LISTENING state.
3864 int sctp_inet_listen(struct socket *sock, int backlog)
3866 struct sock *sk = sock->sk;
3867 struct crypto_tfm *tfm=NULL;
3870 if (unlikely(backlog < 0))
3875 if (sock->state != SS_UNCONNECTED)
3878 /* Allocate HMAC for generating cookie. */
3879 if (sctp_hmac_alg) {
3880 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
3887 switch (sock->type) {
3888 case SOCK_SEQPACKET:
3889 err = sctp_seqpacket_listen(sk, backlog);
3892 err = sctp_stream_listen(sk, backlog);
3900 /* Store away the transform reference. */
3901 sctp_sk(sk)->hmac = tfm;
3903 sctp_release_sock(sk);
3907 sctp_crypto_free_tfm(tfm);
3912 * This function is done by modeling the current datagram_poll() and the
3913 * tcp_poll(). Note that, based on these implementations, we don't
3914 * lock the socket in this function, even though it seems that,
3915 * ideally, locking or some other mechanisms can be used to ensure
3916 * the integrity of the counters (sndbuf and wmem_queued) used
3917 * in this place. We assume that we don't need locks either until proven
3920 * Another thing to note is that we include the Async I/O support
3921 * here, again, by modeling the current TCP/UDP code. We don't have
3922 * a good way to test with it yet.
3924 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
3926 struct sock *sk = sock->sk;
3927 struct sctp_opt *sp = sctp_sk(sk);
3930 poll_wait(file, sk->sk_sleep, wait);
3932 /* A TCP-style listening socket becomes readable when the accept queue
3935 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3936 return (!list_empty(&sp->ep->asocs)) ?
3937 (POLLIN | POLLRDNORM) : 0;
3941 /* Is there any exceptional events? */
3942 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
3944 if (sk->sk_shutdown == SHUTDOWN_MASK)
3947 /* Is it readable? Reconsider this code with TCP-style support. */
3948 if (!skb_queue_empty(&sk->sk_receive_queue) ||
3949 (sk->sk_shutdown & RCV_SHUTDOWN))
3950 mask |= POLLIN | POLLRDNORM;
3952 /* The association is either gone or not ready. */
3953 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
3956 /* Is it writable? */
3957 if (sctp_writeable(sk)) {
3958 mask |= POLLOUT | POLLWRNORM;
3960 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
3962 * Since the socket is not locked, the buffer
3963 * might be made available after the writeable check and
3964 * before the bit is set. This could cause a lost I/O
3965 * signal. tcp_poll() has a race breaker for this race
3966 * condition. Based on their implementation, we put
3967 * in the following code to cover it as well.
3969 if (sctp_writeable(sk))
3970 mask |= POLLOUT | POLLWRNORM;
3975 /********************************************************************
3976 * 2nd Level Abstractions
3977 ********************************************************************/
3979 static struct sctp_bind_bucket *sctp_bucket_create(
3980 struct sctp_bind_hashbucket *head, unsigned short snum)
3982 struct sctp_bind_bucket *pp;
3984 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
3985 SCTP_DBG_OBJCNT_INC(bind_bucket);
3989 INIT_HLIST_HEAD(&pp->owner);
3990 if ((pp->next = head->chain) != NULL)
3991 pp->next->pprev = &pp->next;
3993 pp->pprev = &head->chain;
3998 /* Caller must hold hashbucket lock for this tb with local BH disabled */
3999 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4001 if (hlist_empty(&pp->owner)) {
4003 pp->next->pprev = pp->pprev;
4004 *(pp->pprev) = pp->next;
4005 kmem_cache_free(sctp_bucket_cachep, pp);
4006 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4010 /* Release this socket's reference to a local port. */
4011 static inline void __sctp_put_port(struct sock *sk)
4013 struct sctp_bind_hashbucket *head =
4014 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4015 struct sctp_bind_bucket *pp;
4017 sctp_spin_lock(&head->lock);
4018 pp = sctp_sk(sk)->bind_hash;
4019 __sk_del_bind_node(sk);
4020 sctp_sk(sk)->bind_hash = NULL;
4021 inet_sk(sk)->num = 0;
4022 sctp_bucket_destroy(pp);
4023 sctp_spin_unlock(&head->lock);
4026 void sctp_put_port(struct sock *sk)
4028 sctp_local_bh_disable();
4029 __sctp_put_port(sk);
4030 sctp_local_bh_enable();
4034 * The system picks an ephemeral port and choose an address set equivalent
4035 * to binding with a wildcard address.
4036 * One of those addresses will be the primary address for the association.
4037 * This automatically enables the multihoming capability of SCTP.
4039 static int sctp_autobind(struct sock *sk)
4041 union sctp_addr autoaddr;
4043 unsigned short port;
4045 /* Initialize a local sockaddr structure to INADDR_ANY. */
4046 af = sctp_sk(sk)->pf->af;
4048 port = htons(inet_sk(sk)->num);
4049 af->inaddr_any(&autoaddr, port);
4051 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4054 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4057 * 4.2 The cmsghdr Structure *
4059 * When ancillary data is sent or received, any number of ancillary data
4060 * objects can be specified by the msg_control and msg_controllen members of
4061 * the msghdr structure, because each object is preceded by
4062 * a cmsghdr structure defining the object's length (the cmsg_len member).
4063 * Historically Berkeley-derived implementations have passed only one object
4064 * at a time, but this API allows multiple objects to be
4065 * passed in a single call to sendmsg() or recvmsg(). The following example
4066 * shows two ancillary data objects in a control buffer.
4068 * |<--------------------------- msg_controllen -------------------------->|
4071 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4073 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4076 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4078 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4081 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4082 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4084 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4086 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4093 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4094 sctp_cmsgs_t *cmsgs)
4096 struct cmsghdr *cmsg;
4098 for (cmsg = CMSG_FIRSTHDR(msg);
4100 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4101 if (!CMSG_OK(msg, cmsg))
4104 /* Should we parse this header or ignore? */
4105 if (cmsg->cmsg_level != IPPROTO_SCTP)
4108 /* Strictly check lengths following example in SCM code. */
4109 switch (cmsg->cmsg_type) {
4111 /* SCTP Socket API Extension
4112 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4114 * This cmsghdr structure provides information for
4115 * initializing new SCTP associations with sendmsg().
4116 * The SCTP_INITMSG socket option uses this same data
4117 * structure. This structure is not used for
4120 * cmsg_level cmsg_type cmsg_data[]
4121 * ------------ ------------ ----------------------
4122 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4124 if (cmsg->cmsg_len !=
4125 CMSG_LEN(sizeof(struct sctp_initmsg)))
4127 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4131 /* SCTP Socket API Extension
4132 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4134 * This cmsghdr structure specifies SCTP options for
4135 * sendmsg() and describes SCTP header information
4136 * about a received message through recvmsg().
4138 * cmsg_level cmsg_type cmsg_data[]
4139 * ------------ ------------ ----------------------
4140 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4142 if (cmsg->cmsg_len !=
4143 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4147 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4149 /* Minimally, validate the sinfo_flags. */
4150 if (cmsgs->info->sinfo_flags &
4151 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4152 MSG_ABORT | MSG_EOF))
4164 * Wait for a packet..
4165 * Note: This function is the same function as in core/datagram.c
4166 * with a few modifications to make lksctp work.
4168 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4173 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4175 /* Socket errors? */
4176 error = sock_error(sk);
4180 if (!skb_queue_empty(&sk->sk_receive_queue))
4183 /* Socket shut down? */
4184 if (sk->sk_shutdown & RCV_SHUTDOWN)
4187 /* Sequenced packets can come disconnected. If so we report the
4192 /* Is there a good reason to think that we may receive some data? */
4193 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4196 /* Handle signals. */
4197 if (signal_pending(current))
4200 /* Let another process have a go. Since we are going to sleep
4201 * anyway. Note: This may cause odd behaviors if the message
4202 * does not fit in the user's buffer, but this seems to be the
4203 * only way to honor MSG_DONTWAIT realistically.
4205 sctp_release_sock(sk);
4206 *timeo_p = schedule_timeout(*timeo_p);
4210 finish_wait(sk->sk_sleep, &wait);
4214 error = sock_intr_errno(*timeo_p);
4217 finish_wait(sk->sk_sleep, &wait);
4222 /* Receive a datagram.
4223 * Note: This is pretty much the same routine as in core/datagram.c
4224 * with a few changes to make lksctp work.
4226 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4227 int noblock, int *err)
4230 struct sk_buff *skb;
4233 /* Caller is allowed not to check sk->sk_err before calling. */
4234 error = sock_error(sk);
4238 timeo = sock_rcvtimeo(sk, noblock);
4240 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4241 timeo, MAX_SCHEDULE_TIMEOUT);
4244 /* Again only user level code calls this function,
4245 * so nothing interrupt level
4246 * will suddenly eat the receive_queue.
4248 * Look at current nfs client by the way...
4249 * However, this function was corrent in any case. 8)
4251 if (flags & MSG_PEEK) {
4252 unsigned long cpu_flags;
4254 sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4256 skb = skb_peek(&sk->sk_receive_queue);
4258 atomic_inc(&skb->users);
4259 sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4262 skb = skb_dequeue(&sk->sk_receive_queue);
4268 if (sk->sk_shutdown & RCV_SHUTDOWN)
4271 /* User doesn't want to wait. */
4275 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4284 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4285 static void __sctp_write_space(struct sctp_association *asoc)
4287 struct sock *sk = asoc->base.sk;
4288 struct socket *sock = sk->sk_socket;
4290 if ((sctp_wspace(asoc) > 0) && sock) {
4291 if (waitqueue_active(&asoc->wait))
4292 wake_up_interruptible(&asoc->wait);
4294 if (sctp_writeable(sk)) {
4295 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4296 wake_up_interruptible(sk->sk_sleep);
4298 /* Note that we try to include the Async I/O support
4299 * here by modeling from the current TCP/UDP code.
4300 * We have not tested with it yet.
4302 if (sock->fasync_list &&
4303 !(sk->sk_shutdown & SEND_SHUTDOWN))
4304 sock_wake_async(sock, 2, POLL_OUT);
4309 /* Do accounting for the sndbuf space.
4310 * Decrement the used sndbuf space of the corresponding association by the
4311 * data size which was just transmitted(freed).
4313 static void sctp_wfree(struct sk_buff *skb)
4315 struct sctp_association *asoc;
4316 struct sctp_chunk *chunk;
4319 /* Get the saved chunk pointer. */
4320 chunk = *((struct sctp_chunk **)(skb->cb));
4323 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk);
4324 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk);
4325 __sctp_write_space(asoc);
4327 sctp_association_put(asoc);
4330 /* Helper function to wait for space in the sndbuf. */
4331 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4334 struct sock *sk = asoc->base.sk;
4336 long current_timeo = *timeo_p;
4339 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4340 asoc, (long)(*timeo_p), msg_len);
4342 /* Increment the association's refcnt. */
4343 sctp_association_hold(asoc);
4345 /* Wait on the association specific sndbuf space. */
4347 prepare_to_wait_exclusive(&asoc->wait, &wait,
4348 TASK_INTERRUPTIBLE);
4351 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4354 if (signal_pending(current))
4355 goto do_interrupted;
4356 if (msg_len <= sctp_wspace(asoc))
4359 /* Let another process have a go. Since we are going
4362 sctp_release_sock(sk);
4363 current_timeo = schedule_timeout(current_timeo);
4366 *timeo_p = current_timeo;
4370 finish_wait(&asoc->wait, &wait);
4372 /* Release the association's refcnt. */
4373 sctp_association_put(asoc);
4382 err = sock_intr_errno(*timeo_p);
4390 /* If socket sndbuf has changed, wake up all per association waiters. */
4391 void sctp_write_space(struct sock *sk)
4393 struct sctp_association *asoc;
4394 struct list_head *pos;
4396 /* Wake up the tasks in each wait queue. */
4397 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4398 asoc = list_entry(pos, struct sctp_association, asocs);
4399 __sctp_write_space(asoc);
4403 /* Is there any sndbuf space available on the socket?
4405 * Note that wmem_queued is the sum of the send buffers on all of the
4406 * associations on the same socket. For a UDP-style socket with
4407 * multiple associations, it is possible for it to be "unwriteable"
4408 * prematurely. I assume that this is acceptable because
4409 * a premature "unwriteable" is better than an accidental "writeable" which
4410 * would cause an unwanted block under certain circumstances. For the 1-1
4411 * UDP-style sockets or TCP-style sockets, this code should work.
4414 static int sctp_writeable(struct sock *sk)
4418 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4424 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4425 * returns immediately with EINPROGRESS.
4427 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4429 struct sock *sk = asoc->base.sk;
4431 long current_timeo = *timeo_p;
4434 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4437 /* Increment the association's refcnt. */
4438 sctp_association_hold(asoc);
4441 prepare_to_wait_exclusive(&asoc->wait, &wait,
4442 TASK_INTERRUPTIBLE);
4445 if (sk->sk_shutdown & RCV_SHUTDOWN)
4447 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4450 if (signal_pending(current))
4451 goto do_interrupted;
4453 if (sctp_state(asoc, ESTABLISHED))
4456 /* Let another process have a go. Since we are going
4459 sctp_release_sock(sk);
4460 current_timeo = schedule_timeout(current_timeo);
4463 *timeo_p = current_timeo;
4467 finish_wait(&asoc->wait, &wait);
4469 /* Release the association's refcnt. */
4470 sctp_association_put(asoc);
4475 if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
4476 asoc->max_init_attempts)
4479 err = -ECONNREFUSED;
4483 err = sock_intr_errno(*timeo_p);
4491 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4493 struct sctp_endpoint *ep;
4497 ep = sctp_sk(sk)->ep;
4501 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4502 TASK_INTERRUPTIBLE);
4504 if (list_empty(&ep->asocs)) {
4505 sctp_release_sock(sk);
4506 timeo = schedule_timeout(timeo);
4511 if (!sctp_sstate(sk, LISTENING))
4515 if (!list_empty(&ep->asocs))
4518 err = sock_intr_errno(timeo);
4519 if (signal_pending(current))
4527 finish_wait(sk->sk_sleep, &wait);
4532 void sctp_wait_for_close(struct sock *sk, long timeout)
4537 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4538 if (list_empty(&sctp_sk(sk)->ep->asocs))
4540 sctp_release_sock(sk);
4541 timeout = schedule_timeout(timeout);
4543 } while (!signal_pending(current) && timeout);
4545 finish_wait(sk->sk_sleep, &wait);
4548 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4549 * and its messages to the newsk.
4551 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4552 struct sctp_association *assoc,
4553 sctp_socket_type_t type)
4555 struct sctp_opt *oldsp = sctp_sk(oldsk);
4556 struct sctp_opt *newsp = sctp_sk(newsk);
4557 struct sctp_bind_bucket *pp; /* hash list port iterator */
4558 struct sctp_endpoint *newep = newsp->ep;
4559 struct sk_buff *skb, *tmp;
4560 struct sctp_ulpevent *event;
4562 /* Migrate socket buffer sizes and all the socket level options to the
4565 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4566 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4567 /* Brute force copy old sctp opt. */
4568 memcpy(newsp, oldsp, sizeof(struct sctp_opt));
4570 /* Restore the ep value that was overwritten with the above structure
4576 /* Hook this new socket in to the bind_hash list. */
4577 pp = sctp_sk(oldsk)->bind_hash;
4578 sk_add_bind_node(newsk, &pp->owner);
4579 sctp_sk(newsk)->bind_hash = pp;
4580 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4582 /* Move any messages in the old socket's receive queue that are for the
4583 * peeled off association to the new socket's receive queue.
4585 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4586 event = sctp_skb2event(skb);
4587 if (event->asoc == assoc) {
4588 __skb_unlink(skb, skb->list);
4589 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4593 /* Clean up any messages pending delivery due to partial
4594 * delivery. Three cases:
4595 * 1) No partial deliver; no work.
4596 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4597 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
4599 skb_queue_head_init(&newsp->pd_lobby);
4600 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4602 if (sctp_sk(oldsk)->pd_mode) {
4603 struct sk_buff_head *queue;
4605 /* Decide which queue to move pd_lobby skbs to. */
4606 if (assoc->ulpq.pd_mode) {
4607 queue = &newsp->pd_lobby;
4609 queue = &newsk->sk_receive_queue;
4611 /* Walk through the pd_lobby, looking for skbs that
4612 * need moved to the new socket.
4614 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4615 event = sctp_skb2event(skb);
4616 if (event->asoc == assoc) {
4617 __skb_unlink(skb, skb->list);
4618 __skb_queue_tail(queue, skb);
4622 /* Clear up any skbs waiting for the partial
4623 * delivery to finish.
4625 if (assoc->ulpq.pd_mode)
4626 sctp_clear_pd(oldsk);
4630 /* Set the type of socket to indicate that it is peeled off from the
4631 * original UDP-style socket or created with the accept() call on a
4632 * TCP-style socket..
4636 /* Migrate the association to the new socket. */
4637 sctp_assoc_migrate(assoc, newsk);
4639 /* If the association on the newsk is already closed before accept()
4640 * is called, set RCV_SHUTDOWN flag.
4642 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4643 newsk->sk_shutdown |= RCV_SHUTDOWN;
4645 newsk->sk_state = SCTP_SS_ESTABLISHED;
4648 /* This proto struct describes the ULP interface for SCTP. */
4649 struct proto sctp_prot = {
4651 .owner = THIS_MODULE,
4652 .close = sctp_close,
4653 .connect = sctp_connect,
4654 .disconnect = sctp_disconnect,
4655 .accept = sctp_accept,
4656 .ioctl = sctp_ioctl,
4657 .init = sctp_init_sock,
4658 .destroy = sctp_destroy_sock,
4659 .shutdown = sctp_shutdown,
4660 .setsockopt = sctp_setsockopt,
4661 .getsockopt = sctp_getsockopt,
4662 .sendmsg = sctp_sendmsg,
4663 .recvmsg = sctp_recvmsg,
4665 .backlog_rcv = sctp_backlog_rcv,
4667 .unhash = sctp_unhash,
4668 .get_port = sctp_get_port,
4669 .slab_obj_size = sizeof(struct sctp_sock),
4672 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4673 struct proto sctpv6_prot = {
4675 .owner = THIS_MODULE,
4676 .close = sctp_close,
4677 .connect = sctp_connect,
4678 .disconnect = sctp_disconnect,
4679 .accept = sctp_accept,
4680 .ioctl = sctp_ioctl,
4681 .init = sctp_init_sock,
4682 .destroy = sctp_destroy_sock,
4683 .shutdown = sctp_shutdown,
4684 .setsockopt = sctp_setsockopt,
4685 .getsockopt = sctp_getsockopt,
4686 .sendmsg = sctp_sendmsg,
4687 .recvmsg = sctp_recvmsg,
4689 .backlog_rcv = sctp_backlog_rcv,
4691 .unhash = sctp_unhash,
4692 .get_port = sctp_get_port,
4693 .slab_obj_size = sizeof(struct sctp6_sock),
4695 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */