sctp: pass a kernel pointer to __sctp_setsockopt_connectx
[linux-2.6-block.git] / net / sctp / socket.c
CommitLineData
47505b8b 1// SPDX-License-Identifier: GPL-2.0-or-later
60c778b2 2/* SCTP kernel implementation
1da177e4
LT
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
7 * Copyright (c) 2001-2002 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
9 *
60c778b2 10 * This file is part of the SCTP kernel implementation
1da177e4
LT
11 *
12 * These functions interface with the sockets layer to implement the
13 * SCTP Extensions for the Sockets API.
14 *
15 * Note that the descriptions from the specification are USER level
16 * functions--this file is the functions which populate the struct proto
17 * for SCTP which is the BOTTOM of the sockets interface.
18 *
1da177e4
LT
19 * Please send any bug reports or fixes you make to the
20 * email address(es):
91705c61 21 * lksctp developers <linux-sctp@vger.kernel.org>
1da177e4 22 *
1da177e4
LT
23 * Written or modified by:
24 * La Monte H.P. Yarroll <piggy@acm.org>
25 * Narasimha Budihal <narsi@refcode.org>
26 * Karl Knutson <karl@athena.chicago.il.us>
27 * Jon Grimm <jgrimm@us.ibm.com>
28 * Xingang Guo <xingang.guo@intel.com>
29 * Daisy Chang <daisyc@us.ibm.com>
30 * Sridhar Samudrala <samudrala@us.ibm.com>
31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32 * Ardelle Fan <ardelle.fan@intel.com>
33 * Ryan Layer <rmlayer@us.ibm.com>
34 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35 * Kevin Gao <kevin.gao@intel.com>
1da177e4
LT
36 */
37
145ce502
JP
38#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
5821c769 40#include <crypto/hash.h>
1da177e4
LT
41#include <linux/types.h>
42#include <linux/kernel.h>
43#include <linux/wait.h>
44#include <linux/time.h>
3f07c014 45#include <linux/sched/signal.h>
1da177e4 46#include <linux/ip.h>
4fc268d2 47#include <linux/capability.h>
1da177e4
LT
48#include <linux/fcntl.h>
49#include <linux/poll.h>
50#include <linux/init.h>
5a0e3ad6 51#include <linux/slab.h>
56b31d1c 52#include <linux/file.h>
ffd59393 53#include <linux/compat.h>
0eb71a9d 54#include <linux/rhashtable.h>
1da177e4
LT
55
56#include <net/ip.h>
57#include <net/icmp.h>
58#include <net/route.h>
59#include <net/ipv6.h>
60#include <net/inet_common.h>
8465a5fc 61#include <net/busy_poll.h>
1da177e4
LT
62
63#include <linux/socket.h> /* for sa_family_t */
bc3b2d7f 64#include <linux/export.h>
1da177e4
LT
65#include <net/sock.h>
66#include <net/sctp/sctp.h>
67#include <net/sctp/sm.h>
13aa8770 68#include <net/sctp/stream_sched.h>
1da177e4 69
1da177e4 70/* Forward declarations for internal helper functions. */
cd305c74 71static bool sctp_writeable(struct sock *sk);
1da177e4 72static void sctp_wfree(struct sk_buff *skb);
cea0cc80 73static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
a0ff6600 74 size_t msg_len);
26ac8e5f 75static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
1da177e4
LT
76static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77static int sctp_wait_for_accept(struct sock *sk, long timeo);
78static void sctp_wait_for_close(struct sock *sk, long timeo);
0a2fbac1 79static void sctp_destruct_sock(struct sock *sk);
1da177e4
LT
80static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81 union sctp_addr *addr, int len);
82static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86static int sctp_send_asconf(struct sctp_association *asoc,
87 struct sctp_chunk *chunk);
88static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89static int sctp_autobind(struct sock *sk);
89664c62
XL
90static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91 struct sctp_association *assoc,
92 enum sctp_socket_type type);
1da177e4 93
06044751 94static unsigned long sctp_memory_pressure;
8d987e5c 95static atomic_long_t sctp_memory_allocated;
1748376b 96struct percpu_counter sctp_sockets_allocated;
4d93df0a 97
5c52ba17 98static void sctp_enter_memory_pressure(struct sock *sk)
4d93df0a
NH
99{
100 sctp_memory_pressure = 1;
101}
102
103
1da177e4
LT
104/* Get the sndbuf space available at the time on the association. */
105static inline int sctp_wspace(struct sctp_association *asoc)
106{
cd305c74 107 struct sock *sk = asoc->base.sk;
1da177e4 108
cd305c74
XL
109 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110 : sk_stream_wspace(sk);
1da177e4
LT
111}
112
113/* Increment the used sndbuf space count of the corresponding association by
114 * the size of the outgoing data chunk.
115 * Also, set the skb destructor for sndbuf accounting later.
116 *
117 * Since it is always 1-1 between chunk and skb, and also a new skb is always
118 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119 * destructor in the data chunk skb for the purpose of the sndbuf space
120 * tracking.
121 */
122static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
123{
124 struct sctp_association *asoc = chunk->asoc;
125 struct sock *sk = asoc->base.sk;
126
127 /* The sndbuf space is tracked per association. */
128 sctp_association_hold(asoc);
129
1b1e0bc9
XL
130 if (chunk->shkey)
131 sctp_auth_shkey_hold(chunk->shkey);
132
4eb701df
NH
133 skb_set_owner_w(chunk->skb, sk);
134
1da177e4
LT
135 chunk->skb->destructor = sctp_wfree;
136 /* Save the chunk pointer in skb for sctp_wfree to use later. */
f869c912 137 skb_shinfo(chunk->skb)->destructor_arg = chunk;
1da177e4 138
14afee4b 139 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
605c0ac1
XL
140 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
3ab224be 142 sk_mem_charge(sk, chunk->skb->truesize);
1da177e4
LT
143}
144
d04adf1b
XL
145static void sctp_clear_owner_w(struct sctp_chunk *chunk)
146{
147 skb_orphan(chunk->skb);
148}
149
5c3e82fe
QH
150#define traverse_and_process() \
151do { \
152 msg = chunk->msg; \
153 if (msg == prev_msg) \
154 continue; \
155 list_for_each_entry(c, &msg->chunks, frag_list) { \
156 if ((clear && asoc->base.sk == c->skb->sk) || \
157 (!clear && asoc->base.sk != c->skb->sk)) \
158 cb(c); \
159 } \
160 prev_msg = msg; \
161} while (0)
162
d04adf1b 163static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
5c3e82fe 164 bool clear,
d04adf1b
XL
165 void (*cb)(struct sctp_chunk *))
166
167{
5c3e82fe 168 struct sctp_datamsg *msg, *prev_msg = NULL;
d04adf1b 169 struct sctp_outq *q = &asoc->outqueue;
5c3e82fe 170 struct sctp_chunk *chunk, *c;
d04adf1b 171 struct sctp_transport *t;
d04adf1b
XL
172
173 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
5c3e82fe 175 traverse_and_process();
d04adf1b 176
a8dd3979 177 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
5c3e82fe 178 traverse_and_process();
d04adf1b 179
a8dd3979 180 list_for_each_entry(chunk, &q->sacked, transmitted_list)
5c3e82fe 181 traverse_and_process();
d04adf1b 182
a8dd3979 183 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
5c3e82fe 184 traverse_and_process();
d04adf1b
XL
185
186 list_for_each_entry(chunk, &q->out_chunk_list, list)
5c3e82fe 187 traverse_and_process();
d04adf1b
XL
188}
189
13228238
XL
190static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191 void (*cb)(struct sk_buff *, struct sock *))
192
193{
194 struct sk_buff *skb, *tmp;
195
196 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
197 cb(skb, sk);
198
199 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
200 cb(skb, sk);
201
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
203 cb(skb, sk);
204}
205
1da177e4
LT
206/* Verify that this is a valid address. */
207static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
208 int len)
209{
210 struct sctp_af *af;
211
212 /* Verify basic sockaddr. */
213 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
214 if (!af)
215 return -EINVAL;
216
217 /* Is this a valid SCTP address? */
5636bef7 218 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
1da177e4
LT
219 return -EINVAL;
220
221 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
222 return -EINVAL;
223
224 return 0;
225}
226
227/* Look up the association by its id. If this is not a UDP-style
228 * socket, the ID field is always ignored.
229 */
230struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
231{
232 struct sctp_association *asoc = NULL;
233
234 /* If this is not a UDP-style socket, assoc id should be ignored. */
235 if (!sctp_style(sk, UDP)) {
236 /* Return NULL if the socket state is not ESTABLISHED. It
237 * could be a TCP-style listening socket or a socket which
238 * hasn't yet called connect() to establish an association.
239 */
e5b13f34 240 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
1da177e4
LT
241 return NULL;
242
243 /* Get the first and the only association from the list. */
244 if (!list_empty(&sctp_sk(sk)->ep->asocs))
245 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246 struct sctp_association, asocs);
247 return asoc;
248 }
249
250 /* Otherwise this is a UDP-style socket. */
80df2704 251 if (id <= SCTP_ALL_ASSOC)
1da177e4
LT
252 return NULL;
253
254 spin_lock_bh(&sctp_assocs_id_lock);
255 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
b336deca
MRL
256 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
257 asoc = NULL;
1da177e4
LT
258 spin_unlock_bh(&sctp_assocs_id_lock);
259
1da177e4
LT
260 return asoc;
261}
262
263/* Look up the transport from an address and an assoc id. If both address and
264 * id are specified, the associations matching the address and the id should be
265 * the same.
266 */
267static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
268 struct sockaddr_storage *addr,
269 sctp_assoc_t id)
270{
271 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
6f29a130 272 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
1da177e4 273 union sctp_addr *laddr = (union sctp_addr *)addr;
6f29a130
XL
274 struct sctp_transport *transport;
275
912964ea 276 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
6f29a130 277 return NULL;
1da177e4 278
1da177e4 279 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
cd4ff034 280 laddr,
1da177e4 281 &transport);
1da177e4
LT
282
283 if (!addr_asoc)
284 return NULL;
285
286 id_asoc = sctp_id2assoc(sk, id);
287 if (id_asoc && (id_asoc != addr_asoc))
288 return NULL;
289
299ee123 290 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
1da177e4
LT
291 (union sctp_addr *)addr);
292
293 return transport;
294}
295
296/* API 3.1.2 bind() - UDP Style Syntax
297 * The syntax of bind() is,
298 *
299 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
300 *
301 * sd - the socket descriptor returned by socket().
302 * addr - the address structure (struct sockaddr_in or struct
303 * sockaddr_in6 [RFC 2553]),
304 * addr_len - the size of the address structure.
305 */
dda91928 306static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
1da177e4
LT
307{
308 int retval = 0;
309
048ed4b6 310 lock_sock(sk);
1da177e4 311
bb33381d
DB
312 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
313 addr, addr_len);
1da177e4
LT
314
315 /* Disallow binding twice. */
316 if (!sctp_sk(sk)->ep->base.bind_addr.port)
3f7a87d2 317 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
1da177e4
LT
318 addr_len);
319 else
320 retval = -EINVAL;
321
048ed4b6 322 release_sock(sk);
1da177e4
LT
323
324 return retval;
325}
326
8e2ef6ab 327static int sctp_get_port_local(struct sock *, union sctp_addr *);
1da177e4
LT
328
329/* Verify this is a valid sockaddr. */
330static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
331 union sctp_addr *addr, int len)
332{
333 struct sctp_af *af;
334
335 /* Check minimum size. */
336 if (len < sizeof (struct sockaddr))
337 return NULL;
338
c5006b8a
XL
339 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
340 return NULL;
341
81e98370
ED
342 if (addr->sa.sa_family == AF_INET6) {
343 if (len < SIN6_LEN_RFC2133)
344 return NULL;
345 /* V4 mapped address are really of AF_INET family */
346 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
347 !opt->pf->af_supported(AF_INET, opt))
348 return NULL;
349 }
1da177e4
LT
350
351 /* If we get this far, af is valid. */
352 af = sctp_get_af_specific(addr->sa.sa_family);
353
354 if (len < af->sockaddr_len)
355 return NULL;
356
357 return af;
358}
359
360/* Bind a local address either to an endpoint or to an association. */
dda91928 361static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
1da177e4 362{
3594698a 363 struct net *net = sock_net(sk);
1da177e4
LT
364 struct sctp_sock *sp = sctp_sk(sk);
365 struct sctp_endpoint *ep = sp->ep;
366 struct sctp_bind_addr *bp = &ep->base.bind_addr;
367 struct sctp_af *af;
368 unsigned short snum;
369 int ret = 0;
370
1da177e4
LT
371 /* Common sockaddr verification. */
372 af = sctp_sockaddr_af(sp, addr, len);
3f7a87d2 373 if (!af) {
bb33381d
DB
374 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
375 __func__, sk, addr, len);
1da177e4 376 return -EINVAL;
3f7a87d2
FF
377 }
378
379 snum = ntohs(addr->v4.sin_port);
380
bb33381d
DB
381 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
382 __func__, sk, &addr->sa, bp->port, snum, len);
1da177e4
LT
383
384 /* PF specific bind() address verification. */
385 if (!sp->pf->bind_verify(sp, addr))
386 return -EADDRNOTAVAIL;
387
8b358056
VY
388 /* We must either be unbound, or bind to the same port.
389 * It's OK to allow 0 ports if we are already bound.
390 * We'll just inhert an already bound port in this case
391 */
392 if (bp->port) {
393 if (!snum)
394 snum = bp->port;
395 else if (snum != bp->port) {
bb33381d
DB
396 pr_debug("%s: new port %d doesn't match existing port "
397 "%d\n", __func__, snum, bp->port);
8b358056
VY
398 return -EINVAL;
399 }
1da177e4
LT
400 }
401
82f31ebf 402 if (snum && inet_port_requires_bind_service(net, snum) &&
3594698a 403 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1da177e4
LT
404 return -EACCES;
405
4e54064e
VY
406 /* See if the address matches any of the addresses we may have
407 * already bound before checking against other endpoints.
408 */
409 if (sctp_bind_addr_match(bp, addr, sp))
410 return -EINVAL;
411
1da177e4
LT
412 /* Make sure we are allowed to bind here.
413 * The function sctp_get_port_local() does duplicate address
414 * detection.
415 */
2772b495 416 addr->v4.sin_port = htons(snum);
e0e4b8de 417 if (sctp_get_port_local(sk, addr))
4e54064e 418 return -EADDRINUSE;
1da177e4
LT
419
420 /* Refresh ephemeral port. */
421 if (!bp->port)
c720c7e8 422 bp->port = inet_sk(sk)->inet_num;
1da177e4 423
559cf710
VY
424 /* Add the address to the bind address list.
425 * Use GFP_ATOMIC since BHs will be disabled.
426 */
133800d1
MRL
427 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
428 SCTP_ADDR_SRC, GFP_ATOMIC);
1da177e4 429
29b99f54
MW
430 if (ret) {
431 sctp_put_port(sk);
432 return ret;
1da177e4 433 }
29b99f54
MW
434 /* Copy back into socket for getsockname() use. */
435 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
436 sp->pf->to_sk_saddr(addr, sk);
1da177e4
LT
437
438 return ret;
439}
440
441 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
442 *
d808ad9a 443 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
1da177e4 444 * at any one time. If a sender, after sending an ASCONF chunk, decides
d808ad9a 445 * it needs to transfer another ASCONF Chunk, it MUST wait until the
1da177e4 446 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
d808ad9a
YH
447 * subsequent ASCONF. Note this restriction binds each side, so at any
448 * time two ASCONF may be in-transit on any given association (one sent
1da177e4
LT
449 * from each endpoint).
450 */
451static int sctp_send_asconf(struct sctp_association *asoc,
452 struct sctp_chunk *chunk)
453{
4e7696d9 454 int retval = 0;
1da177e4
LT
455
456 /* If there is an outstanding ASCONF chunk, queue it for later
457 * transmission.
d808ad9a 458 */
1da177e4 459 if (asoc->addip_last_asconf) {
79af02c2 460 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
d808ad9a 461 goto out;
1da177e4
LT
462 }
463
464 /* Hold the chunk until an ASCONF_ACK is received. */
465 sctp_chunk_hold(chunk);
4e7696d9 466 retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
1da177e4
LT
467 if (retval)
468 sctp_chunk_free(chunk);
469 else
470 asoc->addip_last_asconf = chunk;
471
472out:
473 return retval;
474}
475
476/* Add a list of addresses as bind addresses to local endpoint or
477 * association.
478 *
479 * Basically run through each address specified in the addrs/addrcnt
480 * array/length pair, determine if it is IPv6 or IPv4 and call
481 * sctp_do_bind() on it.
482 *
483 * If any of them fails, then the operation will be reversed and the
484 * ones that were added will be removed.
485 *
486 * Only sctp_setsockopt_bindx() is supposed to call this function.
487 */
04675210 488static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
1da177e4
LT
489{
490 int cnt;
491 int retval = 0;
492 void *addr_buf;
493 struct sockaddr *sa_addr;
494 struct sctp_af *af;
495
bb33381d
DB
496 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
497 addrs, addrcnt);
1da177e4
LT
498
499 addr_buf = addrs;
500 for (cnt = 0; cnt < addrcnt; cnt++) {
501 /* The list may contain either IPv4 or IPv6 address;
502 * determine the address length for walking thru the list.
503 */
ea110733 504 sa_addr = addr_buf;
1da177e4
LT
505 af = sctp_get_af_specific(sa_addr->sa_family);
506 if (!af) {
507 retval = -EINVAL;
508 goto err_bindx_add;
509 }
510
d808ad9a 511 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
1da177e4
LT
512 af->sockaddr_len);
513
514 addr_buf += af->sockaddr_len;
515
516err_bindx_add:
517 if (retval < 0) {
518 /* Failed. Cleanup the ones that have been added */
519 if (cnt > 0)
520 sctp_bindx_rem(sk, addrs, cnt);
521 return retval;
522 }
523 }
524
525 return retval;
526}
527
528/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
529 * associations that are part of the endpoint indicating that a list of local
530 * addresses are added to the endpoint.
531 *
d808ad9a 532 * If any of the addresses is already in the bind address list of the
1da177e4
LT
533 * association, we do not send the chunk for that association. But it will not
534 * affect other associations.
535 *
536 * Only sctp_setsockopt_bindx() is supposed to call this function.
537 */
d808ad9a 538static int sctp_send_asconf_add_ip(struct sock *sk,
1da177e4
LT
539 struct sockaddr *addrs,
540 int addrcnt)
541{
542 struct sctp_sock *sp;
543 struct sctp_endpoint *ep;
544 struct sctp_association *asoc;
545 struct sctp_bind_addr *bp;
546 struct sctp_chunk *chunk;
547 struct sctp_sockaddr_entry *laddr;
548 union sctp_addr *addr;
dc022a98 549 union sctp_addr saveaddr;
1da177e4
LT
550 void *addr_buf;
551 struct sctp_af *af;
1da177e4
LT
552 struct list_head *p;
553 int i;
554 int retval = 0;
555
1da177e4
LT
556 sp = sctp_sk(sk);
557 ep = sp->ep;
558
4e27428f
XL
559 if (!ep->asconf_enable)
560 return retval;
561
bb33381d
DB
562 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
563 __func__, sk, addrs, addrcnt);
1da177e4 564
9dbc15f0 565 list_for_each_entry(asoc, &ep->asocs, asocs) {
1da177e4
LT
566 if (!asoc->peer.asconf_capable)
567 continue;
568
569 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
570 continue;
571
572 if (!sctp_state(asoc, ESTABLISHED))
573 continue;
574
575 /* Check if any address in the packed array of addresses is
d808ad9a
YH
576 * in the bind address list of the association. If so,
577 * do not send the asconf chunk to its peer, but continue with
1da177e4
LT
578 * other associations.
579 */
580 addr_buf = addrs;
581 for (i = 0; i < addrcnt; i++) {
ea110733 582 addr = addr_buf;
1da177e4
LT
583 af = sctp_get_af_specific(addr->v4.sin_family);
584 if (!af) {
585 retval = -EINVAL;
586 goto out;
587 }
588
589 if (sctp_assoc_lookup_laddr(asoc, addr))
590 break;
591
592 addr_buf += af->sockaddr_len;
593 }
594 if (i < addrcnt)
595 continue;
596
559cf710
VY
597 /* Use the first valid address in bind addr list of
598 * association as Address Parameter of ASCONF CHUNK.
1da177e4 599 */
1da177e4
LT
600 bp = &asoc->base.bind_addr;
601 p = bp->address_list.next;
602 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
5ae955cf 603 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
1da177e4
LT
604 addrcnt, SCTP_PARAM_ADD_IP);
605 if (!chunk) {
606 retval = -ENOMEM;
607 goto out;
608 }
609
dc022a98
SS
610 /* Add the new addresses to the bind address list with
611 * use_as_src set to 0.
1da177e4 612 */
dc022a98
SS
613 addr_buf = addrs;
614 for (i = 0; i < addrcnt; i++) {
ea110733 615 addr = addr_buf;
dc022a98
SS
616 af = sctp_get_af_specific(addr->v4.sin_family);
617 memcpy(&saveaddr, addr, af->sockaddr_len);
f57d96b2 618 retval = sctp_add_bind_addr(bp, &saveaddr,
133800d1 619 sizeof(saveaddr),
f57d96b2 620 SCTP_ADDR_NEW, GFP_ATOMIC);
dc022a98
SS
621 addr_buf += af->sockaddr_len;
622 }
8a07eb0a
MH
623 if (asoc->src_out_of_asoc_ok) {
624 struct sctp_transport *trans;
625
626 list_for_each_entry(trans,
627 &asoc->peer.transport_addr_list, transports) {
8a07eb0a
MH
628 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
629 2*asoc->pathmtu, 4380));
630 trans->ssthresh = asoc->peer.i.a_rwnd;
631 trans->rto = asoc->rto_initial;
196d6759 632 sctp_max_rto(asoc, trans);
8a07eb0a 633 trans->rtt = trans->srtt = trans->rttvar = 0;
6e91b578 634 /* Clear the source and route cache */
8a07eb0a 635 sctp_transport_route(trans, NULL,
6e91b578 636 sctp_sk(asoc->base.sk));
8a07eb0a
MH
637 }
638 }
639 retval = sctp_send_asconf(asoc, chunk);
1da177e4
LT
640 }
641
642out:
643 return retval;
644}
645
646/* Remove a list of addresses from bind addresses list. Do not remove the
647 * last address.
648 *
649 * Basically run through each address specified in the addrs/addrcnt
650 * array/length pair, determine if it is IPv6 or IPv4 and call
651 * sctp_del_bind() on it.
652 *
653 * If any of them fails, then the operation will be reversed and the
654 * ones that were removed will be added back.
655 *
656 * At least one address has to be left; if only one address is
657 * available, the operation will return -EBUSY.
658 *
659 * Only sctp_setsockopt_bindx() is supposed to call this function.
660 */
04675210 661static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
1da177e4
LT
662{
663 struct sctp_sock *sp = sctp_sk(sk);
664 struct sctp_endpoint *ep = sp->ep;
665 int cnt;
666 struct sctp_bind_addr *bp = &ep->base.bind_addr;
667 int retval = 0;
1da177e4 668 void *addr_buf;
c9a08505 669 union sctp_addr *sa_addr;
1da177e4
LT
670 struct sctp_af *af;
671
bb33381d
DB
672 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
673 __func__, sk, addrs, addrcnt);
1da177e4
LT
674
675 addr_buf = addrs;
676 for (cnt = 0; cnt < addrcnt; cnt++) {
677 /* If the bind address list is empty or if there is only one
678 * bind address, there is nothing more to be removed (we need
679 * at least one address here).
680 */
681 if (list_empty(&bp->address_list) ||
682 (sctp_list_single_entry(&bp->address_list))) {
683 retval = -EBUSY;
684 goto err_bindx_rem;
685 }
686
ea110733 687 sa_addr = addr_buf;
c9a08505 688 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1da177e4
LT
689 if (!af) {
690 retval = -EINVAL;
691 goto err_bindx_rem;
692 }
0304ff8a
PG
693
694 if (!af->addr_valid(sa_addr, sp, NULL)) {
695 retval = -EADDRNOTAVAIL;
696 goto err_bindx_rem;
697 }
698
ee9cbaca
VY
699 if (sa_addr->v4.sin_port &&
700 sa_addr->v4.sin_port != htons(bp->port)) {
1da177e4
LT
701 retval = -EINVAL;
702 goto err_bindx_rem;
703 }
704
ee9cbaca
VY
705 if (!sa_addr->v4.sin_port)
706 sa_addr->v4.sin_port = htons(bp->port);
707
1da177e4
LT
708 /* FIXME - There is probably a need to check if sk->sk_saddr and
709 * sk->sk_rcv_addr are currently set to one of the addresses to
710 * be removed. This is something which needs to be looked into
711 * when we are fixing the outstanding issues with multi-homing
712 * socket routing and failover schemes. Refer to comments in
713 * sctp_do_bind(). -daisy
714 */
0ed90fb0 715 retval = sctp_del_bind_addr(bp, sa_addr);
1da177e4
LT
716
717 addr_buf += af->sockaddr_len;
718err_bindx_rem:
719 if (retval < 0) {
720 /* Failed. Add the ones that has been removed back */
721 if (cnt > 0)
722 sctp_bindx_add(sk, addrs, cnt);
723 return retval;
724 }
725 }
726
727 return retval;
728}
729
730/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
731 * the associations that are part of the endpoint indicating that a list of
732 * local addresses are removed from the endpoint.
733 *
d808ad9a 734 * If any of the addresses is already in the bind address list of the
1da177e4
LT
735 * association, we do not send the chunk for that association. But it will not
736 * affect other associations.
737 *
738 * Only sctp_setsockopt_bindx() is supposed to call this function.
739 */
740static int sctp_send_asconf_del_ip(struct sock *sk,
741 struct sockaddr *addrs,
742 int addrcnt)
743{
744 struct sctp_sock *sp;
745 struct sctp_endpoint *ep;
746 struct sctp_association *asoc;
dc022a98 747 struct sctp_transport *transport;
1da177e4
LT
748 struct sctp_bind_addr *bp;
749 struct sctp_chunk *chunk;
750 union sctp_addr *laddr;
751 void *addr_buf;
752 struct sctp_af *af;
dc022a98 753 struct sctp_sockaddr_entry *saddr;
1da177e4
LT
754 int i;
755 int retval = 0;
8a07eb0a 756 int stored = 0;
1da177e4 757
8a07eb0a 758 chunk = NULL;
1da177e4
LT
759 sp = sctp_sk(sk);
760 ep = sp->ep;
761
4e27428f
XL
762 if (!ep->asconf_enable)
763 return retval;
764
bb33381d
DB
765 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
766 __func__, sk, addrs, addrcnt);
1da177e4 767
9dbc15f0 768 list_for_each_entry(asoc, &ep->asocs, asocs) {
1da177e4
LT
769
770 if (!asoc->peer.asconf_capable)
771 continue;
772
773 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
774 continue;
775
776 if (!sctp_state(asoc, ESTABLISHED))
777 continue;
778
779 /* Check if any address in the packed array of addresses is
d808ad9a 780 * not present in the bind address list of the association.
1da177e4
LT
781 * If so, do not send the asconf chunk to its peer, but
782 * continue with other associations.
783 */
784 addr_buf = addrs;
785 for (i = 0; i < addrcnt; i++) {
ea110733 786 laddr = addr_buf;
1da177e4
LT
787 af = sctp_get_af_specific(laddr->v4.sin_family);
788 if (!af) {
789 retval = -EINVAL;
790 goto out;
791 }
792
793 if (!sctp_assoc_lookup_laddr(asoc, laddr))
794 break;
795
796 addr_buf += af->sockaddr_len;
797 }
798 if (i < addrcnt)
799 continue;
800
801 /* Find one address in the association's bind address list
802 * that is not in the packed array of addresses. This is to
803 * make sure that we do not delete all the addresses in the
804 * association.
805 */
1da177e4
LT
806 bp = &asoc->base.bind_addr;
807 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
808 addrcnt, sp);
8a07eb0a
MH
809 if ((laddr == NULL) && (addrcnt == 1)) {
810 if (asoc->asconf_addr_del_pending)
811 continue;
812 asoc->asconf_addr_del_pending =
813 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
6d65e5ee
MH
814 if (asoc->asconf_addr_del_pending == NULL) {
815 retval = -ENOMEM;
816 goto out;
817 }
8a07eb0a
MH
818 asoc->asconf_addr_del_pending->sa.sa_family =
819 addrs->sa_family;
820 asoc->asconf_addr_del_pending->v4.sin_port =
821 htons(bp->port);
822 if (addrs->sa_family == AF_INET) {
823 struct sockaddr_in *sin;
824
825 sin = (struct sockaddr_in *)addrs;
826 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
827 } else if (addrs->sa_family == AF_INET6) {
828 struct sockaddr_in6 *sin6;
829
830 sin6 = (struct sockaddr_in6 *)addrs;
4e3fd7a0 831 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
8a07eb0a 832 }
bb33381d
DB
833
834 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
835 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
836 asoc->asconf_addr_del_pending);
837
8a07eb0a
MH
838 asoc->src_out_of_asoc_ok = 1;
839 stored = 1;
840 goto skip_mkasconf;
841 }
1da177e4 842
88362ad8
DB
843 if (laddr == NULL)
844 return -EINVAL;
845
559cf710
VY
846 /* We do not need RCU protection throughout this loop
847 * because this is done under a socket lock from the
848 * setsockopt call.
849 */
1da177e4
LT
850 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
851 SCTP_PARAM_DEL_IP);
852 if (!chunk) {
853 retval = -ENOMEM;
854 goto out;
855 }
856
8a07eb0a 857skip_mkasconf:
dc022a98
SS
858 /* Reset use_as_src flag for the addresses in the bind address
859 * list that are to be deleted.
860 */
dc022a98
SS
861 addr_buf = addrs;
862 for (i = 0; i < addrcnt; i++) {
ea110733 863 laddr = addr_buf;
dc022a98 864 af = sctp_get_af_specific(laddr->v4.sin_family);
559cf710 865 list_for_each_entry(saddr, &bp->address_list, list) {
5f242a13 866 if (sctp_cmp_addr_exact(&saddr->a, laddr))
f57d96b2 867 saddr->state = SCTP_ADDR_DEL;
dc022a98
SS
868 }
869 addr_buf += af->sockaddr_len;
870 }
1da177e4 871
dc022a98
SS
872 /* Update the route and saddr entries for all the transports
873 * as some of the addresses in the bind address list are
874 * about to be deleted and cannot be used as source addresses.
1da177e4 875 */
9dbc15f0
RD
876 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
877 transports) {
dc022a98
SS
878 sctp_transport_route(transport, NULL,
879 sctp_sk(asoc->base.sk));
880 }
881
8a07eb0a
MH
882 if (stored)
883 /* We don't need to transmit ASCONF */
884 continue;
dc022a98 885 retval = sctp_send_asconf(asoc, chunk);
1da177e4
LT
886 }
887out:
888 return retval;
889}
890
9f7d653b
MH
891/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
892int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
893{
894 struct sock *sk = sctp_opt2sk(sp);
895 union sctp_addr *addr;
896 struct sctp_af *af;
897
898 /* It is safe to write port space in caller. */
899 addr = &addrw->a;
900 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
901 af = sctp_get_af_specific(addr->sa.sa_family);
902 if (!af)
903 return -EINVAL;
904 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
905 return -EINVAL;
906
907 if (addrw->state == SCTP_ADDR_NEW)
908 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
909 else
910 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
911}
912
1da177e4
LT
913/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
914 *
915 * API 8.1
916 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
917 * int flags);
918 *
919 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
920 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
921 * or IPv6 addresses.
922 *
923 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
924 * Section 3.1.2 for this usage.
925 *
926 * addrs is a pointer to an array of one or more socket addresses. Each
927 * address is contained in its appropriate structure (i.e. struct
928 * sockaddr_in or struct sockaddr_in6) the family of the address type
23c435f7 929 * must be used to distinguish the address length (note that this
1da177e4
LT
930 * representation is termed a "packed array" of addresses). The caller
931 * specifies the number of addresses in the array with addrcnt.
932 *
933 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
934 * -1, and sets errno to the appropriate error code.
935 *
936 * For SCTP, the port given in each socket address must be the same, or
937 * sctp_bindx() will fail, setting errno to EINVAL.
938 *
939 * The flags parameter is formed from the bitwise OR of zero or more of
940 * the following currently defined flags:
941 *
942 * SCTP_BINDX_ADD_ADDR
943 *
944 * SCTP_BINDX_REM_ADDR
945 *
946 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
947 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
948 * addresses from the association. The two flags are mutually exclusive;
949 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
950 * not remove all addresses from an association; sctp_bindx() will
951 * reject such an attempt with EINVAL.
952 *
953 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
954 * additional addresses with an endpoint after calling bind(). Or use
955 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
956 * socket is associated with so that no new association accepted will be
957 * associated with those addresses. If the endpoint supports dynamic
958 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
959 * endpoint to send the appropriate message to the peer to change the
960 * peers address lists.
961 *
962 * Adding and removing addresses from a connected association is
963 * optional functionality. Implementations that do not support this
964 * functionality should return EOPNOTSUPP.
965 *
966 * Basically do nothing but copying the addresses from user to kernel
967 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
3f7a87d2
FF
968 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
969 * from userspace.
1da177e4 970 *
1da177e4
LT
971 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
972 * it.
973 *
974 * sk The sk of the socket
05bfd366 975 * addrs The pointer to the addresses
1da177e4
LT
976 * addrssize Size of the addrs buffer
977 * op Operation to perform (add or remove, see the flags of
978 * sctp_bindx)
979 *
980 * Returns 0 if ok, <0 errno code on error.
981 */
8c7517f5
CH
982static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs,
983 int addrs_size, int op)
1da177e4 984{
1da177e4
LT
985 int err;
986 int addrcnt = 0;
987 int walk_size = 0;
988 struct sockaddr *sa_addr;
05bfd366 989 void *addr_buf = addrs;
1da177e4
LT
990 struct sctp_af *af;
991
bb33381d 992 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
8c7517f5 993 __func__, sk, addr_buf, addrs_size, op);
1da177e4
LT
994
995 if (unlikely(addrs_size <= 0))
996 return -EINVAL;
997
d808ad9a 998 /* Walk through the addrs buffer and count the number of addresses. */
1da177e4 999 while (walk_size < addrs_size) {
05bfd366 1000 if (walk_size + sizeof(sa_family_t) > addrs_size)
d7e0d19a 1001 return -EINVAL;
d7e0d19a 1002
ea110733 1003 sa_addr = addr_buf;
1da177e4
LT
1004 af = sctp_get_af_specific(sa_addr->sa_family);
1005
1006 /* If the address family is not supported or if this address
1007 * causes the address buffer to overflow return EINVAL.
d808ad9a 1008 */
05bfd366 1009 if (!af || (walk_size + af->sockaddr_len) > addrs_size)
1da177e4 1010 return -EINVAL;
1da177e4
LT
1011 addrcnt++;
1012 addr_buf += af->sockaddr_len;
1013 walk_size += af->sockaddr_len;
1014 }
1015
1016 /* Do the work. */
1017 switch (op) {
1018 case SCTP_BINDX_ADD_ADDR:
2277c7cd
RH
1019 /* Allow security module to validate bindx addresses. */
1020 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
05bfd366 1021 addrs, addrs_size);
2277c7cd 1022 if (err)
05bfd366
CH
1023 return err;
1024 err = sctp_bindx_add(sk, addrs, addrcnt);
1da177e4 1025 if (err)
05bfd366
CH
1026 return err;
1027 return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
1da177e4 1028 case SCTP_BINDX_REM_ADDR:
05bfd366 1029 err = sctp_bindx_rem(sk, addrs, addrcnt);
1da177e4 1030 if (err)
05bfd366
CH
1031 return err;
1032 return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
1da177e4
LT
1033
1034 default:
05bfd366 1035 return -EINVAL;
3ff50b79 1036 }
05bfd366 1037}
1da177e4 1038
c0425a42
CH
1039static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
1040 int addrlen)
1041{
1042 int err;
1043
1044 lock_sock(sk);
8c7517f5 1045 err = sctp_setsockopt_bindx(sk, addrs, addrlen, SCTP_BINDX_ADD_ADDR);
c0425a42
CH
1046 release_sock(sk);
1047 return err;
1048}
1049
f26f9951
XL
1050static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
1051 const union sctp_addr *daddr,
1052 const struct sctp_initmsg *init,
1053 struct sctp_transport **tp)
1054{
1055 struct sctp_association *asoc;
1056 struct sock *sk = ep->base.sk;
1057 struct net *net = sock_net(sk);
1058 enum sctp_scope scope;
1059 int err;
1060
1061 if (sctp_endpoint_is_peeled_off(ep, daddr))
1062 return -EADDRNOTAVAIL;
1063
1064 if (!ep->base.bind_addr.port) {
1065 if (sctp_autobind(sk))
1066 return -EAGAIN;
1067 } else {
82f31ebf 1068 if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
f26f9951
XL
1069 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1070 return -EACCES;
1071 }
1072
1073 scope = sctp_scope(daddr);
1074 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1075 if (!asoc)
1076 return -ENOMEM;
1077
1078 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1079 if (err < 0)
1080 goto free;
1081
1082 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1083 if (!*tp) {
1084 err = -ENOMEM;
1085 goto free;
1086 }
1087
1088 if (!init)
1089 return 0;
1090
1091 if (init->sinit_num_ostreams) {
1092 __u16 outcnt = init->sinit_num_ostreams;
1093
1094 asoc->c.sinit_num_ostreams = outcnt;
1095 /* outcnt has been changed, need to re-init stream */
1096 err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
1097 if (err)
1098 goto free;
1099 }
1100
1101 if (init->sinit_max_instreams)
1102 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1103
1104 if (init->sinit_max_attempts)
1105 asoc->max_init_attempts = init->sinit_max_attempts;
1106
1107 if (init->sinit_max_init_timeo)
1108 asoc->max_init_timeo =
1109 msecs_to_jiffies(init->sinit_max_init_timeo);
1110
1111 return 0;
1112free:
1113 sctp_association_free(asoc);
1114 return err;
1115}
1116
a64e59c7
XL
1117static int sctp_connect_add_peer(struct sctp_association *asoc,
1118 union sctp_addr *daddr, int addr_len)
1119{
1120 struct sctp_endpoint *ep = asoc->ep;
1121 struct sctp_association *old;
1122 struct sctp_transport *t;
1123 int err;
1124
1125 err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
1126 if (err)
1127 return err;
1128
1129 old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
1130 if (old && old != asoc)
1131 return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1132 : -EALREADY;
1133
1134 if (sctp_endpoint_is_peeled_off(ep, daddr))
1135 return -EADDRNOTAVAIL;
1136
1137 t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1138 if (!t)
1139 return -ENOMEM;
1140
1141 return 0;
1142}
1143
3f7a87d2
FF
1144/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1145 *
1146 * Common routine for handling connect() and sctp_connectx().
1147 * Connect will come in with just a single address.
1148 */
dd8378b3
XL
1149static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
1150 int addrs_size, int flags, sctp_assoc_t *assoc_id)
3f7a87d2 1151{
dd8378b3
XL
1152 struct sctp_sock *sp = sctp_sk(sk);
1153 struct sctp_endpoint *ep = sp->ep;
3f7a87d2 1154 struct sctp_transport *transport;
a64e59c7 1155 struct sctp_association *asoc;
dd8378b3
XL
1156 void *addr_buf = kaddrs;
1157 union sctp_addr *daddr;
dd8378b3
XL
1158 struct sctp_af *af;
1159 int walk_size, err;
3f7a87d2 1160 long timeo;
3f7a87d2 1161
e5b13f34 1162 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
dd8378b3
XL
1163 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
1164 return -EISCONN;
1165
1166 daddr = addr_buf;
1167 af = sctp_get_af_specific(daddr->sa.sa_family);
1168 if (!af || af->sockaddr_len > addrs_size)
1169 return -EINVAL;
1170
1171 err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
1172 if (err)
1173 return err;
1174
1175 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1176 if (asoc)
1177 return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1178 : -EALREADY;
1179
f26f9951
XL
1180 err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
1181 if (err)
1182 return err;
1183 asoc = transport->asoc;
3f7a87d2 1184
dd8378b3
XL
1185 addr_buf += af->sockaddr_len;
1186 walk_size = af->sockaddr_len;
1187 while (walk_size < addrs_size) {
1188 err = -EINVAL;
1189 if (walk_size + sizeof(sa_family_t) > addrs_size)
3f7a87d2 1190 goto out_free;
d7e0d19a 1191
dd8378b3
XL
1192 daddr = addr_buf;
1193 af = sctp_get_af_specific(daddr->sa.sa_family);
1194 if (!af || af->sockaddr_len + walk_size > addrs_size)
1195 goto out_free;
e4d1feab 1196
dd8378b3 1197 if (asoc->peer.port != ntohs(daddr->v4.sin_port))
3f7a87d2
FF
1198 goto out_free;
1199
a64e59c7 1200 err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
dd8378b3 1201 if (err)
16d00fb7 1202 goto out_free;
3f7a87d2 1203
dd8378b3 1204 addr_buf += af->sockaddr_len;
3f7a87d2
FF
1205 walk_size += af->sockaddr_len;
1206 }
1207
c6ba68a2
VY
1208 /* In case the user of sctp_connectx() wants an association
1209 * id back, assign one now.
1210 */
1211 if (assoc_id) {
1212 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1213 if (err < 0)
1214 goto out_free;
1215 }
1216
f26f9951 1217 err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
dd8378b3 1218 if (err < 0)
3f7a87d2 1219 goto out_free;
3f7a87d2
FF
1220
1221 /* Initialize sk's dport and daddr for getpeername() */
c720c7e8 1222 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
dd8378b3 1223 sp->pf->to_sk_daddr(daddr, sk);
8de8c873 1224 sk->sk_err = 0;
3f7a87d2 1225
7233bc84 1226 if (assoc_id)
88a0a948 1227 *assoc_id = asoc->assoc_id;
2277c7cd 1228
dd8378b3
XL
1229 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1230 return sctp_wait_for_connect(asoc, &timeo);
3f7a87d2
FF
1231
1232out_free:
bb33381d
DB
1233 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1234 __func__, asoc, kaddrs, err);
dd8378b3 1235 sctp_association_free(asoc);
3f7a87d2
FF
1236 return err;
1237}
1238
1239/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1240 *
1241 * API 8.9
88a0a948
VY
1242 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1243 * sctp_assoc_t *asoc);
3f7a87d2
FF
1244 *
1245 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1246 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1247 * or IPv6 addresses.
1248 *
1249 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1250 * Section 3.1.2 for this usage.
1251 *
1252 * addrs is a pointer to an array of one or more socket addresses. Each
1253 * address is contained in its appropriate structure (i.e. struct
1254 * sockaddr_in or struct sockaddr_in6) the family of the address type
1255 * must be used to distengish the address length (note that this
1256 * representation is termed a "packed array" of addresses). The caller
1257 * specifies the number of addresses in the array with addrcnt.
1258 *
88a0a948
VY
1259 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1260 * the association id of the new association. On failure, sctp_connectx()
1261 * returns -1, and sets errno to the appropriate error code. The assoc_id
1262 * is not touched by the kernel.
3f7a87d2
FF
1263 *
1264 * For SCTP, the port given in each socket address must be the same, or
1265 * sctp_connectx() will fail, setting errno to EINVAL.
1266 *
1267 * An application can use sctp_connectx to initiate an association with
1268 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1269 * allows a caller to specify multiple addresses at which a peer can be
1270 * reached. The way the SCTP stack uses the list of addresses to set up
25985edc 1271 * the association is implementation dependent. This function only
3f7a87d2
FF
1272 * specifies that the stack will try to make use of all the addresses in
1273 * the list when needed.
1274 *
1275 * Note that the list of addresses passed in is only used for setting up
1276 * the association. It does not necessarily equal the set of addresses
1277 * the peer uses for the resulting association. If the caller wants to
1278 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1279 * retrieve them after the association has been set up.
1280 *
1281 * Basically do nothing but copying the addresses from user to kernel
1282 * land and invoking either sctp_connectx(). This is used for tunneling
1283 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1284 *
3f7a87d2
FF
1285 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1286 * it.
1287 *
1288 * sk The sk of the socket
ce5b2f89 1289 * addrs The pointer to the addresses
3f7a87d2
FF
1290 * addrssize Size of the addrs buffer
1291 *
88a0a948 1292 * Returns >=0 if ok, <0 errno code on error.
3f7a87d2 1293 */
ce5b2f89
CH
1294static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs,
1295 int addrs_size, sctp_assoc_t *assoc_id)
3f7a87d2 1296{
644fbdea 1297 int err = 0, flags = 0;
3f7a87d2 1298
bb33381d 1299 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
ce5b2f89 1300 __func__, sk, kaddrs, addrs_size);
3f7a87d2 1301
f40f1177
XL
1302 /* make sure the 1st addr's sa_family is accessible later */
1303 if (unlikely(addrs_size < sizeof(sa_family_t)))
3f7a87d2
FF
1304 return -EINVAL;
1305
2277c7cd
RH
1306 /* Allow security module to validate connectx addresses. */
1307 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1308 (struct sockaddr *)kaddrs,
1309 addrs_size);
1310 if (err)
ce5b2f89 1311 return err;
2277c7cd 1312
644fbdea
XL
1313 /* in-kernel sockets don't generally have a file allocated to them
1314 * if all they do is call sock_create_kern().
1315 */
1316 if (sk->sk_socket->file)
1317 flags = sk->sk_socket->file->f_flags;
1318
ce5b2f89 1319 return __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
3f7a87d2
FF
1320}
1321
88a0a948
VY
1322/*
1323 * This is an older interface. It's kept for backward compatibility
1324 * to the option that doesn't provide association id.
1325 */
26ac8e5f 1326static int sctp_setsockopt_connectx_old(struct sock *sk,
ce5b2f89 1327 struct sockaddr *kaddrs,
dda91928 1328 int addrs_size)
88a0a948 1329{
ce5b2f89 1330 return __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, NULL);
88a0a948
VY
1331}
1332
1333/*
1334 * New interface for the API. The since the API is done with a socket
1335 * option, to make it simple we feed back the association id is as a return
1336 * indication to the call. Error is always negative and association id is
1337 * always positive.
1338 */
26ac8e5f 1339static int sctp_setsockopt_connectx(struct sock *sk,
ce5b2f89 1340 struct sockaddr *kaddrs,
dda91928 1341 int addrs_size)
88a0a948
VY
1342{
1343 sctp_assoc_t assoc_id = 0;
1344 int err = 0;
1345
ce5b2f89 1346 err = __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, &assoc_id);
88a0a948
VY
1347
1348 if (err)
1349 return err;
1350 else
1351 return assoc_id;
1352}
1353
c6ba68a2 1354/*
f9c67811
VY
1355 * New (hopefully final) interface for the API.
1356 * We use the sctp_getaddrs_old structure so that use-space library
ffd59393 1357 * can avoid any unnecessary allocations. The only different part
f9c67811 1358 * is that we store the actual length of the address buffer into the
ffd59393 1359 * addrs_num structure member. That way we can re-use the existing
f9c67811 1360 * code.
c6ba68a2 1361 */
ffd59393
DB
1362#ifdef CONFIG_COMPAT
1363struct compat_sctp_getaddrs_old {
1364 sctp_assoc_t assoc_id;
1365 s32 addr_num;
1366 compat_uptr_t addrs; /* struct sockaddr * */
1367};
1368#endif
1369
26ac8e5f 1370static int sctp_getsockopt_connectx3(struct sock *sk, int len,
dda91928
DB
1371 char __user *optval,
1372 int __user *optlen)
c6ba68a2 1373{
f9c67811 1374 struct sctp_getaddrs_old param;
c6ba68a2 1375 sctp_assoc_t assoc_id = 0;
ce5b2f89 1376 struct sockaddr *kaddrs;
c6ba68a2
VY
1377 int err = 0;
1378
ffd59393 1379#ifdef CONFIG_COMPAT
96c0e0a9 1380 if (in_compat_syscall()) {
ffd59393 1381 struct compat_sctp_getaddrs_old param32;
c6ba68a2 1382
ffd59393
DB
1383 if (len < sizeof(param32))
1384 return -EINVAL;
1385 if (copy_from_user(&param32, optval, sizeof(param32)))
1386 return -EFAULT;
f9c67811 1387
ffd59393
DB
1388 param.assoc_id = param32.assoc_id;
1389 param.addr_num = param32.addr_num;
1390 param.addrs = compat_ptr(param32.addrs);
1391 } else
1392#endif
1393 {
1394 if (len < sizeof(param))
1395 return -EINVAL;
1396 if (copy_from_user(&param, optval, sizeof(param)))
1397 return -EFAULT;
1398 }
c6ba68a2 1399
ce5b2f89
CH
1400 kaddrs = memdup_user(param.addrs, param.addr_num);
1401 if (IS_ERR(kaddrs))
1402 return PTR_ERR(kaddrs);
1403
1404 err = __sctp_setsockopt_connectx(sk, kaddrs, param.addr_num, &assoc_id);
1405 kfree(kaddrs);
c6ba68a2
VY
1406 if (err == 0 || err == -EINPROGRESS) {
1407 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1408 return -EFAULT;
1409 if (put_user(sizeof(assoc_id), optlen))
1410 return -EFAULT;
1411 }
1412
1413 return err;
1414}
1415
1da177e4
LT
1416/* API 3.1.4 close() - UDP Style Syntax
1417 * Applications use close() to perform graceful shutdown (as described in
1418 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1419 * by a UDP-style socket.
1420 *
1421 * The syntax is
1422 *
1423 * ret = close(int sd);
1424 *
1425 * sd - the socket descriptor of the associations to be closed.
1426 *
1427 * To gracefully shutdown a specific association represented by the
1428 * UDP-style socket, an application should use the sendmsg() call,
1429 * passing no user data, but including the appropriate flag in the
1430 * ancillary data (see Section xxxx).
1431 *
1432 * If sd in the close() call is a branched-off socket representing only
1433 * one association, the shutdown is performed on that association only.
1434 *
1435 * 4.1.6 close() - TCP Style Syntax
1436 *
1437 * Applications use close() to gracefully close down an association.
1438 *
1439 * The syntax is:
1440 *
1441 * int close(int sd);
1442 *
1443 * sd - the socket descriptor of the association to be closed.
1444 *
1445 * After an application calls close() on a socket descriptor, no further
1446 * socket operations will succeed on that descriptor.
1447 *
1448 * API 7.1.4 SO_LINGER
1449 *
1450 * An application using the TCP-style socket can use this option to
1451 * perform the SCTP ABORT primitive. The linger option structure is:
1452 *
1453 * struct linger {
1454 * int l_onoff; // option on/off
1455 * int l_linger; // linger time
1456 * };
1457 *
1458 * To enable the option, set l_onoff to 1. If the l_linger value is set
1459 * to 0, calling close() is the same as the ABORT primitive. If the
1460 * value is set to a negative value, the setsockopt() call will return
1461 * an error. If the value is set to a positive value linger_time, the
1462 * close() can be blocked for at most linger_time ms. If the graceful
1463 * shutdown phase does not finish during this period, close() will
1464 * return but the graceful shutdown phase continues in the system.
1465 */
dda91928 1466static void sctp_close(struct sock *sk, long timeout)
1da177e4 1467{
55e26eb9 1468 struct net *net = sock_net(sk);
1da177e4
LT
1469 struct sctp_endpoint *ep;
1470 struct sctp_association *asoc;
1471 struct list_head *pos, *temp;
cd4fcc70 1472 unsigned int data_was_unread;
1da177e4 1473
bb33381d 1474 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1da177e4 1475
6dfe4b97 1476 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1da177e4 1477 sk->sk_shutdown = SHUTDOWN_MASK;
cbabf463 1478 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1da177e4
LT
1479
1480 ep = sctp_sk(sk)->ep;
1481
cd4fcc70
TG
1482 /* Clean up any skbs sitting on the receive queue. */
1483 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1484 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1485
61c9fed4 1486 /* Walk all associations on an endpoint. */
1da177e4
LT
1487 list_for_each_safe(pos, temp, &ep->asocs) {
1488 asoc = list_entry(pos, struct sctp_association, asocs);
1489
1490 if (sctp_style(sk, TCP)) {
1491 /* A closed association can still be in the list if
1492 * it belongs to a TCP-style listening socket that is
1493 * not yet accepted. If so, free it. If not, send an
1494 * ABORT or SHUTDOWN based on the linger options.
1495 */
1496 if (sctp_state(asoc, CLOSED)) {
1da177e4 1497 sctp_association_free(asoc);
b89498a1
VY
1498 continue;
1499 }
1500 }
1da177e4 1501
cd4fcc70
TG
1502 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1503 !skb_queue_empty(&asoc->ulpq.reasm) ||
13228238 1504 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
cd4fcc70 1505 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
b9ac8672
SS
1506 struct sctp_chunk *chunk;
1507
1508 chunk = sctp_make_abort_user(asoc, NULL, 0);
068d8bd3 1509 sctp_primitive_ABORT(net, asoc, chunk);
b9ac8672 1510 } else
55e26eb9 1511 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
1512 }
1513
1da177e4
LT
1514 /* On a TCP-style socket, block for at most linger_time if set. */
1515 if (sctp_style(sk, TCP) && timeout)
1516 sctp_wait_for_close(sk, timeout);
1517
1518 /* This will run the backlog queue. */
048ed4b6 1519 release_sock(sk);
1da177e4
LT
1520
1521 /* Supposedly, no process has access to the socket, but
1522 * the net layers still may.
2d45a02d
MRL
1523 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1524 * held and that should be grabbed before socket lock.
1da177e4 1525 */
2d45a02d 1526 spin_lock_bh(&net->sctp.addr_wq_lock);
6dfe4b97 1527 bh_lock_sock_nested(sk);
1da177e4
LT
1528
1529 /* Hold the sock, since sk_common_release() will put sock_put()
1530 * and we have just a little more cleanup.
1531 */
1532 sock_hold(sk);
1533 sk_common_release(sk);
1534
5bc1d1b4 1535 bh_unlock_sock(sk);
2d45a02d 1536 spin_unlock_bh(&net->sctp.addr_wq_lock);
1da177e4
LT
1537
1538 sock_put(sk);
1539
1540 SCTP_DBG_OBJCNT_DEC(sock);
1541}
1542
1543/* Handle EPIPE error. */
1544static int sctp_error(struct sock *sk, int flags, int err)
1545{
1546 if (err == -EPIPE)
1547 err = sock_error(sk) ? : -EPIPE;
1548 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1549 send_sig(SIGPIPE, current, 0);
1550 return err;
1551}
1552
1553/* API 3.1.3 sendmsg() - UDP Style Syntax
1554 *
1555 * An application uses sendmsg() and recvmsg() calls to transmit data to
1556 * and receive data from its peer.
1557 *
1558 * ssize_t sendmsg(int socket, const struct msghdr *message,
1559 * int flags);
1560 *
1561 * socket - the socket descriptor of the endpoint.
1562 * message - pointer to the msghdr structure which contains a single
1563 * user message and possibly some ancillary data.
1564 *
1565 * See Section 5 for complete description of the data
1566 * structures.
1567 *
1568 * flags - flags sent or received with the user message, see Section
1569 * 5 for complete description of the flags.
1570 *
1571 * Note: This function could use a rewrite especially when explicit
1572 * connect support comes in.
1573 */
1574/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1575
a05437ac
XL
1576static int sctp_msghdr_parse(const struct msghdr *msg,
1577 struct sctp_cmsgs *cmsgs);
1da177e4 1578
204f817f
XL
1579static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1580 struct sctp_sndrcvinfo *srinfo,
1581 const struct msghdr *msg, size_t msg_len)
1582{
1583 __u16 sflags;
1584 int err;
1585
1586 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1587 return -EPIPE;
1588
1589 if (msg_len > sk->sk_sndbuf)
1590 return -EMSGSIZE;
1591
1592 memset(cmsgs, 0, sizeof(*cmsgs));
1593 err = sctp_msghdr_parse(msg, cmsgs);
1594 if (err) {
1595 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1596 return err;
1597 }
1598
1599 memset(srinfo, 0, sizeof(*srinfo));
1600 if (cmsgs->srinfo) {
1601 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1602 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1603 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1604 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1605 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1606 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1607 }
1608
1609 if (cmsgs->sinfo) {
1610 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1611 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1612 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1613 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1614 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1615 }
1616
ed63afb8
XL
1617 if (cmsgs->prinfo) {
1618 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1619 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1620 cmsgs->prinfo->pr_policy);
1621 }
1622
204f817f
XL
1623 sflags = srinfo->sinfo_flags;
1624 if (!sflags && msg_len)
1625 return 0;
1626
1627 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1628 return -EINVAL;
1629
1630 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1631 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1632 return -EINVAL;
1633
1634 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1635 return -EINVAL;
1636
1637 return 0;
1638}
1639
2bfd80f9
XL
1640static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1641 struct sctp_cmsgs *cmsgs,
1642 union sctp_addr *daddr,
1643 struct sctp_transport **tp)
1644{
1645 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2bfd80f9 1646 struct sctp_association *asoc;
2c0dbaa0 1647 struct cmsghdr *cmsg;
4be4139f 1648 __be32 flowinfo = 0;
9eda2d2d 1649 struct sctp_af *af;
d98985dd 1650 int err;
2bfd80f9
XL
1651
1652 *tp = NULL;
1653
1654 if (sflags & (SCTP_EOF | SCTP_ABORT))
1655 return -EINVAL;
1656
1657 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1658 sctp_sstate(sk, CLOSING)))
1659 return -EADDRNOTAVAIL;
1660
9eda2d2d
LT
1661 /* Label connection socket for first association 1-to-many
1662 * style for client sequence socket()->sendmsg(). This
1663 * needs to be done before sctp_assoc_add_peer() as that will
1664 * set up the initial packet that needs to account for any
1665 * security ip options (CIPSO/CALIPSO) added to the packet.
1666 */
1667 af = sctp_get_af_specific(daddr->sa.sa_family);
1668 if (!af)
1669 return -EINVAL;
1670 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1671 (struct sockaddr *)daddr,
1672 af->sockaddr_len);
1673 if (err < 0)
1674 return err;
1da177e4 1675
f26f9951
XL
1676 err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
1677 if (err)
1678 return err;
1679 asoc = (*tp)->asoc;
2bfd80f9 1680
2c0dbaa0
XL
1681 if (!cmsgs->addrs_msg)
1682 return 0;
1683
4be4139f
XL
1684 if (daddr->sa.sa_family == AF_INET6)
1685 flowinfo = daddr->v6.sin6_flowinfo;
1686
2c0dbaa0
XL
1687 /* sendv addr list parse */
1688 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
2c0dbaa0
XL
1689 union sctp_addr _daddr;
1690 int dlen;
1691
1692 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1693 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1694 cmsg->cmsg_type != SCTP_DSTADDRV6))
1695 continue;
1696
1697 daddr = &_daddr;
1698 memset(daddr, 0, sizeof(*daddr));
1699 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1700 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
d98985dd
WY
1701 if (dlen < sizeof(struct in_addr)) {
1702 err = -EINVAL;
2c0dbaa0 1703 goto free;
d98985dd 1704 }
2c0dbaa0
XL
1705
1706 dlen = sizeof(struct in_addr);
1707 daddr->v4.sin_family = AF_INET;
1708 daddr->v4.sin_port = htons(asoc->peer.port);
1709 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1710 } else {
d98985dd
WY
1711 if (dlen < sizeof(struct in6_addr)) {
1712 err = -EINVAL;
2c0dbaa0 1713 goto free;
d98985dd 1714 }
2c0dbaa0
XL
1715
1716 dlen = sizeof(struct in6_addr);
4be4139f 1717 daddr->v6.sin6_flowinfo = flowinfo;
2c0dbaa0
XL
1718 daddr->v6.sin6_family = AF_INET6;
1719 daddr->v6.sin6_port = htons(asoc->peer.port);
1720 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1721 }
2c0dbaa0 1722
a64e59c7
XL
1723 err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
1724 if (err)
2c0dbaa0 1725 goto free;
2c0dbaa0
XL
1726 }
1727
2bfd80f9
XL
1728 return 0;
1729
1730free:
1731 sctp_association_free(asoc);
1732 return err;
1733}
1734
c2666de1
XL
1735static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1736 __u16 sflags, struct msghdr *msg,
1737 size_t msg_len)
1738{
1739 struct sock *sk = asoc->base.sk;
1740 struct net *net = sock_net(sk);
1741
1742 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1743 return -EPIPE;
1744
49102805
XL
1745 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1746 !sctp_state(asoc, ESTABLISHED))
1747 return 0;
1748
c2666de1
XL
1749 if (sflags & SCTP_EOF) {
1750 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1751 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1752
1753 return 0;
1754 }
1755
1756 if (sflags & SCTP_ABORT) {
1757 struct sctp_chunk *chunk;
1758
1759 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1760 if (!chunk)
1761 return -ENOMEM;
1762
1763 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1764 sctp_primitive_ABORT(net, asoc, chunk);
901efe12 1765 iov_iter_revert(&msg->msg_iter, msg_len);
c2666de1
XL
1766
1767 return 0;
1768 }
1769
1770 return 1;
1771}
1772
f84af331
XL
1773static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1774 struct msghdr *msg, size_t msg_len,
1775 struct sctp_transport *transport,
1776 struct sctp_sndrcvinfo *sinfo)
1777{
1778 struct sock *sk = asoc->base.sk;
63d01330 1779 struct sctp_sock *sp = sctp_sk(sk);
f84af331
XL
1780 struct net *net = sock_net(sk);
1781 struct sctp_datamsg *datamsg;
1782 bool wait_connect = false;
1783 struct sctp_chunk *chunk;
1784 long timeo;
1785 int err;
1786
1787 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1788 err = -EINVAL;
1789 goto err;
1790 }
1791
05364ca0 1792 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
f84af331
XL
1793 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1794 if (err)
1795 goto err;
1796 }
1797
63d01330 1798 if (sp->disable_fragments && msg_len > asoc->frag_point) {
f84af331
XL
1799 err = -EMSGSIZE;
1800 goto err;
1801 }
1802
2521680e 1803 if (asoc->pmtu_pending) {
63d01330
MRL
1804 if (sp->param_flags & SPP_PMTUD_ENABLE)
1805 sctp_assoc_sync_pmtu(asoc);
2521680e
MRL
1806 asoc->pmtu_pending = 0;
1807 }
0aee4c25 1808
cd305c74 1809 if (sctp_wspace(asoc) < (int)msg_len)
0aee4c25
NH
1810 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1811
1033990a
XL
1812 if (sk_under_memory_pressure(sk))
1813 sk_mem_reclaim(sk);
1814
1815 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
0aee4c25
NH
1816 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1817 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1818 if (err)
1819 goto err;
1820 }
1821
f84af331
XL
1822 if (sctp_state(asoc, CLOSED)) {
1823 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1824 if (err)
1825 goto err;
1826
e55f4b8b 1827 if (asoc->ep->intl_enable) {
f84af331
XL
1828 timeo = sock_sndtimeo(sk, 0);
1829 err = sctp_wait_for_connect(asoc, &timeo);
c863850c
XL
1830 if (err) {
1831 err = -ESRCH;
f84af331 1832 goto err;
c863850c 1833 }
f84af331
XL
1834 } else {
1835 wait_connect = true;
1836 }
1837
1838 pr_debug("%s: we associated primitively\n", __func__);
1839 }
1840
f84af331
XL
1841 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1842 if (IS_ERR(datamsg)) {
1843 err = PTR_ERR(datamsg);
1844 goto err;
1845 }
1846
1847 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1848
1849 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1850 sctp_chunk_hold(chunk);
1851 sctp_set_owner_w(chunk);
1852 chunk->transport = transport;
1853 }
1854
1855 err = sctp_primitive_SEND(net, asoc, datamsg);
1856 if (err) {
1857 sctp_datamsg_free(datamsg);
1858 goto err;
1859 }
1860
1861 pr_debug("%s: we sent primitively\n", __func__);
1862
1863 sctp_datamsg_put(datamsg);
1864
1865 if (unlikely(wait_connect)) {
1866 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1867 sctp_wait_for_connect(asoc, &timeo);
1868 }
1869
1870 err = msg_len;
1871
1872err:
1873 return err;
1874}
1875
becef9b1
XL
1876static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1877 const struct msghdr *msg,
1878 struct sctp_cmsgs *cmsgs)
1879{
1880 union sctp_addr *daddr = NULL;
1881 int err;
1882
1883 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1884 int len = msg->msg_namelen;
1885
1886 if (len > sizeof(*daddr))
1887 len = sizeof(*daddr);
1888
1889 daddr = (union sctp_addr *)msg->msg_name;
1890
1891 err = sctp_verify_addr(sk, daddr, len);
1892 if (err)
1893 return ERR_PTR(err);
1894 }
1895
1896 return daddr;
1897}
1898
d42cb06e
XL
1899static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1900 struct sctp_sndrcvinfo *sinfo,
1901 struct sctp_cmsgs *cmsgs)
1902{
1903 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1904 sinfo->sinfo_stream = asoc->default_stream;
1905 sinfo->sinfo_ppid = asoc->default_ppid;
1906 sinfo->sinfo_context = asoc->default_context;
1907 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
ed63afb8
XL
1908
1909 if (!cmsgs->prinfo)
1910 sinfo->sinfo_flags = asoc->default_flags;
d42cb06e
XL
1911 }
1912
ed63afb8 1913 if (!cmsgs->srinfo && !cmsgs->prinfo)
d42cb06e 1914 sinfo->sinfo_timetolive = asoc->default_timetolive;
3ff547c0
XL
1915
1916 if (cmsgs->authinfo) {
1917 /* Reuse sinfo_tsn to indicate that authinfo was set and
1918 * sinfo_ssn to save the keyid on tx path.
1919 */
1920 sinfo->sinfo_tsn = 1;
1921 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1922 }
d42cb06e
XL
1923}
1924
1b784140 1925static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1da177e4 1926{
204f817f 1927 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8e87c6eb 1928 struct sctp_transport *transport = NULL;
204f817f 1929 struct sctp_sndrcvinfo _sinfo, *sinfo;
ba59fb02 1930 struct sctp_association *asoc, *tmp;
007b7e18 1931 struct sctp_cmsgs cmsgs;
becef9b1 1932 union sctp_addr *daddr;
007b7e18
XL
1933 bool new = false;
1934 __u16 sflags;
63b94938 1935 int err;
1da177e4 1936
204f817f
XL
1937 /* Parse and get snd_info */
1938 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1939 if (err)
007b7e18 1940 goto out;
1da177e4 1941
204f817f 1942 sinfo = &_sinfo;
007b7e18 1943 sflags = sinfo->sinfo_flags;
1da177e4 1944
becef9b1
XL
1945 /* Get daddr from msg */
1946 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1947 if (IS_ERR(daddr)) {
1948 err = PTR_ERR(daddr);
007b7e18 1949 goto out;
1da177e4
LT
1950 }
1951
048ed4b6 1952 lock_sock(sk);
1da177e4 1953
49102805
XL
1954 /* SCTP_SENDALL process */
1955 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
ba59fb02 1956 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
49102805
XL
1957 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1958 msg_len);
1959 if (err == 0)
1960 continue;
1961 if (err < 0)
1962 goto out_unlock;
1963
1964 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1965
1966 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
1967 NULL, sinfo);
1968 if (err < 0)
1969 goto out_unlock;
1970
1971 iov_iter_revert(&msg->msg_iter, err);
1972 }
1973
1974 goto out_unlock;
1975 }
1976
0a3920d2 1977 /* Get and check or create asoc */
becef9b1 1978 if (daddr) {
becef9b1 1979 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
0a3920d2
XL
1980 if (asoc) {
1981 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1982 msg_len);
1983 if (err <= 0)
1984 goto out_unlock;
1985 } else {
1986 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
1987 &transport);
1988 if (err)
1989 goto out_unlock;
1990
1991 asoc = transport->asoc;
1992 new = true;
1993 }
1994
1995 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
1996 transport = NULL;
1da177e4 1997 } else {
007b7e18 1998 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
1da177e4
LT
1999 if (!asoc) {
2000 err = -EPIPE;
2001 goto out_unlock;
2002 }
1da177e4 2003
007b7e18 2004 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
c2666de1 2005 if (err <= 0)
1da177e4 2006 goto out_unlock;
1da177e4
LT
2007 }
2008
d42cb06e
XL
2009 /* Update snd_info with the asoc */
2010 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1da177e4 2011
f84af331 2012 /* Send msg to the asoc */
8e87c6eb 2013 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
007b7e18 2014 if (err < 0 && err != -ESRCH && new)
1da177e4 2015 sctp_association_free(asoc);
8e87c6eb 2016
1da177e4 2017out_unlock:
048ed4b6 2018 release_sock(sk);
007b7e18 2019out:
f84af331 2020 return sctp_error(sk, msg->msg_flags, err);
1da177e4
LT
2021}
2022
2023/* This is an extended version of skb_pull() that removes the data from the
2024 * start of a skb even when data is spread across the list of skb's in the
2025 * frag_list. len specifies the total amount of data that needs to be removed.
2026 * when 'len' bytes could be removed from the skb, it returns 0.
2027 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2028 * could not be removed.
2029 */
2030static int sctp_skb_pull(struct sk_buff *skb, int len)
2031{
2032 struct sk_buff *list;
2033 int skb_len = skb_headlen(skb);
2034 int rlen;
2035
2036 if (len <= skb_len) {
2037 __skb_pull(skb, len);
2038 return 0;
2039 }
2040 len -= skb_len;
2041 __skb_pull(skb, skb_len);
2042
1b003be3 2043 skb_walk_frags(skb, list) {
1da177e4
LT
2044 rlen = sctp_skb_pull(list, len);
2045 skb->len -= (len-rlen);
2046 skb->data_len -= (len-rlen);
2047
2048 if (!rlen)
2049 return 0;
2050
2051 len = rlen;
2052 }
2053
2054 return len;
2055}
2056
2057/* API 3.1.3 recvmsg() - UDP Style Syntax
2058 *
2059 * ssize_t recvmsg(int socket, struct msghdr *message,
2060 * int flags);
2061 *
2062 * socket - the socket descriptor of the endpoint.
2063 * message - pointer to the msghdr structure which contains a single
2064 * user message and possibly some ancillary data.
2065 *
2066 * See Section 5 for complete description of the data
2067 * structures.
2068 *
2069 * flags - flags sent or received with the user message, see Section
2070 * 5 for complete description of the flags.
2071 */
1b784140
YX
2072static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2073 int noblock, int flags, int *addr_len)
1da177e4
LT
2074{
2075 struct sctp_ulpevent *event = NULL;
2076 struct sctp_sock *sp = sctp_sk(sk);
1f45f78f 2077 struct sk_buff *skb, *head_skb;
1da177e4
LT
2078 int copied;
2079 int err = 0;
2080 int skb_len;
2081
bb33381d
DB
2082 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2083 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2084 addr_len);
1da177e4 2085
048ed4b6 2086 lock_sock(sk);
1da177e4 2087
e5b13f34 2088 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
e0878694 2089 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
1da177e4
LT
2090 err = -ENOTCONN;
2091 goto out;
2092 }
2093
2094 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2095 if (!skb)
2096 goto out;
2097
2098 /* Get the total length of the skb including any skb's in the
2099 * frag_list.
2100 */
2101 skb_len = skb->len;
2102
2103 copied = skb_len;
2104 if (copied > len)
2105 copied = len;
2106
51f3d02b 2107 err = skb_copy_datagram_msg(skb, 0, msg, copied);
1da177e4
LT
2108
2109 event = sctp_skb2event(skb);
2110
2111 if (err)
2112 goto out_free;
2113
1f45f78f
MRL
2114 if (event->chunk && event->chunk->head_skb)
2115 head_skb = event->chunk->head_skb;
2116 else
2117 head_skb = skb;
2118 sock_recv_ts_and_drops(msg, sk, head_skb);
1da177e4
LT
2119 if (sctp_ulpevent_is_notification(event)) {
2120 msg->msg_flags |= MSG_NOTIFICATION;
2121 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2122 } else {
1f45f78f 2123 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
1da177e4
LT
2124 }
2125
2347c80f
GOV
2126 /* Check if we allow SCTP_NXTINFO. */
2127 if (sp->recvnxtinfo)
2128 sctp_ulpevent_read_nxtinfo(event, msg, sk);
0d3a421d
GOV
2129 /* Check if we allow SCTP_RCVINFO. */
2130 if (sp->recvrcvinfo)
2131 sctp_ulpevent_read_rcvinfo(event, msg);
1da177e4 2132 /* Check if we allow SCTP_SNDRCVINFO. */
2cc0eeb6 2133 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
1da177e4 2134 sctp_ulpevent_read_sndrcvinfo(event, msg);
0d3a421d 2135
1da177e4
LT
2136 err = copied;
2137
2138 /* If skb's length exceeds the user's buffer, update the skb and
2139 * push it back to the receive_queue so that the next call to
2140 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2141 */
2142 if (skb_len > copied) {
2143 msg->msg_flags &= ~MSG_EOR;
2144 if (flags & MSG_PEEK)
2145 goto out_free;
2146 sctp_skb_pull(skb, copied);
2147 skb_queue_head(&sk->sk_receive_queue, skb);
2148
362d5204
DB
2149 /* When only partial message is copied to the user, increase
2150 * rwnd by that amount. If all the data in the skb is read,
2151 * rwnd is updated when the event is freed.
2152 */
2153 if (!sctp_ulpevent_is_notification(event))
2154 sctp_assoc_rwnd_increase(event->asoc, copied);
1da177e4
LT
2155 goto out;
2156 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2157 (event->msg_flags & MSG_EOR))
2158 msg->msg_flags |= MSG_EOR;
2159 else
2160 msg->msg_flags &= ~MSG_EOR;
2161
2162out_free:
2163 if (flags & MSG_PEEK) {
2164 /* Release the skb reference acquired after peeking the skb in
2165 * sctp_skb_recv_datagram().
2166 */
2167 kfree_skb(skb);
2168 } else {
2169 /* Free the event which includes releasing the reference to
2170 * the owner of the skb, freeing the skb and updating the
2171 * rwnd.
2172 */
2173 sctp_ulpevent_free(event);
2174 }
2175out:
048ed4b6 2176 release_sock(sk);
1da177e4
LT
2177 return err;
2178}
2179
2180/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2181 *
2182 * This option is a on/off flag. If enabled no SCTP message
2183 * fragmentation will be performed. Instead if a message being sent
2184 * exceeds the current PMTU size, the message will NOT be sent and
2185 * instead a error will be indicated to the user.
2186 */
2187static int sctp_setsockopt_disable_fragments(struct sock *sk,
b7058842
DM
2188 char __user *optval,
2189 unsigned int optlen)
1da177e4
LT
2190{
2191 int val;
2192
2193 if (optlen < sizeof(int))
2194 return -EINVAL;
2195
2196 if (get_user(val, (int __user *)optval))
2197 return -EFAULT;
2198
2199 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2200
2201 return 0;
2202}
2203
2204static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
b7058842 2205 unsigned int optlen)
1da177e4 2206{
2cc0eeb6
XL
2207 struct sctp_event_subscribe subscribe;
2208 __u8 *sn_type = (__u8 *)&subscribe;
2209 struct sctp_sock *sp = sctp_sk(sk);
a1e3a059 2210 struct sctp_association *asoc;
2cc0eeb6 2211 int i;
94912301 2212
7e8616d8 2213 if (optlen > sizeof(struct sctp_event_subscribe))
1da177e4 2214 return -EINVAL;
2cc0eeb6
XL
2215
2216 if (copy_from_user(&subscribe, optval, optlen))
1da177e4 2217 return -EFAULT;
94912301 2218
2cc0eeb6
XL
2219 for (i = 0; i < optlen; i++)
2220 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2221 sn_type[i]);
2222
a1e3a059
XL
2223 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2224 asoc->subscribe = sctp_sk(sk)->subscribe;
2225
bbbea41d 2226 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
94912301
WY
2227 * if there is no data to be sent or retransmit, the stack will
2228 * immediately send up this notification.
2229 */
2cc0eeb6 2230 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2cc0eeb6 2231 struct sctp_ulpevent *event;
94912301 2232
a1e3a059 2233 asoc = sctp_id2assoc(sk, 0);
94912301
WY
2234 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2235 event = sctp_ulpevent_make_sender_dry_event(asoc,
2e83acb9 2236 GFP_USER | __GFP_NOWARN);
94912301
WY
2237 if (!event)
2238 return -ENOMEM;
2239
9162e0ed 2240 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
94912301
WY
2241 }
2242 }
2243
1da177e4
LT
2244 return 0;
2245}
2246
2247/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2248 *
2249 * This socket option is applicable to the UDP-style socket only. When
2250 * set it will cause associations that are idle for more than the
2251 * specified number of seconds to automatically close. An association
2252 * being idle is defined an association that has NOT sent or received
2253 * user data. The special value of '0' indicates that no automatic
2254 * close of any associations should be performed. The option expects an
2255 * integer defining the number of seconds of idle time before an
2256 * association is closed.
2257 */
2258static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
b7058842 2259 unsigned int optlen)
1da177e4
LT
2260{
2261 struct sctp_sock *sp = sctp_sk(sk);
9f70f46b 2262 struct net *net = sock_net(sk);
1da177e4
LT
2263
2264 /* Applicable to UDP-style socket only */
2265 if (sctp_style(sk, TCP))
2266 return -EOPNOTSUPP;
2267 if (optlen != sizeof(int))
2268 return -EINVAL;
2269 if (copy_from_user(&sp->autoclose, optval, optlen))
2270 return -EFAULT;
2271
9f70f46b
NH
2272 if (sp->autoclose > net->sctp.max_autoclose)
2273 sp->autoclose = net->sctp.max_autoclose;
2274
1da177e4
LT
2275 return 0;
2276}
2277
2278/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2279 *
2280 * Applications can enable or disable heartbeats for any peer address of
2281 * an association, modify an address's heartbeat interval, force a
2282 * heartbeat to be sent immediately, and adjust the address's maximum
2283 * number of retransmissions sent before an address is considered
2284 * unreachable. The following structure is used to access and modify an
2285 * address's parameters:
2286 *
2287 * struct sctp_paddrparams {
52ccb8e9
FF
2288 * sctp_assoc_t spp_assoc_id;
2289 * struct sockaddr_storage spp_address;
2290 * uint32_t spp_hbinterval;
2291 * uint16_t spp_pathmaxrxt;
2292 * uint32_t spp_pathmtu;
2293 * uint32_t spp_sackdelay;
2294 * uint32_t spp_flags;
0b0dce7a
XL
2295 * uint32_t spp_ipv6_flowlabel;
2296 * uint8_t spp_dscp;
52ccb8e9
FF
2297 * };
2298 *
2299 * spp_assoc_id - (one-to-many style socket) This is filled in the
2300 * application, and identifies the association for
2301 * this query.
1da177e4
LT
2302 * spp_address - This specifies which address is of interest.
2303 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
2304 * in milliseconds. If a value of zero
2305 * is present in this field then no changes are to
2306 * be made to this parameter.
1da177e4
LT
2307 * spp_pathmaxrxt - This contains the maximum number of
2308 * retransmissions before this address shall be
52ccb8e9
FF
2309 * considered unreachable. If a value of zero
2310 * is present in this field then no changes are to
2311 * be made to this parameter.
2312 * spp_pathmtu - When Path MTU discovery is disabled the value
2313 * specified here will be the "fixed" path mtu.
2314 * Note that if the spp_address field is empty
2315 * then all associations on this address will
2316 * have this fixed path mtu set upon them.
2317 *
2318 * spp_sackdelay - When delayed sack is enabled, this value specifies
2319 * the number of milliseconds that sacks will be delayed
2320 * for. This value will apply to all addresses of an
2321 * association if the spp_address field is empty. Note
2322 * also, that if delayed sack is enabled and this
2323 * value is set to 0, no change is made to the last
2324 * recorded delayed sack timer value.
2325 *
2326 * spp_flags - These flags are used to control various features
2327 * on an association. The flag field may contain
2328 * zero or more of the following options.
2329 *
2330 * SPP_HB_ENABLE - Enable heartbeats on the
2331 * specified address. Note that if the address
2332 * field is empty all addresses for the association
2333 * have heartbeats enabled upon them.
2334 *
2335 * SPP_HB_DISABLE - Disable heartbeats on the
2336 * speicifed address. Note that if the address
2337 * field is empty all addresses for the association
2338 * will have their heartbeats disabled. Note also
2339 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2340 * mutually exclusive, only one of these two should
2341 * be specified. Enabling both fields will have
2342 * undetermined results.
2343 *
2344 * SPP_HB_DEMAND - Request a user initiated heartbeat
2345 * to be made immediately.
2346 *
bdf3092a
VY
2347 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2348 * heartbeat delayis to be set to the value of 0
2349 * milliseconds.
2350 *
52ccb8e9
FF
2351 * SPP_PMTUD_ENABLE - This field will enable PMTU
2352 * discovery upon the specified address. Note that
2353 * if the address feild is empty then all addresses
2354 * on the association are effected.
2355 *
2356 * SPP_PMTUD_DISABLE - This field will disable PMTU
2357 * discovery upon the specified address. Note that
2358 * if the address feild is empty then all addresses
2359 * on the association are effected. Not also that
2360 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2361 * exclusive. Enabling both will have undetermined
2362 * results.
2363 *
2364 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2365 * on delayed sack. The time specified in spp_sackdelay
2366 * is used to specify the sack delay for this address. Note
2367 * that if spp_address is empty then all addresses will
2368 * enable delayed sack and take on the sack delay
2369 * value specified in spp_sackdelay.
2370 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2371 * off delayed sack. If the spp_address field is blank then
2372 * delayed sack is disabled for the entire association. Note
2373 * also that this field is mutually exclusive to
2374 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2375 * results.
0b0dce7a
XL
2376 *
2377 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2378 * setting of the IPV6 flow label value. The value is
2379 * contained in the spp_ipv6_flowlabel field.
2380 * Upon retrieval, this flag will be set to indicate that
2381 * the spp_ipv6_flowlabel field has a valid value returned.
2382 * If a specific destination address is set (in the
2383 * spp_address field), then the value returned is that of
2384 * the address. If just an association is specified (and
2385 * no address), then the association's default flow label
2386 * is returned. If neither an association nor a destination
2387 * is specified, then the socket's default flow label is
2388 * returned. For non-IPv6 sockets, this flag will be left
2389 * cleared.
2390 *
2391 * SPP_DSCP: Setting this flag enables the setting of the
2392 * Differentiated Services Code Point (DSCP) value
2393 * associated with either the association or a specific
2394 * address. The value is obtained in the spp_dscp field.
2395 * Upon retrieval, this flag will be set to indicate that
2396 * the spp_dscp field has a valid value returned. If a
2397 * specific destination address is set when called (in the
2398 * spp_address field), then that specific destination
2399 * address's DSCP value is returned. If just an association
2400 * is specified, then the association's default DSCP is
2401 * returned. If neither an association nor a destination is
2402 * specified, then the socket's default DSCP is returned.
2403 *
2404 * spp_ipv6_flowlabel
2405 * - This field is used in conjunction with the
2406 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2407 * The 20 least significant bits are used for the flow
2408 * label. This setting has precedence over any IPv6-layer
2409 * setting.
2410 *
2411 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2412 * and contains the DSCP. The 6 most significant bits are
2413 * used for the DSCP. This setting has precedence over any
2414 * IPv4- or IPv6- layer setting.
1da177e4 2415 */
16164366
AB
2416static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2417 struct sctp_transport *trans,
2418 struct sctp_association *asoc,
2419 struct sctp_sock *sp,
2420 int hb_change,
2421 int pmtud_change,
2422 int sackdelay_change)
52ccb8e9
FF
2423{
2424 int error;
2425
2426 if (params->spp_flags & SPP_HB_DEMAND && trans) {
4e7696d9
XL
2427 error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
2428 trans->asoc, trans);
52ccb8e9
FF
2429 if (error)
2430 return error;
2431 }
2432
bdf3092a
VY
2433 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2434 * this field is ignored. Note also that a value of zero indicates
2435 * the current setting should be left unchanged.
2436 */
2437 if (params->spp_flags & SPP_HB_ENABLE) {
2438
2439 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2440 * set. This lets us use 0 value when this flag
2441 * is set.
2442 */
2443 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2444 params->spp_hbinterval = 0;
2445
2446 if (params->spp_hbinterval ||
2447 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2448 if (trans) {
2449 trans->hbinterval =
2450 msecs_to_jiffies(params->spp_hbinterval);
2451 } else if (asoc) {
2452 asoc->hbinterval =
2453 msecs_to_jiffies(params->spp_hbinterval);
2454 } else {
2455 sp->hbinterval = params->spp_hbinterval;
2456 }
52ccb8e9
FF
2457 }
2458 }
2459
2460 if (hb_change) {
2461 if (trans) {
2462 trans->param_flags =
2463 (trans->param_flags & ~SPP_HB) | hb_change;
2464 } else if (asoc) {
2465 asoc->param_flags =
2466 (asoc->param_flags & ~SPP_HB) | hb_change;
2467 } else {
2468 sp->param_flags =
2469 (sp->param_flags & ~SPP_HB) | hb_change;
2470 }
2471 }
2472
bdf3092a
VY
2473 /* When Path MTU discovery is disabled the value specified here will
2474 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2475 * include the flag SPP_PMTUD_DISABLE for this field to have any
2476 * effect).
2477 */
2478 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
52ccb8e9
FF
2479 if (trans) {
2480 trans->pathmtu = params->spp_pathmtu;
3ebfdf08 2481 sctp_assoc_sync_pmtu(asoc);
52ccb8e9 2482 } else if (asoc) {
c4b2893d 2483 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
52ccb8e9
FF
2484 } else {
2485 sp->pathmtu = params->spp_pathmtu;
2486 }
2487 }
2488
2489 if (pmtud_change) {
2490 if (trans) {
2491 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2492 (params->spp_flags & SPP_PMTUD_ENABLE);
2493 trans->param_flags =
2494 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2495 if (update) {
9914ae3c 2496 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
3ebfdf08 2497 sctp_assoc_sync_pmtu(asoc);
52ccb8e9
FF
2498 }
2499 } else if (asoc) {
2500 asoc->param_flags =
2501 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2502 } else {
2503 sp->param_flags =
2504 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2505 }
2506 }
2507
bdf3092a
VY
2508 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2509 * value of this field is ignored. Note also that a value of zero
2510 * indicates the current setting should be left unchanged.
2511 */
2512 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
52ccb8e9
FF
2513 if (trans) {
2514 trans->sackdelay =
2515 msecs_to_jiffies(params->spp_sackdelay);
2516 } else if (asoc) {
2517 asoc->sackdelay =
2518 msecs_to_jiffies(params->spp_sackdelay);
2519 } else {
2520 sp->sackdelay = params->spp_sackdelay;
2521 }
2522 }
2523
2524 if (sackdelay_change) {
2525 if (trans) {
2526 trans->param_flags =
2527 (trans->param_flags & ~SPP_SACKDELAY) |
2528 sackdelay_change;
2529 } else if (asoc) {
2530 asoc->param_flags =
2531 (asoc->param_flags & ~SPP_SACKDELAY) |
2532 sackdelay_change;
2533 } else {
2534 sp->param_flags =
2535 (sp->param_flags & ~SPP_SACKDELAY) |
2536 sackdelay_change;
2537 }
2538 }
2539
37051f73
APO
2540 /* Note that a value of zero indicates the current setting should be
2541 left unchanged.
bdf3092a 2542 */
37051f73 2543 if (params->spp_pathmaxrxt) {
52ccb8e9
FF
2544 if (trans) {
2545 trans->pathmaxrxt = params->spp_pathmaxrxt;
2546 } else if (asoc) {
2547 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2548 } else {
2549 sp->pathmaxrxt = params->spp_pathmaxrxt;
2550 }
2551 }
2552
0b0dce7a 2553 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
741880e1
XL
2554 if (trans) {
2555 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2556 trans->flowlabel = params->spp_ipv6_flowlabel &
2557 SCTP_FLOWLABEL_VAL_MASK;
2558 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2559 }
0b0dce7a 2560 } else if (asoc) {
af8a2b8b
XL
2561 struct sctp_transport *t;
2562
2563 list_for_each_entry(t, &asoc->peer.transport_addr_list,
0b0dce7a 2564 transports) {
af8a2b8b 2565 if (t->ipaddr.sa.sa_family != AF_INET6)
0b0dce7a 2566 continue;
af8a2b8b
XL
2567 t->flowlabel = params->spp_ipv6_flowlabel &
2568 SCTP_FLOWLABEL_VAL_MASK;
2569 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
0b0dce7a
XL
2570 }
2571 asoc->flowlabel = params->spp_ipv6_flowlabel &
2572 SCTP_FLOWLABEL_VAL_MASK;
2573 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2574 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2575 sp->flowlabel = params->spp_ipv6_flowlabel &
2576 SCTP_FLOWLABEL_VAL_MASK;
2577 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2578 }
2579 }
2580
2581 if (params->spp_flags & SPP_DSCP) {
2582 if (trans) {
2583 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2584 trans->dscp |= SCTP_DSCP_SET_MASK;
2585 } else if (asoc) {
af8a2b8b
XL
2586 struct sctp_transport *t;
2587
2588 list_for_each_entry(t, &asoc->peer.transport_addr_list,
0b0dce7a 2589 transports) {
af8a2b8b
XL
2590 t->dscp = params->spp_dscp &
2591 SCTP_DSCP_VAL_MASK;
2592 t->dscp |= SCTP_DSCP_SET_MASK;
0b0dce7a
XL
2593 }
2594 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2595 asoc->dscp |= SCTP_DSCP_SET_MASK;
2596 } else {
2597 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2598 sp->dscp |= SCTP_DSCP_SET_MASK;
2599 }
2600 }
2601
52ccb8e9
FF
2602 return 0;
2603}
2604
1da177e4 2605static int sctp_setsockopt_peer_addr_params(struct sock *sk,
b7058842
DM
2606 char __user *optval,
2607 unsigned int optlen)
1da177e4 2608{
52ccb8e9
FF
2609 struct sctp_paddrparams params;
2610 struct sctp_transport *trans = NULL;
2611 struct sctp_association *asoc = NULL;
2612 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 2613 int error;
52ccb8e9 2614 int hb_change, pmtud_change, sackdelay_change;
1da177e4 2615
0b0dce7a
XL
2616 if (optlen == sizeof(params)) {
2617 if (copy_from_user(&params, optval, optlen))
2618 return -EFAULT;
2619 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2620 spp_ipv6_flowlabel), 4)) {
2621 if (copy_from_user(&params, optval, optlen))
2622 return -EFAULT;
2623 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2624 return -EINVAL;
2625 } else {
cb3f837b 2626 return -EINVAL;
0b0dce7a 2627 }
1da177e4 2628
52ccb8e9
FF
2629 /* Validate flags and value parameters. */
2630 hb_change = params.spp_flags & SPP_HB;
2631 pmtud_change = params.spp_flags & SPP_PMTUD;
2632 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2633
2634 if (hb_change == SPP_HB ||
2635 pmtud_change == SPP_PMTUD ||
2636 sackdelay_change == SPP_SACKDELAY ||
2637 params.spp_sackdelay > 500 ||
f64f9e71
JP
2638 (params.spp_pathmtu &&
2639 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
52ccb8e9 2640 return -EINVAL;
1da177e4 2641
52ccb8e9
FF
2642 /* If an address other than INADDR_ANY is specified, and
2643 * no transport is found, then the request is invalid.
2644 */
cb3f837b 2645 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
2646 trans = sctp_addr_id2transport(sk, &params.spp_address,
2647 params.spp_assoc_id);
2648 if (!trans)
1da177e4 2649 return -EINVAL;
1da177e4
LT
2650 }
2651
b99e5e02
XL
2652 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2653 * socket is a one to many style socket, and an association
2654 * was not found, then the id was invalid.
52ccb8e9
FF
2655 */
2656 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
b99e5e02
XL
2657 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2658 sctp_style(sk, UDP))
1da177e4
LT
2659 return -EINVAL;
2660
52ccb8e9
FF
2661 /* Heartbeat demand can only be sent on a transport or
2662 * association, but not a socket.
1da177e4 2663 */
52ccb8e9
FF
2664 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2665 return -EINVAL;
2666
2667 /* Process parameters. */
2668 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2669 hb_change, pmtud_change,
2670 sackdelay_change);
1da177e4 2671
52ccb8e9
FF
2672 if (error)
2673 return error;
2674
2675 /* If changes are for association, also apply parameters to each
2676 * transport.
1da177e4 2677 */
52ccb8e9 2678 if (!trans && asoc) {
9dbc15f0
RD
2679 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2680 transports) {
52ccb8e9
FF
2681 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2682 hb_change, pmtud_change,
2683 sackdelay_change);
2684 }
2685 }
1da177e4
LT
2686
2687 return 0;
2688}
2689
0ea5e4df 2690static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2691{
2692 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2693}
2694
2695static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2696{
2697 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2698}
2699
9c5829e1
XL
2700static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2701 struct sctp_association *asoc)
2702{
2703 struct sctp_transport *trans;
2704
2705 if (params->sack_delay) {
2706 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2707 asoc->param_flags =
2708 sctp_spp_sackdelay_enable(asoc->param_flags);
2709 }
2710 if (params->sack_freq == 1) {
2711 asoc->param_flags =
2712 sctp_spp_sackdelay_disable(asoc->param_flags);
2713 } else if (params->sack_freq > 1) {
2714 asoc->sackfreq = params->sack_freq;
2715 asoc->param_flags =
2716 sctp_spp_sackdelay_enable(asoc->param_flags);
2717 }
2718
2719 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2720 transports) {
2721 if (params->sack_delay) {
2722 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2723 trans->param_flags =
2724 sctp_spp_sackdelay_enable(trans->param_flags);
2725 }
2726 if (params->sack_freq == 1) {
2727 trans->param_flags =
2728 sctp_spp_sackdelay_disable(trans->param_flags);
2729 } else if (params->sack_freq > 1) {
2730 trans->sackfreq = params->sack_freq;
2731 trans->param_flags =
2732 sctp_spp_sackdelay_enable(trans->param_flags);
2733 }
2734 }
2735}
2736
d364d927
WY
2737/*
2738 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2739 *
2740 * This option will effect the way delayed acks are performed. This
2741 * option allows you to get or set the delayed ack time, in
2742 * milliseconds. It also allows changing the delayed ack frequency.
2743 * Changing the frequency to 1 disables the delayed sack algorithm. If
2744 * the assoc_id is 0, then this sets or gets the endpoints default
2745 * values. If the assoc_id field is non-zero, then the set or get
2746 * effects the specified association for the one to many model (the
2747 * assoc_id field is ignored by the one to one model). Note that if
2748 * sack_delay or sack_freq are 0 when setting this option, then the
2749 * current values will remain unchanged.
2750 *
2751 * struct sctp_sack_info {
2752 * sctp_assoc_t sack_assoc_id;
2753 * uint32_t sack_delay;
2754 * uint32_t sack_freq;
2755 * };
2756 *
2757 * sack_assoc_id - This parameter, indicates which association the user
2758 * is performing an action upon. Note that if this field's value is
2759 * zero then the endpoints default value is changed (effecting future
2760 * associations only).
2761 *
2762 * sack_delay - This parameter contains the number of milliseconds that
2763 * the user is requesting the delayed ACK timer be set to. Note that
2764 * this value is defined in the standard to be between 200 and 500
2765 * milliseconds.
2766 *
2767 * sack_freq - This parameter contains the number of packets that must
2768 * be received before a sack is sent without waiting for the delay
2769 * timer to expire. The default value for this is 2, setting this
2770 * value to 1 will disable the delayed sack algorithm.
7708610b
FF
2771 */
2772
d364d927 2773static int sctp_setsockopt_delayed_ack(struct sock *sk,
b7058842 2774 char __user *optval, unsigned int optlen)
7708610b 2775{
9c5829e1
XL
2776 struct sctp_sock *sp = sctp_sk(sk);
2777 struct sctp_association *asoc;
2778 struct sctp_sack_info params;
7708610b 2779
d364d927
WY
2780 if (optlen == sizeof(struct sctp_sack_info)) {
2781 if (copy_from_user(&params, optval, optlen))
2782 return -EFAULT;
7708610b 2783
d364d927
WY
2784 if (params.sack_delay == 0 && params.sack_freq == 0)
2785 return 0;
2786 } else if (optlen == sizeof(struct sctp_assoc_value)) {
94f65193 2787 pr_warn_ratelimited(DEPRECATED
f916ec96 2788 "%s (pid %d) "
94f65193 2789 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
f916ec96
NH
2790 "Use struct sctp_sack_info instead\n",
2791 current->comm, task_pid_nr(current));
d364d927
WY
2792 if (copy_from_user(&params, optval, optlen))
2793 return -EFAULT;
2794
2795 if (params.sack_delay == 0)
2796 params.sack_freq = 1;
2797 else
2798 params.sack_freq = 0;
2799 } else
cb3f837b 2800 return -EINVAL;
7708610b
FF
2801
2802 /* Validate value parameter. */
d364d927 2803 if (params.sack_delay > 500)
7708610b
FF
2804 return -EINVAL;
2805
9c5829e1
XL
2806 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2807 * socket is a one to many style socket, and an association
2808 * was not found, then the id was invalid.
d808ad9a 2809 */
d364d927 2810 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
9c5829e1
XL
2811 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2812 sctp_style(sk, UDP))
7708610b
FF
2813 return -EINVAL;
2814
9c5829e1
XL
2815 if (asoc) {
2816 sctp_apply_asoc_delayed_ack(&params, asoc);
2817
2818 return 0;
2819 }
2820
8e2614fc
XL
2821 if (sctp_style(sk, TCP))
2822 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2823
9c5829e1
XL
2824 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2825 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2826 if (params.sack_delay) {
d364d927 2827 sp->sackdelay = params.sack_delay;
d808ad9a 2828 sp->param_flags =
0ea5e4df 2829 sctp_spp_sackdelay_enable(sp->param_flags);
7708610b 2830 }
9c5829e1 2831 if (params.sack_freq == 1) {
d808ad9a 2832 sp->param_flags =
0ea5e4df 2833 sctp_spp_sackdelay_disable(sp->param_flags);
9c5829e1 2834 } else if (params.sack_freq > 1) {
d364d927
WY
2835 sp->sackfreq = params.sack_freq;
2836 sp->param_flags =
0ea5e4df 2837 sctp_spp_sackdelay_enable(sp->param_flags);
d364d927 2838 }
7708610b
FF
2839 }
2840
9c5829e1
XL
2841 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2842 params.sack_assoc_id == SCTP_ALL_ASSOC)
2843 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2844 sctp_apply_asoc_delayed_ack(&params, asoc);
d808ad9a 2845
7708610b
FF
2846 return 0;
2847}
2848
1da177e4
LT
2849/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2850 *
2851 * Applications can specify protocol parameters for the default association
2852 * initialization. The option name argument to setsockopt() and getsockopt()
2853 * is SCTP_INITMSG.
2854 *
2855 * Setting initialization parameters is effective only on an unconnected
2856 * socket (for UDP-style sockets only future associations are effected
2857 * by the change). With TCP-style sockets, this option is inherited by
2858 * sockets derived from a listener socket.
2859 */
b7058842 2860static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2861{
2862 struct sctp_initmsg sinit;
2863 struct sctp_sock *sp = sctp_sk(sk);
2864
2865 if (optlen != sizeof(struct sctp_initmsg))
2866 return -EINVAL;
2867 if (copy_from_user(&sinit, optval, optlen))
2868 return -EFAULT;
2869
2870 if (sinit.sinit_num_ostreams)
d808ad9a 2871 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1da177e4 2872 if (sinit.sinit_max_instreams)
d808ad9a 2873 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1da177e4 2874 if (sinit.sinit_max_attempts)
d808ad9a 2875 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1da177e4 2876 if (sinit.sinit_max_init_timeo)
d808ad9a 2877 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1da177e4
LT
2878
2879 return 0;
2880}
2881
2882/*
2883 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2884 *
2885 * Applications that wish to use the sendto() system call may wish to
2886 * specify a default set of parameters that would normally be supplied
2887 * through the inclusion of ancillary data. This socket option allows
2888 * such an application to set the default sctp_sndrcvinfo structure.
2889 * The application that wishes to use this socket option simply passes
2890 * in to this call the sctp_sndrcvinfo structure defined in Section
2891 * 5.2.2) The input parameters accepted by this call include
2892 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2893 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2894 * to this call if the caller is using the UDP model.
2895 */
2896static int sctp_setsockopt_default_send_param(struct sock *sk,
b7058842
DM
2897 char __user *optval,
2898 unsigned int optlen)
1da177e4 2899{
1da177e4 2900 struct sctp_sock *sp = sctp_sk(sk);
6b3fd5f3
GOV
2901 struct sctp_association *asoc;
2902 struct sctp_sndrcvinfo info;
1da177e4 2903
6b3fd5f3 2904 if (optlen != sizeof(info))
1da177e4
LT
2905 return -EINVAL;
2906 if (copy_from_user(&info, optval, optlen))
2907 return -EFAULT;
6b3fd5f3
GOV
2908 if (info.sinfo_flags &
2909 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2910 SCTP_ABORT | SCTP_EOF))
2911 return -EINVAL;
1da177e4
LT
2912
2913 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
707e45b3
XL
2914 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
2915 sctp_style(sk, UDP))
1da177e4 2916 return -EINVAL;
707e45b3 2917
1da177e4
LT
2918 if (asoc) {
2919 asoc->default_stream = info.sinfo_stream;
2920 asoc->default_flags = info.sinfo_flags;
2921 asoc->default_ppid = info.sinfo_ppid;
2922 asoc->default_context = info.sinfo_context;
2923 asoc->default_timetolive = info.sinfo_timetolive;
707e45b3
XL
2924
2925 return 0;
2926 }
2927
1354e72f
MRL
2928 if (sctp_style(sk, TCP))
2929 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2930
707e45b3
XL
2931 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2932 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
1da177e4
LT
2933 sp->default_stream = info.sinfo_stream;
2934 sp->default_flags = info.sinfo_flags;
2935 sp->default_ppid = info.sinfo_ppid;
2936 sp->default_context = info.sinfo_context;
2937 sp->default_timetolive = info.sinfo_timetolive;
2938 }
2939
707e45b3
XL
2940 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2941 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2942 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2943 asoc->default_stream = info.sinfo_stream;
2944 asoc->default_flags = info.sinfo_flags;
2945 asoc->default_ppid = info.sinfo_ppid;
2946 asoc->default_context = info.sinfo_context;
2947 asoc->default_timetolive = info.sinfo_timetolive;
2948 }
2949 }
2950
1da177e4
LT
2951 return 0;
2952}
2953
6b3fd5f3
GOV
2954/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2955 * (SCTP_DEFAULT_SNDINFO)
2956 */
2957static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2958 char __user *optval,
2959 unsigned int optlen)
2960{
2961 struct sctp_sock *sp = sctp_sk(sk);
2962 struct sctp_association *asoc;
2963 struct sctp_sndinfo info;
2964
2965 if (optlen != sizeof(info))
2966 return -EINVAL;
2967 if (copy_from_user(&info, optval, optlen))
2968 return -EFAULT;
2969 if (info.snd_flags &
2970 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2971 SCTP_ABORT | SCTP_EOF))
2972 return -EINVAL;
2973
2974 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
92fc3bd9
XL
2975 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
2976 sctp_style(sk, UDP))
6b3fd5f3 2977 return -EINVAL;
92fc3bd9 2978
6b3fd5f3
GOV
2979 if (asoc) {
2980 asoc->default_stream = info.snd_sid;
2981 asoc->default_flags = info.snd_flags;
2982 asoc->default_ppid = info.snd_ppid;
2983 asoc->default_context = info.snd_context;
92fc3bd9
XL
2984
2985 return 0;
2986 }
2987
a842e65b
XL
2988 if (sctp_style(sk, TCP))
2989 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
2990
92fc3bd9
XL
2991 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
2992 info.snd_assoc_id == SCTP_ALL_ASSOC) {
6b3fd5f3
GOV
2993 sp->default_stream = info.snd_sid;
2994 sp->default_flags = info.snd_flags;
2995 sp->default_ppid = info.snd_ppid;
2996 sp->default_context = info.snd_context;
2997 }
2998
92fc3bd9
XL
2999 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3000 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3001 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3002 asoc->default_stream = info.snd_sid;
3003 asoc->default_flags = info.snd_flags;
3004 asoc->default_ppid = info.snd_ppid;
3005 asoc->default_context = info.snd_context;
3006 }
3007 }
3008
6b3fd5f3
GOV
3009 return 0;
3010}
3011
1da177e4
LT
3012/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3013 *
3014 * Requests that the local SCTP stack use the enclosed peer address as
3015 * the association primary. The enclosed address must be one of the
3016 * association peer's addresses.
3017 */
3018static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
b7058842 3019 unsigned int optlen)
1da177e4
LT
3020{
3021 struct sctp_prim prim;
3022 struct sctp_transport *trans;
2277c7cd
RH
3023 struct sctp_af *af;
3024 int err;
1da177e4
LT
3025
3026 if (optlen != sizeof(struct sctp_prim))
3027 return -EINVAL;
3028
3029 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3030 return -EFAULT;
3031
2277c7cd
RH
3032 /* Allow security module to validate address but need address len. */
3033 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3034 if (!af)
3035 return -EINVAL;
3036
3037 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3038 (struct sockaddr *)&prim.ssp_addr,
3039 af->sockaddr_len);
3040 if (err)
3041 return err;
3042
1da177e4
LT
3043 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3044 if (!trans)
3045 return -EINVAL;
3046
3047 sctp_assoc_set_primary(trans->asoc, trans);
3048
3049 return 0;
3050}
3051
3052/*
3053 * 7.1.5 SCTP_NODELAY
3054 *
3055 * Turn on/off any Nagle-like algorithm. This means that packets are
3056 * generally sent as soon as possible and no unnecessary delays are
3057 * introduced, at the cost of more packets in the network. Expects an
3058 * integer boolean flag.
3059 */
3060static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
b7058842 3061 unsigned int optlen)
1da177e4
LT
3062{
3063 int val;
3064
3065 if (optlen < sizeof(int))
3066 return -EINVAL;
3067 if (get_user(val, (int __user *)optval))
3068 return -EFAULT;
3069
3070 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3071 return 0;
3072}
3073
3074/*
3075 *
3076 * 7.1.1 SCTP_RTOINFO
3077 *
3078 * The protocol parameters used to initialize and bound retransmission
3079 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3080 * and modify these parameters.
3081 * All parameters are time values, in milliseconds. A value of 0, when
3082 * modifying the parameters, indicates that the current value should not
3083 * be changed.
3084 *
3085 */
b7058842
DM
3086static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3087{
1da177e4
LT
3088 struct sctp_rtoinfo rtoinfo;
3089 struct sctp_association *asoc;
85f935d4 3090 unsigned long rto_min, rto_max;
3091 struct sctp_sock *sp = sctp_sk(sk);
1da177e4
LT
3092
3093 if (optlen != sizeof (struct sctp_rtoinfo))
3094 return -EINVAL;
3095
3096 if (copy_from_user(&rtoinfo, optval, optlen))
3097 return -EFAULT;
3098
3099 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3100
3101 /* Set the values to the specific association */
7adb5ed5
XL
3102 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3103 sctp_style(sk, UDP))
1da177e4
LT
3104 return -EINVAL;
3105
85f935d4 3106 rto_max = rtoinfo.srto_max;
3107 rto_min = rtoinfo.srto_min;
3108
3109 if (rto_max)
3110 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3111 else
3112 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3113
3114 if (rto_min)
3115 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3116 else
3117 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3118
3119 if (rto_min > rto_max)
3120 return -EINVAL;
3121
1da177e4
LT
3122 if (asoc) {
3123 if (rtoinfo.srto_initial != 0)
d808ad9a 3124 asoc->rto_initial =
1da177e4 3125 msecs_to_jiffies(rtoinfo.srto_initial);
85f935d4 3126 asoc->rto_max = rto_max;
3127 asoc->rto_min = rto_min;
1da177e4
LT
3128 } else {
3129 /* If there is no association or the association-id = 0
3130 * set the values to the endpoint.
3131 */
1da177e4
LT
3132 if (rtoinfo.srto_initial != 0)
3133 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
85f935d4 3134 sp->rtoinfo.srto_max = rto_max;
3135 sp->rtoinfo.srto_min = rto_min;
1da177e4
LT
3136 }
3137
3138 return 0;
3139}
3140
3141/*
3142 *
3143 * 7.1.2 SCTP_ASSOCINFO
3144 *
59c51591 3145 * This option is used to tune the maximum retransmission attempts
1da177e4
LT
3146 * of the association.
3147 * Returns an error if the new association retransmission value is
3148 * greater than the sum of the retransmission value of the peer.
3149 * See [SCTP] for more information.
3150 *
3151 */
b7058842 3152static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
3153{
3154
3155 struct sctp_assocparams assocparams;
3156 struct sctp_association *asoc;
3157
3158 if (optlen != sizeof(struct sctp_assocparams))
3159 return -EINVAL;
3160 if (copy_from_user(&assocparams, optval, optlen))
3161 return -EFAULT;
3162
3163 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3164
8889394d
XL
3165 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3166 sctp_style(sk, UDP))
1da177e4
LT
3167 return -EINVAL;
3168
3169 /* Set the values to the specific association */
3170 if (asoc) {
402d68c4
VY
3171 if (assocparams.sasoc_asocmaxrxt != 0) {
3172 __u32 path_sum = 0;
3173 int paths = 0;
402d68c4
VY
3174 struct sctp_transport *peer_addr;
3175
9dbc15f0
RD
3176 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3177 transports) {
402d68c4
VY
3178 path_sum += peer_addr->pathmaxrxt;
3179 paths++;
3180 }
3181
025dfdaf 3182 /* Only validate asocmaxrxt if we have more than
402d68c4
VY
3183 * one path/transport. We do this because path
3184 * retransmissions are only counted when we have more
3185 * then one path.
3186 */
3187 if (paths > 1 &&
3188 assocparams.sasoc_asocmaxrxt > path_sum)
3189 return -EINVAL;
3190
1da177e4 3191 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
402d68c4
VY
3192 }
3193
52db882f
DB
3194 if (assocparams.sasoc_cookie_life != 0)
3195 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
1da177e4
LT
3196 } else {
3197 /* Set the values to the endpoint */
3198 struct sctp_sock *sp = sctp_sk(sk);
3199
3200 if (assocparams.sasoc_asocmaxrxt != 0)
3201 sp->assocparams.sasoc_asocmaxrxt =
3202 assocparams.sasoc_asocmaxrxt;
3203 if (assocparams.sasoc_cookie_life != 0)
3204 sp->assocparams.sasoc_cookie_life =
3205 assocparams.sasoc_cookie_life;
3206 }
3207 return 0;
3208}
3209
3210/*
3211 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3212 *
3213 * This socket option is a boolean flag which turns on or off mapped V4
3214 * addresses. If this option is turned on and the socket is type
3215 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3216 * If this option is turned off, then no mapping will be done of V4
3217 * addresses and a user will receive both PF_INET6 and PF_INET type
3218 * addresses on the socket.
3219 */
b7058842 3220static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
3221{
3222 int val;
3223 struct sctp_sock *sp = sctp_sk(sk);
3224
3225 if (optlen < sizeof(int))
3226 return -EINVAL;
3227 if (get_user(val, (int __user *)optval))
3228 return -EFAULT;
3229 if (val)
3230 sp->v4mapped = 1;
3231 else
3232 sp->v4mapped = 0;
3233
3234 return 0;
3235}
3236
3237/*
e89c2095
WY
3238 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3239 * This option will get or set the maximum size to put in any outgoing
3240 * SCTP DATA chunk. If a message is larger than this size it will be
1da177e4
LT
3241 * fragmented by SCTP into the specified size. Note that the underlying
3242 * SCTP implementation may fragment into smaller sized chunks when the
3243 * PMTU of the underlying association is smaller than the value set by
e89c2095
WY
3244 * the user. The default value for this option is '0' which indicates
3245 * the user is NOT limiting fragmentation and only the PMTU will effect
3246 * SCTP's choice of DATA chunk size. Note also that values set larger
3247 * than the maximum size of an IP datagram will effectively let SCTP
3248 * control fragmentation (i.e. the same as setting this option to 0).
3249 *
3250 * The following structure is used to access and modify this parameter:
3251 *
3252 * struct sctp_assoc_value {
3253 * sctp_assoc_t assoc_id;
3254 * uint32_t assoc_value;
3255 * };
3256 *
3257 * assoc_id: This parameter is ignored for one-to-one style sockets.
3258 * For one-to-many style sockets this parameter indicates which
3259 * association the user is performing an action upon. Note that if
3260 * this field's value is zero then the endpoints default value is
3261 * changed (effecting future associations only).
3262 * assoc_value: This parameter specifies the maximum size in bytes.
1da177e4 3263 */
b7058842 3264static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4 3265{
ecca8f88 3266 struct sctp_sock *sp = sctp_sk(sk);
e89c2095 3267 struct sctp_assoc_value params;
1da177e4 3268 struct sctp_association *asoc;
1da177e4
LT
3269 int val;
3270
e89c2095 3271 if (optlen == sizeof(int)) {
94f65193 3272 pr_warn_ratelimited(DEPRECATED
f916ec96 3273 "%s (pid %d) "
94f65193 3274 "Use of int in maxseg socket option.\n"
f916ec96
NH
3275 "Use struct sctp_assoc_value instead\n",
3276 current->comm, task_pid_nr(current));
e89c2095
WY
3277 if (copy_from_user(&val, optval, optlen))
3278 return -EFAULT;
6fd769be 3279 params.assoc_id = SCTP_FUTURE_ASSOC;
e89c2095
WY
3280 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3281 if (copy_from_user(&params, optval, optlen))
3282 return -EFAULT;
3283 val = params.assoc_value;
ecca8f88 3284 } else {
1da177e4 3285 return -EINVAL;
ecca8f88 3286 }
e89c2095 3287
439ef030 3288 asoc = sctp_id2assoc(sk, params.assoc_id);
6fd769be
XL
3289 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3290 sctp_style(sk, UDP))
3291 return -EINVAL;
439ef030 3292
ecca8f88
XL
3293 if (val) {
3294 int min_len, max_len;
439ef030
MRL
3295 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3296 sizeof(struct sctp_data_chunk);
1da177e4 3297
afd0a800 3298 min_len = sctp_min_frag_point(sp, datasize);
439ef030 3299 max_len = SCTP_MAX_CHUNK_LEN - datasize;
e89c2095 3300
ecca8f88
XL
3301 if (val < min_len || val > max_len)
3302 return -EINVAL;
3303 }
3304
e89c2095 3305 if (asoc) {
f68b2e05 3306 asoc->user_frag = val;
2f5e3c9d 3307 sctp_assoc_update_frag_point(asoc);
e89c2095
WY
3308 } else {
3309 sp->user_frag = val;
1da177e4
LT
3310 }
3311
3312 return 0;
3313}
3314
3315
3316/*
3317 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3318 *
3319 * Requests that the peer mark the enclosed address as the association
3320 * primary. The enclosed address must be one of the association's
3321 * locally bound addresses. The following structure is used to make a
3322 * set primary request:
3323 */
3324static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
b7058842 3325 unsigned int optlen)
1da177e4
LT
3326{
3327 struct sctp_sock *sp;
1da177e4
LT
3328 struct sctp_association *asoc = NULL;
3329 struct sctp_setpeerprim prim;
3330 struct sctp_chunk *chunk;
40a01039 3331 struct sctp_af *af;
1da177e4
LT
3332 int err;
3333
3334 sp = sctp_sk(sk);
1da177e4 3335
4e27428f 3336 if (!sp->ep->asconf_enable)
1da177e4
LT
3337 return -EPERM;
3338
3339 if (optlen != sizeof(struct sctp_setpeerprim))
3340 return -EINVAL;
3341
3342 if (copy_from_user(&prim, optval, optlen))
3343 return -EFAULT;
3344
3345 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
d808ad9a 3346 if (!asoc)
1da177e4
LT
3347 return -EINVAL;
3348
3349 if (!asoc->peer.asconf_capable)
3350 return -EPERM;
3351
3352 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3353 return -EPERM;
3354
3355 if (!sctp_state(asoc, ESTABLISHED))
3356 return -ENOTCONN;
3357
40a01039
WY
3358 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3359 if (!af)
3360 return -EINVAL;
3361
3362 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3363 return -EADDRNOTAVAIL;
3364
1da177e4
LT
3365 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3366 return -EADDRNOTAVAIL;
3367
2277c7cd
RH
3368 /* Allow security module to validate address. */
3369 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3370 (struct sockaddr *)&prim.sspp_addr,
3371 af->sockaddr_len);
3372 if (err)
3373 return err;
3374
1da177e4
LT
3375 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3376 chunk = sctp_make_asconf_set_prim(asoc,
3377 (union sctp_addr *)&prim.sspp_addr);
3378 if (!chunk)
3379 return -ENOMEM;
3380
3381 err = sctp_send_asconf(asoc, chunk);
3382
bb33381d 3383 pr_debug("%s: we set peer primary addr primitively\n", __func__);
1da177e4
LT
3384
3385 return err;
3386}
3387
0f3fffd8 3388static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
b7058842 3389 unsigned int optlen)
1da177e4 3390{
0f3fffd8 3391 struct sctp_setadaptation adaptation;
1da177e4 3392
0f3fffd8 3393 if (optlen != sizeof(struct sctp_setadaptation))
1da177e4 3394 return -EINVAL;
0f3fffd8 3395 if (copy_from_user(&adaptation, optval, optlen))
1da177e4
LT
3396 return -EFAULT;
3397
0f3fffd8 3398 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
1da177e4
LT
3399
3400 return 0;
3401}
3402
6ab792f5
ISJ
3403/*
3404 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3405 *
3406 * The context field in the sctp_sndrcvinfo structure is normally only
3407 * used when a failed message is retrieved holding the value that was
3408 * sent down on the actual send call. This option allows the setting of
3409 * a default context on an association basis that will be received on
3410 * reading messages from the peer. This is especially helpful in the
3411 * one-2-many model for an application to keep some reference to an
3412 * internal state machine that is processing messages on the
3413 * association. Note that the setting of this value only effects
3414 * received messages from the peer and does not effect the value that is
3415 * saved with outbound messages.
3416 */
3417static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
b7058842 3418 unsigned int optlen)
6ab792f5 3419{
49b037ac 3420 struct sctp_sock *sp = sctp_sk(sk);
6ab792f5 3421 struct sctp_assoc_value params;
6ab792f5
ISJ
3422 struct sctp_association *asoc;
3423
3424 if (optlen != sizeof(struct sctp_assoc_value))
3425 return -EINVAL;
3426 if (copy_from_user(&params, optval, optlen))
3427 return -EFAULT;
3428
49b037ac
XL
3429 asoc = sctp_id2assoc(sk, params.assoc_id);
3430 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3431 sctp_style(sk, UDP))
3432 return -EINVAL;
6ab792f5 3433
49b037ac 3434 if (asoc) {
6ab792f5 3435 asoc->default_rcv_context = params.assoc_value;
49b037ac
XL
3436
3437 return 0;
6ab792f5
ISJ
3438 }
3439
cface2cb
XL
3440 if (sctp_style(sk, TCP))
3441 params.assoc_id = SCTP_FUTURE_ASSOC;
3442
49b037ac
XL
3443 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3444 params.assoc_id == SCTP_ALL_ASSOC)
3445 sp->default_rcv_context = params.assoc_value;
3446
3447 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3448 params.assoc_id == SCTP_ALL_ASSOC)
3449 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3450 asoc->default_rcv_context = params.assoc_value;
3451
6ab792f5
ISJ
3452 return 0;
3453}
3454
b6e1331f
VY
3455/*
3456 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3457 *
3458 * This options will at a minimum specify if the implementation is doing
3459 * fragmented interleave. Fragmented interleave, for a one to many
3460 * socket, is when subsequent calls to receive a message may return
3461 * parts of messages from different associations. Some implementations
3462 * may allow you to turn this value on or off. If so, when turned off,
3463 * no fragment interleave will occur (which will cause a head of line
3464 * blocking amongst multiple associations sharing the same one to many
3465 * socket). When this option is turned on, then each receive call may
3466 * come from a different association (thus the user must receive data
3467 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3468 * association each receive belongs to.
3469 *
3470 * This option takes a boolean value. A non-zero value indicates that
3471 * fragmented interleave is on. A value of zero indicates that
3472 * fragmented interleave is off.
3473 *
3474 * Note that it is important that an implementation that allows this
3475 * option to be turned on, have it off by default. Otherwise an unaware
3476 * application using the one to many model may become confused and act
3477 * incorrectly.
3478 */
3479static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3480 char __user *optval,
b7058842 3481 unsigned int optlen)
b6e1331f
VY
3482{
3483 int val;
3484
3485 if (optlen != sizeof(int))
3486 return -EINVAL;
3487 if (get_user(val, (int __user *)optval))
3488 return -EFAULT;
3489
772a5869
XL
3490 sctp_sk(sk)->frag_interleave = !!val;
3491
3492 if (!sctp_sk(sk)->frag_interleave)
e55f4b8b 3493 sctp_sk(sk)->ep->intl_enable = 0;
b6e1331f
VY
3494
3495 return 0;
3496}
3497
d49d91d7 3498/*
8510b937 3499 * 8.1.21. Set or Get the SCTP Partial Delivery Point
d49d91d7 3500 * (SCTP_PARTIAL_DELIVERY_POINT)
8510b937 3501 *
d49d91d7
VY
3502 * This option will set or get the SCTP partial delivery point. This
3503 * point is the size of a message where the partial delivery API will be
3504 * invoked to help free up rwnd space for the peer. Setting this to a
8510b937 3505 * lower value will cause partial deliveries to happen more often. The
d49d91d7 3506 * calls argument is an integer that sets or gets the partial delivery
8510b937
WY
3507 * point. Note also that the call will fail if the user attempts to set
3508 * this value larger than the socket receive buffer size.
3509 *
3510 * Note that any single message having a length smaller than or equal to
3511 * the SCTP partial delivery point will be delivered in one single read
3512 * call as long as the user provided buffer is large enough to hold the
3513 * message.
d49d91d7
VY
3514 */
3515static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3516 char __user *optval,
b7058842 3517 unsigned int optlen)
d49d91d7
VY
3518{
3519 u32 val;
3520
3521 if (optlen != sizeof(u32))
3522 return -EINVAL;
3523 if (get_user(val, (int __user *)optval))
3524 return -EFAULT;
3525
8510b937
WY
3526 /* Note: We double the receive buffer from what the user sets
3527 * it to be, also initial rwnd is based on rcvbuf/2.
3528 */
3529 if (val > (sk->sk_rcvbuf >> 1))
3530 return -EINVAL;
3531
d49d91d7
VY
3532 sctp_sk(sk)->pd_point = val;
3533
3534 return 0; /* is this the right error code? */
3535}
3536
70331571
VY
3537/*
3538 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3539 *
3540 * This option will allow a user to change the maximum burst of packets
3541 * that can be emitted by this association. Note that the default value
3542 * is 4, and some implementations may restrict this setting so that it
3543 * can only be lowered.
3544 *
3545 * NOTE: This text doesn't seem right. Do this on a socket basis with
3546 * future associations inheriting the socket value.
3547 */
3548static int sctp_setsockopt_maxburst(struct sock *sk,
3549 char __user *optval,
b7058842 3550 unsigned int optlen)
70331571 3551{
e0651a0d 3552 struct sctp_sock *sp = sctp_sk(sk);
219b99a9 3553 struct sctp_assoc_value params;
219b99a9 3554 struct sctp_association *asoc;
70331571 3555
219b99a9 3556 if (optlen == sizeof(int)) {
94f65193 3557 pr_warn_ratelimited(DEPRECATED
f916ec96 3558 "%s (pid %d) "
94f65193 3559 "Use of int in max_burst socket option deprecated.\n"
f916ec96
NH
3560 "Use struct sctp_assoc_value instead\n",
3561 current->comm, task_pid_nr(current));
e0651a0d 3562 if (copy_from_user(&params.assoc_value, optval, optlen))
219b99a9 3563 return -EFAULT;
e0651a0d 3564 params.assoc_id = SCTP_FUTURE_ASSOC;
219b99a9
NH
3565 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3566 if (copy_from_user(&params, optval, optlen))
3567 return -EFAULT;
219b99a9 3568 } else
70331571
VY
3569 return -EINVAL;
3570
e0651a0d
XL
3571 asoc = sctp_id2assoc(sk, params.assoc_id);
3572 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3573 sctp_style(sk, UDP))
3574 return -EINVAL;
219b99a9 3575
e0651a0d
XL
3576 if (asoc) {
3577 asoc->max_burst = params.assoc_value;
3578
3579 return 0;
3580 }
3581
746bc215
XL
3582 if (sctp_style(sk, TCP))
3583 params.assoc_id = SCTP_FUTURE_ASSOC;
3584
e0651a0d
XL
3585 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3586 params.assoc_id == SCTP_ALL_ASSOC)
3587 sp->max_burst = params.assoc_value;
3588
3589 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3590 params.assoc_id == SCTP_ALL_ASSOC)
3591 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3592 asoc->max_burst = params.assoc_value;
70331571
VY
3593
3594 return 0;
3595}
3596
65b07e5d
VY
3597/*
3598 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3599 *
3600 * This set option adds a chunk type that the user is requesting to be
3601 * received only in an authenticated way. Changes to the list of chunks
3602 * will only effect future associations on the socket.
3603 */
3604static int sctp_setsockopt_auth_chunk(struct sock *sk,
b7058842
DM
3605 char __user *optval,
3606 unsigned int optlen)
65b07e5d 3607{
b14878cc 3608 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3609 struct sctp_authchunk val;
3610
b14878cc 3611 if (!ep->auth_enable)
5e739d17
VY
3612 return -EACCES;
3613
65b07e5d
VY
3614 if (optlen != sizeof(struct sctp_authchunk))
3615 return -EINVAL;
3616 if (copy_from_user(&val, optval, optlen))
3617 return -EFAULT;
3618
3619 switch (val.sauth_chunk) {
7fd71b1e
JP
3620 case SCTP_CID_INIT:
3621 case SCTP_CID_INIT_ACK:
3622 case SCTP_CID_SHUTDOWN_COMPLETE:
3623 case SCTP_CID_AUTH:
3624 return -EINVAL;
65b07e5d
VY
3625 }
3626
3627 /* add this chunk id to the endpoint */
b14878cc 3628 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
65b07e5d
VY
3629}
3630
3631/*
3632 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3633 *
3634 * This option gets or sets the list of HMAC algorithms that the local
3635 * endpoint requires the peer to use.
3636 */
3637static int sctp_setsockopt_hmac_ident(struct sock *sk,
b7058842
DM
3638 char __user *optval,
3639 unsigned int optlen)
65b07e5d 3640{
b14878cc 3641 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d 3642 struct sctp_hmacalgo *hmacs;
d9724055 3643 u32 idents;
65b07e5d
VY
3644 int err;
3645
b14878cc 3646 if (!ep->auth_enable)
5e739d17
VY
3647 return -EACCES;
3648
65b07e5d
VY
3649 if (optlen < sizeof(struct sctp_hmacalgo))
3650 return -EINVAL;
5960cefa
MRL
3651 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3652 SCTP_AUTH_NUM_HMACS * sizeof(u16));
65b07e5d 3653
cb3f837b 3654 hmacs = memdup_user(optval, optlen);
934253a7
SW
3655 if (IS_ERR(hmacs))
3656 return PTR_ERR(hmacs);
65b07e5d 3657
d9724055
VY
3658 idents = hmacs->shmac_num_idents;
3659 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3660 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
65b07e5d
VY
3661 err = -EINVAL;
3662 goto out;
3663 }
3664
b14878cc 3665 err = sctp_auth_ep_set_hmacs(ep, hmacs);
65b07e5d
VY
3666out:
3667 kfree(hmacs);
3668 return err;
3669}
3670
3671/*
3672 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3673 *
3674 * This option will set a shared secret key which is used to build an
3675 * association shared key.
3676 */
3677static int sctp_setsockopt_auth_key(struct sock *sk,
3678 char __user *optval,
b7058842 3679 unsigned int optlen)
65b07e5d 3680{
b14878cc 3681 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3682 struct sctp_authkey *authkey;
3683 struct sctp_association *asoc;
7fb3be13 3684 int ret = -EINVAL;
65b07e5d
VY
3685
3686 if (optlen <= sizeof(struct sctp_authkey))
3687 return -EINVAL;
5960cefa
MRL
3688 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3689 * this.
3690 */
7fb3be13 3691 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
65b07e5d 3692
cb3f837b 3693 authkey = memdup_user(optval, optlen);
934253a7
SW
3694 if (IS_ERR(authkey))
3695 return PTR_ERR(authkey);
65b07e5d 3696
7fb3be13 3697 if (authkey->sca_keylength > optlen - sizeof(*authkey))
30c2235c 3698 goto out;
30c2235c 3699
65b07e5d 3700 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
7fb3be13
XL
3701 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3702 sctp_style(sk, UDP))
65b07e5d 3703 goto out;
7fb3be13
XL
3704
3705 if (asoc) {
3706 ret = sctp_auth_set_key(ep, asoc, authkey);
3707 goto out;
3708 }
3709
0685d6b7
XL
3710 if (sctp_style(sk, TCP))
3711 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3712
7fb3be13
XL
3713 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3714 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3715 ret = sctp_auth_set_key(ep, asoc, authkey);
3716 if (ret)
3717 goto out;
3718 }
3719
3720 ret = 0;
3721
3722 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3723 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3724 list_for_each_entry(asoc, &ep->asocs, asocs) {
3725 int res = sctp_auth_set_key(ep, asoc, authkey);
3726
3727 if (res && !ret)
3728 ret = res;
3729 }
65b07e5d
VY
3730 }
3731
65b07e5d 3732out:
6ba542a2 3733 kzfree(authkey);
65b07e5d
VY
3734 return ret;
3735}
3736
3737/*
3738 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3739 *
3740 * This option will get or set the active shared key to be used to build
3741 * the association shared key.
3742 */
3743static int sctp_setsockopt_active_key(struct sock *sk,
b7058842
DM
3744 char __user *optval,
3745 unsigned int optlen)
65b07e5d 3746{
b14878cc 3747 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d 3748 struct sctp_association *asoc;
bf9fb6ad
XL
3749 struct sctp_authkeyid val;
3750 int ret = 0;
65b07e5d
VY
3751
3752 if (optlen != sizeof(struct sctp_authkeyid))
3753 return -EINVAL;
3754 if (copy_from_user(&val, optval, optlen))
3755 return -EFAULT;
3756
3757 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
bf9fb6ad
XL
3758 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3759 sctp_style(sk, UDP))
65b07e5d
VY
3760 return -EINVAL;
3761
bf9fb6ad
XL
3762 if (asoc)
3763 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3764
06b39e85
XL
3765 if (sctp_style(sk, TCP))
3766 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3767
bf9fb6ad
XL
3768 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3769 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3770 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3771 if (ret)
3772 return ret;
3773 }
3774
3775 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3776 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3777 list_for_each_entry(asoc, &ep->asocs, asocs) {
3778 int res = sctp_auth_set_active_key(ep, asoc,
3779 val.scact_keynumber);
3780
3781 if (res && !ret)
3782 ret = res;
3783 }
3784 }
3785
3786 return ret;
65b07e5d
VY
3787}
3788
3789/*
3790 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3791 *
3792 * This set option will delete a shared secret key from use.
3793 */
3794static int sctp_setsockopt_del_key(struct sock *sk,
b7058842
DM
3795 char __user *optval,
3796 unsigned int optlen)
65b07e5d 3797{
b14878cc 3798 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d 3799 struct sctp_association *asoc;
3adcc300
XL
3800 struct sctp_authkeyid val;
3801 int ret = 0;
65b07e5d
VY
3802
3803 if (optlen != sizeof(struct sctp_authkeyid))
3804 return -EINVAL;
3805 if (copy_from_user(&val, optval, optlen))
3806 return -EFAULT;
3807
3808 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3adcc300
XL
3809 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3810 sctp_style(sk, UDP))
65b07e5d
VY
3811 return -EINVAL;
3812
3adcc300
XL
3813 if (asoc)
3814 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
65b07e5d 3815
220675eb
XL
3816 if (sctp_style(sk, TCP))
3817 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3818
3adcc300
XL
3819 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3820 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3821 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3822 if (ret)
3823 return ret;
3824 }
3825
3826 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3827 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3828 list_for_each_entry(asoc, &ep->asocs, asocs) {
3829 int res = sctp_auth_del_key_id(ep, asoc,
3830 val.scact_keynumber);
3831
3832 if (res && !ret)
3833 ret = res;
3834 }
3835 }
3836
3837 return ret;
65b07e5d
VY
3838}
3839
601590ec
XL
3840/*
3841 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3842 *
3843 * This set option will deactivate a shared secret key.
3844 */
3845static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3846 unsigned int optlen)
3847{
3848 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
601590ec 3849 struct sctp_association *asoc;
2af66ff3
XL
3850 struct sctp_authkeyid val;
3851 int ret = 0;
601590ec 3852
601590ec
XL
3853 if (optlen != sizeof(struct sctp_authkeyid))
3854 return -EINVAL;
3855 if (copy_from_user(&val, optval, optlen))
3856 return -EFAULT;
3857
3858 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
2af66ff3
XL
3859 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3860 sctp_style(sk, UDP))
601590ec
XL
3861 return -EINVAL;
3862
2af66ff3
XL
3863 if (asoc)
3864 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3865
200f3a3b
XL
3866 if (sctp_style(sk, TCP))
3867 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3868
2af66ff3
XL
3869 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3870 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3871 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3872 if (ret)
3873 return ret;
3874 }
3875
3876 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3877 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3878 list_for_each_entry(asoc, &ep->asocs, asocs) {
3879 int res = sctp_auth_deact_key_id(ep, asoc,
3880 val.scact_keynumber);
3881
3882 if (res && !ret)
3883 ret = res;
3884 }
3885 }
3886
3887 return ret;
601590ec
XL
3888}
3889
7dc04d71
MH
3890/*
3891 * 8.1.23 SCTP_AUTO_ASCONF
3892 *
3893 * This option will enable or disable the use of the automatic generation of
3894 * ASCONF chunks to add and delete addresses to an existing association. Note
3895 * that this option has two caveats namely: a) it only affects sockets that
3896 * are bound to all addresses available to the SCTP stack, and b) the system
3897 * administrator may have an overriding control that turns the ASCONF feature
3898 * off no matter what setting the socket option may have.
3899 * This option expects an integer boolean flag, where a non-zero value turns on
3900 * the option, and a zero value turns off the option.
3901 * Note. In this implementation, socket operation overrides default parameter
3902 * being set by sysctl as well as FreeBSD implementation
3903 */
3904static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3905 unsigned int optlen)
3906{
3907 int val;
3908 struct sctp_sock *sp = sctp_sk(sk);
3909
3910 if (optlen < sizeof(int))
3911 return -EINVAL;
3912 if (get_user(val, (int __user *)optval))
3913 return -EFAULT;
3914 if (!sctp_is_ep_boundall(sk) && val)
3915 return -EINVAL;
3916 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3917 return 0;
3918
2d45a02d 3919 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
7dc04d71
MH
3920 if (val == 0 && sp->do_auto_asconf) {
3921 list_del(&sp->auto_asconf_list);
3922 sp->do_auto_asconf = 0;
3923 } else if (val && !sp->do_auto_asconf) {
3924 list_add_tail(&sp->auto_asconf_list,
4db67e80 3925 &sock_net(sk)->sctp.auto_asconf_splist);
7dc04d71
MH
3926 sp->do_auto_asconf = 1;
3927 }
2d45a02d 3928 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
7dc04d71
MH
3929 return 0;
3930}
3931
5aa93bcf
NH
3932/*
3933 * SCTP_PEER_ADDR_THLDS
3934 *
3935 * This option allows us to alter the partially failed threshold for one or all
3936 * transports in an association. See Section 6.1 of:
3937 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3938 */
3939static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3940 char __user *optval,
d467ac0a 3941 unsigned int optlen, bool v2)
5aa93bcf 3942{
d467ac0a 3943 struct sctp_paddrthlds_v2 val;
5aa93bcf
NH
3944 struct sctp_transport *trans;
3945 struct sctp_association *asoc;
d467ac0a 3946 int len;
5aa93bcf 3947
d467ac0a
XL
3948 len = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
3949 if (optlen < len)
5aa93bcf 3950 return -EINVAL;
d467ac0a 3951 if (copy_from_user(&val, optval, len))
5aa93bcf
NH
3952 return -EFAULT;
3953
d467ac0a
XL
3954 if (v2 && val.spt_pathpfthld > val.spt_pathcpthld)
3955 return -EINVAL;
3956
8add543e
XL
3957 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3958 trans = sctp_addr_id2transport(sk, &val.spt_address,
3959 val.spt_assoc_id);
3960 if (!trans)
5aa93bcf 3961 return -ENOENT;
8add543e
XL
3962
3963 if (val.spt_pathmaxrxt)
3964 trans->pathmaxrxt = val.spt_pathmaxrxt;
d467ac0a
XL
3965 if (v2)
3966 trans->ps_retrans = val.spt_pathcpthld;
8add543e
XL
3967 trans->pf_retrans = val.spt_pathpfthld;
3968
3969 return 0;
3970 }
3971
3972 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3973 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
3974 sctp_style(sk, UDP))
3975 return -EINVAL;
3976
3977 if (asoc) {
5aa93bcf
NH
3978 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3979 transports) {
3980 if (val.spt_pathmaxrxt)
3981 trans->pathmaxrxt = val.spt_pathmaxrxt;
d467ac0a
XL
3982 if (v2)
3983 trans->ps_retrans = val.spt_pathcpthld;
5aa93bcf
NH
3984 trans->pf_retrans = val.spt_pathpfthld;
3985 }
3986
3987 if (val.spt_pathmaxrxt)
3988 asoc->pathmaxrxt = val.spt_pathmaxrxt;
d467ac0a
XL
3989 if (v2)
3990 asoc->ps_retrans = val.spt_pathcpthld;
5aa93bcf
NH
3991 asoc->pf_retrans = val.spt_pathpfthld;
3992 } else {
8add543e 3993 struct sctp_sock *sp = sctp_sk(sk);
5aa93bcf
NH
3994
3995 if (val.spt_pathmaxrxt)
8add543e 3996 sp->pathmaxrxt = val.spt_pathmaxrxt;
d467ac0a
XL
3997 if (v2)
3998 sp->ps_retrans = val.spt_pathcpthld;
8add543e 3999 sp->pf_retrans = val.spt_pathpfthld;
5aa93bcf
NH
4000 }
4001
4002 return 0;
4003}
4004
0d3a421d
GOV
4005static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4006 char __user *optval,
4007 unsigned int optlen)
4008{
4009 int val;
4010
4011 if (optlen < sizeof(int))
4012 return -EINVAL;
4013 if (get_user(val, (int __user *) optval))
4014 return -EFAULT;
4015
4016 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4017
4018 return 0;
4019}
4020
2347c80f
GOV
4021static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4022 char __user *optval,
4023 unsigned int optlen)
4024{
4025 int val;
4026
4027 if (optlen < sizeof(int))
4028 return -EINVAL;
4029 if (get_user(val, (int __user *) optval))
4030 return -EFAULT;
4031
4032 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4033
4034 return 0;
4035}
4036
28aa4c26
XL
4037static int sctp_setsockopt_pr_supported(struct sock *sk,
4038 char __user *optval,
4039 unsigned int optlen)
4040{
4041 struct sctp_assoc_value params;
fb195605 4042 struct sctp_association *asoc;
28aa4c26
XL
4043
4044 if (optlen != sizeof(params))
cc3ccf26 4045 return -EINVAL;
28aa4c26 4046
cc3ccf26
XL
4047 if (copy_from_user(&params, optval, optlen))
4048 return -EFAULT;
28aa4c26 4049
fb195605
XL
4050 asoc = sctp_id2assoc(sk, params.assoc_id);
4051 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4052 sctp_style(sk, UDP))
4053 return -EINVAL;
4054
cc3ccf26 4055 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
28aa4c26 4056
cc3ccf26 4057 return 0;
28aa4c26
XL
4058}
4059
f959fb44
XL
4060static int sctp_setsockopt_default_prinfo(struct sock *sk,
4061 char __user *optval,
4062 unsigned int optlen)
4063{
3a583059 4064 struct sctp_sock *sp = sctp_sk(sk);
f959fb44
XL
4065 struct sctp_default_prinfo info;
4066 struct sctp_association *asoc;
4067 int retval = -EINVAL;
4068
4069 if (optlen != sizeof(info))
4070 goto out;
4071
4072 if (copy_from_user(&info, optval, sizeof(info))) {
4073 retval = -EFAULT;
4074 goto out;
4075 }
4076
4077 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4078 goto out;
4079
4080 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4081 info.pr_value = 0;
4082
4083 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3a583059
XL
4084 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4085 sctp_style(sk, UDP))
4086 goto out;
4087
4088 retval = 0;
4089
f959fb44
XL
4090 if (asoc) {
4091 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4092 asoc->default_timetolive = info.pr_value;
3a583059
XL
4093 goto out;
4094 }
f959fb44 4095
cbb45c6c
XL
4096 if (sctp_style(sk, TCP))
4097 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4098
3a583059
XL
4099 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4100 info.pr_assoc_id == SCTP_ALL_ASSOC) {
f959fb44
XL
4101 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4102 sp->default_timetolive = info.pr_value;
f959fb44
XL
4103 }
4104
3a583059
XL
4105 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4106 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4107 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4108 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4109 asoc->default_timetolive = info.pr_value;
4110 }
4111 }
f959fb44
XL
4112
4113out:
4114 return retval;
4115}
4116
c0d8bab6
XL
4117static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4118 char __user *optval,
4119 unsigned int optlen)
4120{
4121 struct sctp_assoc_value params;
4122 struct sctp_association *asoc;
4123 int retval = -EINVAL;
4124
4125 if (optlen != sizeof(params))
4126 goto out;
4127
4128 if (copy_from_user(&params, optval, optlen)) {
4129 retval = -EFAULT;
4130 goto out;
4131 }
4132
4133 asoc = sctp_id2assoc(sk, params.assoc_id);
acce7f3b
XL
4134 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4135 sctp_style(sk, UDP))
c0d8bab6 4136 goto out;
acce7f3b 4137
a96701fb 4138 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
c0d8bab6
XL
4139
4140 retval = 0;
4141
4142out:
4143 return retval;
4144}
4145
9fb657ae
XL
4146static int sctp_setsockopt_enable_strreset(struct sock *sk,
4147 char __user *optval,
4148 unsigned int optlen)
4149{
99a62135 4150 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
9fb657ae
XL
4151 struct sctp_assoc_value params;
4152 struct sctp_association *asoc;
4153 int retval = -EINVAL;
4154
4155 if (optlen != sizeof(params))
4156 goto out;
4157
4158 if (copy_from_user(&params, optval, optlen)) {
4159 retval = -EFAULT;
4160 goto out;
4161 }
4162
4163 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4164 goto out;
4165
4166 asoc = sctp_id2assoc(sk, params.assoc_id);
99a62135
XL
4167 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4168 sctp_style(sk, UDP))
4169 goto out;
4170
4171 retval = 0;
4172
9fb657ae
XL
4173 if (asoc) {
4174 asoc->strreset_enable = params.assoc_value;
9fb657ae
XL
4175 goto out;
4176 }
4177
9430ff99
XL
4178 if (sctp_style(sk, TCP))
4179 params.assoc_id = SCTP_FUTURE_ASSOC;
4180
99a62135
XL
4181 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4182 params.assoc_id == SCTP_ALL_ASSOC)
4183 ep->strreset_enable = params.assoc_value;
4184
4185 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4186 params.assoc_id == SCTP_ALL_ASSOC)
4187 list_for_each_entry(asoc, &ep->asocs, asocs)
4188 asoc->strreset_enable = params.assoc_value;
9fb657ae
XL
4189
4190out:
4191 return retval;
4192}
4193
7f9d68ac
XL
4194static int sctp_setsockopt_reset_streams(struct sock *sk,
4195 char __user *optval,
4196 unsigned int optlen)
4197{
4198 struct sctp_reset_streams *params;
4199 struct sctp_association *asoc;
4200 int retval = -EINVAL;
4201
2342b8d9 4202 if (optlen < sizeof(*params))
7f9d68ac 4203 return -EINVAL;
5960cefa
MRL
4204 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4205 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4206 sizeof(__u16) * sizeof(*params));
7f9d68ac
XL
4207
4208 params = memdup_user(optval, optlen);
4209 if (IS_ERR(params))
4210 return PTR_ERR(params);
4211
2342b8d9
XL
4212 if (params->srs_number_streams * sizeof(__u16) >
4213 optlen - sizeof(*params))
4214 goto out;
4215
7f9d68ac
XL
4216 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4217 if (!asoc)
4218 goto out;
4219
4220 retval = sctp_send_reset_streams(asoc, params);
4221
4222out:
4223 kfree(params);
4224 return retval;
4225}
4226
a92ce1a4
XL
4227static int sctp_setsockopt_reset_assoc(struct sock *sk,
4228 char __user *optval,
4229 unsigned int optlen)
4230{
4231 struct sctp_association *asoc;
4232 sctp_assoc_t associd;
4233 int retval = -EINVAL;
4234
4235 if (optlen != sizeof(associd))
4236 goto out;
4237
4238 if (copy_from_user(&associd, optval, optlen)) {
4239 retval = -EFAULT;
4240 goto out;
4241 }
4242
4243 asoc = sctp_id2assoc(sk, associd);
4244 if (!asoc)
4245 goto out;
4246
4247 retval = sctp_send_reset_assoc(asoc);
4248
4249out:
4250 return retval;
4251}
4252
242bd2d5
XL
4253static int sctp_setsockopt_add_streams(struct sock *sk,
4254 char __user *optval,
4255 unsigned int optlen)
4256{
4257 struct sctp_association *asoc;
4258 struct sctp_add_streams params;
4259 int retval = -EINVAL;
4260
4261 if (optlen != sizeof(params))
4262 goto out;
4263
4264 if (copy_from_user(&params, optval, optlen)) {
4265 retval = -EFAULT;
4266 goto out;
4267 }
4268
4269 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4270 if (!asoc)
4271 goto out;
4272
4273 retval = sctp_send_add_streams(asoc, &params);
4274
4275out:
4276 return retval;
4277}
4278
13aa8770
MRL
4279static int sctp_setsockopt_scheduler(struct sock *sk,
4280 char __user *optval,
4281 unsigned int optlen)
4282{
7efba10d 4283 struct sctp_sock *sp = sctp_sk(sk);
13aa8770
MRL
4284 struct sctp_association *asoc;
4285 struct sctp_assoc_value params;
7efba10d 4286 int retval = 0;
13aa8770
MRL
4287
4288 if (optlen < sizeof(params))
7efba10d 4289 return -EINVAL;
13aa8770
MRL
4290
4291 optlen = sizeof(params);
7efba10d
XL
4292 if (copy_from_user(&params, optval, optlen))
4293 return -EFAULT;
13aa8770
MRL
4294
4295 if (params.assoc_value > SCTP_SS_MAX)
7efba10d 4296 return -EINVAL;
13aa8770
MRL
4297
4298 asoc = sctp_id2assoc(sk, params.assoc_id);
7efba10d
XL
4299 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4300 sctp_style(sk, UDP))
4301 return -EINVAL;
13aa8770 4302
7efba10d
XL
4303 if (asoc)
4304 return sctp_sched_set_sched(asoc, params.assoc_value);
4305
b59c19d9
XL
4306 if (sctp_style(sk, TCP))
4307 params.assoc_id = SCTP_FUTURE_ASSOC;
4308
7efba10d
XL
4309 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4310 params.assoc_id == SCTP_ALL_ASSOC)
4311 sp->default_ss = params.assoc_value;
4312
4313 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4314 params.assoc_id == SCTP_ALL_ASSOC) {
4315 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4316 int ret = sctp_sched_set_sched(asoc,
4317 params.assoc_value);
4318
4319 if (ret && !retval)
4320 retval = ret;
4321 }
4322 }
13aa8770 4323
13aa8770
MRL
4324 return retval;
4325}
4326
0ccdf3c7
MRL
4327static int sctp_setsockopt_scheduler_value(struct sock *sk,
4328 char __user *optval,
4329 unsigned int optlen)
4330{
0ccdf3c7 4331 struct sctp_stream_value params;
e7f28248 4332 struct sctp_association *asoc;
0ccdf3c7
MRL
4333 int retval = -EINVAL;
4334
4335 if (optlen < sizeof(params))
4336 goto out;
4337
4338 optlen = sizeof(params);
4339 if (copy_from_user(&params, optval, optlen)) {
4340 retval = -EFAULT;
4341 goto out;
4342 }
4343
4344 asoc = sctp_id2assoc(sk, params.assoc_id);
e7f28248
XL
4345 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4346 sctp_style(sk, UDP))
0ccdf3c7
MRL
4347 goto out;
4348
e7f28248
XL
4349 if (asoc) {
4350 retval = sctp_sched_set_value(asoc, params.stream_id,
4351 params.stream_value, GFP_KERNEL);
4352 goto out;
4353 }
4354
4355 retval = 0;
4356
4357 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4358 int ret = sctp_sched_set_value(asoc, params.stream_id,
4359 params.stream_value, GFP_KERNEL);
4360 if (ret && !retval) /* try to return the 1st error. */
4361 retval = ret;
4362 }
0ccdf3c7
MRL
4363
4364out:
4365 return retval;
4366}
4367
772a5869
XL
4368static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4369 char __user *optval,
4370 unsigned int optlen)
4371{
4372 struct sctp_sock *sp = sctp_sk(sk);
772a5869 4373 struct sctp_assoc_value params;
2e7709d1 4374 struct sctp_association *asoc;
772a5869
XL
4375 int retval = -EINVAL;
4376
4377 if (optlen < sizeof(params))
4378 goto out;
4379
4380 optlen = sizeof(params);
4381 if (copy_from_user(&params, optval, optlen)) {
4382 retval = -EFAULT;
4383 goto out;
4384 }
4385
2e7709d1
XL
4386 asoc = sctp_id2assoc(sk, params.assoc_id);
4387 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4388 sctp_style(sk, UDP))
772a5869
XL
4389 goto out;
4390
2e7709d1 4391 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
772a5869
XL
4392 retval = -EPERM;
4393 goto out;
4394 }
4395
e55f4b8b 4396 sp->ep->intl_enable = !!params.assoc_value;
772a5869
XL
4397
4398 retval = 0;
4399
4400out:
4401 return retval;
4402}
4403
b0e9a2fe
XL
4404static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4405 unsigned int optlen)
4406{
4407 int val;
4408
4409 if (!sctp_style(sk, TCP))
4410 return -EOPNOTSUPP;
4411
4412 if (sctp_sk(sk)->ep->base.bind_addr.port)
4413 return -EFAULT;
4414
4415 if (optlen < sizeof(int))
4416 return -EINVAL;
4417
4418 if (get_user(val, (int __user *)optval))
4419 return -EFAULT;
4420
4421 sctp_sk(sk)->reuse = !!val;
4422
4423 return 0;
4424}
4425
d251f05e
XL
4426static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4427 struct sctp_association *asoc)
4428{
4429 struct sctp_ulpevent *event;
4430
4431 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4432
4433 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4434 if (sctp_outq_is_empty(&asoc->outqueue)) {
4435 event = sctp_ulpevent_make_sender_dry_event(asoc,
4436 GFP_USER | __GFP_NOWARN);
4437 if (!event)
4438 return -ENOMEM;
4439
4440 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4441 }
4442 }
4443
4444 return 0;
4445}
4446
480ba9c1
XL
4447static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4448 unsigned int optlen)
4449{
d251f05e 4450 struct sctp_sock *sp = sctp_sk(sk);
480ba9c1 4451 struct sctp_association *asoc;
480ba9c1
XL
4452 struct sctp_event param;
4453 int retval = 0;
4454
d251f05e
XL
4455 if (optlen < sizeof(param))
4456 return -EINVAL;
480ba9c1
XL
4457
4458 optlen = sizeof(param);
d251f05e
XL
4459 if (copy_from_user(&param, optval, optlen))
4460 return -EFAULT;
480ba9c1
XL
4461
4462 if (param.se_type < SCTP_SN_TYPE_BASE ||
d251f05e
XL
4463 param.se_type > SCTP_SN_TYPE_MAX)
4464 return -EINVAL;
480ba9c1
XL
4465
4466 asoc = sctp_id2assoc(sk, param.se_assoc_id);
d251f05e
XL
4467 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4468 sctp_style(sk, UDP))
4469 return -EINVAL;
480ba9c1 4470
d251f05e
XL
4471 if (asoc)
4472 return sctp_assoc_ulpevent_type_set(&param, asoc);
480ba9c1 4473
99518619
XL
4474 if (sctp_style(sk, TCP))
4475 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4476
d251f05e
XL
4477 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4478 param.se_assoc_id == SCTP_ALL_ASSOC)
4479 sctp_ulpevent_type_set(&sp->subscribe,
4480 param.se_type, param.se_on);
480ba9c1 4481
d251f05e
XL
4482 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4483 param.se_assoc_id == SCTP_ALL_ASSOC) {
4484 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4485 int ret = sctp_assoc_ulpevent_type_set(&param, asoc);
4486
4487 if (ret && !retval)
4488 retval = ret;
480ba9c1
XL
4489 }
4490 }
4491
480ba9c1
XL
4492 return retval;
4493}
4494
df2c71ff
XL
4495static int sctp_setsockopt_asconf_supported(struct sock *sk,
4496 char __user *optval,
4497 unsigned int optlen)
4498{
4499 struct sctp_assoc_value params;
4500 struct sctp_association *asoc;
4501 struct sctp_endpoint *ep;
4502 int retval = -EINVAL;
4503
4504 if (optlen != sizeof(params))
4505 goto out;
4506
4507 if (copy_from_user(&params, optval, optlen)) {
4508 retval = -EFAULT;
4509 goto out;
4510 }
4511
4512 asoc = sctp_id2assoc(sk, params.assoc_id);
4513 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4514 sctp_style(sk, UDP))
4515 goto out;
4516
4517 ep = sctp_sk(sk)->ep;
4518 ep->asconf_enable = !!params.assoc_value;
4519
4520 if (ep->asconf_enable && ep->auth_enable) {
4521 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4522 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4523 }
4524
4525 retval = 0;
4526
4527out:
4528 return retval;
4529}
4530
56dd525a
XL
4531static int sctp_setsockopt_auth_supported(struct sock *sk,
4532 char __user *optval,
4533 unsigned int optlen)
4534{
4535 struct sctp_assoc_value params;
4536 struct sctp_association *asoc;
4537 struct sctp_endpoint *ep;
4538 int retval = -EINVAL;
4539
4540 if (optlen != sizeof(params))
4541 goto out;
4542
4543 if (copy_from_user(&params, optval, optlen)) {
4544 retval = -EFAULT;
4545 goto out;
4546 }
4547
4548 asoc = sctp_id2assoc(sk, params.assoc_id);
4549 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4550 sctp_style(sk, UDP))
4551 goto out;
4552
4553 ep = sctp_sk(sk)->ep;
4554 if (params.assoc_value) {
4555 retval = sctp_auth_init(ep, GFP_KERNEL);
4556 if (retval)
4557 goto out;
4558 if (ep->asconf_enable) {
4559 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4560 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4561 }
4562 }
4563
4564 ep->auth_enable = !!params.assoc_value;
4565 retval = 0;
4566
4567out:
4568 return retval;
4569}
4570
d5886b91
XL
4571static int sctp_setsockopt_ecn_supported(struct sock *sk,
4572 char __user *optval,
4573 unsigned int optlen)
4574{
4575 struct sctp_assoc_value params;
4576 struct sctp_association *asoc;
4577 int retval = -EINVAL;
4578
4579 if (optlen != sizeof(params))
4580 goto out;
4581
4582 if (copy_from_user(&params, optval, optlen)) {
4583 retval = -EFAULT;
4584 goto out;
4585 }
4586
4587 asoc = sctp_id2assoc(sk, params.assoc_id);
4588 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4589 sctp_style(sk, UDP))
4590 goto out;
4591
4592 sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
4593 retval = 0;
4594
4595out:
4596 return retval;
4597}
4598
8d2a6935
XL
4599static int sctp_setsockopt_pf_expose(struct sock *sk,
4600 char __user *optval,
4601 unsigned int optlen)
4602{
4603 struct sctp_assoc_value params;
4604 struct sctp_association *asoc;
4605 int retval = -EINVAL;
4606
4607 if (optlen != sizeof(params))
4608 goto out;
4609
4610 if (copy_from_user(&params, optval, optlen)) {
4611 retval = -EFAULT;
4612 goto out;
4613 }
4614
4615 if (params.assoc_value > SCTP_PF_EXPOSE_MAX)
4616 goto out;
4617
4618 asoc = sctp_id2assoc(sk, params.assoc_id);
4619 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4620 sctp_style(sk, UDP))
4621 goto out;
4622
4623 if (asoc)
4624 asoc->pf_expose = params.assoc_value;
4625 else
4626 sctp_sk(sk)->pf_expose = params.assoc_value;
4627 retval = 0;
4628
4629out:
4630 return retval;
4631}
4632
1da177e4
LT
4633/* API 6.2 setsockopt(), getsockopt()
4634 *
4635 * Applications use setsockopt() and getsockopt() to set or retrieve
4636 * socket options. Socket options are used to change the default
4637 * behavior of sockets calls. They are described in Section 7.
4638 *
4639 * The syntax is:
4640 *
4641 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4642 * int __user *optlen);
4643 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4644 * int optlen);
4645 *
4646 * sd - the socket descript.
4647 * level - set to IPPROTO_SCTP for all SCTP options.
4648 * optname - the option name.
4649 * optval - the buffer to store the value of the option.
4650 * optlen - the size of the buffer.
4651 */
dda91928
DB
4652static int sctp_setsockopt(struct sock *sk, int level, int optname,
4653 char __user *optval, unsigned int optlen)
1da177e4 4654{
ca84bd05 4655 void *kopt = NULL;
1da177e4
LT
4656 int retval = 0;
4657
bb33381d 4658 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
1da177e4
LT
4659
4660 /* I can hardly begin to describe how wrong this is. This is
4661 * so broken as to be worse than useless. The API draft
4662 * REALLY is NOT helpful here... I am not convinced that the
4663 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4664 * are at all well-founded.
4665 */
4666 if (level != SOL_SCTP) {
4667 struct sctp_af *af = sctp_sk(sk)->pf->af;
4668 retval = af->setsockopt(sk, level, optname, optval, optlen);
4669 goto out_nounlock;
4670 }
4671
ca84bd05
CH
4672 if (optlen > 0) {
4673 kopt = memdup_user(optval, optlen);
4674 if (IS_ERR(kopt))
4675 return PTR_ERR(kopt);
4676 }
4677
048ed4b6 4678 lock_sock(sk);
1da177e4
LT
4679
4680 switch (optname) {
4681 case SCTP_SOCKOPT_BINDX_ADD:
4682 /* 'optlen' is the size of the addresses buffer. */
8c7517f5
CH
4683 retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4684 SCTP_BINDX_ADD_ADDR);
1da177e4
LT
4685 break;
4686
4687 case SCTP_SOCKOPT_BINDX_REM:
4688 /* 'optlen' is the size of the addresses buffer. */
8c7517f5
CH
4689 retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4690 SCTP_BINDX_REM_ADDR);
1da177e4
LT
4691 break;
4692
88a0a948
VY
4693 case SCTP_SOCKOPT_CONNECTX_OLD:
4694 /* 'optlen' is the size of the addresses buffer. */
ce5b2f89 4695 retval = sctp_setsockopt_connectx_old(sk, kopt, optlen);
88a0a948
VY
4696 break;
4697
3f7a87d2
FF
4698 case SCTP_SOCKOPT_CONNECTX:
4699 /* 'optlen' is the size of the addresses buffer. */
ce5b2f89 4700 retval = sctp_setsockopt_connectx(sk, kopt, optlen);
3f7a87d2
FF
4701 break;
4702
1da177e4
LT
4703 case SCTP_DISABLE_FRAGMENTS:
4704 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4705 break;
4706
4707 case SCTP_EVENTS:
4708 retval = sctp_setsockopt_events(sk, optval, optlen);
4709 break;
4710
4711 case SCTP_AUTOCLOSE:
4712 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4713 break;
4714
4715 case SCTP_PEER_ADDR_PARAMS:
4716 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4717 break;
4718
4580ccc0 4719 case SCTP_DELAYED_SACK:
d364d927 4720 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
7708610b 4721 break;
d49d91d7
VY
4722 case SCTP_PARTIAL_DELIVERY_POINT:
4723 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4724 break;
7708610b 4725
1da177e4
LT
4726 case SCTP_INITMSG:
4727 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4728 break;
4729 case SCTP_DEFAULT_SEND_PARAM:
4730 retval = sctp_setsockopt_default_send_param(sk, optval,
4731 optlen);
4732 break;
6b3fd5f3
GOV
4733 case SCTP_DEFAULT_SNDINFO:
4734 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4735 break;
1da177e4
LT
4736 case SCTP_PRIMARY_ADDR:
4737 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4738 break;
4739 case SCTP_SET_PEER_PRIMARY_ADDR:
4740 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4741 break;
4742 case SCTP_NODELAY:
4743 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4744 break;
4745 case SCTP_RTOINFO:
4746 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4747 break;
4748 case SCTP_ASSOCINFO:
4749 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4750 break;
4751 case SCTP_I_WANT_MAPPED_V4_ADDR:
4752 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4753 break;
4754 case SCTP_MAXSEG:
4755 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4756 break;
0f3fffd8
ISJ
4757 case SCTP_ADAPTATION_LAYER:
4758 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
1da177e4 4759 break;
6ab792f5
ISJ
4760 case SCTP_CONTEXT:
4761 retval = sctp_setsockopt_context(sk, optval, optlen);
4762 break;
b6e1331f
VY
4763 case SCTP_FRAGMENT_INTERLEAVE:
4764 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4765 break;
70331571
VY
4766 case SCTP_MAX_BURST:
4767 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4768 break;
65b07e5d
VY
4769 case SCTP_AUTH_CHUNK:
4770 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4771 break;
4772 case SCTP_HMAC_IDENT:
4773 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4774 break;
4775 case SCTP_AUTH_KEY:
4776 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4777 break;
4778 case SCTP_AUTH_ACTIVE_KEY:
4779 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4780 break;
4781 case SCTP_AUTH_DELETE_KEY:
4782 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4783 break;
601590ec
XL
4784 case SCTP_AUTH_DEACTIVATE_KEY:
4785 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4786 break;
7dc04d71
MH
4787 case SCTP_AUTO_ASCONF:
4788 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4789 break;
5aa93bcf 4790 case SCTP_PEER_ADDR_THLDS:
d467ac0a
XL
4791 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
4792 false);
4793 break;
4794 case SCTP_PEER_ADDR_THLDS_V2:
4795 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen,
4796 true);
5aa93bcf 4797 break;
0d3a421d
GOV
4798 case SCTP_RECVRCVINFO:
4799 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4800 break;
2347c80f
GOV
4801 case SCTP_RECVNXTINFO:
4802 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4803 break;
28aa4c26
XL
4804 case SCTP_PR_SUPPORTED:
4805 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4806 break;
f959fb44
XL
4807 case SCTP_DEFAULT_PRINFO:
4808 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4809 break;
c0d8bab6
XL
4810 case SCTP_RECONFIG_SUPPORTED:
4811 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4812 break;
9fb657ae
XL
4813 case SCTP_ENABLE_STREAM_RESET:
4814 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4815 break;
7f9d68ac
XL
4816 case SCTP_RESET_STREAMS:
4817 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4818 break;
a92ce1a4
XL
4819 case SCTP_RESET_ASSOC:
4820 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4821 break;
242bd2d5
XL
4822 case SCTP_ADD_STREAMS:
4823 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4824 break;
13aa8770
MRL
4825 case SCTP_STREAM_SCHEDULER:
4826 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4827 break;
0ccdf3c7
MRL
4828 case SCTP_STREAM_SCHEDULER_VALUE:
4829 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4830 break;
772a5869
XL
4831 case SCTP_INTERLEAVING_SUPPORTED:
4832 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4833 optlen);
4834 break;
b0e9a2fe
XL
4835 case SCTP_REUSE_PORT:
4836 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4837 break;
480ba9c1
XL
4838 case SCTP_EVENT:
4839 retval = sctp_setsockopt_event(sk, optval, optlen);
4840 break;
df2c71ff
XL
4841 case SCTP_ASCONF_SUPPORTED:
4842 retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
4843 break;
56dd525a
XL
4844 case SCTP_AUTH_SUPPORTED:
4845 retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
4846 break;
d5886b91
XL
4847 case SCTP_ECN_SUPPORTED:
4848 retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
4849 break;
8d2a6935
XL
4850 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
4851 retval = sctp_setsockopt_pf_expose(sk, optval, optlen);
4852 break;
1da177e4
LT
4853 default:
4854 retval = -ENOPROTOOPT;
4855 break;
3ff50b79 4856 }
1da177e4 4857
048ed4b6 4858 release_sock(sk);
ca84bd05 4859 kfree(kopt);
1da177e4
LT
4860
4861out_nounlock:
4862 return retval;
4863}
4864
4865/* API 3.1.6 connect() - UDP Style Syntax
4866 *
4867 * An application may use the connect() call in the UDP model to initiate an
4868 * association without sending data.
4869 *
4870 * The syntax is:
4871 *
4872 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4873 *
4874 * sd: the socket descriptor to have a new association added to.
4875 *
4876 * nam: the address structure (either struct sockaddr_in or struct
4877 * sockaddr_in6 defined in RFC2553 [7]).
4878 *
4879 * len: the size of the address.
4880 */
dda91928 4881static int sctp_connect(struct sock *sk, struct sockaddr *addr,
644fbdea 4882 int addr_len, int flags)
1da177e4 4883{
3f7a87d2 4884 struct sctp_af *af;
9b6c0887 4885 int err = -EINVAL;
1da177e4 4886
048ed4b6 4887 lock_sock(sk);
bb33381d
DB
4888 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4889 addr, addr_len);
1da177e4 4890
3f7a87d2 4891 /* Validate addr_len before calling common connect/connectx routine. */
9b6c0887
XL
4892 af = sctp_get_af_specific(addr->sa_family);
4893 if (af && addr_len >= af->sockaddr_len)
644fbdea 4894 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
1da177e4 4895
048ed4b6 4896 release_sock(sk);
1da177e4
LT
4897 return err;
4898}
4899
644fbdea
XL
4900int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4901 int addr_len, int flags)
4902{
4903 if (addr_len < sizeof(uaddr->sa_family))
4904 return -EINVAL;
4905
4906 if (uaddr->sa_family == AF_UNSPEC)
4907 return -EOPNOTSUPP;
4908
4909 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4910}
4911
1da177e4 4912/* FIXME: Write comments. */
dda91928 4913static int sctp_disconnect(struct sock *sk, int flags)
1da177e4
LT
4914{
4915 return -EOPNOTSUPP; /* STUB */
4916}
4917
4918/* 4.1.4 accept() - TCP Style Syntax
4919 *
4920 * Applications use accept() call to remove an established SCTP
4921 * association from the accept queue of the endpoint. A new socket
4922 * descriptor will be returned from accept() to represent the newly
4923 * formed association.
4924 */
cdfbabfb 4925static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
1da177e4
LT
4926{
4927 struct sctp_sock *sp;
4928 struct sctp_endpoint *ep;
4929 struct sock *newsk = NULL;
4930 struct sctp_association *asoc;
4931 long timeo;
4932 int error = 0;
4933
048ed4b6 4934 lock_sock(sk);
1da177e4
LT
4935
4936 sp = sctp_sk(sk);
4937 ep = sp->ep;
4938
4939 if (!sctp_style(sk, TCP)) {
4940 error = -EOPNOTSUPP;
4941 goto out;
4942 }
4943
4944 if (!sctp_sstate(sk, LISTENING)) {
4945 error = -EINVAL;
4946 goto out;
4947 }
4948
8abfedd8 4949 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1da177e4
LT
4950
4951 error = sctp_wait_for_accept(sk, timeo);
4952 if (error)
4953 goto out;
4954
4955 /* We treat the list of associations on the endpoint as the accept
4956 * queue and pick the first association on the list.
4957 */
4958 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4959
cdfbabfb 4960 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
1da177e4
LT
4961 if (!newsk) {
4962 error = -ENOMEM;
4963 goto out;
4964 }
4965
4966 /* Populate the fields of the newsk from the oldsk and migrate the
4967 * asoc to the newsk.
4968 */
89664c62
XL
4969 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4970 if (error) {
4971 sk_common_release(newsk);
4972 newsk = NULL;
4973 }
1da177e4
LT
4974
4975out:
048ed4b6 4976 release_sock(sk);
d808ad9a 4977 *err = error;
1da177e4
LT
4978 return newsk;
4979}
4980
4981/* The SCTP ioctl handler. */
dda91928 4982static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
1da177e4 4983{
65040c33
DEFP
4984 int rc = -ENOTCONN;
4985
048ed4b6 4986 lock_sock(sk);
65040c33
DEFP
4987
4988 /*
4989 * SEQPACKET-style sockets in LISTENING state are valid, for
4990 * SCTP, so only discard TCP-style sockets in LISTENING state.
4991 */
4992 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4993 goto out;
4994
4995 switch (cmd) {
4996 case SIOCINQ: {
4997 struct sk_buff *skb;
4998 unsigned int amount = 0;
4999
5000 skb = skb_peek(&sk->sk_receive_queue);
5001 if (skb != NULL) {
5002 /*
5003 * We will only return the amount of this packet since
5004 * that is all that will be read.
5005 */
5006 amount = skb->len;
5007 }
5008 rc = put_user(amount, (int __user *)arg);
65040c33 5009 break;
9a7241c2 5010 }
65040c33
DEFP
5011 default:
5012 rc = -ENOIOCTLCMD;
5013 break;
5014 }
5015out:
048ed4b6 5016 release_sock(sk);
65040c33 5017 return rc;
1da177e4
LT
5018}
5019
5020/* This is the function which gets called during socket creation to
5021 * initialized the SCTP-specific portion of the sock.
5022 * The sock structure should already be zero-filled memory.
5023 */
dda91928 5024static int sctp_init_sock(struct sock *sk)
1da177e4 5025{
e1fc3b14 5026 struct net *net = sock_net(sk);
1da177e4
LT
5027 struct sctp_sock *sp;
5028
bb33381d 5029 pr_debug("%s: sk:%p\n", __func__, sk);
1da177e4
LT
5030
5031 sp = sctp_sk(sk);
5032
5033 /* Initialize the SCTP per socket area. */
5034 switch (sk->sk_type) {
5035 case SOCK_SEQPACKET:
5036 sp->type = SCTP_SOCKET_UDP;
5037 break;
5038 case SOCK_STREAM:
5039 sp->type = SCTP_SOCKET_TCP;
5040 break;
5041 default:
5042 return -ESOCKTNOSUPPORT;
5043 }
5044
90017acc
MRL
5045 sk->sk_gso_type = SKB_GSO_SCTP;
5046
1da177e4
LT
5047 /* Initialize default send parameters. These parameters can be
5048 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
5049 */
5050 sp->default_stream = 0;
5051 sp->default_ppid = 0;
5052 sp->default_flags = 0;
5053 sp->default_context = 0;
5054 sp->default_timetolive = 0;
5055
6ab792f5 5056 sp->default_rcv_context = 0;
e1fc3b14 5057 sp->max_burst = net->sctp.max_burst;
6ab792f5 5058
3c68198e
NH
5059 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5060
1da177e4
LT
5061 /* Initialize default setup parameters. These parameters
5062 * can be modified with the SCTP_INITMSG socket option or
5063 * overridden by the SCTP_INIT CMSG.
5064 */
5065 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5066 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
e1fc3b14
EB
5067 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5068 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
1da177e4
LT
5069
5070 /* Initialize default RTO related parameters. These parameters can
5071 * be modified for with the SCTP_RTOINFO socket option.
5072 */
e1fc3b14
EB
5073 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5074 sp->rtoinfo.srto_max = net->sctp.rto_max;
5075 sp->rtoinfo.srto_min = net->sctp.rto_min;
1da177e4
LT
5076
5077 /* Initialize default association related parameters. These parameters
5078 * can be modified with the SCTP_ASSOCINFO socket option.
5079 */
e1fc3b14 5080 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
1da177e4
LT
5081 sp->assocparams.sasoc_number_peer_destinations = 0;
5082 sp->assocparams.sasoc_peer_rwnd = 0;
5083 sp->assocparams.sasoc_local_rwnd = 0;
e1fc3b14 5084 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
1da177e4
LT
5085
5086 /* Initialize default event subscriptions. By default, all the
d808ad9a 5087 * options are off.
1da177e4 5088 */
2cc0eeb6 5089 sp->subscribe = 0;
1da177e4
LT
5090
5091 /* Default Peer Address Parameters. These defaults can
5092 * be modified via SCTP_PEER_ADDR_PARAMS
5093 */
e1fc3b14
EB
5094 sp->hbinterval = net->sctp.hb_interval;
5095 sp->pathmaxrxt = net->sctp.max_retrans_path;
8add543e 5096 sp->pf_retrans = net->sctp.pf_retrans;
34515e94 5097 sp->ps_retrans = net->sctp.ps_retrans;
aef587be 5098 sp->pf_expose = net->sctp.pf_expose;
4e2d52bf 5099 sp->pathmtu = 0; /* allow default discovery */
e1fc3b14 5100 sp->sackdelay = net->sctp.sack_timeout;
7bfe8bdb 5101 sp->sackfreq = 2;
52ccb8e9 5102 sp->param_flags = SPP_HB_ENABLE |
d808ad9a
YH
5103 SPP_PMTUD_ENABLE |
5104 SPP_SACKDELAY_ENABLE;
7efba10d 5105 sp->default_ss = SCTP_SS_DEFAULT;
1da177e4
LT
5106
5107 /* If enabled no SCTP message fragmentation will be performed.
5108 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5109 */
5110 sp->disable_fragments = 0;
5111
208edef6
SS
5112 /* Enable Nagle algorithm by default. */
5113 sp->nodelay = 0;
1da177e4 5114
0d3a421d 5115 sp->recvrcvinfo = 0;
2347c80f 5116 sp->recvnxtinfo = 0;
0d3a421d 5117
1da177e4
LT
5118 /* Enable by default. */
5119 sp->v4mapped = 1;
5120
5121 /* Auto-close idle associations after the configured
5122 * number of seconds. A value of 0 disables this
5123 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5124 * for UDP-style sockets only.
5125 */
5126 sp->autoclose = 0;
5127
5128 /* User specified fragmentation limit. */
5129 sp->user_frag = 0;
5130
0f3fffd8 5131 sp->adaptation_ind = 0;
1da177e4
LT
5132
5133 sp->pf = sctp_get_pf_specific(sk->sk_family);
5134
5135 /* Control variables for partial data delivery. */
b6e1331f 5136 atomic_set(&sp->pd_mode, 0);
1da177e4 5137 skb_queue_head_init(&sp->pd_lobby);
b6e1331f 5138 sp->frag_interleave = 0;
1da177e4
LT
5139
5140 /* Create a per socket endpoint structure. Even if we
5141 * change the data structure relationships, this may still
5142 * be useful for storing pre-connect address information.
5143 */
c164b838
DB
5144 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5145 if (!sp->ep)
1da177e4
LT
5146 return -ENOMEM;
5147
1da177e4
LT
5148 sp->hmac = NULL;
5149
0a2fbac1
DB
5150 sk->sk_destruct = sctp_destruct_sock;
5151
1da177e4 5152 SCTP_DBG_OBJCNT_INC(sock);
6f756a8c
DM
5153
5154 local_bh_disable();
8cb38a60 5155 sk_sockets_allocated_inc(sk);
e1fc3b14 5156 sock_prot_inuse_add(net, sk->sk_prot, 1);
2d45a02d
MRL
5157
5158 /* Nothing can fail after this block, otherwise
5159 * sctp_destroy_sock() will be called without addr_wq_lock held
5160 */
e1fc3b14 5161 if (net->sctp.default_auto_asconf) {
2d45a02d 5162 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
9f7d653b 5163 list_add_tail(&sp->auto_asconf_list,
e1fc3b14 5164 &net->sctp.auto_asconf_splist);
9f7d653b 5165 sp->do_auto_asconf = 1;
2d45a02d
MRL
5166 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5167 } else {
9f7d653b 5168 sp->do_auto_asconf = 0;
2d45a02d
MRL
5169 }
5170
6f756a8c
DM
5171 local_bh_enable();
5172
1da177e4
LT
5173 return 0;
5174}
5175
2d45a02d
MRL
5176/* Cleanup any SCTP per socket resources. Must be called with
5177 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5178 */
dda91928 5179static void sctp_destroy_sock(struct sock *sk)
1da177e4 5180{
9f7d653b 5181 struct sctp_sock *sp;
1da177e4 5182
bb33381d 5183 pr_debug("%s: sk:%p\n", __func__, sk);
1da177e4
LT
5184
5185 /* Release our hold on the endpoint. */
9f7d653b 5186 sp = sctp_sk(sk);
1abd165e
DB
5187 /* This could happen during socket init, thus we bail out
5188 * early, since the rest of the below is not setup either.
5189 */
5190 if (sp->ep == NULL)
5191 return;
5192
9f7d653b
MH
5193 if (sp->do_auto_asconf) {
5194 sp->do_auto_asconf = 0;
5195 list_del(&sp->auto_asconf_list);
5196 }
5197 sctp_endpoint_free(sp->ep);
5bc0b3bf 5198 local_bh_disable();
8cb38a60 5199 sk_sockets_allocated_dec(sk);
9a57f7fa 5200 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5bc0b3bf 5201 local_bh_enable();
1da177e4
LT
5202}
5203
0a2fbac1
DB
5204/* Triggered when there are no references on the socket anymore */
5205static void sctp_destruct_sock(struct sock *sk)
5206{
5207 struct sctp_sock *sp = sctp_sk(sk);
5208
5209 /* Free up the HMAC transform. */
5821c769 5210 crypto_free_shash(sp->hmac);
0a2fbac1
DB
5211
5212 inet_sock_destruct(sk);
5213}
5214
1da177e4
LT
5215/* API 4.1.7 shutdown() - TCP Style Syntax
5216 * int shutdown(int socket, int how);
5217 *
5218 * sd - the socket descriptor of the association to be closed.
5219 * how - Specifies the type of shutdown. The values are
5220 * as follows:
5221 * SHUT_RD
5222 * Disables further receive operations. No SCTP
5223 * protocol action is taken.
5224 * SHUT_WR
5225 * Disables further send operations, and initiates
5226 * the SCTP shutdown sequence.
5227 * SHUT_RDWR
5228 * Disables further send and receive operations
5229 * and initiates the SCTP shutdown sequence.
5230 */
dda91928 5231static void sctp_shutdown(struct sock *sk, int how)
1da177e4 5232{
55e26eb9 5233 struct net *net = sock_net(sk);
1da177e4 5234 struct sctp_endpoint *ep;
1da177e4
LT
5235
5236 if (!sctp_style(sk, TCP))
5237 return;
5238
5bf35ddf
XL
5239 ep = sctp_sk(sk)->ep;
5240 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5241 struct sctp_association *asoc;
5242
cbabf463 5243 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5bf35ddf
XL
5244 asoc = list_entry(ep->asocs.next,
5245 struct sctp_association, asocs);
5246 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
5247 }
5248}
5249
52c52a61
XL
5250int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5251 struct sctp_info *info)
5252{
5253 struct sctp_transport *prim;
5254 struct list_head *pos;
5255 int mask;
5256
5257 memset(info, 0, sizeof(*info));
5258 if (!asoc) {
5259 struct sctp_sock *sp = sctp_sk(sk);
5260
5261 info->sctpi_s_autoclose = sp->autoclose;
5262 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5263 info->sctpi_s_pd_point = sp->pd_point;
5264 info->sctpi_s_nodelay = sp->nodelay;
5265 info->sctpi_s_disable_fragments = sp->disable_fragments;
5266 info->sctpi_s_v4mapped = sp->v4mapped;
5267 info->sctpi_s_frag_interleave = sp->frag_interleave;
40eb90e9 5268 info->sctpi_s_type = sp->type;
52c52a61
XL
5269
5270 return 0;
5271 }
5272
5273 info->sctpi_tag = asoc->c.my_vtag;
5274 info->sctpi_state = asoc->state;
5275 info->sctpi_rwnd = asoc->a_rwnd;
5276 info->sctpi_unackdata = asoc->unack_data;
5277 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
cee360ab
XL
5278 info->sctpi_instrms = asoc->stream.incnt;
5279 info->sctpi_outstrms = asoc->stream.outcnt;
52c52a61
XL
5280 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5281 info->sctpi_inqueue++;
5282 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5283 info->sctpi_outqueue++;
5284 info->sctpi_overall_error = asoc->overall_error_count;
5285 info->sctpi_max_burst = asoc->max_burst;
5286 info->sctpi_maxseg = asoc->frag_point;
5287 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5288 info->sctpi_peer_tag = asoc->c.peer_vtag;
5289
5290 mask = asoc->peer.ecn_capable << 1;
5291 mask = (mask | asoc->peer.ipv4_address) << 1;
5292 mask = (mask | asoc->peer.ipv6_address) << 1;
5293 mask = (mask | asoc->peer.hostname_address) << 1;
5294 mask = (mask | asoc->peer.asconf_capable) << 1;
5295 mask = (mask | asoc->peer.prsctp_capable) << 1;
5296 mask = (mask | asoc->peer.auth_capable);
5297 info->sctpi_peer_capable = mask;
5298 mask = asoc->peer.sack_needed << 1;
5299 mask = (mask | asoc->peer.sack_generation) << 1;
5300 mask = (mask | asoc->peer.zero_window_announced);
5301 info->sctpi_peer_sack = mask;
5302
5303 info->sctpi_isacks = asoc->stats.isacks;
5304 info->sctpi_osacks = asoc->stats.osacks;
5305 info->sctpi_opackets = asoc->stats.opackets;
5306 info->sctpi_ipackets = asoc->stats.ipackets;
5307 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5308 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5309 info->sctpi_idupchunks = asoc->stats.idupchunks;
5310 info->sctpi_gapcnt = asoc->stats.gapcnt;
5311 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5312 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5313 info->sctpi_oodchunks = asoc->stats.oodchunks;
5314 info->sctpi_iodchunks = asoc->stats.iodchunks;
5315 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5316 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5317
5318 prim = asoc->peer.primary_path;
ee6c88bb 5319 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
52c52a61
XL
5320 info->sctpi_p_state = prim->state;
5321 info->sctpi_p_cwnd = prim->cwnd;
5322 info->sctpi_p_srtt = prim->srtt;
5323 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5324 info->sctpi_p_hbinterval = prim->hbinterval;
5325 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5326 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5327 info->sctpi_p_ssthresh = prim->ssthresh;
5328 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5329 info->sctpi_p_flight_size = prim->flight_size;
5330 info->sctpi_p_error = prim->error_count;
5331
5332 return 0;
5333}
5334EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5335
626d16f5 5336/* use callback to avoid exporting the core structure */
6c72b774 5337void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
626d16f5 5338{
7fda702f 5339 rhltable_walk_enter(&sctp_transport_hashtable, iter);
626d16f5 5340
97a6ec4a 5341 rhashtable_walk_start(iter);
626d16f5
XL
5342}
5343
b77b4f63 5344void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
626d16f5
XL
5345{
5346 rhashtable_walk_stop(iter);
5347 rhashtable_walk_exit(iter);
5348}
5349
5350struct sctp_transport *sctp_transport_get_next(struct net *net,
5351 struct rhashtable_iter *iter)
5352{
5353 struct sctp_transport *t;
5354
5355 t = rhashtable_walk_next(iter);
5356 for (; t; t = rhashtable_walk_next(iter)) {
5357 if (IS_ERR(t)) {
5358 if (PTR_ERR(t) == -EAGAIN)
5359 continue;
5360 break;
5361 }
5362
bab1be79
XL
5363 if (!sctp_transport_hold(t))
5364 continue;
5365
4e7696d9 5366 if (net_eq(t->asoc->base.net, net) &&
626d16f5
XL
5367 t->asoc->peer.primary_path == t)
5368 break;
bab1be79
XL
5369
5370 sctp_transport_put(t);
626d16f5
XL
5371 }
5372
5373 return t;
5374}
5375
5376struct sctp_transport *sctp_transport_get_idx(struct net *net,
5377 struct rhashtable_iter *iter,
5378 int pos)
5379{
bab1be79 5380 struct sctp_transport *t;
626d16f5 5381
bab1be79
XL
5382 if (!pos)
5383 return SEQ_START_TOKEN;
626d16f5 5384
bab1be79
XL
5385 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5386 if (!--pos)
5387 break;
5388 sctp_transport_put(t);
5389 }
5390
5391 return t;
626d16f5
XL
5392}
5393
5394int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5395 void *p) {
5396 int err = 0;
5397 int hash = 0;
5398 struct sctp_ep_common *epb;
5399 struct sctp_hashbucket *head;
5400
5401 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5402 hash++, head++) {
581409da 5403 read_lock_bh(&head->lock);
626d16f5
XL
5404 sctp_for_each_hentry(epb, &head->chain) {
5405 err = cb(sctp_ep(epb), p);
5406 if (err)
5407 break;
5408 }
581409da 5409 read_unlock_bh(&head->lock);
626d16f5
XL
5410 }
5411
5412 return err;
5413}
5414EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5415
5416int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5417 struct net *net,
5418 const union sctp_addr *laddr,
5419 const union sctp_addr *paddr, void *p)
5420{
5421 struct sctp_transport *transport;
08abb795 5422 int err;
626d16f5
XL
5423
5424 rcu_read_lock();
5425 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
08abb795 5426 rcu_read_unlock();
7fda702f 5427 if (!transport)
08abb795 5428 return -ENOENT;
1cceda78 5429
1cceda78 5430 err = cb(transport, p);
cd26da4f 5431 sctp_transport_put(transport);
1cceda78 5432
626d16f5
XL
5433 return err;
5434}
5435EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5436
5437int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
d25adbeb
XL
5438 int (*cb_done)(struct sctp_transport *, void *),
5439 struct net *net, int *pos, void *p) {
626d16f5 5440 struct rhashtable_iter hti;
d25adbeb
XL
5441 struct sctp_transport *tsp;
5442 int ret;
626d16f5 5443
d25adbeb 5444again:
f53d77e1 5445 ret = 0;
97a6ec4a 5446 sctp_transport_walk_start(&hti);
626d16f5 5447
d25adbeb
XL
5448 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5449 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
d25adbeb
XL
5450 ret = cb(tsp, p);
5451 if (ret)
626d16f5 5452 break;
d25adbeb
XL
5453 (*pos)++;
5454 sctp_transport_put(tsp);
626d16f5 5455 }
626d16f5 5456 sctp_transport_walk_stop(&hti);
53fa1036 5457
d25adbeb
XL
5458 if (ret) {
5459 if (cb_done && !cb_done(tsp, p)) {
5460 (*pos)++;
5461 sctp_transport_put(tsp);
5462 goto again;
5463 }
5464 sctp_transport_put(tsp);
5465 }
5466
5467 return ret;
626d16f5
XL
5468}
5469EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5470
1da177e4
LT
5471/* 7.2.1 Association Status (SCTP_STATUS)
5472
5473 * Applications can retrieve current status information about an
5474 * association, including association state, peer receiver window size,
5475 * number of unacked data chunks, and number of data chunks pending
5476 * receipt. This information is read-only.
5477 */
5478static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5479 char __user *optval,
5480 int __user *optlen)
5481{
5482 struct sctp_status status;
5483 struct sctp_association *asoc = NULL;
5484 struct sctp_transport *transport;
5485 sctp_assoc_t associd;
5486 int retval = 0;
5487
408f22e8 5488 if (len < sizeof(status)) {
1da177e4
LT
5489 retval = -EINVAL;
5490 goto out;
5491 }
5492
408f22e8
NH
5493 len = sizeof(status);
5494 if (copy_from_user(&status, optval, len)) {
1da177e4
LT
5495 retval = -EFAULT;
5496 goto out;
5497 }
5498
5499 associd = status.sstat_assoc_id;
5500 asoc = sctp_id2assoc(sk, associd);
5501 if (!asoc) {
5502 retval = -EINVAL;
5503 goto out;
5504 }
5505
5506 transport = asoc->peer.primary_path;
5507
5508 status.sstat_assoc_id = sctp_assoc2id(asoc);
38ab1fa9 5509 status.sstat_state = sctp_assoc_to_state(asoc);
1da177e4
LT
5510 status.sstat_rwnd = asoc->peer.rwnd;
5511 status.sstat_unackdata = asoc->unack_data;
5512
5513 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
cee360ab
XL
5514 status.sstat_instrms = asoc->stream.incnt;
5515 status.sstat_outstrms = asoc->stream.outcnt;
1da177e4
LT
5516 status.sstat_fragmentation_point = asoc->frag_point;
5517 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
8cec6b80
AV
5518 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5519 transport->af_specific->sockaddr_len);
1da177e4 5520 /* Map ipv4 address into v4-mapped-on-v6 address. */
299ee123 5521 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
1da177e4 5522 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3f7a87d2 5523 status.sstat_primary.spinfo_state = transport->state;
1da177e4
LT
5524 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5525 status.sstat_primary.spinfo_srtt = transport->srtt;
5526 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 5527 status.sstat_primary.spinfo_mtu = transport->pathmtu;
1da177e4 5528
3f7a87d2
FF
5529 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5530 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5531
1da177e4
LT
5532 if (put_user(len, optlen)) {
5533 retval = -EFAULT;
5534 goto out;
5535 }
5536
bb33381d
DB
5537 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5538 __func__, len, status.sstat_state, status.sstat_rwnd,
5539 status.sstat_assoc_id);
1da177e4
LT
5540
5541 if (copy_to_user(optval, &status, len)) {
5542 retval = -EFAULT;
5543 goto out;
5544 }
5545
5546out:
a02cec21 5547 return retval;
1da177e4
LT
5548}
5549
5550
5551/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5552 *
5553 * Applications can retrieve information about a specific peer address
5554 * of an association, including its reachability state, congestion
5555 * window, and retransmission timer values. This information is
5556 * read-only.
5557 */
5558static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5559 char __user *optval,
5560 int __user *optlen)
5561{
5562 struct sctp_paddrinfo pinfo;
5563 struct sctp_transport *transport;
5564 int retval = 0;
5565
408f22e8 5566 if (len < sizeof(pinfo)) {
1da177e4
LT
5567 retval = -EINVAL;
5568 goto out;
5569 }
5570
408f22e8
NH
5571 len = sizeof(pinfo);
5572 if (copy_from_user(&pinfo, optval, len)) {
1da177e4
LT
5573 retval = -EFAULT;
5574 goto out;
5575 }
5576
5577 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5578 pinfo.spinfo_assoc_id);
aef587be
XL
5579 if (!transport) {
5580 retval = -EINVAL;
5581 goto out;
5582 }
5583
5584 if (transport->state == SCTP_PF &&
5585 transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
5586 retval = -EACCES;
5587 goto out;
5588 }
1da177e4
LT
5589
5590 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3f7a87d2 5591 pinfo.spinfo_state = transport->state;
1da177e4
LT
5592 pinfo.spinfo_cwnd = transport->cwnd;
5593 pinfo.spinfo_srtt = transport->srtt;
5594 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 5595 pinfo.spinfo_mtu = transport->pathmtu;
1da177e4 5596
3f7a87d2
FF
5597 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5598 pinfo.spinfo_state = SCTP_ACTIVE;
5599
1da177e4
LT
5600 if (put_user(len, optlen)) {
5601 retval = -EFAULT;
5602 goto out;
5603 }
5604
5605 if (copy_to_user(optval, &pinfo, len)) {
5606 retval = -EFAULT;
5607 goto out;
5608 }
5609
5610out:
a02cec21 5611 return retval;
1da177e4
LT
5612}
5613
5614/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5615 *
5616 * This option is a on/off flag. If enabled no SCTP message
5617 * fragmentation will be performed. Instead if a message being sent
5618 * exceeds the current PMTU size, the message will NOT be sent and
5619 * instead a error will be indicated to the user.
5620 */
5621static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5622 char __user *optval, int __user *optlen)
5623{
5624 int val;
5625
5626 if (len < sizeof(int))
5627 return -EINVAL;
5628
5629 len = sizeof(int);
5630 val = (sctp_sk(sk)->disable_fragments == 1);
5631 if (put_user(len, optlen))
5632 return -EFAULT;
5633 if (copy_to_user(optval, &val, len))
5634 return -EFAULT;
5635 return 0;
5636}
5637
5638/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5639 *
5640 * This socket option is used to specify various notifications and
5641 * ancillary data the user wishes to receive.
5642 */
5643static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5644 int __user *optlen)
5645{
2cc0eeb6
XL
5646 struct sctp_event_subscribe subscribe;
5647 __u8 *sn_type = (__u8 *)&subscribe;
5648 int i;
5649
a4b8e71b 5650 if (len == 0)
1da177e4 5651 return -EINVAL;
acdd5985
TG
5652 if (len > sizeof(struct sctp_event_subscribe))
5653 len = sizeof(struct sctp_event_subscribe);
408f22e8
NH
5654 if (put_user(len, optlen))
5655 return -EFAULT;
2cc0eeb6
XL
5656
5657 for (i = 0; i < len; i++)
5658 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5659 SCTP_SN_TYPE_BASE + i);
5660
5661 if (copy_to_user(optval, &subscribe, len))
1da177e4 5662 return -EFAULT;
2cc0eeb6 5663
1da177e4
LT
5664 return 0;
5665}
5666
5667/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5668 *
5669 * This socket option is applicable to the UDP-style socket only. When
5670 * set it will cause associations that are idle for more than the
5671 * specified number of seconds to automatically close. An association
5672 * being idle is defined an association that has NOT sent or received
5673 * user data. The special value of '0' indicates that no automatic
5674 * close of any associations should be performed. The option expects an
5675 * integer defining the number of seconds of idle time before an
5676 * association is closed.
5677 */
5678static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5679{
5680 /* Applicable to UDP-style socket only */
5681 if (sctp_style(sk, TCP))
5682 return -EOPNOTSUPP;
408f22e8 5683 if (len < sizeof(int))
1da177e4 5684 return -EINVAL;
408f22e8
NH
5685 len = sizeof(int);
5686 if (put_user(len, optlen))
5687 return -EFAULT;
b2ce04c2 5688 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
1da177e4
LT
5689 return -EFAULT;
5690 return 0;
5691}
5692
5693/* Helper routine to branch off an association to a new socket. */
0343c554 5694int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
1da177e4 5695{
0343c554 5696 struct sctp_association *asoc = sctp_id2assoc(sk, id);
299ee123 5697 struct sctp_sock *sp = sctp_sk(sk);
1da177e4
LT
5698 struct socket *sock;
5699 int err = 0;
5700
df80cd9b
XL
5701 /* Do not peel off from one netns to another one. */
5702 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5703 return -EINVAL;
5704
0343c554
BP
5705 if (!asoc)
5706 return -EINVAL;
5707
1da177e4
LT
5708 /* An association cannot be branched off from an already peeled-off
5709 * socket, nor is this supported for tcp style sockets.
5710 */
5711 if (!sctp_style(sk, UDP))
5712 return -EINVAL;
5713
5714 /* Create a new socket. */
5715 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5716 if (err < 0)
5717 return err;
5718
914e1c8b 5719 sctp_copy_sock(sock->sk, sk, asoc);
4f444308
VY
5720
5721 /* Make peeled-off sockets more like 1-1 accepted sockets.
b7e10c25
RH
5722 * Set the daddr and initialize id to something more random and also
5723 * copy over any ip options.
4f444308 5724 */
299ee123 5725 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
b7e10c25 5726 sp->pf->copy_ip_options(sk, sock->sk);
914e1c8b
VY
5727
5728 /* Populate the fields of the newsk from the oldsk and migrate the
5729 * asoc to the newsk.
5730 */
89664c62
XL
5731 err = sctp_sock_migrate(sk, sock->sk, asoc,
5732 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5733 if (err) {
5734 sock_release(sock);
5735 sock = NULL;
5736 }
4f444308 5737
1da177e4
LT
5738 *sockp = sock;
5739
5740 return err;
5741}
0343c554 5742EXPORT_SYMBOL(sctp_do_peeloff);
1da177e4 5743
2cb5c8e3
NH
5744static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5745 struct file **newfile, unsigned flags)
5746{
5747 struct socket *newsock;
5748 int retval;
5749
5750 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5751 if (retval < 0)
5752 goto out;
5753
5754 /* Map the socket to an unused fd that can be returned to the user. */
5755 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5756 if (retval < 0) {
5757 sock_release(newsock);
5758 goto out;
5759 }
5760
5761 *newfile = sock_alloc_file(newsock, 0, NULL);
5762 if (IS_ERR(*newfile)) {
5763 put_unused_fd(retval);
2cb5c8e3
NH
5764 retval = PTR_ERR(*newfile);
5765 *newfile = NULL;
5766 return retval;
5767 }
5768
5769 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5770 retval);
5771
5772 peeloff->sd = retval;
5773
5774 if (flags & SOCK_NONBLOCK)
5775 (*newfile)->f_flags |= O_NONBLOCK;
5776out:
5777 return retval;
5778}
5779
1da177e4
LT
5780static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5781{
5782 sctp_peeloff_arg_t peeloff;
2cb5c8e3 5783 struct file *newfile = NULL;
1da177e4 5784 int retval = 0;
1da177e4 5785
408f22e8 5786 if (len < sizeof(sctp_peeloff_arg_t))
1da177e4 5787 return -EINVAL;
408f22e8 5788 len = sizeof(sctp_peeloff_arg_t);
1da177e4
LT
5789 if (copy_from_user(&peeloff, optval, len))
5790 return -EFAULT;
5791
2cb5c8e3 5792 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
1da177e4
LT
5793 if (retval < 0)
5794 goto out;
5795
2cb5c8e3
NH
5796 /* Return the fd mapped to the new socket. */
5797 if (put_user(len, optlen)) {
5798 fput(newfile);
5799 put_unused_fd(retval);
5800 return -EFAULT;
1da177e4
LT
5801 }
5802
2cb5c8e3
NH
5803 if (copy_to_user(optval, &peeloff, len)) {
5804 fput(newfile);
56b31d1c 5805 put_unused_fd(retval);
2cb5c8e3 5806 return -EFAULT;
56b31d1c 5807 }
2cb5c8e3
NH
5808 fd_install(retval, newfile);
5809out:
5810 return retval;
5811}
56b31d1c 5812
2cb5c8e3
NH
5813static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5814 char __user *optval, int __user *optlen)
5815{
5816 sctp_peeloff_flags_arg_t peeloff;
5817 struct file *newfile = NULL;
5818 int retval = 0;
5819
5820 if (len < sizeof(sctp_peeloff_flags_arg_t))
5821 return -EINVAL;
5822 len = sizeof(sctp_peeloff_flags_arg_t);
5823 if (copy_from_user(&peeloff, optval, len))
5824 return -EFAULT;
5825
5826 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5827 &newfile, peeloff.flags);
5828 if (retval < 0)
5829 goto out;
1da177e4
LT
5830
5831 /* Return the fd mapped to the new socket. */
56b31d1c
AV
5832 if (put_user(len, optlen)) {
5833 fput(newfile);
5834 put_unused_fd(retval);
5835 return -EFAULT;
5836 }
2cb5c8e3 5837
56b31d1c
AV
5838 if (copy_to_user(optval, &peeloff, len)) {
5839 fput(newfile);
5840 put_unused_fd(retval);
408f22e8 5841 return -EFAULT;
56b31d1c
AV
5842 }
5843 fd_install(retval, newfile);
1da177e4
LT
5844out:
5845 return retval;
5846}
5847
5848/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5849 *
5850 * Applications can enable or disable heartbeats for any peer address of
5851 * an association, modify an address's heartbeat interval, force a
5852 * heartbeat to be sent immediately, and adjust the address's maximum
5853 * number of retransmissions sent before an address is considered
5854 * unreachable. The following structure is used to access and modify an
5855 * address's parameters:
5856 *
5857 * struct sctp_paddrparams {
52ccb8e9
FF
5858 * sctp_assoc_t spp_assoc_id;
5859 * struct sockaddr_storage spp_address;
5860 * uint32_t spp_hbinterval;
5861 * uint16_t spp_pathmaxrxt;
5862 * uint32_t spp_pathmtu;
5863 * uint32_t spp_sackdelay;
5864 * uint32_t spp_flags;
5865 * };
5866 *
5867 * spp_assoc_id - (one-to-many style socket) This is filled in the
5868 * application, and identifies the association for
5869 * this query.
1da177e4
LT
5870 * spp_address - This specifies which address is of interest.
5871 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
5872 * in milliseconds. If a value of zero
5873 * is present in this field then no changes are to
5874 * be made to this parameter.
1da177e4
LT
5875 * spp_pathmaxrxt - This contains the maximum number of
5876 * retransmissions before this address shall be
52ccb8e9
FF
5877 * considered unreachable. If a value of zero
5878 * is present in this field then no changes are to
5879 * be made to this parameter.
5880 * spp_pathmtu - When Path MTU discovery is disabled the value
5881 * specified here will be the "fixed" path mtu.
5882 * Note that if the spp_address field is empty
5883 * then all associations on this address will
5884 * have this fixed path mtu set upon them.
5885 *
5886 * spp_sackdelay - When delayed sack is enabled, this value specifies
5887 * the number of milliseconds that sacks will be delayed
5888 * for. This value will apply to all addresses of an
5889 * association if the spp_address field is empty. Note
5890 * also, that if delayed sack is enabled and this
5891 * value is set to 0, no change is made to the last
5892 * recorded delayed sack timer value.
5893 *
5894 * spp_flags - These flags are used to control various features
5895 * on an association. The flag field may contain
5896 * zero or more of the following options.
5897 *
5898 * SPP_HB_ENABLE - Enable heartbeats on the
5899 * specified address. Note that if the address
5900 * field is empty all addresses for the association
5901 * have heartbeats enabled upon them.
5902 *
5903 * SPP_HB_DISABLE - Disable heartbeats on the
5904 * speicifed address. Note that if the address
5905 * field is empty all addresses for the association
5906 * will have their heartbeats disabled. Note also
5907 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5908 * mutually exclusive, only one of these two should
5909 * be specified. Enabling both fields will have
5910 * undetermined results.
5911 *
5912 * SPP_HB_DEMAND - Request a user initiated heartbeat
5913 * to be made immediately.
5914 *
5915 * SPP_PMTUD_ENABLE - This field will enable PMTU
5916 * discovery upon the specified address. Note that
5917 * if the address feild is empty then all addresses
5918 * on the association are effected.
5919 *
5920 * SPP_PMTUD_DISABLE - This field will disable PMTU
5921 * discovery upon the specified address. Note that
5922 * if the address feild is empty then all addresses
5923 * on the association are effected. Not also that
5924 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5925 * exclusive. Enabling both will have undetermined
5926 * results.
5927 *
5928 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5929 * on delayed sack. The time specified in spp_sackdelay
5930 * is used to specify the sack delay for this address. Note
5931 * that if spp_address is empty then all addresses will
5932 * enable delayed sack and take on the sack delay
5933 * value specified in spp_sackdelay.
5934 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5935 * off delayed sack. If the spp_address field is blank then
5936 * delayed sack is disabled for the entire association. Note
5937 * also that this field is mutually exclusive to
5938 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5939 * results.
0b0dce7a
XL
5940 *
5941 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5942 * setting of the IPV6 flow label value. The value is
5943 * contained in the spp_ipv6_flowlabel field.
5944 * Upon retrieval, this flag will be set to indicate that
5945 * the spp_ipv6_flowlabel field has a valid value returned.
5946 * If a specific destination address is set (in the
5947 * spp_address field), then the value returned is that of
5948 * the address. If just an association is specified (and
5949 * no address), then the association's default flow label
5950 * is returned. If neither an association nor a destination
5951 * is specified, then the socket's default flow label is
5952 * returned. For non-IPv6 sockets, this flag will be left
5953 * cleared.
5954 *
5955 * SPP_DSCP: Setting this flag enables the setting of the
5956 * Differentiated Services Code Point (DSCP) value
5957 * associated with either the association or a specific
5958 * address. The value is obtained in the spp_dscp field.
5959 * Upon retrieval, this flag will be set to indicate that
5960 * the spp_dscp field has a valid value returned. If a
5961 * specific destination address is set when called (in the
5962 * spp_address field), then that specific destination
5963 * address's DSCP value is returned. If just an association
5964 * is specified, then the association's default DSCP is
5965 * returned. If neither an association nor a destination is
5966 * specified, then the socket's default DSCP is returned.
5967 *
5968 * spp_ipv6_flowlabel
5969 * - This field is used in conjunction with the
5970 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5971 * The 20 least significant bits are used for the flow
5972 * label. This setting has precedence over any IPv6-layer
5973 * setting.
5974 *
5975 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5976 * and contains the DSCP. The 6 most significant bits are
5977 * used for the DSCP. This setting has precedence over any
5978 * IPv4- or IPv6- layer setting.
1da177e4
LT
5979 */
5980static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
52ccb8e9 5981 char __user *optval, int __user *optlen)
1da177e4 5982{
52ccb8e9
FF
5983 struct sctp_paddrparams params;
5984 struct sctp_transport *trans = NULL;
5985 struct sctp_association *asoc = NULL;
5986 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 5987
0b0dce7a
XL
5988 if (len >= sizeof(params))
5989 len = sizeof(params);
5990 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5991 spp_ipv6_flowlabel), 4))
5992 len = ALIGN(offsetof(struct sctp_paddrparams,
5993 spp_ipv6_flowlabel), 4);
5994 else
1da177e4 5995 return -EINVAL;
0b0dce7a 5996
1da177e4
LT
5997 if (copy_from_user(&params, optval, len))
5998 return -EFAULT;
5999
52ccb8e9
FF
6000 /* If an address other than INADDR_ANY is specified, and
6001 * no transport is found, then the request is invalid.
1da177e4 6002 */
cb3f837b 6003 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
6004 trans = sctp_addr_id2transport(sk, &params.spp_address,
6005 params.spp_assoc_id);
6006 if (!trans) {
bb33381d 6007 pr_debug("%s: failed no transport\n", __func__);
52ccb8e9
FF
6008 return -EINVAL;
6009 }
1da177e4
LT
6010 }
6011
b99e5e02
XL
6012 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
6013 * socket is a one to many style socket, and an association
6014 * was not found, then the id was invalid.
52ccb8e9
FF
6015 */
6016 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
b99e5e02
XL
6017 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
6018 sctp_style(sk, UDP)) {
bb33381d 6019 pr_debug("%s: failed no association\n", __func__);
1da177e4 6020 return -EINVAL;
52ccb8e9 6021 }
1da177e4 6022
52ccb8e9
FF
6023 if (trans) {
6024 /* Fetch transport values. */
6025 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
6026 params.spp_pathmtu = trans->pathmtu;
6027 params.spp_pathmaxrxt = trans->pathmaxrxt;
6028 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
6029
6030 /*draft-11 doesn't say what to return in spp_flags*/
6031 params.spp_flags = trans->param_flags;
0b0dce7a
XL
6032 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6033 params.spp_ipv6_flowlabel = trans->flowlabel &
6034 SCTP_FLOWLABEL_VAL_MASK;
6035 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6036 }
6037 if (trans->dscp & SCTP_DSCP_SET_MASK) {
6038 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
6039 params.spp_flags |= SPP_DSCP;
6040 }
52ccb8e9
FF
6041 } else if (asoc) {
6042 /* Fetch association values. */
6043 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
6044 params.spp_pathmtu = asoc->pathmtu;
6045 params.spp_pathmaxrxt = asoc->pathmaxrxt;
6046 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
6047
6048 /*draft-11 doesn't say what to return in spp_flags*/
6049 params.spp_flags = asoc->param_flags;
0b0dce7a
XL
6050 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6051 params.spp_ipv6_flowlabel = asoc->flowlabel &
6052 SCTP_FLOWLABEL_VAL_MASK;
6053 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6054 }
6055 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
6056 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6057 params.spp_flags |= SPP_DSCP;
6058 }
52ccb8e9
FF
6059 } else {
6060 /* Fetch socket values. */
6061 params.spp_hbinterval = sp->hbinterval;
6062 params.spp_pathmtu = sp->pathmtu;
6063 params.spp_sackdelay = sp->sackdelay;
6064 params.spp_pathmaxrxt = sp->pathmaxrxt;
1da177e4 6065
52ccb8e9
FF
6066 /*draft-11 doesn't say what to return in spp_flags*/
6067 params.spp_flags = sp->param_flags;
0b0dce7a
XL
6068 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6069 params.spp_ipv6_flowlabel = sp->flowlabel &
6070 SCTP_FLOWLABEL_VAL_MASK;
6071 params.spp_flags |= SPP_IPV6_FLOWLABEL;
6072 }
6073 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6074 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6075 params.spp_flags |= SPP_DSCP;
6076 }
52ccb8e9 6077 }
1da177e4 6078
1da177e4
LT
6079 if (copy_to_user(optval, &params, len))
6080 return -EFAULT;
6081
6082 if (put_user(len, optlen))
6083 return -EFAULT;
6084
6085 return 0;
6086}
6087
d364d927
WY
6088/*
6089 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
6090 *
6091 * This option will effect the way delayed acks are performed. This
6092 * option allows you to get or set the delayed ack time, in
6093 * milliseconds. It also allows changing the delayed ack frequency.
6094 * Changing the frequency to 1 disables the delayed sack algorithm. If
6095 * the assoc_id is 0, then this sets or gets the endpoints default
6096 * values. If the assoc_id field is non-zero, then the set or get
6097 * effects the specified association for the one to many model (the
6098 * assoc_id field is ignored by the one to one model). Note that if
6099 * sack_delay or sack_freq are 0 when setting this option, then the
6100 * current values will remain unchanged.
6101 *
6102 * struct sctp_sack_info {
6103 * sctp_assoc_t sack_assoc_id;
6104 * uint32_t sack_delay;
6105 * uint32_t sack_freq;
6106 * };
7708610b 6107 *
d364d927
WY
6108 * sack_assoc_id - This parameter, indicates which association the user
6109 * is performing an action upon. Note that if this field's value is
6110 * zero then the endpoints default value is changed (effecting future
6111 * associations only).
7708610b 6112 *
d364d927
WY
6113 * sack_delay - This parameter contains the number of milliseconds that
6114 * the user is requesting the delayed ACK timer be set to. Note that
6115 * this value is defined in the standard to be between 200 and 500
6116 * milliseconds.
7708610b 6117 *
d364d927
WY
6118 * sack_freq - This parameter contains the number of packets that must
6119 * be received before a sack is sent without waiting for the delay
6120 * timer to expire. The default value for this is 2, setting this
6121 * value to 1 will disable the delayed sack algorithm.
7708610b 6122 */
d364d927 6123static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
7708610b
FF
6124 char __user *optval,
6125 int __user *optlen)
6126{
d364d927 6127 struct sctp_sack_info params;
7708610b
FF
6128 struct sctp_association *asoc = NULL;
6129 struct sctp_sock *sp = sctp_sk(sk);
6130
d364d927
WY
6131 if (len >= sizeof(struct sctp_sack_info)) {
6132 len = sizeof(struct sctp_sack_info);
7708610b 6133
d364d927
WY
6134 if (copy_from_user(&params, optval, len))
6135 return -EFAULT;
6136 } else if (len == sizeof(struct sctp_assoc_value)) {
94f65193 6137 pr_warn_ratelimited(DEPRECATED
f916ec96 6138 "%s (pid %d) "
94f65193 6139 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
f916ec96
NH
6140 "Use struct sctp_sack_info instead\n",
6141 current->comm, task_pid_nr(current));
d364d927
WY
6142 if (copy_from_user(&params, optval, len))
6143 return -EFAULT;
6144 } else
cb3f837b 6145 return -EINVAL;
7708610b 6146
9c5829e1
XL
6147 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6148 * socket is a one to many style socket, and an association
6149 * was not found, then the id was invalid.
d808ad9a 6150 */
d364d927 6151 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
9c5829e1
XL
6152 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6153 sctp_style(sk, UDP))
7708610b
FF
6154 return -EINVAL;
6155
6156 if (asoc) {
6157 /* Fetch association values. */
d364d927 6158 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
9c5829e1 6159 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
d364d927
WY
6160 params.sack_freq = asoc->sackfreq;
6161
6162 } else {
6163 params.sack_delay = 0;
6164 params.sack_freq = 1;
6165 }
7708610b
FF
6166 } else {
6167 /* Fetch socket values. */
d364d927
WY
6168 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6169 params.sack_delay = sp->sackdelay;
6170 params.sack_freq = sp->sackfreq;
6171 } else {
6172 params.sack_delay = 0;
6173 params.sack_freq = 1;
6174 }
7708610b
FF
6175 }
6176
6177 if (copy_to_user(optval, &params, len))
6178 return -EFAULT;
6179
6180 if (put_user(len, optlen))
6181 return -EFAULT;
6182
6183 return 0;
6184}
6185
1da177e4
LT
6186/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6187 *
6188 * Applications can specify protocol parameters for the default association
6189 * initialization. The option name argument to setsockopt() and getsockopt()
6190 * is SCTP_INITMSG.
6191 *
6192 * Setting initialization parameters is effective only on an unconnected
6193 * socket (for UDP-style sockets only future associations are effected
6194 * by the change). With TCP-style sockets, this option is inherited by
6195 * sockets derived from a listener socket.
6196 */
6197static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6198{
408f22e8 6199 if (len < sizeof(struct sctp_initmsg))
1da177e4 6200 return -EINVAL;
408f22e8
NH
6201 len = sizeof(struct sctp_initmsg);
6202 if (put_user(len, optlen))
6203 return -EFAULT;
1da177e4
LT
6204 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6205 return -EFAULT;
6206 return 0;
6207}
6208