1 /* SCTP kernel 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 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 * This SCTP 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 * This SCTP 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>
70 #include <linux/slab.h>
74 #include <net/route.h>
76 #include <net/inet_common.h>
78 #include <linux/socket.h> /* for sa_family_t */
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
83 /* WARNING: Please do not remove the SCTP_STATIC attribute to
84 * any of the functions below as they are used to export functions
85 * used by a project regression testsuite.
88 /* Forward declarations for internal helper functions. */
89 static int sctp_writeable(struct sock
*sk
);
90 static void sctp_wfree(struct sk_buff
*skb
);
91 static int sctp_wait_for_sndbuf(struct sctp_association
*, long *timeo_p
,
93 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
);
94 static int sctp_wait_for_connect(struct sctp_association
*, long *timeo_p
);
95 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
);
96 static void sctp_wait_for_close(struct sock
*sk
, long timeo
);
97 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
98 union sctp_addr
*addr
, int len
);
99 static int sctp_bindx_add(struct sock
*, struct sockaddr
*, int);
100 static int sctp_bindx_rem(struct sock
*, struct sockaddr
*, int);
101 static int sctp_send_asconf_add_ip(struct sock
*, struct sockaddr
*, int);
102 static int sctp_send_asconf_del_ip(struct sock
*, struct sockaddr
*, int);
103 static int sctp_send_asconf(struct sctp_association
*asoc
,
104 struct sctp_chunk
*chunk
);
105 static int sctp_do_bind(struct sock
*, union sctp_addr
*, int);
106 static int sctp_autobind(struct sock
*sk
);
107 static void sctp_sock_migrate(struct sock
*, struct sock
*,
108 struct sctp_association
*, sctp_socket_type_t
);
109 static char *sctp_hmac_alg
= SCTP_COOKIE_HMAC_ALG
;
111 extern struct kmem_cache
*sctp_bucket_cachep
;
112 extern int sysctl_sctp_mem
[3];
113 extern int sysctl_sctp_rmem
[3];
114 extern int sysctl_sctp_wmem
[3];
116 static int sctp_memory_pressure
;
117 static atomic_t sctp_memory_allocated
;
118 struct percpu_counter sctp_sockets_allocated
;
120 static void sctp_enter_memory_pressure(struct sock
*sk
)
122 sctp_memory_pressure
= 1;
126 /* Get the sndbuf space available at the time on the association. */
127 static inline int sctp_wspace(struct sctp_association
*asoc
)
131 if (asoc
->ep
->sndbuf_policy
)
132 amt
= asoc
->sndbuf_used
;
134 amt
= sk_wmem_alloc_get(asoc
->base
.sk
);
136 if (amt
>= asoc
->base
.sk
->sk_sndbuf
) {
137 if (asoc
->base
.sk
->sk_userlocks
& SOCK_SNDBUF_LOCK
)
140 amt
= sk_stream_wspace(asoc
->base
.sk
);
145 amt
= asoc
->base
.sk
->sk_sndbuf
- amt
;
150 /* Increment the used sndbuf space count of the corresponding association by
151 * the size of the outgoing data chunk.
152 * Also, set the skb destructor for sndbuf accounting later.
154 * Since it is always 1-1 between chunk and skb, and also a new skb is always
155 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
156 * destructor in the data chunk skb for the purpose of the sndbuf space
159 static inline void sctp_set_owner_w(struct sctp_chunk
*chunk
)
161 struct sctp_association
*asoc
= chunk
->asoc
;
162 struct sock
*sk
= asoc
->base
.sk
;
164 /* The sndbuf space is tracked per association. */
165 sctp_association_hold(asoc
);
167 skb_set_owner_w(chunk
->skb
, sk
);
169 chunk
->skb
->destructor
= sctp_wfree
;
170 /* Save the chunk pointer in skb for sctp_wfree to use later. */
171 *((struct sctp_chunk
**)(chunk
->skb
->cb
)) = chunk
;
173 asoc
->sndbuf_used
+= SCTP_DATA_SNDSIZE(chunk
) +
174 sizeof(struct sk_buff
) +
175 sizeof(struct sctp_chunk
);
177 atomic_add(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
178 sk
->sk_wmem_queued
+= chunk
->skb
->truesize
;
179 sk_mem_charge(sk
, chunk
->skb
->truesize
);
182 /* Verify that this is a valid address. */
183 static inline int sctp_verify_addr(struct sock
*sk
, union sctp_addr
*addr
,
188 /* Verify basic sockaddr. */
189 af
= sctp_sockaddr_af(sctp_sk(sk
), addr
, len
);
193 /* Is this a valid SCTP address? */
194 if (!af
->addr_valid(addr
, sctp_sk(sk
), NULL
))
197 if (!sctp_sk(sk
)->pf
->send_verify(sctp_sk(sk
), (addr
)))
203 /* Look up the association by its id. If this is not a UDP-style
204 * socket, the ID field is always ignored.
206 struct sctp_association
*sctp_id2assoc(struct sock
*sk
, sctp_assoc_t id
)
208 struct sctp_association
*asoc
= NULL
;
210 /* If this is not a UDP-style socket, assoc id should be ignored. */
211 if (!sctp_style(sk
, UDP
)) {
212 /* Return NULL if the socket state is not ESTABLISHED. It
213 * could be a TCP-style listening socket or a socket which
214 * hasn't yet called connect() to establish an association.
216 if (!sctp_sstate(sk
, ESTABLISHED
))
219 /* Get the first and the only association from the list. */
220 if (!list_empty(&sctp_sk(sk
)->ep
->asocs
))
221 asoc
= list_entry(sctp_sk(sk
)->ep
->asocs
.next
,
222 struct sctp_association
, asocs
);
226 /* Otherwise this is a UDP-style socket. */
227 if (!id
|| (id
== (sctp_assoc_t
)-1))
230 spin_lock_bh(&sctp_assocs_id_lock
);
231 asoc
= (struct sctp_association
*)idr_find(&sctp_assocs_id
, (int)id
);
232 spin_unlock_bh(&sctp_assocs_id_lock
);
234 if (!asoc
|| (asoc
->base
.sk
!= sk
) || asoc
->base
.dead
)
240 /* Look up the transport from an address and an assoc id. If both address and
241 * id are specified, the associations matching the address and the id should be
244 static struct sctp_transport
*sctp_addr_id2transport(struct sock
*sk
,
245 struct sockaddr_storage
*addr
,
248 struct sctp_association
*addr_asoc
= NULL
, *id_asoc
= NULL
;
249 struct sctp_transport
*transport
;
250 union sctp_addr
*laddr
= (union sctp_addr
*)addr
;
252 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
259 id_asoc
= sctp_id2assoc(sk
, id
);
260 if (id_asoc
&& (id_asoc
!= addr_asoc
))
263 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
264 (union sctp_addr
*)addr
);
269 /* API 3.1.2 bind() - UDP Style Syntax
270 * The syntax of bind() is,
272 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
274 * sd - the socket descriptor returned by socket().
275 * addr - the address structure (struct sockaddr_in or struct
276 * sockaddr_in6 [RFC 2553]),
277 * addr_len - the size of the address structure.
279 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
285 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
288 /* Disallow binding twice. */
289 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
290 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
295 sctp_release_sock(sk
);
300 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
302 /* Verify this is a valid sockaddr. */
303 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
304 union sctp_addr
*addr
, int len
)
308 /* Check minimum size. */
309 if (len
< sizeof (struct sockaddr
))
312 /* V4 mapped address are really of AF_INET family */
313 if (addr
->sa
.sa_family
== AF_INET6
&&
314 ipv6_addr_v4mapped(&addr
->v6
.sin6_addr
)) {
315 if (!opt
->pf
->af_supported(AF_INET
, opt
))
318 /* Does this PF support this AF? */
319 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
323 /* If we get this far, af is valid. */
324 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
326 if (len
< af
->sockaddr_len
)
332 /* Bind a local address either to an endpoint or to an association. */
333 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
335 struct sctp_sock
*sp
= sctp_sk(sk
);
336 struct sctp_endpoint
*ep
= sp
->ep
;
337 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
342 /* Common sockaddr verification. */
343 af
= sctp_sockaddr_af(sp
, addr
, len
);
345 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
350 snum
= ntohs(addr
->v4
.sin_port
);
352 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
353 ", port: %d, new port: %d, len: %d)\n",
359 /* PF specific bind() address verification. */
360 if (!sp
->pf
->bind_verify(sp
, addr
))
361 return -EADDRNOTAVAIL
;
363 /* We must either be unbound, or bind to the same port.
364 * It's OK to allow 0 ports if we are already bound.
365 * We'll just inhert an already bound port in this case
370 else if (snum
!= bp
->port
) {
371 SCTP_DEBUG_PRINTK("sctp_do_bind:"
372 " New port %d does not match existing port "
373 "%d.\n", snum
, bp
->port
);
378 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
381 /* See if the address matches any of the addresses we may have
382 * already bound before checking against other endpoints.
384 if (sctp_bind_addr_match(bp
, addr
, sp
))
387 /* Make sure we are allowed to bind here.
388 * The function sctp_get_port_local() does duplicate address
391 addr
->v4
.sin_port
= htons(snum
);
392 if ((ret
= sctp_get_port_local(sk
, addr
))) {
396 /* Refresh ephemeral port. */
398 bp
->port
= inet_sk(sk
)->inet_num
;
400 /* Add the address to the bind address list.
401 * Use GFP_ATOMIC since BHs will be disabled.
403 ret
= sctp_add_bind_addr(bp
, addr
, SCTP_ADDR_SRC
, GFP_ATOMIC
);
405 /* Copy back into socket for getsockname() use. */
407 inet_sk(sk
)->inet_sport
= htons(inet_sk(sk
)->inet_num
);
408 af
->to_sk_saddr(addr
, sk
);
414 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
416 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
417 * at any one time. If a sender, after sending an ASCONF chunk, decides
418 * it needs to transfer another ASCONF Chunk, it MUST wait until the
419 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
420 * subsequent ASCONF. Note this restriction binds each side, so at any
421 * time two ASCONF may be in-transit on any given association (one sent
422 * from each endpoint).
424 static int sctp_send_asconf(struct sctp_association
*asoc
,
425 struct sctp_chunk
*chunk
)
429 /* If there is an outstanding ASCONF chunk, queue it for later
432 if (asoc
->addip_last_asconf
) {
433 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
437 /* Hold the chunk until an ASCONF_ACK is received. */
438 sctp_chunk_hold(chunk
);
439 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
441 sctp_chunk_free(chunk
);
443 asoc
->addip_last_asconf
= chunk
;
449 /* Add a list of addresses as bind addresses to local endpoint or
452 * Basically run through each address specified in the addrs/addrcnt
453 * array/length pair, determine if it is IPv6 or IPv4 and call
454 * sctp_do_bind() on it.
456 * If any of them fails, then the operation will be reversed and the
457 * ones that were added will be removed.
459 * Only sctp_setsockopt_bindx() is supposed to call this function.
461 static int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
466 struct sockaddr
*sa_addr
;
469 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
473 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
474 /* The list may contain either IPv4 or IPv6 address;
475 * determine the address length for walking thru the list.
477 sa_addr
= (struct sockaddr
*)addr_buf
;
478 af
= sctp_get_af_specific(sa_addr
->sa_family
);
484 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
487 addr_buf
+= af
->sockaddr_len
;
491 /* Failed. Cleanup the ones that have been added */
493 sctp_bindx_rem(sk
, addrs
, cnt
);
501 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
502 * associations that are part of the endpoint indicating that a list of local
503 * addresses are added to the endpoint.
505 * If any of the addresses is already in the bind address list of the
506 * association, we do not send the chunk for that association. But it will not
507 * affect other associations.
509 * Only sctp_setsockopt_bindx() is supposed to call this function.
511 static int sctp_send_asconf_add_ip(struct sock
*sk
,
512 struct sockaddr
*addrs
,
515 struct sctp_sock
*sp
;
516 struct sctp_endpoint
*ep
;
517 struct sctp_association
*asoc
;
518 struct sctp_bind_addr
*bp
;
519 struct sctp_chunk
*chunk
;
520 struct sctp_sockaddr_entry
*laddr
;
521 union sctp_addr
*addr
;
522 union sctp_addr saveaddr
;
529 if (!sctp_addip_enable
)
535 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
536 __func__
, sk
, addrs
, addrcnt
);
538 list_for_each_entry(asoc
, &ep
->asocs
, asocs
) {
540 if (!asoc
->peer
.asconf_capable
)
543 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
546 if (!sctp_state(asoc
, ESTABLISHED
))
549 /* Check if any address in the packed array of addresses is
550 * in the bind address list of the association. If so,
551 * do not send the asconf chunk to its peer, but continue with
552 * other associations.
555 for (i
= 0; i
< addrcnt
; i
++) {
556 addr
= (union sctp_addr
*)addr_buf
;
557 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
563 if (sctp_assoc_lookup_laddr(asoc
, addr
))
566 addr_buf
+= af
->sockaddr_len
;
571 /* Use the first valid address in bind addr list of
572 * association as Address Parameter of ASCONF CHUNK.
574 bp
= &asoc
->base
.bind_addr
;
575 p
= bp
->address_list
.next
;
576 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
577 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
578 addrcnt
, SCTP_PARAM_ADD_IP
);
584 retval
= sctp_send_asconf(asoc
, chunk
);
588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
592 for (i
= 0; i
< addrcnt
; i
++) {
593 addr
= (union sctp_addr
*)addr_buf
;
594 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
595 memcpy(&saveaddr
, addr
, af
->sockaddr_len
);
596 retval
= sctp_add_bind_addr(bp
, &saveaddr
,
597 SCTP_ADDR_NEW
, GFP_ATOMIC
);
598 addr_buf
+= af
->sockaddr_len
;
606 /* Remove a list of addresses from bind addresses list. Do not remove the
609 * Basically run through each address specified in the addrs/addrcnt
610 * array/length pair, determine if it is IPv6 or IPv4 and call
611 * sctp_del_bind() on it.
613 * If any of them fails, then the operation will be reversed and the
614 * ones that were removed will be added back.
616 * At least one address has to be left; if only one address is
617 * available, the operation will return -EBUSY.
619 * Only sctp_setsockopt_bindx() is supposed to call this function.
621 static int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
623 struct sctp_sock
*sp
= sctp_sk(sk
);
624 struct sctp_endpoint
*ep
= sp
->ep
;
626 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
629 union sctp_addr
*sa_addr
;
632 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
636 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
637 /* If the bind address list is empty or if there is only one
638 * bind address, there is nothing more to be removed (we need
639 * at least one address here).
641 if (list_empty(&bp
->address_list
) ||
642 (sctp_list_single_entry(&bp
->address_list
))) {
647 sa_addr
= (union sctp_addr
*)addr_buf
;
648 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
654 if (!af
->addr_valid(sa_addr
, sp
, NULL
)) {
655 retval
= -EADDRNOTAVAIL
;
659 if (sa_addr
->v4
.sin_port
!= htons(bp
->port
)) {
664 retval
= sctp_del_bind_addr(bp
, sa_addr
);
666 addr_buf
+= af
->sockaddr_len
;
669 /* Failed. Add the ones that has been removed back */
671 sctp_bindx_add(sk
, addrs
, cnt
);
679 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
680 * the associations that are part of the endpoint indicating that a list of
681 * local addresses are removed from the endpoint.
683 * If any of the addresses is already in the bind address list of the
684 * association, we do not send the chunk for that association. But it will not
685 * affect other associations.
687 * Only sctp_setsockopt_bindx() is supposed to call this function.
689 static int sctp_send_asconf_del_ip(struct sock
*sk
,
690 struct sockaddr
*addrs
,
693 struct sctp_sock
*sp
;
694 struct sctp_endpoint
*ep
;
695 struct sctp_association
*asoc
;
696 struct sctp_transport
*transport
;
697 struct sctp_bind_addr
*bp
;
698 struct sctp_chunk
*chunk
;
699 union sctp_addr
*laddr
;
702 struct sctp_sockaddr_entry
*saddr
;
706 if (!sctp_addip_enable
)
712 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
713 __func__
, sk
, addrs
, addrcnt
);
715 list_for_each_entry(asoc
, &ep
->asocs
, asocs
) {
717 if (!asoc
->peer
.asconf_capable
)
720 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
723 if (!sctp_state(asoc
, ESTABLISHED
))
726 /* Check if any address in the packed array of addresses is
727 * not present in the bind address list of the association.
728 * If so, do not send the asconf chunk to its peer, but
729 * continue with other associations.
732 for (i
= 0; i
< addrcnt
; i
++) {
733 laddr
= (union sctp_addr
*)addr_buf
;
734 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
740 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
743 addr_buf
+= af
->sockaddr_len
;
748 /* Find one address in the association's bind address list
749 * that is not in the packed array of addresses. This is to
750 * make sure that we do not delete all the addresses in the
753 bp
= &asoc
->base
.bind_addr
;
754 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
759 /* We do not need RCU protection throughout this loop
760 * because this is done under a socket lock from the
763 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
770 /* Reset use_as_src flag for the addresses in the bind address
771 * list that are to be deleted.
774 for (i
= 0; i
< addrcnt
; i
++) {
775 laddr
= (union sctp_addr
*)addr_buf
;
776 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
777 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
778 if (sctp_cmp_addr_exact(&saddr
->a
, laddr
))
779 saddr
->state
= SCTP_ADDR_DEL
;
781 addr_buf
+= af
->sockaddr_len
;
784 /* Update the route and saddr entries for all the transports
785 * as some of the addresses in the bind address list are
786 * about to be deleted and cannot be used as source addresses.
788 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
790 dst_release(transport
->dst
);
791 sctp_transport_route(transport
, NULL
,
792 sctp_sk(asoc
->base
.sk
));
795 retval
= sctp_send_asconf(asoc
, chunk
);
801 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
804 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
807 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
808 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
811 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
812 * Section 3.1.2 for this usage.
814 * addrs is a pointer to an array of one or more socket addresses. Each
815 * address is contained in its appropriate structure (i.e. struct
816 * sockaddr_in or struct sockaddr_in6) the family of the address type
817 * must be used to distinguish the address length (note that this
818 * representation is termed a "packed array" of addresses). The caller
819 * specifies the number of addresses in the array with addrcnt.
821 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
822 * -1, and sets errno to the appropriate error code.
824 * For SCTP, the port given in each socket address must be the same, or
825 * sctp_bindx() will fail, setting errno to EINVAL.
827 * The flags parameter is formed from the bitwise OR of zero or more of
828 * the following currently defined flags:
830 * SCTP_BINDX_ADD_ADDR
832 * SCTP_BINDX_REM_ADDR
834 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
835 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
836 * addresses from the association. The two flags are mutually exclusive;
837 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
838 * not remove all addresses from an association; sctp_bindx() will
839 * reject such an attempt with EINVAL.
841 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
842 * additional addresses with an endpoint after calling bind(). Or use
843 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
844 * socket is associated with so that no new association accepted will be
845 * associated with those addresses. If the endpoint supports dynamic
846 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
847 * endpoint to send the appropriate message to the peer to change the
848 * peers address lists.
850 * Adding and removing addresses from a connected association is
851 * optional functionality. Implementations that do not support this
852 * functionality should return EOPNOTSUPP.
854 * Basically do nothing but copying the addresses from user to kernel
855 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
856 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
859 * We don't use copy_from_user() for optimization: we first do the
860 * sanity checks (buffer size -fast- and access check-healthy
861 * pointer); if all of those succeed, then we can alloc the memory
862 * (expensive operation) needed to copy the data to kernel. Then we do
863 * the copying without checking the user space area
864 * (__copy_from_user()).
866 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
869 * sk The sk of the socket
870 * addrs The pointer to the addresses in user land
871 * addrssize Size of the addrs buffer
872 * op Operation to perform (add or remove, see the flags of
875 * Returns 0 if ok, <0 errno code on error.
877 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
878 struct sockaddr __user
*addrs
,
879 int addrs_size
, int op
)
881 struct sockaddr
*kaddrs
;
885 struct sockaddr
*sa_addr
;
889 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
890 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
892 if (unlikely(addrs_size
<= 0))
895 /* Check the user passed a healthy pointer. */
896 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
899 /* Alloc space for the address array in kernel memory. */
900 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
901 if (unlikely(!kaddrs
))
904 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
909 /* Walk through the addrs buffer and count the number of addresses. */
911 while (walk_size
< addrs_size
) {
912 if (walk_size
+ sizeof(sa_family_t
) > addrs_size
) {
917 sa_addr
= (struct sockaddr
*)addr_buf
;
918 af
= sctp_get_af_specific(sa_addr
->sa_family
);
920 /* If the address family is not supported or if this address
921 * causes the address buffer to overflow return EINVAL.
923 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
928 addr_buf
+= af
->sockaddr_len
;
929 walk_size
+= af
->sockaddr_len
;
934 case SCTP_BINDX_ADD_ADDR
:
935 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
938 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
941 case SCTP_BINDX_REM_ADDR
:
942 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
945 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
959 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
961 * Common routine for handling connect() and sctp_connectx().
962 * Connect will come in with just a single address.
964 static int __sctp_connect(struct sock
* sk
,
965 struct sockaddr
*kaddrs
,
967 sctp_assoc_t
*assoc_id
)
969 struct sctp_sock
*sp
;
970 struct sctp_endpoint
*ep
;
971 struct sctp_association
*asoc
= NULL
;
972 struct sctp_association
*asoc2
;
973 struct sctp_transport
*transport
;
981 union sctp_addr
*sa_addr
= NULL
;
984 unsigned int f_flags
= 0;
989 /* connect() cannot be done on a socket that is already in ESTABLISHED
990 * state - UDP-style peeled off socket or a TCP-style socket that
991 * is already connected.
992 * It cannot be done even on a TCP-style listening socket.
994 if (sctp_sstate(sk
, ESTABLISHED
) ||
995 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
1000 /* Walk through the addrs buffer and count the number of addresses. */
1002 while (walk_size
< addrs_size
) {
1003 if (walk_size
+ sizeof(sa_family_t
) > addrs_size
) {
1008 sa_addr
= (union sctp_addr
*)addr_buf
;
1009 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
1011 /* If the address family is not supported or if this address
1012 * causes the address buffer to overflow return EINVAL.
1014 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
1019 port
= ntohs(sa_addr
->v4
.sin_port
);
1021 /* Save current address so we can work with it */
1022 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
1024 err
= sctp_verify_addr(sk
, &to
, af
->sockaddr_len
);
1028 /* Make sure the destination port is correctly set
1031 if (asoc
&& asoc
->peer
.port
&& asoc
->peer
.port
!= port
)
1035 /* Check if there already is a matching association on the
1036 * endpoint (other than the one created here).
1038 asoc2
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1039 if (asoc2
&& asoc2
!= asoc
) {
1040 if (asoc2
->state
>= SCTP_STATE_ESTABLISHED
)
1047 /* If we could not find a matching association on the endpoint,
1048 * make sure that there is no peeled-off association matching
1049 * the peer address even on another socket.
1051 if (sctp_endpoint_is_peeled_off(ep
, &to
)) {
1052 err
= -EADDRNOTAVAIL
;
1057 /* If a bind() or sctp_bindx() is not called prior to
1058 * an sctp_connectx() call, the system picks an
1059 * ephemeral port and will choose an address set
1060 * equivalent to binding with a wildcard address.
1062 if (!ep
->base
.bind_addr
.port
) {
1063 if (sctp_autobind(sk
)) {
1069 * If an unprivileged user inherits a 1-many
1070 * style socket with open associations on a
1071 * privileged port, it MAY be permitted to
1072 * accept new associations, but it SHOULD NOT
1073 * be permitted to open new associations.
1075 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1076 !capable(CAP_NET_BIND_SERVICE
)) {
1082 scope
= sctp_scope(&to
);
1083 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1089 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, scope
,
1097 /* Prime the peer's transport structures. */
1098 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
,
1106 addr_buf
+= af
->sockaddr_len
;
1107 walk_size
+= af
->sockaddr_len
;
1110 /* In case the user of sctp_connectx() wants an association
1111 * id back, assign one now.
1114 err
= sctp_assoc_set_id(asoc
, GFP_KERNEL
);
1119 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1124 /* Initialize sk's dport and daddr for getpeername() */
1125 inet_sk(sk
)->inet_dport
= htons(asoc
->peer
.port
);
1126 af
= sctp_get_af_specific(sa_addr
->sa
.sa_family
);
1127 af
->to_sk_daddr(sa_addr
, sk
);
1130 /* in-kernel sockets don't generally have a file allocated to them
1131 * if all they do is call sock_create_kern().
1133 if (sk
->sk_socket
->file
)
1134 f_flags
= sk
->sk_socket
->file
->f_flags
;
1136 timeo
= sock_sndtimeo(sk
, f_flags
& O_NONBLOCK
);
1138 err
= sctp_wait_for_connect(asoc
, &timeo
);
1139 if ((err
== 0 || err
== -EINPROGRESS
) && assoc_id
)
1140 *assoc_id
= asoc
->assoc_id
;
1142 /* Don't free association on exit. */
1147 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1148 " kaddrs: %p err: %d\n",
1151 sctp_association_free(asoc
);
1155 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1158 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1159 * sctp_assoc_t *asoc);
1161 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1162 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1163 * or IPv6 addresses.
1165 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1166 * Section 3.1.2 for this usage.
1168 * addrs is a pointer to an array of one or more socket addresses. Each
1169 * address is contained in its appropriate structure (i.e. struct
1170 * sockaddr_in or struct sockaddr_in6) the family of the address type
1171 * must be used to distengish the address length (note that this
1172 * representation is termed a "packed array" of addresses). The caller
1173 * specifies the number of addresses in the array with addrcnt.
1175 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1176 * the association id of the new association. On failure, sctp_connectx()
1177 * returns -1, and sets errno to the appropriate error code. The assoc_id
1178 * is not touched by the kernel.
1180 * For SCTP, the port given in each socket address must be the same, or
1181 * sctp_connectx() will fail, setting errno to EINVAL.
1183 * An application can use sctp_connectx to initiate an association with
1184 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1185 * allows a caller to specify multiple addresses at which a peer can be
1186 * reached. The way the SCTP stack uses the list of addresses to set up
1187 * the association is implementation dependant. This function only
1188 * specifies that the stack will try to make use of all the addresses in
1189 * the list when needed.
1191 * Note that the list of addresses passed in is only used for setting up
1192 * the association. It does not necessarily equal the set of addresses
1193 * the peer uses for the resulting association. If the caller wants to
1194 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1195 * retrieve them after the association has been set up.
1197 * Basically do nothing but copying the addresses from user to kernel
1198 * land and invoking either sctp_connectx(). This is used for tunneling
1199 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1201 * We don't use copy_from_user() for optimization: we first do the
1202 * sanity checks (buffer size -fast- and access check-healthy
1203 * pointer); if all of those succeed, then we can alloc the memory
1204 * (expensive operation) needed to copy the data to kernel. Then we do
1205 * the copying without checking the user space area
1206 * (__copy_from_user()).
1208 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1211 * sk The sk of the socket
1212 * addrs The pointer to the addresses in user land
1213 * addrssize Size of the addrs buffer
1215 * Returns >=0 if ok, <0 errno code on error.
1217 SCTP_STATIC
int __sctp_setsockopt_connectx(struct sock
* sk
,
1218 struct sockaddr __user
*addrs
,
1220 sctp_assoc_t
*assoc_id
)
1223 struct sockaddr
*kaddrs
;
1225 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1226 __func__
, sk
, addrs
, addrs_size
);
1228 if (unlikely(addrs_size
<= 0))
1231 /* Check the user passed a healthy pointer. */
1232 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1235 /* Alloc space for the address array in kernel memory. */
1236 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1237 if (unlikely(!kaddrs
))
1240 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1243 err
= __sctp_connect(sk
, kaddrs
, addrs_size
, assoc_id
);
1252 * This is an older interface. It's kept for backward compatibility
1253 * to the option that doesn't provide association id.
1255 SCTP_STATIC
int sctp_setsockopt_connectx_old(struct sock
* sk
,
1256 struct sockaddr __user
*addrs
,
1259 return __sctp_setsockopt_connectx(sk
, addrs
, addrs_size
, NULL
);
1263 * New interface for the API. The since the API is done with a socket
1264 * option, to make it simple we feed back the association id is as a return
1265 * indication to the call. Error is always negative and association id is
1268 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1269 struct sockaddr __user
*addrs
,
1272 sctp_assoc_t assoc_id
= 0;
1275 err
= __sctp_setsockopt_connectx(sk
, addrs
, addrs_size
, &assoc_id
);
1284 * New (hopefully final) interface for the API.
1285 * We use the sctp_getaddrs_old structure so that use-space library
1286 * can avoid any unnecessary allocations. The only defferent part
1287 * is that we store the actual length of the address buffer into the
1288 * addrs_num structure member. That way we can re-use the existing
1291 SCTP_STATIC
int sctp_getsockopt_connectx3(struct sock
* sk
, int len
,
1292 char __user
*optval
,
1295 struct sctp_getaddrs_old param
;
1296 sctp_assoc_t assoc_id
= 0;
1299 if (len
< sizeof(param
))
1302 if (copy_from_user(¶m
, optval
, sizeof(param
)))
1305 err
= __sctp_setsockopt_connectx(sk
,
1306 (struct sockaddr __user
*)param
.addrs
,
1307 param
.addr_num
, &assoc_id
);
1309 if (err
== 0 || err
== -EINPROGRESS
) {
1310 if (copy_to_user(optval
, &assoc_id
, sizeof(assoc_id
)))
1312 if (put_user(sizeof(assoc_id
), optlen
))
1319 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1321 struct sctp_endpoint
*ep
;
1322 struct sctp_association
*asoc
;
1323 struct list_head
*pos
, *temp
;
1325 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1328 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1329 sk
->sk_state
= SCTP_SS_CLOSING
;
1331 ep
= sctp_sk(sk
)->ep
;
1333 /* Walk all associations on an endpoint. */
1334 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1335 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1337 if (sctp_style(sk
, TCP
)) {
1338 /* A closed association can still be in the list if
1339 * it belongs to a TCP-style listening socket that is
1340 * not yet accepted. If so, free it. If not, send an
1341 * ABORT or SHUTDOWN based on the linger options.
1343 if (sctp_state(asoc
, CLOSED
)) {
1344 sctp_unhash_established(asoc
);
1345 sctp_association_free(asoc
);
1350 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
) {
1351 struct sctp_chunk
*chunk
;
1353 chunk
= sctp_make_abort_user(asoc
, NULL
, 0);
1355 sctp_primitive_ABORT(asoc
, chunk
);
1357 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1360 /* Clean up any skbs sitting on the receive queue. */
1361 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1362 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1364 /* On a TCP-style socket, block for at most linger_time if set. */
1365 if (sctp_style(sk
, TCP
) && timeout
)
1366 sctp_wait_for_close(sk
, timeout
);
1368 /* This will run the backlog queue. */
1369 sctp_release_sock(sk
);
1371 /* Supposedly, no process has access to the socket, but
1372 * the net layers still may.
1374 sctp_local_bh_disable();
1375 sctp_bh_lock_sock(sk
);
1377 /* Hold the sock, since sk_common_release() will put sock_put()
1378 * and we have just a little more cleanup.
1381 sk_common_release(sk
);
1383 sctp_bh_unlock_sock(sk
);
1384 sctp_local_bh_enable();
1388 SCTP_DBG_OBJCNT_DEC(sock
);
1391 /* Handle EPIPE error. */
1392 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1395 err
= sock_error(sk
) ? : -EPIPE
;
1396 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1397 send_sig(SIGPIPE
, current
, 0);
1401 /* API 3.1.3 sendmsg() - UDP Style Syntax
1403 * An application uses sendmsg() and recvmsg() calls to transmit data to
1404 * and receive data from its peer.
1406 * ssize_t sendmsg(int socket, const struct msghdr *message,
1409 * socket - the socket descriptor of the endpoint.
1410 * message - pointer to the msghdr structure which contains a single
1411 * user message and possibly some ancillary data.
1413 * See Section 5 for complete description of the data
1416 * flags - flags sent or received with the user message, see Section
1417 * 5 for complete description of the flags.
1419 * Note: This function could use a rewrite especially when explicit
1420 * connect support comes in.
1422 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1424 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1426 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1427 struct msghdr
*msg
, size_t msg_len
)
1429 struct sctp_sock
*sp
;
1430 struct sctp_endpoint
*ep
;
1431 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1432 struct sctp_transport
*transport
, *chunk_tp
;
1433 struct sctp_chunk
*chunk
;
1435 struct sockaddr
*msg_name
= NULL
;
1436 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1437 struct sctp_sndrcvinfo
*sinfo
;
1438 struct sctp_initmsg
*sinit
;
1439 sctp_assoc_t associd
= 0;
1440 sctp_cmsgs_t cmsgs
= { NULL
};
1444 __u16 sinfo_flags
= 0;
1445 struct sctp_datamsg
*datamsg
;
1446 int msg_flags
= msg
->msg_flags
;
1448 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1455 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1457 /* We cannot send a message over a TCP-style listening socket. */
1458 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1463 /* Parse out the SCTP CMSGs. */
1464 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1467 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1471 /* Fetch the destination address for this packet. This
1472 * address only selects the association--it is not necessarily
1473 * the address we will send to.
1474 * For a peeled-off socket, msg_name is ignored.
1476 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1477 int msg_namelen
= msg
->msg_namelen
;
1479 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1484 if (msg_namelen
> sizeof(to
))
1485 msg_namelen
= sizeof(to
);
1486 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1487 msg_name
= msg
->msg_name
;
1493 /* Did the user specify SNDRCVINFO? */
1495 sinfo_flags
= sinfo
->sinfo_flags
;
1496 associd
= sinfo
->sinfo_assoc_id
;
1499 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1500 msg_len
, sinfo_flags
);
1502 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1503 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1508 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1509 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1510 * If SCTP_ABORT is set, the message length could be non zero with
1511 * the msg_iov set to the user abort reason.
1513 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1514 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1519 /* If SCTP_ADDR_OVER is set, there must be an address
1520 * specified in msg_name.
1522 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1529 SCTP_DEBUG_PRINTK("About to look up association.\n");
1533 /* If a msg_name has been specified, assume this is to be used. */
1535 /* Look for a matching association on the endpoint. */
1536 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1538 /* If we could not find a matching association on the
1539 * endpoint, make sure that it is not a TCP-style
1540 * socket that already has an association or there is
1541 * no peeled-off association on another socket.
1543 if ((sctp_style(sk
, TCP
) &&
1544 sctp_sstate(sk
, ESTABLISHED
)) ||
1545 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1546 err
= -EADDRNOTAVAIL
;
1551 asoc
= sctp_id2assoc(sk
, associd
);
1559 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1561 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1562 * socket that has an association in CLOSED state. This can
1563 * happen when an accepted socket has an association that is
1566 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1571 if (sinfo_flags
& SCTP_EOF
) {
1572 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1574 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1578 if (sinfo_flags
& SCTP_ABORT
) {
1580 chunk
= sctp_make_abort_user(asoc
, msg
, msg_len
);
1586 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1587 sctp_primitive_ABORT(asoc
, chunk
);
1593 /* Do we need to create the association? */
1595 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1597 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1602 /* Check for invalid stream against the stream counts,
1603 * either the default or the user specified stream counts.
1606 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1607 /* Check against the defaults. */
1608 if (sinfo
->sinfo_stream
>=
1609 sp
->initmsg
.sinit_num_ostreams
) {
1614 /* Check against the requested. */
1615 if (sinfo
->sinfo_stream
>=
1616 sinit
->sinit_num_ostreams
) {
1624 * API 3.1.2 bind() - UDP Style Syntax
1625 * If a bind() or sctp_bindx() is not called prior to a
1626 * sendmsg() call that initiates a new association, the
1627 * system picks an ephemeral port and will choose an address
1628 * set equivalent to binding with a wildcard address.
1630 if (!ep
->base
.bind_addr
.port
) {
1631 if (sctp_autobind(sk
)) {
1637 * If an unprivileged user inherits a one-to-many
1638 * style socket with open associations on a privileged
1639 * port, it MAY be permitted to accept new associations,
1640 * but it SHOULD NOT be permitted to open new
1643 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1644 !capable(CAP_NET_BIND_SERVICE
)) {
1650 scope
= sctp_scope(&to
);
1651 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1657 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, scope
, GFP_KERNEL
);
1663 /* If the SCTP_INIT ancillary data is specified, set all
1664 * the association init values accordingly.
1667 if (sinit
->sinit_num_ostreams
) {
1668 asoc
->c
.sinit_num_ostreams
=
1669 sinit
->sinit_num_ostreams
;
1671 if (sinit
->sinit_max_instreams
) {
1672 asoc
->c
.sinit_max_instreams
=
1673 sinit
->sinit_max_instreams
;
1675 if (sinit
->sinit_max_attempts
) {
1676 asoc
->max_init_attempts
1677 = sinit
->sinit_max_attempts
;
1679 if (sinit
->sinit_max_init_timeo
) {
1680 asoc
->max_init_timeo
=
1681 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1685 /* Prime the peer's transport structures. */
1686 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1693 /* ASSERT: we have a valid association at this point. */
1694 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1697 /* If the user didn't specify SNDRCVINFO, make up one with
1700 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1701 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1702 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1703 default_sinfo
.sinfo_context
= asoc
->default_context
;
1704 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1705 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1706 sinfo
= &default_sinfo
;
1709 /* API 7.1.7, the sndbuf size per association bounds the
1710 * maximum size of data that can be sent in a single send call.
1712 if (msg_len
> sk
->sk_sndbuf
) {
1717 if (asoc
->pmtu_pending
)
1718 sctp_assoc_pending_pmtu(asoc
);
1720 /* If fragmentation is disabled and the message length exceeds the
1721 * association fragmentation point, return EMSGSIZE. The I-D
1722 * does not specify what this error is, but this looks like
1725 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1731 /* Check for invalid stream. */
1732 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1738 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1739 if (!sctp_wspace(asoc
)) {
1740 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1745 /* If an address is passed with the sendto/sendmsg call, it is used
1746 * to override the primary destination address in the TCP model, or
1747 * when SCTP_ADDR_OVER flag is set in the UDP model.
1749 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1750 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1751 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1759 /* Auto-connect, if we aren't connected already. */
1760 if (sctp_state(asoc
, CLOSED
)) {
1761 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1764 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1767 /* Break the message into multiple chunks of maximum size. */
1768 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1774 /* Now send the (possibly) fragmented message. */
1775 list_for_each_entry(chunk
, &datamsg
->chunks
, frag_list
) {
1776 sctp_chunk_hold(chunk
);
1778 /* Do accounting for the write space. */
1779 sctp_set_owner_w(chunk
);
1781 chunk
->transport
= chunk_tp
;
1784 /* Send it to the lower layers. Note: all chunks
1785 * must either fail or succeed. The lower layer
1786 * works that way today. Keep it that way or this
1789 err
= sctp_primitive_SEND(asoc
, datamsg
);
1790 /* Did the lower layer accept the chunk? */
1792 sctp_datamsg_free(datamsg
);
1794 sctp_datamsg_put(datamsg
);
1796 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1803 /* If we are already past ASSOCIATE, the lower
1804 * layers are responsible for association cleanup.
1810 sctp_association_free(asoc
);
1812 sctp_release_sock(sk
);
1815 return sctp_error(sk
, msg_flags
, err
);
1819 /* This is an extended version of skb_pull() that removes the data from the
1820 * start of a skb even when data is spread across the list of skb's in the
1821 * frag_list. len specifies the total amount of data that needs to be removed.
1822 * when 'len' bytes could be removed from the skb, it returns 0.
1823 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1824 * could not be removed.
1826 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1828 struct sk_buff
*list
;
1829 int skb_len
= skb_headlen(skb
);
1832 if (len
<= skb_len
) {
1833 __skb_pull(skb
, len
);
1837 __skb_pull(skb
, skb_len
);
1839 skb_walk_frags(skb
, list
) {
1840 rlen
= sctp_skb_pull(list
, len
);
1841 skb
->len
-= (len
-rlen
);
1842 skb
->data_len
-= (len
-rlen
);
1853 /* API 3.1.3 recvmsg() - UDP Style Syntax
1855 * ssize_t recvmsg(int socket, struct msghdr *message,
1858 * socket - the socket descriptor of the endpoint.
1859 * message - pointer to the msghdr structure which contains a single
1860 * user message and possibly some ancillary data.
1862 * See Section 5 for complete description of the data
1865 * flags - flags sent or received with the user message, see Section
1866 * 5 for complete description of the flags.
1868 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1870 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1871 struct msghdr
*msg
, size_t len
, int noblock
,
1872 int flags
, int *addr_len
)
1874 struct sctp_ulpevent
*event
= NULL
;
1875 struct sctp_sock
*sp
= sctp_sk(sk
);
1876 struct sk_buff
*skb
;
1881 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1882 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1883 "len", len
, "knoblauch", noblock
,
1884 "flags", flags
, "addr_len", addr_len
);
1888 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1893 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1897 /* Get the total length of the skb including any skb's in the
1906 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1908 event
= sctp_skb2event(skb
);
1913 sock_recv_ts_and_drops(msg
, sk
, skb
);
1914 if (sctp_ulpevent_is_notification(event
)) {
1915 msg
->msg_flags
|= MSG_NOTIFICATION
;
1916 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1918 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1921 /* Check if we allow SCTP_SNDRCVINFO. */
1922 if (sp
->subscribe
.sctp_data_io_event
)
1923 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1927 /* If skb's length exceeds the user's buffer, update the skb and
1928 * push it back to the receive_queue so that the next call to
1929 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1931 if (skb_len
> copied
) {
1932 msg
->msg_flags
&= ~MSG_EOR
;
1933 if (flags
& MSG_PEEK
)
1935 sctp_skb_pull(skb
, copied
);
1936 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1938 /* When only partial message is copied to the user, increase
1939 * rwnd by that amount. If all the data in the skb is read,
1940 * rwnd is updated when the event is freed.
1942 if (!sctp_ulpevent_is_notification(event
))
1943 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1945 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1946 (event
->msg_flags
& MSG_EOR
))
1947 msg
->msg_flags
|= MSG_EOR
;
1949 msg
->msg_flags
&= ~MSG_EOR
;
1952 if (flags
& MSG_PEEK
) {
1953 /* Release the skb reference acquired after peeking the skb in
1954 * sctp_skb_recv_datagram().
1958 /* Free the event which includes releasing the reference to
1959 * the owner of the skb, freeing the skb and updating the
1962 sctp_ulpevent_free(event
);
1965 sctp_release_sock(sk
);
1969 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1971 * This option is a on/off flag. If enabled no SCTP message
1972 * fragmentation will be performed. Instead if a message being sent
1973 * exceeds the current PMTU size, the message will NOT be sent and
1974 * instead a error will be indicated to the user.
1976 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1977 char __user
*optval
,
1978 unsigned int optlen
)
1982 if (optlen
< sizeof(int))
1985 if (get_user(val
, (int __user
*)optval
))
1988 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1993 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1994 unsigned int optlen
)
1996 if (optlen
> sizeof(struct sctp_event_subscribe
))
1998 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
2003 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2005 * This socket option is applicable to the UDP-style socket only. When
2006 * set it will cause associations that are idle for more than the
2007 * specified number of seconds to automatically close. An association
2008 * being idle is defined an association that has NOT sent or received
2009 * user data. The special value of '0' indicates that no automatic
2010 * close of any associations should be performed. The option expects an
2011 * integer defining the number of seconds of idle time before an
2012 * association is closed.
2014 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
2015 unsigned int optlen
)
2017 struct sctp_sock
*sp
= sctp_sk(sk
);
2019 /* Applicable to UDP-style socket only */
2020 if (sctp_style(sk
, TCP
))
2022 if (optlen
!= sizeof(int))
2024 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
2026 /* make sure it won't exceed MAX_SCHEDULE_TIMEOUT */
2027 sp
->autoclose
= min_t(long, sp
->autoclose
, MAX_SCHEDULE_TIMEOUT
/ HZ
);
2032 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2034 * Applications can enable or disable heartbeats for any peer address of
2035 * an association, modify an address's heartbeat interval, force a
2036 * heartbeat to be sent immediately, and adjust the address's maximum
2037 * number of retransmissions sent before an address is considered
2038 * unreachable. The following structure is used to access and modify an
2039 * address's parameters:
2041 * struct sctp_paddrparams {
2042 * sctp_assoc_t spp_assoc_id;
2043 * struct sockaddr_storage spp_address;
2044 * uint32_t spp_hbinterval;
2045 * uint16_t spp_pathmaxrxt;
2046 * uint32_t spp_pathmtu;
2047 * uint32_t spp_sackdelay;
2048 * uint32_t spp_flags;
2051 * spp_assoc_id - (one-to-many style socket) This is filled in the
2052 * application, and identifies the association for
2054 * spp_address - This specifies which address is of interest.
2055 * spp_hbinterval - This contains the value of the heartbeat interval,
2056 * in milliseconds. If a value of zero
2057 * is present in this field then no changes are to
2058 * be made to this parameter.
2059 * spp_pathmaxrxt - This contains the maximum number of
2060 * retransmissions before this address shall be
2061 * considered unreachable. If a value of zero
2062 * is present in this field then no changes are to
2063 * be made to this parameter.
2064 * spp_pathmtu - When Path MTU discovery is disabled the value
2065 * specified here will be the "fixed" path mtu.
2066 * Note that if the spp_address field is empty
2067 * then all associations on this address will
2068 * have this fixed path mtu set upon them.
2070 * spp_sackdelay - When delayed sack is enabled, this value specifies
2071 * the number of milliseconds that sacks will be delayed
2072 * for. This value will apply to all addresses of an
2073 * association if the spp_address field is empty. Note
2074 * also, that if delayed sack is enabled and this
2075 * value is set to 0, no change is made to the last
2076 * recorded delayed sack timer value.
2078 * spp_flags - These flags are used to control various features
2079 * on an association. The flag field may contain
2080 * zero or more of the following options.
2082 * SPP_HB_ENABLE - Enable heartbeats on the
2083 * specified address. Note that if the address
2084 * field is empty all addresses for the association
2085 * have heartbeats enabled upon them.
2087 * SPP_HB_DISABLE - Disable heartbeats on the
2088 * speicifed address. Note that if the address
2089 * field is empty all addresses for the association
2090 * will have their heartbeats disabled. Note also
2091 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2092 * mutually exclusive, only one of these two should
2093 * be specified. Enabling both fields will have
2094 * undetermined results.
2096 * SPP_HB_DEMAND - Request a user initiated heartbeat
2097 * to be made immediately.
2099 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2100 * heartbeat delayis to be set to the value of 0
2103 * SPP_PMTUD_ENABLE - This field will enable PMTU
2104 * discovery upon the specified address. Note that
2105 * if the address feild is empty then all addresses
2106 * on the association are effected.
2108 * SPP_PMTUD_DISABLE - This field will disable PMTU
2109 * discovery upon the specified address. Note that
2110 * if the address feild is empty then all addresses
2111 * on the association are effected. Not also that
2112 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2113 * exclusive. Enabling both will have undetermined
2116 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2117 * on delayed sack. The time specified in spp_sackdelay
2118 * is used to specify the sack delay for this address. Note
2119 * that if spp_address is empty then all addresses will
2120 * enable delayed sack and take on the sack delay
2121 * value specified in spp_sackdelay.
2122 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2123 * off delayed sack. If the spp_address field is blank then
2124 * delayed sack is disabled for the entire association. Note
2125 * also that this field is mutually exclusive to
2126 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2129 static int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2130 struct sctp_transport
*trans
,
2131 struct sctp_association
*asoc
,
2132 struct sctp_sock
*sp
,
2135 int sackdelay_change
)
2139 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2140 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2145 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2146 * this field is ignored. Note also that a value of zero indicates
2147 * the current setting should be left unchanged.
2149 if (params
->spp_flags
& SPP_HB_ENABLE
) {
2151 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2152 * set. This lets us use 0 value when this flag
2155 if (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)
2156 params
->spp_hbinterval
= 0;
2158 if (params
->spp_hbinterval
||
2159 (params
->spp_flags
& SPP_HB_TIME_IS_ZERO
)) {
2162 msecs_to_jiffies(params
->spp_hbinterval
);
2165 msecs_to_jiffies(params
->spp_hbinterval
);
2167 sp
->hbinterval
= params
->spp_hbinterval
;
2174 trans
->param_flags
=
2175 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2178 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2181 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2185 /* When Path MTU discovery is disabled the value specified here will
2186 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2187 * include the flag SPP_PMTUD_DISABLE for this field to have any
2190 if ((params
->spp_flags
& SPP_PMTUD_DISABLE
) && params
->spp_pathmtu
) {
2192 trans
->pathmtu
= params
->spp_pathmtu
;
2193 sctp_assoc_sync_pmtu(asoc
);
2195 asoc
->pathmtu
= params
->spp_pathmtu
;
2196 sctp_frag_point(asoc
, params
->spp_pathmtu
);
2198 sp
->pathmtu
= params
->spp_pathmtu
;
2204 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2205 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2206 trans
->param_flags
=
2207 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2209 sctp_transport_pmtu(trans
);
2210 sctp_assoc_sync_pmtu(asoc
);
2214 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2217 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2221 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2222 * value of this field is ignored. Note also that a value of zero
2223 * indicates the current setting should be left unchanged.
2225 if ((params
->spp_flags
& SPP_SACKDELAY_ENABLE
) && params
->spp_sackdelay
) {
2228 msecs_to_jiffies(params
->spp_sackdelay
);
2231 msecs_to_jiffies(params
->spp_sackdelay
);
2233 sp
->sackdelay
= params
->spp_sackdelay
;
2237 if (sackdelay_change
) {
2239 trans
->param_flags
=
2240 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2244 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2248 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2253 /* Note that a value of zero indicates the current setting should be
2256 if (params
->spp_pathmaxrxt
) {
2258 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2260 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2262 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2269 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2270 char __user
*optval
,
2271 unsigned int optlen
)
2273 struct sctp_paddrparams params
;
2274 struct sctp_transport
*trans
= NULL
;
2275 struct sctp_association
*asoc
= NULL
;
2276 struct sctp_sock
*sp
= sctp_sk(sk
);
2278 int hb_change
, pmtud_change
, sackdelay_change
;
2280 if (optlen
!= sizeof(struct sctp_paddrparams
))
2283 if (copy_from_user(¶ms
, optval
, optlen
))
2286 /* Validate flags and value parameters. */
2287 hb_change
= params
.spp_flags
& SPP_HB
;
2288 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2289 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2291 if (hb_change
== SPP_HB
||
2292 pmtud_change
== SPP_PMTUD
||
2293 sackdelay_change
== SPP_SACKDELAY
||
2294 params
.spp_sackdelay
> 500 ||
2295 (params
.spp_pathmtu
&&
2296 params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2299 /* If an address other than INADDR_ANY is specified, and
2300 * no transport is found, then the request is invalid.
2302 if (!sctp_is_any(sk
, ( union sctp_addr
*)¶ms
.spp_address
)) {
2303 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2304 params
.spp_assoc_id
);
2309 /* Get association, if assoc_id != 0 and the socket is a one
2310 * to many style socket, and an association was not found, then
2311 * the id was invalid.
2313 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2314 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2317 /* Heartbeat demand can only be sent on a transport or
2318 * association, but not a socket.
2320 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2323 /* Process parameters. */
2324 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2325 hb_change
, pmtud_change
,
2331 /* If changes are for association, also apply parameters to each
2334 if (!trans
&& asoc
) {
2335 list_for_each_entry(trans
, &asoc
->peer
.transport_addr_list
,
2337 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2338 hb_change
, pmtud_change
,
2347 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2349 * This option will effect the way delayed acks are performed. This
2350 * option allows you to get or set the delayed ack time, in
2351 * milliseconds. It also allows changing the delayed ack frequency.
2352 * Changing the frequency to 1 disables the delayed sack algorithm. If
2353 * the assoc_id is 0, then this sets or gets the endpoints default
2354 * values. If the assoc_id field is non-zero, then the set or get
2355 * effects the specified association for the one to many model (the
2356 * assoc_id field is ignored by the one to one model). Note that if
2357 * sack_delay or sack_freq are 0 when setting this option, then the
2358 * current values will remain unchanged.
2360 * struct sctp_sack_info {
2361 * sctp_assoc_t sack_assoc_id;
2362 * uint32_t sack_delay;
2363 * uint32_t sack_freq;
2366 * sack_assoc_id - This parameter, indicates which association the user
2367 * is performing an action upon. Note that if this field's value is
2368 * zero then the endpoints default value is changed (effecting future
2369 * associations only).
2371 * sack_delay - This parameter contains the number of milliseconds that
2372 * the user is requesting the delayed ACK timer be set to. Note that
2373 * this value is defined in the standard to be between 200 and 500
2376 * sack_freq - This parameter contains the number of packets that must
2377 * be received before a sack is sent without waiting for the delay
2378 * timer to expire. The default value for this is 2, setting this
2379 * value to 1 will disable the delayed sack algorithm.
2382 static int sctp_setsockopt_delayed_ack(struct sock
*sk
,
2383 char __user
*optval
, unsigned int optlen
)
2385 struct sctp_sack_info params
;
2386 struct sctp_transport
*trans
= NULL
;
2387 struct sctp_association
*asoc
= NULL
;
2388 struct sctp_sock
*sp
= sctp_sk(sk
);
2390 if (optlen
== sizeof(struct sctp_sack_info
)) {
2391 if (copy_from_user(¶ms
, optval
, optlen
))
2394 if (params
.sack_delay
== 0 && params
.sack_freq
== 0)
2396 } else if (optlen
== sizeof(struct sctp_assoc_value
)) {
2397 printk(KERN_WARNING
"SCTP: Use of struct sctp_assoc_value "
2398 "in delayed_ack socket option deprecated\n");
2399 printk(KERN_WARNING
"SCTP: Use struct sctp_sack_info instead\n");
2400 if (copy_from_user(¶ms
, optval
, optlen
))
2403 if (params
.sack_delay
== 0)
2404 params
.sack_freq
= 1;
2406 params
.sack_freq
= 0;
2410 /* Validate value parameter. */
2411 if (params
.sack_delay
> 500)
2414 /* Get association, if sack_assoc_id != 0 and the socket is a one
2415 * to many style socket, and an association was not found, then
2416 * the id was invalid.
2418 asoc
= sctp_id2assoc(sk
, params
.sack_assoc_id
);
2419 if (!asoc
&& params
.sack_assoc_id
&& sctp_style(sk
, UDP
))
2422 if (params
.sack_delay
) {
2425 msecs_to_jiffies(params
.sack_delay
);
2427 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2428 SPP_SACKDELAY_ENABLE
;
2430 sp
->sackdelay
= params
.sack_delay
;
2432 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2433 SPP_SACKDELAY_ENABLE
;
2437 if (params
.sack_freq
== 1) {
2440 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2441 SPP_SACKDELAY_DISABLE
;
2444 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2445 SPP_SACKDELAY_DISABLE
;
2447 } else if (params
.sack_freq
> 1) {
2449 asoc
->sackfreq
= params
.sack_freq
;
2451 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2452 SPP_SACKDELAY_ENABLE
;
2454 sp
->sackfreq
= params
.sack_freq
;
2456 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2457 SPP_SACKDELAY_ENABLE
;
2461 /* If change is for association, also apply to each transport. */
2463 list_for_each_entry(trans
, &asoc
->peer
.transport_addr_list
,
2465 if (params
.sack_delay
) {
2467 msecs_to_jiffies(params
.sack_delay
);
2468 trans
->param_flags
=
2469 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2470 SPP_SACKDELAY_ENABLE
;
2472 if (params
.sack_freq
== 1) {
2473 trans
->param_flags
=
2474 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2475 SPP_SACKDELAY_DISABLE
;
2476 } else if (params
.sack_freq
> 1) {
2477 trans
->sackfreq
= params
.sack_freq
;
2478 trans
->param_flags
=
2479 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2480 SPP_SACKDELAY_ENABLE
;
2488 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2490 * Applications can specify protocol parameters for the default association
2491 * initialization. The option name argument to setsockopt() and getsockopt()
2494 * Setting initialization parameters is effective only on an unconnected
2495 * socket (for UDP-style sockets only future associations are effected
2496 * by the change). With TCP-style sockets, this option is inherited by
2497 * sockets derived from a listener socket.
2499 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, unsigned int optlen
)
2501 struct sctp_initmsg sinit
;
2502 struct sctp_sock
*sp
= sctp_sk(sk
);
2504 if (optlen
!= sizeof(struct sctp_initmsg
))
2506 if (copy_from_user(&sinit
, optval
, optlen
))
2509 if (sinit
.sinit_num_ostreams
)
2510 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2511 if (sinit
.sinit_max_instreams
)
2512 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2513 if (sinit
.sinit_max_attempts
)
2514 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2515 if (sinit
.sinit_max_init_timeo
)
2516 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2522 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2524 * Applications that wish to use the sendto() system call may wish to
2525 * specify a default set of parameters that would normally be supplied
2526 * through the inclusion of ancillary data. This socket option allows
2527 * such an application to set the default sctp_sndrcvinfo structure.
2528 * The application that wishes to use this socket option simply passes
2529 * in to this call the sctp_sndrcvinfo structure defined in Section
2530 * 5.2.2) The input parameters accepted by this call include
2531 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2532 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2533 * to this call if the caller is using the UDP model.
2535 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2536 char __user
*optval
,
2537 unsigned int optlen
)
2539 struct sctp_sndrcvinfo info
;
2540 struct sctp_association
*asoc
;
2541 struct sctp_sock
*sp
= sctp_sk(sk
);
2543 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2545 if (copy_from_user(&info
, optval
, optlen
))
2548 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2549 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2553 asoc
->default_stream
= info
.sinfo_stream
;
2554 asoc
->default_flags
= info
.sinfo_flags
;
2555 asoc
->default_ppid
= info
.sinfo_ppid
;
2556 asoc
->default_context
= info
.sinfo_context
;
2557 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2559 sp
->default_stream
= info
.sinfo_stream
;
2560 sp
->default_flags
= info
.sinfo_flags
;
2561 sp
->default_ppid
= info
.sinfo_ppid
;
2562 sp
->default_context
= info
.sinfo_context
;
2563 sp
->default_timetolive
= info
.sinfo_timetolive
;
2569 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2571 * Requests that the local SCTP stack use the enclosed peer address as
2572 * the association primary. The enclosed address must be one of the
2573 * association peer's addresses.
2575 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2576 unsigned int optlen
)
2578 struct sctp_prim prim
;
2579 struct sctp_transport
*trans
;
2581 if (optlen
!= sizeof(struct sctp_prim
))
2584 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2587 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2591 sctp_assoc_set_primary(trans
->asoc
, trans
);
2597 * 7.1.5 SCTP_NODELAY
2599 * Turn on/off any Nagle-like algorithm. This means that packets are
2600 * generally sent as soon as possible and no unnecessary delays are
2601 * introduced, at the cost of more packets in the network. Expects an
2602 * integer boolean flag.
2604 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2605 unsigned int optlen
)
2609 if (optlen
< sizeof(int))
2611 if (get_user(val
, (int __user
*)optval
))
2614 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2620 * 7.1.1 SCTP_RTOINFO
2622 * The protocol parameters used to initialize and bound retransmission
2623 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2624 * and modify these parameters.
2625 * All parameters are time values, in milliseconds. A value of 0, when
2626 * modifying the parameters, indicates that the current value should not
2630 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, unsigned int optlen
)
2632 struct sctp_rtoinfo rtoinfo
;
2633 struct sctp_association
*asoc
;
2635 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2638 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2641 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2643 /* Set the values to the specific association */
2644 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2648 if (rtoinfo
.srto_initial
!= 0)
2650 msecs_to_jiffies(rtoinfo
.srto_initial
);
2651 if (rtoinfo
.srto_max
!= 0)
2652 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2653 if (rtoinfo
.srto_min
!= 0)
2654 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2656 /* If there is no association or the association-id = 0
2657 * set the values to the endpoint.
2659 struct sctp_sock
*sp
= sctp_sk(sk
);
2661 if (rtoinfo
.srto_initial
!= 0)
2662 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2663 if (rtoinfo
.srto_max
!= 0)
2664 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2665 if (rtoinfo
.srto_min
!= 0)
2666 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2674 * 7.1.2 SCTP_ASSOCINFO
2676 * This option is used to tune the maximum retransmission attempts
2677 * of the association.
2678 * Returns an error if the new association retransmission value is
2679 * greater than the sum of the retransmission value of the peer.
2680 * See [SCTP] for more information.
2683 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, unsigned int optlen
)
2686 struct sctp_assocparams assocparams
;
2687 struct sctp_association
*asoc
;
2689 if (optlen
!= sizeof(struct sctp_assocparams
))
2691 if (copy_from_user(&assocparams
, optval
, optlen
))
2694 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2696 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2699 /* Set the values to the specific association */
2701 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2704 struct sctp_transport
*peer_addr
;
2706 list_for_each_entry(peer_addr
, &asoc
->peer
.transport_addr_list
,
2708 path_sum
+= peer_addr
->pathmaxrxt
;
2712 /* Only validate asocmaxrxt if we have more than
2713 * one path/transport. We do this because path
2714 * retransmissions are only counted when we have more
2718 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2721 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2724 if (assocparams
.sasoc_cookie_life
!= 0) {
2725 asoc
->cookie_life
.tv_sec
=
2726 assocparams
.sasoc_cookie_life
/ 1000;
2727 asoc
->cookie_life
.tv_usec
=
2728 (assocparams
.sasoc_cookie_life
% 1000)
2732 /* Set the values to the endpoint */
2733 struct sctp_sock
*sp
= sctp_sk(sk
);
2735 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2736 sp
->assocparams
.sasoc_asocmaxrxt
=
2737 assocparams
.sasoc_asocmaxrxt
;
2738 if (assocparams
.sasoc_cookie_life
!= 0)
2739 sp
->assocparams
.sasoc_cookie_life
=
2740 assocparams
.sasoc_cookie_life
;
2746 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2748 * This socket option is a boolean flag which turns on or off mapped V4
2749 * addresses. If this option is turned on and the socket is type
2750 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2751 * If this option is turned off, then no mapping will be done of V4
2752 * addresses and a user will receive both PF_INET6 and PF_INET type
2753 * addresses on the socket.
2755 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, unsigned int optlen
)
2758 struct sctp_sock
*sp
= sctp_sk(sk
);
2760 if (optlen
< sizeof(int))
2762 if (get_user(val
, (int __user
*)optval
))
2773 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2774 * This option will get or set the maximum size to put in any outgoing
2775 * SCTP DATA chunk. If a message is larger than this size it will be
2776 * fragmented by SCTP into the specified size. Note that the underlying
2777 * SCTP implementation may fragment into smaller sized chunks when the
2778 * PMTU of the underlying association is smaller than the value set by
2779 * the user. The default value for this option is '0' which indicates
2780 * the user is NOT limiting fragmentation and only the PMTU will effect
2781 * SCTP's choice of DATA chunk size. Note also that values set larger
2782 * than the maximum size of an IP datagram will effectively let SCTP
2783 * control fragmentation (i.e. the same as setting this option to 0).
2785 * The following structure is used to access and modify this parameter:
2787 * struct sctp_assoc_value {
2788 * sctp_assoc_t assoc_id;
2789 * uint32_t assoc_value;
2792 * assoc_id: This parameter is ignored for one-to-one style sockets.
2793 * For one-to-many style sockets this parameter indicates which
2794 * association the user is performing an action upon. Note that if
2795 * this field's value is zero then the endpoints default value is
2796 * changed (effecting future associations only).
2797 * assoc_value: This parameter specifies the maximum size in bytes.
2799 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, unsigned int optlen
)
2801 struct sctp_assoc_value params
;
2802 struct sctp_association
*asoc
;
2803 struct sctp_sock
*sp
= sctp_sk(sk
);
2806 if (optlen
== sizeof(int)) {
2808 "SCTP: Use of int in maxseg socket option deprecated\n");
2810 "SCTP: Use struct sctp_assoc_value instead\n");
2811 if (copy_from_user(&val
, optval
, optlen
))
2813 params
.assoc_id
= 0;
2814 } else if (optlen
== sizeof(struct sctp_assoc_value
)) {
2815 if (copy_from_user(¶ms
, optval
, optlen
))
2817 val
= params
.assoc_value
;
2821 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2824 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2825 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2830 val
= asoc
->pathmtu
;
2831 val
-= sp
->pf
->af
->net_header_len
;
2832 val
-= sizeof(struct sctphdr
) +
2833 sizeof(struct sctp_data_chunk
);
2835 asoc
->user_frag
= val
;
2836 asoc
->frag_point
= sctp_frag_point(asoc
, asoc
->pathmtu
);
2838 sp
->user_frag
= val
;
2846 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2848 * Requests that the peer mark the enclosed address as the association
2849 * primary. The enclosed address must be one of the association's
2850 * locally bound addresses. The following structure is used to make a
2851 * set primary request:
2853 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2854 unsigned int optlen
)
2856 struct sctp_sock
*sp
;
2857 struct sctp_endpoint
*ep
;
2858 struct sctp_association
*asoc
= NULL
;
2859 struct sctp_setpeerprim prim
;
2860 struct sctp_chunk
*chunk
;
2866 if (!sctp_addip_enable
)
2869 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2872 if (copy_from_user(&prim
, optval
, optlen
))
2875 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2879 if (!asoc
->peer
.asconf_capable
)
2882 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2885 if (!sctp_state(asoc
, ESTABLISHED
))
2888 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2889 return -EADDRNOTAVAIL
;
2891 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2892 chunk
= sctp_make_asconf_set_prim(asoc
,
2893 (union sctp_addr
*)&prim
.sspp_addr
);
2897 err
= sctp_send_asconf(asoc
, chunk
);
2899 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2904 static int sctp_setsockopt_adaptation_layer(struct sock
*sk
, char __user
*optval
,
2905 unsigned int optlen
)
2907 struct sctp_setadaptation adaptation
;
2909 if (optlen
!= sizeof(struct sctp_setadaptation
))
2911 if (copy_from_user(&adaptation
, optval
, optlen
))
2914 sctp_sk(sk
)->adaptation_ind
= adaptation
.ssb_adaptation_ind
;
2920 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2922 * The context field in the sctp_sndrcvinfo structure is normally only
2923 * used when a failed message is retrieved holding the value that was
2924 * sent down on the actual send call. This option allows the setting of
2925 * a default context on an association basis that will be received on
2926 * reading messages from the peer. This is especially helpful in the
2927 * one-2-many model for an application to keep some reference to an
2928 * internal state machine that is processing messages on the
2929 * association. Note that the setting of this value only effects
2930 * received messages from the peer and does not effect the value that is
2931 * saved with outbound messages.
2933 static int sctp_setsockopt_context(struct sock
*sk
, char __user
*optval
,
2934 unsigned int optlen
)
2936 struct sctp_assoc_value params
;
2937 struct sctp_sock
*sp
;
2938 struct sctp_association
*asoc
;
2940 if (optlen
!= sizeof(struct sctp_assoc_value
))
2942 if (copy_from_user(¶ms
, optval
, optlen
))
2947 if (params
.assoc_id
!= 0) {
2948 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2951 asoc
->default_rcv_context
= params
.assoc_value
;
2953 sp
->default_rcv_context
= params
.assoc_value
;
2960 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2962 * This options will at a minimum specify if the implementation is doing
2963 * fragmented interleave. Fragmented interleave, for a one to many
2964 * socket, is when subsequent calls to receive a message may return
2965 * parts of messages from different associations. Some implementations
2966 * may allow you to turn this value on or off. If so, when turned off,
2967 * no fragment interleave will occur (which will cause a head of line
2968 * blocking amongst multiple associations sharing the same one to many
2969 * socket). When this option is turned on, then each receive call may
2970 * come from a different association (thus the user must receive data
2971 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2972 * association each receive belongs to.
2974 * This option takes a boolean value. A non-zero value indicates that
2975 * fragmented interleave is on. A value of zero indicates that
2976 * fragmented interleave is off.
2978 * Note that it is important that an implementation that allows this
2979 * option to be turned on, have it off by default. Otherwise an unaware
2980 * application using the one to many model may become confused and act
2983 static int sctp_setsockopt_fragment_interleave(struct sock
*sk
,
2984 char __user
*optval
,
2985 unsigned int optlen
)
2989 if (optlen
!= sizeof(int))
2991 if (get_user(val
, (int __user
*)optval
))
2994 sctp_sk(sk
)->frag_interleave
= (val
== 0) ? 0 : 1;
3000 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3001 * (SCTP_PARTIAL_DELIVERY_POINT)
3003 * This option will set or get the SCTP partial delivery point. This
3004 * point is the size of a message where the partial delivery API will be
3005 * invoked to help free up rwnd space for the peer. Setting this to a
3006 * lower value will cause partial deliveries to happen more often. The
3007 * calls argument is an integer that sets or gets the partial delivery
3008 * point. Note also that the call will fail if the user attempts to set
3009 * this value larger than the socket receive buffer size.
3011 * Note that any single message having a length smaller than or equal to
3012 * the SCTP partial delivery point will be delivered in one single read
3013 * call as long as the user provided buffer is large enough to hold the
3016 static int sctp_setsockopt_partial_delivery_point(struct sock
*sk
,
3017 char __user
*optval
,
3018 unsigned int optlen
)
3022 if (optlen
!= sizeof(u32
))
3024 if (get_user(val
, (int __user
*)optval
))
3027 /* Note: We double the receive buffer from what the user sets
3028 * it to be, also initial rwnd is based on rcvbuf/2.
3030 if (val
> (sk
->sk_rcvbuf
>> 1))
3033 sctp_sk(sk
)->pd_point
= val
;
3035 return 0; /* is this the right error code? */
3039 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3041 * This option will allow a user to change the maximum burst of packets
3042 * that can be emitted by this association. Note that the default value
3043 * is 4, and some implementations may restrict this setting so that it
3044 * can only be lowered.
3046 * NOTE: This text doesn't seem right. Do this on a socket basis with
3047 * future associations inheriting the socket value.
3049 static int sctp_setsockopt_maxburst(struct sock
*sk
,
3050 char __user
*optval
,
3051 unsigned int optlen
)
3053 struct sctp_assoc_value params
;
3054 struct sctp_sock
*sp
;
3055 struct sctp_association
*asoc
;
3059 if (optlen
== sizeof(int)) {
3061 "SCTP: Use of int in max_burst socket option deprecated\n");
3063 "SCTP: Use struct sctp_assoc_value instead\n");
3064 if (copy_from_user(&val
, optval
, optlen
))
3066 } else if (optlen
== sizeof(struct sctp_assoc_value
)) {
3067 if (copy_from_user(¶ms
, optval
, optlen
))
3069 val
= params
.assoc_value
;
3070 assoc_id
= params
.assoc_id
;
3076 if (assoc_id
!= 0) {
3077 asoc
= sctp_id2assoc(sk
, assoc_id
);
3080 asoc
->max_burst
= val
;
3082 sp
->max_burst
= val
;
3088 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3090 * This set option adds a chunk type that the user is requesting to be
3091 * received only in an authenticated way. Changes to the list of chunks
3092 * will only effect future associations on the socket.
3094 static int sctp_setsockopt_auth_chunk(struct sock
*sk
,
3095 char __user
*optval
,
3096 unsigned int optlen
)
3098 struct sctp_authchunk val
;
3100 if (!sctp_auth_enable
)
3103 if (optlen
!= sizeof(struct sctp_authchunk
))
3105 if (copy_from_user(&val
, optval
, optlen
))
3108 switch (val
.sauth_chunk
) {
3110 case SCTP_CID_INIT_ACK
:
3111 case SCTP_CID_SHUTDOWN_COMPLETE
:
3116 /* add this chunk id to the endpoint */
3117 return sctp_auth_ep_add_chunkid(sctp_sk(sk
)->ep
, val
.sauth_chunk
);
3121 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3123 * This option gets or sets the list of HMAC algorithms that the local
3124 * endpoint requires the peer to use.
3126 static int sctp_setsockopt_hmac_ident(struct sock
*sk
,
3127 char __user
*optval
,
3128 unsigned int optlen
)
3130 struct sctp_hmacalgo
*hmacs
;
3134 if (!sctp_auth_enable
)
3137 if (optlen
< sizeof(struct sctp_hmacalgo
))
3140 hmacs
= kmalloc(optlen
, GFP_KERNEL
);
3144 if (copy_from_user(hmacs
, optval
, optlen
)) {
3149 idents
= hmacs
->shmac_num_idents
;
3150 if (idents
== 0 || idents
> SCTP_AUTH_NUM_HMACS
||
3151 (idents
* sizeof(u16
)) > (optlen
- sizeof(struct sctp_hmacalgo
))) {
3156 err
= sctp_auth_ep_set_hmacs(sctp_sk(sk
)->ep
, hmacs
);
3163 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3165 * This option will set a shared secret key which is used to build an
3166 * association shared key.
3168 static int sctp_setsockopt_auth_key(struct sock
*sk
,
3169 char __user
*optval
,
3170 unsigned int optlen
)
3172 struct sctp_authkey
*authkey
;
3173 struct sctp_association
*asoc
;
3176 if (!sctp_auth_enable
)
3179 if (optlen
<= sizeof(struct sctp_authkey
))
3182 authkey
= kmalloc(optlen
, GFP_KERNEL
);
3186 if (copy_from_user(authkey
, optval
, optlen
)) {
3191 if (authkey
->sca_keylength
> optlen
- sizeof(struct sctp_authkey
)) {
3196 asoc
= sctp_id2assoc(sk
, authkey
->sca_assoc_id
);
3197 if (!asoc
&& authkey
->sca_assoc_id
&& sctp_style(sk
, UDP
)) {
3202 ret
= sctp_auth_set_key(sctp_sk(sk
)->ep
, asoc
, authkey
);
3209 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3211 * This option will get or set the active shared key to be used to build
3212 * the association shared key.
3214 static int sctp_setsockopt_active_key(struct sock
*sk
,
3215 char __user
*optval
,
3216 unsigned int optlen
)
3218 struct sctp_authkeyid val
;
3219 struct sctp_association
*asoc
;
3221 if (!sctp_auth_enable
)
3224 if (optlen
!= sizeof(struct sctp_authkeyid
))
3226 if (copy_from_user(&val
, optval
, optlen
))
3229 asoc
= sctp_id2assoc(sk
, val
.scact_assoc_id
);
3230 if (!asoc
&& val
.scact_assoc_id
&& sctp_style(sk
, UDP
))
3233 return sctp_auth_set_active_key(sctp_sk(sk
)->ep
, asoc
,
3234 val
.scact_keynumber
);
3238 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3240 * This set option will delete a shared secret key from use.
3242 static int sctp_setsockopt_del_key(struct sock
*sk
,
3243 char __user
*optval
,
3244 unsigned int optlen
)
3246 struct sctp_authkeyid val
;
3247 struct sctp_association
*asoc
;
3249 if (!sctp_auth_enable
)
3252 if (optlen
!= sizeof(struct sctp_authkeyid
))
3254 if (copy_from_user(&val
, optval
, optlen
))
3257 asoc
= sctp_id2assoc(sk
, val
.scact_assoc_id
);
3258 if (!asoc
&& val
.scact_assoc_id
&& sctp_style(sk
, UDP
))
3261 return sctp_auth_del_key_id(sctp_sk(sk
)->ep
, asoc
,
3262 val
.scact_keynumber
);
3267 /* API 6.2 setsockopt(), getsockopt()
3269 * Applications use setsockopt() and getsockopt() to set or retrieve
3270 * socket options. Socket options are used to change the default
3271 * behavior of sockets calls. They are described in Section 7.
3275 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3276 * int __user *optlen);
3277 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3280 * sd - the socket descript.
3281 * level - set to IPPROTO_SCTP for all SCTP options.
3282 * optname - the option name.
3283 * optval - the buffer to store the value of the option.
3284 * optlen - the size of the buffer.
3286 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
3287 char __user
*optval
, unsigned int optlen
)
3291 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3294 /* I can hardly begin to describe how wrong this is. This is
3295 * so broken as to be worse than useless. The API draft
3296 * REALLY is NOT helpful here... I am not convinced that the
3297 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3298 * are at all well-founded.
3300 if (level
!= SOL_SCTP
) {
3301 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
3302 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
3309 case SCTP_SOCKOPT_BINDX_ADD
:
3310 /* 'optlen' is the size of the addresses buffer. */
3311 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
3312 optlen
, SCTP_BINDX_ADD_ADDR
);
3315 case SCTP_SOCKOPT_BINDX_REM
:
3316 /* 'optlen' is the size of the addresses buffer. */
3317 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
3318 optlen
, SCTP_BINDX_REM_ADDR
);
3321 case SCTP_SOCKOPT_CONNECTX_OLD
:
3322 /* 'optlen' is the size of the addresses buffer. */
3323 retval
= sctp_setsockopt_connectx_old(sk
,
3324 (struct sockaddr __user
*)optval
,
3328 case SCTP_SOCKOPT_CONNECTX
:
3329 /* 'optlen' is the size of the addresses buffer. */
3330 retval
= sctp_setsockopt_connectx(sk
,
3331 (struct sockaddr __user
*)optval
,
3335 case SCTP_DISABLE_FRAGMENTS
:
3336 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
3340 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
3343 case SCTP_AUTOCLOSE
:
3344 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
3347 case SCTP_PEER_ADDR_PARAMS
:
3348 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
3351 case SCTP_DELAYED_ACK
:
3352 retval
= sctp_setsockopt_delayed_ack(sk
, optval
, optlen
);
3354 case SCTP_PARTIAL_DELIVERY_POINT
:
3355 retval
= sctp_setsockopt_partial_delivery_point(sk
, optval
, optlen
);
3359 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
3361 case SCTP_DEFAULT_SEND_PARAM
:
3362 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
3365 case SCTP_PRIMARY_ADDR
:
3366 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
3368 case SCTP_SET_PEER_PRIMARY_ADDR
:
3369 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
3372 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
3375 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
3377 case SCTP_ASSOCINFO
:
3378 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
3380 case SCTP_I_WANT_MAPPED_V4_ADDR
:
3381 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
3384 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
3386 case SCTP_ADAPTATION_LAYER
:
3387 retval
= sctp_setsockopt_adaptation_layer(sk
, optval
, optlen
);
3390 retval
= sctp_setsockopt_context(sk
, optval
, optlen
);
3392 case SCTP_FRAGMENT_INTERLEAVE
:
3393 retval
= sctp_setsockopt_fragment_interleave(sk
, optval
, optlen
);
3395 case SCTP_MAX_BURST
:
3396 retval
= sctp_setsockopt_maxburst(sk
, optval
, optlen
);
3398 case SCTP_AUTH_CHUNK
:
3399 retval
= sctp_setsockopt_auth_chunk(sk
, optval
, optlen
);
3401 case SCTP_HMAC_IDENT
:
3402 retval
= sctp_setsockopt_hmac_ident(sk
, optval
, optlen
);
3405 retval
= sctp_setsockopt_auth_key(sk
, optval
, optlen
);
3407 case SCTP_AUTH_ACTIVE_KEY
:
3408 retval
= sctp_setsockopt_active_key(sk
, optval
, optlen
);
3410 case SCTP_AUTH_DELETE_KEY
:
3411 retval
= sctp_setsockopt_del_key(sk
, optval
, optlen
);
3414 retval
= -ENOPROTOOPT
;
3418 sctp_release_sock(sk
);
3424 /* API 3.1.6 connect() - UDP Style Syntax
3426 * An application may use the connect() call in the UDP model to initiate an
3427 * association without sending data.
3431 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3433 * sd: the socket descriptor to have a new association added to.
3435 * nam: the address structure (either struct sockaddr_in or struct
3436 * sockaddr_in6 defined in RFC2553 [7]).
3438 * len: the size of the address.
3440 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
3448 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3449 __func__
, sk
, addr
, addr_len
);
3451 /* Validate addr_len before calling common connect/connectx routine. */
3452 af
= sctp_get_af_specific(addr
->sa_family
);
3453 if (!af
|| addr_len
< af
->sockaddr_len
) {
3456 /* Pass correct addr len to common routine (so it knows there
3457 * is only one address being passed.
3459 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
, NULL
);
3462 sctp_release_sock(sk
);
3466 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
3468 return -EOPNOTSUPP
; /* STUB */
3471 /* 4.1.4 accept() - TCP Style Syntax
3473 * Applications use accept() call to remove an established SCTP
3474 * association from the accept queue of the endpoint. A new socket
3475 * descriptor will be returned from accept() to represent the newly
3476 * formed association.
3478 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
3480 struct sctp_sock
*sp
;
3481 struct sctp_endpoint
*ep
;
3482 struct sock
*newsk
= NULL
;
3483 struct sctp_association
*asoc
;
3492 if (!sctp_style(sk
, TCP
)) {
3493 error
= -EOPNOTSUPP
;
3497 if (!sctp_sstate(sk
, LISTENING
)) {
3502 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
3504 error
= sctp_wait_for_accept(sk
, timeo
);
3508 /* We treat the list of associations on the endpoint as the accept
3509 * queue and pick the first association on the list.
3511 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
3513 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
3519 /* Populate the fields of the newsk from the oldsk and migrate the
3520 * asoc to the newsk.
3522 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
3525 sctp_release_sock(sk
);
3530 /* The SCTP ioctl handler. */
3531 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
3533 return -ENOIOCTLCMD
;
3536 /* This is the function which gets called during socket creation to
3537 * initialized the SCTP-specific portion of the sock.
3538 * The sock structure should already be zero-filled memory.
3540 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
3542 struct sctp_endpoint
*ep
;
3543 struct sctp_sock
*sp
;
3545 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
3549 /* Initialize the SCTP per socket area. */
3550 switch (sk
->sk_type
) {
3551 case SOCK_SEQPACKET
:
3552 sp
->type
= SCTP_SOCKET_UDP
;
3555 sp
->type
= SCTP_SOCKET_TCP
;
3558 return -ESOCKTNOSUPPORT
;
3561 /* Initialize default send parameters. These parameters can be
3562 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3564 sp
->default_stream
= 0;
3565 sp
->default_ppid
= 0;
3566 sp
->default_flags
= 0;
3567 sp
->default_context
= 0;
3568 sp
->default_timetolive
= 0;
3570 sp
->default_rcv_context
= 0;
3571 sp
->max_burst
= sctp_max_burst
;
3573 /* Initialize default setup parameters. These parameters
3574 * can be modified with the SCTP_INITMSG socket option or
3575 * overridden by the SCTP_INIT CMSG.
3577 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
3578 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
3579 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
3580 sp
->initmsg
.sinit_max_init_timeo
= sctp_rto_max
;
3582 /* Initialize default RTO related parameters. These parameters can
3583 * be modified for with the SCTP_RTOINFO socket option.
3585 sp
->rtoinfo
.srto_initial
= sctp_rto_initial
;
3586 sp
->rtoinfo
.srto_max
= sctp_rto_max
;
3587 sp
->rtoinfo
.srto_min
= sctp_rto_min
;
3589 /* Initialize default association related parameters. These parameters
3590 * can be modified with the SCTP_ASSOCINFO socket option.
3592 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3593 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3594 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3595 sp
->assocparams
.sasoc_local_rwnd
= 0;
3596 sp
->assocparams
.sasoc_cookie_life
= sctp_valid_cookie_life
;
3598 /* Initialize default event subscriptions. By default, all the
3601 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3603 /* Default Peer Address Parameters. These defaults can
3604 * be modified via SCTP_PEER_ADDR_PARAMS
3606 sp
->hbinterval
= sctp_hb_interval
;
3607 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3608 sp
->pathmtu
= 0; // allow default discovery
3609 sp
->sackdelay
= sctp_sack_timeout
;
3611 sp
->param_flags
= SPP_HB_ENABLE
|
3613 SPP_SACKDELAY_ENABLE
;
3615 /* If enabled no SCTP message fragmentation will be performed.
3616 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3618 sp
->disable_fragments
= 0;
3620 /* Enable Nagle algorithm by default. */
3623 /* Enable by default. */
3626 /* Auto-close idle associations after the configured
3627 * number of seconds. A value of 0 disables this
3628 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3629 * for UDP-style sockets only.
3633 /* User specified fragmentation limit. */
3636 sp
->adaptation_ind
= 0;
3638 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3640 /* Control variables for partial data delivery. */
3641 atomic_set(&sp
->pd_mode
, 0);
3642 skb_queue_head_init(&sp
->pd_lobby
);
3643 sp
->frag_interleave
= 0;
3645 /* Create a per socket endpoint structure. Even if we
3646 * change the data structure relationships, this may still
3647 * be useful for storing pre-connect address information.
3649 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3656 SCTP_DBG_OBJCNT_INC(sock
);
3659 percpu_counter_inc(&sctp_sockets_allocated
);
3660 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
3666 /* Cleanup any SCTP per socket resources. */
3667 SCTP_STATIC
void sctp_destroy_sock(struct sock
*sk
)
3669 struct sctp_endpoint
*ep
;
3671 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3673 /* Release our hold on the endpoint. */
3674 ep
= sctp_sk(sk
)->ep
;
3675 sctp_endpoint_free(ep
);
3677 percpu_counter_dec(&sctp_sockets_allocated
);
3678 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
3682 /* API 4.1.7 shutdown() - TCP Style Syntax
3683 * int shutdown(int socket, int how);
3685 * sd - the socket descriptor of the association to be closed.
3686 * how - Specifies the type of shutdown. The values are
3689 * Disables further receive operations. No SCTP
3690 * protocol action is taken.
3692 * Disables further send operations, and initiates
3693 * the SCTP shutdown sequence.
3695 * Disables further send and receive operations
3696 * and initiates the SCTP shutdown sequence.
3698 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3700 struct sctp_endpoint
*ep
;
3701 struct sctp_association
*asoc
;
3703 if (!sctp_style(sk
, TCP
))
3706 if (how
& SEND_SHUTDOWN
) {
3707 ep
= sctp_sk(sk
)->ep
;
3708 if (!list_empty(&ep
->asocs
)) {
3709 asoc
= list_entry(ep
->asocs
.next
,
3710 struct sctp_association
, asocs
);
3711 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3716 /* 7.2.1 Association Status (SCTP_STATUS)
3718 * Applications can retrieve current status information about an
3719 * association, including association state, peer receiver window size,
3720 * number of unacked data chunks, and number of data chunks pending
3721 * receipt. This information is read-only.
3723 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3724 char __user
*optval
,
3727 struct sctp_status status
;
3728 struct sctp_association
*asoc
= NULL
;
3729 struct sctp_transport
*transport
;
3730 sctp_assoc_t associd
;
3733 if (len
< sizeof(status
)) {
3738 len
= sizeof(status
);
3739 if (copy_from_user(&status
, optval
, len
)) {
3744 associd
= status
.sstat_assoc_id
;
3745 asoc
= sctp_id2assoc(sk
, associd
);
3751 transport
= asoc
->peer
.primary_path
;
3753 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3754 status
.sstat_state
= asoc
->state
;
3755 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3756 status
.sstat_unackdata
= asoc
->unack_data
;
3758 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3759 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3760 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3761 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3762 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3763 memcpy(&status
.sstat_primary
.spinfo_address
, &transport
->ipaddr
,
3764 transport
->af_specific
->sockaddr_len
);
3765 /* Map ipv4 address into v4-mapped-on-v6 address. */
3766 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3767 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3768 status
.sstat_primary
.spinfo_state
= transport
->state
;
3769 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3770 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3771 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3772 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3774 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3775 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3777 if (put_user(len
, optlen
)) {
3782 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3783 len
, status
.sstat_state
, status
.sstat_rwnd
,
3784 status
.sstat_assoc_id
);
3786 if (copy_to_user(optval
, &status
, len
)) {
3796 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3798 * Applications can retrieve information about a specific peer address
3799 * of an association, including its reachability state, congestion
3800 * window, and retransmission timer values. This information is
3803 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3804 char __user
*optval
,
3807 struct sctp_paddrinfo pinfo
;
3808 struct sctp_transport
*transport
;
3811 if (len
< sizeof(pinfo
)) {
3816 len
= sizeof(pinfo
);
3817 if (copy_from_user(&pinfo
, optval
, len
)) {
3822 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3823 pinfo
.spinfo_assoc_id
);
3827 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3828 pinfo
.spinfo_state
= transport
->state
;
3829 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3830 pinfo
.spinfo_srtt
= transport
->srtt
;
3831 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3832 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3834 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3835 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3837 if (put_user(len
, optlen
)) {
3842 if (copy_to_user(optval
, &pinfo
, len
)) {
3851 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3853 * This option is a on/off flag. If enabled no SCTP message
3854 * fragmentation will be performed. Instead if a message being sent
3855 * exceeds the current PMTU size, the message will NOT be sent and
3856 * instead a error will be indicated to the user.
3858 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3859 char __user
*optval
, int __user
*optlen
)
3863 if (len
< sizeof(int))
3867 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3868 if (put_user(len
, optlen
))
3870 if (copy_to_user(optval
, &val
, len
))
3875 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3877 * This socket option is used to specify various notifications and
3878 * ancillary data the user wishes to receive.
3880 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3883 if (len
< sizeof(struct sctp_event_subscribe
))
3885 len
= sizeof(struct sctp_event_subscribe
);
3886 if (put_user(len
, optlen
))
3888 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3893 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3895 * This socket option is applicable to the UDP-style socket only. When
3896 * set it will cause associations that are idle for more than the
3897 * specified number of seconds to automatically close. An association
3898 * being idle is defined an association that has NOT sent or received
3899 * user data. The special value of '0' indicates that no automatic
3900 * close of any associations should be performed. The option expects an
3901 * integer defining the number of seconds of idle time before an
3902 * association is closed.
3904 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3906 /* Applicable to UDP-style socket only */
3907 if (sctp_style(sk
, TCP
))
3909 if (len
< sizeof(int))
3912 if (put_user(len
, optlen
))
3914 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, sizeof(int)))
3919 /* Helper routine to branch off an association to a new socket. */
3920 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3921 struct socket
**sockp
)
3923 struct sock
*sk
= asoc
->base
.sk
;
3924 struct socket
*sock
;
3928 /* An association cannot be branched off from an already peeled-off
3929 * socket, nor is this supported for tcp style sockets.
3931 if (!sctp_style(sk
, UDP
))
3934 /* Create a new socket. */
3935 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3939 sctp_copy_sock(sock
->sk
, sk
, asoc
);
3941 /* Make peeled-off sockets more like 1-1 accepted sockets.
3942 * Set the daddr and initialize id to something more random
3944 af
= sctp_get_af_specific(asoc
->peer
.primary_addr
.sa
.sa_family
);
3945 af
->to_sk_daddr(&asoc
->peer
.primary_addr
, sk
);
3947 /* Populate the fields of the newsk from the oldsk and migrate the
3948 * asoc to the newsk.
3950 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3957 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3959 sctp_peeloff_arg_t peeloff
;
3960 struct socket
*newsock
;
3962 struct sctp_association
*asoc
;
3964 if (len
< sizeof(sctp_peeloff_arg_t
))
3966 len
= sizeof(sctp_peeloff_arg_t
);
3967 if (copy_from_user(&peeloff
, optval
, len
))
3970 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3976 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __func__
, sk
, asoc
);
3978 retval
= sctp_do_peeloff(asoc
, &newsock
);
3982 /* Map the socket to an unused fd that can be returned to the user. */
3983 retval
= sock_map_fd(newsock
, 0);
3985 sock_release(newsock
);
3989 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3990 __func__
, sk
, asoc
, newsock
->sk
, retval
);
3992 /* Return the fd mapped to the new socket. */
3993 peeloff
.sd
= retval
;
3994 if (put_user(len
, optlen
))
3996 if (copy_to_user(optval
, &peeloff
, len
))
4003 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4005 * Applications can enable or disable heartbeats for any peer address of
4006 * an association, modify an address's heartbeat interval, force a
4007 * heartbeat to be sent immediately, and adjust the address's maximum
4008 * number of retransmissions sent before an address is considered
4009 * unreachable. The following structure is used to access and modify an
4010 * address's parameters:
4012 * struct sctp_paddrparams {
4013 * sctp_assoc_t spp_assoc_id;
4014 * struct sockaddr_storage spp_address;
4015 * uint32_t spp_hbinterval;
4016 * uint16_t spp_pathmaxrxt;
4017 * uint32_t spp_pathmtu;
4018 * uint32_t spp_sackdelay;
4019 * uint32_t spp_flags;
4022 * spp_assoc_id - (one-to-many style socket) This is filled in the
4023 * application, and identifies the association for
4025 * spp_address - This specifies which address is of interest.
4026 * spp_hbinterval - This contains the value of the heartbeat interval,
4027 * in milliseconds. If a value of zero
4028 * is present in this field then no changes are to
4029 * be made to this parameter.
4030 * spp_pathmaxrxt - This contains the maximum number of
4031 * retransmissions before this address shall be
4032 * considered unreachable. If a value of zero
4033 * is present in this field then no changes are to
4034 * be made to this parameter.
4035 * spp_pathmtu - When Path MTU discovery is disabled the value
4036 * specified here will be the "fixed" path mtu.
4037 * Note that if the spp_address field is empty
4038 * then all associations on this address will
4039 * have this fixed path mtu set upon them.
4041 * spp_sackdelay - When delayed sack is enabled, this value specifies
4042 * the number of milliseconds that sacks will be delayed
4043 * for. This value will apply to all addresses of an
4044 * association if the spp_address field is empty. Note
4045 * also, that if delayed sack is enabled and this
4046 * value is set to 0, no change is made to the last
4047 * recorded delayed sack timer value.
4049 * spp_flags - These flags are used to control various features
4050 * on an association. The flag field may contain
4051 * zero or more of the following options.
4053 * SPP_HB_ENABLE - Enable heartbeats on the
4054 * specified address. Note that if the address
4055 * field is empty all addresses for the association
4056 * have heartbeats enabled upon them.
4058 * SPP_HB_DISABLE - Disable heartbeats on the
4059 * speicifed address. Note that if the address
4060 * field is empty all addresses for the association
4061 * will have their heartbeats disabled. Note also
4062 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4063 * mutually exclusive, only one of these two should
4064 * be specified. Enabling both fields will have
4065 * undetermined results.
4067 * SPP_HB_DEMAND - Request a user initiated heartbeat
4068 * to be made immediately.
4070 * SPP_PMTUD_ENABLE - This field will enable PMTU
4071 * discovery upon the specified address. Note that
4072 * if the address feild is empty then all addresses
4073 * on the association are effected.
4075 * SPP_PMTUD_DISABLE - This field will disable PMTU
4076 * discovery upon the specified address. Note that
4077 * if the address feild is empty then all addresses
4078 * on the association are effected. Not also that
4079 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4080 * exclusive. Enabling both will have undetermined
4083 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4084 * on delayed sack. The time specified in spp_sackdelay
4085 * is used to specify the sack delay for this address. Note
4086 * that if spp_address is empty then all addresses will
4087 * enable delayed sack and take on the sack delay
4088 * value specified in spp_sackdelay.
4089 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4090 * off delayed sack. If the spp_address field is blank then
4091 * delayed sack is disabled for the entire association. Note
4092 * also that this field is mutually exclusive to
4093 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4096 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
4097 char __user
*optval
, int __user
*optlen
)
4099 struct sctp_paddrparams params
;
4100 struct sctp_transport
*trans
= NULL
;
4101 struct sctp_association
*asoc
= NULL
;
4102 struct sctp_sock
*sp
= sctp_sk(sk
);
4104 if (len
< sizeof(struct sctp_paddrparams
))
4106 len
= sizeof(struct sctp_paddrparams
);
4107 if (copy_from_user(¶ms
, optval
, len
))
4110 /* If an address other than INADDR_ANY is specified, and
4111 * no transport is found, then the request is invalid.
4113 if (!sctp_is_any(sk
, ( union sctp_addr
*)¶ms
.spp_address
)) {
4114 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
4115 params
.spp_assoc_id
);
4117 SCTP_DEBUG_PRINTK("Failed no transport\n");
4122 /* Get association, if assoc_id != 0 and the socket is a one
4123 * to many style socket, and an association was not found, then
4124 * the id was invalid.
4126 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
4127 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
4128 SCTP_DEBUG_PRINTK("Failed no association\n");
4133 /* Fetch transport values. */
4134 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
4135 params
.spp_pathmtu
= trans
->pathmtu
;
4136 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
4137 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
4139 /*draft-11 doesn't say what to return in spp_flags*/
4140 params
.spp_flags
= trans
->param_flags
;
4142 /* Fetch association values. */
4143 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
4144 params
.spp_pathmtu
= asoc
->pathmtu
;
4145 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
4146 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
4148 /*draft-11 doesn't say what to return in spp_flags*/
4149 params
.spp_flags
= asoc
->param_flags
;
4151 /* Fetch socket values. */
4152 params
.spp_hbinterval
= sp
->hbinterval
;
4153 params
.spp_pathmtu
= sp
->pathmtu
;
4154 params
.spp_sackdelay
= sp
->sackdelay
;
4155 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
4157 /*draft-11 doesn't say what to return in spp_flags*/
4158 params
.spp_flags
= sp
->param_flags
;
4161 if (copy_to_user(optval
, ¶ms
, len
))
4164 if (put_user(len
, optlen
))
4171 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
4173 * This option will effect the way delayed acks are performed. This
4174 * option allows you to get or set the delayed ack time, in
4175 * milliseconds. It also allows changing the delayed ack frequency.
4176 * Changing the frequency to 1 disables the delayed sack algorithm. If
4177 * the assoc_id is 0, then this sets or gets the endpoints default
4178 * values. If the assoc_id field is non-zero, then the set or get
4179 * effects the specified association for the one to many model (the
4180 * assoc_id field is ignored by the one to one model). Note that if
4181 * sack_delay or sack_freq are 0 when setting this option, then the
4182 * current values will remain unchanged.
4184 * struct sctp_sack_info {
4185 * sctp_assoc_t sack_assoc_id;
4186 * uint32_t sack_delay;
4187 * uint32_t sack_freq;
4190 * sack_assoc_id - This parameter, indicates which association the user
4191 * is performing an action upon. Note that if this field's value is
4192 * zero then the endpoints default value is changed (effecting future
4193 * associations only).
4195 * sack_delay - This parameter contains the number of milliseconds that
4196 * the user is requesting the delayed ACK timer be set to. Note that
4197 * this value is defined in the standard to be between 200 and 500
4200 * sack_freq - This parameter contains the number of packets that must
4201 * be received before a sack is sent without waiting for the delay
4202 * timer to expire. The default value for this is 2, setting this
4203 * value to 1 will disable the delayed sack algorithm.
4205 static int sctp_getsockopt_delayed_ack(struct sock
*sk
, int len
,
4206 char __user
*optval
,
4209 struct sctp_sack_info params
;
4210 struct sctp_association
*asoc
= NULL
;
4211 struct sctp_sock
*sp
= sctp_sk(sk
);
4213 if (len
>= sizeof(struct sctp_sack_info
)) {
4214 len
= sizeof(struct sctp_sack_info
);
4216 if (copy_from_user(¶ms
, optval
, len
))
4218 } else if (len
== sizeof(struct sctp_assoc_value
)) {
4219 printk(KERN_WARNING
"SCTP: Use of struct sctp_assoc_value "
4220 "in delayed_ack socket option deprecated\n");
4221 printk(KERN_WARNING
"SCTP: Use struct sctp_sack_info instead\n");
4222 if (copy_from_user(¶ms
, optval
, len
))
4227 /* Get association, if sack_assoc_id != 0 and the socket is a one
4228 * to many style socket, and an association was not found, then
4229 * the id was invalid.
4231 asoc
= sctp_id2assoc(sk
, params
.sack_assoc_id
);
4232 if (!asoc
&& params
.sack_assoc_id
&& sctp_style(sk
, UDP
))
4236 /* Fetch association values. */
4237 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
) {
4238 params
.sack_delay
= jiffies_to_msecs(
4240 params
.sack_freq
= asoc
->sackfreq
;
4243 params
.sack_delay
= 0;
4244 params
.sack_freq
= 1;
4247 /* Fetch socket values. */
4248 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
) {
4249 params
.sack_delay
= sp
->sackdelay
;
4250 params
.sack_freq
= sp
->sackfreq
;
4252 params
.sack_delay
= 0;
4253 params
.sack_freq
= 1;
4257 if (copy_to_user(optval
, ¶ms
, len
))
4260 if (put_user(len
, optlen
))
4266 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4268 * Applications can specify protocol parameters for the default association
4269 * initialization. The option name argument to setsockopt() and getsockopt()
4272 * Setting initialization parameters is effective only on an unconnected
4273 * socket (for UDP-style sockets only future associations are effected
4274 * by the change). With TCP-style sockets, this option is inherited by
4275 * sockets derived from a listener socket.
4277 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
4279 if (len
< sizeof(struct sctp_initmsg
))
4281 len
= sizeof(struct sctp_initmsg
);
4282 if (put_user(len
, optlen
))
4284 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
4290 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
4291 char __user
*optval
, int __user
*optlen
)
4293 struct sctp_association
*asoc
;
4295 struct sctp_getaddrs getaddrs
;
4296 struct sctp_transport
*from
;
4298 union sctp_addr temp
;
4299 struct sctp_sock
*sp
= sctp_sk(sk
);
4304 if (len
< sizeof(struct sctp_getaddrs
))
4307 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4310 /* For UDP-style sockets, id specifies the association to query. */
4311 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4315 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4316 space_left
= len
- offsetof(struct sctp_getaddrs
,addrs
);
4318 list_for_each_entry(from
, &asoc
->peer
.transport_addr_list
,
4320 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
4321 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4322 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4323 if (space_left
< addrlen
)
4325 if (copy_to_user(to
, &temp
, addrlen
))
4329 space_left
-= addrlen
;
4332 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4334 bytes_copied
= ((char __user
*)to
) - optval
;
4335 if (put_user(bytes_copied
, optlen
))
4341 static int sctp_copy_laddrs(struct sock
*sk
, __u16 port
, void *to
,
4342 size_t space_left
, int *bytes_copied
)
4344 struct sctp_sockaddr_entry
*addr
;
4345 union sctp_addr temp
;
4350 list_for_each_entry_rcu(addr
, &sctp_local_addr_list
, list
) {
4354 if ((PF_INET
== sk
->sk_family
) &&
4355 (AF_INET6
== addr
->a
.sa
.sa_family
))
4357 if ((PF_INET6
== sk
->sk_family
) &&
4358 inet_v6_ipv6only(sk
) &&
4359 (AF_INET
== addr
->a
.sa
.sa_family
))
4361 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4362 if (!temp
.v4
.sin_port
)
4363 temp
.v4
.sin_port
= htons(port
);
4365 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
4367 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4368 if (space_left
< addrlen
) {
4372 memcpy(to
, &temp
, addrlen
);
4376 space_left
-= addrlen
;
4377 *bytes_copied
+= addrlen
;
4385 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4386 char __user
*optval
, int __user
*optlen
)
4388 struct sctp_bind_addr
*bp
;
4389 struct sctp_association
*asoc
;
4391 struct sctp_getaddrs getaddrs
;
4392 struct sctp_sockaddr_entry
*addr
;
4394 union sctp_addr temp
;
4395 struct sctp_sock
*sp
= sctp_sk(sk
);
4399 int bytes_copied
= 0;
4403 if (len
< sizeof(struct sctp_getaddrs
))
4406 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4410 * For UDP-style sockets, id specifies the association to query.
4411 * If the id field is set to the value '0' then the locally bound
4412 * addresses are returned without regard to any particular
4415 if (0 == getaddrs
.assoc_id
) {
4416 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4418 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4421 bp
= &asoc
->base
.bind_addr
;
4424 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4425 space_left
= len
- offsetof(struct sctp_getaddrs
,addrs
);
4427 addrs
= kmalloc(space_left
, GFP_KERNEL
);
4431 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4432 * addresses from the global local address list.
4434 if (sctp_list_single_entry(&bp
->address_list
)) {
4435 addr
= list_entry(bp
->address_list
.next
,
4436 struct sctp_sockaddr_entry
, list
);
4437 if (sctp_is_any(sk
, &addr
->a
)) {
4438 cnt
= sctp_copy_laddrs(sk
, bp
->port
, addrs
,
4439 space_left
, &bytes_copied
);
4449 /* Protection on the bound address list is not needed since
4450 * in the socket option context we hold a socket lock and
4451 * thus the bound address list can't change.
4453 list_for_each_entry(addr
, &bp
->address_list
, list
) {
4454 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4455 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4456 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4457 if (space_left
< addrlen
) {
4461 memcpy(buf
, &temp
, addrlen
);
4463 bytes_copied
+= addrlen
;
4465 space_left
-= addrlen
;
4469 if (copy_to_user(to
, addrs
, bytes_copied
)) {
4473 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
)) {
4477 if (put_user(bytes_copied
, optlen
))
4484 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4486 * Requests that the local SCTP stack use the enclosed peer address as
4487 * the association primary. The enclosed address must be one of the
4488 * association peer's addresses.
4490 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4491 char __user
*optval
, int __user
*optlen
)
4493 struct sctp_prim prim
;
4494 struct sctp_association
*asoc
;
4495 struct sctp_sock
*sp
= sctp_sk(sk
);
4497 if (len
< sizeof(struct sctp_prim
))
4500 len
= sizeof(struct sctp_prim
);
4502 if (copy_from_user(&prim
, optval
, len
))
4505 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4509 if (!asoc
->peer
.primary_path
)
4512 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4513 asoc
->peer
.primary_path
->af_specific
->sockaddr_len
);
4515 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4516 (union sctp_addr
*)&prim
.ssp_addr
);
4518 if (put_user(len
, optlen
))
4520 if (copy_to_user(optval
, &prim
, len
))
4527 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4529 * Requests that the local endpoint set the specified Adaptation Layer
4530 * Indication parameter for all future INIT and INIT-ACK exchanges.
4532 static int sctp_getsockopt_adaptation_layer(struct sock
*sk
, int len
,
4533 char __user
*optval
, int __user
*optlen
)
4535 struct sctp_setadaptation adaptation
;
4537 if (len
< sizeof(struct sctp_setadaptation
))
4540 len
= sizeof(struct sctp_setadaptation
);
4542 adaptation
.ssb_adaptation_ind
= sctp_sk(sk
)->adaptation_ind
;
4544 if (put_user(len
, optlen
))
4546 if (copy_to_user(optval
, &adaptation
, len
))
4554 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4556 * Applications that wish to use the sendto() system call may wish to
4557 * specify a default set of parameters that would normally be supplied
4558 * through the inclusion of ancillary data. This socket option allows
4559 * such an application to set the default sctp_sndrcvinfo structure.
4562 * The application that wishes to use this socket option simply passes
4563 * in to this call the sctp_sndrcvinfo structure defined in Section
4564 * 5.2.2) The input parameters accepted by this call include
4565 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4566 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4567 * to this call if the caller is using the UDP model.
4569 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4571 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4572 int len
, char __user
*optval
,
4575 struct sctp_sndrcvinfo info
;
4576 struct sctp_association
*asoc
;
4577 struct sctp_sock
*sp
= sctp_sk(sk
);
4579 if (len
< sizeof(struct sctp_sndrcvinfo
))
4582 len
= sizeof(struct sctp_sndrcvinfo
);
4584 if (copy_from_user(&info
, optval
, len
))
4587 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4588 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4592 info
.sinfo_stream
= asoc
->default_stream
;
4593 info
.sinfo_flags
= asoc
->default_flags
;
4594 info
.sinfo_ppid
= asoc
->default_ppid
;
4595 info
.sinfo_context
= asoc
->default_context
;
4596 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4598 info
.sinfo_stream
= sp
->default_stream
;
4599 info
.sinfo_flags
= sp
->default_flags
;
4600 info
.sinfo_ppid
= sp
->default_ppid
;
4601 info
.sinfo_context
= sp
->default_context
;
4602 info
.sinfo_timetolive
= sp
->default_timetolive
;
4605 if (put_user(len
, optlen
))
4607 if (copy_to_user(optval
, &info
, len
))
4615 * 7.1.5 SCTP_NODELAY
4617 * Turn on/off any Nagle-like algorithm. This means that packets are
4618 * generally sent as soon as possible and no unnecessary delays are
4619 * introduced, at the cost of more packets in the network. Expects an
4620 * integer boolean flag.
4623 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4624 char __user
*optval
, int __user
*optlen
)
4628 if (len
< sizeof(int))
4632 val
= (sctp_sk(sk
)->nodelay
== 1);
4633 if (put_user(len
, optlen
))
4635 if (copy_to_user(optval
, &val
, len
))
4642 * 7.1.1 SCTP_RTOINFO
4644 * The protocol parameters used to initialize and bound retransmission
4645 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4646 * and modify these parameters.
4647 * All parameters are time values, in milliseconds. A value of 0, when
4648 * modifying the parameters, indicates that the current value should not
4652 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4653 char __user
*optval
,
4654 int __user
*optlen
) {
4655 struct sctp_rtoinfo rtoinfo
;
4656 struct sctp_association
*asoc
;
4658 if (len
< sizeof (struct sctp_rtoinfo
))
4661 len
= sizeof(struct sctp_rtoinfo
);
4663 if (copy_from_user(&rtoinfo
, optval
, len
))
4666 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4668 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4671 /* Values corresponding to the specific association. */
4673 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4674 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4675 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4677 /* Values corresponding to the endpoint. */
4678 struct sctp_sock
*sp
= sctp_sk(sk
);
4680 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4681 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4682 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4685 if (put_user(len
, optlen
))
4688 if (copy_to_user(optval
, &rtoinfo
, len
))
4696 * 7.1.2 SCTP_ASSOCINFO
4698 * This option is used to tune the maximum retransmission attempts
4699 * of the association.
4700 * Returns an error if the new association retransmission value is
4701 * greater than the sum of the retransmission value of the peer.
4702 * See [SCTP] for more information.
4705 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4706 char __user
*optval
,
4710 struct sctp_assocparams assocparams
;
4711 struct sctp_association
*asoc
;
4712 struct list_head
*pos
;
4715 if (len
< sizeof (struct sctp_assocparams
))
4718 len
= sizeof(struct sctp_assocparams
);
4720 if (copy_from_user(&assocparams
, optval
, len
))
4723 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4725 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4728 /* Values correspoinding to the specific association */
4730 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4731 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4732 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4733 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4735 (asoc
->cookie_life
.tv_usec
4738 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4742 assocparams
.sasoc_number_peer_destinations
= cnt
;
4744 /* Values corresponding to the endpoint */
4745 struct sctp_sock
*sp
= sctp_sk(sk
);
4747 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4748 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4749 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4750 assocparams
.sasoc_cookie_life
=
4751 sp
->assocparams
.sasoc_cookie_life
;
4752 assocparams
.sasoc_number_peer_destinations
=
4754 sasoc_number_peer_destinations
;
4757 if (put_user(len
, optlen
))
4760 if (copy_to_user(optval
, &assocparams
, len
))
4767 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4769 * This socket option is a boolean flag which turns on or off mapped V4
4770 * addresses. If this option is turned on and the socket is type
4771 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4772 * If this option is turned off, then no mapping will be done of V4
4773 * addresses and a user will receive both PF_INET6 and PF_INET type
4774 * addresses on the socket.
4776 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4777 char __user
*optval
, int __user
*optlen
)
4780 struct sctp_sock
*sp
= sctp_sk(sk
);
4782 if (len
< sizeof(int))
4787 if (put_user(len
, optlen
))
4789 if (copy_to_user(optval
, &val
, len
))
4796 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4797 * (chapter and verse is quoted at sctp_setsockopt_context())
4799 static int sctp_getsockopt_context(struct sock
*sk
, int len
,
4800 char __user
*optval
, int __user
*optlen
)
4802 struct sctp_assoc_value params
;
4803 struct sctp_sock
*sp
;
4804 struct sctp_association
*asoc
;
4806 if (len
< sizeof(struct sctp_assoc_value
))
4809 len
= sizeof(struct sctp_assoc_value
);
4811 if (copy_from_user(¶ms
, optval
, len
))
4816 if (params
.assoc_id
!= 0) {
4817 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4820 params
.assoc_value
= asoc
->default_rcv_context
;
4822 params
.assoc_value
= sp
->default_rcv_context
;
4825 if (put_user(len
, optlen
))
4827 if (copy_to_user(optval
, ¶ms
, len
))
4834 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
4835 * This option will get or set the maximum size to put in any outgoing
4836 * SCTP DATA chunk. If a message is larger than this size it will be
4837 * fragmented by SCTP into the specified size. Note that the underlying
4838 * SCTP implementation may fragment into smaller sized chunks when the
4839 * PMTU of the underlying association is smaller than the value set by
4840 * the user. The default value for this option is '0' which indicates
4841 * the user is NOT limiting fragmentation and only the PMTU will effect
4842 * SCTP's choice of DATA chunk size. Note also that values set larger
4843 * than the maximum size of an IP datagram will effectively let SCTP
4844 * control fragmentation (i.e. the same as setting this option to 0).
4846 * The following structure is used to access and modify this parameter:
4848 * struct sctp_assoc_value {
4849 * sctp_assoc_t assoc_id;
4850 * uint32_t assoc_value;
4853 * assoc_id: This parameter is ignored for one-to-one style sockets.
4854 * For one-to-many style sockets this parameter indicates which
4855 * association the user is performing an action upon. Note that if
4856 * this field's value is zero then the endpoints default value is
4857 * changed (effecting future associations only).
4858 * assoc_value: This parameter specifies the maximum size in bytes.
4860 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4861 char __user
*optval
, int __user
*optlen
)
4863 struct sctp_assoc_value params
;
4864 struct sctp_association
*asoc
;
4866 if (len
== sizeof(int)) {
4868 "SCTP: Use of int in maxseg socket option deprecated\n");
4870 "SCTP: Use struct sctp_assoc_value instead\n");
4871 params
.assoc_id
= 0;
4872 } else if (len
>= sizeof(struct sctp_assoc_value
)) {
4873 len
= sizeof(struct sctp_assoc_value
);
4874 if (copy_from_user(¶ms
, optval
, sizeof(params
)))
4879 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4880 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
4884 params
.assoc_value
= asoc
->frag_point
;
4886 params
.assoc_value
= sctp_sk(sk
)->user_frag
;
4888 if (put_user(len
, optlen
))
4890 if (len
== sizeof(int)) {
4891 if (copy_to_user(optval
, ¶ms
.assoc_value
, len
))
4894 if (copy_to_user(optval
, ¶ms
, len
))
4902 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4903 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4905 static int sctp_getsockopt_fragment_interleave(struct sock
*sk
, int len
,
4906 char __user
*optval
, int __user
*optlen
)
4910 if (len
< sizeof(int))
4915 val
= sctp_sk(sk
)->frag_interleave
;
4916 if (put_user(len
, optlen
))
4918 if (copy_to_user(optval
, &val
, len
))
4925 * 7.1.25. Set or Get the sctp partial delivery point
4926 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4928 static int sctp_getsockopt_partial_delivery_point(struct sock
*sk
, int len
,
4929 char __user
*optval
,
4934 if (len
< sizeof(u32
))
4939 val
= sctp_sk(sk
)->pd_point
;
4940 if (put_user(len
, optlen
))
4942 if (copy_to_user(optval
, &val
, len
))
4949 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4950 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4952 static int sctp_getsockopt_maxburst(struct sock
*sk
, int len
,
4953 char __user
*optval
,
4956 struct sctp_assoc_value params
;
4957 struct sctp_sock
*sp
;
4958 struct sctp_association
*asoc
;
4960 if (len
== sizeof(int)) {
4962 "SCTP: Use of int in max_burst socket option deprecated\n");
4964 "SCTP: Use struct sctp_assoc_value instead\n");
4965 params
.assoc_id
= 0;
4966 } else if (len
>= sizeof(struct sctp_assoc_value
)) {
4967 len
= sizeof(struct sctp_assoc_value
);
4968 if (copy_from_user(¶ms
, optval
, len
))
4975 if (params
.assoc_id
!= 0) {
4976 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
4979 params
.assoc_value
= asoc
->max_burst
;
4981 params
.assoc_value
= sp
->max_burst
;
4983 if (len
== sizeof(int)) {
4984 if (copy_to_user(optval
, ¶ms
.assoc_value
, len
))
4987 if (copy_to_user(optval
, ¶ms
, len
))
4995 static int sctp_getsockopt_hmac_ident(struct sock
*sk
, int len
,
4996 char __user
*optval
, int __user
*optlen
)
4998 struct sctp_hmacalgo __user
*p
= (void __user
*)optval
;
4999 struct sctp_hmac_algo_param
*hmacs
;
5003 if (!sctp_auth_enable
)
5006 hmacs
= sctp_sk(sk
)->ep
->auth_hmacs_list
;
5007 data_len
= ntohs(hmacs
->param_hdr
.length
) - sizeof(sctp_paramhdr_t
);
5009 if (len
< sizeof(struct sctp_hmacalgo
) + data_len
)
5012 len
= sizeof(struct sctp_hmacalgo
) + data_len
;
5013 num_idents
= data_len
/ sizeof(u16
);
5015 if (put_user(len
, optlen
))
5017 if (put_user(num_idents
, &p
->shmac_num_idents
))
5019 if (copy_to_user(p
->shmac_idents
, hmacs
->hmac_ids
, data_len
))
5024 static int sctp_getsockopt_active_key(struct sock
*sk
, int len
,
5025 char __user
*optval
, int __user
*optlen
)
5027 struct sctp_authkeyid val
;
5028 struct sctp_association
*asoc
;
5030 if (!sctp_auth_enable
)
5033 if (len
< sizeof(struct sctp_authkeyid
))
5035 if (copy_from_user(&val
, optval
, sizeof(struct sctp_authkeyid
)))
5038 asoc
= sctp_id2assoc(sk
, val
.scact_assoc_id
);
5039 if (!asoc
&& val
.scact_assoc_id
&& sctp_style(sk
, UDP
))
5043 val
.scact_keynumber
= asoc
->active_key_id
;
5045 val
.scact_keynumber
= sctp_sk(sk
)->ep
->active_key_id
;
5047 len
= sizeof(struct sctp_authkeyid
);
5048 if (put_user(len
, optlen
))
5050 if (copy_to_user(optval
, &val
, len
))
5056 static int sctp_getsockopt_peer_auth_chunks(struct sock
*sk
, int len
,
5057 char __user
*optval
, int __user
*optlen
)
5059 struct sctp_authchunks __user
*p
= (void __user
*)optval
;
5060 struct sctp_authchunks val
;
5061 struct sctp_association
*asoc
;
5062 struct sctp_chunks_param
*ch
;
5066 if (!sctp_auth_enable
)
5069 if (len
< sizeof(struct sctp_authchunks
))
5072 if (copy_from_user(&val
, optval
, sizeof(struct sctp_authchunks
)))
5075 to
= p
->gauth_chunks
;
5076 asoc
= sctp_id2assoc(sk
, val
.gauth_assoc_id
);
5080 ch
= asoc
->peer
.peer_chunks
;
5084 /* See if the user provided enough room for all the data */
5085 num_chunks
= ntohs(ch
->param_hdr
.length
) - sizeof(sctp_paramhdr_t
);
5086 if (len
< num_chunks
)
5089 if (copy_to_user(to
, ch
->chunks
, num_chunks
))
5092 len
= sizeof(struct sctp_authchunks
) + num_chunks
;
5093 if (put_user(len
, optlen
)) return -EFAULT
;
5094 if (put_user(num_chunks
, &p
->gauth_number_of_chunks
))
5099 static int sctp_getsockopt_local_auth_chunks(struct sock
*sk
, int len
,
5100 char __user
*optval
, int __user
*optlen
)
5102 struct sctp_authchunks __user
*p
= (void __user
*)optval
;
5103 struct sctp_authchunks val
;
5104 struct sctp_association
*asoc
;
5105 struct sctp_chunks_param
*ch
;
5109 if (!sctp_auth_enable
)
5112 if (len
< sizeof(struct sctp_authchunks
))
5115 if (copy_from_user(&val
, optval
, sizeof(struct sctp_authchunks
)))
5118 to
= p
->gauth_chunks
;
5119 asoc
= sctp_id2assoc(sk
, val
.gauth_assoc_id
);
5120 if (!asoc
&& val
.gauth_assoc_id
&& sctp_style(sk
, UDP
))
5124 ch
= (struct sctp_chunks_param
*)asoc
->c
.auth_chunks
;
5126 ch
= sctp_sk(sk
)->ep
->auth_chunk_list
;
5131 num_chunks
= ntohs(ch
->param_hdr
.length
) - sizeof(sctp_paramhdr_t
);
5132 if (len
< sizeof(struct sctp_authchunks
) + num_chunks
)
5135 if (copy_to_user(to
, ch
->chunks
, num_chunks
))
5138 len
= sizeof(struct sctp_authchunks
) + num_chunks
;
5139 if (put_user(len
, optlen
))
5141 if (put_user(num_chunks
, &p
->gauth_number_of_chunks
))
5148 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5149 * This option gets the current number of associations that are attached
5150 * to a one-to-many style socket. The option value is an uint32_t.
5152 static int sctp_getsockopt_assoc_number(struct sock
*sk
, int len
,
5153 char __user
*optval
, int __user
*optlen
)
5155 struct sctp_sock
*sp
= sctp_sk(sk
);
5156 struct sctp_association
*asoc
;
5159 if (sctp_style(sk
, TCP
))
5162 if (len
< sizeof(u32
))
5167 list_for_each_entry(asoc
, &(sp
->ep
->asocs
), asocs
) {
5171 if (put_user(len
, optlen
))
5173 if (copy_to_user(optval
, &val
, len
))
5179 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
5180 char __user
*optval
, int __user
*optlen
)
5185 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5188 /* I can hardly begin to describe how wrong this is. This is
5189 * so broken as to be worse than useless. The API draft
5190 * REALLY is NOT helpful here... I am not convinced that the
5191 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5192 * are at all well-founded.
5194 if (level
!= SOL_SCTP
) {
5195 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
5197 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
5201 if (get_user(len
, optlen
))
5208 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
5210 case SCTP_DISABLE_FRAGMENTS
:
5211 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
5215 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
5217 case SCTP_AUTOCLOSE
:
5218 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
5220 case SCTP_SOCKOPT_PEELOFF
:
5221 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
5223 case SCTP_PEER_ADDR_PARAMS
:
5224 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
5227 case SCTP_DELAYED_ACK
:
5228 retval
= sctp_getsockopt_delayed_ack(sk
, len
, optval
,
5232 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
5234 case SCTP_GET_PEER_ADDRS
:
5235 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
5238 case SCTP_GET_LOCAL_ADDRS
:
5239 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
5242 case SCTP_SOCKOPT_CONNECTX3
:
5243 retval
= sctp_getsockopt_connectx3(sk
, len
, optval
, optlen
);
5245 case SCTP_DEFAULT_SEND_PARAM
:
5246 retval
= sctp_getsockopt_default_send_param(sk
, len
,
5249 case SCTP_PRIMARY_ADDR
:
5250 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
5253 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
5256 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
5258 case SCTP_ASSOCINFO
:
5259 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
5261 case SCTP_I_WANT_MAPPED_V4_ADDR
:
5262 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
5265 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
5267 case SCTP_GET_PEER_ADDR_INFO
:
5268 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
5271 case SCTP_ADAPTATION_LAYER
:
5272 retval
= sctp_getsockopt_adaptation_layer(sk
, len
, optval
,
5276 retval
= sctp_getsockopt_context(sk
, len
, optval
, optlen
);
5278 case SCTP_FRAGMENT_INTERLEAVE
:
5279 retval
= sctp_getsockopt_fragment_interleave(sk
, len
, optval
,
5282 case SCTP_PARTIAL_DELIVERY_POINT
:
5283 retval
= sctp_getsockopt_partial_delivery_point(sk
, len
, optval
,
5286 case SCTP_MAX_BURST
:
5287 retval
= sctp_getsockopt_maxburst(sk
, len
, optval
, optlen
);
5290 case SCTP_AUTH_CHUNK
:
5291 case SCTP_AUTH_DELETE_KEY
:
5292 retval
= -EOPNOTSUPP
;
5294 case SCTP_HMAC_IDENT
:
5295 retval
= sctp_getsockopt_hmac_ident(sk
, len
, optval
, optlen
);
5297 case SCTP_AUTH_ACTIVE_KEY
:
5298 retval
= sctp_getsockopt_active_key(sk
, len
, optval
, optlen
);
5300 case SCTP_PEER_AUTH_CHUNKS
:
5301 retval
= sctp_getsockopt_peer_auth_chunks(sk
, len
, optval
,
5304 case SCTP_LOCAL_AUTH_CHUNKS
:
5305 retval
= sctp_getsockopt_local_auth_chunks(sk
, len
, optval
,
5308 case SCTP_GET_ASSOC_NUMBER
:
5309 retval
= sctp_getsockopt_assoc_number(sk
, len
, optval
, optlen
);
5312 retval
= -ENOPROTOOPT
;
5316 sctp_release_sock(sk
);
5320 static void sctp_hash(struct sock
*sk
)
5325 static void sctp_unhash(struct sock
*sk
)
5330 static struct sctp_bind_bucket
*sctp_bucket_create(
5331 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
5333 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
5335 struct sctp_bind_hashbucket
*head
; /* hash list */
5336 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5337 struct hlist_node
*node
;
5338 unsigned short snum
;
5341 snum
= ntohs(addr
->v4
.sin_port
);
5343 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
5344 sctp_local_bh_disable();
5347 /* Search for an available port. */
5348 int low
, high
, remaining
, index
;
5351 inet_get_local_port_range(&low
, &high
);
5352 remaining
= (high
- low
) + 1;
5353 rover
= net_random() % remaining
+ low
;
5357 if ((rover
< low
) || (rover
> high
))
5359 if (inet_is_reserved_local_port(rover
))
5361 index
= sctp_phashfn(rover
);
5362 head
= &sctp_port_hashtable
[index
];
5363 sctp_spin_lock(&head
->lock
);
5364 sctp_for_each_hentry(pp
, node
, &head
->chain
)
5365 if (pp
->port
== rover
)
5369 sctp_spin_unlock(&head
->lock
);
5370 } while (--remaining
> 0);
5372 /* Exhausted local port range during search? */
5377 /* OK, here is the one we will use. HEAD (the port
5378 * hash table list entry) is non-NULL and we hold it's
5383 /* We are given an specific port number; we verify
5384 * that it is not being used. If it is used, we will
5385 * exahust the search in the hash list corresponding
5386 * to the port number (snum) - we detect that with the
5387 * port iterator, pp being NULL.
5389 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
5390 sctp_spin_lock(&head
->lock
);
5391 sctp_for_each_hentry(pp
, node
, &head
->chain
) {
5392 if (pp
->port
== snum
)
5399 if (!hlist_empty(&pp
->owner
)) {
5400 /* We had a port hash table hit - there is an
5401 * available port (pp != NULL) and it is being
5402 * used by other socket (pp->owner not empty); that other
5403 * socket is going to be sk2.
5405 int reuse
= sk
->sk_reuse
;
5408 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5409 if (pp
->fastreuse
&& sk
->sk_reuse
&&
5410 sk
->sk_state
!= SCTP_SS_LISTENING
)
5413 /* Run through the list of sockets bound to the port
5414 * (pp->port) [via the pointers bind_next and
5415 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5416 * we get the endpoint they describe and run through
5417 * the endpoint's list of IP (v4 or v6) addresses,
5418 * comparing each of the addresses with the address of
5419 * the socket sk. If we find a match, then that means
5420 * that this port/socket (sk) combination are already
5423 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
5424 struct sctp_endpoint
*ep2
;
5425 ep2
= sctp_sk(sk2
)->ep
;
5428 (reuse
&& sk2
->sk_reuse
&&
5429 sk2
->sk_state
!= SCTP_SS_LISTENING
))
5432 if (sctp_bind_addr_conflict(&ep2
->base
.bind_addr
, addr
,
5433 sctp_sk(sk2
), sctp_sk(sk
))) {
5438 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5441 /* If there was a hash table miss, create a new port. */
5443 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
5446 /* In either case (hit or miss), make sure fastreuse is 1 only
5447 * if sk->sk_reuse is too (that is, if the caller requested
5448 * SO_REUSEADDR on this socket -sk-).
5450 if (hlist_empty(&pp
->owner
)) {
5451 if (sk
->sk_reuse
&& sk
->sk_state
!= SCTP_SS_LISTENING
)
5455 } else if (pp
->fastreuse
&&
5456 (!sk
->sk_reuse
|| sk
->sk_state
== SCTP_SS_LISTENING
))
5460 if (!sctp_sk(sk
)->bind_hash
) {
5461 inet_sk(sk
)->inet_num
= snum
;
5462 sk_add_bind_node(sk
, &pp
->owner
);
5463 sctp_sk(sk
)->bind_hash
= pp
;
5468 sctp_spin_unlock(&head
->lock
);
5471 sctp_local_bh_enable();
5475 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5476 * port is requested.
5478 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
5481 union sctp_addr addr
;
5482 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
5484 /* Set up a dummy address struct from the sk. */
5485 af
->from_sk(&addr
, sk
);
5486 addr
.v4
.sin_port
= htons(snum
);
5488 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5489 ret
= sctp_get_port_local(sk
, &addr
);
5491 return (ret
? 1 : 0);
5495 * Move a socket to LISTENING state.
5497 SCTP_STATIC
int sctp_listen_start(struct sock
*sk
, int backlog
)
5499 struct sctp_sock
*sp
= sctp_sk(sk
);
5500 struct sctp_endpoint
*ep
= sp
->ep
;
5501 struct crypto_hash
*tfm
= NULL
;
5503 /* Allocate HMAC for generating cookie. */
5504 if (!sctp_sk(sk
)->hmac
&& sctp_hmac_alg
) {
5505 tfm
= crypto_alloc_hash(sctp_hmac_alg
, 0, CRYPTO_ALG_ASYNC
);
5507 if (net_ratelimit()) {
5509 "SCTP: failed to load transform for %s: %ld\n",
5510 sctp_hmac_alg
, PTR_ERR(tfm
));
5514 sctp_sk(sk
)->hmac
= tfm
;
5518 * If a bind() or sctp_bindx() is not called prior to a listen()
5519 * call that allows new associations to be accepted, the system
5520 * picks an ephemeral port and will choose an address set equivalent
5521 * to binding with a wildcard address.
5523 * This is not currently spelled out in the SCTP sockets
5524 * extensions draft, but follows the practice as seen in TCP
5528 sk
->sk_state
= SCTP_SS_LISTENING
;
5529 if (!ep
->base
.bind_addr
.port
) {
5530 if (sctp_autobind(sk
))
5533 if (sctp_get_port(sk
, inet_sk(sk
)->inet_num
)) {
5534 sk
->sk_state
= SCTP_SS_CLOSED
;
5539 sk
->sk_max_ack_backlog
= backlog
;
5540 sctp_hash_endpoint(ep
);
5545 * 4.1.3 / 5.1.3 listen()
5547 * By default, new associations are not accepted for UDP style sockets.
5548 * An application uses listen() to mark a socket as being able to
5549 * accept new associations.
5551 * On TCP style sockets, applications use listen() to ready the SCTP
5552 * endpoint for accepting inbound associations.
5554 * On both types of endpoints a backlog of '0' disables listening.
5556 * Move a socket to LISTENING state.
5558 int sctp_inet_listen(struct socket
*sock
, int backlog
)
5560 struct sock
*sk
= sock
->sk
;
5561 struct sctp_endpoint
*ep
= sctp_sk(sk
)->ep
;
5564 if (unlikely(backlog
< 0))
5569 /* Peeled-off sockets are not allowed to listen(). */
5570 if (sctp_style(sk
, UDP_HIGH_BANDWIDTH
))
5573 if (sock
->state
!= SS_UNCONNECTED
)
5576 /* If backlog is zero, disable listening. */
5578 if (sctp_sstate(sk
, CLOSED
))
5582 sctp_unhash_endpoint(ep
);
5583 sk
->sk_state
= SCTP_SS_CLOSED
;
5585 sctp_sk(sk
)->bind_hash
->fastreuse
= 1;
5589 /* If we are already listening, just update the backlog */
5590 if (sctp_sstate(sk
, LISTENING
))
5591 sk
->sk_max_ack_backlog
= backlog
;
5593 err
= sctp_listen_start(sk
, backlog
);
5600 sctp_release_sock(sk
);
5605 * This function is done by modeling the current datagram_poll() and the
5606 * tcp_poll(). Note that, based on these implementations, we don't
5607 * lock the socket in this function, even though it seems that,
5608 * ideally, locking or some other mechanisms can be used to ensure
5609 * the integrity of the counters (sndbuf and wmem_alloc) used
5610 * in this place. We assume that we don't need locks either until proven
5613 * Another thing to note is that we include the Async I/O support
5614 * here, again, by modeling the current TCP/UDP code. We don't have
5615 * a good way to test with it yet.
5617 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
5619 struct sock
*sk
= sock
->sk
;
5620 struct sctp_sock
*sp
= sctp_sk(sk
);
5623 poll_wait(file
, sk_sleep(sk
), wait
);
5625 /* A TCP-style listening socket becomes readable when the accept queue
5628 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
5629 return (!list_empty(&sp
->ep
->asocs
)) ?
5630 (POLLIN
| POLLRDNORM
) : 0;
5634 /* Is there any exceptional events? */
5635 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
5637 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5639 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
5642 /* Is it readable? Reconsider this code with TCP-style support. */
5643 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
5644 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
5645 mask
|= POLLIN
| POLLRDNORM
;
5647 /* The association is either gone or not ready. */
5648 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
5651 /* Is it writable? */
5652 if (sctp_writeable(sk
)) {
5653 mask
|= POLLOUT
| POLLWRNORM
;
5655 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
5657 * Since the socket is not locked, the buffer
5658 * might be made available after the writeable check and
5659 * before the bit is set. This could cause a lost I/O
5660 * signal. tcp_poll() has a race breaker for this race
5661 * condition. Based on their implementation, we put
5662 * in the following code to cover it as well.
5664 if (sctp_writeable(sk
))
5665 mask
|= POLLOUT
| POLLWRNORM
;
5670 /********************************************************************
5671 * 2nd Level Abstractions
5672 ********************************************************************/
5674 static struct sctp_bind_bucket
*sctp_bucket_create(
5675 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
5677 struct sctp_bind_bucket
*pp
;
5679 pp
= kmem_cache_alloc(sctp_bucket_cachep
, GFP_ATOMIC
);
5681 SCTP_DBG_OBJCNT_INC(bind_bucket
);
5684 INIT_HLIST_HEAD(&pp
->owner
);
5685 hlist_add_head(&pp
->node
, &head
->chain
);
5690 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5691 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
5693 if (pp
&& hlist_empty(&pp
->owner
)) {
5694 __hlist_del(&pp
->node
);
5695 kmem_cache_free(sctp_bucket_cachep
, pp
);
5696 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
5700 /* Release this socket's reference to a local port. */
5701 static inline void __sctp_put_port(struct sock
*sk
)
5703 struct sctp_bind_hashbucket
*head
=
5704 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->inet_num
)];
5705 struct sctp_bind_bucket
*pp
;
5707 sctp_spin_lock(&head
->lock
);
5708 pp
= sctp_sk(sk
)->bind_hash
;
5709 __sk_del_bind_node(sk
);
5710 sctp_sk(sk
)->bind_hash
= NULL
;
5711 inet_sk(sk
)->inet_num
= 0;
5712 sctp_bucket_destroy(pp
);
5713 sctp_spin_unlock(&head
->lock
);
5716 void sctp_put_port(struct sock
*sk
)
5718 sctp_local_bh_disable();
5719 __sctp_put_port(sk
);
5720 sctp_local_bh_enable();
5724 * The system picks an ephemeral port and choose an address set equivalent
5725 * to binding with a wildcard address.
5726 * One of those addresses will be the primary address for the association.
5727 * This automatically enables the multihoming capability of SCTP.
5729 static int sctp_autobind(struct sock
*sk
)
5731 union sctp_addr autoaddr
;
5735 /* Initialize a local sockaddr structure to INADDR_ANY. */
5736 af
= sctp_sk(sk
)->pf
->af
;
5738 port
= htons(inet_sk(sk
)->inet_num
);
5739 af
->inaddr_any(&autoaddr
, port
);
5741 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5744 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5747 * 4.2 The cmsghdr Structure *
5749 * When ancillary data is sent or received, any number of ancillary data
5750 * objects can be specified by the msg_control and msg_controllen members of
5751 * the msghdr structure, because each object is preceded by
5752 * a cmsghdr structure defining the object's length (the cmsg_len member).
5753 * Historically Berkeley-derived implementations have passed only one object
5754 * at a time, but this API allows multiple objects to be
5755 * passed in a single call to sendmsg() or recvmsg(). The following example
5756 * shows two ancillary data objects in a control buffer.
5758 * |<--------------------------- msg_controllen -------------------------->|
5761 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5763 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5766 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5768 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5771 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5772 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5774 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5776 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5783 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5784 sctp_cmsgs_t
*cmsgs
)
5786 struct cmsghdr
*cmsg
;
5787 struct msghdr
*my_msg
= (struct msghdr
*)msg
;
5789 for (cmsg
= CMSG_FIRSTHDR(msg
);
5791 cmsg
= CMSG_NXTHDR(my_msg
, cmsg
)) {
5792 if (!CMSG_OK(my_msg
, cmsg
))
5795 /* Should we parse this header or ignore? */
5796 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5799 /* Strictly check lengths following example in SCM code. */
5800 switch (cmsg
->cmsg_type
) {
5802 /* SCTP Socket API Extension
5803 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5805 * This cmsghdr structure provides information for
5806 * initializing new SCTP associations with sendmsg().
5807 * The SCTP_INITMSG socket option uses this same data
5808 * structure. This structure is not used for
5811 * cmsg_level cmsg_type cmsg_data[]
5812 * ------------ ------------ ----------------------
5813 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5815 if (cmsg
->cmsg_len
!=
5816 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5818 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5822 /* SCTP Socket API Extension
5823 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5825 * This cmsghdr structure specifies SCTP options for
5826 * sendmsg() and describes SCTP header information
5827 * about a received message through recvmsg().
5829 * cmsg_level cmsg_type cmsg_data[]
5830 * ------------ ------------ ----------------------
5831 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5833 if (cmsg
->cmsg_len
!=
5834 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5838 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5840 /* Minimally, validate the sinfo_flags. */
5841 if (cmsgs
->info
->sinfo_flags
&
5842 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5843 SCTP_ABORT
| SCTP_EOF
))
5855 * Wait for a packet..
5856 * Note: This function is the same function as in core/datagram.c
5857 * with a few modifications to make lksctp work.
5859 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5864 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
5866 /* Socket errors? */
5867 error
= sock_error(sk
);
5871 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5874 /* Socket shut down? */
5875 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5878 /* Sequenced packets can come disconnected. If so we report the
5883 /* Is there a good reason to think that we may receive some data? */
5884 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5887 /* Handle signals. */
5888 if (signal_pending(current
))
5891 /* Let another process have a go. Since we are going to sleep
5892 * anyway. Note: This may cause odd behaviors if the message
5893 * does not fit in the user's buffer, but this seems to be the
5894 * only way to honor MSG_DONTWAIT realistically.
5896 sctp_release_sock(sk
);
5897 *timeo_p
= schedule_timeout(*timeo_p
);
5901 finish_wait(sk_sleep(sk
), &wait
);
5905 error
= sock_intr_errno(*timeo_p
);
5908 finish_wait(sk_sleep(sk
), &wait
);
5913 /* Receive a datagram.
5914 * Note: This is pretty much the same routine as in core/datagram.c
5915 * with a few changes to make lksctp work.
5917 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5918 int noblock
, int *err
)
5921 struct sk_buff
*skb
;
5924 timeo
= sock_rcvtimeo(sk
, noblock
);
5926 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5927 timeo
, MAX_SCHEDULE_TIMEOUT
);
5930 /* Again only user level code calls this function,
5931 * so nothing interrupt level
5932 * will suddenly eat the receive_queue.
5934 * Look at current nfs client by the way...
5935 * However, this function was corrent in any case. 8)
5937 if (flags
& MSG_PEEK
) {
5938 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5939 skb
= skb_peek(&sk
->sk_receive_queue
);
5941 atomic_inc(&skb
->users
);
5942 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5944 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5950 /* Caller is allowed not to check sk->sk_err before calling. */
5951 error
= sock_error(sk
);
5955 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5958 /* User doesn't want to wait. */
5962 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5971 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5972 static void __sctp_write_space(struct sctp_association
*asoc
)
5974 struct sock
*sk
= asoc
->base
.sk
;
5975 struct socket
*sock
= sk
->sk_socket
;
5977 if ((sctp_wspace(asoc
) > 0) && sock
) {
5978 if (waitqueue_active(&asoc
->wait
))
5979 wake_up_interruptible(&asoc
->wait
);
5981 if (sctp_writeable(sk
)) {
5982 if (sk_sleep(sk
) && waitqueue_active(sk_sleep(sk
)))
5983 wake_up_interruptible(sk_sleep(sk
));
5985 /* Note that we try to include the Async I/O support
5986 * here by modeling from the current TCP/UDP code.
5987 * We have not tested with it yet.
5989 if (sock
->wq
->fasync_list
&&
5990 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5991 sock_wake_async(sock
,
5992 SOCK_WAKE_SPACE
, POLL_OUT
);
5997 /* Do accounting for the sndbuf space.
5998 * Decrement the used sndbuf space of the corresponding association by the
5999 * data size which was just transmitted(freed).
6001 static void sctp_wfree(struct sk_buff
*skb
)
6003 struct sctp_association
*asoc
;
6004 struct sctp_chunk
*chunk
;
6007 /* Get the saved chunk pointer. */
6008 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
6011 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
6012 sizeof(struct sk_buff
) +
6013 sizeof(struct sctp_chunk
);
6015 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
6018 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
6020 sk
->sk_wmem_queued
-= skb
->truesize
;
6021 sk_mem_uncharge(sk
, skb
->truesize
);
6024 __sctp_write_space(asoc
);
6026 sctp_association_put(asoc
);
6029 /* Do accounting for the receive space on the socket.
6030 * Accounting for the association is done in ulpevent.c
6031 * We set this as a destructor for the cloned data skbs so that
6032 * accounting is done at the correct time.
6034 void sctp_sock_rfree(struct sk_buff
*skb
)
6036 struct sock
*sk
= skb
->sk
;
6037 struct sctp_ulpevent
*event
= sctp_skb2event(skb
);
6039 atomic_sub(event
->rmem_len
, &sk
->sk_rmem_alloc
);
6042 * Mimic the behavior of sock_rfree
6044 sk_mem_uncharge(sk
, event
->rmem_len
);
6048 /* Helper function to wait for space in the sndbuf. */
6049 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
6052 struct sock
*sk
= asoc
->base
.sk
;
6054 long current_timeo
= *timeo_p
;
6057 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
6058 asoc
, (long)(*timeo_p
), msg_len
);
6060 /* Increment the association's refcnt. */
6061 sctp_association_hold(asoc
);
6063 /* Wait on the association specific sndbuf space. */
6065 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
6066 TASK_INTERRUPTIBLE
);
6069 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
6072 if (signal_pending(current
))
6073 goto do_interrupted
;
6074 if (msg_len
<= sctp_wspace(asoc
))
6077 /* Let another process have a go. Since we are going
6080 sctp_release_sock(sk
);
6081 current_timeo
= schedule_timeout(current_timeo
);
6082 BUG_ON(sk
!= asoc
->base
.sk
);
6085 *timeo_p
= current_timeo
;
6089 finish_wait(&asoc
->wait
, &wait
);
6091 /* Release the association's refcnt. */
6092 sctp_association_put(asoc
);
6101 err
= sock_intr_errno(*timeo_p
);
6109 void sctp_data_ready(struct sock
*sk
, int len
)
6111 struct socket_wq
*wq
;
6114 wq
= rcu_dereference(sk
->sk_wq
);
6115 if (wq_has_sleeper(wq
))
6116 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
6117 POLLRDNORM
| POLLRDBAND
);
6118 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
6122 /* If socket sndbuf has changed, wake up all per association waiters. */
6123 void sctp_write_space(struct sock
*sk
)
6125 struct sctp_association
*asoc
;
6127 /* Wake up the tasks in each wait queue. */
6128 list_for_each_entry(asoc
, &((sctp_sk(sk
))->ep
->asocs
), asocs
) {
6129 __sctp_write_space(asoc
);
6133 /* Is there any sndbuf space available on the socket?
6135 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
6136 * associations on the same socket. For a UDP-style socket with
6137 * multiple associations, it is possible for it to be "unwriteable"
6138 * prematurely. I assume that this is acceptable because
6139 * a premature "unwriteable" is better than an accidental "writeable" which
6140 * would cause an unwanted block under certain circumstances. For the 1-1
6141 * UDP-style sockets or TCP-style sockets, this code should work.
6144 static int sctp_writeable(struct sock
*sk
)
6148 amt
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
6154 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6155 * returns immediately with EINPROGRESS.
6157 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
6159 struct sock
*sk
= asoc
->base
.sk
;
6161 long current_timeo
= *timeo_p
;
6164 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__
, asoc
,
6167 /* Increment the association's refcnt. */
6168 sctp_association_hold(asoc
);
6171 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
6172 TASK_INTERRUPTIBLE
);
6175 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
6177 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
6180 if (signal_pending(current
))
6181 goto do_interrupted
;
6183 if (sctp_state(asoc
, ESTABLISHED
))
6186 /* Let another process have a go. Since we are going
6189 sctp_release_sock(sk
);
6190 current_timeo
= schedule_timeout(current_timeo
);
6193 *timeo_p
= current_timeo
;
6197 finish_wait(&asoc
->wait
, &wait
);
6199 /* Release the association's refcnt. */
6200 sctp_association_put(asoc
);
6205 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
6208 err
= -ECONNREFUSED
;
6212 err
= sock_intr_errno(*timeo_p
);
6220 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
6222 struct sctp_endpoint
*ep
;
6226 ep
= sctp_sk(sk
)->ep
;
6230 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
6231 TASK_INTERRUPTIBLE
);
6233 if (list_empty(&ep
->asocs
)) {
6234 sctp_release_sock(sk
);
6235 timeo
= schedule_timeout(timeo
);
6240 if (!sctp_sstate(sk
, LISTENING
))
6244 if (!list_empty(&ep
->asocs
))
6247 err
= sock_intr_errno(timeo
);
6248 if (signal_pending(current
))
6256 finish_wait(sk_sleep(sk
), &wait
);
6261 static void sctp_wait_for_close(struct sock
*sk
, long timeout
)
6266 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
6267 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
6269 sctp_release_sock(sk
);
6270 timeout
= schedule_timeout(timeout
);
6272 } while (!signal_pending(current
) && timeout
);
6274 finish_wait(sk_sleep(sk
), &wait
);
6277 static void sctp_skb_set_owner_r_frag(struct sk_buff
*skb
, struct sock
*sk
)
6279 struct sk_buff
*frag
;
6284 /* Don't forget the fragments. */
6285 skb_walk_frags(skb
, frag
)
6286 sctp_skb_set_owner_r_frag(frag
, sk
);
6289 sctp_skb_set_owner_r(skb
, sk
);
6292 void sctp_copy_sock(struct sock
*newsk
, struct sock
*sk
,
6293 struct sctp_association
*asoc
)
6295 struct inet_sock
*inet
= inet_sk(sk
);
6296 struct inet_sock
*newinet
;
6298 newsk
->sk_type
= sk
->sk_type
;
6299 newsk
->sk_bound_dev_if
= sk
->sk_bound_dev_if
;
6300 newsk
->sk_flags
= sk
->sk_flags
;
6301 newsk
->sk_no_check
= sk
->sk_no_check
;
6302 newsk
->sk_reuse
= sk
->sk_reuse
;
6304 newsk
->sk_shutdown
= sk
->sk_shutdown
;
6305 newsk
->sk_destruct
= inet_sock_destruct
;
6306 newsk
->sk_family
= sk
->sk_family
;
6307 newsk
->sk_protocol
= IPPROTO_SCTP
;
6308 newsk
->sk_backlog_rcv
= sk
->sk_prot
->backlog_rcv
;
6309 newsk
->sk_sndbuf
= sk
->sk_sndbuf
;
6310 newsk
->sk_rcvbuf
= sk
->sk_rcvbuf
;
6311 newsk
->sk_lingertime
= sk
->sk_lingertime
;
6312 newsk
->sk_rcvtimeo
= sk
->sk_rcvtimeo
;
6313 newsk
->sk_sndtimeo
= sk
->sk_sndtimeo
;
6315 newinet
= inet_sk(newsk
);
6317 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6318 * getsockname() and getpeername()
6320 newinet
->inet_sport
= inet
->inet_sport
;
6321 newinet
->inet_saddr
= inet
->inet_saddr
;
6322 newinet
->inet_rcv_saddr
= inet
->inet_rcv_saddr
;
6323 newinet
->inet_dport
= htons(asoc
->peer
.port
);
6324 newinet
->pmtudisc
= inet
->pmtudisc
;
6325 newinet
->inet_id
= asoc
->next_tsn
^ jiffies
;
6327 newinet
->uc_ttl
= inet
->uc_ttl
;
6328 newinet
->mc_loop
= 1;
6329 newinet
->mc_ttl
= 1;
6330 newinet
->mc_index
= 0;
6331 newinet
->mc_list
= NULL
;
6334 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6335 * and its messages to the newsk.
6337 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
6338 struct sctp_association
*assoc
,
6339 sctp_socket_type_t type
)
6341 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
6342 struct sctp_sock
*newsp
= sctp_sk(newsk
);
6343 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
6344 struct sctp_endpoint
*newep
= newsp
->ep
;
6345 struct sk_buff
*skb
, *tmp
;
6346 struct sctp_ulpevent
*event
;
6347 struct sctp_bind_hashbucket
*head
;
6349 /* Migrate socket buffer sizes and all the socket level options to the
6352 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
6353 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
6354 /* Brute force copy old sctp opt. */
6355 inet_sk_copy_descendant(newsk
, oldsk
);
6357 /* Restore the ep value that was overwritten with the above structure
6363 /* Hook this new socket in to the bind_hash list. */
6364 head
= &sctp_port_hashtable
[sctp_phashfn(inet_sk(oldsk
)->inet_num
)];
6365 sctp_local_bh_disable();
6366 sctp_spin_lock(&head
->lock
);
6367 pp
= sctp_sk(oldsk
)->bind_hash
;
6368 sk_add_bind_node(newsk
, &pp
->owner
);
6369 sctp_sk(newsk
)->bind_hash
= pp
;
6370 inet_sk(newsk
)->inet_num
= inet_sk(oldsk
)->inet_num
;
6371 sctp_spin_unlock(&head
->lock
);
6372 sctp_local_bh_enable();
6374 /* Copy the bind_addr list from the original endpoint to the new
6375 * endpoint so that we can handle restarts properly
6377 sctp_bind_addr_dup(&newsp
->ep
->base
.bind_addr
,
6378 &oldsp
->ep
->base
.bind_addr
, GFP_KERNEL
);
6380 /* Move any messages in the old socket's receive queue that are for the
6381 * peeled off association to the new socket's receive queue.
6383 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
6384 event
= sctp_skb2event(skb
);
6385 if (event
->asoc
== assoc
) {
6386 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
6387 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
6388 sctp_skb_set_owner_r_frag(skb
, newsk
);
6392 /* Clean up any messages pending delivery due to partial
6393 * delivery. Three cases:
6394 * 1) No partial deliver; no work.
6395 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6396 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6398 skb_queue_head_init(&newsp
->pd_lobby
);
6399 atomic_set(&sctp_sk(newsk
)->pd_mode
, assoc
->ulpq
.pd_mode
);
6401 if (atomic_read(&sctp_sk(oldsk
)->pd_mode
)) {
6402 struct sk_buff_head
*queue
;
6404 /* Decide which queue to move pd_lobby skbs to. */
6405 if (assoc
->ulpq
.pd_mode
) {
6406 queue
= &newsp
->pd_lobby
;
6408 queue
= &newsk
->sk_receive_queue
;
6410 /* Walk through the pd_lobby, looking for skbs that
6411 * need moved to the new socket.
6413 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
6414 event
= sctp_skb2event(skb
);
6415 if (event
->asoc
== assoc
) {
6416 __skb_unlink(skb
, &oldsp
->pd_lobby
);
6417 __skb_queue_tail(queue
, skb
);
6418 sctp_skb_set_owner_r_frag(skb
, newsk
);
6422 /* Clear up any skbs waiting for the partial
6423 * delivery to finish.
6425 if (assoc
->ulpq
.pd_mode
)
6426 sctp_clear_pd(oldsk
, NULL
);
6430 sctp_skb_for_each(skb
, &assoc
->ulpq
.reasm
, tmp
)
6431 sctp_skb_set_owner_r_frag(skb
, newsk
);
6433 sctp_skb_for_each(skb
, &assoc
->ulpq
.lobby
, tmp
)
6434 sctp_skb_set_owner_r_frag(skb
, newsk
);
6436 /* Set the type of socket to indicate that it is peeled off from the
6437 * original UDP-style socket or created with the accept() call on a
6438 * TCP-style socket..
6442 /* Mark the new socket "in-use" by the user so that any packets
6443 * that may arrive on the association after we've moved it are
6444 * queued to the backlog. This prevents a potential race between
6445 * backlog processing on the old socket and new-packet processing
6446 * on the new socket.
6448 * The caller has just allocated newsk so we can guarantee that other
6449 * paths won't try to lock it and then oldsk.
6451 lock_sock_nested(newsk
, SINGLE_DEPTH_NESTING
);
6452 sctp_assoc_migrate(assoc
, newsk
);
6454 /* If the association on the newsk is already closed before accept()
6455 * is called, set RCV_SHUTDOWN flag.
6457 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
6458 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
6460 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
6461 sctp_release_sock(newsk
);
6465 /* This proto struct describes the ULP interface for SCTP. */
6466 struct proto sctp_prot
= {
6468 .owner
= THIS_MODULE
,
6469 .close
= sctp_close
,
6470 .connect
= sctp_connect
,
6471 .disconnect
= sctp_disconnect
,
6472 .accept
= sctp_accept
,
6473 .ioctl
= sctp_ioctl
,
6474 .init
= sctp_init_sock
,
6475 .destroy
= sctp_destroy_sock
,
6476 .shutdown
= sctp_shutdown
,
6477 .setsockopt
= sctp_setsockopt
,
6478 .getsockopt
= sctp_getsockopt
,
6479 .sendmsg
= sctp_sendmsg
,
6480 .recvmsg
= sctp_recvmsg
,
6482 .backlog_rcv
= sctp_backlog_rcv
,
6484 .unhash
= sctp_unhash
,
6485 .get_port
= sctp_get_port
,
6486 .obj_size
= sizeof(struct sctp_sock
),
6487 .sysctl_mem
= sysctl_sctp_mem
,
6488 .sysctl_rmem
= sysctl_sctp_rmem
,
6489 .sysctl_wmem
= sysctl_sctp_wmem
,
6490 .memory_pressure
= &sctp_memory_pressure
,
6491 .enter_memory_pressure
= sctp_enter_memory_pressure
,
6492 .memory_allocated
= &sctp_memory_allocated
,
6493 .sockets_allocated
= &sctp_sockets_allocated
,
6496 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6498 struct proto sctpv6_prot
= {
6500 .owner
= THIS_MODULE
,
6501 .close
= sctp_close
,
6502 .connect
= sctp_connect
,
6503 .disconnect
= sctp_disconnect
,
6504 .accept
= sctp_accept
,
6505 .ioctl
= sctp_ioctl
,
6506 .init
= sctp_init_sock
,
6507 .destroy
= sctp_destroy_sock
,
6508 .shutdown
= sctp_shutdown
,
6509 .setsockopt
= sctp_setsockopt
,
6510 .getsockopt
= sctp_getsockopt
,
6511 .sendmsg
= sctp_sendmsg
,
6512 .recvmsg
= sctp_recvmsg
,
6514 .backlog_rcv
= sctp_backlog_rcv
,
6516 .unhash
= sctp_unhash
,
6517 .get_port
= sctp_get_port
,
6518 .obj_size
= sizeof(struct sctp6_sock
),
6519 .sysctl_mem
= sysctl_sctp_mem
,
6520 .sysctl_rmem
= sysctl_sctp_rmem
,
6521 .sysctl_wmem
= sysctl_sctp_wmem
,
6522 .memory_pressure
= &sctp_memory_pressure
,
6523 .enter_memory_pressure
= sctp_enter_memory_pressure
,
6524 .memory_allocated
= &sctp_memory_allocated
,
6525 .sockets_allocated
= &sctp_sockets_allocated
,
6527 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */