GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / sctp / socket.c
blob450467265c84b9e97afec88b81cea106337f1e9d
1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * This SCTP implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
24 * This SCTP implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/types.h>
61 #include <linux/kernel.h>
62 #include <linux/wait.h>
63 #include <linux/time.h>
64 #include <linux/ip.h>
65 #include <linux/capability.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
70 #include <linux/slab.h>
72 #include <net/ip.h>
73 #include <net/icmp.h>
74 #include <net/route.h>
75 #include <net/ipv6.h>
76 #include <net/inet_common.h>
78 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sock.h>
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,
92 size_t msg_len);
93 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
94 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
95 static int sctp_wait_for_accept(struct sock *sk, long timeo);
96 static void sctp_wait_for_close(struct sock *sk, long timeo);
97 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
98 union sctp_addr *addr, int len);
99 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
100 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
103 static int sctp_send_asconf(struct sctp_association *asoc,
104 struct sctp_chunk *chunk);
105 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
106 static int sctp_autobind(struct sock *sk);
107 static void sctp_sock_migrate(struct sock *, struct sock *,
108 struct sctp_association *, sctp_socket_type_t);
109 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
111 extern struct kmem_cache *sctp_bucket_cachep;
112 extern int sysctl_sctp_mem[3];
113 extern int sysctl_sctp_rmem[3];
114 extern int sysctl_sctp_wmem[3];
116 static int sctp_memory_pressure;
117 static atomic_t sctp_memory_allocated;
118 struct percpu_counter sctp_sockets_allocated;
120 static void sctp_enter_memory_pressure(struct sock *sk)
122 sctp_memory_pressure = 1;
126 /* Get the sndbuf space available at the time on the association. */
127 static inline int sctp_wspace(struct sctp_association *asoc)
129 int amt;
131 if (asoc->ep->sndbuf_policy)
132 amt = asoc->sndbuf_used;
133 else
134 amt = sk_wmem_alloc_get(asoc->base.sk);
136 if (amt >= asoc->base.sk->sk_sndbuf) {
137 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
138 amt = 0;
139 else {
140 amt = sk_stream_wspace(asoc->base.sk);
141 if (amt < 0)
142 amt = 0;
144 } else {
145 amt = asoc->base.sk->sk_sndbuf - amt;
147 return amt;
150 /* Increment the used sndbuf space count of the corresponding association by
151 * the size of the outgoing data chunk.
152 * Also, set the skb destructor for sndbuf accounting later.
154 * Since it is always 1-1 between chunk and skb, and also a new skb is always
155 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
156 * destructor in the data chunk skb for the purpose of the sndbuf space
157 * tracking.
159 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
161 struct sctp_association *asoc = chunk->asoc;
162 struct sock *sk = asoc->base.sk;
164 /* The sndbuf space is tracked per association. */
165 sctp_association_hold(asoc);
167 skb_set_owner_w(chunk->skb, sk);
169 chunk->skb->destructor = sctp_wfree;
170 /* Save the chunk pointer in skb for sctp_wfree to use later. */
171 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
173 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
174 sizeof(struct sk_buff) +
175 sizeof(struct sctp_chunk);
177 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
178 sk->sk_wmem_queued += chunk->skb->truesize;
179 sk_mem_charge(sk, chunk->skb->truesize);
182 /* Verify that this is a valid address. */
183 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
184 int len)
186 struct sctp_af *af;
188 /* Verify basic sockaddr. */
189 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
190 if (!af)
191 return -EINVAL;
193 /* Is this a valid SCTP address? */
194 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
195 return -EINVAL;
197 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
198 return -EINVAL;
200 return 0;
203 /* Look up the association by its id. If this is not a UDP-style
204 * socket, the ID field is always ignored.
206 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
208 struct sctp_association *asoc = NULL;
210 /* If this is not a UDP-style socket, assoc id should be ignored. */
211 if (!sctp_style(sk, UDP)) {
212 /* Return NULL if the socket state is not ESTABLISHED. It
213 * could be a TCP-style listening socket or a socket which
214 * hasn't yet called connect() to establish an association.
216 if (!sctp_sstate(sk, ESTABLISHED))
217 return NULL;
219 /* Get the first and the only association from the list. */
220 if (!list_empty(&sctp_sk(sk)->ep->asocs))
221 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
222 struct sctp_association, asocs);
223 return asoc;
226 /* Otherwise this is a UDP-style socket. */
227 if (!id || (id == (sctp_assoc_t)-1))
228 return NULL;
230 spin_lock_bh(&sctp_assocs_id_lock);
231 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
232 spin_unlock_bh(&sctp_assocs_id_lock);
234 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
235 return NULL;
237 return asoc;
240 /* Look up the transport from an address and an assoc id. If both address and
241 * id are specified, the associations matching the address and the id should be
242 * the same.
244 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
245 struct sockaddr_storage *addr,
246 sctp_assoc_t id)
248 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
249 struct sctp_transport *transport;
250 union sctp_addr *laddr = (union sctp_addr *)addr;
252 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
253 laddr,
254 &transport);
256 if (!addr_asoc)
257 return NULL;
259 id_asoc = sctp_id2assoc(sk, id);
260 if (id_asoc && (id_asoc != addr_asoc))
261 return NULL;
263 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
264 (union sctp_addr *)addr);
266 return transport;
269 /* API 3.1.2 bind() - UDP Style Syntax
270 * The syntax of bind() is,
272 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
274 * sd - the socket descriptor returned by socket().
275 * addr - the address structure (struct sockaddr_in or struct
276 * sockaddr_in6 [RFC 2553]),
277 * addr_len - the size of the address structure.
279 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
281 int retval = 0;
283 sctp_lock_sock(sk);
285 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
286 sk, addr, addr_len);
288 /* Disallow binding twice. */
289 if (!sctp_sk(sk)->ep->base.bind_addr.port)
290 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
291 addr_len);
292 else
293 retval = -EINVAL;
295 sctp_release_sock(sk);
297 return retval;
300 static long sctp_get_port_local(struct sock *, union sctp_addr *);
302 /* Verify this is a valid sockaddr. */
303 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
304 union sctp_addr *addr, int len)
306 struct sctp_af *af;
308 /* Check minimum size. */
309 if (len < sizeof (struct sockaddr))
310 return NULL;
312 /* V4 mapped address are really of AF_INET family */
313 if (addr->sa.sa_family == AF_INET6 &&
314 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
315 if (!opt->pf->af_supported(AF_INET, opt))
316 return NULL;
317 } else {
318 /* Does this PF support this AF? */
319 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
320 return NULL;
323 /* If we get this far, af is valid. */
324 af = sctp_get_af_specific(addr->sa.sa_family);
326 if (len < af->sockaddr_len)
327 return NULL;
329 return af;
332 /* Bind a local address either to an endpoint or to an association. */
333 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
335 struct sctp_sock *sp = sctp_sk(sk);
336 struct sctp_endpoint *ep = sp->ep;
337 struct sctp_bind_addr *bp = &ep->base.bind_addr;
338 struct sctp_af *af;
339 unsigned short snum;
340 int ret = 0;
342 /* Common sockaddr verification. */
343 af = sctp_sockaddr_af(sp, addr, len);
344 if (!af) {
345 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
346 sk, addr, len);
347 return -EINVAL;
350 snum = ntohs(addr->v4.sin_port);
352 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
353 ", port: %d, new port: %d, len: %d)\n",
355 addr,
356 bp->port, snum,
357 len);
359 /* PF specific bind() address verification. */
360 if (!sp->pf->bind_verify(sp, addr))
361 return -EADDRNOTAVAIL;
363 /* We must either be unbound, or bind to the same port.
364 * It's OK to allow 0 ports if we are already bound.
365 * We'll just inhert an already bound port in this case
367 if (bp->port) {
368 if (!snum)
369 snum = bp->port;
370 else if (snum != bp->port) {
371 SCTP_DEBUG_PRINTK("sctp_do_bind:"
372 " New port %d does not match existing port "
373 "%d.\n", snum, bp->port);
374 return -EINVAL;
378 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
379 return -EACCES;
381 /* See if the address matches any of the addresses we may have
382 * already bound before checking against other endpoints.
384 if (sctp_bind_addr_match(bp, addr, sp))
385 return -EINVAL;
387 /* Make sure we are allowed to bind here.
388 * The function sctp_get_port_local() does duplicate address
389 * detection.
391 addr->v4.sin_port = htons(snum);
392 if ((ret = sctp_get_port_local(sk, addr))) {
393 return -EADDRINUSE;
396 /* Refresh ephemeral port. */
397 if (!bp->port)
398 bp->port = inet_sk(sk)->inet_num;
400 /* Add the address to the bind address list.
401 * Use GFP_ATOMIC since BHs will be disabled.
403 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
405 /* Copy back into socket for getsockname() use. */
406 if (!ret) {
407 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
408 af->to_sk_saddr(addr, sk);
411 return ret;
414 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
416 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
417 * at any one time. If a sender, after sending an ASCONF chunk, decides
418 * it needs to transfer another ASCONF Chunk, it MUST wait until the
419 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
420 * subsequent ASCONF. Note this restriction binds each side, so at any
421 * time two ASCONF may be in-transit on any given association (one sent
422 * from each endpoint).
424 static int sctp_send_asconf(struct sctp_association *asoc,
425 struct sctp_chunk *chunk)
427 int retval = 0;
429 /* If there is an outstanding ASCONF chunk, queue it for later
430 * transmission.
432 if (asoc->addip_last_asconf) {
433 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
434 goto out;
437 /* Hold the chunk until an ASCONF_ACK is received. */
438 sctp_chunk_hold(chunk);
439 retval = sctp_primitive_ASCONF(asoc, chunk);
440 if (retval)
441 sctp_chunk_free(chunk);
442 else
443 asoc->addip_last_asconf = chunk;
445 out:
446 return retval;
449 /* Add a list of addresses as bind addresses to local endpoint or
450 * association.
452 * Basically run through each address specified in the addrs/addrcnt
453 * array/length pair, determine if it is IPv6 or IPv4 and call
454 * sctp_do_bind() on it.
456 * If any of them fails, then the operation will be reversed and the
457 * ones that were added will be removed.
459 * Only sctp_setsockopt_bindx() is supposed to call this function.
461 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
463 int cnt;
464 int retval = 0;
465 void *addr_buf;
466 struct sockaddr *sa_addr;
467 struct sctp_af *af;
469 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
470 sk, addrs, addrcnt);
472 addr_buf = addrs;
473 for (cnt = 0; cnt < addrcnt; cnt++) {
474 /* The list may contain either IPv4 or IPv6 address;
475 * determine the address length for walking thru the list.
477 sa_addr = (struct sockaddr *)addr_buf;
478 af = sctp_get_af_specific(sa_addr->sa_family);
479 if (!af) {
480 retval = -EINVAL;
481 goto err_bindx_add;
484 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
485 af->sockaddr_len);
487 addr_buf += af->sockaddr_len;
489 err_bindx_add:
490 if (retval < 0) {
491 /* Failed. Cleanup the ones that have been added */
492 if (cnt > 0)
493 sctp_bindx_rem(sk, addrs, cnt);
494 return retval;
498 return retval;
501 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
502 * associations that are part of the endpoint indicating that a list of local
503 * addresses are added to the endpoint.
505 * If any of the addresses is already in the bind address list of the
506 * association, we do not send the chunk for that association. But it will not
507 * affect other associations.
509 * Only sctp_setsockopt_bindx() is supposed to call this function.
511 static int sctp_send_asconf_add_ip(struct sock *sk,
512 struct sockaddr *addrs,
513 int addrcnt)
515 struct sctp_sock *sp;
516 struct sctp_endpoint *ep;
517 struct sctp_association *asoc;
518 struct sctp_bind_addr *bp;
519 struct sctp_chunk *chunk;
520 struct sctp_sockaddr_entry *laddr;
521 union sctp_addr *addr;
522 union sctp_addr saveaddr;
523 void *addr_buf;
524 struct sctp_af *af;
525 struct list_head *p;
526 int i;
527 int retval = 0;
529 if (!sctp_addip_enable)
530 return retval;
532 sp = sctp_sk(sk);
533 ep = sp->ep;
535 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
536 __func__, sk, addrs, addrcnt);
538 list_for_each_entry(asoc, &ep->asocs, asocs) {
540 if (!asoc->peer.asconf_capable)
541 continue;
543 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
544 continue;
546 if (!sctp_state(asoc, ESTABLISHED))
547 continue;
549 /* Check if any address in the packed array of addresses is
550 * in the bind address list of the association. If so,
551 * do not send the asconf chunk to its peer, but continue with
552 * other associations.
554 addr_buf = addrs;
555 for (i = 0; i < addrcnt; i++) {
556 addr = (union sctp_addr *)addr_buf;
557 af = sctp_get_af_specific(addr->v4.sin_family);
558 if (!af) {
559 retval = -EINVAL;
560 goto out;
563 if (sctp_assoc_lookup_laddr(asoc, addr))
564 break;
566 addr_buf += af->sockaddr_len;
568 if (i < addrcnt)
569 continue;
571 /* Use the first valid address in bind addr list of
572 * association as Address Parameter of ASCONF CHUNK.
574 bp = &asoc->base.bind_addr;
575 p = bp->address_list.next;
576 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
577 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
578 addrcnt, SCTP_PARAM_ADD_IP);
579 if (!chunk) {
580 retval = -ENOMEM;
581 goto out;
584 retval = sctp_send_asconf(asoc, chunk);
585 if (retval)
586 goto out;
588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
591 addr_buf = addrs;
592 for (i = 0; i < addrcnt; i++) {
593 addr = (union sctp_addr *)addr_buf;
594 af = sctp_get_af_specific(addr->v4.sin_family);
595 memcpy(&saveaddr, addr, af->sockaddr_len);
596 retval = sctp_add_bind_addr(bp, &saveaddr,
597 SCTP_ADDR_NEW, GFP_ATOMIC);
598 addr_buf += af->sockaddr_len;
602 out:
603 return retval;
606 /* Remove a list of addresses from bind addresses list. Do not remove the
607 * last address.
609 * Basically run through each address specified in the addrs/addrcnt
610 * array/length pair, determine if it is IPv6 or IPv4 and call
611 * sctp_del_bind() on it.
613 * If any of them fails, then the operation will be reversed and the
614 * ones that were removed will be added back.
616 * At least one address has to be left; if only one address is
617 * available, the operation will return -EBUSY.
619 * Only sctp_setsockopt_bindx() is supposed to call this function.
621 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
623 struct sctp_sock *sp = sctp_sk(sk);
624 struct sctp_endpoint *ep = sp->ep;
625 int cnt;
626 struct sctp_bind_addr *bp = &ep->base.bind_addr;
627 int retval = 0;
628 void *addr_buf;
629 union sctp_addr *sa_addr;
630 struct sctp_af *af;
632 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
633 sk, addrs, addrcnt);
635 addr_buf = addrs;
636 for (cnt = 0; cnt < addrcnt; cnt++) {
637 /* If the bind address list is empty or if there is only one
638 * bind address, there is nothing more to be removed (we need
639 * at least one address here).
641 if (list_empty(&bp->address_list) ||
642 (sctp_list_single_entry(&bp->address_list))) {
643 retval = -EBUSY;
644 goto err_bindx_rem;
647 sa_addr = (union sctp_addr *)addr_buf;
648 af = sctp_get_af_specific(sa_addr->sa.sa_family);
649 if (!af) {
650 retval = -EINVAL;
651 goto err_bindx_rem;
654 if (!af->addr_valid(sa_addr, sp, NULL)) {
655 retval = -EADDRNOTAVAIL;
656 goto err_bindx_rem;
659 if (sa_addr->v4.sin_port != htons(bp->port)) {
660 retval = -EINVAL;
661 goto err_bindx_rem;
664 retval = sctp_del_bind_addr(bp, sa_addr);
666 addr_buf += af->sockaddr_len;
667 err_bindx_rem:
668 if (retval < 0) {
669 /* Failed. Add the ones that has been removed back */
670 if (cnt > 0)
671 sctp_bindx_add(sk, addrs, cnt);
672 return retval;
676 return retval;
679 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
680 * the associations that are part of the endpoint indicating that a list of
681 * local addresses are removed from the endpoint.
683 * If any of the addresses is already in the bind address list of the
684 * association, we do not send the chunk for that association. But it will not
685 * affect other associations.
687 * Only sctp_setsockopt_bindx() is supposed to call this function.
689 static int sctp_send_asconf_del_ip(struct sock *sk,
690 struct sockaddr *addrs,
691 int addrcnt)
693 struct sctp_sock *sp;
694 struct sctp_endpoint *ep;
695 struct sctp_association *asoc;
696 struct sctp_transport *transport;
697 struct sctp_bind_addr *bp;
698 struct sctp_chunk *chunk;
699 union sctp_addr *laddr;
700 void *addr_buf;
701 struct sctp_af *af;
702 struct sctp_sockaddr_entry *saddr;
703 int i;
704 int retval = 0;
706 if (!sctp_addip_enable)
707 return retval;
709 sp = sctp_sk(sk);
710 ep = sp->ep;
712 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
713 __func__, sk, addrs, addrcnt);
715 list_for_each_entry(asoc, &ep->asocs, asocs) {
717 if (!asoc->peer.asconf_capable)
718 continue;
720 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
721 continue;
723 if (!sctp_state(asoc, ESTABLISHED))
724 continue;
726 /* Check if any address in the packed array of addresses is
727 * not present in the bind address list of the association.
728 * If so, do not send the asconf chunk to its peer, but
729 * continue with other associations.
731 addr_buf = addrs;
732 for (i = 0; i < addrcnt; i++) {
733 laddr = (union sctp_addr *)addr_buf;
734 af = sctp_get_af_specific(laddr->v4.sin_family);
735 if (!af) {
736 retval = -EINVAL;
737 goto out;
740 if (!sctp_assoc_lookup_laddr(asoc, laddr))
741 break;
743 addr_buf += af->sockaddr_len;
745 if (i < addrcnt)
746 continue;
748 /* Find one address in the association's bind address list
749 * that is not in the packed array of addresses. This is to
750 * make sure that we do not delete all the addresses in the
751 * association.
753 bp = &asoc->base.bind_addr;
754 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
755 addrcnt, sp);
756 if (!laddr)
757 continue;
759 /* We do not need RCU protection throughout this loop
760 * because this is done under a socket lock from the
761 * setsockopt call.
763 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
764 SCTP_PARAM_DEL_IP);
765 if (!chunk) {
766 retval = -ENOMEM;
767 goto out;
770 /* Reset use_as_src flag for the addresses in the bind address
771 * list that are to be deleted.
773 addr_buf = addrs;
774 for (i = 0; i < addrcnt; i++) {
775 laddr = (union sctp_addr *)addr_buf;
776 af = sctp_get_af_specific(laddr->v4.sin_family);
777 list_for_each_entry(saddr, &bp->address_list, list) {
778 if (sctp_cmp_addr_exact(&saddr->a, laddr))
779 saddr->state = SCTP_ADDR_DEL;
781 addr_buf += af->sockaddr_len;
784 /* Update the route and saddr entries for all the transports
785 * as some of the addresses in the bind address list are
786 * about to be deleted and cannot be used as source addresses.
788 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
789 transports) {
790 dst_release(transport->dst);
791 sctp_transport_route(transport, NULL,
792 sctp_sk(asoc->base.sk));
795 retval = sctp_send_asconf(asoc, chunk);
797 out:
798 return retval;
801 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
803 * API 8.1
804 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
805 * int flags);
807 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
808 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
809 * or IPv6 addresses.
811 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
812 * Section 3.1.2 for this usage.
814 * addrs is a pointer to an array of one or more socket addresses. Each
815 * address is contained in its appropriate structure (i.e. struct
816 * sockaddr_in or struct sockaddr_in6) the family of the address type
817 * must be used to distinguish the address length (note that this
818 * representation is termed a "packed array" of addresses). The caller
819 * specifies the number of addresses in the array with addrcnt.
821 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
822 * -1, and sets errno to the appropriate error code.
824 * For SCTP, the port given in each socket address must be the same, or
825 * sctp_bindx() will fail, setting errno to EINVAL.
827 * The flags parameter is formed from the bitwise OR of zero or more of
828 * the following currently defined flags:
830 * SCTP_BINDX_ADD_ADDR
832 * SCTP_BINDX_REM_ADDR
834 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
835 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
836 * addresses from the association. The two flags are mutually exclusive;
837 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
838 * not remove all addresses from an association; sctp_bindx() will
839 * reject such an attempt with EINVAL.
841 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
842 * additional addresses with an endpoint after calling bind(). Or use
843 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
844 * socket is associated with so that no new association accepted will be
845 * associated with those addresses. If the endpoint supports dynamic
846 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
847 * endpoint to send the appropriate message to the peer to change the
848 * peers address lists.
850 * Adding and removing addresses from a connected association is
851 * optional functionality. Implementations that do not support this
852 * functionality should return EOPNOTSUPP.
854 * Basically do nothing but copying the addresses from user to kernel
855 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
856 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
857 * from userspace.
859 * We don't use copy_from_user() for optimization: we first do the
860 * sanity checks (buffer size -fast- and access check-healthy
861 * pointer); if all of those succeed, then we can alloc the memory
862 * (expensive operation) needed to copy the data to kernel. Then we do
863 * the copying without checking the user space area
864 * (__copy_from_user()).
866 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
867 * it.
869 * sk The sk of the socket
870 * addrs The pointer to the addresses in user land
871 * addrssize Size of the addrs buffer
872 * op Operation to perform (add or remove, see the flags of
873 * sctp_bindx)
875 * Returns 0 if ok, <0 errno code on error.
877 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
878 struct sockaddr __user *addrs,
879 int addrs_size, int op)
881 struct sockaddr *kaddrs;
882 int err;
883 int addrcnt = 0;
884 int walk_size = 0;
885 struct sockaddr *sa_addr;
886 void *addr_buf;
887 struct sctp_af *af;
889 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
890 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
892 if (unlikely(addrs_size <= 0))
893 return -EINVAL;
895 /* Check the user passed a healthy pointer. */
896 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
897 return -EFAULT;
899 /* Alloc space for the address array in kernel memory. */
900 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
901 if (unlikely(!kaddrs))
902 return -ENOMEM;
904 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
905 kfree(kaddrs);
906 return -EFAULT;
909 /* Walk through the addrs buffer and count the number of addresses. */
910 addr_buf = kaddrs;
911 while (walk_size < addrs_size) {
912 if (walk_size + sizeof(sa_family_t) > addrs_size) {
913 kfree(kaddrs);
914 return -EINVAL;
917 sa_addr = (struct sockaddr *)addr_buf;
918 af = sctp_get_af_specific(sa_addr->sa_family);
920 /* If the address family is not supported or if this address
921 * causes the address buffer to overflow return EINVAL.
923 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
924 kfree(kaddrs);
925 return -EINVAL;
927 addrcnt++;
928 addr_buf += af->sockaddr_len;
929 walk_size += af->sockaddr_len;
932 /* Do the work. */
933 switch (op) {
934 case SCTP_BINDX_ADD_ADDR:
935 err = sctp_bindx_add(sk, kaddrs, addrcnt);
936 if (err)
937 goto out;
938 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
939 break;
941 case SCTP_BINDX_REM_ADDR:
942 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
943 if (err)
944 goto out;
945 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
946 break;
948 default:
949 err = -EINVAL;
950 break;
953 out:
954 kfree(kaddrs);
956 return err;
959 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
961 * Common routine for handling connect() and sctp_connectx().
962 * Connect will come in with just a single address.
964 static int __sctp_connect(struct sock* sk,
965 struct sockaddr *kaddrs,
966 int addrs_size,
967 sctp_assoc_t *assoc_id)
969 struct sctp_sock *sp;
970 struct sctp_endpoint *ep;
971 struct sctp_association *asoc = NULL;
972 struct sctp_association *asoc2;
973 struct sctp_transport *transport;
974 union sctp_addr to;
975 struct sctp_af *af;
976 sctp_scope_t scope;
977 long timeo;
978 int err = 0;
979 int addrcnt = 0;
980 int walk_size = 0;
981 union sctp_addr *sa_addr = NULL;
982 void *addr_buf;
983 unsigned short port;
984 unsigned int f_flags = 0;
986 sp = sctp_sk(sk);
987 ep = sp->ep;
989 /* connect() cannot be done on a socket that is already in ESTABLISHED
990 * state - UDP-style peeled off socket or a TCP-style socket that
991 * is already connected.
992 * It cannot be done even on a TCP-style listening socket.
994 if (sctp_sstate(sk, ESTABLISHED) ||
995 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
996 err = -EISCONN;
997 goto out_free;
1000 /* Walk through the addrs buffer and count the number of addresses. */
1001 addr_buf = kaddrs;
1002 while (walk_size < addrs_size) {
1003 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1004 err = -EINVAL;
1005 goto out_free;
1008 sa_addr = (union sctp_addr *)addr_buf;
1009 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1011 /* If the address family is not supported or if this address
1012 * causes the address buffer to overflow return EINVAL.
1014 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1015 err = -EINVAL;
1016 goto out_free;
1019 port = ntohs(sa_addr->v4.sin_port);
1021 /* Save current address so we can work with it */
1022 memcpy(&to, sa_addr, af->sockaddr_len);
1024 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1025 if (err)
1026 goto out_free;
1028 /* Make sure the destination port is correctly set
1029 * in all addresses.
1031 if (asoc && asoc->peer.port && asoc->peer.port != port)
1032 goto out_free;
1035 /* Check if there already is a matching association on the
1036 * endpoint (other than the one created here).
1038 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1039 if (asoc2 && asoc2 != asoc) {
1040 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1041 err = -EISCONN;
1042 else
1043 err = -EALREADY;
1044 goto out_free;
1047 /* If we could not find a matching association on the endpoint,
1048 * make sure that there is no peeled-off association matching
1049 * the peer address even on another socket.
1051 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1052 err = -EADDRNOTAVAIL;
1053 goto out_free;
1056 if (!asoc) {
1057 /* If a bind() or sctp_bindx() is not called prior to
1058 * an sctp_connectx() call, the system picks an
1059 * ephemeral port and will choose an address set
1060 * equivalent to binding with a wildcard address.
1062 if (!ep->base.bind_addr.port) {
1063 if (sctp_autobind(sk)) {
1064 err = -EAGAIN;
1065 goto out_free;
1067 } else {
1069 * If an unprivileged user inherits a 1-many
1070 * style socket with open associations on a
1071 * privileged port, it MAY be permitted to
1072 * accept new associations, but it SHOULD NOT
1073 * be permitted to open new associations.
1075 if (ep->base.bind_addr.port < PROT_SOCK &&
1076 !capable(CAP_NET_BIND_SERVICE)) {
1077 err = -EACCES;
1078 goto out_free;
1082 scope = sctp_scope(&to);
1083 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1084 if (!asoc) {
1085 err = -ENOMEM;
1086 goto out_free;
1089 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1090 GFP_KERNEL);
1091 if (err < 0) {
1092 goto out_free;
1097 /* Prime the peer's transport structures. */
1098 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1099 SCTP_UNKNOWN);
1100 if (!transport) {
1101 err = -ENOMEM;
1102 goto out_free;
1105 addrcnt++;
1106 addr_buf += af->sockaddr_len;
1107 walk_size += af->sockaddr_len;
1110 /* In case the user of sctp_connectx() wants an association
1111 * id back, assign one now.
1113 if (assoc_id) {
1114 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1115 if (err < 0)
1116 goto out_free;
1119 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1120 if (err < 0) {
1121 goto out_free;
1124 /* Initialize sk's dport and daddr for getpeername() */
1125 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1126 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1127 af->to_sk_daddr(sa_addr, sk);
1128 sk->sk_err = 0;
1130 /* in-kernel sockets don't generally have a file allocated to them
1131 * if all they do is call sock_create_kern().
1133 if (sk->sk_socket->file)
1134 f_flags = sk->sk_socket->file->f_flags;
1136 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1138 err = sctp_wait_for_connect(asoc, &timeo);
1139 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
1140 *assoc_id = asoc->assoc_id;
1142 /* Don't free association on exit. */
1143 asoc = NULL;
1145 out_free:
1147 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1148 " kaddrs: %p err: %d\n",
1149 asoc, kaddrs, err);
1150 if (asoc)
1151 sctp_association_free(asoc);
1152 return err;
1155 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1157 * API 8.9
1158 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1159 * sctp_assoc_t *asoc);
1161 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1162 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1163 * or IPv6 addresses.
1165 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1166 * Section 3.1.2 for this usage.
1168 * addrs is a pointer to an array of one or more socket addresses. Each
1169 * address is contained in its appropriate structure (i.e. struct
1170 * sockaddr_in or struct sockaddr_in6) the family of the address type
1171 * must be used to distengish the address length (note that this
1172 * representation is termed a "packed array" of addresses). The caller
1173 * specifies the number of addresses in the array with addrcnt.
1175 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1176 * the association id of the new association. On failure, sctp_connectx()
1177 * returns -1, and sets errno to the appropriate error code. The assoc_id
1178 * is not touched by the kernel.
1180 * For SCTP, the port given in each socket address must be the same, or
1181 * sctp_connectx() will fail, setting errno to EINVAL.
1183 * An application can use sctp_connectx to initiate an association with
1184 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1185 * allows a caller to specify multiple addresses at which a peer can be
1186 * reached. The way the SCTP stack uses the list of addresses to set up
1187 * the association is implementation dependant. This function only
1188 * specifies that the stack will try to make use of all the addresses in
1189 * the list when needed.
1191 * Note that the list of addresses passed in is only used for setting up
1192 * the association. It does not necessarily equal the set of addresses
1193 * the peer uses for the resulting association. If the caller wants to
1194 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1195 * retrieve them after the association has been set up.
1197 * Basically do nothing but copying the addresses from user to kernel
1198 * land and invoking either sctp_connectx(). This is used for tunneling
1199 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1201 * We don't use copy_from_user() for optimization: we first do the
1202 * sanity checks (buffer size -fast- and access check-healthy
1203 * pointer); if all of those succeed, then we can alloc the memory
1204 * (expensive operation) needed to copy the data to kernel. Then we do
1205 * the copying without checking the user space area
1206 * (__copy_from_user()).
1208 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1209 * it.
1211 * sk The sk of the socket
1212 * addrs The pointer to the addresses in user land
1213 * addrssize Size of the addrs buffer
1215 * Returns >=0 if ok, <0 errno code on error.
1217 SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
1218 struct sockaddr __user *addrs,
1219 int addrs_size,
1220 sctp_assoc_t *assoc_id)
1222 int err = 0;
1223 struct sockaddr *kaddrs;
1225 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1226 __func__, sk, addrs, addrs_size);
1228 if (unlikely(addrs_size <= 0))
1229 return -EINVAL;
1231 /* Check the user passed a healthy pointer. */
1232 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1233 return -EFAULT;
1235 /* Alloc space for the address array in kernel memory. */
1236 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1237 if (unlikely(!kaddrs))
1238 return -ENOMEM;
1240 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1241 err = -EFAULT;
1242 } else {
1243 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1246 kfree(kaddrs);
1248 return err;
1252 * This is an older interface. It's kept for backward compatibility
1253 * to the option that doesn't provide association id.
1255 SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1256 struct sockaddr __user *addrs,
1257 int addrs_size)
1259 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1263 * New interface for the API. The since the API is done with a socket
1264 * option, to make it simple we feed back the association id is as a return
1265 * indication to the call. Error is always negative and association id is
1266 * always positive.
1268 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1269 struct sockaddr __user *addrs,
1270 int addrs_size)
1272 sctp_assoc_t assoc_id = 0;
1273 int err = 0;
1275 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1277 if (err)
1278 return err;
1279 else
1280 return assoc_id;
1284 * New (hopefully final) interface for the API.
1285 * We use the sctp_getaddrs_old structure so that use-space library
1286 * can avoid any unnecessary allocations. The only defferent part
1287 * is that we store the actual length of the address buffer into the
1288 * addrs_num structure member. That way we can re-use the existing
1289 * code.
1291 SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1292 char __user *optval,
1293 int __user *optlen)
1295 struct sctp_getaddrs_old param;
1296 sctp_assoc_t assoc_id = 0;
1297 int err = 0;
1299 if (len < sizeof(param))
1300 return -EINVAL;
1302 if (copy_from_user(&param, optval, sizeof(param)))
1303 return -EFAULT;
1305 err = __sctp_setsockopt_connectx(sk,
1306 (struct sockaddr __user *)param.addrs,
1307 param.addr_num, &assoc_id);
1309 if (err == 0 || err == -EINPROGRESS) {
1310 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1311 return -EFAULT;
1312 if (put_user(sizeof(assoc_id), optlen))
1313 return -EFAULT;
1316 return err;
1319 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1321 struct sctp_endpoint *ep;
1322 struct sctp_association *asoc;
1323 struct list_head *pos, *temp;
1325 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1327 sctp_lock_sock(sk);
1328 sk->sk_shutdown = SHUTDOWN_MASK;
1329 sk->sk_state = SCTP_SS_CLOSING;
1331 ep = sctp_sk(sk)->ep;
1333 /* Walk all associations on an endpoint. */
1334 list_for_each_safe(pos, temp, &ep->asocs) {
1335 asoc = list_entry(pos, struct sctp_association, asocs);
1337 if (sctp_style(sk, TCP)) {
1338 /* A closed association can still be in the list if
1339 * it belongs to a TCP-style listening socket that is
1340 * not yet accepted. If so, free it. If not, send an
1341 * ABORT or SHUTDOWN based on the linger options.
1343 if (sctp_state(asoc, CLOSED)) {
1344 sctp_unhash_established(asoc);
1345 sctp_association_free(asoc);
1346 continue;
1350 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1351 struct sctp_chunk *chunk;
1353 chunk = sctp_make_abort_user(asoc, NULL, 0);
1354 if (chunk)
1355 sctp_primitive_ABORT(asoc, chunk);
1356 } else
1357 sctp_primitive_SHUTDOWN(asoc, NULL);
1360 /* Clean up any skbs sitting on the receive queue. */
1361 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1362 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1364 /* On a TCP-style socket, block for at most linger_time if set. */
1365 if (sctp_style(sk, TCP) && timeout)
1366 sctp_wait_for_close(sk, timeout);
1368 /* This will run the backlog queue. */
1369 sctp_release_sock(sk);
1371 /* Supposedly, no process has access to the socket, but
1372 * the net layers still may.
1374 sctp_local_bh_disable();
1375 sctp_bh_lock_sock(sk);
1377 /* Hold the sock, since sk_common_release() will put sock_put()
1378 * and we have just a little more cleanup.
1380 sock_hold(sk);
1381 sk_common_release(sk);
1383 sctp_bh_unlock_sock(sk);
1384 sctp_local_bh_enable();
1386 sock_put(sk);
1388 SCTP_DBG_OBJCNT_DEC(sock);
1391 /* Handle EPIPE error. */
1392 static int sctp_error(struct sock *sk, int flags, int err)
1394 if (err == -EPIPE)
1395 err = sock_error(sk) ? : -EPIPE;
1396 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1397 send_sig(SIGPIPE, current, 0);
1398 return err;
1401 /* API 3.1.3 sendmsg() - UDP Style Syntax
1403 * An application uses sendmsg() and recvmsg() calls to transmit data to
1404 * and receive data from its peer.
1406 * ssize_t sendmsg(int socket, const struct msghdr *message,
1407 * int flags);
1409 * socket - the socket descriptor of the endpoint.
1410 * message - pointer to the msghdr structure which contains a single
1411 * user message and possibly some ancillary data.
1413 * See Section 5 for complete description of the data
1414 * structures.
1416 * flags - flags sent or received with the user message, see Section
1417 * 5 for complete description of the flags.
1419 * Note: This function could use a rewrite especially when explicit
1420 * connect support comes in.
1422 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1424 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1426 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1427 struct msghdr *msg, size_t msg_len)
1429 struct sctp_sock *sp;
1430 struct sctp_endpoint *ep;
1431 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1432 struct sctp_transport *transport, *chunk_tp;
1433 struct sctp_chunk *chunk;
1434 union sctp_addr to;
1435 struct sockaddr *msg_name = NULL;
1436 struct sctp_sndrcvinfo default_sinfo = { 0 };
1437 struct sctp_sndrcvinfo *sinfo;
1438 struct sctp_initmsg *sinit;
1439 sctp_assoc_t associd = 0;
1440 sctp_cmsgs_t cmsgs = { NULL };
1441 int err;
1442 sctp_scope_t scope;
1443 long timeo;
1444 __u16 sinfo_flags = 0;
1445 struct sctp_datamsg *datamsg;
1446 int msg_flags = msg->msg_flags;
1448 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1449 sk, msg, msg_len);
1451 err = 0;
1452 sp = sctp_sk(sk);
1453 ep = sp->ep;
1455 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1457 /* We cannot send a message over a TCP-style listening socket. */
1458 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1459 err = -EPIPE;
1460 goto out_nounlock;
1463 /* Parse out the SCTP CMSGs. */
1464 err = sctp_msghdr_parse(msg, &cmsgs);
1466 if (err) {
1467 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1468 goto out_nounlock;
1471 /* Fetch the destination address for this packet. This
1472 * address only selects the association--it is not necessarily
1473 * the address we will send to.
1474 * For a peeled-off socket, msg_name is ignored.
1476 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1477 int msg_namelen = msg->msg_namelen;
1479 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1480 msg_namelen);
1481 if (err)
1482 return err;
1484 if (msg_namelen > sizeof(to))
1485 msg_namelen = sizeof(to);
1486 memcpy(&to, msg->msg_name, msg_namelen);
1487 msg_name = msg->msg_name;
1490 sinfo = cmsgs.info;
1491 sinit = cmsgs.init;
1493 /* Did the user specify SNDRCVINFO? */
1494 if (sinfo) {
1495 sinfo_flags = sinfo->sinfo_flags;
1496 associd = sinfo->sinfo_assoc_id;
1499 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1500 msg_len, sinfo_flags);
1502 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1503 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1504 err = -EINVAL;
1505 goto out_nounlock;
1508 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1509 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1510 * If SCTP_ABORT is set, the message length could be non zero with
1511 * the msg_iov set to the user abort reason.
1513 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1514 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1515 err = -EINVAL;
1516 goto out_nounlock;
1519 /* If SCTP_ADDR_OVER is set, there must be an address
1520 * specified in msg_name.
1522 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1523 err = -EINVAL;
1524 goto out_nounlock;
1527 transport = NULL;
1529 SCTP_DEBUG_PRINTK("About to look up association.\n");
1531 sctp_lock_sock(sk);
1533 /* If a msg_name has been specified, assume this is to be used. */
1534 if (msg_name) {
1535 /* Look for a matching association on the endpoint. */
1536 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1537 if (!asoc) {
1538 /* If we could not find a matching association on the
1539 * endpoint, make sure that it is not a TCP-style
1540 * socket that already has an association or there is
1541 * no peeled-off association on another socket.
1543 if ((sctp_style(sk, TCP) &&
1544 sctp_sstate(sk, ESTABLISHED)) ||
1545 sctp_endpoint_is_peeled_off(ep, &to)) {
1546 err = -EADDRNOTAVAIL;
1547 goto out_unlock;
1550 } else {
1551 asoc = sctp_id2assoc(sk, associd);
1552 if (!asoc) {
1553 err = -EPIPE;
1554 goto out_unlock;
1558 if (asoc) {
1559 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1561 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1562 * socket that has an association in CLOSED state. This can
1563 * happen when an accepted socket has an association that is
1564 * already CLOSED.
1566 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1567 err = -EPIPE;
1568 goto out_unlock;
1571 if (sinfo_flags & SCTP_EOF) {
1572 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1573 asoc);
1574 sctp_primitive_SHUTDOWN(asoc, NULL);
1575 err = 0;
1576 goto out_unlock;
1578 if (sinfo_flags & SCTP_ABORT) {
1580 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1581 if (!chunk) {
1582 err = -ENOMEM;
1583 goto out_unlock;
1586 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1587 sctp_primitive_ABORT(asoc, chunk);
1588 err = 0;
1589 goto out_unlock;
1593 /* Do we need to create the association? */
1594 if (!asoc) {
1595 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1597 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1598 err = -EINVAL;
1599 goto out_unlock;
1602 /* Check for invalid stream against the stream counts,
1603 * either the default or the user specified stream counts.
1605 if (sinfo) {
1606 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1607 /* Check against the defaults. */
1608 if (sinfo->sinfo_stream >=
1609 sp->initmsg.sinit_num_ostreams) {
1610 err = -EINVAL;
1611 goto out_unlock;
1613 } else {
1614 /* Check against the requested. */
1615 if (sinfo->sinfo_stream >=
1616 sinit->sinit_num_ostreams) {
1617 err = -EINVAL;
1618 goto out_unlock;
1624 * API 3.1.2 bind() - UDP Style Syntax
1625 * If a bind() or sctp_bindx() is not called prior to a
1626 * sendmsg() call that initiates a new association, the
1627 * system picks an ephemeral port and will choose an address
1628 * set equivalent to binding with a wildcard address.
1630 if (!ep->base.bind_addr.port) {
1631 if (sctp_autobind(sk)) {
1632 err = -EAGAIN;
1633 goto out_unlock;
1635 } else {
1637 * If an unprivileged user inherits a one-to-many
1638 * style socket with open associations on a privileged
1639 * port, it MAY be permitted to accept new associations,
1640 * but it SHOULD NOT be permitted to open new
1641 * associations.
1643 if (ep->base.bind_addr.port < PROT_SOCK &&
1644 !capable(CAP_NET_BIND_SERVICE)) {
1645 err = -EACCES;
1646 goto out_unlock;
1650 scope = sctp_scope(&to);
1651 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1652 if (!new_asoc) {
1653 err = -ENOMEM;
1654 goto out_unlock;
1656 asoc = new_asoc;
1657 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1658 if (err < 0) {
1659 err = -ENOMEM;
1660 goto out_free;
1663 /* If the SCTP_INIT ancillary data is specified, set all
1664 * the association init values accordingly.
1666 if (sinit) {
1667 if (sinit->sinit_num_ostreams) {
1668 asoc->c.sinit_num_ostreams =
1669 sinit->sinit_num_ostreams;
1671 if (sinit->sinit_max_instreams) {
1672 asoc->c.sinit_max_instreams =
1673 sinit->sinit_max_instreams;
1675 if (sinit->sinit_max_attempts) {
1676 asoc->max_init_attempts
1677 = sinit->sinit_max_attempts;
1679 if (sinit->sinit_max_init_timeo) {
1680 asoc->max_init_timeo =
1681 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1685 /* Prime the peer's transport structures. */
1686 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1687 if (!transport) {
1688 err = -ENOMEM;
1689 goto out_free;
1693 /* ASSERT: we have a valid association at this point. */
1694 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1696 if (!sinfo) {
1697 /* If the user didn't specify SNDRCVINFO, make up one with
1698 * some defaults.
1700 default_sinfo.sinfo_stream = asoc->default_stream;
1701 default_sinfo.sinfo_flags = asoc->default_flags;
1702 default_sinfo.sinfo_ppid = asoc->default_ppid;
1703 default_sinfo.sinfo_context = asoc->default_context;
1704 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1705 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1706 sinfo = &default_sinfo;
1709 /* API 7.1.7, the sndbuf size per association bounds the
1710 * maximum size of data that can be sent in a single send call.
1712 if (msg_len > sk->sk_sndbuf) {
1713 err = -EMSGSIZE;
1714 goto out_free;
1717 if (asoc->pmtu_pending)
1718 sctp_assoc_pending_pmtu(asoc);
1720 /* If fragmentation is disabled and the message length exceeds the
1721 * association fragmentation point, return EMSGSIZE. The I-D
1722 * does not specify what this error is, but this looks like
1723 * a great fit.
1725 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1726 err = -EMSGSIZE;
1727 goto out_free;
1730 if (sinfo) {
1731 /* Check for invalid stream. */
1732 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1733 err = -EINVAL;
1734 goto out_free;
1738 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1739 if (!sctp_wspace(asoc)) {
1740 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1741 if (err)
1742 goto out_free;
1745 /* If an address is passed with the sendto/sendmsg call, it is used
1746 * to override the primary destination address in the TCP model, or
1747 * when SCTP_ADDR_OVER flag is set in the UDP model.
1749 if ((sctp_style(sk, TCP) && msg_name) ||
1750 (sinfo_flags & SCTP_ADDR_OVER)) {
1751 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1752 if (!chunk_tp) {
1753 err = -EINVAL;
1754 goto out_free;
1756 } else
1757 chunk_tp = NULL;
1759 /* Auto-connect, if we aren't connected already. */
1760 if (sctp_state(asoc, CLOSED)) {
1761 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1762 if (err < 0)
1763 goto out_free;
1764 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1767 /* Break the message into multiple chunks of maximum size. */
1768 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1769 if (!datamsg) {
1770 err = -ENOMEM;
1771 goto out_free;
1774 /* Now send the (possibly) fragmented message. */
1775 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1776 sctp_chunk_hold(chunk);
1778 /* Do accounting for the write space. */
1779 sctp_set_owner_w(chunk);
1781 chunk->transport = chunk_tp;
1784 /* Send it to the lower layers. Note: all chunks
1785 * must either fail or succeed. The lower layer
1786 * works that way today. Keep it that way or this
1787 * breaks.
1789 err = sctp_primitive_SEND(asoc, datamsg);
1790 /* Did the lower layer accept the chunk? */
1791 if (err)
1792 sctp_datamsg_free(datamsg);
1793 else
1794 sctp_datamsg_put(datamsg);
1796 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1798 if (err)
1799 goto out_free;
1800 else
1801 err = msg_len;
1803 /* If we are already past ASSOCIATE, the lower
1804 * layers are responsible for association cleanup.
1806 goto out_unlock;
1808 out_free:
1809 if (new_asoc)
1810 sctp_association_free(asoc);
1811 out_unlock:
1812 sctp_release_sock(sk);
1814 out_nounlock:
1815 return sctp_error(sk, msg_flags, err);
1819 /* This is an extended version of skb_pull() that removes the data from the
1820 * start of a skb even when data is spread across the list of skb's in the
1821 * frag_list. len specifies the total amount of data that needs to be removed.
1822 * when 'len' bytes could be removed from the skb, it returns 0.
1823 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1824 * could not be removed.
1826 static int sctp_skb_pull(struct sk_buff *skb, int len)
1828 struct sk_buff *list;
1829 int skb_len = skb_headlen(skb);
1830 int rlen;
1832 if (len <= skb_len) {
1833 __skb_pull(skb, len);
1834 return 0;
1836 len -= skb_len;
1837 __skb_pull(skb, skb_len);
1839 skb_walk_frags(skb, list) {
1840 rlen = sctp_skb_pull(list, len);
1841 skb->len -= (len-rlen);
1842 skb->data_len -= (len-rlen);
1844 if (!rlen)
1845 return 0;
1847 len = rlen;
1850 return len;
1853 /* API 3.1.3 recvmsg() - UDP Style Syntax
1855 * ssize_t recvmsg(int socket, struct msghdr *message,
1856 * int flags);
1858 * socket - the socket descriptor of the endpoint.
1859 * message - pointer to the msghdr structure which contains a single
1860 * user message and possibly some ancillary data.
1862 * See Section 5 for complete description of the data
1863 * structures.
1865 * flags - flags sent or received with the user message, see Section
1866 * 5 for complete description of the flags.
1868 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1870 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1871 struct msghdr *msg, size_t len, int noblock,
1872 int flags, int *addr_len)
1874 struct sctp_ulpevent *event = NULL;
1875 struct sctp_sock *sp = sctp_sk(sk);
1876 struct sk_buff *skb;
1877 int copied;
1878 int err = 0;
1879 int skb_len;
1881 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1882 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1883 "len", len, "knoblauch", noblock,
1884 "flags", flags, "addr_len", addr_len);
1886 sctp_lock_sock(sk);
1888 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1889 err = -ENOTCONN;
1890 goto out;
1893 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1894 if (!skb)
1895 goto out;
1897 /* Get the total length of the skb including any skb's in the
1898 * frag_list.
1900 skb_len = skb->len;
1902 copied = skb_len;
1903 if (copied > len)
1904 copied = len;
1906 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1908 event = sctp_skb2event(skb);
1910 if (err)
1911 goto out_free;
1913 sock_recv_ts_and_drops(msg, sk, skb);
1914 if (sctp_ulpevent_is_notification(event)) {
1915 msg->msg_flags |= MSG_NOTIFICATION;
1916 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1917 } else {
1918 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1921 /* Check if we allow SCTP_SNDRCVINFO. */
1922 if (sp->subscribe.sctp_data_io_event)
1923 sctp_ulpevent_read_sndrcvinfo(event, msg);
1925 err = copied;
1927 /* If skb's length exceeds the user's buffer, update the skb and
1928 * push it back to the receive_queue so that the next call to
1929 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1931 if (skb_len > copied) {
1932 msg->msg_flags &= ~MSG_EOR;
1933 if (flags & MSG_PEEK)
1934 goto out_free;
1935 sctp_skb_pull(skb, copied);
1936 skb_queue_head(&sk->sk_receive_queue, skb);
1938 /* When only partial message is copied to the user, increase
1939 * rwnd by that amount. If all the data in the skb is read,
1940 * rwnd is updated when the event is freed.
1942 if (!sctp_ulpevent_is_notification(event))
1943 sctp_assoc_rwnd_increase(event->asoc, copied);
1944 goto out;
1945 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1946 (event->msg_flags & MSG_EOR))
1947 msg->msg_flags |= MSG_EOR;
1948 else
1949 msg->msg_flags &= ~MSG_EOR;
1951 out_free:
1952 if (flags & MSG_PEEK) {
1953 /* Release the skb reference acquired after peeking the skb in
1954 * sctp_skb_recv_datagram().
1956 kfree_skb(skb);
1957 } else {
1958 /* Free the event which includes releasing the reference to
1959 * the owner of the skb, freeing the skb and updating the
1960 * rwnd.
1962 sctp_ulpevent_free(event);
1964 out:
1965 sctp_release_sock(sk);
1966 return err;
1969 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1971 * This option is a on/off flag. If enabled no SCTP message
1972 * fragmentation will be performed. Instead if a message being sent
1973 * exceeds the current PMTU size, the message will NOT be sent and
1974 * instead a error will be indicated to the user.
1976 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1977 char __user *optval,
1978 unsigned int optlen)
1980 int val;
1982 if (optlen < sizeof(int))
1983 return -EINVAL;
1985 if (get_user(val, (int __user *)optval))
1986 return -EFAULT;
1988 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1990 return 0;
1993 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1994 unsigned int optlen)
1996 if (optlen > sizeof(struct sctp_event_subscribe))
1997 return -EINVAL;
1998 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1999 return -EFAULT;
2000 return 0;
2003 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2005 * This socket option is applicable to the UDP-style socket only. When
2006 * set it will cause associations that are idle for more than the
2007 * specified number of seconds to automatically close. An association
2008 * being idle is defined an association that has NOT sent or received
2009 * user data. The special value of '0' indicates that no automatic
2010 * close of any associations should be performed. The option expects an
2011 * integer defining the number of seconds of idle time before an
2012 * association is closed.
2014 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2015 unsigned int optlen)
2017 struct sctp_sock *sp = sctp_sk(sk);
2019 /* Applicable to UDP-style socket only */
2020 if (sctp_style(sk, TCP))
2021 return -EOPNOTSUPP;
2022 if (optlen != sizeof(int))
2023 return -EINVAL;
2024 if (copy_from_user(&sp->autoclose, optval, optlen))
2025 return -EFAULT;
2026 /* make sure it won't exceed MAX_SCHEDULE_TIMEOUT */
2027 sp->autoclose = min_t(long, sp->autoclose, MAX_SCHEDULE_TIMEOUT / HZ);
2029 return 0;
2032 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2034 * Applications can enable or disable heartbeats for any peer address of
2035 * an association, modify an address's heartbeat interval, force a
2036 * heartbeat to be sent immediately, and adjust the address's maximum
2037 * number of retransmissions sent before an address is considered
2038 * unreachable. The following structure is used to access and modify an
2039 * address's parameters:
2041 * struct sctp_paddrparams {
2042 * sctp_assoc_t spp_assoc_id;
2043 * struct sockaddr_storage spp_address;
2044 * uint32_t spp_hbinterval;
2045 * uint16_t spp_pathmaxrxt;
2046 * uint32_t spp_pathmtu;
2047 * uint32_t spp_sackdelay;
2048 * uint32_t spp_flags;
2049 * };
2051 * spp_assoc_id - (one-to-many style socket) This is filled in the
2052 * application, and identifies the association for
2053 * this query.
2054 * spp_address - This specifies which address is of interest.
2055 * spp_hbinterval - This contains the value of the heartbeat interval,
2056 * in milliseconds. If a value of zero
2057 * is present in this field then no changes are to
2058 * be made to this parameter.
2059 * spp_pathmaxrxt - This contains the maximum number of
2060 * retransmissions before this address shall be
2061 * considered unreachable. If a value of zero
2062 * is present in this field then no changes are to
2063 * be made to this parameter.
2064 * spp_pathmtu - When Path MTU discovery is disabled the value
2065 * specified here will be the "fixed" path mtu.
2066 * Note that if the spp_address field is empty
2067 * then all associations on this address will
2068 * have this fixed path mtu set upon them.
2070 * spp_sackdelay - When delayed sack is enabled, this value specifies
2071 * the number of milliseconds that sacks will be delayed
2072 * for. This value will apply to all addresses of an
2073 * association if the spp_address field is empty. Note
2074 * also, that if delayed sack is enabled and this
2075 * value is set to 0, no change is made to the last
2076 * recorded delayed sack timer value.
2078 * spp_flags - These flags are used to control various features
2079 * on an association. The flag field may contain
2080 * zero or more of the following options.
2082 * SPP_HB_ENABLE - Enable heartbeats on the
2083 * specified address. Note that if the address
2084 * field is empty all addresses for the association
2085 * have heartbeats enabled upon them.
2087 * SPP_HB_DISABLE - Disable heartbeats on the
2088 * speicifed address. Note that if the address
2089 * field is empty all addresses for the association
2090 * will have their heartbeats disabled. Note also
2091 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2092 * mutually exclusive, only one of these two should
2093 * be specified. Enabling both fields will have
2094 * undetermined results.
2096 * SPP_HB_DEMAND - Request a user initiated heartbeat
2097 * to be made immediately.
2099 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2100 * heartbeat delayis to be set to the value of 0
2101 * milliseconds.
2103 * SPP_PMTUD_ENABLE - This field will enable PMTU
2104 * discovery upon the specified address. Note that
2105 * if the address feild is empty then all addresses
2106 * on the association are effected.
2108 * SPP_PMTUD_DISABLE - This field will disable PMTU
2109 * discovery upon the specified address. Note that
2110 * if the address feild is empty then all addresses
2111 * on the association are effected. Not also that
2112 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2113 * exclusive. Enabling both will have undetermined
2114 * results.
2116 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2117 * on delayed sack. The time specified in spp_sackdelay
2118 * is used to specify the sack delay for this address. Note
2119 * that if spp_address is empty then all addresses will
2120 * enable delayed sack and take on the sack delay
2121 * value specified in spp_sackdelay.
2122 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2123 * off delayed sack. If the spp_address field is blank then
2124 * delayed sack is disabled for the entire association. Note
2125 * also that this field is mutually exclusive to
2126 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2127 * results.
2129 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2130 struct sctp_transport *trans,
2131 struct sctp_association *asoc,
2132 struct sctp_sock *sp,
2133 int hb_change,
2134 int pmtud_change,
2135 int sackdelay_change)
2137 int error;
2139 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2140 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2141 if (error)
2142 return error;
2145 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2146 * this field is ignored. Note also that a value of zero indicates
2147 * the current setting should be left unchanged.
2149 if (params->spp_flags & SPP_HB_ENABLE) {
2151 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2152 * set. This lets us use 0 value when this flag
2153 * is set.
2155 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2156 params->spp_hbinterval = 0;
2158 if (params->spp_hbinterval ||
2159 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2160 if (trans) {
2161 trans->hbinterval =
2162 msecs_to_jiffies(params->spp_hbinterval);
2163 } else if (asoc) {
2164 asoc->hbinterval =
2165 msecs_to_jiffies(params->spp_hbinterval);
2166 } else {
2167 sp->hbinterval = params->spp_hbinterval;
2172 if (hb_change) {
2173 if (trans) {
2174 trans->param_flags =
2175 (trans->param_flags & ~SPP_HB) | hb_change;
2176 } else if (asoc) {
2177 asoc->param_flags =
2178 (asoc->param_flags & ~SPP_HB) | hb_change;
2179 } else {
2180 sp->param_flags =
2181 (sp->param_flags & ~SPP_HB) | hb_change;
2185 /* When Path MTU discovery is disabled the value specified here will
2186 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2187 * include the flag SPP_PMTUD_DISABLE for this field to have any
2188 * effect).
2190 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2191 if (trans) {
2192 trans->pathmtu = params->spp_pathmtu;
2193 sctp_assoc_sync_pmtu(asoc);
2194 } else if (asoc) {
2195 asoc->pathmtu = params->spp_pathmtu;
2196 sctp_frag_point(asoc, params->spp_pathmtu);
2197 } else {
2198 sp->pathmtu = params->spp_pathmtu;
2202 if (pmtud_change) {
2203 if (trans) {
2204 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2205 (params->spp_flags & SPP_PMTUD_ENABLE);
2206 trans->param_flags =
2207 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2208 if (update) {
2209 sctp_transport_pmtu(trans);
2210 sctp_assoc_sync_pmtu(asoc);
2212 } else if (asoc) {
2213 asoc->param_flags =
2214 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2215 } else {
2216 sp->param_flags =
2217 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2221 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2222 * value of this field is ignored. Note also that a value of zero
2223 * indicates the current setting should be left unchanged.
2225 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2226 if (trans) {
2227 trans->sackdelay =
2228 msecs_to_jiffies(params->spp_sackdelay);
2229 } else if (asoc) {
2230 asoc->sackdelay =
2231 msecs_to_jiffies(params->spp_sackdelay);
2232 } else {
2233 sp->sackdelay = params->spp_sackdelay;
2237 if (sackdelay_change) {
2238 if (trans) {
2239 trans->param_flags =
2240 (trans->param_flags & ~SPP_SACKDELAY) |
2241 sackdelay_change;
2242 } else if (asoc) {
2243 asoc->param_flags =
2244 (asoc->param_flags & ~SPP_SACKDELAY) |
2245 sackdelay_change;
2246 } else {
2247 sp->param_flags =
2248 (sp->param_flags & ~SPP_SACKDELAY) |
2249 sackdelay_change;
2253 /* Note that a value of zero indicates the current setting should be
2254 left unchanged.
2256 if (params->spp_pathmaxrxt) {
2257 if (trans) {
2258 trans->pathmaxrxt = params->spp_pathmaxrxt;
2259 } else if (asoc) {
2260 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2261 } else {
2262 sp->pathmaxrxt = params->spp_pathmaxrxt;
2266 return 0;
2269 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2270 char __user *optval,
2271 unsigned int optlen)
2273 struct sctp_paddrparams params;
2274 struct sctp_transport *trans = NULL;
2275 struct sctp_association *asoc = NULL;
2276 struct sctp_sock *sp = sctp_sk(sk);
2277 int error;
2278 int hb_change, pmtud_change, sackdelay_change;
2280 if (optlen != sizeof(struct sctp_paddrparams))
2281 return - EINVAL;
2283 if (copy_from_user(&params, optval, optlen))
2284 return -EFAULT;
2286 /* Validate flags and value parameters. */
2287 hb_change = params.spp_flags & SPP_HB;
2288 pmtud_change = params.spp_flags & SPP_PMTUD;
2289 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2291 if (hb_change == SPP_HB ||
2292 pmtud_change == SPP_PMTUD ||
2293 sackdelay_change == SPP_SACKDELAY ||
2294 params.spp_sackdelay > 500 ||
2295 (params.spp_pathmtu &&
2296 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2297 return -EINVAL;
2299 /* If an address other than INADDR_ANY is specified, and
2300 * no transport is found, then the request is invalid.
2302 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
2303 trans = sctp_addr_id2transport(sk, &params.spp_address,
2304 params.spp_assoc_id);
2305 if (!trans)
2306 return -EINVAL;
2309 /* Get association, if assoc_id != 0 and the socket is a one
2310 * to many style socket, and an association was not found, then
2311 * the id was invalid.
2313 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2314 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2315 return -EINVAL;
2317 /* Heartbeat demand can only be sent on a transport or
2318 * association, but not a socket.
2320 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2321 return -EINVAL;
2323 /* Process parameters. */
2324 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2325 hb_change, pmtud_change,
2326 sackdelay_change);
2328 if (error)
2329 return error;
2331 /* If changes are for association, also apply parameters to each
2332 * transport.
2334 if (!trans && asoc) {
2335 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2336 transports) {
2337 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2338 hb_change, pmtud_change,
2339 sackdelay_change);
2343 return 0;
2347 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2349 * This option will effect the way delayed acks are performed. This
2350 * option allows you to get or set the delayed ack time, in
2351 * milliseconds. It also allows changing the delayed ack frequency.
2352 * Changing the frequency to 1 disables the delayed sack algorithm. If
2353 * the assoc_id is 0, then this sets or gets the endpoints default
2354 * values. If the assoc_id field is non-zero, then the set or get
2355 * effects the specified association for the one to many model (the
2356 * assoc_id field is ignored by the one to one model). Note that if
2357 * sack_delay or sack_freq are 0 when setting this option, then the
2358 * current values will remain unchanged.
2360 * struct sctp_sack_info {
2361 * sctp_assoc_t sack_assoc_id;
2362 * uint32_t sack_delay;
2363 * uint32_t sack_freq;
2364 * };
2366 * sack_assoc_id - This parameter, indicates which association the user
2367 * is performing an action upon. Note that if this field's value is
2368 * zero then the endpoints default value is changed (effecting future
2369 * associations only).
2371 * sack_delay - This parameter contains the number of milliseconds that
2372 * the user is requesting the delayed ACK timer be set to. Note that
2373 * this value is defined in the standard to be between 200 and 500
2374 * milliseconds.
2376 * sack_freq - This parameter contains the number of packets that must
2377 * be received before a sack is sent without waiting for the delay
2378 * timer to expire. The default value for this is 2, setting this
2379 * value to 1 will disable the delayed sack algorithm.
2382 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2383 char __user *optval, unsigned int optlen)
2385 struct sctp_sack_info params;
2386 struct sctp_transport *trans = NULL;
2387 struct sctp_association *asoc = NULL;
2388 struct sctp_sock *sp = sctp_sk(sk);
2390 if (optlen == sizeof(struct sctp_sack_info)) {
2391 if (copy_from_user(&params, optval, optlen))
2392 return -EFAULT;
2394 if (params.sack_delay == 0 && params.sack_freq == 0)
2395 return 0;
2396 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2397 printk(KERN_WARNING "SCTP: Use of struct sctp_assoc_value "
2398 "in delayed_ack socket option deprecated\n");
2399 printk(KERN_WARNING "SCTP: Use struct sctp_sack_info instead\n");
2400 if (copy_from_user(&params, optval, optlen))
2401 return -EFAULT;
2403 if (params.sack_delay == 0)
2404 params.sack_freq = 1;
2405 else
2406 params.sack_freq = 0;
2407 } else
2408 return - EINVAL;
2410 /* Validate value parameter. */
2411 if (params.sack_delay > 500)
2412 return -EINVAL;
2414 /* Get association, if sack_assoc_id != 0 and the socket is a one
2415 * to many style socket, and an association was not found, then
2416 * the id was invalid.
2418 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2419 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2420 return -EINVAL;
2422 if (params.sack_delay) {
2423 if (asoc) {
2424 asoc->sackdelay =
2425 msecs_to_jiffies(params.sack_delay);
2426 asoc->param_flags =
2427 (asoc->param_flags & ~SPP_SACKDELAY) |
2428 SPP_SACKDELAY_ENABLE;
2429 } else {
2430 sp->sackdelay = params.sack_delay;
2431 sp->param_flags =
2432 (sp->param_flags & ~SPP_SACKDELAY) |
2433 SPP_SACKDELAY_ENABLE;
2437 if (params.sack_freq == 1) {
2438 if (asoc) {
2439 asoc->param_flags =
2440 (asoc->param_flags & ~SPP_SACKDELAY) |
2441 SPP_SACKDELAY_DISABLE;
2442 } else {
2443 sp->param_flags =
2444 (sp->param_flags & ~SPP_SACKDELAY) |
2445 SPP_SACKDELAY_DISABLE;
2447 } else if (params.sack_freq > 1) {
2448 if (asoc) {
2449 asoc->sackfreq = params.sack_freq;
2450 asoc->param_flags =
2451 (asoc->param_flags & ~SPP_SACKDELAY) |
2452 SPP_SACKDELAY_ENABLE;
2453 } else {
2454 sp->sackfreq = params.sack_freq;
2455 sp->param_flags =
2456 (sp->param_flags & ~SPP_SACKDELAY) |
2457 SPP_SACKDELAY_ENABLE;
2461 /* If change is for association, also apply to each transport. */
2462 if (asoc) {
2463 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2464 transports) {
2465 if (params.sack_delay) {
2466 trans->sackdelay =
2467 msecs_to_jiffies(params.sack_delay);
2468 trans->param_flags =
2469 (trans->param_flags & ~SPP_SACKDELAY) |
2470 SPP_SACKDELAY_ENABLE;
2472 if (params.sack_freq == 1) {
2473 trans->param_flags =
2474 (trans->param_flags & ~SPP_SACKDELAY) |
2475 SPP_SACKDELAY_DISABLE;
2476 } else if (params.sack_freq > 1) {
2477 trans->sackfreq = params.sack_freq;
2478 trans->param_flags =
2479 (trans->param_flags & ~SPP_SACKDELAY) |
2480 SPP_SACKDELAY_ENABLE;
2485 return 0;
2488 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2490 * Applications can specify protocol parameters for the default association
2491 * initialization. The option name argument to setsockopt() and getsockopt()
2492 * is SCTP_INITMSG.
2494 * Setting initialization parameters is effective only on an unconnected
2495 * socket (for UDP-style sockets only future associations are effected
2496 * by the change). With TCP-style sockets, this option is inherited by
2497 * sockets derived from a listener socket.
2499 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2501 struct sctp_initmsg sinit;
2502 struct sctp_sock *sp = sctp_sk(sk);
2504 if (optlen != sizeof(struct sctp_initmsg))
2505 return -EINVAL;
2506 if (copy_from_user(&sinit, optval, optlen))
2507 return -EFAULT;
2509 if (sinit.sinit_num_ostreams)
2510 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2511 if (sinit.sinit_max_instreams)
2512 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2513 if (sinit.sinit_max_attempts)
2514 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2515 if (sinit.sinit_max_init_timeo)
2516 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2518 return 0;
2522 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2524 * Applications that wish to use the sendto() system call may wish to
2525 * specify a default set of parameters that would normally be supplied
2526 * through the inclusion of ancillary data. This socket option allows
2527 * such an application to set the default sctp_sndrcvinfo structure.
2528 * The application that wishes to use this socket option simply passes
2529 * in to this call the sctp_sndrcvinfo structure defined in Section
2530 * 5.2.2) The input parameters accepted by this call include
2531 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2532 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2533 * to this call if the caller is using the UDP model.
2535 static int sctp_setsockopt_default_send_param(struct sock *sk,
2536 char __user *optval,
2537 unsigned int optlen)
2539 struct sctp_sndrcvinfo info;
2540 struct sctp_association *asoc;
2541 struct sctp_sock *sp = sctp_sk(sk);
2543 if (optlen != sizeof(struct sctp_sndrcvinfo))
2544 return -EINVAL;
2545 if (copy_from_user(&info, optval, optlen))
2546 return -EFAULT;
2548 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2549 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2550 return -EINVAL;
2552 if (asoc) {
2553 asoc->default_stream = info.sinfo_stream;
2554 asoc->default_flags = info.sinfo_flags;
2555 asoc->default_ppid = info.sinfo_ppid;
2556 asoc->default_context = info.sinfo_context;
2557 asoc->default_timetolive = info.sinfo_timetolive;
2558 } else {
2559 sp->default_stream = info.sinfo_stream;
2560 sp->default_flags = info.sinfo_flags;
2561 sp->default_ppid = info.sinfo_ppid;
2562 sp->default_context = info.sinfo_context;
2563 sp->default_timetolive = info.sinfo_timetolive;
2566 return 0;
2569 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2571 * Requests that the local SCTP stack use the enclosed peer address as
2572 * the association primary. The enclosed address must be one of the
2573 * association peer's addresses.
2575 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2576 unsigned int optlen)
2578 struct sctp_prim prim;
2579 struct sctp_transport *trans;
2581 if (optlen != sizeof(struct sctp_prim))
2582 return -EINVAL;
2584 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2585 return -EFAULT;
2587 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2588 if (!trans)
2589 return -EINVAL;
2591 sctp_assoc_set_primary(trans->asoc, trans);
2593 return 0;
2597 * 7.1.5 SCTP_NODELAY
2599 * Turn on/off any Nagle-like algorithm. This means that packets are
2600 * generally sent as soon as possible and no unnecessary delays are
2601 * introduced, at the cost of more packets in the network. Expects an
2602 * integer boolean flag.
2604 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2605 unsigned int optlen)
2607 int val;
2609 if (optlen < sizeof(int))
2610 return -EINVAL;
2611 if (get_user(val, (int __user *)optval))
2612 return -EFAULT;
2614 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2615 return 0;
2620 * 7.1.1 SCTP_RTOINFO
2622 * The protocol parameters used to initialize and bound retransmission
2623 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2624 * and modify these parameters.
2625 * All parameters are time values, in milliseconds. A value of 0, when
2626 * modifying the parameters, indicates that the current value should not
2627 * be changed.
2630 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2632 struct sctp_rtoinfo rtoinfo;
2633 struct sctp_association *asoc;
2635 if (optlen != sizeof (struct sctp_rtoinfo))
2636 return -EINVAL;
2638 if (copy_from_user(&rtoinfo, optval, optlen))
2639 return -EFAULT;
2641 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2643 /* Set the values to the specific association */
2644 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2645 return -EINVAL;
2647 if (asoc) {
2648 if (rtoinfo.srto_initial != 0)
2649 asoc->rto_initial =
2650 msecs_to_jiffies(rtoinfo.srto_initial);
2651 if (rtoinfo.srto_max != 0)
2652 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2653 if (rtoinfo.srto_min != 0)
2654 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2655 } else {
2656 /* If there is no association or the association-id = 0
2657 * set the values to the endpoint.
2659 struct sctp_sock *sp = sctp_sk(sk);
2661 if (rtoinfo.srto_initial != 0)
2662 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2663 if (rtoinfo.srto_max != 0)
2664 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2665 if (rtoinfo.srto_min != 0)
2666 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2669 return 0;
2674 * 7.1.2 SCTP_ASSOCINFO
2676 * This option is used to tune the maximum retransmission attempts
2677 * of the association.
2678 * Returns an error if the new association retransmission value is
2679 * greater than the sum of the retransmission value of the peer.
2680 * See [SCTP] for more information.
2683 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
2686 struct sctp_assocparams assocparams;
2687 struct sctp_association *asoc;
2689 if (optlen != sizeof(struct sctp_assocparams))
2690 return -EINVAL;
2691 if (copy_from_user(&assocparams, optval, optlen))
2692 return -EFAULT;
2694 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2696 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2697 return -EINVAL;
2699 /* Set the values to the specific association */
2700 if (asoc) {
2701 if (assocparams.sasoc_asocmaxrxt != 0) {
2702 __u32 path_sum = 0;
2703 int paths = 0;
2704 struct sctp_transport *peer_addr;
2706 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2707 transports) {
2708 path_sum += peer_addr->pathmaxrxt;
2709 paths++;
2712 /* Only validate asocmaxrxt if we have more than
2713 * one path/transport. We do this because path
2714 * retransmissions are only counted when we have more
2715 * then one path.
2717 if (paths > 1 &&
2718 assocparams.sasoc_asocmaxrxt > path_sum)
2719 return -EINVAL;
2721 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2724 if (assocparams.sasoc_cookie_life != 0) {
2725 asoc->cookie_life.tv_sec =
2726 assocparams.sasoc_cookie_life / 1000;
2727 asoc->cookie_life.tv_usec =
2728 (assocparams.sasoc_cookie_life % 1000)
2729 * 1000;
2731 } else {
2732 /* Set the values to the endpoint */
2733 struct sctp_sock *sp = sctp_sk(sk);
2735 if (assocparams.sasoc_asocmaxrxt != 0)
2736 sp->assocparams.sasoc_asocmaxrxt =
2737 assocparams.sasoc_asocmaxrxt;
2738 if (assocparams.sasoc_cookie_life != 0)
2739 sp->assocparams.sasoc_cookie_life =
2740 assocparams.sasoc_cookie_life;
2742 return 0;
2746 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2748 * This socket option is a boolean flag which turns on or off mapped V4
2749 * addresses. If this option is turned on and the socket is type
2750 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2751 * If this option is turned off, then no mapping will be done of V4
2752 * addresses and a user will receive both PF_INET6 and PF_INET type
2753 * addresses on the socket.
2755 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
2757 int val;
2758 struct sctp_sock *sp = sctp_sk(sk);
2760 if (optlen < sizeof(int))
2761 return -EINVAL;
2762 if (get_user(val, (int __user *)optval))
2763 return -EFAULT;
2764 if (val)
2765 sp->v4mapped = 1;
2766 else
2767 sp->v4mapped = 0;
2769 return 0;
2773 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2774 * This option will get or set the maximum size to put in any outgoing
2775 * SCTP DATA chunk. If a message is larger than this size it will be
2776 * fragmented by SCTP into the specified size. Note that the underlying
2777 * SCTP implementation may fragment into smaller sized chunks when the
2778 * PMTU of the underlying association is smaller than the value set by
2779 * the user. The default value for this option is '0' which indicates
2780 * the user is NOT limiting fragmentation and only the PMTU will effect
2781 * SCTP's choice of DATA chunk size. Note also that values set larger
2782 * than the maximum size of an IP datagram will effectively let SCTP
2783 * control fragmentation (i.e. the same as setting this option to 0).
2785 * The following structure is used to access and modify this parameter:
2787 * struct sctp_assoc_value {
2788 * sctp_assoc_t assoc_id;
2789 * uint32_t assoc_value;
2790 * };
2792 * assoc_id: This parameter is ignored for one-to-one style sockets.
2793 * For one-to-many style sockets this parameter indicates which
2794 * association the user is performing an action upon. Note that if
2795 * this field's value is zero then the endpoints default value is
2796 * changed (effecting future associations only).
2797 * assoc_value: This parameter specifies the maximum size in bytes.
2799 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
2801 struct sctp_assoc_value params;
2802 struct sctp_association *asoc;
2803 struct sctp_sock *sp = sctp_sk(sk);
2804 int val;
2806 if (optlen == sizeof(int)) {
2807 printk(KERN_WARNING
2808 "SCTP: Use of int in maxseg socket option deprecated\n");
2809 printk(KERN_WARNING
2810 "SCTP: Use struct sctp_assoc_value instead\n");
2811 if (copy_from_user(&val, optval, optlen))
2812 return -EFAULT;
2813 params.assoc_id = 0;
2814 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2815 if (copy_from_user(&params, optval, optlen))
2816 return -EFAULT;
2817 val = params.assoc_value;
2818 } else
2819 return -EINVAL;
2821 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2822 return -EINVAL;
2824 asoc = sctp_id2assoc(sk, params.assoc_id);
2825 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2826 return -EINVAL;
2828 if (asoc) {
2829 if (val == 0) {
2830 val = asoc->pathmtu;
2831 val -= sp->pf->af->net_header_len;
2832 val -= sizeof(struct sctphdr) +
2833 sizeof(struct sctp_data_chunk);
2835 asoc->user_frag = val;
2836 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
2837 } else {
2838 sp->user_frag = val;
2841 return 0;
2846 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2848 * Requests that the peer mark the enclosed address as the association
2849 * primary. The enclosed address must be one of the association's
2850 * locally bound addresses. The following structure is used to make a
2851 * set primary request:
2853 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2854 unsigned int optlen)
2856 struct sctp_sock *sp;
2857 struct sctp_endpoint *ep;
2858 struct sctp_association *asoc = NULL;
2859 struct sctp_setpeerprim prim;
2860 struct sctp_chunk *chunk;
2861 int err;
2863 sp = sctp_sk(sk);
2864 ep = sp->ep;
2866 if (!sctp_addip_enable)
2867 return -EPERM;
2869 if (optlen != sizeof(struct sctp_setpeerprim))
2870 return -EINVAL;
2872 if (copy_from_user(&prim, optval, optlen))
2873 return -EFAULT;
2875 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2876 if (!asoc)
2877 return -EINVAL;
2879 if (!asoc->peer.asconf_capable)
2880 return -EPERM;
2882 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2883 return -EPERM;
2885 if (!sctp_state(asoc, ESTABLISHED))
2886 return -ENOTCONN;
2888 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2889 return -EADDRNOTAVAIL;
2891 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2892 chunk = sctp_make_asconf_set_prim(asoc,
2893 (union sctp_addr *)&prim.sspp_addr);
2894 if (!chunk)
2895 return -ENOMEM;
2897 err = sctp_send_asconf(asoc, chunk);
2899 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2901 return err;
2904 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2905 unsigned int optlen)
2907 struct sctp_setadaptation adaptation;
2909 if (optlen != sizeof(struct sctp_setadaptation))
2910 return -EINVAL;
2911 if (copy_from_user(&adaptation, optval, optlen))
2912 return -EFAULT;
2914 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2916 return 0;
2920 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2922 * The context field in the sctp_sndrcvinfo structure is normally only
2923 * used when a failed message is retrieved holding the value that was
2924 * sent down on the actual send call. This option allows the setting of
2925 * a default context on an association basis that will be received on
2926 * reading messages from the peer. This is especially helpful in the
2927 * one-2-many model for an application to keep some reference to an
2928 * internal state machine that is processing messages on the
2929 * association. Note that the setting of this value only effects
2930 * received messages from the peer and does not effect the value that is
2931 * saved with outbound messages.
2933 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2934 unsigned int optlen)
2936 struct sctp_assoc_value params;
2937 struct sctp_sock *sp;
2938 struct sctp_association *asoc;
2940 if (optlen != sizeof(struct sctp_assoc_value))
2941 return -EINVAL;
2942 if (copy_from_user(&params, optval, optlen))
2943 return -EFAULT;
2945 sp = sctp_sk(sk);
2947 if (params.assoc_id != 0) {
2948 asoc = sctp_id2assoc(sk, params.assoc_id);
2949 if (!asoc)
2950 return -EINVAL;
2951 asoc->default_rcv_context = params.assoc_value;
2952 } else {
2953 sp->default_rcv_context = params.assoc_value;
2956 return 0;
2960 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2962 * This options will at a minimum specify if the implementation is doing
2963 * fragmented interleave. Fragmented interleave, for a one to many
2964 * socket, is when subsequent calls to receive a message may return
2965 * parts of messages from different associations. Some implementations
2966 * may allow you to turn this value on or off. If so, when turned off,
2967 * no fragment interleave will occur (which will cause a head of line
2968 * blocking amongst multiple associations sharing the same one to many
2969 * socket). When this option is turned on, then each receive call may
2970 * come from a different association (thus the user must receive data
2971 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2972 * association each receive belongs to.
2974 * This option takes a boolean value. A non-zero value indicates that
2975 * fragmented interleave is on. A value of zero indicates that
2976 * fragmented interleave is off.
2978 * Note that it is important that an implementation that allows this
2979 * option to be turned on, have it off by default. Otherwise an unaware
2980 * application using the one to many model may become confused and act
2981 * incorrectly.
2983 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2984 char __user *optval,
2985 unsigned int optlen)
2987 int val;
2989 if (optlen != sizeof(int))
2990 return -EINVAL;
2991 if (get_user(val, (int __user *)optval))
2992 return -EFAULT;
2994 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2996 return 0;
3000 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3001 * (SCTP_PARTIAL_DELIVERY_POINT)
3003 * This option will set or get the SCTP partial delivery point. This
3004 * point is the size of a message where the partial delivery API will be
3005 * invoked to help free up rwnd space for the peer. Setting this to a
3006 * lower value will cause partial deliveries to happen more often. The
3007 * calls argument is an integer that sets or gets the partial delivery
3008 * point. Note also that the call will fail if the user attempts to set
3009 * this value larger than the socket receive buffer size.
3011 * Note that any single message having a length smaller than or equal to
3012 * the SCTP partial delivery point will be delivered in one single read
3013 * call as long as the user provided buffer is large enough to hold the
3014 * message.
3016 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3017 char __user *optval,
3018 unsigned int optlen)
3020 u32 val;
3022 if (optlen != sizeof(u32))
3023 return -EINVAL;
3024 if (get_user(val, (int __user *)optval))
3025 return -EFAULT;
3027 /* Note: We double the receive buffer from what the user sets
3028 * it to be, also initial rwnd is based on rcvbuf/2.
3030 if (val > (sk->sk_rcvbuf >> 1))
3031 return -EINVAL;
3033 sctp_sk(sk)->pd_point = val;
3035 return 0; /* is this the right error code? */
3039 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3041 * This option will allow a user to change the maximum burst of packets
3042 * that can be emitted by this association. Note that the default value
3043 * is 4, and some implementations may restrict this setting so that it
3044 * can only be lowered.
3046 * NOTE: This text doesn't seem right. Do this on a socket basis with
3047 * future associations inheriting the socket value.
3049 static int sctp_setsockopt_maxburst(struct sock *sk,
3050 char __user *optval,
3051 unsigned int optlen)
3053 struct sctp_assoc_value params;
3054 struct sctp_sock *sp;
3055 struct sctp_association *asoc;
3056 int val;
3057 int assoc_id = 0;
3059 if (optlen == sizeof(int)) {
3060 printk(KERN_WARNING
3061 "SCTP: Use of int in max_burst socket option deprecated\n");
3062 printk(KERN_WARNING
3063 "SCTP: Use struct sctp_assoc_value instead\n");
3064 if (copy_from_user(&val, optval, optlen))
3065 return -EFAULT;
3066 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3067 if (copy_from_user(&params, optval, optlen))
3068 return -EFAULT;
3069 val = params.assoc_value;
3070 assoc_id = params.assoc_id;
3071 } else
3072 return -EINVAL;
3074 sp = sctp_sk(sk);
3076 if (assoc_id != 0) {
3077 asoc = sctp_id2assoc(sk, assoc_id);
3078 if (!asoc)
3079 return -EINVAL;
3080 asoc->max_burst = val;
3081 } else
3082 sp->max_burst = val;
3084 return 0;
3088 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3090 * This set option adds a chunk type that the user is requesting to be
3091 * received only in an authenticated way. Changes to the list of chunks
3092 * will only effect future associations on the socket.
3094 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3095 char __user *optval,
3096 unsigned int optlen)
3098 struct sctp_authchunk val;
3100 if (!sctp_auth_enable)
3101 return -EACCES;
3103 if (optlen != sizeof(struct sctp_authchunk))
3104 return -EINVAL;
3105 if (copy_from_user(&val, optval, optlen))
3106 return -EFAULT;
3108 switch (val.sauth_chunk) {
3109 case SCTP_CID_INIT:
3110 case SCTP_CID_INIT_ACK:
3111 case SCTP_CID_SHUTDOWN_COMPLETE:
3112 case SCTP_CID_AUTH:
3113 return -EINVAL;
3116 /* add this chunk id to the endpoint */
3117 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3121 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3123 * This option gets or sets the list of HMAC algorithms that the local
3124 * endpoint requires the peer to use.
3126 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3127 char __user *optval,
3128 unsigned int optlen)
3130 struct sctp_hmacalgo *hmacs;
3131 u32 idents;
3132 int err;
3134 if (!sctp_auth_enable)
3135 return -EACCES;
3137 if (optlen < sizeof(struct sctp_hmacalgo))
3138 return -EINVAL;
3140 hmacs = kmalloc(optlen, GFP_KERNEL);
3141 if (!hmacs)
3142 return -ENOMEM;
3144 if (copy_from_user(hmacs, optval, optlen)) {
3145 err = -EFAULT;
3146 goto out;
3149 idents = hmacs->shmac_num_idents;
3150 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3151 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3152 err = -EINVAL;
3153 goto out;
3156 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3157 out:
3158 kfree(hmacs);
3159 return err;
3163 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3165 * This option will set a shared secret key which is used to build an
3166 * association shared key.
3168 static int sctp_setsockopt_auth_key(struct sock *sk,
3169 char __user *optval,
3170 unsigned int optlen)
3172 struct sctp_authkey *authkey;
3173 struct sctp_association *asoc;
3174 int ret;
3176 if (!sctp_auth_enable)
3177 return -EACCES;
3179 if (optlen <= sizeof(struct sctp_authkey))
3180 return -EINVAL;
3182 authkey = kmalloc(optlen, GFP_KERNEL);
3183 if (!authkey)
3184 return -ENOMEM;
3186 if (copy_from_user(authkey, optval, optlen)) {
3187 ret = -EFAULT;
3188 goto out;
3191 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3192 ret = -EINVAL;
3193 goto out;
3196 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3197 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3198 ret = -EINVAL;
3199 goto out;
3202 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3203 out:
3204 kfree(authkey);
3205 return ret;
3209 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3211 * This option will get or set the active shared key to be used to build
3212 * the association shared key.
3214 static int sctp_setsockopt_active_key(struct sock *sk,
3215 char __user *optval,
3216 unsigned int optlen)
3218 struct sctp_authkeyid val;
3219 struct sctp_association *asoc;
3221 if (!sctp_auth_enable)
3222 return -EACCES;
3224 if (optlen != sizeof(struct sctp_authkeyid))
3225 return -EINVAL;
3226 if (copy_from_user(&val, optval, optlen))
3227 return -EFAULT;
3229 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3230 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3231 return -EINVAL;
3233 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3234 val.scact_keynumber);
3238 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3240 * This set option will delete a shared secret key from use.
3242 static int sctp_setsockopt_del_key(struct sock *sk,
3243 char __user *optval,
3244 unsigned int optlen)
3246 struct sctp_authkeyid val;
3247 struct sctp_association *asoc;
3249 if (!sctp_auth_enable)
3250 return -EACCES;
3252 if (optlen != sizeof(struct sctp_authkeyid))
3253 return -EINVAL;
3254 if (copy_from_user(&val, optval, optlen))
3255 return -EFAULT;
3257 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3258 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3259 return -EINVAL;
3261 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3262 val.scact_keynumber);
3267 /* API 6.2 setsockopt(), getsockopt()
3269 * Applications use setsockopt() and getsockopt() to set or retrieve
3270 * socket options. Socket options are used to change the default
3271 * behavior of sockets calls. They are described in Section 7.
3273 * The syntax is:
3275 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3276 * int __user *optlen);
3277 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3278 * int optlen);
3280 * sd - the socket descript.
3281 * level - set to IPPROTO_SCTP for all SCTP options.
3282 * optname - the option name.
3283 * optval - the buffer to store the value of the option.
3284 * optlen - the size of the buffer.
3286 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
3287 char __user *optval, unsigned int optlen)
3289 int retval = 0;
3291 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3292 sk, optname);
3294 /* I can hardly begin to describe how wrong this is. This is
3295 * so broken as to be worse than useless. The API draft
3296 * REALLY is NOT helpful here... I am not convinced that the
3297 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3298 * are at all well-founded.
3300 if (level != SOL_SCTP) {
3301 struct sctp_af *af = sctp_sk(sk)->pf->af;
3302 retval = af->setsockopt(sk, level, optname, optval, optlen);
3303 goto out_nounlock;
3306 sctp_lock_sock(sk);
3308 switch (optname) {
3309 case SCTP_SOCKOPT_BINDX_ADD:
3310 /* 'optlen' is the size of the addresses buffer. */
3311 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3312 optlen, SCTP_BINDX_ADD_ADDR);
3313 break;
3315 case SCTP_SOCKOPT_BINDX_REM:
3316 /* 'optlen' is the size of the addresses buffer. */
3317 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3318 optlen, SCTP_BINDX_REM_ADDR);
3319 break;
3321 case SCTP_SOCKOPT_CONNECTX_OLD:
3322 /* 'optlen' is the size of the addresses buffer. */
3323 retval = sctp_setsockopt_connectx_old(sk,
3324 (struct sockaddr __user *)optval,
3325 optlen);
3326 break;
3328 case SCTP_SOCKOPT_CONNECTX:
3329 /* 'optlen' is the size of the addresses buffer. */
3330 retval = sctp_setsockopt_connectx(sk,
3331 (struct sockaddr __user *)optval,
3332 optlen);
3333 break;
3335 case SCTP_DISABLE_FRAGMENTS:
3336 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3337 break;
3339 case SCTP_EVENTS:
3340 retval = sctp_setsockopt_events(sk, optval, optlen);
3341 break;
3343 case SCTP_AUTOCLOSE:
3344 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3345 break;
3347 case SCTP_PEER_ADDR_PARAMS:
3348 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3349 break;
3351 case SCTP_DELAYED_ACK:
3352 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
3353 break;
3354 case SCTP_PARTIAL_DELIVERY_POINT:
3355 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3356 break;
3358 case SCTP_INITMSG:
3359 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3360 break;
3361 case SCTP_DEFAULT_SEND_PARAM:
3362 retval = sctp_setsockopt_default_send_param(sk, optval,
3363 optlen);
3364 break;
3365 case SCTP_PRIMARY_ADDR:
3366 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3367 break;
3368 case SCTP_SET_PEER_PRIMARY_ADDR:
3369 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3370 break;
3371 case SCTP_NODELAY:
3372 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3373 break;
3374 case SCTP_RTOINFO:
3375 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3376 break;
3377 case SCTP_ASSOCINFO:
3378 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3379 break;
3380 case SCTP_I_WANT_MAPPED_V4_ADDR:
3381 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3382 break;
3383 case SCTP_MAXSEG:
3384 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3385 break;
3386 case SCTP_ADAPTATION_LAYER:
3387 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3388 break;
3389 case SCTP_CONTEXT:
3390 retval = sctp_setsockopt_context(sk, optval, optlen);
3391 break;
3392 case SCTP_FRAGMENT_INTERLEAVE:
3393 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3394 break;
3395 case SCTP_MAX_BURST:
3396 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3397 break;
3398 case SCTP_AUTH_CHUNK:
3399 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3400 break;
3401 case SCTP_HMAC_IDENT:
3402 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3403 break;
3404 case SCTP_AUTH_KEY:
3405 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3406 break;
3407 case SCTP_AUTH_ACTIVE_KEY:
3408 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3409 break;
3410 case SCTP_AUTH_DELETE_KEY:
3411 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3412 break;
3413 default:
3414 retval = -ENOPROTOOPT;
3415 break;
3418 sctp_release_sock(sk);
3420 out_nounlock:
3421 return retval;
3424 /* API 3.1.6 connect() - UDP Style Syntax
3426 * An application may use the connect() call in the UDP model to initiate an
3427 * association without sending data.
3429 * The syntax is:
3431 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3433 * sd: the socket descriptor to have a new association added to.
3435 * nam: the address structure (either struct sockaddr_in or struct
3436 * sockaddr_in6 defined in RFC2553 [7]).
3438 * len: the size of the address.
3440 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3441 int addr_len)
3443 int err = 0;
3444 struct sctp_af *af;
3446 sctp_lock_sock(sk);
3448 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3449 __func__, sk, addr, addr_len);
3451 /* Validate addr_len before calling common connect/connectx routine. */
3452 af = sctp_get_af_specific(addr->sa_family);
3453 if (!af || addr_len < af->sockaddr_len) {
3454 err = -EINVAL;
3455 } else {
3456 /* Pass correct addr len to common routine (so it knows there
3457 * is only one address being passed.
3459 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
3462 sctp_release_sock(sk);
3463 return err;
3466 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3468 return -EOPNOTSUPP; /* STUB */
3471 /* 4.1.4 accept() - TCP Style Syntax
3473 * Applications use accept() call to remove an established SCTP
3474 * association from the accept queue of the endpoint. A new socket
3475 * descriptor will be returned from accept() to represent the newly
3476 * formed association.
3478 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3480 struct sctp_sock *sp;
3481 struct sctp_endpoint *ep;
3482 struct sock *newsk = NULL;
3483 struct sctp_association *asoc;
3484 long timeo;
3485 int error = 0;
3487 sctp_lock_sock(sk);
3489 sp = sctp_sk(sk);
3490 ep = sp->ep;
3492 if (!sctp_style(sk, TCP)) {
3493 error = -EOPNOTSUPP;
3494 goto out;
3497 if (!sctp_sstate(sk, LISTENING)) {
3498 error = -EINVAL;
3499 goto out;
3502 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3504 error = sctp_wait_for_accept(sk, timeo);
3505 if (error)
3506 goto out;
3508 /* We treat the list of associations on the endpoint as the accept
3509 * queue and pick the first association on the list.
3511 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3513 newsk = sp->pf->create_accept_sk(sk, asoc);
3514 if (!newsk) {
3515 error = -ENOMEM;
3516 goto out;
3519 /* Populate the fields of the newsk from the oldsk and migrate the
3520 * asoc to the newsk.
3522 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3524 out:
3525 sctp_release_sock(sk);
3526 *err = error;
3527 return newsk;
3530 /* The SCTP ioctl handler. */
3531 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3533 return -ENOIOCTLCMD;
3536 /* This is the function which gets called during socket creation to
3537 * initialized the SCTP-specific portion of the sock.
3538 * The sock structure should already be zero-filled memory.
3540 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3542 struct sctp_endpoint *ep;
3543 struct sctp_sock *sp;
3545 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3547 sp = sctp_sk(sk);
3549 /* Initialize the SCTP per socket area. */
3550 switch (sk->sk_type) {
3551 case SOCK_SEQPACKET:
3552 sp->type = SCTP_SOCKET_UDP;
3553 break;
3554 case SOCK_STREAM:
3555 sp->type = SCTP_SOCKET_TCP;
3556 break;
3557 default:
3558 return -ESOCKTNOSUPPORT;
3561 /* Initialize default send parameters. These parameters can be
3562 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3564 sp->default_stream = 0;
3565 sp->default_ppid = 0;
3566 sp->default_flags = 0;
3567 sp->default_context = 0;
3568 sp->default_timetolive = 0;
3570 sp->default_rcv_context = 0;
3571 sp->max_burst = sctp_max_burst;
3573 /* Initialize default setup parameters. These parameters
3574 * can be modified with the SCTP_INITMSG socket option or
3575 * overridden by the SCTP_INIT CMSG.
3577 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3578 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3579 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3580 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3582 /* Initialize default RTO related parameters. These parameters can
3583 * be modified for with the SCTP_RTOINFO socket option.
3585 sp->rtoinfo.srto_initial = sctp_rto_initial;
3586 sp->rtoinfo.srto_max = sctp_rto_max;
3587 sp->rtoinfo.srto_min = sctp_rto_min;
3589 /* Initialize default association related parameters. These parameters
3590 * can be modified with the SCTP_ASSOCINFO socket option.
3592 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3593 sp->assocparams.sasoc_number_peer_destinations = 0;
3594 sp->assocparams.sasoc_peer_rwnd = 0;
3595 sp->assocparams.sasoc_local_rwnd = 0;
3596 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3598 /* Initialize default event subscriptions. By default, all the
3599 * options are off.
3601 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3603 /* Default Peer Address Parameters. These defaults can
3604 * be modified via SCTP_PEER_ADDR_PARAMS
3606 sp->hbinterval = sctp_hb_interval;
3607 sp->pathmaxrxt = sctp_max_retrans_path;
3608 sp->pathmtu = 0; // allow default discovery
3609 sp->sackdelay = sctp_sack_timeout;
3610 sp->sackfreq = 2;
3611 sp->param_flags = SPP_HB_ENABLE |
3612 SPP_PMTUD_ENABLE |
3613 SPP_SACKDELAY_ENABLE;
3615 /* If enabled no SCTP message fragmentation will be performed.
3616 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3618 sp->disable_fragments = 0;
3620 /* Enable Nagle algorithm by default. */
3621 sp->nodelay = 0;
3623 /* Enable by default. */
3624 sp->v4mapped = 1;
3626 /* Auto-close idle associations after the configured
3627 * number of seconds. A value of 0 disables this
3628 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3629 * for UDP-style sockets only.
3631 sp->autoclose = 0;
3633 /* User specified fragmentation limit. */
3634 sp->user_frag = 0;
3636 sp->adaptation_ind = 0;
3638 sp->pf = sctp_get_pf_specific(sk->sk_family);
3640 /* Control variables for partial data delivery. */
3641 atomic_set(&sp->pd_mode, 0);
3642 skb_queue_head_init(&sp->pd_lobby);
3643 sp->frag_interleave = 0;
3645 /* Create a per socket endpoint structure. Even if we
3646 * change the data structure relationships, this may still
3647 * be useful for storing pre-connect address information.
3649 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3650 if (!ep)
3651 return -ENOMEM;
3653 sp->ep = ep;
3654 sp->hmac = NULL;
3656 SCTP_DBG_OBJCNT_INC(sock);
3658 local_bh_disable();
3659 percpu_counter_inc(&sctp_sockets_allocated);
3660 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3661 local_bh_enable();
3663 return 0;
3666 /* Cleanup any SCTP per socket resources. */
3667 SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
3669 struct sctp_endpoint *ep;
3671 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3673 /* Release our hold on the endpoint. */
3674 ep = sctp_sk(sk)->ep;
3675 sctp_endpoint_free(ep);
3676 local_bh_disable();
3677 percpu_counter_dec(&sctp_sockets_allocated);
3678 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
3679 local_bh_enable();
3682 /* API 4.1.7 shutdown() - TCP Style Syntax
3683 * int shutdown(int socket, int how);
3685 * sd - the socket descriptor of the association to be closed.
3686 * how - Specifies the type of shutdown. The values are
3687 * as follows:
3688 * SHUT_RD
3689 * Disables further receive operations. No SCTP
3690 * protocol action is taken.
3691 * SHUT_WR
3692 * Disables further send operations, and initiates
3693 * the SCTP shutdown sequence.
3694 * SHUT_RDWR
3695 * Disables further send and receive operations
3696 * and initiates the SCTP shutdown sequence.
3698 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3700 struct sctp_endpoint *ep;
3701 struct sctp_association *asoc;
3703 if (!sctp_style(sk, TCP))
3704 return;
3706 if (how & SEND_SHUTDOWN) {
3707 ep = sctp_sk(sk)->ep;
3708 if (!list_empty(&ep->asocs)) {
3709 asoc = list_entry(ep->asocs.next,
3710 struct sctp_association, asocs);
3711 sctp_primitive_SHUTDOWN(asoc, NULL);
3716 /* 7.2.1 Association Status (SCTP_STATUS)
3718 * Applications can retrieve current status information about an
3719 * association, including association state, peer receiver window size,
3720 * number of unacked data chunks, and number of data chunks pending
3721 * receipt. This information is read-only.
3723 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3724 char __user *optval,
3725 int __user *optlen)
3727 struct sctp_status status;
3728 struct sctp_association *asoc = NULL;
3729 struct sctp_transport *transport;
3730 sctp_assoc_t associd;
3731 int retval = 0;
3733 if (len < sizeof(status)) {
3734 retval = -EINVAL;
3735 goto out;
3738 len = sizeof(status);
3739 if (copy_from_user(&status, optval, len)) {
3740 retval = -EFAULT;
3741 goto out;
3744 associd = status.sstat_assoc_id;
3745 asoc = sctp_id2assoc(sk, associd);
3746 if (!asoc) {
3747 retval = -EINVAL;
3748 goto out;
3751 transport = asoc->peer.primary_path;
3753 status.sstat_assoc_id = sctp_assoc2id(asoc);
3754 status.sstat_state = asoc->state;
3755 status.sstat_rwnd = asoc->peer.rwnd;
3756 status.sstat_unackdata = asoc->unack_data;
3758 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3759 status.sstat_instrms = asoc->c.sinit_max_instreams;
3760 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3761 status.sstat_fragmentation_point = asoc->frag_point;
3762 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3763 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3764 transport->af_specific->sockaddr_len);
3765 /* Map ipv4 address into v4-mapped-on-v6 address. */
3766 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3767 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3768 status.sstat_primary.spinfo_state = transport->state;
3769 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3770 status.sstat_primary.spinfo_srtt = transport->srtt;
3771 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3772 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3774 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3775 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3777 if (put_user(len, optlen)) {
3778 retval = -EFAULT;
3779 goto out;
3782 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3783 len, status.sstat_state, status.sstat_rwnd,
3784 status.sstat_assoc_id);
3786 if (copy_to_user(optval, &status, len)) {
3787 retval = -EFAULT;
3788 goto out;
3791 out:
3792 return (retval);
3796 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3798 * Applications can retrieve information about a specific peer address
3799 * of an association, including its reachability state, congestion
3800 * window, and retransmission timer values. This information is
3801 * read-only.
3803 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3804 char __user *optval,
3805 int __user *optlen)
3807 struct sctp_paddrinfo pinfo;
3808 struct sctp_transport *transport;
3809 int retval = 0;
3811 if (len < sizeof(pinfo)) {
3812 retval = -EINVAL;
3813 goto out;
3816 len = sizeof(pinfo);
3817 if (copy_from_user(&pinfo, optval, len)) {
3818 retval = -EFAULT;
3819 goto out;
3822 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3823 pinfo.spinfo_assoc_id);
3824 if (!transport)
3825 return -EINVAL;
3827 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3828 pinfo.spinfo_state = transport->state;
3829 pinfo.spinfo_cwnd = transport->cwnd;
3830 pinfo.spinfo_srtt = transport->srtt;
3831 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3832 pinfo.spinfo_mtu = transport->pathmtu;
3834 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3835 pinfo.spinfo_state = SCTP_ACTIVE;
3837 if (put_user(len, optlen)) {
3838 retval = -EFAULT;
3839 goto out;
3842 if (copy_to_user(optval, &pinfo, len)) {
3843 retval = -EFAULT;
3844 goto out;
3847 out:
3848 return (retval);
3851 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3853 * This option is a on/off flag. If enabled no SCTP message
3854 * fragmentation will be performed. Instead if a message being sent
3855 * exceeds the current PMTU size, the message will NOT be sent and
3856 * instead a error will be indicated to the user.
3858 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3859 char __user *optval, int __user *optlen)
3861 int val;
3863 if (len < sizeof(int))
3864 return -EINVAL;
3866 len = sizeof(int);
3867 val = (sctp_sk(sk)->disable_fragments == 1);
3868 if (put_user(len, optlen))
3869 return -EFAULT;
3870 if (copy_to_user(optval, &val, len))
3871 return -EFAULT;
3872 return 0;
3875 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3877 * This socket option is used to specify various notifications and
3878 * ancillary data the user wishes to receive.
3880 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3881 int __user *optlen)
3883 if (len < sizeof(struct sctp_event_subscribe))
3884 return -EINVAL;
3885 len = sizeof(struct sctp_event_subscribe);
3886 if (put_user(len, optlen))
3887 return -EFAULT;
3888 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3889 return -EFAULT;
3890 return 0;
3893 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3895 * This socket option is applicable to the UDP-style socket only. When
3896 * set it will cause associations that are idle for more than the
3897 * specified number of seconds to automatically close. An association
3898 * being idle is defined an association that has NOT sent or received
3899 * user data. The special value of '0' indicates that no automatic
3900 * close of any associations should be performed. The option expects an
3901 * integer defining the number of seconds of idle time before an
3902 * association is closed.
3904 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3906 /* Applicable to UDP-style socket only */
3907 if (sctp_style(sk, TCP))
3908 return -EOPNOTSUPP;
3909 if (len < sizeof(int))
3910 return -EINVAL;
3911 len = sizeof(int);
3912 if (put_user(len, optlen))
3913 return -EFAULT;
3914 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
3915 return -EFAULT;
3916 return 0;
3919 /* Helper routine to branch off an association to a new socket. */
3920 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3921 struct socket **sockp)
3923 struct sock *sk = asoc->base.sk;
3924 struct socket *sock;
3925 struct sctp_af *af;
3926 int err = 0;
3928 /* An association cannot be branched off from an already peeled-off
3929 * socket, nor is this supported for tcp style sockets.
3931 if (!sctp_style(sk, UDP))
3932 return -EINVAL;
3934 /* Create a new socket. */
3935 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3936 if (err < 0)
3937 return err;
3939 sctp_copy_sock(sock->sk, sk, asoc);
3941 /* Make peeled-off sockets more like 1-1 accepted sockets.
3942 * Set the daddr and initialize id to something more random
3944 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3945 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3947 /* Populate the fields of the newsk from the oldsk and migrate the
3948 * asoc to the newsk.
3950 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3952 *sockp = sock;
3954 return err;
3957 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3959 sctp_peeloff_arg_t peeloff;
3960 struct socket *newsock;
3961 int retval = 0;
3962 struct sctp_association *asoc;
3964 if (len < sizeof(sctp_peeloff_arg_t))
3965 return -EINVAL;
3966 len = sizeof(sctp_peeloff_arg_t);
3967 if (copy_from_user(&peeloff, optval, len))
3968 return -EFAULT;
3970 asoc = sctp_id2assoc(sk, peeloff.associd);
3971 if (!asoc) {
3972 retval = -EINVAL;
3973 goto out;
3976 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __func__, sk, asoc);
3978 retval = sctp_do_peeloff(asoc, &newsock);
3979 if (retval < 0)
3980 goto out;
3982 /* Map the socket to an unused fd that can be returned to the user. */
3983 retval = sock_map_fd(newsock, 0);
3984 if (retval < 0) {
3985 sock_release(newsock);
3986 goto out;
3989 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3990 __func__, sk, asoc, newsock->sk, retval);
3992 /* Return the fd mapped to the new socket. */
3993 peeloff.sd = retval;
3994 if (put_user(len, optlen))
3995 return -EFAULT;
3996 if (copy_to_user(optval, &peeloff, len))
3997 retval = -EFAULT;
3999 out:
4000 return retval;
4003 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4005 * Applications can enable or disable heartbeats for any peer address of
4006 * an association, modify an address's heartbeat interval, force a
4007 * heartbeat to be sent immediately, and adjust the address's maximum
4008 * number of retransmissions sent before an address is considered
4009 * unreachable. The following structure is used to access and modify an
4010 * address's parameters:
4012 * struct sctp_paddrparams {
4013 * sctp_assoc_t spp_assoc_id;
4014 * struct sockaddr_storage spp_address;
4015 * uint32_t spp_hbinterval;
4016 * uint16_t spp_pathmaxrxt;
4017 * uint32_t spp_pathmtu;
4018 * uint32_t spp_sackdelay;
4019 * uint32_t spp_flags;
4020 * };
4022 * spp_assoc_id - (one-to-many style socket) This is filled in the
4023 * application, and identifies the association for
4024 * this query.
4025 * spp_address - This specifies which address is of interest.
4026 * spp_hbinterval - This contains the value of the heartbeat interval,
4027 * in milliseconds. If a value of zero
4028 * is present in this field then no changes are to
4029 * be made to this parameter.
4030 * spp_pathmaxrxt - This contains the maximum number of
4031 * retransmissions before this address shall be
4032 * considered unreachable. If a value of zero
4033 * is present in this field then no changes are to
4034 * be made to this parameter.
4035 * spp_pathmtu - When Path MTU discovery is disabled the value
4036 * specified here will be the "fixed" path mtu.
4037 * Note that if the spp_address field is empty
4038 * then all associations on this address will
4039 * have this fixed path mtu set upon them.
4041 * spp_sackdelay - When delayed sack is enabled, this value specifies
4042 * the number of milliseconds that sacks will be delayed
4043 * for. This value will apply to all addresses of an
4044 * association if the spp_address field is empty. Note
4045 * also, that if delayed sack is enabled and this
4046 * value is set to 0, no change is made to the last
4047 * recorded delayed sack timer value.
4049 * spp_flags - These flags are used to control various features
4050 * on an association. The flag field may contain
4051 * zero or more of the following options.
4053 * SPP_HB_ENABLE - Enable heartbeats on the
4054 * specified address. Note that if the address
4055 * field is empty all addresses for the association
4056 * have heartbeats enabled upon them.
4058 * SPP_HB_DISABLE - Disable heartbeats on the
4059 * speicifed address. Note that if the address
4060 * field is empty all addresses for the association
4061 * will have their heartbeats disabled. Note also
4062 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4063 * mutually exclusive, only one of these two should
4064 * be specified. Enabling both fields will have
4065 * undetermined results.
4067 * SPP_HB_DEMAND - Request a user initiated heartbeat
4068 * to be made immediately.
4070 * SPP_PMTUD_ENABLE - This field will enable PMTU
4071 * discovery upon the specified address. Note that
4072 * if the address feild is empty then all addresses
4073 * on the association are effected.
4075 * SPP_PMTUD_DISABLE - This field will disable PMTU
4076 * discovery upon the specified address. Note that
4077 * if the address feild is empty then all addresses
4078 * on the association are effected. Not also that
4079 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4080 * exclusive. Enabling both will have undetermined
4081 * results.
4083 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4084 * on delayed sack. The time specified in spp_sackdelay
4085 * is used to specify the sack delay for this address. Note
4086 * that if spp_address is empty then all addresses will
4087 * enable delayed sack and take on the sack delay
4088 * value specified in spp_sackdelay.
4089 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4090 * off delayed sack. If the spp_address field is blank then
4091 * delayed sack is disabled for the entire association. Note
4092 * also that this field is mutually exclusive to
4093 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4094 * results.
4096 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
4097 char __user *optval, int __user *optlen)
4099 struct sctp_paddrparams params;
4100 struct sctp_transport *trans = NULL;
4101 struct sctp_association *asoc = NULL;
4102 struct sctp_sock *sp = sctp_sk(sk);
4104 if (len < sizeof(struct sctp_paddrparams))
4105 return -EINVAL;
4106 len = sizeof(struct sctp_paddrparams);
4107 if (copy_from_user(&params, optval, len))
4108 return -EFAULT;
4110 /* If an address other than INADDR_ANY is specified, and
4111 * no transport is found, then the request is invalid.
4113 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
4114 trans = sctp_addr_id2transport(sk, &params.spp_address,
4115 params.spp_assoc_id);
4116 if (!trans) {
4117 SCTP_DEBUG_PRINTK("Failed no transport\n");
4118 return -EINVAL;
4122 /* Get association, if assoc_id != 0 and the socket is a one
4123 * to many style socket, and an association was not found, then
4124 * the id was invalid.
4126 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4127 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4128 SCTP_DEBUG_PRINTK("Failed no association\n");
4129 return -EINVAL;
4132 if (trans) {
4133 /* Fetch transport values. */
4134 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4135 params.spp_pathmtu = trans->pathmtu;
4136 params.spp_pathmaxrxt = trans->pathmaxrxt;
4137 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
4139 /*draft-11 doesn't say what to return in spp_flags*/
4140 params.spp_flags = trans->param_flags;
4141 } else if (asoc) {
4142 /* Fetch association values. */
4143 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4144 params.spp_pathmtu = asoc->pathmtu;
4145 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4146 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
4148 /*draft-11 doesn't say what to return in spp_flags*/
4149 params.spp_flags = asoc->param_flags;
4150 } else {
4151 /* Fetch socket values. */
4152 params.spp_hbinterval = sp->hbinterval;
4153 params.spp_pathmtu = sp->pathmtu;
4154 params.spp_sackdelay = sp->sackdelay;
4155 params.spp_pathmaxrxt = sp->pathmaxrxt;
4157 /*draft-11 doesn't say what to return in spp_flags*/
4158 params.spp_flags = sp->param_flags;
4161 if (copy_to_user(optval, &params, len))
4162 return -EFAULT;
4164 if (put_user(len, optlen))
4165 return -EFAULT;
4167 return 0;
4171 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
4173 * This option will effect the way delayed acks are performed. This
4174 * option allows you to get or set the delayed ack time, in
4175 * milliseconds. It also allows changing the delayed ack frequency.
4176 * Changing the frequency to 1 disables the delayed sack algorithm. If
4177 * the assoc_id is 0, then this sets or gets the endpoints default
4178 * values. If the assoc_id field is non-zero, then the set or get
4179 * effects the specified association for the one to many model (the
4180 * assoc_id field is ignored by the one to one model). Note that if
4181 * sack_delay or sack_freq are 0 when setting this option, then the
4182 * current values will remain unchanged.
4184 * struct sctp_sack_info {
4185 * sctp_assoc_t sack_assoc_id;
4186 * uint32_t sack_delay;
4187 * uint32_t sack_freq;
4188 * };
4190 * sack_assoc_id - This parameter, indicates which association the user
4191 * is performing an action upon. Note that if this field's value is
4192 * zero then the endpoints default value is changed (effecting future
4193 * associations only).
4195 * sack_delay - This parameter contains the number of milliseconds that
4196 * the user is requesting the delayed ACK timer be set to. Note that
4197 * this value is defined in the standard to be between 200 and 500
4198 * milliseconds.
4200 * sack_freq - This parameter contains the number of packets that must
4201 * be received before a sack is sent without waiting for the delay
4202 * timer to expire. The default value for this is 2, setting this
4203 * value to 1 will disable the delayed sack algorithm.
4205 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
4206 char __user *optval,
4207 int __user *optlen)
4209 struct sctp_sack_info params;
4210 struct sctp_association *asoc = NULL;
4211 struct sctp_sock *sp = sctp_sk(sk);
4213 if (len >= sizeof(struct sctp_sack_info)) {
4214 len = sizeof(struct sctp_sack_info);
4216 if (copy_from_user(&params, optval, len))
4217 return -EFAULT;
4218 } else if (len == sizeof(struct sctp_assoc_value)) {
4219 printk(KERN_WARNING "SCTP: Use of struct sctp_assoc_value "
4220 "in delayed_ack socket option deprecated\n");
4221 printk(KERN_WARNING "SCTP: Use struct sctp_sack_info instead\n");
4222 if (copy_from_user(&params, optval, len))
4223 return -EFAULT;
4224 } else
4225 return - EINVAL;
4227 /* Get association, if sack_assoc_id != 0 and the socket is a one
4228 * to many style socket, and an association was not found, then
4229 * the id was invalid.
4231 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4232 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
4233 return -EINVAL;
4235 if (asoc) {
4236 /* Fetch association values. */
4237 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4238 params.sack_delay = jiffies_to_msecs(
4239 asoc->sackdelay);
4240 params.sack_freq = asoc->sackfreq;
4242 } else {
4243 params.sack_delay = 0;
4244 params.sack_freq = 1;
4246 } else {
4247 /* Fetch socket values. */
4248 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4249 params.sack_delay = sp->sackdelay;
4250 params.sack_freq = sp->sackfreq;
4251 } else {
4252 params.sack_delay = 0;
4253 params.sack_freq = 1;
4257 if (copy_to_user(optval, &params, len))
4258 return -EFAULT;
4260 if (put_user(len, optlen))
4261 return -EFAULT;
4263 return 0;
4266 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4268 * Applications can specify protocol parameters for the default association
4269 * initialization. The option name argument to setsockopt() and getsockopt()
4270 * is SCTP_INITMSG.
4272 * Setting initialization parameters is effective only on an unconnected
4273 * socket (for UDP-style sockets only future associations are effected
4274 * by the change). With TCP-style sockets, this option is inherited by
4275 * sockets derived from a listener socket.
4277 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4279 if (len < sizeof(struct sctp_initmsg))
4280 return -EINVAL;
4281 len = sizeof(struct sctp_initmsg);
4282 if (put_user(len, optlen))
4283 return -EFAULT;
4284 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4285 return -EFAULT;
4286 return 0;
4290 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4291 char __user *optval, int __user *optlen)
4293 struct sctp_association *asoc;
4294 int cnt = 0;
4295 struct sctp_getaddrs getaddrs;
4296 struct sctp_transport *from;
4297 void __user *to;
4298 union sctp_addr temp;
4299 struct sctp_sock *sp = sctp_sk(sk);
4300 int addrlen;
4301 size_t space_left;
4302 int bytes_copied;
4304 if (len < sizeof(struct sctp_getaddrs))
4305 return -EINVAL;
4307 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4308 return -EFAULT;
4310 /* For UDP-style sockets, id specifies the association to query. */
4311 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4312 if (!asoc)
4313 return -EINVAL;
4315 to = optval + offsetof(struct sctp_getaddrs,addrs);
4316 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4318 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4319 transports) {
4320 memcpy(&temp, &from->ipaddr, sizeof(temp));
4321 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4322 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4323 if (space_left < addrlen)
4324 return -ENOMEM;
4325 if (copy_to_user(to, &temp, addrlen))
4326 return -EFAULT;
4327 to += addrlen;
4328 cnt++;
4329 space_left -= addrlen;
4332 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4333 return -EFAULT;
4334 bytes_copied = ((char __user *)to) - optval;
4335 if (put_user(bytes_copied, optlen))
4336 return -EFAULT;
4338 return 0;
4341 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4342 size_t space_left, int *bytes_copied)
4344 struct sctp_sockaddr_entry *addr;
4345 union sctp_addr temp;
4346 int cnt = 0;
4347 int addrlen;
4349 rcu_read_lock();
4350 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4351 if (!addr->valid)
4352 continue;
4354 if ((PF_INET == sk->sk_family) &&
4355 (AF_INET6 == addr->a.sa.sa_family))
4356 continue;
4357 if ((PF_INET6 == sk->sk_family) &&
4358 inet_v6_ipv6only(sk) &&
4359 (AF_INET == addr->a.sa.sa_family))
4360 continue;
4361 memcpy(&temp, &addr->a, sizeof(temp));
4362 if (!temp.v4.sin_port)
4363 temp.v4.sin_port = htons(port);
4365 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4366 &temp);
4367 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4368 if (space_left < addrlen) {
4369 cnt = -ENOMEM;
4370 break;
4372 memcpy(to, &temp, addrlen);
4374 to += addrlen;
4375 cnt ++;
4376 space_left -= addrlen;
4377 *bytes_copied += addrlen;
4379 rcu_read_unlock();
4381 return cnt;
4385 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4386 char __user *optval, int __user *optlen)
4388 struct sctp_bind_addr *bp;
4389 struct sctp_association *asoc;
4390 int cnt = 0;
4391 struct sctp_getaddrs getaddrs;
4392 struct sctp_sockaddr_entry *addr;
4393 void __user *to;
4394 union sctp_addr temp;
4395 struct sctp_sock *sp = sctp_sk(sk);
4396 int addrlen;
4397 int err = 0;
4398 size_t space_left;
4399 int bytes_copied = 0;
4400 void *addrs;
4401 void *buf;
4403 if (len < sizeof(struct sctp_getaddrs))
4404 return -EINVAL;
4406 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4407 return -EFAULT;
4410 * For UDP-style sockets, id specifies the association to query.
4411 * If the id field is set to the value '0' then the locally bound
4412 * addresses are returned without regard to any particular
4413 * association.
4415 if (0 == getaddrs.assoc_id) {
4416 bp = &sctp_sk(sk)->ep->base.bind_addr;
4417 } else {
4418 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4419 if (!asoc)
4420 return -EINVAL;
4421 bp = &asoc->base.bind_addr;
4424 to = optval + offsetof(struct sctp_getaddrs,addrs);
4425 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4427 addrs = kmalloc(space_left, GFP_KERNEL);
4428 if (!addrs)
4429 return -ENOMEM;
4431 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4432 * addresses from the global local address list.
4434 if (sctp_list_single_entry(&bp->address_list)) {
4435 addr = list_entry(bp->address_list.next,
4436 struct sctp_sockaddr_entry, list);
4437 if (sctp_is_any(sk, &addr->a)) {
4438 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4439 space_left, &bytes_copied);
4440 if (cnt < 0) {
4441 err = cnt;
4442 goto out;
4444 goto copy_getaddrs;
4448 buf = addrs;
4449 /* Protection on the bound address list is not needed since
4450 * in the socket option context we hold a socket lock and
4451 * thus the bound address list can't change.
4453 list_for_each_entry(addr, &bp->address_list, list) {
4454 memcpy(&temp, &addr->a, sizeof(temp));
4455 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4456 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4457 if (space_left < addrlen) {
4458 err = -ENOMEM;
4459 goto out;
4461 memcpy(buf, &temp, addrlen);
4462 buf += addrlen;
4463 bytes_copied += addrlen;
4464 cnt ++;
4465 space_left -= addrlen;
4468 copy_getaddrs:
4469 if (copy_to_user(to, addrs, bytes_copied)) {
4470 err = -EFAULT;
4471 goto out;
4473 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4474 err = -EFAULT;
4475 goto out;
4477 if (put_user(bytes_copied, optlen))
4478 err = -EFAULT;
4479 out:
4480 kfree(addrs);
4481 return err;
4484 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4486 * Requests that the local SCTP stack use the enclosed peer address as
4487 * the association primary. The enclosed address must be one of the
4488 * association peer's addresses.
4490 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4491 char __user *optval, int __user *optlen)
4493 struct sctp_prim prim;
4494 struct sctp_association *asoc;
4495 struct sctp_sock *sp = sctp_sk(sk);
4497 if (len < sizeof(struct sctp_prim))
4498 return -EINVAL;
4500 len = sizeof(struct sctp_prim);
4502 if (copy_from_user(&prim, optval, len))
4503 return -EFAULT;
4505 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4506 if (!asoc)
4507 return -EINVAL;
4509 if (!asoc->peer.primary_path)
4510 return -ENOTCONN;
4512 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4513 asoc->peer.primary_path->af_specific->sockaddr_len);
4515 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4516 (union sctp_addr *)&prim.ssp_addr);
4518 if (put_user(len, optlen))
4519 return -EFAULT;
4520 if (copy_to_user(optval, &prim, len))
4521 return -EFAULT;
4523 return 0;
4527 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4529 * Requests that the local endpoint set the specified Adaptation Layer
4530 * Indication parameter for all future INIT and INIT-ACK exchanges.
4532 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4533 char __user *optval, int __user *optlen)
4535 struct sctp_setadaptation adaptation;
4537 if (len < sizeof(struct sctp_setadaptation))
4538 return -EINVAL;
4540 len = sizeof(struct sctp_setadaptation);
4542 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4544 if (put_user(len, optlen))
4545 return -EFAULT;
4546 if (copy_to_user(optval, &adaptation, len))
4547 return -EFAULT;
4549 return 0;
4554 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4556 * Applications that wish to use the sendto() system call may wish to
4557 * specify a default set of parameters that would normally be supplied
4558 * through the inclusion of ancillary data. This socket option allows
4559 * such an application to set the default sctp_sndrcvinfo structure.
4562 * The application that wishes to use this socket option simply passes
4563 * in to this call the sctp_sndrcvinfo structure defined in Section
4564 * 5.2.2) The input parameters accepted by this call include
4565 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4566 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4567 * to this call if the caller is using the UDP model.
4569 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4571 static int sctp_getsockopt_default_send_param(struct sock *sk,
4572 int len, char __user *optval,
4573 int __user *optlen)
4575 struct sctp_sndrcvinfo info;
4576 struct sctp_association *asoc;
4577 struct sctp_sock *sp = sctp_sk(sk);
4579 if (len < sizeof(struct sctp_sndrcvinfo))
4580 return -EINVAL;
4582 len = sizeof(struct sctp_sndrcvinfo);
4584 if (copy_from_user(&info, optval, len))
4585 return -EFAULT;
4587 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4588 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4589 return -EINVAL;
4591 if (asoc) {
4592 info.sinfo_stream = asoc->default_stream;
4593 info.sinfo_flags = asoc->default_flags;
4594 info.sinfo_ppid = asoc->default_ppid;
4595 info.sinfo_context = asoc->default_context;
4596 info.sinfo_timetolive = asoc->default_timetolive;
4597 } else {
4598 info.sinfo_stream = sp->default_stream;
4599 info.sinfo_flags = sp->default_flags;
4600 info.sinfo_ppid = sp->default_ppid;
4601 info.sinfo_context = sp->default_context;
4602 info.sinfo_timetolive = sp->default_timetolive;
4605 if (put_user(len, optlen))
4606 return -EFAULT;
4607 if (copy_to_user(optval, &info, len))
4608 return -EFAULT;
4610 return 0;
4615 * 7.1.5 SCTP_NODELAY
4617 * Turn on/off any Nagle-like algorithm. This means that packets are
4618 * generally sent as soon as possible and no unnecessary delays are
4619 * introduced, at the cost of more packets in the network. Expects an
4620 * integer boolean flag.
4623 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4624 char __user *optval, int __user *optlen)
4626 int val;
4628 if (len < sizeof(int))
4629 return -EINVAL;
4631 len = sizeof(int);
4632 val = (sctp_sk(sk)->nodelay == 1);
4633 if (put_user(len, optlen))
4634 return -EFAULT;
4635 if (copy_to_user(optval, &val, len))
4636 return -EFAULT;
4637 return 0;
4642 * 7.1.1 SCTP_RTOINFO
4644 * The protocol parameters used to initialize and bound retransmission
4645 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4646 * and modify these parameters.
4647 * All parameters are time values, in milliseconds. A value of 0, when
4648 * modifying the parameters, indicates that the current value should not
4649 * be changed.
4652 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4653 char __user *optval,
4654 int __user *optlen) {
4655 struct sctp_rtoinfo rtoinfo;
4656 struct sctp_association *asoc;
4658 if (len < sizeof (struct sctp_rtoinfo))
4659 return -EINVAL;
4661 len = sizeof(struct sctp_rtoinfo);
4663 if (copy_from_user(&rtoinfo, optval, len))
4664 return -EFAULT;
4666 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4668 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4669 return -EINVAL;
4671 /* Values corresponding to the specific association. */
4672 if (asoc) {
4673 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4674 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4675 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4676 } else {
4677 /* Values corresponding to the endpoint. */
4678 struct sctp_sock *sp = sctp_sk(sk);
4680 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4681 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4682 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4685 if (put_user(len, optlen))
4686 return -EFAULT;
4688 if (copy_to_user(optval, &rtoinfo, len))
4689 return -EFAULT;
4691 return 0;
4696 * 7.1.2 SCTP_ASSOCINFO
4698 * This option is used to tune the maximum retransmission attempts
4699 * of the association.
4700 * Returns an error if the new association retransmission value is
4701 * greater than the sum of the retransmission value of the peer.
4702 * See [SCTP] for more information.
4705 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4706 char __user *optval,
4707 int __user *optlen)
4710 struct sctp_assocparams assocparams;
4711 struct sctp_association *asoc;
4712 struct list_head *pos;
4713 int cnt = 0;
4715 if (len < sizeof (struct sctp_assocparams))
4716 return -EINVAL;
4718 len = sizeof(struct sctp_assocparams);
4720 if (copy_from_user(&assocparams, optval, len))
4721 return -EFAULT;
4723 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4725 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4726 return -EINVAL;
4728 /* Values correspoinding to the specific association */
4729 if (asoc) {
4730 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4731 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4732 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4733 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4734 * 1000) +
4735 (asoc->cookie_life.tv_usec
4736 / 1000);
4738 list_for_each(pos, &asoc->peer.transport_addr_list) {
4739 cnt ++;
4742 assocparams.sasoc_number_peer_destinations = cnt;
4743 } else {
4744 /* Values corresponding to the endpoint */
4745 struct sctp_sock *sp = sctp_sk(sk);
4747 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4748 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4749 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4750 assocparams.sasoc_cookie_life =
4751 sp->assocparams.sasoc_cookie_life;
4752 assocparams.sasoc_number_peer_destinations =
4753 sp->assocparams.
4754 sasoc_number_peer_destinations;
4757 if (put_user(len, optlen))
4758 return -EFAULT;
4760 if (copy_to_user(optval, &assocparams, len))
4761 return -EFAULT;
4763 return 0;
4767 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4769 * This socket option is a boolean flag which turns on or off mapped V4
4770 * addresses. If this option is turned on and the socket is type
4771 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4772 * If this option is turned off, then no mapping will be done of V4
4773 * addresses and a user will receive both PF_INET6 and PF_INET type
4774 * addresses on the socket.
4776 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4777 char __user *optval, int __user *optlen)
4779 int val;
4780 struct sctp_sock *sp = sctp_sk(sk);
4782 if (len < sizeof(int))
4783 return -EINVAL;
4785 len = sizeof(int);
4786 val = sp->v4mapped;
4787 if (put_user(len, optlen))
4788 return -EFAULT;
4789 if (copy_to_user(optval, &val, len))
4790 return -EFAULT;
4792 return 0;
4796 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4797 * (chapter and verse is quoted at sctp_setsockopt_context())
4799 static int sctp_getsockopt_context(struct sock *sk, int len,
4800 char __user *optval, int __user *optlen)
4802 struct sctp_assoc_value params;
4803 struct sctp_sock *sp;
4804 struct sctp_association *asoc;
4806 if (len < sizeof(struct sctp_assoc_value))
4807 return -EINVAL;
4809 len = sizeof(struct sctp_assoc_value);
4811 if (copy_from_user(&params, optval, len))
4812 return -EFAULT;
4814 sp = sctp_sk(sk);
4816 if (params.assoc_id != 0) {
4817 asoc = sctp_id2assoc(sk, params.assoc_id);
4818 if (!asoc)
4819 return -EINVAL;
4820 params.assoc_value = asoc->default_rcv_context;
4821 } else {
4822 params.assoc_value = sp->default_rcv_context;
4825 if (put_user(len, optlen))
4826 return -EFAULT;
4827 if (copy_to_user(optval, &params, len))
4828 return -EFAULT;
4830 return 0;
4834 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
4835 * This option will get or set the maximum size to put in any outgoing
4836 * SCTP DATA chunk. If a message is larger than this size it will be
4837 * fragmented by SCTP into the specified size. Note that the underlying
4838 * SCTP implementation may fragment into smaller sized chunks when the
4839 * PMTU of the underlying association is smaller than the value set by
4840 * the user. The default value for this option is '0' which indicates
4841 * the user is NOT limiting fragmentation and only the PMTU will effect
4842 * SCTP's choice of DATA chunk size. Note also that values set larger
4843 * than the maximum size of an IP datagram will effectively let SCTP
4844 * control fragmentation (i.e. the same as setting this option to 0).
4846 * The following structure is used to access and modify this parameter:
4848 * struct sctp_assoc_value {
4849 * sctp_assoc_t assoc_id;
4850 * uint32_t assoc_value;
4851 * };
4853 * assoc_id: This parameter is ignored for one-to-one style sockets.
4854 * For one-to-many style sockets this parameter indicates which
4855 * association the user is performing an action upon. Note that if
4856 * this field's value is zero then the endpoints default value is
4857 * changed (effecting future associations only).
4858 * assoc_value: This parameter specifies the maximum size in bytes.
4860 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4861 char __user *optval, int __user *optlen)
4863 struct sctp_assoc_value params;
4864 struct sctp_association *asoc;
4866 if (len == sizeof(int)) {
4867 printk(KERN_WARNING
4868 "SCTP: Use of int in maxseg socket option deprecated\n");
4869 printk(KERN_WARNING
4870 "SCTP: Use struct sctp_assoc_value instead\n");
4871 params.assoc_id = 0;
4872 } else if (len >= sizeof(struct sctp_assoc_value)) {
4873 len = sizeof(struct sctp_assoc_value);
4874 if (copy_from_user(&params, optval, sizeof(params)))
4875 return -EFAULT;
4876 } else
4877 return -EINVAL;
4879 asoc = sctp_id2assoc(sk, params.assoc_id);
4880 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
4881 return -EINVAL;
4883 if (asoc)
4884 params.assoc_value = asoc->frag_point;
4885 else
4886 params.assoc_value = sctp_sk(sk)->user_frag;
4888 if (put_user(len, optlen))
4889 return -EFAULT;
4890 if (len == sizeof(int)) {
4891 if (copy_to_user(optval, &params.assoc_value, len))
4892 return -EFAULT;
4893 } else {
4894 if (copy_to_user(optval, &params, len))
4895 return -EFAULT;
4898 return 0;
4902 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4903 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4905 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4906 char __user *optval, int __user *optlen)
4908 int val;
4910 if (len < sizeof(int))
4911 return -EINVAL;
4913 len = sizeof(int);
4915 val = sctp_sk(sk)->frag_interleave;
4916 if (put_user(len, optlen))
4917 return -EFAULT;
4918 if (copy_to_user(optval, &val, len))
4919 return -EFAULT;
4921 return 0;
4925 * 7.1.25. Set or Get the sctp partial delivery point
4926 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4928 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4929 char __user *optval,
4930 int __user *optlen)
4932 u32 val;
4934 if (len < sizeof(u32))
4935 return -EINVAL;
4937 len = sizeof(u32);
4939 val = sctp_sk(sk)->pd_point;
4940 if (put_user(len, optlen))
4941 return -EFAULT;
4942 if (copy_to_user(optval, &val, len))
4943 return -EFAULT;
4945 return -ENOTSUPP;
4949 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4950 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4952 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4953 char __user *optval,
4954 int __user *optlen)
4956 struct sctp_assoc_value params;
4957 struct sctp_sock *sp;
4958 struct sctp_association *asoc;
4960 if (len == sizeof(int)) {
4961 printk(KERN_WARNING
4962 "SCTP: Use of int in max_burst socket option deprecated\n");
4963 printk(KERN_WARNING
4964 "SCTP: Use struct sctp_assoc_value instead\n");
4965 params.assoc_id = 0;
4966 } else if (len >= sizeof(struct sctp_assoc_value)) {
4967 len = sizeof(struct sctp_assoc_value);
4968 if (copy_from_user(&params, optval, len))
4969 return -EFAULT;
4970 } else
4971 return -EINVAL;
4973 sp = sctp_sk(sk);
4975 if (params.assoc_id != 0) {
4976 asoc = sctp_id2assoc(sk, params.assoc_id);
4977 if (!asoc)
4978 return -EINVAL;
4979 params.assoc_value = asoc->max_burst;
4980 } else
4981 params.assoc_value = sp->max_burst;
4983 if (len == sizeof(int)) {
4984 if (copy_to_user(optval, &params.assoc_value, len))
4985 return -EFAULT;
4986 } else {
4987 if (copy_to_user(optval, &params, len))
4988 return -EFAULT;
4991 return 0;
4995 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
4996 char __user *optval, int __user *optlen)
4998 struct sctp_hmacalgo __user *p = (void __user *)optval;
4999 struct sctp_hmac_algo_param *hmacs;
5000 __u16 data_len = 0;
5001 u32 num_idents;
5003 if (!sctp_auth_enable)
5004 return -EACCES;
5006 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5007 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
5009 if (len < sizeof(struct sctp_hmacalgo) + data_len)
5010 return -EINVAL;
5012 len = sizeof(struct sctp_hmacalgo) + data_len;
5013 num_idents = data_len / sizeof(u16);
5015 if (put_user(len, optlen))
5016 return -EFAULT;
5017 if (put_user(num_idents, &p->shmac_num_idents))
5018 return -EFAULT;
5019 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5020 return -EFAULT;
5021 return 0;
5024 static int sctp_getsockopt_active_key(struct sock *sk, int len,
5025 char __user *optval, int __user *optlen)
5027 struct sctp_authkeyid val;
5028 struct sctp_association *asoc;
5030 if (!sctp_auth_enable)
5031 return -EACCES;
5033 if (len < sizeof(struct sctp_authkeyid))
5034 return -EINVAL;
5035 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5036 return -EFAULT;
5038 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5039 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5040 return -EINVAL;
5042 if (asoc)
5043 val.scact_keynumber = asoc->active_key_id;
5044 else
5045 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5047 len = sizeof(struct sctp_authkeyid);
5048 if (put_user(len, optlen))
5049 return -EFAULT;
5050 if (copy_to_user(optval, &val, len))
5051 return -EFAULT;
5053 return 0;
5056 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5057 char __user *optval, int __user *optlen)
5059 struct sctp_authchunks __user *p = (void __user *)optval;
5060 struct sctp_authchunks val;
5061 struct sctp_association *asoc;
5062 struct sctp_chunks_param *ch;
5063 u32 num_chunks = 0;
5064 char __user *to;
5066 if (!sctp_auth_enable)
5067 return -EACCES;
5069 if (len < sizeof(struct sctp_authchunks))
5070 return -EINVAL;
5072 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5073 return -EFAULT;
5075 to = p->gauth_chunks;
5076 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5077 if (!asoc)
5078 return -EINVAL;
5080 ch = asoc->peer.peer_chunks;
5081 if (!ch)
5082 goto num;
5084 /* See if the user provided enough room for all the data */
5085 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5086 if (len < num_chunks)
5087 return -EINVAL;
5089 if (copy_to_user(to, ch->chunks, num_chunks))
5090 return -EFAULT;
5091 num:
5092 len = sizeof(struct sctp_authchunks) + num_chunks;
5093 if (put_user(len, optlen)) return -EFAULT;
5094 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5095 return -EFAULT;
5096 return 0;
5099 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5100 char __user *optval, int __user *optlen)
5102 struct sctp_authchunks __user *p = (void __user *)optval;
5103 struct sctp_authchunks val;
5104 struct sctp_association *asoc;
5105 struct sctp_chunks_param *ch;
5106 u32 num_chunks = 0;
5107 char __user *to;
5109 if (!sctp_auth_enable)
5110 return -EACCES;
5112 if (len < sizeof(struct sctp_authchunks))
5113 return -EINVAL;
5115 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5116 return -EFAULT;
5118 to = p->gauth_chunks;
5119 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5120 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5121 return -EINVAL;
5123 if (asoc)
5124 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5125 else
5126 ch = sctp_sk(sk)->ep->auth_chunk_list;
5128 if (!ch)
5129 goto num;
5131 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5132 if (len < sizeof(struct sctp_authchunks) + num_chunks)
5133 return -EINVAL;
5135 if (copy_to_user(to, ch->chunks, num_chunks))
5136 return -EFAULT;
5137 num:
5138 len = sizeof(struct sctp_authchunks) + num_chunks;
5139 if (put_user(len, optlen))
5140 return -EFAULT;
5141 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5142 return -EFAULT;
5144 return 0;
5148 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5149 * This option gets the current number of associations that are attached
5150 * to a one-to-many style socket. The option value is an uint32_t.
5152 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5153 char __user *optval, int __user *optlen)
5155 struct sctp_sock *sp = sctp_sk(sk);
5156 struct sctp_association *asoc;
5157 u32 val = 0;
5159 if (sctp_style(sk, TCP))
5160 return -EOPNOTSUPP;
5162 if (len < sizeof(u32))
5163 return -EINVAL;
5165 len = sizeof(u32);
5167 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5168 val++;
5171 if (put_user(len, optlen))
5172 return -EFAULT;
5173 if (copy_to_user(optval, &val, len))
5174 return -EFAULT;
5176 return 0;
5179 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5180 char __user *optval, int __user *optlen)
5182 int retval = 0;
5183 int len;
5185 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5186 sk, optname);
5188 /* I can hardly begin to describe how wrong this is. This is
5189 * so broken as to be worse than useless. The API draft
5190 * REALLY is NOT helpful here... I am not convinced that the
5191 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5192 * are at all well-founded.
5194 if (level != SOL_SCTP) {
5195 struct sctp_af *af = sctp_sk(sk)->pf->af;
5197 retval = af->getsockopt(sk, level, optname, optval, optlen);
5198 return retval;
5201 if (get_user(len, optlen))
5202 return -EFAULT;
5204 sctp_lock_sock(sk);
5206 switch (optname) {
5207 case SCTP_STATUS:
5208 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5209 break;
5210 case SCTP_DISABLE_FRAGMENTS:
5211 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5212 optlen);
5213 break;
5214 case SCTP_EVENTS:
5215 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5216 break;
5217 case SCTP_AUTOCLOSE:
5218 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5219 break;
5220 case SCTP_SOCKOPT_PEELOFF:
5221 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5222 break;
5223 case SCTP_PEER_ADDR_PARAMS:
5224 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5225 optlen);
5226 break;
5227 case SCTP_DELAYED_ACK:
5228 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
5229 optlen);
5230 break;
5231 case SCTP_INITMSG:
5232 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5233 break;
5234 case SCTP_GET_PEER_ADDRS:
5235 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5236 optlen);
5237 break;
5238 case SCTP_GET_LOCAL_ADDRS:
5239 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5240 optlen);
5241 break;
5242 case SCTP_SOCKOPT_CONNECTX3:
5243 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5244 break;
5245 case SCTP_DEFAULT_SEND_PARAM:
5246 retval = sctp_getsockopt_default_send_param(sk, len,
5247 optval, optlen);
5248 break;
5249 case SCTP_PRIMARY_ADDR:
5250 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5251 break;
5252 case SCTP_NODELAY:
5253 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5254 break;
5255 case SCTP_RTOINFO:
5256 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5257 break;
5258 case SCTP_ASSOCINFO:
5259 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5260 break;
5261 case SCTP_I_WANT_MAPPED_V4_ADDR:
5262 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5263 break;
5264 case SCTP_MAXSEG:
5265 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5266 break;
5267 case SCTP_GET_PEER_ADDR_INFO:
5268 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5269 optlen);
5270 break;
5271 case SCTP_ADAPTATION_LAYER:
5272 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
5273 optlen);
5274 break;
5275 case SCTP_CONTEXT:
5276 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5277 break;
5278 case SCTP_FRAGMENT_INTERLEAVE:
5279 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5280 optlen);
5281 break;
5282 case SCTP_PARTIAL_DELIVERY_POINT:
5283 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5284 optlen);
5285 break;
5286 case SCTP_MAX_BURST:
5287 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5288 break;
5289 case SCTP_AUTH_KEY:
5290 case SCTP_AUTH_CHUNK:
5291 case SCTP_AUTH_DELETE_KEY:
5292 retval = -EOPNOTSUPP;
5293 break;
5294 case SCTP_HMAC_IDENT:
5295 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5296 break;
5297 case SCTP_AUTH_ACTIVE_KEY:
5298 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5299 break;
5300 case SCTP_PEER_AUTH_CHUNKS:
5301 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5302 optlen);
5303 break;
5304 case SCTP_LOCAL_AUTH_CHUNKS:
5305 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5306 optlen);
5307 break;
5308 case SCTP_GET_ASSOC_NUMBER:
5309 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5310 break;
5311 default:
5312 retval = -ENOPROTOOPT;
5313 break;
5316 sctp_release_sock(sk);
5317 return retval;
5320 static void sctp_hash(struct sock *sk)
5322 /* STUB */
5325 static void sctp_unhash(struct sock *sk)
5327 /* STUB */
5330 static struct sctp_bind_bucket *sctp_bucket_create(
5331 struct sctp_bind_hashbucket *head, unsigned short snum);
5333 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5335 struct sctp_bind_hashbucket *head; /* hash list */
5336 struct sctp_bind_bucket *pp; /* hash list port iterator */
5337 struct hlist_node *node;
5338 unsigned short snum;
5339 int ret;
5341 snum = ntohs(addr->v4.sin_port);
5343 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5344 sctp_local_bh_disable();
5346 if (snum == 0) {
5347 /* Search for an available port. */
5348 int low, high, remaining, index;
5349 unsigned int rover;
5351 inet_get_local_port_range(&low, &high);
5352 remaining = (high - low) + 1;
5353 rover = net_random() % remaining + low;
5355 do {
5356 rover++;
5357 if ((rover < low) || (rover > high))
5358 rover = low;
5359 if (inet_is_reserved_local_port(rover))
5360 continue;
5361 index = sctp_phashfn(rover);
5362 head = &sctp_port_hashtable[index];
5363 sctp_spin_lock(&head->lock);
5364 sctp_for_each_hentry(pp, node, &head->chain)
5365 if (pp->port == rover)
5366 goto next;
5367 break;
5368 next:
5369 sctp_spin_unlock(&head->lock);
5370 } while (--remaining > 0);
5372 /* Exhausted local port range during search? */
5373 ret = 1;
5374 if (remaining <= 0)
5375 goto fail;
5377 /* OK, here is the one we will use. HEAD (the port
5378 * hash table list entry) is non-NULL and we hold it's
5379 * mutex.
5381 snum = rover;
5382 } else {
5383 /* We are given an specific port number; we verify
5384 * that it is not being used. If it is used, we will
5385 * exahust the search in the hash list corresponding
5386 * to the port number (snum) - we detect that with the
5387 * port iterator, pp being NULL.
5389 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5390 sctp_spin_lock(&head->lock);
5391 sctp_for_each_hentry(pp, node, &head->chain) {
5392 if (pp->port == snum)
5393 goto pp_found;
5396 pp = NULL;
5397 goto pp_not_found;
5398 pp_found:
5399 if (!hlist_empty(&pp->owner)) {
5400 /* We had a port hash table hit - there is an
5401 * available port (pp != NULL) and it is being
5402 * used by other socket (pp->owner not empty); that other
5403 * socket is going to be sk2.
5405 int reuse = sk->sk_reuse;
5406 struct sock *sk2;
5408 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5409 if (pp->fastreuse && sk->sk_reuse &&
5410 sk->sk_state != SCTP_SS_LISTENING)
5411 goto success;
5413 /* Run through the list of sockets bound to the port
5414 * (pp->port) [via the pointers bind_next and
5415 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5416 * we get the endpoint they describe and run through
5417 * the endpoint's list of IP (v4 or v6) addresses,
5418 * comparing each of the addresses with the address of
5419 * the socket sk. If we find a match, then that means
5420 * that this port/socket (sk) combination are already
5421 * in an endpoint.
5423 sk_for_each_bound(sk2, node, &pp->owner) {
5424 struct sctp_endpoint *ep2;
5425 ep2 = sctp_sk(sk2)->ep;
5427 if (sk == sk2 ||
5428 (reuse && sk2->sk_reuse &&
5429 sk2->sk_state != SCTP_SS_LISTENING))
5430 continue;
5432 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5433 sctp_sk(sk2), sctp_sk(sk))) {
5434 ret = (long)sk2;
5435 goto fail_unlock;
5438 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5440 pp_not_found:
5441 /* If there was a hash table miss, create a new port. */
5442 ret = 1;
5443 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5444 goto fail_unlock;
5446 /* In either case (hit or miss), make sure fastreuse is 1 only
5447 * if sk->sk_reuse is too (that is, if the caller requested
5448 * SO_REUSEADDR on this socket -sk-).
5450 if (hlist_empty(&pp->owner)) {
5451 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5452 pp->fastreuse = 1;
5453 else
5454 pp->fastreuse = 0;
5455 } else if (pp->fastreuse &&
5456 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5457 pp->fastreuse = 0;
5459 success:
5460 if (!sctp_sk(sk)->bind_hash) {
5461 inet_sk(sk)->inet_num = snum;
5462 sk_add_bind_node(sk, &pp->owner);
5463 sctp_sk(sk)->bind_hash = pp;
5465 ret = 0;
5467 fail_unlock:
5468 sctp_spin_unlock(&head->lock);
5470 fail:
5471 sctp_local_bh_enable();
5472 return ret;
5475 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5476 * port is requested.
5478 static int sctp_get_port(struct sock *sk, unsigned short snum)
5480 long ret;
5481 union sctp_addr addr;
5482 struct sctp_af *af = sctp_sk(sk)->pf->af;
5484 /* Set up a dummy address struct from the sk. */
5485 af->from_sk(&addr, sk);
5486 addr.v4.sin_port = htons(snum);
5488 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5489 ret = sctp_get_port_local(sk, &addr);
5491 return (ret ? 1 : 0);
5495 * Move a socket to LISTENING state.
5497 SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
5499 struct sctp_sock *sp = sctp_sk(sk);
5500 struct sctp_endpoint *ep = sp->ep;
5501 struct crypto_hash *tfm = NULL;
5503 /* Allocate HMAC for generating cookie. */
5504 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
5505 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5506 if (IS_ERR(tfm)) {
5507 if (net_ratelimit()) {
5508 printk(KERN_INFO
5509 "SCTP: failed to load transform for %s: %ld\n",
5510 sctp_hmac_alg, PTR_ERR(tfm));
5512 return -ENOSYS;
5514 sctp_sk(sk)->hmac = tfm;
5518 * If a bind() or sctp_bindx() is not called prior to a listen()
5519 * call that allows new associations to be accepted, the system
5520 * picks an ephemeral port and will choose an address set equivalent
5521 * to binding with a wildcard address.
5523 * This is not currently spelled out in the SCTP sockets
5524 * extensions draft, but follows the practice as seen in TCP
5525 * sockets.
5528 sk->sk_state = SCTP_SS_LISTENING;
5529 if (!ep->base.bind_addr.port) {
5530 if (sctp_autobind(sk))
5531 return -EAGAIN;
5532 } else {
5533 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
5534 sk->sk_state = SCTP_SS_CLOSED;
5535 return -EADDRINUSE;
5539 sk->sk_max_ack_backlog = backlog;
5540 sctp_hash_endpoint(ep);
5541 return 0;
5545 * 4.1.3 / 5.1.3 listen()
5547 * By default, new associations are not accepted for UDP style sockets.
5548 * An application uses listen() to mark a socket as being able to
5549 * accept new associations.
5551 * On TCP style sockets, applications use listen() to ready the SCTP
5552 * endpoint for accepting inbound associations.
5554 * On both types of endpoints a backlog of '0' disables listening.
5556 * Move a socket to LISTENING state.
5558 int sctp_inet_listen(struct socket *sock, int backlog)
5560 struct sock *sk = sock->sk;
5561 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
5562 int err = -EINVAL;
5564 if (unlikely(backlog < 0))
5565 return err;
5567 sctp_lock_sock(sk);
5569 /* Peeled-off sockets are not allowed to listen(). */
5570 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
5571 goto out;
5573 if (sock->state != SS_UNCONNECTED)
5574 goto out;
5576 /* If backlog is zero, disable listening. */
5577 if (!backlog) {
5578 if (sctp_sstate(sk, CLOSED))
5579 goto out;
5581 err = 0;
5582 sctp_unhash_endpoint(ep);
5583 sk->sk_state = SCTP_SS_CLOSED;
5584 if (sk->sk_reuse)
5585 sctp_sk(sk)->bind_hash->fastreuse = 1;
5586 goto out;
5589 /* If we are already listening, just update the backlog */
5590 if (sctp_sstate(sk, LISTENING))
5591 sk->sk_max_ack_backlog = backlog;
5592 else {
5593 err = sctp_listen_start(sk, backlog);
5594 if (err)
5595 goto out;
5598 err = 0;
5599 out:
5600 sctp_release_sock(sk);
5601 return err;
5605 * This function is done by modeling the current datagram_poll() and the
5606 * tcp_poll(). Note that, based on these implementations, we don't
5607 * lock the socket in this function, even though it seems that,
5608 * ideally, locking or some other mechanisms can be used to ensure
5609 * the integrity of the counters (sndbuf and wmem_alloc) used
5610 * in this place. We assume that we don't need locks either until proven
5611 * otherwise.
5613 * Another thing to note is that we include the Async I/O support
5614 * here, again, by modeling the current TCP/UDP code. We don't have
5615 * a good way to test with it yet.
5617 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5619 struct sock *sk = sock->sk;
5620 struct sctp_sock *sp = sctp_sk(sk);
5621 unsigned int mask;
5623 poll_wait(file, sk_sleep(sk), wait);
5625 /* A TCP-style listening socket becomes readable when the accept queue
5626 * is not empty.
5628 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5629 return (!list_empty(&sp->ep->asocs)) ?
5630 (POLLIN | POLLRDNORM) : 0;
5632 mask = 0;
5634 /* Is there any exceptional events? */
5635 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5636 mask |= POLLERR;
5637 if (sk->sk_shutdown & RCV_SHUTDOWN)
5638 mask |= POLLRDHUP;
5639 if (sk->sk_shutdown == SHUTDOWN_MASK)
5640 mask |= POLLHUP;
5642 /* Is it readable? Reconsider this code with TCP-style support. */
5643 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5644 (sk->sk_shutdown & RCV_SHUTDOWN))
5645 mask |= POLLIN | POLLRDNORM;
5647 /* The association is either gone or not ready. */
5648 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5649 return mask;
5651 /* Is it writable? */
5652 if (sctp_writeable(sk)) {
5653 mask |= POLLOUT | POLLWRNORM;
5654 } else {
5655 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5657 * Since the socket is not locked, the buffer
5658 * might be made available after the writeable check and
5659 * before the bit is set. This could cause a lost I/O
5660 * signal. tcp_poll() has a race breaker for this race
5661 * condition. Based on their implementation, we put
5662 * in the following code to cover it as well.
5664 if (sctp_writeable(sk))
5665 mask |= POLLOUT | POLLWRNORM;
5667 return mask;
5670 /********************************************************************
5671 * 2nd Level Abstractions
5672 ********************************************************************/
5674 static struct sctp_bind_bucket *sctp_bucket_create(
5675 struct sctp_bind_hashbucket *head, unsigned short snum)
5677 struct sctp_bind_bucket *pp;
5679 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5680 if (pp) {
5681 SCTP_DBG_OBJCNT_INC(bind_bucket);
5682 pp->port = snum;
5683 pp->fastreuse = 0;
5684 INIT_HLIST_HEAD(&pp->owner);
5685 hlist_add_head(&pp->node, &head->chain);
5687 return pp;
5690 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5691 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5693 if (pp && hlist_empty(&pp->owner)) {
5694 __hlist_del(&pp->node);
5695 kmem_cache_free(sctp_bucket_cachep, pp);
5696 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5700 /* Release this socket's reference to a local port. */
5701 static inline void __sctp_put_port(struct sock *sk)
5703 struct sctp_bind_hashbucket *head =
5704 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)];
5705 struct sctp_bind_bucket *pp;
5707 sctp_spin_lock(&head->lock);
5708 pp = sctp_sk(sk)->bind_hash;
5709 __sk_del_bind_node(sk);
5710 sctp_sk(sk)->bind_hash = NULL;
5711 inet_sk(sk)->inet_num = 0;
5712 sctp_bucket_destroy(pp);
5713 sctp_spin_unlock(&head->lock);
5716 void sctp_put_port(struct sock *sk)
5718 sctp_local_bh_disable();
5719 __sctp_put_port(sk);
5720 sctp_local_bh_enable();
5724 * The system picks an ephemeral port and choose an address set equivalent
5725 * to binding with a wildcard address.
5726 * One of those addresses will be the primary address for the association.
5727 * This automatically enables the multihoming capability of SCTP.
5729 static int sctp_autobind(struct sock *sk)
5731 union sctp_addr autoaddr;
5732 struct sctp_af *af;
5733 __be16 port;
5735 /* Initialize a local sockaddr structure to INADDR_ANY. */
5736 af = sctp_sk(sk)->pf->af;
5738 port = htons(inet_sk(sk)->inet_num);
5739 af->inaddr_any(&autoaddr, port);
5741 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5744 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5746 * From RFC 2292
5747 * 4.2 The cmsghdr Structure *
5749 * When ancillary data is sent or received, any number of ancillary data
5750 * objects can be specified by the msg_control and msg_controllen members of
5751 * the msghdr structure, because each object is preceded by
5752 * a cmsghdr structure defining the object's length (the cmsg_len member).
5753 * Historically Berkeley-derived implementations have passed only one object
5754 * at a time, but this API allows multiple objects to be
5755 * passed in a single call to sendmsg() or recvmsg(). The following example
5756 * shows two ancillary data objects in a control buffer.
5758 * |<--------------------------- msg_controllen -------------------------->|
5759 * | |
5761 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5763 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5764 * | | |
5766 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5768 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5769 * | | | | |
5771 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5772 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5774 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5776 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5780 * msg_control
5781 * points here
5783 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5784 sctp_cmsgs_t *cmsgs)
5786 struct cmsghdr *cmsg;
5787 struct msghdr *my_msg = (struct msghdr *)msg;
5789 for (cmsg = CMSG_FIRSTHDR(msg);
5790 cmsg != NULL;
5791 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
5792 if (!CMSG_OK(my_msg, cmsg))
5793 return -EINVAL;
5795 /* Should we parse this header or ignore? */
5796 if (cmsg->cmsg_level != IPPROTO_SCTP)
5797 continue;
5799 /* Strictly check lengths following example in SCM code. */
5800 switch (cmsg->cmsg_type) {
5801 case SCTP_INIT:
5802 /* SCTP Socket API Extension
5803 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5805 * This cmsghdr structure provides information for
5806 * initializing new SCTP associations with sendmsg().
5807 * The SCTP_INITMSG socket option uses this same data
5808 * structure. This structure is not used for
5809 * recvmsg().
5811 * cmsg_level cmsg_type cmsg_data[]
5812 * ------------ ------------ ----------------------
5813 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5815 if (cmsg->cmsg_len !=
5816 CMSG_LEN(sizeof(struct sctp_initmsg)))
5817 return -EINVAL;
5818 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5819 break;
5821 case SCTP_SNDRCV:
5822 /* SCTP Socket API Extension
5823 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5825 * This cmsghdr structure specifies SCTP options for
5826 * sendmsg() and describes SCTP header information
5827 * about a received message through recvmsg().
5829 * cmsg_level cmsg_type cmsg_data[]
5830 * ------------ ------------ ----------------------
5831 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5833 if (cmsg->cmsg_len !=
5834 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5835 return -EINVAL;
5837 cmsgs->info =
5838 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5840 /* Minimally, validate the sinfo_flags. */
5841 if (cmsgs->info->sinfo_flags &
5842 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5843 SCTP_ABORT | SCTP_EOF))
5844 return -EINVAL;
5845 break;
5847 default:
5848 return -EINVAL;
5851 return 0;
5855 * Wait for a packet..
5856 * Note: This function is the same function as in core/datagram.c
5857 * with a few modifications to make lksctp work.
5859 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5861 int error;
5862 DEFINE_WAIT(wait);
5864 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
5866 /* Socket errors? */
5867 error = sock_error(sk);
5868 if (error)
5869 goto out;
5871 if (!skb_queue_empty(&sk->sk_receive_queue))
5872 goto ready;
5874 /* Socket shut down? */
5875 if (sk->sk_shutdown & RCV_SHUTDOWN)
5876 goto out;
5878 /* Sequenced packets can come disconnected. If so we report the
5879 * problem.
5881 error = -ENOTCONN;
5883 /* Is there a good reason to think that we may receive some data? */
5884 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5885 goto out;
5887 /* Handle signals. */
5888 if (signal_pending(current))
5889 goto interrupted;
5891 /* Let another process have a go. Since we are going to sleep
5892 * anyway. Note: This may cause odd behaviors if the message
5893 * does not fit in the user's buffer, but this seems to be the
5894 * only way to honor MSG_DONTWAIT realistically.
5896 sctp_release_sock(sk);
5897 *timeo_p = schedule_timeout(*timeo_p);
5898 sctp_lock_sock(sk);
5900 ready:
5901 finish_wait(sk_sleep(sk), &wait);
5902 return 0;
5904 interrupted:
5905 error = sock_intr_errno(*timeo_p);
5907 out:
5908 finish_wait(sk_sleep(sk), &wait);
5909 *err = error;
5910 return error;
5913 /* Receive a datagram.
5914 * Note: This is pretty much the same routine as in core/datagram.c
5915 * with a few changes to make lksctp work.
5917 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5918 int noblock, int *err)
5920 int error;
5921 struct sk_buff *skb;
5922 long timeo;
5924 timeo = sock_rcvtimeo(sk, noblock);
5926 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5927 timeo, MAX_SCHEDULE_TIMEOUT);
5929 do {
5930 /* Again only user level code calls this function,
5931 * so nothing interrupt level
5932 * will suddenly eat the receive_queue.
5934 * Look at current nfs client by the way...
5935 * However, this function was corrent in any case. 8)
5937 if (flags & MSG_PEEK) {
5938 spin_lock_bh(&sk->sk_receive_queue.lock);
5939 skb = skb_peek(&sk->sk_receive_queue);
5940 if (skb)
5941 atomic_inc(&skb->users);
5942 spin_unlock_bh(&sk->sk_receive_queue.lock);
5943 } else {
5944 skb = skb_dequeue(&sk->sk_receive_queue);
5947 if (skb)
5948 return skb;
5950 /* Caller is allowed not to check sk->sk_err before calling. */
5951 error = sock_error(sk);
5952 if (error)
5953 goto no_packet;
5955 if (sk->sk_shutdown & RCV_SHUTDOWN)
5956 break;
5958 /* User doesn't want to wait. */
5959 error = -EAGAIN;
5960 if (!timeo)
5961 goto no_packet;
5962 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5964 return NULL;
5966 no_packet:
5967 *err = error;
5968 return NULL;
5971 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5972 static void __sctp_write_space(struct sctp_association *asoc)
5974 struct sock *sk = asoc->base.sk;
5975 struct socket *sock = sk->sk_socket;
5977 if ((sctp_wspace(asoc) > 0) && sock) {
5978 if (waitqueue_active(&asoc->wait))
5979 wake_up_interruptible(&asoc->wait);
5981 if (sctp_writeable(sk)) {
5982 if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
5983 wake_up_interruptible(sk_sleep(sk));
5985 /* Note that we try to include the Async I/O support
5986 * here by modeling from the current TCP/UDP code.
5987 * We have not tested with it yet.
5989 if (sock->wq->fasync_list &&
5990 !(sk->sk_shutdown & SEND_SHUTDOWN))
5991 sock_wake_async(sock,
5992 SOCK_WAKE_SPACE, POLL_OUT);
5997 /* Do accounting for the sndbuf space.
5998 * Decrement the used sndbuf space of the corresponding association by the
5999 * data size which was just transmitted(freed).
6001 static void sctp_wfree(struct sk_buff *skb)
6003 struct sctp_association *asoc;
6004 struct sctp_chunk *chunk;
6005 struct sock *sk;
6007 /* Get the saved chunk pointer. */
6008 chunk = *((struct sctp_chunk **)(skb->cb));
6009 asoc = chunk->asoc;
6010 sk = asoc->base.sk;
6011 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6012 sizeof(struct sk_buff) +
6013 sizeof(struct sctp_chunk);
6015 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6018 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
6020 sk->sk_wmem_queued -= skb->truesize;
6021 sk_mem_uncharge(sk, skb->truesize);
6023 sock_wfree(skb);
6024 __sctp_write_space(asoc);
6026 sctp_association_put(asoc);
6029 /* Do accounting for the receive space on the socket.
6030 * Accounting for the association is done in ulpevent.c
6031 * We set this as a destructor for the cloned data skbs so that
6032 * accounting is done at the correct time.
6034 void sctp_sock_rfree(struct sk_buff *skb)
6036 struct sock *sk = skb->sk;
6037 struct sctp_ulpevent *event = sctp_skb2event(skb);
6039 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
6042 * Mimic the behavior of sock_rfree
6044 sk_mem_uncharge(sk, event->rmem_len);
6048 /* Helper function to wait for space in the sndbuf. */
6049 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6050 size_t msg_len)
6052 struct sock *sk = asoc->base.sk;
6053 int err = 0;
6054 long current_timeo = *timeo_p;
6055 DEFINE_WAIT(wait);
6057 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
6058 asoc, (long)(*timeo_p), msg_len);
6060 /* Increment the association's refcnt. */
6061 sctp_association_hold(asoc);
6063 /* Wait on the association specific sndbuf space. */
6064 for (;;) {
6065 prepare_to_wait_exclusive(&asoc->wait, &wait,
6066 TASK_INTERRUPTIBLE);
6067 if (!*timeo_p)
6068 goto do_nonblock;
6069 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6070 asoc->base.dead)
6071 goto do_error;
6072 if (signal_pending(current))
6073 goto do_interrupted;
6074 if (msg_len <= sctp_wspace(asoc))
6075 break;
6077 /* Let another process have a go. Since we are going
6078 * to sleep anyway.
6080 sctp_release_sock(sk);
6081 current_timeo = schedule_timeout(current_timeo);
6082 BUG_ON(sk != asoc->base.sk);
6083 sctp_lock_sock(sk);
6085 *timeo_p = current_timeo;
6088 out:
6089 finish_wait(&asoc->wait, &wait);
6091 /* Release the association's refcnt. */
6092 sctp_association_put(asoc);
6094 return err;
6096 do_error:
6097 err = -EPIPE;
6098 goto out;
6100 do_interrupted:
6101 err = sock_intr_errno(*timeo_p);
6102 goto out;
6104 do_nonblock:
6105 err = -EAGAIN;
6106 goto out;
6109 void sctp_data_ready(struct sock *sk, int len)
6111 struct socket_wq *wq;
6113 rcu_read_lock();
6114 wq = rcu_dereference(sk->sk_wq);
6115 if (wq_has_sleeper(wq))
6116 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
6117 POLLRDNORM | POLLRDBAND);
6118 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
6119 rcu_read_unlock();
6122 /* If socket sndbuf has changed, wake up all per association waiters. */
6123 void sctp_write_space(struct sock *sk)
6125 struct sctp_association *asoc;
6127 /* Wake up the tasks in each wait queue. */
6128 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
6129 __sctp_write_space(asoc);
6133 /* Is there any sndbuf space available on the socket?
6135 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
6136 * associations on the same socket. For a UDP-style socket with
6137 * multiple associations, it is possible for it to be "unwriteable"
6138 * prematurely. I assume that this is acceptable because
6139 * a premature "unwriteable" is better than an accidental "writeable" which
6140 * would cause an unwanted block under certain circumstances. For the 1-1
6141 * UDP-style sockets or TCP-style sockets, this code should work.
6142 * - Daisy
6144 static int sctp_writeable(struct sock *sk)
6146 int amt = 0;
6148 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
6149 if (amt < 0)
6150 amt = 0;
6151 return amt;
6154 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6155 * returns immediately with EINPROGRESS.
6157 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6159 struct sock *sk = asoc->base.sk;
6160 int err = 0;
6161 long current_timeo = *timeo_p;
6162 DEFINE_WAIT(wait);
6164 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
6165 (long)(*timeo_p));
6167 /* Increment the association's refcnt. */
6168 sctp_association_hold(asoc);
6170 for (;;) {
6171 prepare_to_wait_exclusive(&asoc->wait, &wait,
6172 TASK_INTERRUPTIBLE);
6173 if (!*timeo_p)
6174 goto do_nonblock;
6175 if (sk->sk_shutdown & RCV_SHUTDOWN)
6176 break;
6177 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6178 asoc->base.dead)
6179 goto do_error;
6180 if (signal_pending(current))
6181 goto do_interrupted;
6183 if (sctp_state(asoc, ESTABLISHED))
6184 break;
6186 /* Let another process have a go. Since we are going
6187 * to sleep anyway.
6189 sctp_release_sock(sk);
6190 current_timeo = schedule_timeout(current_timeo);
6191 sctp_lock_sock(sk);
6193 *timeo_p = current_timeo;
6196 out:
6197 finish_wait(&asoc->wait, &wait);
6199 /* Release the association's refcnt. */
6200 sctp_association_put(asoc);
6202 return err;
6204 do_error:
6205 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
6206 err = -ETIMEDOUT;
6207 else
6208 err = -ECONNREFUSED;
6209 goto out;
6211 do_interrupted:
6212 err = sock_intr_errno(*timeo_p);
6213 goto out;
6215 do_nonblock:
6216 err = -EINPROGRESS;
6217 goto out;
6220 static int sctp_wait_for_accept(struct sock *sk, long timeo)
6222 struct sctp_endpoint *ep;
6223 int err = 0;
6224 DEFINE_WAIT(wait);
6226 ep = sctp_sk(sk)->ep;
6229 for (;;) {
6230 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
6231 TASK_INTERRUPTIBLE);
6233 if (list_empty(&ep->asocs)) {
6234 sctp_release_sock(sk);
6235 timeo = schedule_timeout(timeo);
6236 sctp_lock_sock(sk);
6239 err = -EINVAL;
6240 if (!sctp_sstate(sk, LISTENING))
6241 break;
6243 err = 0;
6244 if (!list_empty(&ep->asocs))
6245 break;
6247 err = sock_intr_errno(timeo);
6248 if (signal_pending(current))
6249 break;
6251 err = -EAGAIN;
6252 if (!timeo)
6253 break;
6256 finish_wait(sk_sleep(sk), &wait);
6258 return err;
6261 static void sctp_wait_for_close(struct sock *sk, long timeout)
6263 DEFINE_WAIT(wait);
6265 do {
6266 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
6267 if (list_empty(&sctp_sk(sk)->ep->asocs))
6268 break;
6269 sctp_release_sock(sk);
6270 timeout = schedule_timeout(timeout);
6271 sctp_lock_sock(sk);
6272 } while (!signal_pending(current) && timeout);
6274 finish_wait(sk_sleep(sk), &wait);
6277 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6279 struct sk_buff *frag;
6281 if (!skb->data_len)
6282 goto done;
6284 /* Don't forget the fragments. */
6285 skb_walk_frags(skb, frag)
6286 sctp_skb_set_owner_r_frag(frag, sk);
6288 done:
6289 sctp_skb_set_owner_r(skb, sk);
6292 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6293 struct sctp_association *asoc)
6295 struct inet_sock *inet = inet_sk(sk);
6296 struct inet_sock *newinet;
6298 newsk->sk_type = sk->sk_type;
6299 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6300 newsk->sk_flags = sk->sk_flags;
6301 newsk->sk_no_check = sk->sk_no_check;
6302 newsk->sk_reuse = sk->sk_reuse;
6304 newsk->sk_shutdown = sk->sk_shutdown;
6305 newsk->sk_destruct = inet_sock_destruct;
6306 newsk->sk_family = sk->sk_family;
6307 newsk->sk_protocol = IPPROTO_SCTP;
6308 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6309 newsk->sk_sndbuf = sk->sk_sndbuf;
6310 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6311 newsk->sk_lingertime = sk->sk_lingertime;
6312 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6313 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6315 newinet = inet_sk(newsk);
6317 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6318 * getsockname() and getpeername()
6320 newinet->inet_sport = inet->inet_sport;
6321 newinet->inet_saddr = inet->inet_saddr;
6322 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6323 newinet->inet_dport = htons(asoc->peer.port);
6324 newinet->pmtudisc = inet->pmtudisc;
6325 newinet->inet_id = asoc->next_tsn ^ jiffies;
6327 newinet->uc_ttl = inet->uc_ttl;
6328 newinet->mc_loop = 1;
6329 newinet->mc_ttl = 1;
6330 newinet->mc_index = 0;
6331 newinet->mc_list = NULL;
6334 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6335 * and its messages to the newsk.
6337 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6338 struct sctp_association *assoc,
6339 sctp_socket_type_t type)
6341 struct sctp_sock *oldsp = sctp_sk(oldsk);
6342 struct sctp_sock *newsp = sctp_sk(newsk);
6343 struct sctp_bind_bucket *pp; /* hash list port iterator */
6344 struct sctp_endpoint *newep = newsp->ep;
6345 struct sk_buff *skb, *tmp;
6346 struct sctp_ulpevent *event;
6347 struct sctp_bind_hashbucket *head;
6349 /* Migrate socket buffer sizes and all the socket level options to the
6350 * new socket.
6352 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6353 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6354 /* Brute force copy old sctp opt. */
6355 inet_sk_copy_descendant(newsk, oldsk);
6357 /* Restore the ep value that was overwritten with the above structure
6358 * copy.
6360 newsp->ep = newep;
6361 newsp->hmac = NULL;
6363 /* Hook this new socket in to the bind_hash list. */
6364 head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)];
6365 sctp_local_bh_disable();
6366 sctp_spin_lock(&head->lock);
6367 pp = sctp_sk(oldsk)->bind_hash;
6368 sk_add_bind_node(newsk, &pp->owner);
6369 sctp_sk(newsk)->bind_hash = pp;
6370 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
6371 sctp_spin_unlock(&head->lock);
6372 sctp_local_bh_enable();
6374 /* Copy the bind_addr list from the original endpoint to the new
6375 * endpoint so that we can handle restarts properly
6377 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6378 &oldsp->ep->base.bind_addr, GFP_KERNEL);
6380 /* Move any messages in the old socket's receive queue that are for the
6381 * peeled off association to the new socket's receive queue.
6383 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6384 event = sctp_skb2event(skb);
6385 if (event->asoc == assoc) {
6386 __skb_unlink(skb, &oldsk->sk_receive_queue);
6387 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6388 sctp_skb_set_owner_r_frag(skb, newsk);
6392 /* Clean up any messages pending delivery due to partial
6393 * delivery. Three cases:
6394 * 1) No partial deliver; no work.
6395 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6396 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6398 skb_queue_head_init(&newsp->pd_lobby);
6399 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6401 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6402 struct sk_buff_head *queue;
6404 /* Decide which queue to move pd_lobby skbs to. */
6405 if (assoc->ulpq.pd_mode) {
6406 queue = &newsp->pd_lobby;
6407 } else
6408 queue = &newsk->sk_receive_queue;
6410 /* Walk through the pd_lobby, looking for skbs that
6411 * need moved to the new socket.
6413 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6414 event = sctp_skb2event(skb);
6415 if (event->asoc == assoc) {
6416 __skb_unlink(skb, &oldsp->pd_lobby);
6417 __skb_queue_tail(queue, skb);
6418 sctp_skb_set_owner_r_frag(skb, newsk);
6422 /* Clear up any skbs waiting for the partial
6423 * delivery to finish.
6425 if (assoc->ulpq.pd_mode)
6426 sctp_clear_pd(oldsk, NULL);
6430 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
6431 sctp_skb_set_owner_r_frag(skb, newsk);
6433 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
6434 sctp_skb_set_owner_r_frag(skb, newsk);
6436 /* Set the type of socket to indicate that it is peeled off from the
6437 * original UDP-style socket or created with the accept() call on a
6438 * TCP-style socket..
6440 newsp->type = type;
6442 /* Mark the new socket "in-use" by the user so that any packets
6443 * that may arrive on the association after we've moved it are
6444 * queued to the backlog. This prevents a potential race between
6445 * backlog processing on the old socket and new-packet processing
6446 * on the new socket.
6448 * The caller has just allocated newsk so we can guarantee that other
6449 * paths won't try to lock it and then oldsk.
6451 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6452 sctp_assoc_migrate(assoc, newsk);
6454 /* If the association on the newsk is already closed before accept()
6455 * is called, set RCV_SHUTDOWN flag.
6457 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6458 newsk->sk_shutdown |= RCV_SHUTDOWN;
6460 newsk->sk_state = SCTP_SS_ESTABLISHED;
6461 sctp_release_sock(newsk);
6465 /* This proto struct describes the ULP interface for SCTP. */
6466 struct proto sctp_prot = {
6467 .name = "SCTP",
6468 .owner = THIS_MODULE,
6469 .close = sctp_close,
6470 .connect = sctp_connect,
6471 .disconnect = sctp_disconnect,
6472 .accept = sctp_accept,
6473 .ioctl = sctp_ioctl,
6474 .init = sctp_init_sock,
6475 .destroy = sctp_destroy_sock,
6476 .shutdown = sctp_shutdown,
6477 .setsockopt = sctp_setsockopt,
6478 .getsockopt = sctp_getsockopt,
6479 .sendmsg = sctp_sendmsg,
6480 .recvmsg = sctp_recvmsg,
6481 .bind = sctp_bind,
6482 .backlog_rcv = sctp_backlog_rcv,
6483 .hash = sctp_hash,
6484 .unhash = sctp_unhash,
6485 .get_port = sctp_get_port,
6486 .obj_size = sizeof(struct sctp_sock),
6487 .sysctl_mem = sysctl_sctp_mem,
6488 .sysctl_rmem = sysctl_sctp_rmem,
6489 .sysctl_wmem = sysctl_sctp_wmem,
6490 .memory_pressure = &sctp_memory_pressure,
6491 .enter_memory_pressure = sctp_enter_memory_pressure,
6492 .memory_allocated = &sctp_memory_allocated,
6493 .sockets_allocated = &sctp_sockets_allocated,
6496 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6498 struct proto sctpv6_prot = {
6499 .name = "SCTPv6",
6500 .owner = THIS_MODULE,
6501 .close = sctp_close,
6502 .connect = sctp_connect,
6503 .disconnect = sctp_disconnect,
6504 .accept = sctp_accept,
6505 .ioctl = sctp_ioctl,
6506 .init = sctp_init_sock,
6507 .destroy = sctp_destroy_sock,
6508 .shutdown = sctp_shutdown,
6509 .setsockopt = sctp_setsockopt,
6510 .getsockopt = sctp_getsockopt,
6511 .sendmsg = sctp_sendmsg,
6512 .recvmsg = sctp_recvmsg,
6513 .bind = sctp_bind,
6514 .backlog_rcv = sctp_backlog_rcv,
6515 .hash = sctp_hash,
6516 .unhash = sctp_unhash,
6517 .get_port = sctp_get_port,
6518 .obj_size = sizeof(struct sctp6_sock),
6519 .sysctl_mem = sysctl_sctp_mem,
6520 .sysctl_rmem = sysctl_sctp_rmem,
6521 .sysctl_wmem = sysctl_sctp_wmem,
6522 .memory_pressure = &sctp_memory_pressure,
6523 .enter_memory_pressure = sctp_enter_memory_pressure,
6524 .memory_allocated = &sctp_memory_allocated,
6525 .sockets_allocated = &sctp_sockets_allocated,
6527 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */