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