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