Merge master.kernel.org:/home/rmk/linux-2.6-arm
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / sctp / socket.c
CommitLineData
1da177e4
LT
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/config.h>
61#include <linux/types.h>
62#include <linux/kernel.h>
63#include <linux/wait.h>
64#include <linux/time.h>
65#include <linux/ip.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. */
88static int sctp_writeable(struct sock *sk);
89static void sctp_wfree(struct sk_buff *skb);
90static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94static int sctp_wait_for_accept(struct sock *sk, long timeo);
95static void sctp_wait_for_close(struct sock *sk, long timeo);
96static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105static int sctp_autobind(struct sock *sk);
106static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
109
110extern kmem_cache_t *sctp_bucket_cachep;
111
112/* Get the sndbuf space available at the time on the association. */
113static inline int sctp_wspace(struct sctp_association *asoc)
114{
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
117
4eb701df
NH
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
1da177e4
LT
126 if (amt < 0)
127 amt = 0;
4eb701df 128
1da177e4
LT
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 */
141static 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
4eb701df
NH
149 skb_set_owner_w(chunk->skb, sk);
150
1da177e4
LT
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
4eb701df
NH
155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
158
159 sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
160 sizeof(struct sk_buff) +
161 sizeof(struct sctp_chunk);
162
163 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
1da177e4
LT
164}
165
166/* Verify that this is a valid address. */
167static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
168 int len)
169{
170 struct sctp_af *af;
171
172 /* Verify basic sockaddr. */
173 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
174 if (!af)
175 return -EINVAL;
176
177 /* Is this a valid SCTP address? */
178 if (!af->addr_valid(addr, sctp_sk(sk)))
179 return -EINVAL;
180
181 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
182 return -EINVAL;
183
184 return 0;
185}
186
187/* Look up the association by its id. If this is not a UDP-style
188 * socket, the ID field is always ignored.
189 */
190struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
191{
192 struct sctp_association *asoc = NULL;
193
194 /* If this is not a UDP-style socket, assoc id should be ignored. */
195 if (!sctp_style(sk, UDP)) {
196 /* Return NULL if the socket state is not ESTABLISHED. It
197 * could be a TCP-style listening socket or a socket which
198 * hasn't yet called connect() to establish an association.
199 */
200 if (!sctp_sstate(sk, ESTABLISHED))
201 return NULL;
202
203 /* Get the first and the only association from the list. */
204 if (!list_empty(&sctp_sk(sk)->ep->asocs))
205 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
206 struct sctp_association, asocs);
207 return asoc;
208 }
209
210 /* Otherwise this is a UDP-style socket. */
211 if (!id || (id == (sctp_assoc_t)-1))
212 return NULL;
213
214 spin_lock_bh(&sctp_assocs_id_lock);
215 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
216 spin_unlock_bh(&sctp_assocs_id_lock);
217
218 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
219 return NULL;
220
221 return asoc;
222}
223
224/* Look up the transport from an address and an assoc id. If both address and
225 * id are specified, the associations matching the address and the id should be
226 * the same.
227 */
228static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
229 struct sockaddr_storage *addr,
230 sctp_assoc_t id)
231{
232 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
233 struct sctp_transport *transport;
234 union sctp_addr *laddr = (union sctp_addr *)addr;
235
236 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
237 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
238 (union sctp_addr *)addr,
239 &transport);
240 laddr->v4.sin_port = htons(laddr->v4.sin_port);
241
242 if (!addr_asoc)
243 return NULL;
244
245 id_asoc = sctp_id2assoc(sk, id);
246 if (id_asoc && (id_asoc != addr_asoc))
247 return NULL;
248
249 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
250 (union sctp_addr *)addr);
251
252 return transport;
253}
254
255/* API 3.1.2 bind() - UDP Style Syntax
256 * The syntax of bind() is,
257 *
258 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
259 *
260 * sd - the socket descriptor returned by socket().
261 * addr - the address structure (struct sockaddr_in or struct
262 * sockaddr_in6 [RFC 2553]),
263 * addr_len - the size of the address structure.
264 */
265SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
266{
267 int retval = 0;
268
269 sctp_lock_sock(sk);
270
271 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
272 sk, uaddr, addr_len);
273
274 /* Disallow binding twice. */
275 if (!sctp_sk(sk)->ep->base.bind_addr.port)
276 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
277 addr_len);
278 else
279 retval = -EINVAL;
280
281 sctp_release_sock(sk);
282
283 return retval;
284}
285
286static long sctp_get_port_local(struct sock *, union sctp_addr *);
287
288/* Verify this is a valid sockaddr. */
289static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
290 union sctp_addr *addr, int len)
291{
292 struct sctp_af *af;
293
294 /* Check minimum size. */
295 if (len < sizeof (struct sockaddr))
296 return NULL;
297
298 /* Does this PF support this AF? */
299 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
300 return NULL;
301
302 /* If we get this far, af is valid. */
303 af = sctp_get_af_specific(addr->sa.sa_family);
304
305 if (len < af->sockaddr_len)
306 return NULL;
307
308 return af;
309}
310
311/* Bind a local address either to an endpoint or to an association. */
312SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
313{
314 struct sctp_sock *sp = sctp_sk(sk);
315 struct sctp_endpoint *ep = sp->ep;
316 struct sctp_bind_addr *bp = &ep->base.bind_addr;
317 struct sctp_af *af;
318 unsigned short snum;
319 int ret = 0;
320
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
322 sk, addr, len);
323
324 /* Common sockaddr verification. */
325 af = sctp_sockaddr_af(sp, addr, len);
326 if (!af)
327 return -EINVAL;
328
329 /* PF specific bind() address verification. */
330 if (!sp->pf->bind_verify(sp, addr))
331 return -EADDRNOTAVAIL;
332
333 snum= ntohs(addr->v4.sin_port);
334
335 SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
336 bp->port, snum);
337
338 /* We must either be unbound, or bind to the same port. */
339 if (bp->port && (snum != bp->port)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum, bp->port);
343 return -EINVAL;
344 }
345
346 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
347 return -EACCES;
348
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
351 * detection.
352 */
353 if ((ret = sctp_get_port_local(sk, addr))) {
354 if (ret == (long) sk) {
355 /* This endpoint has a conflicting address. */
356 return -EINVAL;
357 } else {
358 return -EADDRINUSE;
359 }
360 }
361
362 /* Refresh ephemeral port. */
363 if (!bp->port)
364 bp->port = inet_sk(sk)->num;
365
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep->base.addr_lock);
369
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr->v4.sin_port = ntohs(addr->v4.sin_port);
372 ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
373 addr->v4.sin_port = htons(addr->v4.sin_port);
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 */
396static 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 __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
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
417out:
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 */
433int 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
461err_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 */
483static 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 void *addr_buf;
495 struct sctp_af *af;
496 struct list_head *pos;
497 struct list_head *p;
498 int i;
499 int retval = 0;
500
501 if (!sctp_addip_enable)
502 return retval;
503
504 sp = sctp_sk(sk);
505 ep = sp->ep;
506
507 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
508 __FUNCTION__, sk, addrs, addrcnt);
509
510 list_for_each(pos, &ep->asocs) {
511 asoc = list_entry(pos, struct sctp_association, asocs);
512
513 if (!asoc->peer.asconf_capable)
514 continue;
515
516 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
517 continue;
518
519 if (!sctp_state(asoc, ESTABLISHED))
520 continue;
521
522 /* Check if any address in the packed array of addresses is
523 * in the bind address list of the association. If so,
524 * do not send the asconf chunk to its peer, but continue with
525 * other associations.
526 */
527 addr_buf = addrs;
528 for (i = 0; i < addrcnt; i++) {
529 addr = (union sctp_addr *)addr_buf;
530 af = sctp_get_af_specific(addr->v4.sin_family);
531 if (!af) {
532 retval = -EINVAL;
533 goto out;
534 }
535
536 if (sctp_assoc_lookup_laddr(asoc, addr))
537 break;
538
539 addr_buf += af->sockaddr_len;
540 }
541 if (i < addrcnt)
542 continue;
543
544 /* Use the first address in bind addr list of association as
545 * Address Parameter of ASCONF CHUNK.
546 */
547 sctp_read_lock(&asoc->base.addr_lock);
548 bp = &asoc->base.bind_addr;
549 p = bp->address_list.next;
550 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
551 sctp_read_unlock(&asoc->base.addr_lock);
552
553 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
554 addrcnt, SCTP_PARAM_ADD_IP);
555 if (!chunk) {
556 retval = -ENOMEM;
557 goto out;
558 }
559
560 retval = sctp_send_asconf(asoc, chunk);
561
562 /* FIXME: After sending the add address ASCONF chunk, we
563 * cannot append the address to the association's binding
564 * address list, because the new address may be used as the
565 * source of a message sent to the peer before the ASCONF
566 * chunk is received by the peer. So we should wait until
567 * ASCONF_ACK is received.
568 */
569 }
570
571out:
572 return retval;
573}
574
575/* Remove a list of addresses from bind addresses list. Do not remove the
576 * last address.
577 *
578 * Basically run through each address specified in the addrs/addrcnt
579 * array/length pair, determine if it is IPv6 or IPv4 and call
580 * sctp_del_bind() on it.
581 *
582 * If any of them fails, then the operation will be reversed and the
583 * ones that were removed will be added back.
584 *
585 * At least one address has to be left; if only one address is
586 * available, the operation will return -EBUSY.
587 *
588 * Only sctp_setsockopt_bindx() is supposed to call this function.
589 */
590int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
591{
592 struct sctp_sock *sp = sctp_sk(sk);
593 struct sctp_endpoint *ep = sp->ep;
594 int cnt;
595 struct sctp_bind_addr *bp = &ep->base.bind_addr;
596 int retval = 0;
597 union sctp_addr saveaddr;
598 void *addr_buf;
599 struct sockaddr *sa_addr;
600 struct sctp_af *af;
601
602 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
603 sk, addrs, addrcnt);
604
605 addr_buf = addrs;
606 for (cnt = 0; cnt < addrcnt; cnt++) {
607 /* If the bind address list is empty or if there is only one
608 * bind address, there is nothing more to be removed (we need
609 * at least one address here).
610 */
611 if (list_empty(&bp->address_list) ||
612 (sctp_list_single_entry(&bp->address_list))) {
613 retval = -EBUSY;
614 goto err_bindx_rem;
615 }
616
617 /* The list may contain either IPv4 or IPv6 address;
618 * determine the address length to copy the address to
619 * saveaddr.
620 */
621 sa_addr = (struct sockaddr *)addr_buf;
622 af = sctp_get_af_specific(sa_addr->sa_family);
623 if (!af) {
624 retval = -EINVAL;
625 goto err_bindx_rem;
626 }
627 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
628 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
629 if (saveaddr.v4.sin_port != bp->port) {
630 retval = -EINVAL;
631 goto err_bindx_rem;
632 }
633
634 /* FIXME - There is probably a need to check if sk->sk_saddr and
635 * sk->sk_rcv_addr are currently set to one of the addresses to
636 * be removed. This is something which needs to be looked into
637 * when we are fixing the outstanding issues with multi-homing
638 * socket routing and failover schemes. Refer to comments in
639 * sctp_do_bind(). -daisy
640 */
641 sctp_local_bh_disable();
642 sctp_write_lock(&ep->base.addr_lock);
643
644 retval = sctp_del_bind_addr(bp, &saveaddr);
645
646 sctp_write_unlock(&ep->base.addr_lock);
647 sctp_local_bh_enable();
648
649 addr_buf += af->sockaddr_len;
650err_bindx_rem:
651 if (retval < 0) {
652 /* Failed. Add the ones that has been removed back */
653 if (cnt > 0)
654 sctp_bindx_add(sk, addrs, cnt);
655 return retval;
656 }
657 }
658
659 return retval;
660}
661
662/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
663 * the associations that are part of the endpoint indicating that a list of
664 * local addresses are removed from the endpoint.
665 *
666 * If any of the addresses is already in the bind address list of the
667 * association, we do not send the chunk for that association. But it will not
668 * affect other associations.
669 *
670 * Only sctp_setsockopt_bindx() is supposed to call this function.
671 */
672static int sctp_send_asconf_del_ip(struct sock *sk,
673 struct sockaddr *addrs,
674 int addrcnt)
675{
676 struct sctp_sock *sp;
677 struct sctp_endpoint *ep;
678 struct sctp_association *asoc;
679 struct sctp_bind_addr *bp;
680 struct sctp_chunk *chunk;
681 union sctp_addr *laddr;
682 void *addr_buf;
683 struct sctp_af *af;
684 struct list_head *pos;
685 int i;
686 int retval = 0;
687
688 if (!sctp_addip_enable)
689 return retval;
690
691 sp = sctp_sk(sk);
692 ep = sp->ep;
693
694 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
695 __FUNCTION__, sk, addrs, addrcnt);
696
697 list_for_each(pos, &ep->asocs) {
698 asoc = list_entry(pos, struct sctp_association, asocs);
699
700 if (!asoc->peer.asconf_capable)
701 continue;
702
703 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
704 continue;
705
706 if (!sctp_state(asoc, ESTABLISHED))
707 continue;
708
709 /* Check if any address in the packed array of addresses is
710 * not present in the bind address list of the association.
711 * If so, do not send the asconf chunk to its peer, but
712 * continue with other associations.
713 */
714 addr_buf = addrs;
715 for (i = 0; i < addrcnt; i++) {
716 laddr = (union sctp_addr *)addr_buf;
717 af = sctp_get_af_specific(laddr->v4.sin_family);
718 if (!af) {
719 retval = -EINVAL;
720 goto out;
721 }
722
723 if (!sctp_assoc_lookup_laddr(asoc, laddr))
724 break;
725
726 addr_buf += af->sockaddr_len;
727 }
728 if (i < addrcnt)
729 continue;
730
731 /* Find one address in the association's bind address list
732 * that is not in the packed array of addresses. This is to
733 * make sure that we do not delete all the addresses in the
734 * association.
735 */
736 sctp_read_lock(&asoc->base.addr_lock);
737 bp = &asoc->base.bind_addr;
738 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
739 addrcnt, sp);
740 sctp_read_unlock(&asoc->base.addr_lock);
741 if (!laddr)
742 continue;
743
744 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
745 SCTP_PARAM_DEL_IP);
746 if (!chunk) {
747 retval = -ENOMEM;
748 goto out;
749 }
750
751 retval = sctp_send_asconf(asoc, chunk);
752
753 /* FIXME: After sending the delete address ASCONF chunk, we
754 * cannot remove the addresses from the association's bind
755 * address list, because there maybe some packet send to
756 * the delete addresses, so we should wait until ASCONF_ACK
757 * packet is received.
758 */
759 }
760out:
761 return retval;
762}
763
764/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
765 *
766 * API 8.1
767 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
768 * int flags);
769 *
770 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
771 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
772 * or IPv6 addresses.
773 *
774 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
775 * Section 3.1.2 for this usage.
776 *
777 * addrs is a pointer to an array of one or more socket addresses. Each
778 * address is contained in its appropriate structure (i.e. struct
779 * sockaddr_in or struct sockaddr_in6) the family of the address type
780 * must be used to distengish the address length (note that this
781 * representation is termed a "packed array" of addresses). The caller
782 * specifies the number of addresses in the array with addrcnt.
783 *
784 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
785 * -1, and sets errno to the appropriate error code.
786 *
787 * For SCTP, the port given in each socket address must be the same, or
788 * sctp_bindx() will fail, setting errno to EINVAL.
789 *
790 * The flags parameter is formed from the bitwise OR of zero or more of
791 * the following currently defined flags:
792 *
793 * SCTP_BINDX_ADD_ADDR
794 *
795 * SCTP_BINDX_REM_ADDR
796 *
797 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
798 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
799 * addresses from the association. The two flags are mutually exclusive;
800 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
801 * not remove all addresses from an association; sctp_bindx() will
802 * reject such an attempt with EINVAL.
803 *
804 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
805 * additional addresses with an endpoint after calling bind(). Or use
806 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
807 * socket is associated with so that no new association accepted will be
808 * associated with those addresses. If the endpoint supports dynamic
809 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
810 * endpoint to send the appropriate message to the peer to change the
811 * peers address lists.
812 *
813 * Adding and removing addresses from a connected association is
814 * optional functionality. Implementations that do not support this
815 * functionality should return EOPNOTSUPP.
816 *
817 * Basically do nothing but copying the addresses from user to kernel
818 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
819 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
820 *
821 * We don't use copy_from_user() for optimization: we first do the
822 * sanity checks (buffer size -fast- and access check-healthy
823 * pointer); if all of those succeed, then we can alloc the memory
824 * (expensive operation) needed to copy the data to kernel. Then we do
825 * the copying without checking the user space area
826 * (__copy_from_user()).
827 *
828 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
829 * it.
830 *
831 * sk The sk of the socket
832 * addrs The pointer to the addresses in user land
833 * addrssize Size of the addrs buffer
834 * op Operation to perform (add or remove, see the flags of
835 * sctp_bindx)
836 *
837 * Returns 0 if ok, <0 errno code on error.
838 */
839SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
840 struct sockaddr __user *addrs,
841 int addrs_size, int op)
842{
843 struct sockaddr *kaddrs;
844 int err;
845 int addrcnt = 0;
846 int walk_size = 0;
847 struct sockaddr *sa_addr;
848 void *addr_buf;
849 struct sctp_af *af;
850
851 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
852 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
853
854 if (unlikely(addrs_size <= 0))
855 return -EINVAL;
856
857 /* Check the user passed a healthy pointer. */
858 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
859 return -EFAULT;
860
861 /* Alloc space for the address array in kernel memory. */
862 kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
863 if (unlikely(!kaddrs))
864 return -ENOMEM;
865
866 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
867 kfree(kaddrs);
868 return -EFAULT;
869 }
870
871 /* Walk through the addrs buffer and count the number of addresses. */
872 addr_buf = kaddrs;
873 while (walk_size < addrs_size) {
874 sa_addr = (struct sockaddr *)addr_buf;
875 af = sctp_get_af_specific(sa_addr->sa_family);
876
877 /* If the address family is not supported or if this address
878 * causes the address buffer to overflow return EINVAL.
879 */
880 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
881 kfree(kaddrs);
882 return -EINVAL;
883 }
884 addrcnt++;
885 addr_buf += af->sockaddr_len;
886 walk_size += af->sockaddr_len;
887 }
888
889 /* Do the work. */
890 switch (op) {
891 case SCTP_BINDX_ADD_ADDR:
892 err = sctp_bindx_add(sk, kaddrs, addrcnt);
893 if (err)
894 goto out;
895 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
896 break;
897
898 case SCTP_BINDX_REM_ADDR:
899 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
900 if (err)
901 goto out;
902 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
903 break;
904
905 default:
906 err = -EINVAL;
907 break;
908 };
909
910out:
911 kfree(kaddrs);
912
913 return err;
914}
915
916/* API 3.1.4 close() - UDP Style Syntax
917 * Applications use close() to perform graceful shutdown (as described in
918 * Section 10.1 of [SCTP]) on ALL the associations currently represented
919 * by a UDP-style socket.
920 *
921 * The syntax is
922 *
923 * ret = close(int sd);
924 *
925 * sd - the socket descriptor of the associations to be closed.
926 *
927 * To gracefully shutdown a specific association represented by the
928 * UDP-style socket, an application should use the sendmsg() call,
929 * passing no user data, but including the appropriate flag in the
930 * ancillary data (see Section xxxx).
931 *
932 * If sd in the close() call is a branched-off socket representing only
933 * one association, the shutdown is performed on that association only.
934 *
935 * 4.1.6 close() - TCP Style Syntax
936 *
937 * Applications use close() to gracefully close down an association.
938 *
939 * The syntax is:
940 *
941 * int close(int sd);
942 *
943 * sd - the socket descriptor of the association to be closed.
944 *
945 * After an application calls close() on a socket descriptor, no further
946 * socket operations will succeed on that descriptor.
947 *
948 * API 7.1.4 SO_LINGER
949 *
950 * An application using the TCP-style socket can use this option to
951 * perform the SCTP ABORT primitive. The linger option structure is:
952 *
953 * struct linger {
954 * int l_onoff; // option on/off
955 * int l_linger; // linger time
956 * };
957 *
958 * To enable the option, set l_onoff to 1. If the l_linger value is set
959 * to 0, calling close() is the same as the ABORT primitive. If the
960 * value is set to a negative value, the setsockopt() call will return
961 * an error. If the value is set to a positive value linger_time, the
962 * close() can be blocked for at most linger_time ms. If the graceful
963 * shutdown phase does not finish during this period, close() will
964 * return but the graceful shutdown phase continues in the system.
965 */
966SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
967{
968 struct sctp_endpoint *ep;
969 struct sctp_association *asoc;
970 struct list_head *pos, *temp;
971
972 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
973
974 sctp_lock_sock(sk);
975 sk->sk_shutdown = SHUTDOWN_MASK;
976
977 ep = sctp_sk(sk)->ep;
978
979 /* Walk all associations on a socket, not on an endpoint. */
980 list_for_each_safe(pos, temp, &ep->asocs) {
981 asoc = list_entry(pos, struct sctp_association, asocs);
982
983 if (sctp_style(sk, TCP)) {
984 /* A closed association can still be in the list if
985 * it belongs to a TCP-style listening socket that is
986 * not yet accepted. If so, free it. If not, send an
987 * ABORT or SHUTDOWN based on the linger options.
988 */
989 if (sctp_state(asoc, CLOSED)) {
990 sctp_unhash_established(asoc);
991 sctp_association_free(asoc);
992
993 } else if (sock_flag(sk, SOCK_LINGER) &&
994 !sk->sk_lingertime)
995 sctp_primitive_ABORT(asoc, NULL);
996 else
997 sctp_primitive_SHUTDOWN(asoc, NULL);
998 } else
999 sctp_primitive_SHUTDOWN(asoc, NULL);
1000 }
1001
1002 /* Clean up any skbs sitting on the receive queue. */
1003 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1004 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1005
1006 /* On a TCP-style socket, block for at most linger_time if set. */
1007 if (sctp_style(sk, TCP) && timeout)
1008 sctp_wait_for_close(sk, timeout);
1009
1010 /* This will run the backlog queue. */
1011 sctp_release_sock(sk);
1012
1013 /* Supposedly, no process has access to the socket, but
1014 * the net layers still may.
1015 */
1016 sctp_local_bh_disable();
1017 sctp_bh_lock_sock(sk);
1018
1019 /* Hold the sock, since sk_common_release() will put sock_put()
1020 * and we have just a little more cleanup.
1021 */
1022 sock_hold(sk);
1023 sk_common_release(sk);
1024
1025 sctp_bh_unlock_sock(sk);
1026 sctp_local_bh_enable();
1027
1028 sock_put(sk);
1029
1030 SCTP_DBG_OBJCNT_DEC(sock);
1031}
1032
1033/* Handle EPIPE error. */
1034static int sctp_error(struct sock *sk, int flags, int err)
1035{
1036 if (err == -EPIPE)
1037 err = sock_error(sk) ? : -EPIPE;
1038 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1039 send_sig(SIGPIPE, current, 0);
1040 return err;
1041}
1042
1043/* API 3.1.3 sendmsg() - UDP Style Syntax
1044 *
1045 * An application uses sendmsg() and recvmsg() calls to transmit data to
1046 * and receive data from its peer.
1047 *
1048 * ssize_t sendmsg(int socket, const struct msghdr *message,
1049 * int flags);
1050 *
1051 * socket - the socket descriptor of the endpoint.
1052 * message - pointer to the msghdr structure which contains a single
1053 * user message and possibly some ancillary data.
1054 *
1055 * See Section 5 for complete description of the data
1056 * structures.
1057 *
1058 * flags - flags sent or received with the user message, see Section
1059 * 5 for complete description of the flags.
1060 *
1061 * Note: This function could use a rewrite especially when explicit
1062 * connect support comes in.
1063 */
1064/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1065
1066SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1067
1068SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1069 struct msghdr *msg, size_t msg_len)
1070{
1071 struct sctp_sock *sp;
1072 struct sctp_endpoint *ep;
1073 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1074 struct sctp_transport *transport, *chunk_tp;
1075 struct sctp_chunk *chunk;
1076 union sctp_addr to;
1077 struct sockaddr *msg_name = NULL;
1078 struct sctp_sndrcvinfo default_sinfo = { 0 };
1079 struct sctp_sndrcvinfo *sinfo;
1080 struct sctp_initmsg *sinit;
1081 sctp_assoc_t associd = 0;
1082 sctp_cmsgs_t cmsgs = { NULL };
1083 int err;
1084 sctp_scope_t scope;
1085 long timeo;
1086 __u16 sinfo_flags = 0;
1087 struct sctp_datamsg *datamsg;
1088 struct list_head *pos;
1089 int msg_flags = msg->msg_flags;
1090
1091 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1092 sk, msg, msg_len);
1093
1094 err = 0;
1095 sp = sctp_sk(sk);
1096 ep = sp->ep;
1097
1098 SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
1099
1100 /* We cannot send a message over a TCP-style listening socket. */
1101 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1102 err = -EPIPE;
1103 goto out_nounlock;
1104 }
1105
1106 /* Parse out the SCTP CMSGs. */
1107 err = sctp_msghdr_parse(msg, &cmsgs);
1108
1109 if (err) {
1110 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1111 goto out_nounlock;
1112 }
1113
1114 /* Fetch the destination address for this packet. This
1115 * address only selects the association--it is not necessarily
1116 * the address we will send to.
1117 * For a peeled-off socket, msg_name is ignored.
1118 */
1119 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1120 int msg_namelen = msg->msg_namelen;
1121
1122 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1123 msg_namelen);
1124 if (err)
1125 return err;
1126
1127 if (msg_namelen > sizeof(to))
1128 msg_namelen = sizeof(to);
1129 memcpy(&to, msg->msg_name, msg_namelen);
1130 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1131 "0x%x:%u.\n",
1132 to.v4.sin_addr.s_addr, to.v4.sin_port);
1133
1134 to.v4.sin_port = ntohs(to.v4.sin_port);
1135 msg_name = msg->msg_name;
1136 }
1137
1138 sinfo = cmsgs.info;
1139 sinit = cmsgs.init;
1140
1141 /* Did the user specify SNDRCVINFO? */
1142 if (sinfo) {
1143 sinfo_flags = sinfo->sinfo_flags;
1144 associd = sinfo->sinfo_assoc_id;
1145 }
1146
1147 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1148 msg_len, sinfo_flags);
1149
1150 /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1151 if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1152 err = -EINVAL;
1153 goto out_nounlock;
1154 }
1155
1156 /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1157 * length messages when MSG_EOF|MSG_ABORT is not set.
1158 * If MSG_ABORT is set, the message length could be non zero with
1159 * the msg_iov set to the user abort reason.
1160 */
1161 if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1162 (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1163 err = -EINVAL;
1164 goto out_nounlock;
1165 }
1166
1167 /* If MSG_ADDR_OVER is set, there must be an address
1168 * specified in msg_name.
1169 */
1170 if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1171 err = -EINVAL;
1172 goto out_nounlock;
1173 }
1174
1175 transport = NULL;
1176
1177 SCTP_DEBUG_PRINTK("About to look up association.\n");
1178
1179 sctp_lock_sock(sk);
1180
1181 /* If a msg_name has been specified, assume this is to be used. */
1182 if (msg_name) {
1183 /* Look for a matching association on the endpoint. */
1184 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1185 if (!asoc) {
1186 /* If we could not find a matching association on the
1187 * endpoint, make sure that it is not a TCP-style
1188 * socket that already has an association or there is
1189 * no peeled-off association on another socket.
1190 */
1191 if ((sctp_style(sk, TCP) &&
1192 sctp_sstate(sk, ESTABLISHED)) ||
1193 sctp_endpoint_is_peeled_off(ep, &to)) {
1194 err = -EADDRNOTAVAIL;
1195 goto out_unlock;
1196 }
1197 }
1198 } else {
1199 asoc = sctp_id2assoc(sk, associd);
1200 if (!asoc) {
1201 err = -EPIPE;
1202 goto out_unlock;
1203 }
1204 }
1205
1206 if (asoc) {
1207 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1208
1209 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1210 * socket that has an association in CLOSED state. This can
1211 * happen when an accepted socket has an association that is
1212 * already CLOSED.
1213 */
1214 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1215 err = -EPIPE;
1216 goto out_unlock;
1217 }
1218
1219 if (sinfo_flags & MSG_EOF) {
1220 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1221 asoc);
1222 sctp_primitive_SHUTDOWN(asoc, NULL);
1223 err = 0;
1224 goto out_unlock;
1225 }
1226 if (sinfo_flags & MSG_ABORT) {
1227 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1228 sctp_primitive_ABORT(asoc, msg);
1229 err = 0;
1230 goto out_unlock;
1231 }
1232 }
1233
1234 /* Do we need to create the association? */
1235 if (!asoc) {
1236 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1237
1238 if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
1239 err = -EINVAL;
1240 goto out_unlock;
1241 }
1242
1243 /* Check for invalid stream against the stream counts,
1244 * either the default or the user specified stream counts.
1245 */
1246 if (sinfo) {
1247 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1248 /* Check against the defaults. */
1249 if (sinfo->sinfo_stream >=
1250 sp->initmsg.sinit_num_ostreams) {
1251 err = -EINVAL;
1252 goto out_unlock;
1253 }
1254 } else {
1255 /* Check against the requested. */
1256 if (sinfo->sinfo_stream >=
1257 sinit->sinit_num_ostreams) {
1258 err = -EINVAL;
1259 goto out_unlock;
1260 }
1261 }
1262 }
1263
1264 /*
1265 * API 3.1.2 bind() - UDP Style Syntax
1266 * If a bind() or sctp_bindx() is not called prior to a
1267 * sendmsg() call that initiates a new association, the
1268 * system picks an ephemeral port and will choose an address
1269 * set equivalent to binding with a wildcard address.
1270 */
1271 if (!ep->base.bind_addr.port) {
1272 if (sctp_autobind(sk)) {
1273 err = -EAGAIN;
1274 goto out_unlock;
1275 }
1276 }
1277
1278 scope = sctp_scope(&to);
1279 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1280 if (!new_asoc) {
1281 err = -ENOMEM;
1282 goto out_unlock;
1283 }
1284 asoc = new_asoc;
1285
1286 /* If the SCTP_INIT ancillary data is specified, set all
1287 * the association init values accordingly.
1288 */
1289 if (sinit) {
1290 if (sinit->sinit_num_ostreams) {
1291 asoc->c.sinit_num_ostreams =
1292 sinit->sinit_num_ostreams;
1293 }
1294 if (sinit->sinit_max_instreams) {
1295 asoc->c.sinit_max_instreams =
1296 sinit->sinit_max_instreams;
1297 }
1298 if (sinit->sinit_max_attempts) {
1299 asoc->max_init_attempts
1300 = sinit->sinit_max_attempts;
1301 }
1302 if (sinit->sinit_max_init_timeo) {
1303 asoc->max_init_timeo =
1304 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1305 }
1306 }
1307
1308 /* Prime the peer's transport structures. */
1309 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1310 if (!transport) {
1311 err = -ENOMEM;
1312 goto out_free;
1313 }
1314 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1315 if (err < 0) {
1316 err = -ENOMEM;
1317 goto out_free;
1318 }
1319 }
1320
1321 /* ASSERT: we have a valid association at this point. */
1322 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1323
1324 if (!sinfo) {
1325 /* If the user didn't specify SNDRCVINFO, make up one with
1326 * some defaults.
1327 */
1328 default_sinfo.sinfo_stream = asoc->default_stream;
1329 default_sinfo.sinfo_flags = asoc->default_flags;
1330 default_sinfo.sinfo_ppid = asoc->default_ppid;
1331 default_sinfo.sinfo_context = asoc->default_context;
1332 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1333 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1334 sinfo = &default_sinfo;
1335 }
1336
1337 /* API 7.1.7, the sndbuf size per association bounds the
1338 * maximum size of data that can be sent in a single send call.
1339 */
1340 if (msg_len > sk->sk_sndbuf) {
1341 err = -EMSGSIZE;
1342 goto out_free;
1343 }
1344
1345 /* If fragmentation is disabled and the message length exceeds the
1346 * association fragmentation point, return EMSGSIZE. The I-D
1347 * does not specify what this error is, but this looks like
1348 * a great fit.
1349 */
1350 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1351 err = -EMSGSIZE;
1352 goto out_free;
1353 }
1354
1355 if (sinfo) {
1356 /* Check for invalid stream. */
1357 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1358 err = -EINVAL;
1359 goto out_free;
1360 }
1361 }
1362
1363 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1364 if (!sctp_wspace(asoc)) {
1365 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1366 if (err)
1367 goto out_free;
1368 }
1369
1370 /* If an address is passed with the sendto/sendmsg call, it is used
1371 * to override the primary destination address in the TCP model, or
1372 * when MSG_ADDR_OVER flag is set in the UDP model.
1373 */
1374 if ((sctp_style(sk, TCP) && msg_name) ||
1375 (sinfo_flags & MSG_ADDR_OVER)) {
1376 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1377 if (!chunk_tp) {
1378 err = -EINVAL;
1379 goto out_free;
1380 }
1381 } else
1382 chunk_tp = NULL;
1383
1384 /* Auto-connect, if we aren't connected already. */
1385 if (sctp_state(asoc, CLOSED)) {
1386 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1387 if (err < 0)
1388 goto out_free;
1389 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1390 }
1391
1392 /* Break the message into multiple chunks of maximum size. */
1393 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1394 if (!datamsg) {
1395 err = -ENOMEM;
1396 goto out_free;
1397 }
1398
1399 /* Now send the (possibly) fragmented message. */
1400 list_for_each(pos, &datamsg->chunks) {
1401 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1402 sctp_datamsg_track(chunk);
1403
1404 /* Do accounting for the write space. */
1405 sctp_set_owner_w(chunk);
1406
1407 chunk->transport = chunk_tp;
1408
1409 /* Send it to the lower layers. Note: all chunks
1410 * must either fail or succeed. The lower layer
1411 * works that way today. Keep it that way or this
1412 * breaks.
1413 */
1414 err = sctp_primitive_SEND(asoc, chunk);
1415 /* Did the lower layer accept the chunk? */
1416 if (err)
1417 sctp_chunk_free(chunk);
1418 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1419 }
1420
1421 sctp_datamsg_free(datamsg);
1422 if (err)
1423 goto out_free;
1424 else
1425 err = msg_len;
1426
1427 /* If we are already past ASSOCIATE, the lower
1428 * layers are responsible for association cleanup.
1429 */
1430 goto out_unlock;
1431
1432out_free:
1433 if (new_asoc)
1434 sctp_association_free(asoc);
1435out_unlock:
1436 sctp_release_sock(sk);
1437
1438out_nounlock:
1439 return sctp_error(sk, msg_flags, err);
1440
1441#if 0
1442do_sock_err:
1443 if (msg_len)
1444 err = msg_len;
1445 else
1446 err = sock_error(sk);
1447 goto out;
1448
1449do_interrupted:
1450 if (msg_len)
1451 err = msg_len;
1452 goto out;
1453#endif /* 0 */
1454}
1455
1456/* This is an extended version of skb_pull() that removes the data from the
1457 * start of a skb even when data is spread across the list of skb's in the
1458 * frag_list. len specifies the total amount of data that needs to be removed.
1459 * when 'len' bytes could be removed from the skb, it returns 0.
1460 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1461 * could not be removed.
1462 */
1463static int sctp_skb_pull(struct sk_buff *skb, int len)
1464{
1465 struct sk_buff *list;
1466 int skb_len = skb_headlen(skb);
1467 int rlen;
1468
1469 if (len <= skb_len) {
1470 __skb_pull(skb, len);
1471 return 0;
1472 }
1473 len -= skb_len;
1474 __skb_pull(skb, skb_len);
1475
1476 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1477 rlen = sctp_skb_pull(list, len);
1478 skb->len -= (len-rlen);
1479 skb->data_len -= (len-rlen);
1480
1481 if (!rlen)
1482 return 0;
1483
1484 len = rlen;
1485 }
1486
1487 return len;
1488}
1489
1490/* API 3.1.3 recvmsg() - UDP Style Syntax
1491 *
1492 * ssize_t recvmsg(int socket, struct msghdr *message,
1493 * int flags);
1494 *
1495 * socket - the socket descriptor of the endpoint.
1496 * message - pointer to the msghdr structure which contains a single
1497 * user message and possibly some ancillary data.
1498 *
1499 * See Section 5 for complete description of the data
1500 * structures.
1501 *
1502 * flags - flags sent or received with the user message, see Section
1503 * 5 for complete description of the flags.
1504 */
1505static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1506
1507SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1508 struct msghdr *msg, size_t len, int noblock,
1509 int flags, int *addr_len)
1510{
1511 struct sctp_ulpevent *event = NULL;
1512 struct sctp_sock *sp = sctp_sk(sk);
1513 struct sk_buff *skb;
1514 int copied;
1515 int err = 0;
1516 int skb_len;
1517
1518 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1519 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1520 "len", len, "knoblauch", noblock,
1521 "flags", flags, "addr_len", addr_len);
1522
1523 sctp_lock_sock(sk);
1524
1525 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1526 err = -ENOTCONN;
1527 goto out;
1528 }
1529
1530 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1531 if (!skb)
1532 goto out;
1533
1534 /* Get the total length of the skb including any skb's in the
1535 * frag_list.
1536 */
1537 skb_len = skb->len;
1538
1539 copied = skb_len;
1540 if (copied > len)
1541 copied = len;
1542
1543 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1544
1545 event = sctp_skb2event(skb);
1546
1547 if (err)
1548 goto out_free;
1549
1550 sock_recv_timestamp(msg, sk, skb);
1551 if (sctp_ulpevent_is_notification(event)) {
1552 msg->msg_flags |= MSG_NOTIFICATION;
1553 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1554 } else {
1555 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1556 }
1557
1558 /* Check if we allow SCTP_SNDRCVINFO. */
1559 if (sp->subscribe.sctp_data_io_event)
1560 sctp_ulpevent_read_sndrcvinfo(event, msg);
1561#if 0
1562 /* FIXME: we should be calling IP/IPv6 layers. */
1563 if (sk->sk_protinfo.af_inet.cmsg_flags)
1564 ip_cmsg_recv(msg, skb);
1565#endif
1566
1567 err = copied;
1568
1569 /* If skb's length exceeds the user's buffer, update the skb and
1570 * push it back to the receive_queue so that the next call to
1571 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1572 */
1573 if (skb_len > copied) {
1574 msg->msg_flags &= ~MSG_EOR;
1575 if (flags & MSG_PEEK)
1576 goto out_free;
1577 sctp_skb_pull(skb, copied);
1578 skb_queue_head(&sk->sk_receive_queue, skb);
1579
1580 /* When only partial message is copied to the user, increase
1581 * rwnd by that amount. If all the data in the skb is read,
1582 * rwnd is updated when the event is freed.
1583 */
1584 sctp_assoc_rwnd_increase(event->asoc, copied);
1585 goto out;
1586 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1587 (event->msg_flags & MSG_EOR))
1588 msg->msg_flags |= MSG_EOR;
1589 else
1590 msg->msg_flags &= ~MSG_EOR;
1591
1592out_free:
1593 if (flags & MSG_PEEK) {
1594 /* Release the skb reference acquired after peeking the skb in
1595 * sctp_skb_recv_datagram().
1596 */
1597 kfree_skb(skb);
1598 } else {
1599 /* Free the event which includes releasing the reference to
1600 * the owner of the skb, freeing the skb and updating the
1601 * rwnd.
1602 */
1603 sctp_ulpevent_free(event);
1604 }
1605out:
1606 sctp_release_sock(sk);
1607 return err;
1608}
1609
1610/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1611 *
1612 * This option is a on/off flag. If enabled no SCTP message
1613 * fragmentation will be performed. Instead if a message being sent
1614 * exceeds the current PMTU size, the message will NOT be sent and
1615 * instead a error will be indicated to the user.
1616 */
1617static int sctp_setsockopt_disable_fragments(struct sock *sk,
1618 char __user *optval, int optlen)
1619{
1620 int val;
1621
1622 if (optlen < sizeof(int))
1623 return -EINVAL;
1624
1625 if (get_user(val, (int __user *)optval))
1626 return -EFAULT;
1627
1628 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1629
1630 return 0;
1631}
1632
1633static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1634 int optlen)
1635{
1636 if (optlen != sizeof(struct sctp_event_subscribe))
1637 return -EINVAL;
1638 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1639 return -EFAULT;
1640 return 0;
1641}
1642
1643/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1644 *
1645 * This socket option is applicable to the UDP-style socket only. When
1646 * set it will cause associations that are idle for more than the
1647 * specified number of seconds to automatically close. An association
1648 * being idle is defined an association that has NOT sent or received
1649 * user data. The special value of '0' indicates that no automatic
1650 * close of any associations should be performed. The option expects an
1651 * integer defining the number of seconds of idle time before an
1652 * association is closed.
1653 */
1654static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1655 int optlen)
1656{
1657 struct sctp_sock *sp = sctp_sk(sk);
1658
1659 /* Applicable to UDP-style socket only */
1660 if (sctp_style(sk, TCP))
1661 return -EOPNOTSUPP;
1662 if (optlen != sizeof(int))
1663 return -EINVAL;
1664 if (copy_from_user(&sp->autoclose, optval, optlen))
1665 return -EFAULT;
1666
1667 sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1668 return 0;
1669}
1670
1671/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1672 *
1673 * Applications can enable or disable heartbeats for any peer address of
1674 * an association, modify an address's heartbeat interval, force a
1675 * heartbeat to be sent immediately, and adjust the address's maximum
1676 * number of retransmissions sent before an address is considered
1677 * unreachable. The following structure is used to access and modify an
1678 * address's parameters:
1679 *
1680 * struct sctp_paddrparams {
1681 * sctp_assoc_t spp_assoc_id;
1682 * struct sockaddr_storage spp_address;
1683 * uint32_t spp_hbinterval;
1684 * uint16_t spp_pathmaxrxt;
1685 * };
1686 *
1687 * spp_assoc_id - (UDP style socket) This is filled in the application,
1688 * and identifies the association for this query.
1689 * spp_address - This specifies which address is of interest.
1690 * spp_hbinterval - This contains the value of the heartbeat interval,
1691 * in milliseconds. A value of 0, when modifying the
1692 * parameter, specifies that the heartbeat on this
1693 * address should be disabled. A value of UINT32_MAX
1694 * (4294967295), when modifying the parameter,
1695 * specifies that a heartbeat should be sent
1696 * immediately to the peer address, and the current
1697 * interval should remain unchanged.
1698 * spp_pathmaxrxt - This contains the maximum number of
1699 * retransmissions before this address shall be
1700 * considered unreachable.
1701 */
1702static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1703 char __user *optval, int optlen)
1704{
1705 struct sctp_paddrparams params;
1706 struct sctp_transport *trans;
1707 int error;
1708
1709 if (optlen != sizeof(struct sctp_paddrparams))
1710 return -EINVAL;
1711 if (copy_from_user(&params, optval, optlen))
1712 return -EFAULT;
1713
1714 /*
1715 * API 7. Socket Options (setting the default value for the endpoint)
1716 * All options that support specific settings on an association by
1717 * filling in either an association id variable or a sockaddr_storage
1718 * SHOULD also support setting of the same value for the entire endpoint
1719 * (i.e. future associations). To accomplish this the following logic is
1720 * used when setting one of these options:
1721
1722 * c) If neither the sockaddr_storage or association identification is
1723 * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
1724 * the association identification is 0, the settings are a default
1725 * and to be applied to the endpoint (all future associations).
1726 */
1727
1728 /* update default value for endpoint (all future associations) */
1729 if (!params.spp_assoc_id &&
1730 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
1731 /* Manual heartbeat on an endpoint is invalid. */
1732 if (0xffffffff == params.spp_hbinterval)
1733 return -EINVAL;
1734 else if (params.spp_hbinterval)
1735 sctp_sk(sk)->paddrparam.spp_hbinterval =
1736 params.spp_hbinterval;
1737 if (params.spp_pathmaxrxt)
1738 sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
1739 params.spp_pathmaxrxt;
1740 return 0;
1741 }
1742
1743 trans = sctp_addr_id2transport(sk, &params.spp_address,
1744 params.spp_assoc_id);
1745 if (!trans)
1746 return -EINVAL;
1747
1748 /* Applications can enable or disable heartbeats for any peer address
1749 * of an association, modify an address's heartbeat interval, force a
1750 * heartbeat to be sent immediately, and adjust the address's maximum
1751 * number of retransmissions sent before an address is considered
1752 * unreachable.
1753 *
1754 * The value of the heartbeat interval, in milliseconds. A value of
1755 * UINT32_MAX (4294967295), when modifying the parameter, specifies
1756 * that a heartbeat should be sent immediately to the peer address,
1757 * and the current interval should remain unchanged.
1758 */
1759 if (0xffffffff == params.spp_hbinterval) {
1760 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1761 if (error)
1762 return error;
1763 } else {
1764 /* The value of the heartbeat interval, in milliseconds. A value of 0,
1765 * when modifying the parameter, specifies that the heartbeat on this
1766 * address should be disabled.
1767 */
1768 if (params.spp_hbinterval) {
1769 trans->hb_allowed = 1;
1770 trans->hb_interval =
1771 msecs_to_jiffies(params.spp_hbinterval);
1772 } else
1773 trans->hb_allowed = 0;
1774 }
1775
1776 /* spp_pathmaxrxt contains the maximum number of retransmissions
1777 * before this address shall be considered unreachable.
1778 */
1779 if (params.spp_pathmaxrxt)
1780 trans->max_retrans = params.spp_pathmaxrxt;
1781
1782 return 0;
1783}
1784
1785/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1786 *
1787 * Applications can specify protocol parameters for the default association
1788 * initialization. The option name argument to setsockopt() and getsockopt()
1789 * is SCTP_INITMSG.
1790 *
1791 * Setting initialization parameters is effective only on an unconnected
1792 * socket (for UDP-style sockets only future associations are effected
1793 * by the change). With TCP-style sockets, this option is inherited by
1794 * sockets derived from a listener socket.
1795 */
1796static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
1797{
1798 struct sctp_initmsg sinit;
1799 struct sctp_sock *sp = sctp_sk(sk);
1800
1801 if (optlen != sizeof(struct sctp_initmsg))
1802 return -EINVAL;
1803 if (copy_from_user(&sinit, optval, optlen))
1804 return -EFAULT;
1805
1806 if (sinit.sinit_num_ostreams)
1807 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1808 if (sinit.sinit_max_instreams)
1809 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1810 if (sinit.sinit_max_attempts)
1811 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1812 if (sinit.sinit_max_init_timeo)
1813 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1814
1815 return 0;
1816}
1817
1818/*
1819 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1820 *
1821 * Applications that wish to use the sendto() system call may wish to
1822 * specify a default set of parameters that would normally be supplied
1823 * through the inclusion of ancillary data. This socket option allows
1824 * such an application to set the default sctp_sndrcvinfo structure.
1825 * The application that wishes to use this socket option simply passes
1826 * in to this call the sctp_sndrcvinfo structure defined in Section
1827 * 5.2.2) The input parameters accepted by this call include
1828 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1829 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
1830 * to this call if the caller is using the UDP model.
1831 */
1832static int sctp_setsockopt_default_send_param(struct sock *sk,
1833 char __user *optval, int optlen)
1834{
1835 struct sctp_sndrcvinfo info;
1836 struct sctp_association *asoc;
1837 struct sctp_sock *sp = sctp_sk(sk);
1838
1839 if (optlen != sizeof(struct sctp_sndrcvinfo))
1840 return -EINVAL;
1841 if (copy_from_user(&info, optval, optlen))
1842 return -EFAULT;
1843
1844 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1845 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1846 return -EINVAL;
1847
1848 if (asoc) {
1849 asoc->default_stream = info.sinfo_stream;
1850 asoc->default_flags = info.sinfo_flags;
1851 asoc->default_ppid = info.sinfo_ppid;
1852 asoc->default_context = info.sinfo_context;
1853 asoc->default_timetolive = info.sinfo_timetolive;
1854 } else {
1855 sp->default_stream = info.sinfo_stream;
1856 sp->default_flags = info.sinfo_flags;
1857 sp->default_ppid = info.sinfo_ppid;
1858 sp->default_context = info.sinfo_context;
1859 sp->default_timetolive = info.sinfo_timetolive;
1860 }
1861
1862 return 0;
1863}
1864
1865/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1866 *
1867 * Requests that the local SCTP stack use the enclosed peer address as
1868 * the association primary. The enclosed address must be one of the
1869 * association peer's addresses.
1870 */
1871static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
1872 int optlen)
1873{
1874 struct sctp_prim prim;
1875 struct sctp_transport *trans;
1876
1877 if (optlen != sizeof(struct sctp_prim))
1878 return -EINVAL;
1879
1880 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1881 return -EFAULT;
1882
1883 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1884 if (!trans)
1885 return -EINVAL;
1886
1887 sctp_assoc_set_primary(trans->asoc, trans);
1888
1889 return 0;
1890}
1891
1892/*
1893 * 7.1.5 SCTP_NODELAY
1894 *
1895 * Turn on/off any Nagle-like algorithm. This means that packets are
1896 * generally sent as soon as possible and no unnecessary delays are
1897 * introduced, at the cost of more packets in the network. Expects an
1898 * integer boolean flag.
1899 */
1900static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
1901 int optlen)
1902{
1903 int val;
1904
1905 if (optlen < sizeof(int))
1906 return -EINVAL;
1907 if (get_user(val, (int __user *)optval))
1908 return -EFAULT;
1909
1910 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1911 return 0;
1912}
1913
1914/*
1915 *
1916 * 7.1.1 SCTP_RTOINFO
1917 *
1918 * The protocol parameters used to initialize and bound retransmission
1919 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1920 * and modify these parameters.
1921 * All parameters are time values, in milliseconds. A value of 0, when
1922 * modifying the parameters, indicates that the current value should not
1923 * be changed.
1924 *
1925 */
1926static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
1927 struct sctp_rtoinfo rtoinfo;
1928 struct sctp_association *asoc;
1929
1930 if (optlen != sizeof (struct sctp_rtoinfo))
1931 return -EINVAL;
1932
1933 if (copy_from_user(&rtoinfo, optval, optlen))
1934 return -EFAULT;
1935
1936 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1937
1938 /* Set the values to the specific association */
1939 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1940 return -EINVAL;
1941
1942 if (asoc) {
1943 if (rtoinfo.srto_initial != 0)
1944 asoc->rto_initial =
1945 msecs_to_jiffies(rtoinfo.srto_initial);
1946 if (rtoinfo.srto_max != 0)
1947 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
1948 if (rtoinfo.srto_min != 0)
1949 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
1950 } else {
1951 /* If there is no association or the association-id = 0
1952 * set the values to the endpoint.
1953 */
1954 struct sctp_sock *sp = sctp_sk(sk);
1955
1956 if (rtoinfo.srto_initial != 0)
1957 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1958 if (rtoinfo.srto_max != 0)
1959 sp->rtoinfo.srto_max = rtoinfo.srto_max;
1960 if (rtoinfo.srto_min != 0)
1961 sp->rtoinfo.srto_min = rtoinfo.srto_min;
1962 }
1963
1964 return 0;
1965}
1966
1967/*
1968 *
1969 * 7.1.2 SCTP_ASSOCINFO
1970 *
1971 * This option is used to tune the the maximum retransmission attempts
1972 * of the association.
1973 * Returns an error if the new association retransmission value is
1974 * greater than the sum of the retransmission value of the peer.
1975 * See [SCTP] for more information.
1976 *
1977 */
1978static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
1979{
1980
1981 struct sctp_assocparams assocparams;
1982 struct sctp_association *asoc;
1983
1984 if (optlen != sizeof(struct sctp_assocparams))
1985 return -EINVAL;
1986 if (copy_from_user(&assocparams, optval, optlen))
1987 return -EFAULT;
1988
1989 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1990
1991 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1992 return -EINVAL;
1993
1994 /* Set the values to the specific association */
1995 if (asoc) {
1996 if (assocparams.sasoc_asocmaxrxt != 0)
1997 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1998 if (assocparams.sasoc_cookie_life != 0) {
1999 asoc->cookie_life.tv_sec =
2000 assocparams.sasoc_cookie_life / 1000;
2001 asoc->cookie_life.tv_usec =
2002 (assocparams.sasoc_cookie_life % 1000)
2003 * 1000;
2004 }
2005 } else {
2006 /* Set the values to the endpoint */
2007 struct sctp_sock *sp = sctp_sk(sk);
2008
2009 if (assocparams.sasoc_asocmaxrxt != 0)
2010 sp->assocparams.sasoc_asocmaxrxt =
2011 assocparams.sasoc_asocmaxrxt;
2012 if (assocparams.sasoc_cookie_life != 0)
2013 sp->assocparams.sasoc_cookie_life =
2014 assocparams.sasoc_cookie_life;
2015 }
2016 return 0;
2017}
2018
2019/*
2020 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2021 *
2022 * This socket option is a boolean flag which turns on or off mapped V4
2023 * addresses. If this option is turned on and the socket is type
2024 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2025 * If this option is turned off, then no mapping will be done of V4
2026 * addresses and a user will receive both PF_INET6 and PF_INET type
2027 * addresses on the socket.
2028 */
2029static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2030{
2031 int val;
2032 struct sctp_sock *sp = sctp_sk(sk);
2033
2034 if (optlen < sizeof(int))
2035 return -EINVAL;
2036 if (get_user(val, (int __user *)optval))
2037 return -EFAULT;
2038 if (val)
2039 sp->v4mapped = 1;
2040 else
2041 sp->v4mapped = 0;
2042
2043 return 0;
2044}
2045
2046/*
2047 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2048 *
2049 * This socket option specifies the maximum size to put in any outgoing
2050 * SCTP chunk. If a message is larger than this size it will be
2051 * fragmented by SCTP into the specified size. Note that the underlying
2052 * SCTP implementation may fragment into smaller sized chunks when the
2053 * PMTU of the underlying association is smaller than the value set by
2054 * the user.
2055 */
2056static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2057{
2058 struct sctp_association *asoc;
2059 struct list_head *pos;
2060 struct sctp_sock *sp = sctp_sk(sk);
2061 int val;
2062
2063 if (optlen < sizeof(int))
2064 return -EINVAL;
2065 if (get_user(val, (int __user *)optval))
2066 return -EFAULT;
2067 if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
2068 return -EINVAL;
2069 sp->user_frag = val;
2070
2071 if (val) {
2072 /* Update the frag_point of the existing associations. */
2073 list_for_each(pos, &(sp->ep->asocs)) {
2074 asoc = list_entry(pos, struct sctp_association, asocs);
2075 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
2076 }
2077 }
2078
2079 return 0;
2080}
2081
2082
2083/*
2084 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2085 *
2086 * Requests that the peer mark the enclosed address as the association
2087 * primary. The enclosed address must be one of the association's
2088 * locally bound addresses. The following structure is used to make a
2089 * set primary request:
2090 */
2091static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2092 int optlen)
2093{
2094 struct sctp_sock *sp;
2095 struct sctp_endpoint *ep;
2096 struct sctp_association *asoc = NULL;
2097 struct sctp_setpeerprim prim;
2098 struct sctp_chunk *chunk;
2099 int err;
2100
2101 sp = sctp_sk(sk);
2102 ep = sp->ep;
2103
2104 if (!sctp_addip_enable)
2105 return -EPERM;
2106
2107 if (optlen != sizeof(struct sctp_setpeerprim))
2108 return -EINVAL;
2109
2110 if (copy_from_user(&prim, optval, optlen))
2111 return -EFAULT;
2112
2113 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2114 if (!asoc)
2115 return -EINVAL;
2116
2117 if (!asoc->peer.asconf_capable)
2118 return -EPERM;
2119
2120 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2121 return -EPERM;
2122
2123 if (!sctp_state(asoc, ESTABLISHED))
2124 return -ENOTCONN;
2125
2126 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2127 return -EADDRNOTAVAIL;
2128
2129 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2130 chunk = sctp_make_asconf_set_prim(asoc,
2131 (union sctp_addr *)&prim.sspp_addr);
2132 if (!chunk)
2133 return -ENOMEM;
2134
2135 err = sctp_send_asconf(asoc, chunk);
2136
2137 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2138
2139 return err;
2140}
2141
2142static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2143 int optlen)
2144{
2145 __u32 val;
2146
2147 if (optlen < sizeof(__u32))
2148 return -EINVAL;
2149 if (copy_from_user(&val, optval, sizeof(__u32)))
2150 return -EFAULT;
2151
2152 sctp_sk(sk)->adaption_ind = val;
2153
2154 return 0;
2155}
2156
2157/* API 6.2 setsockopt(), getsockopt()
2158 *
2159 * Applications use setsockopt() and getsockopt() to set or retrieve
2160 * socket options. Socket options are used to change the default
2161 * behavior of sockets calls. They are described in Section 7.
2162 *
2163 * The syntax is:
2164 *
2165 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2166 * int __user *optlen);
2167 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2168 * int optlen);
2169 *
2170 * sd - the socket descript.
2171 * level - set to IPPROTO_SCTP for all SCTP options.
2172 * optname - the option name.
2173 * optval - the buffer to store the value of the option.
2174 * optlen - the size of the buffer.
2175 */
2176SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2177 char __user *optval, int optlen)
2178{
2179 int retval = 0;
2180
2181 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2182 sk, optname);
2183
2184 /* I can hardly begin to describe how wrong this is. This is
2185 * so broken as to be worse than useless. The API draft
2186 * REALLY is NOT helpful here... I am not convinced that the
2187 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2188 * are at all well-founded.
2189 */
2190 if (level != SOL_SCTP) {
2191 struct sctp_af *af = sctp_sk(sk)->pf->af;
2192 retval = af->setsockopt(sk, level, optname, optval, optlen);
2193 goto out_nounlock;
2194 }
2195
2196 sctp_lock_sock(sk);
2197
2198 switch (optname) {
2199 case SCTP_SOCKOPT_BINDX_ADD:
2200 /* 'optlen' is the size of the addresses buffer. */
2201 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2202 optlen, SCTP_BINDX_ADD_ADDR);
2203 break;
2204
2205 case SCTP_SOCKOPT_BINDX_REM:
2206 /* 'optlen' is the size of the addresses buffer. */
2207 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2208 optlen, SCTP_BINDX_REM_ADDR);
2209 break;
2210
2211 case SCTP_DISABLE_FRAGMENTS:
2212 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2213 break;
2214
2215 case SCTP_EVENTS:
2216 retval = sctp_setsockopt_events(sk, optval, optlen);
2217 break;
2218
2219 case SCTP_AUTOCLOSE:
2220 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2221 break;
2222
2223 case SCTP_PEER_ADDR_PARAMS:
2224 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2225 break;
2226
2227 case SCTP_INITMSG:
2228 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2229 break;
2230 case SCTP_DEFAULT_SEND_PARAM:
2231 retval = sctp_setsockopt_default_send_param(sk, optval,
2232 optlen);
2233 break;
2234 case SCTP_PRIMARY_ADDR:
2235 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2236 break;
2237 case SCTP_SET_PEER_PRIMARY_ADDR:
2238 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2239 break;
2240 case SCTP_NODELAY:
2241 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2242 break;
2243 case SCTP_RTOINFO:
2244 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2245 break;
2246 case SCTP_ASSOCINFO:
2247 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2248 break;
2249 case SCTP_I_WANT_MAPPED_V4_ADDR:
2250 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2251 break;
2252 case SCTP_MAXSEG:
2253 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2254 break;
2255 case SCTP_ADAPTION_LAYER:
2256 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2257 break;
2258
2259 default:
2260 retval = -ENOPROTOOPT;
2261 break;
2262 };
2263
2264 sctp_release_sock(sk);
2265
2266out_nounlock:
2267 return retval;
2268}
2269
2270/* API 3.1.6 connect() - UDP Style Syntax
2271 *
2272 * An application may use the connect() call in the UDP model to initiate an
2273 * association without sending data.
2274 *
2275 * The syntax is:
2276 *
2277 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2278 *
2279 * sd: the socket descriptor to have a new association added to.
2280 *
2281 * nam: the address structure (either struct sockaddr_in or struct
2282 * sockaddr_in6 defined in RFC2553 [7]).
2283 *
2284 * len: the size of the address.
2285 */
2286SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2287 int addr_len)
2288{
2289 struct sctp_sock *sp;
2290 struct sctp_endpoint *ep;
2291 struct sctp_association *asoc;
2292 struct sctp_transport *transport;
2293 union sctp_addr to;
2294 struct sctp_af *af;
2295 sctp_scope_t scope;
2296 long timeo;
2297 int err = 0;
2298
2299 sctp_lock_sock(sk);
2300
2301 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2302 __FUNCTION__, sk, uaddr, addr_len);
2303
2304 sp = sctp_sk(sk);
2305 ep = sp->ep;
2306
2307 /* connect() cannot be done on a socket that is already in ESTABLISHED
2308 * state - UDP-style peeled off socket or a TCP-style socket that
2309 * is already connected.
2310 * It cannot be done even on a TCP-style listening socket.
2311 */
2312 if (sctp_sstate(sk, ESTABLISHED) ||
2313 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2314 err = -EISCONN;
2315 goto out_unlock;
2316 }
2317
2318 err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2319 if (err)
2320 goto out_unlock;
2321
2322 if (addr_len > sizeof(to))
2323 addr_len = sizeof(to);
2324 memcpy(&to, uaddr, addr_len);
2325 to.v4.sin_port = ntohs(to.v4.sin_port);
2326
2327 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2328 if (asoc) {
2329 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2330 err = -EISCONN;
2331 else
2332 err = -EALREADY;
2333 goto out_unlock;
2334 }
2335
2336 /* If we could not find a matching association on the endpoint,
2337 * make sure that there is no peeled-off association matching the
2338 * peer address even on another socket.
2339 */
2340 if (sctp_endpoint_is_peeled_off(ep, &to)) {
2341 err = -EADDRNOTAVAIL;
2342 goto out_unlock;
2343 }
2344
2345 /* If a bind() or sctp_bindx() is not called prior to a connect()
2346 * call, the system picks an ephemeral port and will choose an address
2347 * set equivalent to binding with a wildcard address.
2348 */
2349 if (!ep->base.bind_addr.port) {
2350 if (sctp_autobind(sk)) {
2351 err = -EAGAIN;
2352 goto out_unlock;
2353 }
2354 }
2355
2356 scope = sctp_scope(&to);
2357 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2358 if (!asoc) {
2359 err = -ENOMEM;
2360 goto out_unlock;
2361 }
2362
2363 /* Prime the peer's transport structures. */
2364 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2365 if (!transport) {
2366 sctp_association_free(asoc);
2367 goto out_unlock;
2368 }
2369 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2370 if (err < 0) {
2371 sctp_association_free(asoc);
2372 goto out_unlock;
2373 }
2374
2375 err = sctp_primitive_ASSOCIATE(asoc, NULL);
2376 if (err < 0) {
2377 sctp_association_free(asoc);
2378 goto out_unlock;
2379 }
2380
2381 /* Initialize sk's dport and daddr for getpeername() */
2382 inet_sk(sk)->dport = htons(asoc->peer.port);
2383 af = sctp_get_af_specific(to.sa.sa_family);
2384 af->to_sk_daddr(&to, sk);
2385
2386 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2387 err = sctp_wait_for_connect(asoc, &timeo);
2388
2389out_unlock:
2390 sctp_release_sock(sk);
2391
2392 return err;
2393}
2394
2395/* FIXME: Write comments. */
2396SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2397{
2398 return -EOPNOTSUPP; /* STUB */
2399}
2400
2401/* 4.1.4 accept() - TCP Style Syntax
2402 *
2403 * Applications use accept() call to remove an established SCTP
2404 * association from the accept queue of the endpoint. A new socket
2405 * descriptor will be returned from accept() to represent the newly
2406 * formed association.
2407 */
2408SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2409{
2410 struct sctp_sock *sp;
2411 struct sctp_endpoint *ep;
2412 struct sock *newsk = NULL;
2413 struct sctp_association *asoc;
2414 long timeo;
2415 int error = 0;
2416
2417 sctp_lock_sock(sk);
2418
2419 sp = sctp_sk(sk);
2420 ep = sp->ep;
2421
2422 if (!sctp_style(sk, TCP)) {
2423 error = -EOPNOTSUPP;
2424 goto out;
2425 }
2426
2427 if (!sctp_sstate(sk, LISTENING)) {
2428 error = -EINVAL;
2429 goto out;
2430 }
2431
2432 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2433
2434 error = sctp_wait_for_accept(sk, timeo);
2435 if (error)
2436 goto out;
2437
2438 /* We treat the list of associations on the endpoint as the accept
2439 * queue and pick the first association on the list.
2440 */
2441 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2442
2443 newsk = sp->pf->create_accept_sk(sk, asoc);
2444 if (!newsk) {
2445 error = -ENOMEM;
2446 goto out;
2447 }
2448
2449 /* Populate the fields of the newsk from the oldsk and migrate the
2450 * asoc to the newsk.
2451 */
2452 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2453
2454out:
2455 sctp_release_sock(sk);
2456 *err = error;
2457 return newsk;
2458}
2459
2460/* The SCTP ioctl handler. */
2461SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2462{
2463 return -ENOIOCTLCMD;
2464}
2465
2466/* This is the function which gets called during socket creation to
2467 * initialized the SCTP-specific portion of the sock.
2468 * The sock structure should already be zero-filled memory.
2469 */
2470SCTP_STATIC int sctp_init_sock(struct sock *sk)
2471{
2472 struct sctp_endpoint *ep;
2473 struct sctp_sock *sp;
2474
2475 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2476
2477 sp = sctp_sk(sk);
2478
2479 /* Initialize the SCTP per socket area. */
2480 switch (sk->sk_type) {
2481 case SOCK_SEQPACKET:
2482 sp->type = SCTP_SOCKET_UDP;
2483 break;
2484 case SOCK_STREAM:
2485 sp->type = SCTP_SOCKET_TCP;
2486 break;
2487 default:
2488 return -ESOCKTNOSUPPORT;
2489 }
2490
2491 /* Initialize default send parameters. These parameters can be
2492 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2493 */
2494 sp->default_stream = 0;
2495 sp->default_ppid = 0;
2496 sp->default_flags = 0;
2497 sp->default_context = 0;
2498 sp->default_timetolive = 0;
2499
2500 /* Initialize default setup parameters. These parameters
2501 * can be modified with the SCTP_INITMSG socket option or
2502 * overridden by the SCTP_INIT CMSG.
2503 */
2504 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2505 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2506 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2507 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2508
2509 /* Initialize default RTO related parameters. These parameters can
2510 * be modified for with the SCTP_RTOINFO socket option.
2511 */
2512 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2513 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2514 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2515
2516 /* Initialize default association related parameters. These parameters
2517 * can be modified with the SCTP_ASSOCINFO socket option.
2518 */
2519 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2520 sp->assocparams.sasoc_number_peer_destinations = 0;
2521 sp->assocparams.sasoc_peer_rwnd = 0;
2522 sp->assocparams.sasoc_local_rwnd = 0;
2523 sp->assocparams.sasoc_cookie_life =
2524 jiffies_to_msecs(sctp_valid_cookie_life);
2525
2526 /* Initialize default event subscriptions. By default, all the
2527 * options are off.
2528 */
2529 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2530
2531 /* Default Peer Address Parameters. These defaults can
2532 * be modified via SCTP_PEER_ADDR_PARAMS
2533 */
2534 sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
2535 sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2536
2537 /* If enabled no SCTP message fragmentation will be performed.
2538 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2539 */
2540 sp->disable_fragments = 0;
2541
2542 /* Turn on/off any Nagle-like algorithm. */
2543 sp->nodelay = 1;
2544
2545 /* Enable by default. */
2546 sp->v4mapped = 1;
2547
2548 /* Auto-close idle associations after the configured
2549 * number of seconds. A value of 0 disables this
2550 * feature. Configure through the SCTP_AUTOCLOSE socket option,
2551 * for UDP-style sockets only.
2552 */
2553 sp->autoclose = 0;
2554
2555 /* User specified fragmentation limit. */
2556 sp->user_frag = 0;
2557
2558 sp->adaption_ind = 0;
2559
2560 sp->pf = sctp_get_pf_specific(sk->sk_family);
2561
2562 /* Control variables for partial data delivery. */
2563 sp->pd_mode = 0;
2564 skb_queue_head_init(&sp->pd_lobby);
2565
2566 /* Create a per socket endpoint structure. Even if we
2567 * change the data structure relationships, this may still
2568 * be useful for storing pre-connect address information.
2569 */
2570 ep = sctp_endpoint_new(sk, GFP_KERNEL);
2571 if (!ep)
2572 return -ENOMEM;
2573
2574 sp->ep = ep;
2575 sp->hmac = NULL;
2576
2577 SCTP_DBG_OBJCNT_INC(sock);
2578 return 0;
2579}
2580
2581/* Cleanup any SCTP per socket resources. */
2582SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2583{
2584 struct sctp_endpoint *ep;
2585
2586 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2587
2588 /* Release our hold on the endpoint. */
2589 ep = sctp_sk(sk)->ep;
2590 sctp_endpoint_free(ep);
2591
2592 return 0;
2593}
2594
2595/* API 4.1.7 shutdown() - TCP Style Syntax
2596 * int shutdown(int socket, int how);
2597 *
2598 * sd - the socket descriptor of the association to be closed.
2599 * how - Specifies the type of shutdown. The values are
2600 * as follows:
2601 * SHUT_RD
2602 * Disables further receive operations. No SCTP
2603 * protocol action is taken.
2604 * SHUT_WR
2605 * Disables further send operations, and initiates
2606 * the SCTP shutdown sequence.
2607 * SHUT_RDWR
2608 * Disables further send and receive operations
2609 * and initiates the SCTP shutdown sequence.
2610 */
2611SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2612{
2613 struct sctp_endpoint *ep;
2614 struct sctp_association *asoc;
2615
2616 if (!sctp_style(sk, TCP))
2617 return;
2618
2619 if (how & SEND_SHUTDOWN) {
2620 ep = sctp_sk(sk)->ep;
2621 if (!list_empty(&ep->asocs)) {
2622 asoc = list_entry(ep->asocs.next,
2623 struct sctp_association, asocs);
2624 sctp_primitive_SHUTDOWN(asoc, NULL);
2625 }
2626 }
2627}
2628
2629/* 7.2.1 Association Status (SCTP_STATUS)
2630
2631 * Applications can retrieve current status information about an
2632 * association, including association state, peer receiver window size,
2633 * number of unacked data chunks, and number of data chunks pending
2634 * receipt. This information is read-only.
2635 */
2636static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
2637 char __user *optval,
2638 int __user *optlen)
2639{
2640 struct sctp_status status;
2641 struct sctp_association *asoc = NULL;
2642 struct sctp_transport *transport;
2643 sctp_assoc_t associd;
2644 int retval = 0;
2645
2646 if (len != sizeof(status)) {
2647 retval = -EINVAL;
2648 goto out;
2649 }
2650
2651 if (copy_from_user(&status, optval, sizeof(status))) {
2652 retval = -EFAULT;
2653 goto out;
2654 }
2655
2656 associd = status.sstat_assoc_id;
2657 asoc = sctp_id2assoc(sk, associd);
2658 if (!asoc) {
2659 retval = -EINVAL;
2660 goto out;
2661 }
2662
2663 transport = asoc->peer.primary_path;
2664
2665 status.sstat_assoc_id = sctp_assoc2id(asoc);
2666 status.sstat_state = asoc->state;
2667 status.sstat_rwnd = asoc->peer.rwnd;
2668 status.sstat_unackdata = asoc->unack_data;
2669
2670 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2671 status.sstat_instrms = asoc->c.sinit_max_instreams;
2672 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2673 status.sstat_fragmentation_point = asoc->frag_point;
2674 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2675 memcpy(&status.sstat_primary.spinfo_address,
2676 &(transport->ipaddr), sizeof(union sctp_addr));
2677 /* Map ipv4 address into v4-mapped-on-v6 address. */
2678 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2679 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2680 status.sstat_primary.spinfo_state = transport->active;
2681 status.sstat_primary.spinfo_cwnd = transport->cwnd;
2682 status.sstat_primary.spinfo_srtt = transport->srtt;
2683 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
2684 status.sstat_primary.spinfo_mtu = transport->pmtu;
2685
2686 if (put_user(len, optlen)) {
2687 retval = -EFAULT;
2688 goto out;
2689 }
2690
2691 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
2692 len, status.sstat_state, status.sstat_rwnd,
2693 status.sstat_assoc_id);
2694
2695 if (copy_to_user(optval, &status, len)) {
2696 retval = -EFAULT;
2697 goto out;
2698 }
2699
2700out:
2701 return (retval);
2702}
2703
2704
2705/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2706 *
2707 * Applications can retrieve information about a specific peer address
2708 * of an association, including its reachability state, congestion
2709 * window, and retransmission timer values. This information is
2710 * read-only.
2711 */
2712static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2713 char __user *optval,
2714 int __user *optlen)
2715{
2716 struct sctp_paddrinfo pinfo;
2717 struct sctp_transport *transport;
2718 int retval = 0;
2719
2720 if (len != sizeof(pinfo)) {
2721 retval = -EINVAL;
2722 goto out;
2723 }
2724
2725 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2726 retval = -EFAULT;
2727 goto out;
2728 }
2729
2730 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2731 pinfo.spinfo_assoc_id);
2732 if (!transport)
2733 return -EINVAL;
2734
2735 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2736 pinfo.spinfo_state = transport->active;
2737 pinfo.spinfo_cwnd = transport->cwnd;
2738 pinfo.spinfo_srtt = transport->srtt;
2739 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
2740 pinfo.spinfo_mtu = transport->pmtu;
2741
2742 if (put_user(len, optlen)) {
2743 retval = -EFAULT;
2744 goto out;
2745 }
2746
2747 if (copy_to_user(optval, &pinfo, len)) {
2748 retval = -EFAULT;
2749 goto out;
2750 }
2751
2752out:
2753 return (retval);
2754}
2755
2756/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2757 *
2758 * This option is a on/off flag. If enabled no SCTP message
2759 * fragmentation will be performed. Instead if a message being sent
2760 * exceeds the current PMTU size, the message will NOT be sent and
2761 * instead a error will be indicated to the user.
2762 */
2763static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2764 char __user *optval, int __user *optlen)
2765{
2766 int val;
2767
2768 if (len < sizeof(int))
2769 return -EINVAL;
2770
2771 len = sizeof(int);
2772 val = (sctp_sk(sk)->disable_fragments == 1);
2773 if (put_user(len, optlen))
2774 return -EFAULT;
2775 if (copy_to_user(optval, &val, len))
2776 return -EFAULT;
2777 return 0;
2778}
2779
2780/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2781 *
2782 * This socket option is used to specify various notifications and
2783 * ancillary data the user wishes to receive.
2784 */
2785static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
2786 int __user *optlen)
2787{
2788 if (len != sizeof(struct sctp_event_subscribe))
2789 return -EINVAL;
2790 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2791 return -EFAULT;
2792 return 0;
2793}
2794
2795/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2796 *
2797 * This socket option is applicable to the UDP-style socket only. When
2798 * set it will cause associations that are idle for more than the
2799 * specified number of seconds to automatically close. An association
2800 * being idle is defined an association that has NOT sent or received
2801 * user data. The special value of '0' indicates that no automatic
2802 * close of any associations should be performed. The option expects an
2803 * integer defining the number of seconds of idle time before an
2804 * association is closed.
2805 */
2806static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
2807{
2808 /* Applicable to UDP-style socket only */
2809 if (sctp_style(sk, TCP))
2810 return -EOPNOTSUPP;
2811 if (len != sizeof(int))
2812 return -EINVAL;
2813 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2814 return -EFAULT;
2815 return 0;
2816}
2817
2818/* Helper routine to branch off an association to a new socket. */
2819SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2820 struct socket **sockp)
2821{
2822 struct sock *sk = asoc->base.sk;
2823 struct socket *sock;
2824 int err = 0;
2825
2826 /* An association cannot be branched off from an already peeled-off
2827 * socket, nor is this supported for tcp style sockets.
2828 */
2829 if (!sctp_style(sk, UDP))
2830 return -EINVAL;
2831
2832 /* Create a new socket. */
2833 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2834 if (err < 0)
2835 return err;
2836
2837 /* Populate the fields of the newsk from the oldsk and migrate the
2838 * asoc to the newsk.
2839 */
2840 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2841 *sockp = sock;
2842
2843 return err;
2844}
2845
2846static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
2847{
2848 sctp_peeloff_arg_t peeloff;
2849 struct socket *newsock;
2850 int retval = 0;
2851 struct sctp_association *asoc;
2852
2853 if (len != sizeof(sctp_peeloff_arg_t))
2854 return -EINVAL;
2855 if (copy_from_user(&peeloff, optval, len))
2856 return -EFAULT;
2857
2858 asoc = sctp_id2assoc(sk, peeloff.associd);
2859 if (!asoc) {
2860 retval = -EINVAL;
2861 goto out;
2862 }
2863
2864 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2865
2866 retval = sctp_do_peeloff(asoc, &newsock);
2867 if (retval < 0)
2868 goto out;
2869
2870 /* Map the socket to an unused fd that can be returned to the user. */
2871 retval = sock_map_fd(newsock);
2872 if (retval < 0) {
2873 sock_release(newsock);
2874 goto out;
2875 }
2876
2877 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2878 __FUNCTION__, sk, asoc, newsock->sk, retval);
2879
2880 /* Return the fd mapped to the new socket. */
2881 peeloff.sd = retval;
2882 if (copy_to_user(optval, &peeloff, len))
2883 retval = -EFAULT;
2884
2885out:
2886 return retval;
2887}
2888
2889/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2890 *
2891 * Applications can enable or disable heartbeats for any peer address of
2892 * an association, modify an address's heartbeat interval, force a
2893 * heartbeat to be sent immediately, and adjust the address's maximum
2894 * number of retransmissions sent before an address is considered
2895 * unreachable. The following structure is used to access and modify an
2896 * address's parameters:
2897 *
2898 * struct sctp_paddrparams {
2899 * sctp_assoc_t spp_assoc_id;
2900 * struct sockaddr_storage spp_address;
2901 * uint32_t spp_hbinterval;
2902 * uint16_t spp_pathmaxrxt;
2903 * };
2904 *
2905 * spp_assoc_id - (UDP style socket) This is filled in the application,
2906 * and identifies the association for this query.
2907 * spp_address - This specifies which address is of interest.
2908 * spp_hbinterval - This contains the value of the heartbeat interval,
2909 * in milliseconds. A value of 0, when modifying the
2910 * parameter, specifies that the heartbeat on this
2911 * address should be disabled. A value of UINT32_MAX
2912 * (4294967295), when modifying the parameter,
2913 * specifies that a heartbeat should be sent
2914 * immediately to the peer address, and the current
2915 * interval should remain unchanged.
2916 * spp_pathmaxrxt - This contains the maximum number of
2917 * retransmissions before this address shall be
2918 * considered unreachable.
2919 */
2920static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2921 char __user *optval, int __user *optlen)
2922{
2923 struct sctp_paddrparams params;
2924 struct sctp_transport *trans;
2925
2926 if (len != sizeof(struct sctp_paddrparams))
2927 return -EINVAL;
2928 if (copy_from_user(&params, optval, len))
2929 return -EFAULT;
2930
2931 /* If no association id is specified retrieve the default value
2932 * for the endpoint that will be used for all future associations
2933 */
2934 if (!params.spp_assoc_id &&
2935 sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2936 params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
2937 params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
2938
2939 goto done;
2940 }
2941
2942 trans = sctp_addr_id2transport(sk, &params.spp_address,
2943 params.spp_assoc_id);
2944 if (!trans)
2945 return -EINVAL;
2946
2947 /* The value of the heartbeat interval, in milliseconds. A value of 0,
2948 * when modifying the parameter, specifies that the heartbeat on this
2949 * address should be disabled.
2950 */
2951 if (!trans->hb_allowed)
2952 params.spp_hbinterval = 0;
2953 else
2954 params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
2955
2956 /* spp_pathmaxrxt contains the maximum number of retransmissions
2957 * before this address shall be considered unreachable.
2958 */
2959 params.spp_pathmaxrxt = trans->max_retrans;
2960
2961done:
2962 if (copy_to_user(optval, &params, len))
2963 return -EFAULT;
2964
2965 if (put_user(len, optlen))
2966 return -EFAULT;
2967
2968 return 0;
2969}
2970
2971/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2972 *
2973 * Applications can specify protocol parameters for the default association
2974 * initialization. The option name argument to setsockopt() and getsockopt()
2975 * is SCTP_INITMSG.
2976 *
2977 * Setting initialization parameters is effective only on an unconnected
2978 * socket (for UDP-style sockets only future associations are effected
2979 * by the change). With TCP-style sockets, this option is inherited by
2980 * sockets derived from a listener socket.
2981 */
2982static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
2983{
2984 if (len != sizeof(struct sctp_initmsg))
2985 return -EINVAL;
2986 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2987 return -EFAULT;
2988 return 0;
2989}
2990
2991static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2992 char __user *optval, int __user *optlen)
2993{
2994 sctp_assoc_t id;
2995 struct sctp_association *asoc;
2996 struct list_head *pos;
2997 int cnt = 0;
2998
2999 if (len != sizeof(sctp_assoc_t))
3000 return -EINVAL;
3001
3002 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3003 return -EFAULT;
3004
3005 /* For UDP-style sockets, id specifies the association to query. */
3006 asoc = sctp_id2assoc(sk, id);
3007 if (!asoc)
3008 return -EINVAL;
3009
3010 list_for_each(pos, &asoc->peer.transport_addr_list) {
3011 cnt ++;
3012 }
3013
3014 return cnt;
3015}
3016
3017static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3018 char __user *optval, int __user *optlen)
3019{
3020 struct sctp_association *asoc;
3021 struct list_head *pos;
3022 int cnt = 0;
3023 struct sctp_getaddrs getaddrs;
3024 struct sctp_transport *from;
3025 void __user *to;
3026 union sctp_addr temp;
3027 struct sctp_sock *sp = sctp_sk(sk);
3028 int addrlen;
3029
3030 if (len != sizeof(struct sctp_getaddrs))
3031 return -EINVAL;
3032
3033 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3034 return -EFAULT;
3035
3036 if (getaddrs.addr_num <= 0) return -EINVAL;
3037
3038 /* For UDP-style sockets, id specifies the association to query. */
3039 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3040 if (!asoc)
3041 return -EINVAL;
3042
3043 to = (void __user *)getaddrs.addrs;
3044 list_for_each(pos, &asoc->peer.transport_addr_list) {
3045 from = list_entry(pos, struct sctp_transport, transports);
3046 memcpy(&temp, &from->ipaddr, sizeof(temp));
3047 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3048 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3049 temp.v4.sin_port = htons(temp.v4.sin_port);
3050 if (copy_to_user(to, &temp, addrlen))
3051 return -EFAULT;
3052 to += addrlen ;
3053 cnt ++;
3054 if (cnt >= getaddrs.addr_num) break;
3055 }
3056 getaddrs.addr_num = cnt;
3057 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3058 return -EFAULT;
3059
3060 return 0;
3061}
3062
3063static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
3064 char __user *optval,
3065 int __user *optlen)
3066{
3067 sctp_assoc_t id;
3068 struct sctp_bind_addr *bp;
3069 struct sctp_association *asoc;
3070 struct list_head *pos;
3071 struct sctp_sockaddr_entry *addr;
3072 rwlock_t *addr_lock;
3073 unsigned long flags;
3074 int cnt = 0;
3075
3076 if (len != sizeof(sctp_assoc_t))
3077 return -EINVAL;
3078
3079 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3080 return -EFAULT;
3081
3082 /*
3083 * For UDP-style sockets, id specifies the association to query.
3084 * If the id field is set to the value '0' then the locally bound
3085 * addresses are returned without regard to any particular
3086 * association.
3087 */
3088 if (0 == id) {
3089 bp = &sctp_sk(sk)->ep->base.bind_addr;
3090 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3091 } else {
3092 asoc = sctp_id2assoc(sk, id);
3093 if (!asoc)
3094 return -EINVAL;
3095 bp = &asoc->base.bind_addr;
3096 addr_lock = &asoc->base.addr_lock;
3097 }
3098
3099 sctp_read_lock(addr_lock);
3100
3101 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3102 * addresses from the global local address list.
3103 */
3104 if (sctp_list_single_entry(&bp->address_list)) {
3105 addr = list_entry(bp->address_list.next,
3106 struct sctp_sockaddr_entry, list);
3107 if (sctp_is_any(&addr->a)) {
3108 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3109 list_for_each(pos, &sctp_local_addr_list) {
3110 addr = list_entry(pos,
3111 struct sctp_sockaddr_entry,
3112 list);
3113 if ((PF_INET == sk->sk_family) &&
3114 (AF_INET6 == addr->a.sa.sa_family))
3115 continue;
3116 cnt++;
3117 }
3118 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3119 flags);
3120 } else {
3121 cnt = 1;
3122 }
3123 goto done;
3124 }
3125
3126 list_for_each(pos, &bp->address_list) {
3127 cnt ++;
3128 }
3129
3130done:
3131 sctp_read_unlock(addr_lock);
3132 return cnt;
3133}
3134
3135/* Helper function that copies local addresses to user and returns the number
3136 * of addresses copied.
3137 */
3138static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, int max_addrs,
3139 void __user *to)
3140{
3141 struct list_head *pos;
3142 struct sctp_sockaddr_entry *addr;
3143 unsigned long flags;
3144 union sctp_addr temp;
3145 int cnt = 0;
3146 int addrlen;
3147
3148 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3149 list_for_each(pos, &sctp_local_addr_list) {
3150 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3151 if ((PF_INET == sk->sk_family) &&
3152 (AF_INET6 == addr->a.sa.sa_family))
3153 continue;
3154 memcpy(&temp, &addr->a, sizeof(temp));
3155 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3156 &temp);
3157 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3158 temp.v4.sin_port = htons(port);
3159 if (copy_to_user(to, &temp, addrlen)) {
3160 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3161 flags);
3162 return -EFAULT;
3163 }
3164 to += addrlen;
3165 cnt ++;
3166 if (cnt >= max_addrs) break;
3167 }
3168 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3169
3170 return cnt;
3171}
3172
3173static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3174 char __user *optval, int __user *optlen)
3175{
3176 struct sctp_bind_addr *bp;
3177 struct sctp_association *asoc;
3178 struct list_head *pos;
3179 int cnt = 0;
3180 struct sctp_getaddrs getaddrs;
3181 struct sctp_sockaddr_entry *addr;
3182 void __user *to;
3183 union sctp_addr temp;
3184 struct sctp_sock *sp = sctp_sk(sk);
3185 int addrlen;
3186 rwlock_t *addr_lock;
3187 int err = 0;
3188
3189 if (len != sizeof(struct sctp_getaddrs))
3190 return -EINVAL;
3191
3192 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3193 return -EFAULT;
3194
3195 if (getaddrs.addr_num <= 0) return -EINVAL;
3196 /*
3197 * For UDP-style sockets, id specifies the association to query.
3198 * If the id field is set to the value '0' then the locally bound
3199 * addresses are returned without regard to any particular
3200 * association.
3201 */
3202 if (0 == getaddrs.assoc_id) {
3203 bp = &sctp_sk(sk)->ep->base.bind_addr;
3204 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3205 } else {
3206 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3207 if (!asoc)
3208 return -EINVAL;
3209 bp = &asoc->base.bind_addr;
3210 addr_lock = &asoc->base.addr_lock;
3211 }
3212
3213 to = getaddrs.addrs;
3214
3215 sctp_read_lock(addr_lock);
3216
3217 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3218 * addresses from the global local address list.
3219 */
3220 if (sctp_list_single_entry(&bp->address_list)) {
3221 addr = list_entry(bp->address_list.next,
3222 struct sctp_sockaddr_entry, list);
3223 if (sctp_is_any(&addr->a)) {
3224 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
3225 getaddrs.addr_num, to);
3226 if (cnt < 0) {
3227 err = cnt;
3228 goto unlock;
3229 }
3230 goto copy_getaddrs;
3231 }
3232 }
3233
3234 list_for_each(pos, &bp->address_list) {
3235 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3236 memcpy(&temp, &addr->a, sizeof(temp));
3237 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3238 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3239 temp.v4.sin_port = htons(temp.v4.sin_port);
3240 if (copy_to_user(to, &temp, addrlen)) {
3241 err = -EFAULT;
3242 goto unlock;
3243 }
3244 to += addrlen;
3245 cnt ++;
3246 if (cnt >= getaddrs.addr_num) break;
3247 }
3248
3249copy_getaddrs:
3250 getaddrs.addr_num = cnt;
3251 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
3252 err = -EFAULT;
3253
3254unlock:
3255 sctp_read_unlock(addr_lock);
3256 return err;
3257}
3258
3259/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3260 *
3261 * Requests that the local SCTP stack use the enclosed peer address as
3262 * the association primary. The enclosed address must be one of the
3263 * association peer's addresses.
3264 */
3265static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
3266 char __user *optval, int __user *optlen)
3267{
3268 struct sctp_prim prim;
3269 struct sctp_association *asoc;
3270 struct sctp_sock *sp = sctp_sk(sk);
3271
3272 if (len != sizeof(struct sctp_prim))
3273 return -EINVAL;
3274
3275 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3276 return -EFAULT;
3277
3278 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
3279 if (!asoc)
3280 return -EINVAL;
3281
3282 if (!asoc->peer.primary_path)
3283 return -ENOTCONN;
3284
3285 asoc->peer.primary_path->ipaddr.v4.sin_port =
3286 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
3287 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
3288 sizeof(union sctp_addr));
3289 asoc->peer.primary_path->ipaddr.v4.sin_port =
3290 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
3291
3292 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
3293 (union sctp_addr *)&prim.ssp_addr);
3294
3295 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
3296 return -EFAULT;
3297
3298 return 0;
3299}
3300
3301/*
3302 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
3303 *
3304 * Requests that the local endpoint set the specified Adaption Layer
3305 * Indication parameter for all future INIT and INIT-ACK exchanges.
3306 */
3307static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
3308 char __user *optval, int __user *optlen)
3309{
3310 __u32 val;
3311
3312 if (len < sizeof(__u32))
3313 return -EINVAL;
3314
3315 len = sizeof(__u32);
3316 val = sctp_sk(sk)->adaption_ind;
3317 if (put_user(len, optlen))
3318 return -EFAULT;
3319 if (copy_to_user(optval, &val, len))
3320 return -EFAULT;
3321 return 0;
3322}
3323
3324/*
3325 *
3326 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
3327 *
3328 * Applications that wish to use the sendto() system call may wish to
3329 * specify a default set of parameters that would normally be supplied
3330 * through the inclusion of ancillary data. This socket option allows
3331 * such an application to set the default sctp_sndrcvinfo structure.
3332
3333
3334 * The application that wishes to use this socket option simply passes
3335 * in to this call the sctp_sndrcvinfo structure defined in Section
3336 * 5.2.2) The input parameters accepted by this call include
3337 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
3338 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
3339 * to this call if the caller is using the UDP model.
3340 *
3341 * For getsockopt, it get the default sctp_sndrcvinfo structure.
3342 */
3343static int sctp_getsockopt_default_send_param(struct sock *sk,
3344 int len, char __user *optval,
3345 int __user *optlen)
3346{
3347 struct sctp_sndrcvinfo info;
3348 struct sctp_association *asoc;
3349 struct sctp_sock *sp = sctp_sk(sk);
3350
3351 if (len != sizeof(struct sctp_sndrcvinfo))
3352 return -EINVAL;
3353 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3354 return -EFAULT;
3355
3356 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3357 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3358 return -EINVAL;
3359
3360 if (asoc) {
3361 info.sinfo_stream = asoc->default_stream;
3362 info.sinfo_flags = asoc->default_flags;
3363 info.sinfo_ppid = asoc->default_ppid;
3364 info.sinfo_context = asoc->default_context;
3365 info.sinfo_timetolive = asoc->default_timetolive;
3366 } else {
3367 info.sinfo_stream = sp->default_stream;
3368 info.sinfo_flags = sp->default_flags;
3369 info.sinfo_ppid = sp->default_ppid;
3370 info.sinfo_context = sp->default_context;
3371 info.sinfo_timetolive = sp->default_timetolive;
3372 }
3373
3374 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3375 return -EFAULT;
3376
3377 return 0;
3378}
3379
3380/*
3381 *
3382 * 7.1.5 SCTP_NODELAY
3383 *
3384 * Turn on/off any Nagle-like algorithm. This means that packets are
3385 * generally sent as soon as possible and no unnecessary delays are
3386 * introduced, at the cost of more packets in the network. Expects an
3387 * integer boolean flag.
3388 */
3389
3390static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3391 char __user *optval, int __user *optlen)
3392{
3393 int val;
3394
3395 if (len < sizeof(int))
3396 return -EINVAL;
3397
3398 len = sizeof(int);
3399 val = (sctp_sk(sk)->nodelay == 1);
3400 if (put_user(len, optlen))
3401 return -EFAULT;
3402 if (copy_to_user(optval, &val, len))
3403 return -EFAULT;
3404 return 0;
3405}
3406
3407/*
3408 *
3409 * 7.1.1 SCTP_RTOINFO
3410 *
3411 * The protocol parameters used to initialize and bound retransmission
3412 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3413 * and modify these parameters.
3414 * All parameters are time values, in milliseconds. A value of 0, when
3415 * modifying the parameters, indicates that the current value should not
3416 * be changed.
3417 *
3418 */
3419static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
3420 char __user *optval,
3421 int __user *optlen) {
3422 struct sctp_rtoinfo rtoinfo;
3423 struct sctp_association *asoc;
3424
3425 if (len != sizeof (struct sctp_rtoinfo))
3426 return -EINVAL;
3427
3428 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3429 return -EFAULT;
3430
3431 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3432
3433 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3434 return -EINVAL;
3435
3436 /* Values corresponding to the specific association. */
3437 if (asoc) {
3438 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
3439 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
3440 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
3441 } else {
3442 /* Values corresponding to the endpoint. */
3443 struct sctp_sock *sp = sctp_sk(sk);
3444
3445 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3446 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3447 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3448 }
3449
3450 if (put_user(len, optlen))
3451 return -EFAULT;
3452
3453 if (copy_to_user(optval, &rtoinfo, len))
3454 return -EFAULT;
3455
3456 return 0;
3457}
3458
3459/*
3460 *
3461 * 7.1.2 SCTP_ASSOCINFO
3462 *
3463 * This option is used to tune the the maximum retransmission attempts
3464 * of the association.
3465 * Returns an error if the new association retransmission value is
3466 * greater than the sum of the retransmission value of the peer.
3467 * See [SCTP] for more information.
3468 *
3469 */
3470static int sctp_getsockopt_associnfo(struct sock *sk, int len,
3471 char __user *optval,
3472 int __user *optlen)
3473{
3474
3475 struct sctp_assocparams assocparams;
3476 struct sctp_association *asoc;
3477 struct list_head *pos;
3478 int cnt = 0;
3479
3480 if (len != sizeof (struct sctp_assocparams))
3481 return -EINVAL;
3482
3483 if (copy_from_user(&assocparams, optval,
3484 sizeof (struct sctp_assocparams)))
3485 return -EFAULT;
3486
3487 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3488
3489 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3490 return -EINVAL;
3491
3492 /* Values correspoinding to the specific association */
17337216 3493 if (asoc) {
1da177e4
LT
3494 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3495 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3496 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3497 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3498 * 1000) +
3499 (asoc->cookie_life.tv_usec
3500 / 1000);
3501
3502 list_for_each(pos, &asoc->peer.transport_addr_list) {
3503 cnt ++;
3504 }
3505
3506 assocparams.sasoc_number_peer_destinations = cnt;
3507 } else {
3508 /* Values corresponding to the endpoint */
3509 struct sctp_sock *sp = sctp_sk(sk);
3510
3511 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3512 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3513 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3514 assocparams.sasoc_cookie_life =
3515 sp->assocparams.sasoc_cookie_life;
3516 assocparams.sasoc_number_peer_destinations =
3517 sp->assocparams.
3518 sasoc_number_peer_destinations;
3519 }
3520
3521 if (put_user(len, optlen))
3522 return -EFAULT;
3523
3524 if (copy_to_user(optval, &assocparams, len))
3525 return -EFAULT;
3526
3527 return 0;
3528}
3529
3530/*
3531 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3532 *
3533 * This socket option is a boolean flag which turns on or off mapped V4
3534 * addresses. If this option is turned on and the socket is type
3535 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3536 * If this option is turned off, then no mapping will be done of V4
3537 * addresses and a user will receive both PF_INET6 and PF_INET type
3538 * addresses on the socket.
3539 */
3540static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3541 char __user *optval, int __user *optlen)
3542{
3543 int val;
3544 struct sctp_sock *sp = sctp_sk(sk);
3545
3546 if (len < sizeof(int))
3547 return -EINVAL;
3548
3549 len = sizeof(int);
3550 val = sp->v4mapped;
3551 if (put_user(len, optlen))
3552 return -EFAULT;
3553 if (copy_to_user(optval, &val, len))
3554 return -EFAULT;
3555
3556 return 0;
3557}
3558
3559/*
3560 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3561 *
3562 * This socket option specifies the maximum size to put in any outgoing
3563 * SCTP chunk. If a message is larger than this size it will be
3564 * fragmented by SCTP into the specified size. Note that the underlying
3565 * SCTP implementation may fragment into smaller sized chunks when the
3566 * PMTU of the underlying association is smaller than the value set by
3567 * the user.
3568 */
3569static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3570 char __user *optval, int __user *optlen)
3571{
3572 int val;
3573
3574 if (len < sizeof(int))
3575 return -EINVAL;
3576
3577 len = sizeof(int);
3578
3579 val = sctp_sk(sk)->user_frag;
3580 if (put_user(len, optlen))
3581 return -EFAULT;
3582 if (copy_to_user(optval, &val, len))
3583 return -EFAULT;
3584
3585 return 0;
3586}
3587
3588SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3589 char __user *optval, int __user *optlen)
3590{
3591 int retval = 0;
3592 int len;
3593
3594 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3595
3596 /* I can hardly begin to describe how wrong this is. This is
3597 * so broken as to be worse than useless. The API draft
3598 * REALLY is NOT helpful here... I am not convinced that the
3599 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3600 * are at all well-founded.
3601 */
3602 if (level != SOL_SCTP) {
3603 struct sctp_af *af = sctp_sk(sk)->pf->af;
3604
3605 retval = af->getsockopt(sk, level, optname, optval, optlen);
3606 return retval;
3607 }
3608
3609 if (get_user(len, optlen))
3610 return -EFAULT;
3611
3612 sctp_lock_sock(sk);
3613
3614 switch (optname) {
3615 case SCTP_STATUS:
3616 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3617 break;
3618 case SCTP_DISABLE_FRAGMENTS:
3619 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3620 optlen);
3621 break;
3622 case SCTP_EVENTS:
3623 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3624 break;
3625 case SCTP_AUTOCLOSE:
3626 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3627 break;
3628 case SCTP_SOCKOPT_PEELOFF:
3629 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3630 break;
3631 case SCTP_PEER_ADDR_PARAMS:
3632 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3633 optlen);
3634 break;
3635 case SCTP_INITMSG:
3636 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3637 break;
3638 case SCTP_GET_PEER_ADDRS_NUM:
3639 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3640 optlen);
3641 break;
3642 case SCTP_GET_LOCAL_ADDRS_NUM:
3643 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3644 optlen);
3645 break;
3646 case SCTP_GET_PEER_ADDRS:
3647 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3648 optlen);
3649 break;
3650 case SCTP_GET_LOCAL_ADDRS:
3651 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3652 optlen);
3653 break;
3654 case SCTP_DEFAULT_SEND_PARAM:
3655 retval = sctp_getsockopt_default_send_param(sk, len,
3656 optval, optlen);
3657 break;
3658 case SCTP_PRIMARY_ADDR:
3659 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3660 break;
3661 case SCTP_NODELAY:
3662 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3663 break;
3664 case SCTP_RTOINFO:
3665 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3666 break;
3667 case SCTP_ASSOCINFO:
3668 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3669 break;
3670 case SCTP_I_WANT_MAPPED_V4_ADDR:
3671 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3672 break;
3673 case SCTP_MAXSEG:
3674 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3675 break;
3676 case SCTP_GET_PEER_ADDR_INFO:
3677 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3678 optlen);
3679 break;
3680 case SCTP_ADAPTION_LAYER:
3681 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
3682 optlen);
3683 break;
3684 default:
3685 retval = -ENOPROTOOPT;
3686 break;
3687 };
3688
3689 sctp_release_sock(sk);
3690 return retval;
3691}
3692
3693static void sctp_hash(struct sock *sk)
3694{
3695 /* STUB */
3696}
3697
3698static void sctp_unhash(struct sock *sk)
3699{
3700 /* STUB */
3701}
3702
3703/* Check if port is acceptable. Possibly find first available port.
3704 *
3705 * The port hash table (contained in the 'global' SCTP protocol storage
3706 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3707 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3708 * list (the list number is the port number hashed out, so as you
3709 * would expect from a hash function, all the ports in a given list have
3710 * such a number that hashes out to the same list number; you were
3711 * expecting that, right?); so each list has a set of ports, with a
3712 * link to the socket (struct sock) that uses it, the port number and
3713 * a fastreuse flag (FIXME: NPI ipg).
3714 */
3715static struct sctp_bind_bucket *sctp_bucket_create(
3716 struct sctp_bind_hashbucket *head, unsigned short snum);
3717
3718static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3719{
3720 struct sctp_bind_hashbucket *head; /* hash list */
3721 struct sctp_bind_bucket *pp; /* hash list port iterator */
3722 unsigned short snum;
3723 int ret;
3724
3725 /* NOTE: Remember to put this back to net order. */
3726 addr->v4.sin_port = ntohs(addr->v4.sin_port);
3727 snum = addr->v4.sin_port;
3728
3729 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3730 sctp_local_bh_disable();
3731
3732 if (snum == 0) {
3733 /* Search for an available port.
3734 *
3735 * 'sctp_port_rover' was the last port assigned, so
3736 * we start to search from 'sctp_port_rover +
3737 * 1'. What we do is first check if port 'rover' is
3738 * already in the hash table; if not, we use that; if
3739 * it is, we try next.
3740 */
3741 int low = sysctl_local_port_range[0];
3742 int high = sysctl_local_port_range[1];
3743 int remaining = (high - low) + 1;
3744 int rover;
3745 int index;
3746
3747 sctp_spin_lock(&sctp_port_alloc_lock);
3748 rover = sctp_port_rover;
3749 do {
3750 rover++;
3751 if ((rover < low) || (rover > high))
3752 rover = low;
3753 index = sctp_phashfn(rover);
3754 head = &sctp_port_hashtable[index];
3755 sctp_spin_lock(&head->lock);
3756 for (pp = head->chain; pp; pp = pp->next)
3757 if (pp->port == rover)
3758 goto next;
3759 break;
3760 next:
3761 sctp_spin_unlock(&head->lock);
3762 } while (--remaining > 0);
3763 sctp_port_rover = rover;
3764 sctp_spin_unlock(&sctp_port_alloc_lock);
3765
3766 /* Exhausted local port range during search? */
3767 ret = 1;
3768 if (remaining <= 0)
3769 goto fail;
3770
3771 /* OK, here is the one we will use. HEAD (the port
3772 * hash table list entry) is non-NULL and we hold it's
3773 * mutex.
3774 */
3775 snum = rover;
3776 } else {
3777 /* We are given an specific port number; we verify
3778 * that it is not being used. If it is used, we will
3779 * exahust the search in the hash list corresponding
3780 * to the port number (snum) - we detect that with the
3781 * port iterator, pp being NULL.
3782 */
3783 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3784 sctp_spin_lock(&head->lock);
3785 for (pp = head->chain; pp; pp = pp->next) {
3786 if (pp->port == snum)
3787 goto pp_found;
3788 }
3789 }
3790 pp = NULL;
3791 goto pp_not_found;
3792pp_found:
3793 if (!hlist_empty(&pp->owner)) {
3794 /* We had a port hash table hit - there is an
3795 * available port (pp != NULL) and it is being
3796 * used by other socket (pp->owner not empty); that other
3797 * socket is going to be sk2.
3798 */
3799 int reuse = sk->sk_reuse;
3800 struct sock *sk2;
3801 struct hlist_node *node;
3802
3803 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3804 if (pp->fastreuse && sk->sk_reuse)
3805 goto success;
3806
3807 /* Run through the list of sockets bound to the port
3808 * (pp->port) [via the pointers bind_next and
3809 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3810 * we get the endpoint they describe and run through
3811 * the endpoint's list of IP (v4 or v6) addresses,
3812 * comparing each of the addresses with the address of
3813 * the socket sk. If we find a match, then that means
3814 * that this port/socket (sk) combination are already
3815 * in an endpoint.
3816 */
3817 sk_for_each_bound(sk2, node, &pp->owner) {
3818 struct sctp_endpoint *ep2;
3819 ep2 = sctp_sk(sk2)->ep;
3820
3821 if (reuse && sk2->sk_reuse)
3822 continue;
3823
3824 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3825 sctp_sk(sk))) {
3826 ret = (long)sk2;
3827 goto fail_unlock;
3828 }
3829 }
3830 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3831 }
3832pp_not_found:
3833 /* If there was a hash table miss, create a new port. */
3834 ret = 1;
3835 if (!pp && !(pp = sctp_bucket_create(head, snum)))
3836 goto fail_unlock;
3837
3838 /* In either case (hit or miss), make sure fastreuse is 1 only
3839 * if sk->sk_reuse is too (that is, if the caller requested
3840 * SO_REUSEADDR on this socket -sk-).
3841 */
3842 if (hlist_empty(&pp->owner))
3843 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3844 else if (pp->fastreuse && !sk->sk_reuse)
3845 pp->fastreuse = 0;
3846
3847 /* We are set, so fill up all the data in the hash table
3848 * entry, tie the socket list information with the rest of the
3849 * sockets FIXME: Blurry, NPI (ipg).
3850 */
3851success:
3852 inet_sk(sk)->num = snum;
3853 if (!sctp_sk(sk)->bind_hash) {
3854 sk_add_bind_node(sk, &pp->owner);
3855 sctp_sk(sk)->bind_hash = pp;
3856 }
3857 ret = 0;
3858
3859fail_unlock:
3860 sctp_spin_unlock(&head->lock);
3861
3862fail:
3863 sctp_local_bh_enable();
3864 addr->v4.sin_port = htons(addr->v4.sin_port);
3865 return ret;
3866}
3867
3868/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
3869 * port is requested.
3870 */
3871static int sctp_get_port(struct sock *sk, unsigned short snum)
3872{
3873 long ret;
3874 union sctp_addr addr;
3875 struct sctp_af *af = sctp_sk(sk)->pf->af;
3876
3877 /* Set up a dummy address struct from the sk. */
3878 af->from_sk(&addr, sk);
3879 addr.v4.sin_port = htons(snum);
3880
3881 /* Note: sk->sk_num gets filled in if ephemeral port request. */
3882 ret = sctp_get_port_local(sk, &addr);
3883
3884 return (ret ? 1 : 0);
3885}
3886
3887/*
3888 * 3.1.3 listen() - UDP Style Syntax
3889 *
3890 * By default, new associations are not accepted for UDP style sockets.
3891 * An application uses listen() to mark a socket as being able to
3892 * accept new associations.
3893 */
3894SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3895{
3896 struct sctp_sock *sp = sctp_sk(sk);
3897 struct sctp_endpoint *ep = sp->ep;
3898
3899 /* Only UDP style sockets that are not peeled off are allowed to
3900 * listen().
3901 */
3902 if (!sctp_style(sk, UDP))
3903 return -EINVAL;
3904
3905 /* If backlog is zero, disable listening. */
3906 if (!backlog) {
3907 if (sctp_sstate(sk, CLOSED))
3908 return 0;
3909
3910 sctp_unhash_endpoint(ep);
3911 sk->sk_state = SCTP_SS_CLOSED;
3912 }
3913
3914 /* Return if we are already listening. */
3915 if (sctp_sstate(sk, LISTENING))
3916 return 0;
3917
3918 /*
3919 * If a bind() or sctp_bindx() is not called prior to a listen()
3920 * call that allows new associations to be accepted, the system
3921 * picks an ephemeral port and will choose an address set equivalent
3922 * to binding with a wildcard address.
3923 *
3924 * This is not currently spelled out in the SCTP sockets
3925 * extensions draft, but follows the practice as seen in TCP
3926 * sockets.
3927 */
3928 if (!ep->base.bind_addr.port) {
3929 if (sctp_autobind(sk))
3930 return -EAGAIN;
3931 }
3932 sk->sk_state = SCTP_SS_LISTENING;
3933 sctp_hash_endpoint(ep);
3934 return 0;
3935}
3936
3937/*
3938 * 4.1.3 listen() - TCP Style Syntax
3939 *
3940 * Applications uses listen() to ready the SCTP endpoint for accepting
3941 * inbound associations.
3942 */
3943SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3944{
3945 struct sctp_sock *sp = sctp_sk(sk);
3946 struct sctp_endpoint *ep = sp->ep;
3947
3948 /* If backlog is zero, disable listening. */
3949 if (!backlog) {
3950 if (sctp_sstate(sk, CLOSED))
3951 return 0;
3952
3953 sctp_unhash_endpoint(ep);
3954 sk->sk_state = SCTP_SS_CLOSED;
3955 }
3956
3957 if (sctp_sstate(sk, LISTENING))
3958 return 0;
3959
3960 /*
3961 * If a bind() or sctp_bindx() is not called prior to a listen()
3962 * call that allows new associations to be accepted, the system
3963 * picks an ephemeral port and will choose an address set equivalent
3964 * to binding with a wildcard address.
3965 *
3966 * This is not currently spelled out in the SCTP sockets
3967 * extensions draft, but follows the practice as seen in TCP
3968 * sockets.
3969 */
3970 if (!ep->base.bind_addr.port) {
3971 if (sctp_autobind(sk))
3972 return -EAGAIN;
3973 }
3974 sk->sk_state = SCTP_SS_LISTENING;
3975 sk->sk_max_ack_backlog = backlog;
3976 sctp_hash_endpoint(ep);
3977 return 0;
3978}
3979
3980/*
3981 * Move a socket to LISTENING state.
3982 */
3983int sctp_inet_listen(struct socket *sock, int backlog)
3984{
3985 struct sock *sk = sock->sk;
3986 struct crypto_tfm *tfm=NULL;
3987 int err = -EINVAL;
3988
3989 if (unlikely(backlog < 0))
3990 goto out;
3991
3992 sctp_lock_sock(sk);
3993
3994 if (sock->state != SS_UNCONNECTED)
3995 goto out;
3996
3997 /* Allocate HMAC for generating cookie. */
3998 if (sctp_hmac_alg) {
3999 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4000 if (!tfm) {
4001 err = -ENOSYS;
4002 goto out;
4003 }
4004 }
4005
4006 switch (sock->type) {
4007 case SOCK_SEQPACKET:
4008 err = sctp_seqpacket_listen(sk, backlog);
4009 break;
4010 case SOCK_STREAM:
4011 err = sctp_stream_listen(sk, backlog);
4012 break;
4013 default:
4014 break;
4015 };
4016 if (err)
4017 goto cleanup;
4018
4019 /* Store away the transform reference. */
4020 sctp_sk(sk)->hmac = tfm;
4021out:
4022 sctp_release_sock(sk);
4023 return err;
4024cleanup:
4025 if (tfm)
4026 sctp_crypto_free_tfm(tfm);
4027 goto out;
4028}
4029
4030/*
4031 * This function is done by modeling the current datagram_poll() and the
4032 * tcp_poll(). Note that, based on these implementations, we don't
4033 * lock the socket in this function, even though it seems that,
4034 * ideally, locking or some other mechanisms can be used to ensure
4035 * the integrity of the counters (sndbuf and wmem_queued) used
4036 * in this place. We assume that we don't need locks either until proven
4037 * otherwise.
4038 *
4039 * Another thing to note is that we include the Async I/O support
4040 * here, again, by modeling the current TCP/UDP code. We don't have
4041 * a good way to test with it yet.
4042 */
4043unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4044{
4045 struct sock *sk = sock->sk;
4046 struct sctp_sock *sp = sctp_sk(sk);
4047 unsigned int mask;
4048
4049 poll_wait(file, sk->sk_sleep, wait);
4050
4051 /* A TCP-style listening socket becomes readable when the accept queue
4052 * is not empty.
4053 */
4054 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4055 return (!list_empty(&sp->ep->asocs)) ?
4056 (POLLIN | POLLRDNORM) : 0;
4057
4058 mask = 0;
4059
4060 /* Is there any exceptional events? */
4061 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4062 mask |= POLLERR;
4063 if (sk->sk_shutdown == SHUTDOWN_MASK)
4064 mask |= POLLHUP;
4065
4066 /* Is it readable? Reconsider this code with TCP-style support. */
4067 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4068 (sk->sk_shutdown & RCV_SHUTDOWN))
4069 mask |= POLLIN | POLLRDNORM;
4070
4071 /* The association is either gone or not ready. */
4072 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4073 return mask;
4074
4075 /* Is it writable? */
4076 if (sctp_writeable(sk)) {
4077 mask |= POLLOUT | POLLWRNORM;
4078 } else {
4079 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4080 /*
4081 * Since the socket is not locked, the buffer
4082 * might be made available after the writeable check and
4083 * before the bit is set. This could cause a lost I/O
4084 * signal. tcp_poll() has a race breaker for this race
4085 * condition. Based on their implementation, we put
4086 * in the following code to cover it as well.
4087 */
4088 if (sctp_writeable(sk))
4089 mask |= POLLOUT | POLLWRNORM;
4090 }
4091 return mask;
4092}
4093
4094/********************************************************************
4095 * 2nd Level Abstractions
4096 ********************************************************************/
4097
4098static struct sctp_bind_bucket *sctp_bucket_create(
4099 struct sctp_bind_hashbucket *head, unsigned short snum)
4100{
4101 struct sctp_bind_bucket *pp;
4102
4103 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4104 SCTP_DBG_OBJCNT_INC(bind_bucket);
4105 if (pp) {
4106 pp->port = snum;
4107 pp->fastreuse = 0;
4108 INIT_HLIST_HEAD(&pp->owner);
4109 if ((pp->next = head->chain) != NULL)
4110 pp->next->pprev = &pp->next;
4111 head->chain = pp;
4112 pp->pprev = &head->chain;
4113 }
4114 return pp;
4115}
4116
4117/* Caller must hold hashbucket lock for this tb with local BH disabled */
4118static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4119{
4120 if (hlist_empty(&pp->owner)) {
4121 if (pp->next)
4122 pp->next->pprev = pp->pprev;
4123 *(pp->pprev) = pp->next;
4124 kmem_cache_free(sctp_bucket_cachep, pp);
4125 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4126 }
4127}
4128
4129/* Release this socket's reference to a local port. */
4130static inline void __sctp_put_port(struct sock *sk)
4131{
4132 struct sctp_bind_hashbucket *head =
4133 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4134 struct sctp_bind_bucket *pp;
4135
4136 sctp_spin_lock(&head->lock);
4137 pp = sctp_sk(sk)->bind_hash;
4138 __sk_del_bind_node(sk);
4139 sctp_sk(sk)->bind_hash = NULL;
4140 inet_sk(sk)->num = 0;
4141 sctp_bucket_destroy(pp);
4142 sctp_spin_unlock(&head->lock);
4143}
4144
4145void sctp_put_port(struct sock *sk)
4146{
4147 sctp_local_bh_disable();
4148 __sctp_put_port(sk);
4149 sctp_local_bh_enable();
4150}
4151
4152/*
4153 * The system picks an ephemeral port and choose an address set equivalent
4154 * to binding with a wildcard address.
4155 * One of those addresses will be the primary address for the association.
4156 * This automatically enables the multihoming capability of SCTP.
4157 */
4158static int sctp_autobind(struct sock *sk)
4159{
4160 union sctp_addr autoaddr;
4161 struct sctp_af *af;
4162 unsigned short port;
4163
4164 /* Initialize a local sockaddr structure to INADDR_ANY. */
4165 af = sctp_sk(sk)->pf->af;
4166
4167 port = htons(inet_sk(sk)->num);
4168 af->inaddr_any(&autoaddr, port);
4169
4170 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
4171}
4172
4173/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
4174 *
4175 * From RFC 2292
4176 * 4.2 The cmsghdr Structure *
4177 *
4178 * When ancillary data is sent or received, any number of ancillary data
4179 * objects can be specified by the msg_control and msg_controllen members of
4180 * the msghdr structure, because each object is preceded by
4181 * a cmsghdr structure defining the object's length (the cmsg_len member).
4182 * Historically Berkeley-derived implementations have passed only one object
4183 * at a time, but this API allows multiple objects to be
4184 * passed in a single call to sendmsg() or recvmsg(). The following example
4185 * shows two ancillary data objects in a control buffer.
4186 *
4187 * |<--------------------------- msg_controllen -------------------------->|
4188 * | |
4189 *
4190 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
4191 *
4192 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
4193 * | | |
4194 *
4195 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
4196 *
4197 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
4198 * | | | | |
4199 *
4200 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4201 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
4202 *
4203 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
4204 *
4205 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
4206 * ^
4207 * |
4208 *
4209 * msg_control
4210 * points here
4211 */
4212SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
4213 sctp_cmsgs_t *cmsgs)
4214{
4215 struct cmsghdr *cmsg;
4216
4217 for (cmsg = CMSG_FIRSTHDR(msg);
4218 cmsg != NULL;
4219 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
4220 if (!CMSG_OK(msg, cmsg))
4221 return -EINVAL;
4222
4223 /* Should we parse this header or ignore? */
4224 if (cmsg->cmsg_level != IPPROTO_SCTP)
4225 continue;
4226
4227 /* Strictly check lengths following example in SCM code. */
4228 switch (cmsg->cmsg_type) {
4229 case SCTP_INIT:
4230 /* SCTP Socket API Extension
4231 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
4232 *
4233 * This cmsghdr structure provides information for
4234 * initializing new SCTP associations with sendmsg().
4235 * The SCTP_INITMSG socket option uses this same data
4236 * structure. This structure is not used for
4237 * recvmsg().
4238 *
4239 * cmsg_level cmsg_type cmsg_data[]
4240 * ------------ ------------ ----------------------
4241 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
4242 */
4243 if (cmsg->cmsg_len !=
4244 CMSG_LEN(sizeof(struct sctp_initmsg)))
4245 return -EINVAL;
4246 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
4247 break;
4248
4249 case SCTP_SNDRCV:
4250 /* SCTP Socket API Extension
4251 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
4252 *
4253 * This cmsghdr structure specifies SCTP options for
4254 * sendmsg() and describes SCTP header information
4255 * about a received message through recvmsg().
4256 *
4257 * cmsg_level cmsg_type cmsg_data[]
4258 * ------------ ------------ ----------------------
4259 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
4260 */
4261 if (cmsg->cmsg_len !=
4262 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
4263 return -EINVAL;
4264
4265 cmsgs->info =
4266 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
4267
4268 /* Minimally, validate the sinfo_flags. */
4269 if (cmsgs->info->sinfo_flags &
4270 ~(MSG_UNORDERED | MSG_ADDR_OVER |
4271 MSG_ABORT | MSG_EOF))
4272 return -EINVAL;
4273 break;
4274
4275 default:
4276 return -EINVAL;
4277 };
4278 }
4279 return 0;
4280}
4281
4282/*
4283 * Wait for a packet..
4284 * Note: This function is the same function as in core/datagram.c
4285 * with a few modifications to make lksctp work.
4286 */
4287static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
4288{
4289 int error;
4290 DEFINE_WAIT(wait);
4291
4292 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4293
4294 /* Socket errors? */
4295 error = sock_error(sk);
4296 if (error)
4297 goto out;
4298
4299 if (!skb_queue_empty(&sk->sk_receive_queue))
4300 goto ready;
4301
4302 /* Socket shut down? */
4303 if (sk->sk_shutdown & RCV_SHUTDOWN)
4304 goto out;
4305
4306 /* Sequenced packets can come disconnected. If so we report the
4307 * problem.
4308 */
4309 error = -ENOTCONN;
4310
4311 /* Is there a good reason to think that we may receive some data? */
4312 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
4313 goto out;
4314
4315 /* Handle signals. */
4316 if (signal_pending(current))
4317 goto interrupted;
4318
4319 /* Let another process have a go. Since we are going to sleep
4320 * anyway. Note: This may cause odd behaviors if the message
4321 * does not fit in the user's buffer, but this seems to be the
4322 * only way to honor MSG_DONTWAIT realistically.
4323 */
4324 sctp_release_sock(sk);
4325 *timeo_p = schedule_timeout(*timeo_p);
4326 sctp_lock_sock(sk);
4327
4328ready:
4329 finish_wait(sk->sk_sleep, &wait);
4330 return 0;
4331
4332interrupted:
4333 error = sock_intr_errno(*timeo_p);
4334
4335out:
4336 finish_wait(sk->sk_sleep, &wait);
4337 *err = error;
4338 return error;
4339}
4340
4341/* Receive a datagram.
4342 * Note: This is pretty much the same routine as in core/datagram.c
4343 * with a few changes to make lksctp work.
4344 */
4345static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
4346 int noblock, int *err)
4347{
4348 int error;
4349 struct sk_buff *skb;
4350 long timeo;
4351
4352 /* Caller is allowed not to check sk->sk_err before calling. */
4353 error = sock_error(sk);
4354 if (error)
4355 goto no_packet;
4356
4357 timeo = sock_rcvtimeo(sk, noblock);
4358
4359 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4360 timeo, MAX_SCHEDULE_TIMEOUT);
4361
4362 do {
4363 /* Again only user level code calls this function,
4364 * so nothing interrupt level
4365 * will suddenly eat the receive_queue.
4366 *
4367 * Look at current nfs client by the way...
4368 * However, this function was corrent in any case. 8)
4369 */
4370 if (flags & MSG_PEEK) {
1e061ab2 4371 spin_lock_bh(&sk->sk_receive_queue.lock);
1da177e4
LT
4372 skb = skb_peek(&sk->sk_receive_queue);
4373 if (skb)
4374 atomic_inc(&skb->users);
1e061ab2 4375 spin_unlock_bh(&sk->sk_receive_queue.lock);
1da177e4
LT
4376 } else {
4377 skb = skb_dequeue(&sk->sk_receive_queue);
4378 }
4379
4380 if (skb)
4381 return skb;
4382
4383 if (sk->sk_shutdown & RCV_SHUTDOWN)
4384 break;
4385
4386 /* User doesn't want to wait. */
4387 error = -EAGAIN;
4388 if (!timeo)
4389 goto no_packet;
4390 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4391
4392 return NULL;
4393
4394no_packet:
4395 *err = error;
4396 return NULL;
4397}
4398
4399/* If sndbuf has changed, wake up per association sndbuf waiters. */
4400static void __sctp_write_space(struct sctp_association *asoc)
4401{
4402 struct sock *sk = asoc->base.sk;
4403 struct socket *sock = sk->sk_socket;
4404
4405 if ((sctp_wspace(asoc) > 0) && sock) {
4406 if (waitqueue_active(&asoc->wait))
4407 wake_up_interruptible(&asoc->wait);
4408
4409 if (sctp_writeable(sk)) {
4410 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4411 wake_up_interruptible(sk->sk_sleep);
4412
4413 /* Note that we try to include the Async I/O support
4414 * here by modeling from the current TCP/UDP code.
4415 * We have not tested with it yet.
4416 */
4417 if (sock->fasync_list &&
4418 !(sk->sk_shutdown & SEND_SHUTDOWN))
4419 sock_wake_async(sock, 2, POLL_OUT);
4420 }
4421 }
4422}
4423
4424/* Do accounting for the sndbuf space.
4425 * Decrement the used sndbuf space of the corresponding association by the
4426 * data size which was just transmitted(freed).
4427 */
4428static void sctp_wfree(struct sk_buff *skb)
4429{
4430 struct sctp_association *asoc;
4431 struct sctp_chunk *chunk;
4432 struct sock *sk;
4433
4434 /* Get the saved chunk pointer. */
4435 chunk = *((struct sctp_chunk **)(skb->cb));
4436 asoc = chunk->asoc;
4437 sk = asoc->base.sk;
4eb701df
NH
4438 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
4439 sizeof(struct sk_buff) +
4440 sizeof(struct sctp_chunk);
4441
4442 sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
4443 sizeof(struct sk_buff) +
4444 sizeof(struct sctp_chunk);
4445
4446 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
4447
4448 sock_wfree(skb);
1da177e4
LT
4449 __sctp_write_space(asoc);
4450
4451 sctp_association_put(asoc);
4452}
4453
4454/* Helper function to wait for space in the sndbuf. */
4455static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4456 size_t msg_len)
4457{
4458 struct sock *sk = asoc->base.sk;
4459 int err = 0;
4460 long current_timeo = *timeo_p;
4461 DEFINE_WAIT(wait);
4462
4463 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
4464 asoc, (long)(*timeo_p), msg_len);
4465
4466 /* Increment the association's refcnt. */
4467 sctp_association_hold(asoc);
4468
4469 /* Wait on the association specific sndbuf space. */
4470 for (;;) {
4471 prepare_to_wait_exclusive(&asoc->wait, &wait,
4472 TASK_INTERRUPTIBLE);
4473 if (!*timeo_p)
4474 goto do_nonblock;
4475 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4476 asoc->base.dead)
4477 goto do_error;
4478 if (signal_pending(current))
4479 goto do_interrupted;
4480 if (msg_len <= sctp_wspace(asoc))
4481 break;
4482
4483 /* Let another process have a go. Since we are going
4484 * to sleep anyway.
4485 */
4486 sctp_release_sock(sk);
4487 current_timeo = schedule_timeout(current_timeo);
4488 sctp_lock_sock(sk);
4489
4490 *timeo_p = current_timeo;
4491 }
4492
4493out:
4494 finish_wait(&asoc->wait, &wait);
4495
4496 /* Release the association's refcnt. */
4497 sctp_association_put(asoc);
4498
4499 return err;
4500
4501do_error:
4502 err = -EPIPE;
4503 goto out;
4504
4505do_interrupted:
4506 err = sock_intr_errno(*timeo_p);
4507 goto out;
4508
4509do_nonblock:
4510 err = -EAGAIN;
4511 goto out;
4512}
4513
4514/* If socket sndbuf has changed, wake up all per association waiters. */
4515void sctp_write_space(struct sock *sk)
4516{
4517 struct sctp_association *asoc;
4518 struct list_head *pos;
4519
4520 /* Wake up the tasks in each wait queue. */
4521 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4522 asoc = list_entry(pos, struct sctp_association, asocs);
4523 __sctp_write_space(asoc);
4524 }
4525}
4526
4527/* Is there any sndbuf space available on the socket?
4528 *
4529 * Note that wmem_queued is the sum of the send buffers on all of the
4530 * associations on the same socket. For a UDP-style socket with
4531 * multiple associations, it is possible for it to be "unwriteable"
4532 * prematurely. I assume that this is acceptable because
4533 * a premature "unwriteable" is better than an accidental "writeable" which
4534 * would cause an unwanted block under certain circumstances. For the 1-1
4535 * UDP-style sockets or TCP-style sockets, this code should work.
4536 * - Daisy
4537 */
4538static int sctp_writeable(struct sock *sk)
4539{
4540 int amt = 0;
4541
4542 amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4543 if (amt < 0)
4544 amt = 0;
4545 return amt;
4546}
4547
4548/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4549 * returns immediately with EINPROGRESS.
4550 */
4551static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4552{
4553 struct sock *sk = asoc->base.sk;
4554 int err = 0;
4555 long current_timeo = *timeo_p;
4556 DEFINE_WAIT(wait);
4557
4558 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4559 (long)(*timeo_p));
4560
4561 /* Increment the association's refcnt. */
4562 sctp_association_hold(asoc);
4563
4564 for (;;) {
4565 prepare_to_wait_exclusive(&asoc->wait, &wait,
4566 TASK_INTERRUPTIBLE);
4567 if (!*timeo_p)
4568 goto do_nonblock;
4569 if (sk->sk_shutdown & RCV_SHUTDOWN)
4570 break;
4571 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4572 asoc->base.dead)
4573 goto do_error;
4574 if (signal_pending(current))
4575 goto do_interrupted;
4576
4577 if (sctp_state(asoc, ESTABLISHED))
4578 break;
4579
4580 /* Let another process have a go. Since we are going
4581 * to sleep anyway.
4582 */
4583 sctp_release_sock(sk);
4584 current_timeo = schedule_timeout(current_timeo);
4585 sctp_lock_sock(sk);
4586
4587 *timeo_p = current_timeo;
4588 }
4589
4590out:
4591 finish_wait(&asoc->wait, &wait);
4592
4593 /* Release the association's refcnt. */
4594 sctp_association_put(asoc);
4595
4596 return err;
4597
4598do_error:
4599 if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
4600 asoc->max_init_attempts)
4601 err = -ETIMEDOUT;
4602 else
4603 err = -ECONNREFUSED;
4604 goto out;
4605
4606do_interrupted:
4607 err = sock_intr_errno(*timeo_p);
4608 goto out;
4609
4610do_nonblock:
4611 err = -EINPROGRESS;
4612 goto out;
4613}
4614
4615static int sctp_wait_for_accept(struct sock *sk, long timeo)
4616{
4617 struct sctp_endpoint *ep;
4618 int err = 0;
4619 DEFINE_WAIT(wait);
4620
4621 ep = sctp_sk(sk)->ep;
4622
4623
4624 for (;;) {
4625 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4626 TASK_INTERRUPTIBLE);
4627
4628 if (list_empty(&ep->asocs)) {
4629 sctp_release_sock(sk);
4630 timeo = schedule_timeout(timeo);
4631 sctp_lock_sock(sk);
4632 }
4633
4634 err = -EINVAL;
4635 if (!sctp_sstate(sk, LISTENING))
4636 break;
4637
4638 err = 0;
4639 if (!list_empty(&ep->asocs))
4640 break;
4641
4642 err = sock_intr_errno(timeo);
4643 if (signal_pending(current))
4644 break;
4645
4646 err = -EAGAIN;
4647 if (!timeo)
4648 break;
4649 }
4650
4651 finish_wait(sk->sk_sleep, &wait);
4652
4653 return err;
4654}
4655
4656void sctp_wait_for_close(struct sock *sk, long timeout)
4657{
4658 DEFINE_WAIT(wait);
4659
4660 do {
4661 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4662 if (list_empty(&sctp_sk(sk)->ep->asocs))
4663 break;
4664 sctp_release_sock(sk);
4665 timeout = schedule_timeout(timeout);
4666 sctp_lock_sock(sk);
4667 } while (!signal_pending(current) && timeout);
4668
4669 finish_wait(sk->sk_sleep, &wait);
4670}
4671
4672/* Populate the fields of the newsk from the oldsk and migrate the assoc
4673 * and its messages to the newsk.
4674 */
4675static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4676 struct sctp_association *assoc,
4677 sctp_socket_type_t type)
4678{
4679 struct sctp_sock *oldsp = sctp_sk(oldsk);
4680 struct sctp_sock *newsp = sctp_sk(newsk);
4681 struct sctp_bind_bucket *pp; /* hash list port iterator */
4682 struct sctp_endpoint *newep = newsp->ep;
4683 struct sk_buff *skb, *tmp;
4684 struct sctp_ulpevent *event;
4243cac1 4685 int flags = 0;
1da177e4
LT
4686
4687 /* Migrate socket buffer sizes and all the socket level options to the
4688 * new socket.
4689 */
4690 newsk->sk_sndbuf = oldsk->sk_sndbuf;
4691 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4692 /* Brute force copy old sctp opt. */
4693 inet_sk_copy_descendant(newsk, oldsk);
4694
4695 /* Restore the ep value that was overwritten with the above structure
4696 * copy.
4697 */
4698 newsp->ep = newep;
4699 newsp->hmac = NULL;
4700
4701 /* Hook this new socket in to the bind_hash list. */
4702 pp = sctp_sk(oldsk)->bind_hash;
4703 sk_add_bind_node(newsk, &pp->owner);
4704 sctp_sk(newsk)->bind_hash = pp;
4705 inet_sk(newsk)->num = inet_sk(oldsk)->num;
4706
4243cac1
VY
4707 /* Copy the bind_addr list from the original endpoint to the new
4708 * endpoint so that we can handle restarts properly
4709 */
4710 if (assoc->peer.ipv4_address)
4711 flags |= SCTP_ADDR4_PEERSUPP;
4712 if (assoc->peer.ipv6_address)
4713 flags |= SCTP_ADDR6_PEERSUPP;
4714 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
4715 &oldsp->ep->base.bind_addr,
4716 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
4717
1da177e4
LT
4718 /* Move any messages in the old socket's receive queue that are for the
4719 * peeled off association to the new socket's receive queue.
4720 */
4721 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4722 event = sctp_skb2event(skb);
4723 if (event->asoc == assoc) {
4724 __skb_unlink(skb, skb->list);
4725 __skb_queue_tail(&newsk->sk_receive_queue, skb);
4726 }
4727 }
4728
4729 /* Clean up any messages pending delivery due to partial
4730 * delivery. Three cases:
4731 * 1) No partial deliver; no work.
4732 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4733 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
4734 */
4735 skb_queue_head_init(&newsp->pd_lobby);
4736 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
4737
4738 if (sctp_sk(oldsk)->pd_mode) {
4739 struct sk_buff_head *queue;
4740
4741 /* Decide which queue to move pd_lobby skbs to. */
4742 if (assoc->ulpq.pd_mode) {
4743 queue = &newsp->pd_lobby;
4744 } else
4745 queue = &newsk->sk_receive_queue;
4746
4747 /* Walk through the pd_lobby, looking for skbs that
4748 * need moved to the new socket.
4749 */
4750 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4751 event = sctp_skb2event(skb);
4752 if (event->asoc == assoc) {
4753 __skb_unlink(skb, skb->list);
4754 __skb_queue_tail(queue, skb);
4755 }
4756 }
4757
4758 /* Clear up any skbs waiting for the partial
4759 * delivery to finish.
4760 */
4761 if (assoc->ulpq.pd_mode)
4762 sctp_clear_pd(oldsk);
4763
4764 }
4765
4766 /* Set the type of socket to indicate that it is peeled off from the
4767 * original UDP-style socket or created with the accept() call on a
4768 * TCP-style socket..
4769 */
4770 newsp->type = type;
4771
4772 /* Migrate the association to the new socket. */
4773 sctp_assoc_migrate(assoc, newsk);
4774
4775 /* If the association on the newsk is already closed before accept()
4776 * is called, set RCV_SHUTDOWN flag.
4777 */
4778 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4779 newsk->sk_shutdown |= RCV_SHUTDOWN;
4780
4781 newsk->sk_state = SCTP_SS_ESTABLISHED;
4782}
4783
4784/* This proto struct describes the ULP interface for SCTP. */
4785struct proto sctp_prot = {
4786 .name = "SCTP",
4787 .owner = THIS_MODULE,
4788 .close = sctp_close,
4789 .connect = sctp_connect,
4790 .disconnect = sctp_disconnect,
4791 .accept = sctp_accept,
4792 .ioctl = sctp_ioctl,
4793 .init = sctp_init_sock,
4794 .destroy = sctp_destroy_sock,
4795 .shutdown = sctp_shutdown,
4796 .setsockopt = sctp_setsockopt,
4797 .getsockopt = sctp_getsockopt,
4798 .sendmsg = sctp_sendmsg,
4799 .recvmsg = sctp_recvmsg,
4800 .bind = sctp_bind,
4801 .backlog_rcv = sctp_backlog_rcv,
4802 .hash = sctp_hash,
4803 .unhash = sctp_unhash,
4804 .get_port = sctp_get_port,
4805 .obj_size = sizeof(struct sctp_sock),
4806};
4807
4808#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4809struct proto sctpv6_prot = {
4810 .name = "SCTPv6",
4811 .owner = THIS_MODULE,
4812 .close = sctp_close,
4813 .connect = sctp_connect,
4814 .disconnect = sctp_disconnect,
4815 .accept = sctp_accept,
4816 .ioctl = sctp_ioctl,
4817 .init = sctp_init_sock,
4818 .destroy = sctp_destroy_sock,
4819 .shutdown = sctp_shutdown,
4820 .setsockopt = sctp_setsockopt,
4821 .getsockopt = sctp_getsockopt,
4822 .sendmsg = sctp_sendmsg,
4823 .recvmsg = sctp_recvmsg,
4824 .bind = sctp_bind,
4825 .backlog_rcv = sctp_backlog_rcv,
4826 .hash = sctp_hash,
4827 .unhash = sctp_unhash,
4828 .get_port = sctp_get_port,
4829 .obj_size = sizeof(struct sctp6_sock),
4830};
4831#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */