e1000: Add device IDs of blade version of the 82571 quad port
[pv_ops_mirror.git] / net / sctp / socket.c
blob772fbfb4bfda801e63229c4ac6479278e823459c
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/types.h>
61 #include <linux/kernel.h>
62 #include <linux/wait.h>
63 #include <linux/time.h>
64 #include <linux/ip.h>
65 #include <linux/capability.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
71 #include <net/ip.h>
72 #include <net/icmp.h>
73 #include <net/route.h>
74 #include <net/ipv6.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <net/sock.h>
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 /* Get the sndbuf space available at the time on the association. */
111 static inline int sctp_wspace(struct sctp_association *asoc)
113 struct sock *sk = asoc->base.sk;
114 int amt = 0;
116 if (asoc->ep->sndbuf_policy) {
117 /* make sure that no association uses more than sk_sndbuf */
118 amt = sk->sk_sndbuf - asoc->sndbuf_used;
119 } else {
120 /* do socket level accounting */
121 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
124 if (amt < 0)
125 amt = 0;
127 return amt;
130 /* Increment the used sndbuf space count of the corresponding association by
131 * the size of the outgoing data chunk.
132 * Also, set the skb destructor for sndbuf accounting later.
134 * Since it is always 1-1 between chunk and skb, and also a new skb is always
135 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
136 * destructor in the data chunk skb for the purpose of the sndbuf space
137 * tracking.
139 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
141 struct sctp_association *asoc = chunk->asoc;
142 struct sock *sk = asoc->base.sk;
144 /* The sndbuf space is tracked per association. */
145 sctp_association_hold(asoc);
147 skb_set_owner_w(chunk->skb, sk);
149 chunk->skb->destructor = sctp_wfree;
150 /* Save the chunk pointer in skb for sctp_wfree to use later. */
151 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
153 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
154 sizeof(struct sk_buff) +
155 sizeof(struct sctp_chunk);
157 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
160 /* Verify that this is a valid address. */
161 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
162 int len)
164 struct sctp_af *af;
166 /* Verify basic sockaddr. */
167 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
168 if (!af)
169 return -EINVAL;
171 /* Is this a valid SCTP address? */
172 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
173 return -EINVAL;
175 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
176 return -EINVAL;
178 return 0;
181 /* Look up the association by its id. If this is not a UDP-style
182 * socket, the ID field is always ignored.
184 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
186 struct sctp_association *asoc = NULL;
188 /* If this is not a UDP-style socket, assoc id should be ignored. */
189 if (!sctp_style(sk, UDP)) {
190 /* Return NULL if the socket state is not ESTABLISHED. It
191 * could be a TCP-style listening socket or a socket which
192 * hasn't yet called connect() to establish an association.
194 if (!sctp_sstate(sk, ESTABLISHED))
195 return NULL;
197 /* Get the first and the only association from the list. */
198 if (!list_empty(&sctp_sk(sk)->ep->asocs))
199 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
200 struct sctp_association, asocs);
201 return asoc;
204 /* Otherwise this is a UDP-style socket. */
205 if (!id || (id == (sctp_assoc_t)-1))
206 return NULL;
208 spin_lock_bh(&sctp_assocs_id_lock);
209 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
210 spin_unlock_bh(&sctp_assocs_id_lock);
212 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
213 return NULL;
215 return asoc;
218 /* Look up the transport from an address and an assoc id. If both address and
219 * id are specified, the associations matching the address and the id should be
220 * the same.
222 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
223 struct sockaddr_storage *addr,
224 sctp_assoc_t id)
226 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
227 struct sctp_transport *transport;
228 union sctp_addr *laddr = (union sctp_addr *)addr;
230 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
231 laddr,
232 &transport);
234 if (!addr_asoc)
235 return NULL;
237 id_asoc = sctp_id2assoc(sk, id);
238 if (id_asoc && (id_asoc != addr_asoc))
239 return NULL;
241 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
242 (union sctp_addr *)addr);
244 return transport;
247 /* API 3.1.2 bind() - UDP Style Syntax
248 * The syntax of bind() is,
250 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
252 * sd - the socket descriptor returned by socket().
253 * addr - the address structure (struct sockaddr_in or struct
254 * sockaddr_in6 [RFC 2553]),
255 * addr_len - the size of the address structure.
257 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
259 int retval = 0;
261 sctp_lock_sock(sk);
263 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
264 sk, addr, addr_len);
266 /* Disallow binding twice. */
267 if (!sctp_sk(sk)->ep->base.bind_addr.port)
268 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
269 addr_len);
270 else
271 retval = -EINVAL;
273 sctp_release_sock(sk);
275 return retval;
278 static long sctp_get_port_local(struct sock *, union sctp_addr *);
280 /* Verify this is a valid sockaddr. */
281 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
282 union sctp_addr *addr, int len)
284 struct sctp_af *af;
286 /* Check minimum size. */
287 if (len < sizeof (struct sockaddr))
288 return NULL;
290 /* Does this PF support this AF? */
291 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
292 return NULL;
294 /* If we get this far, af is valid. */
295 af = sctp_get_af_specific(addr->sa.sa_family);
297 if (len < af->sockaddr_len)
298 return NULL;
300 return af;
303 /* Bind a local address either to an endpoint or to an association. */
304 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
306 struct sctp_sock *sp = sctp_sk(sk);
307 struct sctp_endpoint *ep = sp->ep;
308 struct sctp_bind_addr *bp = &ep->base.bind_addr;
309 struct sctp_af *af;
310 unsigned short snum;
311 int ret = 0;
313 /* Common sockaddr verification. */
314 af = sctp_sockaddr_af(sp, addr, len);
315 if (!af) {
316 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
317 sk, addr, len);
318 return -EINVAL;
321 snum = ntohs(addr->v4.sin_port);
323 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
324 ", port: %d, new port: %d, len: %d)\n",
326 addr,
327 bp->port, snum,
328 len);
330 /* PF specific bind() address verification. */
331 if (!sp->pf->bind_verify(sp, addr))
332 return -EADDRNOTAVAIL;
334 /* We must either be unbound, or bind to the same port.
335 * It's OK to allow 0 ports if we are already bound.
336 * We'll just inhert an already bound port in this case
338 if (bp->port) {
339 if (!snum)
340 snum = bp->port;
341 else if (snum != bp->port) {
342 SCTP_DEBUG_PRINTK("sctp_do_bind:"
343 " New port %d does not match existing port "
344 "%d.\n", snum, bp->port);
345 return -EINVAL;
349 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
350 return -EACCES;
352 /* Make sure we are allowed to bind here.
353 * The function sctp_get_port_local() does duplicate address
354 * detection.
356 addr->v4.sin_port = htons(snum);
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 * Use GFP_ATOMIC since BHs will be disabled.
373 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
375 /* Copy back into socket for getsockname() use. */
376 if (!ret) {
377 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
378 af->to_sk_saddr(addr, sk);
381 return ret;
384 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
386 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
387 * at any one time. If a sender, after sending an ASCONF chunk, decides
388 * it needs to transfer another ASCONF Chunk, it MUST wait until the
389 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
390 * subsequent ASCONF. Note this restriction binds each side, so at any
391 * time two ASCONF may be in-transit on any given association (one sent
392 * from each endpoint).
394 static int sctp_send_asconf(struct sctp_association *asoc,
395 struct sctp_chunk *chunk)
397 int retval = 0;
399 /* If there is an outstanding ASCONF chunk, queue it for later
400 * transmission.
402 if (asoc->addip_last_asconf) {
403 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
404 goto out;
407 /* Hold the chunk until an ASCONF_ACK is received. */
408 sctp_chunk_hold(chunk);
409 retval = sctp_primitive_ASCONF(asoc, chunk);
410 if (retval)
411 sctp_chunk_free(chunk);
412 else
413 asoc->addip_last_asconf = chunk;
415 out:
416 return retval;
419 /* Add a list of addresses as bind addresses to local endpoint or
420 * association.
422 * Basically run through each address specified in the addrs/addrcnt
423 * array/length pair, determine if it is IPv6 or IPv4 and call
424 * sctp_do_bind() on it.
426 * If any of them fails, then the operation will be reversed and the
427 * ones that were added will be removed.
429 * Only sctp_setsockopt_bindx() is supposed to call this function.
431 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
433 int cnt;
434 int retval = 0;
435 void *addr_buf;
436 struct sockaddr *sa_addr;
437 struct sctp_af *af;
439 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
440 sk, addrs, addrcnt);
442 addr_buf = addrs;
443 for (cnt = 0; cnt < addrcnt; cnt++) {
444 /* The list may contain either IPv4 or IPv6 address;
445 * determine the address length for walking thru the list.
447 sa_addr = (struct sockaddr *)addr_buf;
448 af = sctp_get_af_specific(sa_addr->sa_family);
449 if (!af) {
450 retval = -EINVAL;
451 goto err_bindx_add;
454 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
455 af->sockaddr_len);
457 addr_buf += af->sockaddr_len;
459 err_bindx_add:
460 if (retval < 0) {
461 /* Failed. Cleanup the ones that have been added */
462 if (cnt > 0)
463 sctp_bindx_rem(sk, addrs, cnt);
464 return retval;
468 return retval;
471 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
472 * associations that are part of the endpoint indicating that a list of local
473 * addresses are added to the endpoint.
475 * If any of the addresses is already in the bind address list of the
476 * association, we do not send the chunk for that association. But it will not
477 * affect other associations.
479 * Only sctp_setsockopt_bindx() is supposed to call this function.
481 static int sctp_send_asconf_add_ip(struct sock *sk,
482 struct sockaddr *addrs,
483 int addrcnt)
485 struct sctp_sock *sp;
486 struct sctp_endpoint *ep;
487 struct sctp_association *asoc;
488 struct sctp_bind_addr *bp;
489 struct sctp_chunk *chunk;
490 struct sctp_sockaddr_entry *laddr;
491 union sctp_addr *addr;
492 union sctp_addr saveaddr;
493 void *addr_buf;
494 struct sctp_af *af;
495 struct list_head *pos;
496 struct list_head *p;
497 int i;
498 int retval = 0;
500 if (!sctp_addip_enable)
501 return retval;
503 sp = sctp_sk(sk);
504 ep = sp->ep;
506 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
507 __FUNCTION__, sk, addrs, addrcnt);
509 list_for_each(pos, &ep->asocs) {
510 asoc = list_entry(pos, struct sctp_association, asocs);
512 if (!asoc->peer.asconf_capable)
513 continue;
515 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
516 continue;
518 if (!sctp_state(asoc, ESTABLISHED))
519 continue;
521 /* Check if any address in the packed array of addresses is
522 * in the bind address list of the association. If so,
523 * do not send the asconf chunk to its peer, but continue with
524 * other associations.
526 addr_buf = addrs;
527 for (i = 0; i < addrcnt; i++) {
528 addr = (union sctp_addr *)addr_buf;
529 af = sctp_get_af_specific(addr->v4.sin_family);
530 if (!af) {
531 retval = -EINVAL;
532 goto out;
535 if (sctp_assoc_lookup_laddr(asoc, addr))
536 break;
538 addr_buf += af->sockaddr_len;
540 if (i < addrcnt)
541 continue;
543 /* Use the first valid address in bind addr list of
544 * association as Address Parameter of ASCONF CHUNK.
546 bp = &asoc->base.bind_addr;
547 p = bp->address_list.next;
548 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
549 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
550 addrcnt, SCTP_PARAM_ADD_IP);
551 if (!chunk) {
552 retval = -ENOMEM;
553 goto out;
556 retval = sctp_send_asconf(asoc, chunk);
557 if (retval)
558 goto out;
560 /* Add the new addresses to the bind address list with
561 * use_as_src set to 0.
563 addr_buf = addrs;
564 for (i = 0; i < addrcnt; i++) {
565 addr = (union sctp_addr *)addr_buf;
566 af = sctp_get_af_specific(addr->v4.sin_family);
567 memcpy(&saveaddr, addr, af->sockaddr_len);
568 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
569 GFP_ATOMIC);
570 addr_buf += af->sockaddr_len;
574 out:
575 return retval;
578 /* Remove a list of addresses from bind addresses list. Do not remove the
579 * last address.
581 * Basically run through each address specified in the addrs/addrcnt
582 * array/length pair, determine if it is IPv6 or IPv4 and call
583 * sctp_del_bind() on it.
585 * If any of them fails, then the operation will be reversed and the
586 * ones that were removed will be added back.
588 * At least one address has to be left; if only one address is
589 * available, the operation will return -EBUSY.
591 * Only sctp_setsockopt_bindx() is supposed to call this function.
593 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
595 struct sctp_sock *sp = sctp_sk(sk);
596 struct sctp_endpoint *ep = sp->ep;
597 int cnt;
598 struct sctp_bind_addr *bp = &ep->base.bind_addr;
599 int retval = 0;
600 void *addr_buf;
601 union sctp_addr *sa_addr;
602 struct sctp_af *af;
604 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
605 sk, addrs, addrcnt);
607 addr_buf = addrs;
608 for (cnt = 0; cnt < addrcnt; cnt++) {
609 /* If the bind address list is empty or if there is only one
610 * bind address, there is nothing more to be removed (we need
611 * at least one address here).
613 if (list_empty(&bp->address_list) ||
614 (sctp_list_single_entry(&bp->address_list))) {
615 retval = -EBUSY;
616 goto err_bindx_rem;
619 sa_addr = (union sctp_addr *)addr_buf;
620 af = sctp_get_af_specific(sa_addr->sa.sa_family);
621 if (!af) {
622 retval = -EINVAL;
623 goto err_bindx_rem;
626 if (!af->addr_valid(sa_addr, sp, NULL)) {
627 retval = -EADDRNOTAVAIL;
628 goto err_bindx_rem;
631 if (sa_addr->v4.sin_port != htons(bp->port)) {
632 retval = -EINVAL;
633 goto err_bindx_rem;
636 /* FIXME - There is probably a need to check if sk->sk_saddr and
637 * sk->sk_rcv_addr are currently set to one of the addresses to
638 * be removed. This is something which needs to be looked into
639 * when we are fixing the outstanding issues with multi-homing
640 * socket routing and failover schemes. Refer to comments in
641 * sctp_do_bind(). -daisy
643 retval = sctp_del_bind_addr(bp, sa_addr, call_rcu);
645 addr_buf += af->sockaddr_len;
646 err_bindx_rem:
647 if (retval < 0) {
648 /* Failed. Add the ones that has been removed back */
649 if (cnt > 0)
650 sctp_bindx_add(sk, addrs, cnt);
651 return retval;
655 return retval;
658 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
659 * the associations that are part of the endpoint indicating that a list of
660 * local addresses are removed from the endpoint.
662 * If any of the addresses is already in the bind address list of the
663 * association, we do not send the chunk for that association. But it will not
664 * affect other associations.
666 * Only sctp_setsockopt_bindx() is supposed to call this function.
668 static int sctp_send_asconf_del_ip(struct sock *sk,
669 struct sockaddr *addrs,
670 int addrcnt)
672 struct sctp_sock *sp;
673 struct sctp_endpoint *ep;
674 struct sctp_association *asoc;
675 struct sctp_transport *transport;
676 struct sctp_bind_addr *bp;
677 struct sctp_chunk *chunk;
678 union sctp_addr *laddr;
679 void *addr_buf;
680 struct sctp_af *af;
681 struct list_head *pos, *pos1;
682 struct sctp_sockaddr_entry *saddr;
683 int i;
684 int retval = 0;
686 if (!sctp_addip_enable)
687 return retval;
689 sp = sctp_sk(sk);
690 ep = sp->ep;
692 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
693 __FUNCTION__, sk, addrs, addrcnt);
695 list_for_each(pos, &ep->asocs) {
696 asoc = list_entry(pos, struct sctp_association, asocs);
698 if (!asoc->peer.asconf_capable)
699 continue;
701 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
702 continue;
704 if (!sctp_state(asoc, ESTABLISHED))
705 continue;
707 /* Check if any address in the packed array of addresses is
708 * not present in the bind address list of the association.
709 * If so, do not send the asconf chunk to its peer, but
710 * continue with other associations.
712 addr_buf = addrs;
713 for (i = 0; i < addrcnt; i++) {
714 laddr = (union sctp_addr *)addr_buf;
715 af = sctp_get_af_specific(laddr->v4.sin_family);
716 if (!af) {
717 retval = -EINVAL;
718 goto out;
721 if (!sctp_assoc_lookup_laddr(asoc, laddr))
722 break;
724 addr_buf += af->sockaddr_len;
726 if (i < addrcnt)
727 continue;
729 /* Find one address in the association's bind address list
730 * that is not in the packed array of addresses. This is to
731 * make sure that we do not delete all the addresses in the
732 * association.
734 bp = &asoc->base.bind_addr;
735 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
736 addrcnt, sp);
737 if (!laddr)
738 continue;
740 /* We do not need RCU protection throughout this loop
741 * because this is done under a socket lock from the
742 * setsockopt call.
744 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
745 SCTP_PARAM_DEL_IP);
746 if (!chunk) {
747 retval = -ENOMEM;
748 goto out;
751 /* Reset use_as_src flag for the addresses in the bind address
752 * list that are to be deleted.
754 addr_buf = addrs;
755 for (i = 0; i < addrcnt; i++) {
756 laddr = (union sctp_addr *)addr_buf;
757 af = sctp_get_af_specific(laddr->v4.sin_family);
758 list_for_each_entry(saddr, &bp->address_list, list) {
759 if (sctp_cmp_addr_exact(&saddr->a, laddr))
760 saddr->use_as_src = 0;
762 addr_buf += af->sockaddr_len;
765 /* Update the route and saddr entries for all the transports
766 * as some of the addresses in the bind address list are
767 * about to be deleted and cannot be used as source addresses.
769 list_for_each(pos1, &asoc->peer.transport_addr_list) {
770 transport = list_entry(pos1, struct sctp_transport,
771 transports);
772 dst_release(transport->dst);
773 sctp_transport_route(transport, NULL,
774 sctp_sk(asoc->base.sk));
777 retval = sctp_send_asconf(asoc, chunk);
779 out:
780 return retval;
783 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
785 * API 8.1
786 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
787 * int flags);
789 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
790 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
791 * or IPv6 addresses.
793 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
794 * Section 3.1.2 for this usage.
796 * addrs is a pointer to an array of one or more socket addresses. Each
797 * address is contained in its appropriate structure (i.e. struct
798 * sockaddr_in or struct sockaddr_in6) the family of the address type
799 * must be used to distinguish the address length (note that this
800 * representation is termed a "packed array" of addresses). The caller
801 * specifies the number of addresses in the array with addrcnt.
803 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
804 * -1, and sets errno to the appropriate error code.
806 * For SCTP, the port given in each socket address must be the same, or
807 * sctp_bindx() will fail, setting errno to EINVAL.
809 * The flags parameter is formed from the bitwise OR of zero or more of
810 * the following currently defined flags:
812 * SCTP_BINDX_ADD_ADDR
814 * SCTP_BINDX_REM_ADDR
816 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
817 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
818 * addresses from the association. The two flags are mutually exclusive;
819 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
820 * not remove all addresses from an association; sctp_bindx() will
821 * reject such an attempt with EINVAL.
823 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
824 * additional addresses with an endpoint after calling bind(). Or use
825 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
826 * socket is associated with so that no new association accepted will be
827 * associated with those addresses. If the endpoint supports dynamic
828 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
829 * endpoint to send the appropriate message to the peer to change the
830 * peers address lists.
832 * Adding and removing addresses from a connected association is
833 * optional functionality. Implementations that do not support this
834 * functionality should return EOPNOTSUPP.
836 * Basically do nothing but copying the addresses from user to kernel
837 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
838 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
839 * from userspace.
841 * We don't use copy_from_user() for optimization: we first do the
842 * sanity checks (buffer size -fast- and access check-healthy
843 * pointer); if all of those succeed, then we can alloc the memory
844 * (expensive operation) needed to copy the data to kernel. Then we do
845 * the copying without checking the user space area
846 * (__copy_from_user()).
848 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
849 * it.
851 * sk The sk of the socket
852 * addrs The pointer to the addresses in user land
853 * addrssize Size of the addrs buffer
854 * op Operation to perform (add or remove, see the flags of
855 * sctp_bindx)
857 * Returns 0 if ok, <0 errno code on error.
859 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
860 struct sockaddr __user *addrs,
861 int addrs_size, int op)
863 struct sockaddr *kaddrs;
864 int err;
865 int addrcnt = 0;
866 int walk_size = 0;
867 struct sockaddr *sa_addr;
868 void *addr_buf;
869 struct sctp_af *af;
871 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
872 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
874 if (unlikely(addrs_size <= 0))
875 return -EINVAL;
877 /* Check the user passed a healthy pointer. */
878 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
879 return -EFAULT;
881 /* Alloc space for the address array in kernel memory. */
882 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
883 if (unlikely(!kaddrs))
884 return -ENOMEM;
886 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
887 kfree(kaddrs);
888 return -EFAULT;
891 /* Walk through the addrs buffer and count the number of addresses. */
892 addr_buf = kaddrs;
893 while (walk_size < addrs_size) {
894 sa_addr = (struct sockaddr *)addr_buf;
895 af = sctp_get_af_specific(sa_addr->sa_family);
897 /* If the address family is not supported or if this address
898 * causes the address buffer to overflow return EINVAL.
900 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
901 kfree(kaddrs);
902 return -EINVAL;
904 addrcnt++;
905 addr_buf += af->sockaddr_len;
906 walk_size += af->sockaddr_len;
909 /* Do the work. */
910 switch (op) {
911 case SCTP_BINDX_ADD_ADDR:
912 err = sctp_bindx_add(sk, kaddrs, addrcnt);
913 if (err)
914 goto out;
915 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
916 break;
918 case SCTP_BINDX_REM_ADDR:
919 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
920 if (err)
921 goto out;
922 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
923 break;
925 default:
926 err = -EINVAL;
927 break;
930 out:
931 kfree(kaddrs);
933 return err;
936 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
938 * Common routine for handling connect() and sctp_connectx().
939 * Connect will come in with just a single address.
941 static int __sctp_connect(struct sock* sk,
942 struct sockaddr *kaddrs,
943 int addrs_size)
945 struct sctp_sock *sp;
946 struct sctp_endpoint *ep;
947 struct sctp_association *asoc = NULL;
948 struct sctp_association *asoc2;
949 struct sctp_transport *transport;
950 union sctp_addr to;
951 struct sctp_af *af;
952 sctp_scope_t scope;
953 long timeo;
954 int err = 0;
955 int addrcnt = 0;
956 int walk_size = 0;
957 union sctp_addr *sa_addr = NULL;
958 void *addr_buf;
959 unsigned short port;
960 unsigned int f_flags = 0;
962 sp = sctp_sk(sk);
963 ep = sp->ep;
965 /* connect() cannot be done on a socket that is already in ESTABLISHED
966 * state - UDP-style peeled off socket or a TCP-style socket that
967 * is already connected.
968 * It cannot be done even on a TCP-style listening socket.
970 if (sctp_sstate(sk, ESTABLISHED) ||
971 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
972 err = -EISCONN;
973 goto out_free;
976 /* Walk through the addrs buffer and count the number of addresses. */
977 addr_buf = kaddrs;
978 while (walk_size < addrs_size) {
979 sa_addr = (union sctp_addr *)addr_buf;
980 af = sctp_get_af_specific(sa_addr->sa.sa_family);
981 port = ntohs(sa_addr->v4.sin_port);
983 /* If the address family is not supported or if this address
984 * causes the address buffer to overflow return EINVAL.
986 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
987 err = -EINVAL;
988 goto out_free;
991 /* Save current address so we can work with it */
992 memcpy(&to, sa_addr, af->sockaddr_len);
994 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
995 if (err)
996 goto out_free;
998 /* Make sure the destination port is correctly set
999 * in all addresses.
1001 if (asoc && asoc->peer.port && asoc->peer.port != port)
1002 goto out_free;
1005 /* Check if there already is a matching association on the
1006 * endpoint (other than the one created here).
1008 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1009 if (asoc2 && asoc2 != asoc) {
1010 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1011 err = -EISCONN;
1012 else
1013 err = -EALREADY;
1014 goto out_free;
1017 /* If we could not find a matching association on the endpoint,
1018 * make sure that there is no peeled-off association matching
1019 * the peer address even on another socket.
1021 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1022 err = -EADDRNOTAVAIL;
1023 goto out_free;
1026 if (!asoc) {
1027 /* If a bind() or sctp_bindx() is not called prior to
1028 * an sctp_connectx() call, the system picks an
1029 * ephemeral port and will choose an address set
1030 * equivalent to binding with a wildcard address.
1032 if (!ep->base.bind_addr.port) {
1033 if (sctp_autobind(sk)) {
1034 err = -EAGAIN;
1035 goto out_free;
1037 } else {
1039 * If an unprivileged user inherits a 1-many
1040 * style socket with open associations on a
1041 * privileged port, it MAY be permitted to
1042 * accept new associations, but it SHOULD NOT
1043 * be permitted to open new associations.
1045 if (ep->base.bind_addr.port < PROT_SOCK &&
1046 !capable(CAP_NET_BIND_SERVICE)) {
1047 err = -EACCES;
1048 goto out_free;
1052 scope = sctp_scope(&to);
1053 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1054 if (!asoc) {
1055 err = -ENOMEM;
1056 goto out_free;
1060 /* Prime the peer's transport structures. */
1061 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1062 SCTP_UNKNOWN);
1063 if (!transport) {
1064 err = -ENOMEM;
1065 goto out_free;
1068 addrcnt++;
1069 addr_buf += af->sockaddr_len;
1070 walk_size += af->sockaddr_len;
1073 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1074 if (err < 0) {
1075 goto out_free;
1078 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1079 if (err < 0) {
1080 goto out_free;
1083 /* Initialize sk's dport and daddr for getpeername() */
1084 inet_sk(sk)->dport = htons(asoc->peer.port);
1085 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1086 af->to_sk_daddr(sa_addr, sk);
1087 sk->sk_err = 0;
1089 /* in-kernel sockets don't generally have a file allocated to them
1090 * if all they do is call sock_create_kern().
1092 if (sk->sk_socket->file)
1093 f_flags = sk->sk_socket->file->f_flags;
1095 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1097 err = sctp_wait_for_connect(asoc, &timeo);
1099 /* Don't free association on exit. */
1100 asoc = NULL;
1102 out_free:
1104 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1105 " kaddrs: %p err: %d\n",
1106 asoc, kaddrs, err);
1107 if (asoc)
1108 sctp_association_free(asoc);
1109 return err;
1112 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1114 * API 8.9
1115 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1117 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1118 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1119 * or IPv6 addresses.
1121 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1122 * Section 3.1.2 for this usage.
1124 * addrs is a pointer to an array of one or more socket addresses. Each
1125 * address is contained in its appropriate structure (i.e. struct
1126 * sockaddr_in or struct sockaddr_in6) the family of the address type
1127 * must be used to distengish the address length (note that this
1128 * representation is termed a "packed array" of addresses). The caller
1129 * specifies the number of addresses in the array with addrcnt.
1131 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1132 * -1, and sets errno to the appropriate error code.
1134 * For SCTP, the port given in each socket address must be the same, or
1135 * sctp_connectx() will fail, setting errno to EINVAL.
1137 * An application can use sctp_connectx to initiate an association with
1138 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1139 * allows a caller to specify multiple addresses at which a peer can be
1140 * reached. The way the SCTP stack uses the list of addresses to set up
1141 * the association is implementation dependant. This function only
1142 * specifies that the stack will try to make use of all the addresses in
1143 * the list when needed.
1145 * Note that the list of addresses passed in is only used for setting up
1146 * the association. It does not necessarily equal the set of addresses
1147 * the peer uses for the resulting association. If the caller wants to
1148 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1149 * retrieve them after the association has been set up.
1151 * Basically do nothing but copying the addresses from user to kernel
1152 * land and invoking either sctp_connectx(). This is used for tunneling
1153 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1155 * We don't use copy_from_user() for optimization: we first do the
1156 * sanity checks (buffer size -fast- and access check-healthy
1157 * pointer); if all of those succeed, then we can alloc the memory
1158 * (expensive operation) needed to copy the data to kernel. Then we do
1159 * the copying without checking the user space area
1160 * (__copy_from_user()).
1162 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1163 * it.
1165 * sk The sk of the socket
1166 * addrs The pointer to the addresses in user land
1167 * addrssize Size of the addrs buffer
1169 * Returns 0 if ok, <0 errno code on error.
1171 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1172 struct sockaddr __user *addrs,
1173 int addrs_size)
1175 int err = 0;
1176 struct sockaddr *kaddrs;
1178 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1179 __FUNCTION__, sk, addrs, addrs_size);
1181 if (unlikely(addrs_size <= 0))
1182 return -EINVAL;
1184 /* Check the user passed a healthy pointer. */
1185 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1186 return -EFAULT;
1188 /* Alloc space for the address array in kernel memory. */
1189 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1190 if (unlikely(!kaddrs))
1191 return -ENOMEM;
1193 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1194 err = -EFAULT;
1195 } else {
1196 err = __sctp_connect(sk, kaddrs, addrs_size);
1199 kfree(kaddrs);
1200 return err;
1203 /* API 3.1.4 close() - UDP Style Syntax
1204 * Applications use close() to perform graceful shutdown (as described in
1205 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1206 * by a UDP-style socket.
1208 * The syntax is
1210 * ret = close(int sd);
1212 * sd - the socket descriptor of the associations to be closed.
1214 * To gracefully shutdown a specific association represented by the
1215 * UDP-style socket, an application should use the sendmsg() call,
1216 * passing no user data, but including the appropriate flag in the
1217 * ancillary data (see Section xxxx).
1219 * If sd in the close() call is a branched-off socket representing only
1220 * one association, the shutdown is performed on that association only.
1222 * 4.1.6 close() - TCP Style Syntax
1224 * Applications use close() to gracefully close down an association.
1226 * The syntax is:
1228 * int close(int sd);
1230 * sd - the socket descriptor of the association to be closed.
1232 * After an application calls close() on a socket descriptor, no further
1233 * socket operations will succeed on that descriptor.
1235 * API 7.1.4 SO_LINGER
1237 * An application using the TCP-style socket can use this option to
1238 * perform the SCTP ABORT primitive. The linger option structure is:
1240 * struct linger {
1241 * int l_onoff; // option on/off
1242 * int l_linger; // linger time
1243 * };
1245 * To enable the option, set l_onoff to 1. If the l_linger value is set
1246 * to 0, calling close() is the same as the ABORT primitive. If the
1247 * value is set to a negative value, the setsockopt() call will return
1248 * an error. If the value is set to a positive value linger_time, the
1249 * close() can be blocked for at most linger_time ms. If the graceful
1250 * shutdown phase does not finish during this period, close() will
1251 * return but the graceful shutdown phase continues in the system.
1253 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1255 struct sctp_endpoint *ep;
1256 struct sctp_association *asoc;
1257 struct list_head *pos, *temp;
1259 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1261 sctp_lock_sock(sk);
1262 sk->sk_shutdown = SHUTDOWN_MASK;
1264 ep = sctp_sk(sk)->ep;
1266 /* Walk all associations on an endpoint. */
1267 list_for_each_safe(pos, temp, &ep->asocs) {
1268 asoc = list_entry(pos, struct sctp_association, asocs);
1270 if (sctp_style(sk, TCP)) {
1271 /* A closed association can still be in the list if
1272 * it belongs to a TCP-style listening socket that is
1273 * not yet accepted. If so, free it. If not, send an
1274 * ABORT or SHUTDOWN based on the linger options.
1276 if (sctp_state(asoc, CLOSED)) {
1277 sctp_unhash_established(asoc);
1278 sctp_association_free(asoc);
1279 continue;
1283 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1284 struct sctp_chunk *chunk;
1286 chunk = sctp_make_abort_user(asoc, NULL, 0);
1287 if (chunk)
1288 sctp_primitive_ABORT(asoc, chunk);
1289 } else
1290 sctp_primitive_SHUTDOWN(asoc, NULL);
1293 /* Clean up any skbs sitting on the receive queue. */
1294 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1295 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1297 /* On a TCP-style socket, block for at most linger_time if set. */
1298 if (sctp_style(sk, TCP) && timeout)
1299 sctp_wait_for_close(sk, timeout);
1301 /* This will run the backlog queue. */
1302 sctp_release_sock(sk);
1304 /* Supposedly, no process has access to the socket, but
1305 * the net layers still may.
1307 sctp_local_bh_disable();
1308 sctp_bh_lock_sock(sk);
1310 /* Hold the sock, since sk_common_release() will put sock_put()
1311 * and we have just a little more cleanup.
1313 sock_hold(sk);
1314 sk_common_release(sk);
1316 sctp_bh_unlock_sock(sk);
1317 sctp_local_bh_enable();
1319 sock_put(sk);
1321 SCTP_DBG_OBJCNT_DEC(sock);
1324 /* Handle EPIPE error. */
1325 static int sctp_error(struct sock *sk, int flags, int err)
1327 if (err == -EPIPE)
1328 err = sock_error(sk) ? : -EPIPE;
1329 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1330 send_sig(SIGPIPE, current, 0);
1331 return err;
1334 /* API 3.1.3 sendmsg() - UDP Style Syntax
1336 * An application uses sendmsg() and recvmsg() calls to transmit data to
1337 * and receive data from its peer.
1339 * ssize_t sendmsg(int socket, const struct msghdr *message,
1340 * int flags);
1342 * socket - the socket descriptor of the endpoint.
1343 * message - pointer to the msghdr structure which contains a single
1344 * user message and possibly some ancillary data.
1346 * See Section 5 for complete description of the data
1347 * structures.
1349 * flags - flags sent or received with the user message, see Section
1350 * 5 for complete description of the flags.
1352 * Note: This function could use a rewrite especially when explicit
1353 * connect support comes in.
1355 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1357 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1359 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1360 struct msghdr *msg, size_t msg_len)
1362 struct sctp_sock *sp;
1363 struct sctp_endpoint *ep;
1364 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1365 struct sctp_transport *transport, *chunk_tp;
1366 struct sctp_chunk *chunk;
1367 union sctp_addr to;
1368 struct sockaddr *msg_name = NULL;
1369 struct sctp_sndrcvinfo default_sinfo = { 0 };
1370 struct sctp_sndrcvinfo *sinfo;
1371 struct sctp_initmsg *sinit;
1372 sctp_assoc_t associd = 0;
1373 sctp_cmsgs_t cmsgs = { NULL };
1374 int err;
1375 sctp_scope_t scope;
1376 long timeo;
1377 __u16 sinfo_flags = 0;
1378 struct sctp_datamsg *datamsg;
1379 struct list_head *pos;
1380 int msg_flags = msg->msg_flags;
1382 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1383 sk, msg, msg_len);
1385 err = 0;
1386 sp = sctp_sk(sk);
1387 ep = sp->ep;
1389 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1391 /* We cannot send a message over a TCP-style listening socket. */
1392 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1393 err = -EPIPE;
1394 goto out_nounlock;
1397 /* Parse out the SCTP CMSGs. */
1398 err = sctp_msghdr_parse(msg, &cmsgs);
1400 if (err) {
1401 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1402 goto out_nounlock;
1405 /* Fetch the destination address for this packet. This
1406 * address only selects the association--it is not necessarily
1407 * the address we will send to.
1408 * For a peeled-off socket, msg_name is ignored.
1410 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1411 int msg_namelen = msg->msg_namelen;
1413 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1414 msg_namelen);
1415 if (err)
1416 return err;
1418 if (msg_namelen > sizeof(to))
1419 msg_namelen = sizeof(to);
1420 memcpy(&to, msg->msg_name, msg_namelen);
1421 msg_name = msg->msg_name;
1424 sinfo = cmsgs.info;
1425 sinit = cmsgs.init;
1427 /* Did the user specify SNDRCVINFO? */
1428 if (sinfo) {
1429 sinfo_flags = sinfo->sinfo_flags;
1430 associd = sinfo->sinfo_assoc_id;
1433 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1434 msg_len, sinfo_flags);
1436 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1437 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1438 err = -EINVAL;
1439 goto out_nounlock;
1442 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1443 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1444 * If SCTP_ABORT is set, the message length could be non zero with
1445 * the msg_iov set to the user abort reason.
1447 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1448 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1449 err = -EINVAL;
1450 goto out_nounlock;
1453 /* If SCTP_ADDR_OVER is set, there must be an address
1454 * specified in msg_name.
1456 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1457 err = -EINVAL;
1458 goto out_nounlock;
1461 transport = NULL;
1463 SCTP_DEBUG_PRINTK("About to look up association.\n");
1465 sctp_lock_sock(sk);
1467 /* If a msg_name has been specified, assume this is to be used. */
1468 if (msg_name) {
1469 /* Look for a matching association on the endpoint. */
1470 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1471 if (!asoc) {
1472 /* If we could not find a matching association on the
1473 * endpoint, make sure that it is not a TCP-style
1474 * socket that already has an association or there is
1475 * no peeled-off association on another socket.
1477 if ((sctp_style(sk, TCP) &&
1478 sctp_sstate(sk, ESTABLISHED)) ||
1479 sctp_endpoint_is_peeled_off(ep, &to)) {
1480 err = -EADDRNOTAVAIL;
1481 goto out_unlock;
1484 } else {
1485 asoc = sctp_id2assoc(sk, associd);
1486 if (!asoc) {
1487 err = -EPIPE;
1488 goto out_unlock;
1492 if (asoc) {
1493 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1495 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1496 * socket that has an association in CLOSED state. This can
1497 * happen when an accepted socket has an association that is
1498 * already CLOSED.
1500 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1501 err = -EPIPE;
1502 goto out_unlock;
1505 if (sinfo_flags & SCTP_EOF) {
1506 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1507 asoc);
1508 sctp_primitive_SHUTDOWN(asoc, NULL);
1509 err = 0;
1510 goto out_unlock;
1512 if (sinfo_flags & SCTP_ABORT) {
1514 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1515 if (!chunk) {
1516 err = -ENOMEM;
1517 goto out_unlock;
1520 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1521 sctp_primitive_ABORT(asoc, chunk);
1522 err = 0;
1523 goto out_unlock;
1527 /* Do we need to create the association? */
1528 if (!asoc) {
1529 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1531 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1532 err = -EINVAL;
1533 goto out_unlock;
1536 /* Check for invalid stream against the stream counts,
1537 * either the default or the user specified stream counts.
1539 if (sinfo) {
1540 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1541 /* Check against the defaults. */
1542 if (sinfo->sinfo_stream >=
1543 sp->initmsg.sinit_num_ostreams) {
1544 err = -EINVAL;
1545 goto out_unlock;
1547 } else {
1548 /* Check against the requested. */
1549 if (sinfo->sinfo_stream >=
1550 sinit->sinit_num_ostreams) {
1551 err = -EINVAL;
1552 goto out_unlock;
1558 * API 3.1.2 bind() - UDP Style Syntax
1559 * If a bind() or sctp_bindx() is not called prior to a
1560 * sendmsg() call that initiates a new association, the
1561 * system picks an ephemeral port and will choose an address
1562 * set equivalent to binding with a wildcard address.
1564 if (!ep->base.bind_addr.port) {
1565 if (sctp_autobind(sk)) {
1566 err = -EAGAIN;
1567 goto out_unlock;
1569 } else {
1571 * If an unprivileged user inherits a one-to-many
1572 * style socket with open associations on a privileged
1573 * port, it MAY be permitted to accept new associations,
1574 * but it SHOULD NOT be permitted to open new
1575 * associations.
1577 if (ep->base.bind_addr.port < PROT_SOCK &&
1578 !capable(CAP_NET_BIND_SERVICE)) {
1579 err = -EACCES;
1580 goto out_unlock;
1584 scope = sctp_scope(&to);
1585 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1586 if (!new_asoc) {
1587 err = -ENOMEM;
1588 goto out_unlock;
1590 asoc = new_asoc;
1592 /* If the SCTP_INIT ancillary data is specified, set all
1593 * the association init values accordingly.
1595 if (sinit) {
1596 if (sinit->sinit_num_ostreams) {
1597 asoc->c.sinit_num_ostreams =
1598 sinit->sinit_num_ostreams;
1600 if (sinit->sinit_max_instreams) {
1601 asoc->c.sinit_max_instreams =
1602 sinit->sinit_max_instreams;
1604 if (sinit->sinit_max_attempts) {
1605 asoc->max_init_attempts
1606 = sinit->sinit_max_attempts;
1608 if (sinit->sinit_max_init_timeo) {
1609 asoc->max_init_timeo =
1610 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1614 /* Prime the peer's transport structures. */
1615 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1616 if (!transport) {
1617 err = -ENOMEM;
1618 goto out_free;
1620 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1621 if (err < 0) {
1622 err = -ENOMEM;
1623 goto out_free;
1627 /* ASSERT: we have a valid association at this point. */
1628 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1630 if (!sinfo) {
1631 /* If the user didn't specify SNDRCVINFO, make up one with
1632 * some defaults.
1634 default_sinfo.sinfo_stream = asoc->default_stream;
1635 default_sinfo.sinfo_flags = asoc->default_flags;
1636 default_sinfo.sinfo_ppid = asoc->default_ppid;
1637 default_sinfo.sinfo_context = asoc->default_context;
1638 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1639 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1640 sinfo = &default_sinfo;
1643 /* API 7.1.7, the sndbuf size per association bounds the
1644 * maximum size of data that can be sent in a single send call.
1646 if (msg_len > sk->sk_sndbuf) {
1647 err = -EMSGSIZE;
1648 goto out_free;
1651 if (asoc->pmtu_pending)
1652 sctp_assoc_pending_pmtu(asoc);
1654 /* If fragmentation is disabled and the message length exceeds the
1655 * association fragmentation point, return EMSGSIZE. The I-D
1656 * does not specify what this error is, but this looks like
1657 * a great fit.
1659 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1660 err = -EMSGSIZE;
1661 goto out_free;
1664 if (sinfo) {
1665 /* Check for invalid stream. */
1666 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1667 err = -EINVAL;
1668 goto out_free;
1672 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1673 if (!sctp_wspace(asoc)) {
1674 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1675 if (err)
1676 goto out_free;
1679 /* If an address is passed with the sendto/sendmsg call, it is used
1680 * to override the primary destination address in the TCP model, or
1681 * when SCTP_ADDR_OVER flag is set in the UDP model.
1683 if ((sctp_style(sk, TCP) && msg_name) ||
1684 (sinfo_flags & SCTP_ADDR_OVER)) {
1685 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1686 if (!chunk_tp) {
1687 err = -EINVAL;
1688 goto out_free;
1690 } else
1691 chunk_tp = NULL;
1693 /* Auto-connect, if we aren't connected already. */
1694 if (sctp_state(asoc, CLOSED)) {
1695 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1696 if (err < 0)
1697 goto out_free;
1698 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1701 /* Break the message into multiple chunks of maximum size. */
1702 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1703 if (!datamsg) {
1704 err = -ENOMEM;
1705 goto out_free;
1708 /* Now send the (possibly) fragmented message. */
1709 list_for_each(pos, &datamsg->chunks) {
1710 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1711 sctp_datamsg_track(chunk);
1713 /* Do accounting for the write space. */
1714 sctp_set_owner_w(chunk);
1716 chunk->transport = chunk_tp;
1718 /* Send it to the lower layers. Note: all chunks
1719 * must either fail or succeed. The lower layer
1720 * works that way today. Keep it that way or this
1721 * breaks.
1723 err = sctp_primitive_SEND(asoc, chunk);
1724 /* Did the lower layer accept the chunk? */
1725 if (err)
1726 sctp_chunk_free(chunk);
1727 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1730 sctp_datamsg_free(datamsg);
1731 if (err)
1732 goto out_free;
1733 else
1734 err = msg_len;
1736 /* If we are already past ASSOCIATE, the lower
1737 * layers are responsible for association cleanup.
1739 goto out_unlock;
1741 out_free:
1742 if (new_asoc)
1743 sctp_association_free(asoc);
1744 out_unlock:
1745 sctp_release_sock(sk);
1747 out_nounlock:
1748 return sctp_error(sk, msg_flags, err);
1750 #if 0
1751 do_sock_err:
1752 if (msg_len)
1753 err = msg_len;
1754 else
1755 err = sock_error(sk);
1756 goto out;
1758 do_interrupted:
1759 if (msg_len)
1760 err = msg_len;
1761 goto out;
1762 #endif /* 0 */
1765 /* This is an extended version of skb_pull() that removes the data from the
1766 * start of a skb even when data is spread across the list of skb's in the
1767 * frag_list. len specifies the total amount of data that needs to be removed.
1768 * when 'len' bytes could be removed from the skb, it returns 0.
1769 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1770 * could not be removed.
1772 static int sctp_skb_pull(struct sk_buff *skb, int len)
1774 struct sk_buff *list;
1775 int skb_len = skb_headlen(skb);
1776 int rlen;
1778 if (len <= skb_len) {
1779 __skb_pull(skb, len);
1780 return 0;
1782 len -= skb_len;
1783 __skb_pull(skb, skb_len);
1785 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1786 rlen = sctp_skb_pull(list, len);
1787 skb->len -= (len-rlen);
1788 skb->data_len -= (len-rlen);
1790 if (!rlen)
1791 return 0;
1793 len = rlen;
1796 return len;
1799 /* API 3.1.3 recvmsg() - UDP Style Syntax
1801 * ssize_t recvmsg(int socket, struct msghdr *message,
1802 * int flags);
1804 * socket - the socket descriptor of the endpoint.
1805 * message - pointer to the msghdr structure which contains a single
1806 * user message and possibly some ancillary data.
1808 * See Section 5 for complete description of the data
1809 * structures.
1811 * flags - flags sent or received with the user message, see Section
1812 * 5 for complete description of the flags.
1814 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1816 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1817 struct msghdr *msg, size_t len, int noblock,
1818 int flags, int *addr_len)
1820 struct sctp_ulpevent *event = NULL;
1821 struct sctp_sock *sp = sctp_sk(sk);
1822 struct sk_buff *skb;
1823 int copied;
1824 int err = 0;
1825 int skb_len;
1827 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1828 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1829 "len", len, "knoblauch", noblock,
1830 "flags", flags, "addr_len", addr_len);
1832 sctp_lock_sock(sk);
1834 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1835 err = -ENOTCONN;
1836 goto out;
1839 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1840 if (!skb)
1841 goto out;
1843 /* Get the total length of the skb including any skb's in the
1844 * frag_list.
1846 skb_len = skb->len;
1848 copied = skb_len;
1849 if (copied > len)
1850 copied = len;
1852 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1854 event = sctp_skb2event(skb);
1856 if (err)
1857 goto out_free;
1859 sock_recv_timestamp(msg, sk, skb);
1860 if (sctp_ulpevent_is_notification(event)) {
1861 msg->msg_flags |= MSG_NOTIFICATION;
1862 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1863 } else {
1864 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1867 /* Check if we allow SCTP_SNDRCVINFO. */
1868 if (sp->subscribe.sctp_data_io_event)
1869 sctp_ulpevent_read_sndrcvinfo(event, msg);
1870 #if 0
1871 /* FIXME: we should be calling IP/IPv6 layers. */
1872 if (sk->sk_protinfo.af_inet.cmsg_flags)
1873 ip_cmsg_recv(msg, skb);
1874 #endif
1876 err = copied;
1878 /* If skb's length exceeds the user's buffer, update the skb and
1879 * push it back to the receive_queue so that the next call to
1880 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1882 if (skb_len > copied) {
1883 msg->msg_flags &= ~MSG_EOR;
1884 if (flags & MSG_PEEK)
1885 goto out_free;
1886 sctp_skb_pull(skb, copied);
1887 skb_queue_head(&sk->sk_receive_queue, skb);
1889 /* When only partial message is copied to the user, increase
1890 * rwnd by that amount. If all the data in the skb is read,
1891 * rwnd is updated when the event is freed.
1893 sctp_assoc_rwnd_increase(event->asoc, copied);
1894 goto out;
1895 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1896 (event->msg_flags & MSG_EOR))
1897 msg->msg_flags |= MSG_EOR;
1898 else
1899 msg->msg_flags &= ~MSG_EOR;
1901 out_free:
1902 if (flags & MSG_PEEK) {
1903 /* Release the skb reference acquired after peeking the skb in
1904 * sctp_skb_recv_datagram().
1906 kfree_skb(skb);
1907 } else {
1908 /* Free the event which includes releasing the reference to
1909 * the owner of the skb, freeing the skb and updating the
1910 * rwnd.
1912 sctp_ulpevent_free(event);
1914 out:
1915 sctp_release_sock(sk);
1916 return err;
1919 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1921 * This option is a on/off flag. If enabled no SCTP message
1922 * fragmentation will be performed. Instead if a message being sent
1923 * exceeds the current PMTU size, the message will NOT be sent and
1924 * instead a error will be indicated to the user.
1926 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1927 char __user *optval, int optlen)
1929 int val;
1931 if (optlen < sizeof(int))
1932 return -EINVAL;
1934 if (get_user(val, (int __user *)optval))
1935 return -EFAULT;
1937 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1939 return 0;
1942 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1943 int optlen)
1945 if (optlen != sizeof(struct sctp_event_subscribe))
1946 return -EINVAL;
1947 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1948 return -EFAULT;
1949 return 0;
1952 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1954 * This socket option is applicable to the UDP-style socket only. When
1955 * set it will cause associations that are idle for more than the
1956 * specified number of seconds to automatically close. An association
1957 * being idle is defined an association that has NOT sent or received
1958 * user data. The special value of '0' indicates that no automatic
1959 * close of any associations should be performed. The option expects an
1960 * integer defining the number of seconds of idle time before an
1961 * association is closed.
1963 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1964 int optlen)
1966 struct sctp_sock *sp = sctp_sk(sk);
1968 /* Applicable to UDP-style socket only */
1969 if (sctp_style(sk, TCP))
1970 return -EOPNOTSUPP;
1971 if (optlen != sizeof(int))
1972 return -EINVAL;
1973 if (copy_from_user(&sp->autoclose, optval, optlen))
1974 return -EFAULT;
1976 return 0;
1979 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1981 * Applications can enable or disable heartbeats for any peer address of
1982 * an association, modify an address's heartbeat interval, force a
1983 * heartbeat to be sent immediately, and adjust the address's maximum
1984 * number of retransmissions sent before an address is considered
1985 * unreachable. The following structure is used to access and modify an
1986 * address's parameters:
1988 * struct sctp_paddrparams {
1989 * sctp_assoc_t spp_assoc_id;
1990 * struct sockaddr_storage spp_address;
1991 * uint32_t spp_hbinterval;
1992 * uint16_t spp_pathmaxrxt;
1993 * uint32_t spp_pathmtu;
1994 * uint32_t spp_sackdelay;
1995 * uint32_t spp_flags;
1996 * };
1998 * spp_assoc_id - (one-to-many style socket) This is filled in the
1999 * application, and identifies the association for
2000 * this query.
2001 * spp_address - This specifies which address is of interest.
2002 * spp_hbinterval - This contains the value of the heartbeat interval,
2003 * in milliseconds. If a value of zero
2004 * is present in this field then no changes are to
2005 * be made to this parameter.
2006 * spp_pathmaxrxt - This contains the maximum number of
2007 * retransmissions before this address shall be
2008 * considered unreachable. If a value of zero
2009 * is present in this field then no changes are to
2010 * be made to this parameter.
2011 * spp_pathmtu - When Path MTU discovery is disabled the value
2012 * specified here will be the "fixed" path mtu.
2013 * Note that if the spp_address field is empty
2014 * then all associations on this address will
2015 * have this fixed path mtu set upon them.
2017 * spp_sackdelay - When delayed sack is enabled, this value specifies
2018 * the number of milliseconds that sacks will be delayed
2019 * for. This value will apply to all addresses of an
2020 * association if the spp_address field is empty. Note
2021 * also, that if delayed sack is enabled and this
2022 * value is set to 0, no change is made to the last
2023 * recorded delayed sack timer value.
2025 * spp_flags - These flags are used to control various features
2026 * on an association. The flag field may contain
2027 * zero or more of the following options.
2029 * SPP_HB_ENABLE - Enable heartbeats on the
2030 * specified address. Note that if the address
2031 * field is empty all addresses for the association
2032 * have heartbeats enabled upon them.
2034 * SPP_HB_DISABLE - Disable heartbeats on the
2035 * speicifed address. Note that if the address
2036 * field is empty all addresses for the association
2037 * will have their heartbeats disabled. Note also
2038 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2039 * mutually exclusive, only one of these two should
2040 * be specified. Enabling both fields will have
2041 * undetermined results.
2043 * SPP_HB_DEMAND - Request a user initiated heartbeat
2044 * to be made immediately.
2046 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2047 * heartbeat delayis to be set to the value of 0
2048 * milliseconds.
2050 * SPP_PMTUD_ENABLE - This field will enable PMTU
2051 * discovery upon the specified address. Note that
2052 * if the address feild is empty then all addresses
2053 * on the association are effected.
2055 * SPP_PMTUD_DISABLE - This field will disable PMTU
2056 * discovery upon the specified address. Note that
2057 * if the address feild is empty then all addresses
2058 * on the association are effected. Not also that
2059 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2060 * exclusive. Enabling both will have undetermined
2061 * results.
2063 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2064 * on delayed sack. The time specified in spp_sackdelay
2065 * is used to specify the sack delay for this address. Note
2066 * that if spp_address is empty then all addresses will
2067 * enable delayed sack and take on the sack delay
2068 * value specified in spp_sackdelay.
2069 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2070 * off delayed sack. If the spp_address field is blank then
2071 * delayed sack is disabled for the entire association. Note
2072 * also that this field is mutually exclusive to
2073 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2074 * results.
2076 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2077 struct sctp_transport *trans,
2078 struct sctp_association *asoc,
2079 struct sctp_sock *sp,
2080 int hb_change,
2081 int pmtud_change,
2082 int sackdelay_change)
2084 int error;
2086 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2087 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2088 if (error)
2089 return error;
2092 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2093 * this field is ignored. Note also that a value of zero indicates
2094 * the current setting should be left unchanged.
2096 if (params->spp_flags & SPP_HB_ENABLE) {
2098 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2099 * set. This lets us use 0 value when this flag
2100 * is set.
2102 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2103 params->spp_hbinterval = 0;
2105 if (params->spp_hbinterval ||
2106 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2107 if (trans) {
2108 trans->hbinterval =
2109 msecs_to_jiffies(params->spp_hbinterval);
2110 } else if (asoc) {
2111 asoc->hbinterval =
2112 msecs_to_jiffies(params->spp_hbinterval);
2113 } else {
2114 sp->hbinterval = params->spp_hbinterval;
2119 if (hb_change) {
2120 if (trans) {
2121 trans->param_flags =
2122 (trans->param_flags & ~SPP_HB) | hb_change;
2123 } else if (asoc) {
2124 asoc->param_flags =
2125 (asoc->param_flags & ~SPP_HB) | hb_change;
2126 } else {
2127 sp->param_flags =
2128 (sp->param_flags & ~SPP_HB) | hb_change;
2132 /* When Path MTU discovery is disabled the value specified here will
2133 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2134 * include the flag SPP_PMTUD_DISABLE for this field to have any
2135 * effect).
2137 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2138 if (trans) {
2139 trans->pathmtu = params->spp_pathmtu;
2140 sctp_assoc_sync_pmtu(asoc);
2141 } else if (asoc) {
2142 asoc->pathmtu = params->spp_pathmtu;
2143 sctp_frag_point(sp, params->spp_pathmtu);
2144 } else {
2145 sp->pathmtu = params->spp_pathmtu;
2149 if (pmtud_change) {
2150 if (trans) {
2151 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2152 (params->spp_flags & SPP_PMTUD_ENABLE);
2153 trans->param_flags =
2154 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2155 if (update) {
2156 sctp_transport_pmtu(trans);
2157 sctp_assoc_sync_pmtu(asoc);
2159 } else if (asoc) {
2160 asoc->param_flags =
2161 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2162 } else {
2163 sp->param_flags =
2164 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2168 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2169 * value of this field is ignored. Note also that a value of zero
2170 * indicates the current setting should be left unchanged.
2172 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2173 if (trans) {
2174 trans->sackdelay =
2175 msecs_to_jiffies(params->spp_sackdelay);
2176 } else if (asoc) {
2177 asoc->sackdelay =
2178 msecs_to_jiffies(params->spp_sackdelay);
2179 } else {
2180 sp->sackdelay = params->spp_sackdelay;
2184 if (sackdelay_change) {
2185 if (trans) {
2186 trans->param_flags =
2187 (trans->param_flags & ~SPP_SACKDELAY) |
2188 sackdelay_change;
2189 } else if (asoc) {
2190 asoc->param_flags =
2191 (asoc->param_flags & ~SPP_SACKDELAY) |
2192 sackdelay_change;
2193 } else {
2194 sp->param_flags =
2195 (sp->param_flags & ~SPP_SACKDELAY) |
2196 sackdelay_change;
2200 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2201 * of this field is ignored. Note also that a value of zero
2202 * indicates the current setting should be left unchanged.
2204 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
2205 if (trans) {
2206 trans->pathmaxrxt = params->spp_pathmaxrxt;
2207 } else if (asoc) {
2208 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2209 } else {
2210 sp->pathmaxrxt = params->spp_pathmaxrxt;
2214 return 0;
2217 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2218 char __user *optval, int optlen)
2220 struct sctp_paddrparams params;
2221 struct sctp_transport *trans = NULL;
2222 struct sctp_association *asoc = NULL;
2223 struct sctp_sock *sp = sctp_sk(sk);
2224 int error;
2225 int hb_change, pmtud_change, sackdelay_change;
2227 if (optlen != sizeof(struct sctp_paddrparams))
2228 return - EINVAL;
2230 if (copy_from_user(&params, optval, optlen))
2231 return -EFAULT;
2233 /* Validate flags and value parameters. */
2234 hb_change = params.spp_flags & SPP_HB;
2235 pmtud_change = params.spp_flags & SPP_PMTUD;
2236 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2238 if (hb_change == SPP_HB ||
2239 pmtud_change == SPP_PMTUD ||
2240 sackdelay_change == SPP_SACKDELAY ||
2241 params.spp_sackdelay > 500 ||
2242 (params.spp_pathmtu
2243 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2244 return -EINVAL;
2246 /* If an address other than INADDR_ANY is specified, and
2247 * no transport is found, then the request is invalid.
2249 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2250 trans = sctp_addr_id2transport(sk, &params.spp_address,
2251 params.spp_assoc_id);
2252 if (!trans)
2253 return -EINVAL;
2256 /* Get association, if assoc_id != 0 and the socket is a one
2257 * to many style socket, and an association was not found, then
2258 * the id was invalid.
2260 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2261 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2262 return -EINVAL;
2264 /* Heartbeat demand can only be sent on a transport or
2265 * association, but not a socket.
2267 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2268 return -EINVAL;
2270 /* Process parameters. */
2271 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2272 hb_change, pmtud_change,
2273 sackdelay_change);
2275 if (error)
2276 return error;
2278 /* If changes are for association, also apply parameters to each
2279 * transport.
2281 if (!trans && asoc) {
2282 struct list_head *pos;
2284 list_for_each(pos, &asoc->peer.transport_addr_list) {
2285 trans = list_entry(pos, struct sctp_transport,
2286 transports);
2287 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2288 hb_change, pmtud_change,
2289 sackdelay_change);
2293 return 0;
2296 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2298 * This options will get or set the delayed ack timer. The time is set
2299 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2300 * endpoints default delayed ack timer value. If the assoc_id field is
2301 * non-zero, then the set or get effects the specified association.
2303 * struct sctp_assoc_value {
2304 * sctp_assoc_t assoc_id;
2305 * uint32_t assoc_value;
2306 * };
2308 * assoc_id - This parameter, indicates which association the
2309 * user is preforming an action upon. Note that if
2310 * this field's value is zero then the endpoints
2311 * default value is changed (effecting future
2312 * associations only).
2314 * assoc_value - This parameter contains the number of milliseconds
2315 * that the user is requesting the delayed ACK timer
2316 * be set to. Note that this value is defined in
2317 * the standard to be between 200 and 500 milliseconds.
2319 * Note: a value of zero will leave the value alone,
2320 * but disable SACK delay. A non-zero value will also
2321 * enable SACK delay.
2324 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2325 char __user *optval, int optlen)
2327 struct sctp_assoc_value params;
2328 struct sctp_transport *trans = NULL;
2329 struct sctp_association *asoc = NULL;
2330 struct sctp_sock *sp = sctp_sk(sk);
2332 if (optlen != sizeof(struct sctp_assoc_value))
2333 return - EINVAL;
2335 if (copy_from_user(&params, optval, optlen))
2336 return -EFAULT;
2338 /* Validate value parameter. */
2339 if (params.assoc_value > 500)
2340 return -EINVAL;
2342 /* Get association, if assoc_id != 0 and the socket is a one
2343 * to many style socket, and an association was not found, then
2344 * the id was invalid.
2346 asoc = sctp_id2assoc(sk, params.assoc_id);
2347 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2348 return -EINVAL;
2350 if (params.assoc_value) {
2351 if (asoc) {
2352 asoc->sackdelay =
2353 msecs_to_jiffies(params.assoc_value);
2354 asoc->param_flags =
2355 (asoc->param_flags & ~SPP_SACKDELAY) |
2356 SPP_SACKDELAY_ENABLE;
2357 } else {
2358 sp->sackdelay = params.assoc_value;
2359 sp->param_flags =
2360 (sp->param_flags & ~SPP_SACKDELAY) |
2361 SPP_SACKDELAY_ENABLE;
2363 } else {
2364 if (asoc) {
2365 asoc->param_flags =
2366 (asoc->param_flags & ~SPP_SACKDELAY) |
2367 SPP_SACKDELAY_DISABLE;
2368 } else {
2369 sp->param_flags =
2370 (sp->param_flags & ~SPP_SACKDELAY) |
2371 SPP_SACKDELAY_DISABLE;
2375 /* If change is for association, also apply to each transport. */
2376 if (asoc) {
2377 struct list_head *pos;
2379 list_for_each(pos, &asoc->peer.transport_addr_list) {
2380 trans = list_entry(pos, struct sctp_transport,
2381 transports);
2382 if (params.assoc_value) {
2383 trans->sackdelay =
2384 msecs_to_jiffies(params.assoc_value);
2385 trans->param_flags =
2386 (trans->param_flags & ~SPP_SACKDELAY) |
2387 SPP_SACKDELAY_ENABLE;
2388 } else {
2389 trans->param_flags =
2390 (trans->param_flags & ~SPP_SACKDELAY) |
2391 SPP_SACKDELAY_DISABLE;
2396 return 0;
2399 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2401 * Applications can specify protocol parameters for the default association
2402 * initialization. The option name argument to setsockopt() and getsockopt()
2403 * is SCTP_INITMSG.
2405 * Setting initialization parameters is effective only on an unconnected
2406 * socket (for UDP-style sockets only future associations are effected
2407 * by the change). With TCP-style sockets, this option is inherited by
2408 * sockets derived from a listener socket.
2410 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2412 struct sctp_initmsg sinit;
2413 struct sctp_sock *sp = sctp_sk(sk);
2415 if (optlen != sizeof(struct sctp_initmsg))
2416 return -EINVAL;
2417 if (copy_from_user(&sinit, optval, optlen))
2418 return -EFAULT;
2420 if (sinit.sinit_num_ostreams)
2421 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2422 if (sinit.sinit_max_instreams)
2423 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2424 if (sinit.sinit_max_attempts)
2425 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2426 if (sinit.sinit_max_init_timeo)
2427 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2429 return 0;
2433 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2435 * Applications that wish to use the sendto() system call may wish to
2436 * specify a default set of parameters that would normally be supplied
2437 * through the inclusion of ancillary data. This socket option allows
2438 * such an application to set the default sctp_sndrcvinfo structure.
2439 * The application that wishes to use this socket option simply passes
2440 * in to this call the sctp_sndrcvinfo structure defined in Section
2441 * 5.2.2) The input parameters accepted by this call include
2442 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2443 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2444 * to this call if the caller is using the UDP model.
2446 static int sctp_setsockopt_default_send_param(struct sock *sk,
2447 char __user *optval, int optlen)
2449 struct sctp_sndrcvinfo info;
2450 struct sctp_association *asoc;
2451 struct sctp_sock *sp = sctp_sk(sk);
2453 if (optlen != sizeof(struct sctp_sndrcvinfo))
2454 return -EINVAL;
2455 if (copy_from_user(&info, optval, optlen))
2456 return -EFAULT;
2458 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2459 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2460 return -EINVAL;
2462 if (asoc) {
2463 asoc->default_stream = info.sinfo_stream;
2464 asoc->default_flags = info.sinfo_flags;
2465 asoc->default_ppid = info.sinfo_ppid;
2466 asoc->default_context = info.sinfo_context;
2467 asoc->default_timetolive = info.sinfo_timetolive;
2468 } else {
2469 sp->default_stream = info.sinfo_stream;
2470 sp->default_flags = info.sinfo_flags;
2471 sp->default_ppid = info.sinfo_ppid;
2472 sp->default_context = info.sinfo_context;
2473 sp->default_timetolive = info.sinfo_timetolive;
2476 return 0;
2479 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2481 * Requests that the local SCTP stack use the enclosed peer address as
2482 * the association primary. The enclosed address must be one of the
2483 * association peer's addresses.
2485 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2486 int optlen)
2488 struct sctp_prim prim;
2489 struct sctp_transport *trans;
2491 if (optlen != sizeof(struct sctp_prim))
2492 return -EINVAL;
2494 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2495 return -EFAULT;
2497 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2498 if (!trans)
2499 return -EINVAL;
2501 sctp_assoc_set_primary(trans->asoc, trans);
2503 return 0;
2507 * 7.1.5 SCTP_NODELAY
2509 * Turn on/off any Nagle-like algorithm. This means that packets are
2510 * generally sent as soon as possible and no unnecessary delays are
2511 * introduced, at the cost of more packets in the network. Expects an
2512 * integer boolean flag.
2514 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2515 int optlen)
2517 int val;
2519 if (optlen < sizeof(int))
2520 return -EINVAL;
2521 if (get_user(val, (int __user *)optval))
2522 return -EFAULT;
2524 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2525 return 0;
2530 * 7.1.1 SCTP_RTOINFO
2532 * The protocol parameters used to initialize and bound retransmission
2533 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2534 * and modify these parameters.
2535 * All parameters are time values, in milliseconds. A value of 0, when
2536 * modifying the parameters, indicates that the current value should not
2537 * be changed.
2540 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2541 struct sctp_rtoinfo rtoinfo;
2542 struct sctp_association *asoc;
2544 if (optlen != sizeof (struct sctp_rtoinfo))
2545 return -EINVAL;
2547 if (copy_from_user(&rtoinfo, optval, optlen))
2548 return -EFAULT;
2550 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2552 /* Set the values to the specific association */
2553 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2554 return -EINVAL;
2556 if (asoc) {
2557 if (rtoinfo.srto_initial != 0)
2558 asoc->rto_initial =
2559 msecs_to_jiffies(rtoinfo.srto_initial);
2560 if (rtoinfo.srto_max != 0)
2561 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2562 if (rtoinfo.srto_min != 0)
2563 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2564 } else {
2565 /* If there is no association or the association-id = 0
2566 * set the values to the endpoint.
2568 struct sctp_sock *sp = sctp_sk(sk);
2570 if (rtoinfo.srto_initial != 0)
2571 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2572 if (rtoinfo.srto_max != 0)
2573 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2574 if (rtoinfo.srto_min != 0)
2575 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2578 return 0;
2583 * 7.1.2 SCTP_ASSOCINFO
2585 * This option is used to tune the maximum retransmission attempts
2586 * of the association.
2587 * Returns an error if the new association retransmission value is
2588 * greater than the sum of the retransmission value of the peer.
2589 * See [SCTP] for more information.
2592 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2595 struct sctp_assocparams assocparams;
2596 struct sctp_association *asoc;
2598 if (optlen != sizeof(struct sctp_assocparams))
2599 return -EINVAL;
2600 if (copy_from_user(&assocparams, optval, optlen))
2601 return -EFAULT;
2603 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2605 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2606 return -EINVAL;
2608 /* Set the values to the specific association */
2609 if (asoc) {
2610 if (assocparams.sasoc_asocmaxrxt != 0) {
2611 __u32 path_sum = 0;
2612 int paths = 0;
2613 struct list_head *pos;
2614 struct sctp_transport *peer_addr;
2616 list_for_each(pos, &asoc->peer.transport_addr_list) {
2617 peer_addr = list_entry(pos,
2618 struct sctp_transport,
2619 transports);
2620 path_sum += peer_addr->pathmaxrxt;
2621 paths++;
2624 /* Only validate asocmaxrxt if we have more then
2625 * one path/transport. We do this because path
2626 * retransmissions are only counted when we have more
2627 * then one path.
2629 if (paths > 1 &&
2630 assocparams.sasoc_asocmaxrxt > path_sum)
2631 return -EINVAL;
2633 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2636 if (assocparams.sasoc_cookie_life != 0) {
2637 asoc->cookie_life.tv_sec =
2638 assocparams.sasoc_cookie_life / 1000;
2639 asoc->cookie_life.tv_usec =
2640 (assocparams.sasoc_cookie_life % 1000)
2641 * 1000;
2643 } else {
2644 /* Set the values to the endpoint */
2645 struct sctp_sock *sp = sctp_sk(sk);
2647 if (assocparams.sasoc_asocmaxrxt != 0)
2648 sp->assocparams.sasoc_asocmaxrxt =
2649 assocparams.sasoc_asocmaxrxt;
2650 if (assocparams.sasoc_cookie_life != 0)
2651 sp->assocparams.sasoc_cookie_life =
2652 assocparams.sasoc_cookie_life;
2654 return 0;
2658 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2660 * This socket option is a boolean flag which turns on or off mapped V4
2661 * addresses. If this option is turned on and the socket is type
2662 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2663 * If this option is turned off, then no mapping will be done of V4
2664 * addresses and a user will receive both PF_INET6 and PF_INET type
2665 * addresses on the socket.
2667 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2669 int val;
2670 struct sctp_sock *sp = sctp_sk(sk);
2672 if (optlen < sizeof(int))
2673 return -EINVAL;
2674 if (get_user(val, (int __user *)optval))
2675 return -EFAULT;
2676 if (val)
2677 sp->v4mapped = 1;
2678 else
2679 sp->v4mapped = 0;
2681 return 0;
2685 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2687 * This socket option specifies the maximum size to put in any outgoing
2688 * SCTP chunk. If a message is larger than this size it will be
2689 * fragmented by SCTP into the specified size. Note that the underlying
2690 * SCTP implementation may fragment into smaller sized chunks when the
2691 * PMTU of the underlying association is smaller than the value set by
2692 * the user.
2694 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2696 struct sctp_association *asoc;
2697 struct list_head *pos;
2698 struct sctp_sock *sp = sctp_sk(sk);
2699 int val;
2701 if (optlen < sizeof(int))
2702 return -EINVAL;
2703 if (get_user(val, (int __user *)optval))
2704 return -EFAULT;
2705 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2706 return -EINVAL;
2707 sp->user_frag = val;
2709 /* Update the frag_point of the existing associations. */
2710 list_for_each(pos, &(sp->ep->asocs)) {
2711 asoc = list_entry(pos, struct sctp_association, asocs);
2712 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2715 return 0;
2720 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2722 * Requests that the peer mark the enclosed address as the association
2723 * primary. The enclosed address must be one of the association's
2724 * locally bound addresses. The following structure is used to make a
2725 * set primary request:
2727 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2728 int optlen)
2730 struct sctp_sock *sp;
2731 struct sctp_endpoint *ep;
2732 struct sctp_association *asoc = NULL;
2733 struct sctp_setpeerprim prim;
2734 struct sctp_chunk *chunk;
2735 int err;
2737 sp = sctp_sk(sk);
2738 ep = sp->ep;
2740 if (!sctp_addip_enable)
2741 return -EPERM;
2743 if (optlen != sizeof(struct sctp_setpeerprim))
2744 return -EINVAL;
2746 if (copy_from_user(&prim, optval, optlen))
2747 return -EFAULT;
2749 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2750 if (!asoc)
2751 return -EINVAL;
2753 if (!asoc->peer.asconf_capable)
2754 return -EPERM;
2756 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2757 return -EPERM;
2759 if (!sctp_state(asoc, ESTABLISHED))
2760 return -ENOTCONN;
2762 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2763 return -EADDRNOTAVAIL;
2765 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2766 chunk = sctp_make_asconf_set_prim(asoc,
2767 (union sctp_addr *)&prim.sspp_addr);
2768 if (!chunk)
2769 return -ENOMEM;
2771 err = sctp_send_asconf(asoc, chunk);
2773 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2775 return err;
2778 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2779 int optlen)
2781 struct sctp_setadaptation adaptation;
2783 if (optlen != sizeof(struct sctp_setadaptation))
2784 return -EINVAL;
2785 if (copy_from_user(&adaptation, optval, optlen))
2786 return -EFAULT;
2788 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2790 return 0;
2794 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2796 * The context field in the sctp_sndrcvinfo structure is normally only
2797 * used when a failed message is retrieved holding the value that was
2798 * sent down on the actual send call. This option allows the setting of
2799 * a default context on an association basis that will be received on
2800 * reading messages from the peer. This is especially helpful in the
2801 * one-2-many model for an application to keep some reference to an
2802 * internal state machine that is processing messages on the
2803 * association. Note that the setting of this value only effects
2804 * received messages from the peer and does not effect the value that is
2805 * saved with outbound messages.
2807 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2808 int optlen)
2810 struct sctp_assoc_value params;
2811 struct sctp_sock *sp;
2812 struct sctp_association *asoc;
2814 if (optlen != sizeof(struct sctp_assoc_value))
2815 return -EINVAL;
2816 if (copy_from_user(&params, optval, optlen))
2817 return -EFAULT;
2819 sp = sctp_sk(sk);
2821 if (params.assoc_id != 0) {
2822 asoc = sctp_id2assoc(sk, params.assoc_id);
2823 if (!asoc)
2824 return -EINVAL;
2825 asoc->default_rcv_context = params.assoc_value;
2826 } else {
2827 sp->default_rcv_context = params.assoc_value;
2830 return 0;
2834 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2836 * This options will at a minimum specify if the implementation is doing
2837 * fragmented interleave. Fragmented interleave, for a one to many
2838 * socket, is when subsequent calls to receive a message may return
2839 * parts of messages from different associations. Some implementations
2840 * may allow you to turn this value on or off. If so, when turned off,
2841 * no fragment interleave will occur (which will cause a head of line
2842 * blocking amongst multiple associations sharing the same one to many
2843 * socket). When this option is turned on, then each receive call may
2844 * come from a different association (thus the user must receive data
2845 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2846 * association each receive belongs to.
2848 * This option takes a boolean value. A non-zero value indicates that
2849 * fragmented interleave is on. A value of zero indicates that
2850 * fragmented interleave is off.
2852 * Note that it is important that an implementation that allows this
2853 * option to be turned on, have it off by default. Otherwise an unaware
2854 * application using the one to many model may become confused and act
2855 * incorrectly.
2857 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2858 char __user *optval,
2859 int optlen)
2861 int val;
2863 if (optlen != sizeof(int))
2864 return -EINVAL;
2865 if (get_user(val, (int __user *)optval))
2866 return -EFAULT;
2868 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2870 return 0;
2874 * 7.1.25. Set or Get the sctp partial delivery point
2875 * (SCTP_PARTIAL_DELIVERY_POINT)
2876 * This option will set or get the SCTP partial delivery point. This
2877 * point is the size of a message where the partial delivery API will be
2878 * invoked to help free up rwnd space for the peer. Setting this to a
2879 * lower value will cause partial delivery's to happen more often. The
2880 * calls argument is an integer that sets or gets the partial delivery
2881 * point.
2883 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2884 char __user *optval,
2885 int optlen)
2887 u32 val;
2889 if (optlen != sizeof(u32))
2890 return -EINVAL;
2891 if (get_user(val, (int __user *)optval))
2892 return -EFAULT;
2894 sctp_sk(sk)->pd_point = val;
2896 return 0; /* is this the right error code? */
2900 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2902 * This option will allow a user to change the maximum burst of packets
2903 * that can be emitted by this association. Note that the default value
2904 * is 4, and some implementations may restrict this setting so that it
2905 * can only be lowered.
2907 * NOTE: This text doesn't seem right. Do this on a socket basis with
2908 * future associations inheriting the socket value.
2910 static int sctp_setsockopt_maxburst(struct sock *sk,
2911 char __user *optval,
2912 int optlen)
2914 int val;
2916 if (optlen != sizeof(int))
2917 return -EINVAL;
2918 if (get_user(val, (int __user *)optval))
2919 return -EFAULT;
2921 if (val < 0)
2922 return -EINVAL;
2924 sctp_sk(sk)->max_burst = val;
2926 return 0;
2929 /* API 6.2 setsockopt(), getsockopt()
2931 * Applications use setsockopt() and getsockopt() to set or retrieve
2932 * socket options. Socket options are used to change the default
2933 * behavior of sockets calls. They are described in Section 7.
2935 * The syntax is:
2937 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2938 * int __user *optlen);
2939 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2940 * int optlen);
2942 * sd - the socket descript.
2943 * level - set to IPPROTO_SCTP for all SCTP options.
2944 * optname - the option name.
2945 * optval - the buffer to store the value of the option.
2946 * optlen - the size of the buffer.
2948 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2949 char __user *optval, int optlen)
2951 int retval = 0;
2953 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2954 sk, optname);
2956 /* I can hardly begin to describe how wrong this is. This is
2957 * so broken as to be worse than useless. The API draft
2958 * REALLY is NOT helpful here... I am not convinced that the
2959 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2960 * are at all well-founded.
2962 if (level != SOL_SCTP) {
2963 struct sctp_af *af = sctp_sk(sk)->pf->af;
2964 retval = af->setsockopt(sk, level, optname, optval, optlen);
2965 goto out_nounlock;
2968 sctp_lock_sock(sk);
2970 switch (optname) {
2971 case SCTP_SOCKOPT_BINDX_ADD:
2972 /* 'optlen' is the size of the addresses buffer. */
2973 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2974 optlen, SCTP_BINDX_ADD_ADDR);
2975 break;
2977 case SCTP_SOCKOPT_BINDX_REM:
2978 /* 'optlen' is the size of the addresses buffer. */
2979 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2980 optlen, SCTP_BINDX_REM_ADDR);
2981 break;
2983 case SCTP_SOCKOPT_CONNECTX:
2984 /* 'optlen' is the size of the addresses buffer. */
2985 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2986 optlen);
2987 break;
2989 case SCTP_DISABLE_FRAGMENTS:
2990 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2991 break;
2993 case SCTP_EVENTS:
2994 retval = sctp_setsockopt_events(sk, optval, optlen);
2995 break;
2997 case SCTP_AUTOCLOSE:
2998 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2999 break;
3001 case SCTP_PEER_ADDR_PARAMS:
3002 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3003 break;
3005 case SCTP_DELAYED_ACK_TIME:
3006 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3007 break;
3008 case SCTP_PARTIAL_DELIVERY_POINT:
3009 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3010 break;
3012 case SCTP_INITMSG:
3013 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3014 break;
3015 case SCTP_DEFAULT_SEND_PARAM:
3016 retval = sctp_setsockopt_default_send_param(sk, optval,
3017 optlen);
3018 break;
3019 case SCTP_PRIMARY_ADDR:
3020 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3021 break;
3022 case SCTP_SET_PEER_PRIMARY_ADDR:
3023 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3024 break;
3025 case SCTP_NODELAY:
3026 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3027 break;
3028 case SCTP_RTOINFO:
3029 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3030 break;
3031 case SCTP_ASSOCINFO:
3032 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3033 break;
3034 case SCTP_I_WANT_MAPPED_V4_ADDR:
3035 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3036 break;
3037 case SCTP_MAXSEG:
3038 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3039 break;
3040 case SCTP_ADAPTATION_LAYER:
3041 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3042 break;
3043 case SCTP_CONTEXT:
3044 retval = sctp_setsockopt_context(sk, optval, optlen);
3045 break;
3046 case SCTP_FRAGMENT_INTERLEAVE:
3047 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3048 break;
3049 case SCTP_MAX_BURST:
3050 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3051 break;
3052 default:
3053 retval = -ENOPROTOOPT;
3054 break;
3057 sctp_release_sock(sk);
3059 out_nounlock:
3060 return retval;
3063 /* API 3.1.6 connect() - UDP Style Syntax
3065 * An application may use the connect() call in the UDP model to initiate an
3066 * association without sending data.
3068 * The syntax is:
3070 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3072 * sd: the socket descriptor to have a new association added to.
3074 * nam: the address structure (either struct sockaddr_in or struct
3075 * sockaddr_in6 defined in RFC2553 [7]).
3077 * len: the size of the address.
3079 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3080 int addr_len)
3082 int err = 0;
3083 struct sctp_af *af;
3085 sctp_lock_sock(sk);
3087 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3088 __FUNCTION__, sk, addr, addr_len);
3090 /* Validate addr_len before calling common connect/connectx routine. */
3091 af = sctp_get_af_specific(addr->sa_family);
3092 if (!af || addr_len < af->sockaddr_len) {
3093 err = -EINVAL;
3094 } else {
3095 /* Pass correct addr len to common routine (so it knows there
3096 * is only one address being passed.
3098 err = __sctp_connect(sk, addr, af->sockaddr_len);
3101 sctp_release_sock(sk);
3102 return err;
3105 /* FIXME: Write comments. */
3106 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3108 return -EOPNOTSUPP; /* STUB */
3111 /* 4.1.4 accept() - TCP Style Syntax
3113 * Applications use accept() call to remove an established SCTP
3114 * association from the accept queue of the endpoint. A new socket
3115 * descriptor will be returned from accept() to represent the newly
3116 * formed association.
3118 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3120 struct sctp_sock *sp;
3121 struct sctp_endpoint *ep;
3122 struct sock *newsk = NULL;
3123 struct sctp_association *asoc;
3124 long timeo;
3125 int error = 0;
3127 sctp_lock_sock(sk);
3129 sp = sctp_sk(sk);
3130 ep = sp->ep;
3132 if (!sctp_style(sk, TCP)) {
3133 error = -EOPNOTSUPP;
3134 goto out;
3137 if (!sctp_sstate(sk, LISTENING)) {
3138 error = -EINVAL;
3139 goto out;
3142 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3144 error = sctp_wait_for_accept(sk, timeo);
3145 if (error)
3146 goto out;
3148 /* We treat the list of associations on the endpoint as the accept
3149 * queue and pick the first association on the list.
3151 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3153 newsk = sp->pf->create_accept_sk(sk, asoc);
3154 if (!newsk) {
3155 error = -ENOMEM;
3156 goto out;
3159 /* Populate the fields of the newsk from the oldsk and migrate the
3160 * asoc to the newsk.
3162 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3164 out:
3165 sctp_release_sock(sk);
3166 *err = error;
3167 return newsk;
3170 /* The SCTP ioctl handler. */
3171 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3173 return -ENOIOCTLCMD;
3176 /* This is the function which gets called during socket creation to
3177 * initialized the SCTP-specific portion of the sock.
3178 * The sock structure should already be zero-filled memory.
3180 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3182 struct sctp_endpoint *ep;
3183 struct sctp_sock *sp;
3185 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3187 sp = sctp_sk(sk);
3189 /* Initialize the SCTP per socket area. */
3190 switch (sk->sk_type) {
3191 case SOCK_SEQPACKET:
3192 sp->type = SCTP_SOCKET_UDP;
3193 break;
3194 case SOCK_STREAM:
3195 sp->type = SCTP_SOCKET_TCP;
3196 break;
3197 default:
3198 return -ESOCKTNOSUPPORT;
3201 /* Initialize default send parameters. These parameters can be
3202 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3204 sp->default_stream = 0;
3205 sp->default_ppid = 0;
3206 sp->default_flags = 0;
3207 sp->default_context = 0;
3208 sp->default_timetolive = 0;
3210 sp->default_rcv_context = 0;
3211 sp->max_burst = sctp_max_burst;
3213 /* Initialize default setup parameters. These parameters
3214 * can be modified with the SCTP_INITMSG socket option or
3215 * overridden by the SCTP_INIT CMSG.
3217 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3218 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3219 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3220 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3222 /* Initialize default RTO related parameters. These parameters can
3223 * be modified for with the SCTP_RTOINFO socket option.
3225 sp->rtoinfo.srto_initial = sctp_rto_initial;
3226 sp->rtoinfo.srto_max = sctp_rto_max;
3227 sp->rtoinfo.srto_min = sctp_rto_min;
3229 /* Initialize default association related parameters. These parameters
3230 * can be modified with the SCTP_ASSOCINFO socket option.
3232 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3233 sp->assocparams.sasoc_number_peer_destinations = 0;
3234 sp->assocparams.sasoc_peer_rwnd = 0;
3235 sp->assocparams.sasoc_local_rwnd = 0;
3236 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3238 /* Initialize default event subscriptions. By default, all the
3239 * options are off.
3241 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3243 /* Default Peer Address Parameters. These defaults can
3244 * be modified via SCTP_PEER_ADDR_PARAMS
3246 sp->hbinterval = sctp_hb_interval;
3247 sp->pathmaxrxt = sctp_max_retrans_path;
3248 sp->pathmtu = 0; // allow default discovery
3249 sp->sackdelay = sctp_sack_timeout;
3250 sp->param_flags = SPP_HB_ENABLE |
3251 SPP_PMTUD_ENABLE |
3252 SPP_SACKDELAY_ENABLE;
3254 /* If enabled no SCTP message fragmentation will be performed.
3255 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3257 sp->disable_fragments = 0;
3259 /* Enable Nagle algorithm by default. */
3260 sp->nodelay = 0;
3262 /* Enable by default. */
3263 sp->v4mapped = 1;
3265 /* Auto-close idle associations after the configured
3266 * number of seconds. A value of 0 disables this
3267 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3268 * for UDP-style sockets only.
3270 sp->autoclose = 0;
3272 /* User specified fragmentation limit. */
3273 sp->user_frag = 0;
3275 sp->adaptation_ind = 0;
3277 sp->pf = sctp_get_pf_specific(sk->sk_family);
3279 /* Control variables for partial data delivery. */
3280 atomic_set(&sp->pd_mode, 0);
3281 skb_queue_head_init(&sp->pd_lobby);
3282 sp->frag_interleave = 0;
3284 /* Create a per socket endpoint structure. Even if we
3285 * change the data structure relationships, this may still
3286 * be useful for storing pre-connect address information.
3288 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3289 if (!ep)
3290 return -ENOMEM;
3292 sp->ep = ep;
3293 sp->hmac = NULL;
3295 SCTP_DBG_OBJCNT_INC(sock);
3296 return 0;
3299 /* Cleanup any SCTP per socket resources. */
3300 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3302 struct sctp_endpoint *ep;
3304 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3306 /* Release our hold on the endpoint. */
3307 ep = sctp_sk(sk)->ep;
3308 sctp_endpoint_free(ep);
3310 return 0;
3313 /* API 4.1.7 shutdown() - TCP Style Syntax
3314 * int shutdown(int socket, int how);
3316 * sd - the socket descriptor of the association to be closed.
3317 * how - Specifies the type of shutdown. The values are
3318 * as follows:
3319 * SHUT_RD
3320 * Disables further receive operations. No SCTP
3321 * protocol action is taken.
3322 * SHUT_WR
3323 * Disables further send operations, and initiates
3324 * the SCTP shutdown sequence.
3325 * SHUT_RDWR
3326 * Disables further send and receive operations
3327 * and initiates the SCTP shutdown sequence.
3329 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3331 struct sctp_endpoint *ep;
3332 struct sctp_association *asoc;
3334 if (!sctp_style(sk, TCP))
3335 return;
3337 if (how & SEND_SHUTDOWN) {
3338 ep = sctp_sk(sk)->ep;
3339 if (!list_empty(&ep->asocs)) {
3340 asoc = list_entry(ep->asocs.next,
3341 struct sctp_association, asocs);
3342 sctp_primitive_SHUTDOWN(asoc, NULL);
3347 /* 7.2.1 Association Status (SCTP_STATUS)
3349 * Applications can retrieve current status information about an
3350 * association, including association state, peer receiver window size,
3351 * number of unacked data chunks, and number of data chunks pending
3352 * receipt. This information is read-only.
3354 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3355 char __user *optval,
3356 int __user *optlen)
3358 struct sctp_status status;
3359 struct sctp_association *asoc = NULL;
3360 struct sctp_transport *transport;
3361 sctp_assoc_t associd;
3362 int retval = 0;
3364 if (len < sizeof(status)) {
3365 retval = -EINVAL;
3366 goto out;
3369 len = sizeof(status);
3370 if (copy_from_user(&status, optval, len)) {
3371 retval = -EFAULT;
3372 goto out;
3375 associd = status.sstat_assoc_id;
3376 asoc = sctp_id2assoc(sk, associd);
3377 if (!asoc) {
3378 retval = -EINVAL;
3379 goto out;
3382 transport = asoc->peer.primary_path;
3384 status.sstat_assoc_id = sctp_assoc2id(asoc);
3385 status.sstat_state = asoc->state;
3386 status.sstat_rwnd = asoc->peer.rwnd;
3387 status.sstat_unackdata = asoc->unack_data;
3389 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3390 status.sstat_instrms = asoc->c.sinit_max_instreams;
3391 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3392 status.sstat_fragmentation_point = asoc->frag_point;
3393 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3394 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3395 transport->af_specific->sockaddr_len);
3396 /* Map ipv4 address into v4-mapped-on-v6 address. */
3397 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3398 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3399 status.sstat_primary.spinfo_state = transport->state;
3400 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3401 status.sstat_primary.spinfo_srtt = transport->srtt;
3402 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3403 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3405 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3406 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3408 if (put_user(len, optlen)) {
3409 retval = -EFAULT;
3410 goto out;
3413 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3414 len, status.sstat_state, status.sstat_rwnd,
3415 status.sstat_assoc_id);
3417 if (copy_to_user(optval, &status, len)) {
3418 retval = -EFAULT;
3419 goto out;
3422 out:
3423 return (retval);
3427 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3429 * Applications can retrieve information about a specific peer address
3430 * of an association, including its reachability state, congestion
3431 * window, and retransmission timer values. This information is
3432 * read-only.
3434 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3435 char __user *optval,
3436 int __user *optlen)
3438 struct sctp_paddrinfo pinfo;
3439 struct sctp_transport *transport;
3440 int retval = 0;
3442 if (len < sizeof(pinfo)) {
3443 retval = -EINVAL;
3444 goto out;
3447 len = sizeof(pinfo);
3448 if (copy_from_user(&pinfo, optval, len)) {
3449 retval = -EFAULT;
3450 goto out;
3453 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3454 pinfo.spinfo_assoc_id);
3455 if (!transport)
3456 return -EINVAL;
3458 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3459 pinfo.spinfo_state = transport->state;
3460 pinfo.spinfo_cwnd = transport->cwnd;
3461 pinfo.spinfo_srtt = transport->srtt;
3462 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3463 pinfo.spinfo_mtu = transport->pathmtu;
3465 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3466 pinfo.spinfo_state = SCTP_ACTIVE;
3468 if (put_user(len, optlen)) {
3469 retval = -EFAULT;
3470 goto out;
3473 if (copy_to_user(optval, &pinfo, len)) {
3474 retval = -EFAULT;
3475 goto out;
3478 out:
3479 return (retval);
3482 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3484 * This option is a on/off flag. If enabled no SCTP message
3485 * fragmentation will be performed. Instead if a message being sent
3486 * exceeds the current PMTU size, the message will NOT be sent and
3487 * instead a error will be indicated to the user.
3489 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3490 char __user *optval, int __user *optlen)
3492 int val;
3494 if (len < sizeof(int))
3495 return -EINVAL;
3497 len = sizeof(int);
3498 val = (sctp_sk(sk)->disable_fragments == 1);
3499 if (put_user(len, optlen))
3500 return -EFAULT;
3501 if (copy_to_user(optval, &val, len))
3502 return -EFAULT;
3503 return 0;
3506 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3508 * This socket option is used to specify various notifications and
3509 * ancillary data the user wishes to receive.
3511 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3512 int __user *optlen)
3514 if (len < sizeof(struct sctp_event_subscribe))
3515 return -EINVAL;
3516 len = sizeof(struct sctp_event_subscribe);
3517 if (put_user(len, optlen))
3518 return -EFAULT;
3519 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3520 return -EFAULT;
3521 return 0;
3524 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3526 * This socket option is applicable to the UDP-style socket only. When
3527 * set it will cause associations that are idle for more than the
3528 * specified number of seconds to automatically close. An association
3529 * being idle is defined an association that has NOT sent or received
3530 * user data. The special value of '0' indicates that no automatic
3531 * close of any associations should be performed. The option expects an
3532 * integer defining the number of seconds of idle time before an
3533 * association is closed.
3535 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3537 /* Applicable to UDP-style socket only */
3538 if (sctp_style(sk, TCP))
3539 return -EOPNOTSUPP;
3540 if (len < sizeof(int))
3541 return -EINVAL;
3542 len = sizeof(int);
3543 if (put_user(len, optlen))
3544 return -EFAULT;
3545 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
3546 return -EFAULT;
3547 return 0;
3550 /* Helper routine to branch off an association to a new socket. */
3551 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3552 struct socket **sockp)
3554 struct sock *sk = asoc->base.sk;
3555 struct socket *sock;
3556 struct inet_sock *inetsk;
3557 struct sctp_af *af;
3558 int err = 0;
3560 /* An association cannot be branched off from an already peeled-off
3561 * socket, nor is this supported for tcp style sockets.
3563 if (!sctp_style(sk, UDP))
3564 return -EINVAL;
3566 /* Create a new socket. */
3567 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3568 if (err < 0)
3569 return err;
3571 /* Populate the fields of the newsk from the oldsk and migrate the
3572 * asoc to the newsk.
3574 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3576 /* Make peeled-off sockets more like 1-1 accepted sockets.
3577 * Set the daddr and initialize id to something more random
3579 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3580 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3581 inetsk = inet_sk(sock->sk);
3582 inetsk->id = asoc->next_tsn ^ jiffies;
3584 *sockp = sock;
3586 return err;
3589 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3591 sctp_peeloff_arg_t peeloff;
3592 struct socket *newsock;
3593 int retval = 0;
3594 struct sctp_association *asoc;
3596 if (len < sizeof(sctp_peeloff_arg_t))
3597 return -EINVAL;
3598 len = sizeof(sctp_peeloff_arg_t);
3599 if (copy_from_user(&peeloff, optval, len))
3600 return -EFAULT;
3602 asoc = sctp_id2assoc(sk, peeloff.associd);
3603 if (!asoc) {
3604 retval = -EINVAL;
3605 goto out;
3608 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3610 retval = sctp_do_peeloff(asoc, &newsock);
3611 if (retval < 0)
3612 goto out;
3614 /* Map the socket to an unused fd that can be returned to the user. */
3615 retval = sock_map_fd(newsock);
3616 if (retval < 0) {
3617 sock_release(newsock);
3618 goto out;
3621 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3622 __FUNCTION__, sk, asoc, newsock->sk, retval);
3624 /* Return the fd mapped to the new socket. */
3625 peeloff.sd = retval;
3626 if (put_user(len, optlen))
3627 return -EFAULT;
3628 if (copy_to_user(optval, &peeloff, len))
3629 retval = -EFAULT;
3631 out:
3632 return retval;
3635 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3637 * Applications can enable or disable heartbeats for any peer address of
3638 * an association, modify an address's heartbeat interval, force a
3639 * heartbeat to be sent immediately, and adjust the address's maximum
3640 * number of retransmissions sent before an address is considered
3641 * unreachable. The following structure is used to access and modify an
3642 * address's parameters:
3644 * struct sctp_paddrparams {
3645 * sctp_assoc_t spp_assoc_id;
3646 * struct sockaddr_storage spp_address;
3647 * uint32_t spp_hbinterval;
3648 * uint16_t spp_pathmaxrxt;
3649 * uint32_t spp_pathmtu;
3650 * uint32_t spp_sackdelay;
3651 * uint32_t spp_flags;
3652 * };
3654 * spp_assoc_id - (one-to-many style socket) This is filled in the
3655 * application, and identifies the association for
3656 * this query.
3657 * spp_address - This specifies which address is of interest.
3658 * spp_hbinterval - This contains the value of the heartbeat interval,
3659 * in milliseconds. If a value of zero
3660 * is present in this field then no changes are to
3661 * be made to this parameter.
3662 * spp_pathmaxrxt - This contains the maximum number of
3663 * retransmissions before this address shall be
3664 * considered unreachable. If a value of zero
3665 * is present in this field then no changes are to
3666 * be made to this parameter.
3667 * spp_pathmtu - When Path MTU discovery is disabled the value
3668 * specified here will be the "fixed" path mtu.
3669 * Note that if the spp_address field is empty
3670 * then all associations on this address will
3671 * have this fixed path mtu set upon them.
3673 * spp_sackdelay - When delayed sack is enabled, this value specifies
3674 * the number of milliseconds that sacks will be delayed
3675 * for. This value will apply to all addresses of an
3676 * association if the spp_address field is empty. Note
3677 * also, that if delayed sack is enabled and this
3678 * value is set to 0, no change is made to the last
3679 * recorded delayed sack timer value.
3681 * spp_flags - These flags are used to control various features
3682 * on an association. The flag field may contain
3683 * zero or more of the following options.
3685 * SPP_HB_ENABLE - Enable heartbeats on the
3686 * specified address. Note that if the address
3687 * field is empty all addresses for the association
3688 * have heartbeats enabled upon them.
3690 * SPP_HB_DISABLE - Disable heartbeats on the
3691 * speicifed address. Note that if the address
3692 * field is empty all addresses for the association
3693 * will have their heartbeats disabled. Note also
3694 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3695 * mutually exclusive, only one of these two should
3696 * be specified. Enabling both fields will have
3697 * undetermined results.
3699 * SPP_HB_DEMAND - Request a user initiated heartbeat
3700 * to be made immediately.
3702 * SPP_PMTUD_ENABLE - This field will enable PMTU
3703 * discovery upon the specified address. Note that
3704 * if the address feild is empty then all addresses
3705 * on the association are effected.
3707 * SPP_PMTUD_DISABLE - This field will disable PMTU
3708 * discovery upon the specified address. Note that
3709 * if the address feild is empty then all addresses
3710 * on the association are effected. Not also that
3711 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3712 * exclusive. Enabling both will have undetermined
3713 * results.
3715 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3716 * on delayed sack. The time specified in spp_sackdelay
3717 * is used to specify the sack delay for this address. Note
3718 * that if spp_address is empty then all addresses will
3719 * enable delayed sack and take on the sack delay
3720 * value specified in spp_sackdelay.
3721 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3722 * off delayed sack. If the spp_address field is blank then
3723 * delayed sack is disabled for the entire association. Note
3724 * also that this field is mutually exclusive to
3725 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3726 * results.
3728 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3729 char __user *optval, int __user *optlen)
3731 struct sctp_paddrparams params;
3732 struct sctp_transport *trans = NULL;
3733 struct sctp_association *asoc = NULL;
3734 struct sctp_sock *sp = sctp_sk(sk);
3736 if (len < sizeof(struct sctp_paddrparams))
3737 return -EINVAL;
3738 len = sizeof(struct sctp_paddrparams);
3739 if (copy_from_user(&params, optval, len))
3740 return -EFAULT;
3742 /* If an address other than INADDR_ANY is specified, and
3743 * no transport is found, then the request is invalid.
3745 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3746 trans = sctp_addr_id2transport(sk, &params.spp_address,
3747 params.spp_assoc_id);
3748 if (!trans) {
3749 SCTP_DEBUG_PRINTK("Failed no transport\n");
3750 return -EINVAL;
3754 /* Get association, if assoc_id != 0 and the socket is a one
3755 * to many style socket, and an association was not found, then
3756 * the id was invalid.
3758 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3759 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3760 SCTP_DEBUG_PRINTK("Failed no association\n");
3761 return -EINVAL;
3764 if (trans) {
3765 /* Fetch transport values. */
3766 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3767 params.spp_pathmtu = trans->pathmtu;
3768 params.spp_pathmaxrxt = trans->pathmaxrxt;
3769 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3771 /*draft-11 doesn't say what to return in spp_flags*/
3772 params.spp_flags = trans->param_flags;
3773 } else if (asoc) {
3774 /* Fetch association values. */
3775 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3776 params.spp_pathmtu = asoc->pathmtu;
3777 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3778 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3780 /*draft-11 doesn't say what to return in spp_flags*/
3781 params.spp_flags = asoc->param_flags;
3782 } else {
3783 /* Fetch socket values. */
3784 params.spp_hbinterval = sp->hbinterval;
3785 params.spp_pathmtu = sp->pathmtu;
3786 params.spp_sackdelay = sp->sackdelay;
3787 params.spp_pathmaxrxt = sp->pathmaxrxt;
3789 /*draft-11 doesn't say what to return in spp_flags*/
3790 params.spp_flags = sp->param_flags;
3793 if (copy_to_user(optval, &params, len))
3794 return -EFAULT;
3796 if (put_user(len, optlen))
3797 return -EFAULT;
3799 return 0;
3802 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3804 * This options will get or set the delayed ack timer. The time is set
3805 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3806 * endpoints default delayed ack timer value. If the assoc_id field is
3807 * non-zero, then the set or get effects the specified association.
3809 * struct sctp_assoc_value {
3810 * sctp_assoc_t assoc_id;
3811 * uint32_t assoc_value;
3812 * };
3814 * assoc_id - This parameter, indicates which association the
3815 * user is preforming an action upon. Note that if
3816 * this field's value is zero then the endpoints
3817 * default value is changed (effecting future
3818 * associations only).
3820 * assoc_value - This parameter contains the number of milliseconds
3821 * that the user is requesting the delayed ACK timer
3822 * be set to. Note that this value is defined in
3823 * the standard to be between 200 and 500 milliseconds.
3825 * Note: a value of zero will leave the value alone,
3826 * but disable SACK delay. A non-zero value will also
3827 * enable SACK delay.
3829 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3830 char __user *optval,
3831 int __user *optlen)
3833 struct sctp_assoc_value params;
3834 struct sctp_association *asoc = NULL;
3835 struct sctp_sock *sp = sctp_sk(sk);
3837 if (len < sizeof(struct sctp_assoc_value))
3838 return - EINVAL;
3840 len = sizeof(struct sctp_assoc_value);
3842 if (copy_from_user(&params, optval, len))
3843 return -EFAULT;
3845 /* Get association, if assoc_id != 0 and the socket is a one
3846 * to many style socket, and an association was not found, then
3847 * the id was invalid.
3849 asoc = sctp_id2assoc(sk, params.assoc_id);
3850 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3851 return -EINVAL;
3853 if (asoc) {
3854 /* Fetch association values. */
3855 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3856 params.assoc_value = jiffies_to_msecs(
3857 asoc->sackdelay);
3858 else
3859 params.assoc_value = 0;
3860 } else {
3861 /* Fetch socket values. */
3862 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3863 params.assoc_value = sp->sackdelay;
3864 else
3865 params.assoc_value = 0;
3868 if (copy_to_user(optval, &params, len))
3869 return -EFAULT;
3871 if (put_user(len, optlen))
3872 return -EFAULT;
3874 return 0;
3877 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3879 * Applications can specify protocol parameters for the default association
3880 * initialization. The option name argument to setsockopt() and getsockopt()
3881 * is SCTP_INITMSG.
3883 * Setting initialization parameters is effective only on an unconnected
3884 * socket (for UDP-style sockets only future associations are effected
3885 * by the change). With TCP-style sockets, this option is inherited by
3886 * sockets derived from a listener socket.
3888 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3890 if (len < sizeof(struct sctp_initmsg))
3891 return -EINVAL;
3892 len = sizeof(struct sctp_initmsg);
3893 if (put_user(len, optlen))
3894 return -EFAULT;
3895 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3896 return -EFAULT;
3897 return 0;
3900 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3901 char __user *optval,
3902 int __user *optlen)
3904 sctp_assoc_t id;
3905 struct sctp_association *asoc;
3906 struct list_head *pos;
3907 int cnt = 0;
3909 if (len < sizeof(sctp_assoc_t))
3910 return -EINVAL;
3912 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3913 return -EFAULT;
3915 /* For UDP-style sockets, id specifies the association to query. */
3916 asoc = sctp_id2assoc(sk, id);
3917 if (!asoc)
3918 return -EINVAL;
3920 list_for_each(pos, &asoc->peer.transport_addr_list) {
3921 cnt ++;
3924 return cnt;
3928 * Old API for getting list of peer addresses. Does not work for 32-bit
3929 * programs running on a 64-bit kernel
3931 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3932 char __user *optval,
3933 int __user *optlen)
3935 struct sctp_association *asoc;
3936 struct list_head *pos;
3937 int cnt = 0;
3938 struct sctp_getaddrs_old getaddrs;
3939 struct sctp_transport *from;
3940 void __user *to;
3941 union sctp_addr temp;
3942 struct sctp_sock *sp = sctp_sk(sk);
3943 int addrlen;
3945 if (len < sizeof(struct sctp_getaddrs_old))
3946 return -EINVAL;
3948 len = sizeof(struct sctp_getaddrs_old);
3950 if (copy_from_user(&getaddrs, optval, len))
3951 return -EFAULT;
3953 if (getaddrs.addr_num <= 0) return -EINVAL;
3955 /* For UDP-style sockets, id specifies the association to query. */
3956 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3957 if (!asoc)
3958 return -EINVAL;
3960 to = (void __user *)getaddrs.addrs;
3961 list_for_each(pos, &asoc->peer.transport_addr_list) {
3962 from = list_entry(pos, struct sctp_transport, transports);
3963 memcpy(&temp, &from->ipaddr, sizeof(temp));
3964 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3965 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3966 if (copy_to_user(to, &temp, addrlen))
3967 return -EFAULT;
3968 to += addrlen ;
3969 cnt ++;
3970 if (cnt >= getaddrs.addr_num) break;
3972 getaddrs.addr_num = cnt;
3973 if (put_user(len, optlen))
3974 return -EFAULT;
3975 if (copy_to_user(optval, &getaddrs, len))
3976 return -EFAULT;
3978 return 0;
3981 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3982 char __user *optval, int __user *optlen)
3984 struct sctp_association *asoc;
3985 struct list_head *pos;
3986 int cnt = 0;
3987 struct sctp_getaddrs getaddrs;
3988 struct sctp_transport *from;
3989 void __user *to;
3990 union sctp_addr temp;
3991 struct sctp_sock *sp = sctp_sk(sk);
3992 int addrlen;
3993 size_t space_left;
3994 int bytes_copied;
3996 if (len < sizeof(struct sctp_getaddrs))
3997 return -EINVAL;
3999 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4000 return -EFAULT;
4002 /* For UDP-style sockets, id specifies the association to query. */
4003 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4004 if (!asoc)
4005 return -EINVAL;
4007 to = optval + offsetof(struct sctp_getaddrs,addrs);
4008 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4010 list_for_each(pos, &asoc->peer.transport_addr_list) {
4011 from = list_entry(pos, struct sctp_transport, transports);
4012 memcpy(&temp, &from->ipaddr, sizeof(temp));
4013 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4014 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4015 if (space_left < addrlen)
4016 return -ENOMEM;
4017 if (copy_to_user(to, &temp, addrlen))
4018 return -EFAULT;
4019 to += addrlen;
4020 cnt++;
4021 space_left -= addrlen;
4024 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4025 return -EFAULT;
4026 bytes_copied = ((char __user *)to) - optval;
4027 if (put_user(bytes_copied, optlen))
4028 return -EFAULT;
4030 return 0;
4033 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4034 char __user *optval,
4035 int __user *optlen)
4037 sctp_assoc_t id;
4038 struct sctp_bind_addr *bp;
4039 struct sctp_association *asoc;
4040 struct sctp_sockaddr_entry *addr;
4041 int cnt = 0;
4043 if (len < sizeof(sctp_assoc_t))
4044 return -EINVAL;
4046 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4047 return -EFAULT;
4050 * For UDP-style sockets, id specifies the association to query.
4051 * If the id field is set to the value '0' then the locally bound
4052 * addresses are returned without regard to any particular
4053 * association.
4055 if (0 == id) {
4056 bp = &sctp_sk(sk)->ep->base.bind_addr;
4057 } else {
4058 asoc = sctp_id2assoc(sk, id);
4059 if (!asoc)
4060 return -EINVAL;
4061 bp = &asoc->base.bind_addr;
4064 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4065 * addresses from the global local address list.
4067 if (sctp_list_single_entry(&bp->address_list)) {
4068 addr = list_entry(bp->address_list.next,
4069 struct sctp_sockaddr_entry, list);
4070 if (sctp_is_any(&addr->a)) {
4071 rcu_read_lock();
4072 list_for_each_entry_rcu(addr,
4073 &sctp_local_addr_list, list) {
4074 if (!addr->valid)
4075 continue;
4077 if ((PF_INET == sk->sk_family) &&
4078 (AF_INET6 == addr->a.sa.sa_family))
4079 continue;
4081 cnt++;
4083 rcu_read_unlock();
4084 } else {
4085 cnt = 1;
4087 goto done;
4090 /* Protection on the bound address list is not needed,
4091 * since in the socket option context we hold the socket lock,
4092 * so there is no way that the bound address list can change.
4094 list_for_each_entry(addr, &bp->address_list, list) {
4095 cnt ++;
4097 done:
4098 return cnt;
4101 /* Helper function that copies local addresses to user and returns the number
4102 * of addresses copied.
4104 static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4105 int max_addrs, void *to,
4106 int *bytes_copied)
4108 struct sctp_sockaddr_entry *addr;
4109 union sctp_addr temp;
4110 int cnt = 0;
4111 int addrlen;
4113 rcu_read_lock();
4114 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4115 if (!addr->valid)
4116 continue;
4118 if ((PF_INET == sk->sk_family) &&
4119 (AF_INET6 == addr->a.sa.sa_family))
4120 continue;
4121 memcpy(&temp, &addr->a, sizeof(temp));
4122 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4123 &temp);
4124 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4125 memcpy(to, &temp, addrlen);
4127 to += addrlen;
4128 *bytes_copied += addrlen;
4129 cnt ++;
4130 if (cnt >= max_addrs) break;
4132 rcu_read_unlock();
4134 return cnt;
4137 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4138 size_t space_left, int *bytes_copied)
4140 struct sctp_sockaddr_entry *addr;
4141 union sctp_addr temp;
4142 int cnt = 0;
4143 int addrlen;
4145 rcu_read_lock();
4146 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4147 if (!addr->valid)
4148 continue;
4150 if ((PF_INET == sk->sk_family) &&
4151 (AF_INET6 == addr->a.sa.sa_family))
4152 continue;
4153 memcpy(&temp, &addr->a, sizeof(temp));
4154 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4155 &temp);
4156 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4157 if (space_left < addrlen) {
4158 cnt = -ENOMEM;
4159 break;
4161 memcpy(to, &temp, addrlen);
4163 to += addrlen;
4164 cnt ++;
4165 space_left -= addrlen;
4166 *bytes_copied += addrlen;
4168 rcu_read_unlock();
4170 return cnt;
4173 /* Old API for getting list of local addresses. Does not work for 32-bit
4174 * programs running on a 64-bit kernel
4176 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4177 char __user *optval, int __user *optlen)
4179 struct sctp_bind_addr *bp;
4180 struct sctp_association *asoc;
4181 int cnt = 0;
4182 struct sctp_getaddrs_old getaddrs;
4183 struct sctp_sockaddr_entry *addr;
4184 void __user *to;
4185 union sctp_addr temp;
4186 struct sctp_sock *sp = sctp_sk(sk);
4187 int addrlen;
4188 int err = 0;
4189 void *addrs;
4190 void *buf;
4191 int bytes_copied = 0;
4193 if (len < sizeof(struct sctp_getaddrs_old))
4194 return -EINVAL;
4196 len = sizeof(struct sctp_getaddrs_old);
4197 if (copy_from_user(&getaddrs, optval, len))
4198 return -EFAULT;
4200 if (getaddrs.addr_num <= 0) return -EINVAL;
4202 * For UDP-style sockets, id specifies the association to query.
4203 * If the id field is set to the value '0' then the locally bound
4204 * addresses are returned without regard to any particular
4205 * association.
4207 if (0 == getaddrs.assoc_id) {
4208 bp = &sctp_sk(sk)->ep->base.bind_addr;
4209 } else {
4210 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4211 if (!asoc)
4212 return -EINVAL;
4213 bp = &asoc->base.bind_addr;
4216 to = getaddrs.addrs;
4218 /* Allocate space for a local instance of packed array to hold all
4219 * the data. We store addresses here first and then put write them
4220 * to the user in one shot.
4222 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4223 GFP_KERNEL);
4224 if (!addrs)
4225 return -ENOMEM;
4227 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4228 * addresses from the global local address list.
4230 if (sctp_list_single_entry(&bp->address_list)) {
4231 addr = list_entry(bp->address_list.next,
4232 struct sctp_sockaddr_entry, list);
4233 if (sctp_is_any(&addr->a)) {
4234 cnt = sctp_copy_laddrs_old(sk, bp->port,
4235 getaddrs.addr_num,
4236 addrs, &bytes_copied);
4237 goto copy_getaddrs;
4241 buf = addrs;
4242 /* Protection on the bound address list is not needed since
4243 * in the socket option context we hold a socket lock and
4244 * thus the bound address list can't change.
4246 list_for_each_entry(addr, &bp->address_list, list) {
4247 memcpy(&temp, &addr->a, sizeof(temp));
4248 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4249 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4250 memcpy(buf, &temp, addrlen);
4251 buf += addrlen;
4252 bytes_copied += addrlen;
4253 cnt ++;
4254 if (cnt >= getaddrs.addr_num) break;
4257 copy_getaddrs:
4258 /* copy the entire address list into the user provided space */
4259 if (copy_to_user(to, addrs, bytes_copied)) {
4260 err = -EFAULT;
4261 goto error;
4264 /* copy the leading structure back to user */
4265 getaddrs.addr_num = cnt;
4266 if (copy_to_user(optval, &getaddrs, len))
4267 err = -EFAULT;
4269 error:
4270 kfree(addrs);
4271 return err;
4274 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4275 char __user *optval, int __user *optlen)
4277 struct sctp_bind_addr *bp;
4278 struct sctp_association *asoc;
4279 int cnt = 0;
4280 struct sctp_getaddrs getaddrs;
4281 struct sctp_sockaddr_entry *addr;
4282 void __user *to;
4283 union sctp_addr temp;
4284 struct sctp_sock *sp = sctp_sk(sk);
4285 int addrlen;
4286 int err = 0;
4287 size_t space_left;
4288 int bytes_copied = 0;
4289 void *addrs;
4290 void *buf;
4292 if (len < sizeof(struct sctp_getaddrs))
4293 return -EINVAL;
4295 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4296 return -EFAULT;
4299 * For UDP-style sockets, id specifies the association to query.
4300 * If the id field is set to the value '0' then the locally bound
4301 * addresses are returned without regard to any particular
4302 * association.
4304 if (0 == getaddrs.assoc_id) {
4305 bp = &sctp_sk(sk)->ep->base.bind_addr;
4306 } else {
4307 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4308 if (!asoc)
4309 return -EINVAL;
4310 bp = &asoc->base.bind_addr;
4313 to = optval + offsetof(struct sctp_getaddrs,addrs);
4314 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4316 addrs = kmalloc(space_left, GFP_KERNEL);
4317 if (!addrs)
4318 return -ENOMEM;
4320 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4321 * addresses from the global local address list.
4323 if (sctp_list_single_entry(&bp->address_list)) {
4324 addr = list_entry(bp->address_list.next,
4325 struct sctp_sockaddr_entry, list);
4326 if (sctp_is_any(&addr->a)) {
4327 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4328 space_left, &bytes_copied);
4329 if (cnt < 0) {
4330 err = cnt;
4331 goto out;
4333 goto copy_getaddrs;
4337 buf = addrs;
4338 /* Protection on the bound address list is not needed since
4339 * in the socket option context we hold a socket lock and
4340 * thus the bound address list can't change.
4342 list_for_each_entry(addr, &bp->address_list, list) {
4343 memcpy(&temp, &addr->a, sizeof(temp));
4344 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4345 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4346 if (space_left < addrlen) {
4347 err = -ENOMEM; /*fixme: right error?*/
4348 goto out;
4350 memcpy(buf, &temp, addrlen);
4351 buf += addrlen;
4352 bytes_copied += addrlen;
4353 cnt ++;
4354 space_left -= addrlen;
4357 copy_getaddrs:
4358 if (copy_to_user(to, addrs, bytes_copied)) {
4359 err = -EFAULT;
4360 goto out;
4362 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4363 err = -EFAULT;
4364 goto out;
4366 if (put_user(bytes_copied, optlen))
4367 err = -EFAULT;
4368 out:
4369 kfree(addrs);
4370 return err;
4373 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4375 * Requests that the local SCTP stack use the enclosed peer address as
4376 * the association primary. The enclosed address must be one of the
4377 * association peer's addresses.
4379 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4380 char __user *optval, int __user *optlen)
4382 struct sctp_prim prim;
4383 struct sctp_association *asoc;
4384 struct sctp_sock *sp = sctp_sk(sk);
4386 if (len < sizeof(struct sctp_prim))
4387 return -EINVAL;
4389 len = sizeof(struct sctp_prim);
4391 if (copy_from_user(&prim, optval, len))
4392 return -EFAULT;
4394 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4395 if (!asoc)
4396 return -EINVAL;
4398 if (!asoc->peer.primary_path)
4399 return -ENOTCONN;
4401 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4402 asoc->peer.primary_path->af_specific->sockaddr_len);
4404 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4405 (union sctp_addr *)&prim.ssp_addr);
4407 if (put_user(len, optlen))
4408 return -EFAULT;
4409 if (copy_to_user(optval, &prim, len))
4410 return -EFAULT;
4412 return 0;
4416 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4418 * Requests that the local endpoint set the specified Adaptation Layer
4419 * Indication parameter for all future INIT and INIT-ACK exchanges.
4421 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4422 char __user *optval, int __user *optlen)
4424 struct sctp_setadaptation adaptation;
4426 if (len < sizeof(struct sctp_setadaptation))
4427 return -EINVAL;
4429 len = sizeof(struct sctp_setadaptation);
4431 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4433 if (put_user(len, optlen))
4434 return -EFAULT;
4435 if (copy_to_user(optval, &adaptation, len))
4436 return -EFAULT;
4438 return 0;
4443 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4445 * Applications that wish to use the sendto() system call may wish to
4446 * specify a default set of parameters that would normally be supplied
4447 * through the inclusion of ancillary data. This socket option allows
4448 * such an application to set the default sctp_sndrcvinfo structure.
4451 * The application that wishes to use this socket option simply passes
4452 * in to this call the sctp_sndrcvinfo structure defined in Section
4453 * 5.2.2) The input parameters accepted by this call include
4454 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4455 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4456 * to this call if the caller is using the UDP model.
4458 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4460 static int sctp_getsockopt_default_send_param(struct sock *sk,
4461 int len, char __user *optval,
4462 int __user *optlen)
4464 struct sctp_sndrcvinfo info;
4465 struct sctp_association *asoc;
4466 struct sctp_sock *sp = sctp_sk(sk);
4468 if (len < sizeof(struct sctp_sndrcvinfo))
4469 return -EINVAL;
4471 len = sizeof(struct sctp_sndrcvinfo);
4473 if (copy_from_user(&info, optval, len))
4474 return -EFAULT;
4476 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4477 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4478 return -EINVAL;
4480 if (asoc) {
4481 info.sinfo_stream = asoc->default_stream;
4482 info.sinfo_flags = asoc->default_flags;
4483 info.sinfo_ppid = asoc->default_ppid;
4484 info.sinfo_context = asoc->default_context;
4485 info.sinfo_timetolive = asoc->default_timetolive;
4486 } else {
4487 info.sinfo_stream = sp->default_stream;
4488 info.sinfo_flags = sp->default_flags;
4489 info.sinfo_ppid = sp->default_ppid;
4490 info.sinfo_context = sp->default_context;
4491 info.sinfo_timetolive = sp->default_timetolive;
4494 if (put_user(len, optlen))
4495 return -EFAULT;
4496 if (copy_to_user(optval, &info, len))
4497 return -EFAULT;
4499 return 0;
4504 * 7.1.5 SCTP_NODELAY
4506 * Turn on/off any Nagle-like algorithm. This means that packets are
4507 * generally sent as soon as possible and no unnecessary delays are
4508 * introduced, at the cost of more packets in the network. Expects an
4509 * integer boolean flag.
4512 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4513 char __user *optval, int __user *optlen)
4515 int val;
4517 if (len < sizeof(int))
4518 return -EINVAL;
4520 len = sizeof(int);
4521 val = (sctp_sk(sk)->nodelay == 1);
4522 if (put_user(len, optlen))
4523 return -EFAULT;
4524 if (copy_to_user(optval, &val, len))
4525 return -EFAULT;
4526 return 0;
4531 * 7.1.1 SCTP_RTOINFO
4533 * The protocol parameters used to initialize and bound retransmission
4534 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4535 * and modify these parameters.
4536 * All parameters are time values, in milliseconds. A value of 0, when
4537 * modifying the parameters, indicates that the current value should not
4538 * be changed.
4541 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4542 char __user *optval,
4543 int __user *optlen) {
4544 struct sctp_rtoinfo rtoinfo;
4545 struct sctp_association *asoc;
4547 if (len < sizeof (struct sctp_rtoinfo))
4548 return -EINVAL;
4550 len = sizeof(struct sctp_rtoinfo);
4552 if (copy_from_user(&rtoinfo, optval, len))
4553 return -EFAULT;
4555 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4557 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4558 return -EINVAL;
4560 /* Values corresponding to the specific association. */
4561 if (asoc) {
4562 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4563 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4564 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4565 } else {
4566 /* Values corresponding to the endpoint. */
4567 struct sctp_sock *sp = sctp_sk(sk);
4569 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4570 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4571 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4574 if (put_user(len, optlen))
4575 return -EFAULT;
4577 if (copy_to_user(optval, &rtoinfo, len))
4578 return -EFAULT;
4580 return 0;
4585 * 7.1.2 SCTP_ASSOCINFO
4587 * This option is used to tune the maximum retransmission attempts
4588 * of the association.
4589 * Returns an error if the new association retransmission value is
4590 * greater than the sum of the retransmission value of the peer.
4591 * See [SCTP] for more information.
4594 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4595 char __user *optval,
4596 int __user *optlen)
4599 struct sctp_assocparams assocparams;
4600 struct sctp_association *asoc;
4601 struct list_head *pos;
4602 int cnt = 0;
4604 if (len < sizeof (struct sctp_assocparams))
4605 return -EINVAL;
4607 len = sizeof(struct sctp_assocparams);
4609 if (copy_from_user(&assocparams, optval, len))
4610 return -EFAULT;
4612 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4614 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4615 return -EINVAL;
4617 /* Values correspoinding to the specific association */
4618 if (asoc) {
4619 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4620 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4621 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4622 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4623 * 1000) +
4624 (asoc->cookie_life.tv_usec
4625 / 1000);
4627 list_for_each(pos, &asoc->peer.transport_addr_list) {
4628 cnt ++;
4631 assocparams.sasoc_number_peer_destinations = cnt;
4632 } else {
4633 /* Values corresponding to the endpoint */
4634 struct sctp_sock *sp = sctp_sk(sk);
4636 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4637 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4638 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4639 assocparams.sasoc_cookie_life =
4640 sp->assocparams.sasoc_cookie_life;
4641 assocparams.sasoc_number_peer_destinations =
4642 sp->assocparams.
4643 sasoc_number_peer_destinations;
4646 if (put_user(len, optlen))
4647 return -EFAULT;
4649 if (copy_to_user(optval, &assocparams, len))
4650 return -EFAULT;
4652 return 0;
4656 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4658 * This socket option is a boolean flag which turns on or off mapped V4
4659 * addresses. If this option is turned on and the socket is type
4660 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4661 * If this option is turned off, then no mapping will be done of V4
4662 * addresses and a user will receive both PF_INET6 and PF_INET type
4663 * addresses on the socket.
4665 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4666 char __user *optval, int __user *optlen)
4668 int val;
4669 struct sctp_sock *sp = sctp_sk(sk);
4671 if (len < sizeof(int))
4672 return -EINVAL;
4674 len = sizeof(int);
4675 val = sp->v4mapped;
4676 if (put_user(len, optlen))
4677 return -EFAULT;
4678 if (copy_to_user(optval, &val, len))
4679 return -EFAULT;
4681 return 0;
4685 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4686 * (chapter and verse is quoted at sctp_setsockopt_context())
4688 static int sctp_getsockopt_context(struct sock *sk, int len,
4689 char __user *optval, int __user *optlen)
4691 struct sctp_assoc_value params;
4692 struct sctp_sock *sp;
4693 struct sctp_association *asoc;
4695 if (len < sizeof(struct sctp_assoc_value))
4696 return -EINVAL;
4698 len = sizeof(struct sctp_assoc_value);
4700 if (copy_from_user(&params, optval, len))
4701 return -EFAULT;
4703 sp = sctp_sk(sk);
4705 if (params.assoc_id != 0) {
4706 asoc = sctp_id2assoc(sk, params.assoc_id);
4707 if (!asoc)
4708 return -EINVAL;
4709 params.assoc_value = asoc->default_rcv_context;
4710 } else {
4711 params.assoc_value = sp->default_rcv_context;
4714 if (put_user(len, optlen))
4715 return -EFAULT;
4716 if (copy_to_user(optval, &params, len))
4717 return -EFAULT;
4719 return 0;
4723 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4725 * This socket option specifies the maximum size to put in any outgoing
4726 * SCTP chunk. If a message is larger than this size it will be
4727 * fragmented by SCTP into the specified size. Note that the underlying
4728 * SCTP implementation may fragment into smaller sized chunks when the
4729 * PMTU of the underlying association is smaller than the value set by
4730 * the user.
4732 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4733 char __user *optval, int __user *optlen)
4735 int val;
4737 if (len < sizeof(int))
4738 return -EINVAL;
4740 len = sizeof(int);
4742 val = sctp_sk(sk)->user_frag;
4743 if (put_user(len, optlen))
4744 return -EFAULT;
4745 if (copy_to_user(optval, &val, len))
4746 return -EFAULT;
4748 return 0;
4752 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4753 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4755 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4756 char __user *optval, int __user *optlen)
4758 int val;
4760 if (len < sizeof(int))
4761 return -EINVAL;
4763 len = sizeof(int);
4765 val = sctp_sk(sk)->frag_interleave;
4766 if (put_user(len, optlen))
4767 return -EFAULT;
4768 if (copy_to_user(optval, &val, len))
4769 return -EFAULT;
4771 return 0;
4775 * 7.1.25. Set or Get the sctp partial delivery point
4776 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4778 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4779 char __user *optval,
4780 int __user *optlen)
4782 u32 val;
4784 if (len < sizeof(u32))
4785 return -EINVAL;
4787 len = sizeof(u32);
4789 val = sctp_sk(sk)->pd_point;
4790 if (put_user(len, optlen))
4791 return -EFAULT;
4792 if (copy_to_user(optval, &val, len))
4793 return -EFAULT;
4795 return -ENOTSUPP;
4799 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4800 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4802 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4803 char __user *optval,
4804 int __user *optlen)
4806 int val;
4808 if (len < sizeof(int))
4809 return -EINVAL;
4811 len = sizeof(int);
4813 val = sctp_sk(sk)->max_burst;
4814 if (put_user(len, optlen))
4815 return -EFAULT;
4816 if (copy_to_user(optval, &val, len))
4817 return -EFAULT;
4819 return -ENOTSUPP;
4822 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4823 char __user *optval, int __user *optlen)
4825 int retval = 0;
4826 int len;
4828 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4829 sk, optname);
4831 /* I can hardly begin to describe how wrong this is. This is
4832 * so broken as to be worse than useless. The API draft
4833 * REALLY is NOT helpful here... I am not convinced that the
4834 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4835 * are at all well-founded.
4837 if (level != SOL_SCTP) {
4838 struct sctp_af *af = sctp_sk(sk)->pf->af;
4840 retval = af->getsockopt(sk, level, optname, optval, optlen);
4841 return retval;
4844 if (get_user(len, optlen))
4845 return -EFAULT;
4847 sctp_lock_sock(sk);
4849 switch (optname) {
4850 case SCTP_STATUS:
4851 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4852 break;
4853 case SCTP_DISABLE_FRAGMENTS:
4854 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4855 optlen);
4856 break;
4857 case SCTP_EVENTS:
4858 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4859 break;
4860 case SCTP_AUTOCLOSE:
4861 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4862 break;
4863 case SCTP_SOCKOPT_PEELOFF:
4864 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4865 break;
4866 case SCTP_PEER_ADDR_PARAMS:
4867 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4868 optlen);
4869 break;
4870 case SCTP_DELAYED_ACK_TIME:
4871 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4872 optlen);
4873 break;
4874 case SCTP_INITMSG:
4875 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4876 break;
4877 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4878 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4879 optlen);
4880 break;
4881 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4882 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4883 optlen);
4884 break;
4885 case SCTP_GET_PEER_ADDRS_OLD:
4886 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4887 optlen);
4888 break;
4889 case SCTP_GET_LOCAL_ADDRS_OLD:
4890 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4891 optlen);
4892 break;
4893 case SCTP_GET_PEER_ADDRS:
4894 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4895 optlen);
4896 break;
4897 case SCTP_GET_LOCAL_ADDRS:
4898 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4899 optlen);
4900 break;
4901 case SCTP_DEFAULT_SEND_PARAM:
4902 retval = sctp_getsockopt_default_send_param(sk, len,
4903 optval, optlen);
4904 break;
4905 case SCTP_PRIMARY_ADDR:
4906 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4907 break;
4908 case SCTP_NODELAY:
4909 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4910 break;
4911 case SCTP_RTOINFO:
4912 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4913 break;
4914 case SCTP_ASSOCINFO:
4915 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4916 break;
4917 case SCTP_I_WANT_MAPPED_V4_ADDR:
4918 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4919 break;
4920 case SCTP_MAXSEG:
4921 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4922 break;
4923 case SCTP_GET_PEER_ADDR_INFO:
4924 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4925 optlen);
4926 break;
4927 case SCTP_ADAPTATION_LAYER:
4928 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
4929 optlen);
4930 break;
4931 case SCTP_CONTEXT:
4932 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4933 break;
4934 case SCTP_FRAGMENT_INTERLEAVE:
4935 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4936 optlen);
4937 break;
4938 case SCTP_PARTIAL_DELIVERY_POINT:
4939 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4940 optlen);
4941 break;
4942 case SCTP_MAX_BURST:
4943 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4944 break;
4945 default:
4946 retval = -ENOPROTOOPT;
4947 break;
4950 sctp_release_sock(sk);
4951 return retval;
4954 static void sctp_hash(struct sock *sk)
4956 /* STUB */
4959 static void sctp_unhash(struct sock *sk)
4961 /* STUB */
4964 /* Check if port is acceptable. Possibly find first available port.
4966 * The port hash table (contained in the 'global' SCTP protocol storage
4967 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4968 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4969 * list (the list number is the port number hashed out, so as you
4970 * would expect from a hash function, all the ports in a given list have
4971 * such a number that hashes out to the same list number; you were
4972 * expecting that, right?); so each list has a set of ports, with a
4973 * link to the socket (struct sock) that uses it, the port number and
4974 * a fastreuse flag (FIXME: NPI ipg).
4976 static struct sctp_bind_bucket *sctp_bucket_create(
4977 struct sctp_bind_hashbucket *head, unsigned short snum);
4979 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4981 struct sctp_bind_hashbucket *head; /* hash list */
4982 struct sctp_bind_bucket *pp; /* hash list port iterator */
4983 unsigned short snum;
4984 int ret;
4986 snum = ntohs(addr->v4.sin_port);
4988 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4989 sctp_local_bh_disable();
4991 if (snum == 0) {
4992 /* Search for an available port.
4994 * 'sctp_port_rover' was the last port assigned, so
4995 * we start to search from 'sctp_port_rover +
4996 * 1'. What we do is first check if port 'rover' is
4997 * already in the hash table; if not, we use that; if
4998 * it is, we try next.
5000 int low = sysctl_local_port_range[0];
5001 int high = sysctl_local_port_range[1];
5002 int remaining = (high - low) + 1;
5003 int rover;
5004 int index;
5006 sctp_spin_lock(&sctp_port_alloc_lock);
5007 rover = sctp_port_rover;
5008 do {
5009 rover++;
5010 if ((rover < low) || (rover > high))
5011 rover = low;
5012 index = sctp_phashfn(rover);
5013 head = &sctp_port_hashtable[index];
5014 sctp_spin_lock(&head->lock);
5015 for (pp = head->chain; pp; pp = pp->next)
5016 if (pp->port == rover)
5017 goto next;
5018 break;
5019 next:
5020 sctp_spin_unlock(&head->lock);
5021 } while (--remaining > 0);
5022 sctp_port_rover = rover;
5023 sctp_spin_unlock(&sctp_port_alloc_lock);
5025 /* Exhausted local port range during search? */
5026 ret = 1;
5027 if (remaining <= 0)
5028 goto fail;
5030 /* OK, here is the one we will use. HEAD (the port
5031 * hash table list entry) is non-NULL and we hold it's
5032 * mutex.
5034 snum = rover;
5035 } else {
5036 /* We are given an specific port number; we verify
5037 * that it is not being used. If it is used, we will
5038 * exahust the search in the hash list corresponding
5039 * to the port number (snum) - we detect that with the
5040 * port iterator, pp being NULL.
5042 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5043 sctp_spin_lock(&head->lock);
5044 for (pp = head->chain; pp; pp = pp->next) {
5045 if (pp->port == snum)
5046 goto pp_found;
5049 pp = NULL;
5050 goto pp_not_found;
5051 pp_found:
5052 if (!hlist_empty(&pp->owner)) {
5053 /* We had a port hash table hit - there is an
5054 * available port (pp != NULL) and it is being
5055 * used by other socket (pp->owner not empty); that other
5056 * socket is going to be sk2.
5058 int reuse = sk->sk_reuse;
5059 struct sock *sk2;
5060 struct hlist_node *node;
5062 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5063 if (pp->fastreuse && sk->sk_reuse &&
5064 sk->sk_state != SCTP_SS_LISTENING)
5065 goto success;
5067 /* Run through the list of sockets bound to the port
5068 * (pp->port) [via the pointers bind_next and
5069 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5070 * we get the endpoint they describe and run through
5071 * the endpoint's list of IP (v4 or v6) addresses,
5072 * comparing each of the addresses with the address of
5073 * the socket sk. If we find a match, then that means
5074 * that this port/socket (sk) combination are already
5075 * in an endpoint.
5077 sk_for_each_bound(sk2, node, &pp->owner) {
5078 struct sctp_endpoint *ep2;
5079 ep2 = sctp_sk(sk2)->ep;
5081 if (reuse && sk2->sk_reuse &&
5082 sk2->sk_state != SCTP_SS_LISTENING)
5083 continue;
5085 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
5086 sctp_sk(sk))) {
5087 ret = (long)sk2;
5088 goto fail_unlock;
5091 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5093 pp_not_found:
5094 /* If there was a hash table miss, create a new port. */
5095 ret = 1;
5096 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5097 goto fail_unlock;
5099 /* In either case (hit or miss), make sure fastreuse is 1 only
5100 * if sk->sk_reuse is too (that is, if the caller requested
5101 * SO_REUSEADDR on this socket -sk-).
5103 if (hlist_empty(&pp->owner)) {
5104 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5105 pp->fastreuse = 1;
5106 else
5107 pp->fastreuse = 0;
5108 } else if (pp->fastreuse &&
5109 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5110 pp->fastreuse = 0;
5112 /* We are set, so fill up all the data in the hash table
5113 * entry, tie the socket list information with the rest of the
5114 * sockets FIXME: Blurry, NPI (ipg).
5116 success:
5117 if (!sctp_sk(sk)->bind_hash) {
5118 inet_sk(sk)->num = snum;
5119 sk_add_bind_node(sk, &pp->owner);
5120 sctp_sk(sk)->bind_hash = pp;
5122 ret = 0;
5124 fail_unlock:
5125 sctp_spin_unlock(&head->lock);
5127 fail:
5128 sctp_local_bh_enable();
5129 return ret;
5132 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5133 * port is requested.
5135 static int sctp_get_port(struct sock *sk, unsigned short snum)
5137 long ret;
5138 union sctp_addr addr;
5139 struct sctp_af *af = sctp_sk(sk)->pf->af;
5141 /* Set up a dummy address struct from the sk. */
5142 af->from_sk(&addr, sk);
5143 addr.v4.sin_port = htons(snum);
5145 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5146 ret = sctp_get_port_local(sk, &addr);
5148 return (ret ? 1 : 0);
5152 * 3.1.3 listen() - UDP Style Syntax
5154 * By default, new associations are not accepted for UDP style sockets.
5155 * An application uses listen() to mark a socket as being able to
5156 * accept new associations.
5158 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5160 struct sctp_sock *sp = sctp_sk(sk);
5161 struct sctp_endpoint *ep = sp->ep;
5163 /* Only UDP style sockets that are not peeled off are allowed to
5164 * listen().
5166 if (!sctp_style(sk, UDP))
5167 return -EINVAL;
5169 /* If backlog is zero, disable listening. */
5170 if (!backlog) {
5171 if (sctp_sstate(sk, CLOSED))
5172 return 0;
5174 sctp_unhash_endpoint(ep);
5175 sk->sk_state = SCTP_SS_CLOSED;
5176 return 0;
5179 /* Return if we are already listening. */
5180 if (sctp_sstate(sk, LISTENING))
5181 return 0;
5184 * If a bind() or sctp_bindx() is not called prior to a listen()
5185 * call that allows new associations to be accepted, the system
5186 * picks an ephemeral port and will choose an address set equivalent
5187 * to binding with a wildcard address.
5189 * This is not currently spelled out in the SCTP sockets
5190 * extensions draft, but follows the practice as seen in TCP
5191 * sockets.
5193 * Additionally, turn off fastreuse flag since we are not listening
5195 sk->sk_state = SCTP_SS_LISTENING;
5196 if (!ep->base.bind_addr.port) {
5197 if (sctp_autobind(sk))
5198 return -EAGAIN;
5199 } else
5200 sctp_sk(sk)->bind_hash->fastreuse = 0;
5202 sctp_hash_endpoint(ep);
5203 return 0;
5207 * 4.1.3 listen() - TCP Style Syntax
5209 * Applications uses listen() to ready the SCTP endpoint for accepting
5210 * inbound associations.
5212 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5214 struct sctp_sock *sp = sctp_sk(sk);
5215 struct sctp_endpoint *ep = sp->ep;
5217 /* If backlog is zero, disable listening. */
5218 if (!backlog) {
5219 if (sctp_sstate(sk, CLOSED))
5220 return 0;
5222 sctp_unhash_endpoint(ep);
5223 sk->sk_state = SCTP_SS_CLOSED;
5224 return 0;
5227 if (sctp_sstate(sk, LISTENING))
5228 return 0;
5231 * If a bind() or sctp_bindx() is not called prior to a listen()
5232 * call that allows new associations to be accepted, the system
5233 * picks an ephemeral port and will choose an address set equivalent
5234 * to binding with a wildcard address.
5236 * This is not currently spelled out in the SCTP sockets
5237 * extensions draft, but follows the practice as seen in TCP
5238 * sockets.
5240 sk->sk_state = SCTP_SS_LISTENING;
5241 if (!ep->base.bind_addr.port) {
5242 if (sctp_autobind(sk))
5243 return -EAGAIN;
5244 } else
5245 sctp_sk(sk)->bind_hash->fastreuse = 0;
5247 sk->sk_max_ack_backlog = backlog;
5248 sctp_hash_endpoint(ep);
5249 return 0;
5253 * Move a socket to LISTENING state.
5255 int sctp_inet_listen(struct socket *sock, int backlog)
5257 struct sock *sk = sock->sk;
5258 struct crypto_hash *tfm = NULL;
5259 int err = -EINVAL;
5261 if (unlikely(backlog < 0))
5262 goto out;
5264 sctp_lock_sock(sk);
5266 if (sock->state != SS_UNCONNECTED)
5267 goto out;
5269 /* Allocate HMAC for generating cookie. */
5270 if (sctp_hmac_alg) {
5271 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5272 if (IS_ERR(tfm)) {
5273 if (net_ratelimit()) {
5274 printk(KERN_INFO
5275 "SCTP: failed to load transform for %s: %ld\n",
5276 sctp_hmac_alg, PTR_ERR(tfm));
5278 err = -ENOSYS;
5279 goto out;
5283 switch (sock->type) {
5284 case SOCK_SEQPACKET:
5285 err = sctp_seqpacket_listen(sk, backlog);
5286 break;
5287 case SOCK_STREAM:
5288 err = sctp_stream_listen(sk, backlog);
5289 break;
5290 default:
5291 break;
5294 if (err)
5295 goto cleanup;
5297 /* Store away the transform reference. */
5298 sctp_sk(sk)->hmac = tfm;
5299 out:
5300 sctp_release_sock(sk);
5301 return err;
5302 cleanup:
5303 crypto_free_hash(tfm);
5304 goto out;
5308 * This function is done by modeling the current datagram_poll() and the
5309 * tcp_poll(). Note that, based on these implementations, we don't
5310 * lock the socket in this function, even though it seems that,
5311 * ideally, locking or some other mechanisms can be used to ensure
5312 * the integrity of the counters (sndbuf and wmem_alloc) used
5313 * in this place. We assume that we don't need locks either until proven
5314 * otherwise.
5316 * Another thing to note is that we include the Async I/O support
5317 * here, again, by modeling the current TCP/UDP code. We don't have
5318 * a good way to test with it yet.
5320 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5322 struct sock *sk = sock->sk;
5323 struct sctp_sock *sp = sctp_sk(sk);
5324 unsigned int mask;
5326 poll_wait(file, sk->sk_sleep, wait);
5328 /* A TCP-style listening socket becomes readable when the accept queue
5329 * is not empty.
5331 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5332 return (!list_empty(&sp->ep->asocs)) ?
5333 (POLLIN | POLLRDNORM) : 0;
5335 mask = 0;
5337 /* Is there any exceptional events? */
5338 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5339 mask |= POLLERR;
5340 if (sk->sk_shutdown & RCV_SHUTDOWN)
5341 mask |= POLLRDHUP;
5342 if (sk->sk_shutdown == SHUTDOWN_MASK)
5343 mask |= POLLHUP;
5345 /* Is it readable? Reconsider this code with TCP-style support. */
5346 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5347 (sk->sk_shutdown & RCV_SHUTDOWN))
5348 mask |= POLLIN | POLLRDNORM;
5350 /* The association is either gone or not ready. */
5351 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5352 return mask;
5354 /* Is it writable? */
5355 if (sctp_writeable(sk)) {
5356 mask |= POLLOUT | POLLWRNORM;
5357 } else {
5358 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5360 * Since the socket is not locked, the buffer
5361 * might be made available after the writeable check and
5362 * before the bit is set. This could cause a lost I/O
5363 * signal. tcp_poll() has a race breaker for this race
5364 * condition. Based on their implementation, we put
5365 * in the following code to cover it as well.
5367 if (sctp_writeable(sk))
5368 mask |= POLLOUT | POLLWRNORM;
5370 return mask;
5373 /********************************************************************
5374 * 2nd Level Abstractions
5375 ********************************************************************/
5377 static struct sctp_bind_bucket *sctp_bucket_create(
5378 struct sctp_bind_hashbucket *head, unsigned short snum)
5380 struct sctp_bind_bucket *pp;
5382 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5383 SCTP_DBG_OBJCNT_INC(bind_bucket);
5384 if (pp) {
5385 pp->port = snum;
5386 pp->fastreuse = 0;
5387 INIT_HLIST_HEAD(&pp->owner);
5388 if ((pp->next = head->chain) != NULL)
5389 pp->next->pprev = &pp->next;
5390 head->chain = pp;
5391 pp->pprev = &head->chain;
5393 return pp;
5396 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5397 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5399 if (pp && hlist_empty(&pp->owner)) {
5400 if (pp->next)
5401 pp->next->pprev = pp->pprev;
5402 *(pp->pprev) = pp->next;
5403 kmem_cache_free(sctp_bucket_cachep, pp);
5404 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5408 /* Release this socket's reference to a local port. */
5409 static inline void __sctp_put_port(struct sock *sk)
5411 struct sctp_bind_hashbucket *head =
5412 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5413 struct sctp_bind_bucket *pp;
5415 sctp_spin_lock(&head->lock);
5416 pp = sctp_sk(sk)->bind_hash;
5417 __sk_del_bind_node(sk);
5418 sctp_sk(sk)->bind_hash = NULL;
5419 inet_sk(sk)->num = 0;
5420 sctp_bucket_destroy(pp);
5421 sctp_spin_unlock(&head->lock);
5424 void sctp_put_port(struct sock *sk)
5426 sctp_local_bh_disable();
5427 __sctp_put_port(sk);
5428 sctp_local_bh_enable();
5432 * The system picks an ephemeral port and choose an address set equivalent
5433 * to binding with a wildcard address.
5434 * One of those addresses will be the primary address for the association.
5435 * This automatically enables the multihoming capability of SCTP.
5437 static int sctp_autobind(struct sock *sk)
5439 union sctp_addr autoaddr;
5440 struct sctp_af *af;
5441 __be16 port;
5443 /* Initialize a local sockaddr structure to INADDR_ANY. */
5444 af = sctp_sk(sk)->pf->af;
5446 port = htons(inet_sk(sk)->num);
5447 af->inaddr_any(&autoaddr, port);
5449 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5452 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5454 * From RFC 2292
5455 * 4.2 The cmsghdr Structure *
5457 * When ancillary data is sent or received, any number of ancillary data
5458 * objects can be specified by the msg_control and msg_controllen members of
5459 * the msghdr structure, because each object is preceded by
5460 * a cmsghdr structure defining the object's length (the cmsg_len member).
5461 * Historically Berkeley-derived implementations have passed only one object
5462 * at a time, but this API allows multiple objects to be
5463 * passed in a single call to sendmsg() or recvmsg(). The following example
5464 * shows two ancillary data objects in a control buffer.
5466 * |<--------------------------- msg_controllen -------------------------->|
5467 * | |
5469 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5471 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5472 * | | |
5474 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5476 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5477 * | | | | |
5479 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5480 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5482 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5484 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5488 * msg_control
5489 * points here
5491 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5492 sctp_cmsgs_t *cmsgs)
5494 struct cmsghdr *cmsg;
5496 for (cmsg = CMSG_FIRSTHDR(msg);
5497 cmsg != NULL;
5498 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5499 if (!CMSG_OK(msg, cmsg))
5500 return -EINVAL;
5502 /* Should we parse this header or ignore? */
5503 if (cmsg->cmsg_level != IPPROTO_SCTP)
5504 continue;
5506 /* Strictly check lengths following example in SCM code. */
5507 switch (cmsg->cmsg_type) {
5508 case SCTP_INIT:
5509 /* SCTP Socket API Extension
5510 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5512 * This cmsghdr structure provides information for
5513 * initializing new SCTP associations with sendmsg().
5514 * The SCTP_INITMSG socket option uses this same data
5515 * structure. This structure is not used for
5516 * recvmsg().
5518 * cmsg_level cmsg_type cmsg_data[]
5519 * ------------ ------------ ----------------------
5520 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5522 if (cmsg->cmsg_len !=
5523 CMSG_LEN(sizeof(struct sctp_initmsg)))
5524 return -EINVAL;
5525 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5526 break;
5528 case SCTP_SNDRCV:
5529 /* SCTP Socket API Extension
5530 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5532 * This cmsghdr structure specifies SCTP options for
5533 * sendmsg() and describes SCTP header information
5534 * about a received message through recvmsg().
5536 * cmsg_level cmsg_type cmsg_data[]
5537 * ------------ ------------ ----------------------
5538 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5540 if (cmsg->cmsg_len !=
5541 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5542 return -EINVAL;
5544 cmsgs->info =
5545 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5547 /* Minimally, validate the sinfo_flags. */
5548 if (cmsgs->info->sinfo_flags &
5549 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5550 SCTP_ABORT | SCTP_EOF))
5551 return -EINVAL;
5552 break;
5554 default:
5555 return -EINVAL;
5558 return 0;
5562 * Wait for a packet..
5563 * Note: This function is the same function as in core/datagram.c
5564 * with a few modifications to make lksctp work.
5566 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5568 int error;
5569 DEFINE_WAIT(wait);
5571 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5573 /* Socket errors? */
5574 error = sock_error(sk);
5575 if (error)
5576 goto out;
5578 if (!skb_queue_empty(&sk->sk_receive_queue))
5579 goto ready;
5581 /* Socket shut down? */
5582 if (sk->sk_shutdown & RCV_SHUTDOWN)
5583 goto out;
5585 /* Sequenced packets can come disconnected. If so we report the
5586 * problem.
5588 error = -ENOTCONN;
5590 /* Is there a good reason to think that we may receive some data? */
5591 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5592 goto out;
5594 /* Handle signals. */
5595 if (signal_pending(current))
5596 goto interrupted;
5598 /* Let another process have a go. Since we are going to sleep
5599 * anyway. Note: This may cause odd behaviors if the message
5600 * does not fit in the user's buffer, but this seems to be the
5601 * only way to honor MSG_DONTWAIT realistically.
5603 sctp_release_sock(sk);
5604 *timeo_p = schedule_timeout(*timeo_p);
5605 sctp_lock_sock(sk);
5607 ready:
5608 finish_wait(sk->sk_sleep, &wait);
5609 return 0;
5611 interrupted:
5612 error = sock_intr_errno(*timeo_p);
5614 out:
5615 finish_wait(sk->sk_sleep, &wait);
5616 *err = error;
5617 return error;
5620 /* Receive a datagram.
5621 * Note: This is pretty much the same routine as in core/datagram.c
5622 * with a few changes to make lksctp work.
5624 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5625 int noblock, int *err)
5627 int error;
5628 struct sk_buff *skb;
5629 long timeo;
5631 timeo = sock_rcvtimeo(sk, noblock);
5633 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5634 timeo, MAX_SCHEDULE_TIMEOUT);
5636 do {
5637 /* Again only user level code calls this function,
5638 * so nothing interrupt level
5639 * will suddenly eat the receive_queue.
5641 * Look at current nfs client by the way...
5642 * However, this function was corrent in any case. 8)
5644 if (flags & MSG_PEEK) {
5645 spin_lock_bh(&sk->sk_receive_queue.lock);
5646 skb = skb_peek(&sk->sk_receive_queue);
5647 if (skb)
5648 atomic_inc(&skb->users);
5649 spin_unlock_bh(&sk->sk_receive_queue.lock);
5650 } else {
5651 skb = skb_dequeue(&sk->sk_receive_queue);
5654 if (skb)
5655 return skb;
5657 /* Caller is allowed not to check sk->sk_err before calling. */
5658 error = sock_error(sk);
5659 if (error)
5660 goto no_packet;
5662 if (sk->sk_shutdown & RCV_SHUTDOWN)
5663 break;
5665 /* User doesn't want to wait. */
5666 error = -EAGAIN;
5667 if (!timeo)
5668 goto no_packet;
5669 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5671 return NULL;
5673 no_packet:
5674 *err = error;
5675 return NULL;
5678 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5679 static void __sctp_write_space(struct sctp_association *asoc)
5681 struct sock *sk = asoc->base.sk;
5682 struct socket *sock = sk->sk_socket;
5684 if ((sctp_wspace(asoc) > 0) && sock) {
5685 if (waitqueue_active(&asoc->wait))
5686 wake_up_interruptible(&asoc->wait);
5688 if (sctp_writeable(sk)) {
5689 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5690 wake_up_interruptible(sk->sk_sleep);
5692 /* Note that we try to include the Async I/O support
5693 * here by modeling from the current TCP/UDP code.
5694 * We have not tested with it yet.
5696 if (sock->fasync_list &&
5697 !(sk->sk_shutdown & SEND_SHUTDOWN))
5698 sock_wake_async(sock, 2, POLL_OUT);
5703 /* Do accounting for the sndbuf space.
5704 * Decrement the used sndbuf space of the corresponding association by the
5705 * data size which was just transmitted(freed).
5707 static void sctp_wfree(struct sk_buff *skb)
5709 struct sctp_association *asoc;
5710 struct sctp_chunk *chunk;
5711 struct sock *sk;
5713 /* Get the saved chunk pointer. */
5714 chunk = *((struct sctp_chunk **)(skb->cb));
5715 asoc = chunk->asoc;
5716 sk = asoc->base.sk;
5717 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5718 sizeof(struct sk_buff) +
5719 sizeof(struct sctp_chunk);
5721 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5723 sock_wfree(skb);
5724 __sctp_write_space(asoc);
5726 sctp_association_put(asoc);
5729 /* Do accounting for the receive space on the socket.
5730 * Accounting for the association is done in ulpevent.c
5731 * We set this as a destructor for the cloned data skbs so that
5732 * accounting is done at the correct time.
5734 void sctp_sock_rfree(struct sk_buff *skb)
5736 struct sock *sk = skb->sk;
5737 struct sctp_ulpevent *event = sctp_skb2event(skb);
5739 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5743 /* Helper function to wait for space in the sndbuf. */
5744 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5745 size_t msg_len)
5747 struct sock *sk = asoc->base.sk;
5748 int err = 0;
5749 long current_timeo = *timeo_p;
5750 DEFINE_WAIT(wait);
5752 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5753 asoc, (long)(*timeo_p), msg_len);
5755 /* Increment the association's refcnt. */
5756 sctp_association_hold(asoc);
5758 /* Wait on the association specific sndbuf space. */
5759 for (;;) {
5760 prepare_to_wait_exclusive(&asoc->wait, &wait,
5761 TASK_INTERRUPTIBLE);
5762 if (!*timeo_p)
5763 goto do_nonblock;
5764 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5765 asoc->base.dead)
5766 goto do_error;
5767 if (signal_pending(current))
5768 goto do_interrupted;
5769 if (msg_len <= sctp_wspace(asoc))
5770 break;
5772 /* Let another process have a go. Since we are going
5773 * to sleep anyway.
5775 sctp_release_sock(sk);
5776 current_timeo = schedule_timeout(current_timeo);
5777 BUG_ON(sk != asoc->base.sk);
5778 sctp_lock_sock(sk);
5780 *timeo_p = current_timeo;
5783 out:
5784 finish_wait(&asoc->wait, &wait);
5786 /* Release the association's refcnt. */
5787 sctp_association_put(asoc);
5789 return err;
5791 do_error:
5792 err = -EPIPE;
5793 goto out;
5795 do_interrupted:
5796 err = sock_intr_errno(*timeo_p);
5797 goto out;
5799 do_nonblock:
5800 err = -EAGAIN;
5801 goto out;
5804 /* If socket sndbuf has changed, wake up all per association waiters. */
5805 void sctp_write_space(struct sock *sk)
5807 struct sctp_association *asoc;
5808 struct list_head *pos;
5810 /* Wake up the tasks in each wait queue. */
5811 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5812 asoc = list_entry(pos, struct sctp_association, asocs);
5813 __sctp_write_space(asoc);
5817 /* Is there any sndbuf space available on the socket?
5819 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5820 * associations on the same socket. For a UDP-style socket with
5821 * multiple associations, it is possible for it to be "unwriteable"
5822 * prematurely. I assume that this is acceptable because
5823 * a premature "unwriteable" is better than an accidental "writeable" which
5824 * would cause an unwanted block under certain circumstances. For the 1-1
5825 * UDP-style sockets or TCP-style sockets, this code should work.
5826 * - Daisy
5828 static int sctp_writeable(struct sock *sk)
5830 int amt = 0;
5832 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5833 if (amt < 0)
5834 amt = 0;
5835 return amt;
5838 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5839 * returns immediately with EINPROGRESS.
5841 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5843 struct sock *sk = asoc->base.sk;
5844 int err = 0;
5845 long current_timeo = *timeo_p;
5846 DEFINE_WAIT(wait);
5848 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5849 (long)(*timeo_p));
5851 /* Increment the association's refcnt. */
5852 sctp_association_hold(asoc);
5854 for (;;) {
5855 prepare_to_wait_exclusive(&asoc->wait, &wait,
5856 TASK_INTERRUPTIBLE);
5857 if (!*timeo_p)
5858 goto do_nonblock;
5859 if (sk->sk_shutdown & RCV_SHUTDOWN)
5860 break;
5861 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5862 asoc->base.dead)
5863 goto do_error;
5864 if (signal_pending(current))
5865 goto do_interrupted;
5867 if (sctp_state(asoc, ESTABLISHED))
5868 break;
5870 /* Let another process have a go. Since we are going
5871 * to sleep anyway.
5873 sctp_release_sock(sk);
5874 current_timeo = schedule_timeout(current_timeo);
5875 sctp_lock_sock(sk);
5877 *timeo_p = current_timeo;
5880 out:
5881 finish_wait(&asoc->wait, &wait);
5883 /* Release the association's refcnt. */
5884 sctp_association_put(asoc);
5886 return err;
5888 do_error:
5889 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5890 err = -ETIMEDOUT;
5891 else
5892 err = -ECONNREFUSED;
5893 goto out;
5895 do_interrupted:
5896 err = sock_intr_errno(*timeo_p);
5897 goto out;
5899 do_nonblock:
5900 err = -EINPROGRESS;
5901 goto out;
5904 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5906 struct sctp_endpoint *ep;
5907 int err = 0;
5908 DEFINE_WAIT(wait);
5910 ep = sctp_sk(sk)->ep;
5913 for (;;) {
5914 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5915 TASK_INTERRUPTIBLE);
5917 if (list_empty(&ep->asocs)) {
5918 sctp_release_sock(sk);
5919 timeo = schedule_timeout(timeo);
5920 sctp_lock_sock(sk);
5923 err = -EINVAL;
5924 if (!sctp_sstate(sk, LISTENING))
5925 break;
5927 err = 0;
5928 if (!list_empty(&ep->asocs))
5929 break;
5931 err = sock_intr_errno(timeo);
5932 if (signal_pending(current))
5933 break;
5935 err = -EAGAIN;
5936 if (!timeo)
5937 break;
5940 finish_wait(sk->sk_sleep, &wait);
5942 return err;
5945 static void sctp_wait_for_close(struct sock *sk, long timeout)
5947 DEFINE_WAIT(wait);
5949 do {
5950 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5951 if (list_empty(&sctp_sk(sk)->ep->asocs))
5952 break;
5953 sctp_release_sock(sk);
5954 timeout = schedule_timeout(timeout);
5955 sctp_lock_sock(sk);
5956 } while (!signal_pending(current) && timeout);
5958 finish_wait(sk->sk_sleep, &wait);
5961 static void sctp_sock_rfree_frag(struct sk_buff *skb)
5963 struct sk_buff *frag;
5965 if (!skb->data_len)
5966 goto done;
5968 /* Don't forget the fragments. */
5969 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5970 sctp_sock_rfree_frag(frag);
5972 done:
5973 sctp_sock_rfree(skb);
5976 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5978 struct sk_buff *frag;
5980 if (!skb->data_len)
5981 goto done;
5983 /* Don't forget the fragments. */
5984 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5985 sctp_skb_set_owner_r_frag(frag, sk);
5987 done:
5988 sctp_skb_set_owner_r(skb, sk);
5991 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5992 * and its messages to the newsk.
5994 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5995 struct sctp_association *assoc,
5996 sctp_socket_type_t type)
5998 struct sctp_sock *oldsp = sctp_sk(oldsk);
5999 struct sctp_sock *newsp = sctp_sk(newsk);
6000 struct sctp_bind_bucket *pp; /* hash list port iterator */
6001 struct sctp_endpoint *newep = newsp->ep;
6002 struct sk_buff *skb, *tmp;
6003 struct sctp_ulpevent *event;
6004 int flags = 0;
6006 /* Migrate socket buffer sizes and all the socket level options to the
6007 * new socket.
6009 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6010 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6011 /* Brute force copy old sctp opt. */
6012 inet_sk_copy_descendant(newsk, oldsk);
6014 /* Restore the ep value that was overwritten with the above structure
6015 * copy.
6017 newsp->ep = newep;
6018 newsp->hmac = NULL;
6020 /* Hook this new socket in to the bind_hash list. */
6021 pp = sctp_sk(oldsk)->bind_hash;
6022 sk_add_bind_node(newsk, &pp->owner);
6023 sctp_sk(newsk)->bind_hash = pp;
6024 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6026 /* Copy the bind_addr list from the original endpoint to the new
6027 * endpoint so that we can handle restarts properly
6029 if (PF_INET6 == assoc->base.sk->sk_family)
6030 flags = SCTP_ADDR6_ALLOWED;
6031 if (assoc->peer.ipv4_address)
6032 flags |= SCTP_ADDR4_PEERSUPP;
6033 if (assoc->peer.ipv6_address)
6034 flags |= SCTP_ADDR6_PEERSUPP;
6035 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6036 &oldsp->ep->base.bind_addr,
6037 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6039 /* Move any messages in the old socket's receive queue that are for the
6040 * peeled off association to the new socket's receive queue.
6042 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6043 event = sctp_skb2event(skb);
6044 if (event->asoc == assoc) {
6045 sctp_sock_rfree_frag(skb);
6046 __skb_unlink(skb, &oldsk->sk_receive_queue);
6047 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6048 sctp_skb_set_owner_r_frag(skb, newsk);
6052 /* Clean up any messages pending delivery due to partial
6053 * delivery. Three cases:
6054 * 1) No partial deliver; no work.
6055 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6056 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6058 skb_queue_head_init(&newsp->pd_lobby);
6059 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6061 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6062 struct sk_buff_head *queue;
6064 /* Decide which queue to move pd_lobby skbs to. */
6065 if (assoc->ulpq.pd_mode) {
6066 queue = &newsp->pd_lobby;
6067 } else
6068 queue = &newsk->sk_receive_queue;
6070 /* Walk through the pd_lobby, looking for skbs that
6071 * need moved to the new socket.
6073 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6074 event = sctp_skb2event(skb);
6075 if (event->asoc == assoc) {
6076 sctp_sock_rfree_frag(skb);
6077 __skb_unlink(skb, &oldsp->pd_lobby);
6078 __skb_queue_tail(queue, skb);
6079 sctp_skb_set_owner_r_frag(skb, newsk);
6083 /* Clear up any skbs waiting for the partial
6084 * delivery to finish.
6086 if (assoc->ulpq.pd_mode)
6087 sctp_clear_pd(oldsk, NULL);
6091 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6092 sctp_sock_rfree_frag(skb);
6093 sctp_skb_set_owner_r_frag(skb, newsk);
6096 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6097 sctp_sock_rfree_frag(skb);
6098 sctp_skb_set_owner_r_frag(skb, newsk);
6101 /* Set the type of socket to indicate that it is peeled off from the
6102 * original UDP-style socket or created with the accept() call on a
6103 * TCP-style socket..
6105 newsp->type = type;
6107 /* Mark the new socket "in-use" by the user so that any packets
6108 * that may arrive on the association after we've moved it are
6109 * queued to the backlog. This prevents a potential race between
6110 * backlog processing on the old socket and new-packet processing
6111 * on the new socket.
6113 * The caller has just allocated newsk so we can guarantee that other
6114 * paths won't try to lock it and then oldsk.
6116 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6117 sctp_assoc_migrate(assoc, newsk);
6119 /* If the association on the newsk is already closed before accept()
6120 * is called, set RCV_SHUTDOWN flag.
6122 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6123 newsk->sk_shutdown |= RCV_SHUTDOWN;
6125 newsk->sk_state = SCTP_SS_ESTABLISHED;
6126 sctp_release_sock(newsk);
6129 /* This proto struct describes the ULP interface for SCTP. */
6130 struct proto sctp_prot = {
6131 .name = "SCTP",
6132 .owner = THIS_MODULE,
6133 .close = sctp_close,
6134 .connect = sctp_connect,
6135 .disconnect = sctp_disconnect,
6136 .accept = sctp_accept,
6137 .ioctl = sctp_ioctl,
6138 .init = sctp_init_sock,
6139 .destroy = sctp_destroy_sock,
6140 .shutdown = sctp_shutdown,
6141 .setsockopt = sctp_setsockopt,
6142 .getsockopt = sctp_getsockopt,
6143 .sendmsg = sctp_sendmsg,
6144 .recvmsg = sctp_recvmsg,
6145 .bind = sctp_bind,
6146 .backlog_rcv = sctp_backlog_rcv,
6147 .hash = sctp_hash,
6148 .unhash = sctp_unhash,
6149 .get_port = sctp_get_port,
6150 .obj_size = sizeof(struct sctp_sock),
6153 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6154 struct proto sctpv6_prot = {
6155 .name = "SCTPv6",
6156 .owner = THIS_MODULE,
6157 .close = sctp_close,
6158 .connect = sctp_connect,
6159 .disconnect = sctp_disconnect,
6160 .accept = sctp_accept,
6161 .ioctl = sctp_ioctl,
6162 .init = sctp_init_sock,
6163 .destroy = sctp_destroy_sock,
6164 .shutdown = sctp_shutdown,
6165 .setsockopt = sctp_setsockopt,
6166 .getsockopt = sctp_getsockopt,
6167 .sendmsg = sctp_sendmsg,
6168 .recvmsg = sctp_recvmsg,
6169 .bind = sctp_bind,
6170 .backlog_rcv = sctp_backlog_rcv,
6171 .hash = sctp_hash,
6172 .unhash = sctp_unhash,
6173 .get_port = sctp_get_port,
6174 .obj_size = sizeof(struct sctp6_sock),
6176 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */