sctp: Push struct net down into sctp_verify_ext_param
[deliverable/linux.git] / net / sctp / socket.c
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
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 *
60c778b2 9 * This file is part of the SCTP kernel implementation
1da177e4
LT
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 *
60c778b2 18 * This SCTP implementation is free software;
1da177e4
LT
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 *
60c778b2 24 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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
145ce502
JP
60#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61
1da177e4
LT
62#include <linux/types.h>
63#include <linux/kernel.h>
64#include <linux/wait.h>
65#include <linux/time.h>
66#include <linux/ip.h>
4fc268d2 67#include <linux/capability.h>
1da177e4
LT
68#include <linux/fcntl.h>
69#include <linux/poll.h>
70#include <linux/init.h>
71#include <linux/crypto.h>
5a0e3ad6 72#include <linux/slab.h>
1da177e4
LT
73
74#include <net/ip.h>
75#include <net/icmp.h>
76#include <net/route.h>
77#include <net/ipv6.h>
78#include <net/inet_common.h>
79
80#include <linux/socket.h> /* for sa_family_t */
bc3b2d7f 81#include <linux/export.h>
1da177e4
LT
82#include <net/sock.h>
83#include <net/sctp/sctp.h>
84#include <net/sctp/sm.h>
85
86/* WARNING: Please do not remove the SCTP_STATIC attribute to
87 * any of the functions below as they are used to export functions
88 * used by a project regression testsuite.
89 */
90
91/* Forward declarations for internal helper functions. */
92static int sctp_writeable(struct sock *sk);
93static void sctp_wfree(struct sk_buff *skb);
94static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
95 size_t msg_len);
96static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
97static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
98static int sctp_wait_for_accept(struct sock *sk, long timeo);
99static void sctp_wait_for_close(struct sock *sk, long timeo);
100static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
101 union sctp_addr *addr, int len);
102static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
103static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
104static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
105static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
106static int sctp_send_asconf(struct sctp_association *asoc,
107 struct sctp_chunk *chunk);
108static int sctp_do_bind(struct sock *, union sctp_addr *, int);
109static int sctp_autobind(struct sock *sk);
110static void sctp_sock_migrate(struct sock *, struct sock *,
111 struct sctp_association *, sctp_socket_type_t);
112static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
113
4d93df0a 114extern struct kmem_cache *sctp_bucket_cachep;
8d987e5c 115extern long sysctl_sctp_mem[3];
4d93df0a
NH
116extern int sysctl_sctp_rmem[3];
117extern int sysctl_sctp_wmem[3];
118
b6fa1a4d 119static int sctp_memory_pressure;
8d987e5c 120static atomic_long_t sctp_memory_allocated;
1748376b 121struct percpu_counter sctp_sockets_allocated;
4d93df0a 122
5c52ba17 123static void sctp_enter_memory_pressure(struct sock *sk)
4d93df0a
NH
124{
125 sctp_memory_pressure = 1;
126}
127
128
1da177e4
LT
129/* Get the sndbuf space available at the time on the association. */
130static inline int sctp_wspace(struct sctp_association *asoc)
131{
4d93df0a 132 int amt;
1da177e4 133
4d93df0a
NH
134 if (asoc->ep->sndbuf_policy)
135 amt = asoc->sndbuf_used;
136 else
31e6d363 137 amt = sk_wmem_alloc_get(asoc->base.sk);
4d93df0a
NH
138
139 if (amt >= asoc->base.sk->sk_sndbuf) {
140 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
141 amt = 0;
142 else {
143 amt = sk_stream_wspace(asoc->base.sk);
144 if (amt < 0)
145 amt = 0;
146 }
4eb701df 147 } else {
4d93df0a 148 amt = asoc->base.sk->sk_sndbuf - amt;
4eb701df 149 }
1da177e4
LT
150 return amt;
151}
152
153/* Increment the used sndbuf space count of the corresponding association by
154 * the size of the outgoing data chunk.
155 * Also, set the skb destructor for sndbuf accounting later.
156 *
157 * Since it is always 1-1 between chunk and skb, and also a new skb is always
158 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
159 * destructor in the data chunk skb for the purpose of the sndbuf space
160 * tracking.
161 */
162static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
163{
164 struct sctp_association *asoc = chunk->asoc;
165 struct sock *sk = asoc->base.sk;
166
167 /* The sndbuf space is tracked per association. */
168 sctp_association_hold(asoc);
169
4eb701df
NH
170 skb_set_owner_w(chunk->skb, sk);
171
1da177e4
LT
172 chunk->skb->destructor = sctp_wfree;
173 /* Save the chunk pointer in skb for sctp_wfree to use later. */
174 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
175
4eb701df
NH
176 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
177 sizeof(struct sk_buff) +
178 sizeof(struct sctp_chunk);
179
4eb701df 180 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
3ab224be
HA
181 sk->sk_wmem_queued += chunk->skb->truesize;
182 sk_mem_charge(sk, chunk->skb->truesize);
1da177e4
LT
183}
184
185/* Verify that this is a valid address. */
186static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
187 int len)
188{
189 struct sctp_af *af;
190
191 /* Verify basic sockaddr. */
192 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
193 if (!af)
194 return -EINVAL;
195
196 /* Is this a valid SCTP address? */
5636bef7 197 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
1da177e4
LT
198 return -EINVAL;
199
200 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
201 return -EINVAL;
202
203 return 0;
204}
205
206/* Look up the association by its id. If this is not a UDP-style
207 * socket, the ID field is always ignored.
208 */
209struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
210{
211 struct sctp_association *asoc = NULL;
212
213 /* If this is not a UDP-style socket, assoc id should be ignored. */
214 if (!sctp_style(sk, UDP)) {
215 /* Return NULL if the socket state is not ESTABLISHED. It
216 * could be a TCP-style listening socket or a socket which
217 * hasn't yet called connect() to establish an association.
218 */
219 if (!sctp_sstate(sk, ESTABLISHED))
220 return NULL;
221
222 /* Get the first and the only association from the list. */
223 if (!list_empty(&sctp_sk(sk)->ep->asocs))
224 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
225 struct sctp_association, asocs);
226 return asoc;
227 }
228
229 /* Otherwise this is a UDP-style socket. */
230 if (!id || (id == (sctp_assoc_t)-1))
231 return NULL;
232
233 spin_lock_bh(&sctp_assocs_id_lock);
234 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
235 spin_unlock_bh(&sctp_assocs_id_lock);
236
237 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
238 return NULL;
239
240 return asoc;
241}
242
243/* Look up the transport from an address and an assoc id. If both address and
244 * id are specified, the associations matching the address and the id should be
245 * the same.
246 */
247static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
248 struct sockaddr_storage *addr,
249 sctp_assoc_t id)
250{
251 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
252 struct sctp_transport *transport;
253 union sctp_addr *laddr = (union sctp_addr *)addr;
254
1da177e4 255 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
cd4ff034 256 laddr,
1da177e4 257 &transport);
1da177e4
LT
258
259 if (!addr_asoc)
260 return NULL;
261
262 id_asoc = sctp_id2assoc(sk, id);
263 if (id_asoc && (id_asoc != addr_asoc))
264 return NULL;
265
266 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
267 (union sctp_addr *)addr);
268
269 return transport;
270}
271
272/* API 3.1.2 bind() - UDP Style Syntax
273 * The syntax of bind() is,
274 *
275 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
276 *
277 * sd - the socket descriptor returned by socket().
278 * addr - the address structure (struct sockaddr_in or struct
279 * sockaddr_in6 [RFC 2553]),
280 * addr_len - the size of the address structure.
281 */
3f7a87d2 282SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
1da177e4
LT
283{
284 int retval = 0;
285
286 sctp_lock_sock(sk);
287
3f7a87d2
FF
288 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
289 sk, addr, addr_len);
1da177e4
LT
290
291 /* Disallow binding twice. */
292 if (!sctp_sk(sk)->ep->base.bind_addr.port)
3f7a87d2 293 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
1da177e4
LT
294 addr_len);
295 else
296 retval = -EINVAL;
297
298 sctp_release_sock(sk);
299
300 return retval;
301}
302
303static long sctp_get_port_local(struct sock *, union sctp_addr *);
304
305/* Verify this is a valid sockaddr. */
306static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
307 union sctp_addr *addr, int len)
308{
309 struct sctp_af *af;
310
311 /* Check minimum size. */
312 if (len < sizeof (struct sockaddr))
313 return NULL;
314
7dab83de
VY
315 /* V4 mapped address are really of AF_INET family */
316 if (addr->sa.sa_family == AF_INET6 &&
317 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
318 if (!opt->pf->af_supported(AF_INET, opt))
319 return NULL;
320 } else {
321 /* Does this PF support this AF? */
322 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
323 return NULL;
324 }
1da177e4
LT
325
326 /* If we get this far, af is valid. */
327 af = sctp_get_af_specific(addr->sa.sa_family);
328
329 if (len < af->sockaddr_len)
330 return NULL;
331
332 return af;
333}
334
335/* Bind a local address either to an endpoint or to an association. */
336SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
337{
338 struct sctp_sock *sp = sctp_sk(sk);
339 struct sctp_endpoint *ep = sp->ep;
340 struct sctp_bind_addr *bp = &ep->base.bind_addr;
341 struct sctp_af *af;
342 unsigned short snum;
343 int ret = 0;
344
1da177e4
LT
345 /* Common sockaddr verification. */
346 af = sctp_sockaddr_af(sp, addr, len);
3f7a87d2
FF
347 if (!af) {
348 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
349 sk, addr, len);
1da177e4 350 return -EINVAL;
3f7a87d2
FF
351 }
352
353 snum = ntohs(addr->v4.sin_port);
354
355 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
356 ", port: %d, new port: %d, len: %d)\n",
357 sk,
358 addr,
359 bp->port, snum,
360 len);
1da177e4
LT
361
362 /* PF specific bind() address verification. */
363 if (!sp->pf->bind_verify(sp, addr))
364 return -EADDRNOTAVAIL;
365
8b358056
VY
366 /* We must either be unbound, or bind to the same port.
367 * It's OK to allow 0 ports if we are already bound.
368 * We'll just inhert an already bound port in this case
369 */
370 if (bp->port) {
371 if (!snum)
372 snum = bp->port;
373 else if (snum != bp->port) {
374 SCTP_DEBUG_PRINTK("sctp_do_bind:"
1da177e4
LT
375 " New port %d does not match existing port "
376 "%d.\n", snum, bp->port);
8b358056
VY
377 return -EINVAL;
378 }
1da177e4
LT
379 }
380
381 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
382 return -EACCES;
383
4e54064e
VY
384 /* See if the address matches any of the addresses we may have
385 * already bound before checking against other endpoints.
386 */
387 if (sctp_bind_addr_match(bp, addr, sp))
388 return -EINVAL;
389
1da177e4
LT
390 /* Make sure we are allowed to bind here.
391 * The function sctp_get_port_local() does duplicate address
392 * detection.
393 */
2772b495 394 addr->v4.sin_port = htons(snum);
1da177e4 395 if ((ret = sctp_get_port_local(sk, addr))) {
4e54064e 396 return -EADDRINUSE;
1da177e4
LT
397 }
398
399 /* Refresh ephemeral port. */
400 if (!bp->port)
c720c7e8 401 bp->port = inet_sk(sk)->inet_num;
1da177e4 402
559cf710
VY
403 /* Add the address to the bind address list.
404 * Use GFP_ATOMIC since BHs will be disabled.
405 */
f57d96b2 406 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
1da177e4
LT
407
408 /* Copy back into socket for getsockname() use. */
409 if (!ret) {
c720c7e8 410 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
1da177e4
LT
411 af->to_sk_saddr(addr, sk);
412 }
413
414 return ret;
415}
416
417 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
418 *
d808ad9a 419 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
1da177e4 420 * at any one time. If a sender, after sending an ASCONF chunk, decides
d808ad9a 421 * it needs to transfer another ASCONF Chunk, it MUST wait until the
1da177e4 422 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
d808ad9a
YH
423 * subsequent ASCONF. Note this restriction binds each side, so at any
424 * time two ASCONF may be in-transit on any given association (one sent
1da177e4
LT
425 * from each endpoint).
426 */
427static int sctp_send_asconf(struct sctp_association *asoc,
428 struct sctp_chunk *chunk)
429{
55e26eb9 430 struct net *net = sock_net(asoc->base.sk);
1da177e4
LT
431 int retval = 0;
432
433 /* If there is an outstanding ASCONF chunk, queue it for later
434 * transmission.
d808ad9a 435 */
1da177e4 436 if (asoc->addip_last_asconf) {
79af02c2 437 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
d808ad9a 438 goto out;
1da177e4
LT
439 }
440
441 /* Hold the chunk until an ASCONF_ACK is received. */
442 sctp_chunk_hold(chunk);
55e26eb9 443 retval = sctp_primitive_ASCONF(net, asoc, chunk);
1da177e4
LT
444 if (retval)
445 sctp_chunk_free(chunk);
446 else
447 asoc->addip_last_asconf = chunk;
448
449out:
450 return retval;
451}
452
453/* Add a list of addresses as bind addresses to local endpoint or
454 * association.
455 *
456 * Basically run through each address specified in the addrs/addrcnt
457 * array/length pair, determine if it is IPv6 or IPv4 and call
458 * sctp_do_bind() on it.
459 *
460 * If any of them fails, then the operation will be reversed and the
461 * ones that were added will be removed.
462 *
463 * Only sctp_setsockopt_bindx() is supposed to call this function.
464 */
04675210 465static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
1da177e4
LT
466{
467 int cnt;
468 int retval = 0;
469 void *addr_buf;
470 struct sockaddr *sa_addr;
471 struct sctp_af *af;
472
473 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
474 sk, addrs, addrcnt);
475
476 addr_buf = addrs;
477 for (cnt = 0; cnt < addrcnt; cnt++) {
478 /* The list may contain either IPv4 or IPv6 address;
479 * determine the address length for walking thru the list.
480 */
ea110733 481 sa_addr = addr_buf;
1da177e4
LT
482 af = sctp_get_af_specific(sa_addr->sa_family);
483 if (!af) {
484 retval = -EINVAL;
485 goto err_bindx_add;
486 }
487
d808ad9a 488 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
1da177e4
LT
489 af->sockaddr_len);
490
491 addr_buf += af->sockaddr_len;
492
493err_bindx_add:
494 if (retval < 0) {
495 /* Failed. Cleanup the ones that have been added */
496 if (cnt > 0)
497 sctp_bindx_rem(sk, addrs, cnt);
498 return retval;
499 }
500 }
501
502 return retval;
503}
504
505/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
506 * associations that are part of the endpoint indicating that a list of local
507 * addresses are added to the endpoint.
508 *
d808ad9a 509 * If any of the addresses is already in the bind address list of the
1da177e4
LT
510 * association, we do not send the chunk for that association. But it will not
511 * affect other associations.
512 *
513 * Only sctp_setsockopt_bindx() is supposed to call this function.
514 */
d808ad9a 515static int sctp_send_asconf_add_ip(struct sock *sk,
1da177e4
LT
516 struct sockaddr *addrs,
517 int addrcnt)
518{
519 struct sctp_sock *sp;
520 struct sctp_endpoint *ep;
521 struct sctp_association *asoc;
522 struct sctp_bind_addr *bp;
523 struct sctp_chunk *chunk;
524 struct sctp_sockaddr_entry *laddr;
525 union sctp_addr *addr;
dc022a98 526 union sctp_addr saveaddr;
1da177e4
LT
527 void *addr_buf;
528 struct sctp_af *af;
1da177e4
LT
529 struct list_head *p;
530 int i;
531 int retval = 0;
532
533 if (!sctp_addip_enable)
534 return retval;
535
536 sp = sctp_sk(sk);
537 ep = sp->ep;
538
539 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
0dc47877 540 __func__, sk, addrs, addrcnt);
1da177e4 541
9dbc15f0 542 list_for_each_entry(asoc, &ep->asocs, asocs) {
1da177e4
LT
543
544 if (!asoc->peer.asconf_capable)
545 continue;
546
547 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
548 continue;
549
550 if (!sctp_state(asoc, ESTABLISHED))
551 continue;
552
553 /* Check if any address in the packed array of addresses is
d808ad9a
YH
554 * in the bind address list of the association. If so,
555 * do not send the asconf chunk to its peer, but continue with
1da177e4
LT
556 * other associations.
557 */
558 addr_buf = addrs;
559 for (i = 0; i < addrcnt; i++) {
ea110733 560 addr = addr_buf;
1da177e4
LT
561 af = sctp_get_af_specific(addr->v4.sin_family);
562 if (!af) {
563 retval = -EINVAL;
564 goto out;
565 }
566
567 if (sctp_assoc_lookup_laddr(asoc, addr))
568 break;
569
570 addr_buf += af->sockaddr_len;
571 }
572 if (i < addrcnt)
573 continue;
574
559cf710
VY
575 /* Use the first valid address in bind addr list of
576 * association as Address Parameter of ASCONF CHUNK.
1da177e4 577 */
1da177e4
LT
578 bp = &asoc->base.bind_addr;
579 p = bp->address_list.next;
580 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
5ae955cf 581 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
1da177e4
LT
582 addrcnt, SCTP_PARAM_ADD_IP);
583 if (!chunk) {
584 retval = -ENOMEM;
585 goto out;
586 }
587
dc022a98
SS
588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
1da177e4 590 */
dc022a98
SS
591 addr_buf = addrs;
592 for (i = 0; i < addrcnt; i++) {
ea110733 593 addr = addr_buf;
dc022a98
SS
594 af = sctp_get_af_specific(addr->v4.sin_family);
595 memcpy(&saveaddr, addr, af->sockaddr_len);
f57d96b2
VY
596 retval = sctp_add_bind_addr(bp, &saveaddr,
597 SCTP_ADDR_NEW, GFP_ATOMIC);
dc022a98
SS
598 addr_buf += af->sockaddr_len;
599 }
8a07eb0a
MH
600 if (asoc->src_out_of_asoc_ok) {
601 struct sctp_transport *trans;
602
603 list_for_each_entry(trans,
604 &asoc->peer.transport_addr_list, transports) {
605 /* Clear the source and route cache */
606 dst_release(trans->dst);
607 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
608 2*asoc->pathmtu, 4380));
609 trans->ssthresh = asoc->peer.i.a_rwnd;
610 trans->rto = asoc->rto_initial;
611 trans->rtt = trans->srtt = trans->rttvar = 0;
612 sctp_transport_route(trans, NULL,
613 sctp_sk(asoc->base.sk));
614 }
615 }
616 retval = sctp_send_asconf(asoc, chunk);
1da177e4
LT
617 }
618
619out:
620 return retval;
621}
622
623/* Remove a list of addresses from bind addresses list. Do not remove the
624 * last address.
625 *
626 * Basically run through each address specified in the addrs/addrcnt
627 * array/length pair, determine if it is IPv6 or IPv4 and call
628 * sctp_del_bind() on it.
629 *
630 * If any of them fails, then the operation will be reversed and the
631 * ones that were removed will be added back.
632 *
633 * At least one address has to be left; if only one address is
634 * available, the operation will return -EBUSY.
635 *
636 * Only sctp_setsockopt_bindx() is supposed to call this function.
637 */
04675210 638static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
1da177e4
LT
639{
640 struct sctp_sock *sp = sctp_sk(sk);
641 struct sctp_endpoint *ep = sp->ep;
642 int cnt;
643 struct sctp_bind_addr *bp = &ep->base.bind_addr;
644 int retval = 0;
1da177e4 645 void *addr_buf;
c9a08505 646 union sctp_addr *sa_addr;
1da177e4
LT
647 struct sctp_af *af;
648
649 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
650 sk, addrs, addrcnt);
651
652 addr_buf = addrs;
653 for (cnt = 0; cnt < addrcnt; cnt++) {
654 /* If the bind address list is empty or if there is only one
655 * bind address, there is nothing more to be removed (we need
656 * at least one address here).
657 */
658 if (list_empty(&bp->address_list) ||
659 (sctp_list_single_entry(&bp->address_list))) {
660 retval = -EBUSY;
661 goto err_bindx_rem;
662 }
663
ea110733 664 sa_addr = addr_buf;
c9a08505 665 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1da177e4
LT
666 if (!af) {
667 retval = -EINVAL;
668 goto err_bindx_rem;
669 }
0304ff8a
PG
670
671 if (!af->addr_valid(sa_addr, sp, NULL)) {
672 retval = -EADDRNOTAVAIL;
673 goto err_bindx_rem;
674 }
675
ee9cbaca
VY
676 if (sa_addr->v4.sin_port &&
677 sa_addr->v4.sin_port != htons(bp->port)) {
1da177e4
LT
678 retval = -EINVAL;
679 goto err_bindx_rem;
680 }
681
ee9cbaca
VY
682 if (!sa_addr->v4.sin_port)
683 sa_addr->v4.sin_port = htons(bp->port);
684
1da177e4
LT
685 /* FIXME - There is probably a need to check if sk->sk_saddr and
686 * sk->sk_rcv_addr are currently set to one of the addresses to
687 * be removed. This is something which needs to be looked into
688 * when we are fixing the outstanding issues with multi-homing
689 * socket routing and failover schemes. Refer to comments in
690 * sctp_do_bind(). -daisy
691 */
0ed90fb0 692 retval = sctp_del_bind_addr(bp, sa_addr);
1da177e4
LT
693
694 addr_buf += af->sockaddr_len;
695err_bindx_rem:
696 if (retval < 0) {
697 /* Failed. Add the ones that has been removed back */
698 if (cnt > 0)
699 sctp_bindx_add(sk, addrs, cnt);
700 return retval;
701 }
702 }
703
704 return retval;
705}
706
707/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
708 * the associations that are part of the endpoint indicating that a list of
709 * local addresses are removed from the endpoint.
710 *
d808ad9a 711 * If any of the addresses is already in the bind address list of the
1da177e4
LT
712 * association, we do not send the chunk for that association. But it will not
713 * affect other associations.
714 *
715 * Only sctp_setsockopt_bindx() is supposed to call this function.
716 */
717static int sctp_send_asconf_del_ip(struct sock *sk,
718 struct sockaddr *addrs,
719 int addrcnt)
720{
721 struct sctp_sock *sp;
722 struct sctp_endpoint *ep;
723 struct sctp_association *asoc;
dc022a98 724 struct sctp_transport *transport;
1da177e4
LT
725 struct sctp_bind_addr *bp;
726 struct sctp_chunk *chunk;
727 union sctp_addr *laddr;
728 void *addr_buf;
729 struct sctp_af *af;
dc022a98 730 struct sctp_sockaddr_entry *saddr;
1da177e4
LT
731 int i;
732 int retval = 0;
8a07eb0a 733 int stored = 0;
1da177e4 734
8a07eb0a 735 chunk = NULL;
1da177e4
LT
736 if (!sctp_addip_enable)
737 return retval;
738
739 sp = sctp_sk(sk);
740 ep = sp->ep;
741
742 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
0dc47877 743 __func__, sk, addrs, addrcnt);
1da177e4 744
9dbc15f0 745 list_for_each_entry(asoc, &ep->asocs, asocs) {
1da177e4
LT
746
747 if (!asoc->peer.asconf_capable)
748 continue;
749
750 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
751 continue;
752
753 if (!sctp_state(asoc, ESTABLISHED))
754 continue;
755
756 /* Check if any address in the packed array of addresses is
d808ad9a 757 * not present in the bind address list of the association.
1da177e4
LT
758 * If so, do not send the asconf chunk to its peer, but
759 * continue with other associations.
760 */
761 addr_buf = addrs;
762 for (i = 0; i < addrcnt; i++) {
ea110733 763 laddr = addr_buf;
1da177e4
LT
764 af = sctp_get_af_specific(laddr->v4.sin_family);
765 if (!af) {
766 retval = -EINVAL;
767 goto out;
768 }
769
770 if (!sctp_assoc_lookup_laddr(asoc, laddr))
771 break;
772
773 addr_buf += af->sockaddr_len;
774 }
775 if (i < addrcnt)
776 continue;
777
778 /* Find one address in the association's bind address list
779 * that is not in the packed array of addresses. This is to
780 * make sure that we do not delete all the addresses in the
781 * association.
782 */
1da177e4
LT
783 bp = &asoc->base.bind_addr;
784 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
785 addrcnt, sp);
8a07eb0a
MH
786 if ((laddr == NULL) && (addrcnt == 1)) {
787 if (asoc->asconf_addr_del_pending)
788 continue;
789 asoc->asconf_addr_del_pending =
790 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
6d65e5ee
MH
791 if (asoc->asconf_addr_del_pending == NULL) {
792 retval = -ENOMEM;
793 goto out;
794 }
8a07eb0a
MH
795 asoc->asconf_addr_del_pending->sa.sa_family =
796 addrs->sa_family;
797 asoc->asconf_addr_del_pending->v4.sin_port =
798 htons(bp->port);
799 if (addrs->sa_family == AF_INET) {
800 struct sockaddr_in *sin;
801
802 sin = (struct sockaddr_in *)addrs;
803 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
804 } else if (addrs->sa_family == AF_INET6) {
805 struct sockaddr_in6 *sin6;
806
807 sin6 = (struct sockaddr_in6 *)addrs;
4e3fd7a0 808 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
8a07eb0a
MH
809 }
810 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
811 " at %p\n", asoc, asoc->asconf_addr_del_pending,
812 asoc->asconf_addr_del_pending);
813 asoc->src_out_of_asoc_ok = 1;
814 stored = 1;
815 goto skip_mkasconf;
816 }
1da177e4 817
559cf710
VY
818 /* We do not need RCU protection throughout this loop
819 * because this is done under a socket lock from the
820 * setsockopt call.
821 */
1da177e4
LT
822 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
823 SCTP_PARAM_DEL_IP);
824 if (!chunk) {
825 retval = -ENOMEM;
826 goto out;
827 }
828
8a07eb0a 829skip_mkasconf:
dc022a98
SS
830 /* Reset use_as_src flag for the addresses in the bind address
831 * list that are to be deleted.
832 */
dc022a98
SS
833 addr_buf = addrs;
834 for (i = 0; i < addrcnt; i++) {
ea110733 835 laddr = addr_buf;
dc022a98 836 af = sctp_get_af_specific(laddr->v4.sin_family);
559cf710 837 list_for_each_entry(saddr, &bp->address_list, list) {
5f242a13 838 if (sctp_cmp_addr_exact(&saddr->a, laddr))
f57d96b2 839 saddr->state = SCTP_ADDR_DEL;
dc022a98
SS
840 }
841 addr_buf += af->sockaddr_len;
842 }
1da177e4 843
dc022a98
SS
844 /* Update the route and saddr entries for all the transports
845 * as some of the addresses in the bind address list are
846 * about to be deleted and cannot be used as source addresses.
1da177e4 847 */
9dbc15f0
RD
848 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
849 transports) {
dc022a98
SS
850 dst_release(transport->dst);
851 sctp_transport_route(transport, NULL,
852 sctp_sk(asoc->base.sk));
853 }
854
8a07eb0a
MH
855 if (stored)
856 /* We don't need to transmit ASCONF */
857 continue;
dc022a98 858 retval = sctp_send_asconf(asoc, chunk);
1da177e4
LT
859 }
860out:
861 return retval;
862}
863
9f7d653b
MH
864/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
865int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
866{
867 struct sock *sk = sctp_opt2sk(sp);
868 union sctp_addr *addr;
869 struct sctp_af *af;
870
871 /* It is safe to write port space in caller. */
872 addr = &addrw->a;
873 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
874 af = sctp_get_af_specific(addr->sa.sa_family);
875 if (!af)
876 return -EINVAL;
877 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
878 return -EINVAL;
879
880 if (addrw->state == SCTP_ADDR_NEW)
881 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
882 else
883 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
884}
885
1da177e4
LT
886/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
887 *
888 * API 8.1
889 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
890 * int flags);
891 *
892 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
893 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
894 * or IPv6 addresses.
895 *
896 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
897 * Section 3.1.2 for this usage.
898 *
899 * addrs is a pointer to an array of one or more socket addresses. Each
900 * address is contained in its appropriate structure (i.e. struct
901 * sockaddr_in or struct sockaddr_in6) the family of the address type
23c435f7 902 * must be used to distinguish the address length (note that this
1da177e4
LT
903 * representation is termed a "packed array" of addresses). The caller
904 * specifies the number of addresses in the array with addrcnt.
905 *
906 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
907 * -1, and sets errno to the appropriate error code.
908 *
909 * For SCTP, the port given in each socket address must be the same, or
910 * sctp_bindx() will fail, setting errno to EINVAL.
911 *
912 * The flags parameter is formed from the bitwise OR of zero or more of
913 * the following currently defined flags:
914 *
915 * SCTP_BINDX_ADD_ADDR
916 *
917 * SCTP_BINDX_REM_ADDR
918 *
919 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
920 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
921 * addresses from the association. The two flags are mutually exclusive;
922 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
923 * not remove all addresses from an association; sctp_bindx() will
924 * reject such an attempt with EINVAL.
925 *
926 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
927 * additional addresses with an endpoint after calling bind(). Or use
928 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
929 * socket is associated with so that no new association accepted will be
930 * associated with those addresses. If the endpoint supports dynamic
931 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
932 * endpoint to send the appropriate message to the peer to change the
933 * peers address lists.
934 *
935 * Adding and removing addresses from a connected association is
936 * optional functionality. Implementations that do not support this
937 * functionality should return EOPNOTSUPP.
938 *
939 * Basically do nothing but copying the addresses from user to kernel
940 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
3f7a87d2
FF
941 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
942 * from userspace.
1da177e4
LT
943 *
944 * We don't use copy_from_user() for optimization: we first do the
945 * sanity checks (buffer size -fast- and access check-healthy
946 * pointer); if all of those succeed, then we can alloc the memory
947 * (expensive operation) needed to copy the data to kernel. Then we do
948 * the copying without checking the user space area
949 * (__copy_from_user()).
950 *
951 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
952 * it.
953 *
954 * sk The sk of the socket
955 * addrs The pointer to the addresses in user land
956 * addrssize Size of the addrs buffer
957 * op Operation to perform (add or remove, see the flags of
958 * sctp_bindx)
959 *
960 * Returns 0 if ok, <0 errno code on error.
961 */
962SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
963 struct sockaddr __user *addrs,
964 int addrs_size, int op)
965{
966 struct sockaddr *kaddrs;
967 int err;
968 int addrcnt = 0;
969 int walk_size = 0;
970 struct sockaddr *sa_addr;
971 void *addr_buf;
972 struct sctp_af *af;
973
974 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
975 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
976
977 if (unlikely(addrs_size <= 0))
978 return -EINVAL;
979
980 /* Check the user passed a healthy pointer. */
981 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
982 return -EFAULT;
983
984 /* Alloc space for the address array in kernel memory. */
8b3a7005 985 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1da177e4
LT
986 if (unlikely(!kaddrs))
987 return -ENOMEM;
988
989 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
990 kfree(kaddrs);
991 return -EFAULT;
992 }
993
d808ad9a 994 /* Walk through the addrs buffer and count the number of addresses. */
1da177e4
LT
995 addr_buf = kaddrs;
996 while (walk_size < addrs_size) {
d7e0d19a
DR
997 if (walk_size + sizeof(sa_family_t) > addrs_size) {
998 kfree(kaddrs);
999 return -EINVAL;
1000 }
1001
ea110733 1002 sa_addr = addr_buf;
1da177e4
LT
1003 af = sctp_get_af_specific(sa_addr->sa_family);
1004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
d808ad9a 1007 */
1da177e4
LT
1008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 kfree(kaddrs);
1010 return -EINVAL;
1011 }
1012 addrcnt++;
1013 addr_buf += af->sockaddr_len;
1014 walk_size += af->sockaddr_len;
1015 }
1016
1017 /* Do the work. */
1018 switch (op) {
1019 case SCTP_BINDX_ADD_ADDR:
1020 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1021 if (err)
1022 goto out;
1023 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1024 break;
1025
1026 case SCTP_BINDX_REM_ADDR:
1027 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1028 if (err)
1029 goto out;
1030 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1031 break;
1032
1033 default:
1034 err = -EINVAL;
1035 break;
3ff50b79 1036 }
1da177e4
LT
1037
1038out:
1039 kfree(kaddrs);
1040
1041 return err;
1042}
1043
3f7a87d2
FF
1044/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1045 *
1046 * Common routine for handling connect() and sctp_connectx().
1047 * Connect will come in with just a single address.
1048 */
1049static int __sctp_connect(struct sock* sk,
1050 struct sockaddr *kaddrs,
88a0a948
VY
1051 int addrs_size,
1052 sctp_assoc_t *assoc_id)
3f7a87d2 1053{
55e26eb9 1054 struct net *net = sock_net(sk);
3f7a87d2
FF
1055 struct sctp_sock *sp;
1056 struct sctp_endpoint *ep;
1057 struct sctp_association *asoc = NULL;
1058 struct sctp_association *asoc2;
1059 struct sctp_transport *transport;
1060 union sctp_addr to;
1061 struct sctp_af *af;
1062 sctp_scope_t scope;
1063 long timeo;
1064 int err = 0;
1065 int addrcnt = 0;
1066 int walk_size = 0;
e4d1feab 1067 union sctp_addr *sa_addr = NULL;
3f7a87d2 1068 void *addr_buf;
16d00fb7 1069 unsigned short port;
f50f95ca 1070 unsigned int f_flags = 0;
3f7a87d2
FF
1071
1072 sp = sctp_sk(sk);
1073 ep = sp->ep;
1074
1075 /* connect() cannot be done on a socket that is already in ESTABLISHED
1076 * state - UDP-style peeled off socket or a TCP-style socket that
1077 * is already connected.
1078 * It cannot be done even on a TCP-style listening socket.
1079 */
1080 if (sctp_sstate(sk, ESTABLISHED) ||
1081 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1082 err = -EISCONN;
1083 goto out_free;
1084 }
1085
1086 /* Walk through the addrs buffer and count the number of addresses. */
1087 addr_buf = kaddrs;
1088 while (walk_size < addrs_size) {
d7e0d19a
DR
1089 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1090 err = -EINVAL;
1091 goto out_free;
1092 }
1093
ea110733 1094 sa_addr = addr_buf;
4bdf4b5f 1095 af = sctp_get_af_specific(sa_addr->sa.sa_family);
3f7a87d2
FF
1096
1097 /* If the address family is not supported or if this address
1098 * causes the address buffer to overflow return EINVAL.
1099 */
1100 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1101 err = -EINVAL;
1102 goto out_free;
1103 }
1104
d7e0d19a
DR
1105 port = ntohs(sa_addr->v4.sin_port);
1106
e4d1feab
VY
1107 /* Save current address so we can work with it */
1108 memcpy(&to, sa_addr, af->sockaddr_len);
1109
1110 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
3f7a87d2
FF
1111 if (err)
1112 goto out_free;
1113
16d00fb7
VY
1114 /* Make sure the destination port is correctly set
1115 * in all addresses.
1116 */
1117 if (asoc && asoc->peer.port && asoc->peer.port != port)
1118 goto out_free;
1119
3f7a87d2
FF
1120
1121 /* Check if there already is a matching association on the
1122 * endpoint (other than the one created here).
1123 */
e4d1feab 1124 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
3f7a87d2
FF
1125 if (asoc2 && asoc2 != asoc) {
1126 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1127 err = -EISCONN;
1128 else
1129 err = -EALREADY;
1130 goto out_free;
1131 }
1132
1133 /* If we could not find a matching association on the endpoint,
1134 * make sure that there is no peeled-off association matching
1135 * the peer address even on another socket.
1136 */
e4d1feab 1137 if (sctp_endpoint_is_peeled_off(ep, &to)) {
3f7a87d2
FF
1138 err = -EADDRNOTAVAIL;
1139 goto out_free;
1140 }
1141
1142 if (!asoc) {
1143 /* If a bind() or sctp_bindx() is not called prior to
1144 * an sctp_connectx() call, the system picks an
1145 * ephemeral port and will choose an address set
1146 * equivalent to binding with a wildcard address.
1147 */
1148 if (!ep->base.bind_addr.port) {
1149 if (sctp_autobind(sk)) {
1150 err = -EAGAIN;
1151 goto out_free;
1152 }
64a0c1c8
ISJ
1153 } else {
1154 /*
d808ad9a
YH
1155 * If an unprivileged user inherits a 1-many
1156 * style socket with open associations on a
1157 * privileged port, it MAY be permitted to
1158 * accept new associations, but it SHOULD NOT
64a0c1c8
ISJ
1159 * be permitted to open new associations.
1160 */
1161 if (ep->base.bind_addr.port < PROT_SOCK &&
1162 !capable(CAP_NET_BIND_SERVICE)) {
1163 err = -EACCES;
1164 goto out_free;
1165 }
3f7a87d2
FF
1166 }
1167
e4d1feab 1168 scope = sctp_scope(&to);
3f7a87d2
FF
1169 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1170 if (!asoc) {
1171 err = -ENOMEM;
1172 goto out_free;
1173 }
409b95af
VY
1174
1175 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1176 GFP_KERNEL);
1177 if (err < 0) {
1178 goto out_free;
1179 }
1180
3f7a87d2
FF
1181 }
1182
1183 /* Prime the peer's transport structures. */
e4d1feab 1184 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
3f7a87d2
FF
1185 SCTP_UNKNOWN);
1186 if (!transport) {
1187 err = -ENOMEM;
1188 goto out_free;
1189 }
1190
1191 addrcnt++;
1192 addr_buf += af->sockaddr_len;
1193 walk_size += af->sockaddr_len;
1194 }
1195
c6ba68a2
VY
1196 /* In case the user of sctp_connectx() wants an association
1197 * id back, assign one now.
1198 */
1199 if (assoc_id) {
1200 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1201 if (err < 0)
1202 goto out_free;
1203 }
1204
55e26eb9 1205 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
3f7a87d2
FF
1206 if (err < 0) {
1207 goto out_free;
1208 }
1209
1210 /* Initialize sk's dport and daddr for getpeername() */
c720c7e8 1211 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
e4d1feab
VY
1212 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1213 af->to_sk_daddr(sa_addr, sk);
8de8c873 1214 sk->sk_err = 0;
3f7a87d2 1215
f50f95ca
VY
1216 /* in-kernel sockets don't generally have a file allocated to them
1217 * if all they do is call sock_create_kern().
1218 */
1219 if (sk->sk_socket->file)
1220 f_flags = sk->sk_socket->file->f_flags;
1221
1222 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1223
3f7a87d2 1224 err = sctp_wait_for_connect(asoc, &timeo);
c6ba68a2 1225 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
88a0a948 1226 *assoc_id = asoc->assoc_id;
3f7a87d2
FF
1227
1228 /* Don't free association on exit. */
1229 asoc = NULL;
1230
1231out_free:
1232
1233 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
d808ad9a
YH
1234 " kaddrs: %p err: %d\n",
1235 asoc, kaddrs, err);
2eebc1e1
NH
1236 if (asoc) {
1237 /* sctp_primitive_ASSOCIATE may have added this association
1238 * To the hash table, try to unhash it, just in case, its a noop
1239 * if it wasn't hashed so we're safe
1240 */
1241 sctp_unhash_established(asoc);
3f7a87d2 1242 sctp_association_free(asoc);
2eebc1e1 1243 }
3f7a87d2
FF
1244 return err;
1245}
1246
1247/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1248 *
1249 * API 8.9
88a0a948
VY
1250 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1251 * sctp_assoc_t *asoc);
3f7a87d2
FF
1252 *
1253 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1254 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1255 * or IPv6 addresses.
1256 *
1257 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1258 * Section 3.1.2 for this usage.
1259 *
1260 * addrs is a pointer to an array of one or more socket addresses. Each
1261 * address is contained in its appropriate structure (i.e. struct
1262 * sockaddr_in or struct sockaddr_in6) the family of the address type
1263 * must be used to distengish the address length (note that this
1264 * representation is termed a "packed array" of addresses). The caller
1265 * specifies the number of addresses in the array with addrcnt.
1266 *
88a0a948
VY
1267 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1268 * the association id of the new association. On failure, sctp_connectx()
1269 * returns -1, and sets errno to the appropriate error code. The assoc_id
1270 * is not touched by the kernel.
3f7a87d2
FF
1271 *
1272 * For SCTP, the port given in each socket address must be the same, or
1273 * sctp_connectx() will fail, setting errno to EINVAL.
1274 *
1275 * An application can use sctp_connectx to initiate an association with
1276 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1277 * allows a caller to specify multiple addresses at which a peer can be
1278 * reached. The way the SCTP stack uses the list of addresses to set up
25985edc 1279 * the association is implementation dependent. This function only
3f7a87d2
FF
1280 * specifies that the stack will try to make use of all the addresses in
1281 * the list when needed.
1282 *
1283 * Note that the list of addresses passed in is only used for setting up
1284 * the association. It does not necessarily equal the set of addresses
1285 * the peer uses for the resulting association. If the caller wants to
1286 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1287 * retrieve them after the association has been set up.
1288 *
1289 * Basically do nothing but copying the addresses from user to kernel
1290 * land and invoking either sctp_connectx(). This is used for tunneling
1291 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1292 *
1293 * We don't use copy_from_user() for optimization: we first do the
1294 * sanity checks (buffer size -fast- and access check-healthy
1295 * pointer); if all of those succeed, then we can alloc the memory
1296 * (expensive operation) needed to copy the data to kernel. Then we do
1297 * the copying without checking the user space area
1298 * (__copy_from_user()).
1299 *
1300 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1301 * it.
1302 *
1303 * sk The sk of the socket
1304 * addrs The pointer to the addresses in user land
1305 * addrssize Size of the addrs buffer
1306 *
88a0a948 1307 * Returns >=0 if ok, <0 errno code on error.
3f7a87d2 1308 */
88a0a948 1309SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
3f7a87d2 1310 struct sockaddr __user *addrs,
88a0a948
VY
1311 int addrs_size,
1312 sctp_assoc_t *assoc_id)
3f7a87d2
FF
1313{
1314 int err = 0;
1315 struct sockaddr *kaddrs;
1316
1317 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
0dc47877 1318 __func__, sk, addrs, addrs_size);
3f7a87d2
FF
1319
1320 if (unlikely(addrs_size <= 0))
1321 return -EINVAL;
1322
1323 /* Check the user passed a healthy pointer. */
1324 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1325 return -EFAULT;
1326
1327 /* Alloc space for the address array in kernel memory. */
8b3a7005 1328 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
3f7a87d2
FF
1329 if (unlikely(!kaddrs))
1330 return -ENOMEM;
1331
1332 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1333 err = -EFAULT;
1334 } else {
88a0a948 1335 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
3f7a87d2
FF
1336 }
1337
1338 kfree(kaddrs);
88a0a948 1339
3f7a87d2
FF
1340 return err;
1341}
1342
88a0a948
VY
1343/*
1344 * This is an older interface. It's kept for backward compatibility
1345 * to the option that doesn't provide association id.
1346 */
1347SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1348 struct sockaddr __user *addrs,
1349 int addrs_size)
1350{
1351 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1352}
1353
1354/*
1355 * New interface for the API. The since the API is done with a socket
1356 * option, to make it simple we feed back the association id is as a return
1357 * indication to the call. Error is always negative and association id is
1358 * always positive.
1359 */
1360SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1361 struct sockaddr __user *addrs,
1362 int addrs_size)
1363{
1364 sctp_assoc_t assoc_id = 0;
1365 int err = 0;
1366
1367 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1368
1369 if (err)
1370 return err;
1371 else
1372 return assoc_id;
1373}
1374
c6ba68a2 1375/*
f9c67811
VY
1376 * New (hopefully final) interface for the API.
1377 * We use the sctp_getaddrs_old structure so that use-space library
1378 * can avoid any unnecessary allocations. The only defferent part
1379 * is that we store the actual length of the address buffer into the
1380 * addrs_num structure member. That way we can re-use the existing
1381 * code.
c6ba68a2
VY
1382 */
1383SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1384 char __user *optval,
1385 int __user *optlen)
1386{
f9c67811 1387 struct sctp_getaddrs_old param;
c6ba68a2
VY
1388 sctp_assoc_t assoc_id = 0;
1389 int err = 0;
1390
f9c67811 1391 if (len < sizeof(param))
c6ba68a2
VY
1392 return -EINVAL;
1393
f9c67811
VY
1394 if (copy_from_user(&param, optval, sizeof(param)))
1395 return -EFAULT;
1396
c6ba68a2 1397 err = __sctp_setsockopt_connectx(sk,
f9c67811
VY
1398 (struct sockaddr __user *)param.addrs,
1399 param.addr_num, &assoc_id);
c6ba68a2
VY
1400
1401 if (err == 0 || err == -EINPROGRESS) {
1402 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1403 return -EFAULT;
1404 if (put_user(sizeof(assoc_id), optlen))
1405 return -EFAULT;
1406 }
1407
1408 return err;
1409}
1410
1da177e4
LT
1411/* API 3.1.4 close() - UDP Style Syntax
1412 * Applications use close() to perform graceful shutdown (as described in
1413 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1414 * by a UDP-style socket.
1415 *
1416 * The syntax is
1417 *
1418 * ret = close(int sd);
1419 *
1420 * sd - the socket descriptor of the associations to be closed.
1421 *
1422 * To gracefully shutdown a specific association represented by the
1423 * UDP-style socket, an application should use the sendmsg() call,
1424 * passing no user data, but including the appropriate flag in the
1425 * ancillary data (see Section xxxx).
1426 *
1427 * If sd in the close() call is a branched-off socket representing only
1428 * one association, the shutdown is performed on that association only.
1429 *
1430 * 4.1.6 close() - TCP Style Syntax
1431 *
1432 * Applications use close() to gracefully close down an association.
1433 *
1434 * The syntax is:
1435 *
1436 * int close(int sd);
1437 *
1438 * sd - the socket descriptor of the association to be closed.
1439 *
1440 * After an application calls close() on a socket descriptor, no further
1441 * socket operations will succeed on that descriptor.
1442 *
1443 * API 7.1.4 SO_LINGER
1444 *
1445 * An application using the TCP-style socket can use this option to
1446 * perform the SCTP ABORT primitive. The linger option structure is:
1447 *
1448 * struct linger {
1449 * int l_onoff; // option on/off
1450 * int l_linger; // linger time
1451 * };
1452 *
1453 * To enable the option, set l_onoff to 1. If the l_linger value is set
1454 * to 0, calling close() is the same as the ABORT primitive. If the
1455 * value is set to a negative value, the setsockopt() call will return
1456 * an error. If the value is set to a positive value linger_time, the
1457 * close() can be blocked for at most linger_time ms. If the graceful
1458 * shutdown phase does not finish during this period, close() will
1459 * return but the graceful shutdown phase continues in the system.
1460 */
1461SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1462{
55e26eb9 1463 struct net *net = sock_net(sk);
1da177e4
LT
1464 struct sctp_endpoint *ep;
1465 struct sctp_association *asoc;
1466 struct list_head *pos, *temp;
cd4fcc70 1467 unsigned int data_was_unread;
1da177e4
LT
1468
1469 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1470
1471 sctp_lock_sock(sk);
1472 sk->sk_shutdown = SHUTDOWN_MASK;
bec9640b 1473 sk->sk_state = SCTP_SS_CLOSING;
1da177e4
LT
1474
1475 ep = sctp_sk(sk)->ep;
1476
cd4fcc70
TG
1477 /* Clean up any skbs sitting on the receive queue. */
1478 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1479 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1480
61c9fed4 1481 /* Walk all associations on an endpoint. */
1da177e4
LT
1482 list_for_each_safe(pos, temp, &ep->asocs) {
1483 asoc = list_entry(pos, struct sctp_association, asocs);
1484
1485 if (sctp_style(sk, TCP)) {
1486 /* A closed association can still be in the list if
1487 * it belongs to a TCP-style listening socket that is
1488 * not yet accepted. If so, free it. If not, send an
1489 * ABORT or SHUTDOWN based on the linger options.
1490 */
1491 if (sctp_state(asoc, CLOSED)) {
1492 sctp_unhash_established(asoc);
1493 sctp_association_free(asoc);
b89498a1
VY
1494 continue;
1495 }
1496 }
1da177e4 1497
cd4fcc70
TG
1498 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1499 !skb_queue_empty(&asoc->ulpq.reasm) ||
1500 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
b9ac8672
SS
1501 struct sctp_chunk *chunk;
1502
1503 chunk = sctp_make_abort_user(asoc, NULL, 0);
1504 if (chunk)
55e26eb9 1505 sctp_primitive_ABORT(net, asoc, chunk);
b9ac8672 1506 } else
55e26eb9 1507 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
1508 }
1509
1da177e4
LT
1510 /* On a TCP-style socket, block for at most linger_time if set. */
1511 if (sctp_style(sk, TCP) && timeout)
1512 sctp_wait_for_close(sk, timeout);
1513
1514 /* This will run the backlog queue. */
1515 sctp_release_sock(sk);
1516
1517 /* Supposedly, no process has access to the socket, but
1518 * the net layers still may.
1519 */
1520 sctp_local_bh_disable();
1521 sctp_bh_lock_sock(sk);
1522
1523 /* Hold the sock, since sk_common_release() will put sock_put()
1524 * and we have just a little more cleanup.
1525 */
1526 sock_hold(sk);
1527 sk_common_release(sk);
1528
1529 sctp_bh_unlock_sock(sk);
1530 sctp_local_bh_enable();
1531
1532 sock_put(sk);
1533
1534 SCTP_DBG_OBJCNT_DEC(sock);
1535}
1536
1537/* Handle EPIPE error. */
1538static int sctp_error(struct sock *sk, int flags, int err)
1539{
1540 if (err == -EPIPE)
1541 err = sock_error(sk) ? : -EPIPE;
1542 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1543 send_sig(SIGPIPE, current, 0);
1544 return err;
1545}
1546
1547/* API 3.1.3 sendmsg() - UDP Style Syntax
1548 *
1549 * An application uses sendmsg() and recvmsg() calls to transmit data to
1550 * and receive data from its peer.
1551 *
1552 * ssize_t sendmsg(int socket, const struct msghdr *message,
1553 * int flags);
1554 *
1555 * socket - the socket descriptor of the endpoint.
1556 * message - pointer to the msghdr structure which contains a single
1557 * user message and possibly some ancillary data.
1558 *
1559 * See Section 5 for complete description of the data
1560 * structures.
1561 *
1562 * flags - flags sent or received with the user message, see Section
1563 * 5 for complete description of the flags.
1564 *
1565 * Note: This function could use a rewrite especially when explicit
1566 * connect support comes in.
1567 */
1568/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1569
1570SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1571
1572SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1573 struct msghdr *msg, size_t msg_len)
1574{
55e26eb9 1575 struct net *net = sock_net(sk);
1da177e4
LT
1576 struct sctp_sock *sp;
1577 struct sctp_endpoint *ep;
1578 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1579 struct sctp_transport *transport, *chunk_tp;
1580 struct sctp_chunk *chunk;
dce116ae 1581 union sctp_addr to;
1da177e4 1582 struct sockaddr *msg_name = NULL;
517aa0bc 1583 struct sctp_sndrcvinfo default_sinfo;
1da177e4
LT
1584 struct sctp_sndrcvinfo *sinfo;
1585 struct sctp_initmsg *sinit;
1586 sctp_assoc_t associd = 0;
1587 sctp_cmsgs_t cmsgs = { NULL };
1588 int err;
1589 sctp_scope_t scope;
1590 long timeo;
1591 __u16 sinfo_flags = 0;
1592 struct sctp_datamsg *datamsg;
1da177e4
LT
1593 int msg_flags = msg->msg_flags;
1594
1595 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1596 sk, msg, msg_len);
1597
1598 err = 0;
1599 sp = sctp_sk(sk);
1600 ep = sp->ep;
1601
3f7a87d2 1602 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1da177e4
LT
1603
1604 /* We cannot send a message over a TCP-style listening socket. */
1605 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1606 err = -EPIPE;
1607 goto out_nounlock;
1608 }
1609
1610 /* Parse out the SCTP CMSGs. */
1611 err = sctp_msghdr_parse(msg, &cmsgs);
1612
1613 if (err) {
1614 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1615 goto out_nounlock;
1616 }
1617
1618 /* Fetch the destination address for this packet. This
1619 * address only selects the association--it is not necessarily
1620 * the address we will send to.
1621 * For a peeled-off socket, msg_name is ignored.
1622 */
1623 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1624 int msg_namelen = msg->msg_namelen;
1625
1626 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1627 msg_namelen);
1628 if (err)
1629 return err;
1630
1631 if (msg_namelen > sizeof(to))
1632 msg_namelen = sizeof(to);
1633 memcpy(&to, msg->msg_name, msg_namelen);
1da177e4
LT
1634 msg_name = msg->msg_name;
1635 }
1636
1637 sinfo = cmsgs.info;
1638 sinit = cmsgs.init;
1639
1640 /* Did the user specify SNDRCVINFO? */
1641 if (sinfo) {
1642 sinfo_flags = sinfo->sinfo_flags;
1643 associd = sinfo->sinfo_assoc_id;
1644 }
1645
1646 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1647 msg_len, sinfo_flags);
1648
eaa5c54d
ISJ
1649 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1650 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1da177e4
LT
1651 err = -EINVAL;
1652 goto out_nounlock;
1653 }
1654
eaa5c54d
ISJ
1655 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1656 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1657 * If SCTP_ABORT is set, the message length could be non zero with
1da177e4 1658 * the msg_iov set to the user abort reason.
d808ad9a 1659 */
eaa5c54d
ISJ
1660 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1661 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1da177e4
LT
1662 err = -EINVAL;
1663 goto out_nounlock;
1664 }
1665
eaa5c54d 1666 /* If SCTP_ADDR_OVER is set, there must be an address
1da177e4
LT
1667 * specified in msg_name.
1668 */
eaa5c54d 1669 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1da177e4
LT
1670 err = -EINVAL;
1671 goto out_nounlock;
1672 }
1673
1674 transport = NULL;
1675
1676 SCTP_DEBUG_PRINTK("About to look up association.\n");
1677
1678 sctp_lock_sock(sk);
1679
1680 /* If a msg_name has been specified, assume this is to be used. */
1681 if (msg_name) {
1682 /* Look for a matching association on the endpoint. */
dce116ae 1683 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1da177e4
LT
1684 if (!asoc) {
1685 /* If we could not find a matching association on the
1686 * endpoint, make sure that it is not a TCP-style
1687 * socket that already has an association or there is
1688 * no peeled-off association on another socket.
1689 */
1690 if ((sctp_style(sk, TCP) &&
1691 sctp_sstate(sk, ESTABLISHED)) ||
dce116ae 1692 sctp_endpoint_is_peeled_off(ep, &to)) {
1da177e4
LT
1693 err = -EADDRNOTAVAIL;
1694 goto out_unlock;
1695 }
1696 }
1697 } else {
1698 asoc = sctp_id2assoc(sk, associd);
1699 if (!asoc) {
1700 err = -EPIPE;
1701 goto out_unlock;
1702 }
1703 }
1704
1705 if (asoc) {
1706 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1707
1708 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1709 * socket that has an association in CLOSED state. This can
1710 * happen when an accepted socket has an association that is
1711 * already CLOSED.
1712 */
1713 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1714 err = -EPIPE;
1715 goto out_unlock;
1716 }
1717
eaa5c54d 1718 if (sinfo_flags & SCTP_EOF) {
1da177e4
LT
1719 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1720 asoc);
55e26eb9 1721 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
1722 err = 0;
1723 goto out_unlock;
1724 }
eaa5c54d 1725 if (sinfo_flags & SCTP_ABORT) {
c164a9ba
SS
1726
1727 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1728 if (!chunk) {
1729 err = -ENOMEM;
1730 goto out_unlock;
1731 }
1732
1da177e4 1733 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
55e26eb9 1734 sctp_primitive_ABORT(net, asoc, chunk);
1da177e4
LT
1735 err = 0;
1736 goto out_unlock;
1737 }
1738 }
1739
1740 /* Do we need to create the association? */
1741 if (!asoc) {
1742 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1743
eaa5c54d 1744 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1da177e4
LT
1745 err = -EINVAL;
1746 goto out_unlock;
1747 }
1748
1749 /* Check for invalid stream against the stream counts,
1750 * either the default or the user specified stream counts.
1751 */
1752 if (sinfo) {
1753 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1754 /* Check against the defaults. */
1755 if (sinfo->sinfo_stream >=
1756 sp->initmsg.sinit_num_ostreams) {
1757 err = -EINVAL;
1758 goto out_unlock;
1759 }
1760 } else {
1761 /* Check against the requested. */
1762 if (sinfo->sinfo_stream >=
1763 sinit->sinit_num_ostreams) {
1764 err = -EINVAL;
1765 goto out_unlock;
1766 }
1767 }
1768 }
1769
1770 /*
1771 * API 3.1.2 bind() - UDP Style Syntax
1772 * If a bind() or sctp_bindx() is not called prior to a
1773 * sendmsg() call that initiates a new association, the
1774 * system picks an ephemeral port and will choose an address
1775 * set equivalent to binding with a wildcard address.
1776 */
1777 if (!ep->base.bind_addr.port) {
1778 if (sctp_autobind(sk)) {
1779 err = -EAGAIN;
1780 goto out_unlock;
1781 }
64a0c1c8
ISJ
1782 } else {
1783 /*
1784 * If an unprivileged user inherits a one-to-many
1785 * style socket with open associations on a privileged
1786 * port, it MAY be permitted to accept new associations,
1787 * but it SHOULD NOT be permitted to open new
1788 * associations.
1789 */
1790 if (ep->base.bind_addr.port < PROT_SOCK &&
1791 !capable(CAP_NET_BIND_SERVICE)) {
1792 err = -EACCES;
1793 goto out_unlock;
1794 }
1da177e4
LT
1795 }
1796
1797 scope = sctp_scope(&to);
1798 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1799 if (!new_asoc) {
1800 err = -ENOMEM;
1801 goto out_unlock;
1802 }
1803 asoc = new_asoc;
409b95af
VY
1804 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1805 if (err < 0) {
1806 err = -ENOMEM;
1807 goto out_free;
1808 }
1da177e4
LT
1809
1810 /* If the SCTP_INIT ancillary data is specified, set all
1811 * the association init values accordingly.
1812 */
1813 if (sinit) {
1814 if (sinit->sinit_num_ostreams) {
1815 asoc->c.sinit_num_ostreams =
1816 sinit->sinit_num_ostreams;
1817 }
1818 if (sinit->sinit_max_instreams) {
1819 asoc->c.sinit_max_instreams =
1820 sinit->sinit_max_instreams;
1821 }
1822 if (sinit->sinit_max_attempts) {
1823 asoc->max_init_attempts
1824 = sinit->sinit_max_attempts;
1825 }
1826 if (sinit->sinit_max_init_timeo) {
d808ad9a 1827 asoc->max_init_timeo =
1da177e4
LT
1828 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1829 }
1830 }
1831
1832 /* Prime the peer's transport structures. */
dce116ae 1833 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1da177e4
LT
1834 if (!transport) {
1835 err = -ENOMEM;
1836 goto out_free;
1837 }
1da177e4
LT
1838 }
1839
1840 /* ASSERT: we have a valid association at this point. */
1841 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1842
1843 if (!sinfo) {
1844 /* If the user didn't specify SNDRCVINFO, make up one with
1845 * some defaults.
1846 */
517aa0bc 1847 memset(&default_sinfo, 0, sizeof(default_sinfo));
1da177e4
LT
1848 default_sinfo.sinfo_stream = asoc->default_stream;
1849 default_sinfo.sinfo_flags = asoc->default_flags;
1850 default_sinfo.sinfo_ppid = asoc->default_ppid;
1851 default_sinfo.sinfo_context = asoc->default_context;
1852 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1853 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1854 sinfo = &default_sinfo;
1855 }
1856
1857 /* API 7.1.7, the sndbuf size per association bounds the
1858 * maximum size of data that can be sent in a single send call.
1859 */
1860 if (msg_len > sk->sk_sndbuf) {
1861 err = -EMSGSIZE;
1862 goto out_free;
1863 }
1864
8a479491 1865 if (asoc->pmtu_pending)
02f3d4ce 1866 sctp_assoc_pending_pmtu(sk, asoc);
8a479491 1867
1da177e4
LT
1868 /* If fragmentation is disabled and the message length exceeds the
1869 * association fragmentation point, return EMSGSIZE. The I-D
1870 * does not specify what this error is, but this looks like
1871 * a great fit.
1872 */
1873 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1874 err = -EMSGSIZE;
1875 goto out_free;
1876 }
1877
afd7614c
JP
1878 /* Check for invalid stream. */
1879 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1880 err = -EINVAL;
1881 goto out_free;
1da177e4
LT
1882 }
1883
1884 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1885 if (!sctp_wspace(asoc)) {
1886 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1887 if (err)
1888 goto out_free;
1889 }
1890
1891 /* If an address is passed with the sendto/sendmsg call, it is used
1892 * to override the primary destination address in the TCP model, or
eaa5c54d 1893 * when SCTP_ADDR_OVER flag is set in the UDP model.
1da177e4
LT
1894 */
1895 if ((sctp_style(sk, TCP) && msg_name) ||
eaa5c54d 1896 (sinfo_flags & SCTP_ADDR_OVER)) {
dce116ae 1897 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1da177e4
LT
1898 if (!chunk_tp) {
1899 err = -EINVAL;
1900 goto out_free;
1901 }
1902 } else
1903 chunk_tp = NULL;
1904
1905 /* Auto-connect, if we aren't connected already. */
1906 if (sctp_state(asoc, CLOSED)) {
55e26eb9 1907 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1da177e4
LT
1908 if (err < 0)
1909 goto out_free;
1910 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1911 }
1912
1913 /* Break the message into multiple chunks of maximum size. */
1914 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1915 if (!datamsg) {
1916 err = -ENOMEM;
1917 goto out_free;
1918 }
1919
1920 /* Now send the (possibly) fragmented message. */
9dbc15f0 1921 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
80445cfb 1922 sctp_chunk_hold(chunk);
1da177e4
LT
1923
1924 /* Do accounting for the write space. */
1925 sctp_set_owner_w(chunk);
1926
1927 chunk->transport = chunk_tp;
1da177e4
LT
1928 }
1929
9c5c62be
VY
1930 /* Send it to the lower layers. Note: all chunks
1931 * must either fail or succeed. The lower layer
1932 * works that way today. Keep it that way or this
1933 * breaks.
1934 */
55e26eb9 1935 err = sctp_primitive_SEND(net, asoc, datamsg);
9c5c62be
VY
1936 /* Did the lower layer accept the chunk? */
1937 if (err)
1938 sctp_datamsg_free(datamsg);
1939 else
1940 sctp_datamsg_put(datamsg);
1941
1942 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1943
1da177e4
LT
1944 if (err)
1945 goto out_free;
1946 else
1947 err = msg_len;
1948
1949 /* If we are already past ASSOCIATE, the lower
1950 * layers are responsible for association cleanup.
1951 */
1952 goto out_unlock;
1953
1954out_free:
2eebc1e1
NH
1955 if (new_asoc) {
1956 sctp_unhash_established(asoc);
1da177e4 1957 sctp_association_free(asoc);
2eebc1e1 1958 }
1da177e4
LT
1959out_unlock:
1960 sctp_release_sock(sk);
1961
1962out_nounlock:
1963 return sctp_error(sk, msg_flags, err);
1964
1965#if 0
1966do_sock_err:
1967 if (msg_len)
1968 err = msg_len;
1969 else
1970 err = sock_error(sk);
1971 goto out;
1972
1973do_interrupted:
1974 if (msg_len)
1975 err = msg_len;
1976 goto out;
1977#endif /* 0 */
1978}
1979
1980/* This is an extended version of skb_pull() that removes the data from the
1981 * start of a skb even when data is spread across the list of skb's in the
1982 * frag_list. len specifies the total amount of data that needs to be removed.
1983 * when 'len' bytes could be removed from the skb, it returns 0.
1984 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1985 * could not be removed.
1986 */
1987static int sctp_skb_pull(struct sk_buff *skb, int len)
1988{
1989 struct sk_buff *list;
1990 int skb_len = skb_headlen(skb);
1991 int rlen;
1992
1993 if (len <= skb_len) {
1994 __skb_pull(skb, len);
1995 return 0;
1996 }
1997 len -= skb_len;
1998 __skb_pull(skb, skb_len);
1999
1b003be3 2000 skb_walk_frags(skb, list) {
1da177e4
LT
2001 rlen = sctp_skb_pull(list, len);
2002 skb->len -= (len-rlen);
2003 skb->data_len -= (len-rlen);
2004
2005 if (!rlen)
2006 return 0;
2007
2008 len = rlen;
2009 }
2010
2011 return len;
2012}
2013
2014/* API 3.1.3 recvmsg() - UDP Style Syntax
2015 *
2016 * ssize_t recvmsg(int socket, struct msghdr *message,
2017 * int flags);
2018 *
2019 * socket - the socket descriptor of the endpoint.
2020 * message - pointer to the msghdr structure which contains a single
2021 * user message and possibly some ancillary data.
2022 *
2023 * See Section 5 for complete description of the data
2024 * structures.
2025 *
2026 * flags - flags sent or received with the user message, see Section
2027 * 5 for complete description of the flags.
2028 */
2029static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2030
2031SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2032 struct msghdr *msg, size_t len, int noblock,
2033 int flags, int *addr_len)
2034{
2035 struct sctp_ulpevent *event = NULL;
2036 struct sctp_sock *sp = sctp_sk(sk);
2037 struct sk_buff *skb;
2038 int copied;
2039 int err = 0;
2040 int skb_len;
2041
2042 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2043 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2044 "len", len, "knoblauch", noblock,
2045 "flags", flags, "addr_len", addr_len);
2046
2047 sctp_lock_sock(sk);
2048
2049 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2050 err = -ENOTCONN;
2051 goto out;
2052 }
2053
2054 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2055 if (!skb)
2056 goto out;
2057
2058 /* Get the total length of the skb including any skb's in the
2059 * frag_list.
2060 */
2061 skb_len = skb->len;
2062
2063 copied = skb_len;
2064 if (copied > len)
2065 copied = len;
2066
2067 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2068
2069 event = sctp_skb2event(skb);
2070
2071 if (err)
2072 goto out_free;
2073
3b885787 2074 sock_recv_ts_and_drops(msg, sk, skb);
1da177e4
LT
2075 if (sctp_ulpevent_is_notification(event)) {
2076 msg->msg_flags |= MSG_NOTIFICATION;
2077 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2078 } else {
2079 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2080 }
2081
2082 /* Check if we allow SCTP_SNDRCVINFO. */
2083 if (sp->subscribe.sctp_data_io_event)
2084 sctp_ulpevent_read_sndrcvinfo(event, msg);
2085#if 0
2086 /* FIXME: we should be calling IP/IPv6 layers. */
2087 if (sk->sk_protinfo.af_inet.cmsg_flags)
2088 ip_cmsg_recv(msg, skb);
2089#endif
2090
2091 err = copied;
2092
2093 /* If skb's length exceeds the user's buffer, update the skb and
2094 * push it back to the receive_queue so that the next call to
2095 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2096 */
2097 if (skb_len > copied) {
2098 msg->msg_flags &= ~MSG_EOR;
2099 if (flags & MSG_PEEK)
2100 goto out_free;
2101 sctp_skb_pull(skb, copied);
2102 skb_queue_head(&sk->sk_receive_queue, skb);
2103
2104 /* When only partial message is copied to the user, increase
2105 * rwnd by that amount. If all the data in the skb is read,
2106 * rwnd is updated when the event is freed.
2107 */
0eca8fee
VY
2108 if (!sctp_ulpevent_is_notification(event))
2109 sctp_assoc_rwnd_increase(event->asoc, copied);
1da177e4
LT
2110 goto out;
2111 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2112 (event->msg_flags & MSG_EOR))
2113 msg->msg_flags |= MSG_EOR;
2114 else
2115 msg->msg_flags &= ~MSG_EOR;
2116
2117out_free:
2118 if (flags & MSG_PEEK) {
2119 /* Release the skb reference acquired after peeking the skb in
2120 * sctp_skb_recv_datagram().
2121 */
2122 kfree_skb(skb);
2123 } else {
2124 /* Free the event which includes releasing the reference to
2125 * the owner of the skb, freeing the skb and updating the
2126 * rwnd.
2127 */
2128 sctp_ulpevent_free(event);
2129 }
2130out:
2131 sctp_release_sock(sk);
2132 return err;
2133}
2134
2135/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2136 *
2137 * This option is a on/off flag. If enabled no SCTP message
2138 * fragmentation will be performed. Instead if a message being sent
2139 * exceeds the current PMTU size, the message will NOT be sent and
2140 * instead a error will be indicated to the user.
2141 */
2142static int sctp_setsockopt_disable_fragments(struct sock *sk,
b7058842
DM
2143 char __user *optval,
2144 unsigned int optlen)
1da177e4
LT
2145{
2146 int val;
2147
2148 if (optlen < sizeof(int))
2149 return -EINVAL;
2150
2151 if (get_user(val, (int __user *)optval))
2152 return -EFAULT;
2153
2154 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2155
2156 return 0;
2157}
2158
2159static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
b7058842 2160 unsigned int optlen)
1da177e4 2161{
94912301
WY
2162 struct sctp_association *asoc;
2163 struct sctp_ulpevent *event;
2164
7e8616d8 2165 if (optlen > sizeof(struct sctp_event_subscribe))
1da177e4
LT
2166 return -EINVAL;
2167 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2168 return -EFAULT;
94912301
WY
2169
2170 /*
2171 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2172 * if there is no data to be sent or retransmit, the stack will
2173 * immediately send up this notification.
2174 */
2175 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2176 &sctp_sk(sk)->subscribe)) {
2177 asoc = sctp_id2assoc(sk, 0);
2178
2179 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2180 event = sctp_ulpevent_make_sender_dry_event(asoc,
2181 GFP_ATOMIC);
2182 if (!event)
2183 return -ENOMEM;
2184
2185 sctp_ulpq_tail_event(&asoc->ulpq, event);
2186 }
2187 }
2188
1da177e4
LT
2189 return 0;
2190}
2191
2192/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2193 *
2194 * This socket option is applicable to the UDP-style socket only. When
2195 * set it will cause associations that are idle for more than the
2196 * specified number of seconds to automatically close. An association
2197 * being idle is defined an association that has NOT sent or received
2198 * user data. The special value of '0' indicates that no automatic
2199 * close of any associations should be performed. The option expects an
2200 * integer defining the number of seconds of idle time before an
2201 * association is closed.
2202 */
2203static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
b7058842 2204 unsigned int optlen)
1da177e4
LT
2205{
2206 struct sctp_sock *sp = sctp_sk(sk);
2207
2208 /* Applicable to UDP-style socket only */
2209 if (sctp_style(sk, TCP))
2210 return -EOPNOTSUPP;
2211 if (optlen != sizeof(int))
2212 return -EINVAL;
2213 if (copy_from_user(&sp->autoclose, optval, optlen))
2214 return -EFAULT;
2215
1da177e4
LT
2216 return 0;
2217}
2218
2219/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2220 *
2221 * Applications can enable or disable heartbeats for any peer address of
2222 * an association, modify an address's heartbeat interval, force a
2223 * heartbeat to be sent immediately, and adjust the address's maximum
2224 * number of retransmissions sent before an address is considered
2225 * unreachable. The following structure is used to access and modify an
2226 * address's parameters:
2227 *
2228 * struct sctp_paddrparams {
52ccb8e9
FF
2229 * sctp_assoc_t spp_assoc_id;
2230 * struct sockaddr_storage spp_address;
2231 * uint32_t spp_hbinterval;
2232 * uint16_t spp_pathmaxrxt;
2233 * uint32_t spp_pathmtu;
2234 * uint32_t spp_sackdelay;
2235 * uint32_t spp_flags;
2236 * };
2237 *
2238 * spp_assoc_id - (one-to-many style socket) This is filled in the
2239 * application, and identifies the association for
2240 * this query.
1da177e4
LT
2241 * spp_address - This specifies which address is of interest.
2242 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
2243 * in milliseconds. If a value of zero
2244 * is present in this field then no changes are to
2245 * be made to this parameter.
1da177e4
LT
2246 * spp_pathmaxrxt - This contains the maximum number of
2247 * retransmissions before this address shall be
52ccb8e9
FF
2248 * considered unreachable. If a value of zero
2249 * is present in this field then no changes are to
2250 * be made to this parameter.
2251 * spp_pathmtu - When Path MTU discovery is disabled the value
2252 * specified here will be the "fixed" path mtu.
2253 * Note that if the spp_address field is empty
2254 * then all associations on this address will
2255 * have this fixed path mtu set upon them.
2256 *
2257 * spp_sackdelay - When delayed sack is enabled, this value specifies
2258 * the number of milliseconds that sacks will be delayed
2259 * for. This value will apply to all addresses of an
2260 * association if the spp_address field is empty. Note
2261 * also, that if delayed sack is enabled and this
2262 * value is set to 0, no change is made to the last
2263 * recorded delayed sack timer value.
2264 *
2265 * spp_flags - These flags are used to control various features
2266 * on an association. The flag field may contain
2267 * zero or more of the following options.
2268 *
2269 * SPP_HB_ENABLE - Enable heartbeats on the
2270 * specified address. Note that if the address
2271 * field is empty all addresses for the association
2272 * have heartbeats enabled upon them.
2273 *
2274 * SPP_HB_DISABLE - Disable heartbeats on the
2275 * speicifed address. Note that if the address
2276 * field is empty all addresses for the association
2277 * will have their heartbeats disabled. Note also
2278 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2279 * mutually exclusive, only one of these two should
2280 * be specified. Enabling both fields will have
2281 * undetermined results.
2282 *
2283 * SPP_HB_DEMAND - Request a user initiated heartbeat
2284 * to be made immediately.
2285 *
bdf3092a
VY
2286 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2287 * heartbeat delayis to be set to the value of 0
2288 * milliseconds.
2289 *
52ccb8e9
FF
2290 * SPP_PMTUD_ENABLE - This field will enable PMTU
2291 * discovery upon the specified address. Note that
2292 * if the address feild is empty then all addresses
2293 * on the association are effected.
2294 *
2295 * SPP_PMTUD_DISABLE - This field will disable PMTU
2296 * discovery upon the specified address. Note that
2297 * if the address feild is empty then all addresses
2298 * on the association are effected. Not also that
2299 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2300 * exclusive. Enabling both will have undetermined
2301 * results.
2302 *
2303 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2304 * on delayed sack. The time specified in spp_sackdelay
2305 * is used to specify the sack delay for this address. Note
2306 * that if spp_address is empty then all addresses will
2307 * enable delayed sack and take on the sack delay
2308 * value specified in spp_sackdelay.
2309 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2310 * off delayed sack. If the spp_address field is blank then
2311 * delayed sack is disabled for the entire association. Note
2312 * also that this field is mutually exclusive to
2313 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2314 * results.
1da177e4 2315 */
16164366
AB
2316static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2317 struct sctp_transport *trans,
2318 struct sctp_association *asoc,
2319 struct sctp_sock *sp,
2320 int hb_change,
2321 int pmtud_change,
2322 int sackdelay_change)
52ccb8e9
FF
2323{
2324 int error;
2325
2326 if (params->spp_flags & SPP_HB_DEMAND && trans) {
55e26eb9
EB
2327 struct net *net = sock_net(trans->asoc->base.sk);
2328
2329 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
52ccb8e9
FF
2330 if (error)
2331 return error;
2332 }
2333
bdf3092a
VY
2334 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2335 * this field is ignored. Note also that a value of zero indicates
2336 * the current setting should be left unchanged.
2337 */
2338 if (params->spp_flags & SPP_HB_ENABLE) {
2339
2340 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2341 * set. This lets us use 0 value when this flag
2342 * is set.
2343 */
2344 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2345 params->spp_hbinterval = 0;
2346
2347 if (params->spp_hbinterval ||
2348 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2349 if (trans) {
2350 trans->hbinterval =
2351 msecs_to_jiffies(params->spp_hbinterval);
2352 } else if (asoc) {
2353 asoc->hbinterval =
2354 msecs_to_jiffies(params->spp_hbinterval);
2355 } else {
2356 sp->hbinterval = params->spp_hbinterval;
2357 }
52ccb8e9
FF
2358 }
2359 }
2360
2361 if (hb_change) {
2362 if (trans) {
2363 trans->param_flags =
2364 (trans->param_flags & ~SPP_HB) | hb_change;
2365 } else if (asoc) {
2366 asoc->param_flags =
2367 (asoc->param_flags & ~SPP_HB) | hb_change;
2368 } else {
2369 sp->param_flags =
2370 (sp->param_flags & ~SPP_HB) | hb_change;
2371 }
2372 }
2373
bdf3092a
VY
2374 /* When Path MTU discovery is disabled the value specified here will
2375 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2376 * include the flag SPP_PMTUD_DISABLE for this field to have any
2377 * effect).
2378 */
2379 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
52ccb8e9
FF
2380 if (trans) {
2381 trans->pathmtu = params->spp_pathmtu;
02f3d4ce 2382 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
52ccb8e9
FF
2383 } else if (asoc) {
2384 asoc->pathmtu = params->spp_pathmtu;
f68b2e05 2385 sctp_frag_point(asoc, params->spp_pathmtu);
52ccb8e9
FF
2386 } else {
2387 sp->pathmtu = params->spp_pathmtu;
2388 }
2389 }
2390
2391 if (pmtud_change) {
2392 if (trans) {
2393 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2394 (params->spp_flags & SPP_PMTUD_ENABLE);
2395 trans->param_flags =
2396 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2397 if (update) {
9914ae3c 2398 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
02f3d4ce 2399 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
52ccb8e9
FF
2400 }
2401 } else if (asoc) {
2402 asoc->param_flags =
2403 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2404 } else {
2405 sp->param_flags =
2406 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2407 }
2408 }
2409
bdf3092a
VY
2410 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2411 * value of this field is ignored. Note also that a value of zero
2412 * indicates the current setting should be left unchanged.
2413 */
2414 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
52ccb8e9
FF
2415 if (trans) {
2416 trans->sackdelay =
2417 msecs_to_jiffies(params->spp_sackdelay);
2418 } else if (asoc) {
2419 asoc->sackdelay =
2420 msecs_to_jiffies(params->spp_sackdelay);
2421 } else {
2422 sp->sackdelay = params->spp_sackdelay;
2423 }
2424 }
2425
2426 if (sackdelay_change) {
2427 if (trans) {
2428 trans->param_flags =
2429 (trans->param_flags & ~SPP_SACKDELAY) |
2430 sackdelay_change;
2431 } else if (asoc) {
2432 asoc->param_flags =
2433 (asoc->param_flags & ~SPP_SACKDELAY) |
2434 sackdelay_change;
2435 } else {
2436 sp->param_flags =
2437 (sp->param_flags & ~SPP_SACKDELAY) |
2438 sackdelay_change;
2439 }
2440 }
2441
37051f73
APO
2442 /* Note that a value of zero indicates the current setting should be
2443 left unchanged.
bdf3092a 2444 */
37051f73 2445 if (params->spp_pathmaxrxt) {
52ccb8e9
FF
2446 if (trans) {
2447 trans->pathmaxrxt = params->spp_pathmaxrxt;
2448 } else if (asoc) {
2449 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2450 } else {
2451 sp->pathmaxrxt = params->spp_pathmaxrxt;
2452 }
2453 }
2454
2455 return 0;
2456}
2457
1da177e4 2458static int sctp_setsockopt_peer_addr_params(struct sock *sk,
b7058842
DM
2459 char __user *optval,
2460 unsigned int optlen)
1da177e4 2461{
52ccb8e9
FF
2462 struct sctp_paddrparams params;
2463 struct sctp_transport *trans = NULL;
2464 struct sctp_association *asoc = NULL;
2465 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 2466 int error;
52ccb8e9 2467 int hb_change, pmtud_change, sackdelay_change;
1da177e4
LT
2468
2469 if (optlen != sizeof(struct sctp_paddrparams))
52ccb8e9
FF
2470 return - EINVAL;
2471
1da177e4
LT
2472 if (copy_from_user(&params, optval, optlen))
2473 return -EFAULT;
2474
52ccb8e9
FF
2475 /* Validate flags and value parameters. */
2476 hb_change = params.spp_flags & SPP_HB;
2477 pmtud_change = params.spp_flags & SPP_PMTUD;
2478 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2479
2480 if (hb_change == SPP_HB ||
2481 pmtud_change == SPP_PMTUD ||
2482 sackdelay_change == SPP_SACKDELAY ||
2483 params.spp_sackdelay > 500 ||
f64f9e71
JP
2484 (params.spp_pathmtu &&
2485 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
52ccb8e9 2486 return -EINVAL;
1da177e4 2487
52ccb8e9
FF
2488 /* If an address other than INADDR_ANY is specified, and
2489 * no transport is found, then the request is invalid.
2490 */
52cae8f0 2491 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
2492 trans = sctp_addr_id2transport(sk, &params.spp_address,
2493 params.spp_assoc_id);
2494 if (!trans)
1da177e4 2495 return -EINVAL;
1da177e4
LT
2496 }
2497
52ccb8e9
FF
2498 /* Get association, if assoc_id != 0 and the socket is a one
2499 * to many style socket, and an association was not found, then
2500 * the id was invalid.
2501 */
2502 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2503 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
1da177e4
LT
2504 return -EINVAL;
2505
52ccb8e9
FF
2506 /* Heartbeat demand can only be sent on a transport or
2507 * association, but not a socket.
1da177e4 2508 */
52ccb8e9
FF
2509 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2510 return -EINVAL;
2511
2512 /* Process parameters. */
2513 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2514 hb_change, pmtud_change,
2515 sackdelay_change);
1da177e4 2516
52ccb8e9
FF
2517 if (error)
2518 return error;
2519
2520 /* If changes are for association, also apply parameters to each
2521 * transport.
1da177e4 2522 */
52ccb8e9 2523 if (!trans && asoc) {
9dbc15f0
RD
2524 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2525 transports) {
52ccb8e9
FF
2526 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2527 hb_change, pmtud_change,
2528 sackdelay_change);
2529 }
2530 }
1da177e4
LT
2531
2532 return 0;
2533}
2534
d364d927
WY
2535/*
2536 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2537 *
2538 * This option will effect the way delayed acks are performed. This
2539 * option allows you to get or set the delayed ack time, in
2540 * milliseconds. It also allows changing the delayed ack frequency.
2541 * Changing the frequency to 1 disables the delayed sack algorithm. If
2542 * the assoc_id is 0, then this sets or gets the endpoints default
2543 * values. If the assoc_id field is non-zero, then the set or get
2544 * effects the specified association for the one to many model (the
2545 * assoc_id field is ignored by the one to one model). Note that if
2546 * sack_delay or sack_freq are 0 when setting this option, then the
2547 * current values will remain unchanged.
2548 *
2549 * struct sctp_sack_info {
2550 * sctp_assoc_t sack_assoc_id;
2551 * uint32_t sack_delay;
2552 * uint32_t sack_freq;
2553 * };
2554 *
2555 * sack_assoc_id - This parameter, indicates which association the user
2556 * is performing an action upon. Note that if this field's value is
2557 * zero then the endpoints default value is changed (effecting future
2558 * associations only).
2559 *
2560 * sack_delay - This parameter contains the number of milliseconds that
2561 * the user is requesting the delayed ACK timer be set to. Note that
2562 * this value is defined in the standard to be between 200 and 500
2563 * milliseconds.
2564 *
2565 * sack_freq - This parameter contains the number of packets that must
2566 * be received before a sack is sent without waiting for the delay
2567 * timer to expire. The default value for this is 2, setting this
2568 * value to 1 will disable the delayed sack algorithm.
7708610b
FF
2569 */
2570
d364d927 2571static int sctp_setsockopt_delayed_ack(struct sock *sk,
b7058842 2572 char __user *optval, unsigned int optlen)
7708610b 2573{
d364d927 2574 struct sctp_sack_info params;
7708610b
FF
2575 struct sctp_transport *trans = NULL;
2576 struct sctp_association *asoc = NULL;
2577 struct sctp_sock *sp = sctp_sk(sk);
2578
d364d927
WY
2579 if (optlen == sizeof(struct sctp_sack_info)) {
2580 if (copy_from_user(&params, optval, optlen))
2581 return -EFAULT;
7708610b 2582
d364d927
WY
2583 if (params.sack_delay == 0 && params.sack_freq == 0)
2584 return 0;
2585 } else if (optlen == sizeof(struct sctp_assoc_value)) {
145ce502
JP
2586 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2587 pr_warn("Use struct sctp_sack_info instead\n");
d364d927
WY
2588 if (copy_from_user(&params, optval, optlen))
2589 return -EFAULT;
2590
2591 if (params.sack_delay == 0)
2592 params.sack_freq = 1;
2593 else
2594 params.sack_freq = 0;
2595 } else
2596 return - EINVAL;
7708610b
FF
2597
2598 /* Validate value parameter. */
d364d927 2599 if (params.sack_delay > 500)
7708610b
FF
2600 return -EINVAL;
2601
d364d927 2602 /* Get association, if sack_assoc_id != 0 and the socket is a one
7708610b
FF
2603 * to many style socket, and an association was not found, then
2604 * the id was invalid.
d808ad9a 2605 */
d364d927
WY
2606 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2607 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
7708610b
FF
2608 return -EINVAL;
2609
d364d927 2610 if (params.sack_delay) {
7708610b
FF
2611 if (asoc) {
2612 asoc->sackdelay =
d364d927 2613 msecs_to_jiffies(params.sack_delay);
d808ad9a 2614 asoc->param_flags =
7708610b
FF
2615 (asoc->param_flags & ~SPP_SACKDELAY) |
2616 SPP_SACKDELAY_ENABLE;
2617 } else {
d364d927 2618 sp->sackdelay = params.sack_delay;
d808ad9a 2619 sp->param_flags =
7708610b
FF
2620 (sp->param_flags & ~SPP_SACKDELAY) |
2621 SPP_SACKDELAY_ENABLE;
2622 }
d364d927
WY
2623 }
2624
2625 if (params.sack_freq == 1) {
7708610b 2626 if (asoc) {
d808ad9a 2627 asoc->param_flags =
7708610b
FF
2628 (asoc->param_flags & ~SPP_SACKDELAY) |
2629 SPP_SACKDELAY_DISABLE;
2630 } else {
d808ad9a 2631 sp->param_flags =
7708610b
FF
2632 (sp->param_flags & ~SPP_SACKDELAY) |
2633 SPP_SACKDELAY_DISABLE;
2634 }
d364d927
WY
2635 } else if (params.sack_freq > 1) {
2636 if (asoc) {
2637 asoc->sackfreq = params.sack_freq;
2638 asoc->param_flags =
2639 (asoc->param_flags & ~SPP_SACKDELAY) |
2640 SPP_SACKDELAY_ENABLE;
2641 } else {
2642 sp->sackfreq = params.sack_freq;
2643 sp->param_flags =
2644 (sp->param_flags & ~SPP_SACKDELAY) |
2645 SPP_SACKDELAY_ENABLE;
2646 }
7708610b
FF
2647 }
2648
2649 /* If change is for association, also apply to each transport. */
2650 if (asoc) {
9dbc15f0
RD
2651 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2652 transports) {
d364d927 2653 if (params.sack_delay) {
7708610b 2654 trans->sackdelay =
d364d927 2655 msecs_to_jiffies(params.sack_delay);
d808ad9a 2656 trans->param_flags =
7708610b
FF
2657 (trans->param_flags & ~SPP_SACKDELAY) |
2658 SPP_SACKDELAY_ENABLE;
d364d927 2659 }
7bfe8bdb 2660 if (params.sack_freq == 1) {
d808ad9a 2661 trans->param_flags =
7708610b
FF
2662 (trans->param_flags & ~SPP_SACKDELAY) |
2663 SPP_SACKDELAY_DISABLE;
d364d927
WY
2664 } else if (params.sack_freq > 1) {
2665 trans->sackfreq = params.sack_freq;
2666 trans->param_flags =
2667 (trans->param_flags & ~SPP_SACKDELAY) |
2668 SPP_SACKDELAY_ENABLE;
7708610b
FF
2669 }
2670 }
2671 }
d808ad9a 2672
7708610b
FF
2673 return 0;
2674}
2675
1da177e4
LT
2676/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2677 *
2678 * Applications can specify protocol parameters for the default association
2679 * initialization. The option name argument to setsockopt() and getsockopt()
2680 * is SCTP_INITMSG.
2681 *
2682 * Setting initialization parameters is effective only on an unconnected
2683 * socket (for UDP-style sockets only future associations are effected
2684 * by the change). With TCP-style sockets, this option is inherited by
2685 * sockets derived from a listener socket.
2686 */
b7058842 2687static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2688{
2689 struct sctp_initmsg sinit;
2690 struct sctp_sock *sp = sctp_sk(sk);
2691
2692 if (optlen != sizeof(struct sctp_initmsg))
2693 return -EINVAL;
2694 if (copy_from_user(&sinit, optval, optlen))
2695 return -EFAULT;
2696
2697 if (sinit.sinit_num_ostreams)
d808ad9a 2698 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1da177e4 2699 if (sinit.sinit_max_instreams)
d808ad9a 2700 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1da177e4 2701 if (sinit.sinit_max_attempts)
d808ad9a 2702 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1da177e4 2703 if (sinit.sinit_max_init_timeo)
d808ad9a 2704 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1da177e4
LT
2705
2706 return 0;
2707}
2708
2709/*
2710 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2711 *
2712 * Applications that wish to use the sendto() system call may wish to
2713 * specify a default set of parameters that would normally be supplied
2714 * through the inclusion of ancillary data. This socket option allows
2715 * such an application to set the default sctp_sndrcvinfo structure.
2716 * The application that wishes to use this socket option simply passes
2717 * in to this call the sctp_sndrcvinfo structure defined in Section
2718 * 5.2.2) The input parameters accepted by this call include
2719 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2720 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2721 * to this call if the caller is using the UDP model.
2722 */
2723static int sctp_setsockopt_default_send_param(struct sock *sk,
b7058842
DM
2724 char __user *optval,
2725 unsigned int optlen)
1da177e4
LT
2726{
2727 struct sctp_sndrcvinfo info;
2728 struct sctp_association *asoc;
2729 struct sctp_sock *sp = sctp_sk(sk);
2730
2731 if (optlen != sizeof(struct sctp_sndrcvinfo))
2732 return -EINVAL;
2733 if (copy_from_user(&info, optval, optlen))
2734 return -EFAULT;
2735
2736 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2737 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2738 return -EINVAL;
2739
2740 if (asoc) {
2741 asoc->default_stream = info.sinfo_stream;
2742 asoc->default_flags = info.sinfo_flags;
2743 asoc->default_ppid = info.sinfo_ppid;
2744 asoc->default_context = info.sinfo_context;
2745 asoc->default_timetolive = info.sinfo_timetolive;
2746 } else {
2747 sp->default_stream = info.sinfo_stream;
2748 sp->default_flags = info.sinfo_flags;
2749 sp->default_ppid = info.sinfo_ppid;
2750 sp->default_context = info.sinfo_context;
2751 sp->default_timetolive = info.sinfo_timetolive;
2752 }
2753
2754 return 0;
2755}
2756
2757/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2758 *
2759 * Requests that the local SCTP stack use the enclosed peer address as
2760 * the association primary. The enclosed address must be one of the
2761 * association peer's addresses.
2762 */
2763static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
b7058842 2764 unsigned int optlen)
1da177e4
LT
2765{
2766 struct sctp_prim prim;
2767 struct sctp_transport *trans;
2768
2769 if (optlen != sizeof(struct sctp_prim))
2770 return -EINVAL;
2771
2772 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2773 return -EFAULT;
2774
2775 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2776 if (!trans)
2777 return -EINVAL;
2778
2779 sctp_assoc_set_primary(trans->asoc, trans);
2780
2781 return 0;
2782}
2783
2784/*
2785 * 7.1.5 SCTP_NODELAY
2786 *
2787 * Turn on/off any Nagle-like algorithm. This means that packets are
2788 * generally sent as soon as possible and no unnecessary delays are
2789 * introduced, at the cost of more packets in the network. Expects an
2790 * integer boolean flag.
2791 */
2792static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
b7058842 2793 unsigned int optlen)
1da177e4
LT
2794{
2795 int val;
2796
2797 if (optlen < sizeof(int))
2798 return -EINVAL;
2799 if (get_user(val, (int __user *)optval))
2800 return -EFAULT;
2801
2802 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2803 return 0;
2804}
2805
2806/*
2807 *
2808 * 7.1.1 SCTP_RTOINFO
2809 *
2810 * The protocol parameters used to initialize and bound retransmission
2811 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2812 * and modify these parameters.
2813 * All parameters are time values, in milliseconds. A value of 0, when
2814 * modifying the parameters, indicates that the current value should not
2815 * be changed.
2816 *
2817 */
b7058842
DM
2818static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2819{
1da177e4
LT
2820 struct sctp_rtoinfo rtoinfo;
2821 struct sctp_association *asoc;
2822
2823 if (optlen != sizeof (struct sctp_rtoinfo))
2824 return -EINVAL;
2825
2826 if (copy_from_user(&rtoinfo, optval, optlen))
2827 return -EFAULT;
2828
2829 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2830
2831 /* Set the values to the specific association */
2832 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2833 return -EINVAL;
2834
2835 if (asoc) {
2836 if (rtoinfo.srto_initial != 0)
d808ad9a 2837 asoc->rto_initial =
1da177e4
LT
2838 msecs_to_jiffies(rtoinfo.srto_initial);
2839 if (rtoinfo.srto_max != 0)
2840 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2841 if (rtoinfo.srto_min != 0)
2842 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2843 } else {
2844 /* If there is no association or the association-id = 0
2845 * set the values to the endpoint.
2846 */
2847 struct sctp_sock *sp = sctp_sk(sk);
2848
2849 if (rtoinfo.srto_initial != 0)
2850 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2851 if (rtoinfo.srto_max != 0)
2852 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2853 if (rtoinfo.srto_min != 0)
2854 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2855 }
2856
2857 return 0;
2858}
2859
2860/*
2861 *
2862 * 7.1.2 SCTP_ASSOCINFO
2863 *
59c51591 2864 * This option is used to tune the maximum retransmission attempts
1da177e4
LT
2865 * of the association.
2866 * Returns an error if the new association retransmission value is
2867 * greater than the sum of the retransmission value of the peer.
2868 * See [SCTP] for more information.
2869 *
2870 */
b7058842 2871static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2872{
2873
2874 struct sctp_assocparams assocparams;
2875 struct sctp_association *asoc;
2876
2877 if (optlen != sizeof(struct sctp_assocparams))
2878 return -EINVAL;
2879 if (copy_from_user(&assocparams, optval, optlen))
2880 return -EFAULT;
2881
2882 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2883
2884 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2885 return -EINVAL;
2886
2887 /* Set the values to the specific association */
2888 if (asoc) {
402d68c4
VY
2889 if (assocparams.sasoc_asocmaxrxt != 0) {
2890 __u32 path_sum = 0;
2891 int paths = 0;
402d68c4
VY
2892 struct sctp_transport *peer_addr;
2893
9dbc15f0
RD
2894 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2895 transports) {
402d68c4
VY
2896 path_sum += peer_addr->pathmaxrxt;
2897 paths++;
2898 }
2899
025dfdaf 2900 /* Only validate asocmaxrxt if we have more than
402d68c4
VY
2901 * one path/transport. We do this because path
2902 * retransmissions are only counted when we have more
2903 * then one path.
2904 */
2905 if (paths > 1 &&
2906 assocparams.sasoc_asocmaxrxt > path_sum)
2907 return -EINVAL;
2908
1da177e4 2909 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
402d68c4
VY
2910 }
2911
1da177e4
LT
2912 if (assocparams.sasoc_cookie_life != 0) {
2913 asoc->cookie_life.tv_sec =
2914 assocparams.sasoc_cookie_life / 1000;
2915 asoc->cookie_life.tv_usec =
2916 (assocparams.sasoc_cookie_life % 1000)
2917 * 1000;
2918 }
2919 } else {
2920 /* Set the values to the endpoint */
2921 struct sctp_sock *sp = sctp_sk(sk);
2922
2923 if (assocparams.sasoc_asocmaxrxt != 0)
2924 sp->assocparams.sasoc_asocmaxrxt =
2925 assocparams.sasoc_asocmaxrxt;
2926 if (assocparams.sasoc_cookie_life != 0)
2927 sp->assocparams.sasoc_cookie_life =
2928 assocparams.sasoc_cookie_life;
2929 }
2930 return 0;
2931}
2932
2933/*
2934 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2935 *
2936 * This socket option is a boolean flag which turns on or off mapped V4
2937 * addresses. If this option is turned on and the socket is type
2938 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2939 * If this option is turned off, then no mapping will be done of V4
2940 * addresses and a user will receive both PF_INET6 and PF_INET type
2941 * addresses on the socket.
2942 */
b7058842 2943static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2944{
2945 int val;
2946 struct sctp_sock *sp = sctp_sk(sk);
2947
2948 if (optlen < sizeof(int))
2949 return -EINVAL;
2950 if (get_user(val, (int __user *)optval))
2951 return -EFAULT;
2952 if (val)
2953 sp->v4mapped = 1;
2954 else
2955 sp->v4mapped = 0;
2956
2957 return 0;
2958}
2959
2960/*
e89c2095
WY
2961 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2962 * This option will get or set the maximum size to put in any outgoing
2963 * SCTP DATA chunk. If a message is larger than this size it will be
1da177e4
LT
2964 * fragmented by SCTP into the specified size. Note that the underlying
2965 * SCTP implementation may fragment into smaller sized chunks when the
2966 * PMTU of the underlying association is smaller than the value set by
e89c2095
WY
2967 * the user. The default value for this option is '0' which indicates
2968 * the user is NOT limiting fragmentation and only the PMTU will effect
2969 * SCTP's choice of DATA chunk size. Note also that values set larger
2970 * than the maximum size of an IP datagram will effectively let SCTP
2971 * control fragmentation (i.e. the same as setting this option to 0).
2972 *
2973 * The following structure is used to access and modify this parameter:
2974 *
2975 * struct sctp_assoc_value {
2976 * sctp_assoc_t assoc_id;
2977 * uint32_t assoc_value;
2978 * };
2979 *
2980 * assoc_id: This parameter is ignored for one-to-one style sockets.
2981 * For one-to-many style sockets this parameter indicates which
2982 * association the user is performing an action upon. Note that if
2983 * this field's value is zero then the endpoints default value is
2984 * changed (effecting future associations only).
2985 * assoc_value: This parameter specifies the maximum size in bytes.
1da177e4 2986 */
b7058842 2987static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4 2988{
e89c2095 2989 struct sctp_assoc_value params;
1da177e4 2990 struct sctp_association *asoc;
1da177e4
LT
2991 struct sctp_sock *sp = sctp_sk(sk);
2992 int val;
2993
e89c2095 2994 if (optlen == sizeof(int)) {
145ce502
JP
2995 pr_warn("Use of int in maxseg socket option deprecated\n");
2996 pr_warn("Use struct sctp_assoc_value instead\n");
e89c2095
WY
2997 if (copy_from_user(&val, optval, optlen))
2998 return -EFAULT;
2999 params.assoc_id = 0;
3000 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3001 if (copy_from_user(&params, optval, optlen))
3002 return -EFAULT;
3003 val = params.assoc_value;
3004 } else
1da177e4 3005 return -EINVAL;
e89c2095 3006
96a33998 3007 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
1da177e4 3008 return -EINVAL;
1da177e4 3009
e89c2095
WY
3010 asoc = sctp_id2assoc(sk, params.assoc_id);
3011 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3012 return -EINVAL;
3013
3014 if (asoc) {
3015 if (val == 0) {
3016 val = asoc->pathmtu;
3017 val -= sp->pf->af->net_header_len;
3018 val -= sizeof(struct sctphdr) +
3019 sizeof(struct sctp_data_chunk);
3020 }
f68b2e05
VY
3021 asoc->user_frag = val;
3022 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
e89c2095
WY
3023 } else {
3024 sp->user_frag = val;
1da177e4
LT
3025 }
3026
3027 return 0;
3028}
3029
3030
3031/*
3032 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3033 *
3034 * Requests that the peer mark the enclosed address as the association
3035 * primary. The enclosed address must be one of the association's
3036 * locally bound addresses. The following structure is used to make a
3037 * set primary request:
3038 */
3039static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
b7058842 3040 unsigned int optlen)
1da177e4
LT
3041{
3042 struct sctp_sock *sp;
1da177e4
LT
3043 struct sctp_association *asoc = NULL;
3044 struct sctp_setpeerprim prim;
3045 struct sctp_chunk *chunk;
40a01039 3046 struct sctp_af *af;
1da177e4
LT
3047 int err;
3048
3049 sp = sctp_sk(sk);
1da177e4
LT
3050
3051 if (!sctp_addip_enable)
3052 return -EPERM;
3053
3054 if (optlen != sizeof(struct sctp_setpeerprim))
3055 return -EINVAL;
3056
3057 if (copy_from_user(&prim, optval, optlen))
3058 return -EFAULT;
3059
3060 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
d808ad9a 3061 if (!asoc)
1da177e4
LT
3062 return -EINVAL;
3063
3064 if (!asoc->peer.asconf_capable)
3065 return -EPERM;
3066
3067 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3068 return -EPERM;
3069
3070 if (!sctp_state(asoc, ESTABLISHED))
3071 return -ENOTCONN;
3072
40a01039
WY
3073 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3074 if (!af)
3075 return -EINVAL;
3076
3077 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3078 return -EADDRNOTAVAIL;
3079
1da177e4
LT
3080 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3081 return -EADDRNOTAVAIL;
3082
3083 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3084 chunk = sctp_make_asconf_set_prim(asoc,
3085 (union sctp_addr *)&prim.sspp_addr);
3086 if (!chunk)
3087 return -ENOMEM;
3088
3089 err = sctp_send_asconf(asoc, chunk);
3090
3091 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3092
3093 return err;
3094}
3095
0f3fffd8 3096static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
b7058842 3097 unsigned int optlen)
1da177e4 3098{
0f3fffd8 3099 struct sctp_setadaptation adaptation;
1da177e4 3100
0f3fffd8 3101 if (optlen != sizeof(struct sctp_setadaptation))
1da177e4 3102 return -EINVAL;
0f3fffd8 3103 if (copy_from_user(&adaptation, optval, optlen))
1da177e4
LT
3104 return -EFAULT;
3105
0f3fffd8 3106 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
1da177e4
LT
3107
3108 return 0;
3109}
3110
6ab792f5
ISJ
3111/*
3112 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3113 *
3114 * The context field in the sctp_sndrcvinfo structure is normally only
3115 * used when a failed message is retrieved holding the value that was
3116 * sent down on the actual send call. This option allows the setting of
3117 * a default context on an association basis that will be received on
3118 * reading messages from the peer. This is especially helpful in the
3119 * one-2-many model for an application to keep some reference to an
3120 * internal state machine that is processing messages on the
3121 * association. Note that the setting of this value only effects
3122 * received messages from the peer and does not effect the value that is
3123 * saved with outbound messages.
3124 */
3125static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
b7058842 3126 unsigned int optlen)
6ab792f5
ISJ
3127{
3128 struct sctp_assoc_value params;
3129 struct sctp_sock *sp;
3130 struct sctp_association *asoc;
3131
3132 if (optlen != sizeof(struct sctp_assoc_value))
3133 return -EINVAL;
3134 if (copy_from_user(&params, optval, optlen))
3135 return -EFAULT;
3136
3137 sp = sctp_sk(sk);
3138
3139 if (params.assoc_id != 0) {
3140 asoc = sctp_id2assoc(sk, params.assoc_id);
3141 if (!asoc)
3142 return -EINVAL;
3143 asoc->default_rcv_context = params.assoc_value;
3144 } else {
3145 sp->default_rcv_context = params.assoc_value;
3146 }
3147
3148 return 0;
3149}
3150
b6e1331f
VY
3151/*
3152 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3153 *
3154 * This options will at a minimum specify if the implementation is doing
3155 * fragmented interleave. Fragmented interleave, for a one to many
3156 * socket, is when subsequent calls to receive a message may return
3157 * parts of messages from different associations. Some implementations
3158 * may allow you to turn this value on or off. If so, when turned off,
3159 * no fragment interleave will occur (which will cause a head of line
3160 * blocking amongst multiple associations sharing the same one to many
3161 * socket). When this option is turned on, then each receive call may
3162 * come from a different association (thus the user must receive data
3163 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3164 * association each receive belongs to.
3165 *
3166 * This option takes a boolean value. A non-zero value indicates that
3167 * fragmented interleave is on. A value of zero indicates that
3168 * fragmented interleave is off.
3169 *
3170 * Note that it is important that an implementation that allows this
3171 * option to be turned on, have it off by default. Otherwise an unaware
3172 * application using the one to many model may become confused and act
3173 * incorrectly.
3174 */
3175static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3176 char __user *optval,
b7058842 3177 unsigned int optlen)
b6e1331f
VY
3178{
3179 int val;
3180
3181 if (optlen != sizeof(int))
3182 return -EINVAL;
3183 if (get_user(val, (int __user *)optval))
3184 return -EFAULT;
3185
3186 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3187
3188 return 0;
3189}
3190
d49d91d7 3191/*
8510b937 3192 * 8.1.21. Set or Get the SCTP Partial Delivery Point
d49d91d7 3193 * (SCTP_PARTIAL_DELIVERY_POINT)
8510b937 3194 *
d49d91d7
VY
3195 * This option will set or get the SCTP partial delivery point. This
3196 * point is the size of a message where the partial delivery API will be
3197 * invoked to help free up rwnd space for the peer. Setting this to a
8510b937 3198 * lower value will cause partial deliveries to happen more often. The
d49d91d7 3199 * calls argument is an integer that sets or gets the partial delivery
8510b937
WY
3200 * point. Note also that the call will fail if the user attempts to set
3201 * this value larger than the socket receive buffer size.
3202 *
3203 * Note that any single message having a length smaller than or equal to
3204 * the SCTP partial delivery point will be delivered in one single read
3205 * call as long as the user provided buffer is large enough to hold the
3206 * message.
d49d91d7
VY
3207 */
3208static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3209 char __user *optval,
b7058842 3210 unsigned int optlen)
d49d91d7
VY
3211{
3212 u32 val;
3213
3214 if (optlen != sizeof(u32))
3215 return -EINVAL;
3216 if (get_user(val, (int __user *)optval))
3217 return -EFAULT;
3218
8510b937
WY
3219 /* Note: We double the receive buffer from what the user sets
3220 * it to be, also initial rwnd is based on rcvbuf/2.
3221 */
3222 if (val > (sk->sk_rcvbuf >> 1))
3223 return -EINVAL;
3224
d49d91d7
VY
3225 sctp_sk(sk)->pd_point = val;
3226
3227 return 0; /* is this the right error code? */
3228}
3229
70331571
VY
3230/*
3231 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3232 *
3233 * This option will allow a user to change the maximum burst of packets
3234 * that can be emitted by this association. Note that the default value
3235 * is 4, and some implementations may restrict this setting so that it
3236 * can only be lowered.
3237 *
3238 * NOTE: This text doesn't seem right. Do this on a socket basis with
3239 * future associations inheriting the socket value.
3240 */
3241static int sctp_setsockopt_maxburst(struct sock *sk,
3242 char __user *optval,
b7058842 3243 unsigned int optlen)
70331571 3244{
219b99a9
NH
3245 struct sctp_assoc_value params;
3246 struct sctp_sock *sp;
3247 struct sctp_association *asoc;
70331571 3248 int val;
219b99a9 3249 int assoc_id = 0;
70331571 3250
219b99a9 3251 if (optlen == sizeof(int)) {
145ce502
JP
3252 pr_warn("Use of int in max_burst socket option deprecated\n");
3253 pr_warn("Use struct sctp_assoc_value instead\n");
219b99a9
NH
3254 if (copy_from_user(&val, optval, optlen))
3255 return -EFAULT;
3256 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3257 if (copy_from_user(&params, optval, optlen))
3258 return -EFAULT;
3259 val = params.assoc_value;
3260 assoc_id = params.assoc_id;
3261 } else
70331571
VY
3262 return -EINVAL;
3263
219b99a9
NH
3264 sp = sctp_sk(sk);
3265
3266 if (assoc_id != 0) {
3267 asoc = sctp_id2assoc(sk, assoc_id);
3268 if (!asoc)
3269 return -EINVAL;
3270 asoc->max_burst = val;
3271 } else
3272 sp->max_burst = val;
70331571
VY
3273
3274 return 0;
3275}
3276
65b07e5d
VY
3277/*
3278 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3279 *
3280 * This set option adds a chunk type that the user is requesting to be
3281 * received only in an authenticated way. Changes to the list of chunks
3282 * will only effect future associations on the socket.
3283 */
3284static int sctp_setsockopt_auth_chunk(struct sock *sk,
b7058842
DM
3285 char __user *optval,
3286 unsigned int optlen)
65b07e5d
VY
3287{
3288 struct sctp_authchunk val;
3289
5e739d17
VY
3290 if (!sctp_auth_enable)
3291 return -EACCES;
3292
65b07e5d
VY
3293 if (optlen != sizeof(struct sctp_authchunk))
3294 return -EINVAL;
3295 if (copy_from_user(&val, optval, optlen))
3296 return -EFAULT;
3297
3298 switch (val.sauth_chunk) {
7fd71b1e
JP
3299 case SCTP_CID_INIT:
3300 case SCTP_CID_INIT_ACK:
3301 case SCTP_CID_SHUTDOWN_COMPLETE:
3302 case SCTP_CID_AUTH:
3303 return -EINVAL;
65b07e5d
VY
3304 }
3305
3306 /* add this chunk id to the endpoint */
3307 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3308}
3309
3310/*
3311 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3312 *
3313 * This option gets or sets the list of HMAC algorithms that the local
3314 * endpoint requires the peer to use.
3315 */
3316static int sctp_setsockopt_hmac_ident(struct sock *sk,
b7058842
DM
3317 char __user *optval,
3318 unsigned int optlen)
65b07e5d
VY
3319{
3320 struct sctp_hmacalgo *hmacs;
d9724055 3321 u32 idents;
65b07e5d
VY
3322 int err;
3323
5e739d17
VY
3324 if (!sctp_auth_enable)
3325 return -EACCES;
3326
65b07e5d
VY
3327 if (optlen < sizeof(struct sctp_hmacalgo))
3328 return -EINVAL;
3329
934253a7
SW
3330 hmacs= memdup_user(optval, optlen);
3331 if (IS_ERR(hmacs))
3332 return PTR_ERR(hmacs);
65b07e5d 3333
d9724055
VY
3334 idents = hmacs->shmac_num_idents;
3335 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3336 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
65b07e5d
VY
3337 err = -EINVAL;
3338 goto out;
3339 }
3340
3341 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3342out:
3343 kfree(hmacs);
3344 return err;
3345}
3346
3347/*
3348 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3349 *
3350 * This option will set a shared secret key which is used to build an
3351 * association shared key.
3352 */
3353static int sctp_setsockopt_auth_key(struct sock *sk,
3354 char __user *optval,
b7058842 3355 unsigned int optlen)
65b07e5d
VY
3356{
3357 struct sctp_authkey *authkey;
3358 struct sctp_association *asoc;
3359 int ret;
3360
5e739d17
VY
3361 if (!sctp_auth_enable)
3362 return -EACCES;
3363
65b07e5d
VY
3364 if (optlen <= sizeof(struct sctp_authkey))
3365 return -EINVAL;
3366
934253a7
SW
3367 authkey= memdup_user(optval, optlen);
3368 if (IS_ERR(authkey))
3369 return PTR_ERR(authkey);
65b07e5d 3370
328fc47e 3371 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
30c2235c
VY
3372 ret = -EINVAL;
3373 goto out;
3374 }
3375
65b07e5d
VY
3376 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3377 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3378 ret = -EINVAL;
3379 goto out;
3380 }
3381
3382 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3383out:
3384 kfree(authkey);
3385 return ret;
3386}
3387
3388/*
3389 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3390 *
3391 * This option will get or set the active shared key to be used to build
3392 * the association shared key.
3393 */
3394static int sctp_setsockopt_active_key(struct sock *sk,
b7058842
DM
3395 char __user *optval,
3396 unsigned int optlen)
65b07e5d
VY
3397{
3398 struct sctp_authkeyid val;
3399 struct sctp_association *asoc;
3400
5e739d17
VY
3401 if (!sctp_auth_enable)
3402 return -EACCES;
3403
65b07e5d
VY
3404 if (optlen != sizeof(struct sctp_authkeyid))
3405 return -EINVAL;
3406 if (copy_from_user(&val, optval, optlen))
3407 return -EFAULT;
3408
3409 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3410 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3411 return -EINVAL;
3412
3413 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3414 val.scact_keynumber);
3415}
3416
3417/*
3418 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3419 *
3420 * This set option will delete a shared secret key from use.
3421 */
3422static int sctp_setsockopt_del_key(struct sock *sk,
b7058842
DM
3423 char __user *optval,
3424 unsigned int optlen)
65b07e5d
VY
3425{
3426 struct sctp_authkeyid val;
3427 struct sctp_association *asoc;
3428
5e739d17
VY
3429 if (!sctp_auth_enable)
3430 return -EACCES;
3431
65b07e5d
VY
3432 if (optlen != sizeof(struct sctp_authkeyid))
3433 return -EINVAL;
3434 if (copy_from_user(&val, optval, optlen))
3435 return -EFAULT;
3436
3437 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3438 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3439 return -EINVAL;
3440
3441 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3442 val.scact_keynumber);
3443
3444}
3445
7dc04d71
MH
3446/*
3447 * 8.1.23 SCTP_AUTO_ASCONF
3448 *
3449 * This option will enable or disable the use of the automatic generation of
3450 * ASCONF chunks to add and delete addresses to an existing association. Note
3451 * that this option has two caveats namely: a) it only affects sockets that
3452 * are bound to all addresses available to the SCTP stack, and b) the system
3453 * administrator may have an overriding control that turns the ASCONF feature
3454 * off no matter what setting the socket option may have.
3455 * This option expects an integer boolean flag, where a non-zero value turns on
3456 * the option, and a zero value turns off the option.
3457 * Note. In this implementation, socket operation overrides default parameter
3458 * being set by sysctl as well as FreeBSD implementation
3459 */
3460static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3461 unsigned int optlen)
3462{
3463 int val;
3464 struct sctp_sock *sp = sctp_sk(sk);
3465
3466 if (optlen < sizeof(int))
3467 return -EINVAL;
3468 if (get_user(val, (int __user *)optval))
3469 return -EFAULT;
3470 if (!sctp_is_ep_boundall(sk) && val)
3471 return -EINVAL;
3472 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3473 return 0;
3474
3475 if (val == 0 && sp->do_auto_asconf) {
3476 list_del(&sp->auto_asconf_list);
3477 sp->do_auto_asconf = 0;
3478 } else if (val && !sp->do_auto_asconf) {
3479 list_add_tail(&sp->auto_asconf_list,
4db67e80 3480 &sock_net(sk)->sctp.auto_asconf_splist);
7dc04d71
MH
3481 sp->do_auto_asconf = 1;
3482 }
3483 return 0;
3484}
3485
65b07e5d 3486
5aa93bcf
NH
3487/*
3488 * SCTP_PEER_ADDR_THLDS
3489 *
3490 * This option allows us to alter the partially failed threshold for one or all
3491 * transports in an association. See Section 6.1 of:
3492 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3493 */
3494static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3495 char __user *optval,
3496 unsigned int optlen)
3497{
3498 struct sctp_paddrthlds val;
3499 struct sctp_transport *trans;
3500 struct sctp_association *asoc;
3501
3502 if (optlen < sizeof(struct sctp_paddrthlds))
3503 return -EINVAL;
3504 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3505 sizeof(struct sctp_paddrthlds)))
3506 return -EFAULT;
3507
3508
3509 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3510 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3511 if (!asoc)
3512 return -ENOENT;
3513 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3514 transports) {
3515 if (val.spt_pathmaxrxt)
3516 trans->pathmaxrxt = val.spt_pathmaxrxt;
3517 trans->pf_retrans = val.spt_pathpfthld;
3518 }
3519
3520 if (val.spt_pathmaxrxt)
3521 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3522 asoc->pf_retrans = val.spt_pathpfthld;
3523 } else {
3524 trans = sctp_addr_id2transport(sk, &val.spt_address,
3525 val.spt_assoc_id);
3526 if (!trans)
3527 return -ENOENT;
3528
3529 if (val.spt_pathmaxrxt)
3530 trans->pathmaxrxt = val.spt_pathmaxrxt;
3531 trans->pf_retrans = val.spt_pathpfthld;
3532 }
3533
3534 return 0;
3535}
3536
1da177e4
LT
3537/* API 6.2 setsockopt(), getsockopt()
3538 *
3539 * Applications use setsockopt() and getsockopt() to set or retrieve
3540 * socket options. Socket options are used to change the default
3541 * behavior of sockets calls. They are described in Section 7.
3542 *
3543 * The syntax is:
3544 *
3545 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3546 * int __user *optlen);
3547 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3548 * int optlen);
3549 *
3550 * sd - the socket descript.
3551 * level - set to IPPROTO_SCTP for all SCTP options.
3552 * optname - the option name.
3553 * optval - the buffer to store the value of the option.
3554 * optlen - the size of the buffer.
3555 */
3556SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
b7058842 3557 char __user *optval, unsigned int optlen)
1da177e4
LT
3558{
3559 int retval = 0;
3560
3561 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3562 sk, optname);
3563
3564 /* I can hardly begin to describe how wrong this is. This is
3565 * so broken as to be worse than useless. The API draft
3566 * REALLY is NOT helpful here... I am not convinced that the
3567 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3568 * are at all well-founded.
3569 */
3570 if (level != SOL_SCTP) {
3571 struct sctp_af *af = sctp_sk(sk)->pf->af;
3572 retval = af->setsockopt(sk, level, optname, optval, optlen);
3573 goto out_nounlock;
3574 }
3575
3576 sctp_lock_sock(sk);
3577
3578 switch (optname) {
3579 case SCTP_SOCKOPT_BINDX_ADD:
3580 /* 'optlen' is the size of the addresses buffer. */
3581 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3582 optlen, SCTP_BINDX_ADD_ADDR);
3583 break;
3584
3585 case SCTP_SOCKOPT_BINDX_REM:
3586 /* 'optlen' is the size of the addresses buffer. */
3587 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3588 optlen, SCTP_BINDX_REM_ADDR);
3589 break;
3590
88a0a948
VY
3591 case SCTP_SOCKOPT_CONNECTX_OLD:
3592 /* 'optlen' is the size of the addresses buffer. */
3593 retval = sctp_setsockopt_connectx_old(sk,
3594 (struct sockaddr __user *)optval,
3595 optlen);
3596 break;
3597
3f7a87d2
FF
3598 case SCTP_SOCKOPT_CONNECTX:
3599 /* 'optlen' is the size of the addresses buffer. */
88a0a948
VY
3600 retval = sctp_setsockopt_connectx(sk,
3601 (struct sockaddr __user *)optval,
3602 optlen);
3f7a87d2
FF
3603 break;
3604
1da177e4
LT
3605 case SCTP_DISABLE_FRAGMENTS:
3606 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3607 break;
3608
3609 case SCTP_EVENTS:
3610 retval = sctp_setsockopt_events(sk, optval, optlen);
3611 break;
3612
3613 case SCTP_AUTOCLOSE:
3614 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3615 break;
3616
3617 case SCTP_PEER_ADDR_PARAMS:
3618 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3619 break;
3620
4580ccc0 3621 case SCTP_DELAYED_SACK:
d364d927 3622 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
7708610b 3623 break;
d49d91d7
VY
3624 case SCTP_PARTIAL_DELIVERY_POINT:
3625 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3626 break;
7708610b 3627
1da177e4
LT
3628 case SCTP_INITMSG:
3629 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3630 break;
3631 case SCTP_DEFAULT_SEND_PARAM:
3632 retval = sctp_setsockopt_default_send_param(sk, optval,
3633 optlen);
3634 break;
3635 case SCTP_PRIMARY_ADDR:
3636 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3637 break;
3638 case SCTP_SET_PEER_PRIMARY_ADDR:
3639 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3640 break;
3641 case SCTP_NODELAY:
3642 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3643 break;
3644 case SCTP_RTOINFO:
3645 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3646 break;
3647 case SCTP_ASSOCINFO:
3648 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3649 break;
3650 case SCTP_I_WANT_MAPPED_V4_ADDR:
3651 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3652 break;
3653 case SCTP_MAXSEG:
3654 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3655 break;
0f3fffd8
ISJ
3656 case SCTP_ADAPTATION_LAYER:
3657 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
1da177e4 3658 break;
6ab792f5
ISJ
3659 case SCTP_CONTEXT:
3660 retval = sctp_setsockopt_context(sk, optval, optlen);
3661 break;
b6e1331f
VY
3662 case SCTP_FRAGMENT_INTERLEAVE:
3663 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3664 break;
70331571
VY
3665 case SCTP_MAX_BURST:
3666 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3667 break;
65b07e5d
VY
3668 case SCTP_AUTH_CHUNK:
3669 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3670 break;
3671 case SCTP_HMAC_IDENT:
3672 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3673 break;
3674 case SCTP_AUTH_KEY:
3675 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3676 break;
3677 case SCTP_AUTH_ACTIVE_KEY:
3678 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3679 break;
3680 case SCTP_AUTH_DELETE_KEY:
3681 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3682 break;
7dc04d71
MH
3683 case SCTP_AUTO_ASCONF:
3684 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3685 break;
5aa93bcf
NH
3686 case SCTP_PEER_ADDR_THLDS:
3687 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3688 break;
1da177e4
LT
3689 default:
3690 retval = -ENOPROTOOPT;
3691 break;
3ff50b79 3692 }
1da177e4
LT
3693
3694 sctp_release_sock(sk);
3695
3696out_nounlock:
3697 return retval;
3698}
3699
3700/* API 3.1.6 connect() - UDP Style Syntax
3701 *
3702 * An application may use the connect() call in the UDP model to initiate an
3703 * association without sending data.
3704 *
3705 * The syntax is:
3706 *
3707 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3708 *
3709 * sd: the socket descriptor to have a new association added to.
3710 *
3711 * nam: the address structure (either struct sockaddr_in or struct
3712 * sockaddr_in6 defined in RFC2553 [7]).
3713 *
3714 * len: the size of the address.
3715 */
3f7a87d2 3716SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
1da177e4
LT
3717 int addr_len)
3718{
1da177e4 3719 int err = 0;
3f7a87d2 3720 struct sctp_af *af;
1da177e4
LT
3721
3722 sctp_lock_sock(sk);
3723
3f7a87d2 3724 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
0dc47877 3725 __func__, sk, addr, addr_len);
1da177e4 3726
3f7a87d2
FF
3727 /* Validate addr_len before calling common connect/connectx routine. */
3728 af = sctp_get_af_specific(addr->sa_family);
3729 if (!af || addr_len < af->sockaddr_len) {
3730 err = -EINVAL;
3731 } else {
3732 /* Pass correct addr len to common routine (so it knows there
3733 * is only one address being passed.
3734 */
88a0a948 3735 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
1da177e4
LT
3736 }
3737
1da177e4 3738 sctp_release_sock(sk);
1da177e4
LT
3739 return err;
3740}
3741
3742/* FIXME: Write comments. */
3743SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3744{
3745 return -EOPNOTSUPP; /* STUB */
3746}
3747
3748/* 4.1.4 accept() - TCP Style Syntax
3749 *
3750 * Applications use accept() call to remove an established SCTP
3751 * association from the accept queue of the endpoint. A new socket
3752 * descriptor will be returned from accept() to represent the newly
3753 * formed association.
3754 */
3755SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3756{
3757 struct sctp_sock *sp;
3758 struct sctp_endpoint *ep;
3759 struct sock *newsk = NULL;
3760 struct sctp_association *asoc;
3761 long timeo;
3762 int error = 0;
3763
3764 sctp_lock_sock(sk);
3765
3766 sp = sctp_sk(sk);
3767 ep = sp->ep;
3768
3769 if (!sctp_style(sk, TCP)) {
3770 error = -EOPNOTSUPP;
3771 goto out;
3772 }
3773
3774 if (!sctp_sstate(sk, LISTENING)) {
3775 error = -EINVAL;
3776 goto out;
3777 }
3778
8abfedd8 3779 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1da177e4
LT
3780
3781 error = sctp_wait_for_accept(sk, timeo);
3782 if (error)
3783 goto out;
3784
3785 /* We treat the list of associations on the endpoint as the accept
3786 * queue and pick the first association on the list.
3787 */
3788 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3789
3790 newsk = sp->pf->create_accept_sk(sk, asoc);
3791 if (!newsk) {
3792 error = -ENOMEM;
3793 goto out;
3794 }
3795
3796 /* Populate the fields of the newsk from the oldsk and migrate the
3797 * asoc to the newsk.
3798 */
3799 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3800
3801out:
3802 sctp_release_sock(sk);
d808ad9a 3803 *err = error;
1da177e4
LT
3804 return newsk;
3805}
3806
3807/* The SCTP ioctl handler. */
3808SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3809{
65040c33
DEFP
3810 int rc = -ENOTCONN;
3811
3812 sctp_lock_sock(sk);
3813
3814 /*
3815 * SEQPACKET-style sockets in LISTENING state are valid, for
3816 * SCTP, so only discard TCP-style sockets in LISTENING state.
3817 */
3818 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3819 goto out;
3820
3821 switch (cmd) {
3822 case SIOCINQ: {
3823 struct sk_buff *skb;
3824 unsigned int amount = 0;
3825
3826 skb = skb_peek(&sk->sk_receive_queue);
3827 if (skb != NULL) {
3828 /*
3829 * We will only return the amount of this packet since
3830 * that is all that will be read.
3831 */
3832 amount = skb->len;
3833 }
3834 rc = put_user(amount, (int __user *)arg);
65040c33 3835 break;
9a7241c2 3836 }
65040c33
DEFP
3837 default:
3838 rc = -ENOIOCTLCMD;
3839 break;
3840 }
3841out:
3842 sctp_release_sock(sk);
3843 return rc;
1da177e4
LT
3844}
3845
3846/* This is the function which gets called during socket creation to
3847 * initialized the SCTP-specific portion of the sock.
3848 * The sock structure should already be zero-filled memory.
3849 */
3850SCTP_STATIC int sctp_init_sock(struct sock *sk)
3851{
3852 struct sctp_endpoint *ep;
3853 struct sctp_sock *sp;
3854
3855 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3856
3857 sp = sctp_sk(sk);
3858
3859 /* Initialize the SCTP per socket area. */
3860 switch (sk->sk_type) {
3861 case SOCK_SEQPACKET:
3862 sp->type = SCTP_SOCKET_UDP;
3863 break;
3864 case SOCK_STREAM:
3865 sp->type = SCTP_SOCKET_TCP;
3866 break;
3867 default:
3868 return -ESOCKTNOSUPPORT;
3869 }
3870
3871 /* Initialize default send parameters. These parameters can be
3872 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3873 */
3874 sp->default_stream = 0;
3875 sp->default_ppid = 0;
3876 sp->default_flags = 0;
3877 sp->default_context = 0;
3878 sp->default_timetolive = 0;
3879
6ab792f5 3880 sp->default_rcv_context = 0;
70331571 3881 sp->max_burst = sctp_max_burst;
6ab792f5 3882
1da177e4
LT
3883 /* Initialize default setup parameters. These parameters
3884 * can be modified with the SCTP_INITMSG socket option or
3885 * overridden by the SCTP_INIT CMSG.
3886 */
3887 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3888 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3889 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3fd091e7 3890 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
1da177e4
LT
3891
3892 /* Initialize default RTO related parameters. These parameters can
3893 * be modified for with the SCTP_RTOINFO socket option.
3894 */
3fd091e7
VY
3895 sp->rtoinfo.srto_initial = sctp_rto_initial;
3896 sp->rtoinfo.srto_max = sctp_rto_max;
3897 sp->rtoinfo.srto_min = sctp_rto_min;
1da177e4
LT
3898
3899 /* Initialize default association related parameters. These parameters
3900 * can be modified with the SCTP_ASSOCINFO socket option.
3901 */
3902 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3903 sp->assocparams.sasoc_number_peer_destinations = 0;
3904 sp->assocparams.sasoc_peer_rwnd = 0;
3905 sp->assocparams.sasoc_local_rwnd = 0;
3fd091e7 3906 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
1da177e4
LT
3907
3908 /* Initialize default event subscriptions. By default, all the
d808ad9a 3909 * options are off.
1da177e4
LT
3910 */
3911 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3912
3913 /* Default Peer Address Parameters. These defaults can
3914 * be modified via SCTP_PEER_ADDR_PARAMS
3915 */
3fd091e7 3916 sp->hbinterval = sctp_hb_interval;
52ccb8e9
FF
3917 sp->pathmaxrxt = sctp_max_retrans_path;
3918 sp->pathmtu = 0; // allow default discovery
3fd091e7 3919 sp->sackdelay = sctp_sack_timeout;
7bfe8bdb 3920 sp->sackfreq = 2;
52ccb8e9 3921 sp->param_flags = SPP_HB_ENABLE |
d808ad9a
YH
3922 SPP_PMTUD_ENABLE |
3923 SPP_SACKDELAY_ENABLE;
1da177e4
LT
3924
3925 /* If enabled no SCTP message fragmentation will be performed.
3926 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3927 */
3928 sp->disable_fragments = 0;
3929
208edef6
SS
3930 /* Enable Nagle algorithm by default. */
3931 sp->nodelay = 0;
1da177e4
LT
3932
3933 /* Enable by default. */
3934 sp->v4mapped = 1;
3935
3936 /* Auto-close idle associations after the configured
3937 * number of seconds. A value of 0 disables this
3938 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3939 * for UDP-style sockets only.
3940 */
3941 sp->autoclose = 0;
3942
3943 /* User specified fragmentation limit. */
3944 sp->user_frag = 0;
3945
0f3fffd8 3946 sp->adaptation_ind = 0;
1da177e4
LT
3947
3948 sp->pf = sctp_get_pf_specific(sk->sk_family);
3949
3950 /* Control variables for partial data delivery. */
b6e1331f 3951 atomic_set(&sp->pd_mode, 0);
1da177e4 3952 skb_queue_head_init(&sp->pd_lobby);
b6e1331f 3953 sp->frag_interleave = 0;
1da177e4
LT
3954
3955 /* Create a per socket endpoint structure. Even if we
3956 * change the data structure relationships, this may still
3957 * be useful for storing pre-connect address information.
3958 */
3959 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3960 if (!ep)
3961 return -ENOMEM;
3962
3963 sp->ep = ep;
3964 sp->hmac = NULL;
3965
3966 SCTP_DBG_OBJCNT_INC(sock);
6f756a8c
DM
3967
3968 local_bh_disable();
81419d86 3969 percpu_counter_inc(&sctp_sockets_allocated);
9a57f7fa 3970 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
9f7d653b
MH
3971 if (sctp_default_auto_asconf) {
3972 list_add_tail(&sp->auto_asconf_list,
4db67e80 3973 &sock_net(sk)->sctp.auto_asconf_splist);
9f7d653b
MH
3974 sp->do_auto_asconf = 1;
3975 } else
3976 sp->do_auto_asconf = 0;
6f756a8c
DM
3977 local_bh_enable();
3978
1da177e4
LT
3979 return 0;
3980}
3981
3982/* Cleanup any SCTP per socket resources. */
7d06b2e0 3983SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
1da177e4 3984{
9f7d653b 3985 struct sctp_sock *sp;
1da177e4
LT
3986
3987 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3988
3989 /* Release our hold on the endpoint. */
9f7d653b
MH
3990 sp = sctp_sk(sk);
3991 if (sp->do_auto_asconf) {
3992 sp->do_auto_asconf = 0;
3993 list_del(&sp->auto_asconf_list);
3994 }
3995 sctp_endpoint_free(sp->ep);
5bc0b3bf 3996 local_bh_disable();
81419d86 3997 percpu_counter_dec(&sctp_sockets_allocated);
9a57f7fa 3998 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5bc0b3bf 3999 local_bh_enable();
1da177e4
LT
4000}
4001
4002/* API 4.1.7 shutdown() - TCP Style Syntax
4003 * int shutdown(int socket, int how);
4004 *
4005 * sd - the socket descriptor of the association to be closed.
4006 * how - Specifies the type of shutdown. The values are
4007 * as follows:
4008 * SHUT_RD
4009 * Disables further receive operations. No SCTP
4010 * protocol action is taken.
4011 * SHUT_WR
4012 * Disables further send operations, and initiates
4013 * the SCTP shutdown sequence.
4014 * SHUT_RDWR
4015 * Disables further send and receive operations
4016 * and initiates the SCTP shutdown sequence.
4017 */
4018SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4019{
55e26eb9 4020 struct net *net = sock_net(sk);
1da177e4
LT
4021 struct sctp_endpoint *ep;
4022 struct sctp_association *asoc;
4023
4024 if (!sctp_style(sk, TCP))
4025 return;
4026
4027 if (how & SEND_SHUTDOWN) {
4028 ep = sctp_sk(sk)->ep;
4029 if (!list_empty(&ep->asocs)) {
4030 asoc = list_entry(ep->asocs.next,
4031 struct sctp_association, asocs);
55e26eb9 4032 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
4033 }
4034 }
4035}
4036
4037/* 7.2.1 Association Status (SCTP_STATUS)
4038
4039 * Applications can retrieve current status information about an
4040 * association, including association state, peer receiver window size,
4041 * number of unacked data chunks, and number of data chunks pending
4042 * receipt. This information is read-only.
4043 */
4044static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4045 char __user *optval,
4046 int __user *optlen)
4047{
4048 struct sctp_status status;
4049 struct sctp_association *asoc = NULL;
4050 struct sctp_transport *transport;
4051 sctp_assoc_t associd;
4052 int retval = 0;
4053
408f22e8 4054 if (len < sizeof(status)) {
1da177e4
LT
4055 retval = -EINVAL;
4056 goto out;
4057 }
4058
408f22e8
NH
4059 len = sizeof(status);
4060 if (copy_from_user(&status, optval, len)) {
1da177e4
LT
4061 retval = -EFAULT;
4062 goto out;
4063 }
4064
4065 associd = status.sstat_assoc_id;
4066 asoc = sctp_id2assoc(sk, associd);
4067 if (!asoc) {
4068 retval = -EINVAL;
4069 goto out;
4070 }
4071
4072 transport = asoc->peer.primary_path;
4073
4074 status.sstat_assoc_id = sctp_assoc2id(asoc);
4075 status.sstat_state = asoc->state;
4076 status.sstat_rwnd = asoc->peer.rwnd;
4077 status.sstat_unackdata = asoc->unack_data;
4078
4079 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4080 status.sstat_instrms = asoc->c.sinit_max_instreams;
4081 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4082 status.sstat_fragmentation_point = asoc->frag_point;
4083 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
8cec6b80
AV
4084 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4085 transport->af_specific->sockaddr_len);
1da177e4
LT
4086 /* Map ipv4 address into v4-mapped-on-v6 address. */
4087 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4088 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3f7a87d2 4089 status.sstat_primary.spinfo_state = transport->state;
1da177e4
LT
4090 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4091 status.sstat_primary.spinfo_srtt = transport->srtt;
4092 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 4093 status.sstat_primary.spinfo_mtu = transport->pathmtu;
1da177e4 4094
3f7a87d2
FF
4095 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4096 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4097
1da177e4
LT
4098 if (put_user(len, optlen)) {
4099 retval = -EFAULT;
4100 goto out;
4101 }
4102
4103 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4104 len, status.sstat_state, status.sstat_rwnd,
4105 status.sstat_assoc_id);
4106
4107 if (copy_to_user(optval, &status, len)) {
4108 retval = -EFAULT;
4109 goto out;
4110 }
4111
4112out:
a02cec21 4113 return retval;
1da177e4
LT
4114}
4115
4116
4117/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4118 *
4119 * Applications can retrieve information about a specific peer address
4120 * of an association, including its reachability state, congestion
4121 * window, and retransmission timer values. This information is
4122 * read-only.
4123 */
4124static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4125 char __user *optval,
4126 int __user *optlen)
4127{
4128 struct sctp_paddrinfo pinfo;
4129 struct sctp_transport *transport;
4130 int retval = 0;
4131
408f22e8 4132 if (len < sizeof(pinfo)) {
1da177e4
LT
4133 retval = -EINVAL;
4134 goto out;
4135 }
4136
408f22e8
NH
4137 len = sizeof(pinfo);
4138 if (copy_from_user(&pinfo, optval, len)) {
1da177e4
LT
4139 retval = -EFAULT;
4140 goto out;
4141 }
4142
4143 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4144 pinfo.spinfo_assoc_id);
4145 if (!transport)
4146 return -EINVAL;
4147
4148 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3f7a87d2 4149 pinfo.spinfo_state = transport->state;
1da177e4
LT
4150 pinfo.spinfo_cwnd = transport->cwnd;
4151 pinfo.spinfo_srtt = transport->srtt;
4152 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 4153 pinfo.spinfo_mtu = transport->pathmtu;
1da177e4 4154
3f7a87d2
FF
4155 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4156 pinfo.spinfo_state = SCTP_ACTIVE;
4157
1da177e4
LT
4158 if (put_user(len, optlen)) {
4159 retval = -EFAULT;
4160 goto out;
4161 }
4162
4163 if (copy_to_user(optval, &pinfo, len)) {
4164 retval = -EFAULT;
4165 goto out;
4166 }
4167
4168out:
a02cec21 4169 return retval;
1da177e4
LT
4170}
4171
4172/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4173 *
4174 * This option is a on/off flag. If enabled no SCTP message
4175 * fragmentation will be performed. Instead if a message being sent
4176 * exceeds the current PMTU size, the message will NOT be sent and
4177 * instead a error will be indicated to the user.
4178 */
4179static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4180 char __user *optval, int __user *optlen)
4181{
4182 int val;
4183
4184 if (len < sizeof(int))
4185 return -EINVAL;
4186
4187 len = sizeof(int);
4188 val = (sctp_sk(sk)->disable_fragments == 1);
4189 if (put_user(len, optlen))
4190 return -EFAULT;
4191 if (copy_to_user(optval, &val, len))
4192 return -EFAULT;
4193 return 0;
4194}
4195
4196/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4197 *
4198 * This socket option is used to specify various notifications and
4199 * ancillary data the user wishes to receive.
4200 */
4201static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4202 int __user *optlen)
4203{
acdd5985 4204 if (len <= 0)
1da177e4 4205 return -EINVAL;
acdd5985
TG
4206 if (len > sizeof(struct sctp_event_subscribe))
4207 len = sizeof(struct sctp_event_subscribe);
408f22e8
NH
4208 if (put_user(len, optlen))
4209 return -EFAULT;
1da177e4
LT
4210 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4211 return -EFAULT;
4212 return 0;
4213}
4214
4215/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4216 *
4217 * This socket option is applicable to the UDP-style socket only. When
4218 * set it will cause associations that are idle for more than the
4219 * specified number of seconds to automatically close. An association
4220 * being idle is defined an association that has NOT sent or received
4221 * user data. The special value of '0' indicates that no automatic
4222 * close of any associations should be performed. The option expects an
4223 * integer defining the number of seconds of idle time before an
4224 * association is closed.
4225 */
4226static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4227{
4228 /* Applicable to UDP-style socket only */
4229 if (sctp_style(sk, TCP))
4230 return -EOPNOTSUPP;
408f22e8 4231 if (len < sizeof(int))
1da177e4 4232 return -EINVAL;
408f22e8
NH
4233 len = sizeof(int);
4234 if (put_user(len, optlen))
4235 return -EFAULT;
4236 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
1da177e4
LT
4237 return -EFAULT;
4238 return 0;
4239}
4240
4241/* Helper routine to branch off an association to a new socket. */
0343c554 4242int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
1da177e4 4243{
0343c554 4244 struct sctp_association *asoc = sctp_id2assoc(sk, id);
1da177e4 4245 struct socket *sock;
d570ee49 4246 struct sctp_af *af;
1da177e4
LT
4247 int err = 0;
4248
0343c554
BP
4249 if (!asoc)
4250 return -EINVAL;
4251
1da177e4
LT
4252 /* An association cannot be branched off from an already peeled-off
4253 * socket, nor is this supported for tcp style sockets.
4254 */
4255 if (!sctp_style(sk, UDP))
4256 return -EINVAL;
4257
4258 /* Create a new socket. */
4259 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4260 if (err < 0)
4261 return err;
4262
914e1c8b 4263 sctp_copy_sock(sock->sk, sk, asoc);
4f444308
VY
4264
4265 /* Make peeled-off sockets more like 1-1 accepted sockets.
4266 * Set the daddr and initialize id to something more random
4267 */
d570ee49
VY
4268 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4269 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
914e1c8b
VY
4270
4271 /* Populate the fields of the newsk from the oldsk and migrate the
4272 * asoc to the newsk.
4273 */
4274 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
4f444308 4275
1da177e4
LT
4276 *sockp = sock;
4277
4278 return err;
4279}
0343c554 4280EXPORT_SYMBOL(sctp_do_peeloff);
1da177e4
LT
4281
4282static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4283{
4284 sctp_peeloff_arg_t peeloff;
4285 struct socket *newsock;
4286 int retval = 0;
1da177e4 4287
408f22e8 4288 if (len < sizeof(sctp_peeloff_arg_t))
1da177e4 4289 return -EINVAL;
408f22e8 4290 len = sizeof(sctp_peeloff_arg_t);
1da177e4
LT
4291 if (copy_from_user(&peeloff, optval, len))
4292 return -EFAULT;
4293
0343c554 4294 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
1da177e4
LT
4295 if (retval < 0)
4296 goto out;
4297
4298 /* Map the socket to an unused fd that can be returned to the user. */
a677a039 4299 retval = sock_map_fd(newsock, 0);
1da177e4
LT
4300 if (retval < 0) {
4301 sock_release(newsock);
4302 goto out;
4303 }
4304
0343c554
BP
4305 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4306 __func__, sk, newsock->sk, retval);
1da177e4
LT
4307
4308 /* Return the fd mapped to the new socket. */
4309 peeloff.sd = retval;
408f22e8
NH
4310 if (put_user(len, optlen))
4311 return -EFAULT;
1da177e4
LT
4312 if (copy_to_user(optval, &peeloff, len))
4313 retval = -EFAULT;
4314
4315out:
4316 return retval;
4317}
4318
4319/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4320 *
4321 * Applications can enable or disable heartbeats for any peer address of
4322 * an association, modify an address's heartbeat interval, force a
4323 * heartbeat to be sent immediately, and adjust the address's maximum
4324 * number of retransmissions sent before an address is considered
4325 * unreachable. The following structure is used to access and modify an
4326 * address's parameters:
4327 *
4328 * struct sctp_paddrparams {
52ccb8e9
FF
4329 * sctp_assoc_t spp_assoc_id;
4330 * struct sockaddr_storage spp_address;
4331 * uint32_t spp_hbinterval;
4332 * uint16_t spp_pathmaxrxt;
4333 * uint32_t spp_pathmtu;
4334 * uint32_t spp_sackdelay;
4335 * uint32_t spp_flags;
4336 * };
4337 *
4338 * spp_assoc_id - (one-to-many style socket) This is filled in the
4339 * application, and identifies the association for
4340 * this query.
1da177e4
LT
4341 * spp_address - This specifies which address is of interest.
4342 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
4343 * in milliseconds. If a value of zero
4344 * is present in this field then no changes are to
4345 * be made to this parameter.
1da177e4
LT
4346 * spp_pathmaxrxt - This contains the maximum number of
4347 * retransmissions before this address shall be
52ccb8e9
FF
4348 * considered unreachable. If a value of zero
4349 * is present in this field then no changes are to
4350 * be made to this parameter.
4351 * spp_pathmtu - When Path MTU discovery is disabled the value
4352 * specified here will be the "fixed" path mtu.
4353 * Note that if the spp_address field is empty
4354 * then all associations on this address will
4355 * have this fixed path mtu set upon them.
4356 *
4357 * spp_sackdelay - When delayed sack is enabled, this value specifies
4358 * the number of milliseconds that sacks will be delayed
4359 * for. This value will apply to all addresses of an
4360 * association if the spp_address field is empty. Note
4361 * also, that if delayed sack is enabled and this
4362 * value is set to 0, no change is made to the last
4363 * recorded delayed sack timer value.
4364 *
4365 * spp_flags - These flags are used to control various features
4366 * on an association. The flag field may contain
4367 * zero or more of the following options.
4368 *
4369 * SPP_HB_ENABLE - Enable heartbeats on the
4370 * specified address. Note that if the address
4371 * field is empty all addresses for the association
4372 * have heartbeats enabled upon them.
4373 *
4374 * SPP_HB_DISABLE - Disable heartbeats on the
4375 * speicifed address. Note that if the address
4376 * field is empty all addresses for the association
4377 * will have their heartbeats disabled. Note also
4378 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4379 * mutually exclusive, only one of these two should
4380 * be specified. Enabling both fields will have
4381 * undetermined results.
4382 *
4383 * SPP_HB_DEMAND - Request a user initiated heartbeat
4384 * to be made immediately.
4385 *
4386 * SPP_PMTUD_ENABLE - This field will enable PMTU
4387 * discovery upon the specified address. Note that
4388 * if the address feild is empty then all addresses
4389 * on the association are effected.
4390 *
4391 * SPP_PMTUD_DISABLE - This field will disable PMTU
4392 * discovery upon the specified address. Note that
4393 * if the address feild is empty then all addresses
4394 * on the association are effected. Not also that
4395 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4396 * exclusive. Enabling both will have undetermined
4397 * results.
4398 *
4399 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4400 * on delayed sack. The time specified in spp_sackdelay
4401 * is used to specify the sack delay for this address. Note
4402 * that if spp_address is empty then all addresses will
4403 * enable delayed sack and take on the sack delay
4404 * value specified in spp_sackdelay.
4405 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4406 * off delayed sack. If the spp_address field is blank then
4407 * delayed sack is disabled for the entire association. Note
4408 * also that this field is mutually exclusive to
4409 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4410 * results.
1da177e4
LT
4411 */
4412static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
52ccb8e9 4413 char __user *optval, int __user *optlen)
1da177e4 4414{
52ccb8e9
FF
4415 struct sctp_paddrparams params;
4416 struct sctp_transport *trans = NULL;
4417 struct sctp_association *asoc = NULL;
4418 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 4419
408f22e8 4420 if (len < sizeof(struct sctp_paddrparams))
1da177e4 4421 return -EINVAL;
408f22e8 4422 len = sizeof(struct sctp_paddrparams);
1da177e4
LT
4423 if (copy_from_user(&params, optval, len))
4424 return -EFAULT;
4425
52ccb8e9
FF
4426 /* If an address other than INADDR_ANY is specified, and
4427 * no transport is found, then the request is invalid.
1da177e4 4428 */
52cae8f0 4429 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
4430 trans = sctp_addr_id2transport(sk, &params.spp_address,
4431 params.spp_assoc_id);
4432 if (!trans) {
4433 SCTP_DEBUG_PRINTK("Failed no transport\n");
4434 return -EINVAL;
4435 }
1da177e4
LT
4436 }
4437
52ccb8e9
FF
4438 /* Get association, if assoc_id != 0 and the socket is a one
4439 * to many style socket, and an association was not found, then
4440 * the id was invalid.
4441 */
4442 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4443 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4444 SCTP_DEBUG_PRINTK("Failed no association\n");
1da177e4 4445 return -EINVAL;
52ccb8e9 4446 }
1da177e4 4447
52ccb8e9
FF
4448 if (trans) {
4449 /* Fetch transport values. */
4450 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4451 params.spp_pathmtu = trans->pathmtu;
4452 params.spp_pathmaxrxt = trans->pathmaxrxt;
4453 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
4454
4455 /*draft-11 doesn't say what to return in spp_flags*/
4456 params.spp_flags = trans->param_flags;
4457 } else if (asoc) {
4458 /* Fetch association values. */
4459 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4460 params.spp_pathmtu = asoc->pathmtu;
4461 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4462 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
4463
4464 /*draft-11 doesn't say what to return in spp_flags*/
4465 params.spp_flags = asoc->param_flags;
4466 } else {
4467 /* Fetch socket values. */
4468 params.spp_hbinterval = sp->hbinterval;
4469 params.spp_pathmtu = sp->pathmtu;
4470 params.spp_sackdelay = sp->sackdelay;
4471 params.spp_pathmaxrxt = sp->pathmaxrxt;
1da177e4 4472
52ccb8e9
FF
4473 /*draft-11 doesn't say what to return in spp_flags*/
4474 params.spp_flags = sp->param_flags;
4475 }
1da177e4 4476
1da177e4
LT
4477 if (copy_to_user(optval, &params, len))
4478 return -EFAULT;
4479
4480 if (put_user(len, optlen))
4481 return -EFAULT;
4482
4483 return 0;
4484}
4485
d364d927
WY
4486/*
4487 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
4488 *
4489 * This option will effect the way delayed acks are performed. This
4490 * option allows you to get or set the delayed ack time, in
4491 * milliseconds. It also allows changing the delayed ack frequency.
4492 * Changing the frequency to 1 disables the delayed sack algorithm. If
4493 * the assoc_id is 0, then this sets or gets the endpoints default
4494 * values. If the assoc_id field is non-zero, then the set or get
4495 * effects the specified association for the one to many model (the
4496 * assoc_id field is ignored by the one to one model). Note that if
4497 * sack_delay or sack_freq are 0 when setting this option, then the
4498 * current values will remain unchanged.
4499 *
4500 * struct sctp_sack_info {
4501 * sctp_assoc_t sack_assoc_id;
4502 * uint32_t sack_delay;
4503 * uint32_t sack_freq;
4504 * };
7708610b 4505 *
d364d927
WY
4506 * sack_assoc_id - This parameter, indicates which association the user
4507 * is performing an action upon. Note that if this field's value is
4508 * zero then the endpoints default value is changed (effecting future
4509 * associations only).
7708610b 4510 *
d364d927
WY
4511 * sack_delay - This parameter contains the number of milliseconds that
4512 * the user is requesting the delayed ACK timer be set to. Note that
4513 * this value is defined in the standard to be between 200 and 500
4514 * milliseconds.
7708610b 4515 *
d364d927
WY
4516 * sack_freq - This parameter contains the number of packets that must
4517 * be received before a sack is sent without waiting for the delay
4518 * timer to expire. The default value for this is 2, setting this
4519 * value to 1 will disable the delayed sack algorithm.
7708610b 4520 */
d364d927 4521static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
7708610b
FF
4522 char __user *optval,
4523 int __user *optlen)
4524{
d364d927 4525 struct sctp_sack_info params;
7708610b
FF
4526 struct sctp_association *asoc = NULL;
4527 struct sctp_sock *sp = sctp_sk(sk);
4528
d364d927
WY
4529 if (len >= sizeof(struct sctp_sack_info)) {
4530 len = sizeof(struct sctp_sack_info);
7708610b 4531
d364d927
WY
4532 if (copy_from_user(&params, optval, len))
4533 return -EFAULT;
4534 } else if (len == sizeof(struct sctp_assoc_value)) {
145ce502
JP
4535 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4536 pr_warn("Use struct sctp_sack_info instead\n");
d364d927
WY
4537 if (copy_from_user(&params, optval, len))
4538 return -EFAULT;
4539 } else
4540 return - EINVAL;
7708610b 4541
d364d927 4542 /* Get association, if sack_assoc_id != 0 and the socket is a one
7708610b
FF
4543 * to many style socket, and an association was not found, then
4544 * the id was invalid.
d808ad9a 4545 */
d364d927
WY
4546 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4547 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
7708610b
FF
4548 return -EINVAL;
4549
4550 if (asoc) {
4551 /* Fetch association values. */
d364d927
WY
4552 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4553 params.sack_delay = jiffies_to_msecs(
7708610b 4554 asoc->sackdelay);
d364d927
WY
4555 params.sack_freq = asoc->sackfreq;
4556
4557 } else {
4558 params.sack_delay = 0;
4559 params.sack_freq = 1;
4560 }
7708610b
FF
4561 } else {
4562 /* Fetch socket values. */
d364d927
WY
4563 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4564 params.sack_delay = sp->sackdelay;
4565 params.sack_freq = sp->sackfreq;
4566 } else {
4567 params.sack_delay = 0;
4568 params.sack_freq = 1;
4569 }
7708610b
FF
4570 }
4571
4572 if (copy_to_user(optval, &params, len))
4573 return -EFAULT;
4574
4575 if (put_user(len, optlen))
4576 return -EFAULT;
4577
4578 return 0;
4579}
4580
1da177e4
LT
4581/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4582 *
4583 * Applications can specify protocol parameters for the default association
4584 * initialization. The option name argument to setsockopt() and getsockopt()
4585 * is SCTP_INITMSG.
4586 *
4587 * Setting initialization parameters is effective only on an unconnected
4588 * socket (for UDP-style sockets only future associations are effected
4589 * by the change). With TCP-style sockets, this option is inherited by
4590 * sockets derived from a listener socket.
4591 */
4592static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4593{
408f22e8 4594 if (len < sizeof(struct sctp_initmsg))
1da177e4 4595 return -EINVAL;
408f22e8
NH
4596 len = sizeof(struct sctp_initmsg);
4597 if (put_user(len, optlen))
4598 return -EFAULT;
1da177e4
LT
4599 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4600 return -EFAULT;
4601 return 0;
4602}
4603