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