]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sctp/socket.c
SCTP: Fix thinko in sctp_copy_laddrs()
[mirror_ubuntu-bionic-kernel.git] / net / sctp / socket.c
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
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.
17 *
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
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.
34 *
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 *
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
41 *
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>
55 *
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
58 */
59
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>
70
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>
76
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>
81
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.
85 */
86
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;
109
110 extern struct kmem_cache *sctp_bucket_cachep;
111
112 /* Get the sndbuf space available at the time on the association. */
113 static inline int sctp_wspace(struct sctp_association *asoc)
114 {
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
117
118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
121 } else {
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
124 }
125
126 if (amt < 0)
127 amt = 0;
128
129 return amt;
130 }
131
132 /* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
135 *
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
139 * tracking.
140 */
141 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
142 {
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
145
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
148
149 skb_set_owner_w(chunk->skb, sk);
150
151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
154
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
158
159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
160 }
161
162 /* Verify that this is a valid address. */
163 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
164 int len)
165 {
166 struct sctp_af *af;
167
168 /* Verify basic sockaddr. */
169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
170 if (!af)
171 return -EINVAL;
172
173 /* Is this a valid SCTP address? */
174 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
175 return -EINVAL;
176
177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
178 return -EINVAL;
179
180 return 0;
181 }
182
183 /* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
185 */
186 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
187 {
188 struct sctp_association *asoc = NULL;
189
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk, UDP)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
195 */
196 if (!sctp_sstate(sk, ESTABLISHED))
197 return NULL;
198
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk)->ep->asocs))
201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
202 struct sctp_association, asocs);
203 return asoc;
204 }
205
206 /* Otherwise this is a UDP-style socket. */
207 if (!id || (id == (sctp_assoc_t)-1))
208 return NULL;
209
210 spin_lock_bh(&sctp_assocs_id_lock);
211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
212 spin_unlock_bh(&sctp_assocs_id_lock);
213
214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
215 return NULL;
216
217 return asoc;
218 }
219
220 /* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
222 * the same.
223 */
224 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
225 struct sockaddr_storage *addr,
226 sctp_assoc_t id)
227 {
228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
229 struct sctp_transport *transport;
230 union sctp_addr *laddr = (union sctp_addr *)addr;
231
232 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
233 laddr,
234 &transport);
235
236 if (!addr_asoc)
237 return NULL;
238
239 id_asoc = sctp_id2assoc(sk, id);
240 if (id_asoc && (id_asoc != addr_asoc))
241 return NULL;
242
243 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
244 (union sctp_addr *)addr);
245
246 return transport;
247 }
248
249 /* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
251 *
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
253 *
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
258 */
259 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
260 {
261 int retval = 0;
262
263 sctp_lock_sock(sk);
264
265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
266 sk, addr, addr_len);
267
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk)->ep->base.bind_addr.port)
270 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
271 addr_len);
272 else
273 retval = -EINVAL;
274
275 sctp_release_sock(sk);
276
277 return retval;
278 }
279
280 static long sctp_get_port_local(struct sock *, union sctp_addr *);
281
282 /* Verify this is a valid sockaddr. */
283 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
284 union sctp_addr *addr, int len)
285 {
286 struct sctp_af *af;
287
288 /* Check minimum size. */
289 if (len < sizeof (struct sockaddr))
290 return NULL;
291
292 /* Does this PF support this AF? */
293 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
294 return NULL;
295
296 /* If we get this far, af is valid. */
297 af = sctp_get_af_specific(addr->sa.sa_family);
298
299 if (len < af->sockaddr_len)
300 return NULL;
301
302 return af;
303 }
304
305 /* Bind a local address either to an endpoint or to an association. */
306 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
307 {
308 struct sctp_sock *sp = sctp_sk(sk);
309 struct sctp_endpoint *ep = sp->ep;
310 struct sctp_bind_addr *bp = &ep->base.bind_addr;
311 struct sctp_af *af;
312 unsigned short snum;
313 int ret = 0;
314
315 /* Common sockaddr verification. */
316 af = sctp_sockaddr_af(sp, addr, len);
317 if (!af) {
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
319 sk, addr, len);
320 return -EINVAL;
321 }
322
323 snum = ntohs(addr->v4.sin_port);
324
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
327 sk,
328 addr,
329 bp->port, snum,
330 len);
331
332 /* PF specific bind() address verification. */
333 if (!sp->pf->bind_verify(sp, addr))
334 return -EADDRNOTAVAIL;
335
336 /* We must either be unbound, or bind to the same port.
337 * It's OK to allow 0 ports if we are already bound.
338 * We'll just inhert an already bound port in this case
339 */
340 if (bp->port) {
341 if (!snum)
342 snum = bp->port;
343 else if (snum != bp->port) {
344 SCTP_DEBUG_PRINTK("sctp_do_bind:"
345 " New port %d does not match existing port "
346 "%d.\n", snum, bp->port);
347 return -EINVAL;
348 }
349 }
350
351 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
352 return -EACCES;
353
354 /* Make sure we are allowed to bind here.
355 * The function sctp_get_port_local() does duplicate address
356 * detection.
357 */
358 if ((ret = sctp_get_port_local(sk, addr))) {
359 if (ret == (long) sk) {
360 /* This endpoint has a conflicting address. */
361 return -EINVAL;
362 } else {
363 return -EADDRINUSE;
364 }
365 }
366
367 /* Refresh ephemeral port. */
368 if (!bp->port)
369 bp->port = inet_sk(sk)->num;
370
371 /* Add the address to the bind address list. */
372 sctp_local_bh_disable();
373 sctp_write_lock(&ep->base.addr_lock);
374
375 /* Use GFP_ATOMIC since BHs are disabled. */
376 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
377 sctp_write_unlock(&ep->base.addr_lock);
378 sctp_local_bh_enable();
379
380 /* Copy back into socket for getsockname() use. */
381 if (!ret) {
382 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
383 af->to_sk_saddr(addr, sk);
384 }
385
386 return ret;
387 }
388
389 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
390 *
391 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
392 * at any one time. If a sender, after sending an ASCONF chunk, decides
393 * it needs to transfer another ASCONF Chunk, it MUST wait until the
394 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
395 * subsequent ASCONF. Note this restriction binds each side, so at any
396 * time two ASCONF may be in-transit on any given association (one sent
397 * from each endpoint).
398 */
399 static int sctp_send_asconf(struct sctp_association *asoc,
400 struct sctp_chunk *chunk)
401 {
402 int retval = 0;
403
404 /* If there is an outstanding ASCONF chunk, queue it for later
405 * transmission.
406 */
407 if (asoc->addip_last_asconf) {
408 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
409 goto out;
410 }
411
412 /* Hold the chunk until an ASCONF_ACK is received. */
413 sctp_chunk_hold(chunk);
414 retval = sctp_primitive_ASCONF(asoc, chunk);
415 if (retval)
416 sctp_chunk_free(chunk);
417 else
418 asoc->addip_last_asconf = chunk;
419
420 out:
421 return retval;
422 }
423
424 /* Add a list of addresses as bind addresses to local endpoint or
425 * association.
426 *
427 * Basically run through each address specified in the addrs/addrcnt
428 * array/length pair, determine if it is IPv6 or IPv4 and call
429 * sctp_do_bind() on it.
430 *
431 * If any of them fails, then the operation will be reversed and the
432 * ones that were added will be removed.
433 *
434 * Only sctp_setsockopt_bindx() is supposed to call this function.
435 */
436 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
437 {
438 int cnt;
439 int retval = 0;
440 void *addr_buf;
441 struct sockaddr *sa_addr;
442 struct sctp_af *af;
443
444 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
445 sk, addrs, addrcnt);
446
447 addr_buf = addrs;
448 for (cnt = 0; cnt < addrcnt; cnt++) {
449 /* The list may contain either IPv4 or IPv6 address;
450 * determine the address length for walking thru the list.
451 */
452 sa_addr = (struct sockaddr *)addr_buf;
453 af = sctp_get_af_specific(sa_addr->sa_family);
454 if (!af) {
455 retval = -EINVAL;
456 goto err_bindx_add;
457 }
458
459 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
460 af->sockaddr_len);
461
462 addr_buf += af->sockaddr_len;
463
464 err_bindx_add:
465 if (retval < 0) {
466 /* Failed. Cleanup the ones that have been added */
467 if (cnt > 0)
468 sctp_bindx_rem(sk, addrs, cnt);
469 return retval;
470 }
471 }
472
473 return retval;
474 }
475
476 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
477 * associations that are part of the endpoint indicating that a list of local
478 * addresses are added to the endpoint.
479 *
480 * If any of the addresses is already in the bind address list of the
481 * association, we do not send the chunk for that association. But it will not
482 * affect other associations.
483 *
484 * Only sctp_setsockopt_bindx() is supposed to call this function.
485 */
486 static int sctp_send_asconf_add_ip(struct sock *sk,
487 struct sockaddr *addrs,
488 int addrcnt)
489 {
490 struct sctp_sock *sp;
491 struct sctp_endpoint *ep;
492 struct sctp_association *asoc;
493 struct sctp_bind_addr *bp;
494 struct sctp_chunk *chunk;
495 struct sctp_sockaddr_entry *laddr;
496 union sctp_addr *addr;
497 union sctp_addr saveaddr;
498 void *addr_buf;
499 struct sctp_af *af;
500 struct list_head *pos;
501 struct list_head *p;
502 int i;
503 int retval = 0;
504
505 if (!sctp_addip_enable)
506 return retval;
507
508 sp = sctp_sk(sk);
509 ep = sp->ep;
510
511 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
512 __FUNCTION__, sk, addrs, addrcnt);
513
514 list_for_each(pos, &ep->asocs) {
515 asoc = list_entry(pos, struct sctp_association, asocs);
516
517 if (!asoc->peer.asconf_capable)
518 continue;
519
520 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
521 continue;
522
523 if (!sctp_state(asoc, ESTABLISHED))
524 continue;
525
526 /* Check if any address in the packed array of addresses is
527 * in the bind address list of the association. If so,
528 * do not send the asconf chunk to its peer, but continue with
529 * other associations.
530 */
531 addr_buf = addrs;
532 for (i = 0; i < addrcnt; i++) {
533 addr = (union sctp_addr *)addr_buf;
534 af = sctp_get_af_specific(addr->v4.sin_family);
535 if (!af) {
536 retval = -EINVAL;
537 goto out;
538 }
539
540 if (sctp_assoc_lookup_laddr(asoc, addr))
541 break;
542
543 addr_buf += af->sockaddr_len;
544 }
545 if (i < addrcnt)
546 continue;
547
548 /* Use the first address in bind addr list of association as
549 * Address Parameter of ASCONF CHUNK.
550 */
551 sctp_read_lock(&asoc->base.addr_lock);
552 bp = &asoc->base.bind_addr;
553 p = bp->address_list.next;
554 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
555 sctp_read_unlock(&asoc->base.addr_lock);
556
557 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
558 addrcnt, SCTP_PARAM_ADD_IP);
559 if (!chunk) {
560 retval = -ENOMEM;
561 goto out;
562 }
563
564 retval = sctp_send_asconf(asoc, chunk);
565 if (retval)
566 goto out;
567
568 /* Add the new addresses to the bind address list with
569 * use_as_src set to 0.
570 */
571 sctp_local_bh_disable();
572 sctp_write_lock(&asoc->base.addr_lock);
573 addr_buf = addrs;
574 for (i = 0; i < addrcnt; i++) {
575 addr = (union sctp_addr *)addr_buf;
576 af = sctp_get_af_specific(addr->v4.sin_family);
577 memcpy(&saveaddr, addr, af->sockaddr_len);
578 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
579 GFP_ATOMIC);
580 addr_buf += af->sockaddr_len;
581 }
582 sctp_write_unlock(&asoc->base.addr_lock);
583 sctp_local_bh_enable();
584 }
585
586 out:
587 return retval;
588 }
589
590 /* Remove a list of addresses from bind addresses list. Do not remove the
591 * last address.
592 *
593 * Basically run through each address specified in the addrs/addrcnt
594 * array/length pair, determine if it is IPv6 or IPv4 and call
595 * sctp_del_bind() on it.
596 *
597 * If any of them fails, then the operation will be reversed and the
598 * ones that were removed will be added back.
599 *
600 * At least one address has to be left; if only one address is
601 * available, the operation will return -EBUSY.
602 *
603 * Only sctp_setsockopt_bindx() is supposed to call this function.
604 */
605 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
606 {
607 struct sctp_sock *sp = sctp_sk(sk);
608 struct sctp_endpoint *ep = sp->ep;
609 int cnt;
610 struct sctp_bind_addr *bp = &ep->base.bind_addr;
611 int retval = 0;
612 void *addr_buf;
613 union sctp_addr *sa_addr;
614 struct sctp_af *af;
615
616 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
617 sk, addrs, addrcnt);
618
619 addr_buf = addrs;
620 for (cnt = 0; cnt < addrcnt; cnt++) {
621 /* If the bind address list is empty or if there is only one
622 * bind address, there is nothing more to be removed (we need
623 * at least one address here).
624 */
625 if (list_empty(&bp->address_list) ||
626 (sctp_list_single_entry(&bp->address_list))) {
627 retval = -EBUSY;
628 goto err_bindx_rem;
629 }
630
631 sa_addr = (union sctp_addr *)addr_buf;
632 af = sctp_get_af_specific(sa_addr->sa.sa_family);
633 if (!af) {
634 retval = -EINVAL;
635 goto err_bindx_rem;
636 }
637
638 if (!af->addr_valid(sa_addr, sp, NULL)) {
639 retval = -EADDRNOTAVAIL;
640 goto err_bindx_rem;
641 }
642
643 if (sa_addr->v4.sin_port != htons(bp->port)) {
644 retval = -EINVAL;
645 goto err_bindx_rem;
646 }
647
648 /* FIXME - There is probably a need to check if sk->sk_saddr and
649 * sk->sk_rcv_addr are currently set to one of the addresses to
650 * be removed. This is something which needs to be looked into
651 * when we are fixing the outstanding issues with multi-homing
652 * socket routing and failover schemes. Refer to comments in
653 * sctp_do_bind(). -daisy
654 */
655 sctp_local_bh_disable();
656 sctp_write_lock(&ep->base.addr_lock);
657
658 retval = sctp_del_bind_addr(bp, sa_addr);
659
660 sctp_write_unlock(&ep->base.addr_lock);
661 sctp_local_bh_enable();
662
663 addr_buf += af->sockaddr_len;
664 err_bindx_rem:
665 if (retval < 0) {
666 /* Failed. Add the ones that has been removed back */
667 if (cnt > 0)
668 sctp_bindx_add(sk, addrs, cnt);
669 return retval;
670 }
671 }
672
673 return retval;
674 }
675
676 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
677 * the associations that are part of the endpoint indicating that a list of
678 * local addresses are removed from the endpoint.
679 *
680 * If any of the addresses is already in the bind address list of the
681 * association, we do not send the chunk for that association. But it will not
682 * affect other associations.
683 *
684 * Only sctp_setsockopt_bindx() is supposed to call this function.
685 */
686 static int sctp_send_asconf_del_ip(struct sock *sk,
687 struct sockaddr *addrs,
688 int addrcnt)
689 {
690 struct sctp_sock *sp;
691 struct sctp_endpoint *ep;
692 struct sctp_association *asoc;
693 struct sctp_transport *transport;
694 struct sctp_bind_addr *bp;
695 struct sctp_chunk *chunk;
696 union sctp_addr *laddr;
697 void *addr_buf;
698 struct sctp_af *af;
699 struct list_head *pos, *pos1;
700 struct sctp_sockaddr_entry *saddr;
701 int i;
702 int retval = 0;
703
704 if (!sctp_addip_enable)
705 return retval;
706
707 sp = sctp_sk(sk);
708 ep = sp->ep;
709
710 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
711 __FUNCTION__, sk, addrs, addrcnt);
712
713 list_for_each(pos, &ep->asocs) {
714 asoc = list_entry(pos, struct sctp_association, asocs);
715
716 if (!asoc->peer.asconf_capable)
717 continue;
718
719 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
720 continue;
721
722 if (!sctp_state(asoc, ESTABLISHED))
723 continue;
724
725 /* Check if any address in the packed array of addresses is
726 * not present in the bind address list of the association.
727 * If so, do not send the asconf chunk to its peer, but
728 * continue with other associations.
729 */
730 addr_buf = addrs;
731 for (i = 0; i < addrcnt; i++) {
732 laddr = (union sctp_addr *)addr_buf;
733 af = sctp_get_af_specific(laddr->v4.sin_family);
734 if (!af) {
735 retval = -EINVAL;
736 goto out;
737 }
738
739 if (!sctp_assoc_lookup_laddr(asoc, laddr))
740 break;
741
742 addr_buf += af->sockaddr_len;
743 }
744 if (i < addrcnt)
745 continue;
746
747 /* Find one address in the association's bind address list
748 * that is not in the packed array of addresses. This is to
749 * make sure that we do not delete all the addresses in the
750 * association.
751 */
752 sctp_read_lock(&asoc->base.addr_lock);
753 bp = &asoc->base.bind_addr;
754 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
755 addrcnt, sp);
756 sctp_read_unlock(&asoc->base.addr_lock);
757 if (!laddr)
758 continue;
759
760 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
761 SCTP_PARAM_DEL_IP);
762 if (!chunk) {
763 retval = -ENOMEM;
764 goto out;
765 }
766
767 /* Reset use_as_src flag for the addresses in the bind address
768 * list that are to be deleted.
769 */
770 sctp_local_bh_disable();
771 sctp_write_lock(&asoc->base.addr_lock);
772 addr_buf = addrs;
773 for (i = 0; i < addrcnt; i++) {
774 laddr = (union sctp_addr *)addr_buf;
775 af = sctp_get_af_specific(laddr->v4.sin_family);
776 list_for_each(pos1, &bp->address_list) {
777 saddr = list_entry(pos1,
778 struct sctp_sockaddr_entry,
779 list);
780 if (sctp_cmp_addr_exact(&saddr->a, laddr))
781 saddr->use_as_src = 0;
782 }
783 addr_buf += af->sockaddr_len;
784 }
785 sctp_write_unlock(&asoc->base.addr_lock);
786 sctp_local_bh_enable();
787
788 /* Update the route and saddr entries for all the transports
789 * as some of the addresses in the bind address list are
790 * about to be deleted and cannot be used as source addresses.
791 */
792 list_for_each(pos1, &asoc->peer.transport_addr_list) {
793 transport = list_entry(pos1, struct sctp_transport,
794 transports);
795 dst_release(transport->dst);
796 sctp_transport_route(transport, NULL,
797 sctp_sk(asoc->base.sk));
798 }
799
800 retval = sctp_send_asconf(asoc, chunk);
801 }
802 out:
803 return retval;
804 }
805
806 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
807 *
808 * API 8.1
809 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
810 * int flags);
811 *
812 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
813 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
814 * or IPv6 addresses.
815 *
816 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
817 * Section 3.1.2 for this usage.
818 *
819 * addrs is a pointer to an array of one or more socket addresses. Each
820 * address is contained in its appropriate structure (i.e. struct
821 * sockaddr_in or struct sockaddr_in6) the family of the address type
822 * must be used to distinguish the address length (note that this
823 * representation is termed a "packed array" of addresses). The caller
824 * specifies the number of addresses in the array with addrcnt.
825 *
826 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
827 * -1, and sets errno to the appropriate error code.
828 *
829 * For SCTP, the port given in each socket address must be the same, or
830 * sctp_bindx() will fail, setting errno to EINVAL.
831 *
832 * The flags parameter is formed from the bitwise OR of zero or more of
833 * the following currently defined flags:
834 *
835 * SCTP_BINDX_ADD_ADDR
836 *
837 * SCTP_BINDX_REM_ADDR
838 *
839 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
840 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
841 * addresses from the association. The two flags are mutually exclusive;
842 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
843 * not remove all addresses from an association; sctp_bindx() will
844 * reject such an attempt with EINVAL.
845 *
846 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
847 * additional addresses with an endpoint after calling bind(). Or use
848 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
849 * socket is associated with so that no new association accepted will be
850 * associated with those addresses. If the endpoint supports dynamic
851 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
852 * endpoint to send the appropriate message to the peer to change the
853 * peers address lists.
854 *
855 * Adding and removing addresses from a connected association is
856 * optional functionality. Implementations that do not support this
857 * functionality should return EOPNOTSUPP.
858 *
859 * Basically do nothing but copying the addresses from user to kernel
860 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
861 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
862 * from userspace.
863 *
864 * We don't use copy_from_user() for optimization: we first do the
865 * sanity checks (buffer size -fast- and access check-healthy
866 * pointer); if all of those succeed, then we can alloc the memory
867 * (expensive operation) needed to copy the data to kernel. Then we do
868 * the copying without checking the user space area
869 * (__copy_from_user()).
870 *
871 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
872 * it.
873 *
874 * sk The sk of the socket
875 * addrs The pointer to the addresses in user land
876 * addrssize Size of the addrs buffer
877 * op Operation to perform (add or remove, see the flags of
878 * sctp_bindx)
879 *
880 * Returns 0 if ok, <0 errno code on error.
881 */
882 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
883 struct sockaddr __user *addrs,
884 int addrs_size, int op)
885 {
886 struct sockaddr *kaddrs;
887 int err;
888 int addrcnt = 0;
889 int walk_size = 0;
890 struct sockaddr *sa_addr;
891 void *addr_buf;
892 struct sctp_af *af;
893
894 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
895 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
896
897 if (unlikely(addrs_size <= 0))
898 return -EINVAL;
899
900 /* Check the user passed a healthy pointer. */
901 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
902 return -EFAULT;
903
904 /* Alloc space for the address array in kernel memory. */
905 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
906 if (unlikely(!kaddrs))
907 return -ENOMEM;
908
909 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
910 kfree(kaddrs);
911 return -EFAULT;
912 }
913
914 /* Walk through the addrs buffer and count the number of addresses. */
915 addr_buf = kaddrs;
916 while (walk_size < addrs_size) {
917 sa_addr = (struct sockaddr *)addr_buf;
918 af = sctp_get_af_specific(sa_addr->sa_family);
919
920 /* If the address family is not supported or if this address
921 * causes the address buffer to overflow return EINVAL.
922 */
923 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
924 kfree(kaddrs);
925 return -EINVAL;
926 }
927 addrcnt++;
928 addr_buf += af->sockaddr_len;
929 walk_size += af->sockaddr_len;
930 }
931
932 /* Do the work. */
933 switch (op) {
934 case SCTP_BINDX_ADD_ADDR:
935 err = sctp_bindx_add(sk, kaddrs, addrcnt);
936 if (err)
937 goto out;
938 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
939 break;
940
941 case SCTP_BINDX_REM_ADDR:
942 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
943 if (err)
944 goto out;
945 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
946 break;
947
948 default:
949 err = -EINVAL;
950 break;
951 }
952
953 out:
954 kfree(kaddrs);
955
956 return err;
957 }
958
959 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
960 *
961 * Common routine for handling connect() and sctp_connectx().
962 * Connect will come in with just a single address.
963 */
964 static int __sctp_connect(struct sock* sk,
965 struct sockaddr *kaddrs,
966 int addrs_size)
967 {
968 struct sctp_sock *sp;
969 struct sctp_endpoint *ep;
970 struct sctp_association *asoc = NULL;
971 struct sctp_association *asoc2;
972 struct sctp_transport *transport;
973 union sctp_addr to;
974 struct sctp_af *af;
975 sctp_scope_t scope;
976 long timeo;
977 int err = 0;
978 int addrcnt = 0;
979 int walk_size = 0;
980 union sctp_addr *sa_addr;
981 void *addr_buf;
982 unsigned short port;
983
984 sp = sctp_sk(sk);
985 ep = sp->ep;
986
987 /* connect() cannot be done on a socket that is already in ESTABLISHED
988 * state - UDP-style peeled off socket or a TCP-style socket that
989 * is already connected.
990 * It cannot be done even on a TCP-style listening socket.
991 */
992 if (sctp_sstate(sk, ESTABLISHED) ||
993 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
994 err = -EISCONN;
995 goto out_free;
996 }
997
998 /* Walk through the addrs buffer and count the number of addresses. */
999 addr_buf = kaddrs;
1000 while (walk_size < addrs_size) {
1001 sa_addr = (union sctp_addr *)addr_buf;
1002 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1003 port = ntohs(sa_addr->v4.sin_port);
1004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
1007 */
1008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 err = -EINVAL;
1010 goto out_free;
1011 }
1012
1013 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
1014 if (err)
1015 goto out_free;
1016
1017 /* Make sure the destination port is correctly set
1018 * in all addresses.
1019 */
1020 if (asoc && asoc->peer.port && asoc->peer.port != port)
1021 goto out_free;
1022
1023 memcpy(&to, sa_addr, af->sockaddr_len);
1024
1025 /* Check if there already is a matching association on the
1026 * endpoint (other than the one created here).
1027 */
1028 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
1029 if (asoc2 && asoc2 != asoc) {
1030 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1031 err = -EISCONN;
1032 else
1033 err = -EALREADY;
1034 goto out_free;
1035 }
1036
1037 /* If we could not find a matching association on the endpoint,
1038 * make sure that there is no peeled-off association matching
1039 * the peer address even on another socket.
1040 */
1041 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
1042 err = -EADDRNOTAVAIL;
1043 goto out_free;
1044 }
1045
1046 if (!asoc) {
1047 /* If a bind() or sctp_bindx() is not called prior to
1048 * an sctp_connectx() call, the system picks an
1049 * ephemeral port and will choose an address set
1050 * equivalent to binding with a wildcard address.
1051 */
1052 if (!ep->base.bind_addr.port) {
1053 if (sctp_autobind(sk)) {
1054 err = -EAGAIN;
1055 goto out_free;
1056 }
1057 } else {
1058 /*
1059 * If an unprivileged user inherits a 1-many
1060 * style socket with open associations on a
1061 * privileged port, it MAY be permitted to
1062 * accept new associations, but it SHOULD NOT
1063 * be permitted to open new associations.
1064 */
1065 if (ep->base.bind_addr.port < PROT_SOCK &&
1066 !capable(CAP_NET_BIND_SERVICE)) {
1067 err = -EACCES;
1068 goto out_free;
1069 }
1070 }
1071
1072 scope = sctp_scope(sa_addr);
1073 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1074 if (!asoc) {
1075 err = -ENOMEM;
1076 goto out_free;
1077 }
1078 }
1079
1080 /* Prime the peer's transport structures. */
1081 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL,
1082 SCTP_UNKNOWN);
1083 if (!transport) {
1084 err = -ENOMEM;
1085 goto out_free;
1086 }
1087
1088 addrcnt++;
1089 addr_buf += af->sockaddr_len;
1090 walk_size += af->sockaddr_len;
1091 }
1092
1093 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1094 if (err < 0) {
1095 goto out_free;
1096 }
1097
1098 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1099 if (err < 0) {
1100 goto out_free;
1101 }
1102
1103 /* Initialize sk's dport and daddr for getpeername() */
1104 inet_sk(sk)->dport = htons(asoc->peer.port);
1105 af = sctp_get_af_specific(to.sa.sa_family);
1106 af->to_sk_daddr(&to, sk);
1107 sk->sk_err = 0;
1108
1109 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1110 err = sctp_wait_for_connect(asoc, &timeo);
1111
1112 /* Don't free association on exit. */
1113 asoc = NULL;
1114
1115 out_free:
1116
1117 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1118 " kaddrs: %p err: %d\n",
1119 asoc, kaddrs, err);
1120 if (asoc)
1121 sctp_association_free(asoc);
1122 return err;
1123 }
1124
1125 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1126 *
1127 * API 8.9
1128 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1129 *
1130 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1131 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1132 * or IPv6 addresses.
1133 *
1134 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1135 * Section 3.1.2 for this usage.
1136 *
1137 * addrs is a pointer to an array of one or more socket addresses. Each
1138 * address is contained in its appropriate structure (i.e. struct
1139 * sockaddr_in or struct sockaddr_in6) the family of the address type
1140 * must be used to distengish the address length (note that this
1141 * representation is termed a "packed array" of addresses). The caller
1142 * specifies the number of addresses in the array with addrcnt.
1143 *
1144 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1145 * -1, and sets errno to the appropriate error code.
1146 *
1147 * For SCTP, the port given in each socket address must be the same, or
1148 * sctp_connectx() will fail, setting errno to EINVAL.
1149 *
1150 * An application can use sctp_connectx to initiate an association with
1151 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1152 * allows a caller to specify multiple addresses at which a peer can be
1153 * reached. The way the SCTP stack uses the list of addresses to set up
1154 * the association is implementation dependant. This function only
1155 * specifies that the stack will try to make use of all the addresses in
1156 * the list when needed.
1157 *
1158 * Note that the list of addresses passed in is only used for setting up
1159 * the association. It does not necessarily equal the set of addresses
1160 * the peer uses for the resulting association. If the caller wants to
1161 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1162 * retrieve them after the association has been set up.
1163 *
1164 * Basically do nothing but copying the addresses from user to kernel
1165 * land and invoking either sctp_connectx(). This is used for tunneling
1166 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1167 *
1168 * We don't use copy_from_user() for optimization: we first do the
1169 * sanity checks (buffer size -fast- and access check-healthy
1170 * pointer); if all of those succeed, then we can alloc the memory
1171 * (expensive operation) needed to copy the data to kernel. Then we do
1172 * the copying without checking the user space area
1173 * (__copy_from_user()).
1174 *
1175 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1176 * it.
1177 *
1178 * sk The sk of the socket
1179 * addrs The pointer to the addresses in user land
1180 * addrssize Size of the addrs buffer
1181 *
1182 * Returns 0 if ok, <0 errno code on error.
1183 */
1184 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1185 struct sockaddr __user *addrs,
1186 int addrs_size)
1187 {
1188 int err = 0;
1189 struct sockaddr *kaddrs;
1190
1191 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1192 __FUNCTION__, sk, addrs, addrs_size);
1193
1194 if (unlikely(addrs_size <= 0))
1195 return -EINVAL;
1196
1197 /* Check the user passed a healthy pointer. */
1198 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1199 return -EFAULT;
1200
1201 /* Alloc space for the address array in kernel memory. */
1202 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1203 if (unlikely(!kaddrs))
1204 return -ENOMEM;
1205
1206 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1207 err = -EFAULT;
1208 } else {
1209 err = __sctp_connect(sk, kaddrs, addrs_size);
1210 }
1211
1212 kfree(kaddrs);
1213 return err;
1214 }
1215
1216 /* API 3.1.4 close() - UDP Style Syntax
1217 * Applications use close() to perform graceful shutdown (as described in
1218 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1219 * by a UDP-style socket.
1220 *
1221 * The syntax is
1222 *
1223 * ret = close(int sd);
1224 *
1225 * sd - the socket descriptor of the associations to be closed.
1226 *
1227 * To gracefully shutdown a specific association represented by the
1228 * UDP-style socket, an application should use the sendmsg() call,
1229 * passing no user data, but including the appropriate flag in the
1230 * ancillary data (see Section xxxx).
1231 *
1232 * If sd in the close() call is a branched-off socket representing only
1233 * one association, the shutdown is performed on that association only.
1234 *
1235 * 4.1.6 close() - TCP Style Syntax
1236 *
1237 * Applications use close() to gracefully close down an association.
1238 *
1239 * The syntax is:
1240 *
1241 * int close(int sd);
1242 *
1243 * sd - the socket descriptor of the association to be closed.
1244 *
1245 * After an application calls close() on a socket descriptor, no further
1246 * socket operations will succeed on that descriptor.
1247 *
1248 * API 7.1.4 SO_LINGER
1249 *
1250 * An application using the TCP-style socket can use this option to
1251 * perform the SCTP ABORT primitive. The linger option structure is:
1252 *
1253 * struct linger {
1254 * int l_onoff; // option on/off
1255 * int l_linger; // linger time
1256 * };
1257 *
1258 * To enable the option, set l_onoff to 1. If the l_linger value is set
1259 * to 0, calling close() is the same as the ABORT primitive. If the
1260 * value is set to a negative value, the setsockopt() call will return
1261 * an error. If the value is set to a positive value linger_time, the
1262 * close() can be blocked for at most linger_time ms. If the graceful
1263 * shutdown phase does not finish during this period, close() will
1264 * return but the graceful shutdown phase continues in the system.
1265 */
1266 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1267 {
1268 struct sctp_endpoint *ep;
1269 struct sctp_association *asoc;
1270 struct list_head *pos, *temp;
1271
1272 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1273
1274 sctp_lock_sock(sk);
1275 sk->sk_shutdown = SHUTDOWN_MASK;
1276
1277 ep = sctp_sk(sk)->ep;
1278
1279 /* Walk all associations on an endpoint. */
1280 list_for_each_safe(pos, temp, &ep->asocs) {
1281 asoc = list_entry(pos, struct sctp_association, asocs);
1282
1283 if (sctp_style(sk, TCP)) {
1284 /* A closed association can still be in the list if
1285 * it belongs to a TCP-style listening socket that is
1286 * not yet accepted. If so, free it. If not, send an
1287 * ABORT or SHUTDOWN based on the linger options.
1288 */
1289 if (sctp_state(asoc, CLOSED)) {
1290 sctp_unhash_established(asoc);
1291 sctp_association_free(asoc);
1292 continue;
1293 }
1294 }
1295
1296 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1297 struct sctp_chunk *chunk;
1298
1299 chunk = sctp_make_abort_user(asoc, NULL, 0);
1300 if (chunk)
1301 sctp_primitive_ABORT(asoc, chunk);
1302 } else
1303 sctp_primitive_SHUTDOWN(asoc, NULL);
1304 }
1305
1306 /* Clean up any skbs sitting on the receive queue. */
1307 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1308 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1309
1310 /* On a TCP-style socket, block for at most linger_time if set. */
1311 if (sctp_style(sk, TCP) && timeout)
1312 sctp_wait_for_close(sk, timeout);
1313
1314 /* This will run the backlog queue. */
1315 sctp_release_sock(sk);
1316
1317 /* Supposedly, no process has access to the socket, but
1318 * the net layers still may.
1319 */
1320 sctp_local_bh_disable();
1321 sctp_bh_lock_sock(sk);
1322
1323 /* Hold the sock, since sk_common_release() will put sock_put()
1324 * and we have just a little more cleanup.
1325 */
1326 sock_hold(sk);
1327 sk_common_release(sk);
1328
1329 sctp_bh_unlock_sock(sk);
1330 sctp_local_bh_enable();
1331
1332 sock_put(sk);
1333
1334 SCTP_DBG_OBJCNT_DEC(sock);
1335 }
1336
1337 /* Handle EPIPE error. */
1338 static int sctp_error(struct sock *sk, int flags, int err)
1339 {
1340 if (err == -EPIPE)
1341 err = sock_error(sk) ? : -EPIPE;
1342 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1343 send_sig(SIGPIPE, current, 0);
1344 return err;
1345 }
1346
1347 /* API 3.1.3 sendmsg() - UDP Style Syntax
1348 *
1349 * An application uses sendmsg() and recvmsg() calls to transmit data to
1350 * and receive data from its peer.
1351 *
1352 * ssize_t sendmsg(int socket, const struct msghdr *message,
1353 * int flags);
1354 *
1355 * socket - the socket descriptor of the endpoint.
1356 * message - pointer to the msghdr structure which contains a single
1357 * user message and possibly some ancillary data.
1358 *
1359 * See Section 5 for complete description of the data
1360 * structures.
1361 *
1362 * flags - flags sent or received with the user message, see Section
1363 * 5 for complete description of the flags.
1364 *
1365 * Note: This function could use a rewrite especially when explicit
1366 * connect support comes in.
1367 */
1368 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1369
1370 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1371
1372 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1373 struct msghdr *msg, size_t msg_len)
1374 {
1375 struct sctp_sock *sp;
1376 struct sctp_endpoint *ep;
1377 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1378 struct sctp_transport *transport, *chunk_tp;
1379 struct sctp_chunk *chunk;
1380 union sctp_addr to;
1381 struct sockaddr *msg_name = NULL;
1382 struct sctp_sndrcvinfo default_sinfo = { 0 };
1383 struct sctp_sndrcvinfo *sinfo;
1384 struct sctp_initmsg *sinit;
1385 sctp_assoc_t associd = 0;
1386 sctp_cmsgs_t cmsgs = { NULL };
1387 int err;
1388 sctp_scope_t scope;
1389 long timeo;
1390 __u16 sinfo_flags = 0;
1391 struct sctp_datamsg *datamsg;
1392 struct list_head *pos;
1393 int msg_flags = msg->msg_flags;
1394
1395 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1396 sk, msg, msg_len);
1397
1398 err = 0;
1399 sp = sctp_sk(sk);
1400 ep = sp->ep;
1401
1402 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1403
1404 /* We cannot send a message over a TCP-style listening socket. */
1405 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1406 err = -EPIPE;
1407 goto out_nounlock;
1408 }
1409
1410 /* Parse out the SCTP CMSGs. */
1411 err = sctp_msghdr_parse(msg, &cmsgs);
1412
1413 if (err) {
1414 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1415 goto out_nounlock;
1416 }
1417
1418 /* Fetch the destination address for this packet. This
1419 * address only selects the association--it is not necessarily
1420 * the address we will send to.
1421 * For a peeled-off socket, msg_name is ignored.
1422 */
1423 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1424 int msg_namelen = msg->msg_namelen;
1425
1426 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1427 msg_namelen);
1428 if (err)
1429 return err;
1430
1431 if (msg_namelen > sizeof(to))
1432 msg_namelen = sizeof(to);
1433 memcpy(&to, msg->msg_name, msg_namelen);
1434 msg_name = msg->msg_name;
1435 }
1436
1437 sinfo = cmsgs.info;
1438 sinit = cmsgs.init;
1439
1440 /* Did the user specify SNDRCVINFO? */
1441 if (sinfo) {
1442 sinfo_flags = sinfo->sinfo_flags;
1443 associd = sinfo->sinfo_assoc_id;
1444 }
1445
1446 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1447 msg_len, sinfo_flags);
1448
1449 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1450 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1451 err = -EINVAL;
1452 goto out_nounlock;
1453 }
1454
1455 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1456 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1457 * If SCTP_ABORT is set, the message length could be non zero with
1458 * the msg_iov set to the user abort reason.
1459 */
1460 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1461 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1462 err = -EINVAL;
1463 goto out_nounlock;
1464 }
1465
1466 /* If SCTP_ADDR_OVER is set, there must be an address
1467 * specified in msg_name.
1468 */
1469 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1470 err = -EINVAL;
1471 goto out_nounlock;
1472 }
1473
1474 transport = NULL;
1475
1476 SCTP_DEBUG_PRINTK("About to look up association.\n");
1477
1478 sctp_lock_sock(sk);
1479
1480 /* If a msg_name has been specified, assume this is to be used. */
1481 if (msg_name) {
1482 /* Look for a matching association on the endpoint. */
1483 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1484 if (!asoc) {
1485 /* If we could not find a matching association on the
1486 * endpoint, make sure that it is not a TCP-style
1487 * socket that already has an association or there is
1488 * no peeled-off association on another socket.
1489 */
1490 if ((sctp_style(sk, TCP) &&
1491 sctp_sstate(sk, ESTABLISHED)) ||
1492 sctp_endpoint_is_peeled_off(ep, &to)) {
1493 err = -EADDRNOTAVAIL;
1494 goto out_unlock;
1495 }
1496 }
1497 } else {
1498 asoc = sctp_id2assoc(sk, associd);
1499 if (!asoc) {
1500 err = -EPIPE;
1501 goto out_unlock;
1502 }
1503 }
1504
1505 if (asoc) {
1506 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1507
1508 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1509 * socket that has an association in CLOSED state. This can
1510 * happen when an accepted socket has an association that is
1511 * already CLOSED.
1512 */
1513 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1514 err = -EPIPE;
1515 goto out_unlock;
1516 }
1517
1518 if (sinfo_flags & SCTP_EOF) {
1519 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1520 asoc);
1521 sctp_primitive_SHUTDOWN(asoc, NULL);
1522 err = 0;
1523 goto out_unlock;
1524 }
1525 if (sinfo_flags & SCTP_ABORT) {
1526 struct sctp_chunk *chunk;
1527
1528 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1529 if (!chunk) {
1530 err = -ENOMEM;
1531 goto out_unlock;
1532 }
1533
1534 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1535 sctp_primitive_ABORT(asoc, chunk);
1536 err = 0;
1537 goto out_unlock;
1538 }
1539 }
1540
1541 /* Do we need to create the association? */
1542 if (!asoc) {
1543 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1544
1545 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1546 err = -EINVAL;
1547 goto out_unlock;
1548 }
1549
1550 /* Check for invalid stream against the stream counts,
1551 * either the default or the user specified stream counts.
1552 */
1553 if (sinfo) {
1554 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1555 /* Check against the defaults. */
1556 if (sinfo->sinfo_stream >=
1557 sp->initmsg.sinit_num_ostreams) {
1558 err = -EINVAL;
1559 goto out_unlock;
1560 }
1561 } else {
1562 /* Check against the requested. */
1563 if (sinfo->sinfo_stream >=
1564 sinit->sinit_num_ostreams) {
1565 err = -EINVAL;
1566 goto out_unlock;
1567 }
1568 }
1569 }
1570
1571 /*
1572 * API 3.1.2 bind() - UDP Style Syntax
1573 * If a bind() or sctp_bindx() is not called prior to a
1574 * sendmsg() call that initiates a new association, the
1575 * system picks an ephemeral port and will choose an address
1576 * set equivalent to binding with a wildcard address.
1577 */
1578 if (!ep->base.bind_addr.port) {
1579 if (sctp_autobind(sk)) {
1580 err = -EAGAIN;
1581 goto out_unlock;
1582 }
1583 } else {
1584 /*
1585 * If an unprivileged user inherits a one-to-many
1586 * style socket with open associations on a privileged
1587 * port, it MAY be permitted to accept new associations,
1588 * but it SHOULD NOT be permitted to open new
1589 * associations.
1590 */
1591 if (ep->base.bind_addr.port < PROT_SOCK &&
1592 !capable(CAP_NET_BIND_SERVICE)) {
1593 err = -EACCES;
1594 goto out_unlock;
1595 }
1596 }
1597
1598 scope = sctp_scope(&to);
1599 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1600 if (!new_asoc) {
1601 err = -ENOMEM;
1602 goto out_unlock;
1603 }
1604 asoc = new_asoc;
1605
1606 /* If the SCTP_INIT ancillary data is specified, set all
1607 * the association init values accordingly.
1608 */
1609 if (sinit) {
1610 if (sinit->sinit_num_ostreams) {
1611 asoc->c.sinit_num_ostreams =
1612 sinit->sinit_num_ostreams;
1613 }
1614 if (sinit->sinit_max_instreams) {
1615 asoc->c.sinit_max_instreams =
1616 sinit->sinit_max_instreams;
1617 }
1618 if (sinit->sinit_max_attempts) {
1619 asoc->max_init_attempts
1620 = sinit->sinit_max_attempts;
1621 }
1622 if (sinit->sinit_max_init_timeo) {
1623 asoc->max_init_timeo =
1624 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1625 }
1626 }
1627
1628 /* Prime the peer's transport structures. */
1629 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1630 if (!transport) {
1631 err = -ENOMEM;
1632 goto out_free;
1633 }
1634 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1635 if (err < 0) {
1636 err = -ENOMEM;
1637 goto out_free;
1638 }
1639 }
1640
1641 /* ASSERT: we have a valid association at this point. */
1642 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1643
1644 if (!sinfo) {
1645 /* If the user didn't specify SNDRCVINFO, make up one with
1646 * some defaults.
1647 */
1648 default_sinfo.sinfo_stream = asoc->default_stream;
1649 default_sinfo.sinfo_flags = asoc->default_flags;
1650 default_sinfo.sinfo_ppid = asoc->default_ppid;
1651 default_sinfo.sinfo_context = asoc->default_context;
1652 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1653 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1654 sinfo = &default_sinfo;
1655 }
1656
1657 /* API 7.1.7, the sndbuf size per association bounds the
1658 * maximum size of data that can be sent in a single send call.
1659 */
1660 if (msg_len > sk->sk_sndbuf) {
1661 err = -EMSGSIZE;
1662 goto out_free;
1663 }
1664
1665 if (asoc->pmtu_pending)
1666 sctp_assoc_pending_pmtu(asoc);
1667
1668 /* If fragmentation is disabled and the message length exceeds the
1669 * association fragmentation point, return EMSGSIZE. The I-D
1670 * does not specify what this error is, but this looks like
1671 * a great fit.
1672 */
1673 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1674 err = -EMSGSIZE;
1675 goto out_free;
1676 }
1677
1678 if (sinfo) {
1679 /* Check for invalid stream. */
1680 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1681 err = -EINVAL;
1682 goto out_free;
1683 }
1684 }
1685
1686 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1687 if (!sctp_wspace(asoc)) {
1688 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1689 if (err)
1690 goto out_free;
1691 }
1692
1693 /* If an address is passed with the sendto/sendmsg call, it is used
1694 * to override the primary destination address in the TCP model, or
1695 * when SCTP_ADDR_OVER flag is set in the UDP model.
1696 */
1697 if ((sctp_style(sk, TCP) && msg_name) ||
1698 (sinfo_flags & SCTP_ADDR_OVER)) {
1699 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1700 if (!chunk_tp) {
1701 err = -EINVAL;
1702 goto out_free;
1703 }
1704 } else
1705 chunk_tp = NULL;
1706
1707 /* Auto-connect, if we aren't connected already. */
1708 if (sctp_state(asoc, CLOSED)) {
1709 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1710 if (err < 0)
1711 goto out_free;
1712 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1713 }
1714
1715 /* Break the message into multiple chunks of maximum size. */
1716 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1717 if (!datamsg) {
1718 err = -ENOMEM;
1719 goto out_free;
1720 }
1721
1722 /* Now send the (possibly) fragmented message. */
1723 list_for_each(pos, &datamsg->chunks) {
1724 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1725 sctp_datamsg_track(chunk);
1726
1727 /* Do accounting for the write space. */
1728 sctp_set_owner_w(chunk);
1729
1730 chunk->transport = chunk_tp;
1731
1732 /* Send it to the lower layers. Note: all chunks
1733 * must either fail or succeed. The lower layer
1734 * works that way today. Keep it that way or this
1735 * breaks.
1736 */
1737 err = sctp_primitive_SEND(asoc, chunk);
1738 /* Did the lower layer accept the chunk? */
1739 if (err)
1740 sctp_chunk_free(chunk);
1741 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1742 }
1743
1744 sctp_datamsg_free(datamsg);
1745 if (err)
1746 goto out_free;
1747 else
1748 err = msg_len;
1749
1750 /* If we are already past ASSOCIATE, the lower
1751 * layers are responsible for association cleanup.
1752 */
1753 goto out_unlock;
1754
1755 out_free:
1756 if (new_asoc)
1757 sctp_association_free(asoc);
1758 out_unlock:
1759 sctp_release_sock(sk);
1760
1761 out_nounlock:
1762 return sctp_error(sk, msg_flags, err);
1763
1764 #if 0
1765 do_sock_err:
1766 if (msg_len)
1767 err = msg_len;
1768 else
1769 err = sock_error(sk);
1770 goto out;
1771
1772 do_interrupted:
1773 if (msg_len)
1774 err = msg_len;
1775 goto out;
1776 #endif /* 0 */
1777 }
1778
1779 /* This is an extended version of skb_pull() that removes the data from the
1780 * start of a skb even when data is spread across the list of skb's in the
1781 * frag_list. len specifies the total amount of data that needs to be removed.
1782 * when 'len' bytes could be removed from the skb, it returns 0.
1783 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1784 * could not be removed.
1785 */
1786 static int sctp_skb_pull(struct sk_buff *skb, int len)
1787 {
1788 struct sk_buff *list;
1789 int skb_len = skb_headlen(skb);
1790 int rlen;
1791
1792 if (len <= skb_len) {
1793 __skb_pull(skb, len);
1794 return 0;
1795 }
1796 len -= skb_len;
1797 __skb_pull(skb, skb_len);
1798
1799 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1800 rlen = sctp_skb_pull(list, len);
1801 skb->len -= (len-rlen);
1802 skb->data_len -= (len-rlen);
1803
1804 if (!rlen)
1805 return 0;
1806
1807 len = rlen;
1808 }
1809
1810 return len;
1811 }
1812
1813 /* API 3.1.3 recvmsg() - UDP Style Syntax
1814 *
1815 * ssize_t recvmsg(int socket, struct msghdr *message,
1816 * int flags);
1817 *
1818 * socket - the socket descriptor of the endpoint.
1819 * message - pointer to the msghdr structure which contains a single
1820 * user message and possibly some ancillary data.
1821 *
1822 * See Section 5 for complete description of the data
1823 * structures.
1824 *
1825 * flags - flags sent or received with the user message, see Section
1826 * 5 for complete description of the flags.
1827 */
1828 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1829
1830 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1831 struct msghdr *msg, size_t len, int noblock,
1832 int flags, int *addr_len)
1833 {
1834 struct sctp_ulpevent *event = NULL;
1835 struct sctp_sock *sp = sctp_sk(sk);
1836 struct sk_buff *skb;
1837 int copied;
1838 int err = 0;
1839 int skb_len;
1840
1841 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1842 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1843 "len", len, "knoblauch", noblock,
1844 "flags", flags, "addr_len", addr_len);
1845
1846 sctp_lock_sock(sk);
1847
1848 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1849 err = -ENOTCONN;
1850 goto out;
1851 }
1852
1853 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1854 if (!skb)
1855 goto out;
1856
1857 /* Get the total length of the skb including any skb's in the
1858 * frag_list.
1859 */
1860 skb_len = skb->len;
1861
1862 copied = skb_len;
1863 if (copied > len)
1864 copied = len;
1865
1866 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1867
1868 event = sctp_skb2event(skb);
1869
1870 if (err)
1871 goto out_free;
1872
1873 sock_recv_timestamp(msg, sk, skb);
1874 if (sctp_ulpevent_is_notification(event)) {
1875 msg->msg_flags |= MSG_NOTIFICATION;
1876 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1877 } else {
1878 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1879 }
1880
1881 /* Check if we allow SCTP_SNDRCVINFO. */
1882 if (sp->subscribe.sctp_data_io_event)
1883 sctp_ulpevent_read_sndrcvinfo(event, msg);
1884 #if 0
1885 /* FIXME: we should be calling IP/IPv6 layers. */
1886 if (sk->sk_protinfo.af_inet.cmsg_flags)
1887 ip_cmsg_recv(msg, skb);
1888 #endif
1889
1890 err = copied;
1891
1892 /* If skb's length exceeds the user's buffer, update the skb and
1893 * push it back to the receive_queue so that the next call to
1894 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1895 */
1896 if (skb_len > copied) {
1897 msg->msg_flags &= ~MSG_EOR;
1898 if (flags & MSG_PEEK)
1899 goto out_free;
1900 sctp_skb_pull(skb, copied);
1901 skb_queue_head(&sk->sk_receive_queue, skb);
1902
1903 /* When only partial message is copied to the user, increase
1904 * rwnd by that amount. If all the data in the skb is read,
1905 * rwnd is updated when the event is freed.
1906 */
1907 sctp_assoc_rwnd_increase(event->asoc, copied);
1908 goto out;
1909 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1910 (event->msg_flags & MSG_EOR))
1911 msg->msg_flags |= MSG_EOR;
1912 else
1913 msg->msg_flags &= ~MSG_EOR;
1914
1915 out_free:
1916 if (flags & MSG_PEEK) {
1917 /* Release the skb reference acquired after peeking the skb in
1918 * sctp_skb_recv_datagram().
1919 */
1920 kfree_skb(skb);
1921 } else {
1922 /* Free the event which includes releasing the reference to
1923 * the owner of the skb, freeing the skb and updating the
1924 * rwnd.
1925 */
1926 sctp_ulpevent_free(event);
1927 }
1928 out:
1929 sctp_release_sock(sk);
1930 return err;
1931 }
1932
1933 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1934 *
1935 * This option is a on/off flag. If enabled no SCTP message
1936 * fragmentation will be performed. Instead if a message being sent
1937 * exceeds the current PMTU size, the message will NOT be sent and
1938 * instead a error will be indicated to the user.
1939 */
1940 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1941 char __user *optval, int optlen)
1942 {
1943 int val;
1944
1945 if (optlen < sizeof(int))
1946 return -EINVAL;
1947
1948 if (get_user(val, (int __user *)optval))
1949 return -EFAULT;
1950
1951 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1952
1953 return 0;
1954 }
1955
1956 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1957 int optlen)
1958 {
1959 if (optlen != sizeof(struct sctp_event_subscribe))
1960 return -EINVAL;
1961 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1962 return -EFAULT;
1963 return 0;
1964 }
1965
1966 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1967 *
1968 * This socket option is applicable to the UDP-style socket only. When
1969 * set it will cause associations that are idle for more than the
1970 * specified number of seconds to automatically close. An association
1971 * being idle is defined an association that has NOT sent or received
1972 * user data. The special value of '0' indicates that no automatic
1973 * close of any associations should be performed. The option expects an
1974 * integer defining the number of seconds of idle time before an
1975 * association is closed.
1976 */
1977 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1978 int optlen)
1979 {
1980 struct sctp_sock *sp = sctp_sk(sk);
1981
1982 /* Applicable to UDP-style socket only */
1983 if (sctp_style(sk, TCP))
1984 return -EOPNOTSUPP;
1985 if (optlen != sizeof(int))
1986 return -EINVAL;
1987 if (copy_from_user(&sp->autoclose, optval, optlen))
1988 return -EFAULT;
1989
1990 return 0;
1991 }
1992
1993 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1994 *
1995 * Applications can enable or disable heartbeats for any peer address of
1996 * an association, modify an address's heartbeat interval, force a
1997 * heartbeat to be sent immediately, and adjust the address's maximum
1998 * number of retransmissions sent before an address is considered
1999 * unreachable. The following structure is used to access and modify an
2000 * address's parameters:
2001 *
2002 * struct sctp_paddrparams {
2003 * sctp_assoc_t spp_assoc_id;
2004 * struct sockaddr_storage spp_address;
2005 * uint32_t spp_hbinterval;
2006 * uint16_t spp_pathmaxrxt;
2007 * uint32_t spp_pathmtu;
2008 * uint32_t spp_sackdelay;
2009 * uint32_t spp_flags;
2010 * };
2011 *
2012 * spp_assoc_id - (one-to-many style socket) This is filled in the
2013 * application, and identifies the association for
2014 * this query.
2015 * spp_address - This specifies which address is of interest.
2016 * spp_hbinterval - This contains the value of the heartbeat interval,
2017 * in milliseconds. If a value of zero
2018 * is present in this field then no changes are to
2019 * be made to this parameter.
2020 * spp_pathmaxrxt - This contains the maximum number of
2021 * retransmissions before this address shall be
2022 * considered unreachable. If a value of zero
2023 * is present in this field then no changes are to
2024 * be made to this parameter.
2025 * spp_pathmtu - When Path MTU discovery is disabled the value
2026 * specified here will be the "fixed" path mtu.
2027 * Note that if the spp_address field is empty
2028 * then all associations on this address will
2029 * have this fixed path mtu set upon them.
2030 *
2031 * spp_sackdelay - When delayed sack is enabled, this value specifies
2032 * the number of milliseconds that sacks will be delayed
2033 * for. This value will apply to all addresses of an
2034 * association if the spp_address field is empty. Note
2035 * also, that if delayed sack is enabled and this
2036 * value is set to 0, no change is made to the last
2037 * recorded delayed sack timer value.
2038 *
2039 * spp_flags - These flags are used to control various features
2040 * on an association. The flag field may contain
2041 * zero or more of the following options.
2042 *
2043 * SPP_HB_ENABLE - Enable heartbeats on the
2044 * specified address. Note that if the address
2045 * field is empty all addresses for the association
2046 * have heartbeats enabled upon them.
2047 *
2048 * SPP_HB_DISABLE - Disable heartbeats on the
2049 * speicifed address. Note that if the address
2050 * field is empty all addresses for the association
2051 * will have their heartbeats disabled. Note also
2052 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2053 * mutually exclusive, only one of these two should
2054 * be specified. Enabling both fields will have
2055 * undetermined results.
2056 *
2057 * SPP_HB_DEMAND - Request a user initiated heartbeat
2058 * to be made immediately.
2059 *
2060 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2061 * heartbeat delayis to be set to the value of 0
2062 * milliseconds.
2063 *
2064 * SPP_PMTUD_ENABLE - This field will enable PMTU
2065 * discovery upon the specified address. Note that
2066 * if the address feild is empty then all addresses
2067 * on the association are effected.
2068 *
2069 * SPP_PMTUD_DISABLE - This field will disable PMTU
2070 * discovery upon the specified address. Note that
2071 * if the address feild is empty then all addresses
2072 * on the association are effected. Not also that
2073 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2074 * exclusive. Enabling both will have undetermined
2075 * results.
2076 *
2077 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2078 * on delayed sack. The time specified in spp_sackdelay
2079 * is used to specify the sack delay for this address. Note
2080 * that if spp_address is empty then all addresses will
2081 * enable delayed sack and take on the sack delay
2082 * value specified in spp_sackdelay.
2083 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2084 * off delayed sack. If the spp_address field is blank then
2085 * delayed sack is disabled for the entire association. Note
2086 * also that this field is mutually exclusive to
2087 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2088 * results.
2089 */
2090 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2091 struct sctp_transport *trans,
2092 struct sctp_association *asoc,
2093 struct sctp_sock *sp,
2094 int hb_change,
2095 int pmtud_change,
2096 int sackdelay_change)
2097 {
2098 int error;
2099
2100 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2101 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2102 if (error)
2103 return error;
2104 }
2105
2106 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2107 * this field is ignored. Note also that a value of zero indicates
2108 * the current setting should be left unchanged.
2109 */
2110 if (params->spp_flags & SPP_HB_ENABLE) {
2111
2112 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2113 * set. This lets us use 0 value when this flag
2114 * is set.
2115 */
2116 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2117 params->spp_hbinterval = 0;
2118
2119 if (params->spp_hbinterval ||
2120 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2121 if (trans) {
2122 trans->hbinterval =
2123 msecs_to_jiffies(params->spp_hbinterval);
2124 } else if (asoc) {
2125 asoc->hbinterval =
2126 msecs_to_jiffies(params->spp_hbinterval);
2127 } else {
2128 sp->hbinterval = params->spp_hbinterval;
2129 }
2130 }
2131 }
2132
2133 if (hb_change) {
2134 if (trans) {
2135 trans->param_flags =
2136 (trans->param_flags & ~SPP_HB) | hb_change;
2137 } else if (asoc) {
2138 asoc->param_flags =
2139 (asoc->param_flags & ~SPP_HB) | hb_change;
2140 } else {
2141 sp->param_flags =
2142 (sp->param_flags & ~SPP_HB) | hb_change;
2143 }
2144 }
2145
2146 /* When Path MTU discovery is disabled the value specified here will
2147 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2148 * include the flag SPP_PMTUD_DISABLE for this field to have any
2149 * effect).
2150 */
2151 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2152 if (trans) {
2153 trans->pathmtu = params->spp_pathmtu;
2154 sctp_assoc_sync_pmtu(asoc);
2155 } else if (asoc) {
2156 asoc->pathmtu = params->spp_pathmtu;
2157 sctp_frag_point(sp, params->spp_pathmtu);
2158 } else {
2159 sp->pathmtu = params->spp_pathmtu;
2160 }
2161 }
2162
2163 if (pmtud_change) {
2164 if (trans) {
2165 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2166 (params->spp_flags & SPP_PMTUD_ENABLE);
2167 trans->param_flags =
2168 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2169 if (update) {
2170 sctp_transport_pmtu(trans);
2171 sctp_assoc_sync_pmtu(asoc);
2172 }
2173 } else if (asoc) {
2174 asoc->param_flags =
2175 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2176 } else {
2177 sp->param_flags =
2178 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2179 }
2180 }
2181
2182 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2183 * value of this field is ignored. Note also that a value of zero
2184 * indicates the current setting should be left unchanged.
2185 */
2186 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2187 if (trans) {
2188 trans->sackdelay =
2189 msecs_to_jiffies(params->spp_sackdelay);
2190 } else if (asoc) {
2191 asoc->sackdelay =
2192 msecs_to_jiffies(params->spp_sackdelay);
2193 } else {
2194 sp->sackdelay = params->spp_sackdelay;
2195 }
2196 }
2197
2198 if (sackdelay_change) {
2199 if (trans) {
2200 trans->param_flags =
2201 (trans->param_flags & ~SPP_SACKDELAY) |
2202 sackdelay_change;
2203 } else if (asoc) {
2204 asoc->param_flags =
2205 (asoc->param_flags & ~SPP_SACKDELAY) |
2206 sackdelay_change;
2207 } else {
2208 sp->param_flags =
2209 (sp->param_flags & ~SPP_SACKDELAY) |
2210 sackdelay_change;
2211 }
2212 }
2213
2214 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2215 * of this field is ignored. Note also that a value of zero
2216 * indicates the current setting should be left unchanged.
2217 */
2218 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
2219 if (trans) {
2220 trans->pathmaxrxt = params->spp_pathmaxrxt;
2221 } else if (asoc) {
2222 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2223 } else {
2224 sp->pathmaxrxt = params->spp_pathmaxrxt;
2225 }
2226 }
2227
2228 return 0;
2229 }
2230
2231 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2232 char __user *optval, int optlen)
2233 {
2234 struct sctp_paddrparams params;
2235 struct sctp_transport *trans = NULL;
2236 struct sctp_association *asoc = NULL;
2237 struct sctp_sock *sp = sctp_sk(sk);
2238 int error;
2239 int hb_change, pmtud_change, sackdelay_change;
2240
2241 if (optlen != sizeof(struct sctp_paddrparams))
2242 return - EINVAL;
2243
2244 if (copy_from_user(&params, optval, optlen))
2245 return -EFAULT;
2246
2247 /* Validate flags and value parameters. */
2248 hb_change = params.spp_flags & SPP_HB;
2249 pmtud_change = params.spp_flags & SPP_PMTUD;
2250 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2251
2252 if (hb_change == SPP_HB ||
2253 pmtud_change == SPP_PMTUD ||
2254 sackdelay_change == SPP_SACKDELAY ||
2255 params.spp_sackdelay > 500 ||
2256 (params.spp_pathmtu
2257 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2258 return -EINVAL;
2259
2260 /* If an address other than INADDR_ANY is specified, and
2261 * no transport is found, then the request is invalid.
2262 */
2263 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2264 trans = sctp_addr_id2transport(sk, &params.spp_address,
2265 params.spp_assoc_id);
2266 if (!trans)
2267 return -EINVAL;
2268 }
2269
2270 /* Get association, if assoc_id != 0 and the socket is a one
2271 * to many style socket, and an association was not found, then
2272 * the id was invalid.
2273 */
2274 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2275 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2276 return -EINVAL;
2277
2278 /* Heartbeat demand can only be sent on a transport or
2279 * association, but not a socket.
2280 */
2281 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2282 return -EINVAL;
2283
2284 /* Process parameters. */
2285 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2286 hb_change, pmtud_change,
2287 sackdelay_change);
2288
2289 if (error)
2290 return error;
2291
2292 /* If changes are for association, also apply parameters to each
2293 * transport.
2294 */
2295 if (!trans && asoc) {
2296 struct list_head *pos;
2297
2298 list_for_each(pos, &asoc->peer.transport_addr_list) {
2299 trans = list_entry(pos, struct sctp_transport,
2300 transports);
2301 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2302 hb_change, pmtud_change,
2303 sackdelay_change);
2304 }
2305 }
2306
2307 return 0;
2308 }
2309
2310 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2311 *
2312 * This options will get or set the delayed ack timer. The time is set
2313 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2314 * endpoints default delayed ack timer value. If the assoc_id field is
2315 * non-zero, then the set or get effects the specified association.
2316 *
2317 * struct sctp_assoc_value {
2318 * sctp_assoc_t assoc_id;
2319 * uint32_t assoc_value;
2320 * };
2321 *
2322 * assoc_id - This parameter, indicates which association the
2323 * user is preforming an action upon. Note that if
2324 * this field's value is zero then the endpoints
2325 * default value is changed (effecting future
2326 * associations only).
2327 *
2328 * assoc_value - This parameter contains the number of milliseconds
2329 * that the user is requesting the delayed ACK timer
2330 * be set to. Note that this value is defined in
2331 * the standard to be between 200 and 500 milliseconds.
2332 *
2333 * Note: a value of zero will leave the value alone,
2334 * but disable SACK delay. A non-zero value will also
2335 * enable SACK delay.
2336 */
2337
2338 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2339 char __user *optval, int optlen)
2340 {
2341 struct sctp_assoc_value params;
2342 struct sctp_transport *trans = NULL;
2343 struct sctp_association *asoc = NULL;
2344 struct sctp_sock *sp = sctp_sk(sk);
2345
2346 if (optlen != sizeof(struct sctp_assoc_value))
2347 return - EINVAL;
2348
2349 if (copy_from_user(&params, optval, optlen))
2350 return -EFAULT;
2351
2352 /* Validate value parameter. */
2353 if (params.assoc_value > 500)
2354 return -EINVAL;
2355
2356 /* Get association, if assoc_id != 0 and the socket is a one
2357 * to many style socket, and an association was not found, then
2358 * the id was invalid.
2359 */
2360 asoc = sctp_id2assoc(sk, params.assoc_id);
2361 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2362 return -EINVAL;
2363
2364 if (params.assoc_value) {
2365 if (asoc) {
2366 asoc->sackdelay =
2367 msecs_to_jiffies(params.assoc_value);
2368 asoc->param_flags =
2369 (asoc->param_flags & ~SPP_SACKDELAY) |
2370 SPP_SACKDELAY_ENABLE;
2371 } else {
2372 sp->sackdelay = params.assoc_value;
2373 sp->param_flags =
2374 (sp->param_flags & ~SPP_SACKDELAY) |
2375 SPP_SACKDELAY_ENABLE;
2376 }
2377 } else {
2378 if (asoc) {
2379 asoc->param_flags =
2380 (asoc->param_flags & ~SPP_SACKDELAY) |
2381 SPP_SACKDELAY_DISABLE;
2382 } else {
2383 sp->param_flags =
2384 (sp->param_flags & ~SPP_SACKDELAY) |
2385 SPP_SACKDELAY_DISABLE;
2386 }
2387 }
2388
2389 /* If change is for association, also apply to each transport. */
2390 if (asoc) {
2391 struct list_head *pos;
2392
2393 list_for_each(pos, &asoc->peer.transport_addr_list) {
2394 trans = list_entry(pos, struct sctp_transport,
2395 transports);
2396 if (params.assoc_value) {
2397 trans->sackdelay =
2398 msecs_to_jiffies(params.assoc_value);
2399 trans->param_flags =
2400 (trans->param_flags & ~SPP_SACKDELAY) |
2401 SPP_SACKDELAY_ENABLE;
2402 } else {
2403 trans->param_flags =
2404 (trans->param_flags & ~SPP_SACKDELAY) |
2405 SPP_SACKDELAY_DISABLE;
2406 }
2407 }
2408 }
2409
2410 return 0;
2411 }
2412
2413 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2414 *
2415 * Applications can specify protocol parameters for the default association
2416 * initialization. The option name argument to setsockopt() and getsockopt()
2417 * is SCTP_INITMSG.
2418 *
2419 * Setting initialization parameters is effective only on an unconnected
2420 * socket (for UDP-style sockets only future associations are effected
2421 * by the change). With TCP-style sockets, this option is inherited by
2422 * sockets derived from a listener socket.
2423 */
2424 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2425 {
2426 struct sctp_initmsg sinit;
2427 struct sctp_sock *sp = sctp_sk(sk);
2428
2429 if (optlen != sizeof(struct sctp_initmsg))
2430 return -EINVAL;
2431 if (copy_from_user(&sinit, optval, optlen))
2432 return -EFAULT;
2433
2434 if (sinit.sinit_num_ostreams)
2435 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2436 if (sinit.sinit_max_instreams)
2437 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2438 if (sinit.sinit_max_attempts)
2439 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2440 if (sinit.sinit_max_init_timeo)
2441 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2442
2443 return 0;
2444 }
2445
2446 /*
2447 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2448 *
2449 * Applications that wish to use the sendto() system call may wish to
2450 * specify a default set of parameters that would normally be supplied
2451 * through the inclusion of ancillary data. This socket option allows
2452 * such an application to set the default sctp_sndrcvinfo structure.
2453 * The application that wishes to use this socket option simply passes
2454 * in to this call the sctp_sndrcvinfo structure defined in Section
2455 * 5.2.2) The input parameters accepted by this call include
2456 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2457 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2458 * to this call if the caller is using the UDP model.
2459 */
2460 static int sctp_setsockopt_default_send_param(struct sock *sk,
2461 char __user *optval, int optlen)
2462 {
2463 struct sctp_sndrcvinfo info;
2464 struct sctp_association *asoc;
2465 struct sctp_sock *sp = sctp_sk(sk);
2466
2467 if (optlen != sizeof(struct sctp_sndrcvinfo))
2468 return -EINVAL;
2469 if (copy_from_user(&info, optval, optlen))
2470 return -EFAULT;
2471
2472 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2473 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2474 return -EINVAL;
2475
2476 if (asoc) {
2477 asoc->default_stream = info.sinfo_stream;
2478 asoc->default_flags = info.sinfo_flags;
2479 asoc->default_ppid = info.sinfo_ppid;
2480 asoc->default_context = info.sinfo_context;
2481 asoc->default_timetolive = info.sinfo_timetolive;
2482 } else {
2483 sp->default_stream = info.sinfo_stream;
2484 sp->default_flags = info.sinfo_flags;
2485 sp->default_ppid = info.sinfo_ppid;
2486 sp->default_context = info.sinfo_context;
2487 sp->default_timetolive = info.sinfo_timetolive;
2488 }
2489
2490 return 0;
2491 }
2492
2493 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2494 *
2495 * Requests that the local SCTP stack use the enclosed peer address as
2496 * the association primary. The enclosed address must be one of the
2497 * association peer's addresses.
2498 */
2499 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2500 int optlen)
2501 {
2502 struct sctp_prim prim;
2503 struct sctp_transport *trans;
2504
2505 if (optlen != sizeof(struct sctp_prim))
2506 return -EINVAL;
2507
2508 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2509 return -EFAULT;
2510
2511 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2512 if (!trans)
2513 return -EINVAL;
2514
2515 sctp_assoc_set_primary(trans->asoc, trans);
2516
2517 return 0;
2518 }
2519
2520 /*
2521 * 7.1.5 SCTP_NODELAY
2522 *
2523 * Turn on/off any Nagle-like algorithm. This means that packets are
2524 * generally sent as soon as possible and no unnecessary delays are
2525 * introduced, at the cost of more packets in the network. Expects an
2526 * integer boolean flag.
2527 */
2528 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2529 int optlen)
2530 {
2531 int val;
2532
2533 if (optlen < sizeof(int))
2534 return -EINVAL;
2535 if (get_user(val, (int __user *)optval))
2536 return -EFAULT;
2537
2538 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2539 return 0;
2540 }
2541
2542 /*
2543 *
2544 * 7.1.1 SCTP_RTOINFO
2545 *
2546 * The protocol parameters used to initialize and bound retransmission
2547 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2548 * and modify these parameters.
2549 * All parameters are time values, in milliseconds. A value of 0, when
2550 * modifying the parameters, indicates that the current value should not
2551 * be changed.
2552 *
2553 */
2554 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2555 struct sctp_rtoinfo rtoinfo;
2556 struct sctp_association *asoc;
2557
2558 if (optlen != sizeof (struct sctp_rtoinfo))
2559 return -EINVAL;
2560
2561 if (copy_from_user(&rtoinfo, optval, optlen))
2562 return -EFAULT;
2563
2564 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2565
2566 /* Set the values to the specific association */
2567 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2568 return -EINVAL;
2569
2570 if (asoc) {
2571 if (rtoinfo.srto_initial != 0)
2572 asoc->rto_initial =
2573 msecs_to_jiffies(rtoinfo.srto_initial);
2574 if (rtoinfo.srto_max != 0)
2575 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2576 if (rtoinfo.srto_min != 0)
2577 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2578 } else {
2579 /* If there is no association or the association-id = 0
2580 * set the values to the endpoint.
2581 */
2582 struct sctp_sock *sp = sctp_sk(sk);
2583
2584 if (rtoinfo.srto_initial != 0)
2585 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2586 if (rtoinfo.srto_max != 0)
2587 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2588 if (rtoinfo.srto_min != 0)
2589 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2590 }
2591
2592 return 0;
2593 }
2594
2595 /*
2596 *
2597 * 7.1.2 SCTP_ASSOCINFO
2598 *
2599 * This option is used to tune the maximum retransmission attempts
2600 * of the association.
2601 * Returns an error if the new association retransmission value is
2602 * greater than the sum of the retransmission value of the peer.
2603 * See [SCTP] for more information.
2604 *
2605 */
2606 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2607 {
2608
2609 struct sctp_assocparams assocparams;
2610 struct sctp_association *asoc;
2611
2612 if (optlen != sizeof(struct sctp_assocparams))
2613 return -EINVAL;
2614 if (copy_from_user(&assocparams, optval, optlen))
2615 return -EFAULT;
2616
2617 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2618
2619 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2620 return -EINVAL;
2621
2622 /* Set the values to the specific association */
2623 if (asoc) {
2624 if (assocparams.sasoc_asocmaxrxt != 0) {
2625 __u32 path_sum = 0;
2626 int paths = 0;
2627 struct list_head *pos;
2628 struct sctp_transport *peer_addr;
2629
2630 list_for_each(pos, &asoc->peer.transport_addr_list) {
2631 peer_addr = list_entry(pos,
2632 struct sctp_transport,
2633 transports);
2634 path_sum += peer_addr->pathmaxrxt;
2635 paths++;
2636 }
2637
2638 /* Only validate asocmaxrxt if we have more then
2639 * one path/transport. We do this because path
2640 * retransmissions are only counted when we have more
2641 * then one path.
2642 */
2643 if (paths > 1 &&
2644 assocparams.sasoc_asocmaxrxt > path_sum)
2645 return -EINVAL;
2646
2647 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2648 }
2649
2650 if (assocparams.sasoc_cookie_life != 0) {
2651 asoc->cookie_life.tv_sec =
2652 assocparams.sasoc_cookie_life / 1000;
2653 asoc->cookie_life.tv_usec =
2654 (assocparams.sasoc_cookie_life % 1000)
2655 * 1000;
2656 }
2657 } else {
2658 /* Set the values to the endpoint */
2659 struct sctp_sock *sp = sctp_sk(sk);
2660
2661 if (assocparams.sasoc_asocmaxrxt != 0)
2662 sp->assocparams.sasoc_asocmaxrxt =
2663 assocparams.sasoc_asocmaxrxt;
2664 if (assocparams.sasoc_cookie_life != 0)
2665 sp->assocparams.sasoc_cookie_life =
2666 assocparams.sasoc_cookie_life;
2667 }
2668 return 0;
2669 }
2670
2671 /*
2672 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2673 *
2674 * This socket option is a boolean flag which turns on or off mapped V4
2675 * addresses. If this option is turned on and the socket is type
2676 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2677 * If this option is turned off, then no mapping will be done of V4
2678 * addresses and a user will receive both PF_INET6 and PF_INET type
2679 * addresses on the socket.
2680 */
2681 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2682 {
2683 int val;
2684 struct sctp_sock *sp = sctp_sk(sk);
2685
2686 if (optlen < sizeof(int))
2687 return -EINVAL;
2688 if (get_user(val, (int __user *)optval))
2689 return -EFAULT;
2690 if (val)
2691 sp->v4mapped = 1;
2692 else
2693 sp->v4mapped = 0;
2694
2695 return 0;
2696 }
2697
2698 /*
2699 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2700 *
2701 * This socket option specifies the maximum size to put in any outgoing
2702 * SCTP chunk. If a message is larger than this size it will be
2703 * fragmented by SCTP into the specified size. Note that the underlying
2704 * SCTP implementation may fragment into smaller sized chunks when the
2705 * PMTU of the underlying association is smaller than the value set by
2706 * the user.
2707 */
2708 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2709 {
2710 struct sctp_association *asoc;
2711 struct list_head *pos;
2712 struct sctp_sock *sp = sctp_sk(sk);
2713 int val;
2714
2715 if (optlen < sizeof(int))
2716 return -EINVAL;
2717 if (get_user(val, (int __user *)optval))
2718 return -EFAULT;
2719 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2720 return -EINVAL;
2721 sp->user_frag = val;
2722
2723 /* Update the frag_point of the existing associations. */
2724 list_for_each(pos, &(sp->ep->asocs)) {
2725 asoc = list_entry(pos, struct sctp_association, asocs);
2726 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
2727 }
2728
2729 return 0;
2730 }
2731
2732
2733 /*
2734 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2735 *
2736 * Requests that the peer mark the enclosed address as the association
2737 * primary. The enclosed address must be one of the association's
2738 * locally bound addresses. The following structure is used to make a
2739 * set primary request:
2740 */
2741 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2742 int optlen)
2743 {
2744 struct sctp_sock *sp;
2745 struct sctp_endpoint *ep;
2746 struct sctp_association *asoc = NULL;
2747 struct sctp_setpeerprim prim;
2748 struct sctp_chunk *chunk;
2749 int err;
2750
2751 sp = sctp_sk(sk);
2752 ep = sp->ep;
2753
2754 if (!sctp_addip_enable)
2755 return -EPERM;
2756
2757 if (optlen != sizeof(struct sctp_setpeerprim))
2758 return -EINVAL;
2759
2760 if (copy_from_user(&prim, optval, optlen))
2761 return -EFAULT;
2762
2763 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2764 if (!asoc)
2765 return -EINVAL;
2766
2767 if (!asoc->peer.asconf_capable)
2768 return -EPERM;
2769
2770 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2771 return -EPERM;
2772
2773 if (!sctp_state(asoc, ESTABLISHED))
2774 return -ENOTCONN;
2775
2776 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2777 return -EADDRNOTAVAIL;
2778
2779 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2780 chunk = sctp_make_asconf_set_prim(asoc,
2781 (union sctp_addr *)&prim.sspp_addr);
2782 if (!chunk)
2783 return -ENOMEM;
2784
2785 err = sctp_send_asconf(asoc, chunk);
2786
2787 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2788
2789 return err;
2790 }
2791
2792 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
2793 int optlen)
2794 {
2795 struct sctp_setadaptation adaptation;
2796
2797 if (optlen != sizeof(struct sctp_setadaptation))
2798 return -EINVAL;
2799 if (copy_from_user(&adaptation, optval, optlen))
2800 return -EFAULT;
2801
2802 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
2803
2804 return 0;
2805 }
2806
2807 /*
2808 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2809 *
2810 * The context field in the sctp_sndrcvinfo structure is normally only
2811 * used when a failed message is retrieved holding the value that was
2812 * sent down on the actual send call. This option allows the setting of
2813 * a default context on an association basis that will be received on
2814 * reading messages from the peer. This is especially helpful in the
2815 * one-2-many model for an application to keep some reference to an
2816 * internal state machine that is processing messages on the
2817 * association. Note that the setting of this value only effects
2818 * received messages from the peer and does not effect the value that is
2819 * saved with outbound messages.
2820 */
2821 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2822 int optlen)
2823 {
2824 struct sctp_assoc_value params;
2825 struct sctp_sock *sp;
2826 struct sctp_association *asoc;
2827
2828 if (optlen != sizeof(struct sctp_assoc_value))
2829 return -EINVAL;
2830 if (copy_from_user(&params, optval, optlen))
2831 return -EFAULT;
2832
2833 sp = sctp_sk(sk);
2834
2835 if (params.assoc_id != 0) {
2836 asoc = sctp_id2assoc(sk, params.assoc_id);
2837 if (!asoc)
2838 return -EINVAL;
2839 asoc->default_rcv_context = params.assoc_value;
2840 } else {
2841 sp->default_rcv_context = params.assoc_value;
2842 }
2843
2844 return 0;
2845 }
2846
2847 /*
2848 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2849 *
2850 * This options will at a minimum specify if the implementation is doing
2851 * fragmented interleave. Fragmented interleave, for a one to many
2852 * socket, is when subsequent calls to receive a message may return
2853 * parts of messages from different associations. Some implementations
2854 * may allow you to turn this value on or off. If so, when turned off,
2855 * no fragment interleave will occur (which will cause a head of line
2856 * blocking amongst multiple associations sharing the same one to many
2857 * socket). When this option is turned on, then each receive call may
2858 * come from a different association (thus the user must receive data
2859 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2860 * association each receive belongs to.
2861 *
2862 * This option takes a boolean value. A non-zero value indicates that
2863 * fragmented interleave is on. A value of zero indicates that
2864 * fragmented interleave is off.
2865 *
2866 * Note that it is important that an implementation that allows this
2867 * option to be turned on, have it off by default. Otherwise an unaware
2868 * application using the one to many model may become confused and act
2869 * incorrectly.
2870 */
2871 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2872 char __user *optval,
2873 int optlen)
2874 {
2875 int val;
2876
2877 if (optlen != sizeof(int))
2878 return -EINVAL;
2879 if (get_user(val, (int __user *)optval))
2880 return -EFAULT;
2881
2882 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2883
2884 return 0;
2885 }
2886
2887 /*
2888 * 7.1.25. Set or Get the sctp partial delivery point
2889 * (SCTP_PARTIAL_DELIVERY_POINT)
2890 * This option will set or get the SCTP partial delivery point. This
2891 * point is the size of a message where the partial delivery API will be
2892 * invoked to help free up rwnd space for the peer. Setting this to a
2893 * lower value will cause partial delivery's to happen more often. The
2894 * calls argument is an integer that sets or gets the partial delivery
2895 * point.
2896 */
2897 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2898 char __user *optval,
2899 int optlen)
2900 {
2901 u32 val;
2902
2903 if (optlen != sizeof(u32))
2904 return -EINVAL;
2905 if (get_user(val, (int __user *)optval))
2906 return -EFAULT;
2907
2908 sctp_sk(sk)->pd_point = val;
2909
2910 return 0; /* is this the right error code? */
2911 }
2912
2913 /*
2914 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2915 *
2916 * This option will allow a user to change the maximum burst of packets
2917 * that can be emitted by this association. Note that the default value
2918 * is 4, and some implementations may restrict this setting so that it
2919 * can only be lowered.
2920 *
2921 * NOTE: This text doesn't seem right. Do this on a socket basis with
2922 * future associations inheriting the socket value.
2923 */
2924 static int sctp_setsockopt_maxburst(struct sock *sk,
2925 char __user *optval,
2926 int optlen)
2927 {
2928 int val;
2929
2930 if (optlen != sizeof(int))
2931 return -EINVAL;
2932 if (get_user(val, (int __user *)optval))
2933 return -EFAULT;
2934
2935 if (val < 0)
2936 return -EINVAL;
2937
2938 sctp_sk(sk)->max_burst = val;
2939
2940 return 0;
2941 }
2942
2943 /* API 6.2 setsockopt(), getsockopt()
2944 *
2945 * Applications use setsockopt() and getsockopt() to set or retrieve
2946 * socket options. Socket options are used to change the default
2947 * behavior of sockets calls. They are described in Section 7.
2948 *
2949 * The syntax is:
2950 *
2951 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2952 * int __user *optlen);
2953 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2954 * int optlen);
2955 *
2956 * sd - the socket descript.
2957 * level - set to IPPROTO_SCTP for all SCTP options.
2958 * optname - the option name.
2959 * optval - the buffer to store the value of the option.
2960 * optlen - the size of the buffer.
2961 */
2962 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2963 char __user *optval, int optlen)
2964 {
2965 int retval = 0;
2966
2967 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2968 sk, optname);
2969
2970 /* I can hardly begin to describe how wrong this is. This is
2971 * so broken as to be worse than useless. The API draft
2972 * REALLY is NOT helpful here... I am not convinced that the
2973 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2974 * are at all well-founded.
2975 */
2976 if (level != SOL_SCTP) {
2977 struct sctp_af *af = sctp_sk(sk)->pf->af;
2978 retval = af->setsockopt(sk, level, optname, optval, optlen);
2979 goto out_nounlock;
2980 }
2981
2982 sctp_lock_sock(sk);
2983
2984 switch (optname) {
2985 case SCTP_SOCKOPT_BINDX_ADD:
2986 /* 'optlen' is the size of the addresses buffer. */
2987 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2988 optlen, SCTP_BINDX_ADD_ADDR);
2989 break;
2990
2991 case SCTP_SOCKOPT_BINDX_REM:
2992 /* 'optlen' is the size of the addresses buffer. */
2993 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2994 optlen, SCTP_BINDX_REM_ADDR);
2995 break;
2996
2997 case SCTP_SOCKOPT_CONNECTX:
2998 /* 'optlen' is the size of the addresses buffer. */
2999 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3000 optlen);
3001 break;
3002
3003 case SCTP_DISABLE_FRAGMENTS:
3004 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3005 break;
3006
3007 case SCTP_EVENTS:
3008 retval = sctp_setsockopt_events(sk, optval, optlen);
3009 break;
3010
3011 case SCTP_AUTOCLOSE:
3012 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3013 break;
3014
3015 case SCTP_PEER_ADDR_PARAMS:
3016 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3017 break;
3018
3019 case SCTP_DELAYED_ACK_TIME:
3020 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3021 break;
3022 case SCTP_PARTIAL_DELIVERY_POINT:
3023 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3024 break;
3025
3026 case SCTP_INITMSG:
3027 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3028 break;
3029 case SCTP_DEFAULT_SEND_PARAM:
3030 retval = sctp_setsockopt_default_send_param(sk, optval,
3031 optlen);
3032 break;
3033 case SCTP_PRIMARY_ADDR:
3034 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3035 break;
3036 case SCTP_SET_PEER_PRIMARY_ADDR:
3037 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3038 break;
3039 case SCTP_NODELAY:
3040 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3041 break;
3042 case SCTP_RTOINFO:
3043 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3044 break;
3045 case SCTP_ASSOCINFO:
3046 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3047 break;
3048 case SCTP_I_WANT_MAPPED_V4_ADDR:
3049 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3050 break;
3051 case SCTP_MAXSEG:
3052 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3053 break;
3054 case SCTP_ADAPTATION_LAYER:
3055 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3056 break;
3057 case SCTP_CONTEXT:
3058 retval = sctp_setsockopt_context(sk, optval, optlen);
3059 break;
3060 case SCTP_FRAGMENT_INTERLEAVE:
3061 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3062 break;
3063 case SCTP_MAX_BURST:
3064 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3065 break;
3066 default:
3067 retval = -ENOPROTOOPT;
3068 break;
3069 }
3070
3071 sctp_release_sock(sk);
3072
3073 out_nounlock:
3074 return retval;
3075 }
3076
3077 /* API 3.1.6 connect() - UDP Style Syntax
3078 *
3079 * An application may use the connect() call in the UDP model to initiate an
3080 * association without sending data.
3081 *
3082 * The syntax is:
3083 *
3084 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3085 *
3086 * sd: the socket descriptor to have a new association added to.
3087 *
3088 * nam: the address structure (either struct sockaddr_in or struct
3089 * sockaddr_in6 defined in RFC2553 [7]).
3090 *
3091 * len: the size of the address.
3092 */
3093 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
3094 int addr_len)
3095 {
3096 int err = 0;
3097 struct sctp_af *af;
3098
3099 sctp_lock_sock(sk);
3100
3101 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3102 __FUNCTION__, sk, addr, addr_len);
3103
3104 /* Validate addr_len before calling common connect/connectx routine. */
3105 af = sctp_get_af_specific(addr->sa_family);
3106 if (!af || addr_len < af->sockaddr_len) {
3107 err = -EINVAL;
3108 } else {
3109 /* Pass correct addr len to common routine (so it knows there
3110 * is only one address being passed.
3111 */
3112 err = __sctp_connect(sk, addr, af->sockaddr_len);
3113 }
3114
3115 sctp_release_sock(sk);
3116 return err;
3117 }
3118
3119 /* FIXME: Write comments. */
3120 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3121 {
3122 return -EOPNOTSUPP; /* STUB */
3123 }
3124
3125 /* 4.1.4 accept() - TCP Style Syntax
3126 *
3127 * Applications use accept() call to remove an established SCTP
3128 * association from the accept queue of the endpoint. A new socket
3129 * descriptor will be returned from accept() to represent the newly
3130 * formed association.
3131 */
3132 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3133 {
3134 struct sctp_sock *sp;
3135 struct sctp_endpoint *ep;
3136 struct sock *newsk = NULL;
3137 struct sctp_association *asoc;
3138 long timeo;
3139 int error = 0;
3140
3141 sctp_lock_sock(sk);
3142
3143 sp = sctp_sk(sk);
3144 ep = sp->ep;
3145
3146 if (!sctp_style(sk, TCP)) {
3147 error = -EOPNOTSUPP;
3148 goto out;
3149 }
3150
3151 if (!sctp_sstate(sk, LISTENING)) {
3152 error = -EINVAL;
3153 goto out;
3154 }
3155
3156 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
3157
3158 error = sctp_wait_for_accept(sk, timeo);
3159 if (error)
3160 goto out;
3161
3162 /* We treat the list of associations on the endpoint as the accept
3163 * queue and pick the first association on the list.
3164 */
3165 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3166
3167 newsk = sp->pf->create_accept_sk(sk, asoc);
3168 if (!newsk) {
3169 error = -ENOMEM;
3170 goto out;
3171 }
3172
3173 /* Populate the fields of the newsk from the oldsk and migrate the
3174 * asoc to the newsk.
3175 */
3176 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3177
3178 out:
3179 sctp_release_sock(sk);
3180 *err = error;
3181 return newsk;
3182 }
3183
3184 /* The SCTP ioctl handler. */
3185 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3186 {
3187 return -ENOIOCTLCMD;
3188 }
3189
3190 /* This is the function which gets called during socket creation to
3191 * initialized the SCTP-specific portion of the sock.
3192 * The sock structure should already be zero-filled memory.
3193 */
3194 SCTP_STATIC int sctp_init_sock(struct sock *sk)
3195 {
3196 struct sctp_endpoint *ep;
3197 struct sctp_sock *sp;
3198
3199 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3200
3201 sp = sctp_sk(sk);
3202
3203 /* Initialize the SCTP per socket area. */
3204 switch (sk->sk_type) {
3205 case SOCK_SEQPACKET:
3206 sp->type = SCTP_SOCKET_UDP;
3207 break;
3208 case SOCK_STREAM:
3209 sp->type = SCTP_SOCKET_TCP;
3210 break;
3211 default:
3212 return -ESOCKTNOSUPPORT;
3213 }
3214
3215 /* Initialize default send parameters. These parameters can be
3216 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3217 */
3218 sp->default_stream = 0;
3219 sp->default_ppid = 0;
3220 sp->default_flags = 0;
3221 sp->default_context = 0;
3222 sp->default_timetolive = 0;
3223
3224 sp->default_rcv_context = 0;
3225 sp->max_burst = sctp_max_burst;
3226
3227 /* Initialize default setup parameters. These parameters
3228 * can be modified with the SCTP_INITMSG socket option or
3229 * overridden by the SCTP_INIT CMSG.
3230 */
3231 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3232 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3233 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3234 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
3235
3236 /* Initialize default RTO related parameters. These parameters can
3237 * be modified for with the SCTP_RTOINFO socket option.
3238 */
3239 sp->rtoinfo.srto_initial = sctp_rto_initial;
3240 sp->rtoinfo.srto_max = sctp_rto_max;
3241 sp->rtoinfo.srto_min = sctp_rto_min;
3242
3243 /* Initialize default association related parameters. These parameters
3244 * can be modified with the SCTP_ASSOCINFO socket option.
3245 */
3246 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3247 sp->assocparams.sasoc_number_peer_destinations = 0;
3248 sp->assocparams.sasoc_peer_rwnd = 0;
3249 sp->assocparams.sasoc_local_rwnd = 0;
3250 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
3251
3252 /* Initialize default event subscriptions. By default, all the
3253 * options are off.
3254 */
3255 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3256
3257 /* Default Peer Address Parameters. These defaults can
3258 * be modified via SCTP_PEER_ADDR_PARAMS
3259 */
3260 sp->hbinterval = sctp_hb_interval;
3261 sp->pathmaxrxt = sctp_max_retrans_path;
3262 sp->pathmtu = 0; // allow default discovery
3263 sp->sackdelay = sctp_sack_timeout;
3264 sp->param_flags = SPP_HB_ENABLE |
3265 SPP_PMTUD_ENABLE |
3266 SPP_SACKDELAY_ENABLE;
3267
3268 /* If enabled no SCTP message fragmentation will be performed.
3269 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3270 */
3271 sp->disable_fragments = 0;
3272
3273 /* Enable Nagle algorithm by default. */
3274 sp->nodelay = 0;
3275
3276 /* Enable by default. */
3277 sp->v4mapped = 1;
3278
3279 /* Auto-close idle associations after the configured
3280 * number of seconds. A value of 0 disables this
3281 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3282 * for UDP-style sockets only.
3283 */
3284 sp->autoclose = 0;
3285
3286 /* User specified fragmentation limit. */
3287 sp->user_frag = 0;
3288
3289 sp->adaptation_ind = 0;
3290
3291 sp->pf = sctp_get_pf_specific(sk->sk_family);
3292
3293 /* Control variables for partial data delivery. */
3294 atomic_set(&sp->pd_mode, 0);
3295 skb_queue_head_init(&sp->pd_lobby);
3296 sp->frag_interleave = 0;
3297
3298 /* Create a per socket endpoint structure. Even if we
3299 * change the data structure relationships, this may still
3300 * be useful for storing pre-connect address information.
3301 */
3302 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3303 if (!ep)
3304 return -ENOMEM;
3305
3306 sp->ep = ep;
3307 sp->hmac = NULL;
3308
3309 SCTP_DBG_OBJCNT_INC(sock);
3310 return 0;
3311 }
3312
3313 /* Cleanup any SCTP per socket resources. */
3314 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3315 {
3316 struct sctp_endpoint *ep;
3317
3318 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3319
3320 /* Release our hold on the endpoint. */
3321 ep = sctp_sk(sk)->ep;
3322 sctp_endpoint_free(ep);
3323
3324 return 0;
3325 }
3326
3327 /* API 4.1.7 shutdown() - TCP Style Syntax
3328 * int shutdown(int socket, int how);
3329 *
3330 * sd - the socket descriptor of the association to be closed.
3331 * how - Specifies the type of shutdown. The values are
3332 * as follows:
3333 * SHUT_RD
3334 * Disables further receive operations. No SCTP
3335 * protocol action is taken.
3336 * SHUT_WR
3337 * Disables further send operations, and initiates
3338 * the SCTP shutdown sequence.
3339 * SHUT_RDWR
3340 * Disables further send and receive operations
3341 * and initiates the SCTP shutdown sequence.
3342 */
3343 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3344 {
3345 struct sctp_endpoint *ep;
3346 struct sctp_association *asoc;
3347
3348 if (!sctp_style(sk, TCP))
3349 return;
3350
3351 if (how & SEND_SHUTDOWN) {
3352 ep = sctp_sk(sk)->ep;
3353 if (!list_empty(&ep->asocs)) {
3354 asoc = list_entry(ep->asocs.next,
3355 struct sctp_association, asocs);
3356 sctp_primitive_SHUTDOWN(asoc, NULL);
3357 }
3358 }
3359 }
3360
3361 /* 7.2.1 Association Status (SCTP_STATUS)
3362
3363 * Applications can retrieve current status information about an
3364 * association, including association state, peer receiver window size,
3365 * number of unacked data chunks, and number of data chunks pending
3366 * receipt. This information is read-only.
3367 */
3368 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3369 char __user *optval,
3370 int __user *optlen)
3371 {
3372 struct sctp_status status;
3373 struct sctp_association *asoc = NULL;
3374 struct sctp_transport *transport;
3375 sctp_assoc_t associd;
3376 int retval = 0;
3377
3378 if (len < sizeof(status)) {
3379 retval = -EINVAL;
3380 goto out;
3381 }
3382
3383 len = sizeof(status);
3384 if (copy_from_user(&status, optval, len)) {
3385 retval = -EFAULT;
3386 goto out;
3387 }
3388
3389 associd = status.sstat_assoc_id;
3390 asoc = sctp_id2assoc(sk, associd);
3391 if (!asoc) {
3392 retval = -EINVAL;
3393 goto out;
3394 }
3395
3396 transport = asoc->peer.primary_path;
3397
3398 status.sstat_assoc_id = sctp_assoc2id(asoc);
3399 status.sstat_state = asoc->state;
3400 status.sstat_rwnd = asoc->peer.rwnd;
3401 status.sstat_unackdata = asoc->unack_data;
3402
3403 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3404 status.sstat_instrms = asoc->c.sinit_max_instreams;
3405 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3406 status.sstat_fragmentation_point = asoc->frag_point;
3407 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3408 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3409 transport->af_specific->sockaddr_len);
3410 /* Map ipv4 address into v4-mapped-on-v6 address. */
3411 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3412 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3413 status.sstat_primary.spinfo_state = transport->state;
3414 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3415 status.sstat_primary.spinfo_srtt = transport->srtt;
3416 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3417 status.sstat_primary.spinfo_mtu = transport->pathmtu;
3418
3419 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3420 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3421
3422 if (put_user(len, optlen)) {
3423 retval = -EFAULT;
3424 goto out;
3425 }
3426
3427 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3428 len, status.sstat_state, status.sstat_rwnd,
3429 status.sstat_assoc_id);
3430
3431 if (copy_to_user(optval, &status, len)) {
3432 retval = -EFAULT;
3433 goto out;
3434 }
3435
3436 out:
3437 return (retval);
3438 }
3439
3440
3441 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3442 *
3443 * Applications can retrieve information about a specific peer address
3444 * of an association, including its reachability state, congestion
3445 * window, and retransmission timer values. This information is
3446 * read-only.
3447 */
3448 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3449 char __user *optval,
3450 int __user *optlen)
3451 {
3452 struct sctp_paddrinfo pinfo;
3453 struct sctp_transport *transport;
3454 int retval = 0;
3455
3456 if (len < sizeof(pinfo)) {
3457 retval = -EINVAL;
3458 goto out;
3459 }
3460
3461 len = sizeof(pinfo);
3462 if (copy_from_user(&pinfo, optval, len)) {
3463 retval = -EFAULT;
3464 goto out;
3465 }
3466
3467 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3468 pinfo.spinfo_assoc_id);
3469 if (!transport)
3470 return -EINVAL;
3471
3472 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3473 pinfo.spinfo_state = transport->state;
3474 pinfo.spinfo_cwnd = transport->cwnd;
3475 pinfo.spinfo_srtt = transport->srtt;
3476 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3477 pinfo.spinfo_mtu = transport->pathmtu;
3478
3479 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3480 pinfo.spinfo_state = SCTP_ACTIVE;
3481
3482 if (put_user(len, optlen)) {
3483 retval = -EFAULT;
3484 goto out;
3485 }
3486
3487 if (copy_to_user(optval, &pinfo, len)) {
3488 retval = -EFAULT;
3489 goto out;
3490 }
3491
3492 out:
3493 return (retval);
3494 }
3495
3496 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3497 *
3498 * This option is a on/off flag. If enabled no SCTP message
3499 * fragmentation will be performed. Instead if a message being sent
3500 * exceeds the current PMTU size, the message will NOT be sent and
3501 * instead a error will be indicated to the user.
3502 */
3503 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3504 char __user *optval, int __user *optlen)
3505 {
3506 int val;
3507
3508 if (len < sizeof(int))
3509 return -EINVAL;
3510
3511 len = sizeof(int);
3512 val = (sctp_sk(sk)->disable_fragments == 1);
3513 if (put_user(len, optlen))
3514 return -EFAULT;
3515 if (copy_to_user(optval, &val, len))
3516 return -EFAULT;
3517 return 0;
3518 }
3519
3520 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3521 *
3522 * This socket option is used to specify various notifications and
3523 * ancillary data the user wishes to receive.
3524 */
3525 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3526 int __user *optlen)
3527 {
3528 if (len < sizeof(struct sctp_event_subscribe))
3529 return -EINVAL;
3530 len = sizeof(struct sctp_event_subscribe);
3531 if (put_user(len, optlen))
3532 return -EFAULT;
3533 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3534 return -EFAULT;
3535 return 0;
3536 }
3537
3538 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3539 *
3540 * This socket option is applicable to the UDP-style socket only. When
3541 * set it will cause associations that are idle for more than the
3542 * specified number of seconds to automatically close. An association
3543 * being idle is defined an association that has NOT sent or received
3544 * user data. The special value of '0' indicates that no automatic
3545 * close of any associations should be performed. The option expects an
3546 * integer defining the number of seconds of idle time before an
3547 * association is closed.
3548 */
3549 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3550 {
3551 /* Applicable to UDP-style socket only */
3552 if (sctp_style(sk, TCP))
3553 return -EOPNOTSUPP;
3554 if (len < sizeof(int))
3555 return -EINVAL;
3556 len = sizeof(int);
3557 if (put_user(len, optlen))
3558 return -EFAULT;
3559 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
3560 return -EFAULT;
3561 return 0;
3562 }
3563
3564 /* Helper routine to branch off an association to a new socket. */
3565 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3566 struct socket **sockp)
3567 {
3568 struct sock *sk = asoc->base.sk;
3569 struct socket *sock;
3570 struct inet_sock *inetsk;
3571 struct sctp_af *af;
3572 int err = 0;
3573
3574 /* An association cannot be branched off from an already peeled-off
3575 * socket, nor is this supported for tcp style sockets.
3576 */
3577 if (!sctp_style(sk, UDP))
3578 return -EINVAL;
3579
3580 /* Create a new socket. */
3581 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3582 if (err < 0)
3583 return err;
3584
3585 /* Populate the fields of the newsk from the oldsk and migrate the
3586 * asoc to the newsk.
3587 */
3588 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3589
3590 /* Make peeled-off sockets more like 1-1 accepted sockets.
3591 * Set the daddr and initialize id to something more random
3592 */
3593 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3594 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
3595 inetsk = inet_sk(sock->sk);
3596 inetsk->id = asoc->next_tsn ^ jiffies;
3597
3598 *sockp = sock;
3599
3600 return err;
3601 }
3602
3603 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3604 {
3605 sctp_peeloff_arg_t peeloff;
3606 struct socket *newsock;
3607 int retval = 0;
3608 struct sctp_association *asoc;
3609
3610 if (len < sizeof(sctp_peeloff_arg_t))
3611 return -EINVAL;
3612 len = sizeof(sctp_peeloff_arg_t);
3613 if (copy_from_user(&peeloff, optval, len))
3614 return -EFAULT;
3615
3616 asoc = sctp_id2assoc(sk, peeloff.associd);
3617 if (!asoc) {
3618 retval = -EINVAL;
3619 goto out;
3620 }
3621
3622 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3623
3624 retval = sctp_do_peeloff(asoc, &newsock);
3625 if (retval < 0)
3626 goto out;
3627
3628 /* Map the socket to an unused fd that can be returned to the user. */
3629 retval = sock_map_fd(newsock);
3630 if (retval < 0) {
3631 sock_release(newsock);
3632 goto out;
3633 }
3634
3635 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3636 __FUNCTION__, sk, asoc, newsock->sk, retval);
3637
3638 /* Return the fd mapped to the new socket. */
3639 peeloff.sd = retval;
3640 if (put_user(len, optlen))
3641 return -EFAULT;
3642 if (copy_to_user(optval, &peeloff, len))
3643 retval = -EFAULT;
3644
3645 out:
3646 return retval;
3647 }
3648
3649 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3650 *
3651 * Applications can enable or disable heartbeats for any peer address of
3652 * an association, modify an address's heartbeat interval, force a
3653 * heartbeat to be sent immediately, and adjust the address's maximum
3654 * number of retransmissions sent before an address is considered
3655 * unreachable. The following structure is used to access and modify an
3656 * address's parameters:
3657 *
3658 * struct sctp_paddrparams {
3659 * sctp_assoc_t spp_assoc_id;
3660 * struct sockaddr_storage spp_address;
3661 * uint32_t spp_hbinterval;
3662 * uint16_t spp_pathmaxrxt;
3663 * uint32_t spp_pathmtu;
3664 * uint32_t spp_sackdelay;
3665 * uint32_t spp_flags;
3666 * };
3667 *
3668 * spp_assoc_id - (one-to-many style socket) This is filled in the
3669 * application, and identifies the association for
3670 * this query.
3671 * spp_address - This specifies which address is of interest.
3672 * spp_hbinterval - This contains the value of the heartbeat interval,
3673 * in milliseconds. If a value of zero
3674 * is present in this field then no changes are to
3675 * be made to this parameter.
3676 * spp_pathmaxrxt - This contains the maximum number of
3677 * retransmissions before this address shall be
3678 * considered unreachable. If a value of zero
3679 * is present in this field then no changes are to
3680 * be made to this parameter.
3681 * spp_pathmtu - When Path MTU discovery is disabled the value
3682 * specified here will be the "fixed" path mtu.
3683 * Note that if the spp_address field is empty
3684 * then all associations on this address will
3685 * have this fixed path mtu set upon them.
3686 *
3687 * spp_sackdelay - When delayed sack is enabled, this value specifies
3688 * the number of milliseconds that sacks will be delayed
3689 * for. This value will apply to all addresses of an
3690 * association if the spp_address field is empty. Note
3691 * also, that if delayed sack is enabled and this
3692 * value is set to 0, no change is made to the last
3693 * recorded delayed sack timer value.
3694 *
3695 * spp_flags - These flags are used to control various features
3696 * on an association. The flag field may contain
3697 * zero or more of the following options.
3698 *
3699 * SPP_HB_ENABLE - Enable heartbeats on the
3700 * specified address. Note that if the address
3701 * field is empty all addresses for the association
3702 * have heartbeats enabled upon them.
3703 *
3704 * SPP_HB_DISABLE - Disable heartbeats on the
3705 * speicifed address. Note that if the address
3706 * field is empty all addresses for the association
3707 * will have their heartbeats disabled. Note also
3708 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3709 * mutually exclusive, only one of these two should
3710 * be specified. Enabling both fields will have
3711 * undetermined results.
3712 *
3713 * SPP_HB_DEMAND - Request a user initiated heartbeat
3714 * to be made immediately.
3715 *
3716 * SPP_PMTUD_ENABLE - This field will enable PMTU
3717 * discovery upon the specified address. Note that
3718 * if the address feild is empty then all addresses
3719 * on the association are effected.
3720 *
3721 * SPP_PMTUD_DISABLE - This field will disable PMTU
3722 * discovery upon the specified address. Note that
3723 * if the address feild is empty then all addresses
3724 * on the association are effected. Not also that
3725 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3726 * exclusive. Enabling both will have undetermined
3727 * results.
3728 *
3729 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3730 * on delayed sack. The time specified in spp_sackdelay
3731 * is used to specify the sack delay for this address. Note
3732 * that if spp_address is empty then all addresses will
3733 * enable delayed sack and take on the sack delay
3734 * value specified in spp_sackdelay.
3735 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3736 * off delayed sack. If the spp_address field is blank then
3737 * delayed sack is disabled for the entire association. Note
3738 * also that this field is mutually exclusive to
3739 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3740 * results.
3741 */
3742 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3743 char __user *optval, int __user *optlen)
3744 {
3745 struct sctp_paddrparams params;
3746 struct sctp_transport *trans = NULL;
3747 struct sctp_association *asoc = NULL;
3748 struct sctp_sock *sp = sctp_sk(sk);
3749
3750 if (len < sizeof(struct sctp_paddrparams))
3751 return -EINVAL;
3752 len = sizeof(struct sctp_paddrparams);
3753 if (copy_from_user(&params, optval, len))
3754 return -EFAULT;
3755
3756 /* If an address other than INADDR_ANY is specified, and
3757 * no transport is found, then the request is invalid.
3758 */
3759 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3760 trans = sctp_addr_id2transport(sk, &params.spp_address,
3761 params.spp_assoc_id);
3762 if (!trans) {
3763 SCTP_DEBUG_PRINTK("Failed no transport\n");
3764 return -EINVAL;
3765 }
3766 }
3767
3768 /* Get association, if assoc_id != 0 and the socket is a one
3769 * to many style socket, and an association was not found, then
3770 * the id was invalid.
3771 */
3772 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3773 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3774 SCTP_DEBUG_PRINTK("Failed no association\n");
3775 return -EINVAL;
3776 }
3777
3778 if (trans) {
3779 /* Fetch transport values. */
3780 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3781 params.spp_pathmtu = trans->pathmtu;
3782 params.spp_pathmaxrxt = trans->pathmaxrxt;
3783 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3784
3785 /*draft-11 doesn't say what to return in spp_flags*/
3786 params.spp_flags = trans->param_flags;
3787 } else if (asoc) {
3788 /* Fetch association values. */
3789 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3790 params.spp_pathmtu = asoc->pathmtu;
3791 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3792 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3793
3794 /*draft-11 doesn't say what to return in spp_flags*/
3795 params.spp_flags = asoc->param_flags;
3796 } else {
3797 /* Fetch socket values. */
3798 params.spp_hbinterval = sp->hbinterval;
3799 params.spp_pathmtu = sp->pathmtu;
3800 params.spp_sackdelay = sp->sackdelay;
3801 params.spp_pathmaxrxt = sp->pathmaxrxt;
3802
3803 /*draft-11 doesn't say what to return in spp_flags*/
3804 params.spp_flags = sp->param_flags;
3805 }
3806
3807 if (copy_to_user(optval, &params, len))
3808 return -EFAULT;
3809
3810 if (put_user(len, optlen))
3811 return -EFAULT;
3812
3813 return 0;
3814 }
3815
3816 /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3817 *
3818 * This options will get or set the delayed ack timer. The time is set
3819 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3820 * endpoints default delayed ack timer value. If the assoc_id field is
3821 * non-zero, then the set or get effects the specified association.
3822 *
3823 * struct sctp_assoc_value {
3824 * sctp_assoc_t assoc_id;
3825 * uint32_t assoc_value;
3826 * };
3827 *
3828 * assoc_id - This parameter, indicates which association the
3829 * user is preforming an action upon. Note that if
3830 * this field's value is zero then the endpoints
3831 * default value is changed (effecting future
3832 * associations only).
3833 *
3834 * assoc_value - This parameter contains the number of milliseconds
3835 * that the user is requesting the delayed ACK timer
3836 * be set to. Note that this value is defined in
3837 * the standard to be between 200 and 500 milliseconds.
3838 *
3839 * Note: a value of zero will leave the value alone,
3840 * but disable SACK delay. A non-zero value will also
3841 * enable SACK delay.
3842 */
3843 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3844 char __user *optval,
3845 int __user *optlen)
3846 {
3847 struct sctp_assoc_value params;
3848 struct sctp_association *asoc = NULL;
3849 struct sctp_sock *sp = sctp_sk(sk);
3850
3851 if (len < sizeof(struct sctp_assoc_value))
3852 return - EINVAL;
3853
3854 len = sizeof(struct sctp_assoc_value);
3855
3856 if (copy_from_user(&params, optval, len))
3857 return -EFAULT;
3858
3859 /* Get association, if assoc_id != 0 and the socket is a one
3860 * to many style socket, and an association was not found, then
3861 * the id was invalid.
3862 */
3863 asoc = sctp_id2assoc(sk, params.assoc_id);
3864 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3865 return -EINVAL;
3866
3867 if (asoc) {
3868 /* Fetch association values. */
3869 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3870 params.assoc_value = jiffies_to_msecs(
3871 asoc->sackdelay);
3872 else
3873 params.assoc_value = 0;
3874 } else {
3875 /* Fetch socket values. */
3876 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3877 params.assoc_value = sp->sackdelay;
3878 else
3879 params.assoc_value = 0;
3880 }
3881
3882 if (copy_to_user(optval, &params, len))
3883 return -EFAULT;
3884
3885 if (put_user(len, optlen))
3886 return -EFAULT;
3887
3888 return 0;
3889 }
3890
3891 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3892 *
3893 * Applications can specify protocol parameters for the default association
3894 * initialization. The option name argument to setsockopt() and getsockopt()
3895 * is SCTP_INITMSG.
3896 *
3897 * Setting initialization parameters is effective only on an unconnected
3898 * socket (for UDP-style sockets only future associations are effected
3899 * by the change). With TCP-style sockets, this option is inherited by
3900 * sockets derived from a listener socket.
3901 */
3902 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3903 {
3904 if (len < sizeof(struct sctp_initmsg))
3905 return -EINVAL;
3906 len = sizeof(struct sctp_initmsg);
3907 if (put_user(len, optlen))
3908 return -EFAULT;
3909 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3910 return -EFAULT;
3911 return 0;
3912 }
3913
3914 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3915 char __user *optval,
3916 int __user *optlen)
3917 {
3918 sctp_assoc_t id;
3919 struct sctp_association *asoc;
3920 struct list_head *pos;
3921 int cnt = 0;
3922
3923 if (len < sizeof(sctp_assoc_t))
3924 return -EINVAL;
3925
3926 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3927 return -EFAULT;
3928
3929 /* For UDP-style sockets, id specifies the association to query. */
3930 asoc = sctp_id2assoc(sk, id);
3931 if (!asoc)
3932 return -EINVAL;
3933
3934 list_for_each(pos, &asoc->peer.transport_addr_list) {
3935 cnt ++;
3936 }
3937
3938 return cnt;
3939 }
3940
3941 /*
3942 * Old API for getting list of peer addresses. Does not work for 32-bit
3943 * programs running on a 64-bit kernel
3944 */
3945 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3946 char __user *optval,
3947 int __user *optlen)
3948 {
3949 struct sctp_association *asoc;
3950 struct list_head *pos;
3951 int cnt = 0;
3952 struct sctp_getaddrs_old getaddrs;
3953 struct sctp_transport *from;
3954 void __user *to;
3955 union sctp_addr temp;
3956 struct sctp_sock *sp = sctp_sk(sk);
3957 int addrlen;
3958
3959 if (len < sizeof(struct sctp_getaddrs_old))
3960 return -EINVAL;
3961
3962 len = sizeof(struct sctp_getaddrs_old);
3963
3964 if (copy_from_user(&getaddrs, optval, len))
3965 return -EFAULT;
3966
3967 if (getaddrs.addr_num <= 0) return -EINVAL;
3968
3969 /* For UDP-style sockets, id specifies the association to query. */
3970 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3971 if (!asoc)
3972 return -EINVAL;
3973
3974 to = (void __user *)getaddrs.addrs;
3975 list_for_each(pos, &asoc->peer.transport_addr_list) {
3976 from = list_entry(pos, struct sctp_transport, transports);
3977 memcpy(&temp, &from->ipaddr, sizeof(temp));
3978 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3979 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3980 if (copy_to_user(to, &temp, addrlen))
3981 return -EFAULT;
3982 to += addrlen ;
3983 cnt ++;
3984 if (cnt >= getaddrs.addr_num) break;
3985 }
3986 getaddrs.addr_num = cnt;
3987 if (put_user(len, optlen))
3988 return -EFAULT;
3989 if (copy_to_user(optval, &getaddrs, len))
3990 return -EFAULT;
3991
3992 return 0;
3993 }
3994
3995 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3996 char __user *optval, int __user *optlen)
3997 {
3998 struct sctp_association *asoc;
3999 struct list_head *pos;
4000 int cnt = 0;
4001 struct sctp_getaddrs getaddrs;
4002 struct sctp_transport *from;
4003 void __user *to;
4004 union sctp_addr temp;
4005 struct sctp_sock *sp = sctp_sk(sk);
4006 int addrlen;
4007 size_t space_left;
4008 int bytes_copied;
4009
4010 if (len < sizeof(struct sctp_getaddrs))
4011 return -EINVAL;
4012
4013 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4014 return -EFAULT;
4015
4016 /* For UDP-style sockets, id specifies the association to query. */
4017 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4018 if (!asoc)
4019 return -EINVAL;
4020
4021 to = optval + offsetof(struct sctp_getaddrs,addrs);
4022 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4023
4024 list_for_each(pos, &asoc->peer.transport_addr_list) {
4025 from = list_entry(pos, struct sctp_transport, transports);
4026 memcpy(&temp, &from->ipaddr, sizeof(temp));
4027 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4028 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
4029 if (space_left < addrlen)
4030 return -ENOMEM;
4031 if (copy_to_user(to, &temp, addrlen))
4032 return -EFAULT;
4033 to += addrlen;
4034 cnt++;
4035 space_left -= addrlen;
4036 }
4037
4038 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4039 return -EFAULT;
4040 bytes_copied = ((char __user *)to) - optval;
4041 if (put_user(bytes_copied, optlen))
4042 return -EFAULT;
4043
4044 return 0;
4045 }
4046
4047 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4048 char __user *optval,
4049 int __user *optlen)
4050 {
4051 sctp_assoc_t id;
4052 struct sctp_bind_addr *bp;
4053 struct sctp_association *asoc;
4054 struct list_head *pos, *temp;
4055 struct sctp_sockaddr_entry *addr;
4056 rwlock_t *addr_lock;
4057 int cnt = 0;
4058
4059 if (len < sizeof(sctp_assoc_t))
4060 return -EINVAL;
4061
4062 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4063 return -EFAULT;
4064
4065 /*
4066 * For UDP-style sockets, id specifies the association to query.
4067 * If the id field is set to the value '0' then the locally bound
4068 * addresses are returned without regard to any particular
4069 * association.
4070 */
4071 if (0 == id) {
4072 bp = &sctp_sk(sk)->ep->base.bind_addr;
4073 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4074 } else {
4075 asoc = sctp_id2assoc(sk, id);
4076 if (!asoc)
4077 return -EINVAL;
4078 bp = &asoc->base.bind_addr;
4079 addr_lock = &asoc->base.addr_lock;
4080 }
4081
4082 sctp_read_lock(addr_lock);
4083
4084 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4085 * addresses from the global local address list.
4086 */
4087 if (sctp_list_single_entry(&bp->address_list)) {
4088 addr = list_entry(bp->address_list.next,
4089 struct sctp_sockaddr_entry, list);
4090 if (sctp_is_any(&addr->a)) {
4091 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
4092 addr = list_entry(pos,
4093 struct sctp_sockaddr_entry,
4094 list);
4095 if ((PF_INET == sk->sk_family) &&
4096 (AF_INET6 == addr->a.sa.sa_family))
4097 continue;
4098 cnt++;
4099 }
4100 } else {
4101 cnt = 1;
4102 }
4103 goto done;
4104 }
4105
4106 list_for_each(pos, &bp->address_list) {
4107 cnt ++;
4108 }
4109
4110 done:
4111 sctp_read_unlock(addr_lock);
4112 return cnt;
4113 }
4114
4115 /* Helper function that copies local addresses to user and returns the number
4116 * of addresses copied.
4117 */
4118 static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4119 int max_addrs, void *to,
4120 int *bytes_copied)
4121 {
4122 struct list_head *pos, *next;
4123 struct sctp_sockaddr_entry *addr;
4124 union sctp_addr temp;
4125 int cnt = 0;
4126 int addrlen;
4127
4128 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4129 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4130 if ((PF_INET == sk->sk_family) &&
4131 (AF_INET6 == addr->a.sa.sa_family))
4132 continue;
4133 memcpy(&temp, &addr->a, sizeof(temp));
4134 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4135 &temp);
4136 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4137 memcpy(to, &temp, addrlen);
4138
4139 to += addrlen;
4140 *bytes_copied += addrlen;
4141 cnt ++;
4142 if (cnt >= max_addrs) break;
4143 }
4144
4145 return cnt;
4146 }
4147
4148 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4149 size_t space_left, int *bytes_copied)
4150 {
4151 struct list_head *pos, *next;
4152 struct sctp_sockaddr_entry *addr;
4153 union sctp_addr temp;
4154 int cnt = 0;
4155 int addrlen;
4156
4157 list_for_each_safe(pos, next, &sctp_local_addr_list) {
4158 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4159 if ((PF_INET == sk->sk_family) &&
4160 (AF_INET6 == addr->a.sa.sa_family))
4161 continue;
4162 memcpy(&temp, &addr->a, sizeof(temp));
4163 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4164 &temp);
4165 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4166 if (space_left < addrlen)
4167 return -ENOMEM;
4168 memcpy(to, &temp, addrlen);
4169
4170 to += addrlen;
4171 cnt ++;
4172 space_left -= addrlen;
4173 *bytes_copied += addrlen;
4174 }
4175
4176 return cnt;
4177 }
4178
4179 /* Old API for getting list of local addresses. Does not work for 32-bit
4180 * programs running on a 64-bit kernel
4181 */
4182 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4183 char __user *optval, int __user *optlen)
4184 {
4185 struct sctp_bind_addr *bp;
4186 struct sctp_association *asoc;
4187 struct list_head *pos;
4188 int cnt = 0;
4189 struct sctp_getaddrs_old getaddrs;
4190 struct sctp_sockaddr_entry *addr;
4191 void __user *to;
4192 union sctp_addr temp;
4193 struct sctp_sock *sp = sctp_sk(sk);
4194 int addrlen;
4195 rwlock_t *addr_lock;
4196 int err = 0;
4197 void *addrs;
4198 void *buf;
4199 int bytes_copied = 0;
4200
4201 if (len < sizeof(struct sctp_getaddrs_old))
4202 return -EINVAL;
4203
4204 len = sizeof(struct sctp_getaddrs_old);
4205 if (copy_from_user(&getaddrs, optval, len))
4206 return -EFAULT;
4207
4208 if (getaddrs.addr_num <= 0) return -EINVAL;
4209 /*
4210 * For UDP-style sockets, id specifies the association to query.
4211 * If the id field is set to the value '0' then the locally bound
4212 * addresses are returned without regard to any particular
4213 * association.
4214 */
4215 if (0 == getaddrs.assoc_id) {
4216 bp = &sctp_sk(sk)->ep->base.bind_addr;
4217 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4218 } else {
4219 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4220 if (!asoc)
4221 return -EINVAL;
4222 bp = &asoc->base.bind_addr;
4223 addr_lock = &asoc->base.addr_lock;
4224 }
4225
4226 to = getaddrs.addrs;
4227
4228 /* Allocate space for a local instance of packed array to hold all
4229 * the data. We store addresses here first and then put write them
4230 * to the user in one shot.
4231 */
4232 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4233 GFP_KERNEL);
4234 if (!addrs)
4235 return -ENOMEM;
4236
4237 sctp_read_lock(addr_lock);
4238
4239 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4240 * addresses from the global local address list.
4241 */
4242 if (sctp_list_single_entry(&bp->address_list)) {
4243 addr = list_entry(bp->address_list.next,
4244 struct sctp_sockaddr_entry, list);
4245 if (sctp_is_any(&addr->a)) {
4246 cnt = sctp_copy_laddrs_old(sk, bp->port,
4247 getaddrs.addr_num,
4248 addrs, &bytes_copied);
4249 goto copy_getaddrs;
4250 }
4251 }
4252
4253 buf = addrs;
4254 list_for_each(pos, &bp->address_list) {
4255 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4256 memcpy(&temp, &addr->a, sizeof(temp));
4257 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4258 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4259 memcpy(buf, &temp, addrlen);
4260 buf += addrlen;
4261 bytes_copied += addrlen;
4262 cnt ++;
4263 if (cnt >= getaddrs.addr_num) break;
4264 }
4265
4266 copy_getaddrs:
4267 sctp_read_unlock(addr_lock);
4268
4269 /* copy the entire address list into the user provided space */
4270 if (copy_to_user(to, addrs, bytes_copied)) {
4271 err = -EFAULT;
4272 goto error;
4273 }
4274
4275 /* copy the leading structure back to user */
4276 getaddrs.addr_num = cnt;
4277 if (copy_to_user(optval, &getaddrs, len))
4278 err = -EFAULT;
4279
4280 error:
4281 kfree(addrs);
4282 return err;
4283 }
4284
4285 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4286 char __user *optval, int __user *optlen)
4287 {
4288 struct sctp_bind_addr *bp;
4289 struct sctp_association *asoc;
4290 struct list_head *pos;
4291 int cnt = 0;
4292 struct sctp_getaddrs getaddrs;
4293 struct sctp_sockaddr_entry *addr;
4294 void __user *to;
4295 union sctp_addr temp;
4296 struct sctp_sock *sp = sctp_sk(sk);
4297 int addrlen;
4298 rwlock_t *addr_lock;
4299 int err = 0;
4300 size_t space_left;
4301 int bytes_copied = 0;
4302 void *addrs;
4303 void *buf;
4304
4305 if (len < sizeof(struct sctp_getaddrs))
4306 return -EINVAL;
4307
4308 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4309 return -EFAULT;
4310
4311 /*
4312 * For UDP-style sockets, id specifies the association to query.
4313 * If the id field is set to the value '0' then the locally bound
4314 * addresses are returned without regard to any particular
4315 * association.
4316 */
4317 if (0 == getaddrs.assoc_id) {
4318 bp = &sctp_sk(sk)->ep->base.bind_addr;
4319 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4320 } else {
4321 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4322 if (!asoc)
4323 return -EINVAL;
4324 bp = &asoc->base.bind_addr;
4325 addr_lock = &asoc->base.addr_lock;
4326 }
4327
4328 to = optval + offsetof(struct sctp_getaddrs,addrs);
4329 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4330
4331 addrs = kmalloc(space_left, GFP_KERNEL);
4332 if (!addrs)
4333 return -ENOMEM;
4334
4335 sctp_read_lock(addr_lock);
4336
4337 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4338 * addresses from the global local address list.
4339 */
4340 if (sctp_list_single_entry(&bp->address_list)) {
4341 addr = list_entry(bp->address_list.next,
4342 struct sctp_sockaddr_entry, list);
4343 if (sctp_is_any(&addr->a)) {
4344 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4345 space_left, &bytes_copied);
4346 if (cnt < 0) {
4347 err = cnt;
4348 goto error;
4349 }
4350 goto copy_getaddrs;
4351 }
4352 }
4353
4354 buf = addrs;
4355 list_for_each(pos, &bp->address_list) {
4356 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4357 memcpy(&temp, &addr->a, sizeof(temp));
4358 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4359 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4360 if (space_left < addrlen) {
4361 err = -ENOMEM; /*fixme: right error?*/
4362 goto error;
4363 }
4364 memcpy(buf, &temp, addrlen);
4365 buf += addrlen;
4366 bytes_copied += addrlen;
4367 cnt ++;
4368 space_left -= addrlen;
4369 }
4370
4371 copy_getaddrs:
4372 sctp_read_unlock(addr_lock);
4373
4374 if (copy_to_user(to, addrs, bytes_copied)) {
4375 err = -EFAULT;
4376 goto error;
4377 }
4378 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4379 err = -EFAULT;
4380 goto error;
4381 }
4382 if (put_user(bytes_copied, optlen))
4383 err = -EFAULT;
4384 error:
4385 kfree(addrs);
4386 return err;
4387 }
4388
4389 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4390 *
4391 * Requests that the local SCTP stack use the enclosed peer address as
4392 * the association primary. The enclosed address must be one of the
4393 * association peer's addresses.
4394 */
4395 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4396 char __user *optval, int __user *optlen)
4397 {
4398 struct sctp_prim prim;
4399 struct sctp_association *asoc;
4400 struct sctp_sock *sp = sctp_sk(sk);
4401
4402 if (len < sizeof(struct sctp_prim))
4403 return -EINVAL;
4404
4405 len = sizeof(struct sctp_prim);
4406
4407 if (copy_from_user(&prim, optval, len))
4408 return -EFAULT;
4409
4410 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4411 if (!asoc)
4412 return -EINVAL;
4413
4414 if (!asoc->peer.primary_path)
4415 return -ENOTCONN;
4416
4417 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4418 asoc->peer.primary_path->af_specific->sockaddr_len);
4419
4420 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4421 (union sctp_addr *)&prim.ssp_addr);
4422
4423 if (put_user(len, optlen))
4424 return -EFAULT;
4425 if (copy_to_user(optval, &prim, len))
4426 return -EFAULT;
4427
4428 return 0;
4429 }
4430
4431 /*
4432 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
4433 *
4434 * Requests that the local endpoint set the specified Adaptation Layer
4435 * Indication parameter for all future INIT and INIT-ACK exchanges.
4436 */
4437 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
4438 char __user *optval, int __user *optlen)
4439 {
4440 struct sctp_setadaptation adaptation;
4441
4442 if (len < sizeof(struct sctp_setadaptation))
4443 return -EINVAL;
4444
4445 len = sizeof(struct sctp_setadaptation);
4446
4447 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4448
4449 if (put_user(len, optlen))
4450 return -EFAULT;
4451 if (copy_to_user(optval, &adaptation, len))
4452 return -EFAULT;
4453
4454 return 0;
4455 }
4456
4457 /*
4458 *
4459 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4460 *
4461 * Applications that wish to use the sendto() system call may wish to
4462 * specify a default set of parameters that would normally be supplied
4463 * through the inclusion of ancillary data. This socket option allows
4464 * such an application to set the default sctp_sndrcvinfo structure.
4465
4466
4467 * The application that wishes to use this socket option simply passes
4468 * in to this call the sctp_sndrcvinfo structure defined in Section
4469 * 5.2.2) The input parameters accepted by this call include
4470 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4471 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4472 * to this call if the caller is using the UDP model.
4473 *
4474 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4475 */
4476 static int sctp_getsockopt_default_send_param(struct sock *sk,
4477 int len, char __user *optval,
4478 int __user *optlen)
4479 {
4480 struct sctp_sndrcvinfo info;
4481 struct sctp_association *asoc;
4482 struct sctp_sock *sp = sctp_sk(sk);
4483
4484 if (len < sizeof(struct sctp_sndrcvinfo))
4485 return -EINVAL;
4486
4487 len = sizeof(struct sctp_sndrcvinfo);
4488
4489 if (copy_from_user(&info, optval, len))
4490 return -EFAULT;
4491
4492 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4493 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4494 return -EINVAL;
4495
4496 if (asoc) {
4497 info.sinfo_stream = asoc->default_stream;
4498 info.sinfo_flags = asoc->default_flags;
4499 info.sinfo_ppid = asoc->default_ppid;
4500 info.sinfo_context = asoc->default_context;
4501 info.sinfo_timetolive = asoc->default_timetolive;
4502 } else {
4503 info.sinfo_stream = sp->default_stream;
4504 info.sinfo_flags = sp->default_flags;
4505 info.sinfo_ppid = sp->default_ppid;
4506 info.sinfo_context = sp->default_context;
4507 info.sinfo_timetolive = sp->default_timetolive;
4508 }
4509
4510 if (put_user(len, optlen))
4511 return -EFAULT;
4512 if (copy_to_user(optval, &info, len))
4513 return -EFAULT;
4514
4515 return 0;
4516 }
4517
4518 /*
4519 *
4520 * 7.1.5 SCTP_NODELAY
4521 *
4522 * Turn on/off any Nagle-like algorithm. This means that packets are
4523 * generally sent as soon as possible and no unnecessary delays are
4524 * introduced, at the cost of more packets in the network. Expects an
4525 * integer boolean flag.
4526 */
4527
4528 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4529 char __user *optval, int __user *optlen)
4530 {
4531 int val;
4532
4533 if (len < sizeof(int))
4534 return -EINVAL;
4535
4536 len = sizeof(int);
4537 val = (sctp_sk(sk)->nodelay == 1);
4538 if (put_user(len, optlen))
4539 return -EFAULT;
4540 if (copy_to_user(optval, &val, len))
4541 return -EFAULT;
4542 return 0;
4543 }
4544
4545 /*
4546 *
4547 * 7.1.1 SCTP_RTOINFO
4548 *
4549 * The protocol parameters used to initialize and bound retransmission
4550 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4551 * and modify these parameters.
4552 * All parameters are time values, in milliseconds. A value of 0, when
4553 * modifying the parameters, indicates that the current value should not
4554 * be changed.
4555 *
4556 */
4557 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4558 char __user *optval,
4559 int __user *optlen) {
4560 struct sctp_rtoinfo rtoinfo;
4561 struct sctp_association *asoc;
4562
4563 if (len < sizeof (struct sctp_rtoinfo))
4564 return -EINVAL;
4565
4566 len = sizeof(struct sctp_rtoinfo);
4567
4568 if (copy_from_user(&rtoinfo, optval, len))
4569 return -EFAULT;
4570
4571 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4572
4573 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4574 return -EINVAL;
4575
4576 /* Values corresponding to the specific association. */
4577 if (asoc) {
4578 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4579 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4580 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4581 } else {
4582 /* Values corresponding to the endpoint. */
4583 struct sctp_sock *sp = sctp_sk(sk);
4584
4585 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4586 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4587 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4588 }
4589
4590 if (put_user(len, optlen))
4591 return -EFAULT;
4592
4593 if (copy_to_user(optval, &rtoinfo, len))
4594 return -EFAULT;
4595
4596 return 0;
4597 }
4598
4599 /*
4600 *
4601 * 7.1.2 SCTP_ASSOCINFO
4602 *
4603 * This option is used to tune the maximum retransmission attempts
4604 * of the association.
4605 * Returns an error if the new association retransmission value is
4606 * greater than the sum of the retransmission value of the peer.
4607 * See [SCTP] for more information.
4608 *
4609 */
4610 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4611 char __user *optval,
4612 int __user *optlen)
4613 {
4614
4615 struct sctp_assocparams assocparams;
4616 struct sctp_association *asoc;
4617 struct list_head *pos;
4618 int cnt = 0;
4619
4620 if (len < sizeof (struct sctp_assocparams))
4621 return -EINVAL;
4622
4623 len = sizeof(struct sctp_assocparams);
4624
4625 if (copy_from_user(&assocparams, optval, len))
4626 return -EFAULT;
4627
4628 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4629
4630 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4631 return -EINVAL;
4632
4633 /* Values correspoinding to the specific association */
4634 if (asoc) {
4635 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4636 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4637 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4638 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4639 * 1000) +
4640 (asoc->cookie_life.tv_usec
4641 / 1000);
4642
4643 list_for_each(pos, &asoc->peer.transport_addr_list) {
4644 cnt ++;
4645 }
4646
4647 assocparams.sasoc_number_peer_destinations = cnt;
4648 } else {
4649 /* Values corresponding to the endpoint */
4650 struct sctp_sock *sp = sctp_sk(sk);
4651
4652 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4653 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4654 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4655 assocparams.sasoc_cookie_life =
4656 sp->assocparams.sasoc_cookie_life;
4657 assocparams.sasoc_number_peer_destinations =
4658 sp->assocparams.
4659 sasoc_number_peer_destinations;
4660 }
4661
4662 if (put_user(len, optlen))
4663 return -EFAULT;
4664
4665 if (copy_to_user(optval, &assocparams, len))
4666 return -EFAULT;
4667
4668 return 0;
4669 }
4670
4671 /*
4672 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4673 *
4674 * This socket option is a boolean flag which turns on or off mapped V4
4675 * addresses. If this option is turned on and the socket is type
4676 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4677 * If this option is turned off, then no mapping will be done of V4
4678 * addresses and a user will receive both PF_INET6 and PF_INET type
4679 * addresses on the socket.
4680 */
4681 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4682 char __user *optval, int __user *optlen)
4683 {
4684 int val;
4685 struct sctp_sock *sp = sctp_sk(sk);
4686
4687 if (len < sizeof(int))
4688 return -EINVAL;
4689
4690 len = sizeof(int);
4691 val = sp->v4mapped;
4692 if (put_user(len, optlen))
4693 return -EFAULT;
4694 if (copy_to_user(optval, &val, len))
4695 return -EFAULT;
4696
4697 return 0;
4698 }
4699
4700 /*
4701 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4702 * (chapter and verse is quoted at sctp_setsockopt_context())
4703 */
4704 static int sctp_getsockopt_context(struct sock *sk, int len,
4705 char __user *optval, int __user *optlen)
4706 {
4707 struct sctp_assoc_value params;
4708 struct sctp_sock *sp;
4709 struct sctp_association *asoc;
4710
4711 if (len < sizeof(struct sctp_assoc_value))
4712 return -EINVAL;
4713
4714 len = sizeof(struct sctp_assoc_value);
4715
4716 if (copy_from_user(&params, optval, len))
4717 return -EFAULT;
4718
4719 sp = sctp_sk(sk);
4720
4721 if (params.assoc_id != 0) {
4722 asoc = sctp_id2assoc(sk, params.assoc_id);
4723 if (!asoc)
4724 return -EINVAL;
4725 params.assoc_value = asoc->default_rcv_context;
4726 } else {
4727 params.assoc_value = sp->default_rcv_context;
4728 }
4729
4730 if (put_user(len, optlen))
4731 return -EFAULT;
4732 if (copy_to_user(optval, &params, len))
4733 return -EFAULT;
4734
4735 return 0;
4736 }
4737
4738 /*
4739 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4740 *
4741 * This socket option specifies the maximum size to put in any outgoing
4742 * SCTP chunk. If a message is larger than this size it will be
4743 * fragmented by SCTP into the specified size. Note that the underlying
4744 * SCTP implementation may fragment into smaller sized chunks when the
4745 * PMTU of the underlying association is smaller than the value set by
4746 * the user.
4747 */
4748 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4749 char __user *optval, int __user *optlen)
4750 {
4751 int val;
4752
4753 if (len < sizeof(int))
4754 return -EINVAL;
4755
4756 len = sizeof(int);
4757
4758 val = sctp_sk(sk)->user_frag;
4759 if (put_user(len, optlen))
4760 return -EFAULT;
4761 if (copy_to_user(optval, &val, len))
4762 return -EFAULT;
4763
4764 return 0;
4765 }
4766
4767 /*
4768 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4769 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4770 */
4771 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4772 char __user *optval, int __user *optlen)
4773 {
4774 int val;
4775
4776 if (len < sizeof(int))
4777 return -EINVAL;
4778
4779 len = sizeof(int);
4780
4781 val = sctp_sk(sk)->frag_interleave;
4782 if (put_user(len, optlen))
4783 return -EFAULT;
4784 if (copy_to_user(optval, &val, len))
4785 return -EFAULT;
4786
4787 return 0;
4788 }
4789
4790 /*
4791 * 7.1.25. Set or Get the sctp partial delivery point
4792 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4793 */
4794 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4795 char __user *optval,
4796 int __user *optlen)
4797 {
4798 u32 val;
4799
4800 if (len < sizeof(u32))
4801 return -EINVAL;
4802
4803 len = sizeof(u32);
4804
4805 val = sctp_sk(sk)->pd_point;
4806 if (put_user(len, optlen))
4807 return -EFAULT;
4808 if (copy_to_user(optval, &val, len))
4809 return -EFAULT;
4810
4811 return -ENOTSUPP;
4812 }
4813
4814 /*
4815 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4816 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4817 */
4818 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4819 char __user *optval,
4820 int __user *optlen)
4821 {
4822 int val;
4823
4824 if (len < sizeof(int))
4825 return -EINVAL;
4826
4827 len = sizeof(int);
4828
4829 val = sctp_sk(sk)->max_burst;
4830 if (put_user(len, optlen))
4831 return -EFAULT;
4832 if (copy_to_user(optval, &val, len))
4833 return -EFAULT;
4834
4835 return -ENOTSUPP;
4836 }
4837
4838 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4839 char __user *optval, int __user *optlen)
4840 {
4841 int retval = 0;
4842 int len;
4843
4844 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4845 sk, optname);
4846
4847 /* I can hardly begin to describe how wrong this is. This is
4848 * so broken as to be worse than useless. The API draft
4849 * REALLY is NOT helpful here... I am not convinced that the
4850 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4851 * are at all well-founded.
4852 */
4853 if (level != SOL_SCTP) {
4854 struct sctp_af *af = sctp_sk(sk)->pf->af;
4855
4856 retval = af->getsockopt(sk, level, optname, optval, optlen);
4857 return retval;
4858 }
4859
4860 if (get_user(len, optlen))
4861 return -EFAULT;
4862
4863 sctp_lock_sock(sk);
4864
4865 switch (optname) {
4866 case SCTP_STATUS:
4867 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4868 break;
4869 case SCTP_DISABLE_FRAGMENTS:
4870 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4871 optlen);
4872 break;
4873 case SCTP_EVENTS:
4874 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4875 break;
4876 case SCTP_AUTOCLOSE:
4877 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4878 break;
4879 case SCTP_SOCKOPT_PEELOFF:
4880 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4881 break;
4882 case SCTP_PEER_ADDR_PARAMS:
4883 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4884 optlen);
4885 break;
4886 case SCTP_DELAYED_ACK_TIME:
4887 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4888 optlen);
4889 break;
4890 case SCTP_INITMSG:
4891 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4892 break;
4893 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4894 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4895 optlen);
4896 break;
4897 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4898 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4899 optlen);
4900 break;
4901 case SCTP_GET_PEER_ADDRS_OLD:
4902 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4903 optlen);
4904 break;
4905 case SCTP_GET_LOCAL_ADDRS_OLD:
4906 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4907 optlen);
4908 break;
4909 case SCTP_GET_PEER_ADDRS:
4910 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4911 optlen);
4912 break;
4913 case SCTP_GET_LOCAL_ADDRS:
4914 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4915 optlen);
4916 break;
4917 case SCTP_DEFAULT_SEND_PARAM:
4918 retval = sctp_getsockopt_default_send_param(sk, len,
4919 optval, optlen);
4920 break;
4921 case SCTP_PRIMARY_ADDR:
4922 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4923 break;
4924 case SCTP_NODELAY:
4925 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4926 break;
4927 case SCTP_RTOINFO:
4928 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4929 break;
4930 case SCTP_ASSOCINFO:
4931 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4932 break;
4933 case SCTP_I_WANT_MAPPED_V4_ADDR:
4934 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4935 break;
4936 case SCTP_MAXSEG:
4937 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4938 break;
4939 case SCTP_GET_PEER_ADDR_INFO:
4940 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4941 optlen);
4942 break;
4943 case SCTP_ADAPTATION_LAYER:
4944 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
4945 optlen);
4946 break;
4947 case SCTP_CONTEXT:
4948 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4949 break;
4950 case SCTP_FRAGMENT_INTERLEAVE:
4951 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4952 optlen);
4953 break;
4954 case SCTP_PARTIAL_DELIVERY_POINT:
4955 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4956 optlen);
4957 break;
4958 case SCTP_MAX_BURST:
4959 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4960 break;
4961 default:
4962 retval = -ENOPROTOOPT;
4963 break;
4964 }
4965
4966 sctp_release_sock(sk);
4967 return retval;
4968 }
4969
4970 static void sctp_hash(struct sock *sk)
4971 {
4972 /* STUB */
4973 }
4974
4975 static void sctp_unhash(struct sock *sk)
4976 {
4977 /* STUB */
4978 }
4979
4980 /* Check if port is acceptable. Possibly find first available port.
4981 *
4982 * The port hash table (contained in the 'global' SCTP protocol storage
4983 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4984 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4985 * list (the list number is the port number hashed out, so as you
4986 * would expect from a hash function, all the ports in a given list have
4987 * such a number that hashes out to the same list number; you were
4988 * expecting that, right?); so each list has a set of ports, with a
4989 * link to the socket (struct sock) that uses it, the port number and
4990 * a fastreuse flag (FIXME: NPI ipg).
4991 */
4992 static struct sctp_bind_bucket *sctp_bucket_create(
4993 struct sctp_bind_hashbucket *head, unsigned short snum);
4994
4995 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4996 {
4997 struct sctp_bind_hashbucket *head; /* hash list */
4998 struct sctp_bind_bucket *pp; /* hash list port iterator */
4999 unsigned short snum;
5000 int ret;
5001
5002 snum = ntohs(addr->v4.sin_port);
5003
5004 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5005 sctp_local_bh_disable();
5006
5007 if (snum == 0) {
5008 /* Search for an available port.
5009 *
5010 * 'sctp_port_rover' was the last port assigned, so
5011 * we start to search from 'sctp_port_rover +
5012 * 1'. What we do is first check if port 'rover' is
5013 * already in the hash table; if not, we use that; if
5014 * it is, we try next.
5015 */
5016 int low = sysctl_local_port_range[0];
5017 int high = sysctl_local_port_range[1];
5018 int remaining = (high - low) + 1;
5019 int rover;
5020 int index;
5021
5022 sctp_spin_lock(&sctp_port_alloc_lock);
5023 rover = sctp_port_rover;
5024 do {
5025 rover++;
5026 if ((rover < low) || (rover > high))
5027 rover = low;
5028 index = sctp_phashfn(rover);
5029 head = &sctp_port_hashtable[index];
5030 sctp_spin_lock(&head->lock);
5031 for (pp = head->chain; pp; pp = pp->next)
5032 if (pp->port == rover)
5033 goto next;
5034 break;
5035 next:
5036 sctp_spin_unlock(&head->lock);
5037 } while (--remaining > 0);
5038 sctp_port_rover = rover;
5039 sctp_spin_unlock(&sctp_port_alloc_lock);
5040
5041 /* Exhausted local port range during search? */
5042 ret = 1;
5043 if (remaining <= 0)
5044 goto fail;
5045
5046 /* OK, here is the one we will use. HEAD (the port
5047 * hash table list entry) is non-NULL and we hold it's
5048 * mutex.
5049 */
5050 snum = rover;
5051 } else {
5052 /* We are given an specific port number; we verify
5053 * that it is not being used. If it is used, we will
5054 * exahust the search in the hash list corresponding
5055 * to the port number (snum) - we detect that with the
5056 * port iterator, pp being NULL.
5057 */
5058 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5059 sctp_spin_lock(&head->lock);
5060 for (pp = head->chain; pp; pp = pp->next) {
5061 if (pp->port == snum)
5062 goto pp_found;
5063 }
5064 }
5065 pp = NULL;
5066 goto pp_not_found;
5067 pp_found:
5068 if (!hlist_empty(&pp->owner)) {
5069 /* We had a port hash table hit - there is an
5070 * available port (pp != NULL) and it is being
5071 * used by other socket (pp->owner not empty); that other
5072 * socket is going to be sk2.
5073 */
5074 int reuse = sk->sk_reuse;
5075 struct sock *sk2;
5076 struct hlist_node *node;
5077
5078 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5079 if (pp->fastreuse && sk->sk_reuse &&
5080 sk->sk_state != SCTP_SS_LISTENING)
5081 goto success;
5082
5083 /* Run through the list of sockets bound to the port
5084 * (pp->port) [via the pointers bind_next and
5085 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5086 * we get the endpoint they describe and run through
5087 * the endpoint's list of IP (v4 or v6) addresses,
5088 * comparing each of the addresses with the address of
5089 * the socket sk. If we find a match, then that means
5090 * that this port/socket (sk) combination are already
5091 * in an endpoint.
5092 */
5093 sk_for_each_bound(sk2, node, &pp->owner) {
5094 struct sctp_endpoint *ep2;
5095 ep2 = sctp_sk(sk2)->ep;
5096
5097 if (reuse && sk2->sk_reuse &&
5098 sk2->sk_state != SCTP_SS_LISTENING)
5099 continue;
5100
5101 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
5102 sctp_sk(sk))) {
5103 ret = (long)sk2;
5104 goto fail_unlock;
5105 }
5106 }
5107 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5108 }
5109 pp_not_found:
5110 /* If there was a hash table miss, create a new port. */
5111 ret = 1;
5112 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5113 goto fail_unlock;
5114
5115 /* In either case (hit or miss), make sure fastreuse is 1 only
5116 * if sk->sk_reuse is too (that is, if the caller requested
5117 * SO_REUSEADDR on this socket -sk-).
5118 */
5119 if (hlist_empty(&pp->owner)) {
5120 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5121 pp->fastreuse = 1;
5122 else
5123 pp->fastreuse = 0;
5124 } else if (pp->fastreuse &&
5125 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
5126 pp->fastreuse = 0;
5127
5128 /* We are set, so fill up all the data in the hash table
5129 * entry, tie the socket list information with the rest of the
5130 * sockets FIXME: Blurry, NPI (ipg).
5131 */
5132 success:
5133 if (!sctp_sk(sk)->bind_hash) {
5134 inet_sk(sk)->num = snum;
5135 sk_add_bind_node(sk, &pp->owner);
5136 sctp_sk(sk)->bind_hash = pp;
5137 }
5138 ret = 0;
5139
5140 fail_unlock:
5141 sctp_spin_unlock(&head->lock);
5142
5143 fail:
5144 sctp_local_bh_enable();
5145 return ret;
5146 }
5147
5148 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5149 * port is requested.
5150 */
5151 static int sctp_get_port(struct sock *sk, unsigned short snum)
5152 {
5153 long ret;
5154 union sctp_addr addr;
5155 struct sctp_af *af = sctp_sk(sk)->pf->af;
5156
5157 /* Set up a dummy address struct from the sk. */
5158 af->from_sk(&addr, sk);
5159 addr.v4.sin_port = htons(snum);
5160
5161 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5162 ret = sctp_get_port_local(sk, &addr);
5163
5164 return (ret ? 1 : 0);
5165 }
5166
5167 /*
5168 * 3.1.3 listen() - UDP Style Syntax
5169 *
5170 * By default, new associations are not accepted for UDP style sockets.
5171 * An application uses listen() to mark a socket as being able to
5172 * accept new associations.
5173 */
5174 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5175 {
5176 struct sctp_sock *sp = sctp_sk(sk);
5177 struct sctp_endpoint *ep = sp->ep;
5178
5179 /* Only UDP style sockets that are not peeled off are allowed to
5180 * listen().
5181 */
5182 if (!sctp_style(sk, UDP))
5183 return -EINVAL;
5184
5185 /* If backlog is zero, disable listening. */
5186 if (!backlog) {
5187 if (sctp_sstate(sk, CLOSED))
5188 return 0;
5189
5190 sctp_unhash_endpoint(ep);
5191 sk->sk_state = SCTP_SS_CLOSED;
5192 }
5193
5194 /* Return if we are already listening. */
5195 if (sctp_sstate(sk, LISTENING))
5196 return 0;
5197
5198 /*
5199 * If a bind() or sctp_bindx() is not called prior to a listen()
5200 * call that allows new associations to be accepted, the system
5201 * picks an ephemeral port and will choose an address set equivalent
5202 * to binding with a wildcard address.
5203 *
5204 * This is not currently spelled out in the SCTP sockets
5205 * extensions draft, but follows the practice as seen in TCP
5206 * sockets.
5207 *
5208 * Additionally, turn off fastreuse flag since we are not listening
5209 */
5210 sk->sk_state = SCTP_SS_LISTENING;
5211 if (!ep->base.bind_addr.port) {
5212 if (sctp_autobind(sk))
5213 return -EAGAIN;
5214 } else
5215 sctp_sk(sk)->bind_hash->fastreuse = 0;
5216
5217 sctp_hash_endpoint(ep);
5218 return 0;
5219 }
5220
5221 /*
5222 * 4.1.3 listen() - TCP Style Syntax
5223 *
5224 * Applications uses listen() to ready the SCTP endpoint for accepting
5225 * inbound associations.
5226 */
5227 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5228 {
5229 struct sctp_sock *sp = sctp_sk(sk);
5230 struct sctp_endpoint *ep = sp->ep;
5231
5232 /* If backlog is zero, disable listening. */
5233 if (!backlog) {
5234 if (sctp_sstate(sk, CLOSED))
5235 return 0;
5236
5237 sctp_unhash_endpoint(ep);
5238 sk->sk_state = SCTP_SS_CLOSED;
5239 }
5240
5241 if (sctp_sstate(sk, LISTENING))
5242 return 0;
5243
5244 /*
5245 * If a bind() or sctp_bindx() is not called prior to a listen()
5246 * call that allows new associations to be accepted, the system
5247 * picks an ephemeral port and will choose an address set equivalent
5248 * to binding with a wildcard address.
5249 *
5250 * This is not currently spelled out in the SCTP sockets
5251 * extensions draft, but follows the practice as seen in TCP
5252 * sockets.
5253 */
5254 sk->sk_state = SCTP_SS_LISTENING;
5255 if (!ep->base.bind_addr.port) {
5256 if (sctp_autobind(sk))
5257 return -EAGAIN;
5258 } else
5259 sctp_sk(sk)->bind_hash->fastreuse = 0;
5260
5261 sk->sk_max_ack_backlog = backlog;
5262 sctp_hash_endpoint(ep);
5263 return 0;
5264 }
5265
5266 /*
5267 * Move a socket to LISTENING state.
5268 */
5269 int sctp_inet_listen(struct socket *sock, int backlog)
5270 {
5271 struct sock *sk = sock->sk;
5272 struct crypto_hash *tfm = NULL;
5273 int err = -EINVAL;
5274
5275 if (unlikely(backlog < 0))
5276 goto out;
5277
5278 sctp_lock_sock(sk);
5279
5280 if (sock->state != SS_UNCONNECTED)
5281 goto out;
5282
5283 /* Allocate HMAC for generating cookie. */
5284 if (sctp_hmac_alg) {
5285 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
5286 if (IS_ERR(tfm)) {
5287 if (net_ratelimit()) {
5288 printk(KERN_INFO
5289 "SCTP: failed to load transform for %s: %ld\n",
5290 sctp_hmac_alg, PTR_ERR(tfm));
5291 }
5292 err = -ENOSYS;
5293 goto out;
5294 }
5295 }
5296
5297 switch (sock->type) {
5298 case SOCK_SEQPACKET:
5299 err = sctp_seqpacket_listen(sk, backlog);
5300 break;
5301 case SOCK_STREAM:
5302 err = sctp_stream_listen(sk, backlog);
5303 break;
5304 default:
5305 break;
5306 }
5307
5308 if (err)
5309 goto cleanup;
5310
5311 /* Store away the transform reference. */
5312 sctp_sk(sk)->hmac = tfm;
5313 out:
5314 sctp_release_sock(sk);
5315 return err;
5316 cleanup:
5317 crypto_free_hash(tfm);
5318 goto out;
5319 }
5320
5321 /*
5322 * This function is done by modeling the current datagram_poll() and the
5323 * tcp_poll(). Note that, based on these implementations, we don't
5324 * lock the socket in this function, even though it seems that,
5325 * ideally, locking or some other mechanisms can be used to ensure
5326 * the integrity of the counters (sndbuf and wmem_alloc) used
5327 * in this place. We assume that we don't need locks either until proven
5328 * otherwise.
5329 *
5330 * Another thing to note is that we include the Async I/O support
5331 * here, again, by modeling the current TCP/UDP code. We don't have
5332 * a good way to test with it yet.
5333 */
5334 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5335 {
5336 struct sock *sk = sock->sk;
5337 struct sctp_sock *sp = sctp_sk(sk);
5338 unsigned int mask;
5339
5340 poll_wait(file, sk->sk_sleep, wait);
5341
5342 /* A TCP-style listening socket becomes readable when the accept queue
5343 * is not empty.
5344 */
5345 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5346 return (!list_empty(&sp->ep->asocs)) ?
5347 (POLLIN | POLLRDNORM) : 0;
5348
5349 mask = 0;
5350
5351 /* Is there any exceptional events? */
5352 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5353 mask |= POLLERR;
5354 if (sk->sk_shutdown & RCV_SHUTDOWN)
5355 mask |= POLLRDHUP;
5356 if (sk->sk_shutdown == SHUTDOWN_MASK)
5357 mask |= POLLHUP;
5358
5359 /* Is it readable? Reconsider this code with TCP-style support. */
5360 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5361 (sk->sk_shutdown & RCV_SHUTDOWN))
5362 mask |= POLLIN | POLLRDNORM;
5363
5364 /* The association is either gone or not ready. */
5365 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5366 return mask;
5367
5368 /* Is it writable? */
5369 if (sctp_writeable(sk)) {
5370 mask |= POLLOUT | POLLWRNORM;
5371 } else {
5372 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5373 /*
5374 * Since the socket is not locked, the buffer
5375 * might be made available after the writeable check and
5376 * before the bit is set. This could cause a lost I/O
5377 * signal. tcp_poll() has a race breaker for this race
5378 * condition. Based on their implementation, we put
5379 * in the following code to cover it as well.
5380 */
5381 if (sctp_writeable(sk))
5382 mask |= POLLOUT | POLLWRNORM;
5383 }
5384 return mask;
5385 }
5386
5387 /********************************************************************
5388 * 2nd Level Abstractions
5389 ********************************************************************/
5390
5391 static struct sctp_bind_bucket *sctp_bucket_create(
5392 struct sctp_bind_hashbucket *head, unsigned short snum)
5393 {
5394 struct sctp_bind_bucket *pp;
5395
5396 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
5397 SCTP_DBG_OBJCNT_INC(bind_bucket);
5398 if (pp) {
5399 pp->port = snum;
5400 pp->fastreuse = 0;
5401 INIT_HLIST_HEAD(&pp->owner);
5402 if ((pp->next = head->chain) != NULL)
5403 pp->next->pprev = &pp->next;
5404 head->chain = pp;
5405 pp->pprev = &head->chain;
5406 }
5407 return pp;
5408 }
5409
5410 /* Caller must hold hashbucket lock for this tb with local BH disabled */
5411 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5412 {
5413 if (pp && hlist_empty(&pp->owner)) {
5414 if (pp->next)
5415 pp->next->pprev = pp->pprev;
5416 *(pp->pprev) = pp->next;
5417 kmem_cache_free(sctp_bucket_cachep, pp);
5418 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5419 }
5420 }
5421
5422 /* Release this socket's reference to a local port. */
5423 static inline void __sctp_put_port(struct sock *sk)
5424 {
5425 struct sctp_bind_hashbucket *head =
5426 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5427 struct sctp_bind_bucket *pp;
5428
5429 sctp_spin_lock(&head->lock);
5430 pp = sctp_sk(sk)->bind_hash;
5431 __sk_del_bind_node(sk);
5432 sctp_sk(sk)->bind_hash = NULL;
5433 inet_sk(sk)->num = 0;
5434 sctp_bucket_destroy(pp);
5435 sctp_spin_unlock(&head->lock);
5436 }
5437
5438 void sctp_put_port(struct sock *sk)
5439 {
5440 sctp_local_bh_disable();
5441 __sctp_put_port(sk);
5442 sctp_local_bh_enable();
5443 }
5444
5445 /*
5446 * The system picks an ephemeral port and choose an address set equivalent
5447 * to binding with a wildcard address.
5448 * One of those addresses will be the primary address for the association.
5449 * This automatically enables the multihoming capability of SCTP.
5450 */
5451 static int sctp_autobind(struct sock *sk)
5452 {
5453 union sctp_addr autoaddr;
5454 struct sctp_af *af;
5455 __be16 port;
5456
5457 /* Initialize a local sockaddr structure to INADDR_ANY. */
5458 af = sctp_sk(sk)->pf->af;
5459
5460 port = htons(inet_sk(sk)->num);
5461 af->inaddr_any(&autoaddr, port);
5462
5463 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5464 }
5465
5466 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5467 *
5468 * From RFC 2292
5469 * 4.2 The cmsghdr Structure *
5470 *
5471 * When ancillary data is sent or received, any number of ancillary data
5472 * objects can be specified by the msg_control and msg_controllen members of
5473 * the msghdr structure, because each object is preceded by
5474 * a cmsghdr structure defining the object's length (the cmsg_len member).
5475 * Historically Berkeley-derived implementations have passed only one object
5476 * at a time, but this API allows multiple objects to be
5477 * passed in a single call to sendmsg() or recvmsg(). The following example
5478 * shows two ancillary data objects in a control buffer.
5479 *
5480 * |<--------------------------- msg_controllen -------------------------->|
5481 * | |
5482 *
5483 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5484 *
5485 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5486 * | | |
5487 *
5488 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5489 *
5490 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5491 * | | | | |
5492 *
5493 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5494 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5495 *
5496 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5497 *
5498 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5499 * ^
5500 * |
5501 *
5502 * msg_control
5503 * points here
5504 */
5505 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5506 sctp_cmsgs_t *cmsgs)
5507 {
5508 struct cmsghdr *cmsg;
5509
5510 for (cmsg = CMSG_FIRSTHDR(msg);
5511 cmsg != NULL;
5512 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5513 if (!CMSG_OK(msg, cmsg))
5514 return -EINVAL;
5515
5516 /* Should we parse this header or ignore? */
5517 if (cmsg->cmsg_level != IPPROTO_SCTP)
5518 continue;
5519
5520 /* Strictly check lengths following example in SCM code. */
5521 switch (cmsg->cmsg_type) {
5522 case SCTP_INIT:
5523 /* SCTP Socket API Extension
5524 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5525 *
5526 * This cmsghdr structure provides information for
5527 * initializing new SCTP associations with sendmsg().
5528 * The SCTP_INITMSG socket option uses this same data
5529 * structure. This structure is not used for
5530 * recvmsg().
5531 *
5532 * cmsg_level cmsg_type cmsg_data[]
5533 * ------------ ------------ ----------------------
5534 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5535 */
5536 if (cmsg->cmsg_len !=
5537 CMSG_LEN(sizeof(struct sctp_initmsg)))
5538 return -EINVAL;
5539 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5540 break;
5541
5542 case SCTP_SNDRCV:
5543 /* SCTP Socket API Extension
5544 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5545 *
5546 * This cmsghdr structure specifies SCTP options for
5547 * sendmsg() and describes SCTP header information
5548 * about a received message through recvmsg().
5549 *
5550 * cmsg_level cmsg_type cmsg_data[]
5551 * ------------ ------------ ----------------------
5552 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5553 */
5554 if (cmsg->cmsg_len !=
5555 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5556 return -EINVAL;
5557
5558 cmsgs->info =
5559 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5560
5561 /* Minimally, validate the sinfo_flags. */
5562 if (cmsgs->info->sinfo_flags &
5563 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5564 SCTP_ABORT | SCTP_EOF))
5565 return -EINVAL;
5566 break;
5567
5568 default:
5569 return -EINVAL;
5570 }
5571 }
5572 return 0;
5573 }
5574
5575 /*
5576 * Wait for a packet..
5577 * Note: This function is the same function as in core/datagram.c
5578 * with a few modifications to make lksctp work.
5579 */
5580 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5581 {
5582 int error;
5583 DEFINE_WAIT(wait);
5584
5585 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5586
5587 /* Socket errors? */
5588 error = sock_error(sk);
5589 if (error)
5590 goto out;
5591
5592 if (!skb_queue_empty(&sk->sk_receive_queue))
5593 goto ready;
5594
5595 /* Socket shut down? */
5596 if (sk->sk_shutdown & RCV_SHUTDOWN)
5597 goto out;
5598
5599 /* Sequenced packets can come disconnected. If so we report the
5600 * problem.
5601 */
5602 error = -ENOTCONN;
5603
5604 /* Is there a good reason to think that we may receive some data? */
5605 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5606 goto out;
5607
5608 /* Handle signals. */
5609 if (signal_pending(current))
5610 goto interrupted;
5611
5612 /* Let another process have a go. Since we are going to sleep
5613 * anyway. Note: This may cause odd behaviors if the message
5614 * does not fit in the user's buffer, but this seems to be the
5615 * only way to honor MSG_DONTWAIT realistically.
5616 */
5617 sctp_release_sock(sk);
5618 *timeo_p = schedule_timeout(*timeo_p);
5619 sctp_lock_sock(sk);
5620
5621 ready:
5622 finish_wait(sk->sk_sleep, &wait);
5623 return 0;
5624
5625 interrupted:
5626 error = sock_intr_errno(*timeo_p);
5627
5628 out:
5629 finish_wait(sk->sk_sleep, &wait);
5630 *err = error;
5631 return error;
5632 }
5633
5634 /* Receive a datagram.
5635 * Note: This is pretty much the same routine as in core/datagram.c
5636 * with a few changes to make lksctp work.
5637 */
5638 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5639 int noblock, int *err)
5640 {
5641 int error;
5642 struct sk_buff *skb;
5643 long timeo;
5644
5645 timeo = sock_rcvtimeo(sk, noblock);
5646
5647 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5648 timeo, MAX_SCHEDULE_TIMEOUT);
5649
5650 do {
5651 /* Again only user level code calls this function,
5652 * so nothing interrupt level
5653 * will suddenly eat the receive_queue.
5654 *
5655 * Look at current nfs client by the way...
5656 * However, this function was corrent in any case. 8)
5657 */
5658 if (flags & MSG_PEEK) {
5659 spin_lock_bh(&sk->sk_receive_queue.lock);
5660 skb = skb_peek(&sk->sk_receive_queue);
5661 if (skb)
5662 atomic_inc(&skb->users);
5663 spin_unlock_bh(&sk->sk_receive_queue.lock);
5664 } else {
5665 skb = skb_dequeue(&sk->sk_receive_queue);
5666 }
5667
5668 if (skb)
5669 return skb;
5670
5671 /* Caller is allowed not to check sk->sk_err before calling. */
5672 error = sock_error(sk);
5673 if (error)
5674 goto no_packet;
5675
5676 if (sk->sk_shutdown & RCV_SHUTDOWN)
5677 break;
5678
5679 /* User doesn't want to wait. */
5680 error = -EAGAIN;
5681 if (!timeo)
5682 goto no_packet;
5683 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5684
5685 return NULL;
5686
5687 no_packet:
5688 *err = error;
5689 return NULL;
5690 }
5691
5692 /* If sndbuf has changed, wake up per association sndbuf waiters. */
5693 static void __sctp_write_space(struct sctp_association *asoc)
5694 {
5695 struct sock *sk = asoc->base.sk;
5696 struct socket *sock = sk->sk_socket;
5697
5698 if ((sctp_wspace(asoc) > 0) && sock) {
5699 if (waitqueue_active(&asoc->wait))
5700 wake_up_interruptible(&asoc->wait);
5701
5702 if (sctp_writeable(sk)) {
5703 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5704 wake_up_interruptible(sk->sk_sleep);
5705
5706 /* Note that we try to include the Async I/O support
5707 * here by modeling from the current TCP/UDP code.
5708 * We have not tested with it yet.
5709 */
5710 if (sock->fasync_list &&
5711 !(sk->sk_shutdown & SEND_SHUTDOWN))
5712 sock_wake_async(sock, 2, POLL_OUT);
5713 }
5714 }
5715 }
5716
5717 /* Do accounting for the sndbuf space.
5718 * Decrement the used sndbuf space of the corresponding association by the
5719 * data size which was just transmitted(freed).
5720 */
5721 static void sctp_wfree(struct sk_buff *skb)
5722 {
5723 struct sctp_association *asoc;
5724 struct sctp_chunk *chunk;
5725 struct sock *sk;
5726
5727 /* Get the saved chunk pointer. */
5728 chunk = *((struct sctp_chunk **)(skb->cb));
5729 asoc = chunk->asoc;
5730 sk = asoc->base.sk;
5731 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5732 sizeof(struct sk_buff) +
5733 sizeof(struct sctp_chunk);
5734
5735 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5736
5737 sock_wfree(skb);
5738 __sctp_write_space(asoc);
5739
5740 sctp_association_put(asoc);
5741 }
5742
5743 /* Do accounting for the receive space on the socket.
5744 * Accounting for the association is done in ulpevent.c
5745 * We set this as a destructor for the cloned data skbs so that
5746 * accounting is done at the correct time.
5747 */
5748 void sctp_sock_rfree(struct sk_buff *skb)
5749 {
5750 struct sock *sk = skb->sk;
5751 struct sctp_ulpevent *event = sctp_skb2event(skb);
5752
5753 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5754 }
5755
5756
5757 /* Helper function to wait for space in the sndbuf. */
5758 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5759 size_t msg_len)
5760 {
5761 struct sock *sk = asoc->base.sk;
5762 int err = 0;
5763 long current_timeo = *timeo_p;
5764 DEFINE_WAIT(wait);
5765
5766 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5767 asoc, (long)(*timeo_p), msg_len);
5768
5769 /* Increment the association's refcnt. */
5770 sctp_association_hold(asoc);
5771
5772 /* Wait on the association specific sndbuf space. */
5773 for (;;) {
5774 prepare_to_wait_exclusive(&asoc->wait, &wait,
5775 TASK_INTERRUPTIBLE);
5776 if (!*timeo_p)
5777 goto do_nonblock;
5778 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5779 asoc->base.dead)
5780 goto do_error;
5781 if (signal_pending(current))
5782 goto do_interrupted;
5783 if (msg_len <= sctp_wspace(asoc))
5784 break;
5785
5786 /* Let another process have a go. Since we are going
5787 * to sleep anyway.
5788 */
5789 sctp_release_sock(sk);
5790 current_timeo = schedule_timeout(current_timeo);
5791 BUG_ON(sk != asoc->base.sk);
5792 sctp_lock_sock(sk);
5793
5794 *timeo_p = current_timeo;
5795 }
5796
5797 out:
5798 finish_wait(&asoc->wait, &wait);
5799
5800 /* Release the association's refcnt. */
5801 sctp_association_put(asoc);
5802
5803 return err;
5804
5805 do_error:
5806 err = -EPIPE;
5807 goto out;
5808
5809 do_interrupted:
5810 err = sock_intr_errno(*timeo_p);
5811 goto out;
5812
5813 do_nonblock:
5814 err = -EAGAIN;
5815 goto out;
5816 }
5817
5818 /* If socket sndbuf has changed, wake up all per association waiters. */
5819 void sctp_write_space(struct sock *sk)
5820 {
5821 struct sctp_association *asoc;
5822 struct list_head *pos;
5823
5824 /* Wake up the tasks in each wait queue. */
5825 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5826 asoc = list_entry(pos, struct sctp_association, asocs);
5827 __sctp_write_space(asoc);
5828 }
5829 }
5830
5831 /* Is there any sndbuf space available on the socket?
5832 *
5833 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5834 * associations on the same socket. For a UDP-style socket with
5835 * multiple associations, it is possible for it to be "unwriteable"
5836 * prematurely. I assume that this is acceptable because
5837 * a premature "unwriteable" is better than an accidental "writeable" which
5838 * would cause an unwanted block under certain circumstances. For the 1-1
5839 * UDP-style sockets or TCP-style sockets, this code should work.
5840 * - Daisy
5841 */
5842 static int sctp_writeable(struct sock *sk)
5843 {
5844 int amt = 0;
5845
5846 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5847 if (amt < 0)
5848 amt = 0;
5849 return amt;
5850 }
5851
5852 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5853 * returns immediately with EINPROGRESS.
5854 */
5855 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5856 {
5857 struct sock *sk = asoc->base.sk;
5858 int err = 0;
5859 long current_timeo = *timeo_p;
5860 DEFINE_WAIT(wait);
5861
5862 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5863 (long)(*timeo_p));
5864
5865 /* Increment the association's refcnt. */
5866 sctp_association_hold(asoc);
5867
5868 for (;;) {
5869 prepare_to_wait_exclusive(&asoc->wait, &wait,
5870 TASK_INTERRUPTIBLE);
5871 if (!*timeo_p)
5872 goto do_nonblock;
5873 if (sk->sk_shutdown & RCV_SHUTDOWN)
5874 break;
5875 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5876 asoc->base.dead)
5877 goto do_error;
5878 if (signal_pending(current))
5879 goto do_interrupted;
5880
5881 if (sctp_state(asoc, ESTABLISHED))
5882 break;
5883
5884 /* Let another process have a go. Since we are going
5885 * to sleep anyway.
5886 */
5887 sctp_release_sock(sk);
5888 current_timeo = schedule_timeout(current_timeo);
5889 sctp_lock_sock(sk);
5890
5891 *timeo_p = current_timeo;
5892 }
5893
5894 out:
5895 finish_wait(&asoc->wait, &wait);
5896
5897 /* Release the association's refcnt. */
5898 sctp_association_put(asoc);
5899
5900 return err;
5901
5902 do_error:
5903 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5904 err = -ETIMEDOUT;
5905 else
5906 err = -ECONNREFUSED;
5907 goto out;
5908
5909 do_interrupted:
5910 err = sock_intr_errno(*timeo_p);
5911 goto out;
5912
5913 do_nonblock:
5914 err = -EINPROGRESS;
5915 goto out;
5916 }
5917
5918 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5919 {
5920 struct sctp_endpoint *ep;
5921 int err = 0;
5922 DEFINE_WAIT(wait);
5923
5924 ep = sctp_sk(sk)->ep;
5925
5926
5927 for (;;) {
5928 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5929 TASK_INTERRUPTIBLE);
5930
5931 if (list_empty(&ep->asocs)) {
5932 sctp_release_sock(sk);
5933 timeo = schedule_timeout(timeo);
5934 sctp_lock_sock(sk);
5935 }
5936
5937 err = -EINVAL;
5938 if (!sctp_sstate(sk, LISTENING))
5939 break;
5940
5941 err = 0;
5942 if (!list_empty(&ep->asocs))
5943 break;
5944
5945 err = sock_intr_errno(timeo);
5946 if (signal_pending(current))
5947 break;
5948
5949 err = -EAGAIN;
5950 if (!timeo)
5951 break;
5952 }
5953
5954 finish_wait(sk->sk_sleep, &wait);
5955
5956 return err;
5957 }
5958
5959 void sctp_wait_for_close(struct sock *sk, long timeout)
5960 {
5961 DEFINE_WAIT(wait);
5962
5963 do {
5964 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5965 if (list_empty(&sctp_sk(sk)->ep->asocs))
5966 break;
5967 sctp_release_sock(sk);
5968 timeout = schedule_timeout(timeout);
5969 sctp_lock_sock(sk);
5970 } while (!signal_pending(current) && timeout);
5971
5972 finish_wait(sk->sk_sleep, &wait);
5973 }
5974
5975 static void sctp_sock_rfree_frag(struct sk_buff *skb)
5976 {
5977 struct sk_buff *frag;
5978
5979 if (!skb->data_len)
5980 goto done;
5981
5982 /* Don't forget the fragments. */
5983 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5984 sctp_sock_rfree_frag(frag);
5985
5986 done:
5987 sctp_sock_rfree(skb);
5988 }
5989
5990 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5991 {
5992 struct sk_buff *frag;
5993
5994 if (!skb->data_len)
5995 goto done;
5996
5997 /* Don't forget the fragments. */
5998 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5999 sctp_skb_set_owner_r_frag(frag, sk);
6000
6001 done:
6002 sctp_skb_set_owner_r(skb, sk);
6003 }
6004
6005 /* Populate the fields of the newsk from the oldsk and migrate the assoc
6006 * and its messages to the newsk.
6007 */
6008 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6009 struct sctp_association *assoc,
6010 sctp_socket_type_t type)
6011 {
6012 struct sctp_sock *oldsp = sctp_sk(oldsk);
6013 struct sctp_sock *newsp = sctp_sk(newsk);
6014 struct sctp_bind_bucket *pp; /* hash list port iterator */
6015 struct sctp_endpoint *newep = newsp->ep;
6016 struct sk_buff *skb, *tmp;
6017 struct sctp_ulpevent *event;
6018 int flags = 0;
6019
6020 /* Migrate socket buffer sizes and all the socket level options to the
6021 * new socket.
6022 */
6023 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6024 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6025 /* Brute force copy old sctp opt. */
6026 inet_sk_copy_descendant(newsk, oldsk);
6027
6028 /* Restore the ep value that was overwritten with the above structure
6029 * copy.
6030 */
6031 newsp->ep = newep;
6032 newsp->hmac = NULL;
6033
6034 /* Hook this new socket in to the bind_hash list. */
6035 pp = sctp_sk(oldsk)->bind_hash;
6036 sk_add_bind_node(newsk, &pp->owner);
6037 sctp_sk(newsk)->bind_hash = pp;
6038 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6039
6040 /* Copy the bind_addr list from the original endpoint to the new
6041 * endpoint so that we can handle restarts properly
6042 */
6043 if (PF_INET6 == assoc->base.sk->sk_family)
6044 flags = SCTP_ADDR6_ALLOWED;
6045 if (assoc->peer.ipv4_address)
6046 flags |= SCTP_ADDR4_PEERSUPP;
6047 if (assoc->peer.ipv6_address)
6048 flags |= SCTP_ADDR6_PEERSUPP;
6049 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6050 &oldsp->ep->base.bind_addr,
6051 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6052
6053 /* Move any messages in the old socket's receive queue that are for the
6054 * peeled off association to the new socket's receive queue.
6055 */
6056 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6057 event = sctp_skb2event(skb);
6058 if (event->asoc == assoc) {
6059 sctp_sock_rfree_frag(skb);
6060 __skb_unlink(skb, &oldsk->sk_receive_queue);
6061 __skb_queue_tail(&newsk->sk_receive_queue, skb);
6062 sctp_skb_set_owner_r_frag(skb, newsk);
6063 }
6064 }
6065
6066 /* Clean up any messages pending delivery due to partial
6067 * delivery. Three cases:
6068 * 1) No partial deliver; no work.
6069 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6070 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6071 */
6072 skb_queue_head_init(&newsp->pd_lobby);
6073 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
6074
6075 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
6076 struct sk_buff_head *queue;
6077
6078 /* Decide which queue to move pd_lobby skbs to. */
6079 if (assoc->ulpq.pd_mode) {
6080 queue = &newsp->pd_lobby;
6081 } else
6082 queue = &newsk->sk_receive_queue;
6083
6084 /* Walk through the pd_lobby, looking for skbs that
6085 * need moved to the new socket.
6086 */
6087 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6088 event = sctp_skb2event(skb);
6089 if (event->asoc == assoc) {
6090 sctp_sock_rfree_frag(skb);
6091 __skb_unlink(skb, &oldsp->pd_lobby);
6092 __skb_queue_tail(queue, skb);
6093 sctp_skb_set_owner_r_frag(skb, newsk);
6094 }
6095 }
6096
6097 /* Clear up any skbs waiting for the partial
6098 * delivery to finish.
6099 */
6100 if (assoc->ulpq.pd_mode)
6101 sctp_clear_pd(oldsk, NULL);
6102
6103 }
6104
6105 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6106 sctp_sock_rfree_frag(skb);
6107 sctp_skb_set_owner_r_frag(skb, newsk);
6108 }
6109
6110 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6111 sctp_sock_rfree_frag(skb);
6112 sctp_skb_set_owner_r_frag(skb, newsk);
6113 }
6114
6115 /* Set the type of socket to indicate that it is peeled off from the
6116 * original UDP-style socket or created with the accept() call on a
6117 * TCP-style socket..
6118 */
6119 newsp->type = type;
6120
6121 /* Mark the new socket "in-use" by the user so that any packets
6122 * that may arrive on the association after we've moved it are
6123 * queued to the backlog. This prevents a potential race between
6124 * backlog processing on the old socket and new-packet processing
6125 * on the new socket.
6126 *
6127 * The caller has just allocated newsk so we can guarantee that other
6128 * paths won't try to lock it and then oldsk.
6129 */
6130 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
6131 sctp_assoc_migrate(assoc, newsk);
6132
6133 /* If the association on the newsk is already closed before accept()
6134 * is called, set RCV_SHUTDOWN flag.
6135 */
6136 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6137 newsk->sk_shutdown |= RCV_SHUTDOWN;
6138
6139 newsk->sk_state = SCTP_SS_ESTABLISHED;
6140 sctp_release_sock(newsk);
6141 }
6142
6143 /* This proto struct describes the ULP interface for SCTP. */
6144 struct proto sctp_prot = {
6145 .name = "SCTP",
6146 .owner = THIS_MODULE,
6147 .close = sctp_close,
6148 .connect = sctp_connect,
6149 .disconnect = sctp_disconnect,
6150 .accept = sctp_accept,
6151 .ioctl = sctp_ioctl,
6152 .init = sctp_init_sock,
6153 .destroy = sctp_destroy_sock,
6154 .shutdown = sctp_shutdown,
6155 .setsockopt = sctp_setsockopt,
6156 .getsockopt = sctp_getsockopt,
6157 .sendmsg = sctp_sendmsg,
6158 .recvmsg = sctp_recvmsg,
6159 .bind = sctp_bind,
6160 .backlog_rcv = sctp_backlog_rcv,
6161 .hash = sctp_hash,
6162 .unhash = sctp_unhash,
6163 .get_port = sctp_get_port,
6164 .obj_size = sizeof(struct sctp_sock),
6165 };
6166
6167 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6168 struct proto sctpv6_prot = {
6169 .name = "SCTPv6",
6170 .owner = THIS_MODULE,
6171 .close = sctp_close,
6172 .connect = sctp_connect,
6173 .disconnect = sctp_disconnect,
6174 .accept = sctp_accept,
6175 .ioctl = sctp_ioctl,
6176 .init = sctp_init_sock,
6177 .destroy = sctp_destroy_sock,
6178 .shutdown = sctp_shutdown,
6179 .setsockopt = sctp_setsockopt,
6180 .getsockopt = sctp_getsockopt,
6181 .sendmsg = sctp_sendmsg,
6182 .recvmsg = sctp_recvmsg,
6183 .bind = sctp_bind,
6184 .backlog_rcv = sctp_backlog_rcv,
6185 .hash = sctp_hash,
6186 .unhash = sctp_unhash,
6187 .get_port = sctp_get_port,
6188 .obj_size = sizeof(struct sctp6_sock),
6189 };
6190 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */