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 inline int sctp_wspace(struct sctp_association *asoc);
90 static inline void sctp_set_owner_w(struct sctp_chunk *chunk);
91 static void sctp_wfree(struct sk_buff *skb);
92 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
94 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
95 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
96 static int sctp_wait_for_accept(struct sock *sk, long timeo);
97 static void sctp_wait_for_close(struct sock *sk, long timeo);
98 static inline int sctp_verify_addr(struct sock *, union sctp_addr *, int);
99 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
100 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
103 static int sctp_send_asconf(struct sctp_association *asoc,
104 struct sctp_chunk *chunk);
105 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
106 static int sctp_autobind(struct sock *sk);
107 static void sctp_sock_migrate(struct sock *, struct sock *,
108 struct sctp_association *, sctp_socket_type_t);
109 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
111 extern kmem_cache_t *sctp_bucket_cachep;
112 extern int sctp_assoc_valid(struct sock *sk, struct sctp_association *asoc);
114 /* Look up the association by its id. If this is not a UDP-style
115 * socket, the ID field is always ignored.
117 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
119 struct sctp_association *asoc = NULL;
121 /* If this is not a UDP-style socket, assoc id should be ignored. */
122 if (!sctp_style(sk, UDP)) {
123 /* Return NULL if the socket state is not ESTABLISHED. It
124 * could be a TCP-style listening socket or a socket which
125 * hasn't yet called connect() to establish an association.
127 if (!sctp_sstate(sk, ESTABLISHED))
130 /* Get the first and the only association from the list. */
131 if (!list_empty(&sctp_sk(sk)->ep->asocs))
132 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
133 struct sctp_association, asocs);
137 /* Otherwise this is a UDP-style socket. */
138 if (!id || (id == (sctp_assoc_t)-1))
141 spin_lock_bh(&sctp_assocs_id_lock);
142 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
143 spin_unlock_bh(&sctp_assocs_id_lock);
145 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
151 /* Look up the transport from an address and an assoc id. If both address and
152 * id are specified, the associations matching the address and the id should be
155 struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
156 struct sockaddr_storage *addr,
159 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
160 struct sctp_transport *transport;
161 union sctp_addr *laddr = (union sctp_addr *)addr;
163 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
164 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
165 (union sctp_addr *)addr,
167 laddr->v4.sin_port = htons(laddr->v4.sin_port);
172 id_asoc = sctp_id2assoc(sk, id);
173 if (id_asoc && (id_asoc != addr_asoc))
176 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
177 (union sctp_addr *)addr);
182 /* API 3.1.2 bind() - UDP Style Syntax
183 * The syntax of bind() is,
185 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
187 * sd - the socket descriptor returned by socket().
188 * addr - the address structure (struct sockaddr_in or struct
189 * sockaddr_in6 [RFC 2553]),
190 * addr_len - the size of the address structure.
192 int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
198 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
199 sk, uaddr, addr_len);
201 /* Disallow binding twice. */
202 if (!sctp_sk(sk)->ep->base.bind_addr.port)
203 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
208 sctp_release_sock(sk);
213 static long sctp_get_port_local(struct sock *, union sctp_addr *);
215 /* Verify this is a valid sockaddr. */
216 static struct sctp_af *sctp_sockaddr_af(struct sctp_opt *opt,
217 union sctp_addr *addr, int len)
221 /* Check minimum size. */
222 if (len < sizeof (struct sockaddr))
225 /* Does this PF support this AF? */
226 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
229 /* If we get this far, af is valid. */
230 af = sctp_get_af_specific(addr->sa.sa_family);
232 if (len < af->sockaddr_len)
238 /* Bind a local address either to an endpoint or to an association. */
239 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
241 struct sctp_opt *sp = sctp_sk(sk);
242 struct sctp_endpoint *ep = sp->ep;
243 struct sctp_bind_addr *bp = &ep->base.bind_addr;
248 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
251 /* Common sockaddr verification. */
252 af = sctp_sockaddr_af(sp, addr, len);
256 /* PF specific bind() address verification. */
257 if (!sp->pf->bind_verify(sp, addr))
258 return -EADDRNOTAVAIL;
260 snum= ntohs(addr->v4.sin_port);
262 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
265 /* We must either be unbound, or bind to the same port. */
266 if (bp->port && (snum != bp->port)) {
267 SCTP_DEBUG_PRINTK("sctp_do_bind:"
268 " New port %d does not match existing port "
269 "%d.\n", snum, bp->port);
273 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
276 /* Make sure we are allowed to bind here.
277 * The function sctp_get_port_local() does duplicate address
280 if ((ret = sctp_get_port_local(sk, addr))) {
281 if (ret == (long) sk) {
282 /* This endpoint has a conflicting address. */
289 /* Refresh ephemeral port. */
291 snum = inet_sk(sk)->num;
293 /* Add the address to the bind address list. */
294 sctp_local_bh_disable();
295 sctp_write_lock(&ep->base.addr_lock);
297 /* Use GFP_ATOMIC since BHs are disabled. */
298 addr->v4.sin_port = ntohs(addr->v4.sin_port);
299 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
300 addr->v4.sin_port = htons(addr->v4.sin_port);
301 if (!ret && !bp->port)
303 sctp_write_unlock(&ep->base.addr_lock);
304 sctp_local_bh_enable();
306 /* Copy back into socket for getsockname() use. */
308 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
309 af->to_sk_saddr(addr, sk);
315 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
317 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
318 * at any one time. If a sender, after sending an ASCONF chunk, decides
319 * it needs to transfer another ASCONF Chunk, it MUST wait until the
320 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
321 * subsequent ASCONF. Note this restriction binds each side, so at any
322 * time two ASCONF may be in-transit on any given association (one sent
323 * from each endpoint).
325 static int sctp_send_asconf(struct sctp_association *asoc,
326 struct sctp_chunk *chunk)
330 /* If there is an outstanding ASCONF chunk, queue it for later
333 if (asoc->addip_last_asconf) {
334 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
338 /* Hold the chunk until an ASCONF_ACK is received. */
339 sctp_chunk_hold(chunk);
340 retval = sctp_primitive_ASCONF(asoc, chunk);
342 sctp_chunk_free(chunk);
344 asoc->addip_last_asconf = chunk;
350 /* Add a list of addresses as bind addresses to local endpoint or
353 * Basically run through each address specified in the addrs/addrcnt
354 * array/length pair, determine if it is IPv6 or IPv4 and call
355 * sctp_do_bind() on it.
357 * If any of them fails, then the operation will be reversed and the
358 * ones that were added will be removed.
360 * Only sctp_setsockopt_bindx() is supposed to call this function.
362 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
367 struct sockaddr *sa_addr;
370 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
374 for (cnt = 0; cnt < addrcnt; cnt++) {
375 /* The list may contain either IPv4 or IPv6 address;
376 * determine the address length for walking thru the list.
378 sa_addr = (struct sockaddr *)addr_buf;
379 af = sctp_get_af_specific(sa_addr->sa_family);
385 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
388 addr_buf += af->sockaddr_len;
392 /* Failed. Cleanup the ones that have been added */
394 sctp_bindx_rem(sk, addrs, cnt);
402 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
403 * associations that are part of the endpoint indicating that a list of local
404 * addresses are added to the endpoint.
406 * If any of the addresses is already in the bind address list of the
407 * association, we do not send the chunk for that association. But it will not
408 * affect other associations.
410 * Only sctp_setsockopt_bindx() is supposed to call this function.
412 static int sctp_send_asconf_add_ip(struct sock *sk,
413 struct sockaddr *addrs,
417 struct sctp_endpoint *ep;
418 struct sctp_association *asoc;
419 struct sctp_bind_addr *bp;
420 struct sctp_chunk *chunk;
421 struct sctp_sockaddr_entry *laddr;
422 union sctp_addr *addr;
425 struct list_head *pos;
430 if (!sctp_addip_enable)
436 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
437 __FUNCTION__, sk, addrs, addrcnt);
439 list_for_each(pos, &ep->asocs) {
440 asoc = list_entry(pos, struct sctp_association, asocs);
442 if (!asoc->peer.asconf_capable)
445 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
448 if (!sctp_state(asoc, ESTABLISHED))
451 /* Check if any address in the packed array of addresses is
452 * in the bind address list of the association. If so,
453 * do not send the asconf chunk to its peer, but continue with
454 * other associations.
457 for (i = 0; i < addrcnt; i++) {
458 addr = (union sctp_addr *)addr_buf;
459 af = sctp_get_af_specific(addr->v4.sin_family);
465 if (sctp_assoc_lookup_laddr(asoc, addr))
468 addr_buf += af->sockaddr_len;
473 /* Use the first address in bind addr list of association as
474 * Address Parameter of ASCONF CHUNK.
476 sctp_read_lock(&asoc->base.addr_lock);
477 bp = &asoc->base.bind_addr;
478 p = bp->address_list.next;
479 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
480 sctp_read_unlock(&asoc->base.addr_lock);
482 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
483 addrcnt, SCTP_PARAM_ADD_IP);
489 retval = sctp_send_asconf(asoc, chunk);
491 /* FIXME: After sending the add address ASCONF chunk, we
492 * cannot append the address to the association's binding
493 * address list, because the new address may be used as the
494 * source of a message sent to the peer before the ASCONF
495 * chunk is received by the peer. So we should wait until
496 * ASCONF_ACK is received.
504 /* Remove a list of addresses from bind addresses list. Do not remove the
507 * Basically run through each address specified in the addrs/addrcnt
508 * array/length pair, determine if it is IPv6 or IPv4 and call
509 * sctp_del_bind() on it.
511 * If any of them fails, then the operation will be reversed and the
512 * ones that were removed will be added back.
514 * At least one address has to be left; if only one address is
515 * available, the operation will return -EBUSY.
517 * Only sctp_setsockopt_bindx() is supposed to call this function.
519 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
521 struct sctp_opt *sp = sctp_sk(sk);
522 struct sctp_endpoint *ep = sp->ep;
524 struct sctp_bind_addr *bp = &ep->base.bind_addr;
526 union sctp_addr saveaddr;
528 struct sockaddr *sa_addr;
531 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
535 for (cnt = 0; cnt < addrcnt; cnt++) {
536 /* If the bind address list is empty or if there is only one
537 * bind address, there is nothing more to be removed (we need
538 * at least one address here).
540 if (list_empty(&bp->address_list) ||
541 (sctp_list_single_entry(&bp->address_list))) {
546 /* The list may contain either IPv4 or IPv6 address;
547 * determine the address length to copy the address to
550 sa_addr = (struct sockaddr *)addr_buf;
551 af = sctp_get_af_specific(sa_addr->sa_family);
556 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
557 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
558 if (saveaddr.v4.sin_port != bp->port) {
563 /* FIXME - There is probably a need to check if sk->sk_saddr and
564 * sk->sk_rcv_addr are currently set to one of the addresses to
565 * be removed. This is something which needs to be looked into
566 * when we are fixing the outstanding issues with multi-homing
567 * socket routing and failover schemes. Refer to comments in
568 * sctp_do_bind(). -daisy
570 sctp_local_bh_disable();
571 sctp_write_lock(&ep->base.addr_lock);
573 retval = sctp_del_bind_addr(bp, &saveaddr);
575 sctp_write_unlock(&ep->base.addr_lock);
576 sctp_local_bh_enable();
578 addr_buf += af->sockaddr_len;
581 /* Failed. Add the ones that has been removed back */
583 sctp_bindx_add(sk, addrs, cnt);
591 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
592 * the associations that are part of the endpoint indicating that a list of
593 * local addresses are removed from the endpoint.
595 * If any of the addresses is already in the bind address list of the
596 * association, we do not send the chunk for that association. But it will not
597 * affect other associations.
599 * Only sctp_setsockopt_bindx() is supposed to call this function.
601 static int sctp_send_asconf_del_ip(struct sock *sk,
602 struct sockaddr *addrs,
606 struct sctp_endpoint *ep;
607 struct sctp_association *asoc;
608 struct sctp_bind_addr *bp;
609 struct sctp_chunk *chunk;
610 union sctp_addr *laddr;
613 struct list_head *pos;
617 if (!sctp_addip_enable)
623 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
624 __FUNCTION__, sk, addrs, addrcnt);
626 list_for_each(pos, &ep->asocs) {
627 asoc = list_entry(pos, struct sctp_association, asocs);
629 if (!asoc->peer.asconf_capable)
632 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
635 if (!sctp_state(asoc, ESTABLISHED))
638 /* Check if any address in the packed array of addresses is
639 * not present in the bind address list of the association.
640 * If so, do not send the asconf chunk to its peer, but
641 * continue with other associations.
644 for (i = 0; i < addrcnt; i++) {
645 laddr = (union sctp_addr *)addr_buf;
646 af = sctp_get_af_specific(laddr->v4.sin_family);
652 if (!sctp_assoc_lookup_laddr(asoc, laddr))
655 addr_buf += af->sockaddr_len;
660 /* Find one address in the association's bind address list
661 * that is not in the packed array of addresses. This is to
662 * make sure that we do not delete all the addresses in the
665 sctp_read_lock(&asoc->base.addr_lock);
666 bp = &asoc->base.bind_addr;
667 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
669 sctp_read_unlock(&asoc->base.addr_lock);
673 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
680 retval = sctp_send_asconf(asoc, chunk);
682 /* FIXME: After sending the delete address ASCONF chunk, we
683 * cannot remove the addresses from the association's bind
684 * address list, because there maybe some packet send to
685 * the delete addresses, so we should wait until ASCONF_ACK
686 * packet is received.
693 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
696 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
699 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
700 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
703 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
704 * Section 3.1.2 for this usage.
706 * addrs is a pointer to an array of one or more socket addresses. Each
707 * address is contained in its appropriate structure (i.e. struct
708 * sockaddr_in or struct sockaddr_in6) the family of the address type
709 * must be used to distengish the address length (note that this
710 * representation is termed a "packed array" of addresses). The caller
711 * specifies the number of addresses in the array with addrcnt.
713 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
714 * -1, and sets errno to the appropriate error code.
716 * For SCTP, the port given in each socket address must be the same, or
717 * sctp_bindx() will fail, setting errno to EINVAL.
719 * The flags parameter is formed from the bitwise OR of zero or more of
720 * the following currently defined flags:
722 * SCTP_BINDX_ADD_ADDR
724 * SCTP_BINDX_REM_ADDR
726 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
727 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
728 * addresses from the association. The two flags are mutually exclusive;
729 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
730 * not remove all addresses from an association; sctp_bindx() will
731 * reject such an attempt with EINVAL.
733 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
734 * additional addresses with an endpoint after calling bind(). Or use
735 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
736 * socket is associated with so that no new association accepted will be
737 * associated with those addresses. If the endpoint supports dynamic
738 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
739 * endpoint to send the appropriate message to the peer to change the
740 * peers address lists.
742 * Adding and removing addresses from a connected association is
743 * optional functionality. Implementations that do not support this
744 * functionality should return EOPNOTSUPP.
746 * Basically do nothing but copying the addresses from user to kernel
747 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
748 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
750 * We don't use copy_from_user() for optimization: we first do the
751 * sanity checks (buffer size -fast- and access check-healthy
752 * pointer); if all of those succeed, then we can alloc the memory
753 * (expensive operation) needed to copy the data to kernel. Then we do
754 * the copying without checking the user space area
755 * (__copy_from_user()).
757 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
760 * sk The sk of the socket
761 * addrs The pointer to the addresses in user land
762 * addrssize Size of the addrs buffer
763 * op Operation to perform (add or remove, see the flags of
766 * Returns 0 if ok, <0 errno code on error.
768 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
769 struct sockaddr __user *addrs,
770 int addrs_size, int op)
772 struct sockaddr *kaddrs;
776 struct sockaddr *sa_addr;
780 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
781 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
783 if (unlikely(addrs_size <= 0))
786 /* Check the user passed a healthy pointer. */
787 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
790 /* Alloc space for the address array in kernel memory. */
791 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
792 if (unlikely(!kaddrs))
795 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
800 /* Walk through the addrs buffer and count the number of addresses. */
802 while (walk_size < addrs_size) {
803 sa_addr = (struct sockaddr *)addr_buf;
804 af = sctp_get_af_specific(sa_addr->sa_family);
806 /* If the address family is not supported or if this address
807 * causes the address buffer to overflow return EINVAL.
809 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
814 addr_buf += af->sockaddr_len;
815 walk_size += af->sockaddr_len;
820 case SCTP_BINDX_ADD_ADDR:
821 err = sctp_bindx_add(sk, kaddrs, addrcnt);
824 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
827 case SCTP_BINDX_REM_ADDR:
828 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
831 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
845 /* API 3.1.4 close() - UDP Style Syntax
846 * Applications use close() to perform graceful shutdown (as described in
847 * Section 10.1 of [SCTP]) on ALL the associations currently represented
848 * by a UDP-style socket.
852 * ret = close(int sd);
854 * sd - the socket descriptor of the associations to be closed.
856 * To gracefully shutdown a specific association represented by the
857 * UDP-style socket, an application should use the sendmsg() call,
858 * passing no user data, but including the appropriate flag in the
859 * ancillary data (see Section xxxx).
861 * If sd in the close() call is a branched-off socket representing only
862 * one association, the shutdown is performed on that association only.
864 * 4.1.6 close() - TCP Style Syntax
866 * Applications use close() to gracefully close down an association.
872 * sd - the socket descriptor of the association to be closed.
874 * After an application calls close() on a socket descriptor, no further
875 * socket operations will succeed on that descriptor.
877 * API 7.1.4 SO_LINGER
879 * An application using the TCP-style socket can use this option to
880 * perform the SCTP ABORT primitive. The linger option structure is:
883 * int l_onoff; // option on/off
884 * int l_linger; // linger time
887 * To enable the option, set l_onoff to 1. If the l_linger value is set
888 * to 0, calling close() is the same as the ABORT primitive. If the
889 * value is set to a negative value, the setsockopt() call will return
890 * an error. If the value is set to a positive value linger_time, the
891 * close() can be blocked for at most linger_time ms. If the graceful
892 * shutdown phase does not finish during this period, close() will
893 * return but the graceful shutdown phase continues in the system.
895 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
897 struct sctp_endpoint *ep;
898 struct sctp_association *asoc;
899 struct list_head *pos, *temp;
901 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
904 sk->sk_shutdown = SHUTDOWN_MASK;
906 ep = sctp_sk(sk)->ep;
908 /* Walk all associations on a socket, not on an endpoint. */
909 list_for_each_safe(pos, temp, &ep->asocs) {
910 asoc = list_entry(pos, struct sctp_association, asocs);
912 if (sctp_style(sk, TCP)) {
913 /* A closed association can still be in the list if
914 * it belongs to a TCP-style listening socket that is
915 * not yet accepted. If so, free it. If not, send an
916 * ABORT or SHUTDOWN based on the linger options.
918 if (sctp_state(asoc, CLOSED)) {
919 sctp_unhash_established(asoc);
920 sctp_association_free(asoc);
922 } else if (sock_flag(sk, SOCK_LINGER) &&
924 sctp_primitive_ABORT(asoc, NULL);
926 sctp_primitive_SHUTDOWN(asoc, NULL);
928 sctp_primitive_SHUTDOWN(asoc, NULL);
931 /* Clean up any skbs sitting on the receive queue. */
932 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
933 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
935 /* On a TCP-style socket, block for at most linger_time if set. */
936 if (sctp_style(sk, TCP) && timeout)
937 sctp_wait_for_close(sk, timeout);
939 /* This will run the backlog queue. */
940 sctp_release_sock(sk);
942 /* Supposedly, no process has access to the socket, but
943 * the net layers still may.
945 sctp_local_bh_disable();
946 sctp_bh_lock_sock(sk);
948 /* Hold the sock, since inet_sock_release() will put sock_put()
949 * and we have just a little more cleanup.
952 inet_sock_release(sk);
954 sctp_bh_unlock_sock(sk);
955 sctp_local_bh_enable();
959 SCTP_DBG_OBJCNT_DEC(sock);
962 /* Handle EPIPE error. */
963 static int sctp_error(struct sock *sk, int flags, int err)
966 err = sock_error(sk) ? : -EPIPE;
967 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
968 send_sig(SIGPIPE, current, 0);
972 /* API 3.1.3 sendmsg() - UDP Style Syntax
974 * An application uses sendmsg() and recvmsg() calls to transmit data to
975 * and receive data from its peer.
977 * ssize_t sendmsg(int socket, const struct msghdr *message,
980 * socket - the socket descriptor of the endpoint.
981 * message - pointer to the msghdr structure which contains a single
982 * user message and possibly some ancillary data.
984 * See Section 5 for complete description of the data
987 * flags - flags sent or received with the user message, see Section
988 * 5 for complete description of the flags.
990 * Note: This function could use a rewrite especially when explicit
991 * connect support comes in.
993 /* BUG: We do not implement the equivalent of wait_for_tcp_memory(). */
995 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
997 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
998 struct msghdr *msg, size_t msg_len)
1000 struct sctp_opt *sp;
1001 struct sctp_endpoint *ep;
1002 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1003 struct sctp_transport *transport, *chunk_tp;
1004 struct sctp_chunk *chunk;
1006 struct sockaddr *msg_name = NULL;
1007 struct sctp_sndrcvinfo default_sinfo = { 0 };
1008 struct sctp_sndrcvinfo *sinfo;
1009 struct sctp_initmsg *sinit;
1010 sctp_assoc_t associd = NULL;
1011 sctp_cmsgs_t cmsgs = { 0 };
1015 __u16 sinfo_flags = 0;
1016 struct sctp_datamsg *datamsg;
1017 struct list_head *pos;
1018 int msg_flags = msg->msg_flags;
1020 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1027 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1029 /* We cannot send a message over a TCP-style listening socket. */
1030 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1035 /* Parse out the SCTP CMSGs. */
1036 err = sctp_msghdr_parse(msg, &cmsgs);
1039 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1043 /* Fetch the destination address for this packet. This
1044 * address only selects the association--it is not necessarily
1045 * the address we will send to.
1046 * For a peeled-off socket, msg_name is ignored.
1048 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1049 int msg_namelen = msg->msg_namelen;
1051 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1056 if (msg_namelen > sizeof(to))
1057 msg_namelen = sizeof(to);
1058 memcpy(&to, msg->msg_name, msg_namelen);
1059 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1061 to.v4.sin_addr.s_addr, to.v4.sin_port);
1063 to.v4.sin_port = ntohs(to.v4.sin_port);
1064 msg_name = msg->msg_name;
1070 /* Did the user specify SNDRCVINFO? */
1072 sinfo_flags = sinfo->sinfo_flags;
1073 associd = sinfo->sinfo_assoc_id;
1076 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1077 msg_len, sinfo_flags);
1079 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1080 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1085 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1086 * length messages when MSG_EOF|MSG_ABORT is not set.
1087 * If MSG_ABORT is set, the message length could be non zero with
1088 * the msg_iov set to the user abort reason.
1090 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1091 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1096 /* If MSG_ADDR_OVER is set, there must be an address
1097 * specified in msg_name.
1099 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1106 SCTP_DEBUG_PRINTK("About to look up association.\n");
1110 /* If a msg_name has been specified, assume this is to be used. */
1112 /* Look for a matching association on the endpoint. */
1113 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1115 /* If we could not find a matching association on the
1116 * endpoint, make sure that it is not a TCP-style
1117 * socket that already has an association or there is
1118 * no peeled-off association on another socket.
1120 if ((sctp_style(sk, TCP) &&
1121 sctp_sstate(sk, ESTABLISHED)) ||
1122 sctp_endpoint_is_peeled_off(ep, &to)) {
1123 err = -EADDRNOTAVAIL;
1128 asoc = sctp_id2assoc(sk, associd);
1136 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1138 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1139 * socket that has an association in CLOSED state. This can
1140 * happen when an accepted socket has an association that is
1143 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1148 if (sinfo_flags & MSG_EOF) {
1149 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1151 sctp_primitive_SHUTDOWN(asoc, NULL);
1155 if (sinfo_flags & MSG_ABORT) {
1156 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1157 sctp_primitive_ABORT(asoc, msg);
1163 /* Do we need to create the association? */
1165 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1167 /* Check for invalid stream against the stream counts,
1168 * either the default or the user specified stream counts.
1171 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1172 /* Check against the defaults. */
1173 if (sinfo->sinfo_stream >=
1174 sp->initmsg.sinit_num_ostreams) {
1179 /* Check against the requested. */
1180 if (sinfo->sinfo_stream >=
1181 sinit->sinit_num_ostreams) {
1189 * API 3.1.2 bind() - UDP Style Syntax
1190 * If a bind() or sctp_bindx() is not called prior to a
1191 * sendmsg() call that initiates a new association, the
1192 * system picks an ephemeral port and will choose an address
1193 * set equivalent to binding with a wildcard address.
1195 if (!ep->base.bind_addr.port) {
1196 if (sctp_autobind(sk)) {
1202 scope = sctp_scope(&to);
1203 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1210 /* If the SCTP_INIT ancillary data is specified, set all
1211 * the association init values accordingly.
1214 if (sinit->sinit_num_ostreams) {
1215 asoc->c.sinit_num_ostreams =
1216 sinit->sinit_num_ostreams;
1218 if (sinit->sinit_max_instreams) {
1219 asoc->c.sinit_max_instreams =
1220 sinit->sinit_max_instreams;
1222 if (sinit->sinit_max_attempts) {
1223 asoc->max_init_attempts
1224 = sinit->sinit_max_attempts;
1226 if (sinit->sinit_max_init_timeo) {
1227 asoc->max_init_timeo =
1228 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1232 /* Prime the peer's transport structures. */
1233 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1238 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1245 /* ASSERT: we have a valid association at this point. */
1246 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1249 /* If the user didn't specify SNDRCVINFO, make up one with
1252 default_sinfo.sinfo_stream = asoc->default_stream;
1253 default_sinfo.sinfo_flags = asoc->default_flags;
1254 default_sinfo.sinfo_ppid = asoc->default_ppid;
1255 default_sinfo.sinfo_context = asoc->default_context;
1256 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1257 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1258 sinfo = &default_sinfo;
1261 /* API 7.1.7, the sndbuf size per association bounds the
1262 * maximum size of data that can be sent in a single send call.
1264 if (msg_len > sk->sk_sndbuf) {
1269 /* If fragmentation is disabled and the message length exceeds the
1270 * association fragmentation point, return EMSGSIZE. The I-D
1271 * does not specify what this error is, but this looks like
1274 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1280 /* Check for invalid stream. */
1281 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1287 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1288 if (!sctp_wspace(asoc)) {
1289 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1294 /* If an address is passed with the sendto/sendmsg call, it is used
1295 * to override the primary destination address in the TCP model, or
1296 * when MSG_ADDR_OVER flag is set in the UDP model.
1298 if ((sctp_style(sk, TCP) && msg_name) ||
1299 (sinfo_flags & MSG_ADDR_OVER)) {
1300 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1308 /* Auto-connect, if we aren't connected already. */
1309 if (sctp_state(asoc, CLOSED)) {
1310 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1313 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1316 /* Break the message into multiple chunks of maximum size. */
1317 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1323 /* Now send the (possibly) fragmented message. */
1324 list_for_each(pos, &datamsg->chunks) {
1325 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1326 sctp_datamsg_track(chunk);
1328 /* Do accounting for the write space. */
1329 sctp_set_owner_w(chunk);
1331 chunk->transport = chunk_tp;
1333 /* Send it to the lower layers. Note: all chunks
1334 * must either fail or succeed. The lower layer
1335 * works that way today. Keep it that way or this
1338 err = sctp_primitive_SEND(asoc, chunk);
1339 /* Did the lower layer accept the chunk? */
1341 sctp_chunk_free(chunk);
1342 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1345 sctp_datamsg_free(datamsg);
1351 /* If we are already past ASSOCIATE, the lower
1352 * layers are responsible for association cleanup.
1358 sctp_association_free(asoc);
1360 sctp_release_sock(sk);
1363 return sctp_error(sk, msg_flags, err);
1370 err = sock_error(sk);
1380 /* This is an extended version of skb_pull() that removes the data from the
1381 * start of a skb even when data is spread across the list of skb's in the
1382 * frag_list. len specifies the total amount of data that needs to be removed.
1383 * when 'len' bytes could be removed from the skb, it returns 0.
1384 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1385 * could not be removed.
1387 static int sctp_skb_pull(struct sk_buff *skb, int len)
1389 struct sk_buff *list;
1390 int skb_len = skb_headlen(skb);
1393 if (len <= skb_len) {
1394 __skb_pull(skb, len);
1398 __skb_pull(skb, skb_len);
1400 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1401 rlen = sctp_skb_pull(list, len);
1402 skb->len -= (len-rlen);
1403 skb->data_len -= (len-rlen);
1414 /* API 3.1.3 recvmsg() - UDP Style Syntax
1416 * ssize_t recvmsg(int socket, struct msghdr *message,
1419 * socket - the socket descriptor of the endpoint.
1420 * message - pointer to the msghdr structure which contains a single
1421 * user message and possibly some ancillary data.
1423 * See Section 5 for complete description of the data
1426 * flags - flags sent or received with the user message, see Section
1427 * 5 for complete description of the flags.
1429 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1431 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1432 struct msghdr *msg, size_t len, int noblock,
1433 int flags, int *addr_len)
1435 struct sctp_ulpevent *event = NULL;
1436 struct sctp_opt *sp = sctp_sk(sk);
1437 struct sk_buff *skb;
1442 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1443 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1444 "len", len, "knoblauch", noblock,
1445 "flags", flags, "addr_len", addr_len);
1449 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1454 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1458 /* Get the total length of the skb including any skb's in the
1467 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1469 event = sctp_skb2event(skb);
1474 sock_recv_timestamp(msg, sk, skb);
1475 if (sctp_ulpevent_is_notification(event)) {
1476 msg->msg_flags |= MSG_NOTIFICATION;
1477 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1479 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1482 /* Check if we allow SCTP_SNDRCVINFO. */
1483 if (sp->subscribe.sctp_data_io_event)
1484 sctp_ulpevent_read_sndrcvinfo(event, msg);
1486 /* FIXME: we should be calling IP/IPv6 layers. */
1487 if (sk->sk_protinfo.af_inet.cmsg_flags)
1488 ip_cmsg_recv(msg, skb);
1493 /* If skb's length exceeds the user's buffer, update the skb and
1494 * push it back to the receive_queue so that the next call to
1495 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1497 if (skb_len > copied) {
1498 msg->msg_flags &= ~MSG_EOR;
1499 if (flags & MSG_PEEK)
1501 sctp_skb_pull(skb, copied);
1502 skb_queue_head(&sk->sk_receive_queue, skb);
1504 /* When only partial message is copied to the user, increase
1505 * rwnd by that amount. If all the data in the skb is read,
1506 * rwnd is updated when the event is freed.
1508 sctp_assoc_rwnd_increase(event->asoc, copied);
1510 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1511 (event->msg_flags & MSG_EOR))
1512 msg->msg_flags |= MSG_EOR;
1514 msg->msg_flags &= ~MSG_EOR;
1517 if (flags & MSG_PEEK) {
1518 /* Release the skb reference acquired after peeking the skb in
1519 * sctp_skb_recv_datagram().
1523 /* Free the event which includes releasing the reference to
1524 * the owner of the skb, freeing the skb and updating the
1527 sctp_ulpevent_free(event);
1530 sctp_release_sock(sk);
1534 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1536 * This option is a on/off flag. If enabled no SCTP message
1537 * fragmentation will be performed. Instead if a message being sent
1538 * exceeds the current PMTU size, the message will NOT be sent and
1539 * instead a error will be indicated to the user.
1541 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1542 char __user *optval, int optlen)
1546 if (optlen < sizeof(int))
1549 if (get_user(val, (int __user *)optval))
1552 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1557 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1560 if (optlen != sizeof(struct sctp_event_subscribe))
1562 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1567 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1569 * This socket option is applicable to the UDP-style socket only. When
1570 * set it will cause associations that are idle for more than the
1571 * specified number of seconds to automatically close. An association
1572 * being idle is defined an association that has NOT sent or received
1573 * user data. The special value of '0' indicates that no automatic
1574 * close of any associations should be performed. The option expects an
1575 * integer defining the number of seconds of idle time before an
1576 * association is closed.
1578 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1581 struct sctp_opt *sp = sctp_sk(sk);
1583 /* Applicable to UDP-style socket only */
1584 if (sctp_style(sk, TCP))
1586 if (optlen != sizeof(int))
1588 if (copy_from_user(&sp->autoclose, optval, optlen))
1591 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1595 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1597 * Applications can enable or disable heartbeats for any peer address of
1598 * an association, modify an address's heartbeat interval, force a
1599 * heartbeat to be sent immediately, and adjust the address's maximum
1600 * number of retransmissions sent before an address is considered
1601 * unreachable. The following structure is used to access and modify an
1602 * address's parameters:
1604 * struct sctp_paddrparams {
1605 * sctp_assoc_t spp_assoc_id;
1606 * struct sockaddr_storage spp_address;
1607 * uint32_t spp_hbinterval;
1608 * uint16_t spp_pathmaxrxt;
1611 * spp_assoc_id - (UDP style socket) This is filled in the application,
1612 * and identifies the association for this query.
1613 * spp_address - This specifies which address is of interest.
1614 * spp_hbinterval - This contains the value of the heartbeat interval,
1615 * in milliseconds. A value of 0, when modifying the
1616 * parameter, specifies that the heartbeat on this
1617 * address should be disabled. A value of UINT32_MAX
1618 * (4294967295), when modifying the parameter,
1619 * specifies that a heartbeat should be sent
1620 * immediately to the peer address, and the current
1621 * interval should remain unchanged.
1622 * spp_pathmaxrxt - This contains the maximum number of
1623 * retransmissions before this address shall be
1624 * considered unreachable.
1626 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1627 char __user *optval, int optlen)
1629 struct sctp_paddrparams params;
1630 struct sctp_transport *trans;
1633 if (optlen != sizeof(struct sctp_paddrparams))
1635 if (copy_from_user(¶ms, optval, optlen))
1638 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
1639 params.spp_assoc_id);
1643 /* Applications can enable or disable heartbeats for any peer address
1644 * of an association, modify an address's heartbeat interval, force a
1645 * heartbeat to be sent immediately, and adjust the address's maximum
1646 * number of retransmissions sent before an address is considered
1649 * The value of the heartbeat interval, in milliseconds. A value of
1650 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1651 * that a heartbeat should be sent immediately to the peer address,
1652 * and the current interval should remain unchanged.
1654 if (0xffffffff == params.spp_hbinterval) {
1655 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1659 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1660 * when modifying the parameter, specifies that the heartbeat on this
1661 * address should be disabled.
1663 if (params.spp_hbinterval) {
1664 trans->hb_allowed = 1;
1665 trans->hb_interval =
1666 msecs_to_jiffies(params.spp_hbinterval);
1668 trans->hb_allowed = 0;
1671 /* spp_pathmaxrxt contains the maximum number of retransmissions
1672 * before this address shall be considered unreachable.
1674 trans->error_threshold = params.spp_pathmaxrxt;
1679 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1681 * Applications can specify protocol parameters for the default association
1682 * initialization. The option name argument to setsockopt() and getsockopt()
1685 * Setting initialization parameters is effective only on an unconnected
1686 * socket (for UDP-style sockets only future associations are effected
1687 * by the change). With TCP-style sockets, this option is inherited by
1688 * sockets derived from a listener socket.
1690 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1692 struct sctp_initmsg sinit;
1693 struct sctp_opt *sp = sctp_sk(sk);
1695 if (optlen != sizeof(struct sctp_initmsg))
1697 if (copy_from_user(&sinit, optval, optlen))
1700 if (sinit.sinit_num_ostreams)
1701 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1702 if (sinit.sinit_max_instreams)
1703 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1704 if (sinit.sinit_max_attempts)
1705 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1706 if (sinit.sinit_max_init_timeo)
1707 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1713 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1715 * Applications that wish to use the sendto() system call may wish to
1716 * specify a default set of parameters that would normally be supplied
1717 * through the inclusion of ancillary data. This socket option allows
1718 * such an application to set the default sctp_sndrcvinfo structure.
1719 * The application that wishes to use this socket option simply passes
1720 * in to this call the sctp_sndrcvinfo structure defined in Section
1721 * 5.2.2) The input parameters accepted by this call include
1722 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1723 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1724 * to this call if the caller is using the UDP model.
1726 static int sctp_setsockopt_default_send_param(struct sock *sk,
1727 char __user *optval, int optlen)
1729 struct sctp_sndrcvinfo info;
1730 struct sctp_association *asoc;
1731 struct sctp_opt *sp = sctp_sk(sk);
1733 if (optlen != sizeof(struct sctp_sndrcvinfo))
1735 if (copy_from_user(&info, optval, optlen))
1738 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1739 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1743 asoc->default_stream = info.sinfo_stream;
1744 asoc->default_flags = info.sinfo_flags;
1745 asoc->default_ppid = info.sinfo_ppid;
1746 asoc->default_context = info.sinfo_context;
1747 asoc->default_timetolive = info.sinfo_timetolive;
1749 sp->default_stream = info.sinfo_stream;
1750 sp->default_flags = info.sinfo_flags;
1751 sp->default_ppid = info.sinfo_ppid;
1752 sp->default_context = info.sinfo_context;
1753 sp->default_timetolive = info.sinfo_timetolive;
1759 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1761 * Requests that the local SCTP stack use the enclosed peer address as
1762 * the association primary. The enclosed address must be one of the
1763 * association peer's addresses.
1765 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1768 struct sctp_prim prim;
1769 struct sctp_transport *trans;
1771 if (optlen != sizeof(struct sctp_prim))
1774 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1777 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1781 sctp_assoc_set_primary(trans->asoc, trans);
1787 * 7.1.5 SCTP_NODELAY
1789 * Turn on/off any Nagle-like algorithm. This means that packets are
1790 * generally sent as soon as possible and no unnecessary delays are
1791 * introduced, at the cost of more packets in the network. Expects an
1792 * integer boolean flag.
1794 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1799 if (optlen < sizeof(int))
1801 if (get_user(val, (int __user *)optval))
1804 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1810 * 7.1.1 SCTP_RTOINFO
1812 * The protocol parameters used to initialize and bound retransmission
1813 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1814 * and modify these parameters.
1815 * All parameters are time values, in milliseconds. A value of 0, when
1816 * modifying the parameters, indicates that the current value should not
1820 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1821 struct sctp_rtoinfo rtoinfo;
1822 struct sctp_association *asoc;
1824 if (optlen != sizeof (struct sctp_rtoinfo))
1827 if (copy_from_user(&rtoinfo, optval, optlen))
1830 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1832 /* Set the values to the specific association */
1833 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1837 if (rtoinfo.srto_initial != 0)
1839 msecs_to_jiffies(rtoinfo.srto_initial);
1840 if (rtoinfo.srto_max != 0)
1841 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1842 if (rtoinfo.srto_min != 0)
1843 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1845 /* If there is no association or the association-id = 0
1846 * set the values to the endpoint.
1848 struct sctp_opt *sp = sctp_sk(sk);
1850 if (rtoinfo.srto_initial != 0)
1851 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1852 if (rtoinfo.srto_max != 0)
1853 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1854 if (rtoinfo.srto_min != 0)
1855 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1863 * 7.1.2 SCTP_ASSOCINFO
1865 * This option is used to tune the the maximum retransmission attempts
1866 * of the association.
1867 * Returns an error if the new association retransmission value is
1868 * greater than the sum of the retransmission value of the peer.
1869 * See [SCTP] for more information.
1872 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1875 struct sctp_assocparams assocparams;
1876 struct sctp_association *asoc;
1878 if (optlen != sizeof(struct sctp_assocparams))
1880 if (copy_from_user(&assocparams, optval, optlen))
1883 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1885 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1888 /* Set the values to the specific association */
1890 if (assocparams.sasoc_asocmaxrxt != 0)
1891 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1892 if (assocparams.sasoc_cookie_life != 0) {
1893 asoc->cookie_life.tv_sec =
1894 assocparams.sasoc_cookie_life / 1000;
1895 asoc->cookie_life.tv_usec =
1896 (assocparams.sasoc_cookie_life % 1000)
1900 /* Set the values to the endpoint */
1901 struct sctp_opt *sp = sctp_sk(sk);
1903 if (assocparams.sasoc_asocmaxrxt != 0)
1904 sp->assocparams.sasoc_asocmaxrxt =
1905 assocparams.sasoc_asocmaxrxt;
1906 if (assocparams.sasoc_cookie_life != 0)
1907 sp->assocparams.sasoc_cookie_life =
1908 assocparams.sasoc_cookie_life;
1914 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
1916 * This socket option is a boolean flag which turns on or off mapped V4
1917 * addresses. If this option is turned on and the socket is type
1918 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
1919 * If this option is turned off, then no mapping will be done of V4
1920 * addresses and a user will receive both PF_INET6 and PF_INET type
1921 * addresses on the socket.
1923 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
1926 struct sctp_opt *sp = sctp_sk(sk);
1928 if (optlen < sizeof(int))
1930 if (get_user(val, (int __user *)optval))
1941 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
1943 * This socket option specifies the maximum size to put in any outgoing
1944 * SCTP chunk. If a message is larger than this size it will be
1945 * fragmented by SCTP into the specified size. Note that the underlying
1946 * SCTP implementation may fragment into smaller sized chunks when the
1947 * PMTU of the underlying association is smaller than the value set by
1950 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
1952 struct sctp_association *asoc;
1953 struct list_head *pos;
1954 struct sctp_opt *sp = sctp_sk(sk);
1957 if (optlen < sizeof(int))
1959 if (get_user(val, (int __user *)optval))
1961 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
1963 sp->user_frag = val;
1966 /* Update the frag_point of the existing associations. */
1967 list_for_each(pos, &(sp->ep->asocs)) {
1968 asoc = list_entry(pos, struct sctp_association, asocs);
1969 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
1978 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
1980 * Requests that the peer mark the enclosed address as the association
1981 * primary. The enclosed address must be one of the association's
1982 * locally bound addresses. The following structure is used to make a
1983 * set primary request:
1985 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
1988 struct sctp_opt *sp;
1989 struct sctp_endpoint *ep;
1990 struct sctp_association *asoc = NULL;
1991 struct sctp_setpeerprim prim;
1992 struct sctp_chunk *chunk;
1998 if (!sctp_addip_enable)
2001 if (optlen != sizeof(struct sctp_setpeerprim))
2004 if (copy_from_user(&prim, optval, optlen))
2007 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2011 if (!asoc->peer.asconf_capable)
2014 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2017 if (!sctp_state(asoc, ESTABLISHED))
2020 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2021 return -EADDRNOTAVAIL;
2023 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2024 chunk = sctp_make_asconf_set_prim(asoc,
2025 (union sctp_addr *)&prim.sspp_addr);
2029 err = sctp_send_asconf(asoc, chunk);
2031 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2037 /* API 6.2 setsockopt(), getsockopt()
2039 * Applications use setsockopt() and getsockopt() to set or retrieve
2040 * socket options. Socket options are used to change the default
2041 * behavior of sockets calls. They are described in Section 7.
2045 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2046 * int __user *optlen);
2047 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2050 * sd - the socket descript.
2051 * level - set to IPPROTO_SCTP for all SCTP options.
2052 * optname - the option name.
2053 * optval - the buffer to store the value of the option.
2054 * optlen - the size of the buffer.
2056 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2057 char __user *optval, int optlen)
2061 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2064 /* I can hardly begin to describe how wrong this is. This is
2065 * so broken as to be worse than useless. The API draft
2066 * REALLY is NOT helpful here... I am not convinced that the
2067 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2068 * are at all well-founded.
2070 if (level != SOL_SCTP) {
2071 struct sctp_af *af = sctp_sk(sk)->pf->af;
2072 retval = af->setsockopt(sk, level, optname, optval, optlen);
2079 case SCTP_SOCKOPT_BINDX_ADD:
2080 /* 'optlen' is the size of the addresses buffer. */
2081 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2082 optlen, SCTP_BINDX_ADD_ADDR);
2085 case SCTP_SOCKOPT_BINDX_REM:
2086 /* 'optlen' is the size of the addresses buffer. */
2087 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2088 optlen, SCTP_BINDX_REM_ADDR);
2091 case SCTP_DISABLE_FRAGMENTS:
2092 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2096 retval = sctp_setsockopt_events(sk, optval, optlen);
2099 case SCTP_AUTOCLOSE:
2100 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2103 case SCTP_PEER_ADDR_PARAMS:
2104 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2108 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2110 case SCTP_DEFAULT_SEND_PARAM:
2111 retval = sctp_setsockopt_default_send_param(sk, optval,
2114 case SCTP_PRIMARY_ADDR:
2115 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2117 case SCTP_SET_PEER_PRIMARY_ADDR:
2118 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2121 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2124 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2126 case SCTP_ASSOCINFO:
2127 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2129 case SCTP_I_WANT_MAPPED_V4_ADDR:
2130 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2133 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2136 retval = -ENOPROTOOPT;
2140 sctp_release_sock(sk);
2146 /* API 3.1.6 connect() - UDP Style Syntax
2148 * An application may use the connect() call in the UDP model to initiate an
2149 * association without sending data.
2153 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2155 * sd: the socket descriptor to have a new association added to.
2157 * nam: the address structure (either struct sockaddr_in or struct
2158 * sockaddr_in6 defined in RFC2553 [7]).
2160 * len: the size of the address.
2162 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2165 struct sctp_opt *sp;
2166 struct sctp_endpoint *ep;
2167 struct sctp_association *asoc;
2168 struct sctp_transport *transport;
2177 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2178 __FUNCTION__, sk, uaddr, addr_len);
2183 /* connect() cannot be done on a socket that is already in ESTABLISHED
2184 * state - UDP-style peeled off socket or a TCP-style socket that
2185 * is already connected.
2186 * It cannot be done even on a TCP-style listening socket.
2188 if (sctp_sstate(sk, ESTABLISHED) ||
2189 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2194 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2198 if (addr_len > sizeof(to))
2199 addr_len = sizeof(to);
2200 memcpy(&to, uaddr, addr_len);
2201 to.v4.sin_port = ntohs(to.v4.sin_port);
2203 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2205 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2212 /* If we could not find a matching association on the endpoint,
2213 * make sure that there is no peeled-off association matching the
2214 * peer address even on another socket.
2216 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2217 err = -EADDRNOTAVAIL;
2221 /* If a bind() or sctp_bindx() is not called prior to a connect()
2222 * call, the system picks an ephemeral port and will choose an address
2223 * set equivalent to binding with a wildcard address.
2225 if (!ep->base.bind_addr.port) {
2226 if (sctp_autobind(sk)) {
2232 scope = sctp_scope(&to);
2233 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2239 /* Prime the peer's transport structures. */
2240 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2242 sctp_association_free(asoc);
2245 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2247 sctp_association_free(asoc);
2251 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2253 sctp_association_free(asoc);
2257 /* Initialize sk's dport and daddr for getpeername() */
2258 inet_sk(sk)->dport = htons(asoc->peer.port);
2259 af = sctp_get_af_specific(to.sa.sa_family);
2260 af->to_sk_daddr(&to, sk);
2262 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2263 err = sctp_wait_for_connect(asoc, &timeo);
2266 sctp_release_sock(sk);
2271 /* FIXME: Write comments. */
2272 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2274 return -EOPNOTSUPP; /* STUB */
2277 /* 4.1.4 accept() - TCP Style Syntax
2279 * Applications use accept() call to remove an established SCTP
2280 * association from the accept queue of the endpoint. A new socket
2281 * descriptor will be returned from accept() to represent the newly
2282 * formed association.
2284 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2286 struct sctp_opt *sp;
2287 struct sctp_endpoint *ep;
2288 struct sock *newsk = NULL;
2289 struct sctp_association *asoc;
2298 if (!sctp_style(sk, TCP)) {
2299 error = -EOPNOTSUPP;
2303 if (!sctp_sstate(sk, LISTENING)) {
2308 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2310 error = sctp_wait_for_accept(sk, timeo);
2314 /* We treat the list of associations on the endpoint as the accept
2315 * queue and pick the first association on the list.
2317 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2319 newsk = sp->pf->create_accept_sk(sk, asoc);
2325 /* Populate the fields of the newsk from the oldsk and migrate the
2326 * asoc to the newsk.
2328 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2331 sctp_release_sock(sk);
2336 /* The SCTP ioctl handler. */
2337 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2339 return -ENOIOCTLCMD;
2342 /* This is the function which gets called during socket creation to
2343 * initialized the SCTP-specific portion of the sock.
2344 * The sock structure should already be zero-filled memory.
2346 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2348 struct sctp_endpoint *ep;
2349 struct sctp_opt *sp;
2351 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2355 /* Initialize the SCTP per socket area. */
2356 switch (sk->sk_type) {
2357 case SOCK_SEQPACKET:
2358 sp->type = SCTP_SOCKET_UDP;
2361 sp->type = SCTP_SOCKET_TCP;
2364 return -ESOCKTNOSUPPORT;
2367 /* Initialize default send parameters. These parameters can be
2368 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2370 sp->default_stream = 0;
2371 sp->default_ppid = 0;
2372 sp->default_flags = 0;
2373 sp->default_context = 0;
2374 sp->default_timetolive = 0;
2376 /* Initialize default setup parameters. These parameters
2377 * can be modified with the SCTP_INITMSG socket option or
2378 * overridden by the SCTP_INIT CMSG.
2380 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2381 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2382 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2383 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2385 /* Initialize default RTO related parameters. These parameters can
2386 * be modified for with the SCTP_RTOINFO socket option.
2388 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2389 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2390 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2392 /* Initialize default association related parameters. These parameters
2393 * can be modified with the SCTP_ASSOCINFO socket option.
2395 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2396 sp->assocparams.sasoc_number_peer_destinations = 0;
2397 sp->assocparams.sasoc_peer_rwnd = 0;
2398 sp->assocparams.sasoc_local_rwnd = 0;
2399 sp->assocparams.sasoc_cookie_life =
2400 jiffies_to_msecs(sctp_valid_cookie_life);
2402 /* Initialize default event subscriptions. By default, all the
2405 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2407 /* Default Peer Address Parameters. These defaults can
2408 * be modified via SCTP_PEER_ADDR_PARAMS
2410 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2411 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2413 /* If enabled no SCTP message fragmentation will be performed.
2414 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2416 sp->disable_fragments = 0;
2418 /* Turn on/off any Nagle-like algorithm. */
2421 /* Enable by default. */
2424 /* Auto-close idle associations after the configured
2425 * number of seconds. A value of 0 disables this
2426 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2427 * for UDP-style sockets only.
2431 /* User specified fragmentation limit. */
2434 sp->pf = sctp_get_pf_specific(sk->sk_family);
2436 /* Control variables for partial data delivery. */
2438 skb_queue_head_init(&sp->pd_lobby);
2440 /* Create a per socket endpoint structure. Even if we
2441 * change the data structure relationships, this may still
2442 * be useful for storing pre-connect address information.
2444 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2451 SCTP_DBG_OBJCNT_INC(sock);
2455 /* Cleanup any SCTP per socket resources. */
2456 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2458 struct sctp_endpoint *ep;
2460 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2462 /* Release our hold on the endpoint. */
2463 ep = sctp_sk(sk)->ep;
2464 sctp_endpoint_free(ep);
2469 /* API 4.1.7 shutdown() - TCP Style Syntax
2470 * int shutdown(int socket, int how);
2472 * sd - the socket descriptor of the association to be closed.
2473 * how - Specifies the type of shutdown. The values are
2476 * Disables further receive operations. No SCTP
2477 * protocol action is taken.
2479 * Disables further send operations, and initiates
2480 * the SCTP shutdown sequence.
2482 * Disables further send and receive operations
2483 * and initiates the SCTP shutdown sequence.
2485 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2487 struct sctp_endpoint *ep;
2488 struct sctp_association *asoc;
2490 if (!sctp_style(sk, TCP))
2493 if (how & SEND_SHUTDOWN) {
2494 ep = sctp_sk(sk)->ep;
2495 if (!list_empty(&ep->asocs)) {
2496 asoc = list_entry(ep->asocs.next,
2497 struct sctp_association, asocs);
2498 sctp_primitive_SHUTDOWN(asoc, NULL);
2503 /* 7.2.1 Association Status (SCTP_STATUS)
2505 * Applications can retrieve current status information about an
2506 * association, including association state, peer receiver window size,
2507 * number of unacked data chunks, and number of data chunks pending
2508 * receipt. This information is read-only.
2510 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2511 char __user *optval,
2514 struct sctp_status status;
2515 struct sctp_association *asoc = NULL;
2516 struct sctp_transport *transport;
2517 sctp_assoc_t associd;
2520 if (len != sizeof(status)) {
2525 if (copy_from_user(&status, optval, sizeof(status))) {
2530 associd = status.sstat_assoc_id;
2531 asoc = sctp_id2assoc(sk, associd);
2537 transport = asoc->peer.primary_path;
2539 status.sstat_assoc_id = sctp_assoc2id(asoc);
2540 status.sstat_state = asoc->state;
2541 status.sstat_rwnd = asoc->peer.rwnd;
2542 status.sstat_unackdata = asoc->unack_data;
2544 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2545 status.sstat_instrms = asoc->c.sinit_max_instreams;
2546 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2547 status.sstat_fragmentation_point = asoc->frag_point;
2548 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2549 memcpy(&status.sstat_primary.spinfo_address,
2550 &(transport->ipaddr), sizeof(union sctp_addr));
2551 /* Map ipv4 address into v4-mapped-on-v6 address. */
2552 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2553 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2554 status.sstat_primary.spinfo_state = transport->active;
2555 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2556 status.sstat_primary.spinfo_srtt = transport->srtt;
2557 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2558 status.sstat_primary.spinfo_mtu = transport->pmtu;
2560 if (put_user(len, optlen)) {
2565 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %p\n",
2566 len, status.sstat_state, status.sstat_rwnd,
2567 status.sstat_assoc_id);
2569 if (copy_to_user(optval, &status, len)) {
2579 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2581 * Applications can retrieve information about a specific peer address
2582 * of an association, including its reachability state, congestion
2583 * window, and retransmission timer values. This information is
2586 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2587 char __user *optval,
2590 struct sctp_paddrinfo pinfo;
2591 struct sctp_transport *transport;
2594 if (len != sizeof(pinfo)) {
2599 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2604 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2605 pinfo.spinfo_assoc_id);
2609 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2610 pinfo.spinfo_state = transport->active;
2611 pinfo.spinfo_cwnd = transport->cwnd;
2612 pinfo.spinfo_srtt = transport->srtt;
2613 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2614 pinfo.spinfo_mtu = transport->pmtu;
2616 if (put_user(len, optlen)) {
2621 if (copy_to_user(optval, &pinfo, len)) {
2630 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2632 * This option is a on/off flag. If enabled no SCTP message
2633 * fragmentation will be performed. Instead if a message being sent
2634 * exceeds the current PMTU size, the message will NOT be sent and
2635 * instead a error will be indicated to the user.
2637 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2638 char __user *optval, int __user *optlen)
2642 if (len < sizeof(int))
2646 val = (sctp_sk(sk)->disable_fragments == 1);
2647 if (put_user(len, optlen))
2649 if (copy_to_user(optval, &val, len))
2654 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2656 * This socket option is used to specify various notifications and
2657 * ancillary data the user wishes to receive.
2659 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2662 if (len != sizeof(struct sctp_event_subscribe))
2664 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2669 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2671 * This socket option is applicable to the UDP-style socket only. When
2672 * set it will cause associations that are idle for more than the
2673 * specified number of seconds to automatically close. An association
2674 * being idle is defined an association that has NOT sent or received
2675 * user data. The special value of '0' indicates that no automatic
2676 * close of any associations should be performed. The option expects an
2677 * integer defining the number of seconds of idle time before an
2678 * association is closed.
2680 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2682 /* Applicable to UDP-style socket only */
2683 if (sctp_style(sk, TCP))
2685 if (len != sizeof(int))
2687 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2692 /* Helper routine to branch off an association to a new socket. */
2693 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2694 struct socket **sockp)
2696 struct sock *sk = asoc->base.sk;
2697 struct socket *sock;
2700 /* An association cannot be branched off from an already peeled-off
2701 * socket, nor is this supported for tcp style sockets.
2703 if (!sctp_style(sk, UDP))
2706 /* Create a new socket. */
2707 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2711 /* Populate the fields of the newsk from the oldsk and migrate the
2712 * asoc to the newsk.
2714 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2720 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2722 sctp_peeloff_arg_t peeloff;
2723 struct socket *newsock;
2725 struct sctp_association *asoc;
2727 if (len != sizeof(sctp_peeloff_arg_t))
2729 if (copy_from_user(&peeloff, optval, len))
2732 asoc = sctp_id2assoc(sk, peeloff.associd);
2738 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2740 retval = sctp_do_peeloff(asoc, &newsock);
2744 /* Map the socket to an unused fd that can be returned to the user. */
2745 retval = sock_map_fd(newsock);
2747 sock_release(newsock);
2751 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2752 __FUNCTION__, sk, asoc, newsock->sk, retval);
2754 /* Return the fd mapped to the new socket. */
2755 peeloff.sd = retval;
2756 if (copy_to_user(optval, &peeloff, len))
2763 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2765 * Applications can enable or disable heartbeats for any peer address of
2766 * an association, modify an address's heartbeat interval, force a
2767 * heartbeat to be sent immediately, and adjust the address's maximum
2768 * number of retransmissions sent before an address is considered
2769 * unreachable. The following structure is used to access and modify an
2770 * address's parameters:
2772 * struct sctp_paddrparams {
2773 * sctp_assoc_t spp_assoc_id;
2774 * struct sockaddr_storage spp_address;
2775 * uint32_t spp_hbinterval;
2776 * uint16_t spp_pathmaxrxt;
2779 * spp_assoc_id - (UDP style socket) This is filled in the application,
2780 * and identifies the association for this query.
2781 * spp_address - This specifies which address is of interest.
2782 * spp_hbinterval - This contains the value of the heartbeat interval,
2783 * in milliseconds. A value of 0, when modifying the
2784 * parameter, specifies that the heartbeat on this
2785 * address should be disabled. A value of UINT32_MAX
2786 * (4294967295), when modifying the parameter,
2787 * specifies that a heartbeat should be sent
2788 * immediately to the peer address, and the current
2789 * interval should remain unchanged.
2790 * spp_pathmaxrxt - This contains the maximum number of
2791 * retransmissions before this address shall be
2792 * considered unreachable.
2794 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2795 char __user *optval, int __user *optlen)
2797 struct sctp_paddrparams params;
2798 struct sctp_transport *trans;
2800 if (len != sizeof(struct sctp_paddrparams))
2802 if (copy_from_user(¶ms, optval, *optlen)) /* XXXXXX */
2805 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2806 params.spp_assoc_id);
2810 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2811 * when modifying the parameter, specifies that the heartbeat on this
2812 * address should be disabled.
2814 if (!trans->hb_allowed)
2815 params.spp_hbinterval = 0;
2817 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2819 /* spp_pathmaxrxt contains the maximum number of retransmissions
2820 * before this address shall be considered unreachable.
2822 params.spp_pathmaxrxt = trans->error_threshold;
2824 if (copy_to_user(optval, ¶ms, len))
2827 if (put_user(len, optlen))
2833 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2835 * Applications can specify protocol parameters for the default association
2836 * initialization. The option name argument to setsockopt() and getsockopt()
2839 * Setting initialization parameters is effective only on an unconnected
2840 * socket (for UDP-style sockets only future associations are effected
2841 * by the change). With TCP-style sockets, this option is inherited by
2842 * sockets derived from a listener socket.
2844 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2846 if (len != sizeof(struct sctp_initmsg))
2848 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2853 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2854 char __user *optval, int __user *optlen)
2857 struct sctp_association *asoc;
2858 struct list_head *pos;
2861 if (len != sizeof(sctp_assoc_t))
2864 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2867 /* For UDP-style sockets, id specifies the association to query. */
2868 asoc = sctp_id2assoc(sk, id);
2872 list_for_each(pos, &asoc->peer.transport_addr_list) {
2879 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
2880 char __user *optval, int __user *optlen)
2882 struct sctp_association *asoc;
2883 struct list_head *pos;
2885 struct sctp_getaddrs getaddrs;
2886 struct sctp_transport *from;
2888 union sctp_addr temp;
2889 struct sctp_opt *sp = sctp_sk(sk);
2892 if (len != sizeof(struct sctp_getaddrs))
2895 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
2898 if (getaddrs.addr_num <= 0) return -EINVAL;
2900 /* For UDP-style sockets, id specifies the association to query. */
2901 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
2905 to = (void __user *)getaddrs.addrs;
2906 list_for_each(pos, &asoc->peer.transport_addr_list) {
2907 from = list_entry(pos, struct sctp_transport, transports);
2908 memcpy(&temp, &from->ipaddr, sizeof(temp));
2909 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
2910 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
2911 temp.v4.sin_port = htons(temp.v4.sin_port);
2912 if (copy_to_user(to, &temp, addrlen))
2916 if (cnt >= getaddrs.addr_num) break;
2918 getaddrs.addr_num = cnt;
2919 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
2925 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
2926 char __user *optval,
2930 struct sctp_bind_addr *bp;
2931 struct sctp_association *asoc;
2932 struct list_head *pos;
2935 if (len != sizeof(sctp_assoc_t))
2938 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2942 * For UDP-style sockets, id specifies the association to query.
2943 * If the id field is set to the value '0' then the locally bound
2944 * addresses are returned without regard to any particular
2948 bp = &sctp_sk(sk)->ep->base.bind_addr;
2950 asoc = sctp_id2assoc(sk, id);
2953 bp = &asoc->base.bind_addr;
2956 list_for_each(pos, &bp->address_list) {
2963 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
2964 char __user *optval, int __user *optlen)
2966 struct sctp_bind_addr *bp;
2967 struct sctp_association *asoc;
2968 struct list_head *pos;
2970 struct sctp_getaddrs getaddrs;
2971 struct sctp_sockaddr_entry *from;
2973 union sctp_addr temp;
2974 struct sctp_opt *sp = sctp_sk(sk);
2977 if (len != sizeof(struct sctp_getaddrs))
2980 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
2983 if (getaddrs.addr_num <= 0) return -EINVAL;
2985 * For UDP-style sockets, id specifies the association to query.
2986 * If the id field is set to the value '0' then the locally bound
2987 * addresses are returned without regard to any particular
2990 if (0 == getaddrs.assoc_id) {
2991 bp = &sctp_sk(sk)->ep->base.bind_addr;
2993 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
2996 bp = &asoc->base.bind_addr;
2999 to = (void *)getaddrs.addrs;
3000 list_for_each(pos, &bp->address_list) {
3001 from = list_entry(pos,
3002 struct sctp_sockaddr_entry,
3004 memcpy(&temp, &from->a, sizeof(temp));
3005 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3006 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3007 temp.v4.sin_port = htons(temp.v4.sin_port);
3008 if (copy_to_user(to, &temp, addrlen))
3012 if (cnt >= getaddrs.addr_num) break;
3014 getaddrs.addr_num = cnt;
3015 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3021 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3023 * Requests that the local SCTP stack use the enclosed peer address as
3024 * the association primary. The enclosed address must be one of the
3025 * association peer's addresses.
3027 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3028 char __user *optval, int __user *optlen)
3030 struct sctp_prim prim;
3031 struct sctp_association *asoc;
3032 struct sctp_opt *sp = sctp_sk(sk);
3034 if (len != sizeof(struct sctp_prim))
3037 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3040 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3044 if (!asoc->peer.primary_path)
3047 asoc->peer.primary_path->ipaddr.v4.sin_port =
3048 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3049 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3050 sizeof(union sctp_addr));
3051 asoc->peer.primary_path->ipaddr.v4.sin_port =
3052 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3054 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3055 (union sctp_addr *)&prim.ssp_addr);
3057 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3065 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3067 * Applications that wish to use the sendto() system call may wish to
3068 * specify a default set of parameters that would normally be supplied
3069 * through the inclusion of ancillary data. This socket option allows
3070 * such an application to set the default sctp_sndrcvinfo structure.
3073 * The application that wishes to use this socket option simply passes
3074 * in to this call the sctp_sndrcvinfo structure defined in Section
3075 * 5.2.2) The input parameters accepted by this call include
3076 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3077 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3078 * to this call if the caller is using the UDP model.
3080 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3082 static int sctp_getsockopt_default_send_param(struct sock *sk,
3083 int len, char __user *optval,
3086 struct sctp_sndrcvinfo info;
3087 struct sctp_association *asoc;
3088 struct sctp_opt *sp = sctp_sk(sk);
3090 if (len != sizeof(struct sctp_sndrcvinfo))
3092 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3095 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3096 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3100 info.sinfo_stream = asoc->default_stream;
3101 info.sinfo_flags = asoc->default_flags;
3102 info.sinfo_ppid = asoc->default_ppid;
3103 info.sinfo_context = asoc->default_context;
3104 info.sinfo_timetolive = asoc->default_timetolive;
3106 info.sinfo_stream = sp->default_stream;
3107 info.sinfo_flags = sp->default_flags;
3108 info.sinfo_ppid = sp->default_ppid;
3109 info.sinfo_context = sp->default_context;
3110 info.sinfo_timetolive = sp->default_timetolive;
3113 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3121 * 7.1.5 SCTP_NODELAY
3123 * Turn on/off any Nagle-like algorithm. This means that packets are
3124 * generally sent as soon as possible and no unnecessary delays are
3125 * introduced, at the cost of more packets in the network. Expects an
3126 * integer boolean flag.
3129 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3130 char __user *optval, int __user *optlen)
3134 if (len < sizeof(int))
3138 val = (sctp_sk(sk)->nodelay == 1);
3139 if (put_user(len, optlen))
3141 if (copy_to_user(optval, &val, len))
3148 * 7.1.1 SCTP_RTOINFO
3150 * The protocol parameters used to initialize and bound retransmission
3151 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3152 * and modify these parameters.
3153 * All parameters are time values, in milliseconds. A value of 0, when
3154 * modifying the parameters, indicates that the current value should not
3158 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3159 char __user *optval,
3160 int __user *optlen) {
3161 struct sctp_rtoinfo rtoinfo;
3162 struct sctp_association *asoc;
3164 if (len != sizeof (struct sctp_rtoinfo))
3167 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3170 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3172 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3175 /* Values corresponding to the specific association. */
3177 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3178 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3179 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3181 /* Values corresponding to the endpoint. */
3182 struct sctp_opt *sp = sctp_sk(sk);
3184 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3185 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3186 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3189 if (put_user(len, optlen))
3192 if (copy_to_user(optval, &rtoinfo, len))
3200 * 7.1.2 SCTP_ASSOCINFO
3202 * This option is used to tune the the maximum retransmission attempts
3203 * of the association.
3204 * Returns an error if the new association retransmission value is
3205 * greater than the sum of the retransmission value of the peer.
3206 * See [SCTP] for more information.
3209 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3210 char __user *optval,
3214 struct sctp_assocparams assocparams;
3215 struct sctp_association *asoc;
3216 struct list_head *pos;
3219 if (len != sizeof (struct sctp_assocparams))
3222 if (copy_from_user(&assocparams, optval,
3223 sizeof (struct sctp_assocparams)))
3226 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3228 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3231 /* Values correspoinding to the specific association */
3232 if (assocparams.sasoc_assoc_id != 0) {
3233 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3234 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3235 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3236 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3238 (asoc->cookie_life.tv_usec
3241 list_for_each(pos, &asoc->peer.transport_addr_list) {
3245 assocparams.sasoc_number_peer_destinations = cnt;
3247 /* Values corresponding to the endpoint */
3248 struct sctp_opt *sp = sctp_sk(sk);
3250 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3251 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3252 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3253 assocparams.sasoc_cookie_life =
3254 sp->assocparams.sasoc_cookie_life;
3255 assocparams.sasoc_number_peer_destinations =
3257 sasoc_number_peer_destinations;
3260 if (put_user(len, optlen))
3263 if (copy_to_user(optval, &assocparams, len))
3270 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3272 * This socket option is a boolean flag which turns on or off mapped V4
3273 * addresses. If this option is turned on and the socket is type
3274 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3275 * If this option is turned off, then no mapping will be done of V4
3276 * addresses and a user will receive both PF_INET6 and PF_INET type
3277 * addresses on the socket.
3279 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3280 char __user *optval, int __user *optlen)
3283 struct sctp_opt *sp = sctp_sk(sk);
3285 if (len < sizeof(int))
3290 if (put_user(len, optlen))
3292 if (copy_to_user(optval, &val, len))
3299 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3301 * This socket option specifies the maximum size to put in any outgoing
3302 * SCTP chunk. If a message is larger than this size it will be
3303 * fragmented by SCTP into the specified size. Note that the underlying
3304 * SCTP implementation may fragment into smaller sized chunks when the
3305 * PMTU of the underlying association is smaller than the value set by
3308 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3309 char __user *optval, int __user *optlen)
3313 if (len < sizeof(int))
3318 val = sctp_sk(sk)->user_frag;
3319 if (put_user(len, optlen))
3321 if (copy_to_user(optval, &val, len))
3327 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3328 char __user *optval, int __user *optlen)
3333 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3335 /* I can hardly begin to describe how wrong this is. This is
3336 * so broken as to be worse than useless. The API draft
3337 * REALLY is NOT helpful here... I am not convinced that the
3338 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3339 * are at all well-founded.
3341 if (level != SOL_SCTP) {
3342 struct sctp_af *af = sctp_sk(sk)->pf->af;
3344 retval = af->getsockopt(sk, level, optname, optval, optlen);
3348 if (get_user(len, optlen))
3355 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3357 case SCTP_DISABLE_FRAGMENTS:
3358 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3362 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3364 case SCTP_AUTOCLOSE:
3365 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3367 case SCTP_SOCKOPT_PEELOFF:
3368 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3370 case SCTP_PEER_ADDR_PARAMS:
3371 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3375 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3377 case SCTP_GET_PEER_ADDRS_NUM:
3378 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3381 case SCTP_GET_LOCAL_ADDRS_NUM:
3382 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3385 case SCTP_GET_PEER_ADDRS:
3386 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3389 case SCTP_GET_LOCAL_ADDRS:
3390 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3393 case SCTP_DEFAULT_SEND_PARAM:
3394 retval = sctp_getsockopt_default_send_param(sk, len,
3397 case SCTP_PRIMARY_ADDR:
3398 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3401 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3404 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3406 case SCTP_ASSOCINFO:
3407 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3409 case SCTP_I_WANT_MAPPED_V4_ADDR:
3410 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3413 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3415 case SCTP_GET_PEER_ADDR_INFO:
3416 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3420 retval = -ENOPROTOOPT;
3424 sctp_release_sock(sk);
3428 static void sctp_hash(struct sock *sk)
3433 static void sctp_unhash(struct sock *sk)
3438 /* Check if port is acceptable. Possibly find first available port.
3440 * The port hash table (contained in the 'global' SCTP protocol storage
3441 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3442 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3443 * list (the list number is the port number hashed out, so as you
3444 * would expect from a hash function, all the ports in a given list have
3445 * such a number that hashes out to the same list number; you were
3446 * expecting that, right?); so each list has a set of ports, with a
3447 * link to the socket (struct sock) that uses it, the port number and
3448 * a fastreuse flag (FIXME: NPI ipg).
3450 static struct sctp_bind_bucket *sctp_bucket_create(
3451 struct sctp_bind_hashbucket *head, unsigned short snum);
3453 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3455 struct sctp_bind_hashbucket *head; /* hash list */
3456 struct sctp_bind_bucket *pp; /* hash list port iterator */
3457 unsigned short snum;
3460 /* NOTE: Remember to put this back to net order. */
3461 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3462 snum = addr->v4.sin_port;
3464 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3465 sctp_local_bh_disable();
3468 /* Search for an available port.
3470 * 'sctp_port_rover' was the last port assigned, so
3471 * we start to search from 'sctp_port_rover +
3472 * 1'. What we do is first check if port 'rover' is
3473 * already in the hash table; if not, we use that; if
3474 * it is, we try next.
3476 int low = sysctl_local_port_range[0];
3477 int high = sysctl_local_port_range[1];
3478 int remaining = (high - low) + 1;
3482 sctp_spin_lock(&sctp_port_alloc_lock);
3483 rover = sctp_port_rover;
3486 if ((rover < low) || (rover > high))
3488 index = sctp_phashfn(rover);
3489 head = &sctp_port_hashtable[index];
3490 sctp_spin_lock(&head->lock);
3491 for (pp = head->chain; pp; pp = pp->next)
3492 if (pp->port == rover)
3496 sctp_spin_unlock(&head->lock);
3497 } while (--remaining > 0);
3498 sctp_port_rover = rover;
3499 sctp_spin_unlock(&sctp_port_alloc_lock);
3501 /* Exhausted local port range during search? */
3506 /* OK, here is the one we will use. HEAD (the port
3507 * hash table list entry) is non-NULL and we hold it's
3512 /* We are given an specific port number; we verify
3513 * that it is not being used. If it is used, we will
3514 * exahust the search in the hash list corresponding
3515 * to the port number (snum) - we detect that with the
3516 * port iterator, pp being NULL.
3518 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3519 sctp_spin_lock(&head->lock);
3520 for (pp = head->chain; pp; pp = pp->next) {
3521 if (pp->port == snum)
3528 if (!hlist_empty(&pp->owner)) {
3529 /* We had a port hash table hit - there is an
3530 * available port (pp != NULL) and it is being
3531 * used by other socket (pp->owner not empty); that other
3532 * socket is going to be sk2.
3534 int reuse = sk->sk_reuse;
3536 struct hlist_node *node;
3538 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3539 if (pp->fastreuse && sk->sk_reuse)
3542 /* Run through the list of sockets bound to the port
3543 * (pp->port) [via the pointers bind_next and
3544 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3545 * we get the endpoint they describe and run through
3546 * the endpoint's list of IP (v4 or v6) addresses,
3547 * comparing each of the addresses with the address of
3548 * the socket sk. If we find a match, then that means
3549 * that this port/socket (sk) combination are already
3552 sk_for_each_bound(sk2, node, &pp->owner) {
3553 struct sctp_endpoint *ep2;
3554 ep2 = sctp_sk(sk2)->ep;
3556 if (reuse && sk2->sk_reuse)
3559 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3565 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3568 /* If there was a hash table miss, create a new port. */
3570 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3573 /* In either case (hit or miss), make sure fastreuse is 1 only
3574 * if sk->sk_reuse is too (that is, if the caller requested
3575 * SO_REUSEADDR on this socket -sk-).
3577 if (hlist_empty(&pp->owner))
3578 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3579 else if (pp->fastreuse && !sk->sk_reuse)
3582 /* We are set, so fill up all the data in the hash table
3583 * entry, tie the socket list information with the rest of the
3584 * sockets FIXME: Blurry, NPI (ipg).
3587 inet_sk(sk)->num = snum;
3588 if (!sctp_sk(sk)->bind_hash) {
3589 sk_add_bind_node(sk, &pp->owner);
3590 sctp_sk(sk)->bind_hash = pp;
3595 sctp_spin_unlock(&head->lock);
3598 sctp_local_bh_enable();
3599 addr->v4.sin_port = htons(addr->v4.sin_port);
3603 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3604 * port is requested.
3606 static int sctp_get_port(struct sock *sk, unsigned short snum)
3609 union sctp_addr addr;
3610 struct sctp_af *af = sctp_sk(sk)->pf->af;
3612 /* Set up a dummy address struct from the sk. */
3613 af->from_sk(&addr, sk);
3614 addr.v4.sin_port = htons(snum);
3616 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3617 ret = sctp_get_port_local(sk, &addr);
3619 return (ret ? 1 : 0);
3623 * 3.1.3 listen() - UDP Style Syntax
3625 * By default, new associations are not accepted for UDP style sockets.
3626 * An application uses listen() to mark a socket as being able to
3627 * accept new associations.
3629 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3631 struct sctp_opt *sp = sctp_sk(sk);
3632 struct sctp_endpoint *ep = sp->ep;
3634 /* Only UDP style sockets that are not peeled off are allowed to
3637 if (!sctp_style(sk, UDP))
3640 /* If backlog is zero, disable listening. */
3642 if (sctp_sstate(sk, CLOSED))
3645 sctp_unhash_endpoint(ep);
3646 sk->sk_state = SCTP_SS_CLOSED;
3649 /* Return if we are already listening. */
3650 if (sctp_sstate(sk, LISTENING))
3654 * If a bind() or sctp_bindx() is not called prior to a listen()
3655 * call that allows new associations to be accepted, the system
3656 * picks an ephemeral port and will choose an address set equivalent
3657 * to binding with a wildcard address.
3659 * This is not currently spelled out in the SCTP sockets
3660 * extensions draft, but follows the practice as seen in TCP
3663 if (!ep->base.bind_addr.port) {
3664 if (sctp_autobind(sk))
3667 sk->sk_state = SCTP_SS_LISTENING;
3668 sctp_hash_endpoint(ep);
3673 * 4.1.3 listen() - TCP Style Syntax
3675 * Applications uses listen() to ready the SCTP endpoint for accepting
3676 * inbound associations.
3678 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3680 struct sctp_opt *sp = sctp_sk(sk);
3681 struct sctp_endpoint *ep = sp->ep;
3683 /* If backlog is zero, disable listening. */
3685 if (sctp_sstate(sk, CLOSED))
3688 sctp_unhash_endpoint(ep);
3689 sk->sk_state = SCTP_SS_CLOSED;
3692 if (sctp_sstate(sk, LISTENING))
3696 * If a bind() or sctp_bindx() is not called prior to a listen()
3697 * call that allows new associations to be accepted, the system
3698 * picks an ephemeral port and will choose an address set equivalent
3699 * to binding with a wildcard address.
3701 * This is not currently spelled out in the SCTP sockets
3702 * extensions draft, but follows the practice as seen in TCP
3705 if (!ep->base.bind_addr.port) {
3706 if (sctp_autobind(sk))
3709 sk->sk_state = SCTP_SS_LISTENING;
3710 sk->sk_max_ack_backlog = backlog;
3711 sctp_hash_endpoint(ep);
3716 * Move a socket to LISTENING state.
3718 int sctp_inet_listen(struct socket *sock, int backlog)
3720 struct sock *sk = sock->sk;
3721 struct crypto_tfm *tfm=NULL;
3724 if (unlikely(backlog < 0))
3729 if (sock->state != SS_UNCONNECTED)
3732 /* Allocate HMAC for generating cookie. */
3733 if (sctp_hmac_alg) {
3734 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
3741 switch (sock->type) {
3742 case SOCK_SEQPACKET:
3743 err = sctp_seqpacket_listen(sk, backlog);
3746 err = sctp_stream_listen(sk, backlog);
3754 /* Store away the transform reference. */
3755 sctp_sk(sk)->hmac = tfm;
3757 sctp_release_sock(sk);
3761 sctp_crypto_free_tfm(tfm);
3766 * This function is done by modeling the current datagram_poll() and the
3767 * tcp_poll(). Note that, based on these implementations, we don't
3768 * lock the socket in this function, even though it seems that,
3769 * ideally, locking or some other mechanisms can be used to ensure
3770 * the integrity of the counters (sndbuf and wmem_queued) used
3771 * in this place. We assume that we don't need locks either until proven
3774 * Another thing to note is that we include the Async I/O support
3775 * here, again, by modeling the current TCP/UDP code. We don't have
3776 * a good way to test with it yet.
3778 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
3780 struct sock *sk = sock->sk;
3781 struct sctp_opt *sp = sctp_sk(sk);
3784 poll_wait(file, sk->sk_sleep, wait);
3786 /* A TCP-style listening socket becomes readable when the accept queue
3789 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3790 return (!list_empty(&sp->ep->asocs)) ?
3791 (POLLIN | POLLRDNORM) : 0;
3795 /* Is there any exceptional events? */
3796 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
3798 if (sk->sk_shutdown == SHUTDOWN_MASK)
3801 /* Is it readable? Reconsider this code with TCP-style support. */
3802 if (!skb_queue_empty(&sk->sk_receive_queue) ||
3803 (sk->sk_shutdown & RCV_SHUTDOWN))
3804 mask |= POLLIN | POLLRDNORM;
3806 /* The association is either gone or not ready. */
3807 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
3810 /* Is it writable? */
3811 if (sctp_writeable(sk)) {
3812 mask |= POLLOUT | POLLWRNORM;
3814 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
3816 * Since the socket is not locked, the buffer
3817 * might be made available after the writeable check and
3818 * before the bit is set. This could cause a lost I/O
3819 * signal. tcp_poll() has a race breaker for this race
3820 * condition. Based on their implementation, we put
3821 * in the following code to cover it as well.
3823 if (sctp_writeable(sk))
3824 mask |= POLLOUT | POLLWRNORM;
3829 /********************************************************************
3830 * 2nd Level Abstractions
3831 ********************************************************************/
3833 static struct sctp_bind_bucket *sctp_bucket_create(
3834 struct sctp_bind_hashbucket *head, unsigned short snum)
3836 struct sctp_bind_bucket *pp;
3838 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
3839 SCTP_DBG_OBJCNT_INC(bind_bucket);
3843 INIT_HLIST_HEAD(&pp->owner);
3844 if ((pp->next = head->chain) != NULL)
3845 pp->next->pprev = &pp->next;
3847 pp->pprev = &head->chain;
3852 /* Caller must hold hashbucket lock for this tb with local BH disabled */
3853 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
3855 if (hlist_empty(&pp->owner)) {
3857 pp->next->pprev = pp->pprev;
3858 *(pp->pprev) = pp->next;
3859 kmem_cache_free(sctp_bucket_cachep, pp);
3860 SCTP_DBG_OBJCNT_DEC(bind_bucket);
3864 /* Release this socket's reference to a local port. */
3865 static inline void __sctp_put_port(struct sock *sk)
3867 struct sctp_bind_hashbucket *head =
3868 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
3869 struct sctp_bind_bucket *pp;
3871 sctp_spin_lock(&head->lock);
3872 pp = sctp_sk(sk)->bind_hash;
3873 __sk_del_bind_node(sk);
3874 sctp_sk(sk)->bind_hash = NULL;
3875 inet_sk(sk)->num = 0;
3876 sctp_bucket_destroy(pp);
3877 sctp_spin_unlock(&head->lock);
3880 void sctp_put_port(struct sock *sk)
3882 sctp_local_bh_disable();
3883 __sctp_put_port(sk);
3884 sctp_local_bh_enable();
3888 * The system picks an ephemeral port and choose an address set equivalent
3889 * to binding with a wildcard address.
3890 * One of those addresses will be the primary address for the association.
3891 * This automatically enables the multihoming capability of SCTP.
3893 static int sctp_autobind(struct sock *sk)
3895 union sctp_addr autoaddr;
3897 unsigned short port;
3899 /* Initialize a local sockaddr structure to INADDR_ANY. */
3900 af = sctp_sk(sk)->pf->af;
3902 port = htons(inet_sk(sk)->num);
3903 af->inaddr_any(&autoaddr, port);
3905 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
3908 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
3911 * 4.2 The cmsghdr Structure *
3913 * When ancillary data is sent or received, any number of ancillary data
3914 * objects can be specified by the msg_control and msg_controllen members of
3915 * the msghdr structure, because each object is preceded by
3916 * a cmsghdr structure defining the object's length (the cmsg_len member).
3917 * Historically Berkeley-derived implementations have passed only one object
3918 * at a time, but this API allows multiple objects to be
3919 * passed in a single call to sendmsg() or recvmsg(). The following example
3920 * shows two ancillary data objects in a control buffer.
3922 * |<--------------------------- msg_controllen -------------------------->|
3925 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
3927 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
3930 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
3932 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
3935 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
3936 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
3938 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
3940 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
3947 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
3948 sctp_cmsgs_t *cmsgs)
3950 struct cmsghdr *cmsg;
3952 for (cmsg = CMSG_FIRSTHDR(msg);
3954 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
3955 /* Check for minimum length. The SCM code has this check. */
3956 if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
3957 (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
3958 + cmsg->cmsg_len) > msg->msg_controllen) {
3962 /* Should we parse this header or ignore? */
3963 if (cmsg->cmsg_level != IPPROTO_SCTP)
3966 /* Strictly check lengths following example in SCM code. */
3967 switch (cmsg->cmsg_type) {
3969 /* SCTP Socket API Extension
3970 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
3972 * This cmsghdr structure provides information for
3973 * initializing new SCTP associations with sendmsg().
3974 * The SCTP_INITMSG socket option uses this same data
3975 * structure. This structure is not used for
3978 * cmsg_level cmsg_type cmsg_data[]
3979 * ------------ ------------ ----------------------
3980 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
3982 if (cmsg->cmsg_len !=
3983 CMSG_LEN(sizeof(struct sctp_initmsg)))
3985 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
3989 /* SCTP Socket API Extension
3990 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
3992 * This cmsghdr structure specifies SCTP options for
3993 * sendmsg() and describes SCTP header information
3994 * about a received message through recvmsg().
3996 * cmsg_level cmsg_type cmsg_data[]
3997 * ------------ ------------ ----------------------
3998 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4000 if (cmsg->cmsg_len !=
4001 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4005 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4007 /* Minimally, validate the sinfo_flags. */
4008 if (cmsgs->info->sinfo_flags &
4009 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4010 MSG_ABORT | MSG_EOF))
4022 * Wait for a packet..
4023 * Note: This function is the same function as in core/datagram.c
4024 * with a few modifications to make lksctp work.
4026 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4031 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4033 /* Socket errors? */
4034 error = sock_error(sk);
4038 if (!skb_queue_empty(&sk->sk_receive_queue))
4041 /* Socket shut down? */
4042 if (sk->sk_shutdown & RCV_SHUTDOWN)
4045 /* Sequenced packets can come disconnected. If so we report the
4050 /* Is there a good reason to think that we may receive some data? */
4051 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4054 /* Handle signals. */
4055 if (signal_pending(current))
4058 /* Let another process have a go. Since we are going to sleep
4059 * anyway. Note: This may cause odd behaviors if the message
4060 * does not fit in the user's buffer, but this seems to be the
4061 * only way to honor MSG_DONTWAIT realistically.
4063 sctp_release_sock(sk);
4064 *timeo_p = schedule_timeout(*timeo_p);
4068 finish_wait(sk->sk_sleep, &wait);
4072 error = sock_intr_errno(*timeo_p);
4075 finish_wait(sk->sk_sleep, &wait);
4080 /* Receive a datagram.
4081 * Note: This is pretty much the same routine as in core/datagram.c
4082 * with a few changes to make lksctp work.
4084 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4085 int noblock, int *err)
4088 struct sk_buff *skb;
4091 /* Caller is allowed not to check sk->sk_err before calling. */
4092 error = sock_error(sk);
4096 timeo = sock_rcvtimeo(sk, noblock);
4098 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4099 timeo, MAX_SCHEDULE_TIMEOUT);
4102 /* Again only user level code calls this function,
4103 * so nothing interrupt level
4104 * will suddenly eat the receive_queue.
4106 * Look at current nfs client by the way...
4107 * However, this function was corrent in any case. 8)
4109 if (flags & MSG_PEEK) {
4110 unsigned long cpu_flags;
4112 sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4114 skb = skb_peek(&sk->sk_receive_queue);
4116 atomic_inc(&skb->users);
4117 sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4120 skb = skb_dequeue(&sk->sk_receive_queue);
4126 if (sk->sk_shutdown & RCV_SHUTDOWN)
4129 /* User doesn't want to wait. */
4133 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4142 /* Verify that this is a valid address. */
4143 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
4148 /* Verify basic sockaddr. */
4149 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
4153 /* Is this a valid SCTP address? */
4154 if (!af->addr_valid(addr, sctp_sk(sk)))
4157 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
4163 /* Get the sndbuf space available at the time on the association. */
4164 static inline int sctp_wspace(struct sctp_association *asoc)
4166 struct sock *sk = asoc->base.sk;
4169 amt = sk->sk_sndbuf - asoc->sndbuf_used;
4175 /* Increment the used sndbuf space count of the corresponding association by
4176 * the size of the outgoing data chunk.
4177 * Also, set the skb destructor for sndbuf accounting later.
4179 * Since it is always 1-1 between chunk and skb, and also a new skb is always
4180 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
4181 * destructor in the data chunk skb for the purpose of the sndbuf space
4184 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
4186 struct sctp_association *asoc = chunk->asoc;
4187 struct sock *sk = asoc->base.sk;
4189 /* The sndbuf space is tracked per association. */
4190 sctp_association_hold(asoc);
4192 chunk->skb->destructor = sctp_wfree;
4193 /* Save the chunk pointer in skb for sctp_wfree to use later. */
4194 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
4196 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk);
4197 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk);
4200 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4201 static void __sctp_write_space(struct sctp_association *asoc)
4203 struct sock *sk = asoc->base.sk;
4204 struct socket *sock = sk->sk_socket;
4206 if ((sctp_wspace(asoc) > 0) && sock) {
4207 if (waitqueue_active(&asoc->wait))
4208 wake_up_interruptible(&asoc->wait);
4210 if (sctp_writeable(sk)) {
4211 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4212 wake_up_interruptible(sk->sk_sleep);
4214 /* Note that we try to include the Async I/O support
4215 * here by modeling from the current TCP/UDP code.
4216 * We have not tested with it yet.
4218 if (sock->fasync_list &&
4219 !(sk->sk_shutdown & SEND_SHUTDOWN))
4220 sock_wake_async(sock, 2, POLL_OUT);
4225 /* Do accounting for the sndbuf space.
4226 * Decrement the used sndbuf space of the corresponding association by the
4227 * data size which was just transmitted(freed).
4229 static void sctp_wfree(struct sk_buff *skb)
4231 struct sctp_association *asoc;
4232 struct sctp_chunk *chunk;
4235 /* Get the saved chunk pointer. */
4236 chunk = *((struct sctp_chunk **)(skb->cb));
4239 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk);
4240 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk);
4241 __sctp_write_space(asoc);
4243 sctp_association_put(asoc);
4246 /* Helper function to wait for space in the sndbuf. */
4247 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4250 struct sock *sk = asoc->base.sk;
4252 long current_timeo = *timeo_p;
4255 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4256 asoc, (long)(*timeo_p), msg_len);
4258 /* Increment the association's refcnt. */
4259 sctp_association_hold(asoc);
4261 /* Wait on the association specific sndbuf space. */
4263 prepare_to_wait_exclusive(&asoc->wait, &wait,
4264 TASK_INTERRUPTIBLE);
4267 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4270 if (signal_pending(current))
4271 goto do_interrupted;
4272 if (msg_len <= sctp_wspace(asoc))
4275 /* Let another process have a go. Since we are going
4278 sctp_release_sock(sk);
4279 current_timeo = schedule_timeout(current_timeo);
4282 *timeo_p = current_timeo;
4286 finish_wait(&asoc->wait, &wait);
4288 /* Release the association's refcnt. */
4289 sctp_association_put(asoc);
4298 err = sock_intr_errno(*timeo_p);
4306 /* If socket sndbuf has changed, wake up all per association waiters. */
4307 void sctp_write_space(struct sock *sk)
4309 struct sctp_association *asoc;
4310 struct list_head *pos;
4312 /* Wake up the tasks in each wait queue. */
4313 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4314 asoc = list_entry(pos, struct sctp_association, asocs);
4315 __sctp_write_space(asoc);
4319 /* Is there any sndbuf space available on the socket?
4321 * Note that wmem_queued is the sum of the send buffers on all of the
4322 * associations on the same socket. For a UDP-style socket with
4323 * multiple associations, it is possible for it to be "unwriteable"
4324 * prematurely. I assume that this is acceptable because
4325 * a premature "unwriteable" is better than an accidental "writeable" which
4326 * would cause an unwanted block under certain circumstances. For the 1-1
4327 * UDP-style sockets or TCP-style sockets, this code should work.
4330 static int sctp_writeable(struct sock *sk)
4334 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4340 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4341 * returns immediately with EINPROGRESS.
4343 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4345 struct sock *sk = asoc->base.sk;
4347 long current_timeo = *timeo_p;
4350 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4353 /* Increment the association's refcnt. */
4354 sctp_association_hold(asoc);
4357 prepare_to_wait_exclusive(&asoc->wait, &wait,
4358 TASK_INTERRUPTIBLE);
4361 if (sk->sk_shutdown & RCV_SHUTDOWN)
4363 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4366 if (signal_pending(current))
4367 goto do_interrupted;
4369 if (sctp_state(asoc, ESTABLISHED))
4372 /* Let another process have a go. Since we are going
4375 sctp_release_sock(sk);
4376 current_timeo = schedule_timeout(current_timeo);
4379 *timeo_p = current_timeo;
4383 finish_wait(&asoc->wait, &wait);
4385 /* Release the association's refcnt. */
4386 sctp_association_put(asoc);
4391 err = -ECONNREFUSED;
4395 err = sock_intr_errno(*timeo_p);
4403 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4405 struct sctp_endpoint *ep;
4409 ep = sctp_sk(sk)->ep;
4413 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4414 TASK_INTERRUPTIBLE);
4416 if (list_empty(&ep->asocs)) {
4417 sctp_release_sock(sk);
4418 timeo = schedule_timeout(timeo);
4423 if (!sctp_sstate(sk, LISTENING))
4427 if (!list_empty(&ep->asocs))
4430 err = sock_intr_errno(timeo);
4431 if (signal_pending(current))
4439 finish_wait(sk->sk_sleep, &wait);
4444 void sctp_wait_for_close(struct sock *sk, long timeout)
4449 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4450 if (list_empty(&sctp_sk(sk)->ep->asocs))
4452 sctp_release_sock(sk);
4453 timeout = schedule_timeout(timeout);
4455 } while (!signal_pending(current) && timeout);
4457 finish_wait(sk->sk_sleep, &wait);
4460 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4461 * and its messages to the newsk.
4463 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4464 struct sctp_association *assoc,
4465 sctp_socket_type_t type)
4467 struct sctp_opt *oldsp = sctp_sk(oldsk);
4468 struct sctp_opt *newsp = sctp_sk(newsk);
4469 struct sctp_bind_bucket *pp; /* hash list port iterator */
4470 struct sctp_endpoint *newep = newsp->ep;
4471 struct sk_buff *skb, *tmp;
4472 struct sctp_ulpevent *event;
4474 /* Migrate socket buffer sizes and all the socket level options to the
4477 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4478 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4479 /* Brute force copy old sctp opt. */
4480 memcpy(newsp, oldsp, sizeof(struct sctp_opt));
4482 /* Restore the ep value that was overwritten with the above structure
4488 /* Hook this new socket in to the bind_hash list. */
4489 pp = sctp_sk(oldsk)->bind_hash;
4490 sk_add_bind_node(newsk, &pp->owner);
4491 sctp_sk(newsk)->bind_hash = pp;
4492 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4494 /* Move any messages in the old socket's receive queue that are for the
4495 * peeled off association to the new socket's receive queue.
4497 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4498 event = sctp_skb2event(skb);
4499 if (event->asoc == assoc) {
4500 __skb_unlink(skb, skb->list);
4501 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4505 /* Clean up any messages pending delivery due to partial
4506 * delivery. Three cases:
4507 * 1) No partial deliver; no work.
4508 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4509 * 3) Peeling off non-partial delivery; move pd_lobby to recieve_queue.
4511 skb_queue_head_init(&newsp->pd_lobby);
4512 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4514 if (sctp_sk(oldsk)->pd_mode) {
4515 struct sk_buff_head *queue;
4517 /* Decide which queue to move pd_lobby skbs to. */
4518 if (assoc->ulpq.pd_mode) {
4519 queue = &newsp->pd_lobby;
4521 queue = &newsk->sk_receive_queue;
4523 /* Walk through the pd_lobby, looking for skbs that
4524 * need moved to the new socket.
4526 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4527 event = sctp_skb2event(skb);
4528 if (event->asoc == assoc) {
4529 __skb_unlink(skb, skb->list);
4530 __skb_queue_tail(queue, skb);
4534 /* Clear up any skbs waiting for the partial
4535 * delivery to finish.
4537 if (assoc->ulpq.pd_mode)
4538 sctp_clear_pd(oldsk);
4542 /* Set the type of socket to indicate that it is peeled off from the
4543 * original UDP-style socket or created with the accept() call on a
4544 * TCP-style socket..
4548 /* Migrate the association to the new socket. */
4549 sctp_assoc_migrate(assoc, newsk);
4551 /* If the association on the newsk is already closed before accept()
4552 * is called, set RCV_SHUTDOWN flag.
4554 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4555 newsk->sk_shutdown |= RCV_SHUTDOWN;
4557 newsk->sk_state = SCTP_SS_ESTABLISHED;
4560 /* This proto struct describes the ULP interface for SCTP. */
4561 struct proto sctp_prot = {
4563 .close = sctp_close,
4564 .connect = sctp_connect,
4565 .disconnect = sctp_disconnect,
4566 .accept = sctp_accept,
4567 .ioctl = sctp_ioctl,
4568 .init = sctp_init_sock,
4569 .destroy = sctp_destroy_sock,
4570 .shutdown = sctp_shutdown,
4571 .setsockopt = sctp_setsockopt,
4572 .getsockopt = sctp_getsockopt,
4573 .sendmsg = sctp_sendmsg,
4574 .recvmsg = sctp_recvmsg,
4576 .backlog_rcv = sctp_backlog_rcv,
4578 .unhash = sctp_unhash,
4579 .get_port = sctp_get_port,