Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / sctp / socket.c
blobc9781e373f89911671f6f82cc42da528bb45d4a3
1 /* SCTP kernel 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 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 * This SCTP 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 * This SCTP implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
60 #include <linux/types.h>
61 #include <linux/kernel.h>
62 #include <linux/wait.h>
63 #include <linux/time.h>
64 #include <linux/ip.h>
65 #include <linux/capability.h>
66 #include <linux/fcntl.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <linux/crypto.h>
71 #include <net/ip.h>
72 #include <net/icmp.h>
73 #include <net/route.h>
74 #include <net/ipv6.h>
75 #include <net/inet_common.h>
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <net/sock.h>
79 #include <net/sctp/sctp.h>
80 #include <net/sctp/sm.h>
82 /* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
87 /* Forward declarations for internal helper functions. */
88 static int sctp_writeable(struct sock *sk);
89 static void sctp_wfree(struct sk_buff *skb);
90 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94 static int sctp_wait_for_accept(struct sock *sk, long timeo);
95 static void sctp_wait_for_close(struct sock *sk, long timeo);
96 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105 static int sctp_autobind(struct sock *sk);
106 static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110 extern struct kmem_cache *sctp_bucket_cachep;
111 extern int sysctl_sctp_mem[3];
112 extern int sysctl_sctp_rmem[3];
113 extern int sysctl_sctp_wmem[3];
115 static int sctp_memory_pressure;
116 static atomic_t sctp_memory_allocated;
117 static atomic_t sctp_sockets_allocated;
119 static void sctp_enter_memory_pressure(void)
121 sctp_memory_pressure = 1;
125 /* Get the sndbuf space available at the time on the association. */
126 static inline int sctp_wspace(struct sctp_association *asoc)
128 int amt;
130 if (asoc->ep->sndbuf_policy)
131 amt = asoc->sndbuf_used;
132 else
133 amt = atomic_read(&asoc->base.sk->sk_wmem_alloc);
135 if (amt >= asoc->base.sk->sk_sndbuf) {
136 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
137 amt = 0;
138 else {
139 amt = sk_stream_wspace(asoc->base.sk);
140 if (amt < 0)
141 amt = 0;
143 } else {
144 amt = asoc->base.sk->sk_sndbuf - amt;
146 return amt;
149 /* Increment the used sndbuf space count of the corresponding association by
150 * the size of the outgoing data chunk.
151 * Also, set the skb destructor for sndbuf accounting later.
153 * Since it is always 1-1 between chunk and skb, and also a new skb is always
154 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
155 * destructor in the data chunk skb for the purpose of the sndbuf space
156 * tracking.
158 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
160 struct sctp_association *asoc = chunk->asoc;
161 struct sock *sk = asoc->base.sk;
163 /* The sndbuf space is tracked per association. */
164 sctp_association_hold(asoc);
166 skb_set_owner_w(chunk->skb, sk);
168 chunk->skb->destructor = sctp_wfree;
169 /* Save the chunk pointer in skb for sctp_wfree to use later. */
170 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
172 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
173 sizeof(struct sk_buff) +
174 sizeof(struct sctp_chunk);
176 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
177 sk->sk_wmem_queued += chunk->skb->truesize;
178 sk_mem_charge(sk, chunk->skb->truesize);
181 /* Verify that this is a valid address. */
182 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
183 int len)
185 struct sctp_af *af;
187 /* Verify basic sockaddr. */
188 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
189 if (!af)
190 return -EINVAL;
192 /* Is this a valid SCTP address? */
193 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
194 return -EINVAL;
196 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
197 return -EINVAL;
199 return 0;
202 /* Look up the association by its id. If this is not a UDP-style
203 * socket, the ID field is always ignored.
205 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
207 struct sctp_association *asoc = NULL;
209 /* If this is not a UDP-style socket, assoc id should be ignored. */
210 if (!sctp_style(sk, UDP)) {
211 /* Return NULL if the socket state is not ESTABLISHED. It
212 * could be a TCP-style listening socket or a socket which
213 * hasn't yet called connect() to establish an association.
215 if (!sctp_sstate(sk, ESTABLISHED))
216 return NULL;
218 /* Get the first and the only association from the list. */
219 if (!list_empty(&sctp_sk(sk)->ep->asocs))
220 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
221 struct sctp_association, asocs);
222 return asoc;
225 /* Otherwise this is a UDP-style socket. */
226 if (!id || (id == (sctp_assoc_t)-1))
227 return NULL;
229 spin_lock_bh(&sctp_assocs_id_lock);
230 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
231 spin_unlock_bh(&sctp_assocs_id_lock);
233 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
234 return NULL;
236 return asoc;
239 /* Look up the transport from an address and an assoc id. If both address and
240 * id are specified, the associations matching the address and the id should be
241 * the same.
243 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
244 struct sockaddr_storage *addr,
245 sctp_assoc_t id)
247 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
248 struct sctp_transport *transport;
249 union sctp_addr *laddr = (union sctp_addr *)addr;
251 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
252 laddr,
253 &transport);
255 if (!addr_asoc)
256 return NULL;
258 id_asoc = sctp_id2assoc(sk, id);
259 if (id_asoc && (id_asoc != addr_asoc))
260 return NULL;
262 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
263 (union sctp_addr *)addr);
265 return transport;
268 /* API 3.1.2 bind() - UDP Style Syntax
269 * The syntax of bind() is,
271 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
273 * sd - the socket descriptor returned by socket().
274 * addr - the address structure (struct sockaddr_in or struct
275 * sockaddr_in6 [RFC 2553]),
276 * addr_len - the size of the address structure.
278 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
280 int retval = 0;
282 sctp_lock_sock(sk);
284 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
285 sk, addr, addr_len);
287 /* Disallow binding twice. */
288 if (!sctp_sk(sk)->ep->base.bind_addr.port)
289 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
290 addr_len);
291 else
292 retval = -EINVAL;
294 sctp_release_sock(sk);
296 return retval;
299 static long sctp_get_port_local(struct sock *, union sctp_addr *);
301 /* Verify this is a valid sockaddr. */
302 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
303 union sctp_addr *addr, int len)
305 struct sctp_af *af;
307 /* Check minimum size. */
308 if (len < sizeof (struct sockaddr))
309 return NULL;
311 /* Does this PF support this AF? */
312 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
313 return NULL;
315 /* If we get this far, af is valid. */
316 af = sctp_get_af_specific(addr->sa.sa_family);
318 if (len < af->sockaddr_len)
319 return NULL;
321 return af;
324 /* Bind a local address either to an endpoint or to an association. */
325 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
327 struct sctp_sock *sp = sctp_sk(sk);
328 struct sctp_endpoint *ep = sp->ep;
329 struct sctp_bind_addr *bp = &ep->base.bind_addr;
330 struct sctp_af *af;
331 unsigned short snum;
332 int ret = 0;
334 /* Common sockaddr verification. */
335 af = sctp_sockaddr_af(sp, addr, len);
336 if (!af) {
337 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
338 sk, addr, len);
339 return -EINVAL;
342 snum = ntohs(addr->v4.sin_port);
344 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
345 ", port: %d, new port: %d, len: %d)\n",
347 addr,
348 bp->port, snum,
349 len);
351 /* PF specific bind() address verification. */
352 if (!sp->pf->bind_verify(sp, addr))
353 return -EADDRNOTAVAIL;
355 /* We must either be unbound, or bind to the same port.
356 * It's OK to allow 0 ports if we are already bound.
357 * We'll just inhert an already bound port in this case
359 if (bp->port) {
360 if (!snum)
361 snum = bp->port;
362 else if (snum != bp->port) {
363 SCTP_DEBUG_PRINTK("sctp_do_bind:"
364 " New port %d does not match existing port "
365 "%d.\n", snum, bp->port);
366 return -EINVAL;
370 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
371 return -EACCES;
373 /* Make sure we are allowed to bind here.
374 * The function sctp_get_port_local() does duplicate address
375 * detection.
377 addr->v4.sin_port = htons(snum);
378 if ((ret = sctp_get_port_local(sk, addr))) {
379 if (ret == (long) sk) {
380 /* This endpoint has a conflicting address. */
381 return -EINVAL;
382 } else {
383 return -EADDRINUSE;
387 /* Refresh ephemeral port. */
388 if (!bp->port)
389 bp->port = inet_sk(sk)->num;
391 /* Add the address to the bind address list.
392 * Use GFP_ATOMIC since BHs will be disabled.
394 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
396 /* Copy back into socket for getsockname() use. */
397 if (!ret) {
398 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
399 af->to_sk_saddr(addr, sk);
402 return ret;
405 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
407 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
408 * at any one time. If a sender, after sending an ASCONF chunk, decides
409 * it needs to transfer another ASCONF Chunk, it MUST wait until the
410 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
411 * subsequent ASCONF. Note this restriction binds each side, so at any
412 * time two ASCONF may be in-transit on any given association (one sent
413 * from each endpoint).
415 static int sctp_send_asconf(struct sctp_association *asoc,
416 struct sctp_chunk *chunk)
418 int retval = 0;
420 /* If there is an outstanding ASCONF chunk, queue it for later
421 * transmission.
423 if (asoc->addip_last_asconf) {
424 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
425 goto out;
428 /* Hold the chunk until an ASCONF_ACK is received. */
429 sctp_chunk_hold(chunk);
430 retval = sctp_primitive_ASCONF(asoc, chunk);
431 if (retval)
432 sctp_chunk_free(chunk);
433 else
434 asoc->addip_last_asconf = chunk;
436 out:
437 return retval;
440 /* Add a list of addresses as bind addresses to local endpoint or
441 * association.
443 * Basically run through each address specified in the addrs/addrcnt
444 * array/length pair, determine if it is IPv6 or IPv4 and call
445 * sctp_do_bind() on it.
447 * If any of them fails, then the operation will be reversed and the
448 * ones that were added will be removed.
450 * Only sctp_setsockopt_bindx() is supposed to call this function.
452 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
454 int cnt;
455 int retval = 0;
456 void *addr_buf;
457 struct sockaddr *sa_addr;
458 struct sctp_af *af;
460 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
461 sk, addrs, addrcnt);
463 addr_buf = addrs;
464 for (cnt = 0; cnt < addrcnt; cnt++) {
465 /* The list may contain either IPv4 or IPv6 address;
466 * determine the address length for walking thru the list.
468 sa_addr = (struct sockaddr *)addr_buf;
469 af = sctp_get_af_specific(sa_addr->sa_family);
470 if (!af) {
471 retval = -EINVAL;
472 goto err_bindx_add;
475 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
476 af->sockaddr_len);
478 addr_buf += af->sockaddr_len;
480 err_bindx_add:
481 if (retval < 0) {
482 /* Failed. Cleanup the ones that have been added */
483 if (cnt > 0)
484 sctp_bindx_rem(sk, addrs, cnt);
485 return retval;
489 return retval;
492 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
493 * associations that are part of the endpoint indicating that a list of local
494 * addresses are added to the endpoint.
496 * If any of the addresses is already in the bind address list of the
497 * association, we do not send the chunk for that association. But it will not
498 * affect other associations.
500 * Only sctp_setsockopt_bindx() is supposed to call this function.
502 static int sctp_send_asconf_add_ip(struct sock *sk,
503 struct sockaddr *addrs,
504 int addrcnt)
506 struct sctp_sock *sp;
507 struct sctp_endpoint *ep;
508 struct sctp_association *asoc;
509 struct sctp_bind_addr *bp;
510 struct sctp_chunk *chunk;
511 struct sctp_sockaddr_entry *laddr;
512 union sctp_addr *addr;
513 union sctp_addr saveaddr;
514 void *addr_buf;
515 struct sctp_af *af;
516 struct list_head *pos;
517 struct list_head *p;
518 int i;
519 int retval = 0;
521 if (!sctp_addip_enable)
522 return retval;
524 sp = sctp_sk(sk);
525 ep = sp->ep;
527 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
528 __FUNCTION__, sk, addrs, addrcnt);
530 list_for_each(pos, &ep->asocs) {
531 asoc = list_entry(pos, struct sctp_association, asocs);
533 if (!asoc->peer.asconf_capable)
534 continue;
536 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
537 continue;
539 if (!sctp_state(asoc, ESTABLISHED))
540 continue;
542 /* Check if any address in the packed array of addresses is
543 * in the bind address list of the association. If so,
544 * do not send the asconf chunk to its peer, but continue with
545 * other associations.
547 addr_buf = addrs;
548 for (i = 0; i < addrcnt; i++) {
549 addr = (union sctp_addr *)addr_buf;
550 af = sctp_get_af_specific(addr->v4.sin_family);
551 if (!af) {
552 retval = -EINVAL;
553 goto out;
556 if (sctp_assoc_lookup_laddr(asoc, addr))
557 break;
559 addr_buf += af->sockaddr_len;
561 if (i < addrcnt)
562 continue;
564 /* Use the first valid address in bind addr list of
565 * association as Address Parameter of ASCONF CHUNK.
567 bp = &asoc->base.bind_addr;
568 p = bp->address_list.next;
569 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
570 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
571 addrcnt, SCTP_PARAM_ADD_IP);
572 if (!chunk) {
573 retval = -ENOMEM;
574 goto out;
577 retval = sctp_send_asconf(asoc, chunk);
578 if (retval)
579 goto out;
581 /* Add the new addresses to the bind address list with
582 * use_as_src set to 0.
584 addr_buf = addrs;
585 for (i = 0; i < addrcnt; i++) {
586 addr = (union sctp_addr *)addr_buf;
587 af = sctp_get_af_specific(addr->v4.sin_family);
588 memcpy(&saveaddr, addr, af->sockaddr_len);
589 retval = sctp_add_bind_addr(bp, &saveaddr,
590 SCTP_ADDR_NEW, GFP_ATOMIC);
591 addr_buf += af->sockaddr_len;
595 out:
596 return retval;
599 /* Remove a list of addresses from bind addresses list. Do not remove the
600 * last address.
602 * Basically run through each address specified in the addrs/addrcnt
603 * array/length pair, determine if it is IPv6 or IPv4 and call
604 * sctp_del_bind() on it.
606 * If any of them fails, then the operation will be reversed and the
607 * ones that were removed will be added back.
609 * At least one address has to be left; if only one address is
610 * available, the operation will return -EBUSY.
612 * Only sctp_setsockopt_bindx() is supposed to call this function.
614 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
616 struct sctp_sock *sp = sctp_sk(sk);
617 struct sctp_endpoint *ep = sp->ep;
618 int cnt;
619 struct sctp_bind_addr *bp = &ep->base.bind_addr;
620 int retval = 0;
621 void *addr_buf;
622 union sctp_addr *sa_addr;
623 struct sctp_af *af;
625 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
626 sk, addrs, addrcnt);
628 addr_buf = addrs;
629 for (cnt = 0; cnt < addrcnt; cnt++) {
630 /* If the bind address list is empty or if there is only one
631 * bind address, there is nothing more to be removed (we need
632 * at least one address here).
634 if (list_empty(&bp->address_list) ||
635 (sctp_list_single_entry(&bp->address_list))) {
636 retval = -EBUSY;
637 goto err_bindx_rem;
640 sa_addr = (union sctp_addr *)addr_buf;
641 af = sctp_get_af_specific(sa_addr->sa.sa_family);
642 if (!af) {
643 retval = -EINVAL;
644 goto err_bindx_rem;
647 if (!af->addr_valid(sa_addr, sp, NULL)) {
648 retval = -EADDRNOTAVAIL;
649 goto err_bindx_rem;
652 if (sa_addr->v4.sin_port != htons(bp->port)) {
653 retval = -EINVAL;
654 goto err_bindx_rem;
657 /* FIXME - There is probably a need to check if sk->sk_saddr and
658 * sk->sk_rcv_addr are currently set to one of the addresses to
659 * be removed. This is something which needs to be looked into
660 * when we are fixing the outstanding issues with multi-homing
661 * socket routing and failover schemes. Refer to comments in
662 * sctp_do_bind(). -daisy
664 retval = sctp_del_bind_addr(bp, sa_addr);
666 addr_buf += af->sockaddr_len;
667 err_bindx_rem:
668 if (retval < 0) {
669 /* Failed. Add the ones that has been removed back */
670 if (cnt > 0)
671 sctp_bindx_add(sk, addrs, cnt);
672 return retval;
676 return retval;
679 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
680 * the associations that are part of the endpoint indicating that a list of
681 * local addresses are removed from the endpoint.
683 * If any of the addresses is already in the bind address list of the
684 * association, we do not send the chunk for that association. But it will not
685 * affect other associations.
687 * Only sctp_setsockopt_bindx() is supposed to call this function.
689 static int sctp_send_asconf_del_ip(struct sock *sk,
690 struct sockaddr *addrs,
691 int addrcnt)
693 struct sctp_sock *sp;
694 struct sctp_endpoint *ep;
695 struct sctp_association *asoc;
696 struct sctp_transport *transport;
697 struct sctp_bind_addr *bp;
698 struct sctp_chunk *chunk;
699 union sctp_addr *laddr;
700 void *addr_buf;
701 struct sctp_af *af;
702 struct list_head *pos, *pos1;
703 struct sctp_sockaddr_entry *saddr;
704 int i;
705 int retval = 0;
707 if (!sctp_addip_enable)
708 return retval;
710 sp = sctp_sk(sk);
711 ep = sp->ep;
713 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
714 __FUNCTION__, sk, addrs, addrcnt);
716 list_for_each(pos, &ep->asocs) {
717 asoc = list_entry(pos, struct sctp_association, asocs);
719 if (!asoc->peer.asconf_capable)
720 continue;
722 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
723 continue;
725 if (!sctp_state(asoc, ESTABLISHED))
726 continue;
728 /* Check if any address in the packed array of addresses is
729 * not present in the bind address list of the association.
730 * If so, do not send the asconf chunk to its peer, but
731 * continue with other associations.
733 addr_buf = addrs;
734 for (i = 0; i < addrcnt; i++) {
735 laddr = (union sctp_addr *)addr_buf;
736 af = sctp_get_af_specific(laddr->v4.sin_family);
737 if (!af) {
738 retval = -EINVAL;
739 goto out;
742 if (!sctp_assoc_lookup_laddr(asoc, laddr))
743 break;
745 addr_buf += af->sockaddr_len;
747 if (i < addrcnt)
748 continue;
750 /* Find one address in the association's bind address list
751 * that is not in the packed array of addresses. This is to
752 * make sure that we do not delete all the addresses in the
753 * association.
755 bp = &asoc->base.bind_addr;
756 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
757 addrcnt, sp);
758 if (!laddr)
759 continue;
761 /* We do not need RCU protection throughout this loop
762 * because this is done under a socket lock from the
763 * setsockopt call.
765 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
766 SCTP_PARAM_DEL_IP);
767 if (!chunk) {
768 retval = -ENOMEM;
769 goto out;
772 /* Reset use_as_src flag for the addresses in the bind address
773 * list that are to be deleted.
775 addr_buf = addrs;
776 for (i = 0; i < addrcnt; i++) {
777 laddr = (union sctp_addr *)addr_buf;
778 af = sctp_get_af_specific(laddr->v4.sin_family);
779 list_for_each_entry(saddr, &bp->address_list, list) {
780 if (sctp_cmp_addr_exact(&saddr->a, laddr))
781 saddr->state = SCTP_ADDR_DEL;
783 addr_buf += af->sockaddr_len;
786 /* Update the route and saddr entries for all the transports
787 * as some of the addresses in the bind address list are
788 * about to be deleted and cannot be used as source addresses.
790 list_for_each(pos1, &asoc->peer.transport_addr_list) {
791 transport = list_entry(pos1, struct sctp_transport,
792 transports);
793 dst_release(transport->dst);
794 sctp_transport_route(transport, NULL,
795 sctp_sk(asoc->base.sk));
798 retval = sctp_send_asconf(asoc, chunk);
800 out:
801 return retval;
804 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
806 * API 8.1
807 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
808 * int flags);
810 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
811 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
812 * or IPv6 addresses.
814 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
815 * Section 3.1.2 for this usage.
817 * addrs is a pointer to an array of one or more socket addresses. Each
818 * address is contained in its appropriate structure (i.e. struct
819 * sockaddr_in or struct sockaddr_in6) the family of the address type
820 * must be used to distinguish the address length (note that this
821 * representation is termed a "packed array" of addresses). The caller
822 * specifies the number of addresses in the array with addrcnt.
824 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
825 * -1, and sets errno to the appropriate error code.
827 * For SCTP, the port given in each socket address must be the same, or
828 * sctp_bindx() will fail, setting errno to EINVAL.
830 * The flags parameter is formed from the bitwise OR of zero or more of
831 * the following currently defined flags:
833 * SCTP_BINDX_ADD_ADDR
835 * SCTP_BINDX_REM_ADDR
837 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
838 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
839 * addresses from the association. The two flags are mutually exclusive;
840 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
841 * not remove all addresses from an association; sctp_bindx() will
842 * reject such an attempt with EINVAL.
844 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
845 * additional addresses with an endpoint after calling bind(). Or use
846 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
847 * socket is associated with so that no new association accepted will be
848 * associated with those addresses. If the endpoint supports dynamic
849 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
850 * endpoint to send the appropriate message to the peer to change the
851 * peers address lists.
853 * Adding and removing addresses from a connected association is
854 * optional functionality. Implementations that do not support this
855 * functionality should return EOPNOTSUPP.
857 * Basically do nothing but copying the addresses from user to kernel
858 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
859 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
860 * from userspace.
862 * We don't use copy_from_user() for optimization: we first do the
863 * sanity checks (buffer size -fast- and access check-healthy
864 * pointer); if all of those succeed, then we can alloc the memory
865 * (expensive operation) needed to copy the data to kernel. Then we do
866 * the copying without checking the user space area
867 * (__copy_from_user()).
869 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
870 * it.
872 * sk The sk of the socket
873 * addrs The pointer to the addresses in user land
874 * addrssize Size of the addrs buffer
875 * op Operation to perform (add or remove, see the flags of
876 * sctp_bindx)
878 * Returns 0 if ok, <0 errno code on error.
880 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
881 struct sockaddr __user *addrs,
882 int addrs_size, int op)
884 struct sockaddr *kaddrs;
885 int err;
886 int addrcnt = 0;
887 int walk_size = 0;
888 struct sockaddr *sa_addr;
889 void *addr_buf;
890 struct sctp_af *af;
892 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
893 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
895 if (unlikely(addrs_size <= 0))
896 return -EINVAL;
898 /* Check the user passed a healthy pointer. */
899 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
900 return -EFAULT;
902 /* Alloc space for the address array in kernel memory. */
903 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
904 if (unlikely(!kaddrs))
905 return -ENOMEM;
907 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
908 kfree(kaddrs);
909 return -EFAULT;
912 /* Walk through the addrs buffer and count the number of addresses. */
913 addr_buf = kaddrs;
914 while (walk_size < addrs_size) {
915 sa_addr = (struct sockaddr *)addr_buf;
916 af = sctp_get_af_specific(sa_addr->sa_family);
918 /* If the address family is not supported or if this address
919 * causes the address buffer to overflow return EINVAL.
921 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
922 kfree(kaddrs);
923 return -EINVAL;
925 addrcnt++;
926 addr_buf += af->sockaddr_len;
927 walk_size += af->sockaddr_len;
930 /* Do the work. */
931 switch (op) {
932 case SCTP_BINDX_ADD_ADDR:
933 err = sctp_bindx_add(sk, kaddrs, addrcnt);
934 if (err)
935 goto out;
936 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
937 break;
939 case SCTP_BINDX_REM_ADDR:
940 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
941 if (err)
942 goto out;
943 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
944 break;
946 default:
947 err = -EINVAL;
948 break;
951 out:
952 kfree(kaddrs);
954 return err;
957 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
959 * Common routine for handling connect() and sctp_connectx().
960 * Connect will come in with just a single address.
962 static int __sctp_connect(struct sock* sk,
963 struct sockaddr *kaddrs,
964 int addrs_size)
966 struct sctp_sock *sp;
967 struct sctp_endpoint *ep;
968 struct sctp_association *asoc = NULL;
969 struct sctp_association *asoc2;
970 struct sctp_transport *transport;
971 union sctp_addr to;
972 struct sctp_af *af;
973 sctp_scope_t scope;
974 long timeo;
975 int err = 0;
976 int addrcnt = 0;
977 int walk_size = 0;
978 union sctp_addr *sa_addr = NULL;
979 void *addr_buf;
980 unsigned short port;
981 unsigned int f_flags = 0;
983 sp = sctp_sk(sk);
984 ep = sp->ep;
986 /* connect() cannot be done on a socket that is already in ESTABLISHED
987 * state - UDP-style peeled off socket or a TCP-style socket that
988 * is already connected.
989 * It cannot be done even on a TCP-style listening socket.
991 if (sctp_sstate(sk, ESTABLISHED) ||
992 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
993 err = -EISCONN;
994 goto out_free;
997 /* Walk through the addrs buffer and count the number of addresses. */
998 addr_buf = kaddrs;
999 while (walk_size < addrs_size) {
1000 sa_addr = (union sctp_addr *)addr_buf;
1001 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1002 port = ntohs(sa_addr->v4.sin_port);
1004 /* If the address family is not supported or if this address
1005 * causes the address buffer to overflow return EINVAL.
1007 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1008 err = -EINVAL;
1009 goto out_free;
1012 /* Save current address so we can work with it */
1013 memcpy(&to, sa_addr, af->sockaddr_len);
1015 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1016 if (err)
1017 goto out_free;
1019 /* Make sure the destination port is correctly set
1020 * in all addresses.
1022 if (asoc && asoc->peer.port && asoc->peer.port != port)
1023 goto out_free;
1026 /* Check if there already is a matching association on the
1027 * endpoint (other than the one created here).
1029 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1030 if (asoc2 && asoc2 != asoc) {
1031 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1032 err = -EISCONN;
1033 else
1034 err = -EALREADY;
1035 goto out_free;
1038 /* If we could not find a matching association on the endpoint,
1039 * make sure that there is no peeled-off association matching
1040 * the peer address even on another socket.
1042 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1043 err = -EADDRNOTAVAIL;
1044 goto out_free;
1047 if (!asoc) {
1048 /* If a bind() or sctp_bindx() is not called prior to
1049 * an sctp_connectx() call, the system picks an
1050 * ephemeral port and will choose an address set
1051 * equivalent to binding with a wildcard address.
1053 if (!ep->base.bind_addr.port) {
1054 if (sctp_autobind(sk)) {
1055 err = -EAGAIN;
1056 goto out_free;
1058 } else {
1060 * If an unprivileged user inherits a 1-many
1061 * style socket with open associations on a
1062 * privileged port, it MAY be permitted to
1063 * accept new associations, but it SHOULD NOT
1064 * be permitted to open new associations.
1066 if (ep->base.bind_addr.port < PROT_SOCK &&
1067 !capable(CAP_NET_BIND_SERVICE)) {
1068 err = -EACCES;
1069 goto out_free;
1073 scope = sctp_scope(&to);
1074 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1075 if (!asoc) {
1076 err = -ENOMEM;
1077 goto out_free;
1081 /* Prime the peer's transport structures. */
1082 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1083 SCTP_UNKNOWN);
1084 if (!transport) {
1085 err = -ENOMEM;
1086 goto out_free;
1089 addrcnt++;
1090 addr_buf += af->sockaddr_len;
1091 walk_size += af->sockaddr_len;
1094 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1095 if (err < 0) {
1096 goto out_free;
1099 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1100 if (err < 0) {
1101 goto out_free;
1104 /* Initialize sk's dport and daddr for getpeername() */
1105 inet_sk(sk)->dport = htons(asoc->peer.port);
1106 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1107 af->to_sk_daddr(sa_addr, sk);
1108 sk->sk_err = 0;
1110 /* in-kernel sockets don't generally have a file allocated to them
1111 * if all they do is call sock_create_kern().
1113 if (sk->sk_socket->file)
1114 f_flags = sk->sk_socket->file->f_flags;
1116 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1118 err = sctp_wait_for_connect(asoc, &timeo);
1120 /* Don't free association on exit. */
1121 asoc = NULL;
1123 out_free:
1125 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1126 " kaddrs: %p err: %d\n",
1127 asoc, kaddrs, err);
1128 if (asoc)
1129 sctp_association_free(asoc);
1130 return err;
1133 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1135 * API 8.9
1136 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1138 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1139 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1140 * or IPv6 addresses.
1142 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1143 * Section 3.1.2 for this usage.
1145 * addrs is a pointer to an array of one or more socket addresses. Each
1146 * address is contained in its appropriate structure (i.e. struct
1147 * sockaddr_in or struct sockaddr_in6) the family of the address type
1148 * must be used to distengish the address length (note that this
1149 * representation is termed a "packed array" of addresses). The caller
1150 * specifies the number of addresses in the array with addrcnt.
1152 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1153 * -1, and sets errno to the appropriate error code.
1155 * For SCTP, the port given in each socket address must be the same, or
1156 * sctp_connectx() will fail, setting errno to EINVAL.
1158 * An application can use sctp_connectx to initiate an association with
1159 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1160 * allows a caller to specify multiple addresses at which a peer can be
1161 * reached. The way the SCTP stack uses the list of addresses to set up
1162 * the association is implementation dependant. This function only
1163 * specifies that the stack will try to make use of all the addresses in
1164 * the list when needed.
1166 * Note that the list of addresses passed in is only used for setting up
1167 * the association. It does not necessarily equal the set of addresses
1168 * the peer uses for the resulting association. If the caller wants to
1169 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1170 * retrieve them after the association has been set up.
1172 * Basically do nothing but copying the addresses from user to kernel
1173 * land and invoking either sctp_connectx(). This is used for tunneling
1174 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1176 * We don't use copy_from_user() for optimization: we first do the
1177 * sanity checks (buffer size -fast- and access check-healthy
1178 * pointer); if all of those succeed, then we can alloc the memory
1179 * (expensive operation) needed to copy the data to kernel. Then we do
1180 * the copying without checking the user space area
1181 * (__copy_from_user()).
1183 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1184 * it.
1186 * sk The sk of the socket
1187 * addrs The pointer to the addresses in user land
1188 * addrssize Size of the addrs buffer
1190 * Returns 0 if ok, <0 errno code on error.
1192 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1193 struct sockaddr __user *addrs,
1194 int addrs_size)
1196 int err = 0;
1197 struct sockaddr *kaddrs;
1199 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1200 __FUNCTION__, sk, addrs, addrs_size);
1202 if (unlikely(addrs_size <= 0))
1203 return -EINVAL;
1205 /* Check the user passed a healthy pointer. */
1206 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1207 return -EFAULT;
1209 /* Alloc space for the address array in kernel memory. */
1210 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1211 if (unlikely(!kaddrs))
1212 return -ENOMEM;
1214 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1215 err = -EFAULT;
1216 } else {
1217 err = __sctp_connect(sk, kaddrs, addrs_size);
1220 kfree(kaddrs);
1221 return err;
1224 /* API 3.1.4 close() - UDP Style Syntax
1225 * Applications use close() to perform graceful shutdown (as described in
1226 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1227 * by a UDP-style socket.
1229 * The syntax is
1231 * ret = close(int sd);
1233 * sd - the socket descriptor of the associations to be closed.
1235 * To gracefully shutdown a specific association represented by the
1236 * UDP-style socket, an application should use the sendmsg() call,
1237 * passing no user data, but including the appropriate flag in the
1238 * ancillary data (see Section xxxx).
1240 * If sd in the close() call is a branched-off socket representing only
1241 * one association, the shutdown is performed on that association only.
1243 * 4.1.6 close() - TCP Style Syntax
1245 * Applications use close() to gracefully close down an association.
1247 * The syntax is:
1249 * int close(int sd);
1251 * sd - the socket descriptor of the association to be closed.
1253 * After an application calls close() on a socket descriptor, no further
1254 * socket operations will succeed on that descriptor.
1256 * API 7.1.4 SO_LINGER
1258 * An application using the TCP-style socket can use this option to
1259 * perform the SCTP ABORT primitive. The linger option structure is:
1261 * struct linger {
1262 * int l_onoff; // option on/off
1263 * int l_linger; // linger time
1264 * };
1266 * To enable the option, set l_onoff to 1. If the l_linger value is set
1267 * to 0, calling close() is the same as the ABORT primitive. If the
1268 * value is set to a negative value, the setsockopt() call will return
1269 * an error. If the value is set to a positive value linger_time, the
1270 * close() can be blocked for at most linger_time ms. If the graceful
1271 * shutdown phase does not finish during this period, close() will
1272 * return but the graceful shutdown phase continues in the system.
1274 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1276 struct sctp_endpoint *ep;
1277 struct sctp_association *asoc;
1278 struct list_head *pos, *temp;
1280 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1282 sctp_lock_sock(sk);
1283 sk->sk_shutdown = SHUTDOWN_MASK;
1285 ep = sctp_sk(sk)->ep;
1287 /* Walk all associations on an endpoint. */
1288 list_for_each_safe(pos, temp, &ep->asocs) {
1289 asoc = list_entry(pos, struct sctp_association, asocs);
1291 if (sctp_style(sk, TCP)) {
1292 /* A closed association can still be in the list if
1293 * it belongs to a TCP-style listening socket that is
1294 * not yet accepted. If so, free it. If not, send an
1295 * ABORT or SHUTDOWN based on the linger options.
1297 if (sctp_state(asoc, CLOSED)) {
1298 sctp_unhash_established(asoc);
1299 sctp_association_free(asoc);
1300 continue;
1304 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1305 struct sctp_chunk *chunk;
1307 chunk = sctp_make_abort_user(asoc, NULL, 0);
1308 if (chunk)
1309 sctp_primitive_ABORT(asoc, chunk);
1310 } else
1311 sctp_primitive_SHUTDOWN(asoc, NULL);
1314 /* Clean up any skbs sitting on the receive queue. */
1315 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1316 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1318 /* On a TCP-style socket, block for at most linger_time if set. */
1319 if (sctp_style(sk, TCP) && timeout)
1320 sctp_wait_for_close(sk, timeout);
1322 /* This will run the backlog queue. */
1323 sctp_release_sock(sk);
1325 /* Supposedly, no process has access to the socket, but
1326 * the net layers still may.
1328 sctp_local_bh_disable();
1329 sctp_bh_lock_sock(sk);
1331 /* Hold the sock, since sk_common_release() will put sock_put()
1332 * and we have just a little more cleanup.
1334 sock_hold(sk);
1335 sk_common_release(sk);
1337 sctp_bh_unlock_sock(sk);
1338 sctp_local_bh_enable();
1340 sock_put(sk);
1342 SCTP_DBG_OBJCNT_DEC(sock);
1345 /* Handle EPIPE error. */
1346 static int sctp_error(struct sock *sk, int flags, int err)
1348 if (err == -EPIPE)
1349 err = sock_error(sk) ? : -EPIPE;
1350 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1351 send_sig(SIGPIPE, current, 0);
1352 return err;
1355 /* API 3.1.3 sendmsg() - UDP Style Syntax
1357 * An application uses sendmsg() and recvmsg() calls to transmit data to
1358 * and receive data from its peer.
1360 * ssize_t sendmsg(int socket, const struct msghdr *message,
1361 * int flags);
1363 * socket - the socket descriptor of the endpoint.
1364 * message - pointer to the msghdr structure which contains a single
1365 * user message and possibly some ancillary data.
1367 * See Section 5 for complete description of the data
1368 * structures.
1370 * flags - flags sent or received with the user message, see Section
1371 * 5 for complete description of the flags.
1373 * Note: This function could use a rewrite especially when explicit
1374 * connect support comes in.
1376 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1378 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1380 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1381 struct msghdr *msg, size_t msg_len)
1383 struct sctp_sock *sp;
1384 struct sctp_endpoint *ep;
1385 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1386 struct sctp_transport *transport, *chunk_tp;
1387 struct sctp_chunk *chunk;
1388 union sctp_addr to;
1389 struct sockaddr *msg_name = NULL;
1390 struct sctp_sndrcvinfo default_sinfo = { 0 };
1391 struct sctp_sndrcvinfo *sinfo;
1392 struct sctp_initmsg *sinit;
1393 sctp_assoc_t associd = 0;
1394 sctp_cmsgs_t cmsgs = { NULL };
1395 int err;
1396 sctp_scope_t scope;
1397 long timeo;
1398 __u16 sinfo_flags = 0;
1399 struct sctp_datamsg *datamsg;
1400 struct list_head *pos;
1401 int msg_flags = msg->msg_flags;
1403 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1404 sk, msg, msg_len);
1406 err = 0;
1407 sp = sctp_sk(sk);
1408 ep = sp->ep;
1410 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1412 /* We cannot send a message over a TCP-style listening socket. */
1413 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1414 err = -EPIPE;
1415 goto out_nounlock;
1418 /* Parse out the SCTP CMSGs. */
1419 err = sctp_msghdr_parse(msg, &cmsgs);
1421 if (err) {
1422 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1423 goto out_nounlock;
1426 /* Fetch the destination address for this packet. This
1427 * address only selects the association--it is not necessarily
1428 * the address we will send to.
1429 * For a peeled-off socket, msg_name is ignored.
1431 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1432 int msg_namelen = msg->msg_namelen;
1434 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1435 msg_namelen);
1436 if (err)
1437 return err;
1439 if (msg_namelen > sizeof(to))
1440 msg_namelen = sizeof(to);
1441 memcpy(&to, msg->msg_name, msg_namelen);
1442 msg_name = msg->msg_name;
1445 sinfo = cmsgs.info;
1446 sinit = cmsgs.init;
1448 /* Did the user specify SNDRCVINFO? */
1449 if (sinfo) {
1450 sinfo_flags = sinfo->sinfo_flags;
1451 associd = sinfo->sinfo_assoc_id;
1454 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1455 msg_len, sinfo_flags);
1457 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1458 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1459 err = -EINVAL;
1460 goto out_nounlock;
1463 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1464 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1465 * If SCTP_ABORT is set, the message length could be non zero with
1466 * the msg_iov set to the user abort reason.
1468 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1469 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1470 err = -EINVAL;
1471 goto out_nounlock;
1474 /* If SCTP_ADDR_OVER is set, there must be an address
1475 * specified in msg_name.
1477 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1478 err = -EINVAL;
1479 goto out_nounlock;
1482 transport = NULL;
1484 SCTP_DEBUG_PRINTK("About to look up association.\n");
1486 sctp_lock_sock(sk);
1488 /* If a msg_name has been specified, assume this is to be used. */
1489 if (msg_name) {
1490 /* Look for a matching association on the endpoint. */
1491 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1492 if (!asoc) {
1493 /* If we could not find a matching association on the
1494 * endpoint, make sure that it is not a TCP-style
1495 * socket that already has an association or there is
1496 * no peeled-off association on another socket.
1498 if ((sctp_style(sk, TCP) &&
1499 sctp_sstate(sk, ESTABLISHED)) ||
1500 sctp_endpoint_is_peeled_off(ep, &to)) {
1501 err = -EADDRNOTAVAIL;
1502 goto out_unlock;
1505 } else {
1506 asoc = sctp_id2assoc(sk, associd);
1507 if (!asoc) {
1508 err = -EPIPE;
1509 goto out_unlock;
1513 if (asoc) {
1514 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1516 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1517 * socket that has an association in CLOSED state. This can
1518 * happen when an accepted socket has an association that is
1519 * already CLOSED.
1521 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1522 err = -EPIPE;
1523 goto out_unlock;
1526 if (sinfo_flags & SCTP_EOF) {
1527 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1528 asoc);
1529 sctp_primitive_SHUTDOWN(asoc, NULL);
1530 err = 0;
1531 goto out_unlock;
1533 if (sinfo_flags & SCTP_ABORT) {
1535 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1536 if (!chunk) {
1537 err = -ENOMEM;
1538 goto out_unlock;
1541 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1542 sctp_primitive_ABORT(asoc, chunk);
1543 err = 0;
1544 goto out_unlock;
1548 /* Do we need to create the association? */
1549 if (!asoc) {
1550 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1552 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1553 err = -EINVAL;
1554 goto out_unlock;
1557 /* Check for invalid stream against the stream counts,
1558 * either the default or the user specified stream counts.
1560 if (sinfo) {
1561 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1562 /* Check against the defaults. */
1563 if (sinfo->sinfo_stream >=
1564 sp->initmsg.sinit_num_ostreams) {
1565 err = -EINVAL;
1566 goto out_unlock;
1568 } else {
1569 /* Check against the requested. */
1570 if (sinfo->sinfo_stream >=
1571 sinit->sinit_num_ostreams) {
1572 err = -EINVAL;
1573 goto out_unlock;
1579 * API 3.1.2 bind() - UDP Style Syntax
1580 * If a bind() or sctp_bindx() is not called prior to a
1581 * sendmsg() call that initiates a new association, the
1582 * system picks an ephemeral port and will choose an address
1583 * set equivalent to binding with a wildcard address.
1585 if (!ep->base.bind_addr.port) {
1586 if (sctp_autobind(sk)) {
1587 err = -EAGAIN;
1588 goto out_unlock;
1590 } else {
1592 * If an unprivileged user inherits a one-to-many
1593 * style socket with open associations on a privileged
1594 * port, it MAY be permitted to accept new associations,
1595 * but it SHOULD NOT be permitted to open new
1596 * associations.
1598 if (ep->base.bind_addr.port < PROT_SOCK &&
1599 !capable(CAP_NET_BIND_SERVICE)) {
1600 err = -EACCES;
1601 goto out_unlock;
1605 scope = sctp_scope(&to);
1606 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1607 if (!new_asoc) {
1608 err = -ENOMEM;
1609 goto out_unlock;
1611 asoc = new_asoc;
1613 /* If the SCTP_INIT ancillary data is specified, set all
1614 * the association init values accordingly.
1616 if (sinit) {
1617 if (sinit->sinit_num_ostreams) {
1618 asoc->c.sinit_num_ostreams =
1619 sinit->sinit_num_ostreams;
1621 if (sinit->sinit_max_instreams) {
1622 asoc->c.sinit_max_instreams =
1623 sinit->sinit_max_instreams;
1625 if (sinit->sinit_max_attempts) {
1626 asoc->max_init_attempts
1627 = sinit->sinit_max_attempts;
1629 if (sinit->sinit_max_init_timeo) {
1630 asoc->max_init_timeo =
1631 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1635 /* Prime the peer's transport structures. */
1636 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1637 if (!transport) {
1638 err = -ENOMEM;
1639 goto out_free;
1641 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1642 if (err < 0) {
1643 err = -ENOMEM;
1644 goto out_free;
1648 /* ASSERT: we have a valid association at this point. */
1649 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1651 if (!sinfo) {
1652 /* If the user didn't specify SNDRCVINFO, make up one with
1653 * some defaults.
1655 default_sinfo.sinfo_stream = asoc->default_stream;
1656 default_sinfo.sinfo_flags = asoc->default_flags;
1657 default_sinfo.sinfo_ppid = asoc->default_ppid;
1658 default_sinfo.sinfo_context = asoc->default_context;
1659 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1660 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1661 sinfo = &default_sinfo;
1664 /* API 7.1.7, the sndbuf size per association bounds the
1665 * maximum size of data that can be sent in a single send call.
1667 if (msg_len > sk->sk_sndbuf) {
1668 err = -EMSGSIZE;
1669 goto out_free;
1672 if (asoc->pmtu_pending)
1673 sctp_assoc_pending_pmtu(asoc);
1675 /* If fragmentation is disabled and the message length exceeds the
1676 * association fragmentation point, return EMSGSIZE. The I-D
1677 * does not specify what this error is, but this looks like
1678 * a great fit.
1680 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1681 err = -EMSGSIZE;
1682 goto out_free;
1685 if (sinfo) {
1686 /* Check for invalid stream. */
1687 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1688 err = -EINVAL;
1689 goto out_free;
1693 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1694 if (!sctp_wspace(asoc)) {
1695 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1696 if (err)
1697 goto out_free;
1700 /* If an address is passed with the sendto/sendmsg call, it is used
1701 * to override the primary destination address in the TCP model, or
1702 * when SCTP_ADDR_OVER flag is set in the UDP model.
1704 if ((sctp_style(sk, TCP) && msg_name) ||
1705 (sinfo_flags & SCTP_ADDR_OVER)) {
1706 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1707 if (!chunk_tp) {
1708 err = -EINVAL;
1709 goto out_free;
1711 } else
1712 chunk_tp = NULL;
1714 /* Auto-connect, if we aren't connected already. */
1715 if (sctp_state(asoc, CLOSED)) {
1716 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1717 if (err < 0)
1718 goto out_free;
1719 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1722 /* Break the message into multiple chunks of maximum size. */
1723 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1724 if (!datamsg) {
1725 err = -ENOMEM;
1726 goto out_free;
1729 /* Now send the (possibly) fragmented message. */
1730 list_for_each(pos, &datamsg->chunks) {
1731 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1732 sctp_datamsg_track(chunk);
1734 /* Do accounting for the write space. */
1735 sctp_set_owner_w(chunk);
1737 chunk->transport = chunk_tp;
1739 /* Send it to the lower layers. Note: all chunks
1740 * must either fail or succeed. The lower layer
1741 * works that way today. Keep it that way or this
1742 * breaks.
1744 err = sctp_primitive_SEND(asoc, chunk);
1745 /* Did the lower layer accept the chunk? */
1746 if (err)
1747 sctp_chunk_free(chunk);
1748 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1751 sctp_datamsg_free(datamsg);
1752 if (err)
1753 goto out_free;
1754 else
1755 err = msg_len;
1757 /* If we are already past ASSOCIATE, the lower
1758 * layers are responsible for association cleanup.
1760 goto out_unlock;
1762 out_free:
1763 if (new_asoc)
1764 sctp_association_free(asoc);
1765 out_unlock:
1766 sctp_release_sock(sk);
1768 out_nounlock:
1769 return sctp_error(sk, msg_flags, err);
1771 #if 0
1772 do_sock_err:
1773 if (msg_len)
1774 err = msg_len;
1775 else
1776 err = sock_error(sk);
1777 goto out;
1779 do_interrupted:
1780 if (msg_len)
1781 err = msg_len;
1782 goto out;
1783 #endif /* 0 */
1786 /* This is an extended version of skb_pull() that removes the data from the
1787 * start of a skb even when data is spread across the list of skb's in the
1788 * frag_list. len specifies the total amount of data that needs to be removed.
1789 * when 'len' bytes could be removed from the skb, it returns 0.
1790 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1791 * could not be removed.
1793 static int sctp_skb_pull(struct sk_buff *skb, int len)
1795 struct sk_buff *list;
1796 int skb_len = skb_headlen(skb);
1797 int rlen;
1799 if (len <= skb_len) {
1800 __skb_pull(skb, len);
1801 return 0;
1803 len -= skb_len;
1804 __skb_pull(skb, skb_len);
1806 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1807 rlen = sctp_skb_pull(list, len);
1808 skb->len -= (len-rlen);
1809 skb->data_len -= (len-rlen);
1811 if (!rlen)
1812 return 0;
1814 len = rlen;
1817 return len;
1820 /* API 3.1.3 recvmsg() - UDP Style Syntax
1822 * ssize_t recvmsg(int socket, struct msghdr *message,
1823 * int flags);
1825 * socket - the socket descriptor of the endpoint.
1826 * message - pointer to the msghdr structure which contains a single
1827 * user message and possibly some ancillary data.
1829 * See Section 5 for complete description of the data
1830 * structures.
1832 * flags - flags sent or received with the user message, see Section
1833 * 5 for complete description of the flags.
1835 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1837 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1838 struct msghdr *msg, size_t len, int noblock,
1839 int flags, int *addr_len)
1841 struct sctp_ulpevent *event = NULL;
1842 struct sctp_sock *sp = sctp_sk(sk);
1843 struct sk_buff *skb;
1844 int copied;
1845 int err = 0;
1846 int skb_len;
1848 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1849 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1850 "len", len, "knoblauch", noblock,
1851 "flags", flags, "addr_len", addr_len);
1853 sctp_lock_sock(sk);
1855 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1856 err = -ENOTCONN;
1857 goto out;
1860 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1861 if (!skb)
1862 goto out;
1864 /* Get the total length of the skb including any skb's in the
1865 * frag_list.
1867 skb_len = skb->len;
1869 copied = skb_len;
1870 if (copied > len)
1871 copied = len;
1873 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1875 event = sctp_skb2event(skb);
1877 if (err)
1878 goto out_free;
1880 sock_recv_timestamp(msg, sk, skb);
1881 if (sctp_ulpevent_is_notification(event)) {
1882 msg->msg_flags |= MSG_NOTIFICATION;
1883 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1884 } else {
1885 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1888 /* Check if we allow SCTP_SNDRCVINFO. */
1889 if (sp->subscribe.sctp_data_io_event)
1890 sctp_ulpevent_read_sndrcvinfo(event, msg);
1891 #if 0
1892 /* FIXME: we should be calling IP/IPv6 layers. */
1893 if (sk->sk_protinfo.af_inet.cmsg_flags)
1894 ip_cmsg_recv(msg, skb);
1895 #endif
1897 err = copied;
1899 /* If skb's length exceeds the user's buffer, update the skb and
1900 * push it back to the receive_queue so that the next call to
1901 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1903 if (skb_len > copied) {
1904 msg->msg_flags &= ~MSG_EOR;
1905 if (flags & MSG_PEEK)
1906 goto out_free;
1907 sctp_skb_pull(skb, copied);
1908 skb_queue_head(&sk->sk_receive_queue, skb);
1910 /* When only partial message is copied to the user, increase
1911 * rwnd by that amount. If all the data in the skb is read,
1912 * rwnd is updated when the event is freed.
1914 if (!sctp_ulpevent_is_notification(event))
1915 sctp_assoc_rwnd_increase(event->asoc, copied);
1916 goto out;
1917 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1918 (event->msg_flags & MSG_EOR))
1919 msg->msg_flags |= MSG_EOR;
1920 else
1921 msg->msg_flags &= ~MSG_EOR;
1923 out_free:
1924 if (flags & MSG_PEEK) {
1925 /* Release the skb reference acquired after peeking the skb in
1926 * sctp_skb_recv_datagram().
1928 kfree_skb(skb);
1929 } else {
1930 /* Free the event which includes releasing the reference to
1931 * the owner of the skb, freeing the skb and updating the
1932 * rwnd.
1934 sctp_ulpevent_free(event);
1936 out:
1937 sctp_release_sock(sk);
1938 return err;
1941 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1943 * This option is a on/off flag. If enabled no SCTP message
1944 * fragmentation will be performed. Instead if a message being sent
1945 * exceeds the current PMTU size, the message will NOT be sent and
1946 * instead a error will be indicated to the user.
1948 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1949 char __user *optval, int optlen)
1951 int val;
1953 if (optlen < sizeof(int))
1954 return -EINVAL;
1956 if (get_user(val, (int __user *)optval))
1957 return -EFAULT;
1959 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1961 return 0;
1964 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1965 int optlen)
1967 <<<<<<< HEAD:net/sctp/socket.c
1968 if (optlen != sizeof(struct sctp_event_subscribe))
1969 =======
1970 if (optlen > sizeof(struct sctp_event_subscribe))
1971 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
1972 return -EINVAL;
1973 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1974 return -EFAULT;
1975 return 0;
1978 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1980 * This socket option is applicable to the UDP-style socket only. When
1981 * set it will cause associations that are idle for more than the
1982 * specified number of seconds to automatically close. An association
1983 * being idle is defined an association that has NOT sent or received
1984 * user data. The special value of '0' indicates that no automatic
1985 * close of any associations should be performed. The option expects an
1986 * integer defining the number of seconds of idle time before an
1987 * association is closed.
1989 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1990 int optlen)
1992 struct sctp_sock *sp = sctp_sk(sk);
1994 /* Applicable to UDP-style socket only */
1995 if (sctp_style(sk, TCP))
1996 return -EOPNOTSUPP;
1997 if (optlen != sizeof(int))
1998 return -EINVAL;
1999 if (copy_from_user(&sp->autoclose, optval, optlen))
2000 return -EFAULT;
2002 return 0;
2005 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2007 * Applications can enable or disable heartbeats for any peer address of
2008 * an association, modify an address's heartbeat interval, force a
2009 * heartbeat to be sent immediately, and adjust the address's maximum
2010 * number of retransmissions sent before an address is considered
2011 * unreachable. The following structure is used to access and modify an
2012 * address's parameters:
2014 * struct sctp_paddrparams {
2015 * sctp_assoc_t spp_assoc_id;
2016 * struct sockaddr_storage spp_address;
2017 * uint32_t spp_hbinterval;
2018 * uint16_t spp_pathmaxrxt;
2019 * uint32_t spp_pathmtu;
2020 * uint32_t spp_sackdelay;
2021 * uint32_t spp_flags;
2022 * };
2024 * spp_assoc_id - (one-to-many style socket) This is filled in the
2025 * application, and identifies the association for
2026 * this query.
2027 * spp_address - This specifies which address is of interest.
2028 * spp_hbinterval - This contains the value of the heartbeat interval,
2029 * in milliseconds. If a value of zero
2030 * is present in this field then no changes are to
2031 * be made to this parameter.
2032 * spp_pathmaxrxt - This contains the maximum number of
2033 * retransmissions before this address shall be
2034 * considered unreachable. If a value of zero
2035 * is present in this field then no changes are to
2036 * be made to this parameter.
2037 * spp_pathmtu - When Path MTU discovery is disabled the value
2038 * specified here will be the "fixed" path mtu.
2039 * Note that if the spp_address field is empty
2040 * then all associations on this address will
2041 * have this fixed path mtu set upon them.
2043 * spp_sackdelay - When delayed sack is enabled, this value specifies
2044 * the number of milliseconds that sacks will be delayed
2045 * for. This value will apply to all addresses of an
2046 * association if the spp_address field is empty. Note
2047 * also, that if delayed sack is enabled and this
2048 * value is set to 0, no change is made to the last
2049 * recorded delayed sack timer value.
2051 * spp_flags - These flags are used to control various features
2052 * on an association. The flag field may contain
2053 * zero or more of the following options.
2055 * SPP_HB_ENABLE - Enable heartbeats on the
2056 * specified address. Note that if the address
2057 * field is empty all addresses for the association
2058 * have heartbeats enabled upon them.
2060 * SPP_HB_DISABLE - Disable heartbeats on the
2061 * speicifed address. Note that if the address
2062 * field is empty all addresses for the association
2063 * will have their heartbeats disabled. Note also
2064 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2065 * mutually exclusive, only one of these two should
2066 * be specified. Enabling both fields will have
2067 * undetermined results.
2069 * SPP_HB_DEMAND - Request a user initiated heartbeat
2070 * to be made immediately.
2072 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2073 * heartbeat delayis to be set to the value of 0
2074 * milliseconds.
2076 * SPP_PMTUD_ENABLE - This field will enable PMTU
2077 * discovery upon the specified address. Note that
2078 * if the address feild is empty then all addresses
2079 * on the association are effected.
2081 * SPP_PMTUD_DISABLE - This field will disable PMTU
2082 * discovery upon the specified address. Note that
2083 * if the address feild is empty then all addresses
2084 * on the association are effected. Not also that
2085 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2086 * exclusive. Enabling both will have undetermined
2087 * results.
2089 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2090 * on delayed sack. The time specified in spp_sackdelay
2091 * is used to specify the sack delay for this address. Note
2092 * that if spp_address is empty then all addresses will
2093 * enable delayed sack and take on the sack delay
2094 * value specified in spp_sackdelay.
2095 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2096 * off delayed sack. If the spp_address field is blank then
2097 * delayed sack is disabled for the entire association. Note
2098 * also that this field is mutually exclusive to
2099 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2100 * results.
2102 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2103 struct sctp_transport *trans,
2104 struct sctp_association *asoc,
2105 struct sctp_sock *sp,
2106 int hb_change,
2107 int pmtud_change,
2108 int sackdelay_change)
2110 int error;
2112 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2113 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2114 if (error)
2115 return error;
2118 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2119 * this field is ignored. Note also that a value of zero indicates
2120 * the current setting should be left unchanged.
2122 if (params->spp_flags & SPP_HB_ENABLE) {
2124 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2125 * set. This lets us use 0 value when this flag
2126 * is set.
2128 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2129 params->spp_hbinterval = 0;
2131 if (params->spp_hbinterval ||
2132 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2133 if (trans) {
2134 trans->hbinterval =
2135 msecs_to_jiffies(params->spp_hbinterval);
2136 } else if (asoc) {
2137 asoc->hbinterval =
2138 msecs_to_jiffies(params->spp_hbinterval);
2139 } else {
2140 sp->hbinterval = params->spp_hbinterval;
2145 if (hb_change) {
2146 if (trans) {
2147 trans->param_flags =
2148 (trans->param_flags & ~SPP_HB) | hb_change;
2149 } else if (asoc) {
2150 asoc->param_flags =
2151 (asoc->param_flags & ~SPP_HB) | hb_change;
2152 } else {
2153 sp->param_flags =
2154 (sp->param_flags & ~SPP_HB) | hb_change;
2158 /* When Path MTU discovery is disabled the value specified here will
2159 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2160 * include the flag SPP_PMTUD_DISABLE for this field to have any
2161 * effect).
2163 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2164 if (trans) {
2165 trans->pathmtu = params->spp_pathmtu;
2166 sctp_assoc_sync_pmtu(asoc);
2167 } else if (asoc) {
2168 asoc->pathmtu = params->spp_pathmtu;
2169 sctp_frag_point(sp, params->spp_pathmtu);
2170 } else {
2171 sp->pathmtu = params->spp_pathmtu;
2175 if (pmtud_change) {
2176 if (trans) {
2177 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2178 (params->spp_flags & SPP_PMTUD_ENABLE);
2179 trans->param_flags =
2180 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2181 if (update) {
2182 sctp_transport_pmtu(trans);
2183 sctp_assoc_sync_pmtu(asoc);
2185 } else if (asoc) {
2186 asoc->param_flags =
2187 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2188 } else {
2189 sp->param_flags =
2190 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2194 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2195 * value of this field is ignored. Note also that a value of zero
2196 * indicates the current setting should be left unchanged.
2198 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2199 if (trans) {
2200 trans->sackdelay =
2201 msecs_to_jiffies(params->spp_sackdelay);
2202 } else if (asoc) {
2203 asoc->sackdelay =
2204 msecs_to_jiffies(params->spp_sackdelay);
2205 } else {
2206 sp->sackdelay = params->spp_sackdelay;
2210 if (sackdelay_change) {
2211 if (trans) {
2212 trans->param_flags =
2213 (trans->param_flags & ~SPP_SACKDELAY) |
2214 sackdelay_change;
2215 } else if (asoc) {
2216 asoc->param_flags =
2217 (asoc->param_flags & ~SPP_SACKDELAY) |
2218 sackdelay_change;
2219 } else {
2220 sp->param_flags =
2221 (sp->param_flags & ~SPP_SACKDELAY) |
2222 sackdelay_change;
2226 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2227 * of this field is ignored. Note also that a value of zero
2228 * indicates the current setting should be left unchanged.
2230 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
2231 if (trans) {
2232 trans->pathmaxrxt = params->spp_pathmaxrxt;
2233 } else if (asoc) {
2234 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2235 } else {
2236 sp->pathmaxrxt = params->spp_pathmaxrxt;
2240 return 0;
2243 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2244 char __user *optval, int optlen)
2246 struct sctp_paddrparams params;
2247 struct sctp_transport *trans = NULL;
2248 struct sctp_association *asoc = NULL;
2249 struct sctp_sock *sp = sctp_sk(sk);
2250 int error;
2251 int hb_change, pmtud_change, sackdelay_change;
2253 if (optlen != sizeof(struct sctp_paddrparams))
2254 return - EINVAL;
2256 if (copy_from_user(&params, optval, optlen))
2257 return -EFAULT;
2259 /* Validate flags and value parameters. */
2260 hb_change = params.spp_flags & SPP_HB;
2261 pmtud_change = params.spp_flags & SPP_PMTUD;
2262 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2264 if (hb_change == SPP_HB ||
2265 pmtud_change == SPP_PMTUD ||
2266 sackdelay_change == SPP_SACKDELAY ||
2267 params.spp_sackdelay > 500 ||
2268 (params.spp_pathmtu
2269 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2270 return -EINVAL;
2272 /* If an address other than INADDR_ANY is specified, and
2273 * no transport is found, then the request is invalid.
2275 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2276 trans = sctp_addr_id2transport(sk, &params.spp_address,
2277 params.spp_assoc_id);
2278 if (!trans)
2279 return -EINVAL;
2282 /* Get association, if assoc_id != 0 and the socket is a one
2283 * to many style socket, and an association was not found, then
2284 * the id was invalid.
2286 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2287 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2288 return -EINVAL;
2290 /* Heartbeat demand can only be sent on a transport or
2291 * association, but not a socket.
2293 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2294 return -EINVAL;
2296 /* Process parameters. */
2297 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2298 hb_change, pmtud_change,
2299 sackdelay_change);
2301 if (error)
2302 return error;
2304 /* If changes are for association, also apply parameters to each
2305 * transport.
2307 if (!trans && asoc) {
2308 struct list_head *pos;
2310 list_for_each(pos, &asoc->peer.transport_addr_list) {
2311 trans = list_entry(pos, struct sctp_transport,
2312 transports);
2313 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2314 hb_change, pmtud_change,
2315 sackdelay_change);
2319 return 0;
2322 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2324 * This options will get or set the delayed ack timer. The time is set
2325 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2326 * endpoints default delayed ack timer value. If the assoc_id field is
2327 * non-zero, then the set or get effects the specified association.
2329 * struct sctp_assoc_value {
2330 * sctp_assoc_t assoc_id;
2331 * uint32_t assoc_value;
2332 * };
2334 * assoc_id - This parameter, indicates which association the
2335 * user is preforming an action upon. Note that if
2336 * this field's value is zero then the endpoints
2337 * default value is changed (effecting future
2338 * associations only).
2340 * assoc_value - This parameter contains the number of milliseconds
2341 * that the user is requesting the delayed ACK timer
2342 * be set to. Note that this value is defined in
2343 * the standard to be between 200 and 500 milliseconds.
2345 * Note: a value of zero will leave the value alone,
2346 * but disable SACK delay. A non-zero value will also
2347 * enable SACK delay.
2350 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2351 char __user *optval, int optlen)
2353 struct sctp_assoc_value params;
2354 struct sctp_transport *trans = NULL;
2355 struct sctp_association *asoc = NULL;
2356 struct sctp_sock *sp = sctp_sk(sk);
2358 if (optlen != sizeof(struct sctp_assoc_value))
2359 return - EINVAL;
2361 if (copy_from_user(&params, optval, optlen))
2362 return -EFAULT;
2364 /* Validate value parameter. */
2365 if (params.assoc_value > 500)
2366 return -EINVAL;
2368 /* Get association, if assoc_id != 0 and the socket is a one
2369 * to many style socket, and an association was not found, then
2370 * the id was invalid.
2372 asoc = sctp_id2assoc(sk, params.assoc_id);
2373 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2374 return -EINVAL;
2376 if (params.assoc_value) {
2377 if (asoc) {
2378 asoc->sackdelay =
2379 msecs_to_jiffies(params.assoc_value);
2380 asoc->param_flags =
2381 (asoc->param_flags & ~SPP_SACKDELAY) |
2382 SPP_SACKDELAY_ENABLE;
2383 } else {
2384 sp->sackdelay = params.assoc_value;
2385 sp->param_flags =
2386 (sp->param_flags & ~SPP_SACKDELAY) |
2387 SPP_SACKDELAY_ENABLE;
2389 } else {
2390 if (asoc) {
2391 asoc->param_flags =
2392 (asoc->param_flags & ~SPP_SACKDELAY) |
2393 SPP_SACKDELAY_DISABLE;
2394 } else {
2395 sp->param_flags =
2396 (sp->param_flags & ~SPP_SACKDELAY) |
2397 SPP_SACKDELAY_DISABLE;
2401 /* If change is for association, also apply to each transport. */
2402 if (asoc) {
2403 struct list_head *pos;
2405 list_for_each(pos, &asoc->peer.transport_addr_list) {
2406 trans = list_entry(pos, struct sctp_transport,
2407 transports);
2408 if (params.assoc_value) {
2409 trans->sackdelay =
2410 msecs_to_jiffies(params.assoc_value);
2411 trans->param_flags =
2412 (trans->param_flags & ~SPP_SACKDELAY) |
2413 SPP_SACKDELAY_ENABLE;
2414 } else {
2415 trans->param_flags =
2416 (trans->param_flags & ~SPP_SACKDELAY) |
2417 SPP_SACKDELAY_DISABLE;
2422 return 0;
2425 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2427 * Applications can specify protocol parameters for the default association
2428 * initialization. The option name argument to setsockopt() and getsockopt()
2429 * is SCTP_INITMSG.
2431 * Setting initialization parameters is effective only on an unconnected
2432 * socket (for UDP-style sockets only future associations are effected
2433 * by the change). With TCP-style sockets, this option is inherited by
2434 * sockets derived from a listener socket.
2436 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2438 struct sctp_initmsg sinit;
2439 struct sctp_sock *sp = sctp_sk(sk);
2441 if (optlen != sizeof(struct sctp_initmsg))
2442 return -EINVAL;
2443 if (copy_from_user(&sinit, optval, optlen))
2444 return -EFAULT;
2446 if (sinit.sinit_num_ostreams)
2447 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2448 if (sinit.sinit_max_instreams)
2449 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2450 if (sinit.sinit_max_attempts)
2451 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2452 if (sinit.sinit_max_init_timeo)
2453 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2455 return 0;
2459 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2461 * Applications that wish to use the sendto() system call may wish to
2462 * specify a default set of parameters that would normally be supplied
2463 * through the inclusion of ancillary data. This socket option allows
2464 * such an application to set the default sctp_sndrcvinfo structure.
2465 * The application that wishes to use this socket option simply passes
2466 * in to this call the sctp_sndrcvinfo structure defined in Section
2467 * 5.2.2) The input parameters accepted by this call include
2468 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2469 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2470 * to this call if the caller is using the UDP model.
2472 static int sctp_setsockopt_default_send_param(struct sock *sk,
2473 char __user *optval, int optlen)
2475 struct sctp_sndrcvinfo info;
2476 struct sctp_association *asoc;
2477 struct sctp_sock *sp = sctp_sk(sk);
2479 if (optlen != sizeof(struct sctp_sndrcvinfo))
2480 return -EINVAL;
2481 if (copy_from_user(&info, optval, optlen))
2482 return -EFAULT;
2484 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2485 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2486 return -EINVAL;
2488 if (asoc) {
2489 asoc->default_stream = info.sinfo_stream;
2490 asoc->default_flags = info.sinfo_flags;
2491 asoc->default_ppid = info.sinfo_ppid;
2492 asoc->default_context = info.sinfo_context;
2493 asoc->default_timetolive = info.sinfo_timetolive;
2494 } else {
2495 sp->default_stream = info.sinfo_stream;
2496 sp->default_flags = info.sinfo_flags;
2497 sp->default_ppid = info.sinfo_ppid;
2498 sp->default_context = info.sinfo_context;
2499 sp->default_timetolive = info.sinfo_timetolive;
2502 return 0;
2505 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2507 * Requests that the local SCTP stack use the enclosed peer address as
2508 * the association primary. The enclosed address must be one of the
2509 * association peer's addresses.
2511 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2512 int optlen)
2514 struct sctp_prim prim;
2515 struct sctp_transport *trans;
2517 if (optlen != sizeof(struct sctp_prim))
2518 return -EINVAL;
2520 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2521 return -EFAULT;
2523 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2524 if (!trans)
2525 return -EINVAL;
2527 sctp_assoc_set_primary(trans->asoc, trans);
2529 return 0;
2533 * 7.1.5 SCTP_NODELAY
2535 * Turn on/off any Nagle-like algorithm. This means that packets are
2536 * generally sent as soon as possible and no unnecessary delays are
2537 * introduced, at the cost of more packets in the network. Expects an
2538 * integer boolean flag.
2540 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2541 int optlen)
2543 int val;
2545 if (optlen < sizeof(int))
2546 return -EINVAL;
2547 if (get_user(val, (int __user *)optval))
2548 return -EFAULT;
2550 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2551 return 0;
2556 * 7.1.1 SCTP_RTOINFO
2558 * The protocol parameters used to initialize and bound retransmission
2559 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2560 * and modify these parameters.
2561 * All parameters are time values, in milliseconds. A value of 0, when
2562 * modifying the parameters, indicates that the current value should not
2563 * be changed.
2566 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2567 struct sctp_rtoinfo rtoinfo;
2568 struct sctp_association *asoc;
2570 if (optlen != sizeof (struct sctp_rtoinfo))
2571 return -EINVAL;
2573 if (copy_from_user(&rtoinfo, optval, optlen))
2574 return -EFAULT;
2576 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2578 /* Set the values to the specific association */
2579 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2580 return -EINVAL;
2582 if (asoc) {
2583 if (rtoinfo.srto_initial != 0)
2584 asoc->rto_initial =
2585 msecs_to_jiffies(rtoinfo.srto_initial);
2586 if (rtoinfo.srto_max != 0)
2587 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2588 if (rtoinfo.srto_min != 0)
2589 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2590 } else {
2591 /* If there is no association or the association-id = 0
2592 * set the values to the endpoint.
2594 struct sctp_sock *sp = sctp_sk(sk);
2596 if (rtoinfo.srto_initial != 0)
2597 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2598 if (rtoinfo.srto_max != 0)
2599 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2600 if (rtoinfo.srto_min != 0)
2601 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2604 return 0;
2609 * 7.1.2 SCTP_ASSOCINFO
2611 * This option is used to tune the maximum retransmission attempts
2612 * of the association.
2613 * Returns an error if the new association retransmission value is
2614 * greater than the sum of the retransmission value of the peer.
2615 * See [SCTP] for more information.
2618 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2621 struct sctp_assocparams assocparams;
2622 struct sctp_association *asoc;
2624 if (optlen != sizeof(struct sctp_assocparams))
2625 return -EINVAL;
2626 if (copy_from_user(&assocparams, optval, optlen))
2627 return -EFAULT;
2629 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2631 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2632 return -EINVAL;
2634 /* Set the values to the specific association */
2635 if (asoc) {
2636 if (assocparams.sasoc_asocmaxrxt != 0) {
2637 __u32 path_sum = 0;
2638 int paths = 0;
2639 struct list_head *pos;
2640 struct sctp_transport *peer_addr;
2642 list_for_each(pos, &asoc->peer.transport_addr_list) {
2643 peer_addr = list_entry(pos,
2644 struct sctp_transport,
2645 transports);
2646 path_sum += peer_addr->pathmaxrxt;
2647 paths++;
2650 /* Only validate asocmaxrxt if we have more then
2651 * one path/transport. We do this because path
2652 * retransmissions are only counted when we have more
2653 * then one path.
2655 if (paths > 1 &&
2656 assocparams.sasoc_asocmaxrxt > path_sum)
2657 return -EINVAL;
2659 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2662 if (assocparams.sasoc_cookie_life != 0) {
2663 asoc->cookie_life.tv_sec =
2664 assocparams.sasoc_cookie_life / 1000;
2665 asoc->cookie_life.tv_usec =
2666 (assocparams.sasoc_cookie_life % 1000)
2667 * 1000;
2669 } else {
2670 /* Set the values to the endpoint */
2671 struct sctp_sock *sp = sctp_sk(sk);
2673 if (assocparams.sasoc_asocmaxrxt != 0)
2674 sp->assocparams.sasoc_asocmaxrxt =
2675 assocparams.sasoc_asocmaxrxt;
2676 if (assocparams.sasoc_cookie_life != 0)
2677 sp->assocparams.sasoc_cookie_life =
2678 assocparams.sasoc_cookie_life;
2680 return 0;
2684 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2686 * This socket option is a boolean flag which turns on or off mapped V4
2687 * addresses. If this option is turned on and the socket is type
2688 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2689 * If this option is turned off, then no mapping will be done of V4
2690 * addresses and a user will receive both PF_INET6 and PF_INET type
2691 * addresses on the socket.
2693 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2695 int val;
2696 struct sctp_sock *sp = sctp_sk(sk);
2698 if (optlen < sizeof(int))
2699 return -EINVAL;
2700 if (get_user(val, (int __user *)optval))
2701 return -EFAULT;
2702 if (val)
2703 sp->v4mapped = 1;
2704 else
2705 sp->v4mapped = 0;
2707 return 0;
2711 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2713 * This socket option specifies the maximum size to put in any outgoing
2714 * SCTP chunk. If a message is larger than this size it will be
2715 * fragmented by SCTP into the specified size. Note that the underlying
2716 * SCTP implementation may fragment into smaller sized chunks when the
2717 * PMTU of the underlying association is smaller than the value set by
2718 * the user.
2720 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2722 struct sctp_association *asoc;
2723 struct list_head *pos;
2724 struct sctp_sock *sp = sctp_sk(sk);
2725 int val;
2727 if (optlen < sizeof(int))
2728 return -EINVAL;
2729 if (get_user(val, (int __user *)optval))
2730 return -EFAULT;
2731 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2732 return -EINVAL;
2733 sp->user_frag = val;
2735 /* Update the frag_point of the existing associations. */
2736 list_for_each(pos, &(sp->ep->asocs)) {
2737 asoc = list_entry(pos, struct sctp_association, asocs);
2738 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2741 return 0;
2746 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2748 * Requests that the peer mark the enclosed address as the association
2749 * primary. The enclosed address must be one of the association's
2750 * locally bound addresses. The following structure is used to make a
2751 * set primary request:
2753 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2754 int optlen)
2756 struct sctp_sock *sp;
2757 struct sctp_endpoint *ep;
2758 struct sctp_association *asoc = NULL;
2759 struct sctp_setpeerprim prim;
2760 struct sctp_chunk *chunk;
2761 int err;
2763 sp = sctp_sk(sk);
2764 ep = sp->ep;
2766 if (!sctp_addip_enable)
2767 return -EPERM;
2769 if (optlen != sizeof(struct sctp_setpeerprim))
2770 return -EINVAL;
2772 if (copy_from_user(&prim, optval, optlen))
2773 return -EFAULT;
2775 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2776 if (!asoc)
2777 return -EINVAL;
2779 if (!asoc->peer.asconf_capable)
2780 return -EPERM;
2782 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2783 return -EPERM;
2785 if (!sctp_state(asoc, ESTABLISHED))
2786 return -ENOTCONN;
2788 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2789 return -EADDRNOTAVAIL;
2791 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2792 chunk = sctp_make_asconf_set_prim(asoc,
2793 (union sctp_addr *)&prim.sspp_addr);
2794 if (!chunk)
2795 return -ENOMEM;
2797 err = sctp_send_asconf(asoc, chunk);
2799 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2801 return err;
2804 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2805 int optlen)
2807 struct sctp_setadaptation adaptation;
2809 if (optlen != sizeof(struct sctp_setadaptation))
2810 return -EINVAL;
2811 if (copy_from_user(&adaptation, optval, optlen))
2812 return -EFAULT;
2814 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2816 return 0;
2820 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2822 * The context field in the sctp_sndrcvinfo structure is normally only
2823 * used when a failed message is retrieved holding the value that was
2824 * sent down on the actual send call. This option allows the setting of
2825 * a default context on an association basis that will be received on
2826 * reading messages from the peer. This is especially helpful in the
2827 * one-2-many model for an application to keep some reference to an
2828 * internal state machine that is processing messages on the
2829 * association. Note that the setting of this value only effects
2830 * received messages from the peer and does not effect the value that is
2831 * saved with outbound messages.
2833 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2834 int optlen)
2836 struct sctp_assoc_value params;
2837 struct sctp_sock *sp;
2838 struct sctp_association *asoc;
2840 if (optlen != sizeof(struct sctp_assoc_value))
2841 return -EINVAL;
2842 if (copy_from_user(&params, optval, optlen))
2843 return -EFAULT;
2845 sp = sctp_sk(sk);
2847 if (params.assoc_id != 0) {
2848 asoc = sctp_id2assoc(sk, params.assoc_id);
2849 if (!asoc)
2850 return -EINVAL;
2851 asoc->default_rcv_context = params.assoc_value;
2852 } else {
2853 sp->default_rcv_context = params.assoc_value;
2856 return 0;
2860 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2862 * This options will at a minimum specify if the implementation is doing
2863 * fragmented interleave. Fragmented interleave, for a one to many
2864 * socket, is when subsequent calls to receive a message may return
2865 * parts of messages from different associations. Some implementations
2866 * may allow you to turn this value on or off. If so, when turned off,
2867 * no fragment interleave will occur (which will cause a head of line
2868 * blocking amongst multiple associations sharing the same one to many
2869 * socket). When this option is turned on, then each receive call may
2870 * come from a different association (thus the user must receive data
2871 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2872 * association each receive belongs to.
2874 * This option takes a boolean value. A non-zero value indicates that
2875 * fragmented interleave is on. A value of zero indicates that
2876 * fragmented interleave is off.
2878 * Note that it is important that an implementation that allows this
2879 * option to be turned on, have it off by default. Otherwise an unaware
2880 * application using the one to many model may become confused and act
2881 * incorrectly.
2883 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2884 char __user *optval,
2885 int optlen)
2887 int val;
2889 if (optlen != sizeof(int))
2890 return -EINVAL;
2891 if (get_user(val, (int __user *)optval))
2892 return -EFAULT;
2894 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2896 return 0;
2900 * 7.1.25. Set or Get the sctp partial delivery point
2901 * (SCTP_PARTIAL_DELIVERY_POINT)
2902 * This option will set or get the SCTP partial delivery point. This
2903 * point is the size of a message where the partial delivery API will be
2904 * invoked to help free up rwnd space for the peer. Setting this to a
2905 * lower value will cause partial delivery's to happen more often. The
2906 * calls argument is an integer that sets or gets the partial delivery
2907 * point.
2909 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2910 char __user *optval,
2911 int optlen)
2913 u32 val;
2915 if (optlen != sizeof(u32))
2916 return -EINVAL;
2917 if (get_user(val, (int __user *)optval))
2918 return -EFAULT;
2920 sctp_sk(sk)->pd_point = val;
2922 return 0; /* is this the right error code? */
2926 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2928 * This option will allow a user to change the maximum burst of packets
2929 * that can be emitted by this association. Note that the default value
2930 * is 4, and some implementations may restrict this setting so that it
2931 * can only be lowered.
2933 * NOTE: This text doesn't seem right. Do this on a socket basis with
2934 * future associations inheriting the socket value.
2936 static int sctp_setsockopt_maxburst(struct sock *sk,
2937 char __user *optval,
2938 int optlen)
2940 <<<<<<< HEAD:net/sctp/socket.c
2941 =======
2942 struct sctp_assoc_value params;
2943 struct sctp_sock *sp;
2944 struct sctp_association *asoc;
2945 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2946 int val;
2947 <<<<<<< HEAD:net/sctp/socket.c
2948 =======
2949 int assoc_id = 0;
2950 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2952 <<<<<<< HEAD:net/sctp/socket.c
2953 if (optlen != sizeof(int))
2954 =======
2955 if (optlen < sizeof(int))
2956 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2957 return -EINVAL;
2958 <<<<<<< HEAD:net/sctp/socket.c
2959 if (get_user(val, (int __user *)optval))
2960 return -EFAULT;
2961 =======
2962 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2964 <<<<<<< HEAD:net/sctp/socket.c
2965 if (val < 0)
2966 =======
2967 if (optlen == sizeof(int)) {
2968 printk(KERN_WARNING
2969 "SCTP: Use of int in max_burst socket option deprecated\n");
2970 printk(KERN_WARNING
2971 "SCTP: Use struct sctp_assoc_value instead\n");
2972 if (copy_from_user(&val, optval, optlen))
2973 return -EFAULT;
2974 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2975 if (copy_from_user(&params, optval, optlen))
2976 return -EFAULT;
2977 val = params.assoc_value;
2978 assoc_id = params.assoc_id;
2979 } else
2980 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2981 return -EINVAL;
2983 <<<<<<< HEAD:net/sctp/socket.c
2984 sctp_sk(sk)->max_burst = val;
2985 =======
2986 sp = sctp_sk(sk);
2988 if (assoc_id != 0) {
2989 asoc = sctp_id2assoc(sk, assoc_id);
2990 if (!asoc)
2991 return -EINVAL;
2992 asoc->max_burst = val;
2993 } else
2994 sp->max_burst = val;
2995 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
2997 return 0;
3001 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3003 * This set option adds a chunk type that the user is requesting to be
3004 * received only in an authenticated way. Changes to the list of chunks
3005 * will only effect future associations on the socket.
3007 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3008 char __user *optval,
3009 int optlen)
3011 struct sctp_authchunk val;
3013 if (optlen != sizeof(struct sctp_authchunk))
3014 return -EINVAL;
3015 if (copy_from_user(&val, optval, optlen))
3016 return -EFAULT;
3018 switch (val.sauth_chunk) {
3019 case SCTP_CID_INIT:
3020 case SCTP_CID_INIT_ACK:
3021 case SCTP_CID_SHUTDOWN_COMPLETE:
3022 case SCTP_CID_AUTH:
3023 return -EINVAL;
3026 /* add this chunk id to the endpoint */
3027 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3031 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3033 * This option gets or sets the list of HMAC algorithms that the local
3034 * endpoint requires the peer to use.
3036 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3037 char __user *optval,
3038 int optlen)
3040 struct sctp_hmacalgo *hmacs;
3041 int err;
3043 if (optlen < sizeof(struct sctp_hmacalgo))
3044 return -EINVAL;
3046 hmacs = kmalloc(optlen, GFP_KERNEL);
3047 if (!hmacs)
3048 return -ENOMEM;
3050 if (copy_from_user(hmacs, optval, optlen)) {
3051 err = -EFAULT;
3052 goto out;
3055 if (hmacs->shmac_num_idents == 0 ||
3056 hmacs->shmac_num_idents > SCTP_AUTH_NUM_HMACS) {
3057 err = -EINVAL;
3058 goto out;
3061 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3062 out:
3063 kfree(hmacs);
3064 return err;
3068 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3070 * This option will set a shared secret key which is used to build an
3071 * association shared key.
3073 static int sctp_setsockopt_auth_key(struct sock *sk,
3074 char __user *optval,
3075 int optlen)
3077 struct sctp_authkey *authkey;
3078 struct sctp_association *asoc;
3079 int ret;
3081 if (optlen <= sizeof(struct sctp_authkey))
3082 return -EINVAL;
3084 authkey = kmalloc(optlen, GFP_KERNEL);
3085 if (!authkey)
3086 return -ENOMEM;
3088 if (copy_from_user(authkey, optval, optlen)) {
3089 ret = -EFAULT;
3090 goto out;
3093 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3094 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3095 ret = -EINVAL;
3096 goto out;
3099 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3100 out:
3101 kfree(authkey);
3102 return ret;
3106 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3108 * This option will get or set the active shared key to be used to build
3109 * the association shared key.
3111 static int sctp_setsockopt_active_key(struct sock *sk,
3112 char __user *optval,
3113 int optlen)
3115 struct sctp_authkeyid val;
3116 struct sctp_association *asoc;
3118 if (optlen != sizeof(struct sctp_authkeyid))
3119 return -EINVAL;
3120 if (copy_from_user(&val, optval, optlen))
3121 return -EFAULT;
3123 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3124 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3125 return -EINVAL;
3127 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3128 val.scact_keynumber);
3132 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3134 * This set option will delete a shared secret key from use.
3136 static int sctp_setsockopt_del_key(struct sock *sk,
3137 char __user *optval,
3138 int optlen)
3140 struct sctp_authkeyid val;
3141 struct sctp_association *asoc;
3143 if (optlen != sizeof(struct sctp_authkeyid))
3144 return -EINVAL;
3145 if (copy_from_user(&val, optval, optlen))
3146 return -EFAULT;
3148 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3149 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3150 return -EINVAL;
3152 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3153 val.scact_keynumber);
3158 /* API 6.2 setsockopt(), getsockopt()
3160 * Applications use setsockopt() and getsockopt() to set or retrieve
3161 * socket options. Socket options are used to change the default
3162 * behavior of sockets calls. They are described in Section 7.
3164 * The syntax is:
3166 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3167 * int __user *optlen);
3168 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3169 * int optlen);
3171 * sd - the socket descript.
3172 * level - set to IPPROTO_SCTP for all SCTP options.
3173 * optname - the option name.
3174 * optval - the buffer to store the value of the option.
3175 * optlen - the size of the buffer.
3177 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
3178 char __user *optval, int optlen)
3180 int retval = 0;
3182 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3183 sk, optname);
3185 /* I can hardly begin to describe how wrong this is. This is
3186 * so broken as to be worse than useless. The API draft
3187 * REALLY is NOT helpful here... I am not convinced that the
3188 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3189 * are at all well-founded.
3191 if (level != SOL_SCTP) {
3192 struct sctp_af *af = sctp_sk(sk)->pf->af;
3193 retval = af->setsockopt(sk, level, optname, optval, optlen);
3194 goto out_nounlock;
3197 sctp_lock_sock(sk);
3199 switch (optname) {
3200 case SCTP_SOCKOPT_BINDX_ADD:
3201 /* 'optlen' is the size of the addresses buffer. */
3202 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3203 optlen, SCTP_BINDX_ADD_ADDR);
3204 break;
3206 case SCTP_SOCKOPT_BINDX_REM:
3207 /* 'optlen' is the size of the addresses buffer. */
3208 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3209 optlen, SCTP_BINDX_REM_ADDR);
3210 break;
3212 case SCTP_SOCKOPT_CONNECTX:
3213 /* 'optlen' is the size of the addresses buffer. */
3214 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3215 optlen);
3216 break;
3218 case SCTP_DISABLE_FRAGMENTS:
3219 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3220 break;
3222 case SCTP_EVENTS:
3223 retval = sctp_setsockopt_events(sk, optval, optlen);
3224 break;
3226 case SCTP_AUTOCLOSE:
3227 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3228 break;
3230 case SCTP_PEER_ADDR_PARAMS:
3231 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3232 break;
3234 case SCTP_DELAYED_ACK_TIME:
3235 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3236 break;
3237 case SCTP_PARTIAL_DELIVERY_POINT:
3238 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3239 break;
3241 case SCTP_INITMSG:
3242 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3243 break;
3244 case SCTP_DEFAULT_SEND_PARAM:
3245 retval = sctp_setsockopt_default_send_param(sk, optval,
3246 optlen);
3247 break;
3248 case SCTP_PRIMARY_ADDR:
3249 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3250 break;
3251 case SCTP_SET_PEER_PRIMARY_ADDR:
3252 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3253 break;
3254 case SCTP_NODELAY:
3255 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3256 break;
3257 case SCTP_RTOINFO:
3258 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3259 break;
3260 case SCTP_ASSOCINFO:
3261 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3262 break;
3263 case SCTP_I_WANT_MAPPED_V4_ADDR:
3264 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3265 break;
3266 case SCTP_MAXSEG:
3267 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3268 break;
3269 case SCTP_ADAPTATION_LAYER:
3270 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3271 break;
3272 case SCTP_CONTEXT:
3273 retval = sctp_setsockopt_context(sk, optval, optlen);
3274 break;
3275 case SCTP_FRAGMENT_INTERLEAVE:
3276 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3277 break;
3278 case SCTP_MAX_BURST:
3279 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3280 break;
3281 case SCTP_AUTH_CHUNK:
3282 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3283 break;
3284 case SCTP_HMAC_IDENT:
3285 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3286 break;
3287 case SCTP_AUTH_KEY:
3288 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3289 break;
3290 case SCTP_AUTH_ACTIVE_KEY:
3291 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3292 break;
3293 case SCTP_AUTH_DELETE_KEY:
3294 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3295 break;
3296 default:
3297 retval = -ENOPROTOOPT;
3298 break;
3301 sctp_release_sock(sk);
3303 out_nounlock:
3304 return retval;
3307 /* API 3.1.6 connect() - UDP Style Syntax
3309 * An application may use the connect() call in the UDP model to initiate an
3310 * association without sending data.
3312 * The syntax is:
3314 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3316 * sd: the socket descriptor to have a new association added to.
3318 * nam: the address structure (either struct sockaddr_in or struct
3319 * sockaddr_in6 defined in RFC2553 [7]).
3321 * len: the size of the address.
3323 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3324 int addr_len)
3326 int err = 0;
3327 struct sctp_af *af;
3329 sctp_lock_sock(sk);
3331 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3332 __FUNCTION__, sk, addr, addr_len);
3334 /* Validate addr_len before calling common connect/connectx routine. */
3335 af = sctp_get_af_specific(addr->sa_family);
3336 if (!af || addr_len < af->sockaddr_len) {
3337 err = -EINVAL;
3338 } else {
3339 /* Pass correct addr len to common routine (so it knows there
3340 * is only one address being passed.
3342 err = __sctp_connect(sk, addr, af->sockaddr_len);
3345 sctp_release_sock(sk);
3346 return err;
3349 /* FIXME: Write comments. */
3350 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3352 return -EOPNOTSUPP; /* STUB */
3355 /* 4.1.4 accept() - TCP Style Syntax
3357 * Applications use accept() call to remove an established SCTP
3358 * association from the accept queue of the endpoint. A new socket
3359 * descriptor will be returned from accept() to represent the newly
3360 * formed association.
3362 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3364 struct sctp_sock *sp;
3365 struct sctp_endpoint *ep;
3366 struct sock *newsk = NULL;
3367 struct sctp_association *asoc;
3368 long timeo;
3369 int error = 0;
3371 sctp_lock_sock(sk);
3373 sp = sctp_sk(sk);
3374 ep = sp->ep;
3376 if (!sctp_style(sk, TCP)) {
3377 error = -EOPNOTSUPP;
3378 goto out;
3381 if (!sctp_sstate(sk, LISTENING)) {
3382 error = -EINVAL;
3383 goto out;
3386 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3388 error = sctp_wait_for_accept(sk, timeo);
3389 if (error)
3390 goto out;
3392 /* We treat the list of associations on the endpoint as the accept
3393 * queue and pick the first association on the list.
3395 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3397 newsk = sp->pf->create_accept_sk(sk, asoc);
3398 if (!newsk) {
3399 error = -ENOMEM;
3400 goto out;
3403 /* Populate the fields of the newsk from the oldsk and migrate the
3404 * asoc to the newsk.
3406 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3408 out:
3409 sctp_release_sock(sk);
3410 *err = error;
3411 return newsk;
3414 /* The SCTP ioctl handler. */
3415 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3417 return -ENOIOCTLCMD;
3420 /* This is the function which gets called during socket creation to
3421 * initialized the SCTP-specific portion of the sock.
3422 * The sock structure should already be zero-filled memory.
3424 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3426 struct sctp_endpoint *ep;
3427 struct sctp_sock *sp;
3429 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3431 sp = sctp_sk(sk);
3433 /* Initialize the SCTP per socket area. */
3434 switch (sk->sk_type) {
3435 case SOCK_SEQPACKET:
3436 sp->type = SCTP_SOCKET_UDP;
3437 break;
3438 case SOCK_STREAM:
3439 sp->type = SCTP_SOCKET_TCP;
3440 break;
3441 default:
3442 return -ESOCKTNOSUPPORT;
3445 /* Initialize default send parameters. These parameters can be
3446 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3448 sp->default_stream = 0;
3449 sp->default_ppid = 0;
3450 sp->default_flags = 0;
3451 sp->default_context = 0;
3452 sp->default_timetolive = 0;
3454 sp->default_rcv_context = 0;
3455 sp->max_burst = sctp_max_burst;
3457 /* Initialize default setup parameters. These parameters
3458 * can be modified with the SCTP_INITMSG socket option or
3459 * overridden by the SCTP_INIT CMSG.
3461 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3462 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3463 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3464 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3466 /* Initialize default RTO related parameters. These parameters can
3467 * be modified for with the SCTP_RTOINFO socket option.
3469 sp->rtoinfo.srto_initial = sctp_rto_initial;
3470 sp->rtoinfo.srto_max = sctp_rto_max;
3471 sp->rtoinfo.srto_min = sctp_rto_min;
3473 /* Initialize default association related parameters. These parameters
3474 * can be modified with the SCTP_ASSOCINFO socket option.
3476 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3477 sp->assocparams.sasoc_number_peer_destinations = 0;
3478 sp->assocparams.sasoc_peer_rwnd = 0;
3479 sp->assocparams.sasoc_local_rwnd = 0;
3480 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3482 /* Initialize default event subscriptions. By default, all the
3483 * options are off.
3485 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3487 /* Default Peer Address Parameters. These defaults can
3488 * be modified via SCTP_PEER_ADDR_PARAMS
3490 sp->hbinterval = sctp_hb_interval;
3491 sp->pathmaxrxt = sctp_max_retrans_path;
3492 sp->pathmtu = 0; // allow default discovery
3493 sp->sackdelay = sctp_sack_timeout;
3494 sp->param_flags = SPP_HB_ENABLE |
3495 SPP_PMTUD_ENABLE |
3496 SPP_SACKDELAY_ENABLE;
3498 /* If enabled no SCTP message fragmentation will be performed.
3499 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3501 sp->disable_fragments = 0;
3503 /* Enable Nagle algorithm by default. */
3504 sp->nodelay = 0;
3506 /* Enable by default. */
3507 sp->v4mapped = 1;
3509 /* Auto-close idle associations after the configured
3510 * number of seconds. A value of 0 disables this
3511 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3512 * for UDP-style sockets only.
3514 sp->autoclose = 0;
3516 /* User specified fragmentation limit. */
3517 sp->user_frag = 0;
3519 sp->adaptation_ind = 0;
3521 sp->pf = sctp_get_pf_specific(sk->sk_family);
3523 /* Control variables for partial data delivery. */
3524 atomic_set(&sp->pd_mode, 0);
3525 skb_queue_head_init(&sp->pd_lobby);
3526 sp->frag_interleave = 0;
3528 /* Create a per socket endpoint structure. Even if we
3529 * change the data structure relationships, this may still
3530 * be useful for storing pre-connect address information.
3532 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3533 if (!ep)
3534 return -ENOMEM;
3536 sp->ep = ep;
3537 sp->hmac = NULL;
3539 SCTP_DBG_OBJCNT_INC(sock);
3540 atomic_inc(&sctp_sockets_allocated);
3541 return 0;
3544 /* Cleanup any SCTP per socket resources. */
3545 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3547 struct sctp_endpoint *ep;
3549 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3551 /* Release our hold on the endpoint. */
3552 ep = sctp_sk(sk)->ep;
3553 sctp_endpoint_free(ep);
3554 atomic_dec(&sctp_sockets_allocated);
3555 return 0;
3558 /* API 4.1.7 shutdown() - TCP Style Syntax
3559 * int shutdown(int socket, int how);
3561 * sd - the socket descriptor of the association to be closed.
3562 * how - Specifies the type of shutdown. The values are
3563 * as follows:
3564 * SHUT_RD
3565 * Disables further receive operations. No SCTP
3566 * protocol action is taken.
3567 * SHUT_WR
3568 * Disables further send operations, and initiates
3569 * the SCTP shutdown sequence.
3570 * SHUT_RDWR
3571 * Disables further send and receive operations
3572 * and initiates the SCTP shutdown sequence.
3574 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3576 struct sctp_endpoint *ep;
3577 struct sctp_association *asoc;
3579 if (!sctp_style(sk, TCP))
3580 return;
3582 if (how & SEND_SHUTDOWN) {
3583 ep = sctp_sk(sk)->ep;
3584 if (!list_empty(&ep->asocs)) {
3585 asoc = list_entry(ep->asocs.next,
3586 struct sctp_association, asocs);
3587 sctp_primitive_SHUTDOWN(asoc, NULL);
3592 /* 7.2.1 Association Status (SCTP_STATUS)
3594 * Applications can retrieve current status information about an
3595 * association, including association state, peer receiver window size,
3596 * number of unacked data chunks, and number of data chunks pending
3597 * receipt. This information is read-only.
3599 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3600 char __user *optval,
3601 int __user *optlen)
3603 struct sctp_status status;
3604 struct sctp_association *asoc = NULL;
3605 struct sctp_transport *transport;
3606 sctp_assoc_t associd;
3607 int retval = 0;
3609 if (len < sizeof(status)) {
3610 retval = -EINVAL;
3611 goto out;
3614 len = sizeof(status);
3615 if (copy_from_user(&status, optval, len)) {
3616 retval = -EFAULT;
3617 goto out;
3620 associd = status.sstat_assoc_id;
3621 asoc = sctp_id2assoc(sk, associd);
3622 if (!asoc) {
3623 retval = -EINVAL;
3624 goto out;
3627 transport = asoc->peer.primary_path;
3629 status.sstat_assoc_id = sctp_assoc2id(asoc);
3630 status.sstat_state = asoc->state;
3631 status.sstat_rwnd = asoc->peer.rwnd;
3632 status.sstat_unackdata = asoc->unack_data;
3634 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3635 status.sstat_instrms = asoc->c.sinit_max_instreams;
3636 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3637 status.sstat_fragmentation_point = asoc->frag_point;
3638 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3639 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3640 transport->af_specific->sockaddr_len);
3641 /* Map ipv4 address into v4-mapped-on-v6 address. */
3642 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3643 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3644 status.sstat_primary.spinfo_state = transport->state;
3645 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3646 status.sstat_primary.spinfo_srtt = transport->srtt;
3647 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3648 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3650 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3651 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3653 if (put_user(len, optlen)) {
3654 retval = -EFAULT;
3655 goto out;
3658 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3659 len, status.sstat_state, status.sstat_rwnd,
3660 status.sstat_assoc_id);
3662 if (copy_to_user(optval, &status, len)) {
3663 retval = -EFAULT;
3664 goto out;
3667 out:
3668 return (retval);
3672 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3674 * Applications can retrieve information about a specific peer address
3675 * of an association, including its reachability state, congestion
3676 * window, and retransmission timer values. This information is
3677 * read-only.
3679 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3680 char __user *optval,
3681 int __user *optlen)
3683 struct sctp_paddrinfo pinfo;
3684 struct sctp_transport *transport;
3685 int retval = 0;
3687 if (len < sizeof(pinfo)) {
3688 retval = -EINVAL;
3689 goto out;
3692 len = sizeof(pinfo);
3693 if (copy_from_user(&pinfo, optval, len)) {
3694 retval = -EFAULT;
3695 goto out;
3698 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3699 pinfo.spinfo_assoc_id);
3700 if (!transport)
3701 return -EINVAL;
3703 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3704 pinfo.spinfo_state = transport->state;
3705 pinfo.spinfo_cwnd = transport->cwnd;
3706 pinfo.spinfo_srtt = transport->srtt;
3707 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3708 pinfo.spinfo_mtu = transport->pathmtu;
3710 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3711 pinfo.spinfo_state = SCTP_ACTIVE;
3713 if (put_user(len, optlen)) {
3714 retval = -EFAULT;
3715 goto out;
3718 if (copy_to_user(optval, &pinfo, len)) {
3719 retval = -EFAULT;
3720 goto out;
3723 out:
3724 return (retval);
3727 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3729 * This option is a on/off flag. If enabled no SCTP message
3730 * fragmentation will be performed. Instead if a message being sent
3731 * exceeds the current PMTU size, the message will NOT be sent and
3732 * instead a error will be indicated to the user.
3734 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3735 char __user *optval, int __user *optlen)
3737 int val;
3739 if (len < sizeof(int))
3740 return -EINVAL;
3742 len = sizeof(int);
3743 val = (sctp_sk(sk)->disable_fragments == 1);
3744 if (put_user(len, optlen))
3745 return -EFAULT;
3746 if (copy_to_user(optval, &val, len))
3747 return -EFAULT;
3748 return 0;
3751 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3753 * This socket option is used to specify various notifications and
3754 * ancillary data the user wishes to receive.
3756 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3757 int __user *optlen)
3759 if (len < sizeof(struct sctp_event_subscribe))
3760 return -EINVAL;
3761 len = sizeof(struct sctp_event_subscribe);
3762 if (put_user(len, optlen))
3763 return -EFAULT;
3764 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3765 return -EFAULT;
3766 return 0;
3769 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3771 * This socket option is applicable to the UDP-style socket only. When
3772 * set it will cause associations that are idle for more than the
3773 * specified number of seconds to automatically close. An association
3774 * being idle is defined an association that has NOT sent or received
3775 * user data. The special value of '0' indicates that no automatic
3776 * close of any associations should be performed. The option expects an
3777 * integer defining the number of seconds of idle time before an
3778 * association is closed.
3780 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3782 /* Applicable to UDP-style socket only */
3783 if (sctp_style(sk, TCP))
3784 return -EOPNOTSUPP;
3785 if (len < sizeof(int))
3786 return -EINVAL;
3787 len = sizeof(int);
3788 if (put_user(len, optlen))
3789 return -EFAULT;
3790 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
3791 return -EFAULT;
3792 return 0;
3795 /* Helper routine to branch off an association to a new socket. */
3796 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3797 struct socket **sockp)
3799 struct sock *sk = asoc->base.sk;
3800 struct socket *sock;
3801 struct inet_sock *inetsk;
3802 struct sctp_af *af;
3803 int err = 0;
3805 /* An association cannot be branched off from an already peeled-off
3806 * socket, nor is this supported for tcp style sockets.
3808 if (!sctp_style(sk, UDP))
3809 return -EINVAL;
3811 /* Create a new socket. */
3812 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3813 if (err < 0)
3814 return err;
3816 /* Populate the fields of the newsk from the oldsk and migrate the
3817 * asoc to the newsk.
3819 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3821 /* Make peeled-off sockets more like 1-1 accepted sockets.
3822 * Set the daddr and initialize id to something more random
3824 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3825 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3826 inetsk = inet_sk(sock->sk);
3827 inetsk->id = asoc->next_tsn ^ jiffies;
3829 *sockp = sock;
3831 return err;
3834 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3836 sctp_peeloff_arg_t peeloff;
3837 struct socket *newsock;
3838 int retval = 0;
3839 struct sctp_association *asoc;
3841 if (len < sizeof(sctp_peeloff_arg_t))
3842 return -EINVAL;
3843 len = sizeof(sctp_peeloff_arg_t);
3844 if (copy_from_user(&peeloff, optval, len))
3845 return -EFAULT;
3847 asoc = sctp_id2assoc(sk, peeloff.associd);
3848 if (!asoc) {
3849 retval = -EINVAL;
3850 goto out;
3853 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3855 retval = sctp_do_peeloff(asoc, &newsock);
3856 if (retval < 0)
3857 goto out;
3859 /* Map the socket to an unused fd that can be returned to the user. */
3860 retval = sock_map_fd(newsock);
3861 if (retval < 0) {
3862 sock_release(newsock);
3863 goto out;
3866 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3867 __FUNCTION__, sk, asoc, newsock->sk, retval);
3869 /* Return the fd mapped to the new socket. */
3870 peeloff.sd = retval;
3871 if (put_user(len, optlen))
3872 return -EFAULT;
3873 if (copy_to_user(optval, &peeloff, len))
3874 retval = -EFAULT;
3876 out:
3877 return retval;
3880 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3882 * Applications can enable or disable heartbeats for any peer address of
3883 * an association, modify an address's heartbeat interval, force a
3884 * heartbeat to be sent immediately, and adjust the address's maximum
3885 * number of retransmissions sent before an address is considered
3886 * unreachable. The following structure is used to access and modify an
3887 * address's parameters:
3889 * struct sctp_paddrparams {
3890 * sctp_assoc_t spp_assoc_id;
3891 * struct sockaddr_storage spp_address;
3892 * uint32_t spp_hbinterval;
3893 * uint16_t spp_pathmaxrxt;
3894 * uint32_t spp_pathmtu;
3895 * uint32_t spp_sackdelay;
3896 * uint32_t spp_flags;
3897 * };
3899 * spp_assoc_id - (one-to-many style socket) This is filled in the
3900 * application, and identifies the association for
3901 * this query.
3902 * spp_address - This specifies which address is of interest.
3903 * spp_hbinterval - This contains the value of the heartbeat interval,
3904 * in milliseconds. If a value of zero
3905 * is present in this field then no changes are to
3906 * be made to this parameter.
3907 * spp_pathmaxrxt - This contains the maximum number of
3908 * retransmissions before this address shall be
3909 * considered unreachable. If a value of zero
3910 * is present in this field then no changes are to
3911 * be made to this parameter.
3912 * spp_pathmtu - When Path MTU discovery is disabled the value
3913 * specified here will be the "fixed" path mtu.
3914 * Note that if the spp_address field is empty
3915 * then all associations on this address will
3916 * have this fixed path mtu set upon them.
3918 * spp_sackdelay - When delayed sack is enabled, this value specifies
3919 * the number of milliseconds that sacks will be delayed
3920 * for. This value will apply to all addresses of an
3921 * association if the spp_address field is empty. Note
3922 * also, that if delayed sack is enabled and this
3923 * value is set to 0, no change is made to the last
3924 * recorded delayed sack timer value.
3926 * spp_flags - These flags are used to control various features
3927 * on an association. The flag field may contain
3928 * zero or more of the following options.
3930 * SPP_HB_ENABLE - Enable heartbeats on the
3931 * specified address. Note that if the address
3932 * field is empty all addresses for the association
3933 * have heartbeats enabled upon them.
3935 * SPP_HB_DISABLE - Disable heartbeats on the
3936 * speicifed address. Note that if the address
3937 * field is empty all addresses for the association
3938 * will have their heartbeats disabled. Note also
3939 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3940 * mutually exclusive, only one of these two should
3941 * be specified. Enabling both fields will have
3942 * undetermined results.
3944 * SPP_HB_DEMAND - Request a user initiated heartbeat
3945 * to be made immediately.
3947 * SPP_PMTUD_ENABLE - This field will enable PMTU
3948 * discovery upon the specified address. Note that
3949 * if the address feild is empty then all addresses
3950 * on the association are effected.
3952 * SPP_PMTUD_DISABLE - This field will disable PMTU
3953 * discovery upon the specified address. Note that
3954 * if the address feild is empty then all addresses
3955 * on the association are effected. Not also that
3956 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3957 * exclusive. Enabling both will have undetermined
3958 * results.
3960 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3961 * on delayed sack. The time specified in spp_sackdelay
3962 * is used to specify the sack delay for this address. Note
3963 * that if spp_address is empty then all addresses will
3964 * enable delayed sack and take on the sack delay
3965 * value specified in spp_sackdelay.
3966 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3967 * off delayed sack. If the spp_address field is blank then
3968 * delayed sack is disabled for the entire association. Note
3969 * also that this field is mutually exclusive to
3970 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3971 * results.
3973 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3974 char __user *optval, int __user *optlen)
3976 struct sctp_paddrparams params;
3977 struct sctp_transport *trans = NULL;
3978 struct sctp_association *asoc = NULL;
3979 struct sctp_sock *sp = sctp_sk(sk);
3981 if (len < sizeof(struct sctp_paddrparams))
3982 return -EINVAL;
3983 len = sizeof(struct sctp_paddrparams);
3984 if (copy_from_user(&params, optval, len))
3985 return -EFAULT;
3987 /* If an address other than INADDR_ANY is specified, and
3988 * no transport is found, then the request is invalid.
3990 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3991 trans = sctp_addr_id2transport(sk, &params.spp_address,
3992 params.spp_assoc_id);
3993 if (!trans) {
3994 SCTP_DEBUG_PRINTK("Failed no transport\n");
3995 return -EINVAL;
3999 /* Get association, if assoc_id != 0 and the socket is a one
4000 * to many style socket, and an association was not found, then
4001 * the id was invalid.
4003 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4004 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4005 SCTP_DEBUG_PRINTK("Failed no association\n");
4006 return -EINVAL;
4009 if (trans) {
4010 /* Fetch transport values. */
4011 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4012 params.spp_pathmtu = trans->pathmtu;
4013 params.spp_pathmaxrxt = trans->pathmaxrxt;
4014 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
4016 /*draft-11 doesn't say what to return in spp_flags*/
4017 params.spp_flags = trans->param_flags;
4018 } else if (asoc) {
4019 /* Fetch association values. */
4020 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4021 params.spp_pathmtu = asoc->pathmtu;
4022 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4023 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
4025 /*draft-11 doesn't say what to return in spp_flags*/
4026 params.spp_flags = asoc->param_flags;
4027 } else {
4028 /* Fetch socket values. */
4029 params.spp_hbinterval = sp->hbinterval;
4030 params.spp_pathmtu = sp->pathmtu;
4031 params.spp_sackdelay = sp->sackdelay;
4032 params.spp_pathmaxrxt = sp->pathmaxrxt;
4034 /*draft-11 doesn't say what to return in spp_flags*/
4035 params.spp_flags = sp->param_flags;
4038 if (copy_to_user(optval, &params, len))
4039 return -EFAULT;
4041 if (put_user(len, optlen))
4042 return -EFAULT;
4044 return 0;
4047 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
4049 * This options will get or set the delayed ack timer. The time is set
4050 * in milliseconds. If the assoc_id is 0, then this sets or gets the
4051 * endpoints default delayed ack timer value. If the assoc_id field is
4052 * non-zero, then the set or get effects the specified association.
4054 * struct sctp_assoc_value {
4055 * sctp_assoc_t assoc_id;
4056 * uint32_t assoc_value;
4057 * };
4059 * assoc_id - This parameter, indicates which association the
4060 * user is preforming an action upon. Note that if
4061 * this field's value is zero then the endpoints
4062 * default value is changed (effecting future
4063 * associations only).
4065 * assoc_value - This parameter contains the number of milliseconds
4066 * that the user is requesting the delayed ACK timer
4067 * be set to. Note that this value is defined in
4068 * the standard to be between 200 and 500 milliseconds.
4070 * Note: a value of zero will leave the value alone,
4071 * but disable SACK delay. A non-zero value will also
4072 * enable SACK delay.
4074 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
4075 char __user *optval,
4076 int __user *optlen)
4078 struct sctp_assoc_value params;
4079 struct sctp_association *asoc = NULL;
4080 struct sctp_sock *sp = sctp_sk(sk);
4082 if (len < sizeof(struct sctp_assoc_value))
4083 return - EINVAL;
4085 len = sizeof(struct sctp_assoc_value);
4087 if (copy_from_user(&params, optval, len))
4088 return -EFAULT;
4090 /* Get association, if assoc_id != 0 and the socket is a one
4091 * to many style socket, and an association was not found, then
4092 * the id was invalid.
4094 asoc = sctp_id2assoc(sk, params.assoc_id);
4095 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
4096 return -EINVAL;
4098 if (asoc) {
4099 /* Fetch association values. */
4100 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
4101 params.assoc_value = jiffies_to_msecs(
4102 asoc->sackdelay);
4103 else
4104 params.assoc_value = 0;
4105 } else {
4106 /* Fetch socket values. */
4107 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
4108 params.assoc_value = sp->sackdelay;
4109 else
4110 params.assoc_value = 0;
4113 if (copy_to_user(optval, &params, len))
4114 return -EFAULT;
4116 if (put_user(len, optlen))
4117 return -EFAULT;
4119 return 0;
4122 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4124 * Applications can specify protocol parameters for the default association
4125 * initialization. The option name argument to setsockopt() and getsockopt()
4126 * is SCTP_INITMSG.
4128 * Setting initialization parameters is effective only on an unconnected
4129 * socket (for UDP-style sockets only future associations are effected
4130 * by the change). With TCP-style sockets, this option is inherited by
4131 * sockets derived from a listener socket.
4133 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4135 if (len < sizeof(struct sctp_initmsg))
4136 return -EINVAL;
4137 len = sizeof(struct sctp_initmsg);
4138 if (put_user(len, optlen))
4139 return -EFAULT;
4140 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4141 return -EFAULT;
4142 return 0;
4145 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
4146 char __user *optval,
4147 int __user *optlen)
4149 sctp_assoc_t id;
4150 struct sctp_association *asoc;
4151 struct list_head *pos;
4152 int cnt = 0;
4154 if (len < sizeof(sctp_assoc_t))
4155 return -EINVAL;
4157 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4158 return -EFAULT;
4160 /* For UDP-style sockets, id specifies the association to query. */
4161 asoc = sctp_id2assoc(sk, id);
4162 if (!asoc)
4163 return -EINVAL;
4165 list_for_each(pos, &asoc->peer.transport_addr_list) {
4166 cnt ++;
4169 return cnt;
4173 * Old API for getting list of peer addresses. Does not work for 32-bit
4174 * programs running on a 64-bit kernel
4176 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
4177 char __user *optval,
4178 int __user *optlen)
4180 struct sctp_association *asoc;
4181 struct list_head *pos;
4182 int cnt = 0;
4183 struct sctp_getaddrs_old getaddrs;
4184 struct sctp_transport *from;
4185 void __user *to;
4186 union sctp_addr temp;
4187 struct sctp_sock *sp = sctp_sk(sk);
4188 int addrlen;
4190 if (len < sizeof(struct sctp_getaddrs_old))
4191 return -EINVAL;
4193 len = sizeof(struct sctp_getaddrs_old);
4195 if (copy_from_user(&getaddrs, optval, len))
4196 return -EFAULT;
4198 if (getaddrs.addr_num <= 0) return -EINVAL;
4200 /* For UDP-style sockets, id specifies the association to query. */
4201 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4202 if (!asoc)
4203 return -EINVAL;
4205 to = (void __user *)getaddrs.addrs;
4206 list_for_each(pos, &asoc->peer.transport_addr_list) {
4207 from = list_entry(pos, struct sctp_transport, transports);
4208 memcpy(&temp, &from->ipaddr, sizeof(temp));
4209 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4210 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4211 if (copy_to_user(to, &temp, addrlen))
4212 return -EFAULT;
4213 to += addrlen ;
4214 cnt ++;
4215 if (cnt >= getaddrs.addr_num) break;
4217 getaddrs.addr_num = cnt;
4218 if (put_user(len, optlen))
4219 return -EFAULT;
4220 if (copy_to_user(optval, &getaddrs, len))
4221 return -EFAULT;
4223 return 0;
4226 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4227 char __user *optval, int __user *optlen)
4229 struct sctp_association *asoc;
4230 struct list_head *pos;
4231 int cnt = 0;
4232 struct sctp_getaddrs getaddrs;
4233 struct sctp_transport *from;
4234 void __user *to;
4235 union sctp_addr temp;
4236 struct sctp_sock *sp = sctp_sk(sk);
4237 int addrlen;
4238 size_t space_left;
4239 int bytes_copied;
4241 if (len < sizeof(struct sctp_getaddrs))
4242 return -EINVAL;
4244 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4245 return -EFAULT;
4247 /* For UDP-style sockets, id specifies the association to query. */
4248 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4249 if (!asoc)
4250 return -EINVAL;
4252 to = optval + offsetof(struct sctp_getaddrs,addrs);
4253 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4255 list_for_each(pos, &asoc->peer.transport_addr_list) {
4256 from = list_entry(pos, struct sctp_transport, transports);
4257 memcpy(&temp, &from->ipaddr, sizeof(temp));
4258 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4259 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4260 if (space_left < addrlen)
4261 return -ENOMEM;
4262 if (copy_to_user(to, &temp, addrlen))
4263 return -EFAULT;
4264 to += addrlen;
4265 cnt++;
4266 space_left -= addrlen;
4269 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4270 return -EFAULT;
4271 bytes_copied = ((char __user *)to) - optval;
4272 if (put_user(bytes_copied, optlen))
4273 return -EFAULT;
4275 return 0;
4278 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4279 char __user *optval,
4280 int __user *optlen)
4282 sctp_assoc_t id;
4283 struct sctp_bind_addr *bp;
4284 struct sctp_association *asoc;
4285 struct sctp_sockaddr_entry *addr;
4286 int cnt = 0;
4288 if (len < sizeof(sctp_assoc_t))
4289 return -EINVAL;
4291 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4292 return -EFAULT;
4295 * For UDP-style sockets, id specifies the association to query.
4296 * If the id field is set to the value '0' then the locally bound
4297 * addresses are returned without regard to any particular
4298 * association.
4300 if (0 == id) {
4301 bp = &sctp_sk(sk)->ep->base.bind_addr;
4302 } else {
4303 asoc = sctp_id2assoc(sk, id);
4304 if (!asoc)
4305 return -EINVAL;
4306 bp = &asoc->base.bind_addr;
4309 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4310 * addresses from the global local address list.
4312 if (sctp_list_single_entry(&bp->address_list)) {
4313 addr = list_entry(bp->address_list.next,
4314 struct sctp_sockaddr_entry, list);
4315 if (sctp_is_any(&addr->a)) {
4316 rcu_read_lock();
4317 list_for_each_entry_rcu(addr,
4318 &sctp_local_addr_list, list) {
4319 if (!addr->valid)
4320 continue;
4322 if ((PF_INET == sk->sk_family) &&
4323 (AF_INET6 == addr->a.sa.sa_family))
4324 continue;
4326 cnt++;
4328 rcu_read_unlock();
4329 } else {
4330 cnt = 1;
4332 goto done;
4335 /* Protection on the bound address list is not needed,
4336 * since in the socket option context we hold the socket lock,
4337 * so there is no way that the bound address list can change.
4339 list_for_each_entry(addr, &bp->address_list, list) {
4340 cnt ++;
4342 done:
4343 return cnt;
4346 /* Helper function that copies local addresses to user and returns the number
4347 * of addresses copied.
4349 static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4350 int max_addrs, void *to,
4351 int *bytes_copied)
4353 struct sctp_sockaddr_entry *addr;
4354 union sctp_addr temp;
4355 int cnt = 0;
4356 int addrlen;
4358 rcu_read_lock();
4359 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4360 if (!addr->valid)
4361 continue;
4363 if ((PF_INET == sk->sk_family) &&
4364 (AF_INET6 == addr->a.sa.sa_family))
4365 continue;
4366 memcpy(&temp, &addr->a, sizeof(temp));
4367 if (!temp.v4.sin_port)
4368 temp.v4.sin_port = htons(port);
4370 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4371 &temp);
4372 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4373 memcpy(to, &temp, addrlen);
4375 to += addrlen;
4376 *bytes_copied += addrlen;
4377 cnt ++;
4378 if (cnt >= max_addrs) break;
4380 rcu_read_unlock();
4382 return cnt;
4385 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4386 size_t space_left, int *bytes_copied)
4388 struct sctp_sockaddr_entry *addr;
4389 union sctp_addr temp;
4390 int cnt = 0;
4391 int addrlen;
4393 rcu_read_lock();
4394 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4395 if (!addr->valid)
4396 continue;
4398 if ((PF_INET == sk->sk_family) &&
4399 (AF_INET6 == addr->a.sa.sa_family))
4400 continue;
4401 memcpy(&temp, &addr->a, sizeof(temp));
4402 if (!temp.v4.sin_port)
4403 temp.v4.sin_port = htons(port);
4405 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4406 &temp);
4407 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4408 if (space_left < addrlen) {
4409 cnt = -ENOMEM;
4410 break;
4412 memcpy(to, &temp, addrlen);
4414 to += addrlen;
4415 cnt ++;
4416 space_left -= addrlen;
4417 *bytes_copied += addrlen;
4419 rcu_read_unlock();
4421 return cnt;
4424 /* Old API for getting list of local addresses. Does not work for 32-bit
4425 * programs running on a 64-bit kernel
4427 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4428 char __user *optval, int __user *optlen)
4430 struct sctp_bind_addr *bp;
4431 struct sctp_association *asoc;
4432 int cnt = 0;
4433 struct sctp_getaddrs_old getaddrs;
4434 struct sctp_sockaddr_entry *addr;
4435 void __user *to;
4436 union sctp_addr temp;
4437 struct sctp_sock *sp = sctp_sk(sk);
4438 int addrlen;
4439 int err = 0;
4440 void *addrs;
4441 void *buf;
4442 int bytes_copied = 0;
4444 if (len < sizeof(struct sctp_getaddrs_old))
4445 return -EINVAL;
4447 len = sizeof(struct sctp_getaddrs_old);
4448 if (copy_from_user(&getaddrs, optval, len))
4449 return -EFAULT;
4451 if (getaddrs.addr_num <= 0) return -EINVAL;
4453 * For UDP-style sockets, id specifies the association to query.
4454 * If the id field is set to the value '0' then the locally bound
4455 * addresses are returned without regard to any particular
4456 * association.
4458 if (0 == getaddrs.assoc_id) {
4459 bp = &sctp_sk(sk)->ep->base.bind_addr;
4460 } else {
4461 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4462 if (!asoc)
4463 return -EINVAL;
4464 bp = &asoc->base.bind_addr;
4467 to = getaddrs.addrs;
4469 /* Allocate space for a local instance of packed array to hold all
4470 * the data. We store addresses here first and then put write them
4471 * to the user in one shot.
4473 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4474 GFP_KERNEL);
4475 if (!addrs)
4476 return -ENOMEM;
4478 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4479 * addresses from the global local address list.
4481 if (sctp_list_single_entry(&bp->address_list)) {
4482 addr = list_entry(bp->address_list.next,
4483 struct sctp_sockaddr_entry, list);
4484 if (sctp_is_any(&addr->a)) {
4485 cnt = sctp_copy_laddrs_old(sk, bp->port,
4486 getaddrs.addr_num,
4487 addrs, &bytes_copied);
4488 goto copy_getaddrs;
4492 buf = addrs;
4493 /* Protection on the bound address list is not needed since
4494 * in the socket option context we hold a socket lock and
4495 * thus the bound address list can't change.
4497 list_for_each_entry(addr, &bp->address_list, list) {
4498 memcpy(&temp, &addr->a, sizeof(temp));
4499 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4500 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4501 memcpy(buf, &temp, addrlen);
4502 buf += addrlen;
4503 bytes_copied += addrlen;
4504 cnt ++;
4505 if (cnt >= getaddrs.addr_num) break;
4508 copy_getaddrs:
4509 /* copy the entire address list into the user provided space */
4510 if (copy_to_user(to, addrs, bytes_copied)) {
4511 err = -EFAULT;
4512 goto error;
4515 /* copy the leading structure back to user */
4516 getaddrs.addr_num = cnt;
4517 if (copy_to_user(optval, &getaddrs, len))
4518 err = -EFAULT;
4520 error:
4521 kfree(addrs);
4522 return err;
4525 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4526 char __user *optval, int __user *optlen)
4528 struct sctp_bind_addr *bp;
4529 struct sctp_association *asoc;
4530 int cnt = 0;
4531 struct sctp_getaddrs getaddrs;
4532 struct sctp_sockaddr_entry *addr;
4533 void __user *to;
4534 union sctp_addr temp;
4535 struct sctp_sock *sp = sctp_sk(sk);
4536 int addrlen;
4537 int err = 0;
4538 size_t space_left;
4539 int bytes_copied = 0;
4540 void *addrs;
4541 void *buf;
4543 if (len < sizeof(struct sctp_getaddrs))
4544 return -EINVAL;
4546 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4547 return -EFAULT;
4550 * For UDP-style sockets, id specifies the association to query.
4551 * If the id field is set to the value '0' then the locally bound
4552 * addresses are returned without regard to any particular
4553 * association.
4555 if (0 == getaddrs.assoc_id) {
4556 bp = &sctp_sk(sk)->ep->base.bind_addr;
4557 } else {
4558 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4559 if (!asoc)
4560 return -EINVAL;
4561 bp = &asoc->base.bind_addr;
4564 to = optval + offsetof(struct sctp_getaddrs,addrs);
4565 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4567 addrs = kmalloc(space_left, GFP_KERNEL);
4568 if (!addrs)
4569 return -ENOMEM;
4571 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4572 * addresses from the global local address list.
4574 if (sctp_list_single_entry(&bp->address_list)) {
4575 addr = list_entry(bp->address_list.next,
4576 struct sctp_sockaddr_entry, list);
4577 if (sctp_is_any(&addr->a)) {
4578 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4579 space_left, &bytes_copied);
4580 if (cnt < 0) {
4581 err = cnt;
4582 goto out;
4584 goto copy_getaddrs;
4588 buf = addrs;
4589 /* Protection on the bound address list is not needed since
4590 * in the socket option context we hold a socket lock and
4591 * thus the bound address list can't change.
4593 list_for_each_entry(addr, &bp->address_list, list) {
4594 memcpy(&temp, &addr->a, sizeof(temp));
4595 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4596 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4597 if (space_left < addrlen) {
4598 err = -ENOMEM; /*fixme: right error?*/
4599 goto out;
4601 memcpy(buf, &temp, addrlen);
4602 buf += addrlen;
4603 bytes_copied += addrlen;
4604 cnt ++;
4605 space_left -= addrlen;
4608 copy_getaddrs:
4609 if (copy_to_user(to, addrs, bytes_copied)) {
4610 err = -EFAULT;
4611 goto out;
4613 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4614 err = -EFAULT;
4615 goto out;
4617 if (put_user(bytes_copied, optlen))
4618 err = -EFAULT;
4619 out:
4620 kfree(addrs);
4621 return err;
4624 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4626 * Requests that the local SCTP stack use the enclosed peer address as
4627 * the association primary. The enclosed address must be one of the
4628 * association peer's addresses.
4630 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4631 char __user *optval, int __user *optlen)
4633 struct sctp_prim prim;
4634 struct sctp_association *asoc;
4635 struct sctp_sock *sp = sctp_sk(sk);
4637 if (len < sizeof(struct sctp_prim))
4638 return -EINVAL;
4640 len = sizeof(struct sctp_prim);
4642 if (copy_from_user(&prim, optval, len))
4643 return -EFAULT;
4645 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4646 if (!asoc)
4647 return -EINVAL;
4649 if (!asoc->peer.primary_path)
4650 return -ENOTCONN;
4652 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4653 asoc->peer.primary_path->af_specific->sockaddr_len);
4655 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4656 (union sctp_addr *)&prim.ssp_addr);
4658 if (put_user(len, optlen))
4659 return -EFAULT;
4660 if (copy_to_user(optval, &prim, len))
4661 return -EFAULT;
4663 return 0;
4667 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4669 * Requests that the local endpoint set the specified Adaptation Layer
4670 * Indication parameter for all future INIT and INIT-ACK exchanges.
4672 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4673 char __user *optval, int __user *optlen)
4675 struct sctp_setadaptation adaptation;
4677 if (len < sizeof(struct sctp_setadaptation))
4678 return -EINVAL;
4680 len = sizeof(struct sctp_setadaptation);
4682 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4684 if (put_user(len, optlen))
4685 return -EFAULT;
4686 if (copy_to_user(optval, &adaptation, len))
4687 return -EFAULT;
4689 return 0;
4694 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4696 * Applications that wish to use the sendto() system call may wish to
4697 * specify a default set of parameters that would normally be supplied
4698 * through the inclusion of ancillary data. This socket option allows
4699 * such an application to set the default sctp_sndrcvinfo structure.
4702 * The application that wishes to use this socket option simply passes
4703 * in to this call the sctp_sndrcvinfo structure defined in Section
4704 * 5.2.2) The input parameters accepted by this call include
4705 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4706 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4707 * to this call if the caller is using the UDP model.
4709 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4711 static int sctp_getsockopt_default_send_param(struct sock *sk,
4712 int len, char __user *optval,
4713 int __user *optlen)
4715 struct sctp_sndrcvinfo info;
4716 struct sctp_association *asoc;
4717 struct sctp_sock *sp = sctp_sk(sk);
4719 if (len < sizeof(struct sctp_sndrcvinfo))
4720 return -EINVAL;
4722 len = sizeof(struct sctp_sndrcvinfo);
4724 if (copy_from_user(&info, optval, len))
4725 return -EFAULT;
4727 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4728 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4729 return -EINVAL;
4731 if (asoc) {
4732 info.sinfo_stream = asoc->default_stream;
4733 info.sinfo_flags = asoc->default_flags;
4734 info.sinfo_ppid = asoc->default_ppid;
4735 info.sinfo_context = asoc->default_context;
4736 info.sinfo_timetolive = asoc->default_timetolive;
4737 } else {
4738 info.sinfo_stream = sp->default_stream;
4739 info.sinfo_flags = sp->default_flags;
4740 info.sinfo_ppid = sp->default_ppid;
4741 info.sinfo_context = sp->default_context;
4742 info.sinfo_timetolive = sp->default_timetolive;
4745 if (put_user(len, optlen))
4746 return -EFAULT;
4747 if (copy_to_user(optval, &info, len))
4748 return -EFAULT;
4750 return 0;
4755 * 7.1.5 SCTP_NODELAY
4757 * Turn on/off any Nagle-like algorithm. This means that packets are
4758 * generally sent as soon as possible and no unnecessary delays are
4759 * introduced, at the cost of more packets in the network. Expects an
4760 * integer boolean flag.
4763 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4764 char __user *optval, int __user *optlen)
4766 int val;
4768 if (len < sizeof(int))
4769 return -EINVAL;
4771 len = sizeof(int);
4772 val = (sctp_sk(sk)->nodelay == 1);
4773 if (put_user(len, optlen))
4774 return -EFAULT;
4775 if (copy_to_user(optval, &val, len))
4776 return -EFAULT;
4777 return 0;
4782 * 7.1.1 SCTP_RTOINFO
4784 * The protocol parameters used to initialize and bound retransmission
4785 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4786 * and modify these parameters.
4787 * All parameters are time values, in milliseconds. A value of 0, when
4788 * modifying the parameters, indicates that the current value should not
4789 * be changed.
4792 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4793 char __user *optval,
4794 int __user *optlen) {
4795 struct sctp_rtoinfo rtoinfo;
4796 struct sctp_association *asoc;
4798 if (len < sizeof (struct sctp_rtoinfo))
4799 return -EINVAL;
4801 len = sizeof(struct sctp_rtoinfo);
4803 if (copy_from_user(&rtoinfo, optval, len))
4804 return -EFAULT;
4806 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4808 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4809 return -EINVAL;
4811 /* Values corresponding to the specific association. */
4812 if (asoc) {
4813 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4814 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4815 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4816 } else {
4817 /* Values corresponding to the endpoint. */
4818 struct sctp_sock *sp = sctp_sk(sk);
4820 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4821 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4822 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4825 if (put_user(len, optlen))
4826 return -EFAULT;
4828 if (copy_to_user(optval, &rtoinfo, len))
4829 return -EFAULT;
4831 return 0;
4836 * 7.1.2 SCTP_ASSOCINFO
4838 * This option is used to tune the maximum retransmission attempts
4839 * of the association.
4840 * Returns an error if the new association retransmission value is
4841 * greater than the sum of the retransmission value of the peer.
4842 * See [SCTP] for more information.
4845 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4846 char __user *optval,
4847 int __user *optlen)
4850 struct sctp_assocparams assocparams;
4851 struct sctp_association *asoc;
4852 struct list_head *pos;
4853 int cnt = 0;
4855 if (len < sizeof (struct sctp_assocparams))
4856 return -EINVAL;
4858 len = sizeof(struct sctp_assocparams);
4860 if (copy_from_user(&assocparams, optval, len))
4861 return -EFAULT;
4863 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4865 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4866 return -EINVAL;
4868 /* Values correspoinding to the specific association */
4869 if (asoc) {
4870 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4871 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4872 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4873 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4874 * 1000) +
4875 (asoc->cookie_life.tv_usec
4876 / 1000);
4878 list_for_each(pos, &asoc->peer.transport_addr_list) {
4879 cnt ++;
4882 assocparams.sasoc_number_peer_destinations = cnt;
4883 } else {
4884 /* Values corresponding to the endpoint */
4885 struct sctp_sock *sp = sctp_sk(sk);
4887 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4888 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4889 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4890 assocparams.sasoc_cookie_life =
4891 sp->assocparams.sasoc_cookie_life;
4892 assocparams.sasoc_number_peer_destinations =
4893 sp->assocparams.
4894 sasoc_number_peer_destinations;
4897 if (put_user(len, optlen))
4898 return -EFAULT;
4900 if (copy_to_user(optval, &assocparams, len))
4901 return -EFAULT;
4903 return 0;
4907 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4909 * This socket option is a boolean flag which turns on or off mapped V4
4910 * addresses. If this option is turned on and the socket is type
4911 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4912 * If this option is turned off, then no mapping will be done of V4
4913 * addresses and a user will receive both PF_INET6 and PF_INET type
4914 * addresses on the socket.
4916 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4917 char __user *optval, int __user *optlen)
4919 int val;
4920 struct sctp_sock *sp = sctp_sk(sk);
4922 if (len < sizeof(int))
4923 return -EINVAL;
4925 len = sizeof(int);
4926 val = sp->v4mapped;
4927 if (put_user(len, optlen))
4928 return -EFAULT;
4929 if (copy_to_user(optval, &val, len))
4930 return -EFAULT;
4932 return 0;
4936 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4937 * (chapter and verse is quoted at sctp_setsockopt_context())
4939 static int sctp_getsockopt_context(struct sock *sk, int len,
4940 char __user *optval, int __user *optlen)
4942 struct sctp_assoc_value params;
4943 struct sctp_sock *sp;
4944 struct sctp_association *asoc;
4946 if (len < sizeof(struct sctp_assoc_value))
4947 return -EINVAL;
4949 len = sizeof(struct sctp_assoc_value);
4951 if (copy_from_user(&params, optval, len))
4952 return -EFAULT;
4954 sp = sctp_sk(sk);
4956 if (params.assoc_id != 0) {
4957 asoc = sctp_id2assoc(sk, params.assoc_id);
4958 if (!asoc)
4959 return -EINVAL;
4960 params.assoc_value = asoc->default_rcv_context;
4961 } else {
4962 params.assoc_value = sp->default_rcv_context;
4965 if (put_user(len, optlen))
4966 return -EFAULT;
4967 if (copy_to_user(optval, &params, len))
4968 return -EFAULT;
4970 return 0;
4974 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4976 * This socket option specifies the maximum size to put in any outgoing
4977 * SCTP chunk. If a message is larger than this size it will be
4978 * fragmented by SCTP into the specified size. Note that the underlying
4979 * SCTP implementation may fragment into smaller sized chunks when the
4980 * PMTU of the underlying association is smaller than the value set by
4981 * the user.
4983 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4984 char __user *optval, int __user *optlen)
4986 int val;
4988 if (len < sizeof(int))
4989 return -EINVAL;
4991 len = sizeof(int);
4993 val = sctp_sk(sk)->user_frag;
4994 if (put_user(len, optlen))
4995 return -EFAULT;
4996 if (copy_to_user(optval, &val, len))
4997 return -EFAULT;
4999 return 0;
5003 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5004 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5006 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5007 char __user *optval, int __user *optlen)
5009 int val;
5011 if (len < sizeof(int))
5012 return -EINVAL;
5014 len = sizeof(int);
5016 val = sctp_sk(sk)->frag_interleave;
5017 if (put_user(len, optlen))
5018 return -EFAULT;
5019 if (copy_to_user(optval, &val, len))
5020 return -EFAULT;
5022 return 0;
5026 * 7.1.25. Set or Get the sctp partial delivery point
5027 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5029 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5030 char __user *optval,
5031 int __user *optlen)
5033 u32 val;
5035 if (len < sizeof(u32))
5036 return -EINVAL;
5038 len = sizeof(u32);
5040 val = sctp_sk(sk)->pd_point;
5041 if (put_user(len, optlen))
5042 return -EFAULT;
5043 if (copy_to_user(optval, &val, len))
5044 return -EFAULT;
5046 return -ENOTSUPP;
5050 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5051 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5053 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5054 char __user *optval,
5055 int __user *optlen)
5057 <<<<<<< HEAD:net/sctp/socket.c
5058 int val;
5059 =======
5060 struct sctp_assoc_value params;
5061 struct sctp_sock *sp;
5062 struct sctp_association *asoc;
5063 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5065 if (len < sizeof(int))
5066 return -EINVAL;
5068 <<<<<<< HEAD:net/sctp/socket.c
5069 len = sizeof(int);
5070 =======
5071 if (len == sizeof(int)) {
5072 printk(KERN_WARNING
5073 "SCTP: Use of int in max_burst socket option deprecated\n");
5074 printk(KERN_WARNING
5075 "SCTP: Use struct sctp_assoc_value instead\n");
5076 params.assoc_id = 0;
5077 } else if (len == sizeof (struct sctp_assoc_value)) {
5078 if (copy_from_user(&params, optval, len))
5079 return -EFAULT;
5080 } else
5081 return -EINVAL;
5082 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5084 <<<<<<< HEAD:net/sctp/socket.c
5085 val = sctp_sk(sk)->max_burst;
5086 if (put_user(len, optlen))
5087 return -EFAULT;
5088 if (copy_to_user(optval, &val, len))
5089 return -EFAULT;
5090 =======
5091 sp = sctp_sk(sk);
5093 if (params.assoc_id != 0) {
5094 asoc = sctp_id2assoc(sk, params.assoc_id);
5095 if (!asoc)
5096 return -EINVAL;
5097 params.assoc_value = asoc->max_burst;
5098 } else
5099 params.assoc_value = sp->max_burst;
5101 if (len == sizeof(int)) {
5102 if (copy_to_user(optval, &params.assoc_value, len))
5103 return -EFAULT;
5104 } else {
5105 if (copy_to_user(optval, &params, len))
5106 return -EFAULT;
5109 return 0;
5110 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5112 <<<<<<< HEAD:net/sctp/socket.c
5113 return -ENOTSUPP;
5114 =======
5115 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5118 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5119 char __user *optval, int __user *optlen)
5121 struct sctp_hmac_algo_param *hmacs;
5122 __u16 param_len;
5124 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5125 param_len = ntohs(hmacs->param_hdr.length);
5127 if (len < param_len)
5128 return -EINVAL;
5129 if (put_user(len, optlen))
5130 return -EFAULT;
5131 if (copy_to_user(optval, hmacs->hmac_ids, len))
5132 return -EFAULT;
5134 return 0;
5137 static int sctp_getsockopt_active_key(struct sock *sk, int len,
5138 char __user *optval, int __user *optlen)
5140 struct sctp_authkeyid val;
5141 struct sctp_association *asoc;
5143 if (len < sizeof(struct sctp_authkeyid))
5144 return -EINVAL;
5145 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5146 return -EFAULT;
5148 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5149 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5150 return -EINVAL;
5152 if (asoc)
5153 val.scact_keynumber = asoc->active_key_id;
5154 else
5155 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5157 return 0;
5160 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5161 char __user *optval, int __user *optlen)
5163 struct sctp_authchunks __user *p = (void __user *)optval;
5164 struct sctp_authchunks val;
5165 struct sctp_association *asoc;
5166 struct sctp_chunks_param *ch;
5167 <<<<<<< HEAD:net/sctp/socket.c
5168 =======
5169 u32 num_chunks;
5170 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5171 char __user *to;
5173 if (len <= sizeof(struct sctp_authchunks))
5174 return -EINVAL;
5176 if (copy_from_user(&val, p, sizeof(struct sctp_authchunks)))
5177 return -EFAULT;
5179 to = p->gauth_chunks;
5180 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5181 if (!asoc)
5182 return -EINVAL;
5184 ch = asoc->peer.peer_chunks;
5186 /* See if the user provided enough room for all the data */
5187 <<<<<<< HEAD:net/sctp/socket.c
5188 if (len < ntohs(ch->param_hdr.length))
5189 =======
5190 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5191 if (len < num_chunks)
5192 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5193 return -EINVAL;
5195 <<<<<<< HEAD:net/sctp/socket.c
5196 len = ntohs(ch->param_hdr.length);
5197 =======
5198 len = num_chunks;
5199 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5200 if (put_user(len, optlen))
5201 return -EFAULT;
5202 <<<<<<< HEAD:net/sctp/socket.c
5203 =======
5204 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5205 return -EFAULT;
5206 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5207 if (copy_to_user(to, ch->chunks, len))
5208 return -EFAULT;
5210 return 0;
5213 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5214 char __user *optval, int __user *optlen)
5216 struct sctp_authchunks __user *p = (void __user *)optval;
5217 struct sctp_authchunks val;
5218 struct sctp_association *asoc;
5219 struct sctp_chunks_param *ch;
5220 <<<<<<< HEAD:net/sctp/socket.c
5221 =======
5222 u32 num_chunks;
5223 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5224 char __user *to;
5226 if (len <= sizeof(struct sctp_authchunks))
5227 return -EINVAL;
5229 if (copy_from_user(&val, p, sizeof(struct sctp_authchunks)))
5230 return -EFAULT;
5232 to = p->gauth_chunks;
5233 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5234 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5235 return -EINVAL;
5237 if (asoc)
5238 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5239 else
5240 ch = sctp_sk(sk)->ep->auth_chunk_list;
5242 <<<<<<< HEAD:net/sctp/socket.c
5243 if (len < ntohs(ch->param_hdr.length))
5244 =======
5245 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5246 if (len < num_chunks)
5247 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5248 return -EINVAL;
5250 <<<<<<< HEAD:net/sctp/socket.c
5251 len = ntohs(ch->param_hdr.length);
5252 =======
5253 len = num_chunks;
5254 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5255 if (put_user(len, optlen))
5256 return -EFAULT;
5257 <<<<<<< HEAD:net/sctp/socket.c
5258 =======
5259 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5260 return -EFAULT;
5261 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
5262 if (copy_to_user(to, ch->chunks, len))
5263 return -EFAULT;
5265 return 0;
5268 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5269 char __user *optval, int __user *optlen)
5271 int retval = 0;
5272 int len;
5274 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5275 sk, optname);
5277 /* I can hardly begin to describe how wrong this is. This is
5278 * so broken as to be worse than useless. The API draft
5279 * REALLY is NOT helpful here... I am not convinced that the
5280 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5281 * are at all well-founded.
5283 if (level != SOL_SCTP) {
5284 struct sctp_af *af = sctp_sk(sk)->pf->af;
5286 retval = af->getsockopt(sk, level, optname, optval, optlen);
5287 return retval;
5290 if (get_user(len, optlen))
5291 return -EFAULT;
5293 sctp_lock_sock(sk);
5295 switch (optname) {
5296 case SCTP_STATUS:
5297 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5298 break;
5299 case SCTP_DISABLE_FRAGMENTS:
5300 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5301 optlen);
5302 break;
5303 case SCTP_EVENTS:
5304 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5305 break;
5306 case SCTP_AUTOCLOSE:
5307 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5308 break;
5309 case SCTP_SOCKOPT_PEELOFF:
5310 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5311 break;
5312 case SCTP_PEER_ADDR_PARAMS:
5313 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5314 optlen);
5315 break;
5316 case SCTP_DELAYED_ACK_TIME:
5317 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
5318 optlen);
5319 break;
5320 case SCTP_INITMSG:
5321 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5322 break;
5323 case SCTP_GET_PEER_ADDRS_NUM_OLD:
5324 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
5325 optlen);
5326 break;
5327 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
5328 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
5329 optlen);
5330 break;
5331 case SCTP_GET_PEER_ADDRS_OLD:
5332 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
5333 optlen);
5334 break;
5335 case SCTP_GET_LOCAL_ADDRS_OLD:
5336 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
5337 optlen);
5338 break;
5339 case SCTP_GET_PEER_ADDRS:
5340 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5341 optlen);
5342 break;
5343 case SCTP_GET_LOCAL_ADDRS:
5344 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5345 optlen);
5346 break;
5347 case SCTP_DEFAULT_SEND_PARAM:
5348 retval = sctp_getsockopt_default_send_param(sk, len,
5349 optval, optlen);
5350 break;
5351 case SCTP_PRIMARY_ADDR:
5352 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5353 break;
5354 case SCTP_NODELAY:
5355 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5356 break;
5357 case SCTP_RTOINFO:
5358 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5359 break;
5360 case SCTP_ASSOCINFO:
5361 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5362 break;
5363 case SCTP_I_WANT_MAPPED_V4_ADDR:
5364 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5365 break;
5366 case SCTP_MAXSEG:
5367 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5368 break;
5369 case SCTP_GET_PEER_ADDR_INFO:
5370 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5371 optlen);
5372 break;
5373 case SCTP_ADAPTATION_LAYER:
5374 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
5375 optlen);
5376 break;
5377 case SCTP_CONTEXT:
5378 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5379 break;
5380 case SCTP_FRAGMENT_INTERLEAVE:
5381 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5382 optlen);
5383 break;
5384 case SCTP_PARTIAL_DELIVERY_POINT:
5385 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5386 optlen);
5387 break;
5388 case SCTP_MAX_BURST:
5389 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5390 break;
5391 case SCTP_AUTH_KEY:
5392 case SCTP_AUTH_CHUNK:
5393 case SCTP_AUTH_DELETE_KEY:
5394 retval = -EOPNOTSUPP;
5395 break;
5396 case SCTP_HMAC_IDENT:
5397 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5398 break;
5399 case SCTP_AUTH_ACTIVE_KEY:
5400 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5401 break;
5402 case SCTP_PEER_AUTH_CHUNKS:
5403 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5404 optlen);
5405 break;
5406 case SCTP_LOCAL_AUTH_CHUNKS:
5407 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5408 optlen);
5409 break;
5410 default:
5411 retval = -ENOPROTOOPT;
5412 break;
5415 sctp_release_sock(sk);
5416 return retval;
5419 static void sctp_hash(struct sock *sk)
5421 /* STUB */
5424 static void sctp_unhash(struct sock *sk)
5426 /* STUB */
5429 /* Check if port is acceptable. Possibly find first available port.
5431 * The port hash table (contained in the 'global' SCTP protocol storage
5432 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5433 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5434 * list (the list number is the port number hashed out, so as you
5435 * would expect from a hash function, all the ports in a given list have
5436 * such a number that hashes out to the same list number; you were
5437 * expecting that, right?); so each list has a set of ports, with a
5438 * link to the socket (struct sock) that uses it, the port number and
5439 * a fastreuse flag (FIXME: NPI ipg).
5441 static struct sctp_bind_bucket *sctp_bucket_create(
5442 struct sctp_bind_hashbucket *head, unsigned short snum);
5444 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5446 struct sctp_bind_hashbucket *head; /* hash list */
5447 struct sctp_bind_bucket *pp; /* hash list port iterator */
5448 struct hlist_node *node;
5449 unsigned short snum;
5450 int ret;
5452 snum = ntohs(addr->v4.sin_port);
5454 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5455 sctp_local_bh_disable();
5457 if (snum == 0) {
5458 /* Search for an available port. */
5459 int low, high, remaining, index;
5460 unsigned int rover;
5462 inet_get_local_port_range(&low, &high);
5463 remaining = (high - low) + 1;
5464 rover = net_random() % remaining + low;
5466 do {
5467 rover++;
5468 if ((rover < low) || (rover > high))
5469 rover = low;
5470 index = sctp_phashfn(rover);
5471 head = &sctp_port_hashtable[index];
5472 sctp_spin_lock(&head->lock);
5473 sctp_for_each_hentry(pp, node, &head->chain)
5474 if (pp->port == rover)
5475 goto next;
5476 break;
5477 next:
5478 sctp_spin_unlock(&head->lock);
5479 } while (--remaining > 0);
5481 /* Exhausted local port range during search? */
5482 ret = 1;
5483 if (remaining <= 0)
5484 goto fail;
5486 /* OK, here is the one we will use. HEAD (the port
5487 * hash table list entry) is non-NULL and we hold it's
5488 * mutex.
5490 snum = rover;
5491 } else {
5492 /* We are given an specific port number; we verify
5493 * that it is not being used. If it is used, we will
5494 * exahust the search in the hash list corresponding
5495 * to the port number (snum) - we detect that with the
5496 * port iterator, pp being NULL.
5498 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5499 sctp_spin_lock(&head->lock);
5500 sctp_for_each_hentry(pp, node, &head->chain) {
5501 if (pp->port == snum)
5502 goto pp_found;
5505 pp = NULL;
5506 goto pp_not_found;
5507 pp_found:
5508 if (!hlist_empty(&pp->owner)) {
5509 /* We had a port hash table hit - there is an
5510 * available port (pp != NULL) and it is being
5511 * used by other socket (pp->owner not empty); that other
5512 * socket is going to be sk2.
5514 int reuse = sk->sk_reuse;
5515 struct sock *sk2;
5516 struct hlist_node *node;
5518 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5519 if (pp->fastreuse && sk->sk_reuse &&
5520 sk->sk_state != SCTP_SS_LISTENING)
5521 goto success;
5523 /* Run through the list of sockets bound to the port
5524 * (pp->port) [via the pointers bind_next and
5525 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5526 * we get the endpoint they describe and run through
5527 * the endpoint's list of IP (v4 or v6) addresses,
5528 * comparing each of the addresses with the address of
5529 * the socket sk. If we find a match, then that means
5530 * that this port/socket (sk) combination are already
5531 * in an endpoint.
5533 sk_for_each_bound(sk2, node, &pp->owner) {
5534 struct sctp_endpoint *ep2;
5535 ep2 = sctp_sk(sk2)->ep;
5537 if (reuse && sk2->sk_reuse &&
5538 sk2->sk_state != SCTP_SS_LISTENING)
5539 continue;
5541 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
5542 sctp_sk(sk))) {
5543 ret = (long)sk2;
5544 goto fail_unlock;
5547 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5549 pp_not_found:
5550 /* If there was a hash table miss, create a new port. */
5551 ret = 1;
5552 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5553 goto fail_unlock;
5555 /* In either case (hit or miss), make sure fastreuse is 1 only
5556 * if sk->sk_reuse is too (that is, if the caller requested
5557 * SO_REUSEADDR on this socket -sk-).
5559 if (hlist_empty(&pp->owner)) {
5560 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5561 pp->fastreuse = 1;
5562 else
5563 pp->fastreuse = 0;
5564 } else if (pp->fastreuse &&
5565 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5566 pp->fastreuse = 0;
5568 /* We are set, so fill up all the data in the hash table
5569 * entry, tie the socket list information with the rest of the
5570 * sockets FIXME: Blurry, NPI (ipg).
5572 success:
5573 if (!sctp_sk(sk)->bind_hash) {
5574 inet_sk(sk)->num = snum;
5575 sk_add_bind_node(sk, &pp->owner);
5576 sctp_sk(sk)->bind_hash = pp;
5578 ret = 0;
5580 fail_unlock:
5581 sctp_spin_unlock(&head->lock);
5583 fail:
5584 sctp_local_bh_enable();
5585 return ret;
5588 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5589 * port is requested.
5591 static int sctp_get_port(struct sock *sk, unsigned short snum)
5593 long ret;
5594 union sctp_addr addr;
5595 struct sctp_af *af = sctp_sk(sk)->pf->af;
5597 /* Set up a dummy address struct from the sk. */
5598 af->from_sk(&addr, sk);
5599 addr.v4.sin_port = htons(snum);
5601 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5602 ret = sctp_get_port_local(sk, &addr);
5604 return (ret ? 1 : 0);
5608 * 3.1.3 listen() - UDP Style Syntax
5610 * By default, new associations are not accepted for UDP style sockets.
5611 * An application uses listen() to mark a socket as being able to
5612 * accept new associations.
5614 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5616 struct sctp_sock *sp = sctp_sk(sk);
5617 struct sctp_endpoint *ep = sp->ep;
5619 /* Only UDP style sockets that are not peeled off are allowed to
5620 * listen().
5622 if (!sctp_style(sk, UDP))
5623 return -EINVAL;
5625 /* If backlog is zero, disable listening. */
5626 if (!backlog) {
5627 if (sctp_sstate(sk, CLOSED))
5628 return 0;
5630 sctp_unhash_endpoint(ep);
5631 sk->sk_state = SCTP_SS_CLOSED;
5632 return 0;
5635 /* Return if we are already listening. */
5636 if (sctp_sstate(sk, LISTENING))
5637 return 0;
5640 * If a bind() or sctp_bindx() is not called prior to a listen()
5641 * call that allows new associations to be accepted, the system
5642 * picks an ephemeral port and will choose an address set equivalent
5643 * to binding with a wildcard address.
5645 * This is not currently spelled out in the SCTP sockets
5646 * extensions draft, but follows the practice as seen in TCP
5647 * sockets.
5649 * Additionally, turn off fastreuse flag since we are not listening
5651 sk->sk_state = SCTP_SS_LISTENING;
5652 if (!ep->base.bind_addr.port) {
5653 if (sctp_autobind(sk))
5654 return -EAGAIN;
5655 } else
5656 sctp_sk(sk)->bind_hash->fastreuse = 0;
5658 sctp_hash_endpoint(ep);
5659 return 0;
5663 * 4.1.3 listen() - TCP Style Syntax
5665 * Applications uses listen() to ready the SCTP endpoint for accepting
5666 * inbound associations.
5668 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5670 struct sctp_sock *sp = sctp_sk(sk);
5671 struct sctp_endpoint *ep = sp->ep;
5673 /* If backlog is zero, disable listening. */
5674 if (!backlog) {
5675 if (sctp_sstate(sk, CLOSED))
5676 return 0;
5678 sctp_unhash_endpoint(ep);
5679 sk->sk_state = SCTP_SS_CLOSED;
5680 return 0;
5683 if (sctp_sstate(sk, LISTENING))
5684 return 0;
5687 * If a bind() or sctp_bindx() is not called prior to a listen()
5688 * call that allows new associations to be accepted, the system
5689 * picks an ephemeral port and will choose an address set equivalent
5690 * to binding with a wildcard address.
5692 * This is not currently spelled out in the SCTP sockets
5693 * extensions draft, but follows the practice as seen in TCP
5694 * sockets.
5696 sk->sk_state = SCTP_SS_LISTENING;
5697 if (!ep->base.bind_addr.port) {
5698 if (sctp_autobind(sk))
5699 return -EAGAIN;
5700 } else
5701 sctp_sk(sk)->bind_hash->fastreuse = 0;
5703 sk->sk_max_ack_backlog = backlog;
5704 sctp_hash_endpoint(ep);
5705 return 0;
5709 * Move a socket to LISTENING state.
5711 int sctp_inet_listen(struct socket *sock, int backlog)
5713 struct sock *sk = sock->sk;
5714 struct crypto_hash *tfm = NULL;
5715 int err = -EINVAL;
5717 if (unlikely(backlog < 0))
5718 goto out;
5720 sctp_lock_sock(sk);
5722 if (sock->state != SS_UNCONNECTED)
5723 goto out;
5725 /* Allocate HMAC for generating cookie. */
5726 if (sctp_hmac_alg) {
5727 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5728 if (IS_ERR(tfm)) {
5729 if (net_ratelimit()) {
5730 printk(KERN_INFO
5731 "SCTP: failed to load transform for %s: %ld\n",
5732 sctp_hmac_alg, PTR_ERR(tfm));
5734 err = -ENOSYS;
5735 goto out;
5739 switch (sock->type) {
5740 case SOCK_SEQPACKET:
5741 err = sctp_seqpacket_listen(sk, backlog);
5742 break;
5743 case SOCK_STREAM:
5744 err = sctp_stream_listen(sk, backlog);
5745 break;
5746 default:
5747 break;
5750 if (err)
5751 goto cleanup;
5753 /* Store away the transform reference. */
5754 sctp_sk(sk)->hmac = tfm;
5755 out:
5756 sctp_release_sock(sk);
5757 return err;
5758 cleanup:
5759 crypto_free_hash(tfm);
5760 goto out;
5764 * This function is done by modeling the current datagram_poll() and the
5765 * tcp_poll(). Note that, based on these implementations, we don't
5766 * lock the socket in this function, even though it seems that,
5767 * ideally, locking or some other mechanisms can be used to ensure
5768 * the integrity of the counters (sndbuf and wmem_alloc) used
5769 * in this place. We assume that we don't need locks either until proven
5770 * otherwise.
5772 * Another thing to note is that we include the Async I/O support
5773 * here, again, by modeling the current TCP/UDP code. We don't have
5774 * a good way to test with it yet.
5776 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5778 struct sock *sk = sock->sk;
5779 struct sctp_sock *sp = sctp_sk(sk);
5780 unsigned int mask;
5782 poll_wait(file, sk->sk_sleep, wait);
5784 /* A TCP-style listening socket becomes readable when the accept queue
5785 * is not empty.
5787 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5788 return (!list_empty(&sp->ep->asocs)) ?
5789 (POLLIN | POLLRDNORM) : 0;
5791 mask = 0;
5793 /* Is there any exceptional events? */
5794 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5795 mask |= POLLERR;
5796 if (sk->sk_shutdown & RCV_SHUTDOWN)
5797 mask |= POLLRDHUP;
5798 if (sk->sk_shutdown == SHUTDOWN_MASK)
5799 mask |= POLLHUP;
5801 /* Is it readable? Reconsider this code with TCP-style support. */
5802 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5803 (sk->sk_shutdown & RCV_SHUTDOWN))
5804 mask |= POLLIN | POLLRDNORM;
5806 /* The association is either gone or not ready. */
5807 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5808 return mask;
5810 /* Is it writable? */
5811 if (sctp_writeable(sk)) {
5812 mask |= POLLOUT | POLLWRNORM;
5813 } else {
5814 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5816 * Since the socket is not locked, the buffer
5817 * might be made available after the writeable check and
5818 * before the bit is set. This could cause a lost I/O
5819 * signal. tcp_poll() has a race breaker for this race
5820 * condition. Based on their implementation, we put
5821 * in the following code to cover it as well.
5823 if (sctp_writeable(sk))
5824 mask |= POLLOUT | POLLWRNORM;
5826 return mask;
5829 /********************************************************************
5830 * 2nd Level Abstractions
5831 ********************************************************************/
5833 static struct sctp_bind_bucket *sctp_bucket_create(
5834 struct sctp_bind_hashbucket *head, unsigned short snum)
5836 struct sctp_bind_bucket *pp;
5838 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5839 SCTP_DBG_OBJCNT_INC(bind_bucket);
5840 if (pp) {
5841 pp->port = snum;
5842 pp->fastreuse = 0;
5843 INIT_HLIST_HEAD(&pp->owner);
5844 hlist_add_head(&pp->node, &head->chain);
5846 return pp;
5849 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5850 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5852 if (pp && hlist_empty(&pp->owner)) {
5853 __hlist_del(&pp->node);
5854 kmem_cache_free(sctp_bucket_cachep, pp);
5855 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5859 /* Release this socket's reference to a local port. */
5860 static inline void __sctp_put_port(struct sock *sk)
5862 struct sctp_bind_hashbucket *head =
5863 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5864 struct sctp_bind_bucket *pp;
5866 sctp_spin_lock(&head->lock);
5867 pp = sctp_sk(sk)->bind_hash;
5868 __sk_del_bind_node(sk);
5869 sctp_sk(sk)->bind_hash = NULL;
5870 inet_sk(sk)->num = 0;
5871 sctp_bucket_destroy(pp);
5872 sctp_spin_unlock(&head->lock);
5875 void sctp_put_port(struct sock *sk)
5877 sctp_local_bh_disable();
5878 __sctp_put_port(sk);
5879 sctp_local_bh_enable();
5883 * The system picks an ephemeral port and choose an address set equivalent
5884 * to binding with a wildcard address.
5885 * One of those addresses will be the primary address for the association.
5886 * This automatically enables the multihoming capability of SCTP.
5888 static int sctp_autobind(struct sock *sk)
5890 union sctp_addr autoaddr;
5891 struct sctp_af *af;
5892 __be16 port;
5894 /* Initialize a local sockaddr structure to INADDR_ANY. */
5895 af = sctp_sk(sk)->pf->af;
5897 port = htons(inet_sk(sk)->num);
5898 af->inaddr_any(&autoaddr, port);
5900 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5903 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5905 * From RFC 2292
5906 * 4.2 The cmsghdr Structure *
5908 * When ancillary data is sent or received, any number of ancillary data
5909 * objects can be specified by the msg_control and msg_controllen members of
5910 * the msghdr structure, because each object is preceded by
5911 * a cmsghdr structure defining the object's length (the cmsg_len member).
5912 * Historically Berkeley-derived implementations have passed only one object
5913 * at a time, but this API allows multiple objects to be
5914 * passed in a single call to sendmsg() or recvmsg(). The following example
5915 * shows two ancillary data objects in a control buffer.
5917 * |<--------------------------- msg_controllen -------------------------->|
5918 * | |
5920 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5922 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5923 * | | |
5925 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5927 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5928 * | | | | |
5930 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5931 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5933 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5935 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5939 * msg_control
5940 * points here
5942 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5943 sctp_cmsgs_t *cmsgs)
5945 struct cmsghdr *cmsg;
5947 for (cmsg = CMSG_FIRSTHDR(msg);
5948 cmsg != NULL;
5949 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5950 if (!CMSG_OK(msg, cmsg))
5951 return -EINVAL;
5953 /* Should we parse this header or ignore? */
5954 if (cmsg->cmsg_level != IPPROTO_SCTP)
5955 continue;
5957 /* Strictly check lengths following example in SCM code. */
5958 switch (cmsg->cmsg_type) {
5959 case SCTP_INIT:
5960 /* SCTP Socket API Extension
5961 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5963 * This cmsghdr structure provides information for
5964 * initializing new SCTP associations with sendmsg().
5965 * The SCTP_INITMSG socket option uses this same data
5966 * structure. This structure is not used for
5967 * recvmsg().
5969 * cmsg_level cmsg_type cmsg_data[]
5970 * ------------ ------------ ----------------------
5971 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5973 if (cmsg->cmsg_len !=
5974 CMSG_LEN(sizeof(struct sctp_initmsg)))
5975 return -EINVAL;
5976 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5977 break;
5979 case SCTP_SNDRCV:
5980 /* SCTP Socket API Extension
5981 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5983 * This cmsghdr structure specifies SCTP options for
5984 * sendmsg() and describes SCTP header information
5985 * about a received message through recvmsg().
5987 * cmsg_level cmsg_type cmsg_data[]
5988 * ------------ ------------ ----------------------
5989 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5991 if (cmsg->cmsg_len !=
5992 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5993 return -EINVAL;
5995 cmsgs->info =
5996 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5998 /* Minimally, validate the sinfo_flags. */
5999 if (cmsgs->info->sinfo_flags &
6000 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6001 SCTP_ABORT | SCTP_EOF))
6002 return -EINVAL;
6003 break;
6005 default:
6006 return -EINVAL;
6009 return 0;
6013 * Wait for a packet..
6014 * Note: This function is the same function as in core/datagram.c
6015 * with a few modifications to make lksctp work.
6017 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6019 int error;
6020 DEFINE_WAIT(wait);
6022 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
6024 /* Socket errors? */
6025 error = sock_error(sk);
6026 if (error)
6027 goto out;
6029 if (!skb_queue_empty(&sk->sk_receive_queue))
6030 goto ready;
6032 /* Socket shut down? */
6033 if (sk->sk_shutdown & RCV_SHUTDOWN)
6034 goto out;
6036 /* Sequenced packets can come disconnected. If so we report the
6037 * problem.
6039 error = -ENOTCONN;
6041 /* Is there a good reason to think that we may receive some data? */
6042 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6043 goto out;
6045 /* Handle signals. */
6046 if (signal_pending(current))
6047 goto interrupted;
6049 /* Let another process have a go. Since we are going to sleep
6050 * anyway. Note: This may cause odd behaviors if the message
6051 * does not fit in the user's buffer, but this seems to be the
6052 * only way to honor MSG_DONTWAIT realistically.
6054 sctp_release_sock(sk);
6055 *timeo_p = schedule_timeout(*timeo_p);
6056 sctp_lock_sock(sk);
6058 ready:
6059 finish_wait(sk->sk_sleep, &wait);
6060 return 0;
6062 interrupted:
6063 error = sock_intr_errno(*timeo_p);
6065 out:
6066 finish_wait(sk->sk_sleep, &wait);
6067 *err = error;
6068 return error;
6071 /* Receive a datagram.
6072 * Note: This is pretty much the same routine as in core/datagram.c
6073 * with a few changes to make lksctp work.
6075 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6076 int noblock, int *err)
6078 int error;
6079 struct sk_buff *skb;
6080 long timeo;
6082 timeo = sock_rcvtimeo(sk, noblock);
6084 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6085 timeo, MAX_SCHEDULE_TIMEOUT);
6087 do {
6088 /* Again only user level code calls this function,
6089 * so nothing interrupt level
6090 * will suddenly eat the receive_queue.
6092 * Look at current nfs client by the way...
6093 * However, this function was corrent in any case. 8)
6095 if (flags & MSG_PEEK) {
6096 spin_lock_bh(&sk->sk_receive_queue.lock);
6097 skb = skb_peek(&sk->sk_receive_queue);
6098 if (skb)
6099 atomic_inc(&skb->users);
6100 spin_unlock_bh(&sk->sk_receive_queue.lock);
6101 } else {
6102 skb = skb_dequeue(&sk->sk_receive_queue);
6105 if (skb)
6106 return skb;
6108 /* Caller is allowed not to check sk->sk_err before calling. */
6109 error = sock_error(sk);
6110 if (error)
6111 goto no_packet;
6113 if (sk->sk_shutdown & RCV_SHUTDOWN)
6114 break;
6116 /* User doesn't want to wait. */
6117 error = -EAGAIN;
6118 if (!timeo)
6119 goto no_packet;
6120 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6122 return NULL;
6124 no_packet:
6125 *err = error;
6126 return NULL;
6129 /* If sndbuf has changed, wake up per association sndbuf waiters. */
6130 static void __sctp_write_space(struct sctp_association *asoc)
6132 struct sock *sk = asoc->base.sk;
6133 struct socket *sock = sk->sk_socket;
6135 if ((sctp_wspace(asoc) > 0) && sock) {
6136 if (waitqueue_active(&asoc->wait))
6137 wake_up_interruptible(&asoc->wait);
6139 if (sctp_writeable(sk)) {
6140 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
6141 wake_up_interruptible(sk->sk_sleep);
6143 /* Note that we try to include the Async I/O support
6144 * here by modeling from the current TCP/UDP code.
6145 * We have not tested with it yet.
6147 if (sock->fasync_list &&
6148 !(sk->sk_shutdown & SEND_SHUTDOWN))
6149 sock_wake_async(sock,
6150 SOCK_WAKE_SPACE, POLL_OUT);
6155 /* Do accounting for the sndbuf space.
6156 * Decrement the used sndbuf space of the corresponding association by the
6157 * data size which was just transmitted(freed).
6159 static void sctp_wfree(struct sk_buff *skb)
6161 struct sctp_association *asoc;
6162 struct sctp_chunk *chunk;
6163 struct sock *sk;
6165 /* Get the saved chunk pointer. */
6166 chunk = *((struct sctp_chunk **)(skb->cb));
6167 asoc = chunk->asoc;
6168 sk = asoc->base.sk;
6169 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6170 sizeof(struct sk_buff) +
6171 sizeof(struct sctp_chunk);
6173 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6176 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
6178 sk->sk_wmem_queued -= skb->truesize;
6179 sk_mem_uncharge(sk, skb->truesize);
6181 sock_wfree(skb);
6182 __sctp_write_space(asoc);
6184 sctp_association_put(asoc);
6187 /* Do accounting for the receive space on the socket.
6188 * Accounting for the association is done in ulpevent.c
6189 * We set this as a destructor for the cloned data skbs so that
6190 * accounting is done at the correct time.
6192 void sctp_sock_rfree(struct sk_buff *skb)
6194 struct sock *sk = skb->sk;
6195 struct sctp_ulpevent *event = sctp_skb2event(skb);
6197 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
6200 * Mimic the behavior of sock_rfree
6202 sk_mem_uncharge(sk, event->rmem_len);
6206 /* Helper function to wait for space in the sndbuf. */
6207 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6208 size_t msg_len)
6210 struct sock *sk = asoc->base.sk;
6211 int err = 0;
6212 long current_timeo = *timeo_p;
6213 DEFINE_WAIT(wait);
6215 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
6216 asoc, (long)(*timeo_p), msg_len);
6218 /* Increment the association's refcnt. */
6219 sctp_association_hold(asoc);
6221 /* Wait on the association specific sndbuf space. */
6222 for (;;) {
6223 prepare_to_wait_exclusive(&asoc->wait, &wait,
6224 TASK_INTERRUPTIBLE);
6225 if (!*timeo_p)
6226 goto do_nonblock;
6227 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6228 asoc->base.dead)
6229 goto do_error;
6230 if (signal_pending(current))
6231 goto do_interrupted;
6232 if (msg_len <= sctp_wspace(asoc))
6233 break;
6235 /* Let another process have a go. Since we are going
6236 * to sleep anyway.
6238 sctp_release_sock(sk);
6239 current_timeo = schedule_timeout(current_timeo);
6240 BUG_ON(sk != asoc->base.sk);
6241 sctp_lock_sock(sk);
6243 *timeo_p = current_timeo;
6246 out:
6247 finish_wait(&asoc->wait, &wait);
6249 /* Release the association's refcnt. */
6250 sctp_association_put(asoc);
6252 return err;
6254 do_error:
6255 err = -EPIPE;
6256 goto out;
6258 do_interrupted:
6259 err = sock_intr_errno(*timeo_p);
6260 goto out;
6262 do_nonblock:
6263 err = -EAGAIN;
6264 goto out;
6267 /* If socket sndbuf has changed, wake up all per association waiters. */
6268 void sctp_write_space(struct sock *sk)
6270 struct sctp_association *asoc;
6271 struct list_head *pos;
6273 /* Wake up the tasks in each wait queue. */
6274 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
6275 asoc = list_entry(pos, struct sctp_association, asocs);
6276 __sctp_write_space(asoc);
6280 /* Is there any sndbuf space available on the socket?
6282 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
6283 * associations on the same socket. For a UDP-style socket with
6284 * multiple associations, it is possible for it to be "unwriteable"
6285 * prematurely. I assume that this is acceptable because
6286 * a premature "unwriteable" is better than an accidental "writeable" which
6287 * would cause an unwanted block under certain circumstances. For the 1-1
6288 * UDP-style sockets or TCP-style sockets, this code should work.
6289 * - Daisy
6291 static int sctp_writeable(struct sock *sk)
6293 int amt = 0;
6295 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
6296 if (amt < 0)
6297 amt = 0;
6298 return amt;
6301 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6302 * returns immediately with EINPROGRESS.
6304 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6306 struct sock *sk = asoc->base.sk;
6307 int err = 0;
6308 long current_timeo = *timeo_p;
6309 DEFINE_WAIT(wait);
6311 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
6312 (long)(*timeo_p));
6314 /* Increment the association's refcnt. */
6315 sctp_association_hold(asoc);
6317 for (;;) {
6318 prepare_to_wait_exclusive(&asoc->wait, &wait,
6319 TASK_INTERRUPTIBLE);
6320 if (!*timeo_p)
6321 goto do_nonblock;
6322 if (sk->sk_shutdown & RCV_SHUTDOWN)
6323 break;
6324 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6325 asoc->base.dead)
6326 goto do_error;
6327 if (signal_pending(current))
6328 goto do_interrupted;
6330 if (sctp_state(asoc, ESTABLISHED))
6331 break;
6333 /* Let another process have a go. Since we are going
6334 * to sleep anyway.
6336 sctp_release_sock(sk);
6337 current_timeo = schedule_timeout(current_timeo);
6338 sctp_lock_sock(sk);
6340 *timeo_p = current_timeo;
6343 out:
6344 finish_wait(&asoc->wait, &wait);
6346 /* Release the association's refcnt. */
6347 sctp_association_put(asoc);
6349 return err;
6351 do_error:
6352 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
6353 err = -ETIMEDOUT;
6354 else
6355 err = -ECONNREFUSED;
6356 goto out;
6358 do_interrupted:
6359 err = sock_intr_errno(*timeo_p);
6360 goto out;
6362 do_nonblock:
6363 err = -EINPROGRESS;
6364 goto out;
6367 static int sctp_wait_for_accept(struct sock *sk, long timeo)
6369 struct sctp_endpoint *ep;
6370 int err = 0;
6371 DEFINE_WAIT(wait);
6373 ep = sctp_sk(sk)->ep;
6376 for (;;) {
6377 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
6378 TASK_INTERRUPTIBLE);
6380 if (list_empty(&ep->asocs)) {
6381 sctp_release_sock(sk);
6382 timeo = schedule_timeout(timeo);
6383 sctp_lock_sock(sk);
6386 err = -EINVAL;
6387 if (!sctp_sstate(sk, LISTENING))
6388 break;
6390 err = 0;
6391 if (!list_empty(&ep->asocs))
6392 break;
6394 err = sock_intr_errno(timeo);
6395 if (signal_pending(current))
6396 break;
6398 err = -EAGAIN;
6399 if (!timeo)
6400 break;
6403 finish_wait(sk->sk_sleep, &wait);
6405 return err;
6408 static void sctp_wait_for_close(struct sock *sk, long timeout)
6410 DEFINE_WAIT(wait);
6412 do {
6413 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
6414 if (list_empty(&sctp_sk(sk)->ep->asocs))
6415 break;
6416 sctp_release_sock(sk);
6417 timeout = schedule_timeout(timeout);
6418 sctp_lock_sock(sk);
6419 } while (!signal_pending(current) && timeout);
6421 finish_wait(sk->sk_sleep, &wait);
6424 static void sctp_sock_rfree_frag(struct sk_buff *skb)
6426 struct sk_buff *frag;
6428 if (!skb->data_len)
6429 goto done;
6431 /* Don't forget the fragments. */
6432 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6433 sctp_sock_rfree_frag(frag);
6435 done:
6436 sctp_sock_rfree(skb);
6439 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6441 struct sk_buff *frag;
6443 if (!skb->data_len)
6444 goto done;
6446 /* Don't forget the fragments. */
6447 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6448 sctp_skb_set_owner_r_frag(frag, sk);
6450 done:
6451 sctp_skb_set_owner_r(skb, sk);
6454 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6455 * and its messages to the newsk.
6457 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6458 struct sctp_association *assoc,
6459 sctp_socket_type_t type)
6461 struct sctp_sock *oldsp = sctp_sk(oldsk);
6462 struct sctp_sock *newsp = sctp_sk(newsk);
6463 struct sctp_bind_bucket *pp; /* hash list port iterator */
6464 struct sctp_endpoint *newep = newsp->ep;
6465 struct sk_buff *skb, *tmp;
6466 struct sctp_ulpevent *event;
6467 struct sctp_bind_hashbucket *head;
6469 /* Migrate socket buffer sizes and all the socket level options to the
6470 * new socket.
6472 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6473 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6474 /* Brute force copy old sctp opt. */
6475 inet_sk_copy_descendant(newsk, oldsk);
6477 /* Restore the ep value that was overwritten with the above structure
6478 * copy.
6480 newsp->ep = newep;
6481 newsp->hmac = NULL;
6483 /* Hook this new socket in to the bind_hash list. */
6484 head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->num)];
6485 sctp_local_bh_disable();
6486 sctp_spin_lock(&head->lock);
6487 pp = sctp_sk(oldsk)->bind_hash;
6488 sk_add_bind_node(newsk, &pp->owner);
6489 sctp_sk(newsk)->bind_hash = pp;
6490 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6491 sctp_spin_unlock(&head->lock);
6492 sctp_local_bh_enable();
6494 /* Copy the bind_addr list from the original endpoint to the new
6495 * endpoint so that we can handle restarts properly
6497 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6498 &oldsp->ep->base.bind_addr, GFP_KERNEL);
6500 /* Move any messages in the old socket's receive queue that are for the
6501 * peeled off association to the new socket's receive queue.
6503 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6504 event = sctp_skb2event(skb);
6505 if (event->asoc == assoc) {
6506 sctp_sock_rfree_frag(skb);
6507 __skb_unlink(skb, &oldsk->sk_receive_queue);
6508 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6509 sctp_skb_set_owner_r_frag(skb, newsk);
6513 /* Clean up any messages pending delivery due to partial
6514 * delivery. Three cases:
6515 * 1) No partial deliver; no work.
6516 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6517 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6519 skb_queue_head_init(&newsp->pd_lobby);
6520 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6522 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6523 struct sk_buff_head *queue;
6525 /* Decide which queue to move pd_lobby skbs to. */
6526 if (assoc->ulpq.pd_mode) {
6527 queue = &newsp->pd_lobby;
6528 } else
6529 queue = &newsk->sk_receive_queue;
6531 /* Walk through the pd_lobby, looking for skbs that
6532 * need moved to the new socket.
6534 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6535 event = sctp_skb2event(skb);
6536 if (event->asoc == assoc) {
6537 sctp_sock_rfree_frag(skb);
6538 __skb_unlink(skb, &oldsp->pd_lobby);
6539 __skb_queue_tail(queue, skb);
6540 sctp_skb_set_owner_r_frag(skb, newsk);
6544 /* Clear up any skbs waiting for the partial
6545 * delivery to finish.
6547 if (assoc->ulpq.pd_mode)
6548 sctp_clear_pd(oldsk, NULL);
6552 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6553 sctp_sock_rfree_frag(skb);
6554 sctp_skb_set_owner_r_frag(skb, newsk);
6557 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6558 sctp_sock_rfree_frag(skb);
6559 sctp_skb_set_owner_r_frag(skb, newsk);
6562 /* Set the type of socket to indicate that it is peeled off from the
6563 * original UDP-style socket or created with the accept() call on a
6564 * TCP-style socket..
6566 newsp->type = type;
6568 /* Mark the new socket "in-use" by the user so that any packets
6569 * that may arrive on the association after we've moved it are
6570 * queued to the backlog. This prevents a potential race between
6571 * backlog processing on the old socket and new-packet processing
6572 * on the new socket.
6574 * The caller has just allocated newsk so we can guarantee that other
6575 * paths won't try to lock it and then oldsk.
6577 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6578 sctp_assoc_migrate(assoc, newsk);
6580 /* If the association on the newsk is already closed before accept()
6581 * is called, set RCV_SHUTDOWN flag.
6583 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6584 newsk->sk_shutdown |= RCV_SHUTDOWN;
6586 newsk->sk_state = SCTP_SS_ESTABLISHED;
6587 sctp_release_sock(newsk);
6591 DEFINE_PROTO_INUSE(sctp)
6593 /* This proto struct describes the ULP interface for SCTP. */
6594 struct proto sctp_prot = {
6595 .name = "SCTP",
6596 .owner = THIS_MODULE,
6597 .close = sctp_close,
6598 .connect = sctp_connect,
6599 .disconnect = sctp_disconnect,
6600 .accept = sctp_accept,
6601 .ioctl = sctp_ioctl,
6602 .init = sctp_init_sock,
6603 .destroy = sctp_destroy_sock,
6604 .shutdown = sctp_shutdown,
6605 .setsockopt = sctp_setsockopt,
6606 .getsockopt = sctp_getsockopt,
6607 .sendmsg = sctp_sendmsg,
6608 .recvmsg = sctp_recvmsg,
6609 .bind = sctp_bind,
6610 .backlog_rcv = sctp_backlog_rcv,
6611 .hash = sctp_hash,
6612 .unhash = sctp_unhash,
6613 .get_port = sctp_get_port,
6614 .obj_size = sizeof(struct sctp_sock),
6615 .sysctl_mem = sysctl_sctp_mem,
6616 .sysctl_rmem = sysctl_sctp_rmem,
6617 .sysctl_wmem = sysctl_sctp_wmem,
6618 .memory_pressure = &sctp_memory_pressure,
6619 .enter_memory_pressure = sctp_enter_memory_pressure,
6620 .memory_allocated = &sctp_memory_allocated,
6621 <<<<<<< HEAD:net/sctp/socket.c
6622 =======
6623 .sockets_allocated = &sctp_sockets_allocated,
6624 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
6625 REF_PROTO_INUSE(sctp)
6628 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6629 DEFINE_PROTO_INUSE(sctpv6)
6631 struct proto sctpv6_prot = {
6632 .name = "SCTPv6",
6633 .owner = THIS_MODULE,
6634 .close = sctp_close,
6635 .connect = sctp_connect,
6636 .disconnect = sctp_disconnect,
6637 .accept = sctp_accept,
6638 .ioctl = sctp_ioctl,
6639 .init = sctp_init_sock,
6640 .destroy = sctp_destroy_sock,
6641 .shutdown = sctp_shutdown,
6642 .setsockopt = sctp_setsockopt,
6643 .getsockopt = sctp_getsockopt,
6644 .sendmsg = sctp_sendmsg,
6645 .recvmsg = sctp_recvmsg,
6646 .bind = sctp_bind,
6647 .backlog_rcv = sctp_backlog_rcv,
6648 .hash = sctp_hash,
6649 .unhash = sctp_unhash,
6650 .get_port = sctp_get_port,
6651 .obj_size = sizeof(struct sctp6_sock),
6652 .sysctl_mem = sysctl_sctp_mem,
6653 .sysctl_rmem = sysctl_sctp_rmem,
6654 .sysctl_wmem = sysctl_sctp_wmem,
6655 .memory_pressure = &sctp_memory_pressure,
6656 .enter_memory_pressure = sctp_enter_memory_pressure,
6657 .memory_allocated = &sctp_memory_allocated,
6658 <<<<<<< HEAD:net/sctp/socket.c
6659 =======
6660 .sockets_allocated = &sctp_sockets_allocated,
6661 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sctp/socket.c
6662 REF_PROTO_INUSE(sctpv6)
6664 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */