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