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