[PATCH] cpufreq: documentation for 'ondemand' and 'conservative'
[linux-2.6/mini2440.git] / net / sctp / socket.c
blobabab81f3818f1a4c77742b315ad943853ad062ce
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
65 #include <linux/ip.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
71 #include <net/ip.h>
72 #include <net/icmp.h>
73 #include <net/route.h>
74 #include <net/ipv6.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <net/sock.h>
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 extern kmem_cache_t *sctp_bucket_cachep;
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
121 } else {
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
126 if (amt < 0)
127 amt = 0;
129 return amt;
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
139 * tracking.
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
149 skb_set_owner_w(chunk->skb, sk);
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
159 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
160 sizeof(struct sk_buff) +
161 sizeof(struct sctp_chunk);
163 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
166 /* Verify that this is a valid address. */
167 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
168 int len)
170 struct sctp_af *af;
172 /* Verify basic sockaddr. */
173 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
174 if (!af)
175 return -EINVAL;
177 /* Is this a valid SCTP address? */
178 if (!af->addr_valid(addr, sctp_sk(sk)))
179 return -EINVAL;
181 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
182 return -EINVAL;
184 return 0;
187 /* Look up the association by its id. If this is not a UDP-style
188 * socket, the ID field is always ignored.
190 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
192 struct sctp_association *asoc = NULL;
194 /* If this is not a UDP-style socket, assoc id should be ignored. */
195 if (!sctp_style(sk, UDP)) {
196 /* Return NULL if the socket state is not ESTABLISHED. It
197 * could be a TCP-style listening socket or a socket which
198 * hasn't yet called connect() to establish an association.
200 if (!sctp_sstate(sk, ESTABLISHED))
201 return NULL;
203 /* Get the first and the only association from the list. */
204 if (!list_empty(&sctp_sk(sk)->ep->asocs))
205 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
206 struct sctp_association, asocs);
207 return asoc;
210 /* Otherwise this is a UDP-style socket. */
211 if (!id || (id == (sctp_assoc_t)-1))
212 return NULL;
214 spin_lock_bh(&sctp_assocs_id_lock);
215 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
216 spin_unlock_bh(&sctp_assocs_id_lock);
218 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
219 return NULL;
221 return asoc;
224 /* Look up the transport from an address and an assoc id. If both address and
225 * id are specified, the associations matching the address and the id should be
226 * the same.
228 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
229 struct sockaddr_storage *addr,
230 sctp_assoc_t id)
232 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
233 struct sctp_transport *transport;
234 union sctp_addr *laddr = (union sctp_addr *)addr;
236 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
237 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
238 (union sctp_addr *)addr,
239 &transport);
240 laddr->v4.sin_port = htons(laddr->v4.sin_port);
242 if (!addr_asoc)
243 return NULL;
245 id_asoc = sctp_id2assoc(sk, id);
246 if (id_asoc && (id_asoc != addr_asoc))
247 return NULL;
249 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
250 (union sctp_addr *)addr);
252 return transport;
255 /* API 3.1.2 bind() - UDP Style Syntax
256 * The syntax of bind() is,
258 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
260 * sd - the socket descriptor returned by socket().
261 * addr - the address structure (struct sockaddr_in or struct
262 * sockaddr_in6 [RFC 2553]),
263 * addr_len - the size of the address structure.
265 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
267 int retval = 0;
269 sctp_lock_sock(sk);
271 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
272 sk, addr, addr_len);
274 /* Disallow binding twice. */
275 if (!sctp_sk(sk)->ep->base.bind_addr.port)
276 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
277 addr_len);
278 else
279 retval = -EINVAL;
281 sctp_release_sock(sk);
283 return retval;
286 static long sctp_get_port_local(struct sock *, union sctp_addr *);
288 /* Verify this is a valid sockaddr. */
289 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
290 union sctp_addr *addr, int len)
292 struct sctp_af *af;
294 /* Check minimum size. */
295 if (len < sizeof (struct sockaddr))
296 return NULL;
298 /* Does this PF support this AF? */
299 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
300 return NULL;
302 /* If we get this far, af is valid. */
303 af = sctp_get_af_specific(addr->sa.sa_family);
305 if (len < af->sockaddr_len)
306 return NULL;
308 return af;
311 /* Bind a local address either to an endpoint or to an association. */
312 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
314 struct sctp_sock *sp = sctp_sk(sk);
315 struct sctp_endpoint *ep = sp->ep;
316 struct sctp_bind_addr *bp = &ep->base.bind_addr;
317 struct sctp_af *af;
318 unsigned short snum;
319 int ret = 0;
321 /* Common sockaddr verification. */
322 af = sctp_sockaddr_af(sp, addr, len);
323 if (!af) {
324 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
325 sk, addr, len);
326 return -EINVAL;
329 snum = ntohs(addr->v4.sin_port);
331 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
332 ", port: %d, new port: %d, len: %d)\n",
334 addr,
335 bp->port, snum,
336 len);
338 /* PF specific bind() address verification. */
339 if (!sp->pf->bind_verify(sp, addr))
340 return -EADDRNOTAVAIL;
342 /* We must either be unbound, or bind to the same port. */
343 if (bp->port && (snum != bp->port)) {
344 SCTP_DEBUG_PRINTK("sctp_do_bind:"
345 " New port %d does not match existing port "
346 "%d.\n", snum, bp->port);
347 return -EINVAL;
350 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
351 return -EACCES;
353 /* Make sure we are allowed to bind here.
354 * The function sctp_get_port_local() does duplicate address
355 * detection.
357 if ((ret = sctp_get_port_local(sk, addr))) {
358 if (ret == (long) sk) {
359 /* This endpoint has a conflicting address. */
360 return -EINVAL;
361 } else {
362 return -EADDRINUSE;
366 /* Refresh ephemeral port. */
367 if (!bp->port)
368 bp->port = inet_sk(sk)->num;
370 /* Add the address to the bind address list. */
371 sctp_local_bh_disable();
372 sctp_write_lock(&ep->base.addr_lock);
374 /* Use GFP_ATOMIC since BHs are disabled. */
375 addr->v4.sin_port = ntohs(addr->v4.sin_port);
376 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
377 addr->v4.sin_port = htons(addr->v4.sin_port);
378 sctp_write_unlock(&ep->base.addr_lock);
379 sctp_local_bh_enable();
381 /* Copy back into socket for getsockname() use. */
382 if (!ret) {
383 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
384 af->to_sk_saddr(addr, sk);
387 return ret;
390 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
392 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
393 * at any one time. If a sender, after sending an ASCONF chunk, decides
394 * it needs to transfer another ASCONF Chunk, it MUST wait until the
395 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
396 * subsequent ASCONF. Note this restriction binds each side, so at any
397 * time two ASCONF may be in-transit on any given association (one sent
398 * from each endpoint).
400 static int sctp_send_asconf(struct sctp_association *asoc,
401 struct sctp_chunk *chunk)
403 int retval = 0;
405 /* If there is an outstanding ASCONF chunk, queue it for later
406 * transmission.
408 if (asoc->addip_last_asconf) {
409 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
410 goto out;
413 /* Hold the chunk until an ASCONF_ACK is received. */
414 sctp_chunk_hold(chunk);
415 retval = sctp_primitive_ASCONF(asoc, chunk);
416 if (retval)
417 sctp_chunk_free(chunk);
418 else
419 asoc->addip_last_asconf = chunk;
421 out:
422 return retval;
425 /* Add a list of addresses as bind addresses to local endpoint or
426 * association.
428 * Basically run through each address specified in the addrs/addrcnt
429 * array/length pair, determine if it is IPv6 or IPv4 and call
430 * sctp_do_bind() on it.
432 * If any of them fails, then the operation will be reversed and the
433 * ones that were added will be removed.
435 * Only sctp_setsockopt_bindx() is supposed to call this function.
437 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
439 int cnt;
440 int retval = 0;
441 void *addr_buf;
442 struct sockaddr *sa_addr;
443 struct sctp_af *af;
445 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
446 sk, addrs, addrcnt);
448 addr_buf = addrs;
449 for (cnt = 0; cnt < addrcnt; cnt++) {
450 /* The list may contain either IPv4 or IPv6 address;
451 * determine the address length for walking thru the list.
453 sa_addr = (struct sockaddr *)addr_buf;
454 af = sctp_get_af_specific(sa_addr->sa_family);
455 if (!af) {
456 retval = -EINVAL;
457 goto err_bindx_add;
460 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
461 af->sockaddr_len);
463 addr_buf += af->sockaddr_len;
465 err_bindx_add:
466 if (retval < 0) {
467 /* Failed. Cleanup the ones that have been added */
468 if (cnt > 0)
469 sctp_bindx_rem(sk, addrs, cnt);
470 return retval;
474 return retval;
477 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
478 * associations that are part of the endpoint indicating that a list of local
479 * addresses are added to the endpoint.
481 * If any of the addresses is already in the bind address list of the
482 * association, we do not send the chunk for that association. But it will not
483 * affect other associations.
485 * Only sctp_setsockopt_bindx() is supposed to call this function.
487 static int sctp_send_asconf_add_ip(struct sock *sk,
488 struct sockaddr *addrs,
489 int addrcnt)
491 struct sctp_sock *sp;
492 struct sctp_endpoint *ep;
493 struct sctp_association *asoc;
494 struct sctp_bind_addr *bp;
495 struct sctp_chunk *chunk;
496 struct sctp_sockaddr_entry *laddr;
497 union sctp_addr *addr;
498 void *addr_buf;
499 struct sctp_af *af;
500 struct list_head *pos;
501 struct list_head *p;
502 int i;
503 int retval = 0;
505 if (!sctp_addip_enable)
506 return retval;
508 sp = sctp_sk(sk);
509 ep = sp->ep;
511 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
512 __FUNCTION__, sk, addrs, addrcnt);
514 list_for_each(pos, &ep->asocs) {
515 asoc = list_entry(pos, struct sctp_association, asocs);
517 if (!asoc->peer.asconf_capable)
518 continue;
520 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
521 continue;
523 if (!sctp_state(asoc, ESTABLISHED))
524 continue;
526 /* Check if any address in the packed array of addresses is
527 * in the bind address list of the association. If so,
528 * do not send the asconf chunk to its peer, but continue with
529 * other associations.
531 addr_buf = addrs;
532 for (i = 0; i < addrcnt; i++) {
533 addr = (union sctp_addr *)addr_buf;
534 af = sctp_get_af_specific(addr->v4.sin_family);
535 if (!af) {
536 retval = -EINVAL;
537 goto out;
540 if (sctp_assoc_lookup_laddr(asoc, addr))
541 break;
543 addr_buf += af->sockaddr_len;
545 if (i < addrcnt)
546 continue;
548 /* Use the first address in bind addr list of association as
549 * Address Parameter of ASCONF CHUNK.
551 sctp_read_lock(&asoc->base.addr_lock);
552 bp = &asoc->base.bind_addr;
553 p = bp->address_list.next;
554 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
555 sctp_read_unlock(&asoc->base.addr_lock);
557 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
558 addrcnt, SCTP_PARAM_ADD_IP);
559 if (!chunk) {
560 retval = -ENOMEM;
561 goto out;
564 retval = sctp_send_asconf(asoc, chunk);
566 /* FIXME: After sending the add address ASCONF chunk, we
567 * cannot append the address to the association's binding
568 * address list, because the new address may be used as the
569 * source of a message sent to the peer before the ASCONF
570 * chunk is received by the peer. So we should wait until
571 * ASCONF_ACK is received.
575 out:
576 return retval;
579 /* Remove a list of addresses from bind addresses list. Do not remove the
580 * last address.
582 * Basically run through each address specified in the addrs/addrcnt
583 * array/length pair, determine if it is IPv6 or IPv4 and call
584 * sctp_del_bind() on it.
586 * If any of them fails, then the operation will be reversed and the
587 * ones that were removed will be added back.
589 * At least one address has to be left; if only one address is
590 * available, the operation will return -EBUSY.
592 * Only sctp_setsockopt_bindx() is supposed to call this function.
594 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
596 struct sctp_sock *sp = sctp_sk(sk);
597 struct sctp_endpoint *ep = sp->ep;
598 int cnt;
599 struct sctp_bind_addr *bp = &ep->base.bind_addr;
600 int retval = 0;
601 union sctp_addr saveaddr;
602 void *addr_buf;
603 struct sockaddr *sa_addr;
604 struct sctp_af *af;
606 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
607 sk, addrs, addrcnt);
609 addr_buf = addrs;
610 for (cnt = 0; cnt < addrcnt; cnt++) {
611 /* If the bind address list is empty or if there is only one
612 * bind address, there is nothing more to be removed (we need
613 * at least one address here).
615 if (list_empty(&bp->address_list) ||
616 (sctp_list_single_entry(&bp->address_list))) {
617 retval = -EBUSY;
618 goto err_bindx_rem;
621 /* The list may contain either IPv4 or IPv6 address;
622 * determine the address length to copy the address to
623 * saveaddr.
625 sa_addr = (struct sockaddr *)addr_buf;
626 af = sctp_get_af_specific(sa_addr->sa_family);
627 if (!af) {
628 retval = -EINVAL;
629 goto err_bindx_rem;
631 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
632 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
633 if (saveaddr.v4.sin_port != bp->port) {
634 retval = -EINVAL;
635 goto err_bindx_rem;
638 /* FIXME - There is probably a need to check if sk->sk_saddr and
639 * sk->sk_rcv_addr are currently set to one of the addresses to
640 * be removed. This is something which needs to be looked into
641 * when we are fixing the outstanding issues with multi-homing
642 * socket routing and failover schemes. Refer to comments in
643 * sctp_do_bind(). -daisy
645 sctp_local_bh_disable();
646 sctp_write_lock(&ep->base.addr_lock);
648 retval = sctp_del_bind_addr(bp, &saveaddr);
650 sctp_write_unlock(&ep->base.addr_lock);
651 sctp_local_bh_enable();
653 addr_buf += af->sockaddr_len;
654 err_bindx_rem:
655 if (retval < 0) {
656 /* Failed. Add the ones that has been removed back */
657 if (cnt > 0)
658 sctp_bindx_add(sk, addrs, cnt);
659 return retval;
663 return retval;
666 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
667 * the associations that are part of the endpoint indicating that a list of
668 * local addresses are removed from the endpoint.
670 * If any of the addresses is already in the bind address list of the
671 * association, we do not send the chunk for that association. But it will not
672 * affect other associations.
674 * Only sctp_setsockopt_bindx() is supposed to call this function.
676 static int sctp_send_asconf_del_ip(struct sock *sk,
677 struct sockaddr *addrs,
678 int addrcnt)
680 struct sctp_sock *sp;
681 struct sctp_endpoint *ep;
682 struct sctp_association *asoc;
683 struct sctp_bind_addr *bp;
684 struct sctp_chunk *chunk;
685 union sctp_addr *laddr;
686 void *addr_buf;
687 struct sctp_af *af;
688 struct list_head *pos;
689 int i;
690 int retval = 0;
692 if (!sctp_addip_enable)
693 return retval;
695 sp = sctp_sk(sk);
696 ep = sp->ep;
698 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
699 __FUNCTION__, sk, addrs, addrcnt);
701 list_for_each(pos, &ep->asocs) {
702 asoc = list_entry(pos, struct sctp_association, asocs);
704 if (!asoc->peer.asconf_capable)
705 continue;
707 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
708 continue;
710 if (!sctp_state(asoc, ESTABLISHED))
711 continue;
713 /* Check if any address in the packed array of addresses is
714 * not present in the bind address list of the association.
715 * If so, do not send the asconf chunk to its peer, but
716 * continue with other associations.
718 addr_buf = addrs;
719 for (i = 0; i < addrcnt; i++) {
720 laddr = (union sctp_addr *)addr_buf;
721 af = sctp_get_af_specific(laddr->v4.sin_family);
722 if (!af) {
723 retval = -EINVAL;
724 goto out;
727 if (!sctp_assoc_lookup_laddr(asoc, laddr))
728 break;
730 addr_buf += af->sockaddr_len;
732 if (i < addrcnt)
733 continue;
735 /* Find one address in the association's bind address list
736 * that is not in the packed array of addresses. This is to
737 * make sure that we do not delete all the addresses in the
738 * association.
740 sctp_read_lock(&asoc->base.addr_lock);
741 bp = &asoc->base.bind_addr;
742 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
743 addrcnt, sp);
744 sctp_read_unlock(&asoc->base.addr_lock);
745 if (!laddr)
746 continue;
748 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
749 SCTP_PARAM_DEL_IP);
750 if (!chunk) {
751 retval = -ENOMEM;
752 goto out;
755 retval = sctp_send_asconf(asoc, chunk);
757 /* FIXME: After sending the delete address ASCONF chunk, we
758 * cannot remove the addresses from the association's bind
759 * address list, because there maybe some packet send to
760 * the delete addresses, so we should wait until ASCONF_ACK
761 * packet is received.
764 out:
765 return retval;
768 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
770 * API 8.1
771 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
772 * int flags);
774 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
775 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
776 * or IPv6 addresses.
778 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
779 * Section 3.1.2 for this usage.
781 * addrs is a pointer to an array of one or more socket addresses. Each
782 * address is contained in its appropriate structure (i.e. struct
783 * sockaddr_in or struct sockaddr_in6) the family of the address type
784 * must be used to distengish the address length (note that this
785 * representation is termed a "packed array" of addresses). The caller
786 * specifies the number of addresses in the array with addrcnt.
788 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
789 * -1, and sets errno to the appropriate error code.
791 * For SCTP, the port given in each socket address must be the same, or
792 * sctp_bindx() will fail, setting errno to EINVAL.
794 * The flags parameter is formed from the bitwise OR of zero or more of
795 * the following currently defined flags:
797 * SCTP_BINDX_ADD_ADDR
799 * SCTP_BINDX_REM_ADDR
801 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
802 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
803 * addresses from the association. The two flags are mutually exclusive;
804 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
805 * not remove all addresses from an association; sctp_bindx() will
806 * reject such an attempt with EINVAL.
808 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
809 * additional addresses with an endpoint after calling bind(). Or use
810 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
811 * socket is associated with so that no new association accepted will be
812 * associated with those addresses. If the endpoint supports dynamic
813 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
814 * endpoint to send the appropriate message to the peer to change the
815 * peers address lists.
817 * Adding and removing addresses from a connected association is
818 * optional functionality. Implementations that do not support this
819 * functionality should return EOPNOTSUPP.
821 * Basically do nothing but copying the addresses from user to kernel
822 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
823 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
824 * from userspace.
826 * We don't use copy_from_user() for optimization: we first do the
827 * sanity checks (buffer size -fast- and access check-healthy
828 * pointer); if all of those succeed, then we can alloc the memory
829 * (expensive operation) needed to copy the data to kernel. Then we do
830 * the copying without checking the user space area
831 * (__copy_from_user()).
833 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
834 * it.
836 * sk The sk of the socket
837 * addrs The pointer to the addresses in user land
838 * addrssize Size of the addrs buffer
839 * op Operation to perform (add or remove, see the flags of
840 * sctp_bindx)
842 * Returns 0 if ok, <0 errno code on error.
844 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
845 struct sockaddr __user *addrs,
846 int addrs_size, int op)
848 struct sockaddr *kaddrs;
849 int err;
850 int addrcnt = 0;
851 int walk_size = 0;
852 struct sockaddr *sa_addr;
853 void *addr_buf;
854 struct sctp_af *af;
856 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
857 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
859 if (unlikely(addrs_size <= 0))
860 return -EINVAL;
862 /* Check the user passed a healthy pointer. */
863 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
864 return -EFAULT;
866 /* Alloc space for the address array in kernel memory. */
867 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
868 if (unlikely(!kaddrs))
869 return -ENOMEM;
871 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
872 kfree(kaddrs);
873 return -EFAULT;
876 /* Walk through the addrs buffer and count the number of addresses. */
877 addr_buf = kaddrs;
878 while (walk_size < addrs_size) {
879 sa_addr = (struct sockaddr *)addr_buf;
880 af = sctp_get_af_specific(sa_addr->sa_family);
882 /* If the address family is not supported or if this address
883 * causes the address buffer to overflow return EINVAL.
885 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
886 kfree(kaddrs);
887 return -EINVAL;
889 addrcnt++;
890 addr_buf += af->sockaddr_len;
891 walk_size += af->sockaddr_len;
894 /* Do the work. */
895 switch (op) {
896 case SCTP_BINDX_ADD_ADDR:
897 err = sctp_bindx_add(sk, kaddrs, addrcnt);
898 if (err)
899 goto out;
900 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
901 break;
903 case SCTP_BINDX_REM_ADDR:
904 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
905 if (err)
906 goto out;
907 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
908 break;
910 default:
911 err = -EINVAL;
912 break;
915 out:
916 kfree(kaddrs);
918 return err;
921 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
923 * Common routine for handling connect() and sctp_connectx().
924 * Connect will come in with just a single address.
926 static int __sctp_connect(struct sock* sk,
927 struct sockaddr *kaddrs,
928 int addrs_size)
930 struct sctp_sock *sp;
931 struct sctp_endpoint *ep;
932 struct sctp_association *asoc = NULL;
933 struct sctp_association *asoc2;
934 struct sctp_transport *transport;
935 union sctp_addr to;
936 struct sctp_af *af;
937 sctp_scope_t scope;
938 long timeo;
939 int err = 0;
940 int addrcnt = 0;
941 int walk_size = 0;
942 struct sockaddr *sa_addr;
943 void *addr_buf;
945 sp = sctp_sk(sk);
946 ep = sp->ep;
948 /* connect() cannot be done on a socket that is already in ESTABLISHED
949 * state - UDP-style peeled off socket or a TCP-style socket that
950 * is already connected.
951 * It cannot be done even on a TCP-style listening socket.
953 if (sctp_sstate(sk, ESTABLISHED) ||
954 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
955 err = -EISCONN;
956 goto out_free;
959 /* Walk through the addrs buffer and count the number of addresses. */
960 addr_buf = kaddrs;
961 while (walk_size < addrs_size) {
962 sa_addr = (struct sockaddr *)addr_buf;
963 af = sctp_get_af_specific(sa_addr->sa_family);
965 /* If the address family is not supported or if this address
966 * causes the address buffer to overflow return EINVAL.
968 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
969 err = -EINVAL;
970 goto out_free;
973 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
974 af->sockaddr_len);
975 if (err)
976 goto out_free;
978 memcpy(&to, sa_addr, af->sockaddr_len);
979 to.v4.sin_port = ntohs(to.v4.sin_port);
981 /* Check if there already is a matching association on the
982 * endpoint (other than the one created here).
984 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
985 if (asoc2 && asoc2 != asoc) {
986 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
987 err = -EISCONN;
988 else
989 err = -EALREADY;
990 goto out_free;
993 /* If we could not find a matching association on the endpoint,
994 * make sure that there is no peeled-off association matching
995 * the peer address even on another socket.
997 if (sctp_endpoint_is_peeled_off(ep, &to)) {
998 err = -EADDRNOTAVAIL;
999 goto out_free;
1002 if (!asoc) {
1003 /* If a bind() or sctp_bindx() is not called prior to
1004 * an sctp_connectx() call, the system picks an
1005 * ephemeral port and will choose an address set
1006 * equivalent to binding with a wildcard address.
1008 if (!ep->base.bind_addr.port) {
1009 if (sctp_autobind(sk)) {
1010 err = -EAGAIN;
1011 goto out_free;
1013 } else {
1015 * If an unprivileged user inherits a 1-many
1016 * style socket with open associations on a
1017 * privileged port, it MAY be permitted to
1018 * accept new associations, but it SHOULD NOT
1019 * be permitted to open new associations.
1021 if (ep->base.bind_addr.port < PROT_SOCK &&
1022 !capable(CAP_NET_BIND_SERVICE)) {
1023 err = -EACCES;
1024 goto out_free;
1028 scope = sctp_scope(&to);
1029 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1030 if (!asoc) {
1031 err = -ENOMEM;
1032 goto out_free;
1036 /* Prime the peer's transport structures. */
1037 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1038 SCTP_UNKNOWN);
1039 if (!transport) {
1040 err = -ENOMEM;
1041 goto out_free;
1044 addrcnt++;
1045 addr_buf += af->sockaddr_len;
1046 walk_size += af->sockaddr_len;
1049 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1050 if (err < 0) {
1051 goto out_free;
1054 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1055 if (err < 0) {
1056 goto out_free;
1059 /* Initialize sk's dport and daddr for getpeername() */
1060 inet_sk(sk)->dport = htons(asoc->peer.port);
1061 af = sctp_get_af_specific(to.sa.sa_family);
1062 af->to_sk_daddr(&to, sk);
1064 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1065 err = sctp_wait_for_connect(asoc, &timeo);
1067 /* Don't free association on exit. */
1068 asoc = NULL;
1070 out_free:
1072 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1073 " kaddrs: %p err: %d\n",
1074 asoc, kaddrs, err);
1075 if (asoc)
1076 sctp_association_free(asoc);
1077 return err;
1080 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1082 * API 8.9
1083 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1085 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1086 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1087 * or IPv6 addresses.
1089 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1090 * Section 3.1.2 for this usage.
1092 * addrs is a pointer to an array of one or more socket addresses. Each
1093 * address is contained in its appropriate structure (i.e. struct
1094 * sockaddr_in or struct sockaddr_in6) the family of the address type
1095 * must be used to distengish the address length (note that this
1096 * representation is termed a "packed array" of addresses). The caller
1097 * specifies the number of addresses in the array with addrcnt.
1099 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1100 * -1, and sets errno to the appropriate error code.
1102 * For SCTP, the port given in each socket address must be the same, or
1103 * sctp_connectx() will fail, setting errno to EINVAL.
1105 * An application can use sctp_connectx to initiate an association with
1106 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1107 * allows a caller to specify multiple addresses at which a peer can be
1108 * reached. The way the SCTP stack uses the list of addresses to set up
1109 * the association is implementation dependant. This function only
1110 * specifies that the stack will try to make use of all the addresses in
1111 * the list when needed.
1113 * Note that the list of addresses passed in is only used for setting up
1114 * the association. It does not necessarily equal the set of addresses
1115 * the peer uses for the resulting association. If the caller wants to
1116 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1117 * retrieve them after the association has been set up.
1119 * Basically do nothing but copying the addresses from user to kernel
1120 * land and invoking either sctp_connectx(). This is used for tunneling
1121 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1123 * We don't use copy_from_user() for optimization: we first do the
1124 * sanity checks (buffer size -fast- and access check-healthy
1125 * pointer); if all of those succeed, then we can alloc the memory
1126 * (expensive operation) needed to copy the data to kernel. Then we do
1127 * the copying without checking the user space area
1128 * (__copy_from_user()).
1130 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1131 * it.
1133 * sk The sk of the socket
1134 * addrs The pointer to the addresses in user land
1135 * addrssize Size of the addrs buffer
1137 * Returns 0 if ok, <0 errno code on error.
1139 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1140 struct sockaddr __user *addrs,
1141 int addrs_size)
1143 int err = 0;
1144 struct sockaddr *kaddrs;
1146 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1147 __FUNCTION__, sk, addrs, addrs_size);
1149 if (unlikely(addrs_size <= 0))
1150 return -EINVAL;
1152 /* Check the user passed a healthy pointer. */
1153 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1154 return -EFAULT;
1156 /* Alloc space for the address array in kernel memory. */
1157 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
1158 if (unlikely(!kaddrs))
1159 return -ENOMEM;
1161 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1162 err = -EFAULT;
1163 } else {
1164 err = __sctp_connect(sk, kaddrs, addrs_size);
1167 kfree(kaddrs);
1168 return err;
1171 /* API 3.1.4 close() - UDP Style Syntax
1172 * Applications use close() to perform graceful shutdown (as described in
1173 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1174 * by a UDP-style socket.
1176 * The syntax is
1178 * ret = close(int sd);
1180 * sd - the socket descriptor of the associations to be closed.
1182 * To gracefully shutdown a specific association represented by the
1183 * UDP-style socket, an application should use the sendmsg() call,
1184 * passing no user data, but including the appropriate flag in the
1185 * ancillary data (see Section xxxx).
1187 * If sd in the close() call is a branched-off socket representing only
1188 * one association, the shutdown is performed on that association only.
1190 * 4.1.6 close() - TCP Style Syntax
1192 * Applications use close() to gracefully close down an association.
1194 * The syntax is:
1196 * int close(int sd);
1198 * sd - the socket descriptor of the association to be closed.
1200 * After an application calls close() on a socket descriptor, no further
1201 * socket operations will succeed on that descriptor.
1203 * API 7.1.4 SO_LINGER
1205 * An application using the TCP-style socket can use this option to
1206 * perform the SCTP ABORT primitive. The linger option structure is:
1208 * struct linger {
1209 * int l_onoff; // option on/off
1210 * int l_linger; // linger time
1211 * };
1213 * To enable the option, set l_onoff to 1. If the l_linger value is set
1214 * to 0, calling close() is the same as the ABORT primitive. If the
1215 * value is set to a negative value, the setsockopt() call will return
1216 * an error. If the value is set to a positive value linger_time, the
1217 * close() can be blocked for at most linger_time ms. If the graceful
1218 * shutdown phase does not finish during this period, close() will
1219 * return but the graceful shutdown phase continues in the system.
1221 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1223 struct sctp_endpoint *ep;
1224 struct sctp_association *asoc;
1225 struct list_head *pos, *temp;
1227 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1229 sctp_lock_sock(sk);
1230 sk->sk_shutdown = SHUTDOWN_MASK;
1232 ep = sctp_sk(sk)->ep;
1234 /* Walk all associations on a socket, not on an endpoint. */
1235 list_for_each_safe(pos, temp, &ep->asocs) {
1236 asoc = list_entry(pos, struct sctp_association, asocs);
1238 if (sctp_style(sk, TCP)) {
1239 /* A closed association can still be in the list if
1240 * it belongs to a TCP-style listening socket that is
1241 * not yet accepted. If so, free it. If not, send an
1242 * ABORT or SHUTDOWN based on the linger options.
1244 if (sctp_state(asoc, CLOSED)) {
1245 sctp_unhash_established(asoc);
1246 sctp_association_free(asoc);
1248 } else if (sock_flag(sk, SOCK_LINGER) &&
1249 !sk->sk_lingertime)
1250 sctp_primitive_ABORT(asoc, NULL);
1251 else
1252 sctp_primitive_SHUTDOWN(asoc, NULL);
1253 } else
1254 sctp_primitive_SHUTDOWN(asoc, NULL);
1257 /* Clean up any skbs sitting on the receive queue. */
1258 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1259 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1261 /* On a TCP-style socket, block for at most linger_time if set. */
1262 if (sctp_style(sk, TCP) && timeout)
1263 sctp_wait_for_close(sk, timeout);
1265 /* This will run the backlog queue. */
1266 sctp_release_sock(sk);
1268 /* Supposedly, no process has access to the socket, but
1269 * the net layers still may.
1271 sctp_local_bh_disable();
1272 sctp_bh_lock_sock(sk);
1274 /* Hold the sock, since sk_common_release() will put sock_put()
1275 * and we have just a little more cleanup.
1277 sock_hold(sk);
1278 sk_common_release(sk);
1280 sctp_bh_unlock_sock(sk);
1281 sctp_local_bh_enable();
1283 sock_put(sk);
1285 SCTP_DBG_OBJCNT_DEC(sock);
1288 /* Handle EPIPE error. */
1289 static int sctp_error(struct sock *sk, int flags, int err)
1291 if (err == -EPIPE)
1292 err = sock_error(sk) ? : -EPIPE;
1293 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1294 send_sig(SIGPIPE, current, 0);
1295 return err;
1298 /* API 3.1.3 sendmsg() - UDP Style Syntax
1300 * An application uses sendmsg() and recvmsg() calls to transmit data to
1301 * and receive data from its peer.
1303 * ssize_t sendmsg(int socket, const struct msghdr *message,
1304 * int flags);
1306 * socket - the socket descriptor of the endpoint.
1307 * message - pointer to the msghdr structure which contains a single
1308 * user message and possibly some ancillary data.
1310 * See Section 5 for complete description of the data
1311 * structures.
1313 * flags - flags sent or received with the user message, see Section
1314 * 5 for complete description of the flags.
1316 * Note: This function could use a rewrite especially when explicit
1317 * connect support comes in.
1319 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1321 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1323 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1324 struct msghdr *msg, size_t msg_len)
1326 struct sctp_sock *sp;
1327 struct sctp_endpoint *ep;
1328 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1329 struct sctp_transport *transport, *chunk_tp;
1330 struct sctp_chunk *chunk;
1331 union sctp_addr to;
1332 struct sockaddr *msg_name = NULL;
1333 struct sctp_sndrcvinfo default_sinfo = { 0 };
1334 struct sctp_sndrcvinfo *sinfo;
1335 struct sctp_initmsg *sinit;
1336 sctp_assoc_t associd = 0;
1337 sctp_cmsgs_t cmsgs = { NULL };
1338 int err;
1339 sctp_scope_t scope;
1340 long timeo;
1341 __u16 sinfo_flags = 0;
1342 struct sctp_datamsg *datamsg;
1343 struct list_head *pos;
1344 int msg_flags = msg->msg_flags;
1346 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1347 sk, msg, msg_len);
1349 err = 0;
1350 sp = sctp_sk(sk);
1351 ep = sp->ep;
1353 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1355 /* We cannot send a message over a TCP-style listening socket. */
1356 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1357 err = -EPIPE;
1358 goto out_nounlock;
1361 /* Parse out the SCTP CMSGs. */
1362 err = sctp_msghdr_parse(msg, &cmsgs);
1364 if (err) {
1365 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1366 goto out_nounlock;
1369 /* Fetch the destination address for this packet. This
1370 * address only selects the association--it is not necessarily
1371 * the address we will send to.
1372 * For a peeled-off socket, msg_name is ignored.
1374 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1375 int msg_namelen = msg->msg_namelen;
1377 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1378 msg_namelen);
1379 if (err)
1380 return err;
1382 if (msg_namelen > sizeof(to))
1383 msg_namelen = sizeof(to);
1384 memcpy(&to, msg->msg_name, msg_namelen);
1385 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1386 "0x%x:%u.\n",
1387 to.v4.sin_addr.s_addr, to.v4.sin_port);
1389 to.v4.sin_port = ntohs(to.v4.sin_port);
1390 msg_name = msg->msg_name;
1393 sinfo = cmsgs.info;
1394 sinit = cmsgs.init;
1396 /* Did the user specify SNDRCVINFO? */
1397 if (sinfo) {
1398 sinfo_flags = sinfo->sinfo_flags;
1399 associd = sinfo->sinfo_assoc_id;
1402 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1403 msg_len, sinfo_flags);
1405 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1406 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1407 err = -EINVAL;
1408 goto out_nounlock;
1411 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1412 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1413 * If SCTP_ABORT is set, the message length could be non zero with
1414 * the msg_iov set to the user abort reason.
1416 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1417 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1418 err = -EINVAL;
1419 goto out_nounlock;
1422 /* If SCTP_ADDR_OVER is set, there must be an address
1423 * specified in msg_name.
1425 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1426 err = -EINVAL;
1427 goto out_nounlock;
1430 transport = NULL;
1432 SCTP_DEBUG_PRINTK("About to look up association.\n");
1434 sctp_lock_sock(sk);
1436 /* If a msg_name has been specified, assume this is to be used. */
1437 if (msg_name) {
1438 /* Look for a matching association on the endpoint. */
1439 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1440 if (!asoc) {
1441 /* If we could not find a matching association on the
1442 * endpoint, make sure that it is not a TCP-style
1443 * socket that already has an association or there is
1444 * no peeled-off association on another socket.
1446 if ((sctp_style(sk, TCP) &&
1447 sctp_sstate(sk, ESTABLISHED)) ||
1448 sctp_endpoint_is_peeled_off(ep, &to)) {
1449 err = -EADDRNOTAVAIL;
1450 goto out_unlock;
1453 } else {
1454 asoc = sctp_id2assoc(sk, associd);
1455 if (!asoc) {
1456 err = -EPIPE;
1457 goto out_unlock;
1461 if (asoc) {
1462 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1464 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1465 * socket that has an association in CLOSED state. This can
1466 * happen when an accepted socket has an association that is
1467 * already CLOSED.
1469 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1470 err = -EPIPE;
1471 goto out_unlock;
1474 if (sinfo_flags & SCTP_EOF) {
1475 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1476 asoc);
1477 sctp_primitive_SHUTDOWN(asoc, NULL);
1478 err = 0;
1479 goto out_unlock;
1481 if (sinfo_flags & SCTP_ABORT) {
1482 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1483 sctp_primitive_ABORT(asoc, msg);
1484 err = 0;
1485 goto out_unlock;
1489 /* Do we need to create the association? */
1490 if (!asoc) {
1491 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1493 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1494 err = -EINVAL;
1495 goto out_unlock;
1498 /* Check for invalid stream against the stream counts,
1499 * either the default or the user specified stream counts.
1501 if (sinfo) {
1502 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1503 /* Check against the defaults. */
1504 if (sinfo->sinfo_stream >=
1505 sp->initmsg.sinit_num_ostreams) {
1506 err = -EINVAL;
1507 goto out_unlock;
1509 } else {
1510 /* Check against the requested. */
1511 if (sinfo->sinfo_stream >=
1512 sinit->sinit_num_ostreams) {
1513 err = -EINVAL;
1514 goto out_unlock;
1520 * API 3.1.2 bind() - UDP Style Syntax
1521 * If a bind() or sctp_bindx() is not called prior to a
1522 * sendmsg() call that initiates a new association, the
1523 * system picks an ephemeral port and will choose an address
1524 * set equivalent to binding with a wildcard address.
1526 if (!ep->base.bind_addr.port) {
1527 if (sctp_autobind(sk)) {
1528 err = -EAGAIN;
1529 goto out_unlock;
1531 } else {
1533 * If an unprivileged user inherits a one-to-many
1534 * style socket with open associations on a privileged
1535 * port, it MAY be permitted to accept new associations,
1536 * but it SHOULD NOT be permitted to open new
1537 * associations.
1539 if (ep->base.bind_addr.port < PROT_SOCK &&
1540 !capable(CAP_NET_BIND_SERVICE)) {
1541 err = -EACCES;
1542 goto out_unlock;
1546 scope = sctp_scope(&to);
1547 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1548 if (!new_asoc) {
1549 err = -ENOMEM;
1550 goto out_unlock;
1552 asoc = new_asoc;
1554 /* If the SCTP_INIT ancillary data is specified, set all
1555 * the association init values accordingly.
1557 if (sinit) {
1558 if (sinit->sinit_num_ostreams) {
1559 asoc->c.sinit_num_ostreams =
1560 sinit->sinit_num_ostreams;
1562 if (sinit->sinit_max_instreams) {
1563 asoc->c.sinit_max_instreams =
1564 sinit->sinit_max_instreams;
1566 if (sinit->sinit_max_attempts) {
1567 asoc->max_init_attempts
1568 = sinit->sinit_max_attempts;
1570 if (sinit->sinit_max_init_timeo) {
1571 asoc->max_init_timeo =
1572 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1576 /* Prime the peer's transport structures. */
1577 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1578 if (!transport) {
1579 err = -ENOMEM;
1580 goto out_free;
1582 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1583 if (err < 0) {
1584 err = -ENOMEM;
1585 goto out_free;
1589 /* ASSERT: we have a valid association at this point. */
1590 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1592 if (!sinfo) {
1593 /* If the user didn't specify SNDRCVINFO, make up one with
1594 * some defaults.
1596 default_sinfo.sinfo_stream = asoc->default_stream;
1597 default_sinfo.sinfo_flags = asoc->default_flags;
1598 default_sinfo.sinfo_ppid = asoc->default_ppid;
1599 default_sinfo.sinfo_context = asoc->default_context;
1600 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1601 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1602 sinfo = &default_sinfo;
1605 /* API 7.1.7, the sndbuf size per association bounds the
1606 * maximum size of data that can be sent in a single send call.
1608 if (msg_len > sk->sk_sndbuf) {
1609 err = -EMSGSIZE;
1610 goto out_free;
1613 /* If fragmentation is disabled and the message length exceeds the
1614 * association fragmentation point, return EMSGSIZE. The I-D
1615 * does not specify what this error is, but this looks like
1616 * a great fit.
1618 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1619 err = -EMSGSIZE;
1620 goto out_free;
1623 if (sinfo) {
1624 /* Check for invalid stream. */
1625 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1626 err = -EINVAL;
1627 goto out_free;
1631 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1632 if (!sctp_wspace(asoc)) {
1633 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1634 if (err)
1635 goto out_free;
1638 /* If an address is passed with the sendto/sendmsg call, it is used
1639 * to override the primary destination address in the TCP model, or
1640 * when SCTP_ADDR_OVER flag is set in the UDP model.
1642 if ((sctp_style(sk, TCP) && msg_name) ||
1643 (sinfo_flags & SCTP_ADDR_OVER)) {
1644 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1645 if (!chunk_tp) {
1646 err = -EINVAL;
1647 goto out_free;
1649 } else
1650 chunk_tp = NULL;
1652 /* Auto-connect, if we aren't connected already. */
1653 if (sctp_state(asoc, CLOSED)) {
1654 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1655 if (err < 0)
1656 goto out_free;
1657 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1660 /* Break the message into multiple chunks of maximum size. */
1661 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1662 if (!datamsg) {
1663 err = -ENOMEM;
1664 goto out_free;
1667 /* Now send the (possibly) fragmented message. */
1668 list_for_each(pos, &datamsg->chunks) {
1669 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1670 sctp_datamsg_track(chunk);
1672 /* Do accounting for the write space. */
1673 sctp_set_owner_w(chunk);
1675 chunk->transport = chunk_tp;
1677 /* Send it to the lower layers. Note: all chunks
1678 * must either fail or succeed. The lower layer
1679 * works that way today. Keep it that way or this
1680 * breaks.
1682 err = sctp_primitive_SEND(asoc, chunk);
1683 /* Did the lower layer accept the chunk? */
1684 if (err)
1685 sctp_chunk_free(chunk);
1686 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1689 sctp_datamsg_free(datamsg);
1690 if (err)
1691 goto out_free;
1692 else
1693 err = msg_len;
1695 /* If we are already past ASSOCIATE, the lower
1696 * layers are responsible for association cleanup.
1698 goto out_unlock;
1700 out_free:
1701 if (new_asoc)
1702 sctp_association_free(asoc);
1703 out_unlock:
1704 sctp_release_sock(sk);
1706 out_nounlock:
1707 return sctp_error(sk, msg_flags, err);
1709 #if 0
1710 do_sock_err:
1711 if (msg_len)
1712 err = msg_len;
1713 else
1714 err = sock_error(sk);
1715 goto out;
1717 do_interrupted:
1718 if (msg_len)
1719 err = msg_len;
1720 goto out;
1721 #endif /* 0 */
1724 /* This is an extended version of skb_pull() that removes the data from the
1725 * start of a skb even when data is spread across the list of skb's in the
1726 * frag_list. len specifies the total amount of data that needs to be removed.
1727 * when 'len' bytes could be removed from the skb, it returns 0.
1728 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1729 * could not be removed.
1731 static int sctp_skb_pull(struct sk_buff *skb, int len)
1733 struct sk_buff *list;
1734 int skb_len = skb_headlen(skb);
1735 int rlen;
1737 if (len <= skb_len) {
1738 __skb_pull(skb, len);
1739 return 0;
1741 len -= skb_len;
1742 __skb_pull(skb, skb_len);
1744 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1745 rlen = sctp_skb_pull(list, len);
1746 skb->len -= (len-rlen);
1747 skb->data_len -= (len-rlen);
1749 if (!rlen)
1750 return 0;
1752 len = rlen;
1755 return len;
1758 /* API 3.1.3 recvmsg() - UDP Style Syntax
1760 * ssize_t recvmsg(int socket, struct msghdr *message,
1761 * int flags);
1763 * socket - the socket descriptor of the endpoint.
1764 * message - pointer to the msghdr structure which contains a single
1765 * user message and possibly some ancillary data.
1767 * See Section 5 for complete description of the data
1768 * structures.
1770 * flags - flags sent or received with the user message, see Section
1771 * 5 for complete description of the flags.
1773 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1775 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1776 struct msghdr *msg, size_t len, int noblock,
1777 int flags, int *addr_len)
1779 struct sctp_ulpevent *event = NULL;
1780 struct sctp_sock *sp = sctp_sk(sk);
1781 struct sk_buff *skb;
1782 int copied;
1783 int err = 0;
1784 int skb_len;
1786 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1787 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1788 "len", len, "knoblauch", noblock,
1789 "flags", flags, "addr_len", addr_len);
1791 sctp_lock_sock(sk);
1793 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1794 err = -ENOTCONN;
1795 goto out;
1798 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1799 if (!skb)
1800 goto out;
1802 /* Get the total length of the skb including any skb's in the
1803 * frag_list.
1805 skb_len = skb->len;
1807 copied = skb_len;
1808 if (copied > len)
1809 copied = len;
1811 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1813 event = sctp_skb2event(skb);
1815 if (err)
1816 goto out_free;
1818 sock_recv_timestamp(msg, sk, skb);
1819 if (sctp_ulpevent_is_notification(event)) {
1820 msg->msg_flags |= MSG_NOTIFICATION;
1821 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1822 } else {
1823 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1826 /* Check if we allow SCTP_SNDRCVINFO. */
1827 if (sp->subscribe.sctp_data_io_event)
1828 sctp_ulpevent_read_sndrcvinfo(event, msg);
1829 #if 0
1830 /* FIXME: we should be calling IP/IPv6 layers. */
1831 if (sk->sk_protinfo.af_inet.cmsg_flags)
1832 ip_cmsg_recv(msg, skb);
1833 #endif
1835 err = copied;
1837 /* If skb's length exceeds the user's buffer, update the skb and
1838 * push it back to the receive_queue so that the next call to
1839 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1841 if (skb_len > copied) {
1842 msg->msg_flags &= ~MSG_EOR;
1843 if (flags & MSG_PEEK)
1844 goto out_free;
1845 sctp_skb_pull(skb, copied);
1846 skb_queue_head(&sk->sk_receive_queue, skb);
1848 /* When only partial message is copied to the user, increase
1849 * rwnd by that amount. If all the data in the skb is read,
1850 * rwnd is updated when the event is freed.
1852 sctp_assoc_rwnd_increase(event->asoc, copied);
1853 goto out;
1854 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1855 (event->msg_flags & MSG_EOR))
1856 msg->msg_flags |= MSG_EOR;
1857 else
1858 msg->msg_flags &= ~MSG_EOR;
1860 out_free:
1861 if (flags & MSG_PEEK) {
1862 /* Release the skb reference acquired after peeking the skb in
1863 * sctp_skb_recv_datagram().
1865 kfree_skb(skb);
1866 } else {
1867 /* Free the event which includes releasing the reference to
1868 * the owner of the skb, freeing the skb and updating the
1869 * rwnd.
1871 sctp_ulpevent_free(event);
1873 out:
1874 sctp_release_sock(sk);
1875 return err;
1878 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1880 * This option is a on/off flag. If enabled no SCTP message
1881 * fragmentation will be performed. Instead if a message being sent
1882 * exceeds the current PMTU size, the message will NOT be sent and
1883 * instead a error will be indicated to the user.
1885 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1886 char __user *optval, int optlen)
1888 int val;
1890 if (optlen < sizeof(int))
1891 return -EINVAL;
1893 if (get_user(val, (int __user *)optval))
1894 return -EFAULT;
1896 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1898 return 0;
1901 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1902 int optlen)
1904 if (optlen != sizeof(struct sctp_event_subscribe))
1905 return -EINVAL;
1906 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1907 return -EFAULT;
1908 return 0;
1911 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1913 * This socket option is applicable to the UDP-style socket only. When
1914 * set it will cause associations that are idle for more than the
1915 * specified number of seconds to automatically close. An association
1916 * being idle is defined an association that has NOT sent or received
1917 * user data. The special value of '0' indicates that no automatic
1918 * close of any associations should be performed. The option expects an
1919 * integer defining the number of seconds of idle time before an
1920 * association is closed.
1922 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1923 int optlen)
1925 struct sctp_sock *sp = sctp_sk(sk);
1927 /* Applicable to UDP-style socket only */
1928 if (sctp_style(sk, TCP))
1929 return -EOPNOTSUPP;
1930 if (optlen != sizeof(int))
1931 return -EINVAL;
1932 if (copy_from_user(&sp->autoclose, optval, optlen))
1933 return -EFAULT;
1935 return 0;
1938 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1940 * Applications can enable or disable heartbeats for any peer address of
1941 * an association, modify an address's heartbeat interval, force a
1942 * heartbeat to be sent immediately, and adjust the address's maximum
1943 * number of retransmissions sent before an address is considered
1944 * unreachable. The following structure is used to access and modify an
1945 * address's parameters:
1947 * struct sctp_paddrparams {
1948 * sctp_assoc_t spp_assoc_id;
1949 * struct sockaddr_storage spp_address;
1950 * uint32_t spp_hbinterval;
1951 * uint16_t spp_pathmaxrxt;
1952 * };
1954 * spp_assoc_id - (UDP style socket) This is filled in the application,
1955 * and identifies the association for this query.
1956 * spp_address - This specifies which address is of interest.
1957 * spp_hbinterval - This contains the value of the heartbeat interval,
1958 * in milliseconds. A value of 0, when modifying the
1959 * parameter, specifies that the heartbeat on this
1960 * address should be disabled. A value of UINT32_MAX
1961 * (4294967295), when modifying the parameter,
1962 * specifies that a heartbeat should be sent
1963 * immediately to the peer address, and the current
1964 * interval should remain unchanged.
1965 * spp_pathmaxrxt - This contains the maximum number of
1966 * retransmissions before this address shall be
1967 * considered unreachable.
1969 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1970 char __user *optval, int optlen)
1972 struct sctp_paddrparams params;
1973 struct sctp_transport *trans;
1974 int error;
1976 if (optlen != sizeof(struct sctp_paddrparams))
1977 return -EINVAL;
1978 if (copy_from_user(&params, optval, optlen))
1979 return -EFAULT;
1982 * API 7. Socket Options (setting the default value for the endpoint)
1983 * All options that support specific settings on an association by
1984 * filling in either an association id variable or a sockaddr_storage
1985 * SHOULD also support setting of the same value for the entire endpoint
1986 * (i.e. future associations). To accomplish this the following logic is
1987 * used when setting one of these options:
1989 * c) If neither the sockaddr_storage or association identification is
1990 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1991 * the association identification is 0, the settings are a default
1992 * and to be applied to the endpoint (all future associations).
1995 /* update default value for endpoint (all future associations) */
1996 if (!params.spp_assoc_id &&
1997 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
1998 /* Manual heartbeat on an endpoint is invalid. */
1999 if (0xffffffff == params.spp_hbinterval)
2000 return -EINVAL;
2001 else if (params.spp_hbinterval)
2002 sctp_sk(sk)->paddrparam.spp_hbinterval =
2003 params.spp_hbinterval;
2004 if (params.spp_pathmaxrxt)
2005 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
2006 params.spp_pathmaxrxt;
2007 return 0;
2010 trans = sctp_addr_id2transport(sk, &params.spp_address,
2011 params.spp_assoc_id);
2012 if (!trans)
2013 return -EINVAL;
2015 /* Applications can enable or disable heartbeats for any peer address
2016 * of an association, modify an address's heartbeat interval, force a
2017 * heartbeat to be sent immediately, and adjust the address's maximum
2018 * number of retransmissions sent before an address is considered
2019 * unreachable.
2021 * The value of the heartbeat interval, in milliseconds. A value of
2022 * UINT32_MAX (4294967295), when modifying the parameter, specifies
2023 * that a heartbeat should be sent immediately to the peer address,
2024 * and the current interval should remain unchanged.
2026 if (0xffffffff == params.spp_hbinterval) {
2027 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2028 if (error)
2029 return error;
2030 } else {
2031 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2032 * when modifying the parameter, specifies that the heartbeat on this
2033 * address should be disabled.
2035 if (params.spp_hbinterval) {
2036 trans->hb_allowed = 1;
2037 trans->hb_interval =
2038 msecs_to_jiffies(params.spp_hbinterval);
2039 } else
2040 trans->hb_allowed = 0;
2043 /* spp_pathmaxrxt contains the maximum number of retransmissions
2044 * before this address shall be considered unreachable.
2046 if (params.spp_pathmaxrxt)
2047 trans->max_retrans = params.spp_pathmaxrxt;
2049 return 0;
2052 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2054 * Applications can specify protocol parameters for the default association
2055 * initialization. The option name argument to setsockopt() and getsockopt()
2056 * is SCTP_INITMSG.
2058 * Setting initialization parameters is effective only on an unconnected
2059 * socket (for UDP-style sockets only future associations are effected
2060 * by the change). With TCP-style sockets, this option is inherited by
2061 * sockets derived from a listener socket.
2063 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2065 struct sctp_initmsg sinit;
2066 struct sctp_sock *sp = sctp_sk(sk);
2068 if (optlen != sizeof(struct sctp_initmsg))
2069 return -EINVAL;
2070 if (copy_from_user(&sinit, optval, optlen))
2071 return -EFAULT;
2073 if (sinit.sinit_num_ostreams)
2074 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2075 if (sinit.sinit_max_instreams)
2076 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2077 if (sinit.sinit_max_attempts)
2078 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2079 if (sinit.sinit_max_init_timeo)
2080 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2082 return 0;
2086 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2088 * Applications that wish to use the sendto() system call may wish to
2089 * specify a default set of parameters that would normally be supplied
2090 * through the inclusion of ancillary data. This socket option allows
2091 * such an application to set the default sctp_sndrcvinfo structure.
2092 * The application that wishes to use this socket option simply passes
2093 * in to this call the sctp_sndrcvinfo structure defined in Section
2094 * 5.2.2) The input parameters accepted by this call include
2095 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2096 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2097 * to this call if the caller is using the UDP model.
2099 static int sctp_setsockopt_default_send_param(struct sock *sk,
2100 char __user *optval, int optlen)
2102 struct sctp_sndrcvinfo info;
2103 struct sctp_association *asoc;
2104 struct sctp_sock *sp = sctp_sk(sk);
2106 if (optlen != sizeof(struct sctp_sndrcvinfo))
2107 return -EINVAL;
2108 if (copy_from_user(&info, optval, optlen))
2109 return -EFAULT;
2111 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2112 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2113 return -EINVAL;
2115 if (asoc) {
2116 asoc->default_stream = info.sinfo_stream;
2117 asoc->default_flags = info.sinfo_flags;
2118 asoc->default_ppid = info.sinfo_ppid;
2119 asoc->default_context = info.sinfo_context;
2120 asoc->default_timetolive = info.sinfo_timetolive;
2121 } else {
2122 sp->default_stream = info.sinfo_stream;
2123 sp->default_flags = info.sinfo_flags;
2124 sp->default_ppid = info.sinfo_ppid;
2125 sp->default_context = info.sinfo_context;
2126 sp->default_timetolive = info.sinfo_timetolive;
2129 return 0;
2132 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2134 * Requests that the local SCTP stack use the enclosed peer address as
2135 * the association primary. The enclosed address must be one of the
2136 * association peer's addresses.
2138 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2139 int optlen)
2141 struct sctp_prim prim;
2142 struct sctp_transport *trans;
2144 if (optlen != sizeof(struct sctp_prim))
2145 return -EINVAL;
2147 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2148 return -EFAULT;
2150 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2151 if (!trans)
2152 return -EINVAL;
2154 sctp_assoc_set_primary(trans->asoc, trans);
2156 return 0;
2160 * 7.1.5 SCTP_NODELAY
2162 * Turn on/off any Nagle-like algorithm. This means that packets are
2163 * generally sent as soon as possible and no unnecessary delays are
2164 * introduced, at the cost of more packets in the network. Expects an
2165 * integer boolean flag.
2167 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2168 int optlen)
2170 int val;
2172 if (optlen < sizeof(int))
2173 return -EINVAL;
2174 if (get_user(val, (int __user *)optval))
2175 return -EFAULT;
2177 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2178 return 0;
2183 * 7.1.1 SCTP_RTOINFO
2185 * The protocol parameters used to initialize and bound retransmission
2186 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2187 * and modify these parameters.
2188 * All parameters are time values, in milliseconds. A value of 0, when
2189 * modifying the parameters, indicates that the current value should not
2190 * be changed.
2193 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2194 struct sctp_rtoinfo rtoinfo;
2195 struct sctp_association *asoc;
2197 if (optlen != sizeof (struct sctp_rtoinfo))
2198 return -EINVAL;
2200 if (copy_from_user(&rtoinfo, optval, optlen))
2201 return -EFAULT;
2203 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2205 /* Set the values to the specific association */
2206 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2207 return -EINVAL;
2209 if (asoc) {
2210 if (rtoinfo.srto_initial != 0)
2211 asoc->rto_initial =
2212 msecs_to_jiffies(rtoinfo.srto_initial);
2213 if (rtoinfo.srto_max != 0)
2214 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2215 if (rtoinfo.srto_min != 0)
2216 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2217 } else {
2218 /* If there is no association or the association-id = 0
2219 * set the values to the endpoint.
2221 struct sctp_sock *sp = sctp_sk(sk);
2223 if (rtoinfo.srto_initial != 0)
2224 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2225 if (rtoinfo.srto_max != 0)
2226 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2227 if (rtoinfo.srto_min != 0)
2228 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2231 return 0;
2236 * 7.1.2 SCTP_ASSOCINFO
2238 * This option is used to tune the the maximum retransmission attempts
2239 * of the association.
2240 * Returns an error if the new association retransmission value is
2241 * greater than the sum of the retransmission value of the peer.
2242 * See [SCTP] for more information.
2245 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2248 struct sctp_assocparams assocparams;
2249 struct sctp_association *asoc;
2251 if (optlen != sizeof(struct sctp_assocparams))
2252 return -EINVAL;
2253 if (copy_from_user(&assocparams, optval, optlen))
2254 return -EFAULT;
2256 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2258 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2259 return -EINVAL;
2261 /* Set the values to the specific association */
2262 if (asoc) {
2263 if (assocparams.sasoc_asocmaxrxt != 0)
2264 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2265 if (assocparams.sasoc_cookie_life != 0) {
2266 asoc->cookie_life.tv_sec =
2267 assocparams.sasoc_cookie_life / 1000;
2268 asoc->cookie_life.tv_usec =
2269 (assocparams.sasoc_cookie_life % 1000)
2270 * 1000;
2272 } else {
2273 /* Set the values to the endpoint */
2274 struct sctp_sock *sp = sctp_sk(sk);
2276 if (assocparams.sasoc_asocmaxrxt != 0)
2277 sp->assocparams.sasoc_asocmaxrxt =
2278 assocparams.sasoc_asocmaxrxt;
2279 if (assocparams.sasoc_cookie_life != 0)
2280 sp->assocparams.sasoc_cookie_life =
2281 assocparams.sasoc_cookie_life;
2283 return 0;
2287 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2289 * This socket option is a boolean flag which turns on or off mapped V4
2290 * addresses. If this option is turned on and the socket is type
2291 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2292 * If this option is turned off, then no mapping will be done of V4
2293 * addresses and a user will receive both PF_INET6 and PF_INET type
2294 * addresses on the socket.
2296 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2298 int val;
2299 struct sctp_sock *sp = sctp_sk(sk);
2301 if (optlen < sizeof(int))
2302 return -EINVAL;
2303 if (get_user(val, (int __user *)optval))
2304 return -EFAULT;
2305 if (val)
2306 sp->v4mapped = 1;
2307 else
2308 sp->v4mapped = 0;
2310 return 0;
2314 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2316 * This socket option specifies the maximum size to put in any outgoing
2317 * SCTP chunk. If a message is larger than this size it will be
2318 * fragmented by SCTP into the specified size. Note that the underlying
2319 * SCTP implementation may fragment into smaller sized chunks when the
2320 * PMTU of the underlying association is smaller than the value set by
2321 * the user.
2323 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2325 struct sctp_association *asoc;
2326 struct list_head *pos;
2327 struct sctp_sock *sp = sctp_sk(sk);
2328 int val;
2330 if (optlen < sizeof(int))
2331 return -EINVAL;
2332 if (get_user(val, (int __user *)optval))
2333 return -EFAULT;
2334 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2335 return -EINVAL;
2336 sp->user_frag = val;
2338 /* Update the frag_point of the existing associations. */
2339 list_for_each(pos, &(sp->ep->asocs)) {
2340 asoc = list_entry(pos, struct sctp_association, asocs);
2341 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2344 return 0;
2349 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2351 * Requests that the peer mark the enclosed address as the association
2352 * primary. The enclosed address must be one of the association's
2353 * locally bound addresses. The following structure is used to make a
2354 * set primary request:
2356 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2357 int optlen)
2359 struct sctp_sock *sp;
2360 struct sctp_endpoint *ep;
2361 struct sctp_association *asoc = NULL;
2362 struct sctp_setpeerprim prim;
2363 struct sctp_chunk *chunk;
2364 int err;
2366 sp = sctp_sk(sk);
2367 ep = sp->ep;
2369 if (!sctp_addip_enable)
2370 return -EPERM;
2372 if (optlen != sizeof(struct sctp_setpeerprim))
2373 return -EINVAL;
2375 if (copy_from_user(&prim, optval, optlen))
2376 return -EFAULT;
2378 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2379 if (!asoc)
2380 return -EINVAL;
2382 if (!asoc->peer.asconf_capable)
2383 return -EPERM;
2385 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2386 return -EPERM;
2388 if (!sctp_state(asoc, ESTABLISHED))
2389 return -ENOTCONN;
2391 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2392 return -EADDRNOTAVAIL;
2394 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2395 chunk = sctp_make_asconf_set_prim(asoc,
2396 (union sctp_addr *)&prim.sspp_addr);
2397 if (!chunk)
2398 return -ENOMEM;
2400 err = sctp_send_asconf(asoc, chunk);
2402 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2404 return err;
2407 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2408 int optlen)
2410 struct sctp_setadaption adaption;
2412 if (optlen != sizeof(struct sctp_setadaption))
2413 return -EINVAL;
2414 if (copy_from_user(&adaption, optval, optlen))
2415 return -EFAULT;
2417 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2419 return 0;
2422 /* API 6.2 setsockopt(), getsockopt()
2424 * Applications use setsockopt() and getsockopt() to set or retrieve
2425 * socket options. Socket options are used to change the default
2426 * behavior of sockets calls. They are described in Section 7.
2428 * The syntax is:
2430 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2431 * int __user *optlen);
2432 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2433 * int optlen);
2435 * sd - the socket descript.
2436 * level - set to IPPROTO_SCTP for all SCTP options.
2437 * optname - the option name.
2438 * optval - the buffer to store the value of the option.
2439 * optlen - the size of the buffer.
2441 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2442 char __user *optval, int optlen)
2444 int retval = 0;
2446 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2447 sk, optname);
2449 /* I can hardly begin to describe how wrong this is. This is
2450 * so broken as to be worse than useless. The API draft
2451 * REALLY is NOT helpful here... I am not convinced that the
2452 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2453 * are at all well-founded.
2455 if (level != SOL_SCTP) {
2456 struct sctp_af *af = sctp_sk(sk)->pf->af;
2457 retval = af->setsockopt(sk, level, optname, optval, optlen);
2458 goto out_nounlock;
2461 sctp_lock_sock(sk);
2463 switch (optname) {
2464 case SCTP_SOCKOPT_BINDX_ADD:
2465 /* 'optlen' is the size of the addresses buffer. */
2466 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2467 optlen, SCTP_BINDX_ADD_ADDR);
2468 break;
2470 case SCTP_SOCKOPT_BINDX_REM:
2471 /* 'optlen' is the size of the addresses buffer. */
2472 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2473 optlen, SCTP_BINDX_REM_ADDR);
2474 break;
2476 case SCTP_SOCKOPT_CONNECTX:
2477 /* 'optlen' is the size of the addresses buffer. */
2478 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2479 optlen);
2480 break;
2482 case SCTP_DISABLE_FRAGMENTS:
2483 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2484 break;
2486 case SCTP_EVENTS:
2487 retval = sctp_setsockopt_events(sk, optval, optlen);
2488 break;
2490 case SCTP_AUTOCLOSE:
2491 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2492 break;
2494 case SCTP_PEER_ADDR_PARAMS:
2495 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2496 break;
2498 case SCTP_INITMSG:
2499 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2500 break;
2501 case SCTP_DEFAULT_SEND_PARAM:
2502 retval = sctp_setsockopt_default_send_param(sk, optval,
2503 optlen);
2504 break;
2505 case SCTP_PRIMARY_ADDR:
2506 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2507 break;
2508 case SCTP_SET_PEER_PRIMARY_ADDR:
2509 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2510 break;
2511 case SCTP_NODELAY:
2512 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2513 break;
2514 case SCTP_RTOINFO:
2515 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2516 break;
2517 case SCTP_ASSOCINFO:
2518 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2519 break;
2520 case SCTP_I_WANT_MAPPED_V4_ADDR:
2521 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2522 break;
2523 case SCTP_MAXSEG:
2524 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2525 break;
2526 case SCTP_ADAPTION_LAYER:
2527 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2528 break;
2530 default:
2531 retval = -ENOPROTOOPT;
2532 break;
2535 sctp_release_sock(sk);
2537 out_nounlock:
2538 return retval;
2541 /* API 3.1.6 connect() - UDP Style Syntax
2543 * An application may use the connect() call in the UDP model to initiate an
2544 * association without sending data.
2546 * The syntax is:
2548 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2550 * sd: the socket descriptor to have a new association added to.
2552 * nam: the address structure (either struct sockaddr_in or struct
2553 * sockaddr_in6 defined in RFC2553 [7]).
2555 * len: the size of the address.
2557 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2558 int addr_len)
2560 int err = 0;
2561 struct sctp_af *af;
2563 sctp_lock_sock(sk);
2565 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2566 __FUNCTION__, sk, addr, addr_len);
2568 /* Validate addr_len before calling common connect/connectx routine. */
2569 af = sctp_get_af_specific(addr->sa_family);
2570 if (!af || addr_len < af->sockaddr_len) {
2571 err = -EINVAL;
2572 } else {
2573 /* Pass correct addr len to common routine (so it knows there
2574 * is only one address being passed.
2576 err = __sctp_connect(sk, addr, af->sockaddr_len);
2579 sctp_release_sock(sk);
2580 return err;
2583 /* FIXME: Write comments. */
2584 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2586 return -EOPNOTSUPP; /* STUB */
2589 /* 4.1.4 accept() - TCP Style Syntax
2591 * Applications use accept() call to remove an established SCTP
2592 * association from the accept queue of the endpoint. A new socket
2593 * descriptor will be returned from accept() to represent the newly
2594 * formed association.
2596 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2598 struct sctp_sock *sp;
2599 struct sctp_endpoint *ep;
2600 struct sock *newsk = NULL;
2601 struct sctp_association *asoc;
2602 long timeo;
2603 int error = 0;
2605 sctp_lock_sock(sk);
2607 sp = sctp_sk(sk);
2608 ep = sp->ep;
2610 if (!sctp_style(sk, TCP)) {
2611 error = -EOPNOTSUPP;
2612 goto out;
2615 if (!sctp_sstate(sk, LISTENING)) {
2616 error = -EINVAL;
2617 goto out;
2620 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2622 error = sctp_wait_for_accept(sk, timeo);
2623 if (error)
2624 goto out;
2626 /* We treat the list of associations on the endpoint as the accept
2627 * queue and pick the first association on the list.
2629 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2631 newsk = sp->pf->create_accept_sk(sk, asoc);
2632 if (!newsk) {
2633 error = -ENOMEM;
2634 goto out;
2637 /* Populate the fields of the newsk from the oldsk and migrate the
2638 * asoc to the newsk.
2640 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2642 out:
2643 sctp_release_sock(sk);
2644 *err = error;
2645 return newsk;
2648 /* The SCTP ioctl handler. */
2649 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2651 return -ENOIOCTLCMD;
2654 /* This is the function which gets called during socket creation to
2655 * initialized the SCTP-specific portion of the sock.
2656 * The sock structure should already be zero-filled memory.
2658 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2660 struct sctp_endpoint *ep;
2661 struct sctp_sock *sp;
2663 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2665 sp = sctp_sk(sk);
2667 /* Initialize the SCTP per socket area. */
2668 switch (sk->sk_type) {
2669 case SOCK_SEQPACKET:
2670 sp->type = SCTP_SOCKET_UDP;
2671 break;
2672 case SOCK_STREAM:
2673 sp->type = SCTP_SOCKET_TCP;
2674 break;
2675 default:
2676 return -ESOCKTNOSUPPORT;
2679 /* Initialize default send parameters. These parameters can be
2680 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2682 sp->default_stream = 0;
2683 sp->default_ppid = 0;
2684 sp->default_flags = 0;
2685 sp->default_context = 0;
2686 sp->default_timetolive = 0;
2688 /* Initialize default setup parameters. These parameters
2689 * can be modified with the SCTP_INITMSG socket option or
2690 * overridden by the SCTP_INIT CMSG.
2692 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2693 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2694 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2695 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2697 /* Initialize default RTO related parameters. These parameters can
2698 * be modified for with the SCTP_RTOINFO socket option.
2700 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2701 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2702 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2704 /* Initialize default association related parameters. These parameters
2705 * can be modified with the SCTP_ASSOCINFO socket option.
2707 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2708 sp->assocparams.sasoc_number_peer_destinations = 0;
2709 sp->assocparams.sasoc_peer_rwnd = 0;
2710 sp->assocparams.sasoc_local_rwnd = 0;
2711 sp->assocparams.sasoc_cookie_life =
2712 jiffies_to_msecs(sctp_valid_cookie_life);
2714 /* Initialize default event subscriptions. By default, all the
2715 * options are off.
2717 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2719 /* Default Peer Address Parameters. These defaults can
2720 * be modified via SCTP_PEER_ADDR_PARAMS
2722 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2723 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2725 /* If enabled no SCTP message fragmentation will be performed.
2726 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2728 sp->disable_fragments = 0;
2730 /* Turn on/off any Nagle-like algorithm. */
2731 sp->nodelay = 1;
2733 /* Enable by default. */
2734 sp->v4mapped = 1;
2736 /* Auto-close idle associations after the configured
2737 * number of seconds. A value of 0 disables this
2738 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2739 * for UDP-style sockets only.
2741 sp->autoclose = 0;
2743 /* User specified fragmentation limit. */
2744 sp->user_frag = 0;
2746 sp->adaption_ind = 0;
2748 sp->pf = sctp_get_pf_specific(sk->sk_family);
2750 /* Control variables for partial data delivery. */
2751 sp->pd_mode = 0;
2752 skb_queue_head_init(&sp->pd_lobby);
2754 /* Create a per socket endpoint structure. Even if we
2755 * change the data structure relationships, this may still
2756 * be useful for storing pre-connect address information.
2758 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2759 if (!ep)
2760 return -ENOMEM;
2762 sp->ep = ep;
2763 sp->hmac = NULL;
2765 SCTP_DBG_OBJCNT_INC(sock);
2766 return 0;
2769 /* Cleanup any SCTP per socket resources. */
2770 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2772 struct sctp_endpoint *ep;
2774 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2776 /* Release our hold on the endpoint. */
2777 ep = sctp_sk(sk)->ep;
2778 sctp_endpoint_free(ep);
2780 return 0;
2783 /* API 4.1.7 shutdown() - TCP Style Syntax
2784 * int shutdown(int socket, int how);
2786 * sd - the socket descriptor of the association to be closed.
2787 * how - Specifies the type of shutdown. The values are
2788 * as follows:
2789 * SHUT_RD
2790 * Disables further receive operations. No SCTP
2791 * protocol action is taken.
2792 * SHUT_WR
2793 * Disables further send operations, and initiates
2794 * the SCTP shutdown sequence.
2795 * SHUT_RDWR
2796 * Disables further send and receive operations
2797 * and initiates the SCTP shutdown sequence.
2799 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2801 struct sctp_endpoint *ep;
2802 struct sctp_association *asoc;
2804 if (!sctp_style(sk, TCP))
2805 return;
2807 if (how & SEND_SHUTDOWN) {
2808 ep = sctp_sk(sk)->ep;
2809 if (!list_empty(&ep->asocs)) {
2810 asoc = list_entry(ep->asocs.next,
2811 struct sctp_association, asocs);
2812 sctp_primitive_SHUTDOWN(asoc, NULL);
2817 /* 7.2.1 Association Status (SCTP_STATUS)
2819 * Applications can retrieve current status information about an
2820 * association, including association state, peer receiver window size,
2821 * number of unacked data chunks, and number of data chunks pending
2822 * receipt. This information is read-only.
2824 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2825 char __user *optval,
2826 int __user *optlen)
2828 struct sctp_status status;
2829 struct sctp_association *asoc = NULL;
2830 struct sctp_transport *transport;
2831 sctp_assoc_t associd;
2832 int retval = 0;
2834 if (len != sizeof(status)) {
2835 retval = -EINVAL;
2836 goto out;
2839 if (copy_from_user(&status, optval, sizeof(status))) {
2840 retval = -EFAULT;
2841 goto out;
2844 associd = status.sstat_assoc_id;
2845 asoc = sctp_id2assoc(sk, associd);
2846 if (!asoc) {
2847 retval = -EINVAL;
2848 goto out;
2851 transport = asoc->peer.primary_path;
2853 status.sstat_assoc_id = sctp_assoc2id(asoc);
2854 status.sstat_state = asoc->state;
2855 status.sstat_rwnd = asoc->peer.rwnd;
2856 status.sstat_unackdata = asoc->unack_data;
2858 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2859 status.sstat_instrms = asoc->c.sinit_max_instreams;
2860 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2861 status.sstat_fragmentation_point = asoc->frag_point;
2862 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2863 memcpy(&status.sstat_primary.spinfo_address,
2864 &(transport->ipaddr), sizeof(union sctp_addr));
2865 /* Map ipv4 address into v4-mapped-on-v6 address. */
2866 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2867 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2868 status.sstat_primary.spinfo_state = transport->state;
2869 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2870 status.sstat_primary.spinfo_srtt = transport->srtt;
2871 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2872 status.sstat_primary.spinfo_mtu = transport->pmtu;
2874 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
2875 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
2877 if (put_user(len, optlen)) {
2878 retval = -EFAULT;
2879 goto out;
2882 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
2883 len, status.sstat_state, status.sstat_rwnd,
2884 status.sstat_assoc_id);
2886 if (copy_to_user(optval, &status, len)) {
2887 retval = -EFAULT;
2888 goto out;
2891 out:
2892 return (retval);
2896 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2898 * Applications can retrieve information about a specific peer address
2899 * of an association, including its reachability state, congestion
2900 * window, and retransmission timer values. This information is
2901 * read-only.
2903 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2904 char __user *optval,
2905 int __user *optlen)
2907 struct sctp_paddrinfo pinfo;
2908 struct sctp_transport *transport;
2909 int retval = 0;
2911 if (len != sizeof(pinfo)) {
2912 retval = -EINVAL;
2913 goto out;
2916 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2917 retval = -EFAULT;
2918 goto out;
2921 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2922 pinfo.spinfo_assoc_id);
2923 if (!transport)
2924 return -EINVAL;
2926 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2927 pinfo.spinfo_state = transport->state;
2928 pinfo.spinfo_cwnd = transport->cwnd;
2929 pinfo.spinfo_srtt = transport->srtt;
2930 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2931 pinfo.spinfo_mtu = transport->pmtu;
2933 if (pinfo.spinfo_state == SCTP_UNKNOWN)
2934 pinfo.spinfo_state = SCTP_ACTIVE;
2936 if (put_user(len, optlen)) {
2937 retval = -EFAULT;
2938 goto out;
2941 if (copy_to_user(optval, &pinfo, len)) {
2942 retval = -EFAULT;
2943 goto out;
2946 out:
2947 return (retval);
2950 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2952 * This option is a on/off flag. If enabled no SCTP message
2953 * fragmentation will be performed. Instead if a message being sent
2954 * exceeds the current PMTU size, the message will NOT be sent and
2955 * instead a error will be indicated to the user.
2957 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2958 char __user *optval, int __user *optlen)
2960 int val;
2962 if (len < sizeof(int))
2963 return -EINVAL;
2965 len = sizeof(int);
2966 val = (sctp_sk(sk)->disable_fragments == 1);
2967 if (put_user(len, optlen))
2968 return -EFAULT;
2969 if (copy_to_user(optval, &val, len))
2970 return -EFAULT;
2971 return 0;
2974 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2976 * This socket option is used to specify various notifications and
2977 * ancillary data the user wishes to receive.
2979 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2980 int __user *optlen)
2982 if (len != sizeof(struct sctp_event_subscribe))
2983 return -EINVAL;
2984 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2985 return -EFAULT;
2986 return 0;
2989 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2991 * This socket option is applicable to the UDP-style socket only. When
2992 * set it will cause associations that are idle for more than the
2993 * specified number of seconds to automatically close. An association
2994 * being idle is defined an association that has NOT sent or received
2995 * user data. The special value of '0' indicates that no automatic
2996 * close of any associations should be performed. The option expects an
2997 * integer defining the number of seconds of idle time before an
2998 * association is closed.
3000 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3002 /* Applicable to UDP-style socket only */
3003 if (sctp_style(sk, TCP))
3004 return -EOPNOTSUPP;
3005 if (len != sizeof(int))
3006 return -EINVAL;
3007 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3008 return -EFAULT;
3009 return 0;
3012 /* Helper routine to branch off an association to a new socket. */
3013 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3014 struct socket **sockp)
3016 struct sock *sk = asoc->base.sk;
3017 struct socket *sock;
3018 int err = 0;
3020 /* An association cannot be branched off from an already peeled-off
3021 * socket, nor is this supported for tcp style sockets.
3023 if (!sctp_style(sk, UDP))
3024 return -EINVAL;
3026 /* Create a new socket. */
3027 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3028 if (err < 0)
3029 return err;
3031 /* Populate the fields of the newsk from the oldsk and migrate the
3032 * asoc to the newsk.
3034 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3035 *sockp = sock;
3037 return err;
3040 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3042 sctp_peeloff_arg_t peeloff;
3043 struct socket *newsock;
3044 int retval = 0;
3045 struct sctp_association *asoc;
3047 if (len != sizeof(sctp_peeloff_arg_t))
3048 return -EINVAL;
3049 if (copy_from_user(&peeloff, optval, len))
3050 return -EFAULT;
3052 asoc = sctp_id2assoc(sk, peeloff.associd);
3053 if (!asoc) {
3054 retval = -EINVAL;
3055 goto out;
3058 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3060 retval = sctp_do_peeloff(asoc, &newsock);
3061 if (retval < 0)
3062 goto out;
3064 /* Map the socket to an unused fd that can be returned to the user. */
3065 retval = sock_map_fd(newsock);
3066 if (retval < 0) {
3067 sock_release(newsock);
3068 goto out;
3071 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3072 __FUNCTION__, sk, asoc, newsock->sk, retval);
3074 /* Return the fd mapped to the new socket. */
3075 peeloff.sd = retval;
3076 if (copy_to_user(optval, &peeloff, len))
3077 retval = -EFAULT;
3079 out:
3080 return retval;
3083 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3085 * Applications can enable or disable heartbeats for any peer address of
3086 * an association, modify an address's heartbeat interval, force a
3087 * heartbeat to be sent immediately, and adjust the address's maximum
3088 * number of retransmissions sent before an address is considered
3089 * unreachable. The following structure is used to access and modify an
3090 * address's parameters:
3092 * struct sctp_paddrparams {
3093 * sctp_assoc_t spp_assoc_id;
3094 * struct sockaddr_storage spp_address;
3095 * uint32_t spp_hbinterval;
3096 * uint16_t spp_pathmaxrxt;
3097 * };
3099 * spp_assoc_id - (UDP style socket) This is filled in the application,
3100 * and identifies the association for this query.
3101 * spp_address - This specifies which address is of interest.
3102 * spp_hbinterval - This contains the value of the heartbeat interval,
3103 * in milliseconds. A value of 0, when modifying the
3104 * parameter, specifies that the heartbeat on this
3105 * address should be disabled. A value of UINT32_MAX
3106 * (4294967295), when modifying the parameter,
3107 * specifies that a heartbeat should be sent
3108 * immediately to the peer address, and the current
3109 * interval should remain unchanged.
3110 * spp_pathmaxrxt - This contains the maximum number of
3111 * retransmissions before this address shall be
3112 * considered unreachable.
3114 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3115 char __user *optval, int __user *optlen)
3117 struct sctp_paddrparams params;
3118 struct sctp_transport *trans;
3120 if (len != sizeof(struct sctp_paddrparams))
3121 return -EINVAL;
3122 if (copy_from_user(&params, optval, len))
3123 return -EFAULT;
3125 /* If no association id is specified retrieve the default value
3126 * for the endpoint that will be used for all future associations
3128 if (!params.spp_assoc_id &&
3129 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3130 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
3131 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
3133 goto done;
3136 trans = sctp_addr_id2transport(sk, &params.spp_address,
3137 params.spp_assoc_id);
3138 if (!trans)
3139 return -EINVAL;
3141 /* The value of the heartbeat interval, in milliseconds. A value of 0,
3142 * when modifying the parameter, specifies that the heartbeat on this
3143 * address should be disabled.
3145 if (!trans->hb_allowed)
3146 params.spp_hbinterval = 0;
3147 else
3148 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
3150 /* spp_pathmaxrxt contains the maximum number of retransmissions
3151 * before this address shall be considered unreachable.
3153 params.spp_pathmaxrxt = trans->max_retrans;
3155 done:
3156 if (copy_to_user(optval, &params, len))
3157 return -EFAULT;
3159 if (put_user(len, optlen))
3160 return -EFAULT;
3162 return 0;
3165 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3167 * Applications can specify protocol parameters for the default association
3168 * initialization. The option name argument to setsockopt() and getsockopt()
3169 * is SCTP_INITMSG.
3171 * Setting initialization parameters is effective only on an unconnected
3172 * socket (for UDP-style sockets only future associations are effected
3173 * by the change). With TCP-style sockets, this option is inherited by
3174 * sockets derived from a listener socket.
3176 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3178 if (len != sizeof(struct sctp_initmsg))
3179 return -EINVAL;
3180 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3181 return -EFAULT;
3182 return 0;
3185 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3186 char __user *optval,
3187 int __user *optlen)
3189 sctp_assoc_t id;
3190 struct sctp_association *asoc;
3191 struct list_head *pos;
3192 int cnt = 0;
3194 if (len != sizeof(sctp_assoc_t))
3195 return -EINVAL;
3197 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3198 return -EFAULT;
3200 /* For UDP-style sockets, id specifies the association to query. */
3201 asoc = sctp_id2assoc(sk, id);
3202 if (!asoc)
3203 return -EINVAL;
3205 list_for_each(pos, &asoc->peer.transport_addr_list) {
3206 cnt ++;
3209 return cnt;
3213 * Old API for getting list of peer addresses. Does not work for 32-bit
3214 * programs running on a 64-bit kernel
3216 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3217 char __user *optval,
3218 int __user *optlen)
3220 struct sctp_association *asoc;
3221 struct list_head *pos;
3222 int cnt = 0;
3223 struct sctp_getaddrs_old getaddrs;
3224 struct sctp_transport *from;
3225 void __user *to;
3226 union sctp_addr temp;
3227 struct sctp_sock *sp = sctp_sk(sk);
3228 int addrlen;
3230 if (len != sizeof(struct sctp_getaddrs_old))
3231 return -EINVAL;
3233 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3234 return -EFAULT;
3236 if (getaddrs.addr_num <= 0) return -EINVAL;
3238 /* For UDP-style sockets, id specifies the association to query. */
3239 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3240 if (!asoc)
3241 return -EINVAL;
3243 to = (void __user *)getaddrs.addrs;
3244 list_for_each(pos, &asoc->peer.transport_addr_list) {
3245 from = list_entry(pos, struct sctp_transport, transports);
3246 memcpy(&temp, &from->ipaddr, sizeof(temp));
3247 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3248 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3249 temp.v4.sin_port = htons(temp.v4.sin_port);
3250 if (copy_to_user(to, &temp, addrlen))
3251 return -EFAULT;
3252 to += addrlen ;
3253 cnt ++;
3254 if (cnt >= getaddrs.addr_num) break;
3256 getaddrs.addr_num = cnt;
3257 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3258 return -EFAULT;
3260 return 0;
3263 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3264 char __user *optval, int __user *optlen)
3266 struct sctp_association *asoc;
3267 struct list_head *pos;
3268 int cnt = 0;
3269 struct sctp_getaddrs getaddrs;
3270 struct sctp_transport *from;
3271 void __user *to;
3272 union sctp_addr temp;
3273 struct sctp_sock *sp = sctp_sk(sk);
3274 int addrlen;
3275 size_t space_left;
3276 int bytes_copied;
3278 if (len < sizeof(struct sctp_getaddrs))
3279 return -EINVAL;
3281 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3282 return -EFAULT;
3284 /* For UDP-style sockets, id specifies the association to query. */
3285 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3286 if (!asoc)
3287 return -EINVAL;
3289 to = optval + offsetof(struct sctp_getaddrs,addrs);
3290 space_left = len - sizeof(struct sctp_getaddrs) -
3291 offsetof(struct sctp_getaddrs,addrs);
3293 list_for_each(pos, &asoc->peer.transport_addr_list) {
3294 from = list_entry(pos, struct sctp_transport, transports);
3295 memcpy(&temp, &from->ipaddr, sizeof(temp));
3296 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3297 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3298 if(space_left < addrlen)
3299 return -ENOMEM;
3300 temp.v4.sin_port = htons(temp.v4.sin_port);
3301 if (copy_to_user(to, &temp, addrlen))
3302 return -EFAULT;
3303 to += addrlen;
3304 cnt++;
3305 space_left -= addrlen;
3308 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3309 return -EFAULT;
3310 bytes_copied = ((char __user *)to) - optval;
3311 if (put_user(bytes_copied, optlen))
3312 return -EFAULT;
3314 return 0;
3317 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3318 char __user *optval,
3319 int __user *optlen)
3321 sctp_assoc_t id;
3322 struct sctp_bind_addr *bp;
3323 struct sctp_association *asoc;
3324 struct list_head *pos;
3325 struct sctp_sockaddr_entry *addr;
3326 rwlock_t *addr_lock;
3327 unsigned long flags;
3328 int cnt = 0;
3330 if (len != sizeof(sctp_assoc_t))
3331 return -EINVAL;
3333 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3334 return -EFAULT;
3337 * For UDP-style sockets, id specifies the association to query.
3338 * If the id field is set to the value '0' then the locally bound
3339 * addresses are returned without regard to any particular
3340 * association.
3342 if (0 == id) {
3343 bp = &sctp_sk(sk)->ep->base.bind_addr;
3344 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3345 } else {
3346 asoc = sctp_id2assoc(sk, id);
3347 if (!asoc)
3348 return -EINVAL;
3349 bp = &asoc->base.bind_addr;
3350 addr_lock = &asoc->base.addr_lock;
3353 sctp_read_lock(addr_lock);
3355 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3356 * addresses from the global local address list.
3358 if (sctp_list_single_entry(&bp->address_list)) {
3359 addr = list_entry(bp->address_list.next,
3360 struct sctp_sockaddr_entry, list);
3361 if (sctp_is_any(&addr->a)) {
3362 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3363 list_for_each(pos, &sctp_local_addr_list) {
3364 addr = list_entry(pos,
3365 struct sctp_sockaddr_entry,
3366 list);
3367 if ((PF_INET == sk->sk_family) &&
3368 (AF_INET6 == addr->a.sa.sa_family))
3369 continue;
3370 cnt++;
3372 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3373 flags);
3374 } else {
3375 cnt = 1;
3377 goto done;
3380 list_for_each(pos, &bp->address_list) {
3381 cnt ++;
3384 done:
3385 sctp_read_unlock(addr_lock);
3386 return cnt;
3389 /* Helper function that copies local addresses to user and returns the number
3390 * of addresses copied.
3392 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3393 void __user *to)
3395 struct list_head *pos;
3396 struct sctp_sockaddr_entry *addr;
3397 unsigned long flags;
3398 union sctp_addr temp;
3399 int cnt = 0;
3400 int addrlen;
3402 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3403 list_for_each(pos, &sctp_local_addr_list) {
3404 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3405 if ((PF_INET == sk->sk_family) &&
3406 (AF_INET6 == addr->a.sa.sa_family))
3407 continue;
3408 memcpy(&temp, &addr->a, sizeof(temp));
3409 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3410 &temp);
3411 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3412 temp.v4.sin_port = htons(port);
3413 if (copy_to_user(to, &temp, addrlen)) {
3414 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3415 flags);
3416 return -EFAULT;
3418 to += addrlen;
3419 cnt ++;
3420 if (cnt >= max_addrs) break;
3422 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3424 return cnt;
3427 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3428 void * __user *to, size_t space_left)
3430 struct list_head *pos;
3431 struct sctp_sockaddr_entry *addr;
3432 unsigned long flags;
3433 union sctp_addr temp;
3434 int cnt = 0;
3435 int addrlen;
3437 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3438 list_for_each(pos, &sctp_local_addr_list) {
3439 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3440 if ((PF_INET == sk->sk_family) &&
3441 (AF_INET6 == addr->a.sa.sa_family))
3442 continue;
3443 memcpy(&temp, &addr->a, sizeof(temp));
3444 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3445 &temp);
3446 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3447 if(space_left<addrlen)
3448 return -ENOMEM;
3449 temp.v4.sin_port = htons(port);
3450 if (copy_to_user(*to, &temp, addrlen)) {
3451 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3452 flags);
3453 return -EFAULT;
3455 *to += addrlen;
3456 cnt ++;
3457 space_left -= addrlen;
3459 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3461 return cnt;
3464 /* Old API for getting list of local addresses. Does not work for 32-bit
3465 * programs running on a 64-bit kernel
3467 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3468 char __user *optval, int __user *optlen)
3470 struct sctp_bind_addr *bp;
3471 struct sctp_association *asoc;
3472 struct list_head *pos;
3473 int cnt = 0;
3474 struct sctp_getaddrs_old getaddrs;
3475 struct sctp_sockaddr_entry *addr;
3476 void __user *to;
3477 union sctp_addr temp;
3478 struct sctp_sock *sp = sctp_sk(sk);
3479 int addrlen;
3480 rwlock_t *addr_lock;
3481 int err = 0;
3483 if (len != sizeof(struct sctp_getaddrs_old))
3484 return -EINVAL;
3486 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3487 return -EFAULT;
3489 if (getaddrs.addr_num <= 0) return -EINVAL;
3491 * For UDP-style sockets, id specifies the association to query.
3492 * If the id field is set to the value '0' then the locally bound
3493 * addresses are returned without regard to any particular
3494 * association.
3496 if (0 == getaddrs.assoc_id) {
3497 bp = &sctp_sk(sk)->ep->base.bind_addr;
3498 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3499 } else {
3500 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3501 if (!asoc)
3502 return -EINVAL;
3503 bp = &asoc->base.bind_addr;
3504 addr_lock = &asoc->base.addr_lock;
3507 to = getaddrs.addrs;
3509 sctp_read_lock(addr_lock);
3511 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3512 * addresses from the global local address list.
3514 if (sctp_list_single_entry(&bp->address_list)) {
3515 addr = list_entry(bp->address_list.next,
3516 struct sctp_sockaddr_entry, list);
3517 if (sctp_is_any(&addr->a)) {
3518 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3519 getaddrs.addr_num,
3520 to);
3521 if (cnt < 0) {
3522 err = cnt;
3523 goto unlock;
3525 goto copy_getaddrs;
3529 list_for_each(pos, &bp->address_list) {
3530 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3531 memcpy(&temp, &addr->a, sizeof(temp));
3532 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3533 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3534 temp.v4.sin_port = htons(temp.v4.sin_port);
3535 if (copy_to_user(to, &temp, addrlen)) {
3536 err = -EFAULT;
3537 goto unlock;
3539 to += addrlen;
3540 cnt ++;
3541 if (cnt >= getaddrs.addr_num) break;
3544 copy_getaddrs:
3545 getaddrs.addr_num = cnt;
3546 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3547 err = -EFAULT;
3549 unlock:
3550 sctp_read_unlock(addr_lock);
3551 return err;
3554 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3555 char __user *optval, int __user *optlen)
3557 struct sctp_bind_addr *bp;
3558 struct sctp_association *asoc;
3559 struct list_head *pos;
3560 int cnt = 0;
3561 struct sctp_getaddrs getaddrs;
3562 struct sctp_sockaddr_entry *addr;
3563 void __user *to;
3564 union sctp_addr temp;
3565 struct sctp_sock *sp = sctp_sk(sk);
3566 int addrlen;
3567 rwlock_t *addr_lock;
3568 int err = 0;
3569 size_t space_left;
3570 int bytes_copied;
3572 if (len <= sizeof(struct sctp_getaddrs))
3573 return -EINVAL;
3575 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3576 return -EFAULT;
3579 * For UDP-style sockets, id specifies the association to query.
3580 * If the id field is set to the value '0' then the locally bound
3581 * addresses are returned without regard to any particular
3582 * association.
3584 if (0 == getaddrs.assoc_id) {
3585 bp = &sctp_sk(sk)->ep->base.bind_addr;
3586 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3587 } else {
3588 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3589 if (!asoc)
3590 return -EINVAL;
3591 bp = &asoc->base.bind_addr;
3592 addr_lock = &asoc->base.addr_lock;
3595 to = optval + offsetof(struct sctp_getaddrs,addrs);
3596 space_left = len - sizeof(struct sctp_getaddrs) -
3597 offsetof(struct sctp_getaddrs,addrs);
3599 sctp_read_lock(addr_lock);
3601 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3602 * addresses from the global local address list.
3604 if (sctp_list_single_entry(&bp->address_list)) {
3605 addr = list_entry(bp->address_list.next,
3606 struct sctp_sockaddr_entry, list);
3607 if (sctp_is_any(&addr->a)) {
3608 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
3609 &to, space_left);
3610 if (cnt < 0) {
3611 err = cnt;
3612 goto unlock;
3614 goto copy_getaddrs;
3618 list_for_each(pos, &bp->address_list) {
3619 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3620 memcpy(&temp, &addr->a, sizeof(temp));
3621 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3622 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3623 if(space_left < addrlen)
3624 return -ENOMEM; /*fixme: right error?*/
3625 temp.v4.sin_port = htons(temp.v4.sin_port);
3626 if (copy_to_user(to, &temp, addrlen)) {
3627 err = -EFAULT;
3628 goto unlock;
3630 to += addrlen;
3631 cnt ++;
3632 space_left -= addrlen;
3635 copy_getaddrs:
3636 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3637 return -EFAULT;
3638 bytes_copied = ((char __user *)to) - optval;
3639 if (put_user(bytes_copied, optlen))
3640 return -EFAULT;
3642 unlock:
3643 sctp_read_unlock(addr_lock);
3644 return err;
3647 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3649 * Requests that the local SCTP stack use the enclosed peer address as
3650 * the association primary. The enclosed address must be one of the
3651 * association peer's addresses.
3653 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3654 char __user *optval, int __user *optlen)
3656 struct sctp_prim prim;
3657 struct sctp_association *asoc;
3658 struct sctp_sock *sp = sctp_sk(sk);
3660 if (len != sizeof(struct sctp_prim))
3661 return -EINVAL;
3663 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3664 return -EFAULT;
3666 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3667 if (!asoc)
3668 return -EINVAL;
3670 if (!asoc->peer.primary_path)
3671 return -ENOTCONN;
3673 asoc->peer.primary_path->ipaddr.v4.sin_port =
3674 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3675 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3676 sizeof(union sctp_addr));
3677 asoc->peer.primary_path->ipaddr.v4.sin_port =
3678 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3680 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3681 (union sctp_addr *)&prim.ssp_addr);
3683 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3684 return -EFAULT;
3686 return 0;
3690 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
3692 * Requests that the local endpoint set the specified Adaption Layer
3693 * Indication parameter for all future INIT and INIT-ACK exchanges.
3695 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
3696 char __user *optval, int __user *optlen)
3698 struct sctp_setadaption adaption;
3700 if (len != sizeof(struct sctp_setadaption))
3701 return -EINVAL;
3703 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
3704 if (copy_to_user(optval, &adaption, len))
3705 return -EFAULT;
3707 return 0;
3712 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3714 * Applications that wish to use the sendto() system call may wish to
3715 * specify a default set of parameters that would normally be supplied
3716 * through the inclusion of ancillary data. This socket option allows
3717 * such an application to set the default sctp_sndrcvinfo structure.
3720 * The application that wishes to use this socket option simply passes
3721 * in to this call the sctp_sndrcvinfo structure defined in Section
3722 * 5.2.2) The input parameters accepted by this call include
3723 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3724 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3725 * to this call if the caller is using the UDP model.
3727 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3729 static int sctp_getsockopt_default_send_param(struct sock *sk,
3730 int len, char __user *optval,
3731 int __user *optlen)
3733 struct sctp_sndrcvinfo info;
3734 struct sctp_association *asoc;
3735 struct sctp_sock *sp = sctp_sk(sk);
3737 if (len != sizeof(struct sctp_sndrcvinfo))
3738 return -EINVAL;
3739 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3740 return -EFAULT;
3742 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3743 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3744 return -EINVAL;
3746 if (asoc) {
3747 info.sinfo_stream = asoc->default_stream;
3748 info.sinfo_flags = asoc->default_flags;
3749 info.sinfo_ppid = asoc->default_ppid;
3750 info.sinfo_context = asoc->default_context;
3751 info.sinfo_timetolive = asoc->default_timetolive;
3752 } else {
3753 info.sinfo_stream = sp->default_stream;
3754 info.sinfo_flags = sp->default_flags;
3755 info.sinfo_ppid = sp->default_ppid;
3756 info.sinfo_context = sp->default_context;
3757 info.sinfo_timetolive = sp->default_timetolive;
3760 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3761 return -EFAULT;
3763 return 0;
3768 * 7.1.5 SCTP_NODELAY
3770 * Turn on/off any Nagle-like algorithm. This means that packets are
3771 * generally sent as soon as possible and no unnecessary delays are
3772 * introduced, at the cost of more packets in the network. Expects an
3773 * integer boolean flag.
3776 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3777 char __user *optval, int __user *optlen)
3779 int val;
3781 if (len < sizeof(int))
3782 return -EINVAL;
3784 len = sizeof(int);
3785 val = (sctp_sk(sk)->nodelay == 1);
3786 if (put_user(len, optlen))
3787 return -EFAULT;
3788 if (copy_to_user(optval, &val, len))
3789 return -EFAULT;
3790 return 0;
3795 * 7.1.1 SCTP_RTOINFO
3797 * The protocol parameters used to initialize and bound retransmission
3798 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3799 * and modify these parameters.
3800 * All parameters are time values, in milliseconds. A value of 0, when
3801 * modifying the parameters, indicates that the current value should not
3802 * be changed.
3805 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3806 char __user *optval,
3807 int __user *optlen) {
3808 struct sctp_rtoinfo rtoinfo;
3809 struct sctp_association *asoc;
3811 if (len != sizeof (struct sctp_rtoinfo))
3812 return -EINVAL;
3814 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3815 return -EFAULT;
3817 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3819 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3820 return -EINVAL;
3822 /* Values corresponding to the specific association. */
3823 if (asoc) {
3824 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3825 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3826 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3827 } else {
3828 /* Values corresponding to the endpoint. */
3829 struct sctp_sock *sp = sctp_sk(sk);
3831 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3832 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3833 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3836 if (put_user(len, optlen))
3837 return -EFAULT;
3839 if (copy_to_user(optval, &rtoinfo, len))
3840 return -EFAULT;
3842 return 0;
3847 * 7.1.2 SCTP_ASSOCINFO
3849 * This option is used to tune the the maximum retransmission attempts
3850 * of the association.
3851 * Returns an error if the new association retransmission value is
3852 * greater than the sum of the retransmission value of the peer.
3853 * See [SCTP] for more information.
3856 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3857 char __user *optval,
3858 int __user *optlen)
3861 struct sctp_assocparams assocparams;
3862 struct sctp_association *asoc;
3863 struct list_head *pos;
3864 int cnt = 0;
3866 if (len != sizeof (struct sctp_assocparams))
3867 return -EINVAL;
3869 if (copy_from_user(&assocparams, optval,
3870 sizeof (struct sctp_assocparams)))
3871 return -EFAULT;
3873 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3875 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3876 return -EINVAL;
3878 /* Values correspoinding to the specific association */
3879 if (asoc) {
3880 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3881 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3882 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3883 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3884 * 1000) +
3885 (asoc->cookie_life.tv_usec
3886 / 1000);
3888 list_for_each(pos, &asoc->peer.transport_addr_list) {
3889 cnt ++;
3892 assocparams.sasoc_number_peer_destinations = cnt;
3893 } else {
3894 /* Values corresponding to the endpoint */
3895 struct sctp_sock *sp = sctp_sk(sk);
3897 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3898 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3899 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3900 assocparams.sasoc_cookie_life =
3901 sp->assocparams.sasoc_cookie_life;
3902 assocparams.sasoc_number_peer_destinations =
3903 sp->assocparams.
3904 sasoc_number_peer_destinations;
3907 if (put_user(len, optlen))
3908 return -EFAULT;
3910 if (copy_to_user(optval, &assocparams, len))
3911 return -EFAULT;
3913 return 0;
3917 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3919 * This socket option is a boolean flag which turns on or off mapped V4
3920 * addresses. If this option is turned on and the socket is type
3921 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3922 * If this option is turned off, then no mapping will be done of V4
3923 * addresses and a user will receive both PF_INET6 and PF_INET type
3924 * addresses on the socket.
3926 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3927 char __user *optval, int __user *optlen)
3929 int val;
3930 struct sctp_sock *sp = sctp_sk(sk);
3932 if (len < sizeof(int))
3933 return -EINVAL;
3935 len = sizeof(int);
3936 val = sp->v4mapped;
3937 if (put_user(len, optlen))
3938 return -EFAULT;
3939 if (copy_to_user(optval, &val, len))
3940 return -EFAULT;
3942 return 0;
3946 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3948 * This socket option specifies the maximum size to put in any outgoing
3949 * SCTP chunk. If a message is larger than this size it will be
3950 * fragmented by SCTP into the specified size. Note that the underlying
3951 * SCTP implementation may fragment into smaller sized chunks when the
3952 * PMTU of the underlying association is smaller than the value set by
3953 * the user.
3955 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3956 char __user *optval, int __user *optlen)
3958 int val;
3960 if (len < sizeof(int))
3961 return -EINVAL;
3963 len = sizeof(int);
3965 val = sctp_sk(sk)->user_frag;
3966 if (put_user(len, optlen))
3967 return -EFAULT;
3968 if (copy_to_user(optval, &val, len))
3969 return -EFAULT;
3971 return 0;
3974 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3975 char __user *optval, int __user *optlen)
3977 int retval = 0;
3978 int len;
3980 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
3981 sk, optname);
3983 /* I can hardly begin to describe how wrong this is. This is
3984 * so broken as to be worse than useless. The API draft
3985 * REALLY is NOT helpful here... I am not convinced that the
3986 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3987 * are at all well-founded.
3989 if (level != SOL_SCTP) {
3990 struct sctp_af *af = sctp_sk(sk)->pf->af;
3992 retval = af->getsockopt(sk, level, optname, optval, optlen);
3993 return retval;
3996 if (get_user(len, optlen))
3997 return -EFAULT;
3999 sctp_lock_sock(sk);
4001 switch (optname) {
4002 case SCTP_STATUS:
4003 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4004 break;
4005 case SCTP_DISABLE_FRAGMENTS:
4006 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4007 optlen);
4008 break;
4009 case SCTP_EVENTS:
4010 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4011 break;
4012 case SCTP_AUTOCLOSE:
4013 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4014 break;
4015 case SCTP_SOCKOPT_PEELOFF:
4016 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4017 break;
4018 case SCTP_PEER_ADDR_PARAMS:
4019 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4020 optlen);
4021 break;
4022 case SCTP_INITMSG:
4023 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4024 break;
4025 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4026 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4027 optlen);
4028 break;
4029 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4030 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4031 optlen);
4032 break;
4033 case SCTP_GET_PEER_ADDRS_OLD:
4034 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4035 optlen);
4036 break;
4037 case SCTP_GET_LOCAL_ADDRS_OLD:
4038 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4039 optlen);
4040 break;
4041 case SCTP_GET_PEER_ADDRS:
4042 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4043 optlen);
4044 break;
4045 case SCTP_GET_LOCAL_ADDRS:
4046 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4047 optlen);
4048 break;
4049 case SCTP_DEFAULT_SEND_PARAM:
4050 retval = sctp_getsockopt_default_send_param(sk, len,
4051 optval, optlen);
4052 break;
4053 case SCTP_PRIMARY_ADDR:
4054 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4055 break;
4056 case SCTP_NODELAY:
4057 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4058 break;
4059 case SCTP_RTOINFO:
4060 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4061 break;
4062 case SCTP_ASSOCINFO:
4063 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4064 break;
4065 case SCTP_I_WANT_MAPPED_V4_ADDR:
4066 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4067 break;
4068 case SCTP_MAXSEG:
4069 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4070 break;
4071 case SCTP_GET_PEER_ADDR_INFO:
4072 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4073 optlen);
4074 break;
4075 case SCTP_ADAPTION_LAYER:
4076 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4077 optlen);
4078 break;
4079 default:
4080 retval = -ENOPROTOOPT;
4081 break;
4084 sctp_release_sock(sk);
4085 return retval;
4088 static void sctp_hash(struct sock *sk)
4090 /* STUB */
4093 static void sctp_unhash(struct sock *sk)
4095 /* STUB */
4098 /* Check if port is acceptable. Possibly find first available port.
4100 * The port hash table (contained in the 'global' SCTP protocol storage
4101 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4102 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4103 * list (the list number is the port number hashed out, so as you
4104 * would expect from a hash function, all the ports in a given list have
4105 * such a number that hashes out to the same list number; you were
4106 * expecting that, right?); so each list has a set of ports, with a
4107 * link to the socket (struct sock) that uses it, the port number and
4108 * a fastreuse flag (FIXME: NPI ipg).
4110 static struct sctp_bind_bucket *sctp_bucket_create(
4111 struct sctp_bind_hashbucket *head, unsigned short snum);
4113 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4115 struct sctp_bind_hashbucket *head; /* hash list */
4116 struct sctp_bind_bucket *pp; /* hash list port iterator */
4117 unsigned short snum;
4118 int ret;
4120 /* NOTE: Remember to put this back to net order. */
4121 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4122 snum = addr->v4.sin_port;
4124 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4125 sctp_local_bh_disable();
4127 if (snum == 0) {
4128 /* Search for an available port.
4130 * 'sctp_port_rover' was the last port assigned, so
4131 * we start to search from 'sctp_port_rover +
4132 * 1'. What we do is first check if port 'rover' is
4133 * already in the hash table; if not, we use that; if
4134 * it is, we try next.
4136 int low = sysctl_local_port_range[0];
4137 int high = sysctl_local_port_range[1];
4138 int remaining = (high - low) + 1;
4139 int rover;
4140 int index;
4142 sctp_spin_lock(&sctp_port_alloc_lock);
4143 rover = sctp_port_rover;
4144 do {
4145 rover++;
4146 if ((rover < low) || (rover > high))
4147 rover = low;
4148 index = sctp_phashfn(rover);
4149 head = &sctp_port_hashtable[index];
4150 sctp_spin_lock(&head->lock);
4151 for (pp = head->chain; pp; pp = pp->next)
4152 if (pp->port == rover)
4153 goto next;
4154 break;
4155 next:
4156 sctp_spin_unlock(&head->lock);
4157 } while (--remaining > 0);
4158 sctp_port_rover = rover;
4159 sctp_spin_unlock(&sctp_port_alloc_lock);
4161 /* Exhausted local port range during search? */
4162 ret = 1;
4163 if (remaining <= 0)
4164 goto fail;
4166 /* OK, here is the one we will use. HEAD (the port
4167 * hash table list entry) is non-NULL and we hold it's
4168 * mutex.
4170 snum = rover;
4171 } else {
4172 /* We are given an specific port number; we verify
4173 * that it is not being used. If it is used, we will
4174 * exahust the search in the hash list corresponding
4175 * to the port number (snum) - we detect that with the
4176 * port iterator, pp being NULL.
4178 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4179 sctp_spin_lock(&head->lock);
4180 for (pp = head->chain; pp; pp = pp->next) {
4181 if (pp->port == snum)
4182 goto pp_found;
4185 pp = NULL;
4186 goto pp_not_found;
4187 pp_found:
4188 if (!hlist_empty(&pp->owner)) {
4189 /* We had a port hash table hit - there is an
4190 * available port (pp != NULL) and it is being
4191 * used by other socket (pp->owner not empty); that other
4192 * socket is going to be sk2.
4194 int reuse = sk->sk_reuse;
4195 struct sock *sk2;
4196 struct hlist_node *node;
4198 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4199 if (pp->fastreuse && sk->sk_reuse)
4200 goto success;
4202 /* Run through the list of sockets bound to the port
4203 * (pp->port) [via the pointers bind_next and
4204 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4205 * we get the endpoint they describe and run through
4206 * the endpoint's list of IP (v4 or v6) addresses,
4207 * comparing each of the addresses with the address of
4208 * the socket sk. If we find a match, then that means
4209 * that this port/socket (sk) combination are already
4210 * in an endpoint.
4212 sk_for_each_bound(sk2, node, &pp->owner) {
4213 struct sctp_endpoint *ep2;
4214 ep2 = sctp_sk(sk2)->ep;
4216 if (reuse && sk2->sk_reuse)
4217 continue;
4219 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4220 sctp_sk(sk))) {
4221 ret = (long)sk2;
4222 goto fail_unlock;
4225 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4227 pp_not_found:
4228 /* If there was a hash table miss, create a new port. */
4229 ret = 1;
4230 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4231 goto fail_unlock;
4233 /* In either case (hit or miss), make sure fastreuse is 1 only
4234 * if sk->sk_reuse is too (that is, if the caller requested
4235 * SO_REUSEADDR on this socket -sk-).
4237 if (hlist_empty(&pp->owner))
4238 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4239 else if (pp->fastreuse && !sk->sk_reuse)
4240 pp->fastreuse = 0;
4242 /* We are set, so fill up all the data in the hash table
4243 * entry, tie the socket list information with the rest of the
4244 * sockets FIXME: Blurry, NPI (ipg).
4246 success:
4247 inet_sk(sk)->num = snum;
4248 if (!sctp_sk(sk)->bind_hash) {
4249 sk_add_bind_node(sk, &pp->owner);
4250 sctp_sk(sk)->bind_hash = pp;
4252 ret = 0;
4254 fail_unlock:
4255 sctp_spin_unlock(&head->lock);
4257 fail:
4258 sctp_local_bh_enable();
4259 addr->v4.sin_port = htons(addr->v4.sin_port);
4260 return ret;
4263 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4264 * port is requested.
4266 static int sctp_get_port(struct sock *sk, unsigned short snum)
4268 long ret;
4269 union sctp_addr addr;
4270 struct sctp_af *af = sctp_sk(sk)->pf->af;
4272 /* Set up a dummy address struct from the sk. */
4273 af->from_sk(&addr, sk);
4274 addr.v4.sin_port = htons(snum);
4276 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4277 ret = sctp_get_port_local(sk, &addr);
4279 return (ret ? 1 : 0);
4283 * 3.1.3 listen() - UDP Style Syntax
4285 * By default, new associations are not accepted for UDP style sockets.
4286 * An application uses listen() to mark a socket as being able to
4287 * accept new associations.
4289 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4291 struct sctp_sock *sp = sctp_sk(sk);
4292 struct sctp_endpoint *ep = sp->ep;
4294 /* Only UDP style sockets that are not peeled off are allowed to
4295 * listen().
4297 if (!sctp_style(sk, UDP))
4298 return -EINVAL;
4300 /* If backlog is zero, disable listening. */
4301 if (!backlog) {
4302 if (sctp_sstate(sk, CLOSED))
4303 return 0;
4305 sctp_unhash_endpoint(ep);
4306 sk->sk_state = SCTP_SS_CLOSED;
4309 /* Return if we are already listening. */
4310 if (sctp_sstate(sk, LISTENING))
4311 return 0;
4314 * If a bind() or sctp_bindx() is not called prior to a listen()
4315 * call that allows new associations to be accepted, the system
4316 * picks an ephemeral port and will choose an address set equivalent
4317 * to binding with a wildcard address.
4319 * This is not currently spelled out in the SCTP sockets
4320 * extensions draft, but follows the practice as seen in TCP
4321 * sockets.
4323 if (!ep->base.bind_addr.port) {
4324 if (sctp_autobind(sk))
4325 return -EAGAIN;
4327 sk->sk_state = SCTP_SS_LISTENING;
4328 sctp_hash_endpoint(ep);
4329 return 0;
4333 * 4.1.3 listen() - TCP Style Syntax
4335 * Applications uses listen() to ready the SCTP endpoint for accepting
4336 * inbound associations.
4338 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4340 struct sctp_sock *sp = sctp_sk(sk);
4341 struct sctp_endpoint *ep = sp->ep;
4343 /* If backlog is zero, disable listening. */
4344 if (!backlog) {
4345 if (sctp_sstate(sk, CLOSED))
4346 return 0;
4348 sctp_unhash_endpoint(ep);
4349 sk->sk_state = SCTP_SS_CLOSED;
4352 if (sctp_sstate(sk, LISTENING))
4353 return 0;
4356 * If a bind() or sctp_bindx() is not called prior to a listen()
4357 * call that allows new associations to be accepted, the system
4358 * picks an ephemeral port and will choose an address set equivalent
4359 * to binding with a wildcard address.
4361 * This is not currently spelled out in the SCTP sockets
4362 * extensions draft, but follows the practice as seen in TCP
4363 * sockets.
4365 if (!ep->base.bind_addr.port) {
4366 if (sctp_autobind(sk))
4367 return -EAGAIN;
4369 sk->sk_state = SCTP_SS_LISTENING;
4370 sk->sk_max_ack_backlog = backlog;
4371 sctp_hash_endpoint(ep);
4372 return 0;
4376 * Move a socket to LISTENING state.
4378 int sctp_inet_listen(struct socket *sock, int backlog)
4380 struct sock *sk = sock->sk;
4381 struct crypto_tfm *tfm=NULL;
4382 int err = -EINVAL;
4384 if (unlikely(backlog < 0))
4385 goto out;
4387 sctp_lock_sock(sk);
4389 if (sock->state != SS_UNCONNECTED)
4390 goto out;
4392 /* Allocate HMAC for generating cookie. */
4393 if (sctp_hmac_alg) {
4394 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4395 if (!tfm) {
4396 err = -ENOSYS;
4397 goto out;
4401 switch (sock->type) {
4402 case SOCK_SEQPACKET:
4403 err = sctp_seqpacket_listen(sk, backlog);
4404 break;
4405 case SOCK_STREAM:
4406 err = sctp_stream_listen(sk, backlog);
4407 break;
4408 default:
4409 break;
4411 if (err)
4412 goto cleanup;
4414 /* Store away the transform reference. */
4415 sctp_sk(sk)->hmac = tfm;
4416 out:
4417 sctp_release_sock(sk);
4418 return err;
4419 cleanup:
4420 sctp_crypto_free_tfm(tfm);
4421 goto out;
4425 * This function is done by modeling the current datagram_poll() and the
4426 * tcp_poll(). Note that, based on these implementations, we don't
4427 * lock the socket in this function, even though it seems that,
4428 * ideally, locking or some other mechanisms can be used to ensure
4429 * the integrity of the counters (sndbuf and wmem_queued) used
4430 * in this place. We assume that we don't need locks either until proven
4431 * otherwise.
4433 * Another thing to note is that we include the Async I/O support
4434 * here, again, by modeling the current TCP/UDP code. We don't have
4435 * a good way to test with it yet.
4437 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4439 struct sock *sk = sock->sk;
4440 struct sctp_sock *sp = sctp_sk(sk);
4441 unsigned int mask;
4443 poll_wait(file, sk->sk_sleep, wait);
4445 /* A TCP-style listening socket becomes readable when the accept queue
4446 * is not empty.
4448 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4449 return (!list_empty(&sp->ep->asocs)) ?
4450 (POLLIN | POLLRDNORM) : 0;
4452 mask = 0;
4454 /* Is there any exceptional events? */
4455 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4456 mask |= POLLERR;
4457 if (sk->sk_shutdown == SHUTDOWN_MASK)
4458 mask |= POLLHUP;
4460 /* Is it readable? Reconsider this code with TCP-style support. */
4461 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4462 (sk->sk_shutdown & RCV_SHUTDOWN))
4463 mask |= POLLIN | POLLRDNORM;
4465 /* The association is either gone or not ready. */
4466 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4467 return mask;
4469 /* Is it writable? */
4470 if (sctp_writeable(sk)) {
4471 mask |= POLLOUT | POLLWRNORM;
4472 } else {
4473 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4475 * Since the socket is not locked, the buffer
4476 * might be made available after the writeable check and
4477 * before the bit is set. This could cause a lost I/O
4478 * signal. tcp_poll() has a race breaker for this race
4479 * condition. Based on their implementation, we put
4480 * in the following code to cover it as well.
4482 if (sctp_writeable(sk))
4483 mask |= POLLOUT | POLLWRNORM;
4485 return mask;
4488 /********************************************************************
4489 * 2nd Level Abstractions
4490 ********************************************************************/
4492 static struct sctp_bind_bucket *sctp_bucket_create(
4493 struct sctp_bind_hashbucket *head, unsigned short snum)
4495 struct sctp_bind_bucket *pp;
4497 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4498 SCTP_DBG_OBJCNT_INC(bind_bucket);
4499 if (pp) {
4500 pp->port = snum;
4501 pp->fastreuse = 0;
4502 INIT_HLIST_HEAD(&pp->owner);
4503 if ((pp->next = head->chain) != NULL)
4504 pp->next->pprev = &pp->next;
4505 head->chain = pp;
4506 pp->pprev = &head->chain;
4508 return pp;
4511 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4512 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4514 if (hlist_empty(&pp->owner)) {
4515 if (pp->next)
4516 pp->next->pprev = pp->pprev;
4517 *(pp->pprev) = pp->next;
4518 kmem_cache_free(sctp_bucket_cachep, pp);
4519 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4523 /* Release this socket's reference to a local port. */
4524 static inline void __sctp_put_port(struct sock *sk)
4526 struct sctp_bind_hashbucket *head =
4527 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4528 struct sctp_bind_bucket *pp;
4530 sctp_spin_lock(&head->lock);
4531 pp = sctp_sk(sk)->bind_hash;
4532 __sk_del_bind_node(sk);
4533 sctp_sk(sk)->bind_hash = NULL;
4534 inet_sk(sk)->num = 0;
4535 sctp_bucket_destroy(pp);
4536 sctp_spin_unlock(&head->lock);
4539 void sctp_put_port(struct sock *sk)
4541 sctp_local_bh_disable();
4542 __sctp_put_port(sk);
4543 sctp_local_bh_enable();
4547 * The system picks an ephemeral port and choose an address set equivalent
4548 * to binding with a wildcard address.
4549 * One of those addresses will be the primary address for the association.
4550 * This automatically enables the multihoming capability of SCTP.
4552 static int sctp_autobind(struct sock *sk)
4554 union sctp_addr autoaddr;
4555 struct sctp_af *af;
4556 unsigned short port;
4558 /* Initialize a local sockaddr structure to INADDR_ANY. */
4559 af = sctp_sk(sk)->pf->af;
4561 port = htons(inet_sk(sk)->num);
4562 af->inaddr_any(&autoaddr, port);
4564 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4567 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4569 * From RFC 2292
4570 * 4.2 The cmsghdr Structure *
4572 * When ancillary data is sent or received, any number of ancillary data
4573 * objects can be specified by the msg_control and msg_controllen members of
4574 * the msghdr structure, because each object is preceded by
4575 * a cmsghdr structure defining the object's length (the cmsg_len member).
4576 * Historically Berkeley-derived implementations have passed only one object
4577 * at a time, but this API allows multiple objects to be
4578 * passed in a single call to sendmsg() or recvmsg(). The following example
4579 * shows two ancillary data objects in a control buffer.
4581 * |<--------------------------- msg_controllen -------------------------->|
4582 * | |
4584 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4586 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4587 * | | |
4589 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4591 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4592 * | | | | |
4594 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4595 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4597 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4599 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4603 * msg_control
4604 * points here
4606 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4607 sctp_cmsgs_t *cmsgs)
4609 struct cmsghdr *cmsg;
4611 for (cmsg = CMSG_FIRSTHDR(msg);
4612 cmsg != NULL;
4613 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4614 if (!CMSG_OK(msg, cmsg))
4615 return -EINVAL;
4617 /* Should we parse this header or ignore? */
4618 if (cmsg->cmsg_level != IPPROTO_SCTP)
4619 continue;
4621 /* Strictly check lengths following example in SCM code. */
4622 switch (cmsg->cmsg_type) {
4623 case SCTP_INIT:
4624 /* SCTP Socket API Extension
4625 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4627 * This cmsghdr structure provides information for
4628 * initializing new SCTP associations with sendmsg().
4629 * The SCTP_INITMSG socket option uses this same data
4630 * structure. This structure is not used for
4631 * recvmsg().
4633 * cmsg_level cmsg_type cmsg_data[]
4634 * ------------ ------------ ----------------------
4635 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4637 if (cmsg->cmsg_len !=
4638 CMSG_LEN(sizeof(struct sctp_initmsg)))
4639 return -EINVAL;
4640 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4641 break;
4643 case SCTP_SNDRCV:
4644 /* SCTP Socket API Extension
4645 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4647 * This cmsghdr structure specifies SCTP options for
4648 * sendmsg() and describes SCTP header information
4649 * about a received message through recvmsg().
4651 * cmsg_level cmsg_type cmsg_data[]
4652 * ------------ ------------ ----------------------
4653 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4655 if (cmsg->cmsg_len !=
4656 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4657 return -EINVAL;
4659 cmsgs->info =
4660 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4662 /* Minimally, validate the sinfo_flags. */
4663 if (cmsgs->info->sinfo_flags &
4664 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
4665 SCTP_ABORT | SCTP_EOF))
4666 return -EINVAL;
4667 break;
4669 default:
4670 return -EINVAL;
4673 return 0;
4677 * Wait for a packet..
4678 * Note: This function is the same function as in core/datagram.c
4679 * with a few modifications to make lksctp work.
4681 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4683 int error;
4684 DEFINE_WAIT(wait);
4686 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4688 /* Socket errors? */
4689 error = sock_error(sk);
4690 if (error)
4691 goto out;
4693 if (!skb_queue_empty(&sk->sk_receive_queue))
4694 goto ready;
4696 /* Socket shut down? */
4697 if (sk->sk_shutdown & RCV_SHUTDOWN)
4698 goto out;
4700 /* Sequenced packets can come disconnected. If so we report the
4701 * problem.
4703 error = -ENOTCONN;
4705 /* Is there a good reason to think that we may receive some data? */
4706 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4707 goto out;
4709 /* Handle signals. */
4710 if (signal_pending(current))
4711 goto interrupted;
4713 /* Let another process have a go. Since we are going to sleep
4714 * anyway. Note: This may cause odd behaviors if the message
4715 * does not fit in the user's buffer, but this seems to be the
4716 * only way to honor MSG_DONTWAIT realistically.
4718 sctp_release_sock(sk);
4719 *timeo_p = schedule_timeout(*timeo_p);
4720 sctp_lock_sock(sk);
4722 ready:
4723 finish_wait(sk->sk_sleep, &wait);
4724 return 0;
4726 interrupted:
4727 error = sock_intr_errno(*timeo_p);
4729 out:
4730 finish_wait(sk->sk_sleep, &wait);
4731 *err = error;
4732 return error;
4735 /* Receive a datagram.
4736 * Note: This is pretty much the same routine as in core/datagram.c
4737 * with a few changes to make lksctp work.
4739 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4740 int noblock, int *err)
4742 int error;
4743 struct sk_buff *skb;
4744 long timeo;
4746 /* Caller is allowed not to check sk->sk_err before calling. */
4747 error = sock_error(sk);
4748 if (error)
4749 goto no_packet;
4751 timeo = sock_rcvtimeo(sk, noblock);
4753 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4754 timeo, MAX_SCHEDULE_TIMEOUT);
4756 do {
4757 /* Again only user level code calls this function,
4758 * so nothing interrupt level
4759 * will suddenly eat the receive_queue.
4761 * Look at current nfs client by the way...
4762 * However, this function was corrent in any case. 8)
4764 if (flags & MSG_PEEK) {
4765 spin_lock_bh(&sk->sk_receive_queue.lock);
4766 skb = skb_peek(&sk->sk_receive_queue);
4767 if (skb)
4768 atomic_inc(&skb->users);
4769 spin_unlock_bh(&sk->sk_receive_queue.lock);
4770 } else {
4771 skb = skb_dequeue(&sk->sk_receive_queue);
4774 if (skb)
4775 return skb;
4777 if (sk->sk_shutdown & RCV_SHUTDOWN)
4778 break;
4780 /* User doesn't want to wait. */
4781 error = -EAGAIN;
4782 if (!timeo)
4783 goto no_packet;
4784 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4786 return NULL;
4788 no_packet:
4789 *err = error;
4790 return NULL;
4793 /* If sndbuf has changed, wake up per association sndbuf waiters. */
4794 static void __sctp_write_space(struct sctp_association *asoc)
4796 struct sock *sk = asoc->base.sk;
4797 struct socket *sock = sk->sk_socket;
4799 if ((sctp_wspace(asoc) > 0) && sock) {
4800 if (waitqueue_active(&asoc->wait))
4801 wake_up_interruptible(&asoc->wait);
4803 if (sctp_writeable(sk)) {
4804 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4805 wake_up_interruptible(sk->sk_sleep);
4807 /* Note that we try to include the Async I/O support
4808 * here by modeling from the current TCP/UDP code.
4809 * We have not tested with it yet.
4811 if (sock->fasync_list &&
4812 !(sk->sk_shutdown & SEND_SHUTDOWN))
4813 sock_wake_async(sock, 2, POLL_OUT);
4818 /* Do accounting for the sndbuf space.
4819 * Decrement the used sndbuf space of the corresponding association by the
4820 * data size which was just transmitted(freed).
4822 static void sctp_wfree(struct sk_buff *skb)
4824 struct sctp_association *asoc;
4825 struct sctp_chunk *chunk;
4826 struct sock *sk;
4828 /* Get the saved chunk pointer. */
4829 chunk = *((struct sctp_chunk **)(skb->cb));
4830 asoc = chunk->asoc;
4831 sk = asoc->base.sk;
4832 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
4833 sizeof(struct sk_buff) +
4834 sizeof(struct sctp_chunk);
4836 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
4837 sizeof(struct sk_buff) +
4838 sizeof(struct sctp_chunk);
4840 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
4842 sock_wfree(skb);
4843 __sctp_write_space(asoc);
4845 sctp_association_put(asoc);
4848 /* Helper function to wait for space in the sndbuf. */
4849 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4850 size_t msg_len)
4852 struct sock *sk = asoc->base.sk;
4853 int err = 0;
4854 long current_timeo = *timeo_p;
4855 DEFINE_WAIT(wait);
4857 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4858 asoc, (long)(*timeo_p), msg_len);
4860 /* Increment the association's refcnt. */
4861 sctp_association_hold(asoc);
4863 /* Wait on the association specific sndbuf space. */
4864 for (;;) {
4865 prepare_to_wait_exclusive(&asoc->wait, &wait,
4866 TASK_INTERRUPTIBLE);
4867 if (!*timeo_p)
4868 goto do_nonblock;
4869 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4870 asoc->base.dead)
4871 goto do_error;
4872 if (signal_pending(current))
4873 goto do_interrupted;
4874 if (msg_len <= sctp_wspace(asoc))
4875 break;
4877 /* Let another process have a go. Since we are going
4878 * to sleep anyway.
4880 sctp_release_sock(sk);
4881 current_timeo = schedule_timeout(current_timeo);
4882 sctp_lock_sock(sk);
4884 *timeo_p = current_timeo;
4887 out:
4888 finish_wait(&asoc->wait, &wait);
4890 /* Release the association's refcnt. */
4891 sctp_association_put(asoc);
4893 return err;
4895 do_error:
4896 err = -EPIPE;
4897 goto out;
4899 do_interrupted:
4900 err = sock_intr_errno(*timeo_p);
4901 goto out;
4903 do_nonblock:
4904 err = -EAGAIN;
4905 goto out;
4908 /* If socket sndbuf has changed, wake up all per association waiters. */
4909 void sctp_write_space(struct sock *sk)
4911 struct sctp_association *asoc;
4912 struct list_head *pos;
4914 /* Wake up the tasks in each wait queue. */
4915 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4916 asoc = list_entry(pos, struct sctp_association, asocs);
4917 __sctp_write_space(asoc);
4921 /* Is there any sndbuf space available on the socket?
4923 * Note that wmem_queued is the sum of the send buffers on all of the
4924 * associations on the same socket. For a UDP-style socket with
4925 * multiple associations, it is possible for it to be "unwriteable"
4926 * prematurely. I assume that this is acceptable because
4927 * a premature "unwriteable" is better than an accidental "writeable" which
4928 * would cause an unwanted block under certain circumstances. For the 1-1
4929 * UDP-style sockets or TCP-style sockets, this code should work.
4930 * - Daisy
4932 static int sctp_writeable(struct sock *sk)
4934 int amt = 0;
4936 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4937 if (amt < 0)
4938 amt = 0;
4939 return amt;
4942 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4943 * returns immediately with EINPROGRESS.
4945 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4947 struct sock *sk = asoc->base.sk;
4948 int err = 0;
4949 long current_timeo = *timeo_p;
4950 DEFINE_WAIT(wait);
4952 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4953 (long)(*timeo_p));
4955 /* Increment the association's refcnt. */
4956 sctp_association_hold(asoc);
4958 for (;;) {
4959 prepare_to_wait_exclusive(&asoc->wait, &wait,
4960 TASK_INTERRUPTIBLE);
4961 if (!*timeo_p)
4962 goto do_nonblock;
4963 if (sk->sk_shutdown & RCV_SHUTDOWN)
4964 break;
4965 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4966 asoc->base.dead)
4967 goto do_error;
4968 if (signal_pending(current))
4969 goto do_interrupted;
4971 if (sctp_state(asoc, ESTABLISHED))
4972 break;
4974 /* Let another process have a go. Since we are going
4975 * to sleep anyway.
4977 sctp_release_sock(sk);
4978 current_timeo = schedule_timeout(current_timeo);
4979 sctp_lock_sock(sk);
4981 *timeo_p = current_timeo;
4984 out:
4985 finish_wait(&asoc->wait, &wait);
4987 /* Release the association's refcnt. */
4988 sctp_association_put(asoc);
4990 return err;
4992 do_error:
4993 if (asoc->init_err_counter + 1 >= asoc->max_init_attempts)
4994 err = -ETIMEDOUT;
4995 else
4996 err = -ECONNREFUSED;
4997 goto out;
4999 do_interrupted:
5000 err = sock_intr_errno(*timeo_p);
5001 goto out;
5003 do_nonblock:
5004 err = -EINPROGRESS;
5005 goto out;
5008 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5010 struct sctp_endpoint *ep;
5011 int err = 0;
5012 DEFINE_WAIT(wait);
5014 ep = sctp_sk(sk)->ep;
5017 for (;;) {
5018 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5019 TASK_INTERRUPTIBLE);
5021 if (list_empty(&ep->asocs)) {
5022 sctp_release_sock(sk);
5023 timeo = schedule_timeout(timeo);
5024 sctp_lock_sock(sk);
5027 err = -EINVAL;
5028 if (!sctp_sstate(sk, LISTENING))
5029 break;
5031 err = 0;
5032 if (!list_empty(&ep->asocs))
5033 break;
5035 err = sock_intr_errno(timeo);
5036 if (signal_pending(current))
5037 break;
5039 err = -EAGAIN;
5040 if (!timeo)
5041 break;
5044 finish_wait(sk->sk_sleep, &wait);
5046 return err;
5049 void sctp_wait_for_close(struct sock *sk, long timeout)
5051 DEFINE_WAIT(wait);
5053 do {
5054 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5055 if (list_empty(&sctp_sk(sk)->ep->asocs))
5056 break;
5057 sctp_release_sock(sk);
5058 timeout = schedule_timeout(timeout);
5059 sctp_lock_sock(sk);
5060 } while (!signal_pending(current) && timeout);
5062 finish_wait(sk->sk_sleep, &wait);
5065 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5066 * and its messages to the newsk.
5068 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5069 struct sctp_association *assoc,
5070 sctp_socket_type_t type)
5072 struct sctp_sock *oldsp = sctp_sk(oldsk);
5073 struct sctp_sock *newsp = sctp_sk(newsk);
5074 struct sctp_bind_bucket *pp; /* hash list port iterator */
5075 struct sctp_endpoint *newep = newsp->ep;
5076 struct sk_buff *skb, *tmp;
5077 struct sctp_ulpevent *event;
5078 int flags = 0;
5080 /* Migrate socket buffer sizes and all the socket level options to the
5081 * new socket.
5083 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5084 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5085 /* Brute force copy old sctp opt. */
5086 inet_sk_copy_descendant(newsk, oldsk);
5088 /* Restore the ep value that was overwritten with the above structure
5089 * copy.
5091 newsp->ep = newep;
5092 newsp->hmac = NULL;
5094 /* Hook this new socket in to the bind_hash list. */
5095 pp = sctp_sk(oldsk)->bind_hash;
5096 sk_add_bind_node(newsk, &pp->owner);
5097 sctp_sk(newsk)->bind_hash = pp;
5098 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5100 /* Copy the bind_addr list from the original endpoint to the new
5101 * endpoint so that we can handle restarts properly
5103 if (assoc->peer.ipv4_address)
5104 flags |= SCTP_ADDR4_PEERSUPP;
5105 if (assoc->peer.ipv6_address)
5106 flags |= SCTP_ADDR6_PEERSUPP;
5107 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5108 &oldsp->ep->base.bind_addr,
5109 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5111 /* Move any messages in the old socket's receive queue that are for the
5112 * peeled off association to the new socket's receive queue.
5114 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5115 event = sctp_skb2event(skb);
5116 if (event->asoc == assoc) {
5117 sock_rfree(skb);
5118 __skb_unlink(skb, &oldsk->sk_receive_queue);
5119 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5120 skb_set_owner_r(skb, newsk);
5124 /* Clean up any messages pending delivery due to partial
5125 * delivery. Three cases:
5126 * 1) No partial deliver; no work.
5127 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5128 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5130 skb_queue_head_init(&newsp->pd_lobby);
5131 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5133 if (sctp_sk(oldsk)->pd_mode) {
5134 struct sk_buff_head *queue;
5136 /* Decide which queue to move pd_lobby skbs to. */
5137 if (assoc->ulpq.pd_mode) {
5138 queue = &newsp->pd_lobby;
5139 } else
5140 queue = &newsk->sk_receive_queue;
5142 /* Walk through the pd_lobby, looking for skbs that
5143 * need moved to the new socket.
5145 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5146 event = sctp_skb2event(skb);
5147 if (event->asoc == assoc) {
5148 sock_rfree(skb);
5149 __skb_unlink(skb, &oldsp->pd_lobby);
5150 __skb_queue_tail(queue, skb);
5151 skb_set_owner_r(skb, newsk);
5155 /* Clear up any skbs waiting for the partial
5156 * delivery to finish.
5158 if (assoc->ulpq.pd_mode)
5159 sctp_clear_pd(oldsk);
5163 /* Set the type of socket to indicate that it is peeled off from the
5164 * original UDP-style socket or created with the accept() call on a
5165 * TCP-style socket..
5167 newsp->type = type;
5169 /* Migrate the association to the new socket. */
5170 sctp_assoc_migrate(assoc, newsk);
5172 /* If the association on the newsk is already closed before accept()
5173 * is called, set RCV_SHUTDOWN flag.
5175 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5176 newsk->sk_shutdown |= RCV_SHUTDOWN;
5178 newsk->sk_state = SCTP_SS_ESTABLISHED;
5181 /* This proto struct describes the ULP interface for SCTP. */
5182 struct proto sctp_prot = {
5183 .name = "SCTP",
5184 .owner = THIS_MODULE,
5185 .close = sctp_close,
5186 .connect = sctp_connect,
5187 .disconnect = sctp_disconnect,
5188 .accept = sctp_accept,
5189 .ioctl = sctp_ioctl,
5190 .init = sctp_init_sock,
5191 .destroy = sctp_destroy_sock,
5192 .shutdown = sctp_shutdown,
5193 .setsockopt = sctp_setsockopt,
5194 .getsockopt = sctp_getsockopt,
5195 .sendmsg = sctp_sendmsg,
5196 .recvmsg = sctp_recvmsg,
5197 .bind = sctp_bind,
5198 .backlog_rcv = sctp_backlog_rcv,
5199 .hash = sctp_hash,
5200 .unhash = sctp_unhash,
5201 .get_port = sctp_get_port,
5202 .obj_size = sizeof(struct sctp_sock),
5205 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5206 struct proto sctpv6_prot = {
5207 .name = "SCTPv6",
5208 .owner = THIS_MODULE,
5209 .close = sctp_close,
5210 .connect = sctp_connect,
5211 .disconnect = sctp_disconnect,
5212 .accept = sctp_accept,
5213 .ioctl = sctp_ioctl,
5214 .init = sctp_init_sock,
5215 .destroy = sctp_destroy_sock,
5216 .shutdown = sctp_shutdown,
5217 .setsockopt = sctp_setsockopt,
5218 .getsockopt = sctp_getsockopt,
5219 .sendmsg = sctp_sendmsg,
5220 .recvmsg = sctp_recvmsg,
5221 .bind = sctp_bind,
5222 .backlog_rcv = sctp_backlog_rcv,
5223 .hash = sctp_hash,
5224 .unhash = sctp_unhash,
5225 .get_port = sctp_get_port,
5226 .obj_size = sizeof(struct sctp6_sock),
5228 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */