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