allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / sctp / socket.c
blob61f705cdae2d14c1d9e729706e27b2cbf387a6f2
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 - sk_wmem_alloc_get(sk);
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 * It's OK to allow 0 ports if we are already bound.
338 * We'll just inhert an already bound port in this case
340 if (bp->port) {
341 if (!snum)
342 snum = bp->port;
343 else if (snum != bp->port) {
344 SCTP_DEBUG_PRINTK("sctp_do_bind:"
345 " New port %d does not match existing port "
346 "%d.\n", snum, bp->port);
347 return -EINVAL;
351 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
352 return -EACCES;
354 /* Make sure we are allowed to bind here.
355 * The function sctp_get_port_local() does duplicate address
356 * detection.
358 if ((ret = sctp_get_port_local(sk, addr))) {
359 if (ret == (long) sk) {
360 /* This endpoint has a conflicting address. */
361 return -EINVAL;
362 } else {
363 return -EADDRINUSE;
367 /* Refresh ephemeral port. */
368 if (!bp->port)
369 bp->port = inet_sk(sk)->num;
371 /* Add the address to the bind address list. */
372 sctp_local_bh_disable();
373 sctp_write_lock(&ep->base.addr_lock);
375 /* Use GFP_ATOMIC since BHs are disabled. */
376 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
377 sctp_write_unlock(&ep->base.addr_lock);
378 sctp_local_bh_enable();
380 /* Copy back into socket for getsockname() use. */
381 if (!ret) {
382 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
383 af->to_sk_saddr(addr, sk);
386 return ret;
389 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
391 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
392 * at any one time. If a sender, after sending an ASCONF chunk, decides
393 * it needs to transfer another ASCONF Chunk, it MUST wait until the
394 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
395 * subsequent ASCONF. Note this restriction binds each side, so at any
396 * time two ASCONF may be in-transit on any given association (one sent
397 * from each endpoint).
399 static int sctp_send_asconf(struct sctp_association *asoc,
400 struct sctp_chunk *chunk)
402 int retval = 0;
404 /* If there is an outstanding ASCONF chunk, queue it for later
405 * transmission.
407 if (asoc->addip_last_asconf) {
408 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
409 goto out;
412 /* Hold the chunk until an ASCONF_ACK is received. */
413 sctp_chunk_hold(chunk);
414 retval = sctp_primitive_ASCONF(asoc, chunk);
415 if (retval)
416 sctp_chunk_free(chunk);
417 else
418 asoc->addip_last_asconf = chunk;
420 out:
421 return retval;
424 /* Add a list of addresses as bind addresses to local endpoint or
425 * association.
427 * Basically run through each address specified in the addrs/addrcnt
428 * array/length pair, determine if it is IPv6 or IPv4 and call
429 * sctp_do_bind() on it.
431 * If any of them fails, then the operation will be reversed and the
432 * ones that were added will be removed.
434 * Only sctp_setsockopt_bindx() is supposed to call this function.
436 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
438 int cnt;
439 int retval = 0;
440 void *addr_buf;
441 struct sockaddr *sa_addr;
442 struct sctp_af *af;
444 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
445 sk, addrs, addrcnt);
447 addr_buf = addrs;
448 for (cnt = 0; cnt < addrcnt; cnt++) {
449 /* The list may contain either IPv4 or IPv6 address;
450 * determine the address length for walking thru the list.
452 sa_addr = (struct sockaddr *)addr_buf;
453 af = sctp_get_af_specific(sa_addr->sa_family);
454 if (!af) {
455 retval = -EINVAL;
456 goto err_bindx_add;
459 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
460 af->sockaddr_len);
462 addr_buf += af->sockaddr_len;
464 err_bindx_add:
465 if (retval < 0) {
466 /* Failed. Cleanup the ones that have been added */
467 if (cnt > 0)
468 sctp_bindx_rem(sk, addrs, cnt);
469 return retval;
473 return retval;
476 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
477 * associations that are part of the endpoint indicating that a list of local
478 * addresses are added to the endpoint.
480 * If any of the addresses is already in the bind address list of the
481 * association, we do not send the chunk for that association. But it will not
482 * affect other associations.
484 * Only sctp_setsockopt_bindx() is supposed to call this function.
486 static int sctp_send_asconf_add_ip(struct sock *sk,
487 struct sockaddr *addrs,
488 int addrcnt)
490 struct sctp_sock *sp;
491 struct sctp_endpoint *ep;
492 struct sctp_association *asoc;
493 struct sctp_bind_addr *bp;
494 struct sctp_chunk *chunk;
495 struct sctp_sockaddr_entry *laddr;
496 union sctp_addr *addr;
497 union sctp_addr saveaddr;
498 void *addr_buf;
499 struct sctp_af *af;
500 struct list_head *pos;
501 struct list_head *p;
502 int i;
503 int retval = 0;
505 if (!sctp_addip_enable)
506 return retval;
508 sp = sctp_sk(sk);
509 ep = sp->ep;
511 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
512 __FUNCTION__, sk, addrs, addrcnt);
514 list_for_each(pos, &ep->asocs) {
515 asoc = list_entry(pos, struct sctp_association, asocs);
517 if (!asoc->peer.asconf_capable)
518 continue;
520 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
521 continue;
523 if (!sctp_state(asoc, ESTABLISHED))
524 continue;
526 /* Check if any address in the packed array of addresses is
527 * in the bind address list of the association. If so,
528 * do not send the asconf chunk to its peer, but continue with
529 * other associations.
531 addr_buf = addrs;
532 for (i = 0; i < addrcnt; i++) {
533 addr = (union sctp_addr *)addr_buf;
534 af = sctp_get_af_specific(addr->v4.sin_family);
535 if (!af) {
536 retval = -EINVAL;
537 goto out;
540 if (sctp_assoc_lookup_laddr(asoc, addr))
541 break;
543 addr_buf += af->sockaddr_len;
545 if (i < addrcnt)
546 continue;
548 /* Use the first address in bind addr list of association as
549 * Address Parameter of ASCONF CHUNK.
551 sctp_read_lock(&asoc->base.addr_lock);
552 bp = &asoc->base.bind_addr;
553 p = bp->address_list.next;
554 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
555 sctp_read_unlock(&asoc->base.addr_lock);
557 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
558 addrcnt, SCTP_PARAM_ADD_IP);
559 if (!chunk) {
560 retval = -ENOMEM;
561 goto out;
564 retval = sctp_send_asconf(asoc, chunk);
565 if (retval)
566 goto out;
568 /* Add the new addresses to the bind address list with
569 * use_as_src set to 0.
571 sctp_local_bh_disable();
572 sctp_write_lock(&asoc->base.addr_lock);
573 addr_buf = addrs;
574 for (i = 0; i < addrcnt; i++) {
575 addr = (union sctp_addr *)addr_buf;
576 af = sctp_get_af_specific(addr->v4.sin_family);
577 memcpy(&saveaddr, addr, af->sockaddr_len);
578 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
579 GFP_ATOMIC);
580 addr_buf += af->sockaddr_len;
582 sctp_write_unlock(&asoc->base.addr_lock);
583 sctp_local_bh_enable();
586 out:
587 return retval;
590 /* Remove a list of addresses from bind addresses list. Do not remove the
591 * last address.
593 * Basically run through each address specified in the addrs/addrcnt
594 * array/length pair, determine if it is IPv6 or IPv4 and call
595 * sctp_del_bind() on it.
597 * If any of them fails, then the operation will be reversed and the
598 * ones that were removed will be added back.
600 * At least one address has to be left; if only one address is
601 * available, the operation will return -EBUSY.
603 * Only sctp_setsockopt_bindx() is supposed to call this function.
605 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
607 struct sctp_sock *sp = sctp_sk(sk);
608 struct sctp_endpoint *ep = sp->ep;
609 int cnt;
610 struct sctp_bind_addr *bp = &ep->base.bind_addr;
611 int retval = 0;
612 void *addr_buf;
613 union sctp_addr *sa_addr;
614 struct sctp_af *af;
616 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
617 sk, addrs, addrcnt);
619 addr_buf = addrs;
620 for (cnt = 0; cnt < addrcnt; cnt++) {
621 /* If the bind address list is empty or if there is only one
622 * bind address, there is nothing more to be removed (we need
623 * at least one address here).
625 if (list_empty(&bp->address_list) ||
626 (sctp_list_single_entry(&bp->address_list))) {
627 retval = -EBUSY;
628 goto err_bindx_rem;
631 sa_addr = (union sctp_addr *)addr_buf;
632 af = sctp_get_af_specific(sa_addr->sa.sa_family);
633 if (!af) {
634 retval = -EINVAL;
635 goto err_bindx_rem;
638 if (!af->addr_valid(sa_addr, sp, NULL)) {
639 retval = -EADDRNOTAVAIL;
640 goto err_bindx_rem;
643 if (sa_addr->v4.sin_port != htons(bp->port)) {
644 retval = -EINVAL;
645 goto err_bindx_rem;
648 /* FIXME - There is probably a need to check if sk->sk_saddr and
649 * sk->sk_rcv_addr are currently set to one of the addresses to
650 * be removed. This is something which needs to be looked into
651 * when we are fixing the outstanding issues with multi-homing
652 * socket routing and failover schemes. Refer to comments in
653 * sctp_do_bind(). -daisy
655 sctp_local_bh_disable();
656 sctp_write_lock(&ep->base.addr_lock);
658 retval = sctp_del_bind_addr(bp, sa_addr);
660 sctp_write_unlock(&ep->base.addr_lock);
661 sctp_local_bh_enable();
663 addr_buf += af->sockaddr_len;
664 err_bindx_rem:
665 if (retval < 0) {
666 /* Failed. Add the ones that has been removed back */
667 if (cnt > 0)
668 sctp_bindx_add(sk, addrs, cnt);
669 return retval;
673 return retval;
676 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
677 * the associations that are part of the endpoint indicating that a list of
678 * local addresses are removed from the endpoint.
680 * If any of the addresses is already in the bind address list of the
681 * association, we do not send the chunk for that association. But it will not
682 * affect other associations.
684 * Only sctp_setsockopt_bindx() is supposed to call this function.
686 static int sctp_send_asconf_del_ip(struct sock *sk,
687 struct sockaddr *addrs,
688 int addrcnt)
690 struct sctp_sock *sp;
691 struct sctp_endpoint *ep;
692 struct sctp_association *asoc;
693 struct sctp_transport *transport;
694 struct sctp_bind_addr *bp;
695 struct sctp_chunk *chunk;
696 union sctp_addr *laddr;
697 void *addr_buf;
698 struct sctp_af *af;
699 struct list_head *pos, *pos1;
700 struct sctp_sockaddr_entry *saddr;
701 int i;
702 int retval = 0;
704 if (!sctp_addip_enable)
705 return retval;
707 sp = sctp_sk(sk);
708 ep = sp->ep;
710 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
711 __FUNCTION__, sk, addrs, addrcnt);
713 list_for_each(pos, &ep->asocs) {
714 asoc = list_entry(pos, struct sctp_association, asocs);
716 if (!asoc->peer.asconf_capable)
717 continue;
719 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
720 continue;
722 if (!sctp_state(asoc, ESTABLISHED))
723 continue;
725 /* Check if any address in the packed array of addresses is
726 * not present in the bind address list of the association.
727 * If so, do not send the asconf chunk to its peer, but
728 * continue with other associations.
730 addr_buf = addrs;
731 for (i = 0; i < addrcnt; i++) {
732 laddr = (union sctp_addr *)addr_buf;
733 af = sctp_get_af_specific(laddr->v4.sin_family);
734 if (!af) {
735 retval = -EINVAL;
736 goto out;
739 if (!sctp_assoc_lookup_laddr(asoc, laddr))
740 break;
742 addr_buf += af->sockaddr_len;
744 if (i < addrcnt)
745 continue;
747 /* Find one address in the association's bind address list
748 * that is not in the packed array of addresses. This is to
749 * make sure that we do not delete all the addresses in the
750 * association.
752 sctp_read_lock(&asoc->base.addr_lock);
753 bp = &asoc->base.bind_addr;
754 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
755 addrcnt, sp);
756 sctp_read_unlock(&asoc->base.addr_lock);
757 if (!laddr)
758 continue;
760 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
761 SCTP_PARAM_DEL_IP);
762 if (!chunk) {
763 retval = -ENOMEM;
764 goto out;
767 /* Reset use_as_src flag for the addresses in the bind address
768 * list that are to be deleted.
770 sctp_local_bh_disable();
771 sctp_write_lock(&asoc->base.addr_lock);
772 addr_buf = addrs;
773 for (i = 0; i < addrcnt; i++) {
774 laddr = (union sctp_addr *)addr_buf;
775 af = sctp_get_af_specific(laddr->v4.sin_family);
776 list_for_each(pos1, &bp->address_list) {
777 saddr = list_entry(pos1,
778 struct sctp_sockaddr_entry,
779 list);
780 if (sctp_cmp_addr_exact(&saddr->a, laddr))
781 saddr->use_as_src = 0;
783 addr_buf += af->sockaddr_len;
785 sctp_write_unlock(&asoc->base.addr_lock);
786 sctp_local_bh_enable();
788 /* Update the route and saddr entries for all the transports
789 * as some of the addresses in the bind address list are
790 * about to be deleted and cannot be used as source addresses.
792 list_for_each(pos1, &asoc->peer.transport_addr_list) {
793 transport = list_entry(pos1, struct sctp_transport,
794 transports);
795 dst_release(transport->dst);
796 sctp_transport_route(transport, NULL,
797 sctp_sk(asoc->base.sk));
800 retval = sctp_send_asconf(asoc, chunk);
802 out:
803 return retval;
806 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
808 * API 8.1
809 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
810 * int flags);
812 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
813 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
814 * or IPv6 addresses.
816 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
817 * Section 3.1.2 for this usage.
819 * addrs is a pointer to an array of one or more socket addresses. Each
820 * address is contained in its appropriate structure (i.e. struct
821 * sockaddr_in or struct sockaddr_in6) the family of the address type
822 * must be used to distinguish the address length (note that this
823 * representation is termed a "packed array" of addresses). The caller
824 * specifies the number of addresses in the array with addrcnt.
826 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
827 * -1, and sets errno to the appropriate error code.
829 * For SCTP, the port given in each socket address must be the same, or
830 * sctp_bindx() will fail, setting errno to EINVAL.
832 * The flags parameter is formed from the bitwise OR of zero or more of
833 * the following currently defined flags:
835 * SCTP_BINDX_ADD_ADDR
837 * SCTP_BINDX_REM_ADDR
839 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
840 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
841 * addresses from the association. The two flags are mutually exclusive;
842 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
843 * not remove all addresses from an association; sctp_bindx() will
844 * reject such an attempt with EINVAL.
846 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
847 * additional addresses with an endpoint after calling bind(). Or use
848 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
849 * socket is associated with so that no new association accepted will be
850 * associated with those addresses. If the endpoint supports dynamic
851 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
852 * endpoint to send the appropriate message to the peer to change the
853 * peers address lists.
855 * Adding and removing addresses from a connected association is
856 * optional functionality. Implementations that do not support this
857 * functionality should return EOPNOTSUPP.
859 * Basically do nothing but copying the addresses from user to kernel
860 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
861 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
862 * from userspace.
864 * We don't use copy_from_user() for optimization: we first do the
865 * sanity checks (buffer size -fast- and access check-healthy
866 * pointer); if all of those succeed, then we can alloc the memory
867 * (expensive operation) needed to copy the data to kernel. Then we do
868 * the copying without checking the user space area
869 * (__copy_from_user()).
871 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
872 * it.
874 * sk The sk of the socket
875 * addrs The pointer to the addresses in user land
876 * addrssize Size of the addrs buffer
877 * op Operation to perform (add or remove, see the flags of
878 * sctp_bindx)
880 * Returns 0 if ok, <0 errno code on error.
882 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
883 struct sockaddr __user *addrs,
884 int addrs_size, int op)
886 struct sockaddr *kaddrs;
887 int err;
888 int addrcnt = 0;
889 int walk_size = 0;
890 struct sockaddr *sa_addr;
891 void *addr_buf;
892 struct sctp_af *af;
894 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
895 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
897 if (unlikely(addrs_size <= 0))
898 return -EINVAL;
900 /* Check the user passed a healthy pointer. */
901 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
902 return -EFAULT;
904 /* Alloc space for the address array in kernel memory. */
905 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
906 if (unlikely(!kaddrs))
907 return -ENOMEM;
909 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
910 kfree(kaddrs);
911 return -EFAULT;
914 /* Walk through the addrs buffer and count the number of addresses. */
915 addr_buf = kaddrs;
916 while (walk_size < addrs_size) {
917 sa_addr = (struct sockaddr *)addr_buf;
918 af = sctp_get_af_specific(sa_addr->sa_family);
920 /* If the address family is not supported or if this address
921 * causes the address buffer to overflow return EINVAL.
923 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
924 kfree(kaddrs);
925 return -EINVAL;
927 addrcnt++;
928 addr_buf += af->sockaddr_len;
929 walk_size += af->sockaddr_len;
932 /* Do the work. */
933 switch (op) {
934 case SCTP_BINDX_ADD_ADDR:
935 err = sctp_bindx_add(sk, kaddrs, addrcnt);
936 if (err)
937 goto out;
938 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
939 break;
941 case SCTP_BINDX_REM_ADDR:
942 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
943 if (err)
944 goto out;
945 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
946 break;
948 default:
949 err = -EINVAL;
950 break;
953 out:
954 kfree(kaddrs);
956 return err;
959 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
961 * Common routine for handling connect() and sctp_connectx().
962 * Connect will come in with just a single address.
964 static int __sctp_connect(struct sock* sk,
965 struct sockaddr *kaddrs,
966 int addrs_size)
968 struct sctp_sock *sp;
969 struct sctp_endpoint *ep;
970 struct sctp_association *asoc = NULL;
971 struct sctp_association *asoc2;
972 struct sctp_transport *transport;
973 union sctp_addr to;
974 struct sctp_af *af;
975 sctp_scope_t scope;
976 long timeo;
977 int err = 0;
978 int addrcnt = 0;
979 int walk_size = 0;
980 union sctp_addr *sa_addr;
981 void *addr_buf;
982 unsigned short port;
983 unsigned int f_flags = 0;
985 sp = sctp_sk(sk);
986 ep = sp->ep;
988 /* connect() cannot be done on a socket that is already in ESTABLISHED
989 * state - UDP-style peeled off socket or a TCP-style socket that
990 * is already connected.
991 * It cannot be done even on a TCP-style listening socket.
993 if (sctp_sstate(sk, ESTABLISHED) ||
994 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
995 err = -EISCONN;
996 goto out_free;
999 /* Walk through the addrs buffer and count the number of addresses. */
1000 addr_buf = kaddrs;
1001 while (walk_size < addrs_size) {
1002 sa_addr = (union sctp_addr *)addr_buf;
1003 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1004 port = ntohs(sa_addr->v4.sin_port);
1006 /* If the address family is not supported or if this address
1007 * causes the address buffer to overflow return EINVAL.
1009 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1010 err = -EINVAL;
1011 goto out_free;
1014 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
1015 if (err)
1016 goto out_free;
1018 /* Make sure the destination port is correctly set
1019 * in all addresses.
1021 if (asoc && asoc->peer.port && asoc->peer.port != port)
1022 goto out_free;
1024 memcpy(&to, sa_addr, af->sockaddr_len);
1026 /* Check if there already is a matching association on the
1027 * endpoint (other than the one created here).
1029 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
1030 if (asoc2 && asoc2 != asoc) {
1031 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1032 err = -EISCONN;
1033 else
1034 err = -EALREADY;
1035 goto out_free;
1038 /* If we could not find a matching association on the endpoint,
1039 * make sure that there is no peeled-off association matching
1040 * the peer address even on another socket.
1042 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
1043 err = -EADDRNOTAVAIL;
1044 goto out_free;
1047 if (!asoc) {
1048 /* If a bind() or sctp_bindx() is not called prior to
1049 * an sctp_connectx() call, the system picks an
1050 * ephemeral port and will choose an address set
1051 * equivalent to binding with a wildcard address.
1053 if (!ep->base.bind_addr.port) {
1054 if (sctp_autobind(sk)) {
1055 err = -EAGAIN;
1056 goto out_free;
1058 } else {
1060 * If an unprivileged user inherits a 1-many
1061 * style socket with open associations on a
1062 * privileged port, it MAY be permitted to
1063 * accept new associations, but it SHOULD NOT
1064 * be permitted to open new associations.
1066 if (ep->base.bind_addr.port < PROT_SOCK &&
1067 !capable(CAP_NET_BIND_SERVICE)) {
1068 err = -EACCES;
1069 goto out_free;
1073 scope = sctp_scope(sa_addr);
1074 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1075 if (!asoc) {
1076 err = -ENOMEM;
1077 goto out_free;
1081 /* Prime the peer's transport structures. */
1082 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL,
1083 SCTP_UNKNOWN);
1084 if (!transport) {
1085 err = -ENOMEM;
1086 goto out_free;
1089 addrcnt++;
1090 addr_buf += af->sockaddr_len;
1091 walk_size += af->sockaddr_len;
1094 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1095 if (err < 0) {
1096 goto out_free;
1099 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1100 if (err < 0) {
1101 goto out_free;
1104 /* Initialize sk's dport and daddr for getpeername() */
1105 inet_sk(sk)->dport = htons(asoc->peer.port);
1106 af = sctp_get_af_specific(to.sa.sa_family);
1107 af->to_sk_daddr(&to, sk);
1108 sk->sk_err = 0;
1110 /* in-kernel sockets don't generally have a file allocated to them
1111 * if all they do is call sock_create_kern().
1113 if (sk->sk_socket->file)
1114 f_flags = sk->sk_socket->file->f_flags;
1116 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1118 err = sctp_wait_for_connect(asoc, &timeo);
1120 /* Don't free association on exit. */
1121 asoc = NULL;
1123 out_free:
1125 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1126 " kaddrs: %p err: %d\n",
1127 asoc, kaddrs, err);
1128 if (asoc)
1129 sctp_association_free(asoc);
1130 return err;
1133 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1135 * API 8.9
1136 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1138 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1139 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1140 * or IPv6 addresses.
1142 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1143 * Section 3.1.2 for this usage.
1145 * addrs is a pointer to an array of one or more socket addresses. Each
1146 * address is contained in its appropriate structure (i.e. struct
1147 * sockaddr_in or struct sockaddr_in6) the family of the address type
1148 * must be used to distengish the address length (note that this
1149 * representation is termed a "packed array" of addresses). The caller
1150 * specifies the number of addresses in the array with addrcnt.
1152 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1153 * -1, and sets errno to the appropriate error code.
1155 * For SCTP, the port given in each socket address must be the same, or
1156 * sctp_connectx() will fail, setting errno to EINVAL.
1158 * An application can use sctp_connectx to initiate an association with
1159 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1160 * allows a caller to specify multiple addresses at which a peer can be
1161 * reached. The way the SCTP stack uses the list of addresses to set up
1162 * the association is implementation dependant. This function only
1163 * specifies that the stack will try to make use of all the addresses in
1164 * the list when needed.
1166 * Note that the list of addresses passed in is only used for setting up
1167 * the association. It does not necessarily equal the set of addresses
1168 * the peer uses for the resulting association. If the caller wants to
1169 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1170 * retrieve them after the association has been set up.
1172 * Basically do nothing but copying the addresses from user to kernel
1173 * land and invoking either sctp_connectx(). This is used for tunneling
1174 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1176 * We don't use copy_from_user() for optimization: we first do the
1177 * sanity checks (buffer size -fast- and access check-healthy
1178 * pointer); if all of those succeed, then we can alloc the memory
1179 * (expensive operation) needed to copy the data to kernel. Then we do
1180 * the copying without checking the user space area
1181 * (__copy_from_user()).
1183 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1184 * it.
1186 * sk The sk of the socket
1187 * addrs The pointer to the addresses in user land
1188 * addrssize Size of the addrs buffer
1190 * Returns 0 if ok, <0 errno code on error.
1192 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1193 struct sockaddr __user *addrs,
1194 int addrs_size)
1196 int err = 0;
1197 struct sockaddr *kaddrs;
1199 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1200 __FUNCTION__, sk, addrs, addrs_size);
1202 if (unlikely(addrs_size <= 0))
1203 return -EINVAL;
1205 /* Check the user passed a healthy pointer. */
1206 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1207 return -EFAULT;
1209 /* Alloc space for the address array in kernel memory. */
1210 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1211 if (unlikely(!kaddrs))
1212 return -ENOMEM;
1214 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1215 err = -EFAULT;
1216 } else {
1217 err = __sctp_connect(sk, kaddrs, addrs_size);
1220 kfree(kaddrs);
1221 return err;
1224 /* API 3.1.4 close() - UDP Style Syntax
1225 * Applications use close() to perform graceful shutdown (as described in
1226 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1227 * by a UDP-style socket.
1229 * The syntax is
1231 * ret = close(int sd);
1233 * sd - the socket descriptor of the associations to be closed.
1235 * To gracefully shutdown a specific association represented by the
1236 * UDP-style socket, an application should use the sendmsg() call,
1237 * passing no user data, but including the appropriate flag in the
1238 * ancillary data (see Section xxxx).
1240 * If sd in the close() call is a branched-off socket representing only
1241 * one association, the shutdown is performed on that association only.
1243 * 4.1.6 close() - TCP Style Syntax
1245 * Applications use close() to gracefully close down an association.
1247 * The syntax is:
1249 * int close(int sd);
1251 * sd - the socket descriptor of the association to be closed.
1253 * After an application calls close() on a socket descriptor, no further
1254 * socket operations will succeed on that descriptor.
1256 * API 7.1.4 SO_LINGER
1258 * An application using the TCP-style socket can use this option to
1259 * perform the SCTP ABORT primitive. The linger option structure is:
1261 * struct linger {
1262 * int l_onoff; // option on/off
1263 * int l_linger; // linger time
1264 * };
1266 * To enable the option, set l_onoff to 1. If the l_linger value is set
1267 * to 0, calling close() is the same as the ABORT primitive. If the
1268 * value is set to a negative value, the setsockopt() call will return
1269 * an error. If the value is set to a positive value linger_time, the
1270 * close() can be blocked for at most linger_time ms. If the graceful
1271 * shutdown phase does not finish during this period, close() will
1272 * return but the graceful shutdown phase continues in the system.
1274 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1276 struct sctp_endpoint *ep;
1277 struct sctp_association *asoc;
1278 struct list_head *pos, *temp;
1280 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1282 sctp_lock_sock(sk);
1283 sk->sk_shutdown = SHUTDOWN_MASK;
1285 ep = sctp_sk(sk)->ep;
1287 /* Walk all associations on an endpoint. */
1288 list_for_each_safe(pos, temp, &ep->asocs) {
1289 asoc = list_entry(pos, struct sctp_association, asocs);
1291 if (sctp_style(sk, TCP)) {
1292 /* A closed association can still be in the list if
1293 * it belongs to a TCP-style listening socket that is
1294 * not yet accepted. If so, free it. If not, send an
1295 * ABORT or SHUTDOWN based on the linger options.
1297 if (sctp_state(asoc, CLOSED)) {
1298 sctp_unhash_established(asoc);
1299 sctp_association_free(asoc);
1300 continue;
1304 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1305 struct sctp_chunk *chunk;
1307 chunk = sctp_make_abort_user(asoc, NULL, 0);
1308 if (chunk)
1309 sctp_primitive_ABORT(asoc, chunk);
1310 } else
1311 sctp_primitive_SHUTDOWN(asoc, NULL);
1314 /* Clean up any skbs sitting on the receive queue. */
1315 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1316 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1318 /* On a TCP-style socket, block for at most linger_time if set. */
1319 if (sctp_style(sk, TCP) && timeout)
1320 sctp_wait_for_close(sk, timeout);
1322 /* This will run the backlog queue. */
1323 sctp_release_sock(sk);
1325 /* Supposedly, no process has access to the socket, but
1326 * the net layers still may.
1328 sctp_local_bh_disable();
1329 sctp_bh_lock_sock(sk);
1331 /* Hold the sock, since sk_common_release() will put sock_put()
1332 * and we have just a little more cleanup.
1334 sock_hold(sk);
1335 sk_common_release(sk);
1337 sctp_bh_unlock_sock(sk);
1338 sctp_local_bh_enable();
1340 sock_put(sk);
1342 SCTP_DBG_OBJCNT_DEC(sock);
1345 /* Handle EPIPE error. */
1346 static int sctp_error(struct sock *sk, int flags, int err)
1348 if (err == -EPIPE)
1349 err = sock_error(sk) ? : -EPIPE;
1350 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1351 send_sig(SIGPIPE, current, 0);
1352 return err;
1355 /* API 3.1.3 sendmsg() - UDP Style Syntax
1357 * An application uses sendmsg() and recvmsg() calls to transmit data to
1358 * and receive data from its peer.
1360 * ssize_t sendmsg(int socket, const struct msghdr *message,
1361 * int flags);
1363 * socket - the socket descriptor of the endpoint.
1364 * message - pointer to the msghdr structure which contains a single
1365 * user message and possibly some ancillary data.
1367 * See Section 5 for complete description of the data
1368 * structures.
1370 * flags - flags sent or received with the user message, see Section
1371 * 5 for complete description of the flags.
1373 * Note: This function could use a rewrite especially when explicit
1374 * connect support comes in.
1376 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1378 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1380 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1381 struct msghdr *msg, size_t msg_len)
1383 struct sctp_sock *sp;
1384 struct sctp_endpoint *ep;
1385 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1386 struct sctp_transport *transport, *chunk_tp;
1387 struct sctp_chunk *chunk;
1388 union sctp_addr to;
1389 struct sockaddr *msg_name = NULL;
1390 struct sctp_sndrcvinfo default_sinfo = { 0 };
1391 struct sctp_sndrcvinfo *sinfo;
1392 struct sctp_initmsg *sinit;
1393 sctp_assoc_t associd = 0;
1394 sctp_cmsgs_t cmsgs = { NULL };
1395 int err;
1396 sctp_scope_t scope;
1397 long timeo;
1398 __u16 sinfo_flags = 0;
1399 struct sctp_datamsg *datamsg;
1400 struct list_head *pos;
1401 int msg_flags = msg->msg_flags;
1403 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1404 sk, msg, msg_len);
1406 err = 0;
1407 sp = sctp_sk(sk);
1408 ep = sp->ep;
1410 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1412 /* We cannot send a message over a TCP-style listening socket. */
1413 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1414 err = -EPIPE;
1415 goto out_nounlock;
1418 /* Parse out the SCTP CMSGs. */
1419 err = sctp_msghdr_parse(msg, &cmsgs);
1421 if (err) {
1422 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1423 goto out_nounlock;
1426 /* Fetch the destination address for this packet. This
1427 * address only selects the association--it is not necessarily
1428 * the address we will send to.
1429 * For a peeled-off socket, msg_name is ignored.
1431 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1432 int msg_namelen = msg->msg_namelen;
1434 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1435 msg_namelen);
1436 if (err)
1437 return err;
1439 if (msg_namelen > sizeof(to))
1440 msg_namelen = sizeof(to);
1441 memcpy(&to, msg->msg_name, msg_namelen);
1442 msg_name = msg->msg_name;
1445 sinfo = cmsgs.info;
1446 sinit = cmsgs.init;
1448 /* Did the user specify SNDRCVINFO? */
1449 if (sinfo) {
1450 sinfo_flags = sinfo->sinfo_flags;
1451 associd = sinfo->sinfo_assoc_id;
1454 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1455 msg_len, sinfo_flags);
1457 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1458 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1459 err = -EINVAL;
1460 goto out_nounlock;
1463 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1464 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1465 * If SCTP_ABORT is set, the message length could be non zero with
1466 * the msg_iov set to the user abort reason.
1468 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1469 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1470 err = -EINVAL;
1471 goto out_nounlock;
1474 /* If SCTP_ADDR_OVER is set, there must be an address
1475 * specified in msg_name.
1477 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1478 err = -EINVAL;
1479 goto out_nounlock;
1482 transport = NULL;
1484 SCTP_DEBUG_PRINTK("About to look up association.\n");
1486 sctp_lock_sock(sk);
1488 /* If a msg_name has been specified, assume this is to be used. */
1489 if (msg_name) {
1490 /* Look for a matching association on the endpoint. */
1491 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1492 if (!asoc) {
1493 /* If we could not find a matching association on the
1494 * endpoint, make sure that it is not a TCP-style
1495 * socket that already has an association or there is
1496 * no peeled-off association on another socket.
1498 if ((sctp_style(sk, TCP) &&
1499 sctp_sstate(sk, ESTABLISHED)) ||
1500 sctp_endpoint_is_peeled_off(ep, &to)) {
1501 err = -EADDRNOTAVAIL;
1502 goto out_unlock;
1505 } else {
1506 asoc = sctp_id2assoc(sk, associd);
1507 if (!asoc) {
1508 err = -EPIPE;
1509 goto out_unlock;
1513 if (asoc) {
1514 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1516 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1517 * socket that has an association in CLOSED state. This can
1518 * happen when an accepted socket has an association that is
1519 * already CLOSED.
1521 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1522 err = -EPIPE;
1523 goto out_unlock;
1526 if (sinfo_flags & SCTP_EOF) {
1527 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1528 asoc);
1529 sctp_primitive_SHUTDOWN(asoc, NULL);
1530 err = 0;
1531 goto out_unlock;
1533 if (sinfo_flags & SCTP_ABORT) {
1534 struct sctp_chunk *chunk;
1536 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1537 if (!chunk) {
1538 err = -ENOMEM;
1539 goto out_unlock;
1542 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1543 sctp_primitive_ABORT(asoc, chunk);
1544 err = 0;
1545 goto out_unlock;
1549 /* Do we need to create the association? */
1550 if (!asoc) {
1551 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1553 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1554 err = -EINVAL;
1555 goto out_unlock;
1558 /* Check for invalid stream against the stream counts,
1559 * either the default or the user specified stream counts.
1561 if (sinfo) {
1562 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1563 /* Check against the defaults. */
1564 if (sinfo->sinfo_stream >=
1565 sp->initmsg.sinit_num_ostreams) {
1566 err = -EINVAL;
1567 goto out_unlock;
1569 } else {
1570 /* Check against the requested. */
1571 if (sinfo->sinfo_stream >=
1572 sinit->sinit_num_ostreams) {
1573 err = -EINVAL;
1574 goto out_unlock;
1580 * API 3.1.2 bind() - UDP Style Syntax
1581 * If a bind() or sctp_bindx() is not called prior to a
1582 * sendmsg() call that initiates a new association, the
1583 * system picks an ephemeral port and will choose an address
1584 * set equivalent to binding with a wildcard address.
1586 if (!ep->base.bind_addr.port) {
1587 if (sctp_autobind(sk)) {
1588 err = -EAGAIN;
1589 goto out_unlock;
1591 } else {
1593 * If an unprivileged user inherits a one-to-many
1594 * style socket with open associations on a privileged
1595 * port, it MAY be permitted to accept new associations,
1596 * but it SHOULD NOT be permitted to open new
1597 * associations.
1599 if (ep->base.bind_addr.port < PROT_SOCK &&
1600 !capable(CAP_NET_BIND_SERVICE)) {
1601 err = -EACCES;
1602 goto out_unlock;
1606 scope = sctp_scope(&to);
1607 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1608 if (!new_asoc) {
1609 err = -ENOMEM;
1610 goto out_unlock;
1612 asoc = new_asoc;
1614 /* If the SCTP_INIT ancillary data is specified, set all
1615 * the association init values accordingly.
1617 if (sinit) {
1618 if (sinit->sinit_num_ostreams) {
1619 asoc->c.sinit_num_ostreams =
1620 sinit->sinit_num_ostreams;
1622 if (sinit->sinit_max_instreams) {
1623 asoc->c.sinit_max_instreams =
1624 sinit->sinit_max_instreams;
1626 if (sinit->sinit_max_attempts) {
1627 asoc->max_init_attempts
1628 = sinit->sinit_max_attempts;
1630 if (sinit->sinit_max_init_timeo) {
1631 asoc->max_init_timeo =
1632 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1636 /* Prime the peer's transport structures. */
1637 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1638 if (!transport) {
1639 err = -ENOMEM;
1640 goto out_free;
1642 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1643 if (err < 0) {
1644 err = -ENOMEM;
1645 goto out_free;
1649 /* ASSERT: we have a valid association at this point. */
1650 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1652 if (!sinfo) {
1653 /* If the user didn't specify SNDRCVINFO, make up one with
1654 * some defaults.
1656 default_sinfo.sinfo_stream = asoc->default_stream;
1657 default_sinfo.sinfo_flags = asoc->default_flags;
1658 default_sinfo.sinfo_ppid = asoc->default_ppid;
1659 default_sinfo.sinfo_context = asoc->default_context;
1660 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1661 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1662 sinfo = &default_sinfo;
1665 /* API 7.1.7, the sndbuf size per association bounds the
1666 * maximum size of data that can be sent in a single send call.
1668 if (msg_len > sk->sk_sndbuf) {
1669 err = -EMSGSIZE;
1670 goto out_free;
1673 if (asoc->pmtu_pending)
1674 sctp_assoc_pending_pmtu(asoc);
1676 /* If fragmentation is disabled and the message length exceeds the
1677 * association fragmentation point, return EMSGSIZE. The I-D
1678 * does not specify what this error is, but this looks like
1679 * a great fit.
1681 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1682 err = -EMSGSIZE;
1683 goto out_free;
1686 if (sinfo) {
1687 /* Check for invalid stream. */
1688 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1689 err = -EINVAL;
1690 goto out_free;
1694 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1695 if (!sctp_wspace(asoc)) {
1696 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1697 if (err)
1698 goto out_free;
1701 /* If an address is passed with the sendto/sendmsg call, it is used
1702 * to override the primary destination address in the TCP model, or
1703 * when SCTP_ADDR_OVER flag is set in the UDP model.
1705 if ((sctp_style(sk, TCP) && msg_name) ||
1706 (sinfo_flags & SCTP_ADDR_OVER)) {
1707 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1708 if (!chunk_tp) {
1709 err = -EINVAL;
1710 goto out_free;
1712 } else
1713 chunk_tp = NULL;
1715 /* Auto-connect, if we aren't connected already. */
1716 if (sctp_state(asoc, CLOSED)) {
1717 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1718 if (err < 0)
1719 goto out_free;
1720 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1723 /* Break the message into multiple chunks of maximum size. */
1724 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1725 if (!datamsg) {
1726 err = -ENOMEM;
1727 goto out_free;
1730 /* Now send the (possibly) fragmented message. */
1731 list_for_each(pos, &datamsg->chunks) {
1732 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1733 sctp_datamsg_track(chunk);
1735 /* Do accounting for the write space. */
1736 sctp_set_owner_w(chunk);
1738 chunk->transport = chunk_tp;
1740 /* Send it to the lower layers. Note: all chunks
1741 * must either fail or succeed. The lower layer
1742 * works that way today. Keep it that way or this
1743 * breaks.
1745 err = sctp_primitive_SEND(asoc, chunk);
1746 /* Did the lower layer accept the chunk? */
1747 if (err)
1748 sctp_chunk_free(chunk);
1749 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1752 sctp_datamsg_free(datamsg);
1753 if (err)
1754 goto out_free;
1755 else
1756 err = msg_len;
1758 /* If we are already past ASSOCIATE, the lower
1759 * layers are responsible for association cleanup.
1761 goto out_unlock;
1763 out_free:
1764 if (new_asoc)
1765 sctp_association_free(asoc);
1766 out_unlock:
1767 sctp_release_sock(sk);
1769 out_nounlock:
1770 return sctp_error(sk, msg_flags, err);
1772 #if 0
1773 do_sock_err:
1774 if (msg_len)
1775 err = msg_len;
1776 else
1777 err = sock_error(sk);
1778 goto out;
1780 do_interrupted:
1781 if (msg_len)
1782 err = msg_len;
1783 goto out;
1784 #endif /* 0 */
1787 /* This is an extended version of skb_pull() that removes the data from the
1788 * start of a skb even when data is spread across the list of skb's in the
1789 * frag_list. len specifies the total amount of data that needs to be removed.
1790 * when 'len' bytes could be removed from the skb, it returns 0.
1791 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1792 * could not be removed.
1794 static int sctp_skb_pull(struct sk_buff *skb, int len)
1796 struct sk_buff *list;
1797 int skb_len = skb_headlen(skb);
1798 int rlen;
1800 if (len <= skb_len) {
1801 __skb_pull(skb, len);
1802 return 0;
1804 len -= skb_len;
1805 __skb_pull(skb, skb_len);
1807 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1808 rlen = sctp_skb_pull(list, len);
1809 skb->len -= (len-rlen);
1810 skb->data_len -= (len-rlen);
1812 if (!rlen)
1813 return 0;
1815 len = rlen;
1818 return len;
1821 /* API 3.1.3 recvmsg() - UDP Style Syntax
1823 * ssize_t recvmsg(int socket, struct msghdr *message,
1824 * int flags);
1826 * socket - the socket descriptor of the endpoint.
1827 * message - pointer to the msghdr structure which contains a single
1828 * user message and possibly some ancillary data.
1830 * See Section 5 for complete description of the data
1831 * structures.
1833 * flags - flags sent or received with the user message, see Section
1834 * 5 for complete description of the flags.
1836 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1838 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1839 struct msghdr *msg, size_t len, int noblock,
1840 int flags, int *addr_len)
1842 struct sctp_ulpevent *event = NULL;
1843 struct sctp_sock *sp = sctp_sk(sk);
1844 struct sk_buff *skb;
1845 int copied;
1846 int err = 0;
1847 int skb_len;
1849 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1850 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1851 "len", len, "knoblauch", noblock,
1852 "flags", flags, "addr_len", addr_len);
1854 sctp_lock_sock(sk);
1856 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1857 err = -ENOTCONN;
1858 goto out;
1861 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1862 if (!skb)
1863 goto out;
1865 /* Get the total length of the skb including any skb's in the
1866 * frag_list.
1868 skb_len = skb->len;
1870 copied = skb_len;
1871 if (copied > len)
1872 copied = len;
1874 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1876 event = sctp_skb2event(skb);
1878 if (err)
1879 goto out_free;
1881 sock_recv_timestamp(msg, sk, skb);
1882 if (sctp_ulpevent_is_notification(event)) {
1883 msg->msg_flags |= MSG_NOTIFICATION;
1884 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1885 } else {
1886 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1889 /* Check if we allow SCTP_SNDRCVINFO. */
1890 if (sp->subscribe.sctp_data_io_event)
1891 sctp_ulpevent_read_sndrcvinfo(event, msg);
1892 #if 0
1893 /* FIXME: we should be calling IP/IPv6 layers. */
1894 if (sk->sk_protinfo.af_inet.cmsg_flags)
1895 ip_cmsg_recv(msg, skb);
1896 #endif
1898 err = copied;
1900 /* If skb's length exceeds the user's buffer, update the skb and
1901 * push it back to the receive_queue so that the next call to
1902 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1904 if (skb_len > copied) {
1905 msg->msg_flags &= ~MSG_EOR;
1906 if (flags & MSG_PEEK)
1907 goto out_free;
1908 sctp_skb_pull(skb, copied);
1909 skb_queue_head(&sk->sk_receive_queue, skb);
1911 /* When only partial message is copied to the user, increase
1912 * rwnd by that amount. If all the data in the skb is read,
1913 * rwnd is updated when the event is freed.
1915 sctp_assoc_rwnd_increase(event->asoc, copied);
1916 goto out;
1917 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1918 (event->msg_flags & MSG_EOR))
1919 msg->msg_flags |= MSG_EOR;
1920 else
1921 msg->msg_flags &= ~MSG_EOR;
1923 out_free:
1924 if (flags & MSG_PEEK) {
1925 /* Release the skb reference acquired after peeking the skb in
1926 * sctp_skb_recv_datagram().
1928 kfree_skb(skb);
1929 } else {
1930 /* Free the event which includes releasing the reference to
1931 * the owner of the skb, freeing the skb and updating the
1932 * rwnd.
1934 sctp_ulpevent_free(event);
1936 out:
1937 sctp_release_sock(sk);
1938 return err;
1941 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1943 * This option is a on/off flag. If enabled no SCTP message
1944 * fragmentation will be performed. Instead if a message being sent
1945 * exceeds the current PMTU size, the message will NOT be sent and
1946 * instead a error will be indicated to the user.
1948 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1949 char __user *optval, int optlen)
1951 int val;
1953 if (optlen < sizeof(int))
1954 return -EINVAL;
1956 if (get_user(val, (int __user *)optval))
1957 return -EFAULT;
1959 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1961 return 0;
1964 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1965 int optlen)
1967 if (optlen != sizeof(struct sctp_event_subscribe))
1968 return -EINVAL;
1969 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1970 return -EFAULT;
1971 return 0;
1974 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1976 * This socket option is applicable to the UDP-style socket only. When
1977 * set it will cause associations that are idle for more than the
1978 * specified number of seconds to automatically close. An association
1979 * being idle is defined an association that has NOT sent or received
1980 * user data. The special value of '0' indicates that no automatic
1981 * close of any associations should be performed. The option expects an
1982 * integer defining the number of seconds of idle time before an
1983 * association is closed.
1985 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1986 int optlen)
1988 struct sctp_sock *sp = sctp_sk(sk);
1990 /* Applicable to UDP-style socket only */
1991 if (sctp_style(sk, TCP))
1992 return -EOPNOTSUPP;
1993 if (optlen != sizeof(int))
1994 return -EINVAL;
1995 if (copy_from_user(&sp->autoclose, optval, optlen))
1996 return -EFAULT;
1998 return 0;
2001 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2003 * Applications can enable or disable heartbeats for any peer address of
2004 * an association, modify an address's heartbeat interval, force a
2005 * heartbeat to be sent immediately, and adjust the address's maximum
2006 * number of retransmissions sent before an address is considered
2007 * unreachable. The following structure is used to access and modify an
2008 * address's parameters:
2010 * struct sctp_paddrparams {
2011 * sctp_assoc_t spp_assoc_id;
2012 * struct sockaddr_storage spp_address;
2013 * uint32_t spp_hbinterval;
2014 * uint16_t spp_pathmaxrxt;
2015 * uint32_t spp_pathmtu;
2016 * uint32_t spp_sackdelay;
2017 * uint32_t spp_flags;
2018 * };
2020 * spp_assoc_id - (one-to-many style socket) This is filled in the
2021 * application, and identifies the association for
2022 * this query.
2023 * spp_address - This specifies which address is of interest.
2024 * spp_hbinterval - This contains the value of the heartbeat interval,
2025 * in milliseconds. If a value of zero
2026 * is present in this field then no changes are to
2027 * be made to this parameter.
2028 * spp_pathmaxrxt - This contains the maximum number of
2029 * retransmissions before this address shall be
2030 * considered unreachable. If a value of zero
2031 * is present in this field then no changes are to
2032 * be made to this parameter.
2033 * spp_pathmtu - When Path MTU discovery is disabled the value
2034 * specified here will be the "fixed" path mtu.
2035 * Note that if the spp_address field is empty
2036 * then all associations on this address will
2037 * have this fixed path mtu set upon them.
2039 * spp_sackdelay - When delayed sack is enabled, this value specifies
2040 * the number of milliseconds that sacks will be delayed
2041 * for. This value will apply to all addresses of an
2042 * association if the spp_address field is empty. Note
2043 * also, that if delayed sack is enabled and this
2044 * value is set to 0, no change is made to the last
2045 * recorded delayed sack timer value.
2047 * spp_flags - These flags are used to control various features
2048 * on an association. The flag field may contain
2049 * zero or more of the following options.
2051 * SPP_HB_ENABLE - Enable heartbeats on the
2052 * specified address. Note that if the address
2053 * field is empty all addresses for the association
2054 * have heartbeats enabled upon them.
2056 * SPP_HB_DISABLE - Disable heartbeats on the
2057 * speicifed address. Note that if the address
2058 * field is empty all addresses for the association
2059 * will have their heartbeats disabled. Note also
2060 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2061 * mutually exclusive, only one of these two should
2062 * be specified. Enabling both fields will have
2063 * undetermined results.
2065 * SPP_HB_DEMAND - Request a user initiated heartbeat
2066 * to be made immediately.
2068 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2069 * heartbeat delayis to be set to the value of 0
2070 * milliseconds.
2072 * SPP_PMTUD_ENABLE - This field will enable PMTU
2073 * discovery upon the specified address. Note that
2074 * if the address feild is empty then all addresses
2075 * on the association are effected.
2077 * SPP_PMTUD_DISABLE - This field will disable PMTU
2078 * discovery upon the specified address. Note that
2079 * if the address feild is empty then all addresses
2080 * on the association are effected. Not also that
2081 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2082 * exclusive. Enabling both will have undetermined
2083 * results.
2085 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2086 * on delayed sack. The time specified in spp_sackdelay
2087 * is used to specify the sack delay for this address. Note
2088 * that if spp_address is empty then all addresses will
2089 * enable delayed sack and take on the sack delay
2090 * value specified in spp_sackdelay.
2091 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2092 * off delayed sack. If the spp_address field is blank then
2093 * delayed sack is disabled for the entire association. Note
2094 * also that this field is mutually exclusive to
2095 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2096 * results.
2098 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2099 struct sctp_transport *trans,
2100 struct sctp_association *asoc,
2101 struct sctp_sock *sp,
2102 int hb_change,
2103 int pmtud_change,
2104 int sackdelay_change)
2106 int error;
2108 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2109 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2110 if (error)
2111 return error;
2114 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2115 * this field is ignored. Note also that a value of zero indicates
2116 * the current setting should be left unchanged.
2118 if (params->spp_flags & SPP_HB_ENABLE) {
2120 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2121 * set. This lets us use 0 value when this flag
2122 * is set.
2124 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2125 params->spp_hbinterval = 0;
2127 if (params->spp_hbinterval ||
2128 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2129 if (trans) {
2130 trans->hbinterval =
2131 msecs_to_jiffies(params->spp_hbinterval);
2132 } else if (asoc) {
2133 asoc->hbinterval =
2134 msecs_to_jiffies(params->spp_hbinterval);
2135 } else {
2136 sp->hbinterval = params->spp_hbinterval;
2141 if (hb_change) {
2142 if (trans) {
2143 trans->param_flags =
2144 (trans->param_flags & ~SPP_HB) | hb_change;
2145 } else if (asoc) {
2146 asoc->param_flags =
2147 (asoc->param_flags & ~SPP_HB) | hb_change;
2148 } else {
2149 sp->param_flags =
2150 (sp->param_flags & ~SPP_HB) | hb_change;
2154 /* When Path MTU discovery is disabled the value specified here will
2155 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2156 * include the flag SPP_PMTUD_DISABLE for this field to have any
2157 * effect).
2159 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2160 if (trans) {
2161 trans->pathmtu = params->spp_pathmtu;
2162 sctp_assoc_sync_pmtu(asoc);
2163 } else if (asoc) {
2164 asoc->pathmtu = params->spp_pathmtu;
2165 sctp_frag_point(sp, params->spp_pathmtu);
2166 } else {
2167 sp->pathmtu = params->spp_pathmtu;
2171 if (pmtud_change) {
2172 if (trans) {
2173 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2174 (params->spp_flags & SPP_PMTUD_ENABLE);
2175 trans->param_flags =
2176 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2177 if (update) {
2178 sctp_transport_pmtu(trans);
2179 sctp_assoc_sync_pmtu(asoc);
2181 } else if (asoc) {
2182 asoc->param_flags =
2183 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2184 } else {
2185 sp->param_flags =
2186 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2190 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2191 * value of this field is ignored. Note also that a value of zero
2192 * indicates the current setting should be left unchanged.
2194 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2195 if (trans) {
2196 trans->sackdelay =
2197 msecs_to_jiffies(params->spp_sackdelay);
2198 } else if (asoc) {
2199 asoc->sackdelay =
2200 msecs_to_jiffies(params->spp_sackdelay);
2201 } else {
2202 sp->sackdelay = params->spp_sackdelay;
2206 if (sackdelay_change) {
2207 if (trans) {
2208 trans->param_flags =
2209 (trans->param_flags & ~SPP_SACKDELAY) |
2210 sackdelay_change;
2211 } else if (asoc) {
2212 asoc->param_flags =
2213 (asoc->param_flags & ~SPP_SACKDELAY) |
2214 sackdelay_change;
2215 } else {
2216 sp->param_flags =
2217 (sp->param_flags & ~SPP_SACKDELAY) |
2218 sackdelay_change;
2222 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2223 * of this field is ignored. Note also that a value of zero
2224 * indicates the current setting should be left unchanged.
2226 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
2227 if (trans) {
2228 trans->pathmaxrxt = params->spp_pathmaxrxt;
2229 } else if (asoc) {
2230 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2231 } else {
2232 sp->pathmaxrxt = params->spp_pathmaxrxt;
2236 return 0;
2239 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2240 char __user *optval, int optlen)
2242 struct sctp_paddrparams params;
2243 struct sctp_transport *trans = NULL;
2244 struct sctp_association *asoc = NULL;
2245 struct sctp_sock *sp = sctp_sk(sk);
2246 int error;
2247 int hb_change, pmtud_change, sackdelay_change;
2249 if (optlen != sizeof(struct sctp_paddrparams))
2250 return - EINVAL;
2252 if (copy_from_user(&params, optval, optlen))
2253 return -EFAULT;
2255 /* Validate flags and value parameters. */
2256 hb_change = params.spp_flags & SPP_HB;
2257 pmtud_change = params.spp_flags & SPP_PMTUD;
2258 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2260 if (hb_change == SPP_HB ||
2261 pmtud_change == SPP_PMTUD ||
2262 sackdelay_change == SPP_SACKDELAY ||
2263 params.spp_sackdelay > 500 ||
2264 (params.spp_pathmtu
2265 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2266 return -EINVAL;
2268 /* If an address other than INADDR_ANY is specified, and
2269 * no transport is found, then the request is invalid.
2271 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2272 trans = sctp_addr_id2transport(sk, &params.spp_address,
2273 params.spp_assoc_id);
2274 if (!trans)
2275 return -EINVAL;
2278 /* Get association, if assoc_id != 0 and the socket is a one
2279 * to many style socket, and an association was not found, then
2280 * the id was invalid.
2282 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2283 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2284 return -EINVAL;
2286 /* Heartbeat demand can only be sent on a transport or
2287 * association, but not a socket.
2289 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2290 return -EINVAL;
2292 /* Process parameters. */
2293 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2294 hb_change, pmtud_change,
2295 sackdelay_change);
2297 if (error)
2298 return error;
2300 /* If changes are for association, also apply parameters to each
2301 * transport.
2303 if (!trans && asoc) {
2304 struct list_head *pos;
2306 list_for_each(pos, &asoc->peer.transport_addr_list) {
2307 trans = list_entry(pos, struct sctp_transport,
2308 transports);
2309 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2310 hb_change, pmtud_change,
2311 sackdelay_change);
2315 return 0;
2318 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2320 * This options will get or set the delayed ack timer. The time is set
2321 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2322 * endpoints default delayed ack timer value. If the assoc_id field is
2323 * non-zero, then the set or get effects the specified association.
2325 * struct sctp_assoc_value {
2326 * sctp_assoc_t assoc_id;
2327 * uint32_t assoc_value;
2328 * };
2330 * assoc_id - This parameter, indicates which association the
2331 * user is preforming an action upon. Note that if
2332 * this field's value is zero then the endpoints
2333 * default value is changed (effecting future
2334 * associations only).
2336 * assoc_value - This parameter contains the number of milliseconds
2337 * that the user is requesting the delayed ACK timer
2338 * be set to. Note that this value is defined in
2339 * the standard to be between 200 and 500 milliseconds.
2341 * Note: a value of zero will leave the value alone,
2342 * but disable SACK delay. A non-zero value will also
2343 * enable SACK delay.
2346 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2347 char __user *optval, int optlen)
2349 struct sctp_assoc_value params;
2350 struct sctp_transport *trans = NULL;
2351 struct sctp_association *asoc = NULL;
2352 struct sctp_sock *sp = sctp_sk(sk);
2354 if (optlen != sizeof(struct sctp_assoc_value))
2355 return - EINVAL;
2357 if (copy_from_user(&params, optval, optlen))
2358 return -EFAULT;
2360 /* Validate value parameter. */
2361 if (params.assoc_value > 500)
2362 return -EINVAL;
2364 /* Get association, if assoc_id != 0 and the socket is a one
2365 * to many style socket, and an association was not found, then
2366 * the id was invalid.
2368 asoc = sctp_id2assoc(sk, params.assoc_id);
2369 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2370 return -EINVAL;
2372 if (params.assoc_value) {
2373 if (asoc) {
2374 asoc->sackdelay =
2375 msecs_to_jiffies(params.assoc_value);
2376 asoc->param_flags =
2377 (asoc->param_flags & ~SPP_SACKDELAY) |
2378 SPP_SACKDELAY_ENABLE;
2379 } else {
2380 sp->sackdelay = params.assoc_value;
2381 sp->param_flags =
2382 (sp->param_flags & ~SPP_SACKDELAY) |
2383 SPP_SACKDELAY_ENABLE;
2385 } else {
2386 if (asoc) {
2387 asoc->param_flags =
2388 (asoc->param_flags & ~SPP_SACKDELAY) |
2389 SPP_SACKDELAY_DISABLE;
2390 } else {
2391 sp->param_flags =
2392 (sp->param_flags & ~SPP_SACKDELAY) |
2393 SPP_SACKDELAY_DISABLE;
2397 /* If change is for association, also apply to each transport. */
2398 if (asoc) {
2399 struct list_head *pos;
2401 list_for_each(pos, &asoc->peer.transport_addr_list) {
2402 trans = list_entry(pos, struct sctp_transport,
2403 transports);
2404 if (params.assoc_value) {
2405 trans->sackdelay =
2406 msecs_to_jiffies(params.assoc_value);
2407 trans->param_flags =
2408 (trans->param_flags & ~SPP_SACKDELAY) |
2409 SPP_SACKDELAY_ENABLE;
2410 } else {
2411 trans->param_flags =
2412 (trans->param_flags & ~SPP_SACKDELAY) |
2413 SPP_SACKDELAY_DISABLE;
2418 return 0;
2421 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2423 * Applications can specify protocol parameters for the default association
2424 * initialization. The option name argument to setsockopt() and getsockopt()
2425 * is SCTP_INITMSG.
2427 * Setting initialization parameters is effective only on an unconnected
2428 * socket (for UDP-style sockets only future associations are effected
2429 * by the change). With TCP-style sockets, this option is inherited by
2430 * sockets derived from a listener socket.
2432 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2434 struct sctp_initmsg sinit;
2435 struct sctp_sock *sp = sctp_sk(sk);
2437 if (optlen != sizeof(struct sctp_initmsg))
2438 return -EINVAL;
2439 if (copy_from_user(&sinit, optval, optlen))
2440 return -EFAULT;
2442 if (sinit.sinit_num_ostreams)
2443 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2444 if (sinit.sinit_max_instreams)
2445 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2446 if (sinit.sinit_max_attempts)
2447 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2448 if (sinit.sinit_max_init_timeo)
2449 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2451 return 0;
2455 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2457 * Applications that wish to use the sendto() system call may wish to
2458 * specify a default set of parameters that would normally be supplied
2459 * through the inclusion of ancillary data. This socket option allows
2460 * such an application to set the default sctp_sndrcvinfo structure.
2461 * The application that wishes to use this socket option simply passes
2462 * in to this call the sctp_sndrcvinfo structure defined in Section
2463 * 5.2.2) The input parameters accepted by this call include
2464 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2465 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2466 * to this call if the caller is using the UDP model.
2468 static int sctp_setsockopt_default_send_param(struct sock *sk,
2469 char __user *optval, int optlen)
2471 struct sctp_sndrcvinfo info;
2472 struct sctp_association *asoc;
2473 struct sctp_sock *sp = sctp_sk(sk);
2475 if (optlen != sizeof(struct sctp_sndrcvinfo))
2476 return -EINVAL;
2477 if (copy_from_user(&info, optval, optlen))
2478 return -EFAULT;
2480 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2481 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2482 return -EINVAL;
2484 if (asoc) {
2485 asoc->default_stream = info.sinfo_stream;
2486 asoc->default_flags = info.sinfo_flags;
2487 asoc->default_ppid = info.sinfo_ppid;
2488 asoc->default_context = info.sinfo_context;
2489 asoc->default_timetolive = info.sinfo_timetolive;
2490 } else {
2491 sp->default_stream = info.sinfo_stream;
2492 sp->default_flags = info.sinfo_flags;
2493 sp->default_ppid = info.sinfo_ppid;
2494 sp->default_context = info.sinfo_context;
2495 sp->default_timetolive = info.sinfo_timetolive;
2498 return 0;
2501 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2503 * Requests that the local SCTP stack use the enclosed peer address as
2504 * the association primary. The enclosed address must be one of the
2505 * association peer's addresses.
2507 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2508 int optlen)
2510 struct sctp_prim prim;
2511 struct sctp_transport *trans;
2513 if (optlen != sizeof(struct sctp_prim))
2514 return -EINVAL;
2516 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2517 return -EFAULT;
2519 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2520 if (!trans)
2521 return -EINVAL;
2523 sctp_assoc_set_primary(trans->asoc, trans);
2525 return 0;
2529 * 7.1.5 SCTP_NODELAY
2531 * Turn on/off any Nagle-like algorithm. This means that packets are
2532 * generally sent as soon as possible and no unnecessary delays are
2533 * introduced, at the cost of more packets in the network. Expects an
2534 * integer boolean flag.
2536 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2537 int optlen)
2539 int val;
2541 if (optlen < sizeof(int))
2542 return -EINVAL;
2543 if (get_user(val, (int __user *)optval))
2544 return -EFAULT;
2546 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2547 return 0;
2552 * 7.1.1 SCTP_RTOINFO
2554 * The protocol parameters used to initialize and bound retransmission
2555 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2556 * and modify these parameters.
2557 * All parameters are time values, in milliseconds. A value of 0, when
2558 * modifying the parameters, indicates that the current value should not
2559 * be changed.
2562 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2563 struct sctp_rtoinfo rtoinfo;
2564 struct sctp_association *asoc;
2566 if (optlen != sizeof (struct sctp_rtoinfo))
2567 return -EINVAL;
2569 if (copy_from_user(&rtoinfo, optval, optlen))
2570 return -EFAULT;
2572 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2574 /* Set the values to the specific association */
2575 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2576 return -EINVAL;
2578 if (asoc) {
2579 if (rtoinfo.srto_initial != 0)
2580 asoc->rto_initial =
2581 msecs_to_jiffies(rtoinfo.srto_initial);
2582 if (rtoinfo.srto_max != 0)
2583 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2584 if (rtoinfo.srto_min != 0)
2585 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2586 } else {
2587 /* If there is no association or the association-id = 0
2588 * set the values to the endpoint.
2590 struct sctp_sock *sp = sctp_sk(sk);
2592 if (rtoinfo.srto_initial != 0)
2593 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2594 if (rtoinfo.srto_max != 0)
2595 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2596 if (rtoinfo.srto_min != 0)
2597 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2600 return 0;
2605 * 7.1.2 SCTP_ASSOCINFO
2607 * This option is used to tune the maximum retransmission attempts
2608 * of the association.
2609 * Returns an error if the new association retransmission value is
2610 * greater than the sum of the retransmission value of the peer.
2611 * See [SCTP] for more information.
2614 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2617 struct sctp_assocparams assocparams;
2618 struct sctp_association *asoc;
2620 if (optlen != sizeof(struct sctp_assocparams))
2621 return -EINVAL;
2622 if (copy_from_user(&assocparams, optval, optlen))
2623 return -EFAULT;
2625 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2627 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2628 return -EINVAL;
2630 /* Set the values to the specific association */
2631 if (asoc) {
2632 if (assocparams.sasoc_asocmaxrxt != 0) {
2633 __u32 path_sum = 0;
2634 int paths = 0;
2635 struct list_head *pos;
2636 struct sctp_transport *peer_addr;
2638 list_for_each(pos, &asoc->peer.transport_addr_list) {
2639 peer_addr = list_entry(pos,
2640 struct sctp_transport,
2641 transports);
2642 path_sum += peer_addr->pathmaxrxt;
2643 paths++;
2646 /* Only validate asocmaxrxt if we have more then
2647 * one path/transport. We do this because path
2648 * retransmissions are only counted when we have more
2649 * then one path.
2651 if (paths > 1 &&
2652 assocparams.sasoc_asocmaxrxt > path_sum)
2653 return -EINVAL;
2655 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2658 if (assocparams.sasoc_cookie_life != 0) {
2659 asoc->cookie_life.tv_sec =
2660 assocparams.sasoc_cookie_life / 1000;
2661 asoc->cookie_life.tv_usec =
2662 (assocparams.sasoc_cookie_life % 1000)
2663 * 1000;
2665 } else {
2666 /* Set the values to the endpoint */
2667 struct sctp_sock *sp = sctp_sk(sk);
2669 if (assocparams.sasoc_asocmaxrxt != 0)
2670 sp->assocparams.sasoc_asocmaxrxt =
2671 assocparams.sasoc_asocmaxrxt;
2672 if (assocparams.sasoc_cookie_life != 0)
2673 sp->assocparams.sasoc_cookie_life =
2674 assocparams.sasoc_cookie_life;
2676 return 0;
2680 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2682 * This socket option is a boolean flag which turns on or off mapped V4
2683 * addresses. If this option is turned on and the socket is type
2684 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2685 * If this option is turned off, then no mapping will be done of V4
2686 * addresses and a user will receive both PF_INET6 and PF_INET type
2687 * addresses on the socket.
2689 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2691 int val;
2692 struct sctp_sock *sp = sctp_sk(sk);
2694 if (optlen < sizeof(int))
2695 return -EINVAL;
2696 if (get_user(val, (int __user *)optval))
2697 return -EFAULT;
2698 if (val)
2699 sp->v4mapped = 1;
2700 else
2701 sp->v4mapped = 0;
2703 return 0;
2707 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2709 * This socket option specifies the maximum size to put in any outgoing
2710 * SCTP chunk. If a message is larger than this size it will be
2711 * fragmented by SCTP into the specified size. Note that the underlying
2712 * SCTP implementation may fragment into smaller sized chunks when the
2713 * PMTU of the underlying association is smaller than the value set by
2714 * the user.
2716 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2718 struct sctp_association *asoc;
2719 struct list_head *pos;
2720 struct sctp_sock *sp = sctp_sk(sk);
2721 int val;
2723 if (optlen < sizeof(int))
2724 return -EINVAL;
2725 if (get_user(val, (int __user *)optval))
2726 return -EFAULT;
2727 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2728 return -EINVAL;
2729 sp->user_frag = val;
2731 /* Update the frag_point of the existing associations. */
2732 list_for_each(pos, &(sp->ep->asocs)) {
2733 asoc = list_entry(pos, struct sctp_association, asocs);
2734 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2737 return 0;
2742 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2744 * Requests that the peer mark the enclosed address as the association
2745 * primary. The enclosed address must be one of the association's
2746 * locally bound addresses. The following structure is used to make a
2747 * set primary request:
2749 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2750 int optlen)
2752 struct sctp_sock *sp;
2753 struct sctp_endpoint *ep;
2754 struct sctp_association *asoc = NULL;
2755 struct sctp_setpeerprim prim;
2756 struct sctp_chunk *chunk;
2757 int err;
2759 sp = sctp_sk(sk);
2760 ep = sp->ep;
2762 if (!sctp_addip_enable)
2763 return -EPERM;
2765 if (optlen != sizeof(struct sctp_setpeerprim))
2766 return -EINVAL;
2768 if (copy_from_user(&prim, optval, optlen))
2769 return -EFAULT;
2771 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2772 if (!asoc)
2773 return -EINVAL;
2775 if (!asoc->peer.asconf_capable)
2776 return -EPERM;
2778 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2779 return -EPERM;
2781 if (!sctp_state(asoc, ESTABLISHED))
2782 return -ENOTCONN;
2784 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2785 return -EADDRNOTAVAIL;
2787 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2788 chunk = sctp_make_asconf_set_prim(asoc,
2789 (union sctp_addr *)&prim.sspp_addr);
2790 if (!chunk)
2791 return -ENOMEM;
2793 err = sctp_send_asconf(asoc, chunk);
2795 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2797 return err;
2800 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2801 int optlen)
2803 struct sctp_setadaptation adaptation;
2805 if (optlen != sizeof(struct sctp_setadaptation))
2806 return -EINVAL;
2807 if (copy_from_user(&adaptation, optval, optlen))
2808 return -EFAULT;
2810 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2812 return 0;
2816 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2818 * The context field in the sctp_sndrcvinfo structure is normally only
2819 * used when a failed message is retrieved holding the value that was
2820 * sent down on the actual send call. This option allows the setting of
2821 * a default context on an association basis that will be received on
2822 * reading messages from the peer. This is especially helpful in the
2823 * one-2-many model for an application to keep some reference to an
2824 * internal state machine that is processing messages on the
2825 * association. Note that the setting of this value only effects
2826 * received messages from the peer and does not effect the value that is
2827 * saved with outbound messages.
2829 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2830 int optlen)
2832 struct sctp_assoc_value params;
2833 struct sctp_sock *sp;
2834 struct sctp_association *asoc;
2836 if (optlen != sizeof(struct sctp_assoc_value))
2837 return -EINVAL;
2838 if (copy_from_user(&params, optval, optlen))
2839 return -EFAULT;
2841 sp = sctp_sk(sk);
2843 if (params.assoc_id != 0) {
2844 asoc = sctp_id2assoc(sk, params.assoc_id);
2845 if (!asoc)
2846 return -EINVAL;
2847 asoc->default_rcv_context = params.assoc_value;
2848 } else {
2849 sp->default_rcv_context = params.assoc_value;
2852 return 0;
2856 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2858 * This options will at a minimum specify if the implementation is doing
2859 * fragmented interleave. Fragmented interleave, for a one to many
2860 * socket, is when subsequent calls to receive a message may return
2861 * parts of messages from different associations. Some implementations
2862 * may allow you to turn this value on or off. If so, when turned off,
2863 * no fragment interleave will occur (which will cause a head of line
2864 * blocking amongst multiple associations sharing the same one to many
2865 * socket). When this option is turned on, then each receive call may
2866 * come from a different association (thus the user must receive data
2867 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2868 * association each receive belongs to.
2870 * This option takes a boolean value. A non-zero value indicates that
2871 * fragmented interleave is on. A value of zero indicates that
2872 * fragmented interleave is off.
2874 * Note that it is important that an implementation that allows this
2875 * option to be turned on, have it off by default. Otherwise an unaware
2876 * application using the one to many model may become confused and act
2877 * incorrectly.
2879 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2880 char __user *optval,
2881 int optlen)
2883 int val;
2885 if (optlen != sizeof(int))
2886 return -EINVAL;
2887 if (get_user(val, (int __user *)optval))
2888 return -EFAULT;
2890 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2892 return 0;
2896 * 7.1.25. Set or Get the sctp partial delivery point
2897 * (SCTP_PARTIAL_DELIVERY_POINT)
2898 * This option will set or get the SCTP partial delivery point. This
2899 * point is the size of a message where the partial delivery API will be
2900 * invoked to help free up rwnd space for the peer. Setting this to a
2901 * lower value will cause partial delivery's to happen more often. The
2902 * calls argument is an integer that sets or gets the partial delivery
2903 * point.
2905 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2906 char __user *optval,
2907 int optlen)
2909 u32 val;
2911 if (optlen != sizeof(u32))
2912 return -EINVAL;
2913 if (get_user(val, (int __user *)optval))
2914 return -EFAULT;
2916 sctp_sk(sk)->pd_point = val;
2918 return 0; /* is this the right error code? */
2922 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2924 * This option will allow a user to change the maximum burst of packets
2925 * that can be emitted by this association. Note that the default value
2926 * is 4, and some implementations may restrict this setting so that it
2927 * can only be lowered.
2929 * NOTE: This text doesn't seem right. Do this on a socket basis with
2930 * future associations inheriting the socket value.
2932 static int sctp_setsockopt_maxburst(struct sock *sk,
2933 char __user *optval,
2934 int optlen)
2936 int val;
2938 if (optlen != sizeof(int))
2939 return -EINVAL;
2940 if (get_user(val, (int __user *)optval))
2941 return -EFAULT;
2943 if (val < 0)
2944 return -EINVAL;
2946 sctp_sk(sk)->max_burst = val;
2948 return 0;
2951 /* API 6.2 setsockopt(), getsockopt()
2953 * Applications use setsockopt() and getsockopt() to set or retrieve
2954 * socket options. Socket options are used to change the default
2955 * behavior of sockets calls. They are described in Section 7.
2957 * The syntax is:
2959 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2960 * int __user *optlen);
2961 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2962 * int optlen);
2964 * sd - the socket descript.
2965 * level - set to IPPROTO_SCTP for all SCTP options.
2966 * optname - the option name.
2967 * optval - the buffer to store the value of the option.
2968 * optlen - the size of the buffer.
2970 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2971 char __user *optval, int optlen)
2973 int retval = 0;
2975 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2976 sk, optname);
2978 /* I can hardly begin to describe how wrong this is. This is
2979 * so broken as to be worse than useless. The API draft
2980 * REALLY is NOT helpful here... I am not convinced that the
2981 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2982 * are at all well-founded.
2984 if (level != SOL_SCTP) {
2985 struct sctp_af *af = sctp_sk(sk)->pf->af;
2986 retval = af->setsockopt(sk, level, optname, optval, optlen);
2987 goto out_nounlock;
2990 sctp_lock_sock(sk);
2992 switch (optname) {
2993 case SCTP_SOCKOPT_BINDX_ADD:
2994 /* 'optlen' is the size of the addresses buffer. */
2995 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2996 optlen, SCTP_BINDX_ADD_ADDR);
2997 break;
2999 case SCTP_SOCKOPT_BINDX_REM:
3000 /* 'optlen' is the size of the addresses buffer. */
3001 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3002 optlen, SCTP_BINDX_REM_ADDR);
3003 break;
3005 case SCTP_SOCKOPT_CONNECTX:
3006 /* 'optlen' is the size of the addresses buffer. */
3007 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3008 optlen);
3009 break;
3011 case SCTP_DISABLE_FRAGMENTS:
3012 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3013 break;
3015 case SCTP_EVENTS:
3016 retval = sctp_setsockopt_events(sk, optval, optlen);
3017 break;
3019 case SCTP_AUTOCLOSE:
3020 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3021 break;
3023 case SCTP_PEER_ADDR_PARAMS:
3024 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3025 break;
3027 case SCTP_DELAYED_ACK_TIME:
3028 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3029 break;
3030 case SCTP_PARTIAL_DELIVERY_POINT:
3031 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3032 break;
3034 case SCTP_INITMSG:
3035 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3036 break;
3037 case SCTP_DEFAULT_SEND_PARAM:
3038 retval = sctp_setsockopt_default_send_param(sk, optval,
3039 optlen);
3040 break;
3041 case SCTP_PRIMARY_ADDR:
3042 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3043 break;
3044 case SCTP_SET_PEER_PRIMARY_ADDR:
3045 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3046 break;
3047 case SCTP_NODELAY:
3048 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3049 break;
3050 case SCTP_RTOINFO:
3051 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3052 break;
3053 case SCTP_ASSOCINFO:
3054 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3055 break;
3056 case SCTP_I_WANT_MAPPED_V4_ADDR:
3057 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3058 break;
3059 case SCTP_MAXSEG:
3060 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3061 break;
3062 case SCTP_ADAPTATION_LAYER:
3063 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3064 break;
3065 case SCTP_CONTEXT:
3066 retval = sctp_setsockopt_context(sk, optval, optlen);
3067 break;
3068 case SCTP_FRAGMENT_INTERLEAVE:
3069 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3070 break;
3071 case SCTP_MAX_BURST:
3072 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3073 break;
3074 default:
3075 retval = -ENOPROTOOPT;
3076 break;
3079 sctp_release_sock(sk);
3081 out_nounlock:
3082 return retval;
3085 /* API 3.1.6 connect() - UDP Style Syntax
3087 * An application may use the connect() call in the UDP model to initiate an
3088 * association without sending data.
3090 * The syntax is:
3092 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3094 * sd: the socket descriptor to have a new association added to.
3096 * nam: the address structure (either struct sockaddr_in or struct
3097 * sockaddr_in6 defined in RFC2553 [7]).
3099 * len: the size of the address.
3101 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3102 int addr_len)
3104 int err = 0;
3105 struct sctp_af *af;
3107 sctp_lock_sock(sk);
3109 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3110 __FUNCTION__, sk, addr, addr_len);
3112 /* Validate addr_len before calling common connect/connectx routine. */
3113 af = sctp_get_af_specific(addr->sa_family);
3114 if (!af || addr_len < af->sockaddr_len) {
3115 err = -EINVAL;
3116 } else {
3117 /* Pass correct addr len to common routine (so it knows there
3118 * is only one address being passed.
3120 err = __sctp_connect(sk, addr, af->sockaddr_len);
3123 sctp_release_sock(sk);
3124 return err;
3127 /* FIXME: Write comments. */
3128 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3130 return -EOPNOTSUPP; /* STUB */
3133 /* 4.1.4 accept() - TCP Style Syntax
3135 * Applications use accept() call to remove an established SCTP
3136 * association from the accept queue of the endpoint. A new socket
3137 * descriptor will be returned from accept() to represent the newly
3138 * formed association.
3140 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3142 struct sctp_sock *sp;
3143 struct sctp_endpoint *ep;
3144 struct sock *newsk = NULL;
3145 struct sctp_association *asoc;
3146 long timeo;
3147 int error = 0;
3149 sctp_lock_sock(sk);
3151 sp = sctp_sk(sk);
3152 ep = sp->ep;
3154 if (!sctp_style(sk, TCP)) {
3155 error = -EOPNOTSUPP;
3156 goto out;
3159 if (!sctp_sstate(sk, LISTENING)) {
3160 error = -EINVAL;
3161 goto out;
3164 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3166 error = sctp_wait_for_accept(sk, timeo);
3167 if (error)
3168 goto out;
3170 /* We treat the list of associations on the endpoint as the accept
3171 * queue and pick the first association on the list.
3173 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3175 newsk = sp->pf->create_accept_sk(sk, asoc);
3176 if (!newsk) {
3177 error = -ENOMEM;
3178 goto out;
3181 /* Populate the fields of the newsk from the oldsk and migrate the
3182 * asoc to the newsk.
3184 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3186 out:
3187 sctp_release_sock(sk);
3188 *err = error;
3189 return newsk;
3192 /* The SCTP ioctl handler. */
3193 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3195 return -ENOIOCTLCMD;
3198 /* This is the function which gets called during socket creation to
3199 * initialized the SCTP-specific portion of the sock.
3200 * The sock structure should already be zero-filled memory.
3202 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3204 struct sctp_endpoint *ep;
3205 struct sctp_sock *sp;
3207 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3209 sp = sctp_sk(sk);
3211 /* Initialize the SCTP per socket area. */
3212 switch (sk->sk_type) {
3213 case SOCK_SEQPACKET:
3214 sp->type = SCTP_SOCKET_UDP;
3215 break;
3216 case SOCK_STREAM:
3217 sp->type = SCTP_SOCKET_TCP;
3218 break;
3219 default:
3220 return -ESOCKTNOSUPPORT;
3223 /* Initialize default send parameters. These parameters can be
3224 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3226 sp->default_stream = 0;
3227 sp->default_ppid = 0;
3228 sp->default_flags = 0;
3229 sp->default_context = 0;
3230 sp->default_timetolive = 0;
3232 sp->default_rcv_context = 0;
3233 sp->max_burst = sctp_max_burst;
3235 /* Initialize default setup parameters. These parameters
3236 * can be modified with the SCTP_INITMSG socket option or
3237 * overridden by the SCTP_INIT CMSG.
3239 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3240 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3241 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3242 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3244 /* Initialize default RTO related parameters. These parameters can
3245 * be modified for with the SCTP_RTOINFO socket option.
3247 sp->rtoinfo.srto_initial = sctp_rto_initial;
3248 sp->rtoinfo.srto_max = sctp_rto_max;
3249 sp->rtoinfo.srto_min = sctp_rto_min;
3251 /* Initialize default association related parameters. These parameters
3252 * can be modified with the SCTP_ASSOCINFO socket option.
3254 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3255 sp->assocparams.sasoc_number_peer_destinations = 0;
3256 sp->assocparams.sasoc_peer_rwnd = 0;
3257 sp->assocparams.sasoc_local_rwnd = 0;
3258 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3260 /* Initialize default event subscriptions. By default, all the
3261 * options are off.
3263 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3265 /* Default Peer Address Parameters. These defaults can
3266 * be modified via SCTP_PEER_ADDR_PARAMS
3268 sp->hbinterval = sctp_hb_interval;
3269 sp->pathmaxrxt = sctp_max_retrans_path;
3270 sp->pathmtu = 0; // allow default discovery
3271 sp->sackdelay = sctp_sack_timeout;
3272 sp->param_flags = SPP_HB_ENABLE |
3273 SPP_PMTUD_ENABLE |
3274 SPP_SACKDELAY_ENABLE;
3276 /* If enabled no SCTP message fragmentation will be performed.
3277 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3279 sp->disable_fragments = 0;
3281 /* Enable Nagle algorithm by default. */
3282 sp->nodelay = 0;
3284 /* Enable by default. */
3285 sp->v4mapped = 1;
3287 /* Auto-close idle associations after the configured
3288 * number of seconds. A value of 0 disables this
3289 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3290 * for UDP-style sockets only.
3292 sp->autoclose = 0;
3294 /* User specified fragmentation limit. */
3295 sp->user_frag = 0;
3297 sp->adaptation_ind = 0;
3299 sp->pf = sctp_get_pf_specific(sk->sk_family);
3301 /* Control variables for partial data delivery. */
3302 atomic_set(&sp->pd_mode, 0);
3303 skb_queue_head_init(&sp->pd_lobby);
3304 sp->frag_interleave = 0;
3306 /* Create a per socket endpoint structure. Even if we
3307 * change the data structure relationships, this may still
3308 * be useful for storing pre-connect address information.
3310 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3311 if (!ep)
3312 return -ENOMEM;
3314 sp->ep = ep;
3315 sp->hmac = NULL;
3317 SCTP_DBG_OBJCNT_INC(sock);
3318 return 0;
3321 /* Cleanup any SCTP per socket resources. */
3322 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3324 struct sctp_endpoint *ep;
3326 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3328 /* Release our hold on the endpoint. */
3329 ep = sctp_sk(sk)->ep;
3330 sctp_endpoint_free(ep);
3332 return 0;
3335 /* API 4.1.7 shutdown() - TCP Style Syntax
3336 * int shutdown(int socket, int how);
3338 * sd - the socket descriptor of the association to be closed.
3339 * how - Specifies the type of shutdown. The values are
3340 * as follows:
3341 * SHUT_RD
3342 * Disables further receive operations. No SCTP
3343 * protocol action is taken.
3344 * SHUT_WR
3345 * Disables further send operations, and initiates
3346 * the SCTP shutdown sequence.
3347 * SHUT_RDWR
3348 * Disables further send and receive operations
3349 * and initiates the SCTP shutdown sequence.
3351 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3353 struct sctp_endpoint *ep;
3354 struct sctp_association *asoc;
3356 if (!sctp_style(sk, TCP))
3357 return;
3359 if (how & SEND_SHUTDOWN) {
3360 ep = sctp_sk(sk)->ep;
3361 if (!list_empty(&ep->asocs)) {
3362 asoc = list_entry(ep->asocs.next,
3363 struct sctp_association, asocs);
3364 sctp_primitive_SHUTDOWN(asoc, NULL);
3369 /* 7.2.1 Association Status (SCTP_STATUS)
3371 * Applications can retrieve current status information about an
3372 * association, including association state, peer receiver window size,
3373 * number of unacked data chunks, and number of data chunks pending
3374 * receipt. This information is read-only.
3376 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3377 char __user *optval,
3378 int __user *optlen)
3380 struct sctp_status status;
3381 struct sctp_association *asoc = NULL;
3382 struct sctp_transport *transport;
3383 sctp_assoc_t associd;
3384 int retval = 0;
3386 if (len < sizeof(status)) {
3387 retval = -EINVAL;
3388 goto out;
3391 len = sizeof(status);
3392 if (copy_from_user(&status, optval, len)) {
3393 retval = -EFAULT;
3394 goto out;
3397 associd = status.sstat_assoc_id;
3398 asoc = sctp_id2assoc(sk, associd);
3399 if (!asoc) {
3400 retval = -EINVAL;
3401 goto out;
3404 transport = asoc->peer.primary_path;
3406 status.sstat_assoc_id = sctp_assoc2id(asoc);
3407 status.sstat_state = asoc->state;
3408 status.sstat_rwnd = asoc->peer.rwnd;
3409 status.sstat_unackdata = asoc->unack_data;
3411 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3412 status.sstat_instrms = asoc->c.sinit_max_instreams;
3413 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3414 status.sstat_fragmentation_point = asoc->frag_point;
3415 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3416 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3417 transport->af_specific->sockaddr_len);
3418 /* Map ipv4 address into v4-mapped-on-v6 address. */
3419 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3420 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3421 status.sstat_primary.spinfo_state = transport->state;
3422 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3423 status.sstat_primary.spinfo_srtt = transport->srtt;
3424 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3425 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3427 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3428 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3430 if (put_user(len, optlen)) {
3431 retval = -EFAULT;
3432 goto out;
3435 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3436 len, status.sstat_state, status.sstat_rwnd,
3437 status.sstat_assoc_id);
3439 if (copy_to_user(optval, &status, len)) {
3440 retval = -EFAULT;
3441 goto out;
3444 out:
3445 return (retval);
3449 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3451 * Applications can retrieve information about a specific peer address
3452 * of an association, including its reachability state, congestion
3453 * window, and retransmission timer values. This information is
3454 * read-only.
3456 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3457 char __user *optval,
3458 int __user *optlen)
3460 struct sctp_paddrinfo pinfo;
3461 struct sctp_transport *transport;
3462 int retval = 0;
3464 if (len < sizeof(pinfo)) {
3465 retval = -EINVAL;
3466 goto out;
3469 len = sizeof(pinfo);
3470 if (copy_from_user(&pinfo, optval, len)) {
3471 retval = -EFAULT;
3472 goto out;
3475 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3476 pinfo.spinfo_assoc_id);
3477 if (!transport)
3478 return -EINVAL;
3480 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3481 pinfo.spinfo_state = transport->state;
3482 pinfo.spinfo_cwnd = transport->cwnd;
3483 pinfo.spinfo_srtt = transport->srtt;
3484 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3485 pinfo.spinfo_mtu = transport->pathmtu;
3487 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3488 pinfo.spinfo_state = SCTP_ACTIVE;
3490 if (put_user(len, optlen)) {
3491 retval = -EFAULT;
3492 goto out;
3495 if (copy_to_user(optval, &pinfo, len)) {
3496 retval = -EFAULT;
3497 goto out;
3500 out:
3501 return (retval);
3504 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3506 * This option is a on/off flag. If enabled no SCTP message
3507 * fragmentation will be performed. Instead if a message being sent
3508 * exceeds the current PMTU size, the message will NOT be sent and
3509 * instead a error will be indicated to the user.
3511 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3512 char __user *optval, int __user *optlen)
3514 int val;
3516 if (len < sizeof(int))
3517 return -EINVAL;
3519 len = sizeof(int);
3520 val = (sctp_sk(sk)->disable_fragments == 1);
3521 if (put_user(len, optlen))
3522 return -EFAULT;
3523 if (copy_to_user(optval, &val, len))
3524 return -EFAULT;
3525 return 0;
3528 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3530 * This socket option is used to specify various notifications and
3531 * ancillary data the user wishes to receive.
3533 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3534 int __user *optlen)
3536 if (len < sizeof(struct sctp_event_subscribe))
3537 return -EINVAL;
3538 len = sizeof(struct sctp_event_subscribe);
3539 if (put_user(len, optlen))
3540 return -EFAULT;
3541 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3542 return -EFAULT;
3543 return 0;
3546 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3548 * This socket option is applicable to the UDP-style socket only. When
3549 * set it will cause associations that are idle for more than the
3550 * specified number of seconds to automatically close. An association
3551 * being idle is defined an association that has NOT sent or received
3552 * user data. The special value of '0' indicates that no automatic
3553 * close of any associations should be performed. The option expects an
3554 * integer defining the number of seconds of idle time before an
3555 * association is closed.
3557 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3559 /* Applicable to UDP-style socket only */
3560 if (sctp_style(sk, TCP))
3561 return -EOPNOTSUPP;
3562 if (len < sizeof(int))
3563 return -EINVAL;
3564 len = sizeof(int);
3565 if (put_user(len, optlen))
3566 return -EFAULT;
3567 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
3568 return -EFAULT;
3569 return 0;
3572 /* Helper routine to branch off an association to a new socket. */
3573 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3574 struct socket **sockp)
3576 struct sock *sk = asoc->base.sk;
3577 struct socket *sock;
3578 struct inet_sock *inetsk;
3579 struct sctp_af *af;
3580 int err = 0;
3582 /* An association cannot be branched off from an already peeled-off
3583 * socket, nor is this supported for tcp style sockets.
3585 if (!sctp_style(sk, UDP))
3586 return -EINVAL;
3588 /* Create a new socket. */
3589 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3590 if (err < 0)
3591 return err;
3593 /* Populate the fields of the newsk from the oldsk and migrate the
3594 * asoc to the newsk.
3596 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3598 /* Make peeled-off sockets more like 1-1 accepted sockets.
3599 * Set the daddr and initialize id to something more random
3601 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3602 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3603 inetsk = inet_sk(sock->sk);
3604 inetsk->id = asoc->next_tsn ^ jiffies;
3606 *sockp = sock;
3608 return err;
3611 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3613 sctp_peeloff_arg_t peeloff;
3614 struct socket *newsock;
3615 int retval = 0;
3616 struct sctp_association *asoc;
3618 if (len < sizeof(sctp_peeloff_arg_t))
3619 return -EINVAL;
3620 len = sizeof(sctp_peeloff_arg_t);
3621 if (copy_from_user(&peeloff, optval, len))
3622 return -EFAULT;
3624 asoc = sctp_id2assoc(sk, peeloff.associd);
3625 if (!asoc) {
3626 retval = -EINVAL;
3627 goto out;
3630 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3632 retval = sctp_do_peeloff(asoc, &newsock);
3633 if (retval < 0)
3634 goto out;
3636 /* Map the socket to an unused fd that can be returned to the user. */
3637 retval = sock_map_fd(newsock);
3638 if (retval < 0) {
3639 sock_release(newsock);
3640 goto out;
3643 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3644 __FUNCTION__, sk, asoc, newsock->sk, retval);
3646 /* Return the fd mapped to the new socket. */
3647 peeloff.sd = retval;
3648 if (put_user(len, optlen))
3649 return -EFAULT;
3650 if (copy_to_user(optval, &peeloff, len))
3651 retval = -EFAULT;
3653 out:
3654 return retval;
3657 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3659 * Applications can enable or disable heartbeats for any peer address of
3660 * an association, modify an address's heartbeat interval, force a
3661 * heartbeat to be sent immediately, and adjust the address's maximum
3662 * number of retransmissions sent before an address is considered
3663 * unreachable. The following structure is used to access and modify an
3664 * address's parameters:
3666 * struct sctp_paddrparams {
3667 * sctp_assoc_t spp_assoc_id;
3668 * struct sockaddr_storage spp_address;
3669 * uint32_t spp_hbinterval;
3670 * uint16_t spp_pathmaxrxt;
3671 * uint32_t spp_pathmtu;
3672 * uint32_t spp_sackdelay;
3673 * uint32_t spp_flags;
3674 * };
3676 * spp_assoc_id - (one-to-many style socket) This is filled in the
3677 * application, and identifies the association for
3678 * this query.
3679 * spp_address - This specifies which address is of interest.
3680 * spp_hbinterval - This contains the value of the heartbeat interval,
3681 * in milliseconds. If a value of zero
3682 * is present in this field then no changes are to
3683 * be made to this parameter.
3684 * spp_pathmaxrxt - This contains the maximum number of
3685 * retransmissions before this address shall be
3686 * considered unreachable. If a value of zero
3687 * is present in this field then no changes are to
3688 * be made to this parameter.
3689 * spp_pathmtu - When Path MTU discovery is disabled the value
3690 * specified here will be the "fixed" path mtu.
3691 * Note that if the spp_address field is empty
3692 * then all associations on this address will
3693 * have this fixed path mtu set upon them.
3695 * spp_sackdelay - When delayed sack is enabled, this value specifies
3696 * the number of milliseconds that sacks will be delayed
3697 * for. This value will apply to all addresses of an
3698 * association if the spp_address field is empty. Note
3699 * also, that if delayed sack is enabled and this
3700 * value is set to 0, no change is made to the last
3701 * recorded delayed sack timer value.
3703 * spp_flags - These flags are used to control various features
3704 * on an association. The flag field may contain
3705 * zero or more of the following options.
3707 * SPP_HB_ENABLE - Enable heartbeats on the
3708 * specified address. Note that if the address
3709 * field is empty all addresses for the association
3710 * have heartbeats enabled upon them.
3712 * SPP_HB_DISABLE - Disable heartbeats on the
3713 * speicifed address. Note that if the address
3714 * field is empty all addresses for the association
3715 * will have their heartbeats disabled. Note also
3716 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3717 * mutually exclusive, only one of these two should
3718 * be specified. Enabling both fields will have
3719 * undetermined results.
3721 * SPP_HB_DEMAND - Request a user initiated heartbeat
3722 * to be made immediately.
3724 * SPP_PMTUD_ENABLE - This field will enable PMTU
3725 * discovery upon the specified address. Note that
3726 * if the address feild is empty then all addresses
3727 * on the association are effected.
3729 * SPP_PMTUD_DISABLE - This field will disable PMTU
3730 * discovery upon the specified address. Note that
3731 * if the address feild is empty then all addresses
3732 * on the association are effected. Not also that
3733 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3734 * exclusive. Enabling both will have undetermined
3735 * results.
3737 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3738 * on delayed sack. The time specified in spp_sackdelay
3739 * is used to specify the sack delay for this address. Note
3740 * that if spp_address is empty then all addresses will
3741 * enable delayed sack and take on the sack delay
3742 * value specified in spp_sackdelay.
3743 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3744 * off delayed sack. If the spp_address field is blank then
3745 * delayed sack is disabled for the entire association. Note
3746 * also that this field is mutually exclusive to
3747 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3748 * results.
3750 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3751 char __user *optval, int __user *optlen)
3753 struct sctp_paddrparams params;
3754 struct sctp_transport *trans = NULL;
3755 struct sctp_association *asoc = NULL;
3756 struct sctp_sock *sp = sctp_sk(sk);
3758 if (len < sizeof(struct sctp_paddrparams))
3759 return -EINVAL;
3760 len = sizeof(struct sctp_paddrparams);
3761 if (copy_from_user(&params, optval, len))
3762 return -EFAULT;
3764 /* If an address other than INADDR_ANY is specified, and
3765 * no transport is found, then the request is invalid.
3767 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3768 trans = sctp_addr_id2transport(sk, &params.spp_address,
3769 params.spp_assoc_id);
3770 if (!trans) {
3771 SCTP_DEBUG_PRINTK("Failed no transport\n");
3772 return -EINVAL;
3776 /* Get association, if assoc_id != 0 and the socket is a one
3777 * to many style socket, and an association was not found, then
3778 * the id was invalid.
3780 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3781 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3782 SCTP_DEBUG_PRINTK("Failed no association\n");
3783 return -EINVAL;
3786 if (trans) {
3787 /* Fetch transport values. */
3788 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3789 params.spp_pathmtu = trans->pathmtu;
3790 params.spp_pathmaxrxt = trans->pathmaxrxt;
3791 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3793 /*draft-11 doesn't say what to return in spp_flags*/
3794 params.spp_flags = trans->param_flags;
3795 } else if (asoc) {
3796 /* Fetch association values. */
3797 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3798 params.spp_pathmtu = asoc->pathmtu;
3799 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3800 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3802 /*draft-11 doesn't say what to return in spp_flags*/
3803 params.spp_flags = asoc->param_flags;
3804 } else {
3805 /* Fetch socket values. */
3806 params.spp_hbinterval = sp->hbinterval;
3807 params.spp_pathmtu = sp->pathmtu;
3808 params.spp_sackdelay = sp->sackdelay;
3809 params.spp_pathmaxrxt = sp->pathmaxrxt;
3811 /*draft-11 doesn't say what to return in spp_flags*/
3812 params.spp_flags = sp->param_flags;
3815 if (copy_to_user(optval, &params, len))
3816 return -EFAULT;
3818 if (put_user(len, optlen))
3819 return -EFAULT;
3821 return 0;
3824 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3826 * This options will get or set the delayed ack timer. The time is set
3827 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3828 * endpoints default delayed ack timer value. If the assoc_id field is
3829 * non-zero, then the set or get effects the specified association.
3831 * struct sctp_assoc_value {
3832 * sctp_assoc_t assoc_id;
3833 * uint32_t assoc_value;
3834 * };
3836 * assoc_id - This parameter, indicates which association the
3837 * user is preforming an action upon. Note that if
3838 * this field's value is zero then the endpoints
3839 * default value is changed (effecting future
3840 * associations only).
3842 * assoc_value - This parameter contains the number of milliseconds
3843 * that the user is requesting the delayed ACK timer
3844 * be set to. Note that this value is defined in
3845 * the standard to be between 200 and 500 milliseconds.
3847 * Note: a value of zero will leave the value alone,
3848 * but disable SACK delay. A non-zero value will also
3849 * enable SACK delay.
3851 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3852 char __user *optval,
3853 int __user *optlen)
3855 struct sctp_assoc_value params;
3856 struct sctp_association *asoc = NULL;
3857 struct sctp_sock *sp = sctp_sk(sk);
3859 if (len < sizeof(struct sctp_assoc_value))
3860 return - EINVAL;
3862 len = sizeof(struct sctp_assoc_value);
3864 if (copy_from_user(&params, optval, len))
3865 return -EFAULT;
3867 /* Get association, if assoc_id != 0 and the socket is a one
3868 * to many style socket, and an association was not found, then
3869 * the id was invalid.
3871 asoc = sctp_id2assoc(sk, params.assoc_id);
3872 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3873 return -EINVAL;
3875 if (asoc) {
3876 /* Fetch association values. */
3877 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3878 params.assoc_value = jiffies_to_msecs(
3879 asoc->sackdelay);
3880 else
3881 params.assoc_value = 0;
3882 } else {
3883 /* Fetch socket values. */
3884 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3885 params.assoc_value = sp->sackdelay;
3886 else
3887 params.assoc_value = 0;
3890 if (copy_to_user(optval, &params, len))
3891 return -EFAULT;
3893 if (put_user(len, optlen))
3894 return -EFAULT;
3896 return 0;
3899 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3901 * Applications can specify protocol parameters for the default association
3902 * initialization. The option name argument to setsockopt() and getsockopt()
3903 * is SCTP_INITMSG.
3905 * Setting initialization parameters is effective only on an unconnected
3906 * socket (for UDP-style sockets only future associations are effected
3907 * by the change). With TCP-style sockets, this option is inherited by
3908 * sockets derived from a listener socket.
3910 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3912 if (len < sizeof(struct sctp_initmsg))
3913 return -EINVAL;
3914 len = sizeof(struct sctp_initmsg);
3915 if (put_user(len, optlen))
3916 return -EFAULT;
3917 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3918 return -EFAULT;
3919 return 0;
3922 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3923 char __user *optval,
3924 int __user *optlen)
3926 sctp_assoc_t id;
3927 struct sctp_association *asoc;
3928 struct list_head *pos;
3929 int cnt = 0;
3931 if (len < sizeof(sctp_assoc_t))
3932 return -EINVAL;
3934 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3935 return -EFAULT;
3937 /* For UDP-style sockets, id specifies the association to query. */
3938 asoc = sctp_id2assoc(sk, id);
3939 if (!asoc)
3940 return -EINVAL;
3942 list_for_each(pos, &asoc->peer.transport_addr_list) {
3943 cnt ++;
3946 return cnt;
3950 * Old API for getting list of peer addresses. Does not work for 32-bit
3951 * programs running on a 64-bit kernel
3953 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3954 char __user *optval,
3955 int __user *optlen)
3957 struct sctp_association *asoc;
3958 struct list_head *pos;
3959 int cnt = 0;
3960 struct sctp_getaddrs_old getaddrs;
3961 struct sctp_transport *from;
3962 void __user *to;
3963 union sctp_addr temp;
3964 struct sctp_sock *sp = sctp_sk(sk);
3965 int addrlen;
3967 if (len < sizeof(struct sctp_getaddrs_old))
3968 return -EINVAL;
3970 len = sizeof(struct sctp_getaddrs_old);
3972 if (copy_from_user(&getaddrs, optval, len))
3973 return -EFAULT;
3975 if (getaddrs.addr_num <= 0) return -EINVAL;
3977 /* For UDP-style sockets, id specifies the association to query. */
3978 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3979 if (!asoc)
3980 return -EINVAL;
3982 to = (void __user *)getaddrs.addrs;
3983 list_for_each(pos, &asoc->peer.transport_addr_list) {
3984 from = list_entry(pos, struct sctp_transport, transports);
3985 memcpy(&temp, &from->ipaddr, sizeof(temp));
3986 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3987 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3988 if (copy_to_user(to, &temp, addrlen))
3989 return -EFAULT;
3990 to += addrlen ;
3991 cnt ++;
3992 if (cnt >= getaddrs.addr_num) break;
3994 getaddrs.addr_num = cnt;
3995 if (put_user(len, optlen))
3996 return -EFAULT;
3997 if (copy_to_user(optval, &getaddrs, len))
3998 return -EFAULT;
4000 return 0;
4003 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4004 char __user *optval, int __user *optlen)
4006 struct sctp_association *asoc;
4007 struct list_head *pos;
4008 int cnt = 0;
4009 struct sctp_getaddrs getaddrs;
4010 struct sctp_transport *from;
4011 void __user *to;
4012 union sctp_addr temp;
4013 struct sctp_sock *sp = sctp_sk(sk);
4014 int addrlen;
4015 size_t space_left;
4016 int bytes_copied;
4018 if (len < sizeof(struct sctp_getaddrs))
4019 return -EINVAL;
4021 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4022 return -EFAULT;
4024 /* For UDP-style sockets, id specifies the association to query. */
4025 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4026 if (!asoc)
4027 return -EINVAL;
4029 to = optval + offsetof(struct sctp_getaddrs,addrs);
4030 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4032 list_for_each(pos, &asoc->peer.transport_addr_list) {
4033 from = list_entry(pos, struct sctp_transport, transports);
4034 memcpy(&temp, &from->ipaddr, sizeof(temp));
4035 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4036 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4037 if (space_left < addrlen)
4038 return -ENOMEM;
4039 if (copy_to_user(to, &temp, addrlen))
4040 return -EFAULT;
4041 to += addrlen;
4042 cnt++;
4043 space_left -= addrlen;
4046 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4047 return -EFAULT;
4048 bytes_copied = ((char __user *)to) - optval;
4049 if (put_user(bytes_copied, optlen))
4050 return -EFAULT;
4052 return 0;
4055 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4056 char __user *optval,
4057 int __user *optlen)
4059 sctp_assoc_t id;
4060 struct sctp_bind_addr *bp;
4061 struct sctp_association *asoc;
4062 struct list_head *pos, *temp;
4063 struct sctp_sockaddr_entry *addr;
4064 rwlock_t *addr_lock;
4065 int cnt = 0;
4067 if (len < sizeof(sctp_assoc_t))
4068 return -EINVAL;
4070 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4071 return -EFAULT;
4074 * For UDP-style sockets, id specifies the association to query.
4075 * If the id field is set to the value '0' then the locally bound
4076 * addresses are returned without regard to any particular
4077 * association.
4079 if (0 == id) {
4080 bp = &sctp_sk(sk)->ep->base.bind_addr;
4081 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4082 } else {
4083 asoc = sctp_id2assoc(sk, id);
4084 if (!asoc)
4085 return -EINVAL;
4086 bp = &asoc->base.bind_addr;
4087 addr_lock = &asoc->base.addr_lock;
4090 sctp_read_lock(addr_lock);
4092 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4093 * addresses from the global local address list.
4095 if (sctp_list_single_entry(&bp->address_list)) {
4096 addr = list_entry(bp->address_list.next,
4097 struct sctp_sockaddr_entry, list);
4098 if (sctp_is_any(&addr->a)) {
4099 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
4100 addr = list_entry(pos,
4101 struct sctp_sockaddr_entry,
4102 list);
4103 if ((PF_INET == sk->sk_family) &&
4104 (AF_INET6 == addr->a.sa.sa_family))
4105 continue;
4106 cnt++;
4108 } else {
4109 cnt = 1;
4111 goto done;
4114 list_for_each(pos, &bp->address_list) {
4115 cnt ++;
4118 done:
4119 sctp_read_unlock(addr_lock);
4120 return cnt;
4123 /* Helper function that copies local addresses to user and returns the number
4124 * of addresses copied.
4126 static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4127 int max_addrs, void *to,
4128 int *bytes_copied)
4130 struct list_head *pos, *next;
4131 struct sctp_sockaddr_entry *addr;
4132 union sctp_addr temp;
4133 int cnt = 0;
4134 int addrlen;
4136 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4137 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4138 if ((PF_INET == sk->sk_family) &&
4139 (AF_INET6 == addr->a.sa.sa_family))
4140 continue;
4141 memcpy(&temp, &addr->a, sizeof(temp));
4142 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4143 &temp);
4144 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4145 memcpy(to, &temp, addrlen);
4147 to += addrlen;
4148 *bytes_copied += addrlen;
4149 cnt ++;
4150 if (cnt >= max_addrs) break;
4153 return cnt;
4156 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4157 size_t space_left, int *bytes_copied)
4159 struct list_head *pos, *next;
4160 struct sctp_sockaddr_entry *addr;
4161 union sctp_addr temp;
4162 int cnt = 0;
4163 int addrlen;
4165 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4166 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4167 if ((PF_INET == sk->sk_family) &&
4168 (AF_INET6 == addr->a.sa.sa_family))
4169 continue;
4170 memcpy(&temp, &addr->a, sizeof(temp));
4171 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4172 &temp);
4173 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4174 if (space_left < addrlen)
4175 return -ENOMEM;
4176 memcpy(to, &temp, addrlen);
4178 to += addrlen;
4179 cnt ++;
4180 space_left -= addrlen;
4181 *bytes_copied += addrlen;
4184 return cnt;
4187 /* Old API for getting list of local addresses. Does not work for 32-bit
4188 * programs running on a 64-bit kernel
4190 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4191 char __user *optval, int __user *optlen)
4193 struct sctp_bind_addr *bp;
4194 struct sctp_association *asoc;
4195 struct list_head *pos;
4196 int cnt = 0;
4197 struct sctp_getaddrs_old getaddrs;
4198 struct sctp_sockaddr_entry *addr;
4199 void __user *to;
4200 union sctp_addr temp;
4201 struct sctp_sock *sp = sctp_sk(sk);
4202 int addrlen;
4203 rwlock_t *addr_lock;
4204 int err = 0;
4205 void *addrs;
4206 void *buf;
4207 int bytes_copied = 0;
4209 if (len < sizeof(struct sctp_getaddrs_old))
4210 return -EINVAL;
4212 len = sizeof(struct sctp_getaddrs_old);
4213 if (copy_from_user(&getaddrs, optval, len))
4214 return -EFAULT;
4216 if (getaddrs.addr_num <= 0) return -EINVAL;
4218 * For UDP-style sockets, id specifies the association to query.
4219 * If the id field is set to the value '0' then the locally bound
4220 * addresses are returned without regard to any particular
4221 * association.
4223 if (0 == getaddrs.assoc_id) {
4224 bp = &sctp_sk(sk)->ep->base.bind_addr;
4225 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4226 } else {
4227 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4228 if (!asoc)
4229 return -EINVAL;
4230 bp = &asoc->base.bind_addr;
4231 addr_lock = &asoc->base.addr_lock;
4234 to = getaddrs.addrs;
4236 /* Allocate space for a local instance of packed array to hold all
4237 * the data. We store addresses here first and then put write them
4238 * to the user in one shot.
4240 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4241 GFP_KERNEL);
4242 if (!addrs)
4243 return -ENOMEM;
4245 sctp_read_lock(addr_lock);
4247 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4248 * addresses from the global local address list.
4250 if (sctp_list_single_entry(&bp->address_list)) {
4251 addr = list_entry(bp->address_list.next,
4252 struct sctp_sockaddr_entry, list);
4253 if (sctp_is_any(&addr->a)) {
4254 cnt = sctp_copy_laddrs_old(sk, bp->port,
4255 getaddrs.addr_num,
4256 addrs, &bytes_copied);
4257 goto copy_getaddrs;
4261 buf = addrs;
4262 list_for_each(pos, &bp->address_list) {
4263 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4264 memcpy(&temp, &addr->a, sizeof(temp));
4265 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4266 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4267 memcpy(buf, &temp, addrlen);
4268 buf += addrlen;
4269 bytes_copied += addrlen;
4270 cnt ++;
4271 if (cnt >= getaddrs.addr_num) break;
4274 copy_getaddrs:
4275 sctp_read_unlock(addr_lock);
4277 /* copy the entire address list into the user provided space */
4278 if (copy_to_user(to, addrs, bytes_copied)) {
4279 err = -EFAULT;
4280 goto error;
4283 /* copy the leading structure back to user */
4284 getaddrs.addr_num = cnt;
4285 if (copy_to_user(optval, &getaddrs, len))
4286 err = -EFAULT;
4288 error:
4289 kfree(addrs);
4290 return err;
4293 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4294 char __user *optval, int __user *optlen)
4296 struct sctp_bind_addr *bp;
4297 struct sctp_association *asoc;
4298 struct list_head *pos;
4299 int cnt = 0;
4300 struct sctp_getaddrs getaddrs;
4301 struct sctp_sockaddr_entry *addr;
4302 void __user *to;
4303 union sctp_addr temp;
4304 struct sctp_sock *sp = sctp_sk(sk);
4305 int addrlen;
4306 rwlock_t *addr_lock;
4307 int err = 0;
4308 size_t space_left;
4309 int bytes_copied = 0;
4310 void *addrs;
4311 void *buf;
4313 if (len < sizeof(struct sctp_getaddrs))
4314 return -EINVAL;
4316 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4317 return -EFAULT;
4320 * For UDP-style sockets, id specifies the association to query.
4321 * If the id field is set to the value '0' then the locally bound
4322 * addresses are returned without regard to any particular
4323 * association.
4325 if (0 == getaddrs.assoc_id) {
4326 bp = &sctp_sk(sk)->ep->base.bind_addr;
4327 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4328 } else {
4329 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4330 if (!asoc)
4331 return -EINVAL;
4332 bp = &asoc->base.bind_addr;
4333 addr_lock = &asoc->base.addr_lock;
4336 to = optval + offsetof(struct sctp_getaddrs,addrs);
4337 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4339 addrs = kmalloc(space_left, GFP_KERNEL);
4340 if (!addrs)
4341 return -ENOMEM;
4343 sctp_read_lock(addr_lock);
4345 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4346 * addresses from the global local address list.
4348 if (sctp_list_single_entry(&bp->address_list)) {
4349 addr = list_entry(bp->address_list.next,
4350 struct sctp_sockaddr_entry, list);
4351 if (sctp_is_any(&addr->a)) {
4352 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4353 space_left, &bytes_copied);
4354 if (cnt < 0) {
4355 err = cnt;
4356 goto error;
4358 goto copy_getaddrs;
4362 buf = addrs;
4363 list_for_each(pos, &bp->address_list) {
4364 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4365 memcpy(&temp, &addr->a, sizeof(temp));
4366 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4367 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4368 if (space_left < addrlen) {
4369 err = -ENOMEM; /*fixme: right error?*/
4370 goto error;
4372 memcpy(buf, &temp, addrlen);
4373 buf += addrlen;
4374 bytes_copied += addrlen;
4375 cnt ++;
4376 space_left -= addrlen;
4379 copy_getaddrs:
4380 sctp_read_unlock(addr_lock);
4382 if (copy_to_user(to, addrs, bytes_copied)) {
4383 err = -EFAULT;
4384 goto error;
4386 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4387 err = -EFAULT;
4388 goto error;
4390 if (put_user(bytes_copied, optlen))
4391 err = -EFAULT;
4392 error:
4393 kfree(addrs);
4394 return err;
4397 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4399 * Requests that the local SCTP stack use the enclosed peer address as
4400 * the association primary. The enclosed address must be one of the
4401 * association peer's addresses.
4403 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4404 char __user *optval, int __user *optlen)
4406 struct sctp_prim prim;
4407 struct sctp_association *asoc;
4408 struct sctp_sock *sp = sctp_sk(sk);
4410 if (len < sizeof(struct sctp_prim))
4411 return -EINVAL;
4413 len = sizeof(struct sctp_prim);
4415 if (copy_from_user(&prim, optval, len))
4416 return -EFAULT;
4418 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4419 if (!asoc)
4420 return -EINVAL;
4422 if (!asoc->peer.primary_path)
4423 return -ENOTCONN;
4425 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4426 asoc->peer.primary_path->af_specific->sockaddr_len);
4428 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4429 (union sctp_addr *)&prim.ssp_addr);
4431 if (put_user(len, optlen))
4432 return -EFAULT;
4433 if (copy_to_user(optval, &prim, len))
4434 return -EFAULT;
4436 return 0;
4440 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4442 * Requests that the local endpoint set the specified Adaptation Layer
4443 * Indication parameter for all future INIT and INIT-ACK exchanges.
4445 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4446 char __user *optval, int __user *optlen)
4448 struct sctp_setadaptation adaptation;
4450 if (len < sizeof(struct sctp_setadaptation))
4451 return -EINVAL;
4453 len = sizeof(struct sctp_setadaptation);
4455 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4457 if (put_user(len, optlen))
4458 return -EFAULT;
4459 if (copy_to_user(optval, &adaptation, len))
4460 return -EFAULT;
4462 return 0;
4467 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4469 * Applications that wish to use the sendto() system call may wish to
4470 * specify a default set of parameters that would normally be supplied
4471 * through the inclusion of ancillary data. This socket option allows
4472 * such an application to set the default sctp_sndrcvinfo structure.
4475 * The application that wishes to use this socket option simply passes
4476 * in to this call the sctp_sndrcvinfo structure defined in Section
4477 * 5.2.2) The input parameters accepted by this call include
4478 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4479 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4480 * to this call if the caller is using the UDP model.
4482 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4484 static int sctp_getsockopt_default_send_param(struct sock *sk,
4485 int len, char __user *optval,
4486 int __user *optlen)
4488 struct sctp_sndrcvinfo info;
4489 struct sctp_association *asoc;
4490 struct sctp_sock *sp = sctp_sk(sk);
4492 if (len < sizeof(struct sctp_sndrcvinfo))
4493 return -EINVAL;
4495 len = sizeof(struct sctp_sndrcvinfo);
4497 if (copy_from_user(&info, optval, len))
4498 return -EFAULT;
4500 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4501 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4502 return -EINVAL;
4504 if (asoc) {
4505 info.sinfo_stream = asoc->default_stream;
4506 info.sinfo_flags = asoc->default_flags;
4507 info.sinfo_ppid = asoc->default_ppid;
4508 info.sinfo_context = asoc->default_context;
4509 info.sinfo_timetolive = asoc->default_timetolive;
4510 } else {
4511 info.sinfo_stream = sp->default_stream;
4512 info.sinfo_flags = sp->default_flags;
4513 info.sinfo_ppid = sp->default_ppid;
4514 info.sinfo_context = sp->default_context;
4515 info.sinfo_timetolive = sp->default_timetolive;
4518 if (put_user(len, optlen))
4519 return -EFAULT;
4520 if (copy_to_user(optval, &info, len))
4521 return -EFAULT;
4523 return 0;
4528 * 7.1.5 SCTP_NODELAY
4530 * Turn on/off any Nagle-like algorithm. This means that packets are
4531 * generally sent as soon as possible and no unnecessary delays are
4532 * introduced, at the cost of more packets in the network. Expects an
4533 * integer boolean flag.
4536 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4537 char __user *optval, int __user *optlen)
4539 int val;
4541 if (len < sizeof(int))
4542 return -EINVAL;
4544 len = sizeof(int);
4545 val = (sctp_sk(sk)->nodelay == 1);
4546 if (put_user(len, optlen))
4547 return -EFAULT;
4548 if (copy_to_user(optval, &val, len))
4549 return -EFAULT;
4550 return 0;
4555 * 7.1.1 SCTP_RTOINFO
4557 * The protocol parameters used to initialize and bound retransmission
4558 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4559 * and modify these parameters.
4560 * All parameters are time values, in milliseconds. A value of 0, when
4561 * modifying the parameters, indicates that the current value should not
4562 * be changed.
4565 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4566 char __user *optval,
4567 int __user *optlen) {
4568 struct sctp_rtoinfo rtoinfo;
4569 struct sctp_association *asoc;
4571 if (len < sizeof (struct sctp_rtoinfo))
4572 return -EINVAL;
4574 len = sizeof(struct sctp_rtoinfo);
4576 if (copy_from_user(&rtoinfo, optval, len))
4577 return -EFAULT;
4579 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4581 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4582 return -EINVAL;
4584 /* Values corresponding to the specific association. */
4585 if (asoc) {
4586 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4587 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4588 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4589 } else {
4590 /* Values corresponding to the endpoint. */
4591 struct sctp_sock *sp = sctp_sk(sk);
4593 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4594 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4595 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4598 if (put_user(len, optlen))
4599 return -EFAULT;
4601 if (copy_to_user(optval, &rtoinfo, len))
4602 return -EFAULT;
4604 return 0;
4609 * 7.1.2 SCTP_ASSOCINFO
4611 * This option is used to tune the maximum retransmission attempts
4612 * of the association.
4613 * Returns an error if the new association retransmission value is
4614 * greater than the sum of the retransmission value of the peer.
4615 * See [SCTP] for more information.
4618 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4619 char __user *optval,
4620 int __user *optlen)
4623 struct sctp_assocparams assocparams;
4624 struct sctp_association *asoc;
4625 struct list_head *pos;
4626 int cnt = 0;
4628 if (len < sizeof (struct sctp_assocparams))
4629 return -EINVAL;
4631 len = sizeof(struct sctp_assocparams);
4633 if (copy_from_user(&assocparams, optval, len))
4634 return -EFAULT;
4636 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4638 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4639 return -EINVAL;
4641 /* Values correspoinding to the specific association */
4642 if (asoc) {
4643 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4644 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4645 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4646 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4647 * 1000) +
4648 (asoc->cookie_life.tv_usec
4649 / 1000);
4651 list_for_each(pos, &asoc->peer.transport_addr_list) {
4652 cnt ++;
4655 assocparams.sasoc_number_peer_destinations = cnt;
4656 } else {
4657 /* Values corresponding to the endpoint */
4658 struct sctp_sock *sp = sctp_sk(sk);
4660 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4661 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4662 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4663 assocparams.sasoc_cookie_life =
4664 sp->assocparams.sasoc_cookie_life;
4665 assocparams.sasoc_number_peer_destinations =
4666 sp->assocparams.
4667 sasoc_number_peer_destinations;
4670 if (put_user(len, optlen))
4671 return -EFAULT;
4673 if (copy_to_user(optval, &assocparams, len))
4674 return -EFAULT;
4676 return 0;
4680 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4682 * This socket option is a boolean flag which turns on or off mapped V4
4683 * addresses. If this option is turned on and the socket is type
4684 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4685 * If this option is turned off, then no mapping will be done of V4
4686 * addresses and a user will receive both PF_INET6 and PF_INET type
4687 * addresses on the socket.
4689 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4690 char __user *optval, int __user *optlen)
4692 int val;
4693 struct sctp_sock *sp = sctp_sk(sk);
4695 if (len < sizeof(int))
4696 return -EINVAL;
4698 len = sizeof(int);
4699 val = sp->v4mapped;
4700 if (put_user(len, optlen))
4701 return -EFAULT;
4702 if (copy_to_user(optval, &val, len))
4703 return -EFAULT;
4705 return 0;
4709 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4710 * (chapter and verse is quoted at sctp_setsockopt_context())
4712 static int sctp_getsockopt_context(struct sock *sk, int len,
4713 char __user *optval, int __user *optlen)
4715 struct sctp_assoc_value params;
4716 struct sctp_sock *sp;
4717 struct sctp_association *asoc;
4719 if (len < sizeof(struct sctp_assoc_value))
4720 return -EINVAL;
4722 len = sizeof(struct sctp_assoc_value);
4724 if (copy_from_user(&params, optval, len))
4725 return -EFAULT;
4727 sp = sctp_sk(sk);
4729 if (params.assoc_id != 0) {
4730 asoc = sctp_id2assoc(sk, params.assoc_id);
4731 if (!asoc)
4732 return -EINVAL;
4733 params.assoc_value = asoc->default_rcv_context;
4734 } else {
4735 params.assoc_value = sp->default_rcv_context;
4738 if (put_user(len, optlen))
4739 return -EFAULT;
4740 if (copy_to_user(optval, &params, len))
4741 return -EFAULT;
4743 return 0;
4747 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4749 * This socket option specifies the maximum size to put in any outgoing
4750 * SCTP chunk. If a message is larger than this size it will be
4751 * fragmented by SCTP into the specified size. Note that the underlying
4752 * SCTP implementation may fragment into smaller sized chunks when the
4753 * PMTU of the underlying association is smaller than the value set by
4754 * the user.
4756 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4757 char __user *optval, int __user *optlen)
4759 int val;
4761 if (len < sizeof(int))
4762 return -EINVAL;
4764 len = sizeof(int);
4766 val = sctp_sk(sk)->user_frag;
4767 if (put_user(len, optlen))
4768 return -EFAULT;
4769 if (copy_to_user(optval, &val, len))
4770 return -EFAULT;
4772 return 0;
4776 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4777 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4779 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4780 char __user *optval, int __user *optlen)
4782 int val;
4784 if (len < sizeof(int))
4785 return -EINVAL;
4787 len = sizeof(int);
4789 val = sctp_sk(sk)->frag_interleave;
4790 if (put_user(len, optlen))
4791 return -EFAULT;
4792 if (copy_to_user(optval, &val, len))
4793 return -EFAULT;
4795 return 0;
4799 * 7.1.25. Set or Get the sctp partial delivery point
4800 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4802 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4803 char __user *optval,
4804 int __user *optlen)
4806 u32 val;
4808 if (len < sizeof(u32))
4809 return -EINVAL;
4811 len = sizeof(u32);
4813 val = sctp_sk(sk)->pd_point;
4814 if (put_user(len, optlen))
4815 return -EFAULT;
4816 if (copy_to_user(optval, &val, len))
4817 return -EFAULT;
4819 return -ENOTSUPP;
4823 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4824 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4826 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4827 char __user *optval,
4828 int __user *optlen)
4830 int val;
4832 if (len < sizeof(int))
4833 return -EINVAL;
4835 len = sizeof(int);
4837 val = sctp_sk(sk)->max_burst;
4838 if (put_user(len, optlen))
4839 return -EFAULT;
4840 if (copy_to_user(optval, &val, len))
4841 return -EFAULT;
4843 return -ENOTSUPP;
4846 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4847 char __user *optval, int __user *optlen)
4849 int retval = 0;
4850 int len;
4852 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4853 sk, optname);
4855 /* I can hardly begin to describe how wrong this is. This is
4856 * so broken as to be worse than useless. The API draft
4857 * REALLY is NOT helpful here... I am not convinced that the
4858 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4859 * are at all well-founded.
4861 if (level != SOL_SCTP) {
4862 struct sctp_af *af = sctp_sk(sk)->pf->af;
4864 retval = af->getsockopt(sk, level, optname, optval, optlen);
4865 return retval;
4868 if (get_user(len, optlen))
4869 return -EFAULT;
4871 sctp_lock_sock(sk);
4873 switch (optname) {
4874 case SCTP_STATUS:
4875 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4876 break;
4877 case SCTP_DISABLE_FRAGMENTS:
4878 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4879 optlen);
4880 break;
4881 case SCTP_EVENTS:
4882 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4883 break;
4884 case SCTP_AUTOCLOSE:
4885 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4886 break;
4887 case SCTP_SOCKOPT_PEELOFF:
4888 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4889 break;
4890 case SCTP_PEER_ADDR_PARAMS:
4891 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4892 optlen);
4893 break;
4894 case SCTP_DELAYED_ACK_TIME:
4895 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4896 optlen);
4897 break;
4898 case SCTP_INITMSG:
4899 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4900 break;
4901 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4902 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4903 optlen);
4904 break;
4905 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4906 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4907 optlen);
4908 break;
4909 case SCTP_GET_PEER_ADDRS_OLD:
4910 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4911 optlen);
4912 break;
4913 case SCTP_GET_LOCAL_ADDRS_OLD:
4914 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4915 optlen);
4916 break;
4917 case SCTP_GET_PEER_ADDRS:
4918 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4919 optlen);
4920 break;
4921 case SCTP_GET_LOCAL_ADDRS:
4922 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4923 optlen);
4924 break;
4925 case SCTP_DEFAULT_SEND_PARAM:
4926 retval = sctp_getsockopt_default_send_param(sk, len,
4927 optval, optlen);
4928 break;
4929 case SCTP_PRIMARY_ADDR:
4930 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4931 break;
4932 case SCTP_NODELAY:
4933 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4934 break;
4935 case SCTP_RTOINFO:
4936 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4937 break;
4938 case SCTP_ASSOCINFO:
4939 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4940 break;
4941 case SCTP_I_WANT_MAPPED_V4_ADDR:
4942 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4943 break;
4944 case SCTP_MAXSEG:
4945 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4946 break;
4947 case SCTP_GET_PEER_ADDR_INFO:
4948 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4949 optlen);
4950 break;
4951 case SCTP_ADAPTATION_LAYER:
4952 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
4953 optlen);
4954 break;
4955 case SCTP_CONTEXT:
4956 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4957 break;
4958 case SCTP_FRAGMENT_INTERLEAVE:
4959 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4960 optlen);
4961 break;
4962 case SCTP_PARTIAL_DELIVERY_POINT:
4963 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4964 optlen);
4965 break;
4966 case SCTP_MAX_BURST:
4967 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4968 break;
4969 default:
4970 retval = -ENOPROTOOPT;
4971 break;
4974 sctp_release_sock(sk);
4975 return retval;
4978 static void sctp_hash(struct sock *sk)
4980 /* STUB */
4983 static void sctp_unhash(struct sock *sk)
4985 /* STUB */
4988 /* Check if port is acceptable. Possibly find first available port.
4990 * The port hash table (contained in the 'global' SCTP protocol storage
4991 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4992 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4993 * list (the list number is the port number hashed out, so as you
4994 * would expect from a hash function, all the ports in a given list have
4995 * such a number that hashes out to the same list number; you were
4996 * expecting that, right?); so each list has a set of ports, with a
4997 * link to the socket (struct sock) that uses it, the port number and
4998 * a fastreuse flag (FIXME: NPI ipg).
5000 static struct sctp_bind_bucket *sctp_bucket_create(
5001 struct sctp_bind_hashbucket *head, unsigned short snum);
5003 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5005 struct sctp_bind_hashbucket *head; /* hash list */
5006 struct sctp_bind_bucket *pp; /* hash list port iterator */
5007 unsigned short snum;
5008 int ret;
5010 snum = ntohs(addr->v4.sin_port);
5012 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5013 sctp_local_bh_disable();
5015 if (snum == 0) {
5016 /* Search for an available port.
5018 * 'sctp_port_rover' was the last port assigned, so
5019 * we start to search from 'sctp_port_rover +
5020 * 1'. What we do is first check if port 'rover' is
5021 * already in the hash table; if not, we use that; if
5022 * it is, we try next.
5024 int low = sysctl_local_port_range[0];
5025 int high = sysctl_local_port_range[1];
5026 int remaining = (high - low) + 1;
5027 int rover;
5028 int index;
5030 sctp_spin_lock(&sctp_port_alloc_lock);
5031 rover = sctp_port_rover;
5032 do {
5033 rover++;
5034 if ((rover < low) || (rover > high))
5035 rover = low;
5036 index = sctp_phashfn(rover);
5037 head = &sctp_port_hashtable[index];
5038 sctp_spin_lock(&head->lock);
5039 for (pp = head->chain; pp; pp = pp->next)
5040 if (pp->port == rover)
5041 goto next;
5042 break;
5043 next:
5044 sctp_spin_unlock(&head->lock);
5045 } while (--remaining > 0);
5046 sctp_port_rover = rover;
5047 sctp_spin_unlock(&sctp_port_alloc_lock);
5049 /* Exhausted local port range during search? */
5050 ret = 1;
5051 if (remaining <= 0)
5052 goto fail;
5054 /* OK, here is the one we will use. HEAD (the port
5055 * hash table list entry) is non-NULL and we hold it's
5056 * mutex.
5058 snum = rover;
5059 } else {
5060 /* We are given an specific port number; we verify
5061 * that it is not being used. If it is used, we will
5062 * exahust the search in the hash list corresponding
5063 * to the port number (snum) - we detect that with the
5064 * port iterator, pp being NULL.
5066 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5067 sctp_spin_lock(&head->lock);
5068 for (pp = head->chain; pp; pp = pp->next) {
5069 if (pp->port == snum)
5070 goto pp_found;
5073 pp = NULL;
5074 goto pp_not_found;
5075 pp_found:
5076 if (!hlist_empty(&pp->owner)) {
5077 /* We had a port hash table hit - there is an
5078 * available port (pp != NULL) and it is being
5079 * used by other socket (pp->owner not empty); that other
5080 * socket is going to be sk2.
5082 int reuse = sk->sk_reuse;
5083 struct sock *sk2;
5084 struct hlist_node *node;
5086 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5087 if (pp->fastreuse && sk->sk_reuse &&
5088 sk->sk_state != SCTP_SS_LISTENING)
5089 goto success;
5091 /* Run through the list of sockets bound to the port
5092 * (pp->port) [via the pointers bind_next and
5093 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5094 * we get the endpoint they describe and run through
5095 * the endpoint's list of IP (v4 or v6) addresses,
5096 * comparing each of the addresses with the address of
5097 * the socket sk. If we find a match, then that means
5098 * that this port/socket (sk) combination are already
5099 * in an endpoint.
5101 sk_for_each_bound(sk2, node, &pp->owner) {
5102 struct sctp_endpoint *ep2;
5103 ep2 = sctp_sk(sk2)->ep;
5105 if (reuse && sk2->sk_reuse &&
5106 sk2->sk_state != SCTP_SS_LISTENING)
5107 continue;
5109 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
5110 sctp_sk(sk))) {
5111 ret = (long)sk2;
5112 goto fail_unlock;
5115 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5117 pp_not_found:
5118 /* If there was a hash table miss, create a new port. */
5119 ret = 1;
5120 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5121 goto fail_unlock;
5123 /* In either case (hit or miss), make sure fastreuse is 1 only
5124 * if sk->sk_reuse is too (that is, if the caller requested
5125 * SO_REUSEADDR on this socket -sk-).
5127 if (hlist_empty(&pp->owner)) {
5128 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5129 pp->fastreuse = 1;
5130 else
5131 pp->fastreuse = 0;
5132 } else if (pp->fastreuse &&
5133 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5134 pp->fastreuse = 0;
5136 /* We are set, so fill up all the data in the hash table
5137 * entry, tie the socket list information with the rest of the
5138 * sockets FIXME: Blurry, NPI (ipg).
5140 success:
5141 if (!sctp_sk(sk)->bind_hash) {
5142 inet_sk(sk)->num = snum;
5143 sk_add_bind_node(sk, &pp->owner);
5144 sctp_sk(sk)->bind_hash = pp;
5146 ret = 0;
5148 fail_unlock:
5149 sctp_spin_unlock(&head->lock);
5151 fail:
5152 sctp_local_bh_enable();
5153 return ret;
5156 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5157 * port is requested.
5159 static int sctp_get_port(struct sock *sk, unsigned short snum)
5161 long ret;
5162 union sctp_addr addr;
5163 struct sctp_af *af = sctp_sk(sk)->pf->af;
5165 /* Set up a dummy address struct from the sk. */
5166 af->from_sk(&addr, sk);
5167 addr.v4.sin_port = htons(snum);
5169 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5170 ret = sctp_get_port_local(sk, &addr);
5172 return (ret ? 1 : 0);
5176 * 3.1.3 listen() - UDP Style Syntax
5178 * By default, new associations are not accepted for UDP style sockets.
5179 * An application uses listen() to mark a socket as being able to
5180 * accept new associations.
5182 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5184 struct sctp_sock *sp = sctp_sk(sk);
5185 struct sctp_endpoint *ep = sp->ep;
5187 /* Only UDP style sockets that are not peeled off are allowed to
5188 * listen().
5190 if (!sctp_style(sk, UDP))
5191 return -EINVAL;
5193 /* If backlog is zero, disable listening. */
5194 if (!backlog) {
5195 if (sctp_sstate(sk, CLOSED))
5196 return 0;
5198 sctp_unhash_endpoint(ep);
5199 sk->sk_state = SCTP_SS_CLOSED;
5202 /* Return if we are already listening. */
5203 if (sctp_sstate(sk, LISTENING))
5204 return 0;
5207 * If a bind() or sctp_bindx() is not called prior to a listen()
5208 * call that allows new associations to be accepted, the system
5209 * picks an ephemeral port and will choose an address set equivalent
5210 * to binding with a wildcard address.
5212 * This is not currently spelled out in the SCTP sockets
5213 * extensions draft, but follows the practice as seen in TCP
5214 * sockets.
5216 * Additionally, turn off fastreuse flag since we are not listening
5218 sk->sk_state = SCTP_SS_LISTENING;
5219 if (!ep->base.bind_addr.port) {
5220 if (sctp_autobind(sk))
5221 return -EAGAIN;
5222 } else
5223 sctp_sk(sk)->bind_hash->fastreuse = 0;
5225 sctp_hash_endpoint(ep);
5226 return 0;
5230 * 4.1.3 listen() - TCP Style Syntax
5232 * Applications uses listen() to ready the SCTP endpoint for accepting
5233 * inbound associations.
5235 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5237 struct sctp_sock *sp = sctp_sk(sk);
5238 struct sctp_endpoint *ep = sp->ep;
5240 /* If backlog is zero, disable listening. */
5241 if (!backlog) {
5242 if (sctp_sstate(sk, CLOSED))
5243 return 0;
5245 sctp_unhash_endpoint(ep);
5246 sk->sk_state = SCTP_SS_CLOSED;
5249 if (sctp_sstate(sk, LISTENING))
5250 return 0;
5253 * If a bind() or sctp_bindx() is not called prior to a listen()
5254 * call that allows new associations to be accepted, the system
5255 * picks an ephemeral port and will choose an address set equivalent
5256 * to binding with a wildcard address.
5258 * This is not currently spelled out in the SCTP sockets
5259 * extensions draft, but follows the practice as seen in TCP
5260 * sockets.
5262 sk->sk_state = SCTP_SS_LISTENING;
5263 if (!ep->base.bind_addr.port) {
5264 if (sctp_autobind(sk))
5265 return -EAGAIN;
5266 } else
5267 sctp_sk(sk)->bind_hash->fastreuse = 0;
5269 sk->sk_max_ack_backlog = backlog;
5270 sctp_hash_endpoint(ep);
5271 return 0;
5275 * Move a socket to LISTENING state.
5277 int sctp_inet_listen(struct socket *sock, int backlog)
5279 struct sock *sk = sock->sk;
5280 struct crypto_hash *tfm = NULL;
5281 int err = -EINVAL;
5283 if (unlikely(backlog < 0))
5284 goto out;
5286 sctp_lock_sock(sk);
5288 if (sock->state != SS_UNCONNECTED)
5289 goto out;
5291 /* Allocate HMAC for generating cookie. */
5292 if (sctp_hmac_alg) {
5293 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5294 if (IS_ERR(tfm)) {
5295 if (net_ratelimit()) {
5296 printk(KERN_INFO
5297 "SCTP: failed to load transform for %s: %ld\n",
5298 sctp_hmac_alg, PTR_ERR(tfm));
5300 err = -ENOSYS;
5301 goto out;
5305 switch (sock->type) {
5306 case SOCK_SEQPACKET:
5307 err = sctp_seqpacket_listen(sk, backlog);
5308 break;
5309 case SOCK_STREAM:
5310 err = sctp_stream_listen(sk, backlog);
5311 break;
5312 default:
5313 break;
5316 if (err)
5317 goto cleanup;
5319 /* Store away the transform reference. */
5320 sctp_sk(sk)->hmac = tfm;
5321 out:
5322 sctp_release_sock(sk);
5323 return err;
5324 cleanup:
5325 crypto_free_hash(tfm);
5326 goto out;
5330 * This function is done by modeling the current datagram_poll() and the
5331 * tcp_poll(). Note that, based on these implementations, we don't
5332 * lock the socket in this function, even though it seems that,
5333 * ideally, locking or some other mechanisms can be used to ensure
5334 * the integrity of the counters (sndbuf and wmem_alloc) used
5335 * in this place. We assume that we don't need locks either until proven
5336 * otherwise.
5338 * Another thing to note is that we include the Async I/O support
5339 * here, again, by modeling the current TCP/UDP code. We don't have
5340 * a good way to test with it yet.
5342 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5344 struct sock *sk = sock->sk;
5345 struct sctp_sock *sp = sctp_sk(sk);
5346 unsigned int mask;
5348 poll_wait(file, sk->sk_sleep, wait);
5350 /* A TCP-style listening socket becomes readable when the accept queue
5351 * is not empty.
5353 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5354 return (!list_empty(&sp->ep->asocs)) ?
5355 (POLLIN | POLLRDNORM) : 0;
5357 mask = 0;
5359 /* Is there any exceptional events? */
5360 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5361 mask |= POLLERR;
5362 if (sk->sk_shutdown & RCV_SHUTDOWN)
5363 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
5364 if (sk->sk_shutdown == SHUTDOWN_MASK)
5365 mask |= POLLHUP;
5367 /* Is it readable? Reconsider this code with TCP-style support. */
5368 if (!skb_queue_empty(&sk->sk_receive_queue))
5369 mask |= POLLIN | POLLRDNORM;
5371 /* The association is either gone or not ready. */
5372 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5373 return mask;
5375 /* Is it writable? */
5376 if (sctp_writeable(sk)) {
5377 mask |= POLLOUT | POLLWRNORM;
5378 } else {
5379 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5381 * Since the socket is not locked, the buffer
5382 * might be made available after the writeable check and
5383 * before the bit is set. This could cause a lost I/O
5384 * signal. tcp_poll() has a race breaker for this race
5385 * condition. Based on their implementation, we put
5386 * in the following code to cover it as well.
5388 if (sctp_writeable(sk))
5389 mask |= POLLOUT | POLLWRNORM;
5391 return mask;
5394 /********************************************************************
5395 * 2nd Level Abstractions
5396 ********************************************************************/
5398 static struct sctp_bind_bucket *sctp_bucket_create(
5399 struct sctp_bind_hashbucket *head, unsigned short snum)
5401 struct sctp_bind_bucket *pp;
5403 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5404 SCTP_DBG_OBJCNT_INC(bind_bucket);
5405 if (pp) {
5406 pp->port = snum;
5407 pp->fastreuse = 0;
5408 INIT_HLIST_HEAD(&pp->owner);
5409 if ((pp->next = head->chain) != NULL)
5410 pp->next->pprev = &pp->next;
5411 head->chain = pp;
5412 pp->pprev = &head->chain;
5414 return pp;
5417 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5418 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5420 if (pp && hlist_empty(&pp->owner)) {
5421 if (pp->next)
5422 pp->next->pprev = pp->pprev;
5423 *(pp->pprev) = pp->next;
5424 kmem_cache_free(sctp_bucket_cachep, pp);
5425 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5429 /* Release this socket's reference to a local port. */
5430 static inline void __sctp_put_port(struct sock *sk)
5432 struct sctp_bind_hashbucket *head =
5433 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5434 struct sctp_bind_bucket *pp;
5436 sctp_spin_lock(&head->lock);
5437 pp = sctp_sk(sk)->bind_hash;
5438 __sk_del_bind_node(sk);
5439 sctp_sk(sk)->bind_hash = NULL;
5440 inet_sk(sk)->num = 0;
5441 sctp_bucket_destroy(pp);
5442 sctp_spin_unlock(&head->lock);
5445 void sctp_put_port(struct sock *sk)
5447 sctp_local_bh_disable();
5448 __sctp_put_port(sk);
5449 sctp_local_bh_enable();
5453 * The system picks an ephemeral port and choose an address set equivalent
5454 * to binding with a wildcard address.
5455 * One of those addresses will be the primary address for the association.
5456 * This automatically enables the multihoming capability of SCTP.
5458 static int sctp_autobind(struct sock *sk)
5460 union sctp_addr autoaddr;
5461 struct sctp_af *af;
5462 __be16 port;
5464 /* Initialize a local sockaddr structure to INADDR_ANY. */
5465 af = sctp_sk(sk)->pf->af;
5467 port = htons(inet_sk(sk)->num);
5468 af->inaddr_any(&autoaddr, port);
5470 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5473 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5475 * From RFC 2292
5476 * 4.2 The cmsghdr Structure *
5478 * When ancillary data is sent or received, any number of ancillary data
5479 * objects can be specified by the msg_control and msg_controllen members of
5480 * the msghdr structure, because each object is preceded by
5481 * a cmsghdr structure defining the object's length (the cmsg_len member).
5482 * Historically Berkeley-derived implementations have passed only one object
5483 * at a time, but this API allows multiple objects to be
5484 * passed in a single call to sendmsg() or recvmsg(). The following example
5485 * shows two ancillary data objects in a control buffer.
5487 * |<--------------------------- msg_controllen -------------------------->|
5488 * | |
5490 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5492 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5493 * | | |
5495 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5497 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5498 * | | | | |
5500 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5501 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5503 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5505 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5509 * msg_control
5510 * points here
5512 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5513 sctp_cmsgs_t *cmsgs)
5515 struct cmsghdr *cmsg;
5517 for (cmsg = CMSG_FIRSTHDR(msg);
5518 cmsg != NULL;
5519 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5520 if (!CMSG_OK(msg, cmsg))
5521 return -EINVAL;
5523 /* Should we parse this header or ignore? */
5524 if (cmsg->cmsg_level != IPPROTO_SCTP)
5525 continue;
5527 /* Strictly check lengths following example in SCM code. */
5528 switch (cmsg->cmsg_type) {
5529 case SCTP_INIT:
5530 /* SCTP Socket API Extension
5531 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5533 * This cmsghdr structure provides information for
5534 * initializing new SCTP associations with sendmsg().
5535 * The SCTP_INITMSG socket option uses this same data
5536 * structure. This structure is not used for
5537 * recvmsg().
5539 * cmsg_level cmsg_type cmsg_data[]
5540 * ------------ ------------ ----------------------
5541 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5543 if (cmsg->cmsg_len !=
5544 CMSG_LEN(sizeof(struct sctp_initmsg)))
5545 return -EINVAL;
5546 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5547 break;
5549 case SCTP_SNDRCV:
5550 /* SCTP Socket API Extension
5551 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5553 * This cmsghdr structure specifies SCTP options for
5554 * sendmsg() and describes SCTP header information
5555 * about a received message through recvmsg().
5557 * cmsg_level cmsg_type cmsg_data[]
5558 * ------------ ------------ ----------------------
5559 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5561 if (cmsg->cmsg_len !=
5562 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5563 return -EINVAL;
5565 cmsgs->info =
5566 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5568 /* Minimally, validate the sinfo_flags. */
5569 if (cmsgs->info->sinfo_flags &
5570 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5571 SCTP_ABORT | SCTP_EOF))
5572 return -EINVAL;
5573 break;
5575 default:
5576 return -EINVAL;
5579 return 0;
5583 * Wait for a packet..
5584 * Note: This function is the same function as in core/datagram.c
5585 * with a few modifications to make lksctp work.
5587 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5589 int error;
5590 DEFINE_WAIT(wait);
5592 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5594 /* Socket errors? */
5595 error = sock_error(sk);
5596 if (error)
5597 goto out;
5599 if (!skb_queue_empty(&sk->sk_receive_queue))
5600 goto ready;
5602 /* Socket shut down? */
5603 if (sk->sk_shutdown & RCV_SHUTDOWN)
5604 goto out;
5606 /* Sequenced packets can come disconnected. If so we report the
5607 * problem.
5609 error = -ENOTCONN;
5611 /* Is there a good reason to think that we may receive some data? */
5612 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5613 goto out;
5615 /* Handle signals. */
5616 if (signal_pending(current))
5617 goto interrupted;
5619 /* Let another process have a go. Since we are going to sleep
5620 * anyway. Note: This may cause odd behaviors if the message
5621 * does not fit in the user's buffer, but this seems to be the
5622 * only way to honor MSG_DONTWAIT realistically.
5624 sctp_release_sock(sk);
5625 *timeo_p = schedule_timeout(*timeo_p);
5626 sctp_lock_sock(sk);
5628 ready:
5629 finish_wait(sk->sk_sleep, &wait);
5630 return 0;
5632 interrupted:
5633 error = sock_intr_errno(*timeo_p);
5635 out:
5636 finish_wait(sk->sk_sleep, &wait);
5637 *err = error;
5638 return error;
5641 /* Receive a datagram.
5642 * Note: This is pretty much the same routine as in core/datagram.c
5643 * with a few changes to make lksctp work.
5645 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5646 int noblock, int *err)
5648 int error;
5649 struct sk_buff *skb;
5650 long timeo;
5652 timeo = sock_rcvtimeo(sk, noblock);
5654 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5655 timeo, MAX_SCHEDULE_TIMEOUT);
5657 do {
5658 /* Again only user level code calls this function,
5659 * so nothing interrupt level
5660 * will suddenly eat the receive_queue.
5662 * Look at current nfs client by the way...
5663 * However, this function was corrent in any case. 8)
5665 if (flags & MSG_PEEK) {
5666 spin_lock_bh(&sk->sk_receive_queue.lock);
5667 skb = skb_peek(&sk->sk_receive_queue);
5668 if (skb)
5669 atomic_inc(&skb->users);
5670 spin_unlock_bh(&sk->sk_receive_queue.lock);
5671 } else {
5672 skb = skb_dequeue(&sk->sk_receive_queue);
5675 if (skb)
5676 return skb;
5678 /* Caller is allowed not to check sk->sk_err before calling. */
5679 error = sock_error(sk);
5680 if (error)
5681 goto no_packet;
5683 if (sk->sk_shutdown & RCV_SHUTDOWN)
5684 break;
5686 /* User doesn't want to wait. */
5687 error = -EAGAIN;
5688 if (!timeo)
5689 goto no_packet;
5690 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5692 return NULL;
5694 no_packet:
5695 *err = error;
5696 return NULL;
5699 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5700 static void __sctp_write_space(struct sctp_association *asoc)
5702 struct sock *sk = asoc->base.sk;
5703 struct socket *sock = sk->sk_socket;
5705 if ((sctp_wspace(asoc) > 0) && sock) {
5706 if (waitqueue_active(&asoc->wait))
5707 wake_up_interruptible(&asoc->wait);
5709 if (sctp_writeable(sk)) {
5710 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5711 wake_up_interruptible(sk->sk_sleep);
5713 /* Note that we try to include the Async I/O support
5714 * here by modeling from the current TCP/UDP code.
5715 * We have not tested with it yet.
5717 if (sock->fasync_list &&
5718 !(sk->sk_shutdown & SEND_SHUTDOWN))
5719 sock_wake_async(sock, 2, POLL_OUT);
5724 /* Do accounting for the sndbuf space.
5725 * Decrement the used sndbuf space of the corresponding association by the
5726 * data size which was just transmitted(freed).
5728 static void sctp_wfree(struct sk_buff *skb)
5730 struct sctp_association *asoc;
5731 struct sctp_chunk *chunk;
5732 struct sock *sk;
5734 /* Get the saved chunk pointer. */
5735 chunk = *((struct sctp_chunk **)(skb->cb));
5736 asoc = chunk->asoc;
5737 sk = asoc->base.sk;
5738 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5739 sizeof(struct sk_buff) +
5740 sizeof(struct sctp_chunk);
5742 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5744 sock_wfree(skb);
5745 __sctp_write_space(asoc);
5747 sctp_association_put(asoc);
5750 /* Do accounting for the receive space on the socket.
5751 * Accounting for the association is done in ulpevent.c
5752 * We set this as a destructor for the cloned data skbs so that
5753 * accounting is done at the correct time.
5755 void sctp_sock_rfree(struct sk_buff *skb)
5757 struct sock *sk = skb->sk;
5758 struct sctp_ulpevent *event = sctp_skb2event(skb);
5760 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5764 /* Helper function to wait for space in the sndbuf. */
5765 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5766 size_t msg_len)
5768 struct sock *sk = asoc->base.sk;
5769 int err = 0;
5770 long current_timeo = *timeo_p;
5771 DEFINE_WAIT(wait);
5773 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5774 asoc, (long)(*timeo_p), msg_len);
5776 /* Increment the association's refcnt. */
5777 sctp_association_hold(asoc);
5779 /* Wait on the association specific sndbuf space. */
5780 for (;;) {
5781 prepare_to_wait_exclusive(&asoc->wait, &wait,
5782 TASK_INTERRUPTIBLE);
5783 if (!*timeo_p)
5784 goto do_nonblock;
5785 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5786 asoc->base.dead)
5787 goto do_error;
5788 if (signal_pending(current))
5789 goto do_interrupted;
5790 if (msg_len <= sctp_wspace(asoc))
5791 break;
5793 /* Let another process have a go. Since we are going
5794 * to sleep anyway.
5796 sctp_release_sock(sk);
5797 current_timeo = schedule_timeout(current_timeo);
5798 BUG_ON(sk != asoc->base.sk);
5799 sctp_lock_sock(sk);
5801 *timeo_p = current_timeo;
5804 out:
5805 finish_wait(&asoc->wait, &wait);
5807 /* Release the association's refcnt. */
5808 sctp_association_put(asoc);
5810 return err;
5812 do_error:
5813 err = -EPIPE;
5814 goto out;
5816 do_interrupted:
5817 err = sock_intr_errno(*timeo_p);
5818 goto out;
5820 do_nonblock:
5821 err = -EAGAIN;
5822 goto out;
5825 /* If socket sndbuf has changed, wake up all per association waiters. */
5826 void sctp_write_space(struct sock *sk)
5828 struct sctp_association *asoc;
5829 struct list_head *pos;
5831 /* Wake up the tasks in each wait queue. */
5832 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5833 asoc = list_entry(pos, struct sctp_association, asocs);
5834 __sctp_write_space(asoc);
5838 /* Is there any sndbuf space available on the socket?
5840 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5841 * associations on the same socket. For a UDP-style socket with
5842 * multiple associations, it is possible for it to be "unwriteable"
5843 * prematurely. I assume that this is acceptable because
5844 * a premature "unwriteable" is better than an accidental "writeable" which
5845 * would cause an unwanted block under certain circumstances. For the 1-1
5846 * UDP-style sockets or TCP-style sockets, this code should work.
5847 * - Daisy
5849 static int sctp_writeable(struct sock *sk)
5851 int amt = 0;
5853 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
5854 if (amt < 0)
5855 amt = 0;
5856 return amt;
5859 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5860 * returns immediately with EINPROGRESS.
5862 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5864 struct sock *sk = asoc->base.sk;
5865 int err = 0;
5866 long current_timeo = *timeo_p;
5867 DEFINE_WAIT(wait);
5869 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5870 (long)(*timeo_p));
5872 /* Increment the association's refcnt. */
5873 sctp_association_hold(asoc);
5875 for (;;) {
5876 prepare_to_wait_exclusive(&asoc->wait, &wait,
5877 TASK_INTERRUPTIBLE);
5878 if (!*timeo_p)
5879 goto do_nonblock;
5880 if (sk->sk_shutdown & RCV_SHUTDOWN)
5881 break;
5882 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5883 asoc->base.dead)
5884 goto do_error;
5885 if (signal_pending(current))
5886 goto do_interrupted;
5888 if (sctp_state(asoc, ESTABLISHED))
5889 break;
5891 /* Let another process have a go. Since we are going
5892 * to sleep anyway.
5894 sctp_release_sock(sk);
5895 current_timeo = schedule_timeout(current_timeo);
5896 sctp_lock_sock(sk);
5898 *timeo_p = current_timeo;
5901 out:
5902 finish_wait(&asoc->wait, &wait);
5904 /* Release the association's refcnt. */
5905 sctp_association_put(asoc);
5907 return err;
5909 do_error:
5910 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5911 err = -ETIMEDOUT;
5912 else
5913 err = -ECONNREFUSED;
5914 goto out;
5916 do_interrupted:
5917 err = sock_intr_errno(*timeo_p);
5918 goto out;
5920 do_nonblock:
5921 err = -EINPROGRESS;
5922 goto out;
5925 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5927 struct sctp_endpoint *ep;
5928 int err = 0;
5929 DEFINE_WAIT(wait);
5931 ep = sctp_sk(sk)->ep;
5934 for (;;) {
5935 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5936 TASK_INTERRUPTIBLE);
5938 if (list_empty(&ep->asocs)) {
5939 sctp_release_sock(sk);
5940 timeo = schedule_timeout(timeo);
5941 sctp_lock_sock(sk);
5944 err = -EINVAL;
5945 if (!sctp_sstate(sk, LISTENING))
5946 break;
5948 err = 0;
5949 if (!list_empty(&ep->asocs))
5950 break;
5952 err = sock_intr_errno(timeo);
5953 if (signal_pending(current))
5954 break;
5956 err = -EAGAIN;
5957 if (!timeo)
5958 break;
5961 finish_wait(sk->sk_sleep, &wait);
5963 return err;
5966 void sctp_wait_for_close(struct sock *sk, long timeout)
5968 DEFINE_WAIT(wait);
5970 do {
5971 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5972 if (list_empty(&sctp_sk(sk)->ep->asocs))
5973 break;
5974 sctp_release_sock(sk);
5975 timeout = schedule_timeout(timeout);
5976 sctp_lock_sock(sk);
5977 } while (!signal_pending(current) && timeout);
5979 finish_wait(sk->sk_sleep, &wait);
5982 static void sctp_sock_rfree_frag(struct sk_buff *skb)
5984 struct sk_buff *frag;
5986 if (!skb->data_len)
5987 goto done;
5989 /* Don't forget the fragments. */
5990 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5991 sctp_sock_rfree_frag(frag);
5993 done:
5994 sctp_sock_rfree(skb);
5997 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5999 struct sk_buff *frag;
6001 if (!skb->data_len)
6002 goto done;
6004 /* Don't forget the fragments. */
6005 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6006 sctp_skb_set_owner_r_frag(frag, sk);
6008 done:
6009 sctp_skb_set_owner_r(skb, sk);
6012 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6013 * and its messages to the newsk.
6015 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6016 struct sctp_association *assoc,
6017 sctp_socket_type_t type)
6019 struct sctp_sock *oldsp = sctp_sk(oldsk);
6020 struct sctp_sock *newsp = sctp_sk(newsk);
6021 struct sctp_bind_bucket *pp; /* hash list port iterator */
6022 struct sctp_endpoint *newep = newsp->ep;
6023 struct sk_buff *skb, *tmp;
6024 struct sctp_ulpevent *event;
6025 int flags = 0;
6027 /* Migrate socket buffer sizes and all the socket level options to the
6028 * new socket.
6030 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6031 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6032 /* Brute force copy old sctp opt. */
6033 inet_sk_copy_descendant(newsk, oldsk);
6035 /* Restore the ep value that was overwritten with the above structure
6036 * copy.
6038 newsp->ep = newep;
6039 newsp->hmac = NULL;
6041 /* Hook this new socket in to the bind_hash list. */
6042 pp = sctp_sk(oldsk)->bind_hash;
6043 sk_add_bind_node(newsk, &pp->owner);
6044 sctp_sk(newsk)->bind_hash = pp;
6045 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6047 /* Copy the bind_addr list from the original endpoint to the new
6048 * endpoint so that we can handle restarts properly
6050 if (PF_INET6 == assoc->base.sk->sk_family)
6051 flags = SCTP_ADDR6_ALLOWED;
6052 if (assoc->peer.ipv4_address)
6053 flags |= SCTP_ADDR4_PEERSUPP;
6054 if (assoc->peer.ipv6_address)
6055 flags |= SCTP_ADDR6_PEERSUPP;
6056 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6057 &oldsp->ep->base.bind_addr,
6058 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6060 /* Move any messages in the old socket's receive queue that are for the
6061 * peeled off association to the new socket's receive queue.
6063 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6064 event = sctp_skb2event(skb);
6065 if (event->asoc == assoc) {
6066 sctp_sock_rfree_frag(skb);
6067 __skb_unlink(skb, &oldsk->sk_receive_queue);
6068 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6069 sctp_skb_set_owner_r_frag(skb, newsk);
6073 /* Clean up any messages pending delivery due to partial
6074 * delivery. Three cases:
6075 * 1) No partial deliver; no work.
6076 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6077 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6079 skb_queue_head_init(&newsp->pd_lobby);
6080 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6082 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6083 struct sk_buff_head *queue;
6085 /* Decide which queue to move pd_lobby skbs to. */
6086 if (assoc->ulpq.pd_mode) {
6087 queue = &newsp->pd_lobby;
6088 } else
6089 queue = &newsk->sk_receive_queue;
6091 /* Walk through the pd_lobby, looking for skbs that
6092 * need moved to the new socket.
6094 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6095 event = sctp_skb2event(skb);
6096 if (event->asoc == assoc) {
6097 sctp_sock_rfree_frag(skb);
6098 __skb_unlink(skb, &oldsp->pd_lobby);
6099 __skb_queue_tail(queue, skb);
6100 sctp_skb_set_owner_r_frag(skb, newsk);
6104 /* Clear up any skbs waiting for the partial
6105 * delivery to finish.
6107 if (assoc->ulpq.pd_mode)
6108 sctp_clear_pd(oldsk, NULL);
6112 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6113 sctp_sock_rfree_frag(skb);
6114 sctp_skb_set_owner_r_frag(skb, newsk);
6117 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6118 sctp_sock_rfree_frag(skb);
6119 sctp_skb_set_owner_r_frag(skb, newsk);
6122 /* Set the type of socket to indicate that it is peeled off from the
6123 * original UDP-style socket or created with the accept() call on a
6124 * TCP-style socket..
6126 newsp->type = type;
6128 /* Mark the new socket "in-use" by the user so that any packets
6129 * that may arrive on the association after we've moved it are
6130 * queued to the backlog. This prevents a potential race between
6131 * backlog processing on the old socket and new-packet processing
6132 * on the new socket.
6134 * The caller has just allocated newsk so we can guarantee that other
6135 * paths won't try to lock it and then oldsk.
6137 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6138 sctp_assoc_migrate(assoc, newsk);
6140 /* If the association on the newsk is already closed before accept()
6141 * is called, set RCV_SHUTDOWN flag.
6143 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6144 newsk->sk_shutdown |= RCV_SHUTDOWN;
6146 newsk->sk_state = SCTP_SS_ESTABLISHED;
6147 sctp_release_sock(newsk);
6150 /* This proto struct describes the ULP interface for SCTP. */
6151 struct proto sctp_prot = {
6152 .name = "SCTP",
6153 .owner = THIS_MODULE,
6154 .close = sctp_close,
6155 .connect = sctp_connect,
6156 .disconnect = sctp_disconnect,
6157 .accept = sctp_accept,
6158 .ioctl = sctp_ioctl,
6159 .init = sctp_init_sock,
6160 .destroy = sctp_destroy_sock,
6161 .shutdown = sctp_shutdown,
6162 .setsockopt = sctp_setsockopt,
6163 .getsockopt = sctp_getsockopt,
6164 .sendmsg = sctp_sendmsg,
6165 .recvmsg = sctp_recvmsg,
6166 .bind = sctp_bind,
6167 .backlog_rcv = sctp_backlog_rcv,
6168 .hash = sctp_hash,
6169 .unhash = sctp_unhash,
6170 .get_port = sctp_get_port,
6171 .obj_size = sizeof(struct sctp_sock),
6174 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6175 struct proto sctpv6_prot = {
6176 .name = "SCTPv6",
6177 .owner = THIS_MODULE,
6178 .close = sctp_close,
6179 .connect = sctp_connect,
6180 .disconnect = sctp_disconnect,
6181 .accept = sctp_accept,
6182 .ioctl = sctp_ioctl,
6183 .init = sctp_init_sock,
6184 .destroy = sctp_destroy_sock,
6185 .shutdown = sctp_shutdown,
6186 .setsockopt = sctp_setsockopt,
6187 .getsockopt = sctp_getsockopt,
6188 .sendmsg = sctp_sendmsg,
6189 .recvmsg = sctp_recvmsg,
6190 .bind = sctp_bind,
6191 .backlog_rcv = sctp_backlog_rcv,
6192 .hash = sctp_hash,
6193 .unhash = sctp_unhash,
6194 .get_port = sctp_get_port,
6195 .obj_size = sizeof(struct sctp6_sock),
6197 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */