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