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/capability.h>
67 #include <linux/fcntl.h>
68 #include <linux/poll.h>
69 #include <linux/init.h>
70 #include <linux/crypto.h>
74 #include <net/route.h>
76 #include <net/inet_common.h>
78 #include <linux/socket.h> /* for sa_family_t */
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
83 /* WARNING: Please do not remove the SCTP_STATIC attribute to
84 * any of the functions below as they are used to export functions
85 * used by a project regression testsuite.
88 /* Forward declarations for internal helper functions. */
89 static int sctp_writeable(struct sock *sk);
90 static void sctp_wfree(struct sk_buff *skb);
91 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
93 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
94 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
95 static int sctp_wait_for_accept(struct sock *sk, long timeo);
96 static void sctp_wait_for_close(struct sock *sk, long timeo);
97 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
98 union sctp_addr *addr, int len);
99 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
100 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
103 static int sctp_send_asconf(struct sctp_association *asoc,
104 struct sctp_chunk *chunk);
105 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
106 static int sctp_autobind(struct sock *sk);
107 static void sctp_sock_migrate(struct sock *, struct sock *,
108 struct sctp_association *, sctp_socket_type_t);
109 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
111 extern kmem_cache_t *sctp_bucket_cachep;
113 /* Get the sndbuf space available at the time on the association. */
114 static inline int sctp_wspace(struct sctp_association *asoc)
116 struct sock *sk = asoc->base.sk;
119 if (asoc->ep->sndbuf_policy) {
120 /* make sure that no association uses more than sk_sndbuf */
121 amt = sk->sk_sndbuf - asoc->sndbuf_used;
123 /* do socket level accounting */
124 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
133 /* Increment the used sndbuf space count of the corresponding association by
134 * the size of the outgoing data chunk.
135 * Also, set the skb destructor for sndbuf accounting later.
137 * Since it is always 1-1 between chunk and skb, and also a new skb is always
138 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
139 * destructor in the data chunk skb for the purpose of the sndbuf space
142 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
144 struct sctp_association *asoc = chunk->asoc;
145 struct sock *sk = asoc->base.sk;
147 /* The sndbuf space is tracked per association. */
148 sctp_association_hold(asoc);
150 skb_set_owner_w(chunk->skb, sk);
152 chunk->skb->destructor = sctp_wfree;
153 /* Save the chunk pointer in skb for sctp_wfree to use later. */
154 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
156 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
157 sizeof(struct sk_buff) +
158 sizeof(struct sctp_chunk);
160 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
163 /* Verify that this is a valid address. */
164 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
169 /* Verify basic sockaddr. */
170 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
174 /* Is this a valid SCTP address? */
175 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
178 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
184 /* Look up the association by its id. If this is not a UDP-style
185 * socket, the ID field is always ignored.
187 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
189 struct sctp_association *asoc = NULL;
191 /* If this is not a UDP-style socket, assoc id should be ignored. */
192 if (!sctp_style(sk, UDP)) {
193 /* Return NULL if the socket state is not ESTABLISHED. It
194 * could be a TCP-style listening socket or a socket which
195 * hasn't yet called connect() to establish an association.
197 if (!sctp_sstate(sk, ESTABLISHED))
200 /* Get the first and the only association from the list. */
201 if (!list_empty(&sctp_sk(sk)->ep->asocs))
202 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
203 struct sctp_association, asocs);
207 /* Otherwise this is a UDP-style socket. */
208 if (!id || (id == (sctp_assoc_t)-1))
211 spin_lock_bh(&sctp_assocs_id_lock);
212 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
213 spin_unlock_bh(&sctp_assocs_id_lock);
215 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
221 /* Look up the transport from an address and an assoc id. If both address and
222 * id are specified, the associations matching the address and the id should be
225 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
226 struct sockaddr_storage *addr,
229 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
230 struct sctp_transport *transport;
231 union sctp_addr *laddr = (union sctp_addr *)addr;
233 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
234 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
235 (union sctp_addr *)addr,
237 laddr->v4.sin_port = htons(laddr->v4.sin_port);
242 id_asoc = sctp_id2assoc(sk, id);
243 if (id_asoc && (id_asoc != addr_asoc))
246 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
247 (union sctp_addr *)addr);
252 /* API 3.1.2 bind() - UDP Style Syntax
253 * The syntax of bind() is,
255 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
257 * sd - the socket descriptor returned by socket().
258 * addr - the address structure (struct sockaddr_in or struct
259 * sockaddr_in6 [RFC 2553]),
260 * addr_len - the size of the address structure.
262 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
268 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
271 /* Disallow binding twice. */
272 if (!sctp_sk(sk)->ep->base.bind_addr.port)
273 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
278 sctp_release_sock(sk);
283 static long sctp_get_port_local(struct sock *, union sctp_addr *);
285 /* Verify this is a valid sockaddr. */
286 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
287 union sctp_addr *addr, int len)
291 /* Check minimum size. */
292 if (len < sizeof (struct sockaddr))
295 /* Does this PF support this AF? */
296 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
299 /* If we get this far, af is valid. */
300 af = sctp_get_af_specific(addr->sa.sa_family);
302 if (len < af->sockaddr_len)
308 /* Bind a local address either to an endpoint or to an association. */
309 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
311 struct sctp_sock *sp = sctp_sk(sk);
312 struct sctp_endpoint *ep = sp->ep;
313 struct sctp_bind_addr *bp = &ep->base.bind_addr;
318 /* Common sockaddr verification. */
319 af = sctp_sockaddr_af(sp, addr, len);
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
326 snum = ntohs(addr->v4.sin_port);
328 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
329 ", port: %d, new port: %d, len: %d)\n",
335 /* PF specific bind() address verification. */
336 if (!sp->pf->bind_verify(sp, addr))
337 return -EADDRNOTAVAIL;
339 /* We must either be unbound, or bind to the same port. */
340 if (bp->port && (snum != bp->port)) {
341 SCTP_DEBUG_PRINTK("sctp_do_bind:"
342 " New port %d does not match existing port "
343 "%d.\n", snum, bp->port);
347 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
350 /* Make sure we are allowed to bind here.
351 * The function sctp_get_port_local() does duplicate address
354 if ((ret = sctp_get_port_local(sk, addr))) {
355 if (ret == (long) sk) {
356 /* This endpoint has a conflicting address. */
363 /* Refresh ephemeral port. */
365 bp->port = inet_sk(sk)->num;
367 /* Add the address to the bind address list. */
368 sctp_local_bh_disable();
369 sctp_write_lock(&ep->base.addr_lock);
371 /* Use GFP_ATOMIC since BHs are disabled. */
372 addr->v4.sin_port = ntohs(addr->v4.sin_port);
373 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
374 addr->v4.sin_port = htons(addr->v4.sin_port);
375 sctp_write_unlock(&ep->base.addr_lock);
376 sctp_local_bh_enable();
378 /* Copy back into socket for getsockname() use. */
380 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
381 af->to_sk_saddr(addr, sk);
387 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
389 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
390 * at any one time. If a sender, after sending an ASCONF chunk, decides
391 * it needs to transfer another ASCONF Chunk, it MUST wait until the
392 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
393 * subsequent ASCONF. Note this restriction binds each side, so at any
394 * time two ASCONF may be in-transit on any given association (one sent
395 * from each endpoint).
397 static int sctp_send_asconf(struct sctp_association *asoc,
398 struct sctp_chunk *chunk)
402 /* If there is an outstanding ASCONF chunk, queue it for later
405 if (asoc->addip_last_asconf) {
406 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
410 /* Hold the chunk until an ASCONF_ACK is received. */
411 sctp_chunk_hold(chunk);
412 retval = sctp_primitive_ASCONF(asoc, chunk);
414 sctp_chunk_free(chunk);
416 asoc->addip_last_asconf = chunk;
422 /* Add a list of addresses as bind addresses to local endpoint or
425 * Basically run through each address specified in the addrs/addrcnt
426 * array/length pair, determine if it is IPv6 or IPv4 and call
427 * sctp_do_bind() on it.
429 * If any of them fails, then the operation will be reversed and the
430 * ones that were added will be removed.
432 * Only sctp_setsockopt_bindx() is supposed to call this function.
434 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
439 struct sockaddr *sa_addr;
442 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
446 for (cnt = 0; cnt < addrcnt; cnt++) {
447 /* The list may contain either IPv4 or IPv6 address;
448 * determine the address length for walking thru the list.
450 sa_addr = (struct sockaddr *)addr_buf;
451 af = sctp_get_af_specific(sa_addr->sa_family);
457 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
460 addr_buf += af->sockaddr_len;
464 /* Failed. Cleanup the ones that have been added */
466 sctp_bindx_rem(sk, addrs, cnt);
474 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
475 * associations that are part of the endpoint indicating that a list of local
476 * addresses are added to the endpoint.
478 * If any of the addresses is already in the bind address list of the
479 * association, we do not send the chunk for that association. But it will not
480 * affect other associations.
482 * Only sctp_setsockopt_bindx() is supposed to call this function.
484 static int sctp_send_asconf_add_ip(struct sock *sk,
485 struct sockaddr *addrs,
488 struct sctp_sock *sp;
489 struct sctp_endpoint *ep;
490 struct sctp_association *asoc;
491 struct sctp_bind_addr *bp;
492 struct sctp_chunk *chunk;
493 struct sctp_sockaddr_entry *laddr;
494 union sctp_addr *addr;
497 struct list_head *pos;
502 if (!sctp_addip_enable)
508 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
509 __FUNCTION__, sk, addrs, addrcnt);
511 list_for_each(pos, &ep->asocs) {
512 asoc = list_entry(pos, struct sctp_association, asocs);
514 if (!asoc->peer.asconf_capable)
517 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
520 if (!sctp_state(asoc, ESTABLISHED))
523 /* Check if any address in the packed array of addresses is
524 * in the bind address list of the association. If so,
525 * do not send the asconf chunk to its peer, but continue with
526 * other associations.
529 for (i = 0; i < addrcnt; i++) {
530 addr = (union sctp_addr *)addr_buf;
531 af = sctp_get_af_specific(addr->v4.sin_family);
537 if (sctp_assoc_lookup_laddr(asoc, addr))
540 addr_buf += af->sockaddr_len;
545 /* Use the first address in bind addr list of association as
546 * Address Parameter of ASCONF CHUNK.
548 sctp_read_lock(&asoc->base.addr_lock);
549 bp = &asoc->base.bind_addr;
550 p = bp->address_list.next;
551 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
552 sctp_read_unlock(&asoc->base.addr_lock);
554 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
555 addrcnt, SCTP_PARAM_ADD_IP);
561 retval = sctp_send_asconf(asoc, chunk);
563 /* FIXME: After sending the add address ASCONF chunk, we
564 * cannot append the address to the association's binding
565 * address list, because the new address may be used as the
566 * source of a message sent to the peer before the ASCONF
567 * chunk is received by the peer. So we should wait until
568 * ASCONF_ACK is received.
576 /* Remove a list of addresses from bind addresses list. Do not remove the
579 * Basically run through each address specified in the addrs/addrcnt
580 * array/length pair, determine if it is IPv6 or IPv4 and call
581 * sctp_del_bind() on it.
583 * If any of them fails, then the operation will be reversed and the
584 * ones that were removed will be added back.
586 * At least one address has to be left; if only one address is
587 * available, the operation will return -EBUSY.
589 * Only sctp_setsockopt_bindx() is supposed to call this function.
591 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
593 struct sctp_sock *sp = sctp_sk(sk);
594 struct sctp_endpoint *ep = sp->ep;
596 struct sctp_bind_addr *bp = &ep->base.bind_addr;
598 union sctp_addr saveaddr;
600 struct sockaddr *sa_addr;
603 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
607 for (cnt = 0; cnt < addrcnt; cnt++) {
608 /* If the bind address list is empty or if there is only one
609 * bind address, there is nothing more to be removed (we need
610 * at least one address here).
612 if (list_empty(&bp->address_list) ||
613 (sctp_list_single_entry(&bp->address_list))) {
618 /* The list may contain either IPv4 or IPv6 address;
619 * determine the address length to copy the address to
622 sa_addr = (struct sockaddr *)addr_buf;
623 af = sctp_get_af_specific(sa_addr->sa_family);
628 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
629 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
630 if (saveaddr.v4.sin_port != bp->port) {
635 /* FIXME - There is probably a need to check if sk->sk_saddr and
636 * sk->sk_rcv_addr are currently set to one of the addresses to
637 * be removed. This is something which needs to be looked into
638 * when we are fixing the outstanding issues with multi-homing
639 * socket routing and failover schemes. Refer to comments in
640 * sctp_do_bind(). -daisy
642 sctp_local_bh_disable();
643 sctp_write_lock(&ep->base.addr_lock);
645 retval = sctp_del_bind_addr(bp, &saveaddr);
647 sctp_write_unlock(&ep->base.addr_lock);
648 sctp_local_bh_enable();
650 addr_buf += af->sockaddr_len;
653 /* Failed. Add the ones that has been removed back */
655 sctp_bindx_add(sk, addrs, cnt);
663 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
664 * the associations that are part of the endpoint indicating that a list of
665 * local addresses are removed from the endpoint.
667 * If any of the addresses is already in the bind address list of the
668 * association, we do not send the chunk for that association. But it will not
669 * affect other associations.
671 * Only sctp_setsockopt_bindx() is supposed to call this function.
673 static int sctp_send_asconf_del_ip(struct sock *sk,
674 struct sockaddr *addrs,
677 struct sctp_sock *sp;
678 struct sctp_endpoint *ep;
679 struct sctp_association *asoc;
680 struct sctp_bind_addr *bp;
681 struct sctp_chunk *chunk;
682 union sctp_addr *laddr;
685 struct list_head *pos;
689 if (!sctp_addip_enable)
695 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
696 __FUNCTION__, sk, addrs, addrcnt);
698 list_for_each(pos, &ep->asocs) {
699 asoc = list_entry(pos, struct sctp_association, asocs);
701 if (!asoc->peer.asconf_capable)
704 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
707 if (!sctp_state(asoc, ESTABLISHED))
710 /* Check if any address in the packed array of addresses is
711 * not present in the bind address list of the association.
712 * If so, do not send the asconf chunk to its peer, but
713 * continue with other associations.
716 for (i = 0; i < addrcnt; i++) {
717 laddr = (union sctp_addr *)addr_buf;
718 af = sctp_get_af_specific(laddr->v4.sin_family);
724 if (!sctp_assoc_lookup_laddr(asoc, laddr))
727 addr_buf += af->sockaddr_len;
732 /* Find one address in the association's bind address list
733 * that is not in the packed array of addresses. This is to
734 * make sure that we do not delete all the addresses in the
737 sctp_read_lock(&asoc->base.addr_lock);
738 bp = &asoc->base.bind_addr;
739 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
741 sctp_read_unlock(&asoc->base.addr_lock);
745 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
752 retval = sctp_send_asconf(asoc, chunk);
754 /* FIXME: After sending the delete address ASCONF chunk, we
755 * cannot remove the addresses from the association's bind
756 * address list, because there maybe some packet send to
757 * the delete addresses, so we should wait until ASCONF_ACK
758 * packet is received.
765 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
768 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
771 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
772 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
775 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
776 * Section 3.1.2 for this usage.
778 * addrs is a pointer to an array of one or more socket addresses. Each
779 * address is contained in its appropriate structure (i.e. struct
780 * sockaddr_in or struct sockaddr_in6) the family of the address type
781 * must be used to distengish the address length (note that this
782 * representation is termed a "packed array" of addresses). The caller
783 * specifies the number of addresses in the array with addrcnt.
785 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
786 * -1, and sets errno to the appropriate error code.
788 * For SCTP, the port given in each socket address must be the same, or
789 * sctp_bindx() will fail, setting errno to EINVAL.
791 * The flags parameter is formed from the bitwise OR of zero or more of
792 * the following currently defined flags:
794 * SCTP_BINDX_ADD_ADDR
796 * SCTP_BINDX_REM_ADDR
798 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
799 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
800 * addresses from the association. The two flags are mutually exclusive;
801 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
802 * not remove all addresses from an association; sctp_bindx() will
803 * reject such an attempt with EINVAL.
805 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
806 * additional addresses with an endpoint after calling bind(). Or use
807 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
808 * socket is associated with so that no new association accepted will be
809 * associated with those addresses. If the endpoint supports dynamic
810 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
811 * endpoint to send the appropriate message to the peer to change the
812 * peers address lists.
814 * Adding and removing addresses from a connected association is
815 * optional functionality. Implementations that do not support this
816 * functionality should return EOPNOTSUPP.
818 * Basically do nothing but copying the addresses from user to kernel
819 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
820 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
823 * We don't use copy_from_user() for optimization: we first do the
824 * sanity checks (buffer size -fast- and access check-healthy
825 * pointer); if all of those succeed, then we can alloc the memory
826 * (expensive operation) needed to copy the data to kernel. Then we do
827 * the copying without checking the user space area
828 * (__copy_from_user()).
830 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
833 * sk The sk of the socket
834 * addrs The pointer to the addresses in user land
835 * addrssize Size of the addrs buffer
836 * op Operation to perform (add or remove, see the flags of
839 * Returns 0 if ok, <0 errno code on error.
841 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
842 struct sockaddr __user *addrs,
843 int addrs_size, int op)
845 struct sockaddr *kaddrs;
849 struct sockaddr *sa_addr;
853 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
854 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
856 if (unlikely(addrs_size <= 0))
859 /* Check the user passed a healthy pointer. */
860 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
863 /* Alloc space for the address array in kernel memory. */
864 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
865 if (unlikely(!kaddrs))
868 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
873 /* Walk through the addrs buffer and count the number of addresses. */
875 while (walk_size < addrs_size) {
876 sa_addr = (struct sockaddr *)addr_buf;
877 af = sctp_get_af_specific(sa_addr->sa_family);
879 /* If the address family is not supported or if this address
880 * causes the address buffer to overflow return EINVAL.
882 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
887 addr_buf += af->sockaddr_len;
888 walk_size += af->sockaddr_len;
893 case SCTP_BINDX_ADD_ADDR:
894 err = sctp_bindx_add(sk, kaddrs, addrcnt);
897 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
900 case SCTP_BINDX_REM_ADDR:
901 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
904 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
918 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
920 * Common routine for handling connect() and sctp_connectx().
921 * Connect will come in with just a single address.
923 static int __sctp_connect(struct sock* sk,
924 struct sockaddr *kaddrs,
927 struct sctp_sock *sp;
928 struct sctp_endpoint *ep;
929 struct sctp_association *asoc = NULL;
930 struct sctp_association *asoc2;
931 struct sctp_transport *transport;
939 struct sockaddr *sa_addr;
945 /* connect() cannot be done on a socket that is already in ESTABLISHED
946 * state - UDP-style peeled off socket or a TCP-style socket that
947 * is already connected.
948 * It cannot be done even on a TCP-style listening socket.
950 if (sctp_sstate(sk, ESTABLISHED) ||
951 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
956 /* Walk through the addrs buffer and count the number of addresses. */
958 while (walk_size < addrs_size) {
959 sa_addr = (struct sockaddr *)addr_buf;
960 af = sctp_get_af_specific(sa_addr->sa_family);
962 /* If the address family is not supported or if this address
963 * causes the address buffer to overflow return EINVAL.
965 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
970 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
975 memcpy(&to, sa_addr, af->sockaddr_len);
976 to.v4.sin_port = ntohs(to.v4.sin_port);
978 /* Check if there already is a matching association on the
979 * endpoint (other than the one created here).
981 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
982 if (asoc2 && asoc2 != asoc) {
983 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
990 /* If we could not find a matching association on the endpoint,
991 * make sure that there is no peeled-off association matching
992 * the peer address even on another socket.
994 if (sctp_endpoint_is_peeled_off(ep, &to)) {
995 err = -EADDRNOTAVAIL;
1000 /* If a bind() or sctp_bindx() is not called prior to
1001 * an sctp_connectx() call, the system picks an
1002 * ephemeral port and will choose an address set
1003 * equivalent to binding with a wildcard address.
1005 if (!ep->base.bind_addr.port) {
1006 if (sctp_autobind(sk)) {
1012 * If an unprivileged user inherits a 1-many
1013 * style socket with open associations on a
1014 * privileged port, it MAY be permitted to
1015 * accept new associations, but it SHOULD NOT
1016 * be permitted to open new associations.
1018 if (ep->base.bind_addr.port < PROT_SOCK &&
1019 !capable(CAP_NET_BIND_SERVICE)) {
1025 scope = sctp_scope(&to);
1026 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1033 /* Prime the peer's transport structures. */
1034 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1046 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1051 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1056 /* Initialize sk's dport and daddr for getpeername() */
1057 inet_sk(sk)->dport = htons(asoc->peer.port);
1058 af = sctp_get_af_specific(to.sa.sa_family);
1059 af->to_sk_daddr(&to, sk);
1061 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1062 err = sctp_wait_for_connect(asoc, &timeo);
1064 /* Don't free association on exit. */
1069 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1070 " kaddrs: %p err: %d\n",
1073 sctp_association_free(asoc);
1077 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1080 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1082 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1083 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1084 * or IPv6 addresses.
1086 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1087 * Section 3.1.2 for this usage.
1089 * addrs is a pointer to an array of one or more socket addresses. Each
1090 * address is contained in its appropriate structure (i.e. struct
1091 * sockaddr_in or struct sockaddr_in6) the family of the address type
1092 * must be used to distengish the address length (note that this
1093 * representation is termed a "packed array" of addresses). The caller
1094 * specifies the number of addresses in the array with addrcnt.
1096 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1097 * -1, and sets errno to the appropriate error code.
1099 * For SCTP, the port given in each socket address must be the same, or
1100 * sctp_connectx() will fail, setting errno to EINVAL.
1102 * An application can use sctp_connectx to initiate an association with
1103 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1104 * allows a caller to specify multiple addresses at which a peer can be
1105 * reached. The way the SCTP stack uses the list of addresses to set up
1106 * the association is implementation dependant. This function only
1107 * specifies that the stack will try to make use of all the addresses in
1108 * the list when needed.
1110 * Note that the list of addresses passed in is only used for setting up
1111 * the association. It does not necessarily equal the set of addresses
1112 * the peer uses for the resulting association. If the caller wants to
1113 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1114 * retrieve them after the association has been set up.
1116 * Basically do nothing but copying the addresses from user to kernel
1117 * land and invoking either sctp_connectx(). This is used for tunneling
1118 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1120 * We don't use copy_from_user() for optimization: we first do the
1121 * sanity checks (buffer size -fast- and access check-healthy
1122 * pointer); if all of those succeed, then we can alloc the memory
1123 * (expensive operation) needed to copy the data to kernel. Then we do
1124 * the copying without checking the user space area
1125 * (__copy_from_user()).
1127 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1130 * sk The sk of the socket
1131 * addrs The pointer to the addresses in user land
1132 * addrssize Size of the addrs buffer
1134 * Returns 0 if ok, <0 errno code on error.
1136 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1137 struct sockaddr __user *addrs,
1141 struct sockaddr *kaddrs;
1143 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1144 __FUNCTION__, sk, addrs, addrs_size);
1146 if (unlikely(addrs_size <= 0))
1149 /* Check the user passed a healthy pointer. */
1150 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1153 /* Alloc space for the address array in kernel memory. */
1154 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1155 if (unlikely(!kaddrs))
1158 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1161 err = __sctp_connect(sk, kaddrs, addrs_size);
1168 /* API 3.1.4 close() - UDP Style Syntax
1169 * Applications use close() to perform graceful shutdown (as described in
1170 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1171 * by a UDP-style socket.
1175 * ret = close(int sd);
1177 * sd - the socket descriptor of the associations to be closed.
1179 * To gracefully shutdown a specific association represented by the
1180 * UDP-style socket, an application should use the sendmsg() call,
1181 * passing no user data, but including the appropriate flag in the
1182 * ancillary data (see Section xxxx).
1184 * If sd in the close() call is a branched-off socket representing only
1185 * one association, the shutdown is performed on that association only.
1187 * 4.1.6 close() - TCP Style Syntax
1189 * Applications use close() to gracefully close down an association.
1193 * int close(int sd);
1195 * sd - the socket descriptor of the association to be closed.
1197 * After an application calls close() on a socket descriptor, no further
1198 * socket operations will succeed on that descriptor.
1200 * API 7.1.4 SO_LINGER
1202 * An application using the TCP-style socket can use this option to
1203 * perform the SCTP ABORT primitive. The linger option structure is:
1206 * int l_onoff; // option on/off
1207 * int l_linger; // linger time
1210 * To enable the option, set l_onoff to 1. If the l_linger value is set
1211 * to 0, calling close() is the same as the ABORT primitive. If the
1212 * value is set to a negative value, the setsockopt() call will return
1213 * an error. If the value is set to a positive value linger_time, the
1214 * close() can be blocked for at most linger_time ms. If the graceful
1215 * shutdown phase does not finish during this period, close() will
1216 * return but the graceful shutdown phase continues in the system.
1218 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1220 struct sctp_endpoint *ep;
1221 struct sctp_association *asoc;
1222 struct list_head *pos, *temp;
1224 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1227 sk->sk_shutdown = SHUTDOWN_MASK;
1229 ep = sctp_sk(sk)->ep;
1231 /* Walk all associations on a socket, not on an endpoint. */
1232 list_for_each_safe(pos, temp, &ep->asocs) {
1233 asoc = list_entry(pos, struct sctp_association, asocs);
1235 if (sctp_style(sk, TCP)) {
1236 /* A closed association can still be in the list if
1237 * it belongs to a TCP-style listening socket that is
1238 * not yet accepted. If so, free it. If not, send an
1239 * ABORT or SHUTDOWN based on the linger options.
1241 if (sctp_state(asoc, CLOSED)) {
1242 sctp_unhash_established(asoc);
1243 sctp_association_free(asoc);
1245 } else if (sock_flag(sk, SOCK_LINGER) &&
1246 !sk->sk_lingertime) {
1247 struct sctp_chunk *chunk;
1249 chunk = sctp_make_abort_user(asoc, NULL, 0);
1251 sctp_primitive_ABORT(asoc, chunk);
1253 sctp_primitive_SHUTDOWN(asoc, NULL);
1255 sctp_primitive_SHUTDOWN(asoc, NULL);
1258 /* Clean up any skbs sitting on the receive queue. */
1259 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1260 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1262 /* On a TCP-style socket, block for at most linger_time if set. */
1263 if (sctp_style(sk, TCP) && timeout)
1264 sctp_wait_for_close(sk, timeout);
1266 /* This will run the backlog queue. */
1267 sctp_release_sock(sk);
1269 /* Supposedly, no process has access to the socket, but
1270 * the net layers still may.
1272 sctp_local_bh_disable();
1273 sctp_bh_lock_sock(sk);
1275 /* Hold the sock, since sk_common_release() will put sock_put()
1276 * and we have just a little more cleanup.
1279 sk_common_release(sk);
1281 sctp_bh_unlock_sock(sk);
1282 sctp_local_bh_enable();
1286 SCTP_DBG_OBJCNT_DEC(sock);
1289 /* Handle EPIPE error. */
1290 static int sctp_error(struct sock *sk, int flags, int err)
1293 err = sock_error(sk) ? : -EPIPE;
1294 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1295 send_sig(SIGPIPE, current, 0);
1299 /* API 3.1.3 sendmsg() - UDP Style Syntax
1301 * An application uses sendmsg() and recvmsg() calls to transmit data to
1302 * and receive data from its peer.
1304 * ssize_t sendmsg(int socket, const struct msghdr *message,
1307 * socket - the socket descriptor of the endpoint.
1308 * message - pointer to the msghdr structure which contains a single
1309 * user message and possibly some ancillary data.
1311 * See Section 5 for complete description of the data
1314 * flags - flags sent or received with the user message, see Section
1315 * 5 for complete description of the flags.
1317 * Note: This function could use a rewrite especially when explicit
1318 * connect support comes in.
1320 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1322 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1324 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1325 struct msghdr *msg, size_t msg_len)
1327 struct sctp_sock *sp;
1328 struct sctp_endpoint *ep;
1329 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1330 struct sctp_transport *transport, *chunk_tp;
1331 struct sctp_chunk *chunk;
1333 struct sockaddr *msg_name = NULL;
1334 struct sctp_sndrcvinfo default_sinfo = { 0 };
1335 struct sctp_sndrcvinfo *sinfo;
1336 struct sctp_initmsg *sinit;
1337 sctp_assoc_t associd = 0;
1338 sctp_cmsgs_t cmsgs = { NULL };
1342 __u16 sinfo_flags = 0;
1343 struct sctp_datamsg *datamsg;
1344 struct list_head *pos;
1345 int msg_flags = msg->msg_flags;
1347 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1354 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1356 /* We cannot send a message over a TCP-style listening socket. */
1357 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1362 /* Parse out the SCTP CMSGs. */
1363 err = sctp_msghdr_parse(msg, &cmsgs);
1366 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1370 /* Fetch the destination address for this packet. This
1371 * address only selects the association--it is not necessarily
1372 * the address we will send to.
1373 * For a peeled-off socket, msg_name is ignored.
1375 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1376 int msg_namelen = msg->msg_namelen;
1378 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1383 if (msg_namelen > sizeof(to))
1384 msg_namelen = sizeof(to);
1385 memcpy(&to, msg->msg_name, msg_namelen);
1386 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1388 to.v4.sin_addr.s_addr, to.v4.sin_port);
1390 to.v4.sin_port = ntohs(to.v4.sin_port);
1391 msg_name = msg->msg_name;
1397 /* Did the user specify SNDRCVINFO? */
1399 sinfo_flags = sinfo->sinfo_flags;
1400 associd = sinfo->sinfo_assoc_id;
1403 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1404 msg_len, sinfo_flags);
1406 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1407 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1412 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1413 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1414 * If SCTP_ABORT is set, the message length could be non zero with
1415 * the msg_iov set to the user abort reason.
1417 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1418 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1423 /* If SCTP_ADDR_OVER is set, there must be an address
1424 * specified in msg_name.
1426 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1433 SCTP_DEBUG_PRINTK("About to look up association.\n");
1437 /* If a msg_name has been specified, assume this is to be used. */
1439 /* Look for a matching association on the endpoint. */
1440 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1442 /* If we could not find a matching association on the
1443 * endpoint, make sure that it is not a TCP-style
1444 * socket that already has an association or there is
1445 * no peeled-off association on another socket.
1447 if ((sctp_style(sk, TCP) &&
1448 sctp_sstate(sk, ESTABLISHED)) ||
1449 sctp_endpoint_is_peeled_off(ep, &to)) {
1450 err = -EADDRNOTAVAIL;
1455 asoc = sctp_id2assoc(sk, associd);
1463 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1465 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1466 * socket that has an association in CLOSED state. This can
1467 * happen when an accepted socket has an association that is
1470 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1475 if (sinfo_flags & SCTP_EOF) {
1476 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1478 sctp_primitive_SHUTDOWN(asoc, NULL);
1482 if (sinfo_flags & SCTP_ABORT) {
1483 struct sctp_chunk *chunk;
1485 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1491 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1492 sctp_primitive_ABORT(asoc, chunk);
1498 /* Do we need to create the association? */
1500 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1502 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1507 /* Check for invalid stream against the stream counts,
1508 * either the default or the user specified stream counts.
1511 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1512 /* Check against the defaults. */
1513 if (sinfo->sinfo_stream >=
1514 sp->initmsg.sinit_num_ostreams) {
1519 /* Check against the requested. */
1520 if (sinfo->sinfo_stream >=
1521 sinit->sinit_num_ostreams) {
1529 * API 3.1.2 bind() - UDP Style Syntax
1530 * If a bind() or sctp_bindx() is not called prior to a
1531 * sendmsg() call that initiates a new association, the
1532 * system picks an ephemeral port and will choose an address
1533 * set equivalent to binding with a wildcard address.
1535 if (!ep->base.bind_addr.port) {
1536 if (sctp_autobind(sk)) {
1542 * If an unprivileged user inherits a one-to-many
1543 * style socket with open associations on a privileged
1544 * port, it MAY be permitted to accept new associations,
1545 * but it SHOULD NOT be permitted to open new
1548 if (ep->base.bind_addr.port < PROT_SOCK &&
1549 !capable(CAP_NET_BIND_SERVICE)) {
1555 scope = sctp_scope(&to);
1556 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1563 /* If the SCTP_INIT ancillary data is specified, set all
1564 * the association init values accordingly.
1567 if (sinit->sinit_num_ostreams) {
1568 asoc->c.sinit_num_ostreams =
1569 sinit->sinit_num_ostreams;
1571 if (sinit->sinit_max_instreams) {
1572 asoc->c.sinit_max_instreams =
1573 sinit->sinit_max_instreams;
1575 if (sinit->sinit_max_attempts) {
1576 asoc->max_init_attempts
1577 = sinit->sinit_max_attempts;
1579 if (sinit->sinit_max_init_timeo) {
1580 asoc->max_init_timeo =
1581 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1585 /* Prime the peer's transport structures. */
1586 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1591 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1598 /* ASSERT: we have a valid association at this point. */
1599 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1602 /* If the user didn't specify SNDRCVINFO, make up one with
1605 default_sinfo.sinfo_stream = asoc->default_stream;
1606 default_sinfo.sinfo_flags = asoc->default_flags;
1607 default_sinfo.sinfo_ppid = asoc->default_ppid;
1608 default_sinfo.sinfo_context = asoc->default_context;
1609 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1610 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1611 sinfo = &default_sinfo;
1614 /* API 7.1.7, the sndbuf size per association bounds the
1615 * maximum size of data that can be sent in a single send call.
1617 if (msg_len > sk->sk_sndbuf) {
1622 /* If fragmentation is disabled and the message length exceeds the
1623 * association fragmentation point, return EMSGSIZE. The I-D
1624 * does not specify what this error is, but this looks like
1627 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1633 /* Check for invalid stream. */
1634 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1640 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1641 if (!sctp_wspace(asoc)) {
1642 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1647 /* If an address is passed with the sendto/sendmsg call, it is used
1648 * to override the primary destination address in the TCP model, or
1649 * when SCTP_ADDR_OVER flag is set in the UDP model.
1651 if ((sctp_style(sk, TCP) && msg_name) ||
1652 (sinfo_flags & SCTP_ADDR_OVER)) {
1653 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1661 /* Auto-connect, if we aren't connected already. */
1662 if (sctp_state(asoc, CLOSED)) {
1663 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1666 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1669 /* Break the message into multiple chunks of maximum size. */
1670 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1676 /* Now send the (possibly) fragmented message. */
1677 list_for_each(pos, &datamsg->chunks) {
1678 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1679 sctp_datamsg_track(chunk);
1681 /* Do accounting for the write space. */
1682 sctp_set_owner_w(chunk);
1684 chunk->transport = chunk_tp;
1686 /* Send it to the lower layers. Note: all chunks
1687 * must either fail or succeed. The lower layer
1688 * works that way today. Keep it that way or this
1691 err = sctp_primitive_SEND(asoc, chunk);
1692 /* Did the lower layer accept the chunk? */
1694 sctp_chunk_free(chunk);
1695 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1698 sctp_datamsg_free(datamsg);
1704 /* If we are already past ASSOCIATE, the lower
1705 * layers are responsible for association cleanup.
1711 sctp_association_free(asoc);
1713 sctp_release_sock(sk);
1716 return sctp_error(sk, msg_flags, err);
1723 err = sock_error(sk);
1733 /* This is an extended version of skb_pull() that removes the data from the
1734 * start of a skb even when data is spread across the list of skb's in the
1735 * frag_list. len specifies the total amount of data that needs to be removed.
1736 * when 'len' bytes could be removed from the skb, it returns 0.
1737 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1738 * could not be removed.
1740 static int sctp_skb_pull(struct sk_buff *skb, int len)
1742 struct sk_buff *list;
1743 int skb_len = skb_headlen(skb);
1746 if (len <= skb_len) {
1747 __skb_pull(skb, len);
1751 __skb_pull(skb, skb_len);
1753 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1754 rlen = sctp_skb_pull(list, len);
1755 skb->len -= (len-rlen);
1756 skb->data_len -= (len-rlen);
1767 /* API 3.1.3 recvmsg() - UDP Style Syntax
1769 * ssize_t recvmsg(int socket, struct msghdr *message,
1772 * socket - the socket descriptor of the endpoint.
1773 * message - pointer to the msghdr structure which contains a single
1774 * user message and possibly some ancillary data.
1776 * See Section 5 for complete description of the data
1779 * flags - flags sent or received with the user message, see Section
1780 * 5 for complete description of the flags.
1782 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1784 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1785 struct msghdr *msg, size_t len, int noblock,
1786 int flags, int *addr_len)
1788 struct sctp_ulpevent *event = NULL;
1789 struct sctp_sock *sp = sctp_sk(sk);
1790 struct sk_buff *skb;
1795 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1796 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1797 "len", len, "knoblauch", noblock,
1798 "flags", flags, "addr_len", addr_len);
1802 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1807 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1811 /* Get the total length of the skb including any skb's in the
1820 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1822 event = sctp_skb2event(skb);
1827 sock_recv_timestamp(msg, sk, skb);
1828 if (sctp_ulpevent_is_notification(event)) {
1829 msg->msg_flags |= MSG_NOTIFICATION;
1830 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1832 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1835 /* Check if we allow SCTP_SNDRCVINFO. */
1836 if (sp->subscribe.sctp_data_io_event)
1837 sctp_ulpevent_read_sndrcvinfo(event, msg);
1839 /* FIXME: we should be calling IP/IPv6 layers. */
1840 if (sk->sk_protinfo.af_inet.cmsg_flags)
1841 ip_cmsg_recv(msg, skb);
1846 /* If skb's length exceeds the user's buffer, update the skb and
1847 * push it back to the receive_queue so that the next call to
1848 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1850 if (skb_len > copied) {
1851 msg->msg_flags &= ~MSG_EOR;
1852 if (flags & MSG_PEEK)
1854 sctp_skb_pull(skb, copied);
1855 skb_queue_head(&sk->sk_receive_queue, skb);
1857 /* When only partial message is copied to the user, increase
1858 * rwnd by that amount. If all the data in the skb is read,
1859 * rwnd is updated when the event is freed.
1861 sctp_assoc_rwnd_increase(event->asoc, copied);
1863 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1864 (event->msg_flags & MSG_EOR))
1865 msg->msg_flags |= MSG_EOR;
1867 msg->msg_flags &= ~MSG_EOR;
1870 if (flags & MSG_PEEK) {
1871 /* Release the skb reference acquired after peeking the skb in
1872 * sctp_skb_recv_datagram().
1876 /* Free the event which includes releasing the reference to
1877 * the owner of the skb, freeing the skb and updating the
1880 sctp_ulpevent_free(event);
1883 sctp_release_sock(sk);
1887 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1889 * This option is a on/off flag. If enabled no SCTP message
1890 * fragmentation will be performed. Instead if a message being sent
1891 * exceeds the current PMTU size, the message will NOT be sent and
1892 * instead a error will be indicated to the user.
1894 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1895 char __user *optval, int optlen)
1899 if (optlen < sizeof(int))
1902 if (get_user(val, (int __user *)optval))
1905 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1910 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1913 if (optlen != sizeof(struct sctp_event_subscribe))
1915 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1920 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1922 * This socket option is applicable to the UDP-style socket only. When
1923 * set it will cause associations that are idle for more than the
1924 * specified number of seconds to automatically close. An association
1925 * being idle is defined an association that has NOT sent or received
1926 * user data. The special value of '0' indicates that no automatic
1927 * close of any associations should be performed. The option expects an
1928 * integer defining the number of seconds of idle time before an
1929 * association is closed.
1931 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1934 struct sctp_sock *sp = sctp_sk(sk);
1936 /* Applicable to UDP-style socket only */
1937 if (sctp_style(sk, TCP))
1939 if (optlen != sizeof(int))
1941 if (copy_from_user(&sp->autoclose, optval, optlen))
1947 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1949 * Applications can enable or disable heartbeats for any peer address of
1950 * an association, modify an address's heartbeat interval, force a
1951 * heartbeat to be sent immediately, and adjust the address's maximum
1952 * number of retransmissions sent before an address is considered
1953 * unreachable. The following structure is used to access and modify an
1954 * address's parameters:
1956 * struct sctp_paddrparams {
1957 * sctp_assoc_t spp_assoc_id;
1958 * struct sockaddr_storage spp_address;
1959 * uint32_t spp_hbinterval;
1960 * uint16_t spp_pathmaxrxt;
1961 * uint32_t spp_pathmtu;
1962 * uint32_t spp_sackdelay;
1963 * uint32_t spp_flags;
1966 * spp_assoc_id - (one-to-many style socket) This is filled in the
1967 * application, and identifies the association for
1969 * spp_address - This specifies which address is of interest.
1970 * spp_hbinterval - This contains the value of the heartbeat interval,
1971 * in milliseconds. If a value of zero
1972 * is present in this field then no changes are to
1973 * be made to this parameter.
1974 * spp_pathmaxrxt - This contains the maximum number of
1975 * retransmissions before this address shall be
1976 * considered unreachable. If a value of zero
1977 * is present in this field then no changes are to
1978 * be made to this parameter.
1979 * spp_pathmtu - When Path MTU discovery is disabled the value
1980 * specified here will be the "fixed" path mtu.
1981 * Note that if the spp_address field is empty
1982 * then all associations on this address will
1983 * have this fixed path mtu set upon them.
1985 * spp_sackdelay - When delayed sack is enabled, this value specifies
1986 * the number of milliseconds that sacks will be delayed
1987 * for. This value will apply to all addresses of an
1988 * association if the spp_address field is empty. Note
1989 * also, that if delayed sack is enabled and this
1990 * value is set to 0, no change is made to the last
1991 * recorded delayed sack timer value.
1993 * spp_flags - These flags are used to control various features
1994 * on an association. The flag field may contain
1995 * zero or more of the following options.
1997 * SPP_HB_ENABLE - Enable heartbeats on the
1998 * specified address. Note that if the address
1999 * field is empty all addresses for the association
2000 * have heartbeats enabled upon them.
2002 * SPP_HB_DISABLE - Disable heartbeats on the
2003 * speicifed address. Note that if the address
2004 * field is empty all addresses for the association
2005 * will have their heartbeats disabled. Note also
2006 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2007 * mutually exclusive, only one of these two should
2008 * be specified. Enabling both fields will have
2009 * undetermined results.
2011 * SPP_HB_DEMAND - Request a user initiated heartbeat
2012 * to be made immediately.
2014 * SPP_PMTUD_ENABLE - This field will enable PMTU
2015 * discovery upon the specified address. Note that
2016 * if the address feild is empty then all addresses
2017 * on the association are effected.
2019 * SPP_PMTUD_DISABLE - This field will disable PMTU
2020 * discovery upon the specified address. Note that
2021 * if the address feild is empty then all addresses
2022 * on the association are effected. Not also that
2023 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2024 * exclusive. Enabling both will have undetermined
2027 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2028 * on delayed sack. The time specified in spp_sackdelay
2029 * is used to specify the sack delay for this address. Note
2030 * that if spp_address is empty then all addresses will
2031 * enable delayed sack and take on the sack delay
2032 * value specified in spp_sackdelay.
2033 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2034 * off delayed sack. If the spp_address field is blank then
2035 * delayed sack is disabled for the entire association. Note
2036 * also that this field is mutually exclusive to
2037 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2040 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2041 struct sctp_transport *trans,
2042 struct sctp_association *asoc,
2043 struct sctp_sock *sp,
2046 int sackdelay_change)
2050 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2051 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2056 if (params->spp_hbinterval) {
2058 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2060 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2062 sp->hbinterval = params->spp_hbinterval;
2068 trans->param_flags =
2069 (trans->param_flags & ~SPP_HB) | hb_change;
2072 (asoc->param_flags & ~SPP_HB) | hb_change;
2075 (sp->param_flags & ~SPP_HB) | hb_change;
2079 if (params->spp_pathmtu) {
2081 trans->pathmtu = params->spp_pathmtu;
2082 sctp_assoc_sync_pmtu(asoc);
2084 asoc->pathmtu = params->spp_pathmtu;
2085 sctp_frag_point(sp, params->spp_pathmtu);
2087 sp->pathmtu = params->spp_pathmtu;
2093 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2094 (params->spp_flags & SPP_PMTUD_ENABLE);
2095 trans->param_flags =
2096 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2098 sctp_transport_pmtu(trans);
2099 sctp_assoc_sync_pmtu(asoc);
2103 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2106 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2110 if (params->spp_sackdelay) {
2113 msecs_to_jiffies(params->spp_sackdelay);
2116 msecs_to_jiffies(params->spp_sackdelay);
2118 sp->sackdelay = params->spp_sackdelay;
2122 if (sackdelay_change) {
2124 trans->param_flags =
2125 (trans->param_flags & ~SPP_SACKDELAY) |
2129 (asoc->param_flags & ~SPP_SACKDELAY) |
2133 (sp->param_flags & ~SPP_SACKDELAY) |
2138 if (params->spp_pathmaxrxt) {
2140 trans->pathmaxrxt = params->spp_pathmaxrxt;
2142 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2144 sp->pathmaxrxt = params->spp_pathmaxrxt;
2151 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2152 char __user *optval, int optlen)
2154 struct sctp_paddrparams params;
2155 struct sctp_transport *trans = NULL;
2156 struct sctp_association *asoc = NULL;
2157 struct sctp_sock *sp = sctp_sk(sk);
2159 int hb_change, pmtud_change, sackdelay_change;
2161 if (optlen != sizeof(struct sctp_paddrparams))
2164 if (copy_from_user(¶ms, optval, optlen))
2167 /* Validate flags and value parameters. */
2168 hb_change = params.spp_flags & SPP_HB;
2169 pmtud_change = params.spp_flags & SPP_PMTUD;
2170 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2172 if (hb_change == SPP_HB ||
2173 pmtud_change == SPP_PMTUD ||
2174 sackdelay_change == SPP_SACKDELAY ||
2175 params.spp_sackdelay > 500 ||
2177 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2180 /* If an address other than INADDR_ANY is specified, and
2181 * no transport is found, then the request is invalid.
2183 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
2184 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2185 params.spp_assoc_id);
2190 /* Get association, if assoc_id != 0 and the socket is a one
2191 * to many style socket, and an association was not found, then
2192 * the id was invalid.
2194 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2195 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2198 /* Heartbeat demand can only be sent on a transport or
2199 * association, but not a socket.
2201 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2204 /* Process parameters. */
2205 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2206 hb_change, pmtud_change,
2212 /* If changes are for association, also apply parameters to each
2215 if (!trans && asoc) {
2216 struct list_head *pos;
2218 list_for_each(pos, &asoc->peer.transport_addr_list) {
2219 trans = list_entry(pos, struct sctp_transport,
2221 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2222 hb_change, pmtud_change,
2230 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2232 * This options will get or set the delayed ack timer. The time is set
2233 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2234 * endpoints default delayed ack timer value. If the assoc_id field is
2235 * non-zero, then the set or get effects the specified association.
2237 * struct sctp_assoc_value {
2238 * sctp_assoc_t assoc_id;
2239 * uint32_t assoc_value;
2242 * assoc_id - This parameter, indicates which association the
2243 * user is preforming an action upon. Note that if
2244 * this field's value is zero then the endpoints
2245 * default value is changed (effecting future
2246 * associations only).
2248 * assoc_value - This parameter contains the number of milliseconds
2249 * that the user is requesting the delayed ACK timer
2250 * be set to. Note that this value is defined in
2251 * the standard to be between 200 and 500 milliseconds.
2253 * Note: a value of zero will leave the value alone,
2254 * but disable SACK delay. A non-zero value will also
2255 * enable SACK delay.
2258 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2259 char __user *optval, int optlen)
2261 struct sctp_assoc_value params;
2262 struct sctp_transport *trans = NULL;
2263 struct sctp_association *asoc = NULL;
2264 struct sctp_sock *sp = sctp_sk(sk);
2266 if (optlen != sizeof(struct sctp_assoc_value))
2269 if (copy_from_user(¶ms, optval, optlen))
2272 /* Validate value parameter. */
2273 if (params.assoc_value > 500)
2276 /* Get association, if assoc_id != 0 and the socket is a one
2277 * to many style socket, and an association was not found, then
2278 * the id was invalid.
2280 asoc = sctp_id2assoc(sk, params.assoc_id);
2281 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2284 if (params.assoc_value) {
2287 msecs_to_jiffies(params.assoc_value);
2289 (asoc->param_flags & ~SPP_SACKDELAY) |
2290 SPP_SACKDELAY_ENABLE;
2292 sp->sackdelay = params.assoc_value;
2294 (sp->param_flags & ~SPP_SACKDELAY) |
2295 SPP_SACKDELAY_ENABLE;
2300 (asoc->param_flags & ~SPP_SACKDELAY) |
2301 SPP_SACKDELAY_DISABLE;
2304 (sp->param_flags & ~SPP_SACKDELAY) |
2305 SPP_SACKDELAY_DISABLE;
2309 /* If change is for association, also apply to each transport. */
2311 struct list_head *pos;
2313 list_for_each(pos, &asoc->peer.transport_addr_list) {
2314 trans = list_entry(pos, struct sctp_transport,
2316 if (params.assoc_value) {
2318 msecs_to_jiffies(params.assoc_value);
2319 trans->param_flags =
2320 (trans->param_flags & ~SPP_SACKDELAY) |
2321 SPP_SACKDELAY_ENABLE;
2323 trans->param_flags =
2324 (trans->param_flags & ~SPP_SACKDELAY) |
2325 SPP_SACKDELAY_DISABLE;
2333 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2335 * Applications can specify protocol parameters for the default association
2336 * initialization. The option name argument to setsockopt() and getsockopt()
2339 * Setting initialization parameters is effective only on an unconnected
2340 * socket (for UDP-style sockets only future associations are effected
2341 * by the change). With TCP-style sockets, this option is inherited by
2342 * sockets derived from a listener socket.
2344 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2346 struct sctp_initmsg sinit;
2347 struct sctp_sock *sp = sctp_sk(sk);
2349 if (optlen != sizeof(struct sctp_initmsg))
2351 if (copy_from_user(&sinit, optval, optlen))
2354 if (sinit.sinit_num_ostreams)
2355 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2356 if (sinit.sinit_max_instreams)
2357 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2358 if (sinit.sinit_max_attempts)
2359 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2360 if (sinit.sinit_max_init_timeo)
2361 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2367 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2369 * Applications that wish to use the sendto() system call may wish to
2370 * specify a default set of parameters that would normally be supplied
2371 * through the inclusion of ancillary data. This socket option allows
2372 * such an application to set the default sctp_sndrcvinfo structure.
2373 * The application that wishes to use this socket option simply passes
2374 * in to this call the sctp_sndrcvinfo structure defined in Section
2375 * 5.2.2) The input parameters accepted by this call include
2376 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2377 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2378 * to this call if the caller is using the UDP model.
2380 static int sctp_setsockopt_default_send_param(struct sock *sk,
2381 char __user *optval, int optlen)
2383 struct sctp_sndrcvinfo info;
2384 struct sctp_association *asoc;
2385 struct sctp_sock *sp = sctp_sk(sk);
2387 if (optlen != sizeof(struct sctp_sndrcvinfo))
2389 if (copy_from_user(&info, optval, optlen))
2392 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2393 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2397 asoc->default_stream = info.sinfo_stream;
2398 asoc->default_flags = info.sinfo_flags;
2399 asoc->default_ppid = info.sinfo_ppid;
2400 asoc->default_context = info.sinfo_context;
2401 asoc->default_timetolive = info.sinfo_timetolive;
2403 sp->default_stream = info.sinfo_stream;
2404 sp->default_flags = info.sinfo_flags;
2405 sp->default_ppid = info.sinfo_ppid;
2406 sp->default_context = info.sinfo_context;
2407 sp->default_timetolive = info.sinfo_timetolive;
2413 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2415 * Requests that the local SCTP stack use the enclosed peer address as
2416 * the association primary. The enclosed address must be one of the
2417 * association peer's addresses.
2419 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2422 struct sctp_prim prim;
2423 struct sctp_transport *trans;
2425 if (optlen != sizeof(struct sctp_prim))
2428 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2431 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2435 sctp_assoc_set_primary(trans->asoc, trans);
2441 * 7.1.5 SCTP_NODELAY
2443 * Turn on/off any Nagle-like algorithm. This means that packets are
2444 * generally sent as soon as possible and no unnecessary delays are
2445 * introduced, at the cost of more packets in the network. Expects an
2446 * integer boolean flag.
2448 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2453 if (optlen < sizeof(int))
2455 if (get_user(val, (int __user *)optval))
2458 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2464 * 7.1.1 SCTP_RTOINFO
2466 * The protocol parameters used to initialize and bound retransmission
2467 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2468 * and modify these parameters.
2469 * All parameters are time values, in milliseconds. A value of 0, when
2470 * modifying the parameters, indicates that the current value should not
2474 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2475 struct sctp_rtoinfo rtoinfo;
2476 struct sctp_association *asoc;
2478 if (optlen != sizeof (struct sctp_rtoinfo))
2481 if (copy_from_user(&rtoinfo, optval, optlen))
2484 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2486 /* Set the values to the specific association */
2487 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2491 if (rtoinfo.srto_initial != 0)
2493 msecs_to_jiffies(rtoinfo.srto_initial);
2494 if (rtoinfo.srto_max != 0)
2495 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2496 if (rtoinfo.srto_min != 0)
2497 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2499 /* If there is no association or the association-id = 0
2500 * set the values to the endpoint.
2502 struct sctp_sock *sp = sctp_sk(sk);
2504 if (rtoinfo.srto_initial != 0)
2505 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2506 if (rtoinfo.srto_max != 0)
2507 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2508 if (rtoinfo.srto_min != 0)
2509 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2517 * 7.1.2 SCTP_ASSOCINFO
2519 * This option is used to tune the the maximum retransmission attempts
2520 * of the association.
2521 * Returns an error if the new association retransmission value is
2522 * greater than the sum of the retransmission value of the peer.
2523 * See [SCTP] for more information.
2526 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2529 struct sctp_assocparams assocparams;
2530 struct sctp_association *asoc;
2532 if (optlen != sizeof(struct sctp_assocparams))
2534 if (copy_from_user(&assocparams, optval, optlen))
2537 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2539 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2542 /* Set the values to the specific association */
2544 if (assocparams.sasoc_asocmaxrxt != 0) {
2547 struct list_head *pos;
2548 struct sctp_transport *peer_addr;
2550 list_for_each(pos, &asoc->peer.transport_addr_list) {
2551 peer_addr = list_entry(pos,
2552 struct sctp_transport,
2554 path_sum += peer_addr->pathmaxrxt;
2558 /* Only validate asocmaxrxt if we have more then
2559 * one path/transport. We do this because path
2560 * retransmissions are only counted when we have more
2564 assocparams.sasoc_asocmaxrxt > path_sum)
2567 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2570 if (assocparams.sasoc_cookie_life != 0) {
2571 asoc->cookie_life.tv_sec =
2572 assocparams.sasoc_cookie_life / 1000;
2573 asoc->cookie_life.tv_usec =
2574 (assocparams.sasoc_cookie_life % 1000)
2578 /* Set the values to the endpoint */
2579 struct sctp_sock *sp = sctp_sk(sk);
2581 if (assocparams.sasoc_asocmaxrxt != 0)
2582 sp->assocparams.sasoc_asocmaxrxt =
2583 assocparams.sasoc_asocmaxrxt;
2584 if (assocparams.sasoc_cookie_life != 0)
2585 sp->assocparams.sasoc_cookie_life =
2586 assocparams.sasoc_cookie_life;
2592 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2594 * This socket option is a boolean flag which turns on or off mapped V4
2595 * addresses. If this option is turned on and the socket is type
2596 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2597 * If this option is turned off, then no mapping will be done of V4
2598 * addresses and a user will receive both PF_INET6 and PF_INET type
2599 * addresses on the socket.
2601 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2604 struct sctp_sock *sp = sctp_sk(sk);
2606 if (optlen < sizeof(int))
2608 if (get_user(val, (int __user *)optval))
2619 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2621 * This socket option specifies the maximum size to put in any outgoing
2622 * SCTP chunk. If a message is larger than this size it will be
2623 * fragmented by SCTP into the specified size. Note that the underlying
2624 * SCTP implementation may fragment into smaller sized chunks when the
2625 * PMTU of the underlying association is smaller than the value set by
2628 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2630 struct sctp_association *asoc;
2631 struct list_head *pos;
2632 struct sctp_sock *sp = sctp_sk(sk);
2635 if (optlen < sizeof(int))
2637 if (get_user(val, (int __user *)optval))
2639 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2641 sp->user_frag = val;
2643 /* Update the frag_point of the existing associations. */
2644 list_for_each(pos, &(sp->ep->asocs)) {
2645 asoc = list_entry(pos, struct sctp_association, asocs);
2646 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2654 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2656 * Requests that the peer mark the enclosed address as the association
2657 * primary. The enclosed address must be one of the association's
2658 * locally bound addresses. The following structure is used to make a
2659 * set primary request:
2661 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2664 struct sctp_sock *sp;
2665 struct sctp_endpoint *ep;
2666 struct sctp_association *asoc = NULL;
2667 struct sctp_setpeerprim prim;
2668 struct sctp_chunk *chunk;
2674 if (!sctp_addip_enable)
2677 if (optlen != sizeof(struct sctp_setpeerprim))
2680 if (copy_from_user(&prim, optval, optlen))
2683 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2687 if (!asoc->peer.asconf_capable)
2690 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2693 if (!sctp_state(asoc, ESTABLISHED))
2696 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2697 return -EADDRNOTAVAIL;
2699 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2700 chunk = sctp_make_asconf_set_prim(asoc,
2701 (union sctp_addr *)&prim.sspp_addr);
2705 err = sctp_send_asconf(asoc, chunk);
2707 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2712 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2715 struct sctp_setadaption adaption;
2717 if (optlen != sizeof(struct sctp_setadaption))
2719 if (copy_from_user(&adaption, optval, optlen))
2722 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2727 /* API 6.2 setsockopt(), getsockopt()
2729 * Applications use setsockopt() and getsockopt() to set or retrieve
2730 * socket options. Socket options are used to change the default
2731 * behavior of sockets calls. They are described in Section 7.
2735 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2736 * int __user *optlen);
2737 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2740 * sd - the socket descript.
2741 * level - set to IPPROTO_SCTP for all SCTP options.
2742 * optname - the option name.
2743 * optval - the buffer to store the value of the option.
2744 * optlen - the size of the buffer.
2746 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2747 char __user *optval, int optlen)
2751 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2754 /* I can hardly begin to describe how wrong this is. This is
2755 * so broken as to be worse than useless. The API draft
2756 * REALLY is NOT helpful here... I am not convinced that the
2757 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2758 * are at all well-founded.
2760 if (level != SOL_SCTP) {
2761 struct sctp_af *af = sctp_sk(sk)->pf->af;
2762 retval = af->setsockopt(sk, level, optname, optval, optlen);
2769 case SCTP_SOCKOPT_BINDX_ADD:
2770 /* 'optlen' is the size of the addresses buffer. */
2771 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2772 optlen, SCTP_BINDX_ADD_ADDR);
2775 case SCTP_SOCKOPT_BINDX_REM:
2776 /* 'optlen' is the size of the addresses buffer. */
2777 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2778 optlen, SCTP_BINDX_REM_ADDR);
2781 case SCTP_SOCKOPT_CONNECTX:
2782 /* 'optlen' is the size of the addresses buffer. */
2783 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2787 case SCTP_DISABLE_FRAGMENTS:
2788 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2792 retval = sctp_setsockopt_events(sk, optval, optlen);
2795 case SCTP_AUTOCLOSE:
2796 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2799 case SCTP_PEER_ADDR_PARAMS:
2800 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2803 case SCTP_DELAYED_ACK_TIME:
2804 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2808 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2810 case SCTP_DEFAULT_SEND_PARAM:
2811 retval = sctp_setsockopt_default_send_param(sk, optval,
2814 case SCTP_PRIMARY_ADDR:
2815 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2817 case SCTP_SET_PEER_PRIMARY_ADDR:
2818 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2821 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2824 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2826 case SCTP_ASSOCINFO:
2827 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2829 case SCTP_I_WANT_MAPPED_V4_ADDR:
2830 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2833 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2835 case SCTP_ADAPTION_LAYER:
2836 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2840 retval = -ENOPROTOOPT;
2844 sctp_release_sock(sk);
2850 /* API 3.1.6 connect() - UDP Style Syntax
2852 * An application may use the connect() call in the UDP model to initiate an
2853 * association without sending data.
2857 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2859 * sd: the socket descriptor to have a new association added to.
2861 * nam: the address structure (either struct sockaddr_in or struct
2862 * sockaddr_in6 defined in RFC2553 [7]).
2864 * len: the size of the address.
2866 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2874 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2875 __FUNCTION__, sk, addr, addr_len);
2877 /* Validate addr_len before calling common connect/connectx routine. */
2878 af = sctp_get_af_specific(addr->sa_family);
2879 if (!af || addr_len < af->sockaddr_len) {
2882 /* Pass correct addr len to common routine (so it knows there
2883 * is only one address being passed.
2885 err = __sctp_connect(sk, addr, af->sockaddr_len);
2888 sctp_release_sock(sk);
2892 /* FIXME: Write comments. */
2893 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2895 return -EOPNOTSUPP; /* STUB */
2898 /* 4.1.4 accept() - TCP Style Syntax
2900 * Applications use accept() call to remove an established SCTP
2901 * association from the accept queue of the endpoint. A new socket
2902 * descriptor will be returned from accept() to represent the newly
2903 * formed association.
2905 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2907 struct sctp_sock *sp;
2908 struct sctp_endpoint *ep;
2909 struct sock *newsk = NULL;
2910 struct sctp_association *asoc;
2919 if (!sctp_style(sk, TCP)) {
2920 error = -EOPNOTSUPP;
2924 if (!sctp_sstate(sk, LISTENING)) {
2929 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2931 error = sctp_wait_for_accept(sk, timeo);
2935 /* We treat the list of associations on the endpoint as the accept
2936 * queue and pick the first association on the list.
2938 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2940 newsk = sp->pf->create_accept_sk(sk, asoc);
2946 /* Populate the fields of the newsk from the oldsk and migrate the
2947 * asoc to the newsk.
2949 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2952 sctp_release_sock(sk);
2957 /* The SCTP ioctl handler. */
2958 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2960 return -ENOIOCTLCMD;
2963 /* This is the function which gets called during socket creation to
2964 * initialized the SCTP-specific portion of the sock.
2965 * The sock structure should already be zero-filled memory.
2967 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2969 struct sctp_endpoint *ep;
2970 struct sctp_sock *sp;
2972 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2976 /* Initialize the SCTP per socket area. */
2977 switch (sk->sk_type) {
2978 case SOCK_SEQPACKET:
2979 sp->type = SCTP_SOCKET_UDP;
2982 sp->type = SCTP_SOCKET_TCP;
2985 return -ESOCKTNOSUPPORT;
2988 /* Initialize default send parameters. These parameters can be
2989 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2991 sp->default_stream = 0;
2992 sp->default_ppid = 0;
2993 sp->default_flags = 0;
2994 sp->default_context = 0;
2995 sp->default_timetolive = 0;
2997 /* Initialize default setup parameters. These parameters
2998 * can be modified with the SCTP_INITMSG socket option or
2999 * overridden by the SCTP_INIT CMSG.
3001 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3002 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3003 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3004 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
3006 /* Initialize default RTO related parameters. These parameters can
3007 * be modified for with the SCTP_RTOINFO socket option.
3009 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
3010 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
3011 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
3013 /* Initialize default association related parameters. These parameters
3014 * can be modified with the SCTP_ASSOCINFO socket option.
3016 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3017 sp->assocparams.sasoc_number_peer_destinations = 0;
3018 sp->assocparams.sasoc_peer_rwnd = 0;
3019 sp->assocparams.sasoc_local_rwnd = 0;
3020 sp->assocparams.sasoc_cookie_life =
3021 jiffies_to_msecs(sctp_valid_cookie_life);
3023 /* Initialize default event subscriptions. By default, all the
3026 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3028 /* Default Peer Address Parameters. These defaults can
3029 * be modified via SCTP_PEER_ADDR_PARAMS
3031 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
3032 sp->pathmaxrxt = sctp_max_retrans_path;
3033 sp->pathmtu = 0; // allow default discovery
3034 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
3035 sp->param_flags = SPP_HB_ENABLE |
3037 SPP_SACKDELAY_ENABLE;
3039 /* If enabled no SCTP message fragmentation will be performed.
3040 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3042 sp->disable_fragments = 0;
3044 /* Turn on/off any Nagle-like algorithm. */
3047 /* Enable by default. */
3050 /* Auto-close idle associations after the configured
3051 * number of seconds. A value of 0 disables this
3052 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3053 * for UDP-style sockets only.
3057 /* User specified fragmentation limit. */
3060 sp->adaption_ind = 0;
3062 sp->pf = sctp_get_pf_specific(sk->sk_family);
3064 /* Control variables for partial data delivery. */
3066 skb_queue_head_init(&sp->pd_lobby);
3068 /* Create a per socket endpoint structure. Even if we
3069 * change the data structure relationships, this may still
3070 * be useful for storing pre-connect address information.
3072 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3079 SCTP_DBG_OBJCNT_INC(sock);
3083 /* Cleanup any SCTP per socket resources. */
3084 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3086 struct sctp_endpoint *ep;
3088 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3090 /* Release our hold on the endpoint. */
3091 ep = sctp_sk(sk)->ep;
3092 sctp_endpoint_free(ep);
3097 /* API 4.1.7 shutdown() - TCP Style Syntax
3098 * int shutdown(int socket, int how);
3100 * sd - the socket descriptor of the association to be closed.
3101 * how - Specifies the type of shutdown. The values are
3104 * Disables further receive operations. No SCTP
3105 * protocol action is taken.
3107 * Disables further send operations, and initiates
3108 * the SCTP shutdown sequence.
3110 * Disables further send and receive operations
3111 * and initiates the SCTP shutdown sequence.
3113 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3115 struct sctp_endpoint *ep;
3116 struct sctp_association *asoc;
3118 if (!sctp_style(sk, TCP))
3121 if (how & SEND_SHUTDOWN) {
3122 ep = sctp_sk(sk)->ep;
3123 if (!list_empty(&ep->asocs)) {
3124 asoc = list_entry(ep->asocs.next,
3125 struct sctp_association, asocs);
3126 sctp_primitive_SHUTDOWN(asoc, NULL);
3131 /* 7.2.1 Association Status (SCTP_STATUS)
3133 * Applications can retrieve current status information about an
3134 * association, including association state, peer receiver window size,
3135 * number of unacked data chunks, and number of data chunks pending
3136 * receipt. This information is read-only.
3138 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3139 char __user *optval,
3142 struct sctp_status status;
3143 struct sctp_association *asoc = NULL;
3144 struct sctp_transport *transport;
3145 sctp_assoc_t associd;
3148 if (len != sizeof(status)) {
3153 if (copy_from_user(&status, optval, sizeof(status))) {
3158 associd = status.sstat_assoc_id;
3159 asoc = sctp_id2assoc(sk, associd);
3165 transport = asoc->peer.primary_path;
3167 status.sstat_assoc_id = sctp_assoc2id(asoc);
3168 status.sstat_state = asoc->state;
3169 status.sstat_rwnd = asoc->peer.rwnd;
3170 status.sstat_unackdata = asoc->unack_data;
3172 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3173 status.sstat_instrms = asoc->c.sinit_max_instreams;
3174 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3175 status.sstat_fragmentation_point = asoc->frag_point;
3176 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3177 memcpy(&status.sstat_primary.spinfo_address,
3178 &(transport->ipaddr), sizeof(union sctp_addr));
3179 /* Map ipv4 address into v4-mapped-on-v6 address. */
3180 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3181 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3182 status.sstat_primary.spinfo_state = transport->state;
3183 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3184 status.sstat_primary.spinfo_srtt = transport->srtt;
3185 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3186 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3188 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3189 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3191 if (put_user(len, optlen)) {
3196 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3197 len, status.sstat_state, status.sstat_rwnd,
3198 status.sstat_assoc_id);
3200 if (copy_to_user(optval, &status, len)) {
3210 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3212 * Applications can retrieve information about a specific peer address
3213 * of an association, including its reachability state, congestion
3214 * window, and retransmission timer values. This information is
3217 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3218 char __user *optval,
3221 struct sctp_paddrinfo pinfo;
3222 struct sctp_transport *transport;
3225 if (len != sizeof(pinfo)) {
3230 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3235 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3236 pinfo.spinfo_assoc_id);
3240 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3241 pinfo.spinfo_state = transport->state;
3242 pinfo.spinfo_cwnd = transport->cwnd;
3243 pinfo.spinfo_srtt = transport->srtt;
3244 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3245 pinfo.spinfo_mtu = transport->pathmtu;
3247 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3248 pinfo.spinfo_state = SCTP_ACTIVE;
3250 if (put_user(len, optlen)) {
3255 if (copy_to_user(optval, &pinfo, len)) {
3264 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3266 * This option is a on/off flag. If enabled no SCTP message
3267 * fragmentation will be performed. Instead if a message being sent
3268 * exceeds the current PMTU size, the message will NOT be sent and
3269 * instead a error will be indicated to the user.
3271 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3272 char __user *optval, int __user *optlen)
3276 if (len < sizeof(int))
3280 val = (sctp_sk(sk)->disable_fragments == 1);
3281 if (put_user(len, optlen))
3283 if (copy_to_user(optval, &val, len))
3288 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3290 * This socket option is used to specify various notifications and
3291 * ancillary data the user wishes to receive.
3293 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3296 if (len != sizeof(struct sctp_event_subscribe))
3298 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3303 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3305 * This socket option is applicable to the UDP-style socket only. When
3306 * set it will cause associations that are idle for more than the
3307 * specified number of seconds to automatically close. An association
3308 * being idle is defined an association that has NOT sent or received
3309 * user data. The special value of '0' indicates that no automatic
3310 * close of any associations should be performed. The option expects an
3311 * integer defining the number of seconds of idle time before an
3312 * association is closed.
3314 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3316 /* Applicable to UDP-style socket only */
3317 if (sctp_style(sk, TCP))
3319 if (len != sizeof(int))
3321 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3326 /* Helper routine to branch off an association to a new socket. */
3327 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3328 struct socket **sockp)
3330 struct sock *sk = asoc->base.sk;
3331 struct socket *sock;
3334 /* An association cannot be branched off from an already peeled-off
3335 * socket, nor is this supported for tcp style sockets.
3337 if (!sctp_style(sk, UDP))
3340 /* Create a new socket. */
3341 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3345 /* Populate the fields of the newsk from the oldsk and migrate the
3346 * asoc to the newsk.
3348 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3354 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3356 sctp_peeloff_arg_t peeloff;
3357 struct socket *newsock;
3359 struct sctp_association *asoc;
3361 if (len != sizeof(sctp_peeloff_arg_t))
3363 if (copy_from_user(&peeloff, optval, len))
3366 asoc = sctp_id2assoc(sk, peeloff.associd);
3372 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3374 retval = sctp_do_peeloff(asoc, &newsock);
3378 /* Map the socket to an unused fd that can be returned to the user. */
3379 retval = sock_map_fd(newsock);
3381 sock_release(newsock);
3385 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3386 __FUNCTION__, sk, asoc, newsock->sk, retval);
3388 /* Return the fd mapped to the new socket. */
3389 peeloff.sd = retval;
3390 if (copy_to_user(optval, &peeloff, len))
3397 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3399 * Applications can enable or disable heartbeats for any peer address of
3400 * an association, modify an address's heartbeat interval, force a
3401 * heartbeat to be sent immediately, and adjust the address's maximum
3402 * number of retransmissions sent before an address is considered
3403 * unreachable. The following structure is used to access and modify an
3404 * address's parameters:
3406 * struct sctp_paddrparams {
3407 * sctp_assoc_t spp_assoc_id;
3408 * struct sockaddr_storage spp_address;
3409 * uint32_t spp_hbinterval;
3410 * uint16_t spp_pathmaxrxt;
3411 * uint32_t spp_pathmtu;
3412 * uint32_t spp_sackdelay;
3413 * uint32_t spp_flags;
3416 * spp_assoc_id - (one-to-many style socket) This is filled in the
3417 * application, and identifies the association for
3419 * spp_address - This specifies which address is of interest.
3420 * spp_hbinterval - This contains the value of the heartbeat interval,
3421 * in milliseconds. If a value of zero
3422 * is present in this field then no changes are to
3423 * be made to this parameter.
3424 * spp_pathmaxrxt - This contains the maximum number of
3425 * retransmissions before this address shall be
3426 * considered unreachable. If a value of zero
3427 * is present in this field then no changes are to
3428 * be made to this parameter.
3429 * spp_pathmtu - When Path MTU discovery is disabled the value
3430 * specified here will be the "fixed" path mtu.
3431 * Note that if the spp_address field is empty
3432 * then all associations on this address will
3433 * have this fixed path mtu set upon them.
3435 * spp_sackdelay - When delayed sack is enabled, this value specifies
3436 * the number of milliseconds that sacks will be delayed
3437 * for. This value will apply to all addresses of an
3438 * association if the spp_address field is empty. Note
3439 * also, that if delayed sack is enabled and this
3440 * value is set to 0, no change is made to the last
3441 * recorded delayed sack timer value.
3443 * spp_flags - These flags are used to control various features
3444 * on an association. The flag field may contain
3445 * zero or more of the following options.
3447 * SPP_HB_ENABLE - Enable heartbeats on the
3448 * specified address. Note that if the address
3449 * field is empty all addresses for the association
3450 * have heartbeats enabled upon them.
3452 * SPP_HB_DISABLE - Disable heartbeats on the
3453 * speicifed address. Note that if the address
3454 * field is empty all addresses for the association
3455 * will have their heartbeats disabled. Note also
3456 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3457 * mutually exclusive, only one of these two should
3458 * be specified. Enabling both fields will have
3459 * undetermined results.
3461 * SPP_HB_DEMAND - Request a user initiated heartbeat
3462 * to be made immediately.
3464 * SPP_PMTUD_ENABLE - This field will enable PMTU
3465 * discovery upon the specified address. Note that
3466 * if the address feild is empty then all addresses
3467 * on the association are effected.
3469 * SPP_PMTUD_DISABLE - This field will disable PMTU
3470 * discovery upon the specified address. Note that
3471 * if the address feild is empty then all addresses
3472 * on the association are effected. Not also that
3473 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3474 * exclusive. Enabling both will have undetermined
3477 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3478 * on delayed sack. The time specified in spp_sackdelay
3479 * is used to specify the sack delay for this address. Note
3480 * that if spp_address is empty then all addresses will
3481 * enable delayed sack and take on the sack delay
3482 * value specified in spp_sackdelay.
3483 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3484 * off delayed sack. If the spp_address field is blank then
3485 * delayed sack is disabled for the entire association. Note
3486 * also that this field is mutually exclusive to
3487 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3490 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3491 char __user *optval, int __user *optlen)
3493 struct sctp_paddrparams params;
3494 struct sctp_transport *trans = NULL;
3495 struct sctp_association *asoc = NULL;
3496 struct sctp_sock *sp = sctp_sk(sk);
3498 if (len != sizeof(struct sctp_paddrparams))
3501 if (copy_from_user(¶ms, optval, len))
3504 /* If an address other than INADDR_ANY is specified, and
3505 * no transport is found, then the request is invalid.
3507 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
3508 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
3509 params.spp_assoc_id);
3511 SCTP_DEBUG_PRINTK("Failed no transport\n");
3516 /* Get association, if assoc_id != 0 and the socket is a one
3517 * to many style socket, and an association was not found, then
3518 * the id was invalid.
3520 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3521 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3522 SCTP_DEBUG_PRINTK("Failed no association\n");
3527 /* Fetch transport values. */
3528 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3529 params.spp_pathmtu = trans->pathmtu;
3530 params.spp_pathmaxrxt = trans->pathmaxrxt;
3531 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3533 /*draft-11 doesn't say what to return in spp_flags*/
3534 params.spp_flags = trans->param_flags;
3536 /* Fetch association values. */
3537 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3538 params.spp_pathmtu = asoc->pathmtu;
3539 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3540 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3542 /*draft-11 doesn't say what to return in spp_flags*/
3543 params.spp_flags = asoc->param_flags;
3545 /* Fetch socket values. */
3546 params.spp_hbinterval = sp->hbinterval;
3547 params.spp_pathmtu = sp->pathmtu;
3548 params.spp_sackdelay = sp->sackdelay;
3549 params.spp_pathmaxrxt = sp->pathmaxrxt;
3551 /*draft-11 doesn't say what to return in spp_flags*/
3552 params.spp_flags = sp->param_flags;
3555 if (copy_to_user(optval, ¶ms, len))
3558 if (put_user(len, optlen))
3564 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3566 * This options will get or set the delayed ack timer. The time is set
3567 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3568 * endpoints default delayed ack timer value. If the assoc_id field is
3569 * non-zero, then the set or get effects the specified association.
3571 * struct sctp_assoc_value {
3572 * sctp_assoc_t assoc_id;
3573 * uint32_t assoc_value;
3576 * assoc_id - This parameter, indicates which association the
3577 * user is preforming an action upon. Note that if
3578 * this field's value is zero then the endpoints
3579 * default value is changed (effecting future
3580 * associations only).
3582 * assoc_value - This parameter contains the number of milliseconds
3583 * that the user is requesting the delayed ACK timer
3584 * be set to. Note that this value is defined in
3585 * the standard to be between 200 and 500 milliseconds.
3587 * Note: a value of zero will leave the value alone,
3588 * but disable SACK delay. A non-zero value will also
3589 * enable SACK delay.
3591 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3592 char __user *optval,
3595 struct sctp_assoc_value params;
3596 struct sctp_association *asoc = NULL;
3597 struct sctp_sock *sp = sctp_sk(sk);
3599 if (len != sizeof(struct sctp_assoc_value))
3602 if (copy_from_user(¶ms, optval, len))
3605 /* Get association, if assoc_id != 0 and the socket is a one
3606 * to many style socket, and an association was not found, then
3607 * the id was invalid.
3609 asoc = sctp_id2assoc(sk, params.assoc_id);
3610 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3614 /* Fetch association values. */
3615 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3616 params.assoc_value = jiffies_to_msecs(
3619 params.assoc_value = 0;
3621 /* Fetch socket values. */
3622 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3623 params.assoc_value = sp->sackdelay;
3625 params.assoc_value = 0;
3628 if (copy_to_user(optval, ¶ms, len))
3631 if (put_user(len, optlen))
3637 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3639 * Applications can specify protocol parameters for the default association
3640 * initialization. The option name argument to setsockopt() and getsockopt()
3643 * Setting initialization parameters is effective only on an unconnected
3644 * socket (for UDP-style sockets only future associations are effected
3645 * by the change). With TCP-style sockets, this option is inherited by
3646 * sockets derived from a listener socket.
3648 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3650 if (len != sizeof(struct sctp_initmsg))
3652 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3657 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3658 char __user *optval,
3662 struct sctp_association *asoc;
3663 struct list_head *pos;
3666 if (len != sizeof(sctp_assoc_t))
3669 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3672 /* For UDP-style sockets, id specifies the association to query. */
3673 asoc = sctp_id2assoc(sk, id);
3677 list_for_each(pos, &asoc->peer.transport_addr_list) {
3685 * Old API for getting list of peer addresses. Does not work for 32-bit
3686 * programs running on a 64-bit kernel
3688 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3689 char __user *optval,
3692 struct sctp_association *asoc;
3693 struct list_head *pos;
3695 struct sctp_getaddrs_old getaddrs;
3696 struct sctp_transport *from;
3698 union sctp_addr temp;
3699 struct sctp_sock *sp = sctp_sk(sk);
3702 if (len != sizeof(struct sctp_getaddrs_old))
3705 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3708 if (getaddrs.addr_num <= 0) return -EINVAL;
3710 /* For UDP-style sockets, id specifies the association to query. */
3711 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3715 to = (void __user *)getaddrs.addrs;
3716 list_for_each(pos, &asoc->peer.transport_addr_list) {
3717 from = list_entry(pos, struct sctp_transport, transports);
3718 memcpy(&temp, &from->ipaddr, sizeof(temp));
3719 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3720 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3721 temp.v4.sin_port = htons(temp.v4.sin_port);
3722 if (copy_to_user(to, &temp, addrlen))
3726 if (cnt >= getaddrs.addr_num) break;
3728 getaddrs.addr_num = cnt;
3729 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3735 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3736 char __user *optval, int __user *optlen)
3738 struct sctp_association *asoc;
3739 struct list_head *pos;
3741 struct sctp_getaddrs getaddrs;
3742 struct sctp_transport *from;
3744 union sctp_addr temp;
3745 struct sctp_sock *sp = sctp_sk(sk);
3750 if (len < sizeof(struct sctp_getaddrs))
3753 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3756 /* For UDP-style sockets, id specifies the association to query. */
3757 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3761 to = optval + offsetof(struct sctp_getaddrs,addrs);
3762 space_left = len - sizeof(struct sctp_getaddrs) -
3763 offsetof(struct sctp_getaddrs,addrs);
3765 list_for_each(pos, &asoc->peer.transport_addr_list) {
3766 from = list_entry(pos, struct sctp_transport, transports);
3767 memcpy(&temp, &from->ipaddr, sizeof(temp));
3768 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3769 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3770 if(space_left < addrlen)
3772 temp.v4.sin_port = htons(temp.v4.sin_port);
3773 if (copy_to_user(to, &temp, addrlen))
3777 space_left -= addrlen;
3780 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3782 bytes_copied = ((char __user *)to) - optval;
3783 if (put_user(bytes_copied, optlen))
3789 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3790 char __user *optval,
3794 struct sctp_bind_addr *bp;
3795 struct sctp_association *asoc;
3796 struct list_head *pos;
3797 struct sctp_sockaddr_entry *addr;
3798 rwlock_t *addr_lock;
3799 unsigned long flags;
3802 if (len != sizeof(sctp_assoc_t))
3805 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3809 * For UDP-style sockets, id specifies the association to query.
3810 * If the id field is set to the value '0' then the locally bound
3811 * addresses are returned without regard to any particular
3815 bp = &sctp_sk(sk)->ep->base.bind_addr;
3816 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3818 asoc = sctp_id2assoc(sk, id);
3821 bp = &asoc->base.bind_addr;
3822 addr_lock = &asoc->base.addr_lock;
3825 sctp_read_lock(addr_lock);
3827 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3828 * addresses from the global local address list.
3830 if (sctp_list_single_entry(&bp->address_list)) {
3831 addr = list_entry(bp->address_list.next,
3832 struct sctp_sockaddr_entry, list);
3833 if (sctp_is_any(&addr->a)) {
3834 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3835 list_for_each(pos, &sctp_local_addr_list) {
3836 addr = list_entry(pos,
3837 struct sctp_sockaddr_entry,
3839 if ((PF_INET == sk->sk_family) &&
3840 (AF_INET6 == addr->a.sa.sa_family))
3844 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3852 list_for_each(pos, &bp->address_list) {
3857 sctp_read_unlock(addr_lock);
3861 /* Helper function that copies local addresses to user and returns the number
3862 * of addresses copied.
3864 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3867 struct list_head *pos;
3868 struct sctp_sockaddr_entry *addr;
3869 unsigned long flags;
3870 union sctp_addr temp;
3874 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3875 list_for_each(pos, &sctp_local_addr_list) {
3876 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3877 if ((PF_INET == sk->sk_family) &&
3878 (AF_INET6 == addr->a.sa.sa_family))
3880 memcpy(&temp, &addr->a, sizeof(temp));
3881 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3883 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3884 temp.v4.sin_port = htons(port);
3885 if (copy_to_user(to, &temp, addrlen)) {
3886 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3892 if (cnt >= max_addrs) break;
3894 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3899 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3900 void __user **to, size_t space_left)
3902 struct list_head *pos;
3903 struct sctp_sockaddr_entry *addr;
3904 unsigned long flags;
3905 union sctp_addr temp;
3909 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3910 list_for_each(pos, &sctp_local_addr_list) {
3911 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3912 if ((PF_INET == sk->sk_family) &&
3913 (AF_INET6 == addr->a.sa.sa_family))
3915 memcpy(&temp, &addr->a, sizeof(temp));
3916 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3918 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3919 if(space_left<addrlen)
3921 temp.v4.sin_port = htons(port);
3922 if (copy_to_user(*to, &temp, addrlen)) {
3923 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3929 space_left -= addrlen;
3931 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3936 /* Old API for getting list of local addresses. Does not work for 32-bit
3937 * programs running on a 64-bit kernel
3939 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3940 char __user *optval, int __user *optlen)
3942 struct sctp_bind_addr *bp;
3943 struct sctp_association *asoc;
3944 struct list_head *pos;
3946 struct sctp_getaddrs_old getaddrs;
3947 struct sctp_sockaddr_entry *addr;
3949 union sctp_addr temp;
3950 struct sctp_sock *sp = sctp_sk(sk);
3952 rwlock_t *addr_lock;
3955 if (len != sizeof(struct sctp_getaddrs_old))
3958 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3961 if (getaddrs.addr_num <= 0) return -EINVAL;
3963 * For UDP-style sockets, id specifies the association to query.
3964 * If the id field is set to the value '0' then the locally bound
3965 * addresses are returned without regard to any particular
3968 if (0 == getaddrs.assoc_id) {
3969 bp = &sctp_sk(sk)->ep->base.bind_addr;
3970 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3972 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3975 bp = &asoc->base.bind_addr;
3976 addr_lock = &asoc->base.addr_lock;
3979 to = getaddrs.addrs;
3981 sctp_read_lock(addr_lock);
3983 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3984 * addresses from the global local address list.
3986 if (sctp_list_single_entry(&bp->address_list)) {
3987 addr = list_entry(bp->address_list.next,
3988 struct sctp_sockaddr_entry, list);
3989 if (sctp_is_any(&addr->a)) {
3990 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
4001 list_for_each(pos, &bp->address_list) {
4002 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4003 memcpy(&temp, &addr->a, sizeof(temp));
4004 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4005 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4006 temp.v4.sin_port = htons(temp.v4.sin_port);
4007 if (copy_to_user(to, &temp, addrlen)) {
4013 if (cnt >= getaddrs.addr_num) break;
4017 getaddrs.addr_num = cnt;
4018 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
4022 sctp_read_unlock(addr_lock);
4026 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4027 char __user *optval, int __user *optlen)
4029 struct sctp_bind_addr *bp;
4030 struct sctp_association *asoc;
4031 struct list_head *pos;
4033 struct sctp_getaddrs getaddrs;
4034 struct sctp_sockaddr_entry *addr;
4036 union sctp_addr temp;
4037 struct sctp_sock *sp = sctp_sk(sk);
4039 rwlock_t *addr_lock;
4044 if (len <= sizeof(struct sctp_getaddrs))
4047 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4051 * For UDP-style sockets, id specifies the association to query.
4052 * If the id field is set to the value '0' then the locally bound
4053 * addresses are returned without regard to any particular
4056 if (0 == getaddrs.assoc_id) {
4057 bp = &sctp_sk(sk)->ep->base.bind_addr;
4058 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4060 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4063 bp = &asoc->base.bind_addr;
4064 addr_lock = &asoc->base.addr_lock;
4067 to = optval + offsetof(struct sctp_getaddrs,addrs);
4068 space_left = len - sizeof(struct sctp_getaddrs) -
4069 offsetof(struct sctp_getaddrs,addrs);
4071 sctp_read_lock(addr_lock);
4073 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4074 * addresses from the global local address list.
4076 if (sctp_list_single_entry(&bp->address_list)) {
4077 addr = list_entry(bp->address_list.next,
4078 struct sctp_sockaddr_entry, list);
4079 if (sctp_is_any(&addr->a)) {
4080 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4090 list_for_each(pos, &bp->address_list) {
4091 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4092 memcpy(&temp, &addr->a, sizeof(temp));
4093 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4094 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4095 if(space_left < addrlen)
4096 return -ENOMEM; /*fixme: right error?*/
4097 temp.v4.sin_port = htons(temp.v4.sin_port);
4098 if (copy_to_user(to, &temp, addrlen)) {
4104 space_left -= addrlen;
4108 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4110 bytes_copied = ((char __user *)to) - optval;
4111 if (put_user(bytes_copied, optlen))
4115 sctp_read_unlock(addr_lock);
4119 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4121 * Requests that the local SCTP stack use the enclosed peer address as
4122 * the association primary. The enclosed address must be one of the
4123 * association peer's addresses.
4125 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4126 char __user *optval, int __user *optlen)
4128 struct sctp_prim prim;
4129 struct sctp_association *asoc;
4130 struct sctp_sock *sp = sctp_sk(sk);
4132 if (len != sizeof(struct sctp_prim))
4135 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4138 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4142 if (!asoc->peer.primary_path)
4145 asoc->peer.primary_path->ipaddr.v4.sin_port =
4146 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4147 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4148 sizeof(union sctp_addr));
4149 asoc->peer.primary_path->ipaddr.v4.sin_port =
4150 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4152 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4153 (union sctp_addr *)&prim.ssp_addr);
4155 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4162 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4164 * Requests that the local endpoint set the specified Adaption Layer
4165 * Indication parameter for all future INIT and INIT-ACK exchanges.
4167 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4168 char __user *optval, int __user *optlen)
4170 struct sctp_setadaption adaption;
4172 if (len != sizeof(struct sctp_setadaption))
4175 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4176 if (copy_to_user(optval, &adaption, len))
4184 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4186 * Applications that wish to use the sendto() system call may wish to
4187 * specify a default set of parameters that would normally be supplied
4188 * through the inclusion of ancillary data. This socket option allows
4189 * such an application to set the default sctp_sndrcvinfo structure.
4192 * The application that wishes to use this socket option simply passes
4193 * in to this call the sctp_sndrcvinfo structure defined in Section
4194 * 5.2.2) The input parameters accepted by this call include
4195 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4196 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4197 * to this call if the caller is using the UDP model.
4199 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4201 static int sctp_getsockopt_default_send_param(struct sock *sk,
4202 int len, char __user *optval,
4205 struct sctp_sndrcvinfo info;
4206 struct sctp_association *asoc;
4207 struct sctp_sock *sp = sctp_sk(sk);
4209 if (len != sizeof(struct sctp_sndrcvinfo))
4211 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4214 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4215 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4219 info.sinfo_stream = asoc->default_stream;
4220 info.sinfo_flags = asoc->default_flags;
4221 info.sinfo_ppid = asoc->default_ppid;
4222 info.sinfo_context = asoc->default_context;
4223 info.sinfo_timetolive = asoc->default_timetolive;
4225 info.sinfo_stream = sp->default_stream;
4226 info.sinfo_flags = sp->default_flags;
4227 info.sinfo_ppid = sp->default_ppid;
4228 info.sinfo_context = sp->default_context;
4229 info.sinfo_timetolive = sp->default_timetolive;
4232 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4240 * 7.1.5 SCTP_NODELAY
4242 * Turn on/off any Nagle-like algorithm. This means that packets are
4243 * generally sent as soon as possible and no unnecessary delays are
4244 * introduced, at the cost of more packets in the network. Expects an
4245 * integer boolean flag.
4248 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4249 char __user *optval, int __user *optlen)
4253 if (len < sizeof(int))
4257 val = (sctp_sk(sk)->nodelay == 1);
4258 if (put_user(len, optlen))
4260 if (copy_to_user(optval, &val, len))
4267 * 7.1.1 SCTP_RTOINFO
4269 * The protocol parameters used to initialize and bound retransmission
4270 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4271 * and modify these parameters.
4272 * All parameters are time values, in milliseconds. A value of 0, when
4273 * modifying the parameters, indicates that the current value should not
4277 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4278 char __user *optval,
4279 int __user *optlen) {
4280 struct sctp_rtoinfo rtoinfo;
4281 struct sctp_association *asoc;
4283 if (len != sizeof (struct sctp_rtoinfo))
4286 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4289 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4291 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4294 /* Values corresponding to the specific association. */
4296 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4297 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4298 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4300 /* Values corresponding to the endpoint. */
4301 struct sctp_sock *sp = sctp_sk(sk);
4303 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4304 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4305 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4308 if (put_user(len, optlen))
4311 if (copy_to_user(optval, &rtoinfo, len))
4319 * 7.1.2 SCTP_ASSOCINFO
4321 * This option is used to tune the the maximum retransmission attempts
4322 * of the association.
4323 * Returns an error if the new association retransmission value is
4324 * greater than the sum of the retransmission value of the peer.
4325 * See [SCTP] for more information.
4328 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4329 char __user *optval,
4333 struct sctp_assocparams assocparams;
4334 struct sctp_association *asoc;
4335 struct list_head *pos;
4338 if (len != sizeof (struct sctp_assocparams))
4341 if (copy_from_user(&assocparams, optval,
4342 sizeof (struct sctp_assocparams)))
4345 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4347 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4350 /* Values correspoinding to the specific association */
4352 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4353 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4354 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4355 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4357 (asoc->cookie_life.tv_usec
4360 list_for_each(pos, &asoc->peer.transport_addr_list) {
4364 assocparams.sasoc_number_peer_destinations = cnt;
4366 /* Values corresponding to the endpoint */
4367 struct sctp_sock *sp = sctp_sk(sk);
4369 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4370 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4371 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4372 assocparams.sasoc_cookie_life =
4373 sp->assocparams.sasoc_cookie_life;
4374 assocparams.sasoc_number_peer_destinations =
4376 sasoc_number_peer_destinations;
4379 if (put_user(len, optlen))
4382 if (copy_to_user(optval, &assocparams, len))
4389 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4391 * This socket option is a boolean flag which turns on or off mapped V4
4392 * addresses. If this option is turned on and the socket is type
4393 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4394 * If this option is turned off, then no mapping will be done of V4
4395 * addresses and a user will receive both PF_INET6 and PF_INET type
4396 * addresses on the socket.
4398 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4399 char __user *optval, int __user *optlen)
4402 struct sctp_sock *sp = sctp_sk(sk);
4404 if (len < sizeof(int))
4409 if (put_user(len, optlen))
4411 if (copy_to_user(optval, &val, len))
4418 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4420 * This socket option specifies the maximum size to put in any outgoing
4421 * SCTP chunk. If a message is larger than this size it will be
4422 * fragmented by SCTP into the specified size. Note that the underlying
4423 * SCTP implementation may fragment into smaller sized chunks when the
4424 * PMTU of the underlying association is smaller than the value set by
4427 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4428 char __user *optval, int __user *optlen)
4432 if (len < sizeof(int))
4437 val = sctp_sk(sk)->user_frag;
4438 if (put_user(len, optlen))
4440 if (copy_to_user(optval, &val, len))
4446 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4447 char __user *optval, int __user *optlen)
4452 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4455 /* I can hardly begin to describe how wrong this is. This is
4456 * so broken as to be worse than useless. The API draft
4457 * REALLY is NOT helpful here... I am not convinced that the
4458 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4459 * are at all well-founded.
4461 if (level != SOL_SCTP) {
4462 struct sctp_af *af = sctp_sk(sk)->pf->af;
4464 retval = af->getsockopt(sk, level, optname, optval, optlen);
4468 if (get_user(len, optlen))
4475 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4477 case SCTP_DISABLE_FRAGMENTS:
4478 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4482 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4484 case SCTP_AUTOCLOSE:
4485 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4487 case SCTP_SOCKOPT_PEELOFF:
4488 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4490 case SCTP_PEER_ADDR_PARAMS:
4491 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4494 case SCTP_DELAYED_ACK_TIME:
4495 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4499 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4501 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4502 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4505 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4506 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4509 case SCTP_GET_PEER_ADDRS_OLD:
4510 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4513 case SCTP_GET_LOCAL_ADDRS_OLD:
4514 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4517 case SCTP_GET_PEER_ADDRS:
4518 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4521 case SCTP_GET_LOCAL_ADDRS:
4522 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4525 case SCTP_DEFAULT_SEND_PARAM:
4526 retval = sctp_getsockopt_default_send_param(sk, len,
4529 case SCTP_PRIMARY_ADDR:
4530 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4533 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4536 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4538 case SCTP_ASSOCINFO:
4539 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4541 case SCTP_I_WANT_MAPPED_V4_ADDR:
4542 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4545 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4547 case SCTP_GET_PEER_ADDR_INFO:
4548 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4551 case SCTP_ADAPTION_LAYER:
4552 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4556 retval = -ENOPROTOOPT;
4560 sctp_release_sock(sk);
4564 static void sctp_hash(struct sock *sk)
4569 static void sctp_unhash(struct sock *sk)
4574 /* Check if port is acceptable. Possibly find first available port.
4576 * The port hash table (contained in the 'global' SCTP protocol storage
4577 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4578 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4579 * list (the list number is the port number hashed out, so as you
4580 * would expect from a hash function, all the ports in a given list have
4581 * such a number that hashes out to the same list number; you were
4582 * expecting that, right?); so each list has a set of ports, with a
4583 * link to the socket (struct sock) that uses it, the port number and
4584 * a fastreuse flag (FIXME: NPI ipg).
4586 static struct sctp_bind_bucket *sctp_bucket_create(
4587 struct sctp_bind_hashbucket *head, unsigned short snum);
4589 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4591 struct sctp_bind_hashbucket *head; /* hash list */
4592 struct sctp_bind_bucket *pp; /* hash list port iterator */
4593 unsigned short snum;
4596 /* NOTE: Remember to put this back to net order. */
4597 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4598 snum = addr->v4.sin_port;
4600 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4601 sctp_local_bh_disable();
4604 /* Search for an available port.
4606 * 'sctp_port_rover' was the last port assigned, so
4607 * we start to search from 'sctp_port_rover +
4608 * 1'. What we do is first check if port 'rover' is
4609 * already in the hash table; if not, we use that; if
4610 * it is, we try next.
4612 int low = sysctl_local_port_range[0];
4613 int high = sysctl_local_port_range[1];
4614 int remaining = (high - low) + 1;
4618 sctp_spin_lock(&sctp_port_alloc_lock);
4619 rover = sctp_port_rover;
4622 if ((rover < low) || (rover > high))
4624 index = sctp_phashfn(rover);
4625 head = &sctp_port_hashtable[index];
4626 sctp_spin_lock(&head->lock);
4627 for (pp = head->chain; pp; pp = pp->next)
4628 if (pp->port == rover)
4632 sctp_spin_unlock(&head->lock);
4633 } while (--remaining > 0);
4634 sctp_port_rover = rover;
4635 sctp_spin_unlock(&sctp_port_alloc_lock);
4637 /* Exhausted local port range during search? */
4642 /* OK, here is the one we will use. HEAD (the port
4643 * hash table list entry) is non-NULL and we hold it's
4648 /* We are given an specific port number; we verify
4649 * that it is not being used. If it is used, we will
4650 * exahust the search in the hash list corresponding
4651 * to the port number (snum) - we detect that with the
4652 * port iterator, pp being NULL.
4654 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4655 sctp_spin_lock(&head->lock);
4656 for (pp = head->chain; pp; pp = pp->next) {
4657 if (pp->port == snum)
4664 if (!hlist_empty(&pp->owner)) {
4665 /* We had a port hash table hit - there is an
4666 * available port (pp != NULL) and it is being
4667 * used by other socket (pp->owner not empty); that other
4668 * socket is going to be sk2.
4670 int reuse = sk->sk_reuse;
4672 struct hlist_node *node;
4674 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4675 if (pp->fastreuse && sk->sk_reuse)
4678 /* Run through the list of sockets bound to the port
4679 * (pp->port) [via the pointers bind_next and
4680 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4681 * we get the endpoint they describe and run through
4682 * the endpoint's list of IP (v4 or v6) addresses,
4683 * comparing each of the addresses with the address of
4684 * the socket sk. If we find a match, then that means
4685 * that this port/socket (sk) combination are already
4688 sk_for_each_bound(sk2, node, &pp->owner) {
4689 struct sctp_endpoint *ep2;
4690 ep2 = sctp_sk(sk2)->ep;
4692 if (reuse && sk2->sk_reuse)
4695 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4701 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4704 /* If there was a hash table miss, create a new port. */
4706 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4709 /* In either case (hit or miss), make sure fastreuse is 1 only
4710 * if sk->sk_reuse is too (that is, if the caller requested
4711 * SO_REUSEADDR on this socket -sk-).
4713 if (hlist_empty(&pp->owner))
4714 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4715 else if (pp->fastreuse && !sk->sk_reuse)
4718 /* We are set, so fill up all the data in the hash table
4719 * entry, tie the socket list information with the rest of the
4720 * sockets FIXME: Blurry, NPI (ipg).
4723 inet_sk(sk)->num = snum;
4724 if (!sctp_sk(sk)->bind_hash) {
4725 sk_add_bind_node(sk, &pp->owner);
4726 sctp_sk(sk)->bind_hash = pp;
4731 sctp_spin_unlock(&head->lock);
4734 sctp_local_bh_enable();
4735 addr->v4.sin_port = htons(addr->v4.sin_port);
4739 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4740 * port is requested.
4742 static int sctp_get_port(struct sock *sk, unsigned short snum)
4745 union sctp_addr addr;
4746 struct sctp_af *af = sctp_sk(sk)->pf->af;
4748 /* Set up a dummy address struct from the sk. */
4749 af->from_sk(&addr, sk);
4750 addr.v4.sin_port = htons(snum);
4752 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4753 ret = sctp_get_port_local(sk, &addr);
4755 return (ret ? 1 : 0);
4759 * 3.1.3 listen() - UDP Style Syntax
4761 * By default, new associations are not accepted for UDP style sockets.
4762 * An application uses listen() to mark a socket as being able to
4763 * accept new associations.
4765 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4767 struct sctp_sock *sp = sctp_sk(sk);
4768 struct sctp_endpoint *ep = sp->ep;
4770 /* Only UDP style sockets that are not peeled off are allowed to
4773 if (!sctp_style(sk, UDP))
4776 /* If backlog is zero, disable listening. */
4778 if (sctp_sstate(sk, CLOSED))
4781 sctp_unhash_endpoint(ep);
4782 sk->sk_state = SCTP_SS_CLOSED;
4785 /* Return if we are already listening. */
4786 if (sctp_sstate(sk, LISTENING))
4790 * If a bind() or sctp_bindx() is not called prior to a listen()
4791 * call that allows new associations to be accepted, the system
4792 * picks an ephemeral port and will choose an address set equivalent
4793 * to binding with a wildcard address.
4795 * This is not currently spelled out in the SCTP sockets
4796 * extensions draft, but follows the practice as seen in TCP
4799 if (!ep->base.bind_addr.port) {
4800 if (sctp_autobind(sk))
4803 sk->sk_state = SCTP_SS_LISTENING;
4804 sctp_hash_endpoint(ep);
4809 * 4.1.3 listen() - TCP Style Syntax
4811 * Applications uses listen() to ready the SCTP endpoint for accepting
4812 * inbound associations.
4814 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4816 struct sctp_sock *sp = sctp_sk(sk);
4817 struct sctp_endpoint *ep = sp->ep;
4819 /* If backlog is zero, disable listening. */
4821 if (sctp_sstate(sk, CLOSED))
4824 sctp_unhash_endpoint(ep);
4825 sk->sk_state = SCTP_SS_CLOSED;
4828 if (sctp_sstate(sk, LISTENING))
4832 * If a bind() or sctp_bindx() is not called prior to a listen()
4833 * call that allows new associations to be accepted, the system
4834 * picks an ephemeral port and will choose an address set equivalent
4835 * to binding with a wildcard address.
4837 * This is not currently spelled out in the SCTP sockets
4838 * extensions draft, but follows the practice as seen in TCP
4841 if (!ep->base.bind_addr.port) {
4842 if (sctp_autobind(sk))
4845 sk->sk_state = SCTP_SS_LISTENING;
4846 sk->sk_max_ack_backlog = backlog;
4847 sctp_hash_endpoint(ep);
4852 * Move a socket to LISTENING state.
4854 int sctp_inet_listen(struct socket *sock, int backlog)
4856 struct sock *sk = sock->sk;
4857 struct crypto_tfm *tfm=NULL;
4860 if (unlikely(backlog < 0))
4865 if (sock->state != SS_UNCONNECTED)
4868 /* Allocate HMAC for generating cookie. */
4869 if (sctp_hmac_alg) {
4870 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4877 switch (sock->type) {
4878 case SOCK_SEQPACKET:
4879 err = sctp_seqpacket_listen(sk, backlog);
4882 err = sctp_stream_listen(sk, backlog);
4890 /* Store away the transform reference. */
4891 sctp_sk(sk)->hmac = tfm;
4893 sctp_release_sock(sk);
4896 sctp_crypto_free_tfm(tfm);
4901 * This function is done by modeling the current datagram_poll() and the
4902 * tcp_poll(). Note that, based on these implementations, we don't
4903 * lock the socket in this function, even though it seems that,
4904 * ideally, locking or some other mechanisms can be used to ensure
4905 * the integrity of the counters (sndbuf and wmem_alloc) used
4906 * in this place. We assume that we don't need locks either until proven
4909 * Another thing to note is that we include the Async I/O support
4910 * here, again, by modeling the current TCP/UDP code. We don't have
4911 * a good way to test with it yet.
4913 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4915 struct sock *sk = sock->sk;
4916 struct sctp_sock *sp = sctp_sk(sk);
4919 poll_wait(file, sk->sk_sleep, wait);
4921 /* A TCP-style listening socket becomes readable when the accept queue
4924 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4925 return (!list_empty(&sp->ep->asocs)) ?
4926 (POLLIN | POLLRDNORM) : 0;
4930 /* Is there any exceptional events? */
4931 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4933 if (sk->sk_shutdown == SHUTDOWN_MASK)
4936 /* Is it readable? Reconsider this code with TCP-style support. */
4937 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4938 (sk->sk_shutdown & RCV_SHUTDOWN))
4939 mask |= POLLIN | POLLRDNORM;
4941 /* The association is either gone or not ready. */
4942 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4945 /* Is it writable? */
4946 if (sctp_writeable(sk)) {
4947 mask |= POLLOUT | POLLWRNORM;
4949 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4951 * Since the socket is not locked, the buffer
4952 * might be made available after the writeable check and
4953 * before the bit is set. This could cause a lost I/O
4954 * signal. tcp_poll() has a race breaker for this race
4955 * condition. Based on their implementation, we put
4956 * in the following code to cover it as well.
4958 if (sctp_writeable(sk))
4959 mask |= POLLOUT | POLLWRNORM;
4964 /********************************************************************
4965 * 2nd Level Abstractions
4966 ********************************************************************/
4968 static struct sctp_bind_bucket *sctp_bucket_create(
4969 struct sctp_bind_hashbucket *head, unsigned short snum)
4971 struct sctp_bind_bucket *pp;
4973 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4974 SCTP_DBG_OBJCNT_INC(bind_bucket);
4978 INIT_HLIST_HEAD(&pp->owner);
4979 if ((pp->next = head->chain) != NULL)
4980 pp->next->pprev = &pp->next;
4982 pp->pprev = &head->chain;
4987 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4988 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4990 if (hlist_empty(&pp->owner)) {
4992 pp->next->pprev = pp->pprev;
4993 *(pp->pprev) = pp->next;
4994 kmem_cache_free(sctp_bucket_cachep, pp);
4995 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4999 /* Release this socket's reference to a local port. */
5000 static inline void __sctp_put_port(struct sock *sk)
5002 struct sctp_bind_hashbucket *head =
5003 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5004 struct sctp_bind_bucket *pp;
5006 sctp_spin_lock(&head->lock);
5007 pp = sctp_sk(sk)->bind_hash;
5008 __sk_del_bind_node(sk);
5009 sctp_sk(sk)->bind_hash = NULL;
5010 inet_sk(sk)->num = 0;
5011 sctp_bucket_destroy(pp);
5012 sctp_spin_unlock(&head->lock);
5015 void sctp_put_port(struct sock *sk)
5017 sctp_local_bh_disable();
5018 __sctp_put_port(sk);
5019 sctp_local_bh_enable();
5023 * The system picks an ephemeral port and choose an address set equivalent
5024 * to binding with a wildcard address.
5025 * One of those addresses will be the primary address for the association.
5026 * This automatically enables the multihoming capability of SCTP.
5028 static int sctp_autobind(struct sock *sk)
5030 union sctp_addr autoaddr;
5032 unsigned short port;
5034 /* Initialize a local sockaddr structure to INADDR_ANY. */
5035 af = sctp_sk(sk)->pf->af;
5037 port = htons(inet_sk(sk)->num);
5038 af->inaddr_any(&autoaddr, port);
5040 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5043 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5046 * 4.2 The cmsghdr Structure *
5048 * When ancillary data is sent or received, any number of ancillary data
5049 * objects can be specified by the msg_control and msg_controllen members of
5050 * the msghdr structure, because each object is preceded by
5051 * a cmsghdr structure defining the object's length (the cmsg_len member).
5052 * Historically Berkeley-derived implementations have passed only one object
5053 * at a time, but this API allows multiple objects to be
5054 * passed in a single call to sendmsg() or recvmsg(). The following example
5055 * shows two ancillary data objects in a control buffer.
5057 * |<--------------------------- msg_controllen -------------------------->|
5060 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5062 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5065 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5067 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5070 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5071 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5073 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5075 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5082 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5083 sctp_cmsgs_t *cmsgs)
5085 struct cmsghdr *cmsg;
5087 for (cmsg = CMSG_FIRSTHDR(msg);
5089 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5090 if (!CMSG_OK(msg, cmsg))
5093 /* Should we parse this header or ignore? */
5094 if (cmsg->cmsg_level != IPPROTO_SCTP)
5097 /* Strictly check lengths following example in SCM code. */
5098 switch (cmsg->cmsg_type) {
5100 /* SCTP Socket API Extension
5101 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5103 * This cmsghdr structure provides information for
5104 * initializing new SCTP associations with sendmsg().
5105 * The SCTP_INITMSG socket option uses this same data
5106 * structure. This structure is not used for
5109 * cmsg_level cmsg_type cmsg_data[]
5110 * ------------ ------------ ----------------------
5111 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5113 if (cmsg->cmsg_len !=
5114 CMSG_LEN(sizeof(struct sctp_initmsg)))
5116 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5120 /* SCTP Socket API Extension
5121 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5123 * This cmsghdr structure specifies SCTP options for
5124 * sendmsg() and describes SCTP header information
5125 * about a received message through recvmsg().
5127 * cmsg_level cmsg_type cmsg_data[]
5128 * ------------ ------------ ----------------------
5129 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5131 if (cmsg->cmsg_len !=
5132 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5136 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5138 /* Minimally, validate the sinfo_flags. */
5139 if (cmsgs->info->sinfo_flags &
5140 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5141 SCTP_ABORT | SCTP_EOF))
5153 * Wait for a packet..
5154 * Note: This function is the same function as in core/datagram.c
5155 * with a few modifications to make lksctp work.
5157 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5162 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5164 /* Socket errors? */
5165 error = sock_error(sk);
5169 if (!skb_queue_empty(&sk->sk_receive_queue))
5172 /* Socket shut down? */
5173 if (sk->sk_shutdown & RCV_SHUTDOWN)
5176 /* Sequenced packets can come disconnected. If so we report the
5181 /* Is there a good reason to think that we may receive some data? */
5182 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5185 /* Handle signals. */
5186 if (signal_pending(current))
5189 /* Let another process have a go. Since we are going to sleep
5190 * anyway. Note: This may cause odd behaviors if the message
5191 * does not fit in the user's buffer, but this seems to be the
5192 * only way to honor MSG_DONTWAIT realistically.
5194 sctp_release_sock(sk);
5195 *timeo_p = schedule_timeout(*timeo_p);
5199 finish_wait(sk->sk_sleep, &wait);
5203 error = sock_intr_errno(*timeo_p);
5206 finish_wait(sk->sk_sleep, &wait);
5211 /* Receive a datagram.
5212 * Note: This is pretty much the same routine as in core/datagram.c
5213 * with a few changes to make lksctp work.
5215 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5216 int noblock, int *err)
5219 struct sk_buff *skb;
5222 timeo = sock_rcvtimeo(sk, noblock);
5224 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5225 timeo, MAX_SCHEDULE_TIMEOUT);
5228 /* Again only user level code calls this function,
5229 * so nothing interrupt level
5230 * will suddenly eat the receive_queue.
5232 * Look at current nfs client by the way...
5233 * However, this function was corrent in any case. 8)
5235 if (flags & MSG_PEEK) {
5236 spin_lock_bh(&sk->sk_receive_queue.lock);
5237 skb = skb_peek(&sk->sk_receive_queue);
5239 atomic_inc(&skb->users);
5240 spin_unlock_bh(&sk->sk_receive_queue.lock);
5242 skb = skb_dequeue(&sk->sk_receive_queue);
5248 /* Caller is allowed not to check sk->sk_err before calling. */
5249 error = sock_error(sk);
5253 if (sk->sk_shutdown & RCV_SHUTDOWN)
5256 /* User doesn't want to wait. */
5260 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5269 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5270 static void __sctp_write_space(struct sctp_association *asoc)
5272 struct sock *sk = asoc->base.sk;
5273 struct socket *sock = sk->sk_socket;
5275 if ((sctp_wspace(asoc) > 0) && sock) {
5276 if (waitqueue_active(&asoc->wait))
5277 wake_up_interruptible(&asoc->wait);
5279 if (sctp_writeable(sk)) {
5280 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5281 wake_up_interruptible(sk->sk_sleep);
5283 /* Note that we try to include the Async I/O support
5284 * here by modeling from the current TCP/UDP code.
5285 * We have not tested with it yet.
5287 if (sock->fasync_list &&
5288 !(sk->sk_shutdown & SEND_SHUTDOWN))
5289 sock_wake_async(sock, 2, POLL_OUT);
5294 /* Do accounting for the sndbuf space.
5295 * Decrement the used sndbuf space of the corresponding association by the
5296 * data size which was just transmitted(freed).
5298 static void sctp_wfree(struct sk_buff *skb)
5300 struct sctp_association *asoc;
5301 struct sctp_chunk *chunk;
5304 /* Get the saved chunk pointer. */
5305 chunk = *((struct sctp_chunk **)(skb->cb));
5308 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5309 sizeof(struct sk_buff) +
5310 sizeof(struct sctp_chunk);
5312 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5315 __sctp_write_space(asoc);
5317 sctp_association_put(asoc);
5320 /* Helper function to wait for space in the sndbuf. */
5321 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5324 struct sock *sk = asoc->base.sk;
5326 long current_timeo = *timeo_p;
5329 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5330 asoc, (long)(*timeo_p), msg_len);
5332 /* Increment the association's refcnt. */
5333 sctp_association_hold(asoc);
5335 /* Wait on the association specific sndbuf space. */
5337 prepare_to_wait_exclusive(&asoc->wait, &wait,
5338 TASK_INTERRUPTIBLE);
5341 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5344 if (signal_pending(current))
5345 goto do_interrupted;
5346 if (msg_len <= sctp_wspace(asoc))
5349 /* Let another process have a go. Since we are going
5352 sctp_release_sock(sk);
5353 current_timeo = schedule_timeout(current_timeo);
5356 *timeo_p = current_timeo;
5360 finish_wait(&asoc->wait, &wait);
5362 /* Release the association's refcnt. */
5363 sctp_association_put(asoc);
5372 err = sock_intr_errno(*timeo_p);
5380 /* If socket sndbuf has changed, wake up all per association waiters. */
5381 void sctp_write_space(struct sock *sk)
5383 struct sctp_association *asoc;
5384 struct list_head *pos;
5386 /* Wake up the tasks in each wait queue. */
5387 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5388 asoc = list_entry(pos, struct sctp_association, asocs);
5389 __sctp_write_space(asoc);
5393 /* Is there any sndbuf space available on the socket?
5395 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5396 * associations on the same socket. For a UDP-style socket with
5397 * multiple associations, it is possible for it to be "unwriteable"
5398 * prematurely. I assume that this is acceptable because
5399 * a premature "unwriteable" is better than an accidental "writeable" which
5400 * would cause an unwanted block under certain circumstances. For the 1-1
5401 * UDP-style sockets or TCP-style sockets, this code should work.
5404 static int sctp_writeable(struct sock *sk)
5408 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5414 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5415 * returns immediately with EINPROGRESS.
5417 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5419 struct sock *sk = asoc->base.sk;
5421 long current_timeo = *timeo_p;
5424 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5427 /* Increment the association's refcnt. */
5428 sctp_association_hold(asoc);
5431 prepare_to_wait_exclusive(&asoc->wait, &wait,
5432 TASK_INTERRUPTIBLE);
5435 if (sk->sk_shutdown & RCV_SHUTDOWN)
5437 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5440 if (signal_pending(current))
5441 goto do_interrupted;
5443 if (sctp_state(asoc, ESTABLISHED))
5446 /* Let another process have a go. Since we are going
5449 sctp_release_sock(sk);
5450 current_timeo = schedule_timeout(current_timeo);
5453 *timeo_p = current_timeo;
5457 finish_wait(&asoc->wait, &wait);
5459 /* Release the association's refcnt. */
5460 sctp_association_put(asoc);
5465 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5468 err = -ECONNREFUSED;
5472 err = sock_intr_errno(*timeo_p);
5480 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5482 struct sctp_endpoint *ep;
5486 ep = sctp_sk(sk)->ep;
5490 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5491 TASK_INTERRUPTIBLE);
5493 if (list_empty(&ep->asocs)) {
5494 sctp_release_sock(sk);
5495 timeo = schedule_timeout(timeo);
5500 if (!sctp_sstate(sk, LISTENING))
5504 if (!list_empty(&ep->asocs))
5507 err = sock_intr_errno(timeo);
5508 if (signal_pending(current))
5516 finish_wait(sk->sk_sleep, &wait);
5521 void sctp_wait_for_close(struct sock *sk, long timeout)
5526 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5527 if (list_empty(&sctp_sk(sk)->ep->asocs))
5529 sctp_release_sock(sk);
5530 timeout = schedule_timeout(timeout);
5532 } while (!signal_pending(current) && timeout);
5534 finish_wait(sk->sk_sleep, &wait);
5537 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5538 * and its messages to the newsk.
5540 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5541 struct sctp_association *assoc,
5542 sctp_socket_type_t type)
5544 struct sctp_sock *oldsp = sctp_sk(oldsk);
5545 struct sctp_sock *newsp = sctp_sk(newsk);
5546 struct sctp_bind_bucket *pp; /* hash list port iterator */
5547 struct sctp_endpoint *newep = newsp->ep;
5548 struct sk_buff *skb, *tmp;
5549 struct sctp_ulpevent *event;
5552 /* Migrate socket buffer sizes and all the socket level options to the
5555 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5556 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5557 /* Brute force copy old sctp opt. */
5558 inet_sk_copy_descendant(newsk, oldsk);
5560 /* Restore the ep value that was overwritten with the above structure
5566 /* Hook this new socket in to the bind_hash list. */
5567 pp = sctp_sk(oldsk)->bind_hash;
5568 sk_add_bind_node(newsk, &pp->owner);
5569 sctp_sk(newsk)->bind_hash = pp;
5570 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5572 /* Copy the bind_addr list from the original endpoint to the new
5573 * endpoint so that we can handle restarts properly
5575 if (assoc->peer.ipv4_address)
5576 flags |= SCTP_ADDR4_PEERSUPP;
5577 if (assoc->peer.ipv6_address)
5578 flags |= SCTP_ADDR6_PEERSUPP;
5579 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5580 &oldsp->ep->base.bind_addr,
5581 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5583 /* Move any messages in the old socket's receive queue that are for the
5584 * peeled off association to the new socket's receive queue.
5586 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5587 event = sctp_skb2event(skb);
5588 if (event->asoc == assoc) {
5590 __skb_unlink(skb, &oldsk->sk_receive_queue);
5591 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5592 skb_set_owner_r(skb, newsk);
5596 /* Clean up any messages pending delivery due to partial
5597 * delivery. Three cases:
5598 * 1) No partial deliver; no work.
5599 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5600 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5602 skb_queue_head_init(&newsp->pd_lobby);
5603 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5605 if (sctp_sk(oldsk)->pd_mode) {
5606 struct sk_buff_head *queue;
5608 /* Decide which queue to move pd_lobby skbs to. */
5609 if (assoc->ulpq.pd_mode) {
5610 queue = &newsp->pd_lobby;
5612 queue = &newsk->sk_receive_queue;
5614 /* Walk through the pd_lobby, looking for skbs that
5615 * need moved to the new socket.
5617 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5618 event = sctp_skb2event(skb);
5619 if (event->asoc == assoc) {
5621 __skb_unlink(skb, &oldsp->pd_lobby);
5622 __skb_queue_tail(queue, skb);
5623 skb_set_owner_r(skb, newsk);
5627 /* Clear up any skbs waiting for the partial
5628 * delivery to finish.
5630 if (assoc->ulpq.pd_mode)
5631 sctp_clear_pd(oldsk);
5635 /* Set the type of socket to indicate that it is peeled off from the
5636 * original UDP-style socket or created with the accept() call on a
5637 * TCP-style socket..
5641 spin_lock_bh(&oldsk->sk_lock.slock);
5642 /* Migrate the backlog from oldsk to newsk. */
5643 sctp_backlog_migrate(assoc, oldsk, newsk);
5644 /* Migrate the association to the new socket. */
5645 sctp_assoc_migrate(assoc, newsk);
5646 spin_unlock_bh(&oldsk->sk_lock.slock);
5648 /* If the association on the newsk is already closed before accept()
5649 * is called, set RCV_SHUTDOWN flag.
5651 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5652 newsk->sk_shutdown |= RCV_SHUTDOWN;
5654 newsk->sk_state = SCTP_SS_ESTABLISHED;
5657 /* This proto struct describes the ULP interface for SCTP. */
5658 struct proto sctp_prot = {
5660 .owner = THIS_MODULE,
5661 .close = sctp_close,
5662 .connect = sctp_connect,
5663 .disconnect = sctp_disconnect,
5664 .accept = sctp_accept,
5665 .ioctl = sctp_ioctl,
5666 .init = sctp_init_sock,
5667 .destroy = sctp_destroy_sock,
5668 .shutdown = sctp_shutdown,
5669 .setsockopt = sctp_setsockopt,
5670 .getsockopt = sctp_getsockopt,
5671 .sendmsg = sctp_sendmsg,
5672 .recvmsg = sctp_recvmsg,
5674 .backlog_rcv = sctp_backlog_rcv,
5676 .unhash = sctp_unhash,
5677 .get_port = sctp_get_port,
5678 .obj_size = sizeof(struct sctp_sock),
5681 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5682 struct proto sctpv6_prot = {
5684 .owner = THIS_MODULE,
5685 .close = sctp_close,
5686 .connect = sctp_connect,
5687 .disconnect = sctp_disconnect,
5688 .accept = sctp_accept,
5689 .ioctl = sctp_ioctl,
5690 .init = sctp_init_sock,
5691 .destroy = sctp_destroy_sock,
5692 .shutdown = sctp_shutdown,
5693 .setsockopt = sctp_setsockopt,
5694 .getsockopt = sctp_getsockopt,
5695 .sendmsg = sctp_sendmsg,
5696 .recvmsg = sctp_recvmsg,
5698 .backlog_rcv = sctp_backlog_rcv,
5700 .unhash = sctp_unhash,
5701 .get_port = sctp_get_port,
5702 .obj_size = sizeof(struct sctp6_sock),
5704 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */