initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / sctp / socket.c
blob0d1bc4507d6b77920d65c7e2b9383e7522499163
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_opt *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 amt = sk->sk_sndbuf - asoc->sndbuf_used;
119 if (amt < 0)
120 amt = 0;
121 return amt;
124 /* Increment the used sndbuf space count of the corresponding association by
125 * the size of the outgoing data chunk.
126 * Also, set the skb destructor for sndbuf accounting later.
128 * Since it is always 1-1 between chunk and skb, and also a new skb is always
129 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
130 * destructor in the data chunk skb for the purpose of the sndbuf space
131 * tracking.
133 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
135 struct sctp_association *asoc = chunk->asoc;
136 struct sock *sk = asoc->base.sk;
138 /* The sndbuf space is tracked per association. */
139 sctp_association_hold(asoc);
141 chunk->skb->destructor = sctp_wfree;
142 /* Save the chunk pointer in skb for sctp_wfree to use later. */
143 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
145 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk);
146 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk);
149 /* Verify that this is a valid address. */
150 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
151 int len)
153 struct sctp_af *af;
155 /* Verify basic sockaddr. */
156 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
157 if (!af)
158 return -EINVAL;
160 /* Is this a valid SCTP address? */
161 if (!af->addr_valid(addr, sctp_sk(sk)))
162 return -EINVAL;
164 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
165 return -EINVAL;
167 return 0;
170 /* Look up the association by its id. If this is not a UDP-style
171 * socket, the ID field is always ignored.
173 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
175 struct sctp_association *asoc = NULL;
177 /* If this is not a UDP-style socket, assoc id should be ignored. */
178 if (!sctp_style(sk, UDP)) {
179 /* Return NULL if the socket state is not ESTABLISHED. It
180 * could be a TCP-style listening socket or a socket which
181 * hasn't yet called connect() to establish an association.
183 if (!sctp_sstate(sk, ESTABLISHED))
184 return NULL;
186 /* Get the first and the only association from the list. */
187 if (!list_empty(&sctp_sk(sk)->ep->asocs))
188 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
189 struct sctp_association, asocs);
190 return asoc;
193 /* Otherwise this is a UDP-style socket. */
194 if (!id || (id == (sctp_assoc_t)-1))
195 return NULL;
197 spin_lock_bh(&sctp_assocs_id_lock);
198 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
199 spin_unlock_bh(&sctp_assocs_id_lock);
201 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
202 return NULL;
204 return asoc;
207 /* Look up the transport from an address and an assoc id. If both address and
208 * id are specified, the associations matching the address and the id should be
209 * the same.
211 struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
212 struct sockaddr_storage *addr,
213 sctp_assoc_t id)
215 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
216 struct sctp_transport *transport;
217 union sctp_addr *laddr = (union sctp_addr *)addr;
219 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
220 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
221 (union sctp_addr *)addr,
222 &transport);
223 laddr->v4.sin_port = htons(laddr->v4.sin_port);
225 if (!addr_asoc)
226 return NULL;
228 id_asoc = sctp_id2assoc(sk, id);
229 if (id_asoc && (id_asoc != addr_asoc))
230 return NULL;
232 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
233 (union sctp_addr *)addr);
235 return transport;
238 /* API 3.1.2 bind() - UDP Style Syntax
239 * The syntax of bind() is,
241 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
243 * sd - the socket descriptor returned by socket().
244 * addr - the address structure (struct sockaddr_in or struct
245 * sockaddr_in6 [RFC 2553]),
246 * addr_len - the size of the address structure.
248 int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
250 int retval = 0;
252 sctp_lock_sock(sk);
254 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
255 sk, uaddr, addr_len);
257 /* Disallow binding twice. */
258 if (!sctp_sk(sk)->ep->base.bind_addr.port)
259 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
260 addr_len);
261 else
262 retval = -EINVAL;
264 sctp_release_sock(sk);
266 return retval;
269 static long sctp_get_port_local(struct sock *, union sctp_addr *);
271 /* Verify this is a valid sockaddr. */
272 static struct sctp_af *sctp_sockaddr_af(struct sctp_opt *opt,
273 union sctp_addr *addr, int len)
275 struct sctp_af *af;
277 /* Check minimum size. */
278 if (len < sizeof (struct sockaddr))
279 return NULL;
281 /* Does this PF support this AF? */
282 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
283 return NULL;
285 /* If we get this far, af is valid. */
286 af = sctp_get_af_specific(addr->sa.sa_family);
288 if (len < af->sockaddr_len)
289 return NULL;
291 return af;
294 /* Bind a local address either to an endpoint or to an association. */
295 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
297 struct sctp_opt *sp = sctp_sk(sk);
298 struct sctp_endpoint *ep = sp->ep;
299 struct sctp_bind_addr *bp = &ep->base.bind_addr;
300 struct sctp_af *af;
301 unsigned short snum;
302 int ret = 0;
304 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
305 sk, addr, len);
307 /* Common sockaddr verification. */
308 af = sctp_sockaddr_af(sp, addr, len);
309 if (!af)
310 return -EINVAL;
312 /* PF specific bind() address verification. */
313 if (!sp->pf->bind_verify(sp, addr))
314 return -EADDRNOTAVAIL;
316 snum= ntohs(addr->v4.sin_port);
318 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
319 bp->port, snum);
321 /* We must either be unbound, or bind to the same port. */
322 if (bp->port && (snum != bp->port)) {
323 SCTP_DEBUG_PRINTK("sctp_do_bind:"
324 " New port %d does not match existing port "
325 "%d.\n", snum, bp->port);
326 return -EINVAL;
329 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
330 return -EACCES;
332 /* Make sure we are allowed to bind here.
333 * The function sctp_get_port_local() does duplicate address
334 * detection.
336 if ((ret = sctp_get_port_local(sk, addr))) {
337 if (ret == (long) sk) {
338 /* This endpoint has a conflicting address. */
339 return -EINVAL;
340 } else {
341 return -EADDRINUSE;
345 /* Refresh ephemeral port. */
346 if (!snum)
347 snum = inet_sk(sk)->num;
349 /* Add the address to the bind address list. */
350 sctp_local_bh_disable();
351 sctp_write_lock(&ep->base.addr_lock);
353 /* Use GFP_ATOMIC since BHs are disabled. */
354 addr->v4.sin_port = ntohs(addr->v4.sin_port);
355 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
356 addr->v4.sin_port = htons(addr->v4.sin_port);
357 if (!ret && !bp->port)
358 bp->port = snum;
359 sctp_write_unlock(&ep->base.addr_lock);
360 sctp_local_bh_enable();
362 /* Copy back into socket for getsockname() use. */
363 if (!ret) {
364 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
365 af->to_sk_saddr(addr, sk);
368 return ret;
371 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
373 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
374 * at any one time. If a sender, after sending an ASCONF chunk, decides
375 * it needs to transfer another ASCONF Chunk, it MUST wait until the
376 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
377 * subsequent ASCONF. Note this restriction binds each side, so at any
378 * time two ASCONF may be in-transit on any given association (one sent
379 * from each endpoint).
381 static int sctp_send_asconf(struct sctp_association *asoc,
382 struct sctp_chunk *chunk)
384 int retval = 0;
386 /* If there is an outstanding ASCONF chunk, queue it for later
387 * transmission.
389 if (asoc->addip_last_asconf) {
390 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
391 goto out;
394 /* Hold the chunk until an ASCONF_ACK is received. */
395 sctp_chunk_hold(chunk);
396 retval = sctp_primitive_ASCONF(asoc, chunk);
397 if (retval)
398 sctp_chunk_free(chunk);
399 else
400 asoc->addip_last_asconf = chunk;
402 out:
403 return retval;
406 /* Add a list of addresses as bind addresses to local endpoint or
407 * association.
409 * Basically run through each address specified in the addrs/addrcnt
410 * array/length pair, determine if it is IPv6 or IPv4 and call
411 * sctp_do_bind() on it.
413 * If any of them fails, then the operation will be reversed and the
414 * ones that were added will be removed.
416 * Only sctp_setsockopt_bindx() is supposed to call this function.
418 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
420 int cnt;
421 int retval = 0;
422 void *addr_buf;
423 struct sockaddr *sa_addr;
424 struct sctp_af *af;
426 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
427 sk, addrs, addrcnt);
429 addr_buf = addrs;
430 for (cnt = 0; cnt < addrcnt; cnt++) {
431 /* The list may contain either IPv4 or IPv6 address;
432 * determine the address length for walking thru the list.
434 sa_addr = (struct sockaddr *)addr_buf;
435 af = sctp_get_af_specific(sa_addr->sa_family);
436 if (!af) {
437 retval = -EINVAL;
438 goto err_bindx_add;
441 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
442 af->sockaddr_len);
444 addr_buf += af->sockaddr_len;
446 err_bindx_add:
447 if (retval < 0) {
448 /* Failed. Cleanup the ones that have been added */
449 if (cnt > 0)
450 sctp_bindx_rem(sk, addrs, cnt);
451 return retval;
455 return retval;
458 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
459 * associations that are part of the endpoint indicating that a list of local
460 * addresses are added to the endpoint.
462 * If any of the addresses is already in the bind address list of the
463 * association, we do not send the chunk for that association. But it will not
464 * affect other associations.
466 * Only sctp_setsockopt_bindx() is supposed to call this function.
468 static int sctp_send_asconf_add_ip(struct sock *sk,
469 struct sockaddr *addrs,
470 int addrcnt)
472 struct sctp_opt *sp;
473 struct sctp_endpoint *ep;
474 struct sctp_association *asoc;
475 struct sctp_bind_addr *bp;
476 struct sctp_chunk *chunk;
477 struct sctp_sockaddr_entry *laddr;
478 union sctp_addr *addr;
479 void *addr_buf;
480 struct sctp_af *af;
481 struct list_head *pos;
482 struct list_head *p;
483 int i;
484 int retval = 0;
486 if (!sctp_addip_enable)
487 return retval;
489 sp = sctp_sk(sk);
490 ep = sp->ep;
492 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
493 __FUNCTION__, sk, addrs, addrcnt);
495 list_for_each(pos, &ep->asocs) {
496 asoc = list_entry(pos, struct sctp_association, asocs);
498 if (!asoc->peer.asconf_capable)
499 continue;
501 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
502 continue;
504 if (!sctp_state(asoc, ESTABLISHED))
505 continue;
507 /* Check if any address in the packed array of addresses is
508 * in the bind address list of the association. If so,
509 * do not send the asconf chunk to its peer, but continue with
510 * other associations.
512 addr_buf = addrs;
513 for (i = 0; i < addrcnt; i++) {
514 addr = (union sctp_addr *)addr_buf;
515 af = sctp_get_af_specific(addr->v4.sin_family);
516 if (!af) {
517 retval = -EINVAL;
518 goto out;
521 if (sctp_assoc_lookup_laddr(asoc, addr))
522 break;
524 addr_buf += af->sockaddr_len;
526 if (i < addrcnt)
527 continue;
529 /* Use the first address in bind addr list of association as
530 * Address Parameter of ASCONF CHUNK.
532 sctp_read_lock(&asoc->base.addr_lock);
533 bp = &asoc->base.bind_addr;
534 p = bp->address_list.next;
535 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
536 sctp_read_unlock(&asoc->base.addr_lock);
538 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
539 addrcnt, SCTP_PARAM_ADD_IP);
540 if (!chunk) {
541 retval = -ENOMEM;
542 goto out;
545 retval = sctp_send_asconf(asoc, chunk);
547 /* FIXME: After sending the add address ASCONF chunk, we
548 * cannot append the address to the association's binding
549 * address list, because the new address may be used as the
550 * source of a message sent to the peer before the ASCONF
551 * chunk is received by the peer. So we should wait until
552 * ASCONF_ACK is received.
556 out:
557 return retval;
560 /* Remove a list of addresses from bind addresses list. Do not remove the
561 * last address.
563 * Basically run through each address specified in the addrs/addrcnt
564 * array/length pair, determine if it is IPv6 or IPv4 and call
565 * sctp_del_bind() on it.
567 * If any of them fails, then the operation will be reversed and the
568 * ones that were removed will be added back.
570 * At least one address has to be left; if only one address is
571 * available, the operation will return -EBUSY.
573 * Only sctp_setsockopt_bindx() is supposed to call this function.
575 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
577 struct sctp_opt *sp = sctp_sk(sk);
578 struct sctp_endpoint *ep = sp->ep;
579 int cnt;
580 struct sctp_bind_addr *bp = &ep->base.bind_addr;
581 int retval = 0;
582 union sctp_addr saveaddr;
583 void *addr_buf;
584 struct sockaddr *sa_addr;
585 struct sctp_af *af;
587 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
588 sk, addrs, addrcnt);
590 addr_buf = addrs;
591 for (cnt = 0; cnt < addrcnt; cnt++) {
592 /* If the bind address list is empty or if there is only one
593 * bind address, there is nothing more to be removed (we need
594 * at least one address here).
596 if (list_empty(&bp->address_list) ||
597 (sctp_list_single_entry(&bp->address_list))) {
598 retval = -EBUSY;
599 goto err_bindx_rem;
602 /* The list may contain either IPv4 or IPv6 address;
603 * determine the address length to copy the address to
604 * saveaddr.
606 sa_addr = (struct sockaddr *)addr_buf;
607 af = sctp_get_af_specific(sa_addr->sa_family);
608 if (!af) {
609 retval = -EINVAL;
610 goto err_bindx_rem;
612 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
613 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
614 if (saveaddr.v4.sin_port != bp->port) {
615 retval = -EINVAL;
616 goto err_bindx_rem;
619 /* FIXME - There is probably a need to check if sk->sk_saddr and
620 * sk->sk_rcv_addr are currently set to one of the addresses to
621 * be removed. This is something which needs to be looked into
622 * when we are fixing the outstanding issues with multi-homing
623 * socket routing and failover schemes. Refer to comments in
624 * sctp_do_bind(). -daisy
626 sctp_local_bh_disable();
627 sctp_write_lock(&ep->base.addr_lock);
629 retval = sctp_del_bind_addr(bp, &saveaddr);
631 sctp_write_unlock(&ep->base.addr_lock);
632 sctp_local_bh_enable();
634 addr_buf += af->sockaddr_len;
635 err_bindx_rem:
636 if (retval < 0) {
637 /* Failed. Add the ones that has been removed back */
638 if (cnt > 0)
639 sctp_bindx_add(sk, addrs, cnt);
640 return retval;
644 return retval;
647 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
648 * the associations that are part of the endpoint indicating that a list of
649 * local addresses are removed from the endpoint.
651 * If any of the addresses is already in the bind address list of the
652 * association, we do not send the chunk for that association. But it will not
653 * affect other associations.
655 * Only sctp_setsockopt_bindx() is supposed to call this function.
657 static int sctp_send_asconf_del_ip(struct sock *sk,
658 struct sockaddr *addrs,
659 int addrcnt)
661 struct sctp_opt *sp;
662 struct sctp_endpoint *ep;
663 struct sctp_association *asoc;
664 struct sctp_bind_addr *bp;
665 struct sctp_chunk *chunk;
666 union sctp_addr *laddr;
667 void *addr_buf;
668 struct sctp_af *af;
669 struct list_head *pos;
670 int i;
671 int retval = 0;
673 if (!sctp_addip_enable)
674 return retval;
676 sp = sctp_sk(sk);
677 ep = sp->ep;
679 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
680 __FUNCTION__, sk, addrs, addrcnt);
682 list_for_each(pos, &ep->asocs) {
683 asoc = list_entry(pos, struct sctp_association, asocs);
685 if (!asoc->peer.asconf_capable)
686 continue;
688 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
689 continue;
691 if (!sctp_state(asoc, ESTABLISHED))
692 continue;
694 /* Check if any address in the packed array of addresses is
695 * not present in the bind address list of the association.
696 * If so, do not send the asconf chunk to its peer, but
697 * continue with other associations.
699 addr_buf = addrs;
700 for (i = 0; i < addrcnt; i++) {
701 laddr = (union sctp_addr *)addr_buf;
702 af = sctp_get_af_specific(laddr->v4.sin_family);
703 if (!af) {
704 retval = -EINVAL;
705 goto out;
708 if (!sctp_assoc_lookup_laddr(asoc, laddr))
709 break;
711 addr_buf += af->sockaddr_len;
713 if (i < addrcnt)
714 continue;
716 /* Find one address in the association's bind address list
717 * that is not in the packed array of addresses. This is to
718 * make sure that we do not delete all the addresses in the
719 * association.
721 sctp_read_lock(&asoc->base.addr_lock);
722 bp = &asoc->base.bind_addr;
723 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
724 addrcnt, sp);
725 sctp_read_unlock(&asoc->base.addr_lock);
726 if (!laddr)
727 continue;
729 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
730 SCTP_PARAM_DEL_IP);
731 if (!chunk) {
732 retval = -ENOMEM;
733 goto out;
736 retval = sctp_send_asconf(asoc, chunk);
738 /* FIXME: After sending the delete address ASCONF chunk, we
739 * cannot remove the addresses from the association's bind
740 * address list, because there maybe some packet send to
741 * the delete addresses, so we should wait until ASCONF_ACK
742 * packet is received.
745 out:
746 return retval;
749 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
751 * API 8.1
752 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
753 * int flags);
755 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
756 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
757 * or IPv6 addresses.
759 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
760 * Section 3.1.2 for this usage.
762 * addrs is a pointer to an array of one or more socket addresses. Each
763 * address is contained in its appropriate structure (i.e. struct
764 * sockaddr_in or struct sockaddr_in6) the family of the address type
765 * must be used to distengish the address length (note that this
766 * representation is termed a "packed array" of addresses). The caller
767 * specifies the number of addresses in the array with addrcnt.
769 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
770 * -1, and sets errno to the appropriate error code.
772 * For SCTP, the port given in each socket address must be the same, or
773 * sctp_bindx() will fail, setting errno to EINVAL.
775 * The flags parameter is formed from the bitwise OR of zero or more of
776 * the following currently defined flags:
778 * SCTP_BINDX_ADD_ADDR
780 * SCTP_BINDX_REM_ADDR
782 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
783 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
784 * addresses from the association. The two flags are mutually exclusive;
785 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
786 * not remove all addresses from an association; sctp_bindx() will
787 * reject such an attempt with EINVAL.
789 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
790 * additional addresses with an endpoint after calling bind(). Or use
791 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
792 * socket is associated with so that no new association accepted will be
793 * associated with those addresses. If the endpoint supports dynamic
794 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
795 * endpoint to send the appropriate message to the peer to change the
796 * peers address lists.
798 * Adding and removing addresses from a connected association is
799 * optional functionality. Implementations that do not support this
800 * functionality should return EOPNOTSUPP.
802 * Basically do nothing but copying the addresses from user to kernel
803 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
804 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
806 * We don't use copy_from_user() for optimization: we first do the
807 * sanity checks (buffer size -fast- and access check-healthy
808 * pointer); if all of those succeed, then we can alloc the memory
809 * (expensive operation) needed to copy the data to kernel. Then we do
810 * the copying without checking the user space area
811 * (__copy_from_user()).
813 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
814 * it.
816 * sk The sk of the socket
817 * addrs The pointer to the addresses in user land
818 * addrssize Size of the addrs buffer
819 * op Operation to perform (add or remove, see the flags of
820 * sctp_bindx)
822 * Returns 0 if ok, <0 errno code on error.
824 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
825 struct sockaddr __user *addrs,
826 int addrs_size, int op)
828 struct sockaddr *kaddrs;
829 int err;
830 int addrcnt = 0;
831 int walk_size = 0;
832 struct sockaddr *sa_addr;
833 void *addr_buf;
834 struct sctp_af *af;
836 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
837 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
839 if (unlikely(addrs_size <= 0))
840 return -EINVAL;
842 /* Check the user passed a healthy pointer. */
843 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
844 return -EFAULT;
846 /* Alloc space for the address array in kernel memory. */
847 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
848 if (unlikely(!kaddrs))
849 return -ENOMEM;
851 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
852 kfree(kaddrs);
853 return -EFAULT;
856 /* Walk through the addrs buffer and count the number of addresses. */
857 addr_buf = kaddrs;
858 while (walk_size < addrs_size) {
859 sa_addr = (struct sockaddr *)addr_buf;
860 af = sctp_get_af_specific(sa_addr->sa_family);
862 /* If the address family is not supported or if this address
863 * causes the address buffer to overflow return EINVAL.
865 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
866 kfree(kaddrs);
867 return -EINVAL;
869 addrcnt++;
870 addr_buf += af->sockaddr_len;
871 walk_size += af->sockaddr_len;
874 /* Do the work. */
875 switch (op) {
876 case SCTP_BINDX_ADD_ADDR:
877 err = sctp_bindx_add(sk, kaddrs, addrcnt);
878 if (err)
879 goto out;
880 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
881 break;
883 case SCTP_BINDX_REM_ADDR:
884 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
885 if (err)
886 goto out;
887 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
888 break;
890 default:
891 err = -EINVAL;
892 break;
895 out:
896 kfree(kaddrs);
898 return err;
901 /* API 3.1.4 close() - UDP Style Syntax
902 * Applications use close() to perform graceful shutdown (as described in
903 * Section 10.1 of [SCTP]) on ALL the associations currently represented
904 * by a UDP-style socket.
906 * The syntax is
908 * ret = close(int sd);
910 * sd - the socket descriptor of the associations to be closed.
912 * To gracefully shutdown a specific association represented by the
913 * UDP-style socket, an application should use the sendmsg() call,
914 * passing no user data, but including the appropriate flag in the
915 * ancillary data (see Section xxxx).
917 * If sd in the close() call is a branched-off socket representing only
918 * one association, the shutdown is performed on that association only.
920 * 4.1.6 close() - TCP Style Syntax
922 * Applications use close() to gracefully close down an association.
924 * The syntax is:
926 * int close(int sd);
928 * sd - the socket descriptor of the association to be closed.
930 * After an application calls close() on a socket descriptor, no further
931 * socket operations will succeed on that descriptor.
933 * API 7.1.4 SO_LINGER
935 * An application using the TCP-style socket can use this option to
936 * perform the SCTP ABORT primitive. The linger option structure is:
938 * struct linger {
939 * int l_onoff; // option on/off
940 * int l_linger; // linger time
941 * };
943 * To enable the option, set l_onoff to 1. If the l_linger value is set
944 * to 0, calling close() is the same as the ABORT primitive. If the
945 * value is set to a negative value, the setsockopt() call will return
946 * an error. If the value is set to a positive value linger_time, the
947 * close() can be blocked for at most linger_time ms. If the graceful
948 * shutdown phase does not finish during this period, close() will
949 * return but the graceful shutdown phase continues in the system.
951 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
953 struct sctp_endpoint *ep;
954 struct sctp_association *asoc;
955 struct list_head *pos, *temp;
957 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
959 sctp_lock_sock(sk);
960 sk->sk_shutdown = SHUTDOWN_MASK;
962 ep = sctp_sk(sk)->ep;
964 /* Walk all associations on a socket, not on an endpoint. */
965 list_for_each_safe(pos, temp, &ep->asocs) {
966 asoc = list_entry(pos, struct sctp_association, asocs);
968 if (sctp_style(sk, TCP)) {
969 /* A closed association can still be in the list if
970 * it belongs to a TCP-style listening socket that is
971 * not yet accepted. If so, free it. If not, send an
972 * ABORT or SHUTDOWN based on the linger options.
974 if (sctp_state(asoc, CLOSED)) {
975 sctp_unhash_established(asoc);
976 sctp_association_free(asoc);
978 } else if (sock_flag(sk, SOCK_LINGER) &&
979 !sk->sk_lingertime)
980 sctp_primitive_ABORT(asoc, NULL);
981 else
982 sctp_primitive_SHUTDOWN(asoc, NULL);
983 } else
984 sctp_primitive_SHUTDOWN(asoc, NULL);
987 /* Clean up any skbs sitting on the receive queue. */
988 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
989 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
991 /* On a TCP-style socket, block for at most linger_time if set. */
992 if (sctp_style(sk, TCP) && timeout)
993 sctp_wait_for_close(sk, timeout);
995 /* This will run the backlog queue. */
996 sctp_release_sock(sk);
998 /* Supposedly, no process has access to the socket, but
999 * the net layers still may.
1001 sctp_local_bh_disable();
1002 sctp_bh_lock_sock(sk);
1004 /* Hold the sock, since sk_common_release() will put sock_put()
1005 * and we have just a little more cleanup.
1007 sock_hold(sk);
1008 sk_common_release(sk);
1010 sctp_bh_unlock_sock(sk);
1011 sctp_local_bh_enable();
1013 sock_put(sk);
1015 SCTP_DBG_OBJCNT_DEC(sock);
1018 /* Handle EPIPE error. */
1019 static int sctp_error(struct sock *sk, int flags, int err)
1021 if (err == -EPIPE)
1022 err = sock_error(sk) ? : -EPIPE;
1023 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1024 send_sig(SIGPIPE, current, 0);
1025 return err;
1028 /* API 3.1.3 sendmsg() - UDP Style Syntax
1030 * An application uses sendmsg() and recvmsg() calls to transmit data to
1031 * and receive data from its peer.
1033 * ssize_t sendmsg(int socket, const struct msghdr *message,
1034 * int flags);
1036 * socket - the socket descriptor of the endpoint.
1037 * message - pointer to the msghdr structure which contains a single
1038 * user message and possibly some ancillary data.
1040 * See Section 5 for complete description of the data
1041 * structures.
1043 * flags - flags sent or received with the user message, see Section
1044 * 5 for complete description of the flags.
1046 * Note: This function could use a rewrite especially when explicit
1047 * connect support comes in.
1049 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1051 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1053 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1054 struct msghdr *msg, size_t msg_len)
1056 struct sctp_opt *sp;
1057 struct sctp_endpoint *ep;
1058 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1059 struct sctp_transport *transport, *chunk_tp;
1060 struct sctp_chunk *chunk;
1061 union sctp_addr to;
1062 struct sockaddr *msg_name = NULL;
1063 struct sctp_sndrcvinfo default_sinfo = { 0 };
1064 struct sctp_sndrcvinfo *sinfo;
1065 struct sctp_initmsg *sinit;
1066 sctp_assoc_t associd = NULL;
1067 sctp_cmsgs_t cmsgs = { NULL };
1068 int err;
1069 sctp_scope_t scope;
1070 long timeo;
1071 __u16 sinfo_flags = 0;
1072 struct sctp_datamsg *datamsg;
1073 struct list_head *pos;
1074 int msg_flags = msg->msg_flags;
1076 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1077 sk, msg, msg_len);
1079 err = 0;
1080 sp = sctp_sk(sk);
1081 ep = sp->ep;
1083 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1085 /* We cannot send a message over a TCP-style listening socket. */
1086 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1087 err = -EPIPE;
1088 goto out_nounlock;
1091 /* Parse out the SCTP CMSGs. */
1092 err = sctp_msghdr_parse(msg, &cmsgs);
1094 if (err) {
1095 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1096 goto out_nounlock;
1099 /* Fetch the destination address for this packet. This
1100 * address only selects the association--it is not necessarily
1101 * the address we will send to.
1102 * For a peeled-off socket, msg_name is ignored.
1104 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1105 int msg_namelen = msg->msg_namelen;
1107 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1108 msg_namelen);
1109 if (err)
1110 return err;
1112 if (msg_namelen > sizeof(to))
1113 msg_namelen = sizeof(to);
1114 memcpy(&to, msg->msg_name, msg_namelen);
1115 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1116 "0x%x:%u.\n",
1117 to.v4.sin_addr.s_addr, to.v4.sin_port);
1119 to.v4.sin_port = ntohs(to.v4.sin_port);
1120 msg_name = msg->msg_name;
1123 sinfo = cmsgs.info;
1124 sinit = cmsgs.init;
1126 /* Did the user specify SNDRCVINFO? */
1127 if (sinfo) {
1128 sinfo_flags = sinfo->sinfo_flags;
1129 associd = sinfo->sinfo_assoc_id;
1132 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1133 msg_len, sinfo_flags);
1135 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1136 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1137 err = -EINVAL;
1138 goto out_nounlock;
1141 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1142 * length messages when MSG_EOF|MSG_ABORT is not set.
1143 * If MSG_ABORT is set, the message length could be non zero with
1144 * the msg_iov set to the user abort reason.
1146 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1147 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1148 err = -EINVAL;
1149 goto out_nounlock;
1152 /* If MSG_ADDR_OVER is set, there must be an address
1153 * specified in msg_name.
1155 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1156 err = -EINVAL;
1157 goto out_nounlock;
1160 transport = NULL;
1162 SCTP_DEBUG_PRINTK("About to look up association.\n");
1164 sctp_lock_sock(sk);
1166 /* If a msg_name has been specified, assume this is to be used. */
1167 if (msg_name) {
1168 /* Look for a matching association on the endpoint. */
1169 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1170 if (!asoc) {
1171 /* If we could not find a matching association on the
1172 * endpoint, make sure that it is not a TCP-style
1173 * socket that already has an association or there is
1174 * no peeled-off association on another socket.
1176 if ((sctp_style(sk, TCP) &&
1177 sctp_sstate(sk, ESTABLISHED)) ||
1178 sctp_endpoint_is_peeled_off(ep, &to)) {
1179 err = -EADDRNOTAVAIL;
1180 goto out_unlock;
1183 } else {
1184 asoc = sctp_id2assoc(sk, associd);
1185 if (!asoc) {
1186 err = -EPIPE;
1187 goto out_unlock;
1191 if (asoc) {
1192 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1194 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1195 * socket that has an association in CLOSED state. This can
1196 * happen when an accepted socket has an association that is
1197 * already CLOSED.
1199 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1200 err = -EPIPE;
1201 goto out_unlock;
1204 if (sinfo_flags & MSG_EOF) {
1205 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1206 asoc);
1207 sctp_primitive_SHUTDOWN(asoc, NULL);
1208 err = 0;
1209 goto out_unlock;
1211 if (sinfo_flags & MSG_ABORT) {
1212 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1213 sctp_primitive_ABORT(asoc, msg);
1214 err = 0;
1215 goto out_unlock;
1219 /* Do we need to create the association? */
1220 if (!asoc) {
1221 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1223 if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
1224 err = -EINVAL;
1225 goto out_unlock;
1228 /* Check for invalid stream against the stream counts,
1229 * either the default or the user specified stream counts.
1231 if (sinfo) {
1232 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1233 /* Check against the defaults. */
1234 if (sinfo->sinfo_stream >=
1235 sp->initmsg.sinit_num_ostreams) {
1236 err = -EINVAL;
1237 goto out_unlock;
1239 } else {
1240 /* Check against the requested. */
1241 if (sinfo->sinfo_stream >=
1242 sinit->sinit_num_ostreams) {
1243 err = -EINVAL;
1244 goto out_unlock;
1250 * API 3.1.2 bind() - UDP Style Syntax
1251 * If a bind() or sctp_bindx() is not called prior to a
1252 * sendmsg() call that initiates a new association, the
1253 * system picks an ephemeral port and will choose an address
1254 * set equivalent to binding with a wildcard address.
1256 if (!ep->base.bind_addr.port) {
1257 if (sctp_autobind(sk)) {
1258 err = -EAGAIN;
1259 goto out_unlock;
1263 scope = sctp_scope(&to);
1264 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1265 if (!new_asoc) {
1266 err = -ENOMEM;
1267 goto out_unlock;
1269 asoc = new_asoc;
1271 /* If the SCTP_INIT ancillary data is specified, set all
1272 * the association init values accordingly.
1274 if (sinit) {
1275 if (sinit->sinit_num_ostreams) {
1276 asoc->c.sinit_num_ostreams =
1277 sinit->sinit_num_ostreams;
1279 if (sinit->sinit_max_instreams) {
1280 asoc->c.sinit_max_instreams =
1281 sinit->sinit_max_instreams;
1283 if (sinit->sinit_max_attempts) {
1284 asoc->max_init_attempts
1285 = sinit->sinit_max_attempts;
1287 if (sinit->sinit_max_init_timeo) {
1288 asoc->max_init_timeo =
1289 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1293 /* Prime the peer's transport structures. */
1294 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1295 if (!transport) {
1296 err = -ENOMEM;
1297 goto out_free;
1299 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1300 if (err < 0) {
1301 err = -ENOMEM;
1302 goto out_free;
1306 /* ASSERT: we have a valid association at this point. */
1307 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1309 if (!sinfo) {
1310 /* If the user didn't specify SNDRCVINFO, make up one with
1311 * some defaults.
1313 default_sinfo.sinfo_stream = asoc->default_stream;
1314 default_sinfo.sinfo_flags = asoc->default_flags;
1315 default_sinfo.sinfo_ppid = asoc->default_ppid;
1316 default_sinfo.sinfo_context = asoc->default_context;
1317 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1318 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1319 sinfo = &default_sinfo;
1322 /* API 7.1.7, the sndbuf size per association bounds the
1323 * maximum size of data that can be sent in a single send call.
1325 if (msg_len > sk->sk_sndbuf) {
1326 err = -EMSGSIZE;
1327 goto out_free;
1330 /* If fragmentation is disabled and the message length exceeds the
1331 * association fragmentation point, return EMSGSIZE. The I-D
1332 * does not specify what this error is, but this looks like
1333 * a great fit.
1335 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1336 err = -EMSGSIZE;
1337 goto out_free;
1340 if (sinfo) {
1341 /* Check for invalid stream. */
1342 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1343 err = -EINVAL;
1344 goto out_free;
1348 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1349 if (!sctp_wspace(asoc)) {
1350 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1351 if (err)
1352 goto out_free;
1355 /* If an address is passed with the sendto/sendmsg call, it is used
1356 * to override the primary destination address in the TCP model, or
1357 * when MSG_ADDR_OVER flag is set in the UDP model.
1359 if ((sctp_style(sk, TCP) && msg_name) ||
1360 (sinfo_flags & MSG_ADDR_OVER)) {
1361 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1362 if (!chunk_tp) {
1363 err = -EINVAL;
1364 goto out_free;
1366 } else
1367 chunk_tp = NULL;
1369 /* Auto-connect, if we aren't connected already. */
1370 if (sctp_state(asoc, CLOSED)) {
1371 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1372 if (err < 0)
1373 goto out_free;
1374 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1377 /* Break the message into multiple chunks of maximum size. */
1378 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1379 if (!datamsg) {
1380 err = -ENOMEM;
1381 goto out_free;
1384 /* Now send the (possibly) fragmented message. */
1385 list_for_each(pos, &datamsg->chunks) {
1386 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1387 sctp_datamsg_track(chunk);
1389 /* Do accounting for the write space. */
1390 sctp_set_owner_w(chunk);
1392 chunk->transport = chunk_tp;
1394 /* Send it to the lower layers. Note: all chunks
1395 * must either fail or succeed. The lower layer
1396 * works that way today. Keep it that way or this
1397 * breaks.
1399 err = sctp_primitive_SEND(asoc, chunk);
1400 /* Did the lower layer accept the chunk? */
1401 if (err)
1402 sctp_chunk_free(chunk);
1403 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1406 sctp_datamsg_free(datamsg);
1407 if (err)
1408 goto out_free;
1409 else
1410 err = msg_len;
1412 /* If we are already past ASSOCIATE, the lower
1413 * layers are responsible for association cleanup.
1415 goto out_unlock;
1417 out_free:
1418 if (new_asoc)
1419 sctp_association_free(asoc);
1420 out_unlock:
1421 sctp_release_sock(sk);
1423 out_nounlock:
1424 return sctp_error(sk, msg_flags, err);
1426 #if 0
1427 do_sock_err:
1428 if (msg_len)
1429 err = msg_len;
1430 else
1431 err = sock_error(sk);
1432 goto out;
1434 do_interrupted:
1435 if (msg_len)
1436 err = msg_len;
1437 goto out;
1438 #endif /* 0 */
1441 /* This is an extended version of skb_pull() that removes the data from the
1442 * start of a skb even when data is spread across the list of skb's in the
1443 * frag_list. len specifies the total amount of data that needs to be removed.
1444 * when 'len' bytes could be removed from the skb, it returns 0.
1445 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1446 * could not be removed.
1448 static int sctp_skb_pull(struct sk_buff *skb, int len)
1450 struct sk_buff *list;
1451 int skb_len = skb_headlen(skb);
1452 int rlen;
1454 if (len <= skb_len) {
1455 __skb_pull(skb, len);
1456 return 0;
1458 len -= skb_len;
1459 __skb_pull(skb, skb_len);
1461 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1462 rlen = sctp_skb_pull(list, len);
1463 skb->len -= (len-rlen);
1464 skb->data_len -= (len-rlen);
1466 if (!rlen)
1467 return 0;
1469 len = rlen;
1472 return len;
1475 /* API 3.1.3 recvmsg() - UDP Style Syntax
1477 * ssize_t recvmsg(int socket, struct msghdr *message,
1478 * int flags);
1480 * socket - the socket descriptor of the endpoint.
1481 * message - pointer to the msghdr structure which contains a single
1482 * user message and possibly some ancillary data.
1484 * See Section 5 for complete description of the data
1485 * structures.
1487 * flags - flags sent or received with the user message, see Section
1488 * 5 for complete description of the flags.
1490 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1492 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1493 struct msghdr *msg, size_t len, int noblock,
1494 int flags, int *addr_len)
1496 struct sctp_ulpevent *event = NULL;
1497 struct sctp_opt *sp = sctp_sk(sk);
1498 struct sk_buff *skb;
1499 int copied;
1500 int err = 0;
1501 int skb_len;
1503 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1504 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1505 "len", len, "knoblauch", noblock,
1506 "flags", flags, "addr_len", addr_len);
1508 sctp_lock_sock(sk);
1510 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1511 err = -ENOTCONN;
1512 goto out;
1515 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1516 if (!skb)
1517 goto out;
1519 /* Get the total length of the skb including any skb's in the
1520 * frag_list.
1522 skb_len = skb->len;
1524 copied = skb_len;
1525 if (copied > len)
1526 copied = len;
1528 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1530 event = sctp_skb2event(skb);
1532 if (err)
1533 goto out_free;
1535 sock_recv_timestamp(msg, sk, skb);
1536 if (sctp_ulpevent_is_notification(event)) {
1537 msg->msg_flags |= MSG_NOTIFICATION;
1538 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1539 } else {
1540 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1543 /* Check if we allow SCTP_SNDRCVINFO. */
1544 if (sp->subscribe.sctp_data_io_event)
1545 sctp_ulpevent_read_sndrcvinfo(event, msg);
1546 #if 0
1547 /* FIXME: we should be calling IP/IPv6 layers. */
1548 if (sk->sk_protinfo.af_inet.cmsg_flags)
1549 ip_cmsg_recv(msg, skb);
1550 #endif
1552 err = copied;
1554 /* If skb's length exceeds the user's buffer, update the skb and
1555 * push it back to the receive_queue so that the next call to
1556 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1558 if (skb_len > copied) {
1559 msg->msg_flags &= ~MSG_EOR;
1560 if (flags & MSG_PEEK)
1561 goto out_free;
1562 sctp_skb_pull(skb, copied);
1563 skb_queue_head(&sk->sk_receive_queue, skb);
1565 /* When only partial message is copied to the user, increase
1566 * rwnd by that amount. If all the data in the skb is read,
1567 * rwnd is updated when the event is freed.
1569 sctp_assoc_rwnd_increase(event->asoc, copied);
1570 goto out;
1571 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1572 (event->msg_flags & MSG_EOR))
1573 msg->msg_flags |= MSG_EOR;
1574 else
1575 msg->msg_flags &= ~MSG_EOR;
1577 out_free:
1578 if (flags & MSG_PEEK) {
1579 /* Release the skb reference acquired after peeking the skb in
1580 * sctp_skb_recv_datagram().
1582 kfree_skb(skb);
1583 } else {
1584 /* Free the event which includes releasing the reference to
1585 * the owner of the skb, freeing the skb and updating the
1586 * rwnd.
1588 sctp_ulpevent_free(event);
1590 out:
1591 sctp_release_sock(sk);
1592 return err;
1595 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1597 * This option is a on/off flag. If enabled no SCTP message
1598 * fragmentation will be performed. Instead if a message being sent
1599 * exceeds the current PMTU size, the message will NOT be sent and
1600 * instead a error will be indicated to the user.
1602 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1603 char __user *optval, int optlen)
1605 int val;
1607 if (optlen < sizeof(int))
1608 return -EINVAL;
1610 if (get_user(val, (int __user *)optval))
1611 return -EFAULT;
1613 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1615 return 0;
1618 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1619 int optlen)
1621 if (optlen != sizeof(struct sctp_event_subscribe))
1622 return -EINVAL;
1623 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1624 return -EFAULT;
1625 return 0;
1628 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1630 * This socket option is applicable to the UDP-style socket only. When
1631 * set it will cause associations that are idle for more than the
1632 * specified number of seconds to automatically close. An association
1633 * being idle is defined an association that has NOT sent or received
1634 * user data. The special value of '0' indicates that no automatic
1635 * close of any associations should be performed. The option expects an
1636 * integer defining the number of seconds of idle time before an
1637 * association is closed.
1639 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1640 int optlen)
1642 struct sctp_opt *sp = sctp_sk(sk);
1644 /* Applicable to UDP-style socket only */
1645 if (sctp_style(sk, TCP))
1646 return -EOPNOTSUPP;
1647 if (optlen != sizeof(int))
1648 return -EINVAL;
1649 if (copy_from_user(&sp->autoclose, optval, optlen))
1650 return -EFAULT;
1652 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1653 return 0;
1656 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1658 * Applications can enable or disable heartbeats for any peer address of
1659 * an association, modify an address's heartbeat interval, force a
1660 * heartbeat to be sent immediately, and adjust the address's maximum
1661 * number of retransmissions sent before an address is considered
1662 * unreachable. The following structure is used to access and modify an
1663 * address's parameters:
1665 * struct sctp_paddrparams {
1666 * sctp_assoc_t spp_assoc_id;
1667 * struct sockaddr_storage spp_address;
1668 * uint32_t spp_hbinterval;
1669 * uint16_t spp_pathmaxrxt;
1670 * };
1672 * spp_assoc_id - (UDP style socket) This is filled in the application,
1673 * and identifies the association for this query.
1674 * spp_address - This specifies which address is of interest.
1675 * spp_hbinterval - This contains the value of the heartbeat interval,
1676 * in milliseconds. A value of 0, when modifying the
1677 * parameter, specifies that the heartbeat on this
1678 * address should be disabled. A value of UINT32_MAX
1679 * (4294967295), when modifying the parameter,
1680 * specifies that a heartbeat should be sent
1681 * immediately to the peer address, and the current
1682 * interval should remain unchanged.
1683 * spp_pathmaxrxt - This contains the maximum number of
1684 * retransmissions before this address shall be
1685 * considered unreachable.
1687 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1688 char __user *optval, int optlen)
1690 struct sctp_paddrparams params;
1691 struct sctp_transport *trans;
1692 int error;
1694 if (optlen != sizeof(struct sctp_paddrparams))
1695 return -EINVAL;
1696 if (copy_from_user(&params, optval, optlen))
1697 return -EFAULT;
1700 * API 7. Socket Options (setting the default value for the endpoint)
1701 * All options that support specific settings on an association by
1702 * filling in either an association id variable or a sockaddr_storage
1703 * SHOULD also support setting of the same value for the entire endpoint
1704 * (i.e. future associations). To accomplish this the following logic is
1705 * used when setting one of these options:
1707 * c) If neither the sockaddr_storage or association identification is
1708 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1709 * the association identification is 0, the settings are a default
1710 * and to be applied to the endpoint (all future associations).
1713 /* update default value for endpoint (all future associations) */
1714 if (!params.spp_assoc_id &&
1715 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
1716 if (params.spp_hbinterval)
1717 sctp_sk(sk)->paddrparam.spp_hbinterval =
1718 params.spp_hbinterval;
1719 if (sctp_max_retrans_path)
1720 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
1721 params.spp_pathmaxrxt;
1722 return 0;
1725 trans = sctp_addr_id2transport(sk, &params.spp_address,
1726 params.spp_assoc_id);
1727 if (!trans)
1728 return -EINVAL;
1730 /* Applications can enable or disable heartbeats for any peer address
1731 * of an association, modify an address's heartbeat interval, force a
1732 * heartbeat to be sent immediately, and adjust the address's maximum
1733 * number of retransmissions sent before an address is considered
1734 * unreachable.
1736 * The value of the heartbeat interval, in milliseconds. A value of
1737 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1738 * that a heartbeat should be sent immediately to the peer address,
1739 * and the current interval should remain unchanged.
1741 if (0xffffffff == params.spp_hbinterval) {
1742 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1743 if (error)
1744 return error;
1745 } else {
1746 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1747 * when modifying the parameter, specifies that the heartbeat on this
1748 * address should be disabled.
1750 if (params.spp_hbinterval) {
1751 trans->hb_allowed = 1;
1752 trans->hb_interval =
1753 msecs_to_jiffies(params.spp_hbinterval);
1754 } else
1755 trans->hb_allowed = 0;
1758 /* spp_pathmaxrxt contains the maximum number of retransmissions
1759 * before this address shall be considered unreachable.
1761 trans->error_threshold = params.spp_pathmaxrxt;
1763 return 0;
1766 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1768 * Applications can specify protocol parameters for the default association
1769 * initialization. The option name argument to setsockopt() and getsockopt()
1770 * is SCTP_INITMSG.
1772 * Setting initialization parameters is effective only on an unconnected
1773 * socket (for UDP-style sockets only future associations are effected
1774 * by the change). With TCP-style sockets, this option is inherited by
1775 * sockets derived from a listener socket.
1777 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1779 struct sctp_initmsg sinit;
1780 struct sctp_opt *sp = sctp_sk(sk);
1782 if (optlen != sizeof(struct sctp_initmsg))
1783 return -EINVAL;
1784 if (copy_from_user(&sinit, optval, optlen))
1785 return -EFAULT;
1787 if (sinit.sinit_num_ostreams)
1788 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1789 if (sinit.sinit_max_instreams)
1790 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1791 if (sinit.sinit_max_attempts)
1792 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1793 if (sinit.sinit_max_init_timeo)
1794 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1796 return 0;
1800 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1802 * Applications that wish to use the sendto() system call may wish to
1803 * specify a default set of parameters that would normally be supplied
1804 * through the inclusion of ancillary data. This socket option allows
1805 * such an application to set the default sctp_sndrcvinfo structure.
1806 * The application that wishes to use this socket option simply passes
1807 * in to this call the sctp_sndrcvinfo structure defined in Section
1808 * 5.2.2) The input parameters accepted by this call include
1809 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1810 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1811 * to this call if the caller is using the UDP model.
1813 static int sctp_setsockopt_default_send_param(struct sock *sk,
1814 char __user *optval, int optlen)
1816 struct sctp_sndrcvinfo info;
1817 struct sctp_association *asoc;
1818 struct sctp_opt *sp = sctp_sk(sk);
1820 if (optlen != sizeof(struct sctp_sndrcvinfo))
1821 return -EINVAL;
1822 if (copy_from_user(&info, optval, optlen))
1823 return -EFAULT;
1825 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1826 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1827 return -EINVAL;
1829 if (asoc) {
1830 asoc->default_stream = info.sinfo_stream;
1831 asoc->default_flags = info.sinfo_flags;
1832 asoc->default_ppid = info.sinfo_ppid;
1833 asoc->default_context = info.sinfo_context;
1834 asoc->default_timetolive = info.sinfo_timetolive;
1835 } else {
1836 sp->default_stream = info.sinfo_stream;
1837 sp->default_flags = info.sinfo_flags;
1838 sp->default_ppid = info.sinfo_ppid;
1839 sp->default_context = info.sinfo_context;
1840 sp->default_timetolive = info.sinfo_timetolive;
1843 return 0;
1846 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1848 * Requests that the local SCTP stack use the enclosed peer address as
1849 * the association primary. The enclosed address must be one of the
1850 * association peer's addresses.
1852 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1853 int optlen)
1855 struct sctp_prim prim;
1856 struct sctp_transport *trans;
1858 if (optlen != sizeof(struct sctp_prim))
1859 return -EINVAL;
1861 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1862 return -EFAULT;
1864 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1865 if (!trans)
1866 return -EINVAL;
1868 sctp_assoc_set_primary(trans->asoc, trans);
1870 return 0;
1874 * 7.1.5 SCTP_NODELAY
1876 * Turn on/off any Nagle-like algorithm. This means that packets are
1877 * generally sent as soon as possible and no unnecessary delays are
1878 * introduced, at the cost of more packets in the network. Expects an
1879 * integer boolean flag.
1881 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1882 int optlen)
1884 int val;
1886 if (optlen < sizeof(int))
1887 return -EINVAL;
1888 if (get_user(val, (int __user *)optval))
1889 return -EFAULT;
1891 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1892 return 0;
1897 * 7.1.1 SCTP_RTOINFO
1899 * The protocol parameters used to initialize and bound retransmission
1900 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1901 * and modify these parameters.
1902 * All parameters are time values, in milliseconds. A value of 0, when
1903 * modifying the parameters, indicates that the current value should not
1904 * be changed.
1907 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1908 struct sctp_rtoinfo rtoinfo;
1909 struct sctp_association *asoc;
1911 if (optlen != sizeof (struct sctp_rtoinfo))
1912 return -EINVAL;
1914 if (copy_from_user(&rtoinfo, optval, optlen))
1915 return -EFAULT;
1917 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1919 /* Set the values to the specific association */
1920 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1921 return -EINVAL;
1923 if (asoc) {
1924 if (rtoinfo.srto_initial != 0)
1925 asoc->rto_initial =
1926 msecs_to_jiffies(rtoinfo.srto_initial);
1927 if (rtoinfo.srto_max != 0)
1928 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1929 if (rtoinfo.srto_min != 0)
1930 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1931 } else {
1932 /* If there is no association or the association-id = 0
1933 * set the values to the endpoint.
1935 struct sctp_opt *sp = sctp_sk(sk);
1937 if (rtoinfo.srto_initial != 0)
1938 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1939 if (rtoinfo.srto_max != 0)
1940 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1941 if (rtoinfo.srto_min != 0)
1942 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1945 return 0;
1950 * 7.1.2 SCTP_ASSOCINFO
1952 * This option is used to tune the the maximum retransmission attempts
1953 * of the association.
1954 * Returns an error if the new association retransmission value is
1955 * greater than the sum of the retransmission value of the peer.
1956 * See [SCTP] for more information.
1959 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1962 struct sctp_assocparams assocparams;
1963 struct sctp_association *asoc;
1965 if (optlen != sizeof(struct sctp_assocparams))
1966 return -EINVAL;
1967 if (copy_from_user(&assocparams, optval, optlen))
1968 return -EFAULT;
1970 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1972 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1973 return -EINVAL;
1975 /* Set the values to the specific association */
1976 if (asoc) {
1977 if (assocparams.sasoc_asocmaxrxt != 0)
1978 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1979 if (assocparams.sasoc_cookie_life != 0) {
1980 asoc->cookie_life.tv_sec =
1981 assocparams.sasoc_cookie_life / 1000;
1982 asoc->cookie_life.tv_usec =
1983 (assocparams.sasoc_cookie_life % 1000)
1984 * 1000;
1986 } else {
1987 /* Set the values to the endpoint */
1988 struct sctp_opt *sp = sctp_sk(sk);
1990 if (assocparams.sasoc_asocmaxrxt != 0)
1991 sp->assocparams.sasoc_asocmaxrxt =
1992 assocparams.sasoc_asocmaxrxt;
1993 if (assocparams.sasoc_cookie_life != 0)
1994 sp->assocparams.sasoc_cookie_life =
1995 assocparams.sasoc_cookie_life;
1997 return 0;
2001 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2003 * This socket option is a boolean flag which turns on or off mapped V4
2004 * addresses. If this option is turned on and the socket is type
2005 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2006 * If this option is turned off, then no mapping will be done of V4
2007 * addresses and a user will receive both PF_INET6 and PF_INET type
2008 * addresses on the socket.
2010 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2012 int val;
2013 struct sctp_opt *sp = sctp_sk(sk);
2015 if (optlen < sizeof(int))
2016 return -EINVAL;
2017 if (get_user(val, (int __user *)optval))
2018 return -EFAULT;
2019 if (val)
2020 sp->v4mapped = 1;
2021 else
2022 sp->v4mapped = 0;
2024 return 0;
2028 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2030 * This socket option specifies the maximum size to put in any outgoing
2031 * SCTP chunk. If a message is larger than this size it will be
2032 * fragmented by SCTP into the specified size. Note that the underlying
2033 * SCTP implementation may fragment into smaller sized chunks when the
2034 * PMTU of the underlying association is smaller than the value set by
2035 * the user.
2037 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2039 struct sctp_association *asoc;
2040 struct list_head *pos;
2041 struct sctp_opt *sp = sctp_sk(sk);
2042 int val;
2044 if (optlen < sizeof(int))
2045 return -EINVAL;
2046 if (get_user(val, (int __user *)optval))
2047 return -EFAULT;
2048 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
2049 return -EINVAL;
2050 sp->user_frag = val;
2052 if (val) {
2053 /* Update the frag_point of the existing associations. */
2054 list_for_each(pos, &(sp->ep->asocs)) {
2055 asoc = list_entry(pos, struct sctp_association, asocs);
2056 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2060 return 0;
2065 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2067 * Requests that the peer mark the enclosed address as the association
2068 * primary. The enclosed address must be one of the association's
2069 * locally bound addresses. The following structure is used to make a
2070 * set primary request:
2072 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2073 int optlen)
2075 struct sctp_opt *sp;
2076 struct sctp_endpoint *ep;
2077 struct sctp_association *asoc = NULL;
2078 struct sctp_setpeerprim prim;
2079 struct sctp_chunk *chunk;
2080 int err;
2082 sp = sctp_sk(sk);
2083 ep = sp->ep;
2085 if (!sctp_addip_enable)
2086 return -EPERM;
2088 if (optlen != sizeof(struct sctp_setpeerprim))
2089 return -EINVAL;
2091 if (copy_from_user(&prim, optval, optlen))
2092 return -EFAULT;
2094 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2095 if (!asoc)
2096 return -EINVAL;
2098 if (!asoc->peer.asconf_capable)
2099 return -EPERM;
2101 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2102 return -EPERM;
2104 if (!sctp_state(asoc, ESTABLISHED))
2105 return -ENOTCONN;
2107 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2108 return -EADDRNOTAVAIL;
2110 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2111 chunk = sctp_make_asconf_set_prim(asoc,
2112 (union sctp_addr *)&prim.sspp_addr);
2113 if (!chunk)
2114 return -ENOMEM;
2116 err = sctp_send_asconf(asoc, chunk);
2118 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2120 return err;
2124 /* API 6.2 setsockopt(), getsockopt()
2126 * Applications use setsockopt() and getsockopt() to set or retrieve
2127 * socket options. Socket options are used to change the default
2128 * behavior of sockets calls. They are described in Section 7.
2130 * The syntax is:
2132 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2133 * int __user *optlen);
2134 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2135 * int optlen);
2137 * sd - the socket descript.
2138 * level - set to IPPROTO_SCTP for all SCTP options.
2139 * optname - the option name.
2140 * optval - the buffer to store the value of the option.
2141 * optlen - the size of the buffer.
2143 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2144 char __user *optval, int optlen)
2146 int retval = 0;
2148 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2149 sk, optname);
2151 /* I can hardly begin to describe how wrong this is. This is
2152 * so broken as to be worse than useless. The API draft
2153 * REALLY is NOT helpful here... I am not convinced that the
2154 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2155 * are at all well-founded.
2157 if (level != SOL_SCTP) {
2158 struct sctp_af *af = sctp_sk(sk)->pf->af;
2159 retval = af->setsockopt(sk, level, optname, optval, optlen);
2160 goto out_nounlock;
2163 sctp_lock_sock(sk);
2165 switch (optname) {
2166 case SCTP_SOCKOPT_BINDX_ADD:
2167 /* 'optlen' is the size of the addresses buffer. */
2168 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2169 optlen, SCTP_BINDX_ADD_ADDR);
2170 break;
2172 case SCTP_SOCKOPT_BINDX_REM:
2173 /* 'optlen' is the size of the addresses buffer. */
2174 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2175 optlen, SCTP_BINDX_REM_ADDR);
2176 break;
2178 case SCTP_DISABLE_FRAGMENTS:
2179 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2180 break;
2182 case SCTP_EVENTS:
2183 retval = sctp_setsockopt_events(sk, optval, optlen);
2184 break;
2186 case SCTP_AUTOCLOSE:
2187 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2188 break;
2190 case SCTP_PEER_ADDR_PARAMS:
2191 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2192 break;
2194 case SCTP_INITMSG:
2195 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2196 break;
2197 case SCTP_DEFAULT_SEND_PARAM:
2198 retval = sctp_setsockopt_default_send_param(sk, optval,
2199 optlen);
2200 break;
2201 case SCTP_PRIMARY_ADDR:
2202 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2203 break;
2204 case SCTP_SET_PEER_PRIMARY_ADDR:
2205 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2206 break;
2207 case SCTP_NODELAY:
2208 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2209 break;
2210 case SCTP_RTOINFO:
2211 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2212 break;
2213 case SCTP_ASSOCINFO:
2214 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2215 break;
2216 case SCTP_I_WANT_MAPPED_V4_ADDR:
2217 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2218 break;
2219 case SCTP_MAXSEG:
2220 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2221 break;
2222 default:
2223 retval = -ENOPROTOOPT;
2224 break;
2227 sctp_release_sock(sk);
2229 out_nounlock:
2230 return retval;
2233 /* API 3.1.6 connect() - UDP Style Syntax
2235 * An application may use the connect() call in the UDP model to initiate an
2236 * association without sending data.
2238 * The syntax is:
2240 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2242 * sd: the socket descriptor to have a new association added to.
2244 * nam: the address structure (either struct sockaddr_in or struct
2245 * sockaddr_in6 defined in RFC2553 [7]).
2247 * len: the size of the address.
2249 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2250 int addr_len)
2252 struct sctp_opt *sp;
2253 struct sctp_endpoint *ep;
2254 struct sctp_association *asoc;
2255 struct sctp_transport *transport;
2256 union sctp_addr to;
2257 struct sctp_af *af;
2258 sctp_scope_t scope;
2259 long timeo;
2260 int err = 0;
2262 sctp_lock_sock(sk);
2264 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2265 __FUNCTION__, sk, uaddr, addr_len);
2267 sp = sctp_sk(sk);
2268 ep = sp->ep;
2270 /* connect() cannot be done on a socket that is already in ESTABLISHED
2271 * state - UDP-style peeled off socket or a TCP-style socket that
2272 * is already connected.
2273 * It cannot be done even on a TCP-style listening socket.
2275 if (sctp_sstate(sk, ESTABLISHED) ||
2276 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2277 err = -EISCONN;
2278 goto out_unlock;
2281 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2282 if (err)
2283 goto out_unlock;
2285 if (addr_len > sizeof(to))
2286 addr_len = sizeof(to);
2287 memcpy(&to, uaddr, addr_len);
2288 to.v4.sin_port = ntohs(to.v4.sin_port);
2290 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2291 if (asoc) {
2292 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2293 err = -EISCONN;
2294 else
2295 err = -EALREADY;
2296 goto out_unlock;
2299 /* If we could not find a matching association on the endpoint,
2300 * make sure that there is no peeled-off association matching the
2301 * peer address even on another socket.
2303 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2304 err = -EADDRNOTAVAIL;
2305 goto out_unlock;
2308 /* If a bind() or sctp_bindx() is not called prior to a connect()
2309 * call, the system picks an ephemeral port and will choose an address
2310 * set equivalent to binding with a wildcard address.
2312 if (!ep->base.bind_addr.port) {
2313 if (sctp_autobind(sk)) {
2314 err = -EAGAIN;
2315 goto out_unlock;
2319 scope = sctp_scope(&to);
2320 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2321 if (!asoc) {
2322 err = -ENOMEM;
2323 goto out_unlock;
2326 /* Prime the peer's transport structures. */
2327 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2328 if (!transport) {
2329 sctp_association_free(asoc);
2330 goto out_unlock;
2332 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2333 if (err < 0) {
2334 sctp_association_free(asoc);
2335 goto out_unlock;
2338 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2339 if (err < 0) {
2340 sctp_association_free(asoc);
2341 goto out_unlock;
2344 /* Initialize sk's dport and daddr for getpeername() */
2345 inet_sk(sk)->dport = htons(asoc->peer.port);
2346 af = sctp_get_af_specific(to.sa.sa_family);
2347 af->to_sk_daddr(&to, sk);
2349 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2350 err = sctp_wait_for_connect(asoc, &timeo);
2352 out_unlock:
2353 sctp_release_sock(sk);
2355 return err;
2358 /* FIXME: Write comments. */
2359 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2361 return -EOPNOTSUPP; /* STUB */
2364 /* 4.1.4 accept() - TCP Style Syntax
2366 * Applications use accept() call to remove an established SCTP
2367 * association from the accept queue of the endpoint. A new socket
2368 * descriptor will be returned from accept() to represent the newly
2369 * formed association.
2371 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2373 struct sctp_opt *sp;
2374 struct sctp_endpoint *ep;
2375 struct sock *newsk = NULL;
2376 struct sctp_association *asoc;
2377 long timeo;
2378 int error = 0;
2380 sctp_lock_sock(sk);
2382 sp = sctp_sk(sk);
2383 ep = sp->ep;
2385 if (!sctp_style(sk, TCP)) {
2386 error = -EOPNOTSUPP;
2387 goto out;
2390 if (!sctp_sstate(sk, LISTENING)) {
2391 error = -EINVAL;
2392 goto out;
2395 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2397 error = sctp_wait_for_accept(sk, timeo);
2398 if (error)
2399 goto out;
2401 /* We treat the list of associations on the endpoint as the accept
2402 * queue and pick the first association on the list.
2404 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2406 newsk = sp->pf->create_accept_sk(sk, asoc);
2407 if (!newsk) {
2408 error = -ENOMEM;
2409 goto out;
2412 /* Populate the fields of the newsk from the oldsk and migrate the
2413 * asoc to the newsk.
2415 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2417 out:
2418 sctp_release_sock(sk);
2419 *err = error;
2420 return newsk;
2423 /* The SCTP ioctl handler. */
2424 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2426 return -ENOIOCTLCMD;
2429 /* This is the function which gets called during socket creation to
2430 * initialized the SCTP-specific portion of the sock.
2431 * The sock structure should already be zero-filled memory.
2433 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2435 struct sctp_endpoint *ep;
2436 struct sctp_opt *sp;
2438 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2440 sp = sctp_sk(sk);
2442 /* Initialize the SCTP per socket area. */
2443 switch (sk->sk_type) {
2444 case SOCK_SEQPACKET:
2445 sp->type = SCTP_SOCKET_UDP;
2446 break;
2447 case SOCK_STREAM:
2448 sp->type = SCTP_SOCKET_TCP;
2449 break;
2450 default:
2451 return -ESOCKTNOSUPPORT;
2454 /* Initialize default send parameters. These parameters can be
2455 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2457 sp->default_stream = 0;
2458 sp->default_ppid = 0;
2459 sp->default_flags = 0;
2460 sp->default_context = 0;
2461 sp->default_timetolive = 0;
2463 /* Initialize default setup parameters. These parameters
2464 * can be modified with the SCTP_INITMSG socket option or
2465 * overridden by the SCTP_INIT CMSG.
2467 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2468 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2469 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2470 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2472 /* Initialize default RTO related parameters. These parameters can
2473 * be modified for with the SCTP_RTOINFO socket option.
2475 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2476 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2477 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2479 /* Initialize default association related parameters. These parameters
2480 * can be modified with the SCTP_ASSOCINFO socket option.
2482 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2483 sp->assocparams.sasoc_number_peer_destinations = 0;
2484 sp->assocparams.sasoc_peer_rwnd = 0;
2485 sp->assocparams.sasoc_local_rwnd = 0;
2486 sp->assocparams.sasoc_cookie_life =
2487 jiffies_to_msecs(sctp_valid_cookie_life);
2489 /* Initialize default event subscriptions. By default, all the
2490 * options are off.
2492 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2494 /* Default Peer Address Parameters. These defaults can
2495 * be modified via SCTP_PEER_ADDR_PARAMS
2497 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2498 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2500 /* If enabled no SCTP message fragmentation will be performed.
2501 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2503 sp->disable_fragments = 0;
2505 /* Turn on/off any Nagle-like algorithm. */
2506 sp->nodelay = 1;
2508 /* Enable by default. */
2509 sp->v4mapped = 1;
2511 /* Auto-close idle associations after the configured
2512 * number of seconds. A value of 0 disables this
2513 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2514 * for UDP-style sockets only.
2516 sp->autoclose = 0;
2518 /* User specified fragmentation limit. */
2519 sp->user_frag = 0;
2521 sp->pf = sctp_get_pf_specific(sk->sk_family);
2523 /* Control variables for partial data delivery. */
2524 sp->pd_mode = 0;
2525 skb_queue_head_init(&sp->pd_lobby);
2527 /* Create a per socket endpoint structure. Even if we
2528 * change the data structure relationships, this may still
2529 * be useful for storing pre-connect address information.
2531 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2532 if (!ep)
2533 return -ENOMEM;
2535 sp->ep = ep;
2536 sp->hmac = NULL;
2538 SCTP_DBG_OBJCNT_INC(sock);
2539 return 0;
2542 /* Cleanup any SCTP per socket resources. */
2543 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2545 struct sctp_endpoint *ep;
2547 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2549 /* Release our hold on the endpoint. */
2550 ep = sctp_sk(sk)->ep;
2551 sctp_endpoint_free(ep);
2553 return 0;
2556 /* API 4.1.7 shutdown() - TCP Style Syntax
2557 * int shutdown(int socket, int how);
2559 * sd - the socket descriptor of the association to be closed.
2560 * how - Specifies the type of shutdown. The values are
2561 * as follows:
2562 * SHUT_RD
2563 * Disables further receive operations. No SCTP
2564 * protocol action is taken.
2565 * SHUT_WR
2566 * Disables further send operations, and initiates
2567 * the SCTP shutdown sequence.
2568 * SHUT_RDWR
2569 * Disables further send and receive operations
2570 * and initiates the SCTP shutdown sequence.
2572 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2574 struct sctp_endpoint *ep;
2575 struct sctp_association *asoc;
2577 if (!sctp_style(sk, TCP))
2578 return;
2580 if (how & SEND_SHUTDOWN) {
2581 ep = sctp_sk(sk)->ep;
2582 if (!list_empty(&ep->asocs)) {
2583 asoc = list_entry(ep->asocs.next,
2584 struct sctp_association, asocs);
2585 sctp_primitive_SHUTDOWN(asoc, NULL);
2590 /* 7.2.1 Association Status (SCTP_STATUS)
2592 * Applications can retrieve current status information about an
2593 * association, including association state, peer receiver window size,
2594 * number of unacked data chunks, and number of data chunks pending
2595 * receipt. This information is read-only.
2597 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2598 char __user *optval,
2599 int __user *optlen)
2601 struct sctp_status status;
2602 struct sctp_association *asoc = NULL;
2603 struct sctp_transport *transport;
2604 sctp_assoc_t associd;
2605 int retval = 0;
2607 if (len != sizeof(status)) {
2608 retval = -EINVAL;
2609 goto out;
2612 if (copy_from_user(&status, optval, sizeof(status))) {
2613 retval = -EFAULT;
2614 goto out;
2617 associd = status.sstat_assoc_id;
2618 asoc = sctp_id2assoc(sk, associd);
2619 if (!asoc) {
2620 retval = -EINVAL;
2621 goto out;
2624 transport = asoc->peer.primary_path;
2626 status.sstat_assoc_id = sctp_assoc2id(asoc);
2627 status.sstat_state = asoc->state;
2628 status.sstat_rwnd = asoc->peer.rwnd;
2629 status.sstat_unackdata = asoc->unack_data;
2631 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2632 status.sstat_instrms = asoc->c.sinit_max_instreams;
2633 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2634 status.sstat_fragmentation_point = asoc->frag_point;
2635 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2636 memcpy(&status.sstat_primary.spinfo_address,
2637 &(transport->ipaddr), sizeof(union sctp_addr));
2638 /* Map ipv4 address into v4-mapped-on-v6 address. */
2639 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2640 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2641 status.sstat_primary.spinfo_state = transport->active;
2642 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2643 status.sstat_primary.spinfo_srtt = transport->srtt;
2644 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2645 status.sstat_primary.spinfo_mtu = transport->pmtu;
2647 if (put_user(len, optlen)) {
2648 retval = -EFAULT;
2649 goto out;
2652 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %p\n",
2653 len, status.sstat_state, status.sstat_rwnd,
2654 status.sstat_assoc_id);
2656 if (copy_to_user(optval, &status, len)) {
2657 retval = -EFAULT;
2658 goto out;
2661 out:
2662 return (retval);
2666 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2668 * Applications can retrieve information about a specific peer address
2669 * of an association, including its reachability state, congestion
2670 * window, and retransmission timer values. This information is
2671 * read-only.
2673 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2674 char __user *optval,
2675 int __user *optlen)
2677 struct sctp_paddrinfo pinfo;
2678 struct sctp_transport *transport;
2679 int retval = 0;
2681 if (len != sizeof(pinfo)) {
2682 retval = -EINVAL;
2683 goto out;
2686 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2687 retval = -EFAULT;
2688 goto out;
2691 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2692 pinfo.spinfo_assoc_id);
2693 if (!transport)
2694 return -EINVAL;
2696 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2697 pinfo.spinfo_state = transport->active;
2698 pinfo.spinfo_cwnd = transport->cwnd;
2699 pinfo.spinfo_srtt = transport->srtt;
2700 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2701 pinfo.spinfo_mtu = transport->pmtu;
2703 if (put_user(len, optlen)) {
2704 retval = -EFAULT;
2705 goto out;
2708 if (copy_to_user(optval, &pinfo, len)) {
2709 retval = -EFAULT;
2710 goto out;
2713 out:
2714 return (retval);
2717 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2719 * This option is a on/off flag. If enabled no SCTP message
2720 * fragmentation will be performed. Instead if a message being sent
2721 * exceeds the current PMTU size, the message will NOT be sent and
2722 * instead a error will be indicated to the user.
2724 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2725 char __user *optval, int __user *optlen)
2727 int val;
2729 if (len < sizeof(int))
2730 return -EINVAL;
2732 len = sizeof(int);
2733 val = (sctp_sk(sk)->disable_fragments == 1);
2734 if (put_user(len, optlen))
2735 return -EFAULT;
2736 if (copy_to_user(optval, &val, len))
2737 return -EFAULT;
2738 return 0;
2741 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2743 * This socket option is used to specify various notifications and
2744 * ancillary data the user wishes to receive.
2746 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2747 int __user *optlen)
2749 if (len != sizeof(struct sctp_event_subscribe))
2750 return -EINVAL;
2751 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2752 return -EFAULT;
2753 return 0;
2756 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2758 * This socket option is applicable to the UDP-style socket only. When
2759 * set it will cause associations that are idle for more than the
2760 * specified number of seconds to automatically close. An association
2761 * being idle is defined an association that has NOT sent or received
2762 * user data. The special value of '0' indicates that no automatic
2763 * close of any associations should be performed. The option expects an
2764 * integer defining the number of seconds of idle time before an
2765 * association is closed.
2767 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2769 /* Applicable to UDP-style socket only */
2770 if (sctp_style(sk, TCP))
2771 return -EOPNOTSUPP;
2772 if (len != sizeof(int))
2773 return -EINVAL;
2774 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2775 return -EFAULT;
2776 return 0;
2779 /* Helper routine to branch off an association to a new socket. */
2780 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2781 struct socket **sockp)
2783 struct sock *sk = asoc->base.sk;
2784 struct socket *sock;
2785 int err = 0;
2787 /* An association cannot be branched off from an already peeled-off
2788 * socket, nor is this supported for tcp style sockets.
2790 if (!sctp_style(sk, UDP))
2791 return -EINVAL;
2793 /* Create a new socket. */
2794 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2795 if (err < 0)
2796 return err;
2798 /* Populate the fields of the newsk from the oldsk and migrate the
2799 * asoc to the newsk.
2801 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2802 *sockp = sock;
2804 return err;
2807 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2809 sctp_peeloff_arg_t peeloff;
2810 struct socket *newsock;
2811 int retval = 0;
2812 struct sctp_association *asoc;
2814 if (len != sizeof(sctp_peeloff_arg_t))
2815 return -EINVAL;
2816 if (copy_from_user(&peeloff, optval, len))
2817 return -EFAULT;
2819 asoc = sctp_id2assoc(sk, peeloff.associd);
2820 if (!asoc) {
2821 retval = -EINVAL;
2822 goto out;
2825 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2827 retval = sctp_do_peeloff(asoc, &newsock);
2828 if (retval < 0)
2829 goto out;
2831 /* Map the socket to an unused fd that can be returned to the user. */
2832 retval = sock_map_fd(newsock);
2833 if (retval < 0) {
2834 sock_release(newsock);
2835 goto out;
2838 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2839 __FUNCTION__, sk, asoc, newsock->sk, retval);
2841 /* Return the fd mapped to the new socket. */
2842 peeloff.sd = retval;
2843 if (copy_to_user(optval, &peeloff, len))
2844 retval = -EFAULT;
2846 out:
2847 return retval;
2850 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2852 * Applications can enable or disable heartbeats for any peer address of
2853 * an association, modify an address's heartbeat interval, force a
2854 * heartbeat to be sent immediately, and adjust the address's maximum
2855 * number of retransmissions sent before an address is considered
2856 * unreachable. The following structure is used to access and modify an
2857 * address's parameters:
2859 * struct sctp_paddrparams {
2860 * sctp_assoc_t spp_assoc_id;
2861 * struct sockaddr_storage spp_address;
2862 * uint32_t spp_hbinterval;
2863 * uint16_t spp_pathmaxrxt;
2864 * };
2866 * spp_assoc_id - (UDP style socket) This is filled in the application,
2867 * and identifies the association for this query.
2868 * spp_address - This specifies which address is of interest.
2869 * spp_hbinterval - This contains the value of the heartbeat interval,
2870 * in milliseconds. A value of 0, when modifying the
2871 * parameter, specifies that the heartbeat on this
2872 * address should be disabled. A value of UINT32_MAX
2873 * (4294967295), when modifying the parameter,
2874 * specifies that a heartbeat should be sent
2875 * immediately to the peer address, and the current
2876 * interval should remain unchanged.
2877 * spp_pathmaxrxt - This contains the maximum number of
2878 * retransmissions before this address shall be
2879 * considered unreachable.
2881 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2882 char __user *optval, int __user *optlen)
2884 struct sctp_paddrparams params;
2885 struct sctp_transport *trans;
2887 if (len != sizeof(struct sctp_paddrparams))
2888 return -EINVAL;
2889 if (copy_from_user(&params, optval, len))
2890 return -EFAULT;
2892 /* If no association id is specified retrieve the default value
2893 * for the endpoint that will be used for all future associations
2895 if (!params.spp_assoc_id &&
2896 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2897 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
2898 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
2900 goto done;
2903 trans = sctp_addr_id2transport(sk, &params.spp_address,
2904 params.spp_assoc_id);
2905 if (!trans)
2906 return -EINVAL;
2908 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2909 * when modifying the parameter, specifies that the heartbeat on this
2910 * address should be disabled.
2912 if (!trans->hb_allowed)
2913 params.spp_hbinterval = 0;
2914 else
2915 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2917 /* spp_pathmaxrxt contains the maximum number of retransmissions
2918 * before this address shall be considered unreachable.
2920 params.spp_pathmaxrxt = trans->error_threshold;
2922 done:
2923 if (copy_to_user(optval, &params, len))
2924 return -EFAULT;
2926 if (put_user(len, optlen))
2927 return -EFAULT;
2929 return 0;
2932 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2934 * Applications can specify protocol parameters for the default association
2935 * initialization. The option name argument to setsockopt() and getsockopt()
2936 * is SCTP_INITMSG.
2938 * Setting initialization parameters is effective only on an unconnected
2939 * socket (for UDP-style sockets only future associations are effected
2940 * by the change). With TCP-style sockets, this option is inherited by
2941 * sockets derived from a listener socket.
2943 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2945 if (len != sizeof(struct sctp_initmsg))
2946 return -EINVAL;
2947 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2948 return -EFAULT;
2949 return 0;
2952 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2953 char __user *optval, int __user *optlen)
2955 sctp_assoc_t id;
2956 struct sctp_association *asoc;
2957 struct list_head *pos;
2958 int cnt = 0;
2960 if (len != sizeof(sctp_assoc_t))
2961 return -EINVAL;
2963 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2964 return -EFAULT;
2966 /* For UDP-style sockets, id specifies the association to query. */
2967 asoc = sctp_id2assoc(sk, id);
2968 if (!asoc)
2969 return -EINVAL;
2971 list_for_each(pos, &asoc->peer.transport_addr_list) {
2972 cnt ++;
2975 return cnt;
2978 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
2979 char __user *optval, int __user *optlen)
2981 struct sctp_association *asoc;
2982 struct list_head *pos;
2983 int cnt = 0;
2984 struct sctp_getaddrs getaddrs;
2985 struct sctp_transport *from;
2986 void __user *to;
2987 union sctp_addr temp;
2988 struct sctp_opt *sp = sctp_sk(sk);
2989 int addrlen;
2991 if (len != sizeof(struct sctp_getaddrs))
2992 return -EINVAL;
2994 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
2995 return -EFAULT;
2997 if (getaddrs.addr_num <= 0) return -EINVAL;
2999 /* For UDP-style sockets, id specifies the association to query. */
3000 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3001 if (!asoc)
3002 return -EINVAL;
3004 to = (void __user *)getaddrs.addrs;
3005 list_for_each(pos, &asoc->peer.transport_addr_list) {
3006 from = list_entry(pos, struct sctp_transport, transports);
3007 memcpy(&temp, &from->ipaddr, sizeof(temp));
3008 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3009 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3010 temp.v4.sin_port = htons(temp.v4.sin_port);
3011 if (copy_to_user(to, &temp, addrlen))
3012 return -EFAULT;
3013 to += addrlen ;
3014 cnt ++;
3015 if (cnt >= getaddrs.addr_num) break;
3017 getaddrs.addr_num = cnt;
3018 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3019 return -EFAULT;
3021 return 0;
3024 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
3025 char __user *optval,
3026 int __user *optlen)
3028 sctp_assoc_t id;
3029 struct sctp_bind_addr *bp;
3030 struct sctp_association *asoc;
3031 struct list_head *pos;
3032 int cnt = 0;
3034 if (len != sizeof(sctp_assoc_t))
3035 return -EINVAL;
3037 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3038 return -EFAULT;
3041 * For UDP-style sockets, id specifies the association to query.
3042 * If the id field is set to the value '0' then the locally bound
3043 * addresses are returned without regard to any particular
3044 * association.
3046 if (0 == id) {
3047 bp = &sctp_sk(sk)->ep->base.bind_addr;
3048 } else {
3049 asoc = sctp_id2assoc(sk, id);
3050 if (!asoc)
3051 return -EINVAL;
3052 bp = &asoc->base.bind_addr;
3055 list_for_each(pos, &bp->address_list) {
3056 cnt ++;
3059 return cnt;
3062 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3063 char __user *optval, int __user *optlen)
3065 struct sctp_bind_addr *bp;
3066 struct sctp_association *asoc;
3067 struct list_head *pos;
3068 int cnt = 0;
3069 struct sctp_getaddrs getaddrs;
3070 struct sctp_sockaddr_entry *from;
3071 void __user *to;
3072 union sctp_addr temp;
3073 struct sctp_opt *sp = sctp_sk(sk);
3074 int addrlen;
3076 if (len != sizeof(struct sctp_getaddrs))
3077 return -EINVAL;
3079 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3080 return -EFAULT;
3082 if (getaddrs.addr_num <= 0) return -EINVAL;
3084 * For UDP-style sockets, id specifies the association to query.
3085 * If the id field is set to the value '0' then the locally bound
3086 * addresses are returned without regard to any particular
3087 * association.
3089 if (0 == getaddrs.assoc_id) {
3090 bp = &sctp_sk(sk)->ep->base.bind_addr;
3091 } else {
3092 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3093 if (!asoc)
3094 return -EINVAL;
3095 bp = &asoc->base.bind_addr;
3098 to = getaddrs.addrs;
3099 list_for_each(pos, &bp->address_list) {
3100 from = list_entry(pos,
3101 struct sctp_sockaddr_entry,
3102 list);
3103 memcpy(&temp, &from->a, sizeof(temp));
3104 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3105 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3106 temp.v4.sin_port = htons(temp.v4.sin_port);
3107 if (copy_to_user(to, &temp, addrlen))
3108 return -EFAULT;
3109 to += addrlen;
3110 cnt ++;
3111 if (cnt >= getaddrs.addr_num) break;
3113 getaddrs.addr_num = cnt;
3114 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3115 return -EFAULT;
3117 return 0;
3120 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3122 * Requests that the local SCTP stack use the enclosed peer address as
3123 * the association primary. The enclosed address must be one of the
3124 * association peer's addresses.
3126 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3127 char __user *optval, int __user *optlen)
3129 struct sctp_prim prim;
3130 struct sctp_association *asoc;
3131 struct sctp_opt *sp = sctp_sk(sk);
3133 if (len != sizeof(struct sctp_prim))
3134 return -EINVAL;
3136 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3137 return -EFAULT;
3139 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3140 if (!asoc)
3141 return -EINVAL;
3143 if (!asoc->peer.primary_path)
3144 return -ENOTCONN;
3146 asoc->peer.primary_path->ipaddr.v4.sin_port =
3147 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3148 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3149 sizeof(union sctp_addr));
3150 asoc->peer.primary_path->ipaddr.v4.sin_port =
3151 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3153 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3154 (union sctp_addr *)&prim.ssp_addr);
3156 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3157 return -EFAULT;
3159 return 0;
3164 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3166 * Applications that wish to use the sendto() system call may wish to
3167 * specify a default set of parameters that would normally be supplied
3168 * through the inclusion of ancillary data. This socket option allows
3169 * such an application to set the default sctp_sndrcvinfo structure.
3172 * The application that wishes to use this socket option simply passes
3173 * in to this call the sctp_sndrcvinfo structure defined in Section
3174 * 5.2.2) The input parameters accepted by this call include
3175 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3176 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3177 * to this call if the caller is using the UDP model.
3179 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3181 static int sctp_getsockopt_default_send_param(struct sock *sk,
3182 int len, char __user *optval,
3183 int __user *optlen)
3185 struct sctp_sndrcvinfo info;
3186 struct sctp_association *asoc;
3187 struct sctp_opt *sp = sctp_sk(sk);
3189 if (len != sizeof(struct sctp_sndrcvinfo))
3190 return -EINVAL;
3191 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3192 return -EFAULT;
3194 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3195 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3196 return -EINVAL;
3198 if (asoc) {
3199 info.sinfo_stream = asoc->default_stream;
3200 info.sinfo_flags = asoc->default_flags;
3201 info.sinfo_ppid = asoc->default_ppid;
3202 info.sinfo_context = asoc->default_context;
3203 info.sinfo_timetolive = asoc->default_timetolive;
3204 } else {
3205 info.sinfo_stream = sp->default_stream;
3206 info.sinfo_flags = sp->default_flags;
3207 info.sinfo_ppid = sp->default_ppid;
3208 info.sinfo_context = sp->default_context;
3209 info.sinfo_timetolive = sp->default_timetolive;
3212 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3213 return -EFAULT;
3215 return 0;
3220 * 7.1.5 SCTP_NODELAY
3222 * Turn on/off any Nagle-like algorithm. This means that packets are
3223 * generally sent as soon as possible and no unnecessary delays are
3224 * introduced, at the cost of more packets in the network. Expects an
3225 * integer boolean flag.
3228 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3229 char __user *optval, int __user *optlen)
3231 int val;
3233 if (len < sizeof(int))
3234 return -EINVAL;
3236 len = sizeof(int);
3237 val = (sctp_sk(sk)->nodelay == 1);
3238 if (put_user(len, optlen))
3239 return -EFAULT;
3240 if (copy_to_user(optval, &val, len))
3241 return -EFAULT;
3242 return 0;
3247 * 7.1.1 SCTP_RTOINFO
3249 * The protocol parameters used to initialize and bound retransmission
3250 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3251 * and modify these parameters.
3252 * All parameters are time values, in milliseconds. A value of 0, when
3253 * modifying the parameters, indicates that the current value should not
3254 * be changed.
3257 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3258 char __user *optval,
3259 int __user *optlen) {
3260 struct sctp_rtoinfo rtoinfo;
3261 struct sctp_association *asoc;
3263 if (len != sizeof (struct sctp_rtoinfo))
3264 return -EINVAL;
3266 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3267 return -EFAULT;
3269 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3271 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3272 return -EINVAL;
3274 /* Values corresponding to the specific association. */
3275 if (asoc) {
3276 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3277 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3278 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3279 } else {
3280 /* Values corresponding to the endpoint. */
3281 struct sctp_opt *sp = sctp_sk(sk);
3283 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3284 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3285 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3288 if (put_user(len, optlen))
3289 return -EFAULT;
3291 if (copy_to_user(optval, &rtoinfo, len))
3292 return -EFAULT;
3294 return 0;
3299 * 7.1.2 SCTP_ASSOCINFO
3301 * This option is used to tune the the maximum retransmission attempts
3302 * of the association.
3303 * Returns an error if the new association retransmission value is
3304 * greater than the sum of the retransmission value of the peer.
3305 * See [SCTP] for more information.
3308 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3309 char __user *optval,
3310 int __user *optlen)
3313 struct sctp_assocparams assocparams;
3314 struct sctp_association *asoc;
3315 struct list_head *pos;
3316 int cnt = 0;
3318 if (len != sizeof (struct sctp_assocparams))
3319 return -EINVAL;
3321 if (copy_from_user(&assocparams, optval,
3322 sizeof (struct sctp_assocparams)))
3323 return -EFAULT;
3325 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3327 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3328 return -EINVAL;
3330 /* Values correspoinding to the specific association */
3331 if (assocparams.sasoc_assoc_id != 0) {
3332 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3333 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3334 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3335 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3336 * 1000) +
3337 (asoc->cookie_life.tv_usec
3338 / 1000);
3340 list_for_each(pos, &asoc->peer.transport_addr_list) {
3341 cnt ++;
3344 assocparams.sasoc_number_peer_destinations = cnt;
3345 } else {
3346 /* Values corresponding to the endpoint */
3347 struct sctp_opt *sp = sctp_sk(sk);
3349 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3350 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3351 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3352 assocparams.sasoc_cookie_life =
3353 sp->assocparams.sasoc_cookie_life;
3354 assocparams.sasoc_number_peer_destinations =
3355 sp->assocparams.
3356 sasoc_number_peer_destinations;
3359 if (put_user(len, optlen))
3360 return -EFAULT;
3362 if (copy_to_user(optval, &assocparams, len))
3363 return -EFAULT;
3365 return 0;
3369 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3371 * This socket option is a boolean flag which turns on or off mapped V4
3372 * addresses. If this option is turned on and the socket is type
3373 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3374 * If this option is turned off, then no mapping will be done of V4
3375 * addresses and a user will receive both PF_INET6 and PF_INET type
3376 * addresses on the socket.
3378 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3379 char __user *optval, int __user *optlen)
3381 int val;
3382 struct sctp_opt *sp = sctp_sk(sk);
3384 if (len < sizeof(int))
3385 return -EINVAL;
3387 len = sizeof(int);
3388 val = sp->v4mapped;
3389 if (put_user(len, optlen))
3390 return -EFAULT;
3391 if (copy_to_user(optval, &val, len))
3392 return -EFAULT;
3394 return 0;
3398 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3400 * This socket option specifies the maximum size to put in any outgoing
3401 * SCTP chunk. If a message is larger than this size it will be
3402 * fragmented by SCTP into the specified size. Note that the underlying
3403 * SCTP implementation may fragment into smaller sized chunks when the
3404 * PMTU of the underlying association is smaller than the value set by
3405 * the user.
3407 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3408 char __user *optval, int __user *optlen)
3410 int val;
3412 if (len < sizeof(int))
3413 return -EINVAL;
3415 len = sizeof(int);
3417 val = sctp_sk(sk)->user_frag;
3418 if (put_user(len, optlen))
3419 return -EFAULT;
3420 if (copy_to_user(optval, &val, len))
3421 return -EFAULT;
3423 return 0;
3426 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3427 char __user *optval, int __user *optlen)
3429 int retval = 0;
3430 int len;
3432 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3434 /* I can hardly begin to describe how wrong this is. This is
3435 * so broken as to be worse than useless. The API draft
3436 * REALLY is NOT helpful here... I am not convinced that the
3437 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3438 * are at all well-founded.
3440 if (level != SOL_SCTP) {
3441 struct sctp_af *af = sctp_sk(sk)->pf->af;
3443 retval = af->getsockopt(sk, level, optname, optval, optlen);
3444 return retval;
3447 if (get_user(len, optlen))
3448 return -EFAULT;
3450 sctp_lock_sock(sk);
3452 switch (optname) {
3453 case SCTP_STATUS:
3454 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3455 break;
3456 case SCTP_DISABLE_FRAGMENTS:
3457 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3458 optlen);
3459 break;
3460 case SCTP_EVENTS:
3461 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3462 break;
3463 case SCTP_AUTOCLOSE:
3464 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3465 break;
3466 case SCTP_SOCKOPT_PEELOFF:
3467 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3468 break;
3469 case SCTP_PEER_ADDR_PARAMS:
3470 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3471 optlen);
3472 break;
3473 case SCTP_INITMSG:
3474 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3475 break;
3476 case SCTP_GET_PEER_ADDRS_NUM:
3477 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3478 optlen);
3479 break;
3480 case SCTP_GET_LOCAL_ADDRS_NUM:
3481 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3482 optlen);
3483 break;
3484 case SCTP_GET_PEER_ADDRS:
3485 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3486 optlen);
3487 break;
3488 case SCTP_GET_LOCAL_ADDRS:
3489 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3490 optlen);
3491 break;
3492 case SCTP_DEFAULT_SEND_PARAM:
3493 retval = sctp_getsockopt_default_send_param(sk, len,
3494 optval, optlen);
3495 break;
3496 case SCTP_PRIMARY_ADDR:
3497 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3498 break;
3499 case SCTP_NODELAY:
3500 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3501 break;
3502 case SCTP_RTOINFO:
3503 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3504 break;
3505 case SCTP_ASSOCINFO:
3506 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3507 break;
3508 case SCTP_I_WANT_MAPPED_V4_ADDR:
3509 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3510 break;
3511 case SCTP_MAXSEG:
3512 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3513 break;
3514 case SCTP_GET_PEER_ADDR_INFO:
3515 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3516 optlen);
3517 break;
3518 default:
3519 retval = -ENOPROTOOPT;
3520 break;
3523 sctp_release_sock(sk);
3524 return retval;
3527 static void sctp_hash(struct sock *sk)
3529 /* STUB */
3532 static void sctp_unhash(struct sock *sk)
3534 /* STUB */
3537 /* Check if port is acceptable. Possibly find first available port.
3539 * The port hash table (contained in the 'global' SCTP protocol storage
3540 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3541 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3542 * list (the list number is the port number hashed out, so as you
3543 * would expect from a hash function, all the ports in a given list have
3544 * such a number that hashes out to the same list number; you were
3545 * expecting that, right?); so each list has a set of ports, with a
3546 * link to the socket (struct sock) that uses it, the port number and
3547 * a fastreuse flag (FIXME: NPI ipg).
3549 static struct sctp_bind_bucket *sctp_bucket_create(
3550 struct sctp_bind_hashbucket *head, unsigned short snum);
3552 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3554 struct sctp_bind_hashbucket *head; /* hash list */
3555 struct sctp_bind_bucket *pp; /* hash list port iterator */
3556 unsigned short snum;
3557 int ret;
3559 /* NOTE: Remember to put this back to net order. */
3560 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3561 snum = addr->v4.sin_port;
3563 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3564 sctp_local_bh_disable();
3566 if (snum == 0) {
3567 /* Search for an available port.
3569 * 'sctp_port_rover' was the last port assigned, so
3570 * we start to search from 'sctp_port_rover +
3571 * 1'. What we do is first check if port 'rover' is
3572 * already in the hash table; if not, we use that; if
3573 * it is, we try next.
3575 int low = sysctl_local_port_range[0];
3576 int high = sysctl_local_port_range[1];
3577 int remaining = (high - low) + 1;
3578 int rover;
3579 int index;
3581 sctp_spin_lock(&sctp_port_alloc_lock);
3582 rover = sctp_port_rover;
3583 do {
3584 rover++;
3585 if ((rover < low) || (rover > high))
3586 rover = low;
3587 index = sctp_phashfn(rover);
3588 head = &sctp_port_hashtable[index];
3589 sctp_spin_lock(&head->lock);
3590 for (pp = head->chain; pp; pp = pp->next)
3591 if (pp->port == rover)
3592 goto next;
3593 break;
3594 next:
3595 sctp_spin_unlock(&head->lock);
3596 } while (--remaining > 0);
3597 sctp_port_rover = rover;
3598 sctp_spin_unlock(&sctp_port_alloc_lock);
3600 /* Exhausted local port range during search? */
3601 ret = 1;
3602 if (remaining <= 0)
3603 goto fail;
3605 /* OK, here is the one we will use. HEAD (the port
3606 * hash table list entry) is non-NULL and we hold it's
3607 * mutex.
3609 snum = rover;
3610 } else {
3611 /* We are given an specific port number; we verify
3612 * that it is not being used. If it is used, we will
3613 * exahust the search in the hash list corresponding
3614 * to the port number (snum) - we detect that with the
3615 * port iterator, pp being NULL.
3617 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3618 sctp_spin_lock(&head->lock);
3619 for (pp = head->chain; pp; pp = pp->next) {
3620 if (pp->port == snum)
3621 goto pp_found;
3624 pp = NULL;
3625 goto pp_not_found;
3626 pp_found:
3627 if (!hlist_empty(&pp->owner)) {
3628 /* We had a port hash table hit - there is an
3629 * available port (pp != NULL) and it is being
3630 * used by other socket (pp->owner not empty); that other
3631 * socket is going to be sk2.
3633 int reuse = sk->sk_reuse;
3634 struct sock *sk2;
3635 struct hlist_node *node;
3637 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3638 if (pp->fastreuse && sk->sk_reuse)
3639 goto success;
3641 /* Run through the list of sockets bound to the port
3642 * (pp->port) [via the pointers bind_next and
3643 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3644 * we get the endpoint they describe and run through
3645 * the endpoint's list of IP (v4 or v6) addresses,
3646 * comparing each of the addresses with the address of
3647 * the socket sk. If we find a match, then that means
3648 * that this port/socket (sk) combination are already
3649 * in an endpoint.
3651 sk_for_each_bound(sk2, node, &pp->owner) {
3652 struct sctp_endpoint *ep2;
3653 ep2 = sctp_sk(sk2)->ep;
3655 if (reuse && sk2->sk_reuse)
3656 continue;
3658 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3659 sctp_sk(sk))) {
3660 ret = (long)sk2;
3661 goto fail_unlock;
3664 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3666 pp_not_found:
3667 /* If there was a hash table miss, create a new port. */
3668 ret = 1;
3669 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3670 goto fail_unlock;
3672 /* In either case (hit or miss), make sure fastreuse is 1 only
3673 * if sk->sk_reuse is too (that is, if the caller requested
3674 * SO_REUSEADDR on this socket -sk-).
3676 if (hlist_empty(&pp->owner))
3677 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3678 else if (pp->fastreuse && !sk->sk_reuse)
3679 pp->fastreuse = 0;
3681 /* We are set, so fill up all the data in the hash table
3682 * entry, tie the socket list information with the rest of the
3683 * sockets FIXME: Blurry, NPI (ipg).
3685 success:
3686 inet_sk(sk)->num = snum;
3687 if (!sctp_sk(sk)->bind_hash) {
3688 sk_add_bind_node(sk, &pp->owner);
3689 sctp_sk(sk)->bind_hash = pp;
3691 ret = 0;
3693 fail_unlock:
3694 sctp_spin_unlock(&head->lock);
3696 fail:
3697 sctp_local_bh_enable();
3698 addr->v4.sin_port = htons(addr->v4.sin_port);
3699 return ret;
3702 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3703 * port is requested.
3705 static int sctp_get_port(struct sock *sk, unsigned short snum)
3707 long ret;
3708 union sctp_addr addr;
3709 struct sctp_af *af = sctp_sk(sk)->pf->af;
3711 /* Set up a dummy address struct from the sk. */
3712 af->from_sk(&addr, sk);
3713 addr.v4.sin_port = htons(snum);
3715 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3716 ret = sctp_get_port_local(sk, &addr);
3718 return (ret ? 1 : 0);
3722 * 3.1.3 listen() - UDP Style Syntax
3724 * By default, new associations are not accepted for UDP style sockets.
3725 * An application uses listen() to mark a socket as being able to
3726 * accept new associations.
3728 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3730 struct sctp_opt *sp = sctp_sk(sk);
3731 struct sctp_endpoint *ep = sp->ep;
3733 /* Only UDP style sockets that are not peeled off are allowed to
3734 * listen().
3736 if (!sctp_style(sk, UDP))
3737 return -EINVAL;
3739 /* If backlog is zero, disable listening. */
3740 if (!backlog) {
3741 if (sctp_sstate(sk, CLOSED))
3742 return 0;
3744 sctp_unhash_endpoint(ep);
3745 sk->sk_state = SCTP_SS_CLOSED;
3748 /* Return if we are already listening. */
3749 if (sctp_sstate(sk, LISTENING))
3750 return 0;
3753 * If a bind() or sctp_bindx() is not called prior to a listen()
3754 * call that allows new associations to be accepted, the system
3755 * picks an ephemeral port and will choose an address set equivalent
3756 * to binding with a wildcard address.
3758 * This is not currently spelled out in the SCTP sockets
3759 * extensions draft, but follows the practice as seen in TCP
3760 * sockets.
3762 if (!ep->base.bind_addr.port) {
3763 if (sctp_autobind(sk))
3764 return -EAGAIN;
3766 sk->sk_state = SCTP_SS_LISTENING;
3767 sctp_hash_endpoint(ep);
3768 return 0;
3772 * 4.1.3 listen() - TCP Style Syntax
3774 * Applications uses listen() to ready the SCTP endpoint for accepting
3775 * inbound associations.
3777 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3779 struct sctp_opt *sp = sctp_sk(sk);
3780 struct sctp_endpoint *ep = sp->ep;
3782 /* If backlog is zero, disable listening. */
3783 if (!backlog) {
3784 if (sctp_sstate(sk, CLOSED))
3785 return 0;
3787 sctp_unhash_endpoint(ep);
3788 sk->sk_state = SCTP_SS_CLOSED;
3791 if (sctp_sstate(sk, LISTENING))
3792 return 0;
3795 * If a bind() or sctp_bindx() is not called prior to a listen()
3796 * call that allows new associations to be accepted, the system
3797 * picks an ephemeral port and will choose an address set equivalent
3798 * to binding with a wildcard address.
3800 * This is not currently spelled out in the SCTP sockets
3801 * extensions draft, but follows the practice as seen in TCP
3802 * sockets.
3804 if (!ep->base.bind_addr.port) {
3805 if (sctp_autobind(sk))
3806 return -EAGAIN;
3808 sk->sk_state = SCTP_SS_LISTENING;
3809 sk->sk_max_ack_backlog = backlog;
3810 sctp_hash_endpoint(ep);
3811 return 0;
3815 * Move a socket to LISTENING state.
3817 int sctp_inet_listen(struct socket *sock, int backlog)
3819 struct sock *sk = sock->sk;
3820 struct crypto_tfm *tfm=NULL;
3821 int err = -EINVAL;
3823 if (unlikely(backlog < 0))
3824 goto out;
3826 sctp_lock_sock(sk);
3828 if (sock->state != SS_UNCONNECTED)
3829 goto out;
3831 /* Allocate HMAC for generating cookie. */
3832 if (sctp_hmac_alg) {
3833 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
3834 if (!tfm) {
3835 err = -ENOSYS;
3836 goto out;
3840 switch (sock->type) {
3841 case SOCK_SEQPACKET:
3842 err = sctp_seqpacket_listen(sk, backlog);
3843 break;
3844 case SOCK_STREAM:
3845 err = sctp_stream_listen(sk, backlog);
3846 break;
3847 default:
3848 break;
3850 if (err)
3851 goto cleanup;
3853 /* Store away the transform reference. */
3854 sctp_sk(sk)->hmac = tfm;
3855 out:
3856 sctp_release_sock(sk);
3857 return err;
3858 cleanup:
3859 if (tfm)
3860 sctp_crypto_free_tfm(tfm);
3861 goto out;
3865 * This function is done by modeling the current datagram_poll() and the
3866 * tcp_poll(). Note that, based on these implementations, we don't
3867 * lock the socket in this function, even though it seems that,
3868 * ideally, locking or some other mechanisms can be used to ensure
3869 * the integrity of the counters (sndbuf and wmem_queued) used
3870 * in this place. We assume that we don't need locks either until proven
3871 * otherwise.
3873 * Another thing to note is that we include the Async I/O support
3874 * here, again, by modeling the current TCP/UDP code. We don't have
3875 * a good way to test with it yet.
3877 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
3879 struct sock *sk = sock->sk;
3880 struct sctp_opt *sp = sctp_sk(sk);
3881 unsigned int mask;
3883 poll_wait(file, sk->sk_sleep, wait);
3885 /* A TCP-style listening socket becomes readable when the accept queue
3886 * is not empty.
3888 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3889 return (!list_empty(&sp->ep->asocs)) ?
3890 (POLLIN | POLLRDNORM) : 0;
3892 mask = 0;
3894 /* Is there any exceptional events? */
3895 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
3896 mask |= POLLERR;
3897 if (sk->sk_shutdown == SHUTDOWN_MASK)
3898 mask |= POLLHUP;
3900 /* Is it readable? Reconsider this code with TCP-style support. */
3901 if (!skb_queue_empty(&sk->sk_receive_queue) ||
3902 (sk->sk_shutdown & RCV_SHUTDOWN))
3903 mask |= POLLIN | POLLRDNORM;
3905 /* The association is either gone or not ready. */
3906 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
3907 return mask;
3909 /* Is it writable? */
3910 if (sctp_writeable(sk)) {
3911 mask |= POLLOUT | POLLWRNORM;
3912 } else {
3913 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
3915 * Since the socket is not locked, the buffer
3916 * might be made available after the writeable check and
3917 * before the bit is set. This could cause a lost I/O
3918 * signal. tcp_poll() has a race breaker for this race
3919 * condition. Based on their implementation, we put
3920 * in the following code to cover it as well.
3922 if (sctp_writeable(sk))
3923 mask |= POLLOUT | POLLWRNORM;
3925 return mask;
3928 /********************************************************************
3929 * 2nd Level Abstractions
3930 ********************************************************************/
3932 static struct sctp_bind_bucket *sctp_bucket_create(
3933 struct sctp_bind_hashbucket *head, unsigned short snum)
3935 struct sctp_bind_bucket *pp;
3937 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
3938 SCTP_DBG_OBJCNT_INC(bind_bucket);
3939 if (pp) {
3940 pp->port = snum;
3941 pp->fastreuse = 0;
3942 INIT_HLIST_HEAD(&pp->owner);
3943 if ((pp->next = head->chain) != NULL)
3944 pp->next->pprev = &pp->next;
3945 head->chain = pp;
3946 pp->pprev = &head->chain;
3948 return pp;
3951 /* Caller must hold hashbucket lock for this tb with local BH disabled */
3952 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
3954 if (hlist_empty(&pp->owner)) {
3955 if (pp->next)
3956 pp->next->pprev = pp->pprev;
3957 *(pp->pprev) = pp->next;
3958 kmem_cache_free(sctp_bucket_cachep, pp);
3959 SCTP_DBG_OBJCNT_DEC(bind_bucket);
3963 /* Release this socket's reference to a local port. */
3964 static inline void __sctp_put_port(struct sock *sk)
3966 struct sctp_bind_hashbucket *head =
3967 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
3968 struct sctp_bind_bucket *pp;
3970 sctp_spin_lock(&head->lock);
3971 pp = sctp_sk(sk)->bind_hash;
3972 __sk_del_bind_node(sk);
3973 sctp_sk(sk)->bind_hash = NULL;
3974 inet_sk(sk)->num = 0;
3975 sctp_bucket_destroy(pp);
3976 sctp_spin_unlock(&head->lock);
3979 void sctp_put_port(struct sock *sk)
3981 sctp_local_bh_disable();
3982 __sctp_put_port(sk);
3983 sctp_local_bh_enable();
3987 * The system picks an ephemeral port and choose an address set equivalent
3988 * to binding with a wildcard address.
3989 * One of those addresses will be the primary address for the association.
3990 * This automatically enables the multihoming capability of SCTP.
3992 static int sctp_autobind(struct sock *sk)
3994 union sctp_addr autoaddr;
3995 struct sctp_af *af;
3996 unsigned short port;
3998 /* Initialize a local sockaddr structure to INADDR_ANY. */
3999 af = sctp_sk(sk)->pf->af;
4001 port = htons(inet_sk(sk)->num);
4002 af->inaddr_any(&autoaddr, port);
4004 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4007 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4009 * From RFC 2292
4010 * 4.2 The cmsghdr Structure *
4012 * When ancillary data is sent or received, any number of ancillary data
4013 * objects can be specified by the msg_control and msg_controllen members of
4014 * the msghdr structure, because each object is preceded by
4015 * a cmsghdr structure defining the object's length (the cmsg_len member).
4016 * Historically Berkeley-derived implementations have passed only one object
4017 * at a time, but this API allows multiple objects to be
4018 * passed in a single call to sendmsg() or recvmsg(). The following example
4019 * shows two ancillary data objects in a control buffer.
4021 * |<--------------------------- msg_controllen -------------------------->|
4022 * | |
4024 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4026 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4027 * | | |
4029 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4031 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4032 * | | | | |
4034 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4035 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4037 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4039 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4043 * msg_control
4044 * points here
4046 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4047 sctp_cmsgs_t *cmsgs)
4049 struct cmsghdr *cmsg;
4051 for (cmsg = CMSG_FIRSTHDR(msg);
4052 cmsg != NULL;
4053 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4054 /* Check for minimum length. The SCM code has this check. */
4055 if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
4056 (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
4057 + cmsg->cmsg_len) > msg->msg_controllen) {
4058 return -EINVAL;
4061 /* Should we parse this header or ignore? */
4062 if (cmsg->cmsg_level != IPPROTO_SCTP)
4063 continue;
4065 /* Strictly check lengths following example in SCM code. */
4066 switch (cmsg->cmsg_type) {
4067 case SCTP_INIT:
4068 /* SCTP Socket API Extension
4069 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4071 * This cmsghdr structure provides information for
4072 * initializing new SCTP associations with sendmsg().
4073 * The SCTP_INITMSG socket option uses this same data
4074 * structure. This structure is not used for
4075 * recvmsg().
4077 * cmsg_level cmsg_type cmsg_data[]
4078 * ------------ ------------ ----------------------
4079 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4081 if (cmsg->cmsg_len !=
4082 CMSG_LEN(sizeof(struct sctp_initmsg)))
4083 return -EINVAL;
4084 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4085 break;
4087 case SCTP_SNDRCV:
4088 /* SCTP Socket API Extension
4089 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4091 * This cmsghdr structure specifies SCTP options for
4092 * sendmsg() and describes SCTP header information
4093 * about a received message through recvmsg().
4095 * cmsg_level cmsg_type cmsg_data[]
4096 * ------------ ------------ ----------------------
4097 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4099 if (cmsg->cmsg_len !=
4100 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4101 return -EINVAL;
4103 cmsgs->info =
4104 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4106 /* Minimally, validate the sinfo_flags. */
4107 if (cmsgs->info->sinfo_flags &
4108 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4109 MSG_ABORT | MSG_EOF))
4110 return -EINVAL;
4111 break;
4113 default:
4114 return -EINVAL;
4117 return 0;
4121 * Wait for a packet..
4122 * Note: This function is the same function as in core/datagram.c
4123 * with a few modifications to make lksctp work.
4125 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4127 int error;
4128 DEFINE_WAIT(wait);
4130 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4132 /* Socket errors? */
4133 error = sock_error(sk);
4134 if (error)
4135 goto out;
4137 if (!skb_queue_empty(&sk->sk_receive_queue))
4138 goto ready;
4140 /* Socket shut down? */
4141 if (sk->sk_shutdown & RCV_SHUTDOWN)
4142 goto out;
4144 /* Sequenced packets can come disconnected. If so we report the
4145 * problem.
4147 error = -ENOTCONN;
4149 /* Is there a good reason to think that we may receive some data? */
4150 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4151 goto out;
4153 /* Handle signals. */
4154 if (signal_pending(current))
4155 goto interrupted;
4157 /* Let another process have a go. Since we are going to sleep
4158 * anyway. Note: This may cause odd behaviors if the message
4159 * does not fit in the user's buffer, but this seems to be the
4160 * only way to honor MSG_DONTWAIT realistically.
4162 sctp_release_sock(sk);
4163 *timeo_p = schedule_timeout(*timeo_p);
4164 sctp_lock_sock(sk);
4166 ready:
4167 finish_wait(sk->sk_sleep, &wait);
4168 return 0;
4170 interrupted:
4171 error = sock_intr_errno(*timeo_p);
4173 out:
4174 finish_wait(sk->sk_sleep, &wait);
4175 *err = error;
4176 return error;
4179 /* Receive a datagram.
4180 * Note: This is pretty much the same routine as in core/datagram.c
4181 * with a few changes to make lksctp work.
4183 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4184 int noblock, int *err)
4186 int error;
4187 struct sk_buff *skb;
4188 long timeo;
4190 /* Caller is allowed not to check sk->sk_err before calling. */
4191 error = sock_error(sk);
4192 if (error)
4193 goto no_packet;
4195 timeo = sock_rcvtimeo(sk, noblock);
4197 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4198 timeo, MAX_SCHEDULE_TIMEOUT);
4200 do {
4201 /* Again only user level code calls this function,
4202 * so nothing interrupt level
4203 * will suddenly eat the receive_queue.
4205 * Look at current nfs client by the way...
4206 * However, this function was corrent in any case. 8)
4208 if (flags & MSG_PEEK) {
4209 unsigned long cpu_flags;
4211 sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4212 cpu_flags);
4213 skb = skb_peek(&sk->sk_receive_queue);
4214 if (skb)
4215 atomic_inc(&skb->users);
4216 sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4217 cpu_flags);
4218 } else {
4219 skb = skb_dequeue(&sk->sk_receive_queue);
4222 if (skb)
4223 return skb;
4225 if (sk->sk_shutdown & RCV_SHUTDOWN)
4226 break;
4228 /* User doesn't want to wait. */
4229 error = -EAGAIN;
4230 if (!timeo)
4231 goto no_packet;
4232 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4234 return NULL;
4236 no_packet:
4237 *err = error;
4238 return NULL;
4241 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4242 static void __sctp_write_space(struct sctp_association *asoc)
4244 struct sock *sk = asoc->base.sk;
4245 struct socket *sock = sk->sk_socket;
4247 if ((sctp_wspace(asoc) > 0) && sock) {
4248 if (waitqueue_active(&asoc->wait))
4249 wake_up_interruptible(&asoc->wait);
4251 if (sctp_writeable(sk)) {
4252 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4253 wake_up_interruptible(sk->sk_sleep);
4255 /* Note that we try to include the Async I/O support
4256 * here by modeling from the current TCP/UDP code.
4257 * We have not tested with it yet.
4259 if (sock->fasync_list &&
4260 !(sk->sk_shutdown & SEND_SHUTDOWN))
4261 sock_wake_async(sock, 2, POLL_OUT);
4266 /* Do accounting for the sndbuf space.
4267 * Decrement the used sndbuf space of the corresponding association by the
4268 * data size which was just transmitted(freed).
4270 static void sctp_wfree(struct sk_buff *skb)
4272 struct sctp_association *asoc;
4273 struct sctp_chunk *chunk;
4274 struct sock *sk;
4276 /* Get the saved chunk pointer. */
4277 chunk = *((struct sctp_chunk **)(skb->cb));
4278 asoc = chunk->asoc;
4279 sk = asoc->base.sk;
4280 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk);
4281 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk);
4282 __sctp_write_space(asoc);
4284 sctp_association_put(asoc);
4287 /* Helper function to wait for space in the sndbuf. */
4288 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4289 size_t msg_len)
4291 struct sock *sk = asoc->base.sk;
4292 int err = 0;
4293 long current_timeo = *timeo_p;
4294 DEFINE_WAIT(wait);
4296 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4297 asoc, (long)(*timeo_p), msg_len);
4299 /* Increment the association's refcnt. */
4300 sctp_association_hold(asoc);
4302 /* Wait on the association specific sndbuf space. */
4303 for (;;) {
4304 prepare_to_wait_exclusive(&asoc->wait, &wait,
4305 TASK_INTERRUPTIBLE);
4306 if (!*timeo_p)
4307 goto do_nonblock;
4308 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4309 asoc->base.dead)
4310 goto do_error;
4311 if (signal_pending(current))
4312 goto do_interrupted;
4313 if (msg_len <= sctp_wspace(asoc))
4314 break;
4316 /* Let another process have a go. Since we are going
4317 * to sleep anyway.
4319 sctp_release_sock(sk);
4320 current_timeo = schedule_timeout(current_timeo);
4321 sctp_lock_sock(sk);
4323 *timeo_p = current_timeo;
4326 out:
4327 finish_wait(&asoc->wait, &wait);
4329 /* Release the association's refcnt. */
4330 sctp_association_put(asoc);
4332 return err;
4334 do_error:
4335 err = -EPIPE;
4336 goto out;
4338 do_interrupted:
4339 err = sock_intr_errno(*timeo_p);
4340 goto out;
4342 do_nonblock:
4343 err = -EAGAIN;
4344 goto out;
4347 /* If socket sndbuf has changed, wake up all per association waiters. */
4348 void sctp_write_space(struct sock *sk)
4350 struct sctp_association *asoc;
4351 struct list_head *pos;
4353 /* Wake up the tasks in each wait queue. */
4354 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4355 asoc = list_entry(pos, struct sctp_association, asocs);
4356 __sctp_write_space(asoc);
4360 /* Is there any sndbuf space available on the socket?
4362 * Note that wmem_queued is the sum of the send buffers on all of the
4363 * associations on the same socket. For a UDP-style socket with
4364 * multiple associations, it is possible for it to be "unwriteable"
4365 * prematurely. I assume that this is acceptable because
4366 * a premature "unwriteable" is better than an accidental "writeable" which
4367 * would cause an unwanted block under certain circumstances. For the 1-1
4368 * UDP-style sockets or TCP-style sockets, this code should work.
4369 * - Daisy
4371 static int sctp_writeable(struct sock *sk)
4373 int amt = 0;
4375 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4376 if (amt < 0)
4377 amt = 0;
4378 return amt;
4381 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4382 * returns immediately with EINPROGRESS.
4384 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4386 struct sock *sk = asoc->base.sk;
4387 int err = 0;
4388 long current_timeo = *timeo_p;
4389 DEFINE_WAIT(wait);
4391 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4392 (long)(*timeo_p));
4394 /* Increment the association's refcnt. */
4395 sctp_association_hold(asoc);
4397 for (;;) {
4398 prepare_to_wait_exclusive(&asoc->wait, &wait,
4399 TASK_INTERRUPTIBLE);
4400 if (!*timeo_p)
4401 goto do_nonblock;
4402 if (sk->sk_shutdown & RCV_SHUTDOWN)
4403 break;
4404 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4405 asoc->base.dead)
4406 goto do_error;
4407 if (signal_pending(current))
4408 goto do_interrupted;
4410 if (sctp_state(asoc, ESTABLISHED))
4411 break;
4413 /* Let another process have a go. Since we are going
4414 * to sleep anyway.
4416 sctp_release_sock(sk);
4417 current_timeo = schedule_timeout(current_timeo);
4418 sctp_lock_sock(sk);
4420 *timeo_p = current_timeo;
4423 out:
4424 finish_wait(&asoc->wait, &wait);
4426 /* Release the association's refcnt. */
4427 sctp_association_put(asoc);
4429 return err;
4431 do_error:
4432 if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
4433 asoc->max_init_attempts)
4434 err = -ETIMEDOUT;
4435 else
4436 err = -ECONNREFUSED;
4437 goto out;
4439 do_interrupted:
4440 err = sock_intr_errno(*timeo_p);
4441 goto out;
4443 do_nonblock:
4444 err = -EINPROGRESS;
4445 goto out;
4448 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4450 struct sctp_endpoint *ep;
4451 int err = 0;
4452 DEFINE_WAIT(wait);
4454 ep = sctp_sk(sk)->ep;
4457 for (;;) {
4458 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4459 TASK_INTERRUPTIBLE);
4461 if (list_empty(&ep->asocs)) {
4462 sctp_release_sock(sk);
4463 timeo = schedule_timeout(timeo);
4464 sctp_lock_sock(sk);
4467 err = -EINVAL;
4468 if (!sctp_sstate(sk, LISTENING))
4469 break;
4471 err = 0;
4472 if (!list_empty(&ep->asocs))
4473 break;
4475 err = sock_intr_errno(timeo);
4476 if (signal_pending(current))
4477 break;
4479 err = -EAGAIN;
4480 if (!timeo)
4481 break;
4484 finish_wait(sk->sk_sleep, &wait);
4486 return err;
4489 void sctp_wait_for_close(struct sock *sk, long timeout)
4491 DEFINE_WAIT(wait);
4493 do {
4494 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4495 if (list_empty(&sctp_sk(sk)->ep->asocs))
4496 break;
4497 sctp_release_sock(sk);
4498 timeout = schedule_timeout(timeout);
4499 sctp_lock_sock(sk);
4500 } while (!signal_pending(current) && timeout);
4502 finish_wait(sk->sk_sleep, &wait);
4505 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4506 * and its messages to the newsk.
4508 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4509 struct sctp_association *assoc,
4510 sctp_socket_type_t type)
4512 struct sctp_opt *oldsp = sctp_sk(oldsk);
4513 struct sctp_opt *newsp = sctp_sk(newsk);
4514 struct sctp_bind_bucket *pp; /* hash list port iterator */
4515 struct sctp_endpoint *newep = newsp->ep;
4516 struct sk_buff *skb, *tmp;
4517 struct sctp_ulpevent *event;
4519 /* Migrate socket buffer sizes and all the socket level options to the
4520 * new socket.
4522 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4523 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4524 /* Brute force copy old sctp opt. */
4525 memcpy(newsp, oldsp, sizeof(struct sctp_opt));
4527 /* Restore the ep value that was overwritten with the above structure
4528 * copy.
4530 newsp->ep = newep;
4531 newsp->hmac = NULL;
4533 /* Hook this new socket in to the bind_hash list. */
4534 pp = sctp_sk(oldsk)->bind_hash;
4535 sk_add_bind_node(newsk, &pp->owner);
4536 sctp_sk(newsk)->bind_hash = pp;
4537 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4539 /* Move any messages in the old socket's receive queue that are for the
4540 * peeled off association to the new socket's receive queue.
4542 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4543 event = sctp_skb2event(skb);
4544 if (event->asoc == assoc) {
4545 __skb_unlink(skb, skb->list);
4546 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4550 /* Clean up any messages pending delivery due to partial
4551 * delivery. Three cases:
4552 * 1) No partial deliver; no work.
4553 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4554 * 3) Peeling off non-partial delivery; move pd_lobby to recieve_queue.
4556 skb_queue_head_init(&newsp->pd_lobby);
4557 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4559 if (sctp_sk(oldsk)->pd_mode) {
4560 struct sk_buff_head *queue;
4562 /* Decide which queue to move pd_lobby skbs to. */
4563 if (assoc->ulpq.pd_mode) {
4564 queue = &newsp->pd_lobby;
4565 } else
4566 queue = &newsk->sk_receive_queue;
4568 /* Walk through the pd_lobby, looking for skbs that
4569 * need moved to the new socket.
4571 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4572 event = sctp_skb2event(skb);
4573 if (event->asoc == assoc) {
4574 __skb_unlink(skb, skb->list);
4575 __skb_queue_tail(queue, skb);
4579 /* Clear up any skbs waiting for the partial
4580 * delivery to finish.
4582 if (assoc->ulpq.pd_mode)
4583 sctp_clear_pd(oldsk);
4587 /* Set the type of socket to indicate that it is peeled off from the
4588 * original UDP-style socket or created with the accept() call on a
4589 * TCP-style socket..
4591 newsp->type = type;
4593 /* Migrate the association to the new socket. */
4594 sctp_assoc_migrate(assoc, newsk);
4596 /* If the association on the newsk is already closed before accept()
4597 * is called, set RCV_SHUTDOWN flag.
4599 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4600 newsk->sk_shutdown |= RCV_SHUTDOWN;
4602 newsk->sk_state = SCTP_SS_ESTABLISHED;
4605 /* This proto struct describes the ULP interface for SCTP. */
4606 struct proto sctp_prot = {
4607 .name = "SCTP",
4608 .close = sctp_close,
4609 .connect = sctp_connect,
4610 .disconnect = sctp_disconnect,
4611 .accept = sctp_accept,
4612 .ioctl = sctp_ioctl,
4613 .init = sctp_init_sock,
4614 .destroy = sctp_destroy_sock,
4615 .shutdown = sctp_shutdown,
4616 .setsockopt = sctp_setsockopt,
4617 .getsockopt = sctp_getsockopt,
4618 .sendmsg = sctp_sendmsg,
4619 .recvmsg = sctp_recvmsg,
4620 .bind = sctp_bind,
4621 .backlog_rcv = sctp_backlog_rcv,
4622 .hash = sctp_hash,
4623 .unhash = sctp_unhash,
4624 .get_port = sctp_get_port,
4625 .slab_obj_size = sizeof(struct sctp_sock),
4628 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4629 struct proto sctpv6_prot = {
4630 .name = "SCTPv6",
4631 .close = sctp_close,
4632 .connect = sctp_connect,
4633 .disconnect = sctp_disconnect,
4634 .accept = sctp_accept,
4635 .ioctl = sctp_ioctl,
4636 .init = sctp_init_sock,
4637 .destroy = sctp_destroy_sock,
4638 .shutdown = sctp_shutdown,
4639 .setsockopt = sctp_setsockopt,
4640 .getsockopt = sctp_getsockopt,
4641 .sendmsg = sctp_sendmsg,
4642 .recvmsg = sctp_recvmsg,
4643 .bind = sctp_bind,
4644 .backlog_rcv = sctp_backlog_rcv,
4645 .hash = sctp_hash,
4646 .unhash = sctp_unhash,
4647 .get_port = sctp_get_port,
4648 .slab_obj_size = sizeof(struct sctp6_sock),
4650 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */