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