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)))
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) &&
1247 sctp_primitive_ABORT(asoc, NULL);
1249 sctp_primitive_SHUTDOWN(asoc, NULL);
1251 sctp_primitive_SHUTDOWN(asoc, NULL);
1254 /* Clean up any skbs sitting on the receive queue. */
1255 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1256 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1258 /* On a TCP-style socket, block for at most linger_time if set. */
1259 if (sctp_style(sk, TCP) && timeout)
1260 sctp_wait_for_close(sk, timeout);
1262 /* This will run the backlog queue. */
1263 sctp_release_sock(sk);
1265 /* Supposedly, no process has access to the socket, but
1266 * the net layers still may.
1268 sctp_local_bh_disable();
1269 sctp_bh_lock_sock(sk);
1271 /* Hold the sock, since sk_common_release() will put sock_put()
1272 * and we have just a little more cleanup.
1275 sk_common_release(sk);
1277 sctp_bh_unlock_sock(sk);
1278 sctp_local_bh_enable();
1282 SCTP_DBG_OBJCNT_DEC(sock);
1285 /* Handle EPIPE error. */
1286 static int sctp_error(struct sock *sk, int flags, int err)
1289 err = sock_error(sk) ? : -EPIPE;
1290 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1291 send_sig(SIGPIPE, current, 0);
1295 /* API 3.1.3 sendmsg() - UDP Style Syntax
1297 * An application uses sendmsg() and recvmsg() calls to transmit data to
1298 * and receive data from its peer.
1300 * ssize_t sendmsg(int socket, const struct msghdr *message,
1303 * socket - the socket descriptor of the endpoint.
1304 * message - pointer to the msghdr structure which contains a single
1305 * user message and possibly some ancillary data.
1307 * See Section 5 for complete description of the data
1310 * flags - flags sent or received with the user message, see Section
1311 * 5 for complete description of the flags.
1313 * Note: This function could use a rewrite especially when explicit
1314 * connect support comes in.
1316 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1318 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1320 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1321 struct msghdr *msg, size_t msg_len)
1323 struct sctp_sock *sp;
1324 struct sctp_endpoint *ep;
1325 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1326 struct sctp_transport *transport, *chunk_tp;
1327 struct sctp_chunk *chunk;
1329 struct sockaddr *msg_name = NULL;
1330 struct sctp_sndrcvinfo default_sinfo = { 0 };
1331 struct sctp_sndrcvinfo *sinfo;
1332 struct sctp_initmsg *sinit;
1333 sctp_assoc_t associd = 0;
1334 sctp_cmsgs_t cmsgs = { NULL };
1338 __u16 sinfo_flags = 0;
1339 struct sctp_datamsg *datamsg;
1340 struct list_head *pos;
1341 int msg_flags = msg->msg_flags;
1343 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1350 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1352 /* We cannot send a message over a TCP-style listening socket. */
1353 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1358 /* Parse out the SCTP CMSGs. */
1359 err = sctp_msghdr_parse(msg, &cmsgs);
1362 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1366 /* Fetch the destination address for this packet. This
1367 * address only selects the association--it is not necessarily
1368 * the address we will send to.
1369 * For a peeled-off socket, msg_name is ignored.
1371 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1372 int msg_namelen = msg->msg_namelen;
1374 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1379 if (msg_namelen > sizeof(to))
1380 msg_namelen = sizeof(to);
1381 memcpy(&to, msg->msg_name, msg_namelen);
1382 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1384 to.v4.sin_addr.s_addr, to.v4.sin_port);
1386 to.v4.sin_port = ntohs(to.v4.sin_port);
1387 msg_name = msg->msg_name;
1393 /* Did the user specify SNDRCVINFO? */
1395 sinfo_flags = sinfo->sinfo_flags;
1396 associd = sinfo->sinfo_assoc_id;
1399 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1400 msg_len, sinfo_flags);
1402 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1403 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1408 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1409 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1410 * If SCTP_ABORT is set, the message length could be non zero with
1411 * the msg_iov set to the user abort reason.
1413 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1414 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1419 /* If SCTP_ADDR_OVER is set, there must be an address
1420 * specified in msg_name.
1422 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1429 SCTP_DEBUG_PRINTK("About to look up association.\n");
1433 /* If a msg_name has been specified, assume this is to be used. */
1435 /* Look for a matching association on the endpoint. */
1436 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1438 /* If we could not find a matching association on the
1439 * endpoint, make sure that it is not a TCP-style
1440 * socket that already has an association or there is
1441 * no peeled-off association on another socket.
1443 if ((sctp_style(sk, TCP) &&
1444 sctp_sstate(sk, ESTABLISHED)) ||
1445 sctp_endpoint_is_peeled_off(ep, &to)) {
1446 err = -EADDRNOTAVAIL;
1451 asoc = sctp_id2assoc(sk, associd);
1459 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1461 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1462 * socket that has an association in CLOSED state. This can
1463 * happen when an accepted socket has an association that is
1466 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1471 if (sinfo_flags & SCTP_EOF) {
1472 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1474 sctp_primitive_SHUTDOWN(asoc, NULL);
1478 if (sinfo_flags & SCTP_ABORT) {
1479 struct sctp_chunk *chunk;
1481 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1487 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1488 sctp_primitive_ABORT(asoc, chunk);
1494 /* Do we need to create the association? */
1496 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1498 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1503 /* Check for invalid stream against the stream counts,
1504 * either the default or the user specified stream counts.
1507 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1508 /* Check against the defaults. */
1509 if (sinfo->sinfo_stream >=
1510 sp->initmsg.sinit_num_ostreams) {
1515 /* Check against the requested. */
1516 if (sinfo->sinfo_stream >=
1517 sinit->sinit_num_ostreams) {
1525 * API 3.1.2 bind() - UDP Style Syntax
1526 * If a bind() or sctp_bindx() is not called prior to a
1527 * sendmsg() call that initiates a new association, the
1528 * system picks an ephemeral port and will choose an address
1529 * set equivalent to binding with a wildcard address.
1531 if (!ep->base.bind_addr.port) {
1532 if (sctp_autobind(sk)) {
1538 * If an unprivileged user inherits a one-to-many
1539 * style socket with open associations on a privileged
1540 * port, it MAY be permitted to accept new associations,
1541 * but it SHOULD NOT be permitted to open new
1544 if (ep->base.bind_addr.port < PROT_SOCK &&
1545 !capable(CAP_NET_BIND_SERVICE)) {
1551 scope = sctp_scope(&to);
1552 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1559 /* If the SCTP_INIT ancillary data is specified, set all
1560 * the association init values accordingly.
1563 if (sinit->sinit_num_ostreams) {
1564 asoc->c.sinit_num_ostreams =
1565 sinit->sinit_num_ostreams;
1567 if (sinit->sinit_max_instreams) {
1568 asoc->c.sinit_max_instreams =
1569 sinit->sinit_max_instreams;
1571 if (sinit->sinit_max_attempts) {
1572 asoc->max_init_attempts
1573 = sinit->sinit_max_attempts;
1575 if (sinit->sinit_max_init_timeo) {
1576 asoc->max_init_timeo =
1577 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1581 /* Prime the peer's transport structures. */
1582 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1587 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1594 /* ASSERT: we have a valid association at this point. */
1595 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1598 /* If the user didn't specify SNDRCVINFO, make up one with
1601 default_sinfo.sinfo_stream = asoc->default_stream;
1602 default_sinfo.sinfo_flags = asoc->default_flags;
1603 default_sinfo.sinfo_ppid = asoc->default_ppid;
1604 default_sinfo.sinfo_context = asoc->default_context;
1605 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1606 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1607 sinfo = &default_sinfo;
1610 /* API 7.1.7, the sndbuf size per association bounds the
1611 * maximum size of data that can be sent in a single send call.
1613 if (msg_len > sk->sk_sndbuf) {
1618 /* If fragmentation is disabled and the message length exceeds the
1619 * association fragmentation point, return EMSGSIZE. The I-D
1620 * does not specify what this error is, but this looks like
1623 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1629 /* Check for invalid stream. */
1630 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1636 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1637 if (!sctp_wspace(asoc)) {
1638 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1643 /* If an address is passed with the sendto/sendmsg call, it is used
1644 * to override the primary destination address in the TCP model, or
1645 * when SCTP_ADDR_OVER flag is set in the UDP model.
1647 if ((sctp_style(sk, TCP) && msg_name) ||
1648 (sinfo_flags & SCTP_ADDR_OVER)) {
1649 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1657 /* Auto-connect, if we aren't connected already. */
1658 if (sctp_state(asoc, CLOSED)) {
1659 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1662 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1665 /* Break the message into multiple chunks of maximum size. */
1666 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1672 /* Now send the (possibly) fragmented message. */
1673 list_for_each(pos, &datamsg->chunks) {
1674 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1675 sctp_datamsg_track(chunk);
1677 /* Do accounting for the write space. */
1678 sctp_set_owner_w(chunk);
1680 chunk->transport = chunk_tp;
1682 /* Send it to the lower layers. Note: all chunks
1683 * must either fail or succeed. The lower layer
1684 * works that way today. Keep it that way or this
1687 err = sctp_primitive_SEND(asoc, chunk);
1688 /* Did the lower layer accept the chunk? */
1690 sctp_chunk_free(chunk);
1691 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1694 sctp_datamsg_free(datamsg);
1700 /* If we are already past ASSOCIATE, the lower
1701 * layers are responsible for association cleanup.
1707 sctp_association_free(asoc);
1709 sctp_release_sock(sk);
1712 return sctp_error(sk, msg_flags, err);
1719 err = sock_error(sk);
1729 /* This is an extended version of skb_pull() that removes the data from the
1730 * start of a skb even when data is spread across the list of skb's in the
1731 * frag_list. len specifies the total amount of data that needs to be removed.
1732 * when 'len' bytes could be removed from the skb, it returns 0.
1733 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1734 * could not be removed.
1736 static int sctp_skb_pull(struct sk_buff *skb, int len)
1738 struct sk_buff *list;
1739 int skb_len = skb_headlen(skb);
1742 if (len <= skb_len) {
1743 __skb_pull(skb, len);
1747 __skb_pull(skb, skb_len);
1749 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1750 rlen = sctp_skb_pull(list, len);
1751 skb->len -= (len-rlen);
1752 skb->data_len -= (len-rlen);
1763 /* API 3.1.3 recvmsg() - UDP Style Syntax
1765 * ssize_t recvmsg(int socket, struct msghdr *message,
1768 * socket - the socket descriptor of the endpoint.
1769 * message - pointer to the msghdr structure which contains a single
1770 * user message and possibly some ancillary data.
1772 * See Section 5 for complete description of the data
1775 * flags - flags sent or received with the user message, see Section
1776 * 5 for complete description of the flags.
1778 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1780 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1781 struct msghdr *msg, size_t len, int noblock,
1782 int flags, int *addr_len)
1784 struct sctp_ulpevent *event = NULL;
1785 struct sctp_sock *sp = sctp_sk(sk);
1786 struct sk_buff *skb;
1791 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1792 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1793 "len", len, "knoblauch", noblock,
1794 "flags", flags, "addr_len", addr_len);
1798 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1803 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1807 /* Get the total length of the skb including any skb's in the
1816 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1818 event = sctp_skb2event(skb);
1823 sock_recv_timestamp(msg, sk, skb);
1824 if (sctp_ulpevent_is_notification(event)) {
1825 msg->msg_flags |= MSG_NOTIFICATION;
1826 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1828 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1831 /* Check if we allow SCTP_SNDRCVINFO. */
1832 if (sp->subscribe.sctp_data_io_event)
1833 sctp_ulpevent_read_sndrcvinfo(event, msg);
1835 /* FIXME: we should be calling IP/IPv6 layers. */
1836 if (sk->sk_protinfo.af_inet.cmsg_flags)
1837 ip_cmsg_recv(msg, skb);
1842 /* If skb's length exceeds the user's buffer, update the skb and
1843 * push it back to the receive_queue so that the next call to
1844 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1846 if (skb_len > copied) {
1847 msg->msg_flags &= ~MSG_EOR;
1848 if (flags & MSG_PEEK)
1850 sctp_skb_pull(skb, copied);
1851 skb_queue_head(&sk->sk_receive_queue, skb);
1853 /* When only partial message is copied to the user, increase
1854 * rwnd by that amount. If all the data in the skb is read,
1855 * rwnd is updated when the event is freed.
1857 sctp_assoc_rwnd_increase(event->asoc, copied);
1859 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1860 (event->msg_flags & MSG_EOR))
1861 msg->msg_flags |= MSG_EOR;
1863 msg->msg_flags &= ~MSG_EOR;
1866 if (flags & MSG_PEEK) {
1867 /* Release the skb reference acquired after peeking the skb in
1868 * sctp_skb_recv_datagram().
1872 /* Free the event which includes releasing the reference to
1873 * the owner of the skb, freeing the skb and updating the
1876 sctp_ulpevent_free(event);
1879 sctp_release_sock(sk);
1883 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1885 * This option is a on/off flag. If enabled no SCTP message
1886 * fragmentation will be performed. Instead if a message being sent
1887 * exceeds the current PMTU size, the message will NOT be sent and
1888 * instead a error will be indicated to the user.
1890 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1891 char __user *optval, int optlen)
1895 if (optlen < sizeof(int))
1898 if (get_user(val, (int __user *)optval))
1901 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1906 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1909 if (optlen != sizeof(struct sctp_event_subscribe))
1911 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1916 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1918 * This socket option is applicable to the UDP-style socket only. When
1919 * set it will cause associations that are idle for more than the
1920 * specified number of seconds to automatically close. An association
1921 * being idle is defined an association that has NOT sent or received
1922 * user data. The special value of '0' indicates that no automatic
1923 * close of any associations should be performed. The option expects an
1924 * integer defining the number of seconds of idle time before an
1925 * association is closed.
1927 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1930 struct sctp_sock *sp = sctp_sk(sk);
1932 /* Applicable to UDP-style socket only */
1933 if (sctp_style(sk, TCP))
1935 if (optlen != sizeof(int))
1937 if (copy_from_user(&sp->autoclose, optval, optlen))
1943 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1945 * Applications can enable or disable heartbeats for any peer address of
1946 * an association, modify an address's heartbeat interval, force a
1947 * heartbeat to be sent immediately, and adjust the address's maximum
1948 * number of retransmissions sent before an address is considered
1949 * unreachable. The following structure is used to access and modify an
1950 * address's parameters:
1952 * struct sctp_paddrparams {
1953 * sctp_assoc_t spp_assoc_id;
1954 * struct sockaddr_storage spp_address;
1955 * uint32_t spp_hbinterval;
1956 * uint16_t spp_pathmaxrxt;
1957 * uint32_t spp_pathmtu;
1958 * uint32_t spp_sackdelay;
1959 * uint32_t spp_flags;
1962 * spp_assoc_id - (one-to-many style socket) This is filled in the
1963 * application, and identifies the association for
1965 * spp_address - This specifies which address is of interest.
1966 * spp_hbinterval - This contains the value of the heartbeat interval,
1967 * in milliseconds. If a value of zero
1968 * is present in this field then no changes are to
1969 * be made to this parameter.
1970 * spp_pathmaxrxt - This contains the maximum number of
1971 * retransmissions before this address shall be
1972 * considered unreachable. If a value of zero
1973 * is present in this field then no changes are to
1974 * be made to this parameter.
1975 * spp_pathmtu - When Path MTU discovery is disabled the value
1976 * specified here will be the "fixed" path mtu.
1977 * Note that if the spp_address field is empty
1978 * then all associations on this address will
1979 * have this fixed path mtu set upon them.
1981 * spp_sackdelay - When delayed sack is enabled, this value specifies
1982 * the number of milliseconds that sacks will be delayed
1983 * for. This value will apply to all addresses of an
1984 * association if the spp_address field is empty. Note
1985 * also, that if delayed sack is enabled and this
1986 * value is set to 0, no change is made to the last
1987 * recorded delayed sack timer value.
1989 * spp_flags - These flags are used to control various features
1990 * on an association. The flag field may contain
1991 * zero or more of the following options.
1993 * SPP_HB_ENABLE - Enable heartbeats on the
1994 * specified address. Note that if the address
1995 * field is empty all addresses for the association
1996 * have heartbeats enabled upon them.
1998 * SPP_HB_DISABLE - Disable heartbeats on the
1999 * speicifed address. Note that if the address
2000 * field is empty all addresses for the association
2001 * will have their heartbeats disabled. Note also
2002 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2003 * mutually exclusive, only one of these two should
2004 * be specified. Enabling both fields will have
2005 * undetermined results.
2007 * SPP_HB_DEMAND - Request a user initiated heartbeat
2008 * to be made immediately.
2010 * SPP_PMTUD_ENABLE - This field will enable PMTU
2011 * discovery upon the specified address. Note that
2012 * if the address feild is empty then all addresses
2013 * on the association are effected.
2015 * SPP_PMTUD_DISABLE - This field will disable PMTU
2016 * discovery upon the specified address. Note that
2017 * if the address feild is empty then all addresses
2018 * on the association are effected. Not also that
2019 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2020 * exclusive. Enabling both will have undetermined
2023 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2024 * on delayed sack. The time specified in spp_sackdelay
2025 * is used to specify the sack delay for this address. Note
2026 * that if spp_address is empty then all addresses will
2027 * enable delayed sack and take on the sack delay
2028 * value specified in spp_sackdelay.
2029 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2030 * off delayed sack. If the spp_address field is blank then
2031 * delayed sack is disabled for the entire association. Note
2032 * also that this field is mutually exclusive to
2033 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2036 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2037 struct sctp_transport *trans,
2038 struct sctp_association *asoc,
2039 struct sctp_sock *sp,
2042 int sackdelay_change)
2046 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2047 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2052 if (params->spp_hbinterval) {
2054 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2056 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2058 sp->hbinterval = params->spp_hbinterval;
2064 trans->param_flags =
2065 (trans->param_flags & ~SPP_HB) | hb_change;
2068 (asoc->param_flags & ~SPP_HB) | hb_change;
2071 (sp->param_flags & ~SPP_HB) | hb_change;
2075 if (params->spp_pathmtu) {
2077 trans->pathmtu = params->spp_pathmtu;
2078 sctp_assoc_sync_pmtu(asoc);
2080 asoc->pathmtu = params->spp_pathmtu;
2081 sctp_frag_point(sp, params->spp_pathmtu);
2083 sp->pathmtu = params->spp_pathmtu;
2089 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2090 (params->spp_flags & SPP_PMTUD_ENABLE);
2091 trans->param_flags =
2092 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2094 sctp_transport_pmtu(trans);
2095 sctp_assoc_sync_pmtu(asoc);
2099 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2102 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2106 if (params->spp_sackdelay) {
2109 msecs_to_jiffies(params->spp_sackdelay);
2112 msecs_to_jiffies(params->spp_sackdelay);
2114 sp->sackdelay = params->spp_sackdelay;
2118 if (sackdelay_change) {
2120 trans->param_flags =
2121 (trans->param_flags & ~SPP_SACKDELAY) |
2125 (asoc->param_flags & ~SPP_SACKDELAY) |
2129 (sp->param_flags & ~SPP_SACKDELAY) |
2134 if (params->spp_pathmaxrxt) {
2136 trans->pathmaxrxt = params->spp_pathmaxrxt;
2138 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2140 sp->pathmaxrxt = params->spp_pathmaxrxt;
2147 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2148 char __user *optval, int optlen)
2150 struct sctp_paddrparams params;
2151 struct sctp_transport *trans = NULL;
2152 struct sctp_association *asoc = NULL;
2153 struct sctp_sock *sp = sctp_sk(sk);
2155 int hb_change, pmtud_change, sackdelay_change;
2157 if (optlen != sizeof(struct sctp_paddrparams))
2160 if (copy_from_user(¶ms, optval, optlen))
2163 /* Validate flags and value parameters. */
2164 hb_change = params.spp_flags & SPP_HB;
2165 pmtud_change = params.spp_flags & SPP_PMTUD;
2166 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2168 if (hb_change == SPP_HB ||
2169 pmtud_change == SPP_PMTUD ||
2170 sackdelay_change == SPP_SACKDELAY ||
2171 params.spp_sackdelay > 500 ||
2173 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2176 /* If an address other than INADDR_ANY is specified, and
2177 * no transport is found, then the request is invalid.
2179 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
2180 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2181 params.spp_assoc_id);
2186 /* Get association, if assoc_id != 0 and the socket is a one
2187 * to many style socket, and an association was not found, then
2188 * the id was invalid.
2190 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2191 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2194 /* Heartbeat demand can only be sent on a transport or
2195 * association, but not a socket.
2197 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2200 /* Process parameters. */
2201 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2202 hb_change, pmtud_change,
2208 /* If changes are for association, also apply parameters to each
2211 if (!trans && asoc) {
2212 struct list_head *pos;
2214 list_for_each(pos, &asoc->peer.transport_addr_list) {
2215 trans = list_entry(pos, struct sctp_transport,
2217 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2218 hb_change, pmtud_change,
2226 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2228 * This options will get or set the delayed ack timer. The time is set
2229 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2230 * endpoints default delayed ack timer value. If the assoc_id field is
2231 * non-zero, then the set or get effects the specified association.
2233 * struct sctp_assoc_value {
2234 * sctp_assoc_t assoc_id;
2235 * uint32_t assoc_value;
2238 * assoc_id - This parameter, indicates which association the
2239 * user is preforming an action upon. Note that if
2240 * this field's value is zero then the endpoints
2241 * default value is changed (effecting future
2242 * associations only).
2244 * assoc_value - This parameter contains the number of milliseconds
2245 * that the user is requesting the delayed ACK timer
2246 * be set to. Note that this value is defined in
2247 * the standard to be between 200 and 500 milliseconds.
2249 * Note: a value of zero will leave the value alone,
2250 * but disable SACK delay. A non-zero value will also
2251 * enable SACK delay.
2254 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2255 char __user *optval, int optlen)
2257 struct sctp_assoc_value params;
2258 struct sctp_transport *trans = NULL;
2259 struct sctp_association *asoc = NULL;
2260 struct sctp_sock *sp = sctp_sk(sk);
2262 if (optlen != sizeof(struct sctp_assoc_value))
2265 if (copy_from_user(¶ms, optval, optlen))
2268 /* Validate value parameter. */
2269 if (params.assoc_value > 500)
2272 /* Get association, if assoc_id != 0 and the socket is a one
2273 * to many style socket, and an association was not found, then
2274 * the id was invalid.
2276 asoc = sctp_id2assoc(sk, params.assoc_id);
2277 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2280 if (params.assoc_value) {
2283 msecs_to_jiffies(params.assoc_value);
2285 (asoc->param_flags & ~SPP_SACKDELAY) |
2286 SPP_SACKDELAY_ENABLE;
2288 sp->sackdelay = params.assoc_value;
2290 (sp->param_flags & ~SPP_SACKDELAY) |
2291 SPP_SACKDELAY_ENABLE;
2296 (asoc->param_flags & ~SPP_SACKDELAY) |
2297 SPP_SACKDELAY_DISABLE;
2300 (sp->param_flags & ~SPP_SACKDELAY) |
2301 SPP_SACKDELAY_DISABLE;
2305 /* If change is for association, also apply to each transport. */
2307 struct list_head *pos;
2309 list_for_each(pos, &asoc->peer.transport_addr_list) {
2310 trans = list_entry(pos, struct sctp_transport,
2312 if (params.assoc_value) {
2314 msecs_to_jiffies(params.assoc_value);
2315 trans->param_flags =
2316 (trans->param_flags & ~SPP_SACKDELAY) |
2317 SPP_SACKDELAY_ENABLE;
2319 trans->param_flags =
2320 (trans->param_flags & ~SPP_SACKDELAY) |
2321 SPP_SACKDELAY_DISABLE;
2329 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2331 * Applications can specify protocol parameters for the default association
2332 * initialization. The option name argument to setsockopt() and getsockopt()
2335 * Setting initialization parameters is effective only on an unconnected
2336 * socket (for UDP-style sockets only future associations are effected
2337 * by the change). With TCP-style sockets, this option is inherited by
2338 * sockets derived from a listener socket.
2340 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2342 struct sctp_initmsg sinit;
2343 struct sctp_sock *sp = sctp_sk(sk);
2345 if (optlen != sizeof(struct sctp_initmsg))
2347 if (copy_from_user(&sinit, optval, optlen))
2350 if (sinit.sinit_num_ostreams)
2351 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2352 if (sinit.sinit_max_instreams)
2353 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2354 if (sinit.sinit_max_attempts)
2355 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2356 if (sinit.sinit_max_init_timeo)
2357 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2363 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2365 * Applications that wish to use the sendto() system call may wish to
2366 * specify a default set of parameters that would normally be supplied
2367 * through the inclusion of ancillary data. This socket option allows
2368 * such an application to set the default sctp_sndrcvinfo structure.
2369 * The application that wishes to use this socket option simply passes
2370 * in to this call the sctp_sndrcvinfo structure defined in Section
2371 * 5.2.2) The input parameters accepted by this call include
2372 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2373 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2374 * to this call if the caller is using the UDP model.
2376 static int sctp_setsockopt_default_send_param(struct sock *sk,
2377 char __user *optval, int optlen)
2379 struct sctp_sndrcvinfo info;
2380 struct sctp_association *asoc;
2381 struct sctp_sock *sp = sctp_sk(sk);
2383 if (optlen != sizeof(struct sctp_sndrcvinfo))
2385 if (copy_from_user(&info, optval, optlen))
2388 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2389 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2393 asoc->default_stream = info.sinfo_stream;
2394 asoc->default_flags = info.sinfo_flags;
2395 asoc->default_ppid = info.sinfo_ppid;
2396 asoc->default_context = info.sinfo_context;
2397 asoc->default_timetolive = info.sinfo_timetolive;
2399 sp->default_stream = info.sinfo_stream;
2400 sp->default_flags = info.sinfo_flags;
2401 sp->default_ppid = info.sinfo_ppid;
2402 sp->default_context = info.sinfo_context;
2403 sp->default_timetolive = info.sinfo_timetolive;
2409 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2411 * Requests that the local SCTP stack use the enclosed peer address as
2412 * the association primary. The enclosed address must be one of the
2413 * association peer's addresses.
2415 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2418 struct sctp_prim prim;
2419 struct sctp_transport *trans;
2421 if (optlen != sizeof(struct sctp_prim))
2424 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2427 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2431 sctp_assoc_set_primary(trans->asoc, trans);
2437 * 7.1.5 SCTP_NODELAY
2439 * Turn on/off any Nagle-like algorithm. This means that packets are
2440 * generally sent as soon as possible and no unnecessary delays are
2441 * introduced, at the cost of more packets in the network. Expects an
2442 * integer boolean flag.
2444 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2449 if (optlen < sizeof(int))
2451 if (get_user(val, (int __user *)optval))
2454 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2460 * 7.1.1 SCTP_RTOINFO
2462 * The protocol parameters used to initialize and bound retransmission
2463 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2464 * and modify these parameters.
2465 * All parameters are time values, in milliseconds. A value of 0, when
2466 * modifying the parameters, indicates that the current value should not
2470 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2471 struct sctp_rtoinfo rtoinfo;
2472 struct sctp_association *asoc;
2474 if (optlen != sizeof (struct sctp_rtoinfo))
2477 if (copy_from_user(&rtoinfo, optval, optlen))
2480 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2482 /* Set the values to the specific association */
2483 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2487 if (rtoinfo.srto_initial != 0)
2489 msecs_to_jiffies(rtoinfo.srto_initial);
2490 if (rtoinfo.srto_max != 0)
2491 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2492 if (rtoinfo.srto_min != 0)
2493 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2495 /* If there is no association or the association-id = 0
2496 * set the values to the endpoint.
2498 struct sctp_sock *sp = sctp_sk(sk);
2500 if (rtoinfo.srto_initial != 0)
2501 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2502 if (rtoinfo.srto_max != 0)
2503 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2504 if (rtoinfo.srto_min != 0)
2505 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2513 * 7.1.2 SCTP_ASSOCINFO
2515 * This option is used to tune the the maximum retransmission attempts
2516 * of the association.
2517 * Returns an error if the new association retransmission value is
2518 * greater than the sum of the retransmission value of the peer.
2519 * See [SCTP] for more information.
2522 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2525 struct sctp_assocparams assocparams;
2526 struct sctp_association *asoc;
2528 if (optlen != sizeof(struct sctp_assocparams))
2530 if (copy_from_user(&assocparams, optval, optlen))
2533 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2535 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2538 /* Set the values to the specific association */
2540 if (assocparams.sasoc_asocmaxrxt != 0)
2541 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2542 if (assocparams.sasoc_cookie_life != 0) {
2543 asoc->cookie_life.tv_sec =
2544 assocparams.sasoc_cookie_life / 1000;
2545 asoc->cookie_life.tv_usec =
2546 (assocparams.sasoc_cookie_life % 1000)
2550 /* Set the values to the endpoint */
2551 struct sctp_sock *sp = sctp_sk(sk);
2553 if (assocparams.sasoc_asocmaxrxt != 0)
2554 sp->assocparams.sasoc_asocmaxrxt =
2555 assocparams.sasoc_asocmaxrxt;
2556 if (assocparams.sasoc_cookie_life != 0)
2557 sp->assocparams.sasoc_cookie_life =
2558 assocparams.sasoc_cookie_life;
2564 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2566 * This socket option is a boolean flag which turns on or off mapped V4
2567 * addresses. If this option is turned on and the socket is type
2568 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2569 * If this option is turned off, then no mapping will be done of V4
2570 * addresses and a user will receive both PF_INET6 and PF_INET type
2571 * addresses on the socket.
2573 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2576 struct sctp_sock *sp = sctp_sk(sk);
2578 if (optlen < sizeof(int))
2580 if (get_user(val, (int __user *)optval))
2591 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2593 * This socket option specifies the maximum size to put in any outgoing
2594 * SCTP chunk. If a message is larger than this size it will be
2595 * fragmented by SCTP into the specified size. Note that the underlying
2596 * SCTP implementation may fragment into smaller sized chunks when the
2597 * PMTU of the underlying association is smaller than the value set by
2600 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2602 struct sctp_association *asoc;
2603 struct list_head *pos;
2604 struct sctp_sock *sp = sctp_sk(sk);
2607 if (optlen < sizeof(int))
2609 if (get_user(val, (int __user *)optval))
2611 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2613 sp->user_frag = val;
2615 /* Update the frag_point of the existing associations. */
2616 list_for_each(pos, &(sp->ep->asocs)) {
2617 asoc = list_entry(pos, struct sctp_association, asocs);
2618 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2626 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2628 * Requests that the peer mark the enclosed address as the association
2629 * primary. The enclosed address must be one of the association's
2630 * locally bound addresses. The following structure is used to make a
2631 * set primary request:
2633 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2636 struct sctp_sock *sp;
2637 struct sctp_endpoint *ep;
2638 struct sctp_association *asoc = NULL;
2639 struct sctp_setpeerprim prim;
2640 struct sctp_chunk *chunk;
2646 if (!sctp_addip_enable)
2649 if (optlen != sizeof(struct sctp_setpeerprim))
2652 if (copy_from_user(&prim, optval, optlen))
2655 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2659 if (!asoc->peer.asconf_capable)
2662 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2665 if (!sctp_state(asoc, ESTABLISHED))
2668 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2669 return -EADDRNOTAVAIL;
2671 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2672 chunk = sctp_make_asconf_set_prim(asoc,
2673 (union sctp_addr *)&prim.sspp_addr);
2677 err = sctp_send_asconf(asoc, chunk);
2679 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2684 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2687 struct sctp_setadaption adaption;
2689 if (optlen != sizeof(struct sctp_setadaption))
2691 if (copy_from_user(&adaption, optval, optlen))
2694 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2699 /* API 6.2 setsockopt(), getsockopt()
2701 * Applications use setsockopt() and getsockopt() to set or retrieve
2702 * socket options. Socket options are used to change the default
2703 * behavior of sockets calls. They are described in Section 7.
2707 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2708 * int __user *optlen);
2709 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2712 * sd - the socket descript.
2713 * level - set to IPPROTO_SCTP for all SCTP options.
2714 * optname - the option name.
2715 * optval - the buffer to store the value of the option.
2716 * optlen - the size of the buffer.
2718 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2719 char __user *optval, int optlen)
2723 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2726 /* I can hardly begin to describe how wrong this is. This is
2727 * so broken as to be worse than useless. The API draft
2728 * REALLY is NOT helpful here... I am not convinced that the
2729 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2730 * are at all well-founded.
2732 if (level != SOL_SCTP) {
2733 struct sctp_af *af = sctp_sk(sk)->pf->af;
2734 retval = af->setsockopt(sk, level, optname, optval, optlen);
2741 case SCTP_SOCKOPT_BINDX_ADD:
2742 /* 'optlen' is the size of the addresses buffer. */
2743 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2744 optlen, SCTP_BINDX_ADD_ADDR);
2747 case SCTP_SOCKOPT_BINDX_REM:
2748 /* 'optlen' is the size of the addresses buffer. */
2749 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2750 optlen, SCTP_BINDX_REM_ADDR);
2753 case SCTP_SOCKOPT_CONNECTX:
2754 /* 'optlen' is the size of the addresses buffer. */
2755 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2759 case SCTP_DISABLE_FRAGMENTS:
2760 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2764 retval = sctp_setsockopt_events(sk, optval, optlen);
2767 case SCTP_AUTOCLOSE:
2768 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2771 case SCTP_PEER_ADDR_PARAMS:
2772 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2775 case SCTP_DELAYED_ACK_TIME:
2776 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2780 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2782 case SCTP_DEFAULT_SEND_PARAM:
2783 retval = sctp_setsockopt_default_send_param(sk, optval,
2786 case SCTP_PRIMARY_ADDR:
2787 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2789 case SCTP_SET_PEER_PRIMARY_ADDR:
2790 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2793 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2796 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2798 case SCTP_ASSOCINFO:
2799 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2801 case SCTP_I_WANT_MAPPED_V4_ADDR:
2802 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2805 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2807 case SCTP_ADAPTION_LAYER:
2808 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2812 retval = -ENOPROTOOPT;
2816 sctp_release_sock(sk);
2822 /* API 3.1.6 connect() - UDP Style Syntax
2824 * An application may use the connect() call in the UDP model to initiate an
2825 * association without sending data.
2829 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2831 * sd: the socket descriptor to have a new association added to.
2833 * nam: the address structure (either struct sockaddr_in or struct
2834 * sockaddr_in6 defined in RFC2553 [7]).
2836 * len: the size of the address.
2838 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2846 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2847 __FUNCTION__, sk, addr, addr_len);
2849 /* Validate addr_len before calling common connect/connectx routine. */
2850 af = sctp_get_af_specific(addr->sa_family);
2851 if (!af || addr_len < af->sockaddr_len) {
2854 /* Pass correct addr len to common routine (so it knows there
2855 * is only one address being passed.
2857 err = __sctp_connect(sk, addr, af->sockaddr_len);
2860 sctp_release_sock(sk);
2864 /* FIXME: Write comments. */
2865 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2867 return -EOPNOTSUPP; /* STUB */
2870 /* 4.1.4 accept() - TCP Style Syntax
2872 * Applications use accept() call to remove an established SCTP
2873 * association from the accept queue of the endpoint. A new socket
2874 * descriptor will be returned from accept() to represent the newly
2875 * formed association.
2877 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2879 struct sctp_sock *sp;
2880 struct sctp_endpoint *ep;
2881 struct sock *newsk = NULL;
2882 struct sctp_association *asoc;
2891 if (!sctp_style(sk, TCP)) {
2892 error = -EOPNOTSUPP;
2896 if (!sctp_sstate(sk, LISTENING)) {
2901 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2903 error = sctp_wait_for_accept(sk, timeo);
2907 /* We treat the list of associations on the endpoint as the accept
2908 * queue and pick the first association on the list.
2910 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2912 newsk = sp->pf->create_accept_sk(sk, asoc);
2918 /* Populate the fields of the newsk from the oldsk and migrate the
2919 * asoc to the newsk.
2921 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2924 sctp_release_sock(sk);
2929 /* The SCTP ioctl handler. */
2930 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2932 return -ENOIOCTLCMD;
2935 /* This is the function which gets called during socket creation to
2936 * initialized the SCTP-specific portion of the sock.
2937 * The sock structure should already be zero-filled memory.
2939 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2941 struct sctp_endpoint *ep;
2942 struct sctp_sock *sp;
2944 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2948 /* Initialize the SCTP per socket area. */
2949 switch (sk->sk_type) {
2950 case SOCK_SEQPACKET:
2951 sp->type = SCTP_SOCKET_UDP;
2954 sp->type = SCTP_SOCKET_TCP;
2957 return -ESOCKTNOSUPPORT;
2960 /* Initialize default send parameters. These parameters can be
2961 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2963 sp->default_stream = 0;
2964 sp->default_ppid = 0;
2965 sp->default_flags = 0;
2966 sp->default_context = 0;
2967 sp->default_timetolive = 0;
2969 /* Initialize default setup parameters. These parameters
2970 * can be modified with the SCTP_INITMSG socket option or
2971 * overridden by the SCTP_INIT CMSG.
2973 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2974 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2975 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2976 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2978 /* Initialize default RTO related parameters. These parameters can
2979 * be modified for with the SCTP_RTOINFO socket option.
2981 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2982 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2983 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2985 /* Initialize default association related parameters. These parameters
2986 * can be modified with the SCTP_ASSOCINFO socket option.
2988 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2989 sp->assocparams.sasoc_number_peer_destinations = 0;
2990 sp->assocparams.sasoc_peer_rwnd = 0;
2991 sp->assocparams.sasoc_local_rwnd = 0;
2992 sp->assocparams.sasoc_cookie_life =
2993 jiffies_to_msecs(sctp_valid_cookie_life);
2995 /* Initialize default event subscriptions. By default, all the
2998 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3000 /* Default Peer Address Parameters. These defaults can
3001 * be modified via SCTP_PEER_ADDR_PARAMS
3003 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
3004 sp->pathmaxrxt = sctp_max_retrans_path;
3005 sp->pathmtu = 0; // allow default discovery
3006 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
3007 sp->param_flags = SPP_HB_ENABLE |
3009 SPP_SACKDELAY_ENABLE;
3011 /* If enabled no SCTP message fragmentation will be performed.
3012 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3014 sp->disable_fragments = 0;
3016 /* Turn on/off any Nagle-like algorithm. */
3019 /* Enable by default. */
3022 /* Auto-close idle associations after the configured
3023 * number of seconds. A value of 0 disables this
3024 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3025 * for UDP-style sockets only.
3029 /* User specified fragmentation limit. */
3032 sp->adaption_ind = 0;
3034 sp->pf = sctp_get_pf_specific(sk->sk_family);
3036 /* Control variables for partial data delivery. */
3038 skb_queue_head_init(&sp->pd_lobby);
3040 /* Create a per socket endpoint structure. Even if we
3041 * change the data structure relationships, this may still
3042 * be useful for storing pre-connect address information.
3044 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3051 SCTP_DBG_OBJCNT_INC(sock);
3055 /* Cleanup any SCTP per socket resources. */
3056 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3058 struct sctp_endpoint *ep;
3060 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3062 /* Release our hold on the endpoint. */
3063 ep = sctp_sk(sk)->ep;
3064 sctp_endpoint_free(ep);
3069 /* API 4.1.7 shutdown() - TCP Style Syntax
3070 * int shutdown(int socket, int how);
3072 * sd - the socket descriptor of the association to be closed.
3073 * how - Specifies the type of shutdown. The values are
3076 * Disables further receive operations. No SCTP
3077 * protocol action is taken.
3079 * Disables further send operations, and initiates
3080 * the SCTP shutdown sequence.
3082 * Disables further send and receive operations
3083 * and initiates the SCTP shutdown sequence.
3085 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3087 struct sctp_endpoint *ep;
3088 struct sctp_association *asoc;
3090 if (!sctp_style(sk, TCP))
3093 if (how & SEND_SHUTDOWN) {
3094 ep = sctp_sk(sk)->ep;
3095 if (!list_empty(&ep->asocs)) {
3096 asoc = list_entry(ep->asocs.next,
3097 struct sctp_association, asocs);
3098 sctp_primitive_SHUTDOWN(asoc, NULL);
3103 /* 7.2.1 Association Status (SCTP_STATUS)
3105 * Applications can retrieve current status information about an
3106 * association, including association state, peer receiver window size,
3107 * number of unacked data chunks, and number of data chunks pending
3108 * receipt. This information is read-only.
3110 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3111 char __user *optval,
3114 struct sctp_status status;
3115 struct sctp_association *asoc = NULL;
3116 struct sctp_transport *transport;
3117 sctp_assoc_t associd;
3120 if (len != sizeof(status)) {
3125 if (copy_from_user(&status, optval, sizeof(status))) {
3130 associd = status.sstat_assoc_id;
3131 asoc = sctp_id2assoc(sk, associd);
3137 transport = asoc->peer.primary_path;
3139 status.sstat_assoc_id = sctp_assoc2id(asoc);
3140 status.sstat_state = asoc->state;
3141 status.sstat_rwnd = asoc->peer.rwnd;
3142 status.sstat_unackdata = asoc->unack_data;
3144 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3145 status.sstat_instrms = asoc->c.sinit_max_instreams;
3146 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3147 status.sstat_fragmentation_point = asoc->frag_point;
3148 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3149 memcpy(&status.sstat_primary.spinfo_address,
3150 &(transport->ipaddr), sizeof(union sctp_addr));
3151 /* Map ipv4 address into v4-mapped-on-v6 address. */
3152 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3153 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3154 status.sstat_primary.spinfo_state = transport->state;
3155 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3156 status.sstat_primary.spinfo_srtt = transport->srtt;
3157 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3158 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3160 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3161 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3163 if (put_user(len, optlen)) {
3168 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3169 len, status.sstat_state, status.sstat_rwnd,
3170 status.sstat_assoc_id);
3172 if (copy_to_user(optval, &status, len)) {
3182 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3184 * Applications can retrieve information about a specific peer address
3185 * of an association, including its reachability state, congestion
3186 * window, and retransmission timer values. This information is
3189 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3190 char __user *optval,
3193 struct sctp_paddrinfo pinfo;
3194 struct sctp_transport *transport;
3197 if (len != sizeof(pinfo)) {
3202 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3207 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3208 pinfo.spinfo_assoc_id);
3212 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3213 pinfo.spinfo_state = transport->state;
3214 pinfo.spinfo_cwnd = transport->cwnd;
3215 pinfo.spinfo_srtt = transport->srtt;
3216 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3217 pinfo.spinfo_mtu = transport->pathmtu;
3219 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3220 pinfo.spinfo_state = SCTP_ACTIVE;
3222 if (put_user(len, optlen)) {
3227 if (copy_to_user(optval, &pinfo, len)) {
3236 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3238 * This option is a on/off flag. If enabled no SCTP message
3239 * fragmentation will be performed. Instead if a message being sent
3240 * exceeds the current PMTU size, the message will NOT be sent and
3241 * instead a error will be indicated to the user.
3243 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3244 char __user *optval, int __user *optlen)
3248 if (len < sizeof(int))
3252 val = (sctp_sk(sk)->disable_fragments == 1);
3253 if (put_user(len, optlen))
3255 if (copy_to_user(optval, &val, len))
3260 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3262 * This socket option is used to specify various notifications and
3263 * ancillary data the user wishes to receive.
3265 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3268 if (len != sizeof(struct sctp_event_subscribe))
3270 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3275 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3277 * This socket option is applicable to the UDP-style socket only. When
3278 * set it will cause associations that are idle for more than the
3279 * specified number of seconds to automatically close. An association
3280 * being idle is defined an association that has NOT sent or received
3281 * user data. The special value of '0' indicates that no automatic
3282 * close of any associations should be performed. The option expects an
3283 * integer defining the number of seconds of idle time before an
3284 * association is closed.
3286 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3288 /* Applicable to UDP-style socket only */
3289 if (sctp_style(sk, TCP))
3291 if (len != sizeof(int))
3293 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3298 /* Helper routine to branch off an association to a new socket. */
3299 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3300 struct socket **sockp)
3302 struct sock *sk = asoc->base.sk;
3303 struct socket *sock;
3306 /* An association cannot be branched off from an already peeled-off
3307 * socket, nor is this supported for tcp style sockets.
3309 if (!sctp_style(sk, UDP))
3312 /* Create a new socket. */
3313 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3317 /* Populate the fields of the newsk from the oldsk and migrate the
3318 * asoc to the newsk.
3320 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3326 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3328 sctp_peeloff_arg_t peeloff;
3329 struct socket *newsock;
3331 struct sctp_association *asoc;
3333 if (len != sizeof(sctp_peeloff_arg_t))
3335 if (copy_from_user(&peeloff, optval, len))
3338 asoc = sctp_id2assoc(sk, peeloff.associd);
3344 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3346 retval = sctp_do_peeloff(asoc, &newsock);
3350 /* Map the socket to an unused fd that can be returned to the user. */
3351 retval = sock_map_fd(newsock);
3353 sock_release(newsock);
3357 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3358 __FUNCTION__, sk, asoc, newsock->sk, retval);
3360 /* Return the fd mapped to the new socket. */
3361 peeloff.sd = retval;
3362 if (copy_to_user(optval, &peeloff, len))
3369 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3371 * Applications can enable or disable heartbeats for any peer address of
3372 * an association, modify an address's heartbeat interval, force a
3373 * heartbeat to be sent immediately, and adjust the address's maximum
3374 * number of retransmissions sent before an address is considered
3375 * unreachable. The following structure is used to access and modify an
3376 * address's parameters:
3378 * struct sctp_paddrparams {
3379 * sctp_assoc_t spp_assoc_id;
3380 * struct sockaddr_storage spp_address;
3381 * uint32_t spp_hbinterval;
3382 * uint16_t spp_pathmaxrxt;
3383 * uint32_t spp_pathmtu;
3384 * uint32_t spp_sackdelay;
3385 * uint32_t spp_flags;
3388 * spp_assoc_id - (one-to-many style socket) This is filled in the
3389 * application, and identifies the association for
3391 * spp_address - This specifies which address is of interest.
3392 * spp_hbinterval - This contains the value of the heartbeat interval,
3393 * in milliseconds. If a value of zero
3394 * is present in this field then no changes are to
3395 * be made to this parameter.
3396 * spp_pathmaxrxt - This contains the maximum number of
3397 * retransmissions before this address shall be
3398 * considered unreachable. If a value of zero
3399 * is present in this field then no changes are to
3400 * be made to this parameter.
3401 * spp_pathmtu - When Path MTU discovery is disabled the value
3402 * specified here will be the "fixed" path mtu.
3403 * Note that if the spp_address field is empty
3404 * then all associations on this address will
3405 * have this fixed path mtu set upon them.
3407 * spp_sackdelay - When delayed sack is enabled, this value specifies
3408 * the number of milliseconds that sacks will be delayed
3409 * for. This value will apply to all addresses of an
3410 * association if the spp_address field is empty. Note
3411 * also, that if delayed sack is enabled and this
3412 * value is set to 0, no change is made to the last
3413 * recorded delayed sack timer value.
3415 * spp_flags - These flags are used to control various features
3416 * on an association. The flag field may contain
3417 * zero or more of the following options.
3419 * SPP_HB_ENABLE - Enable heartbeats on the
3420 * specified address. Note that if the address
3421 * field is empty all addresses for the association
3422 * have heartbeats enabled upon them.
3424 * SPP_HB_DISABLE - Disable heartbeats on the
3425 * speicifed address. Note that if the address
3426 * field is empty all addresses for the association
3427 * will have their heartbeats disabled. Note also
3428 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3429 * mutually exclusive, only one of these two should
3430 * be specified. Enabling both fields will have
3431 * undetermined results.
3433 * SPP_HB_DEMAND - Request a user initiated heartbeat
3434 * to be made immediately.
3436 * SPP_PMTUD_ENABLE - This field will enable PMTU
3437 * discovery upon the specified address. Note that
3438 * if the address feild is empty then all addresses
3439 * on the association are effected.
3441 * SPP_PMTUD_DISABLE - This field will disable PMTU
3442 * discovery upon the specified address. Note that
3443 * if the address feild is empty then all addresses
3444 * on the association are effected. Not also that
3445 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3446 * exclusive. Enabling both will have undetermined
3449 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3450 * on delayed sack. The time specified in spp_sackdelay
3451 * is used to specify the sack delay for this address. Note
3452 * that if spp_address is empty then all addresses will
3453 * enable delayed sack and take on the sack delay
3454 * value specified in spp_sackdelay.
3455 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3456 * off delayed sack. If the spp_address field is blank then
3457 * delayed sack is disabled for the entire association. Note
3458 * also that this field is mutually exclusive to
3459 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3462 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3463 char __user *optval, int __user *optlen)
3465 struct sctp_paddrparams params;
3466 struct sctp_transport *trans = NULL;
3467 struct sctp_association *asoc = NULL;
3468 struct sctp_sock *sp = sctp_sk(sk);
3470 if (len != sizeof(struct sctp_paddrparams))
3473 if (copy_from_user(¶ms, optval, len))
3476 /* If an address other than INADDR_ANY is specified, and
3477 * no transport is found, then the request is invalid.
3479 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) {
3480 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
3481 params.spp_assoc_id);
3483 SCTP_DEBUG_PRINTK("Failed no transport\n");
3488 /* Get association, if assoc_id != 0 and the socket is a one
3489 * to many style socket, and an association was not found, then
3490 * the id was invalid.
3492 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3493 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3494 SCTP_DEBUG_PRINTK("Failed no association\n");
3499 /* Fetch transport values. */
3500 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3501 params.spp_pathmtu = trans->pathmtu;
3502 params.spp_pathmaxrxt = trans->pathmaxrxt;
3503 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3505 /*draft-11 doesn't say what to return in spp_flags*/
3506 params.spp_flags = trans->param_flags;
3508 /* Fetch association values. */
3509 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3510 params.spp_pathmtu = asoc->pathmtu;
3511 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3512 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3514 /*draft-11 doesn't say what to return in spp_flags*/
3515 params.spp_flags = asoc->param_flags;
3517 /* Fetch socket values. */
3518 params.spp_hbinterval = sp->hbinterval;
3519 params.spp_pathmtu = sp->pathmtu;
3520 params.spp_sackdelay = sp->sackdelay;
3521 params.spp_pathmaxrxt = sp->pathmaxrxt;
3523 /*draft-11 doesn't say what to return in spp_flags*/
3524 params.spp_flags = sp->param_flags;
3527 if (copy_to_user(optval, ¶ms, len))
3530 if (put_user(len, optlen))
3536 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3538 * This options will get or set the delayed ack timer. The time is set
3539 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3540 * endpoints default delayed ack timer value. If the assoc_id field is
3541 * non-zero, then the set or get effects the specified association.
3543 * struct sctp_assoc_value {
3544 * sctp_assoc_t assoc_id;
3545 * uint32_t assoc_value;
3548 * assoc_id - This parameter, indicates which association the
3549 * user is preforming an action upon. Note that if
3550 * this field's value is zero then the endpoints
3551 * default value is changed (effecting future
3552 * associations only).
3554 * assoc_value - This parameter contains the number of milliseconds
3555 * that the user is requesting the delayed ACK timer
3556 * be set to. Note that this value is defined in
3557 * the standard to be between 200 and 500 milliseconds.
3559 * Note: a value of zero will leave the value alone,
3560 * but disable SACK delay. A non-zero value will also
3561 * enable SACK delay.
3563 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3564 char __user *optval,
3567 struct sctp_assoc_value params;
3568 struct sctp_association *asoc = NULL;
3569 struct sctp_sock *sp = sctp_sk(sk);
3571 if (len != sizeof(struct sctp_assoc_value))
3574 if (copy_from_user(¶ms, optval, len))
3577 /* Get association, if assoc_id != 0 and the socket is a one
3578 * to many style socket, and an association was not found, then
3579 * the id was invalid.
3581 asoc = sctp_id2assoc(sk, params.assoc_id);
3582 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3586 /* Fetch association values. */
3587 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3588 params.assoc_value = jiffies_to_msecs(
3591 params.assoc_value = 0;
3593 /* Fetch socket values. */
3594 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3595 params.assoc_value = sp->sackdelay;
3597 params.assoc_value = 0;
3600 if (copy_to_user(optval, ¶ms, len))
3603 if (put_user(len, optlen))
3609 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3611 * Applications can specify protocol parameters for the default association
3612 * initialization. The option name argument to setsockopt() and getsockopt()
3615 * Setting initialization parameters is effective only on an unconnected
3616 * socket (for UDP-style sockets only future associations are effected
3617 * by the change). With TCP-style sockets, this option is inherited by
3618 * sockets derived from a listener socket.
3620 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3622 if (len != sizeof(struct sctp_initmsg))
3624 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3629 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3630 char __user *optval,
3634 struct sctp_association *asoc;
3635 struct list_head *pos;
3638 if (len != sizeof(sctp_assoc_t))
3641 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3644 /* For UDP-style sockets, id specifies the association to query. */
3645 asoc = sctp_id2assoc(sk, id);
3649 list_for_each(pos, &asoc->peer.transport_addr_list) {
3657 * Old API for getting list of peer addresses. Does not work for 32-bit
3658 * programs running on a 64-bit kernel
3660 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3661 char __user *optval,
3664 struct sctp_association *asoc;
3665 struct list_head *pos;
3667 struct sctp_getaddrs_old getaddrs;
3668 struct sctp_transport *from;
3670 union sctp_addr temp;
3671 struct sctp_sock *sp = sctp_sk(sk);
3674 if (len != sizeof(struct sctp_getaddrs_old))
3677 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3680 if (getaddrs.addr_num <= 0) return -EINVAL;
3682 /* For UDP-style sockets, id specifies the association to query. */
3683 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3687 to = (void __user *)getaddrs.addrs;
3688 list_for_each(pos, &asoc->peer.transport_addr_list) {
3689 from = list_entry(pos, struct sctp_transport, transports);
3690 memcpy(&temp, &from->ipaddr, sizeof(temp));
3691 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3692 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3693 temp.v4.sin_port = htons(temp.v4.sin_port);
3694 if (copy_to_user(to, &temp, addrlen))
3698 if (cnt >= getaddrs.addr_num) break;
3700 getaddrs.addr_num = cnt;
3701 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3707 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3708 char __user *optval, int __user *optlen)
3710 struct sctp_association *asoc;
3711 struct list_head *pos;
3713 struct sctp_getaddrs getaddrs;
3714 struct sctp_transport *from;
3716 union sctp_addr temp;
3717 struct sctp_sock *sp = sctp_sk(sk);
3722 if (len < sizeof(struct sctp_getaddrs))
3725 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3728 /* For UDP-style sockets, id specifies the association to query. */
3729 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3733 to = optval + offsetof(struct sctp_getaddrs,addrs);
3734 space_left = len - sizeof(struct sctp_getaddrs) -
3735 offsetof(struct sctp_getaddrs,addrs);
3737 list_for_each(pos, &asoc->peer.transport_addr_list) {
3738 from = list_entry(pos, struct sctp_transport, transports);
3739 memcpy(&temp, &from->ipaddr, sizeof(temp));
3740 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3741 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3742 if(space_left < addrlen)
3744 temp.v4.sin_port = htons(temp.v4.sin_port);
3745 if (copy_to_user(to, &temp, addrlen))
3749 space_left -= addrlen;
3752 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3754 bytes_copied = ((char __user *)to) - optval;
3755 if (put_user(bytes_copied, optlen))
3761 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3762 char __user *optval,
3766 struct sctp_bind_addr *bp;
3767 struct sctp_association *asoc;
3768 struct list_head *pos;
3769 struct sctp_sockaddr_entry *addr;
3770 rwlock_t *addr_lock;
3771 unsigned long flags;
3774 if (len != sizeof(sctp_assoc_t))
3777 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3781 * For UDP-style sockets, id specifies the association to query.
3782 * If the id field is set to the value '0' then the locally bound
3783 * addresses are returned without regard to any particular
3787 bp = &sctp_sk(sk)->ep->base.bind_addr;
3788 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3790 asoc = sctp_id2assoc(sk, id);
3793 bp = &asoc->base.bind_addr;
3794 addr_lock = &asoc->base.addr_lock;
3797 sctp_read_lock(addr_lock);
3799 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3800 * addresses from the global local address list.
3802 if (sctp_list_single_entry(&bp->address_list)) {
3803 addr = list_entry(bp->address_list.next,
3804 struct sctp_sockaddr_entry, list);
3805 if (sctp_is_any(&addr->a)) {
3806 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3807 list_for_each(pos, &sctp_local_addr_list) {
3808 addr = list_entry(pos,
3809 struct sctp_sockaddr_entry,
3811 if ((PF_INET == sk->sk_family) &&
3812 (AF_INET6 == addr->a.sa.sa_family))
3816 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3824 list_for_each(pos, &bp->address_list) {
3829 sctp_read_unlock(addr_lock);
3833 /* Helper function that copies local addresses to user and returns the number
3834 * of addresses copied.
3836 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3839 struct list_head *pos;
3840 struct sctp_sockaddr_entry *addr;
3841 unsigned long flags;
3842 union sctp_addr temp;
3846 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3847 list_for_each(pos, &sctp_local_addr_list) {
3848 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3849 if ((PF_INET == sk->sk_family) &&
3850 (AF_INET6 == addr->a.sa.sa_family))
3852 memcpy(&temp, &addr->a, sizeof(temp));
3853 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3855 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3856 temp.v4.sin_port = htons(port);
3857 if (copy_to_user(to, &temp, addrlen)) {
3858 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3864 if (cnt >= max_addrs) break;
3866 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3871 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3872 void __user **to, size_t space_left)
3874 struct list_head *pos;
3875 struct sctp_sockaddr_entry *addr;
3876 unsigned long flags;
3877 union sctp_addr temp;
3881 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3882 list_for_each(pos, &sctp_local_addr_list) {
3883 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3884 if ((PF_INET == sk->sk_family) &&
3885 (AF_INET6 == addr->a.sa.sa_family))
3887 memcpy(&temp, &addr->a, sizeof(temp));
3888 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3890 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3891 if(space_left<addrlen)
3893 temp.v4.sin_port = htons(port);
3894 if (copy_to_user(*to, &temp, addrlen)) {
3895 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3901 space_left -= addrlen;
3903 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3908 /* Old API for getting list of local addresses. Does not work for 32-bit
3909 * programs running on a 64-bit kernel
3911 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3912 char __user *optval, int __user *optlen)
3914 struct sctp_bind_addr *bp;
3915 struct sctp_association *asoc;
3916 struct list_head *pos;
3918 struct sctp_getaddrs_old getaddrs;
3919 struct sctp_sockaddr_entry *addr;
3921 union sctp_addr temp;
3922 struct sctp_sock *sp = sctp_sk(sk);
3924 rwlock_t *addr_lock;
3927 if (len != sizeof(struct sctp_getaddrs_old))
3930 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3933 if (getaddrs.addr_num <= 0) return -EINVAL;
3935 * For UDP-style sockets, id specifies the association to query.
3936 * If the id field is set to the value '0' then the locally bound
3937 * addresses are returned without regard to any particular
3940 if (0 == getaddrs.assoc_id) {
3941 bp = &sctp_sk(sk)->ep->base.bind_addr;
3942 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3944 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3947 bp = &asoc->base.bind_addr;
3948 addr_lock = &asoc->base.addr_lock;
3951 to = getaddrs.addrs;
3953 sctp_read_lock(addr_lock);
3955 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3956 * addresses from the global local address list.
3958 if (sctp_list_single_entry(&bp->address_list)) {
3959 addr = list_entry(bp->address_list.next,
3960 struct sctp_sockaddr_entry, list);
3961 if (sctp_is_any(&addr->a)) {
3962 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3973 list_for_each(pos, &bp->address_list) {
3974 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3975 memcpy(&temp, &addr->a, sizeof(temp));
3976 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3977 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3978 temp.v4.sin_port = htons(temp.v4.sin_port);
3979 if (copy_to_user(to, &temp, addrlen)) {
3985 if (cnt >= getaddrs.addr_num) break;
3989 getaddrs.addr_num = cnt;
3990 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3994 sctp_read_unlock(addr_lock);
3998 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3999 char __user *optval, int __user *optlen)
4001 struct sctp_bind_addr *bp;
4002 struct sctp_association *asoc;
4003 struct list_head *pos;
4005 struct sctp_getaddrs getaddrs;
4006 struct sctp_sockaddr_entry *addr;
4008 union sctp_addr temp;
4009 struct sctp_sock *sp = sctp_sk(sk);
4011 rwlock_t *addr_lock;
4016 if (len <= sizeof(struct sctp_getaddrs))
4019 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4023 * For UDP-style sockets, id specifies the association to query.
4024 * If the id field is set to the value '0' then the locally bound
4025 * addresses are returned without regard to any particular
4028 if (0 == getaddrs.assoc_id) {
4029 bp = &sctp_sk(sk)->ep->base.bind_addr;
4030 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4032 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4035 bp = &asoc->base.bind_addr;
4036 addr_lock = &asoc->base.addr_lock;
4039 to = optval + offsetof(struct sctp_getaddrs,addrs);
4040 space_left = len - sizeof(struct sctp_getaddrs) -
4041 offsetof(struct sctp_getaddrs,addrs);
4043 sctp_read_lock(addr_lock);
4045 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4046 * addresses from the global local address list.
4048 if (sctp_list_single_entry(&bp->address_list)) {
4049 addr = list_entry(bp->address_list.next,
4050 struct sctp_sockaddr_entry, list);
4051 if (sctp_is_any(&addr->a)) {
4052 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4062 list_for_each(pos, &bp->address_list) {
4063 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4064 memcpy(&temp, &addr->a, sizeof(temp));
4065 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4066 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4067 if(space_left < addrlen)
4068 return -ENOMEM; /*fixme: right error?*/
4069 temp.v4.sin_port = htons(temp.v4.sin_port);
4070 if (copy_to_user(to, &temp, addrlen)) {
4076 space_left -= addrlen;
4080 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4082 bytes_copied = ((char __user *)to) - optval;
4083 if (put_user(bytes_copied, optlen))
4087 sctp_read_unlock(addr_lock);
4091 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4093 * Requests that the local SCTP stack use the enclosed peer address as
4094 * the association primary. The enclosed address must be one of the
4095 * association peer's addresses.
4097 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4098 char __user *optval, int __user *optlen)
4100 struct sctp_prim prim;
4101 struct sctp_association *asoc;
4102 struct sctp_sock *sp = sctp_sk(sk);
4104 if (len != sizeof(struct sctp_prim))
4107 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4110 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4114 if (!asoc->peer.primary_path)
4117 asoc->peer.primary_path->ipaddr.v4.sin_port =
4118 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4119 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4120 sizeof(union sctp_addr));
4121 asoc->peer.primary_path->ipaddr.v4.sin_port =
4122 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4124 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4125 (union sctp_addr *)&prim.ssp_addr);
4127 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4134 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4136 * Requests that the local endpoint set the specified Adaption Layer
4137 * Indication parameter for all future INIT and INIT-ACK exchanges.
4139 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4140 char __user *optval, int __user *optlen)
4142 struct sctp_setadaption adaption;
4144 if (len != sizeof(struct sctp_setadaption))
4147 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4148 if (copy_to_user(optval, &adaption, len))
4156 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4158 * Applications that wish to use the sendto() system call may wish to
4159 * specify a default set of parameters that would normally be supplied
4160 * through the inclusion of ancillary data. This socket option allows
4161 * such an application to set the default sctp_sndrcvinfo structure.
4164 * The application that wishes to use this socket option simply passes
4165 * in to this call the sctp_sndrcvinfo structure defined in Section
4166 * 5.2.2) The input parameters accepted by this call include
4167 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4168 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4169 * to this call if the caller is using the UDP model.
4171 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4173 static int sctp_getsockopt_default_send_param(struct sock *sk,
4174 int len, char __user *optval,
4177 struct sctp_sndrcvinfo info;
4178 struct sctp_association *asoc;
4179 struct sctp_sock *sp = sctp_sk(sk);
4181 if (len != sizeof(struct sctp_sndrcvinfo))
4183 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4186 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4187 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4191 info.sinfo_stream = asoc->default_stream;
4192 info.sinfo_flags = asoc->default_flags;
4193 info.sinfo_ppid = asoc->default_ppid;
4194 info.sinfo_context = asoc->default_context;
4195 info.sinfo_timetolive = asoc->default_timetolive;
4197 info.sinfo_stream = sp->default_stream;
4198 info.sinfo_flags = sp->default_flags;
4199 info.sinfo_ppid = sp->default_ppid;
4200 info.sinfo_context = sp->default_context;
4201 info.sinfo_timetolive = sp->default_timetolive;
4204 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4212 * 7.1.5 SCTP_NODELAY
4214 * Turn on/off any Nagle-like algorithm. This means that packets are
4215 * generally sent as soon as possible and no unnecessary delays are
4216 * introduced, at the cost of more packets in the network. Expects an
4217 * integer boolean flag.
4220 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4221 char __user *optval, int __user *optlen)
4225 if (len < sizeof(int))
4229 val = (sctp_sk(sk)->nodelay == 1);
4230 if (put_user(len, optlen))
4232 if (copy_to_user(optval, &val, len))
4239 * 7.1.1 SCTP_RTOINFO
4241 * The protocol parameters used to initialize and bound retransmission
4242 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4243 * and modify these parameters.
4244 * All parameters are time values, in milliseconds. A value of 0, when
4245 * modifying the parameters, indicates that the current value should not
4249 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4250 char __user *optval,
4251 int __user *optlen) {
4252 struct sctp_rtoinfo rtoinfo;
4253 struct sctp_association *asoc;
4255 if (len != sizeof (struct sctp_rtoinfo))
4258 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4261 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4263 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4266 /* Values corresponding to the specific association. */
4268 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4269 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4270 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4272 /* Values corresponding to the endpoint. */
4273 struct sctp_sock *sp = sctp_sk(sk);
4275 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4276 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4277 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4280 if (put_user(len, optlen))
4283 if (copy_to_user(optval, &rtoinfo, len))
4291 * 7.1.2 SCTP_ASSOCINFO
4293 * This option is used to tune the the maximum retransmission attempts
4294 * of the association.
4295 * Returns an error if the new association retransmission value is
4296 * greater than the sum of the retransmission value of the peer.
4297 * See [SCTP] for more information.
4300 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4301 char __user *optval,
4305 struct sctp_assocparams assocparams;
4306 struct sctp_association *asoc;
4307 struct list_head *pos;
4310 if (len != sizeof (struct sctp_assocparams))
4313 if (copy_from_user(&assocparams, optval,
4314 sizeof (struct sctp_assocparams)))
4317 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4319 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4322 /* Values correspoinding to the specific association */
4324 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4325 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4326 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4327 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4329 (asoc->cookie_life.tv_usec
4332 list_for_each(pos, &asoc->peer.transport_addr_list) {
4336 assocparams.sasoc_number_peer_destinations = cnt;
4338 /* Values corresponding to the endpoint */
4339 struct sctp_sock *sp = sctp_sk(sk);
4341 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4342 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4343 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4344 assocparams.sasoc_cookie_life =
4345 sp->assocparams.sasoc_cookie_life;
4346 assocparams.sasoc_number_peer_destinations =
4348 sasoc_number_peer_destinations;
4351 if (put_user(len, optlen))
4354 if (copy_to_user(optval, &assocparams, len))
4361 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4363 * This socket option is a boolean flag which turns on or off mapped V4
4364 * addresses. If this option is turned on and the socket is type
4365 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4366 * If this option is turned off, then no mapping will be done of V4
4367 * addresses and a user will receive both PF_INET6 and PF_INET type
4368 * addresses on the socket.
4370 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4371 char __user *optval, int __user *optlen)
4374 struct sctp_sock *sp = sctp_sk(sk);
4376 if (len < sizeof(int))
4381 if (put_user(len, optlen))
4383 if (copy_to_user(optval, &val, len))
4390 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4392 * This socket option specifies the maximum size to put in any outgoing
4393 * SCTP chunk. If a message is larger than this size it will be
4394 * fragmented by SCTP into the specified size. Note that the underlying
4395 * SCTP implementation may fragment into smaller sized chunks when the
4396 * PMTU of the underlying association is smaller than the value set by
4399 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4400 char __user *optval, int __user *optlen)
4404 if (len < sizeof(int))
4409 val = sctp_sk(sk)->user_frag;
4410 if (put_user(len, optlen))
4412 if (copy_to_user(optval, &val, len))
4418 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4419 char __user *optval, int __user *optlen)
4424 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4427 /* I can hardly begin to describe how wrong this is. This is
4428 * so broken as to be worse than useless. The API draft
4429 * REALLY is NOT helpful here... I am not convinced that the
4430 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4431 * are at all well-founded.
4433 if (level != SOL_SCTP) {
4434 struct sctp_af *af = sctp_sk(sk)->pf->af;
4436 retval = af->getsockopt(sk, level, optname, optval, optlen);
4440 if (get_user(len, optlen))
4447 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4449 case SCTP_DISABLE_FRAGMENTS:
4450 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4454 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4456 case SCTP_AUTOCLOSE:
4457 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4459 case SCTP_SOCKOPT_PEELOFF:
4460 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4462 case SCTP_PEER_ADDR_PARAMS:
4463 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4466 case SCTP_DELAYED_ACK_TIME:
4467 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4471 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4473 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4474 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4477 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4478 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4481 case SCTP_GET_PEER_ADDRS_OLD:
4482 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4485 case SCTP_GET_LOCAL_ADDRS_OLD:
4486 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4489 case SCTP_GET_PEER_ADDRS:
4490 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4493 case SCTP_GET_LOCAL_ADDRS:
4494 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4497 case SCTP_DEFAULT_SEND_PARAM:
4498 retval = sctp_getsockopt_default_send_param(sk, len,
4501 case SCTP_PRIMARY_ADDR:
4502 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4505 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4508 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4510 case SCTP_ASSOCINFO:
4511 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4513 case SCTP_I_WANT_MAPPED_V4_ADDR:
4514 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4517 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4519 case SCTP_GET_PEER_ADDR_INFO:
4520 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4523 case SCTP_ADAPTION_LAYER:
4524 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4528 retval = -ENOPROTOOPT;
4532 sctp_release_sock(sk);
4536 static void sctp_hash(struct sock *sk)
4541 static void sctp_unhash(struct sock *sk)
4546 /* Check if port is acceptable. Possibly find first available port.
4548 * The port hash table (contained in the 'global' SCTP protocol storage
4549 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4550 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4551 * list (the list number is the port number hashed out, so as you
4552 * would expect from a hash function, all the ports in a given list have
4553 * such a number that hashes out to the same list number; you were
4554 * expecting that, right?); so each list has a set of ports, with a
4555 * link to the socket (struct sock) that uses it, the port number and
4556 * a fastreuse flag (FIXME: NPI ipg).
4558 static struct sctp_bind_bucket *sctp_bucket_create(
4559 struct sctp_bind_hashbucket *head, unsigned short snum);
4561 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4563 struct sctp_bind_hashbucket *head; /* hash list */
4564 struct sctp_bind_bucket *pp; /* hash list port iterator */
4565 unsigned short snum;
4568 /* NOTE: Remember to put this back to net order. */
4569 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4570 snum = addr->v4.sin_port;
4572 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4573 sctp_local_bh_disable();
4576 /* Search for an available port.
4578 * 'sctp_port_rover' was the last port assigned, so
4579 * we start to search from 'sctp_port_rover +
4580 * 1'. What we do is first check if port 'rover' is
4581 * already in the hash table; if not, we use that; if
4582 * it is, we try next.
4584 int low = sysctl_local_port_range[0];
4585 int high = sysctl_local_port_range[1];
4586 int remaining = (high - low) + 1;
4590 sctp_spin_lock(&sctp_port_alloc_lock);
4591 rover = sctp_port_rover;
4594 if ((rover < low) || (rover > high))
4596 index = sctp_phashfn(rover);
4597 head = &sctp_port_hashtable[index];
4598 sctp_spin_lock(&head->lock);
4599 for (pp = head->chain; pp; pp = pp->next)
4600 if (pp->port == rover)
4604 sctp_spin_unlock(&head->lock);
4605 } while (--remaining > 0);
4606 sctp_port_rover = rover;
4607 sctp_spin_unlock(&sctp_port_alloc_lock);
4609 /* Exhausted local port range during search? */
4614 /* OK, here is the one we will use. HEAD (the port
4615 * hash table list entry) is non-NULL and we hold it's
4620 /* We are given an specific port number; we verify
4621 * that it is not being used. If it is used, we will
4622 * exahust the search in the hash list corresponding
4623 * to the port number (snum) - we detect that with the
4624 * port iterator, pp being NULL.
4626 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4627 sctp_spin_lock(&head->lock);
4628 for (pp = head->chain; pp; pp = pp->next) {
4629 if (pp->port == snum)
4636 if (!hlist_empty(&pp->owner)) {
4637 /* We had a port hash table hit - there is an
4638 * available port (pp != NULL) and it is being
4639 * used by other socket (pp->owner not empty); that other
4640 * socket is going to be sk2.
4642 int reuse = sk->sk_reuse;
4644 struct hlist_node *node;
4646 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4647 if (pp->fastreuse && sk->sk_reuse)
4650 /* Run through the list of sockets bound to the port
4651 * (pp->port) [via the pointers bind_next and
4652 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4653 * we get the endpoint they describe and run through
4654 * the endpoint's list of IP (v4 or v6) addresses,
4655 * comparing each of the addresses with the address of
4656 * the socket sk. If we find a match, then that means
4657 * that this port/socket (sk) combination are already
4660 sk_for_each_bound(sk2, node, &pp->owner) {
4661 struct sctp_endpoint *ep2;
4662 ep2 = sctp_sk(sk2)->ep;
4664 if (reuse && sk2->sk_reuse)
4667 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4673 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4676 /* If there was a hash table miss, create a new port. */
4678 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4681 /* In either case (hit or miss), make sure fastreuse is 1 only
4682 * if sk->sk_reuse is too (that is, if the caller requested
4683 * SO_REUSEADDR on this socket -sk-).
4685 if (hlist_empty(&pp->owner))
4686 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4687 else if (pp->fastreuse && !sk->sk_reuse)
4690 /* We are set, so fill up all the data in the hash table
4691 * entry, tie the socket list information with the rest of the
4692 * sockets FIXME: Blurry, NPI (ipg).
4695 inet_sk(sk)->num = snum;
4696 if (!sctp_sk(sk)->bind_hash) {
4697 sk_add_bind_node(sk, &pp->owner);
4698 sctp_sk(sk)->bind_hash = pp;
4703 sctp_spin_unlock(&head->lock);
4706 sctp_local_bh_enable();
4707 addr->v4.sin_port = htons(addr->v4.sin_port);
4711 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4712 * port is requested.
4714 static int sctp_get_port(struct sock *sk, unsigned short snum)
4717 union sctp_addr addr;
4718 struct sctp_af *af = sctp_sk(sk)->pf->af;
4720 /* Set up a dummy address struct from the sk. */
4721 af->from_sk(&addr, sk);
4722 addr.v4.sin_port = htons(snum);
4724 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4725 ret = sctp_get_port_local(sk, &addr);
4727 return (ret ? 1 : 0);
4731 * 3.1.3 listen() - UDP Style Syntax
4733 * By default, new associations are not accepted for UDP style sockets.
4734 * An application uses listen() to mark a socket as being able to
4735 * accept new associations.
4737 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4739 struct sctp_sock *sp = sctp_sk(sk);
4740 struct sctp_endpoint *ep = sp->ep;
4742 /* Only UDP style sockets that are not peeled off are allowed to
4745 if (!sctp_style(sk, UDP))
4748 /* If backlog is zero, disable listening. */
4750 if (sctp_sstate(sk, CLOSED))
4753 sctp_unhash_endpoint(ep);
4754 sk->sk_state = SCTP_SS_CLOSED;
4757 /* Return if we are already listening. */
4758 if (sctp_sstate(sk, LISTENING))
4762 * If a bind() or sctp_bindx() is not called prior to a listen()
4763 * call that allows new associations to be accepted, the system
4764 * picks an ephemeral port and will choose an address set equivalent
4765 * to binding with a wildcard address.
4767 * This is not currently spelled out in the SCTP sockets
4768 * extensions draft, but follows the practice as seen in TCP
4771 if (!ep->base.bind_addr.port) {
4772 if (sctp_autobind(sk))
4775 sk->sk_state = SCTP_SS_LISTENING;
4776 sctp_hash_endpoint(ep);
4781 * 4.1.3 listen() - TCP Style Syntax
4783 * Applications uses listen() to ready the SCTP endpoint for accepting
4784 * inbound associations.
4786 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4788 struct sctp_sock *sp = sctp_sk(sk);
4789 struct sctp_endpoint *ep = sp->ep;
4791 /* If backlog is zero, disable listening. */
4793 if (sctp_sstate(sk, CLOSED))
4796 sctp_unhash_endpoint(ep);
4797 sk->sk_state = SCTP_SS_CLOSED;
4800 if (sctp_sstate(sk, LISTENING))
4804 * If a bind() or sctp_bindx() is not called prior to a listen()
4805 * call that allows new associations to be accepted, the system
4806 * picks an ephemeral port and will choose an address set equivalent
4807 * to binding with a wildcard address.
4809 * This is not currently spelled out in the SCTP sockets
4810 * extensions draft, but follows the practice as seen in TCP
4813 if (!ep->base.bind_addr.port) {
4814 if (sctp_autobind(sk))
4817 sk->sk_state = SCTP_SS_LISTENING;
4818 sk->sk_max_ack_backlog = backlog;
4819 sctp_hash_endpoint(ep);
4824 * Move a socket to LISTENING state.
4826 int sctp_inet_listen(struct socket *sock, int backlog)
4828 struct sock *sk = sock->sk;
4829 struct crypto_tfm *tfm=NULL;
4832 if (unlikely(backlog < 0))
4837 if (sock->state != SS_UNCONNECTED)
4840 /* Allocate HMAC for generating cookie. */
4841 if (sctp_hmac_alg) {
4842 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4849 switch (sock->type) {
4850 case SOCK_SEQPACKET:
4851 err = sctp_seqpacket_listen(sk, backlog);
4854 err = sctp_stream_listen(sk, backlog);
4862 /* Store away the transform reference. */
4863 sctp_sk(sk)->hmac = tfm;
4865 sctp_release_sock(sk);
4868 sctp_crypto_free_tfm(tfm);
4873 * This function is done by modeling the current datagram_poll() and the
4874 * tcp_poll(). Note that, based on these implementations, we don't
4875 * lock the socket in this function, even though it seems that,
4876 * ideally, locking or some other mechanisms can be used to ensure
4877 * the integrity of the counters (sndbuf and wmem_alloc) used
4878 * in this place. We assume that we don't need locks either until proven
4881 * Another thing to note is that we include the Async I/O support
4882 * here, again, by modeling the current TCP/UDP code. We don't have
4883 * a good way to test with it yet.
4885 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4887 struct sock *sk = sock->sk;
4888 struct sctp_sock *sp = sctp_sk(sk);
4891 poll_wait(file, sk->sk_sleep, wait);
4893 /* A TCP-style listening socket becomes readable when the accept queue
4896 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4897 return (!list_empty(&sp->ep->asocs)) ?
4898 (POLLIN | POLLRDNORM) : 0;
4902 /* Is there any exceptional events? */
4903 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4905 if (sk->sk_shutdown == SHUTDOWN_MASK)
4908 /* Is it readable? Reconsider this code with TCP-style support. */
4909 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4910 (sk->sk_shutdown & RCV_SHUTDOWN))
4911 mask |= POLLIN | POLLRDNORM;
4913 /* The association is either gone or not ready. */
4914 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4917 /* Is it writable? */
4918 if (sctp_writeable(sk)) {
4919 mask |= POLLOUT | POLLWRNORM;
4921 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4923 * Since the socket is not locked, the buffer
4924 * might be made available after the writeable check and
4925 * before the bit is set. This could cause a lost I/O
4926 * signal. tcp_poll() has a race breaker for this race
4927 * condition. Based on their implementation, we put
4928 * in the following code to cover it as well.
4930 if (sctp_writeable(sk))
4931 mask |= POLLOUT | POLLWRNORM;
4936 /********************************************************************
4937 * 2nd Level Abstractions
4938 ********************************************************************/
4940 static struct sctp_bind_bucket *sctp_bucket_create(
4941 struct sctp_bind_hashbucket *head, unsigned short snum)
4943 struct sctp_bind_bucket *pp;
4945 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4946 SCTP_DBG_OBJCNT_INC(bind_bucket);
4950 INIT_HLIST_HEAD(&pp->owner);
4951 if ((pp->next = head->chain) != NULL)
4952 pp->next->pprev = &pp->next;
4954 pp->pprev = &head->chain;
4959 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4960 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4962 if (hlist_empty(&pp->owner)) {
4964 pp->next->pprev = pp->pprev;
4965 *(pp->pprev) = pp->next;
4966 kmem_cache_free(sctp_bucket_cachep, pp);
4967 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4971 /* Release this socket's reference to a local port. */
4972 static inline void __sctp_put_port(struct sock *sk)
4974 struct sctp_bind_hashbucket *head =
4975 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4976 struct sctp_bind_bucket *pp;
4978 sctp_spin_lock(&head->lock);
4979 pp = sctp_sk(sk)->bind_hash;
4980 __sk_del_bind_node(sk);
4981 sctp_sk(sk)->bind_hash = NULL;
4982 inet_sk(sk)->num = 0;
4983 sctp_bucket_destroy(pp);
4984 sctp_spin_unlock(&head->lock);
4987 void sctp_put_port(struct sock *sk)
4989 sctp_local_bh_disable();
4990 __sctp_put_port(sk);
4991 sctp_local_bh_enable();
4995 * The system picks an ephemeral port and choose an address set equivalent
4996 * to binding with a wildcard address.
4997 * One of those addresses will be the primary address for the association.
4998 * This automatically enables the multihoming capability of SCTP.
5000 static int sctp_autobind(struct sock *sk)
5002 union sctp_addr autoaddr;
5004 unsigned short port;
5006 /* Initialize a local sockaddr structure to INADDR_ANY. */
5007 af = sctp_sk(sk)->pf->af;
5009 port = htons(inet_sk(sk)->num);
5010 af->inaddr_any(&autoaddr, port);
5012 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5015 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5018 * 4.2 The cmsghdr Structure *
5020 * When ancillary data is sent or received, any number of ancillary data
5021 * objects can be specified by the msg_control and msg_controllen members of
5022 * the msghdr structure, because each object is preceded by
5023 * a cmsghdr structure defining the object's length (the cmsg_len member).
5024 * Historically Berkeley-derived implementations have passed only one object
5025 * at a time, but this API allows multiple objects to be
5026 * passed in a single call to sendmsg() or recvmsg(). The following example
5027 * shows two ancillary data objects in a control buffer.
5029 * |<--------------------------- msg_controllen -------------------------->|
5032 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5034 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5037 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5039 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5042 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5043 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5045 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5047 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5054 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5055 sctp_cmsgs_t *cmsgs)
5057 struct cmsghdr *cmsg;
5059 for (cmsg = CMSG_FIRSTHDR(msg);
5061 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5062 if (!CMSG_OK(msg, cmsg))
5065 /* Should we parse this header or ignore? */
5066 if (cmsg->cmsg_level != IPPROTO_SCTP)
5069 /* Strictly check lengths following example in SCM code. */
5070 switch (cmsg->cmsg_type) {
5072 /* SCTP Socket API Extension
5073 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5075 * This cmsghdr structure provides information for
5076 * initializing new SCTP associations with sendmsg().
5077 * The SCTP_INITMSG socket option uses this same data
5078 * structure. This structure is not used for
5081 * cmsg_level cmsg_type cmsg_data[]
5082 * ------------ ------------ ----------------------
5083 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5085 if (cmsg->cmsg_len !=
5086 CMSG_LEN(sizeof(struct sctp_initmsg)))
5088 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5092 /* SCTP Socket API Extension
5093 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5095 * This cmsghdr structure specifies SCTP options for
5096 * sendmsg() and describes SCTP header information
5097 * about a received message through recvmsg().
5099 * cmsg_level cmsg_type cmsg_data[]
5100 * ------------ ------------ ----------------------
5101 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5103 if (cmsg->cmsg_len !=
5104 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5108 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5110 /* Minimally, validate the sinfo_flags. */
5111 if (cmsgs->info->sinfo_flags &
5112 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5113 SCTP_ABORT | SCTP_EOF))
5125 * Wait for a packet..
5126 * Note: This function is the same function as in core/datagram.c
5127 * with a few modifications to make lksctp work.
5129 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5134 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5136 /* Socket errors? */
5137 error = sock_error(sk);
5141 if (!skb_queue_empty(&sk->sk_receive_queue))
5144 /* Socket shut down? */
5145 if (sk->sk_shutdown & RCV_SHUTDOWN)
5148 /* Sequenced packets can come disconnected. If so we report the
5153 /* Is there a good reason to think that we may receive some data? */
5154 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5157 /* Handle signals. */
5158 if (signal_pending(current))
5161 /* Let another process have a go. Since we are going to sleep
5162 * anyway. Note: This may cause odd behaviors if the message
5163 * does not fit in the user's buffer, but this seems to be the
5164 * only way to honor MSG_DONTWAIT realistically.
5166 sctp_release_sock(sk);
5167 *timeo_p = schedule_timeout(*timeo_p);
5171 finish_wait(sk->sk_sleep, &wait);
5175 error = sock_intr_errno(*timeo_p);
5178 finish_wait(sk->sk_sleep, &wait);
5183 /* Receive a datagram.
5184 * Note: This is pretty much the same routine as in core/datagram.c
5185 * with a few changes to make lksctp work.
5187 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5188 int noblock, int *err)
5191 struct sk_buff *skb;
5194 timeo = sock_rcvtimeo(sk, noblock);
5196 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5197 timeo, MAX_SCHEDULE_TIMEOUT);
5200 /* Again only user level code calls this function,
5201 * so nothing interrupt level
5202 * will suddenly eat the receive_queue.
5204 * Look at current nfs client by the way...
5205 * However, this function was corrent in any case. 8)
5207 if (flags & MSG_PEEK) {
5208 spin_lock_bh(&sk->sk_receive_queue.lock);
5209 skb = skb_peek(&sk->sk_receive_queue);
5211 atomic_inc(&skb->users);
5212 spin_unlock_bh(&sk->sk_receive_queue.lock);
5214 skb = skb_dequeue(&sk->sk_receive_queue);
5220 /* Caller is allowed not to check sk->sk_err before calling. */
5221 error = sock_error(sk);
5225 if (sk->sk_shutdown & RCV_SHUTDOWN)
5228 /* User doesn't want to wait. */
5232 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5241 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5242 static void __sctp_write_space(struct sctp_association *asoc)
5244 struct sock *sk = asoc->base.sk;
5245 struct socket *sock = sk->sk_socket;
5247 if ((sctp_wspace(asoc) > 0) && sock) {
5248 if (waitqueue_active(&asoc->wait))
5249 wake_up_interruptible(&asoc->wait);
5251 if (sctp_writeable(sk)) {
5252 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5253 wake_up_interruptible(sk->sk_sleep);
5255 /* Note that we try to include the Async I/O support
5256 * here by modeling from the current TCP/UDP code.
5257 * We have not tested with it yet.
5259 if (sock->fasync_list &&
5260 !(sk->sk_shutdown & SEND_SHUTDOWN))
5261 sock_wake_async(sock, 2, POLL_OUT);
5266 /* Do accounting for the sndbuf space.
5267 * Decrement the used sndbuf space of the corresponding association by the
5268 * data size which was just transmitted(freed).
5270 static void sctp_wfree(struct sk_buff *skb)
5272 struct sctp_association *asoc;
5273 struct sctp_chunk *chunk;
5276 /* Get the saved chunk pointer. */
5277 chunk = *((struct sctp_chunk **)(skb->cb));
5280 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5281 sizeof(struct sk_buff) +
5282 sizeof(struct sctp_chunk);
5284 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5287 __sctp_write_space(asoc);
5289 sctp_association_put(asoc);
5292 /* Helper function to wait for space in the sndbuf. */
5293 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5296 struct sock *sk = asoc->base.sk;
5298 long current_timeo = *timeo_p;
5301 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5302 asoc, (long)(*timeo_p), msg_len);
5304 /* Increment the association's refcnt. */
5305 sctp_association_hold(asoc);
5307 /* Wait on the association specific sndbuf space. */
5309 prepare_to_wait_exclusive(&asoc->wait, &wait,
5310 TASK_INTERRUPTIBLE);
5313 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5316 if (signal_pending(current))
5317 goto do_interrupted;
5318 if (msg_len <= sctp_wspace(asoc))
5321 /* Let another process have a go. Since we are going
5324 sctp_release_sock(sk);
5325 current_timeo = schedule_timeout(current_timeo);
5328 *timeo_p = current_timeo;
5332 finish_wait(&asoc->wait, &wait);
5334 /* Release the association's refcnt. */
5335 sctp_association_put(asoc);
5344 err = sock_intr_errno(*timeo_p);
5352 /* If socket sndbuf has changed, wake up all per association waiters. */
5353 void sctp_write_space(struct sock *sk)
5355 struct sctp_association *asoc;
5356 struct list_head *pos;
5358 /* Wake up the tasks in each wait queue. */
5359 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5360 asoc = list_entry(pos, struct sctp_association, asocs);
5361 __sctp_write_space(asoc);
5365 /* Is there any sndbuf space available on the socket?
5367 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5368 * associations on the same socket. For a UDP-style socket with
5369 * multiple associations, it is possible for it to be "unwriteable"
5370 * prematurely. I assume that this is acceptable because
5371 * a premature "unwriteable" is better than an accidental "writeable" which
5372 * would cause an unwanted block under certain circumstances. For the 1-1
5373 * UDP-style sockets or TCP-style sockets, this code should work.
5376 static int sctp_writeable(struct sock *sk)
5380 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5386 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5387 * returns immediately with EINPROGRESS.
5389 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5391 struct sock *sk = asoc->base.sk;
5393 long current_timeo = *timeo_p;
5396 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5399 /* Increment the association's refcnt. */
5400 sctp_association_hold(asoc);
5403 prepare_to_wait_exclusive(&asoc->wait, &wait,
5404 TASK_INTERRUPTIBLE);
5407 if (sk->sk_shutdown & RCV_SHUTDOWN)
5409 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5412 if (signal_pending(current))
5413 goto do_interrupted;
5415 if (sctp_state(asoc, ESTABLISHED))
5418 /* Let another process have a go. Since we are going
5421 sctp_release_sock(sk);
5422 current_timeo = schedule_timeout(current_timeo);
5425 *timeo_p = current_timeo;
5429 finish_wait(&asoc->wait, &wait);
5431 /* Release the association's refcnt. */
5432 sctp_association_put(asoc);
5437 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5440 err = -ECONNREFUSED;
5444 err = sock_intr_errno(*timeo_p);
5452 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5454 struct sctp_endpoint *ep;
5458 ep = sctp_sk(sk)->ep;
5462 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5463 TASK_INTERRUPTIBLE);
5465 if (list_empty(&ep->asocs)) {
5466 sctp_release_sock(sk);
5467 timeo = schedule_timeout(timeo);
5472 if (!sctp_sstate(sk, LISTENING))
5476 if (!list_empty(&ep->asocs))
5479 err = sock_intr_errno(timeo);
5480 if (signal_pending(current))
5488 finish_wait(sk->sk_sleep, &wait);
5493 void sctp_wait_for_close(struct sock *sk, long timeout)
5498 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5499 if (list_empty(&sctp_sk(sk)->ep->asocs))
5501 sctp_release_sock(sk);
5502 timeout = schedule_timeout(timeout);
5504 } while (!signal_pending(current) && timeout);
5506 finish_wait(sk->sk_sleep, &wait);
5509 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5510 * and its messages to the newsk.
5512 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5513 struct sctp_association *assoc,
5514 sctp_socket_type_t type)
5516 struct sctp_sock *oldsp = sctp_sk(oldsk);
5517 struct sctp_sock *newsp = sctp_sk(newsk);
5518 struct sctp_bind_bucket *pp; /* hash list port iterator */
5519 struct sctp_endpoint *newep = newsp->ep;
5520 struct sk_buff *skb, *tmp;
5521 struct sctp_ulpevent *event;
5524 /* Migrate socket buffer sizes and all the socket level options to the
5527 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5528 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5529 /* Brute force copy old sctp opt. */
5530 inet_sk_copy_descendant(newsk, oldsk);
5532 /* Restore the ep value that was overwritten with the above structure
5538 /* Hook this new socket in to the bind_hash list. */
5539 pp = sctp_sk(oldsk)->bind_hash;
5540 sk_add_bind_node(newsk, &pp->owner);
5541 sctp_sk(newsk)->bind_hash = pp;
5542 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5544 /* Copy the bind_addr list from the original endpoint to the new
5545 * endpoint so that we can handle restarts properly
5547 if (assoc->peer.ipv4_address)
5548 flags |= SCTP_ADDR4_PEERSUPP;
5549 if (assoc->peer.ipv6_address)
5550 flags |= SCTP_ADDR6_PEERSUPP;
5551 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5552 &oldsp->ep->base.bind_addr,
5553 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5555 /* Move any messages in the old socket's receive queue that are for the
5556 * peeled off association to the new socket's receive queue.
5558 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5559 event = sctp_skb2event(skb);
5560 if (event->asoc == assoc) {
5562 __skb_unlink(skb, &oldsk->sk_receive_queue);
5563 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5564 skb_set_owner_r(skb, newsk);
5568 /* Clean up any messages pending delivery due to partial
5569 * delivery. Three cases:
5570 * 1) No partial deliver; no work.
5571 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5572 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5574 skb_queue_head_init(&newsp->pd_lobby);
5575 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5577 if (sctp_sk(oldsk)->pd_mode) {
5578 struct sk_buff_head *queue;
5580 /* Decide which queue to move pd_lobby skbs to. */
5581 if (assoc->ulpq.pd_mode) {
5582 queue = &newsp->pd_lobby;
5584 queue = &newsk->sk_receive_queue;
5586 /* Walk through the pd_lobby, looking for skbs that
5587 * need moved to the new socket.
5589 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5590 event = sctp_skb2event(skb);
5591 if (event->asoc == assoc) {
5593 __skb_unlink(skb, &oldsp->pd_lobby);
5594 __skb_queue_tail(queue, skb);
5595 skb_set_owner_r(skb, newsk);
5599 /* Clear up any skbs waiting for the partial
5600 * delivery to finish.
5602 if (assoc->ulpq.pd_mode)
5603 sctp_clear_pd(oldsk);
5607 /* Set the type of socket to indicate that it is peeled off from the
5608 * original UDP-style socket or created with the accept() call on a
5609 * TCP-style socket..
5613 spin_lock_bh(&oldsk->sk_lock.slock);
5614 /* Migrate the backlog from oldsk to newsk. */
5615 sctp_backlog_migrate(assoc, oldsk, newsk);
5616 /* Migrate the association to the new socket. */
5617 sctp_assoc_migrate(assoc, newsk);
5618 spin_unlock_bh(&oldsk->sk_lock.slock);
5620 /* If the association on the newsk is already closed before accept()
5621 * is called, set RCV_SHUTDOWN flag.
5623 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5624 newsk->sk_shutdown |= RCV_SHUTDOWN;
5626 newsk->sk_state = SCTP_SS_ESTABLISHED;
5629 /* This proto struct describes the ULP interface for SCTP. */
5630 struct proto sctp_prot = {
5632 .owner = THIS_MODULE,
5633 .close = sctp_close,
5634 .connect = sctp_connect,
5635 .disconnect = sctp_disconnect,
5636 .accept = sctp_accept,
5637 .ioctl = sctp_ioctl,
5638 .init = sctp_init_sock,
5639 .destroy = sctp_destroy_sock,
5640 .shutdown = sctp_shutdown,
5641 .setsockopt = sctp_setsockopt,
5642 .getsockopt = sctp_getsockopt,
5643 .sendmsg = sctp_sendmsg,
5644 .recvmsg = sctp_recvmsg,
5646 .backlog_rcv = sctp_backlog_rcv,
5648 .unhash = sctp_unhash,
5649 .get_port = sctp_get_port,
5650 .obj_size = sizeof(struct sctp_sock),
5653 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5654 struct proto sctpv6_prot = {
5656 .owner = THIS_MODULE,
5657 .close = sctp_close,
5658 .connect = sctp_connect,
5659 .disconnect = sctp_disconnect,
5660 .accept = sctp_accept,
5661 .ioctl = sctp_ioctl,
5662 .init = sctp_init_sock,
5663 .destroy = sctp_destroy_sock,
5664 .shutdown = sctp_shutdown,
5665 .setsockopt = sctp_setsockopt,
5666 .getsockopt = sctp_getsockopt,
5667 .sendmsg = sctp_sendmsg,
5668 .recvmsg = sctp_recvmsg,
5670 .backlog_rcv = sctp_backlog_rcv,
5672 .unhash = sctp_unhash,
5673 .get_port = sctp_get_port,
5674 .obj_size = sizeof(struct sctp6_sock),
5676 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */