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