[NET] SCHED: Fix whitespace errors.
[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
1da177e4
LT
60#include <linux/types.h>
61#include <linux/kernel.h>
62#include <linux/wait.h>
63#include <linux/time.h>
64#include <linux/ip.h>
4fc268d2 65#include <linux/capability.h>
1da177e4
LT
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
e18b890b 110extern struct kmem_cache *sctp_bucket_cachep;
1da177e4
LT
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
4eb701df 159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
1da177e4
LT
160}
161
162/* Verify that this is a valid address. */
163static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
164 int len)
165{
166 struct sctp_af *af;
167
168 /* Verify basic sockaddr. */
169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
170 if (!af)
171 return -EINVAL;
172
173 /* Is this a valid SCTP address? */
5636bef7 174 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
1da177e4
LT
175 return -EINVAL;
176
177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
178 return -EINVAL;
179
180 return 0;
181}
182
183/* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
185 */
186struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
187{
188 struct sctp_association *asoc = NULL;
189
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk, UDP)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
195 */
196 if (!sctp_sstate(sk, ESTABLISHED))
197 return NULL;
198
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk)->ep->asocs))
201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
202 struct sctp_association, asocs);
203 return asoc;
204 }
205
206 /* Otherwise this is a UDP-style socket. */
207 if (!id || (id == (sctp_assoc_t)-1))
208 return NULL;
209
210 spin_lock_bh(&sctp_assocs_id_lock);
211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
212 spin_unlock_bh(&sctp_assocs_id_lock);
213
214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
215 return NULL;
216
217 return asoc;
218}
219
220/* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
222 * the same.
223 */
224static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
225 struct sockaddr_storage *addr,
226 sctp_assoc_t id)
227{
228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
229 struct sctp_transport *transport;
230 union sctp_addr *laddr = (union sctp_addr *)addr;
231
1da177e4 232 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
cd4ff034 233 laddr,
1da177e4 234 &transport);
1da177e4
LT
235
236 if (!addr_asoc)
237 return NULL;
238
239 id_asoc = sctp_id2assoc(sk, id);
240 if (id_asoc && (id_asoc != addr_asoc))
241 return NULL;
242
243 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
244 (union sctp_addr *)addr);
245
246 return transport;
247}
248
249/* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
251 *
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
253 *
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
258 */
3f7a87d2 259SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
1da177e4
LT
260{
261 int retval = 0;
262
263 sctp_lock_sock(sk);
264
3f7a87d2
FF
265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
266 sk, addr, addr_len);
1da177e4
LT
267
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk)->ep->base.bind_addr.port)
3f7a87d2 270 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
1da177e4
LT
271 addr_len);
272 else
273 retval = -EINVAL;
274
275 sctp_release_sock(sk);
276
277 return retval;
278}
279
280static long sctp_get_port_local(struct sock *, union sctp_addr *);
281
282/* Verify this is a valid sockaddr. */
283static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
284 union sctp_addr *addr, int len)
285{
286 struct sctp_af *af;
287
288 /* Check minimum size. */
289 if (len < sizeof (struct sockaddr))
290 return NULL;
291
292 /* Does this PF support this AF? */
293 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
294 return NULL;
295
296 /* If we get this far, af is valid. */
297 af = sctp_get_af_specific(addr->sa.sa_family);
298
299 if (len < af->sockaddr_len)
300 return NULL;
301
302 return af;
303}
304
305/* Bind a local address either to an endpoint or to an association. */
306SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
307{
308 struct sctp_sock *sp = sctp_sk(sk);
309 struct sctp_endpoint *ep = sp->ep;
310 struct sctp_bind_addr *bp = &ep->base.bind_addr;
311 struct sctp_af *af;
312 unsigned short snum;
313 int ret = 0;
314
1da177e4
LT
315 /* Common sockaddr verification. */
316 af = sctp_sockaddr_af(sp, addr, len);
3f7a87d2
FF
317 if (!af) {
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
319 sk, addr, len);
1da177e4 320 return -EINVAL;
3f7a87d2
FF
321 }
322
323 snum = ntohs(addr->v4.sin_port);
324
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
327 sk,
328 addr,
329 bp->port, snum,
330 len);
1da177e4
LT
331
332 /* PF specific bind() address verification. */
333 if (!sp->pf->bind_verify(sp, addr))
334 return -EADDRNOTAVAIL;
335
1da177e4
LT
336 /* We must either be unbound, or bind to the same port. */
337 if (bp->port && (snum != bp->port)) {
338 SCTP_DEBUG_PRINTK("sctp_do_bind:"
339 " New port %d does not match existing port "
340 "%d.\n", snum, bp->port);
341 return -EINVAL;
342 }
343
344 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
345 return -EACCES;
346
347 /* Make sure we are allowed to bind here.
348 * The function sctp_get_port_local() does duplicate address
349 * detection.
350 */
351 if ((ret = sctp_get_port_local(sk, addr))) {
352 if (ret == (long) sk) {
353 /* This endpoint has a conflicting address. */
354 return -EINVAL;
355 } else {
356 return -EADDRINUSE;
357 }
358 }
359
360 /* Refresh ephemeral port. */
361 if (!bp->port)
362 bp->port = inet_sk(sk)->num;
363
364 /* Add the address to the bind address list. */
365 sctp_local_bh_disable();
366 sctp_write_lock(&ep->base.addr_lock);
367
368 /* Use GFP_ATOMIC since BHs are disabled. */
5ab7b859 369 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
1da177e4
LT
370 sctp_write_unlock(&ep->base.addr_lock);
371 sctp_local_bh_enable();
372
373 /* Copy back into socket for getsockname() use. */
374 if (!ret) {
375 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
376 af->to_sk_saddr(addr, sk);
377 }
378
379 return ret;
380}
381
382 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
383 *
384 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
385 * at any one time. If a sender, after sending an ASCONF chunk, decides
386 * it needs to transfer another ASCONF Chunk, it MUST wait until the
387 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
388 * subsequent ASCONF. Note this restriction binds each side, so at any
389 * time two ASCONF may be in-transit on any given association (one sent
390 * from each endpoint).
391 */
392static int sctp_send_asconf(struct sctp_association *asoc,
393 struct sctp_chunk *chunk)
394{
395 int retval = 0;
396
397 /* If there is an outstanding ASCONF chunk, queue it for later
398 * transmission.
399 */
400 if (asoc->addip_last_asconf) {
79af02c2 401 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
1da177e4
LT
402 goto out;
403 }
404
405 /* Hold the chunk until an ASCONF_ACK is received. */
406 sctp_chunk_hold(chunk);
407 retval = sctp_primitive_ASCONF(asoc, chunk);
408 if (retval)
409 sctp_chunk_free(chunk);
410 else
411 asoc->addip_last_asconf = chunk;
412
413out:
414 return retval;
415}
416
417/* Add a list of addresses as bind addresses to local endpoint or
418 * association.
419 *
420 * Basically run through each address specified in the addrs/addrcnt
421 * array/length pair, determine if it is IPv6 or IPv4 and call
422 * sctp_do_bind() on it.
423 *
424 * If any of them fails, then the operation will be reversed and the
425 * ones that were added will be removed.
426 *
427 * Only sctp_setsockopt_bindx() is supposed to call this function.
428 */
429int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
430{
431 int cnt;
432 int retval = 0;
433 void *addr_buf;
434 struct sockaddr *sa_addr;
435 struct sctp_af *af;
436
437 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
438 sk, addrs, addrcnt);
439
440 addr_buf = addrs;
441 for (cnt = 0; cnt < addrcnt; cnt++) {
442 /* The list may contain either IPv4 or IPv6 address;
443 * determine the address length for walking thru the list.
444 */
445 sa_addr = (struct sockaddr *)addr_buf;
446 af = sctp_get_af_specific(sa_addr->sa_family);
447 if (!af) {
448 retval = -EINVAL;
449 goto err_bindx_add;
450 }
451
452 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
453 af->sockaddr_len);
454
455 addr_buf += af->sockaddr_len;
456
457err_bindx_add:
458 if (retval < 0) {
459 /* Failed. Cleanup the ones that have been added */
460 if (cnt > 0)
461 sctp_bindx_rem(sk, addrs, cnt);
462 return retval;
463 }
464 }
465
466 return retval;
467}
468
469/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
470 * associations that are part of the endpoint indicating that a list of local
471 * addresses are added to the endpoint.
472 *
473 * If any of the addresses is already in the bind address list of the
474 * association, we do not send the chunk for that association. But it will not
475 * affect other associations.
476 *
477 * Only sctp_setsockopt_bindx() is supposed to call this function.
478 */
479static int sctp_send_asconf_add_ip(struct sock *sk,
480 struct sockaddr *addrs,
481 int addrcnt)
482{
483 struct sctp_sock *sp;
484 struct sctp_endpoint *ep;
485 struct sctp_association *asoc;
486 struct sctp_bind_addr *bp;
487 struct sctp_chunk *chunk;
488 struct sctp_sockaddr_entry *laddr;
489 union sctp_addr *addr;
dc022a98 490 union sctp_addr saveaddr;
1da177e4
LT
491 void *addr_buf;
492 struct sctp_af *af;
493 struct list_head *pos;
494 struct list_head *p;
495 int i;
496 int retval = 0;
497
498 if (!sctp_addip_enable)
499 return retval;
500
501 sp = sctp_sk(sk);
502 ep = sp->ep;
503
504 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
505 __FUNCTION__, sk, addrs, addrcnt);
506
507 list_for_each(pos, &ep->asocs) {
508 asoc = list_entry(pos, struct sctp_association, asocs);
509
510 if (!asoc->peer.asconf_capable)
511 continue;
512
513 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
514 continue;
515
516 if (!sctp_state(asoc, ESTABLISHED))
517 continue;
518
519 /* Check if any address in the packed array of addresses is
520 * in the bind address list of the association. If so,
521 * do not send the asconf chunk to its peer, but continue with
522 * other associations.
523 */
524 addr_buf = addrs;
525 for (i = 0; i < addrcnt; i++) {
526 addr = (union sctp_addr *)addr_buf;
527 af = sctp_get_af_specific(addr->v4.sin_family);
528 if (!af) {
529 retval = -EINVAL;
530 goto out;
531 }
532
533 if (sctp_assoc_lookup_laddr(asoc, addr))
534 break;
535
536 addr_buf += af->sockaddr_len;
537 }
538 if (i < addrcnt)
539 continue;
540
541 /* Use the first address in bind addr list of association as
542 * Address Parameter of ASCONF CHUNK.
543 */
544 sctp_read_lock(&asoc->base.addr_lock);
545 bp = &asoc->base.bind_addr;
546 p = bp->address_list.next;
547 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
548 sctp_read_unlock(&asoc->base.addr_lock);
549
5ae955cf 550 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
1da177e4
LT
551 addrcnt, SCTP_PARAM_ADD_IP);
552 if (!chunk) {
553 retval = -ENOMEM;
554 goto out;
555 }
556
557 retval = sctp_send_asconf(asoc, chunk);
dc022a98
SS
558 if (retval)
559 goto out;
1da177e4 560
dc022a98
SS
561 /* Add the new addresses to the bind address list with
562 * use_as_src set to 0.
1da177e4 563 */
dc022a98
SS
564 sctp_local_bh_disable();
565 sctp_write_lock(&asoc->base.addr_lock);
566 addr_buf = addrs;
567 for (i = 0; i < addrcnt; i++) {
568 addr = (union sctp_addr *)addr_buf;
569 af = sctp_get_af_specific(addr->v4.sin_family);
570 memcpy(&saveaddr, addr, af->sockaddr_len);
dc022a98
SS
571 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
572 GFP_ATOMIC);
573 addr_buf += af->sockaddr_len;
574 }
575 sctp_write_unlock(&asoc->base.addr_lock);
576 sctp_local_bh_enable();
1da177e4
LT
577 }
578
579out:
580 return retval;
581}
582
583/* Remove a list of addresses from bind addresses list. Do not remove the
584 * last address.
585 *
586 * Basically run through each address specified in the addrs/addrcnt
587 * array/length pair, determine if it is IPv6 or IPv4 and call
588 * sctp_del_bind() on it.
589 *
590 * If any of them fails, then the operation will be reversed and the
591 * ones that were removed will be added back.
592 *
593 * At least one address has to be left; if only one address is
594 * available, the operation will return -EBUSY.
595 *
596 * Only sctp_setsockopt_bindx() is supposed to call this function.
597 */
598int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
599{
600 struct sctp_sock *sp = sctp_sk(sk);
601 struct sctp_endpoint *ep = sp->ep;
602 int cnt;
603 struct sctp_bind_addr *bp = &ep->base.bind_addr;
604 int retval = 0;
1da177e4 605 void *addr_buf;
c9a08505 606 union sctp_addr *sa_addr;
1da177e4
LT
607 struct sctp_af *af;
608
609 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
610 sk, addrs, addrcnt);
611
612 addr_buf = addrs;
613 for (cnt = 0; cnt < addrcnt; cnt++) {
614 /* If the bind address list is empty or if there is only one
615 * bind address, there is nothing more to be removed (we need
616 * at least one address here).
617 */
618 if (list_empty(&bp->address_list) ||
619 (sctp_list_single_entry(&bp->address_list))) {
620 retval = -EBUSY;
621 goto err_bindx_rem;
622 }
623
c9a08505
AV
624 sa_addr = (union sctp_addr *)addr_buf;
625 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1da177e4
LT
626 if (!af) {
627 retval = -EINVAL;
628 goto err_bindx_rem;
629 }
c9a08505 630 if (sa_addr->v4.sin_port != htons(bp->port)) {
1da177e4
LT
631 retval = -EINVAL;
632 goto err_bindx_rem;
633 }
634
635 /* FIXME - There is probably a need to check if sk->sk_saddr and
636 * sk->sk_rcv_addr are currently set to one of the addresses to
637 * be removed. This is something which needs to be looked into
638 * when we are fixing the outstanding issues with multi-homing
639 * socket routing and failover schemes. Refer to comments in
640 * sctp_do_bind(). -daisy
641 */
642 sctp_local_bh_disable();
643 sctp_write_lock(&ep->base.addr_lock);
644
c9a08505 645 retval = sctp_del_bind_addr(bp, sa_addr);
1da177e4
LT
646
647 sctp_write_unlock(&ep->base.addr_lock);
648 sctp_local_bh_enable();
649
650 addr_buf += af->sockaddr_len;
651err_bindx_rem:
652 if (retval < 0) {
653 /* Failed. Add the ones that has been removed back */
654 if (cnt > 0)
655 sctp_bindx_add(sk, addrs, cnt);
656 return retval;
657 }
658 }
659
660 return retval;
661}
662
663/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
664 * the associations that are part of the endpoint indicating that a list of
665 * local addresses are removed from the endpoint.
666 *
667 * If any of the addresses is already in the bind address list of the
668 * association, we do not send the chunk for that association. But it will not
669 * affect other associations.
670 *
671 * Only sctp_setsockopt_bindx() is supposed to call this function.
672 */
673static int sctp_send_asconf_del_ip(struct sock *sk,
674 struct sockaddr *addrs,
675 int addrcnt)
676{
677 struct sctp_sock *sp;
678 struct sctp_endpoint *ep;
679 struct sctp_association *asoc;
dc022a98 680 struct sctp_transport *transport;
1da177e4
LT
681 struct sctp_bind_addr *bp;
682 struct sctp_chunk *chunk;
683 union sctp_addr *laddr;
684 void *addr_buf;
685 struct sctp_af *af;
dc022a98
SS
686 struct list_head *pos, *pos1;
687 struct sctp_sockaddr_entry *saddr;
1da177e4
LT
688 int i;
689 int retval = 0;
690
691 if (!sctp_addip_enable)
692 return retval;
693
694 sp = sctp_sk(sk);
695 ep = sp->ep;
696
697 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
698 __FUNCTION__, sk, addrs, addrcnt);
699
700 list_for_each(pos, &ep->asocs) {
701 asoc = list_entry(pos, struct sctp_association, asocs);
702
703 if (!asoc->peer.asconf_capable)
704 continue;
705
706 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
707 continue;
708
709 if (!sctp_state(asoc, ESTABLISHED))
710 continue;
711
712 /* Check if any address in the packed array of addresses is
713 * not present in the bind address list of the association.
714 * If so, do not send the asconf chunk to its peer, but
715 * continue with other associations.
716 */
717 addr_buf = addrs;
718 for (i = 0; i < addrcnt; i++) {
719 laddr = (union sctp_addr *)addr_buf;
720 af = sctp_get_af_specific(laddr->v4.sin_family);
721 if (!af) {
722 retval = -EINVAL;
723 goto out;
724 }
725
726 if (!sctp_assoc_lookup_laddr(asoc, laddr))
727 break;
728
729 addr_buf += af->sockaddr_len;
730 }
731 if (i < addrcnt)
732 continue;
733
734 /* Find one address in the association's bind address list
735 * that is not in the packed array of addresses. This is to
736 * make sure that we do not delete all the addresses in the
737 * association.
738 */
739 sctp_read_lock(&asoc->base.addr_lock);
740 bp = &asoc->base.bind_addr;
741 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
742 addrcnt, sp);
743 sctp_read_unlock(&asoc->base.addr_lock);
744 if (!laddr)
745 continue;
746
747 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
748 SCTP_PARAM_DEL_IP);
749 if (!chunk) {
750 retval = -ENOMEM;
751 goto out;
752 }
753
dc022a98
SS
754 /* Reset use_as_src flag for the addresses in the bind address
755 * list that are to be deleted.
756 */
757 sctp_local_bh_disable();
758 sctp_write_lock(&asoc->base.addr_lock);
759 addr_buf = addrs;
760 for (i = 0; i < addrcnt; i++) {
761 laddr = (union sctp_addr *)addr_buf;
762 af = sctp_get_af_specific(laddr->v4.sin_family);
dc022a98
SS
763 list_for_each(pos1, &bp->address_list) {
764 saddr = list_entry(pos1,
765 struct sctp_sockaddr_entry,
766 list);
5f242a13 767 if (sctp_cmp_addr_exact(&saddr->a, laddr))
dc022a98
SS
768 saddr->use_as_src = 0;
769 }
770 addr_buf += af->sockaddr_len;
771 }
772 sctp_write_unlock(&asoc->base.addr_lock);
773 sctp_local_bh_enable();
1da177e4 774
dc022a98
SS
775 /* Update the route and saddr entries for all the transports
776 * as some of the addresses in the bind address list are
777 * about to be deleted and cannot be used as source addresses.
1da177e4 778 */
dc022a98
SS
779 list_for_each(pos1, &asoc->peer.transport_addr_list) {
780 transport = list_entry(pos1, struct sctp_transport,
781 transports);
782 dst_release(transport->dst);
783 sctp_transport_route(transport, NULL,
784 sctp_sk(asoc->base.sk));
785 }
786
787 retval = sctp_send_asconf(asoc, chunk);
1da177e4
LT
788 }
789out:
790 return retval;
791}
792
793/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
794 *
795 * API 8.1
796 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
797 * int flags);
798 *
799 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
800 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
801 * or IPv6 addresses.
802 *
803 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
804 * Section 3.1.2 for this usage.
805 *
806 * addrs is a pointer to an array of one or more socket addresses. Each
807 * address is contained in its appropriate structure (i.e. struct
808 * sockaddr_in or struct sockaddr_in6) the family of the address type
23c435f7 809 * must be used to distinguish the address length (note that this
1da177e4
LT
810 * representation is termed a "packed array" of addresses). The caller
811 * specifies the number of addresses in the array with addrcnt.
812 *
813 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
814 * -1, and sets errno to the appropriate error code.
815 *
816 * For SCTP, the port given in each socket address must be the same, or
817 * sctp_bindx() will fail, setting errno to EINVAL.
818 *
819 * The flags parameter is formed from the bitwise OR of zero or more of
820 * the following currently defined flags:
821 *
822 * SCTP_BINDX_ADD_ADDR
823 *
824 * SCTP_BINDX_REM_ADDR
825 *
826 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
827 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
828 * addresses from the association. The two flags are mutually exclusive;
829 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
830 * not remove all addresses from an association; sctp_bindx() will
831 * reject such an attempt with EINVAL.
832 *
833 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
834 * additional addresses with an endpoint after calling bind(). Or use
835 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
836 * socket is associated with so that no new association accepted will be
837 * associated with those addresses. If the endpoint supports dynamic
838 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
839 * endpoint to send the appropriate message to the peer to change the
840 * peers address lists.
841 *
842 * Adding and removing addresses from a connected association is
843 * optional functionality. Implementations that do not support this
844 * functionality should return EOPNOTSUPP.
845 *
846 * Basically do nothing but copying the addresses from user to kernel
847 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
3f7a87d2
FF
848 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
849 * from userspace.
1da177e4
LT
850 *
851 * We don't use copy_from_user() for optimization: we first do the
852 * sanity checks (buffer size -fast- and access check-healthy
853 * pointer); if all of those succeed, then we can alloc the memory
854 * (expensive operation) needed to copy the data to kernel. Then we do
855 * the copying without checking the user space area
856 * (__copy_from_user()).
857 *
858 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
859 * it.
860 *
861 * sk The sk of the socket
862 * addrs The pointer to the addresses in user land
863 * addrssize Size of the addrs buffer
864 * op Operation to perform (add or remove, see the flags of
865 * sctp_bindx)
866 *
867 * Returns 0 if ok, <0 errno code on error.
868 */
869SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
870 struct sockaddr __user *addrs,
871 int addrs_size, int op)
872{
873 struct sockaddr *kaddrs;
874 int err;
875 int addrcnt = 0;
876 int walk_size = 0;
877 struct sockaddr *sa_addr;
878 void *addr_buf;
879 struct sctp_af *af;
880
881 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
882 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
883
884 if (unlikely(addrs_size <= 0))
885 return -EINVAL;
886
887 /* Check the user passed a healthy pointer. */
888 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
889 return -EFAULT;
890
891 /* Alloc space for the address array in kernel memory. */
8b3a7005 892 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1da177e4
LT
893 if (unlikely(!kaddrs))
894 return -ENOMEM;
895
896 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
897 kfree(kaddrs);
898 return -EFAULT;
899 }
900
901 /* Walk through the addrs buffer and count the number of addresses. */
902 addr_buf = kaddrs;
903 while (walk_size < addrs_size) {
904 sa_addr = (struct sockaddr *)addr_buf;
905 af = sctp_get_af_specific(sa_addr->sa_family);
906
907 /* If the address family is not supported or if this address
908 * causes the address buffer to overflow return EINVAL.
909 */
910 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
911 kfree(kaddrs);
912 return -EINVAL;
913 }
914 addrcnt++;
915 addr_buf += af->sockaddr_len;
916 walk_size += af->sockaddr_len;
917 }
918
919 /* Do the work. */
920 switch (op) {
921 case SCTP_BINDX_ADD_ADDR:
922 err = sctp_bindx_add(sk, kaddrs, addrcnt);
923 if (err)
924 goto out;
925 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
926 break;
927
928 case SCTP_BINDX_REM_ADDR:
929 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
930 if (err)
931 goto out;
932 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
933 break;
934
935 default:
936 err = -EINVAL;
937 break;
938 };
939
940out:
941 kfree(kaddrs);
942
943 return err;
944}
945
3f7a87d2
FF
946/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
947 *
948 * Common routine for handling connect() and sctp_connectx().
949 * Connect will come in with just a single address.
950 */
951static int __sctp_connect(struct sock* sk,
952 struct sockaddr *kaddrs,
953 int addrs_size)
954{
955 struct sctp_sock *sp;
956 struct sctp_endpoint *ep;
957 struct sctp_association *asoc = NULL;
958 struct sctp_association *asoc2;
959 struct sctp_transport *transport;
960 union sctp_addr to;
961 struct sctp_af *af;
962 sctp_scope_t scope;
963 long timeo;
964 int err = 0;
965 int addrcnt = 0;
966 int walk_size = 0;
4bdf4b5f 967 union sctp_addr *sa_addr;
3f7a87d2
FF
968 void *addr_buf;
969
970 sp = sctp_sk(sk);
971 ep = sp->ep;
972
973 /* connect() cannot be done on a socket that is already in ESTABLISHED
974 * state - UDP-style peeled off socket or a TCP-style socket that
975 * is already connected.
976 * It cannot be done even on a TCP-style listening socket.
977 */
978 if (sctp_sstate(sk, ESTABLISHED) ||
979 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
980 err = -EISCONN;
981 goto out_free;
982 }
983
984 /* Walk through the addrs buffer and count the number of addresses. */
985 addr_buf = kaddrs;
986 while (walk_size < addrs_size) {
4bdf4b5f
AV
987 sa_addr = (union sctp_addr *)addr_buf;
988 af = sctp_get_af_specific(sa_addr->sa.sa_family);
3f7a87d2
FF
989
990 /* If the address family is not supported or if this address
991 * causes the address buffer to overflow return EINVAL.
992 */
993 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
994 err = -EINVAL;
995 goto out_free;
996 }
997
4bdf4b5f 998 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
3f7a87d2
FF
999 if (err)
1000 goto out_free;
1001
1002 memcpy(&to, sa_addr, af->sockaddr_len);
3f7a87d2
FF
1003
1004 /* Check if there already is a matching association on the
1005 * endpoint (other than the one created here).
1006 */
cd4ff034 1007 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
3f7a87d2
FF
1008 if (asoc2 && asoc2 != asoc) {
1009 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1010 err = -EISCONN;
1011 else
1012 err = -EALREADY;
1013 goto out_free;
1014 }
1015
1016 /* If we could not find a matching association on the endpoint,
1017 * make sure that there is no peeled-off association matching
1018 * the peer address even on another socket.
1019 */
6c7be55c 1020 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
3f7a87d2
FF
1021 err = -EADDRNOTAVAIL;
1022 goto out_free;
1023 }
1024
1025 if (!asoc) {
1026 /* If a bind() or sctp_bindx() is not called prior to
1027 * an sctp_connectx() call, the system picks an
1028 * ephemeral port and will choose an address set
1029 * equivalent to binding with a wildcard address.
1030 */
1031 if (!ep->base.bind_addr.port) {
1032 if (sctp_autobind(sk)) {
1033 err = -EAGAIN;
1034 goto out_free;
1035 }
64a0c1c8
ISJ
1036 } else {
1037 /*
1038 * If an unprivileged user inherits a 1-many
1039 * style socket with open associations on a
1040 * privileged port, it MAY be permitted to
1041 * accept new associations, but it SHOULD NOT
1042 * be permitted to open new associations.
1043 */
1044 if (ep->base.bind_addr.port < PROT_SOCK &&
1045 !capable(CAP_NET_BIND_SERVICE)) {
1046 err = -EACCES;
1047 goto out_free;
1048 }
3f7a87d2
FF
1049 }
1050
dce116ae 1051 scope = sctp_scope(sa_addr);
3f7a87d2
FF
1052 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1053 if (!asoc) {
1054 err = -ENOMEM;
1055 goto out_free;
1056 }
1057 }
1058
1059 /* Prime the peer's transport structures. */
4bdf4b5f 1060 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL,
3f7a87d2
FF
1061 SCTP_UNKNOWN);
1062 if (!transport) {
1063 err = -ENOMEM;
1064 goto out_free;
1065 }
1066
1067 addrcnt++;
1068 addr_buf += af->sockaddr_len;
1069 walk_size += af->sockaddr_len;
1070 }
1071
1072 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1073 if (err < 0) {
1074 goto out_free;
1075 }
1076
1077 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1078 if (err < 0) {
1079 goto out_free;
1080 }
1081
1082 /* Initialize sk's dport and daddr for getpeername() */
1083 inet_sk(sk)->dport = htons(asoc->peer.port);
1084 af = sctp_get_af_specific(to.sa.sa_family);
1085 af->to_sk_daddr(&to, sk);
8de8c873 1086 sk->sk_err = 0;
3f7a87d2
FF
1087
1088 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1089 err = sctp_wait_for_connect(asoc, &timeo);
1090
1091 /* Don't free association on exit. */
1092 asoc = NULL;
1093
1094out_free:
1095
1096 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1097 " kaddrs: %p err: %d\n",
1098 asoc, kaddrs, err);
1099 if (asoc)
1100 sctp_association_free(asoc);
1101 return err;
1102}
1103
1104/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1105 *
1106 * API 8.9
1107 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1108 *
1109 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1110 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1111 * or IPv6 addresses.
1112 *
1113 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1114 * Section 3.1.2 for this usage.
1115 *
1116 * addrs is a pointer to an array of one or more socket addresses. Each
1117 * address is contained in its appropriate structure (i.e. struct
1118 * sockaddr_in or struct sockaddr_in6) the family of the address type
1119 * must be used to distengish the address length (note that this
1120 * representation is termed a "packed array" of addresses). The caller
1121 * specifies the number of addresses in the array with addrcnt.
1122 *
1123 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1124 * -1, and sets errno to the appropriate error code.
1125 *
1126 * For SCTP, the port given in each socket address must be the same, or
1127 * sctp_connectx() will fail, setting errno to EINVAL.
1128 *
1129 * An application can use sctp_connectx to initiate an association with
1130 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1131 * allows a caller to specify multiple addresses at which a peer can be
1132 * reached. The way the SCTP stack uses the list of addresses to set up
1133 * the association is implementation dependant. This function only
1134 * specifies that the stack will try to make use of all the addresses in
1135 * the list when needed.
1136 *
1137 * Note that the list of addresses passed in is only used for setting up
1138 * the association. It does not necessarily equal the set of addresses
1139 * the peer uses for the resulting association. If the caller wants to
1140 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1141 * retrieve them after the association has been set up.
1142 *
1143 * Basically do nothing but copying the addresses from user to kernel
1144 * land and invoking either sctp_connectx(). This is used for tunneling
1145 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1146 *
1147 * We don't use copy_from_user() for optimization: we first do the
1148 * sanity checks (buffer size -fast- and access check-healthy
1149 * pointer); if all of those succeed, then we can alloc the memory
1150 * (expensive operation) needed to copy the data to kernel. Then we do
1151 * the copying without checking the user space area
1152 * (__copy_from_user()).
1153 *
1154 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1155 * it.
1156 *
1157 * sk The sk of the socket
1158 * addrs The pointer to the addresses in user land
1159 * addrssize Size of the addrs buffer
1160 *
1161 * Returns 0 if ok, <0 errno code on error.
1162 */
1163SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1164 struct sockaddr __user *addrs,
1165 int addrs_size)
1166{
1167 int err = 0;
1168 struct sockaddr *kaddrs;
1169
1170 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1171 __FUNCTION__, sk, addrs, addrs_size);
1172
1173 if (unlikely(addrs_size <= 0))
1174 return -EINVAL;
1175
1176 /* Check the user passed a healthy pointer. */
1177 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1178 return -EFAULT;
1179
1180 /* Alloc space for the address array in kernel memory. */
8b3a7005 1181 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
3f7a87d2
FF
1182 if (unlikely(!kaddrs))
1183 return -ENOMEM;
1184
1185 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1186 err = -EFAULT;
1187 } else {
1188 err = __sctp_connect(sk, kaddrs, addrs_size);
1189 }
1190
1191 kfree(kaddrs);
1192 return err;
1193}
1194
1da177e4
LT
1195/* API 3.1.4 close() - UDP Style Syntax
1196 * Applications use close() to perform graceful shutdown (as described in
1197 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1198 * by a UDP-style socket.
1199 *
1200 * The syntax is
1201 *
1202 * ret = close(int sd);
1203 *
1204 * sd - the socket descriptor of the associations to be closed.
1205 *
1206 * To gracefully shutdown a specific association represented by the
1207 * UDP-style socket, an application should use the sendmsg() call,
1208 * passing no user data, but including the appropriate flag in the
1209 * ancillary data (see Section xxxx).
1210 *
1211 * If sd in the close() call is a branched-off socket representing only
1212 * one association, the shutdown is performed on that association only.
1213 *
1214 * 4.1.6 close() - TCP Style Syntax
1215 *
1216 * Applications use close() to gracefully close down an association.
1217 *
1218 * The syntax is:
1219 *
1220 * int close(int sd);
1221 *
1222 * sd - the socket descriptor of the association to be closed.
1223 *
1224 * After an application calls close() on a socket descriptor, no further
1225 * socket operations will succeed on that descriptor.
1226 *
1227 * API 7.1.4 SO_LINGER
1228 *
1229 * An application using the TCP-style socket can use this option to
1230 * perform the SCTP ABORT primitive. The linger option structure is:
1231 *
1232 * struct linger {
1233 * int l_onoff; // option on/off
1234 * int l_linger; // linger time
1235 * };
1236 *
1237 * To enable the option, set l_onoff to 1. If the l_linger value is set
1238 * to 0, calling close() is the same as the ABORT primitive. If the
1239 * value is set to a negative value, the setsockopt() call will return
1240 * an error. If the value is set to a positive value linger_time, the
1241 * close() can be blocked for at most linger_time ms. If the graceful
1242 * shutdown phase does not finish during this period, close() will
1243 * return but the graceful shutdown phase continues in the system.
1244 */
1245SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1246{
1247 struct sctp_endpoint *ep;
1248 struct sctp_association *asoc;
1249 struct list_head *pos, *temp;
1250
1251 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1252
1253 sctp_lock_sock(sk);
1254 sk->sk_shutdown = SHUTDOWN_MASK;
1255
1256 ep = sctp_sk(sk)->ep;
1257
61c9fed4 1258 /* Walk all associations on an endpoint. */
1da177e4
LT
1259 list_for_each_safe(pos, temp, &ep->asocs) {
1260 asoc = list_entry(pos, struct sctp_association, asocs);
1261
1262 if (sctp_style(sk, TCP)) {
1263 /* A closed association can still be in the list if
1264 * it belongs to a TCP-style listening socket that is
1265 * not yet accepted. If so, free it. If not, send an
1266 * ABORT or SHUTDOWN based on the linger options.
1267 */
1268 if (sctp_state(asoc, CLOSED)) {
1269 sctp_unhash_established(asoc);
1270 sctp_association_free(asoc);
b89498a1
VY
1271 continue;
1272 }
1273 }
1da177e4 1274
b9ac8672
SS
1275 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1276 struct sctp_chunk *chunk;
1277
1278 chunk = sctp_make_abort_user(asoc, NULL, 0);
1279 if (chunk)
1280 sctp_primitive_ABORT(asoc, chunk);
1281 } else
1da177e4
LT
1282 sctp_primitive_SHUTDOWN(asoc, NULL);
1283 }
1284
1285 /* Clean up any skbs sitting on the receive queue. */
1286 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1287 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1288
1289 /* On a TCP-style socket, block for at most linger_time if set. */
1290 if (sctp_style(sk, TCP) && timeout)
1291 sctp_wait_for_close(sk, timeout);
1292
1293 /* This will run the backlog queue. */
1294 sctp_release_sock(sk);
1295
1296 /* Supposedly, no process has access to the socket, but
1297 * the net layers still may.
1298 */
1299 sctp_local_bh_disable();
1300 sctp_bh_lock_sock(sk);
1301
1302 /* Hold the sock, since sk_common_release() will put sock_put()
1303 * and we have just a little more cleanup.
1304 */
1305 sock_hold(sk);
1306 sk_common_release(sk);
1307
1308 sctp_bh_unlock_sock(sk);
1309 sctp_local_bh_enable();
1310
1311 sock_put(sk);
1312
1313 SCTP_DBG_OBJCNT_DEC(sock);
1314}
1315
1316/* Handle EPIPE error. */
1317static int sctp_error(struct sock *sk, int flags, int err)
1318{
1319 if (err == -EPIPE)
1320 err = sock_error(sk) ? : -EPIPE;
1321 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1322 send_sig(SIGPIPE, current, 0);
1323 return err;
1324}
1325
1326/* API 3.1.3 sendmsg() - UDP Style Syntax
1327 *
1328 * An application uses sendmsg() and recvmsg() calls to transmit data to
1329 * and receive data from its peer.
1330 *
1331 * ssize_t sendmsg(int socket, const struct msghdr *message,
1332 * int flags);
1333 *
1334 * socket - the socket descriptor of the endpoint.
1335 * message - pointer to the msghdr structure which contains a single
1336 * user message and possibly some ancillary data.
1337 *
1338 * See Section 5 for complete description of the data
1339 * structures.
1340 *
1341 * flags - flags sent or received with the user message, see Section
1342 * 5 for complete description of the flags.
1343 *
1344 * Note: This function could use a rewrite especially when explicit
1345 * connect support comes in.
1346 */
1347/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1348
1349SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1350
1351SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1352 struct msghdr *msg, size_t msg_len)
1353{
1354 struct sctp_sock *sp;
1355 struct sctp_endpoint *ep;
1356 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1357 struct sctp_transport *transport, *chunk_tp;
1358 struct sctp_chunk *chunk;
dce116ae 1359 union sctp_addr to;
1da177e4
LT
1360 struct sockaddr *msg_name = NULL;
1361 struct sctp_sndrcvinfo default_sinfo = { 0 };
1362 struct sctp_sndrcvinfo *sinfo;
1363 struct sctp_initmsg *sinit;
1364 sctp_assoc_t associd = 0;
1365 sctp_cmsgs_t cmsgs = { NULL };
1366 int err;
1367 sctp_scope_t scope;
1368 long timeo;
1369 __u16 sinfo_flags = 0;
1370 struct sctp_datamsg *datamsg;
1371 struct list_head *pos;
1372 int msg_flags = msg->msg_flags;
1373
1374 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1375 sk, msg, msg_len);
1376
1377 err = 0;
1378 sp = sctp_sk(sk);
1379 ep = sp->ep;
1380
3f7a87d2 1381 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1da177e4
LT
1382
1383 /* We cannot send a message over a TCP-style listening socket. */
1384 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1385 err = -EPIPE;
1386 goto out_nounlock;
1387 }
1388
1389 /* Parse out the SCTP CMSGs. */
1390 err = sctp_msghdr_parse(msg, &cmsgs);
1391
1392 if (err) {
1393 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1394 goto out_nounlock;
1395 }
1396
1397 /* Fetch the destination address for this packet. This
1398 * address only selects the association--it is not necessarily
1399 * the address we will send to.
1400 * For a peeled-off socket, msg_name is ignored.
1401 */
1402 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1403 int msg_namelen = msg->msg_namelen;
1404
1405 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1406 msg_namelen);
1407 if (err)
1408 return err;
1409
1410 if (msg_namelen > sizeof(to))
1411 msg_namelen = sizeof(to);
1412 memcpy(&to, msg->msg_name, msg_namelen);
1da177e4
LT
1413 msg_name = msg->msg_name;
1414 }
1415
1416 sinfo = cmsgs.info;
1417 sinit = cmsgs.init;
1418
1419 /* Did the user specify SNDRCVINFO? */
1420 if (sinfo) {
1421 sinfo_flags = sinfo->sinfo_flags;
1422 associd = sinfo->sinfo_assoc_id;
1423 }
1424
1425 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1426 msg_len, sinfo_flags);
1427
eaa5c54d
ISJ
1428 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1429 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1da177e4
LT
1430 err = -EINVAL;
1431 goto out_nounlock;
1432 }
1433
eaa5c54d
ISJ
1434 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1435 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1436 * If SCTP_ABORT is set, the message length could be non zero with
1da177e4
LT
1437 * the msg_iov set to the user abort reason.
1438 */
eaa5c54d
ISJ
1439 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1440 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1da177e4
LT
1441 err = -EINVAL;
1442 goto out_nounlock;
1443 }
1444
eaa5c54d 1445 /* If SCTP_ADDR_OVER is set, there must be an address
1da177e4
LT
1446 * specified in msg_name.
1447 */
eaa5c54d 1448 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1da177e4
LT
1449 err = -EINVAL;
1450 goto out_nounlock;
1451 }
1452
1453 transport = NULL;
1454
1455 SCTP_DEBUG_PRINTK("About to look up association.\n");
1456
1457 sctp_lock_sock(sk);
1458
1459 /* If a msg_name has been specified, assume this is to be used. */
1460 if (msg_name) {
1461 /* Look for a matching association on the endpoint. */
dce116ae 1462 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1da177e4
LT
1463 if (!asoc) {
1464 /* If we could not find a matching association on the
1465 * endpoint, make sure that it is not a TCP-style
1466 * socket that already has an association or there is
1467 * no peeled-off association on another socket.
1468 */
1469 if ((sctp_style(sk, TCP) &&
1470 sctp_sstate(sk, ESTABLISHED)) ||
dce116ae 1471 sctp_endpoint_is_peeled_off(ep, &to)) {
1da177e4
LT
1472 err = -EADDRNOTAVAIL;
1473 goto out_unlock;
1474 }
1475 }
1476 } else {
1477 asoc = sctp_id2assoc(sk, associd);
1478 if (!asoc) {
1479 err = -EPIPE;
1480 goto out_unlock;
1481 }
1482 }
1483
1484 if (asoc) {
1485 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1486
1487 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1488 * socket that has an association in CLOSED state. This can
1489 * happen when an accepted socket has an association that is
1490 * already CLOSED.
1491 */
1492 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1493 err = -EPIPE;
1494 goto out_unlock;
1495 }
1496
eaa5c54d 1497 if (sinfo_flags & SCTP_EOF) {
1da177e4
LT
1498 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1499 asoc);
1500 sctp_primitive_SHUTDOWN(asoc, NULL);
1501 err = 0;
1502 goto out_unlock;
1503 }
eaa5c54d 1504 if (sinfo_flags & SCTP_ABORT) {
c164a9ba
SS
1505 struct sctp_chunk *chunk;
1506
1507 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1508 if (!chunk) {
1509 err = -ENOMEM;
1510 goto out_unlock;
1511 }
1512
1da177e4 1513 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
c164a9ba 1514 sctp_primitive_ABORT(asoc, chunk);
1da177e4
LT
1515 err = 0;
1516 goto out_unlock;
1517 }
1518 }
1519
1520 /* Do we need to create the association? */
1521 if (!asoc) {
1522 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1523
eaa5c54d 1524 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1da177e4
LT
1525 err = -EINVAL;
1526 goto out_unlock;
1527 }
1528
1529 /* Check for invalid stream against the stream counts,
1530 * either the default or the user specified stream counts.
1531 */
1532 if (sinfo) {
1533 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1534 /* Check against the defaults. */
1535 if (sinfo->sinfo_stream >=
1536 sp->initmsg.sinit_num_ostreams) {
1537 err = -EINVAL;
1538 goto out_unlock;
1539 }
1540 } else {
1541 /* Check against the requested. */
1542 if (sinfo->sinfo_stream >=
1543 sinit->sinit_num_ostreams) {
1544 err = -EINVAL;
1545 goto out_unlock;
1546 }
1547 }
1548 }
1549
1550 /*
1551 * API 3.1.2 bind() - UDP Style Syntax
1552 * If a bind() or sctp_bindx() is not called prior to a
1553 * sendmsg() call that initiates a new association, the
1554 * system picks an ephemeral port and will choose an address
1555 * set equivalent to binding with a wildcard address.
1556 */
1557 if (!ep->base.bind_addr.port) {
1558 if (sctp_autobind(sk)) {
1559 err = -EAGAIN;
1560 goto out_unlock;
1561 }
64a0c1c8
ISJ
1562 } else {
1563 /*
1564 * If an unprivileged user inherits a one-to-many
1565 * style socket with open associations on a privileged
1566 * port, it MAY be permitted to accept new associations,
1567 * but it SHOULD NOT be permitted to open new
1568 * associations.
1569 */
1570 if (ep->base.bind_addr.port < PROT_SOCK &&
1571 !capable(CAP_NET_BIND_SERVICE)) {
1572 err = -EACCES;
1573 goto out_unlock;
1574 }
1da177e4
LT
1575 }
1576
1577 scope = sctp_scope(&to);
1578 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1579 if (!new_asoc) {
1580 err = -ENOMEM;
1581 goto out_unlock;
1582 }
1583 asoc = new_asoc;
1584
1585 /* If the SCTP_INIT ancillary data is specified, set all
1586 * the association init values accordingly.
1587 */
1588 if (sinit) {
1589 if (sinit->sinit_num_ostreams) {
1590 asoc->c.sinit_num_ostreams =
1591 sinit->sinit_num_ostreams;
1592 }
1593 if (sinit->sinit_max_instreams) {
1594 asoc->c.sinit_max_instreams =
1595 sinit->sinit_max_instreams;
1596 }
1597 if (sinit->sinit_max_attempts) {
1598 asoc->max_init_attempts
1599 = sinit->sinit_max_attempts;
1600 }
1601 if (sinit->sinit_max_init_timeo) {
1602 asoc->max_init_timeo =
1603 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1604 }
1605 }
1606
1607 /* Prime the peer's transport structures. */
dce116ae 1608 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1da177e4
LT
1609 if (!transport) {
1610 err = -ENOMEM;
1611 goto out_free;
1612 }
1613 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1614 if (err < 0) {
1615 err = -ENOMEM;
1616 goto out_free;
1617 }
1618 }
1619
1620 /* ASSERT: we have a valid association at this point. */
1621 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1622
1623 if (!sinfo) {
1624 /* If the user didn't specify SNDRCVINFO, make up one with
1625 * some defaults.
1626 */
1627 default_sinfo.sinfo_stream = asoc->default_stream;
1628 default_sinfo.sinfo_flags = asoc->default_flags;
1629 default_sinfo.sinfo_ppid = asoc->default_ppid;
1630 default_sinfo.sinfo_context = asoc->default_context;
1631 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1632 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1633 sinfo = &default_sinfo;
1634 }
1635
1636 /* API 7.1.7, the sndbuf size per association bounds the
1637 * maximum size of data that can be sent in a single send call.
1638 */
1639 if (msg_len > sk->sk_sndbuf) {
1640 err = -EMSGSIZE;
1641 goto out_free;
1642 }
1643
1644 /* If fragmentation is disabled and the message length exceeds the
1645 * association fragmentation point, return EMSGSIZE. The I-D
1646 * does not specify what this error is, but this looks like
1647 * a great fit.
1648 */
1649 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1650 err = -EMSGSIZE;
1651 goto out_free;
1652 }
1653
1654 if (sinfo) {
1655 /* Check for invalid stream. */
1656 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1657 err = -EINVAL;
1658 goto out_free;
1659 }
1660 }
1661
1662 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1663 if (!sctp_wspace(asoc)) {
1664 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1665 if (err)
1666 goto out_free;
1667 }
1668
1669 /* If an address is passed with the sendto/sendmsg call, it is used
1670 * to override the primary destination address in the TCP model, or
eaa5c54d 1671 * when SCTP_ADDR_OVER flag is set in the UDP model.
1da177e4
LT
1672 */
1673 if ((sctp_style(sk, TCP) && msg_name) ||
eaa5c54d 1674 (sinfo_flags & SCTP_ADDR_OVER)) {
dce116ae 1675 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1da177e4
LT
1676 if (!chunk_tp) {
1677 err = -EINVAL;
1678 goto out_free;
1679 }
1680 } else
1681 chunk_tp = NULL;
1682
1683 /* Auto-connect, if we aren't connected already. */
1684 if (sctp_state(asoc, CLOSED)) {
1685 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1686 if (err < 0)
1687 goto out_free;
1688 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1689 }
1690
1691 /* Break the message into multiple chunks of maximum size. */
1692 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1693 if (!datamsg) {
1694 err = -ENOMEM;
1695 goto out_free;
1696 }
1697
1698 /* Now send the (possibly) fragmented message. */
1699 list_for_each(pos, &datamsg->chunks) {
1700 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1701 sctp_datamsg_track(chunk);
1702
1703 /* Do accounting for the write space. */
1704 sctp_set_owner_w(chunk);
1705
1706 chunk->transport = chunk_tp;
1707
1708 /* Send it to the lower layers. Note: all chunks
1709 * must either fail or succeed. The lower layer
1710 * works that way today. Keep it that way or this
1711 * breaks.
1712 */
1713 err = sctp_primitive_SEND(asoc, chunk);
1714 /* Did the lower layer accept the chunk? */
1715 if (err)
1716 sctp_chunk_free(chunk);
1717 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1718 }
1719
1720 sctp_datamsg_free(datamsg);
1721 if (err)
1722 goto out_free;
1723 else
1724 err = msg_len;
1725
1726 /* If we are already past ASSOCIATE, the lower
1727 * layers are responsible for association cleanup.
1728 */
1729 goto out_unlock;
1730
1731out_free:
1732 if (new_asoc)
1733 sctp_association_free(asoc);
1734out_unlock:
1735 sctp_release_sock(sk);
1736
1737out_nounlock:
1738 return sctp_error(sk, msg_flags, err);
1739
1740#if 0
1741do_sock_err:
1742 if (msg_len)
1743 err = msg_len;
1744 else
1745 err = sock_error(sk);
1746 goto out;
1747
1748do_interrupted:
1749 if (msg_len)
1750 err = msg_len;
1751 goto out;
1752#endif /* 0 */
1753}
1754
1755/* This is an extended version of skb_pull() that removes the data from the
1756 * start of a skb even when data is spread across the list of skb's in the
1757 * frag_list. len specifies the total amount of data that needs to be removed.
1758 * when 'len' bytes could be removed from the skb, it returns 0.
1759 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1760 * could not be removed.
1761 */
1762static int sctp_skb_pull(struct sk_buff *skb, int len)
1763{
1764 struct sk_buff *list;
1765 int skb_len = skb_headlen(skb);
1766 int rlen;
1767
1768 if (len <= skb_len) {
1769 __skb_pull(skb, len);
1770 return 0;
1771 }
1772 len -= skb_len;
1773 __skb_pull(skb, skb_len);
1774
1775 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1776 rlen = sctp_skb_pull(list, len);
1777 skb->len -= (len-rlen);
1778 skb->data_len -= (len-rlen);
1779
1780 if (!rlen)
1781 return 0;
1782
1783 len = rlen;
1784 }
1785
1786 return len;
1787}
1788
1789/* API 3.1.3 recvmsg() - UDP Style Syntax
1790 *
1791 * ssize_t recvmsg(int socket, struct msghdr *message,
1792 * int flags);
1793 *
1794 * socket - the socket descriptor of the endpoint.
1795 * message - pointer to the msghdr structure which contains a single
1796 * user message and possibly some ancillary data.
1797 *
1798 * See Section 5 for complete description of the data
1799 * structures.
1800 *
1801 * flags - flags sent or received with the user message, see Section
1802 * 5 for complete description of the flags.
1803 */
1804static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1805
1806SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1807 struct msghdr *msg, size_t len, int noblock,
1808 int flags, int *addr_len)
1809{
1810 struct sctp_ulpevent *event = NULL;
1811 struct sctp_sock *sp = sctp_sk(sk);
1812 struct sk_buff *skb;
1813 int copied;
1814 int err = 0;
1815 int skb_len;
1816
1817 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1818 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1819 "len", len, "knoblauch", noblock,
1820 "flags", flags, "addr_len", addr_len);
1821
1822 sctp_lock_sock(sk);
1823
1824 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1825 err = -ENOTCONN;
1826 goto out;
1827 }
1828
1829 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1830 if (!skb)
1831 goto out;
1832
1833 /* Get the total length of the skb including any skb's in the
1834 * frag_list.
1835 */
1836 skb_len = skb->len;
1837
1838 copied = skb_len;
1839 if (copied > len)
1840 copied = len;
1841
1842 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1843
1844 event = sctp_skb2event(skb);
1845
1846 if (err)
1847 goto out_free;
1848
1849 sock_recv_timestamp(msg, sk, skb);
1850 if (sctp_ulpevent_is_notification(event)) {
1851 msg->msg_flags |= MSG_NOTIFICATION;
1852 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1853 } else {
1854 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1855 }
1856
1857 /* Check if we allow SCTP_SNDRCVINFO. */
1858 if (sp->subscribe.sctp_data_io_event)
1859 sctp_ulpevent_read_sndrcvinfo(event, msg);
1860#if 0
1861 /* FIXME: we should be calling IP/IPv6 layers. */
1862 if (sk->sk_protinfo.af_inet.cmsg_flags)
1863 ip_cmsg_recv(msg, skb);
1864#endif
1865
1866 err = copied;
1867
1868 /* If skb's length exceeds the user's buffer, update the skb and
1869 * push it back to the receive_queue so that the next call to
1870 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1871 */
1872 if (skb_len > copied) {
1873 msg->msg_flags &= ~MSG_EOR;
1874 if (flags & MSG_PEEK)
1875 goto out_free;
1876 sctp_skb_pull(skb, copied);
1877 skb_queue_head(&sk->sk_receive_queue, skb);
1878
1879 /* When only partial message is copied to the user, increase
1880 * rwnd by that amount. If all the data in the skb is read,
1881 * rwnd is updated when the event is freed.
1882 */
1883 sctp_assoc_rwnd_increase(event->asoc, copied);
1884 goto out;
1885 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1886 (event->msg_flags & MSG_EOR))
1887 msg->msg_flags |= MSG_EOR;
1888 else
1889 msg->msg_flags &= ~MSG_EOR;
1890
1891out_free:
1892 if (flags & MSG_PEEK) {
1893 /* Release the skb reference acquired after peeking the skb in
1894 * sctp_skb_recv_datagram().
1895 */
1896 kfree_skb(skb);
1897 } else {
1898 /* Free the event which includes releasing the reference to
1899 * the owner of the skb, freeing the skb and updating the
1900 * rwnd.
1901 */
1902 sctp_ulpevent_free(event);
1903 }
1904out:
1905 sctp_release_sock(sk);
1906 return err;
1907}
1908
1909/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1910 *
1911 * This option is a on/off flag. If enabled no SCTP message
1912 * fragmentation will be performed. Instead if a message being sent
1913 * exceeds the current PMTU size, the message will NOT be sent and
1914 * instead a error will be indicated to the user.
1915 */
1916static int sctp_setsockopt_disable_fragments(struct sock *sk,
1917 char __user *optval, int optlen)
1918{
1919 int val;
1920
1921 if (optlen < sizeof(int))
1922 return -EINVAL;
1923
1924 if (get_user(val, (int __user *)optval))
1925 return -EFAULT;
1926
1927 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1928
1929 return 0;
1930}
1931
1932static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1933 int optlen)
1934{
1935 if (optlen != sizeof(struct sctp_event_subscribe))
1936 return -EINVAL;
1937 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1938 return -EFAULT;
1939 return 0;
1940}
1941
1942/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1943 *
1944 * This socket option is applicable to the UDP-style socket only. When
1945 * set it will cause associations that are idle for more than the
1946 * specified number of seconds to automatically close. An association
1947 * being idle is defined an association that has NOT sent or received
1948 * user data. The special value of '0' indicates that no automatic
1949 * close of any associations should be performed. The option expects an
1950 * integer defining the number of seconds of idle time before an
1951 * association is closed.
1952 */
1953static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1954 int optlen)
1955{
1956 struct sctp_sock *sp = sctp_sk(sk);
1957
1958 /* Applicable to UDP-style socket only */
1959 if (sctp_style(sk, TCP))
1960 return -EOPNOTSUPP;
1961 if (optlen != sizeof(int))
1962 return -EINVAL;
1963 if (copy_from_user(&sp->autoclose, optval, optlen))
1964 return -EFAULT;
1965
1da177e4
LT
1966 return 0;
1967}
1968
1969/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1970 *
1971 * Applications can enable or disable heartbeats for any peer address of
1972 * an association, modify an address's heartbeat interval, force a
1973 * heartbeat to be sent immediately, and adjust the address's maximum
1974 * number of retransmissions sent before an address is considered
1975 * unreachable. The following structure is used to access and modify an
1976 * address's parameters:
1977 *
1978 * struct sctp_paddrparams {
52ccb8e9
FF
1979 * sctp_assoc_t spp_assoc_id;
1980 * struct sockaddr_storage spp_address;
1981 * uint32_t spp_hbinterval;
1982 * uint16_t spp_pathmaxrxt;
1983 * uint32_t spp_pathmtu;
1984 * uint32_t spp_sackdelay;
1985 * uint32_t spp_flags;
1986 * };
1987 *
1988 * spp_assoc_id - (one-to-many style socket) This is filled in the
1989 * application, and identifies the association for
1990 * this query.
1da177e4
LT
1991 * spp_address - This specifies which address is of interest.
1992 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
1993 * in milliseconds. If a value of zero
1994 * is present in this field then no changes are to
1995 * be made to this parameter.
1da177e4
LT
1996 * spp_pathmaxrxt - This contains the maximum number of
1997 * retransmissions before this address shall be
52ccb8e9
FF
1998 * considered unreachable. If a value of zero
1999 * is present in this field then no changes are to
2000 * be made to this parameter.
2001 * spp_pathmtu - When Path MTU discovery is disabled the value
2002 * specified here will be the "fixed" path mtu.
2003 * Note that if the spp_address field is empty
2004 * then all associations on this address will
2005 * have this fixed path mtu set upon them.
2006 *
2007 * spp_sackdelay - When delayed sack is enabled, this value specifies
2008 * the number of milliseconds that sacks will be delayed
2009 * for. This value will apply to all addresses of an
2010 * association if the spp_address field is empty. Note
2011 * also, that if delayed sack is enabled and this
2012 * value is set to 0, no change is made to the last
2013 * recorded delayed sack timer value.
2014 *
2015 * spp_flags - These flags are used to control various features
2016 * on an association. The flag field may contain
2017 * zero or more of the following options.
2018 *
2019 * SPP_HB_ENABLE - Enable heartbeats on the
2020 * specified address. Note that if the address
2021 * field is empty all addresses for the association
2022 * have heartbeats enabled upon them.
2023 *
2024 * SPP_HB_DISABLE - Disable heartbeats on the
2025 * speicifed address. Note that if the address
2026 * field is empty all addresses for the association
2027 * will have their heartbeats disabled. Note also
2028 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2029 * mutually exclusive, only one of these two should
2030 * be specified. Enabling both fields will have
2031 * undetermined results.
2032 *
2033 * SPP_HB_DEMAND - Request a user initiated heartbeat
2034 * to be made immediately.
2035 *
2036 * SPP_PMTUD_ENABLE - This field will enable PMTU
2037 * discovery upon the specified address. Note that
2038 * if the address feild is empty then all addresses
2039 * on the association are effected.
2040 *
2041 * SPP_PMTUD_DISABLE - This field will disable PMTU
2042 * discovery upon the specified address. Note that
2043 * if the address feild is empty then all addresses
2044 * on the association are effected. Not also that
2045 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2046 * exclusive. Enabling both will have undetermined
2047 * results.
2048 *
2049 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2050 * on delayed sack. The time specified in spp_sackdelay
2051 * is used to specify the sack delay for this address. Note
2052 * that if spp_address is empty then all addresses will
2053 * enable delayed sack and take on the sack delay
2054 * value specified in spp_sackdelay.
2055 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2056 * off delayed sack. If the spp_address field is blank then
2057 * delayed sack is disabled for the entire association. Note
2058 * also that this field is mutually exclusive to
2059 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2060 * results.
1da177e4 2061 */
16164366
AB
2062static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2063 struct sctp_transport *trans,
2064 struct sctp_association *asoc,
2065 struct sctp_sock *sp,
2066 int hb_change,
2067 int pmtud_change,
2068 int sackdelay_change)
52ccb8e9
FF
2069{
2070 int error;
2071
2072 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2073 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2074 if (error)
2075 return error;
2076 }
2077
2078 if (params->spp_hbinterval) {
2079 if (trans) {
2080 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2081 } else if (asoc) {
2082 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2083 } else {
2084 sp->hbinterval = params->spp_hbinterval;
2085 }
2086 }
2087
2088 if (hb_change) {
2089 if (trans) {
2090 trans->param_flags =
2091 (trans->param_flags & ~SPP_HB) | hb_change;
2092 } else if (asoc) {
2093 asoc->param_flags =
2094 (asoc->param_flags & ~SPP_HB) | hb_change;
2095 } else {
2096 sp->param_flags =
2097 (sp->param_flags & ~SPP_HB) | hb_change;
2098 }
2099 }
2100
2101 if (params->spp_pathmtu) {
2102 if (trans) {
2103 trans->pathmtu = params->spp_pathmtu;
2104 sctp_assoc_sync_pmtu(asoc);
2105 } else if (asoc) {
2106 asoc->pathmtu = params->spp_pathmtu;
2107 sctp_frag_point(sp, params->spp_pathmtu);
2108 } else {
2109 sp->pathmtu = params->spp_pathmtu;
2110 }
2111 }
2112
2113 if (pmtud_change) {
2114 if (trans) {
2115 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2116 (params->spp_flags & SPP_PMTUD_ENABLE);
2117 trans->param_flags =
2118 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2119 if (update) {
2120 sctp_transport_pmtu(trans);
2121 sctp_assoc_sync_pmtu(asoc);
2122 }
2123 } else if (asoc) {
2124 asoc->param_flags =
2125 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2126 } else {
2127 sp->param_flags =
2128 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2129 }
2130 }
2131
2132 if (params->spp_sackdelay) {
2133 if (trans) {
2134 trans->sackdelay =
2135 msecs_to_jiffies(params->spp_sackdelay);
2136 } else if (asoc) {
2137 asoc->sackdelay =
2138 msecs_to_jiffies(params->spp_sackdelay);
2139 } else {
2140 sp->sackdelay = params->spp_sackdelay;
2141 }
2142 }
2143
2144 if (sackdelay_change) {
2145 if (trans) {
2146 trans->param_flags =
2147 (trans->param_flags & ~SPP_SACKDELAY) |
2148 sackdelay_change;
2149 } else if (asoc) {
2150 asoc->param_flags =
2151 (asoc->param_flags & ~SPP_SACKDELAY) |
2152 sackdelay_change;
2153 } else {
2154 sp->param_flags =
2155 (sp->param_flags & ~SPP_SACKDELAY) |
2156 sackdelay_change;
2157 }
2158 }
2159
2160 if (params->spp_pathmaxrxt) {
2161 if (trans) {
2162 trans->pathmaxrxt = params->spp_pathmaxrxt;
2163 } else if (asoc) {
2164 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2165 } else {
2166 sp->pathmaxrxt = params->spp_pathmaxrxt;
2167 }
2168 }
2169
2170 return 0;
2171}
2172
1da177e4
LT
2173static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2174 char __user *optval, int optlen)
2175{
52ccb8e9
FF
2176 struct sctp_paddrparams params;
2177 struct sctp_transport *trans = NULL;
2178 struct sctp_association *asoc = NULL;
2179 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 2180 int error;
52ccb8e9 2181 int hb_change, pmtud_change, sackdelay_change;
1da177e4
LT
2182
2183 if (optlen != sizeof(struct sctp_paddrparams))
52ccb8e9
FF
2184 return - EINVAL;
2185
1da177e4
LT
2186 if (copy_from_user(&params, optval, optlen))
2187 return -EFAULT;
2188
52ccb8e9
FF
2189 /* Validate flags and value parameters. */
2190 hb_change = params.spp_flags & SPP_HB;
2191 pmtud_change = params.spp_flags & SPP_PMTUD;
2192 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2193
2194 if (hb_change == SPP_HB ||
2195 pmtud_change == SPP_PMTUD ||
2196 sackdelay_change == SPP_SACKDELAY ||
2197 params.spp_sackdelay > 500 ||
2198 (params.spp_pathmtu
2199 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2200 return -EINVAL;
1da177e4 2201
52ccb8e9
FF
2202 /* If an address other than INADDR_ANY is specified, and
2203 * no transport is found, then the request is invalid.
2204 */
2205 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2206 trans = sctp_addr_id2transport(sk, &params.spp_address,
2207 params.spp_assoc_id);
2208 if (!trans)
1da177e4 2209 return -EINVAL;
1da177e4
LT
2210 }
2211
52ccb8e9
FF
2212 /* Get association, if assoc_id != 0 and the socket is a one
2213 * to many style socket, and an association was not found, then
2214 * the id was invalid.
2215 */
2216 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2217 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
1da177e4
LT
2218 return -EINVAL;
2219
52ccb8e9
FF
2220 /* Heartbeat demand can only be sent on a transport or
2221 * association, but not a socket.
1da177e4 2222 */
52ccb8e9
FF
2223 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2224 return -EINVAL;
2225
2226 /* Process parameters. */
2227 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2228 hb_change, pmtud_change,
2229 sackdelay_change);
1da177e4 2230
52ccb8e9
FF
2231 if (error)
2232 return error;
2233
2234 /* If changes are for association, also apply parameters to each
2235 * transport.
1da177e4 2236 */
52ccb8e9
FF
2237 if (!trans && asoc) {
2238 struct list_head *pos;
2239
2240 list_for_each(pos, &asoc->peer.transport_addr_list) {
2241 trans = list_entry(pos, struct sctp_transport,
2242 transports);
2243 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2244 hb_change, pmtud_change,
2245 sackdelay_change);
2246 }
2247 }
1da177e4
LT
2248
2249 return 0;
2250}
2251
7708610b
FF
2252/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2253 *
2254 * This options will get or set the delayed ack timer. The time is set
2255 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2256 * endpoints default delayed ack timer value. If the assoc_id field is
2257 * non-zero, then the set or get effects the specified association.
2258 *
2259 * struct sctp_assoc_value {
2260 * sctp_assoc_t assoc_id;
2261 * uint32_t assoc_value;
2262 * };
2263 *
2264 * assoc_id - This parameter, indicates which association the
2265 * user is preforming an action upon. Note that if
2266 * this field's value is zero then the endpoints
2267 * default value is changed (effecting future
2268 * associations only).
2269 *
2270 * assoc_value - This parameter contains the number of milliseconds
2271 * that the user is requesting the delayed ACK timer
2272 * be set to. Note that this value is defined in
2273 * the standard to be between 200 and 500 milliseconds.
2274 *
2275 * Note: a value of zero will leave the value alone,
2276 * but disable SACK delay. A non-zero value will also
2277 * enable SACK delay.
2278 */
2279
2280static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2281 char __user *optval, int optlen)
2282{
2283 struct sctp_assoc_value params;
2284 struct sctp_transport *trans = NULL;
2285 struct sctp_association *asoc = NULL;
2286 struct sctp_sock *sp = sctp_sk(sk);
2287
2288 if (optlen != sizeof(struct sctp_assoc_value))
2289 return - EINVAL;
2290
2291 if (copy_from_user(&params, optval, optlen))
2292 return -EFAULT;
2293
2294 /* Validate value parameter. */
2295 if (params.assoc_value > 500)
2296 return -EINVAL;
2297
2298 /* Get association, if assoc_id != 0 and the socket is a one
2299 * to many style socket, and an association was not found, then
2300 * the id was invalid.
2301 */
2302 asoc = sctp_id2assoc(sk, params.assoc_id);
2303 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2304 return -EINVAL;
2305
2306 if (params.assoc_value) {
2307 if (asoc) {
2308 asoc->sackdelay =
2309 msecs_to_jiffies(params.assoc_value);
2310 asoc->param_flags =
2311 (asoc->param_flags & ~SPP_SACKDELAY) |
2312 SPP_SACKDELAY_ENABLE;
2313 } else {
2314 sp->sackdelay = params.assoc_value;
2315 sp->param_flags =
2316 (sp->param_flags & ~SPP_SACKDELAY) |
2317 SPP_SACKDELAY_ENABLE;
2318 }
2319 } else {
2320 if (asoc) {
2321 asoc->param_flags =
2322 (asoc->param_flags & ~SPP_SACKDELAY) |
2323 SPP_SACKDELAY_DISABLE;
2324 } else {
2325 sp->param_flags =
2326 (sp->param_flags & ~SPP_SACKDELAY) |
2327 SPP_SACKDELAY_DISABLE;
2328 }
2329 }
2330
2331 /* If change is for association, also apply to each transport. */
2332 if (asoc) {
2333 struct list_head *pos;
2334
2335 list_for_each(pos, &asoc->peer.transport_addr_list) {
2336 trans = list_entry(pos, struct sctp_transport,
2337 transports);
2338 if (params.assoc_value) {
2339 trans->sackdelay =
2340 msecs_to_jiffies(params.assoc_value);
2341 trans->param_flags =
2342 (trans->param_flags & ~SPP_SACKDELAY) |
2343 SPP_SACKDELAY_ENABLE;
2344 } else {
2345 trans->param_flags =
2346 (trans->param_flags & ~SPP_SACKDELAY) |
2347 SPP_SACKDELAY_DISABLE;
2348 }
2349 }
2350 }
2351
2352 return 0;
2353}
2354
1da177e4
LT
2355/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2356 *
2357 * Applications can specify protocol parameters for the default association
2358 * initialization. The option name argument to setsockopt() and getsockopt()
2359 * is SCTP_INITMSG.
2360 *
2361 * Setting initialization parameters is effective only on an unconnected
2362 * socket (for UDP-style sockets only future associations are effected
2363 * by the change). With TCP-style sockets, this option is inherited by
2364 * sockets derived from a listener socket.
2365 */
2366static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2367{
2368 struct sctp_initmsg sinit;
2369 struct sctp_sock *sp = sctp_sk(sk);
2370
2371 if (optlen != sizeof(struct sctp_initmsg))
2372 return -EINVAL;
2373 if (copy_from_user(&sinit, optval, optlen))
2374 return -EFAULT;
2375
2376 if (sinit.sinit_num_ostreams)
2377 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2378 if (sinit.sinit_max_instreams)
2379 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2380 if (sinit.sinit_max_attempts)
2381 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2382 if (sinit.sinit_max_init_timeo)
2383 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2384
2385 return 0;
2386}
2387
2388/*
2389 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2390 *
2391 * Applications that wish to use the sendto() system call may wish to
2392 * specify a default set of parameters that would normally be supplied
2393 * through the inclusion of ancillary data. This socket option allows
2394 * such an application to set the default sctp_sndrcvinfo structure.
2395 * The application that wishes to use this socket option simply passes
2396 * in to this call the sctp_sndrcvinfo structure defined in Section
2397 * 5.2.2) The input parameters accepted by this call include
2398 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2399 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2400 * to this call if the caller is using the UDP model.
2401 */
2402static int sctp_setsockopt_default_send_param(struct sock *sk,
2403 char __user *optval, int optlen)
2404{
2405 struct sctp_sndrcvinfo info;
2406 struct sctp_association *asoc;
2407 struct sctp_sock *sp = sctp_sk(sk);
2408
2409 if (optlen != sizeof(struct sctp_sndrcvinfo))
2410 return -EINVAL;
2411 if (copy_from_user(&info, optval, optlen))
2412 return -EFAULT;
2413
2414 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2415 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2416 return -EINVAL;
2417
2418 if (asoc) {
2419 asoc->default_stream = info.sinfo_stream;
2420 asoc->default_flags = info.sinfo_flags;
2421 asoc->default_ppid = info.sinfo_ppid;
2422 asoc->default_context = info.sinfo_context;
2423 asoc->default_timetolive = info.sinfo_timetolive;
2424 } else {
2425 sp->default_stream = info.sinfo_stream;
2426 sp->default_flags = info.sinfo_flags;
2427 sp->default_ppid = info.sinfo_ppid;
2428 sp->default_context = info.sinfo_context;
2429 sp->default_timetolive = info.sinfo_timetolive;
2430 }
2431
2432 return 0;
2433}
2434
2435/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2436 *
2437 * Requests that the local SCTP stack use the enclosed peer address as
2438 * the association primary. The enclosed address must be one of the
2439 * association peer's addresses.
2440 */
2441static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2442 int optlen)
2443{
2444 struct sctp_prim prim;
2445 struct sctp_transport *trans;
2446
2447 if (optlen != sizeof(struct sctp_prim))
2448 return -EINVAL;
2449
2450 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2451 return -EFAULT;
2452
2453 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2454 if (!trans)
2455 return -EINVAL;
2456
2457 sctp_assoc_set_primary(trans->asoc, trans);
2458
2459 return 0;
2460}
2461
2462/*
2463 * 7.1.5 SCTP_NODELAY
2464 *
2465 * Turn on/off any Nagle-like algorithm. This means that packets are
2466 * generally sent as soon as possible and no unnecessary delays are
2467 * introduced, at the cost of more packets in the network. Expects an
2468 * integer boolean flag.
2469 */
2470static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2471 int optlen)
2472{
2473 int val;
2474
2475 if (optlen < sizeof(int))
2476 return -EINVAL;
2477 if (get_user(val, (int __user *)optval))
2478 return -EFAULT;
2479
2480 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2481 return 0;
2482}
2483
2484/*
2485 *
2486 * 7.1.1 SCTP_RTOINFO
2487 *
2488 * The protocol parameters used to initialize and bound retransmission
2489 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2490 * and modify these parameters.
2491 * All parameters are time values, in milliseconds. A value of 0, when
2492 * modifying the parameters, indicates that the current value should not
2493 * be changed.
2494 *
2495 */
2496static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2497 struct sctp_rtoinfo rtoinfo;
2498 struct sctp_association *asoc;
2499
2500 if (optlen != sizeof (struct sctp_rtoinfo))
2501 return -EINVAL;
2502
2503 if (copy_from_user(&rtoinfo, optval, optlen))
2504 return -EFAULT;
2505
2506 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2507
2508 /* Set the values to the specific association */
2509 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2510 return -EINVAL;
2511
2512 if (asoc) {
2513 if (rtoinfo.srto_initial != 0)
2514 asoc->rto_initial =
2515 msecs_to_jiffies(rtoinfo.srto_initial);
2516 if (rtoinfo.srto_max != 0)
2517 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2518 if (rtoinfo.srto_min != 0)
2519 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2520 } else {
2521 /* If there is no association or the association-id = 0
2522 * set the values to the endpoint.
2523 */
2524 struct sctp_sock *sp = sctp_sk(sk);
2525
2526 if (rtoinfo.srto_initial != 0)
2527 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2528 if (rtoinfo.srto_max != 0)
2529 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2530 if (rtoinfo.srto_min != 0)
2531 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2532 }
2533
2534 return 0;
2535}
2536
2537/*
2538 *
2539 * 7.1.2 SCTP_ASSOCINFO
2540 *
2541 * This option is used to tune the the maximum retransmission attempts
2542 * of the association.
2543 * Returns an error if the new association retransmission value is
2544 * greater than the sum of the retransmission value of the peer.
2545 * See [SCTP] for more information.
2546 *
2547 */
2548static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2549{
2550
2551 struct sctp_assocparams assocparams;
2552 struct sctp_association *asoc;
2553
2554 if (optlen != sizeof(struct sctp_assocparams))
2555 return -EINVAL;
2556 if (copy_from_user(&assocparams, optval, optlen))
2557 return -EFAULT;
2558
2559 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2560
2561 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2562 return -EINVAL;
2563
2564 /* Set the values to the specific association */
2565 if (asoc) {
402d68c4
VY
2566 if (assocparams.sasoc_asocmaxrxt != 0) {
2567 __u32 path_sum = 0;
2568 int paths = 0;
2569 struct list_head *pos;
2570 struct sctp_transport *peer_addr;
2571
2572 list_for_each(pos, &asoc->peer.transport_addr_list) {
2573 peer_addr = list_entry(pos,
2574 struct sctp_transport,
2575 transports);
2576 path_sum += peer_addr->pathmaxrxt;
2577 paths++;
2578 }
2579
2580 /* Only validate asocmaxrxt if we have more then
2581 * one path/transport. We do this because path
2582 * retransmissions are only counted when we have more
2583 * then one path.
2584 */
2585 if (paths > 1 &&
2586 assocparams.sasoc_asocmaxrxt > path_sum)
2587 return -EINVAL;
2588
1da177e4 2589 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
402d68c4
VY
2590 }
2591
1da177e4
LT
2592 if (assocparams.sasoc_cookie_life != 0) {
2593 asoc->cookie_life.tv_sec =
2594 assocparams.sasoc_cookie_life / 1000;
2595 asoc->cookie_life.tv_usec =
2596 (assocparams.sasoc_cookie_life % 1000)
2597 * 1000;
2598 }
2599 } else {
2600 /* Set the values to the endpoint */
2601 struct sctp_sock *sp = sctp_sk(sk);
2602
2603 if (assocparams.sasoc_asocmaxrxt != 0)
2604 sp->assocparams.sasoc_asocmaxrxt =
2605 assocparams.sasoc_asocmaxrxt;
2606 if (assocparams.sasoc_cookie_life != 0)
2607 sp->assocparams.sasoc_cookie_life =
2608 assocparams.sasoc_cookie_life;
2609 }
2610 return 0;
2611}
2612
2613/*
2614 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2615 *
2616 * This socket option is a boolean flag which turns on or off mapped V4
2617 * addresses. If this option is turned on and the socket is type
2618 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2619 * If this option is turned off, then no mapping will be done of V4
2620 * addresses and a user will receive both PF_INET6 and PF_INET type
2621 * addresses on the socket.
2622 */
2623static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2624{
2625 int val;
2626 struct sctp_sock *sp = sctp_sk(sk);
2627
2628 if (optlen < sizeof(int))
2629 return -EINVAL;
2630 if (get_user(val, (int __user *)optval))
2631 return -EFAULT;
2632 if (val)
2633 sp->v4mapped = 1;
2634 else
2635 sp->v4mapped = 0;
2636
2637 return 0;
2638}
2639
2640/*
2641 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2642 *
2643 * This socket option specifies the maximum size to put in any outgoing
2644 * SCTP chunk. If a message is larger than this size it will be
2645 * fragmented by SCTP into the specified size. Note that the underlying
2646 * SCTP implementation may fragment into smaller sized chunks when the
2647 * PMTU of the underlying association is smaller than the value set by
2648 * the user.
2649 */
2650static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2651{
2652 struct sctp_association *asoc;
2653 struct list_head *pos;
2654 struct sctp_sock *sp = sctp_sk(sk);
2655 int val;
2656
2657 if (optlen < sizeof(int))
2658 return -EINVAL;
2659 if (get_user(val, (int __user *)optval))
2660 return -EFAULT;
96a33998 2661 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
1da177e4
LT
2662 return -EINVAL;
2663 sp->user_frag = val;
2664
96a33998
ISJ
2665 /* Update the frag_point of the existing associations. */
2666 list_for_each(pos, &(sp->ep->asocs)) {
2667 asoc = list_entry(pos, struct sctp_association, asocs);
52ccb8e9 2668 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
1da177e4
LT
2669 }
2670
2671 return 0;
2672}
2673
2674
2675/*
2676 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2677 *
2678 * Requests that the peer mark the enclosed address as the association
2679 * primary. The enclosed address must be one of the association's
2680 * locally bound addresses. The following structure is used to make a
2681 * set primary request:
2682 */
2683static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2684 int optlen)
2685{
2686 struct sctp_sock *sp;
2687 struct sctp_endpoint *ep;
2688 struct sctp_association *asoc = NULL;
2689 struct sctp_setpeerprim prim;
2690 struct sctp_chunk *chunk;
2691 int err;
2692
2693 sp = sctp_sk(sk);
2694 ep = sp->ep;
2695
2696 if (!sctp_addip_enable)
2697 return -EPERM;
2698
2699 if (optlen != sizeof(struct sctp_setpeerprim))
2700 return -EINVAL;
2701
2702 if (copy_from_user(&prim, optval, optlen))
2703 return -EFAULT;
2704
2705 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2706 if (!asoc)
2707 return -EINVAL;
2708
2709 if (!asoc->peer.asconf_capable)
2710 return -EPERM;
2711
2712 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2713 return -EPERM;
2714
2715 if (!sctp_state(asoc, ESTABLISHED))
2716 return -ENOTCONN;
2717
2718 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2719 return -EADDRNOTAVAIL;
2720
2721 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2722 chunk = sctp_make_asconf_set_prim(asoc,
2723 (union sctp_addr *)&prim.sspp_addr);
2724 if (!chunk)
2725 return -ENOMEM;
2726
2727 err = sctp_send_asconf(asoc, chunk);
2728
2729 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2730
2731 return err;
2732}
2733
0f3fffd8 2734static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
1da177e4
LT
2735 int optlen)
2736{
0f3fffd8 2737 struct sctp_setadaptation adaptation;
1da177e4 2738
0f3fffd8 2739 if (optlen != sizeof(struct sctp_setadaptation))
1da177e4 2740 return -EINVAL;
0f3fffd8 2741 if (copy_from_user(&adaptation, optval, optlen))
1da177e4
LT
2742 return -EFAULT;
2743
0f3fffd8 2744 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
1da177e4
LT
2745
2746 return 0;
2747}
2748
6ab792f5
ISJ
2749/*
2750 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2751 *
2752 * The context field in the sctp_sndrcvinfo structure is normally only
2753 * used when a failed message is retrieved holding the value that was
2754 * sent down on the actual send call. This option allows the setting of
2755 * a default context on an association basis that will be received on
2756 * reading messages from the peer. This is especially helpful in the
2757 * one-2-many model for an application to keep some reference to an
2758 * internal state machine that is processing messages on the
2759 * association. Note that the setting of this value only effects
2760 * received messages from the peer and does not effect the value that is
2761 * saved with outbound messages.
2762 */
2763static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2764 int optlen)
2765{
2766 struct sctp_assoc_value params;
2767 struct sctp_sock *sp;
2768 struct sctp_association *asoc;
2769
2770 if (optlen != sizeof(struct sctp_assoc_value))
2771 return -EINVAL;
2772 if (copy_from_user(&params, optval, optlen))
2773 return -EFAULT;
2774
2775 sp = sctp_sk(sk);
2776
2777 if (params.assoc_id != 0) {
2778 asoc = sctp_id2assoc(sk, params.assoc_id);
2779 if (!asoc)
2780 return -EINVAL;
2781 asoc->default_rcv_context = params.assoc_value;
2782 } else {
2783 sp->default_rcv_context = params.assoc_value;
2784 }
2785
2786 return 0;
2787}
2788
1da177e4
LT
2789/* API 6.2 setsockopt(), getsockopt()
2790 *
2791 * Applications use setsockopt() and getsockopt() to set or retrieve
2792 * socket options. Socket options are used to change the default
2793 * behavior of sockets calls. They are described in Section 7.
2794 *
2795 * The syntax is:
2796 *
2797 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2798 * int __user *optlen);
2799 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2800 * int optlen);
2801 *
2802 * sd - the socket descript.
2803 * level - set to IPPROTO_SCTP for all SCTP options.
2804 * optname - the option name.
2805 * optval - the buffer to store the value of the option.
2806 * optlen - the size of the buffer.
2807 */
2808SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2809 char __user *optval, int optlen)
2810{
2811 int retval = 0;
2812
2813 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2814 sk, optname);
2815
2816 /* I can hardly begin to describe how wrong this is. This is
2817 * so broken as to be worse than useless. The API draft
2818 * REALLY is NOT helpful here... I am not convinced that the
2819 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2820 * are at all well-founded.
2821 */
2822 if (level != SOL_SCTP) {
2823 struct sctp_af *af = sctp_sk(sk)->pf->af;
2824 retval = af->setsockopt(sk, level, optname, optval, optlen);
2825 goto out_nounlock;
2826 }
2827
2828 sctp_lock_sock(sk);
2829
2830 switch (optname) {
2831 case SCTP_SOCKOPT_BINDX_ADD:
2832 /* 'optlen' is the size of the addresses buffer. */
2833 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2834 optlen, SCTP_BINDX_ADD_ADDR);
2835 break;
2836
2837 case SCTP_SOCKOPT_BINDX_REM:
2838 /* 'optlen' is the size of the addresses buffer. */
2839 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2840 optlen, SCTP_BINDX_REM_ADDR);
2841 break;
2842
3f7a87d2
FF
2843 case SCTP_SOCKOPT_CONNECTX:
2844 /* 'optlen' is the size of the addresses buffer. */
2845 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2846 optlen);
2847 break;
2848
1da177e4
LT
2849 case SCTP_DISABLE_FRAGMENTS:
2850 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2851 break;
2852
2853 case SCTP_EVENTS:
2854 retval = sctp_setsockopt_events(sk, optval, optlen);
2855 break;
2856
2857 case SCTP_AUTOCLOSE:
2858 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2859 break;
2860
2861 case SCTP_PEER_ADDR_PARAMS:
2862 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2863 break;
2864
7708610b
FF
2865 case SCTP_DELAYED_ACK_TIME:
2866 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2867 break;
2868
1da177e4
LT
2869 case SCTP_INITMSG:
2870 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2871 break;
2872 case SCTP_DEFAULT_SEND_PARAM:
2873 retval = sctp_setsockopt_default_send_param(sk, optval,
2874 optlen);
2875 break;
2876 case SCTP_PRIMARY_ADDR:
2877 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2878 break;
2879 case SCTP_SET_PEER_PRIMARY_ADDR:
2880 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2881 break;
2882 case SCTP_NODELAY:
2883 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2884 break;
2885 case SCTP_RTOINFO:
2886 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2887 break;
2888 case SCTP_ASSOCINFO:
2889 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2890 break;
2891 case SCTP_I_WANT_MAPPED_V4_ADDR:
2892 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2893 break;
2894 case SCTP_MAXSEG:
2895 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2896 break;
0f3fffd8
ISJ
2897 case SCTP_ADAPTATION_LAYER:
2898 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
1da177e4 2899 break;
6ab792f5
ISJ
2900 case SCTP_CONTEXT:
2901 retval = sctp_setsockopt_context(sk, optval, optlen);
2902 break;
1da177e4
LT
2903
2904 default:
2905 retval = -ENOPROTOOPT;
2906 break;
2907 };
2908
2909 sctp_release_sock(sk);
2910
2911out_nounlock:
2912 return retval;
2913}
2914
2915/* API 3.1.6 connect() - UDP Style Syntax
2916 *
2917 * An application may use the connect() call in the UDP model to initiate an
2918 * association without sending data.
2919 *
2920 * The syntax is:
2921 *
2922 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2923 *
2924 * sd: the socket descriptor to have a new association added to.
2925 *
2926 * nam: the address structure (either struct sockaddr_in or struct
2927 * sockaddr_in6 defined in RFC2553 [7]).
2928 *
2929 * len: the size of the address.
2930 */
3f7a87d2 2931SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
1da177e4
LT
2932 int addr_len)
2933{
1da177e4 2934 int err = 0;
3f7a87d2 2935 struct sctp_af *af;
1da177e4
LT
2936
2937 sctp_lock_sock(sk);
2938
3f7a87d2
FF
2939 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2940 __FUNCTION__, sk, addr, addr_len);
1da177e4 2941
3f7a87d2
FF
2942 /* Validate addr_len before calling common connect/connectx routine. */
2943 af = sctp_get_af_specific(addr->sa_family);
2944 if (!af || addr_len < af->sockaddr_len) {
2945 err = -EINVAL;
2946 } else {
2947 /* Pass correct addr len to common routine (so it knows there
2948 * is only one address being passed.
2949 */
2950 err = __sctp_connect(sk, addr, af->sockaddr_len);
1da177e4
LT
2951 }
2952
1da177e4 2953 sctp_release_sock(sk);
1da177e4
LT
2954 return err;
2955}
2956
2957/* FIXME: Write comments. */
2958SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2959{
2960 return -EOPNOTSUPP; /* STUB */
2961}
2962
2963/* 4.1.4 accept() - TCP Style Syntax
2964 *
2965 * Applications use accept() call to remove an established SCTP
2966 * association from the accept queue of the endpoint. A new socket
2967 * descriptor will be returned from accept() to represent the newly
2968 * formed association.
2969 */
2970SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2971{
2972 struct sctp_sock *sp;
2973 struct sctp_endpoint *ep;
2974 struct sock *newsk = NULL;
2975 struct sctp_association *asoc;
2976 long timeo;
2977 int error = 0;
2978
2979 sctp_lock_sock(sk);
2980
2981 sp = sctp_sk(sk);
2982 ep = sp->ep;
2983
2984 if (!sctp_style(sk, TCP)) {
2985 error = -EOPNOTSUPP;
2986 goto out;
2987 }
2988
2989 if (!sctp_sstate(sk, LISTENING)) {
2990 error = -EINVAL;
2991 goto out;
2992 }
2993
8abfedd8 2994 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1da177e4
LT
2995
2996 error = sctp_wait_for_accept(sk, timeo);
2997 if (error)
2998 goto out;
2999
3000 /* We treat the list of associations on the endpoint as the accept
3001 * queue and pick the first association on the list.
3002 */
3003 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3004
3005 newsk = sp->pf->create_accept_sk(sk, asoc);
3006 if (!newsk) {
3007 error = -ENOMEM;
3008 goto out;
3009 }
3010
3011 /* Populate the fields of the newsk from the oldsk and migrate the
3012 * asoc to the newsk.
3013 */
3014 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3015
3016out:
3017 sctp_release_sock(sk);
3018 *err = error;
3019 return newsk;
3020}
3021
3022/* The SCTP ioctl handler. */
3023SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3024{
3025 return -ENOIOCTLCMD;
3026}
3027
3028/* This is the function which gets called during socket creation to
3029 * initialized the SCTP-specific portion of the sock.
3030 * The sock structure should already be zero-filled memory.
3031 */
3032SCTP_STATIC int sctp_init_sock(struct sock *sk)
3033{
3034 struct sctp_endpoint *ep;
3035 struct sctp_sock *sp;
3036
3037 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3038
3039 sp = sctp_sk(sk);
3040
3041 /* Initialize the SCTP per socket area. */
3042 switch (sk->sk_type) {
3043 case SOCK_SEQPACKET:
3044 sp->type = SCTP_SOCKET_UDP;
3045 break;
3046 case SOCK_STREAM:
3047 sp->type = SCTP_SOCKET_TCP;
3048 break;
3049 default:
3050 return -ESOCKTNOSUPPORT;
3051 }
3052
3053 /* Initialize default send parameters. These parameters can be
3054 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3055 */
3056 sp->default_stream = 0;
3057 sp->default_ppid = 0;
3058 sp->default_flags = 0;
3059 sp->default_context = 0;
3060 sp->default_timetolive = 0;
3061
6ab792f5
ISJ
3062 sp->default_rcv_context = 0;
3063
1da177e4
LT
3064 /* Initialize default setup parameters. These parameters
3065 * can be modified with the SCTP_INITMSG socket option or
3066 * overridden by the SCTP_INIT CMSG.
3067 */
3068 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3069 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3070 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3fd091e7 3071 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
1da177e4
LT
3072
3073 /* Initialize default RTO related parameters. These parameters can
3074 * be modified for with the SCTP_RTOINFO socket option.
3075 */
3fd091e7
VY
3076 sp->rtoinfo.srto_initial = sctp_rto_initial;
3077 sp->rtoinfo.srto_max = sctp_rto_max;
3078 sp->rtoinfo.srto_min = sctp_rto_min;
1da177e4
LT
3079
3080 /* Initialize default association related parameters. These parameters
3081 * can be modified with the SCTP_ASSOCINFO socket option.
3082 */
3083 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3084 sp->assocparams.sasoc_number_peer_destinations = 0;
3085 sp->assocparams.sasoc_peer_rwnd = 0;
3086 sp->assocparams.sasoc_local_rwnd = 0;
3fd091e7 3087 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
1da177e4
LT
3088
3089 /* Initialize default event subscriptions. By default, all the
3090 * options are off.
3091 */
3092 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3093
3094 /* Default Peer Address Parameters. These defaults can
3095 * be modified via SCTP_PEER_ADDR_PARAMS
3096 */
3fd091e7 3097 sp->hbinterval = sctp_hb_interval;
52ccb8e9
FF
3098 sp->pathmaxrxt = sctp_max_retrans_path;
3099 sp->pathmtu = 0; // allow default discovery
3fd091e7 3100 sp->sackdelay = sctp_sack_timeout;
52ccb8e9
FF
3101 sp->param_flags = SPP_HB_ENABLE |
3102 SPP_PMTUD_ENABLE |
3103 SPP_SACKDELAY_ENABLE;
1da177e4
LT
3104
3105 /* If enabled no SCTP message fragmentation will be performed.
3106 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3107 */
3108 sp->disable_fragments = 0;
3109
208edef6
SS
3110 /* Enable Nagle algorithm by default. */
3111 sp->nodelay = 0;
1da177e4
LT
3112
3113 /* Enable by default. */
3114 sp->v4mapped = 1;
3115
3116 /* Auto-close idle associations after the configured
3117 * number of seconds. A value of 0 disables this
3118 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3119 * for UDP-style sockets only.
3120 */
3121 sp->autoclose = 0;
3122
3123 /* User specified fragmentation limit. */
3124 sp->user_frag = 0;
3125
0f3fffd8 3126 sp->adaptation_ind = 0;
1da177e4
LT
3127
3128 sp->pf = sctp_get_pf_specific(sk->sk_family);
3129
3130 /* Control variables for partial data delivery. */
3131 sp->pd_mode = 0;
3132 skb_queue_head_init(&sp->pd_lobby);
3133
3134 /* Create a per socket endpoint structure. Even if we
3135 * change the data structure relationships, this may still
3136 * be useful for storing pre-connect address information.
3137 */
3138 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3139 if (!ep)
3140 return -ENOMEM;
3141
3142 sp->ep = ep;
3143 sp->hmac = NULL;
3144
3145 SCTP_DBG_OBJCNT_INC(sock);
3146 return 0;
3147}
3148
3149/* Cleanup any SCTP per socket resources. */
3150SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3151{
3152 struct sctp_endpoint *ep;
3153
3154 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3155
3156 /* Release our hold on the endpoint. */
3157 ep = sctp_sk(sk)->ep;
3158 sctp_endpoint_free(ep);
3159
3160 return 0;
3161}
3162
3163/* API 4.1.7 shutdown() - TCP Style Syntax
3164 * int shutdown(int socket, int how);
3165 *
3166 * sd - the socket descriptor of the association to be closed.
3167 * how - Specifies the type of shutdown. The values are
3168 * as follows:
3169 * SHUT_RD
3170 * Disables further receive operations. No SCTP
3171 * protocol action is taken.
3172 * SHUT_WR
3173 * Disables further send operations, and initiates
3174 * the SCTP shutdown sequence.
3175 * SHUT_RDWR
3176 * Disables further send and receive operations
3177 * and initiates the SCTP shutdown sequence.
3178 */
3179SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3180{
3181 struct sctp_endpoint *ep;
3182 struct sctp_association *asoc;
3183
3184 if (!sctp_style(sk, TCP))
3185 return;
3186
3187 if (how & SEND_SHUTDOWN) {
3188 ep = sctp_sk(sk)->ep;
3189 if (!list_empty(&ep->asocs)) {
3190 asoc = list_entry(ep->asocs.next,
3191 struct sctp_association, asocs);
3192 sctp_primitive_SHUTDOWN(asoc, NULL);
3193 }
3194 }
3195}
3196
3197/* 7.2.1 Association Status (SCTP_STATUS)
3198
3199 * Applications can retrieve current status information about an
3200 * association, including association state, peer receiver window size,
3201 * number of unacked data chunks, and number of data chunks pending
3202 * receipt. This information is read-only.
3203 */
3204static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3205 char __user *optval,
3206 int __user *optlen)
3207{
3208 struct sctp_status status;
3209 struct sctp_association *asoc = NULL;
3210 struct sctp_transport *transport;
3211 sctp_assoc_t associd;
3212 int retval = 0;
3213
3214 if (len != sizeof(status)) {
3215 retval = -EINVAL;
3216 goto out;
3217 }
3218
3219 if (copy_from_user(&status, optval, sizeof(status))) {
3220 retval = -EFAULT;
3221 goto out;
3222 }
3223
3224 associd = status.sstat_assoc_id;
3225 asoc = sctp_id2assoc(sk, associd);
3226 if (!asoc) {
3227 retval = -EINVAL;
3228 goto out;
3229 }
3230
3231 transport = asoc->peer.primary_path;
3232
3233 status.sstat_assoc_id = sctp_assoc2id(asoc);
3234 status.sstat_state = asoc->state;
3235 status.sstat_rwnd = asoc->peer.rwnd;
3236 status.sstat_unackdata = asoc->unack_data;
3237
3238 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3239 status.sstat_instrms = asoc->c.sinit_max_instreams;
3240 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3241 status.sstat_fragmentation_point = asoc->frag_point;
3242 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
8cec6b80
AV
3243 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3244 transport->af_specific->sockaddr_len);
1da177e4
LT
3245 /* Map ipv4 address into v4-mapped-on-v6 address. */
3246 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3247 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3f7a87d2 3248 status.sstat_primary.spinfo_state = transport->state;
1da177e4
LT
3249 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3250 status.sstat_primary.spinfo_srtt = transport->srtt;
3251 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 3252 status.sstat_primary.spinfo_mtu = transport->pathmtu;
1da177e4 3253
3f7a87d2
FF
3254 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3255 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3256
1da177e4
LT
3257 if (put_user(len, optlen)) {
3258 retval = -EFAULT;
3259 goto out;
3260 }
3261
3262 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3263 len, status.sstat_state, status.sstat_rwnd,
3264 status.sstat_assoc_id);
3265
3266 if (copy_to_user(optval, &status, len)) {
3267 retval = -EFAULT;
3268 goto out;
3269 }
3270
3271out:
3272 return (retval);
3273}
3274
3275
3276/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3277 *
3278 * Applications can retrieve information about a specific peer address
3279 * of an association, including its reachability state, congestion
3280 * window, and retransmission timer values. This information is
3281 * read-only.
3282 */
3283static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3284 char __user *optval,
3285 int __user *optlen)
3286{
3287 struct sctp_paddrinfo pinfo;
3288 struct sctp_transport *transport;
3289 int retval = 0;
3290
3291 if (len != sizeof(pinfo)) {
3292 retval = -EINVAL;
3293 goto out;
3294 }
3295
3296 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3297 retval = -EFAULT;
3298 goto out;
3299 }
3300
3301 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3302 pinfo.spinfo_assoc_id);
3303 if (!transport)
3304 return -EINVAL;
3305
3306 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3f7a87d2 3307 pinfo.spinfo_state = transport->state;
1da177e4
LT
3308 pinfo.spinfo_cwnd = transport->cwnd;
3309 pinfo.spinfo_srtt = transport->srtt;
3310 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 3311 pinfo.spinfo_mtu = transport->pathmtu;
1da177e4 3312
3f7a87d2
FF
3313 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3314 pinfo.spinfo_state = SCTP_ACTIVE;
3315
1da177e4
LT
3316 if (put_user(len, optlen)) {
3317 retval = -EFAULT;
3318 goto out;
3319 }
3320
3321 if (copy_to_user(optval, &pinfo, len)) {
3322 retval = -EFAULT;
3323 goto out;
3324 }
3325
3326out:
3327 return (retval);
3328}
3329
3330/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3331 *
3332 * This option is a on/off flag. If enabled no SCTP message
3333 * fragmentation will be performed. Instead if a message being sent
3334 * exceeds the current PMTU size, the message will NOT be sent and
3335 * instead a error will be indicated to the user.
3336 */
3337static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3338 char __user *optval, int __user *optlen)
3339{
3340 int val;
3341
3342 if (len < sizeof(int))
3343 return -EINVAL;
3344
3345 len = sizeof(int);
3346 val = (sctp_sk(sk)->disable_fragments == 1);
3347 if (put_user(len, optlen))
3348 return -EFAULT;
3349 if (copy_to_user(optval, &val, len))
3350 return -EFAULT;
3351 return 0;
3352}
3353
3354/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3355 *
3356 * This socket option is used to specify various notifications and
3357 * ancillary data the user wishes to receive.
3358 */
3359static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3360 int __user *optlen)
3361{
3362 if (len != sizeof(struct sctp_event_subscribe))
3363 return -EINVAL;
3364 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3365 return -EFAULT;
3366 return 0;
3367}
3368
3369/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3370 *
3371 * This socket option is applicable to the UDP-style socket only. When
3372 * set it will cause associations that are idle for more than the
3373 * specified number of seconds to automatically close. An association
3374 * being idle is defined an association that has NOT sent or received
3375 * user data. The special value of '0' indicates that no automatic
3376 * close of any associations should be performed. The option expects an
3377 * integer defining the number of seconds of idle time before an
3378 * association is closed.
3379 */
3380static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3381{
3382 /* Applicable to UDP-style socket only */
3383 if (sctp_style(sk, TCP))
3384 return -EOPNOTSUPP;
3385 if (len != sizeof(int))
3386 return -EINVAL;
3387 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3388 return -EFAULT;
3389 return 0;
3390}
3391
3392/* Helper routine to branch off an association to a new socket. */
3393SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3394 struct socket **sockp)
3395{
3396 struct sock *sk = asoc->base.sk;
3397 struct socket *sock;
4f444308 3398 struct inet_sock *inetsk;
1da177e4
LT
3399 int err = 0;
3400
3401 /* An association cannot be branched off from an already peeled-off
3402 * socket, nor is this supported for tcp style sockets.
3403 */
3404 if (!sctp_style(sk, UDP))
3405 return -EINVAL;
3406
3407 /* Create a new socket. */
3408 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3409 if (err < 0)
3410 return err;
3411
3412 /* Populate the fields of the newsk from the oldsk and migrate the
3413 * asoc to the newsk.
3414 */
3415 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
4f444308
VY
3416
3417 /* Make peeled-off sockets more like 1-1 accepted sockets.
3418 * Set the daddr and initialize id to something more random
3419 */
3420 inetsk = inet_sk(sock->sk);
3421 inetsk->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
3422 inetsk->id = asoc->next_tsn ^ jiffies;
3423
1da177e4
LT
3424 *sockp = sock;
3425
3426 return err;
3427}
3428
3429static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3430{
3431 sctp_peeloff_arg_t peeloff;
3432 struct socket *newsock;
3433 int retval = 0;
3434 struct sctp_association *asoc;
3435
3436 if (len != sizeof(sctp_peeloff_arg_t))
3437 return -EINVAL;
3438 if (copy_from_user(&peeloff, optval, len))
3439 return -EFAULT;
3440
3441 asoc = sctp_id2assoc(sk, peeloff.associd);
3442 if (!asoc) {
3443 retval = -EINVAL;
3444 goto out;
3445 }
3446
3447 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3448
3449 retval = sctp_do_peeloff(asoc, &newsock);
3450 if (retval < 0)
3451 goto out;
3452
3453 /* Map the socket to an unused fd that can be returned to the user. */
3454 retval = sock_map_fd(newsock);
3455 if (retval < 0) {
3456 sock_release(newsock);
3457 goto out;
3458 }
3459
3460 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3461 __FUNCTION__, sk, asoc, newsock->sk, retval);
3462
3463 /* Return the fd mapped to the new socket. */
3464 peeloff.sd = retval;
3465 if (copy_to_user(optval, &peeloff, len))
3466 retval = -EFAULT;
3467
3468out:
3469 return retval;
3470}
3471
3472/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3473 *
3474 * Applications can enable or disable heartbeats for any peer address of
3475 * an association, modify an address's heartbeat interval, force a
3476 * heartbeat to be sent immediately, and adjust the address's maximum
3477 * number of retransmissions sent before an address is considered
3478 * unreachable. The following structure is used to access and modify an
3479 * address's parameters:
3480 *
3481 * struct sctp_paddrparams {
52ccb8e9
FF
3482 * sctp_assoc_t spp_assoc_id;
3483 * struct sockaddr_storage spp_address;
3484 * uint32_t spp_hbinterval;
3485 * uint16_t spp_pathmaxrxt;
3486 * uint32_t spp_pathmtu;
3487 * uint32_t spp_sackdelay;
3488 * uint32_t spp_flags;
3489 * };
3490 *
3491 * spp_assoc_id - (one-to-many style socket) This is filled in the
3492 * application, and identifies the association for
3493 * this query.
1da177e4
LT
3494 * spp_address - This specifies which address is of interest.
3495 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
3496 * in milliseconds. If a value of zero
3497 * is present in this field then no changes are to
3498 * be made to this parameter.
1da177e4
LT
3499 * spp_pathmaxrxt - This contains the maximum number of
3500 * retransmissions before this address shall be
52ccb8e9
FF
3501 * considered unreachable. If a value of zero
3502 * is present in this field then no changes are to
3503 * be made to this parameter.
3504 * spp_pathmtu - When Path MTU discovery is disabled the value
3505 * specified here will be the "fixed" path mtu.
3506 * Note that if the spp_address field is empty
3507 * then all associations on this address will
3508 * have this fixed path mtu set upon them.
3509 *
3510 * spp_sackdelay - When delayed sack is enabled, this value specifies
3511 * the number of milliseconds that sacks will be delayed
3512 * for. This value will apply to all addresses of an
3513 * association if the spp_address field is empty. Note
3514 * also, that if delayed sack is enabled and this
3515 * value is set to 0, no change is made to the last
3516 * recorded delayed sack timer value.
3517 *
3518 * spp_flags - These flags are used to control various features
3519 * on an association. The flag field may contain
3520 * zero or more of the following options.
3521 *
3522 * SPP_HB_ENABLE - Enable heartbeats on the
3523 * specified address. Note that if the address
3524 * field is empty all addresses for the association
3525 * have heartbeats enabled upon them.
3526 *
3527 * SPP_HB_DISABLE - Disable heartbeats on the
3528 * speicifed address. Note that if the address
3529 * field is empty all addresses for the association
3530 * will have their heartbeats disabled. Note also
3531 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3532 * mutually exclusive, only one of these two should
3533 * be specified. Enabling both fields will have
3534 * undetermined results.
3535 *
3536 * SPP_HB_DEMAND - Request a user initiated heartbeat
3537 * to be made immediately.
3538 *
3539 * SPP_PMTUD_ENABLE - This field will enable PMTU
3540 * discovery upon the specified address. Note that
3541 * if the address feild is empty then all addresses
3542 * on the association are effected.
3543 *
3544 * SPP_PMTUD_DISABLE - This field will disable PMTU
3545 * discovery upon the specified address. Note that
3546 * if the address feild is empty then all addresses
3547 * on the association are effected. Not also that
3548 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3549 * exclusive. Enabling both will have undetermined
3550 * results.
3551 *
3552 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3553 * on delayed sack. The time specified in spp_sackdelay
3554 * is used to specify the sack delay for this address. Note
3555 * that if spp_address is empty then all addresses will
3556 * enable delayed sack and take on the sack delay
3557 * value specified in spp_sackdelay.
3558 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3559 * off delayed sack. If the spp_address field is blank then
3560 * delayed sack is disabled for the entire association. Note
3561 * also that this field is mutually exclusive to
3562 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3563 * results.
1da177e4
LT
3564 */
3565static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
52ccb8e9 3566 char __user *optval, int __user *optlen)
1da177e4 3567{
52ccb8e9
FF
3568 struct sctp_paddrparams params;
3569 struct sctp_transport *trans = NULL;
3570 struct sctp_association *asoc = NULL;
3571 struct sctp_sock *sp = sctp_sk(sk);
1da177e4
LT
3572
3573 if (len != sizeof(struct sctp_paddrparams))
3574 return -EINVAL;
52ccb8e9 3575
1da177e4
LT
3576 if (copy_from_user(&params, optval, len))
3577 return -EFAULT;
3578
52ccb8e9
FF
3579 /* If an address other than INADDR_ANY is specified, and
3580 * no transport is found, then the request is invalid.
1da177e4 3581 */
52ccb8e9
FF
3582 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3583 trans = sctp_addr_id2transport(sk, &params.spp_address,
3584 params.spp_assoc_id);
3585 if (!trans) {
3586 SCTP_DEBUG_PRINTK("Failed no transport\n");
3587 return -EINVAL;
3588 }
1da177e4
LT
3589 }
3590
52ccb8e9
FF
3591 /* Get association, if assoc_id != 0 and the socket is a one
3592 * to many style socket, and an association was not found, then
3593 * the id was invalid.
3594 */
3595 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3596 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3597 SCTP_DEBUG_PRINTK("Failed no association\n");
1da177e4 3598 return -EINVAL;
52ccb8e9 3599 }
1da177e4 3600
52ccb8e9
FF
3601 if (trans) {
3602 /* Fetch transport values. */
3603 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3604 params.spp_pathmtu = trans->pathmtu;
3605 params.spp_pathmaxrxt = trans->pathmaxrxt;
3606 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
3607
3608 /*draft-11 doesn't say what to return in spp_flags*/
3609 params.spp_flags = trans->param_flags;
3610 } else if (asoc) {
3611 /* Fetch association values. */
3612 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3613 params.spp_pathmtu = asoc->pathmtu;
3614 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3615 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
3616
3617 /*draft-11 doesn't say what to return in spp_flags*/
3618 params.spp_flags = asoc->param_flags;
3619 } else {
3620 /* Fetch socket values. */
3621 params.spp_hbinterval = sp->hbinterval;
3622 params.spp_pathmtu = sp->pathmtu;
3623 params.spp_sackdelay = sp->sackdelay;
3624 params.spp_pathmaxrxt = sp->pathmaxrxt;
1da177e4 3625
52ccb8e9
FF
3626 /*draft-11 doesn't say what to return in spp_flags*/
3627 params.spp_flags = sp->param_flags;
3628 }
1da177e4 3629
1da177e4
LT
3630 if (copy_to_user(optval, &params, len))
3631 return -EFAULT;
3632
3633 if (put_user(len, optlen))
3634 return -EFAULT;
3635
3636 return 0;
3637}
3638
7708610b
FF
3639/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3640 *
3641 * This options will get or set the delayed ack timer. The time is set
3642 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3643 * endpoints default delayed ack timer value. If the assoc_id field is
3644 * non-zero, then the set or get effects the specified association.
3645 *
3646 * struct sctp_assoc_value {
3647 * sctp_assoc_t assoc_id;
3648 * uint32_t assoc_value;
3649 * };
3650 *
3651 * assoc_id - This parameter, indicates which association the
3652 * user is preforming an action upon. Note that if
3653 * this field's value is zero then the endpoints
3654 * default value is changed (effecting future
3655 * associations only).
3656 *
3657 * assoc_value - This parameter contains the number of milliseconds
3658 * that the user is requesting the delayed ACK timer
3659 * be set to. Note that this value is defined in
3660 * the standard to be between 200 and 500 milliseconds.
3661 *
3662 * Note: a value of zero will leave the value alone,
3663 * but disable SACK delay. A non-zero value will also
3664 * enable SACK delay.
3665 */
3666static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3667 char __user *optval,
3668 int __user *optlen)
3669{
3670 struct sctp_assoc_value params;
3671 struct sctp_association *asoc = NULL;
3672 struct sctp_sock *sp = sctp_sk(sk);
3673
3674 if (len != sizeof(struct sctp_assoc_value))
3675 return - EINVAL;
3676
3677 if (copy_from_user(&params, optval, len))
3678 return -EFAULT;
3679
3680 /* Get association, if assoc_id != 0 and the socket is a one
3681 * to many style socket, and an association was not found, then
3682 * the id was invalid.
3683 */
3684 asoc = sctp_id2assoc(sk, params.assoc_id);
3685 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3686 return -EINVAL;
3687
3688 if (asoc) {
3689 /* Fetch association values. */
3690 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3691 params.assoc_value = jiffies_to_msecs(
3692 asoc->sackdelay);
3693 else
3694 params.assoc_value = 0;
3695 } else {
3696 /* Fetch socket values. */
3697 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3698 params.assoc_value = sp->sackdelay;
3699 else
3700 params.assoc_value = 0;
3701 }
3702
3703 if (copy_to_user(optval, &params, len))
3704 return -EFAULT;
3705
3706 if (put_user(len, optlen))
3707 return -EFAULT;
3708
3709 return 0;
3710}
3711
1da177e4
LT
3712/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3713 *
3714 * Applications can specify protocol parameters for the default association
3715 * initialization. The option name argument to setsockopt() and getsockopt()
3716 * is SCTP_INITMSG.
3717 *
3718 * Setting initialization parameters is effective only on an unconnected
3719 * socket (for UDP-style sockets only future associations are effected
3720 * by the change). With TCP-style sockets, this option is inherited by
3721 * sockets derived from a listener socket.
3722 */
3723static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3724{
3725 if (len != sizeof(struct sctp_initmsg))
3726 return -EINVAL;
3727 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3728 return -EFAULT;
3729 return 0;
3730}
3731