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
;
111 extern int sysctl_sctp_mem
[3];
112 extern int sysctl_sctp_rmem
[3];
113 extern int sysctl_sctp_wmem
[3];
115 int sctp_memory_pressure
;
116 atomic_t sctp_memory_allocated
;
117 atomic_t sctp_sockets_allocated
;
119 static void sctp_enter_memory_pressure(void)
121 sctp_memory_pressure
= 1;
125 /* Get the sndbuf space available at the time on the association. */
126 static inline int sctp_wspace(struct sctp_association
*asoc
)
130 if (asoc
->ep
->sndbuf_policy
)
131 amt
= asoc
->sndbuf_used
;
133 amt
= atomic_read(&asoc
->base
.sk
->sk_wmem_alloc
);
135 if (amt
>= asoc
->base
.sk
->sk_sndbuf
) {
136 if (asoc
->base
.sk
->sk_userlocks
& SOCK_SNDBUF_LOCK
)
139 amt
= sk_stream_wspace(asoc
->base
.sk
);
144 amt
= asoc
->base
.sk
->sk_sndbuf
- amt
;
149 /* Increment the used sndbuf space count of the corresponding association by
150 * the size of the outgoing data chunk.
151 * Also, set the skb destructor for sndbuf accounting later.
153 * Since it is always 1-1 between chunk and skb, and also a new skb is always
154 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
155 * destructor in the data chunk skb for the purpose of the sndbuf space
158 static inline void sctp_set_owner_w(struct sctp_chunk
*chunk
)
160 struct sctp_association
*asoc
= chunk
->asoc
;
161 struct sock
*sk
= asoc
->base
.sk
;
163 /* The sndbuf space is tracked per association. */
164 sctp_association_hold(asoc
);
166 skb_set_owner_w(chunk
->skb
, sk
);
168 chunk
->skb
->destructor
= sctp_wfree
;
169 /* Save the chunk pointer in skb for sctp_wfree to use later. */
170 *((struct sctp_chunk
**)(chunk
->skb
->cb
)) = chunk
;
172 asoc
->sndbuf_used
+= SCTP_DATA_SNDSIZE(chunk
) +
173 sizeof(struct sk_buff
) +
174 sizeof(struct sctp_chunk
);
176 atomic_add(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
177 sk_charge_skb(sk
, chunk
->skb
);
180 /* Verify that this is a valid address. */
181 static inline int sctp_verify_addr(struct sock
*sk
, union sctp_addr
*addr
,
186 /* Verify basic sockaddr. */
187 af
= sctp_sockaddr_af(sctp_sk(sk
), addr
, len
);
191 /* Is this a valid SCTP address? */
192 if (!af
->addr_valid(addr
, sctp_sk(sk
), NULL
))
195 if (!sctp_sk(sk
)->pf
->send_verify(sctp_sk(sk
), (addr
)))
201 /* Look up the association by its id. If this is not a UDP-style
202 * socket, the ID field is always ignored.
204 struct sctp_association
*sctp_id2assoc(struct sock
*sk
, sctp_assoc_t id
)
206 struct sctp_association
*asoc
= NULL
;
208 /* If this is not a UDP-style socket, assoc id should be ignored. */
209 if (!sctp_style(sk
, UDP
)) {
210 /* Return NULL if the socket state is not ESTABLISHED. It
211 * could be a TCP-style listening socket or a socket which
212 * hasn't yet called connect() to establish an association.
214 if (!sctp_sstate(sk
, ESTABLISHED
))
217 /* Get the first and the only association from the list. */
218 if (!list_empty(&sctp_sk(sk
)->ep
->asocs
))
219 asoc
= list_entry(sctp_sk(sk
)->ep
->asocs
.next
,
220 struct sctp_association
, asocs
);
224 /* Otherwise this is a UDP-style socket. */
225 if (!id
|| (id
== (sctp_assoc_t
)-1))
228 spin_lock_bh(&sctp_assocs_id_lock
);
229 asoc
= (struct sctp_association
*)idr_find(&sctp_assocs_id
, (int)id
);
230 spin_unlock_bh(&sctp_assocs_id_lock
);
232 if (!asoc
|| (asoc
->base
.sk
!= sk
) || asoc
->base
.dead
)
238 /* Look up the transport from an address and an assoc id. If both address and
239 * id are specified, the associations matching the address and the id should be
242 static struct sctp_transport
*sctp_addr_id2transport(struct sock
*sk
,
243 struct sockaddr_storage
*addr
,
246 struct sctp_association
*addr_asoc
= NULL
, *id_asoc
= NULL
;
247 struct sctp_transport
*transport
;
248 union sctp_addr
*laddr
= (union sctp_addr
*)addr
;
250 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
257 id_asoc
= sctp_id2assoc(sk
, id
);
258 if (id_asoc
&& (id_asoc
!= addr_asoc
))
261 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
262 (union sctp_addr
*)addr
);
267 /* API 3.1.2 bind() - UDP Style Syntax
268 * The syntax of bind() is,
270 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
272 * sd - the socket descriptor returned by socket().
273 * addr - the address structure (struct sockaddr_in or struct
274 * sockaddr_in6 [RFC 2553]),
275 * addr_len - the size of the address structure.
277 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
283 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
286 /* Disallow binding twice. */
287 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
288 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
293 sctp_release_sock(sk
);
298 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
300 /* Verify this is a valid sockaddr. */
301 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
302 union sctp_addr
*addr
, int len
)
306 /* Check minimum size. */
307 if (len
< sizeof (struct sockaddr
))
310 /* Does this PF support this AF? */
311 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
314 /* If we get this far, af is valid. */
315 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
317 if (len
< af
->sockaddr_len
)
323 /* Bind a local address either to an endpoint or to an association. */
324 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
326 struct sctp_sock
*sp
= sctp_sk(sk
);
327 struct sctp_endpoint
*ep
= sp
->ep
;
328 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
333 /* Common sockaddr verification. */
334 af
= sctp_sockaddr_af(sp
, addr
, len
);
336 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
341 snum
= ntohs(addr
->v4
.sin_port
);
343 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
344 ", port: %d, new port: %d, len: %d)\n",
350 /* PF specific bind() address verification. */
351 if (!sp
->pf
->bind_verify(sp
, addr
))
352 return -EADDRNOTAVAIL
;
354 /* We must either be unbound, or bind to the same port.
355 * It's OK to allow 0 ports if we are already bound.
356 * We'll just inhert an already bound port in this case
361 else if (snum
!= bp
->port
) {
362 SCTP_DEBUG_PRINTK("sctp_do_bind:"
363 " New port %d does not match existing port "
364 "%d.\n", snum
, bp
->port
);
369 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
372 /* Make sure we are allowed to bind here.
373 * The function sctp_get_port_local() does duplicate address
376 addr
->v4
.sin_port
= htons(snum
);
377 if ((ret
= sctp_get_port_local(sk
, addr
))) {
378 if (ret
== (long) sk
) {
379 /* This endpoint has a conflicting address. */
386 /* Refresh ephemeral port. */
388 bp
->port
= inet_sk(sk
)->num
;
390 /* Add the address to the bind address list.
391 * Use GFP_ATOMIC since BHs will be disabled.
393 ret
= sctp_add_bind_addr(bp
, addr
, 1, GFP_ATOMIC
);
395 /* Copy back into socket for getsockname() use. */
397 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
398 af
->to_sk_saddr(addr
, sk
);
404 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
406 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
407 * at any one time. If a sender, after sending an ASCONF chunk, decides
408 * it needs to transfer another ASCONF Chunk, it MUST wait until the
409 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
410 * subsequent ASCONF. Note this restriction binds each side, so at any
411 * time two ASCONF may be in-transit on any given association (one sent
412 * from each endpoint).
414 static int sctp_send_asconf(struct sctp_association
*asoc
,
415 struct sctp_chunk
*chunk
)
419 /* If there is an outstanding ASCONF chunk, queue it for later
422 if (asoc
->addip_last_asconf
) {
423 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
427 /* Hold the chunk until an ASCONF_ACK is received. */
428 sctp_chunk_hold(chunk
);
429 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
431 sctp_chunk_free(chunk
);
433 asoc
->addip_last_asconf
= chunk
;
439 /* Add a list of addresses as bind addresses to local endpoint or
442 * Basically run through each address specified in the addrs/addrcnt
443 * array/length pair, determine if it is IPv6 or IPv4 and call
444 * sctp_do_bind() on it.
446 * If any of them fails, then the operation will be reversed and the
447 * ones that were added will be removed.
449 * Only sctp_setsockopt_bindx() is supposed to call this function.
451 static int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
456 struct sockaddr
*sa_addr
;
459 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
463 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
464 /* The list may contain either IPv4 or IPv6 address;
465 * determine the address length for walking thru the list.
467 sa_addr
= (struct sockaddr
*)addr_buf
;
468 af
= sctp_get_af_specific(sa_addr
->sa_family
);
474 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
477 addr_buf
+= af
->sockaddr_len
;
481 /* Failed. Cleanup the ones that have been added */
483 sctp_bindx_rem(sk
, addrs
, cnt
);
491 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
492 * associations that are part of the endpoint indicating that a list of local
493 * addresses are added to the endpoint.
495 * If any of the addresses is already in the bind address list of the
496 * association, we do not send the chunk for that association. But it will not
497 * affect other associations.
499 * Only sctp_setsockopt_bindx() is supposed to call this function.
501 static int sctp_send_asconf_add_ip(struct sock
*sk
,
502 struct sockaddr
*addrs
,
505 struct sctp_sock
*sp
;
506 struct sctp_endpoint
*ep
;
507 struct sctp_association
*asoc
;
508 struct sctp_bind_addr
*bp
;
509 struct sctp_chunk
*chunk
;
510 struct sctp_sockaddr_entry
*laddr
;
511 union sctp_addr
*addr
;
512 union sctp_addr saveaddr
;
515 struct list_head
*pos
;
520 if (!sctp_addip_enable
)
526 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
527 __FUNCTION__
, sk
, addrs
, addrcnt
);
529 list_for_each(pos
, &ep
->asocs
) {
530 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
532 if (!asoc
->peer
.asconf_capable
)
535 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
538 if (!sctp_state(asoc
, ESTABLISHED
))
541 /* Check if any address in the packed array of addresses is
542 * in the bind address list of the association. If so,
543 * do not send the asconf chunk to its peer, but continue with
544 * other associations.
547 for (i
= 0; i
< addrcnt
; i
++) {
548 addr
= (union sctp_addr
*)addr_buf
;
549 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
555 if (sctp_assoc_lookup_laddr(asoc
, addr
))
558 addr_buf
+= af
->sockaddr_len
;
563 /* Use the first valid address in bind addr list of
564 * association as Address Parameter of ASCONF CHUNK.
566 bp
= &asoc
->base
.bind_addr
;
567 p
= bp
->address_list
.next
;
568 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
569 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
570 addrcnt
, SCTP_PARAM_ADD_IP
);
576 retval
= sctp_send_asconf(asoc
, chunk
);
580 /* Add the new addresses to the bind address list with
581 * use_as_src set to 0.
584 for (i
= 0; i
< addrcnt
; i
++) {
585 addr
= (union sctp_addr
*)addr_buf
;
586 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
587 memcpy(&saveaddr
, addr
, af
->sockaddr_len
);
588 retval
= sctp_add_bind_addr(bp
, &saveaddr
, 0,
590 addr_buf
+= af
->sockaddr_len
;
598 /* Remove a list of addresses from bind addresses list. Do not remove the
601 * Basically run through each address specified in the addrs/addrcnt
602 * array/length pair, determine if it is IPv6 or IPv4 and call
603 * sctp_del_bind() on it.
605 * If any of them fails, then the operation will be reversed and the
606 * ones that were removed will be added back.
608 * At least one address has to be left; if only one address is
609 * available, the operation will return -EBUSY.
611 * Only sctp_setsockopt_bindx() is supposed to call this function.
613 static int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
615 struct sctp_sock
*sp
= sctp_sk(sk
);
616 struct sctp_endpoint
*ep
= sp
->ep
;
618 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
621 union sctp_addr
*sa_addr
;
624 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
628 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
629 /* If the bind address list is empty or if there is only one
630 * bind address, there is nothing more to be removed (we need
631 * at least one address here).
633 if (list_empty(&bp
->address_list
) ||
634 (sctp_list_single_entry(&bp
->address_list
))) {
639 sa_addr
= (union sctp_addr
*)addr_buf
;
640 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
646 if (!af
->addr_valid(sa_addr
, sp
, NULL
)) {
647 retval
= -EADDRNOTAVAIL
;
651 if (sa_addr
->v4
.sin_port
!= htons(bp
->port
)) {
656 /* FIXME - There is probably a need to check if sk->sk_saddr and
657 * sk->sk_rcv_addr are currently set to one of the addresses to
658 * be removed. This is something which needs to be looked into
659 * when we are fixing the outstanding issues with multi-homing
660 * socket routing and failover schemes. Refer to comments in
661 * sctp_do_bind(). -daisy
663 retval
= sctp_del_bind_addr(bp
, sa_addr
, call_rcu
);
665 addr_buf
+= af
->sockaddr_len
;
668 /* Failed. Add the ones that has been removed back */
670 sctp_bindx_add(sk
, addrs
, cnt
);
678 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
679 * the associations that are part of the endpoint indicating that a list of
680 * local addresses are removed from the endpoint.
682 * If any of the addresses is already in the bind address list of the
683 * association, we do not send the chunk for that association. But it will not
684 * affect other associations.
686 * Only sctp_setsockopt_bindx() is supposed to call this function.
688 static int sctp_send_asconf_del_ip(struct sock
*sk
,
689 struct sockaddr
*addrs
,
692 struct sctp_sock
*sp
;
693 struct sctp_endpoint
*ep
;
694 struct sctp_association
*asoc
;
695 struct sctp_transport
*transport
;
696 struct sctp_bind_addr
*bp
;
697 struct sctp_chunk
*chunk
;
698 union sctp_addr
*laddr
;
701 struct list_head
*pos
, *pos1
;
702 struct sctp_sockaddr_entry
*saddr
;
706 if (!sctp_addip_enable
)
712 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
713 __FUNCTION__
, sk
, addrs
, addrcnt
);
715 list_for_each(pos
, &ep
->asocs
) {
716 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
718 if (!asoc
->peer
.asconf_capable
)
721 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
724 if (!sctp_state(asoc
, ESTABLISHED
))
727 /* Check if any address in the packed array of addresses is
728 * not present in the bind address list of the association.
729 * If so, do not send the asconf chunk to its peer, but
730 * continue with other associations.
733 for (i
= 0; i
< addrcnt
; i
++) {
734 laddr
= (union sctp_addr
*)addr_buf
;
735 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
741 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
744 addr_buf
+= af
->sockaddr_len
;
749 /* Find one address in the association's bind address list
750 * that is not in the packed array of addresses. This is to
751 * make sure that we do not delete all the addresses in the
754 bp
= &asoc
->base
.bind_addr
;
755 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
760 /* We do not need RCU protection throughout this loop
761 * because this is done under a socket lock from the
764 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
771 /* Reset use_as_src flag for the addresses in the bind address
772 * list that are to be deleted.
775 for (i
= 0; i
< addrcnt
; i
++) {
776 laddr
= (union sctp_addr
*)addr_buf
;
777 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
778 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
779 if (sctp_cmp_addr_exact(&saddr
->a
, laddr
))
780 saddr
->use_as_src
= 0;
782 addr_buf
+= af
->sockaddr_len
;
785 /* Update the route and saddr entries for all the transports
786 * as some of the addresses in the bind address list are
787 * about to be deleted and cannot be used as source addresses.
789 list_for_each(pos1
, &asoc
->peer
.transport_addr_list
) {
790 transport
= list_entry(pos1
, struct sctp_transport
,
792 dst_release(transport
->dst
);
793 sctp_transport_route(transport
, NULL
,
794 sctp_sk(asoc
->base
.sk
));
797 retval
= sctp_send_asconf(asoc
, chunk
);
803 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
806 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
809 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
810 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
813 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
814 * Section 3.1.2 for this usage.
816 * addrs is a pointer to an array of one or more socket addresses. Each
817 * address is contained in its appropriate structure (i.e. struct
818 * sockaddr_in or struct sockaddr_in6) the family of the address type
819 * must be used to distinguish the address length (note that this
820 * representation is termed a "packed array" of addresses). The caller
821 * specifies the number of addresses in the array with addrcnt.
823 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
824 * -1, and sets errno to the appropriate error code.
826 * For SCTP, the port given in each socket address must be the same, or
827 * sctp_bindx() will fail, setting errno to EINVAL.
829 * The flags parameter is formed from the bitwise OR of zero or more of
830 * the following currently defined flags:
832 * SCTP_BINDX_ADD_ADDR
834 * SCTP_BINDX_REM_ADDR
836 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
837 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
838 * addresses from the association. The two flags are mutually exclusive;
839 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
840 * not remove all addresses from an association; sctp_bindx() will
841 * reject such an attempt with EINVAL.
843 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
844 * additional addresses with an endpoint after calling bind(). Or use
845 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
846 * socket is associated with so that no new association accepted will be
847 * associated with those addresses. If the endpoint supports dynamic
848 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
849 * endpoint to send the appropriate message to the peer to change the
850 * peers address lists.
852 * Adding and removing addresses from a connected association is
853 * optional functionality. Implementations that do not support this
854 * functionality should return EOPNOTSUPP.
856 * Basically do nothing but copying the addresses from user to kernel
857 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
858 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
861 * We don't use copy_from_user() for optimization: we first do the
862 * sanity checks (buffer size -fast- and access check-healthy
863 * pointer); if all of those succeed, then we can alloc the memory
864 * (expensive operation) needed to copy the data to kernel. Then we do
865 * the copying without checking the user space area
866 * (__copy_from_user()).
868 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
871 * sk The sk of the socket
872 * addrs The pointer to the addresses in user land
873 * addrssize Size of the addrs buffer
874 * op Operation to perform (add or remove, see the flags of
877 * Returns 0 if ok, <0 errno code on error.
879 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
880 struct sockaddr __user
*addrs
,
881 int addrs_size
, int op
)
883 struct sockaddr
*kaddrs
;
887 struct sockaddr
*sa_addr
;
891 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
892 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
894 if (unlikely(addrs_size
<= 0))
897 /* Check the user passed a healthy pointer. */
898 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
901 /* Alloc space for the address array in kernel memory. */
902 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
903 if (unlikely(!kaddrs
))
906 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
911 /* Walk through the addrs buffer and count the number of addresses. */
913 while (walk_size
< addrs_size
) {
914 sa_addr
= (struct sockaddr
*)addr_buf
;
915 af
= sctp_get_af_specific(sa_addr
->sa_family
);
917 /* If the address family is not supported or if this address
918 * causes the address buffer to overflow return EINVAL.
920 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
925 addr_buf
+= af
->sockaddr_len
;
926 walk_size
+= af
->sockaddr_len
;
931 case SCTP_BINDX_ADD_ADDR
:
932 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
935 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
938 case SCTP_BINDX_REM_ADDR
:
939 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
942 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
956 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
958 * Common routine for handling connect() and sctp_connectx().
959 * Connect will come in with just a single address.
961 static int __sctp_connect(struct sock
* sk
,
962 struct sockaddr
*kaddrs
,
965 struct sctp_sock
*sp
;
966 struct sctp_endpoint
*ep
;
967 struct sctp_association
*asoc
= NULL
;
968 struct sctp_association
*asoc2
;
969 struct sctp_transport
*transport
;
977 union sctp_addr
*sa_addr
= NULL
;
980 unsigned int f_flags
= 0;
985 /* connect() cannot be done on a socket that is already in ESTABLISHED
986 * state - UDP-style peeled off socket or a TCP-style socket that
987 * is already connected.
988 * It cannot be done even on a TCP-style listening socket.
990 if (sctp_sstate(sk
, ESTABLISHED
) ||
991 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
996 /* Walk through the addrs buffer and count the number of addresses. */
998 while (walk_size
< addrs_size
) {
999 sa_addr
= (union sctp_addr
*)addr_buf
;
1000 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
1001 port
= ntohs(sa_addr
->v4
.sin_port
);
1003 /* If the address family is not supported or if this address
1004 * causes the address buffer to overflow return EINVAL.
1006 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
1011 /* Save current address so we can work with it */
1012 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
1014 err
= sctp_verify_addr(sk
, &to
, af
->sockaddr_len
);
1018 /* Make sure the destination port is correctly set
1021 if (asoc
&& asoc
->peer
.port
&& asoc
->peer
.port
!= port
)
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
, &to
, &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
, &to
)) {
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(&to
);
1073 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1080 /* Prime the peer's transport structures. */
1081 transport
= sctp_assoc_add_peer(asoc
, &to
, 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(sa_addr
->sa
.sa_family
);
1106 af
->to_sk_daddr(sa_addr
, sk
);
1109 /* in-kernel sockets don't generally have a file allocated to them
1110 * if all they do is call sock_create_kern().
1112 if (sk
->sk_socket
->file
)
1113 f_flags
= sk
->sk_socket
->file
->f_flags
;
1115 timeo
= sock_sndtimeo(sk
, f_flags
& O_NONBLOCK
);
1117 err
= sctp_wait_for_connect(asoc
, &timeo
);
1119 /* Don't free association on exit. */
1124 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1125 " kaddrs: %p err: %d\n",
1128 sctp_association_free(asoc
);
1132 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1135 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1137 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1138 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1139 * or IPv6 addresses.
1141 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1142 * Section 3.1.2 for this usage.
1144 * addrs is a pointer to an array of one or more socket addresses. Each
1145 * address is contained in its appropriate structure (i.e. struct
1146 * sockaddr_in or struct sockaddr_in6) the family of the address type
1147 * must be used to distengish the address length (note that this
1148 * representation is termed a "packed array" of addresses). The caller
1149 * specifies the number of addresses in the array with addrcnt.
1151 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1152 * -1, and sets errno to the appropriate error code.
1154 * For SCTP, the port given in each socket address must be the same, or
1155 * sctp_connectx() will fail, setting errno to EINVAL.
1157 * An application can use sctp_connectx to initiate an association with
1158 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1159 * allows a caller to specify multiple addresses at which a peer can be
1160 * reached. The way the SCTP stack uses the list of addresses to set up
1161 * the association is implementation dependant. This function only
1162 * specifies that the stack will try to make use of all the addresses in
1163 * the list when needed.
1165 * Note that the list of addresses passed in is only used for setting up
1166 * the association. It does not necessarily equal the set of addresses
1167 * the peer uses for the resulting association. If the caller wants to
1168 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1169 * retrieve them after the association has been set up.
1171 * Basically do nothing but copying the addresses from user to kernel
1172 * land and invoking either sctp_connectx(). This is used for tunneling
1173 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1175 * We don't use copy_from_user() for optimization: we first do the
1176 * sanity checks (buffer size -fast- and access check-healthy
1177 * pointer); if all of those succeed, then we can alloc the memory
1178 * (expensive operation) needed to copy the data to kernel. Then we do
1179 * the copying without checking the user space area
1180 * (__copy_from_user()).
1182 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1185 * sk The sk of the socket
1186 * addrs The pointer to the addresses in user land
1187 * addrssize Size of the addrs buffer
1189 * Returns 0 if ok, <0 errno code on error.
1191 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1192 struct sockaddr __user
*addrs
,
1196 struct sockaddr
*kaddrs
;
1198 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1199 __FUNCTION__
, sk
, addrs
, addrs_size
);
1201 if (unlikely(addrs_size
<= 0))
1204 /* Check the user passed a healthy pointer. */
1205 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1208 /* Alloc space for the address array in kernel memory. */
1209 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1210 if (unlikely(!kaddrs
))
1213 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1216 err
= __sctp_connect(sk
, kaddrs
, addrs_size
);
1223 /* API 3.1.4 close() - UDP Style Syntax
1224 * Applications use close() to perform graceful shutdown (as described in
1225 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1226 * by a UDP-style socket.
1230 * ret = close(int sd);
1232 * sd - the socket descriptor of the associations to be closed.
1234 * To gracefully shutdown a specific association represented by the
1235 * UDP-style socket, an application should use the sendmsg() call,
1236 * passing no user data, but including the appropriate flag in the
1237 * ancillary data (see Section xxxx).
1239 * If sd in the close() call is a branched-off socket representing only
1240 * one association, the shutdown is performed on that association only.
1242 * 4.1.6 close() - TCP Style Syntax
1244 * Applications use close() to gracefully close down an association.
1248 * int close(int sd);
1250 * sd - the socket descriptor of the association to be closed.
1252 * After an application calls close() on a socket descriptor, no further
1253 * socket operations will succeed on that descriptor.
1255 * API 7.1.4 SO_LINGER
1257 * An application using the TCP-style socket can use this option to
1258 * perform the SCTP ABORT primitive. The linger option structure is:
1261 * int l_onoff; // option on/off
1262 * int l_linger; // linger time
1265 * To enable the option, set l_onoff to 1. If the l_linger value is set
1266 * to 0, calling close() is the same as the ABORT primitive. If the
1267 * value is set to a negative value, the setsockopt() call will return
1268 * an error. If the value is set to a positive value linger_time, the
1269 * close() can be blocked for at most linger_time ms. If the graceful
1270 * shutdown phase does not finish during this period, close() will
1271 * return but the graceful shutdown phase continues in the system.
1273 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1275 struct sctp_endpoint
*ep
;
1276 struct sctp_association
*asoc
;
1277 struct list_head
*pos
, *temp
;
1279 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1282 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1284 ep
= sctp_sk(sk
)->ep
;
1286 /* Walk all associations on an endpoint. */
1287 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1288 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1290 if (sctp_style(sk
, TCP
)) {
1291 /* A closed association can still be in the list if
1292 * it belongs to a TCP-style listening socket that is
1293 * not yet accepted. If so, free it. If not, send an
1294 * ABORT or SHUTDOWN based on the linger options.
1296 if (sctp_state(asoc
, CLOSED
)) {
1297 sctp_unhash_established(asoc
);
1298 sctp_association_free(asoc
);
1303 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
) {
1304 struct sctp_chunk
*chunk
;
1306 chunk
= sctp_make_abort_user(asoc
, NULL
, 0);
1308 sctp_primitive_ABORT(asoc
, chunk
);
1310 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1313 /* Clean up any skbs sitting on the receive queue. */
1314 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1315 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1317 /* On a TCP-style socket, block for at most linger_time if set. */
1318 if (sctp_style(sk
, TCP
) && timeout
)
1319 sctp_wait_for_close(sk
, timeout
);
1321 /* This will run the backlog queue. */
1322 sctp_release_sock(sk
);
1324 /* Supposedly, no process has access to the socket, but
1325 * the net layers still may.
1327 sctp_local_bh_disable();
1328 sctp_bh_lock_sock(sk
);
1330 /* Hold the sock, since sk_common_release() will put sock_put()
1331 * and we have just a little more cleanup.
1334 sk_common_release(sk
);
1336 sctp_bh_unlock_sock(sk
);
1337 sctp_local_bh_enable();
1341 SCTP_DBG_OBJCNT_DEC(sock
);
1344 /* Handle EPIPE error. */
1345 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1348 err
= sock_error(sk
) ? : -EPIPE
;
1349 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1350 send_sig(SIGPIPE
, current
, 0);
1354 /* API 3.1.3 sendmsg() - UDP Style Syntax
1356 * An application uses sendmsg() and recvmsg() calls to transmit data to
1357 * and receive data from its peer.
1359 * ssize_t sendmsg(int socket, const struct msghdr *message,
1362 * socket - the socket descriptor of the endpoint.
1363 * message - pointer to the msghdr structure which contains a single
1364 * user message and possibly some ancillary data.
1366 * See Section 5 for complete description of the data
1369 * flags - flags sent or received with the user message, see Section
1370 * 5 for complete description of the flags.
1372 * Note: This function could use a rewrite especially when explicit
1373 * connect support comes in.
1375 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1377 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1379 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1380 struct msghdr
*msg
, size_t msg_len
)
1382 struct sctp_sock
*sp
;
1383 struct sctp_endpoint
*ep
;
1384 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1385 struct sctp_transport
*transport
, *chunk_tp
;
1386 struct sctp_chunk
*chunk
;
1388 struct sockaddr
*msg_name
= NULL
;
1389 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1390 struct sctp_sndrcvinfo
*sinfo
;
1391 struct sctp_initmsg
*sinit
;
1392 sctp_assoc_t associd
= 0;
1393 sctp_cmsgs_t cmsgs
= { NULL
};
1397 __u16 sinfo_flags
= 0;
1398 struct sctp_datamsg
*datamsg
;
1399 struct list_head
*pos
;
1400 int msg_flags
= msg
->msg_flags
;
1402 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1409 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1411 /* We cannot send a message over a TCP-style listening socket. */
1412 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1417 /* Parse out the SCTP CMSGs. */
1418 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1421 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1425 /* Fetch the destination address for this packet. This
1426 * address only selects the association--it is not necessarily
1427 * the address we will send to.
1428 * For a peeled-off socket, msg_name is ignored.
1430 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1431 int msg_namelen
= msg
->msg_namelen
;
1433 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1438 if (msg_namelen
> sizeof(to
))
1439 msg_namelen
= sizeof(to
);
1440 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1441 msg_name
= msg
->msg_name
;
1447 /* Did the user specify SNDRCVINFO? */
1449 sinfo_flags
= sinfo
->sinfo_flags
;
1450 associd
= sinfo
->sinfo_assoc_id
;
1453 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1454 msg_len
, sinfo_flags
);
1456 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1457 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1462 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1463 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1464 * If SCTP_ABORT is set, the message length could be non zero with
1465 * the msg_iov set to the user abort reason.
1467 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1468 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1473 /* If SCTP_ADDR_OVER is set, there must be an address
1474 * specified in msg_name.
1476 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1483 SCTP_DEBUG_PRINTK("About to look up association.\n");
1487 /* If a msg_name has been specified, assume this is to be used. */
1489 /* Look for a matching association on the endpoint. */
1490 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1492 /* If we could not find a matching association on the
1493 * endpoint, make sure that it is not a TCP-style
1494 * socket that already has an association or there is
1495 * no peeled-off association on another socket.
1497 if ((sctp_style(sk
, TCP
) &&
1498 sctp_sstate(sk
, ESTABLISHED
)) ||
1499 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1500 err
= -EADDRNOTAVAIL
;
1505 asoc
= sctp_id2assoc(sk
, associd
);
1513 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1515 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1516 * socket that has an association in CLOSED state. This can
1517 * happen when an accepted socket has an association that is
1520 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1525 if (sinfo_flags
& SCTP_EOF
) {
1526 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1528 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1532 if (sinfo_flags
& SCTP_ABORT
) {
1534 chunk
= sctp_make_abort_user(asoc
, msg
, msg_len
);
1540 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1541 sctp_primitive_ABORT(asoc
, chunk
);
1547 /* Do we need to create the association? */
1549 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1551 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1556 /* Check for invalid stream against the stream counts,
1557 * either the default or the user specified stream counts.
1560 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1561 /* Check against the defaults. */
1562 if (sinfo
->sinfo_stream
>=
1563 sp
->initmsg
.sinit_num_ostreams
) {
1568 /* Check against the requested. */
1569 if (sinfo
->sinfo_stream
>=
1570 sinit
->sinit_num_ostreams
) {
1578 * API 3.1.2 bind() - UDP Style Syntax
1579 * If a bind() or sctp_bindx() is not called prior to a
1580 * sendmsg() call that initiates a new association, the
1581 * system picks an ephemeral port and will choose an address
1582 * set equivalent to binding with a wildcard address.
1584 if (!ep
->base
.bind_addr
.port
) {
1585 if (sctp_autobind(sk
)) {
1591 * If an unprivileged user inherits a one-to-many
1592 * style socket with open associations on a privileged
1593 * port, it MAY be permitted to accept new associations,
1594 * but it SHOULD NOT be permitted to open new
1597 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1598 !capable(CAP_NET_BIND_SERVICE
)) {
1604 scope
= sctp_scope(&to
);
1605 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1612 /* If the SCTP_INIT ancillary data is specified, set all
1613 * the association init values accordingly.
1616 if (sinit
->sinit_num_ostreams
) {
1617 asoc
->c
.sinit_num_ostreams
=
1618 sinit
->sinit_num_ostreams
;
1620 if (sinit
->sinit_max_instreams
) {
1621 asoc
->c
.sinit_max_instreams
=
1622 sinit
->sinit_max_instreams
;
1624 if (sinit
->sinit_max_attempts
) {
1625 asoc
->max_init_attempts
1626 = sinit
->sinit_max_attempts
;
1628 if (sinit
->sinit_max_init_timeo
) {
1629 asoc
->max_init_timeo
=
1630 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1634 /* Prime the peer's transport structures. */
1635 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1640 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1647 /* ASSERT: we have a valid association at this point. */
1648 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1651 /* If the user didn't specify SNDRCVINFO, make up one with
1654 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1655 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1656 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1657 default_sinfo
.sinfo_context
= asoc
->default_context
;
1658 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1659 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1660 sinfo
= &default_sinfo
;
1663 /* API 7.1.7, the sndbuf size per association bounds the
1664 * maximum size of data that can be sent in a single send call.
1666 if (msg_len
> sk
->sk_sndbuf
) {
1671 if (asoc
->pmtu_pending
)
1672 sctp_assoc_pending_pmtu(asoc
);
1674 /* If fragmentation is disabled and the message length exceeds the
1675 * association fragmentation point, return EMSGSIZE. The I-D
1676 * does not specify what this error is, but this looks like
1679 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1685 /* Check for invalid stream. */
1686 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1692 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1693 if (!sctp_wspace(asoc
)) {
1694 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1699 /* If an address is passed with the sendto/sendmsg call, it is used
1700 * to override the primary destination address in the TCP model, or
1701 * when SCTP_ADDR_OVER flag is set in the UDP model.
1703 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1704 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1705 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1713 /* Auto-connect, if we aren't connected already. */
1714 if (sctp_state(asoc
, CLOSED
)) {
1715 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1718 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1721 /* Break the message into multiple chunks of maximum size. */
1722 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1728 /* Now send the (possibly) fragmented message. */
1729 list_for_each(pos
, &datamsg
->chunks
) {
1730 chunk
= list_entry(pos
, struct sctp_chunk
, frag_list
);
1731 sctp_datamsg_track(chunk
);
1733 /* Do accounting for the write space. */
1734 sctp_set_owner_w(chunk
);
1736 chunk
->transport
= chunk_tp
;
1738 /* Send it to the lower layers. Note: all chunks
1739 * must either fail or succeed. The lower layer
1740 * works that way today. Keep it that way or this
1743 err
= sctp_primitive_SEND(asoc
, chunk
);
1744 /* Did the lower layer accept the chunk? */
1746 sctp_chunk_free(chunk
);
1747 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1750 sctp_datamsg_free(datamsg
);
1756 /* If we are already past ASSOCIATE, the lower
1757 * layers are responsible for association cleanup.
1763 sctp_association_free(asoc
);
1765 sctp_release_sock(sk
);
1768 return sctp_error(sk
, msg_flags
, err
);
1775 err
= sock_error(sk
);
1785 /* This is an extended version of skb_pull() that removes the data from the
1786 * start of a skb even when data is spread across the list of skb's in the
1787 * frag_list. len specifies the total amount of data that needs to be removed.
1788 * when 'len' bytes could be removed from the skb, it returns 0.
1789 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1790 * could not be removed.
1792 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1794 struct sk_buff
*list
;
1795 int skb_len
= skb_headlen(skb
);
1798 if (len
<= skb_len
) {
1799 __skb_pull(skb
, len
);
1803 __skb_pull(skb
, skb_len
);
1805 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
) {
1806 rlen
= sctp_skb_pull(list
, len
);
1807 skb
->len
-= (len
-rlen
);
1808 skb
->data_len
-= (len
-rlen
);
1819 /* API 3.1.3 recvmsg() - UDP Style Syntax
1821 * ssize_t recvmsg(int socket, struct msghdr *message,
1824 * socket - the socket descriptor of the endpoint.
1825 * message - pointer to the msghdr structure which contains a single
1826 * user message and possibly some ancillary data.
1828 * See Section 5 for complete description of the data
1831 * flags - flags sent or received with the user message, see Section
1832 * 5 for complete description of the flags.
1834 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1836 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1837 struct msghdr
*msg
, size_t len
, int noblock
,
1838 int flags
, int *addr_len
)
1840 struct sctp_ulpevent
*event
= NULL
;
1841 struct sctp_sock
*sp
= sctp_sk(sk
);
1842 struct sk_buff
*skb
;
1847 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1848 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1849 "len", len
, "knoblauch", noblock
,
1850 "flags", flags
, "addr_len", addr_len
);
1854 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1859 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1863 /* Get the total length of the skb including any skb's in the
1872 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1874 event
= sctp_skb2event(skb
);
1879 sock_recv_timestamp(msg
, sk
, skb
);
1880 if (sctp_ulpevent_is_notification(event
)) {
1881 msg
->msg_flags
|= MSG_NOTIFICATION
;
1882 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1884 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1887 /* Check if we allow SCTP_SNDRCVINFO. */
1888 if (sp
->subscribe
.sctp_data_io_event
)
1889 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1891 /* FIXME: we should be calling IP/IPv6 layers. */
1892 if (sk
->sk_protinfo
.af_inet
.cmsg_flags
)
1893 ip_cmsg_recv(msg
, skb
);
1898 /* If skb's length exceeds the user's buffer, update the skb and
1899 * push it back to the receive_queue so that the next call to
1900 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1902 if (skb_len
> copied
) {
1903 msg
->msg_flags
&= ~MSG_EOR
;
1904 if (flags
& MSG_PEEK
)
1906 sctp_skb_pull(skb
, copied
);
1907 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1909 /* When only partial message is copied to the user, increase
1910 * rwnd by that amount. If all the data in the skb is read,
1911 * rwnd is updated when the event is freed.
1913 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1915 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1916 (event
->msg_flags
& MSG_EOR
))
1917 msg
->msg_flags
|= MSG_EOR
;
1919 msg
->msg_flags
&= ~MSG_EOR
;
1922 if (flags
& MSG_PEEK
) {
1923 /* Release the skb reference acquired after peeking the skb in
1924 * sctp_skb_recv_datagram().
1928 /* Free the event which includes releasing the reference to
1929 * the owner of the skb, freeing the skb and updating the
1932 sctp_ulpevent_free(event
);
1935 sctp_release_sock(sk
);
1939 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1941 * This option is a on/off flag. If enabled no SCTP message
1942 * fragmentation will be performed. Instead if a message being sent
1943 * exceeds the current PMTU size, the message will NOT be sent and
1944 * instead a error will be indicated to the user.
1946 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1947 char __user
*optval
, int optlen
)
1951 if (optlen
< sizeof(int))
1954 if (get_user(val
, (int __user
*)optval
))
1957 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1962 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1965 if (optlen
!= sizeof(struct sctp_event_subscribe
))
1967 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
1972 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1974 * This socket option is applicable to the UDP-style socket only. When
1975 * set it will cause associations that are idle for more than the
1976 * specified number of seconds to automatically close. An association
1977 * being idle is defined an association that has NOT sent or received
1978 * user data. The special value of '0' indicates that no automatic
1979 * close of any associations should be performed. The option expects an
1980 * integer defining the number of seconds of idle time before an
1981 * association is closed.
1983 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
1986 struct sctp_sock
*sp
= sctp_sk(sk
);
1988 /* Applicable to UDP-style socket only */
1989 if (sctp_style(sk
, TCP
))
1991 if (optlen
!= sizeof(int))
1993 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
1999 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2001 * Applications can enable or disable heartbeats for any peer address of
2002 * an association, modify an address's heartbeat interval, force a
2003 * heartbeat to be sent immediately, and adjust the address's maximum
2004 * number of retransmissions sent before an address is considered
2005 * unreachable. The following structure is used to access and modify an
2006 * address's parameters:
2008 * struct sctp_paddrparams {
2009 * sctp_assoc_t spp_assoc_id;
2010 * struct sockaddr_storage spp_address;
2011 * uint32_t spp_hbinterval;
2012 * uint16_t spp_pathmaxrxt;
2013 * uint32_t spp_pathmtu;
2014 * uint32_t spp_sackdelay;
2015 * uint32_t spp_flags;
2018 * spp_assoc_id - (one-to-many style socket) This is filled in the
2019 * application, and identifies the association for
2021 * spp_address - This specifies which address is of interest.
2022 * spp_hbinterval - This contains the value of the heartbeat interval,
2023 * in milliseconds. If a value of zero
2024 * is present in this field then no changes are to
2025 * be made to this parameter.
2026 * spp_pathmaxrxt - This contains the maximum number of
2027 * retransmissions before this address shall be
2028 * considered unreachable. If a value of zero
2029 * is present in this field then no changes are to
2030 * be made to this parameter.
2031 * spp_pathmtu - When Path MTU discovery is disabled the value
2032 * specified here will be the "fixed" path mtu.
2033 * Note that if the spp_address field is empty
2034 * then all associations on this address will
2035 * have this fixed path mtu set upon them.
2037 * spp_sackdelay - When delayed sack is enabled, this value specifies
2038 * the number of milliseconds that sacks will be delayed
2039 * for. This value will apply to all addresses of an
2040 * association if the spp_address field is empty. Note
2041 * also, that if delayed sack is enabled and this
2042 * value is set to 0, no change is made to the last
2043 * recorded delayed sack timer value.
2045 * spp_flags - These flags are used to control various features
2046 * on an association. The flag field may contain
2047 * zero or more of the following options.
2049 * SPP_HB_ENABLE - Enable heartbeats on the
2050 * specified address. Note that if the address
2051 * field is empty all addresses for the association
2052 * have heartbeats enabled upon them.
2054 * SPP_HB_DISABLE - Disable heartbeats on the
2055 * speicifed address. Note that if the address
2056 * field is empty all addresses for the association
2057 * will have their heartbeats disabled. Note also
2058 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2059 * mutually exclusive, only one of these two should
2060 * be specified. Enabling both fields will have
2061 * undetermined results.
2063 * SPP_HB_DEMAND - Request a user initiated heartbeat
2064 * to be made immediately.
2066 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2067 * heartbeat delayis to be set to the value of 0
2070 * SPP_PMTUD_ENABLE - This field will enable PMTU
2071 * discovery upon the specified address. Note that
2072 * if the address feild is empty then all addresses
2073 * on the association are effected.
2075 * SPP_PMTUD_DISABLE - This field will disable PMTU
2076 * discovery upon the specified address. Note that
2077 * if the address feild is empty then all addresses
2078 * on the association are effected. Not also that
2079 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2080 * exclusive. Enabling both will have undetermined
2083 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2084 * on delayed sack. The time specified in spp_sackdelay
2085 * is used to specify the sack delay for this address. Note
2086 * that if spp_address is empty then all addresses will
2087 * enable delayed sack and take on the sack delay
2088 * value specified in spp_sackdelay.
2089 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2090 * off delayed sack. If the spp_address field is blank then
2091 * delayed sack is disabled for the entire association. Note
2092 * also that this field is mutually exclusive to
2093 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2096 static int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2097 struct sctp_transport
*trans
,
2098 struct sctp_association
*asoc
,
2099 struct sctp_sock
*sp
,
2102 int sackdelay_change
)
2106 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2107 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2112 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2113 * this field is ignored. Note also that a value of zero indicates
2114 * the current setting should be left unchanged.
2116 if (params
->spp_flags
& SPP_HB_ENABLE
) {
2118 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2119 * set. This lets us use 0 value when this flag
2122 if (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)
2123 params
->spp_hbinterval
= 0;
2125 if (params
->spp_hbinterval
||
2126 (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)) {
2129 msecs_to_jiffies(params
->spp_hbinterval
);
2132 msecs_to_jiffies(params
->spp_hbinterval
);
2134 sp
->hbinterval
= params
->spp_hbinterval
;
2141 trans
->param_flags
=
2142 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2145 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2148 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2152 /* When Path MTU discovery is disabled the value specified here will
2153 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2154 * include the flag SPP_PMTUD_DISABLE for this field to have any
2157 if ((params
->spp_flags
& SPP_PMTUD_DISABLE
) && params
->spp_pathmtu
) {
2159 trans
->pathmtu
= params
->spp_pathmtu
;
2160 sctp_assoc_sync_pmtu(asoc
);
2162 asoc
->pathmtu
= params
->spp_pathmtu
;
2163 sctp_frag_point(sp
, params
->spp_pathmtu
);
2165 sp
->pathmtu
= params
->spp_pathmtu
;
2171 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2172 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2173 trans
->param_flags
=
2174 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2176 sctp_transport_pmtu(trans
);
2177 sctp_assoc_sync_pmtu(asoc
);
2181 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2184 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2188 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2189 * value of this field is ignored. Note also that a value of zero
2190 * indicates the current setting should be left unchanged.
2192 if ((params
->spp_flags
& SPP_SACKDELAY_ENABLE
) && params
->spp_sackdelay
) {
2195 msecs_to_jiffies(params
->spp_sackdelay
);
2198 msecs_to_jiffies(params
->spp_sackdelay
);
2200 sp
->sackdelay
= params
->spp_sackdelay
;
2204 if (sackdelay_change
) {
2206 trans
->param_flags
=
2207 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2211 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2215 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2220 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2221 * of this field is ignored. Note also that a value of zero
2222 * indicates the current setting should be left unchanged.
2224 if ((params
->spp_flags
& SPP_PMTUD_ENABLE
) && params
->spp_pathmaxrxt
) {
2226 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2228 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2230 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2237 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2238 char __user
*optval
, int optlen
)
2240 struct sctp_paddrparams params
;
2241 struct sctp_transport
*trans
= NULL
;
2242 struct sctp_association
*asoc
= NULL
;
2243 struct sctp_sock
*sp
= sctp_sk(sk
);
2245 int hb_change
, pmtud_change
, sackdelay_change
;
2247 if (optlen
!= sizeof(struct sctp_paddrparams
))
2250 if (copy_from_user(¶ms
, optval
, optlen
))
2253 /* Validate flags and value parameters. */
2254 hb_change
= params
.spp_flags
& SPP_HB
;
2255 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2256 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2258 if (hb_change
== SPP_HB
||
2259 pmtud_change
== SPP_PMTUD
||
2260 sackdelay_change
== SPP_SACKDELAY
||
2261 params
.spp_sackdelay
> 500 ||
2263 && params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2266 /* If an address other than INADDR_ANY is specified, and
2267 * no transport is found, then the request is invalid.
2269 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
2270 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2271 params
.spp_assoc_id
);
2276 /* Get association, if assoc_id != 0 and the socket is a one
2277 * to many style socket, and an association was not found, then
2278 * the id was invalid.
2280 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2281 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2284 /* Heartbeat demand can only be sent on a transport or
2285 * association, but not a socket.
2287 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2290 /* Process parameters. */
2291 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2292 hb_change
, pmtud_change
,
2298 /* If changes are for association, also apply parameters to each
2301 if (!trans
&& asoc
) {
2302 struct list_head
*pos
;
2304 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2305 trans
= list_entry(pos
, struct sctp_transport
,
2307 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2308 hb_change
, pmtud_change
,
2316 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2318 * This options will get or set the delayed ack timer. The time is set
2319 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2320 * endpoints default delayed ack timer value. If the assoc_id field is
2321 * non-zero, then the set or get effects the specified association.
2323 * struct sctp_assoc_value {
2324 * sctp_assoc_t assoc_id;
2325 * uint32_t assoc_value;
2328 * assoc_id - This parameter, indicates which association the
2329 * user is preforming an action upon. Note that if
2330 * this field's value is zero then the endpoints
2331 * default value is changed (effecting future
2332 * associations only).
2334 * assoc_value - This parameter contains the number of milliseconds
2335 * that the user is requesting the delayed ACK timer
2336 * be set to. Note that this value is defined in
2337 * the standard to be between 200 and 500 milliseconds.
2339 * Note: a value of zero will leave the value alone,
2340 * but disable SACK delay. A non-zero value will also
2341 * enable SACK delay.
2344 static int sctp_setsockopt_delayed_ack_time(struct sock
*sk
,
2345 char __user
*optval
, int optlen
)
2347 struct sctp_assoc_value params
;
2348 struct sctp_transport
*trans
= NULL
;
2349 struct sctp_association
*asoc
= NULL
;
2350 struct sctp_sock
*sp
= sctp_sk(sk
);
2352 if (optlen
!= sizeof(struct sctp_assoc_value
))
2355 if (copy_from_user(¶ms
, optval
, optlen
))
2358 /* Validate value parameter. */
2359 if (params
.assoc_value
> 500)
2362 /* Get association, if assoc_id != 0 and the socket is a one
2363 * to many style socket, and an association was not found, then
2364 * the id was invalid.
2366 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2367 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2370 if (params
.assoc_value
) {
2373 msecs_to_jiffies(params
.assoc_value
);
2375 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2376 SPP_SACKDELAY_ENABLE
;
2378 sp
->sackdelay
= params
.assoc_value
;
2380 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2381 SPP_SACKDELAY_ENABLE
;
2386 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2387 SPP_SACKDELAY_DISABLE
;
2390 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2391 SPP_SACKDELAY_DISABLE
;
2395 /* If change is for association, also apply to each transport. */
2397 struct list_head
*pos
;
2399 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2400 trans
= list_entry(pos
, struct sctp_transport
,
2402 if (params
.assoc_value
) {
2404 msecs_to_jiffies(params
.assoc_value
);
2405 trans
->param_flags
=
2406 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2407 SPP_SACKDELAY_ENABLE
;
2409 trans
->param_flags
=
2410 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2411 SPP_SACKDELAY_DISABLE
;
2419 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2421 * Applications can specify protocol parameters for the default association
2422 * initialization. The option name argument to setsockopt() and getsockopt()
2425 * Setting initialization parameters is effective only on an unconnected
2426 * socket (for UDP-style sockets only future associations are effected
2427 * by the change). With TCP-style sockets, this option is inherited by
2428 * sockets derived from a listener socket.
2430 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, int optlen
)
2432 struct sctp_initmsg sinit
;
2433 struct sctp_sock
*sp
= sctp_sk(sk
);
2435 if (optlen
!= sizeof(struct sctp_initmsg
))
2437 if (copy_from_user(&sinit
, optval
, optlen
))
2440 if (sinit
.sinit_num_ostreams
)
2441 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2442 if (sinit
.sinit_max_instreams
)
2443 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2444 if (sinit
.sinit_max_attempts
)
2445 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2446 if (sinit
.sinit_max_init_timeo
)
2447 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2453 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2455 * Applications that wish to use the sendto() system call may wish to
2456 * specify a default set of parameters that would normally be supplied
2457 * through the inclusion of ancillary data. This socket option allows
2458 * such an application to set the default sctp_sndrcvinfo structure.
2459 * The application that wishes to use this socket option simply passes
2460 * in to this call the sctp_sndrcvinfo structure defined in Section
2461 * 5.2.2) The input parameters accepted by this call include
2462 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2463 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2464 * to this call if the caller is using the UDP model.
2466 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2467 char __user
*optval
, int optlen
)
2469 struct sctp_sndrcvinfo info
;
2470 struct sctp_association
*asoc
;
2471 struct sctp_sock
*sp
= sctp_sk(sk
);
2473 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2475 if (copy_from_user(&info
, optval
, optlen
))
2478 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2479 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2483 asoc
->default_stream
= info
.sinfo_stream
;
2484 asoc
->default_flags
= info
.sinfo_flags
;
2485 asoc
->default_ppid
= info
.sinfo_ppid
;
2486 asoc
->default_context
= info
.sinfo_context
;
2487 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2489 sp
->default_stream
= info
.sinfo_stream
;
2490 sp
->default_flags
= info
.sinfo_flags
;
2491 sp
->default_ppid
= info
.sinfo_ppid
;
2492 sp
->default_context
= info
.sinfo_context
;
2493 sp
->default_timetolive
= info
.sinfo_timetolive
;
2499 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2501 * Requests that the local SCTP stack use the enclosed peer address as
2502 * the association primary. The enclosed address must be one of the
2503 * association peer's addresses.
2505 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2508 struct sctp_prim prim
;
2509 struct sctp_transport
*trans
;
2511 if (optlen
!= sizeof(struct sctp_prim
))
2514 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2517 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2521 sctp_assoc_set_primary(trans
->asoc
, trans
);
2527 * 7.1.5 SCTP_NODELAY
2529 * Turn on/off any Nagle-like algorithm. This means that packets are
2530 * generally sent as soon as possible and no unnecessary delays are
2531 * introduced, at the cost of more packets in the network. Expects an
2532 * integer boolean flag.
2534 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2539 if (optlen
< sizeof(int))
2541 if (get_user(val
, (int __user
*)optval
))
2544 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2550 * 7.1.1 SCTP_RTOINFO
2552 * The protocol parameters used to initialize and bound retransmission
2553 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2554 * and modify these parameters.
2555 * All parameters are time values, in milliseconds. A value of 0, when
2556 * modifying the parameters, indicates that the current value should not
2560 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, int optlen
) {
2561 struct sctp_rtoinfo rtoinfo
;
2562 struct sctp_association
*asoc
;
2564 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2567 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2570 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2572 /* Set the values to the specific association */
2573 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2577 if (rtoinfo
.srto_initial
!= 0)
2579 msecs_to_jiffies(rtoinfo
.srto_initial
);
2580 if (rtoinfo
.srto_max
!= 0)
2581 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2582 if (rtoinfo
.srto_min
!= 0)
2583 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2585 /* If there is no association or the association-id = 0
2586 * set the values to the endpoint.
2588 struct sctp_sock
*sp
= sctp_sk(sk
);
2590 if (rtoinfo
.srto_initial
!= 0)
2591 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2592 if (rtoinfo
.srto_max
!= 0)
2593 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2594 if (rtoinfo
.srto_min
!= 0)
2595 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2603 * 7.1.2 SCTP_ASSOCINFO
2605 * This option is used to tune the maximum retransmission attempts
2606 * of the association.
2607 * Returns an error if the new association retransmission value is
2608 * greater than the sum of the retransmission value of the peer.
2609 * See [SCTP] for more information.
2612 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, int optlen
)
2615 struct sctp_assocparams assocparams
;
2616 struct sctp_association
*asoc
;
2618 if (optlen
!= sizeof(struct sctp_assocparams
))
2620 if (copy_from_user(&assocparams
, optval
, optlen
))
2623 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2625 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2628 /* Set the values to the specific association */
2630 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2633 struct list_head
*pos
;
2634 struct sctp_transport
*peer_addr
;
2636 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2637 peer_addr
= list_entry(pos
,
2638 struct sctp_transport
,
2640 path_sum
+= peer_addr
->pathmaxrxt
;
2644 /* Only validate asocmaxrxt if we have more then
2645 * one path/transport. We do this because path
2646 * retransmissions are only counted when we have more
2650 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2653 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2656 if (assocparams
.sasoc_cookie_life
!= 0) {
2657 asoc
->cookie_life
.tv_sec
=
2658 assocparams
.sasoc_cookie_life
/ 1000;
2659 asoc
->cookie_life
.tv_usec
=
2660 (assocparams
.sasoc_cookie_life
% 1000)
2664 /* Set the values to the endpoint */
2665 struct sctp_sock
*sp
= sctp_sk(sk
);
2667 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2668 sp
->assocparams
.sasoc_asocmaxrxt
=
2669 assocparams
.sasoc_asocmaxrxt
;
2670 if (assocparams
.sasoc_cookie_life
!= 0)
2671 sp
->assocparams
.sasoc_cookie_life
=
2672 assocparams
.sasoc_cookie_life
;
2678 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2680 * This socket option is a boolean flag which turns on or off mapped V4
2681 * addresses. If this option is turned on and the socket is type
2682 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2683 * If this option is turned off, then no mapping will be done of V4
2684 * addresses and a user will receive both PF_INET6 and PF_INET type
2685 * addresses on the socket.
2687 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, int optlen
)
2690 struct sctp_sock
*sp
= sctp_sk(sk
);
2692 if (optlen
< sizeof(int))
2694 if (get_user(val
, (int __user
*)optval
))
2705 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2707 * This socket option specifies the maximum size to put in any outgoing
2708 * SCTP chunk. If a message is larger than this size it will be
2709 * fragmented by SCTP into the specified size. Note that the underlying
2710 * SCTP implementation may fragment into smaller sized chunks when the
2711 * PMTU of the underlying association is smaller than the value set by
2714 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, int optlen
)
2716 struct sctp_association
*asoc
;
2717 struct list_head
*pos
;
2718 struct sctp_sock
*sp
= sctp_sk(sk
);
2721 if (optlen
< sizeof(int))
2723 if (get_user(val
, (int __user
*)optval
))
2725 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2727 sp
->user_frag
= val
;
2729 /* Update the frag_point of the existing associations. */
2730 list_for_each(pos
, &(sp
->ep
->asocs
)) {
2731 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
2732 asoc
->frag_point
= sctp_frag_point(sp
, asoc
->pathmtu
);
2740 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2742 * Requests that the peer mark the enclosed address as the association
2743 * primary. The enclosed address must be one of the association's
2744 * locally bound addresses. The following structure is used to make a
2745 * set primary request:
2747 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2750 struct sctp_sock
*sp
;
2751 struct sctp_endpoint
*ep
;
2752 struct sctp_association
*asoc
= NULL
;
2753 struct sctp_setpeerprim prim
;
2754 struct sctp_chunk
*chunk
;
2760 if (!sctp_addip_enable
)
2763 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2766 if (copy_from_user(&prim
, optval
, optlen
))
2769 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2773 if (!asoc
->peer
.asconf_capable
)
2776 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2779 if (!sctp_state(asoc
, ESTABLISHED
))
2782 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2783 return -EADDRNOTAVAIL
;
2785 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2786 chunk
= sctp_make_asconf_set_prim(asoc
,
2787 (union sctp_addr
*)&prim
.sspp_addr
);
2791 err
= sctp_send_asconf(asoc
, chunk
);
2793 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2798 static int sctp_setsockopt_adaptation_layer(struct sock
*sk
, char __user
*optval
,
2801 struct sctp_setadaptation adaptation
;
2803 if (optlen
!= sizeof(struct sctp_setadaptation
))
2805 if (copy_from_user(&adaptation
, optval
, optlen
))
2808 sctp_sk(sk
)->adaptation_ind
= adaptation
.ssb_adaptation_ind
;
2814 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2816 * The context field in the sctp_sndrcvinfo structure is normally only
2817 * used when a failed message is retrieved holding the value that was
2818 * sent down on the actual send call. This option allows the setting of
2819 * a default context on an association basis that will be received on
2820 * reading messages from the peer. This is especially helpful in the
2821 * one-2-many model for an application to keep some reference to an
2822 * internal state machine that is processing messages on the
2823 * association. Note that the setting of this value only effects
2824 * received messages from the peer and does not effect the value that is
2825 * saved with outbound messages.
2827 static int sctp_setsockopt_context(struct sock
*sk
, char __user
*optval
,
2830 struct sctp_assoc_value params
;
2831 struct sctp_sock
*sp
;
2832 struct sctp_association
*asoc
;
2834 if (optlen
!= sizeof(struct sctp_assoc_value
))
2836 if (copy_from_user(¶ms
, optval
, optlen
))
2841 if (params
.assoc_id
!= 0) {
2842 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2845 asoc
->default_rcv_context
= params
.assoc_value
;
2847 sp
->default_rcv_context
= params
.assoc_value
;
2854 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2856 * This options will at a minimum specify if the implementation is doing
2857 * fragmented interleave. Fragmented interleave, for a one to many
2858 * socket, is when subsequent calls to receive a message may return
2859 * parts of messages from different associations. Some implementations
2860 * may allow you to turn this value on or off. If so, when turned off,
2861 * no fragment interleave will occur (which will cause a head of line
2862 * blocking amongst multiple associations sharing the same one to many
2863 * socket). When this option is turned on, then each receive call may
2864 * come from a different association (thus the user must receive data
2865 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2866 * association each receive belongs to.
2868 * This option takes a boolean value. A non-zero value indicates that
2869 * fragmented interleave is on. A value of zero indicates that
2870 * fragmented interleave is off.
2872 * Note that it is important that an implementation that allows this
2873 * option to be turned on, have it off by default. Otherwise an unaware
2874 * application using the one to many model may become confused and act
2877 static int sctp_setsockopt_fragment_interleave(struct sock
*sk
,
2878 char __user
*optval
,
2883 if (optlen
!= sizeof(int))
2885 if (get_user(val
, (int __user
*)optval
))
2888 sctp_sk(sk
)->frag_interleave
= (val
== 0) ? 0 : 1;
2894 * 7.1.25. Set or Get the sctp partial delivery point
2895 * (SCTP_PARTIAL_DELIVERY_POINT)
2896 * This option will set or get the SCTP partial delivery point. This
2897 * point is the size of a message where the partial delivery API will be
2898 * invoked to help free up rwnd space for the peer. Setting this to a
2899 * lower value will cause partial delivery's to happen more often. The
2900 * calls argument is an integer that sets or gets the partial delivery
2903 static int sctp_setsockopt_partial_delivery_point(struct sock
*sk
,
2904 char __user
*optval
,
2909 if (optlen
!= sizeof(u32
))
2911 if (get_user(val
, (int __user
*)optval
))
2914 sctp_sk(sk
)->pd_point
= val
;
2916 return 0; /* is this the right error code? */
2920 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2922 * This option will allow a user to change the maximum burst of packets
2923 * that can be emitted by this association. Note that the default value
2924 * is 4, and some implementations may restrict this setting so that it
2925 * can only be lowered.
2927 * NOTE: This text doesn't seem right. Do this on a socket basis with
2928 * future associations inheriting the socket value.
2930 static int sctp_setsockopt_maxburst(struct sock
*sk
,
2931 char __user
*optval
,
2936 if (optlen
!= sizeof(int))
2938 if (get_user(val
, (int __user
*)optval
))
2944 sctp_sk(sk
)->max_burst
= val
;
2949 /* API 6.2 setsockopt(), getsockopt()
2951 * Applications use setsockopt() and getsockopt() to set or retrieve
2952 * socket options. Socket options are used to change the default
2953 * behavior of sockets calls. They are described in Section 7.
2957 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2958 * int __user *optlen);
2959 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2962 * sd - the socket descript.
2963 * level - set to IPPROTO_SCTP for all SCTP options.
2964 * optname - the option name.
2965 * optval - the buffer to store the value of the option.
2966 * optlen - the size of the buffer.
2968 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
2969 char __user
*optval
, int optlen
)
2973 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2976 /* I can hardly begin to describe how wrong this is. This is
2977 * so broken as to be worse than useless. The API draft
2978 * REALLY is NOT helpful here... I am not convinced that the
2979 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2980 * are at all well-founded.
2982 if (level
!= SOL_SCTP
) {
2983 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
2984 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
2991 case SCTP_SOCKOPT_BINDX_ADD
:
2992 /* 'optlen' is the size of the addresses buffer. */
2993 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2994 optlen
, SCTP_BINDX_ADD_ADDR
);
2997 case SCTP_SOCKOPT_BINDX_REM
:
2998 /* 'optlen' is the size of the addresses buffer. */
2999 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
3000 optlen
, SCTP_BINDX_REM_ADDR
);
3003 case SCTP_SOCKOPT_CONNECTX
:
3004 /* 'optlen' is the size of the addresses buffer. */
3005 retval
= sctp_setsockopt_connectx(sk
, (struct sockaddr __user
*)optval
,
3009 case SCTP_DISABLE_FRAGMENTS
:
3010 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
3014 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
3017 case SCTP_AUTOCLOSE
:
3018 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
3021 case SCTP_PEER_ADDR_PARAMS
:
3022 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
3025 case SCTP_DELAYED_ACK_TIME
:
3026 retval
= sctp_setsockopt_delayed_ack_time(sk
, optval
, optlen
);
3028 case SCTP_PARTIAL_DELIVERY_POINT
:
3029 retval
= sctp_setsockopt_partial_delivery_point(sk
, optval
, optlen
);
3033 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
3035 case SCTP_DEFAULT_SEND_PARAM
:
3036 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
3039 case SCTP_PRIMARY_ADDR
:
3040 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
3042 case SCTP_SET_PEER_PRIMARY_ADDR
:
3043 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
3046 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
3049 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
3051 case SCTP_ASSOCINFO
:
3052 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
3054 case SCTP_I_WANT_MAPPED_V4_ADDR
:
3055 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
3058 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
3060 case SCTP_ADAPTATION_LAYER
:
3061 retval
= sctp_setsockopt_adaptation_layer(sk
, optval
, optlen
);
3064 retval
= sctp_setsockopt_context(sk
, optval
, optlen
);
3066 case SCTP_FRAGMENT_INTERLEAVE
:
3067 retval
= sctp_setsockopt_fragment_interleave(sk
, optval
, optlen
);
3069 case SCTP_MAX_BURST
:
3070 retval
= sctp_setsockopt_maxburst(sk
, optval
, optlen
);
3073 retval
= -ENOPROTOOPT
;
3077 sctp_release_sock(sk
);
3083 /* API 3.1.6 connect() - UDP Style Syntax
3085 * An application may use the connect() call in the UDP model to initiate an
3086 * association without sending data.
3090 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3092 * sd: the socket descriptor to have a new association added to.
3094 * nam: the address structure (either struct sockaddr_in or struct
3095 * sockaddr_in6 defined in RFC2553 [7]).
3097 * len: the size of the address.
3099 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
3107 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3108 __FUNCTION__
, sk
, addr
, addr_len
);
3110 /* Validate addr_len before calling common connect/connectx routine. */
3111 af
= sctp_get_af_specific(addr
->sa_family
);
3112 if (!af
|| addr_len
< af
->sockaddr_len
) {
3115 /* Pass correct addr len to common routine (so it knows there
3116 * is only one address being passed.
3118 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
);
3121 sctp_release_sock(sk
);
3125 /* FIXME: Write comments. */
3126 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
3128 return -EOPNOTSUPP
; /* STUB */
3131 /* 4.1.4 accept() - TCP Style Syntax
3133 * Applications use accept() call to remove an established SCTP
3134 * association from the accept queue of the endpoint. A new socket
3135 * descriptor will be returned from accept() to represent the newly
3136 * formed association.
3138 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
3140 struct sctp_sock
*sp
;
3141 struct sctp_endpoint
*ep
;
3142 struct sock
*newsk
= NULL
;
3143 struct sctp_association
*asoc
;
3152 if (!sctp_style(sk
, TCP
)) {
3153 error
= -EOPNOTSUPP
;
3157 if (!sctp_sstate(sk
, LISTENING
)) {
3162 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
3164 error
= sctp_wait_for_accept(sk
, timeo
);
3168 /* We treat the list of associations on the endpoint as the accept
3169 * queue and pick the first association on the list.
3171 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
3173 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
3179 /* Populate the fields of the newsk from the oldsk and migrate the
3180 * asoc to the newsk.
3182 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
3185 sctp_release_sock(sk
);
3190 /* The SCTP ioctl handler. */
3191 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
3193 return -ENOIOCTLCMD
;
3196 /* This is the function which gets called during socket creation to
3197 * initialized the SCTP-specific portion of the sock.
3198 * The sock structure should already be zero-filled memory.
3200 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
3202 struct sctp_endpoint
*ep
;
3203 struct sctp_sock
*sp
;
3205 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
3209 /* Initialize the SCTP per socket area. */
3210 switch (sk
->sk_type
) {
3211 case SOCK_SEQPACKET
:
3212 sp
->type
= SCTP_SOCKET_UDP
;
3215 sp
->type
= SCTP_SOCKET_TCP
;
3218 return -ESOCKTNOSUPPORT
;
3221 /* Initialize default send parameters. These parameters can be
3222 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3224 sp
->default_stream
= 0;
3225 sp
->default_ppid
= 0;
3226 sp
->default_flags
= 0;
3227 sp
->default_context
= 0;
3228 sp
->default_timetolive
= 0;
3230 sp
->default_rcv_context
= 0;
3231 sp
->max_burst
= sctp_max_burst
;
3233 /* Initialize default setup parameters. These parameters
3234 * can be modified with the SCTP_INITMSG socket option or
3235 * overridden by the SCTP_INIT CMSG.
3237 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
3238 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
3239 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
3240 sp
->initmsg
.sinit_max_init_timeo
= sctp_rto_max
;
3242 /* Initialize default RTO related parameters. These parameters can
3243 * be modified for with the SCTP_RTOINFO socket option.
3245 sp
->rtoinfo
.srto_initial
= sctp_rto_initial
;
3246 sp
->rtoinfo
.srto_max
= sctp_rto_max
;
3247 sp
->rtoinfo
.srto_min
= sctp_rto_min
;
3249 /* Initialize default association related parameters. These parameters
3250 * can be modified with the SCTP_ASSOCINFO socket option.
3252 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3253 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3254 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3255 sp
->assocparams
.sasoc_local_rwnd
= 0;
3256 sp
->assocparams
.sasoc_cookie_life
= sctp_valid_cookie_life
;
3258 /* Initialize default event subscriptions. By default, all the
3261 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3263 /* Default Peer Address Parameters. These defaults can
3264 * be modified via SCTP_PEER_ADDR_PARAMS
3266 sp
->hbinterval
= sctp_hb_interval
;
3267 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3268 sp
->pathmtu
= 0; // allow default discovery
3269 sp
->sackdelay
= sctp_sack_timeout
;
3270 sp
->param_flags
= SPP_HB_ENABLE
|
3272 SPP_SACKDELAY_ENABLE
;
3274 /* If enabled no SCTP message fragmentation will be performed.
3275 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3277 sp
->disable_fragments
= 0;
3279 /* Enable Nagle algorithm by default. */
3282 /* Enable by default. */
3285 /* Auto-close idle associations after the configured
3286 * number of seconds. A value of 0 disables this
3287 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3288 * for UDP-style sockets only.
3292 /* User specified fragmentation limit. */
3295 sp
->adaptation_ind
= 0;
3297 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3299 /* Control variables for partial data delivery. */
3300 atomic_set(&sp
->pd_mode
, 0);
3301 skb_queue_head_init(&sp
->pd_lobby
);
3302 sp
->frag_interleave
= 0;
3304 /* Create a per socket endpoint structure. Even if we
3305 * change the data structure relationships, this may still
3306 * be useful for storing pre-connect address information.
3308 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3315 SCTP_DBG_OBJCNT_INC(sock
);
3316 atomic_inc(&sctp_sockets_allocated
);
3320 /* Cleanup any SCTP per socket resources. */
3321 SCTP_STATIC
int sctp_destroy_sock(struct sock
*sk
)
3323 struct sctp_endpoint
*ep
;
3325 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3327 /* Release our hold on the endpoint. */
3328 ep
= sctp_sk(sk
)->ep
;
3329 sctp_endpoint_free(ep
);
3330 atomic_dec(&sctp_sockets_allocated
);
3334 /* API 4.1.7 shutdown() - TCP Style Syntax
3335 * int shutdown(int socket, int how);
3337 * sd - the socket descriptor of the association to be closed.
3338 * how - Specifies the type of shutdown. The values are
3341 * Disables further receive operations. No SCTP
3342 * protocol action is taken.
3344 * Disables further send operations, and initiates
3345 * the SCTP shutdown sequence.
3347 * Disables further send and receive operations
3348 * and initiates the SCTP shutdown sequence.
3350 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3352 struct sctp_endpoint
*ep
;
3353 struct sctp_association
*asoc
;
3355 if (!sctp_style(sk
, TCP
))
3358 if (how
& SEND_SHUTDOWN
) {
3359 ep
= sctp_sk(sk
)->ep
;
3360 if (!list_empty(&ep
->asocs
)) {
3361 asoc
= list_entry(ep
->asocs
.next
,
3362 struct sctp_association
, asocs
);
3363 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3368 /* 7.2.1 Association Status (SCTP_STATUS)
3370 * Applications can retrieve current status information about an
3371 * association, including association state, peer receiver window size,
3372 * number of unacked data chunks, and number of data chunks pending
3373 * receipt. This information is read-only.
3375 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3376 char __user
*optval
,
3379 struct sctp_status status
;
3380 struct sctp_association
*asoc
= NULL
;
3381 struct sctp_transport
*transport
;
3382 sctp_assoc_t associd
;
3385 if (len
< sizeof(status
)) {
3390 len
= sizeof(status
);
3391 if (copy_from_user(&status
, optval
, len
)) {
3396 associd
= status
.sstat_assoc_id
;
3397 asoc
= sctp_id2assoc(sk
, associd
);
3403 transport
= asoc
->peer
.primary_path
;
3405 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3406 status
.sstat_state
= asoc
->state
;
3407 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3408 status
.sstat_unackdata
= asoc
->unack_data
;
3410 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3411 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3412 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3413 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3414 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3415 memcpy(&status
.sstat_primary
.spinfo_address
, &transport
->ipaddr
,
3416 transport
->af_specific
->sockaddr_len
);
3417 /* Map ipv4 address into v4-mapped-on-v6 address. */
3418 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3419 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3420 status
.sstat_primary
.spinfo_state
= transport
->state
;
3421 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3422 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3423 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3424 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3426 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3427 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3429 if (put_user(len
, optlen
)) {
3434 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3435 len
, status
.sstat_state
, status
.sstat_rwnd
,
3436 status
.sstat_assoc_id
);
3438 if (copy_to_user(optval
, &status
, len
)) {
3448 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3450 * Applications can retrieve information about a specific peer address
3451 * of an association, including its reachability state, congestion
3452 * window, and retransmission timer values. This information is
3455 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3456 char __user
*optval
,
3459 struct sctp_paddrinfo pinfo
;
3460 struct sctp_transport
*transport
;
3463 if (len
< sizeof(pinfo
)) {
3468 len
= sizeof(pinfo
);
3469 if (copy_from_user(&pinfo
, optval
, len
)) {
3474 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3475 pinfo
.spinfo_assoc_id
);
3479 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3480 pinfo
.spinfo_state
= transport
->state
;
3481 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3482 pinfo
.spinfo_srtt
= transport
->srtt
;
3483 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3484 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3486 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3487 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3489 if (put_user(len
, optlen
)) {
3494 if (copy_to_user(optval
, &pinfo
, len
)) {
3503 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3505 * This option is a on/off flag. If enabled no SCTP message
3506 * fragmentation will be performed. Instead if a message being sent
3507 * exceeds the current PMTU size, the message will NOT be sent and
3508 * instead a error will be indicated to the user.
3510 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3511 char __user
*optval
, int __user
*optlen
)
3515 if (len
< sizeof(int))
3519 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3520 if (put_user(len
, optlen
))
3522 if (copy_to_user(optval
, &val
, len
))
3527 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3529 * This socket option is used to specify various notifications and
3530 * ancillary data the user wishes to receive.
3532 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3535 if (len
< sizeof(struct sctp_event_subscribe
))
3537 len
= sizeof(struct sctp_event_subscribe
);
3538 if (put_user(len
, optlen
))
3540 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3545 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3547 * This socket option is applicable to the UDP-style socket only. When
3548 * set it will cause associations that are idle for more than the
3549 * specified number of seconds to automatically close. An association
3550 * being idle is defined an association that has NOT sent or received
3551 * user data. The special value of '0' indicates that no automatic
3552 * close of any associations should be performed. The option expects an
3553 * integer defining the number of seconds of idle time before an
3554 * association is closed.
3556 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3558 /* Applicable to UDP-style socket only */
3559 if (sctp_style(sk
, TCP
))
3561 if (len
< sizeof(int))
3564 if (put_user(len
, optlen
))
3566 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, sizeof(int)))
3571 /* Helper routine to branch off an association to a new socket. */
3572 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3573 struct socket
**sockp
)
3575 struct sock
*sk
= asoc
->base
.sk
;
3576 struct socket
*sock
;
3577 struct inet_sock
*inetsk
;
3581 /* An association cannot be branched off from an already peeled-off
3582 * socket, nor is this supported for tcp style sockets.
3584 if (!sctp_style(sk
, UDP
))
3587 /* Create a new socket. */
3588 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3592 /* Populate the fields of the newsk from the oldsk and migrate the
3593 * asoc to the newsk.
3595 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3597 /* Make peeled-off sockets more like 1-1 accepted sockets.
3598 * Set the daddr and initialize id to something more random
3600 af
= sctp_get_af_specific(asoc
->peer
.primary_addr
.sa
.sa_family
);
3601 af
->to_sk_daddr(&asoc
->peer
.primary_addr
, sk
);
3602 inetsk
= inet_sk(sock
->sk
);
3603 inetsk
->id
= asoc
->next_tsn
^ jiffies
;
3610 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3612 sctp_peeloff_arg_t peeloff
;
3613 struct socket
*newsock
;
3615 struct sctp_association
*asoc
;
3617 if (len
< sizeof(sctp_peeloff_arg_t
))
3619 len
= sizeof(sctp_peeloff_arg_t
);
3620 if (copy_from_user(&peeloff
, optval
, len
))
3623 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3629 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__
, sk
, asoc
);
3631 retval
= sctp_do_peeloff(asoc
, &newsock
);
3635 /* Map the socket to an unused fd that can be returned to the user. */
3636 retval
= sock_map_fd(newsock
);
3638 sock_release(newsock
);
3642 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3643 __FUNCTION__
, sk
, asoc
, newsock
->sk
, retval
);
3645 /* Return the fd mapped to the new socket. */
3646 peeloff
.sd
= retval
;
3647 if (put_user(len
, optlen
))
3649 if (copy_to_user(optval
, &peeloff
, len
))
3656 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3658 * Applications can enable or disable heartbeats for any peer address of
3659 * an association, modify an address's heartbeat interval, force a
3660 * heartbeat to be sent immediately, and adjust the address's maximum
3661 * number of retransmissions sent before an address is considered
3662 * unreachable. The following structure is used to access and modify an
3663 * address's parameters:
3665 * struct sctp_paddrparams {
3666 * sctp_assoc_t spp_assoc_id;
3667 * struct sockaddr_storage spp_address;
3668 * uint32_t spp_hbinterval;
3669 * uint16_t spp_pathmaxrxt;
3670 * uint32_t spp_pathmtu;
3671 * uint32_t spp_sackdelay;
3672 * uint32_t spp_flags;
3675 * spp_assoc_id - (one-to-many style socket) This is filled in the
3676 * application, and identifies the association for
3678 * spp_address - This specifies which address is of interest.
3679 * spp_hbinterval - This contains the value of the heartbeat interval,
3680 * in milliseconds. If a value of zero
3681 * is present in this field then no changes are to
3682 * be made to this parameter.
3683 * spp_pathmaxrxt - This contains the maximum number of
3684 * retransmissions before this address shall be
3685 * considered unreachable. If a value of zero
3686 * is present in this field then no changes are to
3687 * be made to this parameter.
3688 * spp_pathmtu - When Path MTU discovery is disabled the value
3689 * specified here will be the "fixed" path mtu.
3690 * Note that if the spp_address field is empty
3691 * then all associations on this address will
3692 * have this fixed path mtu set upon them.
3694 * spp_sackdelay - When delayed sack is enabled, this value specifies
3695 * the number of milliseconds that sacks will be delayed
3696 * for. This value will apply to all addresses of an
3697 * association if the spp_address field is empty. Note
3698 * also, that if delayed sack is enabled and this
3699 * value is set to 0, no change is made to the last
3700 * recorded delayed sack timer value.
3702 * spp_flags - These flags are used to control various features
3703 * on an association. The flag field may contain
3704 * zero or more of the following options.
3706 * SPP_HB_ENABLE - Enable heartbeats on the
3707 * specified address. Note that if the address
3708 * field is empty all addresses for the association
3709 * have heartbeats enabled upon them.
3711 * SPP_HB_DISABLE - Disable heartbeats on the
3712 * speicifed address. Note that if the address
3713 * field is empty all addresses for the association
3714 * will have their heartbeats disabled. Note also
3715 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3716 * mutually exclusive, only one of these two should
3717 * be specified. Enabling both fields will have
3718 * undetermined results.
3720 * SPP_HB_DEMAND - Request a user initiated heartbeat
3721 * to be made immediately.
3723 * SPP_PMTUD_ENABLE - This field will enable PMTU
3724 * discovery upon the specified address. Note that
3725 * if the address feild is empty then all addresses
3726 * on the association are effected.
3728 * SPP_PMTUD_DISABLE - This field will disable PMTU
3729 * discovery upon the specified address. Note that
3730 * if the address feild is empty then all addresses
3731 * on the association are effected. Not also that
3732 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3733 * exclusive. Enabling both will have undetermined
3736 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3737 * on delayed sack. The time specified in spp_sackdelay
3738 * is used to specify the sack delay for this address. Note
3739 * that if spp_address is empty then all addresses will
3740 * enable delayed sack and take on the sack delay
3741 * value specified in spp_sackdelay.
3742 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3743 * off delayed sack. If the spp_address field is blank then
3744 * delayed sack is disabled for the entire association. Note
3745 * also that this field is mutually exclusive to
3746 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3749 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
3750 char __user
*optval
, int __user
*optlen
)
3752 struct sctp_paddrparams params
;
3753 struct sctp_transport
*trans
= NULL
;
3754 struct sctp_association
*asoc
= NULL
;
3755 struct sctp_sock
*sp
= sctp_sk(sk
);
3757 if (len
< sizeof(struct sctp_paddrparams
))
3759 len
= sizeof(struct sctp_paddrparams
);
3760 if (copy_from_user(¶ms
, optval
, len
))
3763 /* If an address other than INADDR_ANY is specified, and
3764 * no transport is found, then the request is invalid.
3766 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
3767 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
3768 params
.spp_assoc_id
);
3770 SCTP_DEBUG_PRINTK("Failed no transport\n");
3775 /* Get association, if assoc_id != 0 and the socket is a one
3776 * to many style socket, and an association was not found, then
3777 * the id was invalid.
3779 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
3780 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
3781 SCTP_DEBUG_PRINTK("Failed no association\n");
3786 /* Fetch transport values. */
3787 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
3788 params
.spp_pathmtu
= trans
->pathmtu
;
3789 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
3790 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
3792 /*draft-11 doesn't say what to return in spp_flags*/
3793 params
.spp_flags
= trans
->param_flags
;
3795 /* Fetch association values. */
3796 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
3797 params
.spp_pathmtu
= asoc
->pathmtu
;
3798 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
3799 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
3801 /*draft-11 doesn't say what to return in spp_flags*/
3802 params
.spp_flags
= asoc
->param_flags
;
3804 /* Fetch socket values. */
3805 params
.spp_hbinterval
= sp
->hbinterval
;
3806 params
.spp_pathmtu
= sp
->pathmtu
;
3807 params
.spp_sackdelay
= sp
->sackdelay
;
3808 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
3810 /*draft-11 doesn't say what to return in spp_flags*/
3811 params
.spp_flags
= sp
->param_flags
;
3814 if (copy_to_user(optval
, ¶ms
, len
))
3817 if (put_user(len
, optlen
))
3823 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3825 * This options will get or set the delayed ack timer. The time is set
3826 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3827 * endpoints default delayed ack timer value. If the assoc_id field is
3828 * non-zero, then the set or get effects the specified association.
3830 * struct sctp_assoc_value {
3831 * sctp_assoc_t assoc_id;
3832 * uint32_t assoc_value;
3835 * assoc_id - This parameter, indicates which association the
3836 * user is preforming an action upon. Note that if
3837 * this field's value is zero then the endpoints
3838 * default value is changed (effecting future
3839 * associations only).
3841 * assoc_value - This parameter contains the number of milliseconds
3842 * that the user is requesting the delayed ACK timer
3843 * be set to. Note that this value is defined in
3844 * the standard to be between 200 and 500 milliseconds.
3846 * Note: a value of zero will leave the value alone,
3847 * but disable SACK delay. A non-zero value will also
3848 * enable SACK delay.
3850 static int sctp_getsockopt_delayed_ack_time(struct sock
*sk
, int len
,
3851 char __user
*optval
,
3854 struct sctp_assoc_value params
;
3855 struct sctp_association
*asoc
= NULL
;
3856 struct sctp_sock
*sp
= sctp_sk(sk
);
3858 if (len
< sizeof(struct sctp_assoc_value
))
3861 len
= sizeof(struct sctp_assoc_value
);
3863 if (copy_from_user(¶ms
, optval
, len
))
3866 /* Get association, if assoc_id != 0 and the socket is a one
3867 * to many style socket, and an association was not found, then
3868 * the id was invalid.
3870 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
3871 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
3875 /* Fetch association values. */
3876 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
)
3877 params
.assoc_value
= jiffies_to_msecs(
3880 params
.assoc_value
= 0;
3882 /* Fetch socket values. */
3883 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
)
3884 params
.assoc_value
= sp
->sackdelay
;
3886 params
.assoc_value
= 0;
3889 if (copy_to_user(optval
, ¶ms
, len
))
3892 if (put_user(len
, optlen
))
3898 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3900 * Applications can specify protocol parameters for the default association
3901 * initialization. The option name argument to setsockopt() and getsockopt()
3904 * Setting initialization parameters is effective only on an unconnected
3905 * socket (for UDP-style sockets only future associations are effected
3906 * by the change). With TCP-style sockets, this option is inherited by
3907 * sockets derived from a listener socket.
3909 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3911 if (len
< sizeof(struct sctp_initmsg
))
3913 len
= sizeof(struct sctp_initmsg
);
3914 if (put_user(len
, optlen
))
3916 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
3921 static int sctp_getsockopt_peer_addrs_num_old(struct sock
*sk
, int len
,
3922 char __user
*optval
,
3926 struct sctp_association
*asoc
;
3927 struct list_head
*pos
;
3930 if (len
< sizeof(sctp_assoc_t
))
3933 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3936 /* For UDP-style sockets, id specifies the association to query. */
3937 asoc
= sctp_id2assoc(sk
, id
);
3941 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3949 * Old API for getting list of peer addresses. Does not work for 32-bit
3950 * programs running on a 64-bit kernel
3952 static int sctp_getsockopt_peer_addrs_old(struct sock
*sk
, int len
,
3953 char __user
*optval
,
3956 struct sctp_association
*asoc
;
3957 struct list_head
*pos
;
3959 struct sctp_getaddrs_old getaddrs
;
3960 struct sctp_transport
*from
;
3962 union sctp_addr temp
;
3963 struct sctp_sock
*sp
= sctp_sk(sk
);
3966 if (len
< sizeof(struct sctp_getaddrs_old
))
3969 len
= sizeof(struct sctp_getaddrs_old
);
3971 if (copy_from_user(&getaddrs
, optval
, len
))
3974 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3976 /* For UDP-style sockets, id specifies the association to query. */
3977 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3981 to
= (void __user
*)getaddrs
.addrs
;
3982 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3983 from
= list_entry(pos
, struct sctp_transport
, transports
);
3984 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3985 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3986 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3987 if (copy_to_user(to
, &temp
, addrlen
))
3991 if (cnt
>= getaddrs
.addr_num
) break;
3993 getaddrs
.addr_num
= cnt
;
3994 if (put_user(len
, optlen
))
3996 if (copy_to_user(optval
, &getaddrs
, len
))
4002 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
4003 char __user
*optval
, int __user
*optlen
)
4005 struct sctp_association
*asoc
;
4006 struct list_head
*pos
;
4008 struct sctp_getaddrs getaddrs
;
4009 struct sctp_transport
*from
;
4011 union sctp_addr temp
;
4012 struct sctp_sock
*sp
= sctp_sk(sk
);
4017 if (len
< sizeof(struct sctp_getaddrs
))
4020 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4023 /* For UDP-style sockets, id specifies the association to query. */
4024 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4028 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4029 space_left
= len
- offsetof(struct sctp_getaddrs
,addrs
);
4031 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4032 from
= list_entry(pos
, struct sctp_transport
, transports
);
4033 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
4034 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4035 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
4036 if (space_left
< addrlen
)
4038 if (copy_to_user(to
, &temp
, addrlen
))
4042 space_left
-= addrlen
;
4045 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4047 bytes_copied
= ((char __user
*)to
) - optval
;
4048 if (put_user(bytes_copied
, optlen
))
4054 static int sctp_getsockopt_local_addrs_num_old(struct sock
*sk
, int len
,
4055 char __user
*optval
,
4059 struct sctp_bind_addr
*bp
;
4060 struct sctp_association
*asoc
;
4061 struct sctp_sockaddr_entry
*addr
;
4064 if (len
< sizeof(sctp_assoc_t
))
4067 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
4071 * For UDP-style sockets, id specifies the association to query.
4072 * If the id field is set to the value '0' then the locally bound
4073 * addresses are returned without regard to any particular
4077 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4079 asoc
= sctp_id2assoc(sk
, id
);
4082 bp
= &asoc
->base
.bind_addr
;
4085 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4086 * addresses from the global local address list.
4088 if (sctp_list_single_entry(&bp
->address_list
)) {
4089 addr
= list_entry(bp
->address_list
.next
,
4090 struct sctp_sockaddr_entry
, list
);
4091 if (sctp_is_any(&addr
->a
)) {
4093 list_for_each_entry_rcu(addr
,
4094 &sctp_local_addr_list
, list
) {
4098 if ((PF_INET
== sk
->sk_family
) &&
4099 (AF_INET6
== addr
->a
.sa
.sa_family
))
4111 /* Protection on the bound address list is not needed,
4112 * since in the socket option context we hold the socket lock,
4113 * so there is no way that the bound address list can change.
4115 list_for_each_entry(addr
, &bp
->address_list
, list
) {
4122 /* Helper function that copies local addresses to user and returns the number
4123 * of addresses copied.
4125 static int sctp_copy_laddrs_old(struct sock
*sk
, __u16 port
,
4126 int max_addrs
, void *to
,
4129 struct sctp_sockaddr_entry
*addr
;
4130 union sctp_addr temp
;
4135 list_for_each_entry_rcu(addr
, &sctp_local_addr_list
, list
) {
4139 if ((PF_INET
== sk
->sk_family
) &&
4140 (AF_INET6
== addr
->a
.sa
.sa_family
))
4142 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4143 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
4145 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4146 memcpy(to
, &temp
, addrlen
);
4149 *bytes_copied
+= addrlen
;
4151 if (cnt
>= max_addrs
) break;
4158 static int sctp_copy_laddrs(struct sock
*sk
, __u16 port
, void *to
,
4159 size_t space_left
, int *bytes_copied
)
4161 struct sctp_sockaddr_entry
*addr
;
4162 union sctp_addr temp
;
4167 list_for_each_entry_rcu(addr
, &sctp_local_addr_list
, list
) {
4171 if ((PF_INET
== sk
->sk_family
) &&
4172 (AF_INET6
== addr
->a
.sa
.sa_family
))
4174 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4175 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
4177 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4178 if (space_left
< addrlen
) {
4182 memcpy(to
, &temp
, addrlen
);
4186 space_left
-= addrlen
;
4187 *bytes_copied
+= addrlen
;
4194 /* Old API for getting list of local addresses. Does not work for 32-bit
4195 * programs running on a 64-bit kernel
4197 static int sctp_getsockopt_local_addrs_old(struct sock
*sk
, int len
,
4198 char __user
*optval
, int __user
*optlen
)
4200 struct sctp_bind_addr
*bp
;
4201 struct sctp_association
*asoc
;
4203 struct sctp_getaddrs_old getaddrs
;
4204 struct sctp_sockaddr_entry
*addr
;
4206 union sctp_addr temp
;
4207 struct sctp_sock
*sp
= sctp_sk(sk
);
4212 int bytes_copied
= 0;
4214 if (len
< sizeof(struct sctp_getaddrs_old
))
4217 len
= sizeof(struct sctp_getaddrs_old
);
4218 if (copy_from_user(&getaddrs
, optval
, len
))
4221 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
4223 * For UDP-style sockets, id specifies the association to query.
4224 * If the id field is set to the value '0' then the locally bound
4225 * addresses are returned without regard to any particular
4228 if (0 == getaddrs
.assoc_id
) {
4229 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4231 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4234 bp
= &asoc
->base
.bind_addr
;
4237 to
= getaddrs
.addrs
;
4239 /* Allocate space for a local instance of packed array to hold all
4240 * the data. We store addresses here first and then put write them
4241 * to the user in one shot.
4243 addrs
= kmalloc(sizeof(union sctp_addr
) * getaddrs
.addr_num
,
4248 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4249 * addresses from the global local address list.
4251 if (sctp_list_single_entry(&bp
->address_list
)) {
4252 addr
= list_entry(bp
->address_list
.next
,
4253 struct sctp_sockaddr_entry
, list
);
4254 if (sctp_is_any(&addr
->a
)) {
4255 cnt
= sctp_copy_laddrs_old(sk
, bp
->port
,
4257 addrs
, &bytes_copied
);
4263 /* Protection on the bound address list is not needed since
4264 * in the socket option context we hold a socket lock and
4265 * thus the bound address list can't change.
4267 list_for_each_entry(addr
, &bp
->address_list
, list
) {
4268 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4269 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4270 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4271 memcpy(buf
, &temp
, addrlen
);
4273 bytes_copied
+= addrlen
;
4275 if (cnt
>= getaddrs
.addr_num
) break;
4279 /* copy the entire address list into the user provided space */
4280 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4285 /* copy the leading structure back to user */
4286 getaddrs
.addr_num
= cnt
;
4287 if (copy_to_user(optval
, &getaddrs
, len
))
4295 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4296 char __user
*optval
, int __user
*optlen
)
4298 struct sctp_bind_addr
*bp
;
4299 struct sctp_association
*asoc
;
4301 struct sctp_getaddrs getaddrs
;
4302 struct sctp_sockaddr_entry
*addr
;
4304 union sctp_addr temp
;
4305 struct sctp_sock
*sp
= sctp_sk(sk
);
4309 int bytes_copied
= 0;
4313 if (len
< sizeof(struct sctp_getaddrs
))
4316 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4320 * For UDP-style sockets, id specifies the association to query.
4321 * If the id field is set to the value '0' then the locally bound
4322 * addresses are returned without regard to any particular
4325 if (0 == getaddrs
.assoc_id
) {
4326 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4328 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4331 bp
= &asoc
->base
.bind_addr
;
4334 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4335 space_left
= len
- offsetof(struct sctp_getaddrs
,addrs
);
4337 addrs
= kmalloc(space_left
, GFP_KERNEL
);
4341 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4342 * addresses from the global local address list.
4344 if (sctp_list_single_entry(&bp
->address_list
)) {
4345 addr
= list_entry(bp
->address_list
.next
,
4346 struct sctp_sockaddr_entry
, list
);
4347 if (sctp_is_any(&addr
->a
)) {
4348 cnt
= sctp_copy_laddrs(sk
, bp
->port
, addrs
,
4349 space_left
, &bytes_copied
);
4359 /* Protection on the bound address list is not needed since
4360 * in the socket option context we hold a socket lock and
4361 * thus the bound address list can't change.
4363 list_for_each_entry(addr
, &bp
->address_list
, list
) {
4364 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4365 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4366 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4367 if (space_left
< addrlen
) {
4368 err
= -ENOMEM
; /*fixme: right error?*/
4371 memcpy(buf
, &temp
, addrlen
);
4373 bytes_copied
+= addrlen
;
4375 space_left
-= addrlen
;
4379 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4383 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
)) {
4387 if (put_user(bytes_copied
, optlen
))
4394 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4396 * Requests that the local SCTP stack use the enclosed peer address as
4397 * the association primary. The enclosed address must be one of the
4398 * association peer's addresses.
4400 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4401 char __user
*optval
, int __user
*optlen
)
4403 struct sctp_prim prim
;
4404 struct sctp_association
*asoc
;
4405 struct sctp_sock
*sp
= sctp_sk(sk
);
4407 if (len
< sizeof(struct sctp_prim
))
4410 len
= sizeof(struct sctp_prim
);
4412 if (copy_from_user(&prim
, optval
, len
))
4415 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4419 if (!asoc
->peer
.primary_path
)
4422 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4423 asoc
->peer
.primary_path
->af_specific
->sockaddr_len
);
4425 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4426 (union sctp_addr
*)&prim
.ssp_addr
);
4428 if (put_user(len
, optlen
))
4430 if (copy_to_user(optval
, &prim
, len
))
4437 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4439 * Requests that the local endpoint set the specified Adaptation Layer
4440 * Indication parameter for all future INIT and INIT-ACK exchanges.
4442 static int sctp_getsockopt_adaptation_layer(struct sock
*sk
, int len
,
4443 char __user
*optval
, int __user
*optlen
)
4445 struct sctp_setadaptation adaptation
;
4447 if (len
< sizeof(struct sctp_setadaptation
))
4450 len
= sizeof(struct sctp_setadaptation
);
4452 adaptation
.ssb_adaptation_ind
= sctp_sk(sk
)->adaptation_ind
;
4454 if (put_user(len
, optlen
))
4456 if (copy_to_user(optval
, &adaptation
, len
))
4464 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4466 * Applications that wish to use the sendto() system call may wish to
4467 * specify a default set of parameters that would normally be supplied
4468 * through the inclusion of ancillary data. This socket option allows
4469 * such an application to set the default sctp_sndrcvinfo structure.
4472 * The application that wishes to use this socket option simply passes
4473 * in to this call the sctp_sndrcvinfo structure defined in Section
4474 * 5.2.2) The input parameters accepted by this call include
4475 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4476 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4477 * to this call if the caller is using the UDP model.
4479 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4481 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4482 int len
, char __user
*optval
,
4485 struct sctp_sndrcvinfo info
;
4486 struct sctp_association
*asoc
;
4487 struct sctp_sock
*sp
= sctp_sk(sk
);
4489 if (len
< sizeof(struct sctp_sndrcvinfo
))
4492 len
= sizeof(struct sctp_sndrcvinfo
);
4494 if (copy_from_user(&info
, optval
, len
))
4497 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4498 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4502 info
.sinfo_stream
= asoc
->default_stream
;
4503 info
.sinfo_flags
= asoc
->default_flags
;
4504 info
.sinfo_ppid
= asoc
->default_ppid
;
4505 info
.sinfo_context
= asoc
->default_context
;
4506 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4508 info
.sinfo_stream
= sp
->default_stream
;
4509 info
.sinfo_flags
= sp
->default_flags
;
4510 info
.sinfo_ppid
= sp
->default_ppid
;
4511 info
.sinfo_context
= sp
->default_context
;
4512 info
.sinfo_timetolive
= sp
->default_timetolive
;
4515 if (put_user(len
, optlen
))
4517 if (copy_to_user(optval
, &info
, len
))
4525 * 7.1.5 SCTP_NODELAY
4527 * Turn on/off any Nagle-like algorithm. This means that packets are
4528 * generally sent as soon as possible and no unnecessary delays are
4529 * introduced, at the cost of more packets in the network. Expects an
4530 * integer boolean flag.
4533 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4534 char __user
*optval
, int __user
*optlen
)
4538 if (len
< sizeof(int))
4542 val
= (sctp_sk(sk
)->nodelay
== 1);
4543 if (put_user(len
, optlen
))
4545 if (copy_to_user(optval
, &val
, len
))
4552 * 7.1.1 SCTP_RTOINFO
4554 * The protocol parameters used to initialize and bound retransmission
4555 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4556 * and modify these parameters.
4557 * All parameters are time values, in milliseconds. A value of 0, when
4558 * modifying the parameters, indicates that the current value should not
4562 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4563 char __user
*optval
,
4564 int __user
*optlen
) {
4565 struct sctp_rtoinfo rtoinfo
;
4566 struct sctp_association
*asoc
;
4568 if (len
< sizeof (struct sctp_rtoinfo
))
4571 len
= sizeof(struct sctp_rtoinfo
);
4573 if (copy_from_user(&rtoinfo
, optval
, len
))
4576 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4578 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4581 /* Values corresponding to the specific association. */
4583 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4584 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4585 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4587 /* Values corresponding to the endpoint. */
4588 struct sctp_sock
*sp
= sctp_sk(sk
);
4590 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4591 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4592 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4595 if (put_user(len
, optlen
))
4598 if (copy_to_user(optval
, &rtoinfo
, len
))
4606 * 7.1.2 SCTP_ASSOCINFO
4608 * This option is used to tune the maximum retransmission attempts
4609 * of the association.
4610 * Returns an error if the new association retransmission value is
4611 * greater than the sum of the retransmission value of the peer.
4612 * See [SCTP] for more information.
4615 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4616 char __user
*optval
,
4620 struct sctp_assocparams assocparams
;
4621 struct sctp_association
*asoc
;
4622 struct list_head
*pos
;
4625 if (len
< sizeof (struct sctp_assocparams
))
4628 len
= sizeof(struct sctp_assocparams
);
4630 if (copy_from_user(&assocparams
, optval
, len
))
4633 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4635 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4638 /* Values correspoinding to the specific association */
4640 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4641 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4642 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4643 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4645 (asoc
->cookie_life
.tv_usec
4648 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4652 assocparams
.sasoc_number_peer_destinations
= cnt
;
4654 /* Values corresponding to the endpoint */
4655 struct sctp_sock
*sp
= sctp_sk(sk
);
4657 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4658 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4659 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4660 assocparams
.sasoc_cookie_life
=
4661 sp
->assocparams
.sasoc_cookie_life
;
4662 assocparams
.sasoc_number_peer_destinations
=
4664 sasoc_number_peer_destinations
;
4667 if (put_user(len
, optlen
))
4670 if (copy_to_user(optval
, &assocparams
, len
))
4677 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4679 * This socket option is a boolean flag which turns on or off mapped V4
4680 * addresses. If this option is turned on and the socket is type
4681 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4682 * If this option is turned off, then no mapping will be done of V4
4683 * addresses and a user will receive both PF_INET6 and PF_INET type
4684 * addresses on the socket.
4686 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4687 char __user
*optval
, int __user
*optlen
)
4690 struct sctp_sock
*sp
= sctp_sk(sk
);
4692 if (len
< sizeof(int))
4697 if (put_user(len
, optlen
))
4699 if (copy_to_user(optval
, &val
, len
))
4706 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4707 * (chapter and verse is quoted at sctp_setsockopt_context())
4709 static int sctp_getsockopt_context(struct sock
*sk
, int len
,
4710 char __user
*optval
, int __user
*optlen
)
4712 struct sctp_assoc_value params
;
4713 struct sctp_sock
*sp
;
4714 struct sctp_association
*asoc
;
4716 if (len
< sizeof(struct sctp_assoc_value
))
4719 len
= sizeof(struct sctp_assoc_value
);
4721 if (copy_from_user(¶ms
, optval
, len
))
4726 if (params
.assoc_id
!= 0) {
4727 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4730 params
.assoc_value
= asoc
->default_rcv_context
;
4732 params
.assoc_value
= sp
->default_rcv_context
;
4735 if (put_user(len
, optlen
))
4737 if (copy_to_user(optval
, ¶ms
, len
))
4744 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4746 * This socket option specifies the maximum size to put in any outgoing
4747 * SCTP chunk. If a message is larger than this size it will be
4748 * fragmented by SCTP into the specified size. Note that the underlying
4749 * SCTP implementation may fragment into smaller sized chunks when the
4750 * PMTU of the underlying association is smaller than the value set by
4753 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4754 char __user
*optval
, int __user
*optlen
)
4758 if (len
< sizeof(int))
4763 val
= sctp_sk(sk
)->user_frag
;
4764 if (put_user(len
, optlen
))
4766 if (copy_to_user(optval
, &val
, len
))
4773 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4774 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4776 static int sctp_getsockopt_fragment_interleave(struct sock
*sk
, int len
,
4777 char __user
*optval
, int __user
*optlen
)
4781 if (len
< sizeof(int))
4786 val
= sctp_sk(sk
)->frag_interleave
;
4787 if (put_user(len
, optlen
))
4789 if (copy_to_user(optval
, &val
, len
))
4796 * 7.1.25. Set or Get the sctp partial delivery point
4797 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4799 static int sctp_getsockopt_partial_delivery_point(struct sock
*sk
, int len
,
4800 char __user
*optval
,
4805 if (len
< sizeof(u32
))
4810 val
= sctp_sk(sk
)->pd_point
;
4811 if (put_user(len
, optlen
))
4813 if (copy_to_user(optval
, &val
, len
))
4820 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4821 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4823 static int sctp_getsockopt_maxburst(struct sock
*sk
, int len
,
4824 char __user
*optval
,
4829 if (len
< sizeof(int))
4834 val
= sctp_sk(sk
)->max_burst
;
4835 if (put_user(len
, optlen
))
4837 if (copy_to_user(optval
, &val
, len
))
4843 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
4844 char __user
*optval
, int __user
*optlen
)
4849 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4852 /* I can hardly begin to describe how wrong this is. This is
4853 * so broken as to be worse than useless. The API draft
4854 * REALLY is NOT helpful here... I am not convinced that the
4855 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4856 * are at all well-founded.
4858 if (level
!= SOL_SCTP
) {
4859 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4861 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
4865 if (get_user(len
, optlen
))
4872 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
4874 case SCTP_DISABLE_FRAGMENTS
:
4875 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
4879 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
4881 case SCTP_AUTOCLOSE
:
4882 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
4884 case SCTP_SOCKOPT_PEELOFF
:
4885 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
4887 case SCTP_PEER_ADDR_PARAMS
:
4888 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
4891 case SCTP_DELAYED_ACK_TIME
:
4892 retval
= sctp_getsockopt_delayed_ack_time(sk
, len
, optval
,
4896 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
4898 case SCTP_GET_PEER_ADDRS_NUM_OLD
:
4899 retval
= sctp_getsockopt_peer_addrs_num_old(sk
, len
, optval
,
4902 case SCTP_GET_LOCAL_ADDRS_NUM_OLD
:
4903 retval
= sctp_getsockopt_local_addrs_num_old(sk
, len
, optval
,
4906 case SCTP_GET_PEER_ADDRS_OLD
:
4907 retval
= sctp_getsockopt_peer_addrs_old(sk
, len
, optval
,
4910 case SCTP_GET_LOCAL_ADDRS_OLD
:
4911 retval
= sctp_getsockopt_local_addrs_old(sk
, len
, optval
,
4914 case SCTP_GET_PEER_ADDRS
:
4915 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
4918 case SCTP_GET_LOCAL_ADDRS
:
4919 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
4922 case SCTP_DEFAULT_SEND_PARAM
:
4923 retval
= sctp_getsockopt_default_send_param(sk
, len
,
4926 case SCTP_PRIMARY_ADDR
:
4927 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
4930 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
4933 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
4935 case SCTP_ASSOCINFO
:
4936 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
4938 case SCTP_I_WANT_MAPPED_V4_ADDR
:
4939 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
4942 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
4944 case SCTP_GET_PEER_ADDR_INFO
:
4945 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
4948 case SCTP_ADAPTATION_LAYER
:
4949 retval
= sctp_getsockopt_adaptation_layer(sk
, len
, optval
,
4953 retval
= sctp_getsockopt_context(sk
, len
, optval
, optlen
);
4955 case SCTP_FRAGMENT_INTERLEAVE
:
4956 retval
= sctp_getsockopt_fragment_interleave(sk
, len
, optval
,
4959 case SCTP_PARTIAL_DELIVERY_POINT
:
4960 retval
= sctp_getsockopt_partial_delivery_point(sk
, len
, optval
,
4963 case SCTP_MAX_BURST
:
4964 retval
= sctp_getsockopt_maxburst(sk
, len
, optval
, optlen
);
4967 retval
= -ENOPROTOOPT
;
4971 sctp_release_sock(sk
);
4975 static void sctp_hash(struct sock
*sk
)
4980 static void sctp_unhash(struct sock
*sk
)
4985 /* Check if port is acceptable. Possibly find first available port.
4987 * The port hash table (contained in the 'global' SCTP protocol storage
4988 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4989 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4990 * list (the list number is the port number hashed out, so as you
4991 * would expect from a hash function, all the ports in a given list have
4992 * such a number that hashes out to the same list number; you were
4993 * expecting that, right?); so each list has a set of ports, with a
4994 * link to the socket (struct sock) that uses it, the port number and
4995 * a fastreuse flag (FIXME: NPI ipg).
4997 static struct sctp_bind_bucket
*sctp_bucket_create(
4998 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
5000 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
5002 struct sctp_bind_hashbucket
*head
; /* hash list */
5003 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5004 unsigned short snum
;
5007 snum
= ntohs(addr
->v4
.sin_port
);
5009 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
5010 sctp_local_bh_disable();
5013 /* Search for an available port.
5015 * 'sctp_port_rover' was the last port assigned, so
5016 * we start to search from 'sctp_port_rover +
5017 * 1'. What we do is first check if port 'rover' is
5018 * already in the hash table; if not, we use that; if
5019 * it is, we try next.
5021 int low
= sysctl_local_port_range
[0];
5022 int high
= sysctl_local_port_range
[1];
5023 int remaining
= (high
- low
) + 1;
5027 sctp_spin_lock(&sctp_port_alloc_lock
);
5028 rover
= sctp_port_rover
;
5031 if ((rover
< low
) || (rover
> high
))
5033 index
= sctp_phashfn(rover
);
5034 head
= &sctp_port_hashtable
[index
];
5035 sctp_spin_lock(&head
->lock
);
5036 for (pp
= head
->chain
; pp
; pp
= pp
->next
)
5037 if (pp
->port
== rover
)
5041 sctp_spin_unlock(&head
->lock
);
5042 } while (--remaining
> 0);
5043 sctp_port_rover
= rover
;
5044 sctp_spin_unlock(&sctp_port_alloc_lock
);
5046 /* Exhausted local port range during search? */
5051 /* OK, here is the one we will use. HEAD (the port
5052 * hash table list entry) is non-NULL and we hold it's
5057 /* We are given an specific port number; we verify
5058 * that it is not being used. If it is used, we will
5059 * exahust the search in the hash list corresponding
5060 * to the port number (snum) - we detect that with the
5061 * port iterator, pp being NULL.
5063 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
5064 sctp_spin_lock(&head
->lock
);
5065 for (pp
= head
->chain
; pp
; pp
= pp
->next
) {
5066 if (pp
->port
== snum
)
5073 if (!hlist_empty(&pp
->owner
)) {
5074 /* We had a port hash table hit - there is an
5075 * available port (pp != NULL) and it is being
5076 * used by other socket (pp->owner not empty); that other
5077 * socket is going to be sk2.
5079 int reuse
= sk
->sk_reuse
;
5081 struct hlist_node
*node
;
5083 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5084 if (pp
->fastreuse
&& sk
->sk_reuse
&&
5085 sk
->sk_state
!= SCTP_SS_LISTENING
)
5088 /* Run through the list of sockets bound to the port
5089 * (pp->port) [via the pointers bind_next and
5090 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5091 * we get the endpoint they describe and run through
5092 * the endpoint's list of IP (v4 or v6) addresses,
5093 * comparing each of the addresses with the address of
5094 * the socket sk. If we find a match, then that means
5095 * that this port/socket (sk) combination are already
5098 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
5099 struct sctp_endpoint
*ep2
;
5100 ep2
= sctp_sk(sk2
)->ep
;
5102 if (reuse
&& sk2
->sk_reuse
&&
5103 sk2
->sk_state
!= SCTP_SS_LISTENING
)
5106 if (sctp_bind_addr_match(&ep2
->base
.bind_addr
, addr
,
5112 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5115 /* If there was a hash table miss, create a new port. */
5117 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
5120 /* In either case (hit or miss), make sure fastreuse is 1 only
5121 * if sk->sk_reuse is too (that is, if the caller requested
5122 * SO_REUSEADDR on this socket -sk-).
5124 if (hlist_empty(&pp
->owner
)) {
5125 if (sk
->sk_reuse
&& sk
->sk_state
!= SCTP_SS_LISTENING
)
5129 } else if (pp
->fastreuse
&&
5130 (!sk
->sk_reuse
|| sk
->sk_state
== SCTP_SS_LISTENING
))
5133 /* We are set, so fill up all the data in the hash table
5134 * entry, tie the socket list information with the rest of the
5135 * sockets FIXME: Blurry, NPI (ipg).
5138 if (!sctp_sk(sk
)->bind_hash
) {
5139 inet_sk(sk
)->num
= snum
;
5140 sk_add_bind_node(sk
, &pp
->owner
);
5141 sctp_sk(sk
)->bind_hash
= pp
;
5146 sctp_spin_unlock(&head
->lock
);
5149 sctp_local_bh_enable();
5153 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5154 * port is requested.
5156 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
5159 union sctp_addr addr
;
5160 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
5162 /* Set up a dummy address struct from the sk. */
5163 af
->from_sk(&addr
, sk
);
5164 addr
.v4
.sin_port
= htons(snum
);
5166 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5167 ret
= sctp_get_port_local(sk
, &addr
);
5169 return (ret
? 1 : 0);
5173 * 3.1.3 listen() - UDP Style Syntax
5175 * By default, new associations are not accepted for UDP style sockets.
5176 * An application uses listen() to mark a socket as being able to
5177 * accept new associations.
5179 SCTP_STATIC
int sctp_seqpacket_listen(struct sock
*sk
, int backlog
)
5181 struct sctp_sock
*sp
= sctp_sk(sk
);
5182 struct sctp_endpoint
*ep
= sp
->ep
;
5184 /* Only UDP style sockets that are not peeled off are allowed to
5187 if (!sctp_style(sk
, UDP
))
5190 /* If backlog is zero, disable listening. */
5192 if (sctp_sstate(sk
, CLOSED
))
5195 sctp_unhash_endpoint(ep
);
5196 sk
->sk_state
= SCTP_SS_CLOSED
;
5200 /* Return if we are already listening. */
5201 if (sctp_sstate(sk
, LISTENING
))
5205 * If a bind() or sctp_bindx() is not called prior to a listen()
5206 * call that allows new associations to be accepted, the system
5207 * picks an ephemeral port and will choose an address set equivalent
5208 * to binding with a wildcard address.
5210 * This is not currently spelled out in the SCTP sockets
5211 * extensions draft, but follows the practice as seen in TCP
5214 * Additionally, turn off fastreuse flag since we are not listening
5216 sk
->sk_state
= SCTP_SS_LISTENING
;
5217 if (!ep
->base
.bind_addr
.port
) {
5218 if (sctp_autobind(sk
))
5221 sctp_sk(sk
)->bind_hash
->fastreuse
= 0;
5223 sctp_hash_endpoint(ep
);
5228 * 4.1.3 listen() - TCP Style Syntax
5230 * Applications uses listen() to ready the SCTP endpoint for accepting
5231 * inbound associations.
5233 SCTP_STATIC
int sctp_stream_listen(struct sock
*sk
, int backlog
)
5235 struct sctp_sock
*sp
= sctp_sk(sk
);
5236 struct sctp_endpoint
*ep
= sp
->ep
;
5238 /* If backlog is zero, disable listening. */
5240 if (sctp_sstate(sk
, CLOSED
))
5243 sctp_unhash_endpoint(ep
);
5244 sk
->sk_state
= SCTP_SS_CLOSED
;
5248 if (sctp_sstate(sk
, LISTENING
))
5252 * If a bind() or sctp_bindx() is not called prior to a listen()
5253 * call that allows new associations to be accepted, the system
5254 * picks an ephemeral port and will choose an address set equivalent
5255 * to binding with a wildcard address.
5257 * This is not currently spelled out in the SCTP sockets
5258 * extensions draft, but follows the practice as seen in TCP
5261 sk
->sk_state
= SCTP_SS_LISTENING
;
5262 if (!ep
->base
.bind_addr
.port
) {
5263 if (sctp_autobind(sk
))
5266 sctp_sk(sk
)->bind_hash
->fastreuse
= 0;
5268 sk
->sk_max_ack_backlog
= backlog
;
5269 sctp_hash_endpoint(ep
);
5274 * Move a socket to LISTENING state.
5276 int sctp_inet_listen(struct socket
*sock
, int backlog
)
5278 struct sock
*sk
= sock
->sk
;
5279 struct crypto_hash
*tfm
= NULL
;
5282 if (unlikely(backlog
< 0))
5287 if (sock
->state
!= SS_UNCONNECTED
)
5290 /* Allocate HMAC for generating cookie. */
5291 if (sctp_hmac_alg
) {
5292 tfm
= crypto_alloc_hash(sctp_hmac_alg
, 0, CRYPTO_ALG_ASYNC
);
5294 if (net_ratelimit()) {
5296 "SCTP: failed to load transform for %s: %ld\n",
5297 sctp_hmac_alg
, PTR_ERR(tfm
));
5304 switch (sock
->type
) {
5305 case SOCK_SEQPACKET
:
5306 err
= sctp_seqpacket_listen(sk
, backlog
);
5309 err
= sctp_stream_listen(sk
, backlog
);
5318 /* Store away the transform reference. */
5319 sctp_sk(sk
)->hmac
= tfm
;
5321 sctp_release_sock(sk
);
5324 crypto_free_hash(tfm
);
5329 * This function is done by modeling the current datagram_poll() and the
5330 * tcp_poll(). Note that, based on these implementations, we don't
5331 * lock the socket in this function, even though it seems that,
5332 * ideally, locking or some other mechanisms can be used to ensure
5333 * the integrity of the counters (sndbuf and wmem_alloc) used
5334 * in this place. We assume that we don't need locks either until proven
5337 * Another thing to note is that we include the Async I/O support
5338 * here, again, by modeling the current TCP/UDP code. We don't have
5339 * a good way to test with it yet.
5341 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
5343 struct sock
*sk
= sock
->sk
;
5344 struct sctp_sock
*sp
= sctp_sk(sk
);
5347 poll_wait(file
, sk
->sk_sleep
, wait
);
5349 /* A TCP-style listening socket becomes readable when the accept queue
5352 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
5353 return (!list_empty(&sp
->ep
->asocs
)) ?
5354 (POLLIN
| POLLRDNORM
) : 0;
5358 /* Is there any exceptional events? */
5359 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
5361 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5363 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
5366 /* Is it readable? Reconsider this code with TCP-style support. */
5367 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
5368 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
5369 mask
|= POLLIN
| POLLRDNORM
;
5371 /* The association is either gone or not ready. */
5372 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
5375 /* Is it writable? */
5376 if (sctp_writeable(sk
)) {
5377 mask
|= POLLOUT
| POLLWRNORM
;
5379 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
5381 * Since the socket is not locked, the buffer
5382 * might be made available after the writeable check and
5383 * before the bit is set. This could cause a lost I/O
5384 * signal. tcp_poll() has a race breaker for this race
5385 * condition. Based on their implementation, we put
5386 * in the following code to cover it as well.
5388 if (sctp_writeable(sk
))
5389 mask
|= POLLOUT
| POLLWRNORM
;
5394 /********************************************************************
5395 * 2nd Level Abstractions
5396 ********************************************************************/
5398 static struct sctp_bind_bucket
*sctp_bucket_create(
5399 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
5401 struct sctp_bind_bucket
*pp
;
5403 pp
= kmem_cache_alloc(sctp_bucket_cachep
, GFP_ATOMIC
);
5404 SCTP_DBG_OBJCNT_INC(bind_bucket
);
5408 INIT_HLIST_HEAD(&pp
->owner
);
5409 if ((pp
->next
= head
->chain
) != NULL
)
5410 pp
->next
->pprev
= &pp
->next
;
5412 pp
->pprev
= &head
->chain
;
5417 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5418 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
5420 if (pp
&& hlist_empty(&pp
->owner
)) {
5422 pp
->next
->pprev
= pp
->pprev
;
5423 *(pp
->pprev
) = pp
->next
;
5424 kmem_cache_free(sctp_bucket_cachep
, pp
);
5425 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
5429 /* Release this socket's reference to a local port. */
5430 static inline void __sctp_put_port(struct sock
*sk
)
5432 struct sctp_bind_hashbucket
*head
=
5433 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->num
)];
5434 struct sctp_bind_bucket
*pp
;
5436 sctp_spin_lock(&head
->lock
);
5437 pp
= sctp_sk(sk
)->bind_hash
;
5438 __sk_del_bind_node(sk
);
5439 sctp_sk(sk
)->bind_hash
= NULL
;
5440 inet_sk(sk
)->num
= 0;
5441 sctp_bucket_destroy(pp
);
5442 sctp_spin_unlock(&head
->lock
);
5445 void sctp_put_port(struct sock
*sk
)
5447 sctp_local_bh_disable();
5448 __sctp_put_port(sk
);
5449 sctp_local_bh_enable();
5453 * The system picks an ephemeral port and choose an address set equivalent
5454 * to binding with a wildcard address.
5455 * One of those addresses will be the primary address for the association.
5456 * This automatically enables the multihoming capability of SCTP.
5458 static int sctp_autobind(struct sock
*sk
)
5460 union sctp_addr autoaddr
;
5464 /* Initialize a local sockaddr structure to INADDR_ANY. */
5465 af
= sctp_sk(sk
)->pf
->af
;
5467 port
= htons(inet_sk(sk
)->num
);
5468 af
->inaddr_any(&autoaddr
, port
);
5470 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5473 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5476 * 4.2 The cmsghdr Structure *
5478 * When ancillary data is sent or received, any number of ancillary data
5479 * objects can be specified by the msg_control and msg_controllen members of
5480 * the msghdr structure, because each object is preceded by
5481 * a cmsghdr structure defining the object's length (the cmsg_len member).
5482 * Historically Berkeley-derived implementations have passed only one object
5483 * at a time, but this API allows multiple objects to be
5484 * passed in a single call to sendmsg() or recvmsg(). The following example
5485 * shows two ancillary data objects in a control buffer.
5487 * |<--------------------------- msg_controllen -------------------------->|
5490 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5492 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5495 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5497 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5500 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5501 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5503 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5505 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5512 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5513 sctp_cmsgs_t
*cmsgs
)
5515 struct cmsghdr
*cmsg
;
5517 for (cmsg
= CMSG_FIRSTHDR(msg
);
5519 cmsg
= CMSG_NXTHDR((struct msghdr
*)msg
, cmsg
)) {
5520 if (!CMSG_OK(msg
, cmsg
))
5523 /* Should we parse this header or ignore? */
5524 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5527 /* Strictly check lengths following example in SCM code. */
5528 switch (cmsg
->cmsg_type
) {
5530 /* SCTP Socket API Extension
5531 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5533 * This cmsghdr structure provides information for
5534 * initializing new SCTP associations with sendmsg().
5535 * The SCTP_INITMSG socket option uses this same data
5536 * structure. This structure is not used for
5539 * cmsg_level cmsg_type cmsg_data[]
5540 * ------------ ------------ ----------------------
5541 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5543 if (cmsg
->cmsg_len
!=
5544 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5546 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5550 /* SCTP Socket API Extension
5551 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5553 * This cmsghdr structure specifies SCTP options for
5554 * sendmsg() and describes SCTP header information
5555 * about a received message through recvmsg().
5557 * cmsg_level cmsg_type cmsg_data[]
5558 * ------------ ------------ ----------------------
5559 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5561 if (cmsg
->cmsg_len
!=
5562 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5566 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5568 /* Minimally, validate the sinfo_flags. */
5569 if (cmsgs
->info
->sinfo_flags
&
5570 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5571 SCTP_ABORT
| SCTP_EOF
))
5583 * Wait for a packet..
5584 * Note: This function is the same function as in core/datagram.c
5585 * with a few modifications to make lksctp work.
5587 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5592 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5594 /* Socket errors? */
5595 error
= sock_error(sk
);
5599 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5602 /* Socket shut down? */
5603 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5606 /* Sequenced packets can come disconnected. If so we report the
5611 /* Is there a good reason to think that we may receive some data? */
5612 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5615 /* Handle signals. */
5616 if (signal_pending(current
))
5619 /* Let another process have a go. Since we are going to sleep
5620 * anyway. Note: This may cause odd behaviors if the message
5621 * does not fit in the user's buffer, but this seems to be the
5622 * only way to honor MSG_DONTWAIT realistically.
5624 sctp_release_sock(sk
);
5625 *timeo_p
= schedule_timeout(*timeo_p
);
5629 finish_wait(sk
->sk_sleep
, &wait
);
5633 error
= sock_intr_errno(*timeo_p
);
5636 finish_wait(sk
->sk_sleep
, &wait
);
5641 /* Receive a datagram.
5642 * Note: This is pretty much the same routine as in core/datagram.c
5643 * with a few changes to make lksctp work.
5645 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5646 int noblock
, int *err
)
5649 struct sk_buff
*skb
;
5652 timeo
= sock_rcvtimeo(sk
, noblock
);
5654 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5655 timeo
, MAX_SCHEDULE_TIMEOUT
);
5658 /* Again only user level code calls this function,
5659 * so nothing interrupt level
5660 * will suddenly eat the receive_queue.
5662 * Look at current nfs client by the way...
5663 * However, this function was corrent in any case. 8)
5665 if (flags
& MSG_PEEK
) {
5666 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5667 skb
= skb_peek(&sk
->sk_receive_queue
);
5669 atomic_inc(&skb
->users
);
5670 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5672 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5678 /* Caller is allowed not to check sk->sk_err before calling. */
5679 error
= sock_error(sk
);
5683 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5686 /* User doesn't want to wait. */
5690 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5699 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5700 static void __sctp_write_space(struct sctp_association
*asoc
)
5702 struct sock
*sk
= asoc
->base
.sk
;
5703 struct socket
*sock
= sk
->sk_socket
;
5705 if ((sctp_wspace(asoc
) > 0) && sock
) {
5706 if (waitqueue_active(&asoc
->wait
))
5707 wake_up_interruptible(&asoc
->wait
);
5709 if (sctp_writeable(sk
)) {
5710 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
5711 wake_up_interruptible(sk
->sk_sleep
);
5713 /* Note that we try to include the Async I/O support
5714 * here by modeling from the current TCP/UDP code.
5715 * We have not tested with it yet.
5717 if (sock
->fasync_list
&&
5718 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5719 sock_wake_async(sock
, 2, POLL_OUT
);
5724 /* Do accounting for the sndbuf space.
5725 * Decrement the used sndbuf space of the corresponding association by the
5726 * data size which was just transmitted(freed).
5728 static void sctp_wfree(struct sk_buff
*skb
)
5730 struct sctp_association
*asoc
;
5731 struct sctp_chunk
*chunk
;
5734 /* Get the saved chunk pointer. */
5735 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
5738 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
5739 sizeof(struct sk_buff
) +
5740 sizeof(struct sctp_chunk
);
5742 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
5745 * This undoes what is done via sk_charge_skb
5747 sk
->sk_wmem_queued
-= skb
->truesize
;
5748 sk
->sk_forward_alloc
+= skb
->truesize
;
5751 __sctp_write_space(asoc
);
5753 sctp_association_put(asoc
);
5756 /* Do accounting for the receive space on the socket.
5757 * Accounting for the association is done in ulpevent.c
5758 * We set this as a destructor for the cloned data skbs so that
5759 * accounting is done at the correct time.
5761 void sctp_sock_rfree(struct sk_buff
*skb
)
5763 struct sock
*sk
= skb
->sk
;
5764 struct sctp_ulpevent
*event
= sctp_skb2event(skb
);
5766 atomic_sub(event
->rmem_len
, &sk
->sk_rmem_alloc
);
5769 * Mimic the behavior of sk_stream_rfree
5771 sk
->sk_forward_alloc
+= event
->rmem_len
;
5775 /* Helper function to wait for space in the sndbuf. */
5776 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
5779 struct sock
*sk
= asoc
->base
.sk
;
5781 long current_timeo
= *timeo_p
;
5784 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5785 asoc
, (long)(*timeo_p
), msg_len
);
5787 /* Increment the association's refcnt. */
5788 sctp_association_hold(asoc
);
5790 /* Wait on the association specific sndbuf space. */
5792 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5793 TASK_INTERRUPTIBLE
);
5796 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5799 if (signal_pending(current
))
5800 goto do_interrupted
;
5801 if (msg_len
<= sctp_wspace(asoc
))
5804 /* Let another process have a go. Since we are going
5807 sctp_release_sock(sk
);
5808 current_timeo
= schedule_timeout(current_timeo
);
5809 BUG_ON(sk
!= asoc
->base
.sk
);
5812 *timeo_p
= current_timeo
;
5816 finish_wait(&asoc
->wait
, &wait
);
5818 /* Release the association's refcnt. */
5819 sctp_association_put(asoc
);
5828 err
= sock_intr_errno(*timeo_p
);
5836 /* If socket sndbuf has changed, wake up all per association waiters. */
5837 void sctp_write_space(struct sock
*sk
)
5839 struct sctp_association
*asoc
;
5840 struct list_head
*pos
;
5842 /* Wake up the tasks in each wait queue. */
5843 list_for_each(pos
, &((sctp_sk(sk
))->ep
->asocs
)) {
5844 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
5845 __sctp_write_space(asoc
);
5849 /* Is there any sndbuf space available on the socket?
5851 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5852 * associations on the same socket. For a UDP-style socket with
5853 * multiple associations, it is possible for it to be "unwriteable"
5854 * prematurely. I assume that this is acceptable because
5855 * a premature "unwriteable" is better than an accidental "writeable" which
5856 * would cause an unwanted block under certain circumstances. For the 1-1
5857 * UDP-style sockets or TCP-style sockets, this code should work.
5860 static int sctp_writeable(struct sock
*sk
)
5864 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
5870 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5871 * returns immediately with EINPROGRESS.
5873 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
5875 struct sock
*sk
= asoc
->base
.sk
;
5877 long current_timeo
= *timeo_p
;
5880 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__
, asoc
,
5883 /* Increment the association's refcnt. */
5884 sctp_association_hold(asoc
);
5887 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5888 TASK_INTERRUPTIBLE
);
5891 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5893 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5896 if (signal_pending(current
))
5897 goto do_interrupted
;
5899 if (sctp_state(asoc
, ESTABLISHED
))
5902 /* Let another process have a go. Since we are going
5905 sctp_release_sock(sk
);
5906 current_timeo
= schedule_timeout(current_timeo
);
5909 *timeo_p
= current_timeo
;
5913 finish_wait(&asoc
->wait
, &wait
);
5915 /* Release the association's refcnt. */
5916 sctp_association_put(asoc
);
5921 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
5924 err
= -ECONNREFUSED
;
5928 err
= sock_intr_errno(*timeo_p
);
5936 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
5938 struct sctp_endpoint
*ep
;
5942 ep
= sctp_sk(sk
)->ep
;
5946 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
5947 TASK_INTERRUPTIBLE
);
5949 if (list_empty(&ep
->asocs
)) {
5950 sctp_release_sock(sk
);
5951 timeo
= schedule_timeout(timeo
);
5956 if (!sctp_sstate(sk
, LISTENING
))
5960 if (!list_empty(&ep
->asocs
))
5963 err
= sock_intr_errno(timeo
);
5964 if (signal_pending(current
))
5972 finish_wait(sk
->sk_sleep
, &wait
);
5977 static void sctp_wait_for_close(struct sock
*sk
, long timeout
)
5982 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5983 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
5985 sctp_release_sock(sk
);
5986 timeout
= schedule_timeout(timeout
);
5988 } while (!signal_pending(current
) && timeout
);
5990 finish_wait(sk
->sk_sleep
, &wait
);
5993 static void sctp_sock_rfree_frag(struct sk_buff
*skb
)
5995 struct sk_buff
*frag
;
6000 /* Don't forget the fragments. */
6001 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
6002 sctp_sock_rfree_frag(frag
);
6005 sctp_sock_rfree(skb
);
6008 static void sctp_skb_set_owner_r_frag(struct sk_buff
*skb
, struct sock
*sk
)
6010 struct sk_buff
*frag
;
6015 /* Don't forget the fragments. */
6016 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
6017 sctp_skb_set_owner_r_frag(frag
, sk
);
6020 sctp_skb_set_owner_r(skb
, sk
);
6023 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6024 * and its messages to the newsk.
6026 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
6027 struct sctp_association
*assoc
,
6028 sctp_socket_type_t type
)
6030 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
6031 struct sctp_sock
*newsp
= sctp_sk(newsk
);
6032 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
6033 struct sctp_endpoint
*newep
= newsp
->ep
;
6034 struct sk_buff
*skb
, *tmp
;
6035 struct sctp_ulpevent
*event
;
6038 /* Migrate socket buffer sizes and all the socket level options to the
6041 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
6042 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
6043 /* Brute force copy old sctp opt. */
6044 inet_sk_copy_descendant(newsk
, oldsk
);
6046 /* Restore the ep value that was overwritten with the above structure
6052 /* Hook this new socket in to the bind_hash list. */
6053 pp
= sctp_sk(oldsk
)->bind_hash
;
6054 sk_add_bind_node(newsk
, &pp
->owner
);
6055 sctp_sk(newsk
)->bind_hash
= pp
;
6056 inet_sk(newsk
)->num
= inet_sk(oldsk
)->num
;
6058 /* Copy the bind_addr list from the original endpoint to the new
6059 * endpoint so that we can handle restarts properly
6061 if (PF_INET6
== assoc
->base
.sk
->sk_family
)
6062 flags
= SCTP_ADDR6_ALLOWED
;
6063 if (assoc
->peer
.ipv4_address
)
6064 flags
|= SCTP_ADDR4_PEERSUPP
;
6065 if (assoc
->peer
.ipv6_address
)
6066 flags
|= SCTP_ADDR6_PEERSUPP
;
6067 sctp_bind_addr_copy(&newsp
->ep
->base
.bind_addr
,
6068 &oldsp
->ep
->base
.bind_addr
,
6069 SCTP_SCOPE_GLOBAL
, GFP_KERNEL
, flags
);
6071 /* Move any messages in the old socket's receive queue that are for the
6072 * peeled off association to the new socket's receive queue.
6074 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
6075 event
= sctp_skb2event(skb
);
6076 if (event
->asoc
== assoc
) {
6077 sctp_sock_rfree_frag(skb
);
6078 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
6079 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
6080 sctp_skb_set_owner_r_frag(skb
, newsk
);
6084 /* Clean up any messages pending delivery due to partial
6085 * delivery. Three cases:
6086 * 1) No partial deliver; no work.
6087 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6088 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6090 skb_queue_head_init(&newsp
->pd_lobby
);
6091 atomic_set(&sctp_sk(newsk
)->pd_mode
, assoc
->ulpq
.pd_mode
);
6093 if (atomic_read(&sctp_sk(oldsk
)->pd_mode
)) {
6094 struct sk_buff_head
*queue
;
6096 /* Decide which queue to move pd_lobby skbs to. */
6097 if (assoc
->ulpq
.pd_mode
) {
6098 queue
= &newsp
->pd_lobby
;
6100 queue
= &newsk
->sk_receive_queue
;
6102 /* Walk through the pd_lobby, looking for skbs that
6103 * need moved to the new socket.
6105 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
6106 event
= sctp_skb2event(skb
);
6107 if (event
->asoc
== assoc
) {
6108 sctp_sock_rfree_frag(skb
);
6109 __skb_unlink(skb
, &oldsp
->pd_lobby
);
6110 __skb_queue_tail(queue
, skb
);
6111 sctp_skb_set_owner_r_frag(skb
, newsk
);
6115 /* Clear up any skbs waiting for the partial
6116 * delivery to finish.
6118 if (assoc
->ulpq
.pd_mode
)
6119 sctp_clear_pd(oldsk
, NULL
);
6123 sctp_skb_for_each(skb
, &assoc
->ulpq
.reasm
, tmp
) {
6124 sctp_sock_rfree_frag(skb
);
6125 sctp_skb_set_owner_r_frag(skb
, newsk
);
6128 sctp_skb_for_each(skb
, &assoc
->ulpq
.lobby
, tmp
) {
6129 sctp_sock_rfree_frag(skb
);
6130 sctp_skb_set_owner_r_frag(skb
, newsk
);
6133 /* Set the type of socket to indicate that it is peeled off from the
6134 * original UDP-style socket or created with the accept() call on a
6135 * TCP-style socket..
6139 /* Mark the new socket "in-use" by the user so that any packets
6140 * that may arrive on the association after we've moved it are
6141 * queued to the backlog. This prevents a potential race between
6142 * backlog processing on the old socket and new-packet processing
6143 * on the new socket.
6145 * The caller has just allocated newsk so we can guarantee that other
6146 * paths won't try to lock it and then oldsk.
6148 lock_sock_nested(newsk
, SINGLE_DEPTH_NESTING
);
6149 sctp_assoc_migrate(assoc
, newsk
);
6151 /* If the association on the newsk is already closed before accept()
6152 * is called, set RCV_SHUTDOWN flag.
6154 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
6155 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
6157 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
6158 sctp_release_sock(newsk
);
6162 /* This proto struct describes the ULP interface for SCTP. */
6163 struct proto sctp_prot
= {
6165 .owner
= THIS_MODULE
,
6166 .close
= sctp_close
,
6167 .connect
= sctp_connect
,
6168 .disconnect
= sctp_disconnect
,
6169 .accept
= sctp_accept
,
6170 .ioctl
= sctp_ioctl
,
6171 .init
= sctp_init_sock
,
6172 .destroy
= sctp_destroy_sock
,
6173 .shutdown
= sctp_shutdown
,
6174 .setsockopt
= sctp_setsockopt
,
6175 .getsockopt
= sctp_getsockopt
,
6176 .sendmsg
= sctp_sendmsg
,
6177 .recvmsg
= sctp_recvmsg
,
6179 .backlog_rcv
= sctp_backlog_rcv
,
6181 .unhash
= sctp_unhash
,
6182 .get_port
= sctp_get_port
,
6183 .obj_size
= sizeof(struct sctp_sock
),
6184 .sysctl_mem
= sysctl_sctp_mem
,
6185 .sysctl_rmem
= sysctl_sctp_rmem
,
6186 .sysctl_wmem
= sysctl_sctp_wmem
,
6187 .memory_pressure
= &sctp_memory_pressure
,
6188 .enter_memory_pressure
= sctp_enter_memory_pressure
,
6189 .memory_allocated
= &sctp_memory_allocated
,
6192 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6193 struct proto sctpv6_prot
= {
6195 .owner
= THIS_MODULE
,
6196 .close
= sctp_close
,
6197 .connect
= sctp_connect
,
6198 .disconnect
= sctp_disconnect
,
6199 .accept
= sctp_accept
,
6200 .ioctl
= sctp_ioctl
,
6201 .init
= sctp_init_sock
,
6202 .destroy
= sctp_destroy_sock
,
6203 .shutdown
= sctp_shutdown
,
6204 .setsockopt
= sctp_setsockopt
,
6205 .getsockopt
= sctp_getsockopt
,
6206 .sendmsg
= sctp_sendmsg
,
6207 .recvmsg
= sctp_recvmsg
,
6209 .backlog_rcv
= sctp_backlog_rcv
,
6211 .unhash
= sctp_unhash
,
6212 .get_port
= sctp_get_port
,
6213 .obj_size
= sizeof(struct sctp6_sock
),
6214 .sysctl_mem
= sysctl_sctp_mem
,
6215 .sysctl_rmem
= sysctl_sctp_rmem
,
6216 .sysctl_wmem
= sysctl_sctp_wmem
,
6217 .memory_pressure
= &sctp_memory_pressure
,
6218 .enter_memory_pressure
= sctp_enter_memory_pressure
,
6219 .memory_allocated
= &sctp_memory_allocated
,
6221 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */