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