[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / net / sctp / socket.c
blob0b338eca6dc0b1559e2392af2964f5d7fcfe1820
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/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
65 #include <linux/ip.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 kmem_cache_t *sctp_bucket_cachep;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
121 } else {
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
126 if (amt < 0)
127 amt = 0;
129 return amt;
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
139 * tracking.
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
149 skb_set_owner_w(chunk->skb, sk);
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
159 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
160 sizeof(struct sk_buff) +
161 sizeof(struct sctp_chunk);
163 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
166 /* Verify that this is a valid address. */
167 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
168 int len)
170 struct sctp_af *af;
172 /* Verify basic sockaddr. */
173 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
174 if (!af)
175 return -EINVAL;
177 /* Is this a valid SCTP address? */
178 if (!af->addr_valid(addr, sctp_sk(sk)))
179 return -EINVAL;
181 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
182 return -EINVAL;
184 return 0;
187 /* Look up the association by its id. If this is not a UDP-style
188 * socket, the ID field is always ignored.
190 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
192 struct sctp_association *asoc = NULL;
194 /* If this is not a UDP-style socket, assoc id should be ignored. */
195 if (!sctp_style(sk, UDP)) {
196 /* Return NULL if the socket state is not ESTABLISHED. It
197 * could be a TCP-style listening socket or a socket which
198 * hasn't yet called connect() to establish an association.
200 if (!sctp_sstate(sk, ESTABLISHED))
201 return NULL;
203 /* Get the first and the only association from the list. */
204 if (!list_empty(&sctp_sk(sk)->ep->asocs))
205 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
206 struct sctp_association, asocs);
207 return asoc;
210 /* Otherwise this is a UDP-style socket. */
211 if (!id || (id == (sctp_assoc_t)-1))
212 return NULL;
214 spin_lock_bh(&sctp_assocs_id_lock);
215 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
216 spin_unlock_bh(&sctp_assocs_id_lock);
218 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
219 return NULL;
221 return asoc;
224 /* Look up the transport from an address and an assoc id. If both address and
225 * id are specified, the associations matching the address and the id should be
226 * the same.
228 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
229 struct sockaddr_storage *addr,
230 sctp_assoc_t id)
232 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
233 struct sctp_transport *transport;
234 union sctp_addr *laddr = (union sctp_addr *)addr;
236 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
237 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
238 (union sctp_addr *)addr,
239 &transport);
240 laddr->v4.sin_port = htons(laddr->v4.sin_port);
242 if (!addr_asoc)
243 return NULL;
245 id_asoc = sctp_id2assoc(sk, id);
246 if (id_asoc && (id_asoc != addr_asoc))
247 return NULL;
249 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
250 (union sctp_addr *)addr);
252 return transport;
255 /* API 3.1.2 bind() - UDP Style Syntax
256 * The syntax of bind() is,
258 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
260 * sd - the socket descriptor returned by socket().
261 * addr - the address structure (struct sockaddr_in or struct
262 * sockaddr_in6 [RFC 2553]),
263 * addr_len - the size of the address structure.
265 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
267 int retval = 0;
269 sctp_lock_sock(sk);
271 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
272 sk, uaddr, addr_len);
274 /* Disallow binding twice. */
275 if (!sctp_sk(sk)->ep->base.bind_addr.port)
276 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
277 addr_len);
278 else
279 retval = -EINVAL;
281 sctp_release_sock(sk);
283 return retval;
286 static long sctp_get_port_local(struct sock *, union sctp_addr *);
288 /* Verify this is a valid sockaddr. */
289 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
290 union sctp_addr *addr, int len)
292 struct sctp_af *af;
294 /* Check minimum size. */
295 if (len < sizeof (struct sockaddr))
296 return NULL;
298 /* Does this PF support this AF? */
299 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
300 return NULL;
302 /* If we get this far, af is valid. */
303 af = sctp_get_af_specific(addr->sa.sa_family);
305 if (len < af->sockaddr_len)
306 return NULL;
308 return af;
311 /* Bind a local address either to an endpoint or to an association. */
312 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
314 struct sctp_sock *sp = sctp_sk(sk);
315 struct sctp_endpoint *ep = sp->ep;
316 struct sctp_bind_addr *bp = &ep->base.bind_addr;
317 struct sctp_af *af;
318 unsigned short snum;
319 int ret = 0;
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
322 sk, addr, len);
324 /* Common sockaddr verification. */
325 af = sctp_sockaddr_af(sp, addr, len);
326 if (!af)
327 return -EINVAL;
329 /* PF specific bind() address verification. */
330 if (!sp->pf->bind_verify(sp, addr))
331 return -EADDRNOTAVAIL;
333 snum= ntohs(addr->v4.sin_port);
335 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
336 bp->port, snum);
338 /* We must either be unbound, or bind to the same port. */
339 if (bp->port && (snum != bp->port)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum, bp->port);
343 return -EINVAL;
346 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
347 return -EACCES;
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
351 * detection.
353 if ((ret = sctp_get_port_local(sk, addr))) {
354 if (ret == (long) sk) {
355 /* This endpoint has a conflicting address. */
356 return -EINVAL;
357 } else {
358 return -EADDRINUSE;
362 /* Refresh ephemeral port. */
363 if (!bp->port)
364 bp->port = inet_sk(sk)->num;
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep->base.addr_lock);
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr->v4.sin_port = ntohs(addr->v4.sin_port);
372 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
373 addr->v4.sin_port = htons(addr->v4.sin_port);
374 sctp_write_unlock(&ep->base.addr_lock);
375 sctp_local_bh_enable();
377 /* Copy back into socket for getsockname() use. */
378 if (!ret) {
379 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
380 af->to_sk_saddr(addr, sk);
383 return ret;
386 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
389 * at any one time. If a sender, after sending an ASCONF chunk, decides
390 * it needs to transfer another ASCONF Chunk, it MUST wait until the
391 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
392 * subsequent ASCONF. Note this restriction binds each side, so at any
393 * time two ASCONF may be in-transit on any given association (one sent
394 * from each endpoint).
396 static int sctp_send_asconf(struct sctp_association *asoc,
397 struct sctp_chunk *chunk)
399 int retval = 0;
401 /* If there is an outstanding ASCONF chunk, queue it for later
402 * transmission.
404 if (asoc->addip_last_asconf) {
405 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
406 goto out;
409 /* Hold the chunk until an ASCONF_ACK is received. */
410 sctp_chunk_hold(chunk);
411 retval = sctp_primitive_ASCONF(asoc, chunk);
412 if (retval)
413 sctp_chunk_free(chunk);
414 else
415 asoc->addip_last_asconf = chunk;
417 out:
418 return retval;
421 /* Add a list of addresses as bind addresses to local endpoint or
422 * association.
424 * Basically run through each address specified in the addrs/addrcnt
425 * array/length pair, determine if it is IPv6 or IPv4 and call
426 * sctp_do_bind() on it.
428 * If any of them fails, then the operation will be reversed and the
429 * ones that were added will be removed.
431 * Only sctp_setsockopt_bindx() is supposed to call this function.
433 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
435 int cnt;
436 int retval = 0;
437 void *addr_buf;
438 struct sockaddr *sa_addr;
439 struct sctp_af *af;
441 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
442 sk, addrs, addrcnt);
444 addr_buf = addrs;
445 for (cnt = 0; cnt < addrcnt; cnt++) {
446 /* The list may contain either IPv4 or IPv6 address;
447 * determine the address length for walking thru the list.
449 sa_addr = (struct sockaddr *)addr_buf;
450 af = sctp_get_af_specific(sa_addr->sa_family);
451 if (!af) {
452 retval = -EINVAL;
453 goto err_bindx_add;
456 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
457 af->sockaddr_len);
459 addr_buf += af->sockaddr_len;
461 err_bindx_add:
462 if (retval < 0) {
463 /* Failed. Cleanup the ones that have been added */
464 if (cnt > 0)
465 sctp_bindx_rem(sk, addrs, cnt);
466 return retval;
470 return retval;
473 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
474 * associations that are part of the endpoint indicating that a list of local
475 * addresses are added to the endpoint.
477 * If any of the addresses is already in the bind address list of the
478 * association, we do not send the chunk for that association. But it will not
479 * affect other associations.
481 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 static int sctp_send_asconf_add_ip(struct sock *sk,
484 struct sockaddr *addrs,
485 int addrcnt)
487 struct sctp_sock *sp;
488 struct sctp_endpoint *ep;
489 struct sctp_association *asoc;
490 struct sctp_bind_addr *bp;
491 struct sctp_chunk *chunk;
492 struct sctp_sockaddr_entry *laddr;
493 union sctp_addr *addr;
494 void *addr_buf;
495 struct sctp_af *af;
496 struct list_head *pos;
497 struct list_head *p;
498 int i;
499 int retval = 0;
501 if (!sctp_addip_enable)
502 return retval;
504 sp = sctp_sk(sk);
505 ep = sp->ep;
507 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
508 __FUNCTION__, sk, addrs, addrcnt);
510 list_for_each(pos, &ep->asocs) {
511 asoc = list_entry(pos, struct sctp_association, asocs);
513 if (!asoc->peer.asconf_capable)
514 continue;
516 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
517 continue;
519 if (!sctp_state(asoc, ESTABLISHED))
520 continue;
522 /* Check if any address in the packed array of addresses is
523 * in the bind address list of the association. If so,
524 * do not send the asconf chunk to its peer, but continue with
525 * other associations.
527 addr_buf = addrs;
528 for (i = 0; i < addrcnt; i++) {
529 addr = (union sctp_addr *)addr_buf;
530 af = sctp_get_af_specific(addr->v4.sin_family);
531 if (!af) {
532 retval = -EINVAL;
533 goto out;
536 if (sctp_assoc_lookup_laddr(asoc, addr))
537 break;
539 addr_buf += af->sockaddr_len;
541 if (i < addrcnt)
542 continue;
544 /* Use the first address in bind addr list of association as
545 * Address Parameter of ASCONF CHUNK.
547 sctp_read_lock(&asoc->base.addr_lock);
548 bp = &asoc->base.bind_addr;
549 p = bp->address_list.next;
550 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
551 sctp_read_unlock(&asoc->base.addr_lock);
553 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
554 addrcnt, SCTP_PARAM_ADD_IP);
555 if (!chunk) {
556 retval = -ENOMEM;
557 goto out;
560 retval = sctp_send_asconf(asoc, chunk);
562 /* FIXME: After sending the add address ASCONF chunk, we
563 * cannot append the address to the association's binding
564 * address list, because the new address may be used as the
565 * source of a message sent to the peer before the ASCONF
566 * chunk is received by the peer. So we should wait until
567 * ASCONF_ACK is received.
571 out:
572 return retval;
575 /* Remove a list of addresses from bind addresses list. Do not remove the
576 * last address.
578 * Basically run through each address specified in the addrs/addrcnt
579 * array/length pair, determine if it is IPv6 or IPv4 and call
580 * sctp_del_bind() on it.
582 * If any of them fails, then the operation will be reversed and the
583 * ones that were removed will be added back.
585 * At least one address has to be left; if only one address is
586 * available, the operation will return -EBUSY.
588 * Only sctp_setsockopt_bindx() is supposed to call this function.
590 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
592 struct sctp_sock *sp = sctp_sk(sk);
593 struct sctp_endpoint *ep = sp->ep;
594 int cnt;
595 struct sctp_bind_addr *bp = &ep->base.bind_addr;
596 int retval = 0;
597 union sctp_addr saveaddr;
598 void *addr_buf;
599 struct sockaddr *sa_addr;
600 struct sctp_af *af;
602 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
603 sk, addrs, addrcnt);
605 addr_buf = addrs;
606 for (cnt = 0; cnt < addrcnt; cnt++) {
607 /* If the bind address list is empty or if there is only one
608 * bind address, there is nothing more to be removed (we need
609 * at least one address here).
611 if (list_empty(&bp->address_list) ||
612 (sctp_list_single_entry(&bp->address_list))) {
613 retval = -EBUSY;
614 goto err_bindx_rem;
617 /* The list may contain either IPv4 or IPv6 address;
618 * determine the address length to copy the address to
619 * saveaddr.
621 sa_addr = (struct sockaddr *)addr_buf;
622 af = sctp_get_af_specific(sa_addr->sa_family);
623 if (!af) {
624 retval = -EINVAL;
625 goto err_bindx_rem;
627 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
628 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
629 if (saveaddr.v4.sin_port != bp->port) {
630 retval = -EINVAL;
631 goto err_bindx_rem;
634 /* FIXME - There is probably a need to check if sk->sk_saddr and
635 * sk->sk_rcv_addr are currently set to one of the addresses to
636 * be removed. This is something which needs to be looked into
637 * when we are fixing the outstanding issues with multi-homing
638 * socket routing and failover schemes. Refer to comments in
639 * sctp_do_bind(). -daisy
641 sctp_local_bh_disable();
642 sctp_write_lock(&ep->base.addr_lock);
644 retval = sctp_del_bind_addr(bp, &saveaddr);
646 sctp_write_unlock(&ep->base.addr_lock);
647 sctp_local_bh_enable();
649 addr_buf += af->sockaddr_len;
650 err_bindx_rem:
651 if (retval < 0) {
652 /* Failed. Add the ones that has been removed back */
653 if (cnt > 0)
654 sctp_bindx_add(sk, addrs, cnt);
655 return retval;
659 return retval;
662 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
663 * the associations that are part of the endpoint indicating that a list of
664 * local addresses are removed from the endpoint.
666 * If any of the addresses is already in the bind address list of the
667 * association, we do not send the chunk for that association. But it will not
668 * affect other associations.
670 * Only sctp_setsockopt_bindx() is supposed to call this function.
672 static int sctp_send_asconf_del_ip(struct sock *sk,
673 struct sockaddr *addrs,
674 int addrcnt)
676 struct sctp_sock *sp;
677 struct sctp_endpoint *ep;
678 struct sctp_association *asoc;
679 struct sctp_bind_addr *bp;
680 struct sctp_chunk *chunk;
681 union sctp_addr *laddr;
682 void *addr_buf;
683 struct sctp_af *af;
684 struct list_head *pos;
685 int i;
686 int retval = 0;
688 if (!sctp_addip_enable)
689 return retval;
691 sp = sctp_sk(sk);
692 ep = sp->ep;
694 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
695 __FUNCTION__, sk, addrs, addrcnt);
697 list_for_each(pos, &ep->asocs) {
698 asoc = list_entry(pos, struct sctp_association, asocs);
700 if (!asoc->peer.asconf_capable)
701 continue;
703 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
704 continue;
706 if (!sctp_state(asoc, ESTABLISHED))
707 continue;
709 /* Check if any address in the packed array of addresses is
710 * not present in the bind address list of the association.
711 * If so, do not send the asconf chunk to its peer, but
712 * continue with other associations.
714 addr_buf = addrs;
715 for (i = 0; i < addrcnt; i++) {
716 laddr = (union sctp_addr *)addr_buf;
717 af = sctp_get_af_specific(laddr->v4.sin_family);
718 if (!af) {
719 retval = -EINVAL;
720 goto out;
723 if (!sctp_assoc_lookup_laddr(asoc, laddr))
724 break;
726 addr_buf += af->sockaddr_len;
728 if (i < addrcnt)
729 continue;
731 /* Find one address in the association's bind address list
732 * that is not in the packed array of addresses. This is to
733 * make sure that we do not delete all the addresses in the
734 * association.
736 sctp_read_lock(&asoc->base.addr_lock);
737 bp = &asoc->base.bind_addr;
738 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
739 addrcnt, sp);
740 sctp_read_unlock(&asoc->base.addr_lock);
741 if (!laddr)
742 continue;
744 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
745 SCTP_PARAM_DEL_IP);
746 if (!chunk) {
747 retval = -ENOMEM;
748 goto out;
751 retval = sctp_send_asconf(asoc, chunk);
753 /* FIXME: After sending the delete address ASCONF chunk, we
754 * cannot remove the addresses from the association's bind
755 * address list, because there maybe some packet send to
756 * the delete addresses, so we should wait until ASCONF_ACK
757 * packet is received.
760 out:
761 return retval;
764 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
766 * API 8.1
767 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
768 * int flags);
770 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
771 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
772 * or IPv6 addresses.
774 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
775 * Section 3.1.2 for this usage.
777 * addrs is a pointer to an array of one or more socket addresses. Each
778 * address is contained in its appropriate structure (i.e. struct
779 * sockaddr_in or struct sockaddr_in6) the family of the address type
780 * must be used to distengish the address length (note that this
781 * representation is termed a "packed array" of addresses). The caller
782 * specifies the number of addresses in the array with addrcnt.
784 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
785 * -1, and sets errno to the appropriate error code.
787 * For SCTP, the port given in each socket address must be the same, or
788 * sctp_bindx() will fail, setting errno to EINVAL.
790 * The flags parameter is formed from the bitwise OR of zero or more of
791 * the following currently defined flags:
793 * SCTP_BINDX_ADD_ADDR
795 * SCTP_BINDX_REM_ADDR
797 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
798 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
799 * addresses from the association. The two flags are mutually exclusive;
800 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
801 * not remove all addresses from an association; sctp_bindx() will
802 * reject such an attempt with EINVAL.
804 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
805 * additional addresses with an endpoint after calling bind(). Or use
806 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
807 * socket is associated with so that no new association accepted will be
808 * associated with those addresses. If the endpoint supports dynamic
809 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
810 * endpoint to send the appropriate message to the peer to change the
811 * peers address lists.
813 * Adding and removing addresses from a connected association is
814 * optional functionality. Implementations that do not support this
815 * functionality should return EOPNOTSUPP.
817 * Basically do nothing but copying the addresses from user to kernel
818 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
819 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
821 * We don't use copy_from_user() for optimization: we first do the
822 * sanity checks (buffer size -fast- and access check-healthy
823 * pointer); if all of those succeed, then we can alloc the memory
824 * (expensive operation) needed to copy the data to kernel. Then we do
825 * the copying without checking the user space area
826 * (__copy_from_user()).
828 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
829 * it.
831 * sk The sk of the socket
832 * addrs The pointer to the addresses in user land
833 * addrssize Size of the addrs buffer
834 * op Operation to perform (add or remove, see the flags of
835 * sctp_bindx)
837 * Returns 0 if ok, <0 errno code on error.
839 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
840 struct sockaddr __user *addrs,
841 int addrs_size, int op)
843 struct sockaddr *kaddrs;
844 int err;
845 int addrcnt = 0;
846 int walk_size = 0;
847 struct sockaddr *sa_addr;
848 void *addr_buf;
849 struct sctp_af *af;
851 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
852 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
854 if (unlikely(addrs_size <= 0))
855 return -EINVAL;
857 /* Check the user passed a healthy pointer. */
858 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
859 return -EFAULT;
861 /* Alloc space for the address array in kernel memory. */
862 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
863 if (unlikely(!kaddrs))
864 return -ENOMEM;
866 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
867 kfree(kaddrs);
868 return -EFAULT;
871 /* Walk through the addrs buffer and count the number of addresses. */
872 addr_buf = kaddrs;
873 while (walk_size < addrs_size) {
874 sa_addr = (struct sockaddr *)addr_buf;
875 af = sctp_get_af_specific(sa_addr->sa_family);
877 /* If the address family is not supported or if this address
878 * causes the address buffer to overflow return EINVAL.
880 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
881 kfree(kaddrs);
882 return -EINVAL;
884 addrcnt++;
885 addr_buf += af->sockaddr_len;
886 walk_size += af->sockaddr_len;
889 /* Do the work. */
890 switch (op) {
891 case SCTP_BINDX_ADD_ADDR:
892 err = sctp_bindx_add(sk, kaddrs, addrcnt);
893 if (err)
894 goto out;
895 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
896 break;
898 case SCTP_BINDX_REM_ADDR:
899 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
900 if (err)
901 goto out;
902 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
903 break;
905 default:
906 err = -EINVAL;
907 break;
910 out:
911 kfree(kaddrs);
913 return err;
916 /* API 3.1.4 close() - UDP Style Syntax
917 * Applications use close() to perform graceful shutdown (as described in
918 * Section 10.1 of [SCTP]) on ALL the associations currently represented
919 * by a UDP-style socket.
921 * The syntax is
923 * ret = close(int sd);
925 * sd - the socket descriptor of the associations to be closed.
927 * To gracefully shutdown a specific association represented by the
928 * UDP-style socket, an application should use the sendmsg() call,
929 * passing no user data, but including the appropriate flag in the
930 * ancillary data (see Section xxxx).
932 * If sd in the close() call is a branched-off socket representing only
933 * one association, the shutdown is performed on that association only.
935 * 4.1.6 close() - TCP Style Syntax
937 * Applications use close() to gracefully close down an association.
939 * The syntax is:
941 * int close(int sd);
943 * sd - the socket descriptor of the association to be closed.
945 * After an application calls close() on a socket descriptor, no further
946 * socket operations will succeed on that descriptor.
948 * API 7.1.4 SO_LINGER
950 * An application using the TCP-style socket can use this option to
951 * perform the SCTP ABORT primitive. The linger option structure is:
953 * struct linger {
954 * int l_onoff; // option on/off
955 * int l_linger; // linger time
956 * };
958 * To enable the option, set l_onoff to 1. If the l_linger value is set
959 * to 0, calling close() is the same as the ABORT primitive. If the
960 * value is set to a negative value, the setsockopt() call will return
961 * an error. If the value is set to a positive value linger_time, the
962 * close() can be blocked for at most linger_time ms. If the graceful
963 * shutdown phase does not finish during this period, close() will
964 * return but the graceful shutdown phase continues in the system.
966 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
968 struct sctp_endpoint *ep;
969 struct sctp_association *asoc;
970 struct list_head *pos, *temp;
972 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
974 sctp_lock_sock(sk);
975 sk->sk_shutdown = SHUTDOWN_MASK;
977 ep = sctp_sk(sk)->ep;
979 /* Walk all associations on a socket, not on an endpoint. */
980 list_for_each_safe(pos, temp, &ep->asocs) {
981 asoc = list_entry(pos, struct sctp_association, asocs);
983 if (sctp_style(sk, TCP)) {
984 /* A closed association can still be in the list if
985 * it belongs to a TCP-style listening socket that is
986 * not yet accepted. If so, free it. If not, send an
987 * ABORT or SHUTDOWN based on the linger options.
989 if (sctp_state(asoc, CLOSED)) {
990 sctp_unhash_established(asoc);
991 sctp_association_free(asoc);
993 } else if (sock_flag(sk, SOCK_LINGER) &&
994 !sk->sk_lingertime)
995 sctp_primitive_ABORT(asoc, NULL);
996 else
997 sctp_primitive_SHUTDOWN(asoc, NULL);
998 } else
999 sctp_primitive_SHUTDOWN(asoc, NULL);
1002 /* Clean up any skbs sitting on the receive queue. */
1003 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1004 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1006 /* On a TCP-style socket, block for at most linger_time if set. */
1007 if (sctp_style(sk, TCP) && timeout)
1008 sctp_wait_for_close(sk, timeout);
1010 /* This will run the backlog queue. */
1011 sctp_release_sock(sk);
1013 /* Supposedly, no process has access to the socket, but
1014 * the net layers still may.
1016 sctp_local_bh_disable();
1017 sctp_bh_lock_sock(sk);
1019 /* Hold the sock, since sk_common_release() will put sock_put()
1020 * and we have just a little more cleanup.
1022 sock_hold(sk);
1023 sk_common_release(sk);
1025 sctp_bh_unlock_sock(sk);
1026 sctp_local_bh_enable();
1028 sock_put(sk);
1030 SCTP_DBG_OBJCNT_DEC(sock);
1033 /* Handle EPIPE error. */
1034 static int sctp_error(struct sock *sk, int flags, int err)
1036 if (err == -EPIPE)
1037 err = sock_error(sk) ? : -EPIPE;
1038 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1039 send_sig(SIGPIPE, current, 0);
1040 return err;
1043 /* API 3.1.3 sendmsg() - UDP Style Syntax
1045 * An application uses sendmsg() and recvmsg() calls to transmit data to
1046 * and receive data from its peer.
1048 * ssize_t sendmsg(int socket, const struct msghdr *message,
1049 * int flags);
1051 * socket - the socket descriptor of the endpoint.
1052 * message - pointer to the msghdr structure which contains a single
1053 * user message and possibly some ancillary data.
1055 * See Section 5 for complete description of the data
1056 * structures.
1058 * flags - flags sent or received with the user message, see Section
1059 * 5 for complete description of the flags.
1061 * Note: This function could use a rewrite especially when explicit
1062 * connect support comes in.
1064 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1066 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1068 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1069 struct msghdr *msg, size_t msg_len)
1071 struct sctp_sock *sp;
1072 struct sctp_endpoint *ep;
1073 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1074 struct sctp_transport *transport, *chunk_tp;
1075 struct sctp_chunk *chunk;
1076 union sctp_addr to;
1077 struct sockaddr *msg_name = NULL;
1078 struct sctp_sndrcvinfo default_sinfo = { 0 };
1079 struct sctp_sndrcvinfo *sinfo;
1080 struct sctp_initmsg *sinit;
1081 sctp_assoc_t associd = 0;
1082 sctp_cmsgs_t cmsgs = { NULL };
1083 int err;
1084 sctp_scope_t scope;
1085 long timeo;
1086 __u16 sinfo_flags = 0;
1087 struct sctp_datamsg *datamsg;
1088 struct list_head *pos;
1089 int msg_flags = msg->msg_flags;
1091 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1092 sk, msg, msg_len);
1094 err = 0;
1095 sp = sctp_sk(sk);
1096 ep = sp->ep;
1098 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1100 /* We cannot send a message over a TCP-style listening socket. */
1101 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1102 err = -EPIPE;
1103 goto out_nounlock;
1106 /* Parse out the SCTP CMSGs. */
1107 err = sctp_msghdr_parse(msg, &cmsgs);
1109 if (err) {
1110 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1111 goto out_nounlock;
1114 /* Fetch the destination address for this packet. This
1115 * address only selects the association--it is not necessarily
1116 * the address we will send to.
1117 * For a peeled-off socket, msg_name is ignored.
1119 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1120 int msg_namelen = msg->msg_namelen;
1122 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1123 msg_namelen);
1124 if (err)
1125 return err;
1127 if (msg_namelen > sizeof(to))
1128 msg_namelen = sizeof(to);
1129 memcpy(&to, msg->msg_name, msg_namelen);
1130 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1131 "0x%x:%u.\n",
1132 to.v4.sin_addr.s_addr, to.v4.sin_port);
1134 to.v4.sin_port = ntohs(to.v4.sin_port);
1135 msg_name = msg->msg_name;
1138 sinfo = cmsgs.info;
1139 sinit = cmsgs.init;
1141 /* Did the user specify SNDRCVINFO? */
1142 if (sinfo) {
1143 sinfo_flags = sinfo->sinfo_flags;
1144 associd = sinfo->sinfo_assoc_id;
1147 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1148 msg_len, sinfo_flags);
1150 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1151 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1152 err = -EINVAL;
1153 goto out_nounlock;
1156 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1157 * length messages when MSG_EOF|MSG_ABORT is not set.
1158 * If MSG_ABORT is set, the message length could be non zero with
1159 * the msg_iov set to the user abort reason.
1161 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1162 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1163 err = -EINVAL;
1164 goto out_nounlock;
1167 /* If MSG_ADDR_OVER is set, there must be an address
1168 * specified in msg_name.
1170 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1171 err = -EINVAL;
1172 goto out_nounlock;
1175 transport = NULL;
1177 SCTP_DEBUG_PRINTK("About to look up association.\n");
1179 sctp_lock_sock(sk);
1181 /* If a msg_name has been specified, assume this is to be used. */
1182 if (msg_name) {
1183 /* Look for a matching association on the endpoint. */
1184 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1185 if (!asoc) {
1186 /* If we could not find a matching association on the
1187 * endpoint, make sure that it is not a TCP-style
1188 * socket that already has an association or there is
1189 * no peeled-off association on another socket.
1191 if ((sctp_style(sk, TCP) &&
1192 sctp_sstate(sk, ESTABLISHED)) ||
1193 sctp_endpoint_is_peeled_off(ep, &to)) {
1194 err = -EADDRNOTAVAIL;
1195 goto out_unlock;
1198 } else {
1199 asoc = sctp_id2assoc(sk, associd);
1200 if (!asoc) {
1201 err = -EPIPE;
1202 goto out_unlock;
1206 if (asoc) {
1207 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1209 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1210 * socket that has an association in CLOSED state. This can
1211 * happen when an accepted socket has an association that is
1212 * already CLOSED.
1214 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1215 err = -EPIPE;
1216 goto out_unlock;
1219 if (sinfo_flags & MSG_EOF) {
1220 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1221 asoc);
1222 sctp_primitive_SHUTDOWN(asoc, NULL);
1223 err = 0;
1224 goto out_unlock;
1226 if (sinfo_flags & MSG_ABORT) {
1227 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1228 sctp_primitive_ABORT(asoc, msg);
1229 err = 0;
1230 goto out_unlock;
1234 /* Do we need to create the association? */
1235 if (!asoc) {
1236 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1238 if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
1239 err = -EINVAL;
1240 goto out_unlock;
1243 /* Check for invalid stream against the stream counts,
1244 * either the default or the user specified stream counts.
1246 if (sinfo) {
1247 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1248 /* Check against the defaults. */
1249 if (sinfo->sinfo_stream >=
1250 sp->initmsg.sinit_num_ostreams) {
1251 err = -EINVAL;
1252 goto out_unlock;
1254 } else {
1255 /* Check against the requested. */
1256 if (sinfo->sinfo_stream >=
1257 sinit->sinit_num_ostreams) {
1258 err = -EINVAL;
1259 goto out_unlock;
1265 * API 3.1.2 bind() - UDP Style Syntax
1266 * If a bind() or sctp_bindx() is not called prior to a
1267 * sendmsg() call that initiates a new association, the
1268 * system picks an ephemeral port and will choose an address
1269 * set equivalent to binding with a wildcard address.
1271 if (!ep->base.bind_addr.port) {
1272 if (sctp_autobind(sk)) {
1273 err = -EAGAIN;
1274 goto out_unlock;
1278 scope = sctp_scope(&to);
1279 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1280 if (!new_asoc) {
1281 err = -ENOMEM;
1282 goto out_unlock;
1284 asoc = new_asoc;
1286 /* If the SCTP_INIT ancillary data is specified, set all
1287 * the association init values accordingly.
1289 if (sinit) {
1290 if (sinit->sinit_num_ostreams) {
1291 asoc->c.sinit_num_ostreams =
1292 sinit->sinit_num_ostreams;
1294 if (sinit->sinit_max_instreams) {
1295 asoc->c.sinit_max_instreams =
1296 sinit->sinit_max_instreams;
1298 if (sinit->sinit_max_attempts) {
1299 asoc->max_init_attempts
1300 = sinit->sinit_max_attempts;
1302 if (sinit->sinit_max_init_timeo) {
1303 asoc->max_init_timeo =
1304 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1308 /* Prime the peer's transport structures. */
1309 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1310 if (!transport) {
1311 err = -ENOMEM;
1312 goto out_free;
1314 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1315 if (err < 0) {
1316 err = -ENOMEM;
1317 goto out_free;
1321 /* ASSERT: we have a valid association at this point. */
1322 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1324 if (!sinfo) {
1325 /* If the user didn't specify SNDRCVINFO, make up one with
1326 * some defaults.
1328 default_sinfo.sinfo_stream = asoc->default_stream;
1329 default_sinfo.sinfo_flags = asoc->default_flags;
1330 default_sinfo.sinfo_ppid = asoc->default_ppid;
1331 default_sinfo.sinfo_context = asoc->default_context;
1332 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1333 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1334 sinfo = &default_sinfo;
1337 /* API 7.1.7, the sndbuf size per association bounds the
1338 * maximum size of data that can be sent in a single send call.
1340 if (msg_len > sk->sk_sndbuf) {
1341 err = -EMSGSIZE;
1342 goto out_free;
1345 /* If fragmentation is disabled and the message length exceeds the
1346 * association fragmentation point, return EMSGSIZE. The I-D
1347 * does not specify what this error is, but this looks like
1348 * a great fit.
1350 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1351 err = -EMSGSIZE;
1352 goto out_free;
1355 if (sinfo) {
1356 /* Check for invalid stream. */
1357 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1358 err = -EINVAL;
1359 goto out_free;
1363 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1364 if (!sctp_wspace(asoc)) {
1365 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1366 if (err)
1367 goto out_free;
1370 /* If an address is passed with the sendto/sendmsg call, it is used
1371 * to override the primary destination address in the TCP model, or
1372 * when MSG_ADDR_OVER flag is set in the UDP model.
1374 if ((sctp_style(sk, TCP) && msg_name) ||
1375 (sinfo_flags & MSG_ADDR_OVER)) {
1376 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1377 if (!chunk_tp) {
1378 err = -EINVAL;
1379 goto out_free;
1381 } else
1382 chunk_tp = NULL;
1384 /* Auto-connect, if we aren't connected already. */
1385 if (sctp_state(asoc, CLOSED)) {
1386 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1387 if (err < 0)
1388 goto out_free;
1389 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1392 /* Break the message into multiple chunks of maximum size. */
1393 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1394 if (!datamsg) {
1395 err = -ENOMEM;
1396 goto out_free;
1399 /* Now send the (possibly) fragmented message. */
1400 list_for_each(pos, &datamsg->chunks) {
1401 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1402 sctp_datamsg_track(chunk);
1404 /* Do accounting for the write space. */
1405 sctp_set_owner_w(chunk);
1407 chunk->transport = chunk_tp;
1409 /* Send it to the lower layers. Note: all chunks
1410 * must either fail or succeed. The lower layer
1411 * works that way today. Keep it that way or this
1412 * breaks.
1414 err = sctp_primitive_SEND(asoc, chunk);
1415 /* Did the lower layer accept the chunk? */
1416 if (err)
1417 sctp_chunk_free(chunk);
1418 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1421 sctp_datamsg_free(datamsg);
1422 if (err)
1423 goto out_free;
1424 else
1425 err = msg_len;
1427 /* If we are already past ASSOCIATE, the lower
1428 * layers are responsible for association cleanup.
1430 goto out_unlock;
1432 out_free:
1433 if (new_asoc)
1434 sctp_association_free(asoc);
1435 out_unlock:
1436 sctp_release_sock(sk);
1438 out_nounlock:
1439 return sctp_error(sk, msg_flags, err);
1441 #if 0
1442 do_sock_err:
1443 if (msg_len)
1444 err = msg_len;
1445 else
1446 err = sock_error(sk);
1447 goto out;
1449 do_interrupted:
1450 if (msg_len)
1451 err = msg_len;
1452 goto out;
1453 #endif /* 0 */
1456 /* This is an extended version of skb_pull() that removes the data from the
1457 * start of a skb even when data is spread across the list of skb's in the
1458 * frag_list. len specifies the total amount of data that needs to be removed.
1459 * when 'len' bytes could be removed from the skb, it returns 0.
1460 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1461 * could not be removed.
1463 static int sctp_skb_pull(struct sk_buff *skb, int len)
1465 struct sk_buff *list;
1466 int skb_len = skb_headlen(skb);
1467 int rlen;
1469 if (len <= skb_len) {
1470 __skb_pull(skb, len);
1471 return 0;
1473 len -= skb_len;
1474 __skb_pull(skb, skb_len);
1476 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1477 rlen = sctp_skb_pull(list, len);
1478 skb->len -= (len-rlen);
1479 skb->data_len -= (len-rlen);
1481 if (!rlen)
1482 return 0;
1484 len = rlen;
1487 return len;
1490 /* API 3.1.3 recvmsg() - UDP Style Syntax
1492 * ssize_t recvmsg(int socket, struct msghdr *message,
1493 * int flags);
1495 * socket - the socket descriptor of the endpoint.
1496 * message - pointer to the msghdr structure which contains a single
1497 * user message and possibly some ancillary data.
1499 * See Section 5 for complete description of the data
1500 * structures.
1502 * flags - flags sent or received with the user message, see Section
1503 * 5 for complete description of the flags.
1505 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1507 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1508 struct msghdr *msg, size_t len, int noblock,
1509 int flags, int *addr_len)
1511 struct sctp_ulpevent *event = NULL;
1512 struct sctp_sock *sp = sctp_sk(sk);
1513 struct sk_buff *skb;
1514 int copied;
1515 int err = 0;
1516 int skb_len;
1518 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1519 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1520 "len", len, "knoblauch", noblock,
1521 "flags", flags, "addr_len", addr_len);
1523 sctp_lock_sock(sk);
1525 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1526 err = -ENOTCONN;
1527 goto out;
1530 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1531 if (!skb)
1532 goto out;
1534 /* Get the total length of the skb including any skb's in the
1535 * frag_list.
1537 skb_len = skb->len;
1539 copied = skb_len;
1540 if (copied > len)
1541 copied = len;
1543 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1545 event = sctp_skb2event(skb);
1547 if (err)
1548 goto out_free;
1550 sock_recv_timestamp(msg, sk, skb);
1551 if (sctp_ulpevent_is_notification(event)) {
1552 msg->msg_flags |= MSG_NOTIFICATION;
1553 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1554 } else {
1555 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1558 /* Check if we allow SCTP_SNDRCVINFO. */
1559 if (sp->subscribe.sctp_data_io_event)
1560 sctp_ulpevent_read_sndrcvinfo(event, msg);
1561 #if 0
1562 /* FIXME: we should be calling IP/IPv6 layers. */
1563 if (sk->sk_protinfo.af_inet.cmsg_flags)
1564 ip_cmsg_recv(msg, skb);
1565 #endif
1567 err = copied;
1569 /* If skb's length exceeds the user's buffer, update the skb and
1570 * push it back to the receive_queue so that the next call to
1571 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1573 if (skb_len > copied) {
1574 msg->msg_flags &= ~MSG_EOR;
1575 if (flags & MSG_PEEK)
1576 goto out_free;
1577 sctp_skb_pull(skb, copied);
1578 skb_queue_head(&sk->sk_receive_queue, skb);
1580 /* When only partial message is copied to the user, increase
1581 * rwnd by that amount. If all the data in the skb is read,
1582 * rwnd is updated when the event is freed.
1584 sctp_assoc_rwnd_increase(event->asoc, copied);
1585 goto out;
1586 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1587 (event->msg_flags & MSG_EOR))
1588 msg->msg_flags |= MSG_EOR;
1589 else
1590 msg->msg_flags &= ~MSG_EOR;
1592 out_free:
1593 if (flags & MSG_PEEK) {
1594 /* Release the skb reference acquired after peeking the skb in
1595 * sctp_skb_recv_datagram().
1597 kfree_skb(skb);
1598 } else {
1599 /* Free the event which includes releasing the reference to
1600 * the owner of the skb, freeing the skb and updating the
1601 * rwnd.
1603 sctp_ulpevent_free(event);
1605 out:
1606 sctp_release_sock(sk);
1607 return err;
1610 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1612 * This option is a on/off flag. If enabled no SCTP message
1613 * fragmentation will be performed. Instead if a message being sent
1614 * exceeds the current PMTU size, the message will NOT be sent and
1615 * instead a error will be indicated to the user.
1617 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1618 char __user *optval, int optlen)
1620 int val;
1622 if (optlen < sizeof(int))
1623 return -EINVAL;
1625 if (get_user(val, (int __user *)optval))
1626 return -EFAULT;
1628 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1630 return 0;
1633 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1634 int optlen)
1636 if (optlen != sizeof(struct sctp_event_subscribe))
1637 return -EINVAL;
1638 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1639 return -EFAULT;
1640 return 0;
1643 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1645 * This socket option is applicable to the UDP-style socket only. When
1646 * set it will cause associations that are idle for more than the
1647 * specified number of seconds to automatically close. An association
1648 * being idle is defined an association that has NOT sent or received
1649 * user data. The special value of '0' indicates that no automatic
1650 * close of any associations should be performed. The option expects an
1651 * integer defining the number of seconds of idle time before an
1652 * association is closed.
1654 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1655 int optlen)
1657 struct sctp_sock *sp = sctp_sk(sk);
1659 /* Applicable to UDP-style socket only */
1660 if (sctp_style(sk, TCP))
1661 return -EOPNOTSUPP;
1662 if (optlen != sizeof(int))
1663 return -EINVAL;
1664 if (copy_from_user(&sp->autoclose, optval, optlen))
1665 return -EFAULT;
1667 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1668 return 0;
1671 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1673 * Applications can enable or disable heartbeats for any peer address of
1674 * an association, modify an address's heartbeat interval, force a
1675 * heartbeat to be sent immediately, and adjust the address's maximum
1676 * number of retransmissions sent before an address is considered
1677 * unreachable. The following structure is used to access and modify an
1678 * address's parameters:
1680 * struct sctp_paddrparams {
1681 * sctp_assoc_t spp_assoc_id;
1682 * struct sockaddr_storage spp_address;
1683 * uint32_t spp_hbinterval;
1684 * uint16_t spp_pathmaxrxt;
1685 * };
1687 * spp_assoc_id - (UDP style socket) This is filled in the application,
1688 * and identifies the association for this query.
1689 * spp_address - This specifies which address is of interest.
1690 * spp_hbinterval - This contains the value of the heartbeat interval,
1691 * in milliseconds. A value of 0, when modifying the
1692 * parameter, specifies that the heartbeat on this
1693 * address should be disabled. A value of UINT32_MAX
1694 * (4294967295), when modifying the parameter,
1695 * specifies that a heartbeat should be sent
1696 * immediately to the peer address, and the current
1697 * interval should remain unchanged.
1698 * spp_pathmaxrxt - This contains the maximum number of
1699 * retransmissions before this address shall be
1700 * considered unreachable.
1702 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1703 char __user *optval, int optlen)
1705 struct sctp_paddrparams params;
1706 struct sctp_transport *trans;
1707 int error;
1709 if (optlen != sizeof(struct sctp_paddrparams))
1710 return -EINVAL;
1711 if (copy_from_user(&params, optval, optlen))
1712 return -EFAULT;
1715 * API 7. Socket Options (setting the default value for the endpoint)
1716 * All options that support specific settings on an association by
1717 * filling in either an association id variable or a sockaddr_storage
1718 * SHOULD also support setting of the same value for the entire endpoint
1719 * (i.e. future associations). To accomplish this the following logic is
1720 * used when setting one of these options:
1722 * c) If neither the sockaddr_storage or association identification is
1723 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1724 * the association identification is 0, the settings are a default
1725 * and to be applied to the endpoint (all future associations).
1728 /* update default value for endpoint (all future associations) */
1729 if (!params.spp_assoc_id &&
1730 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
1731 /* Manual heartbeat on an endpoint is invalid. */
1732 if (0xffffffff == params.spp_hbinterval)
1733 return -EINVAL;
1734 else if (params.spp_hbinterval)
1735 sctp_sk(sk)->paddrparam.spp_hbinterval =
1736 params.spp_hbinterval;
1737 if (params.spp_pathmaxrxt)
1738 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
1739 params.spp_pathmaxrxt;
1740 return 0;
1743 trans = sctp_addr_id2transport(sk, &params.spp_address,
1744 params.spp_assoc_id);
1745 if (!trans)
1746 return -EINVAL;
1748 /* Applications can enable or disable heartbeats for any peer address
1749 * of an association, modify an address's heartbeat interval, force a
1750 * heartbeat to be sent immediately, and adjust the address's maximum
1751 * number of retransmissions sent before an address is considered
1752 * unreachable.
1754 * The value of the heartbeat interval, in milliseconds. A value of
1755 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1756 * that a heartbeat should be sent immediately to the peer address,
1757 * and the current interval should remain unchanged.
1759 if (0xffffffff == params.spp_hbinterval) {
1760 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1761 if (error)
1762 return error;
1763 } else {
1764 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1765 * when modifying the parameter, specifies that the heartbeat on this
1766 * address should be disabled.
1768 if (params.spp_hbinterval) {
1769 trans->hb_allowed = 1;
1770 trans->hb_interval =
1771 msecs_to_jiffies(params.spp_hbinterval);
1772 } else
1773 trans->hb_allowed = 0;
1776 /* spp_pathmaxrxt contains the maximum number of retransmissions
1777 * before this address shall be considered unreachable.
1779 if (params.spp_pathmaxrxt)
1780 trans->max_retrans = params.spp_pathmaxrxt;
1782 return 0;
1785 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1787 * Applications can specify protocol parameters for the default association
1788 * initialization. The option name argument to setsockopt() and getsockopt()
1789 * is SCTP_INITMSG.
1791 * Setting initialization parameters is effective only on an unconnected
1792 * socket (for UDP-style sockets only future associations are effected
1793 * by the change). With TCP-style sockets, this option is inherited by
1794 * sockets derived from a listener socket.
1796 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1798 struct sctp_initmsg sinit;
1799 struct sctp_sock *sp = sctp_sk(sk);
1801 if (optlen != sizeof(struct sctp_initmsg))
1802 return -EINVAL;
1803 if (copy_from_user(&sinit, optval, optlen))
1804 return -EFAULT;
1806 if (sinit.sinit_num_ostreams)
1807 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1808 if (sinit.sinit_max_instreams)
1809 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1810 if (sinit.sinit_max_attempts)
1811 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1812 if (sinit.sinit_max_init_timeo)
1813 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1815 return 0;
1819 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1821 * Applications that wish to use the sendto() system call may wish to
1822 * specify a default set of parameters that would normally be supplied
1823 * through the inclusion of ancillary data. This socket option allows
1824 * such an application to set the default sctp_sndrcvinfo structure.
1825 * The application that wishes to use this socket option simply passes
1826 * in to this call the sctp_sndrcvinfo structure defined in Section
1827 * 5.2.2) The input parameters accepted by this call include
1828 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1829 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1830 * to this call if the caller is using the UDP model.
1832 static int sctp_setsockopt_default_send_param(struct sock *sk,
1833 char __user *optval, int optlen)
1835 struct sctp_sndrcvinfo info;
1836 struct sctp_association *asoc;
1837 struct sctp_sock *sp = sctp_sk(sk);
1839 if (optlen != sizeof(struct sctp_sndrcvinfo))
1840 return -EINVAL;
1841 if (copy_from_user(&info, optval, optlen))
1842 return -EFAULT;
1844 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1845 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1846 return -EINVAL;
1848 if (asoc) {
1849 asoc->default_stream = info.sinfo_stream;
1850 asoc->default_flags = info.sinfo_flags;
1851 asoc->default_ppid = info.sinfo_ppid;
1852 asoc->default_context = info.sinfo_context;
1853 asoc->default_timetolive = info.sinfo_timetolive;
1854 } else {
1855 sp->default_stream = info.sinfo_stream;
1856 sp->default_flags = info.sinfo_flags;
1857 sp->default_ppid = info.sinfo_ppid;
1858 sp->default_context = info.sinfo_context;
1859 sp->default_timetolive = info.sinfo_timetolive;
1862 return 0;
1865 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1867 * Requests that the local SCTP stack use the enclosed peer address as
1868 * the association primary. The enclosed address must be one of the
1869 * association peer's addresses.
1871 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1872 int optlen)
1874 struct sctp_prim prim;
1875 struct sctp_transport *trans;
1877 if (optlen != sizeof(struct sctp_prim))
1878 return -EINVAL;
1880 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1881 return -EFAULT;
1883 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1884 if (!trans)
1885 return -EINVAL;
1887 sctp_assoc_set_primary(trans->asoc, trans);
1889 return 0;
1893 * 7.1.5 SCTP_NODELAY
1895 * Turn on/off any Nagle-like algorithm. This means that packets are
1896 * generally sent as soon as possible and no unnecessary delays are
1897 * introduced, at the cost of more packets in the network. Expects an
1898 * integer boolean flag.
1900 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1901 int optlen)
1903 int val;
1905 if (optlen < sizeof(int))
1906 return -EINVAL;
1907 if (get_user(val, (int __user *)optval))
1908 return -EFAULT;
1910 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1911 return 0;
1916 * 7.1.1 SCTP_RTOINFO
1918 * The protocol parameters used to initialize and bound retransmission
1919 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1920 * and modify these parameters.
1921 * All parameters are time values, in milliseconds. A value of 0, when
1922 * modifying the parameters, indicates that the current value should not
1923 * be changed.
1926 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1927 struct sctp_rtoinfo rtoinfo;
1928 struct sctp_association *asoc;
1930 if (optlen != sizeof (struct sctp_rtoinfo))
1931 return -EINVAL;
1933 if (copy_from_user(&rtoinfo, optval, optlen))
1934 return -EFAULT;
1936 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1938 /* Set the values to the specific association */
1939 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1940 return -EINVAL;
1942 if (asoc) {
1943 if (rtoinfo.srto_initial != 0)
1944 asoc->rto_initial =
1945 msecs_to_jiffies(rtoinfo.srto_initial);
1946 if (rtoinfo.srto_max != 0)
1947 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1948 if (rtoinfo.srto_min != 0)
1949 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1950 } else {
1951 /* If there is no association or the association-id = 0
1952 * set the values to the endpoint.
1954 struct sctp_sock *sp = sctp_sk(sk);
1956 if (rtoinfo.srto_initial != 0)
1957 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1958 if (rtoinfo.srto_max != 0)
1959 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1960 if (rtoinfo.srto_min != 0)
1961 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1964 return 0;
1969 * 7.1.2 SCTP_ASSOCINFO
1971 * This option is used to tune the the maximum retransmission attempts
1972 * of the association.
1973 * Returns an error if the new association retransmission value is
1974 * greater than the sum of the retransmission value of the peer.
1975 * See [SCTP] for more information.
1978 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1981 struct sctp_assocparams assocparams;
1982 struct sctp_association *asoc;
1984 if (optlen != sizeof(struct sctp_assocparams))
1985 return -EINVAL;
1986 if (copy_from_user(&assocparams, optval, optlen))
1987 return -EFAULT;
1989 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1991 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1992 return -EINVAL;
1994 /* Set the values to the specific association */
1995 if (asoc) {
1996 if (assocparams.sasoc_asocmaxrxt != 0)
1997 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1998 if (assocparams.sasoc_cookie_life != 0) {
1999 asoc->cookie_life.tv_sec =
2000 assocparams.sasoc_cookie_life / 1000;
2001 asoc->cookie_life.tv_usec =
2002 (assocparams.sasoc_cookie_life % 1000)
2003 * 1000;
2005 } else {
2006 /* Set the values to the endpoint */
2007 struct sctp_sock *sp = sctp_sk(sk);
2009 if (assocparams.sasoc_asocmaxrxt != 0)
2010 sp->assocparams.sasoc_asocmaxrxt =
2011 assocparams.sasoc_asocmaxrxt;
2012 if (assocparams.sasoc_cookie_life != 0)
2013 sp->assocparams.sasoc_cookie_life =
2014 assocparams.sasoc_cookie_life;
2016 return 0;
2020 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2022 * This socket option is a boolean flag which turns on or off mapped V4
2023 * addresses. If this option is turned on and the socket is type
2024 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2025 * If this option is turned off, then no mapping will be done of V4
2026 * addresses and a user will receive both PF_INET6 and PF_INET type
2027 * addresses on the socket.
2029 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2031 int val;
2032 struct sctp_sock *sp = sctp_sk(sk);
2034 if (optlen < sizeof(int))
2035 return -EINVAL;
2036 if (get_user(val, (int __user *)optval))
2037 return -EFAULT;
2038 if (val)
2039 sp->v4mapped = 1;
2040 else
2041 sp->v4mapped = 0;
2043 return 0;
2047 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2049 * This socket option specifies the maximum size to put in any outgoing
2050 * SCTP chunk. If a message is larger than this size it will be
2051 * fragmented by SCTP into the specified size. Note that the underlying
2052 * SCTP implementation may fragment into smaller sized chunks when the
2053 * PMTU of the underlying association is smaller than the value set by
2054 * the user.
2056 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2058 struct sctp_association *asoc;
2059 struct list_head *pos;
2060 struct sctp_sock *sp = sctp_sk(sk);
2061 int val;
2063 if (optlen < sizeof(int))
2064 return -EINVAL;
2065 if (get_user(val, (int __user *)optval))
2066 return -EFAULT;
2067 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
2068 return -EINVAL;
2069 sp->user_frag = val;
2071 if (val) {
2072 /* Update the frag_point of the existing associations. */
2073 list_for_each(pos, &(sp->ep->asocs)) {
2074 asoc = list_entry(pos, struct sctp_association, asocs);
2075 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2079 return 0;
2084 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2086 * Requests that the peer mark the enclosed address as the association
2087 * primary. The enclosed address must be one of the association's
2088 * locally bound addresses. The following structure is used to make a
2089 * set primary request:
2091 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2092 int optlen)
2094 struct sctp_sock *sp;
2095 struct sctp_endpoint *ep;
2096 struct sctp_association *asoc = NULL;
2097 struct sctp_setpeerprim prim;
2098 struct sctp_chunk *chunk;
2099 int err;
2101 sp = sctp_sk(sk);
2102 ep = sp->ep;
2104 if (!sctp_addip_enable)
2105 return -EPERM;
2107 if (optlen != sizeof(struct sctp_setpeerprim))
2108 return -EINVAL;
2110 if (copy_from_user(&prim, optval, optlen))
2111 return -EFAULT;
2113 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2114 if (!asoc)
2115 return -EINVAL;
2117 if (!asoc->peer.asconf_capable)
2118 return -EPERM;
2120 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2121 return -EPERM;
2123 if (!sctp_state(asoc, ESTABLISHED))
2124 return -ENOTCONN;
2126 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2127 return -EADDRNOTAVAIL;
2129 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2130 chunk = sctp_make_asconf_set_prim(asoc,
2131 (union sctp_addr *)&prim.sspp_addr);
2132 if (!chunk)
2133 return -ENOMEM;
2135 err = sctp_send_asconf(asoc, chunk);
2137 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2139 return err;
2142 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2143 int optlen)
2145 __u32 val;
2147 if (optlen < sizeof(__u32))
2148 return -EINVAL;
2149 if (copy_from_user(&val, optval, sizeof(__u32)))
2150 return -EFAULT;
2152 sctp_sk(sk)->adaption_ind = val;
2154 return 0;
2157 /* API 6.2 setsockopt(), getsockopt()
2159 * Applications use setsockopt() and getsockopt() to set or retrieve
2160 * socket options. Socket options are used to change the default
2161 * behavior of sockets calls. They are described in Section 7.
2163 * The syntax is:
2165 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2166 * int __user *optlen);
2167 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2168 * int optlen);
2170 * sd - the socket descript.
2171 * level - set to IPPROTO_SCTP for all SCTP options.
2172 * optname - the option name.
2173 * optval - the buffer to store the value of the option.
2174 * optlen - the size of the buffer.
2176 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2177 char __user *optval, int optlen)
2179 int retval = 0;
2181 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2182 sk, optname);
2184 /* I can hardly begin to describe how wrong this is. This is
2185 * so broken as to be worse than useless. The API draft
2186 * REALLY is NOT helpful here... I am not convinced that the
2187 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2188 * are at all well-founded.
2190 if (level != SOL_SCTP) {
2191 struct sctp_af *af = sctp_sk(sk)->pf->af;
2192 retval = af->setsockopt(sk, level, optname, optval, optlen);
2193 goto out_nounlock;
2196 sctp_lock_sock(sk);
2198 switch (optname) {
2199 case SCTP_SOCKOPT_BINDX_ADD:
2200 /* 'optlen' is the size of the addresses buffer. */
2201 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2202 optlen, SCTP_BINDX_ADD_ADDR);
2203 break;
2205 case SCTP_SOCKOPT_BINDX_REM:
2206 /* 'optlen' is the size of the addresses buffer. */
2207 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2208 optlen, SCTP_BINDX_REM_ADDR);
2209 break;
2211 case SCTP_DISABLE_FRAGMENTS:
2212 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2213 break;
2215 case SCTP_EVENTS:
2216 retval = sctp_setsockopt_events(sk, optval, optlen);
2217 break;
2219 case SCTP_AUTOCLOSE:
2220 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2221 break;
2223 case SCTP_PEER_ADDR_PARAMS:
2224 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2225 break;
2227 case SCTP_INITMSG:
2228 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2229 break;
2230 case SCTP_DEFAULT_SEND_PARAM:
2231 retval = sctp_setsockopt_default_send_param(sk, optval,
2232 optlen);
2233 break;
2234 case SCTP_PRIMARY_ADDR:
2235 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2236 break;
2237 case SCTP_SET_PEER_PRIMARY_ADDR:
2238 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2239 break;
2240 case SCTP_NODELAY:
2241 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2242 break;
2243 case SCTP_RTOINFO:
2244 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2245 break;
2246 case SCTP_ASSOCINFO:
2247 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2248 break;
2249 case SCTP_I_WANT_MAPPED_V4_ADDR:
2250 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2251 break;
2252 case SCTP_MAXSEG:
2253 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2254 break;
2255 case SCTP_ADAPTION_LAYER:
2256 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2257 break;
2259 default:
2260 retval = -ENOPROTOOPT;
2261 break;
2264 sctp_release_sock(sk);
2266 out_nounlock:
2267 return retval;
2270 /* API 3.1.6 connect() - UDP Style Syntax
2272 * An application may use the connect() call in the UDP model to initiate an
2273 * association without sending data.
2275 * The syntax is:
2277 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2279 * sd: the socket descriptor to have a new association added to.
2281 * nam: the address structure (either struct sockaddr_in or struct
2282 * sockaddr_in6 defined in RFC2553 [7]).
2284 * len: the size of the address.
2286 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2287 int addr_len)
2289 struct sctp_sock *sp;
2290 struct sctp_endpoint *ep;
2291 struct sctp_association *asoc;
2292 struct sctp_transport *transport;
2293 union sctp_addr to;
2294 struct sctp_af *af;
2295 sctp_scope_t scope;
2296 long timeo;
2297 int err = 0;
2299 sctp_lock_sock(sk);
2301 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2302 __FUNCTION__, sk, uaddr, addr_len);
2304 sp = sctp_sk(sk);
2305 ep = sp->ep;
2307 /* connect() cannot be done on a socket that is already in ESTABLISHED
2308 * state - UDP-style peeled off socket or a TCP-style socket that
2309 * is already connected.
2310 * It cannot be done even on a TCP-style listening socket.
2312 if (sctp_sstate(sk, ESTABLISHED) ||
2313 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2314 err = -EISCONN;
2315 goto out_unlock;
2318 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2319 if (err)
2320 goto out_unlock;
2322 if (addr_len > sizeof(to))
2323 addr_len = sizeof(to);
2324 memcpy(&to, uaddr, addr_len);
2325 to.v4.sin_port = ntohs(to.v4.sin_port);
2327 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2328 if (asoc) {
2329 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2330 err = -EISCONN;
2331 else
2332 err = -EALREADY;
2333 goto out_unlock;
2336 /* If we could not find a matching association on the endpoint,
2337 * make sure that there is no peeled-off association matching the
2338 * peer address even on another socket.
2340 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2341 err = -EADDRNOTAVAIL;
2342 goto out_unlock;
2345 /* If a bind() or sctp_bindx() is not called prior to a connect()
2346 * call, the system picks an ephemeral port and will choose an address
2347 * set equivalent to binding with a wildcard address.
2349 if (!ep->base.bind_addr.port) {
2350 if (sctp_autobind(sk)) {
2351 err = -EAGAIN;
2352 goto out_unlock;
2356 scope = sctp_scope(&to);
2357 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2358 if (!asoc) {
2359 err = -ENOMEM;
2360 goto out_unlock;
2363 /* Prime the peer's transport structures. */
2364 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2365 if (!transport) {
2366 sctp_association_free(asoc);
2367 goto out_unlock;
2369 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2370 if (err < 0) {
2371 sctp_association_free(asoc);
2372 goto out_unlock;
2375 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2376 if (err < 0) {
2377 sctp_association_free(asoc);
2378 goto out_unlock;
2381 /* Initialize sk's dport and daddr for getpeername() */
2382 inet_sk(sk)->dport = htons(asoc->peer.port);
2383 af = sctp_get_af_specific(to.sa.sa_family);
2384 af->to_sk_daddr(&to, sk);
2386 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2387 err = sctp_wait_for_connect(asoc, &timeo);
2389 out_unlock:
2390 sctp_release_sock(sk);
2392 return err;
2395 /* FIXME: Write comments. */
2396 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2398 return -EOPNOTSUPP; /* STUB */
2401 /* 4.1.4 accept() - TCP Style Syntax
2403 * Applications use accept() call to remove an established SCTP
2404 * association from the accept queue of the endpoint. A new socket
2405 * descriptor will be returned from accept() to represent the newly
2406 * formed association.
2408 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2410 struct sctp_sock *sp;
2411 struct sctp_endpoint *ep;
2412 struct sock *newsk = NULL;
2413 struct sctp_association *asoc;
2414 long timeo;
2415 int error = 0;
2417 sctp_lock_sock(sk);
2419 sp = sctp_sk(sk);
2420 ep = sp->ep;
2422 if (!sctp_style(sk, TCP)) {
2423 error = -EOPNOTSUPP;
2424 goto out;
2427 if (!sctp_sstate(sk, LISTENING)) {
2428 error = -EINVAL;
2429 goto out;
2432 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2434 error = sctp_wait_for_accept(sk, timeo);
2435 if (error)
2436 goto out;
2438 /* We treat the list of associations on the endpoint as the accept
2439 * queue and pick the first association on the list.
2441 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2443 newsk = sp->pf->create_accept_sk(sk, asoc);
2444 if (!newsk) {
2445 error = -ENOMEM;
2446 goto out;
2449 /* Populate the fields of the newsk from the oldsk and migrate the
2450 * asoc to the newsk.
2452 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2454 out:
2455 sctp_release_sock(sk);
2456 *err = error;
2457 return newsk;
2460 /* The SCTP ioctl handler. */
2461 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2463 return -ENOIOCTLCMD;
2466 /* This is the function which gets called during socket creation to
2467 * initialized the SCTP-specific portion of the sock.
2468 * The sock structure should already be zero-filled memory.
2470 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2472 struct sctp_endpoint *ep;
2473 struct sctp_sock *sp;
2475 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2477 sp = sctp_sk(sk);
2479 /* Initialize the SCTP per socket area. */
2480 switch (sk->sk_type) {
2481 case SOCK_SEQPACKET:
2482 sp->type = SCTP_SOCKET_UDP;
2483 break;
2484 case SOCK_STREAM:
2485 sp->type = SCTP_SOCKET_TCP;
2486 break;
2487 default:
2488 return -ESOCKTNOSUPPORT;
2491 /* Initialize default send parameters. These parameters can be
2492 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2494 sp->default_stream = 0;
2495 sp->default_ppid = 0;
2496 sp->default_flags = 0;
2497 sp->default_context = 0;
2498 sp->default_timetolive = 0;
2500 /* Initialize default setup parameters. These parameters
2501 * can be modified with the SCTP_INITMSG socket option or
2502 * overridden by the SCTP_INIT CMSG.
2504 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2505 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2506 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2507 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2509 /* Initialize default RTO related parameters. These parameters can
2510 * be modified for with the SCTP_RTOINFO socket option.
2512 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2513 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2514 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2516 /* Initialize default association related parameters. These parameters
2517 * can be modified with the SCTP_ASSOCINFO socket option.
2519 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2520 sp->assocparams.sasoc_number_peer_destinations = 0;
2521 sp->assocparams.sasoc_peer_rwnd = 0;
2522 sp->assocparams.sasoc_local_rwnd = 0;
2523 sp->assocparams.sasoc_cookie_life =
2524 jiffies_to_msecs(sctp_valid_cookie_life);
2526 /* Initialize default event subscriptions. By default, all the
2527 * options are off.
2529 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2531 /* Default Peer Address Parameters. These defaults can
2532 * be modified via SCTP_PEER_ADDR_PARAMS
2534 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2535 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2537 /* If enabled no SCTP message fragmentation will be performed.
2538 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2540 sp->disable_fragments = 0;
2542 /* Turn on/off any Nagle-like algorithm. */
2543 sp->nodelay = 1;
2545 /* Enable by default. */
2546 sp->v4mapped = 1;
2548 /* Auto-close idle associations after the configured
2549 * number of seconds. A value of 0 disables this
2550 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2551 * for UDP-style sockets only.
2553 sp->autoclose = 0;
2555 /* User specified fragmentation limit. */
2556 sp->user_frag = 0;
2558 sp->adaption_ind = 0;
2560 sp->pf = sctp_get_pf_specific(sk->sk_family);
2562 /* Control variables for partial data delivery. */
2563 sp->pd_mode = 0;
2564 skb_queue_head_init(&sp->pd_lobby);
2566 /* Create a per socket endpoint structure. Even if we
2567 * change the data structure relationships, this may still
2568 * be useful for storing pre-connect address information.
2570 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2571 if (!ep)
2572 return -ENOMEM;
2574 sp->ep = ep;
2575 sp->hmac = NULL;
2577 SCTP_DBG_OBJCNT_INC(sock);
2578 return 0;
2581 /* Cleanup any SCTP per socket resources. */
2582 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2584 struct sctp_endpoint *ep;
2586 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2588 /* Release our hold on the endpoint. */
2589 ep = sctp_sk(sk)->ep;
2590 sctp_endpoint_free(ep);
2592 return 0;
2595 /* API 4.1.7 shutdown() - TCP Style Syntax
2596 * int shutdown(int socket, int how);
2598 * sd - the socket descriptor of the association to be closed.
2599 * how - Specifies the type of shutdown. The values are
2600 * as follows:
2601 * SHUT_RD
2602 * Disables further receive operations. No SCTP
2603 * protocol action is taken.
2604 * SHUT_WR
2605 * Disables further send operations, and initiates
2606 * the SCTP shutdown sequence.
2607 * SHUT_RDWR
2608 * Disables further send and receive operations
2609 * and initiates the SCTP shutdown sequence.
2611 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2613 struct sctp_endpoint *ep;
2614 struct sctp_association *asoc;
2616 if (!sctp_style(sk, TCP))
2617 return;
2619 if (how & SEND_SHUTDOWN) {
2620 ep = sctp_sk(sk)->ep;
2621 if (!list_empty(&ep->asocs)) {
2622 asoc = list_entry(ep->asocs.next,
2623 struct sctp_association, asocs);
2624 sctp_primitive_SHUTDOWN(asoc, NULL);
2629 /* 7.2.1 Association Status (SCTP_STATUS)
2631 * Applications can retrieve current status information about an
2632 * association, including association state, peer receiver window size,
2633 * number of unacked data chunks, and number of data chunks pending
2634 * receipt. This information is read-only.
2636 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2637 char __user *optval,
2638 int __user *optlen)
2640 struct sctp_status status;
2641 struct sctp_association *asoc = NULL;
2642 struct sctp_transport *transport;
2643 sctp_assoc_t associd;
2644 int retval = 0;
2646 if (len != sizeof(status)) {
2647 retval = -EINVAL;
2648 goto out;
2651 if (copy_from_user(&status, optval, sizeof(status))) {
2652 retval = -EFAULT;
2653 goto out;
2656 associd = status.sstat_assoc_id;
2657 asoc = sctp_id2assoc(sk, associd);
2658 if (!asoc) {
2659 retval = -EINVAL;
2660 goto out;
2663 transport = asoc->peer.primary_path;
2665 status.sstat_assoc_id = sctp_assoc2id(asoc);
2666 status.sstat_state = asoc->state;
2667 status.sstat_rwnd = asoc->peer.rwnd;
2668 status.sstat_unackdata = asoc->unack_data;
2670 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2671 status.sstat_instrms = asoc->c.sinit_max_instreams;
2672 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2673 status.sstat_fragmentation_point = asoc->frag_point;
2674 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2675 memcpy(&status.sstat_primary.spinfo_address,
2676 &(transport->ipaddr), sizeof(union sctp_addr));
2677 /* Map ipv4 address into v4-mapped-on-v6 address. */
2678 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2679 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2680 status.sstat_primary.spinfo_state = transport->active;
2681 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2682 status.sstat_primary.spinfo_srtt = transport->srtt;
2683 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2684 status.sstat_primary.spinfo_mtu = transport->pmtu;
2686 if (put_user(len, optlen)) {
2687 retval = -EFAULT;
2688 goto out;
2691 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
2692 len, status.sstat_state, status.sstat_rwnd,
2693 status.sstat_assoc_id);
2695 if (copy_to_user(optval, &status, len)) {
2696 retval = -EFAULT;
2697 goto out;
2700 out:
2701 return (retval);
2705 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2707 * Applications can retrieve information about a specific peer address
2708 * of an association, including its reachability state, congestion
2709 * window, and retransmission timer values. This information is
2710 * read-only.
2712 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2713 char __user *optval,
2714 int __user *optlen)
2716 struct sctp_paddrinfo pinfo;
2717 struct sctp_transport *transport;
2718 int retval = 0;
2720 if (len != sizeof(pinfo)) {
2721 retval = -EINVAL;
2722 goto out;
2725 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2726 retval = -EFAULT;
2727 goto out;
2730 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2731 pinfo.spinfo_assoc_id);
2732 if (!transport)
2733 return -EINVAL;
2735 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2736 pinfo.spinfo_state = transport->active;
2737 pinfo.spinfo_cwnd = transport->cwnd;
2738 pinfo.spinfo_srtt = transport->srtt;
2739 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2740 pinfo.spinfo_mtu = transport->pmtu;
2742 if (put_user(len, optlen)) {
2743 retval = -EFAULT;
2744 goto out;
2747 if (copy_to_user(optval, &pinfo, len)) {
2748 retval = -EFAULT;
2749 goto out;
2752 out:
2753 return (retval);
2756 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2758 * This option is a on/off flag. If enabled no SCTP message
2759 * fragmentation will be performed. Instead if a message being sent
2760 * exceeds the current PMTU size, the message will NOT be sent and
2761 * instead a error will be indicated to the user.
2763 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2764 char __user *optval, int __user *optlen)
2766 int val;
2768 if (len < sizeof(int))
2769 return -EINVAL;
2771 len = sizeof(int);
2772 val = (sctp_sk(sk)->disable_fragments == 1);
2773 if (put_user(len, optlen))
2774 return -EFAULT;
2775 if (copy_to_user(optval, &val, len))
2776 return -EFAULT;
2777 return 0;
2780 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2782 * This socket option is used to specify various notifications and
2783 * ancillary data the user wishes to receive.
2785 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2786 int __user *optlen)
2788 if (len != sizeof(struct sctp_event_subscribe))
2789 return -EINVAL;
2790 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2791 return -EFAULT;
2792 return 0;
2795 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2797 * This socket option is applicable to the UDP-style socket only. When
2798 * set it will cause associations that are idle for more than the
2799 * specified number of seconds to automatically close. An association
2800 * being idle is defined an association that has NOT sent or received
2801 * user data. The special value of '0' indicates that no automatic
2802 * close of any associations should be performed. The option expects an
2803 * integer defining the number of seconds of idle time before an
2804 * association is closed.
2806 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2808 /* Applicable to UDP-style socket only */
2809 if (sctp_style(sk, TCP))
2810 return -EOPNOTSUPP;
2811 if (len != sizeof(int))
2812 return -EINVAL;
2813 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2814 return -EFAULT;
2815 return 0;
2818 /* Helper routine to branch off an association to a new socket. */
2819 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2820 struct socket **sockp)
2822 struct sock *sk = asoc->base.sk;
2823 struct socket *sock;
2824 int err = 0;
2826 /* An association cannot be branched off from an already peeled-off
2827 * socket, nor is this supported for tcp style sockets.
2829 if (!sctp_style(sk, UDP))
2830 return -EINVAL;
2832 /* Create a new socket. */
2833 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2834 if (err < 0)
2835 return err;
2837 /* Populate the fields of the newsk from the oldsk and migrate the
2838 * asoc to the newsk.
2840 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2841 *sockp = sock;
2843 return err;
2846 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2848 sctp_peeloff_arg_t peeloff;
2849 struct socket *newsock;
2850 int retval = 0;
2851 struct sctp_association *asoc;
2853 if (len != sizeof(sctp_peeloff_arg_t))
2854 return -EINVAL;
2855 if (copy_from_user(&peeloff, optval, len))
2856 return -EFAULT;
2858 asoc = sctp_id2assoc(sk, peeloff.associd);
2859 if (!asoc) {
2860 retval = -EINVAL;
2861 goto out;
2864 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2866 retval = sctp_do_peeloff(asoc, &newsock);
2867 if (retval < 0)
2868 goto out;
2870 /* Map the socket to an unused fd that can be returned to the user. */
2871 retval = sock_map_fd(newsock);
2872 if (retval < 0) {
2873 sock_release(newsock);
2874 goto out;
2877 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2878 __FUNCTION__, sk, asoc, newsock->sk, retval);
2880 /* Return the fd mapped to the new socket. */
2881 peeloff.sd = retval;
2882 if (copy_to_user(optval, &peeloff, len))
2883 retval = -EFAULT;
2885 out:
2886 return retval;
2889 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2891 * Applications can enable or disable heartbeats for any peer address of
2892 * an association, modify an address's heartbeat interval, force a
2893 * heartbeat to be sent immediately, and adjust the address's maximum
2894 * number of retransmissions sent before an address is considered
2895 * unreachable. The following structure is used to access and modify an
2896 * address's parameters:
2898 * struct sctp_paddrparams {
2899 * sctp_assoc_t spp_assoc_id;
2900 * struct sockaddr_storage spp_address;
2901 * uint32_t spp_hbinterval;
2902 * uint16_t spp_pathmaxrxt;
2903 * };
2905 * spp_assoc_id - (UDP style socket) This is filled in the application,
2906 * and identifies the association for this query.
2907 * spp_address - This specifies which address is of interest.
2908 * spp_hbinterval - This contains the value of the heartbeat interval,
2909 * in milliseconds. A value of 0, when modifying the
2910 * parameter, specifies that the heartbeat on this
2911 * address should be disabled. A value of UINT32_MAX
2912 * (4294967295), when modifying the parameter,
2913 * specifies that a heartbeat should be sent
2914 * immediately to the peer address, and the current
2915 * interval should remain unchanged.
2916 * spp_pathmaxrxt - This contains the maximum number of
2917 * retransmissions before this address shall be
2918 * considered unreachable.
2920 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2921 char __user *optval, int __user *optlen)
2923 struct sctp_paddrparams params;
2924 struct sctp_transport *trans;
2926 if (len != sizeof(struct sctp_paddrparams))
2927 return -EINVAL;
2928 if (copy_from_user(&params, optval, len))
2929 return -EFAULT;
2931 /* If no association id is specified retrieve the default value
2932 * for the endpoint that will be used for all future associations
2934 if (!params.spp_assoc_id &&
2935 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2936 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
2937 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
2939 goto done;
2942 trans = sctp_addr_id2transport(sk, &params.spp_address,
2943 params.spp_assoc_id);
2944 if (!trans)
2945 return -EINVAL;
2947 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2948 * when modifying the parameter, specifies that the heartbeat on this
2949 * address should be disabled.
2951 if (!trans->hb_allowed)
2952 params.spp_hbinterval = 0;
2953 else
2954 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2956 /* spp_pathmaxrxt contains the maximum number of retransmissions
2957 * before this address shall be considered unreachable.
2959 params.spp_pathmaxrxt = trans->max_retrans;
2961 done:
2962 if (copy_to_user(optval, &params, len))
2963 return -EFAULT;
2965 if (put_user(len, optlen))
2966 return -EFAULT;
2968 return 0;
2971 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2973 * Applications can specify protocol parameters for the default association
2974 * initialization. The option name argument to setsockopt() and getsockopt()
2975 * is SCTP_INITMSG.
2977 * Setting initialization parameters is effective only on an unconnected
2978 * socket (for UDP-style sockets only future associations are effected
2979 * by the change). With TCP-style sockets, this option is inherited by
2980 * sockets derived from a listener socket.
2982 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2984 if (len != sizeof(struct sctp_initmsg))
2985 return -EINVAL;
2986 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2987 return -EFAULT;
2988 return 0;
2991 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2992 char __user *optval, int __user *optlen)
2994 sctp_assoc_t id;
2995 struct sctp_association *asoc;
2996 struct list_head *pos;
2997 int cnt = 0;
2999 if (len != sizeof(sctp_assoc_t))
3000 return -EINVAL;
3002 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3003 return -EFAULT;
3005 /* For UDP-style sockets, id specifies the association to query. */
3006 asoc = sctp_id2assoc(sk, id);
3007 if (!asoc)
3008 return -EINVAL;
3010 list_for_each(pos, &asoc->peer.transport_addr_list) {
3011 cnt ++;
3014 return cnt;
3017 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3018 char __user *optval, int __user *optlen)
3020 struct sctp_association *asoc;
3021 struct list_head *pos;
3022 int cnt = 0;
3023 struct sctp_getaddrs getaddrs;
3024 struct sctp_transport *from;
3025 void __user *to;
3026 union sctp_addr temp;
3027 struct sctp_sock *sp = sctp_sk(sk);
3028 int addrlen;
3030 if (len != sizeof(struct sctp_getaddrs))
3031 return -EINVAL;
3033 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3034 return -EFAULT;
3036 if (getaddrs.addr_num <= 0) return -EINVAL;
3038 /* For UDP-style sockets, id specifies the association to query. */
3039 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3040 if (!asoc)
3041 return -EINVAL;
3043 to = (void __user *)getaddrs.addrs;
3044 list_for_each(pos, &asoc->peer.transport_addr_list) {
3045 from = list_entry(pos, struct sctp_transport, transports);
3046 memcpy(&temp, &from->ipaddr, sizeof(temp));
3047 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3048 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3049 temp.v4.sin_port = htons(temp.v4.sin_port);
3050 if (copy_to_user(to, &temp, addrlen))
3051 return -EFAULT;
3052 to += addrlen ;
3053 cnt ++;
3054 if (cnt >= getaddrs.addr_num) break;
3056 getaddrs.addr_num = cnt;
3057 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3058 return -EFAULT;
3060 return 0;
3063 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
3064 char __user *optval,
3065 int __user *optlen)
3067 sctp_assoc_t id;
3068 struct sctp_bind_addr *bp;
3069 struct sctp_association *asoc;
3070 struct list_head *pos;
3071 struct sctp_sockaddr_entry *addr;
3072 rwlock_t *addr_lock;
3073 unsigned long flags;
3074 int cnt = 0;
3076 if (len != sizeof(sctp_assoc_t))
3077 return -EINVAL;
3079 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3080 return -EFAULT;
3083 * For UDP-style sockets, id specifies the association to query.
3084 * If the id field is set to the value '0' then the locally bound
3085 * addresses are returned without regard to any particular
3086 * association.
3088 if (0 == id) {
3089 bp = &sctp_sk(sk)->ep->base.bind_addr;
3090 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3091 } else {
3092 asoc = sctp_id2assoc(sk, id);
3093 if (!asoc)
3094 return -EINVAL;
3095 bp = &asoc->base.bind_addr;
3096 addr_lock = &asoc->base.addr_lock;
3099 sctp_read_lock(addr_lock);
3101 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3102 * addresses from the global local address list.
3104 if (sctp_list_single_entry(&bp->address_list)) {
3105 addr = list_entry(bp->address_list.next,
3106 struct sctp_sockaddr_entry, list);
3107 if (sctp_is_any(&addr->a)) {
3108 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3109 list_for_each(pos, &sctp_local_addr_list) {
3110 addr = list_entry(pos,
3111 struct sctp_sockaddr_entry,
3112 list);
3113 if ((PF_INET == sk->sk_family) &&
3114 (AF_INET6 == addr->a.sa.sa_family))
3115 continue;
3116 cnt++;
3118 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3119 flags);
3120 } else {
3121 cnt = 1;
3123 goto done;
3126 list_for_each(pos, &bp->address_list) {
3127 cnt ++;
3130 done:
3131 sctp_read_unlock(addr_lock);
3132 return cnt;
3135 /* Helper function that copies local addresses to user and returns the number
3136 * of addresses copied.
3138 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, int max_addrs,
3139 void __user *to)
3141 struct list_head *pos;
3142 struct sctp_sockaddr_entry *addr;
3143 unsigned long flags;
3144 union sctp_addr temp;
3145 int cnt = 0;
3146 int addrlen;
3148 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3149 list_for_each(pos, &sctp_local_addr_list) {
3150 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3151 if ((PF_INET == sk->sk_family) &&
3152 (AF_INET6 == addr->a.sa.sa_family))
3153 continue;
3154 memcpy(&temp, &addr->a, sizeof(temp));
3155 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3156 &temp);
3157 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3158 temp.v4.sin_port = htons(port);
3159 if (copy_to_user(to, &temp, addrlen)) {
3160 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3161 flags);
3162 return -EFAULT;
3164 to += addrlen;
3165 cnt ++;
3166 if (cnt >= max_addrs) break;
3168 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3170 return cnt;
3173 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3174 char __user *optval, int __user *optlen)
3176 struct sctp_bind_addr *bp;
3177 struct sctp_association *asoc;
3178 struct list_head *pos;
3179 int cnt = 0;
3180 struct sctp_getaddrs getaddrs;
3181 struct sctp_sockaddr_entry *addr;
3182 void __user *to;
3183 union sctp_addr temp;
3184 struct sctp_sock *sp = sctp_sk(sk);
3185 int addrlen;
3186 rwlock_t *addr_lock;
3187 int err = 0;
3189 if (len != sizeof(struct sctp_getaddrs))
3190 return -EINVAL;
3192 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3193 return -EFAULT;
3195 if (getaddrs.addr_num <= 0) return -EINVAL;
3197 * For UDP-style sockets, id specifies the association to query.
3198 * If the id field is set to the value '0' then the locally bound
3199 * addresses are returned without regard to any particular
3200 * association.
3202 if (0 == getaddrs.assoc_id) {
3203 bp = &sctp_sk(sk)->ep->base.bind_addr;
3204 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3205 } else {
3206 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3207 if (!asoc)
3208 return -EINVAL;
3209 bp = &asoc->base.bind_addr;
3210 addr_lock = &asoc->base.addr_lock;
3213 to = getaddrs.addrs;
3215 sctp_read_lock(addr_lock);
3217 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3218 * addresses from the global local address list.
3220 if (sctp_list_single_entry(&bp->address_list)) {
3221 addr = list_entry(bp->address_list.next,
3222 struct sctp_sockaddr_entry, list);
3223 if (sctp_is_any(&addr->a)) {
3224 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
3225 getaddrs.addr_num, to);
3226 if (cnt < 0) {
3227 err = cnt;
3228 goto unlock;
3230 goto copy_getaddrs;
3234 list_for_each(pos, &bp->address_list) {
3235 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3236 memcpy(&temp, &addr->a, sizeof(temp));
3237 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3238 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3239 temp.v4.sin_port = htons(temp.v4.sin_port);
3240 if (copy_to_user(to, &temp, addrlen)) {
3241 err = -EFAULT;
3242 goto unlock;
3244 to += addrlen;
3245 cnt ++;
3246 if (cnt >= getaddrs.addr_num) break;
3249 copy_getaddrs:
3250 getaddrs.addr_num = cnt;
3251 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3252 err = -EFAULT;
3254 unlock:
3255 sctp_read_unlock(addr_lock);
3256 return err;
3259 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3261 * Requests that the local SCTP stack use the enclosed peer address as
3262 * the association primary. The enclosed address must be one of the
3263 * association peer's addresses.
3265 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3266 char __user *optval, int __user *optlen)
3268 struct sctp_prim prim;
3269 struct sctp_association *asoc;
3270 struct sctp_sock *sp = sctp_sk(sk);
3272 if (len != sizeof(struct sctp_prim))
3273 return -EINVAL;
3275 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3276 return -EFAULT;
3278 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3279 if (!asoc)
3280 return -EINVAL;
3282 if (!asoc->peer.primary_path)
3283 return -ENOTCONN;
3285 asoc->peer.primary_path->ipaddr.v4.sin_port =
3286 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3287 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3288 sizeof(union sctp_addr));
3289 asoc->peer.primary_path->ipaddr.v4.sin_port =
3290 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3292 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3293 (union sctp_addr *)&prim.ssp_addr);
3295 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3296 return -EFAULT;
3298 return 0;
3302 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
3304 * Requests that the local endpoint set the specified Adaption Layer
3305 * Indication parameter for all future INIT and INIT-ACK exchanges.
3307 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
3308 char __user *optval, int __user *optlen)
3310 __u32 val;
3312 if (len < sizeof(__u32))
3313 return -EINVAL;
3315 len = sizeof(__u32);
3316 val = sctp_sk(sk)->adaption_ind;
3317 if (put_user(len, optlen))
3318 return -EFAULT;
3319 if (copy_to_user(optval, &val, len))
3320 return -EFAULT;
3321 return 0;
3326 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3328 * Applications that wish to use the sendto() system call may wish to
3329 * specify a default set of parameters that would normally be supplied
3330 * through the inclusion of ancillary data. This socket option allows
3331 * such an application to set the default sctp_sndrcvinfo structure.
3334 * The application that wishes to use this socket option simply passes
3335 * in to this call the sctp_sndrcvinfo structure defined in Section
3336 * 5.2.2) The input parameters accepted by this call include
3337 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3338 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3339 * to this call if the caller is using the UDP model.
3341 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3343 static int sctp_getsockopt_default_send_param(struct sock *sk,
3344 int len, char __user *optval,
3345 int __user *optlen)
3347 struct sctp_sndrcvinfo info;
3348 struct sctp_association *asoc;
3349 struct sctp_sock *sp = sctp_sk(sk);
3351 if (len != sizeof(struct sctp_sndrcvinfo))
3352 return -EINVAL;
3353 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3354 return -EFAULT;
3356 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3357 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3358 return -EINVAL;
3360 if (asoc) {
3361 info.sinfo_stream = asoc->default_stream;
3362 info.sinfo_flags = asoc->default_flags;
3363 info.sinfo_ppid = asoc->default_ppid;
3364 info.sinfo_context = asoc->default_context;
3365 info.sinfo_timetolive = asoc->default_timetolive;
3366 } else {
3367 info.sinfo_stream = sp->default_stream;
3368 info.sinfo_flags = sp->default_flags;
3369 info.sinfo_ppid = sp->default_ppid;
3370 info.sinfo_context = sp->default_context;
3371 info.sinfo_timetolive = sp->default_timetolive;
3374 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3375 return -EFAULT;
3377 return 0;
3382 * 7.1.5 SCTP_NODELAY
3384 * Turn on/off any Nagle-like algorithm. This means that packets are
3385 * generally sent as soon as possible and no unnecessary delays are
3386 * introduced, at the cost of more packets in the network. Expects an
3387 * integer boolean flag.
3390 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3391 char __user *optval, int __user *optlen)
3393 int val;
3395 if (len < sizeof(int))
3396 return -EINVAL;
3398 len = sizeof(int);
3399 val = (sctp_sk(sk)->nodelay == 1);
3400 if (put_user(len, optlen))
3401 return -EFAULT;
3402 if (copy_to_user(optval, &val, len))
3403 return -EFAULT;
3404 return 0;
3409 * 7.1.1 SCTP_RTOINFO
3411 * The protocol parameters used to initialize and bound retransmission
3412 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3413 * and modify these parameters.
3414 * All parameters are time values, in milliseconds. A value of 0, when
3415 * modifying the parameters, indicates that the current value should not
3416 * be changed.
3419 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3420 char __user *optval,
3421 int __user *optlen) {
3422 struct sctp_rtoinfo rtoinfo;
3423 struct sctp_association *asoc;
3425 if (len != sizeof (struct sctp_rtoinfo))
3426 return -EINVAL;
3428 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3429 return -EFAULT;
3431 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3433 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3434 return -EINVAL;
3436 /* Values corresponding to the specific association. */
3437 if (asoc) {
3438 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3439 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3440 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3441 } else {
3442 /* Values corresponding to the endpoint. */
3443 struct sctp_sock *sp = sctp_sk(sk);
3445 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3446 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3447 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3450 if (put_user(len, optlen))
3451 return -EFAULT;
3453 if (copy_to_user(optval, &rtoinfo, len))
3454 return -EFAULT;
3456 return 0;
3461 * 7.1.2 SCTP_ASSOCINFO
3463 * This option is used to tune the the maximum retransmission attempts
3464 * of the association.
3465 * Returns an error if the new association retransmission value is
3466 * greater than the sum of the retransmission value of the peer.
3467 * See [SCTP] for more information.
3470 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3471 char __user *optval,
3472 int __user *optlen)
3475 struct sctp_assocparams assocparams;
3476 struct sctp_association *asoc;
3477 struct list_head *pos;
3478 int cnt = 0;
3480 if (len != sizeof (struct sctp_assocparams))
3481 return -EINVAL;
3483 if (copy_from_user(&assocparams, optval,
3484 sizeof (struct sctp_assocparams)))
3485 return -EFAULT;
3487 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3489 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3490 return -EINVAL;
3492 /* Values correspoinding to the specific association */
3493 if (asoc) {
3494 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3495 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3496 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3497 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3498 * 1000) +
3499 (asoc->cookie_life.tv_usec
3500 / 1000);
3502 list_for_each(pos, &asoc->peer.transport_addr_list) {
3503 cnt ++;
3506 assocparams.sasoc_number_peer_destinations = cnt;
3507 } else {
3508 /* Values corresponding to the endpoint */
3509 struct sctp_sock *sp = sctp_sk(sk);
3511 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3512 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3513 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3514 assocparams.sasoc_cookie_life =
3515 sp->assocparams.sasoc_cookie_life;
3516 assocparams.sasoc_number_peer_destinations =
3517 sp->assocparams.
3518 sasoc_number_peer_destinations;
3521 if (put_user(len, optlen))
3522 return -EFAULT;
3524 if (copy_to_user(optval, &assocparams, len))
3525 return -EFAULT;
3527 return 0;
3531 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3533 * This socket option is a boolean flag which turns on or off mapped V4
3534 * addresses. If this option is turned on and the socket is type
3535 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3536 * If this option is turned off, then no mapping will be done of V4
3537 * addresses and a user will receive both PF_INET6 and PF_INET type
3538 * addresses on the socket.
3540 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3541 char __user *optval, int __user *optlen)
3543 int val;
3544 struct sctp_sock *sp = sctp_sk(sk);
3546 if (len < sizeof(int))
3547 return -EINVAL;
3549 len = sizeof(int);
3550 val = sp->v4mapped;
3551 if (put_user(len, optlen))
3552 return -EFAULT;
3553 if (copy_to_user(optval, &val, len))
3554 return -EFAULT;
3556 return 0;
3560 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3562 * This socket option specifies the maximum size to put in any outgoing
3563 * SCTP chunk. If a message is larger than this size it will be
3564 * fragmented by SCTP into the specified size. Note that the underlying
3565 * SCTP implementation may fragment into smaller sized chunks when the
3566 * PMTU of the underlying association is smaller than the value set by
3567 * the user.
3569 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3570 char __user *optval, int __user *optlen)
3572 int val;
3574 if (len < sizeof(int))
3575 return -EINVAL;
3577 len = sizeof(int);
3579 val = sctp_sk(sk)->user_frag;
3580 if (put_user(len, optlen))
3581 return -EFAULT;
3582 if (copy_to_user(optval, &val, len))
3583 return -EFAULT;
3585 return 0;
3588 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3589 char __user *optval, int __user *optlen)
3591 int retval = 0;
3592 int len;
3594 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3596 /* I can hardly begin to describe how wrong this is. This is
3597 * so broken as to be worse than useless. The API draft
3598 * REALLY is NOT helpful here... I am not convinced that the
3599 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3600 * are at all well-founded.
3602 if (level != SOL_SCTP) {
3603 struct sctp_af *af = sctp_sk(sk)->pf->af;
3605 retval = af->getsockopt(sk, level, optname, optval, optlen);
3606 return retval;
3609 if (get_user(len, optlen))
3610 return -EFAULT;
3612 sctp_lock_sock(sk);
3614 switch (optname) {
3615 case SCTP_STATUS:
3616 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3617 break;
3618 case SCTP_DISABLE_FRAGMENTS:
3619 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3620 optlen);
3621 break;
3622 case SCTP_EVENTS:
3623 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3624 break;
3625 case SCTP_AUTOCLOSE:
3626 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3627 break;
3628 case SCTP_SOCKOPT_PEELOFF:
3629 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3630 break;
3631 case SCTP_PEER_ADDR_PARAMS:
3632 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3633 optlen);
3634 break;
3635 case SCTP_INITMSG:
3636 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3637 break;
3638 case SCTP_GET_PEER_ADDRS_NUM:
3639 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3640 optlen);
3641 break;
3642 case SCTP_GET_LOCAL_ADDRS_NUM:
3643 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3644 optlen);
3645 break;
3646 case SCTP_GET_PEER_ADDRS:
3647 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3648 optlen);
3649 break;
3650 case SCTP_GET_LOCAL_ADDRS:
3651 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3652 optlen);
3653 break;
3654 case SCTP_DEFAULT_SEND_PARAM:
3655 retval = sctp_getsockopt_default_send_param(sk, len,
3656 optval, optlen);
3657 break;
3658 case SCTP_PRIMARY_ADDR:
3659 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3660 break;
3661 case SCTP_NODELAY:
3662 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3663 break;
3664 case SCTP_RTOINFO:
3665 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3666 break;
3667 case SCTP_ASSOCINFO:
3668 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3669 break;
3670 case SCTP_I_WANT_MAPPED_V4_ADDR:
3671 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3672 break;
3673 case SCTP_MAXSEG:
3674 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3675 break;
3676 case SCTP_GET_PEER_ADDR_INFO:
3677 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3678 optlen);
3679 break;
3680 case SCTP_ADAPTION_LAYER:
3681 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
3682 optlen);
3683 break;
3684 default:
3685 retval = -ENOPROTOOPT;
3686 break;
3689 sctp_release_sock(sk);
3690 return retval;
3693 static void sctp_hash(struct sock *sk)
3695 /* STUB */
3698 static void sctp_unhash(struct sock *sk)
3700 /* STUB */
3703 /* Check if port is acceptable. Possibly find first available port.
3705 * The port hash table (contained in the 'global' SCTP protocol storage
3706 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3707 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3708 * list (the list number is the port number hashed out, so as you
3709 * would expect from a hash function, all the ports in a given list have
3710 * such a number that hashes out to the same list number; you were
3711 * expecting that, right?); so each list has a set of ports, with a
3712 * link to the socket (struct sock) that uses it, the port number and
3713 * a fastreuse flag (FIXME: NPI ipg).
3715 static struct sctp_bind_bucket *sctp_bucket_create(
3716 struct sctp_bind_hashbucket *head, unsigned short snum);
3718 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3720 struct sctp_bind_hashbucket *head; /* hash list */
3721 struct sctp_bind_bucket *pp; /* hash list port iterator */
3722 unsigned short snum;
3723 int ret;
3725 /* NOTE: Remember to put this back to net order. */
3726 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3727 snum = addr->v4.sin_port;
3729 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3730 sctp_local_bh_disable();
3732 if (snum == 0) {
3733 /* Search for an available port.
3735 * 'sctp_port_rover' was the last port assigned, so
3736 * we start to search from 'sctp_port_rover +
3737 * 1'. What we do is first check if port 'rover' is
3738 * already in the hash table; if not, we use that; if
3739 * it is, we try next.
3741 int low = sysctl_local_port_range[0];
3742 int high = sysctl_local_port_range[1];
3743 int remaining = (high - low) + 1;
3744 int rover;
3745 int index;
3747 sctp_spin_lock(&sctp_port_alloc_lock);
3748 rover = sctp_port_rover;
3749 do {
3750 rover++;
3751 if ((rover < low) || (rover > high))
3752 rover = low;
3753 index = sctp_phashfn(rover);
3754 head = &sctp_port_hashtable[index];
3755 sctp_spin_lock(&head->lock);
3756 for (pp = head->chain; pp; pp = pp->next)
3757 if (pp->port == rover)
3758 goto next;
3759 break;
3760 next:
3761 sctp_spin_unlock(&head->lock);
3762 } while (--remaining > 0);
3763 sctp_port_rover = rover;
3764 sctp_spin_unlock(&sctp_port_alloc_lock);
3766 /* Exhausted local port range during search? */
3767 ret = 1;
3768 if (remaining <= 0)
3769 goto fail;
3771 /* OK, here is the one we will use. HEAD (the port
3772 * hash table list entry) is non-NULL and we hold it's
3773 * mutex.
3775 snum = rover;
3776 } else {
3777 /* We are given an specific port number; we verify
3778 * that it is not being used. If it is used, we will
3779 * exahust the search in the hash list corresponding
3780 * to the port number (snum) - we detect that with the
3781 * port iterator, pp being NULL.
3783 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3784 sctp_spin_lock(&head->lock);
3785 for (pp = head->chain; pp; pp = pp->next) {
3786 if (pp->port == snum)
3787 goto pp_found;
3790 pp = NULL;
3791 goto pp_not_found;
3792 pp_found:
3793 if (!hlist_empty(&pp->owner)) {
3794 /* We had a port hash table hit - there is an
3795 * available port (pp != NULL) and it is being
3796 * used by other socket (pp->owner not empty); that other
3797 * socket is going to be sk2.
3799 int reuse = sk->sk_reuse;
3800 struct sock *sk2;
3801 struct hlist_node *node;
3803 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3804 if (pp->fastreuse && sk->sk_reuse)
3805 goto success;
3807 /* Run through the list of sockets bound to the port
3808 * (pp->port) [via the pointers bind_next and
3809 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3810 * we get the endpoint they describe and run through
3811 * the endpoint's list of IP (v4 or v6) addresses,
3812 * comparing each of the addresses with the address of
3813 * the socket sk. If we find a match, then that means
3814 * that this port/socket (sk) combination are already
3815 * in an endpoint.
3817 sk_for_each_bound(sk2, node, &pp->owner) {
3818 struct sctp_endpoint *ep2;
3819 ep2 = sctp_sk(sk2)->ep;
3821 if (reuse && sk2->sk_reuse)
3822 continue;
3824 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3825 sctp_sk(sk))) {
3826 ret = (long)sk2;
3827 goto fail_unlock;
3830 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3832 pp_not_found:
3833 /* If there was a hash table miss, create a new port. */
3834 ret = 1;
3835 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3836 goto fail_unlock;
3838 /* In either case (hit or miss), make sure fastreuse is 1 only
3839 * if sk->sk_reuse is too (that is, if the caller requested
3840 * SO_REUSEADDR on this socket -sk-).
3842 if (hlist_empty(&pp->owner))
3843 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3844 else if (pp->fastreuse && !sk->sk_reuse)
3845 pp->fastreuse = 0;
3847 /* We are set, so fill up all the data in the hash table
3848 * entry, tie the socket list information with the rest of the
3849 * sockets FIXME: Blurry, NPI (ipg).
3851 success:
3852 inet_sk(sk)->num = snum;
3853 if (!sctp_sk(sk)->bind_hash) {
3854 sk_add_bind_node(sk, &pp->owner);
3855 sctp_sk(sk)->bind_hash = pp;
3857 ret = 0;
3859 fail_unlock:
3860 sctp_spin_unlock(&head->lock);
3862 fail:
3863 sctp_local_bh_enable();
3864 addr->v4.sin_port = htons(addr->v4.sin_port);
3865 return ret;
3868 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3869 * port is requested.
3871 static int sctp_get_port(struct sock *sk, unsigned short snum)
3873 long ret;
3874 union sctp_addr addr;
3875 struct sctp_af *af = sctp_sk(sk)->pf->af;
3877 /* Set up a dummy address struct from the sk. */
3878 af->from_sk(&addr, sk);
3879 addr.v4.sin_port = htons(snum);
3881 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3882 ret = sctp_get_port_local(sk, &addr);
3884 return (ret ? 1 : 0);
3888 * 3.1.3 listen() - UDP Style Syntax
3890 * By default, new associations are not accepted for UDP style sockets.
3891 * An application uses listen() to mark a socket as being able to
3892 * accept new associations.
3894 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3896 struct sctp_sock *sp = sctp_sk(sk);
3897 struct sctp_endpoint *ep = sp->ep;
3899 /* Only UDP style sockets that are not peeled off are allowed to
3900 * listen().
3902 if (!sctp_style(sk, UDP))
3903 return -EINVAL;
3905 /* If backlog is zero, disable listening. */
3906 if (!backlog) {
3907 if (sctp_sstate(sk, CLOSED))
3908 return 0;
3910 sctp_unhash_endpoint(ep);
3911 sk->sk_state = SCTP_SS_CLOSED;
3914 /* Return if we are already listening. */
3915 if (sctp_sstate(sk, LISTENING))
3916 return 0;
3919 * If a bind() or sctp_bindx() is not called prior to a listen()
3920 * call that allows new associations to be accepted, the system
3921 * picks an ephemeral port and will choose an address set equivalent
3922 * to binding with a wildcard address.
3924 * This is not currently spelled out in the SCTP sockets
3925 * extensions draft, but follows the practice as seen in TCP
3926 * sockets.
3928 if (!ep->base.bind_addr.port) {
3929 if (sctp_autobind(sk))
3930 return -EAGAIN;
3932 sk->sk_state = SCTP_SS_LISTENING;
3933 sctp_hash_endpoint(ep);
3934 return 0;
3938 * 4.1.3 listen() - TCP Style Syntax
3940 * Applications uses listen() to ready the SCTP endpoint for accepting
3941 * inbound associations.
3943 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3945 struct sctp_sock *sp = sctp_sk(sk);
3946 struct sctp_endpoint *ep = sp->ep;
3948 /* If backlog is zero, disable listening. */
3949 if (!backlog) {
3950 if (sctp_sstate(sk, CLOSED))
3951 return 0;
3953 sctp_unhash_endpoint(ep);
3954 sk->sk_state = SCTP_SS_CLOSED;
3957 if (sctp_sstate(sk, LISTENING))
3958 return 0;
3961 * If a bind() or sctp_bindx() is not called prior to a listen()
3962 * call that allows new associations to be accepted, the system
3963 * picks an ephemeral port and will choose an address set equivalent
3964 * to binding with a wildcard address.
3966 * This is not currently spelled out in the SCTP sockets
3967 * extensions draft, but follows the practice as seen in TCP
3968 * sockets.
3970 if (!ep->base.bind_addr.port) {
3971 if (sctp_autobind(sk))
3972 return -EAGAIN;
3974 sk->sk_state = SCTP_SS_LISTENING;
3975 sk->sk_max_ack_backlog = backlog;
3976 sctp_hash_endpoint(ep);
3977 return 0;
3981 * Move a socket to LISTENING state.
3983 int sctp_inet_listen(struct socket *sock, int backlog)
3985 struct sock *sk = sock->sk;
3986 struct crypto_tfm *tfm=NULL;
3987 int err = -EINVAL;
3989 if (unlikely(backlog < 0))
3990 goto out;
3992 sctp_lock_sock(sk);
3994 if (sock->state != SS_UNCONNECTED)
3995 goto out;
3997 /* Allocate HMAC for generating cookie. */
3998 if (sctp_hmac_alg) {
3999 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4000 if (!tfm) {
4001 err = -ENOSYS;
4002 goto out;
4006 switch (sock->type) {
4007 case SOCK_SEQPACKET:
4008 err = sctp_seqpacket_listen(sk, backlog);
4009 break;
4010 case SOCK_STREAM:
4011 err = sctp_stream_listen(sk, backlog);
4012 break;
4013 default:
4014 break;
4016 if (err)
4017 goto cleanup;
4019 /* Store away the transform reference. */
4020 sctp_sk(sk)->hmac = tfm;
4021 out:
4022 sctp_release_sock(sk);
4023 return err;
4024 cleanup:
4025 if (tfm)
4026 sctp_crypto_free_tfm(tfm);
4027 goto out;
4031 * This function is done by modeling the current datagram_poll() and the
4032 * tcp_poll(). Note that, based on these implementations, we don't
4033 * lock the socket in this function, even though it seems that,
4034 * ideally, locking or some other mechanisms can be used to ensure
4035 * the integrity of the counters (sndbuf and wmem_queued) used
4036 * in this place. We assume that we don't need locks either until proven
4037 * otherwise.
4039 * Another thing to note is that we include the Async I/O support
4040 * here, again, by modeling the current TCP/UDP code. We don't have
4041 * a good way to test with it yet.
4043 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4045 struct sock *sk = sock->sk;
4046 struct sctp_sock *sp = sctp_sk(sk);
4047 unsigned int mask;
4049 poll_wait(file, sk->sk_sleep, wait);
4051 /* A TCP-style listening socket becomes readable when the accept queue
4052 * is not empty.
4054 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4055 return (!list_empty(&sp->ep->asocs)) ?
4056 (POLLIN | POLLRDNORM) : 0;
4058 mask = 0;
4060 /* Is there any exceptional events? */
4061 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4062 mask |= POLLERR;
4063 if (sk->sk_shutdown == SHUTDOWN_MASK)
4064 mask |= POLLHUP;
4066 /* Is it readable? Reconsider this code with TCP-style support. */
4067 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4068 (sk->sk_shutdown & RCV_SHUTDOWN))
4069 mask |= POLLIN | POLLRDNORM;
4071 /* The association is either gone or not ready. */
4072 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4073 return mask;
4075 /* Is it writable? */
4076 if (sctp_writeable(sk)) {
4077 mask |= POLLOUT | POLLWRNORM;
4078 } else {
4079 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4081 * Since the socket is not locked, the buffer
4082 * might be made available after the writeable check and
4083 * before the bit is set. This could cause a lost I/O
4084 * signal. tcp_poll() has a race breaker for this race
4085 * condition. Based on their implementation, we put
4086 * in the following code to cover it as well.
4088 if (sctp_writeable(sk))
4089 mask |= POLLOUT | POLLWRNORM;
4091 return mask;
4094 /********************************************************************
4095 * 2nd Level Abstractions
4096 ********************************************************************/
4098 static struct sctp_bind_bucket *sctp_bucket_create(
4099 struct sctp_bind_hashbucket *head, unsigned short snum)
4101 struct sctp_bind_bucket *pp;
4103 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4104 SCTP_DBG_OBJCNT_INC(bind_bucket);
4105 if (pp) {
4106 pp->port = snum;
4107 pp->fastreuse = 0;
4108 INIT_HLIST_HEAD(&pp->owner);
4109 if ((pp->next = head->chain) != NULL)
4110 pp->next->pprev = &pp->next;
4111 head->chain = pp;
4112 pp->pprev = &head->chain;
4114 return pp;
4117 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4118 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4120 if (hlist_empty(&pp->owner)) {
4121 if (pp->next)
4122 pp->next->pprev = pp->pprev;
4123 *(pp->pprev) = pp->next;
4124 kmem_cache_free(sctp_bucket_cachep, pp);
4125 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4129 /* Release this socket's reference to a local port. */
4130 static inline void __sctp_put_port(struct sock *sk)
4132 struct sctp_bind_hashbucket *head =
4133 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4134 struct sctp_bind_bucket *pp;
4136 sctp_spin_lock(&head->lock);
4137 pp = sctp_sk(sk)->bind_hash;
4138 __sk_del_bind_node(sk);
4139 sctp_sk(sk)->bind_hash = NULL;
4140 inet_sk(sk)->num = 0;
4141 sctp_bucket_destroy(pp);
4142 sctp_spin_unlock(&head->lock);
4145 void sctp_put_port(struct sock *sk)
4147 sctp_local_bh_disable();
4148 __sctp_put_port(sk);
4149 sctp_local_bh_enable();
4153 * The system picks an ephemeral port and choose an address set equivalent
4154 * to binding with a wildcard address.
4155 * One of those addresses will be the primary address for the association.
4156 * This automatically enables the multihoming capability of SCTP.
4158 static int sctp_autobind(struct sock *sk)
4160 union sctp_addr autoaddr;
4161 struct sctp_af *af;
4162 unsigned short port;
4164 /* Initialize a local sockaddr structure to INADDR_ANY. */
4165 af = sctp_sk(sk)->pf->af;
4167 port = htons(inet_sk(sk)->num);
4168 af->inaddr_any(&autoaddr, port);
4170 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4173 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4175 * From RFC 2292
4176 * 4.2 The cmsghdr Structure *
4178 * When ancillary data is sent or received, any number of ancillary data
4179 * objects can be specified by the msg_control and msg_controllen members of
4180 * the msghdr structure, because each object is preceded by
4181 * a cmsghdr structure defining the object's length (the cmsg_len member).
4182 * Historically Berkeley-derived implementations have passed only one object
4183 * at a time, but this API allows multiple objects to be
4184 * passed in a single call to sendmsg() or recvmsg(). The following example
4185 * shows two ancillary data objects in a control buffer.
4187 * |<--------------------------- msg_controllen -------------------------->|
4188 * | |
4190 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4192 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4193 * | | |
4195 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4197 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4198 * | | | | |
4200 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4201 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4203 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4205 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4209 * msg_control
4210 * points here
4212 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4213 sctp_cmsgs_t *cmsgs)
4215 struct cmsghdr *cmsg;
4217 for (cmsg = CMSG_FIRSTHDR(msg);
4218 cmsg != NULL;
4219 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4220 if (!CMSG_OK(msg, cmsg))
4221 return -EINVAL;
4223 /* Should we parse this header or ignore? */
4224 if (cmsg->cmsg_level != IPPROTO_SCTP)
4225 continue;
4227 /* Strictly check lengths following example in SCM code. */
4228 switch (cmsg->cmsg_type) {
4229 case SCTP_INIT:
4230 /* SCTP Socket API Extension
4231 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4233 * This cmsghdr structure provides information for
4234 * initializing new SCTP associations with sendmsg().
4235 * The SCTP_INITMSG socket option uses this same data
4236 * structure. This structure is not used for
4237 * recvmsg().
4239 * cmsg_level cmsg_type cmsg_data[]
4240 * ------------ ------------ ----------------------
4241 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4243 if (cmsg->cmsg_len !=
4244 CMSG_LEN(sizeof(struct sctp_initmsg)))
4245 return -EINVAL;
4246 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4247 break;
4249 case SCTP_SNDRCV:
4250 /* SCTP Socket API Extension
4251 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4253 * This cmsghdr structure specifies SCTP options for
4254 * sendmsg() and describes SCTP header information
4255 * about a received message through recvmsg().
4257 * cmsg_level cmsg_type cmsg_data[]
4258 * ------------ ------------ ----------------------
4259 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4261 if (cmsg->cmsg_len !=
4262 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4263 return -EINVAL;
4265 cmsgs->info =
4266 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4268 /* Minimally, validate the sinfo_flags. */
4269 if (cmsgs->info->sinfo_flags &
4270 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4271 MSG_ABORT | MSG_EOF))
4272 return -EINVAL;
4273 break;
4275 default:
4276 return -EINVAL;
4279 return 0;
4283 * Wait for a packet..
4284 * Note: This function is the same function as in core/datagram.c
4285 * with a few modifications to make lksctp work.
4287 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4289 int error;
4290 DEFINE_WAIT(wait);
4292 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4294 /* Socket errors? */
4295 error = sock_error(sk);
4296 if (error)
4297 goto out;
4299 if (!skb_queue_empty(&sk->sk_receive_queue))
4300 goto ready;
4302 /* Socket shut down? */
4303 if (sk->sk_shutdown & RCV_SHUTDOWN)
4304 goto out;
4306 /* Sequenced packets can come disconnected. If so we report the
4307 * problem.
4309 error = -ENOTCONN;
4311 /* Is there a good reason to think that we may receive some data? */
4312 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4313 goto out;
4315 /* Handle signals. */
4316 if (signal_pending(current))
4317 goto interrupted;
4319 /* Let another process have a go. Since we are going to sleep
4320 * anyway. Note: This may cause odd behaviors if the message
4321 * does not fit in the user's buffer, but this seems to be the
4322 * only way to honor MSG_DONTWAIT realistically.
4324 sctp_release_sock(sk);
4325 *timeo_p = schedule_timeout(*timeo_p);
4326 sctp_lock_sock(sk);
4328 ready:
4329 finish_wait(sk->sk_sleep, &wait);
4330 return 0;
4332 interrupted:
4333 error = sock_intr_errno(*timeo_p);
4335 out:
4336 finish_wait(sk->sk_sleep, &wait);
4337 *err = error;
4338 return error;
4341 /* Receive a datagram.
4342 * Note: This is pretty much the same routine as in core/datagram.c
4343 * with a few changes to make lksctp work.
4345 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4346 int noblock, int *err)
4348 int error;
4349 struct sk_buff *skb;
4350 long timeo;
4352 /* Caller is allowed not to check sk->sk_err before calling. */
4353 error = sock_error(sk);
4354 if (error)
4355 goto no_packet;
4357 timeo = sock_rcvtimeo(sk, noblock);
4359 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4360 timeo, MAX_SCHEDULE_TIMEOUT);
4362 do {
4363 /* Again only user level code calls this function,
4364 * so nothing interrupt level
4365 * will suddenly eat the receive_queue.
4367 * Look at current nfs client by the way...
4368 * However, this function was corrent in any case. 8)
4370 if (flags & MSG_PEEK) {
4371 unsigned long cpu_flags;
4373 sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4374 cpu_flags);
4375 skb = skb_peek(&sk->sk_receive_queue);
4376 if (skb)
4377 atomic_inc(&skb->users);
4378 sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4379 cpu_flags);
4380 } else {
4381 skb = skb_dequeue(&sk->sk_receive_queue);
4384 if (skb)
4385 return skb;
4387 if (sk->sk_shutdown & RCV_SHUTDOWN)
4388 break;
4390 /* User doesn't want to wait. */
4391 error = -EAGAIN;
4392 if (!timeo)
4393 goto no_packet;
4394 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4396 return NULL;
4398 no_packet:
4399 *err = error;
4400 return NULL;
4403 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4404 static void __sctp_write_space(struct sctp_association *asoc)
4406 struct sock *sk = asoc->base.sk;
4407 struct socket *sock = sk->sk_socket;
4409 if ((sctp_wspace(asoc) > 0) && sock) {
4410 if (waitqueue_active(&asoc->wait))
4411 wake_up_interruptible(&asoc->wait);
4413 if (sctp_writeable(sk)) {
4414 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4415 wake_up_interruptible(sk->sk_sleep);
4417 /* Note that we try to include the Async I/O support
4418 * here by modeling from the current TCP/UDP code.
4419 * We have not tested with it yet.
4421 if (sock->fasync_list &&
4422 !(sk->sk_shutdown & SEND_SHUTDOWN))
4423 sock_wake_async(sock, 2, POLL_OUT);
4428 /* Do accounting for the sndbuf space.
4429 * Decrement the used sndbuf space of the corresponding association by the
4430 * data size which was just transmitted(freed).
4432 static void sctp_wfree(struct sk_buff *skb)
4434 struct sctp_association *asoc;
4435 struct sctp_chunk *chunk;
4436 struct sock *sk;
4438 /* Get the saved chunk pointer. */
4439 chunk = *((struct sctp_chunk **)(skb->cb));
4440 asoc = chunk->asoc;
4441 sk = asoc->base.sk;
4442 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
4443 sizeof(struct sk_buff) +
4444 sizeof(struct sctp_chunk);
4446 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
4447 sizeof(struct sk_buff) +
4448 sizeof(struct sctp_chunk);
4450 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
4452 sock_wfree(skb);
4453 __sctp_write_space(asoc);
4455 sctp_association_put(asoc);
4458 /* Helper function to wait for space in the sndbuf. */
4459 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4460 size_t msg_len)
4462 struct sock *sk = asoc->base.sk;
4463 int err = 0;
4464 long current_timeo = *timeo_p;
4465 DEFINE_WAIT(wait);
4467 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4468 asoc, (long)(*timeo_p), msg_len);
4470 /* Increment the association's refcnt. */
4471 sctp_association_hold(asoc);
4473 /* Wait on the association specific sndbuf space. */
4474 for (;;) {
4475 prepare_to_wait_exclusive(&asoc->wait, &wait,
4476 TASK_INTERRUPTIBLE);
4477 if (!*timeo_p)
4478 goto do_nonblock;
4479 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4480 asoc->base.dead)
4481 goto do_error;
4482 if (signal_pending(current))
4483 goto do_interrupted;
4484 if (msg_len <= sctp_wspace(asoc))
4485 break;
4487 /* Let another process have a go. Since we are going
4488 * to sleep anyway.
4490 sctp_release_sock(sk);
4491 current_timeo = schedule_timeout(current_timeo);
4492 sctp_lock_sock(sk);
4494 *timeo_p = current_timeo;
4497 out:
4498 finish_wait(&asoc->wait, &wait);
4500 /* Release the association's refcnt. */
4501 sctp_association_put(asoc);
4503 return err;
4505 do_error:
4506 err = -EPIPE;
4507 goto out;
4509 do_interrupted:
4510 err = sock_intr_errno(*timeo_p);
4511 goto out;
4513 do_nonblock:
4514 err = -EAGAIN;
4515 goto out;
4518 /* If socket sndbuf has changed, wake up all per association waiters. */
4519 void sctp_write_space(struct sock *sk)
4521 struct sctp_association *asoc;
4522 struct list_head *pos;
4524 /* Wake up the tasks in each wait queue. */
4525 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4526 asoc = list_entry(pos, struct sctp_association, asocs);
4527 __sctp_write_space(asoc);
4531 /* Is there any sndbuf space available on the socket?
4533 * Note that wmem_queued is the sum of the send buffers on all of the
4534 * associations on the same socket. For a UDP-style socket with
4535 * multiple associations, it is possible for it to be "unwriteable"
4536 * prematurely. I assume that this is acceptable because
4537 * a premature "unwriteable" is better than an accidental "writeable" which
4538 * would cause an unwanted block under certain circumstances. For the 1-1
4539 * UDP-style sockets or TCP-style sockets, this code should work.
4540 * - Daisy
4542 static int sctp_writeable(struct sock *sk)
4544 int amt = 0;
4546 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4547 if (amt < 0)
4548 amt = 0;
4549 return amt;
4552 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4553 * returns immediately with EINPROGRESS.
4555 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4557 struct sock *sk = asoc->base.sk;
4558 int err = 0;
4559 long current_timeo = *timeo_p;
4560 DEFINE_WAIT(wait);
4562 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4563 (long)(*timeo_p));
4565 /* Increment the association's refcnt. */
4566 sctp_association_hold(asoc);
4568 for (;;) {
4569 prepare_to_wait_exclusive(&asoc->wait, &wait,
4570 TASK_INTERRUPTIBLE);
4571 if (!*timeo_p)
4572 goto do_nonblock;
4573 if (sk->sk_shutdown & RCV_SHUTDOWN)
4574 break;
4575 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4576 asoc->base.dead)
4577 goto do_error;
4578 if (signal_pending(current))
4579 goto do_interrupted;
4581 if (sctp_state(asoc, ESTABLISHED))
4582 break;
4584 /* Let another process have a go. Since we are going
4585 * to sleep anyway.
4587 sctp_release_sock(sk);
4588 current_timeo = schedule_timeout(current_timeo);
4589 sctp_lock_sock(sk);
4591 *timeo_p = current_timeo;
4594 out:
4595 finish_wait(&asoc->wait, &wait);
4597 /* Release the association's refcnt. */
4598 sctp_association_put(asoc);
4600 return err;
4602 do_error:
4603 if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
4604 asoc->max_init_attempts)
4605 err = -ETIMEDOUT;
4606 else
4607 err = -ECONNREFUSED;
4608 goto out;
4610 do_interrupted:
4611 err = sock_intr_errno(*timeo_p);
4612 goto out;
4614 do_nonblock:
4615 err = -EINPROGRESS;
4616 goto out;
4619 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4621 struct sctp_endpoint *ep;
4622 int err = 0;
4623 DEFINE_WAIT(wait);
4625 ep = sctp_sk(sk)->ep;
4628 for (;;) {
4629 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4630 TASK_INTERRUPTIBLE);
4632 if (list_empty(&ep->asocs)) {
4633 sctp_release_sock(sk);
4634 timeo = schedule_timeout(timeo);
4635 sctp_lock_sock(sk);
4638 err = -EINVAL;
4639 if (!sctp_sstate(sk, LISTENING))
4640 break;
4642 err = 0;
4643 if (!list_empty(&ep->asocs))
4644 break;
4646 err = sock_intr_errno(timeo);
4647 if (signal_pending(current))
4648 break;
4650 err = -EAGAIN;
4651 if (!timeo)
4652 break;
4655 finish_wait(sk->sk_sleep, &wait);
4657 return err;
4660 void sctp_wait_for_close(struct sock *sk, long timeout)
4662 DEFINE_WAIT(wait);
4664 do {
4665 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4666 if (list_empty(&sctp_sk(sk)->ep->asocs))
4667 break;
4668 sctp_release_sock(sk);
4669 timeout = schedule_timeout(timeout);
4670 sctp_lock_sock(sk);
4671 } while (!signal_pending(current) && timeout);
4673 finish_wait(sk->sk_sleep, &wait);
4676 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4677 * and its messages to the newsk.
4679 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4680 struct sctp_association *assoc,
4681 sctp_socket_type_t type)
4683 struct sctp_sock *oldsp = sctp_sk(oldsk);
4684 struct sctp_sock *newsp = sctp_sk(newsk);
4685 struct sctp_bind_bucket *pp; /* hash list port iterator */
4686 struct sctp_endpoint *newep = newsp->ep;
4687 struct sk_buff *skb, *tmp;
4688 struct sctp_ulpevent *event;
4690 /* Migrate socket buffer sizes and all the socket level options to the
4691 * new socket.
4693 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4694 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4695 /* Brute force copy old sctp opt. */
4696 inet_sk_copy_descendant(newsk, oldsk);
4698 /* Restore the ep value that was overwritten with the above structure
4699 * copy.
4701 newsp->ep = newep;
4702 newsp->hmac = NULL;
4704 /* Hook this new socket in to the bind_hash list. */
4705 pp = sctp_sk(oldsk)->bind_hash;
4706 sk_add_bind_node(newsk, &pp->owner);
4707 sctp_sk(newsk)->bind_hash = pp;
4708 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4710 /* Move any messages in the old socket's receive queue that are for the
4711 * peeled off association to the new socket's receive queue.
4713 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4714 event = sctp_skb2event(skb);
4715 if (event->asoc == assoc) {
4716 __skb_unlink(skb, skb->list);
4717 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4721 /* Clean up any messages pending delivery due to partial
4722 * delivery. Three cases:
4723 * 1) No partial deliver; no work.
4724 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4725 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
4727 skb_queue_head_init(&newsp->pd_lobby);
4728 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4730 if (sctp_sk(oldsk)->pd_mode) {
4731 struct sk_buff_head *queue;
4733 /* Decide which queue to move pd_lobby skbs to. */
4734 if (assoc->ulpq.pd_mode) {
4735 queue = &newsp->pd_lobby;
4736 } else
4737 queue = &newsk->sk_receive_queue;
4739 /* Walk through the pd_lobby, looking for skbs that
4740 * need moved to the new socket.
4742 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4743 event = sctp_skb2event(skb);
4744 if (event->asoc == assoc) {
4745 __skb_unlink(skb, skb->list);
4746 __skb_queue_tail(queue, skb);
4750 /* Clear up any skbs waiting for the partial
4751 * delivery to finish.
4753 if (assoc->ulpq.pd_mode)
4754 sctp_clear_pd(oldsk);
4758 /* Set the type of socket to indicate that it is peeled off from the
4759 * original UDP-style socket or created with the accept() call on a
4760 * TCP-style socket..
4762 newsp->type = type;
4764 /* Migrate the association to the new socket. */
4765 sctp_assoc_migrate(assoc, newsk);
4767 /* If the association on the newsk is already closed before accept()
4768 * is called, set RCV_SHUTDOWN flag.
4770 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4771 newsk->sk_shutdown |= RCV_SHUTDOWN;
4773 newsk->sk_state = SCTP_SS_ESTABLISHED;
4776 /* This proto struct describes the ULP interface for SCTP. */
4777 struct proto sctp_prot = {
4778 .name = "SCTP",
4779 .owner = THIS_MODULE,
4780 .close = sctp_close,
4781 .connect = sctp_connect,
4782 .disconnect = sctp_disconnect,
4783 .accept = sctp_accept,
4784 .ioctl = sctp_ioctl,
4785 .init = sctp_init_sock,
4786 .destroy = sctp_destroy_sock,
4787 .shutdown = sctp_shutdown,
4788 .setsockopt = sctp_setsockopt,
4789 .getsockopt = sctp_getsockopt,
4790 .sendmsg = sctp_sendmsg,
4791 .recvmsg = sctp_recvmsg,
4792 .bind = sctp_bind,
4793 .backlog_rcv = sctp_backlog_rcv,
4794 .hash = sctp_hash,
4795 .unhash = sctp_unhash,
4796 .get_port = sctp_get_port,
4797 .obj_size = sizeof(struct sctp_sock),
4800 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4801 struct proto sctpv6_prot = {
4802 .name = "SCTPv6",
4803 .owner = THIS_MODULE,
4804 .close = sctp_close,
4805 .connect = sctp_connect,
4806 .disconnect = sctp_disconnect,
4807 .accept = sctp_accept,
4808 .ioctl = sctp_ioctl,
4809 .init = sctp_init_sock,
4810 .destroy = sctp_destroy_sock,
4811 .shutdown = sctp_shutdown,
4812 .setsockopt = sctp_setsockopt,
4813 .getsockopt = sctp_getsockopt,
4814 .sendmsg = sctp_sendmsg,
4815 .recvmsg = sctp_recvmsg,
4816 .bind = sctp_bind,
4817 .backlog_rcv = sctp_backlog_rcv,
4818 .hash = sctp_hash,
4819 .unhash = sctp_unhash,
4820 .get_port = sctp_get_port,
4821 .obj_size = sizeof(struct sctp6_sock),
4823 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */