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