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 kmem_cache_t
*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 laddr
->v4
.sin_port
= ntohs(laddr
->v4
.sin_port
);
233 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
234 (union sctp_addr
*)addr
,
236 laddr
->v4
.sin_port
= htons(laddr
->v4
.sin_port
);
241 id_asoc
= sctp_id2assoc(sk
, id
);
242 if (id_asoc
&& (id_asoc
!= addr_asoc
))
245 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
246 (union sctp_addr
*)addr
);
251 /* API 3.1.2 bind() - UDP Style Syntax
252 * The syntax of bind() is,
254 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
256 * sd - the socket descriptor returned by socket().
257 * addr - the address structure (struct sockaddr_in or struct
258 * sockaddr_in6 [RFC 2553]),
259 * addr_len - the size of the address structure.
261 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
267 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
270 /* Disallow binding twice. */
271 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
272 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
277 sctp_release_sock(sk
);
282 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
284 /* Verify this is a valid sockaddr. */
285 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
286 union sctp_addr
*addr
, int len
)
290 /* Check minimum size. */
291 if (len
< sizeof (struct sockaddr
))
294 /* Does this PF support this AF? */
295 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
298 /* If we get this far, af is valid. */
299 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
301 if (len
< af
->sockaddr_len
)
307 /* Bind a local address either to an endpoint or to an association. */
308 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
310 struct sctp_sock
*sp
= sctp_sk(sk
);
311 struct sctp_endpoint
*ep
= sp
->ep
;
312 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
317 /* Common sockaddr verification. */
318 af
= sctp_sockaddr_af(sp
, addr
, len
);
320 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
325 snum
= ntohs(addr
->v4
.sin_port
);
327 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
328 ", port: %d, new port: %d, len: %d)\n",
334 /* PF specific bind() address verification. */
335 if (!sp
->pf
->bind_verify(sp
, addr
))
336 return -EADDRNOTAVAIL
;
338 /* We must either be unbound, or bind to the same port. */
339 if (bp
->port
&& (snum
!= bp
->port
)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum
, bp
->port
);
346 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
353 if ((ret
= sctp_get_port_local(sk
, addr
))) {
354 if (ret
== (long) sk
) {
355 /* This endpoint has a conflicting address. */
362 /* Refresh ephemeral port. */
364 bp
->port
= inet_sk(sk
)->num
;
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep
->base
.addr_lock
);
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr
->v4
.sin_port
= ntohs(addr
->v4
.sin_port
);
372 ret
= sctp_add_bind_addr(bp
, addr
, 1, GFP_ATOMIC
);
373 addr
->v4
.sin_port
= htons(addr
->v4
.sin_port
);
374 sctp_write_unlock(&ep
->base
.addr_lock
);
375 sctp_local_bh_enable();
377 /* Copy back into socket for getsockname() use. */
379 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
380 af
->to_sk_saddr(addr
, sk
);
386 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
389 * at any one time. If a sender, after sending an ASCONF chunk, decides
390 * it needs to transfer another ASCONF Chunk, it MUST wait until the
391 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
392 * subsequent ASCONF. Note this restriction binds each side, so at any
393 * time two ASCONF may be in-transit on any given association (one sent
394 * from each endpoint).
396 static int sctp_send_asconf(struct sctp_association
*asoc
,
397 struct sctp_chunk
*chunk
)
401 /* If there is an outstanding ASCONF chunk, queue it for later
404 if (asoc
->addip_last_asconf
) {
405 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
409 /* Hold the chunk until an ASCONF_ACK is received. */
410 sctp_chunk_hold(chunk
);
411 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
413 sctp_chunk_free(chunk
);
415 asoc
->addip_last_asconf
= chunk
;
421 /* Add a list of addresses as bind addresses to local endpoint or
424 * Basically run through each address specified in the addrs/addrcnt
425 * array/length pair, determine if it is IPv6 or IPv4 and call
426 * sctp_do_bind() on it.
428 * If any of them fails, then the operation will be reversed and the
429 * ones that were added will be removed.
431 * Only sctp_setsockopt_bindx() is supposed to call this function.
433 int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
438 struct sockaddr
*sa_addr
;
441 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
445 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
446 /* The list may contain either IPv4 or IPv6 address;
447 * determine the address length for walking thru the list.
449 sa_addr
= (struct sockaddr
*)addr_buf
;
450 af
= sctp_get_af_specific(sa_addr
->sa_family
);
456 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
459 addr_buf
+= af
->sockaddr_len
;
463 /* Failed. Cleanup the ones that have been added */
465 sctp_bindx_rem(sk
, addrs
, cnt
);
473 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
474 * associations that are part of the endpoint indicating that a list of local
475 * addresses are added to the endpoint.
477 * If any of the addresses is already in the bind address list of the
478 * association, we do not send the chunk for that association. But it will not
479 * affect other associations.
481 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 static int sctp_send_asconf_add_ip(struct sock
*sk
,
484 struct sockaddr
*addrs
,
487 struct sctp_sock
*sp
;
488 struct sctp_endpoint
*ep
;
489 struct sctp_association
*asoc
;
490 struct sctp_bind_addr
*bp
;
491 struct sctp_chunk
*chunk
;
492 struct sctp_sockaddr_entry
*laddr
;
493 union sctp_addr
*addr
;
494 union sctp_addr saveaddr
;
497 struct list_head
*pos
;
502 if (!sctp_addip_enable
)
508 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
509 __FUNCTION__
, sk
, addrs
, addrcnt
);
511 list_for_each(pos
, &ep
->asocs
) {
512 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
514 if (!asoc
->peer
.asconf_capable
)
517 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
520 if (!sctp_state(asoc
, ESTABLISHED
))
523 /* Check if any address in the packed array of addresses is
524 * in the bind address list of the association. If so,
525 * do not send the asconf chunk to its peer, but continue with
526 * other associations.
529 for (i
= 0; i
< addrcnt
; i
++) {
530 addr
= (union sctp_addr
*)addr_buf
;
531 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
537 if (sctp_assoc_lookup_laddr(asoc
, addr
))
540 addr_buf
+= af
->sockaddr_len
;
545 /* Use the first address in bind addr list of association as
546 * Address Parameter of ASCONF CHUNK.
548 sctp_read_lock(&asoc
->base
.addr_lock
);
549 bp
= &asoc
->base
.bind_addr
;
550 p
= bp
->address_list
.next
;
551 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
552 sctp_read_unlock(&asoc
->base
.addr_lock
);
554 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
555 addrcnt
, SCTP_PARAM_ADD_IP
);
561 retval
= sctp_send_asconf(asoc
, chunk
);
565 /* Add the new addresses to the bind address list with
566 * use_as_src set to 0.
568 sctp_local_bh_disable();
569 sctp_write_lock(&asoc
->base
.addr_lock
);
571 for (i
= 0; i
< addrcnt
; i
++) {
572 addr
= (union sctp_addr
*)addr_buf
;
573 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
574 memcpy(&saveaddr
, addr
, af
->sockaddr_len
);
575 saveaddr
.v4
.sin_port
= ntohs(saveaddr
.v4
.sin_port
);
576 retval
= sctp_add_bind_addr(bp
, &saveaddr
, 0,
578 addr_buf
+= af
->sockaddr_len
;
580 sctp_write_unlock(&asoc
->base
.addr_lock
);
581 sctp_local_bh_enable();
588 /* Remove a list of addresses from bind addresses list. Do not remove the
591 * Basically run through each address specified in the addrs/addrcnt
592 * array/length pair, determine if it is IPv6 or IPv4 and call
593 * sctp_del_bind() on it.
595 * If any of them fails, then the operation will be reversed and the
596 * ones that were removed will be added back.
598 * At least one address has to be left; if only one address is
599 * available, the operation will return -EBUSY.
601 * Only sctp_setsockopt_bindx() is supposed to call this function.
603 int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
605 struct sctp_sock
*sp
= sctp_sk(sk
);
606 struct sctp_endpoint
*ep
= sp
->ep
;
608 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
610 union sctp_addr saveaddr
;
612 struct sockaddr
*sa_addr
;
615 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
619 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
620 /* If the bind address list is empty or if there is only one
621 * bind address, there is nothing more to be removed (we need
622 * at least one address here).
624 if (list_empty(&bp
->address_list
) ||
625 (sctp_list_single_entry(&bp
->address_list
))) {
630 /* The list may contain either IPv4 or IPv6 address;
631 * determine the address length to copy the address to
634 sa_addr
= (struct sockaddr
*)addr_buf
;
635 af
= sctp_get_af_specific(sa_addr
->sa_family
);
640 memcpy(&saveaddr
, sa_addr
, af
->sockaddr_len
);
641 saveaddr
.v4
.sin_port
= ntohs(saveaddr
.v4
.sin_port
);
642 if (saveaddr
.v4
.sin_port
!= bp
->port
) {
647 /* FIXME - There is probably a need to check if sk->sk_saddr and
648 * sk->sk_rcv_addr are currently set to one of the addresses to
649 * be removed. This is something which needs to be looked into
650 * when we are fixing the outstanding issues with multi-homing
651 * socket routing and failover schemes. Refer to comments in
652 * sctp_do_bind(). -daisy
654 sctp_local_bh_disable();
655 sctp_write_lock(&ep
->base
.addr_lock
);
657 retval
= sctp_del_bind_addr(bp
, &saveaddr
);
659 sctp_write_unlock(&ep
->base
.addr_lock
);
660 sctp_local_bh_enable();
662 addr_buf
+= af
->sockaddr_len
;
665 /* Failed. Add the ones that has been removed back */
667 sctp_bindx_add(sk
, addrs
, cnt
);
675 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
676 * the associations that are part of the endpoint indicating that a list of
677 * local addresses are removed from the endpoint.
679 * If any of the addresses is already in the bind address list of the
680 * association, we do not send the chunk for that association. But it will not
681 * affect other associations.
683 * Only sctp_setsockopt_bindx() is supposed to call this function.
685 static int sctp_send_asconf_del_ip(struct sock
*sk
,
686 struct sockaddr
*addrs
,
689 struct sctp_sock
*sp
;
690 struct sctp_endpoint
*ep
;
691 struct sctp_association
*asoc
;
692 struct sctp_transport
*transport
;
693 struct sctp_bind_addr
*bp
;
694 struct sctp_chunk
*chunk
;
695 union sctp_addr
*laddr
;
696 union sctp_addr saveaddr
;
699 struct list_head
*pos
, *pos1
;
700 struct sctp_sockaddr_entry
*saddr
;
704 if (!sctp_addip_enable
)
710 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
711 __FUNCTION__
, sk
, addrs
, addrcnt
);
713 list_for_each(pos
, &ep
->asocs
) {
714 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
716 if (!asoc
->peer
.asconf_capable
)
719 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
722 if (!sctp_state(asoc
, ESTABLISHED
))
725 /* Check if any address in the packed array of addresses is
726 * not present in the bind address list of the association.
727 * If so, do not send the asconf chunk to its peer, but
728 * continue with other associations.
731 for (i
= 0; i
< addrcnt
; i
++) {
732 laddr
= (union sctp_addr
*)addr_buf
;
733 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
739 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
742 addr_buf
+= af
->sockaddr_len
;
747 /* Find one address in the association's bind address list
748 * that is not in the packed array of addresses. This is to
749 * make sure that we do not delete all the addresses in the
752 sctp_read_lock(&asoc
->base
.addr_lock
);
753 bp
= &asoc
->base
.bind_addr
;
754 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
756 sctp_read_unlock(&asoc
->base
.addr_lock
);
760 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
767 /* Reset use_as_src flag for the addresses in the bind address
768 * list that are to be deleted.
770 sctp_local_bh_disable();
771 sctp_write_lock(&asoc
->base
.addr_lock
);
773 for (i
= 0; i
< addrcnt
; i
++) {
774 laddr
= (union sctp_addr
*)addr_buf
;
775 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
776 memcpy(&saveaddr
, laddr
, af
->sockaddr_len
);
777 saveaddr
.v4
.sin_port
= ntohs(saveaddr
.v4
.sin_port
);
778 list_for_each(pos1
, &bp
->address_list
) {
779 saddr
= list_entry(pos1
,
780 struct sctp_sockaddr_entry
,
782 if (sctp_cmp_addr_exact(&saddr
->a
, &saveaddr
))
783 saddr
->use_as_src
= 0;
785 addr_buf
+= af
->sockaddr_len
;
787 sctp_write_unlock(&asoc
->base
.addr_lock
);
788 sctp_local_bh_enable();
790 /* Update the route and saddr entries for all the transports
791 * as some of the addresses in the bind address list are
792 * about to be deleted and cannot be used as source addresses.
794 list_for_each(pos1
, &asoc
->peer
.transport_addr_list
) {
795 transport
= list_entry(pos1
, struct sctp_transport
,
797 dst_release(transport
->dst
);
798 sctp_transport_route(transport
, NULL
,
799 sctp_sk(asoc
->base
.sk
));
802 retval
= sctp_send_asconf(asoc
, chunk
);
808 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
811 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
814 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
815 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
818 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
819 * Section 3.1.2 for this usage.
821 * addrs is a pointer to an array of one or more socket addresses. Each
822 * address is contained in its appropriate structure (i.e. struct
823 * sockaddr_in or struct sockaddr_in6) the family of the address type
824 * must be used to distengish the address length (note that this
825 * representation is termed a "packed array" of addresses). The caller
826 * specifies the number of addresses in the array with addrcnt.
828 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
829 * -1, and sets errno to the appropriate error code.
831 * For SCTP, the port given in each socket address must be the same, or
832 * sctp_bindx() will fail, setting errno to EINVAL.
834 * The flags parameter is formed from the bitwise OR of zero or more of
835 * the following currently defined flags:
837 * SCTP_BINDX_ADD_ADDR
839 * SCTP_BINDX_REM_ADDR
841 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
842 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
843 * addresses from the association. The two flags are mutually exclusive;
844 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
845 * not remove all addresses from an association; sctp_bindx() will
846 * reject such an attempt with EINVAL.
848 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
849 * additional addresses with an endpoint after calling bind(). Or use
850 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
851 * socket is associated with so that no new association accepted will be
852 * associated with those addresses. If the endpoint supports dynamic
853 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
854 * endpoint to send the appropriate message to the peer to change the
855 * peers address lists.
857 * Adding and removing addresses from a connected association is
858 * optional functionality. Implementations that do not support this
859 * functionality should return EOPNOTSUPP.
861 * Basically do nothing but copying the addresses from user to kernel
862 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
863 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
866 * We don't use copy_from_user() for optimization: we first do the
867 * sanity checks (buffer size -fast- and access check-healthy
868 * pointer); if all of those succeed, then we can alloc the memory
869 * (expensive operation) needed to copy the data to kernel. Then we do
870 * the copying without checking the user space area
871 * (__copy_from_user()).
873 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
876 * sk The sk of the socket
877 * addrs The pointer to the addresses in user land
878 * addrssize Size of the addrs buffer
879 * op Operation to perform (add or remove, see the flags of
882 * Returns 0 if ok, <0 errno code on error.
884 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
885 struct sockaddr __user
*addrs
,
886 int addrs_size
, int op
)
888 struct sockaddr
*kaddrs
;
892 struct sockaddr
*sa_addr
;
896 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
897 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
899 if (unlikely(addrs_size
<= 0))
902 /* Check the user passed a healthy pointer. */
903 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
906 /* Alloc space for the address array in kernel memory. */
907 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
908 if (unlikely(!kaddrs
))
911 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
916 /* Walk through the addrs buffer and count the number of addresses. */
918 while (walk_size
< addrs_size
) {
919 sa_addr
= (struct sockaddr
*)addr_buf
;
920 af
= sctp_get_af_specific(sa_addr
->sa_family
);
922 /* If the address family is not supported or if this address
923 * causes the address buffer to overflow return EINVAL.
925 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
930 addr_buf
+= af
->sockaddr_len
;
931 walk_size
+= af
->sockaddr_len
;
936 case SCTP_BINDX_ADD_ADDR
:
937 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
940 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
943 case SCTP_BINDX_REM_ADDR
:
944 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
947 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
961 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
963 * Common routine for handling connect() and sctp_connectx().
964 * Connect will come in with just a single address.
966 static int __sctp_connect(struct sock
* sk
,
967 struct sockaddr
*kaddrs
,
970 struct sctp_sock
*sp
;
971 struct sctp_endpoint
*ep
;
972 struct sctp_association
*asoc
= NULL
;
973 struct sctp_association
*asoc2
;
974 struct sctp_transport
*transport
;
982 struct sockaddr
*sa_addr
;
988 /* connect() cannot be done on a socket that is already in ESTABLISHED
989 * state - UDP-style peeled off socket or a TCP-style socket that
990 * is already connected.
991 * It cannot be done even on a TCP-style listening socket.
993 if (sctp_sstate(sk
, ESTABLISHED
) ||
994 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
999 /* Walk through the addrs buffer and count the number of addresses. */
1001 while (walk_size
< addrs_size
) {
1002 sa_addr
= (struct sockaddr
*)addr_buf
;
1003 af
= sctp_get_af_specific(sa_addr
->sa_family
);
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
1008 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
1013 err
= sctp_verify_addr(sk
, (union sctp_addr
*)sa_addr
,
1018 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
1019 to
.v4
.sin_port
= ntohs(to
.v4
.sin_port
);
1021 /* Check if there already is a matching association on the
1022 * endpoint (other than the one created here).
1024 asoc2
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1025 if (asoc2
&& asoc2
!= asoc
) {
1026 if (asoc2
->state
>= SCTP_STATE_ESTABLISHED
)
1033 /* If we could not find a matching association on the endpoint,
1034 * make sure that there is no peeled-off association matching
1035 * the peer address even on another socket.
1037 if (sctp_endpoint_is_peeled_off(ep
, &to
)) {
1038 err
= -EADDRNOTAVAIL
;
1043 /* If a bind() or sctp_bindx() is not called prior to
1044 * an sctp_connectx() call, the system picks an
1045 * ephemeral port and will choose an address set
1046 * equivalent to binding with a wildcard address.
1048 if (!ep
->base
.bind_addr
.port
) {
1049 if (sctp_autobind(sk
)) {
1055 * If an unprivileged user inherits a 1-many
1056 * style socket with open associations on a
1057 * privileged port, it MAY be permitted to
1058 * accept new associations, but it SHOULD NOT
1059 * be permitted to open new associations.
1061 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1062 !capable(CAP_NET_BIND_SERVICE
)) {
1068 scope
= sctp_scope(&to
);
1069 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1076 /* Prime the peer's transport structures. */
1077 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
,
1085 addr_buf
+= af
->sockaddr_len
;
1086 walk_size
+= af
->sockaddr_len
;
1089 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1094 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1099 /* Initialize sk's dport and daddr for getpeername() */
1100 inet_sk(sk
)->dport
= htons(asoc
->peer
.port
);
1101 af
= sctp_get_af_specific(to
.sa
.sa_family
);
1102 af
->to_sk_daddr(&to
, sk
);
1105 timeo
= sock_sndtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
1106 err
= sctp_wait_for_connect(asoc
, &timeo
);
1108 /* Don't free association on exit. */
1113 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1114 " kaddrs: %p err: %d\n",
1117 sctp_association_free(asoc
);
1121 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1124 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1126 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1127 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1128 * or IPv6 addresses.
1130 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1131 * Section 3.1.2 for this usage.
1133 * addrs is a pointer to an array of one or more socket addresses. Each
1134 * address is contained in its appropriate structure (i.e. struct
1135 * sockaddr_in or struct sockaddr_in6) the family of the address type
1136 * must be used to distengish the address length (note that this
1137 * representation is termed a "packed array" of addresses). The caller
1138 * specifies the number of addresses in the array with addrcnt.
1140 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1141 * -1, and sets errno to the appropriate error code.
1143 * For SCTP, the port given in each socket address must be the same, or
1144 * sctp_connectx() will fail, setting errno to EINVAL.
1146 * An application can use sctp_connectx to initiate an association with
1147 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1148 * allows a caller to specify multiple addresses at which a peer can be
1149 * reached. The way the SCTP stack uses the list of addresses to set up
1150 * the association is implementation dependant. This function only
1151 * specifies that the stack will try to make use of all the addresses in
1152 * the list when needed.
1154 * Note that the list of addresses passed in is only used for setting up
1155 * the association. It does not necessarily equal the set of addresses
1156 * the peer uses for the resulting association. If the caller wants to
1157 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1158 * retrieve them after the association has been set up.
1160 * Basically do nothing but copying the addresses from user to kernel
1161 * land and invoking either sctp_connectx(). This is used for tunneling
1162 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1164 * We don't use copy_from_user() for optimization: we first do the
1165 * sanity checks (buffer size -fast- and access check-healthy
1166 * pointer); if all of those succeed, then we can alloc the memory
1167 * (expensive operation) needed to copy the data to kernel. Then we do
1168 * the copying without checking the user space area
1169 * (__copy_from_user()).
1171 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1174 * sk The sk of the socket
1175 * addrs The pointer to the addresses in user land
1176 * addrssize Size of the addrs buffer
1178 * Returns 0 if ok, <0 errno code on error.
1180 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1181 struct sockaddr __user
*addrs
,
1185 struct sockaddr
*kaddrs
;
1187 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1188 __FUNCTION__
, sk
, addrs
, addrs_size
);
1190 if (unlikely(addrs_size
<= 0))
1193 /* Check the user passed a healthy pointer. */
1194 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1197 /* Alloc space for the address array in kernel memory. */
1198 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1199 if (unlikely(!kaddrs
))
1202 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1205 err
= __sctp_connect(sk
, kaddrs
, addrs_size
);
1212 /* API 3.1.4 close() - UDP Style Syntax
1213 * Applications use close() to perform graceful shutdown (as described in
1214 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1215 * by a UDP-style socket.
1219 * ret = close(int sd);
1221 * sd - the socket descriptor of the associations to be closed.
1223 * To gracefully shutdown a specific association represented by the
1224 * UDP-style socket, an application should use the sendmsg() call,
1225 * passing no user data, but including the appropriate flag in the
1226 * ancillary data (see Section xxxx).
1228 * If sd in the close() call is a branched-off socket representing only
1229 * one association, the shutdown is performed on that association only.
1231 * 4.1.6 close() - TCP Style Syntax
1233 * Applications use close() to gracefully close down an association.
1237 * int close(int sd);
1239 * sd - the socket descriptor of the association to be closed.
1241 * After an application calls close() on a socket descriptor, no further
1242 * socket operations will succeed on that descriptor.
1244 * API 7.1.4 SO_LINGER
1246 * An application using the TCP-style socket can use this option to
1247 * perform the SCTP ABORT primitive. The linger option structure is:
1250 * int l_onoff; // option on/off
1251 * int l_linger; // linger time
1254 * To enable the option, set l_onoff to 1. If the l_linger value is set
1255 * to 0, calling close() is the same as the ABORT primitive. If the
1256 * value is set to a negative value, the setsockopt() call will return
1257 * an error. If the value is set to a positive value linger_time, the
1258 * close() can be blocked for at most linger_time ms. If the graceful
1259 * shutdown phase does not finish during this period, close() will
1260 * return but the graceful shutdown phase continues in the system.
1262 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1264 struct sctp_endpoint
*ep
;
1265 struct sctp_association
*asoc
;
1266 struct list_head
*pos
, *temp
;
1268 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1271 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1273 ep
= sctp_sk(sk
)->ep
;
1275 /* Walk all associations on an endpoint. */
1276 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1277 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1279 if (sctp_style(sk
, TCP
)) {
1280 /* A closed association can still be in the list if
1281 * it belongs to a TCP-style listening socket that is
1282 * not yet accepted. If so, free it. If not, send an
1283 * ABORT or SHUTDOWN based on the linger options.
1285 if (sctp_state(asoc
, CLOSED
)) {
1286 sctp_unhash_established(asoc
);
1287 sctp_association_free(asoc
);
1292 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
)
1293 sctp_primitive_ABORT(asoc
, NULL
);
1295 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1298 /* Clean up any skbs sitting on the receive queue. */
1299 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1300 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1302 /* On a TCP-style socket, block for at most linger_time if set. */
1303 if (sctp_style(sk
, TCP
) && timeout
)
1304 sctp_wait_for_close(sk
, timeout
);
1306 /* This will run the backlog queue. */
1307 sctp_release_sock(sk
);
1309 /* Supposedly, no process has access to the socket, but
1310 * the net layers still may.
1312 sctp_local_bh_disable();
1313 sctp_bh_lock_sock(sk
);
1315 /* Hold the sock, since sk_common_release() will put sock_put()
1316 * and we have just a little more cleanup.
1319 sk_common_release(sk
);
1321 sctp_bh_unlock_sock(sk
);
1322 sctp_local_bh_enable();
1326 SCTP_DBG_OBJCNT_DEC(sock
);
1329 /* Handle EPIPE error. */
1330 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1333 err
= sock_error(sk
) ? : -EPIPE
;
1334 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1335 send_sig(SIGPIPE
, current
, 0);
1339 /* API 3.1.3 sendmsg() - UDP Style Syntax
1341 * An application uses sendmsg() and recvmsg() calls to transmit data to
1342 * and receive data from its peer.
1344 * ssize_t sendmsg(int socket, const struct msghdr *message,
1347 * socket - the socket descriptor of the endpoint.
1348 * message - pointer to the msghdr structure which contains a single
1349 * user message and possibly some ancillary data.
1351 * See Section 5 for complete description of the data
1354 * flags - flags sent or received with the user message, see Section
1355 * 5 for complete description of the flags.
1357 * Note: This function could use a rewrite especially when explicit
1358 * connect support comes in.
1360 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1362 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1364 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1365 struct msghdr
*msg
, size_t msg_len
)
1367 struct sctp_sock
*sp
;
1368 struct sctp_endpoint
*ep
;
1369 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1370 struct sctp_transport
*transport
, *chunk_tp
;
1371 struct sctp_chunk
*chunk
;
1373 struct sockaddr
*msg_name
= NULL
;
1374 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1375 struct sctp_sndrcvinfo
*sinfo
;
1376 struct sctp_initmsg
*sinit
;
1377 sctp_assoc_t associd
= 0;
1378 sctp_cmsgs_t cmsgs
= { NULL
};
1382 __u16 sinfo_flags
= 0;
1383 struct sctp_datamsg
*datamsg
;
1384 struct list_head
*pos
;
1385 int msg_flags
= msg
->msg_flags
;
1387 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1394 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1396 /* We cannot send a message over a TCP-style listening socket. */
1397 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1402 /* Parse out the SCTP CMSGs. */
1403 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1406 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1410 /* Fetch the destination address for this packet. This
1411 * address only selects the association--it is not necessarily
1412 * the address we will send to.
1413 * For a peeled-off socket, msg_name is ignored.
1415 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1416 int msg_namelen
= msg
->msg_namelen
;
1418 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1423 if (msg_namelen
> sizeof(to
))
1424 msg_namelen
= sizeof(to
);
1425 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1426 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1428 to
.v4
.sin_addr
.s_addr
, to
.v4
.sin_port
);
1430 to
.v4
.sin_port
= ntohs(to
.v4
.sin_port
);
1431 msg_name
= msg
->msg_name
;
1437 /* Did the user specify SNDRCVINFO? */
1439 sinfo_flags
= sinfo
->sinfo_flags
;
1440 associd
= sinfo
->sinfo_assoc_id
;
1443 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1444 msg_len
, sinfo_flags
);
1446 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1447 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1452 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1453 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1454 * If SCTP_ABORT is set, the message length could be non zero with
1455 * the msg_iov set to the user abort reason.
1457 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1458 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1463 /* If SCTP_ADDR_OVER is set, there must be an address
1464 * specified in msg_name.
1466 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1473 SCTP_DEBUG_PRINTK("About to look up association.\n");
1477 /* If a msg_name has been specified, assume this is to be used. */
1479 /* Look for a matching association on the endpoint. */
1480 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1482 /* If we could not find a matching association on the
1483 * endpoint, make sure that it is not a TCP-style
1484 * socket that already has an association or there is
1485 * no peeled-off association on another socket.
1487 if ((sctp_style(sk
, TCP
) &&
1488 sctp_sstate(sk
, ESTABLISHED
)) ||
1489 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1490 err
= -EADDRNOTAVAIL
;
1495 asoc
= sctp_id2assoc(sk
, associd
);
1503 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1505 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1506 * socket that has an association in CLOSED state. This can
1507 * happen when an accepted socket has an association that is
1510 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1515 if (sinfo_flags
& SCTP_EOF
) {
1516 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1518 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1522 if (sinfo_flags
& SCTP_ABORT
) {
1523 struct sctp_chunk
*chunk
;
1525 chunk
= sctp_make_abort_user(asoc
, msg
, msg_len
);
1531 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1532 sctp_primitive_ABORT(asoc
, chunk
);
1538 /* Do we need to create the association? */
1540 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1542 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1547 /* Check for invalid stream against the stream counts,
1548 * either the default or the user specified stream counts.
1551 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1552 /* Check against the defaults. */
1553 if (sinfo
->sinfo_stream
>=
1554 sp
->initmsg
.sinit_num_ostreams
) {
1559 /* Check against the requested. */
1560 if (sinfo
->sinfo_stream
>=
1561 sinit
->sinit_num_ostreams
) {
1569 * API 3.1.2 bind() - UDP Style Syntax
1570 * If a bind() or sctp_bindx() is not called prior to a
1571 * sendmsg() call that initiates a new association, the
1572 * system picks an ephemeral port and will choose an address
1573 * set equivalent to binding with a wildcard address.
1575 if (!ep
->base
.bind_addr
.port
) {
1576 if (sctp_autobind(sk
)) {
1582 * If an unprivileged user inherits a one-to-many
1583 * style socket with open associations on a privileged
1584 * port, it MAY be permitted to accept new associations,
1585 * but it SHOULD NOT be permitted to open new
1588 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1589 !capable(CAP_NET_BIND_SERVICE
)) {
1595 scope
= sctp_scope(&to
);
1596 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1603 /* If the SCTP_INIT ancillary data is specified, set all
1604 * the association init values accordingly.
1607 if (sinit
->sinit_num_ostreams
) {
1608 asoc
->c
.sinit_num_ostreams
=
1609 sinit
->sinit_num_ostreams
;
1611 if (sinit
->sinit_max_instreams
) {
1612 asoc
->c
.sinit_max_instreams
=
1613 sinit
->sinit_max_instreams
;
1615 if (sinit
->sinit_max_attempts
) {
1616 asoc
->max_init_attempts
1617 = sinit
->sinit_max_attempts
;
1619 if (sinit
->sinit_max_init_timeo
) {
1620 asoc
->max_init_timeo
=
1621 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1625 /* Prime the peer's transport structures. */
1626 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1631 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1638 /* ASSERT: we have a valid association at this point. */
1639 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1642 /* If the user didn't specify SNDRCVINFO, make up one with
1645 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1646 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1647 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1648 default_sinfo
.sinfo_context
= asoc
->default_context
;
1649 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1650 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1651 sinfo
= &default_sinfo
;
1654 /* API 7.1.7, the sndbuf size per association bounds the
1655 * maximum size of data that can be sent in a single send call.
1657 if (msg_len
> sk
->sk_sndbuf
) {
1662 /* If fragmentation is disabled and the message length exceeds the
1663 * association fragmentation point, return EMSGSIZE. The I-D
1664 * does not specify what this error is, but this looks like
1667 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1673 /* Check for invalid stream. */
1674 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1680 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1681 if (!sctp_wspace(asoc
)) {
1682 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1687 /* If an address is passed with the sendto/sendmsg call, it is used
1688 * to override the primary destination address in the TCP model, or
1689 * when SCTP_ADDR_OVER flag is set in the UDP model.
1691 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1692 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1693 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1701 /* Auto-connect, if we aren't connected already. */
1702 if (sctp_state(asoc
, CLOSED
)) {
1703 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1706 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1709 /* Break the message into multiple chunks of maximum size. */
1710 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1716 /* Now send the (possibly) fragmented message. */
1717 list_for_each(pos
, &datamsg
->chunks
) {
1718 chunk
= list_entry(pos
, struct sctp_chunk
, frag_list
);
1719 sctp_datamsg_track(chunk
);
1721 /* Do accounting for the write space. */
1722 sctp_set_owner_w(chunk
);
1724 chunk
->transport
= chunk_tp
;
1726 /* Send it to the lower layers. Note: all chunks
1727 * must either fail or succeed. The lower layer
1728 * works that way today. Keep it that way or this
1731 err
= sctp_primitive_SEND(asoc
, chunk
);
1732 /* Did the lower layer accept the chunk? */
1734 sctp_chunk_free(chunk
);
1735 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1738 sctp_datamsg_free(datamsg
);
1744 /* If we are already past ASSOCIATE, the lower
1745 * layers are responsible for association cleanup.
1751 sctp_association_free(asoc
);
1753 sctp_release_sock(sk
);
1756 return sctp_error(sk
, msg_flags
, err
);
1763 err
= sock_error(sk
);
1773 /* This is an extended version of skb_pull() that removes the data from the
1774 * start of a skb even when data is spread across the list of skb's in the
1775 * frag_list. len specifies the total amount of data that needs to be removed.
1776 * when 'len' bytes could be removed from the skb, it returns 0.
1777 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1778 * could not be removed.
1780 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1782 struct sk_buff
*list
;
1783 int skb_len
= skb_headlen(skb
);
1786 if (len
<= skb_len
) {
1787 __skb_pull(skb
, len
);
1791 __skb_pull(skb
, skb_len
);
1793 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
) {
1794 rlen
= sctp_skb_pull(list
, len
);
1795 skb
->len
-= (len
-rlen
);
1796 skb
->data_len
-= (len
-rlen
);
1807 /* API 3.1.3 recvmsg() - UDP Style Syntax
1809 * ssize_t recvmsg(int socket, struct msghdr *message,
1812 * socket - the socket descriptor of the endpoint.
1813 * message - pointer to the msghdr structure which contains a single
1814 * user message and possibly some ancillary data.
1816 * See Section 5 for complete description of the data
1819 * flags - flags sent or received with the user message, see Section
1820 * 5 for complete description of the flags.
1822 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1824 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1825 struct msghdr
*msg
, size_t len
, int noblock
,
1826 int flags
, int *addr_len
)
1828 struct sctp_ulpevent
*event
= NULL
;
1829 struct sctp_sock
*sp
= sctp_sk(sk
);
1830 struct sk_buff
*skb
;
1835 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1836 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1837 "len", len
, "knoblauch", noblock
,
1838 "flags", flags
, "addr_len", addr_len
);
1842 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1847 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1851 /* Get the total length of the skb including any skb's in the
1860 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1862 event
= sctp_skb2event(skb
);
1867 sock_recv_timestamp(msg
, sk
, skb
);
1868 if (sctp_ulpevent_is_notification(event
)) {
1869 msg
->msg_flags
|= MSG_NOTIFICATION
;
1870 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1872 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1875 /* Check if we allow SCTP_SNDRCVINFO. */
1876 if (sp
->subscribe
.sctp_data_io_event
)
1877 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1879 /* FIXME: we should be calling IP/IPv6 layers. */
1880 if (sk
->sk_protinfo
.af_inet
.cmsg_flags
)
1881 ip_cmsg_recv(msg
, skb
);
1886 /* If skb's length exceeds the user's buffer, update the skb and
1887 * push it back to the receive_queue so that the next call to
1888 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1890 if (skb_len
> copied
) {
1891 msg
->msg_flags
&= ~MSG_EOR
;
1892 if (flags
& MSG_PEEK
)
1894 sctp_skb_pull(skb
, copied
);
1895 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1897 /* When only partial message is copied to the user, increase
1898 * rwnd by that amount. If all the data in the skb is read,
1899 * rwnd is updated when the event is freed.
1901 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1903 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1904 (event
->msg_flags
& MSG_EOR
))
1905 msg
->msg_flags
|= MSG_EOR
;
1907 msg
->msg_flags
&= ~MSG_EOR
;
1910 if (flags
& MSG_PEEK
) {
1911 /* Release the skb reference acquired after peeking the skb in
1912 * sctp_skb_recv_datagram().
1916 /* Free the event which includes releasing the reference to
1917 * the owner of the skb, freeing the skb and updating the
1920 sctp_ulpevent_free(event
);
1923 sctp_release_sock(sk
);
1927 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1929 * This option is a on/off flag. If enabled no SCTP message
1930 * fragmentation will be performed. Instead if a message being sent
1931 * exceeds the current PMTU size, the message will NOT be sent and
1932 * instead a error will be indicated to the user.
1934 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1935 char __user
*optval
, int optlen
)
1939 if (optlen
< sizeof(int))
1942 if (get_user(val
, (int __user
*)optval
))
1945 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1950 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1953 if (optlen
!= sizeof(struct sctp_event_subscribe
))
1955 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
1960 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1962 * This socket option is applicable to the UDP-style socket only. When
1963 * set it will cause associations that are idle for more than the
1964 * specified number of seconds to automatically close. An association
1965 * being idle is defined an association that has NOT sent or received
1966 * user data. The special value of '0' indicates that no automatic
1967 * close of any associations should be performed. The option expects an
1968 * integer defining the number of seconds of idle time before an
1969 * association is closed.
1971 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
1974 struct sctp_sock
*sp
= sctp_sk(sk
);
1976 /* Applicable to UDP-style socket only */
1977 if (sctp_style(sk
, TCP
))
1979 if (optlen
!= sizeof(int))
1981 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
1987 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1989 * Applications can enable or disable heartbeats for any peer address of
1990 * an association, modify an address's heartbeat interval, force a
1991 * heartbeat to be sent immediately, and adjust the address's maximum
1992 * number of retransmissions sent before an address is considered
1993 * unreachable. The following structure is used to access and modify an
1994 * address's parameters:
1996 * struct sctp_paddrparams {
1997 * sctp_assoc_t spp_assoc_id;
1998 * struct sockaddr_storage spp_address;
1999 * uint32_t spp_hbinterval;
2000 * uint16_t spp_pathmaxrxt;
2001 * uint32_t spp_pathmtu;
2002 * uint32_t spp_sackdelay;
2003 * uint32_t spp_flags;
2006 * spp_assoc_id - (one-to-many style socket) This is filled in the
2007 * application, and identifies the association for
2009 * spp_address - This specifies which address is of interest.
2010 * spp_hbinterval - This contains the value of the heartbeat interval,
2011 * in milliseconds. If a value of zero
2012 * is present in this field then no changes are to
2013 * be made to this parameter.
2014 * spp_pathmaxrxt - This contains the maximum number of
2015 * retransmissions before this address shall be
2016 * considered unreachable. If a value of zero
2017 * is present in this field then no changes are to
2018 * be made to this parameter.
2019 * spp_pathmtu - When Path MTU discovery is disabled the value
2020 * specified here will be the "fixed" path mtu.
2021 * Note that if the spp_address field is empty
2022 * then all associations on this address will
2023 * have this fixed path mtu set upon them.
2025 * spp_sackdelay - When delayed sack is enabled, this value specifies
2026 * the number of milliseconds that sacks will be delayed
2027 * for. This value will apply to all addresses of an
2028 * association if the spp_address field is empty. Note
2029 * also, that if delayed sack is enabled and this
2030 * value is set to 0, no change is made to the last
2031 * recorded delayed sack timer value.
2033 * spp_flags - These flags are used to control various features
2034 * on an association. The flag field may contain
2035 * zero or more of the following options.
2037 * SPP_HB_ENABLE - Enable heartbeats on the
2038 * specified address. Note that if the address
2039 * field is empty all addresses for the association
2040 * have heartbeats enabled upon them.
2042 * SPP_HB_DISABLE - Disable heartbeats on the
2043 * speicifed address. Note that if the address
2044 * field is empty all addresses for the association
2045 * will have their heartbeats disabled. Note also
2046 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2047 * mutually exclusive, only one of these two should
2048 * be specified. Enabling both fields will have
2049 * undetermined results.
2051 * SPP_HB_DEMAND - Request a user initiated heartbeat
2052 * to be made immediately.
2054 * SPP_PMTUD_ENABLE - This field will enable PMTU
2055 * discovery upon the specified address. Note that
2056 * if the address feild is empty then all addresses
2057 * on the association are effected.
2059 * SPP_PMTUD_DISABLE - This field will disable PMTU
2060 * discovery upon the specified address. Note that
2061 * if the address feild is empty then all addresses
2062 * on the association are effected. Not also that
2063 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2064 * exclusive. Enabling both will have undetermined
2067 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2068 * on delayed sack. The time specified in spp_sackdelay
2069 * is used to specify the sack delay for this address. Note
2070 * that if spp_address is empty then all addresses will
2071 * enable delayed sack and take on the sack delay
2072 * value specified in spp_sackdelay.
2073 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2074 * off delayed sack. If the spp_address field is blank then
2075 * delayed sack is disabled for the entire association. Note
2076 * also that this field is mutually exclusive to
2077 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2080 int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2081 struct sctp_transport
*trans
,
2082 struct sctp_association
*asoc
,
2083 struct sctp_sock
*sp
,
2086 int sackdelay_change
)
2090 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2091 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2096 if (params
->spp_hbinterval
) {
2098 trans
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2100 asoc
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2102 sp
->hbinterval
= params
->spp_hbinterval
;
2108 trans
->param_flags
=
2109 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2112 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2115 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2119 if (params
->spp_pathmtu
) {
2121 trans
->pathmtu
= params
->spp_pathmtu
;
2122 sctp_assoc_sync_pmtu(asoc
);
2124 asoc
->pathmtu
= params
->spp_pathmtu
;
2125 sctp_frag_point(sp
, params
->spp_pathmtu
);
2127 sp
->pathmtu
= params
->spp_pathmtu
;
2133 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2134 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2135 trans
->param_flags
=
2136 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2138 sctp_transport_pmtu(trans
);
2139 sctp_assoc_sync_pmtu(asoc
);
2143 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2146 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2150 if (params
->spp_sackdelay
) {
2153 msecs_to_jiffies(params
->spp_sackdelay
);
2156 msecs_to_jiffies(params
->spp_sackdelay
);
2158 sp
->sackdelay
= params
->spp_sackdelay
;
2162 if (sackdelay_change
) {
2164 trans
->param_flags
=
2165 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2169 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2173 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2178 if (params
->spp_pathmaxrxt
) {
2180 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2182 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2184 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2191 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2192 char __user
*optval
, int optlen
)
2194 struct sctp_paddrparams params
;
2195 struct sctp_transport
*trans
= NULL
;
2196 struct sctp_association
*asoc
= NULL
;
2197 struct sctp_sock
*sp
= sctp_sk(sk
);
2199 int hb_change
, pmtud_change
, sackdelay_change
;
2201 if (optlen
!= sizeof(struct sctp_paddrparams
))
2204 if (copy_from_user(¶ms
, optval
, optlen
))
2207 /* Validate flags and value parameters. */
2208 hb_change
= params
.spp_flags
& SPP_HB
;
2209 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2210 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2212 if (hb_change
== SPP_HB
||
2213 pmtud_change
== SPP_PMTUD
||
2214 sackdelay_change
== SPP_SACKDELAY
||
2215 params
.spp_sackdelay
> 500 ||
2217 && params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2220 /* If an address other than INADDR_ANY is specified, and
2221 * no transport is found, then the request is invalid.
2223 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
2224 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2225 params
.spp_assoc_id
);
2230 /* Get association, if assoc_id != 0 and the socket is a one
2231 * to many style socket, and an association was not found, then
2232 * the id was invalid.
2234 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2235 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2238 /* Heartbeat demand can only be sent on a transport or
2239 * association, but not a socket.
2241 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2244 /* Process parameters. */
2245 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2246 hb_change
, pmtud_change
,
2252 /* If changes are for association, also apply parameters to each
2255 if (!trans
&& asoc
) {
2256 struct list_head
*pos
;
2258 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2259 trans
= list_entry(pos
, struct sctp_transport
,
2261 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2262 hb_change
, pmtud_change
,
2270 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2272 * This options will get or set the delayed ack timer. The time is set
2273 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2274 * endpoints default delayed ack timer value. If the assoc_id field is
2275 * non-zero, then the set or get effects the specified association.
2277 * struct sctp_assoc_value {
2278 * sctp_assoc_t assoc_id;
2279 * uint32_t assoc_value;
2282 * assoc_id - This parameter, indicates which association the
2283 * user is preforming an action upon. Note that if
2284 * this field's value is zero then the endpoints
2285 * default value is changed (effecting future
2286 * associations only).
2288 * assoc_value - This parameter contains the number of milliseconds
2289 * that the user is requesting the delayed ACK timer
2290 * be set to. Note that this value is defined in
2291 * the standard to be between 200 and 500 milliseconds.
2293 * Note: a value of zero will leave the value alone,
2294 * but disable SACK delay. A non-zero value will also
2295 * enable SACK delay.
2298 static int sctp_setsockopt_delayed_ack_time(struct sock
*sk
,
2299 char __user
*optval
, int optlen
)
2301 struct sctp_assoc_value params
;
2302 struct sctp_transport
*trans
= NULL
;
2303 struct sctp_association
*asoc
= NULL
;
2304 struct sctp_sock
*sp
= sctp_sk(sk
);
2306 if (optlen
!= sizeof(struct sctp_assoc_value
))
2309 if (copy_from_user(¶ms
, optval
, optlen
))
2312 /* Validate value parameter. */
2313 if (params
.assoc_value
> 500)
2316 /* Get association, if assoc_id != 0 and the socket is a one
2317 * to many style socket, and an association was not found, then
2318 * the id was invalid.
2320 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2321 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2324 if (params
.assoc_value
) {
2327 msecs_to_jiffies(params
.assoc_value
);
2329 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2330 SPP_SACKDELAY_ENABLE
;
2332 sp
->sackdelay
= params
.assoc_value
;
2334 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2335 SPP_SACKDELAY_ENABLE
;
2340 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2341 SPP_SACKDELAY_DISABLE
;
2344 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2345 SPP_SACKDELAY_DISABLE
;
2349 /* If change is for association, also apply to each transport. */
2351 struct list_head
*pos
;
2353 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2354 trans
= list_entry(pos
, struct sctp_transport
,
2356 if (params
.assoc_value
) {
2358 msecs_to_jiffies(params
.assoc_value
);
2359 trans
->param_flags
=
2360 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2361 SPP_SACKDELAY_ENABLE
;
2363 trans
->param_flags
=
2364 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2365 SPP_SACKDELAY_DISABLE
;
2373 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2375 * Applications can specify protocol parameters for the default association
2376 * initialization. The option name argument to setsockopt() and getsockopt()
2379 * Setting initialization parameters is effective only on an unconnected
2380 * socket (for UDP-style sockets only future associations are effected
2381 * by the change). With TCP-style sockets, this option is inherited by
2382 * sockets derived from a listener socket.
2384 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, int optlen
)
2386 struct sctp_initmsg sinit
;
2387 struct sctp_sock
*sp
= sctp_sk(sk
);
2389 if (optlen
!= sizeof(struct sctp_initmsg
))
2391 if (copy_from_user(&sinit
, optval
, optlen
))
2394 if (sinit
.sinit_num_ostreams
)
2395 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2396 if (sinit
.sinit_max_instreams
)
2397 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2398 if (sinit
.sinit_max_attempts
)
2399 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2400 if (sinit
.sinit_max_init_timeo
)
2401 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2407 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2409 * Applications that wish to use the sendto() system call may wish to
2410 * specify a default set of parameters that would normally be supplied
2411 * through the inclusion of ancillary data. This socket option allows
2412 * such an application to set the default sctp_sndrcvinfo structure.
2413 * The application that wishes to use this socket option simply passes
2414 * in to this call the sctp_sndrcvinfo structure defined in Section
2415 * 5.2.2) The input parameters accepted by this call include
2416 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2417 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2418 * to this call if the caller is using the UDP model.
2420 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2421 char __user
*optval
, int optlen
)
2423 struct sctp_sndrcvinfo info
;
2424 struct sctp_association
*asoc
;
2425 struct sctp_sock
*sp
= sctp_sk(sk
);
2427 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2429 if (copy_from_user(&info
, optval
, optlen
))
2432 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2433 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2437 asoc
->default_stream
= info
.sinfo_stream
;
2438 asoc
->default_flags
= info
.sinfo_flags
;
2439 asoc
->default_ppid
= info
.sinfo_ppid
;
2440 asoc
->default_context
= info
.sinfo_context
;
2441 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2443 sp
->default_stream
= info
.sinfo_stream
;
2444 sp
->default_flags
= info
.sinfo_flags
;
2445 sp
->default_ppid
= info
.sinfo_ppid
;
2446 sp
->default_context
= info
.sinfo_context
;
2447 sp
->default_timetolive
= info
.sinfo_timetolive
;
2453 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2455 * Requests that the local SCTP stack use the enclosed peer address as
2456 * the association primary. The enclosed address must be one of the
2457 * association peer's addresses.
2459 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2462 struct sctp_prim prim
;
2463 struct sctp_transport
*trans
;
2465 if (optlen
!= sizeof(struct sctp_prim
))
2468 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2471 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2475 sctp_assoc_set_primary(trans
->asoc
, trans
);
2481 * 7.1.5 SCTP_NODELAY
2483 * Turn on/off any Nagle-like algorithm. This means that packets are
2484 * generally sent as soon as possible and no unnecessary delays are
2485 * introduced, at the cost of more packets in the network. Expects an
2486 * integer boolean flag.
2488 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2493 if (optlen
< sizeof(int))
2495 if (get_user(val
, (int __user
*)optval
))
2498 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2504 * 7.1.1 SCTP_RTOINFO
2506 * The protocol parameters used to initialize and bound retransmission
2507 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2508 * and modify these parameters.
2509 * All parameters are time values, in milliseconds. A value of 0, when
2510 * modifying the parameters, indicates that the current value should not
2514 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, int optlen
) {
2515 struct sctp_rtoinfo rtoinfo
;
2516 struct sctp_association
*asoc
;
2518 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2521 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2524 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2526 /* Set the values to the specific association */
2527 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2531 if (rtoinfo
.srto_initial
!= 0)
2533 msecs_to_jiffies(rtoinfo
.srto_initial
);
2534 if (rtoinfo
.srto_max
!= 0)
2535 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2536 if (rtoinfo
.srto_min
!= 0)
2537 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2539 /* If there is no association or the association-id = 0
2540 * set the values to the endpoint.
2542 struct sctp_sock
*sp
= sctp_sk(sk
);
2544 if (rtoinfo
.srto_initial
!= 0)
2545 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2546 if (rtoinfo
.srto_max
!= 0)
2547 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2548 if (rtoinfo
.srto_min
!= 0)
2549 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2557 * 7.1.2 SCTP_ASSOCINFO
2559 * This option is used to tune the the maximum retransmission attempts
2560 * of the association.
2561 * Returns an error if the new association retransmission value is
2562 * greater than the sum of the retransmission value of the peer.
2563 * See [SCTP] for more information.
2566 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, int optlen
)
2569 struct sctp_assocparams assocparams
;
2570 struct sctp_association
*asoc
;
2572 if (optlen
!= sizeof(struct sctp_assocparams
))
2574 if (copy_from_user(&assocparams
, optval
, optlen
))
2577 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2579 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2582 /* Set the values to the specific association */
2584 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2587 struct list_head
*pos
;
2588 struct sctp_transport
*peer_addr
;
2590 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2591 peer_addr
= list_entry(pos
,
2592 struct sctp_transport
,
2594 path_sum
+= peer_addr
->pathmaxrxt
;
2598 /* Only validate asocmaxrxt if we have more then
2599 * one path/transport. We do this because path
2600 * retransmissions are only counted when we have more
2604 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2607 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2610 if (assocparams
.sasoc_cookie_life
!= 0) {
2611 asoc
->cookie_life
.tv_sec
=
2612 assocparams
.sasoc_cookie_life
/ 1000;
2613 asoc
->cookie_life
.tv_usec
=
2614 (assocparams
.sasoc_cookie_life
% 1000)
2618 /* Set the values to the endpoint */
2619 struct sctp_sock
*sp
= sctp_sk(sk
);
2621 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2622 sp
->assocparams
.sasoc_asocmaxrxt
=
2623 assocparams
.sasoc_asocmaxrxt
;
2624 if (assocparams
.sasoc_cookie_life
!= 0)
2625 sp
->assocparams
.sasoc_cookie_life
=
2626 assocparams
.sasoc_cookie_life
;
2632 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2634 * This socket option is a boolean flag which turns on or off mapped V4
2635 * addresses. If this option is turned on and the socket is type
2636 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2637 * If this option is turned off, then no mapping will be done of V4
2638 * addresses and a user will receive both PF_INET6 and PF_INET type
2639 * addresses on the socket.
2641 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, int optlen
)
2644 struct sctp_sock
*sp
= sctp_sk(sk
);
2646 if (optlen
< sizeof(int))
2648 if (get_user(val
, (int __user
*)optval
))
2659 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2661 * This socket option specifies the maximum size to put in any outgoing
2662 * SCTP chunk. If a message is larger than this size it will be
2663 * fragmented by SCTP into the specified size. Note that the underlying
2664 * SCTP implementation may fragment into smaller sized chunks when the
2665 * PMTU of the underlying association is smaller than the value set by
2668 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, int optlen
)
2670 struct sctp_association
*asoc
;
2671 struct list_head
*pos
;
2672 struct sctp_sock
*sp
= sctp_sk(sk
);
2675 if (optlen
< sizeof(int))
2677 if (get_user(val
, (int __user
*)optval
))
2679 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2681 sp
->user_frag
= val
;
2683 /* Update the frag_point of the existing associations. */
2684 list_for_each(pos
, &(sp
->ep
->asocs
)) {
2685 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
2686 asoc
->frag_point
= sctp_frag_point(sp
, asoc
->pathmtu
);
2694 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2696 * Requests that the peer mark the enclosed address as the association
2697 * primary. The enclosed address must be one of the association's
2698 * locally bound addresses. The following structure is used to make a
2699 * set primary request:
2701 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2704 struct sctp_sock
*sp
;
2705 struct sctp_endpoint
*ep
;
2706 struct sctp_association
*asoc
= NULL
;
2707 struct sctp_setpeerprim prim
;
2708 struct sctp_chunk
*chunk
;
2714 if (!sctp_addip_enable
)
2717 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2720 if (copy_from_user(&prim
, optval
, optlen
))
2723 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2727 if (!asoc
->peer
.asconf_capable
)
2730 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2733 if (!sctp_state(asoc
, ESTABLISHED
))
2736 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2737 return -EADDRNOTAVAIL
;
2739 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2740 chunk
= sctp_make_asconf_set_prim(asoc
,
2741 (union sctp_addr
*)&prim
.sspp_addr
);
2745 err
= sctp_send_asconf(asoc
, chunk
);
2747 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2752 static int sctp_setsockopt_adaption_layer(struct sock
*sk
, char __user
*optval
,
2755 struct sctp_setadaption adaption
;
2757 if (optlen
!= sizeof(struct sctp_setadaption
))
2759 if (copy_from_user(&adaption
, optval
, optlen
))
2762 sctp_sk(sk
)->adaption_ind
= adaption
.ssb_adaption_ind
;
2767 /* API 6.2 setsockopt(), getsockopt()
2769 * Applications use setsockopt() and getsockopt() to set or retrieve
2770 * socket options. Socket options are used to change the default
2771 * behavior of sockets calls. They are described in Section 7.
2775 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2776 * int __user *optlen);
2777 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2780 * sd - the socket descript.
2781 * level - set to IPPROTO_SCTP for all SCTP options.
2782 * optname - the option name.
2783 * optval - the buffer to store the value of the option.
2784 * optlen - the size of the buffer.
2786 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
2787 char __user
*optval
, int optlen
)
2791 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2794 /* I can hardly begin to describe how wrong this is. This is
2795 * so broken as to be worse than useless. The API draft
2796 * REALLY is NOT helpful here... I am not convinced that the
2797 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2798 * are at all well-founded.
2800 if (level
!= SOL_SCTP
) {
2801 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
2802 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
2809 case SCTP_SOCKOPT_BINDX_ADD
:
2810 /* 'optlen' is the size of the addresses buffer. */
2811 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2812 optlen
, SCTP_BINDX_ADD_ADDR
);
2815 case SCTP_SOCKOPT_BINDX_REM
:
2816 /* 'optlen' is the size of the addresses buffer. */
2817 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2818 optlen
, SCTP_BINDX_REM_ADDR
);
2821 case SCTP_SOCKOPT_CONNECTX
:
2822 /* 'optlen' is the size of the addresses buffer. */
2823 retval
= sctp_setsockopt_connectx(sk
, (struct sockaddr __user
*)optval
,
2827 case SCTP_DISABLE_FRAGMENTS
:
2828 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
2832 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
2835 case SCTP_AUTOCLOSE
:
2836 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
2839 case SCTP_PEER_ADDR_PARAMS
:
2840 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
2843 case SCTP_DELAYED_ACK_TIME
:
2844 retval
= sctp_setsockopt_delayed_ack_time(sk
, optval
, optlen
);
2848 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
2850 case SCTP_DEFAULT_SEND_PARAM
:
2851 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
2854 case SCTP_PRIMARY_ADDR
:
2855 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
2857 case SCTP_SET_PEER_PRIMARY_ADDR
:
2858 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
2861 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
2864 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
2866 case SCTP_ASSOCINFO
:
2867 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
2869 case SCTP_I_WANT_MAPPED_V4_ADDR
:
2870 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
2873 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
2875 case SCTP_ADAPTION_LAYER
:
2876 retval
= sctp_setsockopt_adaption_layer(sk
, optval
, optlen
);
2880 retval
= -ENOPROTOOPT
;
2884 sctp_release_sock(sk
);
2890 /* API 3.1.6 connect() - UDP Style Syntax
2892 * An application may use the connect() call in the UDP model to initiate an
2893 * association without sending data.
2897 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2899 * sd: the socket descriptor to have a new association added to.
2901 * nam: the address structure (either struct sockaddr_in or struct
2902 * sockaddr_in6 defined in RFC2553 [7]).
2904 * len: the size of the address.
2906 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
2914 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2915 __FUNCTION__
, sk
, addr
, addr_len
);
2917 /* Validate addr_len before calling common connect/connectx routine. */
2918 af
= sctp_get_af_specific(addr
->sa_family
);
2919 if (!af
|| addr_len
< af
->sockaddr_len
) {
2922 /* Pass correct addr len to common routine (so it knows there
2923 * is only one address being passed.
2925 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
);
2928 sctp_release_sock(sk
);
2932 /* FIXME: Write comments. */
2933 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
2935 return -EOPNOTSUPP
; /* STUB */
2938 /* 4.1.4 accept() - TCP Style Syntax
2940 * Applications use accept() call to remove an established SCTP
2941 * association from the accept queue of the endpoint. A new socket
2942 * descriptor will be returned from accept() to represent the newly
2943 * formed association.
2945 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
2947 struct sctp_sock
*sp
;
2948 struct sctp_endpoint
*ep
;
2949 struct sock
*newsk
= NULL
;
2950 struct sctp_association
*asoc
;
2959 if (!sctp_style(sk
, TCP
)) {
2960 error
= -EOPNOTSUPP
;
2964 if (!sctp_sstate(sk
, LISTENING
)) {
2969 timeo
= sock_rcvtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
2971 error
= sctp_wait_for_accept(sk
, timeo
);
2975 /* We treat the list of associations on the endpoint as the accept
2976 * queue and pick the first association on the list.
2978 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
2980 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
2986 /* Populate the fields of the newsk from the oldsk and migrate the
2987 * asoc to the newsk.
2989 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
2992 sctp_release_sock(sk
);
2997 /* The SCTP ioctl handler. */
2998 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
3000 return -ENOIOCTLCMD
;
3003 /* This is the function which gets called during socket creation to
3004 * initialized the SCTP-specific portion of the sock.
3005 * The sock structure should already be zero-filled memory.
3007 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
3009 struct sctp_endpoint
*ep
;
3010 struct sctp_sock
*sp
;
3012 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
3016 /* Initialize the SCTP per socket area. */
3017 switch (sk
->sk_type
) {
3018 case SOCK_SEQPACKET
:
3019 sp
->type
= SCTP_SOCKET_UDP
;
3022 sp
->type
= SCTP_SOCKET_TCP
;
3025 return -ESOCKTNOSUPPORT
;
3028 /* Initialize default send parameters. These parameters can be
3029 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3031 sp
->default_stream
= 0;
3032 sp
->default_ppid
= 0;
3033 sp
->default_flags
= 0;
3034 sp
->default_context
= 0;
3035 sp
->default_timetolive
= 0;
3037 /* Initialize default setup parameters. These parameters
3038 * can be modified with the SCTP_INITMSG socket option or
3039 * overridden by the SCTP_INIT CMSG.
3041 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
3042 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
3043 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
3044 sp
->initmsg
.sinit_max_init_timeo
= jiffies_to_msecs(sctp_rto_max
);
3046 /* Initialize default RTO related parameters. These parameters can
3047 * be modified for with the SCTP_RTOINFO socket option.
3049 sp
->rtoinfo
.srto_initial
= jiffies_to_msecs(sctp_rto_initial
);
3050 sp
->rtoinfo
.srto_max
= jiffies_to_msecs(sctp_rto_max
);
3051 sp
->rtoinfo
.srto_min
= jiffies_to_msecs(sctp_rto_min
);
3053 /* Initialize default association related parameters. These parameters
3054 * can be modified with the SCTP_ASSOCINFO socket option.
3056 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3057 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3058 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3059 sp
->assocparams
.sasoc_local_rwnd
= 0;
3060 sp
->assocparams
.sasoc_cookie_life
=
3061 jiffies_to_msecs(sctp_valid_cookie_life
);
3063 /* Initialize default event subscriptions. By default, all the
3066 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3068 /* Default Peer Address Parameters. These defaults can
3069 * be modified via SCTP_PEER_ADDR_PARAMS
3071 sp
->hbinterval
= jiffies_to_msecs(sctp_hb_interval
);
3072 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3073 sp
->pathmtu
= 0; // allow default discovery
3074 sp
->sackdelay
= jiffies_to_msecs(sctp_sack_timeout
);
3075 sp
->param_flags
= SPP_HB_ENABLE
|
3077 SPP_SACKDELAY_ENABLE
;
3079 /* If enabled no SCTP message fragmentation will be performed.
3080 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3082 sp
->disable_fragments
= 0;
3084 /* Turn on/off any Nagle-like algorithm. */
3087 /* Enable by default. */
3090 /* Auto-close idle associations after the configured
3091 * number of seconds. A value of 0 disables this
3092 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3093 * for UDP-style sockets only.
3097 /* User specified fragmentation limit. */
3100 sp
->adaption_ind
= 0;
3102 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3104 /* Control variables for partial data delivery. */
3106 skb_queue_head_init(&sp
->pd_lobby
);
3108 /* Create a per socket endpoint structure. Even if we
3109 * change the data structure relationships, this may still
3110 * be useful for storing pre-connect address information.
3112 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3119 SCTP_DBG_OBJCNT_INC(sock
);
3123 /* Cleanup any SCTP per socket resources. */
3124 SCTP_STATIC
int sctp_destroy_sock(struct sock
*sk
)
3126 struct sctp_endpoint
*ep
;
3128 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3130 /* Release our hold on the endpoint. */
3131 ep
= sctp_sk(sk
)->ep
;
3132 sctp_endpoint_free(ep
);
3137 /* API 4.1.7 shutdown() - TCP Style Syntax
3138 * int shutdown(int socket, int how);
3140 * sd - the socket descriptor of the association to be closed.
3141 * how - Specifies the type of shutdown. The values are
3144 * Disables further receive operations. No SCTP
3145 * protocol action is taken.
3147 * Disables further send operations, and initiates
3148 * the SCTP shutdown sequence.
3150 * Disables further send and receive operations
3151 * and initiates the SCTP shutdown sequence.
3153 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3155 struct sctp_endpoint
*ep
;
3156 struct sctp_association
*asoc
;
3158 if (!sctp_style(sk
, TCP
))
3161 if (how
& SEND_SHUTDOWN
) {
3162 ep
= sctp_sk(sk
)->ep
;
3163 if (!list_empty(&ep
->asocs
)) {
3164 asoc
= list_entry(ep
->asocs
.next
,
3165 struct sctp_association
, asocs
);
3166 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3171 /* 7.2.1 Association Status (SCTP_STATUS)
3173 * Applications can retrieve current status information about an
3174 * association, including association state, peer receiver window size,
3175 * number of unacked data chunks, and number of data chunks pending
3176 * receipt. This information is read-only.
3178 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3179 char __user
*optval
,
3182 struct sctp_status status
;
3183 struct sctp_association
*asoc
= NULL
;
3184 struct sctp_transport
*transport
;
3185 sctp_assoc_t associd
;
3188 if (len
!= sizeof(status
)) {
3193 if (copy_from_user(&status
, optval
, sizeof(status
))) {
3198 associd
= status
.sstat_assoc_id
;
3199 asoc
= sctp_id2assoc(sk
, associd
);
3205 transport
= asoc
->peer
.primary_path
;
3207 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3208 status
.sstat_state
= asoc
->state
;
3209 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3210 status
.sstat_unackdata
= asoc
->unack_data
;
3212 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3213 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3214 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3215 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3216 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3217 memcpy(&status
.sstat_primary
.spinfo_address
,
3218 &(transport
->ipaddr
), sizeof(union sctp_addr
));
3219 /* Map ipv4 address into v4-mapped-on-v6 address. */
3220 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3221 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3222 status
.sstat_primary
.spinfo_state
= transport
->state
;
3223 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3224 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3225 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3226 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3228 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3229 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3231 if (put_user(len
, optlen
)) {
3236 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3237 len
, status
.sstat_state
, status
.sstat_rwnd
,
3238 status
.sstat_assoc_id
);
3240 if (copy_to_user(optval
, &status
, len
)) {
3250 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3252 * Applications can retrieve information about a specific peer address
3253 * of an association, including its reachability state, congestion
3254 * window, and retransmission timer values. This information is
3257 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3258 char __user
*optval
,
3261 struct sctp_paddrinfo pinfo
;
3262 struct sctp_transport
*transport
;
3265 if (len
!= sizeof(pinfo
)) {
3270 if (copy_from_user(&pinfo
, optval
, sizeof(pinfo
))) {
3275 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3276 pinfo
.spinfo_assoc_id
);
3280 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3281 pinfo
.spinfo_state
= transport
->state
;
3282 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3283 pinfo
.spinfo_srtt
= transport
->srtt
;
3284 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3285 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3287 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3288 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3290 if (put_user(len
, optlen
)) {
3295 if (copy_to_user(optval
, &pinfo
, len
)) {
3304 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3306 * This option is a on/off flag. If enabled no SCTP message
3307 * fragmentation will be performed. Instead if a message being sent
3308 * exceeds the current PMTU size, the message will NOT be sent and
3309 * instead a error will be indicated to the user.
3311 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3312 char __user
*optval
, int __user
*optlen
)
3316 if (len
< sizeof(int))
3320 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3321 if (put_user(len
, optlen
))
3323 if (copy_to_user(optval
, &val
, len
))
3328 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3330 * This socket option is used to specify various notifications and
3331 * ancillary data the user wishes to receive.
3333 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3336 if (len
!= sizeof(struct sctp_event_subscribe
))
3338 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3343 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3345 * This socket option is applicable to the UDP-style socket only. When
3346 * set it will cause associations that are idle for more than the
3347 * specified number of seconds to automatically close. An association
3348 * being idle is defined an association that has NOT sent or received
3349 * user data. The special value of '0' indicates that no automatic
3350 * close of any associations should be performed. The option expects an
3351 * integer defining the number of seconds of idle time before an
3352 * association is closed.
3354 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3356 /* Applicable to UDP-style socket only */
3357 if (sctp_style(sk
, TCP
))
3359 if (len
!= sizeof(int))
3361 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, len
))
3366 /* Helper routine to branch off an association to a new socket. */
3367 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3368 struct socket
**sockp
)
3370 struct sock
*sk
= asoc
->base
.sk
;
3371 struct socket
*sock
;
3374 /* An association cannot be branched off from an already peeled-off
3375 * socket, nor is this supported for tcp style sockets.
3377 if (!sctp_style(sk
, UDP
))
3380 /* Create a new socket. */
3381 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3385 /* Populate the fields of the newsk from the oldsk and migrate the
3386 * asoc to the newsk.
3388 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3394 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3396 sctp_peeloff_arg_t peeloff
;
3397 struct socket
*newsock
;
3399 struct sctp_association
*asoc
;
3401 if (len
!= sizeof(sctp_peeloff_arg_t
))
3403 if (copy_from_user(&peeloff
, optval
, len
))
3406 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3412 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__
, sk
, asoc
);
3414 retval
= sctp_do_peeloff(asoc
, &newsock
);
3418 /* Map the socket to an unused fd that can be returned to the user. */
3419 retval
= sock_map_fd(newsock
);
3421 sock_release(newsock
);
3425 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3426 __FUNCTION__
, sk
, asoc
, newsock
->sk
, retval
);
3428 /* Return the fd mapped to the new socket. */
3429 peeloff
.sd
= retval
;
3430 if (copy_to_user(optval
, &peeloff
, len
))
3437 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3439 * Applications can enable or disable heartbeats for any peer address of
3440 * an association, modify an address's heartbeat interval, force a
3441 * heartbeat to be sent immediately, and adjust the address's maximum
3442 * number of retransmissions sent before an address is considered
3443 * unreachable. The following structure is used to access and modify an
3444 * address's parameters:
3446 * struct sctp_paddrparams {
3447 * sctp_assoc_t spp_assoc_id;
3448 * struct sockaddr_storage spp_address;
3449 * uint32_t spp_hbinterval;
3450 * uint16_t spp_pathmaxrxt;
3451 * uint32_t spp_pathmtu;
3452 * uint32_t spp_sackdelay;
3453 * uint32_t spp_flags;
3456 * spp_assoc_id - (one-to-many style socket) This is filled in the
3457 * application, and identifies the association for
3459 * spp_address - This specifies which address is of interest.
3460 * spp_hbinterval - This contains the value of the heartbeat interval,
3461 * in milliseconds. If a value of zero
3462 * is present in this field then no changes are to
3463 * be made to this parameter.
3464 * spp_pathmaxrxt - This contains the maximum number of
3465 * retransmissions before this address shall be
3466 * considered unreachable. If a value of zero
3467 * is present in this field then no changes are to
3468 * be made to this parameter.
3469 * spp_pathmtu - When Path MTU discovery is disabled the value
3470 * specified here will be the "fixed" path mtu.
3471 * Note that if the spp_address field is empty
3472 * then all associations on this address will
3473 * have this fixed path mtu set upon them.
3475 * spp_sackdelay - When delayed sack is enabled, this value specifies
3476 * the number of milliseconds that sacks will be delayed
3477 * for. This value will apply to all addresses of an
3478 * association if the spp_address field is empty. Note
3479 * also, that if delayed sack is enabled and this
3480 * value is set to 0, no change is made to the last
3481 * recorded delayed sack timer value.
3483 * spp_flags - These flags are used to control various features
3484 * on an association. The flag field may contain
3485 * zero or more of the following options.
3487 * SPP_HB_ENABLE - Enable heartbeats on the
3488 * specified address. Note that if the address
3489 * field is empty all addresses for the association
3490 * have heartbeats enabled upon them.
3492 * SPP_HB_DISABLE - Disable heartbeats on the
3493 * speicifed address. Note that if the address
3494 * field is empty all addresses for the association
3495 * will have their heartbeats disabled. Note also
3496 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3497 * mutually exclusive, only one of these two should
3498 * be specified. Enabling both fields will have
3499 * undetermined results.
3501 * SPP_HB_DEMAND - Request a user initiated heartbeat
3502 * to be made immediately.
3504 * SPP_PMTUD_ENABLE - This field will enable PMTU
3505 * discovery upon the specified address. Note that
3506 * if the address feild is empty then all addresses
3507 * on the association are effected.
3509 * SPP_PMTUD_DISABLE - This field will disable PMTU
3510 * discovery upon the specified address. Note that
3511 * if the address feild is empty then all addresses
3512 * on the association are effected. Not also that
3513 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3514 * exclusive. Enabling both will have undetermined
3517 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3518 * on delayed sack. The time specified in spp_sackdelay
3519 * is used to specify the sack delay for this address. Note
3520 * that if spp_address is empty then all addresses will
3521 * enable delayed sack and take on the sack delay
3522 * value specified in spp_sackdelay.
3523 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3524 * off delayed sack. If the spp_address field is blank then
3525 * delayed sack is disabled for the entire association. Note
3526 * also that this field is mutually exclusive to
3527 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3530 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
3531 char __user
*optval
, int __user
*optlen
)
3533 struct sctp_paddrparams params
;
3534 struct sctp_transport
*trans
= NULL
;
3535 struct sctp_association
*asoc
= NULL
;
3536 struct sctp_sock
*sp
= sctp_sk(sk
);
3538 if (len
!= sizeof(struct sctp_paddrparams
))
3541 if (copy_from_user(¶ms
, optval
, len
))
3544 /* If an address other than INADDR_ANY is specified, and
3545 * no transport is found, then the request is invalid.
3547 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
3548 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
3549 params
.spp_assoc_id
);
3551 SCTP_DEBUG_PRINTK("Failed no transport\n");
3556 /* Get association, if assoc_id != 0 and the socket is a one
3557 * to many style socket, and an association was not found, then
3558 * the id was invalid.
3560 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
3561 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
3562 SCTP_DEBUG_PRINTK("Failed no association\n");
3567 /* Fetch transport values. */
3568 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
3569 params
.spp_pathmtu
= trans
->pathmtu
;
3570 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
3571 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
3573 /*draft-11 doesn't say what to return in spp_flags*/
3574 params
.spp_flags
= trans
->param_flags
;
3576 /* Fetch association values. */
3577 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
3578 params
.spp_pathmtu
= asoc
->pathmtu
;
3579 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
3580 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
3582 /*draft-11 doesn't say what to return in spp_flags*/
3583 params
.spp_flags
= asoc
->param_flags
;
3585 /* Fetch socket values. */
3586 params
.spp_hbinterval
= sp
->hbinterval
;
3587 params
.spp_pathmtu
= sp
->pathmtu
;
3588 params
.spp_sackdelay
= sp
->sackdelay
;
3589 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
3591 /*draft-11 doesn't say what to return in spp_flags*/
3592 params
.spp_flags
= sp
->param_flags
;
3595 if (copy_to_user(optval
, ¶ms
, len
))
3598 if (put_user(len
, optlen
))
3604 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3606 * This options will get or set the delayed ack timer. The time is set
3607 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3608 * endpoints default delayed ack timer value. If the assoc_id field is
3609 * non-zero, then the set or get effects the specified association.
3611 * struct sctp_assoc_value {
3612 * sctp_assoc_t assoc_id;
3613 * uint32_t assoc_value;
3616 * assoc_id - This parameter, indicates which association the
3617 * user is preforming an action upon. Note that if
3618 * this field's value is zero then the endpoints
3619 * default value is changed (effecting future
3620 * associations only).
3622 * assoc_value - This parameter contains the number of milliseconds
3623 * that the user is requesting the delayed ACK timer
3624 * be set to. Note that this value is defined in
3625 * the standard to be between 200 and 500 milliseconds.
3627 * Note: a value of zero will leave the value alone,
3628 * but disable SACK delay. A non-zero value will also
3629 * enable SACK delay.
3631 static int sctp_getsockopt_delayed_ack_time(struct sock
*sk
, int len
,
3632 char __user
*optval
,
3635 struct sctp_assoc_value params
;
3636 struct sctp_association
*asoc
= NULL
;
3637 struct sctp_sock
*sp
= sctp_sk(sk
);
3639 if (len
!= sizeof(struct sctp_assoc_value
))
3642 if (copy_from_user(¶ms
, optval
, len
))
3645 /* Get association, if assoc_id != 0 and the socket is a one
3646 * to many style socket, and an association was not found, then
3647 * the id was invalid.
3649 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
3650 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
3654 /* Fetch association values. */
3655 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
)
3656 params
.assoc_value
= jiffies_to_msecs(
3659 params
.assoc_value
= 0;
3661 /* Fetch socket values. */
3662 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
)
3663 params
.assoc_value
= sp
->sackdelay
;
3665 params
.assoc_value
= 0;
3668 if (copy_to_user(optval
, ¶ms
, len
))
3671 if (put_user(len
, optlen
))
3677 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3679 * Applications can specify protocol parameters for the default association
3680 * initialization. The option name argument to setsockopt() and getsockopt()
3683 * Setting initialization parameters is effective only on an unconnected
3684 * socket (for UDP-style sockets only future associations are effected
3685 * by the change). With TCP-style sockets, this option is inherited by
3686 * sockets derived from a listener socket.
3688 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3690 if (len
!= sizeof(struct sctp_initmsg
))
3692 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
3697 static int sctp_getsockopt_peer_addrs_num_old(struct sock
*sk
, int len
,
3698 char __user
*optval
,
3702 struct sctp_association
*asoc
;
3703 struct list_head
*pos
;
3706 if (len
!= sizeof(sctp_assoc_t
))
3709 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3712 /* For UDP-style sockets, id specifies the association to query. */
3713 asoc
= sctp_id2assoc(sk
, id
);
3717 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3725 * Old API for getting list of peer addresses. Does not work for 32-bit
3726 * programs running on a 64-bit kernel
3728 static int sctp_getsockopt_peer_addrs_old(struct sock
*sk
, int len
,
3729 char __user
*optval
,
3732 struct sctp_association
*asoc
;
3733 struct list_head
*pos
;
3735 struct sctp_getaddrs_old getaddrs
;
3736 struct sctp_transport
*from
;
3738 union sctp_addr temp
;
3739 struct sctp_sock
*sp
= sctp_sk(sk
);
3742 if (len
!= sizeof(struct sctp_getaddrs_old
))
3745 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
3748 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3750 /* For UDP-style sockets, id specifies the association to query. */
3751 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3755 to
= (void __user
*)getaddrs
.addrs
;
3756 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3757 from
= list_entry(pos
, struct sctp_transport
, transports
);
3758 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3759 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3760 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3761 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
3762 if (copy_to_user(to
, &temp
, addrlen
))
3766 if (cnt
>= getaddrs
.addr_num
) break;
3768 getaddrs
.addr_num
= cnt
;
3769 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
3775 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
3776 char __user
*optval
, int __user
*optlen
)
3778 struct sctp_association
*asoc
;
3779 struct list_head
*pos
;
3781 struct sctp_getaddrs getaddrs
;
3782 struct sctp_transport
*from
;
3784 union sctp_addr temp
;
3785 struct sctp_sock
*sp
= sctp_sk(sk
);
3790 if (len
< sizeof(struct sctp_getaddrs
))
3793 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
3796 /* For UDP-style sockets, id specifies the association to query. */
3797 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3801 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
3802 space_left
= len
- sizeof(struct sctp_getaddrs
) -
3803 offsetof(struct sctp_getaddrs
,addrs
);
3805 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3806 from
= list_entry(pos
, struct sctp_transport
, transports
);
3807 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3808 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3809 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3810 if(space_left
< addrlen
)
3812 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
3813 if (copy_to_user(to
, &temp
, addrlen
))
3817 space_left
-= addrlen
;
3820 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
3822 bytes_copied
= ((char __user
*)to
) - optval
;
3823 if (put_user(bytes_copied
, optlen
))
3829 static int sctp_getsockopt_local_addrs_num_old(struct sock
*sk
, int len
,
3830 char __user
*optval
,
3834 struct sctp_bind_addr
*bp
;
3835 struct sctp_association
*asoc
;
3836 struct list_head
*pos
;
3837 struct sctp_sockaddr_entry
*addr
;
3838 rwlock_t
*addr_lock
;
3839 unsigned long flags
;
3842 if (len
!= sizeof(sctp_assoc_t
))
3845 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3849 * For UDP-style sockets, id specifies the association to query.
3850 * If the id field is set to the value '0' then the locally bound
3851 * addresses are returned without regard to any particular
3855 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
3856 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
3858 asoc
= sctp_id2assoc(sk
, id
);
3861 bp
= &asoc
->base
.bind_addr
;
3862 addr_lock
= &asoc
->base
.addr_lock
;
3865 sctp_read_lock(addr_lock
);
3867 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3868 * addresses from the global local address list.
3870 if (sctp_list_single_entry(&bp
->address_list
)) {
3871 addr
= list_entry(bp
->address_list
.next
,
3872 struct sctp_sockaddr_entry
, list
);
3873 if (sctp_is_any(&addr
->a
)) {
3874 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3875 list_for_each(pos
, &sctp_local_addr_list
) {
3876 addr
= list_entry(pos
,
3877 struct sctp_sockaddr_entry
,
3879 if ((PF_INET
== sk
->sk_family
) &&
3880 (AF_INET6
== addr
->a
.sa
.sa_family
))
3884 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3892 list_for_each(pos
, &bp
->address_list
) {
3897 sctp_read_unlock(addr_lock
);
3901 /* Helper function that copies local addresses to user and returns the number
3902 * of addresses copied.
3904 static int sctp_copy_laddrs_to_user_old(struct sock
*sk
, __u16 port
, int max_addrs
,
3907 struct list_head
*pos
;
3908 struct sctp_sockaddr_entry
*addr
;
3909 unsigned long flags
;
3910 union sctp_addr temp
;
3914 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3915 list_for_each(pos
, &sctp_local_addr_list
) {
3916 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3917 if ((PF_INET
== sk
->sk_family
) &&
3918 (AF_INET6
== addr
->a
.sa
.sa_family
))
3920 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3921 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3923 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3924 temp
.v4
.sin_port
= htons(port
);
3925 if (copy_to_user(to
, &temp
, addrlen
)) {
3926 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3932 if (cnt
>= max_addrs
) break;
3934 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
, flags
);
3939 static int sctp_copy_laddrs_to_user(struct sock
*sk
, __u16 port
,
3940 void __user
**to
, size_t space_left
)
3942 struct list_head
*pos
;
3943 struct sctp_sockaddr_entry
*addr
;
3944 unsigned long flags
;
3945 union sctp_addr temp
;
3949 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3950 list_for_each(pos
, &sctp_local_addr_list
) {
3951 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3952 if ((PF_INET
== sk
->sk_family
) &&
3953 (AF_INET6
== addr
->a
.sa
.sa_family
))
3955 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3956 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3958 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3959 if(space_left
<addrlen
)
3961 temp
.v4
.sin_port
= htons(port
);
3962 if (copy_to_user(*to
, &temp
, addrlen
)) {
3963 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3969 space_left
-= addrlen
;
3971 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
, flags
);
3976 /* Old API for getting list of local addresses. Does not work for 32-bit
3977 * programs running on a 64-bit kernel
3979 static int sctp_getsockopt_local_addrs_old(struct sock
*sk
, int len
,
3980 char __user
*optval
, int __user
*optlen
)
3982 struct sctp_bind_addr
*bp
;
3983 struct sctp_association
*asoc
;
3984 struct list_head
*pos
;
3986 struct sctp_getaddrs_old getaddrs
;
3987 struct sctp_sockaddr_entry
*addr
;
3989 union sctp_addr temp
;
3990 struct sctp_sock
*sp
= sctp_sk(sk
);
3992 rwlock_t
*addr_lock
;
3995 if (len
!= sizeof(struct sctp_getaddrs_old
))
3998 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
4001 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
4003 * For UDP-style sockets, id specifies the association to query.
4004 * If the id field is set to the value '0' then the locally bound
4005 * addresses are returned without regard to any particular
4008 if (0 == getaddrs
.assoc_id
) {
4009 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4010 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4012 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4015 bp
= &asoc
->base
.bind_addr
;
4016 addr_lock
= &asoc
->base
.addr_lock
;
4019 to
= getaddrs
.addrs
;
4021 sctp_read_lock(addr_lock
);
4023 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4024 * addresses from the global local address list.
4026 if (sctp_list_single_entry(&bp
->address_list
)) {
4027 addr
= list_entry(bp
->address_list
.next
,
4028 struct sctp_sockaddr_entry
, list
);
4029 if (sctp_is_any(&addr
->a
)) {
4030 cnt
= sctp_copy_laddrs_to_user_old(sk
, bp
->port
,
4041 list_for_each(pos
, &bp
->address_list
) {
4042 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4043 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4044 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4045 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4046 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
4047 if (copy_to_user(to
, &temp
, addrlen
)) {
4053 if (cnt
>= getaddrs
.addr_num
) break;
4057 getaddrs
.addr_num
= cnt
;
4058 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
4062 sctp_read_unlock(addr_lock
);
4066 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4067 char __user
*optval
, int __user
*optlen
)
4069 struct sctp_bind_addr
*bp
;
4070 struct sctp_association
*asoc
;
4071 struct list_head
*pos
;
4073 struct sctp_getaddrs getaddrs
;
4074 struct sctp_sockaddr_entry
*addr
;
4076 union sctp_addr temp
;
4077 struct sctp_sock
*sp
= sctp_sk(sk
);
4079 rwlock_t
*addr_lock
;
4084 if (len
<= sizeof(struct sctp_getaddrs
))
4087 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4091 * For UDP-style sockets, id specifies the association to query.
4092 * If the id field is set to the value '0' then the locally bound
4093 * addresses are returned without regard to any particular
4096 if (0 == getaddrs
.assoc_id
) {
4097 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4098 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4100 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4103 bp
= &asoc
->base
.bind_addr
;
4104 addr_lock
= &asoc
->base
.addr_lock
;
4107 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4108 space_left
= len
- sizeof(struct sctp_getaddrs
) -
4109 offsetof(struct sctp_getaddrs
,addrs
);
4111 sctp_read_lock(addr_lock
);
4113 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4114 * addresses from the global local address list.
4116 if (sctp_list_single_entry(&bp
->address_list
)) {
4117 addr
= list_entry(bp
->address_list
.next
,
4118 struct sctp_sockaddr_entry
, list
);
4119 if (sctp_is_any(&addr
->a
)) {
4120 cnt
= sctp_copy_laddrs_to_user(sk
, bp
->port
,
4130 list_for_each(pos
, &bp
->address_list
) {
4131 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4132 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4133 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4134 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4135 if(space_left
< addrlen
)
4136 return -ENOMEM
; /*fixme: right error?*/
4137 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
4138 if (copy_to_user(to
, &temp
, addrlen
)) {
4144 space_left
-= addrlen
;
4148 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4150 bytes_copied
= ((char __user
*)to
) - optval
;
4151 if (put_user(bytes_copied
, optlen
))
4155 sctp_read_unlock(addr_lock
);
4159 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4161 * Requests that the local SCTP stack use the enclosed peer address as
4162 * the association primary. The enclosed address must be one of the
4163 * association peer's addresses.
4165 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4166 char __user
*optval
, int __user
*optlen
)
4168 struct sctp_prim prim
;
4169 struct sctp_association
*asoc
;
4170 struct sctp_sock
*sp
= sctp_sk(sk
);
4172 if (len
!= sizeof(struct sctp_prim
))
4175 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
4178 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4182 if (!asoc
->peer
.primary_path
)
4185 asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
=
4186 htons(asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
);
4187 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4188 sizeof(union sctp_addr
));
4189 asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
=
4190 ntohs(asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
);
4192 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4193 (union sctp_addr
*)&prim
.ssp_addr
);
4195 if (copy_to_user(optval
, &prim
, sizeof(struct sctp_prim
)))
4202 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4204 * Requests that the local endpoint set the specified Adaption Layer
4205 * Indication parameter for all future INIT and INIT-ACK exchanges.
4207 static int sctp_getsockopt_adaption_layer(struct sock
*sk
, int len
,
4208 char __user
*optval
, int __user
*optlen
)
4210 struct sctp_setadaption adaption
;
4212 if (len
!= sizeof(struct sctp_setadaption
))
4215 adaption
.ssb_adaption_ind
= sctp_sk(sk
)->adaption_ind
;
4216 if (copy_to_user(optval
, &adaption
, len
))
4224 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4226 * Applications that wish to use the sendto() system call may wish to
4227 * specify a default set of parameters that would normally be supplied
4228 * through the inclusion of ancillary data. This socket option allows
4229 * such an application to set the default sctp_sndrcvinfo structure.
4232 * The application that wishes to use this socket option simply passes
4233 * in to this call the sctp_sndrcvinfo structure defined in Section
4234 * 5.2.2) The input parameters accepted by this call include
4235 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4236 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4237 * to this call if the caller is using the UDP model.
4239 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4241 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4242 int len
, char __user
*optval
,
4245 struct sctp_sndrcvinfo info
;
4246 struct sctp_association
*asoc
;
4247 struct sctp_sock
*sp
= sctp_sk(sk
);
4249 if (len
!= sizeof(struct sctp_sndrcvinfo
))
4251 if (copy_from_user(&info
, optval
, sizeof(struct sctp_sndrcvinfo
)))
4254 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4255 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4259 info
.sinfo_stream
= asoc
->default_stream
;
4260 info
.sinfo_flags
= asoc
->default_flags
;
4261 info
.sinfo_ppid
= asoc
->default_ppid
;
4262 info
.sinfo_context
= asoc
->default_context
;
4263 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4265 info
.sinfo_stream
= sp
->default_stream
;
4266 info
.sinfo_flags
= sp
->default_flags
;
4267 info
.sinfo_ppid
= sp
->default_ppid
;
4268 info
.sinfo_context
= sp
->default_context
;
4269 info
.sinfo_timetolive
= sp
->default_timetolive
;
4272 if (copy_to_user(optval
, &info
, sizeof(struct sctp_sndrcvinfo
)))
4280 * 7.1.5 SCTP_NODELAY
4282 * Turn on/off any Nagle-like algorithm. This means that packets are
4283 * generally sent as soon as possible and no unnecessary delays are
4284 * introduced, at the cost of more packets in the network. Expects an
4285 * integer boolean flag.
4288 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4289 char __user
*optval
, int __user
*optlen
)
4293 if (len
< sizeof(int))
4297 val
= (sctp_sk(sk
)->nodelay
== 1);
4298 if (put_user(len
, optlen
))
4300 if (copy_to_user(optval
, &val
, len
))
4307 * 7.1.1 SCTP_RTOINFO
4309 * The protocol parameters used to initialize and bound retransmission
4310 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4311 * and modify these parameters.
4312 * All parameters are time values, in milliseconds. A value of 0, when
4313 * modifying the parameters, indicates that the current value should not
4317 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4318 char __user
*optval
,
4319 int __user
*optlen
) {
4320 struct sctp_rtoinfo rtoinfo
;
4321 struct sctp_association
*asoc
;
4323 if (len
!= sizeof (struct sctp_rtoinfo
))
4326 if (copy_from_user(&rtoinfo
, optval
, sizeof (struct sctp_rtoinfo
)))
4329 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4331 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4334 /* Values corresponding to the specific association. */
4336 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4337 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4338 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4340 /* Values corresponding to the endpoint. */
4341 struct sctp_sock
*sp
= sctp_sk(sk
);
4343 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4344 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4345 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4348 if (put_user(len
, optlen
))
4351 if (copy_to_user(optval
, &rtoinfo
, len
))
4359 * 7.1.2 SCTP_ASSOCINFO
4361 * This option is used to tune the the maximum retransmission attempts
4362 * of the association.
4363 * Returns an error if the new association retransmission value is
4364 * greater than the sum of the retransmission value of the peer.
4365 * See [SCTP] for more information.
4368 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4369 char __user
*optval
,
4373 struct sctp_assocparams assocparams
;
4374 struct sctp_association
*asoc
;
4375 struct list_head
*pos
;
4378 if (len
!= sizeof (struct sctp_assocparams
))
4381 if (copy_from_user(&assocparams
, optval
,
4382 sizeof (struct sctp_assocparams
)))
4385 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4387 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4390 /* Values correspoinding to the specific association */
4392 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4393 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4394 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4395 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4397 (asoc
->cookie_life
.tv_usec
4400 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4404 assocparams
.sasoc_number_peer_destinations
= cnt
;
4406 /* Values corresponding to the endpoint */
4407 struct sctp_sock
*sp
= sctp_sk(sk
);
4409 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4410 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4411 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4412 assocparams
.sasoc_cookie_life
=
4413 sp
->assocparams
.sasoc_cookie_life
;
4414 assocparams
.sasoc_number_peer_destinations
=
4416 sasoc_number_peer_destinations
;
4419 if (put_user(len
, optlen
))
4422 if (copy_to_user(optval
, &assocparams
, len
))
4429 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4431 * This socket option is a boolean flag which turns on or off mapped V4
4432 * addresses. If this option is turned on and the socket is type
4433 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4434 * If this option is turned off, then no mapping will be done of V4
4435 * addresses and a user will receive both PF_INET6 and PF_INET type
4436 * addresses on the socket.
4438 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4439 char __user
*optval
, int __user
*optlen
)
4442 struct sctp_sock
*sp
= sctp_sk(sk
);
4444 if (len
< sizeof(int))
4449 if (put_user(len
, optlen
))
4451 if (copy_to_user(optval
, &val
, len
))
4458 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4460 * This socket option specifies the maximum size to put in any outgoing
4461 * SCTP chunk. If a message is larger than this size it will be
4462 * fragmented by SCTP into the specified size. Note that the underlying
4463 * SCTP implementation may fragment into smaller sized chunks when the
4464 * PMTU of the underlying association is smaller than the value set by
4467 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4468 char __user
*optval
, int __user
*optlen
)
4472 if (len
< sizeof(int))
4477 val
= sctp_sk(sk
)->user_frag
;
4478 if (put_user(len
, optlen
))
4480 if (copy_to_user(optval
, &val
, len
))
4486 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
4487 char __user
*optval
, int __user
*optlen
)
4492 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4495 /* I can hardly begin to describe how wrong this is. This is
4496 * so broken as to be worse than useless. The API draft
4497 * REALLY is NOT helpful here... I am not convinced that the
4498 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4499 * are at all well-founded.
4501 if (level
!= SOL_SCTP
) {
4502 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4504 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
4508 if (get_user(len
, optlen
))
4515 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
4517 case SCTP_DISABLE_FRAGMENTS
:
4518 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
4522 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
4524 case SCTP_AUTOCLOSE
:
4525 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
4527 case SCTP_SOCKOPT_PEELOFF
:
4528 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
4530 case SCTP_PEER_ADDR_PARAMS
:
4531 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
4534 case SCTP_DELAYED_ACK_TIME
:
4535 retval
= sctp_getsockopt_delayed_ack_time(sk
, len
, optval
,
4539 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
4541 case SCTP_GET_PEER_ADDRS_NUM_OLD
:
4542 retval
= sctp_getsockopt_peer_addrs_num_old(sk
, len
, optval
,
4545 case SCTP_GET_LOCAL_ADDRS_NUM_OLD
:
4546 retval
= sctp_getsockopt_local_addrs_num_old(sk
, len
, optval
,
4549 case SCTP_GET_PEER_ADDRS_OLD
:
4550 retval
= sctp_getsockopt_peer_addrs_old(sk
, len
, optval
,
4553 case SCTP_GET_LOCAL_ADDRS_OLD
:
4554 retval
= sctp_getsockopt_local_addrs_old(sk
, len
, optval
,
4557 case SCTP_GET_PEER_ADDRS
:
4558 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
4561 case SCTP_GET_LOCAL_ADDRS
:
4562 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
4565 case SCTP_DEFAULT_SEND_PARAM
:
4566 retval
= sctp_getsockopt_default_send_param(sk
, len
,
4569 case SCTP_PRIMARY_ADDR
:
4570 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
4573 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
4576 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
4578 case SCTP_ASSOCINFO
:
4579 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
4581 case SCTP_I_WANT_MAPPED_V4_ADDR
:
4582 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
4585 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
4587 case SCTP_GET_PEER_ADDR_INFO
:
4588 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
4591 case SCTP_ADAPTION_LAYER
:
4592 retval
= sctp_getsockopt_adaption_layer(sk
, len
, optval
,
4596 retval
= -ENOPROTOOPT
;
4600 sctp_release_sock(sk
);
4604 static void sctp_hash(struct sock
*sk
)
4609 static void sctp_unhash(struct sock
*sk
)
4614 /* Check if port is acceptable. Possibly find first available port.
4616 * The port hash table (contained in the 'global' SCTP protocol storage
4617 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4618 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4619 * list (the list number is the port number hashed out, so as you
4620 * would expect from a hash function, all the ports in a given list have
4621 * such a number that hashes out to the same list number; you were
4622 * expecting that, right?); so each list has a set of ports, with a
4623 * link to the socket (struct sock) that uses it, the port number and
4624 * a fastreuse flag (FIXME: NPI ipg).
4626 static struct sctp_bind_bucket
*sctp_bucket_create(
4627 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
4629 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
4631 struct sctp_bind_hashbucket
*head
; /* hash list */
4632 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
4633 unsigned short snum
;
4636 /* NOTE: Remember to put this back to net order. */
4637 addr
->v4
.sin_port
= ntohs(addr
->v4
.sin_port
);
4638 snum
= addr
->v4
.sin_port
;
4640 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
4641 sctp_local_bh_disable();
4644 /* Search for an available port.
4646 * 'sctp_port_rover' was the last port assigned, so
4647 * we start to search from 'sctp_port_rover +
4648 * 1'. What we do is first check if port 'rover' is
4649 * already in the hash table; if not, we use that; if
4650 * it is, we try next.
4652 int low
= sysctl_local_port_range
[0];
4653 int high
= sysctl_local_port_range
[1];
4654 int remaining
= (high
- low
) + 1;
4658 sctp_spin_lock(&sctp_port_alloc_lock
);
4659 rover
= sctp_port_rover
;
4662 if ((rover
< low
) || (rover
> high
))
4664 index
= sctp_phashfn(rover
);
4665 head
= &sctp_port_hashtable
[index
];
4666 sctp_spin_lock(&head
->lock
);
4667 for (pp
= head
->chain
; pp
; pp
= pp
->next
)
4668 if (pp
->port
== rover
)
4672 sctp_spin_unlock(&head
->lock
);
4673 } while (--remaining
> 0);
4674 sctp_port_rover
= rover
;
4675 sctp_spin_unlock(&sctp_port_alloc_lock
);
4677 /* Exhausted local port range during search? */
4682 /* OK, here is the one we will use. HEAD (the port
4683 * hash table list entry) is non-NULL and we hold it's
4688 /* We are given an specific port number; we verify
4689 * that it is not being used. If it is used, we will
4690 * exahust the search in the hash list corresponding
4691 * to the port number (snum) - we detect that with the
4692 * port iterator, pp being NULL.
4694 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
4695 sctp_spin_lock(&head
->lock
);
4696 for (pp
= head
->chain
; pp
; pp
= pp
->next
) {
4697 if (pp
->port
== snum
)
4704 if (!hlist_empty(&pp
->owner
)) {
4705 /* We had a port hash table hit - there is an
4706 * available port (pp != NULL) and it is being
4707 * used by other socket (pp->owner not empty); that other
4708 * socket is going to be sk2.
4710 int reuse
= sk
->sk_reuse
;
4712 struct hlist_node
*node
;
4714 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4715 if (pp
->fastreuse
&& sk
->sk_reuse
)
4718 /* Run through the list of sockets bound to the port
4719 * (pp->port) [via the pointers bind_next and
4720 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4721 * we get the endpoint they describe and run through
4722 * the endpoint's list of IP (v4 or v6) addresses,
4723 * comparing each of the addresses with the address of
4724 * the socket sk. If we find a match, then that means
4725 * that this port/socket (sk) combination are already
4728 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
4729 struct sctp_endpoint
*ep2
;
4730 ep2
= sctp_sk(sk2
)->ep
;
4732 if (reuse
&& sk2
->sk_reuse
)
4735 if (sctp_bind_addr_match(&ep2
->base
.bind_addr
, addr
,
4741 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4744 /* If there was a hash table miss, create a new port. */
4746 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
4749 /* In either case (hit or miss), make sure fastreuse is 1 only
4750 * if sk->sk_reuse is too (that is, if the caller requested
4751 * SO_REUSEADDR on this socket -sk-).
4753 if (hlist_empty(&pp
->owner
))
4754 pp
->fastreuse
= sk
->sk_reuse
? 1 : 0;
4755 else if (pp
->fastreuse
&& !sk
->sk_reuse
)
4758 /* We are set, so fill up all the data in the hash table
4759 * entry, tie the socket list information with the rest of the
4760 * sockets FIXME: Blurry, NPI (ipg).
4763 inet_sk(sk
)->num
= snum
;
4764 if (!sctp_sk(sk
)->bind_hash
) {
4765 sk_add_bind_node(sk
, &pp
->owner
);
4766 sctp_sk(sk
)->bind_hash
= pp
;
4771 sctp_spin_unlock(&head
->lock
);
4774 sctp_local_bh_enable();
4775 addr
->v4
.sin_port
= htons(addr
->v4
.sin_port
);
4779 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4780 * port is requested.
4782 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
4785 union sctp_addr addr
;
4786 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4788 /* Set up a dummy address struct from the sk. */
4789 af
->from_sk(&addr
, sk
);
4790 addr
.v4
.sin_port
= htons(snum
);
4792 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4793 ret
= sctp_get_port_local(sk
, &addr
);
4795 return (ret
? 1 : 0);
4799 * 3.1.3 listen() - UDP Style Syntax
4801 * By default, new associations are not accepted for UDP style sockets.
4802 * An application uses listen() to mark a socket as being able to
4803 * accept new associations.
4805 SCTP_STATIC
int sctp_seqpacket_listen(struct sock
*sk
, int backlog
)
4807 struct sctp_sock
*sp
= sctp_sk(sk
);
4808 struct sctp_endpoint
*ep
= sp
->ep
;
4810 /* Only UDP style sockets that are not peeled off are allowed to
4813 if (!sctp_style(sk
, UDP
))
4816 /* If backlog is zero, disable listening. */
4818 if (sctp_sstate(sk
, CLOSED
))
4821 sctp_unhash_endpoint(ep
);
4822 sk
->sk_state
= SCTP_SS_CLOSED
;
4825 /* Return if we are already listening. */
4826 if (sctp_sstate(sk
, LISTENING
))
4830 * If a bind() or sctp_bindx() is not called prior to a listen()
4831 * call that allows new associations to be accepted, the system
4832 * picks an ephemeral port and will choose an address set equivalent
4833 * to binding with a wildcard address.
4835 * This is not currently spelled out in the SCTP sockets
4836 * extensions draft, but follows the practice as seen in TCP
4839 if (!ep
->base
.bind_addr
.port
) {
4840 if (sctp_autobind(sk
))
4843 sk
->sk_state
= SCTP_SS_LISTENING
;
4844 sctp_hash_endpoint(ep
);
4849 * 4.1.3 listen() - TCP Style Syntax
4851 * Applications uses listen() to ready the SCTP endpoint for accepting
4852 * inbound associations.
4854 SCTP_STATIC
int sctp_stream_listen(struct sock
*sk
, int backlog
)
4856 struct sctp_sock
*sp
= sctp_sk(sk
);
4857 struct sctp_endpoint
*ep
= sp
->ep
;
4859 /* If backlog is zero, disable listening. */
4861 if (sctp_sstate(sk
, CLOSED
))
4864 sctp_unhash_endpoint(ep
);
4865 sk
->sk_state
= SCTP_SS_CLOSED
;
4868 if (sctp_sstate(sk
, LISTENING
))
4872 * If a bind() or sctp_bindx() is not called prior to a listen()
4873 * call that allows new associations to be accepted, the system
4874 * picks an ephemeral port and will choose an address set equivalent
4875 * to binding with a wildcard address.
4877 * This is not currently spelled out in the SCTP sockets
4878 * extensions draft, but follows the practice as seen in TCP
4881 if (!ep
->base
.bind_addr
.port
) {
4882 if (sctp_autobind(sk
))
4885 sk
->sk_state
= SCTP_SS_LISTENING
;
4886 sk
->sk_max_ack_backlog
= backlog
;
4887 sctp_hash_endpoint(ep
);
4892 * Move a socket to LISTENING state.
4894 int sctp_inet_listen(struct socket
*sock
, int backlog
)
4896 struct sock
*sk
= sock
->sk
;
4897 struct crypto_tfm
*tfm
=NULL
;
4900 if (unlikely(backlog
< 0))
4905 if (sock
->state
!= SS_UNCONNECTED
)
4908 /* Allocate HMAC for generating cookie. */
4909 if (sctp_hmac_alg
) {
4910 tfm
= sctp_crypto_alloc_tfm(sctp_hmac_alg
, 0);
4917 switch (sock
->type
) {
4918 case SOCK_SEQPACKET
:
4919 err
= sctp_seqpacket_listen(sk
, backlog
);
4922 err
= sctp_stream_listen(sk
, backlog
);
4930 /* Store away the transform reference. */
4931 sctp_sk(sk
)->hmac
= tfm
;
4933 sctp_release_sock(sk
);
4936 sctp_crypto_free_tfm(tfm
);
4941 * This function is done by modeling the current datagram_poll() and the
4942 * tcp_poll(). Note that, based on these implementations, we don't
4943 * lock the socket in this function, even though it seems that,
4944 * ideally, locking or some other mechanisms can be used to ensure
4945 * the integrity of the counters (sndbuf and wmem_alloc) used
4946 * in this place. We assume that we don't need locks either until proven
4949 * Another thing to note is that we include the Async I/O support
4950 * here, again, by modeling the current TCP/UDP code. We don't have
4951 * a good way to test with it yet.
4953 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
4955 struct sock
*sk
= sock
->sk
;
4956 struct sctp_sock
*sp
= sctp_sk(sk
);
4959 poll_wait(file
, sk
->sk_sleep
, wait
);
4961 /* A TCP-style listening socket becomes readable when the accept queue
4964 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
4965 return (!list_empty(&sp
->ep
->asocs
)) ?
4966 (POLLIN
| POLLRDNORM
) : 0;
4970 /* Is there any exceptional events? */
4971 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
4973 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
4975 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
4978 /* Is it readable? Reconsider this code with TCP-style support. */
4979 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
4980 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
4981 mask
|= POLLIN
| POLLRDNORM
;
4983 /* The association is either gone or not ready. */
4984 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
4987 /* Is it writable? */
4988 if (sctp_writeable(sk
)) {
4989 mask
|= POLLOUT
| POLLWRNORM
;
4991 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
4993 * Since the socket is not locked, the buffer
4994 * might be made available after the writeable check and
4995 * before the bit is set. This could cause a lost I/O
4996 * signal. tcp_poll() has a race breaker for this race
4997 * condition. Based on their implementation, we put
4998 * in the following code to cover it as well.
5000 if (sctp_writeable(sk
))
5001 mask
|= POLLOUT
| POLLWRNORM
;
5006 /********************************************************************
5007 * 2nd Level Abstractions
5008 ********************************************************************/
5010 static struct sctp_bind_bucket
*sctp_bucket_create(
5011 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
5013 struct sctp_bind_bucket
*pp
;
5015 pp
= kmem_cache_alloc(sctp_bucket_cachep
, SLAB_ATOMIC
);
5016 SCTP_DBG_OBJCNT_INC(bind_bucket
);
5020 INIT_HLIST_HEAD(&pp
->owner
);
5021 if ((pp
->next
= head
->chain
) != NULL
)
5022 pp
->next
->pprev
= &pp
->next
;
5024 pp
->pprev
= &head
->chain
;
5029 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5030 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
5032 if (pp
&& hlist_empty(&pp
->owner
)) {
5034 pp
->next
->pprev
= pp
->pprev
;
5035 *(pp
->pprev
) = pp
->next
;
5036 kmem_cache_free(sctp_bucket_cachep
, pp
);
5037 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
5041 /* Release this socket's reference to a local port. */
5042 static inline void __sctp_put_port(struct sock
*sk
)
5044 struct sctp_bind_hashbucket
*head
=
5045 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->num
)];
5046 struct sctp_bind_bucket
*pp
;
5048 sctp_spin_lock(&head
->lock
);
5049 pp
= sctp_sk(sk
)->bind_hash
;
5050 __sk_del_bind_node(sk
);
5051 sctp_sk(sk
)->bind_hash
= NULL
;
5052 inet_sk(sk
)->num
= 0;
5053 sctp_bucket_destroy(pp
);
5054 sctp_spin_unlock(&head
->lock
);
5057 void sctp_put_port(struct sock
*sk
)
5059 sctp_local_bh_disable();
5060 __sctp_put_port(sk
);
5061 sctp_local_bh_enable();
5065 * The system picks an ephemeral port and choose an address set equivalent
5066 * to binding with a wildcard address.
5067 * One of those addresses will be the primary address for the association.
5068 * This automatically enables the multihoming capability of SCTP.
5070 static int sctp_autobind(struct sock
*sk
)
5072 union sctp_addr autoaddr
;
5074 unsigned short port
;
5076 /* Initialize a local sockaddr structure to INADDR_ANY. */
5077 af
= sctp_sk(sk
)->pf
->af
;
5079 port
= htons(inet_sk(sk
)->num
);
5080 af
->inaddr_any(&autoaddr
, port
);
5082 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5085 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5088 * 4.2 The cmsghdr Structure *
5090 * When ancillary data is sent or received, any number of ancillary data
5091 * objects can be specified by the msg_control and msg_controllen members of
5092 * the msghdr structure, because each object is preceded by
5093 * a cmsghdr structure defining the object's length (the cmsg_len member).
5094 * Historically Berkeley-derived implementations have passed only one object
5095 * at a time, but this API allows multiple objects to be
5096 * passed in a single call to sendmsg() or recvmsg(). The following example
5097 * shows two ancillary data objects in a control buffer.
5099 * |<--------------------------- msg_controllen -------------------------->|
5102 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5104 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5107 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5109 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5112 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5113 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5115 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5117 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5124 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5125 sctp_cmsgs_t
*cmsgs
)
5127 struct cmsghdr
*cmsg
;
5129 for (cmsg
= CMSG_FIRSTHDR(msg
);
5131 cmsg
= CMSG_NXTHDR((struct msghdr
*)msg
, cmsg
)) {
5132 if (!CMSG_OK(msg
, cmsg
))
5135 /* Should we parse this header or ignore? */
5136 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5139 /* Strictly check lengths following example in SCM code. */
5140 switch (cmsg
->cmsg_type
) {
5142 /* SCTP Socket API Extension
5143 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5145 * This cmsghdr structure provides information for
5146 * initializing new SCTP associations with sendmsg().
5147 * The SCTP_INITMSG socket option uses this same data
5148 * structure. This structure is not used for
5151 * cmsg_level cmsg_type cmsg_data[]
5152 * ------------ ------------ ----------------------
5153 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5155 if (cmsg
->cmsg_len
!=
5156 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5158 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5162 /* SCTP Socket API Extension
5163 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5165 * This cmsghdr structure specifies SCTP options for
5166 * sendmsg() and describes SCTP header information
5167 * about a received message through recvmsg().
5169 * cmsg_level cmsg_type cmsg_data[]
5170 * ------------ ------------ ----------------------
5171 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5173 if (cmsg
->cmsg_len
!=
5174 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5178 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5180 /* Minimally, validate the sinfo_flags. */
5181 if (cmsgs
->info
->sinfo_flags
&
5182 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5183 SCTP_ABORT
| SCTP_EOF
))
5195 * Wait for a packet..
5196 * Note: This function is the same function as in core/datagram.c
5197 * with a few modifications to make lksctp work.
5199 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5204 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5206 /* Socket errors? */
5207 error
= sock_error(sk
);
5211 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5214 /* Socket shut down? */
5215 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5218 /* Sequenced packets can come disconnected. If so we report the
5223 /* Is there a good reason to think that we may receive some data? */
5224 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5227 /* Handle signals. */
5228 if (signal_pending(current
))
5231 /* Let another process have a go. Since we are going to sleep
5232 * anyway. Note: This may cause odd behaviors if the message
5233 * does not fit in the user's buffer, but this seems to be the
5234 * only way to honor MSG_DONTWAIT realistically.
5236 sctp_release_sock(sk
);
5237 *timeo_p
= schedule_timeout(*timeo_p
);
5241 finish_wait(sk
->sk_sleep
, &wait
);
5245 error
= sock_intr_errno(*timeo_p
);
5248 finish_wait(sk
->sk_sleep
, &wait
);
5253 /* Receive a datagram.
5254 * Note: This is pretty much the same routine as in core/datagram.c
5255 * with a few changes to make lksctp work.
5257 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5258 int noblock
, int *err
)
5261 struct sk_buff
*skb
;
5264 timeo
= sock_rcvtimeo(sk
, noblock
);
5266 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5267 timeo
, MAX_SCHEDULE_TIMEOUT
);
5270 /* Again only user level code calls this function,
5271 * so nothing interrupt level
5272 * will suddenly eat the receive_queue.
5274 * Look at current nfs client by the way...
5275 * However, this function was corrent in any case. 8)
5277 if (flags
& MSG_PEEK
) {
5278 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5279 skb
= skb_peek(&sk
->sk_receive_queue
);
5281 atomic_inc(&skb
->users
);
5282 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5284 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5290 /* Caller is allowed not to check sk->sk_err before calling. */
5291 error
= sock_error(sk
);
5295 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5298 /* User doesn't want to wait. */
5302 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5311 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5312 static void __sctp_write_space(struct sctp_association
*asoc
)
5314 struct sock
*sk
= asoc
->base
.sk
;
5315 struct socket
*sock
= sk
->sk_socket
;
5317 if ((sctp_wspace(asoc
) > 0) && sock
) {
5318 if (waitqueue_active(&asoc
->wait
))
5319 wake_up_interruptible(&asoc
->wait
);
5321 if (sctp_writeable(sk
)) {
5322 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
5323 wake_up_interruptible(sk
->sk_sleep
);
5325 /* Note that we try to include the Async I/O support
5326 * here by modeling from the current TCP/UDP code.
5327 * We have not tested with it yet.
5329 if (sock
->fasync_list
&&
5330 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5331 sock_wake_async(sock
, 2, POLL_OUT
);
5336 /* Do accounting for the sndbuf space.
5337 * Decrement the used sndbuf space of the corresponding association by the
5338 * data size which was just transmitted(freed).
5340 static void sctp_wfree(struct sk_buff
*skb
)
5342 struct sctp_association
*asoc
;
5343 struct sctp_chunk
*chunk
;
5346 /* Get the saved chunk pointer. */
5347 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
5350 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
5351 sizeof(struct sk_buff
) +
5352 sizeof(struct sctp_chunk
);
5354 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
5357 __sctp_write_space(asoc
);
5359 sctp_association_put(asoc
);
5362 /* Helper function to wait for space in the sndbuf. */
5363 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
5366 struct sock
*sk
= asoc
->base
.sk
;
5368 long current_timeo
= *timeo_p
;
5371 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5372 asoc
, (long)(*timeo_p
), msg_len
);
5374 /* Increment the association's refcnt. */
5375 sctp_association_hold(asoc
);
5377 /* Wait on the association specific sndbuf space. */
5379 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5380 TASK_INTERRUPTIBLE
);
5383 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5386 if (signal_pending(current
))
5387 goto do_interrupted
;
5388 if (msg_len
<= sctp_wspace(asoc
))
5391 /* Let another process have a go. Since we are going
5394 sctp_release_sock(sk
);
5395 current_timeo
= schedule_timeout(current_timeo
);
5396 BUG_ON(sk
!= asoc
->base
.sk
);
5399 *timeo_p
= current_timeo
;
5403 finish_wait(&asoc
->wait
, &wait
);
5405 /* Release the association's refcnt. */
5406 sctp_association_put(asoc
);
5415 err
= sock_intr_errno(*timeo_p
);
5423 /* If socket sndbuf has changed, wake up all per association waiters. */
5424 void sctp_write_space(struct sock
*sk
)
5426 struct sctp_association
*asoc
;
5427 struct list_head
*pos
;
5429 /* Wake up the tasks in each wait queue. */
5430 list_for_each(pos
, &((sctp_sk(sk
))->ep
->asocs
)) {
5431 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
5432 __sctp_write_space(asoc
);
5436 /* Is there any sndbuf space available on the socket?
5438 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5439 * associations on the same socket. For a UDP-style socket with
5440 * multiple associations, it is possible for it to be "unwriteable"
5441 * prematurely. I assume that this is acceptable because
5442 * a premature "unwriteable" is better than an accidental "writeable" which
5443 * would cause an unwanted block under certain circumstances. For the 1-1
5444 * UDP-style sockets or TCP-style sockets, this code should work.
5447 static int sctp_writeable(struct sock
*sk
)
5451 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
5457 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5458 * returns immediately with EINPROGRESS.
5460 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
5462 struct sock
*sk
= asoc
->base
.sk
;
5464 long current_timeo
= *timeo_p
;
5467 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__
, asoc
,
5470 /* Increment the association's refcnt. */
5471 sctp_association_hold(asoc
);
5474 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5475 TASK_INTERRUPTIBLE
);
5478 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5480 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5483 if (signal_pending(current
))
5484 goto do_interrupted
;
5486 if (sctp_state(asoc
, ESTABLISHED
))
5489 /* Let another process have a go. Since we are going
5492 sctp_release_sock(sk
);
5493 current_timeo
= schedule_timeout(current_timeo
);
5496 *timeo_p
= current_timeo
;
5500 finish_wait(&asoc
->wait
, &wait
);
5502 /* Release the association's refcnt. */
5503 sctp_association_put(asoc
);
5508 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
5511 err
= -ECONNREFUSED
;
5515 err
= sock_intr_errno(*timeo_p
);
5523 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
5525 struct sctp_endpoint
*ep
;
5529 ep
= sctp_sk(sk
)->ep
;
5533 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
5534 TASK_INTERRUPTIBLE
);
5536 if (list_empty(&ep
->asocs
)) {
5537 sctp_release_sock(sk
);
5538 timeo
= schedule_timeout(timeo
);
5543 if (!sctp_sstate(sk
, LISTENING
))
5547 if (!list_empty(&ep
->asocs
))
5550 err
= sock_intr_errno(timeo
);
5551 if (signal_pending(current
))
5559 finish_wait(sk
->sk_sleep
, &wait
);
5564 void sctp_wait_for_close(struct sock
*sk
, long timeout
)
5569 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5570 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
5572 sctp_release_sock(sk
);
5573 timeout
= schedule_timeout(timeout
);
5575 } while (!signal_pending(current
) && timeout
);
5577 finish_wait(sk
->sk_sleep
, &wait
);
5580 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5581 * and its messages to the newsk.
5583 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
5584 struct sctp_association
*assoc
,
5585 sctp_socket_type_t type
)
5587 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
5588 struct sctp_sock
*newsp
= sctp_sk(newsk
);
5589 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5590 struct sctp_endpoint
*newep
= newsp
->ep
;
5591 struct sk_buff
*skb
, *tmp
;
5592 struct sctp_ulpevent
*event
;
5595 /* Migrate socket buffer sizes and all the socket level options to the
5598 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
5599 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
5600 /* Brute force copy old sctp opt. */
5601 inet_sk_copy_descendant(newsk
, oldsk
);
5603 /* Restore the ep value that was overwritten with the above structure
5609 /* Hook this new socket in to the bind_hash list. */
5610 pp
= sctp_sk(oldsk
)->bind_hash
;
5611 sk_add_bind_node(newsk
, &pp
->owner
);
5612 sctp_sk(newsk
)->bind_hash
= pp
;
5613 inet_sk(newsk
)->num
= inet_sk(oldsk
)->num
;
5615 /* Copy the bind_addr list from the original endpoint to the new
5616 * endpoint so that we can handle restarts properly
5618 if (assoc
->peer
.ipv4_address
)
5619 flags
|= SCTP_ADDR4_PEERSUPP
;
5620 if (assoc
->peer
.ipv6_address
)
5621 flags
|= SCTP_ADDR6_PEERSUPP
;
5622 sctp_bind_addr_copy(&newsp
->ep
->base
.bind_addr
,
5623 &oldsp
->ep
->base
.bind_addr
,
5624 SCTP_SCOPE_GLOBAL
, GFP_KERNEL
, flags
);
5626 /* Move any messages in the old socket's receive queue that are for the
5627 * peeled off association to the new socket's receive queue.
5629 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
5630 event
= sctp_skb2event(skb
);
5631 if (event
->asoc
== assoc
) {
5633 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
5634 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
5635 skb_set_owner_r(skb
, newsk
);
5639 /* Clean up any messages pending delivery due to partial
5640 * delivery. Three cases:
5641 * 1) No partial deliver; no work.
5642 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5643 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5645 skb_queue_head_init(&newsp
->pd_lobby
);
5646 sctp_sk(newsk
)->pd_mode
= assoc
->ulpq
.pd_mode
;
5648 if (sctp_sk(oldsk
)->pd_mode
) {
5649 struct sk_buff_head
*queue
;
5651 /* Decide which queue to move pd_lobby skbs to. */
5652 if (assoc
->ulpq
.pd_mode
) {
5653 queue
= &newsp
->pd_lobby
;
5655 queue
= &newsk
->sk_receive_queue
;
5657 /* Walk through the pd_lobby, looking for skbs that
5658 * need moved to the new socket.
5660 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
5661 event
= sctp_skb2event(skb
);
5662 if (event
->asoc
== assoc
) {
5664 __skb_unlink(skb
, &oldsp
->pd_lobby
);
5665 __skb_queue_tail(queue
, skb
);
5666 skb_set_owner_r(skb
, newsk
);
5670 /* Clear up any skbs waiting for the partial
5671 * delivery to finish.
5673 if (assoc
->ulpq
.pd_mode
)
5674 sctp_clear_pd(oldsk
);
5678 /* Set the type of socket to indicate that it is peeled off from the
5679 * original UDP-style socket or created with the accept() call on a
5680 * TCP-style socket..
5684 /* Mark the new socket "in-use" by the user so that any packets
5685 * that may arrive on the association after we've moved it are
5686 * queued to the backlog. This prevents a potential race between
5687 * backlog processing on the old socket and new-packet processing
5688 * on the new socket.
5690 sctp_lock_sock(newsk
);
5691 sctp_assoc_migrate(assoc
, newsk
);
5693 /* If the association on the newsk is already closed before accept()
5694 * is called, set RCV_SHUTDOWN flag.
5696 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
5697 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
5699 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
5700 sctp_release_sock(newsk
);
5703 /* This proto struct describes the ULP interface for SCTP. */
5704 struct proto sctp_prot
= {
5706 .owner
= THIS_MODULE
,
5707 .close
= sctp_close
,
5708 .connect
= sctp_connect
,
5709 .disconnect
= sctp_disconnect
,
5710 .accept
= sctp_accept
,
5711 .ioctl
= sctp_ioctl
,
5712 .init
= sctp_init_sock
,
5713 .destroy
= sctp_destroy_sock
,
5714 .shutdown
= sctp_shutdown
,
5715 .setsockopt
= sctp_setsockopt
,
5716 .getsockopt
= sctp_getsockopt
,
5717 .sendmsg
= sctp_sendmsg
,
5718 .recvmsg
= sctp_recvmsg
,
5720 .backlog_rcv
= sctp_backlog_rcv
,
5722 .unhash
= sctp_unhash
,
5723 .get_port
= sctp_get_port
,
5724 .obj_size
= sizeof(struct sctp_sock
),
5727 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5728 struct proto sctpv6_prot
= {
5730 .owner
= THIS_MODULE
,
5731 .close
= sctp_close
,
5732 .connect
= sctp_connect
,
5733 .disconnect
= sctp_disconnect
,
5734 .accept
= sctp_accept
,
5735 .ioctl
= sctp_ioctl
,
5736 .init
= sctp_init_sock
,
5737 .destroy
= sctp_destroy_sock
,
5738 .shutdown
= sctp_shutdown
,
5739 .setsockopt
= sctp_setsockopt
,
5740 .getsockopt
= sctp_getsockopt
,
5741 .sendmsg
= sctp_sendmsg
,
5742 .recvmsg
= sctp_recvmsg
,
5744 .backlog_rcv
= sctp_backlog_rcv
,
5746 .unhash
= sctp_unhash
,
5747 .get_port
= sctp_get_port
,
5748 .obj_size
= sizeof(struct sctp6_sock
),
5750 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */