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