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/types.h>
61 #include <linux/kernel.h>
62 #include <linux/wait.h>
63 #include <linux/time.h>
65 #include <linux/capability.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
73 #include <net/route.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock
*sk
);
89 static void sctp_wfree(struct sk_buff
*skb
);
90 static int sctp_wait_for_sndbuf(struct sctp_association
*, long *timeo_p
,
92 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
);
93 static int sctp_wait_for_connect(struct sctp_association
*, long *timeo_p
);
94 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
);
95 static void sctp_wait_for_close(struct sock
*sk
, long timeo
);
96 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
97 union sctp_addr
*addr
, int len
);
98 static int sctp_bindx_add(struct sock
*, struct sockaddr
*, int);
99 static int sctp_bindx_rem(struct sock
*, struct sockaddr
*, int);
100 static int sctp_send_asconf_add_ip(struct sock
*, struct sockaddr
*, int);
101 static int sctp_send_asconf_del_ip(struct sock
*, struct sockaddr
*, int);
102 static int sctp_send_asconf(struct sctp_association
*asoc
,
103 struct sctp_chunk
*chunk
);
104 static int sctp_do_bind(struct sock
*, union sctp_addr
*, int);
105 static int sctp_autobind(struct sock
*sk
);
106 static void sctp_sock_migrate(struct sock
*, struct sock
*,
107 struct sctp_association
*, sctp_socket_type_t
);
108 static char *sctp_hmac_alg
= SCTP_COOKIE_HMAC_ALG
;
110 extern struct kmem_cache
*sctp_bucket_cachep
;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association
*asoc
)
115 struct sock
*sk
= asoc
->base
.sk
;
118 if (asoc
->ep
->sndbuf_policy
) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt
= sk
->sk_sndbuf
- asoc
->sndbuf_used
;
122 /* do socket level accounting */
123 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
141 static inline void sctp_set_owner_w(struct sctp_chunk
*chunk
)
143 struct sctp_association
*asoc
= chunk
->asoc
;
144 struct sock
*sk
= asoc
->base
.sk
;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc
);
149 skb_set_owner_w(chunk
->skb
, sk
);
151 chunk
->skb
->destructor
= sctp_wfree
;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk
**)(chunk
->skb
->cb
)) = chunk
;
155 asoc
->sndbuf_used
+= SCTP_DATA_SNDSIZE(chunk
) +
156 sizeof(struct sk_buff
) +
157 sizeof(struct sctp_chunk
);
159 atomic_add(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
162 /* Verify that this is a valid address. */
163 static inline int sctp_verify_addr(struct sock
*sk
, union sctp_addr
*addr
,
168 /* Verify basic sockaddr. */
169 af
= sctp_sockaddr_af(sctp_sk(sk
), addr
, len
);
173 /* Is this a valid SCTP address? */
174 if (!af
->addr_valid(addr
, sctp_sk(sk
), NULL
))
177 if (!sctp_sk(sk
)->pf
->send_verify(sctp_sk(sk
), (addr
)))
183 /* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
186 struct sctp_association
*sctp_id2assoc(struct sock
*sk
, sctp_assoc_t id
)
188 struct sctp_association
*asoc
= NULL
;
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk
, UDP
)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
196 if (!sctp_sstate(sk
, ESTABLISHED
))
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk
)->ep
->asocs
))
201 asoc
= list_entry(sctp_sk(sk
)->ep
->asocs
.next
,
202 struct sctp_association
, asocs
);
206 /* Otherwise this is a UDP-style socket. */
207 if (!id
|| (id
== (sctp_assoc_t
)-1))
210 spin_lock_bh(&sctp_assocs_id_lock
);
211 asoc
= (struct sctp_association
*)idr_find(&sctp_assocs_id
, (int)id
);
212 spin_unlock_bh(&sctp_assocs_id_lock
);
214 if (!asoc
|| (asoc
->base
.sk
!= sk
) || asoc
->base
.dead
)
220 /* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
224 static struct sctp_transport
*sctp_addr_id2transport(struct sock
*sk
,
225 struct sockaddr_storage
*addr
,
228 struct sctp_association
*addr_asoc
= NULL
, *id_asoc
= NULL
;
229 struct sctp_transport
*transport
;
230 union sctp_addr
*laddr
= (union sctp_addr
*)addr
;
232 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
239 id_asoc
= sctp_id2assoc(sk
, id
);
240 if (id_asoc
&& (id_asoc
!= addr_asoc
))
243 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
244 (union sctp_addr
*)addr
);
249 /* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
259 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
270 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
275 sctp_release_sock(sk
);
280 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
282 /* Verify this is a valid sockaddr. */
283 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
284 union sctp_addr
*addr
, int len
)
288 /* Check minimum size. */
289 if (len
< sizeof (struct sockaddr
))
292 /* Does this PF support this AF? */
293 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
296 /* If we get this far, af is valid. */
297 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
299 if (len
< af
->sockaddr_len
)
305 /* Bind a local address either to an endpoint or to an association. */
306 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
308 struct sctp_sock
*sp
= sctp_sk(sk
);
309 struct sctp_endpoint
*ep
= sp
->ep
;
310 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
315 /* Common sockaddr verification. */
316 af
= sctp_sockaddr_af(sp
, addr
, len
);
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
323 snum
= ntohs(addr
->v4
.sin_port
);
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
332 /* PF specific bind() address verification. */
333 if (!sp
->pf
->bind_verify(sp
, addr
))
334 return -EADDRNOTAVAIL
;
336 /* We must either be unbound, or bind to the same port.
337 * It's OK to allow 0 ports if we are already bound.
338 * We'll just inhert an already bound port in this case
343 else if (snum
!= bp
->port
) {
344 SCTP_DEBUG_PRINTK("sctp_do_bind:"
345 " New port %d does not match existing port "
346 "%d.\n", snum
, bp
->port
);
351 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
354 /* Make sure we are allowed to bind here.
355 * The function sctp_get_port_local() does duplicate address
358 if ((ret
= sctp_get_port_local(sk
, addr
))) {
359 if (ret
== (long) sk
) {
360 /* This endpoint has a conflicting address. */
367 /* Refresh ephemeral port. */
369 bp
->port
= inet_sk(sk
)->num
;
371 /* Add the address to the bind address list. */
372 sctp_local_bh_disable();
373 sctp_write_lock(&ep
->base
.addr_lock
);
375 /* Use GFP_ATOMIC since BHs are disabled. */
376 ret
= sctp_add_bind_addr(bp
, addr
, 1, GFP_ATOMIC
);
377 sctp_write_unlock(&ep
->base
.addr_lock
);
378 sctp_local_bh_enable();
380 /* Copy back into socket for getsockname() use. */
382 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
383 af
->to_sk_saddr(addr
, sk
);
389 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
391 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
392 * at any one time. If a sender, after sending an ASCONF chunk, decides
393 * it needs to transfer another ASCONF Chunk, it MUST wait until the
394 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
395 * subsequent ASCONF. Note this restriction binds each side, so at any
396 * time two ASCONF may be in-transit on any given association (one sent
397 * from each endpoint).
399 static int sctp_send_asconf(struct sctp_association
*asoc
,
400 struct sctp_chunk
*chunk
)
404 /* If there is an outstanding ASCONF chunk, queue it for later
407 if (asoc
->addip_last_asconf
) {
408 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
412 /* Hold the chunk until an ASCONF_ACK is received. */
413 sctp_chunk_hold(chunk
);
414 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
416 sctp_chunk_free(chunk
);
418 asoc
->addip_last_asconf
= chunk
;
424 /* Add a list of addresses as bind addresses to local endpoint or
427 * Basically run through each address specified in the addrs/addrcnt
428 * array/length pair, determine if it is IPv6 or IPv4 and call
429 * sctp_do_bind() on it.
431 * If any of them fails, then the operation will be reversed and the
432 * ones that were added will be removed.
434 * Only sctp_setsockopt_bindx() is supposed to call this function.
436 int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
441 struct sockaddr
*sa_addr
;
444 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
448 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
449 /* The list may contain either IPv4 or IPv6 address;
450 * determine the address length for walking thru the list.
452 sa_addr
= (struct sockaddr
*)addr_buf
;
453 af
= sctp_get_af_specific(sa_addr
->sa_family
);
459 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
462 addr_buf
+= af
->sockaddr_len
;
466 /* Failed. Cleanup the ones that have been added */
468 sctp_bindx_rem(sk
, addrs
, cnt
);
476 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
477 * associations that are part of the endpoint indicating that a list of local
478 * addresses are added to the endpoint.
480 * If any of the addresses is already in the bind address list of the
481 * association, we do not send the chunk for that association. But it will not
482 * affect other associations.
484 * Only sctp_setsockopt_bindx() is supposed to call this function.
486 static int sctp_send_asconf_add_ip(struct sock
*sk
,
487 struct sockaddr
*addrs
,
490 struct sctp_sock
*sp
;
491 struct sctp_endpoint
*ep
;
492 struct sctp_association
*asoc
;
493 struct sctp_bind_addr
*bp
;
494 struct sctp_chunk
*chunk
;
495 struct sctp_sockaddr_entry
*laddr
;
496 union sctp_addr
*addr
;
497 union sctp_addr saveaddr
;
500 struct list_head
*pos
;
505 if (!sctp_addip_enable
)
511 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
512 __FUNCTION__
, sk
, addrs
, addrcnt
);
514 list_for_each(pos
, &ep
->asocs
) {
515 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
517 if (!asoc
->peer
.asconf_capable
)
520 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
523 if (!sctp_state(asoc
, ESTABLISHED
))
526 /* Check if any address in the packed array of addresses is
527 * in the bind address list of the association. If so,
528 * do not send the asconf chunk to its peer, but continue with
529 * other associations.
532 for (i
= 0; i
< addrcnt
; i
++) {
533 addr
= (union sctp_addr
*)addr_buf
;
534 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
540 if (sctp_assoc_lookup_laddr(asoc
, addr
))
543 addr_buf
+= af
->sockaddr_len
;
548 /* Use the first address in bind addr list of association as
549 * Address Parameter of ASCONF CHUNK.
551 sctp_read_lock(&asoc
->base
.addr_lock
);
552 bp
= &asoc
->base
.bind_addr
;
553 p
= bp
->address_list
.next
;
554 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
555 sctp_read_unlock(&asoc
->base
.addr_lock
);
557 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
558 addrcnt
, SCTP_PARAM_ADD_IP
);
564 retval
= sctp_send_asconf(asoc
, chunk
);
568 /* Add the new addresses to the bind address list with
569 * use_as_src set to 0.
571 sctp_local_bh_disable();
572 sctp_write_lock(&asoc
->base
.addr_lock
);
574 for (i
= 0; i
< addrcnt
; i
++) {
575 addr
= (union sctp_addr
*)addr_buf
;
576 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
577 memcpy(&saveaddr
, addr
, af
->sockaddr_len
);
578 retval
= sctp_add_bind_addr(bp
, &saveaddr
, 0,
580 addr_buf
+= af
->sockaddr_len
;
582 sctp_write_unlock(&asoc
->base
.addr_lock
);
583 sctp_local_bh_enable();
590 /* Remove a list of addresses from bind addresses list. Do not remove the
593 * Basically run through each address specified in the addrs/addrcnt
594 * array/length pair, determine if it is IPv6 or IPv4 and call
595 * sctp_del_bind() on it.
597 * If any of them fails, then the operation will be reversed and the
598 * ones that were removed will be added back.
600 * At least one address has to be left; if only one address is
601 * available, the operation will return -EBUSY.
603 * Only sctp_setsockopt_bindx() is supposed to call this function.
605 int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
607 struct sctp_sock
*sp
= sctp_sk(sk
);
608 struct sctp_endpoint
*ep
= sp
->ep
;
610 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
613 union sctp_addr
*sa_addr
;
616 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
620 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
621 /* If the bind address list is empty or if there is only one
622 * bind address, there is nothing more to be removed (we need
623 * at least one address here).
625 if (list_empty(&bp
->address_list
) ||
626 (sctp_list_single_entry(&bp
->address_list
))) {
631 sa_addr
= (union sctp_addr
*)addr_buf
;
632 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
638 if (!af
->addr_valid(sa_addr
, sp
, NULL
)) {
639 retval
= -EADDRNOTAVAIL
;
643 if (sa_addr
->v4
.sin_port
!= htons(bp
->port
)) {
648 /* FIXME - There is probably a need to check if sk->sk_saddr and
649 * sk->sk_rcv_addr are currently set to one of the addresses to
650 * be removed. This is something which needs to be looked into
651 * when we are fixing the outstanding issues with multi-homing
652 * socket routing and failover schemes. Refer to comments in
653 * sctp_do_bind(). -daisy
655 sctp_local_bh_disable();
656 sctp_write_lock(&ep
->base
.addr_lock
);
658 retval
= sctp_del_bind_addr(bp
, sa_addr
);
660 sctp_write_unlock(&ep
->base
.addr_lock
);
661 sctp_local_bh_enable();
663 addr_buf
+= af
->sockaddr_len
;
666 /* Failed. Add the ones that has been removed back */
668 sctp_bindx_add(sk
, addrs
, cnt
);
676 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
677 * the associations that are part of the endpoint indicating that a list of
678 * local addresses are removed from the endpoint.
680 * If any of the addresses is already in the bind address list of the
681 * association, we do not send the chunk for that association. But it will not
682 * affect other associations.
684 * Only sctp_setsockopt_bindx() is supposed to call this function.
686 static int sctp_send_asconf_del_ip(struct sock
*sk
,
687 struct sockaddr
*addrs
,
690 struct sctp_sock
*sp
;
691 struct sctp_endpoint
*ep
;
692 struct sctp_association
*asoc
;
693 struct sctp_transport
*transport
;
694 struct sctp_bind_addr
*bp
;
695 struct sctp_chunk
*chunk
;
696 union sctp_addr
*laddr
;
699 struct list_head
*pos
, *pos1
;
700 struct sctp_sockaddr_entry
*saddr
;
704 if (!sctp_addip_enable
)
710 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
711 __FUNCTION__
, sk
, addrs
, addrcnt
);
713 list_for_each(pos
, &ep
->asocs
) {
714 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
716 if (!asoc
->peer
.asconf_capable
)
719 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
722 if (!sctp_state(asoc
, ESTABLISHED
))
725 /* Check if any address in the packed array of addresses is
726 * not present in the bind address list of the association.
727 * If so, do not send the asconf chunk to its peer, but
728 * continue with other associations.
731 for (i
= 0; i
< addrcnt
; i
++) {
732 laddr
= (union sctp_addr
*)addr_buf
;
733 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
739 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
742 addr_buf
+= af
->sockaddr_len
;
747 /* Find one address in the association's bind address list
748 * that is not in the packed array of addresses. This is to
749 * make sure that we do not delete all the addresses in the
752 sctp_read_lock(&asoc
->base
.addr_lock
);
753 bp
= &asoc
->base
.bind_addr
;
754 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
756 sctp_read_unlock(&asoc
->base
.addr_lock
);
760 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
767 /* Reset use_as_src flag for the addresses in the bind address
768 * list that are to be deleted.
770 sctp_local_bh_disable();
771 sctp_write_lock(&asoc
->base
.addr_lock
);
773 for (i
= 0; i
< addrcnt
; i
++) {
774 laddr
= (union sctp_addr
*)addr_buf
;
775 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
776 list_for_each(pos1
, &bp
->address_list
) {
777 saddr
= list_entry(pos1
,
778 struct sctp_sockaddr_entry
,
780 if (sctp_cmp_addr_exact(&saddr
->a
, laddr
))
781 saddr
->use_as_src
= 0;
783 addr_buf
+= af
->sockaddr_len
;
785 sctp_write_unlock(&asoc
->base
.addr_lock
);
786 sctp_local_bh_enable();
788 /* Update the route and saddr entries for all the transports
789 * as some of the addresses in the bind address list are
790 * about to be deleted and cannot be used as source addresses.
792 list_for_each(pos1
, &asoc
->peer
.transport_addr_list
) {
793 transport
= list_entry(pos1
, struct sctp_transport
,
795 dst_release(transport
->dst
);
796 sctp_transport_route(transport
, NULL
,
797 sctp_sk(asoc
->base
.sk
));
800 retval
= sctp_send_asconf(asoc
, chunk
);
806 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
809 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
812 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
813 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
816 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
817 * Section 3.1.2 for this usage.
819 * addrs is a pointer to an array of one or more socket addresses. Each
820 * address is contained in its appropriate structure (i.e. struct
821 * sockaddr_in or struct sockaddr_in6) the family of the address type
822 * must be used to distinguish the address length (note that this
823 * representation is termed a "packed array" of addresses). The caller
824 * specifies the number of addresses in the array with addrcnt.
826 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
827 * -1, and sets errno to the appropriate error code.
829 * For SCTP, the port given in each socket address must be the same, or
830 * sctp_bindx() will fail, setting errno to EINVAL.
832 * The flags parameter is formed from the bitwise OR of zero or more of
833 * the following currently defined flags:
835 * SCTP_BINDX_ADD_ADDR
837 * SCTP_BINDX_REM_ADDR
839 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
840 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
841 * addresses from the association. The two flags are mutually exclusive;
842 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
843 * not remove all addresses from an association; sctp_bindx() will
844 * reject such an attempt with EINVAL.
846 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
847 * additional addresses with an endpoint after calling bind(). Or use
848 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
849 * socket is associated with so that no new association accepted will be
850 * associated with those addresses. If the endpoint supports dynamic
851 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
852 * endpoint to send the appropriate message to the peer to change the
853 * peers address lists.
855 * Adding and removing addresses from a connected association is
856 * optional functionality. Implementations that do not support this
857 * functionality should return EOPNOTSUPP.
859 * Basically do nothing but copying the addresses from user to kernel
860 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
861 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
864 * We don't use copy_from_user() for optimization: we first do the
865 * sanity checks (buffer size -fast- and access check-healthy
866 * pointer); if all of those succeed, then we can alloc the memory
867 * (expensive operation) needed to copy the data to kernel. Then we do
868 * the copying without checking the user space area
869 * (__copy_from_user()).
871 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
874 * sk The sk of the socket
875 * addrs The pointer to the addresses in user land
876 * addrssize Size of the addrs buffer
877 * op Operation to perform (add or remove, see the flags of
880 * Returns 0 if ok, <0 errno code on error.
882 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
883 struct sockaddr __user
*addrs
,
884 int addrs_size
, int op
)
886 struct sockaddr
*kaddrs
;
890 struct sockaddr
*sa_addr
;
894 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
895 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
897 if (unlikely(addrs_size
<= 0))
900 /* Check the user passed a healthy pointer. */
901 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
904 /* Alloc space for the address array in kernel memory. */
905 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
906 if (unlikely(!kaddrs
))
909 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
914 /* Walk through the addrs buffer and count the number of addresses. */
916 while (walk_size
< addrs_size
) {
917 sa_addr
= (struct sockaddr
*)addr_buf
;
918 af
= sctp_get_af_specific(sa_addr
->sa_family
);
920 /* If the address family is not supported or if this address
921 * causes the address buffer to overflow return EINVAL.
923 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
928 addr_buf
+= af
->sockaddr_len
;
929 walk_size
+= af
->sockaddr_len
;
934 case SCTP_BINDX_ADD_ADDR
:
935 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
938 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
941 case SCTP_BINDX_REM_ADDR
:
942 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
945 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
959 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
961 * Common routine for handling connect() and sctp_connectx().
962 * Connect will come in with just a single address.
964 static int __sctp_connect(struct sock
* sk
,
965 struct sockaddr
*kaddrs
,
968 struct sctp_sock
*sp
;
969 struct sctp_endpoint
*ep
;
970 struct sctp_association
*asoc
= NULL
;
971 struct sctp_association
*asoc2
;
972 struct sctp_transport
*transport
;
980 union sctp_addr
*sa_addr
;
987 /* connect() cannot be done on a socket that is already in ESTABLISHED
988 * state - UDP-style peeled off socket or a TCP-style socket that
989 * is already connected.
990 * It cannot be done even on a TCP-style listening socket.
992 if (sctp_sstate(sk
, ESTABLISHED
) ||
993 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
998 /* Walk through the addrs buffer and count the number of addresses. */
1000 while (walk_size
< addrs_size
) {
1001 sa_addr
= (union sctp_addr
*)addr_buf
;
1002 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
1003 port
= ntohs(sa_addr
->v4
.sin_port
);
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
1008 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
1013 err
= sctp_verify_addr(sk
, sa_addr
, af
->sockaddr_len
);
1017 /* Make sure the destination port is correctly set
1020 if (asoc
&& asoc
->peer
.port
&& asoc
->peer
.port
!= port
)
1023 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
1025 /* Check if there already is a matching association on the
1026 * endpoint (other than the one created here).
1028 asoc2
= sctp_endpoint_lookup_assoc(ep
, sa_addr
, &transport
);
1029 if (asoc2
&& asoc2
!= asoc
) {
1030 if (asoc2
->state
>= SCTP_STATE_ESTABLISHED
)
1037 /* If we could not find a matching association on the endpoint,
1038 * make sure that there is no peeled-off association matching
1039 * the peer address even on another socket.
1041 if (sctp_endpoint_is_peeled_off(ep
, sa_addr
)) {
1042 err
= -EADDRNOTAVAIL
;
1047 /* If a bind() or sctp_bindx() is not called prior to
1048 * an sctp_connectx() call, the system picks an
1049 * ephemeral port and will choose an address set
1050 * equivalent to binding with a wildcard address.
1052 if (!ep
->base
.bind_addr
.port
) {
1053 if (sctp_autobind(sk
)) {
1059 * If an unprivileged user inherits a 1-many
1060 * style socket with open associations on a
1061 * privileged port, it MAY be permitted to
1062 * accept new associations, but it SHOULD NOT
1063 * be permitted to open new associations.
1065 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1066 !capable(CAP_NET_BIND_SERVICE
)) {
1072 scope
= sctp_scope(sa_addr
);
1073 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1080 /* Prime the peer's transport structures. */
1081 transport
= sctp_assoc_add_peer(asoc
, sa_addr
, GFP_KERNEL
,
1089 addr_buf
+= af
->sockaddr_len
;
1090 walk_size
+= af
->sockaddr_len
;
1093 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1098 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1103 /* Initialize sk's dport and daddr for getpeername() */
1104 inet_sk(sk
)->dport
= htons(asoc
->peer
.port
);
1105 af
= sctp_get_af_specific(to
.sa
.sa_family
);
1106 af
->to_sk_daddr(&to
, sk
);
1109 timeo
= sock_sndtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
1110 err
= sctp_wait_for_connect(asoc
, &timeo
);
1112 /* Don't free association on exit. */
1117 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1118 " kaddrs: %p err: %d\n",
1121 sctp_association_free(asoc
);
1125 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1128 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1130 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1131 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1132 * or IPv6 addresses.
1134 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1135 * Section 3.1.2 for this usage.
1137 * addrs is a pointer to an array of one or more socket addresses. Each
1138 * address is contained in its appropriate structure (i.e. struct
1139 * sockaddr_in or struct sockaddr_in6) the family of the address type
1140 * must be used to distengish the address length (note that this
1141 * representation is termed a "packed array" of addresses). The caller
1142 * specifies the number of addresses in the array with addrcnt.
1144 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1145 * -1, and sets errno to the appropriate error code.
1147 * For SCTP, the port given in each socket address must be the same, or
1148 * sctp_connectx() will fail, setting errno to EINVAL.
1150 * An application can use sctp_connectx to initiate an association with
1151 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1152 * allows a caller to specify multiple addresses at which a peer can be
1153 * reached. The way the SCTP stack uses the list of addresses to set up
1154 * the association is implementation dependant. This function only
1155 * specifies that the stack will try to make use of all the addresses in
1156 * the list when needed.
1158 * Note that the list of addresses passed in is only used for setting up
1159 * the association. It does not necessarily equal the set of addresses
1160 * the peer uses for the resulting association. If the caller wants to
1161 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1162 * retrieve them after the association has been set up.
1164 * Basically do nothing but copying the addresses from user to kernel
1165 * land and invoking either sctp_connectx(). This is used for tunneling
1166 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1168 * We don't use copy_from_user() for optimization: we first do the
1169 * sanity checks (buffer size -fast- and access check-healthy
1170 * pointer); if all of those succeed, then we can alloc the memory
1171 * (expensive operation) needed to copy the data to kernel. Then we do
1172 * the copying without checking the user space area
1173 * (__copy_from_user()).
1175 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1178 * sk The sk of the socket
1179 * addrs The pointer to the addresses in user land
1180 * addrssize Size of the addrs buffer
1182 * Returns 0 if ok, <0 errno code on error.
1184 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1185 struct sockaddr __user
*addrs
,
1189 struct sockaddr
*kaddrs
;
1191 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1192 __FUNCTION__
, sk
, addrs
, addrs_size
);
1194 if (unlikely(addrs_size
<= 0))
1197 /* Check the user passed a healthy pointer. */
1198 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1201 /* Alloc space for the address array in kernel memory. */
1202 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1203 if (unlikely(!kaddrs
))
1206 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1209 err
= __sctp_connect(sk
, kaddrs
, addrs_size
);
1216 /* API 3.1.4 close() - UDP Style Syntax
1217 * Applications use close() to perform graceful shutdown (as described in
1218 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1219 * by a UDP-style socket.
1223 * ret = close(int sd);
1225 * sd - the socket descriptor of the associations to be closed.
1227 * To gracefully shutdown a specific association represented by the
1228 * UDP-style socket, an application should use the sendmsg() call,
1229 * passing no user data, but including the appropriate flag in the
1230 * ancillary data (see Section xxxx).
1232 * If sd in the close() call is a branched-off socket representing only
1233 * one association, the shutdown is performed on that association only.
1235 * 4.1.6 close() - TCP Style Syntax
1237 * Applications use close() to gracefully close down an association.
1241 * int close(int sd);
1243 * sd - the socket descriptor of the association to be closed.
1245 * After an application calls close() on a socket descriptor, no further
1246 * socket operations will succeed on that descriptor.
1248 * API 7.1.4 SO_LINGER
1250 * An application using the TCP-style socket can use this option to
1251 * perform the SCTP ABORT primitive. The linger option structure is:
1254 * int l_onoff; // option on/off
1255 * int l_linger; // linger time
1258 * To enable the option, set l_onoff to 1. If the l_linger value is set
1259 * to 0, calling close() is the same as the ABORT primitive. If the
1260 * value is set to a negative value, the setsockopt() call will return
1261 * an error. If the value is set to a positive value linger_time, the
1262 * close() can be blocked for at most linger_time ms. If the graceful
1263 * shutdown phase does not finish during this period, close() will
1264 * return but the graceful shutdown phase continues in the system.
1266 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1268 struct sctp_endpoint
*ep
;
1269 struct sctp_association
*asoc
;
1270 struct list_head
*pos
, *temp
;
1272 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1275 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1277 ep
= sctp_sk(sk
)->ep
;
1279 /* Walk all associations on an endpoint. */
1280 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1281 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1283 if (sctp_style(sk
, TCP
)) {
1284 /* A closed association can still be in the list if
1285 * it belongs to a TCP-style listening socket that is
1286 * not yet accepted. If so, free it. If not, send an
1287 * ABORT or SHUTDOWN based on the linger options.
1289 if (sctp_state(asoc
, CLOSED
)) {
1290 sctp_unhash_established(asoc
);
1291 sctp_association_free(asoc
);
1296 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
) {
1297 struct sctp_chunk
*chunk
;
1299 chunk
= sctp_make_abort_user(asoc
, NULL
, 0);
1301 sctp_primitive_ABORT(asoc
, chunk
);
1303 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1306 /* Clean up any skbs sitting on the receive queue. */
1307 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1308 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1310 /* On a TCP-style socket, block for at most linger_time if set. */
1311 if (sctp_style(sk
, TCP
) && timeout
)
1312 sctp_wait_for_close(sk
, timeout
);
1314 /* This will run the backlog queue. */
1315 sctp_release_sock(sk
);
1317 /* Supposedly, no process has access to the socket, but
1318 * the net layers still may.
1320 sctp_local_bh_disable();
1321 sctp_bh_lock_sock(sk
);
1323 /* Hold the sock, since sk_common_release() will put sock_put()
1324 * and we have just a little more cleanup.
1327 sk_common_release(sk
);
1329 sctp_bh_unlock_sock(sk
);
1330 sctp_local_bh_enable();
1334 SCTP_DBG_OBJCNT_DEC(sock
);
1337 /* Handle EPIPE error. */
1338 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1341 err
= sock_error(sk
) ? : -EPIPE
;
1342 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1343 send_sig(SIGPIPE
, current
, 0);
1347 /* API 3.1.3 sendmsg() - UDP Style Syntax
1349 * An application uses sendmsg() and recvmsg() calls to transmit data to
1350 * and receive data from its peer.
1352 * ssize_t sendmsg(int socket, const struct msghdr *message,
1355 * socket - the socket descriptor of the endpoint.
1356 * message - pointer to the msghdr structure which contains a single
1357 * user message and possibly some ancillary data.
1359 * See Section 5 for complete description of the data
1362 * flags - flags sent or received with the user message, see Section
1363 * 5 for complete description of the flags.
1365 * Note: This function could use a rewrite especially when explicit
1366 * connect support comes in.
1368 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1370 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1372 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1373 struct msghdr
*msg
, size_t msg_len
)
1375 struct sctp_sock
*sp
;
1376 struct sctp_endpoint
*ep
;
1377 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1378 struct sctp_transport
*transport
, *chunk_tp
;
1379 struct sctp_chunk
*chunk
;
1381 struct sockaddr
*msg_name
= NULL
;
1382 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1383 struct sctp_sndrcvinfo
*sinfo
;
1384 struct sctp_initmsg
*sinit
;
1385 sctp_assoc_t associd
= 0;
1386 sctp_cmsgs_t cmsgs
= { NULL
};
1390 __u16 sinfo_flags
= 0;
1391 struct sctp_datamsg
*datamsg
;
1392 struct list_head
*pos
;
1393 int msg_flags
= msg
->msg_flags
;
1395 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1402 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1404 /* We cannot send a message over a TCP-style listening socket. */
1405 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1410 /* Parse out the SCTP CMSGs. */
1411 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1414 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1418 /* Fetch the destination address for this packet. This
1419 * address only selects the association--it is not necessarily
1420 * the address we will send to.
1421 * For a peeled-off socket, msg_name is ignored.
1423 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1424 int msg_namelen
= msg
->msg_namelen
;
1426 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1431 if (msg_namelen
> sizeof(to
))
1432 msg_namelen
= sizeof(to
);
1433 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1434 msg_name
= msg
->msg_name
;
1440 /* Did the user specify SNDRCVINFO? */
1442 sinfo_flags
= sinfo
->sinfo_flags
;
1443 associd
= sinfo
->sinfo_assoc_id
;
1446 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1447 msg_len
, sinfo_flags
);
1449 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1450 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1455 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1456 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1457 * If SCTP_ABORT is set, the message length could be non zero with
1458 * the msg_iov set to the user abort reason.
1460 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1461 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1466 /* If SCTP_ADDR_OVER is set, there must be an address
1467 * specified in msg_name.
1469 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1476 SCTP_DEBUG_PRINTK("About to look up association.\n");
1480 /* If a msg_name has been specified, assume this is to be used. */
1482 /* Look for a matching association on the endpoint. */
1483 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1485 /* If we could not find a matching association on the
1486 * endpoint, make sure that it is not a TCP-style
1487 * socket that already has an association or there is
1488 * no peeled-off association on another socket.
1490 if ((sctp_style(sk
, TCP
) &&
1491 sctp_sstate(sk
, ESTABLISHED
)) ||
1492 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1493 err
= -EADDRNOTAVAIL
;
1498 asoc
= sctp_id2assoc(sk
, associd
);
1506 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1508 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1509 * socket that has an association in CLOSED state. This can
1510 * happen when an accepted socket has an association that is
1513 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1518 if (sinfo_flags
& SCTP_EOF
) {
1519 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1521 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1525 if (sinfo_flags
& SCTP_ABORT
) {
1526 struct sctp_chunk
*chunk
;
1528 chunk
= sctp_make_abort_user(asoc
, msg
, msg_len
);
1534 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1535 sctp_primitive_ABORT(asoc
, chunk
);
1541 /* Do we need to create the association? */
1543 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1545 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1550 /* Check for invalid stream against the stream counts,
1551 * either the default or the user specified stream counts.
1554 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1555 /* Check against the defaults. */
1556 if (sinfo
->sinfo_stream
>=
1557 sp
->initmsg
.sinit_num_ostreams
) {
1562 /* Check against the requested. */
1563 if (sinfo
->sinfo_stream
>=
1564 sinit
->sinit_num_ostreams
) {
1572 * API 3.1.2 bind() - UDP Style Syntax
1573 * If a bind() or sctp_bindx() is not called prior to a
1574 * sendmsg() call that initiates a new association, the
1575 * system picks an ephemeral port and will choose an address
1576 * set equivalent to binding with a wildcard address.
1578 if (!ep
->base
.bind_addr
.port
) {
1579 if (sctp_autobind(sk
)) {
1585 * If an unprivileged user inherits a one-to-many
1586 * style socket with open associations on a privileged
1587 * port, it MAY be permitted to accept new associations,
1588 * but it SHOULD NOT be permitted to open new
1591 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1592 !capable(CAP_NET_BIND_SERVICE
)) {
1598 scope
= sctp_scope(&to
);
1599 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1606 /* If the SCTP_INIT ancillary data is specified, set all
1607 * the association init values accordingly.
1610 if (sinit
->sinit_num_ostreams
) {
1611 asoc
->c
.sinit_num_ostreams
=
1612 sinit
->sinit_num_ostreams
;
1614 if (sinit
->sinit_max_instreams
) {
1615 asoc
->c
.sinit_max_instreams
=
1616 sinit
->sinit_max_instreams
;
1618 if (sinit
->sinit_max_attempts
) {
1619 asoc
->max_init_attempts
1620 = sinit
->sinit_max_attempts
;
1622 if (sinit
->sinit_max_init_timeo
) {
1623 asoc
->max_init_timeo
=
1624 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1628 /* Prime the peer's transport structures. */
1629 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1634 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1641 /* ASSERT: we have a valid association at this point. */
1642 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1645 /* If the user didn't specify SNDRCVINFO, make up one with
1648 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1649 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1650 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1651 default_sinfo
.sinfo_context
= asoc
->default_context
;
1652 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1653 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1654 sinfo
= &default_sinfo
;
1657 /* API 7.1.7, the sndbuf size per association bounds the
1658 * maximum size of data that can be sent in a single send call.
1660 if (msg_len
> sk
->sk_sndbuf
) {
1665 if (asoc
->pmtu_pending
)
1666 sctp_assoc_pending_pmtu(asoc
);
1668 /* If fragmentation is disabled and the message length exceeds the
1669 * association fragmentation point, return EMSGSIZE. The I-D
1670 * does not specify what this error is, but this looks like
1673 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1679 /* Check for invalid stream. */
1680 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1686 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1687 if (!sctp_wspace(asoc
)) {
1688 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1693 /* If an address is passed with the sendto/sendmsg call, it is used
1694 * to override the primary destination address in the TCP model, or
1695 * when SCTP_ADDR_OVER flag is set in the UDP model.
1697 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1698 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1699 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1707 /* Auto-connect, if we aren't connected already. */
1708 if (sctp_state(asoc
, CLOSED
)) {
1709 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1712 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1715 /* Break the message into multiple chunks of maximum size. */
1716 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1722 /* Now send the (possibly) fragmented message. */
1723 list_for_each(pos
, &datamsg
->chunks
) {
1724 chunk
= list_entry(pos
, struct sctp_chunk
, frag_list
);
1725 sctp_datamsg_track(chunk
);
1727 /* Do accounting for the write space. */
1728 sctp_set_owner_w(chunk
);
1730 chunk
->transport
= chunk_tp
;
1732 /* Send it to the lower layers. Note: all chunks
1733 * must either fail or succeed. The lower layer
1734 * works that way today. Keep it that way or this
1737 err
= sctp_primitive_SEND(asoc
, chunk
);
1738 /* Did the lower layer accept the chunk? */
1740 sctp_chunk_free(chunk
);
1741 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1744 sctp_datamsg_free(datamsg
);
1750 /* If we are already past ASSOCIATE, the lower
1751 * layers are responsible for association cleanup.
1757 sctp_association_free(asoc
);
1759 sctp_release_sock(sk
);
1762 return sctp_error(sk
, msg_flags
, err
);
1769 err
= sock_error(sk
);
1779 /* This is an extended version of skb_pull() that removes the data from the
1780 * start of a skb even when data is spread across the list of skb's in the
1781 * frag_list. len specifies the total amount of data that needs to be removed.
1782 * when 'len' bytes could be removed from the skb, it returns 0.
1783 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1784 * could not be removed.
1786 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1788 struct sk_buff
*list
;
1789 int skb_len
= skb_headlen(skb
);
1792 if (len
<= skb_len
) {
1793 __skb_pull(skb
, len
);
1797 __skb_pull(skb
, skb_len
);
1799 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
) {
1800 rlen
= sctp_skb_pull(list
, len
);
1801 skb
->len
-= (len
-rlen
);
1802 skb
->data_len
-= (len
-rlen
);
1813 /* API 3.1.3 recvmsg() - UDP Style Syntax
1815 * ssize_t recvmsg(int socket, struct msghdr *message,
1818 * socket - the socket descriptor of the endpoint.
1819 * message - pointer to the msghdr structure which contains a single
1820 * user message and possibly some ancillary data.
1822 * See Section 5 for complete description of the data
1825 * flags - flags sent or received with the user message, see Section
1826 * 5 for complete description of the flags.
1828 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1830 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1831 struct msghdr
*msg
, size_t len
, int noblock
,
1832 int flags
, int *addr_len
)
1834 struct sctp_ulpevent
*event
= NULL
;
1835 struct sctp_sock
*sp
= sctp_sk(sk
);
1836 struct sk_buff
*skb
;
1841 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1842 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1843 "len", len
, "knoblauch", noblock
,
1844 "flags", flags
, "addr_len", addr_len
);
1848 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1853 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1857 /* Get the total length of the skb including any skb's in the
1866 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1868 event
= sctp_skb2event(skb
);
1873 sock_recv_timestamp(msg
, sk
, skb
);
1874 if (sctp_ulpevent_is_notification(event
)) {
1875 msg
->msg_flags
|= MSG_NOTIFICATION
;
1876 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1878 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1881 /* Check if we allow SCTP_SNDRCVINFO. */
1882 if (sp
->subscribe
.sctp_data_io_event
)
1883 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1885 /* FIXME: we should be calling IP/IPv6 layers. */
1886 if (sk
->sk_protinfo
.af_inet
.cmsg_flags
)
1887 ip_cmsg_recv(msg
, skb
);
1892 /* If skb's length exceeds the user's buffer, update the skb and
1893 * push it back to the receive_queue so that the next call to
1894 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1896 if (skb_len
> copied
) {
1897 msg
->msg_flags
&= ~MSG_EOR
;
1898 if (flags
& MSG_PEEK
)
1900 sctp_skb_pull(skb
, copied
);
1901 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1903 /* When only partial message is copied to the user, increase
1904 * rwnd by that amount. If all the data in the skb is read,
1905 * rwnd is updated when the event is freed.
1907 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1909 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1910 (event
->msg_flags
& MSG_EOR
))
1911 msg
->msg_flags
|= MSG_EOR
;
1913 msg
->msg_flags
&= ~MSG_EOR
;
1916 if (flags
& MSG_PEEK
) {
1917 /* Release the skb reference acquired after peeking the skb in
1918 * sctp_skb_recv_datagram().
1922 /* Free the event which includes releasing the reference to
1923 * the owner of the skb, freeing the skb and updating the
1926 sctp_ulpevent_free(event
);
1929 sctp_release_sock(sk
);
1933 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1935 * This option is a on/off flag. If enabled no SCTP message
1936 * fragmentation will be performed. Instead if a message being sent
1937 * exceeds the current PMTU size, the message will NOT be sent and
1938 * instead a error will be indicated to the user.
1940 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1941 char __user
*optval
, int optlen
)
1945 if (optlen
< sizeof(int))
1948 if (get_user(val
, (int __user
*)optval
))
1951 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1956 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1959 if (optlen
!= sizeof(struct sctp_event_subscribe
))
1961 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
1966 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1968 * This socket option is applicable to the UDP-style socket only. When
1969 * set it will cause associations that are idle for more than the
1970 * specified number of seconds to automatically close. An association
1971 * being idle is defined an association that has NOT sent or received
1972 * user data. The special value of '0' indicates that no automatic
1973 * close of any associations should be performed. The option expects an
1974 * integer defining the number of seconds of idle time before an
1975 * association is closed.
1977 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
1980 struct sctp_sock
*sp
= sctp_sk(sk
);
1982 /* Applicable to UDP-style socket only */
1983 if (sctp_style(sk
, TCP
))
1985 if (optlen
!= sizeof(int))
1987 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
1993 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1995 * Applications can enable or disable heartbeats for any peer address of
1996 * an association, modify an address's heartbeat interval, force a
1997 * heartbeat to be sent immediately, and adjust the address's maximum
1998 * number of retransmissions sent before an address is considered
1999 * unreachable. The following structure is used to access and modify an
2000 * address's parameters:
2002 * struct sctp_paddrparams {
2003 * sctp_assoc_t spp_assoc_id;
2004 * struct sockaddr_storage spp_address;
2005 * uint32_t spp_hbinterval;
2006 * uint16_t spp_pathmaxrxt;
2007 * uint32_t spp_pathmtu;
2008 * uint32_t spp_sackdelay;
2009 * uint32_t spp_flags;
2012 * spp_assoc_id - (one-to-many style socket) This is filled in the
2013 * application, and identifies the association for
2015 * spp_address - This specifies which address is of interest.
2016 * spp_hbinterval - This contains the value of the heartbeat interval,
2017 * in milliseconds. If a value of zero
2018 * is present in this field then no changes are to
2019 * be made to this parameter.
2020 * spp_pathmaxrxt - This contains the maximum number of
2021 * retransmissions before this address shall be
2022 * considered unreachable. If a value of zero
2023 * is present in this field then no changes are to
2024 * be made to this parameter.
2025 * spp_pathmtu - When Path MTU discovery is disabled the value
2026 * specified here will be the "fixed" path mtu.
2027 * Note that if the spp_address field is empty
2028 * then all associations on this address will
2029 * have this fixed path mtu set upon them.
2031 * spp_sackdelay - When delayed sack is enabled, this value specifies
2032 * the number of milliseconds that sacks will be delayed
2033 * for. This value will apply to all addresses of an
2034 * association if the spp_address field is empty. Note
2035 * also, that if delayed sack is enabled and this
2036 * value is set to 0, no change is made to the last
2037 * recorded delayed sack timer value.
2039 * spp_flags - These flags are used to control various features
2040 * on an association. The flag field may contain
2041 * zero or more of the following options.
2043 * SPP_HB_ENABLE - Enable heartbeats on the
2044 * specified address. Note that if the address
2045 * field is empty all addresses for the association
2046 * have heartbeats enabled upon them.
2048 * SPP_HB_DISABLE - Disable heartbeats on the
2049 * speicifed address. Note that if the address
2050 * field is empty all addresses for the association
2051 * will have their heartbeats disabled. Note also
2052 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2053 * mutually exclusive, only one of these two should
2054 * be specified. Enabling both fields will have
2055 * undetermined results.
2057 * SPP_HB_DEMAND - Request a user initiated heartbeat
2058 * to be made immediately.
2060 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2061 * heartbeat delayis to be set to the value of 0
2064 * SPP_PMTUD_ENABLE - This field will enable PMTU
2065 * discovery upon the specified address. Note that
2066 * if the address feild is empty then all addresses
2067 * on the association are effected.
2069 * SPP_PMTUD_DISABLE - This field will disable PMTU
2070 * discovery upon the specified address. Note that
2071 * if the address feild is empty then all addresses
2072 * on the association are effected. Not also that
2073 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2074 * exclusive. Enabling both will have undetermined
2077 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2078 * on delayed sack. The time specified in spp_sackdelay
2079 * is used to specify the sack delay for this address. Note
2080 * that if spp_address is empty then all addresses will
2081 * enable delayed sack and take on the sack delay
2082 * value specified in spp_sackdelay.
2083 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2084 * off delayed sack. If the spp_address field is blank then
2085 * delayed sack is disabled for the entire association. Note
2086 * also that this field is mutually exclusive to
2087 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2090 static int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2091 struct sctp_transport
*trans
,
2092 struct sctp_association
*asoc
,
2093 struct sctp_sock
*sp
,
2096 int sackdelay_change
)
2100 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2101 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2106 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2107 * this field is ignored. Note also that a value of zero indicates
2108 * the current setting should be left unchanged.
2110 if (params
->spp_flags
& SPP_HB_ENABLE
) {
2112 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2113 * set. This lets us use 0 value when this flag
2116 if (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)
2117 params
->spp_hbinterval
= 0;
2119 if (params
->spp_hbinterval
||
2120 (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)) {
2123 msecs_to_jiffies(params
->spp_hbinterval
);
2126 msecs_to_jiffies(params
->spp_hbinterval
);
2128 sp
->hbinterval
= params
->spp_hbinterval
;
2135 trans
->param_flags
=
2136 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2139 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2142 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2146 /* When Path MTU discovery is disabled the value specified here will
2147 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2148 * include the flag SPP_PMTUD_DISABLE for this field to have any
2151 if ((params
->spp_flags
& SPP_PMTUD_DISABLE
) && params
->spp_pathmtu
) {
2153 trans
->pathmtu
= params
->spp_pathmtu
;
2154 sctp_assoc_sync_pmtu(asoc
);
2156 asoc
->pathmtu
= params
->spp_pathmtu
;
2157 sctp_frag_point(sp
, params
->spp_pathmtu
);
2159 sp
->pathmtu
= params
->spp_pathmtu
;
2165 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2166 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2167 trans
->param_flags
=
2168 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2170 sctp_transport_pmtu(trans
);
2171 sctp_assoc_sync_pmtu(asoc
);
2175 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2178 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2182 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2183 * value of this field is ignored. Note also that a value of zero
2184 * indicates the current setting should be left unchanged.
2186 if ((params
->spp_flags
& SPP_SACKDELAY_ENABLE
) && params
->spp_sackdelay
) {
2189 msecs_to_jiffies(params
->spp_sackdelay
);
2192 msecs_to_jiffies(params
->spp_sackdelay
);
2194 sp
->sackdelay
= params
->spp_sackdelay
;
2198 if (sackdelay_change
) {
2200 trans
->param_flags
=
2201 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2205 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2209 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2214 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2215 * of this field is ignored. Note also that a value of zero
2216 * indicates the current setting should be left unchanged.
2218 if ((params
->spp_flags
& SPP_PMTUD_ENABLE
) && params
->spp_pathmaxrxt
) {
2220 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2222 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2224 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2231 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2232 char __user
*optval
, int optlen
)
2234 struct sctp_paddrparams params
;
2235 struct sctp_transport
*trans
= NULL
;
2236 struct sctp_association
*asoc
= NULL
;
2237 struct sctp_sock
*sp
= sctp_sk(sk
);
2239 int hb_change
, pmtud_change
, sackdelay_change
;
2241 if (optlen
!= sizeof(struct sctp_paddrparams
))
2244 if (copy_from_user(¶ms
, optval
, optlen
))
2247 /* Validate flags and value parameters. */
2248 hb_change
= params
.spp_flags
& SPP_HB
;
2249 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2250 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2252 if (hb_change
== SPP_HB
||
2253 pmtud_change
== SPP_PMTUD
||
2254 sackdelay_change
== SPP_SACKDELAY
||
2255 params
.spp_sackdelay
> 500 ||
2257 && params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2260 /* If an address other than INADDR_ANY is specified, and
2261 * no transport is found, then the request is invalid.
2263 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
2264 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2265 params
.spp_assoc_id
);
2270 /* Get association, if assoc_id != 0 and the socket is a one
2271 * to many style socket, and an association was not found, then
2272 * the id was invalid.
2274 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2275 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2278 /* Heartbeat demand can only be sent on a transport or
2279 * association, but not a socket.
2281 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2284 /* Process parameters. */
2285 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2286 hb_change
, pmtud_change
,
2292 /* If changes are for association, also apply parameters to each
2295 if (!trans
&& asoc
) {
2296 struct list_head
*pos
;
2298 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2299 trans
= list_entry(pos
, struct sctp_transport
,
2301 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2302 hb_change
, pmtud_change
,
2310 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2312 * This options will get or set the delayed ack timer. The time is set
2313 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2314 * endpoints default delayed ack timer value. If the assoc_id field is
2315 * non-zero, then the set or get effects the specified association.
2317 * struct sctp_assoc_value {
2318 * sctp_assoc_t assoc_id;
2319 * uint32_t assoc_value;
2322 * assoc_id - This parameter, indicates which association the
2323 * user is preforming an action upon. Note that if
2324 * this field's value is zero then the endpoints
2325 * default value is changed (effecting future
2326 * associations only).
2328 * assoc_value - This parameter contains the number of milliseconds
2329 * that the user is requesting the delayed ACK timer
2330 * be set to. Note that this value is defined in
2331 * the standard to be between 200 and 500 milliseconds.
2333 * Note: a value of zero will leave the value alone,
2334 * but disable SACK delay. A non-zero value will also
2335 * enable SACK delay.
2338 static int sctp_setsockopt_delayed_ack_time(struct sock
*sk
,
2339 char __user
*optval
, int optlen
)
2341 struct sctp_assoc_value params
;
2342 struct sctp_transport
*trans
= NULL
;
2343 struct sctp_association
*asoc
= NULL
;
2344 struct sctp_sock
*sp
= sctp_sk(sk
);
2346 if (optlen
!= sizeof(struct sctp_assoc_value
))
2349 if (copy_from_user(¶ms
, optval
, optlen
))
2352 /* Validate value parameter. */
2353 if (params
.assoc_value
> 500)
2356 /* Get association, if assoc_id != 0 and the socket is a one
2357 * to many style socket, and an association was not found, then
2358 * the id was invalid.
2360 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2361 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2364 if (params
.assoc_value
) {
2367 msecs_to_jiffies(params
.assoc_value
);
2369 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2370 SPP_SACKDELAY_ENABLE
;
2372 sp
->sackdelay
= params
.assoc_value
;
2374 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2375 SPP_SACKDELAY_ENABLE
;
2380 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2381 SPP_SACKDELAY_DISABLE
;
2384 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2385 SPP_SACKDELAY_DISABLE
;
2389 /* If change is for association, also apply to each transport. */
2391 struct list_head
*pos
;
2393 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2394 trans
= list_entry(pos
, struct sctp_transport
,
2396 if (params
.assoc_value
) {
2398 msecs_to_jiffies(params
.assoc_value
);
2399 trans
->param_flags
=
2400 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2401 SPP_SACKDELAY_ENABLE
;
2403 trans
->param_flags
=
2404 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2405 SPP_SACKDELAY_DISABLE
;
2413 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2415 * Applications can specify protocol parameters for the default association
2416 * initialization. The option name argument to setsockopt() and getsockopt()
2419 * Setting initialization parameters is effective only on an unconnected
2420 * socket (for UDP-style sockets only future associations are effected
2421 * by the change). With TCP-style sockets, this option is inherited by
2422 * sockets derived from a listener socket.
2424 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, int optlen
)
2426 struct sctp_initmsg sinit
;
2427 struct sctp_sock
*sp
= sctp_sk(sk
);
2429 if (optlen
!= sizeof(struct sctp_initmsg
))
2431 if (copy_from_user(&sinit
, optval
, optlen
))
2434 if (sinit
.sinit_num_ostreams
)
2435 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2436 if (sinit
.sinit_max_instreams
)
2437 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2438 if (sinit
.sinit_max_attempts
)
2439 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2440 if (sinit
.sinit_max_init_timeo
)
2441 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2447 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2449 * Applications that wish to use the sendto() system call may wish to
2450 * specify a default set of parameters that would normally be supplied
2451 * through the inclusion of ancillary data. This socket option allows
2452 * such an application to set the default sctp_sndrcvinfo structure.
2453 * The application that wishes to use this socket option simply passes
2454 * in to this call the sctp_sndrcvinfo structure defined in Section
2455 * 5.2.2) The input parameters accepted by this call include
2456 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2457 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2458 * to this call if the caller is using the UDP model.
2460 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2461 char __user
*optval
, int optlen
)
2463 struct sctp_sndrcvinfo info
;
2464 struct sctp_association
*asoc
;
2465 struct sctp_sock
*sp
= sctp_sk(sk
);
2467 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2469 if (copy_from_user(&info
, optval
, optlen
))
2472 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2473 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2477 asoc
->default_stream
= info
.sinfo_stream
;
2478 asoc
->default_flags
= info
.sinfo_flags
;
2479 asoc
->default_ppid
= info
.sinfo_ppid
;
2480 asoc
->default_context
= info
.sinfo_context
;
2481 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2483 sp
->default_stream
= info
.sinfo_stream
;
2484 sp
->default_flags
= info
.sinfo_flags
;
2485 sp
->default_ppid
= info
.sinfo_ppid
;
2486 sp
->default_context
= info
.sinfo_context
;
2487 sp
->default_timetolive
= info
.sinfo_timetolive
;
2493 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2495 * Requests that the local SCTP stack use the enclosed peer address as
2496 * the association primary. The enclosed address must be one of the
2497 * association peer's addresses.
2499 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2502 struct sctp_prim prim
;
2503 struct sctp_transport
*trans
;
2505 if (optlen
!= sizeof(struct sctp_prim
))
2508 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2511 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2515 sctp_assoc_set_primary(trans
->asoc
, trans
);
2521 * 7.1.5 SCTP_NODELAY
2523 * Turn on/off any Nagle-like algorithm. This means that packets are
2524 * generally sent as soon as possible and no unnecessary delays are
2525 * introduced, at the cost of more packets in the network. Expects an
2526 * integer boolean flag.
2528 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2533 if (optlen
< sizeof(int))
2535 if (get_user(val
, (int __user
*)optval
))
2538 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2544 * 7.1.1 SCTP_RTOINFO
2546 * The protocol parameters used to initialize and bound retransmission
2547 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2548 * and modify these parameters.
2549 * All parameters are time values, in milliseconds. A value of 0, when
2550 * modifying the parameters, indicates that the current value should not
2554 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, int optlen
) {
2555 struct sctp_rtoinfo rtoinfo
;
2556 struct sctp_association
*asoc
;
2558 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2561 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2564 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2566 /* Set the values to the specific association */
2567 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2571 if (rtoinfo
.srto_initial
!= 0)
2573 msecs_to_jiffies(rtoinfo
.srto_initial
);
2574 if (rtoinfo
.srto_max
!= 0)
2575 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2576 if (rtoinfo
.srto_min
!= 0)
2577 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2579 /* If there is no association or the association-id = 0
2580 * set the values to the endpoint.
2582 struct sctp_sock
*sp
= sctp_sk(sk
);
2584 if (rtoinfo
.srto_initial
!= 0)
2585 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2586 if (rtoinfo
.srto_max
!= 0)
2587 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2588 if (rtoinfo
.srto_min
!= 0)
2589 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2597 * 7.1.2 SCTP_ASSOCINFO
2599 * This option is used to tune the maximum retransmission attempts
2600 * of the association.
2601 * Returns an error if the new association retransmission value is
2602 * greater than the sum of the retransmission value of the peer.
2603 * See [SCTP] for more information.
2606 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, int optlen
)
2609 struct sctp_assocparams assocparams
;
2610 struct sctp_association
*asoc
;
2612 if (optlen
!= sizeof(struct sctp_assocparams
))
2614 if (copy_from_user(&assocparams
, optval
, optlen
))
2617 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2619 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2622 /* Set the values to the specific association */
2624 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2627 struct list_head
*pos
;
2628 struct sctp_transport
*peer_addr
;
2630 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2631 peer_addr
= list_entry(pos
,
2632 struct sctp_transport
,
2634 path_sum
+= peer_addr
->pathmaxrxt
;
2638 /* Only validate asocmaxrxt if we have more then
2639 * one path/transport. We do this because path
2640 * retransmissions are only counted when we have more
2644 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2647 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2650 if (assocparams
.sasoc_cookie_life
!= 0) {
2651 asoc
->cookie_life
.tv_sec
=
2652 assocparams
.sasoc_cookie_life
/ 1000;
2653 asoc
->cookie_life
.tv_usec
=
2654 (assocparams
.sasoc_cookie_life
% 1000)
2658 /* Set the values to the endpoint */
2659 struct sctp_sock
*sp
= sctp_sk(sk
);
2661 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2662 sp
->assocparams
.sasoc_asocmaxrxt
=
2663 assocparams
.sasoc_asocmaxrxt
;
2664 if (assocparams
.sasoc_cookie_life
!= 0)
2665 sp
->assocparams
.sasoc_cookie_life
=
2666 assocparams
.sasoc_cookie_life
;
2672 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2674 * This socket option is a boolean flag which turns on or off mapped V4
2675 * addresses. If this option is turned on and the socket is type
2676 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2677 * If this option is turned off, then no mapping will be done of V4
2678 * addresses and a user will receive both PF_INET6 and PF_INET type
2679 * addresses on the socket.
2681 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, int optlen
)
2684 struct sctp_sock
*sp
= sctp_sk(sk
);
2686 if (optlen
< sizeof(int))
2688 if (get_user(val
, (int __user
*)optval
))
2699 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2701 * This socket option specifies the maximum size to put in any outgoing
2702 * SCTP chunk. If a message is larger than this size it will be
2703 * fragmented by SCTP into the specified size. Note that the underlying
2704 * SCTP implementation may fragment into smaller sized chunks when the
2705 * PMTU of the underlying association is smaller than the value set by
2708 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, int optlen
)
2710 struct sctp_association
*asoc
;
2711 struct list_head
*pos
;
2712 struct sctp_sock
*sp
= sctp_sk(sk
);
2715 if (optlen
< sizeof(int))
2717 if (get_user(val
, (int __user
*)optval
))
2719 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2721 sp
->user_frag
= val
;
2723 /* Update the frag_point of the existing associations. */
2724 list_for_each(pos
, &(sp
->ep
->asocs
)) {
2725 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
2726 asoc
->frag_point
= sctp_frag_point(sp
, asoc
->pathmtu
);
2734 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2736 * Requests that the peer mark the enclosed address as the association
2737 * primary. The enclosed address must be one of the association's
2738 * locally bound addresses. The following structure is used to make a
2739 * set primary request:
2741 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2744 struct sctp_sock
*sp
;
2745 struct sctp_endpoint
*ep
;
2746 struct sctp_association
*asoc
= NULL
;
2747 struct sctp_setpeerprim prim
;
2748 struct sctp_chunk
*chunk
;
2754 if (!sctp_addip_enable
)
2757 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2760 if (copy_from_user(&prim
, optval
, optlen
))
2763 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2767 if (!asoc
->peer
.asconf_capable
)
2770 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2773 if (!sctp_state(asoc
, ESTABLISHED
))
2776 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2777 return -EADDRNOTAVAIL
;
2779 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2780 chunk
= sctp_make_asconf_set_prim(asoc
,
2781 (union sctp_addr
*)&prim
.sspp_addr
);
2785 err
= sctp_send_asconf(asoc
, chunk
);
2787 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2792 static int sctp_setsockopt_adaptation_layer(struct sock
*sk
, char __user
*optval
,
2795 struct sctp_setadaptation adaptation
;
2797 if (optlen
!= sizeof(struct sctp_setadaptation
))
2799 if (copy_from_user(&adaptation
, optval
, optlen
))
2802 sctp_sk(sk
)->adaptation_ind
= adaptation
.ssb_adaptation_ind
;
2808 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2810 * The context field in the sctp_sndrcvinfo structure is normally only
2811 * used when a failed message is retrieved holding the value that was
2812 * sent down on the actual send call. This option allows the setting of
2813 * a default context on an association basis that will be received on
2814 * reading messages from the peer. This is especially helpful in the
2815 * one-2-many model for an application to keep some reference to an
2816 * internal state machine that is processing messages on the
2817 * association. Note that the setting of this value only effects
2818 * received messages from the peer and does not effect the value that is
2819 * saved with outbound messages.
2821 static int sctp_setsockopt_context(struct sock
*sk
, char __user
*optval
,
2824 struct sctp_assoc_value params
;
2825 struct sctp_sock
*sp
;
2826 struct sctp_association
*asoc
;
2828 if (optlen
!= sizeof(struct sctp_assoc_value
))
2830 if (copy_from_user(¶ms
, optval
, optlen
))
2835 if (params
.assoc_id
!= 0) {
2836 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2839 asoc
->default_rcv_context
= params
.assoc_value
;
2841 sp
->default_rcv_context
= params
.assoc_value
;
2848 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2850 * This options will at a minimum specify if the implementation is doing
2851 * fragmented interleave. Fragmented interleave, for a one to many
2852 * socket, is when subsequent calls to receive a message may return
2853 * parts of messages from different associations. Some implementations
2854 * may allow you to turn this value on or off. If so, when turned off,
2855 * no fragment interleave will occur (which will cause a head of line
2856 * blocking amongst multiple associations sharing the same one to many
2857 * socket). When this option is turned on, then each receive call may
2858 * come from a different association (thus the user must receive data
2859 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2860 * association each receive belongs to.
2862 * This option takes a boolean value. A non-zero value indicates that
2863 * fragmented interleave is on. A value of zero indicates that
2864 * fragmented interleave is off.
2866 * Note that it is important that an implementation that allows this
2867 * option to be turned on, have it off by default. Otherwise an unaware
2868 * application using the one to many model may become confused and act
2871 static int sctp_setsockopt_fragment_interleave(struct sock
*sk
,
2872 char __user
*optval
,
2877 if (optlen
!= sizeof(int))
2879 if (get_user(val
, (int __user
*)optval
))
2882 sctp_sk(sk
)->frag_interleave
= (val
== 0) ? 0 : 1;
2888 * 7.1.25. Set or Get the sctp partial delivery point
2889 * (SCTP_PARTIAL_DELIVERY_POINT)
2890 * This option will set or get the SCTP partial delivery point. This
2891 * point is the size of a message where the partial delivery API will be
2892 * invoked to help free up rwnd space for the peer. Setting this to a
2893 * lower value will cause partial delivery's to happen more often. The
2894 * calls argument is an integer that sets or gets the partial delivery
2897 static int sctp_setsockopt_partial_delivery_point(struct sock
*sk
,
2898 char __user
*optval
,
2903 if (optlen
!= sizeof(u32
))
2905 if (get_user(val
, (int __user
*)optval
))
2908 sctp_sk(sk
)->pd_point
= val
;
2910 return 0; /* is this the right error code? */
2914 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2916 * This option will allow a user to change the maximum burst of packets
2917 * that can be emitted by this association. Note that the default value
2918 * is 4, and some implementations may restrict this setting so that it
2919 * can only be lowered.
2921 * NOTE: This text doesn't seem right. Do this on a socket basis with
2922 * future associations inheriting the socket value.
2924 static int sctp_setsockopt_maxburst(struct sock
*sk
,
2925 char __user
*optval
,
2930 if (optlen
!= sizeof(int))
2932 if (get_user(val
, (int __user
*)optval
))
2938 sctp_sk(sk
)->max_burst
= val
;
2943 /* API 6.2 setsockopt(), getsockopt()
2945 * Applications use setsockopt() and getsockopt() to set or retrieve
2946 * socket options. Socket options are used to change the default
2947 * behavior of sockets calls. They are described in Section 7.
2951 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2952 * int __user *optlen);
2953 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2956 * sd - the socket descript.
2957 * level - set to IPPROTO_SCTP for all SCTP options.
2958 * optname - the option name.
2959 * optval - the buffer to store the value of the option.
2960 * optlen - the size of the buffer.
2962 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
2963 char __user
*optval
, int optlen
)
2967 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2970 /* I can hardly begin to describe how wrong this is. This is
2971 * so broken as to be worse than useless. The API draft
2972 * REALLY is NOT helpful here... I am not convinced that the
2973 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2974 * are at all well-founded.
2976 if (level
!= SOL_SCTP
) {
2977 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
2978 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
2985 case SCTP_SOCKOPT_BINDX_ADD
:
2986 /* 'optlen' is the size of the addresses buffer. */
2987 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2988 optlen
, SCTP_BINDX_ADD_ADDR
);
2991 case SCTP_SOCKOPT_BINDX_REM
:
2992 /* 'optlen' is the size of the addresses buffer. */
2993 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2994 optlen
, SCTP_BINDX_REM_ADDR
);
2997 case SCTP_SOCKOPT_CONNECTX
:
2998 /* 'optlen' is the size of the addresses buffer. */
2999 retval
= sctp_setsockopt_connectx(sk
, (struct sockaddr __user
*)optval
,
3003 case SCTP_DISABLE_FRAGMENTS
:
3004 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
3008 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
3011 case SCTP_AUTOCLOSE
:
3012 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
3015 case SCTP_PEER_ADDR_PARAMS
:
3016 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
3019 case SCTP_DELAYED_ACK_TIME
:
3020 retval
= sctp_setsockopt_delayed_ack_time(sk
, optval
, optlen
);
3022 case SCTP_PARTIAL_DELIVERY_POINT
:
3023 retval
= sctp_setsockopt_partial_delivery_point(sk
, optval
, optlen
);
3027 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
3029 case SCTP_DEFAULT_SEND_PARAM
:
3030 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
3033 case SCTP_PRIMARY_ADDR
:
3034 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
3036 case SCTP_SET_PEER_PRIMARY_ADDR
:
3037 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
3040 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
3043 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
3045 case SCTP_ASSOCINFO
:
3046 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
3048 case SCTP_I_WANT_MAPPED_V4_ADDR
:
3049 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
3052 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
3054 case SCTP_ADAPTATION_LAYER
:
3055 retval
= sctp_setsockopt_adaptation_layer(sk
, optval
, optlen
);
3058 retval
= sctp_setsockopt_context(sk
, optval
, optlen
);
3060 case SCTP_FRAGMENT_INTERLEAVE
:
3061 retval
= sctp_setsockopt_fragment_interleave(sk
, optval
, optlen
);
3063 case SCTP_MAX_BURST
:
3064 retval
= sctp_setsockopt_maxburst(sk
, optval
, optlen
);
3067 retval
= -ENOPROTOOPT
;
3071 sctp_release_sock(sk
);
3077 /* API 3.1.6 connect() - UDP Style Syntax
3079 * An application may use the connect() call in the UDP model to initiate an
3080 * association without sending data.
3084 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3086 * sd: the socket descriptor to have a new association added to.
3088 * nam: the address structure (either struct sockaddr_in or struct
3089 * sockaddr_in6 defined in RFC2553 [7]).
3091 * len: the size of the address.
3093 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
3101 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3102 __FUNCTION__
, sk
, addr
, addr_len
);
3104 /* Validate addr_len before calling common connect/connectx routine. */
3105 af
= sctp_get_af_specific(addr
->sa_family
);
3106 if (!af
|| addr_len
< af
->sockaddr_len
) {
3109 /* Pass correct addr len to common routine (so it knows there
3110 * is only one address being passed.
3112 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
);
3115 sctp_release_sock(sk
);
3119 /* FIXME: Write comments. */
3120 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
3122 return -EOPNOTSUPP
; /* STUB */
3125 /* 4.1.4 accept() - TCP Style Syntax
3127 * Applications use accept() call to remove an established SCTP
3128 * association from the accept queue of the endpoint. A new socket
3129 * descriptor will be returned from accept() to represent the newly
3130 * formed association.
3132 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
3134 struct sctp_sock
*sp
;
3135 struct sctp_endpoint
*ep
;
3136 struct sock
*newsk
= NULL
;
3137 struct sctp_association
*asoc
;
3146 if (!sctp_style(sk
, TCP
)) {
3147 error
= -EOPNOTSUPP
;
3151 if (!sctp_sstate(sk
, LISTENING
)) {
3156 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
3158 error
= sctp_wait_for_accept(sk
, timeo
);
3162 /* We treat the list of associations on the endpoint as the accept
3163 * queue and pick the first association on the list.
3165 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
3167 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
3173 /* Populate the fields of the newsk from the oldsk and migrate the
3174 * asoc to the newsk.
3176 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
3179 sctp_release_sock(sk
);
3184 /* The SCTP ioctl handler. */
3185 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
3187 return -ENOIOCTLCMD
;
3190 /* This is the function which gets called during socket creation to
3191 * initialized the SCTP-specific portion of the sock.
3192 * The sock structure should already be zero-filled memory.
3194 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
3196 struct sctp_endpoint
*ep
;
3197 struct sctp_sock
*sp
;
3199 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
3203 /* Initialize the SCTP per socket area. */
3204 switch (sk
->sk_type
) {
3205 case SOCK_SEQPACKET
:
3206 sp
->type
= SCTP_SOCKET_UDP
;
3209 sp
->type
= SCTP_SOCKET_TCP
;
3212 return -ESOCKTNOSUPPORT
;
3215 /* Initialize default send parameters. These parameters can be
3216 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3218 sp
->default_stream
= 0;
3219 sp
->default_ppid
= 0;
3220 sp
->default_flags
= 0;
3221 sp
->default_context
= 0;
3222 sp
->default_timetolive
= 0;
3224 sp
->default_rcv_context
= 0;
3225 sp
->max_burst
= sctp_max_burst
;
3227 /* Initialize default setup parameters. These parameters
3228 * can be modified with the SCTP_INITMSG socket option or
3229 * overridden by the SCTP_INIT CMSG.
3231 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
3232 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
3233 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
3234 sp
->initmsg
.sinit_max_init_timeo
= sctp_rto_max
;
3236 /* Initialize default RTO related parameters. These parameters can
3237 * be modified for with the SCTP_RTOINFO socket option.
3239 sp
->rtoinfo
.srto_initial
= sctp_rto_initial
;
3240 sp
->rtoinfo
.srto_max
= sctp_rto_max
;
3241 sp
->rtoinfo
.srto_min
= sctp_rto_min
;
3243 /* Initialize default association related parameters. These parameters
3244 * can be modified with the SCTP_ASSOCINFO socket option.
3246 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3247 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3248 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3249 sp
->assocparams
.sasoc_local_rwnd
= 0;
3250 sp
->assocparams
.sasoc_cookie_life
= sctp_valid_cookie_life
;
3252 /* Initialize default event subscriptions. By default, all the
3255 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3257 /* Default Peer Address Parameters. These defaults can
3258 * be modified via SCTP_PEER_ADDR_PARAMS
3260 sp
->hbinterval
= sctp_hb_interval
;
3261 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3262 sp
->pathmtu
= 0; // allow default discovery
3263 sp
->sackdelay
= sctp_sack_timeout
;
3264 sp
->param_flags
= SPP_HB_ENABLE
|
3266 SPP_SACKDELAY_ENABLE
;
3268 /* If enabled no SCTP message fragmentation will be performed.
3269 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3271 sp
->disable_fragments
= 0;
3273 /* Enable Nagle algorithm by default. */
3276 /* Enable by default. */
3279 /* Auto-close idle associations after the configured
3280 * number of seconds. A value of 0 disables this
3281 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3282 * for UDP-style sockets only.
3286 /* User specified fragmentation limit. */
3289 sp
->adaptation_ind
= 0;
3291 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3293 /* Control variables for partial data delivery. */
3294 atomic_set(&sp
->pd_mode
, 0);
3295 skb_queue_head_init(&sp
->pd_lobby
);
3296 sp
->frag_interleave
= 0;
3298 /* Create a per socket endpoint structure. Even if we
3299 * change the data structure relationships, this may still
3300 * be useful for storing pre-connect address information.
3302 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3309 SCTP_DBG_OBJCNT_INC(sock
);
3313 /* Cleanup any SCTP per socket resources. */
3314 SCTP_STATIC
int sctp_destroy_sock(struct sock
*sk
)
3316 struct sctp_endpoint
*ep
;
3318 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3320 /* Release our hold on the endpoint. */
3321 ep
= sctp_sk(sk
)->ep
;
3322 sctp_endpoint_free(ep
);
3327 /* API 4.1.7 shutdown() - TCP Style Syntax
3328 * int shutdown(int socket, int how);
3330 * sd - the socket descriptor of the association to be closed.
3331 * how - Specifies the type of shutdown. The values are
3334 * Disables further receive operations. No SCTP
3335 * protocol action is taken.
3337 * Disables further send operations, and initiates
3338 * the SCTP shutdown sequence.
3340 * Disables further send and receive operations
3341 * and initiates the SCTP shutdown sequence.
3343 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3345 struct sctp_endpoint
*ep
;
3346 struct sctp_association
*asoc
;
3348 if (!sctp_style(sk
, TCP
))
3351 if (how
& SEND_SHUTDOWN
) {
3352 ep
= sctp_sk(sk
)->ep
;
3353 if (!list_empty(&ep
->asocs
)) {
3354 asoc
= list_entry(ep
->asocs
.next
,
3355 struct sctp_association
, asocs
);
3356 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3361 /* 7.2.1 Association Status (SCTP_STATUS)
3363 * Applications can retrieve current status information about an
3364 * association, including association state, peer receiver window size,
3365 * number of unacked data chunks, and number of data chunks pending
3366 * receipt. This information is read-only.
3368 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3369 char __user
*optval
,
3372 struct sctp_status status
;
3373 struct sctp_association
*asoc
= NULL
;
3374 struct sctp_transport
*transport
;
3375 sctp_assoc_t associd
;
3378 if (len
!= sizeof(status
)) {
3383 if (copy_from_user(&status
, optval
, sizeof(status
))) {
3388 associd
= status
.sstat_assoc_id
;
3389 asoc
= sctp_id2assoc(sk
, associd
);
3395 transport
= asoc
->peer
.primary_path
;
3397 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3398 status
.sstat_state
= asoc
->state
;
3399 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3400 status
.sstat_unackdata
= asoc
->unack_data
;
3402 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3403 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3404 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3405 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3406 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3407 memcpy(&status
.sstat_primary
.spinfo_address
, &transport
->ipaddr
,
3408 transport
->af_specific
->sockaddr_len
);
3409 /* Map ipv4 address into v4-mapped-on-v6 address. */
3410 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3411 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3412 status
.sstat_primary
.spinfo_state
= transport
->state
;
3413 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3414 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3415 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3416 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3418 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3419 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3421 if (put_user(len
, optlen
)) {
3426 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3427 len
, status
.sstat_state
, status
.sstat_rwnd
,
3428 status
.sstat_assoc_id
);
3430 if (copy_to_user(optval
, &status
, len
)) {
3440 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3442 * Applications can retrieve information about a specific peer address
3443 * of an association, including its reachability state, congestion
3444 * window, and retransmission timer values. This information is
3447 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3448 char __user
*optval
,
3451 struct sctp_paddrinfo pinfo
;
3452 struct sctp_transport
*transport
;
3455 if (len
!= sizeof(pinfo
)) {
3460 if (copy_from_user(&pinfo
, optval
, sizeof(pinfo
))) {
3465 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3466 pinfo
.spinfo_assoc_id
);
3470 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3471 pinfo
.spinfo_state
= transport
->state
;
3472 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3473 pinfo
.spinfo_srtt
= transport
->srtt
;
3474 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3475 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3477 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3478 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3480 if (put_user(len
, optlen
)) {
3485 if (copy_to_user(optval
, &pinfo
, len
)) {
3494 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3496 * This option is a on/off flag. If enabled no SCTP message
3497 * fragmentation will be performed. Instead if a message being sent
3498 * exceeds the current PMTU size, the message will NOT be sent and
3499 * instead a error will be indicated to the user.
3501 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3502 char __user
*optval
, int __user
*optlen
)
3506 if (len
< sizeof(int))
3510 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3511 if (put_user(len
, optlen
))
3513 if (copy_to_user(optval
, &val
, len
))
3518 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3520 * This socket option is used to specify various notifications and
3521 * ancillary data the user wishes to receive.
3523 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3526 if (len
!= sizeof(struct sctp_event_subscribe
))
3528 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3533 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3535 * This socket option is applicable to the UDP-style socket only. When
3536 * set it will cause associations that are idle for more than the
3537 * specified number of seconds to automatically close. An association
3538 * being idle is defined an association that has NOT sent or received
3539 * user data. The special value of '0' indicates that no automatic
3540 * close of any associations should be performed. The option expects an
3541 * integer defining the number of seconds of idle time before an
3542 * association is closed.
3544 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3546 /* Applicable to UDP-style socket only */
3547 if (sctp_style(sk
, TCP
))
3549 if (len
!= sizeof(int))
3551 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, len
))
3556 /* Helper routine to branch off an association to a new socket. */
3557 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3558 struct socket
**sockp
)
3560 struct sock
*sk
= asoc
->base
.sk
;
3561 struct socket
*sock
;
3562 struct inet_sock
*inetsk
;
3566 /* An association cannot be branched off from an already peeled-off
3567 * socket, nor is this supported for tcp style sockets.
3569 if (!sctp_style(sk
, UDP
))
3572 /* Create a new socket. */
3573 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3577 /* Populate the fields of the newsk from the oldsk and migrate the
3578 * asoc to the newsk.
3580 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3582 /* Make peeled-off sockets more like 1-1 accepted sockets.
3583 * Set the daddr and initialize id to something more random
3585 af
= sctp_get_af_specific(asoc
->peer
.primary_addr
.sa
.sa_family
);
3586 af
->to_sk_daddr(&asoc
->peer
.primary_addr
, sk
);
3587 inetsk
= inet_sk(sock
->sk
);
3588 inetsk
->id
= asoc
->next_tsn
^ jiffies
;
3595 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3597 sctp_peeloff_arg_t peeloff
;
3598 struct socket
*newsock
;
3600 struct sctp_association
*asoc
;
3602 if (len
!= sizeof(sctp_peeloff_arg_t
))
3604 if (copy_from_user(&peeloff
, optval
, len
))
3607 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3613 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__
, sk
, asoc
);
3615 retval
= sctp_do_peeloff(asoc
, &newsock
);
3619 /* Map the socket to an unused fd that can be returned to the user. */
3620 retval
= sock_map_fd(newsock
);
3622 sock_release(newsock
);
3626 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3627 __FUNCTION__
, sk
, asoc
, newsock
->sk
, retval
);
3629 /* Return the fd mapped to the new socket. */
3630 peeloff
.sd
= retval
;
3631 if (copy_to_user(optval
, &peeloff
, len
))
3638 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3640 * Applications can enable or disable heartbeats for any peer address of
3641 * an association, modify an address's heartbeat interval, force a
3642 * heartbeat to be sent immediately, and adjust the address's maximum
3643 * number of retransmissions sent before an address is considered
3644 * unreachable. The following structure is used to access and modify an
3645 * address's parameters:
3647 * struct sctp_paddrparams {
3648 * sctp_assoc_t spp_assoc_id;
3649 * struct sockaddr_storage spp_address;
3650 * uint32_t spp_hbinterval;
3651 * uint16_t spp_pathmaxrxt;
3652 * uint32_t spp_pathmtu;
3653 * uint32_t spp_sackdelay;
3654 * uint32_t spp_flags;
3657 * spp_assoc_id - (one-to-many style socket) This is filled in the
3658 * application, and identifies the association for
3660 * spp_address - This specifies which address is of interest.
3661 * spp_hbinterval - This contains the value of the heartbeat interval,
3662 * in milliseconds. If a value of zero
3663 * is present in this field then no changes are to
3664 * be made to this parameter.
3665 * spp_pathmaxrxt - This contains the maximum number of
3666 * retransmissions before this address shall be
3667 * considered unreachable. If a value of zero
3668 * is present in this field then no changes are to
3669 * be made to this parameter.
3670 * spp_pathmtu - When Path MTU discovery is disabled the value
3671 * specified here will be the "fixed" path mtu.
3672 * Note that if the spp_address field is empty
3673 * then all associations on this address will
3674 * have this fixed path mtu set upon them.
3676 * spp_sackdelay - When delayed sack is enabled, this value specifies
3677 * the number of milliseconds that sacks will be delayed
3678 * for. This value will apply to all addresses of an
3679 * association if the spp_address field is empty. Note
3680 * also, that if delayed sack is enabled and this
3681 * value is set to 0, no change is made to the last
3682 * recorded delayed sack timer value.
3684 * spp_flags - These flags are used to control various features
3685 * on an association. The flag field may contain
3686 * zero or more of the following options.
3688 * SPP_HB_ENABLE - Enable heartbeats on the
3689 * specified address. Note that if the address
3690 * field is empty all addresses for the association
3691 * have heartbeats enabled upon them.
3693 * SPP_HB_DISABLE - Disable heartbeats on the
3694 * speicifed address. Note that if the address
3695 * field is empty all addresses for the association
3696 * will have their heartbeats disabled. Note also
3697 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3698 * mutually exclusive, only one of these two should
3699 * be specified. Enabling both fields will have
3700 * undetermined results.
3702 * SPP_HB_DEMAND - Request a user initiated heartbeat
3703 * to be made immediately.
3705 * SPP_PMTUD_ENABLE - This field will enable PMTU
3706 * discovery upon the specified address. Note that
3707 * if the address feild is empty then all addresses
3708 * on the association are effected.
3710 * SPP_PMTUD_DISABLE - This field will disable PMTU
3711 * discovery upon the specified address. Note that
3712 * if the address feild is empty then all addresses
3713 * on the association are effected. Not also that
3714 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3715 * exclusive. Enabling both will have undetermined
3718 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3719 * on delayed sack. The time specified in spp_sackdelay
3720 * is used to specify the sack delay for this address. Note
3721 * that if spp_address is empty then all addresses will
3722 * enable delayed sack and take on the sack delay
3723 * value specified in spp_sackdelay.
3724 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3725 * off delayed sack. If the spp_address field is blank then
3726 * delayed sack is disabled for the entire association. Note
3727 * also that this field is mutually exclusive to
3728 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3731 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
3732 char __user
*optval
, int __user
*optlen
)
3734 struct sctp_paddrparams params
;
3735 struct sctp_transport
*trans
= NULL
;
3736 struct sctp_association
*asoc
= NULL
;
3737 struct sctp_sock
*sp
= sctp_sk(sk
);
3739 if (len
!= sizeof(struct sctp_paddrparams
))
3742 if (copy_from_user(¶ms
, optval
, len
))
3745 /* If an address other than INADDR_ANY is specified, and
3746 * no transport is found, then the request is invalid.
3748 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
3749 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
3750 params
.spp_assoc_id
);
3752 SCTP_DEBUG_PRINTK("Failed no transport\n");
3757 /* Get association, if assoc_id != 0 and the socket is a one
3758 * to many style socket, and an association was not found, then
3759 * the id was invalid.
3761 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
3762 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
3763 SCTP_DEBUG_PRINTK("Failed no association\n");
3768 /* Fetch transport values. */
3769 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
3770 params
.spp_pathmtu
= trans
->pathmtu
;
3771 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
3772 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
3774 /*draft-11 doesn't say what to return in spp_flags*/
3775 params
.spp_flags
= trans
->param_flags
;
3777 /* Fetch association values. */
3778 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
3779 params
.spp_pathmtu
= asoc
->pathmtu
;
3780 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
3781 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
3783 /*draft-11 doesn't say what to return in spp_flags*/
3784 params
.spp_flags
= asoc
->param_flags
;
3786 /* Fetch socket values. */
3787 params
.spp_hbinterval
= sp
->hbinterval
;
3788 params
.spp_pathmtu
= sp
->pathmtu
;
3789 params
.spp_sackdelay
= sp
->sackdelay
;
3790 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
3792 /*draft-11 doesn't say what to return in spp_flags*/
3793 params
.spp_flags
= sp
->param_flags
;
3796 if (copy_to_user(optval
, ¶ms
, len
))
3799 if (put_user(len
, optlen
))
3805 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3807 * This options will get or set the delayed ack timer. The time is set
3808 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3809 * endpoints default delayed ack timer value. If the assoc_id field is
3810 * non-zero, then the set or get effects the specified association.
3812 * struct sctp_assoc_value {
3813 * sctp_assoc_t assoc_id;
3814 * uint32_t assoc_value;
3817 * assoc_id - This parameter, indicates which association the
3818 * user is preforming an action upon. Note that if
3819 * this field's value is zero then the endpoints
3820 * default value is changed (effecting future
3821 * associations only).
3823 * assoc_value - This parameter contains the number of milliseconds
3824 * that the user is requesting the delayed ACK timer
3825 * be set to. Note that this value is defined in
3826 * the standard to be between 200 and 500 milliseconds.
3828 * Note: a value of zero will leave the value alone,
3829 * but disable SACK delay. A non-zero value will also
3830 * enable SACK delay.
3832 static int sctp_getsockopt_delayed_ack_time(struct sock
*sk
, int len
,
3833 char __user
*optval
,
3836 struct sctp_assoc_value params
;
3837 struct sctp_association
*asoc
= NULL
;
3838 struct sctp_sock
*sp
= sctp_sk(sk
);
3840 if (len
!= sizeof(struct sctp_assoc_value
))
3843 if (copy_from_user(¶ms
, optval
, len
))
3846 /* Get association, if assoc_id != 0 and the socket is a one
3847 * to many style socket, and an association was not found, then
3848 * the id was invalid.
3850 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
3851 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
3855 /* Fetch association values. */
3856 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
)
3857 params
.assoc_value
= jiffies_to_msecs(
3860 params
.assoc_value
= 0;
3862 /* Fetch socket values. */
3863 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
)
3864 params
.assoc_value
= sp
->sackdelay
;
3866 params
.assoc_value
= 0;
3869 if (copy_to_user(optval
, ¶ms
, len
))
3872 if (put_user(len
, optlen
))
3878 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3880 * Applications can specify protocol parameters for the default association
3881 * initialization. The option name argument to setsockopt() and getsockopt()
3884 * Setting initialization parameters is effective only on an unconnected
3885 * socket (for UDP-style sockets only future associations are effected
3886 * by the change). With TCP-style sockets, this option is inherited by
3887 * sockets derived from a listener socket.
3889 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3891 if (len
!= sizeof(struct sctp_initmsg
))
3893 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
3898 static int sctp_getsockopt_peer_addrs_num_old(struct sock
*sk
, int len
,
3899 char __user
*optval
,
3903 struct sctp_association
*asoc
;
3904 struct list_head
*pos
;
3907 if (len
!= sizeof(sctp_assoc_t
))
3910 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3913 /* For UDP-style sockets, id specifies the association to query. */
3914 asoc
= sctp_id2assoc(sk
, id
);
3918 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3926 * Old API for getting list of peer addresses. Does not work for 32-bit
3927 * programs running on a 64-bit kernel
3929 static int sctp_getsockopt_peer_addrs_old(struct sock
*sk
, int len
,
3930 char __user
*optval
,
3933 struct sctp_association
*asoc
;
3934 struct list_head
*pos
;
3936 struct sctp_getaddrs_old getaddrs
;
3937 struct sctp_transport
*from
;
3939 union sctp_addr temp
;
3940 struct sctp_sock
*sp
= sctp_sk(sk
);
3943 if (len
!= sizeof(struct sctp_getaddrs_old
))
3946 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
3949 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3951 /* For UDP-style sockets, id specifies the association to query. */
3952 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3956 to
= (void __user
*)getaddrs
.addrs
;
3957 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3958 from
= list_entry(pos
, struct sctp_transport
, transports
);
3959 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3960 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3961 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3962 if (copy_to_user(to
, &temp
, addrlen
))
3966 if (cnt
>= getaddrs
.addr_num
) break;
3968 getaddrs
.addr_num
= cnt
;
3969 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
3975 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
3976 char __user
*optval
, int __user
*optlen
)
3978 struct sctp_association
*asoc
;
3979 struct list_head
*pos
;
3981 struct sctp_getaddrs getaddrs
;
3982 struct sctp_transport
*from
;
3984 union sctp_addr temp
;
3985 struct sctp_sock
*sp
= sctp_sk(sk
);
3990 if (len
< sizeof(struct sctp_getaddrs
))
3993 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
3996 /* For UDP-style sockets, id specifies the association to query. */
3997 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4001 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4002 space_left
= len
- sizeof(struct sctp_getaddrs
) -
4003 offsetof(struct sctp_getaddrs
,addrs
);
4005 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4006 from
= list_entry(pos
, struct sctp_transport
, transports
);
4007 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
4008 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4009 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
4010 if (space_left
< addrlen
)
4012 if (copy_to_user(to
, &temp
, addrlen
))
4016 space_left
-= addrlen
;
4019 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4021 bytes_copied
= ((char __user
*)to
) - optval
;
4022 if (put_user(bytes_copied
, optlen
))
4028 static int sctp_getsockopt_local_addrs_num_old(struct sock
*sk
, int len
,
4029 char __user
*optval
,
4033 struct sctp_bind_addr
*bp
;
4034 struct sctp_association
*asoc
;
4035 struct list_head
*pos
, *temp
;
4036 struct sctp_sockaddr_entry
*addr
;
4037 rwlock_t
*addr_lock
;
4040 if (len
!= sizeof(sctp_assoc_t
))
4043 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
4047 * For UDP-style sockets, id specifies the association to query.
4048 * If the id field is set to the value '0' then the locally bound
4049 * addresses are returned without regard to any particular
4053 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4054 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4056 asoc
= sctp_id2assoc(sk
, id
);
4059 bp
= &asoc
->base
.bind_addr
;
4060 addr_lock
= &asoc
->base
.addr_lock
;
4063 sctp_read_lock(addr_lock
);
4065 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4066 * addresses from the global local address list.
4068 if (sctp_list_single_entry(&bp
->address_list
)) {
4069 addr
= list_entry(bp
->address_list
.next
,
4070 struct sctp_sockaddr_entry
, list
);
4071 if (sctp_is_any(&addr
->a
)) {
4072 list_for_each_safe(pos
, temp
, &sctp_local_addr_list
) {
4073 addr
= list_entry(pos
,
4074 struct sctp_sockaddr_entry
,
4076 if ((PF_INET
== sk
->sk_family
) &&
4077 (AF_INET6
== addr
->a
.sa
.sa_family
))
4087 list_for_each(pos
, &bp
->address_list
) {
4092 sctp_read_unlock(addr_lock
);
4096 /* Helper function that copies local addresses to user and returns the number
4097 * of addresses copied.
4099 static int sctp_copy_laddrs_old(struct sock
*sk
, __u16 port
,
4100 int max_addrs
, void *to
,
4103 struct list_head
*pos
, *next
;
4104 struct sctp_sockaddr_entry
*addr
;
4105 union sctp_addr temp
;
4109 list_for_each_safe(pos
, next
, &sctp_local_addr_list
) {
4110 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4111 if ((PF_INET
== sk
->sk_family
) &&
4112 (AF_INET6
== addr
->a
.sa
.sa_family
))
4114 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4115 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
4117 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4118 memcpy(to
, &temp
, addrlen
);
4121 *bytes_copied
+= addrlen
;
4123 if (cnt
>= max_addrs
) break;
4129 static int sctp_copy_laddrs(struct sock
*sk
, __u16 port
, void *to
,
4130 size_t space_left
, int *bytes_copied
)
4132 struct list_head
*pos
, *next
;
4133 struct sctp_sockaddr_entry
*addr
;
4134 union sctp_addr temp
;
4138 list_for_each_safe(pos
, next
, &sctp_local_addr_list
) {
4139 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4140 if ((PF_INET
== sk
->sk_family
) &&
4141 (AF_INET6
== addr
->a
.sa
.sa_family
))
4143 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4144 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
4146 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4147 if (space_left
< addrlen
)
4149 memcpy(to
, &temp
, addrlen
);
4153 space_left
-= addrlen
;
4154 bytes_copied
+= addrlen
;
4160 /* Old API for getting list of local addresses. Does not work for 32-bit
4161 * programs running on a 64-bit kernel
4163 static int sctp_getsockopt_local_addrs_old(struct sock
*sk
, int len
,
4164 char __user
*optval
, int __user
*optlen
)
4166 struct sctp_bind_addr
*bp
;
4167 struct sctp_association
*asoc
;
4168 struct list_head
*pos
;
4170 struct sctp_getaddrs_old getaddrs
;
4171 struct sctp_sockaddr_entry
*addr
;
4173 union sctp_addr temp
;
4174 struct sctp_sock
*sp
= sctp_sk(sk
);
4176 rwlock_t
*addr_lock
;
4180 int bytes_copied
= 0;
4182 if (len
!= sizeof(struct sctp_getaddrs_old
))
4185 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
4188 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
4190 * For UDP-style sockets, id specifies the association to query.
4191 * If the id field is set to the value '0' then the locally bound
4192 * addresses are returned without regard to any particular
4195 if (0 == getaddrs
.assoc_id
) {
4196 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4197 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4199 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4202 bp
= &asoc
->base
.bind_addr
;
4203 addr_lock
= &asoc
->base
.addr_lock
;
4206 to
= getaddrs
.addrs
;
4208 /* Allocate space for a local instance of packed array to hold all
4209 * the data. We store addresses here first and then put write them
4210 * to the user in one shot.
4212 addrs
= kmalloc(sizeof(union sctp_addr
) * getaddrs
.addr_num
,
4217 sctp_read_lock(addr_lock
);
4219 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4220 * addresses from the global local address list.
4222 if (sctp_list_single_entry(&bp
->address_list
)) {
4223 addr
= list_entry(bp
->address_list
.next
,
4224 struct sctp_sockaddr_entry
, list
);
4225 if (sctp_is_any(&addr
->a
)) {
4226 cnt
= sctp_copy_laddrs_old(sk
, bp
->port
,
4228 addrs
, &bytes_copied
);
4234 list_for_each(pos
, &bp
->address_list
) {
4235 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4236 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4237 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4238 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4239 memcpy(buf
, &temp
, addrlen
);
4241 bytes_copied
+= addrlen
;
4243 if (cnt
>= getaddrs
.addr_num
) break;
4247 sctp_read_unlock(addr_lock
);
4249 /* copy the entire address list into the user provided space */
4250 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4255 /* copy the leading structure back to user */
4256 getaddrs
.addr_num
= cnt
;
4257 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
4265 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4266 char __user
*optval
, int __user
*optlen
)
4268 struct sctp_bind_addr
*bp
;
4269 struct sctp_association
*asoc
;
4270 struct list_head
*pos
;
4272 struct sctp_getaddrs getaddrs
;
4273 struct sctp_sockaddr_entry
*addr
;
4275 union sctp_addr temp
;
4276 struct sctp_sock
*sp
= sctp_sk(sk
);
4278 rwlock_t
*addr_lock
;
4281 int bytes_copied
= 0;
4285 if (len
<= sizeof(struct sctp_getaddrs
))
4288 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4292 * For UDP-style sockets, id specifies the association to query.
4293 * If the id field is set to the value '0' then the locally bound
4294 * addresses are returned without regard to any particular
4297 if (0 == getaddrs
.assoc_id
) {
4298 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4299 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4301 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4304 bp
= &asoc
->base
.bind_addr
;
4305 addr_lock
= &asoc
->base
.addr_lock
;
4308 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4309 space_left
= len
- sizeof(struct sctp_getaddrs
) -
4310 offsetof(struct sctp_getaddrs
,addrs
);
4311 addrs
= kmalloc(space_left
, GFP_KERNEL
);
4315 sctp_read_lock(addr_lock
);
4317 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4318 * addresses from the global local address list.
4320 if (sctp_list_single_entry(&bp
->address_list
)) {
4321 addr
= list_entry(bp
->address_list
.next
,
4322 struct sctp_sockaddr_entry
, list
);
4323 if (sctp_is_any(&addr
->a
)) {
4324 cnt
= sctp_copy_laddrs(sk
, bp
->port
, addrs
,
4325 space_left
, &bytes_copied
);
4335 list_for_each(pos
, &bp
->address_list
) {
4336 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4337 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4338 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4339 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4340 if (space_left
< addrlen
) {
4341 err
= -ENOMEM
; /*fixme: right error?*/
4344 memcpy(buf
, &temp
, addrlen
);
4346 bytes_copied
+= addrlen
;
4348 space_left
-= addrlen
;
4352 sctp_read_unlock(addr_lock
);
4354 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4358 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
)) {
4362 if (put_user(bytes_copied
, optlen
))
4369 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4371 * Requests that the local SCTP stack use the enclosed peer address as
4372 * the association primary. The enclosed address must be one of the
4373 * association peer's addresses.
4375 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4376 char __user
*optval
, int __user
*optlen
)
4378 struct sctp_prim prim
;
4379 struct sctp_association
*asoc
;
4380 struct sctp_sock
*sp
= sctp_sk(sk
);
4382 if (len
!= sizeof(struct sctp_prim
))
4385 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
4388 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4392 if (!asoc
->peer
.primary_path
)
4395 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4396 asoc
->peer
.primary_path
->af_specific
->sockaddr_len
);
4398 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4399 (union sctp_addr
*)&prim
.ssp_addr
);
4401 if (copy_to_user(optval
, &prim
, sizeof(struct sctp_prim
)))
4408 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4410 * Requests that the local endpoint set the specified Adaptation Layer
4411 * Indication parameter for all future INIT and INIT-ACK exchanges.
4413 static int sctp_getsockopt_adaptation_layer(struct sock
*sk
, int len
,
4414 char __user
*optval
, int __user
*optlen
)
4416 struct sctp_setadaptation adaptation
;
4418 if (len
!= sizeof(struct sctp_setadaptation
))
4421 adaptation
.ssb_adaptation_ind
= sctp_sk(sk
)->adaptation_ind
;
4422 if (copy_to_user(optval
, &adaptation
, len
))
4430 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4432 * Applications that wish to use the sendto() system call may wish to
4433 * specify a default set of parameters that would normally be supplied
4434 * through the inclusion of ancillary data. This socket option allows
4435 * such an application to set the default sctp_sndrcvinfo structure.
4438 * The application that wishes to use this socket option simply passes
4439 * in to this call the sctp_sndrcvinfo structure defined in Section
4440 * 5.2.2) The input parameters accepted by this call include
4441 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4442 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4443 * to this call if the caller is using the UDP model.
4445 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4447 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4448 int len
, char __user
*optval
,
4451 struct sctp_sndrcvinfo info
;
4452 struct sctp_association
*asoc
;
4453 struct sctp_sock
*sp
= sctp_sk(sk
);
4455 if (len
!= sizeof(struct sctp_sndrcvinfo
))
4457 if (copy_from_user(&info
, optval
, sizeof(struct sctp_sndrcvinfo
)))
4460 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4461 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4465 info
.sinfo_stream
= asoc
->default_stream
;
4466 info
.sinfo_flags
= asoc
->default_flags
;
4467 info
.sinfo_ppid
= asoc
->default_ppid
;
4468 info
.sinfo_context
= asoc
->default_context
;
4469 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4471 info
.sinfo_stream
= sp
->default_stream
;
4472 info
.sinfo_flags
= sp
->default_flags
;
4473 info
.sinfo_ppid
= sp
->default_ppid
;
4474 info
.sinfo_context
= sp
->default_context
;
4475 info
.sinfo_timetolive
= sp
->default_timetolive
;
4478 if (copy_to_user(optval
, &info
, sizeof(struct sctp_sndrcvinfo
)))
4486 * 7.1.5 SCTP_NODELAY
4488 * Turn on/off any Nagle-like algorithm. This means that packets are
4489 * generally sent as soon as possible and no unnecessary delays are
4490 * introduced, at the cost of more packets in the network. Expects an
4491 * integer boolean flag.
4494 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4495 char __user
*optval
, int __user
*optlen
)
4499 if (len
< sizeof(int))
4503 val
= (sctp_sk(sk
)->nodelay
== 1);
4504 if (put_user(len
, optlen
))
4506 if (copy_to_user(optval
, &val
, len
))
4513 * 7.1.1 SCTP_RTOINFO
4515 * The protocol parameters used to initialize and bound retransmission
4516 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4517 * and modify these parameters.
4518 * All parameters are time values, in milliseconds. A value of 0, when
4519 * modifying the parameters, indicates that the current value should not
4523 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4524 char __user
*optval
,
4525 int __user
*optlen
) {
4526 struct sctp_rtoinfo rtoinfo
;
4527 struct sctp_association
*asoc
;
4529 if (len
!= sizeof (struct sctp_rtoinfo
))
4532 if (copy_from_user(&rtoinfo
, optval
, sizeof (struct sctp_rtoinfo
)))
4535 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4537 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4540 /* Values corresponding to the specific association. */
4542 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4543 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4544 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4546 /* Values corresponding to the endpoint. */
4547 struct sctp_sock
*sp
= sctp_sk(sk
);
4549 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4550 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4551 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4554 if (put_user(len
, optlen
))
4557 if (copy_to_user(optval
, &rtoinfo
, len
))
4565 * 7.1.2 SCTP_ASSOCINFO
4567 * This option is used to tune the maximum retransmission attempts
4568 * of the association.
4569 * Returns an error if the new association retransmission value is
4570 * greater than the sum of the retransmission value of the peer.
4571 * See [SCTP] for more information.
4574 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4575 char __user
*optval
,
4579 struct sctp_assocparams assocparams
;
4580 struct sctp_association
*asoc
;
4581 struct list_head
*pos
;
4584 if (len
!= sizeof (struct sctp_assocparams
))
4587 if (copy_from_user(&assocparams
, optval
,
4588 sizeof (struct sctp_assocparams
)))
4591 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4593 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4596 /* Values correspoinding to the specific association */
4598 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4599 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4600 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4601 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4603 (asoc
->cookie_life
.tv_usec
4606 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4610 assocparams
.sasoc_number_peer_destinations
= cnt
;
4612 /* Values corresponding to the endpoint */
4613 struct sctp_sock
*sp
= sctp_sk(sk
);
4615 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4616 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4617 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4618 assocparams
.sasoc_cookie_life
=
4619 sp
->assocparams
.sasoc_cookie_life
;
4620 assocparams
.sasoc_number_peer_destinations
=
4622 sasoc_number_peer_destinations
;
4625 if (put_user(len
, optlen
))
4628 if (copy_to_user(optval
, &assocparams
, len
))
4635 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4637 * This socket option is a boolean flag which turns on or off mapped V4
4638 * addresses. If this option is turned on and the socket is type
4639 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4640 * If this option is turned off, then no mapping will be done of V4
4641 * addresses and a user will receive both PF_INET6 and PF_INET type
4642 * addresses on the socket.
4644 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4645 char __user
*optval
, int __user
*optlen
)
4648 struct sctp_sock
*sp
= sctp_sk(sk
);
4650 if (len
< sizeof(int))
4655 if (put_user(len
, optlen
))
4657 if (copy_to_user(optval
, &val
, len
))
4664 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4665 * (chapter and verse is quoted at sctp_setsockopt_context())
4667 static int sctp_getsockopt_context(struct sock
*sk
, int len
,
4668 char __user
*optval
, int __user
*optlen
)
4670 struct sctp_assoc_value params
;
4671 struct sctp_sock
*sp
;
4672 struct sctp_association
*asoc
;
4674 if (len
!= sizeof(struct sctp_assoc_value
))
4677 if (copy_from_user(¶ms
, optval
, len
))
4682 if (params
.assoc_id
!= 0) {
4683 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4686 params
.assoc_value
= asoc
->default_rcv_context
;
4688 params
.assoc_value
= sp
->default_rcv_context
;
4691 if (put_user(len
, optlen
))
4693 if (copy_to_user(optval
, ¶ms
, len
))
4700 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4702 * This socket option specifies the maximum size to put in any outgoing
4703 * SCTP chunk. If a message is larger than this size it will be
4704 * fragmented by SCTP into the specified size. Note that the underlying
4705 * SCTP implementation may fragment into smaller sized chunks when the
4706 * PMTU of the underlying association is smaller than the value set by
4709 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4710 char __user
*optval
, int __user
*optlen
)
4714 if (len
< sizeof(int))
4719 val
= sctp_sk(sk
)->user_frag
;
4720 if (put_user(len
, optlen
))
4722 if (copy_to_user(optval
, &val
, len
))
4729 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4730 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4732 static int sctp_getsockopt_fragment_interleave(struct sock
*sk
, int len
,
4733 char __user
*optval
, int __user
*optlen
)
4737 if (len
< sizeof(int))
4742 val
= sctp_sk(sk
)->frag_interleave
;
4743 if (put_user(len
, optlen
))
4745 if (copy_to_user(optval
, &val
, len
))
4752 * 7.1.25. Set or Get the sctp partial delivery point
4753 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4755 static int sctp_getsockopt_partial_delivery_point(struct sock
*sk
, int len
,
4756 char __user
*optval
,
4761 if (len
< sizeof(u32
))
4766 val
= sctp_sk(sk
)->pd_point
;
4767 if (put_user(len
, optlen
))
4769 if (copy_to_user(optval
, &val
, len
))
4776 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4777 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4779 static int sctp_getsockopt_maxburst(struct sock
*sk
, int len
,
4780 char __user
*optval
,
4785 if (len
< sizeof(int))
4790 val
= sctp_sk(sk
)->max_burst
;
4791 if (put_user(len
, optlen
))
4793 if (copy_to_user(optval
, &val
, len
))
4799 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
4800 char __user
*optval
, int __user
*optlen
)
4805 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4808 /* I can hardly begin to describe how wrong this is. This is
4809 * so broken as to be worse than useless. The API draft
4810 * REALLY is NOT helpful here... I am not convinced that the
4811 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4812 * are at all well-founded.
4814 if (level
!= SOL_SCTP
) {
4815 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4817 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
4821 if (get_user(len
, optlen
))
4828 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
4830 case SCTP_DISABLE_FRAGMENTS
:
4831 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
4835 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
4837 case SCTP_AUTOCLOSE
:
4838 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
4840 case SCTP_SOCKOPT_PEELOFF
:
4841 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
4843 case SCTP_PEER_ADDR_PARAMS
:
4844 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
4847 case SCTP_DELAYED_ACK_TIME
:
4848 retval
= sctp_getsockopt_delayed_ack_time(sk
, len
, optval
,
4852 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
4854 case SCTP_GET_PEER_ADDRS_NUM_OLD
:
4855 retval
= sctp_getsockopt_peer_addrs_num_old(sk
, len
, optval
,
4858 case SCTP_GET_LOCAL_ADDRS_NUM_OLD
:
4859 retval
= sctp_getsockopt_local_addrs_num_old(sk
, len
, optval
,
4862 case SCTP_GET_PEER_ADDRS_OLD
:
4863 retval
= sctp_getsockopt_peer_addrs_old(sk
, len
, optval
,
4866 case SCTP_GET_LOCAL_ADDRS_OLD
:
4867 retval
= sctp_getsockopt_local_addrs_old(sk
, len
, optval
,
4870 case SCTP_GET_PEER_ADDRS
:
4871 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
4874 case SCTP_GET_LOCAL_ADDRS
:
4875 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
4878 case SCTP_DEFAULT_SEND_PARAM
:
4879 retval
= sctp_getsockopt_default_send_param(sk
, len
,
4882 case SCTP_PRIMARY_ADDR
:
4883 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
4886 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
4889 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
4891 case SCTP_ASSOCINFO
:
4892 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
4894 case SCTP_I_WANT_MAPPED_V4_ADDR
:
4895 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
4898 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
4900 case SCTP_GET_PEER_ADDR_INFO
:
4901 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
4904 case SCTP_ADAPTATION_LAYER
:
4905 retval
= sctp_getsockopt_adaptation_layer(sk
, len
, optval
,
4909 retval
= sctp_getsockopt_context(sk
, len
, optval
, optlen
);
4911 case SCTP_FRAGMENT_INTERLEAVE
:
4912 retval
= sctp_getsockopt_fragment_interleave(sk
, len
, optval
,
4915 case SCTP_PARTIAL_DELIVERY_POINT
:
4916 retval
= sctp_getsockopt_partial_delivery_point(sk
, len
, optval
,
4919 case SCTP_MAX_BURST
:
4920 retval
= sctp_getsockopt_maxburst(sk
, len
, optval
, optlen
);
4923 retval
= -ENOPROTOOPT
;
4927 sctp_release_sock(sk
);
4931 static void sctp_hash(struct sock
*sk
)
4936 static void sctp_unhash(struct sock
*sk
)
4941 /* Check if port is acceptable. Possibly find first available port.
4943 * The port hash table (contained in the 'global' SCTP protocol storage
4944 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4945 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4946 * list (the list number is the port number hashed out, so as you
4947 * would expect from a hash function, all the ports in a given list have
4948 * such a number that hashes out to the same list number; you were
4949 * expecting that, right?); so each list has a set of ports, with a
4950 * link to the socket (struct sock) that uses it, the port number and
4951 * a fastreuse flag (FIXME: NPI ipg).
4953 static struct sctp_bind_bucket
*sctp_bucket_create(
4954 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
4956 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
4958 struct sctp_bind_hashbucket
*head
; /* hash list */
4959 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
4960 unsigned short snum
;
4963 snum
= ntohs(addr
->v4
.sin_port
);
4965 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
4966 sctp_local_bh_disable();
4969 /* Search for an available port.
4971 * 'sctp_port_rover' was the last port assigned, so
4972 * we start to search from 'sctp_port_rover +
4973 * 1'. What we do is first check if port 'rover' is
4974 * already in the hash table; if not, we use that; if
4975 * it is, we try next.
4977 int low
= sysctl_local_port_range
[0];
4978 int high
= sysctl_local_port_range
[1];
4979 int remaining
= (high
- low
) + 1;
4983 sctp_spin_lock(&sctp_port_alloc_lock
);
4984 rover
= sctp_port_rover
;
4987 if ((rover
< low
) || (rover
> high
))
4989 index
= sctp_phashfn(rover
);
4990 head
= &sctp_port_hashtable
[index
];
4991 sctp_spin_lock(&head
->lock
);
4992 for (pp
= head
->chain
; pp
; pp
= pp
->next
)
4993 if (pp
->port
== rover
)
4997 sctp_spin_unlock(&head
->lock
);
4998 } while (--remaining
> 0);
4999 sctp_port_rover
= rover
;
5000 sctp_spin_unlock(&sctp_port_alloc_lock
);
5002 /* Exhausted local port range during search? */
5007 /* OK, here is the one we will use. HEAD (the port
5008 * hash table list entry) is non-NULL and we hold it's
5013 /* We are given an specific port number; we verify
5014 * that it is not being used. If it is used, we will
5015 * exahust the search in the hash list corresponding
5016 * to the port number (snum) - we detect that with the
5017 * port iterator, pp being NULL.
5019 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
5020 sctp_spin_lock(&head
->lock
);
5021 for (pp
= head
->chain
; pp
; pp
= pp
->next
) {
5022 if (pp
->port
== snum
)
5029 if (!hlist_empty(&pp
->owner
)) {
5030 /* We had a port hash table hit - there is an
5031 * available port (pp != NULL) and it is being
5032 * used by other socket (pp->owner not empty); that other
5033 * socket is going to be sk2.
5035 int reuse
= sk
->sk_reuse
;
5037 struct hlist_node
*node
;
5039 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5040 if (pp
->fastreuse
&& sk
->sk_reuse
&&
5041 sk
->sk_state
!= SCTP_SS_LISTENING
)
5044 /* Run through the list of sockets bound to the port
5045 * (pp->port) [via the pointers bind_next and
5046 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5047 * we get the endpoint they describe and run through
5048 * the endpoint's list of IP (v4 or v6) addresses,
5049 * comparing each of the addresses with the address of
5050 * the socket sk. If we find a match, then that means
5051 * that this port/socket (sk) combination are already
5054 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
5055 struct sctp_endpoint
*ep2
;
5056 ep2
= sctp_sk(sk2
)->ep
;
5058 if (reuse
&& sk2
->sk_reuse
&&
5059 sk2
->sk_state
!= SCTP_SS_LISTENING
)
5062 if (sctp_bind_addr_match(&ep2
->base
.bind_addr
, addr
,
5068 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5071 /* If there was a hash table miss, create a new port. */
5073 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
5076 /* In either case (hit or miss), make sure fastreuse is 1 only
5077 * if sk->sk_reuse is too (that is, if the caller requested
5078 * SO_REUSEADDR on this socket -sk-).
5080 if (hlist_empty(&pp
->owner
)) {
5081 if (sk
->sk_reuse
&& sk
->sk_state
!= SCTP_SS_LISTENING
)
5085 } else if (pp
->fastreuse
&&
5086 (!sk
->sk_reuse
|| sk
->sk_state
== SCTP_SS_LISTENING
))
5089 /* We are set, so fill up all the data in the hash table
5090 * entry, tie the socket list information with the rest of the
5091 * sockets FIXME: Blurry, NPI (ipg).
5094 if (!sctp_sk(sk
)->bind_hash
) {
5095 inet_sk(sk
)->num
= snum
;
5096 sk_add_bind_node(sk
, &pp
->owner
);
5097 sctp_sk(sk
)->bind_hash
= pp
;
5102 sctp_spin_unlock(&head
->lock
);
5105 sctp_local_bh_enable();
5109 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5110 * port is requested.
5112 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
5115 union sctp_addr addr
;
5116 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
5118 /* Set up a dummy address struct from the sk. */
5119 af
->from_sk(&addr
, sk
);
5120 addr
.v4
.sin_port
= htons(snum
);
5122 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5123 ret
= sctp_get_port_local(sk
, &addr
);
5125 return (ret
? 1 : 0);
5129 * 3.1.3 listen() - UDP Style Syntax
5131 * By default, new associations are not accepted for UDP style sockets.
5132 * An application uses listen() to mark a socket as being able to
5133 * accept new associations.
5135 SCTP_STATIC
int sctp_seqpacket_listen(struct sock
*sk
, int backlog
)
5137 struct sctp_sock
*sp
= sctp_sk(sk
);
5138 struct sctp_endpoint
*ep
= sp
->ep
;
5140 /* Only UDP style sockets that are not peeled off are allowed to
5143 if (!sctp_style(sk
, UDP
))
5146 /* If backlog is zero, disable listening. */
5148 if (sctp_sstate(sk
, CLOSED
))
5151 sctp_unhash_endpoint(ep
);
5152 sk
->sk_state
= SCTP_SS_CLOSED
;
5155 /* Return if we are already listening. */
5156 if (sctp_sstate(sk
, LISTENING
))
5160 * If a bind() or sctp_bindx() is not called prior to a listen()
5161 * call that allows new associations to be accepted, the system
5162 * picks an ephemeral port and will choose an address set equivalent
5163 * to binding with a wildcard address.
5165 * This is not currently spelled out in the SCTP sockets
5166 * extensions draft, but follows the practice as seen in TCP
5169 * Additionally, turn off fastreuse flag since we are not listening
5171 sk
->sk_state
= SCTP_SS_LISTENING
;
5172 if (!ep
->base
.bind_addr
.port
) {
5173 if (sctp_autobind(sk
))
5176 sctp_sk(sk
)->bind_hash
->fastreuse
= 0;
5178 sctp_hash_endpoint(ep
);
5183 * 4.1.3 listen() - TCP Style Syntax
5185 * Applications uses listen() to ready the SCTP endpoint for accepting
5186 * inbound associations.
5188 SCTP_STATIC
int sctp_stream_listen(struct sock
*sk
, int backlog
)
5190 struct sctp_sock
*sp
= sctp_sk(sk
);
5191 struct sctp_endpoint
*ep
= sp
->ep
;
5193 /* If backlog is zero, disable listening. */
5195 if (sctp_sstate(sk
, CLOSED
))
5198 sctp_unhash_endpoint(ep
);
5199 sk
->sk_state
= SCTP_SS_CLOSED
;
5202 if (sctp_sstate(sk
, LISTENING
))
5206 * If a bind() or sctp_bindx() is not called prior to a listen()
5207 * call that allows new associations to be accepted, the system
5208 * picks an ephemeral port and will choose an address set equivalent
5209 * to binding with a wildcard address.
5211 * This is not currently spelled out in the SCTP sockets
5212 * extensions draft, but follows the practice as seen in TCP
5215 sk
->sk_state
= SCTP_SS_LISTENING
;
5216 if (!ep
->base
.bind_addr
.port
) {
5217 if (sctp_autobind(sk
))
5220 sctp_sk(sk
)->bind_hash
->fastreuse
= 0;
5222 sk
->sk_max_ack_backlog
= backlog
;
5223 sctp_hash_endpoint(ep
);
5228 * Move a socket to LISTENING state.
5230 int sctp_inet_listen(struct socket
*sock
, int backlog
)
5232 struct sock
*sk
= sock
->sk
;
5233 struct crypto_hash
*tfm
= NULL
;
5236 if (unlikely(backlog
< 0))
5241 if (sock
->state
!= SS_UNCONNECTED
)
5244 /* Allocate HMAC for generating cookie. */
5245 if (sctp_hmac_alg
) {
5246 tfm
= crypto_alloc_hash(sctp_hmac_alg
, 0, CRYPTO_ALG_ASYNC
);
5248 if (net_ratelimit()) {
5250 "SCTP: failed to load transform for %s: %ld\n",
5251 sctp_hmac_alg
, PTR_ERR(tfm
));
5258 switch (sock
->type
) {
5259 case SOCK_SEQPACKET
:
5260 err
= sctp_seqpacket_listen(sk
, backlog
);
5263 err
= sctp_stream_listen(sk
, backlog
);
5272 /* Store away the transform reference. */
5273 sctp_sk(sk
)->hmac
= tfm
;
5275 sctp_release_sock(sk
);
5278 crypto_free_hash(tfm
);
5283 * This function is done by modeling the current datagram_poll() and the
5284 * tcp_poll(). Note that, based on these implementations, we don't
5285 * lock the socket in this function, even though it seems that,
5286 * ideally, locking or some other mechanisms can be used to ensure
5287 * the integrity of the counters (sndbuf and wmem_alloc) used
5288 * in this place. We assume that we don't need locks either until proven
5291 * Another thing to note is that we include the Async I/O support
5292 * here, again, by modeling the current TCP/UDP code. We don't have
5293 * a good way to test with it yet.
5295 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
5297 struct sock
*sk
= sock
->sk
;
5298 struct sctp_sock
*sp
= sctp_sk(sk
);
5301 poll_wait(file
, sk
->sk_sleep
, wait
);
5303 /* A TCP-style listening socket becomes readable when the accept queue
5306 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
5307 return (!list_empty(&sp
->ep
->asocs
)) ?
5308 (POLLIN
| POLLRDNORM
) : 0;
5312 /* Is there any exceptional events? */
5313 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
5315 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5317 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
5320 /* Is it readable? Reconsider this code with TCP-style support. */
5321 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
5322 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
5323 mask
|= POLLIN
| POLLRDNORM
;
5325 /* The association is either gone or not ready. */
5326 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
5329 /* Is it writable? */
5330 if (sctp_writeable(sk
)) {
5331 mask
|= POLLOUT
| POLLWRNORM
;
5333 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
5335 * Since the socket is not locked, the buffer
5336 * might be made available after the writeable check and
5337 * before the bit is set. This could cause a lost I/O
5338 * signal. tcp_poll() has a race breaker for this race
5339 * condition. Based on their implementation, we put
5340 * in the following code to cover it as well.
5342 if (sctp_writeable(sk
))
5343 mask
|= POLLOUT
| POLLWRNORM
;
5348 /********************************************************************
5349 * 2nd Level Abstractions
5350 ********************************************************************/
5352 static struct sctp_bind_bucket
*sctp_bucket_create(
5353 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
5355 struct sctp_bind_bucket
*pp
;
5357 pp
= kmem_cache_alloc(sctp_bucket_cachep
, GFP_ATOMIC
);
5358 SCTP_DBG_OBJCNT_INC(bind_bucket
);
5362 INIT_HLIST_HEAD(&pp
->owner
);
5363 if ((pp
->next
= head
->chain
) != NULL
)
5364 pp
->next
->pprev
= &pp
->next
;
5366 pp
->pprev
= &head
->chain
;
5371 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5372 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
5374 if (pp
&& hlist_empty(&pp
->owner
)) {
5376 pp
->next
->pprev
= pp
->pprev
;
5377 *(pp
->pprev
) = pp
->next
;
5378 kmem_cache_free(sctp_bucket_cachep
, pp
);
5379 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
5383 /* Release this socket's reference to a local port. */
5384 static inline void __sctp_put_port(struct sock
*sk
)
5386 struct sctp_bind_hashbucket
*head
=
5387 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->num
)];
5388 struct sctp_bind_bucket
*pp
;
5390 sctp_spin_lock(&head
->lock
);
5391 pp
= sctp_sk(sk
)->bind_hash
;
5392 __sk_del_bind_node(sk
);
5393 sctp_sk(sk
)->bind_hash
= NULL
;
5394 inet_sk(sk
)->num
= 0;
5395 sctp_bucket_destroy(pp
);
5396 sctp_spin_unlock(&head
->lock
);
5399 void sctp_put_port(struct sock
*sk
)
5401 sctp_local_bh_disable();
5402 __sctp_put_port(sk
);
5403 sctp_local_bh_enable();
5407 * The system picks an ephemeral port and choose an address set equivalent
5408 * to binding with a wildcard address.
5409 * One of those addresses will be the primary address for the association.
5410 * This automatically enables the multihoming capability of SCTP.
5412 static int sctp_autobind(struct sock
*sk
)
5414 union sctp_addr autoaddr
;
5418 /* Initialize a local sockaddr structure to INADDR_ANY. */
5419 af
= sctp_sk(sk
)->pf
->af
;
5421 port
= htons(inet_sk(sk
)->num
);
5422 af
->inaddr_any(&autoaddr
, port
);
5424 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5427 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5430 * 4.2 The cmsghdr Structure *
5432 * When ancillary data is sent or received, any number of ancillary data
5433 * objects can be specified by the msg_control and msg_controllen members of
5434 * the msghdr structure, because each object is preceded by
5435 * a cmsghdr structure defining the object's length (the cmsg_len member).
5436 * Historically Berkeley-derived implementations have passed only one object
5437 * at a time, but this API allows multiple objects to be
5438 * passed in a single call to sendmsg() or recvmsg(). The following example
5439 * shows two ancillary data objects in a control buffer.
5441 * |<--------------------------- msg_controllen -------------------------->|
5444 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5446 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5449 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5451 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5454 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5455 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5457 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5459 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5466 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5467 sctp_cmsgs_t
*cmsgs
)
5469 struct cmsghdr
*cmsg
;
5471 for (cmsg
= CMSG_FIRSTHDR(msg
);
5473 cmsg
= CMSG_NXTHDR((struct msghdr
*)msg
, cmsg
)) {
5474 if (!CMSG_OK(msg
, cmsg
))
5477 /* Should we parse this header or ignore? */
5478 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5481 /* Strictly check lengths following example in SCM code. */
5482 switch (cmsg
->cmsg_type
) {
5484 /* SCTP Socket API Extension
5485 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5487 * This cmsghdr structure provides information for
5488 * initializing new SCTP associations with sendmsg().
5489 * The SCTP_INITMSG socket option uses this same data
5490 * structure. This structure is not used for
5493 * cmsg_level cmsg_type cmsg_data[]
5494 * ------------ ------------ ----------------------
5495 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5497 if (cmsg
->cmsg_len
!=
5498 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5500 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5504 /* SCTP Socket API Extension
5505 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5507 * This cmsghdr structure specifies SCTP options for
5508 * sendmsg() and describes SCTP header information
5509 * about a received message through recvmsg().
5511 * cmsg_level cmsg_type cmsg_data[]
5512 * ------------ ------------ ----------------------
5513 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5515 if (cmsg
->cmsg_len
!=
5516 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5520 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5522 /* Minimally, validate the sinfo_flags. */
5523 if (cmsgs
->info
->sinfo_flags
&
5524 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5525 SCTP_ABORT
| SCTP_EOF
))
5537 * Wait for a packet..
5538 * Note: This function is the same function as in core/datagram.c
5539 * with a few modifications to make lksctp work.
5541 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5546 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5548 /* Socket errors? */
5549 error
= sock_error(sk
);
5553 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5556 /* Socket shut down? */
5557 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5560 /* Sequenced packets can come disconnected. If so we report the
5565 /* Is there a good reason to think that we may receive some data? */
5566 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5569 /* Handle signals. */
5570 if (signal_pending(current
))
5573 /* Let another process have a go. Since we are going to sleep
5574 * anyway. Note: This may cause odd behaviors if the message
5575 * does not fit in the user's buffer, but this seems to be the
5576 * only way to honor MSG_DONTWAIT realistically.
5578 sctp_release_sock(sk
);
5579 *timeo_p
= schedule_timeout(*timeo_p
);
5583 finish_wait(sk
->sk_sleep
, &wait
);
5587 error
= sock_intr_errno(*timeo_p
);
5590 finish_wait(sk
->sk_sleep
, &wait
);
5595 /* Receive a datagram.
5596 * Note: This is pretty much the same routine as in core/datagram.c
5597 * with a few changes to make lksctp work.
5599 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5600 int noblock
, int *err
)
5603 struct sk_buff
*skb
;
5606 timeo
= sock_rcvtimeo(sk
, noblock
);
5608 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5609 timeo
, MAX_SCHEDULE_TIMEOUT
);
5612 /* Again only user level code calls this function,
5613 * so nothing interrupt level
5614 * will suddenly eat the receive_queue.
5616 * Look at current nfs client by the way...
5617 * However, this function was corrent in any case. 8)
5619 if (flags
& MSG_PEEK
) {
5620 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5621 skb
= skb_peek(&sk
->sk_receive_queue
);
5623 atomic_inc(&skb
->users
);
5624 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5626 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5632 /* Caller is allowed not to check sk->sk_err before calling. */
5633 error
= sock_error(sk
);
5637 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5640 /* User doesn't want to wait. */
5644 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5653 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5654 static void __sctp_write_space(struct sctp_association
*asoc
)
5656 struct sock
*sk
= asoc
->base
.sk
;
5657 struct socket
*sock
= sk
->sk_socket
;
5659 if ((sctp_wspace(asoc
) > 0) && sock
) {
5660 if (waitqueue_active(&asoc
->wait
))
5661 wake_up_interruptible(&asoc
->wait
);
5663 if (sctp_writeable(sk
)) {
5664 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
5665 wake_up_interruptible(sk
->sk_sleep
);
5667 /* Note that we try to include the Async I/O support
5668 * here by modeling from the current TCP/UDP code.
5669 * We have not tested with it yet.
5671 if (sock
->fasync_list
&&
5672 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5673 sock_wake_async(sock
, 2, POLL_OUT
);
5678 /* Do accounting for the sndbuf space.
5679 * Decrement the used sndbuf space of the corresponding association by the
5680 * data size which was just transmitted(freed).
5682 static void sctp_wfree(struct sk_buff
*skb
)
5684 struct sctp_association
*asoc
;
5685 struct sctp_chunk
*chunk
;
5688 /* Get the saved chunk pointer. */
5689 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
5692 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
5693 sizeof(struct sk_buff
) +
5694 sizeof(struct sctp_chunk
);
5696 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
5699 __sctp_write_space(asoc
);
5701 sctp_association_put(asoc
);
5704 /* Do accounting for the receive space on the socket.
5705 * Accounting for the association is done in ulpevent.c
5706 * We set this as a destructor for the cloned data skbs so that
5707 * accounting is done at the correct time.
5709 void sctp_sock_rfree(struct sk_buff
*skb
)
5711 struct sock
*sk
= skb
->sk
;
5712 struct sctp_ulpevent
*event
= sctp_skb2event(skb
);
5714 atomic_sub(event
->rmem_len
, &sk
->sk_rmem_alloc
);
5718 /* Helper function to wait for space in the sndbuf. */
5719 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
5722 struct sock
*sk
= asoc
->base
.sk
;
5724 long current_timeo
= *timeo_p
;
5727 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5728 asoc
, (long)(*timeo_p
), msg_len
);
5730 /* Increment the association's refcnt. */
5731 sctp_association_hold(asoc
);
5733 /* Wait on the association specific sndbuf space. */
5735 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5736 TASK_INTERRUPTIBLE
);
5739 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5742 if (signal_pending(current
))
5743 goto do_interrupted
;
5744 if (msg_len
<= sctp_wspace(asoc
))
5747 /* Let another process have a go. Since we are going
5750 sctp_release_sock(sk
);
5751 current_timeo
= schedule_timeout(current_timeo
);
5752 BUG_ON(sk
!= asoc
->base
.sk
);
5755 *timeo_p
= current_timeo
;
5759 finish_wait(&asoc
->wait
, &wait
);
5761 /* Release the association's refcnt. */
5762 sctp_association_put(asoc
);
5771 err
= sock_intr_errno(*timeo_p
);
5779 /* If socket sndbuf has changed, wake up all per association waiters. */
5780 void sctp_write_space(struct sock
*sk
)
5782 struct sctp_association
*asoc
;
5783 struct list_head
*pos
;
5785 /* Wake up the tasks in each wait queue. */
5786 list_for_each(pos
, &((sctp_sk(sk
))->ep
->asocs
)) {
5787 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
5788 __sctp_write_space(asoc
);
5792 /* Is there any sndbuf space available on the socket?
5794 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5795 * associations on the same socket. For a UDP-style socket with
5796 * multiple associations, it is possible for it to be "unwriteable"
5797 * prematurely. I assume that this is acceptable because
5798 * a premature "unwriteable" is better than an accidental "writeable" which
5799 * would cause an unwanted block under certain circumstances. For the 1-1
5800 * UDP-style sockets or TCP-style sockets, this code should work.
5803 static int sctp_writeable(struct sock
*sk
)
5807 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
5813 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5814 * returns immediately with EINPROGRESS.
5816 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
5818 struct sock
*sk
= asoc
->base
.sk
;
5820 long current_timeo
= *timeo_p
;
5823 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__
, asoc
,
5826 /* Increment the association's refcnt. */
5827 sctp_association_hold(asoc
);
5830 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5831 TASK_INTERRUPTIBLE
);
5834 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5836 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5839 if (signal_pending(current
))
5840 goto do_interrupted
;
5842 if (sctp_state(asoc
, ESTABLISHED
))
5845 /* Let another process have a go. Since we are going
5848 sctp_release_sock(sk
);
5849 current_timeo
= schedule_timeout(current_timeo
);
5852 *timeo_p
= current_timeo
;
5856 finish_wait(&asoc
->wait
, &wait
);
5858 /* Release the association's refcnt. */
5859 sctp_association_put(asoc
);
5864 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
5867 err
= -ECONNREFUSED
;
5871 err
= sock_intr_errno(*timeo_p
);
5879 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
5881 struct sctp_endpoint
*ep
;
5885 ep
= sctp_sk(sk
)->ep
;
5889 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
5890 TASK_INTERRUPTIBLE
);
5892 if (list_empty(&ep
->asocs
)) {
5893 sctp_release_sock(sk
);
5894 timeo
= schedule_timeout(timeo
);
5899 if (!sctp_sstate(sk
, LISTENING
))
5903 if (!list_empty(&ep
->asocs
))
5906 err
= sock_intr_errno(timeo
);
5907 if (signal_pending(current
))
5915 finish_wait(sk
->sk_sleep
, &wait
);
5920 void sctp_wait_for_close(struct sock
*sk
, long timeout
)
5925 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5926 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
5928 sctp_release_sock(sk
);
5929 timeout
= schedule_timeout(timeout
);
5931 } while (!signal_pending(current
) && timeout
);
5933 finish_wait(sk
->sk_sleep
, &wait
);
5936 static void sctp_sock_rfree_frag(struct sk_buff
*skb
)
5938 struct sk_buff
*frag
;
5943 /* Don't forget the fragments. */
5944 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
5945 sctp_sock_rfree_frag(frag
);
5948 sctp_sock_rfree(skb
);
5951 static void sctp_skb_set_owner_r_frag(struct sk_buff
*skb
, struct sock
*sk
)
5953 struct sk_buff
*frag
;
5958 /* Don't forget the fragments. */
5959 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
5960 sctp_skb_set_owner_r_frag(frag
, sk
);
5963 sctp_skb_set_owner_r(skb
, sk
);
5966 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5967 * and its messages to the newsk.
5969 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
5970 struct sctp_association
*assoc
,
5971 sctp_socket_type_t type
)
5973 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
5974 struct sctp_sock
*newsp
= sctp_sk(newsk
);
5975 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5976 struct sctp_endpoint
*newep
= newsp
->ep
;
5977 struct sk_buff
*skb
, *tmp
;
5978 struct sctp_ulpevent
*event
;
5981 /* Migrate socket buffer sizes and all the socket level options to the
5984 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
5985 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
5986 /* Brute force copy old sctp opt. */
5987 inet_sk_copy_descendant(newsk
, oldsk
);
5989 /* Restore the ep value that was overwritten with the above structure
5995 /* Hook this new socket in to the bind_hash list. */
5996 pp
= sctp_sk(oldsk
)->bind_hash
;
5997 sk_add_bind_node(newsk
, &pp
->owner
);
5998 sctp_sk(newsk
)->bind_hash
= pp
;
5999 inet_sk(newsk
)->num
= inet_sk(oldsk
)->num
;
6001 /* Copy the bind_addr list from the original endpoint to the new
6002 * endpoint so that we can handle restarts properly
6004 if (PF_INET6
== assoc
->base
.sk
->sk_family
)
6005 flags
= SCTP_ADDR6_ALLOWED
;
6006 if (assoc
->peer
.ipv4_address
)
6007 flags
|= SCTP_ADDR4_PEERSUPP
;
6008 if (assoc
->peer
.ipv6_address
)
6009 flags
|= SCTP_ADDR6_PEERSUPP
;
6010 sctp_bind_addr_copy(&newsp
->ep
->base
.bind_addr
,
6011 &oldsp
->ep
->base
.bind_addr
,
6012 SCTP_SCOPE_GLOBAL
, GFP_KERNEL
, flags
);
6014 /* Move any messages in the old socket's receive queue that are for the
6015 * peeled off association to the new socket's receive queue.
6017 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
6018 event
= sctp_skb2event(skb
);
6019 if (event
->asoc
== assoc
) {
6020 sctp_sock_rfree_frag(skb
);
6021 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
6022 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
6023 sctp_skb_set_owner_r_frag(skb
, newsk
);
6027 /* Clean up any messages pending delivery due to partial
6028 * delivery. Three cases:
6029 * 1) No partial deliver; no work.
6030 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6031 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6033 skb_queue_head_init(&newsp
->pd_lobby
);
6034 atomic_set(&sctp_sk(newsk
)->pd_mode
, assoc
->ulpq
.pd_mode
);
6036 if (atomic_read(&sctp_sk(oldsk
)->pd_mode
)) {
6037 struct sk_buff_head
*queue
;
6039 /* Decide which queue to move pd_lobby skbs to. */
6040 if (assoc
->ulpq
.pd_mode
) {
6041 queue
= &newsp
->pd_lobby
;
6043 queue
= &newsk
->sk_receive_queue
;
6045 /* Walk through the pd_lobby, looking for skbs that
6046 * need moved to the new socket.
6048 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
6049 event
= sctp_skb2event(skb
);
6050 if (event
->asoc
== assoc
) {
6051 sctp_sock_rfree_frag(skb
);
6052 __skb_unlink(skb
, &oldsp
->pd_lobby
);
6053 __skb_queue_tail(queue
, skb
);
6054 sctp_skb_set_owner_r_frag(skb
, newsk
);
6058 /* Clear up any skbs waiting for the partial
6059 * delivery to finish.
6061 if (assoc
->ulpq
.pd_mode
)
6062 sctp_clear_pd(oldsk
, NULL
);
6066 sctp_skb_for_each(skb
, &assoc
->ulpq
.reasm
, tmp
) {
6067 sctp_sock_rfree_frag(skb
);
6068 sctp_skb_set_owner_r_frag(skb
, newsk
);
6071 sctp_skb_for_each(skb
, &assoc
->ulpq
.lobby
, tmp
) {
6072 sctp_sock_rfree_frag(skb
);
6073 sctp_skb_set_owner_r_frag(skb
, newsk
);
6076 /* Set the type of socket to indicate that it is peeled off from the
6077 * original UDP-style socket or created with the accept() call on a
6078 * TCP-style socket..
6082 /* Mark the new socket "in-use" by the user so that any packets
6083 * that may arrive on the association after we've moved it are
6084 * queued to the backlog. This prevents a potential race between
6085 * backlog processing on the old socket and new-packet processing
6086 * on the new socket.
6088 sctp_lock_sock(newsk
);
6089 sctp_assoc_migrate(assoc
, newsk
);
6091 /* If the association on the newsk is already closed before accept()
6092 * is called, set RCV_SHUTDOWN flag.
6094 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
6095 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
6097 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
6098 sctp_release_sock(newsk
);
6101 /* This proto struct describes the ULP interface for SCTP. */
6102 struct proto sctp_prot
= {
6104 .owner
= THIS_MODULE
,
6105 .close
= sctp_close
,
6106 .connect
= sctp_connect
,
6107 .disconnect
= sctp_disconnect
,
6108 .accept
= sctp_accept
,
6109 .ioctl
= sctp_ioctl
,
6110 .init
= sctp_init_sock
,
6111 .destroy
= sctp_destroy_sock
,
6112 .shutdown
= sctp_shutdown
,
6113 .setsockopt
= sctp_setsockopt
,
6114 .getsockopt
= sctp_getsockopt
,
6115 .sendmsg
= sctp_sendmsg
,
6116 .recvmsg
= sctp_recvmsg
,
6118 .backlog_rcv
= sctp_backlog_rcv
,
6120 .unhash
= sctp_unhash
,
6121 .get_port
= sctp_get_port
,
6122 .obj_size
= sizeof(struct sctp_sock
),
6125 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6126 struct proto sctpv6_prot
= {
6128 .owner
= THIS_MODULE
,
6129 .close
= sctp_close
,
6130 .connect
= sctp_connect
,
6131 .disconnect
= sctp_disconnect
,
6132 .accept
= sctp_accept
,
6133 .ioctl
= sctp_ioctl
,
6134 .init
= sctp_init_sock
,
6135 .destroy
= sctp_destroy_sock
,
6136 .shutdown
= sctp_shutdown
,
6137 .setsockopt
= sctp_setsockopt
,
6138 .getsockopt
= sctp_getsockopt
,
6139 .sendmsg
= sctp_sendmsg
,
6140 .recvmsg
= sctp_recvmsg
,
6142 .backlog_rcv
= sctp_backlog_rcv
,
6144 .unhash
= sctp_unhash
,
6145 .get_port
= sctp_get_port
,
6146 .obj_size
= sizeof(struct sctp6_sock
),
6148 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */