1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
66 #include <linux/capability.h>
67 #include <linux/fcntl.h>
68 #include <linux/poll.h>
69 #include <linux/init.h>
70 #include <linux/crypto.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 kmem_cache_t
*sctp_bucket_cachep
;
113 /* Get the sndbuf space available at the time on the association. */
114 static inline int sctp_wspace(struct sctp_association
*asoc
)
116 struct sock
*sk
= asoc
->base
.sk
;
119 if (asoc
->ep
->sndbuf_policy
) {
120 /* make sure that no association uses more than sk_sndbuf */
121 amt
= sk
->sk_sndbuf
- asoc
->sndbuf_used
;
123 /* do socket level accounting */
124 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
133 /* Increment the used sndbuf space count of the corresponding association by
134 * the size of the outgoing data chunk.
135 * Also, set the skb destructor for sndbuf accounting later.
137 * Since it is always 1-1 between chunk and skb, and also a new skb is always
138 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
139 * destructor in the data chunk skb for the purpose of the sndbuf space
142 static inline void sctp_set_owner_w(struct sctp_chunk
*chunk
)
144 struct sctp_association
*asoc
= chunk
->asoc
;
145 struct sock
*sk
= asoc
->base
.sk
;
147 /* The sndbuf space is tracked per association. */
148 sctp_association_hold(asoc
);
150 skb_set_owner_w(chunk
->skb
, sk
);
152 chunk
->skb
->destructor
= sctp_wfree
;
153 /* Save the chunk pointer in skb for sctp_wfree to use later. */
154 *((struct sctp_chunk
**)(chunk
->skb
->cb
)) = chunk
;
156 asoc
->sndbuf_used
+= SCTP_DATA_SNDSIZE(chunk
) +
157 sizeof(struct sk_buff
) +
158 sizeof(struct sctp_chunk
);
160 atomic_add(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
163 /* Verify that this is a valid address. */
164 static inline int sctp_verify_addr(struct sock
*sk
, union sctp_addr
*addr
,
169 /* Verify basic sockaddr. */
170 af
= sctp_sockaddr_af(sctp_sk(sk
), addr
, len
);
174 /* Is this a valid SCTP address? */
175 if (!af
->addr_valid(addr
, sctp_sk(sk
), NULL
))
178 if (!sctp_sk(sk
)->pf
->send_verify(sctp_sk(sk
), (addr
)))
184 /* Look up the association by its id. If this is not a UDP-style
185 * socket, the ID field is always ignored.
187 struct sctp_association
*sctp_id2assoc(struct sock
*sk
, sctp_assoc_t id
)
189 struct sctp_association
*asoc
= NULL
;
191 /* If this is not a UDP-style socket, assoc id should be ignored. */
192 if (!sctp_style(sk
, UDP
)) {
193 /* Return NULL if the socket state is not ESTABLISHED. It
194 * could be a TCP-style listening socket or a socket which
195 * hasn't yet called connect() to establish an association.
197 if (!sctp_sstate(sk
, ESTABLISHED
))
200 /* Get the first and the only association from the list. */
201 if (!list_empty(&sctp_sk(sk
)->ep
->asocs
))
202 asoc
= list_entry(sctp_sk(sk
)->ep
->asocs
.next
,
203 struct sctp_association
, asocs
);
207 /* Otherwise this is a UDP-style socket. */
208 if (!id
|| (id
== (sctp_assoc_t
)-1))
211 spin_lock_bh(&sctp_assocs_id_lock
);
212 asoc
= (struct sctp_association
*)idr_find(&sctp_assocs_id
, (int)id
);
213 spin_unlock_bh(&sctp_assocs_id_lock
);
215 if (!asoc
|| (asoc
->base
.sk
!= sk
) || asoc
->base
.dead
)
221 /* Look up the transport from an address and an assoc id. If both address and
222 * id are specified, the associations matching the address and the id should be
225 static struct sctp_transport
*sctp_addr_id2transport(struct sock
*sk
,
226 struct sockaddr_storage
*addr
,
229 struct sctp_association
*addr_asoc
= NULL
, *id_asoc
= NULL
;
230 struct sctp_transport
*transport
;
231 union sctp_addr
*laddr
= (union sctp_addr
*)addr
;
233 laddr
->v4
.sin_port
= ntohs(laddr
->v4
.sin_port
);
234 addr_asoc
= sctp_endpoint_lookup_assoc(sctp_sk(sk
)->ep
,
235 (union sctp_addr
*)addr
,
237 laddr
->v4
.sin_port
= htons(laddr
->v4
.sin_port
);
242 id_asoc
= sctp_id2assoc(sk
, id
);
243 if (id_asoc
&& (id_asoc
!= addr_asoc
))
246 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
247 (union sctp_addr
*)addr
);
252 /* API 3.1.2 bind() - UDP Style Syntax
253 * The syntax of bind() is,
255 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
257 * sd - the socket descriptor returned by socket().
258 * addr - the address structure (struct sockaddr_in or struct
259 * sockaddr_in6 [RFC 2553]),
260 * addr_len - the size of the address structure.
262 SCTP_STATIC
int sctp_bind(struct sock
*sk
, struct sockaddr
*addr
, int addr_len
)
268 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
271 /* Disallow binding twice. */
272 if (!sctp_sk(sk
)->ep
->base
.bind_addr
.port
)
273 retval
= sctp_do_bind(sk
, (union sctp_addr
*)addr
,
278 sctp_release_sock(sk
);
283 static long sctp_get_port_local(struct sock
*, union sctp_addr
*);
285 /* Verify this is a valid sockaddr. */
286 static struct sctp_af
*sctp_sockaddr_af(struct sctp_sock
*opt
,
287 union sctp_addr
*addr
, int len
)
291 /* Check minimum size. */
292 if (len
< sizeof (struct sockaddr
))
295 /* Does this PF support this AF? */
296 if (!opt
->pf
->af_supported(addr
->sa
.sa_family
, opt
))
299 /* If we get this far, af is valid. */
300 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
302 if (len
< af
->sockaddr_len
)
308 /* Bind a local address either to an endpoint or to an association. */
309 SCTP_STATIC
int sctp_do_bind(struct sock
*sk
, union sctp_addr
*addr
, int len
)
311 struct sctp_sock
*sp
= sctp_sk(sk
);
312 struct sctp_endpoint
*ep
= sp
->ep
;
313 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
318 /* Common sockaddr verification. */
319 af
= sctp_sockaddr_af(sp
, addr
, len
);
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
326 snum
= ntohs(addr
->v4
.sin_port
);
328 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
329 ", port: %d, new port: %d, len: %d)\n",
335 /* PF specific bind() address verification. */
336 if (!sp
->pf
->bind_verify(sp
, addr
))
337 return -EADDRNOTAVAIL
;
339 /* We must either be unbound, or bind to the same port. */
340 if (bp
->port
&& (snum
!= bp
->port
)) {
341 SCTP_DEBUG_PRINTK("sctp_do_bind:"
342 " New port %d does not match existing port "
343 "%d.\n", snum
, bp
->port
);
347 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
350 /* Make sure we are allowed to bind here.
351 * The function sctp_get_port_local() does duplicate address
354 if ((ret
= sctp_get_port_local(sk
, addr
))) {
355 if (ret
== (long) sk
) {
356 /* This endpoint has a conflicting address. */
363 /* Refresh ephemeral port. */
365 bp
->port
= inet_sk(sk
)->num
;
367 /* Add the address to the bind address list. */
368 sctp_local_bh_disable();
369 sctp_write_lock(&ep
->base
.addr_lock
);
371 /* Use GFP_ATOMIC since BHs are disabled. */
372 addr
->v4
.sin_port
= ntohs(addr
->v4
.sin_port
);
373 ret
= sctp_add_bind_addr(bp
, addr
, GFP_ATOMIC
);
374 addr
->v4
.sin_port
= htons(addr
->v4
.sin_port
);
375 sctp_write_unlock(&ep
->base
.addr_lock
);
376 sctp_local_bh_enable();
378 /* Copy back into socket for getsockname() use. */
380 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
381 af
->to_sk_saddr(addr
, sk
);
387 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
389 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
390 * at any one time. If a sender, after sending an ASCONF chunk, decides
391 * it needs to transfer another ASCONF Chunk, it MUST wait until the
392 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
393 * subsequent ASCONF. Note this restriction binds each side, so at any
394 * time two ASCONF may be in-transit on any given association (one sent
395 * from each endpoint).
397 static int sctp_send_asconf(struct sctp_association
*asoc
,
398 struct sctp_chunk
*chunk
)
402 /* If there is an outstanding ASCONF chunk, queue it for later
405 if (asoc
->addip_last_asconf
) {
406 list_add_tail(&chunk
->list
, &asoc
->addip_chunk_list
);
410 /* Hold the chunk until an ASCONF_ACK is received. */
411 sctp_chunk_hold(chunk
);
412 retval
= sctp_primitive_ASCONF(asoc
, chunk
);
414 sctp_chunk_free(chunk
);
416 asoc
->addip_last_asconf
= chunk
;
422 /* Add a list of addresses as bind addresses to local endpoint or
425 * Basically run through each address specified in the addrs/addrcnt
426 * array/length pair, determine if it is IPv6 or IPv4 and call
427 * sctp_do_bind() on it.
429 * If any of them fails, then the operation will be reversed and the
430 * ones that were added will be removed.
432 * Only sctp_setsockopt_bindx() is supposed to call this function.
434 int sctp_bindx_add(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
439 struct sockaddr
*sa_addr
;
442 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
446 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
447 /* The list may contain either IPv4 or IPv6 address;
448 * determine the address length for walking thru the list.
450 sa_addr
= (struct sockaddr
*)addr_buf
;
451 af
= sctp_get_af_specific(sa_addr
->sa_family
);
457 retval
= sctp_do_bind(sk
, (union sctp_addr
*)sa_addr
,
460 addr_buf
+= af
->sockaddr_len
;
464 /* Failed. Cleanup the ones that have been added */
466 sctp_bindx_rem(sk
, addrs
, cnt
);
474 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
475 * associations that are part of the endpoint indicating that a list of local
476 * addresses are added to the endpoint.
478 * If any of the addresses is already in the bind address list of the
479 * association, we do not send the chunk for that association. But it will not
480 * affect other associations.
482 * Only sctp_setsockopt_bindx() is supposed to call this function.
484 static int sctp_send_asconf_add_ip(struct sock
*sk
,
485 struct sockaddr
*addrs
,
488 struct sctp_sock
*sp
;
489 struct sctp_endpoint
*ep
;
490 struct sctp_association
*asoc
;
491 struct sctp_bind_addr
*bp
;
492 struct sctp_chunk
*chunk
;
493 struct sctp_sockaddr_entry
*laddr
;
494 union sctp_addr
*addr
;
497 struct list_head
*pos
;
502 if (!sctp_addip_enable
)
508 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
509 __FUNCTION__
, sk
, addrs
, addrcnt
);
511 list_for_each(pos
, &ep
->asocs
) {
512 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
514 if (!asoc
->peer
.asconf_capable
)
517 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_ADD_IP
)
520 if (!sctp_state(asoc
, ESTABLISHED
))
523 /* Check if any address in the packed array of addresses is
524 * in the bind address list of the association. If so,
525 * do not send the asconf chunk to its peer, but continue with
526 * other associations.
529 for (i
= 0; i
< addrcnt
; i
++) {
530 addr
= (union sctp_addr
*)addr_buf
;
531 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
537 if (sctp_assoc_lookup_laddr(asoc
, addr
))
540 addr_buf
+= af
->sockaddr_len
;
545 /* Use the first address in bind addr list of association as
546 * Address Parameter of ASCONF CHUNK.
548 sctp_read_lock(&asoc
->base
.addr_lock
);
549 bp
= &asoc
->base
.bind_addr
;
550 p
= bp
->address_list
.next
;
551 laddr
= list_entry(p
, struct sctp_sockaddr_entry
, list
);
552 sctp_read_unlock(&asoc
->base
.addr_lock
);
554 chunk
= sctp_make_asconf_update_ip(asoc
, &laddr
->a
, addrs
,
555 addrcnt
, SCTP_PARAM_ADD_IP
);
561 retval
= sctp_send_asconf(asoc
, chunk
);
563 /* FIXME: After sending the add address ASCONF chunk, we
564 * cannot append the address to the association's binding
565 * address list, because the new address may be used as the
566 * source of a message sent to the peer before the ASCONF
567 * chunk is received by the peer. So we should wait until
568 * ASCONF_ACK is received.
576 /* Remove a list of addresses from bind addresses list. Do not remove the
579 * Basically run through each address specified in the addrs/addrcnt
580 * array/length pair, determine if it is IPv6 or IPv4 and call
581 * sctp_del_bind() on it.
583 * If any of them fails, then the operation will be reversed and the
584 * ones that were removed will be added back.
586 * At least one address has to be left; if only one address is
587 * available, the operation will return -EBUSY.
589 * Only sctp_setsockopt_bindx() is supposed to call this function.
591 int sctp_bindx_rem(struct sock
*sk
, struct sockaddr
*addrs
, int addrcnt
)
593 struct sctp_sock
*sp
= sctp_sk(sk
);
594 struct sctp_endpoint
*ep
= sp
->ep
;
596 struct sctp_bind_addr
*bp
= &ep
->base
.bind_addr
;
598 union sctp_addr saveaddr
;
600 struct sockaddr
*sa_addr
;
603 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
607 for (cnt
= 0; cnt
< addrcnt
; cnt
++) {
608 /* If the bind address list is empty or if there is only one
609 * bind address, there is nothing more to be removed (we need
610 * at least one address here).
612 if (list_empty(&bp
->address_list
) ||
613 (sctp_list_single_entry(&bp
->address_list
))) {
618 /* The list may contain either IPv4 or IPv6 address;
619 * determine the address length to copy the address to
622 sa_addr
= (struct sockaddr
*)addr_buf
;
623 af
= sctp_get_af_specific(sa_addr
->sa_family
);
628 memcpy(&saveaddr
, sa_addr
, af
->sockaddr_len
);
629 saveaddr
.v4
.sin_port
= ntohs(saveaddr
.v4
.sin_port
);
630 if (saveaddr
.v4
.sin_port
!= bp
->port
) {
635 /* FIXME - There is probably a need to check if sk->sk_saddr and
636 * sk->sk_rcv_addr are currently set to one of the addresses to
637 * be removed. This is something which needs to be looked into
638 * when we are fixing the outstanding issues with multi-homing
639 * socket routing and failover schemes. Refer to comments in
640 * sctp_do_bind(). -daisy
642 sctp_local_bh_disable();
643 sctp_write_lock(&ep
->base
.addr_lock
);
645 retval
= sctp_del_bind_addr(bp
, &saveaddr
);
647 sctp_write_unlock(&ep
->base
.addr_lock
);
648 sctp_local_bh_enable();
650 addr_buf
+= af
->sockaddr_len
;
653 /* Failed. Add the ones that has been removed back */
655 sctp_bindx_add(sk
, addrs
, cnt
);
663 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
664 * the associations that are part of the endpoint indicating that a list of
665 * local addresses are removed from the endpoint.
667 * If any of the addresses is already in the bind address list of the
668 * association, we do not send the chunk for that association. But it will not
669 * affect other associations.
671 * Only sctp_setsockopt_bindx() is supposed to call this function.
673 static int sctp_send_asconf_del_ip(struct sock
*sk
,
674 struct sockaddr
*addrs
,
677 struct sctp_sock
*sp
;
678 struct sctp_endpoint
*ep
;
679 struct sctp_association
*asoc
;
680 struct sctp_bind_addr
*bp
;
681 struct sctp_chunk
*chunk
;
682 union sctp_addr
*laddr
;
685 struct list_head
*pos
;
689 if (!sctp_addip_enable
)
695 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
696 __FUNCTION__
, sk
, addrs
, addrcnt
);
698 list_for_each(pos
, &ep
->asocs
) {
699 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
701 if (!asoc
->peer
.asconf_capable
)
704 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_DEL_IP
)
707 if (!sctp_state(asoc
, ESTABLISHED
))
710 /* Check if any address in the packed array of addresses is
711 * not present in the bind address list of the association.
712 * If so, do not send the asconf chunk to its peer, but
713 * continue with other associations.
716 for (i
= 0; i
< addrcnt
; i
++) {
717 laddr
= (union sctp_addr
*)addr_buf
;
718 af
= sctp_get_af_specific(laddr
->v4
.sin_family
);
724 if (!sctp_assoc_lookup_laddr(asoc
, laddr
))
727 addr_buf
+= af
->sockaddr_len
;
732 /* Find one address in the association's bind address list
733 * that is not in the packed array of addresses. This is to
734 * make sure that we do not delete all the addresses in the
737 sctp_read_lock(&asoc
->base
.addr_lock
);
738 bp
= &asoc
->base
.bind_addr
;
739 laddr
= sctp_find_unmatch_addr(bp
, (union sctp_addr
*)addrs
,
741 sctp_read_unlock(&asoc
->base
.addr_lock
);
745 chunk
= sctp_make_asconf_update_ip(asoc
, laddr
, addrs
, addrcnt
,
752 retval
= sctp_send_asconf(asoc
, chunk
);
754 /* FIXME: After sending the delete address ASCONF chunk, we
755 * cannot remove the addresses from the association's bind
756 * address list, because there maybe some packet send to
757 * the delete addresses, so we should wait until ASCONF_ACK
758 * packet is received.
765 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
768 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
771 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
772 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
775 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
776 * Section 3.1.2 for this usage.
778 * addrs is a pointer to an array of one or more socket addresses. Each
779 * address is contained in its appropriate structure (i.e. struct
780 * sockaddr_in or struct sockaddr_in6) the family of the address type
781 * must be used to distengish the address length (note that this
782 * representation is termed a "packed array" of addresses). The caller
783 * specifies the number of addresses in the array with addrcnt.
785 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
786 * -1, and sets errno to the appropriate error code.
788 * For SCTP, the port given in each socket address must be the same, or
789 * sctp_bindx() will fail, setting errno to EINVAL.
791 * The flags parameter is formed from the bitwise OR of zero or more of
792 * the following currently defined flags:
794 * SCTP_BINDX_ADD_ADDR
796 * SCTP_BINDX_REM_ADDR
798 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
799 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
800 * addresses from the association. The two flags are mutually exclusive;
801 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
802 * not remove all addresses from an association; sctp_bindx() will
803 * reject such an attempt with EINVAL.
805 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
806 * additional addresses with an endpoint after calling bind(). Or use
807 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
808 * socket is associated with so that no new association accepted will be
809 * associated with those addresses. If the endpoint supports dynamic
810 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
811 * endpoint to send the appropriate message to the peer to change the
812 * peers address lists.
814 * Adding and removing addresses from a connected association is
815 * optional functionality. Implementations that do not support this
816 * functionality should return EOPNOTSUPP.
818 * Basically do nothing but copying the addresses from user to kernel
819 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
820 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
823 * We don't use copy_from_user() for optimization: we first do the
824 * sanity checks (buffer size -fast- and access check-healthy
825 * pointer); if all of those succeed, then we can alloc the memory
826 * (expensive operation) needed to copy the data to kernel. Then we do
827 * the copying without checking the user space area
828 * (__copy_from_user()).
830 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
833 * sk The sk of the socket
834 * addrs The pointer to the addresses in user land
835 * addrssize Size of the addrs buffer
836 * op Operation to perform (add or remove, see the flags of
839 * Returns 0 if ok, <0 errno code on error.
841 SCTP_STATIC
int sctp_setsockopt_bindx(struct sock
* sk
,
842 struct sockaddr __user
*addrs
,
843 int addrs_size
, int op
)
845 struct sockaddr
*kaddrs
;
849 struct sockaddr
*sa_addr
;
853 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
854 " addrs_size %d opt %d\n", sk
, addrs
, addrs_size
, op
);
856 if (unlikely(addrs_size
<= 0))
859 /* Check the user passed a healthy pointer. */
860 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
863 /* Alloc space for the address array in kernel memory. */
864 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
865 if (unlikely(!kaddrs
))
868 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
873 /* Walk through the addrs buffer and count the number of addresses. */
875 while (walk_size
< addrs_size
) {
876 sa_addr
= (struct sockaddr
*)addr_buf
;
877 af
= sctp_get_af_specific(sa_addr
->sa_family
);
879 /* If the address family is not supported or if this address
880 * causes the address buffer to overflow return EINVAL.
882 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
887 addr_buf
+= af
->sockaddr_len
;
888 walk_size
+= af
->sockaddr_len
;
893 case SCTP_BINDX_ADD_ADDR
:
894 err
= sctp_bindx_add(sk
, kaddrs
, addrcnt
);
897 err
= sctp_send_asconf_add_ip(sk
, kaddrs
, addrcnt
);
900 case SCTP_BINDX_REM_ADDR
:
901 err
= sctp_bindx_rem(sk
, kaddrs
, addrcnt
);
904 err
= sctp_send_asconf_del_ip(sk
, kaddrs
, addrcnt
);
918 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
920 * Common routine for handling connect() and sctp_connectx().
921 * Connect will come in with just a single address.
923 static int __sctp_connect(struct sock
* sk
,
924 struct sockaddr
*kaddrs
,
927 struct sctp_sock
*sp
;
928 struct sctp_endpoint
*ep
;
929 struct sctp_association
*asoc
= NULL
;
930 struct sctp_association
*asoc2
;
931 struct sctp_transport
*transport
;
939 struct sockaddr
*sa_addr
;
945 /* connect() cannot be done on a socket that is already in ESTABLISHED
946 * state - UDP-style peeled off socket or a TCP-style socket that
947 * is already connected.
948 * It cannot be done even on a TCP-style listening socket.
950 if (sctp_sstate(sk
, ESTABLISHED
) ||
951 (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))) {
956 /* Walk through the addrs buffer and count the number of addresses. */
958 while (walk_size
< addrs_size
) {
959 sa_addr
= (struct sockaddr
*)addr_buf
;
960 af
= sctp_get_af_specific(sa_addr
->sa_family
);
962 /* If the address family is not supported or if this address
963 * causes the address buffer to overflow return EINVAL.
965 if (!af
|| (walk_size
+ af
->sockaddr_len
) > addrs_size
) {
970 err
= sctp_verify_addr(sk
, (union sctp_addr
*)sa_addr
,
975 memcpy(&to
, sa_addr
, af
->sockaddr_len
);
976 to
.v4
.sin_port
= ntohs(to
.v4
.sin_port
);
978 /* Check if there already is a matching association on the
979 * endpoint (other than the one created here).
981 asoc2
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
982 if (asoc2
&& asoc2
!= asoc
) {
983 if (asoc2
->state
>= SCTP_STATE_ESTABLISHED
)
990 /* If we could not find a matching association on the endpoint,
991 * make sure that there is no peeled-off association matching
992 * the peer address even on another socket.
994 if (sctp_endpoint_is_peeled_off(ep
, &to
)) {
995 err
= -EADDRNOTAVAIL
;
1000 /* If a bind() or sctp_bindx() is not called prior to
1001 * an sctp_connectx() call, the system picks an
1002 * ephemeral port and will choose an address set
1003 * equivalent to binding with a wildcard address.
1005 if (!ep
->base
.bind_addr
.port
) {
1006 if (sctp_autobind(sk
)) {
1012 * If an unprivileged user inherits a 1-many
1013 * style socket with open associations on a
1014 * privileged port, it MAY be permitted to
1015 * accept new associations, but it SHOULD NOT
1016 * be permitted to open new associations.
1018 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1019 !capable(CAP_NET_BIND_SERVICE
)) {
1025 scope
= sctp_scope(&to
);
1026 asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1033 /* Prime the peer's transport structures. */
1034 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
,
1042 addr_buf
+= af
->sockaddr_len
;
1043 walk_size
+= af
->sockaddr_len
;
1046 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1051 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1056 /* Initialize sk's dport and daddr for getpeername() */
1057 inet_sk(sk
)->dport
= htons(asoc
->peer
.port
);
1058 af
= sctp_get_af_specific(to
.sa
.sa_family
);
1059 af
->to_sk_daddr(&to
, sk
);
1062 timeo
= sock_sndtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
1063 err
= sctp_wait_for_connect(asoc
, &timeo
);
1065 /* Don't free association on exit. */
1070 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1071 " kaddrs: %p err: %d\n",
1074 sctp_association_free(asoc
);
1078 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1081 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1083 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1084 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1085 * or IPv6 addresses.
1087 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1088 * Section 3.1.2 for this usage.
1090 * addrs is a pointer to an array of one or more socket addresses. Each
1091 * address is contained in its appropriate structure (i.e. struct
1092 * sockaddr_in or struct sockaddr_in6) the family of the address type
1093 * must be used to distengish the address length (note that this
1094 * representation is termed a "packed array" of addresses). The caller
1095 * specifies the number of addresses in the array with addrcnt.
1097 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1098 * -1, and sets errno to the appropriate error code.
1100 * For SCTP, the port given in each socket address must be the same, or
1101 * sctp_connectx() will fail, setting errno to EINVAL.
1103 * An application can use sctp_connectx to initiate an association with
1104 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1105 * allows a caller to specify multiple addresses at which a peer can be
1106 * reached. The way the SCTP stack uses the list of addresses to set up
1107 * the association is implementation dependant. This function only
1108 * specifies that the stack will try to make use of all the addresses in
1109 * the list when needed.
1111 * Note that the list of addresses passed in is only used for setting up
1112 * the association. It does not necessarily equal the set of addresses
1113 * the peer uses for the resulting association. If the caller wants to
1114 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1115 * retrieve them after the association has been set up.
1117 * Basically do nothing but copying the addresses from user to kernel
1118 * land and invoking either sctp_connectx(). This is used for tunneling
1119 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1121 * We don't use copy_from_user() for optimization: we first do the
1122 * sanity checks (buffer size -fast- and access check-healthy
1123 * pointer); if all of those succeed, then we can alloc the memory
1124 * (expensive operation) needed to copy the data to kernel. Then we do
1125 * the copying without checking the user space area
1126 * (__copy_from_user()).
1128 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1131 * sk The sk of the socket
1132 * addrs The pointer to the addresses in user land
1133 * addrssize Size of the addrs buffer
1135 * Returns 0 if ok, <0 errno code on error.
1137 SCTP_STATIC
int sctp_setsockopt_connectx(struct sock
* sk
,
1138 struct sockaddr __user
*addrs
,
1142 struct sockaddr
*kaddrs
;
1144 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1145 __FUNCTION__
, sk
, addrs
, addrs_size
);
1147 if (unlikely(addrs_size
<= 0))
1150 /* Check the user passed a healthy pointer. */
1151 if (unlikely(!access_ok(VERIFY_READ
, addrs
, addrs_size
)))
1154 /* Alloc space for the address array in kernel memory. */
1155 kaddrs
= kmalloc(addrs_size
, GFP_KERNEL
);
1156 if (unlikely(!kaddrs
))
1159 if (__copy_from_user(kaddrs
, addrs
, addrs_size
)) {
1162 err
= __sctp_connect(sk
, kaddrs
, addrs_size
);
1169 /* API 3.1.4 close() - UDP Style Syntax
1170 * Applications use close() to perform graceful shutdown (as described in
1171 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1172 * by a UDP-style socket.
1176 * ret = close(int sd);
1178 * sd - the socket descriptor of the associations to be closed.
1180 * To gracefully shutdown a specific association represented by the
1181 * UDP-style socket, an application should use the sendmsg() call,
1182 * passing no user data, but including the appropriate flag in the
1183 * ancillary data (see Section xxxx).
1185 * If sd in the close() call is a branched-off socket representing only
1186 * one association, the shutdown is performed on that association only.
1188 * 4.1.6 close() - TCP Style Syntax
1190 * Applications use close() to gracefully close down an association.
1194 * int close(int sd);
1196 * sd - the socket descriptor of the association to be closed.
1198 * After an application calls close() on a socket descriptor, no further
1199 * socket operations will succeed on that descriptor.
1201 * API 7.1.4 SO_LINGER
1203 * An application using the TCP-style socket can use this option to
1204 * perform the SCTP ABORT primitive. The linger option structure is:
1207 * int l_onoff; // option on/off
1208 * int l_linger; // linger time
1211 * To enable the option, set l_onoff to 1. If the l_linger value is set
1212 * to 0, calling close() is the same as the ABORT primitive. If the
1213 * value is set to a negative value, the setsockopt() call will return
1214 * an error. If the value is set to a positive value linger_time, the
1215 * close() can be blocked for at most linger_time ms. If the graceful
1216 * shutdown phase does not finish during this period, close() will
1217 * return but the graceful shutdown phase continues in the system.
1219 SCTP_STATIC
void sctp_close(struct sock
*sk
, long timeout
)
1221 struct sctp_endpoint
*ep
;
1222 struct sctp_association
*asoc
;
1223 struct list_head
*pos
, *temp
;
1225 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk
, timeout
);
1228 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1230 ep
= sctp_sk(sk
)->ep
;
1232 /* Walk all associations on an endpoint. */
1233 list_for_each_safe(pos
, temp
, &ep
->asocs
) {
1234 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
1236 if (sctp_style(sk
, TCP
)) {
1237 /* A closed association can still be in the list if
1238 * it belongs to a TCP-style listening socket that is
1239 * not yet accepted. If so, free it. If not, send an
1240 * ABORT or SHUTDOWN based on the linger options.
1242 if (sctp_state(asoc
, CLOSED
)) {
1243 sctp_unhash_established(asoc
);
1244 sctp_association_free(asoc
);
1249 if (sock_flag(sk
, SOCK_LINGER
) && !sk
->sk_lingertime
)
1250 sctp_primitive_ABORT(asoc
, NULL
);
1252 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1255 /* Clean up any skbs sitting on the receive queue. */
1256 sctp_queue_purge_ulpevents(&sk
->sk_receive_queue
);
1257 sctp_queue_purge_ulpevents(&sctp_sk(sk
)->pd_lobby
);
1259 /* On a TCP-style socket, block for at most linger_time if set. */
1260 if (sctp_style(sk
, TCP
) && timeout
)
1261 sctp_wait_for_close(sk
, timeout
);
1263 /* This will run the backlog queue. */
1264 sctp_release_sock(sk
);
1266 /* Supposedly, no process has access to the socket, but
1267 * the net layers still may.
1269 sctp_local_bh_disable();
1270 sctp_bh_lock_sock(sk
);
1272 /* Hold the sock, since sk_common_release() will put sock_put()
1273 * and we have just a little more cleanup.
1276 sk_common_release(sk
);
1278 sctp_bh_unlock_sock(sk
);
1279 sctp_local_bh_enable();
1283 SCTP_DBG_OBJCNT_DEC(sock
);
1286 /* Handle EPIPE error. */
1287 static int sctp_error(struct sock
*sk
, int flags
, int err
)
1290 err
= sock_error(sk
) ? : -EPIPE
;
1291 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
1292 send_sig(SIGPIPE
, current
, 0);
1296 /* API 3.1.3 sendmsg() - UDP Style Syntax
1298 * An application uses sendmsg() and recvmsg() calls to transmit data to
1299 * and receive data from its peer.
1301 * ssize_t sendmsg(int socket, const struct msghdr *message,
1304 * socket - the socket descriptor of the endpoint.
1305 * message - pointer to the msghdr structure which contains a single
1306 * user message and possibly some ancillary data.
1308 * See Section 5 for complete description of the data
1311 * flags - flags sent or received with the user message, see Section
1312 * 5 for complete description of the flags.
1314 * Note: This function could use a rewrite especially when explicit
1315 * connect support comes in.
1317 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1319 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*, sctp_cmsgs_t
*);
1321 SCTP_STATIC
int sctp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1322 struct msghdr
*msg
, size_t msg_len
)
1324 struct sctp_sock
*sp
;
1325 struct sctp_endpoint
*ep
;
1326 struct sctp_association
*new_asoc
=NULL
, *asoc
=NULL
;
1327 struct sctp_transport
*transport
, *chunk_tp
;
1328 struct sctp_chunk
*chunk
;
1330 struct sockaddr
*msg_name
= NULL
;
1331 struct sctp_sndrcvinfo default_sinfo
= { 0 };
1332 struct sctp_sndrcvinfo
*sinfo
;
1333 struct sctp_initmsg
*sinit
;
1334 sctp_assoc_t associd
= 0;
1335 sctp_cmsgs_t cmsgs
= { NULL
};
1339 __u16 sinfo_flags
= 0;
1340 struct sctp_datamsg
*datamsg
;
1341 struct list_head
*pos
;
1342 int msg_flags
= msg
->msg_flags
;
1344 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1351 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep
);
1353 /* We cannot send a message over a TCP-style listening socket. */
1354 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
)) {
1359 /* Parse out the SCTP CMSGs. */
1360 err
= sctp_msghdr_parse(msg
, &cmsgs
);
1363 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err
);
1367 /* Fetch the destination address for this packet. This
1368 * address only selects the association--it is not necessarily
1369 * the address we will send to.
1370 * For a peeled-off socket, msg_name is ignored.
1372 if (!sctp_style(sk
, UDP_HIGH_BANDWIDTH
) && msg
->msg_name
) {
1373 int msg_namelen
= msg
->msg_namelen
;
1375 err
= sctp_verify_addr(sk
, (union sctp_addr
*)msg
->msg_name
,
1380 if (msg_namelen
> sizeof(to
))
1381 msg_namelen
= sizeof(to
);
1382 memcpy(&to
, msg
->msg_name
, msg_namelen
);
1383 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1385 to
.v4
.sin_addr
.s_addr
, to
.v4
.sin_port
);
1387 to
.v4
.sin_port
= ntohs(to
.v4
.sin_port
);
1388 msg_name
= msg
->msg_name
;
1394 /* Did the user specify SNDRCVINFO? */
1396 sinfo_flags
= sinfo
->sinfo_flags
;
1397 associd
= sinfo
->sinfo_assoc_id
;
1400 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1401 msg_len
, sinfo_flags
);
1403 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1404 if (sctp_style(sk
, TCP
) && (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
))) {
1409 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1410 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1411 * If SCTP_ABORT is set, the message length could be non zero with
1412 * the msg_iov set to the user abort reason.
1414 if (((sinfo_flags
& SCTP_EOF
) && (msg_len
> 0)) ||
1415 (!(sinfo_flags
& (SCTP_EOF
|SCTP_ABORT
)) && (msg_len
== 0))) {
1420 /* If SCTP_ADDR_OVER is set, there must be an address
1421 * specified in msg_name.
1423 if ((sinfo_flags
& SCTP_ADDR_OVER
) && (!msg
->msg_name
)) {
1430 SCTP_DEBUG_PRINTK("About to look up association.\n");
1434 /* If a msg_name has been specified, assume this is to be used. */
1436 /* Look for a matching association on the endpoint. */
1437 asoc
= sctp_endpoint_lookup_assoc(ep
, &to
, &transport
);
1439 /* If we could not find a matching association on the
1440 * endpoint, make sure that it is not a TCP-style
1441 * socket that already has an association or there is
1442 * no peeled-off association on another socket.
1444 if ((sctp_style(sk
, TCP
) &&
1445 sctp_sstate(sk
, ESTABLISHED
)) ||
1446 sctp_endpoint_is_peeled_off(ep
, &to
)) {
1447 err
= -EADDRNOTAVAIL
;
1452 asoc
= sctp_id2assoc(sk
, associd
);
1460 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc
);
1462 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1463 * socket that has an association in CLOSED state. This can
1464 * happen when an accepted socket has an association that is
1467 if (sctp_state(asoc
, CLOSED
) && sctp_style(sk
, TCP
)) {
1472 if (sinfo_flags
& SCTP_EOF
) {
1473 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1475 sctp_primitive_SHUTDOWN(asoc
, NULL
);
1479 if (sinfo_flags
& SCTP_ABORT
) {
1480 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc
);
1481 sctp_primitive_ABORT(asoc
, msg
);
1487 /* Do we need to create the association? */
1489 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1491 if (sinfo_flags
& (SCTP_EOF
| SCTP_ABORT
)) {
1496 /* Check for invalid stream against the stream counts,
1497 * either the default or the user specified stream counts.
1500 if (!sinit
|| (sinit
&& !sinit
->sinit_num_ostreams
)) {
1501 /* Check against the defaults. */
1502 if (sinfo
->sinfo_stream
>=
1503 sp
->initmsg
.sinit_num_ostreams
) {
1508 /* Check against the requested. */
1509 if (sinfo
->sinfo_stream
>=
1510 sinit
->sinit_num_ostreams
) {
1518 * API 3.1.2 bind() - UDP Style Syntax
1519 * If a bind() or sctp_bindx() is not called prior to a
1520 * sendmsg() call that initiates a new association, the
1521 * system picks an ephemeral port and will choose an address
1522 * set equivalent to binding with a wildcard address.
1524 if (!ep
->base
.bind_addr
.port
) {
1525 if (sctp_autobind(sk
)) {
1531 * If an unprivileged user inherits a one-to-many
1532 * style socket with open associations on a privileged
1533 * port, it MAY be permitted to accept new associations,
1534 * but it SHOULD NOT be permitted to open new
1537 if (ep
->base
.bind_addr
.port
< PROT_SOCK
&&
1538 !capable(CAP_NET_BIND_SERVICE
)) {
1544 scope
= sctp_scope(&to
);
1545 new_asoc
= sctp_association_new(ep
, sk
, scope
, GFP_KERNEL
);
1552 /* If the SCTP_INIT ancillary data is specified, set all
1553 * the association init values accordingly.
1556 if (sinit
->sinit_num_ostreams
) {
1557 asoc
->c
.sinit_num_ostreams
=
1558 sinit
->sinit_num_ostreams
;
1560 if (sinit
->sinit_max_instreams
) {
1561 asoc
->c
.sinit_max_instreams
=
1562 sinit
->sinit_max_instreams
;
1564 if (sinit
->sinit_max_attempts
) {
1565 asoc
->max_init_attempts
1566 = sinit
->sinit_max_attempts
;
1568 if (sinit
->sinit_max_init_timeo
) {
1569 asoc
->max_init_timeo
=
1570 msecs_to_jiffies(sinit
->sinit_max_init_timeo
);
1574 /* Prime the peer's transport structures. */
1575 transport
= sctp_assoc_add_peer(asoc
, &to
, GFP_KERNEL
, SCTP_UNKNOWN
);
1580 err
= sctp_assoc_set_bind_addr_from_ep(asoc
, GFP_KERNEL
);
1587 /* ASSERT: we have a valid association at this point. */
1588 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1591 /* If the user didn't specify SNDRCVINFO, make up one with
1594 default_sinfo
.sinfo_stream
= asoc
->default_stream
;
1595 default_sinfo
.sinfo_flags
= asoc
->default_flags
;
1596 default_sinfo
.sinfo_ppid
= asoc
->default_ppid
;
1597 default_sinfo
.sinfo_context
= asoc
->default_context
;
1598 default_sinfo
.sinfo_timetolive
= asoc
->default_timetolive
;
1599 default_sinfo
.sinfo_assoc_id
= sctp_assoc2id(asoc
);
1600 sinfo
= &default_sinfo
;
1603 /* API 7.1.7, the sndbuf size per association bounds the
1604 * maximum size of data that can be sent in a single send call.
1606 if (msg_len
> sk
->sk_sndbuf
) {
1611 /* If fragmentation is disabled and the message length exceeds the
1612 * association fragmentation point, return EMSGSIZE. The I-D
1613 * does not specify what this error is, but this looks like
1616 if (sctp_sk(sk
)->disable_fragments
&& (msg_len
> asoc
->frag_point
)) {
1622 /* Check for invalid stream. */
1623 if (sinfo
->sinfo_stream
>= asoc
->c
.sinit_num_ostreams
) {
1629 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
1630 if (!sctp_wspace(asoc
)) {
1631 err
= sctp_wait_for_sndbuf(asoc
, &timeo
, msg_len
);
1636 /* If an address is passed with the sendto/sendmsg call, it is used
1637 * to override the primary destination address in the TCP model, or
1638 * when SCTP_ADDR_OVER flag is set in the UDP model.
1640 if ((sctp_style(sk
, TCP
) && msg_name
) ||
1641 (sinfo_flags
& SCTP_ADDR_OVER
)) {
1642 chunk_tp
= sctp_assoc_lookup_paddr(asoc
, &to
);
1650 /* Auto-connect, if we aren't connected already. */
1651 if (sctp_state(asoc
, CLOSED
)) {
1652 err
= sctp_primitive_ASSOCIATE(asoc
, NULL
);
1655 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1658 /* Break the message into multiple chunks of maximum size. */
1659 datamsg
= sctp_datamsg_from_user(asoc
, sinfo
, msg
, msg_len
);
1665 /* Now send the (possibly) fragmented message. */
1666 list_for_each(pos
, &datamsg
->chunks
) {
1667 chunk
= list_entry(pos
, struct sctp_chunk
, frag_list
);
1668 sctp_datamsg_track(chunk
);
1670 /* Do accounting for the write space. */
1671 sctp_set_owner_w(chunk
);
1673 chunk
->transport
= chunk_tp
;
1675 /* Send it to the lower layers. Note: all chunks
1676 * must either fail or succeed. The lower layer
1677 * works that way today. Keep it that way or this
1680 err
= sctp_primitive_SEND(asoc
, chunk
);
1681 /* Did the lower layer accept the chunk? */
1683 sctp_chunk_free(chunk
);
1684 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1687 sctp_datamsg_free(datamsg
);
1693 /* If we are already past ASSOCIATE, the lower
1694 * layers are responsible for association cleanup.
1700 sctp_association_free(asoc
);
1702 sctp_release_sock(sk
);
1705 return sctp_error(sk
, msg_flags
, err
);
1712 err
= sock_error(sk
);
1722 /* This is an extended version of skb_pull() that removes the data from the
1723 * start of a skb even when data is spread across the list of skb's in the
1724 * frag_list. len specifies the total amount of data that needs to be removed.
1725 * when 'len' bytes could be removed from the skb, it returns 0.
1726 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1727 * could not be removed.
1729 static int sctp_skb_pull(struct sk_buff
*skb
, int len
)
1731 struct sk_buff
*list
;
1732 int skb_len
= skb_headlen(skb
);
1735 if (len
<= skb_len
) {
1736 __skb_pull(skb
, len
);
1740 __skb_pull(skb
, skb_len
);
1742 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
) {
1743 rlen
= sctp_skb_pull(list
, len
);
1744 skb
->len
-= (len
-rlen
);
1745 skb
->data_len
-= (len
-rlen
);
1756 /* API 3.1.3 recvmsg() - UDP Style Syntax
1758 * ssize_t recvmsg(int socket, struct msghdr *message,
1761 * socket - the socket descriptor of the endpoint.
1762 * message - pointer to the msghdr structure which contains a single
1763 * user message and possibly some ancillary data.
1765 * See Section 5 for complete description of the data
1768 * flags - flags sent or received with the user message, see Section
1769 * 5 for complete description of the flags.
1771 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*, int, int, int *);
1773 SCTP_STATIC
int sctp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1774 struct msghdr
*msg
, size_t len
, int noblock
,
1775 int flags
, int *addr_len
)
1777 struct sctp_ulpevent
*event
= NULL
;
1778 struct sctp_sock
*sp
= sctp_sk(sk
);
1779 struct sk_buff
*skb
;
1784 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1785 "0x%x, %s: %p)\n", "sk", sk
, "msghdr", msg
,
1786 "len", len
, "knoblauch", noblock
,
1787 "flags", flags
, "addr_len", addr_len
);
1791 if (sctp_style(sk
, TCP
) && !sctp_sstate(sk
, ESTABLISHED
)) {
1796 skb
= sctp_skb_recv_datagram(sk
, flags
, noblock
, &err
);
1800 /* Get the total length of the skb including any skb's in the
1809 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1811 event
= sctp_skb2event(skb
);
1816 sock_recv_timestamp(msg
, sk
, skb
);
1817 if (sctp_ulpevent_is_notification(event
)) {
1818 msg
->msg_flags
|= MSG_NOTIFICATION
;
1819 sp
->pf
->event_msgname(event
, msg
->msg_name
, addr_len
);
1821 sp
->pf
->skb_msgname(skb
, msg
->msg_name
, addr_len
);
1824 /* Check if we allow SCTP_SNDRCVINFO. */
1825 if (sp
->subscribe
.sctp_data_io_event
)
1826 sctp_ulpevent_read_sndrcvinfo(event
, msg
);
1828 /* FIXME: we should be calling IP/IPv6 layers. */
1829 if (sk
->sk_protinfo
.af_inet
.cmsg_flags
)
1830 ip_cmsg_recv(msg
, skb
);
1835 /* If skb's length exceeds the user's buffer, update the skb and
1836 * push it back to the receive_queue so that the next call to
1837 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1839 if (skb_len
> copied
) {
1840 msg
->msg_flags
&= ~MSG_EOR
;
1841 if (flags
& MSG_PEEK
)
1843 sctp_skb_pull(skb
, copied
);
1844 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1846 /* When only partial message is copied to the user, increase
1847 * rwnd by that amount. If all the data in the skb is read,
1848 * rwnd is updated when the event is freed.
1850 sctp_assoc_rwnd_increase(event
->asoc
, copied
);
1852 } else if ((event
->msg_flags
& MSG_NOTIFICATION
) ||
1853 (event
->msg_flags
& MSG_EOR
))
1854 msg
->msg_flags
|= MSG_EOR
;
1856 msg
->msg_flags
&= ~MSG_EOR
;
1859 if (flags
& MSG_PEEK
) {
1860 /* Release the skb reference acquired after peeking the skb in
1861 * sctp_skb_recv_datagram().
1865 /* Free the event which includes releasing the reference to
1866 * the owner of the skb, freeing the skb and updating the
1869 sctp_ulpevent_free(event
);
1872 sctp_release_sock(sk
);
1876 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1878 * This option is a on/off flag. If enabled no SCTP message
1879 * fragmentation will be performed. Instead if a message being sent
1880 * exceeds the current PMTU size, the message will NOT be sent and
1881 * instead a error will be indicated to the user.
1883 static int sctp_setsockopt_disable_fragments(struct sock
*sk
,
1884 char __user
*optval
, int optlen
)
1888 if (optlen
< sizeof(int))
1891 if (get_user(val
, (int __user
*)optval
))
1894 sctp_sk(sk
)->disable_fragments
= (val
== 0) ? 0 : 1;
1899 static int sctp_setsockopt_events(struct sock
*sk
, char __user
*optval
,
1902 if (optlen
!= sizeof(struct sctp_event_subscribe
))
1904 if (copy_from_user(&sctp_sk(sk
)->subscribe
, optval
, optlen
))
1909 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1911 * This socket option is applicable to the UDP-style socket only. When
1912 * set it will cause associations that are idle for more than the
1913 * specified number of seconds to automatically close. An association
1914 * being idle is defined an association that has NOT sent or received
1915 * user data. The special value of '0' indicates that no automatic
1916 * close of any associations should be performed. The option expects an
1917 * integer defining the number of seconds of idle time before an
1918 * association is closed.
1920 static int sctp_setsockopt_autoclose(struct sock
*sk
, char __user
*optval
,
1923 struct sctp_sock
*sp
= sctp_sk(sk
);
1925 /* Applicable to UDP-style socket only */
1926 if (sctp_style(sk
, TCP
))
1928 if (optlen
!= sizeof(int))
1930 if (copy_from_user(&sp
->autoclose
, optval
, optlen
))
1936 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1938 * Applications can enable or disable heartbeats for any peer address of
1939 * an association, modify an address's heartbeat interval, force a
1940 * heartbeat to be sent immediately, and adjust the address's maximum
1941 * number of retransmissions sent before an address is considered
1942 * unreachable. The following structure is used to access and modify an
1943 * address's parameters:
1945 * struct sctp_paddrparams {
1946 * sctp_assoc_t spp_assoc_id;
1947 * struct sockaddr_storage spp_address;
1948 * uint32_t spp_hbinterval;
1949 * uint16_t spp_pathmaxrxt;
1950 * uint32_t spp_pathmtu;
1951 * uint32_t spp_sackdelay;
1952 * uint32_t spp_flags;
1955 * spp_assoc_id - (one-to-many style socket) This is filled in the
1956 * application, and identifies the association for
1958 * spp_address - This specifies which address is of interest.
1959 * spp_hbinterval - This contains the value of the heartbeat interval,
1960 * in milliseconds. If a value of zero
1961 * is present in this field then no changes are to
1962 * be made to this parameter.
1963 * spp_pathmaxrxt - This contains the maximum number of
1964 * retransmissions before this address shall be
1965 * considered unreachable. If a value of zero
1966 * is present in this field then no changes are to
1967 * be made to this parameter.
1968 * spp_pathmtu - When Path MTU discovery is disabled the value
1969 * specified here will be the "fixed" path mtu.
1970 * Note that if the spp_address field is empty
1971 * then all associations on this address will
1972 * have this fixed path mtu set upon them.
1974 * spp_sackdelay - When delayed sack is enabled, this value specifies
1975 * the number of milliseconds that sacks will be delayed
1976 * for. This value will apply to all addresses of an
1977 * association if the spp_address field is empty. Note
1978 * also, that if delayed sack is enabled and this
1979 * value is set to 0, no change is made to the last
1980 * recorded delayed sack timer value.
1982 * spp_flags - These flags are used to control various features
1983 * on an association. The flag field may contain
1984 * zero or more of the following options.
1986 * SPP_HB_ENABLE - Enable heartbeats on the
1987 * specified address. Note that if the address
1988 * field is empty all addresses for the association
1989 * have heartbeats enabled upon them.
1991 * SPP_HB_DISABLE - Disable heartbeats on the
1992 * speicifed address. Note that if the address
1993 * field is empty all addresses for the association
1994 * will have their heartbeats disabled. Note also
1995 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
1996 * mutually exclusive, only one of these two should
1997 * be specified. Enabling both fields will have
1998 * undetermined results.
2000 * SPP_HB_DEMAND - Request a user initiated heartbeat
2001 * to be made immediately.
2003 * SPP_PMTUD_ENABLE - This field will enable PMTU
2004 * discovery upon the specified address. Note that
2005 * if the address feild is empty then all addresses
2006 * on the association are effected.
2008 * SPP_PMTUD_DISABLE - This field will disable PMTU
2009 * discovery upon the specified address. Note that
2010 * if the address feild is empty then all addresses
2011 * on the association are effected. Not also that
2012 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2013 * exclusive. Enabling both will have undetermined
2016 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2017 * on delayed sack. The time specified in spp_sackdelay
2018 * is used to specify the sack delay for this address. Note
2019 * that if spp_address is empty then all addresses will
2020 * enable delayed sack and take on the sack delay
2021 * value specified in spp_sackdelay.
2022 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2023 * off delayed sack. If the spp_address field is blank then
2024 * delayed sack is disabled for the entire association. Note
2025 * also that this field is mutually exclusive to
2026 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2029 int sctp_apply_peer_addr_params(struct sctp_paddrparams
*params
,
2030 struct sctp_transport
*trans
,
2031 struct sctp_association
*asoc
,
2032 struct sctp_sock
*sp
,
2035 int sackdelay_change
)
2039 if (params
->spp_flags
& SPP_HB_DEMAND
&& trans
) {
2040 error
= sctp_primitive_REQUESTHEARTBEAT (trans
->asoc
, trans
);
2045 if (params
->spp_hbinterval
) {
2047 trans
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2049 asoc
->hbinterval
= msecs_to_jiffies(params
->spp_hbinterval
);
2051 sp
->hbinterval
= params
->spp_hbinterval
;
2057 trans
->param_flags
=
2058 (trans
->param_flags
& ~SPP_HB
) | hb_change
;
2061 (asoc
->param_flags
& ~SPP_HB
) | hb_change
;
2064 (sp
->param_flags
& ~SPP_HB
) | hb_change
;
2068 if (params
->spp_pathmtu
) {
2070 trans
->pathmtu
= params
->spp_pathmtu
;
2071 sctp_assoc_sync_pmtu(asoc
);
2073 asoc
->pathmtu
= params
->spp_pathmtu
;
2074 sctp_frag_point(sp
, params
->spp_pathmtu
);
2076 sp
->pathmtu
= params
->spp_pathmtu
;
2082 int update
= (trans
->param_flags
& SPP_PMTUD_DISABLE
) &&
2083 (params
->spp_flags
& SPP_PMTUD_ENABLE
);
2084 trans
->param_flags
=
2085 (trans
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2087 sctp_transport_pmtu(trans
);
2088 sctp_assoc_sync_pmtu(asoc
);
2092 (asoc
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2095 (sp
->param_flags
& ~SPP_PMTUD
) | pmtud_change
;
2099 if (params
->spp_sackdelay
) {
2102 msecs_to_jiffies(params
->spp_sackdelay
);
2105 msecs_to_jiffies(params
->spp_sackdelay
);
2107 sp
->sackdelay
= params
->spp_sackdelay
;
2111 if (sackdelay_change
) {
2113 trans
->param_flags
=
2114 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2118 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2122 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2127 if (params
->spp_pathmaxrxt
) {
2129 trans
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2131 asoc
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2133 sp
->pathmaxrxt
= params
->spp_pathmaxrxt
;
2140 static int sctp_setsockopt_peer_addr_params(struct sock
*sk
,
2141 char __user
*optval
, int optlen
)
2143 struct sctp_paddrparams params
;
2144 struct sctp_transport
*trans
= NULL
;
2145 struct sctp_association
*asoc
= NULL
;
2146 struct sctp_sock
*sp
= sctp_sk(sk
);
2148 int hb_change
, pmtud_change
, sackdelay_change
;
2150 if (optlen
!= sizeof(struct sctp_paddrparams
))
2153 if (copy_from_user(¶ms
, optval
, optlen
))
2156 /* Validate flags and value parameters. */
2157 hb_change
= params
.spp_flags
& SPP_HB
;
2158 pmtud_change
= params
.spp_flags
& SPP_PMTUD
;
2159 sackdelay_change
= params
.spp_flags
& SPP_SACKDELAY
;
2161 if (hb_change
== SPP_HB
||
2162 pmtud_change
== SPP_PMTUD
||
2163 sackdelay_change
== SPP_SACKDELAY
||
2164 params
.spp_sackdelay
> 500 ||
2166 && params
.spp_pathmtu
< SCTP_DEFAULT_MINSEGMENT
))
2169 /* If an address other than INADDR_ANY is specified, and
2170 * no transport is found, then the request is invalid.
2172 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
2173 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
2174 params
.spp_assoc_id
);
2179 /* Get association, if assoc_id != 0 and the socket is a one
2180 * to many style socket, and an association was not found, then
2181 * the id was invalid.
2183 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
2184 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
))
2187 /* Heartbeat demand can only be sent on a transport or
2188 * association, but not a socket.
2190 if (params
.spp_flags
& SPP_HB_DEMAND
&& !trans
&& !asoc
)
2193 /* Process parameters. */
2194 error
= sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2195 hb_change
, pmtud_change
,
2201 /* If changes are for association, also apply parameters to each
2204 if (!trans
&& asoc
) {
2205 struct list_head
*pos
;
2207 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2208 trans
= list_entry(pos
, struct sctp_transport
,
2210 sctp_apply_peer_addr_params(¶ms
, trans
, asoc
, sp
,
2211 hb_change
, pmtud_change
,
2219 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2221 * This options will get or set the delayed ack timer. The time is set
2222 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2223 * endpoints default delayed ack timer value. If the assoc_id field is
2224 * non-zero, then the set or get effects the specified association.
2226 * struct sctp_assoc_value {
2227 * sctp_assoc_t assoc_id;
2228 * uint32_t assoc_value;
2231 * assoc_id - This parameter, indicates which association the
2232 * user is preforming an action upon. Note that if
2233 * this field's value is zero then the endpoints
2234 * default value is changed (effecting future
2235 * associations only).
2237 * assoc_value - This parameter contains the number of milliseconds
2238 * that the user is requesting the delayed ACK timer
2239 * be set to. Note that this value is defined in
2240 * the standard to be between 200 and 500 milliseconds.
2242 * Note: a value of zero will leave the value alone,
2243 * but disable SACK delay. A non-zero value will also
2244 * enable SACK delay.
2247 static int sctp_setsockopt_delayed_ack_time(struct sock
*sk
,
2248 char __user
*optval
, int optlen
)
2250 struct sctp_assoc_value params
;
2251 struct sctp_transport
*trans
= NULL
;
2252 struct sctp_association
*asoc
= NULL
;
2253 struct sctp_sock
*sp
= sctp_sk(sk
);
2255 if (optlen
!= sizeof(struct sctp_assoc_value
))
2258 if (copy_from_user(¶ms
, optval
, optlen
))
2261 /* Validate value parameter. */
2262 if (params
.assoc_value
> 500)
2265 /* Get association, if assoc_id != 0 and the socket is a one
2266 * to many style socket, and an association was not found, then
2267 * the id was invalid.
2269 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
2270 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
2273 if (params
.assoc_value
) {
2276 msecs_to_jiffies(params
.assoc_value
);
2278 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2279 SPP_SACKDELAY_ENABLE
;
2281 sp
->sackdelay
= params
.assoc_value
;
2283 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2284 SPP_SACKDELAY_ENABLE
;
2289 (asoc
->param_flags
& ~SPP_SACKDELAY
) |
2290 SPP_SACKDELAY_DISABLE
;
2293 (sp
->param_flags
& ~SPP_SACKDELAY
) |
2294 SPP_SACKDELAY_DISABLE
;
2298 /* If change is for association, also apply to each transport. */
2300 struct list_head
*pos
;
2302 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2303 trans
= list_entry(pos
, struct sctp_transport
,
2305 if (params
.assoc_value
) {
2307 msecs_to_jiffies(params
.assoc_value
);
2308 trans
->param_flags
=
2309 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2310 SPP_SACKDELAY_ENABLE
;
2312 trans
->param_flags
=
2313 (trans
->param_flags
& ~SPP_SACKDELAY
) |
2314 SPP_SACKDELAY_DISABLE
;
2322 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2324 * Applications can specify protocol parameters for the default association
2325 * initialization. The option name argument to setsockopt() and getsockopt()
2328 * Setting initialization parameters is effective only on an unconnected
2329 * socket (for UDP-style sockets only future associations are effected
2330 * by the change). With TCP-style sockets, this option is inherited by
2331 * sockets derived from a listener socket.
2333 static int sctp_setsockopt_initmsg(struct sock
*sk
, char __user
*optval
, int optlen
)
2335 struct sctp_initmsg sinit
;
2336 struct sctp_sock
*sp
= sctp_sk(sk
);
2338 if (optlen
!= sizeof(struct sctp_initmsg
))
2340 if (copy_from_user(&sinit
, optval
, optlen
))
2343 if (sinit
.sinit_num_ostreams
)
2344 sp
->initmsg
.sinit_num_ostreams
= sinit
.sinit_num_ostreams
;
2345 if (sinit
.sinit_max_instreams
)
2346 sp
->initmsg
.sinit_max_instreams
= sinit
.sinit_max_instreams
;
2347 if (sinit
.sinit_max_attempts
)
2348 sp
->initmsg
.sinit_max_attempts
= sinit
.sinit_max_attempts
;
2349 if (sinit
.sinit_max_init_timeo
)
2350 sp
->initmsg
.sinit_max_init_timeo
= sinit
.sinit_max_init_timeo
;
2356 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2358 * Applications that wish to use the sendto() system call may wish to
2359 * specify a default set of parameters that would normally be supplied
2360 * through the inclusion of ancillary data. This socket option allows
2361 * such an application to set the default sctp_sndrcvinfo structure.
2362 * The application that wishes to use this socket option simply passes
2363 * in to this call the sctp_sndrcvinfo structure defined in Section
2364 * 5.2.2) The input parameters accepted by this call include
2365 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2366 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2367 * to this call if the caller is using the UDP model.
2369 static int sctp_setsockopt_default_send_param(struct sock
*sk
,
2370 char __user
*optval
, int optlen
)
2372 struct sctp_sndrcvinfo info
;
2373 struct sctp_association
*asoc
;
2374 struct sctp_sock
*sp
= sctp_sk(sk
);
2376 if (optlen
!= sizeof(struct sctp_sndrcvinfo
))
2378 if (copy_from_user(&info
, optval
, optlen
))
2381 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
2382 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
2386 asoc
->default_stream
= info
.sinfo_stream
;
2387 asoc
->default_flags
= info
.sinfo_flags
;
2388 asoc
->default_ppid
= info
.sinfo_ppid
;
2389 asoc
->default_context
= info
.sinfo_context
;
2390 asoc
->default_timetolive
= info
.sinfo_timetolive
;
2392 sp
->default_stream
= info
.sinfo_stream
;
2393 sp
->default_flags
= info
.sinfo_flags
;
2394 sp
->default_ppid
= info
.sinfo_ppid
;
2395 sp
->default_context
= info
.sinfo_context
;
2396 sp
->default_timetolive
= info
.sinfo_timetolive
;
2402 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2404 * Requests that the local SCTP stack use the enclosed peer address as
2405 * the association primary. The enclosed address must be one of the
2406 * association peer's addresses.
2408 static int sctp_setsockopt_primary_addr(struct sock
*sk
, char __user
*optval
,
2411 struct sctp_prim prim
;
2412 struct sctp_transport
*trans
;
2414 if (optlen
!= sizeof(struct sctp_prim
))
2417 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
2420 trans
= sctp_addr_id2transport(sk
, &prim
.ssp_addr
, prim
.ssp_assoc_id
);
2424 sctp_assoc_set_primary(trans
->asoc
, trans
);
2430 * 7.1.5 SCTP_NODELAY
2432 * Turn on/off any Nagle-like algorithm. This means that packets are
2433 * generally sent as soon as possible and no unnecessary delays are
2434 * introduced, at the cost of more packets in the network. Expects an
2435 * integer boolean flag.
2437 static int sctp_setsockopt_nodelay(struct sock
*sk
, char __user
*optval
,
2442 if (optlen
< sizeof(int))
2444 if (get_user(val
, (int __user
*)optval
))
2447 sctp_sk(sk
)->nodelay
= (val
== 0) ? 0 : 1;
2453 * 7.1.1 SCTP_RTOINFO
2455 * The protocol parameters used to initialize and bound retransmission
2456 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2457 * and modify these parameters.
2458 * All parameters are time values, in milliseconds. A value of 0, when
2459 * modifying the parameters, indicates that the current value should not
2463 static int sctp_setsockopt_rtoinfo(struct sock
*sk
, char __user
*optval
, int optlen
) {
2464 struct sctp_rtoinfo rtoinfo
;
2465 struct sctp_association
*asoc
;
2467 if (optlen
!= sizeof (struct sctp_rtoinfo
))
2470 if (copy_from_user(&rtoinfo
, optval
, optlen
))
2473 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
2475 /* Set the values to the specific association */
2476 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
2480 if (rtoinfo
.srto_initial
!= 0)
2482 msecs_to_jiffies(rtoinfo
.srto_initial
);
2483 if (rtoinfo
.srto_max
!= 0)
2484 asoc
->rto_max
= msecs_to_jiffies(rtoinfo
.srto_max
);
2485 if (rtoinfo
.srto_min
!= 0)
2486 asoc
->rto_min
= msecs_to_jiffies(rtoinfo
.srto_min
);
2488 /* If there is no association or the association-id = 0
2489 * set the values to the endpoint.
2491 struct sctp_sock
*sp
= sctp_sk(sk
);
2493 if (rtoinfo
.srto_initial
!= 0)
2494 sp
->rtoinfo
.srto_initial
= rtoinfo
.srto_initial
;
2495 if (rtoinfo
.srto_max
!= 0)
2496 sp
->rtoinfo
.srto_max
= rtoinfo
.srto_max
;
2497 if (rtoinfo
.srto_min
!= 0)
2498 sp
->rtoinfo
.srto_min
= rtoinfo
.srto_min
;
2506 * 7.1.2 SCTP_ASSOCINFO
2508 * This option is used to tune the the maximum retransmission attempts
2509 * of the association.
2510 * Returns an error if the new association retransmission value is
2511 * greater than the sum of the retransmission value of the peer.
2512 * See [SCTP] for more information.
2515 static int sctp_setsockopt_associnfo(struct sock
*sk
, char __user
*optval
, int optlen
)
2518 struct sctp_assocparams assocparams
;
2519 struct sctp_association
*asoc
;
2521 if (optlen
!= sizeof(struct sctp_assocparams
))
2523 if (copy_from_user(&assocparams
, optval
, optlen
))
2526 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
2528 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
2531 /* Set the values to the specific association */
2533 if (assocparams
.sasoc_asocmaxrxt
!= 0) {
2536 struct list_head
*pos
;
2537 struct sctp_transport
*peer_addr
;
2539 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2540 peer_addr
= list_entry(pos
,
2541 struct sctp_transport
,
2543 path_sum
+= peer_addr
->pathmaxrxt
;
2547 /* Only validate asocmaxrxt if we have more then
2548 * one path/transport. We do this because path
2549 * retransmissions are only counted when we have more
2553 assocparams
.sasoc_asocmaxrxt
> path_sum
)
2556 asoc
->max_retrans
= assocparams
.sasoc_asocmaxrxt
;
2559 if (assocparams
.sasoc_cookie_life
!= 0) {
2560 asoc
->cookie_life
.tv_sec
=
2561 assocparams
.sasoc_cookie_life
/ 1000;
2562 asoc
->cookie_life
.tv_usec
=
2563 (assocparams
.sasoc_cookie_life
% 1000)
2567 /* Set the values to the endpoint */
2568 struct sctp_sock
*sp
= sctp_sk(sk
);
2570 if (assocparams
.sasoc_asocmaxrxt
!= 0)
2571 sp
->assocparams
.sasoc_asocmaxrxt
=
2572 assocparams
.sasoc_asocmaxrxt
;
2573 if (assocparams
.sasoc_cookie_life
!= 0)
2574 sp
->assocparams
.sasoc_cookie_life
=
2575 assocparams
.sasoc_cookie_life
;
2581 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2583 * This socket option is a boolean flag which turns on or off mapped V4
2584 * addresses. If this option is turned on and the socket is type
2585 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2586 * If this option is turned off, then no mapping will be done of V4
2587 * addresses and a user will receive both PF_INET6 and PF_INET type
2588 * addresses on the socket.
2590 static int sctp_setsockopt_mappedv4(struct sock
*sk
, char __user
*optval
, int optlen
)
2593 struct sctp_sock
*sp
= sctp_sk(sk
);
2595 if (optlen
< sizeof(int))
2597 if (get_user(val
, (int __user
*)optval
))
2608 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2610 * This socket option specifies the maximum size to put in any outgoing
2611 * SCTP chunk. If a message is larger than this size it will be
2612 * fragmented by SCTP into the specified size. Note that the underlying
2613 * SCTP implementation may fragment into smaller sized chunks when the
2614 * PMTU of the underlying association is smaller than the value set by
2617 static int sctp_setsockopt_maxseg(struct sock
*sk
, char __user
*optval
, int optlen
)
2619 struct sctp_association
*asoc
;
2620 struct list_head
*pos
;
2621 struct sctp_sock
*sp
= sctp_sk(sk
);
2624 if (optlen
< sizeof(int))
2626 if (get_user(val
, (int __user
*)optval
))
2628 if ((val
!= 0) && ((val
< 8) || (val
> SCTP_MAX_CHUNK_LEN
)))
2630 sp
->user_frag
= val
;
2632 /* Update the frag_point of the existing associations. */
2633 list_for_each(pos
, &(sp
->ep
->asocs
)) {
2634 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
2635 asoc
->frag_point
= sctp_frag_point(sp
, asoc
->pathmtu
);
2643 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2645 * Requests that the peer mark the enclosed address as the association
2646 * primary. The enclosed address must be one of the association's
2647 * locally bound addresses. The following structure is used to make a
2648 * set primary request:
2650 static int sctp_setsockopt_peer_primary_addr(struct sock
*sk
, char __user
*optval
,
2653 struct sctp_sock
*sp
;
2654 struct sctp_endpoint
*ep
;
2655 struct sctp_association
*asoc
= NULL
;
2656 struct sctp_setpeerprim prim
;
2657 struct sctp_chunk
*chunk
;
2663 if (!sctp_addip_enable
)
2666 if (optlen
!= sizeof(struct sctp_setpeerprim
))
2669 if (copy_from_user(&prim
, optval
, optlen
))
2672 asoc
= sctp_id2assoc(sk
, prim
.sspp_assoc_id
);
2676 if (!asoc
->peer
.asconf_capable
)
2679 if (asoc
->peer
.addip_disabled_mask
& SCTP_PARAM_SET_PRIMARY
)
2682 if (!sctp_state(asoc
, ESTABLISHED
))
2685 if (!sctp_assoc_lookup_laddr(asoc
, (union sctp_addr
*)&prim
.sspp_addr
))
2686 return -EADDRNOTAVAIL
;
2688 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2689 chunk
= sctp_make_asconf_set_prim(asoc
,
2690 (union sctp_addr
*)&prim
.sspp_addr
);
2694 err
= sctp_send_asconf(asoc
, chunk
);
2696 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2701 static int sctp_setsockopt_adaption_layer(struct sock
*sk
, char __user
*optval
,
2704 struct sctp_setadaption adaption
;
2706 if (optlen
!= sizeof(struct sctp_setadaption
))
2708 if (copy_from_user(&adaption
, optval
, optlen
))
2711 sctp_sk(sk
)->adaption_ind
= adaption
.ssb_adaption_ind
;
2716 /* API 6.2 setsockopt(), getsockopt()
2718 * Applications use setsockopt() and getsockopt() to set or retrieve
2719 * socket options. Socket options are used to change the default
2720 * behavior of sockets calls. They are described in Section 7.
2724 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2725 * int __user *optlen);
2726 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2729 * sd - the socket descript.
2730 * level - set to IPPROTO_SCTP for all SCTP options.
2731 * optname - the option name.
2732 * optval - the buffer to store the value of the option.
2733 * optlen - the size of the buffer.
2735 SCTP_STATIC
int sctp_setsockopt(struct sock
*sk
, int level
, int optname
,
2736 char __user
*optval
, int optlen
)
2740 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2743 /* I can hardly begin to describe how wrong this is. This is
2744 * so broken as to be worse than useless. The API draft
2745 * REALLY is NOT helpful here... I am not convinced that the
2746 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2747 * are at all well-founded.
2749 if (level
!= SOL_SCTP
) {
2750 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
2751 retval
= af
->setsockopt(sk
, level
, optname
, optval
, optlen
);
2758 case SCTP_SOCKOPT_BINDX_ADD
:
2759 /* 'optlen' is the size of the addresses buffer. */
2760 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2761 optlen
, SCTP_BINDX_ADD_ADDR
);
2764 case SCTP_SOCKOPT_BINDX_REM
:
2765 /* 'optlen' is the size of the addresses buffer. */
2766 retval
= sctp_setsockopt_bindx(sk
, (struct sockaddr __user
*)optval
,
2767 optlen
, SCTP_BINDX_REM_ADDR
);
2770 case SCTP_SOCKOPT_CONNECTX
:
2771 /* 'optlen' is the size of the addresses buffer. */
2772 retval
= sctp_setsockopt_connectx(sk
, (struct sockaddr __user
*)optval
,
2776 case SCTP_DISABLE_FRAGMENTS
:
2777 retval
= sctp_setsockopt_disable_fragments(sk
, optval
, optlen
);
2781 retval
= sctp_setsockopt_events(sk
, optval
, optlen
);
2784 case SCTP_AUTOCLOSE
:
2785 retval
= sctp_setsockopt_autoclose(sk
, optval
, optlen
);
2788 case SCTP_PEER_ADDR_PARAMS
:
2789 retval
= sctp_setsockopt_peer_addr_params(sk
, optval
, optlen
);
2792 case SCTP_DELAYED_ACK_TIME
:
2793 retval
= sctp_setsockopt_delayed_ack_time(sk
, optval
, optlen
);
2797 retval
= sctp_setsockopt_initmsg(sk
, optval
, optlen
);
2799 case SCTP_DEFAULT_SEND_PARAM
:
2800 retval
= sctp_setsockopt_default_send_param(sk
, optval
,
2803 case SCTP_PRIMARY_ADDR
:
2804 retval
= sctp_setsockopt_primary_addr(sk
, optval
, optlen
);
2806 case SCTP_SET_PEER_PRIMARY_ADDR
:
2807 retval
= sctp_setsockopt_peer_primary_addr(sk
, optval
, optlen
);
2810 retval
= sctp_setsockopt_nodelay(sk
, optval
, optlen
);
2813 retval
= sctp_setsockopt_rtoinfo(sk
, optval
, optlen
);
2815 case SCTP_ASSOCINFO
:
2816 retval
= sctp_setsockopt_associnfo(sk
, optval
, optlen
);
2818 case SCTP_I_WANT_MAPPED_V4_ADDR
:
2819 retval
= sctp_setsockopt_mappedv4(sk
, optval
, optlen
);
2822 retval
= sctp_setsockopt_maxseg(sk
, optval
, optlen
);
2824 case SCTP_ADAPTION_LAYER
:
2825 retval
= sctp_setsockopt_adaption_layer(sk
, optval
, optlen
);
2829 retval
= -ENOPROTOOPT
;
2833 sctp_release_sock(sk
);
2839 /* API 3.1.6 connect() - UDP Style Syntax
2841 * An application may use the connect() call in the UDP model to initiate an
2842 * association without sending data.
2846 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2848 * sd: the socket descriptor to have a new association added to.
2850 * nam: the address structure (either struct sockaddr_in or struct
2851 * sockaddr_in6 defined in RFC2553 [7]).
2853 * len: the size of the address.
2855 SCTP_STATIC
int sctp_connect(struct sock
*sk
, struct sockaddr
*addr
,
2863 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2864 __FUNCTION__
, sk
, addr
, addr_len
);
2866 /* Validate addr_len before calling common connect/connectx routine. */
2867 af
= sctp_get_af_specific(addr
->sa_family
);
2868 if (!af
|| addr_len
< af
->sockaddr_len
) {
2871 /* Pass correct addr len to common routine (so it knows there
2872 * is only one address being passed.
2874 err
= __sctp_connect(sk
, addr
, af
->sockaddr_len
);
2877 sctp_release_sock(sk
);
2881 /* FIXME: Write comments. */
2882 SCTP_STATIC
int sctp_disconnect(struct sock
*sk
, int flags
)
2884 return -EOPNOTSUPP
; /* STUB */
2887 /* 4.1.4 accept() - TCP Style Syntax
2889 * Applications use accept() call to remove an established SCTP
2890 * association from the accept queue of the endpoint. A new socket
2891 * descriptor will be returned from accept() to represent the newly
2892 * formed association.
2894 SCTP_STATIC
struct sock
*sctp_accept(struct sock
*sk
, int flags
, int *err
)
2896 struct sctp_sock
*sp
;
2897 struct sctp_endpoint
*ep
;
2898 struct sock
*newsk
= NULL
;
2899 struct sctp_association
*asoc
;
2908 if (!sctp_style(sk
, TCP
)) {
2909 error
= -EOPNOTSUPP
;
2913 if (!sctp_sstate(sk
, LISTENING
)) {
2918 timeo
= sock_rcvtimeo(sk
, sk
->sk_socket
->file
->f_flags
& O_NONBLOCK
);
2920 error
= sctp_wait_for_accept(sk
, timeo
);
2924 /* We treat the list of associations on the endpoint as the accept
2925 * queue and pick the first association on the list.
2927 asoc
= list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
2929 newsk
= sp
->pf
->create_accept_sk(sk
, asoc
);
2935 /* Populate the fields of the newsk from the oldsk and migrate the
2936 * asoc to the newsk.
2938 sctp_sock_migrate(sk
, newsk
, asoc
, SCTP_SOCKET_TCP
);
2941 sctp_release_sock(sk
);
2946 /* The SCTP ioctl handler. */
2947 SCTP_STATIC
int sctp_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
2949 return -ENOIOCTLCMD
;
2952 /* This is the function which gets called during socket creation to
2953 * initialized the SCTP-specific portion of the sock.
2954 * The sock structure should already be zero-filled memory.
2956 SCTP_STATIC
int sctp_init_sock(struct sock
*sk
)
2958 struct sctp_endpoint
*ep
;
2959 struct sctp_sock
*sp
;
2961 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk
);
2965 /* Initialize the SCTP per socket area. */
2966 switch (sk
->sk_type
) {
2967 case SOCK_SEQPACKET
:
2968 sp
->type
= SCTP_SOCKET_UDP
;
2971 sp
->type
= SCTP_SOCKET_TCP
;
2974 return -ESOCKTNOSUPPORT
;
2977 /* Initialize default send parameters. These parameters can be
2978 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2980 sp
->default_stream
= 0;
2981 sp
->default_ppid
= 0;
2982 sp
->default_flags
= 0;
2983 sp
->default_context
= 0;
2984 sp
->default_timetolive
= 0;
2986 /* Initialize default setup parameters. These parameters
2987 * can be modified with the SCTP_INITMSG socket option or
2988 * overridden by the SCTP_INIT CMSG.
2990 sp
->initmsg
.sinit_num_ostreams
= sctp_max_outstreams
;
2991 sp
->initmsg
.sinit_max_instreams
= sctp_max_instreams
;
2992 sp
->initmsg
.sinit_max_attempts
= sctp_max_retrans_init
;
2993 sp
->initmsg
.sinit_max_init_timeo
= jiffies_to_msecs(sctp_rto_max
);
2995 /* Initialize default RTO related parameters. These parameters can
2996 * be modified for with the SCTP_RTOINFO socket option.
2998 sp
->rtoinfo
.srto_initial
= jiffies_to_msecs(sctp_rto_initial
);
2999 sp
->rtoinfo
.srto_max
= jiffies_to_msecs(sctp_rto_max
);
3000 sp
->rtoinfo
.srto_min
= jiffies_to_msecs(sctp_rto_min
);
3002 /* Initialize default association related parameters. These parameters
3003 * can be modified with the SCTP_ASSOCINFO socket option.
3005 sp
->assocparams
.sasoc_asocmaxrxt
= sctp_max_retrans_association
;
3006 sp
->assocparams
.sasoc_number_peer_destinations
= 0;
3007 sp
->assocparams
.sasoc_peer_rwnd
= 0;
3008 sp
->assocparams
.sasoc_local_rwnd
= 0;
3009 sp
->assocparams
.sasoc_cookie_life
=
3010 jiffies_to_msecs(sctp_valid_cookie_life
);
3012 /* Initialize default event subscriptions. By default, all the
3015 memset(&sp
->subscribe
, 0, sizeof(struct sctp_event_subscribe
));
3017 /* Default Peer Address Parameters. These defaults can
3018 * be modified via SCTP_PEER_ADDR_PARAMS
3020 sp
->hbinterval
= jiffies_to_msecs(sctp_hb_interval
);
3021 sp
->pathmaxrxt
= sctp_max_retrans_path
;
3022 sp
->pathmtu
= 0; // allow default discovery
3023 sp
->sackdelay
= jiffies_to_msecs(sctp_sack_timeout
);
3024 sp
->param_flags
= SPP_HB_ENABLE
|
3026 SPP_SACKDELAY_ENABLE
;
3028 /* If enabled no SCTP message fragmentation will be performed.
3029 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3031 sp
->disable_fragments
= 0;
3033 /* Turn on/off any Nagle-like algorithm. */
3036 /* Enable by default. */
3039 /* Auto-close idle associations after the configured
3040 * number of seconds. A value of 0 disables this
3041 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3042 * for UDP-style sockets only.
3046 /* User specified fragmentation limit. */
3049 sp
->adaption_ind
= 0;
3051 sp
->pf
= sctp_get_pf_specific(sk
->sk_family
);
3053 /* Control variables for partial data delivery. */
3055 skb_queue_head_init(&sp
->pd_lobby
);
3057 /* Create a per socket endpoint structure. Even if we
3058 * change the data structure relationships, this may still
3059 * be useful for storing pre-connect address information.
3061 ep
= sctp_endpoint_new(sk
, GFP_KERNEL
);
3068 SCTP_DBG_OBJCNT_INC(sock
);
3072 /* Cleanup any SCTP per socket resources. */
3073 SCTP_STATIC
int sctp_destroy_sock(struct sock
*sk
)
3075 struct sctp_endpoint
*ep
;
3077 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk
);
3079 /* Release our hold on the endpoint. */
3080 ep
= sctp_sk(sk
)->ep
;
3081 sctp_endpoint_free(ep
);
3086 /* API 4.1.7 shutdown() - TCP Style Syntax
3087 * int shutdown(int socket, int how);
3089 * sd - the socket descriptor of the association to be closed.
3090 * how - Specifies the type of shutdown. The values are
3093 * Disables further receive operations. No SCTP
3094 * protocol action is taken.
3096 * Disables further send operations, and initiates
3097 * the SCTP shutdown sequence.
3099 * Disables further send and receive operations
3100 * and initiates the SCTP shutdown sequence.
3102 SCTP_STATIC
void sctp_shutdown(struct sock
*sk
, int how
)
3104 struct sctp_endpoint
*ep
;
3105 struct sctp_association
*asoc
;
3107 if (!sctp_style(sk
, TCP
))
3110 if (how
& SEND_SHUTDOWN
) {
3111 ep
= sctp_sk(sk
)->ep
;
3112 if (!list_empty(&ep
->asocs
)) {
3113 asoc
= list_entry(ep
->asocs
.next
,
3114 struct sctp_association
, asocs
);
3115 sctp_primitive_SHUTDOWN(asoc
, NULL
);
3120 /* 7.2.1 Association Status (SCTP_STATUS)
3122 * Applications can retrieve current status information about an
3123 * association, including association state, peer receiver window size,
3124 * number of unacked data chunks, and number of data chunks pending
3125 * receipt. This information is read-only.
3127 static int sctp_getsockopt_sctp_status(struct sock
*sk
, int len
,
3128 char __user
*optval
,
3131 struct sctp_status status
;
3132 struct sctp_association
*asoc
= NULL
;
3133 struct sctp_transport
*transport
;
3134 sctp_assoc_t associd
;
3137 if (len
!= sizeof(status
)) {
3142 if (copy_from_user(&status
, optval
, sizeof(status
))) {
3147 associd
= status
.sstat_assoc_id
;
3148 asoc
= sctp_id2assoc(sk
, associd
);
3154 transport
= asoc
->peer
.primary_path
;
3156 status
.sstat_assoc_id
= sctp_assoc2id(asoc
);
3157 status
.sstat_state
= asoc
->state
;
3158 status
.sstat_rwnd
= asoc
->peer
.rwnd
;
3159 status
.sstat_unackdata
= asoc
->unack_data
;
3161 status
.sstat_penddata
= sctp_tsnmap_pending(&asoc
->peer
.tsn_map
);
3162 status
.sstat_instrms
= asoc
->c
.sinit_max_instreams
;
3163 status
.sstat_outstrms
= asoc
->c
.sinit_num_ostreams
;
3164 status
.sstat_fragmentation_point
= asoc
->frag_point
;
3165 status
.sstat_primary
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3166 memcpy(&status
.sstat_primary
.spinfo_address
,
3167 &(transport
->ipaddr
), sizeof(union sctp_addr
));
3168 /* Map ipv4 address into v4-mapped-on-v6 address. */
3169 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3170 (union sctp_addr
*)&status
.sstat_primary
.spinfo_address
);
3171 status
.sstat_primary
.spinfo_state
= transport
->state
;
3172 status
.sstat_primary
.spinfo_cwnd
= transport
->cwnd
;
3173 status
.sstat_primary
.spinfo_srtt
= transport
->srtt
;
3174 status
.sstat_primary
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3175 status
.sstat_primary
.spinfo_mtu
= transport
->pathmtu
;
3177 if (status
.sstat_primary
.spinfo_state
== SCTP_UNKNOWN
)
3178 status
.sstat_primary
.spinfo_state
= SCTP_ACTIVE
;
3180 if (put_user(len
, optlen
)) {
3185 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3186 len
, status
.sstat_state
, status
.sstat_rwnd
,
3187 status
.sstat_assoc_id
);
3189 if (copy_to_user(optval
, &status
, len
)) {
3199 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3201 * Applications can retrieve information about a specific peer address
3202 * of an association, including its reachability state, congestion
3203 * window, and retransmission timer values. This information is
3206 static int sctp_getsockopt_peer_addr_info(struct sock
*sk
, int len
,
3207 char __user
*optval
,
3210 struct sctp_paddrinfo pinfo
;
3211 struct sctp_transport
*transport
;
3214 if (len
!= sizeof(pinfo
)) {
3219 if (copy_from_user(&pinfo
, optval
, sizeof(pinfo
))) {
3224 transport
= sctp_addr_id2transport(sk
, &pinfo
.spinfo_address
,
3225 pinfo
.spinfo_assoc_id
);
3229 pinfo
.spinfo_assoc_id
= sctp_assoc2id(transport
->asoc
);
3230 pinfo
.spinfo_state
= transport
->state
;
3231 pinfo
.spinfo_cwnd
= transport
->cwnd
;
3232 pinfo
.spinfo_srtt
= transport
->srtt
;
3233 pinfo
.spinfo_rto
= jiffies_to_msecs(transport
->rto
);
3234 pinfo
.spinfo_mtu
= transport
->pathmtu
;
3236 if (pinfo
.spinfo_state
== SCTP_UNKNOWN
)
3237 pinfo
.spinfo_state
= SCTP_ACTIVE
;
3239 if (put_user(len
, optlen
)) {
3244 if (copy_to_user(optval
, &pinfo
, len
)) {
3253 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3255 * This option is a on/off flag. If enabled no SCTP message
3256 * fragmentation will be performed. Instead if a message being sent
3257 * exceeds the current PMTU size, the message will NOT be sent and
3258 * instead a error will be indicated to the user.
3260 static int sctp_getsockopt_disable_fragments(struct sock
*sk
, int len
,
3261 char __user
*optval
, int __user
*optlen
)
3265 if (len
< sizeof(int))
3269 val
= (sctp_sk(sk
)->disable_fragments
== 1);
3270 if (put_user(len
, optlen
))
3272 if (copy_to_user(optval
, &val
, len
))
3277 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3279 * This socket option is used to specify various notifications and
3280 * ancillary data the user wishes to receive.
3282 static int sctp_getsockopt_events(struct sock
*sk
, int len
, char __user
*optval
,
3285 if (len
!= sizeof(struct sctp_event_subscribe
))
3287 if (copy_to_user(optval
, &sctp_sk(sk
)->subscribe
, len
))
3292 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3294 * This socket option is applicable to the UDP-style socket only. When
3295 * set it will cause associations that are idle for more than the
3296 * specified number of seconds to automatically close. An association
3297 * being idle is defined an association that has NOT sent or received
3298 * user data. The special value of '0' indicates that no automatic
3299 * close of any associations should be performed. The option expects an
3300 * integer defining the number of seconds of idle time before an
3301 * association is closed.
3303 static int sctp_getsockopt_autoclose(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3305 /* Applicable to UDP-style socket only */
3306 if (sctp_style(sk
, TCP
))
3308 if (len
!= sizeof(int))
3310 if (copy_to_user(optval
, &sctp_sk(sk
)->autoclose
, len
))
3315 /* Helper routine to branch off an association to a new socket. */
3316 SCTP_STATIC
int sctp_do_peeloff(struct sctp_association
*asoc
,
3317 struct socket
**sockp
)
3319 struct sock
*sk
= asoc
->base
.sk
;
3320 struct socket
*sock
;
3323 /* An association cannot be branched off from an already peeled-off
3324 * socket, nor is this supported for tcp style sockets.
3326 if (!sctp_style(sk
, UDP
))
3329 /* Create a new socket. */
3330 err
= sock_create(sk
->sk_family
, SOCK_SEQPACKET
, IPPROTO_SCTP
, &sock
);
3334 /* Populate the fields of the newsk from the oldsk and migrate the
3335 * asoc to the newsk.
3337 sctp_sock_migrate(sk
, sock
->sk
, asoc
, SCTP_SOCKET_UDP_HIGH_BANDWIDTH
);
3343 static int sctp_getsockopt_peeloff(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3345 sctp_peeloff_arg_t peeloff
;
3346 struct socket
*newsock
;
3348 struct sctp_association
*asoc
;
3350 if (len
!= sizeof(sctp_peeloff_arg_t
))
3352 if (copy_from_user(&peeloff
, optval
, len
))
3355 asoc
= sctp_id2assoc(sk
, peeloff
.associd
);
3361 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__
, sk
, asoc
);
3363 retval
= sctp_do_peeloff(asoc
, &newsock
);
3367 /* Map the socket to an unused fd that can be returned to the user. */
3368 retval
= sock_map_fd(newsock
);
3370 sock_release(newsock
);
3374 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3375 __FUNCTION__
, sk
, asoc
, newsock
->sk
, retval
);
3377 /* Return the fd mapped to the new socket. */
3378 peeloff
.sd
= retval
;
3379 if (copy_to_user(optval
, &peeloff
, len
))
3386 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3388 * Applications can enable or disable heartbeats for any peer address of
3389 * an association, modify an address's heartbeat interval, force a
3390 * heartbeat to be sent immediately, and adjust the address's maximum
3391 * number of retransmissions sent before an address is considered
3392 * unreachable. The following structure is used to access and modify an
3393 * address's parameters:
3395 * struct sctp_paddrparams {
3396 * sctp_assoc_t spp_assoc_id;
3397 * struct sockaddr_storage spp_address;
3398 * uint32_t spp_hbinterval;
3399 * uint16_t spp_pathmaxrxt;
3400 * uint32_t spp_pathmtu;
3401 * uint32_t spp_sackdelay;
3402 * uint32_t spp_flags;
3405 * spp_assoc_id - (one-to-many style socket) This is filled in the
3406 * application, and identifies the association for
3408 * spp_address - This specifies which address is of interest.
3409 * spp_hbinterval - This contains the value of the heartbeat interval,
3410 * in milliseconds. If a value of zero
3411 * is present in this field then no changes are to
3412 * be made to this parameter.
3413 * spp_pathmaxrxt - This contains the maximum number of
3414 * retransmissions before this address shall be
3415 * considered unreachable. If a value of zero
3416 * is present in this field then no changes are to
3417 * be made to this parameter.
3418 * spp_pathmtu - When Path MTU discovery is disabled the value
3419 * specified here will be the "fixed" path mtu.
3420 * Note that if the spp_address field is empty
3421 * then all associations on this address will
3422 * have this fixed path mtu set upon them.
3424 * spp_sackdelay - When delayed sack is enabled, this value specifies
3425 * the number of milliseconds that sacks will be delayed
3426 * for. This value will apply to all addresses of an
3427 * association if the spp_address field is empty. Note
3428 * also, that if delayed sack is enabled and this
3429 * value is set to 0, no change is made to the last
3430 * recorded delayed sack timer value.
3432 * spp_flags - These flags are used to control various features
3433 * on an association. The flag field may contain
3434 * zero or more of the following options.
3436 * SPP_HB_ENABLE - Enable heartbeats on the
3437 * specified address. Note that if the address
3438 * field is empty all addresses for the association
3439 * have heartbeats enabled upon them.
3441 * SPP_HB_DISABLE - Disable heartbeats on the
3442 * speicifed address. Note that if the address
3443 * field is empty all addresses for the association
3444 * will have their heartbeats disabled. Note also
3445 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3446 * mutually exclusive, only one of these two should
3447 * be specified. Enabling both fields will have
3448 * undetermined results.
3450 * SPP_HB_DEMAND - Request a user initiated heartbeat
3451 * to be made immediately.
3453 * SPP_PMTUD_ENABLE - This field will enable PMTU
3454 * discovery upon the specified address. Note that
3455 * if the address feild is empty then all addresses
3456 * on the association are effected.
3458 * SPP_PMTUD_DISABLE - This field will disable PMTU
3459 * discovery upon the specified address. Note that
3460 * if the address feild is empty then all addresses
3461 * on the association are effected. Not also that
3462 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3463 * exclusive. Enabling both will have undetermined
3466 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3467 * on delayed sack. The time specified in spp_sackdelay
3468 * is used to specify the sack delay for this address. Note
3469 * that if spp_address is empty then all addresses will
3470 * enable delayed sack and take on the sack delay
3471 * value specified in spp_sackdelay.
3472 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3473 * off delayed sack. If the spp_address field is blank then
3474 * delayed sack is disabled for the entire association. Note
3475 * also that this field is mutually exclusive to
3476 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3479 static int sctp_getsockopt_peer_addr_params(struct sock
*sk
, int len
,
3480 char __user
*optval
, int __user
*optlen
)
3482 struct sctp_paddrparams params
;
3483 struct sctp_transport
*trans
= NULL
;
3484 struct sctp_association
*asoc
= NULL
;
3485 struct sctp_sock
*sp
= sctp_sk(sk
);
3487 if (len
!= sizeof(struct sctp_paddrparams
))
3490 if (copy_from_user(¶ms
, optval
, len
))
3493 /* If an address other than INADDR_ANY is specified, and
3494 * no transport is found, then the request is invalid.
3496 if (!sctp_is_any(( union sctp_addr
*)¶ms
.spp_address
)) {
3497 trans
= sctp_addr_id2transport(sk
, ¶ms
.spp_address
,
3498 params
.spp_assoc_id
);
3500 SCTP_DEBUG_PRINTK("Failed no transport\n");
3505 /* Get association, if assoc_id != 0 and the socket is a one
3506 * to many style socket, and an association was not found, then
3507 * the id was invalid.
3509 asoc
= sctp_id2assoc(sk
, params
.spp_assoc_id
);
3510 if (!asoc
&& params
.spp_assoc_id
&& sctp_style(sk
, UDP
)) {
3511 SCTP_DEBUG_PRINTK("Failed no association\n");
3516 /* Fetch transport values. */
3517 params
.spp_hbinterval
= jiffies_to_msecs(trans
->hbinterval
);
3518 params
.spp_pathmtu
= trans
->pathmtu
;
3519 params
.spp_pathmaxrxt
= trans
->pathmaxrxt
;
3520 params
.spp_sackdelay
= jiffies_to_msecs(trans
->sackdelay
);
3522 /*draft-11 doesn't say what to return in spp_flags*/
3523 params
.spp_flags
= trans
->param_flags
;
3525 /* Fetch association values. */
3526 params
.spp_hbinterval
= jiffies_to_msecs(asoc
->hbinterval
);
3527 params
.spp_pathmtu
= asoc
->pathmtu
;
3528 params
.spp_pathmaxrxt
= asoc
->pathmaxrxt
;
3529 params
.spp_sackdelay
= jiffies_to_msecs(asoc
->sackdelay
);
3531 /*draft-11 doesn't say what to return in spp_flags*/
3532 params
.spp_flags
= asoc
->param_flags
;
3534 /* Fetch socket values. */
3535 params
.spp_hbinterval
= sp
->hbinterval
;
3536 params
.spp_pathmtu
= sp
->pathmtu
;
3537 params
.spp_sackdelay
= sp
->sackdelay
;
3538 params
.spp_pathmaxrxt
= sp
->pathmaxrxt
;
3540 /*draft-11 doesn't say what to return in spp_flags*/
3541 params
.spp_flags
= sp
->param_flags
;
3544 if (copy_to_user(optval
, ¶ms
, len
))
3547 if (put_user(len
, optlen
))
3553 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3555 * This options will get or set the delayed ack timer. The time is set
3556 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3557 * endpoints default delayed ack timer value. If the assoc_id field is
3558 * non-zero, then the set or get effects the specified association.
3560 * struct sctp_assoc_value {
3561 * sctp_assoc_t assoc_id;
3562 * uint32_t assoc_value;
3565 * assoc_id - This parameter, indicates which association the
3566 * user is preforming an action upon. Note that if
3567 * this field's value is zero then the endpoints
3568 * default value is changed (effecting future
3569 * associations only).
3571 * assoc_value - This parameter contains the number of milliseconds
3572 * that the user is requesting the delayed ACK timer
3573 * be set to. Note that this value is defined in
3574 * the standard to be between 200 and 500 milliseconds.
3576 * Note: a value of zero will leave the value alone,
3577 * but disable SACK delay. A non-zero value will also
3578 * enable SACK delay.
3580 static int sctp_getsockopt_delayed_ack_time(struct sock
*sk
, int len
,
3581 char __user
*optval
,
3584 struct sctp_assoc_value params
;
3585 struct sctp_association
*asoc
= NULL
;
3586 struct sctp_sock
*sp
= sctp_sk(sk
);
3588 if (len
!= sizeof(struct sctp_assoc_value
))
3591 if (copy_from_user(¶ms
, optval
, len
))
3594 /* Get association, if assoc_id != 0 and the socket is a one
3595 * to many style socket, and an association was not found, then
3596 * the id was invalid.
3598 asoc
= sctp_id2assoc(sk
, params
.assoc_id
);
3599 if (!asoc
&& params
.assoc_id
&& sctp_style(sk
, UDP
))
3603 /* Fetch association values. */
3604 if (asoc
->param_flags
& SPP_SACKDELAY_ENABLE
)
3605 params
.assoc_value
= jiffies_to_msecs(
3608 params
.assoc_value
= 0;
3610 /* Fetch socket values. */
3611 if (sp
->param_flags
& SPP_SACKDELAY_ENABLE
)
3612 params
.assoc_value
= sp
->sackdelay
;
3614 params
.assoc_value
= 0;
3617 if (copy_to_user(optval
, ¶ms
, len
))
3620 if (put_user(len
, optlen
))
3626 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3628 * Applications can specify protocol parameters for the default association
3629 * initialization. The option name argument to setsockopt() and getsockopt()
3632 * Setting initialization parameters is effective only on an unconnected
3633 * socket (for UDP-style sockets only future associations are effected
3634 * by the change). With TCP-style sockets, this option is inherited by
3635 * sockets derived from a listener socket.
3637 static int sctp_getsockopt_initmsg(struct sock
*sk
, int len
, char __user
*optval
, int __user
*optlen
)
3639 if (len
!= sizeof(struct sctp_initmsg
))
3641 if (copy_to_user(optval
, &sctp_sk(sk
)->initmsg
, len
))
3646 static int sctp_getsockopt_peer_addrs_num_old(struct sock
*sk
, int len
,
3647 char __user
*optval
,
3651 struct sctp_association
*asoc
;
3652 struct list_head
*pos
;
3655 if (len
!= sizeof(sctp_assoc_t
))
3658 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3661 /* For UDP-style sockets, id specifies the association to query. */
3662 asoc
= sctp_id2assoc(sk
, id
);
3666 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3674 * Old API for getting list of peer addresses. Does not work for 32-bit
3675 * programs running on a 64-bit kernel
3677 static int sctp_getsockopt_peer_addrs_old(struct sock
*sk
, int len
,
3678 char __user
*optval
,
3681 struct sctp_association
*asoc
;
3682 struct list_head
*pos
;
3684 struct sctp_getaddrs_old getaddrs
;
3685 struct sctp_transport
*from
;
3687 union sctp_addr temp
;
3688 struct sctp_sock
*sp
= sctp_sk(sk
);
3691 if (len
!= sizeof(struct sctp_getaddrs_old
))
3694 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
3697 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3699 /* For UDP-style sockets, id specifies the association to query. */
3700 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3704 to
= (void __user
*)getaddrs
.addrs
;
3705 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3706 from
= list_entry(pos
, struct sctp_transport
, transports
);
3707 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3708 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3709 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3710 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
3711 if (copy_to_user(to
, &temp
, addrlen
))
3715 if (cnt
>= getaddrs
.addr_num
) break;
3717 getaddrs
.addr_num
= cnt
;
3718 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
3724 static int sctp_getsockopt_peer_addrs(struct sock
*sk
, int len
,
3725 char __user
*optval
, int __user
*optlen
)
3727 struct sctp_association
*asoc
;
3728 struct list_head
*pos
;
3730 struct sctp_getaddrs getaddrs
;
3731 struct sctp_transport
*from
;
3733 union sctp_addr temp
;
3734 struct sctp_sock
*sp
= sctp_sk(sk
);
3739 if (len
< sizeof(struct sctp_getaddrs
))
3742 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
3745 /* For UDP-style sockets, id specifies the association to query. */
3746 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3750 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
3751 space_left
= len
- sizeof(struct sctp_getaddrs
) -
3752 offsetof(struct sctp_getaddrs
,addrs
);
3754 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3755 from
= list_entry(pos
, struct sctp_transport
, transports
);
3756 memcpy(&temp
, &from
->ipaddr
, sizeof(temp
));
3757 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3758 addrlen
= sctp_get_af_specific(sk
->sk_family
)->sockaddr_len
;
3759 if(space_left
< addrlen
)
3761 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
3762 if (copy_to_user(to
, &temp
, addrlen
))
3766 space_left
-= addrlen
;
3769 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
3771 bytes_copied
= ((char __user
*)to
) - optval
;
3772 if (put_user(bytes_copied
, optlen
))
3778 static int sctp_getsockopt_local_addrs_num_old(struct sock
*sk
, int len
,
3779 char __user
*optval
,
3783 struct sctp_bind_addr
*bp
;
3784 struct sctp_association
*asoc
;
3785 struct list_head
*pos
;
3786 struct sctp_sockaddr_entry
*addr
;
3787 rwlock_t
*addr_lock
;
3788 unsigned long flags
;
3791 if (len
!= sizeof(sctp_assoc_t
))
3794 if (copy_from_user(&id
, optval
, sizeof(sctp_assoc_t
)))
3798 * For UDP-style sockets, id specifies the association to query.
3799 * If the id field is set to the value '0' then the locally bound
3800 * addresses are returned without regard to any particular
3804 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
3805 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
3807 asoc
= sctp_id2assoc(sk
, id
);
3810 bp
= &asoc
->base
.bind_addr
;
3811 addr_lock
= &asoc
->base
.addr_lock
;
3814 sctp_read_lock(addr_lock
);
3816 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3817 * addresses from the global local address list.
3819 if (sctp_list_single_entry(&bp
->address_list
)) {
3820 addr
= list_entry(bp
->address_list
.next
,
3821 struct sctp_sockaddr_entry
, list
);
3822 if (sctp_is_any(&addr
->a
)) {
3823 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3824 list_for_each(pos
, &sctp_local_addr_list
) {
3825 addr
= list_entry(pos
,
3826 struct sctp_sockaddr_entry
,
3828 if ((PF_INET
== sk
->sk_family
) &&
3829 (AF_INET6
== addr
->a
.sa
.sa_family
))
3833 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3841 list_for_each(pos
, &bp
->address_list
) {
3846 sctp_read_unlock(addr_lock
);
3850 /* Helper function that copies local addresses to user and returns the number
3851 * of addresses copied.
3853 static int sctp_copy_laddrs_to_user_old(struct sock
*sk
, __u16 port
, int max_addrs
,
3856 struct list_head
*pos
;
3857 struct sctp_sockaddr_entry
*addr
;
3858 unsigned long flags
;
3859 union sctp_addr temp
;
3863 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3864 list_for_each(pos
, &sctp_local_addr_list
) {
3865 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3866 if ((PF_INET
== sk
->sk_family
) &&
3867 (AF_INET6
== addr
->a
.sa
.sa_family
))
3869 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3870 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3872 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3873 temp
.v4
.sin_port
= htons(port
);
3874 if (copy_to_user(to
, &temp
, addrlen
)) {
3875 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3881 if (cnt
>= max_addrs
) break;
3883 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
, flags
);
3888 static int sctp_copy_laddrs_to_user(struct sock
*sk
, __u16 port
,
3889 void __user
**to
, size_t space_left
)
3891 struct list_head
*pos
;
3892 struct sctp_sockaddr_entry
*addr
;
3893 unsigned long flags
;
3894 union sctp_addr temp
;
3898 sctp_spin_lock_irqsave(&sctp_local_addr_lock
, flags
);
3899 list_for_each(pos
, &sctp_local_addr_list
) {
3900 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3901 if ((PF_INET
== sk
->sk_family
) &&
3902 (AF_INET6
== addr
->a
.sa
.sa_family
))
3904 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3905 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sctp_sk(sk
),
3907 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3908 if(space_left
<addrlen
)
3910 temp
.v4
.sin_port
= htons(port
);
3911 if (copy_to_user(*to
, &temp
, addrlen
)) {
3912 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
,
3918 space_left
-= addrlen
;
3920 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock
, flags
);
3925 /* Old API for getting list of local addresses. Does not work for 32-bit
3926 * programs running on a 64-bit kernel
3928 static int sctp_getsockopt_local_addrs_old(struct sock
*sk
, int len
,
3929 char __user
*optval
, int __user
*optlen
)
3931 struct sctp_bind_addr
*bp
;
3932 struct sctp_association
*asoc
;
3933 struct list_head
*pos
;
3935 struct sctp_getaddrs_old getaddrs
;
3936 struct sctp_sockaddr_entry
*addr
;
3938 union sctp_addr temp
;
3939 struct sctp_sock
*sp
= sctp_sk(sk
);
3941 rwlock_t
*addr_lock
;
3944 if (len
!= sizeof(struct sctp_getaddrs_old
))
3947 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs_old
)))
3950 if (getaddrs
.addr_num
<= 0) return -EINVAL
;
3952 * For UDP-style sockets, id specifies the association to query.
3953 * If the id field is set to the value '0' then the locally bound
3954 * addresses are returned without regard to any particular
3957 if (0 == getaddrs
.assoc_id
) {
3958 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
3959 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
3961 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
3964 bp
= &asoc
->base
.bind_addr
;
3965 addr_lock
= &asoc
->base
.addr_lock
;
3968 to
= getaddrs
.addrs
;
3970 sctp_read_lock(addr_lock
);
3972 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3973 * addresses from the global local address list.
3975 if (sctp_list_single_entry(&bp
->address_list
)) {
3976 addr
= list_entry(bp
->address_list
.next
,
3977 struct sctp_sockaddr_entry
, list
);
3978 if (sctp_is_any(&addr
->a
)) {
3979 cnt
= sctp_copy_laddrs_to_user_old(sk
, bp
->port
,
3990 list_for_each(pos
, &bp
->address_list
) {
3991 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
3992 memcpy(&temp
, &addr
->a
, sizeof(temp
));
3993 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
3994 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
3995 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
3996 if (copy_to_user(to
, &temp
, addrlen
)) {
4002 if (cnt
>= getaddrs
.addr_num
) break;
4006 getaddrs
.addr_num
= cnt
;
4007 if (copy_to_user(optval
, &getaddrs
, sizeof(struct sctp_getaddrs_old
)))
4011 sctp_read_unlock(addr_lock
);
4015 static int sctp_getsockopt_local_addrs(struct sock
*sk
, int len
,
4016 char __user
*optval
, int __user
*optlen
)
4018 struct sctp_bind_addr
*bp
;
4019 struct sctp_association
*asoc
;
4020 struct list_head
*pos
;
4022 struct sctp_getaddrs getaddrs
;
4023 struct sctp_sockaddr_entry
*addr
;
4025 union sctp_addr temp
;
4026 struct sctp_sock
*sp
= sctp_sk(sk
);
4028 rwlock_t
*addr_lock
;
4033 if (len
<= sizeof(struct sctp_getaddrs
))
4036 if (copy_from_user(&getaddrs
, optval
, sizeof(struct sctp_getaddrs
)))
4040 * For UDP-style sockets, id specifies the association to query.
4041 * If the id field is set to the value '0' then the locally bound
4042 * addresses are returned without regard to any particular
4045 if (0 == getaddrs
.assoc_id
) {
4046 bp
= &sctp_sk(sk
)->ep
->base
.bind_addr
;
4047 addr_lock
= &sctp_sk(sk
)->ep
->base
.addr_lock
;
4049 asoc
= sctp_id2assoc(sk
, getaddrs
.assoc_id
);
4052 bp
= &asoc
->base
.bind_addr
;
4053 addr_lock
= &asoc
->base
.addr_lock
;
4056 to
= optval
+ offsetof(struct sctp_getaddrs
,addrs
);
4057 space_left
= len
- sizeof(struct sctp_getaddrs
) -
4058 offsetof(struct sctp_getaddrs
,addrs
);
4060 sctp_read_lock(addr_lock
);
4062 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4063 * addresses from the global local address list.
4065 if (sctp_list_single_entry(&bp
->address_list
)) {
4066 addr
= list_entry(bp
->address_list
.next
,
4067 struct sctp_sockaddr_entry
, list
);
4068 if (sctp_is_any(&addr
->a
)) {
4069 cnt
= sctp_copy_laddrs_to_user(sk
, bp
->port
,
4079 list_for_each(pos
, &bp
->address_list
) {
4080 addr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
4081 memcpy(&temp
, &addr
->a
, sizeof(temp
));
4082 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
, &temp
);
4083 addrlen
= sctp_get_af_specific(temp
.sa
.sa_family
)->sockaddr_len
;
4084 if(space_left
< addrlen
)
4085 return -ENOMEM
; /*fixme: right error?*/
4086 temp
.v4
.sin_port
= htons(temp
.v4
.sin_port
);
4087 if (copy_to_user(to
, &temp
, addrlen
)) {
4093 space_left
-= addrlen
;
4097 if (put_user(cnt
, &((struct sctp_getaddrs __user
*)optval
)->addr_num
))
4099 bytes_copied
= ((char __user
*)to
) - optval
;
4100 if (put_user(bytes_copied
, optlen
))
4104 sctp_read_unlock(addr_lock
);
4108 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4110 * Requests that the local SCTP stack use the enclosed peer address as
4111 * the association primary. The enclosed address must be one of the
4112 * association peer's addresses.
4114 static int sctp_getsockopt_primary_addr(struct sock
*sk
, int len
,
4115 char __user
*optval
, int __user
*optlen
)
4117 struct sctp_prim prim
;
4118 struct sctp_association
*asoc
;
4119 struct sctp_sock
*sp
= sctp_sk(sk
);
4121 if (len
!= sizeof(struct sctp_prim
))
4124 if (copy_from_user(&prim
, optval
, sizeof(struct sctp_prim
)))
4127 asoc
= sctp_id2assoc(sk
, prim
.ssp_assoc_id
);
4131 if (!asoc
->peer
.primary_path
)
4134 asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
=
4135 htons(asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
);
4136 memcpy(&prim
.ssp_addr
, &asoc
->peer
.primary_path
->ipaddr
,
4137 sizeof(union sctp_addr
));
4138 asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
=
4139 ntohs(asoc
->peer
.primary_path
->ipaddr
.v4
.sin_port
);
4141 sctp_get_pf_specific(sk
->sk_family
)->addr_v4map(sp
,
4142 (union sctp_addr
*)&prim
.ssp_addr
);
4144 if (copy_to_user(optval
, &prim
, sizeof(struct sctp_prim
)))
4151 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4153 * Requests that the local endpoint set the specified Adaption Layer
4154 * Indication parameter for all future INIT and INIT-ACK exchanges.
4156 static int sctp_getsockopt_adaption_layer(struct sock
*sk
, int len
,
4157 char __user
*optval
, int __user
*optlen
)
4159 struct sctp_setadaption adaption
;
4161 if (len
!= sizeof(struct sctp_setadaption
))
4164 adaption
.ssb_adaption_ind
= sctp_sk(sk
)->adaption_ind
;
4165 if (copy_to_user(optval
, &adaption
, len
))
4173 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4175 * Applications that wish to use the sendto() system call may wish to
4176 * specify a default set of parameters that would normally be supplied
4177 * through the inclusion of ancillary data. This socket option allows
4178 * such an application to set the default sctp_sndrcvinfo structure.
4181 * The application that wishes to use this socket option simply passes
4182 * in to this call the sctp_sndrcvinfo structure defined in Section
4183 * 5.2.2) The input parameters accepted by this call include
4184 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4185 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4186 * to this call if the caller is using the UDP model.
4188 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4190 static int sctp_getsockopt_default_send_param(struct sock
*sk
,
4191 int len
, char __user
*optval
,
4194 struct sctp_sndrcvinfo info
;
4195 struct sctp_association
*asoc
;
4196 struct sctp_sock
*sp
= sctp_sk(sk
);
4198 if (len
!= sizeof(struct sctp_sndrcvinfo
))
4200 if (copy_from_user(&info
, optval
, sizeof(struct sctp_sndrcvinfo
)))
4203 asoc
= sctp_id2assoc(sk
, info
.sinfo_assoc_id
);
4204 if (!asoc
&& info
.sinfo_assoc_id
&& sctp_style(sk
, UDP
))
4208 info
.sinfo_stream
= asoc
->default_stream
;
4209 info
.sinfo_flags
= asoc
->default_flags
;
4210 info
.sinfo_ppid
= asoc
->default_ppid
;
4211 info
.sinfo_context
= asoc
->default_context
;
4212 info
.sinfo_timetolive
= asoc
->default_timetolive
;
4214 info
.sinfo_stream
= sp
->default_stream
;
4215 info
.sinfo_flags
= sp
->default_flags
;
4216 info
.sinfo_ppid
= sp
->default_ppid
;
4217 info
.sinfo_context
= sp
->default_context
;
4218 info
.sinfo_timetolive
= sp
->default_timetolive
;
4221 if (copy_to_user(optval
, &info
, sizeof(struct sctp_sndrcvinfo
)))
4229 * 7.1.5 SCTP_NODELAY
4231 * Turn on/off any Nagle-like algorithm. This means that packets are
4232 * generally sent as soon as possible and no unnecessary delays are
4233 * introduced, at the cost of more packets in the network. Expects an
4234 * integer boolean flag.
4237 static int sctp_getsockopt_nodelay(struct sock
*sk
, int len
,
4238 char __user
*optval
, int __user
*optlen
)
4242 if (len
< sizeof(int))
4246 val
= (sctp_sk(sk
)->nodelay
== 1);
4247 if (put_user(len
, optlen
))
4249 if (copy_to_user(optval
, &val
, len
))
4256 * 7.1.1 SCTP_RTOINFO
4258 * The protocol parameters used to initialize and bound retransmission
4259 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4260 * and modify these parameters.
4261 * All parameters are time values, in milliseconds. A value of 0, when
4262 * modifying the parameters, indicates that the current value should not
4266 static int sctp_getsockopt_rtoinfo(struct sock
*sk
, int len
,
4267 char __user
*optval
,
4268 int __user
*optlen
) {
4269 struct sctp_rtoinfo rtoinfo
;
4270 struct sctp_association
*asoc
;
4272 if (len
!= sizeof (struct sctp_rtoinfo
))
4275 if (copy_from_user(&rtoinfo
, optval
, sizeof (struct sctp_rtoinfo
)))
4278 asoc
= sctp_id2assoc(sk
, rtoinfo
.srto_assoc_id
);
4280 if (!asoc
&& rtoinfo
.srto_assoc_id
&& sctp_style(sk
, UDP
))
4283 /* Values corresponding to the specific association. */
4285 rtoinfo
.srto_initial
= jiffies_to_msecs(asoc
->rto_initial
);
4286 rtoinfo
.srto_max
= jiffies_to_msecs(asoc
->rto_max
);
4287 rtoinfo
.srto_min
= jiffies_to_msecs(asoc
->rto_min
);
4289 /* Values corresponding to the endpoint. */
4290 struct sctp_sock
*sp
= sctp_sk(sk
);
4292 rtoinfo
.srto_initial
= sp
->rtoinfo
.srto_initial
;
4293 rtoinfo
.srto_max
= sp
->rtoinfo
.srto_max
;
4294 rtoinfo
.srto_min
= sp
->rtoinfo
.srto_min
;
4297 if (put_user(len
, optlen
))
4300 if (copy_to_user(optval
, &rtoinfo
, len
))
4308 * 7.1.2 SCTP_ASSOCINFO
4310 * This option is used to tune the the maximum retransmission attempts
4311 * of the association.
4312 * Returns an error if the new association retransmission value is
4313 * greater than the sum of the retransmission value of the peer.
4314 * See [SCTP] for more information.
4317 static int sctp_getsockopt_associnfo(struct sock
*sk
, int len
,
4318 char __user
*optval
,
4322 struct sctp_assocparams assocparams
;
4323 struct sctp_association
*asoc
;
4324 struct list_head
*pos
;
4327 if (len
!= sizeof (struct sctp_assocparams
))
4330 if (copy_from_user(&assocparams
, optval
,
4331 sizeof (struct sctp_assocparams
)))
4334 asoc
= sctp_id2assoc(sk
, assocparams
.sasoc_assoc_id
);
4336 if (!asoc
&& assocparams
.sasoc_assoc_id
&& sctp_style(sk
, UDP
))
4339 /* Values correspoinding to the specific association */
4341 assocparams
.sasoc_asocmaxrxt
= asoc
->max_retrans
;
4342 assocparams
.sasoc_peer_rwnd
= asoc
->peer
.rwnd
;
4343 assocparams
.sasoc_local_rwnd
= asoc
->a_rwnd
;
4344 assocparams
.sasoc_cookie_life
= (asoc
->cookie_life
.tv_sec
4346 (asoc
->cookie_life
.tv_usec
4349 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
4353 assocparams
.sasoc_number_peer_destinations
= cnt
;
4355 /* Values corresponding to the endpoint */
4356 struct sctp_sock
*sp
= sctp_sk(sk
);
4358 assocparams
.sasoc_asocmaxrxt
= sp
->assocparams
.sasoc_asocmaxrxt
;
4359 assocparams
.sasoc_peer_rwnd
= sp
->assocparams
.sasoc_peer_rwnd
;
4360 assocparams
.sasoc_local_rwnd
= sp
->assocparams
.sasoc_local_rwnd
;
4361 assocparams
.sasoc_cookie_life
=
4362 sp
->assocparams
.sasoc_cookie_life
;
4363 assocparams
.sasoc_number_peer_destinations
=
4365 sasoc_number_peer_destinations
;
4368 if (put_user(len
, optlen
))
4371 if (copy_to_user(optval
, &assocparams
, len
))
4378 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4380 * This socket option is a boolean flag which turns on or off mapped V4
4381 * addresses. If this option is turned on and the socket is type
4382 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4383 * If this option is turned off, then no mapping will be done of V4
4384 * addresses and a user will receive both PF_INET6 and PF_INET type
4385 * addresses on the socket.
4387 static int sctp_getsockopt_mappedv4(struct sock
*sk
, int len
,
4388 char __user
*optval
, int __user
*optlen
)
4391 struct sctp_sock
*sp
= sctp_sk(sk
);
4393 if (len
< sizeof(int))
4398 if (put_user(len
, optlen
))
4400 if (copy_to_user(optval
, &val
, len
))
4407 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4409 * This socket option specifies the maximum size to put in any outgoing
4410 * SCTP chunk. If a message is larger than this size it will be
4411 * fragmented by SCTP into the specified size. Note that the underlying
4412 * SCTP implementation may fragment into smaller sized chunks when the
4413 * PMTU of the underlying association is smaller than the value set by
4416 static int sctp_getsockopt_maxseg(struct sock
*sk
, int len
,
4417 char __user
*optval
, int __user
*optlen
)
4421 if (len
< sizeof(int))
4426 val
= sctp_sk(sk
)->user_frag
;
4427 if (put_user(len
, optlen
))
4429 if (copy_to_user(optval
, &val
, len
))
4435 SCTP_STATIC
int sctp_getsockopt(struct sock
*sk
, int level
, int optname
,
4436 char __user
*optval
, int __user
*optlen
)
4441 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4444 /* I can hardly begin to describe how wrong this is. This is
4445 * so broken as to be worse than useless. The API draft
4446 * REALLY is NOT helpful here... I am not convinced that the
4447 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4448 * are at all well-founded.
4450 if (level
!= SOL_SCTP
) {
4451 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4453 retval
= af
->getsockopt(sk
, level
, optname
, optval
, optlen
);
4457 if (get_user(len
, optlen
))
4464 retval
= sctp_getsockopt_sctp_status(sk
, len
, optval
, optlen
);
4466 case SCTP_DISABLE_FRAGMENTS
:
4467 retval
= sctp_getsockopt_disable_fragments(sk
, len
, optval
,
4471 retval
= sctp_getsockopt_events(sk
, len
, optval
, optlen
);
4473 case SCTP_AUTOCLOSE
:
4474 retval
= sctp_getsockopt_autoclose(sk
, len
, optval
, optlen
);
4476 case SCTP_SOCKOPT_PEELOFF
:
4477 retval
= sctp_getsockopt_peeloff(sk
, len
, optval
, optlen
);
4479 case SCTP_PEER_ADDR_PARAMS
:
4480 retval
= sctp_getsockopt_peer_addr_params(sk
, len
, optval
,
4483 case SCTP_DELAYED_ACK_TIME
:
4484 retval
= sctp_getsockopt_delayed_ack_time(sk
, len
, optval
,
4488 retval
= sctp_getsockopt_initmsg(sk
, len
, optval
, optlen
);
4490 case SCTP_GET_PEER_ADDRS_NUM_OLD
:
4491 retval
= sctp_getsockopt_peer_addrs_num_old(sk
, len
, optval
,
4494 case SCTP_GET_LOCAL_ADDRS_NUM_OLD
:
4495 retval
= sctp_getsockopt_local_addrs_num_old(sk
, len
, optval
,
4498 case SCTP_GET_PEER_ADDRS_OLD
:
4499 retval
= sctp_getsockopt_peer_addrs_old(sk
, len
, optval
,
4502 case SCTP_GET_LOCAL_ADDRS_OLD
:
4503 retval
= sctp_getsockopt_local_addrs_old(sk
, len
, optval
,
4506 case SCTP_GET_PEER_ADDRS
:
4507 retval
= sctp_getsockopt_peer_addrs(sk
, len
, optval
,
4510 case SCTP_GET_LOCAL_ADDRS
:
4511 retval
= sctp_getsockopt_local_addrs(sk
, len
, optval
,
4514 case SCTP_DEFAULT_SEND_PARAM
:
4515 retval
= sctp_getsockopt_default_send_param(sk
, len
,
4518 case SCTP_PRIMARY_ADDR
:
4519 retval
= sctp_getsockopt_primary_addr(sk
, len
, optval
, optlen
);
4522 retval
= sctp_getsockopt_nodelay(sk
, len
, optval
, optlen
);
4525 retval
= sctp_getsockopt_rtoinfo(sk
, len
, optval
, optlen
);
4527 case SCTP_ASSOCINFO
:
4528 retval
= sctp_getsockopt_associnfo(sk
, len
, optval
, optlen
);
4530 case SCTP_I_WANT_MAPPED_V4_ADDR
:
4531 retval
= sctp_getsockopt_mappedv4(sk
, len
, optval
, optlen
);
4534 retval
= sctp_getsockopt_maxseg(sk
, len
, optval
, optlen
);
4536 case SCTP_GET_PEER_ADDR_INFO
:
4537 retval
= sctp_getsockopt_peer_addr_info(sk
, len
, optval
,
4540 case SCTP_ADAPTION_LAYER
:
4541 retval
= sctp_getsockopt_adaption_layer(sk
, len
, optval
,
4545 retval
= -ENOPROTOOPT
;
4549 sctp_release_sock(sk
);
4553 static void sctp_hash(struct sock
*sk
)
4558 static void sctp_unhash(struct sock
*sk
)
4563 /* Check if port is acceptable. Possibly find first available port.
4565 * The port hash table (contained in the 'global' SCTP protocol storage
4566 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4567 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4568 * list (the list number is the port number hashed out, so as you
4569 * would expect from a hash function, all the ports in a given list have
4570 * such a number that hashes out to the same list number; you were
4571 * expecting that, right?); so each list has a set of ports, with a
4572 * link to the socket (struct sock) that uses it, the port number and
4573 * a fastreuse flag (FIXME: NPI ipg).
4575 static struct sctp_bind_bucket
*sctp_bucket_create(
4576 struct sctp_bind_hashbucket
*head
, unsigned short snum
);
4578 static long sctp_get_port_local(struct sock
*sk
, union sctp_addr
*addr
)
4580 struct sctp_bind_hashbucket
*head
; /* hash list */
4581 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
4582 unsigned short snum
;
4585 /* NOTE: Remember to put this back to net order. */
4586 addr
->v4
.sin_port
= ntohs(addr
->v4
.sin_port
);
4587 snum
= addr
->v4
.sin_port
;
4589 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum
);
4590 sctp_local_bh_disable();
4593 /* Search for an available port.
4595 * 'sctp_port_rover' was the last port assigned, so
4596 * we start to search from 'sctp_port_rover +
4597 * 1'. What we do is first check if port 'rover' is
4598 * already in the hash table; if not, we use that; if
4599 * it is, we try next.
4601 int low
= sysctl_local_port_range
[0];
4602 int high
= sysctl_local_port_range
[1];
4603 int remaining
= (high
- low
) + 1;
4607 sctp_spin_lock(&sctp_port_alloc_lock
);
4608 rover
= sctp_port_rover
;
4611 if ((rover
< low
) || (rover
> high
))
4613 index
= sctp_phashfn(rover
);
4614 head
= &sctp_port_hashtable
[index
];
4615 sctp_spin_lock(&head
->lock
);
4616 for (pp
= head
->chain
; pp
; pp
= pp
->next
)
4617 if (pp
->port
== rover
)
4621 sctp_spin_unlock(&head
->lock
);
4622 } while (--remaining
> 0);
4623 sctp_port_rover
= rover
;
4624 sctp_spin_unlock(&sctp_port_alloc_lock
);
4626 /* Exhausted local port range during search? */
4631 /* OK, here is the one we will use. HEAD (the port
4632 * hash table list entry) is non-NULL and we hold it's
4637 /* We are given an specific port number; we verify
4638 * that it is not being used. If it is used, we will
4639 * exahust the search in the hash list corresponding
4640 * to the port number (snum) - we detect that with the
4641 * port iterator, pp being NULL.
4643 head
= &sctp_port_hashtable
[sctp_phashfn(snum
)];
4644 sctp_spin_lock(&head
->lock
);
4645 for (pp
= head
->chain
; pp
; pp
= pp
->next
) {
4646 if (pp
->port
== snum
)
4653 if (!hlist_empty(&pp
->owner
)) {
4654 /* We had a port hash table hit - there is an
4655 * available port (pp != NULL) and it is being
4656 * used by other socket (pp->owner not empty); that other
4657 * socket is going to be sk2.
4659 int reuse
= sk
->sk_reuse
;
4661 struct hlist_node
*node
;
4663 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4664 if (pp
->fastreuse
&& sk
->sk_reuse
)
4667 /* Run through the list of sockets bound to the port
4668 * (pp->port) [via the pointers bind_next and
4669 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4670 * we get the endpoint they describe and run through
4671 * the endpoint's list of IP (v4 or v6) addresses,
4672 * comparing each of the addresses with the address of
4673 * the socket sk. If we find a match, then that means
4674 * that this port/socket (sk) combination are already
4677 sk_for_each_bound(sk2
, node
, &pp
->owner
) {
4678 struct sctp_endpoint
*ep2
;
4679 ep2
= sctp_sk(sk2
)->ep
;
4681 if (reuse
&& sk2
->sk_reuse
)
4684 if (sctp_bind_addr_match(&ep2
->base
.bind_addr
, addr
,
4690 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4693 /* If there was a hash table miss, create a new port. */
4695 if (!pp
&& !(pp
= sctp_bucket_create(head
, snum
)))
4698 /* In either case (hit or miss), make sure fastreuse is 1 only
4699 * if sk->sk_reuse is too (that is, if the caller requested
4700 * SO_REUSEADDR on this socket -sk-).
4702 if (hlist_empty(&pp
->owner
))
4703 pp
->fastreuse
= sk
->sk_reuse
? 1 : 0;
4704 else if (pp
->fastreuse
&& !sk
->sk_reuse
)
4707 /* We are set, so fill up all the data in the hash table
4708 * entry, tie the socket list information with the rest of the
4709 * sockets FIXME: Blurry, NPI (ipg).
4712 inet_sk(sk
)->num
= snum
;
4713 if (!sctp_sk(sk
)->bind_hash
) {
4714 sk_add_bind_node(sk
, &pp
->owner
);
4715 sctp_sk(sk
)->bind_hash
= pp
;
4720 sctp_spin_unlock(&head
->lock
);
4723 sctp_local_bh_enable();
4724 addr
->v4
.sin_port
= htons(addr
->v4
.sin_port
);
4728 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4729 * port is requested.
4731 static int sctp_get_port(struct sock
*sk
, unsigned short snum
)
4734 union sctp_addr addr
;
4735 struct sctp_af
*af
= sctp_sk(sk
)->pf
->af
;
4737 /* Set up a dummy address struct from the sk. */
4738 af
->from_sk(&addr
, sk
);
4739 addr
.v4
.sin_port
= htons(snum
);
4741 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4742 ret
= sctp_get_port_local(sk
, &addr
);
4744 return (ret
? 1 : 0);
4748 * 3.1.3 listen() - UDP Style Syntax
4750 * By default, new associations are not accepted for UDP style sockets.
4751 * An application uses listen() to mark a socket as being able to
4752 * accept new associations.
4754 SCTP_STATIC
int sctp_seqpacket_listen(struct sock
*sk
, int backlog
)
4756 struct sctp_sock
*sp
= sctp_sk(sk
);
4757 struct sctp_endpoint
*ep
= sp
->ep
;
4759 /* Only UDP style sockets that are not peeled off are allowed to
4762 if (!sctp_style(sk
, UDP
))
4765 /* If backlog is zero, disable listening. */
4767 if (sctp_sstate(sk
, CLOSED
))
4770 sctp_unhash_endpoint(ep
);
4771 sk
->sk_state
= SCTP_SS_CLOSED
;
4774 /* Return if we are already listening. */
4775 if (sctp_sstate(sk
, LISTENING
))
4779 * If a bind() or sctp_bindx() is not called prior to a listen()
4780 * call that allows new associations to be accepted, the system
4781 * picks an ephemeral port and will choose an address set equivalent
4782 * to binding with a wildcard address.
4784 * This is not currently spelled out in the SCTP sockets
4785 * extensions draft, but follows the practice as seen in TCP
4788 if (!ep
->base
.bind_addr
.port
) {
4789 if (sctp_autobind(sk
))
4792 sk
->sk_state
= SCTP_SS_LISTENING
;
4793 sctp_hash_endpoint(ep
);
4798 * 4.1.3 listen() - TCP Style Syntax
4800 * Applications uses listen() to ready the SCTP endpoint for accepting
4801 * inbound associations.
4803 SCTP_STATIC
int sctp_stream_listen(struct sock
*sk
, int backlog
)
4805 struct sctp_sock
*sp
= sctp_sk(sk
);
4806 struct sctp_endpoint
*ep
= sp
->ep
;
4808 /* If backlog is zero, disable listening. */
4810 if (sctp_sstate(sk
, CLOSED
))
4813 sctp_unhash_endpoint(ep
);
4814 sk
->sk_state
= SCTP_SS_CLOSED
;
4817 if (sctp_sstate(sk
, LISTENING
))
4821 * If a bind() or sctp_bindx() is not called prior to a listen()
4822 * call that allows new associations to be accepted, the system
4823 * picks an ephemeral port and will choose an address set equivalent
4824 * to binding with a wildcard address.
4826 * This is not currently spelled out in the SCTP sockets
4827 * extensions draft, but follows the practice as seen in TCP
4830 if (!ep
->base
.bind_addr
.port
) {
4831 if (sctp_autobind(sk
))
4834 sk
->sk_state
= SCTP_SS_LISTENING
;
4835 sk
->sk_max_ack_backlog
= backlog
;
4836 sctp_hash_endpoint(ep
);
4841 * Move a socket to LISTENING state.
4843 int sctp_inet_listen(struct socket
*sock
, int backlog
)
4845 struct sock
*sk
= sock
->sk
;
4846 struct crypto_tfm
*tfm
=NULL
;
4849 if (unlikely(backlog
< 0))
4854 if (sock
->state
!= SS_UNCONNECTED
)
4857 /* Allocate HMAC for generating cookie. */
4858 if (sctp_hmac_alg
) {
4859 tfm
= sctp_crypto_alloc_tfm(sctp_hmac_alg
, 0);
4866 switch (sock
->type
) {
4867 case SOCK_SEQPACKET
:
4868 err
= sctp_seqpacket_listen(sk
, backlog
);
4871 err
= sctp_stream_listen(sk
, backlog
);
4879 /* Store away the transform reference. */
4880 sctp_sk(sk
)->hmac
= tfm
;
4882 sctp_release_sock(sk
);
4885 sctp_crypto_free_tfm(tfm
);
4890 * This function is done by modeling the current datagram_poll() and the
4891 * tcp_poll(). Note that, based on these implementations, we don't
4892 * lock the socket in this function, even though it seems that,
4893 * ideally, locking or some other mechanisms can be used to ensure
4894 * the integrity of the counters (sndbuf and wmem_alloc) used
4895 * in this place. We assume that we don't need locks either until proven
4898 * Another thing to note is that we include the Async I/O support
4899 * here, again, by modeling the current TCP/UDP code. We don't have
4900 * a good way to test with it yet.
4902 unsigned int sctp_poll(struct file
*file
, struct socket
*sock
, poll_table
*wait
)
4904 struct sock
*sk
= sock
->sk
;
4905 struct sctp_sock
*sp
= sctp_sk(sk
);
4908 poll_wait(file
, sk
->sk_sleep
, wait
);
4910 /* A TCP-style listening socket becomes readable when the accept queue
4913 if (sctp_style(sk
, TCP
) && sctp_sstate(sk
, LISTENING
))
4914 return (!list_empty(&sp
->ep
->asocs
)) ?
4915 (POLLIN
| POLLRDNORM
) : 0;
4919 /* Is there any exceptional events? */
4920 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
4922 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
4924 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
4927 /* Is it readable? Reconsider this code with TCP-style support. */
4928 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
4929 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
4930 mask
|= POLLIN
| POLLRDNORM
;
4932 /* The association is either gone or not ready. */
4933 if (!sctp_style(sk
, UDP
) && sctp_sstate(sk
, CLOSED
))
4936 /* Is it writable? */
4937 if (sctp_writeable(sk
)) {
4938 mask
|= POLLOUT
| POLLWRNORM
;
4940 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
4942 * Since the socket is not locked, the buffer
4943 * might be made available after the writeable check and
4944 * before the bit is set. This could cause a lost I/O
4945 * signal. tcp_poll() has a race breaker for this race
4946 * condition. Based on their implementation, we put
4947 * in the following code to cover it as well.
4949 if (sctp_writeable(sk
))
4950 mask
|= POLLOUT
| POLLWRNORM
;
4955 /********************************************************************
4956 * 2nd Level Abstractions
4957 ********************************************************************/
4959 static struct sctp_bind_bucket
*sctp_bucket_create(
4960 struct sctp_bind_hashbucket
*head
, unsigned short snum
)
4962 struct sctp_bind_bucket
*pp
;
4964 pp
= kmem_cache_alloc(sctp_bucket_cachep
, SLAB_ATOMIC
);
4965 SCTP_DBG_OBJCNT_INC(bind_bucket
);
4969 INIT_HLIST_HEAD(&pp
->owner
);
4970 if ((pp
->next
= head
->chain
) != NULL
)
4971 pp
->next
->pprev
= &pp
->next
;
4973 pp
->pprev
= &head
->chain
;
4978 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4979 static void sctp_bucket_destroy(struct sctp_bind_bucket
*pp
)
4981 if (hlist_empty(&pp
->owner
)) {
4983 pp
->next
->pprev
= pp
->pprev
;
4984 *(pp
->pprev
) = pp
->next
;
4985 kmem_cache_free(sctp_bucket_cachep
, pp
);
4986 SCTP_DBG_OBJCNT_DEC(bind_bucket
);
4990 /* Release this socket's reference to a local port. */
4991 static inline void __sctp_put_port(struct sock
*sk
)
4993 struct sctp_bind_hashbucket
*head
=
4994 &sctp_port_hashtable
[sctp_phashfn(inet_sk(sk
)->num
)];
4995 struct sctp_bind_bucket
*pp
;
4997 sctp_spin_lock(&head
->lock
);
4998 pp
= sctp_sk(sk
)->bind_hash
;
4999 __sk_del_bind_node(sk
);
5000 sctp_sk(sk
)->bind_hash
= NULL
;
5001 inet_sk(sk
)->num
= 0;
5002 sctp_bucket_destroy(pp
);
5003 sctp_spin_unlock(&head
->lock
);
5006 void sctp_put_port(struct sock
*sk
)
5008 sctp_local_bh_disable();
5009 __sctp_put_port(sk
);
5010 sctp_local_bh_enable();
5014 * The system picks an ephemeral port and choose an address set equivalent
5015 * to binding with a wildcard address.
5016 * One of those addresses will be the primary address for the association.
5017 * This automatically enables the multihoming capability of SCTP.
5019 static int sctp_autobind(struct sock
*sk
)
5021 union sctp_addr autoaddr
;
5023 unsigned short port
;
5025 /* Initialize a local sockaddr structure to INADDR_ANY. */
5026 af
= sctp_sk(sk
)->pf
->af
;
5028 port
= htons(inet_sk(sk
)->num
);
5029 af
->inaddr_any(&autoaddr
, port
);
5031 return sctp_do_bind(sk
, &autoaddr
, af
->sockaddr_len
);
5034 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5037 * 4.2 The cmsghdr Structure *
5039 * When ancillary data is sent or received, any number of ancillary data
5040 * objects can be specified by the msg_control and msg_controllen members of
5041 * the msghdr structure, because each object is preceded by
5042 * a cmsghdr structure defining the object's length (the cmsg_len member).
5043 * Historically Berkeley-derived implementations have passed only one object
5044 * at a time, but this API allows multiple objects to be
5045 * passed in a single call to sendmsg() or recvmsg(). The following example
5046 * shows two ancillary data objects in a control buffer.
5048 * |<--------------------------- msg_controllen -------------------------->|
5051 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5053 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5056 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5058 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5061 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5062 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5064 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5066 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5073 SCTP_STATIC
int sctp_msghdr_parse(const struct msghdr
*msg
,
5074 sctp_cmsgs_t
*cmsgs
)
5076 struct cmsghdr
*cmsg
;
5078 for (cmsg
= CMSG_FIRSTHDR(msg
);
5080 cmsg
= CMSG_NXTHDR((struct msghdr
*)msg
, cmsg
)) {
5081 if (!CMSG_OK(msg
, cmsg
))
5084 /* Should we parse this header or ignore? */
5085 if (cmsg
->cmsg_level
!= IPPROTO_SCTP
)
5088 /* Strictly check lengths following example in SCM code. */
5089 switch (cmsg
->cmsg_type
) {
5091 /* SCTP Socket API Extension
5092 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5094 * This cmsghdr structure provides information for
5095 * initializing new SCTP associations with sendmsg().
5096 * The SCTP_INITMSG socket option uses this same data
5097 * structure. This structure is not used for
5100 * cmsg_level cmsg_type cmsg_data[]
5101 * ------------ ------------ ----------------------
5102 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5104 if (cmsg
->cmsg_len
!=
5105 CMSG_LEN(sizeof(struct sctp_initmsg
)))
5107 cmsgs
->init
= (struct sctp_initmsg
*)CMSG_DATA(cmsg
);
5111 /* SCTP Socket API Extension
5112 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5114 * This cmsghdr structure specifies SCTP options for
5115 * sendmsg() and describes SCTP header information
5116 * about a received message through recvmsg().
5118 * cmsg_level cmsg_type cmsg_data[]
5119 * ------------ ------------ ----------------------
5120 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5122 if (cmsg
->cmsg_len
!=
5123 CMSG_LEN(sizeof(struct sctp_sndrcvinfo
)))
5127 (struct sctp_sndrcvinfo
*)CMSG_DATA(cmsg
);
5129 /* Minimally, validate the sinfo_flags. */
5130 if (cmsgs
->info
->sinfo_flags
&
5131 ~(SCTP_UNORDERED
| SCTP_ADDR_OVER
|
5132 SCTP_ABORT
| SCTP_EOF
))
5144 * Wait for a packet..
5145 * Note: This function is the same function as in core/datagram.c
5146 * with a few modifications to make lksctp work.
5148 static int sctp_wait_for_packet(struct sock
* sk
, int *err
, long *timeo_p
)
5153 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5155 /* Socket errors? */
5156 error
= sock_error(sk
);
5160 if (!skb_queue_empty(&sk
->sk_receive_queue
))
5163 /* Socket shut down? */
5164 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5167 /* Sequenced packets can come disconnected. If so we report the
5172 /* Is there a good reason to think that we may receive some data? */
5173 if (list_empty(&sctp_sk(sk
)->ep
->asocs
) && !sctp_sstate(sk
, LISTENING
))
5176 /* Handle signals. */
5177 if (signal_pending(current
))
5180 /* Let another process have a go. Since we are going to sleep
5181 * anyway. Note: This may cause odd behaviors if the message
5182 * does not fit in the user's buffer, but this seems to be the
5183 * only way to honor MSG_DONTWAIT realistically.
5185 sctp_release_sock(sk
);
5186 *timeo_p
= schedule_timeout(*timeo_p
);
5190 finish_wait(sk
->sk_sleep
, &wait
);
5194 error
= sock_intr_errno(*timeo_p
);
5197 finish_wait(sk
->sk_sleep
, &wait
);
5202 /* Receive a datagram.
5203 * Note: This is pretty much the same routine as in core/datagram.c
5204 * with a few changes to make lksctp work.
5206 static struct sk_buff
*sctp_skb_recv_datagram(struct sock
*sk
, int flags
,
5207 int noblock
, int *err
)
5210 struct sk_buff
*skb
;
5213 timeo
= sock_rcvtimeo(sk
, noblock
);
5215 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5216 timeo
, MAX_SCHEDULE_TIMEOUT
);
5219 /* Again only user level code calls this function,
5220 * so nothing interrupt level
5221 * will suddenly eat the receive_queue.
5223 * Look at current nfs client by the way...
5224 * However, this function was corrent in any case. 8)
5226 if (flags
& MSG_PEEK
) {
5227 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
5228 skb
= skb_peek(&sk
->sk_receive_queue
);
5230 atomic_inc(&skb
->users
);
5231 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
5233 skb
= skb_dequeue(&sk
->sk_receive_queue
);
5239 /* Caller is allowed not to check sk->sk_err before calling. */
5240 error
= sock_error(sk
);
5244 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5247 /* User doesn't want to wait. */
5251 } while (sctp_wait_for_packet(sk
, err
, &timeo
) == 0);
5260 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5261 static void __sctp_write_space(struct sctp_association
*asoc
)
5263 struct sock
*sk
= asoc
->base
.sk
;
5264 struct socket
*sock
= sk
->sk_socket
;
5266 if ((sctp_wspace(asoc
) > 0) && sock
) {
5267 if (waitqueue_active(&asoc
->wait
))
5268 wake_up_interruptible(&asoc
->wait
);
5270 if (sctp_writeable(sk
)) {
5271 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
5272 wake_up_interruptible(sk
->sk_sleep
);
5274 /* Note that we try to include the Async I/O support
5275 * here by modeling from the current TCP/UDP code.
5276 * We have not tested with it yet.
5278 if (sock
->fasync_list
&&
5279 !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
5280 sock_wake_async(sock
, 2, POLL_OUT
);
5285 /* Do accounting for the sndbuf space.
5286 * Decrement the used sndbuf space of the corresponding association by the
5287 * data size which was just transmitted(freed).
5289 static void sctp_wfree(struct sk_buff
*skb
)
5291 struct sctp_association
*asoc
;
5292 struct sctp_chunk
*chunk
;
5295 /* Get the saved chunk pointer. */
5296 chunk
= *((struct sctp_chunk
**)(skb
->cb
));
5299 asoc
->sndbuf_used
-= SCTP_DATA_SNDSIZE(chunk
) +
5300 sizeof(struct sk_buff
) +
5301 sizeof(struct sctp_chunk
);
5303 atomic_sub(sizeof(struct sctp_chunk
), &sk
->sk_wmem_alloc
);
5306 __sctp_write_space(asoc
);
5308 sctp_association_put(asoc
);
5311 /* Helper function to wait for space in the sndbuf. */
5312 static int sctp_wait_for_sndbuf(struct sctp_association
*asoc
, long *timeo_p
,
5315 struct sock
*sk
= asoc
->base
.sk
;
5317 long current_timeo
= *timeo_p
;
5320 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5321 asoc
, (long)(*timeo_p
), msg_len
);
5323 /* Increment the association's refcnt. */
5324 sctp_association_hold(asoc
);
5326 /* Wait on the association specific sndbuf space. */
5328 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5329 TASK_INTERRUPTIBLE
);
5332 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5335 if (signal_pending(current
))
5336 goto do_interrupted
;
5337 if (msg_len
<= sctp_wspace(asoc
))
5340 /* Let another process have a go. Since we are going
5343 sctp_release_sock(sk
);
5344 current_timeo
= schedule_timeout(current_timeo
);
5345 BUG_ON(sk
!= asoc
->base
.sk
);
5348 *timeo_p
= current_timeo
;
5352 finish_wait(&asoc
->wait
, &wait
);
5354 /* Release the association's refcnt. */
5355 sctp_association_put(asoc
);
5364 err
= sock_intr_errno(*timeo_p
);
5372 /* If socket sndbuf has changed, wake up all per association waiters. */
5373 void sctp_write_space(struct sock
*sk
)
5375 struct sctp_association
*asoc
;
5376 struct list_head
*pos
;
5378 /* Wake up the tasks in each wait queue. */
5379 list_for_each(pos
, &((sctp_sk(sk
))->ep
->asocs
)) {
5380 asoc
= list_entry(pos
, struct sctp_association
, asocs
);
5381 __sctp_write_space(asoc
);
5385 /* Is there any sndbuf space available on the socket?
5387 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5388 * associations on the same socket. For a UDP-style socket with
5389 * multiple associations, it is possible for it to be "unwriteable"
5390 * prematurely. I assume that this is acceptable because
5391 * a premature "unwriteable" is better than an accidental "writeable" which
5392 * would cause an unwanted block under certain circumstances. For the 1-1
5393 * UDP-style sockets or TCP-style sockets, this code should work.
5396 static int sctp_writeable(struct sock
*sk
)
5400 amt
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
5406 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5407 * returns immediately with EINPROGRESS.
5409 static int sctp_wait_for_connect(struct sctp_association
*asoc
, long *timeo_p
)
5411 struct sock
*sk
= asoc
->base
.sk
;
5413 long current_timeo
= *timeo_p
;
5416 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__
, asoc
,
5419 /* Increment the association's refcnt. */
5420 sctp_association_hold(asoc
);
5423 prepare_to_wait_exclusive(&asoc
->wait
, &wait
,
5424 TASK_INTERRUPTIBLE
);
5427 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
5429 if (sk
->sk_err
|| asoc
->state
>= SCTP_STATE_SHUTDOWN_PENDING
||
5432 if (signal_pending(current
))
5433 goto do_interrupted
;
5435 if (sctp_state(asoc
, ESTABLISHED
))
5438 /* Let another process have a go. Since we are going
5441 sctp_release_sock(sk
);
5442 current_timeo
= schedule_timeout(current_timeo
);
5445 *timeo_p
= current_timeo
;
5449 finish_wait(&asoc
->wait
, &wait
);
5451 /* Release the association's refcnt. */
5452 sctp_association_put(asoc
);
5457 if (asoc
->init_err_counter
+ 1 > asoc
->max_init_attempts
)
5460 err
= -ECONNREFUSED
;
5464 err
= sock_intr_errno(*timeo_p
);
5472 static int sctp_wait_for_accept(struct sock
*sk
, long timeo
)
5474 struct sctp_endpoint
*ep
;
5478 ep
= sctp_sk(sk
)->ep
;
5482 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
5483 TASK_INTERRUPTIBLE
);
5485 if (list_empty(&ep
->asocs
)) {
5486 sctp_release_sock(sk
);
5487 timeo
= schedule_timeout(timeo
);
5492 if (!sctp_sstate(sk
, LISTENING
))
5496 if (!list_empty(&ep
->asocs
))
5499 err
= sock_intr_errno(timeo
);
5500 if (signal_pending(current
))
5508 finish_wait(sk
->sk_sleep
, &wait
);
5513 void sctp_wait_for_close(struct sock
*sk
, long timeout
)
5518 prepare_to_wait(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
5519 if (list_empty(&sctp_sk(sk
)->ep
->asocs
))
5521 sctp_release_sock(sk
);
5522 timeout
= schedule_timeout(timeout
);
5524 } while (!signal_pending(current
) && timeout
);
5526 finish_wait(sk
->sk_sleep
, &wait
);
5529 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5530 * and its messages to the newsk.
5532 static void sctp_sock_migrate(struct sock
*oldsk
, struct sock
*newsk
,
5533 struct sctp_association
*assoc
,
5534 sctp_socket_type_t type
)
5536 struct sctp_sock
*oldsp
= sctp_sk(oldsk
);
5537 struct sctp_sock
*newsp
= sctp_sk(newsk
);
5538 struct sctp_bind_bucket
*pp
; /* hash list port iterator */
5539 struct sctp_endpoint
*newep
= newsp
->ep
;
5540 struct sk_buff
*skb
, *tmp
;
5541 struct sctp_ulpevent
*event
;
5544 /* Migrate socket buffer sizes and all the socket level options to the
5547 newsk
->sk_sndbuf
= oldsk
->sk_sndbuf
;
5548 newsk
->sk_rcvbuf
= oldsk
->sk_rcvbuf
;
5549 /* Brute force copy old sctp opt. */
5550 inet_sk_copy_descendant(newsk
, oldsk
);
5552 /* Restore the ep value that was overwritten with the above structure
5558 /* Hook this new socket in to the bind_hash list. */
5559 pp
= sctp_sk(oldsk
)->bind_hash
;
5560 sk_add_bind_node(newsk
, &pp
->owner
);
5561 sctp_sk(newsk
)->bind_hash
= pp
;
5562 inet_sk(newsk
)->num
= inet_sk(oldsk
)->num
;
5564 /* Copy the bind_addr list from the original endpoint to the new
5565 * endpoint so that we can handle restarts properly
5567 if (assoc
->peer
.ipv4_address
)
5568 flags
|= SCTP_ADDR4_PEERSUPP
;
5569 if (assoc
->peer
.ipv6_address
)
5570 flags
|= SCTP_ADDR6_PEERSUPP
;
5571 sctp_bind_addr_copy(&newsp
->ep
->base
.bind_addr
,
5572 &oldsp
->ep
->base
.bind_addr
,
5573 SCTP_SCOPE_GLOBAL
, GFP_KERNEL
, flags
);
5575 /* Move any messages in the old socket's receive queue that are for the
5576 * peeled off association to the new socket's receive queue.
5578 sctp_skb_for_each(skb
, &oldsk
->sk_receive_queue
, tmp
) {
5579 event
= sctp_skb2event(skb
);
5580 if (event
->asoc
== assoc
) {
5582 __skb_unlink(skb
, &oldsk
->sk_receive_queue
);
5583 __skb_queue_tail(&newsk
->sk_receive_queue
, skb
);
5584 skb_set_owner_r(skb
, newsk
);
5588 /* Clean up any messages pending delivery due to partial
5589 * delivery. Three cases:
5590 * 1) No partial deliver; no work.
5591 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5592 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5594 skb_queue_head_init(&newsp
->pd_lobby
);
5595 sctp_sk(newsk
)->pd_mode
= assoc
->ulpq
.pd_mode
;
5597 if (sctp_sk(oldsk
)->pd_mode
) {
5598 struct sk_buff_head
*queue
;
5600 /* Decide which queue to move pd_lobby skbs to. */
5601 if (assoc
->ulpq
.pd_mode
) {
5602 queue
= &newsp
->pd_lobby
;
5604 queue
= &newsk
->sk_receive_queue
;
5606 /* Walk through the pd_lobby, looking for skbs that
5607 * need moved to the new socket.
5609 sctp_skb_for_each(skb
, &oldsp
->pd_lobby
, tmp
) {
5610 event
= sctp_skb2event(skb
);
5611 if (event
->asoc
== assoc
) {
5613 __skb_unlink(skb
, &oldsp
->pd_lobby
);
5614 __skb_queue_tail(queue
, skb
);
5615 skb_set_owner_r(skb
, newsk
);
5619 /* Clear up any skbs waiting for the partial
5620 * delivery to finish.
5622 if (assoc
->ulpq
.pd_mode
)
5623 sctp_clear_pd(oldsk
);
5627 /* Set the type of socket to indicate that it is peeled off from the
5628 * original UDP-style socket or created with the accept() call on a
5629 * TCP-style socket..
5633 /* Mark the new socket "in-use" by the user so that any packets
5634 * that may arrive on the association after we've moved it are
5635 * queued to the backlog. This prevents a potential race between
5636 * backlog processing on the old socket and new-packet processing
5637 * on the new socket.
5639 sctp_lock_sock(newsk
);
5640 sctp_assoc_migrate(assoc
, newsk
);
5642 /* If the association on the newsk is already closed before accept()
5643 * is called, set RCV_SHUTDOWN flag.
5645 if (sctp_state(assoc
, CLOSED
) && sctp_style(newsk
, TCP
))
5646 newsk
->sk_shutdown
|= RCV_SHUTDOWN
;
5648 newsk
->sk_state
= SCTP_SS_ESTABLISHED
;
5649 sctp_release_sock(newsk
);
5652 /* This proto struct describes the ULP interface for SCTP. */
5653 struct proto sctp_prot
= {
5655 .owner
= THIS_MODULE
,
5656 .close
= sctp_close
,
5657 .connect
= sctp_connect
,
5658 .disconnect
= sctp_disconnect
,
5659 .accept
= sctp_accept
,
5660 .ioctl
= sctp_ioctl
,
5661 .init
= sctp_init_sock
,
5662 .destroy
= sctp_destroy_sock
,
5663 .shutdown
= sctp_shutdown
,
5664 .setsockopt
= sctp_setsockopt
,
5665 .getsockopt
= sctp_getsockopt
,
5666 .sendmsg
= sctp_sendmsg
,
5667 .recvmsg
= sctp_recvmsg
,
5669 .backlog_rcv
= sctp_backlog_rcv
,
5671 .unhash
= sctp_unhash
,
5672 .get_port
= sctp_get_port
,
5673 .obj_size
= sizeof(struct sctp_sock
),
5676 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5677 struct proto sctpv6_prot
= {
5679 .owner
= THIS_MODULE
,
5680 .close
= sctp_close
,
5681 .connect
= sctp_connect
,
5682 .disconnect
= sctp_disconnect
,
5683 .accept
= sctp_accept
,
5684 .ioctl
= sctp_ioctl
,
5685 .init
= sctp_init_sock
,
5686 .destroy
= sctp_destroy_sock
,
5687 .shutdown
= sctp_shutdown
,
5688 .setsockopt
= sctp_setsockopt
,
5689 .getsockopt
= sctp_getsockopt
,
5690 .sendmsg
= sctp_sendmsg
,
5691 .recvmsg
= sctp_recvmsg
,
5693 .backlog_rcv
= sctp_backlog_rcv
,
5695 .unhash
= sctp_unhash
,
5696 .get_port
= sctp_get_port
,
5697 .obj_size
= sizeof(struct sctp6_sock
),
5699 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */