Fix sctp privilege elevation (CVE-2006-3745)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / socket.c
blob0f0ef2d87434938ad7b0cee85b73100ea3e38f4f
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
65 #include <linux/ip.h>
66 #include <linux/capability.h>
67 #include <linux/fcntl.h>
68 #include <linux/poll.h>
69 #include <linux/init.h>
70 #include <linux/crypto.h>
72 #include <net/ip.h>
73 #include <net/icmp.h>
74 #include <net/route.h>
75 #include <net/ipv6.h>
76 #include <net/inet_common.h>
78 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sock.h>
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
83 /* WARNING: Please do not remove the SCTP_STATIC attribute to
84 * any of the functions below as they are used to export functions
85 * used by a project regression testsuite.
88 /* Forward declarations for internal helper functions. */
89 static int sctp_writeable(struct sock *sk);
90 static void sctp_wfree(struct sk_buff *skb);
91 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
92 size_t msg_len);
93 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
94 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
95 static int sctp_wait_for_accept(struct sock *sk, long timeo);
96 static void sctp_wait_for_close(struct sock *sk, long timeo);
97 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
98 union sctp_addr *addr, int len);
99 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
100 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
103 static int sctp_send_asconf(struct sctp_association *asoc,
104 struct sctp_chunk *chunk);
105 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
106 static int sctp_autobind(struct sock *sk);
107 static void sctp_sock_migrate(struct sock *, struct sock *,
108 struct sctp_association *, sctp_socket_type_t);
109 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
111 extern kmem_cache_t *sctp_bucket_cachep;
113 /* Get the sndbuf space available at the time on the association. */
114 static inline int sctp_wspace(struct sctp_association *asoc)
116 struct sock *sk = asoc->base.sk;
117 int amt = 0;
119 if (asoc->ep->sndbuf_policy) {
120 /* make sure that no association uses more than sk_sndbuf */
121 amt = sk->sk_sndbuf - asoc->sndbuf_used;
122 } else {
123 /* do socket level accounting */
124 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
127 if (amt < 0)
128 amt = 0;
130 return amt;
133 /* Increment the used sndbuf space count of the corresponding association by
134 * the size of the outgoing data chunk.
135 * Also, set the skb destructor for sndbuf accounting later.
137 * Since it is always 1-1 between chunk and skb, and also a new skb is always
138 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
139 * destructor in the data chunk skb for the purpose of the sndbuf space
140 * tracking.
142 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
144 struct sctp_association *asoc = chunk->asoc;
145 struct sock *sk = asoc->base.sk;
147 /* The sndbuf space is tracked per association. */
148 sctp_association_hold(asoc);
150 skb_set_owner_w(chunk->skb, sk);
152 chunk->skb->destructor = sctp_wfree;
153 /* Save the chunk pointer in skb for sctp_wfree to use later. */
154 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
156 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
157 sizeof(struct sk_buff) +
158 sizeof(struct sctp_chunk);
160 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
163 /* Verify that this is a valid address. */
164 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
165 int len)
167 struct sctp_af *af;
169 /* Verify basic sockaddr. */
170 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
171 if (!af)
172 return -EINVAL;
174 /* Is this a valid SCTP address? */
175 if (!af->addr_valid(addr, sctp_sk(sk)))
176 return -EINVAL;
178 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
179 return -EINVAL;
181 return 0;
184 /* Look up the association by its id. If this is not a UDP-style
185 * socket, the ID field is always ignored.
187 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
189 struct sctp_association *asoc = NULL;
191 /* If this is not a UDP-style socket, assoc id should be ignored. */
192 if (!sctp_style(sk, UDP)) {
193 /* Return NULL if the socket state is not ESTABLISHED. It
194 * could be a TCP-style listening socket or a socket which
195 * hasn't yet called connect() to establish an association.
197 if (!sctp_sstate(sk, ESTABLISHED))
198 return NULL;
200 /* Get the first and the only association from the list. */
201 if (!list_empty(&sctp_sk(sk)->ep->asocs))
202 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
203 struct sctp_association, asocs);
204 return asoc;
207 /* Otherwise this is a UDP-style socket. */
208 if (!id || (id == (sctp_assoc_t)-1))
209 return NULL;
211 spin_lock_bh(&sctp_assocs_id_lock);
212 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
213 spin_unlock_bh(&sctp_assocs_id_lock);
215 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
216 return NULL;
218 return asoc;
221 /* Look up the transport from an address and an assoc id. If both address and
222 * id are specified, the associations matching the address and the id should be
223 * the same.
225 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
226 struct sockaddr_storage *addr,
227 sctp_assoc_t id)
229 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
230 struct sctp_transport *transport;
231 union sctp_addr *laddr = (union sctp_addr *)addr;
233 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
234 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
235 (union sctp_addr *)addr,
236 &transport);
237 laddr->v4.sin_port = htons(laddr->v4.sin_port);
239 if (!addr_asoc)
240 return NULL;
242 id_asoc = sctp_id2assoc(sk, id);
243 if (id_asoc && (id_asoc != addr_asoc))
244 return NULL;
246 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
247 (union sctp_addr *)addr);
249 return transport;
252 /* API 3.1.2 bind() - UDP Style Syntax
253 * The syntax of bind() is,
255 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
257 * sd - the socket descriptor returned by socket().
258 * addr - the address structure (struct sockaddr_in or struct
259 * sockaddr_in6 [RFC 2553]),
260 * addr_len - the size of the address structure.
262 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
264 int retval = 0;
266 sctp_lock_sock(sk);
268 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
269 sk, addr, addr_len);
271 /* Disallow binding twice. */
272 if (!sctp_sk(sk)->ep->base.bind_addr.port)
273 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
274 addr_len);
275 else
276 retval = -EINVAL;
278 sctp_release_sock(sk);
280 return retval;
283 static long sctp_get_port_local(struct sock *, union sctp_addr *);
285 /* Verify this is a valid sockaddr. */
286 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
287 union sctp_addr *addr, int len)
289 struct sctp_af *af;
291 /* Check minimum size. */
292 if (len < sizeof (struct sockaddr))
293 return NULL;
295 /* Does this PF support this AF? */
296 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
297 return NULL;
299 /* If we get this far, af is valid. */
300 af = sctp_get_af_specific(addr->sa.sa_family);
302 if (len < af->sockaddr_len)
303 return NULL;
305 return af;
308 /* Bind a local address either to an endpoint or to an association. */
309 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
311 struct sctp_sock *sp = sctp_sk(sk);
312 struct sctp_endpoint *ep = sp->ep;
313 struct sctp_bind_addr *bp = &ep->base.bind_addr;
314 struct sctp_af *af;
315 unsigned short snum;
316 int ret = 0;
318 /* Common sockaddr verification. */
319 af = sctp_sockaddr_af(sp, addr, len);
320 if (!af) {
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
322 sk, addr, len);
323 return -EINVAL;
326 snum = ntohs(addr->v4.sin_port);
328 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
329 ", port: %d, new port: %d, len: %d)\n",
331 addr,
332 bp->port, snum,
333 len);
335 /* PF specific bind() address verification. */
336 if (!sp->pf->bind_verify(sp, addr))
337 return -EADDRNOTAVAIL;
339 /* We must either be unbound, or bind to the same port. */
340 if (bp->port && (snum != bp->port)) {
341 SCTP_DEBUG_PRINTK("sctp_do_bind:"
342 " New port %d does not match existing port "
343 "%d.\n", snum, bp->port);
344 return -EINVAL;
347 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
348 return -EACCES;
350 /* Make sure we are allowed to bind here.
351 * The function sctp_get_port_local() does duplicate address
352 * detection.
354 if ((ret = sctp_get_port_local(sk, addr))) {
355 if (ret == (long) sk) {
356 /* This endpoint has a conflicting address. */
357 return -EINVAL;
358 } else {
359 return -EADDRINUSE;
363 /* Refresh ephemeral port. */
364 if (!bp->port)
365 bp->port = inet_sk(sk)->num;
367 /* Add the address to the bind address list. */
368 sctp_local_bh_disable();
369 sctp_write_lock(&ep->base.addr_lock);
371 /* Use GFP_ATOMIC since BHs are disabled. */
372 addr->v4.sin_port = ntohs(addr->v4.sin_port);
373 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
374 addr->v4.sin_port = htons(addr->v4.sin_port);
375 sctp_write_unlock(&ep->base.addr_lock);
376 sctp_local_bh_enable();
378 /* Copy back into socket for getsockname() use. */
379 if (!ret) {
380 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
381 af->to_sk_saddr(addr, sk);
384 return ret;
387 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
389 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
390 * at any one time. If a sender, after sending an ASCONF chunk, decides
391 * it needs to transfer another ASCONF Chunk, it MUST wait until the
392 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
393 * subsequent ASCONF. Note this restriction binds each side, so at any
394 * time two ASCONF may be in-transit on any given association (one sent
395 * from each endpoint).
397 static int sctp_send_asconf(struct sctp_association *asoc,
398 struct sctp_chunk *chunk)
400 int retval = 0;
402 /* If there is an outstanding ASCONF chunk, queue it for later
403 * transmission.
405 if (asoc->addip_last_asconf) {
406 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
407 goto out;
410 /* Hold the chunk until an ASCONF_ACK is received. */
411 sctp_chunk_hold(chunk);
412 retval = sctp_primitive_ASCONF(asoc, chunk);
413 if (retval)
414 sctp_chunk_free(chunk);
415 else
416 asoc->addip_last_asconf = chunk;
418 out:
419 return retval;
422 /* Add a list of addresses as bind addresses to local endpoint or
423 * association.
425 * Basically run through each address specified in the addrs/addrcnt
426 * array/length pair, determine if it is IPv6 or IPv4 and call
427 * sctp_do_bind() on it.
429 * If any of them fails, then the operation will be reversed and the
430 * ones that were added will be removed.
432 * Only sctp_setsockopt_bindx() is supposed to call this function.
434 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
436 int cnt;
437 int retval = 0;
438 void *addr_buf;
439 struct sockaddr *sa_addr;
440 struct sctp_af *af;
442 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
443 sk, addrs, addrcnt);
445 addr_buf = addrs;
446 for (cnt = 0; cnt < addrcnt; cnt++) {
447 /* The list may contain either IPv4 or IPv6 address;
448 * determine the address length for walking thru the list.
450 sa_addr = (struct sockaddr *)addr_buf;
451 af = sctp_get_af_specific(sa_addr->sa_family);
452 if (!af) {
453 retval = -EINVAL;
454 goto err_bindx_add;
457 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
458 af->sockaddr_len);
460 addr_buf += af->sockaddr_len;
462 err_bindx_add:
463 if (retval < 0) {
464 /* Failed. Cleanup the ones that have been added */
465 if (cnt > 0)
466 sctp_bindx_rem(sk, addrs, cnt);
467 return retval;
471 return retval;
474 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
475 * associations that are part of the endpoint indicating that a list of local
476 * addresses are added to the endpoint.
478 * If any of the addresses is already in the bind address list of the
479 * association, we do not send the chunk for that association. But it will not
480 * affect other associations.
482 * Only sctp_setsockopt_bindx() is supposed to call this function.
484 static int sctp_send_asconf_add_ip(struct sock *sk,
485 struct sockaddr *addrs,
486 int addrcnt)
488 struct sctp_sock *sp;
489 struct sctp_endpoint *ep;
490 struct sctp_association *asoc;
491 struct sctp_bind_addr *bp;
492 struct sctp_chunk *chunk;
493 struct sctp_sockaddr_entry *laddr;
494 union sctp_addr *addr;
495 void *addr_buf;
496 struct sctp_af *af;
497 struct list_head *pos;
498 struct list_head *p;
499 int i;
500 int retval = 0;
502 if (!sctp_addip_enable)
503 return retval;
505 sp = sctp_sk(sk);
506 ep = sp->ep;
508 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
509 __FUNCTION__, sk, addrs, addrcnt);
511 list_for_each(pos, &ep->asocs) {
512 asoc = list_entry(pos, struct sctp_association, asocs);
514 if (!asoc->peer.asconf_capable)
515 continue;
517 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
518 continue;
520 if (!sctp_state(asoc, ESTABLISHED))
521 continue;
523 /* Check if any address in the packed array of addresses is
524 * in the bind address list of the association. If so,
525 * do not send the asconf chunk to its peer, but continue with
526 * other associations.
528 addr_buf = addrs;
529 for (i = 0; i < addrcnt; i++) {
530 addr = (union sctp_addr *)addr_buf;
531 af = sctp_get_af_specific(addr->v4.sin_family);
532 if (!af) {
533 retval = -EINVAL;
534 goto out;
537 if (sctp_assoc_lookup_laddr(asoc, addr))
538 break;
540 addr_buf += af->sockaddr_len;
542 if (i < addrcnt)
543 continue;
545 /* Use the first address in bind addr list of association as
546 * Address Parameter of ASCONF CHUNK.
548 sctp_read_lock(&asoc->base.addr_lock);
549 bp = &asoc->base.bind_addr;
550 p = bp->address_list.next;
551 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
552 sctp_read_unlock(&asoc->base.addr_lock);
554 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
555 addrcnt, SCTP_PARAM_ADD_IP);
556 if (!chunk) {
557 retval = -ENOMEM;
558 goto out;
561 retval = sctp_send_asconf(asoc, chunk);
563 /* FIXME: After sending the add address ASCONF chunk, we
564 * cannot append the address to the association's binding
565 * address list, because the new address may be used as the
566 * source of a message sent to the peer before the ASCONF
567 * chunk is received by the peer. So we should wait until
568 * ASCONF_ACK is received.
572 out:
573 return retval;
576 /* Remove a list of addresses from bind addresses list. Do not remove the
577 * last address.
579 * Basically run through each address specified in the addrs/addrcnt
580 * array/length pair, determine if it is IPv6 or IPv4 and call
581 * sctp_del_bind() on it.
583 * If any of them fails, then the operation will be reversed and the
584 * ones that were removed will be added back.
586 * At least one address has to be left; if only one address is
587 * available, the operation will return -EBUSY.
589 * Only sctp_setsockopt_bindx() is supposed to call this function.
591 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
593 struct sctp_sock *sp = sctp_sk(sk);
594 struct sctp_endpoint *ep = sp->ep;
595 int cnt;
596 struct sctp_bind_addr *bp = &ep->base.bind_addr;
597 int retval = 0;
598 union sctp_addr saveaddr;
599 void *addr_buf;
600 struct sockaddr *sa_addr;
601 struct sctp_af *af;
603 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
604 sk, addrs, addrcnt);
606 addr_buf = addrs;
607 for (cnt = 0; cnt < addrcnt; cnt++) {
608 /* If the bind address list is empty or if there is only one
609 * bind address, there is nothing more to be removed (we need
610 * at least one address here).
612 if (list_empty(&bp->address_list) ||
613 (sctp_list_single_entry(&bp->address_list))) {
614 retval = -EBUSY;
615 goto err_bindx_rem;
618 /* The list may contain either IPv4 or IPv6 address;
619 * determine the address length to copy the address to
620 * saveaddr.
622 sa_addr = (struct sockaddr *)addr_buf;
623 af = sctp_get_af_specific(sa_addr->sa_family);
624 if (!af) {
625 retval = -EINVAL;
626 goto err_bindx_rem;
628 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
629 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
630 if (saveaddr.v4.sin_port != bp->port) {
631 retval = -EINVAL;
632 goto err_bindx_rem;
635 /* FIXME - There is probably a need to check if sk->sk_saddr and
636 * sk->sk_rcv_addr are currently set to one of the addresses to
637 * be removed. This is something which needs to be looked into
638 * when we are fixing the outstanding issues with multi-homing
639 * socket routing and failover schemes. Refer to comments in
640 * sctp_do_bind(). -daisy
642 sctp_local_bh_disable();
643 sctp_write_lock(&ep->base.addr_lock);
645 retval = sctp_del_bind_addr(bp, &saveaddr);
647 sctp_write_unlock(&ep->base.addr_lock);
648 sctp_local_bh_enable();
650 addr_buf += af->sockaddr_len;
651 err_bindx_rem:
652 if (retval < 0) {
653 /* Failed. Add the ones that has been removed back */
654 if (cnt > 0)
655 sctp_bindx_add(sk, addrs, cnt);
656 return retval;
660 return retval;
663 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
664 * the associations that are part of the endpoint indicating that a list of
665 * local addresses are removed from the endpoint.
667 * If any of the addresses is already in the bind address list of the
668 * association, we do not send the chunk for that association. But it will not
669 * affect other associations.
671 * Only sctp_setsockopt_bindx() is supposed to call this function.
673 static int sctp_send_asconf_del_ip(struct sock *sk,
674 struct sockaddr *addrs,
675 int addrcnt)
677 struct sctp_sock *sp;
678 struct sctp_endpoint *ep;
679 struct sctp_association *asoc;
680 struct sctp_bind_addr *bp;
681 struct sctp_chunk *chunk;
682 union sctp_addr *laddr;
683 void *addr_buf;
684 struct sctp_af *af;
685 struct list_head *pos;
686 int i;
687 int retval = 0;
689 if (!sctp_addip_enable)
690 return retval;
692 sp = sctp_sk(sk);
693 ep = sp->ep;
695 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
696 __FUNCTION__, sk, addrs, addrcnt);
698 list_for_each(pos, &ep->asocs) {
699 asoc = list_entry(pos, struct sctp_association, asocs);
701 if (!asoc->peer.asconf_capable)
702 continue;
704 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
705 continue;
707 if (!sctp_state(asoc, ESTABLISHED))
708 continue;
710 /* Check if any address in the packed array of addresses is
711 * not present in the bind address list of the association.
712 * If so, do not send the asconf chunk to its peer, but
713 * continue with other associations.
715 addr_buf = addrs;
716 for (i = 0; i < addrcnt; i++) {
717 laddr = (union sctp_addr *)addr_buf;
718 af = sctp_get_af_specific(laddr->v4.sin_family);
719 if (!af) {
720 retval = -EINVAL;
721 goto out;
724 if (!sctp_assoc_lookup_laddr(asoc, laddr))
725 break;
727 addr_buf += af->sockaddr_len;
729 if (i < addrcnt)
730 continue;
732 /* Find one address in the association's bind address list
733 * that is not in the packed array of addresses. This is to
734 * make sure that we do not delete all the addresses in the
735 * association.
737 sctp_read_lock(&asoc->base.addr_lock);
738 bp = &asoc->base.bind_addr;
739 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
740 addrcnt, sp);
741 sctp_read_unlock(&asoc->base.addr_lock);
742 if (!laddr)
743 continue;
745 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
746 SCTP_PARAM_DEL_IP);
747 if (!chunk) {
748 retval = -ENOMEM;
749 goto out;
752 retval = sctp_send_asconf(asoc, chunk);
754 /* FIXME: After sending the delete address ASCONF chunk, we
755 * cannot remove the addresses from the association's bind
756 * address list, because there maybe some packet send to
757 * the delete addresses, so we should wait until ASCONF_ACK
758 * packet is received.
761 out:
762 return retval;
765 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
767 * API 8.1
768 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
769 * int flags);
771 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
772 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
773 * or IPv6 addresses.
775 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
776 * Section 3.1.2 for this usage.
778 * addrs is a pointer to an array of one or more socket addresses. Each
779 * address is contained in its appropriate structure (i.e. struct
780 * sockaddr_in or struct sockaddr_in6) the family of the address type
781 * must be used to distengish the address length (note that this
782 * representation is termed a "packed array" of addresses). The caller
783 * specifies the number of addresses in the array with addrcnt.
785 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
786 * -1, and sets errno to the appropriate error code.
788 * For SCTP, the port given in each socket address must be the same, or
789 * sctp_bindx() will fail, setting errno to EINVAL.
791 * The flags parameter is formed from the bitwise OR of zero or more of
792 * the following currently defined flags:
794 * SCTP_BINDX_ADD_ADDR
796 * SCTP_BINDX_REM_ADDR
798 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
799 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
800 * addresses from the association. The two flags are mutually exclusive;
801 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
802 * not remove all addresses from an association; sctp_bindx() will
803 * reject such an attempt with EINVAL.
805 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
806 * additional addresses with an endpoint after calling bind(). Or use
807 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
808 * socket is associated with so that no new association accepted will be
809 * associated with those addresses. If the endpoint supports dynamic
810 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
811 * endpoint to send the appropriate message to the peer to change the
812 * peers address lists.
814 * Adding and removing addresses from a connected association is
815 * optional functionality. Implementations that do not support this
816 * functionality should return EOPNOTSUPP.
818 * Basically do nothing but copying the addresses from user to kernel
819 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
820 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
821 * from userspace.
823 * We don't use copy_from_user() for optimization: we first do the
824 * sanity checks (buffer size -fast- and access check-healthy
825 * pointer); if all of those succeed, then we can alloc the memory
826 * (expensive operation) needed to copy the data to kernel. Then we do
827 * the copying without checking the user space area
828 * (__copy_from_user()).
830 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
831 * it.
833 * sk The sk of the socket
834 * addrs The pointer to the addresses in user land
835 * addrssize Size of the addrs buffer
836 * op Operation to perform (add or remove, see the flags of
837 * sctp_bindx)
839 * Returns 0 if ok, <0 errno code on error.
841 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
842 struct sockaddr __user *addrs,
843 int addrs_size, int op)
845 struct sockaddr *kaddrs;
846 int err;
847 int addrcnt = 0;
848 int walk_size = 0;
849 struct sockaddr *sa_addr;
850 void *addr_buf;
851 struct sctp_af *af;
853 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
854 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
856 if (unlikely(addrs_size <= 0))
857 return -EINVAL;
859 /* Check the user passed a healthy pointer. */
860 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
861 return -EFAULT;
863 /* Alloc space for the address array in kernel memory. */
864 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
865 if (unlikely(!kaddrs))
866 return -ENOMEM;
868 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
869 kfree(kaddrs);
870 return -EFAULT;
873 /* Walk through the addrs buffer and count the number of addresses. */
874 addr_buf = kaddrs;
875 while (walk_size < addrs_size) {
876 sa_addr = (struct sockaddr *)addr_buf;
877 af = sctp_get_af_specific(sa_addr->sa_family);
879 /* If the address family is not supported or if this address
880 * causes the address buffer to overflow return EINVAL.
882 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
883 kfree(kaddrs);
884 return -EINVAL;
886 addrcnt++;
887 addr_buf += af->sockaddr_len;
888 walk_size += af->sockaddr_len;
891 /* Do the work. */
892 switch (op) {
893 case SCTP_BINDX_ADD_ADDR:
894 err = sctp_bindx_add(sk, kaddrs, addrcnt);
895 if (err)
896 goto out;
897 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
898 break;
900 case SCTP_BINDX_REM_ADDR:
901 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
902 if (err)
903 goto out;
904 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
905 break;
907 default:
908 err = -EINVAL;
909 break;
912 out:
913 kfree(kaddrs);
915 return err;
918 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
920 * Common routine for handling connect() and sctp_connectx().
921 * Connect will come in with just a single address.
923 static int __sctp_connect(struct sock* sk,
924 struct sockaddr *kaddrs,
925 int addrs_size)
927 struct sctp_sock *sp;
928 struct sctp_endpoint *ep;
929 struct sctp_association *asoc = NULL;
930 struct sctp_association *asoc2;
931 struct sctp_transport *transport;
932 union sctp_addr to;
933 struct sctp_af *af;
934 sctp_scope_t scope;
935 long timeo;
936 int err = 0;
937 int addrcnt = 0;
938 int walk_size = 0;
939 struct sockaddr *sa_addr;
940 void *addr_buf;
942 sp = sctp_sk(sk);
943 ep = sp->ep;
945 /* connect() cannot be done on a socket that is already in ESTABLISHED
946 * state - UDP-style peeled off socket or a TCP-style socket that
947 * is already connected.
948 * It cannot be done even on a TCP-style listening socket.
950 if (sctp_sstate(sk, ESTABLISHED) ||
951 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
952 err = -EISCONN;
953 goto out_free;
956 /* Walk through the addrs buffer and count the number of addresses. */
957 addr_buf = kaddrs;
958 while (walk_size < addrs_size) {
959 sa_addr = (struct sockaddr *)addr_buf;
960 af = sctp_get_af_specific(sa_addr->sa_family);
962 /* If the address family is not supported or if this address
963 * causes the address buffer to overflow return EINVAL.
965 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
966 err = -EINVAL;
967 goto out_free;
970 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
971 af->sockaddr_len);
972 if (err)
973 goto out_free;
975 memcpy(&to, sa_addr, af->sockaddr_len);
976 to.v4.sin_port = ntohs(to.v4.sin_port);
978 /* Check if there already is a matching association on the
979 * endpoint (other than the one created here).
981 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
982 if (asoc2 && asoc2 != asoc) {
983 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
984 err = -EISCONN;
985 else
986 err = -EALREADY;
987 goto out_free;
990 /* If we could not find a matching association on the endpoint,
991 * make sure that there is no peeled-off association matching
992 * the peer address even on another socket.
994 if (sctp_endpoint_is_peeled_off(ep, &to)) {
995 err = -EADDRNOTAVAIL;
996 goto out_free;
999 if (!asoc) {
1000 /* If a bind() or sctp_bindx() is not called prior to
1001 * an sctp_connectx() call, the system picks an
1002 * ephemeral port and will choose an address set
1003 * equivalent to binding with a wildcard address.
1005 if (!ep->base.bind_addr.port) {
1006 if (sctp_autobind(sk)) {
1007 err = -EAGAIN;
1008 goto out_free;
1010 } else {
1012 * If an unprivileged user inherits a 1-many
1013 * style socket with open associations on a
1014 * privileged port, it MAY be permitted to
1015 * accept new associations, but it SHOULD NOT
1016 * be permitted to open new associations.
1018 if (ep->base.bind_addr.port < PROT_SOCK &&
1019 !capable(CAP_NET_BIND_SERVICE)) {
1020 err = -EACCES;
1021 goto out_free;
1025 scope = sctp_scope(&to);
1026 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1027 if (!asoc) {
1028 err = -ENOMEM;
1029 goto out_free;
1033 /* Prime the peer's transport structures. */
1034 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1035 SCTP_UNKNOWN);
1036 if (!transport) {
1037 err = -ENOMEM;
1038 goto out_free;
1041 addrcnt++;
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1046 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1047 if (err < 0) {
1048 goto out_free;
1051 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1052 if (err < 0) {
1053 goto out_free;
1056 /* Initialize sk's dport and daddr for getpeername() */
1057 inet_sk(sk)->dport = htons(asoc->peer.port);
1058 af = sctp_get_af_specific(to.sa.sa_family);
1059 af->to_sk_daddr(&to, sk);
1061 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1062 err = sctp_wait_for_connect(asoc, &timeo);
1064 /* Don't free association on exit. */
1065 asoc = NULL;
1067 out_free:
1069 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1070 " kaddrs: %p err: %d\n",
1071 asoc, kaddrs, err);
1072 if (asoc)
1073 sctp_association_free(asoc);
1074 return err;
1077 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1079 * API 8.9
1080 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1082 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1083 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1084 * or IPv6 addresses.
1086 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1087 * Section 3.1.2 for this usage.
1089 * addrs is a pointer to an array of one or more socket addresses. Each
1090 * address is contained in its appropriate structure (i.e. struct
1091 * sockaddr_in or struct sockaddr_in6) the family of the address type
1092 * must be used to distengish the address length (note that this
1093 * representation is termed a "packed array" of addresses). The caller
1094 * specifies the number of addresses in the array with addrcnt.
1096 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1097 * -1, and sets errno to the appropriate error code.
1099 * For SCTP, the port given in each socket address must be the same, or
1100 * sctp_connectx() will fail, setting errno to EINVAL.
1102 * An application can use sctp_connectx to initiate an association with
1103 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1104 * allows a caller to specify multiple addresses at which a peer can be
1105 * reached. The way the SCTP stack uses the list of addresses to set up
1106 * the association is implementation dependant. This function only
1107 * specifies that the stack will try to make use of all the addresses in
1108 * the list when needed.
1110 * Note that the list of addresses passed in is only used for setting up
1111 * the association. It does not necessarily equal the set of addresses
1112 * the peer uses for the resulting association. If the caller wants to
1113 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1114 * retrieve them after the association has been set up.
1116 * Basically do nothing but copying the addresses from user to kernel
1117 * land and invoking either sctp_connectx(). This is used for tunneling
1118 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1120 * We don't use copy_from_user() for optimization: we first do the
1121 * sanity checks (buffer size -fast- and access check-healthy
1122 * pointer); if all of those succeed, then we can alloc the memory
1123 * (expensive operation) needed to copy the data to kernel. Then we do
1124 * the copying without checking the user space area
1125 * (__copy_from_user()).
1127 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1128 * it.
1130 * sk The sk of the socket
1131 * addrs The pointer to the addresses in user land
1132 * addrssize Size of the addrs buffer
1134 * Returns 0 if ok, <0 errno code on error.
1136 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1137 struct sockaddr __user *addrs,
1138 int addrs_size)
1140 int err = 0;
1141 struct sockaddr *kaddrs;
1143 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1144 __FUNCTION__, sk, addrs, addrs_size);
1146 if (unlikely(addrs_size <= 0))
1147 return -EINVAL;
1149 /* Check the user passed a healthy pointer. */
1150 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1151 return -EFAULT;
1153 /* Alloc space for the address array in kernel memory. */
1154 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1155 if (unlikely(!kaddrs))
1156 return -ENOMEM;
1158 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1159 err = -EFAULT;
1160 } else {
1161 err = __sctp_connect(sk, kaddrs, addrs_size);
1164 kfree(kaddrs);
1165 return err;
1168 /* API 3.1.4 close() - UDP Style Syntax
1169 * Applications use close() to perform graceful shutdown (as described in
1170 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1171 * by a UDP-style socket.
1173 * The syntax is
1175 * ret = close(int sd);
1177 * sd - the socket descriptor of the associations to be closed.
1179 * To gracefully shutdown a specific association represented by the
1180 * UDP-style socket, an application should use the sendmsg() call,
1181 * passing no user data, but including the appropriate flag in the
1182 * ancillary data (see Section xxxx).
1184 * If sd in the close() call is a branched-off socket representing only
1185 * one association, the shutdown is performed on that association only.
1187 * 4.1.6 close() - TCP Style Syntax
1189 * Applications use close() to gracefully close down an association.
1191 * The syntax is:
1193 * int close(int sd);
1195 * sd - the socket descriptor of the association to be closed.
1197 * After an application calls close() on a socket descriptor, no further
1198 * socket operations will succeed on that descriptor.
1200 * API 7.1.4 SO_LINGER
1202 * An application using the TCP-style socket can use this option to
1203 * perform the SCTP ABORT primitive. The linger option structure is:
1205 * struct linger {
1206 * int l_onoff; // option on/off
1207 * int l_linger; // linger time
1208 * };
1210 * To enable the option, set l_onoff to 1. If the l_linger value is set
1211 * to 0, calling close() is the same as the ABORT primitive. If the
1212 * value is set to a negative value, the setsockopt() call will return
1213 * an error. If the value is set to a positive value linger_time, the
1214 * close() can be blocked for at most linger_time ms. If the graceful
1215 * shutdown phase does not finish during this period, close() will
1216 * return but the graceful shutdown phase continues in the system.
1218 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1220 struct sctp_endpoint *ep;
1221 struct sctp_association *asoc;
1222 struct list_head *pos, *temp;
1224 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1226 sctp_lock_sock(sk);
1227 sk->sk_shutdown = SHUTDOWN_MASK;
1229 ep = sctp_sk(sk)->ep;
1231 /* Walk all associations on a socket, not on an endpoint. */
1232 list_for_each_safe(pos, temp, &ep->asocs) {
1233 asoc = list_entry(pos, struct sctp_association, asocs);
1235 if (sctp_style(sk, TCP)) {
1236 /* A closed association can still be in the list if
1237 * it belongs to a TCP-style listening socket that is
1238 * not yet accepted. If so, free it. If not, send an
1239 * ABORT or SHUTDOWN based on the linger options.
1241 if (sctp_state(asoc, CLOSED)) {
1242 sctp_unhash_established(asoc);
1243 sctp_association_free(asoc);
1245 } else if (sock_flag(sk, SOCK_LINGER) &&
1246 !sk->sk_lingertime)
1247 sctp_primitive_ABORT(asoc, NULL);
1248 else
1249 sctp_primitive_SHUTDOWN(asoc, NULL);
1250 } else
1251 sctp_primitive_SHUTDOWN(asoc, NULL);
1254 /* Clean up any skbs sitting on the receive queue. */
1255 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1256 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1258 /* On a TCP-style socket, block for at most linger_time if set. */
1259 if (sctp_style(sk, TCP) && timeout)
1260 sctp_wait_for_close(sk, timeout);
1262 /* This will run the backlog queue. */
1263 sctp_release_sock(sk);
1265 /* Supposedly, no process has access to the socket, but
1266 * the net layers still may.
1268 sctp_local_bh_disable();
1269 sctp_bh_lock_sock(sk);
1271 /* Hold the sock, since sk_common_release() will put sock_put()
1272 * and we have just a little more cleanup.
1274 sock_hold(sk);
1275 sk_common_release(sk);
1277 sctp_bh_unlock_sock(sk);
1278 sctp_local_bh_enable();
1280 sock_put(sk);
1282 SCTP_DBG_OBJCNT_DEC(sock);
1285 /* Handle EPIPE error. */
1286 static int sctp_error(struct sock *sk, int flags, int err)
1288 if (err == -EPIPE)
1289 err = sock_error(sk) ? : -EPIPE;
1290 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1291 send_sig(SIGPIPE, current, 0);
1292 return err;
1295 /* API 3.1.3 sendmsg() - UDP Style Syntax
1297 * An application uses sendmsg() and recvmsg() calls to transmit data to
1298 * and receive data from its peer.
1300 * ssize_t sendmsg(int socket, const struct msghdr *message,
1301 * int flags);
1303 * socket - the socket descriptor of the endpoint.
1304 * message - pointer to the msghdr structure which contains a single
1305 * user message and possibly some ancillary data.
1307 * See Section 5 for complete description of the data
1308 * structures.
1310 * flags - flags sent or received with the user message, see Section
1311 * 5 for complete description of the flags.
1313 * Note: This function could use a rewrite especially when explicit
1314 * connect support comes in.
1316 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1318 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1320 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1321 struct msghdr *msg, size_t msg_len)
1323 struct sctp_sock *sp;
1324 struct sctp_endpoint *ep;
1325 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1326 struct sctp_transport *transport, *chunk_tp;
1327 struct sctp_chunk *chunk;
1328 union sctp_addr to;
1329 struct sockaddr *msg_name = NULL;
1330 struct sctp_sndrcvinfo default_sinfo = { 0 };
1331 struct sctp_sndrcvinfo *sinfo;
1332 struct sctp_initmsg *sinit;
1333 sctp_assoc_t associd = 0;
1334 sctp_cmsgs_t cmsgs = { NULL };
1335 int err;
1336 sctp_scope_t scope;
1337 long timeo;
1338 __u16 sinfo_flags = 0;
1339 struct sctp_datamsg *datamsg;
1340 struct list_head *pos;
1341 int msg_flags = msg->msg_flags;
1343 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1344 sk, msg, msg_len);
1346 err = 0;
1347 sp = sctp_sk(sk);
1348 ep = sp->ep;
1350 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1352 /* We cannot send a message over a TCP-style listening socket. */
1353 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1354 err = -EPIPE;
1355 goto out_nounlock;
1358 /* Parse out the SCTP CMSGs. */
1359 err = sctp_msghdr_parse(msg, &cmsgs);
1361 if (err) {
1362 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1363 goto out_nounlock;
1366 /* Fetch the destination address for this packet. This
1367 * address only selects the association--it is not necessarily
1368 * the address we will send to.
1369 * For a peeled-off socket, msg_name is ignored.
1371 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1372 int msg_namelen = msg->msg_namelen;
1374 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1375 msg_namelen);
1376 if (err)
1377 return err;
1379 if (msg_namelen > sizeof(to))
1380 msg_namelen = sizeof(to);
1381 memcpy(&to, msg->msg_name, msg_namelen);
1382 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1383 "0x%x:%u.\n",
1384 to.v4.sin_addr.s_addr, to.v4.sin_port);
1386 to.v4.sin_port = ntohs(to.v4.sin_port);
1387 msg_name = msg->msg_name;
1390 sinfo = cmsgs.info;
1391 sinit = cmsgs.init;
1393 /* Did the user specify SNDRCVINFO? */
1394 if (sinfo) {
1395 sinfo_flags = sinfo->sinfo_flags;
1396 associd = sinfo->sinfo_assoc_id;
1399 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1400 msg_len, sinfo_flags);
1402 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1403 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1404 err = -EINVAL;
1405 goto out_nounlock;
1408 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1409 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1410 * If SCTP_ABORT is set, the message length could be non zero with
1411 * the msg_iov set to the user abort reason.
1413 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1414 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1415 err = -EINVAL;
1416 goto out_nounlock;
1419 /* If SCTP_ADDR_OVER is set, there must be an address
1420 * specified in msg_name.
1422 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1423 err = -EINVAL;
1424 goto out_nounlock;
1427 transport = NULL;
1429 SCTP_DEBUG_PRINTK("About to look up association.\n");
1431 sctp_lock_sock(sk);
1433 /* If a msg_name has been specified, assume this is to be used. */
1434 if (msg_name) {
1435 /* Look for a matching association on the endpoint. */
1436 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1437 if (!asoc) {
1438 /* If we could not find a matching association on the
1439 * endpoint, make sure that it is not a TCP-style
1440 * socket that already has an association or there is
1441 * no peeled-off association on another socket.
1443 if ((sctp_style(sk, TCP) &&
1444 sctp_sstate(sk, ESTABLISHED)) ||
1445 sctp_endpoint_is_peeled_off(ep, &to)) {
1446 err = -EADDRNOTAVAIL;
1447 goto out_unlock;
1450 } else {
1451 asoc = sctp_id2assoc(sk, associd);
1452 if (!asoc) {
1453 err = -EPIPE;
1454 goto out_unlock;
1458 if (asoc) {
1459 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1461 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1462 * socket that has an association in CLOSED state. This can
1463 * happen when an accepted socket has an association that is
1464 * already CLOSED.
1466 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1467 err = -EPIPE;
1468 goto out_unlock;
1471 if (sinfo_flags & SCTP_EOF) {
1472 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1473 asoc);
1474 sctp_primitive_SHUTDOWN(asoc, NULL);
1475 err = 0;
1476 goto out_unlock;
1478 if (sinfo_flags & SCTP_ABORT) {
1479 struct sctp_chunk *chunk;
1481 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1482 if (!chunk) {
1483 err = -ENOMEM;
1484 goto out_unlock;
1487 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1488 sctp_primitive_ABORT(asoc, chunk);
1489 err = 0;
1490 goto out_unlock;
1494 /* Do we need to create the association? */
1495 if (!asoc) {
1496 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1498 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1499 err = -EINVAL;
1500 goto out_unlock;
1503 /* Check for invalid stream against the stream counts,
1504 * either the default or the user specified stream counts.
1506 if (sinfo) {
1507 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1508 /* Check against the defaults. */
1509 if (sinfo->sinfo_stream >=
1510 sp->initmsg.sinit_num_ostreams) {
1511 err = -EINVAL;
1512 goto out_unlock;
1514 } else {
1515 /* Check against the requested. */
1516 if (sinfo->sinfo_stream >=
1517 sinit->sinit_num_ostreams) {
1518 err = -EINVAL;
1519 goto out_unlock;
1525 * API 3.1.2 bind() - UDP Style Syntax
1526 * If a bind() or sctp_bindx() is not called prior to a
1527 * sendmsg() call that initiates a new association, the
1528 * system picks an ephemeral port and will choose an address
1529 * set equivalent to binding with a wildcard address.
1531 if (!ep->base.bind_addr.port) {
1532 if (sctp_autobind(sk)) {
1533 err = -EAGAIN;
1534 goto out_unlock;
1536 } else {
1538 * If an unprivileged user inherits a one-to-many
1539 * style socket with open associations on a privileged
1540 * port, it MAY be permitted to accept new associations,
1541 * but it SHOULD NOT be permitted to open new
1542 * associations.
1544 if (ep->base.bind_addr.port < PROT_SOCK &&
1545 !capable(CAP_NET_BIND_SERVICE)) {
1546 err = -EACCES;
1547 goto out_unlock;
1551 scope = sctp_scope(&to);
1552 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1553 if (!new_asoc) {
1554 err = -ENOMEM;
1555 goto out_unlock;
1557 asoc = new_asoc;
1559 /* If the SCTP_INIT ancillary data is specified, set all
1560 * the association init values accordingly.
1562 if (sinit) {
1563 if (sinit->sinit_num_ostreams) {
1564 asoc->c.sinit_num_ostreams =
1565 sinit->sinit_num_ostreams;
1567 if (sinit->sinit_max_instreams) {
1568 asoc->c.sinit_max_instreams =
1569 sinit->sinit_max_instreams;
1571 if (sinit->sinit_max_attempts) {
1572 asoc->max_init_attempts
1573 = sinit->sinit_max_attempts;
1575 if (sinit->sinit_max_init_timeo) {
1576 asoc->max_init_timeo =
1577 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1581 /* Prime the peer's transport structures. */
1582 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1583 if (!transport) {
1584 err = -ENOMEM;
1585 goto out_free;
1587 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1588 if (err < 0) {
1589 err = -ENOMEM;
1590 goto out_free;
1594 /* ASSERT: we have a valid association at this point. */
1595 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1597 if (!sinfo) {
1598 /* If the user didn't specify SNDRCVINFO, make up one with
1599 * some defaults.
1601 default_sinfo.sinfo_stream = asoc->default_stream;
1602 default_sinfo.sinfo_flags = asoc->default_flags;
1603 default_sinfo.sinfo_ppid = asoc->default_ppid;
1604 default_sinfo.sinfo_context = asoc->default_context;
1605 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1606 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1607 sinfo = &default_sinfo;
1610 /* API 7.1.7, the sndbuf size per association bounds the
1611 * maximum size of data that can be sent in a single send call.
1613 if (msg_len > sk->sk_sndbuf) {
1614 err = -EMSGSIZE;
1615 goto out_free;
1618 /* If fragmentation is disabled and the message length exceeds the
1619 * association fragmentation point, return EMSGSIZE. The I-D
1620 * does not specify what this error is, but this looks like
1621 * a great fit.
1623 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1624 err = -EMSGSIZE;
1625 goto out_free;
1628 if (sinfo) {
1629 /* Check for invalid stream. */
1630 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1631 err = -EINVAL;
1632 goto out_free;
1636 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1637 if (!sctp_wspace(asoc)) {
1638 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1639 if (err)
1640 goto out_free;
1643 /* If an address is passed with the sendto/sendmsg call, it is used
1644 * to override the primary destination address in the TCP model, or
1645 * when SCTP_ADDR_OVER flag is set in the UDP model.
1647 if ((sctp_style(sk, TCP) && msg_name) ||
1648 (sinfo_flags & SCTP_ADDR_OVER)) {
1649 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1650 if (!chunk_tp) {
1651 err = -EINVAL;
1652 goto out_free;
1654 } else
1655 chunk_tp = NULL;
1657 /* Auto-connect, if we aren't connected already. */
1658 if (sctp_state(asoc, CLOSED)) {
1659 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1660 if (err < 0)
1661 goto out_free;
1662 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1665 /* Break the message into multiple chunks of maximum size. */
1666 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1667 if (!datamsg) {
1668 err = -ENOMEM;
1669 goto out_free;
1672 /* Now send the (possibly) fragmented message. */
1673 list_for_each(pos, &datamsg->chunks) {
1674 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1675 sctp_datamsg_track(chunk);
1677 /* Do accounting for the write space. */
1678 sctp_set_owner_w(chunk);
1680 chunk->transport = chunk_tp;
1682 /* Send it to the lower layers. Note: all chunks
1683 * must either fail or succeed. The lower layer
1684 * works that way today. Keep it that way or this
1685 * breaks.
1687 err = sctp_primitive_SEND(asoc, chunk);
1688 /* Did the lower layer accept the chunk? */
1689 if (err)
1690 sctp_chunk_free(chunk);
1691 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1694 sctp_datamsg_free(datamsg);
1695 if (err)
1696 goto out_free;
1697 else
1698 err = msg_len;
1700 /* If we are already past ASSOCIATE, the lower
1701 * layers are responsible for association cleanup.
1703 goto out_unlock;
1705 out_free:
1706 if (new_asoc)
1707 sctp_association_free(asoc);
1708 out_unlock:
1709 sctp_release_sock(sk);
1711 out_nounlock:
1712 return sctp_error(sk, msg_flags, err);
1714 #if 0
1715 do_sock_err:
1716 if (msg_len)
1717 err = msg_len;
1718 else
1719 err = sock_error(sk);
1720 goto out;
1722 do_interrupted:
1723 if (msg_len)
1724 err = msg_len;
1725 goto out;
1726 #endif /* 0 */
1729 /* This is an extended version of skb_pull() that removes the data from the
1730 * start of a skb even when data is spread across the list of skb's in the
1731 * frag_list. len specifies the total amount of data that needs to be removed.
1732 * when 'len' bytes could be removed from the skb, it returns 0.
1733 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1734 * could not be removed.
1736 static int sctp_skb_pull(struct sk_buff *skb, int len)
1738 struct sk_buff *list;
1739 int skb_len = skb_headlen(skb);
1740 int rlen;
1742 if (len <= skb_len) {
1743 __skb_pull(skb, len);
1744 return 0;
1746 len -= skb_len;
1747 __skb_pull(skb, skb_len);
1749 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1750 rlen = sctp_skb_pull(list, len);
1751 skb->len -= (len-rlen);
1752 skb->data_len -= (len-rlen);
1754 if (!rlen)
1755 return 0;
1757 len = rlen;
1760 return len;
1763 /* API 3.1.3 recvmsg() - UDP Style Syntax
1765 * ssize_t recvmsg(int socket, struct msghdr *message,
1766 * int flags);
1768 * socket - the socket descriptor of the endpoint.
1769 * message - pointer to the msghdr structure which contains a single
1770 * user message and possibly some ancillary data.
1772 * See Section 5 for complete description of the data
1773 * structures.
1775 * flags - flags sent or received with the user message, see Section
1776 * 5 for complete description of the flags.
1778 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1780 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1781 struct msghdr *msg, size_t len, int noblock,
1782 int flags, int *addr_len)
1784 struct sctp_ulpevent *event = NULL;
1785 struct sctp_sock *sp = sctp_sk(sk);
1786 struct sk_buff *skb;
1787 int copied;
1788 int err = 0;
1789 int skb_len;
1791 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1792 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1793 "len", len, "knoblauch", noblock,
1794 "flags", flags, "addr_len", addr_len);
1796 sctp_lock_sock(sk);
1798 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1799 err = -ENOTCONN;
1800 goto out;
1803 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1804 if (!skb)
1805 goto out;
1807 /* Get the total length of the skb including any skb's in the
1808 * frag_list.
1810 skb_len = skb->len;
1812 copied = skb_len;
1813 if (copied > len)
1814 copied = len;
1816 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1818 event = sctp_skb2event(skb);
1820 if (err)
1821 goto out_free;
1823 sock_recv_timestamp(msg, sk, skb);
1824 if (sctp_ulpevent_is_notification(event)) {
1825 msg->msg_flags |= MSG_NOTIFICATION;
1826 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1827 } else {
1828 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1831 /* Check if we allow SCTP_SNDRCVINFO. */
1832 if (sp->subscribe.sctp_data_io_event)
1833 sctp_ulpevent_read_sndrcvinfo(event, msg);
1834 #if 0
1835 /* FIXME: we should be calling IP/IPv6 layers. */
1836 if (sk->sk_protinfo.af_inet.cmsg_flags)
1837 ip_cmsg_recv(msg, skb);
1838 #endif
1840 err = copied;
1842 /* If skb's length exceeds the user's buffer, update the skb and
1843 * push it back to the receive_queue so that the next call to
1844 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1846 if (skb_len > copied) {
1847 msg->msg_flags &= ~MSG_EOR;
1848 if (flags & MSG_PEEK)
1849 goto out_free;
1850 sctp_skb_pull(skb, copied);
1851 skb_queue_head(&sk->sk_receive_queue, skb);
1853 /* When only partial message is copied to the user, increase
1854 * rwnd by that amount. If all the data in the skb is read,
1855 * rwnd is updated when the event is freed.
1857 sctp_assoc_rwnd_increase(event->asoc, copied);
1858 goto out;
1859 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1860 (event->msg_flags & MSG_EOR))
1861 msg->msg_flags |= MSG_EOR;
1862 else
1863 msg->msg_flags &= ~MSG_EOR;
1865 out_free:
1866 if (flags & MSG_PEEK) {
1867 /* Release the skb reference acquired after peeking the skb in
1868 * sctp_skb_recv_datagram().
1870 kfree_skb(skb);
1871 } else {
1872 /* Free the event which includes releasing the reference to
1873 * the owner of the skb, freeing the skb and updating the
1874 * rwnd.
1876 sctp_ulpevent_free(event);
1878 out:
1879 sctp_release_sock(sk);
1880 return err;
1883 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1885 * This option is a on/off flag. If enabled no SCTP message
1886 * fragmentation will be performed. Instead if a message being sent
1887 * exceeds the current PMTU size, the message will NOT be sent and
1888 * instead a error will be indicated to the user.
1890 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1891 char __user *optval, int optlen)
1893 int val;
1895 if (optlen < sizeof(int))
1896 return -EINVAL;
1898 if (get_user(val, (int __user *)optval))
1899 return -EFAULT;
1901 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1903 return 0;
1906 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1907 int optlen)
1909 if (optlen != sizeof(struct sctp_event_subscribe))
1910 return -EINVAL;
1911 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1912 return -EFAULT;
1913 return 0;
1916 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1918 * This socket option is applicable to the UDP-style socket only. When
1919 * set it will cause associations that are idle for more than the
1920 * specified number of seconds to automatically close. An association
1921 * being idle is defined an association that has NOT sent or received
1922 * user data. The special value of '0' indicates that no automatic
1923 * close of any associations should be performed. The option expects an
1924 * integer defining the number of seconds of idle time before an
1925 * association is closed.
1927 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1928 int optlen)
1930 struct sctp_sock *sp = sctp_sk(sk);
1932 /* Applicable to UDP-style socket only */
1933 if (sctp_style(sk, TCP))
1934 return -EOPNOTSUPP;
1935 if (optlen != sizeof(int))
1936 return -EINVAL;
1937 if (copy_from_user(&sp->autoclose, optval, optlen))
1938 return -EFAULT;
1940 return 0;
1943 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1945 * Applications can enable or disable heartbeats for any peer address of
1946 * an association, modify an address's heartbeat interval, force a
1947 * heartbeat to be sent immediately, and adjust the address's maximum
1948 * number of retransmissions sent before an address is considered
1949 * unreachable. The following structure is used to access and modify an
1950 * address's parameters:
1952 * struct sctp_paddrparams {
1953 * sctp_assoc_t spp_assoc_id;
1954 * struct sockaddr_storage spp_address;
1955 * uint32_t spp_hbinterval;
1956 * uint16_t spp_pathmaxrxt;
1957 * uint32_t spp_pathmtu;
1958 * uint32_t spp_sackdelay;
1959 * uint32_t spp_flags;
1960 * };
1962 * spp_assoc_id - (one-to-many style socket) This is filled in the
1963 * application, and identifies the association for
1964 * this query.
1965 * spp_address - This specifies which address is of interest.
1966 * spp_hbinterval - This contains the value of the heartbeat interval,
1967 * in milliseconds. If a value of zero
1968 * is present in this field then no changes are to
1969 * be made to this parameter.
1970 * spp_pathmaxrxt - This contains the maximum number of
1971 * retransmissions before this address shall be
1972 * considered unreachable. If a value of zero
1973 * is present in this field then no changes are to
1974 * be made to this parameter.
1975 * spp_pathmtu - When Path MTU discovery is disabled the value
1976 * specified here will be the "fixed" path mtu.
1977 * Note that if the spp_address field is empty
1978 * then all associations on this address will
1979 * have this fixed path mtu set upon them.
1981 * spp_sackdelay - When delayed sack is enabled, this value specifies
1982 * the number of milliseconds that sacks will be delayed
1983 * for. This value will apply to all addresses of an
1984 * association if the spp_address field is empty. Note
1985 * also, that if delayed sack is enabled and this
1986 * value is set to 0, no change is made to the last
1987 * recorded delayed sack timer value.
1989 * spp_flags - These flags are used to control various features
1990 * on an association. The flag field may contain
1991 * zero or more of the following options.
1993 * SPP_HB_ENABLE - Enable heartbeats on the
1994 * specified address. Note that if the address
1995 * field is empty all addresses for the association
1996 * have heartbeats enabled upon them.
1998 * SPP_HB_DISABLE - Disable heartbeats on the
1999 * speicifed address. Note that if the address
2000 * field is empty all addresses for the association
2001 * will have their heartbeats disabled. Note also
2002 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2003 * mutually exclusive, only one of these two should
2004 * be specified. Enabling both fields will have
2005 * undetermined results.
2007 * SPP_HB_DEMAND - Request a user initiated heartbeat
2008 * to be made immediately.
2010 * SPP_PMTUD_ENABLE - This field will enable PMTU
2011 * discovery upon the specified address. Note that
2012 * if the address feild is empty then all addresses
2013 * on the association are effected.
2015 * SPP_PMTUD_DISABLE - This field will disable PMTU
2016 * discovery upon the specified address. Note that
2017 * if the address feild is empty then all addresses
2018 * on the association are effected. Not also that
2019 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2020 * exclusive. Enabling both will have undetermined
2021 * results.
2023 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2024 * on delayed sack. The time specified in spp_sackdelay
2025 * is used to specify the sack delay for this address. Note
2026 * that if spp_address is empty then all addresses will
2027 * enable delayed sack and take on the sack delay
2028 * value specified in spp_sackdelay.
2029 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2030 * off delayed sack. If the spp_address field is blank then
2031 * delayed sack is disabled for the entire association. Note
2032 * also that this field is mutually exclusive to
2033 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2034 * results.
2036 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2037 struct sctp_transport *trans,
2038 struct sctp_association *asoc,
2039 struct sctp_sock *sp,
2040 int hb_change,
2041 int pmtud_change,
2042 int sackdelay_change)
2044 int error;
2046 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2047 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2048 if (error)
2049 return error;
2052 if (params->spp_hbinterval) {
2053 if (trans) {
2054 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2055 } else if (asoc) {
2056 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2057 } else {
2058 sp->hbinterval = params->spp_hbinterval;
2062 if (hb_change) {
2063 if (trans) {
2064 trans->param_flags =
2065 (trans->param_flags & ~SPP_HB) | hb_change;
2066 } else if (asoc) {
2067 asoc->param_flags =
2068 (asoc->param_flags & ~SPP_HB) | hb_change;
2069 } else {
2070 sp->param_flags =
2071 (sp->param_flags & ~SPP_HB) | hb_change;
2075 if (params->spp_pathmtu) {
2076 if (trans) {
2077 trans->pathmtu = params->spp_pathmtu;
2078 sctp_assoc_sync_pmtu(asoc);
2079 } else if (asoc) {
2080 asoc->pathmtu = params->spp_pathmtu;
2081 sctp_frag_point(sp, params->spp_pathmtu);
2082 } else {
2083 sp->pathmtu = params->spp_pathmtu;
2087 if (pmtud_change) {
2088 if (trans) {
2089 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2090 (params->spp_flags & SPP_PMTUD_ENABLE);
2091 trans->param_flags =
2092 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2093 if (update) {
2094 sctp_transport_pmtu(trans);
2095 sctp_assoc_sync_pmtu(asoc);
2097 } else if (asoc) {
2098 asoc->param_flags =
2099 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2100 } else {
2101 sp->param_flags =
2102 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2106 if (params->spp_sackdelay) {
2107 if (trans) {
2108 trans->sackdelay =
2109 msecs_to_jiffies(params->spp_sackdelay);
2110 } else if (asoc) {
2111 asoc->sackdelay =
2112 msecs_to_jiffies(params->spp_sackdelay);
2113 } else {
2114 sp->sackdelay = params->spp_sackdelay;
2118 if (sackdelay_change) {
2119 if (trans) {
2120 trans->param_flags =
2121 (trans->param_flags & ~SPP_SACKDELAY) |
2122 sackdelay_change;
2123 } else if (asoc) {
2124 asoc->param_flags =
2125 (asoc->param_flags & ~SPP_SACKDELAY) |
2126 sackdelay_change;
2127 } else {
2128 sp->param_flags =
2129 (sp->param_flags & ~SPP_SACKDELAY) |
2130 sackdelay_change;
2134 if (params->spp_pathmaxrxt) {
2135 if (trans) {
2136 trans->pathmaxrxt = params->spp_pathmaxrxt;
2137 } else if (asoc) {
2138 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2139 } else {
2140 sp->pathmaxrxt = params->spp_pathmaxrxt;
2144 return 0;
2147 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2148 char __user *optval, int optlen)
2150 struct sctp_paddrparams params;
2151 struct sctp_transport *trans = NULL;
2152 struct sctp_association *asoc = NULL;
2153 struct sctp_sock *sp = sctp_sk(sk);
2154 int error;
2155 int hb_change, pmtud_change, sackdelay_change;
2157 if (optlen != sizeof(struct sctp_paddrparams))
2158 return - EINVAL;
2160 if (copy_from_user(&params, optval, optlen))
2161 return -EFAULT;
2163 /* Validate flags and value parameters. */
2164 hb_change = params.spp_flags & SPP_HB;
2165 pmtud_change = params.spp_flags & SPP_PMTUD;
2166 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2168 if (hb_change == SPP_HB ||
2169 pmtud_change == SPP_PMTUD ||
2170 sackdelay_change == SPP_SACKDELAY ||
2171 params.spp_sackdelay > 500 ||
2172 (params.spp_pathmtu
2173 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2174 return -EINVAL;
2176 /* If an address other than INADDR_ANY is specified, and
2177 * no transport is found, then the request is invalid.
2179 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2180 trans = sctp_addr_id2transport(sk, &params.spp_address,
2181 params.spp_assoc_id);
2182 if (!trans)
2183 return -EINVAL;
2186 /* Get association, if assoc_id != 0 and the socket is a one
2187 * to many style socket, and an association was not found, then
2188 * the id was invalid.
2190 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2191 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2192 return -EINVAL;
2194 /* Heartbeat demand can only be sent on a transport or
2195 * association, but not a socket.
2197 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2198 return -EINVAL;
2200 /* Process parameters. */
2201 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2202 hb_change, pmtud_change,
2203 sackdelay_change);
2205 if (error)
2206 return error;
2208 /* If changes are for association, also apply parameters to each
2209 * transport.
2211 if (!trans && asoc) {
2212 struct list_head *pos;
2214 list_for_each(pos, &asoc->peer.transport_addr_list) {
2215 trans = list_entry(pos, struct sctp_transport,
2216 transports);
2217 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2218 hb_change, pmtud_change,
2219 sackdelay_change);
2223 return 0;
2226 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2228 * This options will get or set the delayed ack timer. The time is set
2229 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2230 * endpoints default delayed ack timer value. If the assoc_id field is
2231 * non-zero, then the set or get effects the specified association.
2233 * struct sctp_assoc_value {
2234 * sctp_assoc_t assoc_id;
2235 * uint32_t assoc_value;
2236 * };
2238 * assoc_id - This parameter, indicates which association the
2239 * user is preforming an action upon. Note that if
2240 * this field's value is zero then the endpoints
2241 * default value is changed (effecting future
2242 * associations only).
2244 * assoc_value - This parameter contains the number of milliseconds
2245 * that the user is requesting the delayed ACK timer
2246 * be set to. Note that this value is defined in
2247 * the standard to be between 200 and 500 milliseconds.
2249 * Note: a value of zero will leave the value alone,
2250 * but disable SACK delay. A non-zero value will also
2251 * enable SACK delay.
2254 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2255 char __user *optval, int optlen)
2257 struct sctp_assoc_value params;
2258 struct sctp_transport *trans = NULL;
2259 struct sctp_association *asoc = NULL;
2260 struct sctp_sock *sp = sctp_sk(sk);
2262 if (optlen != sizeof(struct sctp_assoc_value))
2263 return - EINVAL;
2265 if (copy_from_user(&params, optval, optlen))
2266 return -EFAULT;
2268 /* Validate value parameter. */
2269 if (params.assoc_value > 500)
2270 return -EINVAL;
2272 /* Get association, if assoc_id != 0 and the socket is a one
2273 * to many style socket, and an association was not found, then
2274 * the id was invalid.
2276 asoc = sctp_id2assoc(sk, params.assoc_id);
2277 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2278 return -EINVAL;
2280 if (params.assoc_value) {
2281 if (asoc) {
2282 asoc->sackdelay =
2283 msecs_to_jiffies(params.assoc_value);
2284 asoc->param_flags =
2285 (asoc->param_flags & ~SPP_SACKDELAY) |
2286 SPP_SACKDELAY_ENABLE;
2287 } else {
2288 sp->sackdelay = params.assoc_value;
2289 sp->param_flags =
2290 (sp->param_flags & ~SPP_SACKDELAY) |
2291 SPP_SACKDELAY_ENABLE;
2293 } else {
2294 if (asoc) {
2295 asoc->param_flags =
2296 (asoc->param_flags & ~SPP_SACKDELAY) |
2297 SPP_SACKDELAY_DISABLE;
2298 } else {
2299 sp->param_flags =
2300 (sp->param_flags & ~SPP_SACKDELAY) |
2301 SPP_SACKDELAY_DISABLE;
2305 /* If change is for association, also apply to each transport. */
2306 if (asoc) {
2307 struct list_head *pos;
2309 list_for_each(pos, &asoc->peer.transport_addr_list) {
2310 trans = list_entry(pos, struct sctp_transport,
2311 transports);
2312 if (params.assoc_value) {
2313 trans->sackdelay =
2314 msecs_to_jiffies(params.assoc_value);
2315 trans->param_flags =
2316 (trans->param_flags & ~SPP_SACKDELAY) |
2317 SPP_SACKDELAY_ENABLE;
2318 } else {
2319 trans->param_flags =
2320 (trans->param_flags & ~SPP_SACKDELAY) |
2321 SPP_SACKDELAY_DISABLE;
2326 return 0;
2329 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2331 * Applications can specify protocol parameters for the default association
2332 * initialization. The option name argument to setsockopt() and getsockopt()
2333 * is SCTP_INITMSG.
2335 * Setting initialization parameters is effective only on an unconnected
2336 * socket (for UDP-style sockets only future associations are effected
2337 * by the change). With TCP-style sockets, this option is inherited by
2338 * sockets derived from a listener socket.
2340 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2342 struct sctp_initmsg sinit;
2343 struct sctp_sock *sp = sctp_sk(sk);
2345 if (optlen != sizeof(struct sctp_initmsg))
2346 return -EINVAL;
2347 if (copy_from_user(&sinit, optval, optlen))
2348 return -EFAULT;
2350 if (sinit.sinit_num_ostreams)
2351 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2352 if (sinit.sinit_max_instreams)
2353 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2354 if (sinit.sinit_max_attempts)
2355 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2356 if (sinit.sinit_max_init_timeo)
2357 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2359 return 0;
2363 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2365 * Applications that wish to use the sendto() system call may wish to
2366 * specify a default set of parameters that would normally be supplied
2367 * through the inclusion of ancillary data. This socket option allows
2368 * such an application to set the default sctp_sndrcvinfo structure.
2369 * The application that wishes to use this socket option simply passes
2370 * in to this call the sctp_sndrcvinfo structure defined in Section
2371 * 5.2.2) The input parameters accepted by this call include
2372 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2373 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2374 * to this call if the caller is using the UDP model.
2376 static int sctp_setsockopt_default_send_param(struct sock *sk,
2377 char __user *optval, int optlen)
2379 struct sctp_sndrcvinfo info;
2380 struct sctp_association *asoc;
2381 struct sctp_sock *sp = sctp_sk(sk);
2383 if (optlen != sizeof(struct sctp_sndrcvinfo))
2384 return -EINVAL;
2385 if (copy_from_user(&info, optval, optlen))
2386 return -EFAULT;
2388 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2389 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2390 return -EINVAL;
2392 if (asoc) {
2393 asoc->default_stream = info.sinfo_stream;
2394 asoc->default_flags = info.sinfo_flags;
2395 asoc->default_ppid = info.sinfo_ppid;
2396 asoc->default_context = info.sinfo_context;
2397 asoc->default_timetolive = info.sinfo_timetolive;
2398 } else {
2399 sp->default_stream = info.sinfo_stream;
2400 sp->default_flags = info.sinfo_flags;
2401 sp->default_ppid = info.sinfo_ppid;
2402 sp->default_context = info.sinfo_context;
2403 sp->default_timetolive = info.sinfo_timetolive;
2406 return 0;
2409 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2411 * Requests that the local SCTP stack use the enclosed peer address as
2412 * the association primary. The enclosed address must be one of the
2413 * association peer's addresses.
2415 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2416 int optlen)
2418 struct sctp_prim prim;
2419 struct sctp_transport *trans;
2421 if (optlen != sizeof(struct sctp_prim))
2422 return -EINVAL;
2424 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2425 return -EFAULT;
2427 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2428 if (!trans)
2429 return -EINVAL;
2431 sctp_assoc_set_primary(trans->asoc, trans);
2433 return 0;
2437 * 7.1.5 SCTP_NODELAY
2439 * Turn on/off any Nagle-like algorithm. This means that packets are
2440 * generally sent as soon as possible and no unnecessary delays are
2441 * introduced, at the cost of more packets in the network. Expects an
2442 * integer boolean flag.
2444 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2445 int optlen)
2447 int val;
2449 if (optlen < sizeof(int))
2450 return -EINVAL;
2451 if (get_user(val, (int __user *)optval))
2452 return -EFAULT;
2454 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2455 return 0;
2460 * 7.1.1 SCTP_RTOINFO
2462 * The protocol parameters used to initialize and bound retransmission
2463 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2464 * and modify these parameters.
2465 * All parameters are time values, in milliseconds. A value of 0, when
2466 * modifying the parameters, indicates that the current value should not
2467 * be changed.
2470 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2471 struct sctp_rtoinfo rtoinfo;
2472 struct sctp_association *asoc;
2474 if (optlen != sizeof (struct sctp_rtoinfo))
2475 return -EINVAL;
2477 if (copy_from_user(&rtoinfo, optval, optlen))
2478 return -EFAULT;
2480 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2482 /* Set the values to the specific association */
2483 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2484 return -EINVAL;
2486 if (asoc) {
2487 if (rtoinfo.srto_initial != 0)
2488 asoc->rto_initial =
2489 msecs_to_jiffies(rtoinfo.srto_initial);
2490 if (rtoinfo.srto_max != 0)
2491 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2492 if (rtoinfo.srto_min != 0)
2493 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2494 } else {
2495 /* If there is no association or the association-id = 0
2496 * set the values to the endpoint.
2498 struct sctp_sock *sp = sctp_sk(sk);
2500 if (rtoinfo.srto_initial != 0)
2501 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2502 if (rtoinfo.srto_max != 0)
2503 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2504 if (rtoinfo.srto_min != 0)
2505 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2508 return 0;
2513 * 7.1.2 SCTP_ASSOCINFO
2515 * This option is used to tune the the maximum retransmission attempts
2516 * of the association.
2517 * Returns an error if the new association retransmission value is
2518 * greater than the sum of the retransmission value of the peer.
2519 * See [SCTP] for more information.
2522 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2525 struct sctp_assocparams assocparams;
2526 struct sctp_association *asoc;
2528 if (optlen != sizeof(struct sctp_assocparams))
2529 return -EINVAL;
2530 if (copy_from_user(&assocparams, optval, optlen))
2531 return -EFAULT;
2533 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2535 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2536 return -EINVAL;
2538 /* Set the values to the specific association */
2539 if (asoc) {
2540 if (assocparams.sasoc_asocmaxrxt != 0)
2541 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2542 if (assocparams.sasoc_cookie_life != 0) {
2543 asoc->cookie_life.tv_sec =
2544 assocparams.sasoc_cookie_life / 1000;
2545 asoc->cookie_life.tv_usec =
2546 (assocparams.sasoc_cookie_life % 1000)
2547 * 1000;
2549 } else {
2550 /* Set the values to the endpoint */
2551 struct sctp_sock *sp = sctp_sk(sk);
2553 if (assocparams.sasoc_asocmaxrxt != 0)
2554 sp->assocparams.sasoc_asocmaxrxt =
2555 assocparams.sasoc_asocmaxrxt;
2556 if (assocparams.sasoc_cookie_life != 0)
2557 sp->assocparams.sasoc_cookie_life =
2558 assocparams.sasoc_cookie_life;
2560 return 0;
2564 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2566 * This socket option is a boolean flag which turns on or off mapped V4
2567 * addresses. If this option is turned on and the socket is type
2568 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2569 * If this option is turned off, then no mapping will be done of V4
2570 * addresses and a user will receive both PF_INET6 and PF_INET type
2571 * addresses on the socket.
2573 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2575 int val;
2576 struct sctp_sock *sp = sctp_sk(sk);
2578 if (optlen < sizeof(int))
2579 return -EINVAL;
2580 if (get_user(val, (int __user *)optval))
2581 return -EFAULT;
2582 if (val)
2583 sp->v4mapped = 1;
2584 else
2585 sp->v4mapped = 0;
2587 return 0;
2591 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2593 * This socket option specifies the maximum size to put in any outgoing
2594 * SCTP chunk. If a message is larger than this size it will be
2595 * fragmented by SCTP into the specified size. Note that the underlying
2596 * SCTP implementation may fragment into smaller sized chunks when the
2597 * PMTU of the underlying association is smaller than the value set by
2598 * the user.
2600 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2602 struct sctp_association *asoc;
2603 struct list_head *pos;
2604 struct sctp_sock *sp = sctp_sk(sk);
2605 int val;
2607 if (optlen < sizeof(int))
2608 return -EINVAL;
2609 if (get_user(val, (int __user *)optval))
2610 return -EFAULT;
2611 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2612 return -EINVAL;
2613 sp->user_frag = val;
2615 /* Update the frag_point of the existing associations. */
2616 list_for_each(pos, &(sp->ep->asocs)) {
2617 asoc = list_entry(pos, struct sctp_association, asocs);
2618 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2621 return 0;
2626 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2628 * Requests that the peer mark the enclosed address as the association
2629 * primary. The enclosed address must be one of the association's
2630 * locally bound addresses. The following structure is used to make a
2631 * set primary request:
2633 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2634 int optlen)
2636 struct sctp_sock *sp;
2637 struct sctp_endpoint *ep;
2638 struct sctp_association *asoc = NULL;
2639 struct sctp_setpeerprim prim;
2640 struct sctp_chunk *chunk;
2641 int err;
2643 sp = sctp_sk(sk);
2644 ep = sp->ep;
2646 if (!sctp_addip_enable)
2647 return -EPERM;
2649 if (optlen != sizeof(struct sctp_setpeerprim))
2650 return -EINVAL;
2652 if (copy_from_user(&prim, optval, optlen))
2653 return -EFAULT;
2655 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2656 if (!asoc)
2657 return -EINVAL;
2659 if (!asoc->peer.asconf_capable)
2660 return -EPERM;
2662 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2663 return -EPERM;
2665 if (!sctp_state(asoc, ESTABLISHED))
2666 return -ENOTCONN;
2668 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2669 return -EADDRNOTAVAIL;
2671 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2672 chunk = sctp_make_asconf_set_prim(asoc,
2673 (union sctp_addr *)&prim.sspp_addr);
2674 if (!chunk)
2675 return -ENOMEM;
2677 err = sctp_send_asconf(asoc, chunk);
2679 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2681 return err;
2684 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2685 int optlen)
2687 struct sctp_setadaption adaption;
2689 if (optlen != sizeof(struct sctp_setadaption))
2690 return -EINVAL;
2691 if (copy_from_user(&adaption, optval, optlen))
2692 return -EFAULT;
2694 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2696 return 0;
2699 /* API 6.2 setsockopt(), getsockopt()
2701 * Applications use setsockopt() and getsockopt() to set or retrieve
2702 * socket options. Socket options are used to change the default
2703 * behavior of sockets calls. They are described in Section 7.
2705 * The syntax is:
2707 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2708 * int __user *optlen);
2709 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2710 * int optlen);
2712 * sd - the socket descript.
2713 * level - set to IPPROTO_SCTP for all SCTP options.
2714 * optname - the option name.
2715 * optval - the buffer to store the value of the option.
2716 * optlen - the size of the buffer.
2718 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2719 char __user *optval, int optlen)
2721 int retval = 0;
2723 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2724 sk, optname);
2726 /* I can hardly begin to describe how wrong this is. This is
2727 * so broken as to be worse than useless. The API draft
2728 * REALLY is NOT helpful here... I am not convinced that the
2729 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2730 * are at all well-founded.
2732 if (level != SOL_SCTP) {
2733 struct sctp_af *af = sctp_sk(sk)->pf->af;
2734 retval = af->setsockopt(sk, level, optname, optval, optlen);
2735 goto out_nounlock;
2738 sctp_lock_sock(sk);
2740 switch (optname) {
2741 case SCTP_SOCKOPT_BINDX_ADD:
2742 /* 'optlen' is the size of the addresses buffer. */
2743 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2744 optlen, SCTP_BINDX_ADD_ADDR);
2745 break;
2747 case SCTP_SOCKOPT_BINDX_REM:
2748 /* 'optlen' is the size of the addresses buffer. */
2749 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2750 optlen, SCTP_BINDX_REM_ADDR);
2751 break;
2753 case SCTP_SOCKOPT_CONNECTX:
2754 /* 'optlen' is the size of the addresses buffer. */
2755 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2756 optlen);
2757 break;
2759 case SCTP_DISABLE_FRAGMENTS:
2760 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2761 break;
2763 case SCTP_EVENTS:
2764 retval = sctp_setsockopt_events(sk, optval, optlen);
2765 break;
2767 case SCTP_AUTOCLOSE:
2768 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2769 break;
2771 case SCTP_PEER_ADDR_PARAMS:
2772 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2773 break;
2775 case SCTP_DELAYED_ACK_TIME:
2776 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2777 break;
2779 case SCTP_INITMSG:
2780 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2781 break;
2782 case SCTP_DEFAULT_SEND_PARAM:
2783 retval = sctp_setsockopt_default_send_param(sk, optval,
2784 optlen);
2785 break;
2786 case SCTP_PRIMARY_ADDR:
2787 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2788 break;
2789 case SCTP_SET_PEER_PRIMARY_ADDR:
2790 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2791 break;
2792 case SCTP_NODELAY:
2793 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2794 break;
2795 case SCTP_RTOINFO:
2796 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2797 break;
2798 case SCTP_ASSOCINFO:
2799 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2800 break;
2801 case SCTP_I_WANT_MAPPED_V4_ADDR:
2802 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2803 break;
2804 case SCTP_MAXSEG:
2805 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2806 break;
2807 case SCTP_ADAPTION_LAYER:
2808 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2809 break;
2811 default:
2812 retval = -ENOPROTOOPT;
2813 break;
2816 sctp_release_sock(sk);
2818 out_nounlock:
2819 return retval;
2822 /* API 3.1.6 connect() - UDP Style Syntax
2824 * An application may use the connect() call in the UDP model to initiate an
2825 * association without sending data.
2827 * The syntax is:
2829 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2831 * sd: the socket descriptor to have a new association added to.
2833 * nam: the address structure (either struct sockaddr_in or struct
2834 * sockaddr_in6 defined in RFC2553 [7]).
2836 * len: the size of the address.
2838 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2839 int addr_len)
2841 int err = 0;
2842 struct sctp_af *af;
2844 sctp_lock_sock(sk);
2846 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2847 __FUNCTION__, sk, addr, addr_len);
2849 /* Validate addr_len before calling common connect/connectx routine. */
2850 af = sctp_get_af_specific(addr->sa_family);
2851 if (!af || addr_len < af->sockaddr_len) {
2852 err = -EINVAL;
2853 } else {
2854 /* Pass correct addr len to common routine (so it knows there
2855 * is only one address being passed.
2857 err = __sctp_connect(sk, addr, af->sockaddr_len);
2860 sctp_release_sock(sk);
2861 return err;
2864 /* FIXME: Write comments. */
2865 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2867 return -EOPNOTSUPP; /* STUB */
2870 /* 4.1.4 accept() - TCP Style Syntax
2872 * Applications use accept() call to remove an established SCTP
2873 * association from the accept queue of the endpoint. A new socket
2874 * descriptor will be returned from accept() to represent the newly
2875 * formed association.
2877 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2879 struct sctp_sock *sp;
2880 struct sctp_endpoint *ep;
2881 struct sock *newsk = NULL;
2882 struct sctp_association *asoc;
2883 long timeo;
2884 int error = 0;
2886 sctp_lock_sock(sk);
2888 sp = sctp_sk(sk);
2889 ep = sp->ep;
2891 if (!sctp_style(sk, TCP)) {
2892 error = -EOPNOTSUPP;
2893 goto out;
2896 if (!sctp_sstate(sk, LISTENING)) {
2897 error = -EINVAL;
2898 goto out;
2901 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2903 error = sctp_wait_for_accept(sk, timeo);
2904 if (error)
2905 goto out;
2907 /* We treat the list of associations on the endpoint as the accept
2908 * queue and pick the first association on the list.
2910 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2912 newsk = sp->pf->create_accept_sk(sk, asoc);
2913 if (!newsk) {
2914 error = -ENOMEM;
2915 goto out;
2918 /* Populate the fields of the newsk from the oldsk and migrate the
2919 * asoc to the newsk.
2921 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2923 out:
2924 sctp_release_sock(sk);
2925 *err = error;
2926 return newsk;
2929 /* The SCTP ioctl handler. */
2930 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2932 return -ENOIOCTLCMD;
2935 /* This is the function which gets called during socket creation to
2936 * initialized the SCTP-specific portion of the sock.
2937 * The sock structure should already be zero-filled memory.
2939 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2941 struct sctp_endpoint *ep;
2942 struct sctp_sock *sp;
2944 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2946 sp = sctp_sk(sk);
2948 /* Initialize the SCTP per socket area. */
2949 switch (sk->sk_type) {
2950 case SOCK_SEQPACKET:
2951 sp->type = SCTP_SOCKET_UDP;
2952 break;
2953 case SOCK_STREAM:
2954 sp->type = SCTP_SOCKET_TCP;
2955 break;
2956 default:
2957 return -ESOCKTNOSUPPORT;
2960 /* Initialize default send parameters. These parameters can be
2961 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2963 sp->default_stream = 0;
2964 sp->default_ppid = 0;
2965 sp->default_flags = 0;
2966 sp->default_context = 0;
2967 sp->default_timetolive = 0;
2969 /* Initialize default setup parameters. These parameters
2970 * can be modified with the SCTP_INITMSG socket option or
2971 * overridden by the SCTP_INIT CMSG.
2973 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2974 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2975 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2976 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2978 /* Initialize default RTO related parameters. These parameters can
2979 * be modified for with the SCTP_RTOINFO socket option.
2981 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2982 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2983 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2985 /* Initialize default association related parameters. These parameters
2986 * can be modified with the SCTP_ASSOCINFO socket option.
2988 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2989 sp->assocparams.sasoc_number_peer_destinations = 0;
2990 sp->assocparams.sasoc_peer_rwnd = 0;
2991 sp->assocparams.sasoc_local_rwnd = 0;
2992 sp->assocparams.sasoc_cookie_life =
2993 jiffies_to_msecs(sctp_valid_cookie_life);
2995 /* Initialize default event subscriptions. By default, all the
2996 * options are off.
2998 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3000 /* Default Peer Address Parameters. These defaults can
3001 * be modified via SCTP_PEER_ADDR_PARAMS
3003 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
3004 sp->pathmaxrxt = sctp_max_retrans_path;
3005 sp->pathmtu = 0; // allow default discovery
3006 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
3007 sp->param_flags = SPP_HB_ENABLE |
3008 SPP_PMTUD_ENABLE |
3009 SPP_SACKDELAY_ENABLE;
3011 /* If enabled no SCTP message fragmentation will be performed.
3012 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3014 sp->disable_fragments = 0;
3016 /* Turn on/off any Nagle-like algorithm. */
3017 sp->nodelay = 1;
3019 /* Enable by default. */
3020 sp->v4mapped = 1;
3022 /* Auto-close idle associations after the configured
3023 * number of seconds. A value of 0 disables this
3024 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3025 * for UDP-style sockets only.
3027 sp->autoclose = 0;
3029 /* User specified fragmentation limit. */
3030 sp->user_frag = 0;
3032 sp->adaption_ind = 0;
3034 sp->pf = sctp_get_pf_specific(sk->sk_family);
3036 /* Control variables for partial data delivery. */
3037 sp->pd_mode = 0;
3038 skb_queue_head_init(&sp->pd_lobby);
3040 /* Create a per socket endpoint structure. Even if we
3041 * change the data structure relationships, this may still
3042 * be useful for storing pre-connect address information.
3044 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3045 if (!ep)
3046 return -ENOMEM;
3048 sp->ep = ep;
3049 sp->hmac = NULL;
3051 SCTP_DBG_OBJCNT_INC(sock);
3052 return 0;
3055 /* Cleanup any SCTP per socket resources. */
3056 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3058 struct sctp_endpoint *ep;
3060 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3062 /* Release our hold on the endpoint. */
3063 ep = sctp_sk(sk)->ep;
3064 sctp_endpoint_free(ep);
3066 return 0;
3069 /* API 4.1.7 shutdown() - TCP Style Syntax
3070 * int shutdown(int socket, int how);
3072 * sd - the socket descriptor of the association to be closed.
3073 * how - Specifies the type of shutdown. The values are
3074 * as follows:
3075 * SHUT_RD
3076 * Disables further receive operations. No SCTP
3077 * protocol action is taken.
3078 * SHUT_WR
3079 * Disables further send operations, and initiates
3080 * the SCTP shutdown sequence.
3081 * SHUT_RDWR
3082 * Disables further send and receive operations
3083 * and initiates the SCTP shutdown sequence.
3085 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3087 struct sctp_endpoint *ep;
3088 struct sctp_association *asoc;
3090 if (!sctp_style(sk, TCP))
3091 return;
3093 if (how & SEND_SHUTDOWN) {
3094 ep = sctp_sk(sk)->ep;
3095 if (!list_empty(&ep->asocs)) {
3096 asoc = list_entry(ep->asocs.next,
3097 struct sctp_association, asocs);
3098 sctp_primitive_SHUTDOWN(asoc, NULL);
3103 /* 7.2.1 Association Status (SCTP_STATUS)
3105 * Applications can retrieve current status information about an
3106 * association, including association state, peer receiver window size,
3107 * number of unacked data chunks, and number of data chunks pending
3108 * receipt. This information is read-only.
3110 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3111 char __user *optval,
3112 int __user *optlen)
3114 struct sctp_status status;
3115 struct sctp_association *asoc = NULL;
3116 struct sctp_transport *transport;
3117 sctp_assoc_t associd;
3118 int retval = 0;
3120 if (len != sizeof(status)) {
3121 retval = -EINVAL;
3122 goto out;
3125 if (copy_from_user(&status, optval, sizeof(status))) {
3126 retval = -EFAULT;
3127 goto out;
3130 associd = status.sstat_assoc_id;
3131 asoc = sctp_id2assoc(sk, associd);
3132 if (!asoc) {
3133 retval = -EINVAL;
3134 goto out;
3137 transport = asoc->peer.primary_path;
3139 status.sstat_assoc_id = sctp_assoc2id(asoc);
3140 status.sstat_state = asoc->state;
3141 status.sstat_rwnd = asoc->peer.rwnd;
3142 status.sstat_unackdata = asoc->unack_data;
3144 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3145 status.sstat_instrms = asoc->c.sinit_max_instreams;
3146 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3147 status.sstat_fragmentation_point = asoc->frag_point;
3148 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3149 memcpy(&status.sstat_primary.spinfo_address,
3150 &(transport->ipaddr), sizeof(union sctp_addr));
3151 /* Map ipv4 address into v4-mapped-on-v6 address. */
3152 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3153 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3154 status.sstat_primary.spinfo_state = transport->state;
3155 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3156 status.sstat_primary.spinfo_srtt = transport->srtt;
3157 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3158 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3160 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3161 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3163 if (put_user(len, optlen)) {
3164 retval = -EFAULT;
3165 goto out;
3168 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3169 len, status.sstat_state, status.sstat_rwnd,
3170 status.sstat_assoc_id);
3172 if (copy_to_user(optval, &status, len)) {
3173 retval = -EFAULT;
3174 goto out;
3177 out:
3178 return (retval);
3182 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3184 * Applications can retrieve information about a specific peer address
3185 * of an association, including its reachability state, congestion
3186 * window, and retransmission timer values. This information is
3187 * read-only.
3189 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3190 char __user *optval,
3191 int __user *optlen)
3193 struct sctp_paddrinfo pinfo;
3194 struct sctp_transport *transport;
3195 int retval = 0;
3197 if (len != sizeof(pinfo)) {
3198 retval = -EINVAL;
3199 goto out;
3202 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3203 retval = -EFAULT;
3204 goto out;
3207 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3208 pinfo.spinfo_assoc_id);
3209 if (!transport)
3210 return -EINVAL;
3212 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3213 pinfo.spinfo_state = transport->state;
3214 pinfo.spinfo_cwnd = transport->cwnd;
3215 pinfo.spinfo_srtt = transport->srtt;
3216 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3217 pinfo.spinfo_mtu = transport->pathmtu;
3219 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3220 pinfo.spinfo_state = SCTP_ACTIVE;
3222 if (put_user(len, optlen)) {
3223 retval = -EFAULT;
3224 goto out;
3227 if (copy_to_user(optval, &pinfo, len)) {
3228 retval = -EFAULT;
3229 goto out;
3232 out:
3233 return (retval);
3236 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3238 * This option is a on/off flag. If enabled no SCTP message
3239 * fragmentation will be performed. Instead if a message being sent
3240 * exceeds the current PMTU size, the message will NOT be sent and
3241 * instead a error will be indicated to the user.
3243 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3244 char __user *optval, int __user *optlen)
3246 int val;
3248 if (len < sizeof(int))
3249 return -EINVAL;
3251 len = sizeof(int);
3252 val = (sctp_sk(sk)->disable_fragments == 1);
3253 if (put_user(len, optlen))
3254 return -EFAULT;
3255 if (copy_to_user(optval, &val, len))
3256 return -EFAULT;
3257 return 0;
3260 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3262 * This socket option is used to specify various notifications and
3263 * ancillary data the user wishes to receive.
3265 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3266 int __user *optlen)
3268 if (len != sizeof(struct sctp_event_subscribe))
3269 return -EINVAL;
3270 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3271 return -EFAULT;
3272 return 0;
3275 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3277 * This socket option is applicable to the UDP-style socket only. When
3278 * set it will cause associations that are idle for more than the
3279 * specified number of seconds to automatically close. An association
3280 * being idle is defined an association that has NOT sent or received
3281 * user data. The special value of '0' indicates that no automatic
3282 * close of any associations should be performed. The option expects an
3283 * integer defining the number of seconds of idle time before an
3284 * association is closed.
3286 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3288 /* Applicable to UDP-style socket only */
3289 if (sctp_style(sk, TCP))
3290 return -EOPNOTSUPP;
3291 if (len != sizeof(int))
3292 return -EINVAL;
3293 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3294 return -EFAULT;
3295 return 0;
3298 /* Helper routine to branch off an association to a new socket. */
3299 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3300 struct socket **sockp)
3302 struct sock *sk = asoc->base.sk;
3303 struct socket *sock;
3304 int err = 0;
3306 /* An association cannot be branched off from an already peeled-off
3307 * socket, nor is this supported for tcp style sockets.
3309 if (!sctp_style(sk, UDP))
3310 return -EINVAL;
3312 /* Create a new socket. */
3313 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3314 if (err < 0)
3315 return err;
3317 /* Populate the fields of the newsk from the oldsk and migrate the
3318 * asoc to the newsk.
3320 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3321 *sockp = sock;
3323 return err;
3326 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3328 sctp_peeloff_arg_t peeloff;
3329 struct socket *newsock;
3330 int retval = 0;
3331 struct sctp_association *asoc;
3333 if (len != sizeof(sctp_peeloff_arg_t))
3334 return -EINVAL;
3335 if (copy_from_user(&peeloff, optval, len))
3336 return -EFAULT;
3338 asoc = sctp_id2assoc(sk, peeloff.associd);
3339 if (!asoc) {
3340 retval = -EINVAL;
3341 goto out;
3344 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3346 retval = sctp_do_peeloff(asoc, &newsock);
3347 if (retval < 0)
3348 goto out;
3350 /* Map the socket to an unused fd that can be returned to the user. */
3351 retval = sock_map_fd(newsock);
3352 if (retval < 0) {
3353 sock_release(newsock);
3354 goto out;
3357 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3358 __FUNCTION__, sk, asoc, newsock->sk, retval);
3360 /* Return the fd mapped to the new socket. */
3361 peeloff.sd = retval;
3362 if (copy_to_user(optval, &peeloff, len))
3363 retval = -EFAULT;
3365 out:
3366 return retval;
3369 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3371 * Applications can enable or disable heartbeats for any peer address of
3372 * an association, modify an address's heartbeat interval, force a
3373 * heartbeat to be sent immediately, and adjust the address's maximum
3374 * number of retransmissions sent before an address is considered
3375 * unreachable. The following structure is used to access and modify an
3376 * address's parameters:
3378 * struct sctp_paddrparams {
3379 * sctp_assoc_t spp_assoc_id;
3380 * struct sockaddr_storage spp_address;
3381 * uint32_t spp_hbinterval;
3382 * uint16_t spp_pathmaxrxt;
3383 * uint32_t spp_pathmtu;
3384 * uint32_t spp_sackdelay;
3385 * uint32_t spp_flags;
3386 * };
3388 * spp_assoc_id - (one-to-many style socket) This is filled in the
3389 * application, and identifies the association for
3390 * this query.
3391 * spp_address - This specifies which address is of interest.
3392 * spp_hbinterval - This contains the value of the heartbeat interval,
3393 * in milliseconds. If a value of zero
3394 * is present in this field then no changes are to
3395 * be made to this parameter.
3396 * spp_pathmaxrxt - This contains the maximum number of
3397 * retransmissions before this address shall be
3398 * considered unreachable. If a value of zero
3399 * is present in this field then no changes are to
3400 * be made to this parameter.
3401 * spp_pathmtu - When Path MTU discovery is disabled the value
3402 * specified here will be the "fixed" path mtu.
3403 * Note that if the spp_address field is empty
3404 * then all associations on this address will
3405 * have this fixed path mtu set upon them.
3407 * spp_sackdelay - When delayed sack is enabled, this value specifies
3408 * the number of milliseconds that sacks will be delayed
3409 * for. This value will apply to all addresses of an
3410 * association if the spp_address field is empty. Note
3411 * also, that if delayed sack is enabled and this
3412 * value is set to 0, no change is made to the last
3413 * recorded delayed sack timer value.
3415 * spp_flags - These flags are used to control various features
3416 * on an association. The flag field may contain
3417 * zero or more of the following options.
3419 * SPP_HB_ENABLE - Enable heartbeats on the
3420 * specified address. Note that if the address
3421 * field is empty all addresses for the association
3422 * have heartbeats enabled upon them.
3424 * SPP_HB_DISABLE - Disable heartbeats on the
3425 * speicifed address. Note that if the address
3426 * field is empty all addresses for the association
3427 * will have their heartbeats disabled. Note also
3428 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3429 * mutually exclusive, only one of these two should
3430 * be specified. Enabling both fields will have
3431 * undetermined results.
3433 * SPP_HB_DEMAND - Request a user initiated heartbeat
3434 * to be made immediately.
3436 * SPP_PMTUD_ENABLE - This field will enable PMTU
3437 * discovery upon the specified address. Note that
3438 * if the address feild is empty then all addresses
3439 * on the association are effected.
3441 * SPP_PMTUD_DISABLE - This field will disable PMTU
3442 * discovery upon the specified address. Note that
3443 * if the address feild is empty then all addresses
3444 * on the association are effected. Not also that
3445 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3446 * exclusive. Enabling both will have undetermined
3447 * results.
3449 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3450 * on delayed sack. The time specified in spp_sackdelay
3451 * is used to specify the sack delay for this address. Note
3452 * that if spp_address is empty then all addresses will
3453 * enable delayed sack and take on the sack delay
3454 * value specified in spp_sackdelay.
3455 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3456 * off delayed sack. If the spp_address field is blank then
3457 * delayed sack is disabled for the entire association. Note
3458 * also that this field is mutually exclusive to
3459 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3460 * results.
3462 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3463 char __user *optval, int __user *optlen)
3465 struct sctp_paddrparams params;
3466 struct sctp_transport *trans = NULL;
3467 struct sctp_association *asoc = NULL;
3468 struct sctp_sock *sp = sctp_sk(sk);
3470 if (len != sizeof(struct sctp_paddrparams))
3471 return -EINVAL;
3473 if (copy_from_user(&params, optval, len))
3474 return -EFAULT;
3476 /* If an address other than INADDR_ANY is specified, and
3477 * no transport is found, then the request is invalid.
3479 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3480 trans = sctp_addr_id2transport(sk, &params.spp_address,
3481 params.spp_assoc_id);
3482 if (!trans) {
3483 SCTP_DEBUG_PRINTK("Failed no transport\n");
3484 return -EINVAL;
3488 /* Get association, if assoc_id != 0 and the socket is a one
3489 * to many style socket, and an association was not found, then
3490 * the id was invalid.
3492 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3493 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3494 SCTP_DEBUG_PRINTK("Failed no association\n");
3495 return -EINVAL;
3498 if (trans) {
3499 /* Fetch transport values. */
3500 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3501 params.spp_pathmtu = trans->pathmtu;
3502 params.spp_pathmaxrxt = trans->pathmaxrxt;
3503 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3505 /*draft-11 doesn't say what to return in spp_flags*/
3506 params.spp_flags = trans->param_flags;
3507 } else if (asoc) {
3508 /* Fetch association values. */
3509 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3510 params.spp_pathmtu = asoc->pathmtu;
3511 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3512 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3514 /*draft-11 doesn't say what to return in spp_flags*/
3515 params.spp_flags = asoc->param_flags;
3516 } else {
3517 /* Fetch socket values. */
3518 params.spp_hbinterval = sp->hbinterval;
3519 params.spp_pathmtu = sp->pathmtu;
3520 params.spp_sackdelay = sp->sackdelay;
3521 params.spp_pathmaxrxt = sp->pathmaxrxt;
3523 /*draft-11 doesn't say what to return in spp_flags*/
3524 params.spp_flags = sp->param_flags;
3527 if (copy_to_user(optval, &params, len))
3528 return -EFAULT;
3530 if (put_user(len, optlen))
3531 return -EFAULT;
3533 return 0;
3536 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3538 * This options will get or set the delayed ack timer. The time is set
3539 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3540 * endpoints default delayed ack timer value. If the assoc_id field is
3541 * non-zero, then the set or get effects the specified association.
3543 * struct sctp_assoc_value {
3544 * sctp_assoc_t assoc_id;
3545 * uint32_t assoc_value;
3546 * };
3548 * assoc_id - This parameter, indicates which association the
3549 * user is preforming an action upon. Note that if
3550 * this field's value is zero then the endpoints
3551 * default value is changed (effecting future
3552 * associations only).
3554 * assoc_value - This parameter contains the number of milliseconds
3555 * that the user is requesting the delayed ACK timer
3556 * be set to. Note that this value is defined in
3557 * the standard to be between 200 and 500 milliseconds.
3559 * Note: a value of zero will leave the value alone,
3560 * but disable SACK delay. A non-zero value will also
3561 * enable SACK delay.
3563 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3564 char __user *optval,
3565 int __user *optlen)
3567 struct sctp_assoc_value params;
3568 struct sctp_association *asoc = NULL;
3569 struct sctp_sock *sp = sctp_sk(sk);
3571 if (len != sizeof(struct sctp_assoc_value))
3572 return - EINVAL;
3574 if (copy_from_user(&params, optval, len))
3575 return -EFAULT;
3577 /* Get association, if assoc_id != 0 and the socket is a one
3578 * to many style socket, and an association was not found, then
3579 * the id was invalid.
3581 asoc = sctp_id2assoc(sk, params.assoc_id);
3582 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3583 return -EINVAL;
3585 if (asoc) {
3586 /* Fetch association values. */
3587 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3588 params.assoc_value = jiffies_to_msecs(
3589 asoc->sackdelay);
3590 else
3591 params.assoc_value = 0;
3592 } else {
3593 /* Fetch socket values. */
3594 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3595 params.assoc_value = sp->sackdelay;
3596 else
3597 params.assoc_value = 0;
3600 if (copy_to_user(optval, &params, len))
3601 return -EFAULT;
3603 if (put_user(len, optlen))
3604 return -EFAULT;
3606 return 0;
3609 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3611 * Applications can specify protocol parameters for the default association
3612 * initialization. The option name argument to setsockopt() and getsockopt()
3613 * is SCTP_INITMSG.
3615 * Setting initialization parameters is effective only on an unconnected
3616 * socket (for UDP-style sockets only future associations are effected
3617 * by the change). With TCP-style sockets, this option is inherited by
3618 * sockets derived from a listener socket.
3620 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3622 if (len != sizeof(struct sctp_initmsg))
3623 return -EINVAL;
3624 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3625 return -EFAULT;
3626 return 0;
3629 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3630 char __user *optval,
3631 int __user *optlen)
3633 sctp_assoc_t id;
3634 struct sctp_association *asoc;
3635 struct list_head *pos;
3636 int cnt = 0;
3638 if (len != sizeof(sctp_assoc_t))
3639 return -EINVAL;
3641 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3642 return -EFAULT;
3644 /* For UDP-style sockets, id specifies the association to query. */
3645 asoc = sctp_id2assoc(sk, id);
3646 if (!asoc)
3647 return -EINVAL;
3649 list_for_each(pos, &asoc->peer.transport_addr_list) {
3650 cnt ++;
3653 return cnt;
3657 * Old API for getting list of peer addresses. Does not work for 32-bit
3658 * programs running on a 64-bit kernel
3660 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3661 char __user *optval,
3662 int __user *optlen)
3664 struct sctp_association *asoc;
3665 struct list_head *pos;
3666 int cnt = 0;
3667 struct sctp_getaddrs_old getaddrs;
3668 struct sctp_transport *from;
3669 void __user *to;
3670 union sctp_addr temp;
3671 struct sctp_sock *sp = sctp_sk(sk);
3672 int addrlen;
3674 if (len != sizeof(struct sctp_getaddrs_old))
3675 return -EINVAL;
3677 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3678 return -EFAULT;
3680 if (getaddrs.addr_num <= 0) return -EINVAL;
3682 /* For UDP-style sockets, id specifies the association to query. */
3683 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3684 if (!asoc)
3685 return -EINVAL;
3687 to = (void __user *)getaddrs.addrs;
3688 list_for_each(pos, &asoc->peer.transport_addr_list) {
3689 from = list_entry(pos, struct sctp_transport, transports);
3690 memcpy(&temp, &from->ipaddr, sizeof(temp));
3691 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3692 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3693 temp.v4.sin_port = htons(temp.v4.sin_port);
3694 if (copy_to_user(to, &temp, addrlen))
3695 return -EFAULT;
3696 to += addrlen ;
3697 cnt ++;
3698 if (cnt >= getaddrs.addr_num) break;
3700 getaddrs.addr_num = cnt;
3701 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3702 return -EFAULT;
3704 return 0;
3707 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3708 char __user *optval, int __user *optlen)
3710 struct sctp_association *asoc;
3711 struct list_head *pos;
3712 int cnt = 0;
3713 struct sctp_getaddrs getaddrs;
3714 struct sctp_transport *from;
3715 void __user *to;
3716 union sctp_addr temp;
3717 struct sctp_sock *sp = sctp_sk(sk);
3718 int addrlen;
3719 size_t space_left;
3720 int bytes_copied;
3722 if (len < sizeof(struct sctp_getaddrs))
3723 return -EINVAL;
3725 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3726 return -EFAULT;
3728 /* For UDP-style sockets, id specifies the association to query. */
3729 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3730 if (!asoc)
3731 return -EINVAL;
3733 to = optval + offsetof(struct sctp_getaddrs,addrs);
3734 space_left = len - sizeof(struct sctp_getaddrs) -
3735 offsetof(struct sctp_getaddrs,addrs);
3737 list_for_each(pos, &asoc->peer.transport_addr_list) {
3738 from = list_entry(pos, struct sctp_transport, transports);
3739 memcpy(&temp, &from->ipaddr, sizeof(temp));
3740 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3741 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3742 if(space_left < addrlen)
3743 return -ENOMEM;
3744 temp.v4.sin_port = htons(temp.v4.sin_port);
3745 if (copy_to_user(to, &temp, addrlen))
3746 return -EFAULT;
3747 to += addrlen;
3748 cnt++;
3749 space_left -= addrlen;
3752 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3753 return -EFAULT;
3754 bytes_copied = ((char __user *)to) - optval;
3755 if (put_user(bytes_copied, optlen))
3756 return -EFAULT;
3758 return 0;
3761 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3762 char __user *optval,
3763 int __user *optlen)
3765 sctp_assoc_t id;
3766 struct sctp_bind_addr *bp;
3767 struct sctp_association *asoc;
3768 struct list_head *pos;
3769 struct sctp_sockaddr_entry *addr;
3770 rwlock_t *addr_lock;
3771 unsigned long flags;
3772 int cnt = 0;
3774 if (len != sizeof(sctp_assoc_t))
3775 return -EINVAL;
3777 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3778 return -EFAULT;
3781 * For UDP-style sockets, id specifies the association to query.
3782 * If the id field is set to the value '0' then the locally bound
3783 * addresses are returned without regard to any particular
3784 * association.
3786 if (0 == id) {
3787 bp = &sctp_sk(sk)->ep->base.bind_addr;
3788 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3789 } else {
3790 asoc = sctp_id2assoc(sk, id);
3791 if (!asoc)
3792 return -EINVAL;
3793 bp = &asoc->base.bind_addr;
3794 addr_lock = &asoc->base.addr_lock;
3797 sctp_read_lock(addr_lock);
3799 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3800 * addresses from the global local address list.
3802 if (sctp_list_single_entry(&bp->address_list)) {
3803 addr = list_entry(bp->address_list.next,
3804 struct sctp_sockaddr_entry, list);
3805 if (sctp_is_any(&addr->a)) {
3806 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3807 list_for_each(pos, &sctp_local_addr_list) {
3808 addr = list_entry(pos,
3809 struct sctp_sockaddr_entry,
3810 list);
3811 if ((PF_INET == sk->sk_family) &&
3812 (AF_INET6 == addr->a.sa.sa_family))
3813 continue;
3814 cnt++;
3816 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3817 flags);
3818 } else {
3819 cnt = 1;
3821 goto done;
3824 list_for_each(pos, &bp->address_list) {
3825 cnt ++;
3828 done:
3829 sctp_read_unlock(addr_lock);
3830 return cnt;
3833 /* Helper function that copies local addresses to user and returns the number
3834 * of addresses copied.
3836 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3837 void __user *to)
3839 struct list_head *pos;
3840 struct sctp_sockaddr_entry *addr;
3841 unsigned long flags;
3842 union sctp_addr temp;
3843 int cnt = 0;
3844 int addrlen;
3846 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3847 list_for_each(pos, &sctp_local_addr_list) {
3848 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3849 if ((PF_INET == sk->sk_family) &&
3850 (AF_INET6 == addr->a.sa.sa_family))
3851 continue;
3852 memcpy(&temp, &addr->a, sizeof(temp));
3853 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3854 &temp);
3855 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3856 temp.v4.sin_port = htons(port);
3857 if (copy_to_user(to, &temp, addrlen)) {
3858 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3859 flags);
3860 return -EFAULT;
3862 to += addrlen;
3863 cnt ++;
3864 if (cnt >= max_addrs) break;
3866 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3868 return cnt;
3871 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3872 void __user **to, size_t space_left)
3874 struct list_head *pos;
3875 struct sctp_sockaddr_entry *addr;
3876 unsigned long flags;
3877 union sctp_addr temp;
3878 int cnt = 0;
3879 int addrlen;
3881 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3882 list_for_each(pos, &sctp_local_addr_list) {
3883 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3884 if ((PF_INET == sk->sk_family) &&
3885 (AF_INET6 == addr->a.sa.sa_family))
3886 continue;
3887 memcpy(&temp, &addr->a, sizeof(temp));
3888 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3889 &temp);
3890 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3891 if(space_left<addrlen)
3892 return -ENOMEM;
3893 temp.v4.sin_port = htons(port);
3894 if (copy_to_user(*to, &temp, addrlen)) {
3895 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3896 flags);
3897 return -EFAULT;
3899 *to += addrlen;
3900 cnt ++;
3901 space_left -= addrlen;
3903 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3905 return cnt;
3908 /* Old API for getting list of local addresses. Does not work for 32-bit
3909 * programs running on a 64-bit kernel
3911 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3912 char __user *optval, int __user *optlen)
3914 struct sctp_bind_addr *bp;
3915 struct sctp_association *asoc;
3916 struct list_head *pos;
3917 int cnt = 0;
3918 struct sctp_getaddrs_old getaddrs;
3919 struct sctp_sockaddr_entry *addr;
3920 void __user *to;
3921 union sctp_addr temp;
3922 struct sctp_sock *sp = sctp_sk(sk);
3923 int addrlen;
3924 rwlock_t *addr_lock;
3925 int err = 0;
3927 if (len != sizeof(struct sctp_getaddrs_old))
3928 return -EINVAL;
3930 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3931 return -EFAULT;
3933 if (getaddrs.addr_num <= 0) return -EINVAL;
3935 * For UDP-style sockets, id specifies the association to query.
3936 * If the id field is set to the value '0' then the locally bound
3937 * addresses are returned without regard to any particular
3938 * association.
3940 if (0 == getaddrs.assoc_id) {
3941 bp = &sctp_sk(sk)->ep->base.bind_addr;
3942 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3943 } else {
3944 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3945 if (!asoc)
3946 return -EINVAL;
3947 bp = &asoc->base.bind_addr;
3948 addr_lock = &asoc->base.addr_lock;
3951 to = getaddrs.addrs;
3953 sctp_read_lock(addr_lock);
3955 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3956 * addresses from the global local address list.
3958 if (sctp_list_single_entry(&bp->address_list)) {
3959 addr = list_entry(bp->address_list.next,
3960 struct sctp_sockaddr_entry, list);
3961 if (sctp_is_any(&addr->a)) {
3962 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3963 getaddrs.addr_num,
3964 to);
3965 if (cnt < 0) {
3966 err = cnt;
3967 goto unlock;
3969 goto copy_getaddrs;
3973 list_for_each(pos, &bp->address_list) {
3974 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3975 memcpy(&temp, &addr->a, sizeof(temp));
3976 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3977 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3978 temp.v4.sin_port = htons(temp.v4.sin_port);
3979 if (copy_to_user(to, &temp, addrlen)) {
3980 err = -EFAULT;
3981 goto unlock;
3983 to += addrlen;
3984 cnt ++;
3985 if (cnt >= getaddrs.addr_num) break;
3988 copy_getaddrs:
3989 getaddrs.addr_num = cnt;
3990 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3991 err = -EFAULT;
3993 unlock:
3994 sctp_read_unlock(addr_lock);
3995 return err;
3998 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3999 char __user *optval, int __user *optlen)
4001 struct sctp_bind_addr *bp;
4002 struct sctp_association *asoc;
4003 struct list_head *pos;
4004 int cnt = 0;
4005 struct sctp_getaddrs getaddrs;
4006 struct sctp_sockaddr_entry *addr;
4007 void __user *to;
4008 union sctp_addr temp;
4009 struct sctp_sock *sp = sctp_sk(sk);
4010 int addrlen;
4011 rwlock_t *addr_lock;
4012 int err = 0;
4013 size_t space_left;
4014 int bytes_copied;
4016 if (len <= sizeof(struct sctp_getaddrs))
4017 return -EINVAL;
4019 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4020 return -EFAULT;
4023 * For UDP-style sockets, id specifies the association to query.
4024 * If the id field is set to the value '0' then the locally bound
4025 * addresses are returned without regard to any particular
4026 * association.
4028 if (0 == getaddrs.assoc_id) {
4029 bp = &sctp_sk(sk)->ep->base.bind_addr;
4030 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4031 } else {
4032 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4033 if (!asoc)
4034 return -EINVAL;
4035 bp = &asoc->base.bind_addr;
4036 addr_lock = &asoc->base.addr_lock;
4039 to = optval + offsetof(struct sctp_getaddrs,addrs);
4040 space_left = len - sizeof(struct sctp_getaddrs) -
4041 offsetof(struct sctp_getaddrs,addrs);
4043 sctp_read_lock(addr_lock);
4045 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4046 * addresses from the global local address list.
4048 if (sctp_list_single_entry(&bp->address_list)) {
4049 addr = list_entry(bp->address_list.next,
4050 struct sctp_sockaddr_entry, list);
4051 if (sctp_is_any(&addr->a)) {
4052 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4053 &to, space_left);
4054 if (cnt < 0) {
4055 err = cnt;
4056 goto unlock;
4058 goto copy_getaddrs;
4062 list_for_each(pos, &bp->address_list) {
4063 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4064 memcpy(&temp, &addr->a, sizeof(temp));
4065 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4066 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4067 if(space_left < addrlen)
4068 return -ENOMEM; /*fixme: right error?*/
4069 temp.v4.sin_port = htons(temp.v4.sin_port);
4070 if (copy_to_user(to, &temp, addrlen)) {
4071 err = -EFAULT;
4072 goto unlock;
4074 to += addrlen;
4075 cnt ++;
4076 space_left -= addrlen;
4079 copy_getaddrs:
4080 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4081 return -EFAULT;
4082 bytes_copied = ((char __user *)to) - optval;
4083 if (put_user(bytes_copied, optlen))
4084 return -EFAULT;
4086 unlock:
4087 sctp_read_unlock(addr_lock);
4088 return err;
4091 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4093 * Requests that the local SCTP stack use the enclosed peer address as
4094 * the association primary. The enclosed address must be one of the
4095 * association peer's addresses.
4097 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4098 char __user *optval, int __user *optlen)
4100 struct sctp_prim prim;
4101 struct sctp_association *asoc;
4102 struct sctp_sock *sp = sctp_sk(sk);
4104 if (len != sizeof(struct sctp_prim))
4105 return -EINVAL;
4107 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4108 return -EFAULT;
4110 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4111 if (!asoc)
4112 return -EINVAL;
4114 if (!asoc->peer.primary_path)
4115 return -ENOTCONN;
4117 asoc->peer.primary_path->ipaddr.v4.sin_port =
4118 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4119 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4120 sizeof(union sctp_addr));
4121 asoc->peer.primary_path->ipaddr.v4.sin_port =
4122 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4124 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4125 (union sctp_addr *)&prim.ssp_addr);
4127 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4128 return -EFAULT;
4130 return 0;
4134 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4136 * Requests that the local endpoint set the specified Adaption Layer
4137 * Indication parameter for all future INIT and INIT-ACK exchanges.
4139 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4140 char __user *optval, int __user *optlen)
4142 struct sctp_setadaption adaption;
4144 if (len != sizeof(struct sctp_setadaption))
4145 return -EINVAL;
4147 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4148 if (copy_to_user(optval, &adaption, len))
4149 return -EFAULT;
4151 return 0;
4156 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4158 * Applications that wish to use the sendto() system call may wish to
4159 * specify a default set of parameters that would normally be supplied
4160 * through the inclusion of ancillary data. This socket option allows
4161 * such an application to set the default sctp_sndrcvinfo structure.
4164 * The application that wishes to use this socket option simply passes
4165 * in to this call the sctp_sndrcvinfo structure defined in Section
4166 * 5.2.2) The input parameters accepted by this call include
4167 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4168 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4169 * to this call if the caller is using the UDP model.
4171 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4173 static int sctp_getsockopt_default_send_param(struct sock *sk,
4174 int len, char __user *optval,
4175 int __user *optlen)
4177 struct sctp_sndrcvinfo info;
4178 struct sctp_association *asoc;
4179 struct sctp_sock *sp = sctp_sk(sk);
4181 if (len != sizeof(struct sctp_sndrcvinfo))
4182 return -EINVAL;
4183 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4184 return -EFAULT;
4186 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4187 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4188 return -EINVAL;
4190 if (asoc) {
4191 info.sinfo_stream = asoc->default_stream;
4192 info.sinfo_flags = asoc->default_flags;
4193 info.sinfo_ppid = asoc->default_ppid;
4194 info.sinfo_context = asoc->default_context;
4195 info.sinfo_timetolive = asoc->default_timetolive;
4196 } else {
4197 info.sinfo_stream = sp->default_stream;
4198 info.sinfo_flags = sp->default_flags;
4199 info.sinfo_ppid = sp->default_ppid;
4200 info.sinfo_context = sp->default_context;
4201 info.sinfo_timetolive = sp->default_timetolive;
4204 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4205 return -EFAULT;
4207 return 0;
4212 * 7.1.5 SCTP_NODELAY
4214 * Turn on/off any Nagle-like algorithm. This means that packets are
4215 * generally sent as soon as possible and no unnecessary delays are
4216 * introduced, at the cost of more packets in the network. Expects an
4217 * integer boolean flag.
4220 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4221 char __user *optval, int __user *optlen)
4223 int val;
4225 if (len < sizeof(int))
4226 return -EINVAL;
4228 len = sizeof(int);
4229 val = (sctp_sk(sk)->nodelay == 1);
4230 if (put_user(len, optlen))
4231 return -EFAULT;
4232 if (copy_to_user(optval, &val, len))
4233 return -EFAULT;
4234 return 0;
4239 * 7.1.1 SCTP_RTOINFO
4241 * The protocol parameters used to initialize and bound retransmission
4242 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4243 * and modify these parameters.
4244 * All parameters are time values, in milliseconds. A value of 0, when
4245 * modifying the parameters, indicates that the current value should not
4246 * be changed.
4249 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4250 char __user *optval,
4251 int __user *optlen) {
4252 struct sctp_rtoinfo rtoinfo;
4253 struct sctp_association *asoc;
4255 if (len != sizeof (struct sctp_rtoinfo))
4256 return -EINVAL;
4258 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4259 return -EFAULT;
4261 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4263 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4264 return -EINVAL;
4266 /* Values corresponding to the specific association. */
4267 if (asoc) {
4268 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4269 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4270 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4271 } else {
4272 /* Values corresponding to the endpoint. */
4273 struct sctp_sock *sp = sctp_sk(sk);
4275 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4276 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4277 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4280 if (put_user(len, optlen))
4281 return -EFAULT;
4283 if (copy_to_user(optval, &rtoinfo, len))
4284 return -EFAULT;
4286 return 0;
4291 * 7.1.2 SCTP_ASSOCINFO
4293 * This option is used to tune the the maximum retransmission attempts
4294 * of the association.
4295 * Returns an error if the new association retransmission value is
4296 * greater than the sum of the retransmission value of the peer.
4297 * See [SCTP] for more information.
4300 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4301 char __user *optval,
4302 int __user *optlen)
4305 struct sctp_assocparams assocparams;
4306 struct sctp_association *asoc;
4307 struct list_head *pos;
4308 int cnt = 0;
4310 if (len != sizeof (struct sctp_assocparams))
4311 return -EINVAL;
4313 if (copy_from_user(&assocparams, optval,
4314 sizeof (struct sctp_assocparams)))
4315 return -EFAULT;
4317 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4319 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4320 return -EINVAL;
4322 /* Values correspoinding to the specific association */
4323 if (asoc) {
4324 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4325 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4326 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4327 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4328 * 1000) +
4329 (asoc->cookie_life.tv_usec
4330 / 1000);
4332 list_for_each(pos, &asoc->peer.transport_addr_list) {
4333 cnt ++;
4336 assocparams.sasoc_number_peer_destinations = cnt;
4337 } else {
4338 /* Values corresponding to the endpoint */
4339 struct sctp_sock *sp = sctp_sk(sk);
4341 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4342 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4343 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4344 assocparams.sasoc_cookie_life =
4345 sp->assocparams.sasoc_cookie_life;
4346 assocparams.sasoc_number_peer_destinations =
4347 sp->assocparams.
4348 sasoc_number_peer_destinations;
4351 if (put_user(len, optlen))
4352 return -EFAULT;
4354 if (copy_to_user(optval, &assocparams, len))
4355 return -EFAULT;
4357 return 0;
4361 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4363 * This socket option is a boolean flag which turns on or off mapped V4
4364 * addresses. If this option is turned on and the socket is type
4365 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4366 * If this option is turned off, then no mapping will be done of V4
4367 * addresses and a user will receive both PF_INET6 and PF_INET type
4368 * addresses on the socket.
4370 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4371 char __user *optval, int __user *optlen)
4373 int val;
4374 struct sctp_sock *sp = sctp_sk(sk);
4376 if (len < sizeof(int))
4377 return -EINVAL;
4379 len = sizeof(int);
4380 val = sp->v4mapped;
4381 if (put_user(len, optlen))
4382 return -EFAULT;
4383 if (copy_to_user(optval, &val, len))
4384 return -EFAULT;
4386 return 0;
4390 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4392 * This socket option specifies the maximum size to put in any outgoing
4393 * SCTP chunk. If a message is larger than this size it will be
4394 * fragmented by SCTP into the specified size. Note that the underlying
4395 * SCTP implementation may fragment into smaller sized chunks when the
4396 * PMTU of the underlying association is smaller than the value set by
4397 * the user.
4399 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4400 char __user *optval, int __user *optlen)
4402 int val;
4404 if (len < sizeof(int))
4405 return -EINVAL;
4407 len = sizeof(int);
4409 val = sctp_sk(sk)->user_frag;
4410 if (put_user(len, optlen))
4411 return -EFAULT;
4412 if (copy_to_user(optval, &val, len))
4413 return -EFAULT;
4415 return 0;
4418 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4419 char __user *optval, int __user *optlen)
4421 int retval = 0;
4422 int len;
4424 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4425 sk, optname);
4427 /* I can hardly begin to describe how wrong this is. This is
4428 * so broken as to be worse than useless. The API draft
4429 * REALLY is NOT helpful here... I am not convinced that the
4430 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4431 * are at all well-founded.
4433 if (level != SOL_SCTP) {
4434 struct sctp_af *af = sctp_sk(sk)->pf->af;
4436 retval = af->getsockopt(sk, level, optname, optval, optlen);
4437 return retval;
4440 if (get_user(len, optlen))
4441 return -EFAULT;
4443 sctp_lock_sock(sk);
4445 switch (optname) {
4446 case SCTP_STATUS:
4447 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4448 break;
4449 case SCTP_DISABLE_FRAGMENTS:
4450 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4451 optlen);
4452 break;
4453 case SCTP_EVENTS:
4454 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4455 break;
4456 case SCTP_AUTOCLOSE:
4457 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4458 break;
4459 case SCTP_SOCKOPT_PEELOFF:
4460 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4461 break;
4462 case SCTP_PEER_ADDR_PARAMS:
4463 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4464 optlen);
4465 break;
4466 case SCTP_DELAYED_ACK_TIME:
4467 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4468 optlen);
4469 break;
4470 case SCTP_INITMSG:
4471 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4472 break;
4473 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4474 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4475 optlen);
4476 break;
4477 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4478 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4479 optlen);
4480 break;
4481 case SCTP_GET_PEER_ADDRS_OLD:
4482 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4483 optlen);
4484 break;
4485 case SCTP_GET_LOCAL_ADDRS_OLD:
4486 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4487 optlen);
4488 break;
4489 case SCTP_GET_PEER_ADDRS:
4490 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4491 optlen);
4492 break;
4493 case SCTP_GET_LOCAL_ADDRS:
4494 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4495 optlen);
4496 break;
4497 case SCTP_DEFAULT_SEND_PARAM:
4498 retval = sctp_getsockopt_default_send_param(sk, len,
4499 optval, optlen);
4500 break;
4501 case SCTP_PRIMARY_ADDR:
4502 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4503 break;
4504 case SCTP_NODELAY:
4505 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4506 break;
4507 case SCTP_RTOINFO:
4508 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4509 break;
4510 case SCTP_ASSOCINFO:
4511 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4512 break;
4513 case SCTP_I_WANT_MAPPED_V4_ADDR:
4514 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4515 break;
4516 case SCTP_MAXSEG:
4517 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4518 break;
4519 case SCTP_GET_PEER_ADDR_INFO:
4520 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4521 optlen);
4522 break;
4523 case SCTP_ADAPTION_LAYER:
4524 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4525 optlen);
4526 break;
4527 default:
4528 retval = -ENOPROTOOPT;
4529 break;
4532 sctp_release_sock(sk);
4533 return retval;
4536 static void sctp_hash(struct sock *sk)
4538 /* STUB */
4541 static void sctp_unhash(struct sock *sk)
4543 /* STUB */
4546 /* Check if port is acceptable. Possibly find first available port.
4548 * The port hash table (contained in the 'global' SCTP protocol storage
4549 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4550 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4551 * list (the list number is the port number hashed out, so as you
4552 * would expect from a hash function, all the ports in a given list have
4553 * such a number that hashes out to the same list number; you were
4554 * expecting that, right?); so each list has a set of ports, with a
4555 * link to the socket (struct sock) that uses it, the port number and
4556 * a fastreuse flag (FIXME: NPI ipg).
4558 static struct sctp_bind_bucket *sctp_bucket_create(
4559 struct sctp_bind_hashbucket *head, unsigned short snum);
4561 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4563 struct sctp_bind_hashbucket *head; /* hash list */
4564 struct sctp_bind_bucket *pp; /* hash list port iterator */
4565 unsigned short snum;
4566 int ret;
4568 /* NOTE: Remember to put this back to net order. */
4569 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4570 snum = addr->v4.sin_port;
4572 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4573 sctp_local_bh_disable();
4575 if (snum == 0) {
4576 /* Search for an available port.
4578 * 'sctp_port_rover' was the last port assigned, so
4579 * we start to search from 'sctp_port_rover +
4580 * 1'. What we do is first check if port 'rover' is
4581 * already in the hash table; if not, we use that; if
4582 * it is, we try next.
4584 int low = sysctl_local_port_range[0];
4585 int high = sysctl_local_port_range[1];
4586 int remaining = (high - low) + 1;
4587 int rover;
4588 int index;
4590 sctp_spin_lock(&sctp_port_alloc_lock);
4591 rover = sctp_port_rover;
4592 do {
4593 rover++;
4594 if ((rover < low) || (rover > high))
4595 rover = low;
4596 index = sctp_phashfn(rover);
4597 head = &sctp_port_hashtable[index];
4598 sctp_spin_lock(&head->lock);
4599 for (pp = head->chain; pp; pp = pp->next)
4600 if (pp->port == rover)
4601 goto next;
4602 break;
4603 next:
4604 sctp_spin_unlock(&head->lock);
4605 } while (--remaining > 0);
4606 sctp_port_rover = rover;
4607 sctp_spin_unlock(&sctp_port_alloc_lock);
4609 /* Exhausted local port range during search? */
4610 ret = 1;
4611 if (remaining <= 0)
4612 goto fail;
4614 /* OK, here is the one we will use. HEAD (the port
4615 * hash table list entry) is non-NULL and we hold it's
4616 * mutex.
4618 snum = rover;
4619 } else {
4620 /* We are given an specific port number; we verify
4621 * that it is not being used. If it is used, we will
4622 * exahust the search in the hash list corresponding
4623 * to the port number (snum) - we detect that with the
4624 * port iterator, pp being NULL.
4626 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4627 sctp_spin_lock(&head->lock);
4628 for (pp = head->chain; pp; pp = pp->next) {
4629 if (pp->port == snum)
4630 goto pp_found;
4633 pp = NULL;
4634 goto pp_not_found;
4635 pp_found:
4636 if (!hlist_empty(&pp->owner)) {
4637 /* We had a port hash table hit - there is an
4638 * available port (pp != NULL) and it is being
4639 * used by other socket (pp->owner not empty); that other
4640 * socket is going to be sk2.
4642 int reuse = sk->sk_reuse;
4643 struct sock *sk2;
4644 struct hlist_node *node;
4646 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4647 if (pp->fastreuse && sk->sk_reuse)
4648 goto success;
4650 /* Run through the list of sockets bound to the port
4651 * (pp->port) [via the pointers bind_next and
4652 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4653 * we get the endpoint they describe and run through
4654 * the endpoint's list of IP (v4 or v6) addresses,
4655 * comparing each of the addresses with the address of
4656 * the socket sk. If we find a match, then that means
4657 * that this port/socket (sk) combination are already
4658 * in an endpoint.
4660 sk_for_each_bound(sk2, node, &pp->owner) {
4661 struct sctp_endpoint *ep2;
4662 ep2 = sctp_sk(sk2)->ep;
4664 if (reuse && sk2->sk_reuse)
4665 continue;
4667 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4668 sctp_sk(sk))) {
4669 ret = (long)sk2;
4670 goto fail_unlock;
4673 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4675 pp_not_found:
4676 /* If there was a hash table miss, create a new port. */
4677 ret = 1;
4678 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4679 goto fail_unlock;
4681 /* In either case (hit or miss), make sure fastreuse is 1 only
4682 * if sk->sk_reuse is too (that is, if the caller requested
4683 * SO_REUSEADDR on this socket -sk-).
4685 if (hlist_empty(&pp->owner))
4686 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4687 else if (pp->fastreuse && !sk->sk_reuse)
4688 pp->fastreuse = 0;
4690 /* We are set, so fill up all the data in the hash table
4691 * entry, tie the socket list information with the rest of the
4692 * sockets FIXME: Blurry, NPI (ipg).
4694 success:
4695 inet_sk(sk)->num = snum;
4696 if (!sctp_sk(sk)->bind_hash) {
4697 sk_add_bind_node(sk, &pp->owner);
4698 sctp_sk(sk)->bind_hash = pp;
4700 ret = 0;
4702 fail_unlock:
4703 sctp_spin_unlock(&head->lock);
4705 fail:
4706 sctp_local_bh_enable();
4707 addr->v4.sin_port = htons(addr->v4.sin_port);
4708 return ret;
4711 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4712 * port is requested.
4714 static int sctp_get_port(struct sock *sk, unsigned short snum)
4716 long ret;
4717 union sctp_addr addr;
4718 struct sctp_af *af = sctp_sk(sk)->pf->af;
4720 /* Set up a dummy address struct from the sk. */
4721 af->from_sk(&addr, sk);
4722 addr.v4.sin_port = htons(snum);
4724 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4725 ret = sctp_get_port_local(sk, &addr);
4727 return (ret ? 1 : 0);
4731 * 3.1.3 listen() - UDP Style Syntax
4733 * By default, new associations are not accepted for UDP style sockets.
4734 * An application uses listen() to mark a socket as being able to
4735 * accept new associations.
4737 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4739 struct sctp_sock *sp = sctp_sk(sk);
4740 struct sctp_endpoint *ep = sp->ep;
4742 /* Only UDP style sockets that are not peeled off are allowed to
4743 * listen().
4745 if (!sctp_style(sk, UDP))
4746 return -EINVAL;
4748 /* If backlog is zero, disable listening. */
4749 if (!backlog) {
4750 if (sctp_sstate(sk, CLOSED))
4751 return 0;
4753 sctp_unhash_endpoint(ep);
4754 sk->sk_state = SCTP_SS_CLOSED;
4757 /* Return if we are already listening. */
4758 if (sctp_sstate(sk, LISTENING))
4759 return 0;
4762 * If a bind() or sctp_bindx() is not called prior to a listen()
4763 * call that allows new associations to be accepted, the system
4764 * picks an ephemeral port and will choose an address set equivalent
4765 * to binding with a wildcard address.
4767 * This is not currently spelled out in the SCTP sockets
4768 * extensions draft, but follows the practice as seen in TCP
4769 * sockets.
4771 if (!ep->base.bind_addr.port) {
4772 if (sctp_autobind(sk))
4773 return -EAGAIN;
4775 sk->sk_state = SCTP_SS_LISTENING;
4776 sctp_hash_endpoint(ep);
4777 return 0;
4781 * 4.1.3 listen() - TCP Style Syntax
4783 * Applications uses listen() to ready the SCTP endpoint for accepting
4784 * inbound associations.
4786 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4788 struct sctp_sock *sp = sctp_sk(sk);
4789 struct sctp_endpoint *ep = sp->ep;
4791 /* If backlog is zero, disable listening. */
4792 if (!backlog) {
4793 if (sctp_sstate(sk, CLOSED))
4794 return 0;
4796 sctp_unhash_endpoint(ep);
4797 sk->sk_state = SCTP_SS_CLOSED;
4800 if (sctp_sstate(sk, LISTENING))
4801 return 0;
4804 * If a bind() or sctp_bindx() is not called prior to a listen()
4805 * call that allows new associations to be accepted, the system
4806 * picks an ephemeral port and will choose an address set equivalent
4807 * to binding with a wildcard address.
4809 * This is not currently spelled out in the SCTP sockets
4810 * extensions draft, but follows the practice as seen in TCP
4811 * sockets.
4813 if (!ep->base.bind_addr.port) {
4814 if (sctp_autobind(sk))
4815 return -EAGAIN;
4817 sk->sk_state = SCTP_SS_LISTENING;
4818 sk->sk_max_ack_backlog = backlog;
4819 sctp_hash_endpoint(ep);
4820 return 0;
4824 * Move a socket to LISTENING state.
4826 int sctp_inet_listen(struct socket *sock, int backlog)
4828 struct sock *sk = sock->sk;
4829 struct crypto_tfm *tfm=NULL;
4830 int err = -EINVAL;
4832 if (unlikely(backlog < 0))
4833 goto out;
4835 sctp_lock_sock(sk);
4837 if (sock->state != SS_UNCONNECTED)
4838 goto out;
4840 /* Allocate HMAC for generating cookie. */
4841 if (sctp_hmac_alg) {
4842 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4843 if (!tfm) {
4844 err = -ENOSYS;
4845 goto out;
4849 switch (sock->type) {
4850 case SOCK_SEQPACKET:
4851 err = sctp_seqpacket_listen(sk, backlog);
4852 break;
4853 case SOCK_STREAM:
4854 err = sctp_stream_listen(sk, backlog);
4855 break;
4856 default:
4857 break;
4859 if (err)
4860 goto cleanup;
4862 /* Store away the transform reference. */
4863 sctp_sk(sk)->hmac = tfm;
4864 out:
4865 sctp_release_sock(sk);
4866 return err;
4867 cleanup:
4868 sctp_crypto_free_tfm(tfm);
4869 goto out;
4873 * This function is done by modeling the current datagram_poll() and the
4874 * tcp_poll(). Note that, based on these implementations, we don't
4875 * lock the socket in this function, even though it seems that,
4876 * ideally, locking or some other mechanisms can be used to ensure
4877 * the integrity of the counters (sndbuf and wmem_alloc) used
4878 * in this place. We assume that we don't need locks either until proven
4879 * otherwise.
4881 * Another thing to note is that we include the Async I/O support
4882 * here, again, by modeling the current TCP/UDP code. We don't have
4883 * a good way to test with it yet.
4885 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4887 struct sock *sk = sock->sk;
4888 struct sctp_sock *sp = sctp_sk(sk);
4889 unsigned int mask;
4891 poll_wait(file, sk->sk_sleep, wait);
4893 /* A TCP-style listening socket becomes readable when the accept queue
4894 * is not empty.
4896 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4897 return (!list_empty(&sp->ep->asocs)) ?
4898 (POLLIN | POLLRDNORM) : 0;
4900 mask = 0;
4902 /* Is there any exceptional events? */
4903 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4904 mask |= POLLERR;
4905 if (sk->sk_shutdown == SHUTDOWN_MASK)
4906 mask |= POLLHUP;
4908 /* Is it readable? Reconsider this code with TCP-style support. */
4909 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4910 (sk->sk_shutdown & RCV_SHUTDOWN))
4911 mask |= POLLIN | POLLRDNORM;
4913 /* The association is either gone or not ready. */
4914 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4915 return mask;
4917 /* Is it writable? */
4918 if (sctp_writeable(sk)) {
4919 mask |= POLLOUT | POLLWRNORM;
4920 } else {
4921 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4923 * Since the socket is not locked, the buffer
4924 * might be made available after the writeable check and
4925 * before the bit is set. This could cause a lost I/O
4926 * signal. tcp_poll() has a race breaker for this race
4927 * condition. Based on their implementation, we put
4928 * in the following code to cover it as well.
4930 if (sctp_writeable(sk))
4931 mask |= POLLOUT | POLLWRNORM;
4933 return mask;
4936 /********************************************************************
4937 * 2nd Level Abstractions
4938 ********************************************************************/
4940 static struct sctp_bind_bucket *sctp_bucket_create(
4941 struct sctp_bind_hashbucket *head, unsigned short snum)
4943 struct sctp_bind_bucket *pp;
4945 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4946 SCTP_DBG_OBJCNT_INC(bind_bucket);
4947 if (pp) {
4948 pp->port = snum;
4949 pp->fastreuse = 0;
4950 INIT_HLIST_HEAD(&pp->owner);
4951 if ((pp->next = head->chain) != NULL)
4952 pp->next->pprev = &pp->next;
4953 head->chain = pp;
4954 pp->pprev = &head->chain;
4956 return pp;
4959 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4960 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4962 if (hlist_empty(&pp->owner)) {
4963 if (pp->next)
4964 pp->next->pprev = pp->pprev;
4965 *(pp->pprev) = pp->next;
4966 kmem_cache_free(sctp_bucket_cachep, pp);
4967 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4971 /* Release this socket's reference to a local port. */
4972 static inline void __sctp_put_port(struct sock *sk)
4974 struct sctp_bind_hashbucket *head =
4975 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4976 struct sctp_bind_bucket *pp;
4978 sctp_spin_lock(&head->lock);
4979 pp = sctp_sk(sk)->bind_hash;
4980 __sk_del_bind_node(sk);
4981 sctp_sk(sk)->bind_hash = NULL;
4982 inet_sk(sk)->num = 0;
4983 sctp_bucket_destroy(pp);
4984 sctp_spin_unlock(&head->lock);
4987 void sctp_put_port(struct sock *sk)
4989 sctp_local_bh_disable();
4990 __sctp_put_port(sk);
4991 sctp_local_bh_enable();
4995 * The system picks an ephemeral port and choose an address set equivalent
4996 * to binding with a wildcard address.
4997 * One of those addresses will be the primary address for the association.
4998 * This automatically enables the multihoming capability of SCTP.
5000 static int sctp_autobind(struct sock *sk)
5002 union sctp_addr autoaddr;
5003 struct sctp_af *af;
5004 unsigned short port;
5006 /* Initialize a local sockaddr structure to INADDR_ANY. */
5007 af = sctp_sk(sk)->pf->af;
5009 port = htons(inet_sk(sk)->num);
5010 af->inaddr_any(&autoaddr, port);
5012 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5015 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5017 * From RFC 2292
5018 * 4.2 The cmsghdr Structure *
5020 * When ancillary data is sent or received, any number of ancillary data
5021 * objects can be specified by the msg_control and msg_controllen members of
5022 * the msghdr structure, because each object is preceded by
5023 * a cmsghdr structure defining the object's length (the cmsg_len member).
5024 * Historically Berkeley-derived implementations have passed only one object
5025 * at a time, but this API allows multiple objects to be
5026 * passed in a single call to sendmsg() or recvmsg(). The following example
5027 * shows two ancillary data objects in a control buffer.
5029 * |<--------------------------- msg_controllen -------------------------->|
5030 * | |
5032 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5034 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5035 * | | |
5037 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5039 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5040 * | | | | |
5042 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5043 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5045 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5047 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5051 * msg_control
5052 * points here
5054 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5055 sctp_cmsgs_t *cmsgs)
5057 struct cmsghdr *cmsg;
5059 for (cmsg = CMSG_FIRSTHDR(msg);
5060 cmsg != NULL;
5061 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5062 if (!CMSG_OK(msg, cmsg))
5063 return -EINVAL;
5065 /* Should we parse this header or ignore? */
5066 if (cmsg->cmsg_level != IPPROTO_SCTP)
5067 continue;
5069 /* Strictly check lengths following example in SCM code. */
5070 switch (cmsg->cmsg_type) {
5071 case SCTP_INIT:
5072 /* SCTP Socket API Extension
5073 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5075 * This cmsghdr structure provides information for
5076 * initializing new SCTP associations with sendmsg().
5077 * The SCTP_INITMSG socket option uses this same data
5078 * structure. This structure is not used for
5079 * recvmsg().
5081 * cmsg_level cmsg_type cmsg_data[]
5082 * ------------ ------------ ----------------------
5083 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5085 if (cmsg->cmsg_len !=
5086 CMSG_LEN(sizeof(struct sctp_initmsg)))
5087 return -EINVAL;
5088 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5089 break;
5091 case SCTP_SNDRCV:
5092 /* SCTP Socket API Extension
5093 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5095 * This cmsghdr structure specifies SCTP options for
5096 * sendmsg() and describes SCTP header information
5097 * about a received message through recvmsg().
5099 * cmsg_level cmsg_type cmsg_data[]
5100 * ------------ ------------ ----------------------
5101 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5103 if (cmsg->cmsg_len !=
5104 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5105 return -EINVAL;
5107 cmsgs->info =
5108 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5110 /* Minimally, validate the sinfo_flags. */
5111 if (cmsgs->info->sinfo_flags &
5112 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5113 SCTP_ABORT | SCTP_EOF))
5114 return -EINVAL;
5115 break;
5117 default:
5118 return -EINVAL;
5121 return 0;
5125 * Wait for a packet..
5126 * Note: This function is the same function as in core/datagram.c
5127 * with a few modifications to make lksctp work.
5129 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5131 int error;
5132 DEFINE_WAIT(wait);
5134 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5136 /* Socket errors? */
5137 error = sock_error(sk);
5138 if (error)
5139 goto out;
5141 if (!skb_queue_empty(&sk->sk_receive_queue))
5142 goto ready;
5144 /* Socket shut down? */
5145 if (sk->sk_shutdown & RCV_SHUTDOWN)
5146 goto out;
5148 /* Sequenced packets can come disconnected. If so we report the
5149 * problem.
5151 error = -ENOTCONN;
5153 /* Is there a good reason to think that we may receive some data? */
5154 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5155 goto out;
5157 /* Handle signals. */
5158 if (signal_pending(current))
5159 goto interrupted;
5161 /* Let another process have a go. Since we are going to sleep
5162 * anyway. Note: This may cause odd behaviors if the message
5163 * does not fit in the user's buffer, but this seems to be the
5164 * only way to honor MSG_DONTWAIT realistically.
5166 sctp_release_sock(sk);
5167 *timeo_p = schedule_timeout(*timeo_p);
5168 sctp_lock_sock(sk);
5170 ready:
5171 finish_wait(sk->sk_sleep, &wait);
5172 return 0;
5174 interrupted:
5175 error = sock_intr_errno(*timeo_p);
5177 out:
5178 finish_wait(sk->sk_sleep, &wait);
5179 *err = error;
5180 return error;
5183 /* Receive a datagram.
5184 * Note: This is pretty much the same routine as in core/datagram.c
5185 * with a few changes to make lksctp work.
5187 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5188 int noblock, int *err)
5190 int error;
5191 struct sk_buff *skb;
5192 long timeo;
5194 timeo = sock_rcvtimeo(sk, noblock);
5196 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5197 timeo, MAX_SCHEDULE_TIMEOUT);
5199 do {
5200 /* Again only user level code calls this function,
5201 * so nothing interrupt level
5202 * will suddenly eat the receive_queue.
5204 * Look at current nfs client by the way...
5205 * However, this function was corrent in any case. 8)
5207 if (flags & MSG_PEEK) {
5208 spin_lock_bh(&sk->sk_receive_queue.lock);
5209 skb = skb_peek(&sk->sk_receive_queue);
5210 if (skb)
5211 atomic_inc(&skb->users);
5212 spin_unlock_bh(&sk->sk_receive_queue.lock);
5213 } else {
5214 skb = skb_dequeue(&sk->sk_receive_queue);
5217 if (skb)
5218 return skb;
5220 /* Caller is allowed not to check sk->sk_err before calling. */
5221 error = sock_error(sk);
5222 if (error)
5223 goto no_packet;
5225 if (sk->sk_shutdown & RCV_SHUTDOWN)
5226 break;
5228 /* User doesn't want to wait. */
5229 error = -EAGAIN;
5230 if (!timeo)
5231 goto no_packet;
5232 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5234 return NULL;
5236 no_packet:
5237 *err = error;
5238 return NULL;
5241 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5242 static void __sctp_write_space(struct sctp_association *asoc)
5244 struct sock *sk = asoc->base.sk;
5245 struct socket *sock = sk->sk_socket;
5247 if ((sctp_wspace(asoc) > 0) && sock) {
5248 if (waitqueue_active(&asoc->wait))
5249 wake_up_interruptible(&asoc->wait);
5251 if (sctp_writeable(sk)) {
5252 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5253 wake_up_interruptible(sk->sk_sleep);
5255 /* Note that we try to include the Async I/O support
5256 * here by modeling from the current TCP/UDP code.
5257 * We have not tested with it yet.
5259 if (sock->fasync_list &&
5260 !(sk->sk_shutdown & SEND_SHUTDOWN))
5261 sock_wake_async(sock, 2, POLL_OUT);
5266 /* Do accounting for the sndbuf space.
5267 * Decrement the used sndbuf space of the corresponding association by the
5268 * data size which was just transmitted(freed).
5270 static void sctp_wfree(struct sk_buff *skb)
5272 struct sctp_association *asoc;
5273 struct sctp_chunk *chunk;
5274 struct sock *sk;
5276 /* Get the saved chunk pointer. */
5277 chunk = *((struct sctp_chunk **)(skb->cb));
5278 asoc = chunk->asoc;
5279 sk = asoc->base.sk;
5280 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5281 sizeof(struct sk_buff) +
5282 sizeof(struct sctp_chunk);
5284 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5286 sock_wfree(skb);
5287 __sctp_write_space(asoc);
5289 sctp_association_put(asoc);
5292 /* Helper function to wait for space in the sndbuf. */
5293 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5294 size_t msg_len)
5296 struct sock *sk = asoc->base.sk;
5297 int err = 0;
5298 long current_timeo = *timeo_p;
5299 DEFINE_WAIT(wait);
5301 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5302 asoc, (long)(*timeo_p), msg_len);
5304 /* Increment the association's refcnt. */
5305 sctp_association_hold(asoc);
5307 /* Wait on the association specific sndbuf space. */
5308 for (;;) {
5309 prepare_to_wait_exclusive(&asoc->wait, &wait,
5310 TASK_INTERRUPTIBLE);
5311 if (!*timeo_p)
5312 goto do_nonblock;
5313 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5314 asoc->base.dead)
5315 goto do_error;
5316 if (signal_pending(current))
5317 goto do_interrupted;
5318 if (msg_len <= sctp_wspace(asoc))
5319 break;
5321 /* Let another process have a go. Since we are going
5322 * to sleep anyway.
5324 sctp_release_sock(sk);
5325 current_timeo = schedule_timeout(current_timeo);
5326 sctp_lock_sock(sk);
5328 *timeo_p = current_timeo;
5331 out:
5332 finish_wait(&asoc->wait, &wait);
5334 /* Release the association's refcnt. */
5335 sctp_association_put(asoc);
5337 return err;
5339 do_error:
5340 err = -EPIPE;
5341 goto out;
5343 do_interrupted:
5344 err = sock_intr_errno(*timeo_p);
5345 goto out;
5347 do_nonblock:
5348 err = -EAGAIN;
5349 goto out;
5352 /* If socket sndbuf has changed, wake up all per association waiters. */
5353 void sctp_write_space(struct sock *sk)
5355 struct sctp_association *asoc;
5356 struct list_head *pos;
5358 /* Wake up the tasks in each wait queue. */
5359 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5360 asoc = list_entry(pos, struct sctp_association, asocs);
5361 __sctp_write_space(asoc);
5365 /* Is there any sndbuf space available on the socket?
5367 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5368 * associations on the same socket. For a UDP-style socket with
5369 * multiple associations, it is possible for it to be "unwriteable"
5370 * prematurely. I assume that this is acceptable because
5371 * a premature "unwriteable" is better than an accidental "writeable" which
5372 * would cause an unwanted block under certain circumstances. For the 1-1
5373 * UDP-style sockets or TCP-style sockets, this code should work.
5374 * - Daisy
5376 static int sctp_writeable(struct sock *sk)
5378 int amt = 0;
5380 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5381 if (amt < 0)
5382 amt = 0;
5383 return amt;
5386 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5387 * returns immediately with EINPROGRESS.
5389 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5391 struct sock *sk = asoc->base.sk;
5392 int err = 0;
5393 long current_timeo = *timeo_p;
5394 DEFINE_WAIT(wait);
5396 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5397 (long)(*timeo_p));
5399 /* Increment the association's refcnt. */
5400 sctp_association_hold(asoc);
5402 for (;;) {
5403 prepare_to_wait_exclusive(&asoc->wait, &wait,
5404 TASK_INTERRUPTIBLE);
5405 if (!*timeo_p)
5406 goto do_nonblock;
5407 if (sk->sk_shutdown & RCV_SHUTDOWN)
5408 break;
5409 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5410 asoc->base.dead)
5411 goto do_error;
5412 if (signal_pending(current))
5413 goto do_interrupted;
5415 if (sctp_state(asoc, ESTABLISHED))
5416 break;
5418 /* Let another process have a go. Since we are going
5419 * to sleep anyway.
5421 sctp_release_sock(sk);
5422 current_timeo = schedule_timeout(current_timeo);
5423 sctp_lock_sock(sk);
5425 *timeo_p = current_timeo;
5428 out:
5429 finish_wait(&asoc->wait, &wait);
5431 /* Release the association's refcnt. */
5432 sctp_association_put(asoc);
5434 return err;
5436 do_error:
5437 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5438 err = -ETIMEDOUT;
5439 else
5440 err = -ECONNREFUSED;
5441 goto out;
5443 do_interrupted:
5444 err = sock_intr_errno(*timeo_p);
5445 goto out;
5447 do_nonblock:
5448 err = -EINPROGRESS;
5449 goto out;
5452 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5454 struct sctp_endpoint *ep;
5455 int err = 0;
5456 DEFINE_WAIT(wait);
5458 ep = sctp_sk(sk)->ep;
5461 for (;;) {
5462 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5463 TASK_INTERRUPTIBLE);
5465 if (list_empty(&ep->asocs)) {
5466 sctp_release_sock(sk);
5467 timeo = schedule_timeout(timeo);
5468 sctp_lock_sock(sk);
5471 err = -EINVAL;
5472 if (!sctp_sstate(sk, LISTENING))
5473 break;
5475 err = 0;
5476 if (!list_empty(&ep->asocs))
5477 break;
5479 err = sock_intr_errno(timeo);
5480 if (signal_pending(current))
5481 break;
5483 err = -EAGAIN;
5484 if (!timeo)
5485 break;
5488 finish_wait(sk->sk_sleep, &wait);
5490 return err;
5493 void sctp_wait_for_close(struct sock *sk, long timeout)
5495 DEFINE_WAIT(wait);
5497 do {
5498 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5499 if (list_empty(&sctp_sk(sk)->ep->asocs))
5500 break;
5501 sctp_release_sock(sk);
5502 timeout = schedule_timeout(timeout);
5503 sctp_lock_sock(sk);
5504 } while (!signal_pending(current) && timeout);
5506 finish_wait(sk->sk_sleep, &wait);
5509 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5510 * and its messages to the newsk.
5512 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5513 struct sctp_association *assoc,
5514 sctp_socket_type_t type)
5516 struct sctp_sock *oldsp = sctp_sk(oldsk);
5517 struct sctp_sock *newsp = sctp_sk(newsk);
5518 struct sctp_bind_bucket *pp; /* hash list port iterator */
5519 struct sctp_endpoint *newep = newsp->ep;
5520 struct sk_buff *skb, *tmp;
5521 struct sctp_ulpevent *event;
5522 int flags = 0;
5524 /* Migrate socket buffer sizes and all the socket level options to the
5525 * new socket.
5527 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5528 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5529 /* Brute force copy old sctp opt. */
5530 inet_sk_copy_descendant(newsk, oldsk);
5532 /* Restore the ep value that was overwritten with the above structure
5533 * copy.
5535 newsp->ep = newep;
5536 newsp->hmac = NULL;
5538 /* Hook this new socket in to the bind_hash list. */
5539 pp = sctp_sk(oldsk)->bind_hash;
5540 sk_add_bind_node(newsk, &pp->owner);
5541 sctp_sk(newsk)->bind_hash = pp;
5542 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5544 /* Copy the bind_addr list from the original endpoint to the new
5545 * endpoint so that we can handle restarts properly
5547 if (assoc->peer.ipv4_address)
5548 flags |= SCTP_ADDR4_PEERSUPP;
5549 if (assoc->peer.ipv6_address)
5550 flags |= SCTP_ADDR6_PEERSUPP;
5551 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5552 &oldsp->ep->base.bind_addr,
5553 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5555 /* Move any messages in the old socket's receive queue that are for the
5556 * peeled off association to the new socket's receive queue.
5558 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5559 event = sctp_skb2event(skb);
5560 if (event->asoc == assoc) {
5561 sock_rfree(skb);
5562 __skb_unlink(skb, &oldsk->sk_receive_queue);
5563 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5564 skb_set_owner_r(skb, newsk);
5568 /* Clean up any messages pending delivery due to partial
5569 * delivery. Three cases:
5570 * 1) No partial deliver; no work.
5571 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5572 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5574 skb_queue_head_init(&newsp->pd_lobby);
5575 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5577 if (sctp_sk(oldsk)->pd_mode) {
5578 struct sk_buff_head *queue;
5580 /* Decide which queue to move pd_lobby skbs to. */
5581 if (assoc->ulpq.pd_mode) {
5582 queue = &newsp->pd_lobby;
5583 } else
5584 queue = &newsk->sk_receive_queue;
5586 /* Walk through the pd_lobby, looking for skbs that
5587 * need moved to the new socket.
5589 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5590 event = sctp_skb2event(skb);
5591 if (event->asoc == assoc) {
5592 sock_rfree(skb);
5593 __skb_unlink(skb, &oldsp->pd_lobby);
5594 __skb_queue_tail(queue, skb);
5595 skb_set_owner_r(skb, newsk);
5599 /* Clear up any skbs waiting for the partial
5600 * delivery to finish.
5602 if (assoc->ulpq.pd_mode)
5603 sctp_clear_pd(oldsk);
5607 /* Set the type of socket to indicate that it is peeled off from the
5608 * original UDP-style socket or created with the accept() call on a
5609 * TCP-style socket..
5611 newsp->type = type;
5613 spin_lock_bh(&oldsk->sk_lock.slock);
5614 /* Migrate the backlog from oldsk to newsk. */
5615 sctp_backlog_migrate(assoc, oldsk, newsk);
5616 /* Migrate the association to the new socket. */
5617 sctp_assoc_migrate(assoc, newsk);
5618 spin_unlock_bh(&oldsk->sk_lock.slock);
5620 /* If the association on the newsk is already closed before accept()
5621 * is called, set RCV_SHUTDOWN flag.
5623 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5624 newsk->sk_shutdown |= RCV_SHUTDOWN;
5626 newsk->sk_state = SCTP_SS_ESTABLISHED;
5629 /* This proto struct describes the ULP interface for SCTP. */
5630 struct proto sctp_prot = {
5631 .name = "SCTP",
5632 .owner = THIS_MODULE,
5633 .close = sctp_close,
5634 .connect = sctp_connect,
5635 .disconnect = sctp_disconnect,
5636 .accept = sctp_accept,
5637 .ioctl = sctp_ioctl,
5638 .init = sctp_init_sock,
5639 .destroy = sctp_destroy_sock,
5640 .shutdown = sctp_shutdown,
5641 .setsockopt = sctp_setsockopt,
5642 .getsockopt = sctp_getsockopt,
5643 .sendmsg = sctp_sendmsg,
5644 .recvmsg = sctp_recvmsg,
5645 .bind = sctp_bind,
5646 .backlog_rcv = sctp_backlog_rcv,
5647 .hash = sctp_hash,
5648 .unhash = sctp_unhash,
5649 .get_port = sctp_get_port,
5650 .obj_size = sizeof(struct sctp_sock),
5653 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5654 struct proto sctpv6_prot = {
5655 .name = "SCTPv6",
5656 .owner = THIS_MODULE,
5657 .close = sctp_close,
5658 .connect = sctp_connect,
5659 .disconnect = sctp_disconnect,
5660 .accept = sctp_accept,
5661 .ioctl = sctp_ioctl,
5662 .init = sctp_init_sock,
5663 .destroy = sctp_destroy_sock,
5664 .shutdown = sctp_shutdown,
5665 .setsockopt = sctp_setsockopt,
5666 .getsockopt = sctp_getsockopt,
5667 .sendmsg = sctp_sendmsg,
5668 .recvmsg = sctp_recvmsg,
5669 .bind = sctp_bind,
5670 .backlog_rcv = sctp_backlog_rcv,
5671 .hash = sctp_hash,
5672 .unhash = sctp_unhash,
5673 .get_port = sctp_get_port,
5674 .obj_size = sizeof(struct sctp6_sock),
5676 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */