[SCTP]: Correctly set daddr for IPv6 sockets during peeloff
[linux-2.6/libata-dev.git] / net / sctp / socket.c
blobd370c945a7578e659c1ce9cd171bcedd30fa2950
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
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>
71 #include <net/ip.h>
72 #include <net/icmp.h>
73 #include <net/route.h>
74 #include <net/ipv6.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <net/sock.h>
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 extern struct kmem_cache *sctp_bucket_cachep;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
121 } else {
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
126 if (amt < 0)
127 amt = 0;
129 return amt;
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
139 * tracking.
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
149 skb_set_owner_w(chunk->skb, sk);
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
162 /* Verify that this is a valid address. */
163 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
164 int len)
166 struct sctp_af *af;
168 /* Verify basic sockaddr. */
169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
170 if (!af)
171 return -EINVAL;
173 /* Is this a valid SCTP address? */
174 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
175 return -EINVAL;
177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
178 return -EINVAL;
180 return 0;
183 /* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
186 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
188 struct sctp_association *asoc = NULL;
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk, UDP)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
196 if (!sctp_sstate(sk, ESTABLISHED))
197 return NULL;
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk)->ep->asocs))
201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
202 struct sctp_association, asocs);
203 return asoc;
206 /* Otherwise this is a UDP-style socket. */
207 if (!id || (id == (sctp_assoc_t)-1))
208 return NULL;
210 spin_lock_bh(&sctp_assocs_id_lock);
211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
212 spin_unlock_bh(&sctp_assocs_id_lock);
214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
215 return NULL;
217 return asoc;
220 /* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
222 * the same.
224 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
225 struct sockaddr_storage *addr,
226 sctp_assoc_t id)
228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
229 struct sctp_transport *transport;
230 union sctp_addr *laddr = (union sctp_addr *)addr;
232 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
233 laddr,
234 &transport);
236 if (!addr_asoc)
237 return NULL;
239 id_asoc = sctp_id2assoc(sk, id);
240 if (id_asoc && (id_asoc != addr_asoc))
241 return NULL;
243 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
244 (union sctp_addr *)addr);
246 return transport;
249 /* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
259 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
261 int retval = 0;
263 sctp_lock_sock(sk);
265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
266 sk, addr, addr_len);
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk)->ep->base.bind_addr.port)
270 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
271 addr_len);
272 else
273 retval = -EINVAL;
275 sctp_release_sock(sk);
277 return retval;
280 static long sctp_get_port_local(struct sock *, union sctp_addr *);
282 /* Verify this is a valid sockaddr. */
283 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
284 union sctp_addr *addr, int len)
286 struct sctp_af *af;
288 /* Check minimum size. */
289 if (len < sizeof (struct sockaddr))
290 return NULL;
292 /* Does this PF support this AF? */
293 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
294 return NULL;
296 /* If we get this far, af is valid. */
297 af = sctp_get_af_specific(addr->sa.sa_family);
299 if (len < af->sockaddr_len)
300 return NULL;
302 return af;
305 /* Bind a local address either to an endpoint or to an association. */
306 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
308 struct sctp_sock *sp = sctp_sk(sk);
309 struct sctp_endpoint *ep = sp->ep;
310 struct sctp_bind_addr *bp = &ep->base.bind_addr;
311 struct sctp_af *af;
312 unsigned short snum;
313 int ret = 0;
315 /* Common sockaddr verification. */
316 af = sctp_sockaddr_af(sp, addr, len);
317 if (!af) {
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
319 sk, addr, len);
320 return -EINVAL;
323 snum = ntohs(addr->v4.sin_port);
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
328 addr,
329 bp->port, snum,
330 len);
332 /* PF specific bind() address verification. */
333 if (!sp->pf->bind_verify(sp, addr))
334 return -EADDRNOTAVAIL;
336 /* We must either be unbound, or bind to the same port. */
337 if (bp->port && (snum != bp->port)) {
338 SCTP_DEBUG_PRINTK("sctp_do_bind:"
339 " New port %d does not match existing port "
340 "%d.\n", snum, bp->port);
341 return -EINVAL;
344 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
345 return -EACCES;
347 /* Make sure we are allowed to bind here.
348 * The function sctp_get_port_local() does duplicate address
349 * detection.
351 if ((ret = sctp_get_port_local(sk, addr))) {
352 if (ret == (long) sk) {
353 /* This endpoint has a conflicting address. */
354 return -EINVAL;
355 } else {
356 return -EADDRINUSE;
360 /* Refresh ephemeral port. */
361 if (!bp->port)
362 bp->port = inet_sk(sk)->num;
364 /* Add the address to the bind address list. */
365 sctp_local_bh_disable();
366 sctp_write_lock(&ep->base.addr_lock);
368 /* Use GFP_ATOMIC since BHs are disabled. */
369 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
370 sctp_write_unlock(&ep->base.addr_lock);
371 sctp_local_bh_enable();
373 /* Copy back into socket for getsockname() use. */
374 if (!ret) {
375 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
376 af->to_sk_saddr(addr, sk);
379 return ret;
382 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
384 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
385 * at any one time. If a sender, after sending an ASCONF chunk, decides
386 * it needs to transfer another ASCONF Chunk, it MUST wait until the
387 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
388 * subsequent ASCONF. Note this restriction binds each side, so at any
389 * time two ASCONF may be in-transit on any given association (one sent
390 * from each endpoint).
392 static int sctp_send_asconf(struct sctp_association *asoc,
393 struct sctp_chunk *chunk)
395 int retval = 0;
397 /* If there is an outstanding ASCONF chunk, queue it for later
398 * transmission.
400 if (asoc->addip_last_asconf) {
401 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
402 goto out;
405 /* Hold the chunk until an ASCONF_ACK is received. */
406 sctp_chunk_hold(chunk);
407 retval = sctp_primitive_ASCONF(asoc, chunk);
408 if (retval)
409 sctp_chunk_free(chunk);
410 else
411 asoc->addip_last_asconf = chunk;
413 out:
414 return retval;
417 /* Add a list of addresses as bind addresses to local endpoint or
418 * association.
420 * Basically run through each address specified in the addrs/addrcnt
421 * array/length pair, determine if it is IPv6 or IPv4 and call
422 * sctp_do_bind() on it.
424 * If any of them fails, then the operation will be reversed and the
425 * ones that were added will be removed.
427 * Only sctp_setsockopt_bindx() is supposed to call this function.
429 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
431 int cnt;
432 int retval = 0;
433 void *addr_buf;
434 struct sockaddr *sa_addr;
435 struct sctp_af *af;
437 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
438 sk, addrs, addrcnt);
440 addr_buf = addrs;
441 for (cnt = 0; cnt < addrcnt; cnt++) {
442 /* The list may contain either IPv4 or IPv6 address;
443 * determine the address length for walking thru the list.
445 sa_addr = (struct sockaddr *)addr_buf;
446 af = sctp_get_af_specific(sa_addr->sa_family);
447 if (!af) {
448 retval = -EINVAL;
449 goto err_bindx_add;
452 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
453 af->sockaddr_len);
455 addr_buf += af->sockaddr_len;
457 err_bindx_add:
458 if (retval < 0) {
459 /* Failed. Cleanup the ones that have been added */
460 if (cnt > 0)
461 sctp_bindx_rem(sk, addrs, cnt);
462 return retval;
466 return retval;
469 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
470 * associations that are part of the endpoint indicating that a list of local
471 * addresses are added to the endpoint.
473 * If any of the addresses is already in the bind address list of the
474 * association, we do not send the chunk for that association. But it will not
475 * affect other associations.
477 * Only sctp_setsockopt_bindx() is supposed to call this function.
479 static int sctp_send_asconf_add_ip(struct sock *sk,
480 struct sockaddr *addrs,
481 int addrcnt)
483 struct sctp_sock *sp;
484 struct sctp_endpoint *ep;
485 struct sctp_association *asoc;
486 struct sctp_bind_addr *bp;
487 struct sctp_chunk *chunk;
488 struct sctp_sockaddr_entry *laddr;
489 union sctp_addr *addr;
490 union sctp_addr saveaddr;
491 void *addr_buf;
492 struct sctp_af *af;
493 struct list_head *pos;
494 struct list_head *p;
495 int i;
496 int retval = 0;
498 if (!sctp_addip_enable)
499 return retval;
501 sp = sctp_sk(sk);
502 ep = sp->ep;
504 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
505 __FUNCTION__, sk, addrs, addrcnt);
507 list_for_each(pos, &ep->asocs) {
508 asoc = list_entry(pos, struct sctp_association, asocs);
510 if (!asoc->peer.asconf_capable)
511 continue;
513 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
514 continue;
516 if (!sctp_state(asoc, ESTABLISHED))
517 continue;
519 /* Check if any address in the packed array of addresses is
520 * in the bind address list of the association. If so,
521 * do not send the asconf chunk to its peer, but continue with
522 * other associations.
524 addr_buf = addrs;
525 for (i = 0; i < addrcnt; i++) {
526 addr = (union sctp_addr *)addr_buf;
527 af = sctp_get_af_specific(addr->v4.sin_family);
528 if (!af) {
529 retval = -EINVAL;
530 goto out;
533 if (sctp_assoc_lookup_laddr(asoc, addr))
534 break;
536 addr_buf += af->sockaddr_len;
538 if (i < addrcnt)
539 continue;
541 /* Use the first address in bind addr list of association as
542 * Address Parameter of ASCONF CHUNK.
544 sctp_read_lock(&asoc->base.addr_lock);
545 bp = &asoc->base.bind_addr;
546 p = bp->address_list.next;
547 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
548 sctp_read_unlock(&asoc->base.addr_lock);
550 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
551 addrcnt, SCTP_PARAM_ADD_IP);
552 if (!chunk) {
553 retval = -ENOMEM;
554 goto out;
557 retval = sctp_send_asconf(asoc, chunk);
558 if (retval)
559 goto out;
561 /* Add the new addresses to the bind address list with
562 * use_as_src set to 0.
564 sctp_local_bh_disable();
565 sctp_write_lock(&asoc->base.addr_lock);
566 addr_buf = addrs;
567 for (i = 0; i < addrcnt; i++) {
568 addr = (union sctp_addr *)addr_buf;
569 af = sctp_get_af_specific(addr->v4.sin_family);
570 memcpy(&saveaddr, addr, af->sockaddr_len);
571 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
572 GFP_ATOMIC);
573 addr_buf += af->sockaddr_len;
575 sctp_write_unlock(&asoc->base.addr_lock);
576 sctp_local_bh_enable();
579 out:
580 return retval;
583 /* Remove a list of addresses from bind addresses list. Do not remove the
584 * last address.
586 * Basically run through each address specified in the addrs/addrcnt
587 * array/length pair, determine if it is IPv6 or IPv4 and call
588 * sctp_del_bind() on it.
590 * If any of them fails, then the operation will be reversed and the
591 * ones that were removed will be added back.
593 * At least one address has to be left; if only one address is
594 * available, the operation will return -EBUSY.
596 * Only sctp_setsockopt_bindx() is supposed to call this function.
598 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
600 struct sctp_sock *sp = sctp_sk(sk);
601 struct sctp_endpoint *ep = sp->ep;
602 int cnt;
603 struct sctp_bind_addr *bp = &ep->base.bind_addr;
604 int retval = 0;
605 void *addr_buf;
606 union sctp_addr *sa_addr;
607 struct sctp_af *af;
609 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
610 sk, addrs, addrcnt);
612 addr_buf = addrs;
613 for (cnt = 0; cnt < addrcnt; cnt++) {
614 /* If the bind address list is empty or if there is only one
615 * bind address, there is nothing more to be removed (we need
616 * at least one address here).
618 if (list_empty(&bp->address_list) ||
619 (sctp_list_single_entry(&bp->address_list))) {
620 retval = -EBUSY;
621 goto err_bindx_rem;
624 sa_addr = (union sctp_addr *)addr_buf;
625 af = sctp_get_af_specific(sa_addr->sa.sa_family);
626 if (!af) {
627 retval = -EINVAL;
628 goto err_bindx_rem;
631 if (!af->addr_valid(sa_addr, sp, NULL)) {
632 retval = -EADDRNOTAVAIL;
633 goto err_bindx_rem;
636 if (sa_addr->v4.sin_port != htons(bp->port)) {
637 retval = -EINVAL;
638 goto err_bindx_rem;
641 /* FIXME - There is probably a need to check if sk->sk_saddr and
642 * sk->sk_rcv_addr are currently set to one of the addresses to
643 * be removed. This is something which needs to be looked into
644 * when we are fixing the outstanding issues with multi-homing
645 * socket routing and failover schemes. Refer to comments in
646 * sctp_do_bind(). -daisy
648 sctp_local_bh_disable();
649 sctp_write_lock(&ep->base.addr_lock);
651 retval = sctp_del_bind_addr(bp, sa_addr);
653 sctp_write_unlock(&ep->base.addr_lock);
654 sctp_local_bh_enable();
656 addr_buf += af->sockaddr_len;
657 err_bindx_rem:
658 if (retval < 0) {
659 /* Failed. Add the ones that has been removed back */
660 if (cnt > 0)
661 sctp_bindx_add(sk, addrs, cnt);
662 return retval;
666 return retval;
669 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
670 * the associations that are part of the endpoint indicating that a list of
671 * local addresses are removed from the endpoint.
673 * If any of the addresses is already in the bind address list of the
674 * association, we do not send the chunk for that association. But it will not
675 * affect other associations.
677 * Only sctp_setsockopt_bindx() is supposed to call this function.
679 static int sctp_send_asconf_del_ip(struct sock *sk,
680 struct sockaddr *addrs,
681 int addrcnt)
683 struct sctp_sock *sp;
684 struct sctp_endpoint *ep;
685 struct sctp_association *asoc;
686 struct sctp_transport *transport;
687 struct sctp_bind_addr *bp;
688 struct sctp_chunk *chunk;
689 union sctp_addr *laddr;
690 void *addr_buf;
691 struct sctp_af *af;
692 struct list_head *pos, *pos1;
693 struct sctp_sockaddr_entry *saddr;
694 int i;
695 int retval = 0;
697 if (!sctp_addip_enable)
698 return retval;
700 sp = sctp_sk(sk);
701 ep = sp->ep;
703 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
704 __FUNCTION__, sk, addrs, addrcnt);
706 list_for_each(pos, &ep->asocs) {
707 asoc = list_entry(pos, struct sctp_association, asocs);
709 if (!asoc->peer.asconf_capable)
710 continue;
712 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
713 continue;
715 if (!sctp_state(asoc, ESTABLISHED))
716 continue;
718 /* Check if any address in the packed array of addresses is
719 * not present in the bind address list of the association.
720 * If so, do not send the asconf chunk to its peer, but
721 * continue with other associations.
723 addr_buf = addrs;
724 for (i = 0; i < addrcnt; i++) {
725 laddr = (union sctp_addr *)addr_buf;
726 af = sctp_get_af_specific(laddr->v4.sin_family);
727 if (!af) {
728 retval = -EINVAL;
729 goto out;
732 if (!sctp_assoc_lookup_laddr(asoc, laddr))
733 break;
735 addr_buf += af->sockaddr_len;
737 if (i < addrcnt)
738 continue;
740 /* Find one address in the association's bind address list
741 * that is not in the packed array of addresses. This is to
742 * make sure that we do not delete all the addresses in the
743 * association.
745 sctp_read_lock(&asoc->base.addr_lock);
746 bp = &asoc->base.bind_addr;
747 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
748 addrcnt, sp);
749 sctp_read_unlock(&asoc->base.addr_lock);
750 if (!laddr)
751 continue;
753 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
754 SCTP_PARAM_DEL_IP);
755 if (!chunk) {
756 retval = -ENOMEM;
757 goto out;
760 /* Reset use_as_src flag for the addresses in the bind address
761 * list that are to be deleted.
763 sctp_local_bh_disable();
764 sctp_write_lock(&asoc->base.addr_lock);
765 addr_buf = addrs;
766 for (i = 0; i < addrcnt; i++) {
767 laddr = (union sctp_addr *)addr_buf;
768 af = sctp_get_af_specific(laddr->v4.sin_family);
769 list_for_each(pos1, &bp->address_list) {
770 saddr = list_entry(pos1,
771 struct sctp_sockaddr_entry,
772 list);
773 if (sctp_cmp_addr_exact(&saddr->a, laddr))
774 saddr->use_as_src = 0;
776 addr_buf += af->sockaddr_len;
778 sctp_write_unlock(&asoc->base.addr_lock);
779 sctp_local_bh_enable();
781 /* Update the route and saddr entries for all the transports
782 * as some of the addresses in the bind address list are
783 * about to be deleted and cannot be used as source addresses.
785 list_for_each(pos1, &asoc->peer.transport_addr_list) {
786 transport = list_entry(pos1, struct sctp_transport,
787 transports);
788 dst_release(transport->dst);
789 sctp_transport_route(transport, NULL,
790 sctp_sk(asoc->base.sk));
793 retval = sctp_send_asconf(asoc, chunk);
795 out:
796 return retval;
799 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
801 * API 8.1
802 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
803 * int flags);
805 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
806 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
807 * or IPv6 addresses.
809 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
810 * Section 3.1.2 for this usage.
812 * addrs is a pointer to an array of one or more socket addresses. Each
813 * address is contained in its appropriate structure (i.e. struct
814 * sockaddr_in or struct sockaddr_in6) the family of the address type
815 * must be used to distinguish the address length (note that this
816 * representation is termed a "packed array" of addresses). The caller
817 * specifies the number of addresses in the array with addrcnt.
819 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
820 * -1, and sets errno to the appropriate error code.
822 * For SCTP, the port given in each socket address must be the same, or
823 * sctp_bindx() will fail, setting errno to EINVAL.
825 * The flags parameter is formed from the bitwise OR of zero or more of
826 * the following currently defined flags:
828 * SCTP_BINDX_ADD_ADDR
830 * SCTP_BINDX_REM_ADDR
832 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
833 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
834 * addresses from the association. The two flags are mutually exclusive;
835 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
836 * not remove all addresses from an association; sctp_bindx() will
837 * reject such an attempt with EINVAL.
839 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
840 * additional addresses with an endpoint after calling bind(). Or use
841 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
842 * socket is associated with so that no new association accepted will be
843 * associated with those addresses. If the endpoint supports dynamic
844 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
845 * endpoint to send the appropriate message to the peer to change the
846 * peers address lists.
848 * Adding and removing addresses from a connected association is
849 * optional functionality. Implementations that do not support this
850 * functionality should return EOPNOTSUPP.
852 * Basically do nothing but copying the addresses from user to kernel
853 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
854 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
855 * from userspace.
857 * We don't use copy_from_user() for optimization: we first do the
858 * sanity checks (buffer size -fast- and access check-healthy
859 * pointer); if all of those succeed, then we can alloc the memory
860 * (expensive operation) needed to copy the data to kernel. Then we do
861 * the copying without checking the user space area
862 * (__copy_from_user()).
864 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
865 * it.
867 * sk The sk of the socket
868 * addrs The pointer to the addresses in user land
869 * addrssize Size of the addrs buffer
870 * op Operation to perform (add or remove, see the flags of
871 * sctp_bindx)
873 * Returns 0 if ok, <0 errno code on error.
875 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
876 struct sockaddr __user *addrs,
877 int addrs_size, int op)
879 struct sockaddr *kaddrs;
880 int err;
881 int addrcnt = 0;
882 int walk_size = 0;
883 struct sockaddr *sa_addr;
884 void *addr_buf;
885 struct sctp_af *af;
887 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
888 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
890 if (unlikely(addrs_size <= 0))
891 return -EINVAL;
893 /* Check the user passed a healthy pointer. */
894 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
895 return -EFAULT;
897 /* Alloc space for the address array in kernel memory. */
898 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
899 if (unlikely(!kaddrs))
900 return -ENOMEM;
902 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
903 kfree(kaddrs);
904 return -EFAULT;
907 /* Walk through the addrs buffer and count the number of addresses. */
908 addr_buf = kaddrs;
909 while (walk_size < addrs_size) {
910 sa_addr = (struct sockaddr *)addr_buf;
911 af = sctp_get_af_specific(sa_addr->sa_family);
913 /* If the address family is not supported or if this address
914 * causes the address buffer to overflow return EINVAL.
916 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
917 kfree(kaddrs);
918 return -EINVAL;
920 addrcnt++;
921 addr_buf += af->sockaddr_len;
922 walk_size += af->sockaddr_len;
925 /* Do the work. */
926 switch (op) {
927 case SCTP_BINDX_ADD_ADDR:
928 err = sctp_bindx_add(sk, kaddrs, addrcnt);
929 if (err)
930 goto out;
931 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
932 break;
934 case SCTP_BINDX_REM_ADDR:
935 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
936 if (err)
937 goto out;
938 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
939 break;
941 default:
942 err = -EINVAL;
943 break;
946 out:
947 kfree(kaddrs);
949 return err;
952 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
954 * Common routine for handling connect() and sctp_connectx().
955 * Connect will come in with just a single address.
957 static int __sctp_connect(struct sock* sk,
958 struct sockaddr *kaddrs,
959 int addrs_size)
961 struct sctp_sock *sp;
962 struct sctp_endpoint *ep;
963 struct sctp_association *asoc = NULL;
964 struct sctp_association *asoc2;
965 struct sctp_transport *transport;
966 union sctp_addr to;
967 struct sctp_af *af;
968 sctp_scope_t scope;
969 long timeo;
970 int err = 0;
971 int addrcnt = 0;
972 int walk_size = 0;
973 union sctp_addr *sa_addr;
974 void *addr_buf;
975 unsigned short port;
977 sp = sctp_sk(sk);
978 ep = sp->ep;
980 /* connect() cannot be done on a socket that is already in ESTABLISHED
981 * state - UDP-style peeled off socket or a TCP-style socket that
982 * is already connected.
983 * It cannot be done even on a TCP-style listening socket.
985 if (sctp_sstate(sk, ESTABLISHED) ||
986 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
987 err = -EISCONN;
988 goto out_free;
991 /* Walk through the addrs buffer and count the number of addresses. */
992 addr_buf = kaddrs;
993 while (walk_size < addrs_size) {
994 sa_addr = (union sctp_addr *)addr_buf;
995 af = sctp_get_af_specific(sa_addr->sa.sa_family);
996 port = ntohs(sa_addr->v4.sin_port);
998 /* If the address family is not supported or if this address
999 * causes the address buffer to overflow return EINVAL.
1001 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1002 err = -EINVAL;
1003 goto out_free;
1006 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
1007 if (err)
1008 goto out_free;
1010 /* Make sure the destination port is correctly set
1011 * in all addresses.
1013 if (asoc && asoc->peer.port && asoc->peer.port != port)
1014 goto out_free;
1016 memcpy(&to, sa_addr, af->sockaddr_len);
1018 /* Check if there already is a matching association on the
1019 * endpoint (other than the one created here).
1021 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
1022 if (asoc2 && asoc2 != asoc) {
1023 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1024 err = -EISCONN;
1025 else
1026 err = -EALREADY;
1027 goto out_free;
1030 /* If we could not find a matching association on the endpoint,
1031 * make sure that there is no peeled-off association matching
1032 * the peer address even on another socket.
1034 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
1035 err = -EADDRNOTAVAIL;
1036 goto out_free;
1039 if (!asoc) {
1040 /* If a bind() or sctp_bindx() is not called prior to
1041 * an sctp_connectx() call, the system picks an
1042 * ephemeral port and will choose an address set
1043 * equivalent to binding with a wildcard address.
1045 if (!ep->base.bind_addr.port) {
1046 if (sctp_autobind(sk)) {
1047 err = -EAGAIN;
1048 goto out_free;
1050 } else {
1052 * If an unprivileged user inherits a 1-many
1053 * style socket with open associations on a
1054 * privileged port, it MAY be permitted to
1055 * accept new associations, but it SHOULD NOT
1056 * be permitted to open new associations.
1058 if (ep->base.bind_addr.port < PROT_SOCK &&
1059 !capable(CAP_NET_BIND_SERVICE)) {
1060 err = -EACCES;
1061 goto out_free;
1065 scope = sctp_scope(sa_addr);
1066 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1067 if (!asoc) {
1068 err = -ENOMEM;
1069 goto out_free;
1073 /* Prime the peer's transport structures. */
1074 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL,
1075 SCTP_UNKNOWN);
1076 if (!transport) {
1077 err = -ENOMEM;
1078 goto out_free;
1081 addrcnt++;
1082 addr_buf += af->sockaddr_len;
1083 walk_size += af->sockaddr_len;
1086 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1087 if (err < 0) {
1088 goto out_free;
1091 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1092 if (err < 0) {
1093 goto out_free;
1096 /* Initialize sk's dport and daddr for getpeername() */
1097 inet_sk(sk)->dport = htons(asoc->peer.port);
1098 af = sctp_get_af_specific(to.sa.sa_family);
1099 af->to_sk_daddr(&to, sk);
1100 sk->sk_err = 0;
1102 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1103 err = sctp_wait_for_connect(asoc, &timeo);
1105 /* Don't free association on exit. */
1106 asoc = NULL;
1108 out_free:
1110 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1111 " kaddrs: %p err: %d\n",
1112 asoc, kaddrs, err);
1113 if (asoc)
1114 sctp_association_free(asoc);
1115 return err;
1118 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1120 * API 8.9
1121 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1123 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1124 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1125 * or IPv6 addresses.
1127 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1128 * Section 3.1.2 for this usage.
1130 * addrs is a pointer to an array of one or more socket addresses. Each
1131 * address is contained in its appropriate structure (i.e. struct
1132 * sockaddr_in or struct sockaddr_in6) the family of the address type
1133 * must be used to distengish the address length (note that this
1134 * representation is termed a "packed array" of addresses). The caller
1135 * specifies the number of addresses in the array with addrcnt.
1137 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1138 * -1, and sets errno to the appropriate error code.
1140 * For SCTP, the port given in each socket address must be the same, or
1141 * sctp_connectx() will fail, setting errno to EINVAL.
1143 * An application can use sctp_connectx to initiate an association with
1144 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1145 * allows a caller to specify multiple addresses at which a peer can be
1146 * reached. The way the SCTP stack uses the list of addresses to set up
1147 * the association is implementation dependant. This function only
1148 * specifies that the stack will try to make use of all the addresses in
1149 * the list when needed.
1151 * Note that the list of addresses passed in is only used for setting up
1152 * the association. It does not necessarily equal the set of addresses
1153 * the peer uses for the resulting association. If the caller wants to
1154 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1155 * retrieve them after the association has been set up.
1157 * Basically do nothing but copying the addresses from user to kernel
1158 * land and invoking either sctp_connectx(). This is used for tunneling
1159 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1161 * We don't use copy_from_user() for optimization: we first do the
1162 * sanity checks (buffer size -fast- and access check-healthy
1163 * pointer); if all of those succeed, then we can alloc the memory
1164 * (expensive operation) needed to copy the data to kernel. Then we do
1165 * the copying without checking the user space area
1166 * (__copy_from_user()).
1168 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1169 * it.
1171 * sk The sk of the socket
1172 * addrs The pointer to the addresses in user land
1173 * addrssize Size of the addrs buffer
1175 * Returns 0 if ok, <0 errno code on error.
1177 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1178 struct sockaddr __user *addrs,
1179 int addrs_size)
1181 int err = 0;
1182 struct sockaddr *kaddrs;
1184 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1185 __FUNCTION__, sk, addrs, addrs_size);
1187 if (unlikely(addrs_size <= 0))
1188 return -EINVAL;
1190 /* Check the user passed a healthy pointer. */
1191 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1192 return -EFAULT;
1194 /* Alloc space for the address array in kernel memory. */
1195 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1196 if (unlikely(!kaddrs))
1197 return -ENOMEM;
1199 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1200 err = -EFAULT;
1201 } else {
1202 err = __sctp_connect(sk, kaddrs, addrs_size);
1205 kfree(kaddrs);
1206 return err;
1209 /* API 3.1.4 close() - UDP Style Syntax
1210 * Applications use close() to perform graceful shutdown (as described in
1211 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1212 * by a UDP-style socket.
1214 * The syntax is
1216 * ret = close(int sd);
1218 * sd - the socket descriptor of the associations to be closed.
1220 * To gracefully shutdown a specific association represented by the
1221 * UDP-style socket, an application should use the sendmsg() call,
1222 * passing no user data, but including the appropriate flag in the
1223 * ancillary data (see Section xxxx).
1225 * If sd in the close() call is a branched-off socket representing only
1226 * one association, the shutdown is performed on that association only.
1228 * 4.1.6 close() - TCP Style Syntax
1230 * Applications use close() to gracefully close down an association.
1232 * The syntax is:
1234 * int close(int sd);
1236 * sd - the socket descriptor of the association to be closed.
1238 * After an application calls close() on a socket descriptor, no further
1239 * socket operations will succeed on that descriptor.
1241 * API 7.1.4 SO_LINGER
1243 * An application using the TCP-style socket can use this option to
1244 * perform the SCTP ABORT primitive. The linger option structure is:
1246 * struct linger {
1247 * int l_onoff; // option on/off
1248 * int l_linger; // linger time
1249 * };
1251 * To enable the option, set l_onoff to 1. If the l_linger value is set
1252 * to 0, calling close() is the same as the ABORT primitive. If the
1253 * value is set to a negative value, the setsockopt() call will return
1254 * an error. If the value is set to a positive value linger_time, the
1255 * close() can be blocked for at most linger_time ms. If the graceful
1256 * shutdown phase does not finish during this period, close() will
1257 * return but the graceful shutdown phase continues in the system.
1259 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1261 struct sctp_endpoint *ep;
1262 struct sctp_association *asoc;
1263 struct list_head *pos, *temp;
1265 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1267 sctp_lock_sock(sk);
1268 sk->sk_shutdown = SHUTDOWN_MASK;
1270 ep = sctp_sk(sk)->ep;
1272 /* Walk all associations on an endpoint. */
1273 list_for_each_safe(pos, temp, &ep->asocs) {
1274 asoc = list_entry(pos, struct sctp_association, asocs);
1276 if (sctp_style(sk, TCP)) {
1277 /* A closed association can still be in the list if
1278 * it belongs to a TCP-style listening socket that is
1279 * not yet accepted. If so, free it. If not, send an
1280 * ABORT or SHUTDOWN based on the linger options.
1282 if (sctp_state(asoc, CLOSED)) {
1283 sctp_unhash_established(asoc);
1284 sctp_association_free(asoc);
1285 continue;
1289 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1290 struct sctp_chunk *chunk;
1292 chunk = sctp_make_abort_user(asoc, NULL, 0);
1293 if (chunk)
1294 sctp_primitive_ABORT(asoc, chunk);
1295 } else
1296 sctp_primitive_SHUTDOWN(asoc, NULL);
1299 /* Clean up any skbs sitting on the receive queue. */
1300 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1301 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1303 /* On a TCP-style socket, block for at most linger_time if set. */
1304 if (sctp_style(sk, TCP) && timeout)
1305 sctp_wait_for_close(sk, timeout);
1307 /* This will run the backlog queue. */
1308 sctp_release_sock(sk);
1310 /* Supposedly, no process has access to the socket, but
1311 * the net layers still may.
1313 sctp_local_bh_disable();
1314 sctp_bh_lock_sock(sk);
1316 /* Hold the sock, since sk_common_release() will put sock_put()
1317 * and we have just a little more cleanup.
1319 sock_hold(sk);
1320 sk_common_release(sk);
1322 sctp_bh_unlock_sock(sk);
1323 sctp_local_bh_enable();
1325 sock_put(sk);
1327 SCTP_DBG_OBJCNT_DEC(sock);
1330 /* Handle EPIPE error. */
1331 static int sctp_error(struct sock *sk, int flags, int err)
1333 if (err == -EPIPE)
1334 err = sock_error(sk) ? : -EPIPE;
1335 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1336 send_sig(SIGPIPE, current, 0);
1337 return err;
1340 /* API 3.1.3 sendmsg() - UDP Style Syntax
1342 * An application uses sendmsg() and recvmsg() calls to transmit data to
1343 * and receive data from its peer.
1345 * ssize_t sendmsg(int socket, const struct msghdr *message,
1346 * int flags);
1348 * socket - the socket descriptor of the endpoint.
1349 * message - pointer to the msghdr structure which contains a single
1350 * user message and possibly some ancillary data.
1352 * See Section 5 for complete description of the data
1353 * structures.
1355 * flags - flags sent or received with the user message, see Section
1356 * 5 for complete description of the flags.
1358 * Note: This function could use a rewrite especially when explicit
1359 * connect support comes in.
1361 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1363 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1365 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1366 struct msghdr *msg, size_t msg_len)
1368 struct sctp_sock *sp;
1369 struct sctp_endpoint *ep;
1370 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1371 struct sctp_transport *transport, *chunk_tp;
1372 struct sctp_chunk *chunk;
1373 union sctp_addr to;
1374 struct sockaddr *msg_name = NULL;
1375 struct sctp_sndrcvinfo default_sinfo = { 0 };
1376 struct sctp_sndrcvinfo *sinfo;
1377 struct sctp_initmsg *sinit;
1378 sctp_assoc_t associd = 0;
1379 sctp_cmsgs_t cmsgs = { NULL };
1380 int err;
1381 sctp_scope_t scope;
1382 long timeo;
1383 __u16 sinfo_flags = 0;
1384 struct sctp_datamsg *datamsg;
1385 struct list_head *pos;
1386 int msg_flags = msg->msg_flags;
1388 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1389 sk, msg, msg_len);
1391 err = 0;
1392 sp = sctp_sk(sk);
1393 ep = sp->ep;
1395 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1397 /* We cannot send a message over a TCP-style listening socket. */
1398 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1399 err = -EPIPE;
1400 goto out_nounlock;
1403 /* Parse out the SCTP CMSGs. */
1404 err = sctp_msghdr_parse(msg, &cmsgs);
1406 if (err) {
1407 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1408 goto out_nounlock;
1411 /* Fetch the destination address for this packet. This
1412 * address only selects the association--it is not necessarily
1413 * the address we will send to.
1414 * For a peeled-off socket, msg_name is ignored.
1416 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1417 int msg_namelen = msg->msg_namelen;
1419 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1420 msg_namelen);
1421 if (err)
1422 return err;
1424 if (msg_namelen > sizeof(to))
1425 msg_namelen = sizeof(to);
1426 memcpy(&to, msg->msg_name, msg_namelen);
1427 msg_name = msg->msg_name;
1430 sinfo = cmsgs.info;
1431 sinit = cmsgs.init;
1433 /* Did the user specify SNDRCVINFO? */
1434 if (sinfo) {
1435 sinfo_flags = sinfo->sinfo_flags;
1436 associd = sinfo->sinfo_assoc_id;
1439 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1440 msg_len, sinfo_flags);
1442 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1443 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1444 err = -EINVAL;
1445 goto out_nounlock;
1448 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1449 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1450 * If SCTP_ABORT is set, the message length could be non zero with
1451 * the msg_iov set to the user abort reason.
1453 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1454 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1455 err = -EINVAL;
1456 goto out_nounlock;
1459 /* If SCTP_ADDR_OVER is set, there must be an address
1460 * specified in msg_name.
1462 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1463 err = -EINVAL;
1464 goto out_nounlock;
1467 transport = NULL;
1469 SCTP_DEBUG_PRINTK("About to look up association.\n");
1471 sctp_lock_sock(sk);
1473 /* If a msg_name has been specified, assume this is to be used. */
1474 if (msg_name) {
1475 /* Look for a matching association on the endpoint. */
1476 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1477 if (!asoc) {
1478 /* If we could not find a matching association on the
1479 * endpoint, make sure that it is not a TCP-style
1480 * socket that already has an association or there is
1481 * no peeled-off association on another socket.
1483 if ((sctp_style(sk, TCP) &&
1484 sctp_sstate(sk, ESTABLISHED)) ||
1485 sctp_endpoint_is_peeled_off(ep, &to)) {
1486 err = -EADDRNOTAVAIL;
1487 goto out_unlock;
1490 } else {
1491 asoc = sctp_id2assoc(sk, associd);
1492 if (!asoc) {
1493 err = -EPIPE;
1494 goto out_unlock;
1498 if (asoc) {
1499 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1501 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1502 * socket that has an association in CLOSED state. This can
1503 * happen when an accepted socket has an association that is
1504 * already CLOSED.
1506 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1507 err = -EPIPE;
1508 goto out_unlock;
1511 if (sinfo_flags & SCTP_EOF) {
1512 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1513 asoc);
1514 sctp_primitive_SHUTDOWN(asoc, NULL);
1515 err = 0;
1516 goto out_unlock;
1518 if (sinfo_flags & SCTP_ABORT) {
1519 struct sctp_chunk *chunk;
1521 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1522 if (!chunk) {
1523 err = -ENOMEM;
1524 goto out_unlock;
1527 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1528 sctp_primitive_ABORT(asoc, chunk);
1529 err = 0;
1530 goto out_unlock;
1534 /* Do we need to create the association? */
1535 if (!asoc) {
1536 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1538 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1539 err = -EINVAL;
1540 goto out_unlock;
1543 /* Check for invalid stream against the stream counts,
1544 * either the default or the user specified stream counts.
1546 if (sinfo) {
1547 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1548 /* Check against the defaults. */
1549 if (sinfo->sinfo_stream >=
1550 sp->initmsg.sinit_num_ostreams) {
1551 err = -EINVAL;
1552 goto out_unlock;
1554 } else {
1555 /* Check against the requested. */
1556 if (sinfo->sinfo_stream >=
1557 sinit->sinit_num_ostreams) {
1558 err = -EINVAL;
1559 goto out_unlock;
1565 * API 3.1.2 bind() - UDP Style Syntax
1566 * If a bind() or sctp_bindx() is not called prior to a
1567 * sendmsg() call that initiates a new association, the
1568 * system picks an ephemeral port and will choose an address
1569 * set equivalent to binding with a wildcard address.
1571 if (!ep->base.bind_addr.port) {
1572 if (sctp_autobind(sk)) {
1573 err = -EAGAIN;
1574 goto out_unlock;
1576 } else {
1578 * If an unprivileged user inherits a one-to-many
1579 * style socket with open associations on a privileged
1580 * port, it MAY be permitted to accept new associations,
1581 * but it SHOULD NOT be permitted to open new
1582 * associations.
1584 if (ep->base.bind_addr.port < PROT_SOCK &&
1585 !capable(CAP_NET_BIND_SERVICE)) {
1586 err = -EACCES;
1587 goto out_unlock;
1591 scope = sctp_scope(&to);
1592 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1593 if (!new_asoc) {
1594 err = -ENOMEM;
1595 goto out_unlock;
1597 asoc = new_asoc;
1599 /* If the SCTP_INIT ancillary data is specified, set all
1600 * the association init values accordingly.
1602 if (sinit) {
1603 if (sinit->sinit_num_ostreams) {
1604 asoc->c.sinit_num_ostreams =
1605 sinit->sinit_num_ostreams;
1607 if (sinit->sinit_max_instreams) {
1608 asoc->c.sinit_max_instreams =
1609 sinit->sinit_max_instreams;
1611 if (sinit->sinit_max_attempts) {
1612 asoc->max_init_attempts
1613 = sinit->sinit_max_attempts;
1615 if (sinit->sinit_max_init_timeo) {
1616 asoc->max_init_timeo =
1617 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1621 /* Prime the peer's transport structures. */
1622 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1623 if (!transport) {
1624 err = -ENOMEM;
1625 goto out_free;
1627 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1628 if (err < 0) {
1629 err = -ENOMEM;
1630 goto out_free;
1634 /* ASSERT: we have a valid association at this point. */
1635 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1637 if (!sinfo) {
1638 /* If the user didn't specify SNDRCVINFO, make up one with
1639 * some defaults.
1641 default_sinfo.sinfo_stream = asoc->default_stream;
1642 default_sinfo.sinfo_flags = asoc->default_flags;
1643 default_sinfo.sinfo_ppid = asoc->default_ppid;
1644 default_sinfo.sinfo_context = asoc->default_context;
1645 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1646 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1647 sinfo = &default_sinfo;
1650 /* API 7.1.7, the sndbuf size per association bounds the
1651 * maximum size of data that can be sent in a single send call.
1653 if (msg_len > sk->sk_sndbuf) {
1654 err = -EMSGSIZE;
1655 goto out_free;
1658 /* If fragmentation is disabled and the message length exceeds the
1659 * association fragmentation point, return EMSGSIZE. The I-D
1660 * does not specify what this error is, but this looks like
1661 * a great fit.
1663 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1664 err = -EMSGSIZE;
1665 goto out_free;
1668 if (sinfo) {
1669 /* Check for invalid stream. */
1670 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1671 err = -EINVAL;
1672 goto out_free;
1676 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1677 if (!sctp_wspace(asoc)) {
1678 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1679 if (err)
1680 goto out_free;
1683 /* If an address is passed with the sendto/sendmsg call, it is used
1684 * to override the primary destination address in the TCP model, or
1685 * when SCTP_ADDR_OVER flag is set in the UDP model.
1687 if ((sctp_style(sk, TCP) && msg_name) ||
1688 (sinfo_flags & SCTP_ADDR_OVER)) {
1689 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1690 if (!chunk_tp) {
1691 err = -EINVAL;
1692 goto out_free;
1694 } else
1695 chunk_tp = NULL;
1697 /* Auto-connect, if we aren't connected already. */
1698 if (sctp_state(asoc, CLOSED)) {
1699 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1700 if (err < 0)
1701 goto out_free;
1702 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1705 /* Break the message into multiple chunks of maximum size. */
1706 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1707 if (!datamsg) {
1708 err = -ENOMEM;
1709 goto out_free;
1712 /* Now send the (possibly) fragmented message. */
1713 list_for_each(pos, &datamsg->chunks) {
1714 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1715 sctp_datamsg_track(chunk);
1717 /* Do accounting for the write space. */
1718 sctp_set_owner_w(chunk);
1720 chunk->transport = chunk_tp;
1722 /* Send it to the lower layers. Note: all chunks
1723 * must either fail or succeed. The lower layer
1724 * works that way today. Keep it that way or this
1725 * breaks.
1727 err = sctp_primitive_SEND(asoc, chunk);
1728 /* Did the lower layer accept the chunk? */
1729 if (err)
1730 sctp_chunk_free(chunk);
1731 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1734 sctp_datamsg_free(datamsg);
1735 if (err)
1736 goto out_free;
1737 else
1738 err = msg_len;
1740 /* If we are already past ASSOCIATE, the lower
1741 * layers are responsible for association cleanup.
1743 goto out_unlock;
1745 out_free:
1746 if (new_asoc)
1747 sctp_association_free(asoc);
1748 out_unlock:
1749 sctp_release_sock(sk);
1751 out_nounlock:
1752 return sctp_error(sk, msg_flags, err);
1754 #if 0
1755 do_sock_err:
1756 if (msg_len)
1757 err = msg_len;
1758 else
1759 err = sock_error(sk);
1760 goto out;
1762 do_interrupted:
1763 if (msg_len)
1764 err = msg_len;
1765 goto out;
1766 #endif /* 0 */
1769 /* This is an extended version of skb_pull() that removes the data from the
1770 * start of a skb even when data is spread across the list of skb's in the
1771 * frag_list. len specifies the total amount of data that needs to be removed.
1772 * when 'len' bytes could be removed from the skb, it returns 0.
1773 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1774 * could not be removed.
1776 static int sctp_skb_pull(struct sk_buff *skb, int len)
1778 struct sk_buff *list;
1779 int skb_len = skb_headlen(skb);
1780 int rlen;
1782 if (len <= skb_len) {
1783 __skb_pull(skb, len);
1784 return 0;
1786 len -= skb_len;
1787 __skb_pull(skb, skb_len);
1789 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1790 rlen = sctp_skb_pull(list, len);
1791 skb->len -= (len-rlen);
1792 skb->data_len -= (len-rlen);
1794 if (!rlen)
1795 return 0;
1797 len = rlen;
1800 return len;
1803 /* API 3.1.3 recvmsg() - UDP Style Syntax
1805 * ssize_t recvmsg(int socket, struct msghdr *message,
1806 * int flags);
1808 * socket - the socket descriptor of the endpoint.
1809 * message - pointer to the msghdr structure which contains a single
1810 * user message and possibly some ancillary data.
1812 * See Section 5 for complete description of the data
1813 * structures.
1815 * flags - flags sent or received with the user message, see Section
1816 * 5 for complete description of the flags.
1818 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1820 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1821 struct msghdr *msg, size_t len, int noblock,
1822 int flags, int *addr_len)
1824 struct sctp_ulpevent *event = NULL;
1825 struct sctp_sock *sp = sctp_sk(sk);
1826 struct sk_buff *skb;
1827 int copied;
1828 int err = 0;
1829 int skb_len;
1831 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1832 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1833 "len", len, "knoblauch", noblock,
1834 "flags", flags, "addr_len", addr_len);
1836 sctp_lock_sock(sk);
1838 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1839 err = -ENOTCONN;
1840 goto out;
1843 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1844 if (!skb)
1845 goto out;
1847 /* Get the total length of the skb including any skb's in the
1848 * frag_list.
1850 skb_len = skb->len;
1852 copied = skb_len;
1853 if (copied > len)
1854 copied = len;
1856 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1858 event = sctp_skb2event(skb);
1860 if (err)
1861 goto out_free;
1863 sock_recv_timestamp(msg, sk, skb);
1864 if (sctp_ulpevent_is_notification(event)) {
1865 msg->msg_flags |= MSG_NOTIFICATION;
1866 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1867 } else {
1868 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1871 /* Check if we allow SCTP_SNDRCVINFO. */
1872 if (sp->subscribe.sctp_data_io_event)
1873 sctp_ulpevent_read_sndrcvinfo(event, msg);
1874 #if 0
1875 /* FIXME: we should be calling IP/IPv6 layers. */
1876 if (sk->sk_protinfo.af_inet.cmsg_flags)
1877 ip_cmsg_recv(msg, skb);
1878 #endif
1880 err = copied;
1882 /* If skb's length exceeds the user's buffer, update the skb and
1883 * push it back to the receive_queue so that the next call to
1884 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1886 if (skb_len > copied) {
1887 msg->msg_flags &= ~MSG_EOR;
1888 if (flags & MSG_PEEK)
1889 goto out_free;
1890 sctp_skb_pull(skb, copied);
1891 skb_queue_head(&sk->sk_receive_queue, skb);
1893 /* When only partial message is copied to the user, increase
1894 * rwnd by that amount. If all the data in the skb is read,
1895 * rwnd is updated when the event is freed.
1897 sctp_assoc_rwnd_increase(event->asoc, copied);
1898 goto out;
1899 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1900 (event->msg_flags & MSG_EOR))
1901 msg->msg_flags |= MSG_EOR;
1902 else
1903 msg->msg_flags &= ~MSG_EOR;
1905 out_free:
1906 if (flags & MSG_PEEK) {
1907 /* Release the skb reference acquired after peeking the skb in
1908 * sctp_skb_recv_datagram().
1910 kfree_skb(skb);
1911 } else {
1912 /* Free the event which includes releasing the reference to
1913 * the owner of the skb, freeing the skb and updating the
1914 * rwnd.
1916 sctp_ulpevent_free(event);
1918 out:
1919 sctp_release_sock(sk);
1920 return err;
1923 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1925 * This option is a on/off flag. If enabled no SCTP message
1926 * fragmentation will be performed. Instead if a message being sent
1927 * exceeds the current PMTU size, the message will NOT be sent and
1928 * instead a error will be indicated to the user.
1930 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1931 char __user *optval, int optlen)
1933 int val;
1935 if (optlen < sizeof(int))
1936 return -EINVAL;
1938 if (get_user(val, (int __user *)optval))
1939 return -EFAULT;
1941 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1943 return 0;
1946 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1947 int optlen)
1949 if (optlen != sizeof(struct sctp_event_subscribe))
1950 return -EINVAL;
1951 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1952 return -EFAULT;
1953 return 0;
1956 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1958 * This socket option is applicable to the UDP-style socket only. When
1959 * set it will cause associations that are idle for more than the
1960 * specified number of seconds to automatically close. An association
1961 * being idle is defined an association that has NOT sent or received
1962 * user data. The special value of '0' indicates that no automatic
1963 * close of any associations should be performed. The option expects an
1964 * integer defining the number of seconds of idle time before an
1965 * association is closed.
1967 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1968 int optlen)
1970 struct sctp_sock *sp = sctp_sk(sk);
1972 /* Applicable to UDP-style socket only */
1973 if (sctp_style(sk, TCP))
1974 return -EOPNOTSUPP;
1975 if (optlen != sizeof(int))
1976 return -EINVAL;
1977 if (copy_from_user(&sp->autoclose, optval, optlen))
1978 return -EFAULT;
1980 return 0;
1983 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1985 * Applications can enable or disable heartbeats for any peer address of
1986 * an association, modify an address's heartbeat interval, force a
1987 * heartbeat to be sent immediately, and adjust the address's maximum
1988 * number of retransmissions sent before an address is considered
1989 * unreachable. The following structure is used to access and modify an
1990 * address's parameters:
1992 * struct sctp_paddrparams {
1993 * sctp_assoc_t spp_assoc_id;
1994 * struct sockaddr_storage spp_address;
1995 * uint32_t spp_hbinterval;
1996 * uint16_t spp_pathmaxrxt;
1997 * uint32_t spp_pathmtu;
1998 * uint32_t spp_sackdelay;
1999 * uint32_t spp_flags;
2000 * };
2002 * spp_assoc_id - (one-to-many style socket) This is filled in the
2003 * application, and identifies the association for
2004 * this query.
2005 * spp_address - This specifies which address is of interest.
2006 * spp_hbinterval - This contains the value of the heartbeat interval,
2007 * in milliseconds. If a value of zero
2008 * is present in this field then no changes are to
2009 * be made to this parameter.
2010 * spp_pathmaxrxt - This contains the maximum number of
2011 * retransmissions before this address shall be
2012 * considered unreachable. If a value of zero
2013 * is present in this field then no changes are to
2014 * be made to this parameter.
2015 * spp_pathmtu - When Path MTU discovery is disabled the value
2016 * specified here will be the "fixed" path mtu.
2017 * Note that if the spp_address field is empty
2018 * then all associations on this address will
2019 * have this fixed path mtu set upon them.
2021 * spp_sackdelay - When delayed sack is enabled, this value specifies
2022 * the number of milliseconds that sacks will be delayed
2023 * for. This value will apply to all addresses of an
2024 * association if the spp_address field is empty. Note
2025 * also, that if delayed sack is enabled and this
2026 * value is set to 0, no change is made to the last
2027 * recorded delayed sack timer value.
2029 * spp_flags - These flags are used to control various features
2030 * on an association. The flag field may contain
2031 * zero or more of the following options.
2033 * SPP_HB_ENABLE - Enable heartbeats on the
2034 * specified address. Note that if the address
2035 * field is empty all addresses for the association
2036 * have heartbeats enabled upon them.
2038 * SPP_HB_DISABLE - Disable heartbeats on the
2039 * speicifed address. Note that if the address
2040 * field is empty all addresses for the association
2041 * will have their heartbeats disabled. Note also
2042 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2043 * mutually exclusive, only one of these two should
2044 * be specified. Enabling both fields will have
2045 * undetermined results.
2047 * SPP_HB_DEMAND - Request a user initiated heartbeat
2048 * to be made immediately.
2050 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2051 * heartbeat delayis to be set to the value of 0
2052 * milliseconds.
2054 * SPP_PMTUD_ENABLE - This field will enable PMTU
2055 * discovery upon the specified address. Note that
2056 * if the address feild is empty then all addresses
2057 * on the association are effected.
2059 * SPP_PMTUD_DISABLE - This field will disable PMTU
2060 * discovery upon the specified address. Note that
2061 * if the address feild is empty then all addresses
2062 * on the association are effected. Not also that
2063 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2064 * exclusive. Enabling both will have undetermined
2065 * results.
2067 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2068 * on delayed sack. The time specified in spp_sackdelay
2069 * is used to specify the sack delay for this address. Note
2070 * that if spp_address is empty then all addresses will
2071 * enable delayed sack and take on the sack delay
2072 * value specified in spp_sackdelay.
2073 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2074 * off delayed sack. If the spp_address field is blank then
2075 * delayed sack is disabled for the entire association. Note
2076 * also that this field is mutually exclusive to
2077 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2078 * results.
2080 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2081 struct sctp_transport *trans,
2082 struct sctp_association *asoc,
2083 struct sctp_sock *sp,
2084 int hb_change,
2085 int pmtud_change,
2086 int sackdelay_change)
2088 int error;
2090 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2091 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2092 if (error)
2093 return error;
2096 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2097 * this field is ignored. Note also that a value of zero indicates
2098 * the current setting should be left unchanged.
2100 if (params->spp_flags & SPP_HB_ENABLE) {
2102 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2103 * set. This lets us use 0 value when this flag
2104 * is set.
2106 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2107 params->spp_hbinterval = 0;
2109 if (params->spp_hbinterval ||
2110 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2111 if (trans) {
2112 trans->hbinterval =
2113 msecs_to_jiffies(params->spp_hbinterval);
2114 } else if (asoc) {
2115 asoc->hbinterval =
2116 msecs_to_jiffies(params->spp_hbinterval);
2117 } else {
2118 sp->hbinterval = params->spp_hbinterval;
2123 if (hb_change) {
2124 if (trans) {
2125 trans->param_flags =
2126 (trans->param_flags & ~SPP_HB) | hb_change;
2127 } else if (asoc) {
2128 asoc->param_flags =
2129 (asoc->param_flags & ~SPP_HB) | hb_change;
2130 } else {
2131 sp->param_flags =
2132 (sp->param_flags & ~SPP_HB) | hb_change;
2136 /* When Path MTU discovery is disabled the value specified here will
2137 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2138 * include the flag SPP_PMTUD_DISABLE for this field to have any
2139 * effect).
2141 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2142 if (trans) {
2143 trans->pathmtu = params->spp_pathmtu;
2144 sctp_assoc_sync_pmtu(asoc);
2145 } else if (asoc) {
2146 asoc->pathmtu = params->spp_pathmtu;
2147 sctp_frag_point(sp, params->spp_pathmtu);
2148 } else {
2149 sp->pathmtu = params->spp_pathmtu;
2153 if (pmtud_change) {
2154 if (trans) {
2155 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2156 (params->spp_flags & SPP_PMTUD_ENABLE);
2157 trans->param_flags =
2158 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2159 if (update) {
2160 sctp_transport_pmtu(trans);
2161 sctp_assoc_sync_pmtu(asoc);
2163 } else if (asoc) {
2164 asoc->param_flags =
2165 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2166 } else {
2167 sp->param_flags =
2168 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2172 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2173 * value of this field is ignored. Note also that a value of zero
2174 * indicates the current setting should be left unchanged.
2176 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2177 if (trans) {
2178 trans->sackdelay =
2179 msecs_to_jiffies(params->spp_sackdelay);
2180 } else if (asoc) {
2181 asoc->sackdelay =
2182 msecs_to_jiffies(params->spp_sackdelay);
2183 } else {
2184 sp->sackdelay = params->spp_sackdelay;
2188 if (sackdelay_change) {
2189 if (trans) {
2190 trans->param_flags =
2191 (trans->param_flags & ~SPP_SACKDELAY) |
2192 sackdelay_change;
2193 } else if (asoc) {
2194 asoc->param_flags =
2195 (asoc->param_flags & ~SPP_SACKDELAY) |
2196 sackdelay_change;
2197 } else {
2198 sp->param_flags =
2199 (sp->param_flags & ~SPP_SACKDELAY) |
2200 sackdelay_change;
2204 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2205 * of this field is ignored. Note also that a value of zero
2206 * indicates the current setting should be left unchanged.
2208 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
2209 if (trans) {
2210 trans->pathmaxrxt = params->spp_pathmaxrxt;
2211 } else if (asoc) {
2212 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2213 } else {
2214 sp->pathmaxrxt = params->spp_pathmaxrxt;
2218 return 0;
2221 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2222 char __user *optval, int optlen)
2224 struct sctp_paddrparams params;
2225 struct sctp_transport *trans = NULL;
2226 struct sctp_association *asoc = NULL;
2227 struct sctp_sock *sp = sctp_sk(sk);
2228 int error;
2229 int hb_change, pmtud_change, sackdelay_change;
2231 if (optlen != sizeof(struct sctp_paddrparams))
2232 return - EINVAL;
2234 if (copy_from_user(&params, optval, optlen))
2235 return -EFAULT;
2237 /* Validate flags and value parameters. */
2238 hb_change = params.spp_flags & SPP_HB;
2239 pmtud_change = params.spp_flags & SPP_PMTUD;
2240 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2242 if (hb_change == SPP_HB ||
2243 pmtud_change == SPP_PMTUD ||
2244 sackdelay_change == SPP_SACKDELAY ||
2245 params.spp_sackdelay > 500 ||
2246 (params.spp_pathmtu
2247 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2248 return -EINVAL;
2250 /* If an address other than INADDR_ANY is specified, and
2251 * no transport is found, then the request is invalid.
2253 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2254 trans = sctp_addr_id2transport(sk, &params.spp_address,
2255 params.spp_assoc_id);
2256 if (!trans)
2257 return -EINVAL;
2260 /* Get association, if assoc_id != 0 and the socket is a one
2261 * to many style socket, and an association was not found, then
2262 * the id was invalid.
2264 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2265 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2266 return -EINVAL;
2268 /* Heartbeat demand can only be sent on a transport or
2269 * association, but not a socket.
2271 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2272 return -EINVAL;
2274 /* Process parameters. */
2275 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2276 hb_change, pmtud_change,
2277 sackdelay_change);
2279 if (error)
2280 return error;
2282 /* If changes are for association, also apply parameters to each
2283 * transport.
2285 if (!trans && asoc) {
2286 struct list_head *pos;
2288 list_for_each(pos, &asoc->peer.transport_addr_list) {
2289 trans = list_entry(pos, struct sctp_transport,
2290 transports);
2291 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2292 hb_change, pmtud_change,
2293 sackdelay_change);
2297 return 0;
2300 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2302 * This options will get or set the delayed ack timer. The time is set
2303 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2304 * endpoints default delayed ack timer value. If the assoc_id field is
2305 * non-zero, then the set or get effects the specified association.
2307 * struct sctp_assoc_value {
2308 * sctp_assoc_t assoc_id;
2309 * uint32_t assoc_value;
2310 * };
2312 * assoc_id - This parameter, indicates which association the
2313 * user is preforming an action upon. Note that if
2314 * this field's value is zero then the endpoints
2315 * default value is changed (effecting future
2316 * associations only).
2318 * assoc_value - This parameter contains the number of milliseconds
2319 * that the user is requesting the delayed ACK timer
2320 * be set to. Note that this value is defined in
2321 * the standard to be between 200 and 500 milliseconds.
2323 * Note: a value of zero will leave the value alone,
2324 * but disable SACK delay. A non-zero value will also
2325 * enable SACK delay.
2328 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2329 char __user *optval, int optlen)
2331 struct sctp_assoc_value params;
2332 struct sctp_transport *trans = NULL;
2333 struct sctp_association *asoc = NULL;
2334 struct sctp_sock *sp = sctp_sk(sk);
2336 if (optlen != sizeof(struct sctp_assoc_value))
2337 return - EINVAL;
2339 if (copy_from_user(&params, optval, optlen))
2340 return -EFAULT;
2342 /* Validate value parameter. */
2343 if (params.assoc_value > 500)
2344 return -EINVAL;
2346 /* Get association, if assoc_id != 0 and the socket is a one
2347 * to many style socket, and an association was not found, then
2348 * the id was invalid.
2350 asoc = sctp_id2assoc(sk, params.assoc_id);
2351 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2352 return -EINVAL;
2354 if (params.assoc_value) {
2355 if (asoc) {
2356 asoc->sackdelay =
2357 msecs_to_jiffies(params.assoc_value);
2358 asoc->param_flags =
2359 (asoc->param_flags & ~SPP_SACKDELAY) |
2360 SPP_SACKDELAY_ENABLE;
2361 } else {
2362 sp->sackdelay = params.assoc_value;
2363 sp->param_flags =
2364 (sp->param_flags & ~SPP_SACKDELAY) |
2365 SPP_SACKDELAY_ENABLE;
2367 } else {
2368 if (asoc) {
2369 asoc->param_flags =
2370 (asoc->param_flags & ~SPP_SACKDELAY) |
2371 SPP_SACKDELAY_DISABLE;
2372 } else {
2373 sp->param_flags =
2374 (sp->param_flags & ~SPP_SACKDELAY) |
2375 SPP_SACKDELAY_DISABLE;
2379 /* If change is for association, also apply to each transport. */
2380 if (asoc) {
2381 struct list_head *pos;
2383 list_for_each(pos, &asoc->peer.transport_addr_list) {
2384 trans = list_entry(pos, struct sctp_transport,
2385 transports);
2386 if (params.assoc_value) {
2387 trans->sackdelay =
2388 msecs_to_jiffies(params.assoc_value);
2389 trans->param_flags =
2390 (trans->param_flags & ~SPP_SACKDELAY) |
2391 SPP_SACKDELAY_ENABLE;
2392 } else {
2393 trans->param_flags =
2394 (trans->param_flags & ~SPP_SACKDELAY) |
2395 SPP_SACKDELAY_DISABLE;
2400 return 0;
2403 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2405 * Applications can specify protocol parameters for the default association
2406 * initialization. The option name argument to setsockopt() and getsockopt()
2407 * is SCTP_INITMSG.
2409 * Setting initialization parameters is effective only on an unconnected
2410 * socket (for UDP-style sockets only future associations are effected
2411 * by the change). With TCP-style sockets, this option is inherited by
2412 * sockets derived from a listener socket.
2414 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2416 struct sctp_initmsg sinit;
2417 struct sctp_sock *sp = sctp_sk(sk);
2419 if (optlen != sizeof(struct sctp_initmsg))
2420 return -EINVAL;
2421 if (copy_from_user(&sinit, optval, optlen))
2422 return -EFAULT;
2424 if (sinit.sinit_num_ostreams)
2425 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2426 if (sinit.sinit_max_instreams)
2427 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2428 if (sinit.sinit_max_attempts)
2429 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2430 if (sinit.sinit_max_init_timeo)
2431 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2433 return 0;
2437 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2439 * Applications that wish to use the sendto() system call may wish to
2440 * specify a default set of parameters that would normally be supplied
2441 * through the inclusion of ancillary data. This socket option allows
2442 * such an application to set the default sctp_sndrcvinfo structure.
2443 * The application that wishes to use this socket option simply passes
2444 * in to this call the sctp_sndrcvinfo structure defined in Section
2445 * 5.2.2) The input parameters accepted by this call include
2446 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2447 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2448 * to this call if the caller is using the UDP model.
2450 static int sctp_setsockopt_default_send_param(struct sock *sk,
2451 char __user *optval, int optlen)
2453 struct sctp_sndrcvinfo info;
2454 struct sctp_association *asoc;
2455 struct sctp_sock *sp = sctp_sk(sk);
2457 if (optlen != sizeof(struct sctp_sndrcvinfo))
2458 return -EINVAL;
2459 if (copy_from_user(&info, optval, optlen))
2460 return -EFAULT;
2462 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2463 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2464 return -EINVAL;
2466 if (asoc) {
2467 asoc->default_stream = info.sinfo_stream;
2468 asoc->default_flags = info.sinfo_flags;
2469 asoc->default_ppid = info.sinfo_ppid;
2470 asoc->default_context = info.sinfo_context;
2471 asoc->default_timetolive = info.sinfo_timetolive;
2472 } else {
2473 sp->default_stream = info.sinfo_stream;
2474 sp->default_flags = info.sinfo_flags;
2475 sp->default_ppid = info.sinfo_ppid;
2476 sp->default_context = info.sinfo_context;
2477 sp->default_timetolive = info.sinfo_timetolive;
2480 return 0;
2483 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2485 * Requests that the local SCTP stack use the enclosed peer address as
2486 * the association primary. The enclosed address must be one of the
2487 * association peer's addresses.
2489 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2490 int optlen)
2492 struct sctp_prim prim;
2493 struct sctp_transport *trans;
2495 if (optlen != sizeof(struct sctp_prim))
2496 return -EINVAL;
2498 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2499 return -EFAULT;
2501 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2502 if (!trans)
2503 return -EINVAL;
2505 sctp_assoc_set_primary(trans->asoc, trans);
2507 return 0;
2511 * 7.1.5 SCTP_NODELAY
2513 * Turn on/off any Nagle-like algorithm. This means that packets are
2514 * generally sent as soon as possible and no unnecessary delays are
2515 * introduced, at the cost of more packets in the network. Expects an
2516 * integer boolean flag.
2518 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2519 int optlen)
2521 int val;
2523 if (optlen < sizeof(int))
2524 return -EINVAL;
2525 if (get_user(val, (int __user *)optval))
2526 return -EFAULT;
2528 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2529 return 0;
2534 * 7.1.1 SCTP_RTOINFO
2536 * The protocol parameters used to initialize and bound retransmission
2537 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2538 * and modify these parameters.
2539 * All parameters are time values, in milliseconds. A value of 0, when
2540 * modifying the parameters, indicates that the current value should not
2541 * be changed.
2544 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2545 struct sctp_rtoinfo rtoinfo;
2546 struct sctp_association *asoc;
2548 if (optlen != sizeof (struct sctp_rtoinfo))
2549 return -EINVAL;
2551 if (copy_from_user(&rtoinfo, optval, optlen))
2552 return -EFAULT;
2554 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2556 /* Set the values to the specific association */
2557 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2558 return -EINVAL;
2560 if (asoc) {
2561 if (rtoinfo.srto_initial != 0)
2562 asoc->rto_initial =
2563 msecs_to_jiffies(rtoinfo.srto_initial);
2564 if (rtoinfo.srto_max != 0)
2565 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2566 if (rtoinfo.srto_min != 0)
2567 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2568 } else {
2569 /* If there is no association or the association-id = 0
2570 * set the values to the endpoint.
2572 struct sctp_sock *sp = sctp_sk(sk);
2574 if (rtoinfo.srto_initial != 0)
2575 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2576 if (rtoinfo.srto_max != 0)
2577 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2578 if (rtoinfo.srto_min != 0)
2579 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2582 return 0;
2587 * 7.1.2 SCTP_ASSOCINFO
2589 * This option is used to tune the maximum retransmission attempts
2590 * of the association.
2591 * Returns an error if the new association retransmission value is
2592 * greater than the sum of the retransmission value of the peer.
2593 * See [SCTP] for more information.
2596 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2599 struct sctp_assocparams assocparams;
2600 struct sctp_association *asoc;
2602 if (optlen != sizeof(struct sctp_assocparams))
2603 return -EINVAL;
2604 if (copy_from_user(&assocparams, optval, optlen))
2605 return -EFAULT;
2607 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2609 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2610 return -EINVAL;
2612 /* Set the values to the specific association */
2613 if (asoc) {
2614 if (assocparams.sasoc_asocmaxrxt != 0) {
2615 __u32 path_sum = 0;
2616 int paths = 0;
2617 struct list_head *pos;
2618 struct sctp_transport *peer_addr;
2620 list_for_each(pos, &asoc->peer.transport_addr_list) {
2621 peer_addr = list_entry(pos,
2622 struct sctp_transport,
2623 transports);
2624 path_sum += peer_addr->pathmaxrxt;
2625 paths++;
2628 /* Only validate asocmaxrxt if we have more then
2629 * one path/transport. We do this because path
2630 * retransmissions are only counted when we have more
2631 * then one path.
2633 if (paths > 1 &&
2634 assocparams.sasoc_asocmaxrxt > path_sum)
2635 return -EINVAL;
2637 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2640 if (assocparams.sasoc_cookie_life != 0) {
2641 asoc->cookie_life.tv_sec =
2642 assocparams.sasoc_cookie_life / 1000;
2643 asoc->cookie_life.tv_usec =
2644 (assocparams.sasoc_cookie_life % 1000)
2645 * 1000;
2647 } else {
2648 /* Set the values to the endpoint */
2649 struct sctp_sock *sp = sctp_sk(sk);
2651 if (assocparams.sasoc_asocmaxrxt != 0)
2652 sp->assocparams.sasoc_asocmaxrxt =
2653 assocparams.sasoc_asocmaxrxt;
2654 if (assocparams.sasoc_cookie_life != 0)
2655 sp->assocparams.sasoc_cookie_life =
2656 assocparams.sasoc_cookie_life;
2658 return 0;
2662 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2664 * This socket option is a boolean flag which turns on or off mapped V4
2665 * addresses. If this option is turned on and the socket is type
2666 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2667 * If this option is turned off, then no mapping will be done of V4
2668 * addresses and a user will receive both PF_INET6 and PF_INET type
2669 * addresses on the socket.
2671 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2673 int val;
2674 struct sctp_sock *sp = sctp_sk(sk);
2676 if (optlen < sizeof(int))
2677 return -EINVAL;
2678 if (get_user(val, (int __user *)optval))
2679 return -EFAULT;
2680 if (val)
2681 sp->v4mapped = 1;
2682 else
2683 sp->v4mapped = 0;
2685 return 0;
2689 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2691 * This socket option specifies the maximum size to put in any outgoing
2692 * SCTP chunk. If a message is larger than this size it will be
2693 * fragmented by SCTP into the specified size. Note that the underlying
2694 * SCTP implementation may fragment into smaller sized chunks when the
2695 * PMTU of the underlying association is smaller than the value set by
2696 * the user.
2698 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2700 struct sctp_association *asoc;
2701 struct list_head *pos;
2702 struct sctp_sock *sp = sctp_sk(sk);
2703 int val;
2705 if (optlen < sizeof(int))
2706 return -EINVAL;
2707 if (get_user(val, (int __user *)optval))
2708 return -EFAULT;
2709 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2710 return -EINVAL;
2711 sp->user_frag = val;
2713 /* Update the frag_point of the existing associations. */
2714 list_for_each(pos, &(sp->ep->asocs)) {
2715 asoc = list_entry(pos, struct sctp_association, asocs);
2716 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2719 return 0;
2724 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2726 * Requests that the peer mark the enclosed address as the association
2727 * primary. The enclosed address must be one of the association's
2728 * locally bound addresses. The following structure is used to make a
2729 * set primary request:
2731 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2732 int optlen)
2734 struct sctp_sock *sp;
2735 struct sctp_endpoint *ep;
2736 struct sctp_association *asoc = NULL;
2737 struct sctp_setpeerprim prim;
2738 struct sctp_chunk *chunk;
2739 int err;
2741 sp = sctp_sk(sk);
2742 ep = sp->ep;
2744 if (!sctp_addip_enable)
2745 return -EPERM;
2747 if (optlen != sizeof(struct sctp_setpeerprim))
2748 return -EINVAL;
2750 if (copy_from_user(&prim, optval, optlen))
2751 return -EFAULT;
2753 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2754 if (!asoc)
2755 return -EINVAL;
2757 if (!asoc->peer.asconf_capable)
2758 return -EPERM;
2760 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2761 return -EPERM;
2763 if (!sctp_state(asoc, ESTABLISHED))
2764 return -ENOTCONN;
2766 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2767 return -EADDRNOTAVAIL;
2769 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2770 chunk = sctp_make_asconf_set_prim(asoc,
2771 (union sctp_addr *)&prim.sspp_addr);
2772 if (!chunk)
2773 return -ENOMEM;
2775 err = sctp_send_asconf(asoc, chunk);
2777 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2779 return err;
2782 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2783 int optlen)
2785 struct sctp_setadaptation adaptation;
2787 if (optlen != sizeof(struct sctp_setadaptation))
2788 return -EINVAL;
2789 if (copy_from_user(&adaptation, optval, optlen))
2790 return -EFAULT;
2792 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2794 return 0;
2798 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2800 * The context field in the sctp_sndrcvinfo structure is normally only
2801 * used when a failed message is retrieved holding the value that was
2802 * sent down on the actual send call. This option allows the setting of
2803 * a default context on an association basis that will be received on
2804 * reading messages from the peer. This is especially helpful in the
2805 * one-2-many model for an application to keep some reference to an
2806 * internal state machine that is processing messages on the
2807 * association. Note that the setting of this value only effects
2808 * received messages from the peer and does not effect the value that is
2809 * saved with outbound messages.
2811 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2812 int optlen)
2814 struct sctp_assoc_value params;
2815 struct sctp_sock *sp;
2816 struct sctp_association *asoc;
2818 if (optlen != sizeof(struct sctp_assoc_value))
2819 return -EINVAL;
2820 if (copy_from_user(&params, optval, optlen))
2821 return -EFAULT;
2823 sp = sctp_sk(sk);
2825 if (params.assoc_id != 0) {
2826 asoc = sctp_id2assoc(sk, params.assoc_id);
2827 if (!asoc)
2828 return -EINVAL;
2829 asoc->default_rcv_context = params.assoc_value;
2830 } else {
2831 sp->default_rcv_context = params.assoc_value;
2834 return 0;
2838 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2840 * This options will at a minimum specify if the implementation is doing
2841 * fragmented interleave. Fragmented interleave, for a one to many
2842 * socket, is when subsequent calls to receive a message may return
2843 * parts of messages from different associations. Some implementations
2844 * may allow you to turn this value on or off. If so, when turned off,
2845 * no fragment interleave will occur (which will cause a head of line
2846 * blocking amongst multiple associations sharing the same one to many
2847 * socket). When this option is turned on, then each receive call may
2848 * come from a different association (thus the user must receive data
2849 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2850 * association each receive belongs to.
2852 * This option takes a boolean value. A non-zero value indicates that
2853 * fragmented interleave is on. A value of zero indicates that
2854 * fragmented interleave is off.
2856 * Note that it is important that an implementation that allows this
2857 * option to be turned on, have it off by default. Otherwise an unaware
2858 * application using the one to many model may become confused and act
2859 * incorrectly.
2861 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2862 char __user *optval,
2863 int optlen)
2865 int val;
2867 if (optlen != sizeof(int))
2868 return -EINVAL;
2869 if (get_user(val, (int __user *)optval))
2870 return -EFAULT;
2872 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2874 return 0;
2878 * 7.1.25. Set or Get the sctp partial delivery point
2879 * (SCTP_PARTIAL_DELIVERY_POINT)
2880 * This option will set or get the SCTP partial delivery point. This
2881 * point is the size of a message where the partial delivery API will be
2882 * invoked to help free up rwnd space for the peer. Setting this to a
2883 * lower value will cause partial delivery's to happen more often. The
2884 * calls argument is an integer that sets or gets the partial delivery
2885 * point.
2887 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2888 char __user *optval,
2889 int optlen)
2891 u32 val;
2893 if (optlen != sizeof(u32))
2894 return -EINVAL;
2895 if (get_user(val, (int __user *)optval))
2896 return -EFAULT;
2898 sctp_sk(sk)->pd_point = val;
2900 return 0; /* is this the right error code? */
2904 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2906 * This option will allow a user to change the maximum burst of packets
2907 * that can be emitted by this association. Note that the default value
2908 * is 4, and some implementations may restrict this setting so that it
2909 * can only be lowered.
2911 * NOTE: This text doesn't seem right. Do this on a socket basis with
2912 * future associations inheriting the socket value.
2914 static int sctp_setsockopt_maxburst(struct sock *sk,
2915 char __user *optval,
2916 int optlen)
2918 int val;
2920 if (optlen != sizeof(int))
2921 return -EINVAL;
2922 if (get_user(val, (int __user *)optval))
2923 return -EFAULT;
2925 if (val < 0)
2926 return -EINVAL;
2928 sctp_sk(sk)->max_burst = val;
2930 return 0;
2933 /* API 6.2 setsockopt(), getsockopt()
2935 * Applications use setsockopt() and getsockopt() to set or retrieve
2936 * socket options. Socket options are used to change the default
2937 * behavior of sockets calls. They are described in Section 7.
2939 * The syntax is:
2941 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2942 * int __user *optlen);
2943 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2944 * int optlen);
2946 * sd - the socket descript.
2947 * level - set to IPPROTO_SCTP for all SCTP options.
2948 * optname - the option name.
2949 * optval - the buffer to store the value of the option.
2950 * optlen - the size of the buffer.
2952 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2953 char __user *optval, int optlen)
2955 int retval = 0;
2957 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2958 sk, optname);
2960 /* I can hardly begin to describe how wrong this is. This is
2961 * so broken as to be worse than useless. The API draft
2962 * REALLY is NOT helpful here... I am not convinced that the
2963 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2964 * are at all well-founded.
2966 if (level != SOL_SCTP) {
2967 struct sctp_af *af = sctp_sk(sk)->pf->af;
2968 retval = af->setsockopt(sk, level, optname, optval, optlen);
2969 goto out_nounlock;
2972 sctp_lock_sock(sk);
2974 switch (optname) {
2975 case SCTP_SOCKOPT_BINDX_ADD:
2976 /* 'optlen' is the size of the addresses buffer. */
2977 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2978 optlen, SCTP_BINDX_ADD_ADDR);
2979 break;
2981 case SCTP_SOCKOPT_BINDX_REM:
2982 /* 'optlen' is the size of the addresses buffer. */
2983 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2984 optlen, SCTP_BINDX_REM_ADDR);
2985 break;
2987 case SCTP_SOCKOPT_CONNECTX:
2988 /* 'optlen' is the size of the addresses buffer. */
2989 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2990 optlen);
2991 break;
2993 case SCTP_DISABLE_FRAGMENTS:
2994 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2995 break;
2997 case SCTP_EVENTS:
2998 retval = sctp_setsockopt_events(sk, optval, optlen);
2999 break;
3001 case SCTP_AUTOCLOSE:
3002 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3003 break;
3005 case SCTP_PEER_ADDR_PARAMS:
3006 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3007 break;
3009 case SCTP_DELAYED_ACK_TIME:
3010 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3011 break;
3012 case SCTP_PARTIAL_DELIVERY_POINT:
3013 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3014 break;
3016 case SCTP_INITMSG:
3017 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3018 break;
3019 case SCTP_DEFAULT_SEND_PARAM:
3020 retval = sctp_setsockopt_default_send_param(sk, optval,
3021 optlen);
3022 break;
3023 case SCTP_PRIMARY_ADDR:
3024 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3025 break;
3026 case SCTP_SET_PEER_PRIMARY_ADDR:
3027 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3028 break;
3029 case SCTP_NODELAY:
3030 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3031 break;
3032 case SCTP_RTOINFO:
3033 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3034 break;
3035 case SCTP_ASSOCINFO:
3036 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3037 break;
3038 case SCTP_I_WANT_MAPPED_V4_ADDR:
3039 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3040 break;
3041 case SCTP_MAXSEG:
3042 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3043 break;
3044 case SCTP_ADAPTATION_LAYER:
3045 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3046 break;
3047 case SCTP_CONTEXT:
3048 retval = sctp_setsockopt_context(sk, optval, optlen);
3049 break;
3050 case SCTP_FRAGMENT_INTERLEAVE:
3051 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3052 break;
3053 case SCTP_MAX_BURST:
3054 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3055 break;
3056 default:
3057 retval = -ENOPROTOOPT;
3058 break;
3061 sctp_release_sock(sk);
3063 out_nounlock:
3064 return retval;
3067 /* API 3.1.6 connect() - UDP Style Syntax
3069 * An application may use the connect() call in the UDP model to initiate an
3070 * association without sending data.
3072 * The syntax is:
3074 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3076 * sd: the socket descriptor to have a new association added to.
3078 * nam: the address structure (either struct sockaddr_in or struct
3079 * sockaddr_in6 defined in RFC2553 [7]).
3081 * len: the size of the address.
3083 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3084 int addr_len)
3086 int err = 0;
3087 struct sctp_af *af;
3089 sctp_lock_sock(sk);
3091 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3092 __FUNCTION__, sk, addr, addr_len);
3094 /* Validate addr_len before calling common connect/connectx routine. */
3095 af = sctp_get_af_specific(addr->sa_family);
3096 if (!af || addr_len < af->sockaddr_len) {
3097 err = -EINVAL;
3098 } else {
3099 /* Pass correct addr len to common routine (so it knows there
3100 * is only one address being passed.
3102 err = __sctp_connect(sk, addr, af->sockaddr_len);
3105 sctp_release_sock(sk);
3106 return err;
3109 /* FIXME: Write comments. */
3110 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3112 return -EOPNOTSUPP; /* STUB */
3115 /* 4.1.4 accept() - TCP Style Syntax
3117 * Applications use accept() call to remove an established SCTP
3118 * association from the accept queue of the endpoint. A new socket
3119 * descriptor will be returned from accept() to represent the newly
3120 * formed association.
3122 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3124 struct sctp_sock *sp;
3125 struct sctp_endpoint *ep;
3126 struct sock *newsk = NULL;
3127 struct sctp_association *asoc;
3128 long timeo;
3129 int error = 0;
3131 sctp_lock_sock(sk);
3133 sp = sctp_sk(sk);
3134 ep = sp->ep;
3136 if (!sctp_style(sk, TCP)) {
3137 error = -EOPNOTSUPP;
3138 goto out;
3141 if (!sctp_sstate(sk, LISTENING)) {
3142 error = -EINVAL;
3143 goto out;
3146 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3148 error = sctp_wait_for_accept(sk, timeo);
3149 if (error)
3150 goto out;
3152 /* We treat the list of associations on the endpoint as the accept
3153 * queue and pick the first association on the list.
3155 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3157 newsk = sp->pf->create_accept_sk(sk, asoc);
3158 if (!newsk) {
3159 error = -ENOMEM;
3160 goto out;
3163 /* Populate the fields of the newsk from the oldsk and migrate the
3164 * asoc to the newsk.
3166 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3168 out:
3169 sctp_release_sock(sk);
3170 *err = error;
3171 return newsk;
3174 /* The SCTP ioctl handler. */
3175 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3177 return -ENOIOCTLCMD;
3180 /* This is the function which gets called during socket creation to
3181 * initialized the SCTP-specific portion of the sock.
3182 * The sock structure should already be zero-filled memory.
3184 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3186 struct sctp_endpoint *ep;
3187 struct sctp_sock *sp;
3189 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3191 sp = sctp_sk(sk);
3193 /* Initialize the SCTP per socket area. */
3194 switch (sk->sk_type) {
3195 case SOCK_SEQPACKET:
3196 sp->type = SCTP_SOCKET_UDP;
3197 break;
3198 case SOCK_STREAM:
3199 sp->type = SCTP_SOCKET_TCP;
3200 break;
3201 default:
3202 return -ESOCKTNOSUPPORT;
3205 /* Initialize default send parameters. These parameters can be
3206 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3208 sp->default_stream = 0;
3209 sp->default_ppid = 0;
3210 sp->default_flags = 0;
3211 sp->default_context = 0;
3212 sp->default_timetolive = 0;
3214 sp->default_rcv_context = 0;
3215 sp->max_burst = sctp_max_burst;
3217 /* Initialize default setup parameters. These parameters
3218 * can be modified with the SCTP_INITMSG socket option or
3219 * overridden by the SCTP_INIT CMSG.
3221 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3222 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3223 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3224 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3226 /* Initialize default RTO related parameters. These parameters can
3227 * be modified for with the SCTP_RTOINFO socket option.
3229 sp->rtoinfo.srto_initial = sctp_rto_initial;
3230 sp->rtoinfo.srto_max = sctp_rto_max;
3231 sp->rtoinfo.srto_min = sctp_rto_min;
3233 /* Initialize default association related parameters. These parameters
3234 * can be modified with the SCTP_ASSOCINFO socket option.
3236 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3237 sp->assocparams.sasoc_number_peer_destinations = 0;
3238 sp->assocparams.sasoc_peer_rwnd = 0;
3239 sp->assocparams.sasoc_local_rwnd = 0;
3240 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3242 /* Initialize default event subscriptions. By default, all the
3243 * options are off.
3245 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3247 /* Default Peer Address Parameters. These defaults can
3248 * be modified via SCTP_PEER_ADDR_PARAMS
3250 sp->hbinterval = sctp_hb_interval;
3251 sp->pathmaxrxt = sctp_max_retrans_path;
3252 sp->pathmtu = 0; // allow default discovery
3253 sp->sackdelay = sctp_sack_timeout;
3254 sp->param_flags = SPP_HB_ENABLE |
3255 SPP_PMTUD_ENABLE |
3256 SPP_SACKDELAY_ENABLE;
3258 /* If enabled no SCTP message fragmentation will be performed.
3259 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3261 sp->disable_fragments = 0;
3263 /* Enable Nagle algorithm by default. */
3264 sp->nodelay = 0;
3266 /* Enable by default. */
3267 sp->v4mapped = 1;
3269 /* Auto-close idle associations after the configured
3270 * number of seconds. A value of 0 disables this
3271 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3272 * for UDP-style sockets only.
3274 sp->autoclose = 0;
3276 /* User specified fragmentation limit. */
3277 sp->user_frag = 0;
3279 sp->adaptation_ind = 0;
3281 sp->pf = sctp_get_pf_specific(sk->sk_family);
3283 /* Control variables for partial data delivery. */
3284 atomic_set(&sp->pd_mode, 0);
3285 skb_queue_head_init(&sp->pd_lobby);
3286 sp->frag_interleave = 0;
3288 /* Create a per socket endpoint structure. Even if we
3289 * change the data structure relationships, this may still
3290 * be useful for storing pre-connect address information.
3292 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3293 if (!ep)
3294 return -ENOMEM;
3296 sp->ep = ep;
3297 sp->hmac = NULL;
3299 SCTP_DBG_OBJCNT_INC(sock);
3300 return 0;
3303 /* Cleanup any SCTP per socket resources. */
3304 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3306 struct sctp_endpoint *ep;
3308 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3310 /* Release our hold on the endpoint. */
3311 ep = sctp_sk(sk)->ep;
3312 sctp_endpoint_free(ep);
3314 return 0;
3317 /* API 4.1.7 shutdown() - TCP Style Syntax
3318 * int shutdown(int socket, int how);
3320 * sd - the socket descriptor of the association to be closed.
3321 * how - Specifies the type of shutdown. The values are
3322 * as follows:
3323 * SHUT_RD
3324 * Disables further receive operations. No SCTP
3325 * protocol action is taken.
3326 * SHUT_WR
3327 * Disables further send operations, and initiates
3328 * the SCTP shutdown sequence.
3329 * SHUT_RDWR
3330 * Disables further send and receive operations
3331 * and initiates the SCTP shutdown sequence.
3333 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3335 struct sctp_endpoint *ep;
3336 struct sctp_association *asoc;
3338 if (!sctp_style(sk, TCP))
3339 return;
3341 if (how & SEND_SHUTDOWN) {
3342 ep = sctp_sk(sk)->ep;
3343 if (!list_empty(&ep->asocs)) {
3344 asoc = list_entry(ep->asocs.next,
3345 struct sctp_association, asocs);
3346 sctp_primitive_SHUTDOWN(asoc, NULL);
3351 /* 7.2.1 Association Status (SCTP_STATUS)
3353 * Applications can retrieve current status information about an
3354 * association, including association state, peer receiver window size,
3355 * number of unacked data chunks, and number of data chunks pending
3356 * receipt. This information is read-only.
3358 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3359 char __user *optval,
3360 int __user *optlen)
3362 struct sctp_status status;
3363 struct sctp_association *asoc = NULL;
3364 struct sctp_transport *transport;
3365 sctp_assoc_t associd;
3366 int retval = 0;
3368 if (len != sizeof(status)) {
3369 retval = -EINVAL;
3370 goto out;
3373 if (copy_from_user(&status, optval, sizeof(status))) {
3374 retval = -EFAULT;
3375 goto out;
3378 associd = status.sstat_assoc_id;
3379 asoc = sctp_id2assoc(sk, associd);
3380 if (!asoc) {
3381 retval = -EINVAL;
3382 goto out;
3385 transport = asoc->peer.primary_path;
3387 status.sstat_assoc_id = sctp_assoc2id(asoc);
3388 status.sstat_state = asoc->state;
3389 status.sstat_rwnd = asoc->peer.rwnd;
3390 status.sstat_unackdata = asoc->unack_data;
3392 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3393 status.sstat_instrms = asoc->c.sinit_max_instreams;
3394 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3395 status.sstat_fragmentation_point = asoc->frag_point;
3396 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3397 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3398 transport->af_specific->sockaddr_len);
3399 /* Map ipv4 address into v4-mapped-on-v6 address. */
3400 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3401 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3402 status.sstat_primary.spinfo_state = transport->state;
3403 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3404 status.sstat_primary.spinfo_srtt = transport->srtt;
3405 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3406 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3408 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3409 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3411 if (put_user(len, optlen)) {
3412 retval = -EFAULT;
3413 goto out;
3416 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3417 len, status.sstat_state, status.sstat_rwnd,
3418 status.sstat_assoc_id);
3420 if (copy_to_user(optval, &status, len)) {
3421 retval = -EFAULT;
3422 goto out;
3425 out:
3426 return (retval);
3430 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3432 * Applications can retrieve information about a specific peer address
3433 * of an association, including its reachability state, congestion
3434 * window, and retransmission timer values. This information is
3435 * read-only.
3437 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3438 char __user *optval,
3439 int __user *optlen)
3441 struct sctp_paddrinfo pinfo;
3442 struct sctp_transport *transport;
3443 int retval = 0;
3445 if (len != sizeof(pinfo)) {
3446 retval = -EINVAL;
3447 goto out;
3450 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3451 retval = -EFAULT;
3452 goto out;
3455 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3456 pinfo.spinfo_assoc_id);
3457 if (!transport)
3458 return -EINVAL;
3460 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3461 pinfo.spinfo_state = transport->state;
3462 pinfo.spinfo_cwnd = transport->cwnd;
3463 pinfo.spinfo_srtt = transport->srtt;
3464 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3465 pinfo.spinfo_mtu = transport->pathmtu;
3467 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3468 pinfo.spinfo_state = SCTP_ACTIVE;
3470 if (put_user(len, optlen)) {
3471 retval = -EFAULT;
3472 goto out;
3475 if (copy_to_user(optval, &pinfo, len)) {
3476 retval = -EFAULT;
3477 goto out;
3480 out:
3481 return (retval);
3484 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3486 * This option is a on/off flag. If enabled no SCTP message
3487 * fragmentation will be performed. Instead if a message being sent
3488 * exceeds the current PMTU size, the message will NOT be sent and
3489 * instead a error will be indicated to the user.
3491 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3492 char __user *optval, int __user *optlen)
3494 int val;
3496 if (len < sizeof(int))
3497 return -EINVAL;
3499 len = sizeof(int);
3500 val = (sctp_sk(sk)->disable_fragments == 1);
3501 if (put_user(len, optlen))
3502 return -EFAULT;
3503 if (copy_to_user(optval, &val, len))
3504 return -EFAULT;
3505 return 0;
3508 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3510 * This socket option is used to specify various notifications and
3511 * ancillary data the user wishes to receive.
3513 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3514 int __user *optlen)
3516 if (len != sizeof(struct sctp_event_subscribe))
3517 return -EINVAL;
3518 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3519 return -EFAULT;
3520 return 0;
3523 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3525 * This socket option is applicable to the UDP-style socket only. When
3526 * set it will cause associations that are idle for more than the
3527 * specified number of seconds to automatically close. An association
3528 * being idle is defined an association that has NOT sent or received
3529 * user data. The special value of '0' indicates that no automatic
3530 * close of any associations should be performed. The option expects an
3531 * integer defining the number of seconds of idle time before an
3532 * association is closed.
3534 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3536 /* Applicable to UDP-style socket only */
3537 if (sctp_style(sk, TCP))
3538 return -EOPNOTSUPP;
3539 if (len != sizeof(int))
3540 return -EINVAL;
3541 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3542 return -EFAULT;
3543 return 0;
3546 /* Helper routine to branch off an association to a new socket. */
3547 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3548 struct socket **sockp)
3550 struct sock *sk = asoc->base.sk;
3551 struct socket *sock;
3552 struct inet_sock *inetsk;
3553 struct sctp_af *af;
3554 int err = 0;
3556 /* An association cannot be branched off from an already peeled-off
3557 * socket, nor is this supported for tcp style sockets.
3559 if (!sctp_style(sk, UDP))
3560 return -EINVAL;
3562 /* Create a new socket. */
3563 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3564 if (err < 0)
3565 return err;
3567 /* Populate the fields of the newsk from the oldsk and migrate the
3568 * asoc to the newsk.
3570 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3572 /* Make peeled-off sockets more like 1-1 accepted sockets.
3573 * Set the daddr and initialize id to something more random
3575 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3576 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3577 inetsk = inet_sk(sock->sk);
3578 inetsk->id = asoc->next_tsn ^ jiffies;
3580 *sockp = sock;
3582 return err;
3585 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3587 sctp_peeloff_arg_t peeloff;
3588 struct socket *newsock;
3589 int retval = 0;
3590 struct sctp_association *asoc;
3592 if (len != sizeof(sctp_peeloff_arg_t))
3593 return -EINVAL;
3594 if (copy_from_user(&peeloff, optval, len))
3595 return -EFAULT;
3597 asoc = sctp_id2assoc(sk, peeloff.associd);
3598 if (!asoc) {
3599 retval = -EINVAL;
3600 goto out;
3603 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3605 retval = sctp_do_peeloff(asoc, &newsock);
3606 if (retval < 0)
3607 goto out;
3609 /* Map the socket to an unused fd that can be returned to the user. */
3610 retval = sock_map_fd(newsock);
3611 if (retval < 0) {
3612 sock_release(newsock);
3613 goto out;
3616 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3617 __FUNCTION__, sk, asoc, newsock->sk, retval);
3619 /* Return the fd mapped to the new socket. */
3620 peeloff.sd = retval;
3621 if (copy_to_user(optval, &peeloff, len))
3622 retval = -EFAULT;
3624 out:
3625 return retval;
3628 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3630 * Applications can enable or disable heartbeats for any peer address of
3631 * an association, modify an address's heartbeat interval, force a
3632 * heartbeat to be sent immediately, and adjust the address's maximum
3633 * number of retransmissions sent before an address is considered
3634 * unreachable. The following structure is used to access and modify an
3635 * address's parameters:
3637 * struct sctp_paddrparams {
3638 * sctp_assoc_t spp_assoc_id;
3639 * struct sockaddr_storage spp_address;
3640 * uint32_t spp_hbinterval;
3641 * uint16_t spp_pathmaxrxt;
3642 * uint32_t spp_pathmtu;
3643 * uint32_t spp_sackdelay;
3644 * uint32_t spp_flags;
3645 * };
3647 * spp_assoc_id - (one-to-many style socket) This is filled in the
3648 * application, and identifies the association for
3649 * this query.
3650 * spp_address - This specifies which address is of interest.
3651 * spp_hbinterval - This contains the value of the heartbeat interval,
3652 * in milliseconds. If a value of zero
3653 * is present in this field then no changes are to
3654 * be made to this parameter.
3655 * spp_pathmaxrxt - This contains the maximum number of
3656 * retransmissions before this address shall be
3657 * considered unreachable. If a value of zero
3658 * is present in this field then no changes are to
3659 * be made to this parameter.
3660 * spp_pathmtu - When Path MTU discovery is disabled the value
3661 * specified here will be the "fixed" path mtu.
3662 * Note that if the spp_address field is empty
3663 * then all associations on this address will
3664 * have this fixed path mtu set upon them.
3666 * spp_sackdelay - When delayed sack is enabled, this value specifies
3667 * the number of milliseconds that sacks will be delayed
3668 * for. This value will apply to all addresses of an
3669 * association if the spp_address field is empty. Note
3670 * also, that if delayed sack is enabled and this
3671 * value is set to 0, no change is made to the last
3672 * recorded delayed sack timer value.
3674 * spp_flags - These flags are used to control various features
3675 * on an association. The flag field may contain
3676 * zero or more of the following options.
3678 * SPP_HB_ENABLE - Enable heartbeats on the
3679 * specified address. Note that if the address
3680 * field is empty all addresses for the association
3681 * have heartbeats enabled upon them.
3683 * SPP_HB_DISABLE - Disable heartbeats on the
3684 * speicifed address. Note that if the address
3685 * field is empty all addresses for the association
3686 * will have their heartbeats disabled. Note also
3687 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3688 * mutually exclusive, only one of these two should
3689 * be specified. Enabling both fields will have
3690 * undetermined results.
3692 * SPP_HB_DEMAND - Request a user initiated heartbeat
3693 * to be made immediately.
3695 * SPP_PMTUD_ENABLE - This field will enable PMTU
3696 * discovery upon the specified address. Note that
3697 * if the address feild is empty then all addresses
3698 * on the association are effected.
3700 * SPP_PMTUD_DISABLE - This field will disable PMTU
3701 * discovery upon the specified address. Note that
3702 * if the address feild is empty then all addresses
3703 * on the association are effected. Not also that
3704 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3705 * exclusive. Enabling both will have undetermined
3706 * results.
3708 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3709 * on delayed sack. The time specified in spp_sackdelay
3710 * is used to specify the sack delay for this address. Note
3711 * that if spp_address is empty then all addresses will
3712 * enable delayed sack and take on the sack delay
3713 * value specified in spp_sackdelay.
3714 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3715 * off delayed sack. If the spp_address field is blank then
3716 * delayed sack is disabled for the entire association. Note
3717 * also that this field is mutually exclusive to
3718 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3719 * results.
3721 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3722 char __user *optval, int __user *optlen)
3724 struct sctp_paddrparams params;
3725 struct sctp_transport *trans = NULL;
3726 struct sctp_association *asoc = NULL;
3727 struct sctp_sock *sp = sctp_sk(sk);
3729 if (len != sizeof(struct sctp_paddrparams))
3730 return -EINVAL;
3732 if (copy_from_user(&params, optval, len))
3733 return -EFAULT;
3735 /* If an address other than INADDR_ANY is specified, and
3736 * no transport is found, then the request is invalid.
3738 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3739 trans = sctp_addr_id2transport(sk, &params.spp_address,
3740 params.spp_assoc_id);
3741 if (!trans) {
3742 SCTP_DEBUG_PRINTK("Failed no transport\n");
3743 return -EINVAL;
3747 /* Get association, if assoc_id != 0 and the socket is a one
3748 * to many style socket, and an association was not found, then
3749 * the id was invalid.
3751 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3752 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3753 SCTP_DEBUG_PRINTK("Failed no association\n");
3754 return -EINVAL;
3757 if (trans) {
3758 /* Fetch transport values. */
3759 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3760 params.spp_pathmtu = trans->pathmtu;
3761 params.spp_pathmaxrxt = trans->pathmaxrxt;
3762 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3764 /*draft-11 doesn't say what to return in spp_flags*/
3765 params.spp_flags = trans->param_flags;
3766 } else if (asoc) {
3767 /* Fetch association values. */
3768 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3769 params.spp_pathmtu = asoc->pathmtu;
3770 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3771 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3773 /*draft-11 doesn't say what to return in spp_flags*/
3774 params.spp_flags = asoc->param_flags;
3775 } else {
3776 /* Fetch socket values. */
3777 params.spp_hbinterval = sp->hbinterval;
3778 params.spp_pathmtu = sp->pathmtu;
3779 params.spp_sackdelay = sp->sackdelay;
3780 params.spp_pathmaxrxt = sp->pathmaxrxt;
3782 /*draft-11 doesn't say what to return in spp_flags*/
3783 params.spp_flags = sp->param_flags;
3786 if (copy_to_user(optval, &params, len))
3787 return -EFAULT;
3789 if (put_user(len, optlen))
3790 return -EFAULT;
3792 return 0;
3795 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3797 * This options will get or set the delayed ack timer. The time is set
3798 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3799 * endpoints default delayed ack timer value. If the assoc_id field is
3800 * non-zero, then the set or get effects the specified association.
3802 * struct sctp_assoc_value {
3803 * sctp_assoc_t assoc_id;
3804 * uint32_t assoc_value;
3805 * };
3807 * assoc_id - This parameter, indicates which association the
3808 * user is preforming an action upon. Note that if
3809 * this field's value is zero then the endpoints
3810 * default value is changed (effecting future
3811 * associations only).
3813 * assoc_value - This parameter contains the number of milliseconds
3814 * that the user is requesting the delayed ACK timer
3815 * be set to. Note that this value is defined in
3816 * the standard to be between 200 and 500 milliseconds.
3818 * Note: a value of zero will leave the value alone,
3819 * but disable SACK delay. A non-zero value will also
3820 * enable SACK delay.
3822 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3823 char __user *optval,
3824 int __user *optlen)
3826 struct sctp_assoc_value params;
3827 struct sctp_association *asoc = NULL;
3828 struct sctp_sock *sp = sctp_sk(sk);
3830 if (len != sizeof(struct sctp_assoc_value))
3831 return - EINVAL;
3833 if (copy_from_user(&params, optval, len))
3834 return -EFAULT;
3836 /* Get association, if assoc_id != 0 and the socket is a one
3837 * to many style socket, and an association was not found, then
3838 * the id was invalid.
3840 asoc = sctp_id2assoc(sk, params.assoc_id);
3841 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3842 return -EINVAL;
3844 if (asoc) {
3845 /* Fetch association values. */
3846 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3847 params.assoc_value = jiffies_to_msecs(
3848 asoc->sackdelay);
3849 else
3850 params.assoc_value = 0;
3851 } else {
3852 /* Fetch socket values. */
3853 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3854 params.assoc_value = sp->sackdelay;
3855 else
3856 params.assoc_value = 0;
3859 if (copy_to_user(optval, &params, len))
3860 return -EFAULT;
3862 if (put_user(len, optlen))
3863 return -EFAULT;
3865 return 0;
3868 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3870 * Applications can specify protocol parameters for the default association
3871 * initialization. The option name argument to setsockopt() and getsockopt()
3872 * is SCTP_INITMSG.
3874 * Setting initialization parameters is effective only on an unconnected
3875 * socket (for UDP-style sockets only future associations are effected
3876 * by the change). With TCP-style sockets, this option is inherited by
3877 * sockets derived from a listener socket.
3879 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3881 if (len != sizeof(struct sctp_initmsg))
3882 return -EINVAL;
3883 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3884 return -EFAULT;
3885 return 0;
3888 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3889 char __user *optval,
3890 int __user *optlen)
3892 sctp_assoc_t id;
3893 struct sctp_association *asoc;
3894 struct list_head *pos;
3895 int cnt = 0;
3897 if (len != sizeof(sctp_assoc_t))
3898 return -EINVAL;
3900 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3901 return -EFAULT;
3903 /* For UDP-style sockets, id specifies the association to query. */
3904 asoc = sctp_id2assoc(sk, id);
3905 if (!asoc)
3906 return -EINVAL;
3908 list_for_each(pos, &asoc->peer.transport_addr_list) {
3909 cnt ++;
3912 return cnt;
3916 * Old API for getting list of peer addresses. Does not work for 32-bit
3917 * programs running on a 64-bit kernel
3919 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3920 char __user *optval,
3921 int __user *optlen)
3923 struct sctp_association *asoc;
3924 struct list_head *pos;
3925 int cnt = 0;
3926 struct sctp_getaddrs_old getaddrs;
3927 struct sctp_transport *from;
3928 void __user *to;
3929 union sctp_addr temp;
3930 struct sctp_sock *sp = sctp_sk(sk);
3931 int addrlen;
3933 if (len != sizeof(struct sctp_getaddrs_old))
3934 return -EINVAL;
3936 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3937 return -EFAULT;
3939 if (getaddrs.addr_num <= 0) return -EINVAL;
3941 /* For UDP-style sockets, id specifies the association to query. */
3942 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3943 if (!asoc)
3944 return -EINVAL;
3946 to = (void __user *)getaddrs.addrs;
3947 list_for_each(pos, &asoc->peer.transport_addr_list) {
3948 from = list_entry(pos, struct sctp_transport, transports);
3949 memcpy(&temp, &from->ipaddr, sizeof(temp));
3950 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3951 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3952 if (copy_to_user(to, &temp, addrlen))
3953 return -EFAULT;
3954 to += addrlen ;
3955 cnt ++;
3956 if (cnt >= getaddrs.addr_num) break;
3958 getaddrs.addr_num = cnt;
3959 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3960 return -EFAULT;
3962 return 0;
3965 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3966 char __user *optval, int __user *optlen)
3968 struct sctp_association *asoc;
3969 struct list_head *pos;
3970 int cnt = 0;
3971 struct sctp_getaddrs getaddrs;
3972 struct sctp_transport *from;
3973 void __user *to;
3974 union sctp_addr temp;
3975 struct sctp_sock *sp = sctp_sk(sk);
3976 int addrlen;
3977 size_t space_left;
3978 int bytes_copied;
3980 if (len < sizeof(struct sctp_getaddrs))
3981 return -EINVAL;
3983 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3984 return -EFAULT;
3986 /* For UDP-style sockets, id specifies the association to query. */
3987 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3988 if (!asoc)
3989 return -EINVAL;
3991 to = optval + offsetof(struct sctp_getaddrs,addrs);
3992 space_left = len - sizeof(struct sctp_getaddrs) -
3993 offsetof(struct sctp_getaddrs,addrs);
3995 list_for_each(pos, &asoc->peer.transport_addr_list) {
3996 from = list_entry(pos, struct sctp_transport, transports);
3997 memcpy(&temp, &from->ipaddr, sizeof(temp));
3998 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3999 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4000 if (space_left < addrlen)
4001 return -ENOMEM;
4002 if (copy_to_user(to, &temp, addrlen))
4003 return -EFAULT;
4004 to += addrlen;
4005 cnt++;
4006 space_left -= addrlen;
4009 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4010 return -EFAULT;
4011 bytes_copied = ((char __user *)to) - optval;
4012 if (put_user(bytes_copied, optlen))
4013 return -EFAULT;
4015 return 0;
4018 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4019 char __user *optval,
4020 int __user *optlen)
4022 sctp_assoc_t id;
4023 struct sctp_bind_addr *bp;
4024 struct sctp_association *asoc;
4025 struct list_head *pos, *temp;
4026 struct sctp_sockaddr_entry *addr;
4027 rwlock_t *addr_lock;
4028 int cnt = 0;
4030 if (len != sizeof(sctp_assoc_t))
4031 return -EINVAL;
4033 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4034 return -EFAULT;
4037 * For UDP-style sockets, id specifies the association to query.
4038 * If the id field is set to the value '0' then the locally bound
4039 * addresses are returned without regard to any particular
4040 * association.
4042 if (0 == id) {
4043 bp = &sctp_sk(sk)->ep->base.bind_addr;
4044 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4045 } else {
4046 asoc = sctp_id2assoc(sk, id);
4047 if (!asoc)
4048 return -EINVAL;
4049 bp = &asoc->base.bind_addr;
4050 addr_lock = &asoc->base.addr_lock;
4053 sctp_read_lock(addr_lock);
4055 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4056 * addresses from the global local address list.
4058 if (sctp_list_single_entry(&bp->address_list)) {
4059 addr = list_entry(bp->address_list.next,
4060 struct sctp_sockaddr_entry, list);
4061 if (sctp_is_any(&addr->a)) {
4062 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
4063 addr = list_entry(pos,
4064 struct sctp_sockaddr_entry,
4065 list);
4066 if ((PF_INET == sk->sk_family) &&
4067 (AF_INET6 == addr->a.sa.sa_family))
4068 continue;
4069 cnt++;
4071 } else {
4072 cnt = 1;
4074 goto done;
4077 list_for_each(pos, &bp->address_list) {
4078 cnt ++;
4081 done:
4082 sctp_read_unlock(addr_lock);
4083 return cnt;
4086 /* Helper function that copies local addresses to user and returns the number
4087 * of addresses copied.
4089 static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4090 int max_addrs, void *to,
4091 int *bytes_copied)
4093 struct list_head *pos, *next;
4094 struct sctp_sockaddr_entry *addr;
4095 union sctp_addr temp;
4096 int cnt = 0;
4097 int addrlen;
4099 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4100 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4101 if ((PF_INET == sk->sk_family) &&
4102 (AF_INET6 == addr->a.sa.sa_family))
4103 continue;
4104 memcpy(&temp, &addr->a, sizeof(temp));
4105 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4106 &temp);
4107 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4108 memcpy(to, &temp, addrlen);
4110 to += addrlen;
4111 *bytes_copied += addrlen;
4112 cnt ++;
4113 if (cnt >= max_addrs) break;
4116 return cnt;
4119 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4120 size_t space_left, int *bytes_copied)
4122 struct list_head *pos, *next;
4123 struct sctp_sockaddr_entry *addr;
4124 union sctp_addr temp;
4125 int cnt = 0;
4126 int addrlen;
4128 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4129 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4130 if ((PF_INET == sk->sk_family) &&
4131 (AF_INET6 == addr->a.sa.sa_family))
4132 continue;
4133 memcpy(&temp, &addr->a, sizeof(temp));
4134 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4135 &temp);
4136 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4137 if (space_left < addrlen)
4138 return -ENOMEM;
4139 memcpy(to, &temp, addrlen);
4141 to += addrlen;
4142 cnt ++;
4143 space_left -= addrlen;
4144 bytes_copied += addrlen;
4147 return cnt;
4150 /* Old API for getting list of local addresses. Does not work for 32-bit
4151 * programs running on a 64-bit kernel
4153 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4154 char __user *optval, int __user *optlen)
4156 struct sctp_bind_addr *bp;
4157 struct sctp_association *asoc;
4158 struct list_head *pos;
4159 int cnt = 0;
4160 struct sctp_getaddrs_old getaddrs;
4161 struct sctp_sockaddr_entry *addr;
4162 void __user *to;
4163 union sctp_addr temp;
4164 struct sctp_sock *sp = sctp_sk(sk);
4165 int addrlen;
4166 rwlock_t *addr_lock;
4167 int err = 0;
4168 void *addrs;
4169 void *buf;
4170 int bytes_copied = 0;
4172 if (len != sizeof(struct sctp_getaddrs_old))
4173 return -EINVAL;
4175 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
4176 return -EFAULT;
4178 if (getaddrs.addr_num <= 0) return -EINVAL;
4180 * For UDP-style sockets, id specifies the association to query.
4181 * If the id field is set to the value '0' then the locally bound
4182 * addresses are returned without regard to any particular
4183 * association.
4185 if (0 == getaddrs.assoc_id) {
4186 bp = &sctp_sk(sk)->ep->base.bind_addr;
4187 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4188 } else {
4189 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4190 if (!asoc)
4191 return -EINVAL;
4192 bp = &asoc->base.bind_addr;
4193 addr_lock = &asoc->base.addr_lock;
4196 to = getaddrs.addrs;
4198 /* Allocate space for a local instance of packed array to hold all
4199 * the data. We store addresses here first and then put write them
4200 * to the user in one shot.
4202 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4203 GFP_KERNEL);
4204 if (!addrs)
4205 return -ENOMEM;
4207 sctp_read_lock(addr_lock);
4209 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4210 * addresses from the global local address list.
4212 if (sctp_list_single_entry(&bp->address_list)) {
4213 addr = list_entry(bp->address_list.next,
4214 struct sctp_sockaddr_entry, list);
4215 if (sctp_is_any(&addr->a)) {
4216 cnt = sctp_copy_laddrs_old(sk, bp->port,
4217 getaddrs.addr_num,
4218 addrs, &bytes_copied);
4219 goto copy_getaddrs;
4223 buf = addrs;
4224 list_for_each(pos, &bp->address_list) {
4225 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4226 memcpy(&temp, &addr->a, sizeof(temp));
4227 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4228 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4229 memcpy(buf, &temp, addrlen);
4230 buf += addrlen;
4231 bytes_copied += addrlen;
4232 cnt ++;
4233 if (cnt >= getaddrs.addr_num) break;
4236 copy_getaddrs:
4237 sctp_read_unlock(addr_lock);
4239 /* copy the entire address list into the user provided space */
4240 if (copy_to_user(to, addrs, bytes_copied)) {
4241 err = -EFAULT;
4242 goto error;
4245 /* copy the leading structure back to user */
4246 getaddrs.addr_num = cnt;
4247 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
4248 err = -EFAULT;
4250 error:
4251 kfree(addrs);
4252 return err;
4255 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4256 char __user *optval, int __user *optlen)
4258 struct sctp_bind_addr *bp;
4259 struct sctp_association *asoc;
4260 struct list_head *pos;
4261 int cnt = 0;
4262 struct sctp_getaddrs getaddrs;
4263 struct sctp_sockaddr_entry *addr;
4264 void __user *to;
4265 union sctp_addr temp;
4266 struct sctp_sock *sp = sctp_sk(sk);
4267 int addrlen;
4268 rwlock_t *addr_lock;
4269 int err = 0;
4270 size_t space_left;
4271 int bytes_copied = 0;
4272 void *addrs;
4273 void *buf;
4275 if (len <= sizeof(struct sctp_getaddrs))
4276 return -EINVAL;
4278 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4279 return -EFAULT;
4282 * For UDP-style sockets, id specifies the association to query.
4283 * If the id field is set to the value '0' then the locally bound
4284 * addresses are returned without regard to any particular
4285 * association.
4287 if (0 == getaddrs.assoc_id) {
4288 bp = &sctp_sk(sk)->ep->base.bind_addr;
4289 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4290 } else {
4291 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4292 if (!asoc)
4293 return -EINVAL;
4294 bp = &asoc->base.bind_addr;
4295 addr_lock = &asoc->base.addr_lock;
4298 to = optval + offsetof(struct sctp_getaddrs,addrs);
4299 space_left = len - sizeof(struct sctp_getaddrs) -
4300 offsetof(struct sctp_getaddrs,addrs);
4301 addrs = kmalloc(space_left, GFP_KERNEL);
4302 if (!addrs)
4303 return -ENOMEM;
4305 sctp_read_lock(addr_lock);
4307 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4308 * addresses from the global local address list.
4310 if (sctp_list_single_entry(&bp->address_list)) {
4311 addr = list_entry(bp->address_list.next,
4312 struct sctp_sockaddr_entry, list);
4313 if (sctp_is_any(&addr->a)) {
4314 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4315 space_left, &bytes_copied);
4316 if (cnt < 0) {
4317 err = cnt;
4318 goto error;
4320 goto copy_getaddrs;
4324 buf = addrs;
4325 list_for_each(pos, &bp->address_list) {
4326 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4327 memcpy(&temp, &addr->a, sizeof(temp));
4328 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4329 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4330 if (space_left < addrlen) {
4331 err = -ENOMEM; /*fixme: right error?*/
4332 goto error;
4334 memcpy(buf, &temp, addrlen);
4335 buf += addrlen;
4336 bytes_copied += addrlen;
4337 cnt ++;
4338 space_left -= addrlen;
4341 copy_getaddrs:
4342 sctp_read_unlock(addr_lock);
4344 if (copy_to_user(to, addrs, bytes_copied)) {
4345 err = -EFAULT;
4346 goto error;
4348 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4349 return -EFAULT;
4350 if (put_user(bytes_copied, optlen))
4351 return -EFAULT;
4353 error:
4354 kfree(addrs);
4355 return err;
4358 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4360 * Requests that the local SCTP stack use the enclosed peer address as
4361 * the association primary. The enclosed address must be one of the
4362 * association peer's addresses.
4364 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4365 char __user *optval, int __user *optlen)
4367 struct sctp_prim prim;
4368 struct sctp_association *asoc;
4369 struct sctp_sock *sp = sctp_sk(sk);
4371 if (len != sizeof(struct sctp_prim))
4372 return -EINVAL;
4374 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4375 return -EFAULT;
4377 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4378 if (!asoc)
4379 return -EINVAL;
4381 if (!asoc->peer.primary_path)
4382 return -ENOTCONN;
4384 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4385 asoc->peer.primary_path->af_specific->sockaddr_len);
4387 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4388 (union sctp_addr *)&prim.ssp_addr);
4390 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4391 return -EFAULT;
4393 return 0;
4397 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4399 * Requests that the local endpoint set the specified Adaptation Layer
4400 * Indication parameter for all future INIT and INIT-ACK exchanges.
4402 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4403 char __user *optval, int __user *optlen)
4405 struct sctp_setadaptation adaptation;
4407 if (len != sizeof(struct sctp_setadaptation))
4408 return -EINVAL;
4410 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4411 if (copy_to_user(optval, &adaptation, len))
4412 return -EFAULT;
4414 return 0;
4419 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4421 * Applications that wish to use the sendto() system call may wish to
4422 * specify a default set of parameters that would normally be supplied
4423 * through the inclusion of ancillary data. This socket option allows
4424 * such an application to set the default sctp_sndrcvinfo structure.
4427 * The application that wishes to use this socket option simply passes
4428 * in to this call the sctp_sndrcvinfo structure defined in Section
4429 * 5.2.2) The input parameters accepted by this call include
4430 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4431 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4432 * to this call if the caller is using the UDP model.
4434 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4436 static int sctp_getsockopt_default_send_param(struct sock *sk,
4437 int len, char __user *optval,
4438 int __user *optlen)
4440 struct sctp_sndrcvinfo info;
4441 struct sctp_association *asoc;
4442 struct sctp_sock *sp = sctp_sk(sk);
4444 if (len != sizeof(struct sctp_sndrcvinfo))
4445 return -EINVAL;
4446 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4447 return -EFAULT;
4449 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4450 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4451 return -EINVAL;
4453 if (asoc) {
4454 info.sinfo_stream = asoc->default_stream;
4455 info.sinfo_flags = asoc->default_flags;
4456 info.sinfo_ppid = asoc->default_ppid;
4457 info.sinfo_context = asoc->default_context;
4458 info.sinfo_timetolive = asoc->default_timetolive;
4459 } else {
4460 info.sinfo_stream = sp->default_stream;
4461 info.sinfo_flags = sp->default_flags;
4462 info.sinfo_ppid = sp->default_ppid;
4463 info.sinfo_context = sp->default_context;
4464 info.sinfo_timetolive = sp->default_timetolive;
4467 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4468 return -EFAULT;
4470 return 0;
4475 * 7.1.5 SCTP_NODELAY
4477 * Turn on/off any Nagle-like algorithm. This means that packets are
4478 * generally sent as soon as possible and no unnecessary delays are
4479 * introduced, at the cost of more packets in the network. Expects an
4480 * integer boolean flag.
4483 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4484 char __user *optval, int __user *optlen)
4486 int val;
4488 if (len < sizeof(int))
4489 return -EINVAL;
4491 len = sizeof(int);
4492 val = (sctp_sk(sk)->nodelay == 1);
4493 if (put_user(len, optlen))
4494 return -EFAULT;
4495 if (copy_to_user(optval, &val, len))
4496 return -EFAULT;
4497 return 0;
4502 * 7.1.1 SCTP_RTOINFO
4504 * The protocol parameters used to initialize and bound retransmission
4505 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4506 * and modify these parameters.
4507 * All parameters are time values, in milliseconds. A value of 0, when
4508 * modifying the parameters, indicates that the current value should not
4509 * be changed.
4512 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4513 char __user *optval,
4514 int __user *optlen) {
4515 struct sctp_rtoinfo rtoinfo;
4516 struct sctp_association *asoc;
4518 if (len != sizeof (struct sctp_rtoinfo))
4519 return -EINVAL;
4521 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4522 return -EFAULT;
4524 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4526 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4527 return -EINVAL;
4529 /* Values corresponding to the specific association. */
4530 if (asoc) {
4531 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4532 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4533 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4534 } else {
4535 /* Values corresponding to the endpoint. */
4536 struct sctp_sock *sp = sctp_sk(sk);
4538 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4539 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4540 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4543 if (put_user(len, optlen))
4544 return -EFAULT;
4546 if (copy_to_user(optval, &rtoinfo, len))
4547 return -EFAULT;
4549 return 0;
4554 * 7.1.2 SCTP_ASSOCINFO
4556 * This option is used to tune the maximum retransmission attempts
4557 * of the association.
4558 * Returns an error if the new association retransmission value is
4559 * greater than the sum of the retransmission value of the peer.
4560 * See [SCTP] for more information.
4563 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4564 char __user *optval,
4565 int __user *optlen)
4568 struct sctp_assocparams assocparams;
4569 struct sctp_association *asoc;
4570 struct list_head *pos;
4571 int cnt = 0;
4573 if (len != sizeof (struct sctp_assocparams))
4574 return -EINVAL;
4576 if (copy_from_user(&assocparams, optval,
4577 sizeof (struct sctp_assocparams)))
4578 return -EFAULT;
4580 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4582 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4583 return -EINVAL;
4585 /* Values correspoinding to the specific association */
4586 if (asoc) {
4587 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4588 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4589 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4590 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4591 * 1000) +
4592 (asoc->cookie_life.tv_usec
4593 / 1000);
4595 list_for_each(pos, &asoc->peer.transport_addr_list) {
4596 cnt ++;
4599 assocparams.sasoc_number_peer_destinations = cnt;
4600 } else {
4601 /* Values corresponding to the endpoint */
4602 struct sctp_sock *sp = sctp_sk(sk);
4604 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4605 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4606 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4607 assocparams.sasoc_cookie_life =
4608 sp->assocparams.sasoc_cookie_life;
4609 assocparams.sasoc_number_peer_destinations =
4610 sp->assocparams.
4611 sasoc_number_peer_destinations;
4614 if (put_user(len, optlen))
4615 return -EFAULT;
4617 if (copy_to_user(optval, &assocparams, len))
4618 return -EFAULT;
4620 return 0;
4624 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4626 * This socket option is a boolean flag which turns on or off mapped V4
4627 * addresses. If this option is turned on and the socket is type
4628 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4629 * If this option is turned off, then no mapping will be done of V4
4630 * addresses and a user will receive both PF_INET6 and PF_INET type
4631 * addresses on the socket.
4633 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4634 char __user *optval, int __user *optlen)
4636 int val;
4637 struct sctp_sock *sp = sctp_sk(sk);
4639 if (len < sizeof(int))
4640 return -EINVAL;
4642 len = sizeof(int);
4643 val = sp->v4mapped;
4644 if (put_user(len, optlen))
4645 return -EFAULT;
4646 if (copy_to_user(optval, &val, len))
4647 return -EFAULT;
4649 return 0;
4653 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4654 * (chapter and verse is quoted at sctp_setsockopt_context())
4656 static int sctp_getsockopt_context(struct sock *sk, int len,
4657 char __user *optval, int __user *optlen)
4659 struct sctp_assoc_value params;
4660 struct sctp_sock *sp;
4661 struct sctp_association *asoc;
4663 if (len != sizeof(struct sctp_assoc_value))
4664 return -EINVAL;
4666 if (copy_from_user(&params, optval, len))
4667 return -EFAULT;
4669 sp = sctp_sk(sk);
4671 if (params.assoc_id != 0) {
4672 asoc = sctp_id2assoc(sk, params.assoc_id);
4673 if (!asoc)
4674 return -EINVAL;
4675 params.assoc_value = asoc->default_rcv_context;
4676 } else {
4677 params.assoc_value = sp->default_rcv_context;
4680 if (put_user(len, optlen))
4681 return -EFAULT;
4682 if (copy_to_user(optval, &params, len))
4683 return -EFAULT;
4685 return 0;
4689 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4691 * This socket option specifies the maximum size to put in any outgoing
4692 * SCTP chunk. If a message is larger than this size it will be
4693 * fragmented by SCTP into the specified size. Note that the underlying
4694 * SCTP implementation may fragment into smaller sized chunks when the
4695 * PMTU of the underlying association is smaller than the value set by
4696 * the user.
4698 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4699 char __user *optval, int __user *optlen)
4701 int val;
4703 if (len < sizeof(int))
4704 return -EINVAL;
4706 len = sizeof(int);
4708 val = sctp_sk(sk)->user_frag;
4709 if (put_user(len, optlen))
4710 return -EFAULT;
4711 if (copy_to_user(optval, &val, len))
4712 return -EFAULT;
4714 return 0;
4718 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4719 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4721 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4722 char __user *optval, int __user *optlen)
4724 int val;
4726 if (len < sizeof(int))
4727 return -EINVAL;
4729 len = sizeof(int);
4731 val = sctp_sk(sk)->frag_interleave;
4732 if (put_user(len, optlen))
4733 return -EFAULT;
4734 if (copy_to_user(optval, &val, len))
4735 return -EFAULT;
4737 return 0;
4741 * 7.1.25. Set or Get the sctp partial delivery point
4742 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4744 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4745 char __user *optval,
4746 int __user *optlen)
4748 u32 val;
4750 if (len < sizeof(u32))
4751 return -EINVAL;
4753 len = sizeof(u32);
4755 val = sctp_sk(sk)->pd_point;
4756 if (put_user(len, optlen))
4757 return -EFAULT;
4758 if (copy_to_user(optval, &val, len))
4759 return -EFAULT;
4761 return -ENOTSUPP;
4765 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4766 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4768 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4769 char __user *optval,
4770 int __user *optlen)
4772 int val;
4774 if (len < sizeof(int))
4775 return -EINVAL;
4777 len = sizeof(int);
4779 val = sctp_sk(sk)->max_burst;
4780 if (put_user(len, optlen))
4781 return -EFAULT;
4782 if (copy_to_user(optval, &val, len))
4783 return -EFAULT;
4785 return -ENOTSUPP;
4788 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4789 char __user *optval, int __user *optlen)
4791 int retval = 0;
4792 int len;
4794 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4795 sk, optname);
4797 /* I can hardly begin to describe how wrong this is. This is
4798 * so broken as to be worse than useless. The API draft
4799 * REALLY is NOT helpful here... I am not convinced that the
4800 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4801 * are at all well-founded.
4803 if (level != SOL_SCTP) {
4804 struct sctp_af *af = sctp_sk(sk)->pf->af;
4806 retval = af->getsockopt(sk, level, optname, optval, optlen);
4807 return retval;
4810 if (get_user(len, optlen))
4811 return -EFAULT;
4813 sctp_lock_sock(sk);
4815 switch (optname) {
4816 case SCTP_STATUS:
4817 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4818 break;
4819 case SCTP_DISABLE_FRAGMENTS:
4820 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4821 optlen);
4822 break;
4823 case SCTP_EVENTS:
4824 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4825 break;
4826 case SCTP_AUTOCLOSE:
4827 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4828 break;
4829 case SCTP_SOCKOPT_PEELOFF:
4830 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4831 break;
4832 case SCTP_PEER_ADDR_PARAMS:
4833 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4834 optlen);
4835 break;
4836 case SCTP_DELAYED_ACK_TIME:
4837 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4838 optlen);
4839 break;
4840 case SCTP_INITMSG:
4841 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4842 break;
4843 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4844 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4845 optlen);
4846 break;
4847 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4848 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4849 optlen);
4850 break;
4851 case SCTP_GET_PEER_ADDRS_OLD:
4852 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4853 optlen);
4854 break;
4855 case SCTP_GET_LOCAL_ADDRS_OLD:
4856 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4857 optlen);
4858 break;
4859 case SCTP_GET_PEER_ADDRS:
4860 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4861 optlen);
4862 break;
4863 case SCTP_GET_LOCAL_ADDRS:
4864 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4865 optlen);
4866 break;
4867 case SCTP_DEFAULT_SEND_PARAM:
4868 retval = sctp_getsockopt_default_send_param(sk, len,
4869 optval, optlen);
4870 break;
4871 case SCTP_PRIMARY_ADDR:
4872 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4873 break;
4874 case SCTP_NODELAY:
4875 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4876 break;
4877 case SCTP_RTOINFO:
4878 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4879 break;
4880 case SCTP_ASSOCINFO:
4881 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4882 break;
4883 case SCTP_I_WANT_MAPPED_V4_ADDR:
4884 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4885 break;
4886 case SCTP_MAXSEG:
4887 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4888 break;
4889 case SCTP_GET_PEER_ADDR_INFO:
4890 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4891 optlen);
4892 break;
4893 case SCTP_ADAPTATION_LAYER:
4894 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
4895 optlen);
4896 break;
4897 case SCTP_CONTEXT:
4898 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4899 break;
4900 case SCTP_FRAGMENT_INTERLEAVE:
4901 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4902 optlen);
4903 break;
4904 case SCTP_PARTIAL_DELIVERY_POINT:
4905 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4906 optlen);
4907 break;
4908 case SCTP_MAX_BURST:
4909 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4910 break;
4911 default:
4912 retval = -ENOPROTOOPT;
4913 break;
4916 sctp_release_sock(sk);
4917 return retval;
4920 static void sctp_hash(struct sock *sk)
4922 /* STUB */
4925 static void sctp_unhash(struct sock *sk)
4927 /* STUB */
4930 /* Check if port is acceptable. Possibly find first available port.
4932 * The port hash table (contained in the 'global' SCTP protocol storage
4933 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4934 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4935 * list (the list number is the port number hashed out, so as you
4936 * would expect from a hash function, all the ports in a given list have
4937 * such a number that hashes out to the same list number; you were
4938 * expecting that, right?); so each list has a set of ports, with a
4939 * link to the socket (struct sock) that uses it, the port number and
4940 * a fastreuse flag (FIXME: NPI ipg).
4942 static struct sctp_bind_bucket *sctp_bucket_create(
4943 struct sctp_bind_hashbucket *head, unsigned short snum);
4945 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4947 struct sctp_bind_hashbucket *head; /* hash list */
4948 struct sctp_bind_bucket *pp; /* hash list port iterator */
4949 unsigned short snum;
4950 int ret;
4952 snum = ntohs(addr->v4.sin_port);
4954 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4955 sctp_local_bh_disable();
4957 if (snum == 0) {
4958 /* Search for an available port.
4960 * 'sctp_port_rover' was the last port assigned, so
4961 * we start to search from 'sctp_port_rover +
4962 * 1'. What we do is first check if port 'rover' is
4963 * already in the hash table; if not, we use that; if
4964 * it is, we try next.
4966 int low = sysctl_local_port_range[0];
4967 int high = sysctl_local_port_range[1];
4968 int remaining = (high - low) + 1;
4969 int rover;
4970 int index;
4972 sctp_spin_lock(&sctp_port_alloc_lock);
4973 rover = sctp_port_rover;
4974 do {
4975 rover++;
4976 if ((rover < low) || (rover > high))
4977 rover = low;
4978 index = sctp_phashfn(rover);
4979 head = &sctp_port_hashtable[index];
4980 sctp_spin_lock(&head->lock);
4981 for (pp = head->chain; pp; pp = pp->next)
4982 if (pp->port == rover)
4983 goto next;
4984 break;
4985 next:
4986 sctp_spin_unlock(&head->lock);
4987 } while (--remaining > 0);
4988 sctp_port_rover = rover;
4989 sctp_spin_unlock(&sctp_port_alloc_lock);
4991 /* Exhausted local port range during search? */
4992 ret = 1;
4993 if (remaining <= 0)
4994 goto fail;
4996 /* OK, here is the one we will use. HEAD (the port
4997 * hash table list entry) is non-NULL and we hold it's
4998 * mutex.
5000 snum = rover;
5001 } else {
5002 /* We are given an specific port number; we verify
5003 * that it is not being used. If it is used, we will
5004 * exahust the search in the hash list corresponding
5005 * to the port number (snum) - we detect that with the
5006 * port iterator, pp being NULL.
5008 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5009 sctp_spin_lock(&head->lock);
5010 for (pp = head->chain; pp; pp = pp->next) {
5011 if (pp->port == snum)
5012 goto pp_found;
5015 pp = NULL;
5016 goto pp_not_found;
5017 pp_found:
5018 if (!hlist_empty(&pp->owner)) {
5019 /* We had a port hash table hit - there is an
5020 * available port (pp != NULL) and it is being
5021 * used by other socket (pp->owner not empty); that other
5022 * socket is going to be sk2.
5024 int reuse = sk->sk_reuse;
5025 struct sock *sk2;
5026 struct hlist_node *node;
5028 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5029 if (pp->fastreuse && sk->sk_reuse &&
5030 sk->sk_state != SCTP_SS_LISTENING)
5031 goto success;
5033 /* Run through the list of sockets bound to the port
5034 * (pp->port) [via the pointers bind_next and
5035 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5036 * we get the endpoint they describe and run through
5037 * the endpoint's list of IP (v4 or v6) addresses,
5038 * comparing each of the addresses with the address of
5039 * the socket sk. If we find a match, then that means
5040 * that this port/socket (sk) combination are already
5041 * in an endpoint.
5043 sk_for_each_bound(sk2, node, &pp->owner) {
5044 struct sctp_endpoint *ep2;
5045 ep2 = sctp_sk(sk2)->ep;
5047 if (reuse && sk2->sk_reuse &&
5048 sk2->sk_state != SCTP_SS_LISTENING)
5049 continue;
5051 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
5052 sctp_sk(sk))) {
5053 ret = (long)sk2;
5054 goto fail_unlock;
5057 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5059 pp_not_found:
5060 /* If there was a hash table miss, create a new port. */
5061 ret = 1;
5062 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5063 goto fail_unlock;
5065 /* In either case (hit or miss), make sure fastreuse is 1 only
5066 * if sk->sk_reuse is too (that is, if the caller requested
5067 * SO_REUSEADDR on this socket -sk-).
5069 if (hlist_empty(&pp->owner)) {
5070 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5071 pp->fastreuse = 1;
5072 else
5073 pp->fastreuse = 0;
5074 } else if (pp->fastreuse &&
5075 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5076 pp->fastreuse = 0;
5078 /* We are set, so fill up all the data in the hash table
5079 * entry, tie the socket list information with the rest of the
5080 * sockets FIXME: Blurry, NPI (ipg).
5082 success:
5083 if (!sctp_sk(sk)->bind_hash) {
5084 inet_sk(sk)->num = snum;
5085 sk_add_bind_node(sk, &pp->owner);
5086 sctp_sk(sk)->bind_hash = pp;
5088 ret = 0;
5090 fail_unlock:
5091 sctp_spin_unlock(&head->lock);
5093 fail:
5094 sctp_local_bh_enable();
5095 return ret;
5098 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5099 * port is requested.
5101 static int sctp_get_port(struct sock *sk, unsigned short snum)
5103 long ret;
5104 union sctp_addr addr;
5105 struct sctp_af *af = sctp_sk(sk)->pf->af;
5107 /* Set up a dummy address struct from the sk. */
5108 af->from_sk(&addr, sk);
5109 addr.v4.sin_port = htons(snum);
5111 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5112 ret = sctp_get_port_local(sk, &addr);
5114 return (ret ? 1 : 0);
5118 * 3.1.3 listen() - UDP Style Syntax
5120 * By default, new associations are not accepted for UDP style sockets.
5121 * An application uses listen() to mark a socket as being able to
5122 * accept new associations.
5124 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5126 struct sctp_sock *sp = sctp_sk(sk);
5127 struct sctp_endpoint *ep = sp->ep;
5129 /* Only UDP style sockets that are not peeled off are allowed to
5130 * listen().
5132 if (!sctp_style(sk, UDP))
5133 return -EINVAL;
5135 /* If backlog is zero, disable listening. */
5136 if (!backlog) {
5137 if (sctp_sstate(sk, CLOSED))
5138 return 0;
5140 sctp_unhash_endpoint(ep);
5141 sk->sk_state = SCTP_SS_CLOSED;
5144 /* Return if we are already listening. */
5145 if (sctp_sstate(sk, LISTENING))
5146 return 0;
5149 * If a bind() or sctp_bindx() is not called prior to a listen()
5150 * call that allows new associations to be accepted, the system
5151 * picks an ephemeral port and will choose an address set equivalent
5152 * to binding with a wildcard address.
5154 * This is not currently spelled out in the SCTP sockets
5155 * extensions draft, but follows the practice as seen in TCP
5156 * sockets.
5158 * Additionally, turn off fastreuse flag since we are not listening
5160 sk->sk_state = SCTP_SS_LISTENING;
5161 if (!ep->base.bind_addr.port) {
5162 if (sctp_autobind(sk))
5163 return -EAGAIN;
5164 } else
5165 sctp_sk(sk)->bind_hash->fastreuse = 0;
5167 sctp_hash_endpoint(ep);
5168 return 0;
5172 * 4.1.3 listen() - TCP Style Syntax
5174 * Applications uses listen() to ready the SCTP endpoint for accepting
5175 * inbound associations.
5177 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5179 struct sctp_sock *sp = sctp_sk(sk);
5180 struct sctp_endpoint *ep = sp->ep;
5182 /* If backlog is zero, disable listening. */
5183 if (!backlog) {
5184 if (sctp_sstate(sk, CLOSED))
5185 return 0;
5187 sctp_unhash_endpoint(ep);
5188 sk->sk_state = SCTP_SS_CLOSED;
5191 if (sctp_sstate(sk, LISTENING))
5192 return 0;
5195 * If a bind() or sctp_bindx() is not called prior to a listen()
5196 * call that allows new associations to be accepted, the system
5197 * picks an ephemeral port and will choose an address set equivalent
5198 * to binding with a wildcard address.
5200 * This is not currently spelled out in the SCTP sockets
5201 * extensions draft, but follows the practice as seen in TCP
5202 * sockets.
5204 sk->sk_state = SCTP_SS_LISTENING;
5205 if (!ep->base.bind_addr.port) {
5206 if (sctp_autobind(sk))
5207 return -EAGAIN;
5208 } else
5209 sctp_sk(sk)->bind_hash->fastreuse = 0;
5211 sk->sk_max_ack_backlog = backlog;
5212 sctp_hash_endpoint(ep);
5213 return 0;
5217 * Move a socket to LISTENING state.
5219 int sctp_inet_listen(struct socket *sock, int backlog)
5221 struct sock *sk = sock->sk;
5222 struct crypto_hash *tfm = NULL;
5223 int err = -EINVAL;
5225 if (unlikely(backlog < 0))
5226 goto out;
5228 sctp_lock_sock(sk);
5230 if (sock->state != SS_UNCONNECTED)
5231 goto out;
5233 /* Allocate HMAC for generating cookie. */
5234 if (sctp_hmac_alg) {
5235 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5236 if (IS_ERR(tfm)) {
5237 if (net_ratelimit()) {
5238 printk(KERN_INFO
5239 "SCTP: failed to load transform for %s: %ld\n",
5240 sctp_hmac_alg, PTR_ERR(tfm));
5242 err = -ENOSYS;
5243 goto out;
5247 switch (sock->type) {
5248 case SOCK_SEQPACKET:
5249 err = sctp_seqpacket_listen(sk, backlog);
5250 break;
5251 case SOCK_STREAM:
5252 err = sctp_stream_listen(sk, backlog);
5253 break;
5254 default:
5255 break;
5258 if (err)
5259 goto cleanup;
5261 /* Store away the transform reference. */
5262 sctp_sk(sk)->hmac = tfm;
5263 out:
5264 sctp_release_sock(sk);
5265 return err;
5266 cleanup:
5267 crypto_free_hash(tfm);
5268 goto out;
5272 * This function is done by modeling the current datagram_poll() and the
5273 * tcp_poll(). Note that, based on these implementations, we don't
5274 * lock the socket in this function, even though it seems that,
5275 * ideally, locking or some other mechanisms can be used to ensure
5276 * the integrity of the counters (sndbuf and wmem_alloc) used
5277 * in this place. We assume that we don't need locks either until proven
5278 * otherwise.
5280 * Another thing to note is that we include the Async I/O support
5281 * here, again, by modeling the current TCP/UDP code. We don't have
5282 * a good way to test with it yet.
5284 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5286 struct sock *sk = sock->sk;
5287 struct sctp_sock *sp = sctp_sk(sk);
5288 unsigned int mask;
5290 poll_wait(file, sk->sk_sleep, wait);
5292 /* A TCP-style listening socket becomes readable when the accept queue
5293 * is not empty.
5295 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5296 return (!list_empty(&sp->ep->asocs)) ?
5297 (POLLIN | POLLRDNORM) : 0;
5299 mask = 0;
5301 /* Is there any exceptional events? */
5302 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5303 mask |= POLLERR;
5304 if (sk->sk_shutdown & RCV_SHUTDOWN)
5305 mask |= POLLRDHUP;
5306 if (sk->sk_shutdown == SHUTDOWN_MASK)
5307 mask |= POLLHUP;
5309 /* Is it readable? Reconsider this code with TCP-style support. */
5310 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5311 (sk->sk_shutdown & RCV_SHUTDOWN))
5312 mask |= POLLIN | POLLRDNORM;
5314 /* The association is either gone or not ready. */
5315 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5316 return mask;
5318 /* Is it writable? */
5319 if (sctp_writeable(sk)) {
5320 mask |= POLLOUT | POLLWRNORM;
5321 } else {
5322 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5324 * Since the socket is not locked, the buffer
5325 * might be made available after the writeable check and
5326 * before the bit is set. This could cause a lost I/O
5327 * signal. tcp_poll() has a race breaker for this race
5328 * condition. Based on their implementation, we put
5329 * in the following code to cover it as well.
5331 if (sctp_writeable(sk))
5332 mask |= POLLOUT | POLLWRNORM;
5334 return mask;
5337 /********************************************************************
5338 * 2nd Level Abstractions
5339 ********************************************************************/
5341 static struct sctp_bind_bucket *sctp_bucket_create(
5342 struct sctp_bind_hashbucket *head, unsigned short snum)
5344 struct sctp_bind_bucket *pp;
5346 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5347 SCTP_DBG_OBJCNT_INC(bind_bucket);
5348 if (pp) {
5349 pp->port = snum;
5350 pp->fastreuse = 0;
5351 INIT_HLIST_HEAD(&pp->owner);
5352 if ((pp->next = head->chain) != NULL)
5353 pp->next->pprev = &pp->next;
5354 head->chain = pp;
5355 pp->pprev = &head->chain;
5357 return pp;
5360 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5361 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5363 if (pp && hlist_empty(&pp->owner)) {
5364 if (pp->next)
5365 pp->next->pprev = pp->pprev;
5366 *(pp->pprev) = pp->next;
5367 kmem_cache_free(sctp_bucket_cachep, pp);
5368 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5372 /* Release this socket's reference to a local port. */
5373 static inline void __sctp_put_port(struct sock *sk)
5375 struct sctp_bind_hashbucket *head =
5376 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5377 struct sctp_bind_bucket *pp;
5379 sctp_spin_lock(&head->lock);
5380 pp = sctp_sk(sk)->bind_hash;
5381 __sk_del_bind_node(sk);
5382 sctp_sk(sk)->bind_hash = NULL;
5383 inet_sk(sk)->num = 0;
5384 sctp_bucket_destroy(pp);
5385 sctp_spin_unlock(&head->lock);
5388 void sctp_put_port(struct sock *sk)
5390 sctp_local_bh_disable();
5391 __sctp_put_port(sk);
5392 sctp_local_bh_enable();
5396 * The system picks an ephemeral port and choose an address set equivalent
5397 * to binding with a wildcard address.
5398 * One of those addresses will be the primary address for the association.
5399 * This automatically enables the multihoming capability of SCTP.
5401 static int sctp_autobind(struct sock *sk)
5403 union sctp_addr autoaddr;
5404 struct sctp_af *af;
5405 __be16 port;
5407 /* Initialize a local sockaddr structure to INADDR_ANY. */
5408 af = sctp_sk(sk)->pf->af;
5410 port = htons(inet_sk(sk)->num);
5411 af->inaddr_any(&autoaddr, port);
5413 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5416 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5418 * From RFC 2292
5419 * 4.2 The cmsghdr Structure *
5421 * When ancillary data is sent or received, any number of ancillary data
5422 * objects can be specified by the msg_control and msg_controllen members of
5423 * the msghdr structure, because each object is preceded by
5424 * a cmsghdr structure defining the object's length (the cmsg_len member).
5425 * Historically Berkeley-derived implementations have passed only one object
5426 * at a time, but this API allows multiple objects to be
5427 * passed in a single call to sendmsg() or recvmsg(). The following example
5428 * shows two ancillary data objects in a control buffer.
5430 * |<--------------------------- msg_controllen -------------------------->|
5431 * | |
5433 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5435 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5436 * | | |
5438 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5440 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5441 * | | | | |
5443 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5444 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5446 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5448 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5452 * msg_control
5453 * points here
5455 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5456 sctp_cmsgs_t *cmsgs)
5458 struct cmsghdr *cmsg;
5460 for (cmsg = CMSG_FIRSTHDR(msg);
5461 cmsg != NULL;
5462 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5463 if (!CMSG_OK(msg, cmsg))
5464 return -EINVAL;
5466 /* Should we parse this header or ignore? */
5467 if (cmsg->cmsg_level != IPPROTO_SCTP)
5468 continue;
5470 /* Strictly check lengths following example in SCM code. */
5471 switch (cmsg->cmsg_type) {
5472 case SCTP_INIT:
5473 /* SCTP Socket API Extension
5474 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5476 * This cmsghdr structure provides information for
5477 * initializing new SCTP associations with sendmsg().
5478 * The SCTP_INITMSG socket option uses this same data
5479 * structure. This structure is not used for
5480 * recvmsg().
5482 * cmsg_level cmsg_type cmsg_data[]
5483 * ------------ ------------ ----------------------
5484 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5486 if (cmsg->cmsg_len !=
5487 CMSG_LEN(sizeof(struct sctp_initmsg)))
5488 return -EINVAL;
5489 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5490 break;
5492 case SCTP_SNDRCV:
5493 /* SCTP Socket API Extension
5494 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5496 * This cmsghdr structure specifies SCTP options for
5497 * sendmsg() and describes SCTP header information
5498 * about a received message through recvmsg().
5500 * cmsg_level cmsg_type cmsg_data[]
5501 * ------------ ------------ ----------------------
5502 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5504 if (cmsg->cmsg_len !=
5505 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5506 return -EINVAL;
5508 cmsgs->info =
5509 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5511 /* Minimally, validate the sinfo_flags. */
5512 if (cmsgs->info->sinfo_flags &
5513 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5514 SCTP_ABORT | SCTP_EOF))
5515 return -EINVAL;
5516 break;
5518 default:
5519 return -EINVAL;
5522 return 0;
5526 * Wait for a packet..
5527 * Note: This function is the same function as in core/datagram.c
5528 * with a few modifications to make lksctp work.
5530 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5532 int error;
5533 DEFINE_WAIT(wait);
5535 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5537 /* Socket errors? */
5538 error = sock_error(sk);
5539 if (error)
5540 goto out;
5542 if (!skb_queue_empty(&sk->sk_receive_queue))
5543 goto ready;
5545 /* Socket shut down? */
5546 if (sk->sk_shutdown & RCV_SHUTDOWN)
5547 goto out;
5549 /* Sequenced packets can come disconnected. If so we report the
5550 * problem.
5552 error = -ENOTCONN;
5554 /* Is there a good reason to think that we may receive some data? */
5555 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5556 goto out;
5558 /* Handle signals. */
5559 if (signal_pending(current))
5560 goto interrupted;
5562 /* Let another process have a go. Since we are going to sleep
5563 * anyway. Note: This may cause odd behaviors if the message
5564 * does not fit in the user's buffer, but this seems to be the
5565 * only way to honor MSG_DONTWAIT realistically.
5567 sctp_release_sock(sk);
5568 *timeo_p = schedule_timeout(*timeo_p);
5569 sctp_lock_sock(sk);
5571 ready:
5572 finish_wait(sk->sk_sleep, &wait);
5573 return 0;
5575 interrupted:
5576 error = sock_intr_errno(*timeo_p);
5578 out:
5579 finish_wait(sk->sk_sleep, &wait);
5580 *err = error;
5581 return error;
5584 /* Receive a datagram.
5585 * Note: This is pretty much the same routine as in core/datagram.c
5586 * with a few changes to make lksctp work.
5588 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5589 int noblock, int *err)
5591 int error;
5592 struct sk_buff *skb;
5593 long timeo;
5595 timeo = sock_rcvtimeo(sk, noblock);
5597 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5598 timeo, MAX_SCHEDULE_TIMEOUT);
5600 do {
5601 /* Again only user level code calls this function,
5602 * so nothing interrupt level
5603 * will suddenly eat the receive_queue.
5605 * Look at current nfs client by the way...
5606 * However, this function was corrent in any case. 8)
5608 if (flags & MSG_PEEK) {
5609 spin_lock_bh(&sk->sk_receive_queue.lock);
5610 skb = skb_peek(&sk->sk_receive_queue);
5611 if (skb)
5612 atomic_inc(&skb->users);
5613 spin_unlock_bh(&sk->sk_receive_queue.lock);
5614 } else {
5615 skb = skb_dequeue(&sk->sk_receive_queue);
5618 if (skb)
5619 return skb;
5621 /* Caller is allowed not to check sk->sk_err before calling. */
5622 error = sock_error(sk);
5623 if (error)
5624 goto no_packet;
5626 if (sk->sk_shutdown & RCV_SHUTDOWN)
5627 break;
5629 /* User doesn't want to wait. */
5630 error = -EAGAIN;
5631 if (!timeo)
5632 goto no_packet;
5633 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5635 return NULL;
5637 no_packet:
5638 *err = error;
5639 return NULL;
5642 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5643 static void __sctp_write_space(struct sctp_association *asoc)
5645 struct sock *sk = asoc->base.sk;
5646 struct socket *sock = sk->sk_socket;
5648 if ((sctp_wspace(asoc) > 0) && sock) {
5649 if (waitqueue_active(&asoc->wait))
5650 wake_up_interruptible(&asoc->wait);
5652 if (sctp_writeable(sk)) {
5653 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5654 wake_up_interruptible(sk->sk_sleep);
5656 /* Note that we try to include the Async I/O support
5657 * here by modeling from the current TCP/UDP code.
5658 * We have not tested with it yet.
5660 if (sock->fasync_list &&
5661 !(sk->sk_shutdown & SEND_SHUTDOWN))
5662 sock_wake_async(sock, 2, POLL_OUT);
5667 /* Do accounting for the sndbuf space.
5668 * Decrement the used sndbuf space of the corresponding association by the
5669 * data size which was just transmitted(freed).
5671 static void sctp_wfree(struct sk_buff *skb)
5673 struct sctp_association *asoc;
5674 struct sctp_chunk *chunk;
5675 struct sock *sk;
5677 /* Get the saved chunk pointer. */
5678 chunk = *((struct sctp_chunk **)(skb->cb));
5679 asoc = chunk->asoc;
5680 sk = asoc->base.sk;
5681 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5682 sizeof(struct sk_buff) +
5683 sizeof(struct sctp_chunk);
5685 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5687 sock_wfree(skb);
5688 __sctp_write_space(asoc);
5690 sctp_association_put(asoc);
5693 /* Do accounting for the receive space on the socket.
5694 * Accounting for the association is done in ulpevent.c
5695 * We set this as a destructor for the cloned data skbs so that
5696 * accounting is done at the correct time.
5698 void sctp_sock_rfree(struct sk_buff *skb)
5700 struct sock *sk = skb->sk;
5701 struct sctp_ulpevent *event = sctp_skb2event(skb);
5703 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5707 /* Helper function to wait for space in the sndbuf. */
5708 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5709 size_t msg_len)
5711 struct sock *sk = asoc->base.sk;
5712 int err = 0;
5713 long current_timeo = *timeo_p;
5714 DEFINE_WAIT(wait);
5716 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5717 asoc, (long)(*timeo_p), msg_len);
5719 /* Increment the association's refcnt. */
5720 sctp_association_hold(asoc);
5722 /* Wait on the association specific sndbuf space. */
5723 for (;;) {
5724 prepare_to_wait_exclusive(&asoc->wait, &wait,
5725 TASK_INTERRUPTIBLE);
5726 if (!*timeo_p)
5727 goto do_nonblock;
5728 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5729 asoc->base.dead)
5730 goto do_error;
5731 if (signal_pending(current))
5732 goto do_interrupted;
5733 if (msg_len <= sctp_wspace(asoc))
5734 break;
5736 /* Let another process have a go. Since we are going
5737 * to sleep anyway.
5739 sctp_release_sock(sk);
5740 current_timeo = schedule_timeout(current_timeo);
5741 BUG_ON(sk != asoc->base.sk);
5742 sctp_lock_sock(sk);
5744 *timeo_p = current_timeo;
5747 out:
5748 finish_wait(&asoc->wait, &wait);
5750 /* Release the association's refcnt. */
5751 sctp_association_put(asoc);
5753 return err;
5755 do_error:
5756 err = -EPIPE;
5757 goto out;
5759 do_interrupted:
5760 err = sock_intr_errno(*timeo_p);
5761 goto out;
5763 do_nonblock:
5764 err = -EAGAIN;
5765 goto out;
5768 /* If socket sndbuf has changed, wake up all per association waiters. */
5769 void sctp_write_space(struct sock *sk)
5771 struct sctp_association *asoc;
5772 struct list_head *pos;
5774 /* Wake up the tasks in each wait queue. */
5775 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5776 asoc = list_entry(pos, struct sctp_association, asocs);
5777 __sctp_write_space(asoc);
5781 /* Is there any sndbuf space available on the socket?
5783 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5784 * associations on the same socket. For a UDP-style socket with
5785 * multiple associations, it is possible for it to be "unwriteable"
5786 * prematurely. I assume that this is acceptable because
5787 * a premature "unwriteable" is better than an accidental "writeable" which
5788 * would cause an unwanted block under certain circumstances. For the 1-1
5789 * UDP-style sockets or TCP-style sockets, this code should work.
5790 * - Daisy
5792 static int sctp_writeable(struct sock *sk)
5794 int amt = 0;
5796 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5797 if (amt < 0)
5798 amt = 0;
5799 return amt;
5802 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5803 * returns immediately with EINPROGRESS.
5805 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5807 struct sock *sk = asoc->base.sk;
5808 int err = 0;
5809 long current_timeo = *timeo_p;
5810 DEFINE_WAIT(wait);
5812 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5813 (long)(*timeo_p));
5815 /* Increment the association's refcnt. */
5816 sctp_association_hold(asoc);
5818 for (;;) {
5819 prepare_to_wait_exclusive(&asoc->wait, &wait,
5820 TASK_INTERRUPTIBLE);
5821 if (!*timeo_p)
5822 goto do_nonblock;
5823 if (sk->sk_shutdown & RCV_SHUTDOWN)
5824 break;
5825 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5826 asoc->base.dead)
5827 goto do_error;
5828 if (signal_pending(current))
5829 goto do_interrupted;
5831 if (sctp_state(asoc, ESTABLISHED))
5832 break;
5834 /* Let another process have a go. Since we are going
5835 * to sleep anyway.
5837 sctp_release_sock(sk);
5838 current_timeo = schedule_timeout(current_timeo);
5839 sctp_lock_sock(sk);
5841 *timeo_p = current_timeo;
5844 out:
5845 finish_wait(&asoc->wait, &wait);
5847 /* Release the association's refcnt. */
5848 sctp_association_put(asoc);
5850 return err;
5852 do_error:
5853 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5854 err = -ETIMEDOUT;
5855 else
5856 err = -ECONNREFUSED;
5857 goto out;
5859 do_interrupted:
5860 err = sock_intr_errno(*timeo_p);
5861 goto out;
5863 do_nonblock:
5864 err = -EINPROGRESS;
5865 goto out;
5868 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5870 struct sctp_endpoint *ep;
5871 int err = 0;
5872 DEFINE_WAIT(wait);
5874 ep = sctp_sk(sk)->ep;
5877 for (;;) {
5878 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5879 TASK_INTERRUPTIBLE);
5881 if (list_empty(&ep->asocs)) {
5882 sctp_release_sock(sk);
5883 timeo = schedule_timeout(timeo);
5884 sctp_lock_sock(sk);
5887 err = -EINVAL;
5888 if (!sctp_sstate(sk, LISTENING))
5889 break;
5891 err = 0;
5892 if (!list_empty(&ep->asocs))
5893 break;
5895 err = sock_intr_errno(timeo);
5896 if (signal_pending(current))
5897 break;
5899 err = -EAGAIN;
5900 if (!timeo)
5901 break;
5904 finish_wait(sk->sk_sleep, &wait);
5906 return err;
5909 void sctp_wait_for_close(struct sock *sk, long timeout)
5911 DEFINE_WAIT(wait);
5913 do {
5914 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5915 if (list_empty(&sctp_sk(sk)->ep->asocs))
5916 break;
5917 sctp_release_sock(sk);
5918 timeout = schedule_timeout(timeout);
5919 sctp_lock_sock(sk);
5920 } while (!signal_pending(current) && timeout);
5922 finish_wait(sk->sk_sleep, &wait);
5925 static void sctp_sock_rfree_frag(struct sk_buff *skb)
5927 struct sk_buff *frag;
5929 if (!skb->data_len)
5930 goto done;
5932 /* Don't forget the fragments. */
5933 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5934 sctp_sock_rfree_frag(frag);
5936 done:
5937 sctp_sock_rfree(skb);
5940 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5942 struct sk_buff *frag;
5944 if (!skb->data_len)
5945 goto done;
5947 /* Don't forget the fragments. */
5948 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5949 sctp_skb_set_owner_r_frag(frag, sk);
5951 done:
5952 sctp_skb_set_owner_r(skb, sk);
5955 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5956 * and its messages to the newsk.
5958 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5959 struct sctp_association *assoc,
5960 sctp_socket_type_t type)
5962 struct sctp_sock *oldsp = sctp_sk(oldsk);
5963 struct sctp_sock *newsp = sctp_sk(newsk);
5964 struct sctp_bind_bucket *pp; /* hash list port iterator */
5965 struct sctp_endpoint *newep = newsp->ep;
5966 struct sk_buff *skb, *tmp;
5967 struct sctp_ulpevent *event;
5968 int flags = 0;
5970 /* Migrate socket buffer sizes and all the socket level options to the
5971 * new socket.
5973 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5974 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5975 /* Brute force copy old sctp opt. */
5976 inet_sk_copy_descendant(newsk, oldsk);
5978 /* Restore the ep value that was overwritten with the above structure
5979 * copy.
5981 newsp->ep = newep;
5982 newsp->hmac = NULL;
5984 /* Hook this new socket in to the bind_hash list. */
5985 pp = sctp_sk(oldsk)->bind_hash;
5986 sk_add_bind_node(newsk, &pp->owner);
5987 sctp_sk(newsk)->bind_hash = pp;
5988 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5990 /* Copy the bind_addr list from the original endpoint to the new
5991 * endpoint so that we can handle restarts properly
5993 if (PF_INET6 == assoc->base.sk->sk_family)
5994 flags = SCTP_ADDR6_ALLOWED;
5995 if (assoc->peer.ipv4_address)
5996 flags |= SCTP_ADDR4_PEERSUPP;
5997 if (assoc->peer.ipv6_address)
5998 flags |= SCTP_ADDR6_PEERSUPP;
5999 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6000 &oldsp->ep->base.bind_addr,
6001 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6003 /* Move any messages in the old socket's receive queue that are for the
6004 * peeled off association to the new socket's receive queue.
6006 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6007 event = sctp_skb2event(skb);
6008 if (event->asoc == assoc) {
6009 sctp_sock_rfree_frag(skb);
6010 __skb_unlink(skb, &oldsk->sk_receive_queue);
6011 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6012 sctp_skb_set_owner_r_frag(skb, newsk);
6016 /* Clean up any messages pending delivery due to partial
6017 * delivery. Three cases:
6018 * 1) No partial deliver; no work.
6019 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6020 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6022 skb_queue_head_init(&newsp->pd_lobby);
6023 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6025 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6026 struct sk_buff_head *queue;
6028 /* Decide which queue to move pd_lobby skbs to. */
6029 if (assoc->ulpq.pd_mode) {
6030 queue = &newsp->pd_lobby;
6031 } else
6032 queue = &newsk->sk_receive_queue;
6034 /* Walk through the pd_lobby, looking for skbs that
6035 * need moved to the new socket.
6037 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6038 event = sctp_skb2event(skb);
6039 if (event->asoc == assoc) {
6040 sctp_sock_rfree_frag(skb);
6041 __skb_unlink(skb, &oldsp->pd_lobby);
6042 __skb_queue_tail(queue, skb);
6043 sctp_skb_set_owner_r_frag(skb, newsk);
6047 /* Clear up any skbs waiting for the partial
6048 * delivery to finish.
6050 if (assoc->ulpq.pd_mode)
6051 sctp_clear_pd(oldsk, NULL);
6055 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6056 sctp_sock_rfree_frag(skb);
6057 sctp_skb_set_owner_r_frag(skb, newsk);
6060 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6061 sctp_sock_rfree_frag(skb);
6062 sctp_skb_set_owner_r_frag(skb, newsk);
6065 /* Set the type of socket to indicate that it is peeled off from the
6066 * original UDP-style socket or created with the accept() call on a
6067 * TCP-style socket..
6069 newsp->type = type;
6071 /* Mark the new socket "in-use" by the user so that any packets
6072 * that may arrive on the association after we've moved it are
6073 * queued to the backlog. This prevents a potential race between
6074 * backlog processing on the old socket and new-packet processing
6075 * on the new socket.
6077 sctp_lock_sock(newsk);
6078 sctp_assoc_migrate(assoc, newsk);
6080 /* If the association on the newsk is already closed before accept()
6081 * is called, set RCV_SHUTDOWN flag.
6083 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6084 newsk->sk_shutdown |= RCV_SHUTDOWN;
6086 newsk->sk_state = SCTP_SS_ESTABLISHED;
6087 sctp_release_sock(newsk);
6090 /* This proto struct describes the ULP interface for SCTP. */
6091 struct proto sctp_prot = {
6092 .name = "SCTP",
6093 .owner = THIS_MODULE,
6094 .close = sctp_close,
6095 .connect = sctp_connect,
6096 .disconnect = sctp_disconnect,
6097 .accept = sctp_accept,
6098 .ioctl = sctp_ioctl,
6099 .init = sctp_init_sock,
6100 .destroy = sctp_destroy_sock,
6101 .shutdown = sctp_shutdown,
6102 .setsockopt = sctp_setsockopt,
6103 .getsockopt = sctp_getsockopt,
6104 .sendmsg = sctp_sendmsg,
6105 .recvmsg = sctp_recvmsg,
6106 .bind = sctp_bind,
6107 .backlog_rcv = sctp_backlog_rcv,
6108 .hash = sctp_hash,
6109 .unhash = sctp_unhash,
6110 .get_port = sctp_get_port,
6111 .obj_size = sizeof(struct sctp_sock),
6114 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6115 struct proto sctpv6_prot = {
6116 .name = "SCTPv6",
6117 .owner = THIS_MODULE,
6118 .close = sctp_close,
6119 .connect = sctp_connect,
6120 .disconnect = sctp_disconnect,
6121 .accept = sctp_accept,
6122 .ioctl = sctp_ioctl,
6123 .init = sctp_init_sock,
6124 .destroy = sctp_destroy_sock,
6125 .shutdown = sctp_shutdown,
6126 .setsockopt = sctp_setsockopt,
6127 .getsockopt = sctp_getsockopt,
6128 .sendmsg = sctp_sendmsg,
6129 .recvmsg = sctp_recvmsg,
6130 .bind = sctp_bind,
6131 .backlog_rcv = sctp_backlog_rcv,
6132 .hash = sctp_hash,
6133 .unhash = sctp_unhash,
6134 .get_port = sctp_get_port,
6135 .obj_size = sizeof(struct sctp6_sock),
6137 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */