Merge tag 'v3.10.96' 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);
4a3411cc 1536 sctp_primitive_ABORT(net, asoc, chunk);
b9ac8672 1537 } else
55e26eb9 1538 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
1539 }
1540
1da177e4
LT
1541 /* On a TCP-style socket, block for at most linger_time if set. */
1542 if (sctp_style(sk, TCP) && timeout)
1543 sctp_wait_for_close(sk, timeout);
1544
1545 /* This will run the backlog queue. */
1546 sctp_release_sock(sk);
1547
1548 /* Supposedly, no process has access to the socket, but
1549 * the net layers still may.
7bf24986
MRL
1550 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1551 * held and that should be grabbed before socket lock.
1da177e4 1552 */
7bf24986 1553 spin_lock_bh(&net->sctp.addr_wq_lock);
1da177e4
LT
1554 sctp_bh_lock_sock(sk);
1555
1556 /* Hold the sock, since sk_common_release() will put sock_put()
1557 * and we have just a little more cleanup.
1558 */
1559 sock_hold(sk);
1560 sk_common_release(sk);
1561
1562 sctp_bh_unlock_sock(sk);
7bf24986 1563 spin_unlock_bh(&net->sctp.addr_wq_lock);
1da177e4
LT
1564
1565 sock_put(sk);
1566
1567 SCTP_DBG_OBJCNT_DEC(sock);
1568}
1569
1570/* Handle EPIPE error. */
1571static int sctp_error(struct sock *sk, int flags, int err)
1572{
1573 if (err == -EPIPE)
1574 err = sock_error(sk) ? : -EPIPE;
1575 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1576 send_sig(SIGPIPE, current, 0);
1577 return err;
1578}
1579
1580/* API 3.1.3 sendmsg() - UDP Style Syntax
1581 *
1582 * An application uses sendmsg() and recvmsg() calls to transmit data to
1583 * and receive data from its peer.
1584 *
1585 * ssize_t sendmsg(int socket, const struct msghdr *message,
1586 * int flags);
1587 *
1588 * socket - the socket descriptor of the endpoint.
1589 * message - pointer to the msghdr structure which contains a single
1590 * user message and possibly some ancillary data.
1591 *
1592 * See Section 5 for complete description of the data
1593 * structures.
1594 *
1595 * flags - flags sent or received with the user message, see Section
1596 * 5 for complete description of the flags.
1597 *
1598 * Note: This function could use a rewrite especially when explicit
1599 * connect support comes in.
1600 */
1601/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1602
1603SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1604
1605SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1606 struct msghdr *msg, size_t msg_len)
1607{
55e26eb9 1608 struct net *net = sock_net(sk);
1da177e4
LT
1609 struct sctp_sock *sp;
1610 struct sctp_endpoint *ep;
1611 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1612 struct sctp_transport *transport, *chunk_tp;
1613 struct sctp_chunk *chunk;
dce116ae 1614 union sctp_addr to;
1da177e4 1615 struct sockaddr *msg_name = NULL;
517aa0bc 1616 struct sctp_sndrcvinfo default_sinfo;
1da177e4
LT
1617 struct sctp_sndrcvinfo *sinfo;
1618 struct sctp_initmsg *sinit;
1619 sctp_assoc_t associd = 0;
1620 sctp_cmsgs_t cmsgs = { NULL };
1621 int err;
1622 sctp_scope_t scope;
1623 long timeo;
1624 __u16 sinfo_flags = 0;
1625 struct sctp_datamsg *datamsg;
1da177e4
LT
1626 int msg_flags = msg->msg_flags;
1627
1628 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1629 sk, msg, msg_len);
1630
1631 err = 0;
1632 sp = sctp_sk(sk);
1633 ep = sp->ep;
1634
3f7a87d2 1635 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1da177e4
LT
1636
1637 /* We cannot send a message over a TCP-style listening socket. */
1638 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1639 err = -EPIPE;
1640 goto out_nounlock;
1641 }
1642
1643 /* Parse out the SCTP CMSGs. */
1644 err = sctp_msghdr_parse(msg, &cmsgs);
1645
1646 if (err) {
1647 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1648 goto out_nounlock;
1649 }
1650
1651 /* Fetch the destination address for this packet. This
1652 * address only selects the association--it is not necessarily
1653 * the address we will send to.
1654 * For a peeled-off socket, msg_name is ignored.
1655 */
1656 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1657 int msg_namelen = msg->msg_namelen;
1658
1659 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1660 msg_namelen);
1661 if (err)
1662 return err;
1663
1664 if (msg_namelen > sizeof(to))
1665 msg_namelen = sizeof(to);
1666 memcpy(&to, msg->msg_name, msg_namelen);
1da177e4
LT
1667 msg_name = msg->msg_name;
1668 }
1669
1670 sinfo = cmsgs.info;
1671 sinit = cmsgs.init;
1672
1673 /* Did the user specify SNDRCVINFO? */
1674 if (sinfo) {
1675 sinfo_flags = sinfo->sinfo_flags;
1676 associd = sinfo->sinfo_assoc_id;
1677 }
1678
1679 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1680 msg_len, sinfo_flags);
1681
eaa5c54d
ISJ
1682 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1683 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1da177e4
LT
1684 err = -EINVAL;
1685 goto out_nounlock;
1686 }
1687
eaa5c54d
ISJ
1688 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1689 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1690 * If SCTP_ABORT is set, the message length could be non zero with
1da177e4 1691 * the msg_iov set to the user abort reason.
d808ad9a 1692 */
eaa5c54d
ISJ
1693 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1694 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1da177e4
LT
1695 err = -EINVAL;
1696 goto out_nounlock;
1697 }
1698
eaa5c54d 1699 /* If SCTP_ADDR_OVER is set, there must be an address
1da177e4
LT
1700 * specified in msg_name.
1701 */
eaa5c54d 1702 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1da177e4
LT
1703 err = -EINVAL;
1704 goto out_nounlock;
1705 }
1706
1707 transport = NULL;
1708
1709 SCTP_DEBUG_PRINTK("About to look up association.\n");
1710
1711 sctp_lock_sock(sk);
1712
1713 /* If a msg_name has been specified, assume this is to be used. */
1714 if (msg_name) {
1715 /* Look for a matching association on the endpoint. */
dce116ae 1716 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1da177e4
LT
1717 if (!asoc) {
1718 /* If we could not find a matching association on the
1719 * endpoint, make sure that it is not a TCP-style
1720 * socket that already has an association or there is
1721 * no peeled-off association on another socket.
1722 */
1723 if ((sctp_style(sk, TCP) &&
1724 sctp_sstate(sk, ESTABLISHED)) ||
dce116ae 1725 sctp_endpoint_is_peeled_off(ep, &to)) {
1da177e4
LT
1726 err = -EADDRNOTAVAIL;
1727 goto out_unlock;
1728 }
1729 }
1730 } else {
1731 asoc = sctp_id2assoc(sk, associd);
1732 if (!asoc) {
1733 err = -EPIPE;
1734 goto out_unlock;
1735 }
1736 }
1737
1738 if (asoc) {
1739 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1740
1741 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1742 * socket that has an association in CLOSED state. This can
1743 * happen when an accepted socket has an association that is
1744 * already CLOSED.
1745 */
1746 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1747 err = -EPIPE;
1748 goto out_unlock;
1749 }
1750
eaa5c54d 1751 if (sinfo_flags & SCTP_EOF) {
1da177e4
LT
1752 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1753 asoc);
55e26eb9 1754 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
1755 err = 0;
1756 goto out_unlock;
1757 }
eaa5c54d 1758 if (sinfo_flags & SCTP_ABORT) {
c164a9ba
SS
1759
1760 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1761 if (!chunk) {
1762 err = -ENOMEM;
1763 goto out_unlock;
1764 }
1765
1da177e4 1766 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
55e26eb9 1767 sctp_primitive_ABORT(net, asoc, chunk);
1da177e4
LT
1768 err = 0;
1769 goto out_unlock;
1770 }
1771 }
1772
1773 /* Do we need to create the association? */
1774 if (!asoc) {
1775 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1776
eaa5c54d 1777 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1da177e4
LT
1778 err = -EINVAL;
1779 goto out_unlock;
1780 }
1781
1782 /* Check for invalid stream against the stream counts,
1783 * either the default or the user specified stream counts.
1784 */
1785 if (sinfo) {
1786 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1787 /* Check against the defaults. */
1788 if (sinfo->sinfo_stream >=
1789 sp->initmsg.sinit_num_ostreams) {
1790 err = -EINVAL;
1791 goto out_unlock;
1792 }
1793 } else {
1794 /* Check against the requested. */
1795 if (sinfo->sinfo_stream >=
1796 sinit->sinit_num_ostreams) {
1797 err = -EINVAL;
1798 goto out_unlock;
1799 }
1800 }
1801 }
1802
1803 /*
1804 * API 3.1.2 bind() - UDP Style Syntax
1805 * If a bind() or sctp_bindx() is not called prior to a
1806 * sendmsg() call that initiates a new association, the
1807 * system picks an ephemeral port and will choose an address
1808 * set equivalent to binding with a wildcard address.
1809 */
1810 if (!ep->base.bind_addr.port) {
1811 if (sctp_autobind(sk)) {
1812 err = -EAGAIN;
1813 goto out_unlock;
1814 }
64a0c1c8
ISJ
1815 } else {
1816 /*
1817 * If an unprivileged user inherits a one-to-many
1818 * style socket with open associations on a privileged
1819 * port, it MAY be permitted to accept new associations,
1820 * but it SHOULD NOT be permitted to open new
1821 * associations.
1822 */
1823 if (ep->base.bind_addr.port < PROT_SOCK &&
3594698a 1824 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
64a0c1c8
ISJ
1825 err = -EACCES;
1826 goto out_unlock;
1827 }
1da177e4
LT
1828 }
1829
1830 scope = sctp_scope(&to);
1831 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1832 if (!new_asoc) {
1833 err = -ENOMEM;
1834 goto out_unlock;
1835 }
1836 asoc = new_asoc;
409b95af
VY
1837 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1838 if (err < 0) {
1839 err = -ENOMEM;
1840 goto out_free;
1841 }
1da177e4
LT
1842
1843 /* If the SCTP_INIT ancillary data is specified, set all
1844 * the association init values accordingly.
1845 */
1846 if (sinit) {
1847 if (sinit->sinit_num_ostreams) {
1848 asoc->c.sinit_num_ostreams =
1849 sinit->sinit_num_ostreams;
1850 }
1851 if (sinit->sinit_max_instreams) {
1852 asoc->c.sinit_max_instreams =
1853 sinit->sinit_max_instreams;
1854 }
1855 if (sinit->sinit_max_attempts) {
1856 asoc->max_init_attempts
1857 = sinit->sinit_max_attempts;
1858 }
1859 if (sinit->sinit_max_init_timeo) {
d808ad9a 1860 asoc->max_init_timeo =
1da177e4
LT
1861 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1862 }
1863 }
1864
1865 /* Prime the peer's transport structures. */
dce116ae 1866 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1da177e4
LT
1867 if (!transport) {
1868 err = -ENOMEM;
1869 goto out_free;
1870 }
1da177e4
LT
1871 }
1872
1873 /* ASSERT: we have a valid association at this point. */
1874 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1875
1876 if (!sinfo) {
1877 /* If the user didn't specify SNDRCVINFO, make up one with
1878 * some defaults.
1879 */
517aa0bc 1880 memset(&default_sinfo, 0, sizeof(default_sinfo));
1da177e4
LT
1881 default_sinfo.sinfo_stream = asoc->default_stream;
1882 default_sinfo.sinfo_flags = asoc->default_flags;
1883 default_sinfo.sinfo_ppid = asoc->default_ppid;
1884 default_sinfo.sinfo_context = asoc->default_context;
1885 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1886 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1887 sinfo = &default_sinfo;
1888 }
1889
1890 /* API 7.1.7, the sndbuf size per association bounds the
1891 * maximum size of data that can be sent in a single send call.
1892 */
1893 if (msg_len > sk->sk_sndbuf) {
1894 err = -EMSGSIZE;
1895 goto out_free;
1896 }
1897
8a479491 1898 if (asoc->pmtu_pending)
02f3d4ce 1899 sctp_assoc_pending_pmtu(sk, asoc);
8a479491 1900
1da177e4
LT
1901 /* If fragmentation is disabled and the message length exceeds the
1902 * association fragmentation point, return EMSGSIZE. The I-D
1903 * does not specify what this error is, but this looks like
1904 * a great fit.
1905 */
1906 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1907 err = -EMSGSIZE;
1908 goto out_free;
1909 }
1910
afd7614c
JP
1911 /* Check for invalid stream. */
1912 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1913 err = -EINVAL;
1914 goto out_free;
1da177e4
LT
1915 }
1916
1917 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1918 if (!sctp_wspace(asoc)) {
1919 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1920 if (err)
1921 goto out_free;
1922 }
1923
1924 /* If an address is passed with the sendto/sendmsg call, it is used
1925 * to override the primary destination address in the TCP model, or
eaa5c54d 1926 * when SCTP_ADDR_OVER flag is set in the UDP model.
1da177e4
LT
1927 */
1928 if ((sctp_style(sk, TCP) && msg_name) ||
eaa5c54d 1929 (sinfo_flags & SCTP_ADDR_OVER)) {
dce116ae 1930 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1da177e4
LT
1931 if (!chunk_tp) {
1932 err = -EINVAL;
1933 goto out_free;
1934 }
1935 } else
1936 chunk_tp = NULL;
1937
1938 /* Auto-connect, if we aren't connected already. */
1939 if (sctp_state(asoc, CLOSED)) {
55e26eb9 1940 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1da177e4
LT
1941 if (err < 0)
1942 goto out_free;
1943 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1944 }
1945
1946 /* Break the message into multiple chunks of maximum size. */
1947 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
6e51fe75
TR
1948 if (IS_ERR(datamsg)) {
1949 err = PTR_ERR(datamsg);
1da177e4
LT
1950 goto out_free;
1951 }
1952
1953 /* Now send the (possibly) fragmented message. */
9dbc15f0 1954 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
80445cfb 1955 sctp_chunk_hold(chunk);
1da177e4
LT
1956
1957 /* Do accounting for the write space. */
1958 sctp_set_owner_w(chunk);
1959
1960 chunk->transport = chunk_tp;
1da177e4
LT
1961 }
1962
9c5c62be
VY
1963 /* Send it to the lower layers. Note: all chunks
1964 * must either fail or succeed. The lower layer
1965 * works that way today. Keep it that way or this
1966 * breaks.
1967 */
55e26eb9 1968 err = sctp_primitive_SEND(net, asoc, datamsg);
9c5c62be
VY
1969 /* Did the lower layer accept the chunk? */
1970 if (err)
1971 sctp_datamsg_free(datamsg);
1972 else
1973 sctp_datamsg_put(datamsg);
1974
1975 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1976
1da177e4
LT
1977 if (err)
1978 goto out_free;
1979 else
1980 err = msg_len;
1981
1982 /* If we are already past ASSOCIATE, the lower
1983 * layers are responsible for association cleanup.
1984 */
1985 goto out_unlock;
1986
1987out_free:
2eebc1e1
NH
1988 if (new_asoc) {
1989 sctp_unhash_established(asoc);
1da177e4 1990 sctp_association_free(asoc);
2eebc1e1 1991 }
1da177e4
LT
1992out_unlock:
1993 sctp_release_sock(sk);
1994
1995out_nounlock:
1996 return sctp_error(sk, msg_flags, err);
1997
1998#if 0
1999do_sock_err:
2000 if (msg_len)
2001 err = msg_len;
2002 else
2003 err = sock_error(sk);
2004 goto out;
2005
2006do_interrupted:
2007 if (msg_len)
2008 err = msg_len;
2009 goto out;
2010#endif /* 0 */
2011}
2012
2013/* This is an extended version of skb_pull() that removes the data from the
2014 * start of a skb even when data is spread across the list of skb's in the
2015 * frag_list. len specifies the total amount of data that needs to be removed.
2016 * when 'len' bytes could be removed from the skb, it returns 0.
2017 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2018 * could not be removed.
2019 */
2020static int sctp_skb_pull(struct sk_buff *skb, int len)
2021{
2022 struct sk_buff *list;
2023 int skb_len = skb_headlen(skb);
2024 int rlen;
2025
2026 if (len <= skb_len) {
2027 __skb_pull(skb, len);
2028 return 0;
2029 }
2030 len -= skb_len;
2031 __skb_pull(skb, skb_len);
2032
1b003be3 2033 skb_walk_frags(skb, list) {
1da177e4
LT
2034 rlen = sctp_skb_pull(list, len);
2035 skb->len -= (len-rlen);
2036 skb->data_len -= (len-rlen);
2037
2038 if (!rlen)
2039 return 0;
2040
2041 len = rlen;
2042 }
2043
2044 return len;
2045}
2046
2047/* API 3.1.3 recvmsg() - UDP Style Syntax
2048 *
2049 * ssize_t recvmsg(int socket, struct msghdr *message,
2050 * int flags);
2051 *
2052 * socket - the socket descriptor of the endpoint.
2053 * message - pointer to the msghdr structure which contains a single
2054 * user message and possibly some ancillary data.
2055 *
2056 * See Section 5 for complete description of the data
2057 * structures.
2058 *
2059 * flags - flags sent or received with the user message, see Section
2060 * 5 for complete description of the flags.
2061 */
2062static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2063
2064SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2065 struct msghdr *msg, size_t len, int noblock,
2066 int flags, int *addr_len)
2067{
2068 struct sctp_ulpevent *event = NULL;
2069 struct sctp_sock *sp = sctp_sk(sk);
2070 struct sk_buff *skb;
2071 int copied;
2072 int err = 0;
2073 int skb_len;
2074
2075 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2076 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2077 "len", len, "knoblauch", noblock,
2078 "flags", flags, "addr_len", addr_len);
2079
2080 sctp_lock_sock(sk);
2081
2082 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2083 err = -ENOTCONN;
2084 goto out;
2085 }
2086
2087 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2088 if (!skb)
2089 goto out;
2090
2091 /* Get the total length of the skb including any skb's in the
2092 * frag_list.
2093 */
2094 skb_len = skb->len;
2095
2096 copied = skb_len;
2097 if (copied > len)
2098 copied = len;
2099
2100 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2101
2102 event = sctp_skb2event(skb);
2103
2104 if (err)
2105 goto out_free;
2106
3b885787 2107 sock_recv_ts_and_drops(msg, sk, skb);
1da177e4
LT
2108 if (sctp_ulpevent_is_notification(event)) {
2109 msg->msg_flags |= MSG_NOTIFICATION;
2110 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2111 } else {
2112 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2113 }
2114
2115 /* Check if we allow SCTP_SNDRCVINFO. */
2116 if (sp->subscribe.sctp_data_io_event)
2117 sctp_ulpevent_read_sndrcvinfo(event, msg);
2118#if 0
2119 /* FIXME: we should be calling IP/IPv6 layers. */
2120 if (sk->sk_protinfo.af_inet.cmsg_flags)
2121 ip_cmsg_recv(msg, skb);
2122#endif
2123
2124 err = copied;
2125
2126 /* If skb's length exceeds the user's buffer, update the skb and
2127 * push it back to the receive_queue so that the next call to
2128 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2129 */
2130 if (skb_len > copied) {
2131 msg->msg_flags &= ~MSG_EOR;
2132 if (flags & MSG_PEEK)
2133 goto out_free;
2134 sctp_skb_pull(skb, copied);
2135 skb_queue_head(&sk->sk_receive_queue, skb);
2136
2137 /* When only partial message is copied to the user, increase
2138 * rwnd by that amount. If all the data in the skb is read,
2139 * rwnd is updated when the event is freed.
2140 */
0eca8fee
VY
2141 if (!sctp_ulpevent_is_notification(event))
2142 sctp_assoc_rwnd_increase(event->asoc, copied);
1da177e4
LT
2143 goto out;
2144 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2145 (event->msg_flags & MSG_EOR))
2146 msg->msg_flags |= MSG_EOR;
2147 else
2148 msg->msg_flags &= ~MSG_EOR;
2149
2150out_free:
2151 if (flags & MSG_PEEK) {
2152 /* Release the skb reference acquired after peeking the skb in
2153 * sctp_skb_recv_datagram().
2154 */
2155 kfree_skb(skb);
2156 } else {
2157 /* Free the event which includes releasing the reference to
2158 * the owner of the skb, freeing the skb and updating the
2159 * rwnd.
2160 */
2161 sctp_ulpevent_free(event);
2162 }
2163out:
2164 sctp_release_sock(sk);
2165 return err;
2166}
2167
2168/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2169 *
2170 * This option is a on/off flag. If enabled no SCTP message
2171 * fragmentation will be performed. Instead if a message being sent
2172 * exceeds the current PMTU size, the message will NOT be sent and
2173 * instead a error will be indicated to the user.
2174 */
2175static int sctp_setsockopt_disable_fragments(struct sock *sk,
b7058842
DM
2176 char __user *optval,
2177 unsigned int optlen)
1da177e4
LT
2178{
2179 int val;
2180
2181 if (optlen < sizeof(int))
2182 return -EINVAL;
2183
2184 if (get_user(val, (int __user *)optval))
2185 return -EFAULT;
2186
2187 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2188
2189 return 0;
2190}
2191
2192static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
b7058842 2193 unsigned int optlen)
1da177e4 2194{
94912301
WY
2195 struct sctp_association *asoc;
2196 struct sctp_ulpevent *event;
2197
7e8616d8 2198 if (optlen > sizeof(struct sctp_event_subscribe))
1da177e4
LT
2199 return -EINVAL;
2200 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2201 return -EFAULT;
94912301
WY
2202
2203 /*
2204 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2205 * if there is no data to be sent or retransmit, the stack will
2206 * immediately send up this notification.
2207 */
2208 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2209 &sctp_sk(sk)->subscribe)) {
2210 asoc = sctp_id2assoc(sk, 0);
2211
2212 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2213 event = sctp_ulpevent_make_sender_dry_event(asoc,
2214 GFP_ATOMIC);
2215 if (!event)
2216 return -ENOMEM;
2217
2218 sctp_ulpq_tail_event(&asoc->ulpq, event);
2219 }
2220 }
2221
1da177e4
LT
2222 return 0;
2223}
2224
2225/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2226 *
2227 * This socket option is applicable to the UDP-style socket only. When
2228 * set it will cause associations that are idle for more than the
2229 * specified number of seconds to automatically close. An association
2230 * being idle is defined an association that has NOT sent or received
2231 * user data. The special value of '0' indicates that no automatic
2232 * close of any associations should be performed. The option expects an
2233 * integer defining the number of seconds of idle time before an
2234 * association is closed.
2235 */
2236static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
b7058842 2237 unsigned int optlen)
1da177e4
LT
2238{
2239 struct sctp_sock *sp = sctp_sk(sk);
2240
2241 /* Applicable to UDP-style socket only */
2242 if (sctp_style(sk, TCP))
2243 return -EOPNOTSUPP;
2244 if (optlen != sizeof(int))
2245 return -EINVAL;
2246 if (copy_from_user(&sp->autoclose, optval, optlen))
2247 return -EFAULT;
2248
1da177e4
LT
2249 return 0;
2250}
2251
2252/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2253 *
2254 * Applications can enable or disable heartbeats for any peer address of
2255 * an association, modify an address's heartbeat interval, force a
2256 * heartbeat to be sent immediately, and adjust the address's maximum
2257 * number of retransmissions sent before an address is considered
2258 * unreachable. The following structure is used to access and modify an
2259 * address's parameters:
2260 *
2261 * struct sctp_paddrparams {
52ccb8e9
FF
2262 * sctp_assoc_t spp_assoc_id;
2263 * struct sockaddr_storage spp_address;
2264 * uint32_t spp_hbinterval;
2265 * uint16_t spp_pathmaxrxt;
2266 * uint32_t spp_pathmtu;
2267 * uint32_t spp_sackdelay;
2268 * uint32_t spp_flags;
2269 * };
2270 *
2271 * spp_assoc_id - (one-to-many style socket) This is filled in the
2272 * application, and identifies the association for
2273 * this query.
1da177e4
LT
2274 * spp_address - This specifies which address is of interest.
2275 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
2276 * in milliseconds. If a value of zero
2277 * is present in this field then no changes are to
2278 * be made to this parameter.
1da177e4
LT
2279 * spp_pathmaxrxt - This contains the maximum number of
2280 * retransmissions before this address shall be
52ccb8e9
FF
2281 * considered unreachable. If a value of zero
2282 * is present in this field then no changes are to
2283 * be made to this parameter.
2284 * spp_pathmtu - When Path MTU discovery is disabled the value
2285 * specified here will be the "fixed" path mtu.
2286 * Note that if the spp_address field is empty
2287 * then all associations on this address will
2288 * have this fixed path mtu set upon them.
2289 *
2290 * spp_sackdelay - When delayed sack is enabled, this value specifies
2291 * the number of milliseconds that sacks will be delayed
2292 * for. This value will apply to all addresses of an
2293 * association if the spp_address field is empty. Note
2294 * also, that if delayed sack is enabled and this
2295 * value is set to 0, no change is made to the last
2296 * recorded delayed sack timer value.
2297 *
2298 * spp_flags - These flags are used to control various features
2299 * on an association. The flag field may contain
2300 * zero or more of the following options.
2301 *
2302 * SPP_HB_ENABLE - Enable heartbeats on the
2303 * specified address. Note that if the address
2304 * field is empty all addresses for the association
2305 * have heartbeats enabled upon them.
2306 *
2307 * SPP_HB_DISABLE - Disable heartbeats on the
2308 * speicifed address. Note that if the address
2309 * field is empty all addresses for the association
2310 * will have their heartbeats disabled. Note also
2311 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2312 * mutually exclusive, only one of these two should
2313 * be specified. Enabling both fields will have
2314 * undetermined results.
2315 *
2316 * SPP_HB_DEMAND - Request a user initiated heartbeat
2317 * to be made immediately.
2318 *
bdf3092a
VY
2319 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2320 * heartbeat delayis to be set to the value of 0
2321 * milliseconds.
2322 *
52ccb8e9
FF
2323 * SPP_PMTUD_ENABLE - This field will enable PMTU
2324 * discovery upon the specified address. Note that
2325 * if the address feild is empty then all addresses
2326 * on the association are effected.
2327 *
2328 * SPP_PMTUD_DISABLE - This field will disable PMTU
2329 * discovery upon the specified address. Note that
2330 * if the address feild is empty then all addresses
2331 * on the association are effected. Not also that
2332 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2333 * exclusive. Enabling both will have undetermined
2334 * results.
2335 *
2336 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2337 * on delayed sack. The time specified in spp_sackdelay
2338 * is used to specify the sack delay for this address. Note
2339 * that if spp_address is empty then all addresses will
2340 * enable delayed sack and take on the sack delay
2341 * value specified in spp_sackdelay.
2342 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2343 * off delayed sack. If the spp_address field is blank then
2344 * delayed sack is disabled for the entire association. Note
2345 * also that this field is mutually exclusive to
2346 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2347 * results.
1da177e4 2348 */
16164366
AB
2349static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2350 struct sctp_transport *trans,
2351 struct sctp_association *asoc,
2352 struct sctp_sock *sp,
2353 int hb_change,
2354 int pmtud_change,
2355 int sackdelay_change)
52ccb8e9
FF
2356{
2357 int error;
2358
2359 if (params->spp_flags & SPP_HB_DEMAND && trans) {
55e26eb9
EB
2360 struct net *net = sock_net(trans->asoc->base.sk);
2361
2362 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
52ccb8e9
FF
2363 if (error)
2364 return error;
2365 }
2366
bdf3092a
VY
2367 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2368 * this field is ignored. Note also that a value of zero indicates
2369 * the current setting should be left unchanged.
2370 */
2371 if (params->spp_flags & SPP_HB_ENABLE) {
2372
2373 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2374 * set. This lets us use 0 value when this flag
2375 * is set.
2376 */
2377 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2378 params->spp_hbinterval = 0;
2379
2380 if (params->spp_hbinterval ||
2381 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2382 if (trans) {
2383 trans->hbinterval =
2384 msecs_to_jiffies(params->spp_hbinterval);
2385 } else if (asoc) {
2386 asoc->hbinterval =
2387 msecs_to_jiffies(params->spp_hbinterval);
2388 } else {
2389 sp->hbinterval = params->spp_hbinterval;
2390 }
52ccb8e9
FF
2391 }
2392 }
2393
2394 if (hb_change) {
2395 if (trans) {
2396 trans->param_flags =
2397 (trans->param_flags & ~SPP_HB) | hb_change;
2398 } else if (asoc) {
2399 asoc->param_flags =
2400 (asoc->param_flags & ~SPP_HB) | hb_change;
2401 } else {
2402 sp->param_flags =
2403 (sp->param_flags & ~SPP_HB) | hb_change;
2404 }
2405 }
2406
bdf3092a
VY
2407 /* When Path MTU discovery is disabled the value specified here will
2408 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2409 * include the flag SPP_PMTUD_DISABLE for this field to have any
2410 * effect).
2411 */
2412 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
52ccb8e9
FF
2413 if (trans) {
2414 trans->pathmtu = params->spp_pathmtu;
02f3d4ce 2415 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
52ccb8e9
FF
2416 } else if (asoc) {
2417 asoc->pathmtu = params->spp_pathmtu;
f68b2e05 2418 sctp_frag_point(asoc, params->spp_pathmtu);
52ccb8e9
FF
2419 } else {
2420 sp->pathmtu = params->spp_pathmtu;
2421 }
2422 }
2423
2424 if (pmtud_change) {
2425 if (trans) {
2426 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2427 (params->spp_flags & SPP_PMTUD_ENABLE);
2428 trans->param_flags =
2429 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2430 if (update) {
9914ae3c 2431 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
02f3d4ce 2432 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
52ccb8e9
FF
2433 }
2434 } else if (asoc) {
2435 asoc->param_flags =
2436 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2437 } else {
2438 sp->param_flags =
2439 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2440 }
2441 }
2442
bdf3092a
VY
2443 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2444 * value of this field is ignored. Note also that a value of zero
2445 * indicates the current setting should be left unchanged.
2446 */
2447 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
52ccb8e9
FF
2448 if (trans) {
2449 trans->sackdelay =
2450 msecs_to_jiffies(params->spp_sackdelay);
2451 } else if (asoc) {
2452 asoc->sackdelay =
2453 msecs_to_jiffies(params->spp_sackdelay);
2454 } else {
2455 sp->sackdelay = params->spp_sackdelay;
2456 }
2457 }
2458
2459 if (sackdelay_change) {
2460 if (trans) {
2461 trans->param_flags =
2462 (trans->param_flags & ~SPP_SACKDELAY) |
2463 sackdelay_change;
2464 } else if (asoc) {
2465 asoc->param_flags =
2466 (asoc->param_flags & ~SPP_SACKDELAY) |
2467 sackdelay_change;
2468 } else {
2469 sp->param_flags =
2470 (sp->param_flags & ~SPP_SACKDELAY) |
2471 sackdelay_change;
2472 }
2473 }
2474
37051f73
APO
2475 /* Note that a value of zero indicates the current setting should be
2476 left unchanged.
bdf3092a 2477 */
37051f73 2478 if (params->spp_pathmaxrxt) {
52ccb8e9
FF
2479 if (trans) {
2480 trans->pathmaxrxt = params->spp_pathmaxrxt;
2481 } else if (asoc) {
2482 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2483 } else {
2484 sp->pathmaxrxt = params->spp_pathmaxrxt;
2485 }
2486 }
2487
2488 return 0;
2489}
2490
1da177e4 2491static int sctp_setsockopt_peer_addr_params(struct sock *sk,
b7058842
DM
2492 char __user *optval,
2493 unsigned int optlen)
1da177e4 2494{
52ccb8e9
FF
2495 struct sctp_paddrparams params;
2496 struct sctp_transport *trans = NULL;
2497 struct sctp_association *asoc = NULL;
2498 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 2499 int error;
52ccb8e9 2500 int hb_change, pmtud_change, sackdelay_change;
1da177e4
LT
2501
2502 if (optlen != sizeof(struct sctp_paddrparams))
52ccb8e9
FF
2503 return - EINVAL;
2504
1da177e4
LT
2505 if (copy_from_user(&params, optval, optlen))
2506 return -EFAULT;
2507
52ccb8e9
FF
2508 /* Validate flags and value parameters. */
2509 hb_change = params.spp_flags & SPP_HB;
2510 pmtud_change = params.spp_flags & SPP_PMTUD;
2511 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2512
2513 if (hb_change == SPP_HB ||
2514 pmtud_change == SPP_PMTUD ||
2515 sackdelay_change == SPP_SACKDELAY ||
2516 params.spp_sackdelay > 500 ||
f64f9e71
JP
2517 (params.spp_pathmtu &&
2518 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
52ccb8e9 2519 return -EINVAL;
1da177e4 2520
52ccb8e9
FF
2521 /* If an address other than INADDR_ANY is specified, and
2522 * no transport is found, then the request is invalid.
2523 */
52cae8f0 2524 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
2525 trans = sctp_addr_id2transport(sk, &params.spp_address,
2526 params.spp_assoc_id);
2527 if (!trans)
1da177e4 2528 return -EINVAL;
1da177e4
LT
2529 }
2530
52ccb8e9
FF
2531 /* Get association, if assoc_id != 0 and the socket is a one
2532 * to many style socket, and an association was not found, then
2533 * the id was invalid.
2534 */
2535 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2536 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
1da177e4
LT
2537 return -EINVAL;
2538
52ccb8e9
FF
2539 /* Heartbeat demand can only be sent on a transport or
2540 * association, but not a socket.
1da177e4 2541 */
52ccb8e9
FF
2542 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2543 return -EINVAL;
2544
2545 /* Process parameters. */
2546 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2547 hb_change, pmtud_change,
2548 sackdelay_change);
1da177e4 2549
52ccb8e9
FF
2550 if (error)
2551 return error;
2552
2553 /* If changes are for association, also apply parameters to each
2554 * transport.
1da177e4 2555 */
52ccb8e9 2556 if (!trans && asoc) {
9dbc15f0
RD
2557 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2558 transports) {
52ccb8e9
FF
2559 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2560 hb_change, pmtud_change,
2561 sackdelay_change);
2562 }
2563 }
1da177e4
LT
2564
2565 return 0;
2566}
2567
d364d927
WY
2568/*
2569 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2570 *
2571 * This option will effect the way delayed acks are performed. This
2572 * option allows you to get or set the delayed ack time, in
2573 * milliseconds. It also allows changing the delayed ack frequency.
2574 * Changing the frequency to 1 disables the delayed sack algorithm. If
2575 * the assoc_id is 0, then this sets or gets the endpoints default
2576 * values. If the assoc_id field is non-zero, then the set or get
2577 * effects the specified association for the one to many model (the
2578 * assoc_id field is ignored by the one to one model). Note that if
2579 * sack_delay or sack_freq are 0 when setting this option, then the
2580 * current values will remain unchanged.
2581 *
2582 * struct sctp_sack_info {
2583 * sctp_assoc_t sack_assoc_id;
2584 * uint32_t sack_delay;
2585 * uint32_t sack_freq;
2586 * };
2587 *
2588 * sack_assoc_id - This parameter, indicates which association the user
2589 * is performing an action upon. Note that if this field's value is
2590 * zero then the endpoints default value is changed (effecting future
2591 * associations only).
2592 *
2593 * sack_delay - This parameter contains the number of milliseconds that
2594 * the user is requesting the delayed ACK timer be set to. Note that
2595 * this value is defined in the standard to be between 200 and 500
2596 * milliseconds.
2597 *
2598 * sack_freq - This parameter contains the number of packets that must
2599 * be received before a sack is sent without waiting for the delay
2600 * timer to expire. The default value for this is 2, setting this
2601 * value to 1 will disable the delayed sack algorithm.
7708610b
FF
2602 */
2603
d364d927 2604static int sctp_setsockopt_delayed_ack(struct sock *sk,
b7058842 2605 char __user *optval, unsigned int optlen)
7708610b 2606{
d364d927 2607 struct sctp_sack_info params;
7708610b
FF
2608 struct sctp_transport *trans = NULL;
2609 struct sctp_association *asoc = NULL;
2610 struct sctp_sock *sp = sctp_sk(sk);
2611
d364d927
WY
2612 if (optlen == sizeof(struct sctp_sack_info)) {
2613 if (copy_from_user(&params, optval, optlen))
2614 return -EFAULT;
7708610b 2615
d364d927
WY
2616 if (params.sack_delay == 0 && params.sack_freq == 0)
2617 return 0;
2618 } else if (optlen == sizeof(struct sctp_assoc_value)) {
145ce502
JP
2619 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2620 pr_warn("Use struct sctp_sack_info instead\n");
d364d927
WY
2621 if (copy_from_user(&params, optval, optlen))
2622 return -EFAULT;
2623
2624 if (params.sack_delay == 0)
2625 params.sack_freq = 1;
2626 else
2627 params.sack_freq = 0;
2628 } else
2629 return - EINVAL;
7708610b
FF
2630
2631 /* Validate value parameter. */
d364d927 2632 if (params.sack_delay > 500)
7708610b
FF
2633 return -EINVAL;
2634
d364d927 2635 /* Get association, if sack_assoc_id != 0 and the socket is a one
7708610b
FF
2636 * to many style socket, and an association was not found, then
2637 * the id was invalid.
d808ad9a 2638 */
d364d927
WY
2639 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2640 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
7708610b
FF
2641 return -EINVAL;
2642
d364d927 2643 if (params.sack_delay) {
7708610b
FF
2644 if (asoc) {
2645 asoc->sackdelay =
d364d927 2646 msecs_to_jiffies(params.sack_delay);
d808ad9a 2647 asoc->param_flags =
7708610b
FF
2648 (asoc->param_flags & ~SPP_SACKDELAY) |
2649 SPP_SACKDELAY_ENABLE;
2650 } else {
d364d927 2651 sp->sackdelay = params.sack_delay;
d808ad9a 2652 sp->param_flags =
7708610b
FF
2653 (sp->param_flags & ~SPP_SACKDELAY) |
2654 SPP_SACKDELAY_ENABLE;
2655 }
d364d927
WY
2656 }
2657
2658 if (params.sack_freq == 1) {
7708610b 2659 if (asoc) {
d808ad9a 2660 asoc->param_flags =
7708610b
FF
2661 (asoc->param_flags & ~SPP_SACKDELAY) |
2662 SPP_SACKDELAY_DISABLE;
2663 } else {
d808ad9a 2664 sp->param_flags =
7708610b
FF
2665 (sp->param_flags & ~SPP_SACKDELAY) |
2666 SPP_SACKDELAY_DISABLE;
2667 }
d364d927
WY
2668 } else if (params.sack_freq > 1) {
2669 if (asoc) {
2670 asoc->sackfreq = params.sack_freq;
2671 asoc->param_flags =
2672 (asoc->param_flags & ~SPP_SACKDELAY) |
2673 SPP_SACKDELAY_ENABLE;
2674 } else {
2675 sp->sackfreq = params.sack_freq;
2676 sp->param_flags =
2677 (sp->param_flags & ~SPP_SACKDELAY) |
2678 SPP_SACKDELAY_ENABLE;
2679 }
7708610b
FF
2680 }
2681
2682 /* If change is for association, also apply to each transport. */
2683 if (asoc) {
9dbc15f0
RD
2684 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2685 transports) {
d364d927 2686 if (params.sack_delay) {
7708610b 2687 trans->sackdelay =
d364d927 2688 msecs_to_jiffies(params.sack_delay);
d808ad9a 2689 trans->param_flags =
7708610b
FF
2690 (trans->param_flags & ~SPP_SACKDELAY) |
2691 SPP_SACKDELAY_ENABLE;
d364d927 2692 }
7bfe8bdb 2693 if (params.sack_freq == 1) {
d808ad9a 2694 trans->param_flags =
7708610b
FF
2695 (trans->param_flags & ~SPP_SACKDELAY) |
2696 SPP_SACKDELAY_DISABLE;
d364d927
WY
2697 } else if (params.sack_freq > 1) {
2698 trans->sackfreq = params.sack_freq;
2699 trans->param_flags =
2700 (trans->param_flags & ~SPP_SACKDELAY) |
2701 SPP_SACKDELAY_ENABLE;
7708610b
FF
2702 }
2703 }
2704 }
d808ad9a 2705
7708610b
FF
2706 return 0;
2707}
2708
1da177e4
LT
2709/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2710 *
2711 * Applications can specify protocol parameters for the default association
2712 * initialization. The option name argument to setsockopt() and getsockopt()
2713 * is SCTP_INITMSG.
2714 *
2715 * Setting initialization parameters is effective only on an unconnected
2716 * socket (for UDP-style sockets only future associations are effected
2717 * by the change). With TCP-style sockets, this option is inherited by
2718 * sockets derived from a listener socket.
2719 */
b7058842 2720static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2721{
2722 struct sctp_initmsg sinit;
2723 struct sctp_sock *sp = sctp_sk(sk);
2724
2725 if (optlen != sizeof(struct sctp_initmsg))
2726 return -EINVAL;
2727 if (copy_from_user(&sinit, optval, optlen))
2728 return -EFAULT;
2729
2730 if (sinit.sinit_num_ostreams)
d808ad9a 2731 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
1da177e4 2732 if (sinit.sinit_max_instreams)
d808ad9a 2733 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
1da177e4 2734 if (sinit.sinit_max_attempts)
d808ad9a 2735 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
1da177e4 2736 if (sinit.sinit_max_init_timeo)
d808ad9a 2737 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
1da177e4
LT
2738
2739 return 0;
2740}
2741
2742/*
2743 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2744 *
2745 * Applications that wish to use the sendto() system call may wish to
2746 * specify a default set of parameters that would normally be supplied
2747 * through the inclusion of ancillary data. This socket option allows
2748 * such an application to set the default sctp_sndrcvinfo structure.
2749 * The application that wishes to use this socket option simply passes
2750 * in to this call the sctp_sndrcvinfo structure defined in Section
2751 * 5.2.2) The input parameters accepted by this call include
2752 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2753 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2754 * to this call if the caller is using the UDP model.
2755 */
2756static int sctp_setsockopt_default_send_param(struct sock *sk,
b7058842
DM
2757 char __user *optval,
2758 unsigned int optlen)
1da177e4
LT
2759{
2760 struct sctp_sndrcvinfo info;
2761 struct sctp_association *asoc;
2762 struct sctp_sock *sp = sctp_sk(sk);
2763
2764 if (optlen != sizeof(struct sctp_sndrcvinfo))
2765 return -EINVAL;
2766 if (copy_from_user(&info, optval, optlen))
2767 return -EFAULT;
2768
2769 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2770 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2771 return -EINVAL;
2772
2773 if (asoc) {
2774 asoc->default_stream = info.sinfo_stream;
2775 asoc->default_flags = info.sinfo_flags;
2776 asoc->default_ppid = info.sinfo_ppid;
2777 asoc->default_context = info.sinfo_context;
2778 asoc->default_timetolive = info.sinfo_timetolive;
2779 } else {
2780 sp->default_stream = info.sinfo_stream;
2781 sp->default_flags = info.sinfo_flags;
2782 sp->default_ppid = info.sinfo_ppid;
2783 sp->default_context = info.sinfo_context;
2784 sp->default_timetolive = info.sinfo_timetolive;
2785 }
2786
2787 return 0;
2788}
2789
2790/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2791 *
2792 * Requests that the local SCTP stack use the enclosed peer address as
2793 * the association primary. The enclosed address must be one of the
2794 * association peer's addresses.
2795 */
2796static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
b7058842 2797 unsigned int optlen)
1da177e4
LT
2798{
2799 struct sctp_prim prim;
2800 struct sctp_transport *trans;
2801
2802 if (optlen != sizeof(struct sctp_prim))
2803 return -EINVAL;
2804
2805 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2806 return -EFAULT;
2807
2808 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2809 if (!trans)
2810 return -EINVAL;
2811
2812 sctp_assoc_set_primary(trans->asoc, trans);
2813
2814 return 0;
2815}
2816
2817/*
2818 * 7.1.5 SCTP_NODELAY
2819 *
2820 * Turn on/off any Nagle-like algorithm. This means that packets are
2821 * generally sent as soon as possible and no unnecessary delays are
2822 * introduced, at the cost of more packets in the network. Expects an
2823 * integer boolean flag.
2824 */
2825static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
b7058842 2826 unsigned int optlen)
1da177e4
LT
2827{
2828 int val;
2829
2830 if (optlen < sizeof(int))
2831 return -EINVAL;
2832 if (get_user(val, (int __user *)optval))
2833 return -EFAULT;
2834
2835 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2836 return 0;
2837}
2838
2839/*
2840 *
2841 * 7.1.1 SCTP_RTOINFO
2842 *
2843 * The protocol parameters used to initialize and bound retransmission
2844 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2845 * and modify these parameters.
2846 * All parameters are time values, in milliseconds. A value of 0, when
2847 * modifying the parameters, indicates that the current value should not
2848 * be changed.
2849 *
2850 */
b7058842
DM
2851static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2852{
1da177e4
LT
2853 struct sctp_rtoinfo rtoinfo;
2854 struct sctp_association *asoc;
2855
2856 if (optlen != sizeof (struct sctp_rtoinfo))
2857 return -EINVAL;
2858
2859 if (copy_from_user(&rtoinfo, optval, optlen))
2860 return -EFAULT;
2861
2862 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2863
2864 /* Set the values to the specific association */
2865 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2866 return -EINVAL;
2867
2868 if (asoc) {
2869 if (rtoinfo.srto_initial != 0)
d808ad9a 2870 asoc->rto_initial =
1da177e4
LT
2871 msecs_to_jiffies(rtoinfo.srto_initial);
2872 if (rtoinfo.srto_max != 0)
2873 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2874 if (rtoinfo.srto_min != 0)
2875 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2876 } else {
2877 /* If there is no association or the association-id = 0
2878 * set the values to the endpoint.
2879 */
2880 struct sctp_sock *sp = sctp_sk(sk);
2881
2882 if (rtoinfo.srto_initial != 0)
2883 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2884 if (rtoinfo.srto_max != 0)
2885 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2886 if (rtoinfo.srto_min != 0)
2887 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2888 }
2889
2890 return 0;
2891}
2892
2893/*
2894 *
2895 * 7.1.2 SCTP_ASSOCINFO
2896 *
59c51591 2897 * This option is used to tune the maximum retransmission attempts
1da177e4
LT
2898 * of the association.
2899 * Returns an error if the new association retransmission value is
2900 * greater than the sum of the retransmission value of the peer.
2901 * See [SCTP] for more information.
2902 *
2903 */
b7058842 2904static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2905{
2906
2907 struct sctp_assocparams assocparams;
2908 struct sctp_association *asoc;
2909
2910 if (optlen != sizeof(struct sctp_assocparams))
2911 return -EINVAL;
2912 if (copy_from_user(&assocparams, optval, optlen))
2913 return -EFAULT;
2914
2915 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2916
2917 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2918 return -EINVAL;
2919
2920 /* Set the values to the specific association */
2921 if (asoc) {
402d68c4
VY
2922 if (assocparams.sasoc_asocmaxrxt != 0) {
2923 __u32 path_sum = 0;
2924 int paths = 0;
402d68c4
VY
2925 struct sctp_transport *peer_addr;
2926
9dbc15f0
RD
2927 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2928 transports) {
402d68c4
VY
2929 path_sum += peer_addr->pathmaxrxt;
2930 paths++;
2931 }
2932
025dfdaf 2933 /* Only validate asocmaxrxt if we have more than
402d68c4
VY
2934 * one path/transport. We do this because path
2935 * retransmissions are only counted when we have more
2936 * then one path.
2937 */
2938 if (paths > 1 &&
2939 assocparams.sasoc_asocmaxrxt > path_sum)
2940 return -EINVAL;
2941
1da177e4 2942 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
402d68c4
VY
2943 }
2944
1da177e4
LT
2945 if (assocparams.sasoc_cookie_life != 0) {
2946 asoc->cookie_life.tv_sec =
2947 assocparams.sasoc_cookie_life / 1000;
2948 asoc->cookie_life.tv_usec =
2949 (assocparams.sasoc_cookie_life % 1000)
2950 * 1000;
2951 }
2952 } else {
2953 /* Set the values to the endpoint */
2954 struct sctp_sock *sp = sctp_sk(sk);
2955
2956 if (assocparams.sasoc_asocmaxrxt != 0)
2957 sp->assocparams.sasoc_asocmaxrxt =
2958 assocparams.sasoc_asocmaxrxt;
2959 if (assocparams.sasoc_cookie_life != 0)
2960 sp->assocparams.sasoc_cookie_life =
2961 assocparams.sasoc_cookie_life;
2962 }
2963 return 0;
2964}
2965
2966/*
2967 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2968 *
2969 * This socket option is a boolean flag which turns on or off mapped V4
2970 * addresses. If this option is turned on and the socket is type
2971 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2972 * If this option is turned off, then no mapping will be done of V4
2973 * addresses and a user will receive both PF_INET6 and PF_INET type
2974 * addresses on the socket.
2975 */
b7058842 2976static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4
LT
2977{
2978 int val;
2979 struct sctp_sock *sp = sctp_sk(sk);
2980
2981 if (optlen < sizeof(int))
2982 return -EINVAL;
2983 if (get_user(val, (int __user *)optval))
2984 return -EFAULT;
2985 if (val)
2986 sp->v4mapped = 1;
2987 else
2988 sp->v4mapped = 0;
2989
2990 return 0;
2991}
2992
2993/*
e89c2095
WY
2994 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2995 * This option will get or set the maximum size to put in any outgoing
2996 * SCTP DATA chunk. If a message is larger than this size it will be
1da177e4
LT
2997 * fragmented by SCTP into the specified size. Note that the underlying
2998 * SCTP implementation may fragment into smaller sized chunks when the
2999 * PMTU of the underlying association is smaller than the value set by
e89c2095
WY
3000 * the user. The default value for this option is '0' which indicates
3001 * the user is NOT limiting fragmentation and only the PMTU will effect
3002 * SCTP's choice of DATA chunk size. Note also that values set larger
3003 * than the maximum size of an IP datagram will effectively let SCTP
3004 * control fragmentation (i.e. the same as setting this option to 0).
3005 *
3006 * The following structure is used to access and modify this parameter:
3007 *
3008 * struct sctp_assoc_value {
3009 * sctp_assoc_t assoc_id;
3010 * uint32_t assoc_value;
3011 * };
3012 *
3013 * assoc_id: This parameter is ignored for one-to-one style sockets.
3014 * For one-to-many style sockets this parameter indicates which
3015 * association the user is performing an action upon. Note that if
3016 * this field's value is zero then the endpoints default value is
3017 * changed (effecting future associations only).
3018 * assoc_value: This parameter specifies the maximum size in bytes.
1da177e4 3019 */
b7058842 3020static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
1da177e4 3021{
e89c2095 3022 struct sctp_assoc_value params;
1da177e4 3023 struct sctp_association *asoc;
1da177e4
LT
3024 struct sctp_sock *sp = sctp_sk(sk);
3025 int val;
3026
e89c2095 3027 if (optlen == sizeof(int)) {
145ce502
JP
3028 pr_warn("Use of int in maxseg socket option deprecated\n");
3029 pr_warn("Use struct sctp_assoc_value instead\n");
e89c2095
WY
3030 if (copy_from_user(&val, optval, optlen))
3031 return -EFAULT;
3032 params.assoc_id = 0;
3033 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3034 if (copy_from_user(&params, optval, optlen))
3035 return -EFAULT;
3036 val = params.assoc_value;
3037 } else
1da177e4 3038 return -EINVAL;
e89c2095 3039
96a33998 3040 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
1da177e4 3041 return -EINVAL;
1da177e4 3042
e89c2095
WY
3043 asoc = sctp_id2assoc(sk, params.assoc_id);
3044 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3045 return -EINVAL;
3046
3047 if (asoc) {
3048 if (val == 0) {
3049 val = asoc->pathmtu;
3050 val -= sp->pf->af->net_header_len;
3051 val -= sizeof(struct sctphdr) +
3052 sizeof(struct sctp_data_chunk);
3053 }
f68b2e05
VY
3054 asoc->user_frag = val;
3055 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
e89c2095
WY
3056 } else {
3057 sp->user_frag = val;
1da177e4
LT
3058 }
3059
3060 return 0;
3061}
3062
3063
3064/*
3065 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3066 *
3067 * Requests that the peer mark the enclosed address as the association
3068 * primary. The enclosed address must be one of the association's
3069 * locally bound addresses. The following structure is used to make a
3070 * set primary request:
3071 */
3072static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
b7058842 3073 unsigned int optlen)
1da177e4 3074{
e1fc3b14 3075 struct net *net = sock_net(sk);
1da177e4 3076 struct sctp_sock *sp;
1da177e4
LT
3077 struct sctp_association *asoc = NULL;
3078 struct sctp_setpeerprim prim;
3079 struct sctp_chunk *chunk;
40a01039 3080 struct sctp_af *af;
1da177e4
LT
3081 int err;
3082
3083 sp = sctp_sk(sk);
1da177e4 3084
e1fc3b14 3085 if (!net->sctp.addip_enable)
1da177e4
LT
3086 return -EPERM;
3087
3088 if (optlen != sizeof(struct sctp_setpeerprim))
3089 return -EINVAL;
3090
3091 if (copy_from_user(&prim, optval, optlen))
3092 return -EFAULT;
3093
3094 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
d808ad9a 3095 if (!asoc)
1da177e4
LT
3096 return -EINVAL;
3097
3098 if (!asoc->peer.asconf_capable)
3099 return -EPERM;
3100
3101 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3102 return -EPERM;
3103
3104 if (!sctp_state(asoc, ESTABLISHED))
3105 return -ENOTCONN;
3106
40a01039
WY
3107 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3108 if (!af)
3109 return -EINVAL;
3110
3111 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3112 return -EADDRNOTAVAIL;
3113
1da177e4
LT
3114 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3115 return -EADDRNOTAVAIL;
3116
3117 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3118 chunk = sctp_make_asconf_set_prim(asoc,
3119 (union sctp_addr *)&prim.sspp_addr);
3120 if (!chunk)
3121 return -ENOMEM;
3122
3123 err = sctp_send_asconf(asoc, chunk);
3124
3125 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3126
3127 return err;
3128}
3129
0f3fffd8 3130static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
b7058842 3131 unsigned int optlen)
1da177e4 3132{
0f3fffd8 3133 struct sctp_setadaptation adaptation;
1da177e4 3134
0f3fffd8 3135 if (optlen != sizeof(struct sctp_setadaptation))
1da177e4 3136 return -EINVAL;
0f3fffd8 3137 if (copy_from_user(&adaptation, optval, optlen))
1da177e4
LT
3138 return -EFAULT;
3139
0f3fffd8 3140 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
1da177e4
LT
3141
3142 return 0;
3143}
3144
6ab792f5
ISJ
3145/*
3146 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3147 *
3148 * The context field in the sctp_sndrcvinfo structure is normally only
3149 * used when a failed message is retrieved holding the value that was
3150 * sent down on the actual send call. This option allows the setting of
3151 * a default context on an association basis that will be received on
3152 * reading messages from the peer. This is especially helpful in the
3153 * one-2-many model for an application to keep some reference to an
3154 * internal state machine that is processing messages on the
3155 * association. Note that the setting of this value only effects
3156 * received messages from the peer and does not effect the value that is
3157 * saved with outbound messages.
3158 */
3159static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
b7058842 3160 unsigned int optlen)
6ab792f5
ISJ
3161{
3162 struct sctp_assoc_value params;
3163 struct sctp_sock *sp;
3164 struct sctp_association *asoc;
3165
3166 if (optlen != sizeof(struct sctp_assoc_value))
3167 return -EINVAL;
3168 if (copy_from_user(&params, optval, optlen))
3169 return -EFAULT;
3170
3171 sp = sctp_sk(sk);
3172
3173 if (params.assoc_id != 0) {
3174 asoc = sctp_id2assoc(sk, params.assoc_id);
3175 if (!asoc)
3176 return -EINVAL;
3177 asoc->default_rcv_context = params.assoc_value;
3178 } else {
3179 sp->default_rcv_context = params.assoc_value;
3180 }
3181
3182 return 0;
3183}
3184
b6e1331f
VY
3185/*
3186 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3187 *
3188 * This options will at a minimum specify if the implementation is doing
3189 * fragmented interleave. Fragmented interleave, for a one to many
3190 * socket, is when subsequent calls to receive a message may return
3191 * parts of messages from different associations. Some implementations
3192 * may allow you to turn this value on or off. If so, when turned off,
3193 * no fragment interleave will occur (which will cause a head of line
3194 * blocking amongst multiple associations sharing the same one to many
3195 * socket). When this option is turned on, then each receive call may
3196 * come from a different association (thus the user must receive data
3197 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3198 * association each receive belongs to.
3199 *
3200 * This option takes a boolean value. A non-zero value indicates that
3201 * fragmented interleave is on. A value of zero indicates that
3202 * fragmented interleave is off.
3203 *
3204 * Note that it is important that an implementation that allows this
3205 * option to be turned on, have it off by default. Otherwise an unaware
3206 * application using the one to many model may become confused and act
3207 * incorrectly.
3208 */
3209static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3210 char __user *optval,
b7058842 3211 unsigned int optlen)
b6e1331f
VY
3212{
3213 int val;
3214
3215 if (optlen != sizeof(int))
3216 return -EINVAL;
3217 if (get_user(val, (int __user *)optval))
3218 return -EFAULT;
3219
3220 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3221
3222 return 0;
3223}
3224
d49d91d7 3225/*
8510b937 3226 * 8.1.21. Set or Get the SCTP Partial Delivery Point
d49d91d7 3227 * (SCTP_PARTIAL_DELIVERY_POINT)
8510b937 3228 *
d49d91d7
VY
3229 * This option will set or get the SCTP partial delivery point. This
3230 * point is the size of a message where the partial delivery API will be
3231 * invoked to help free up rwnd space for the peer. Setting this to a
8510b937 3232 * lower value will cause partial deliveries to happen more often. The
d49d91d7 3233 * calls argument is an integer that sets or gets the partial delivery
8510b937
WY
3234 * point. Note also that the call will fail if the user attempts to set
3235 * this value larger than the socket receive buffer size.
3236 *
3237 * Note that any single message having a length smaller than or equal to
3238 * the SCTP partial delivery point will be delivered in one single read
3239 * call as long as the user provided buffer is large enough to hold the
3240 * message.
d49d91d7
VY
3241 */
3242static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3243 char __user *optval,
b7058842 3244 unsigned int optlen)
d49d91d7
VY
3245{
3246 u32 val;
3247
3248 if (optlen != sizeof(u32))
3249 return -EINVAL;
3250 if (get_user(val, (int __user *)optval))
3251 return -EFAULT;
3252
8510b937
WY
3253 /* Note: We double the receive buffer from what the user sets
3254 * it to be, also initial rwnd is based on rcvbuf/2.
3255 */
3256 if (val > (sk->sk_rcvbuf >> 1))
3257 return -EINVAL;
3258
d49d91d7
VY
3259 sctp_sk(sk)->pd_point = val;
3260
3261 return 0; /* is this the right error code? */
3262}
3263
70331571
VY
3264/*
3265 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3266 *
3267 * This option will allow a user to change the maximum burst of packets
3268 * that can be emitted by this association. Note that the default value
3269 * is 4, and some implementations may restrict this setting so that it
3270 * can only be lowered.
3271 *
3272 * NOTE: This text doesn't seem right. Do this on a socket basis with
3273 * future associations inheriting the socket value.
3274 */
3275static int sctp_setsockopt_maxburst(struct sock *sk,
3276 char __user *optval,
b7058842 3277 unsigned int optlen)
70331571 3278{
219b99a9
NH
3279 struct sctp_assoc_value params;
3280 struct sctp_sock *sp;
3281 struct sctp_association *asoc;
70331571 3282 int val;
219b99a9 3283 int assoc_id = 0;
70331571 3284
219b99a9 3285 if (optlen == sizeof(int)) {
145ce502
JP
3286 pr_warn("Use of int in max_burst socket option deprecated\n");
3287 pr_warn("Use struct sctp_assoc_value instead\n");
219b99a9
NH
3288 if (copy_from_user(&val, optval, optlen))
3289 return -EFAULT;
3290 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3291 if (copy_from_user(&params, optval, optlen))
3292 return -EFAULT;
3293 val = params.assoc_value;
3294 assoc_id = params.assoc_id;
3295 } else
70331571
VY
3296 return -EINVAL;
3297
219b99a9
NH
3298 sp = sctp_sk(sk);
3299
3300 if (assoc_id != 0) {
3301 asoc = sctp_id2assoc(sk, assoc_id);
3302 if (!asoc)
3303 return -EINVAL;
3304 asoc->max_burst = val;
3305 } else
3306 sp->max_burst = val;
70331571
VY
3307
3308 return 0;
3309}
3310
65b07e5d
VY
3311/*
3312 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3313 *
3314 * This set option adds a chunk type that the user is requesting to be
3315 * received only in an authenticated way. Changes to the list of chunks
3316 * will only effect future associations on the socket.
3317 */
3318static int sctp_setsockopt_auth_chunk(struct sock *sk,
b7058842
DM
3319 char __user *optval,
3320 unsigned int optlen)
65b07e5d 3321{
e5eae4a0 3322 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3323 struct sctp_authchunk val;
3324
e5eae4a0 3325 if (!ep->auth_enable)
5e739d17
VY
3326 return -EACCES;
3327
65b07e5d
VY
3328 if (optlen != sizeof(struct sctp_authchunk))
3329 return -EINVAL;
3330 if (copy_from_user(&val, optval, optlen))
3331 return -EFAULT;
3332
3333 switch (val.sauth_chunk) {
7fd71b1e
JP
3334 case SCTP_CID_INIT:
3335 case SCTP_CID_INIT_ACK:
3336 case SCTP_CID_SHUTDOWN_COMPLETE:
3337 case SCTP_CID_AUTH:
3338 return -EINVAL;
65b07e5d
VY
3339 }
3340
3341 /* add this chunk id to the endpoint */
e5eae4a0 3342 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
65b07e5d
VY
3343}
3344
3345/*
3346 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3347 *
3348 * This option gets or sets the list of HMAC algorithms that the local
3349 * endpoint requires the peer to use.
3350 */
3351static int sctp_setsockopt_hmac_ident(struct sock *sk,
b7058842
DM
3352 char __user *optval,
3353 unsigned int optlen)
65b07e5d 3354{
e5eae4a0 3355 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d 3356 struct sctp_hmacalgo *hmacs;
d9724055 3357 u32 idents;
65b07e5d
VY
3358 int err;
3359
e5eae4a0 3360 if (!ep->auth_enable)
5e739d17
VY
3361 return -EACCES;
3362
65b07e5d
VY
3363 if (optlen < sizeof(struct sctp_hmacalgo))
3364 return -EINVAL;
3365
934253a7
SW
3366 hmacs= memdup_user(optval, optlen);
3367 if (IS_ERR(hmacs))
3368 return PTR_ERR(hmacs);
65b07e5d 3369
d9724055
VY
3370 idents = hmacs->shmac_num_idents;
3371 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3372 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
65b07e5d
VY
3373 err = -EINVAL;
3374 goto out;
3375 }
3376
e5eae4a0 3377 err = sctp_auth_ep_set_hmacs(ep, hmacs);
65b07e5d
VY
3378out:
3379 kfree(hmacs);
3380 return err;
3381}
3382
3383/*
3384 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3385 *
3386 * This option will set a shared secret key which is used to build an
3387 * association shared key.
3388 */
3389static int sctp_setsockopt_auth_key(struct sock *sk,
3390 char __user *optval,
b7058842 3391 unsigned int optlen)
65b07e5d 3392{
e5eae4a0 3393 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3394 struct sctp_authkey *authkey;
3395 struct sctp_association *asoc;
3396 int ret;
3397
e5eae4a0 3398 if (!ep->auth_enable)
5e739d17
VY
3399 return -EACCES;
3400
65b07e5d
VY
3401 if (optlen <= sizeof(struct sctp_authkey))
3402 return -EINVAL;
3403
934253a7
SW
3404 authkey= memdup_user(optval, optlen);
3405 if (IS_ERR(authkey))
3406 return PTR_ERR(authkey);
65b07e5d 3407
328fc47e 3408 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
30c2235c
VY
3409 ret = -EINVAL;
3410 goto out;
3411 }
3412
65b07e5d
VY
3413 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3414 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3415 ret = -EINVAL;
3416 goto out;
3417 }
3418
e5eae4a0 3419 ret = sctp_auth_set_key(ep, asoc, authkey);
65b07e5d 3420out:
6ba542a2 3421 kzfree(authkey);
65b07e5d
VY
3422 return ret;
3423}
3424
3425/*
3426 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3427 *
3428 * This option will get or set the active shared key to be used to build
3429 * the association shared key.
3430 */
3431static int sctp_setsockopt_active_key(struct sock *sk,
b7058842
DM
3432 char __user *optval,
3433 unsigned int optlen)
65b07e5d 3434{
e5eae4a0 3435 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3436 struct sctp_authkeyid val;
3437 struct sctp_association *asoc;
3438
e5eae4a0 3439 if (!ep->auth_enable)
5e739d17
VY
3440 return -EACCES;
3441
65b07e5d
VY
3442 if (optlen != sizeof(struct sctp_authkeyid))
3443 return -EINVAL;
3444 if (copy_from_user(&val, optval, optlen))
3445 return -EFAULT;
3446
3447 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3448 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3449 return -EINVAL;
3450
e5eae4a0 3451 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
65b07e5d
VY
3452}
3453
3454/*
3455 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3456 *
3457 * This set option will delete a shared secret key from use.
3458 */
3459static int sctp_setsockopt_del_key(struct sock *sk,
b7058842
DM
3460 char __user *optval,
3461 unsigned int optlen)
65b07e5d 3462{
e5eae4a0 3463 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
65b07e5d
VY
3464 struct sctp_authkeyid val;
3465 struct sctp_association *asoc;
3466
e5eae4a0 3467 if (!ep->auth_enable)
5e739d17
VY
3468 return -EACCES;
3469
65b07e5d
VY
3470 if (optlen != sizeof(struct sctp_authkeyid))
3471 return -EINVAL;
3472 if (copy_from_user(&val, optval, optlen))
3473 return -EFAULT;
3474
3475 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3476 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3477 return -EINVAL;
3478
e5eae4a0 3479 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
65b07e5d
VY
3480
3481}
3482
7dc04d71
MH
3483/*
3484 * 8.1.23 SCTP_AUTO_ASCONF
3485 *
3486 * This option will enable or disable the use of the automatic generation of
3487 * ASCONF chunks to add and delete addresses to an existing association. Note
3488 * that this option has two caveats namely: a) it only affects sockets that
3489 * are bound to all addresses available to the SCTP stack, and b) the system
3490 * administrator may have an overriding control that turns the ASCONF feature
3491 * off no matter what setting the socket option may have.
3492 * This option expects an integer boolean flag, where a non-zero value turns on
3493 * the option, and a zero value turns off the option.
3494 * Note. In this implementation, socket operation overrides default parameter
3495 * being set by sysctl as well as FreeBSD implementation
3496 */
3497static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3498 unsigned int optlen)
3499{
3500 int val;
3501 struct sctp_sock *sp = sctp_sk(sk);
3502
3503 if (optlen < sizeof(int))
3504 return -EINVAL;
3505 if (get_user(val, (int __user *)optval))
3506 return -EFAULT;
3507 if (!sctp_is_ep_boundall(sk) && val)
3508 return -EINVAL;
3509 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3510 return 0;
3511
7bf24986 3512 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
7dc04d71
MH
3513 if (val == 0 && sp->do_auto_asconf) {
3514 list_del(&sp->auto_asconf_list);
3515 sp->do_auto_asconf = 0;
3516 } else if (val && !sp->do_auto_asconf) {
3517 list_add_tail(&sp->auto_asconf_list,
4db67e80 3518 &sock_net(sk)->sctp.auto_asconf_splist);
7dc04d71
MH
3519 sp->do_auto_asconf = 1;
3520 }
7bf24986 3521 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
7dc04d71
MH
3522 return 0;
3523}
3524
65b07e5d 3525
5aa93bcf
NH
3526/*
3527 * SCTP_PEER_ADDR_THLDS
3528 *
3529 * This option allows us to alter the partially failed threshold for one or all
3530 * transports in an association. See Section 6.1 of:
3531 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3532 */
3533static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3534 char __user *optval,
3535 unsigned int optlen)
3536{
3537 struct sctp_paddrthlds val;
3538 struct sctp_transport *trans;
3539 struct sctp_association *asoc;
3540
3541 if (optlen < sizeof(struct sctp_paddrthlds))
3542 return -EINVAL;
3543 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3544 sizeof(struct sctp_paddrthlds)))
3545 return -EFAULT;
3546
3547
3548 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3549 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3550 if (!asoc)
3551 return -ENOENT;
3552 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3553 transports) {
3554 if (val.spt_pathmaxrxt)
3555 trans->pathmaxrxt = val.spt_pathmaxrxt;
3556 trans->pf_retrans = val.spt_pathpfthld;
3557 }
3558
3559 if (val.spt_pathmaxrxt)
3560 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3561 asoc->pf_retrans = val.spt_pathpfthld;
3562 } else {
3563 trans = sctp_addr_id2transport(sk, &val.spt_address,
3564 val.spt_assoc_id);
3565 if (!trans)
3566 return -ENOENT;
3567
3568 if (val.spt_pathmaxrxt)
3569 trans->pathmaxrxt = val.spt_pathmaxrxt;
3570 trans->pf_retrans = val.spt_pathpfthld;
3571 }
3572
3573 return 0;
3574}
3575
1da177e4
LT
3576/* API 6.2 setsockopt(), getsockopt()
3577 *
3578 * Applications use setsockopt() and getsockopt() to set or retrieve
3579 * socket options. Socket options are used to change the default
3580 * behavior of sockets calls. They are described in Section 7.
3581 *
3582 * The syntax is:
3583 *
3584 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3585 * int __user *optlen);
3586 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3587 * int optlen);
3588 *
3589 * sd - the socket descript.
3590 * level - set to IPPROTO_SCTP for all SCTP options.
3591 * optname - the option name.
3592 * optval - the buffer to store the value of the option.
3593 * optlen - the size of the buffer.
3594 */
3595SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
b7058842 3596 char __user *optval, unsigned int optlen)
1da177e4
LT
3597{
3598 int retval = 0;
3599
3600 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3601 sk, optname);
3602
3603 /* I can hardly begin to describe how wrong this is. This is
3604 * so broken as to be worse than useless. The API draft
3605 * REALLY is NOT helpful here... I am not convinced that the
3606 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3607 * are at all well-founded.
3608 */
3609 if (level != SOL_SCTP) {
3610 struct sctp_af *af = sctp_sk(sk)->pf->af;
3611 retval = af->setsockopt(sk, level, optname, optval, optlen);
3612 goto out_nounlock;
3613 }
3614
3615 sctp_lock_sock(sk);
3616
3617 switch (optname) {
3618 case SCTP_SOCKOPT_BINDX_ADD:
3619 /* 'optlen' is the size of the addresses buffer. */
3620 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3621 optlen, SCTP_BINDX_ADD_ADDR);
3622 break;
3623
3624 case SCTP_SOCKOPT_BINDX_REM:
3625 /* 'optlen' is the size of the addresses buffer. */
3626 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3627 optlen, SCTP_BINDX_REM_ADDR);
3628 break;
3629
88a0a948
VY
3630 case SCTP_SOCKOPT_CONNECTX_OLD:
3631 /* 'optlen' is the size of the addresses buffer. */
3632 retval = sctp_setsockopt_connectx_old(sk,
3633 (struct sockaddr __user *)optval,
3634 optlen);
3635 break;
3636
3f7a87d2
FF
3637 case SCTP_SOCKOPT_CONNECTX:
3638 /* 'optlen' is the size of the addresses buffer. */
88a0a948
VY
3639 retval = sctp_setsockopt_connectx(sk,
3640 (struct sockaddr __user *)optval,
3641 optlen);
3f7a87d2
FF
3642 break;
3643
1da177e4
LT
3644 case SCTP_DISABLE_FRAGMENTS:
3645 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3646 break;
3647
3648 case SCTP_EVENTS:
3649 retval = sctp_setsockopt_events(sk, optval, optlen);
3650 break;
3651
3652 case SCTP_AUTOCLOSE:
3653 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3654 break;
3655
3656 case SCTP_PEER_ADDR_PARAMS:
3657 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3658 break;
3659
4580ccc0 3660 case SCTP_DELAYED_SACK:
d364d927 3661 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
7708610b 3662 break;
d49d91d7
VY
3663 case SCTP_PARTIAL_DELIVERY_POINT:
3664 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3665 break;
7708610b 3666
1da177e4
LT
3667 case SCTP_INITMSG:
3668 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3669 break;
3670 case SCTP_DEFAULT_SEND_PARAM:
3671 retval = sctp_setsockopt_default_send_param(sk, optval,
3672 optlen);
3673 break;
3674 case SCTP_PRIMARY_ADDR:
3675 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3676 break;
3677 case SCTP_SET_PEER_PRIMARY_ADDR:
3678 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3679 break;
3680 case SCTP_NODELAY:
3681 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3682 break;
3683 case SCTP_RTOINFO:
3684 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3685 break;
3686 case SCTP_ASSOCINFO:
3687 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3688 break;
3689 case SCTP_I_WANT_MAPPED_V4_ADDR:
3690 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3691 break;
3692 case SCTP_MAXSEG:
3693 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3694 break;
0f3fffd8
ISJ
3695 case SCTP_ADAPTATION_LAYER:
3696 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
1da177e4 3697 break;
6ab792f5
ISJ
3698 case SCTP_CONTEXT:
3699 retval = sctp_setsockopt_context(sk, optval, optlen);
3700 break;
b6e1331f
VY
3701 case SCTP_FRAGMENT_INTERLEAVE:
3702 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3703 break;
70331571
VY
3704 case SCTP_MAX_BURST:
3705 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3706 break;
65b07e5d
VY
3707 case SCTP_AUTH_CHUNK:
3708 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3709 break;
3710 case SCTP_HMAC_IDENT:
3711 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3712 break;
3713 case SCTP_AUTH_KEY:
3714 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3715 break;
3716 case SCTP_AUTH_ACTIVE_KEY:
3717 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3718 break;
3719 case SCTP_AUTH_DELETE_KEY:
3720 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3721 break;
7dc04d71
MH
3722 case SCTP_AUTO_ASCONF:
3723 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3724 break;
5aa93bcf
NH
3725 case SCTP_PEER_ADDR_THLDS:
3726 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3727 break;
1da177e4
LT
3728 default:
3729 retval = -ENOPROTOOPT;
3730 break;
3ff50b79 3731 }
1da177e4
LT
3732
3733 sctp_release_sock(sk);
3734
3735out_nounlock:
3736 return retval;
3737}
3738
3739/* API 3.1.6 connect() - UDP Style Syntax
3740 *
3741 * An application may use the connect() call in the UDP model to initiate an
3742 * association without sending data.
3743 *
3744 * The syntax is:
3745 *
3746 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3747 *
3748 * sd: the socket descriptor to have a new association added to.
3749 *
3750 * nam: the address structure (either struct sockaddr_in or struct
3751 * sockaddr_in6 defined in RFC2553 [7]).
3752 *
3753 * len: the size of the address.
3754 */
3f7a87d2 3755SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
1da177e4
LT
3756 int addr_len)
3757{
1da177e4 3758 int err = 0;
3f7a87d2 3759 struct sctp_af *af;
1da177e4
LT
3760
3761 sctp_lock_sock(sk);
3762
3f7a87d2 3763 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
0dc47877 3764 __func__, sk, addr, addr_len);
1da177e4 3765
3f7a87d2
FF
3766 /* Validate addr_len before calling common connect/connectx routine. */
3767 af = sctp_get_af_specific(addr->sa_family);
3768 if (!af || addr_len < af->sockaddr_len) {
3769 err = -EINVAL;
3770 } else {
3771 /* Pass correct addr len to common routine (so it knows there
3772 * is only one address being passed.
3773 */
88a0a948 3774 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
1da177e4
LT
3775 }
3776
1da177e4 3777 sctp_release_sock(sk);
1da177e4
LT
3778 return err;
3779}
3780
3781/* FIXME: Write comments. */
3782SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3783{
3784 return -EOPNOTSUPP; /* STUB */
3785}
3786
3787/* 4.1.4 accept() - TCP Style Syntax
3788 *
3789 * Applications use accept() call to remove an established SCTP
3790 * association from the accept queue of the endpoint. A new socket
3791 * descriptor will be returned from accept() to represent the newly
3792 * formed association.
3793 */
3794SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3795{
3796 struct sctp_sock *sp;
3797 struct sctp_endpoint *ep;
3798 struct sock *newsk = NULL;
3799 struct sctp_association *asoc;
3800 long timeo;
3801 int error = 0;
3802
3803 sctp_lock_sock(sk);
3804
3805 sp = sctp_sk(sk);
3806 ep = sp->ep;
3807
3808 if (!sctp_style(sk, TCP)) {
3809 error = -EOPNOTSUPP;
3810 goto out;
3811 }
3812
3813 if (!sctp_sstate(sk, LISTENING)) {
3814 error = -EINVAL;
3815 goto out;
3816 }
3817
8abfedd8 3818 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1da177e4
LT
3819
3820 error = sctp_wait_for_accept(sk, timeo);
3821 if (error)
3822 goto out;
3823
3824 /* We treat the list of associations on the endpoint as the accept
3825 * queue and pick the first association on the list.
3826 */
3827 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3828
3829 newsk = sp->pf->create_accept_sk(sk, asoc);
3830 if (!newsk) {
3831 error = -ENOMEM;
3832 goto out;
3833 }
3834
3835 /* Populate the fields of the newsk from the oldsk and migrate the
3836 * asoc to the newsk.
3837 */
3838 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3839
3840out:
3841 sctp_release_sock(sk);
d808ad9a 3842 *err = error;
1da177e4
LT
3843 return newsk;
3844}
3845
3846/* The SCTP ioctl handler. */
3847SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3848{
65040c33
DEFP
3849 int rc = -ENOTCONN;
3850
3851 sctp_lock_sock(sk);
3852
3853 /*
3854 * SEQPACKET-style sockets in LISTENING state are valid, for
3855 * SCTP, so only discard TCP-style sockets in LISTENING state.
3856 */
3857 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3858 goto out;
3859
3860 switch (cmd) {
3861 case SIOCINQ: {
3862 struct sk_buff *skb;
3863 unsigned int amount = 0;
3864
3865 skb = skb_peek(&sk->sk_receive_queue);
3866 if (skb != NULL) {
3867 /*
3868 * We will only return the amount of this packet since
3869 * that is all that will be read.
3870 */
3871 amount = skb->len;
3872 }
3873 rc = put_user(amount, (int __user *)arg);
65040c33 3874 break;
9a7241c2 3875 }
65040c33
DEFP
3876 default:
3877 rc = -ENOIOCTLCMD;
3878 break;
3879 }
3880out:
3881 sctp_release_sock(sk);
3882 return rc;
1da177e4
LT
3883}
3884
3885/* This is the function which gets called during socket creation to
3886 * initialized the SCTP-specific portion of the sock.
3887 * The sock structure should already be zero-filled memory.
3888 */
3889SCTP_STATIC int sctp_init_sock(struct sock *sk)
3890{
e1fc3b14 3891 struct net *net = sock_net(sk);
1da177e4
LT
3892 struct sctp_endpoint *ep;
3893 struct sctp_sock *sp;
3894
3895 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3896
3897 sp = sctp_sk(sk);
3898
3899 /* Initialize the SCTP per socket area. */
3900 switch (sk->sk_type) {
3901 case SOCK_SEQPACKET:
3902 sp->type = SCTP_SOCKET_UDP;
3903 break;
3904 case SOCK_STREAM:
3905 sp->type = SCTP_SOCKET_TCP;
3906 break;
3907 default:
3908 return -ESOCKTNOSUPPORT;
3909 }
3910
3911 /* Initialize default send parameters. These parameters can be
3912 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3913 */
3914 sp->default_stream = 0;
3915 sp->default_ppid = 0;
3916 sp->default_flags = 0;
3917 sp->default_context = 0;
3918 sp->default_timetolive = 0;
3919
6ab792f5 3920 sp->default_rcv_context = 0;
e1fc3b14 3921 sp->max_burst = net->sctp.max_burst;
6ab792f5 3922
3c68198e
NH
3923 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
3924
1da177e4
LT
3925 /* Initialize default setup parameters. These parameters
3926 * can be modified with the SCTP_INITMSG socket option or
3927 * overridden by the SCTP_INIT CMSG.
3928 */
3929 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3930 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
e1fc3b14
EB
3931 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
3932 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
1da177e4
LT
3933
3934 /* Initialize default RTO related parameters. These parameters can
3935 * be modified for with the SCTP_RTOINFO socket option.
3936 */
e1fc3b14
EB
3937 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
3938 sp->rtoinfo.srto_max = net->sctp.rto_max;
3939 sp->rtoinfo.srto_min = net->sctp.rto_min;
1da177e4
LT
3940
3941 /* Initialize default association related parameters. These parameters
3942 * can be modified with the SCTP_ASSOCINFO socket option.
3943 */
e1fc3b14 3944 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
1da177e4
LT
3945 sp->assocparams.sasoc_number_peer_destinations = 0;
3946 sp->assocparams.sasoc_peer_rwnd = 0;
3947 sp->assocparams.sasoc_local_rwnd = 0;
e1fc3b14 3948 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
1da177e4
LT
3949
3950 /* Initialize default event subscriptions. By default, all the
d808ad9a 3951 * options are off.
1da177e4
LT
3952 */
3953 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3954
3955 /* Default Peer Address Parameters. These defaults can
3956 * be modified via SCTP_PEER_ADDR_PARAMS
3957 */
e1fc3b14
EB
3958 sp->hbinterval = net->sctp.hb_interval;
3959 sp->pathmaxrxt = net->sctp.max_retrans_path;
52ccb8e9 3960 sp->pathmtu = 0; // allow default discovery
e1fc3b14 3961 sp->sackdelay = net->sctp.sack_timeout;
7bfe8bdb 3962 sp->sackfreq = 2;
52ccb8e9 3963 sp->param_flags = SPP_HB_ENABLE |
d808ad9a
YH
3964 SPP_PMTUD_ENABLE |
3965 SPP_SACKDELAY_ENABLE;
1da177e4
LT
3966
3967 /* If enabled no SCTP message fragmentation will be performed.
3968 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3969 */
3970 sp->disable_fragments = 0;
3971
208edef6
SS
3972 /* Enable Nagle algorithm by default. */
3973 sp->nodelay = 0;
1da177e4
LT
3974
3975 /* Enable by default. */
3976 sp->v4mapped = 1;
3977
3978 /* Auto-close idle associations after the configured
3979 * number of seconds. A value of 0 disables this
3980 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3981 * for UDP-style sockets only.
3982 */
3983 sp->autoclose = 0;
3984
3985 /* User specified fragmentation limit. */
3986 sp->user_frag = 0;
3987
0f3fffd8 3988 sp->adaptation_ind = 0;
1da177e4
LT
3989
3990 sp->pf = sctp_get_pf_specific(sk->sk_family);
3991
3992 /* Control variables for partial data delivery. */
b6e1331f 3993 atomic_set(&sp->pd_mode, 0);
1da177e4 3994 skb_queue_head_init(&sp->pd_lobby);
b6e1331f 3995 sp->frag_interleave = 0;
1da177e4
LT
3996
3997 /* Create a per socket endpoint structure. Even if we
3998 * change the data structure relationships, this may still
3999 * be useful for storing pre-connect address information.
4000 */
4001 ep = sctp_endpoint_new(sk, GFP_KERNEL);
4002 if (!ep)
4003 return -ENOMEM;
4004
4005 sp->ep = ep;
4006 sp->hmac = NULL;
4007
4008 SCTP_DBG_OBJCNT_INC(sock);
6f756a8c
DM
4009
4010 local_bh_disable();
81419d86 4011 percpu_counter_inc(&sctp_sockets_allocated);
e1fc3b14 4012 sock_prot_inuse_add(net, sk->sk_prot, 1);
7bf24986
MRL
4013
4014 /* Nothing can fail after this block, otherwise
4015 * sctp_destroy_sock() will be called without addr_wq_lock held
4016 */
e1fc3b14 4017 if (net->sctp.default_auto_asconf) {
7bf24986 4018 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
9f7d653b 4019 list_add_tail(&sp->auto_asconf_list,
e1fc3b14 4020 &net->sctp.auto_asconf_splist);
9f7d653b 4021 sp->do_auto_asconf = 1;
7bf24986
MRL
4022 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4023 } else {
9f7d653b 4024 sp->do_auto_asconf = 0;
7bf24986
MRL
4025 }
4026
6f756a8c
DM
4027 local_bh_enable();
4028
1da177e4
LT
4029 return 0;
4030}
4031
7bf24986
MRL
4032/* Cleanup any SCTP per socket resources. Must be called with
4033 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4034 */
7d06b2e0 4035SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
1da177e4 4036{
9f7d653b 4037 struct sctp_sock *sp;
1da177e4
LT
4038
4039 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
4040
4041 /* Release our hold on the endpoint. */
9f7d653b 4042 sp = sctp_sk(sk);
1abd165e
DB
4043 /* This could happen during socket init, thus we bail out
4044 * early, since the rest of the below is not setup either.
4045 */
4046 if (sp->ep == NULL)
4047 return;
4048
9f7d653b
MH
4049 if (sp->do_auto_asconf) {
4050 sp->do_auto_asconf = 0;
4051 list_del(&sp->auto_asconf_list);
4052 }
4053 sctp_endpoint_free(sp->ep);
5bc0b3bf 4054 local_bh_disable();
81419d86 4055 percpu_counter_dec(&sctp_sockets_allocated);
9a57f7fa 4056 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5bc0b3bf 4057 local_bh_enable();
1da177e4
LT
4058}
4059
4060/* API 4.1.7 shutdown() - TCP Style Syntax
4061 * int shutdown(int socket, int how);
4062 *
4063 * sd - the socket descriptor of the association to be closed.
4064 * how - Specifies the type of shutdown. The values are
4065 * as follows:
4066 * SHUT_RD
4067 * Disables further receive operations. No SCTP
4068 * protocol action is taken.
4069 * SHUT_WR
4070 * Disables further send operations, and initiates
4071 * the SCTP shutdown sequence.
4072 * SHUT_RDWR
4073 * Disables further send and receive operations
4074 * and initiates the SCTP shutdown sequence.
4075 */
4076SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4077{
55e26eb9 4078 struct net *net = sock_net(sk);
1da177e4
LT
4079 struct sctp_endpoint *ep;
4080 struct sctp_association *asoc;
4081
4082 if (!sctp_style(sk, TCP))
4083 return;
4084
4085 if (how & SEND_SHUTDOWN) {
4086 ep = sctp_sk(sk)->ep;
4087 if (!list_empty(&ep->asocs)) {
4088 asoc = list_entry(ep->asocs.next,
4089 struct sctp_association, asocs);
55e26eb9 4090 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1da177e4
LT
4091 }
4092 }
4093}
4094
4095/* 7.2.1 Association Status (SCTP_STATUS)
4096
4097 * Applications can retrieve current status information about an
4098 * association, including association state, peer receiver window size,
4099 * number of unacked data chunks, and number of data chunks pending
4100 * receipt. This information is read-only.
4101 */
4102static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4103 char __user *optval,
4104 int __user *optlen)
4105{
4106 struct sctp_status status;
4107 struct sctp_association *asoc = NULL;
4108 struct sctp_transport *transport;
4109 sctp_assoc_t associd;
4110 int retval = 0;
4111
408f22e8 4112 if (len < sizeof(status)) {
1da177e4
LT
4113 retval = -EINVAL;
4114 goto out;
4115 }
4116
408f22e8
NH
4117 len = sizeof(status);
4118 if (copy_from_user(&status, optval, len)) {
1da177e4
LT
4119 retval = -EFAULT;
4120 goto out;
4121 }
4122
4123 associd = status.sstat_assoc_id;
4124 asoc = sctp_id2assoc(sk, associd);
4125 if (!asoc) {
4126 retval = -EINVAL;
4127 goto out;
4128 }
4129
4130 transport = asoc->peer.primary_path;
4131
4132 status.sstat_assoc_id = sctp_assoc2id(asoc);
4133 status.sstat_state = asoc->state;
4134 status.sstat_rwnd = asoc->peer.rwnd;
4135 status.sstat_unackdata = asoc->unack_data;
4136
4137 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4138 status.sstat_instrms = asoc->c.sinit_max_instreams;
4139 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4140 status.sstat_fragmentation_point = asoc->frag_point;
4141 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
8cec6b80
AV
4142 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4143 transport->af_specific->sockaddr_len);
1da177e4
LT
4144 /* Map ipv4 address into v4-mapped-on-v6 address. */
4145 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4146 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3f7a87d2 4147 status.sstat_primary.spinfo_state = transport->state;
1da177e4
LT
4148 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4149 status.sstat_primary.spinfo_srtt = transport->srtt;
4150 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 4151 status.sstat_primary.spinfo_mtu = transport->pathmtu;
1da177e4 4152
3f7a87d2
FF
4153 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4154 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4155
1da177e4
LT
4156 if (put_user(len, optlen)) {
4157 retval = -EFAULT;
4158 goto out;
4159 }
4160
4161 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4162 len, status.sstat_state, status.sstat_rwnd,
4163 status.sstat_assoc_id);
4164
4165 if (copy_to_user(optval, &status, len)) {
4166 retval = -EFAULT;
4167 goto out;
4168 }
4169
4170out:
a02cec21 4171 return retval;
1da177e4
LT
4172}
4173
4174
4175/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4176 *
4177 * Applications can retrieve information about a specific peer address
4178 * of an association, including its reachability state, congestion
4179 * window, and retransmission timer values. This information is
4180 * read-only.
4181 */
4182static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4183 char __user *optval,
4184 int __user *optlen)
4185{
4186 struct sctp_paddrinfo pinfo;
4187 struct sctp_transport *transport;
4188 int retval = 0;
4189
408f22e8 4190 if (len < sizeof(pinfo)) {
1da177e4
LT
4191 retval = -EINVAL;
4192 goto out;
4193 }
4194
408f22e8
NH
4195 len = sizeof(pinfo);
4196 if (copy_from_user(&pinfo, optval, len)) {
1da177e4
LT
4197 retval = -EFAULT;
4198 goto out;
4199 }
4200
4201 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4202 pinfo.spinfo_assoc_id);
4203 if (!transport)
4204 return -EINVAL;
4205
4206 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3f7a87d2 4207 pinfo.spinfo_state = transport->state;
1da177e4
LT
4208 pinfo.spinfo_cwnd = transport->cwnd;
4209 pinfo.spinfo_srtt = transport->srtt;
4210 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
52ccb8e9 4211 pinfo.spinfo_mtu = transport->pathmtu;
1da177e4 4212
3f7a87d2
FF
4213 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4214 pinfo.spinfo_state = SCTP_ACTIVE;
4215
1da177e4
LT
4216 if (put_user(len, optlen)) {
4217 retval = -EFAULT;
4218 goto out;
4219 }
4220
4221 if (copy_to_user(optval, &pinfo, len)) {
4222 retval = -EFAULT;
4223 goto out;
4224 }
4225
4226out:
a02cec21 4227 return retval;
1da177e4
LT
4228}
4229
4230/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4231 *
4232 * This option is a on/off flag. If enabled no SCTP message
4233 * fragmentation will be performed. Instead if a message being sent
4234 * exceeds the current PMTU size, the message will NOT be sent and
4235 * instead a error will be indicated to the user.
4236 */
4237static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4238 char __user *optval, int __user *optlen)
4239{
4240 int val;
4241
4242 if (len < sizeof(int))
4243 return -EINVAL;
4244
4245 len = sizeof(int);
4246 val = (sctp_sk(sk)->disable_fragments == 1);
4247 if (put_user(len, optlen))
4248 return -EFAULT;
4249 if (copy_to_user(optval, &val, len))
4250 return -EFAULT;
4251 return 0;
4252}
4253
4254/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4255 *
4256 * This socket option is used to specify various notifications and
4257 * ancillary data the user wishes to receive.
4258 */
4259static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4260 int __user *optlen)
4261{
acdd5985 4262 if (len <= 0)
1da177e4 4263 return -EINVAL;
acdd5985
TG
4264 if (len > sizeof(struct sctp_event_subscribe))
4265 len = sizeof(struct sctp_event_subscribe);
408f22e8
NH
4266 if (put_user(len, optlen))
4267 return -EFAULT;
1da177e4
LT
4268 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4269 return -EFAULT;
4270 return 0;
4271}
4272
4273/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4274 *
4275 * This socket option is applicable to the UDP-style socket only. When
4276 * set it will cause associations that are idle for more than the
4277 * specified number of seconds to automatically close. An association
4278 * being idle is defined an association that has NOT sent or received
4279 * user data. The special value of '0' indicates that no automatic
4280 * close of any associations should be performed. The option expects an
4281 * integer defining the number of seconds of idle time before an
4282 * association is closed.
4283 */
4284static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4285{
4286 /* Applicable to UDP-style socket only */
4287 if (sctp_style(sk, TCP))
4288 return -EOPNOTSUPP;
408f22e8 4289 if (len < sizeof(int))
1da177e4 4290 return -EINVAL;
408f22e8
NH
4291 len = sizeof(int);
4292 if (put_user(len, optlen))
4293 return -EFAULT;
4294 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
1da177e4
LT
4295 return -EFAULT;
4296 return 0;
4297}
4298
4299/* Helper routine to branch off an association to a new socket. */
0343c554 4300int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
1da177e4 4301{
0343c554 4302 struct sctp_association *asoc = sctp_id2assoc(sk, id);
1da177e4 4303 struct socket *sock;
d570ee49 4304 struct sctp_af *af;
1da177e4
LT
4305 int err = 0;
4306
0343c554
BP
4307 if (!asoc)
4308 return -EINVAL;
4309
1da177e4
LT
4310 /* An association cannot be branched off from an already peeled-off
4311 * socket, nor is this supported for tcp style sockets.
4312 */
4313 if (!sctp_style(sk, UDP))
4314 return -EINVAL;
4315
4316 /* Create a new socket. */
4317 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4318 if (err < 0)
4319 return err;
4320
914e1c8b 4321 sctp_copy_sock(sock->sk, sk, asoc);
4f444308
VY
4322
4323 /* Make peeled-off sockets more like 1-1 accepted sockets.
4324 * Set the daddr and initialize id to something more random
4325 */
d570ee49
VY
4326 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4327 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
914e1c8b
VY
4328
4329 /* Populate the fields of the newsk from the oldsk and migrate the
4330 * asoc to the newsk.
4331 */
4332 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
4f444308 4333
1da177e4
LT
4334 *sockp = sock;
4335
4336 return err;
4337}
0343c554 4338EXPORT_SYMBOL(sctp_do_peeloff);
1da177e4
LT
4339
4340static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4341{
4342 sctp_peeloff_arg_t peeloff;
4343 struct socket *newsock;
56b31d1c 4344 struct file *newfile;
1da177e4 4345 int retval = 0;
1da177e4 4346
408f22e8 4347 if (len < sizeof(sctp_peeloff_arg_t))
1da177e4 4348 return -EINVAL;
408f22e8 4349 len = sizeof(sctp_peeloff_arg_t);
1da177e4
LT
4350 if (copy_from_user(&peeloff, optval, len))
4351 return -EFAULT;
4352
0343c554 4353 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
1da177e4
LT
4354 if (retval < 0)
4355 goto out;
4356
4357 /* Map the socket to an unused fd that can be returned to the user. */
56b31d1c 4358 retval = get_unused_fd();
1da177e4
LT
4359 if (retval < 0) {
4360 sock_release(newsock);
4361 goto out;
4362 }
4363
aab174f0 4364 newfile = sock_alloc_file(newsock, 0, NULL);
56b31d1c
AV
4365 if (unlikely(IS_ERR(newfile))) {
4366 put_unused_fd(retval);
4367 sock_release(newsock);
4368 return PTR_ERR(newfile);
4369 }
4370
0343c554
BP
4371 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4372 __func__, sk, newsock->sk, retval);
1da177e4
LT
4373
4374 /* Return the fd mapped to the new socket. */
56b31d1c
AV
4375 if (put_user(len, optlen)) {
4376 fput(newfile);
4377 put_unused_fd(retval);
4378 return -EFAULT;
4379 }
1da177e4 4380 peeloff.sd = retval;
56b31d1c
AV
4381 if (copy_to_user(optval, &peeloff, len)) {
4382 fput(newfile);
4383 put_unused_fd(retval);
408f22e8 4384 return -EFAULT;
56b31d1c
AV
4385 }
4386 fd_install(retval, newfile);
1da177e4
LT
4387out:
4388 return retval;
4389}
4390
4391/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4392 *
4393 * Applications can enable or disable heartbeats for any peer address of
4394 * an association, modify an address's heartbeat interval, force a
4395 * heartbeat to be sent immediately, and adjust the address's maximum
4396 * number of retransmissions sent before an address is considered
4397 * unreachable. The following structure is used to access and modify an
4398 * address's parameters:
4399 *
4400 * struct sctp_paddrparams {
52ccb8e9
FF
4401 * sctp_assoc_t spp_assoc_id;
4402 * struct sockaddr_storage spp_address;
4403 * uint32_t spp_hbinterval;
4404 * uint16_t spp_pathmaxrxt;
4405 * uint32_t spp_pathmtu;
4406 * uint32_t spp_sackdelay;
4407 * uint32_t spp_flags;
4408 * };
4409 *
4410 * spp_assoc_id - (one-to-many style socket) This is filled in the
4411 * application, and identifies the association for
4412 * this query.
1da177e4
LT
4413 * spp_address - This specifies which address is of interest.
4414 * spp_hbinterval - This contains the value of the heartbeat interval,
52ccb8e9
FF
4415 * in milliseconds. If a value of zero
4416 * is present in this field then no changes are to
4417 * be made to this parameter.
1da177e4
LT
4418 * spp_pathmaxrxt - This contains the maximum number of
4419 * retransmissions before this address shall be
52ccb8e9
FF
4420 * considered unreachable. If a value of zero
4421 * is present in this field then no changes are to
4422 * be made to this parameter.
4423 * spp_pathmtu - When Path MTU discovery is disabled the value
4424 * specified here will be the "fixed" path mtu.
4425 * Note that if the spp_address field is empty
4426 * then all associations on this address will
4427 * have this fixed path mtu set upon them.
4428 *
4429 * spp_sackdelay - When delayed sack is enabled, this value specifies
4430 * the number of milliseconds that sacks will be delayed
4431 * for. This value will apply to all addresses of an
4432 * association if the spp_address field is empty. Note
4433 * also, that if delayed sack is enabled and this
4434 * value is set to 0, no change is made to the last
4435 * recorded delayed sack timer value.
4436 *
4437 * spp_flags - These flags are used to control various features
4438 * on an association. The flag field may contain
4439 * zero or more of the following options.
4440 *
4441 * SPP_HB_ENABLE - Enable heartbeats on the
4442 * specified address. Note that if the address
4443 * field is empty all addresses for the association
4444 * have heartbeats enabled upon them.
4445 *
4446 * SPP_HB_DISABLE - Disable heartbeats on the
4447 * speicifed address. Note that if the address
4448 * field is empty all addresses for the association
4449 * will have their heartbeats disabled. Note also
4450 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4451 * mutually exclusive, only one of these two should
4452 * be specified. Enabling both fields will have
4453 * undetermined results.
4454 *
4455 * SPP_HB_DEMAND - Request a user initiated heartbeat
4456 * to be made immediately.
4457 *
4458 * SPP_PMTUD_ENABLE - This field will enable PMTU
4459 * discovery upon the specified address. Note that
4460 * if the address feild is empty then all addresses
4461 * on the association are effected.
4462 *
4463 * SPP_PMTUD_DISABLE - This field will disable PMTU
4464 * discovery upon the specified address. Note that
4465 * if the address feild is empty then all addresses
4466 * on the association are effected. Not also that
4467 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4468 * exclusive. Enabling both will have undetermined
4469 * results.
4470 *
4471 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4472 * on delayed sack. The time specified in spp_sackdelay
4473 * is used to specify the sack delay for this address. Note
4474 * that if spp_address is empty then all addresses will
4475 * enable delayed sack and take on the sack delay
4476 * value specified in spp_sackdelay.
4477 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4478 * off delayed sack. If the spp_address field is blank then
4479 * delayed sack is disabled for the entire association. Note
4480 * also that this field is mutually exclusive to
4481 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4482 * results.
1da177e4
LT
4483 */
4484static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
52ccb8e9 4485 char __user *optval, int __user *optlen)
1da177e4 4486{
52ccb8e9
FF
4487 struct sctp_paddrparams params;
4488 struct sctp_transport *trans = NULL;
4489 struct sctp_association *asoc = NULL;
4490 struct sctp_sock *sp = sctp_sk(sk);
1da177e4 4491
408f22e8 4492 if (len < sizeof(struct sctp_paddrparams))
1da177e4 4493 return -EINVAL;
408f22e8 4494 len = sizeof(struct sctp_paddrparams);
1da177e4
LT
4495 if (copy_from_user(&params, optval, len))
4496 return -EFAULT;
4497
52ccb8e9
FF
4498 /* If an address other than INADDR_ANY is specified, and
4499 * no transport is found, then the request is invalid.
1da177e4 4500 */
52cae8f0 4501 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
52ccb8e9
FF
4502 trans = sctp_addr_id2transport(sk, &params.spp_address,
4503 params.spp_assoc_id);
4504 if (!trans) {
4505 SCTP_DEBUG_PRINTK("Failed no transport\n");
4506 return -EINVAL;
4507 }
1da177e4
LT
4508 }
4509
52ccb8e9
FF
4510 /* Get association, if assoc_id != 0 and the socket is a one
4511 * to many style socket, and an association was not found, then
4512 * the id was invalid.
4513 */
4514 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4515 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4516 SCTP_DEBUG_PRINTK("Failed no association\n");
1da177e4 4517 return -EINVAL;
52ccb8e9 4518 }
1da177e4 4519
52ccb8e9
FF
4520 if (trans) {
4521 /* Fetch transport values. */
4522 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4523 params.spp_pathmtu = trans->pathmtu;
4524 params.spp_pathmaxrxt = trans->pathmaxrxt;
4525 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
4526
4527 /*draft-11 doesn't say what to return in spp_flags*/
4528 params.spp_flags = trans->param_flags;
4529 } else if (asoc) {
4530 /* Fetch association values. */
4531 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4532 params.spp_pathmtu = asoc->pathmtu;
4533 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4534 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
4535
4536 /*draft-11 doesn't say what to return in spp_flags*/
4537 params.spp_flags = asoc->param_flags;
4538 } else {
4539 /* Fetch socket values. */
4540 params.spp_hbinterval = sp->hbinterval;
4541 params.spp_pathmtu = sp->pathmtu;
4542 params.spp_sackdelay = sp->sackdelay;
4543 params.spp_pathmaxrxt = sp->pathmaxrxt;
1da177e4 4544
52ccb8e9
FF
4545 /*draft-11 doesn't say what to return in spp_flags*/
4546 params.spp_flags = sp->param_flags;
4547 }
1da177e4 4548
1da177e4
LT
4549 if (copy_to_user(optval, &params, len))
4550 return -EFAULT;
4551
4552 if (put_user(len, optlen))
4553 return -EFAULT;
4554
4555 return 0;
4556}
4557
d364d927
WY
4558/*
4559 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
4560 *
4561 * This option will effect the way delayed acks are performed. This
4562 * option allows you to get or set the delayed ack time, in
4563 * milliseconds. It also allows changing the delayed ack frequency.
4564 * Changing the frequency to 1 disables the delayed sack algorithm. If
4565 * the assoc_id is 0, then this sets or gets the endpoints default
4566 * values. If the assoc_id field is non-zero, then the set or get
4567 * effects the specified association for the one to many model (the
4568 * assoc_id field is ignored by the one to one model). Note that if
4569 * sack_delay or sack_freq are 0 when setting this option, then the
4570 * current values will remain unchanged.
4571 *
4572 * struct sctp_sack_info {
4573 * sctp_assoc_t sack_assoc_id;
4574 * uint32_t sack_delay;
4575 * uint32_t sack_freq;
4576 * };
7708610b 4577 *
d364d927
WY
4578 * sack_assoc_id - This parameter, indicates which association the user
4579 * is performing an action upon. Note that if this field's value is
4580 * zero then the endpoints default value is changed (effecting future
4581 * associations only).
7708610b 4582 *
d364d927
WY
4583 * sack_delay - This parameter contains the number of milliseconds that
4584 * the user is requesting the delayed ACK timer be set to. Note that
4585 * this value is defined in the standard to be between 200 and 500
4586 * milliseconds.
7708610b 4587 *
d364d927
WY
4588 * sack_freq - This parameter contains the number of packets that must
4589 * be received before a sack is sent without waiting for the delay
4590 * timer to expire. The default value for this is 2, setting this
4591 * value to 1 will disable the delayed sack algorithm.
7708610b 4592 */
d364d927 4593static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
7708610b
FF
4594 char __user *optval,
4595 int __user *optlen)
4596{
d364d927 4597 struct sctp_sack_info params;
7708610b
FF
4598 struct sctp_association *asoc = NULL;
4599 struct sctp_sock *sp = sctp_sk(sk);
4600
d364d927
WY
4601 if (len >= sizeof(struct sctp_sack_info)) {
4602 len = sizeof(struct sctp_sack_info);
7708610b 4603
d364d927
WY
4604 if (copy_from_user(&params, optval, len))
4605 return -EFAULT;
4606 } else if (len == sizeof(struct sctp_assoc_value)) {
145ce502
JP
4607 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4608 pr_warn("Use struct sctp_sack_info instead\n");
d364d927
WY
4609 if (copy_from_user(&params, optval, len))
4610 return -EFAULT;
4611 } else
4612 return - EINVAL;
7708610b 4613
d364d927 4614 /* Get association, if sack_assoc_id != 0 and the socket is a one
7708610b
FF
4615 * to many style socket, and an association was not found, then
4616 * the id was invalid.
d808ad9a 4617 */
d364d927
WY
4618 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4619 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
7708610b
FF
4620 return -EINVAL;
4621
4622 if (asoc) {
4623 /* Fetch association values. */
d364d927
WY
4624 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4625 params.sack_delay = jiffies_to_msecs(
7708610b 4626 asoc->sackdelay);
d364d927
WY
4627 params.sack_freq = asoc->sackfreq;
4628
4629 } else {
4630 params.sack_delay = 0;
4631 params.sack_freq = 1;
4632 }
7708610b
FF
4633 } else {
4634 /* Fetch socket values. */
d364d927
WY
4635 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4636 params.sack_delay = sp->sackdelay;
4637 params.sack_freq = sp->sackfreq;
4638 } else {
4639 params.sack_delay = 0;
4640 params.sack_freq = 1;
4641 }
7708610b
FF
4642 }
4643
4644 if (copy_to_user(optval, &params, len))
4645 return -EFAULT;
4646
4647 if (put_user(len, optlen))
4648 return -EFAULT;
4649
4650 return 0;
4651}
4652
1da177e4
LT
4653/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4654 *
4655 * Applications can specify protocol parameters for the default association
4656 * initialization. The option name argument to setsockopt() and getsockopt()
4657 * is SCTP_INITMSG.
4658 *
4659 * Setting initialization parameters is effective only on an unconnected
4660 * socket (for UDP-style sockets only future associations are effected
4661 * by the change). With TCP-style sockets, this option is inherited by
4662 * sockets derived from a listener socket.
4663 */
4664static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4665{
408f22e8 4666 if (len < sizeof(struct sctp_initmsg))
1da177e4 4667 return -EINVAL;
408f22e8
NH
4668 len = sizeof(struct sctp_initmsg);
4669 if (put_user(len, optlen))
4670 return -EFAULT;
1da177e4
LT
4671 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4672 return -EFAULT;
4673 return 0;
4674}
4675