1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
73 #include <net/route.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 extern kmem_cache_t *sctp_bucket_cachep;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
115 struct sock *sk = asoc->base.sk;
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
149 skb_set_owner_w(chunk->skb, sk);
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
159 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
160 sizeof(struct sk_buff) +
161 sizeof(struct sctp_chunk);
163 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
166 /* Verify that this is a valid address. */
167 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
172 /* Verify basic sockaddr. */
173 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
177 /* Is this a valid SCTP address? */
178 if (!af->addr_valid(addr, sctp_sk(sk)))
181 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
187 /* Look up the association by its id. If this is not a UDP-style
188 * socket, the ID field is always ignored.
190 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
192 struct sctp_association *asoc = NULL;
194 /* If this is not a UDP-style socket, assoc id should be ignored. */
195 if (!sctp_style(sk, UDP)) {
196 /* Return NULL if the socket state is not ESTABLISHED. It
197 * could be a TCP-style listening socket or a socket which
198 * hasn't yet called connect() to establish an association.
200 if (!sctp_sstate(sk, ESTABLISHED))
203 /* Get the first and the only association from the list. */
204 if (!list_empty(&sctp_sk(sk)->ep->asocs))
205 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
206 struct sctp_association, asocs);
210 /* Otherwise this is a UDP-style socket. */
211 if (!id || (id == (sctp_assoc_t)-1))
214 spin_lock_bh(&sctp_assocs_id_lock);
215 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
216 spin_unlock_bh(&sctp_assocs_id_lock);
218 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
224 /* Look up the transport from an address and an assoc id. If both address and
225 * id are specified, the associations matching the address and the id should be
228 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
229 struct sockaddr_storage *addr,
232 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
233 struct sctp_transport *transport;
234 union sctp_addr *laddr = (union sctp_addr *)addr;
236 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
237 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
238 (union sctp_addr *)addr,
240 laddr->v4.sin_port = htons(laddr->v4.sin_port);
245 id_asoc = sctp_id2assoc(sk, id);
246 if (id_asoc && (id_asoc != addr_asoc))
249 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
250 (union sctp_addr *)addr);
255 /* API 3.1.2 bind() - UDP Style Syntax
256 * The syntax of bind() is,
258 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
260 * sd - the socket descriptor returned by socket().
261 * addr - the address structure (struct sockaddr_in or struct
262 * sockaddr_in6 [RFC 2553]),
263 * addr_len - the size of the address structure.
265 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
271 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
272 sk, uaddr, addr_len);
274 /* Disallow binding twice. */
275 if (!sctp_sk(sk)->ep->base.bind_addr.port)
276 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
281 sctp_release_sock(sk);
286 static long sctp_get_port_local(struct sock *, union sctp_addr *);
288 /* Verify this is a valid sockaddr. */
289 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
290 union sctp_addr *addr, int len)
294 /* Check minimum size. */
295 if (len < sizeof (struct sockaddr))
298 /* Does this PF support this AF? */
299 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
302 /* If we get this far, af is valid. */
303 af = sctp_get_af_specific(addr->sa.sa_family);
305 if (len < af->sockaddr_len)
311 /* Bind a local address either to an endpoint or to an association. */
312 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
314 struct sctp_sock *sp = sctp_sk(sk);
315 struct sctp_endpoint *ep = sp->ep;
316 struct sctp_bind_addr *bp = &ep->base.bind_addr;
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
324 /* Common sockaddr verification. */
325 af = sctp_sockaddr_af(sp, addr, len);
329 /* PF specific bind() address verification. */
330 if (!sp->pf->bind_verify(sp, addr))
331 return -EADDRNOTAVAIL;
333 snum= ntohs(addr->v4.sin_port);
335 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
338 /* We must either be unbound, or bind to the same port. */
339 if (bp->port && (snum != bp->port)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum, bp->port);
346 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
353 if ((ret = sctp_get_port_local(sk, addr))) {
354 if (ret == (long) sk) {
355 /* This endpoint has a conflicting address. */
362 /* Refresh ephemeral port. */
364 bp->port = inet_sk(sk)->num;
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep->base.addr_lock);
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr->v4.sin_port = ntohs(addr->v4.sin_port);
372 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
373 addr->v4.sin_port = htons(addr->v4.sin_port);
374 sctp_write_unlock(&ep->base.addr_lock);
375 sctp_local_bh_enable();
377 /* Copy back into socket for getsockname() use. */
379 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
380 af->to_sk_saddr(addr, sk);
386 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
389 * at any one time. If a sender, after sending an ASCONF chunk, decides
390 * it needs to transfer another ASCONF Chunk, it MUST wait until the
391 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
392 * subsequent ASCONF. Note this restriction binds each side, so at any
393 * time two ASCONF may be in-transit on any given association (one sent
394 * from each endpoint).
396 static int sctp_send_asconf(struct sctp_association *asoc,
397 struct sctp_chunk *chunk)
401 /* If there is an outstanding ASCONF chunk, queue it for later
404 if (asoc->addip_last_asconf) {
405 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
409 /* Hold the chunk until an ASCONF_ACK is received. */
410 sctp_chunk_hold(chunk);
411 retval = sctp_primitive_ASCONF(asoc, chunk);
413 sctp_chunk_free(chunk);
415 asoc->addip_last_asconf = chunk;
421 /* Add a list of addresses as bind addresses to local endpoint or
424 * Basically run through each address specified in the addrs/addrcnt
425 * array/length pair, determine if it is IPv6 or IPv4 and call
426 * sctp_do_bind() on it.
428 * If any of them fails, then the operation will be reversed and the
429 * ones that were added will be removed.
431 * Only sctp_setsockopt_bindx() is supposed to call this function.
433 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
438 struct sockaddr *sa_addr;
441 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
445 for (cnt = 0; cnt < addrcnt; cnt++) {
446 /* The list may contain either IPv4 or IPv6 address;
447 * determine the address length for walking thru the list.
449 sa_addr = (struct sockaddr *)addr_buf;
450 af = sctp_get_af_specific(sa_addr->sa_family);
456 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
459 addr_buf += af->sockaddr_len;
463 /* Failed. Cleanup the ones that have been added */
465 sctp_bindx_rem(sk, addrs, cnt);
473 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
474 * associations that are part of the endpoint indicating that a list of local
475 * addresses are added to the endpoint.
477 * If any of the addresses is already in the bind address list of the
478 * association, we do not send the chunk for that association. But it will not
479 * affect other associations.
481 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 static int sctp_send_asconf_add_ip(struct sock *sk,
484 struct sockaddr *addrs,
487 struct sctp_sock *sp;
488 struct sctp_endpoint *ep;
489 struct sctp_association *asoc;
490 struct sctp_bind_addr *bp;
491 struct sctp_chunk *chunk;
492 struct sctp_sockaddr_entry *laddr;
493 union sctp_addr *addr;
496 struct list_head *pos;
501 if (!sctp_addip_enable)
507 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
508 __FUNCTION__, sk, addrs, addrcnt);
510 list_for_each(pos, &ep->asocs) {
511 asoc = list_entry(pos, struct sctp_association, asocs);
513 if (!asoc->peer.asconf_capable)
516 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
519 if (!sctp_state(asoc, ESTABLISHED))
522 /* Check if any address in the packed array of addresses is
523 * in the bind address list of the association. If so,
524 * do not send the asconf chunk to its peer, but continue with
525 * other associations.
528 for (i = 0; i < addrcnt; i++) {
529 addr = (union sctp_addr *)addr_buf;
530 af = sctp_get_af_specific(addr->v4.sin_family);
536 if (sctp_assoc_lookup_laddr(asoc, addr))
539 addr_buf += af->sockaddr_len;
544 /* Use the first address in bind addr list of association as
545 * Address Parameter of ASCONF CHUNK.
547 sctp_read_lock(&asoc->base.addr_lock);
548 bp = &asoc->base.bind_addr;
549 p = bp->address_list.next;
550 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
551 sctp_read_unlock(&asoc->base.addr_lock);
553 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
554 addrcnt, SCTP_PARAM_ADD_IP);
560 retval = sctp_send_asconf(asoc, chunk);
562 /* FIXME: After sending the add address ASCONF chunk, we
563 * cannot append the address to the association's binding
564 * address list, because the new address may be used as the
565 * source of a message sent to the peer before the ASCONF
566 * chunk is received by the peer. So we should wait until
567 * ASCONF_ACK is received.
575 /* Remove a list of addresses from bind addresses list. Do not remove the
578 * Basically run through each address specified in the addrs/addrcnt
579 * array/length pair, determine if it is IPv6 or IPv4 and call
580 * sctp_del_bind() on it.
582 * If any of them fails, then the operation will be reversed and the
583 * ones that were removed will be added back.
585 * At least one address has to be left; if only one address is
586 * available, the operation will return -EBUSY.
588 * Only sctp_setsockopt_bindx() is supposed to call this function.
590 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
592 struct sctp_sock *sp = sctp_sk(sk);
593 struct sctp_endpoint *ep = sp->ep;
595 struct sctp_bind_addr *bp = &ep->base.bind_addr;
597 union sctp_addr saveaddr;
599 struct sockaddr *sa_addr;
602 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
606 for (cnt = 0; cnt < addrcnt; cnt++) {
607 /* If the bind address list is empty or if there is only one
608 * bind address, there is nothing more to be removed (we need
609 * at least one address here).
611 if (list_empty(&bp->address_list) ||
612 (sctp_list_single_entry(&bp->address_list))) {
617 /* The list may contain either IPv4 or IPv6 address;
618 * determine the address length to copy the address to
621 sa_addr = (struct sockaddr *)addr_buf;
622 af = sctp_get_af_specific(sa_addr->sa_family);
627 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
628 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
629 if (saveaddr.v4.sin_port != bp->port) {
634 /* FIXME - There is probably a need to check if sk->sk_saddr and
635 * sk->sk_rcv_addr are currently set to one of the addresses to
636 * be removed. This is something which needs to be looked into
637 * when we are fixing the outstanding issues with multi-homing
638 * socket routing and failover schemes. Refer to comments in
639 * sctp_do_bind(). -daisy
641 sctp_local_bh_disable();
642 sctp_write_lock(&ep->base.addr_lock);
644 retval = sctp_del_bind_addr(bp, &saveaddr);
646 sctp_write_unlock(&ep->base.addr_lock);
647 sctp_local_bh_enable();
649 addr_buf += af->sockaddr_len;
652 /* Failed. Add the ones that has been removed back */
654 sctp_bindx_add(sk, addrs, cnt);
662 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
663 * the associations that are part of the endpoint indicating that a list of
664 * local addresses are removed from the endpoint.
666 * If any of the addresses is already in the bind address list of the
667 * association, we do not send the chunk for that association. But it will not
668 * affect other associations.
670 * Only sctp_setsockopt_bindx() is supposed to call this function.
672 static int sctp_send_asconf_del_ip(struct sock *sk,
673 struct sockaddr *addrs,
676 struct sctp_sock *sp;
677 struct sctp_endpoint *ep;
678 struct sctp_association *asoc;
679 struct sctp_bind_addr *bp;
680 struct sctp_chunk *chunk;
681 union sctp_addr *laddr;
684 struct list_head *pos;
688 if (!sctp_addip_enable)
694 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
695 __FUNCTION__, sk, addrs, addrcnt);
697 list_for_each(pos, &ep->asocs) {
698 asoc = list_entry(pos, struct sctp_association, asocs);
700 if (!asoc->peer.asconf_capable)
703 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
706 if (!sctp_state(asoc, ESTABLISHED))
709 /* Check if any address in the packed array of addresses is
710 * not present in the bind address list of the association.
711 * If so, do not send the asconf chunk to its peer, but
712 * continue with other associations.
715 for (i = 0; i < addrcnt; i++) {
716 laddr = (union sctp_addr *)addr_buf;
717 af = sctp_get_af_specific(laddr->v4.sin_family);
723 if (!sctp_assoc_lookup_laddr(asoc, laddr))
726 addr_buf += af->sockaddr_len;
731 /* Find one address in the association's bind address list
732 * that is not in the packed array of addresses. This is to
733 * make sure that we do not delete all the addresses in the
736 sctp_read_lock(&asoc->base.addr_lock);
737 bp = &asoc->base.bind_addr;
738 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
740 sctp_read_unlock(&asoc->base.addr_lock);
744 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
751 retval = sctp_send_asconf(asoc, chunk);
753 /* FIXME: After sending the delete address ASCONF chunk, we
754 * cannot remove the addresses from the association's bind
755 * address list, because there maybe some packet send to
756 * the delete addresses, so we should wait until ASCONF_ACK
757 * packet is received.
764 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
767 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
770 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
771 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
774 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
775 * Section 3.1.2 for this usage.
777 * addrs is a pointer to an array of one or more socket addresses. Each
778 * address is contained in its appropriate structure (i.e. struct
779 * sockaddr_in or struct sockaddr_in6) the family of the address type
780 * must be used to distengish the address length (note that this
781 * representation is termed a "packed array" of addresses). The caller
782 * specifies the number of addresses in the array with addrcnt.
784 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
785 * -1, and sets errno to the appropriate error code.
787 * For SCTP, the port given in each socket address must be the same, or
788 * sctp_bindx() will fail, setting errno to EINVAL.
790 * The flags parameter is formed from the bitwise OR of zero or more of
791 * the following currently defined flags:
793 * SCTP_BINDX_ADD_ADDR
795 * SCTP_BINDX_REM_ADDR
797 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
798 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
799 * addresses from the association. The two flags are mutually exclusive;
800 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
801 * not remove all addresses from an association; sctp_bindx() will
802 * reject such an attempt with EINVAL.
804 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
805 * additional addresses with an endpoint after calling bind(). Or use
806 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
807 * socket is associated with so that no new association accepted will be
808 * associated with those addresses. If the endpoint supports dynamic
809 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
810 * endpoint to send the appropriate message to the peer to change the
811 * peers address lists.
813 * Adding and removing addresses from a connected association is
814 * optional functionality. Implementations that do not support this
815 * functionality should return EOPNOTSUPP.
817 * Basically do nothing but copying the addresses from user to kernel
818 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
819 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
821 * We don't use copy_from_user() for optimization: we first do the
822 * sanity checks (buffer size -fast- and access check-healthy
823 * pointer); if all of those succeed, then we can alloc the memory
824 * (expensive operation) needed to copy the data to kernel. Then we do
825 * the copying without checking the user space area
826 * (__copy_from_user()).
828 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
831 * sk The sk of the socket
832 * addrs The pointer to the addresses in user land
833 * addrssize Size of the addrs buffer
834 * op Operation to perform (add or remove, see the flags of
837 * Returns 0 if ok, <0 errno code on error.
839 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
840 struct sockaddr __user *addrs,
841 int addrs_size, int op)
843 struct sockaddr *kaddrs;
847 struct sockaddr *sa_addr;
851 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
852 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
854 if (unlikely(addrs_size <= 0))
857 /* Check the user passed a healthy pointer. */
858 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
861 /* Alloc space for the address array in kernel memory. */
862 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
863 if (unlikely(!kaddrs))
866 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
871 /* Walk through the addrs buffer and count the number of addresses. */
873 while (walk_size < addrs_size) {
874 sa_addr = (struct sockaddr *)addr_buf;
875 af = sctp_get_af_specific(sa_addr->sa_family);
877 /* If the address family is not supported or if this address
878 * causes the address buffer to overflow return EINVAL.
880 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
885 addr_buf += af->sockaddr_len;
886 walk_size += af->sockaddr_len;
891 case SCTP_BINDX_ADD_ADDR:
892 err = sctp_bindx_add(sk, kaddrs, addrcnt);
895 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
898 case SCTP_BINDX_REM_ADDR:
899 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
902 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
916 /* API 3.1.4 close() - UDP Style Syntax
917 * Applications use close() to perform graceful shutdown (as described in
918 * Section 10.1 of [SCTP]) on ALL the associations currently represented
919 * by a UDP-style socket.
923 * ret = close(int sd);
925 * sd - the socket descriptor of the associations to be closed.
927 * To gracefully shutdown a specific association represented by the
928 * UDP-style socket, an application should use the sendmsg() call,
929 * passing no user data, but including the appropriate flag in the
930 * ancillary data (see Section xxxx).
932 * If sd in the close() call is a branched-off socket representing only
933 * one association, the shutdown is performed on that association only.
935 * 4.1.6 close() - TCP Style Syntax
937 * Applications use close() to gracefully close down an association.
943 * sd - the socket descriptor of the association to be closed.
945 * After an application calls close() on a socket descriptor, no further
946 * socket operations will succeed on that descriptor.
948 * API 7.1.4 SO_LINGER
950 * An application using the TCP-style socket can use this option to
951 * perform the SCTP ABORT primitive. The linger option structure is:
954 * int l_onoff; // option on/off
955 * int l_linger; // linger time
958 * To enable the option, set l_onoff to 1. If the l_linger value is set
959 * to 0, calling close() is the same as the ABORT primitive. If the
960 * value is set to a negative value, the setsockopt() call will return
961 * an error. If the value is set to a positive value linger_time, the
962 * close() can be blocked for at most linger_time ms. If the graceful
963 * shutdown phase does not finish during this period, close() will
964 * return but the graceful shutdown phase continues in the system.
966 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
968 struct sctp_endpoint *ep;
969 struct sctp_association *asoc;
970 struct list_head *pos, *temp;
972 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
975 sk->sk_shutdown = SHUTDOWN_MASK;
977 ep = sctp_sk(sk)->ep;
979 /* Walk all associations on a socket, not on an endpoint. */
980 list_for_each_safe(pos, temp, &ep->asocs) {
981 asoc = list_entry(pos, struct sctp_association, asocs);
983 if (sctp_style(sk, TCP)) {
984 /* A closed association can still be in the list if
985 * it belongs to a TCP-style listening socket that is
986 * not yet accepted. If so, free it. If not, send an
987 * ABORT or SHUTDOWN based on the linger options.
989 if (sctp_state(asoc, CLOSED)) {
990 sctp_unhash_established(asoc);
991 sctp_association_free(asoc);
993 } else if (sock_flag(sk, SOCK_LINGER) &&
995 sctp_primitive_ABORT(asoc, NULL);
997 sctp_primitive_SHUTDOWN(asoc, NULL);
999 sctp_primitive_SHUTDOWN(asoc, NULL);
1002 /* Clean up any skbs sitting on the receive queue. */
1003 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1004 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1006 /* On a TCP-style socket, block for at most linger_time if set. */
1007 if (sctp_style(sk, TCP) && timeout)
1008 sctp_wait_for_close(sk, timeout);
1010 /* This will run the backlog queue. */
1011 sctp_release_sock(sk);
1013 /* Supposedly, no process has access to the socket, but
1014 * the net layers still may.
1016 sctp_local_bh_disable();
1017 sctp_bh_lock_sock(sk);
1019 /* Hold the sock, since sk_common_release() will put sock_put()
1020 * and we have just a little more cleanup.
1023 sk_common_release(sk);
1025 sctp_bh_unlock_sock(sk);
1026 sctp_local_bh_enable();
1030 SCTP_DBG_OBJCNT_DEC(sock);
1033 /* Handle EPIPE error. */
1034 static int sctp_error(struct sock *sk, int flags, int err)
1037 err = sock_error(sk) ? : -EPIPE;
1038 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1039 send_sig(SIGPIPE, current, 0);
1043 /* API 3.1.3 sendmsg() - UDP Style Syntax
1045 * An application uses sendmsg() and recvmsg() calls to transmit data to
1046 * and receive data from its peer.
1048 * ssize_t sendmsg(int socket, const struct msghdr *message,
1051 * socket - the socket descriptor of the endpoint.
1052 * message - pointer to the msghdr structure which contains a single
1053 * user message and possibly some ancillary data.
1055 * See Section 5 for complete description of the data
1058 * flags - flags sent or received with the user message, see Section
1059 * 5 for complete description of the flags.
1061 * Note: This function could use a rewrite especially when explicit
1062 * connect support comes in.
1064 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1066 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1068 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1069 struct msghdr *msg, size_t msg_len)
1071 struct sctp_sock *sp;
1072 struct sctp_endpoint *ep;
1073 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1074 struct sctp_transport *transport, *chunk_tp;
1075 struct sctp_chunk *chunk;
1077 struct sockaddr *msg_name = NULL;
1078 struct sctp_sndrcvinfo default_sinfo = { 0 };
1079 struct sctp_sndrcvinfo *sinfo;
1080 struct sctp_initmsg *sinit;
1081 sctp_assoc_t associd = 0;
1082 sctp_cmsgs_t cmsgs = { NULL };
1086 __u16 sinfo_flags = 0;
1087 struct sctp_datamsg *datamsg;
1088 struct list_head *pos;
1089 int msg_flags = msg->msg_flags;
1091 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1098 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1100 /* We cannot send a message over a TCP-style listening socket. */
1101 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1106 /* Parse out the SCTP CMSGs. */
1107 err = sctp_msghdr_parse(msg, &cmsgs);
1110 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1114 /* Fetch the destination address for this packet. This
1115 * address only selects the association--it is not necessarily
1116 * the address we will send to.
1117 * For a peeled-off socket, msg_name is ignored.
1119 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1120 int msg_namelen = msg->msg_namelen;
1122 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1127 if (msg_namelen > sizeof(to))
1128 msg_namelen = sizeof(to);
1129 memcpy(&to, msg->msg_name, msg_namelen);
1130 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1132 to.v4.sin_addr.s_addr, to.v4.sin_port);
1134 to.v4.sin_port = ntohs(to.v4.sin_port);
1135 msg_name = msg->msg_name;
1141 /* Did the user specify SNDRCVINFO? */
1143 sinfo_flags = sinfo->sinfo_flags;
1144 associd = sinfo->sinfo_assoc_id;
1147 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1148 msg_len, sinfo_flags);
1150 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1151 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1156 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1157 * length messages when MSG_EOF|MSG_ABORT is not set.
1158 * If MSG_ABORT is set, the message length could be non zero with
1159 * the msg_iov set to the user abort reason.
1161 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1162 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1167 /* If MSG_ADDR_OVER is set, there must be an address
1168 * specified in msg_name.
1170 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1177 SCTP_DEBUG_PRINTK("About to look up association.\n");
1181 /* If a msg_name has been specified, assume this is to be used. */
1183 /* Look for a matching association on the endpoint. */
1184 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1186 /* If we could not find a matching association on the
1187 * endpoint, make sure that it is not a TCP-style
1188 * socket that already has an association or there is
1189 * no peeled-off association on another socket.
1191 if ((sctp_style(sk, TCP) &&
1192 sctp_sstate(sk, ESTABLISHED)) ||
1193 sctp_endpoint_is_peeled_off(ep, &to)) {
1194 err = -EADDRNOTAVAIL;
1199 asoc = sctp_id2assoc(sk, associd);
1207 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1209 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1210 * socket that has an association in CLOSED state. This can
1211 * happen when an accepted socket has an association that is
1214 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1219 if (sinfo_flags & MSG_EOF) {
1220 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1222 sctp_primitive_SHUTDOWN(asoc, NULL);
1226 if (sinfo_flags & MSG_ABORT) {
1227 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1228 sctp_primitive_ABORT(asoc, msg);
1234 /* Do we need to create the association? */
1236 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1238 if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
1243 /* Check for invalid stream against the stream counts,
1244 * either the default or the user specified stream counts.
1247 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1248 /* Check against the defaults. */
1249 if (sinfo->sinfo_stream >=
1250 sp->initmsg.sinit_num_ostreams) {
1255 /* Check against the requested. */
1256 if (sinfo->sinfo_stream >=
1257 sinit->sinit_num_ostreams) {
1265 * API 3.1.2 bind() - UDP Style Syntax
1266 * If a bind() or sctp_bindx() is not called prior to a
1267 * sendmsg() call that initiates a new association, the
1268 * system picks an ephemeral port and will choose an address
1269 * set equivalent to binding with a wildcard address.
1271 if (!ep->base.bind_addr.port) {
1272 if (sctp_autobind(sk)) {
1278 scope = sctp_scope(&to);
1279 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1286 /* If the SCTP_INIT ancillary data is specified, set all
1287 * the association init values accordingly.
1290 if (sinit->sinit_num_ostreams) {
1291 asoc->c.sinit_num_ostreams =
1292 sinit->sinit_num_ostreams;
1294 if (sinit->sinit_max_instreams) {
1295 asoc->c.sinit_max_instreams =
1296 sinit->sinit_max_instreams;
1298 if (sinit->sinit_max_attempts) {
1299 asoc->max_init_attempts
1300 = sinit->sinit_max_attempts;
1302 if (sinit->sinit_max_init_timeo) {
1303 asoc->max_init_timeo =
1304 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1308 /* Prime the peer's transport structures. */
1309 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1314 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1321 /* ASSERT: we have a valid association at this point. */
1322 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1325 /* If the user didn't specify SNDRCVINFO, make up one with
1328 default_sinfo.sinfo_stream = asoc->default_stream;
1329 default_sinfo.sinfo_flags = asoc->default_flags;
1330 default_sinfo.sinfo_ppid = asoc->default_ppid;
1331 default_sinfo.sinfo_context = asoc->default_context;
1332 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1333 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1334 sinfo = &default_sinfo;
1337 /* API 7.1.7, the sndbuf size per association bounds the
1338 * maximum size of data that can be sent in a single send call.
1340 if (msg_len > sk->sk_sndbuf) {
1345 /* If fragmentation is disabled and the message length exceeds the
1346 * association fragmentation point, return EMSGSIZE. The I-D
1347 * does not specify what this error is, but this looks like
1350 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1356 /* Check for invalid stream. */
1357 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1363 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1364 if (!sctp_wspace(asoc)) {
1365 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1370 /* If an address is passed with the sendto/sendmsg call, it is used
1371 * to override the primary destination address in the TCP model, or
1372 * when MSG_ADDR_OVER flag is set in the UDP model.
1374 if ((sctp_style(sk, TCP) && msg_name) ||
1375 (sinfo_flags & MSG_ADDR_OVER)) {
1376 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1384 /* Auto-connect, if we aren't connected already. */
1385 if (sctp_state(asoc, CLOSED)) {
1386 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1389 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1392 /* Break the message into multiple chunks of maximum size. */
1393 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1399 /* Now send the (possibly) fragmented message. */
1400 list_for_each(pos, &datamsg->chunks) {
1401 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1402 sctp_datamsg_track(chunk);
1404 /* Do accounting for the write space. */
1405 sctp_set_owner_w(chunk);
1407 chunk->transport = chunk_tp;
1409 /* Send it to the lower layers. Note: all chunks
1410 * must either fail or succeed. The lower layer
1411 * works that way today. Keep it that way or this
1414 err = sctp_primitive_SEND(asoc, chunk);
1415 /* Did the lower layer accept the chunk? */
1417 sctp_chunk_free(chunk);
1418 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1421 sctp_datamsg_free(datamsg);
1427 /* If we are already past ASSOCIATE, the lower
1428 * layers are responsible for association cleanup.
1434 sctp_association_free(asoc);
1436 sctp_release_sock(sk);
1439 return sctp_error(sk, msg_flags, err);
1446 err = sock_error(sk);
1456 /* This is an extended version of skb_pull() that removes the data from the
1457 * start of a skb even when data is spread across the list of skb's in the
1458 * frag_list. len specifies the total amount of data that needs to be removed.
1459 * when 'len' bytes could be removed from the skb, it returns 0.
1460 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1461 * could not be removed.
1463 static int sctp_skb_pull(struct sk_buff *skb, int len)
1465 struct sk_buff *list;
1466 int skb_len = skb_headlen(skb);
1469 if (len <= skb_len) {
1470 __skb_pull(skb, len);
1474 __skb_pull(skb, skb_len);
1476 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1477 rlen = sctp_skb_pull(list, len);
1478 skb->len -= (len-rlen);
1479 skb->data_len -= (len-rlen);
1490 /* API 3.1.3 recvmsg() - UDP Style Syntax
1492 * ssize_t recvmsg(int socket, struct msghdr *message,
1495 * socket - the socket descriptor of the endpoint.
1496 * message - pointer to the msghdr structure which contains a single
1497 * user message and possibly some ancillary data.
1499 * See Section 5 for complete description of the data
1502 * flags - flags sent or received with the user message, see Section
1503 * 5 for complete description of the flags.
1505 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1507 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1508 struct msghdr *msg, size_t len, int noblock,
1509 int flags, int *addr_len)
1511 struct sctp_ulpevent *event = NULL;
1512 struct sctp_sock *sp = sctp_sk(sk);
1513 struct sk_buff *skb;
1518 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1519 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1520 "len", len, "knoblauch", noblock,
1521 "flags", flags, "addr_len", addr_len);
1525 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1530 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1534 /* Get the total length of the skb including any skb's in the
1543 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1545 event = sctp_skb2event(skb);
1550 sock_recv_timestamp(msg, sk, skb);
1551 if (sctp_ulpevent_is_notification(event)) {
1552 msg->msg_flags |= MSG_NOTIFICATION;
1553 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1555 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1558 /* Check if we allow SCTP_SNDRCVINFO. */
1559 if (sp->subscribe.sctp_data_io_event)
1560 sctp_ulpevent_read_sndrcvinfo(event, msg);
1562 /* FIXME: we should be calling IP/IPv6 layers. */
1563 if (sk->sk_protinfo.af_inet.cmsg_flags)
1564 ip_cmsg_recv(msg, skb);
1569 /* If skb's length exceeds the user's buffer, update the skb and
1570 * push it back to the receive_queue so that the next call to
1571 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1573 if (skb_len > copied) {
1574 msg->msg_flags &= ~MSG_EOR;
1575 if (flags & MSG_PEEK)
1577 sctp_skb_pull(skb, copied);
1578 skb_queue_head(&sk->sk_receive_queue, skb);
1580 /* When only partial message is copied to the user, increase
1581 * rwnd by that amount. If all the data in the skb is read,
1582 * rwnd is updated when the event is freed.
1584 sctp_assoc_rwnd_increase(event->asoc, copied);
1586 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1587 (event->msg_flags & MSG_EOR))
1588 msg->msg_flags |= MSG_EOR;
1590 msg->msg_flags &= ~MSG_EOR;
1593 if (flags & MSG_PEEK) {
1594 /* Release the skb reference acquired after peeking the skb in
1595 * sctp_skb_recv_datagram().
1599 /* Free the event which includes releasing the reference to
1600 * the owner of the skb, freeing the skb and updating the
1603 sctp_ulpevent_free(event);
1606 sctp_release_sock(sk);
1610 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1612 * This option is a on/off flag. If enabled no SCTP message
1613 * fragmentation will be performed. Instead if a message being sent
1614 * exceeds the current PMTU size, the message will NOT be sent and
1615 * instead a error will be indicated to the user.
1617 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1618 char __user *optval, int optlen)
1622 if (optlen < sizeof(int))
1625 if (get_user(val, (int __user *)optval))
1628 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1633 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1636 if (optlen != sizeof(struct sctp_event_subscribe))
1638 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1643 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1645 * This socket option is applicable to the UDP-style socket only. When
1646 * set it will cause associations that are idle for more than the
1647 * specified number of seconds to automatically close. An association
1648 * being idle is defined an association that has NOT sent or received
1649 * user data. The special value of '0' indicates that no automatic
1650 * close of any associations should be performed. The option expects an
1651 * integer defining the number of seconds of idle time before an
1652 * association is closed.
1654 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1657 struct sctp_sock *sp = sctp_sk(sk);
1659 /* Applicable to UDP-style socket only */
1660 if (sctp_style(sk, TCP))
1662 if (optlen != sizeof(int))
1664 if (copy_from_user(&sp->autoclose, optval, optlen))
1667 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1671 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1673 * Applications can enable or disable heartbeats for any peer address of
1674 * an association, modify an address's heartbeat interval, force a
1675 * heartbeat to be sent immediately, and adjust the address's maximum
1676 * number of retransmissions sent before an address is considered
1677 * unreachable. The following structure is used to access and modify an
1678 * address's parameters:
1680 * struct sctp_paddrparams {
1681 * sctp_assoc_t spp_assoc_id;
1682 * struct sockaddr_storage spp_address;
1683 * uint32_t spp_hbinterval;
1684 * uint16_t spp_pathmaxrxt;
1687 * spp_assoc_id - (UDP style socket) This is filled in the application,
1688 * and identifies the association for this query.
1689 * spp_address - This specifies which address is of interest.
1690 * spp_hbinterval - This contains the value of the heartbeat interval,
1691 * in milliseconds. A value of 0, when modifying the
1692 * parameter, specifies that the heartbeat on this
1693 * address should be disabled. A value of UINT32_MAX
1694 * (4294967295), when modifying the parameter,
1695 * specifies that a heartbeat should be sent
1696 * immediately to the peer address, and the current
1697 * interval should remain unchanged.
1698 * spp_pathmaxrxt - This contains the maximum number of
1699 * retransmissions before this address shall be
1700 * considered unreachable.
1702 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1703 char __user *optval, int optlen)
1705 struct sctp_paddrparams params;
1706 struct sctp_transport *trans;
1709 if (optlen != sizeof(struct sctp_paddrparams))
1711 if (copy_from_user(¶ms, optval, optlen))
1715 * API 7. Socket Options (setting the default value for the endpoint)
1716 * All options that support specific settings on an association by
1717 * filling in either an association id variable or a sockaddr_storage
1718 * SHOULD also support setting of the same value for the entire endpoint
1719 * (i.e. future associations). To accomplish this the following logic is
1720 * used when setting one of these options:
1722 * c) If neither the sockaddr_storage or association identification is
1723 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1724 * the association identification is 0, the settings are a default
1725 * and to be applied to the endpoint (all future associations).
1728 /* update default value for endpoint (all future associations) */
1729 if (!params.spp_assoc_id &&
1730 sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
1731 /* Manual heartbeat on an endpoint is invalid. */
1732 if (0xffffffff == params.spp_hbinterval)
1734 else if (params.spp_hbinterval)
1735 sctp_sk(sk)->paddrparam.spp_hbinterval =
1736 params.spp_hbinterval;
1737 if (params.spp_pathmaxrxt)
1738 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
1739 params.spp_pathmaxrxt;
1743 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
1744 params.spp_assoc_id);
1748 /* Applications can enable or disable heartbeats for any peer address
1749 * of an association, modify an address's heartbeat interval, force a
1750 * heartbeat to be sent immediately, and adjust the address's maximum
1751 * number of retransmissions sent before an address is considered
1754 * The value of the heartbeat interval, in milliseconds. A value of
1755 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1756 * that a heartbeat should be sent immediately to the peer address,
1757 * and the current interval should remain unchanged.
1759 if (0xffffffff == params.spp_hbinterval) {
1760 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1764 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1765 * when modifying the parameter, specifies that the heartbeat on this
1766 * address should be disabled.
1768 if (params.spp_hbinterval) {
1769 trans->hb_allowed = 1;
1770 trans->hb_interval =
1771 msecs_to_jiffies(params.spp_hbinterval);
1773 trans->hb_allowed = 0;
1776 /* spp_pathmaxrxt contains the maximum number of retransmissions
1777 * before this address shall be considered unreachable.
1779 if (params.spp_pathmaxrxt)
1780 trans->max_retrans = params.spp_pathmaxrxt;
1785 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1787 * Applications can specify protocol parameters for the default association
1788 * initialization. The option name argument to setsockopt() and getsockopt()
1791 * Setting initialization parameters is effective only on an unconnected
1792 * socket (for UDP-style sockets only future associations are effected
1793 * by the change). With TCP-style sockets, this option is inherited by
1794 * sockets derived from a listener socket.
1796 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1798 struct sctp_initmsg sinit;
1799 struct sctp_sock *sp = sctp_sk(sk);
1801 if (optlen != sizeof(struct sctp_initmsg))
1803 if (copy_from_user(&sinit, optval, optlen))
1806 if (sinit.sinit_num_ostreams)
1807 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1808 if (sinit.sinit_max_instreams)
1809 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1810 if (sinit.sinit_max_attempts)
1811 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1812 if (sinit.sinit_max_init_timeo)
1813 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1819 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1821 * Applications that wish to use the sendto() system call may wish to
1822 * specify a default set of parameters that would normally be supplied
1823 * through the inclusion of ancillary data. This socket option allows
1824 * such an application to set the default sctp_sndrcvinfo structure.
1825 * The application that wishes to use this socket option simply passes
1826 * in to this call the sctp_sndrcvinfo structure defined in Section
1827 * 5.2.2) The input parameters accepted by this call include
1828 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1829 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1830 * to this call if the caller is using the UDP model.
1832 static int sctp_setsockopt_default_send_param(struct sock *sk,
1833 char __user *optval, int optlen)
1835 struct sctp_sndrcvinfo info;
1836 struct sctp_association *asoc;
1837 struct sctp_sock *sp = sctp_sk(sk);
1839 if (optlen != sizeof(struct sctp_sndrcvinfo))
1841 if (copy_from_user(&info, optval, optlen))
1844 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1845 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1849 asoc->default_stream = info.sinfo_stream;
1850 asoc->default_flags = info.sinfo_flags;
1851 asoc->default_ppid = info.sinfo_ppid;
1852 asoc->default_context = info.sinfo_context;
1853 asoc->default_timetolive = info.sinfo_timetolive;
1855 sp->default_stream = info.sinfo_stream;
1856 sp->default_flags = info.sinfo_flags;
1857 sp->default_ppid = info.sinfo_ppid;
1858 sp->default_context = info.sinfo_context;
1859 sp->default_timetolive = info.sinfo_timetolive;
1865 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1867 * Requests that the local SCTP stack use the enclosed peer address as
1868 * the association primary. The enclosed address must be one of the
1869 * association peer's addresses.
1871 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1874 struct sctp_prim prim;
1875 struct sctp_transport *trans;
1877 if (optlen != sizeof(struct sctp_prim))
1880 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1883 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1887 sctp_assoc_set_primary(trans->asoc, trans);
1893 * 7.1.5 SCTP_NODELAY
1895 * Turn on/off any Nagle-like algorithm. This means that packets are
1896 * generally sent as soon as possible and no unnecessary delays are
1897 * introduced, at the cost of more packets in the network. Expects an
1898 * integer boolean flag.
1900 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1905 if (optlen < sizeof(int))
1907 if (get_user(val, (int __user *)optval))
1910 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1916 * 7.1.1 SCTP_RTOINFO
1918 * The protocol parameters used to initialize and bound retransmission
1919 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1920 * and modify these parameters.
1921 * All parameters are time values, in milliseconds. A value of 0, when
1922 * modifying the parameters, indicates that the current value should not
1926 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1927 struct sctp_rtoinfo rtoinfo;
1928 struct sctp_association *asoc;
1930 if (optlen != sizeof (struct sctp_rtoinfo))
1933 if (copy_from_user(&rtoinfo, optval, optlen))
1936 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1938 /* Set the values to the specific association */
1939 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1943 if (rtoinfo.srto_initial != 0)
1945 msecs_to_jiffies(rtoinfo.srto_initial);
1946 if (rtoinfo.srto_max != 0)
1947 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1948 if (rtoinfo.srto_min != 0)
1949 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1951 /* If there is no association or the association-id = 0
1952 * set the values to the endpoint.
1954 struct sctp_sock *sp = sctp_sk(sk);
1956 if (rtoinfo.srto_initial != 0)
1957 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1958 if (rtoinfo.srto_max != 0)
1959 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1960 if (rtoinfo.srto_min != 0)
1961 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1969 * 7.1.2 SCTP_ASSOCINFO
1971 * This option is used to tune the the maximum retransmission attempts
1972 * of the association.
1973 * Returns an error if the new association retransmission value is
1974 * greater than the sum of the retransmission value of the peer.
1975 * See [SCTP] for more information.
1978 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1981 struct sctp_assocparams assocparams;
1982 struct sctp_association *asoc;
1984 if (optlen != sizeof(struct sctp_assocparams))
1986 if (copy_from_user(&assocparams, optval, optlen))
1989 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1991 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1994 /* Set the values to the specific association */
1996 if (assocparams.sasoc_asocmaxrxt != 0)
1997 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1998 if (assocparams.sasoc_cookie_life != 0) {
1999 asoc->cookie_life.tv_sec =
2000 assocparams.sasoc_cookie_life / 1000;
2001 asoc->cookie_life.tv_usec =
2002 (assocparams.sasoc_cookie_life % 1000)
2006 /* Set the values to the endpoint */
2007 struct sctp_sock *sp = sctp_sk(sk);
2009 if (assocparams.sasoc_asocmaxrxt != 0)
2010 sp->assocparams.sasoc_asocmaxrxt =
2011 assocparams.sasoc_asocmaxrxt;
2012 if (assocparams.sasoc_cookie_life != 0)
2013 sp->assocparams.sasoc_cookie_life =
2014 assocparams.sasoc_cookie_life;
2020 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2022 * This socket option is a boolean flag which turns on or off mapped V4
2023 * addresses. If this option is turned on and the socket is type
2024 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2025 * If this option is turned off, then no mapping will be done of V4
2026 * addresses and a user will receive both PF_INET6 and PF_INET type
2027 * addresses on the socket.
2029 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2032 struct sctp_sock *sp = sctp_sk(sk);
2034 if (optlen < sizeof(int))
2036 if (get_user(val, (int __user *)optval))
2047 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2049 * This socket option specifies the maximum size to put in any outgoing
2050 * SCTP chunk. If a message is larger than this size it will be
2051 * fragmented by SCTP into the specified size. Note that the underlying
2052 * SCTP implementation may fragment into smaller sized chunks when the
2053 * PMTU of the underlying association is smaller than the value set by
2056 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2058 struct sctp_association *asoc;
2059 struct list_head *pos;
2060 struct sctp_sock *sp = sctp_sk(sk);
2063 if (optlen < sizeof(int))
2065 if (get_user(val, (int __user *)optval))
2067 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
2069 sp->user_frag = val;
2072 /* Update the frag_point of the existing associations. */
2073 list_for_each(pos, &(sp->ep->asocs)) {
2074 asoc = list_entry(pos, struct sctp_association, asocs);
2075 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2084 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2086 * Requests that the peer mark the enclosed address as the association
2087 * primary. The enclosed address must be one of the association's
2088 * locally bound addresses. The following structure is used to make a
2089 * set primary request:
2091 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2094 struct sctp_sock *sp;
2095 struct sctp_endpoint *ep;
2096 struct sctp_association *asoc = NULL;
2097 struct sctp_setpeerprim prim;
2098 struct sctp_chunk *chunk;
2104 if (!sctp_addip_enable)
2107 if (optlen != sizeof(struct sctp_setpeerprim))
2110 if (copy_from_user(&prim, optval, optlen))
2113 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2117 if (!asoc->peer.asconf_capable)
2120 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2123 if (!sctp_state(asoc, ESTABLISHED))
2126 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2127 return -EADDRNOTAVAIL;
2129 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2130 chunk = sctp_make_asconf_set_prim(asoc,
2131 (union sctp_addr *)&prim.sspp_addr);
2135 err = sctp_send_asconf(asoc, chunk);
2137 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2142 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2147 if (optlen < sizeof(__u32))
2149 if (copy_from_user(&val, optval, sizeof(__u32)))
2152 sctp_sk(sk)->adaption_ind = val;
2157 /* API 6.2 setsockopt(), getsockopt()
2159 * Applications use setsockopt() and getsockopt() to set or retrieve
2160 * socket options. Socket options are used to change the default
2161 * behavior of sockets calls. They are described in Section 7.
2165 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2166 * int __user *optlen);
2167 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2170 * sd - the socket descript.
2171 * level - set to IPPROTO_SCTP for all SCTP options.
2172 * optname - the option name.
2173 * optval - the buffer to store the value of the option.
2174 * optlen - the size of the buffer.
2176 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2177 char __user *optval, int optlen)
2181 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2184 /* I can hardly begin to describe how wrong this is. This is
2185 * so broken as to be worse than useless. The API draft
2186 * REALLY is NOT helpful here... I am not convinced that the
2187 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2188 * are at all well-founded.
2190 if (level != SOL_SCTP) {
2191 struct sctp_af *af = sctp_sk(sk)->pf->af;
2192 retval = af->setsockopt(sk, level, optname, optval, optlen);
2199 case SCTP_SOCKOPT_BINDX_ADD:
2200 /* 'optlen' is the size of the addresses buffer. */
2201 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2202 optlen, SCTP_BINDX_ADD_ADDR);
2205 case SCTP_SOCKOPT_BINDX_REM:
2206 /* 'optlen' is the size of the addresses buffer. */
2207 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2208 optlen, SCTP_BINDX_REM_ADDR);
2211 case SCTP_DISABLE_FRAGMENTS:
2212 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2216 retval = sctp_setsockopt_events(sk, optval, optlen);
2219 case SCTP_AUTOCLOSE:
2220 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2223 case SCTP_PEER_ADDR_PARAMS:
2224 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2228 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2230 case SCTP_DEFAULT_SEND_PARAM:
2231 retval = sctp_setsockopt_default_send_param(sk, optval,
2234 case SCTP_PRIMARY_ADDR:
2235 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2237 case SCTP_SET_PEER_PRIMARY_ADDR:
2238 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2241 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2244 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2246 case SCTP_ASSOCINFO:
2247 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2249 case SCTP_I_WANT_MAPPED_V4_ADDR:
2250 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2253 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2255 case SCTP_ADAPTION_LAYER:
2256 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2260 retval = -ENOPROTOOPT;
2264 sctp_release_sock(sk);
2270 /* API 3.1.6 connect() - UDP Style Syntax
2272 * An application may use the connect() call in the UDP model to initiate an
2273 * association without sending data.
2277 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2279 * sd: the socket descriptor to have a new association added to.
2281 * nam: the address structure (either struct sockaddr_in or struct
2282 * sockaddr_in6 defined in RFC2553 [7]).
2284 * len: the size of the address.
2286 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2289 struct sctp_sock *sp;
2290 struct sctp_endpoint *ep;
2291 struct sctp_association *asoc;
2292 struct sctp_transport *transport;
2301 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2302 __FUNCTION__, sk, uaddr, addr_len);
2307 /* connect() cannot be done on a socket that is already in ESTABLISHED
2308 * state - UDP-style peeled off socket or a TCP-style socket that
2309 * is already connected.
2310 * It cannot be done even on a TCP-style listening socket.
2312 if (sctp_sstate(sk, ESTABLISHED) ||
2313 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2318 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2322 if (addr_len > sizeof(to))
2323 addr_len = sizeof(to);
2324 memcpy(&to, uaddr, addr_len);
2325 to.v4.sin_port = ntohs(to.v4.sin_port);
2327 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2329 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2336 /* If we could not find a matching association on the endpoint,
2337 * make sure that there is no peeled-off association matching the
2338 * peer address even on another socket.
2340 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2341 err = -EADDRNOTAVAIL;
2345 /* If a bind() or sctp_bindx() is not called prior to a connect()
2346 * call, the system picks an ephemeral port and will choose an address
2347 * set equivalent to binding with a wildcard address.
2349 if (!ep->base.bind_addr.port) {
2350 if (sctp_autobind(sk)) {
2356 scope = sctp_scope(&to);
2357 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2363 /* Prime the peer's transport structures. */
2364 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2366 sctp_association_free(asoc);
2369 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2371 sctp_association_free(asoc);
2375 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2377 sctp_association_free(asoc);
2381 /* Initialize sk's dport and daddr for getpeername() */
2382 inet_sk(sk)->dport = htons(asoc->peer.port);
2383 af = sctp_get_af_specific(to.sa.sa_family);
2384 af->to_sk_daddr(&to, sk);
2386 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2387 err = sctp_wait_for_connect(asoc, &timeo);
2390 sctp_release_sock(sk);
2395 /* FIXME: Write comments. */
2396 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2398 return -EOPNOTSUPP; /* STUB */
2401 /* 4.1.4 accept() - TCP Style Syntax
2403 * Applications use accept() call to remove an established SCTP
2404 * association from the accept queue of the endpoint. A new socket
2405 * descriptor will be returned from accept() to represent the newly
2406 * formed association.
2408 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2410 struct sctp_sock *sp;
2411 struct sctp_endpoint *ep;
2412 struct sock *newsk = NULL;
2413 struct sctp_association *asoc;
2422 if (!sctp_style(sk, TCP)) {
2423 error = -EOPNOTSUPP;
2427 if (!sctp_sstate(sk, LISTENING)) {
2432 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2434 error = sctp_wait_for_accept(sk, timeo);
2438 /* We treat the list of associations on the endpoint as the accept
2439 * queue and pick the first association on the list.
2441 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2443 newsk = sp->pf->create_accept_sk(sk, asoc);
2449 /* Populate the fields of the newsk from the oldsk and migrate the
2450 * asoc to the newsk.
2452 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2455 sctp_release_sock(sk);
2460 /* The SCTP ioctl handler. */
2461 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2463 return -ENOIOCTLCMD;
2466 /* This is the function which gets called during socket creation to
2467 * initialized the SCTP-specific portion of the sock.
2468 * The sock structure should already be zero-filled memory.
2470 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2472 struct sctp_endpoint *ep;
2473 struct sctp_sock *sp;
2475 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2479 /* Initialize the SCTP per socket area. */
2480 switch (sk->sk_type) {
2481 case SOCK_SEQPACKET:
2482 sp->type = SCTP_SOCKET_UDP;
2485 sp->type = SCTP_SOCKET_TCP;
2488 return -ESOCKTNOSUPPORT;
2491 /* Initialize default send parameters. These parameters can be
2492 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2494 sp->default_stream = 0;
2495 sp->default_ppid = 0;
2496 sp->default_flags = 0;
2497 sp->default_context = 0;
2498 sp->default_timetolive = 0;
2500 /* Initialize default setup parameters. These parameters
2501 * can be modified with the SCTP_INITMSG socket option or
2502 * overridden by the SCTP_INIT CMSG.
2504 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2505 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2506 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2507 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2509 /* Initialize default RTO related parameters. These parameters can
2510 * be modified for with the SCTP_RTOINFO socket option.
2512 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2513 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2514 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2516 /* Initialize default association related parameters. These parameters
2517 * can be modified with the SCTP_ASSOCINFO socket option.
2519 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2520 sp->assocparams.sasoc_number_peer_destinations = 0;
2521 sp->assocparams.sasoc_peer_rwnd = 0;
2522 sp->assocparams.sasoc_local_rwnd = 0;
2523 sp->assocparams.sasoc_cookie_life =
2524 jiffies_to_msecs(sctp_valid_cookie_life);
2526 /* Initialize default event subscriptions. By default, all the
2529 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2531 /* Default Peer Address Parameters. These defaults can
2532 * be modified via SCTP_PEER_ADDR_PARAMS
2534 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2535 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2537 /* If enabled no SCTP message fragmentation will be performed.
2538 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2540 sp->disable_fragments = 0;
2542 /* Turn on/off any Nagle-like algorithm. */
2545 /* Enable by default. */
2548 /* Auto-close idle associations after the configured
2549 * number of seconds. A value of 0 disables this
2550 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2551 * for UDP-style sockets only.
2555 /* User specified fragmentation limit. */
2558 sp->adaption_ind = 0;
2560 sp->pf = sctp_get_pf_specific(sk->sk_family);
2562 /* Control variables for partial data delivery. */
2564 skb_queue_head_init(&sp->pd_lobby);
2566 /* Create a per socket endpoint structure. Even if we
2567 * change the data structure relationships, this may still
2568 * be useful for storing pre-connect address information.
2570 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2577 SCTP_DBG_OBJCNT_INC(sock);
2581 /* Cleanup any SCTP per socket resources. */
2582 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2584 struct sctp_endpoint *ep;
2586 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2588 /* Release our hold on the endpoint. */
2589 ep = sctp_sk(sk)->ep;
2590 sctp_endpoint_free(ep);
2595 /* API 4.1.7 shutdown() - TCP Style Syntax
2596 * int shutdown(int socket, int how);
2598 * sd - the socket descriptor of the association to be closed.
2599 * how - Specifies the type of shutdown. The values are
2602 * Disables further receive operations. No SCTP
2603 * protocol action is taken.
2605 * Disables further send operations, and initiates
2606 * the SCTP shutdown sequence.
2608 * Disables further send and receive operations
2609 * and initiates the SCTP shutdown sequence.
2611 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2613 struct sctp_endpoint *ep;
2614 struct sctp_association *asoc;
2616 if (!sctp_style(sk, TCP))
2619 if (how & SEND_SHUTDOWN) {
2620 ep = sctp_sk(sk)->ep;
2621 if (!list_empty(&ep->asocs)) {
2622 asoc = list_entry(ep->asocs.next,
2623 struct sctp_association, asocs);
2624 sctp_primitive_SHUTDOWN(asoc, NULL);
2629 /* 7.2.1 Association Status (SCTP_STATUS)
2631 * Applications can retrieve current status information about an
2632 * association, including association state, peer receiver window size,
2633 * number of unacked data chunks, and number of data chunks pending
2634 * receipt. This information is read-only.
2636 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2637 char __user *optval,
2640 struct sctp_status status;
2641 struct sctp_association *asoc = NULL;
2642 struct sctp_transport *transport;
2643 sctp_assoc_t associd;
2646 if (len != sizeof(status)) {
2651 if (copy_from_user(&status, optval, sizeof(status))) {
2656 associd = status.sstat_assoc_id;
2657 asoc = sctp_id2assoc(sk, associd);
2663 transport = asoc->peer.primary_path;
2665 status.sstat_assoc_id = sctp_assoc2id(asoc);
2666 status.sstat_state = asoc->state;
2667 status.sstat_rwnd = asoc->peer.rwnd;
2668 status.sstat_unackdata = asoc->unack_data;
2670 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2671 status.sstat_instrms = asoc->c.sinit_max_instreams;
2672 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2673 status.sstat_fragmentation_point = asoc->frag_point;
2674 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2675 memcpy(&status.sstat_primary.spinfo_address,
2676 &(transport->ipaddr), sizeof(union sctp_addr));
2677 /* Map ipv4 address into v4-mapped-on-v6 address. */
2678 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2679 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2680 status.sstat_primary.spinfo_state = transport->active;
2681 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2682 status.sstat_primary.spinfo_srtt = transport->srtt;
2683 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2684 status.sstat_primary.spinfo_mtu = transport->pmtu;
2686 if (put_user(len, optlen)) {
2691 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
2692 len, status.sstat_state, status.sstat_rwnd,
2693 status.sstat_assoc_id);
2695 if (copy_to_user(optval, &status, len)) {
2705 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2707 * Applications can retrieve information about a specific peer address
2708 * of an association, including its reachability state, congestion
2709 * window, and retransmission timer values. This information is
2712 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2713 char __user *optval,
2716 struct sctp_paddrinfo pinfo;
2717 struct sctp_transport *transport;
2720 if (len != sizeof(pinfo)) {
2725 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2730 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2731 pinfo.spinfo_assoc_id);
2735 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2736 pinfo.spinfo_state = transport->active;
2737 pinfo.spinfo_cwnd = transport->cwnd;
2738 pinfo.spinfo_srtt = transport->srtt;
2739 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2740 pinfo.spinfo_mtu = transport->pmtu;
2742 if (put_user(len, optlen)) {
2747 if (copy_to_user(optval, &pinfo, len)) {
2756 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2758 * This option is a on/off flag. If enabled no SCTP message
2759 * fragmentation will be performed. Instead if a message being sent
2760 * exceeds the current PMTU size, the message will NOT be sent and
2761 * instead a error will be indicated to the user.
2763 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2764 char __user *optval, int __user *optlen)
2768 if (len < sizeof(int))
2772 val = (sctp_sk(sk)->disable_fragments == 1);
2773 if (put_user(len, optlen))
2775 if (copy_to_user(optval, &val, len))
2780 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2782 * This socket option is used to specify various notifications and
2783 * ancillary data the user wishes to receive.
2785 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2788 if (len != sizeof(struct sctp_event_subscribe))
2790 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2795 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2797 * This socket option is applicable to the UDP-style socket only. When
2798 * set it will cause associations that are idle for more than the
2799 * specified number of seconds to automatically close. An association
2800 * being idle is defined an association that has NOT sent or received
2801 * user data. The special value of '0' indicates that no automatic
2802 * close of any associations should be performed. The option expects an
2803 * integer defining the number of seconds of idle time before an
2804 * association is closed.
2806 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2808 /* Applicable to UDP-style socket only */
2809 if (sctp_style(sk, TCP))
2811 if (len != sizeof(int))
2813 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2818 /* Helper routine to branch off an association to a new socket. */
2819 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2820 struct socket **sockp)
2822 struct sock *sk = asoc->base.sk;
2823 struct socket *sock;
2826 /* An association cannot be branched off from an already peeled-off
2827 * socket, nor is this supported for tcp style sockets.
2829 if (!sctp_style(sk, UDP))
2832 /* Create a new socket. */
2833 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2837 /* Populate the fields of the newsk from the oldsk and migrate the
2838 * asoc to the newsk.
2840 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2846 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2848 sctp_peeloff_arg_t peeloff;
2849 struct socket *newsock;
2851 struct sctp_association *asoc;
2853 if (len != sizeof(sctp_peeloff_arg_t))
2855 if (copy_from_user(&peeloff, optval, len))
2858 asoc = sctp_id2assoc(sk, peeloff.associd);
2864 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2866 retval = sctp_do_peeloff(asoc, &newsock);
2870 /* Map the socket to an unused fd that can be returned to the user. */
2871 retval = sock_map_fd(newsock);
2873 sock_release(newsock);
2877 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2878 __FUNCTION__, sk, asoc, newsock->sk, retval);
2880 /* Return the fd mapped to the new socket. */
2881 peeloff.sd = retval;
2882 if (copy_to_user(optval, &peeloff, len))
2889 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2891 * Applications can enable or disable heartbeats for any peer address of
2892 * an association, modify an address's heartbeat interval, force a
2893 * heartbeat to be sent immediately, and adjust the address's maximum
2894 * number of retransmissions sent before an address is considered
2895 * unreachable. The following structure is used to access and modify an
2896 * address's parameters:
2898 * struct sctp_paddrparams {
2899 * sctp_assoc_t spp_assoc_id;
2900 * struct sockaddr_storage spp_address;
2901 * uint32_t spp_hbinterval;
2902 * uint16_t spp_pathmaxrxt;
2905 * spp_assoc_id - (UDP style socket) This is filled in the application,
2906 * and identifies the association for this query.
2907 * spp_address - This specifies which address is of interest.
2908 * spp_hbinterval - This contains the value of the heartbeat interval,
2909 * in milliseconds. A value of 0, when modifying the
2910 * parameter, specifies that the heartbeat on this
2911 * address should be disabled. A value of UINT32_MAX
2912 * (4294967295), when modifying the parameter,
2913 * specifies that a heartbeat should be sent
2914 * immediately to the peer address, and the current
2915 * interval should remain unchanged.
2916 * spp_pathmaxrxt - This contains the maximum number of
2917 * retransmissions before this address shall be
2918 * considered unreachable.
2920 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2921 char __user *optval, int __user *optlen)
2923 struct sctp_paddrparams params;
2924 struct sctp_transport *trans;
2926 if (len != sizeof(struct sctp_paddrparams))
2928 if (copy_from_user(¶ms, optval, len))
2931 /* If no association id is specified retrieve the default value
2932 * for the endpoint that will be used for all future associations
2934 if (!params.spp_assoc_id &&
2935 sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
2936 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
2937 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
2942 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2943 params.spp_assoc_id);
2947 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2948 * when modifying the parameter, specifies that the heartbeat on this
2949 * address should be disabled.
2951 if (!trans->hb_allowed)
2952 params.spp_hbinterval = 0;
2954 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2956 /* spp_pathmaxrxt contains the maximum number of retransmissions
2957 * before this address shall be considered unreachable.
2959 params.spp_pathmaxrxt = trans->max_retrans;
2962 if (copy_to_user(optval, ¶ms, len))
2965 if (put_user(len, optlen))
2971 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2973 * Applications can specify protocol parameters for the default association
2974 * initialization. The option name argument to setsockopt() and getsockopt()
2977 * Setting initialization parameters is effective only on an unconnected
2978 * socket (for UDP-style sockets only future associations are effected
2979 * by the change). With TCP-style sockets, this option is inherited by
2980 * sockets derived from a listener socket.
2982 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2984 if (len != sizeof(struct sctp_initmsg))
2986 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2991 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2992 char __user *optval, int __user *optlen)
2995 struct sctp_association *asoc;
2996 struct list_head *pos;
2999 if (len != sizeof(sctp_assoc_t))
3002 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3005 /* For UDP-style sockets, id specifies the association to query. */
3006 asoc = sctp_id2assoc(sk, id);
3010 list_for_each(pos, &asoc->peer.transport_addr_list) {
3017 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3018 char __user *optval, int __user *optlen)
3020 struct sctp_association *asoc;
3021 struct list_head *pos;
3023 struct sctp_getaddrs getaddrs;
3024 struct sctp_transport *from;
3026 union sctp_addr temp;
3027 struct sctp_sock *sp = sctp_sk(sk);
3030 if (len != sizeof(struct sctp_getaddrs))
3033 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3036 if (getaddrs.addr_num <= 0) return -EINVAL;
3038 /* For UDP-style sockets, id specifies the association to query. */
3039 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3043 to = (void __user *)getaddrs.addrs;
3044 list_for_each(pos, &asoc->peer.transport_addr_list) {
3045 from = list_entry(pos, struct sctp_transport, transports);
3046 memcpy(&temp, &from->ipaddr, sizeof(temp));
3047 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3048 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3049 temp.v4.sin_port = htons(temp.v4.sin_port);
3050 if (copy_to_user(to, &temp, addrlen))
3054 if (cnt >= getaddrs.addr_num) break;
3056 getaddrs.addr_num = cnt;
3057 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3063 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
3064 char __user *optval,
3068 struct sctp_bind_addr *bp;
3069 struct sctp_association *asoc;
3070 struct list_head *pos;
3071 struct sctp_sockaddr_entry *addr;
3072 rwlock_t *addr_lock;
3073 unsigned long flags;
3076 if (len != sizeof(sctp_assoc_t))
3079 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3083 * For UDP-style sockets, id specifies the association to query.
3084 * If the id field is set to the value '0' then the locally bound
3085 * addresses are returned without regard to any particular
3089 bp = &sctp_sk(sk)->ep->base.bind_addr;
3090 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3092 asoc = sctp_id2assoc(sk, id);
3095 bp = &asoc->base.bind_addr;
3096 addr_lock = &asoc->base.addr_lock;
3099 sctp_read_lock(addr_lock);
3101 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3102 * addresses from the global local address list.
3104 if (sctp_list_single_entry(&bp->address_list)) {
3105 addr = list_entry(bp->address_list.next,
3106 struct sctp_sockaddr_entry, list);
3107 if (sctp_is_any(&addr->a)) {
3108 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3109 list_for_each(pos, &sctp_local_addr_list) {
3110 addr = list_entry(pos,
3111 struct sctp_sockaddr_entry,
3113 if ((PF_INET == sk->sk_family) &&
3114 (AF_INET6 == addr->a.sa.sa_family))
3118 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3126 list_for_each(pos, &bp->address_list) {
3131 sctp_read_unlock(addr_lock);
3135 /* Helper function that copies local addresses to user and returns the number
3136 * of addresses copied.
3138 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, int max_addrs,
3141 struct list_head *pos;
3142 struct sctp_sockaddr_entry *addr;
3143 unsigned long flags;
3144 union sctp_addr temp;
3148 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3149 list_for_each(pos, &sctp_local_addr_list) {
3150 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3151 if ((PF_INET == sk->sk_family) &&
3152 (AF_INET6 == addr->a.sa.sa_family))
3154 memcpy(&temp, &addr->a, sizeof(temp));
3155 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3157 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3158 temp.v4.sin_port = htons(port);
3159 if (copy_to_user(to, &temp, addrlen)) {
3160 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3166 if (cnt >= max_addrs) break;
3168 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3173 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3174 char __user *optval, int __user *optlen)
3176 struct sctp_bind_addr *bp;
3177 struct sctp_association *asoc;
3178 struct list_head *pos;
3180 struct sctp_getaddrs getaddrs;
3181 struct sctp_sockaddr_entry *addr;
3183 union sctp_addr temp;
3184 struct sctp_sock *sp = sctp_sk(sk);
3186 rwlock_t *addr_lock;
3189 if (len != sizeof(struct sctp_getaddrs))
3192 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3195 if (getaddrs.addr_num <= 0) return -EINVAL;
3197 * For UDP-style sockets, id specifies the association to query.
3198 * If the id field is set to the value '0' then the locally bound
3199 * addresses are returned without regard to any particular
3202 if (0 == getaddrs.assoc_id) {
3203 bp = &sctp_sk(sk)->ep->base.bind_addr;
3204 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3206 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3209 bp = &asoc->base.bind_addr;
3210 addr_lock = &asoc->base.addr_lock;
3213 to = getaddrs.addrs;
3215 sctp_read_lock(addr_lock);
3217 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3218 * addresses from the global local address list.
3220 if (sctp_list_single_entry(&bp->address_list)) {
3221 addr = list_entry(bp->address_list.next,
3222 struct sctp_sockaddr_entry, list);
3223 if (sctp_is_any(&addr->a)) {
3224 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
3225 getaddrs.addr_num, to);
3234 list_for_each(pos, &bp->address_list) {
3235 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3236 memcpy(&temp, &addr->a, sizeof(temp));
3237 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3238 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3239 temp.v4.sin_port = htons(temp.v4.sin_port);
3240 if (copy_to_user(to, &temp, addrlen)) {
3246 if (cnt >= getaddrs.addr_num) break;
3250 getaddrs.addr_num = cnt;
3251 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3255 sctp_read_unlock(addr_lock);
3259 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3261 * Requests that the local SCTP stack use the enclosed peer address as
3262 * the association primary. The enclosed address must be one of the
3263 * association peer's addresses.
3265 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3266 char __user *optval, int __user *optlen)
3268 struct sctp_prim prim;
3269 struct sctp_association *asoc;
3270 struct sctp_sock *sp = sctp_sk(sk);
3272 if (len != sizeof(struct sctp_prim))
3275 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3278 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3282 if (!asoc->peer.primary_path)
3285 asoc->peer.primary_path->ipaddr.v4.sin_port =
3286 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3287 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3288 sizeof(union sctp_addr));
3289 asoc->peer.primary_path->ipaddr.v4.sin_port =
3290 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3292 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3293 (union sctp_addr *)&prim.ssp_addr);
3295 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3302 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
3304 * Requests that the local endpoint set the specified Adaption Layer
3305 * Indication parameter for all future INIT and INIT-ACK exchanges.
3307 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
3308 char __user *optval, int __user *optlen)
3312 if (len < sizeof(__u32))
3315 len = sizeof(__u32);
3316 val = sctp_sk(sk)->adaption_ind;
3317 if (put_user(len, optlen))
3319 if (copy_to_user(optval, &val, len))
3326 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3328 * Applications that wish to use the sendto() system call may wish to
3329 * specify a default set of parameters that would normally be supplied
3330 * through the inclusion of ancillary data. This socket option allows
3331 * such an application to set the default sctp_sndrcvinfo structure.
3334 * The application that wishes to use this socket option simply passes
3335 * in to this call the sctp_sndrcvinfo structure defined in Section
3336 * 5.2.2) The input parameters accepted by this call include
3337 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3338 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3339 * to this call if the caller is using the UDP model.
3341 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3343 static int sctp_getsockopt_default_send_param(struct sock *sk,
3344 int len, char __user *optval,
3347 struct sctp_sndrcvinfo info;
3348 struct sctp_association *asoc;
3349 struct sctp_sock *sp = sctp_sk(sk);
3351 if (len != sizeof(struct sctp_sndrcvinfo))
3353 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3356 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3357 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3361 info.sinfo_stream = asoc->default_stream;
3362 info.sinfo_flags = asoc->default_flags;
3363 info.sinfo_ppid = asoc->default_ppid;
3364 info.sinfo_context = asoc->default_context;
3365 info.sinfo_timetolive = asoc->default_timetolive;
3367 info.sinfo_stream = sp->default_stream;
3368 info.sinfo_flags = sp->default_flags;
3369 info.sinfo_ppid = sp->default_ppid;
3370 info.sinfo_context = sp->default_context;
3371 info.sinfo_timetolive = sp->default_timetolive;
3374 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3382 * 7.1.5 SCTP_NODELAY
3384 * Turn on/off any Nagle-like algorithm. This means that packets are
3385 * generally sent as soon as possible and no unnecessary delays are
3386 * introduced, at the cost of more packets in the network. Expects an
3387 * integer boolean flag.
3390 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3391 char __user *optval, int __user *optlen)
3395 if (len < sizeof(int))
3399 val = (sctp_sk(sk)->nodelay == 1);
3400 if (put_user(len, optlen))
3402 if (copy_to_user(optval, &val, len))
3409 * 7.1.1 SCTP_RTOINFO
3411 * The protocol parameters used to initialize and bound retransmission
3412 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3413 * and modify these parameters.
3414 * All parameters are time values, in milliseconds. A value of 0, when
3415 * modifying the parameters, indicates that the current value should not
3419 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3420 char __user *optval,
3421 int __user *optlen) {
3422 struct sctp_rtoinfo rtoinfo;
3423 struct sctp_association *asoc;
3425 if (len != sizeof (struct sctp_rtoinfo))
3428 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3431 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3433 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3436 /* Values corresponding to the specific association. */
3438 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3439 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3440 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3442 /* Values corresponding to the endpoint. */
3443 struct sctp_sock *sp = sctp_sk(sk);
3445 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3446 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3447 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3450 if (put_user(len, optlen))
3453 if (copy_to_user(optval, &rtoinfo, len))
3461 * 7.1.2 SCTP_ASSOCINFO
3463 * This option is used to tune the the maximum retransmission attempts
3464 * of the association.
3465 * Returns an error if the new association retransmission value is
3466 * greater than the sum of the retransmission value of the peer.
3467 * See [SCTP] for more information.
3470 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3471 char __user *optval,
3475 struct sctp_assocparams assocparams;
3476 struct sctp_association *asoc;
3477 struct list_head *pos;
3480 if (len != sizeof (struct sctp_assocparams))
3483 if (copy_from_user(&assocparams, optval,
3484 sizeof (struct sctp_assocparams)))
3487 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3489 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3492 /* Values correspoinding to the specific association */
3494 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3495 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3496 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3497 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3499 (asoc->cookie_life.tv_usec
3502 list_for_each(pos, &asoc->peer.transport_addr_list) {
3506 assocparams.sasoc_number_peer_destinations = cnt;
3508 /* Values corresponding to the endpoint */
3509 struct sctp_sock *sp = sctp_sk(sk);
3511 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3512 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3513 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3514 assocparams.sasoc_cookie_life =
3515 sp->assocparams.sasoc_cookie_life;
3516 assocparams.sasoc_number_peer_destinations =
3518 sasoc_number_peer_destinations;
3521 if (put_user(len, optlen))
3524 if (copy_to_user(optval, &assocparams, len))
3531 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3533 * This socket option is a boolean flag which turns on or off mapped V4
3534 * addresses. If this option is turned on and the socket is type
3535 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3536 * If this option is turned off, then no mapping will be done of V4
3537 * addresses and a user will receive both PF_INET6 and PF_INET type
3538 * addresses on the socket.
3540 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3541 char __user *optval, int __user *optlen)
3544 struct sctp_sock *sp = sctp_sk(sk);
3546 if (len < sizeof(int))
3551 if (put_user(len, optlen))
3553 if (copy_to_user(optval, &val, len))
3560 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3562 * This socket option specifies the maximum size to put in any outgoing
3563 * SCTP chunk. If a message is larger than this size it will be
3564 * fragmented by SCTP into the specified size. Note that the underlying
3565 * SCTP implementation may fragment into smaller sized chunks when the
3566 * PMTU of the underlying association is smaller than the value set by
3569 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3570 char __user *optval, int __user *optlen)
3574 if (len < sizeof(int))
3579 val = sctp_sk(sk)->user_frag;
3580 if (put_user(len, optlen))
3582 if (copy_to_user(optval, &val, len))
3588 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3589 char __user *optval, int __user *optlen)
3594 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3596 /* I can hardly begin to describe how wrong this is. This is
3597 * so broken as to be worse than useless. The API draft
3598 * REALLY is NOT helpful here... I am not convinced that the
3599 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3600 * are at all well-founded.
3602 if (level != SOL_SCTP) {
3603 struct sctp_af *af = sctp_sk(sk)->pf->af;
3605 retval = af->getsockopt(sk, level, optname, optval, optlen);
3609 if (get_user(len, optlen))
3616 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3618 case SCTP_DISABLE_FRAGMENTS:
3619 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3623 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3625 case SCTP_AUTOCLOSE:
3626 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3628 case SCTP_SOCKOPT_PEELOFF:
3629 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3631 case SCTP_PEER_ADDR_PARAMS:
3632 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3636 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3638 case SCTP_GET_PEER_ADDRS_NUM:
3639 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3642 case SCTP_GET_LOCAL_ADDRS_NUM:
3643 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3646 case SCTP_GET_PEER_ADDRS:
3647 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3650 case SCTP_GET_LOCAL_ADDRS:
3651 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3654 case SCTP_DEFAULT_SEND_PARAM:
3655 retval = sctp_getsockopt_default_send_param(sk, len,
3658 case SCTP_PRIMARY_ADDR:
3659 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3662 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3665 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3667 case SCTP_ASSOCINFO:
3668 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3670 case SCTP_I_WANT_MAPPED_V4_ADDR:
3671 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3674 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3676 case SCTP_GET_PEER_ADDR_INFO:
3677 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3680 case SCTP_ADAPTION_LAYER:
3681 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
3685 retval = -ENOPROTOOPT;
3689 sctp_release_sock(sk);
3693 static void sctp_hash(struct sock *sk)
3698 static void sctp_unhash(struct sock *sk)
3703 /* Check if port is acceptable. Possibly find first available port.
3705 * The port hash table (contained in the 'global' SCTP protocol storage
3706 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3707 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3708 * list (the list number is the port number hashed out, so as you
3709 * would expect from a hash function, all the ports in a given list have
3710 * such a number that hashes out to the same list number; you were
3711 * expecting that, right?); so each list has a set of ports, with a
3712 * link to the socket (struct sock) that uses it, the port number and
3713 * a fastreuse flag (FIXME: NPI ipg).
3715 static struct sctp_bind_bucket *sctp_bucket_create(
3716 struct sctp_bind_hashbucket *head, unsigned short snum);
3718 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3720 struct sctp_bind_hashbucket *head; /* hash list */
3721 struct sctp_bind_bucket *pp; /* hash list port iterator */
3722 unsigned short snum;
3725 /* NOTE: Remember to put this back to net order. */
3726 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3727 snum = addr->v4.sin_port;
3729 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3730 sctp_local_bh_disable();
3733 /* Search for an available port.
3735 * 'sctp_port_rover' was the last port assigned, so
3736 * we start to search from 'sctp_port_rover +
3737 * 1'. What we do is first check if port 'rover' is
3738 * already in the hash table; if not, we use that; if
3739 * it is, we try next.
3741 int low = sysctl_local_port_range[0];
3742 int high = sysctl_local_port_range[1];
3743 int remaining = (high - low) + 1;
3747 sctp_spin_lock(&sctp_port_alloc_lock);
3748 rover = sctp_port_rover;
3751 if ((rover < low) || (rover > high))
3753 index = sctp_phashfn(rover);
3754 head = &sctp_port_hashtable[index];
3755 sctp_spin_lock(&head->lock);
3756 for (pp = head->chain; pp; pp = pp->next)
3757 if (pp->port == rover)
3761 sctp_spin_unlock(&head->lock);
3762 } while (--remaining > 0);
3763 sctp_port_rover = rover;
3764 sctp_spin_unlock(&sctp_port_alloc_lock);
3766 /* Exhausted local port range during search? */
3771 /* OK, here is the one we will use. HEAD (the port
3772 * hash table list entry) is non-NULL and we hold it's
3777 /* We are given an specific port number; we verify
3778 * that it is not being used. If it is used, we will
3779 * exahust the search in the hash list corresponding
3780 * to the port number (snum) - we detect that with the
3781 * port iterator, pp being NULL.
3783 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3784 sctp_spin_lock(&head->lock);
3785 for (pp = head->chain; pp; pp = pp->next) {
3786 if (pp->port == snum)
3793 if (!hlist_empty(&pp->owner)) {
3794 /* We had a port hash table hit - there is an
3795 * available port (pp != NULL) and it is being
3796 * used by other socket (pp->owner not empty); that other
3797 * socket is going to be sk2.
3799 int reuse = sk->sk_reuse;
3801 struct hlist_node *node;
3803 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3804 if (pp->fastreuse && sk->sk_reuse)
3807 /* Run through the list of sockets bound to the port
3808 * (pp->port) [via the pointers bind_next and
3809 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3810 * we get the endpoint they describe and run through
3811 * the endpoint's list of IP (v4 or v6) addresses,
3812 * comparing each of the addresses with the address of
3813 * the socket sk. If we find a match, then that means
3814 * that this port/socket (sk) combination are already
3817 sk_for_each_bound(sk2, node, &pp->owner) {
3818 struct sctp_endpoint *ep2;
3819 ep2 = sctp_sk(sk2)->ep;
3821 if (reuse && sk2->sk_reuse)
3824 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3830 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3833 /* If there was a hash table miss, create a new port. */
3835 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3838 /* In either case (hit or miss), make sure fastreuse is 1 only
3839 * if sk->sk_reuse is too (that is, if the caller requested
3840 * SO_REUSEADDR on this socket -sk-).
3842 if (hlist_empty(&pp->owner))
3843 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3844 else if (pp->fastreuse && !sk->sk_reuse)
3847 /* We are set, so fill up all the data in the hash table
3848 * entry, tie the socket list information with the rest of the
3849 * sockets FIXME: Blurry, NPI (ipg).
3852 inet_sk(sk)->num = snum;
3853 if (!sctp_sk(sk)->bind_hash) {
3854 sk_add_bind_node(sk, &pp->owner);
3855 sctp_sk(sk)->bind_hash = pp;
3860 sctp_spin_unlock(&head->lock);
3863 sctp_local_bh_enable();
3864 addr->v4.sin_port = htons(addr->v4.sin_port);
3868 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3869 * port is requested.
3871 static int sctp_get_port(struct sock *sk, unsigned short snum)
3874 union sctp_addr addr;
3875 struct sctp_af *af = sctp_sk(sk)->pf->af;
3877 /* Set up a dummy address struct from the sk. */
3878 af->from_sk(&addr, sk);
3879 addr.v4.sin_port = htons(snum);
3881 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3882 ret = sctp_get_port_local(sk, &addr);
3884 return (ret ? 1 : 0);
3888 * 3.1.3 listen() - UDP Style Syntax
3890 * By default, new associations are not accepted for UDP style sockets.
3891 * An application uses listen() to mark a socket as being able to
3892 * accept new associations.
3894 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3896 struct sctp_sock *sp = sctp_sk(sk);
3897 struct sctp_endpoint *ep = sp->ep;
3899 /* Only UDP style sockets that are not peeled off are allowed to
3902 if (!sctp_style(sk, UDP))
3905 /* If backlog is zero, disable listening. */
3907 if (sctp_sstate(sk, CLOSED))
3910 sctp_unhash_endpoint(ep);
3911 sk->sk_state = SCTP_SS_CLOSED;
3914 /* Return if we are already listening. */
3915 if (sctp_sstate(sk, LISTENING))
3919 * If a bind() or sctp_bindx() is not called prior to a listen()
3920 * call that allows new associations to be accepted, the system
3921 * picks an ephemeral port and will choose an address set equivalent
3922 * to binding with a wildcard address.
3924 * This is not currently spelled out in the SCTP sockets
3925 * extensions draft, but follows the practice as seen in TCP
3928 if (!ep->base.bind_addr.port) {
3929 if (sctp_autobind(sk))
3932 sk->sk_state = SCTP_SS_LISTENING;
3933 sctp_hash_endpoint(ep);
3938 * 4.1.3 listen() - TCP Style Syntax
3940 * Applications uses listen() to ready the SCTP endpoint for accepting
3941 * inbound associations.
3943 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3945 struct sctp_sock *sp = sctp_sk(sk);
3946 struct sctp_endpoint *ep = sp->ep;
3948 /* If backlog is zero, disable listening. */
3950 if (sctp_sstate(sk, CLOSED))
3953 sctp_unhash_endpoint(ep);
3954 sk->sk_state = SCTP_SS_CLOSED;
3957 if (sctp_sstate(sk, LISTENING))
3961 * If a bind() or sctp_bindx() is not called prior to a listen()
3962 * call that allows new associations to be accepted, the system
3963 * picks an ephemeral port and will choose an address set equivalent
3964 * to binding with a wildcard address.
3966 * This is not currently spelled out in the SCTP sockets
3967 * extensions draft, but follows the practice as seen in TCP
3970 if (!ep->base.bind_addr.port) {
3971 if (sctp_autobind(sk))
3974 sk->sk_state = SCTP_SS_LISTENING;
3975 sk->sk_max_ack_backlog = backlog;
3976 sctp_hash_endpoint(ep);
3981 * Move a socket to LISTENING state.
3983 int sctp_inet_listen(struct socket *sock, int backlog)
3985 struct sock *sk = sock->sk;
3986 struct crypto_tfm *tfm=NULL;
3989 if (unlikely(backlog < 0))
3994 if (sock->state != SS_UNCONNECTED)
3997 /* Allocate HMAC for generating cookie. */
3998 if (sctp_hmac_alg) {
3999 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4006 switch (sock->type) {
4007 case SOCK_SEQPACKET:
4008 err = sctp_seqpacket_listen(sk, backlog);
4011 err = sctp_stream_listen(sk, backlog);
4019 /* Store away the transform reference. */
4020 sctp_sk(sk)->hmac = tfm;
4022 sctp_release_sock(sk);
4026 sctp_crypto_free_tfm(tfm);
4031 * This function is done by modeling the current datagram_poll() and the
4032 * tcp_poll(). Note that, based on these implementations, we don't
4033 * lock the socket in this function, even though it seems that,
4034 * ideally, locking or some other mechanisms can be used to ensure
4035 * the integrity of the counters (sndbuf and wmem_queued) used
4036 * in this place. We assume that we don't need locks either until proven
4039 * Another thing to note is that we include the Async I/O support
4040 * here, again, by modeling the current TCP/UDP code. We don't have
4041 * a good way to test with it yet.
4043 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4045 struct sock *sk = sock->sk;
4046 struct sctp_sock *sp = sctp_sk(sk);
4049 poll_wait(file, sk->sk_sleep, wait);
4051 /* A TCP-style listening socket becomes readable when the accept queue
4054 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4055 return (!list_empty(&sp->ep->asocs)) ?
4056 (POLLIN | POLLRDNORM) : 0;
4060 /* Is there any exceptional events? */
4061 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4063 if (sk->sk_shutdown == SHUTDOWN_MASK)
4066 /* Is it readable? Reconsider this code with TCP-style support. */
4067 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4068 (sk->sk_shutdown & RCV_SHUTDOWN))
4069 mask |= POLLIN | POLLRDNORM;
4071 /* The association is either gone or not ready. */
4072 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4075 /* Is it writable? */
4076 if (sctp_writeable(sk)) {
4077 mask |= POLLOUT | POLLWRNORM;
4079 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4081 * Since the socket is not locked, the buffer
4082 * might be made available after the writeable check and
4083 * before the bit is set. This could cause a lost I/O
4084 * signal. tcp_poll() has a race breaker for this race
4085 * condition. Based on their implementation, we put
4086 * in the following code to cover it as well.
4088 if (sctp_writeable(sk))
4089 mask |= POLLOUT | POLLWRNORM;
4094 /********************************************************************
4095 * 2nd Level Abstractions
4096 ********************************************************************/
4098 static struct sctp_bind_bucket *sctp_bucket_create(
4099 struct sctp_bind_hashbucket *head, unsigned short snum)
4101 struct sctp_bind_bucket *pp;
4103 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4104 SCTP_DBG_OBJCNT_INC(bind_bucket);
4108 INIT_HLIST_HEAD(&pp->owner);
4109 if ((pp->next = head->chain) != NULL)
4110 pp->next->pprev = &pp->next;
4112 pp->pprev = &head->chain;
4117 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4118 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4120 if (hlist_empty(&pp->owner)) {
4122 pp->next->pprev = pp->pprev;
4123 *(pp->pprev) = pp->next;
4124 kmem_cache_free(sctp_bucket_cachep, pp);
4125 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4129 /* Release this socket's reference to a local port. */
4130 static inline void __sctp_put_port(struct sock *sk)
4132 struct sctp_bind_hashbucket *head =
4133 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4134 struct sctp_bind_bucket *pp;
4136 sctp_spin_lock(&head->lock);
4137 pp = sctp_sk(sk)->bind_hash;
4138 __sk_del_bind_node(sk);
4139 sctp_sk(sk)->bind_hash = NULL;
4140 inet_sk(sk)->num = 0;
4141 sctp_bucket_destroy(pp);
4142 sctp_spin_unlock(&head->lock);
4145 void sctp_put_port(struct sock *sk)
4147 sctp_local_bh_disable();
4148 __sctp_put_port(sk);
4149 sctp_local_bh_enable();
4153 * The system picks an ephemeral port and choose an address set equivalent
4154 * to binding with a wildcard address.
4155 * One of those addresses will be the primary address for the association.
4156 * This automatically enables the multihoming capability of SCTP.
4158 static int sctp_autobind(struct sock *sk)
4160 union sctp_addr autoaddr;
4162 unsigned short port;
4164 /* Initialize a local sockaddr structure to INADDR_ANY. */
4165 af = sctp_sk(sk)->pf->af;
4167 port = htons(inet_sk(sk)->num);
4168 af->inaddr_any(&autoaddr, port);
4170 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4173 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4176 * 4.2 The cmsghdr Structure *
4178 * When ancillary data is sent or received, any number of ancillary data
4179 * objects can be specified by the msg_control and msg_controllen members of
4180 * the msghdr structure, because each object is preceded by
4181 * a cmsghdr structure defining the object's length (the cmsg_len member).
4182 * Historically Berkeley-derived implementations have passed only one object
4183 * at a time, but this API allows multiple objects to be
4184 * passed in a single call to sendmsg() or recvmsg(). The following example
4185 * shows two ancillary data objects in a control buffer.
4187 * |<--------------------------- msg_controllen -------------------------->|
4190 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4192 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4195 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4197 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4200 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4201 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4203 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4205 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4212 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4213 sctp_cmsgs_t *cmsgs)
4215 struct cmsghdr *cmsg;
4217 for (cmsg = CMSG_FIRSTHDR(msg);
4219 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4220 if (!CMSG_OK(msg, cmsg))
4223 /* Should we parse this header or ignore? */
4224 if (cmsg->cmsg_level != IPPROTO_SCTP)
4227 /* Strictly check lengths following example in SCM code. */
4228 switch (cmsg->cmsg_type) {
4230 /* SCTP Socket API Extension
4231 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4233 * This cmsghdr structure provides information for
4234 * initializing new SCTP associations with sendmsg().
4235 * The SCTP_INITMSG socket option uses this same data
4236 * structure. This structure is not used for
4239 * cmsg_level cmsg_type cmsg_data[]
4240 * ------------ ------------ ----------------------
4241 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4243 if (cmsg->cmsg_len !=
4244 CMSG_LEN(sizeof(struct sctp_initmsg)))
4246 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4250 /* SCTP Socket API Extension
4251 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4253 * This cmsghdr structure specifies SCTP options for
4254 * sendmsg() and describes SCTP header information
4255 * about a received message through recvmsg().
4257 * cmsg_level cmsg_type cmsg_data[]
4258 * ------------ ------------ ----------------------
4259 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4261 if (cmsg->cmsg_len !=
4262 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4266 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4268 /* Minimally, validate the sinfo_flags. */
4269 if (cmsgs->info->sinfo_flags &
4270 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4271 MSG_ABORT | MSG_EOF))
4283 * Wait for a packet..
4284 * Note: This function is the same function as in core/datagram.c
4285 * with a few modifications to make lksctp work.
4287 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4292 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4294 /* Socket errors? */
4295 error = sock_error(sk);
4299 if (!skb_queue_empty(&sk->sk_receive_queue))
4302 /* Socket shut down? */
4303 if (sk->sk_shutdown & RCV_SHUTDOWN)
4306 /* Sequenced packets can come disconnected. If so we report the
4311 /* Is there a good reason to think that we may receive some data? */
4312 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4315 /* Handle signals. */
4316 if (signal_pending(current))
4319 /* Let another process have a go. Since we are going to sleep
4320 * anyway. Note: This may cause odd behaviors if the message
4321 * does not fit in the user's buffer, but this seems to be the
4322 * only way to honor MSG_DONTWAIT realistically.
4324 sctp_release_sock(sk);
4325 *timeo_p = schedule_timeout(*timeo_p);
4329 finish_wait(sk->sk_sleep, &wait);
4333 error = sock_intr_errno(*timeo_p);
4336 finish_wait(sk->sk_sleep, &wait);
4341 /* Receive a datagram.
4342 * Note: This is pretty much the same routine as in core/datagram.c
4343 * with a few changes to make lksctp work.
4345 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4346 int noblock, int *err)
4349 struct sk_buff *skb;
4352 /* Caller is allowed not to check sk->sk_err before calling. */
4353 error = sock_error(sk);
4357 timeo = sock_rcvtimeo(sk, noblock);
4359 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4360 timeo, MAX_SCHEDULE_TIMEOUT);
4363 /* Again only user level code calls this function,
4364 * so nothing interrupt level
4365 * will suddenly eat the receive_queue.
4367 * Look at current nfs client by the way...
4368 * However, this function was corrent in any case. 8)
4370 if (flags & MSG_PEEK) {
4371 unsigned long cpu_flags;
4373 sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4375 skb = skb_peek(&sk->sk_receive_queue);
4377 atomic_inc(&skb->users);
4378 sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4381 skb = skb_dequeue(&sk->sk_receive_queue);
4387 if (sk->sk_shutdown & RCV_SHUTDOWN)
4390 /* User doesn't want to wait. */
4394 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4403 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4404 static void __sctp_write_space(struct sctp_association *asoc)
4406 struct sock *sk = asoc->base.sk;
4407 struct socket *sock = sk->sk_socket;
4409 if ((sctp_wspace(asoc) > 0) && sock) {
4410 if (waitqueue_active(&asoc->wait))
4411 wake_up_interruptible(&asoc->wait);
4413 if (sctp_writeable(sk)) {
4414 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4415 wake_up_interruptible(sk->sk_sleep);
4417 /* Note that we try to include the Async I/O support
4418 * here by modeling from the current TCP/UDP code.
4419 * We have not tested with it yet.
4421 if (sock->fasync_list &&
4422 !(sk->sk_shutdown & SEND_SHUTDOWN))
4423 sock_wake_async(sock, 2, POLL_OUT);
4428 /* Do accounting for the sndbuf space.
4429 * Decrement the used sndbuf space of the corresponding association by the
4430 * data size which was just transmitted(freed).
4432 static void sctp_wfree(struct sk_buff *skb)
4434 struct sctp_association *asoc;
4435 struct sctp_chunk *chunk;
4438 /* Get the saved chunk pointer. */
4439 chunk = *((struct sctp_chunk **)(skb->cb));
4442 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
4443 sizeof(struct sk_buff) +
4444 sizeof(struct sctp_chunk);
4446 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
4447 sizeof(struct sk_buff) +
4448 sizeof(struct sctp_chunk);
4450 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
4453 __sctp_write_space(asoc);
4455 sctp_association_put(asoc);
4458 /* Helper function to wait for space in the sndbuf. */
4459 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4462 struct sock *sk = asoc->base.sk;
4464 long current_timeo = *timeo_p;
4467 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4468 asoc, (long)(*timeo_p), msg_len);
4470 /* Increment the association's refcnt. */
4471 sctp_association_hold(asoc);
4473 /* Wait on the association specific sndbuf space. */
4475 prepare_to_wait_exclusive(&asoc->wait, &wait,
4476 TASK_INTERRUPTIBLE);
4479 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4482 if (signal_pending(current))
4483 goto do_interrupted;
4484 if (msg_len <= sctp_wspace(asoc))
4487 /* Let another process have a go. Since we are going
4490 sctp_release_sock(sk);
4491 current_timeo = schedule_timeout(current_timeo);
4494 *timeo_p = current_timeo;
4498 finish_wait(&asoc->wait, &wait);
4500 /* Release the association's refcnt. */
4501 sctp_association_put(asoc);
4510 err = sock_intr_errno(*timeo_p);
4518 /* If socket sndbuf has changed, wake up all per association waiters. */
4519 void sctp_write_space(struct sock *sk)
4521 struct sctp_association *asoc;
4522 struct list_head *pos;
4524 /* Wake up the tasks in each wait queue. */
4525 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4526 asoc = list_entry(pos, struct sctp_association, asocs);
4527 __sctp_write_space(asoc);
4531 /* Is there any sndbuf space available on the socket?
4533 * Note that wmem_queued is the sum of the send buffers on all of the
4534 * associations on the same socket. For a UDP-style socket with
4535 * multiple associations, it is possible for it to be "unwriteable"
4536 * prematurely. I assume that this is acceptable because
4537 * a premature "unwriteable" is better than an accidental "writeable" which
4538 * would cause an unwanted block under certain circumstances. For the 1-1
4539 * UDP-style sockets or TCP-style sockets, this code should work.
4542 static int sctp_writeable(struct sock *sk)
4546 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4552 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4553 * returns immediately with EINPROGRESS.
4555 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4557 struct sock *sk = asoc->base.sk;
4559 long current_timeo = *timeo_p;
4562 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4565 /* Increment the association's refcnt. */
4566 sctp_association_hold(asoc);
4569 prepare_to_wait_exclusive(&asoc->wait, &wait,
4570 TASK_INTERRUPTIBLE);
4573 if (sk->sk_shutdown & RCV_SHUTDOWN)
4575 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4578 if (signal_pending(current))
4579 goto do_interrupted;
4581 if (sctp_state(asoc, ESTABLISHED))
4584 /* Let another process have a go. Since we are going
4587 sctp_release_sock(sk);
4588 current_timeo = schedule_timeout(current_timeo);
4591 *timeo_p = current_timeo;
4595 finish_wait(&asoc->wait, &wait);
4597 /* Release the association's refcnt. */
4598 sctp_association_put(asoc);
4603 if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
4604 asoc->max_init_attempts)
4607 err = -ECONNREFUSED;
4611 err = sock_intr_errno(*timeo_p);
4619 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4621 struct sctp_endpoint *ep;
4625 ep = sctp_sk(sk)->ep;
4629 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4630 TASK_INTERRUPTIBLE);
4632 if (list_empty(&ep->asocs)) {
4633 sctp_release_sock(sk);
4634 timeo = schedule_timeout(timeo);
4639 if (!sctp_sstate(sk, LISTENING))
4643 if (!list_empty(&ep->asocs))
4646 err = sock_intr_errno(timeo);
4647 if (signal_pending(current))
4655 finish_wait(sk->sk_sleep, &wait);
4660 void sctp_wait_for_close(struct sock *sk, long timeout)
4665 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4666 if (list_empty(&sctp_sk(sk)->ep->asocs))
4668 sctp_release_sock(sk);
4669 timeout = schedule_timeout(timeout);
4671 } while (!signal_pending(current) && timeout);
4673 finish_wait(sk->sk_sleep, &wait);
4676 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4677 * and its messages to the newsk.
4679 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4680 struct sctp_association *assoc,
4681 sctp_socket_type_t type)
4683 struct sctp_sock *oldsp = sctp_sk(oldsk);
4684 struct sctp_sock *newsp = sctp_sk(newsk);
4685 struct sctp_bind_bucket *pp; /* hash list port iterator */
4686 struct sctp_endpoint *newep = newsp->ep;
4687 struct sk_buff *skb, *tmp;
4688 struct sctp_ulpevent *event;
4691 /* Migrate socket buffer sizes and all the socket level options to the
4694 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4695 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4696 /* Brute force copy old sctp opt. */
4697 inet_sk_copy_descendant(newsk, oldsk);
4699 /* Restore the ep value that was overwritten with the above structure
4705 /* Hook this new socket in to the bind_hash list. */
4706 pp = sctp_sk(oldsk)->bind_hash;
4707 sk_add_bind_node(newsk, &pp->owner);
4708 sctp_sk(newsk)->bind_hash = pp;
4709 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4711 /* Copy the bind_addr list from the original endpoint to the new
4712 * endpoint so that we can handle restarts properly
4714 if (assoc->peer.ipv4_address)
4715 flags |= SCTP_ADDR4_PEERSUPP;
4716 if (assoc->peer.ipv6_address)
4717 flags |= SCTP_ADDR6_PEERSUPP;
4718 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
4719 &oldsp->ep->base.bind_addr,
4720 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
4722 /* Move any messages in the old socket's receive queue that are for the
4723 * peeled off association to the new socket's receive queue.
4725 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4726 event = sctp_skb2event(skb);
4727 if (event->asoc == assoc) {
4728 __skb_unlink(skb, skb->list);
4729 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4733 /* Clean up any messages pending delivery due to partial
4734 * delivery. Three cases:
4735 * 1) No partial deliver; no work.
4736 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4737 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
4739 skb_queue_head_init(&newsp->pd_lobby);
4740 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4742 if (sctp_sk(oldsk)->pd_mode) {
4743 struct sk_buff_head *queue;
4745 /* Decide which queue to move pd_lobby skbs to. */
4746 if (assoc->ulpq.pd_mode) {
4747 queue = &newsp->pd_lobby;
4749 queue = &newsk->sk_receive_queue;
4751 /* Walk through the pd_lobby, looking for skbs that
4752 * need moved to the new socket.
4754 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4755 event = sctp_skb2event(skb);
4756 if (event->asoc == assoc) {
4757 __skb_unlink(skb, skb->list);
4758 __skb_queue_tail(queue, skb);
4762 /* Clear up any skbs waiting for the partial
4763 * delivery to finish.
4765 if (assoc->ulpq.pd_mode)
4766 sctp_clear_pd(oldsk);
4770 /* Set the type of socket to indicate that it is peeled off from the
4771 * original UDP-style socket or created with the accept() call on a
4772 * TCP-style socket..
4776 /* Migrate the association to the new socket. */
4777 sctp_assoc_migrate(assoc, newsk);
4779 /* If the association on the newsk is already closed before accept()
4780 * is called, set RCV_SHUTDOWN flag.
4782 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4783 newsk->sk_shutdown |= RCV_SHUTDOWN;
4785 newsk->sk_state = SCTP_SS_ESTABLISHED;
4788 /* This proto struct describes the ULP interface for SCTP. */
4789 struct proto sctp_prot = {
4791 .owner = THIS_MODULE,
4792 .close = sctp_close,
4793 .connect = sctp_connect,
4794 .disconnect = sctp_disconnect,
4795 .accept = sctp_accept,
4796 .ioctl = sctp_ioctl,
4797 .init = sctp_init_sock,
4798 .destroy = sctp_destroy_sock,
4799 .shutdown = sctp_shutdown,
4800 .setsockopt = sctp_setsockopt,
4801 .getsockopt = sctp_getsockopt,
4802 .sendmsg = sctp_sendmsg,
4803 .recvmsg = sctp_recvmsg,
4805 .backlog_rcv = sctp_backlog_rcv,
4807 .unhash = sctp_unhash,
4808 .get_port = sctp_get_port,
4809 .obj_size = sizeof(struct sctp_sock),
4812 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4813 struct proto sctpv6_prot = {
4815 .owner = THIS_MODULE,
4816 .close = sctp_close,
4817 .connect = sctp_connect,
4818 .disconnect = sctp_disconnect,
4819 .accept = sctp_accept,
4820 .ioctl = sctp_ioctl,
4821 .init = sctp_init_sock,
4822 .destroy = sctp_destroy_sock,
4823 .shutdown = sctp_shutdown,
4824 .setsockopt = sctp_setsockopt,
4825 .getsockopt = sctp_getsockopt,
4826 .sendmsg = sctp_sendmsg,
4827 .recvmsg = sctp_recvmsg,
4829 .backlog_rcv = sctp_backlog_rcv,
4831 .unhash = sctp_unhash,
4832 .get_port = sctp_get_port,
4833 .obj_size = sizeof(struct sctp6_sock),
4835 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */