1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/types.h>
61 #include <linux/kernel.h>
62 #include <linux/wait.h>
63 #include <linux/time.h>
65 #include <linux/capability.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
73 #include <net/route.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock
*sk
);
89 static void sctp_wfree(struct sk_buff
*skb
);
90 static int sctp_wait_for_sndbuf(struct sctp_association
*, long *timeo_p
,
92 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
);
93 static int sctp_wait_for_connect(struct sctp_association
*, long *timeo_p
);
94 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
);
95 static void sctp_wait_for_close(struct sock
*sk
, long timeo
);
96 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
97 union sctp_addr
*addr
, int len
);
98 static int sctp_bindx_add(struct sock
*, struct sockaddr
*, int);
99 static int sctp_bindx_rem(struct sock
*, struct sockaddr
*, int);
100 static int sctp_send_asconf_add_ip(struct sock
*, struct sockaddr
*, int);
101 static int sctp_send_asconf_del_ip(struct sock
*, struct sockaddr
*, int);
102 static int sctp_send_asconf(struct sctp_association
*asoc
,
103 struct sctp_chunk
*chunk
);
104 static int sctp_do_bind(struct sock
*, union sctp_addr
*, int);
105 static int sctp_autobind(struct sock
*sk
);
106 static void sctp_sock_migrate(struct sock
*, struct sock
*,
107 struct sctp_association
*, sctp_socket_type_t
);
108 static char *sctp_hmac_alg
= SCTP_COOKIE_HMAC_ALG
;
110 extern struct kmem_cache
*sctp_bucket_cachep
;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association
*asoc
)
115 struct sock
*sk
= asoc
->base
.sk
;
118 if (asoc
->ep
->sndbuf_policy
) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt
= sk
->sk_sndbuf
- asoc
->sndbuf_used
;
122 /* do socket level accounting */
123 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
141 static inline void sctp_set_owner_w(struct sctp_chunk
*chunk
)
143 struct sctp_association
*asoc
= chunk
->asoc
;
144 struct sock
*sk
= asoc
->base
.sk
;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc
);
149 skb_set_owner_w(chunk
->skb
, sk
);
151 chunk
->skb
->destructor
= sctp_wfree
;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk
**)(chunk
->skb
->cb
)) = chunk
;
155 asoc
->sndbuf_used
+= SCTP_DATA_SNDSIZE(chunk
) +
156 sizeof(struct sk_buff
) +
157 sizeof(struct sctp_chunk
);
159 atomic_add(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
162 /* Verify that this is a valid address. */
163 static inline int sctp_verify_addr(struct sock
*sk
, union sctp_addr
*addr
,
168 /* Verify basic sockaddr. */
169 af
= sctp_sockaddr_af(sctp_sk(sk
), addr
, len
);
173 /* Is this a valid SCTP address? */
174 if (!af
->addr_valid(addr
, sctp_sk(sk
), NULL
))
177 if (!sctp_sk(sk
)->pf
->send_verify(sctp_sk(sk
), (addr
)))
183 /* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
186 struct sctp_association
*sctp_id2assoc(struct sock
*sk
, sctp_assoc_t id
)
188 struct sctp_association
*asoc
= NULL
;
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk
, UDP
)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
196 if (!sctp_sstate(sk
, ESTABLISHED
))
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk
)->ep
->asocs
))
201 asoc
= list_entry(sctp_sk(sk
)->ep
->asocs
.next
,
202 struct sctp_association
, asocs
);
206 /* Otherwise this is a UDP-style socket. */
207 if (!id
|| (id
== (sctp_assoc_t
)-1))
210 spin_lock_bh(&sctp_assocs_id_lock
);
211 asoc
= (struct sctp_association
*)idr_find(&sctp_assocs_id
, (int)id
);
212 spin_unlock_bh(&sctp_assocs_id_lock
);
214 if (!asoc
|| (asoc
->base
.sk
!= sk
) || asoc
->base
.dead
)
220 /* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
224 static struct sctp_transport
*sctp_addr_id2transport(struct sock
*sk
,
225 struct sockaddr_storage
*addr
,
228 struct sctp_association
*addr_asoc
= NULL
, *id_asoc
= NULL
;
229 struct sctp_transport
*transport
;
230 union sctp_addr
*laddr
= (union sctp_addr
*)addr
;
232 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
239 id_asoc
= sctp_id2assoc(sk
, id
);
240 if (id_asoc
&& (id_asoc
!= addr_asoc
))
243 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
244 (union sctp_addr
*)addr
);
249 /* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
259 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
270 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
275 sctp_release_sock(sk
);
280 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
282 /* Verify this is a valid sockaddr. */
283 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
284 union sctp_addr
*addr
, int len
)
288 /* Check minimum size. */
289 if (len
< sizeof (struct sockaddr
))
292 /* Does this PF support this AF? */
293 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
296 /* If we get this far, af is valid. */
297 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
299 if (len
< af
->sockaddr_len
)
305 /* Bind a local address either to an endpoint or to an association. */
306 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
308 struct sctp_sock
*sp
= sctp_sk(sk
);
309 struct sctp_endpoint
*ep
= sp
->ep
;
310 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
315 /* Common sockaddr verification. */
316 af
= sctp_sockaddr_af(sp
, addr
, len
);
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
323 snum
= ntohs(addr
->v4
.sin_port
);
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
332 /* PF specific bind() address verification. */
333 if (!sp
->pf
->bind_verify(sp
, addr
))
334 return -EADDRNOTAVAIL
;
336 /* We must either be unbound, or bind to the same port. */
337 if (bp
->port
&& (snum
!= bp
->port
)) {
338 SCTP_DEBUG_PRINTK("sctp_do_bind:"
339 " New port %d does not match existing port "
340 "%d.\n", snum
, bp
->port
);
344 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
347 /* Make sure we are allowed to bind here.
348 * The function sctp_get_port_local() does duplicate address
351 if ((ret
= sctp_get_port_local(sk
, addr
))) {
352 if (ret
== (long) sk
) {
353 /* This endpoint has a conflicting address. */
360 /* Refresh ephemeral port. */
362 bp
->port
= inet_sk(sk
)->num
;
364 /* Add the address to the bind address list. */
365 sctp_local_bh_disable();
366 sctp_write_lock(&ep
->base
.addr_lock
);
368 /* Use GFP_ATOMIC since BHs are disabled. */
369 ret
= sctp_add_bind_addr(bp
, addr
, 1, GFP_ATOMIC
);
370 sctp_write_unlock(&ep
->base
.addr_lock
);
371 sctp_local_bh_enable();
373 /* Copy back into socket for getsockname() use. */
375 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
376 af
->to_sk_saddr(addr
, sk
);
382 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
384 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
385 * at any one time. If a sender, after sending an ASCONF chunk, decides
386 * it needs to transfer another ASCONF Chunk, it MUST wait until the
387 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
388 * subsequent ASCONF. Note this restriction binds each side, so at any
389 * time two ASCONF may be in-transit on any given association (one sent
390 * from each endpoint).
392 static int sctp_send_asconf(struct sctp_association
*asoc
,
393 struct sctp_chunk
*chunk
)
397 /* If there is an outstanding ASCONF chunk, queue it for later
400 if (asoc
->addip_last_asconf
) {
401 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
405 /* Hold the chunk until an ASCONF_ACK is received. */
406 sctp_chunk_hold(chunk
);
407 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
409 sctp_chunk_free(chunk
);
411 asoc
->addip_last_asconf
= chunk
;
417 /* Add a list of addresses as bind addresses to local endpoint or
420 * Basically run through each address specified in the addrs/addrcnt
421 * array/length pair, determine if it is IPv6 or IPv4 and call
422 * sctp_do_bind() on it.
424 * If any of them fails, then the operation will be reversed and the
425 * ones that were added will be removed.
427 * Only sctp_setsockopt_bindx() is supposed to call this function.
429 int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
434 struct sockaddr
*sa_addr
;
437 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
441 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
442 /* The list may contain either IPv4 or IPv6 address;
443 * determine the address length for walking thru the list.
445 sa_addr
= (struct sockaddr
*)addr_buf
;
446 af
= sctp_get_af_specific(sa_addr
->sa_family
);
452 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
455 addr_buf
+= af
->sockaddr_len
;
459 /* Failed. Cleanup the ones that have been added */
461 sctp_bindx_rem(sk
, addrs
, cnt
);
469 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
470 * associations that are part of the endpoint indicating that a list of local
471 * addresses are added to the endpoint.
473 * If any of the addresses is already in the bind address list of the
474 * association, we do not send the chunk for that association. But it will not
475 * affect other associations.
477 * Only sctp_setsockopt_bindx() is supposed to call this function.
479 static int sctp_send_asconf_add_ip(struct sock
*sk
,
480 struct sockaddr
*addrs
,
483 struct sctp_sock
*sp
;
484 struct sctp_endpoint
*ep
;
485 struct sctp_association
*asoc
;
486 struct sctp_bind_addr
*bp
;
487 struct sctp_chunk
*chunk
;
488 struct sctp_sockaddr_entry
*laddr
;
489 union sctp_addr
*addr
;
490 union sctp_addr saveaddr
;
493 struct list_head
*pos
;
498 if (!sctp_addip_enable
)
504 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
505 __FUNCTION__
, sk
, addrs
, addrcnt
);
507 list_for_each(pos
, &ep
->asocs
) {
508 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
510 if (!asoc
->peer
.asconf_capable
)
513 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
516 if (!sctp_state(asoc
, ESTABLISHED
))
519 /* Check if any address in the packed array of addresses is
520 * in the bind address list of the association. If so,
521 * do not send the asconf chunk to its peer, but continue with
522 * other associations.
525 for (i
= 0; i
< addrcnt
; i
++) {
526 addr
= (union sctp_addr
*)addr_buf
;
527 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
533 if (sctp_assoc_lookup_laddr(asoc
, addr
))
536 addr_buf
+= af
->sockaddr_len
;
541 /* Use the first address in bind addr list of association as
542 * Address Parameter of ASCONF CHUNK.
544 sctp_read_lock(&asoc
->base
.addr_lock
);
545 bp
= &asoc
->base
.bind_addr
;
546 p
= bp
->address_list
.next
;
547 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
548 sctp_read_unlock(&asoc
->base
.addr_lock
);
550 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
551 addrcnt
, SCTP_PARAM_ADD_IP
);
557 retval
= sctp_send_asconf(asoc
, chunk
);
561 /* Add the new addresses to the bind address list with
562 * use_as_src set to 0.
564 sctp_local_bh_disable();
565 sctp_write_lock(&asoc
->base
.addr_lock
);
567 for (i
= 0; i
< addrcnt
; i
++) {
568 addr
= (union sctp_addr
*)addr_buf
;
569 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
570 memcpy(&saveaddr
, addr
, af
->sockaddr_len
);
571 retval
= sctp_add_bind_addr(bp
, &saveaddr
, 0,
573 addr_buf
+= af
->sockaddr_len
;
575 sctp_write_unlock(&asoc
->base
.addr_lock
);
576 sctp_local_bh_enable();
583 /* Remove a list of addresses from bind addresses list. Do not remove the
586 * Basically run through each address specified in the addrs/addrcnt
587 * array/length pair, determine if it is IPv6 or IPv4 and call
588 * sctp_del_bind() on it.
590 * If any of them fails, then the operation will be reversed and the
591 * ones that were removed will be added back.
593 * At least one address has to be left; if only one address is
594 * available, the operation will return -EBUSY.
596 * Only sctp_setsockopt_bindx() is supposed to call this function.
598 int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
600 struct sctp_sock
*sp
= sctp_sk(sk
);
601 struct sctp_endpoint
*ep
= sp
->ep
;
603 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
606 union sctp_addr
*sa_addr
;
609 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
613 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
614 /* If the bind address list is empty or if there is only one
615 * bind address, there is nothing more to be removed (we need
616 * at least one address here).
618 if (list_empty(&bp
->address_list
) ||
619 (sctp_list_single_entry(&bp
->address_list
))) {
624 sa_addr
= (union sctp_addr
*)addr_buf
;
625 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
631 if (!af
->addr_valid(sa_addr
, sp
, NULL
)) {
632 retval
= -EADDRNOTAVAIL
;
636 if (sa_addr
->v4
.sin_port
!= htons(bp
->port
)) {
641 /* FIXME - There is probably a need to check if sk->sk_saddr and
642 * sk->sk_rcv_addr are currently set to one of the addresses to
643 * be removed. This is something which needs to be looked into
644 * when we are fixing the outstanding issues with multi-homing
645 * socket routing and failover schemes. Refer to comments in
646 * sctp_do_bind(). -daisy
648 sctp_local_bh_disable();
649 sctp_write_lock(&ep
->base
.addr_lock
);
651 retval
= sctp_del_bind_addr(bp
, sa_addr
);
653 sctp_write_unlock(&ep
->base
.addr_lock
);
654 sctp_local_bh_enable();
656 addr_buf
+= af
->sockaddr_len
;
659 /* Failed. Add the ones that has been removed back */
661 sctp_bindx_add(sk
, addrs
, cnt
);
669 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
670 * the associations that are part of the endpoint indicating that a list of
671 * local addresses are removed from the endpoint.
673 * If any of the addresses is already in the bind address list of the
674 * association, we do not send the chunk for that association. But it will not
675 * affect other associations.
677 * Only sctp_setsockopt_bindx() is supposed to call this function.
679 static int sctp_send_asconf_del_ip(struct sock
*sk
,
680 struct sockaddr
*addrs
,
683 struct sctp_sock
*sp
;
684 struct sctp_endpoint
*ep
;
685 struct sctp_association
*asoc
;
686 struct sctp_transport
*transport
;
687 struct sctp_bind_addr
*bp
;
688 struct sctp_chunk
*chunk
;
689 union sctp_addr
*laddr
;
692 struct list_head
*pos
, *pos1
;
693 struct sctp_sockaddr_entry
*saddr
;
697 if (!sctp_addip_enable
)
703 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
704 __FUNCTION__
, sk
, addrs
, addrcnt
);
706 list_for_each(pos
, &ep
->asocs
) {
707 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
709 if (!asoc
->peer
.asconf_capable
)
712 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
715 if (!sctp_state(asoc
, ESTABLISHED
))
718 /* Check if any address in the packed array of addresses is
719 * not present in the bind address list of the association.
720 * If so, do not send the asconf chunk to its peer, but
721 * continue with other associations.
724 for (i
= 0; i
< addrcnt
; i
++) {
725 laddr
= (union sctp_addr
*)addr_buf
;
726 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
732 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
735 addr_buf
+= af
->sockaddr_len
;
740 /* Find one address in the association's bind address list
741 * that is not in the packed array of addresses. This is to
742 * make sure that we do not delete all the addresses in the
745 sctp_read_lock(&asoc
->base
.addr_lock
);
746 bp
= &asoc
->base
.bind_addr
;
747 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
749 sctp_read_unlock(&asoc
->base
.addr_lock
);
753 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
760 /* Reset use_as_src flag for the addresses in the bind address
761 * list that are to be deleted.
763 sctp_local_bh_disable();
764 sctp_write_lock(&asoc
->base
.addr_lock
);
766 for (i
= 0; i
< addrcnt
; i
++) {
767 laddr
= (union sctp_addr
*)addr_buf
;
768 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
769 list_for_each(pos1
, &bp
->address_list
) {
770 saddr
= list_entry(pos1
,
771 struct sctp_sockaddr_entry
,
773 if (sctp_cmp_addr_exact(&saddr
->a
, laddr
))
774 saddr
->use_as_src
= 0;
776 addr_buf
+= af
->sockaddr_len
;
778 sctp_write_unlock(&asoc
->base
.addr_lock
);
779 sctp_local_bh_enable();
781 /* Update the route and saddr entries for all the transports
782 * as some of the addresses in the bind address list are
783 * about to be deleted and cannot be used as source addresses.
785 list_for_each(pos1
, &asoc
->peer
.transport_addr_list
) {
786 transport
= list_entry(pos1
, struct sctp_transport
,
788 dst_release(transport
->dst
);
789 sctp_transport_route(transport
, NULL
,
790 sctp_sk(asoc
->base
.sk
));
793 retval
= sctp_send_asconf(asoc
, chunk
);
799 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
802 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
805 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
806 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
809 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
810 * Section 3.1.2 for this usage.
812 * addrs is a pointer to an array of one or more socket addresses. Each
813 * address is contained in its appropriate structure (i.e. struct
814 * sockaddr_in or struct sockaddr_in6) the family of the address type
815 * must be used to distinguish the address length (note that this
816 * representation is termed a "packed array" of addresses). The caller
817 * specifies the number of addresses in the array with addrcnt.
819 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
820 * -1, and sets errno to the appropriate error code.
822 * For SCTP, the port given in each socket address must be the same, or
823 * sctp_bindx() will fail, setting errno to EINVAL.
825 * The flags parameter is formed from the bitwise OR of zero or more of
826 * the following currently defined flags:
828 * SCTP_BINDX_ADD_ADDR
830 * SCTP_BINDX_REM_ADDR
832 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
833 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
834 * addresses from the association. The two flags are mutually exclusive;
835 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
836 * not remove all addresses from an association; sctp_bindx() will
837 * reject such an attempt with EINVAL.
839 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
840 * additional addresses with an endpoint after calling bind(). Or use
841 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
842 * socket is associated with so that no new association accepted will be
843 * associated with those addresses. If the endpoint supports dynamic
844 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
845 * endpoint to send the appropriate message to the peer to change the
846 * peers address lists.
848 * Adding and removing addresses from a connected association is
849 * optional functionality. Implementations that do not support this
850 * functionality should return EOPNOTSUPP.
852 * Basically do nothing but copying the addresses from user to kernel
853 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
854 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
857 * We don't use copy_from_user() for optimization: we first do the
858 * sanity checks (buffer size -fast- and access check-healthy
859 * pointer); if all of those succeed, then we can alloc the memory
860 * (expensive operation) needed to copy the data to kernel. Then we do
861 * the copying without checking the user space area
862 * (__copy_from_user()).
864 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
867 * sk The sk of the socket
868 * addrs The pointer to the addresses in user land
869 * addrssize Size of the addrs buffer
870 * op Operation to perform (add or remove, see the flags of
873 * Returns 0 if ok, <0 errno code on error.
875 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
876 struct sockaddr __user
*addrs
,
877 int addrs_size
, int op
)
879 struct sockaddr
*kaddrs
;
883 struct sockaddr
*sa_addr
;
887 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
888 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
890 if (unlikely(addrs_size
<= 0))
893 /* Check the user passed a healthy pointer. */
894 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
897 /* Alloc space for the address array in kernel memory. */
898 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
899 if (unlikely(!kaddrs
))
902 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
907 /* Walk through the addrs buffer and count the number of addresses. */
909 while (walk_size
< addrs_size
) {
910 sa_addr
= (struct sockaddr
*)addr_buf
;
911 af
= sctp_get_af_specific(sa_addr
->sa_family
);
913 /* If the address family is not supported or if this address
914 * causes the address buffer to overflow return EINVAL.
916 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
921 addr_buf
+= af
->sockaddr_len
;
922 walk_size
+= af
->sockaddr_len
;
927 case SCTP_BINDX_ADD_ADDR
:
928 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
931 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
934 case SCTP_BINDX_REM_ADDR
:
935 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
938 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
952 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
954 * Common routine for handling connect() and sctp_connectx().
955 * Connect will come in with just a single address.
957 static int __sctp_connect(struct sock
* sk
,
958 struct sockaddr
*kaddrs
,
961 struct sctp_sock
*sp
;
962 struct sctp_endpoint
*ep
;
963 struct sctp_association
*asoc
= NULL
;
964 struct sctp_association
*asoc2
;
965 struct sctp_transport
*transport
;
973 union sctp_addr
*sa_addr
;
979 /* connect() cannot be done on a socket that is already in ESTABLISHED
980 * state - UDP-style peeled off socket or a TCP-style socket that
981 * is already connected.
982 * It cannot be done even on a TCP-style listening socket.
984 if (sctp_sstate(sk
, ESTABLISHED
) ||
985 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
990 /* Walk through the addrs buffer and count the number of addresses. */
992 while (walk_size
< addrs_size
) {
993 sa_addr
= (union sctp_addr
*)addr_buf
;
994 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
996 /* If the address family is not supported or if this address
997 * causes the address buffer to overflow return EINVAL.
999 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
1004 err
= sctp_verify_addr(sk
, sa_addr
, af
->sockaddr_len
);
1008 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
1010 /* Check if there already is a matching association on the
1011 * endpoint (other than the one created here).
1013 asoc2
= sctp_endpoint_lookup_assoc(ep
, sa_addr
, &transport
);
1014 if (asoc2
&& asoc2
!= asoc
) {
1015 if (asoc2
->state
>= SCTP_STATE_ESTABLISHED
)
1022 /* If we could not find a matching association on the endpoint,
1023 * make sure that there is no peeled-off association matching
1024 * the peer address even on another socket.
1026 if (sctp_endpoint_is_peeled_off(ep
, sa_addr
)) {
1027 err
= -EADDRNOTAVAIL
;
1032 /* If a bind() or sctp_bindx() is not called prior to
1033 * an sctp_connectx() call, the system picks an
1034 * ephemeral port and will choose an address set
1035 * equivalent to binding with a wildcard address.
1037 if (!ep
->base
.bind_addr
.port
) {
1038 if (sctp_autobind(sk
)) {
1044 * If an unprivileged user inherits a 1-many
1045 * style socket with open associations on a
1046 * privileged port, it MAY be permitted to
1047 * accept new associations, but it SHOULD NOT
1048 * be permitted to open new associations.
1050 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1051 !capable(CAP_NET_BIND_SERVICE
)) {
1057 scope
= sctp_scope(sa_addr
);
1058 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1065 /* Prime the peer's transport structures. */
1066 transport
= sctp_assoc_add_peer(asoc
, sa_addr
, GFP_KERNEL
,
1074 addr_buf
+= af
->sockaddr_len
;
1075 walk_size
+= af
->sockaddr_len
;
1078 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1083 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1088 /* Initialize sk's dport and daddr for getpeername() */
1089 inet_sk(sk
)->dport
= htons(asoc
->peer
.port
);
1090 af
= sctp_get_af_specific(to
.sa
.sa_family
);
1091 af
->to_sk_daddr(&to
, sk
);
1094 timeo
= sock_sndtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
1095 err
= sctp_wait_for_connect(asoc
, &timeo
);
1097 /* Don't free association on exit. */
1102 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1103 " kaddrs: %p err: %d\n",
1106 sctp_association_free(asoc
);
1110 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1113 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1115 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1116 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1117 * or IPv6 addresses.
1119 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1120 * Section 3.1.2 for this usage.
1122 * addrs is a pointer to an array of one or more socket addresses. Each
1123 * address is contained in its appropriate structure (i.e. struct
1124 * sockaddr_in or struct sockaddr_in6) the family of the address type
1125 * must be used to distengish the address length (note that this
1126 * representation is termed a "packed array" of addresses). The caller
1127 * specifies the number of addresses in the array with addrcnt.
1129 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1130 * -1, and sets errno to the appropriate error code.
1132 * For SCTP, the port given in each socket address must be the same, or
1133 * sctp_connectx() will fail, setting errno to EINVAL.
1135 * An application can use sctp_connectx to initiate an association with
1136 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1137 * allows a caller to specify multiple addresses at which a peer can be
1138 * reached. The way the SCTP stack uses the list of addresses to set up
1139 * the association is implementation dependant. This function only
1140 * specifies that the stack will try to make use of all the addresses in
1141 * the list when needed.
1143 * Note that the list of addresses passed in is only used for setting up
1144 * the association. It does not necessarily equal the set of addresses
1145 * the peer uses for the resulting association. If the caller wants to
1146 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1147 * retrieve them after the association has been set up.
1149 * Basically do nothing but copying the addresses from user to kernel
1150 * land and invoking either sctp_connectx(). This is used for tunneling
1151 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1153 * We don't use copy_from_user() for optimization: we first do the
1154 * sanity checks (buffer size -fast- and access check-healthy
1155 * pointer); if all of those succeed, then we can alloc the memory
1156 * (expensive operation) needed to copy the data to kernel. Then we do
1157 * the copying without checking the user space area
1158 * (__copy_from_user()).
1160 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1163 * sk The sk of the socket
1164 * addrs The pointer to the addresses in user land
1165 * addrssize Size of the addrs buffer
1167 * Returns 0 if ok, <0 errno code on error.
1169 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1170 struct sockaddr __user
*addrs
,
1174 struct sockaddr
*kaddrs
;
1176 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1177 __FUNCTION__
, sk
, addrs
, addrs_size
);
1179 if (unlikely(addrs_size
<= 0))
1182 /* Check the user passed a healthy pointer. */
1183 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1186 /* Alloc space for the address array in kernel memory. */
1187 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1188 if (unlikely(!kaddrs
))
1191 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1194 err
= __sctp_connect(sk
, kaddrs
, addrs_size
);
1201 /* API 3.1.4 close() - UDP Style Syntax
1202 * Applications use close() to perform graceful shutdown (as described in
1203 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1204 * by a UDP-style socket.
1208 * ret = close(int sd);
1210 * sd - the socket descriptor of the associations to be closed.
1212 * To gracefully shutdown a specific association represented by the
1213 * UDP-style socket, an application should use the sendmsg() call,
1214 * passing no user data, but including the appropriate flag in the
1215 * ancillary data (see Section xxxx).
1217 * If sd in the close() call is a branched-off socket representing only
1218 * one association, the shutdown is performed on that association only.
1220 * 4.1.6 close() - TCP Style Syntax
1222 * Applications use close() to gracefully close down an association.
1226 * int close(int sd);
1228 * sd - the socket descriptor of the association to be closed.
1230 * After an application calls close() on a socket descriptor, no further
1231 * socket operations will succeed on that descriptor.
1233 * API 7.1.4 SO_LINGER
1235 * An application using the TCP-style socket can use this option to
1236 * perform the SCTP ABORT primitive. The linger option structure is:
1239 * int l_onoff; // option on/off
1240 * int l_linger; // linger time
1243 * To enable the option, set l_onoff to 1. If the l_linger value is set
1244 * to 0, calling close() is the same as the ABORT primitive. If the
1245 * value is set to a negative value, the setsockopt() call will return
1246 * an error. If the value is set to a positive value linger_time, the
1247 * close() can be blocked for at most linger_time ms. If the graceful
1248 * shutdown phase does not finish during this period, close() will
1249 * return but the graceful shutdown phase continues in the system.
1251 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1253 struct sctp_endpoint
*ep
;
1254 struct sctp_association
*asoc
;
1255 struct list_head
*pos
, *temp
;
1257 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1260 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1262 ep
= sctp_sk(sk
)->ep
;
1264 /* Walk all associations on an endpoint. */
1265 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1266 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1268 if (sctp_style(sk
, TCP
)) {
1269 /* A closed association can still be in the list if
1270 * it belongs to a TCP-style listening socket that is
1271 * not yet accepted. If so, free it. If not, send an
1272 * ABORT or SHUTDOWN based on the linger options.
1274 if (sctp_state(asoc
, CLOSED
)) {
1275 sctp_unhash_established(asoc
);
1276 sctp_association_free(asoc
);
1281 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
) {
1282 struct sctp_chunk
*chunk
;
1284 chunk
= sctp_make_abort_user(asoc
, NULL
, 0);
1286 sctp_primitive_ABORT(asoc
, chunk
);
1288 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1291 /* Clean up any skbs sitting on the receive queue. */
1292 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1293 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1295 /* On a TCP-style socket, block for at most linger_time if set. */
1296 if (sctp_style(sk
, TCP
) && timeout
)
1297 sctp_wait_for_close(sk
, timeout
);
1299 /* This will run the backlog queue. */
1300 sctp_release_sock(sk
);
1302 /* Supposedly, no process has access to the socket, but
1303 * the net layers still may.
1305 sctp_local_bh_disable();
1306 sctp_bh_lock_sock(sk
);
1308 /* Hold the sock, since sk_common_release() will put sock_put()
1309 * and we have just a little more cleanup.
1312 sk_common_release(sk
);
1314 sctp_bh_unlock_sock(sk
);
1315 sctp_local_bh_enable();
1319 SCTP_DBG_OBJCNT_DEC(sock
);
1322 /* Handle EPIPE error. */
1323 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1326 err
= sock_error(sk
) ? : -EPIPE
;
1327 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1328 send_sig(SIGPIPE
, current
, 0);
1332 /* API 3.1.3 sendmsg() - UDP Style Syntax
1334 * An application uses sendmsg() and recvmsg() calls to transmit data to
1335 * and receive data from its peer.
1337 * ssize_t sendmsg(int socket, const struct msghdr *message,
1340 * socket - the socket descriptor of the endpoint.
1341 * message - pointer to the msghdr structure which contains a single
1342 * user message and possibly some ancillary data.
1344 * See Section 5 for complete description of the data
1347 * flags - flags sent or received with the user message, see Section
1348 * 5 for complete description of the flags.
1350 * Note: This function could use a rewrite especially when explicit
1351 * connect support comes in.
1353 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1355 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1357 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1358 struct msghdr
*msg
, size_t msg_len
)
1360 struct sctp_sock
*sp
;
1361 struct sctp_endpoint
*ep
;
1362 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1363 struct sctp_transport
*transport
, *chunk_tp
;
1364 struct sctp_chunk
*chunk
;
1366 struct sockaddr
*msg_name
= NULL
;
1367 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1368 struct sctp_sndrcvinfo
*sinfo
;
1369 struct sctp_initmsg
*sinit
;
1370 sctp_assoc_t associd
= 0;
1371 sctp_cmsgs_t cmsgs
= { NULL
};
1375 __u16 sinfo_flags
= 0;
1376 struct sctp_datamsg
*datamsg
;
1377 struct list_head
*pos
;
1378 int msg_flags
= msg
->msg_flags
;
1380 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1387 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1389 /* We cannot send a message over a TCP-style listening socket. */
1390 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1395 /* Parse out the SCTP CMSGs. */
1396 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1399 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1403 /* Fetch the destination address for this packet. This
1404 * address only selects the association--it is not necessarily
1405 * the address we will send to.
1406 * For a peeled-off socket, msg_name is ignored.
1408 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1409 int msg_namelen
= msg
->msg_namelen
;
1411 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1416 if (msg_namelen
> sizeof(to
))
1417 msg_namelen
= sizeof(to
);
1418 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1419 msg_name
= msg
->msg_name
;
1425 /* Did the user specify SNDRCVINFO? */
1427 sinfo_flags
= sinfo
->sinfo_flags
;
1428 associd
= sinfo
->sinfo_assoc_id
;
1431 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1432 msg_len
, sinfo_flags
);
1434 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1435 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1440 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1441 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1442 * If SCTP_ABORT is set, the message length could be non zero with
1443 * the msg_iov set to the user abort reason.
1445 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1446 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1451 /* If SCTP_ADDR_OVER is set, there must be an address
1452 * specified in msg_name.
1454 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1461 SCTP_DEBUG_PRINTK("About to look up association.\n");
1465 /* If a msg_name has been specified, assume this is to be used. */
1467 /* Look for a matching association on the endpoint. */
1468 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1470 /* If we could not find a matching association on the
1471 * endpoint, make sure that it is not a TCP-style
1472 * socket that already has an association or there is
1473 * no peeled-off association on another socket.
1475 if ((sctp_style(sk
, TCP
) &&
1476 sctp_sstate(sk
, ESTABLISHED
)) ||
1477 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1478 err
= -EADDRNOTAVAIL
;
1483 asoc
= sctp_id2assoc(sk
, associd
);
1491 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1493 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1494 * socket that has an association in CLOSED state. This can
1495 * happen when an accepted socket has an association that is
1498 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1503 if (sinfo_flags
& SCTP_EOF
) {
1504 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1506 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1510 if (sinfo_flags
& SCTP_ABORT
) {
1511 struct sctp_chunk
*chunk
;
1513 chunk
= sctp_make_abort_user(asoc
, msg
, msg_len
);
1519 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1520 sctp_primitive_ABORT(asoc
, chunk
);
1526 /* Do we need to create the association? */
1528 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1530 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1535 /* Check for invalid stream against the stream counts,
1536 * either the default or the user specified stream counts.
1539 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1540 /* Check against the defaults. */
1541 if (sinfo
->sinfo_stream
>=
1542 sp
->initmsg
.sinit_num_ostreams
) {
1547 /* Check against the requested. */
1548 if (sinfo
->sinfo_stream
>=
1549 sinit
->sinit_num_ostreams
) {
1557 * API 3.1.2 bind() - UDP Style Syntax
1558 * If a bind() or sctp_bindx() is not called prior to a
1559 * sendmsg() call that initiates a new association, the
1560 * system picks an ephemeral port and will choose an address
1561 * set equivalent to binding with a wildcard address.
1563 if (!ep
->base
.bind_addr
.port
) {
1564 if (sctp_autobind(sk
)) {
1570 * If an unprivileged user inherits a one-to-many
1571 * style socket with open associations on a privileged
1572 * port, it MAY be permitted to accept new associations,
1573 * but it SHOULD NOT be permitted to open new
1576 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1577 !capable(CAP_NET_BIND_SERVICE
)) {
1583 scope
= sctp_scope(&to
);
1584 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1591 /* If the SCTP_INIT ancillary data is specified, set all
1592 * the association init values accordingly.
1595 if (sinit
->sinit_num_ostreams
) {
1596 asoc
->c
.sinit_num_ostreams
=
1597 sinit
->sinit_num_ostreams
;
1599 if (sinit
->sinit_max_instreams
) {
1600 asoc
->c
.sinit_max_instreams
=
1601 sinit
->sinit_max_instreams
;
1603 if (sinit
->sinit_max_attempts
) {
1604 asoc
->max_init_attempts
1605 = sinit
->sinit_max_attempts
;
1607 if (sinit
->sinit_max_init_timeo
) {
1608 asoc
->max_init_timeo
=
1609 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1613 /* Prime the peer's transport structures. */
1614 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1619 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1626 /* ASSERT: we have a valid association at this point. */
1627 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1630 /* If the user didn't specify SNDRCVINFO, make up one with
1633 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1634 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1635 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1636 default_sinfo
.sinfo_context
= asoc
->default_context
;
1637 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1638 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1639 sinfo
= &default_sinfo
;
1642 /* API 7.1.7, the sndbuf size per association bounds the
1643 * maximum size of data that can be sent in a single send call.
1645 if (msg_len
> sk
->sk_sndbuf
) {
1650 /* If fragmentation is disabled and the message length exceeds the
1651 * association fragmentation point, return EMSGSIZE. The I-D
1652 * does not specify what this error is, but this looks like
1655 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1661 /* Check for invalid stream. */
1662 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1668 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1669 if (!sctp_wspace(asoc
)) {
1670 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1675 /* If an address is passed with the sendto/sendmsg call, it is used
1676 * to override the primary destination address in the TCP model, or
1677 * when SCTP_ADDR_OVER flag is set in the UDP model.
1679 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1680 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1681 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1689 /* Auto-connect, if we aren't connected already. */
1690 if (sctp_state(asoc
, CLOSED
)) {
1691 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1694 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1697 /* Break the message into multiple chunks of maximum size. */
1698 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1704 /* Now send the (possibly) fragmented message. */
1705 list_for_each(pos
, &datamsg
->chunks
) {
1706 chunk
= list_entry(pos
, struct sctp_chunk
, frag_list
);
1707 sctp_datamsg_track(chunk
);
1709 /* Do accounting for the write space. */
1710 sctp_set_owner_w(chunk
);
1712 chunk
->transport
= chunk_tp
;
1714 /* Send it to the lower layers. Note: all chunks
1715 * must either fail or succeed. The lower layer
1716 * works that way today. Keep it that way or this
1719 err
= sctp_primitive_SEND(asoc
, chunk
);
1720 /* Did the lower layer accept the chunk? */
1722 sctp_chunk_free(chunk
);
1723 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1726 sctp_datamsg_free(datamsg
);
1732 /* If we are already past ASSOCIATE, the lower
1733 * layers are responsible for association cleanup.
1739 sctp_association_free(asoc
);
1741 sctp_release_sock(sk
);
1744 return sctp_error(sk
, msg_flags
, err
);
1751 err
= sock_error(sk
);
1761 /* This is an extended version of skb_pull() that removes the data from the
1762 * start of a skb even when data is spread across the list of skb's in the
1763 * frag_list. len specifies the total amount of data that needs to be removed.
1764 * when 'len' bytes could be removed from the skb, it returns 0.
1765 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1766 * could not be removed.
1768 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1770 struct sk_buff
*list
;
1771 int skb_len
= skb_headlen(skb
);
1774 if (len
<= skb_len
) {
1775 __skb_pull(skb
, len
);
1779 __skb_pull(skb
, skb_len
);
1781 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
) {
1782 rlen
= sctp_skb_pull(list
, len
);
1783 skb
->len
-= (len
-rlen
);
1784 skb
->data_len
-= (len
-rlen
);
1795 /* API 3.1.3 recvmsg() - UDP Style Syntax
1797 * ssize_t recvmsg(int socket, struct msghdr *message,
1800 * socket - the socket descriptor of the endpoint.
1801 * message - pointer to the msghdr structure which contains a single
1802 * user message and possibly some ancillary data.
1804 * See Section 5 for complete description of the data
1807 * flags - flags sent or received with the user message, see Section
1808 * 5 for complete description of the flags.
1810 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1812 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1813 struct msghdr
*msg
, size_t len
, int noblock
,
1814 int flags
, int *addr_len
)
1816 struct sctp_ulpevent
*event
= NULL
;
1817 struct sctp_sock
*sp
= sctp_sk(sk
);
1818 struct sk_buff
*skb
;
1823 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1824 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1825 "len", len
, "knoblauch", noblock
,
1826 "flags", flags
, "addr_len", addr_len
);
1830 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1835 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1839 /* Get the total length of the skb including any skb's in the
1848 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1850 event
= sctp_skb2event(skb
);
1855 sock_recv_timestamp(msg
, sk
, skb
);
1856 if (sctp_ulpevent_is_notification(event
)) {
1857 msg
->msg_flags
|= MSG_NOTIFICATION
;
1858 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1860 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1863 /* Check if we allow SCTP_SNDRCVINFO. */
1864 if (sp
->subscribe
.sctp_data_io_event
)
1865 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1867 /* FIXME: we should be calling IP/IPv6 layers. */
1868 if (sk
->sk_protinfo
.af_inet
.cmsg_flags
)
1869 ip_cmsg_recv(msg
, skb
);
1874 /* If skb's length exceeds the user's buffer, update the skb and
1875 * push it back to the receive_queue so that the next call to
1876 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1878 if (skb_len
> copied
) {
1879 msg
->msg_flags
&= ~MSG_EOR
;
1880 if (flags
& MSG_PEEK
)
1882 sctp_skb_pull(skb
, copied
);
1883 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1885 /* When only partial message is copied to the user, increase
1886 * rwnd by that amount. If all the data in the skb is read,
1887 * rwnd is updated when the event is freed.
1889 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1891 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1892 (event
->msg_flags
& MSG_EOR
))
1893 msg
->msg_flags
|= MSG_EOR
;
1895 msg
->msg_flags
&= ~MSG_EOR
;
1898 if (flags
& MSG_PEEK
) {
1899 /* Release the skb reference acquired after peeking the skb in
1900 * sctp_skb_recv_datagram().
1904 /* Free the event which includes releasing the reference to
1905 * the owner of the skb, freeing the skb and updating the
1908 sctp_ulpevent_free(event
);
1911 sctp_release_sock(sk
);
1915 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1917 * This option is a on/off flag. If enabled no SCTP message
1918 * fragmentation will be performed. Instead if a message being sent
1919 * exceeds the current PMTU size, the message will NOT be sent and
1920 * instead a error will be indicated to the user.
1922 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1923 char __user
*optval
, int optlen
)
1927 if (optlen
< sizeof(int))
1930 if (get_user(val
, (int __user
*)optval
))
1933 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1938 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1941 if (optlen
!= sizeof(struct sctp_event_subscribe
))
1943 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
1948 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1950 * This socket option is applicable to the UDP-style socket only. When
1951 * set it will cause associations that are idle for more than the
1952 * specified number of seconds to automatically close. An association
1953 * being idle is defined an association that has NOT sent or received
1954 * user data. The special value of '0' indicates that no automatic
1955 * close of any associations should be performed. The option expects an
1956 * integer defining the number of seconds of idle time before an
1957 * association is closed.
1959 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
1962 struct sctp_sock
*sp
= sctp_sk(sk
);
1964 /* Applicable to UDP-style socket only */
1965 if (sctp_style(sk
, TCP
))
1967 if (optlen
!= sizeof(int))
1969 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
1975 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1977 * Applications can enable or disable heartbeats for any peer address of
1978 * an association, modify an address's heartbeat interval, force a
1979 * heartbeat to be sent immediately, and adjust the address's maximum
1980 * number of retransmissions sent before an address is considered
1981 * unreachable. The following structure is used to access and modify an
1982 * address's parameters:
1984 * struct sctp_paddrparams {
1985 * sctp_assoc_t spp_assoc_id;
1986 * struct sockaddr_storage spp_address;
1987 * uint32_t spp_hbinterval;
1988 * uint16_t spp_pathmaxrxt;
1989 * uint32_t spp_pathmtu;
1990 * uint32_t spp_sackdelay;
1991 * uint32_t spp_flags;
1994 * spp_assoc_id - (one-to-many style socket) This is filled in the
1995 * application, and identifies the association for
1997 * spp_address - This specifies which address is of interest.
1998 * spp_hbinterval - This contains the value of the heartbeat interval,
1999 * in milliseconds. If a value of zero
2000 * is present in this field then no changes are to
2001 * be made to this parameter.
2002 * spp_pathmaxrxt - This contains the maximum number of
2003 * retransmissions before this address shall be
2004 * considered unreachable. If a value of zero
2005 * is present in this field then no changes are to
2006 * be made to this parameter.
2007 * spp_pathmtu - When Path MTU discovery is disabled the value
2008 * specified here will be the "fixed" path mtu.
2009 * Note that if the spp_address field is empty
2010 * then all associations on this address will
2011 * have this fixed path mtu set upon them.
2013 * spp_sackdelay - When delayed sack is enabled, this value specifies
2014 * the number of milliseconds that sacks will be delayed
2015 * for. This value will apply to all addresses of an
2016 * association if the spp_address field is empty. Note
2017 * also, that if delayed sack is enabled and this
2018 * value is set to 0, no change is made to the last
2019 * recorded delayed sack timer value.
2021 * spp_flags - These flags are used to control various features
2022 * on an association. The flag field may contain
2023 * zero or more of the following options.
2025 * SPP_HB_ENABLE - Enable heartbeats on the
2026 * specified address. Note that if the address
2027 * field is empty all addresses for the association
2028 * have heartbeats enabled upon them.
2030 * SPP_HB_DISABLE - Disable heartbeats on the
2031 * speicifed address. Note that if the address
2032 * field is empty all addresses for the association
2033 * will have their heartbeats disabled. Note also
2034 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2035 * mutually exclusive, only one of these two should
2036 * be specified. Enabling both fields will have
2037 * undetermined results.
2039 * SPP_HB_DEMAND - Request a user initiated heartbeat
2040 * to be made immediately.
2042 * SPP_PMTUD_ENABLE - This field will enable PMTU
2043 * discovery upon the specified address. Note that
2044 * if the address feild is empty then all addresses
2045 * on the association are effected.
2047 * SPP_PMTUD_DISABLE - This field will disable PMTU
2048 * discovery upon the specified address. Note that
2049 * if the address feild is empty then all addresses
2050 * on the association are effected. Not also that
2051 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2052 * exclusive. Enabling both will have undetermined
2055 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2056 * on delayed sack. The time specified in spp_sackdelay
2057 * is used to specify the sack delay for this address. Note
2058 * that if spp_address is empty then all addresses will
2059 * enable delayed sack and take on the sack delay
2060 * value specified in spp_sackdelay.
2061 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2062 * off delayed sack. If the spp_address field is blank then
2063 * delayed sack is disabled for the entire association. Note
2064 * also that this field is mutually exclusive to
2065 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2068 static int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2069 struct sctp_transport
*trans
,
2070 struct sctp_association
*asoc
,
2071 struct sctp_sock
*sp
,
2074 int sackdelay_change
)
2078 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2079 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2084 if (params
->spp_hbinterval
) {
2086 trans
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2088 asoc
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2090 sp
->hbinterval
= params
->spp_hbinterval
;
2096 trans
->param_flags
=
2097 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2100 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2103 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2107 if (params
->spp_pathmtu
) {
2109 trans
->pathmtu
= params
->spp_pathmtu
;
2110 sctp_assoc_sync_pmtu(asoc
);
2112 asoc
->pathmtu
= params
->spp_pathmtu
;
2113 sctp_frag_point(sp
, params
->spp_pathmtu
);
2115 sp
->pathmtu
= params
->spp_pathmtu
;
2121 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2122 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2123 trans
->param_flags
=
2124 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2126 sctp_transport_pmtu(trans
);
2127 sctp_assoc_sync_pmtu(asoc
);
2131 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2134 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2138 if (params
->spp_sackdelay
) {
2141 msecs_to_jiffies(params
->spp_sackdelay
);
2144 msecs_to_jiffies(params
->spp_sackdelay
);
2146 sp
->sackdelay
= params
->spp_sackdelay
;
2150 if (sackdelay_change
) {
2152 trans
->param_flags
=
2153 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2157 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2161 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2166 if (params
->spp_pathmaxrxt
) {
2168 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2170 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2172 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2179 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2180 char __user
*optval
, int optlen
)
2182 struct sctp_paddrparams params
;
2183 struct sctp_transport
*trans
= NULL
;
2184 struct sctp_association
*asoc
= NULL
;
2185 struct sctp_sock
*sp
= sctp_sk(sk
);
2187 int hb_change
, pmtud_change
, sackdelay_change
;
2189 if (optlen
!= sizeof(struct sctp_paddrparams
))
2192 if (copy_from_user(¶ms
, optval
, optlen
))
2195 /* Validate flags and value parameters. */
2196 hb_change
= params
.spp_flags
& SPP_HB
;
2197 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2198 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2200 if (hb_change
== SPP_HB
||
2201 pmtud_change
== SPP_PMTUD
||
2202 sackdelay_change
== SPP_SACKDELAY
||
2203 params
.spp_sackdelay
> 500 ||
2205 && params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2208 /* If an address other than INADDR_ANY is specified, and
2209 * no transport is found, then the request is invalid.
2211 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
2212 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2213 params
.spp_assoc_id
);
2218 /* Get association, if assoc_id != 0 and the socket is a one
2219 * to many style socket, and an association was not found, then
2220 * the id was invalid.
2222 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2223 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2226 /* Heartbeat demand can only be sent on a transport or
2227 * association, but not a socket.
2229 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2232 /* Process parameters. */
2233 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2234 hb_change
, pmtud_change
,
2240 /* If changes are for association, also apply parameters to each
2243 if (!trans
&& asoc
) {
2244 struct list_head
*pos
;
2246 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2247 trans
= list_entry(pos
, struct sctp_transport
,
2249 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2250 hb_change
, pmtud_change
,
2258 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2260 * This options will get or set the delayed ack timer. The time is set
2261 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2262 * endpoints default delayed ack timer value. If the assoc_id field is
2263 * non-zero, then the set or get effects the specified association.
2265 * struct sctp_assoc_value {
2266 * sctp_assoc_t assoc_id;
2267 * uint32_t assoc_value;
2270 * assoc_id - This parameter, indicates which association the
2271 * user is preforming an action upon. Note that if
2272 * this field's value is zero then the endpoints
2273 * default value is changed (effecting future
2274 * associations only).
2276 * assoc_value - This parameter contains the number of milliseconds
2277 * that the user is requesting the delayed ACK timer
2278 * be set to. Note that this value is defined in
2279 * the standard to be between 200 and 500 milliseconds.
2281 * Note: a value of zero will leave the value alone,
2282 * but disable SACK delay. A non-zero value will also
2283 * enable SACK delay.
2286 static int sctp_setsockopt_delayed_ack_time(struct sock
*sk
,
2287 char __user
*optval
, int optlen
)
2289 struct sctp_assoc_value params
;
2290 struct sctp_transport
*trans
= NULL
;
2291 struct sctp_association
*asoc
= NULL
;
2292 struct sctp_sock
*sp
= sctp_sk(sk
);
2294 if (optlen
!= sizeof(struct sctp_assoc_value
))
2297 if (copy_from_user(¶ms
, optval
, optlen
))
2300 /* Validate value parameter. */
2301 if (params
.assoc_value
> 500)
2304 /* Get association, if assoc_id != 0 and the socket is a one
2305 * to many style socket, and an association was not found, then
2306 * the id was invalid.
2308 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2309 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2312 if (params
.assoc_value
) {
2315 msecs_to_jiffies(params
.assoc_value
);
2317 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2318 SPP_SACKDELAY_ENABLE
;
2320 sp
->sackdelay
= params
.assoc_value
;
2322 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2323 SPP_SACKDELAY_ENABLE
;
2328 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2329 SPP_SACKDELAY_DISABLE
;
2332 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2333 SPP_SACKDELAY_DISABLE
;
2337 /* If change is for association, also apply to each transport. */
2339 struct list_head
*pos
;
2341 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2342 trans
= list_entry(pos
, struct sctp_transport
,
2344 if (params
.assoc_value
) {
2346 msecs_to_jiffies(params
.assoc_value
);
2347 trans
->param_flags
=
2348 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2349 SPP_SACKDELAY_ENABLE
;
2351 trans
->param_flags
=
2352 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2353 SPP_SACKDELAY_DISABLE
;
2361 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2363 * Applications can specify protocol parameters for the default association
2364 * initialization. The option name argument to setsockopt() and getsockopt()
2367 * Setting initialization parameters is effective only on an unconnected
2368 * socket (for UDP-style sockets only future associations are effected
2369 * by the change). With TCP-style sockets, this option is inherited by
2370 * sockets derived from a listener socket.
2372 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, int optlen
)
2374 struct sctp_initmsg sinit
;
2375 struct sctp_sock
*sp
= sctp_sk(sk
);
2377 if (optlen
!= sizeof(struct sctp_initmsg
))
2379 if (copy_from_user(&sinit
, optval
, optlen
))
2382 if (sinit
.sinit_num_ostreams
)
2383 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2384 if (sinit
.sinit_max_instreams
)
2385 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2386 if (sinit
.sinit_max_attempts
)
2387 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2388 if (sinit
.sinit_max_init_timeo
)
2389 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2395 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2397 * Applications that wish to use the sendto() system call may wish to
2398 * specify a default set of parameters that would normally be supplied
2399 * through the inclusion of ancillary data. This socket option allows
2400 * such an application to set the default sctp_sndrcvinfo structure.
2401 * The application that wishes to use this socket option simply passes
2402 * in to this call the sctp_sndrcvinfo structure defined in Section
2403 * 5.2.2) The input parameters accepted by this call include
2404 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2405 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2406 * to this call if the caller is using the UDP model.
2408 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2409 char __user
*optval
, int optlen
)
2411 struct sctp_sndrcvinfo info
;
2412 struct sctp_association
*asoc
;
2413 struct sctp_sock
*sp
= sctp_sk(sk
);
2415 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2417 if (copy_from_user(&info
, optval
, optlen
))
2420 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2421 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2425 asoc
->default_stream
= info
.sinfo_stream
;
2426 asoc
->default_flags
= info
.sinfo_flags
;
2427 asoc
->default_ppid
= info
.sinfo_ppid
;
2428 asoc
->default_context
= info
.sinfo_context
;
2429 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2431 sp
->default_stream
= info
.sinfo_stream
;
2432 sp
->default_flags
= info
.sinfo_flags
;
2433 sp
->default_ppid
= info
.sinfo_ppid
;
2434 sp
->default_context
= info
.sinfo_context
;
2435 sp
->default_timetolive
= info
.sinfo_timetolive
;
2441 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2443 * Requests that the local SCTP stack use the enclosed peer address as
2444 * the association primary. The enclosed address must be one of the
2445 * association peer's addresses.
2447 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2450 struct sctp_prim prim
;
2451 struct sctp_transport
*trans
;
2453 if (optlen
!= sizeof(struct sctp_prim
))
2456 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2459 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2463 sctp_assoc_set_primary(trans
->asoc
, trans
);
2469 * 7.1.5 SCTP_NODELAY
2471 * Turn on/off any Nagle-like algorithm. This means that packets are
2472 * generally sent as soon as possible and no unnecessary delays are
2473 * introduced, at the cost of more packets in the network. Expects an
2474 * integer boolean flag.
2476 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2481 if (optlen
< sizeof(int))
2483 if (get_user(val
, (int __user
*)optval
))
2486 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2492 * 7.1.1 SCTP_RTOINFO
2494 * The protocol parameters used to initialize and bound retransmission
2495 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2496 * and modify these parameters.
2497 * All parameters are time values, in milliseconds. A value of 0, when
2498 * modifying the parameters, indicates that the current value should not
2502 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, int optlen
) {
2503 struct sctp_rtoinfo rtoinfo
;
2504 struct sctp_association
*asoc
;
2506 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2509 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2512 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2514 /* Set the values to the specific association */
2515 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2519 if (rtoinfo
.srto_initial
!= 0)
2521 msecs_to_jiffies(rtoinfo
.srto_initial
);
2522 if (rtoinfo
.srto_max
!= 0)
2523 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2524 if (rtoinfo
.srto_min
!= 0)
2525 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2527 /* If there is no association or the association-id = 0
2528 * set the values to the endpoint.
2530 struct sctp_sock
*sp
= sctp_sk(sk
);
2532 if (rtoinfo
.srto_initial
!= 0)
2533 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2534 if (rtoinfo
.srto_max
!= 0)
2535 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2536 if (rtoinfo
.srto_min
!= 0)
2537 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2545 * 7.1.2 SCTP_ASSOCINFO
2547 * This option is used to tune the the maximum retransmission attempts
2548 * of the association.
2549 * Returns an error if the new association retransmission value is
2550 * greater than the sum of the retransmission value of the peer.
2551 * See [SCTP] for more information.
2554 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, int optlen
)
2557 struct sctp_assocparams assocparams
;
2558 struct sctp_association
*asoc
;
2560 if (optlen
!= sizeof(struct sctp_assocparams
))
2562 if (copy_from_user(&assocparams
, optval
, optlen
))
2565 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2567 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2570 /* Set the values to the specific association */
2572 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2575 struct list_head
*pos
;
2576 struct sctp_transport
*peer_addr
;
2578 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2579 peer_addr
= list_entry(pos
,
2580 struct sctp_transport
,
2582 path_sum
+= peer_addr
->pathmaxrxt
;
2586 /* Only validate asocmaxrxt if we have more then
2587 * one path/transport. We do this because path
2588 * retransmissions are only counted when we have more
2592 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2595 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2598 if (assocparams
.sasoc_cookie_life
!= 0) {
2599 asoc
->cookie_life
.tv_sec
=
2600 assocparams
.sasoc_cookie_life
/ 1000;
2601 asoc
->cookie_life
.tv_usec
=
2602 (assocparams
.sasoc_cookie_life
% 1000)
2606 /* Set the values to the endpoint */
2607 struct sctp_sock
*sp
= sctp_sk(sk
);
2609 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2610 sp
->assocparams
.sasoc_asocmaxrxt
=
2611 assocparams
.sasoc_asocmaxrxt
;
2612 if (assocparams
.sasoc_cookie_life
!= 0)
2613 sp
->assocparams
.sasoc_cookie_life
=
2614 assocparams
.sasoc_cookie_life
;
2620 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2622 * This socket option is a boolean flag which turns on or off mapped V4
2623 * addresses. If this option is turned on and the socket is type
2624 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2625 * If this option is turned off, then no mapping will be done of V4
2626 * addresses and a user will receive both PF_INET6 and PF_INET type
2627 * addresses on the socket.
2629 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, int optlen
)
2632 struct sctp_sock
*sp
= sctp_sk(sk
);
2634 if (optlen
< sizeof(int))
2636 if (get_user(val
, (int __user
*)optval
))
2647 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2649 * This socket option specifies the maximum size to put in any outgoing
2650 * SCTP chunk. If a message is larger than this size it will be
2651 * fragmented by SCTP into the specified size. Note that the underlying
2652 * SCTP implementation may fragment into smaller sized chunks when the
2653 * PMTU of the underlying association is smaller than the value set by
2656 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, int optlen
)
2658 struct sctp_association
*asoc
;
2659 struct list_head
*pos
;
2660 struct sctp_sock
*sp
= sctp_sk(sk
);
2663 if (optlen
< sizeof(int))
2665 if (get_user(val
, (int __user
*)optval
))
2667 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2669 sp
->user_frag
= val
;
2671 /* Update the frag_point of the existing associations. */
2672 list_for_each(pos
, &(sp
->ep
->asocs
)) {
2673 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
2674 asoc
->frag_point
= sctp_frag_point(sp
, asoc
->pathmtu
);
2682 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2684 * Requests that the peer mark the enclosed address as the association
2685 * primary. The enclosed address must be one of the association's
2686 * locally bound addresses. The following structure is used to make a
2687 * set primary request:
2689 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2692 struct sctp_sock
*sp
;
2693 struct sctp_endpoint
*ep
;
2694 struct sctp_association
*asoc
= NULL
;
2695 struct sctp_setpeerprim prim
;
2696 struct sctp_chunk
*chunk
;
2702 if (!sctp_addip_enable
)
2705 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2708 if (copy_from_user(&prim
, optval
, optlen
))
2711 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2715 if (!asoc
->peer
.asconf_capable
)
2718 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2721 if (!sctp_state(asoc
, ESTABLISHED
))
2724 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2725 return -EADDRNOTAVAIL
;
2727 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2728 chunk
= sctp_make_asconf_set_prim(asoc
,
2729 (union sctp_addr
*)&prim
.sspp_addr
);
2733 err
= sctp_send_asconf(asoc
, chunk
);
2735 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2740 static int sctp_setsockopt_adaptation_layer(struct sock
*sk
, char __user
*optval
,
2743 struct sctp_setadaptation adaptation
;
2745 if (optlen
!= sizeof(struct sctp_setadaptation
))
2747 if (copy_from_user(&adaptation
, optval
, optlen
))
2750 sctp_sk(sk
)->adaptation_ind
= adaptation
.ssb_adaptation_ind
;
2756 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2758 * The context field in the sctp_sndrcvinfo structure is normally only
2759 * used when a failed message is retrieved holding the value that was
2760 * sent down on the actual send call. This option allows the setting of
2761 * a default context on an association basis that will be received on
2762 * reading messages from the peer. This is especially helpful in the
2763 * one-2-many model for an application to keep some reference to an
2764 * internal state machine that is processing messages on the
2765 * association. Note that the setting of this value only effects
2766 * received messages from the peer and does not effect the value that is
2767 * saved with outbound messages.
2769 static int sctp_setsockopt_context(struct sock
*sk
, char __user
*optval
,
2772 struct sctp_assoc_value params
;
2773 struct sctp_sock
*sp
;
2774 struct sctp_association
*asoc
;
2776 if (optlen
!= sizeof(struct sctp_assoc_value
))
2778 if (copy_from_user(¶ms
, optval
, optlen
))
2783 if (params
.assoc_id
!= 0) {
2784 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2787 asoc
->default_rcv_context
= params
.assoc_value
;
2789 sp
->default_rcv_context
= params
.assoc_value
;
2795 /* API 6.2 setsockopt(), getsockopt()
2797 * Applications use setsockopt() and getsockopt() to set or retrieve
2798 * socket options. Socket options are used to change the default
2799 * behavior of sockets calls. They are described in Section 7.
2803 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2804 * int __user *optlen);
2805 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2808 * sd - the socket descript.
2809 * level - set to IPPROTO_SCTP for all SCTP options.
2810 * optname - the option name.
2811 * optval - the buffer to store the value of the option.
2812 * optlen - the size of the buffer.
2814 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
2815 char __user
*optval
, int optlen
)
2819 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2822 /* I can hardly begin to describe how wrong this is. This is
2823 * so broken as to be worse than useless. The API draft
2824 * REALLY is NOT helpful here... I am not convinced that the
2825 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2826 * are at all well-founded.
2828 if (level
!= SOL_SCTP
) {
2829 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
2830 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
2837 case SCTP_SOCKOPT_BINDX_ADD
:
2838 /* 'optlen' is the size of the addresses buffer. */
2839 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2840 optlen
, SCTP_BINDX_ADD_ADDR
);
2843 case SCTP_SOCKOPT_BINDX_REM
:
2844 /* 'optlen' is the size of the addresses buffer. */
2845 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2846 optlen
, SCTP_BINDX_REM_ADDR
);
2849 case SCTP_SOCKOPT_CONNECTX
:
2850 /* 'optlen' is the size of the addresses buffer. */
2851 retval
= sctp_setsockopt_connectx(sk
, (struct sockaddr __user
*)optval
,
2855 case SCTP_DISABLE_FRAGMENTS
:
2856 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
2860 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
2863 case SCTP_AUTOCLOSE
:
2864 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
2867 case SCTP_PEER_ADDR_PARAMS
:
2868 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
2871 case SCTP_DELAYED_ACK_TIME
:
2872 retval
= sctp_setsockopt_delayed_ack_time(sk
, optval
, optlen
);
2876 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
2878 case SCTP_DEFAULT_SEND_PARAM
:
2879 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
2882 case SCTP_PRIMARY_ADDR
:
2883 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
2885 case SCTP_SET_PEER_PRIMARY_ADDR
:
2886 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
2889 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
2892 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
2894 case SCTP_ASSOCINFO
:
2895 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
2897 case SCTP_I_WANT_MAPPED_V4_ADDR
:
2898 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
2901 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
2903 case SCTP_ADAPTATION_LAYER
:
2904 retval
= sctp_setsockopt_adaptation_layer(sk
, optval
, optlen
);
2907 retval
= sctp_setsockopt_context(sk
, optval
, optlen
);
2911 retval
= -ENOPROTOOPT
;
2915 sctp_release_sock(sk
);
2921 /* API 3.1.6 connect() - UDP Style Syntax
2923 * An application may use the connect() call in the UDP model to initiate an
2924 * association without sending data.
2928 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2930 * sd: the socket descriptor to have a new association added to.
2932 * nam: the address structure (either struct sockaddr_in or struct
2933 * sockaddr_in6 defined in RFC2553 [7]).
2935 * len: the size of the address.
2937 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
2945 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2946 __FUNCTION__
, sk
, addr
, addr_len
);
2948 /* Validate addr_len before calling common connect/connectx routine. */
2949 af
= sctp_get_af_specific(addr
->sa_family
);
2950 if (!af
|| addr_len
< af
->sockaddr_len
) {
2953 /* Pass correct addr len to common routine (so it knows there
2954 * is only one address being passed.
2956 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
);
2959 sctp_release_sock(sk
);
2963 /* FIXME: Write comments. */
2964 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
2966 return -EOPNOTSUPP
; /* STUB */
2969 /* 4.1.4 accept() - TCP Style Syntax
2971 * Applications use accept() call to remove an established SCTP
2972 * association from the accept queue of the endpoint. A new socket
2973 * descriptor will be returned from accept() to represent the newly
2974 * formed association.
2976 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
2978 struct sctp_sock
*sp
;
2979 struct sctp_endpoint
*ep
;
2980 struct sock
*newsk
= NULL
;
2981 struct sctp_association
*asoc
;
2990 if (!sctp_style(sk
, TCP
)) {
2991 error
= -EOPNOTSUPP
;
2995 if (!sctp_sstate(sk
, LISTENING
)) {
3000 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
3002 error
= sctp_wait_for_accept(sk
, timeo
);
3006 /* We treat the list of associations on the endpoint as the accept
3007 * queue and pick the first association on the list.
3009 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
3011 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
3017 /* Populate the fields of the newsk from the oldsk and migrate the
3018 * asoc to the newsk.
3020 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
3023 sctp_release_sock(sk
);
3028 /* The SCTP ioctl handler. */
3029 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
3031 return -ENOIOCTLCMD
;
3034 /* This is the function which gets called during socket creation to
3035 * initialized the SCTP-specific portion of the sock.
3036 * The sock structure should already be zero-filled memory.
3038 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
3040 struct sctp_endpoint
*ep
;
3041 struct sctp_sock
*sp
;
3043 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
3047 /* Initialize the SCTP per socket area. */
3048 switch (sk
->sk_type
) {
3049 case SOCK_SEQPACKET
:
3050 sp
->type
= SCTP_SOCKET_UDP
;
3053 sp
->type
= SCTP_SOCKET_TCP
;
3056 return -ESOCKTNOSUPPORT
;
3059 /* Initialize default send parameters. These parameters can be
3060 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3062 sp
->default_stream
= 0;
3063 sp
->default_ppid
= 0;
3064 sp
->default_flags
= 0;
3065 sp
->default_context
= 0;
3066 sp
->default_timetolive
= 0;
3068 sp
->default_rcv_context
= 0;
3070 /* Initialize default setup parameters. These parameters
3071 * can be modified with the SCTP_INITMSG socket option or
3072 * overridden by the SCTP_INIT CMSG.
3074 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
3075 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
3076 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
3077 sp
->initmsg
.sinit_max_init_timeo
= sctp_rto_max
;
3079 /* Initialize default RTO related parameters. These parameters can
3080 * be modified for with the SCTP_RTOINFO socket option.
3082 sp
->rtoinfo
.srto_initial
= sctp_rto_initial
;
3083 sp
->rtoinfo
.srto_max
= sctp_rto_max
;
3084 sp
->rtoinfo
.srto_min
= sctp_rto_min
;
3086 /* Initialize default association related parameters. These parameters
3087 * can be modified with the SCTP_ASSOCINFO socket option.
3089 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3090 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3091 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3092 sp
->assocparams
.sasoc_local_rwnd
= 0;
3093 sp
->assocparams
.sasoc_cookie_life
= sctp_valid_cookie_life
;
3095 /* Initialize default event subscriptions. By default, all the
3098 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3100 /* Default Peer Address Parameters. These defaults can
3101 * be modified via SCTP_PEER_ADDR_PARAMS
3103 sp
->hbinterval
= sctp_hb_interval
;
3104 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3105 sp
->pathmtu
= 0; // allow default discovery
3106 sp
->sackdelay
= sctp_sack_timeout
;
3107 sp
->param_flags
= SPP_HB_ENABLE
|
3109 SPP_SACKDELAY_ENABLE
;
3111 /* If enabled no SCTP message fragmentation will be performed.
3112 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3114 sp
->disable_fragments
= 0;
3116 /* Enable Nagle algorithm by default. */
3119 /* Enable by default. */
3122 /* Auto-close idle associations after the configured
3123 * number of seconds. A value of 0 disables this
3124 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3125 * for UDP-style sockets only.
3129 /* User specified fragmentation limit. */
3132 sp
->adaptation_ind
= 0;
3134 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3136 /* Control variables for partial data delivery. */
3138 skb_queue_head_init(&sp
->pd_lobby
);
3140 /* Create a per socket endpoint structure. Even if we
3141 * change the data structure relationships, this may still
3142 * be useful for storing pre-connect address information.
3144 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3151 SCTP_DBG_OBJCNT_INC(sock
);
3155 /* Cleanup any SCTP per socket resources. */
3156 SCTP_STATIC
int sctp_destroy_sock(struct sock
*sk
)
3158 struct sctp_endpoint
*ep
;
3160 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3162 /* Release our hold on the endpoint. */
3163 ep
= sctp_sk(sk
)->ep
;
3164 sctp_endpoint_free(ep
);
3169 /* API 4.1.7 shutdown() - TCP Style Syntax
3170 * int shutdown(int socket, int how);
3172 * sd - the socket descriptor of the association to be closed.
3173 * how - Specifies the type of shutdown. The values are
3176 * Disables further receive operations. No SCTP
3177 * protocol action is taken.
3179 * Disables further send operations, and initiates
3180 * the SCTP shutdown sequence.
3182 * Disables further send and receive operations
3183 * and initiates the SCTP shutdown sequence.
3185 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3187 struct sctp_endpoint
*ep
;
3188 struct sctp_association
*asoc
;
3190 if (!sctp_style(sk
, TCP
))
3193 if (how
& SEND_SHUTDOWN
) {
3194 ep
= sctp_sk(sk
)->ep
;
3195 if (!list_empty(&ep
->asocs
)) {
3196 asoc
= list_entry(ep
->asocs
.next
,
3197 struct sctp_association
, asocs
);
3198 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3203 /* 7.2.1 Association Status (SCTP_STATUS)
3205 * Applications can retrieve current status information about an
3206 * association, including association state, peer receiver window size,
3207 * number of unacked data chunks, and number of data chunks pending
3208 * receipt. This information is read-only.
3210 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3211 char __user
*optval
,
3214 struct sctp_status status
;
3215 struct sctp_association
*asoc
= NULL
;
3216 struct sctp_transport
*transport
;
3217 sctp_assoc_t associd
;
3220 if (len
!= sizeof(status
)) {
3225 if (copy_from_user(&status
, optval
, sizeof(status
))) {
3230 associd
= status
.sstat_assoc_id
;
3231 asoc
= sctp_id2assoc(sk
, associd
);
3237 transport
= asoc
->peer
.primary_path
;
3239 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3240 status
.sstat_state
= asoc
->state
;
3241 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3242 status
.sstat_unackdata
= asoc
->unack_data
;
3244 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3245 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3246 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3247 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3248 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3249 memcpy(&status
.sstat_primary
.spinfo_address
, &transport
->ipaddr
,
3250 transport
->af_specific
->sockaddr_len
);
3251 /* Map ipv4 address into v4-mapped-on-v6 address. */
3252 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3253 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3254 status
.sstat_primary
.spinfo_state
= transport
->state
;
3255 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3256 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3257 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3258 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3260 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3261 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3263 if (put_user(len
, optlen
)) {
3268 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3269 len
, status
.sstat_state
, status
.sstat_rwnd
,
3270 status
.sstat_assoc_id
);
3272 if (copy_to_user(optval
, &status
, len
)) {
3282 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3284 * Applications can retrieve information about a specific peer address
3285 * of an association, including its reachability state, congestion
3286 * window, and retransmission timer values. This information is
3289 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3290 char __user
*optval
,
3293 struct sctp_paddrinfo pinfo
;
3294 struct sctp_transport
*transport
;
3297 if (len
!= sizeof(pinfo
)) {
3302 if (copy_from_user(&pinfo
, optval
, sizeof(pinfo
))) {
3307 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3308 pinfo
.spinfo_assoc_id
);
3312 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3313 pinfo
.spinfo_state
= transport
->state
;
3314 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3315 pinfo
.spinfo_srtt
= transport
->srtt
;
3316 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3317 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3319 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3320 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3322 if (put_user(len
, optlen
)) {
3327 if (copy_to_user(optval
, &pinfo
, len
)) {
3336 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3338 * This option is a on/off flag. If enabled no SCTP message
3339 * fragmentation will be performed. Instead if a message being sent
3340 * exceeds the current PMTU size, the message will NOT be sent and
3341 * instead a error will be indicated to the user.
3343 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3344 char __user
*optval
, int __user
*optlen
)
3348 if (len
< sizeof(int))
3352 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3353 if (put_user(len
, optlen
))
3355 if (copy_to_user(optval
, &val
, len
))
3360 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3362 * This socket option is used to specify various notifications and
3363 * ancillary data the user wishes to receive.
3365 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3368 if (len
!= sizeof(struct sctp_event_subscribe
))
3370 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3375 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3377 * This socket option is applicable to the UDP-style socket only. When
3378 * set it will cause associations that are idle for more than the
3379 * specified number of seconds to automatically close. An association
3380 * being idle is defined an association that has NOT sent or received
3381 * user data. The special value of '0' indicates that no automatic
3382 * close of any associations should be performed. The option expects an
3383 * integer defining the number of seconds of idle time before an
3384 * association is closed.
3386 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3388 /* Applicable to UDP-style socket only */
3389 if (sctp_style(sk
, TCP
))
3391 if (len
!= sizeof(int))
3393 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, len
))
3398 /* Helper routine to branch off an association to a new socket. */
3399 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3400 struct socket
**sockp
)
3402 struct sock
*sk
= asoc
->base
.sk
;
3403 struct socket
*sock
;
3404 struct inet_sock
*inetsk
;
3407 /* An association cannot be branched off from an already peeled-off
3408 * socket, nor is this supported for tcp style sockets.
3410 if (!sctp_style(sk
, UDP
))
3413 /* Create a new socket. */
3414 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3418 /* Populate the fields of the newsk from the oldsk and migrate the
3419 * asoc to the newsk.
3421 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3423 /* Make peeled-off sockets more like 1-1 accepted sockets.
3424 * Set the daddr and initialize id to something more random
3426 inetsk
= inet_sk(sock
->sk
);
3427 inetsk
->daddr
= asoc
->peer
.primary_addr
.v4
.sin_addr
.s_addr
;
3428 inetsk
->id
= asoc
->next_tsn
^ jiffies
;
3435 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3437 sctp_peeloff_arg_t peeloff
;
3438 struct socket
*newsock
;
3440 struct sctp_association
*asoc
;
3442 if (len
!= sizeof(sctp_peeloff_arg_t
))
3444 if (copy_from_user(&peeloff
, optval
, len
))
3447 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3453 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__
, sk
, asoc
);
3455 retval
= sctp_do_peeloff(asoc
, &newsock
);
3459 /* Map the socket to an unused fd that can be returned to the user. */
3460 retval
= sock_map_fd(newsock
);
3462 sock_release(newsock
);
3466 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3467 __FUNCTION__
, sk
, asoc
, newsock
->sk
, retval
);
3469 /* Return the fd mapped to the new socket. */
3470 peeloff
.sd
= retval
;
3471 if (copy_to_user(optval
, &peeloff
, len
))
3478 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3480 * Applications can enable or disable heartbeats for any peer address of
3481 * an association, modify an address's heartbeat interval, force a
3482 * heartbeat to be sent immediately, and adjust the address's maximum
3483 * number of retransmissions sent before an address is considered
3484 * unreachable. The following structure is used to access and modify an
3485 * address's parameters:
3487 * struct sctp_paddrparams {
3488 * sctp_assoc_t spp_assoc_id;
3489 * struct sockaddr_storage spp_address;
3490 * uint32_t spp_hbinterval;
3491 * uint16_t spp_pathmaxrxt;
3492 * uint32_t spp_pathmtu;
3493 * uint32_t spp_sackdelay;
3494 * uint32_t spp_flags;
3497 * spp_assoc_id - (one-to-many style socket) This is filled in the
3498 * application, and identifies the association for
3500 * spp_address - This specifies which address is of interest.
3501 * spp_hbinterval - This contains the value of the heartbeat interval,
3502 * in milliseconds. If a value of zero
3503 * is present in this field then no changes are to
3504 * be made to this parameter.
3505 * spp_pathmaxrxt - This contains the maximum number of
3506 * retransmissions before this address shall be
3507 * considered unreachable. If a value of zero
3508 * is present in this field then no changes are to
3509 * be made to this parameter.
3510 * spp_pathmtu - When Path MTU discovery is disabled the value
3511 * specified here will be the "fixed" path mtu.
3512 * Note that if the spp_address field is empty
3513 * then all associations on this address will
3514 * have this fixed path mtu set upon them.
3516 * spp_sackdelay - When delayed sack is enabled, this value specifies
3517 * the number of milliseconds that sacks will be delayed
3518 * for. This value will apply to all addresses of an
3519 * association if the spp_address field is empty. Note
3520 * also, that if delayed sack is enabled and this
3521 * value is set to 0, no change is made to the last
3522 * recorded delayed sack timer value.
3524 * spp_flags - These flags are used to control various features
3525 * on an association. The flag field may contain
3526 * zero or more of the following options.
3528 * SPP_HB_ENABLE - Enable heartbeats on the
3529 * specified address. Note that if the address
3530 * field is empty all addresses for the association
3531 * have heartbeats enabled upon them.
3533 * SPP_HB_DISABLE - Disable heartbeats on the
3534 * speicifed address. Note that if the address
3535 * field is empty all addresses for the association
3536 * will have their heartbeats disabled. Note also
3537 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3538 * mutually exclusive, only one of these two should
3539 * be specified. Enabling both fields will have
3540 * undetermined results.
3542 * SPP_HB_DEMAND - Request a user initiated heartbeat
3543 * to be made immediately.
3545 * SPP_PMTUD_ENABLE - This field will enable PMTU
3546 * discovery upon the specified address. Note that
3547 * if the address feild is empty then all addresses
3548 * on the association are effected.
3550 * SPP_PMTUD_DISABLE - This field will disable PMTU
3551 * discovery upon the specified address. Note that
3552 * if the address feild is empty then all addresses
3553 * on the association are effected. Not also that
3554 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3555 * exclusive. Enabling both will have undetermined
3558 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3559 * on delayed sack. The time specified in spp_sackdelay
3560 * is used to specify the sack delay for this address. Note
3561 * that if spp_address is empty then all addresses will
3562 * enable delayed sack and take on the sack delay
3563 * value specified in spp_sackdelay.
3564 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3565 * off delayed sack. If the spp_address field is blank then
3566 * delayed sack is disabled for the entire association. Note
3567 * also that this field is mutually exclusive to
3568 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3571 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
3572 char __user
*optval
, int __user
*optlen
)
3574 struct sctp_paddrparams params
;
3575 struct sctp_transport
*trans
= NULL
;
3576 struct sctp_association
*asoc
= NULL
;
3577 struct sctp_sock
*sp
= sctp_sk(sk
);
3579 if (len
!= sizeof(struct sctp_paddrparams
))
3582 if (copy_from_user(¶ms
, optval
, len
))
3585 /* If an address other than INADDR_ANY is specified, and
3586 * no transport is found, then the request is invalid.
3588 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
3589 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
3590 params
.spp_assoc_id
);
3592 SCTP_DEBUG_PRINTK("Failed no transport\n");
3597 /* Get association, if assoc_id != 0 and the socket is a one
3598 * to many style socket, and an association was not found, then
3599 * the id was invalid.
3601 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
3602 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
3603 SCTP_DEBUG_PRINTK("Failed no association\n");
3608 /* Fetch transport values. */
3609 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
3610 params
.spp_pathmtu
= trans
->pathmtu
;
3611 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
3612 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
3614 /*draft-11 doesn't say what to return in spp_flags*/
3615 params
.spp_flags
= trans
->param_flags
;
3617 /* Fetch association values. */
3618 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
3619 params
.spp_pathmtu
= asoc
->pathmtu
;
3620 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
3621 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
3623 /*draft-11 doesn't say what to return in spp_flags*/
3624 params
.spp_flags
= asoc
->param_flags
;
3626 /* Fetch socket values. */
3627 params
.spp_hbinterval
= sp
->hbinterval
;
3628 params
.spp_pathmtu
= sp
->pathmtu
;
3629 params
.spp_sackdelay
= sp
->sackdelay
;
3630 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
3632 /*draft-11 doesn't say what to return in spp_flags*/
3633 params
.spp_flags
= sp
->param_flags
;
3636 if (copy_to_user(optval
, ¶ms
, len
))
3639 if (put_user(len
, optlen
))
3645 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3647 * This options will get or set the delayed ack timer. The time is set
3648 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3649 * endpoints default delayed ack timer value. If the assoc_id field is
3650 * non-zero, then the set or get effects the specified association.
3652 * struct sctp_assoc_value {
3653 * sctp_assoc_t assoc_id;
3654 * uint32_t assoc_value;
3657 * assoc_id - This parameter, indicates which association the
3658 * user is preforming an action upon. Note that if
3659 * this field's value is zero then the endpoints
3660 * default value is changed (effecting future
3661 * associations only).
3663 * assoc_value - This parameter contains the number of milliseconds
3664 * that the user is requesting the delayed ACK timer
3665 * be set to. Note that this value is defined in
3666 * the standard to be between 200 and 500 milliseconds.
3668 * Note: a value of zero will leave the value alone,
3669 * but disable SACK delay. A non-zero value will also
3670 * enable SACK delay.
3672 static int sctp_getsockopt_delayed_ack_time(struct sock
*sk
, int len
,
3673 char __user
*optval
,
3676 struct sctp_assoc_value params
;
3677 struct sctp_association
*asoc
= NULL
;
3678 struct sctp_sock
*sp
= sctp_sk(sk
);
3680 if (len
!= sizeof(struct sctp_assoc_value
))
3683 if (copy_from_user(¶ms
, optval
, len
))
3686 /* Get association, if assoc_id != 0 and the socket is a one
3687 * to many style socket, and an association was not found, then
3688 * the id was invalid.
3690 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
3691 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
3695 /* Fetch association values. */
3696 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
)
3697 params
.assoc_value
= jiffies_to_msecs(
3700 params
.assoc_value
= 0;
3702 /* Fetch socket values. */
3703 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
)
3704 params
.assoc_value
= sp
->sackdelay
;
3706 params
.assoc_value
= 0;
3709 if (copy_to_user(optval
, ¶ms
, len
))
3712 if (put_user(len
, optlen
))
3718 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3720 * Applications can specify protocol parameters for the default association
3721 * initialization. The option name argument to setsockopt() and getsockopt()
3724 * Setting initialization parameters is effective only on an unconnected
3725 * socket (for UDP-style sockets only future associations are effected
3726 * by the change). With TCP-style sockets, this option is inherited by
3727 * sockets derived from a listener socket.
3729 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3731 if (len
!= sizeof(struct sctp_initmsg
))
3733 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
3738 static int sctp_getsockopt_peer_addrs_num_old(struct sock
*sk
, int len
,
3739 char __user
*optval
,
3743 struct sctp_association
*asoc
;
3744 struct list_head
*pos
;
3747 if (len
!= sizeof(sctp_assoc_t
))
3750 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3753 /* For UDP-style sockets, id specifies the association to query. */
3754 asoc
= sctp_id2assoc(sk
, id
);
3758 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3766 * Old API for getting list of peer addresses. Does not work for 32-bit
3767 * programs running on a 64-bit kernel
3769 static int sctp_getsockopt_peer_addrs_old(struct sock
*sk
, int len
,
3770 char __user
*optval
,
3773 struct sctp_association
*asoc
;
3774 struct list_head
*pos
;
3776 struct sctp_getaddrs_old getaddrs
;
3777 struct sctp_transport
*from
;
3779 union sctp_addr temp
;
3780 struct sctp_sock
*sp
= sctp_sk(sk
);
3783 if (len
!= sizeof(struct sctp_getaddrs_old
))
3786 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
3789 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3791 /* For UDP-style sockets, id specifies the association to query. */
3792 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3796 to
= (void __user
*)getaddrs
.addrs
;
3797 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3798 from
= list_entry(pos
, struct sctp_transport
, transports
);
3799 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3800 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3801 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3802 if (copy_to_user(to
, &temp
, addrlen
))
3806 if (cnt
>= getaddrs
.addr_num
) break;
3808 getaddrs
.addr_num
= cnt
;
3809 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
3815 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
3816 char __user
*optval
, int __user
*optlen
)
3818 struct sctp_association
*asoc
;
3819 struct list_head
*pos
;
3821 struct sctp_getaddrs getaddrs
;
3822 struct sctp_transport
*from
;
3824 union sctp_addr temp
;
3825 struct sctp_sock
*sp
= sctp_sk(sk
);
3830 if (len
< sizeof(struct sctp_getaddrs
))
3833 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
3836 /* For UDP-style sockets, id specifies the association to query. */
3837 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3841 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
3842 space_left
= len
- sizeof(struct sctp_getaddrs
) -
3843 offsetof(struct sctp_getaddrs
,addrs
);
3845 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3846 from
= list_entry(pos
, struct sctp_transport
, transports
);
3847 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3848 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3849 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3850 if (space_left
< addrlen
)
3852 if (copy_to_user(to
, &temp
, addrlen
))
3856 space_left
-= addrlen
;
3859 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
3861 bytes_copied
= ((char __user
*)to
) - optval
;
3862 if (put_user(bytes_copied
, optlen
))
3868 static int sctp_getsockopt_local_addrs_num_old(struct sock
*sk
, int len
,
3869 char __user
*optval
,
3873 struct sctp_bind_addr
*bp
;
3874 struct sctp_association
*asoc
;
3875 struct list_head
*pos
, *temp
;
3876 struct sctp_sockaddr_entry
*addr
;
3877 rwlock_t
*addr_lock
;
3880 if (len
!= sizeof(sctp_assoc_t
))
3883 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3887 * For UDP-style sockets, id specifies the association to query.
3888 * If the id field is set to the value '0' then the locally bound
3889 * addresses are returned without regard to any particular
3893 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
3894 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
3896 asoc
= sctp_id2assoc(sk
, id
);
3899 bp
= &asoc
->base
.bind_addr
;
3900 addr_lock
= &asoc
->base
.addr_lock
;
3903 sctp_read_lock(addr_lock
);
3905 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3906 * addresses from the global local address list.
3908 if (sctp_list_single_entry(&bp
->address_list
)) {
3909 addr
= list_entry(bp
->address_list
.next
,
3910 struct sctp_sockaddr_entry
, list
);
3911 if (sctp_is_any(&addr
->a
)) {
3912 list_for_each_safe(pos
, temp
, &sctp_local_addr_list
) {
3913 addr
= list_entry(pos
,
3914 struct sctp_sockaddr_entry
,
3916 if ((PF_INET
== sk
->sk_family
) &&
3917 (AF_INET6
== addr
->a
.sa
.sa_family
))
3927 list_for_each(pos
, &bp
->address_list
) {
3932 sctp_read_unlock(addr_lock
);
3936 /* Helper function that copies local addresses to user and returns the number
3937 * of addresses copied.
3939 static int sctp_copy_laddrs_old(struct sock
*sk
, __u16 port
,
3940 int max_addrs
, void *to
,
3943 struct list_head
*pos
, *next
;
3944 struct sctp_sockaddr_entry
*addr
;
3945 union sctp_addr temp
;
3949 list_for_each_safe(pos
, next
, &sctp_local_addr_list
) {
3950 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3951 if ((PF_INET
== sk
->sk_family
) &&
3952 (AF_INET6
== addr
->a
.sa
.sa_family
))
3954 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3955 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3957 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3958 memcpy(to
, &temp
, addrlen
);
3961 *bytes_copied
+= addrlen
;
3963 if (cnt
>= max_addrs
) break;
3969 static int sctp_copy_laddrs(struct sock
*sk
, __u16 port
, void *to
,
3970 size_t space_left
, int *bytes_copied
)
3972 struct list_head
*pos
, *next
;
3973 struct sctp_sockaddr_entry
*addr
;
3974 union sctp_addr temp
;
3978 list_for_each_safe(pos
, next
, &sctp_local_addr_list
) {
3979 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3980 if ((PF_INET
== sk
->sk_family
) &&
3981 (AF_INET6
== addr
->a
.sa
.sa_family
))
3983 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3984 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3986 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3987 if (space_left
< addrlen
)
3989 memcpy(to
, &temp
, addrlen
);
3993 space_left
-= addrlen
;
3994 bytes_copied
+= addrlen
;
4000 /* Old API for getting list of local addresses. Does not work for 32-bit
4001 * programs running on a 64-bit kernel
4003 static int sctp_getsockopt_local_addrs_old(struct sock
*sk
, int len
,
4004 char __user
*optval
, int __user
*optlen
)
4006 struct sctp_bind_addr
*bp
;
4007 struct sctp_association
*asoc
;
4008 struct list_head
*pos
;
4010 struct sctp_getaddrs_old getaddrs
;
4011 struct sctp_sockaddr_entry
*addr
;
4013 union sctp_addr temp
;
4014 struct sctp_sock
*sp
= sctp_sk(sk
);
4016 rwlock_t
*addr_lock
;
4020 int bytes_copied
= 0;
4022 if (len
!= sizeof(struct sctp_getaddrs_old
))
4025 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
4028 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
4030 * For UDP-style sockets, id specifies the association to query.
4031 * If the id field is set to the value '0' then the locally bound
4032 * addresses are returned without regard to any particular
4035 if (0 == getaddrs
.assoc_id
) {
4036 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4037 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4039 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4042 bp
= &asoc
->base
.bind_addr
;
4043 addr_lock
= &asoc
->base
.addr_lock
;
4046 to
= getaddrs
.addrs
;
4048 /* Allocate space for a local instance of packed array to hold all
4049 * the data. We store addresses here first and then put write them
4050 * to the user in one shot.
4052 addrs
= kmalloc(sizeof(union sctp_addr
) * getaddrs
.addr_num
,
4057 sctp_read_lock(addr_lock
);
4059 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4060 * addresses from the global local address list.
4062 if (sctp_list_single_entry(&bp
->address_list
)) {
4063 addr
= list_entry(bp
->address_list
.next
,
4064 struct sctp_sockaddr_entry
, list
);
4065 if (sctp_is_any(&addr
->a
)) {
4066 cnt
= sctp_copy_laddrs_old(sk
, bp
->port
,
4068 addrs
, &bytes_copied
);
4074 list_for_each(pos
, &bp
->address_list
) {
4075 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4076 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4077 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4078 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4079 memcpy(buf
, &temp
, addrlen
);
4081 bytes_copied
+= addrlen
;
4083 if (cnt
>= getaddrs
.addr_num
) break;
4087 sctp_read_unlock(addr_lock
);
4089 /* copy the entire address list into the user provided space */
4090 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4095 /* copy the leading structure back to user */
4096 getaddrs
.addr_num
= cnt
;
4097 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
4105 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4106 char __user
*optval
, int __user
*optlen
)
4108 struct sctp_bind_addr
*bp
;
4109 struct sctp_association
*asoc
;
4110 struct list_head
*pos
;
4112 struct sctp_getaddrs getaddrs
;
4113 struct sctp_sockaddr_entry
*addr
;
4115 union sctp_addr temp
;
4116 struct sctp_sock
*sp
= sctp_sk(sk
);
4118 rwlock_t
*addr_lock
;
4121 int bytes_copied
= 0;
4125 if (len
<= sizeof(struct sctp_getaddrs
))
4128 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4132 * For UDP-style sockets, id specifies the association to query.
4133 * If the id field is set to the value '0' then the locally bound
4134 * addresses are returned without regard to any particular
4137 if (0 == getaddrs
.assoc_id
) {
4138 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4139 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4141 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4144 bp
= &asoc
->base
.bind_addr
;
4145 addr_lock
= &asoc
->base
.addr_lock
;
4148 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4149 space_left
= len
- sizeof(struct sctp_getaddrs
) -
4150 offsetof(struct sctp_getaddrs
,addrs
);
4151 addrs
= kmalloc(space_left
, GFP_KERNEL
);
4155 sctp_read_lock(addr_lock
);
4157 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4158 * addresses from the global local address list.
4160 if (sctp_list_single_entry(&bp
->address_list
)) {
4161 addr
= list_entry(bp
->address_list
.next
,
4162 struct sctp_sockaddr_entry
, list
);
4163 if (sctp_is_any(&addr
->a
)) {
4164 cnt
= sctp_copy_laddrs(sk
, bp
->port
, addrs
,
4165 space_left
, &bytes_copied
);
4175 list_for_each(pos
, &bp
->address_list
) {
4176 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4177 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4178 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4179 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4180 if (space_left
< addrlen
) {
4181 err
= -ENOMEM
; /*fixme: right error?*/
4184 memcpy(buf
, &temp
, addrlen
);
4186 bytes_copied
+= addrlen
;
4188 space_left
-= addrlen
;
4192 sctp_read_unlock(addr_lock
);
4194 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4198 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4200 if (put_user(bytes_copied
, optlen
))
4208 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4210 * Requests that the local SCTP stack use the enclosed peer address as
4211 * the association primary. The enclosed address must be one of the
4212 * association peer's addresses.
4214 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4215 char __user
*optval
, int __user
*optlen
)
4217 struct sctp_prim prim
;
4218 struct sctp_association
*asoc
;
4219 struct sctp_sock
*sp
= sctp_sk(sk
);
4221 if (len
!= sizeof(struct sctp_prim
))
4224 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
4227 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4231 if (!asoc
->peer
.primary_path
)
4234 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4235 asoc
->peer
.primary_path
->af_specific
->sockaddr_len
);
4237 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4238 (union sctp_addr
*)&prim
.ssp_addr
);
4240 if (copy_to_user(optval
, &prim
, sizeof(struct sctp_prim
)))
4247 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4249 * Requests that the local endpoint set the specified Adaptation Layer
4250 * Indication parameter for all future INIT and INIT-ACK exchanges.
4252 static int sctp_getsockopt_adaptation_layer(struct sock
*sk
, int len
,
4253 char __user
*optval
, int __user
*optlen
)
4255 struct sctp_setadaptation adaptation
;
4257 if (len
!= sizeof(struct sctp_setadaptation
))
4260 adaptation
.ssb_adaptation_ind
= sctp_sk(sk
)->adaptation_ind
;
4261 if (copy_to_user(optval
, &adaptation
, len
))
4269 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4271 * Applications that wish to use the sendto() system call may wish to
4272 * specify a default set of parameters that would normally be supplied
4273 * through the inclusion of ancillary data. This socket option allows
4274 * such an application to set the default sctp_sndrcvinfo structure.
4277 * The application that wishes to use this socket option simply passes
4278 * in to this call the sctp_sndrcvinfo structure defined in Section
4279 * 5.2.2) The input parameters accepted by this call include
4280 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4281 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4282 * to this call if the caller is using the UDP model.
4284 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4286 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4287 int len
, char __user
*optval
,
4290 struct sctp_sndrcvinfo info
;
4291 struct sctp_association
*asoc
;
4292 struct sctp_sock
*sp
= sctp_sk(sk
);
4294 if (len
!= sizeof(struct sctp_sndrcvinfo
))
4296 if (copy_from_user(&info
, optval
, sizeof(struct sctp_sndrcvinfo
)))
4299 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4300 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4304 info
.sinfo_stream
= asoc
->default_stream
;
4305 info
.sinfo_flags
= asoc
->default_flags
;
4306 info
.sinfo_ppid
= asoc
->default_ppid
;
4307 info
.sinfo_context
= asoc
->default_context
;
4308 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4310 info
.sinfo_stream
= sp
->default_stream
;
4311 info
.sinfo_flags
= sp
->default_flags
;
4312 info
.sinfo_ppid
= sp
->default_ppid
;
4313 info
.sinfo_context
= sp
->default_context
;
4314 info
.sinfo_timetolive
= sp
->default_timetolive
;
4317 if (copy_to_user(optval
, &info
, sizeof(struct sctp_sndrcvinfo
)))
4325 * 7.1.5 SCTP_NODELAY
4327 * Turn on/off any Nagle-like algorithm. This means that packets are
4328 * generally sent as soon as possible and no unnecessary delays are
4329 * introduced, at the cost of more packets in the network. Expects an
4330 * integer boolean flag.
4333 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4334 char __user
*optval
, int __user
*optlen
)
4338 if (len
< sizeof(int))
4342 val
= (sctp_sk(sk
)->nodelay
== 1);
4343 if (put_user(len
, optlen
))
4345 if (copy_to_user(optval
, &val
, len
))
4352 * 7.1.1 SCTP_RTOINFO
4354 * The protocol parameters used to initialize and bound retransmission
4355 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4356 * and modify these parameters.
4357 * All parameters are time values, in milliseconds. A value of 0, when
4358 * modifying the parameters, indicates that the current value should not
4362 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4363 char __user
*optval
,
4364 int __user
*optlen
) {
4365 struct sctp_rtoinfo rtoinfo
;
4366 struct sctp_association
*asoc
;
4368 if (len
!= sizeof (struct sctp_rtoinfo
))
4371 if (copy_from_user(&rtoinfo
, optval
, sizeof (struct sctp_rtoinfo
)))
4374 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4376 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4379 /* Values corresponding to the specific association. */
4381 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4382 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4383 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4385 /* Values corresponding to the endpoint. */
4386 struct sctp_sock
*sp
= sctp_sk(sk
);
4388 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4389 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4390 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4393 if (put_user(len
, optlen
))
4396 if (copy_to_user(optval
, &rtoinfo
, len
))
4404 * 7.1.2 SCTP_ASSOCINFO
4406 * This option is used to tune the the maximum retransmission attempts
4407 * of the association.
4408 * Returns an error if the new association retransmission value is
4409 * greater than the sum of the retransmission value of the peer.
4410 * See [SCTP] for more information.
4413 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4414 char __user
*optval
,
4418 struct sctp_assocparams assocparams
;
4419 struct sctp_association
*asoc
;
4420 struct list_head
*pos
;
4423 if (len
!= sizeof (struct sctp_assocparams
))
4426 if (copy_from_user(&assocparams
, optval
,
4427 sizeof (struct sctp_assocparams
)))
4430 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4432 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4435 /* Values correspoinding to the specific association */
4437 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4438 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4439 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4440 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4442 (asoc
->cookie_life
.tv_usec
4445 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4449 assocparams
.sasoc_number_peer_destinations
= cnt
;
4451 /* Values corresponding to the endpoint */
4452 struct sctp_sock
*sp
= sctp_sk(sk
);
4454 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4455 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4456 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4457 assocparams
.sasoc_cookie_life
=
4458 sp
->assocparams
.sasoc_cookie_life
;
4459 assocparams
.sasoc_number_peer_destinations
=
4461 sasoc_number_peer_destinations
;
4464 if (put_user(len
, optlen
))
4467 if (copy_to_user(optval
, &assocparams
, len
))
4474 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4476 * This socket option is a boolean flag which turns on or off mapped V4
4477 * addresses. If this option is turned on and the socket is type
4478 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4479 * If this option is turned off, then no mapping will be done of V4
4480 * addresses and a user will receive both PF_INET6 and PF_INET type
4481 * addresses on the socket.
4483 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4484 char __user
*optval
, int __user
*optlen
)
4487 struct sctp_sock
*sp
= sctp_sk(sk
);
4489 if (len
< sizeof(int))
4494 if (put_user(len
, optlen
))
4496 if (copy_to_user(optval
, &val
, len
))
4503 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4504 * (chapter and verse is quoted at sctp_setsockopt_context())
4506 static int sctp_getsockopt_context(struct sock
*sk
, int len
,
4507 char __user
*optval
, int __user
*optlen
)
4509 struct sctp_assoc_value params
;
4510 struct sctp_sock
*sp
;
4511 struct sctp_association
*asoc
;
4513 if (len
!= sizeof(struct sctp_assoc_value
))
4516 if (copy_from_user(¶ms
, optval
, len
))
4521 if (params
.assoc_id
!= 0) {
4522 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4525 params
.assoc_value
= asoc
->default_rcv_context
;
4527 params
.assoc_value
= sp
->default_rcv_context
;
4530 if (put_user(len
, optlen
))
4532 if (copy_to_user(optval
, ¶ms
, len
))
4539 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4541 * This socket option specifies the maximum size to put in any outgoing
4542 * SCTP chunk. If a message is larger than this size it will be
4543 * fragmented by SCTP into the specified size. Note that the underlying
4544 * SCTP implementation may fragment into smaller sized chunks when the
4545 * PMTU of the underlying association is smaller than the value set by
4548 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4549 char __user
*optval
, int __user
*optlen
)
4553 if (len
< sizeof(int))
4558 val
= sctp_sk(sk
)->user_frag
;
4559 if (put_user(len
, optlen
))
4561 if (copy_to_user(optval
, &val
, len
))
4567 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
4568 char __user
*optval
, int __user
*optlen
)
4573 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4576 /* I can hardly begin to describe how wrong this is. This is
4577 * so broken as to be worse than useless. The API draft
4578 * REALLY is NOT helpful here... I am not convinced that the
4579 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4580 * are at all well-founded.
4582 if (level
!= SOL_SCTP
) {
4583 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4585 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
4589 if (get_user(len
, optlen
))
4596 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
4598 case SCTP_DISABLE_FRAGMENTS
:
4599 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
4603 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
4605 case SCTP_AUTOCLOSE
:
4606 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
4608 case SCTP_SOCKOPT_PEELOFF
:
4609 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
4611 case SCTP_PEER_ADDR_PARAMS
:
4612 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
4615 case SCTP_DELAYED_ACK_TIME
:
4616 retval
= sctp_getsockopt_delayed_ack_time(sk
, len
, optval
,
4620 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
4622 case SCTP_GET_PEER_ADDRS_NUM_OLD
:
4623 retval
= sctp_getsockopt_peer_addrs_num_old(sk
, len
, optval
,
4626 case SCTP_GET_LOCAL_ADDRS_NUM_OLD
:
4627 retval
= sctp_getsockopt_local_addrs_num_old(sk
, len
, optval
,
4630 case SCTP_GET_PEER_ADDRS_OLD
:
4631 retval
= sctp_getsockopt_peer_addrs_old(sk
, len
, optval
,
4634 case SCTP_GET_LOCAL_ADDRS_OLD
:
4635 retval
= sctp_getsockopt_local_addrs_old(sk
, len
, optval
,
4638 case SCTP_GET_PEER_ADDRS
:
4639 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
4642 case SCTP_GET_LOCAL_ADDRS
:
4643 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
4646 case SCTP_DEFAULT_SEND_PARAM
:
4647 retval
= sctp_getsockopt_default_send_param(sk
, len
,
4650 case SCTP_PRIMARY_ADDR
:
4651 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
4654 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
4657 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
4659 case SCTP_ASSOCINFO
:
4660 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
4662 case SCTP_I_WANT_MAPPED_V4_ADDR
:
4663 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
4666 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
4668 case SCTP_GET_PEER_ADDR_INFO
:
4669 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
4672 case SCTP_ADAPTATION_LAYER
:
4673 retval
= sctp_getsockopt_adaptation_layer(sk
, len
, optval
,
4677 retval
= sctp_getsockopt_context(sk
, len
, optval
, optlen
);
4680 retval
= -ENOPROTOOPT
;
4684 sctp_release_sock(sk
);
4688 static void sctp_hash(struct sock
*sk
)
4693 static void sctp_unhash(struct sock
*sk
)
4698 /* Check if port is acceptable. Possibly find first available port.
4700 * The port hash table (contained in the 'global' SCTP protocol storage
4701 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4702 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4703 * list (the list number is the port number hashed out, so as you
4704 * would expect from a hash function, all the ports in a given list have
4705 * such a number that hashes out to the same list number; you were
4706 * expecting that, right?); so each list has a set of ports, with a
4707 * link to the socket (struct sock) that uses it, the port number and
4708 * a fastreuse flag (FIXME: NPI ipg).
4710 static struct sctp_bind_bucket
*sctp_bucket_create(
4711 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
4713 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
4715 struct sctp_bind_hashbucket
*head
; /* hash list */
4716 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
4717 unsigned short snum
;
4720 snum
= ntohs(addr
->v4
.sin_port
);
4722 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
4723 sctp_local_bh_disable();
4726 /* Search for an available port.
4728 * 'sctp_port_rover' was the last port assigned, so
4729 * we start to search from 'sctp_port_rover +
4730 * 1'. What we do is first check if port 'rover' is
4731 * already in the hash table; if not, we use that; if
4732 * it is, we try next.
4734 int low
= sysctl_local_port_range
[0];
4735 int high
= sysctl_local_port_range
[1];
4736 int remaining
= (high
- low
) + 1;
4740 sctp_spin_lock(&sctp_port_alloc_lock
);
4741 rover
= sctp_port_rover
;
4744 if ((rover
< low
) || (rover
> high
))
4746 index
= sctp_phashfn(rover
);
4747 head
= &sctp_port_hashtable
[index
];
4748 sctp_spin_lock(&head
->lock
);
4749 for (pp
= head
->chain
; pp
; pp
= pp
->next
)
4750 if (pp
->port
== rover
)
4754 sctp_spin_unlock(&head
->lock
);
4755 } while (--remaining
> 0);
4756 sctp_port_rover
= rover
;
4757 sctp_spin_unlock(&sctp_port_alloc_lock
);
4759 /* Exhausted local port range during search? */
4764 /* OK, here is the one we will use. HEAD (the port
4765 * hash table list entry) is non-NULL and we hold it's
4770 /* We are given an specific port number; we verify
4771 * that it is not being used. If it is used, we will
4772 * exahust the search in the hash list corresponding
4773 * to the port number (snum) - we detect that with the
4774 * port iterator, pp being NULL.
4776 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
4777 sctp_spin_lock(&head
->lock
);
4778 for (pp
= head
->chain
; pp
; pp
= pp
->next
) {
4779 if (pp
->port
== snum
)
4786 if (!hlist_empty(&pp
->owner
)) {
4787 /* We had a port hash table hit - there is an
4788 * available port (pp != NULL) and it is being
4789 * used by other socket (pp->owner not empty); that other
4790 * socket is going to be sk2.
4792 int reuse
= sk
->sk_reuse
;
4794 struct hlist_node
*node
;
4796 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4797 if (pp
->fastreuse
&& sk
->sk_reuse
)
4800 /* Run through the list of sockets bound to the port
4801 * (pp->port) [via the pointers bind_next and
4802 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4803 * we get the endpoint they describe and run through
4804 * the endpoint's list of IP (v4 or v6) addresses,
4805 * comparing each of the addresses with the address of
4806 * the socket sk. If we find a match, then that means
4807 * that this port/socket (sk) combination are already
4810 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
4811 struct sctp_endpoint
*ep2
;
4812 ep2
= sctp_sk(sk2
)->ep
;
4814 if (reuse
&& sk2
->sk_reuse
)
4817 if (sctp_bind_addr_match(&ep2
->base
.bind_addr
, addr
,
4823 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4826 /* If there was a hash table miss, create a new port. */
4828 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
4831 /* In either case (hit or miss), make sure fastreuse is 1 only
4832 * if sk->sk_reuse is too (that is, if the caller requested
4833 * SO_REUSEADDR on this socket -sk-).
4835 if (hlist_empty(&pp
->owner
))
4836 pp
->fastreuse
= sk
->sk_reuse
? 1 : 0;
4837 else if (pp
->fastreuse
&& !sk
->sk_reuse
)
4840 /* We are set, so fill up all the data in the hash table
4841 * entry, tie the socket list information with the rest of the
4842 * sockets FIXME: Blurry, NPI (ipg).
4845 inet_sk(sk
)->num
= snum
;
4846 if (!sctp_sk(sk
)->bind_hash
) {
4847 sk_add_bind_node(sk
, &pp
->owner
);
4848 sctp_sk(sk
)->bind_hash
= pp
;
4853 sctp_spin_unlock(&head
->lock
);
4856 sctp_local_bh_enable();
4860 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4861 * port is requested.
4863 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
4866 union sctp_addr addr
;
4867 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4869 /* Set up a dummy address struct from the sk. */
4870 af
->from_sk(&addr
, sk
);
4871 addr
.v4
.sin_port
= htons(snum
);
4873 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4874 ret
= sctp_get_port_local(sk
, &addr
);
4876 return (ret
? 1 : 0);
4880 * 3.1.3 listen() - UDP Style Syntax
4882 * By default, new associations are not accepted for UDP style sockets.
4883 * An application uses listen() to mark a socket as being able to
4884 * accept new associations.
4886 SCTP_STATIC
int sctp_seqpacket_listen(struct sock
*sk
, int backlog
)
4888 struct sctp_sock
*sp
= sctp_sk(sk
);
4889 struct sctp_endpoint
*ep
= sp
->ep
;
4891 /* Only UDP style sockets that are not peeled off are allowed to
4894 if (!sctp_style(sk
, UDP
))
4897 /* If backlog is zero, disable listening. */
4899 if (sctp_sstate(sk
, CLOSED
))
4902 sctp_unhash_endpoint(ep
);
4903 sk
->sk_state
= SCTP_SS_CLOSED
;
4906 /* Return if we are already listening. */
4907 if (sctp_sstate(sk
, LISTENING
))
4911 * If a bind() or sctp_bindx() is not called prior to a listen()
4912 * call that allows new associations to be accepted, the system
4913 * picks an ephemeral port and will choose an address set equivalent
4914 * to binding with a wildcard address.
4916 * This is not currently spelled out in the SCTP sockets
4917 * extensions draft, but follows the practice as seen in TCP
4920 if (!ep
->base
.bind_addr
.port
) {
4921 if (sctp_autobind(sk
))
4924 sk
->sk_state
= SCTP_SS_LISTENING
;
4925 sctp_hash_endpoint(ep
);
4930 * 4.1.3 listen() - TCP Style Syntax
4932 * Applications uses listen() to ready the SCTP endpoint for accepting
4933 * inbound associations.
4935 SCTP_STATIC
int sctp_stream_listen(struct sock
*sk
, int backlog
)
4937 struct sctp_sock
*sp
= sctp_sk(sk
);
4938 struct sctp_endpoint
*ep
= sp
->ep
;
4940 /* If backlog is zero, disable listening. */
4942 if (sctp_sstate(sk
, CLOSED
))
4945 sctp_unhash_endpoint(ep
);
4946 sk
->sk_state
= SCTP_SS_CLOSED
;
4949 if (sctp_sstate(sk
, LISTENING
))
4953 * If a bind() or sctp_bindx() is not called prior to a listen()
4954 * call that allows new associations to be accepted, the system
4955 * picks an ephemeral port and will choose an address set equivalent
4956 * to binding with a wildcard address.
4958 * This is not currently spelled out in the SCTP sockets
4959 * extensions draft, but follows the practice as seen in TCP
4962 if (!ep
->base
.bind_addr
.port
) {
4963 if (sctp_autobind(sk
))
4966 sk
->sk_state
= SCTP_SS_LISTENING
;
4967 sk
->sk_max_ack_backlog
= backlog
;
4968 sctp_hash_endpoint(ep
);
4973 * Move a socket to LISTENING state.
4975 int sctp_inet_listen(struct socket
*sock
, int backlog
)
4977 struct sock
*sk
= sock
->sk
;
4978 struct crypto_hash
*tfm
= NULL
;
4981 if (unlikely(backlog
< 0))
4986 if (sock
->state
!= SS_UNCONNECTED
)
4989 /* Allocate HMAC for generating cookie. */
4990 if (sctp_hmac_alg
) {
4991 tfm
= crypto_alloc_hash(sctp_hmac_alg
, 0, CRYPTO_ALG_ASYNC
);
4993 if (net_ratelimit()) {
4995 "SCTP: failed to load transform for %s: %ld\n",
4996 sctp_hmac_alg
, PTR_ERR(tfm
));
5003 switch (sock
->type
) {
5004 case SOCK_SEQPACKET
:
5005 err
= sctp_seqpacket_listen(sk
, backlog
);
5008 err
= sctp_stream_listen(sk
, backlog
);
5016 /* Store away the transform reference. */
5017 sctp_sk(sk
)->hmac
= tfm
;
5019 sctp_release_sock(sk
);
5022 crypto_free_hash(tfm
);
5027 * This function is done by modeling the current datagram_poll() and the
5028 * tcp_poll(). Note that, based on these implementations, we don't
5029 * lock the socket in this function, even though it seems that,
5030 * ideally, locking or some other mechanisms can be used to ensure
5031 * the integrity of the counters (sndbuf and wmem_alloc) used
5032 * in this place. We assume that we don't need locks either until proven
5035 * Another thing to note is that we include the Async I/O support
5036 * here, again, by modeling the current TCP/UDP code. We don't have
5037 * a good way to test with it yet.
5039 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
5041 struct sock
*sk
= sock
->sk
;
5042 struct sctp_sock
*sp
= sctp_sk(sk
);
5045 poll_wait(file
, sk
->sk_sleep
, wait
);
5047 /* A TCP-style listening socket becomes readable when the accept queue
5050 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
5051 return (!list_empty(&sp
->ep
->asocs
)) ?
5052 (POLLIN
| POLLRDNORM
) : 0;
5056 /* Is there any exceptional events? */
5057 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
5059 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5061 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
5064 /* Is it readable? Reconsider this code with TCP-style support. */
5065 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
5066 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
5067 mask
|= POLLIN
| POLLRDNORM
;
5069 /* The association is either gone or not ready. */
5070 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
5073 /* Is it writable? */
5074 if (sctp_writeable(sk
)) {
5075 mask
|= POLLOUT
| POLLWRNORM
;
5077 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
5079 * Since the socket is not locked, the buffer
5080 * might be made available after the writeable check and
5081 * before the bit is set. This could cause a lost I/O
5082 * signal. tcp_poll() has a race breaker for this race
5083 * condition. Based on their implementation, we put
5084 * in the following code to cover it as well.
5086 if (sctp_writeable(sk
))
5087 mask
|= POLLOUT
| POLLWRNORM
;
5092 /********************************************************************
5093 * 2nd Level Abstractions
5094 ********************************************************************/
5096 static struct sctp_bind_bucket
*sctp_bucket_create(
5097 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
5099 struct sctp_bind_bucket
*pp
;
5101 pp
= kmem_cache_alloc(sctp_bucket_cachep
, GFP_ATOMIC
);
5102 SCTP_DBG_OBJCNT_INC(bind_bucket
);
5106 INIT_HLIST_HEAD(&pp
->owner
);
5107 if ((pp
->next
= head
->chain
) != NULL
)
5108 pp
->next
->pprev
= &pp
->next
;
5110 pp
->pprev
= &head
->chain
;
5115 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5116 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
5118 if (pp
&& hlist_empty(&pp
->owner
)) {
5120 pp
->next
->pprev
= pp
->pprev
;
5121 *(pp
->pprev
) = pp
->next
;
5122 kmem_cache_free(sctp_bucket_cachep
, pp
);
5123 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
5127 /* Release this socket's reference to a local port. */
5128 static inline void __sctp_put_port(struct sock
*sk
)
5130 struct sctp_bind_hashbucket
*head
=
5131 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->num
)];
5132 struct sctp_bind_bucket
*pp
;
5134 sctp_spin_lock(&head
->lock
);
5135 pp
= sctp_sk(sk
)->bind_hash
;
5136 __sk_del_bind_node(sk
);
5137 sctp_sk(sk
)->bind_hash
= NULL
;
5138 inet_sk(sk
)->num
= 0;
5139 sctp_bucket_destroy(pp
);
5140 sctp_spin_unlock(&head
->lock
);
5143 void sctp_put_port(struct sock
*sk
)
5145 sctp_local_bh_disable();
5146 __sctp_put_port(sk
);
5147 sctp_local_bh_enable();
5151 * The system picks an ephemeral port and choose an address set equivalent
5152 * to binding with a wildcard address.
5153 * One of those addresses will be the primary address for the association.
5154 * This automatically enables the multihoming capability of SCTP.
5156 static int sctp_autobind(struct sock
*sk
)
5158 union sctp_addr autoaddr
;
5162 /* Initialize a local sockaddr structure to INADDR_ANY. */
5163 af
= sctp_sk(sk
)->pf
->af
;
5165 port
= htons(inet_sk(sk
)->num
);
5166 af
->inaddr_any(&autoaddr
, port
);
5168 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5171 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5174 * 4.2 The cmsghdr Structure *
5176 * When ancillary data is sent or received, any number of ancillary data
5177 * objects can be specified by the msg_control and msg_controllen members of
5178 * the msghdr structure, because each object is preceded by
5179 * a cmsghdr structure defining the object's length (the cmsg_len member).
5180 * Historically Berkeley-derived implementations have passed only one object
5181 * at a time, but this API allows multiple objects to be
5182 * passed in a single call to sendmsg() or recvmsg(). The following example
5183 * shows two ancillary data objects in a control buffer.
5185 * |<--------------------------- msg_controllen -------------------------->|
5188 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5190 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5193 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5195 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5198 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5199 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5201 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5203 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5210 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5211 sctp_cmsgs_t
*cmsgs
)
5213 struct cmsghdr
*cmsg
;
5215 for (cmsg
= CMSG_FIRSTHDR(msg
);
5217 cmsg
= CMSG_NXTHDR((struct msghdr
*)msg
, cmsg
)) {
5218 if (!CMSG_OK(msg
, cmsg
))
5221 /* Should we parse this header or ignore? */
5222 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5225 /* Strictly check lengths following example in SCM code. */
5226 switch (cmsg
->cmsg_type
) {
5228 /* SCTP Socket API Extension
5229 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5231 * This cmsghdr structure provides information for
5232 * initializing new SCTP associations with sendmsg().
5233 * The SCTP_INITMSG socket option uses this same data
5234 * structure. This structure is not used for
5237 * cmsg_level cmsg_type cmsg_data[]
5238 * ------------ ------------ ----------------------
5239 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5241 if (cmsg
->cmsg_len
!=
5242 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5244 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5248 /* SCTP Socket API Extension
5249 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5251 * This cmsghdr structure specifies SCTP options for
5252 * sendmsg() and describes SCTP header information
5253 * about a received message through recvmsg().
5255 * cmsg_level cmsg_type cmsg_data[]
5256 * ------------ ------------ ----------------------
5257 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5259 if (cmsg
->cmsg_len
!=
5260 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5264 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5266 /* Minimally, validate the sinfo_flags. */
5267 if (cmsgs
->info
->sinfo_flags
&
5268 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5269 SCTP_ABORT
| SCTP_EOF
))
5281 * Wait for a packet..
5282 * Note: This function is the same function as in core/datagram.c
5283 * with a few modifications to make lksctp work.
5285 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5290 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5292 /* Socket errors? */
5293 error
= sock_error(sk
);
5297 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5300 /* Socket shut down? */
5301 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5304 /* Sequenced packets can come disconnected. If so we report the
5309 /* Is there a good reason to think that we may receive some data? */
5310 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5313 /* Handle signals. */
5314 if (signal_pending(current
))
5317 /* Let another process have a go. Since we are going to sleep
5318 * anyway. Note: This may cause odd behaviors if the message
5319 * does not fit in the user's buffer, but this seems to be the
5320 * only way to honor MSG_DONTWAIT realistically.
5322 sctp_release_sock(sk
);
5323 *timeo_p
= schedule_timeout(*timeo_p
);
5327 finish_wait(sk
->sk_sleep
, &wait
);
5331 error
= sock_intr_errno(*timeo_p
);
5334 finish_wait(sk
->sk_sleep
, &wait
);
5339 /* Receive a datagram.
5340 * Note: This is pretty much the same routine as in core/datagram.c
5341 * with a few changes to make lksctp work.
5343 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5344 int noblock
, int *err
)
5347 struct sk_buff
*skb
;
5350 timeo
= sock_rcvtimeo(sk
, noblock
);
5352 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5353 timeo
, MAX_SCHEDULE_TIMEOUT
);
5356 /* Again only user level code calls this function,
5357 * so nothing interrupt level
5358 * will suddenly eat the receive_queue.
5360 * Look at current nfs client by the way...
5361 * However, this function was corrent in any case. 8)
5363 if (flags
& MSG_PEEK
) {
5364 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5365 skb
= skb_peek(&sk
->sk_receive_queue
);
5367 atomic_inc(&skb
->users
);
5368 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5370 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5376 /* Caller is allowed not to check sk->sk_err before calling. */
5377 error
= sock_error(sk
);
5381 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5384 /* User doesn't want to wait. */
5388 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5397 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5398 static void __sctp_write_space(struct sctp_association
*asoc
)
5400 struct sock
*sk
= asoc
->base
.sk
;
5401 struct socket
*sock
= sk
->sk_socket
;
5403 if ((sctp_wspace(asoc
) > 0) && sock
) {
5404 if (waitqueue_active(&asoc
->wait
))
5405 wake_up_interruptible(&asoc
->wait
);
5407 if (sctp_writeable(sk
)) {
5408 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
5409 wake_up_interruptible(sk
->sk_sleep
);
5411 /* Note that we try to include the Async I/O support
5412 * here by modeling from the current TCP/UDP code.
5413 * We have not tested with it yet.
5415 if (sock
->fasync_list
&&
5416 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5417 sock_wake_async(sock
, 2, POLL_OUT
);
5422 /* Do accounting for the sndbuf space.
5423 * Decrement the used sndbuf space of the corresponding association by the
5424 * data size which was just transmitted(freed).
5426 static void sctp_wfree(struct sk_buff
*skb
)
5428 struct sctp_association
*asoc
;
5429 struct sctp_chunk
*chunk
;
5432 /* Get the saved chunk pointer. */
5433 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
5436 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
5437 sizeof(struct sk_buff
) +
5438 sizeof(struct sctp_chunk
);
5440 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
5443 __sctp_write_space(asoc
);
5445 sctp_association_put(asoc
);
5448 /* Do accounting for the receive space on the socket.
5449 * Accounting for the association is done in ulpevent.c
5450 * We set this as a destructor for the cloned data skbs so that
5451 * accounting is done at the correct time.
5453 void sctp_sock_rfree(struct sk_buff
*skb
)
5455 struct sock
*sk
= skb
->sk
;
5456 struct sctp_ulpevent
*event
= sctp_skb2event(skb
);
5458 atomic_sub(event
->rmem_len
, &sk
->sk_rmem_alloc
);
5462 /* Helper function to wait for space in the sndbuf. */
5463 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
5466 struct sock
*sk
= asoc
->base
.sk
;
5468 long current_timeo
= *timeo_p
;
5471 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5472 asoc
, (long)(*timeo_p
), msg_len
);
5474 /* Increment the association's refcnt. */
5475 sctp_association_hold(asoc
);
5477 /* Wait on the association specific sndbuf space. */
5479 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5480 TASK_INTERRUPTIBLE
);
5483 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5486 if (signal_pending(current
))
5487 goto do_interrupted
;
5488 if (msg_len
<= sctp_wspace(asoc
))
5491 /* Let another process have a go. Since we are going
5494 sctp_release_sock(sk
);
5495 current_timeo
= schedule_timeout(current_timeo
);
5496 BUG_ON(sk
!= asoc
->base
.sk
);
5499 *timeo_p
= current_timeo
;
5503 finish_wait(&asoc
->wait
, &wait
);
5505 /* Release the association's refcnt. */
5506 sctp_association_put(asoc
);
5515 err
= sock_intr_errno(*timeo_p
);
5523 /* If socket sndbuf has changed, wake up all per association waiters. */
5524 void sctp_write_space(struct sock
*sk
)
5526 struct sctp_association
*asoc
;
5527 struct list_head
*pos
;
5529 /* Wake up the tasks in each wait queue. */
5530 list_for_each(pos
, &((sctp_sk(sk
))->ep
->asocs
)) {
5531 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
5532 __sctp_write_space(asoc
);
5536 /* Is there any sndbuf space available on the socket?
5538 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5539 * associations on the same socket. For a UDP-style socket with
5540 * multiple associations, it is possible for it to be "unwriteable"
5541 * prematurely. I assume that this is acceptable because
5542 * a premature "unwriteable" is better than an accidental "writeable" which
5543 * would cause an unwanted block under certain circumstances. For the 1-1
5544 * UDP-style sockets or TCP-style sockets, this code should work.
5547 static int sctp_writeable(struct sock
*sk
)
5551 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
5557 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5558 * returns immediately with EINPROGRESS.
5560 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
5562 struct sock
*sk
= asoc
->base
.sk
;
5564 long current_timeo
= *timeo_p
;
5567 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__
, asoc
,
5570 /* Increment the association's refcnt. */
5571 sctp_association_hold(asoc
);
5574 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5575 TASK_INTERRUPTIBLE
);
5578 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5580 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5583 if (signal_pending(current
))
5584 goto do_interrupted
;
5586 if (sctp_state(asoc
, ESTABLISHED
))
5589 /* Let another process have a go. Since we are going
5592 sctp_release_sock(sk
);
5593 current_timeo
= schedule_timeout(current_timeo
);
5596 *timeo_p
= current_timeo
;
5600 finish_wait(&asoc
->wait
, &wait
);
5602 /* Release the association's refcnt. */
5603 sctp_association_put(asoc
);
5608 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
5611 err
= -ECONNREFUSED
;
5615 err
= sock_intr_errno(*timeo_p
);
5623 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
5625 struct sctp_endpoint
*ep
;
5629 ep
= sctp_sk(sk
)->ep
;
5633 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
5634 TASK_INTERRUPTIBLE
);
5636 if (list_empty(&ep
->asocs
)) {
5637 sctp_release_sock(sk
);
5638 timeo
= schedule_timeout(timeo
);
5643 if (!sctp_sstate(sk
, LISTENING
))
5647 if (!list_empty(&ep
->asocs
))
5650 err
= sock_intr_errno(timeo
);
5651 if (signal_pending(current
))
5659 finish_wait(sk
->sk_sleep
, &wait
);
5664 void sctp_wait_for_close(struct sock
*sk
, long timeout
)
5669 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5670 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
5672 sctp_release_sock(sk
);
5673 timeout
= schedule_timeout(timeout
);
5675 } while (!signal_pending(current
) && timeout
);
5677 finish_wait(sk
->sk_sleep
, &wait
);
5680 static void sctp_sock_rfree_frag(struct sk_buff
*skb
)
5682 struct sk_buff
*frag
;
5687 /* Don't forget the fragments. */
5688 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
5689 sctp_sock_rfree_frag(frag
);
5692 sctp_sock_rfree(skb
);
5695 static void sctp_skb_set_owner_r_frag(struct sk_buff
*skb
, struct sock
*sk
)
5697 struct sk_buff
*frag
;
5702 /* Don't forget the fragments. */
5703 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
)
5704 sctp_skb_set_owner_r_frag(frag
, sk
);
5707 sctp_skb_set_owner_r(skb
, sk
);
5710 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5711 * and its messages to the newsk.
5713 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
5714 struct sctp_association
*assoc
,
5715 sctp_socket_type_t type
)
5717 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
5718 struct sctp_sock
*newsp
= sctp_sk(newsk
);
5719 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5720 struct sctp_endpoint
*newep
= newsp
->ep
;
5721 struct sk_buff
*skb
, *tmp
;
5722 struct sctp_ulpevent
*event
;
5725 /* Migrate socket buffer sizes and all the socket level options to the
5728 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
5729 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
5730 /* Brute force copy old sctp opt. */
5731 inet_sk_copy_descendant(newsk
, oldsk
);
5733 /* Restore the ep value that was overwritten with the above structure
5739 /* Hook this new socket in to the bind_hash list. */
5740 pp
= sctp_sk(oldsk
)->bind_hash
;
5741 sk_add_bind_node(newsk
, &pp
->owner
);
5742 sctp_sk(newsk
)->bind_hash
= pp
;
5743 inet_sk(newsk
)->num
= inet_sk(oldsk
)->num
;
5745 /* Copy the bind_addr list from the original endpoint to the new
5746 * endpoint so that we can handle restarts properly
5748 if (PF_INET6
== assoc
->base
.sk
->sk_family
)
5749 flags
= SCTP_ADDR6_ALLOWED
;
5750 if (assoc
->peer
.ipv4_address
)
5751 flags
|= SCTP_ADDR4_PEERSUPP
;
5752 if (assoc
->peer
.ipv6_address
)
5753 flags
|= SCTP_ADDR6_PEERSUPP
;
5754 sctp_bind_addr_copy(&newsp
->ep
->base
.bind_addr
,
5755 &oldsp
->ep
->base
.bind_addr
,
5756 SCTP_SCOPE_GLOBAL
, GFP_KERNEL
, flags
);
5758 /* Move any messages in the old socket's receive queue that are for the
5759 * peeled off association to the new socket's receive queue.
5761 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
5762 event
= sctp_skb2event(skb
);
5763 if (event
->asoc
== assoc
) {
5764 sctp_sock_rfree_frag(skb
);
5765 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
5766 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
5767 sctp_skb_set_owner_r_frag(skb
, newsk
);
5771 /* Clean up any messages pending delivery due to partial
5772 * delivery. Three cases:
5773 * 1) No partial deliver; no work.
5774 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5775 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5777 skb_queue_head_init(&newsp
->pd_lobby
);
5778 sctp_sk(newsk
)->pd_mode
= assoc
->ulpq
.pd_mode
;
5780 if (sctp_sk(oldsk
)->pd_mode
) {
5781 struct sk_buff_head
*queue
;
5783 /* Decide which queue to move pd_lobby skbs to. */
5784 if (assoc
->ulpq
.pd_mode
) {
5785 queue
= &newsp
->pd_lobby
;
5787 queue
= &newsk
->sk_receive_queue
;
5789 /* Walk through the pd_lobby, looking for skbs that
5790 * need moved to the new socket.
5792 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
5793 event
= sctp_skb2event(skb
);
5794 if (event
->asoc
== assoc
) {
5795 sctp_sock_rfree_frag(skb
);
5796 __skb_unlink(skb
, &oldsp
->pd_lobby
);
5797 __skb_queue_tail(queue
, skb
);
5798 sctp_skb_set_owner_r_frag(skb
, newsk
);
5802 /* Clear up any skbs waiting for the partial
5803 * delivery to finish.
5805 if (assoc
->ulpq
.pd_mode
)
5806 sctp_clear_pd(oldsk
);
5810 sctp_skb_for_each(skb
, &assoc
->ulpq
.reasm
, tmp
) {
5811 sctp_sock_rfree_frag(skb
);
5812 sctp_skb_set_owner_r_frag(skb
, newsk
);
5815 sctp_skb_for_each(skb
, &assoc
->ulpq
.lobby
, tmp
) {
5816 sctp_sock_rfree_frag(skb
);
5817 sctp_skb_set_owner_r_frag(skb
, newsk
);
5820 /* Set the type of socket to indicate that it is peeled off from the
5821 * original UDP-style socket or created with the accept() call on a
5822 * TCP-style socket..
5826 /* Mark the new socket "in-use" by the user so that any packets
5827 * that may arrive on the association after we've moved it are
5828 * queued to the backlog. This prevents a potential race between
5829 * backlog processing on the old socket and new-packet processing
5830 * on the new socket.
5832 sctp_lock_sock(newsk
);
5833 sctp_assoc_migrate(assoc
, newsk
);
5835 /* If the association on the newsk is already closed before accept()
5836 * is called, set RCV_SHUTDOWN flag.
5838 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
5839 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
5841 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
5842 sctp_release_sock(newsk
);
5845 /* This proto struct describes the ULP interface for SCTP. */
5846 struct proto sctp_prot
= {
5848 .owner
= THIS_MODULE
,
5849 .close
= sctp_close
,
5850 .connect
= sctp_connect
,
5851 .disconnect
= sctp_disconnect
,
5852 .accept
= sctp_accept
,
5853 .ioctl
= sctp_ioctl
,
5854 .init
= sctp_init_sock
,
5855 .destroy
= sctp_destroy_sock
,
5856 .shutdown
= sctp_shutdown
,
5857 .setsockopt
= sctp_setsockopt
,
5858 .getsockopt
= sctp_getsockopt
,
5859 .sendmsg
= sctp_sendmsg
,
5860 .recvmsg
= sctp_recvmsg
,
5862 .backlog_rcv
= sctp_backlog_rcv
,
5864 .unhash
= sctp_unhash
,
5865 .get_port
= sctp_get_port
,
5866 .obj_size
= sizeof(struct sctp_sock
),
5869 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5870 struct proto sctpv6_prot
= {
5872 .owner
= THIS_MODULE
,
5873 .close
= sctp_close
,
5874 .connect
= sctp_connect
,
5875 .disconnect
= sctp_disconnect
,
5876 .accept
= sctp_accept
,
5877 .ioctl
= sctp_ioctl
,
5878 .init
= sctp_init_sock
,
5879 .destroy
= sctp_destroy_sock
,
5880 .shutdown
= sctp_shutdown
,
5881 .setsockopt
= sctp_setsockopt
,
5882 .getsockopt
= sctp_getsockopt
,
5883 .sendmsg
= sctp_sendmsg
,
5884 .recvmsg
= sctp_recvmsg
,
5886 .backlog_rcv
= sctp_backlog_rcv
,
5888 .unhash
= sctp_unhash
,
5889 .get_port
= sctp_get_port
,
5890 .obj_size
= sizeof(struct sctp6_sock
),
5892 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */