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