[PKT_SCHED] RED: Fix overflow in calculation of queue average
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / socket.c
blob49f9305710305f90d2f584111f5b31888857c2d9
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), NULL))
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 struct sctp_chunk *chunk;
1249 chunk = sctp_make_abort_user(asoc, NULL, 0);
1250 if (chunk)
1251 sctp_primitive_ABORT(asoc, chunk);
1252 } else
1253 sctp_primitive_SHUTDOWN(asoc, NULL);
1254 } else
1255 sctp_primitive_SHUTDOWN(asoc, NULL);
1258 /* Clean up any skbs sitting on the receive queue. */
1259 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1260 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1262 /* On a TCP-style socket, block for at most linger_time if set. */
1263 if (sctp_style(sk, TCP) && timeout)
1264 sctp_wait_for_close(sk, timeout);
1266 /* This will run the backlog queue. */
1267 sctp_release_sock(sk);
1269 /* Supposedly, no process has access to the socket, but
1270 * the net layers still may.
1272 sctp_local_bh_disable();
1273 sctp_bh_lock_sock(sk);
1275 /* Hold the sock, since sk_common_release() will put sock_put()
1276 * and we have just a little more cleanup.
1278 sock_hold(sk);
1279 sk_common_release(sk);
1281 sctp_bh_unlock_sock(sk);
1282 sctp_local_bh_enable();
1284 sock_put(sk);
1286 SCTP_DBG_OBJCNT_DEC(sock);
1289 /* Handle EPIPE error. */
1290 static int sctp_error(struct sock *sk, int flags, int err)
1292 if (err == -EPIPE)
1293 err = sock_error(sk) ? : -EPIPE;
1294 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1295 send_sig(SIGPIPE, current, 0);
1296 return err;
1299 /* API 3.1.3 sendmsg() - UDP Style Syntax
1301 * An application uses sendmsg() and recvmsg() calls to transmit data to
1302 * and receive data from its peer.
1304 * ssize_t sendmsg(int socket, const struct msghdr *message,
1305 * int flags);
1307 * socket - the socket descriptor of the endpoint.
1308 * message - pointer to the msghdr structure which contains a single
1309 * user message and possibly some ancillary data.
1311 * See Section 5 for complete description of the data
1312 * structures.
1314 * flags - flags sent or received with the user message, see Section
1315 * 5 for complete description of the flags.
1317 * Note: This function could use a rewrite especially when explicit
1318 * connect support comes in.
1320 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1322 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1324 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1325 struct msghdr *msg, size_t msg_len)
1327 struct sctp_sock *sp;
1328 struct sctp_endpoint *ep;
1329 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1330 struct sctp_transport *transport, *chunk_tp;
1331 struct sctp_chunk *chunk;
1332 union sctp_addr to;
1333 struct sockaddr *msg_name = NULL;
1334 struct sctp_sndrcvinfo default_sinfo = { 0 };
1335 struct sctp_sndrcvinfo *sinfo;
1336 struct sctp_initmsg *sinit;
1337 sctp_assoc_t associd = 0;
1338 sctp_cmsgs_t cmsgs = { NULL };
1339 int err;
1340 sctp_scope_t scope;
1341 long timeo;
1342 __u16 sinfo_flags = 0;
1343 struct sctp_datamsg *datamsg;
1344 struct list_head *pos;
1345 int msg_flags = msg->msg_flags;
1347 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1348 sk, msg, msg_len);
1350 err = 0;
1351 sp = sctp_sk(sk);
1352 ep = sp->ep;
1354 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1356 /* We cannot send a message over a TCP-style listening socket. */
1357 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1358 err = -EPIPE;
1359 goto out_nounlock;
1362 /* Parse out the SCTP CMSGs. */
1363 err = sctp_msghdr_parse(msg, &cmsgs);
1365 if (err) {
1366 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1367 goto out_nounlock;
1370 /* Fetch the destination address for this packet. This
1371 * address only selects the association--it is not necessarily
1372 * the address we will send to.
1373 * For a peeled-off socket, msg_name is ignored.
1375 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1376 int msg_namelen = msg->msg_namelen;
1378 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1379 msg_namelen);
1380 if (err)
1381 return err;
1383 if (msg_namelen > sizeof(to))
1384 msg_namelen = sizeof(to);
1385 memcpy(&to, msg->msg_name, msg_namelen);
1386 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1387 "0x%x:%u.\n",
1388 to.v4.sin_addr.s_addr, to.v4.sin_port);
1390 to.v4.sin_port = ntohs(to.v4.sin_port);
1391 msg_name = msg->msg_name;
1394 sinfo = cmsgs.info;
1395 sinit = cmsgs.init;
1397 /* Did the user specify SNDRCVINFO? */
1398 if (sinfo) {
1399 sinfo_flags = sinfo->sinfo_flags;
1400 associd = sinfo->sinfo_assoc_id;
1403 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1404 msg_len, sinfo_flags);
1406 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1407 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1408 err = -EINVAL;
1409 goto out_nounlock;
1412 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1413 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1414 * If SCTP_ABORT is set, the message length could be non zero with
1415 * the msg_iov set to the user abort reason.
1417 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1418 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1419 err = -EINVAL;
1420 goto out_nounlock;
1423 /* If SCTP_ADDR_OVER is set, there must be an address
1424 * specified in msg_name.
1426 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1427 err = -EINVAL;
1428 goto out_nounlock;
1431 transport = NULL;
1433 SCTP_DEBUG_PRINTK("About to look up association.\n");
1435 sctp_lock_sock(sk);
1437 /* If a msg_name has been specified, assume this is to be used. */
1438 if (msg_name) {
1439 /* Look for a matching association on the endpoint. */
1440 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1441 if (!asoc) {
1442 /* If we could not find a matching association on the
1443 * endpoint, make sure that it is not a TCP-style
1444 * socket that already has an association or there is
1445 * no peeled-off association on another socket.
1447 if ((sctp_style(sk, TCP) &&
1448 sctp_sstate(sk, ESTABLISHED)) ||
1449 sctp_endpoint_is_peeled_off(ep, &to)) {
1450 err = -EADDRNOTAVAIL;
1451 goto out_unlock;
1454 } else {
1455 asoc = sctp_id2assoc(sk, associd);
1456 if (!asoc) {
1457 err = -EPIPE;
1458 goto out_unlock;
1462 if (asoc) {
1463 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1465 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1466 * socket that has an association in CLOSED state. This can
1467 * happen when an accepted socket has an association that is
1468 * already CLOSED.
1470 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1471 err = -EPIPE;
1472 goto out_unlock;
1475 if (sinfo_flags & SCTP_EOF) {
1476 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1477 asoc);
1478 sctp_primitive_SHUTDOWN(asoc, NULL);
1479 err = 0;
1480 goto out_unlock;
1482 if (sinfo_flags & SCTP_ABORT) {
1483 struct sctp_chunk *chunk;
1485 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1486 if (!chunk) {
1487 err = -ENOMEM;
1488 goto out_unlock;
1491 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1492 sctp_primitive_ABORT(asoc, chunk);
1493 err = 0;
1494 goto out_unlock;
1498 /* Do we need to create the association? */
1499 if (!asoc) {
1500 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1502 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1503 err = -EINVAL;
1504 goto out_unlock;
1507 /* Check for invalid stream against the stream counts,
1508 * either the default or the user specified stream counts.
1510 if (sinfo) {
1511 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1512 /* Check against the defaults. */
1513 if (sinfo->sinfo_stream >=
1514 sp->initmsg.sinit_num_ostreams) {
1515 err = -EINVAL;
1516 goto out_unlock;
1518 } else {
1519 /* Check against the requested. */
1520 if (sinfo->sinfo_stream >=
1521 sinit->sinit_num_ostreams) {
1522 err = -EINVAL;
1523 goto out_unlock;
1529 * API 3.1.2 bind() - UDP Style Syntax
1530 * If a bind() or sctp_bindx() is not called prior to a
1531 * sendmsg() call that initiates a new association, the
1532 * system picks an ephemeral port and will choose an address
1533 * set equivalent to binding with a wildcard address.
1535 if (!ep->base.bind_addr.port) {
1536 if (sctp_autobind(sk)) {
1537 err = -EAGAIN;
1538 goto out_unlock;
1540 } else {
1542 * If an unprivileged user inherits a one-to-many
1543 * style socket with open associations on a privileged
1544 * port, it MAY be permitted to accept new associations,
1545 * but it SHOULD NOT be permitted to open new
1546 * associations.
1548 if (ep->base.bind_addr.port < PROT_SOCK &&
1549 !capable(CAP_NET_BIND_SERVICE)) {
1550 err = -EACCES;
1551 goto out_unlock;
1555 scope = sctp_scope(&to);
1556 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1557 if (!new_asoc) {
1558 err = -ENOMEM;
1559 goto out_unlock;
1561 asoc = new_asoc;
1563 /* If the SCTP_INIT ancillary data is specified, set all
1564 * the association init values accordingly.
1566 if (sinit) {
1567 if (sinit->sinit_num_ostreams) {
1568 asoc->c.sinit_num_ostreams =
1569 sinit->sinit_num_ostreams;
1571 if (sinit->sinit_max_instreams) {
1572 asoc->c.sinit_max_instreams =
1573 sinit->sinit_max_instreams;
1575 if (sinit->sinit_max_attempts) {
1576 asoc->max_init_attempts
1577 = sinit->sinit_max_attempts;
1579 if (sinit->sinit_max_init_timeo) {
1580 asoc->max_init_timeo =
1581 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1585 /* Prime the peer's transport structures. */
1586 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1587 if (!transport) {
1588 err = -ENOMEM;
1589 goto out_free;
1591 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1592 if (err < 0) {
1593 err = -ENOMEM;
1594 goto out_free;
1598 /* ASSERT: we have a valid association at this point. */
1599 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1601 if (!sinfo) {
1602 /* If the user didn't specify SNDRCVINFO, make up one with
1603 * some defaults.
1605 default_sinfo.sinfo_stream = asoc->default_stream;
1606 default_sinfo.sinfo_flags = asoc->default_flags;
1607 default_sinfo.sinfo_ppid = asoc->default_ppid;
1608 default_sinfo.sinfo_context = asoc->default_context;
1609 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1610 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1611 sinfo = &default_sinfo;
1614 /* API 7.1.7, the sndbuf size per association bounds the
1615 * maximum size of data that can be sent in a single send call.
1617 if (msg_len > sk->sk_sndbuf) {
1618 err = -EMSGSIZE;
1619 goto out_free;
1622 /* If fragmentation is disabled and the message length exceeds the
1623 * association fragmentation point, return EMSGSIZE. The I-D
1624 * does not specify what this error is, but this looks like
1625 * a great fit.
1627 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1628 err = -EMSGSIZE;
1629 goto out_free;
1632 if (sinfo) {
1633 /* Check for invalid stream. */
1634 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1635 err = -EINVAL;
1636 goto out_free;
1640 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1641 if (!sctp_wspace(asoc)) {
1642 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1643 if (err)
1644 goto out_free;
1647 /* If an address is passed with the sendto/sendmsg call, it is used
1648 * to override the primary destination address in the TCP model, or
1649 * when SCTP_ADDR_OVER flag is set in the UDP model.
1651 if ((sctp_style(sk, TCP) && msg_name) ||
1652 (sinfo_flags & SCTP_ADDR_OVER)) {
1653 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1654 if (!chunk_tp) {
1655 err = -EINVAL;
1656 goto out_free;
1658 } else
1659 chunk_tp = NULL;
1661 /* Auto-connect, if we aren't connected already. */
1662 if (sctp_state(asoc, CLOSED)) {
1663 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1664 if (err < 0)
1665 goto out_free;
1666 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1669 /* Break the message into multiple chunks of maximum size. */
1670 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1671 if (!datamsg) {
1672 err = -ENOMEM;
1673 goto out_free;
1676 /* Now send the (possibly) fragmented message. */
1677 list_for_each(pos, &datamsg->chunks) {
1678 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1679 sctp_datamsg_track(chunk);
1681 /* Do accounting for the write space. */
1682 sctp_set_owner_w(chunk);
1684 chunk->transport = chunk_tp;
1686 /* Send it to the lower layers. Note: all chunks
1687 * must either fail or succeed. The lower layer
1688 * works that way today. Keep it that way or this
1689 * breaks.
1691 err = sctp_primitive_SEND(asoc, chunk);
1692 /* Did the lower layer accept the chunk? */
1693 if (err)
1694 sctp_chunk_free(chunk);
1695 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1698 sctp_datamsg_free(datamsg);
1699 if (err)
1700 goto out_free;
1701 else
1702 err = msg_len;
1704 /* If we are already past ASSOCIATE, the lower
1705 * layers are responsible for association cleanup.
1707 goto out_unlock;
1709 out_free:
1710 if (new_asoc)
1711 sctp_association_free(asoc);
1712 out_unlock:
1713 sctp_release_sock(sk);
1715 out_nounlock:
1716 return sctp_error(sk, msg_flags, err);
1718 #if 0
1719 do_sock_err:
1720 if (msg_len)
1721 err = msg_len;
1722 else
1723 err = sock_error(sk);
1724 goto out;
1726 do_interrupted:
1727 if (msg_len)
1728 err = msg_len;
1729 goto out;
1730 #endif /* 0 */
1733 /* This is an extended version of skb_pull() that removes the data from the
1734 * start of a skb even when data is spread across the list of skb's in the
1735 * frag_list. len specifies the total amount of data that needs to be removed.
1736 * when 'len' bytes could be removed from the skb, it returns 0.
1737 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1738 * could not be removed.
1740 static int sctp_skb_pull(struct sk_buff *skb, int len)
1742 struct sk_buff *list;
1743 int skb_len = skb_headlen(skb);
1744 int rlen;
1746 if (len <= skb_len) {
1747 __skb_pull(skb, len);
1748 return 0;
1750 len -= skb_len;
1751 __skb_pull(skb, skb_len);
1753 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1754 rlen = sctp_skb_pull(list, len);
1755 skb->len -= (len-rlen);
1756 skb->data_len -= (len-rlen);
1758 if (!rlen)
1759 return 0;
1761 len = rlen;
1764 return len;
1767 /* API 3.1.3 recvmsg() - UDP Style Syntax
1769 * ssize_t recvmsg(int socket, struct msghdr *message,
1770 * int flags);
1772 * socket - the socket descriptor of the endpoint.
1773 * message - pointer to the msghdr structure which contains a single
1774 * user message and possibly some ancillary data.
1776 * See Section 5 for complete description of the data
1777 * structures.
1779 * flags - flags sent or received with the user message, see Section
1780 * 5 for complete description of the flags.
1782 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1784 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1785 struct msghdr *msg, size_t len, int noblock,
1786 int flags, int *addr_len)
1788 struct sctp_ulpevent *event = NULL;
1789 struct sctp_sock *sp = sctp_sk(sk);
1790 struct sk_buff *skb;
1791 int copied;
1792 int err = 0;
1793 int skb_len;
1795 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1796 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1797 "len", len, "knoblauch", noblock,
1798 "flags", flags, "addr_len", addr_len);
1800 sctp_lock_sock(sk);
1802 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1803 err = -ENOTCONN;
1804 goto out;
1807 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1808 if (!skb)
1809 goto out;
1811 /* Get the total length of the skb including any skb's in the
1812 * frag_list.
1814 skb_len = skb->len;
1816 copied = skb_len;
1817 if (copied > len)
1818 copied = len;
1820 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1822 event = sctp_skb2event(skb);
1824 if (err)
1825 goto out_free;
1827 sock_recv_timestamp(msg, sk, skb);
1828 if (sctp_ulpevent_is_notification(event)) {
1829 msg->msg_flags |= MSG_NOTIFICATION;
1830 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1831 } else {
1832 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1835 /* Check if we allow SCTP_SNDRCVINFO. */
1836 if (sp->subscribe.sctp_data_io_event)
1837 sctp_ulpevent_read_sndrcvinfo(event, msg);
1838 #if 0
1839 /* FIXME: we should be calling IP/IPv6 layers. */
1840 if (sk->sk_protinfo.af_inet.cmsg_flags)
1841 ip_cmsg_recv(msg, skb);
1842 #endif
1844 err = copied;
1846 /* If skb's length exceeds the user's buffer, update the skb and
1847 * push it back to the receive_queue so that the next call to
1848 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1850 if (skb_len > copied) {
1851 msg->msg_flags &= ~MSG_EOR;
1852 if (flags & MSG_PEEK)
1853 goto out_free;
1854 sctp_skb_pull(skb, copied);
1855 skb_queue_head(&sk->sk_receive_queue, skb);
1857 /* When only partial message is copied to the user, increase
1858 * rwnd by that amount. If all the data in the skb is read,
1859 * rwnd is updated when the event is freed.
1861 sctp_assoc_rwnd_increase(event->asoc, copied);
1862 goto out;
1863 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1864 (event->msg_flags & MSG_EOR))
1865 msg->msg_flags |= MSG_EOR;
1866 else
1867 msg->msg_flags &= ~MSG_EOR;
1869 out_free:
1870 if (flags & MSG_PEEK) {
1871 /* Release the skb reference acquired after peeking the skb in
1872 * sctp_skb_recv_datagram().
1874 kfree_skb(skb);
1875 } else {
1876 /* Free the event which includes releasing the reference to
1877 * the owner of the skb, freeing the skb and updating the
1878 * rwnd.
1880 sctp_ulpevent_free(event);
1882 out:
1883 sctp_release_sock(sk);
1884 return err;
1887 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1889 * This option is a on/off flag. If enabled no SCTP message
1890 * fragmentation will be performed. Instead if a message being sent
1891 * exceeds the current PMTU size, the message will NOT be sent and
1892 * instead a error will be indicated to the user.
1894 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1895 char __user *optval, int optlen)
1897 int val;
1899 if (optlen < sizeof(int))
1900 return -EINVAL;
1902 if (get_user(val, (int __user *)optval))
1903 return -EFAULT;
1905 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1907 return 0;
1910 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1911 int optlen)
1913 if (optlen != sizeof(struct sctp_event_subscribe))
1914 return -EINVAL;
1915 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1916 return -EFAULT;
1917 return 0;
1920 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1922 * This socket option is applicable to the UDP-style socket only. When
1923 * set it will cause associations that are idle for more than the
1924 * specified number of seconds to automatically close. An association
1925 * being idle is defined an association that has NOT sent or received
1926 * user data. The special value of '0' indicates that no automatic
1927 * close of any associations should be performed. The option expects an
1928 * integer defining the number of seconds of idle time before an
1929 * association is closed.
1931 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1932 int optlen)
1934 struct sctp_sock *sp = sctp_sk(sk);
1936 /* Applicable to UDP-style socket only */
1937 if (sctp_style(sk, TCP))
1938 return -EOPNOTSUPP;
1939 if (optlen != sizeof(int))
1940 return -EINVAL;
1941 if (copy_from_user(&sp->autoclose, optval, optlen))
1942 return -EFAULT;
1944 return 0;
1947 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1949 * Applications can enable or disable heartbeats for any peer address of
1950 * an association, modify an address's heartbeat interval, force a
1951 * heartbeat to be sent immediately, and adjust the address's maximum
1952 * number of retransmissions sent before an address is considered
1953 * unreachable. The following structure is used to access and modify an
1954 * address's parameters:
1956 * struct sctp_paddrparams {
1957 * sctp_assoc_t spp_assoc_id;
1958 * struct sockaddr_storage spp_address;
1959 * uint32_t spp_hbinterval;
1960 * uint16_t spp_pathmaxrxt;
1961 * uint32_t spp_pathmtu;
1962 * uint32_t spp_sackdelay;
1963 * uint32_t spp_flags;
1964 * };
1966 * spp_assoc_id - (one-to-many style socket) This is filled in the
1967 * application, and identifies the association for
1968 * this query.
1969 * spp_address - This specifies which address is of interest.
1970 * spp_hbinterval - This contains the value of the heartbeat interval,
1971 * in milliseconds. If a value of zero
1972 * is present in this field then no changes are to
1973 * be made to this parameter.
1974 * spp_pathmaxrxt - This contains the maximum number of
1975 * retransmissions before this address shall be
1976 * considered unreachable. If a value of zero
1977 * is present in this field then no changes are to
1978 * be made to this parameter.
1979 * spp_pathmtu - When Path MTU discovery is disabled the value
1980 * specified here will be the "fixed" path mtu.
1981 * Note that if the spp_address field is empty
1982 * then all associations on this address will
1983 * have this fixed path mtu set upon them.
1985 * spp_sackdelay - When delayed sack is enabled, this value specifies
1986 * the number of milliseconds that sacks will be delayed
1987 * for. This value will apply to all addresses of an
1988 * association if the spp_address field is empty. Note
1989 * also, that if delayed sack is enabled and this
1990 * value is set to 0, no change is made to the last
1991 * recorded delayed sack timer value.
1993 * spp_flags - These flags are used to control various features
1994 * on an association. The flag field may contain
1995 * zero or more of the following options.
1997 * SPP_HB_ENABLE - Enable heartbeats on the
1998 * specified address. Note that if the address
1999 * field is empty all addresses for the association
2000 * have heartbeats enabled upon them.
2002 * SPP_HB_DISABLE - Disable heartbeats on the
2003 * speicifed address. Note that if the address
2004 * field is empty all addresses for the association
2005 * will have their heartbeats disabled. Note also
2006 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2007 * mutually exclusive, only one of these two should
2008 * be specified. Enabling both fields will have
2009 * undetermined results.
2011 * SPP_HB_DEMAND - Request a user initiated heartbeat
2012 * to be made immediately.
2014 * SPP_PMTUD_ENABLE - This field will enable PMTU
2015 * discovery upon the specified address. Note that
2016 * if the address feild is empty then all addresses
2017 * on the association are effected.
2019 * SPP_PMTUD_DISABLE - This field will disable PMTU
2020 * discovery upon the specified address. Note that
2021 * if the address feild is empty then all addresses
2022 * on the association are effected. Not also that
2023 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2024 * exclusive. Enabling both will have undetermined
2025 * results.
2027 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2028 * on delayed sack. The time specified in spp_sackdelay
2029 * is used to specify the sack delay for this address. Note
2030 * that if spp_address is empty then all addresses will
2031 * enable delayed sack and take on the sack delay
2032 * value specified in spp_sackdelay.
2033 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2034 * off delayed sack. If the spp_address field is blank then
2035 * delayed sack is disabled for the entire association. Note
2036 * also that this field is mutually exclusive to
2037 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2038 * results.
2040 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2041 struct sctp_transport *trans,
2042 struct sctp_association *asoc,
2043 struct sctp_sock *sp,
2044 int hb_change,
2045 int pmtud_change,
2046 int sackdelay_change)
2048 int error;
2050 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2051 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2052 if (error)
2053 return error;
2056 if (params->spp_hbinterval) {
2057 if (trans) {
2058 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2059 } else if (asoc) {
2060 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2061 } else {
2062 sp->hbinterval = params->spp_hbinterval;
2066 if (hb_change) {
2067 if (trans) {
2068 trans->param_flags =
2069 (trans->param_flags & ~SPP_HB) | hb_change;
2070 } else if (asoc) {
2071 asoc->param_flags =
2072 (asoc->param_flags & ~SPP_HB) | hb_change;
2073 } else {
2074 sp->param_flags =
2075 (sp->param_flags & ~SPP_HB) | hb_change;
2079 if (params->spp_pathmtu) {
2080 if (trans) {
2081 trans->pathmtu = params->spp_pathmtu;
2082 sctp_assoc_sync_pmtu(asoc);
2083 } else if (asoc) {
2084 asoc->pathmtu = params->spp_pathmtu;
2085 sctp_frag_point(sp, params->spp_pathmtu);
2086 } else {
2087 sp->pathmtu = params->spp_pathmtu;
2091 if (pmtud_change) {
2092 if (trans) {
2093 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2094 (params->spp_flags & SPP_PMTUD_ENABLE);
2095 trans->param_flags =
2096 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2097 if (update) {
2098 sctp_transport_pmtu(trans);
2099 sctp_assoc_sync_pmtu(asoc);
2101 } else if (asoc) {
2102 asoc->param_flags =
2103 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2104 } else {
2105 sp->param_flags =
2106 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2110 if (params->spp_sackdelay) {
2111 if (trans) {
2112 trans->sackdelay =
2113 msecs_to_jiffies(params->spp_sackdelay);
2114 } else if (asoc) {
2115 asoc->sackdelay =
2116 msecs_to_jiffies(params->spp_sackdelay);
2117 } else {
2118 sp->sackdelay = params->spp_sackdelay;
2122 if (sackdelay_change) {
2123 if (trans) {
2124 trans->param_flags =
2125 (trans->param_flags & ~SPP_SACKDELAY) |
2126 sackdelay_change;
2127 } else if (asoc) {
2128 asoc->param_flags =
2129 (asoc->param_flags & ~SPP_SACKDELAY) |
2130 sackdelay_change;
2131 } else {
2132 sp->param_flags =
2133 (sp->param_flags & ~SPP_SACKDELAY) |
2134 sackdelay_change;
2138 if (params->spp_pathmaxrxt) {
2139 if (trans) {
2140 trans->pathmaxrxt = params->spp_pathmaxrxt;
2141 } else if (asoc) {
2142 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2143 } else {
2144 sp->pathmaxrxt = params->spp_pathmaxrxt;
2148 return 0;
2151 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2152 char __user *optval, int optlen)
2154 struct sctp_paddrparams params;
2155 struct sctp_transport *trans = NULL;
2156 struct sctp_association *asoc = NULL;
2157 struct sctp_sock *sp = sctp_sk(sk);
2158 int error;
2159 int hb_change, pmtud_change, sackdelay_change;
2161 if (optlen != sizeof(struct sctp_paddrparams))
2162 return - EINVAL;
2164 if (copy_from_user(&params, optval, optlen))
2165 return -EFAULT;
2167 /* Validate flags and value parameters. */
2168 hb_change = params.spp_flags & SPP_HB;
2169 pmtud_change = params.spp_flags & SPP_PMTUD;
2170 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2172 if (hb_change == SPP_HB ||
2173 pmtud_change == SPP_PMTUD ||
2174 sackdelay_change == SPP_SACKDELAY ||
2175 params.spp_sackdelay > 500 ||
2176 (params.spp_pathmtu
2177 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2178 return -EINVAL;
2180 /* If an address other than INADDR_ANY is specified, and
2181 * no transport is found, then the request is invalid.
2183 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2184 trans = sctp_addr_id2transport(sk, &params.spp_address,
2185 params.spp_assoc_id);
2186 if (!trans)
2187 return -EINVAL;
2190 /* Get association, if assoc_id != 0 and the socket is a one
2191 * to many style socket, and an association was not found, then
2192 * the id was invalid.
2194 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2195 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2196 return -EINVAL;
2198 /* Heartbeat demand can only be sent on a transport or
2199 * association, but not a socket.
2201 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2202 return -EINVAL;
2204 /* Process parameters. */
2205 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2206 hb_change, pmtud_change,
2207 sackdelay_change);
2209 if (error)
2210 return error;
2212 /* If changes are for association, also apply parameters to each
2213 * transport.
2215 if (!trans && asoc) {
2216 struct list_head *pos;
2218 list_for_each(pos, &asoc->peer.transport_addr_list) {
2219 trans = list_entry(pos, struct sctp_transport,
2220 transports);
2221 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2222 hb_change, pmtud_change,
2223 sackdelay_change);
2227 return 0;
2230 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2232 * This options will get or set the delayed ack timer. The time is set
2233 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2234 * endpoints default delayed ack timer value. If the assoc_id field is
2235 * non-zero, then the set or get effects the specified association.
2237 * struct sctp_assoc_value {
2238 * sctp_assoc_t assoc_id;
2239 * uint32_t assoc_value;
2240 * };
2242 * assoc_id - This parameter, indicates which association the
2243 * user is preforming an action upon. Note that if
2244 * this field's value is zero then the endpoints
2245 * default value is changed (effecting future
2246 * associations only).
2248 * assoc_value - This parameter contains the number of milliseconds
2249 * that the user is requesting the delayed ACK timer
2250 * be set to. Note that this value is defined in
2251 * the standard to be between 200 and 500 milliseconds.
2253 * Note: a value of zero will leave the value alone,
2254 * but disable SACK delay. A non-zero value will also
2255 * enable SACK delay.
2258 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2259 char __user *optval, int optlen)
2261 struct sctp_assoc_value params;
2262 struct sctp_transport *trans = NULL;
2263 struct sctp_association *asoc = NULL;
2264 struct sctp_sock *sp = sctp_sk(sk);
2266 if (optlen != sizeof(struct sctp_assoc_value))
2267 return - EINVAL;
2269 if (copy_from_user(&params, optval, optlen))
2270 return -EFAULT;
2272 /* Validate value parameter. */
2273 if (params.assoc_value > 500)
2274 return -EINVAL;
2276 /* Get association, if assoc_id != 0 and the socket is a one
2277 * to many style socket, and an association was not found, then
2278 * the id was invalid.
2280 asoc = sctp_id2assoc(sk, params.assoc_id);
2281 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2282 return -EINVAL;
2284 if (params.assoc_value) {
2285 if (asoc) {
2286 asoc->sackdelay =
2287 msecs_to_jiffies(params.assoc_value);
2288 asoc->param_flags =
2289 (asoc->param_flags & ~SPP_SACKDELAY) |
2290 SPP_SACKDELAY_ENABLE;
2291 } else {
2292 sp->sackdelay = params.assoc_value;
2293 sp->param_flags =
2294 (sp->param_flags & ~SPP_SACKDELAY) |
2295 SPP_SACKDELAY_ENABLE;
2297 } else {
2298 if (asoc) {
2299 asoc->param_flags =
2300 (asoc->param_flags & ~SPP_SACKDELAY) |
2301 SPP_SACKDELAY_DISABLE;
2302 } else {
2303 sp->param_flags =
2304 (sp->param_flags & ~SPP_SACKDELAY) |
2305 SPP_SACKDELAY_DISABLE;
2309 /* If change is for association, also apply to each transport. */
2310 if (asoc) {
2311 struct list_head *pos;
2313 list_for_each(pos, &asoc->peer.transport_addr_list) {
2314 trans = list_entry(pos, struct sctp_transport,
2315 transports);
2316 if (params.assoc_value) {
2317 trans->sackdelay =
2318 msecs_to_jiffies(params.assoc_value);
2319 trans->param_flags =
2320 (trans->param_flags & ~SPP_SACKDELAY) |
2321 SPP_SACKDELAY_ENABLE;
2322 } else {
2323 trans->param_flags =
2324 (trans->param_flags & ~SPP_SACKDELAY) |
2325 SPP_SACKDELAY_DISABLE;
2330 return 0;
2333 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2335 * Applications can specify protocol parameters for the default association
2336 * initialization. The option name argument to setsockopt() and getsockopt()
2337 * is SCTP_INITMSG.
2339 * Setting initialization parameters is effective only on an unconnected
2340 * socket (for UDP-style sockets only future associations are effected
2341 * by the change). With TCP-style sockets, this option is inherited by
2342 * sockets derived from a listener socket.
2344 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2346 struct sctp_initmsg sinit;
2347 struct sctp_sock *sp = sctp_sk(sk);
2349 if (optlen != sizeof(struct sctp_initmsg))
2350 return -EINVAL;
2351 if (copy_from_user(&sinit, optval, optlen))
2352 return -EFAULT;
2354 if (sinit.sinit_num_ostreams)
2355 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2356 if (sinit.sinit_max_instreams)
2357 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2358 if (sinit.sinit_max_attempts)
2359 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2360 if (sinit.sinit_max_init_timeo)
2361 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2363 return 0;
2367 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2369 * Applications that wish to use the sendto() system call may wish to
2370 * specify a default set of parameters that would normally be supplied
2371 * through the inclusion of ancillary data. This socket option allows
2372 * such an application to set the default sctp_sndrcvinfo structure.
2373 * The application that wishes to use this socket option simply passes
2374 * in to this call the sctp_sndrcvinfo structure defined in Section
2375 * 5.2.2) The input parameters accepted by this call include
2376 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2377 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2378 * to this call if the caller is using the UDP model.
2380 static int sctp_setsockopt_default_send_param(struct sock *sk,
2381 char __user *optval, int optlen)
2383 struct sctp_sndrcvinfo info;
2384 struct sctp_association *asoc;
2385 struct sctp_sock *sp = sctp_sk(sk);
2387 if (optlen != sizeof(struct sctp_sndrcvinfo))
2388 return -EINVAL;
2389 if (copy_from_user(&info, optval, optlen))
2390 return -EFAULT;
2392 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2393 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2394 return -EINVAL;
2396 if (asoc) {
2397 asoc->default_stream = info.sinfo_stream;
2398 asoc->default_flags = info.sinfo_flags;
2399 asoc->default_ppid = info.sinfo_ppid;
2400 asoc->default_context = info.sinfo_context;
2401 asoc->default_timetolive = info.sinfo_timetolive;
2402 } else {
2403 sp->default_stream = info.sinfo_stream;
2404 sp->default_flags = info.sinfo_flags;
2405 sp->default_ppid = info.sinfo_ppid;
2406 sp->default_context = info.sinfo_context;
2407 sp->default_timetolive = info.sinfo_timetolive;
2410 return 0;
2413 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2415 * Requests that the local SCTP stack use the enclosed peer address as
2416 * the association primary. The enclosed address must be one of the
2417 * association peer's addresses.
2419 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2420 int optlen)
2422 struct sctp_prim prim;
2423 struct sctp_transport *trans;
2425 if (optlen != sizeof(struct sctp_prim))
2426 return -EINVAL;
2428 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2429 return -EFAULT;
2431 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2432 if (!trans)
2433 return -EINVAL;
2435 sctp_assoc_set_primary(trans->asoc, trans);
2437 return 0;
2441 * 7.1.5 SCTP_NODELAY
2443 * Turn on/off any Nagle-like algorithm. This means that packets are
2444 * generally sent as soon as possible and no unnecessary delays are
2445 * introduced, at the cost of more packets in the network. Expects an
2446 * integer boolean flag.
2448 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2449 int optlen)
2451 int val;
2453 if (optlen < sizeof(int))
2454 return -EINVAL;
2455 if (get_user(val, (int __user *)optval))
2456 return -EFAULT;
2458 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2459 return 0;
2464 * 7.1.1 SCTP_RTOINFO
2466 * The protocol parameters used to initialize and bound retransmission
2467 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2468 * and modify these parameters.
2469 * All parameters are time values, in milliseconds. A value of 0, when
2470 * modifying the parameters, indicates that the current value should not
2471 * be changed.
2474 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2475 struct sctp_rtoinfo rtoinfo;
2476 struct sctp_association *asoc;
2478 if (optlen != sizeof (struct sctp_rtoinfo))
2479 return -EINVAL;
2481 if (copy_from_user(&rtoinfo, optval, optlen))
2482 return -EFAULT;
2484 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2486 /* Set the values to the specific association */
2487 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2488 return -EINVAL;
2490 if (asoc) {
2491 if (rtoinfo.srto_initial != 0)
2492 asoc->rto_initial =
2493 msecs_to_jiffies(rtoinfo.srto_initial);
2494 if (rtoinfo.srto_max != 0)
2495 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2496 if (rtoinfo.srto_min != 0)
2497 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2498 } else {
2499 /* If there is no association or the association-id = 0
2500 * set the values to the endpoint.
2502 struct sctp_sock *sp = sctp_sk(sk);
2504 if (rtoinfo.srto_initial != 0)
2505 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2506 if (rtoinfo.srto_max != 0)
2507 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2508 if (rtoinfo.srto_min != 0)
2509 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2512 return 0;
2517 * 7.1.2 SCTP_ASSOCINFO
2519 * This option is used to tune the the maximum retransmission attempts
2520 * of the association.
2521 * Returns an error if the new association retransmission value is
2522 * greater than the sum of the retransmission value of the peer.
2523 * See [SCTP] for more information.
2526 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2529 struct sctp_assocparams assocparams;
2530 struct sctp_association *asoc;
2532 if (optlen != sizeof(struct sctp_assocparams))
2533 return -EINVAL;
2534 if (copy_from_user(&assocparams, optval, optlen))
2535 return -EFAULT;
2537 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2539 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2540 return -EINVAL;
2542 /* Set the values to the specific association */
2543 if (asoc) {
2544 if (assocparams.sasoc_asocmaxrxt != 0) {
2545 __u32 path_sum = 0;
2546 int paths = 0;
2547 struct list_head *pos;
2548 struct sctp_transport *peer_addr;
2550 list_for_each(pos, &asoc->peer.transport_addr_list) {
2551 peer_addr = list_entry(pos,
2552 struct sctp_transport,
2553 transports);
2554 path_sum += peer_addr->pathmaxrxt;
2555 paths++;
2558 /* Only validate asocmaxrxt if we have more then
2559 * one path/transport. We do this because path
2560 * retransmissions are only counted when we have more
2561 * then one path.
2563 if (paths > 1 &&
2564 assocparams.sasoc_asocmaxrxt > path_sum)
2565 return -EINVAL;
2567 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2570 if (assocparams.sasoc_cookie_life != 0) {
2571 asoc->cookie_life.tv_sec =
2572 assocparams.sasoc_cookie_life / 1000;
2573 asoc->cookie_life.tv_usec =
2574 (assocparams.sasoc_cookie_life % 1000)
2575 * 1000;
2577 } else {
2578 /* Set the values to the endpoint */
2579 struct sctp_sock *sp = sctp_sk(sk);
2581 if (assocparams.sasoc_asocmaxrxt != 0)
2582 sp->assocparams.sasoc_asocmaxrxt =
2583 assocparams.sasoc_asocmaxrxt;
2584 if (assocparams.sasoc_cookie_life != 0)
2585 sp->assocparams.sasoc_cookie_life =
2586 assocparams.sasoc_cookie_life;
2588 return 0;
2592 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2594 * This socket option is a boolean flag which turns on or off mapped V4
2595 * addresses. If this option is turned on and the socket is type
2596 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2597 * If this option is turned off, then no mapping will be done of V4
2598 * addresses and a user will receive both PF_INET6 and PF_INET type
2599 * addresses on the socket.
2601 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2603 int val;
2604 struct sctp_sock *sp = sctp_sk(sk);
2606 if (optlen < sizeof(int))
2607 return -EINVAL;
2608 if (get_user(val, (int __user *)optval))
2609 return -EFAULT;
2610 if (val)
2611 sp->v4mapped = 1;
2612 else
2613 sp->v4mapped = 0;
2615 return 0;
2619 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2621 * This socket option specifies the maximum size to put in any outgoing
2622 * SCTP chunk. If a message is larger than this size it will be
2623 * fragmented by SCTP into the specified size. Note that the underlying
2624 * SCTP implementation may fragment into smaller sized chunks when the
2625 * PMTU of the underlying association is smaller than the value set by
2626 * the user.
2628 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2630 struct sctp_association *asoc;
2631 struct list_head *pos;
2632 struct sctp_sock *sp = sctp_sk(sk);
2633 int val;
2635 if (optlen < sizeof(int))
2636 return -EINVAL;
2637 if (get_user(val, (int __user *)optval))
2638 return -EFAULT;
2639 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2640 return -EINVAL;
2641 sp->user_frag = val;
2643 /* Update the frag_point of the existing associations. */
2644 list_for_each(pos, &(sp->ep->asocs)) {
2645 asoc = list_entry(pos, struct sctp_association, asocs);
2646 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2649 return 0;
2654 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2656 * Requests that the peer mark the enclosed address as the association
2657 * primary. The enclosed address must be one of the association's
2658 * locally bound addresses. The following structure is used to make a
2659 * set primary request:
2661 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2662 int optlen)
2664 struct sctp_sock *sp;
2665 struct sctp_endpoint *ep;
2666 struct sctp_association *asoc = NULL;
2667 struct sctp_setpeerprim prim;
2668 struct sctp_chunk *chunk;
2669 int err;
2671 sp = sctp_sk(sk);
2672 ep = sp->ep;
2674 if (!sctp_addip_enable)
2675 return -EPERM;
2677 if (optlen != sizeof(struct sctp_setpeerprim))
2678 return -EINVAL;
2680 if (copy_from_user(&prim, optval, optlen))
2681 return -EFAULT;
2683 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2684 if (!asoc)
2685 return -EINVAL;
2687 if (!asoc->peer.asconf_capable)
2688 return -EPERM;
2690 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2691 return -EPERM;
2693 if (!sctp_state(asoc, ESTABLISHED))
2694 return -ENOTCONN;
2696 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2697 return -EADDRNOTAVAIL;
2699 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2700 chunk = sctp_make_asconf_set_prim(asoc,
2701 (union sctp_addr *)&prim.sspp_addr);
2702 if (!chunk)
2703 return -ENOMEM;
2705 err = sctp_send_asconf(asoc, chunk);
2707 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2709 return err;
2712 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2713 int optlen)
2715 struct sctp_setadaption adaption;
2717 if (optlen != sizeof(struct sctp_setadaption))
2718 return -EINVAL;
2719 if (copy_from_user(&adaption, optval, optlen))
2720 return -EFAULT;
2722 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2724 return 0;
2727 /* API 6.2 setsockopt(), getsockopt()
2729 * Applications use setsockopt() and getsockopt() to set or retrieve
2730 * socket options. Socket options are used to change the default
2731 * behavior of sockets calls. They are described in Section 7.
2733 * The syntax is:
2735 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2736 * int __user *optlen);
2737 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2738 * int optlen);
2740 * sd - the socket descript.
2741 * level - set to IPPROTO_SCTP for all SCTP options.
2742 * optname - the option name.
2743 * optval - the buffer to store the value of the option.
2744 * optlen - the size of the buffer.
2746 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2747 char __user *optval, int optlen)
2749 int retval = 0;
2751 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2752 sk, optname);
2754 /* I can hardly begin to describe how wrong this is. This is
2755 * so broken as to be worse than useless. The API draft
2756 * REALLY is NOT helpful here... I am not convinced that the
2757 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2758 * are at all well-founded.
2760 if (level != SOL_SCTP) {
2761 struct sctp_af *af = sctp_sk(sk)->pf->af;
2762 retval = af->setsockopt(sk, level, optname, optval, optlen);
2763 goto out_nounlock;
2766 sctp_lock_sock(sk);
2768 switch (optname) {
2769 case SCTP_SOCKOPT_BINDX_ADD:
2770 /* 'optlen' is the size of the addresses buffer. */
2771 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2772 optlen, SCTP_BINDX_ADD_ADDR);
2773 break;
2775 case SCTP_SOCKOPT_BINDX_REM:
2776 /* 'optlen' is the size of the addresses buffer. */
2777 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2778 optlen, SCTP_BINDX_REM_ADDR);
2779 break;
2781 case SCTP_SOCKOPT_CONNECTX:
2782 /* 'optlen' is the size of the addresses buffer. */
2783 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2784 optlen);
2785 break;
2787 case SCTP_DISABLE_FRAGMENTS:
2788 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2789 break;
2791 case SCTP_EVENTS:
2792 retval = sctp_setsockopt_events(sk, optval, optlen);
2793 break;
2795 case SCTP_AUTOCLOSE:
2796 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2797 break;
2799 case SCTP_PEER_ADDR_PARAMS:
2800 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2801 break;
2803 case SCTP_DELAYED_ACK_TIME:
2804 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2805 break;
2807 case SCTP_INITMSG:
2808 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2809 break;
2810 case SCTP_DEFAULT_SEND_PARAM:
2811 retval = sctp_setsockopt_default_send_param(sk, optval,
2812 optlen);
2813 break;
2814 case SCTP_PRIMARY_ADDR:
2815 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2816 break;
2817 case SCTP_SET_PEER_PRIMARY_ADDR:
2818 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2819 break;
2820 case SCTP_NODELAY:
2821 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2822 break;
2823 case SCTP_RTOINFO:
2824 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2825 break;
2826 case SCTP_ASSOCINFO:
2827 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2828 break;
2829 case SCTP_I_WANT_MAPPED_V4_ADDR:
2830 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2831 break;
2832 case SCTP_MAXSEG:
2833 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2834 break;
2835 case SCTP_ADAPTION_LAYER:
2836 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2837 break;
2839 default:
2840 retval = -ENOPROTOOPT;
2841 break;
2844 sctp_release_sock(sk);
2846 out_nounlock:
2847 return retval;
2850 /* API 3.1.6 connect() - UDP Style Syntax
2852 * An application may use the connect() call in the UDP model to initiate an
2853 * association without sending data.
2855 * The syntax is:
2857 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2859 * sd: the socket descriptor to have a new association added to.
2861 * nam: the address structure (either struct sockaddr_in or struct
2862 * sockaddr_in6 defined in RFC2553 [7]).
2864 * len: the size of the address.
2866 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2867 int addr_len)
2869 int err = 0;
2870 struct sctp_af *af;
2872 sctp_lock_sock(sk);
2874 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2875 __FUNCTION__, sk, addr, addr_len);
2877 /* Validate addr_len before calling common connect/connectx routine. */
2878 af = sctp_get_af_specific(addr->sa_family);
2879 if (!af || addr_len < af->sockaddr_len) {
2880 err = -EINVAL;
2881 } else {
2882 /* Pass correct addr len to common routine (so it knows there
2883 * is only one address being passed.
2885 err = __sctp_connect(sk, addr, af->sockaddr_len);
2888 sctp_release_sock(sk);
2889 return err;
2892 /* FIXME: Write comments. */
2893 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2895 return -EOPNOTSUPP; /* STUB */
2898 /* 4.1.4 accept() - TCP Style Syntax
2900 * Applications use accept() call to remove an established SCTP
2901 * association from the accept queue of the endpoint. A new socket
2902 * descriptor will be returned from accept() to represent the newly
2903 * formed association.
2905 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2907 struct sctp_sock *sp;
2908 struct sctp_endpoint *ep;
2909 struct sock *newsk = NULL;
2910 struct sctp_association *asoc;
2911 long timeo;
2912 int error = 0;
2914 sctp_lock_sock(sk);
2916 sp = sctp_sk(sk);
2917 ep = sp->ep;
2919 if (!sctp_style(sk, TCP)) {
2920 error = -EOPNOTSUPP;
2921 goto out;
2924 if (!sctp_sstate(sk, LISTENING)) {
2925 error = -EINVAL;
2926 goto out;
2929 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2931 error = sctp_wait_for_accept(sk, timeo);
2932 if (error)
2933 goto out;
2935 /* We treat the list of associations on the endpoint as the accept
2936 * queue and pick the first association on the list.
2938 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2940 newsk = sp->pf->create_accept_sk(sk, asoc);
2941 if (!newsk) {
2942 error = -ENOMEM;
2943 goto out;
2946 /* Populate the fields of the newsk from the oldsk and migrate the
2947 * asoc to the newsk.
2949 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2951 out:
2952 sctp_release_sock(sk);
2953 *err = error;
2954 return newsk;
2957 /* The SCTP ioctl handler. */
2958 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2960 return -ENOIOCTLCMD;
2963 /* This is the function which gets called during socket creation to
2964 * initialized the SCTP-specific portion of the sock.
2965 * The sock structure should already be zero-filled memory.
2967 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2969 struct sctp_endpoint *ep;
2970 struct sctp_sock *sp;
2972 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2974 sp = sctp_sk(sk);
2976 /* Initialize the SCTP per socket area. */
2977 switch (sk->sk_type) {
2978 case SOCK_SEQPACKET:
2979 sp->type = SCTP_SOCKET_UDP;
2980 break;
2981 case SOCK_STREAM:
2982 sp->type = SCTP_SOCKET_TCP;
2983 break;
2984 default:
2985 return -ESOCKTNOSUPPORT;
2988 /* Initialize default send parameters. These parameters can be
2989 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2991 sp->default_stream = 0;
2992 sp->default_ppid = 0;
2993 sp->default_flags = 0;
2994 sp->default_context = 0;
2995 sp->default_timetolive = 0;
2997 /* Initialize default setup parameters. These parameters
2998 * can be modified with the SCTP_INITMSG socket option or
2999 * overridden by the SCTP_INIT CMSG.
3001 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3002 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3003 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3004 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
3006 /* Initialize default RTO related parameters. These parameters can
3007 * be modified for with the SCTP_RTOINFO socket option.
3009 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
3010 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
3011 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
3013 /* Initialize default association related parameters. These parameters
3014 * can be modified with the SCTP_ASSOCINFO socket option.
3016 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3017 sp->assocparams.sasoc_number_peer_destinations = 0;
3018 sp->assocparams.sasoc_peer_rwnd = 0;
3019 sp->assocparams.sasoc_local_rwnd = 0;
3020 sp->assocparams.sasoc_cookie_life =
3021 jiffies_to_msecs(sctp_valid_cookie_life);
3023 /* Initialize default event subscriptions. By default, all the
3024 * options are off.
3026 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3028 /* Default Peer Address Parameters. These defaults can
3029 * be modified via SCTP_PEER_ADDR_PARAMS
3031 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
3032 sp->pathmaxrxt = sctp_max_retrans_path;
3033 sp->pathmtu = 0; // allow default discovery
3034 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
3035 sp->param_flags = SPP_HB_ENABLE |
3036 SPP_PMTUD_ENABLE |
3037 SPP_SACKDELAY_ENABLE;
3039 /* If enabled no SCTP message fragmentation will be performed.
3040 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3042 sp->disable_fragments = 0;
3044 /* Turn on/off any Nagle-like algorithm. */
3045 sp->nodelay = 1;
3047 /* Enable by default. */
3048 sp->v4mapped = 1;
3050 /* Auto-close idle associations after the configured
3051 * number of seconds. A value of 0 disables this
3052 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3053 * for UDP-style sockets only.
3055 sp->autoclose = 0;
3057 /* User specified fragmentation limit. */
3058 sp->user_frag = 0;
3060 sp->adaption_ind = 0;
3062 sp->pf = sctp_get_pf_specific(sk->sk_family);
3064 /* Control variables for partial data delivery. */
3065 sp->pd_mode = 0;
3066 skb_queue_head_init(&sp->pd_lobby);
3068 /* Create a per socket endpoint structure. Even if we
3069 * change the data structure relationships, this may still
3070 * be useful for storing pre-connect address information.
3072 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3073 if (!ep)
3074 return -ENOMEM;
3076 sp->ep = ep;
3077 sp->hmac = NULL;
3079 SCTP_DBG_OBJCNT_INC(sock);
3080 return 0;
3083 /* Cleanup any SCTP per socket resources. */
3084 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3086 struct sctp_endpoint *ep;
3088 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3090 /* Release our hold on the endpoint. */
3091 ep = sctp_sk(sk)->ep;
3092 sctp_endpoint_free(ep);
3094 return 0;
3097 /* API 4.1.7 shutdown() - TCP Style Syntax
3098 * int shutdown(int socket, int how);
3100 * sd - the socket descriptor of the association to be closed.
3101 * how - Specifies the type of shutdown. The values are
3102 * as follows:
3103 * SHUT_RD
3104 * Disables further receive operations. No SCTP
3105 * protocol action is taken.
3106 * SHUT_WR
3107 * Disables further send operations, and initiates
3108 * the SCTP shutdown sequence.
3109 * SHUT_RDWR
3110 * Disables further send and receive operations
3111 * and initiates the SCTP shutdown sequence.
3113 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3115 struct sctp_endpoint *ep;
3116 struct sctp_association *asoc;
3118 if (!sctp_style(sk, TCP))
3119 return;
3121 if (how & SEND_SHUTDOWN) {
3122 ep = sctp_sk(sk)->ep;
3123 if (!list_empty(&ep->asocs)) {
3124 asoc = list_entry(ep->asocs.next,
3125 struct sctp_association, asocs);
3126 sctp_primitive_SHUTDOWN(asoc, NULL);
3131 /* 7.2.1 Association Status (SCTP_STATUS)
3133 * Applications can retrieve current status information about an
3134 * association, including association state, peer receiver window size,
3135 * number of unacked data chunks, and number of data chunks pending
3136 * receipt. This information is read-only.
3138 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3139 char __user *optval,
3140 int __user *optlen)
3142 struct sctp_status status;
3143 struct sctp_association *asoc = NULL;
3144 struct sctp_transport *transport;
3145 sctp_assoc_t associd;
3146 int retval = 0;
3148 if (len != sizeof(status)) {
3149 retval = -EINVAL;
3150 goto out;
3153 if (copy_from_user(&status, optval, sizeof(status))) {
3154 retval = -EFAULT;
3155 goto out;
3158 associd = status.sstat_assoc_id;
3159 asoc = sctp_id2assoc(sk, associd);
3160 if (!asoc) {
3161 retval = -EINVAL;
3162 goto out;
3165 transport = asoc->peer.primary_path;
3167 status.sstat_assoc_id = sctp_assoc2id(asoc);
3168 status.sstat_state = asoc->state;
3169 status.sstat_rwnd = asoc->peer.rwnd;
3170 status.sstat_unackdata = asoc->unack_data;
3172 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3173 status.sstat_instrms = asoc->c.sinit_max_instreams;
3174 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3175 status.sstat_fragmentation_point = asoc->frag_point;
3176 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3177 memcpy(&status.sstat_primary.spinfo_address,
3178 &(transport->ipaddr), sizeof(union sctp_addr));
3179 /* Map ipv4 address into v4-mapped-on-v6 address. */
3180 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3181 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3182 status.sstat_primary.spinfo_state = transport->state;
3183 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3184 status.sstat_primary.spinfo_srtt = transport->srtt;
3185 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3186 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3188 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3189 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3191 if (put_user(len, optlen)) {
3192 retval = -EFAULT;
3193 goto out;
3196 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3197 len, status.sstat_state, status.sstat_rwnd,
3198 status.sstat_assoc_id);
3200 if (copy_to_user(optval, &status, len)) {
3201 retval = -EFAULT;
3202 goto out;
3205 out:
3206 return (retval);
3210 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3212 * Applications can retrieve information about a specific peer address
3213 * of an association, including its reachability state, congestion
3214 * window, and retransmission timer values. This information is
3215 * read-only.
3217 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3218 char __user *optval,
3219 int __user *optlen)
3221 struct sctp_paddrinfo pinfo;
3222 struct sctp_transport *transport;
3223 int retval = 0;
3225 if (len != sizeof(pinfo)) {
3226 retval = -EINVAL;
3227 goto out;
3230 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3231 retval = -EFAULT;
3232 goto out;
3235 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3236 pinfo.spinfo_assoc_id);
3237 if (!transport)
3238 return -EINVAL;
3240 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3241 pinfo.spinfo_state = transport->state;
3242 pinfo.spinfo_cwnd = transport->cwnd;
3243 pinfo.spinfo_srtt = transport->srtt;
3244 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3245 pinfo.spinfo_mtu = transport->pathmtu;
3247 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3248 pinfo.spinfo_state = SCTP_ACTIVE;
3250 if (put_user(len, optlen)) {
3251 retval = -EFAULT;
3252 goto out;
3255 if (copy_to_user(optval, &pinfo, len)) {
3256 retval = -EFAULT;
3257 goto out;
3260 out:
3261 return (retval);
3264 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3266 * This option is a on/off flag. If enabled no SCTP message
3267 * fragmentation will be performed. Instead if a message being sent
3268 * exceeds the current PMTU size, the message will NOT be sent and
3269 * instead a error will be indicated to the user.
3271 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3272 char __user *optval, int __user *optlen)
3274 int val;
3276 if (len < sizeof(int))
3277 return -EINVAL;
3279 len = sizeof(int);
3280 val = (sctp_sk(sk)->disable_fragments == 1);
3281 if (put_user(len, optlen))
3282 return -EFAULT;
3283 if (copy_to_user(optval, &val, len))
3284 return -EFAULT;
3285 return 0;
3288 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3290 * This socket option is used to specify various notifications and
3291 * ancillary data the user wishes to receive.
3293 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3294 int __user *optlen)
3296 if (len != sizeof(struct sctp_event_subscribe))
3297 return -EINVAL;
3298 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3299 return -EFAULT;
3300 return 0;
3303 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3305 * This socket option is applicable to the UDP-style socket only. When
3306 * set it will cause associations that are idle for more than the
3307 * specified number of seconds to automatically close. An association
3308 * being idle is defined an association that has NOT sent or received
3309 * user data. The special value of '0' indicates that no automatic
3310 * close of any associations should be performed. The option expects an
3311 * integer defining the number of seconds of idle time before an
3312 * association is closed.
3314 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3316 /* Applicable to UDP-style socket only */
3317 if (sctp_style(sk, TCP))
3318 return -EOPNOTSUPP;
3319 if (len != sizeof(int))
3320 return -EINVAL;
3321 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3322 return -EFAULT;
3323 return 0;
3326 /* Helper routine to branch off an association to a new socket. */
3327 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3328 struct socket **sockp)
3330 struct sock *sk = asoc->base.sk;
3331 struct socket *sock;
3332 int err = 0;
3334 /* An association cannot be branched off from an already peeled-off
3335 * socket, nor is this supported for tcp style sockets.
3337 if (!sctp_style(sk, UDP))
3338 return -EINVAL;
3340 /* Create a new socket. */
3341 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3342 if (err < 0)
3343 return err;
3345 /* Populate the fields of the newsk from the oldsk and migrate the
3346 * asoc to the newsk.
3348 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3349 *sockp = sock;
3351 return err;
3354 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3356 sctp_peeloff_arg_t peeloff;
3357 struct socket *newsock;
3358 int retval = 0;
3359 struct sctp_association *asoc;
3361 if (len != sizeof(sctp_peeloff_arg_t))
3362 return -EINVAL;
3363 if (copy_from_user(&peeloff, optval, len))
3364 return -EFAULT;
3366 asoc = sctp_id2assoc(sk, peeloff.associd);
3367 if (!asoc) {
3368 retval = -EINVAL;
3369 goto out;
3372 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3374 retval = sctp_do_peeloff(asoc, &newsock);
3375 if (retval < 0)
3376 goto out;
3378 /* Map the socket to an unused fd that can be returned to the user. */
3379 retval = sock_map_fd(newsock);
3380 if (retval < 0) {
3381 sock_release(newsock);
3382 goto out;
3385 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3386 __FUNCTION__, sk, asoc, newsock->sk, retval);
3388 /* Return the fd mapped to the new socket. */
3389 peeloff.sd = retval;
3390 if (copy_to_user(optval, &peeloff, len))
3391 retval = -EFAULT;
3393 out:
3394 return retval;
3397 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3399 * Applications can enable or disable heartbeats for any peer address of
3400 * an association, modify an address's heartbeat interval, force a
3401 * heartbeat to be sent immediately, and adjust the address's maximum
3402 * number of retransmissions sent before an address is considered
3403 * unreachable. The following structure is used to access and modify an
3404 * address's parameters:
3406 * struct sctp_paddrparams {
3407 * sctp_assoc_t spp_assoc_id;
3408 * struct sockaddr_storage spp_address;
3409 * uint32_t spp_hbinterval;
3410 * uint16_t spp_pathmaxrxt;
3411 * uint32_t spp_pathmtu;
3412 * uint32_t spp_sackdelay;
3413 * uint32_t spp_flags;
3414 * };
3416 * spp_assoc_id - (one-to-many style socket) This is filled in the
3417 * application, and identifies the association for
3418 * this query.
3419 * spp_address - This specifies which address is of interest.
3420 * spp_hbinterval - This contains the value of the heartbeat interval,
3421 * in milliseconds. If a value of zero
3422 * is present in this field then no changes are to
3423 * be made to this parameter.
3424 * spp_pathmaxrxt - This contains the maximum number of
3425 * retransmissions before this address shall be
3426 * considered unreachable. If a value of zero
3427 * is present in this field then no changes are to
3428 * be made to this parameter.
3429 * spp_pathmtu - When Path MTU discovery is disabled the value
3430 * specified here will be the "fixed" path mtu.
3431 * Note that if the spp_address field is empty
3432 * then all associations on this address will
3433 * have this fixed path mtu set upon them.
3435 * spp_sackdelay - When delayed sack is enabled, this value specifies
3436 * the number of milliseconds that sacks will be delayed
3437 * for. This value will apply to all addresses of an
3438 * association if the spp_address field is empty. Note
3439 * also, that if delayed sack is enabled and this
3440 * value is set to 0, no change is made to the last
3441 * recorded delayed sack timer value.
3443 * spp_flags - These flags are used to control various features
3444 * on an association. The flag field may contain
3445 * zero or more of the following options.
3447 * SPP_HB_ENABLE - Enable heartbeats on the
3448 * specified address. Note that if the address
3449 * field is empty all addresses for the association
3450 * have heartbeats enabled upon them.
3452 * SPP_HB_DISABLE - Disable heartbeats on the
3453 * speicifed address. Note that if the address
3454 * field is empty all addresses for the association
3455 * will have their heartbeats disabled. Note also
3456 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3457 * mutually exclusive, only one of these two should
3458 * be specified. Enabling both fields will have
3459 * undetermined results.
3461 * SPP_HB_DEMAND - Request a user initiated heartbeat
3462 * to be made immediately.
3464 * SPP_PMTUD_ENABLE - This field will enable PMTU
3465 * discovery upon the specified address. Note that
3466 * if the address feild is empty then all addresses
3467 * on the association are effected.
3469 * SPP_PMTUD_DISABLE - This field will disable PMTU
3470 * discovery upon the specified address. Note that
3471 * if the address feild is empty then all addresses
3472 * on the association are effected. Not also that
3473 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3474 * exclusive. Enabling both will have undetermined
3475 * results.
3477 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3478 * on delayed sack. The time specified in spp_sackdelay
3479 * is used to specify the sack delay for this address. Note
3480 * that if spp_address is empty then all addresses will
3481 * enable delayed sack and take on the sack delay
3482 * value specified in spp_sackdelay.
3483 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3484 * off delayed sack. If the spp_address field is blank then
3485 * delayed sack is disabled for the entire association. Note
3486 * also that this field is mutually exclusive to
3487 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3488 * results.
3490 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3491 char __user *optval, int __user *optlen)
3493 struct sctp_paddrparams params;
3494 struct sctp_transport *trans = NULL;
3495 struct sctp_association *asoc = NULL;
3496 struct sctp_sock *sp = sctp_sk(sk);
3498 if (len != sizeof(struct sctp_paddrparams))
3499 return -EINVAL;
3501 if (copy_from_user(&params, optval, len))
3502 return -EFAULT;
3504 /* If an address other than INADDR_ANY is specified, and
3505 * no transport is found, then the request is invalid.
3507 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3508 trans = sctp_addr_id2transport(sk, &params.spp_address,
3509 params.spp_assoc_id);
3510 if (!trans) {
3511 SCTP_DEBUG_PRINTK("Failed no transport\n");
3512 return -EINVAL;
3516 /* Get association, if assoc_id != 0 and the socket is a one
3517 * to many style socket, and an association was not found, then
3518 * the id was invalid.
3520 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3521 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3522 SCTP_DEBUG_PRINTK("Failed no association\n");
3523 return -EINVAL;
3526 if (trans) {
3527 /* Fetch transport values. */
3528 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3529 params.spp_pathmtu = trans->pathmtu;
3530 params.spp_pathmaxrxt = trans->pathmaxrxt;
3531 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3533 /*draft-11 doesn't say what to return in spp_flags*/
3534 params.spp_flags = trans->param_flags;
3535 } else if (asoc) {
3536 /* Fetch association values. */
3537 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3538 params.spp_pathmtu = asoc->pathmtu;
3539 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3540 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3542 /*draft-11 doesn't say what to return in spp_flags*/
3543 params.spp_flags = asoc->param_flags;
3544 } else {
3545 /* Fetch socket values. */
3546 params.spp_hbinterval = sp->hbinterval;
3547 params.spp_pathmtu = sp->pathmtu;
3548 params.spp_sackdelay = sp->sackdelay;
3549 params.spp_pathmaxrxt = sp->pathmaxrxt;
3551 /*draft-11 doesn't say what to return in spp_flags*/
3552 params.spp_flags = sp->param_flags;
3555 if (copy_to_user(optval, &params, len))
3556 return -EFAULT;
3558 if (put_user(len, optlen))
3559 return -EFAULT;
3561 return 0;
3564 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3566 * This options will get or set the delayed ack timer. The time is set
3567 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3568 * endpoints default delayed ack timer value. If the assoc_id field is
3569 * non-zero, then the set or get effects the specified association.
3571 * struct sctp_assoc_value {
3572 * sctp_assoc_t assoc_id;
3573 * uint32_t assoc_value;
3574 * };
3576 * assoc_id - This parameter, indicates which association the
3577 * user is preforming an action upon. Note that if
3578 * this field's value is zero then the endpoints
3579 * default value is changed (effecting future
3580 * associations only).
3582 * assoc_value - This parameter contains the number of milliseconds
3583 * that the user is requesting the delayed ACK timer
3584 * be set to. Note that this value is defined in
3585 * the standard to be between 200 and 500 milliseconds.
3587 * Note: a value of zero will leave the value alone,
3588 * but disable SACK delay. A non-zero value will also
3589 * enable SACK delay.
3591 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3592 char __user *optval,
3593 int __user *optlen)
3595 struct sctp_assoc_value params;
3596 struct sctp_association *asoc = NULL;
3597 struct sctp_sock *sp = sctp_sk(sk);
3599 if (len != sizeof(struct sctp_assoc_value))
3600 return - EINVAL;
3602 if (copy_from_user(&params, optval, len))
3603 return -EFAULT;
3605 /* Get association, if assoc_id != 0 and the socket is a one
3606 * to many style socket, and an association was not found, then
3607 * the id was invalid.
3609 asoc = sctp_id2assoc(sk, params.assoc_id);
3610 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3611 return -EINVAL;
3613 if (asoc) {
3614 /* Fetch association values. */
3615 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3616 params.assoc_value = jiffies_to_msecs(
3617 asoc->sackdelay);
3618 else
3619 params.assoc_value = 0;
3620 } else {
3621 /* Fetch socket values. */
3622 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3623 params.assoc_value = sp->sackdelay;
3624 else
3625 params.assoc_value = 0;
3628 if (copy_to_user(optval, &params, len))
3629 return -EFAULT;
3631 if (put_user(len, optlen))
3632 return -EFAULT;
3634 return 0;
3637 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3639 * Applications can specify protocol parameters for the default association
3640 * initialization. The option name argument to setsockopt() and getsockopt()
3641 * is SCTP_INITMSG.
3643 * Setting initialization parameters is effective only on an unconnected
3644 * socket (for UDP-style sockets only future associations are effected
3645 * by the change). With TCP-style sockets, this option is inherited by
3646 * sockets derived from a listener socket.
3648 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3650 if (len != sizeof(struct sctp_initmsg))
3651 return -EINVAL;
3652 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3653 return -EFAULT;
3654 return 0;
3657 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3658 char __user *optval,
3659 int __user *optlen)
3661 sctp_assoc_t id;
3662 struct sctp_association *asoc;
3663 struct list_head *pos;
3664 int cnt = 0;
3666 if (len != sizeof(sctp_assoc_t))
3667 return -EINVAL;
3669 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3670 return -EFAULT;
3672 /* For UDP-style sockets, id specifies the association to query. */
3673 asoc = sctp_id2assoc(sk, id);
3674 if (!asoc)
3675 return -EINVAL;
3677 list_for_each(pos, &asoc->peer.transport_addr_list) {
3678 cnt ++;
3681 return cnt;
3685 * Old API for getting list of peer addresses. Does not work for 32-bit
3686 * programs running on a 64-bit kernel
3688 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3689 char __user *optval,
3690 int __user *optlen)
3692 struct sctp_association *asoc;
3693 struct list_head *pos;
3694 int cnt = 0;
3695 struct sctp_getaddrs_old getaddrs;
3696 struct sctp_transport *from;
3697 void __user *to;
3698 union sctp_addr temp;
3699 struct sctp_sock *sp = sctp_sk(sk);
3700 int addrlen;
3702 if (len != sizeof(struct sctp_getaddrs_old))
3703 return -EINVAL;
3705 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3706 return -EFAULT;
3708 if (getaddrs.addr_num <= 0) return -EINVAL;
3710 /* For UDP-style sockets, id specifies the association to query. */
3711 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3712 if (!asoc)
3713 return -EINVAL;
3715 to = (void __user *)getaddrs.addrs;
3716 list_for_each(pos, &asoc->peer.transport_addr_list) {
3717 from = list_entry(pos, struct sctp_transport, transports);
3718 memcpy(&temp, &from->ipaddr, sizeof(temp));
3719 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3720 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3721 temp.v4.sin_port = htons(temp.v4.sin_port);
3722 if (copy_to_user(to, &temp, addrlen))
3723 return -EFAULT;
3724 to += addrlen ;
3725 cnt ++;
3726 if (cnt >= getaddrs.addr_num) break;
3728 getaddrs.addr_num = cnt;
3729 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3730 return -EFAULT;
3732 return 0;
3735 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3736 char __user *optval, int __user *optlen)
3738 struct sctp_association *asoc;
3739 struct list_head *pos;
3740 int cnt = 0;
3741 struct sctp_getaddrs getaddrs;
3742 struct sctp_transport *from;
3743 void __user *to;
3744 union sctp_addr temp;
3745 struct sctp_sock *sp = sctp_sk(sk);
3746 int addrlen;
3747 size_t space_left;
3748 int bytes_copied;
3750 if (len < sizeof(struct sctp_getaddrs))
3751 return -EINVAL;
3753 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3754 return -EFAULT;
3756 /* For UDP-style sockets, id specifies the association to query. */
3757 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3758 if (!asoc)
3759 return -EINVAL;
3761 to = optval + offsetof(struct sctp_getaddrs,addrs);
3762 space_left = len - sizeof(struct sctp_getaddrs) -
3763 offsetof(struct sctp_getaddrs,addrs);
3765 list_for_each(pos, &asoc->peer.transport_addr_list) {
3766 from = list_entry(pos, struct sctp_transport, transports);
3767 memcpy(&temp, &from->ipaddr, sizeof(temp));
3768 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3769 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3770 if(space_left < addrlen)
3771 return -ENOMEM;
3772 temp.v4.sin_port = htons(temp.v4.sin_port);
3773 if (copy_to_user(to, &temp, addrlen))
3774 return -EFAULT;
3775 to += addrlen;
3776 cnt++;
3777 space_left -= addrlen;
3780 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3781 return -EFAULT;
3782 bytes_copied = ((char __user *)to) - optval;
3783 if (put_user(bytes_copied, optlen))
3784 return -EFAULT;
3786 return 0;
3789 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3790 char __user *optval,
3791 int __user *optlen)
3793 sctp_assoc_t id;
3794 struct sctp_bind_addr *bp;
3795 struct sctp_association *asoc;
3796 struct list_head *pos;
3797 struct sctp_sockaddr_entry *addr;
3798 rwlock_t *addr_lock;
3799 unsigned long flags;
3800 int cnt = 0;
3802 if (len != sizeof(sctp_assoc_t))
3803 return -EINVAL;
3805 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3806 return -EFAULT;
3809 * For UDP-style sockets, id specifies the association to query.
3810 * If the id field is set to the value '0' then the locally bound
3811 * addresses are returned without regard to any particular
3812 * association.
3814 if (0 == id) {
3815 bp = &sctp_sk(sk)->ep->base.bind_addr;
3816 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3817 } else {
3818 asoc = sctp_id2assoc(sk, id);
3819 if (!asoc)
3820 return -EINVAL;
3821 bp = &asoc->base.bind_addr;
3822 addr_lock = &asoc->base.addr_lock;
3825 sctp_read_lock(addr_lock);
3827 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3828 * addresses from the global local address list.
3830 if (sctp_list_single_entry(&bp->address_list)) {
3831 addr = list_entry(bp->address_list.next,
3832 struct sctp_sockaddr_entry, list);
3833 if (sctp_is_any(&addr->a)) {
3834 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3835 list_for_each(pos, &sctp_local_addr_list) {
3836 addr = list_entry(pos,
3837 struct sctp_sockaddr_entry,
3838 list);
3839 if ((PF_INET == sk->sk_family) &&
3840 (AF_INET6 == addr->a.sa.sa_family))
3841 continue;
3842 cnt++;
3844 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3845 flags);
3846 } else {
3847 cnt = 1;
3849 goto done;
3852 list_for_each(pos, &bp->address_list) {
3853 cnt ++;
3856 done:
3857 sctp_read_unlock(addr_lock);
3858 return cnt;
3861 /* Helper function that copies local addresses to user and returns the number
3862 * of addresses copied.
3864 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3865 void __user *to)
3867 struct list_head *pos;
3868 struct sctp_sockaddr_entry *addr;
3869 unsigned long flags;
3870 union sctp_addr temp;
3871 int cnt = 0;
3872 int addrlen;
3874 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3875 list_for_each(pos, &sctp_local_addr_list) {
3876 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3877 if ((PF_INET == sk->sk_family) &&
3878 (AF_INET6 == addr->a.sa.sa_family))
3879 continue;
3880 memcpy(&temp, &addr->a, sizeof(temp));
3881 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3882 &temp);
3883 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3884 temp.v4.sin_port = htons(port);
3885 if (copy_to_user(to, &temp, addrlen)) {
3886 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3887 flags);
3888 return -EFAULT;
3890 to += addrlen;
3891 cnt ++;
3892 if (cnt >= max_addrs) break;
3894 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3896 return cnt;
3899 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3900 void __user **to, size_t space_left)
3902 struct list_head *pos;
3903 struct sctp_sockaddr_entry *addr;
3904 unsigned long flags;
3905 union sctp_addr temp;
3906 int cnt = 0;
3907 int addrlen;
3909 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3910 list_for_each(pos, &sctp_local_addr_list) {
3911 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3912 if ((PF_INET == sk->sk_family) &&
3913 (AF_INET6 == addr->a.sa.sa_family))
3914 continue;
3915 memcpy(&temp, &addr->a, sizeof(temp));
3916 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3917 &temp);
3918 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3919 if(space_left<addrlen)
3920 return -ENOMEM;
3921 temp.v4.sin_port = htons(port);
3922 if (copy_to_user(*to, &temp, addrlen)) {
3923 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3924 flags);
3925 return -EFAULT;
3927 *to += addrlen;
3928 cnt ++;
3929 space_left -= addrlen;
3931 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3933 return cnt;
3936 /* Old API for getting list of local addresses. Does not work for 32-bit
3937 * programs running on a 64-bit kernel
3939 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3940 char __user *optval, int __user *optlen)
3942 struct sctp_bind_addr *bp;
3943 struct sctp_association *asoc;
3944 struct list_head *pos;
3945 int cnt = 0;
3946 struct sctp_getaddrs_old getaddrs;
3947 struct sctp_sockaddr_entry *addr;
3948 void __user *to;
3949 union sctp_addr temp;
3950 struct sctp_sock *sp = sctp_sk(sk);
3951 int addrlen;
3952 rwlock_t *addr_lock;
3953 int err = 0;
3955 if (len != sizeof(struct sctp_getaddrs_old))
3956 return -EINVAL;
3958 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3959 return -EFAULT;
3961 if (getaddrs.addr_num <= 0) return -EINVAL;
3963 * For UDP-style sockets, id specifies the association to query.
3964 * If the id field is set to the value '0' then the locally bound
3965 * addresses are returned without regard to any particular
3966 * association.
3968 if (0 == getaddrs.assoc_id) {
3969 bp = &sctp_sk(sk)->ep->base.bind_addr;
3970 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3971 } else {
3972 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3973 if (!asoc)
3974 return -EINVAL;
3975 bp = &asoc->base.bind_addr;
3976 addr_lock = &asoc->base.addr_lock;
3979 to = getaddrs.addrs;
3981 sctp_read_lock(addr_lock);
3983 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3984 * addresses from the global local address list.
3986 if (sctp_list_single_entry(&bp->address_list)) {
3987 addr = list_entry(bp->address_list.next,
3988 struct sctp_sockaddr_entry, list);
3989 if (sctp_is_any(&addr->a)) {
3990 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3991 getaddrs.addr_num,
3992 to);
3993 if (cnt < 0) {
3994 err = cnt;
3995 goto unlock;
3997 goto copy_getaddrs;
4001 list_for_each(pos, &bp->address_list) {
4002 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4003 memcpy(&temp, &addr->a, sizeof(temp));
4004 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4005 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4006 temp.v4.sin_port = htons(temp.v4.sin_port);
4007 if (copy_to_user(to, &temp, addrlen)) {
4008 err = -EFAULT;
4009 goto unlock;
4011 to += addrlen;
4012 cnt ++;
4013 if (cnt >= getaddrs.addr_num) break;
4016 copy_getaddrs:
4017 getaddrs.addr_num = cnt;
4018 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
4019 err = -EFAULT;
4021 unlock:
4022 sctp_read_unlock(addr_lock);
4023 return err;
4026 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4027 char __user *optval, int __user *optlen)
4029 struct sctp_bind_addr *bp;
4030 struct sctp_association *asoc;
4031 struct list_head *pos;
4032 int cnt = 0;
4033 struct sctp_getaddrs getaddrs;
4034 struct sctp_sockaddr_entry *addr;
4035 void __user *to;
4036 union sctp_addr temp;
4037 struct sctp_sock *sp = sctp_sk(sk);
4038 int addrlen;
4039 rwlock_t *addr_lock;
4040 int err = 0;
4041 size_t space_left;
4042 int bytes_copied;
4044 if (len <= sizeof(struct sctp_getaddrs))
4045 return -EINVAL;
4047 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4048 return -EFAULT;
4051 * For UDP-style sockets, id specifies the association to query.
4052 * If the id field is set to the value '0' then the locally bound
4053 * addresses are returned without regard to any particular
4054 * association.
4056 if (0 == getaddrs.assoc_id) {
4057 bp = &sctp_sk(sk)->ep->base.bind_addr;
4058 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4059 } else {
4060 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4061 if (!asoc)
4062 return -EINVAL;
4063 bp = &asoc->base.bind_addr;
4064 addr_lock = &asoc->base.addr_lock;
4067 to = optval + offsetof(struct sctp_getaddrs,addrs);
4068 space_left = len - sizeof(struct sctp_getaddrs) -
4069 offsetof(struct sctp_getaddrs,addrs);
4071 sctp_read_lock(addr_lock);
4073 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4074 * addresses from the global local address list.
4076 if (sctp_list_single_entry(&bp->address_list)) {
4077 addr = list_entry(bp->address_list.next,
4078 struct sctp_sockaddr_entry, list);
4079 if (sctp_is_any(&addr->a)) {
4080 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4081 &to, space_left);
4082 if (cnt < 0) {
4083 err = cnt;
4084 goto unlock;
4086 goto copy_getaddrs;
4090 list_for_each(pos, &bp->address_list) {
4091 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4092 memcpy(&temp, &addr->a, sizeof(temp));
4093 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4094 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4095 if(space_left < addrlen)
4096 return -ENOMEM; /*fixme: right error?*/
4097 temp.v4.sin_port = htons(temp.v4.sin_port);
4098 if (copy_to_user(to, &temp, addrlen)) {
4099 err = -EFAULT;
4100 goto unlock;
4102 to += addrlen;
4103 cnt ++;
4104 space_left -= addrlen;
4107 copy_getaddrs:
4108 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4109 return -EFAULT;
4110 bytes_copied = ((char __user *)to) - optval;
4111 if (put_user(bytes_copied, optlen))
4112 return -EFAULT;
4114 unlock:
4115 sctp_read_unlock(addr_lock);
4116 return err;
4119 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4121 * Requests that the local SCTP stack use the enclosed peer address as
4122 * the association primary. The enclosed address must be one of the
4123 * association peer's addresses.
4125 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4126 char __user *optval, int __user *optlen)
4128 struct sctp_prim prim;
4129 struct sctp_association *asoc;
4130 struct sctp_sock *sp = sctp_sk(sk);
4132 if (len != sizeof(struct sctp_prim))
4133 return -EINVAL;
4135 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4136 return -EFAULT;
4138 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4139 if (!asoc)
4140 return -EINVAL;
4142 if (!asoc->peer.primary_path)
4143 return -ENOTCONN;
4145 asoc->peer.primary_path->ipaddr.v4.sin_port =
4146 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4147 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4148 sizeof(union sctp_addr));
4149 asoc->peer.primary_path->ipaddr.v4.sin_port =
4150 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4152 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4153 (union sctp_addr *)&prim.ssp_addr);
4155 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4156 return -EFAULT;
4158 return 0;
4162 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4164 * Requests that the local endpoint set the specified Adaption Layer
4165 * Indication parameter for all future INIT and INIT-ACK exchanges.
4167 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4168 char __user *optval, int __user *optlen)
4170 struct sctp_setadaption adaption;
4172 if (len != sizeof(struct sctp_setadaption))
4173 return -EINVAL;
4175 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4176 if (copy_to_user(optval, &adaption, len))
4177 return -EFAULT;
4179 return 0;
4184 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4186 * Applications that wish to use the sendto() system call may wish to
4187 * specify a default set of parameters that would normally be supplied
4188 * through the inclusion of ancillary data. This socket option allows
4189 * such an application to set the default sctp_sndrcvinfo structure.
4192 * The application that wishes to use this socket option simply passes
4193 * in to this call the sctp_sndrcvinfo structure defined in Section
4194 * 5.2.2) The input parameters accepted by this call include
4195 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4196 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4197 * to this call if the caller is using the UDP model.
4199 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4201 static int sctp_getsockopt_default_send_param(struct sock *sk,
4202 int len, char __user *optval,
4203 int __user *optlen)
4205 struct sctp_sndrcvinfo info;
4206 struct sctp_association *asoc;
4207 struct sctp_sock *sp = sctp_sk(sk);
4209 if (len != sizeof(struct sctp_sndrcvinfo))
4210 return -EINVAL;
4211 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4212 return -EFAULT;
4214 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4215 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4216 return -EINVAL;
4218 if (asoc) {
4219 info.sinfo_stream = asoc->default_stream;
4220 info.sinfo_flags = asoc->default_flags;
4221 info.sinfo_ppid = asoc->default_ppid;
4222 info.sinfo_context = asoc->default_context;
4223 info.sinfo_timetolive = asoc->default_timetolive;
4224 } else {
4225 info.sinfo_stream = sp->default_stream;
4226 info.sinfo_flags = sp->default_flags;
4227 info.sinfo_ppid = sp->default_ppid;
4228 info.sinfo_context = sp->default_context;
4229 info.sinfo_timetolive = sp->default_timetolive;
4232 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4233 return -EFAULT;
4235 return 0;
4240 * 7.1.5 SCTP_NODELAY
4242 * Turn on/off any Nagle-like algorithm. This means that packets are
4243 * generally sent as soon as possible and no unnecessary delays are
4244 * introduced, at the cost of more packets in the network. Expects an
4245 * integer boolean flag.
4248 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4249 char __user *optval, int __user *optlen)
4251 int val;
4253 if (len < sizeof(int))
4254 return -EINVAL;
4256 len = sizeof(int);
4257 val = (sctp_sk(sk)->nodelay == 1);
4258 if (put_user(len, optlen))
4259 return -EFAULT;
4260 if (copy_to_user(optval, &val, len))
4261 return -EFAULT;
4262 return 0;
4267 * 7.1.1 SCTP_RTOINFO
4269 * The protocol parameters used to initialize and bound retransmission
4270 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4271 * and modify these parameters.
4272 * All parameters are time values, in milliseconds. A value of 0, when
4273 * modifying the parameters, indicates that the current value should not
4274 * be changed.
4277 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4278 char __user *optval,
4279 int __user *optlen) {
4280 struct sctp_rtoinfo rtoinfo;
4281 struct sctp_association *asoc;
4283 if (len != sizeof (struct sctp_rtoinfo))
4284 return -EINVAL;
4286 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4287 return -EFAULT;
4289 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4291 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4292 return -EINVAL;
4294 /* Values corresponding to the specific association. */
4295 if (asoc) {
4296 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4297 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4298 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4299 } else {
4300 /* Values corresponding to the endpoint. */
4301 struct sctp_sock *sp = sctp_sk(sk);
4303 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4304 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4305 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4308 if (put_user(len, optlen))
4309 return -EFAULT;
4311 if (copy_to_user(optval, &rtoinfo, len))
4312 return -EFAULT;
4314 return 0;
4319 * 7.1.2 SCTP_ASSOCINFO
4321 * This option is used to tune the the maximum retransmission attempts
4322 * of the association.
4323 * Returns an error if the new association retransmission value is
4324 * greater than the sum of the retransmission value of the peer.
4325 * See [SCTP] for more information.
4328 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4329 char __user *optval,
4330 int __user *optlen)
4333 struct sctp_assocparams assocparams;
4334 struct sctp_association *asoc;
4335 struct list_head *pos;
4336 int cnt = 0;
4338 if (len != sizeof (struct sctp_assocparams))
4339 return -EINVAL;
4341 if (copy_from_user(&assocparams, optval,
4342 sizeof (struct sctp_assocparams)))
4343 return -EFAULT;
4345 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4347 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4348 return -EINVAL;
4350 /* Values correspoinding to the specific association */
4351 if (asoc) {
4352 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4353 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4354 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4355 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4356 * 1000) +
4357 (asoc->cookie_life.tv_usec
4358 / 1000);
4360 list_for_each(pos, &asoc->peer.transport_addr_list) {
4361 cnt ++;
4364 assocparams.sasoc_number_peer_destinations = cnt;
4365 } else {
4366 /* Values corresponding to the endpoint */
4367 struct sctp_sock *sp = sctp_sk(sk);
4369 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4370 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4371 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4372 assocparams.sasoc_cookie_life =
4373 sp->assocparams.sasoc_cookie_life;
4374 assocparams.sasoc_number_peer_destinations =
4375 sp->assocparams.
4376 sasoc_number_peer_destinations;
4379 if (put_user(len, optlen))
4380 return -EFAULT;
4382 if (copy_to_user(optval, &assocparams, len))
4383 return -EFAULT;
4385 return 0;
4389 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4391 * This socket option is a boolean flag which turns on or off mapped V4
4392 * addresses. If this option is turned on and the socket is type
4393 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4394 * If this option is turned off, then no mapping will be done of V4
4395 * addresses and a user will receive both PF_INET6 and PF_INET type
4396 * addresses on the socket.
4398 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4399 char __user *optval, int __user *optlen)
4401 int val;
4402 struct sctp_sock *sp = sctp_sk(sk);
4404 if (len < sizeof(int))
4405 return -EINVAL;
4407 len = sizeof(int);
4408 val = sp->v4mapped;
4409 if (put_user(len, optlen))
4410 return -EFAULT;
4411 if (copy_to_user(optval, &val, len))
4412 return -EFAULT;
4414 return 0;
4418 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4420 * This socket option specifies the maximum size to put in any outgoing
4421 * SCTP chunk. If a message is larger than this size it will be
4422 * fragmented by SCTP into the specified size. Note that the underlying
4423 * SCTP implementation may fragment into smaller sized chunks when the
4424 * PMTU of the underlying association is smaller than the value set by
4425 * the user.
4427 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4428 char __user *optval, int __user *optlen)
4430 int val;
4432 if (len < sizeof(int))
4433 return -EINVAL;
4435 len = sizeof(int);
4437 val = sctp_sk(sk)->user_frag;
4438 if (put_user(len, optlen))
4439 return -EFAULT;
4440 if (copy_to_user(optval, &val, len))
4441 return -EFAULT;
4443 return 0;
4446 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4447 char __user *optval, int __user *optlen)
4449 int retval = 0;
4450 int len;
4452 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4453 sk, optname);
4455 /* I can hardly begin to describe how wrong this is. This is
4456 * so broken as to be worse than useless. The API draft
4457 * REALLY is NOT helpful here... I am not convinced that the
4458 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4459 * are at all well-founded.
4461 if (level != SOL_SCTP) {
4462 struct sctp_af *af = sctp_sk(sk)->pf->af;
4464 retval = af->getsockopt(sk, level, optname, optval, optlen);
4465 return retval;
4468 if (get_user(len, optlen))
4469 return -EFAULT;
4471 sctp_lock_sock(sk);
4473 switch (optname) {
4474 case SCTP_STATUS:
4475 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4476 break;
4477 case SCTP_DISABLE_FRAGMENTS:
4478 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4479 optlen);
4480 break;
4481 case SCTP_EVENTS:
4482 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4483 break;
4484 case SCTP_AUTOCLOSE:
4485 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4486 break;
4487 case SCTP_SOCKOPT_PEELOFF:
4488 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4489 break;
4490 case SCTP_PEER_ADDR_PARAMS:
4491 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4492 optlen);
4493 break;
4494 case SCTP_DELAYED_ACK_TIME:
4495 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4496 optlen);
4497 break;
4498 case SCTP_INITMSG:
4499 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4500 break;
4501 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4502 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4503 optlen);
4504 break;
4505 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4506 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4507 optlen);
4508 break;
4509 case SCTP_GET_PEER_ADDRS_OLD:
4510 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4511 optlen);
4512 break;
4513 case SCTP_GET_LOCAL_ADDRS_OLD:
4514 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4515 optlen);
4516 break;
4517 case SCTP_GET_PEER_ADDRS:
4518 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4519 optlen);
4520 break;
4521 case SCTP_GET_LOCAL_ADDRS:
4522 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4523 optlen);
4524 break;
4525 case SCTP_DEFAULT_SEND_PARAM:
4526 retval = sctp_getsockopt_default_send_param(sk, len,
4527 optval, optlen);
4528 break;
4529 case SCTP_PRIMARY_ADDR:
4530 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4531 break;
4532 case SCTP_NODELAY:
4533 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4534 break;
4535 case SCTP_RTOINFO:
4536 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4537 break;
4538 case SCTP_ASSOCINFO:
4539 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4540 break;
4541 case SCTP_I_WANT_MAPPED_V4_ADDR:
4542 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4543 break;
4544 case SCTP_MAXSEG:
4545 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4546 break;
4547 case SCTP_GET_PEER_ADDR_INFO:
4548 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4549 optlen);
4550 break;
4551 case SCTP_ADAPTION_LAYER:
4552 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4553 optlen);
4554 break;
4555 default:
4556 retval = -ENOPROTOOPT;
4557 break;
4560 sctp_release_sock(sk);
4561 return retval;
4564 static void sctp_hash(struct sock *sk)
4566 /* STUB */
4569 static void sctp_unhash(struct sock *sk)
4571 /* STUB */
4574 /* Check if port is acceptable. Possibly find first available port.
4576 * The port hash table (contained in the 'global' SCTP protocol storage
4577 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4578 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4579 * list (the list number is the port number hashed out, so as you
4580 * would expect from a hash function, all the ports in a given list have
4581 * such a number that hashes out to the same list number; you were
4582 * expecting that, right?); so each list has a set of ports, with a
4583 * link to the socket (struct sock) that uses it, the port number and
4584 * a fastreuse flag (FIXME: NPI ipg).
4586 static struct sctp_bind_bucket *sctp_bucket_create(
4587 struct sctp_bind_hashbucket *head, unsigned short snum);
4589 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4591 struct sctp_bind_hashbucket *head; /* hash list */
4592 struct sctp_bind_bucket *pp; /* hash list port iterator */
4593 unsigned short snum;
4594 int ret;
4596 /* NOTE: Remember to put this back to net order. */
4597 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4598 snum = addr->v4.sin_port;
4600 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4601 sctp_local_bh_disable();
4603 if (snum == 0) {
4604 /* Search for an available port.
4606 * 'sctp_port_rover' was the last port assigned, so
4607 * we start to search from 'sctp_port_rover +
4608 * 1'. What we do is first check if port 'rover' is
4609 * already in the hash table; if not, we use that; if
4610 * it is, we try next.
4612 int low = sysctl_local_port_range[0];
4613 int high = sysctl_local_port_range[1];
4614 int remaining = (high - low) + 1;
4615 int rover;
4616 int index;
4618 sctp_spin_lock(&sctp_port_alloc_lock);
4619 rover = sctp_port_rover;
4620 do {
4621 rover++;
4622 if ((rover < low) || (rover > high))
4623 rover = low;
4624 index = sctp_phashfn(rover);
4625 head = &sctp_port_hashtable[index];
4626 sctp_spin_lock(&head->lock);
4627 for (pp = head->chain; pp; pp = pp->next)
4628 if (pp->port == rover)
4629 goto next;
4630 break;
4631 next:
4632 sctp_spin_unlock(&head->lock);
4633 } while (--remaining > 0);
4634 sctp_port_rover = rover;
4635 sctp_spin_unlock(&sctp_port_alloc_lock);
4637 /* Exhausted local port range during search? */
4638 ret = 1;
4639 if (remaining <= 0)
4640 goto fail;
4642 /* OK, here is the one we will use. HEAD (the port
4643 * hash table list entry) is non-NULL and we hold it's
4644 * mutex.
4646 snum = rover;
4647 } else {
4648 /* We are given an specific port number; we verify
4649 * that it is not being used. If it is used, we will
4650 * exahust the search in the hash list corresponding
4651 * to the port number (snum) - we detect that with the
4652 * port iterator, pp being NULL.
4654 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4655 sctp_spin_lock(&head->lock);
4656 for (pp = head->chain; pp; pp = pp->next) {
4657 if (pp->port == snum)
4658 goto pp_found;
4661 pp = NULL;
4662 goto pp_not_found;
4663 pp_found:
4664 if (!hlist_empty(&pp->owner)) {
4665 /* We had a port hash table hit - there is an
4666 * available port (pp != NULL) and it is being
4667 * used by other socket (pp->owner not empty); that other
4668 * socket is going to be sk2.
4670 int reuse = sk->sk_reuse;
4671 struct sock *sk2;
4672 struct hlist_node *node;
4674 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4675 if (pp->fastreuse && sk->sk_reuse)
4676 goto success;
4678 /* Run through the list of sockets bound to the port
4679 * (pp->port) [via the pointers bind_next and
4680 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4681 * we get the endpoint they describe and run through
4682 * the endpoint's list of IP (v4 or v6) addresses,
4683 * comparing each of the addresses with the address of
4684 * the socket sk. If we find a match, then that means
4685 * that this port/socket (sk) combination are already
4686 * in an endpoint.
4688 sk_for_each_bound(sk2, node, &pp->owner) {
4689 struct sctp_endpoint *ep2;
4690 ep2 = sctp_sk(sk2)->ep;
4692 if (reuse && sk2->sk_reuse)
4693 continue;
4695 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4696 sctp_sk(sk))) {
4697 ret = (long)sk2;
4698 goto fail_unlock;
4701 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4703 pp_not_found:
4704 /* If there was a hash table miss, create a new port. */
4705 ret = 1;
4706 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4707 goto fail_unlock;
4709 /* In either case (hit or miss), make sure fastreuse is 1 only
4710 * if sk->sk_reuse is too (that is, if the caller requested
4711 * SO_REUSEADDR on this socket -sk-).
4713 if (hlist_empty(&pp->owner))
4714 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4715 else if (pp->fastreuse && !sk->sk_reuse)
4716 pp->fastreuse = 0;
4718 /* We are set, so fill up all the data in the hash table
4719 * entry, tie the socket list information with the rest of the
4720 * sockets FIXME: Blurry, NPI (ipg).
4722 success:
4723 inet_sk(sk)->num = snum;
4724 if (!sctp_sk(sk)->bind_hash) {
4725 sk_add_bind_node(sk, &pp->owner);
4726 sctp_sk(sk)->bind_hash = pp;
4728 ret = 0;
4730 fail_unlock:
4731 sctp_spin_unlock(&head->lock);
4733 fail:
4734 sctp_local_bh_enable();
4735 addr->v4.sin_port = htons(addr->v4.sin_port);
4736 return ret;
4739 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4740 * port is requested.
4742 static int sctp_get_port(struct sock *sk, unsigned short snum)
4744 long ret;
4745 union sctp_addr addr;
4746 struct sctp_af *af = sctp_sk(sk)->pf->af;
4748 /* Set up a dummy address struct from the sk. */
4749 af->from_sk(&addr, sk);
4750 addr.v4.sin_port = htons(snum);
4752 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4753 ret = sctp_get_port_local(sk, &addr);
4755 return (ret ? 1 : 0);
4759 * 3.1.3 listen() - UDP Style Syntax
4761 * By default, new associations are not accepted for UDP style sockets.
4762 * An application uses listen() to mark a socket as being able to
4763 * accept new associations.
4765 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4767 struct sctp_sock *sp = sctp_sk(sk);
4768 struct sctp_endpoint *ep = sp->ep;
4770 /* Only UDP style sockets that are not peeled off are allowed to
4771 * listen().
4773 if (!sctp_style(sk, UDP))
4774 return -EINVAL;
4776 /* If backlog is zero, disable listening. */
4777 if (!backlog) {
4778 if (sctp_sstate(sk, CLOSED))
4779 return 0;
4781 sctp_unhash_endpoint(ep);
4782 sk->sk_state = SCTP_SS_CLOSED;
4785 /* Return if we are already listening. */
4786 if (sctp_sstate(sk, LISTENING))
4787 return 0;
4790 * If a bind() or sctp_bindx() is not called prior to a listen()
4791 * call that allows new associations to be accepted, the system
4792 * picks an ephemeral port and will choose an address set equivalent
4793 * to binding with a wildcard address.
4795 * This is not currently spelled out in the SCTP sockets
4796 * extensions draft, but follows the practice as seen in TCP
4797 * sockets.
4799 if (!ep->base.bind_addr.port) {
4800 if (sctp_autobind(sk))
4801 return -EAGAIN;
4803 sk->sk_state = SCTP_SS_LISTENING;
4804 sctp_hash_endpoint(ep);
4805 return 0;
4809 * 4.1.3 listen() - TCP Style Syntax
4811 * Applications uses listen() to ready the SCTP endpoint for accepting
4812 * inbound associations.
4814 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4816 struct sctp_sock *sp = sctp_sk(sk);
4817 struct sctp_endpoint *ep = sp->ep;
4819 /* If backlog is zero, disable listening. */
4820 if (!backlog) {
4821 if (sctp_sstate(sk, CLOSED))
4822 return 0;
4824 sctp_unhash_endpoint(ep);
4825 sk->sk_state = SCTP_SS_CLOSED;
4828 if (sctp_sstate(sk, LISTENING))
4829 return 0;
4832 * If a bind() or sctp_bindx() is not called prior to a listen()
4833 * call that allows new associations to be accepted, the system
4834 * picks an ephemeral port and will choose an address set equivalent
4835 * to binding with a wildcard address.
4837 * This is not currently spelled out in the SCTP sockets
4838 * extensions draft, but follows the practice as seen in TCP
4839 * sockets.
4841 if (!ep->base.bind_addr.port) {
4842 if (sctp_autobind(sk))
4843 return -EAGAIN;
4845 sk->sk_state = SCTP_SS_LISTENING;
4846 sk->sk_max_ack_backlog = backlog;
4847 sctp_hash_endpoint(ep);
4848 return 0;
4852 * Move a socket to LISTENING state.
4854 int sctp_inet_listen(struct socket *sock, int backlog)
4856 struct sock *sk = sock->sk;
4857 struct crypto_tfm *tfm=NULL;
4858 int err = -EINVAL;
4860 if (unlikely(backlog < 0))
4861 goto out;
4863 sctp_lock_sock(sk);
4865 if (sock->state != SS_UNCONNECTED)
4866 goto out;
4868 /* Allocate HMAC for generating cookie. */
4869 if (sctp_hmac_alg) {
4870 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4871 if (!tfm) {
4872 err = -ENOSYS;
4873 goto out;
4877 switch (sock->type) {
4878 case SOCK_SEQPACKET:
4879 err = sctp_seqpacket_listen(sk, backlog);
4880 break;
4881 case SOCK_STREAM:
4882 err = sctp_stream_listen(sk, backlog);
4883 break;
4884 default:
4885 break;
4887 if (err)
4888 goto cleanup;
4890 /* Store away the transform reference. */
4891 sctp_sk(sk)->hmac = tfm;
4892 out:
4893 sctp_release_sock(sk);
4894 return err;
4895 cleanup:
4896 sctp_crypto_free_tfm(tfm);
4897 goto out;
4901 * This function is done by modeling the current datagram_poll() and the
4902 * tcp_poll(). Note that, based on these implementations, we don't
4903 * lock the socket in this function, even though it seems that,
4904 * ideally, locking or some other mechanisms can be used to ensure
4905 * the integrity of the counters (sndbuf and wmem_alloc) used
4906 * in this place. We assume that we don't need locks either until proven
4907 * otherwise.
4909 * Another thing to note is that we include the Async I/O support
4910 * here, again, by modeling the current TCP/UDP code. We don't have
4911 * a good way to test with it yet.
4913 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4915 struct sock *sk = sock->sk;
4916 struct sctp_sock *sp = sctp_sk(sk);
4917 unsigned int mask;
4919 poll_wait(file, sk->sk_sleep, wait);
4921 /* A TCP-style listening socket becomes readable when the accept queue
4922 * is not empty.
4924 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4925 return (!list_empty(&sp->ep->asocs)) ?
4926 (POLLIN | POLLRDNORM) : 0;
4928 mask = 0;
4930 /* Is there any exceptional events? */
4931 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4932 mask |= POLLERR;
4933 if (sk->sk_shutdown == SHUTDOWN_MASK)
4934 mask |= POLLHUP;
4936 /* Is it readable? Reconsider this code with TCP-style support. */
4937 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4938 (sk->sk_shutdown & RCV_SHUTDOWN))
4939 mask |= POLLIN | POLLRDNORM;
4941 /* The association is either gone or not ready. */
4942 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4943 return mask;
4945 /* Is it writable? */
4946 if (sctp_writeable(sk)) {
4947 mask |= POLLOUT | POLLWRNORM;
4948 } else {
4949 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4951 * Since the socket is not locked, the buffer
4952 * might be made available after the writeable check and
4953 * before the bit is set. This could cause a lost I/O
4954 * signal. tcp_poll() has a race breaker for this race
4955 * condition. Based on their implementation, we put
4956 * in the following code to cover it as well.
4958 if (sctp_writeable(sk))
4959 mask |= POLLOUT | POLLWRNORM;
4961 return mask;
4964 /********************************************************************
4965 * 2nd Level Abstractions
4966 ********************************************************************/
4968 static struct sctp_bind_bucket *sctp_bucket_create(
4969 struct sctp_bind_hashbucket *head, unsigned short snum)
4971 struct sctp_bind_bucket *pp;
4973 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4974 SCTP_DBG_OBJCNT_INC(bind_bucket);
4975 if (pp) {
4976 pp->port = snum;
4977 pp->fastreuse = 0;
4978 INIT_HLIST_HEAD(&pp->owner);
4979 if ((pp->next = head->chain) != NULL)
4980 pp->next->pprev = &pp->next;
4981 head->chain = pp;
4982 pp->pprev = &head->chain;
4984 return pp;
4987 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4988 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4990 if (hlist_empty(&pp->owner)) {
4991 if (pp->next)
4992 pp->next->pprev = pp->pprev;
4993 *(pp->pprev) = pp->next;
4994 kmem_cache_free(sctp_bucket_cachep, pp);
4995 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4999 /* Release this socket's reference to a local port. */
5000 static inline void __sctp_put_port(struct sock *sk)
5002 struct sctp_bind_hashbucket *head =
5003 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5004 struct sctp_bind_bucket *pp;
5006 sctp_spin_lock(&head->lock);
5007 pp = sctp_sk(sk)->bind_hash;
5008 __sk_del_bind_node(sk);
5009 sctp_sk(sk)->bind_hash = NULL;
5010 inet_sk(sk)->num = 0;
5011 sctp_bucket_destroy(pp);
5012 sctp_spin_unlock(&head->lock);
5015 void sctp_put_port(struct sock *sk)
5017 sctp_local_bh_disable();
5018 __sctp_put_port(sk);
5019 sctp_local_bh_enable();
5023 * The system picks an ephemeral port and choose an address set equivalent
5024 * to binding with a wildcard address.
5025 * One of those addresses will be the primary address for the association.
5026 * This automatically enables the multihoming capability of SCTP.
5028 static int sctp_autobind(struct sock *sk)
5030 union sctp_addr autoaddr;
5031 struct sctp_af *af;
5032 unsigned short port;
5034 /* Initialize a local sockaddr structure to INADDR_ANY. */
5035 af = sctp_sk(sk)->pf->af;
5037 port = htons(inet_sk(sk)->num);
5038 af->inaddr_any(&autoaddr, port);
5040 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5043 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5045 * From RFC 2292
5046 * 4.2 The cmsghdr Structure *
5048 * When ancillary data is sent or received, any number of ancillary data
5049 * objects can be specified by the msg_control and msg_controllen members of
5050 * the msghdr structure, because each object is preceded by
5051 * a cmsghdr structure defining the object's length (the cmsg_len member).
5052 * Historically Berkeley-derived implementations have passed only one object
5053 * at a time, but this API allows multiple objects to be
5054 * passed in a single call to sendmsg() or recvmsg(). The following example
5055 * shows two ancillary data objects in a control buffer.
5057 * |<--------------------------- msg_controllen -------------------------->|
5058 * | |
5060 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5062 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5063 * | | |
5065 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5067 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5068 * | | | | |
5070 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5071 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5073 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5075 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5079 * msg_control
5080 * points here
5082 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5083 sctp_cmsgs_t *cmsgs)
5085 struct cmsghdr *cmsg;
5087 for (cmsg = CMSG_FIRSTHDR(msg);
5088 cmsg != NULL;
5089 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5090 if (!CMSG_OK(msg, cmsg))
5091 return -EINVAL;
5093 /* Should we parse this header or ignore? */
5094 if (cmsg->cmsg_level != IPPROTO_SCTP)
5095 continue;
5097 /* Strictly check lengths following example in SCM code. */
5098 switch (cmsg->cmsg_type) {
5099 case SCTP_INIT:
5100 /* SCTP Socket API Extension
5101 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5103 * This cmsghdr structure provides information for
5104 * initializing new SCTP associations with sendmsg().
5105 * The SCTP_INITMSG socket option uses this same data
5106 * structure. This structure is not used for
5107 * recvmsg().
5109 * cmsg_level cmsg_type cmsg_data[]
5110 * ------------ ------------ ----------------------
5111 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5113 if (cmsg->cmsg_len !=
5114 CMSG_LEN(sizeof(struct sctp_initmsg)))
5115 return -EINVAL;
5116 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5117 break;
5119 case SCTP_SNDRCV:
5120 /* SCTP Socket API Extension
5121 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5123 * This cmsghdr structure specifies SCTP options for
5124 * sendmsg() and describes SCTP header information
5125 * about a received message through recvmsg().
5127 * cmsg_level cmsg_type cmsg_data[]
5128 * ------------ ------------ ----------------------
5129 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5131 if (cmsg->cmsg_len !=
5132 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5133 return -EINVAL;
5135 cmsgs->info =
5136 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5138 /* Minimally, validate the sinfo_flags. */
5139 if (cmsgs->info->sinfo_flags &
5140 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5141 SCTP_ABORT | SCTP_EOF))
5142 return -EINVAL;
5143 break;
5145 default:
5146 return -EINVAL;
5149 return 0;
5153 * Wait for a packet..
5154 * Note: This function is the same function as in core/datagram.c
5155 * with a few modifications to make lksctp work.
5157 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5159 int error;
5160 DEFINE_WAIT(wait);
5162 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5164 /* Socket errors? */
5165 error = sock_error(sk);
5166 if (error)
5167 goto out;
5169 if (!skb_queue_empty(&sk->sk_receive_queue))
5170 goto ready;
5172 /* Socket shut down? */
5173 if (sk->sk_shutdown & RCV_SHUTDOWN)
5174 goto out;
5176 /* Sequenced packets can come disconnected. If so we report the
5177 * problem.
5179 error = -ENOTCONN;
5181 /* Is there a good reason to think that we may receive some data? */
5182 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5183 goto out;
5185 /* Handle signals. */
5186 if (signal_pending(current))
5187 goto interrupted;
5189 /* Let another process have a go. Since we are going to sleep
5190 * anyway. Note: This may cause odd behaviors if the message
5191 * does not fit in the user's buffer, but this seems to be the
5192 * only way to honor MSG_DONTWAIT realistically.
5194 sctp_release_sock(sk);
5195 *timeo_p = schedule_timeout(*timeo_p);
5196 sctp_lock_sock(sk);
5198 ready:
5199 finish_wait(sk->sk_sleep, &wait);
5200 return 0;
5202 interrupted:
5203 error = sock_intr_errno(*timeo_p);
5205 out:
5206 finish_wait(sk->sk_sleep, &wait);
5207 *err = error;
5208 return error;
5211 /* Receive a datagram.
5212 * Note: This is pretty much the same routine as in core/datagram.c
5213 * with a few changes to make lksctp work.
5215 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5216 int noblock, int *err)
5218 int error;
5219 struct sk_buff *skb;
5220 long timeo;
5222 timeo = sock_rcvtimeo(sk, noblock);
5224 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5225 timeo, MAX_SCHEDULE_TIMEOUT);
5227 do {
5228 /* Again only user level code calls this function,
5229 * so nothing interrupt level
5230 * will suddenly eat the receive_queue.
5232 * Look at current nfs client by the way...
5233 * However, this function was corrent in any case. 8)
5235 if (flags & MSG_PEEK) {
5236 spin_lock_bh(&sk->sk_receive_queue.lock);
5237 skb = skb_peek(&sk->sk_receive_queue);
5238 if (skb)
5239 atomic_inc(&skb->users);
5240 spin_unlock_bh(&sk->sk_receive_queue.lock);
5241 } else {
5242 skb = skb_dequeue(&sk->sk_receive_queue);
5245 if (skb)
5246 return skb;
5248 /* Caller is allowed not to check sk->sk_err before calling. */
5249 error = sock_error(sk);
5250 if (error)
5251 goto no_packet;
5253 if (sk->sk_shutdown & RCV_SHUTDOWN)
5254 break;
5256 /* User doesn't want to wait. */
5257 error = -EAGAIN;
5258 if (!timeo)
5259 goto no_packet;
5260 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5262 return NULL;
5264 no_packet:
5265 *err = error;
5266 return NULL;
5269 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5270 static void __sctp_write_space(struct sctp_association *asoc)
5272 struct sock *sk = asoc->base.sk;
5273 struct socket *sock = sk->sk_socket;
5275 if ((sctp_wspace(asoc) > 0) && sock) {
5276 if (waitqueue_active(&asoc->wait))
5277 wake_up_interruptible(&asoc->wait);
5279 if (sctp_writeable(sk)) {
5280 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5281 wake_up_interruptible(sk->sk_sleep);
5283 /* Note that we try to include the Async I/O support
5284 * here by modeling from the current TCP/UDP code.
5285 * We have not tested with it yet.
5287 if (sock->fasync_list &&
5288 !(sk->sk_shutdown & SEND_SHUTDOWN))
5289 sock_wake_async(sock, 2, POLL_OUT);
5294 /* Do accounting for the sndbuf space.
5295 * Decrement the used sndbuf space of the corresponding association by the
5296 * data size which was just transmitted(freed).
5298 static void sctp_wfree(struct sk_buff *skb)
5300 struct sctp_association *asoc;
5301 struct sctp_chunk *chunk;
5302 struct sock *sk;
5304 /* Get the saved chunk pointer. */
5305 chunk = *((struct sctp_chunk **)(skb->cb));
5306 asoc = chunk->asoc;
5307 sk = asoc->base.sk;
5308 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5309 sizeof(struct sk_buff) +
5310 sizeof(struct sctp_chunk);
5312 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5314 sock_wfree(skb);
5315 __sctp_write_space(asoc);
5317 sctp_association_put(asoc);
5320 /* Helper function to wait for space in the sndbuf. */
5321 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5322 size_t msg_len)
5324 struct sock *sk = asoc->base.sk;
5325 int err = 0;
5326 long current_timeo = *timeo_p;
5327 DEFINE_WAIT(wait);
5329 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5330 asoc, (long)(*timeo_p), msg_len);
5332 /* Increment the association's refcnt. */
5333 sctp_association_hold(asoc);
5335 /* Wait on the association specific sndbuf space. */
5336 for (;;) {
5337 prepare_to_wait_exclusive(&asoc->wait, &wait,
5338 TASK_INTERRUPTIBLE);
5339 if (!*timeo_p)
5340 goto do_nonblock;
5341 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5342 asoc->base.dead)
5343 goto do_error;
5344 if (signal_pending(current))
5345 goto do_interrupted;
5346 if (msg_len <= sctp_wspace(asoc))
5347 break;
5349 /* Let another process have a go. Since we are going
5350 * to sleep anyway.
5352 sctp_release_sock(sk);
5353 current_timeo = schedule_timeout(current_timeo);
5354 sctp_lock_sock(sk);
5356 *timeo_p = current_timeo;
5359 out:
5360 finish_wait(&asoc->wait, &wait);
5362 /* Release the association's refcnt. */
5363 sctp_association_put(asoc);
5365 return err;
5367 do_error:
5368 err = -EPIPE;
5369 goto out;
5371 do_interrupted:
5372 err = sock_intr_errno(*timeo_p);
5373 goto out;
5375 do_nonblock:
5376 err = -EAGAIN;
5377 goto out;
5380 /* If socket sndbuf has changed, wake up all per association waiters. */
5381 void sctp_write_space(struct sock *sk)
5383 struct sctp_association *asoc;
5384 struct list_head *pos;
5386 /* Wake up the tasks in each wait queue. */
5387 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5388 asoc = list_entry(pos, struct sctp_association, asocs);
5389 __sctp_write_space(asoc);
5393 /* Is there any sndbuf space available on the socket?
5395 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5396 * associations on the same socket. For a UDP-style socket with
5397 * multiple associations, it is possible for it to be "unwriteable"
5398 * prematurely. I assume that this is acceptable because
5399 * a premature "unwriteable" is better than an accidental "writeable" which
5400 * would cause an unwanted block under certain circumstances. For the 1-1
5401 * UDP-style sockets or TCP-style sockets, this code should work.
5402 * - Daisy
5404 static int sctp_writeable(struct sock *sk)
5406 int amt = 0;
5408 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5409 if (amt < 0)
5410 amt = 0;
5411 return amt;
5414 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5415 * returns immediately with EINPROGRESS.
5417 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5419 struct sock *sk = asoc->base.sk;
5420 int err = 0;
5421 long current_timeo = *timeo_p;
5422 DEFINE_WAIT(wait);
5424 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5425 (long)(*timeo_p));
5427 /* Increment the association's refcnt. */
5428 sctp_association_hold(asoc);
5430 for (;;) {
5431 prepare_to_wait_exclusive(&asoc->wait, &wait,
5432 TASK_INTERRUPTIBLE);
5433 if (!*timeo_p)
5434 goto do_nonblock;
5435 if (sk->sk_shutdown & RCV_SHUTDOWN)
5436 break;
5437 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5438 asoc->base.dead)
5439 goto do_error;
5440 if (signal_pending(current))
5441 goto do_interrupted;
5443 if (sctp_state(asoc, ESTABLISHED))
5444 break;
5446 /* Let another process have a go. Since we are going
5447 * to sleep anyway.
5449 sctp_release_sock(sk);
5450 current_timeo = schedule_timeout(current_timeo);
5451 sctp_lock_sock(sk);
5453 *timeo_p = current_timeo;
5456 out:
5457 finish_wait(&asoc->wait, &wait);
5459 /* Release the association's refcnt. */
5460 sctp_association_put(asoc);
5462 return err;
5464 do_error:
5465 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5466 err = -ETIMEDOUT;
5467 else
5468 err = -ECONNREFUSED;
5469 goto out;
5471 do_interrupted:
5472 err = sock_intr_errno(*timeo_p);
5473 goto out;
5475 do_nonblock:
5476 err = -EINPROGRESS;
5477 goto out;
5480 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5482 struct sctp_endpoint *ep;
5483 int err = 0;
5484 DEFINE_WAIT(wait);
5486 ep = sctp_sk(sk)->ep;
5489 for (;;) {
5490 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5491 TASK_INTERRUPTIBLE);
5493 if (list_empty(&ep->asocs)) {
5494 sctp_release_sock(sk);
5495 timeo = schedule_timeout(timeo);
5496 sctp_lock_sock(sk);
5499 err = -EINVAL;
5500 if (!sctp_sstate(sk, LISTENING))
5501 break;
5503 err = 0;
5504 if (!list_empty(&ep->asocs))
5505 break;
5507 err = sock_intr_errno(timeo);
5508 if (signal_pending(current))
5509 break;
5511 err = -EAGAIN;
5512 if (!timeo)
5513 break;
5516 finish_wait(sk->sk_sleep, &wait);
5518 return err;
5521 void sctp_wait_for_close(struct sock *sk, long timeout)
5523 DEFINE_WAIT(wait);
5525 do {
5526 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5527 if (list_empty(&sctp_sk(sk)->ep->asocs))
5528 break;
5529 sctp_release_sock(sk);
5530 timeout = schedule_timeout(timeout);
5531 sctp_lock_sock(sk);
5532 } while (!signal_pending(current) && timeout);
5534 finish_wait(sk->sk_sleep, &wait);
5537 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5538 * and its messages to the newsk.
5540 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5541 struct sctp_association *assoc,
5542 sctp_socket_type_t type)
5544 struct sctp_sock *oldsp = sctp_sk(oldsk);
5545 struct sctp_sock *newsp = sctp_sk(newsk);
5546 struct sctp_bind_bucket *pp; /* hash list port iterator */
5547 struct sctp_endpoint *newep = newsp->ep;
5548 struct sk_buff *skb, *tmp;
5549 struct sctp_ulpevent *event;
5550 int flags = 0;
5552 /* Migrate socket buffer sizes and all the socket level options to the
5553 * new socket.
5555 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5556 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5557 /* Brute force copy old sctp opt. */
5558 inet_sk_copy_descendant(newsk, oldsk);
5560 /* Restore the ep value that was overwritten with the above structure
5561 * copy.
5563 newsp->ep = newep;
5564 newsp->hmac = NULL;
5566 /* Hook this new socket in to the bind_hash list. */
5567 pp = sctp_sk(oldsk)->bind_hash;
5568 sk_add_bind_node(newsk, &pp->owner);
5569 sctp_sk(newsk)->bind_hash = pp;
5570 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5572 /* Copy the bind_addr list from the original endpoint to the new
5573 * endpoint so that we can handle restarts properly
5575 if (assoc->peer.ipv4_address)
5576 flags |= SCTP_ADDR4_PEERSUPP;
5577 if (assoc->peer.ipv6_address)
5578 flags |= SCTP_ADDR6_PEERSUPP;
5579 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5580 &oldsp->ep->base.bind_addr,
5581 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5583 /* Move any messages in the old socket's receive queue that are for the
5584 * peeled off association to the new socket's receive queue.
5586 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5587 event = sctp_skb2event(skb);
5588 if (event->asoc == assoc) {
5589 sock_rfree(skb);
5590 __skb_unlink(skb, &oldsk->sk_receive_queue);
5591 __skb_queue_tail(&newsk->sk_receive_queue, skb);
5592 skb_set_owner_r(skb, newsk);
5596 /* Clean up any messages pending delivery due to partial
5597 * delivery. Three cases:
5598 * 1) No partial deliver; no work.
5599 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5600 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5602 skb_queue_head_init(&newsp->pd_lobby);
5603 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5605 if (sctp_sk(oldsk)->pd_mode) {
5606 struct sk_buff_head *queue;
5608 /* Decide which queue to move pd_lobby skbs to. */
5609 if (assoc->ulpq.pd_mode) {
5610 queue = &newsp->pd_lobby;
5611 } else
5612 queue = &newsk->sk_receive_queue;
5614 /* Walk through the pd_lobby, looking for skbs that
5615 * need moved to the new socket.
5617 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5618 event = sctp_skb2event(skb);
5619 if (event->asoc == assoc) {
5620 sock_rfree(skb);
5621 __skb_unlink(skb, &oldsp->pd_lobby);
5622 __skb_queue_tail(queue, skb);
5623 skb_set_owner_r(skb, newsk);
5627 /* Clear up any skbs waiting for the partial
5628 * delivery to finish.
5630 if (assoc->ulpq.pd_mode)
5631 sctp_clear_pd(oldsk);
5635 /* Set the type of socket to indicate that it is peeled off from the
5636 * original UDP-style socket or created with the accept() call on a
5637 * TCP-style socket..
5639 newsp->type = type;
5641 spin_lock_bh(&oldsk->sk_lock.slock);
5642 /* Migrate the backlog from oldsk to newsk. */
5643 sctp_backlog_migrate(assoc, oldsk, newsk);
5644 /* Migrate the association to the new socket. */
5645 sctp_assoc_migrate(assoc, newsk);
5646 spin_unlock_bh(&oldsk->sk_lock.slock);
5648 /* If the association on the newsk is already closed before accept()
5649 * is called, set RCV_SHUTDOWN flag.
5651 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5652 newsk->sk_shutdown |= RCV_SHUTDOWN;
5654 newsk->sk_state = SCTP_SS_ESTABLISHED;
5657 /* This proto struct describes the ULP interface for SCTP. */
5658 struct proto sctp_prot = {
5659 .name = "SCTP",
5660 .owner = THIS_MODULE,
5661 .close = sctp_close,
5662 .connect = sctp_connect,
5663 .disconnect = sctp_disconnect,
5664 .accept = sctp_accept,
5665 .ioctl = sctp_ioctl,
5666 .init = sctp_init_sock,
5667 .destroy = sctp_destroy_sock,
5668 .shutdown = sctp_shutdown,
5669 .setsockopt = sctp_setsockopt,
5670 .getsockopt = sctp_getsockopt,
5671 .sendmsg = sctp_sendmsg,
5672 .recvmsg = sctp_recvmsg,
5673 .bind = sctp_bind,
5674 .backlog_rcv = sctp_backlog_rcv,
5675 .hash = sctp_hash,
5676 .unhash = sctp_unhash,
5677 .get_port = sctp_get_port,
5678 .obj_size = sizeof(struct sctp_sock),
5681 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5682 struct proto sctpv6_prot = {
5683 .name = "SCTPv6",
5684 .owner = THIS_MODULE,
5685 .close = sctp_close,
5686 .connect = sctp_connect,
5687 .disconnect = sctp_disconnect,
5688 .accept = sctp_accept,
5689 .ioctl = sctp_ioctl,
5690 .init = sctp_init_sock,
5691 .destroy = sctp_destroy_sock,
5692 .shutdown = sctp_shutdown,
5693 .setsockopt = sctp_setsockopt,
5694 .getsockopt = sctp_getsockopt,
5695 .sendmsg = sctp_sendmsg,
5696 .recvmsg = sctp_recvmsg,
5697 .bind = sctp_bind,
5698 .backlog_rcv = sctp_backlog_rcv,
5699 .hash = sctp_hash,
5700 .unhash = sctp_unhash,
5701 .get_port = sctp_get_port,
5702 .obj_size = sizeof(struct sctp6_sock),
5704 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */