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