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