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