blob: 174d4d35e951590d7bef5d10df113afe36f88ae6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080066#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#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
Neil Horman4eb701d2005-04-28 12:02:04 -0700119 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (amt < 0)
128 amt = 0;
Neil Horman4eb701d2005-04-28 12:02:04 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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
Neil Horman4eb701d2005-04-28 12:02:04 -0700150 skb_set_owner_w(chunk->skb, sk);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 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
Neil Horman4eb701d2005-04-28 12:02:04 -0700156 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
157 sizeof(struct sk_buff) +
158 sizeof(struct sctp_chunk);
159
Neil Horman4eb701d2005-04-28 12:02:04 -0700160 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
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 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700262SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int retval = 0;
265
266 sctp_lock_sock(sk);
267
Frank Filz3f7a87d2005-06-20 13:14:57 -0700268 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
269 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* Disallow binding twice. */
272 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700273 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Common sockaddr verification. */
319 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700320 if (!af) {
321 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
322 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700324 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* PF specific bind() address verification. */
336 if (!sp->pf->bind_verify(sp, addr))
337 return -EADDRNOTAVAIL;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* 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) {
David S. Miller79af02c2005-07-08 21:47:49 -0700406 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 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.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700820 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
821 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
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. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800864 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 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
Frank Filz3f7a87d2005-06-20 13:14:57 -0700918/* __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 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001010 } 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 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001023 }
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);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001060 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001061
1062 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1063 err = sctp_wait_for_connect(asoc, &timeo);
1064
1065 /* Don't free association on exit. */
1066 asoc = NULL;
1067
1068out_free:
1069
1070 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1071 " kaddrs: %p err: %d\n",
1072 asoc, kaddrs, err);
1073 if (asoc)
1074 sctp_association_free(asoc);
1075 return err;
1076}
1077
1078/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1079 *
1080 * API 8.9
1081 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1082 *
1083 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1084 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1085 * or IPv6 addresses.
1086 *
1087 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1088 * Section 3.1.2 for this usage.
1089 *
1090 * addrs is a pointer to an array of one or more socket addresses. Each
1091 * address is contained in its appropriate structure (i.e. struct
1092 * sockaddr_in or struct sockaddr_in6) the family of the address type
1093 * must be used to distengish the address length (note that this
1094 * representation is termed a "packed array" of addresses). The caller
1095 * specifies the number of addresses in the array with addrcnt.
1096 *
1097 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1098 * -1, and sets errno to the appropriate error code.
1099 *
1100 * For SCTP, the port given in each socket address must be the same, or
1101 * sctp_connectx() will fail, setting errno to EINVAL.
1102 *
1103 * An application can use sctp_connectx to initiate an association with
1104 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1105 * allows a caller to specify multiple addresses at which a peer can be
1106 * reached. The way the SCTP stack uses the list of addresses to set up
1107 * the association is implementation dependant. This function only
1108 * specifies that the stack will try to make use of all the addresses in
1109 * the list when needed.
1110 *
1111 * Note that the list of addresses passed in is only used for setting up
1112 * the association. It does not necessarily equal the set of addresses
1113 * the peer uses for the resulting association. If the caller wants to
1114 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1115 * retrieve them after the association has been set up.
1116 *
1117 * Basically do nothing but copying the addresses from user to kernel
1118 * land and invoking either sctp_connectx(). This is used for tunneling
1119 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1120 *
1121 * We don't use copy_from_user() for optimization: we first do the
1122 * sanity checks (buffer size -fast- and access check-healthy
1123 * pointer); if all of those succeed, then we can alloc the memory
1124 * (expensive operation) needed to copy the data to kernel. Then we do
1125 * the copying without checking the user space area
1126 * (__copy_from_user()).
1127 *
1128 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1129 * it.
1130 *
1131 * sk The sk of the socket
1132 * addrs The pointer to the addresses in user land
1133 * addrssize Size of the addrs buffer
1134 *
1135 * Returns 0 if ok, <0 errno code on error.
1136 */
1137SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1138 struct sockaddr __user *addrs,
1139 int addrs_size)
1140{
1141 int err = 0;
1142 struct sockaddr *kaddrs;
1143
1144 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1145 __FUNCTION__, sk, addrs, addrs_size);
1146
1147 if (unlikely(addrs_size <= 0))
1148 return -EINVAL;
1149
1150 /* Check the user passed a healthy pointer. */
1151 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1152 return -EFAULT;
1153
1154 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001155 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001156 if (unlikely(!kaddrs))
1157 return -ENOMEM;
1158
1159 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1160 err = -EFAULT;
1161 } else {
1162 err = __sctp_connect(sk, kaddrs, addrs_size);
1163 }
1164
1165 kfree(kaddrs);
1166 return err;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/* API 3.1.4 close() - UDP Style Syntax
1170 * Applications use close() to perform graceful shutdown (as described in
1171 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1172 * by a UDP-style socket.
1173 *
1174 * The syntax is
1175 *
1176 * ret = close(int sd);
1177 *
1178 * sd - the socket descriptor of the associations to be closed.
1179 *
1180 * To gracefully shutdown a specific association represented by the
1181 * UDP-style socket, an application should use the sendmsg() call,
1182 * passing no user data, but including the appropriate flag in the
1183 * ancillary data (see Section xxxx).
1184 *
1185 * If sd in the close() call is a branched-off socket representing only
1186 * one association, the shutdown is performed on that association only.
1187 *
1188 * 4.1.6 close() - TCP Style Syntax
1189 *
1190 * Applications use close() to gracefully close down an association.
1191 *
1192 * The syntax is:
1193 *
1194 * int close(int sd);
1195 *
1196 * sd - the socket descriptor of the association to be closed.
1197 *
1198 * After an application calls close() on a socket descriptor, no further
1199 * socket operations will succeed on that descriptor.
1200 *
1201 * API 7.1.4 SO_LINGER
1202 *
1203 * An application using the TCP-style socket can use this option to
1204 * perform the SCTP ABORT primitive. The linger option structure is:
1205 *
1206 * struct linger {
1207 * int l_onoff; // option on/off
1208 * int l_linger; // linger time
1209 * };
1210 *
1211 * To enable the option, set l_onoff to 1. If the l_linger value is set
1212 * to 0, calling close() is the same as the ABORT primitive. If the
1213 * value is set to a negative value, the setsockopt() call will return
1214 * an error. If the value is set to a positive value linger_time, the
1215 * close() can be blocked for at most linger_time ms. If the graceful
1216 * shutdown phase does not finish during this period, close() will
1217 * return but the graceful shutdown phase continues in the system.
1218 */
1219SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1220{
1221 struct sctp_endpoint *ep;
1222 struct sctp_association *asoc;
1223 struct list_head *pos, *temp;
1224
1225 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1226
1227 sctp_lock_sock(sk);
1228 sk->sk_shutdown = SHUTDOWN_MASK;
1229
1230 ep = sctp_sk(sk)->ep;
1231
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001232 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 list_for_each_safe(pos, temp, &ep->asocs) {
1234 asoc = list_entry(pos, struct sctp_association, asocs);
1235
1236 if (sctp_style(sk, TCP)) {
1237 /* A closed association can still be in the list if
1238 * it belongs to a TCP-style listening socket that is
1239 * not yet accepted. If so, free it. If not, send an
1240 * ABORT or SHUTDOWN based on the linger options.
1241 */
1242 if (sctp_state(asoc, CLOSED)) {
1243 sctp_unhash_established(asoc);
1244 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001245 continue;
1246 }
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001249 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)
1250 sctp_primitive_ABORT(asoc, NULL);
1251 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sctp_primitive_SHUTDOWN(asoc, NULL);
1253 }
1254
1255 /* Clean up any skbs sitting on the receive queue. */
1256 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1257 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1258
1259 /* On a TCP-style socket, block for at most linger_time if set. */
1260 if (sctp_style(sk, TCP) && timeout)
1261 sctp_wait_for_close(sk, timeout);
1262
1263 /* This will run the backlog queue. */
1264 sctp_release_sock(sk);
1265
1266 /* Supposedly, no process has access to the socket, but
1267 * the net layers still may.
1268 */
1269 sctp_local_bh_disable();
1270 sctp_bh_lock_sock(sk);
1271
1272 /* Hold the sock, since sk_common_release() will put sock_put()
1273 * and we have just a little more cleanup.
1274 */
1275 sock_hold(sk);
1276 sk_common_release(sk);
1277
1278 sctp_bh_unlock_sock(sk);
1279 sctp_local_bh_enable();
1280
1281 sock_put(sk);
1282
1283 SCTP_DBG_OBJCNT_DEC(sock);
1284}
1285
1286/* Handle EPIPE error. */
1287static int sctp_error(struct sock *sk, int flags, int err)
1288{
1289 if (err == -EPIPE)
1290 err = sock_error(sk) ? : -EPIPE;
1291 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1292 send_sig(SIGPIPE, current, 0);
1293 return err;
1294}
1295
1296/* API 3.1.3 sendmsg() - UDP Style Syntax
1297 *
1298 * An application uses sendmsg() and recvmsg() calls to transmit data to
1299 * and receive data from its peer.
1300 *
1301 * ssize_t sendmsg(int socket, const struct msghdr *message,
1302 * int flags);
1303 *
1304 * socket - the socket descriptor of the endpoint.
1305 * message - pointer to the msghdr structure which contains a single
1306 * user message and possibly some ancillary data.
1307 *
1308 * See Section 5 for complete description of the data
1309 * structures.
1310 *
1311 * flags - flags sent or received with the user message, see Section
1312 * 5 for complete description of the flags.
1313 *
1314 * Note: This function could use a rewrite especially when explicit
1315 * connect support comes in.
1316 */
1317/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1318
1319SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1320
1321SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1322 struct msghdr *msg, size_t msg_len)
1323{
1324 struct sctp_sock *sp;
1325 struct sctp_endpoint *ep;
1326 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1327 struct sctp_transport *transport, *chunk_tp;
1328 struct sctp_chunk *chunk;
1329 union sctp_addr to;
1330 struct sockaddr *msg_name = NULL;
1331 struct sctp_sndrcvinfo default_sinfo = { 0 };
1332 struct sctp_sndrcvinfo *sinfo;
1333 struct sctp_initmsg *sinit;
1334 sctp_assoc_t associd = 0;
1335 sctp_cmsgs_t cmsgs = { NULL };
1336 int err;
1337 sctp_scope_t scope;
1338 long timeo;
1339 __u16 sinfo_flags = 0;
1340 struct sctp_datamsg *datamsg;
1341 struct list_head *pos;
1342 int msg_flags = msg->msg_flags;
1343
1344 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1345 sk, msg, msg_len);
1346
1347 err = 0;
1348 sp = sctp_sk(sk);
1349 ep = sp->ep;
1350
Frank Filz3f7a87d2005-06-20 13:14:57 -07001351 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* We cannot send a message over a TCP-style listening socket. */
1354 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1355 err = -EPIPE;
1356 goto out_nounlock;
1357 }
1358
1359 /* Parse out the SCTP CMSGs. */
1360 err = sctp_msghdr_parse(msg, &cmsgs);
1361
1362 if (err) {
1363 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1364 goto out_nounlock;
1365 }
1366
1367 /* Fetch the destination address for this packet. This
1368 * address only selects the association--it is not necessarily
1369 * the address we will send to.
1370 * For a peeled-off socket, msg_name is ignored.
1371 */
1372 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1373 int msg_namelen = msg->msg_namelen;
1374
1375 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1376 msg_namelen);
1377 if (err)
1378 return err;
1379
1380 if (msg_namelen > sizeof(to))
1381 msg_namelen = sizeof(to);
1382 memcpy(&to, msg->msg_name, msg_namelen);
1383 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1384 "0x%x:%u.\n",
1385 to.v4.sin_addr.s_addr, to.v4.sin_port);
1386
1387 to.v4.sin_port = ntohs(to.v4.sin_port);
1388 msg_name = msg->msg_name;
1389 }
1390
1391 sinfo = cmsgs.info;
1392 sinit = cmsgs.init;
1393
1394 /* Did the user specify SNDRCVINFO? */
1395 if (sinfo) {
1396 sinfo_flags = sinfo->sinfo_flags;
1397 associd = sinfo->sinfo_assoc_id;
1398 }
1399
1400 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1401 msg_len, sinfo_flags);
1402
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001403 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1404 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 err = -EINVAL;
1406 goto out_nounlock;
1407 }
1408
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001409 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1410 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1411 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * the msg_iov set to the user abort reason.
1413 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001414 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1415 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 err = -EINVAL;
1417 goto out_nounlock;
1418 }
1419
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001420 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * specified in msg_name.
1422 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001423 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 err = -EINVAL;
1425 goto out_nounlock;
1426 }
1427
1428 transport = NULL;
1429
1430 SCTP_DEBUG_PRINTK("About to look up association.\n");
1431
1432 sctp_lock_sock(sk);
1433
1434 /* If a msg_name has been specified, assume this is to be used. */
1435 if (msg_name) {
1436 /* Look for a matching association on the endpoint. */
1437 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1438 if (!asoc) {
1439 /* If we could not find a matching association on the
1440 * endpoint, make sure that it is not a TCP-style
1441 * socket that already has an association or there is
1442 * no peeled-off association on another socket.
1443 */
1444 if ((sctp_style(sk, TCP) &&
1445 sctp_sstate(sk, ESTABLISHED)) ||
1446 sctp_endpoint_is_peeled_off(ep, &to)) {
1447 err = -EADDRNOTAVAIL;
1448 goto out_unlock;
1449 }
1450 }
1451 } else {
1452 asoc = sctp_id2assoc(sk, associd);
1453 if (!asoc) {
1454 err = -EPIPE;
1455 goto out_unlock;
1456 }
1457 }
1458
1459 if (asoc) {
1460 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1461
1462 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1463 * socket that has an association in CLOSED state. This can
1464 * happen when an accepted socket has an association that is
1465 * already CLOSED.
1466 */
1467 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1468 err = -EPIPE;
1469 goto out_unlock;
1470 }
1471
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001472 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1474 asoc);
1475 sctp_primitive_SHUTDOWN(asoc, NULL);
1476 err = 0;
1477 goto out_unlock;
1478 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001479 if (sinfo_flags & SCTP_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1481 sctp_primitive_ABORT(asoc, msg);
1482 err = 0;
1483 goto out_unlock;
1484 }
1485 }
1486
1487 /* Do we need to create the association? */
1488 if (!asoc) {
1489 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1490
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001491 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 err = -EINVAL;
1493 goto out_unlock;
1494 }
1495
1496 /* Check for invalid stream against the stream counts,
1497 * either the default or the user specified stream counts.
1498 */
1499 if (sinfo) {
1500 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1501 /* Check against the defaults. */
1502 if (sinfo->sinfo_stream >=
1503 sp->initmsg.sinit_num_ostreams) {
1504 err = -EINVAL;
1505 goto out_unlock;
1506 }
1507 } else {
1508 /* Check against the requested. */
1509 if (sinfo->sinfo_stream >=
1510 sinit->sinit_num_ostreams) {
1511 err = -EINVAL;
1512 goto out_unlock;
1513 }
1514 }
1515 }
1516
1517 /*
1518 * API 3.1.2 bind() - UDP Style Syntax
1519 * If a bind() or sctp_bindx() is not called prior to a
1520 * sendmsg() call that initiates a new association, the
1521 * system picks an ephemeral port and will choose an address
1522 * set equivalent to binding with a wildcard address.
1523 */
1524 if (!ep->base.bind_addr.port) {
1525 if (sctp_autobind(sk)) {
1526 err = -EAGAIN;
1527 goto out_unlock;
1528 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001529 } else {
1530 /*
1531 * If an unprivileged user inherits a one-to-many
1532 * style socket with open associations on a privileged
1533 * port, it MAY be permitted to accept new associations,
1534 * but it SHOULD NOT be permitted to open new
1535 * associations.
1536 */
1537 if (ep->base.bind_addr.port < PROT_SOCK &&
1538 !capable(CAP_NET_BIND_SERVICE)) {
1539 err = -EACCES;
1540 goto out_unlock;
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
1544 scope = sctp_scope(&to);
1545 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1546 if (!new_asoc) {
1547 err = -ENOMEM;
1548 goto out_unlock;
1549 }
1550 asoc = new_asoc;
1551
1552 /* If the SCTP_INIT ancillary data is specified, set all
1553 * the association init values accordingly.
1554 */
1555 if (sinit) {
1556 if (sinit->sinit_num_ostreams) {
1557 asoc->c.sinit_num_ostreams =
1558 sinit->sinit_num_ostreams;
1559 }
1560 if (sinit->sinit_max_instreams) {
1561 asoc->c.sinit_max_instreams =
1562 sinit->sinit_max_instreams;
1563 }
1564 if (sinit->sinit_max_attempts) {
1565 asoc->max_init_attempts
1566 = sinit->sinit_max_attempts;
1567 }
1568 if (sinit->sinit_max_init_timeo) {
1569 asoc->max_init_timeo =
1570 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1571 }
1572 }
1573
1574 /* Prime the peer's transport structures. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001575 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (!transport) {
1577 err = -ENOMEM;
1578 goto out_free;
1579 }
1580 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1581 if (err < 0) {
1582 err = -ENOMEM;
1583 goto out_free;
1584 }
1585 }
1586
1587 /* ASSERT: we have a valid association at this point. */
1588 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1589
1590 if (!sinfo) {
1591 /* If the user didn't specify SNDRCVINFO, make up one with
1592 * some defaults.
1593 */
1594 default_sinfo.sinfo_stream = asoc->default_stream;
1595 default_sinfo.sinfo_flags = asoc->default_flags;
1596 default_sinfo.sinfo_ppid = asoc->default_ppid;
1597 default_sinfo.sinfo_context = asoc->default_context;
1598 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1599 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1600 sinfo = &default_sinfo;
1601 }
1602
1603 /* API 7.1.7, the sndbuf size per association bounds the
1604 * maximum size of data that can be sent in a single send call.
1605 */
1606 if (msg_len > sk->sk_sndbuf) {
1607 err = -EMSGSIZE;
1608 goto out_free;
1609 }
1610
1611 /* If fragmentation is disabled and the message length exceeds the
1612 * association fragmentation point, return EMSGSIZE. The I-D
1613 * does not specify what this error is, but this looks like
1614 * a great fit.
1615 */
1616 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1617 err = -EMSGSIZE;
1618 goto out_free;
1619 }
1620
1621 if (sinfo) {
1622 /* Check for invalid stream. */
1623 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1624 err = -EINVAL;
1625 goto out_free;
1626 }
1627 }
1628
1629 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1630 if (!sctp_wspace(asoc)) {
1631 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1632 if (err)
1633 goto out_free;
1634 }
1635
1636 /* If an address is passed with the sendto/sendmsg call, it is used
1637 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001638 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 */
1640 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001641 (sinfo_flags & SCTP_ADDR_OVER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1643 if (!chunk_tp) {
1644 err = -EINVAL;
1645 goto out_free;
1646 }
1647 } else
1648 chunk_tp = NULL;
1649
1650 /* Auto-connect, if we aren't connected already. */
1651 if (sctp_state(asoc, CLOSED)) {
1652 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1653 if (err < 0)
1654 goto out_free;
1655 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1656 }
1657
1658 /* Break the message into multiple chunks of maximum size. */
1659 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1660 if (!datamsg) {
1661 err = -ENOMEM;
1662 goto out_free;
1663 }
1664
1665 /* Now send the (possibly) fragmented message. */
1666 list_for_each(pos, &datamsg->chunks) {
1667 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1668 sctp_datamsg_track(chunk);
1669
1670 /* Do accounting for the write space. */
1671 sctp_set_owner_w(chunk);
1672
1673 chunk->transport = chunk_tp;
1674
1675 /* Send it to the lower layers. Note: all chunks
1676 * must either fail or succeed. The lower layer
1677 * works that way today. Keep it that way or this
1678 * breaks.
1679 */
1680 err = sctp_primitive_SEND(asoc, chunk);
1681 /* Did the lower layer accept the chunk? */
1682 if (err)
1683 sctp_chunk_free(chunk);
1684 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1685 }
1686
1687 sctp_datamsg_free(datamsg);
1688 if (err)
1689 goto out_free;
1690 else
1691 err = msg_len;
1692
1693 /* If we are already past ASSOCIATE, the lower
1694 * layers are responsible for association cleanup.
1695 */
1696 goto out_unlock;
1697
1698out_free:
1699 if (new_asoc)
1700 sctp_association_free(asoc);
1701out_unlock:
1702 sctp_release_sock(sk);
1703
1704out_nounlock:
1705 return sctp_error(sk, msg_flags, err);
1706
1707#if 0
1708do_sock_err:
1709 if (msg_len)
1710 err = msg_len;
1711 else
1712 err = sock_error(sk);
1713 goto out;
1714
1715do_interrupted:
1716 if (msg_len)
1717 err = msg_len;
1718 goto out;
1719#endif /* 0 */
1720}
1721
1722/* This is an extended version of skb_pull() that removes the data from the
1723 * start of a skb even when data is spread across the list of skb's in the
1724 * frag_list. len specifies the total amount of data that needs to be removed.
1725 * when 'len' bytes could be removed from the skb, it returns 0.
1726 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1727 * could not be removed.
1728 */
1729static int sctp_skb_pull(struct sk_buff *skb, int len)
1730{
1731 struct sk_buff *list;
1732 int skb_len = skb_headlen(skb);
1733 int rlen;
1734
1735 if (len <= skb_len) {
1736 __skb_pull(skb, len);
1737 return 0;
1738 }
1739 len -= skb_len;
1740 __skb_pull(skb, skb_len);
1741
1742 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1743 rlen = sctp_skb_pull(list, len);
1744 skb->len -= (len-rlen);
1745 skb->data_len -= (len-rlen);
1746
1747 if (!rlen)
1748 return 0;
1749
1750 len = rlen;
1751 }
1752
1753 return len;
1754}
1755
1756/* API 3.1.3 recvmsg() - UDP Style Syntax
1757 *
1758 * ssize_t recvmsg(int socket, struct msghdr *message,
1759 * int flags);
1760 *
1761 * socket - the socket descriptor of the endpoint.
1762 * message - pointer to the msghdr structure which contains a single
1763 * user message and possibly some ancillary data.
1764 *
1765 * See Section 5 for complete description of the data
1766 * structures.
1767 *
1768 * flags - flags sent or received with the user message, see Section
1769 * 5 for complete description of the flags.
1770 */
1771static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1772
1773SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1774 struct msghdr *msg, size_t len, int noblock,
1775 int flags, int *addr_len)
1776{
1777 struct sctp_ulpevent *event = NULL;
1778 struct sctp_sock *sp = sctp_sk(sk);
1779 struct sk_buff *skb;
1780 int copied;
1781 int err = 0;
1782 int skb_len;
1783
1784 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1785 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1786 "len", len, "knoblauch", noblock,
1787 "flags", flags, "addr_len", addr_len);
1788
1789 sctp_lock_sock(sk);
1790
1791 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1792 err = -ENOTCONN;
1793 goto out;
1794 }
1795
1796 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1797 if (!skb)
1798 goto out;
1799
1800 /* Get the total length of the skb including any skb's in the
1801 * frag_list.
1802 */
1803 skb_len = skb->len;
1804
1805 copied = skb_len;
1806 if (copied > len)
1807 copied = len;
1808
1809 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1810
1811 event = sctp_skb2event(skb);
1812
1813 if (err)
1814 goto out_free;
1815
1816 sock_recv_timestamp(msg, sk, skb);
1817 if (sctp_ulpevent_is_notification(event)) {
1818 msg->msg_flags |= MSG_NOTIFICATION;
1819 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1820 } else {
1821 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1822 }
1823
1824 /* Check if we allow SCTP_SNDRCVINFO. */
1825 if (sp->subscribe.sctp_data_io_event)
1826 sctp_ulpevent_read_sndrcvinfo(event, msg);
1827#if 0
1828 /* FIXME: we should be calling IP/IPv6 layers. */
1829 if (sk->sk_protinfo.af_inet.cmsg_flags)
1830 ip_cmsg_recv(msg, skb);
1831#endif
1832
1833 err = copied;
1834
1835 /* If skb's length exceeds the user's buffer, update the skb and
1836 * push it back to the receive_queue so that the next call to
1837 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1838 */
1839 if (skb_len > copied) {
1840 msg->msg_flags &= ~MSG_EOR;
1841 if (flags & MSG_PEEK)
1842 goto out_free;
1843 sctp_skb_pull(skb, copied);
1844 skb_queue_head(&sk->sk_receive_queue, skb);
1845
1846 /* When only partial message is copied to the user, increase
1847 * rwnd by that amount. If all the data in the skb is read,
1848 * rwnd is updated when the event is freed.
1849 */
1850 sctp_assoc_rwnd_increase(event->asoc, copied);
1851 goto out;
1852 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1853 (event->msg_flags & MSG_EOR))
1854 msg->msg_flags |= MSG_EOR;
1855 else
1856 msg->msg_flags &= ~MSG_EOR;
1857
1858out_free:
1859 if (flags & MSG_PEEK) {
1860 /* Release the skb reference acquired after peeking the skb in
1861 * sctp_skb_recv_datagram().
1862 */
1863 kfree_skb(skb);
1864 } else {
1865 /* Free the event which includes releasing the reference to
1866 * the owner of the skb, freeing the skb and updating the
1867 * rwnd.
1868 */
1869 sctp_ulpevent_free(event);
1870 }
1871out:
1872 sctp_release_sock(sk);
1873 return err;
1874}
1875
1876/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1877 *
1878 * This option is a on/off flag. If enabled no SCTP message
1879 * fragmentation will be performed. Instead if a message being sent
1880 * exceeds the current PMTU size, the message will NOT be sent and
1881 * instead a error will be indicated to the user.
1882 */
1883static int sctp_setsockopt_disable_fragments(struct sock *sk,
1884 char __user *optval, int optlen)
1885{
1886 int val;
1887
1888 if (optlen < sizeof(int))
1889 return -EINVAL;
1890
1891 if (get_user(val, (int __user *)optval))
1892 return -EFAULT;
1893
1894 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1895
1896 return 0;
1897}
1898
1899static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1900 int optlen)
1901{
1902 if (optlen != sizeof(struct sctp_event_subscribe))
1903 return -EINVAL;
1904 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1905 return -EFAULT;
1906 return 0;
1907}
1908
1909/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1910 *
1911 * This socket option is applicable to the UDP-style socket only. When
1912 * set it will cause associations that are idle for more than the
1913 * specified number of seconds to automatically close. An association
1914 * being idle is defined an association that has NOT sent or received
1915 * user data. The special value of '0' indicates that no automatic
1916 * close of any associations should be performed. The option expects an
1917 * integer defining the number of seconds of idle time before an
1918 * association is closed.
1919 */
1920static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1921 int optlen)
1922{
1923 struct sctp_sock *sp = sctp_sk(sk);
1924
1925 /* Applicable to UDP-style socket only */
1926 if (sctp_style(sk, TCP))
1927 return -EOPNOTSUPP;
1928 if (optlen != sizeof(int))
1929 return -EINVAL;
1930 if (copy_from_user(&sp->autoclose, optval, optlen))
1931 return -EFAULT;
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
1936/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1937 *
1938 * Applications can enable or disable heartbeats for any peer address of
1939 * an association, modify an address's heartbeat interval, force a
1940 * heartbeat to be sent immediately, and adjust the address's maximum
1941 * number of retransmissions sent before an address is considered
1942 * unreachable. The following structure is used to access and modify an
1943 * address's parameters:
1944 *
1945 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08001946 * sctp_assoc_t spp_assoc_id;
1947 * struct sockaddr_storage spp_address;
1948 * uint32_t spp_hbinterval;
1949 * uint16_t spp_pathmaxrxt;
1950 * uint32_t spp_pathmtu;
1951 * uint32_t spp_sackdelay;
1952 * uint32_t spp_flags;
1953 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08001955 * spp_assoc_id - (one-to-many style socket) This is filled in the
1956 * application, and identifies the association for
1957 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * spp_address - This specifies which address is of interest.
1959 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08001960 * in milliseconds. If a value of zero
1961 * is present in this field then no changes are to
1962 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * spp_pathmaxrxt - This contains the maximum number of
1964 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08001965 * considered unreachable. If a value of zero
1966 * is present in this field then no changes are to
1967 * be made to this parameter.
1968 * spp_pathmtu - When Path MTU discovery is disabled the value
1969 * specified here will be the "fixed" path mtu.
1970 * Note that if the spp_address field is empty
1971 * then all associations on this address will
1972 * have this fixed path mtu set upon them.
1973 *
1974 * spp_sackdelay - When delayed sack is enabled, this value specifies
1975 * the number of milliseconds that sacks will be delayed
1976 * for. This value will apply to all addresses of an
1977 * association if the spp_address field is empty. Note
1978 * also, that if delayed sack is enabled and this
1979 * value is set to 0, no change is made to the last
1980 * recorded delayed sack timer value.
1981 *
1982 * spp_flags - These flags are used to control various features
1983 * on an association. The flag field may contain
1984 * zero or more of the following options.
1985 *
1986 * SPP_HB_ENABLE - Enable heartbeats on the
1987 * specified address. Note that if the address
1988 * field is empty all addresses for the association
1989 * have heartbeats enabled upon them.
1990 *
1991 * SPP_HB_DISABLE - Disable heartbeats on the
1992 * speicifed address. Note that if the address
1993 * field is empty all addresses for the association
1994 * will have their heartbeats disabled. Note also
1995 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
1996 * mutually exclusive, only one of these two should
1997 * be specified. Enabling both fields will have
1998 * undetermined results.
1999 *
2000 * SPP_HB_DEMAND - Request a user initiated heartbeat
2001 * to be made immediately.
2002 *
2003 * SPP_PMTUD_ENABLE - This field will enable PMTU
2004 * discovery upon the specified address. Note that
2005 * if the address feild is empty then all addresses
2006 * on the association are effected.
2007 *
2008 * SPP_PMTUD_DISABLE - This field will disable PMTU
2009 * discovery upon the specified address. Note that
2010 * if the address feild is empty then all addresses
2011 * on the association are effected. Not also that
2012 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2013 * exclusive. Enabling both will have undetermined
2014 * results.
2015 *
2016 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2017 * on delayed sack. The time specified in spp_sackdelay
2018 * is used to specify the sack delay for this address. Note
2019 * that if spp_address is empty then all addresses will
2020 * enable delayed sack and take on the sack delay
2021 * value specified in spp_sackdelay.
2022 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2023 * off delayed sack. If the spp_address field is blank then
2024 * delayed sack is disabled for the entire association. Note
2025 * also that this field is mutually exclusive to
2026 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2027 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002029int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2030 struct sctp_transport *trans,
2031 struct sctp_association *asoc,
2032 struct sctp_sock *sp,
2033 int hb_change,
2034 int pmtud_change,
2035 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int error;
2038
Frank Filz52ccb8e2005-12-22 11:36:46 -08002039 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2041 if (error)
2042 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
Frank Filz52ccb8e2005-12-22 11:36:46 -08002045 if (params->spp_hbinterval) {
2046 if (trans) {
2047 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2048 } else if (asoc) {
2049 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2050 } else {
2051 sp->hbinterval = params->spp_hbinterval;
2052 }
2053 }
2054
2055 if (hb_change) {
2056 if (trans) {
2057 trans->param_flags =
2058 (trans->param_flags & ~SPP_HB) | hb_change;
2059 } else if (asoc) {
2060 asoc->param_flags =
2061 (asoc->param_flags & ~SPP_HB) | hb_change;
2062 } else {
2063 sp->param_flags =
2064 (sp->param_flags & ~SPP_HB) | hb_change;
2065 }
2066 }
2067
2068 if (params->spp_pathmtu) {
2069 if (trans) {
2070 trans->pathmtu = params->spp_pathmtu;
2071 sctp_assoc_sync_pmtu(asoc);
2072 } else if (asoc) {
2073 asoc->pathmtu = params->spp_pathmtu;
2074 sctp_frag_point(sp, params->spp_pathmtu);
2075 } else {
2076 sp->pathmtu = params->spp_pathmtu;
2077 }
2078 }
2079
2080 if (pmtud_change) {
2081 if (trans) {
2082 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2083 (params->spp_flags & SPP_PMTUD_ENABLE);
2084 trans->param_flags =
2085 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2086 if (update) {
2087 sctp_transport_pmtu(trans);
2088 sctp_assoc_sync_pmtu(asoc);
2089 }
2090 } else if (asoc) {
2091 asoc->param_flags =
2092 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2093 } else {
2094 sp->param_flags =
2095 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2096 }
2097 }
2098
2099 if (params->spp_sackdelay) {
2100 if (trans) {
2101 trans->sackdelay =
2102 msecs_to_jiffies(params->spp_sackdelay);
2103 } else if (asoc) {
2104 asoc->sackdelay =
2105 msecs_to_jiffies(params->spp_sackdelay);
2106 } else {
2107 sp->sackdelay = params->spp_sackdelay;
2108 }
2109 }
2110
2111 if (sackdelay_change) {
2112 if (trans) {
2113 trans->param_flags =
2114 (trans->param_flags & ~SPP_SACKDELAY) |
2115 sackdelay_change;
2116 } else if (asoc) {
2117 asoc->param_flags =
2118 (asoc->param_flags & ~SPP_SACKDELAY) |
2119 sackdelay_change;
2120 } else {
2121 sp->param_flags =
2122 (sp->param_flags & ~SPP_SACKDELAY) |
2123 sackdelay_change;
2124 }
2125 }
2126
2127 if (params->spp_pathmaxrxt) {
2128 if (trans) {
2129 trans->pathmaxrxt = params->spp_pathmaxrxt;
2130 } else if (asoc) {
2131 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2132 } else {
2133 sp->pathmaxrxt = params->spp_pathmaxrxt;
2134 }
2135 }
2136
2137 return 0;
2138}
2139
2140static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2141 char __user *optval, int optlen)
2142{
2143 struct sctp_paddrparams params;
2144 struct sctp_transport *trans = NULL;
2145 struct sctp_association *asoc = NULL;
2146 struct sctp_sock *sp = sctp_sk(sk);
2147 int error;
2148 int hb_change, pmtud_change, sackdelay_change;
2149
2150 if (optlen != sizeof(struct sctp_paddrparams))
2151 return - EINVAL;
2152
2153 if (copy_from_user(&params, optval, optlen))
2154 return -EFAULT;
2155
2156 /* Validate flags and value parameters. */
2157 hb_change = params.spp_flags & SPP_HB;
2158 pmtud_change = params.spp_flags & SPP_PMTUD;
2159 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2160
2161 if (hb_change == SPP_HB ||
2162 pmtud_change == SPP_PMTUD ||
2163 sackdelay_change == SPP_SACKDELAY ||
2164 params.spp_sackdelay > 500 ||
2165 (params.spp_pathmtu
2166 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2167 return -EINVAL;
2168
2169 /* If an address other than INADDR_ANY is specified, and
2170 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002172 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2173 trans = sctp_addr_id2transport(sk, &params.spp_address,
2174 params.spp_assoc_id);
2175 if (!trans)
2176 return -EINVAL;
2177 }
2178
2179 /* Get association, if assoc_id != 0 and the socket is a one
2180 * to many style socket, and an association was not found, then
2181 * the id was invalid.
2182 */
2183 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2184 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2185 return -EINVAL;
2186
2187 /* Heartbeat demand can only be sent on a transport or
2188 * association, but not a socket.
2189 */
2190 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2191 return -EINVAL;
2192
2193 /* Process parameters. */
2194 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2195 hb_change, pmtud_change,
2196 sackdelay_change);
2197
2198 if (error)
2199 return error;
2200
2201 /* If changes are for association, also apply parameters to each
2202 * transport.
2203 */
2204 if (!trans && asoc) {
2205 struct list_head *pos;
2206
2207 list_for_each(pos, &asoc->peer.transport_addr_list) {
2208 trans = list_entry(pos, struct sctp_transport,
2209 transports);
2210 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2211 hb_change, pmtud_change,
2212 sackdelay_change);
2213 }
2214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 return 0;
2217}
2218
Frank Filz77086102005-12-22 11:37:30 -08002219/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2220 *
2221 * This options will get or set the delayed ack timer. The time is set
2222 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2223 * endpoints default delayed ack timer value. If the assoc_id field is
2224 * non-zero, then the set or get effects the specified association.
2225 *
2226 * struct sctp_assoc_value {
2227 * sctp_assoc_t assoc_id;
2228 * uint32_t assoc_value;
2229 * };
2230 *
2231 * assoc_id - This parameter, indicates which association the
2232 * user is preforming an action upon. Note that if
2233 * this field's value is zero then the endpoints
2234 * default value is changed (effecting future
2235 * associations only).
2236 *
2237 * assoc_value - This parameter contains the number of milliseconds
2238 * that the user is requesting the delayed ACK timer
2239 * be set to. Note that this value is defined in
2240 * the standard to be between 200 and 500 milliseconds.
2241 *
2242 * Note: a value of zero will leave the value alone,
2243 * but disable SACK delay. A non-zero value will also
2244 * enable SACK delay.
2245 */
2246
2247static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2248 char __user *optval, int optlen)
2249{
2250 struct sctp_assoc_value params;
2251 struct sctp_transport *trans = NULL;
2252 struct sctp_association *asoc = NULL;
2253 struct sctp_sock *sp = sctp_sk(sk);
2254
2255 if (optlen != sizeof(struct sctp_assoc_value))
2256 return - EINVAL;
2257
2258 if (copy_from_user(&params, optval, optlen))
2259 return -EFAULT;
2260
2261 /* Validate value parameter. */
2262 if (params.assoc_value > 500)
2263 return -EINVAL;
2264
2265 /* Get association, if assoc_id != 0 and the socket is a one
2266 * to many style socket, and an association was not found, then
2267 * the id was invalid.
2268 */
2269 asoc = sctp_id2assoc(sk, params.assoc_id);
2270 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2271 return -EINVAL;
2272
2273 if (params.assoc_value) {
2274 if (asoc) {
2275 asoc->sackdelay =
2276 msecs_to_jiffies(params.assoc_value);
2277 asoc->param_flags =
2278 (asoc->param_flags & ~SPP_SACKDELAY) |
2279 SPP_SACKDELAY_ENABLE;
2280 } else {
2281 sp->sackdelay = params.assoc_value;
2282 sp->param_flags =
2283 (sp->param_flags & ~SPP_SACKDELAY) |
2284 SPP_SACKDELAY_ENABLE;
2285 }
2286 } else {
2287 if (asoc) {
2288 asoc->param_flags =
2289 (asoc->param_flags & ~SPP_SACKDELAY) |
2290 SPP_SACKDELAY_DISABLE;
2291 } else {
2292 sp->param_flags =
2293 (sp->param_flags & ~SPP_SACKDELAY) |
2294 SPP_SACKDELAY_DISABLE;
2295 }
2296 }
2297
2298 /* If change is for association, also apply to each transport. */
2299 if (asoc) {
2300 struct list_head *pos;
2301
2302 list_for_each(pos, &asoc->peer.transport_addr_list) {
2303 trans = list_entry(pos, struct sctp_transport,
2304 transports);
2305 if (params.assoc_value) {
2306 trans->sackdelay =
2307 msecs_to_jiffies(params.assoc_value);
2308 trans->param_flags =
2309 (trans->param_flags & ~SPP_SACKDELAY) |
2310 SPP_SACKDELAY_ENABLE;
2311 } else {
2312 trans->param_flags =
2313 (trans->param_flags & ~SPP_SACKDELAY) |
2314 SPP_SACKDELAY_DISABLE;
2315 }
2316 }
2317 }
2318
2319 return 0;
2320}
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2323 *
2324 * Applications can specify protocol parameters for the default association
2325 * initialization. The option name argument to setsockopt() and getsockopt()
2326 * is SCTP_INITMSG.
2327 *
2328 * Setting initialization parameters is effective only on an unconnected
2329 * socket (for UDP-style sockets only future associations are effected
2330 * by the change). With TCP-style sockets, this option is inherited by
2331 * sockets derived from a listener socket.
2332 */
2333static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2334{
2335 struct sctp_initmsg sinit;
2336 struct sctp_sock *sp = sctp_sk(sk);
2337
2338 if (optlen != sizeof(struct sctp_initmsg))
2339 return -EINVAL;
2340 if (copy_from_user(&sinit, optval, optlen))
2341 return -EFAULT;
2342
2343 if (sinit.sinit_num_ostreams)
2344 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2345 if (sinit.sinit_max_instreams)
2346 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2347 if (sinit.sinit_max_attempts)
2348 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2349 if (sinit.sinit_max_init_timeo)
2350 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2351
2352 return 0;
2353}
2354
2355/*
2356 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2357 *
2358 * Applications that wish to use the sendto() system call may wish to
2359 * specify a default set of parameters that would normally be supplied
2360 * through the inclusion of ancillary data. This socket option allows
2361 * such an application to set the default sctp_sndrcvinfo structure.
2362 * The application that wishes to use this socket option simply passes
2363 * in to this call the sctp_sndrcvinfo structure defined in Section
2364 * 5.2.2) The input parameters accepted by this call include
2365 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2366 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2367 * to this call if the caller is using the UDP model.
2368 */
2369static int sctp_setsockopt_default_send_param(struct sock *sk,
2370 char __user *optval, int optlen)
2371{
2372 struct sctp_sndrcvinfo info;
2373 struct sctp_association *asoc;
2374 struct sctp_sock *sp = sctp_sk(sk);
2375
2376 if (optlen != sizeof(struct sctp_sndrcvinfo))
2377 return -EINVAL;
2378 if (copy_from_user(&info, optval, optlen))
2379 return -EFAULT;
2380
2381 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2382 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2383 return -EINVAL;
2384
2385 if (asoc) {
2386 asoc->default_stream = info.sinfo_stream;
2387 asoc->default_flags = info.sinfo_flags;
2388 asoc->default_ppid = info.sinfo_ppid;
2389 asoc->default_context = info.sinfo_context;
2390 asoc->default_timetolive = info.sinfo_timetolive;
2391 } else {
2392 sp->default_stream = info.sinfo_stream;
2393 sp->default_flags = info.sinfo_flags;
2394 sp->default_ppid = info.sinfo_ppid;
2395 sp->default_context = info.sinfo_context;
2396 sp->default_timetolive = info.sinfo_timetolive;
2397 }
2398
2399 return 0;
2400}
2401
2402/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2403 *
2404 * Requests that the local SCTP stack use the enclosed peer address as
2405 * the association primary. The enclosed address must be one of the
2406 * association peer's addresses.
2407 */
2408static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2409 int optlen)
2410{
2411 struct sctp_prim prim;
2412 struct sctp_transport *trans;
2413
2414 if (optlen != sizeof(struct sctp_prim))
2415 return -EINVAL;
2416
2417 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2418 return -EFAULT;
2419
2420 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2421 if (!trans)
2422 return -EINVAL;
2423
2424 sctp_assoc_set_primary(trans->asoc, trans);
2425
2426 return 0;
2427}
2428
2429/*
2430 * 7.1.5 SCTP_NODELAY
2431 *
2432 * Turn on/off any Nagle-like algorithm. This means that packets are
2433 * generally sent as soon as possible and no unnecessary delays are
2434 * introduced, at the cost of more packets in the network. Expects an
2435 * integer boolean flag.
2436 */
2437static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2438 int optlen)
2439{
2440 int val;
2441
2442 if (optlen < sizeof(int))
2443 return -EINVAL;
2444 if (get_user(val, (int __user *)optval))
2445 return -EFAULT;
2446
2447 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2448 return 0;
2449}
2450
2451/*
2452 *
2453 * 7.1.1 SCTP_RTOINFO
2454 *
2455 * The protocol parameters used to initialize and bound retransmission
2456 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2457 * and modify these parameters.
2458 * All parameters are time values, in milliseconds. A value of 0, when
2459 * modifying the parameters, indicates that the current value should not
2460 * be changed.
2461 *
2462 */
2463static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2464 struct sctp_rtoinfo rtoinfo;
2465 struct sctp_association *asoc;
2466
2467 if (optlen != sizeof (struct sctp_rtoinfo))
2468 return -EINVAL;
2469
2470 if (copy_from_user(&rtoinfo, optval, optlen))
2471 return -EFAULT;
2472
2473 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2474
2475 /* Set the values to the specific association */
2476 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2477 return -EINVAL;
2478
2479 if (asoc) {
2480 if (rtoinfo.srto_initial != 0)
2481 asoc->rto_initial =
2482 msecs_to_jiffies(rtoinfo.srto_initial);
2483 if (rtoinfo.srto_max != 0)
2484 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2485 if (rtoinfo.srto_min != 0)
2486 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2487 } else {
2488 /* If there is no association or the association-id = 0
2489 * set the values to the endpoint.
2490 */
2491 struct sctp_sock *sp = sctp_sk(sk);
2492
2493 if (rtoinfo.srto_initial != 0)
2494 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2495 if (rtoinfo.srto_max != 0)
2496 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2497 if (rtoinfo.srto_min != 0)
2498 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2499 }
2500
2501 return 0;
2502}
2503
2504/*
2505 *
2506 * 7.1.2 SCTP_ASSOCINFO
2507 *
2508 * This option is used to tune the the maximum retransmission attempts
2509 * of the association.
2510 * Returns an error if the new association retransmission value is
2511 * greater than the sum of the retransmission value of the peer.
2512 * See [SCTP] for more information.
2513 *
2514 */
2515static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2516{
2517
2518 struct sctp_assocparams assocparams;
2519 struct sctp_association *asoc;
2520
2521 if (optlen != sizeof(struct sctp_assocparams))
2522 return -EINVAL;
2523 if (copy_from_user(&assocparams, optval, optlen))
2524 return -EFAULT;
2525
2526 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2527
2528 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2529 return -EINVAL;
2530
2531 /* Set the values to the specific association */
2532 if (asoc) {
2533 if (assocparams.sasoc_asocmaxrxt != 0)
2534 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2535 if (assocparams.sasoc_cookie_life != 0) {
2536 asoc->cookie_life.tv_sec =
2537 assocparams.sasoc_cookie_life / 1000;
2538 asoc->cookie_life.tv_usec =
2539 (assocparams.sasoc_cookie_life % 1000)
2540 * 1000;
2541 }
2542 } else {
2543 /* Set the values to the endpoint */
2544 struct sctp_sock *sp = sctp_sk(sk);
2545
2546 if (assocparams.sasoc_asocmaxrxt != 0)
2547 sp->assocparams.sasoc_asocmaxrxt =
2548 assocparams.sasoc_asocmaxrxt;
2549 if (assocparams.sasoc_cookie_life != 0)
2550 sp->assocparams.sasoc_cookie_life =
2551 assocparams.sasoc_cookie_life;
2552 }
2553 return 0;
2554}
2555
2556/*
2557 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2558 *
2559 * This socket option is a boolean flag which turns on or off mapped V4
2560 * addresses. If this option is turned on and the socket is type
2561 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2562 * If this option is turned off, then no mapping will be done of V4
2563 * addresses and a user will receive both PF_INET6 and PF_INET type
2564 * addresses on the socket.
2565 */
2566static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2567{
2568 int val;
2569 struct sctp_sock *sp = sctp_sk(sk);
2570
2571 if (optlen < sizeof(int))
2572 return -EINVAL;
2573 if (get_user(val, (int __user *)optval))
2574 return -EFAULT;
2575 if (val)
2576 sp->v4mapped = 1;
2577 else
2578 sp->v4mapped = 0;
2579
2580 return 0;
2581}
2582
2583/*
2584 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2585 *
2586 * This socket option specifies the maximum size to put in any outgoing
2587 * SCTP chunk. If a message is larger than this size it will be
2588 * fragmented by SCTP into the specified size. Note that the underlying
2589 * SCTP implementation may fragment into smaller sized chunks when the
2590 * PMTU of the underlying association is smaller than the value set by
2591 * the user.
2592 */
2593static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2594{
2595 struct sctp_association *asoc;
2596 struct list_head *pos;
2597 struct sctp_sock *sp = sctp_sk(sk);
2598 int val;
2599
2600 if (optlen < sizeof(int))
2601 return -EINVAL;
2602 if (get_user(val, (int __user *)optval))
2603 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002604 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return -EINVAL;
2606 sp->user_frag = val;
2607
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002608 /* Update the frag_point of the existing associations. */
2609 list_for_each(pos, &(sp->ep->asocs)) {
2610 asoc = list_entry(pos, struct sctp_association, asocs);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002611 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613
2614 return 0;
2615}
2616
2617
2618/*
2619 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2620 *
2621 * Requests that the peer mark the enclosed address as the association
2622 * primary. The enclosed address must be one of the association's
2623 * locally bound addresses. The following structure is used to make a
2624 * set primary request:
2625 */
2626static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2627 int optlen)
2628{
2629 struct sctp_sock *sp;
2630 struct sctp_endpoint *ep;
2631 struct sctp_association *asoc = NULL;
2632 struct sctp_setpeerprim prim;
2633 struct sctp_chunk *chunk;
2634 int err;
2635
2636 sp = sctp_sk(sk);
2637 ep = sp->ep;
2638
2639 if (!sctp_addip_enable)
2640 return -EPERM;
2641
2642 if (optlen != sizeof(struct sctp_setpeerprim))
2643 return -EINVAL;
2644
2645 if (copy_from_user(&prim, optval, optlen))
2646 return -EFAULT;
2647
2648 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2649 if (!asoc)
2650 return -EINVAL;
2651
2652 if (!asoc->peer.asconf_capable)
2653 return -EPERM;
2654
2655 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2656 return -EPERM;
2657
2658 if (!sctp_state(asoc, ESTABLISHED))
2659 return -ENOTCONN;
2660
2661 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2662 return -EADDRNOTAVAIL;
2663
2664 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2665 chunk = sctp_make_asconf_set_prim(asoc,
2666 (union sctp_addr *)&prim.sspp_addr);
2667 if (!chunk)
2668 return -ENOMEM;
2669
2670 err = sctp_send_asconf(asoc, chunk);
2671
2672 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2673
2674 return err;
2675}
2676
2677static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2678 int optlen)
2679{
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002680 struct sctp_setadaption adaption;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002682 if (optlen != sizeof(struct sctp_setadaption))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return -EINVAL;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002684 if (copy_from_user(&adaption, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return -EFAULT;
2686
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002687 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 return 0;
2690}
2691
2692/* API 6.2 setsockopt(), getsockopt()
2693 *
2694 * Applications use setsockopt() and getsockopt() to set or retrieve
2695 * socket options. Socket options are used to change the default
2696 * behavior of sockets calls. They are described in Section 7.
2697 *
2698 * The syntax is:
2699 *
2700 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2701 * int __user *optlen);
2702 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2703 * int optlen);
2704 *
2705 * sd - the socket descript.
2706 * level - set to IPPROTO_SCTP for all SCTP options.
2707 * optname - the option name.
2708 * optval - the buffer to store the value of the option.
2709 * optlen - the size of the buffer.
2710 */
2711SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2712 char __user *optval, int optlen)
2713{
2714 int retval = 0;
2715
2716 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2717 sk, optname);
2718
2719 /* I can hardly begin to describe how wrong this is. This is
2720 * so broken as to be worse than useless. The API draft
2721 * REALLY is NOT helpful here... I am not convinced that the
2722 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2723 * are at all well-founded.
2724 */
2725 if (level != SOL_SCTP) {
2726 struct sctp_af *af = sctp_sk(sk)->pf->af;
2727 retval = af->setsockopt(sk, level, optname, optval, optlen);
2728 goto out_nounlock;
2729 }
2730
2731 sctp_lock_sock(sk);
2732
2733 switch (optname) {
2734 case SCTP_SOCKOPT_BINDX_ADD:
2735 /* 'optlen' is the size of the addresses buffer. */
2736 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2737 optlen, SCTP_BINDX_ADD_ADDR);
2738 break;
2739
2740 case SCTP_SOCKOPT_BINDX_REM:
2741 /* 'optlen' is the size of the addresses buffer. */
2742 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2743 optlen, SCTP_BINDX_REM_ADDR);
2744 break;
2745
Frank Filz3f7a87d2005-06-20 13:14:57 -07002746 case SCTP_SOCKOPT_CONNECTX:
2747 /* 'optlen' is the size of the addresses buffer. */
2748 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2749 optlen);
2750 break;
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 case SCTP_DISABLE_FRAGMENTS:
2753 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2754 break;
2755
2756 case SCTP_EVENTS:
2757 retval = sctp_setsockopt_events(sk, optval, optlen);
2758 break;
2759
2760 case SCTP_AUTOCLOSE:
2761 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2762 break;
2763
2764 case SCTP_PEER_ADDR_PARAMS:
2765 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2766 break;
2767
Frank Filz77086102005-12-22 11:37:30 -08002768 case SCTP_DELAYED_ACK_TIME:
2769 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2770 break;
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 case SCTP_INITMSG:
2773 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2774 break;
2775 case SCTP_DEFAULT_SEND_PARAM:
2776 retval = sctp_setsockopt_default_send_param(sk, optval,
2777 optlen);
2778 break;
2779 case SCTP_PRIMARY_ADDR:
2780 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2781 break;
2782 case SCTP_SET_PEER_PRIMARY_ADDR:
2783 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2784 break;
2785 case SCTP_NODELAY:
2786 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2787 break;
2788 case SCTP_RTOINFO:
2789 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2790 break;
2791 case SCTP_ASSOCINFO:
2792 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2793 break;
2794 case SCTP_I_WANT_MAPPED_V4_ADDR:
2795 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2796 break;
2797 case SCTP_MAXSEG:
2798 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2799 break;
2800 case SCTP_ADAPTION_LAYER:
2801 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2802 break;
2803
2804 default:
2805 retval = -ENOPROTOOPT;
2806 break;
2807 };
2808
2809 sctp_release_sock(sk);
2810
2811out_nounlock:
2812 return retval;
2813}
2814
2815/* API 3.1.6 connect() - UDP Style Syntax
2816 *
2817 * An application may use the connect() call in the UDP model to initiate an
2818 * association without sending data.
2819 *
2820 * The syntax is:
2821 *
2822 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2823 *
2824 * sd: the socket descriptor to have a new association added to.
2825 *
2826 * nam: the address structure (either struct sockaddr_in or struct
2827 * sockaddr_in6 defined in RFC2553 [7]).
2828 *
2829 * len: the size of the address.
2830 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002831SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 int addr_len)
2833{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002835 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 sctp_lock_sock(sk);
2838
Frank Filz3f7a87d2005-06-20 13:14:57 -07002839 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2840 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Frank Filz3f7a87d2005-06-20 13:14:57 -07002842 /* Validate addr_len before calling common connect/connectx routine. */
2843 af = sctp_get_af_specific(addr->sa_family);
2844 if (!af || addr_len < af->sockaddr_len) {
2845 err = -EINVAL;
2846 } else {
2847 /* Pass correct addr len to common routine (so it knows there
2848 * is only one address being passed.
2849 */
2850 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return err;
2855}
2856
2857/* FIXME: Write comments. */
2858SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2859{
2860 return -EOPNOTSUPP; /* STUB */
2861}
2862
2863/* 4.1.4 accept() - TCP Style Syntax
2864 *
2865 * Applications use accept() call to remove an established SCTP
2866 * association from the accept queue of the endpoint. A new socket
2867 * descriptor will be returned from accept() to represent the newly
2868 * formed association.
2869 */
2870SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2871{
2872 struct sctp_sock *sp;
2873 struct sctp_endpoint *ep;
2874 struct sock *newsk = NULL;
2875 struct sctp_association *asoc;
2876 long timeo;
2877 int error = 0;
2878
2879 sctp_lock_sock(sk);
2880
2881 sp = sctp_sk(sk);
2882 ep = sp->ep;
2883
2884 if (!sctp_style(sk, TCP)) {
2885 error = -EOPNOTSUPP;
2886 goto out;
2887 }
2888
2889 if (!sctp_sstate(sk, LISTENING)) {
2890 error = -EINVAL;
2891 goto out;
2892 }
2893
2894 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2895
2896 error = sctp_wait_for_accept(sk, timeo);
2897 if (error)
2898 goto out;
2899
2900 /* We treat the list of associations on the endpoint as the accept
2901 * queue and pick the first association on the list.
2902 */
2903 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2904
2905 newsk = sp->pf->create_accept_sk(sk, asoc);
2906 if (!newsk) {
2907 error = -ENOMEM;
2908 goto out;
2909 }
2910
2911 /* Populate the fields of the newsk from the oldsk and migrate the
2912 * asoc to the newsk.
2913 */
2914 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2915
2916out:
2917 sctp_release_sock(sk);
2918 *err = error;
2919 return newsk;
2920}
2921
2922/* The SCTP ioctl handler. */
2923SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2924{
2925 return -ENOIOCTLCMD;
2926}
2927
2928/* This is the function which gets called during socket creation to
2929 * initialized the SCTP-specific portion of the sock.
2930 * The sock structure should already be zero-filled memory.
2931 */
2932SCTP_STATIC int sctp_init_sock(struct sock *sk)
2933{
2934 struct sctp_endpoint *ep;
2935 struct sctp_sock *sp;
2936
2937 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2938
2939 sp = sctp_sk(sk);
2940
2941 /* Initialize the SCTP per socket area. */
2942 switch (sk->sk_type) {
2943 case SOCK_SEQPACKET:
2944 sp->type = SCTP_SOCKET_UDP;
2945 break;
2946 case SOCK_STREAM:
2947 sp->type = SCTP_SOCKET_TCP;
2948 break;
2949 default:
2950 return -ESOCKTNOSUPPORT;
2951 }
2952
2953 /* Initialize default send parameters. These parameters can be
2954 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2955 */
2956 sp->default_stream = 0;
2957 sp->default_ppid = 0;
2958 sp->default_flags = 0;
2959 sp->default_context = 0;
2960 sp->default_timetolive = 0;
2961
2962 /* Initialize default setup parameters. These parameters
2963 * can be modified with the SCTP_INITMSG socket option or
2964 * overridden by the SCTP_INIT CMSG.
2965 */
2966 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
2967 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
2968 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
2969 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
2970
2971 /* Initialize default RTO related parameters. These parameters can
2972 * be modified for with the SCTP_RTOINFO socket option.
2973 */
2974 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
2975 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
2976 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
2977
2978 /* Initialize default association related parameters. These parameters
2979 * can be modified with the SCTP_ASSOCINFO socket option.
2980 */
2981 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2982 sp->assocparams.sasoc_number_peer_destinations = 0;
2983 sp->assocparams.sasoc_peer_rwnd = 0;
2984 sp->assocparams.sasoc_local_rwnd = 0;
2985 sp->assocparams.sasoc_cookie_life =
2986 jiffies_to_msecs(sctp_valid_cookie_life);
2987
2988 /* Initialize default event subscriptions. By default, all the
2989 * options are off.
2990 */
2991 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2992
2993 /* Default Peer Address Parameters. These defaults can
2994 * be modified via SCTP_PEER_ADDR_PARAMS
2995 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002996 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
2997 sp->pathmaxrxt = sctp_max_retrans_path;
2998 sp->pathmtu = 0; // allow default discovery
Vlad Yasevich8116ffa2006-01-17 11:55:17 -08002999 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003000 sp->param_flags = SPP_HB_ENABLE |
3001 SPP_PMTUD_ENABLE |
3002 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /* If enabled no SCTP message fragmentation will be performed.
3005 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3006 */
3007 sp->disable_fragments = 0;
3008
3009 /* Turn on/off any Nagle-like algorithm. */
3010 sp->nodelay = 1;
3011
3012 /* Enable by default. */
3013 sp->v4mapped = 1;
3014
3015 /* Auto-close idle associations after the configured
3016 * number of seconds. A value of 0 disables this
3017 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3018 * for UDP-style sockets only.
3019 */
3020 sp->autoclose = 0;
3021
3022 /* User specified fragmentation limit. */
3023 sp->user_frag = 0;
3024
3025 sp->adaption_ind = 0;
3026
3027 sp->pf = sctp_get_pf_specific(sk->sk_family);
3028
3029 /* Control variables for partial data delivery. */
3030 sp->pd_mode = 0;
3031 skb_queue_head_init(&sp->pd_lobby);
3032
3033 /* Create a per socket endpoint structure. Even if we
3034 * change the data structure relationships, this may still
3035 * be useful for storing pre-connect address information.
3036 */
3037 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3038 if (!ep)
3039 return -ENOMEM;
3040
3041 sp->ep = ep;
3042 sp->hmac = NULL;
3043
3044 SCTP_DBG_OBJCNT_INC(sock);
3045 return 0;
3046}
3047
3048/* Cleanup any SCTP per socket resources. */
3049SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3050{
3051 struct sctp_endpoint *ep;
3052
3053 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3054
3055 /* Release our hold on the endpoint. */
3056 ep = sctp_sk(sk)->ep;
3057 sctp_endpoint_free(ep);
3058
3059 return 0;
3060}
3061
3062/* API 4.1.7 shutdown() - TCP Style Syntax
3063 * int shutdown(int socket, int how);
3064 *
3065 * sd - the socket descriptor of the association to be closed.
3066 * how - Specifies the type of shutdown. The values are
3067 * as follows:
3068 * SHUT_RD
3069 * Disables further receive operations. No SCTP
3070 * protocol action is taken.
3071 * SHUT_WR
3072 * Disables further send operations, and initiates
3073 * the SCTP shutdown sequence.
3074 * SHUT_RDWR
3075 * Disables further send and receive operations
3076 * and initiates the SCTP shutdown sequence.
3077 */
3078SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3079{
3080 struct sctp_endpoint *ep;
3081 struct sctp_association *asoc;
3082
3083 if (!sctp_style(sk, TCP))
3084 return;
3085
3086 if (how & SEND_SHUTDOWN) {
3087 ep = sctp_sk(sk)->ep;
3088 if (!list_empty(&ep->asocs)) {
3089 asoc = list_entry(ep->asocs.next,
3090 struct sctp_association, asocs);
3091 sctp_primitive_SHUTDOWN(asoc, NULL);
3092 }
3093 }
3094}
3095
3096/* 7.2.1 Association Status (SCTP_STATUS)
3097
3098 * Applications can retrieve current status information about an
3099 * association, including association state, peer receiver window size,
3100 * number of unacked data chunks, and number of data chunks pending
3101 * receipt. This information is read-only.
3102 */
3103static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3104 char __user *optval,
3105 int __user *optlen)
3106{
3107 struct sctp_status status;
3108 struct sctp_association *asoc = NULL;
3109 struct sctp_transport *transport;
3110 sctp_assoc_t associd;
3111 int retval = 0;
3112
3113 if (len != sizeof(status)) {
3114 retval = -EINVAL;
3115 goto out;
3116 }
3117
3118 if (copy_from_user(&status, optval, sizeof(status))) {
3119 retval = -EFAULT;
3120 goto out;
3121 }
3122
3123 associd = status.sstat_assoc_id;
3124 asoc = sctp_id2assoc(sk, associd);
3125 if (!asoc) {
3126 retval = -EINVAL;
3127 goto out;
3128 }
3129
3130 transport = asoc->peer.primary_path;
3131
3132 status.sstat_assoc_id = sctp_assoc2id(asoc);
3133 status.sstat_state = asoc->state;
3134 status.sstat_rwnd = asoc->peer.rwnd;
3135 status.sstat_unackdata = asoc->unack_data;
3136
3137 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3138 status.sstat_instrms = asoc->c.sinit_max_instreams;
3139 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3140 status.sstat_fragmentation_point = asoc->frag_point;
3141 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3142 memcpy(&status.sstat_primary.spinfo_address,
3143 &(transport->ipaddr), sizeof(union sctp_addr));
3144 /* Map ipv4 address into v4-mapped-on-v6 address. */
3145 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3146 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003147 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3149 status.sstat_primary.spinfo_srtt = transport->srtt;
3150 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003151 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Frank Filz3f7a87d2005-06-20 13:14:57 -07003153 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3154 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (put_user(len, optlen)) {
3157 retval = -EFAULT;
3158 goto out;
3159 }
3160
3161 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3162 len, status.sstat_state, status.sstat_rwnd,
3163 status.sstat_assoc_id);
3164
3165 if (copy_to_user(optval, &status, len)) {
3166 retval = -EFAULT;
3167 goto out;
3168 }
3169
3170out:
3171 return (retval);
3172}
3173
3174
3175/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3176 *
3177 * Applications can retrieve information about a specific peer address
3178 * of an association, including its reachability state, congestion
3179 * window, and retransmission timer values. This information is
3180 * read-only.
3181 */
3182static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3183 char __user *optval,
3184 int __user *optlen)
3185{
3186 struct sctp_paddrinfo pinfo;
3187 struct sctp_transport *transport;
3188 int retval = 0;
3189
3190 if (len != sizeof(pinfo)) {
3191 retval = -EINVAL;
3192 goto out;
3193 }
3194
3195 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3196 retval = -EFAULT;
3197 goto out;
3198 }
3199
3200 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3201 pinfo.spinfo_assoc_id);
3202 if (!transport)
3203 return -EINVAL;
3204
3205 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003206 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 pinfo.spinfo_cwnd = transport->cwnd;
3208 pinfo.spinfo_srtt = transport->srtt;
3209 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003210 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Frank Filz3f7a87d2005-06-20 13:14:57 -07003212 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3213 pinfo.spinfo_state = SCTP_ACTIVE;
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (put_user(len, optlen)) {
3216 retval = -EFAULT;
3217 goto out;
3218 }
3219
3220 if (copy_to_user(optval, &pinfo, len)) {
3221 retval = -EFAULT;
3222 goto out;
3223 }
3224
3225out:
3226 return (retval);
3227}
3228
3229/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3230 *
3231 * This option is a on/off flag. If enabled no SCTP message
3232 * fragmentation will be performed. Instead if a message being sent
3233 * exceeds the current PMTU size, the message will NOT be sent and
3234 * instead a error will be indicated to the user.
3235 */
3236static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3237 char __user *optval, int __user *optlen)
3238{
3239 int val;
3240
3241 if (len < sizeof(int))
3242 return -EINVAL;
3243
3244 len = sizeof(int);
3245 val = (sctp_sk(sk)->disable_fragments == 1);
3246 if (put_user(len, optlen))
3247 return -EFAULT;
3248 if (copy_to_user(optval, &val, len))
3249 return -EFAULT;
3250 return 0;
3251}
3252
3253/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3254 *
3255 * This socket option is used to specify various notifications and
3256 * ancillary data the user wishes to receive.
3257 */
3258static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3259 int __user *optlen)
3260{
3261 if (len != sizeof(struct sctp_event_subscribe))
3262 return -EINVAL;
3263 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3264 return -EFAULT;
3265 return 0;
3266}
3267
3268/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3269 *
3270 * This socket option is applicable to the UDP-style socket only. When
3271 * set it will cause associations that are idle for more than the
3272 * specified number of seconds to automatically close. An association
3273 * being idle is defined an association that has NOT sent or received
3274 * user data. The special value of '0' indicates that no automatic
3275 * close of any associations should be performed. The option expects an
3276 * integer defining the number of seconds of idle time before an
3277 * association is closed.
3278 */
3279static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3280{
3281 /* Applicable to UDP-style socket only */
3282 if (sctp_style(sk, TCP))
3283 return -EOPNOTSUPP;
3284 if (len != sizeof(int))
3285 return -EINVAL;
3286 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3287 return -EFAULT;
3288 return 0;
3289}
3290
3291/* Helper routine to branch off an association to a new socket. */
3292SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3293 struct socket **sockp)
3294{
3295 struct sock *sk = asoc->base.sk;
3296 struct socket *sock;
3297 int err = 0;
3298
3299 /* An association cannot be branched off from an already peeled-off
3300 * socket, nor is this supported for tcp style sockets.
3301 */
3302 if (!sctp_style(sk, UDP))
3303 return -EINVAL;
3304
3305 /* Create a new socket. */
3306 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3307 if (err < 0)
3308 return err;
3309
3310 /* Populate the fields of the newsk from the oldsk and migrate the
3311 * asoc to the newsk.
3312 */
3313 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3314 *sockp = sock;
3315
3316 return err;
3317}
3318
3319static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3320{
3321 sctp_peeloff_arg_t peeloff;
3322 struct socket *newsock;
3323 int retval = 0;
3324 struct sctp_association *asoc;
3325
3326 if (len != sizeof(sctp_peeloff_arg_t))
3327 return -EINVAL;
3328 if (copy_from_user(&peeloff, optval, len))
3329 return -EFAULT;
3330
3331 asoc = sctp_id2assoc(sk, peeloff.associd);
3332 if (!asoc) {
3333 retval = -EINVAL;
3334 goto out;
3335 }
3336
3337 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3338
3339 retval = sctp_do_peeloff(asoc, &newsock);
3340 if (retval < 0)
3341 goto out;
3342
3343 /* Map the socket to an unused fd that can be returned to the user. */
3344 retval = sock_map_fd(newsock);
3345 if (retval < 0) {
3346 sock_release(newsock);
3347 goto out;
3348 }
3349
3350 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3351 __FUNCTION__, sk, asoc, newsock->sk, retval);
3352
3353 /* Return the fd mapped to the new socket. */
3354 peeloff.sd = retval;
3355 if (copy_to_user(optval, &peeloff, len))
3356 retval = -EFAULT;
3357
3358out:
3359 return retval;
3360}
3361
3362/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3363 *
3364 * Applications can enable or disable heartbeats for any peer address of
3365 * an association, modify an address's heartbeat interval, force a
3366 * heartbeat to be sent immediately, and adjust the address's maximum
3367 * number of retransmissions sent before an address is considered
3368 * unreachable. The following structure is used to access and modify an
3369 * address's parameters:
3370 *
3371 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003372 * sctp_assoc_t spp_assoc_id;
3373 * struct sockaddr_storage spp_address;
3374 * uint32_t spp_hbinterval;
3375 * uint16_t spp_pathmaxrxt;
3376 * uint32_t spp_pathmtu;
3377 * uint32_t spp_sackdelay;
3378 * uint32_t spp_flags;
3379 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003381 * spp_assoc_id - (one-to-many style socket) This is filled in the
3382 * application, and identifies the association for
3383 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 * spp_address - This specifies which address is of interest.
3385 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003386 * in milliseconds. If a value of zero
3387 * is present in this field then no changes are to
3388 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 * spp_pathmaxrxt - This contains the maximum number of
3390 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003391 * considered unreachable. If a value of zero
3392 * is present in this field then no changes are to
3393 * be made to this parameter.
3394 * spp_pathmtu - When Path MTU discovery is disabled the value
3395 * specified here will be the "fixed" path mtu.
3396 * Note that if the spp_address field is empty
3397 * then all associations on this address will
3398 * have this fixed path mtu set upon them.
3399 *
3400 * spp_sackdelay - When delayed sack is enabled, this value specifies
3401 * the number of milliseconds that sacks will be delayed
3402 * for. This value will apply to all addresses of an
3403 * association if the spp_address field is empty. Note
3404 * also, that if delayed sack is enabled and this
3405 * value is set to 0, no change is made to the last
3406 * recorded delayed sack timer value.
3407 *
3408 * spp_flags - These flags are used to control various features
3409 * on an association. The flag field may contain
3410 * zero or more of the following options.
3411 *
3412 * SPP_HB_ENABLE - Enable heartbeats on the
3413 * specified address. Note that if the address
3414 * field is empty all addresses for the association
3415 * have heartbeats enabled upon them.
3416 *
3417 * SPP_HB_DISABLE - Disable heartbeats on the
3418 * speicifed address. Note that if the address
3419 * field is empty all addresses for the association
3420 * will have their heartbeats disabled. Note also
3421 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3422 * mutually exclusive, only one of these two should
3423 * be specified. Enabling both fields will have
3424 * undetermined results.
3425 *
3426 * SPP_HB_DEMAND - Request a user initiated heartbeat
3427 * to be made immediately.
3428 *
3429 * SPP_PMTUD_ENABLE - This field will enable PMTU
3430 * discovery upon the specified address. Note that
3431 * if the address feild is empty then all addresses
3432 * on the association are effected.
3433 *
3434 * SPP_PMTUD_DISABLE - This field will disable PMTU
3435 * discovery upon the specified address. Note that
3436 * if the address feild is empty then all addresses
3437 * on the association are effected. Not also that
3438 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3439 * exclusive. Enabling both will have undetermined
3440 * results.
3441 *
3442 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3443 * on delayed sack. The time specified in spp_sackdelay
3444 * is used to specify the sack delay for this address. Note
3445 * that if spp_address is empty then all addresses will
3446 * enable delayed sack and take on the sack delay
3447 * value specified in spp_sackdelay.
3448 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3449 * off delayed sack. If the spp_address field is blank then
3450 * delayed sack is disabled for the entire association. Note
3451 * also that this field is mutually exclusive to
3452 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3453 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 */
3455static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003456 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003458 struct sctp_paddrparams params;
3459 struct sctp_transport *trans = NULL;
3460 struct sctp_association *asoc = NULL;
3461 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 if (len != sizeof(struct sctp_paddrparams))
3464 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 if (copy_from_user(&params, optval, len))
3467 return -EFAULT;
3468
Frank Filz52ccb8e2005-12-22 11:36:46 -08003469 /* If an address other than INADDR_ANY is specified, and
3470 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003472 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3473 trans = sctp_addr_id2transport(sk, &params.spp_address,
3474 params.spp_assoc_id);
3475 if (!trans) {
3476 SCTP_DEBUG_PRINTK("Failed no transport\n");
3477 return -EINVAL;
3478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
3480
Frank Filz52ccb8e2005-12-22 11:36:46 -08003481 /* Get association, if assoc_id != 0 and the socket is a one
3482 * to many style socket, and an association was not found, then
3483 * the id was invalid.
3484 */
3485 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3486 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3487 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Frank Filz52ccb8e2005-12-22 11:36:46 -08003491 if (trans) {
3492 /* Fetch transport values. */
3493 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3494 params.spp_pathmtu = trans->pathmtu;
3495 params.spp_pathmaxrxt = trans->pathmaxrxt;
3496 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Frank Filz52ccb8e2005-12-22 11:36:46 -08003498 /*draft-11 doesn't say what to return in spp_flags*/
3499 params.spp_flags = trans->param_flags;
3500 } else if (asoc) {
3501 /* Fetch association values. */
3502 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3503 params.spp_pathmtu = asoc->pathmtu;
3504 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3505 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Frank Filz52ccb8e2005-12-22 11:36:46 -08003507 /*draft-11 doesn't say what to return in spp_flags*/
3508 params.spp_flags = asoc->param_flags;
3509 } else {
3510 /* Fetch socket values. */
3511 params.spp_hbinterval = sp->hbinterval;
3512 params.spp_pathmtu = sp->pathmtu;
3513 params.spp_sackdelay = sp->sackdelay;
3514 params.spp_pathmaxrxt = sp->pathmaxrxt;
3515
3516 /*draft-11 doesn't say what to return in spp_flags*/
3517 params.spp_flags = sp->param_flags;
3518 }
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (copy_to_user(optval, &params, len))
3521 return -EFAULT;
3522
3523 if (put_user(len, optlen))
3524 return -EFAULT;
3525
3526 return 0;
3527}
3528
Frank Filz77086102005-12-22 11:37:30 -08003529/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3530 *
3531 * This options will get or set the delayed ack timer. The time is set
3532 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3533 * endpoints default delayed ack timer value. If the assoc_id field is
3534 * non-zero, then the set or get effects the specified association.
3535 *
3536 * struct sctp_assoc_value {
3537 * sctp_assoc_t assoc_id;
3538 * uint32_t assoc_value;
3539 * };
3540 *
3541 * assoc_id - This parameter, indicates which association the
3542 * user is preforming an action upon. Note that if
3543 * this field's value is zero then the endpoints
3544 * default value is changed (effecting future
3545 * associations only).
3546 *
3547 * assoc_value - This parameter contains the number of milliseconds
3548 * that the user is requesting the delayed ACK timer
3549 * be set to. Note that this value is defined in
3550 * the standard to be between 200 and 500 milliseconds.
3551 *
3552 * Note: a value of zero will leave the value alone,
3553 * but disable SACK delay. A non-zero value will also
3554 * enable SACK delay.
3555 */
3556static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3557 char __user *optval,
3558 int __user *optlen)
3559{
3560 struct sctp_assoc_value params;
3561 struct sctp_association *asoc = NULL;
3562 struct sctp_sock *sp = sctp_sk(sk);
3563
3564 if (len != sizeof(struct sctp_assoc_value))
3565 return - EINVAL;
3566
3567 if (copy_from_user(&params, optval, len))
3568 return -EFAULT;
3569
3570 /* Get association, if assoc_id != 0 and the socket is a one
3571 * to many style socket, and an association was not found, then
3572 * the id was invalid.
3573 */
3574 asoc = sctp_id2assoc(sk, params.assoc_id);
3575 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3576 return -EINVAL;
3577
3578 if (asoc) {
3579 /* Fetch association values. */
3580 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3581 params.assoc_value = jiffies_to_msecs(
3582 asoc->sackdelay);
3583 else
3584 params.assoc_value = 0;
3585 } else {
3586 /* Fetch socket values. */
3587 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3588 params.assoc_value = sp->sackdelay;
3589 else
3590 params.assoc_value = 0;
3591 }
3592
3593 if (copy_to_user(optval, &params, len))
3594 return -EFAULT;
3595
3596 if (put_user(len, optlen))
3597 return -EFAULT;
3598
3599 return 0;
3600}
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3603 *
3604 * Applications can specify protocol parameters for the default association
3605 * initialization. The option name argument to setsockopt() and getsockopt()
3606 * is SCTP_INITMSG.
3607 *
3608 * Setting initialization parameters is effective only on an unconnected
3609 * socket (for UDP-style sockets only future associations are effected
3610 * by the change). With TCP-style sockets, this option is inherited by
3611 * sockets derived from a listener socket.
3612 */
3613static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3614{
3615 if (len != sizeof(struct sctp_initmsg))
3616 return -EINVAL;
3617 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3618 return -EFAULT;
3619 return 0;
3620}
3621
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003622static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3623 char __user *optval,
3624 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 sctp_assoc_t id;
3627 struct sctp_association *asoc;
3628 struct list_head *pos;
3629 int cnt = 0;
3630
3631 if (len != sizeof(sctp_assoc_t))
3632 return -EINVAL;
3633
3634 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3635 return -EFAULT;
3636
3637 /* For UDP-style sockets, id specifies the association to query. */
3638 asoc = sctp_id2assoc(sk, id);
3639 if (!asoc)
3640 return -EINVAL;
3641
3642 list_for_each(pos, &asoc->peer.transport_addr_list) {
3643 cnt ++;
3644 }
3645
3646 return cnt;
3647}
3648
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003649/*
3650 * Old API for getting list of peer addresses. Does not work for 32-bit
3651 * programs running on a 64-bit kernel
3652 */
3653static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3654 char __user *optval,
3655 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 struct sctp_association *asoc;
3658 struct list_head *pos;
3659 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003660 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 struct sctp_transport *from;
3662 void __user *to;
3663 union sctp_addr temp;
3664 struct sctp_sock *sp = sctp_sk(sk);
3665 int addrlen;
3666
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003667 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return -EINVAL;
3669
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003670 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return -EFAULT;
3672
3673 if (getaddrs.addr_num <= 0) return -EINVAL;
3674
3675 /* For UDP-style sockets, id specifies the association to query. */
3676 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3677 if (!asoc)
3678 return -EINVAL;
3679
3680 to = (void __user *)getaddrs.addrs;
3681 list_for_each(pos, &asoc->peer.transport_addr_list) {
3682 from = list_entry(pos, struct sctp_transport, transports);
3683 memcpy(&temp, &from->ipaddr, sizeof(temp));
3684 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3685 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3686 temp.v4.sin_port = htons(temp.v4.sin_port);
3687 if (copy_to_user(to, &temp, addrlen))
3688 return -EFAULT;
3689 to += addrlen ;
3690 cnt ++;
3691 if (cnt >= getaddrs.addr_num) break;
3692 }
3693 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003694 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return -EFAULT;
3696
3697 return 0;
3698}
3699
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003700static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3701 char __user *optval, int __user *optlen)
3702{
3703 struct sctp_association *asoc;
3704 struct list_head *pos;
3705 int cnt = 0;
3706 struct sctp_getaddrs getaddrs;
3707 struct sctp_transport *from;
3708 void __user *to;
3709 union sctp_addr temp;
3710 struct sctp_sock *sp = sctp_sk(sk);
3711 int addrlen;
3712 size_t space_left;
3713 int bytes_copied;
3714
3715 if (len < sizeof(struct sctp_getaddrs))
3716 return -EINVAL;
3717
3718 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3719 return -EFAULT;
3720
3721 /* For UDP-style sockets, id specifies the association to query. */
3722 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3723 if (!asoc)
3724 return -EINVAL;
3725
3726 to = optval + offsetof(struct sctp_getaddrs,addrs);
3727 space_left = len - sizeof(struct sctp_getaddrs) -
3728 offsetof(struct sctp_getaddrs,addrs);
3729
3730 list_for_each(pos, &asoc->peer.transport_addr_list) {
3731 from = list_entry(pos, struct sctp_transport, transports);
3732 memcpy(&temp, &from->ipaddr, sizeof(temp));
3733 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3734 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3735 if(space_left < addrlen)
3736 return -ENOMEM;
3737 temp.v4.sin_port = htons(temp.v4.sin_port);
3738 if (copy_to_user(to, &temp, addrlen))
3739 return -EFAULT;
3740 to += addrlen;
3741 cnt++;
3742 space_left -= addrlen;
3743 }
3744
3745 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3746 return -EFAULT;
3747 bytes_copied = ((char __user *)to) - optval;
3748 if (put_user(bytes_copied, optlen))
3749 return -EFAULT;
3750
3751 return 0;
3752}
3753
3754static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3755 char __user *optval,
3756 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 sctp_assoc_t id;
3759 struct sctp_bind_addr *bp;
3760 struct sctp_association *asoc;
3761 struct list_head *pos;
3762 struct sctp_sockaddr_entry *addr;
3763 rwlock_t *addr_lock;
3764 unsigned long flags;
3765 int cnt = 0;
3766
3767 if (len != sizeof(sctp_assoc_t))
3768 return -EINVAL;
3769
3770 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3771 return -EFAULT;
3772
3773 /*
3774 * For UDP-style sockets, id specifies the association to query.
3775 * If the id field is set to the value '0' then the locally bound
3776 * addresses are returned without regard to any particular
3777 * association.
3778 */
3779 if (0 == id) {
3780 bp = &sctp_sk(sk)->ep->base.bind_addr;
3781 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3782 } else {
3783 asoc = sctp_id2assoc(sk, id);
3784 if (!asoc)
3785 return -EINVAL;
3786 bp = &asoc->base.bind_addr;
3787 addr_lock = &asoc->base.addr_lock;
3788 }
3789
3790 sctp_read_lock(addr_lock);
3791
3792 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3793 * addresses from the global local address list.
3794 */
3795 if (sctp_list_single_entry(&bp->address_list)) {
3796 addr = list_entry(bp->address_list.next,
3797 struct sctp_sockaddr_entry, list);
3798 if (sctp_is_any(&addr->a)) {
3799 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3800 list_for_each(pos, &sctp_local_addr_list) {
3801 addr = list_entry(pos,
3802 struct sctp_sockaddr_entry,
3803 list);
3804 if ((PF_INET == sk->sk_family) &&
3805 (AF_INET6 == addr->a.sa.sa_family))
3806 continue;
3807 cnt++;
3808 }
3809 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3810 flags);
3811 } else {
3812 cnt = 1;
3813 }
3814 goto done;
3815 }
3816
3817 list_for_each(pos, &bp->address_list) {
3818 cnt ++;
3819 }
3820
3821done:
3822 sctp_read_unlock(addr_lock);
3823 return cnt;
3824}
3825
3826/* Helper function that copies local addresses to user and returns the number
3827 * of addresses copied.
3828 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003829static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3830 void __user *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
3832 struct list_head *pos;
3833 struct sctp_sockaddr_entry *addr;
3834 unsigned long flags;
3835 union sctp_addr temp;
3836 int cnt = 0;
3837 int addrlen;
3838
3839 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3840 list_for_each(pos, &sctp_local_addr_list) {
3841 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3842 if ((PF_INET == sk->sk_family) &&
3843 (AF_INET6 == addr->a.sa.sa_family))
3844 continue;
3845 memcpy(&temp, &addr->a, sizeof(temp));
3846 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3847 &temp);
3848 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3849 temp.v4.sin_port = htons(port);
3850 if (copy_to_user(to, &temp, addrlen)) {
3851 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3852 flags);
3853 return -EFAULT;
3854 }
3855 to += addrlen;
3856 cnt ++;
3857 if (cnt >= max_addrs) break;
3858 }
3859 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3860
3861 return cnt;
3862}
3863
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003864static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
Al Virod3a880e2005-12-15 09:18:30 +00003865 void __user **to, size_t space_left)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003866{
3867 struct list_head *pos;
3868 struct sctp_sockaddr_entry *addr;
3869 unsigned long flags;
3870 union sctp_addr temp;
3871 int cnt = 0;
3872 int addrlen;
3873
3874 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3875 list_for_each(pos, &sctp_local_addr_list) {
3876 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3877 if ((PF_INET == sk->sk_family) &&
3878 (AF_INET6 == addr->a.sa.sa_family))
3879 continue;
3880 memcpy(&temp, &addr->a, sizeof(temp));
3881 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3882 &temp);
3883 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3884 if(space_left<addrlen)
3885 return -ENOMEM;
3886 temp.v4.sin_port = htons(port);
3887 if (copy_to_user(*to, &temp, addrlen)) {
3888 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3889 flags);
3890 return -EFAULT;
3891 }
3892 *to += addrlen;
3893 cnt ++;
3894 space_left -= addrlen;
3895 }
3896 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3897
3898 return cnt;
3899}
3900
3901/* Old API for getting list of local addresses. Does not work for 32-bit
3902 * programs running on a 64-bit kernel
3903 */
3904static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3905 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
3907 struct sctp_bind_addr *bp;
3908 struct sctp_association *asoc;
3909 struct list_head *pos;
3910 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003911 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 struct sctp_sockaddr_entry *addr;
3913 void __user *to;
3914 union sctp_addr temp;
3915 struct sctp_sock *sp = sctp_sk(sk);
3916 int addrlen;
3917 rwlock_t *addr_lock;
3918 int err = 0;
3919
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003920 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return -EINVAL;
3922
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003923 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return -EFAULT;
3925
3926 if (getaddrs.addr_num <= 0) return -EINVAL;
3927 /*
3928 * For UDP-style sockets, id specifies the association to query.
3929 * If the id field is set to the value '0' then the locally bound
3930 * addresses are returned without regard to any particular
3931 * association.
3932 */
3933 if (0 == getaddrs.assoc_id) {
3934 bp = &sctp_sk(sk)->ep->base.bind_addr;
3935 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3936 } else {
3937 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3938 if (!asoc)
3939 return -EINVAL;
3940 bp = &asoc->base.bind_addr;
3941 addr_lock = &asoc->base.addr_lock;
3942 }
3943
3944 to = getaddrs.addrs;
3945
3946 sctp_read_lock(addr_lock);
3947
3948 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3949 * addresses from the global local address list.
3950 */
3951 if (sctp_list_single_entry(&bp->address_list)) {
3952 addr = list_entry(bp->address_list.next,
3953 struct sctp_sockaddr_entry, list);
3954 if (sctp_is_any(&addr->a)) {
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003955 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3956 getaddrs.addr_num,
3957 to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (cnt < 0) {
3959 err = cnt;
3960 goto unlock;
3961 }
3962 goto copy_getaddrs;
3963 }
3964 }
3965
3966 list_for_each(pos, &bp->address_list) {
3967 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3968 memcpy(&temp, &addr->a, sizeof(temp));
3969 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3970 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3971 temp.v4.sin_port = htons(temp.v4.sin_port);
3972 if (copy_to_user(to, &temp, addrlen)) {
3973 err = -EFAULT;
3974 goto unlock;
3975 }
3976 to += addrlen;
3977 cnt ++;
3978 if (cnt >= getaddrs.addr_num) break;
3979 }
3980
3981copy_getaddrs:
3982 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003983 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 err = -EFAULT;
3985
3986unlock:
3987 sctp_read_unlock(addr_lock);
3988 return err;
3989}
3990
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003991static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
3992 char __user *optval, int __user *optlen)
3993{
3994 struct sctp_bind_addr *bp;
3995 struct sctp_association *asoc;
3996 struct list_head *pos;
3997 int cnt = 0;
3998 struct sctp_getaddrs getaddrs;
3999 struct sctp_sockaddr_entry *addr;
4000 void __user *to;
4001 union sctp_addr temp;
4002 struct sctp_sock *sp = sctp_sk(sk);
4003 int addrlen;
4004 rwlock_t *addr_lock;
4005 int err = 0;
4006 size_t space_left;
4007 int bytes_copied;
4008
4009 if (len <= sizeof(struct sctp_getaddrs))
4010 return -EINVAL;
4011
4012 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4013 return -EFAULT;
4014
4015 /*
4016 * For UDP-style sockets, id specifies the association to query.
4017 * If the id field is set to the value '0' then the locally bound
4018 * addresses are returned without regard to any particular
4019 * association.
4020 */
4021 if (0 == getaddrs.assoc_id) {
4022 bp = &sctp_sk(sk)->ep->base.bind_addr;
4023 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4024 } else {
4025 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4026 if (!asoc)
4027 return -EINVAL;
4028 bp = &asoc->base.bind_addr;
4029 addr_lock = &asoc->base.addr_lock;
4030 }
4031
4032 to = optval + offsetof(struct sctp_getaddrs,addrs);
4033 space_left = len - sizeof(struct sctp_getaddrs) -
4034 offsetof(struct sctp_getaddrs,addrs);
4035
4036 sctp_read_lock(addr_lock);
4037
4038 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4039 * addresses from the global local address list.
4040 */
4041 if (sctp_list_single_entry(&bp->address_list)) {
4042 addr = list_entry(bp->address_list.next,
4043 struct sctp_sockaddr_entry, list);
4044 if (sctp_is_any(&addr->a)) {
4045 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4046 &to, space_left);
4047 if (cnt < 0) {
4048 err = cnt;
4049 goto unlock;
4050 }
4051 goto copy_getaddrs;
4052 }
4053 }
4054
4055 list_for_each(pos, &bp->address_list) {
4056 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4057 memcpy(&temp, &addr->a, sizeof(temp));
4058 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4059 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4060 if(space_left < addrlen)
4061 return -ENOMEM; /*fixme: right error?*/
4062 temp.v4.sin_port = htons(temp.v4.sin_port);
4063 if (copy_to_user(to, &temp, addrlen)) {
4064 err = -EFAULT;
4065 goto unlock;
4066 }
4067 to += addrlen;
4068 cnt ++;
4069 space_left -= addrlen;
4070 }
4071
4072copy_getaddrs:
4073 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4074 return -EFAULT;
4075 bytes_copied = ((char __user *)to) - optval;
4076 if (put_user(bytes_copied, optlen))
4077 return -EFAULT;
4078
4079unlock:
4080 sctp_read_unlock(addr_lock);
4081 return err;
4082}
4083
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4085 *
4086 * Requests that the local SCTP stack use the enclosed peer address as
4087 * the association primary. The enclosed address must be one of the
4088 * association peer's addresses.
4089 */
4090static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4091 char __user *optval, int __user *optlen)
4092{
4093 struct sctp_prim prim;
4094 struct sctp_association *asoc;
4095 struct sctp_sock *sp = sctp_sk(sk);
4096
4097 if (len != sizeof(struct sctp_prim))
4098 return -EINVAL;
4099
4100 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4101 return -EFAULT;
4102
4103 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4104 if (!asoc)
4105 return -EINVAL;
4106
4107 if (!asoc->peer.primary_path)
4108 return -ENOTCONN;
4109
4110 asoc->peer.primary_path->ipaddr.v4.sin_port =
4111 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4112 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4113 sizeof(union sctp_addr));
4114 asoc->peer.primary_path->ipaddr.v4.sin_port =
4115 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4116
4117 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4118 (union sctp_addr *)&prim.ssp_addr);
4119
4120 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4121 return -EFAULT;
4122
4123 return 0;
4124}
4125
4126/*
4127 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4128 *
4129 * Requests that the local endpoint set the specified Adaption Layer
4130 * Indication parameter for all future INIT and INIT-ACK exchanges.
4131 */
4132static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4133 char __user *optval, int __user *optlen)
4134{
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004135 struct sctp_setadaption adaption;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004137 if (len != sizeof(struct sctp_setadaption))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 return -EINVAL;
4139
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004140 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4141 if (copy_to_user(optval, &adaption, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return 0;
4145}
4146
4147/*
4148 *
4149 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4150 *
4151 * Applications that wish to use the sendto() system call may wish to
4152 * specify a default set of parameters that would normally be supplied
4153 * through the inclusion of ancillary data. This socket option allows
4154 * such an application to set the default sctp_sndrcvinfo structure.
4155
4156
4157 * The application that wishes to use this socket option simply passes
4158 * in to this call the sctp_sndrcvinfo structure defined in Section
4159 * 5.2.2) The input parameters accepted by this call include
4160 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4161 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4162 * to this call if the caller is using the UDP model.
4163 *
4164 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4165 */
4166static int sctp_getsockopt_default_send_param(struct sock *sk,
4167 int len, char __user *optval,
4168 int __user *optlen)
4169{
4170 struct sctp_sndrcvinfo info;
4171 struct sctp_association *asoc;
4172 struct sctp_sock *sp = sctp_sk(sk);
4173
4174 if (len != sizeof(struct sctp_sndrcvinfo))
4175 return -EINVAL;
4176 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4177 return -EFAULT;
4178
4179 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4180 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4181 return -EINVAL;
4182
4183 if (asoc) {
4184 info.sinfo_stream = asoc->default_stream;
4185 info.sinfo_flags = asoc->default_flags;
4186 info.sinfo_ppid = asoc->default_ppid;
4187 info.sinfo_context = asoc->default_context;
4188 info.sinfo_timetolive = asoc->default_timetolive;
4189 } else {
4190 info.sinfo_stream = sp->default_stream;
4191 info.sinfo_flags = sp->default_flags;
4192 info.sinfo_ppid = sp->default_ppid;
4193 info.sinfo_context = sp->default_context;
4194 info.sinfo_timetolive = sp->default_timetolive;
4195 }
4196
4197 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4198 return -EFAULT;
4199
4200 return 0;
4201}
4202
4203/*
4204 *
4205 * 7.1.5 SCTP_NODELAY
4206 *
4207 * Turn on/off any Nagle-like algorithm. This means that packets are
4208 * generally sent as soon as possible and no unnecessary delays are
4209 * introduced, at the cost of more packets in the network. Expects an
4210 * integer boolean flag.
4211 */
4212
4213static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4214 char __user *optval, int __user *optlen)
4215{
4216 int val;
4217
4218 if (len < sizeof(int))
4219 return -EINVAL;
4220
4221 len = sizeof(int);
4222 val = (sctp_sk(sk)->nodelay == 1);
4223 if (put_user(len, optlen))
4224 return -EFAULT;
4225 if (copy_to_user(optval, &val, len))
4226 return -EFAULT;
4227 return 0;
4228}
4229
4230/*
4231 *
4232 * 7.1.1 SCTP_RTOINFO
4233 *
4234 * The protocol parameters used to initialize and bound retransmission
4235 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4236 * and modify these parameters.
4237 * All parameters are time values, in milliseconds. A value of 0, when
4238 * modifying the parameters, indicates that the current value should not
4239 * be changed.
4240 *
4241 */
4242static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4243 char __user *optval,
4244 int __user *optlen) {
4245 struct sctp_rtoinfo rtoinfo;
4246 struct sctp_association *asoc;
4247
4248 if (len != sizeof (struct sctp_rtoinfo))
4249 return -EINVAL;
4250
4251 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4252 return -EFAULT;
4253
4254 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4255
4256 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4257 return -EINVAL;
4258
4259 /* Values corresponding to the specific association. */
4260 if (asoc) {
4261 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4262 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4263 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4264 } else {
4265 /* Values corresponding to the endpoint. */
4266 struct sctp_sock *sp = sctp_sk(sk);
4267
4268 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4269 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4270 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4271 }
4272
4273 if (put_user(len, optlen))
4274 return -EFAULT;
4275
4276 if (copy_to_user(optval, &rtoinfo, len))
4277 return -EFAULT;
4278
4279 return 0;
4280}
4281
4282/*
4283 *
4284 * 7.1.2 SCTP_ASSOCINFO
4285 *
4286 * This option is used to tune the the maximum retransmission attempts
4287 * of the association.
4288 * Returns an error if the new association retransmission value is
4289 * greater than the sum of the retransmission value of the peer.
4290 * See [SCTP] for more information.
4291 *
4292 */
4293static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4294 char __user *optval,
4295 int __user *optlen)
4296{
4297
4298 struct sctp_assocparams assocparams;
4299 struct sctp_association *asoc;
4300 struct list_head *pos;
4301 int cnt = 0;
4302
4303 if (len != sizeof (struct sctp_assocparams))
4304 return -EINVAL;
4305
4306 if (copy_from_user(&assocparams, optval,
4307 sizeof (struct sctp_assocparams)))
4308 return -EFAULT;
4309
4310 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4311
4312 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4313 return -EINVAL;
4314
4315 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004316 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4318 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4319 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4320 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4321 * 1000) +
4322 (asoc->cookie_life.tv_usec
4323 / 1000);
4324
4325 list_for_each(pos, &asoc->peer.transport_addr_list) {
4326 cnt ++;
4327 }
4328
4329 assocparams.sasoc_number_peer_destinations = cnt;
4330 } else {
4331 /* Values corresponding to the endpoint */
4332 struct sctp_sock *sp = sctp_sk(sk);
4333
4334 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4335 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4336 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4337 assocparams.sasoc_cookie_life =
4338 sp->assocparams.sasoc_cookie_life;
4339 assocparams.sasoc_number_peer_destinations =
4340 sp->assocparams.
4341 sasoc_number_peer_destinations;
4342 }
4343
4344 if (put_user(len, optlen))
4345 return -EFAULT;
4346
4347 if (copy_to_user(optval, &assocparams, len))
4348 return -EFAULT;
4349
4350 return 0;
4351}
4352
4353/*
4354 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4355 *
4356 * This socket option is a boolean flag which turns on or off mapped V4
4357 * addresses. If this option is turned on and the socket is type
4358 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4359 * If this option is turned off, then no mapping will be done of V4
4360 * addresses and a user will receive both PF_INET6 and PF_INET type
4361 * addresses on the socket.
4362 */
4363static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4364 char __user *optval, int __user *optlen)
4365{
4366 int val;
4367 struct sctp_sock *sp = sctp_sk(sk);
4368
4369 if (len < sizeof(int))
4370 return -EINVAL;
4371
4372 len = sizeof(int);
4373 val = sp->v4mapped;
4374 if (put_user(len, optlen))
4375 return -EFAULT;
4376 if (copy_to_user(optval, &val, len))
4377 return -EFAULT;
4378
4379 return 0;
4380}
4381
4382/*
4383 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4384 *
4385 * This socket option specifies the maximum size to put in any outgoing
4386 * SCTP chunk. If a message is larger than this size it will be
4387 * fragmented by SCTP into the specified size. Note that the underlying
4388 * SCTP implementation may fragment into smaller sized chunks when the
4389 * PMTU of the underlying association is smaller than the value set by
4390 * the user.
4391 */
4392static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4393 char __user *optval, int __user *optlen)
4394{
4395 int val;
4396
4397 if (len < sizeof(int))
4398 return -EINVAL;
4399
4400 len = sizeof(int);
4401
4402 val = sctp_sk(sk)->user_frag;
4403 if (put_user(len, optlen))
4404 return -EFAULT;
4405 if (copy_to_user(optval, &val, len))
4406 return -EFAULT;
4407
4408 return 0;
4409}
4410
4411SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4412 char __user *optval, int __user *optlen)
4413{
4414 int retval = 0;
4415 int len;
4416
Frank Filz3f7a87d2005-06-20 13:14:57 -07004417 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4418 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 /* I can hardly begin to describe how wrong this is. This is
4421 * so broken as to be worse than useless. The API draft
4422 * REALLY is NOT helpful here... I am not convinced that the
4423 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4424 * are at all well-founded.
4425 */
4426 if (level != SOL_SCTP) {
4427 struct sctp_af *af = sctp_sk(sk)->pf->af;
4428
4429 retval = af->getsockopt(sk, level, optname, optval, optlen);
4430 return retval;
4431 }
4432
4433 if (get_user(len, optlen))
4434 return -EFAULT;
4435
4436 sctp_lock_sock(sk);
4437
4438 switch (optname) {
4439 case SCTP_STATUS:
4440 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4441 break;
4442 case SCTP_DISABLE_FRAGMENTS:
4443 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4444 optlen);
4445 break;
4446 case SCTP_EVENTS:
4447 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4448 break;
4449 case SCTP_AUTOCLOSE:
4450 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4451 break;
4452 case SCTP_SOCKOPT_PEELOFF:
4453 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4454 break;
4455 case SCTP_PEER_ADDR_PARAMS:
4456 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4457 optlen);
4458 break;
Frank Filz77086102005-12-22 11:37:30 -08004459 case SCTP_DELAYED_ACK_TIME:
4460 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4461 optlen);
4462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 case SCTP_INITMSG:
4464 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4465 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004466 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4467 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4468 optlen);
4469 break;
4470 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4471 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4472 optlen);
4473 break;
4474 case SCTP_GET_PEER_ADDRS_OLD:
4475 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 optlen);
4477 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004478 case SCTP_GET_LOCAL_ADDRS_OLD:
4479 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 optlen);
4481 break;
4482 case SCTP_GET_PEER_ADDRS:
4483 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4484 optlen);
4485 break;
4486 case SCTP_GET_LOCAL_ADDRS:
4487 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4488 optlen);
4489 break;
4490 case SCTP_DEFAULT_SEND_PARAM:
4491 retval = sctp_getsockopt_default_send_param(sk, len,
4492 optval, optlen);
4493 break;
4494 case SCTP_PRIMARY_ADDR:
4495 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4496 break;
4497 case SCTP_NODELAY:
4498 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4499 break;
4500 case SCTP_RTOINFO:
4501 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4502 break;
4503 case SCTP_ASSOCINFO:
4504 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4505 break;
4506 case SCTP_I_WANT_MAPPED_V4_ADDR:
4507 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4508 break;
4509 case SCTP_MAXSEG:
4510 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4511 break;
4512 case SCTP_GET_PEER_ADDR_INFO:
4513 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4514 optlen);
4515 break;
4516 case SCTP_ADAPTION_LAYER:
4517 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4518 optlen);
4519 break;
4520 default:
4521 retval = -ENOPROTOOPT;
4522 break;
4523 };
4524
4525 sctp_release_sock(sk);
4526 return retval;
4527}
4528
4529static void sctp_hash(struct sock *sk)
4530{
4531 /* STUB */
4532}
4533
4534static void sctp_unhash(struct sock *sk)
4535{
4536 /* STUB */
4537}
4538
4539/* Check if port is acceptable. Possibly find first available port.
4540 *
4541 * The port hash table (contained in the 'global' SCTP protocol storage
4542 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4543 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4544 * list (the list number is the port number hashed out, so as you
4545 * would expect from a hash function, all the ports in a given list have
4546 * such a number that hashes out to the same list number; you were
4547 * expecting that, right?); so each list has a set of ports, with a
4548 * link to the socket (struct sock) that uses it, the port number and
4549 * a fastreuse flag (FIXME: NPI ipg).
4550 */
4551static struct sctp_bind_bucket *sctp_bucket_create(
4552 struct sctp_bind_hashbucket *head, unsigned short snum);
4553
4554static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4555{
4556 struct sctp_bind_hashbucket *head; /* hash list */
4557 struct sctp_bind_bucket *pp; /* hash list port iterator */
4558 unsigned short snum;
4559 int ret;
4560
4561 /* NOTE: Remember to put this back to net order. */
4562 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4563 snum = addr->v4.sin_port;
4564
4565 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4566 sctp_local_bh_disable();
4567
4568 if (snum == 0) {
4569 /* Search for an available port.
4570 *
4571 * 'sctp_port_rover' was the last port assigned, so
4572 * we start to search from 'sctp_port_rover +
4573 * 1'. What we do is first check if port 'rover' is
4574 * already in the hash table; if not, we use that; if
4575 * it is, we try next.
4576 */
4577 int low = sysctl_local_port_range[0];
4578 int high = sysctl_local_port_range[1];
4579 int remaining = (high - low) + 1;
4580 int rover;
4581 int index;
4582
4583 sctp_spin_lock(&sctp_port_alloc_lock);
4584 rover = sctp_port_rover;
4585 do {
4586 rover++;
4587 if ((rover < low) || (rover > high))
4588 rover = low;
4589 index = sctp_phashfn(rover);
4590 head = &sctp_port_hashtable[index];
4591 sctp_spin_lock(&head->lock);
4592 for (pp = head->chain; pp; pp = pp->next)
4593 if (pp->port == rover)
4594 goto next;
4595 break;
4596 next:
4597 sctp_spin_unlock(&head->lock);
4598 } while (--remaining > 0);
4599 sctp_port_rover = rover;
4600 sctp_spin_unlock(&sctp_port_alloc_lock);
4601
4602 /* Exhausted local port range during search? */
4603 ret = 1;
4604 if (remaining <= 0)
4605 goto fail;
4606
4607 /* OK, here is the one we will use. HEAD (the port
4608 * hash table list entry) is non-NULL and we hold it's
4609 * mutex.
4610 */
4611 snum = rover;
4612 } else {
4613 /* We are given an specific port number; we verify
4614 * that it is not being used. If it is used, we will
4615 * exahust the search in the hash list corresponding
4616 * to the port number (snum) - we detect that with the
4617 * port iterator, pp being NULL.
4618 */
4619 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4620 sctp_spin_lock(&head->lock);
4621 for (pp = head->chain; pp; pp = pp->next) {
4622 if (pp->port == snum)
4623 goto pp_found;
4624 }
4625 }
4626 pp = NULL;
4627 goto pp_not_found;
4628pp_found:
4629 if (!hlist_empty(&pp->owner)) {
4630 /* We had a port hash table hit - there is an
4631 * available port (pp != NULL) and it is being
4632 * used by other socket (pp->owner not empty); that other
4633 * socket is going to be sk2.
4634 */
4635 int reuse = sk->sk_reuse;
4636 struct sock *sk2;
4637 struct hlist_node *node;
4638
4639 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4640 if (pp->fastreuse && sk->sk_reuse)
4641 goto success;
4642
4643 /* Run through the list of sockets bound to the port
4644 * (pp->port) [via the pointers bind_next and
4645 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4646 * we get the endpoint they describe and run through
4647 * the endpoint's list of IP (v4 or v6) addresses,
4648 * comparing each of the addresses with the address of
4649 * the socket sk. If we find a match, then that means
4650 * that this port/socket (sk) combination are already
4651 * in an endpoint.
4652 */
4653 sk_for_each_bound(sk2, node, &pp->owner) {
4654 struct sctp_endpoint *ep2;
4655 ep2 = sctp_sk(sk2)->ep;
4656
4657 if (reuse && sk2->sk_reuse)
4658 continue;
4659
4660 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4661 sctp_sk(sk))) {
4662 ret = (long)sk2;
4663 goto fail_unlock;
4664 }
4665 }
4666 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4667 }
4668pp_not_found:
4669 /* If there was a hash table miss, create a new port. */
4670 ret = 1;
4671 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4672 goto fail_unlock;
4673
4674 /* In either case (hit or miss), make sure fastreuse is 1 only
4675 * if sk->sk_reuse is too (that is, if the caller requested
4676 * SO_REUSEADDR on this socket -sk-).
4677 */
4678 if (hlist_empty(&pp->owner))
4679 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4680 else if (pp->fastreuse && !sk->sk_reuse)
4681 pp->fastreuse = 0;
4682
4683 /* We are set, so fill up all the data in the hash table
4684 * entry, tie the socket list information with the rest of the
4685 * sockets FIXME: Blurry, NPI (ipg).
4686 */
4687success:
4688 inet_sk(sk)->num = snum;
4689 if (!sctp_sk(sk)->bind_hash) {
4690 sk_add_bind_node(sk, &pp->owner);
4691 sctp_sk(sk)->bind_hash = pp;
4692 }
4693 ret = 0;
4694
4695fail_unlock:
4696 sctp_spin_unlock(&head->lock);
4697
4698fail:
4699 sctp_local_bh_enable();
4700 addr->v4.sin_port = htons(addr->v4.sin_port);
4701 return ret;
4702}
4703
4704/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4705 * port is requested.
4706 */
4707static int sctp_get_port(struct sock *sk, unsigned short snum)
4708{
4709 long ret;
4710 union sctp_addr addr;
4711 struct sctp_af *af = sctp_sk(sk)->pf->af;
4712
4713 /* Set up a dummy address struct from the sk. */
4714 af->from_sk(&addr, sk);
4715 addr.v4.sin_port = htons(snum);
4716
4717 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4718 ret = sctp_get_port_local(sk, &addr);
4719
4720 return (ret ? 1 : 0);
4721}
4722
4723/*
4724 * 3.1.3 listen() - UDP Style Syntax
4725 *
4726 * By default, new associations are not accepted for UDP style sockets.
4727 * An application uses listen() to mark a socket as being able to
4728 * accept new associations.
4729 */
4730SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4731{
4732 struct sctp_sock *sp = sctp_sk(sk);
4733 struct sctp_endpoint *ep = sp->ep;
4734
4735 /* Only UDP style sockets that are not peeled off are allowed to
4736 * listen().
4737 */
4738 if (!sctp_style(sk, UDP))
4739 return -EINVAL;
4740
4741 /* If backlog is zero, disable listening. */
4742 if (!backlog) {
4743 if (sctp_sstate(sk, CLOSED))
4744 return 0;
4745
4746 sctp_unhash_endpoint(ep);
4747 sk->sk_state = SCTP_SS_CLOSED;
4748 }
4749
4750 /* Return if we are already listening. */
4751 if (sctp_sstate(sk, LISTENING))
4752 return 0;
4753
4754 /*
4755 * If a bind() or sctp_bindx() is not called prior to a listen()
4756 * call that allows new associations to be accepted, the system
4757 * picks an ephemeral port and will choose an address set equivalent
4758 * to binding with a wildcard address.
4759 *
4760 * This is not currently spelled out in the SCTP sockets
4761 * extensions draft, but follows the practice as seen in TCP
4762 * sockets.
4763 */
4764 if (!ep->base.bind_addr.port) {
4765 if (sctp_autobind(sk))
4766 return -EAGAIN;
4767 }
4768 sk->sk_state = SCTP_SS_LISTENING;
4769 sctp_hash_endpoint(ep);
4770 return 0;
4771}
4772
4773/*
4774 * 4.1.3 listen() - TCP Style Syntax
4775 *
4776 * Applications uses listen() to ready the SCTP endpoint for accepting
4777 * inbound associations.
4778 */
4779SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4780{
4781 struct sctp_sock *sp = sctp_sk(sk);
4782 struct sctp_endpoint *ep = sp->ep;
4783
4784 /* If backlog is zero, disable listening. */
4785 if (!backlog) {
4786 if (sctp_sstate(sk, CLOSED))
4787 return 0;
4788
4789 sctp_unhash_endpoint(ep);
4790 sk->sk_state = SCTP_SS_CLOSED;
4791 }
4792
4793 if (sctp_sstate(sk, LISTENING))
4794 return 0;
4795
4796 /*
4797 * If a bind() or sctp_bindx() is not called prior to a listen()
4798 * call that allows new associations to be accepted, the system
4799 * picks an ephemeral port and will choose an address set equivalent
4800 * to binding with a wildcard address.
4801 *
4802 * This is not currently spelled out in the SCTP sockets
4803 * extensions draft, but follows the practice as seen in TCP
4804 * sockets.
4805 */
4806 if (!ep->base.bind_addr.port) {
4807 if (sctp_autobind(sk))
4808 return -EAGAIN;
4809 }
4810 sk->sk_state = SCTP_SS_LISTENING;
4811 sk->sk_max_ack_backlog = backlog;
4812 sctp_hash_endpoint(ep);
4813 return 0;
4814}
4815
4816/*
4817 * Move a socket to LISTENING state.
4818 */
4819int sctp_inet_listen(struct socket *sock, int backlog)
4820{
4821 struct sock *sk = sock->sk;
4822 struct crypto_tfm *tfm=NULL;
4823 int err = -EINVAL;
4824
4825 if (unlikely(backlog < 0))
4826 goto out;
4827
4828 sctp_lock_sock(sk);
4829
4830 if (sock->state != SS_UNCONNECTED)
4831 goto out;
4832
4833 /* Allocate HMAC for generating cookie. */
4834 if (sctp_hmac_alg) {
4835 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4836 if (!tfm) {
4837 err = -ENOSYS;
4838 goto out;
4839 }
4840 }
4841
4842 switch (sock->type) {
4843 case SOCK_SEQPACKET:
4844 err = sctp_seqpacket_listen(sk, backlog);
4845 break;
4846 case SOCK_STREAM:
4847 err = sctp_stream_listen(sk, backlog);
4848 break;
4849 default:
4850 break;
4851 };
4852 if (err)
4853 goto cleanup;
4854
4855 /* Store away the transform reference. */
4856 sctp_sk(sk)->hmac = tfm;
4857out:
4858 sctp_release_sock(sk);
4859 return err;
4860cleanup:
Jesper Juhl573dbd92005-09-01 17:44:29 -07004861 sctp_crypto_free_tfm(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 goto out;
4863}
4864
4865/*
4866 * This function is done by modeling the current datagram_poll() and the
4867 * tcp_poll(). Note that, based on these implementations, we don't
4868 * lock the socket in this function, even though it seems that,
4869 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08004870 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 * in this place. We assume that we don't need locks either until proven
4872 * otherwise.
4873 *
4874 * Another thing to note is that we include the Async I/O support
4875 * here, again, by modeling the current TCP/UDP code. We don't have
4876 * a good way to test with it yet.
4877 */
4878unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4879{
4880 struct sock *sk = sock->sk;
4881 struct sctp_sock *sp = sctp_sk(sk);
4882 unsigned int mask;
4883
4884 poll_wait(file, sk->sk_sleep, wait);
4885
4886 /* A TCP-style listening socket becomes readable when the accept queue
4887 * is not empty.
4888 */
4889 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4890 return (!list_empty(&sp->ep->asocs)) ?
4891 (POLLIN | POLLRDNORM) : 0;
4892
4893 mask = 0;
4894
4895 /* Is there any exceptional events? */
4896 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4897 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08004898 if (sk->sk_shutdown & RCV_SHUTDOWN)
4899 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if (sk->sk_shutdown == SHUTDOWN_MASK)
4901 mask |= POLLHUP;
4902
4903 /* Is it readable? Reconsider this code with TCP-style support. */
4904 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4905 (sk->sk_shutdown & RCV_SHUTDOWN))
4906 mask |= POLLIN | POLLRDNORM;
4907
4908 /* The association is either gone or not ready. */
4909 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4910 return mask;
4911
4912 /* Is it writable? */
4913 if (sctp_writeable(sk)) {
4914 mask |= POLLOUT | POLLWRNORM;
4915 } else {
4916 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4917 /*
4918 * Since the socket is not locked, the buffer
4919 * might be made available after the writeable check and
4920 * before the bit is set. This could cause a lost I/O
4921 * signal. tcp_poll() has a race breaker for this race
4922 * condition. Based on their implementation, we put
4923 * in the following code to cover it as well.
4924 */
4925 if (sctp_writeable(sk))
4926 mask |= POLLOUT | POLLWRNORM;
4927 }
4928 return mask;
4929}
4930
4931/********************************************************************
4932 * 2nd Level Abstractions
4933 ********************************************************************/
4934
4935static struct sctp_bind_bucket *sctp_bucket_create(
4936 struct sctp_bind_hashbucket *head, unsigned short snum)
4937{
4938 struct sctp_bind_bucket *pp;
4939
4940 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4941 SCTP_DBG_OBJCNT_INC(bind_bucket);
4942 if (pp) {
4943 pp->port = snum;
4944 pp->fastreuse = 0;
4945 INIT_HLIST_HEAD(&pp->owner);
4946 if ((pp->next = head->chain) != NULL)
4947 pp->next->pprev = &pp->next;
4948 head->chain = pp;
4949 pp->pprev = &head->chain;
4950 }
4951 return pp;
4952}
4953
4954/* Caller must hold hashbucket lock for this tb with local BH disabled */
4955static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4956{
4957 if (hlist_empty(&pp->owner)) {
4958 if (pp->next)
4959 pp->next->pprev = pp->pprev;
4960 *(pp->pprev) = pp->next;
4961 kmem_cache_free(sctp_bucket_cachep, pp);
4962 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4963 }
4964}
4965
4966/* Release this socket's reference to a local port. */
4967static inline void __sctp_put_port(struct sock *sk)
4968{
4969 struct sctp_bind_hashbucket *head =
4970 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
4971 struct sctp_bind_bucket *pp;
4972
4973 sctp_spin_lock(&head->lock);
4974 pp = sctp_sk(sk)->bind_hash;
4975 __sk_del_bind_node(sk);
4976 sctp_sk(sk)->bind_hash = NULL;
4977 inet_sk(sk)->num = 0;
4978 sctp_bucket_destroy(pp);
4979 sctp_spin_unlock(&head->lock);
4980}
4981
4982void sctp_put_port(struct sock *sk)
4983{
4984 sctp_local_bh_disable();
4985 __sctp_put_port(sk);
4986 sctp_local_bh_enable();
4987}
4988
4989/*
4990 * The system picks an ephemeral port and choose an address set equivalent
4991 * to binding with a wildcard address.
4992 * One of those addresses will be the primary address for the association.
4993 * This automatically enables the multihoming capability of SCTP.
4994 */
4995static int sctp_autobind(struct sock *sk)
4996{
4997 union sctp_addr autoaddr;
4998 struct sctp_af *af;
4999 unsigned short port;
5000
5001 /* Initialize a local sockaddr structure to INADDR_ANY. */
5002 af = sctp_sk(sk)->pf->af;
5003
5004 port = htons(inet_sk(sk)->num);
5005 af->inaddr_any(&autoaddr, port);
5006
5007 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5008}
5009
5010/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5011 *
5012 * From RFC 2292
5013 * 4.2 The cmsghdr Structure *
5014 *
5015 * When ancillary data is sent or received, any number of ancillary data
5016 * objects can be specified by the msg_control and msg_controllen members of
5017 * the msghdr structure, because each object is preceded by
5018 * a cmsghdr structure defining the object's length (the cmsg_len member).
5019 * Historically Berkeley-derived implementations have passed only one object
5020 * at a time, but this API allows multiple objects to be
5021 * passed in a single call to sendmsg() or recvmsg(). The following example
5022 * shows two ancillary data objects in a control buffer.
5023 *
5024 * |<--------------------------- msg_controllen -------------------------->|
5025 * | |
5026 *
5027 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5028 *
5029 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5030 * | | |
5031 *
5032 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5033 *
5034 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5035 * | | | | |
5036 *
5037 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5038 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5039 *
5040 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5041 *
5042 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5043 * ^
5044 * |
5045 *
5046 * msg_control
5047 * points here
5048 */
5049SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5050 sctp_cmsgs_t *cmsgs)
5051{
5052 struct cmsghdr *cmsg;
5053
5054 for (cmsg = CMSG_FIRSTHDR(msg);
5055 cmsg != NULL;
5056 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5057 if (!CMSG_OK(msg, cmsg))
5058 return -EINVAL;
5059
5060 /* Should we parse this header or ignore? */
5061 if (cmsg->cmsg_level != IPPROTO_SCTP)
5062 continue;
5063
5064 /* Strictly check lengths following example in SCM code. */
5065 switch (cmsg->cmsg_type) {
5066 case SCTP_INIT:
5067 /* SCTP Socket API Extension
5068 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5069 *
5070 * This cmsghdr structure provides information for
5071 * initializing new SCTP associations with sendmsg().
5072 * The SCTP_INITMSG socket option uses this same data
5073 * structure. This structure is not used for
5074 * recvmsg().
5075 *
5076 * cmsg_level cmsg_type cmsg_data[]
5077 * ------------ ------------ ----------------------
5078 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5079 */
5080 if (cmsg->cmsg_len !=
5081 CMSG_LEN(sizeof(struct sctp_initmsg)))
5082 return -EINVAL;
5083 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5084 break;
5085
5086 case SCTP_SNDRCV:
5087 /* SCTP Socket API Extension
5088 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5089 *
5090 * This cmsghdr structure specifies SCTP options for
5091 * sendmsg() and describes SCTP header information
5092 * about a received message through recvmsg().
5093 *
5094 * cmsg_level cmsg_type cmsg_data[]
5095 * ------------ ------------ ----------------------
5096 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5097 */
5098 if (cmsg->cmsg_len !=
5099 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5100 return -EINVAL;
5101
5102 cmsgs->info =
5103 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5104
5105 /* Minimally, validate the sinfo_flags. */
5106 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005107 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5108 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return -EINVAL;
5110 break;
5111
5112 default:
5113 return -EINVAL;
5114 };
5115 }
5116 return 0;
5117}
5118
5119/*
5120 * Wait for a packet..
5121 * Note: This function is the same function as in core/datagram.c
5122 * with a few modifications to make lksctp work.
5123 */
5124static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5125{
5126 int error;
5127 DEFINE_WAIT(wait);
5128
5129 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5130
5131 /* Socket errors? */
5132 error = sock_error(sk);
5133 if (error)
5134 goto out;
5135
5136 if (!skb_queue_empty(&sk->sk_receive_queue))
5137 goto ready;
5138
5139 /* Socket shut down? */
5140 if (sk->sk_shutdown & RCV_SHUTDOWN)
5141 goto out;
5142
5143 /* Sequenced packets can come disconnected. If so we report the
5144 * problem.
5145 */
5146 error = -ENOTCONN;
5147
5148 /* Is there a good reason to think that we may receive some data? */
5149 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5150 goto out;
5151
5152 /* Handle signals. */
5153 if (signal_pending(current))
5154 goto interrupted;
5155
5156 /* Let another process have a go. Since we are going to sleep
5157 * anyway. Note: This may cause odd behaviors if the message
5158 * does not fit in the user's buffer, but this seems to be the
5159 * only way to honor MSG_DONTWAIT realistically.
5160 */
5161 sctp_release_sock(sk);
5162 *timeo_p = schedule_timeout(*timeo_p);
5163 sctp_lock_sock(sk);
5164
5165ready:
5166 finish_wait(sk->sk_sleep, &wait);
5167 return 0;
5168
5169interrupted:
5170 error = sock_intr_errno(*timeo_p);
5171
5172out:
5173 finish_wait(sk->sk_sleep, &wait);
5174 *err = error;
5175 return error;
5176}
5177
5178/* Receive a datagram.
5179 * Note: This is pretty much the same routine as in core/datagram.c
5180 * with a few changes to make lksctp work.
5181 */
5182static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5183 int noblock, int *err)
5184{
5185 int error;
5186 struct sk_buff *skb;
5187 long timeo;
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 timeo = sock_rcvtimeo(sk, noblock);
5190
5191 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5192 timeo, MAX_SCHEDULE_TIMEOUT);
5193
5194 do {
5195 /* Again only user level code calls this function,
5196 * so nothing interrupt level
5197 * will suddenly eat the receive_queue.
5198 *
5199 * Look at current nfs client by the way...
5200 * However, this function was corrent in any case. 8)
5201 */
5202 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005203 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 skb = skb_peek(&sk->sk_receive_queue);
5205 if (skb)
5206 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005207 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 } else {
5209 skb = skb_dequeue(&sk->sk_receive_queue);
5210 }
5211
5212 if (skb)
5213 return skb;
5214
Neil Horman6736dc32005-12-02 20:30:06 -08005215 /* Caller is allowed not to check sk->sk_err before calling. */
5216 error = sock_error(sk);
5217 if (error)
5218 goto no_packet;
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 if (sk->sk_shutdown & RCV_SHUTDOWN)
5221 break;
5222
5223 /* User doesn't want to wait. */
5224 error = -EAGAIN;
5225 if (!timeo)
5226 goto no_packet;
5227 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5228
5229 return NULL;
5230
5231no_packet:
5232 *err = error;
5233 return NULL;
5234}
5235
5236/* If sndbuf has changed, wake up per association sndbuf waiters. */
5237static void __sctp_write_space(struct sctp_association *asoc)
5238{
5239 struct sock *sk = asoc->base.sk;
5240 struct socket *sock = sk->sk_socket;
5241
5242 if ((sctp_wspace(asoc) > 0) && sock) {
5243 if (waitqueue_active(&asoc->wait))
5244 wake_up_interruptible(&asoc->wait);
5245
5246 if (sctp_writeable(sk)) {
5247 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5248 wake_up_interruptible(sk->sk_sleep);
5249
5250 /* Note that we try to include the Async I/O support
5251 * here by modeling from the current TCP/UDP code.
5252 * We have not tested with it yet.
5253 */
5254 if (sock->fasync_list &&
5255 !(sk->sk_shutdown & SEND_SHUTDOWN))
5256 sock_wake_async(sock, 2, POLL_OUT);
5257 }
5258 }
5259}
5260
5261/* Do accounting for the sndbuf space.
5262 * Decrement the used sndbuf space of the corresponding association by the
5263 * data size which was just transmitted(freed).
5264 */
5265static void sctp_wfree(struct sk_buff *skb)
5266{
5267 struct sctp_association *asoc;
5268 struct sctp_chunk *chunk;
5269 struct sock *sk;
5270
5271 /* Get the saved chunk pointer. */
5272 chunk = *((struct sctp_chunk **)(skb->cb));
5273 asoc = chunk->asoc;
5274 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07005275 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5276 sizeof(struct sk_buff) +
5277 sizeof(struct sctp_chunk);
5278
Neil Horman4eb701d2005-04-28 12:02:04 -07005279 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5280
5281 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 __sctp_write_space(asoc);
5283
5284 sctp_association_put(asoc);
5285}
5286
5287/* Helper function to wait for space in the sndbuf. */
5288static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5289 size_t msg_len)
5290{
5291 struct sock *sk = asoc->base.sk;
5292 int err = 0;
5293 long current_timeo = *timeo_p;
5294 DEFINE_WAIT(wait);
5295
5296 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5297 asoc, (long)(*timeo_p), msg_len);
5298
5299 /* Increment the association's refcnt. */
5300 sctp_association_hold(asoc);
5301
5302 /* Wait on the association specific sndbuf space. */
5303 for (;;) {
5304 prepare_to_wait_exclusive(&asoc->wait, &wait,
5305 TASK_INTERRUPTIBLE);
5306 if (!*timeo_p)
5307 goto do_nonblock;
5308 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5309 asoc->base.dead)
5310 goto do_error;
5311 if (signal_pending(current))
5312 goto do_interrupted;
5313 if (msg_len <= sctp_wspace(asoc))
5314 break;
5315
5316 /* Let another process have a go. Since we are going
5317 * to sleep anyway.
5318 */
5319 sctp_release_sock(sk);
5320 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005321 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 sctp_lock_sock(sk);
5323
5324 *timeo_p = current_timeo;
5325 }
5326
5327out:
5328 finish_wait(&asoc->wait, &wait);
5329
5330 /* Release the association's refcnt. */
5331 sctp_association_put(asoc);
5332
5333 return err;
5334
5335do_error:
5336 err = -EPIPE;
5337 goto out;
5338
5339do_interrupted:
5340 err = sock_intr_errno(*timeo_p);
5341 goto out;
5342
5343do_nonblock:
5344 err = -EAGAIN;
5345 goto out;
5346}
5347
5348/* If socket sndbuf has changed, wake up all per association waiters. */
5349void sctp_write_space(struct sock *sk)
5350{
5351 struct sctp_association *asoc;
5352 struct list_head *pos;
5353
5354 /* Wake up the tasks in each wait queue. */
5355 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5356 asoc = list_entry(pos, struct sctp_association, asocs);
5357 __sctp_write_space(asoc);
5358 }
5359}
5360
5361/* Is there any sndbuf space available on the socket?
5362 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08005363 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 * associations on the same socket. For a UDP-style socket with
5365 * multiple associations, it is possible for it to be "unwriteable"
5366 * prematurely. I assume that this is acceptable because
5367 * a premature "unwriteable" is better than an accidental "writeable" which
5368 * would cause an unwanted block under certain circumstances. For the 1-1
5369 * UDP-style sockets or TCP-style sockets, this code should work.
5370 * - Daisy
5371 */
5372static int sctp_writeable(struct sock *sk)
5373{
5374 int amt = 0;
5375
Neil Horman9bffc4a2005-12-19 14:24:40 -08005376 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 if (amt < 0)
5378 amt = 0;
5379 return amt;
5380}
5381
5382/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5383 * returns immediately with EINPROGRESS.
5384 */
5385static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5386{
5387 struct sock *sk = asoc->base.sk;
5388 int err = 0;
5389 long current_timeo = *timeo_p;
5390 DEFINE_WAIT(wait);
5391
5392 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5393 (long)(*timeo_p));
5394
5395 /* Increment the association's refcnt. */
5396 sctp_association_hold(asoc);
5397
5398 for (;;) {
5399 prepare_to_wait_exclusive(&asoc->wait, &wait,
5400 TASK_INTERRUPTIBLE);
5401 if (!*timeo_p)
5402 goto do_nonblock;
5403 if (sk->sk_shutdown & RCV_SHUTDOWN)
5404 break;
5405 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5406 asoc->base.dead)
5407 goto do_error;
5408 if (signal_pending(current))
5409 goto do_interrupted;
5410
5411 if (sctp_state(asoc, ESTABLISHED))
5412 break;
5413
5414 /* Let another process have a go. Since we are going
5415 * to sleep anyway.
5416 */
5417 sctp_release_sock(sk);
5418 current_timeo = schedule_timeout(current_timeo);
5419 sctp_lock_sock(sk);
5420
5421 *timeo_p = current_timeo;
5422 }
5423
5424out:
5425 finish_wait(&asoc->wait, &wait);
5426
5427 /* Release the association's refcnt. */
5428 sctp_association_put(asoc);
5429
5430 return err;
5431
5432do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08005433 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 err = -ETIMEDOUT;
5435 else
5436 err = -ECONNREFUSED;
5437 goto out;
5438
5439do_interrupted:
5440 err = sock_intr_errno(*timeo_p);
5441 goto out;
5442
5443do_nonblock:
5444 err = -EINPROGRESS;
5445 goto out;
5446}
5447
5448static int sctp_wait_for_accept(struct sock *sk, long timeo)
5449{
5450 struct sctp_endpoint *ep;
5451 int err = 0;
5452 DEFINE_WAIT(wait);
5453
5454 ep = sctp_sk(sk)->ep;
5455
5456
5457 for (;;) {
5458 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5459 TASK_INTERRUPTIBLE);
5460
5461 if (list_empty(&ep->asocs)) {
5462 sctp_release_sock(sk);
5463 timeo = schedule_timeout(timeo);
5464 sctp_lock_sock(sk);
5465 }
5466
5467 err = -EINVAL;
5468 if (!sctp_sstate(sk, LISTENING))
5469 break;
5470
5471 err = 0;
5472 if (!list_empty(&ep->asocs))
5473 break;
5474
5475 err = sock_intr_errno(timeo);
5476 if (signal_pending(current))
5477 break;
5478
5479 err = -EAGAIN;
5480 if (!timeo)
5481 break;
5482 }
5483
5484 finish_wait(sk->sk_sleep, &wait);
5485
5486 return err;
5487}
5488
5489void sctp_wait_for_close(struct sock *sk, long timeout)
5490{
5491 DEFINE_WAIT(wait);
5492
5493 do {
5494 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5495 if (list_empty(&sctp_sk(sk)->ep->asocs))
5496 break;
5497 sctp_release_sock(sk);
5498 timeout = schedule_timeout(timeout);
5499 sctp_lock_sock(sk);
5500 } while (!signal_pending(current) && timeout);
5501
5502 finish_wait(sk->sk_sleep, &wait);
5503}
5504
5505/* Populate the fields of the newsk from the oldsk and migrate the assoc
5506 * and its messages to the newsk.
5507 */
5508static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5509 struct sctp_association *assoc,
5510 sctp_socket_type_t type)
5511{
5512 struct sctp_sock *oldsp = sctp_sk(oldsk);
5513 struct sctp_sock *newsp = sctp_sk(newsk);
5514 struct sctp_bind_bucket *pp; /* hash list port iterator */
5515 struct sctp_endpoint *newep = newsp->ep;
5516 struct sk_buff *skb, *tmp;
5517 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005518 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
5520 /* Migrate socket buffer sizes and all the socket level options to the
5521 * new socket.
5522 */
5523 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5524 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5525 /* Brute force copy old sctp opt. */
5526 inet_sk_copy_descendant(newsk, oldsk);
5527
5528 /* Restore the ep value that was overwritten with the above structure
5529 * copy.
5530 */
5531 newsp->ep = newep;
5532 newsp->hmac = NULL;
5533
5534 /* Hook this new socket in to the bind_hash list. */
5535 pp = sctp_sk(oldsk)->bind_hash;
5536 sk_add_bind_node(newsk, &pp->owner);
5537 sctp_sk(newsk)->bind_hash = pp;
5538 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5539
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005540 /* Copy the bind_addr list from the original endpoint to the new
5541 * endpoint so that we can handle restarts properly
5542 */
5543 if (assoc->peer.ipv4_address)
5544 flags |= SCTP_ADDR4_PEERSUPP;
5545 if (assoc->peer.ipv6_address)
5546 flags |= SCTP_ADDR6_PEERSUPP;
5547 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5548 &oldsp->ep->base.bind_addr,
5549 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 /* Move any messages in the old socket's receive queue that are for the
5552 * peeled off association to the new socket's receive queue.
5553 */
5554 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5555 event = sctp_skb2event(skb);
5556 if (event->asoc == assoc) {
Neil Horman049b3ff2005-11-11 16:08:24 -08005557 sock_rfree(skb);
David S. Miller8728b832005-08-09 19:25:21 -07005558 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Neil Horman049b3ff2005-11-11 16:08:24 -08005560 skb_set_owner_r(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 }
5562 }
5563
5564 /* Clean up any messages pending delivery due to partial
5565 * delivery. Three cases:
5566 * 1) No partial deliver; no work.
5567 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5568 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5569 */
5570 skb_queue_head_init(&newsp->pd_lobby);
5571 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5572
5573 if (sctp_sk(oldsk)->pd_mode) {
5574 struct sk_buff_head *queue;
5575
5576 /* Decide which queue to move pd_lobby skbs to. */
5577 if (assoc->ulpq.pd_mode) {
5578 queue = &newsp->pd_lobby;
5579 } else
5580 queue = &newsk->sk_receive_queue;
5581
5582 /* Walk through the pd_lobby, looking for skbs that
5583 * need moved to the new socket.
5584 */
5585 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5586 event = sctp_skb2event(skb);
5587 if (event->asoc == assoc) {
Neil Horman049b3ff2005-11-11 16:08:24 -08005588 sock_rfree(skb);
David S. Miller8728b832005-08-09 19:25:21 -07005589 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 __skb_queue_tail(queue, skb);
Neil Horman049b3ff2005-11-11 16:08:24 -08005591 skb_set_owner_r(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 }
5593 }
5594
5595 /* Clear up any skbs waiting for the partial
5596 * delivery to finish.
5597 */
5598 if (assoc->ulpq.pd_mode)
5599 sctp_clear_pd(oldsk);
5600
5601 }
5602
5603 /* Set the type of socket to indicate that it is peeled off from the
5604 * original UDP-style socket or created with the accept() call on a
5605 * TCP-style socket..
5606 */
5607 newsp->type = type;
5608
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005609 /* Mark the new socket "in-use" by the user so that any packets
5610 * that may arrive on the association after we've moved it are
5611 * queued to the backlog. This prevents a potential race between
5612 * backlog processing on the old socket and new-packet processing
5613 * on the new socket.
5614 */
5615 sctp_lock_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618 /* If the association on the newsk is already closed before accept()
5619 * is called, set RCV_SHUTDOWN flag.
5620 */
5621 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5622 newsk->sk_shutdown |= RCV_SHUTDOWN;
5623
5624 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005625 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626}
5627
5628/* This proto struct describes the ULP interface for SCTP. */
5629struct proto sctp_prot = {
5630 .name = "SCTP",
5631 .owner = THIS_MODULE,
5632 .close = sctp_close,
5633 .connect = sctp_connect,
5634 .disconnect = sctp_disconnect,
5635 .accept = sctp_accept,
5636 .ioctl = sctp_ioctl,
5637 .init = sctp_init_sock,
5638 .destroy = sctp_destroy_sock,
5639 .shutdown = sctp_shutdown,
5640 .setsockopt = sctp_setsockopt,
5641 .getsockopt = sctp_getsockopt,
5642 .sendmsg = sctp_sendmsg,
5643 .recvmsg = sctp_recvmsg,
5644 .bind = sctp_bind,
5645 .backlog_rcv = sctp_backlog_rcv,
5646 .hash = sctp_hash,
5647 .unhash = sctp_unhash,
5648 .get_port = sctp_get_port,
5649 .obj_size = sizeof(struct sctp_sock),
5650};
5651
5652#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5653struct proto sctpv6_prot = {
5654 .name = "SCTPv6",
5655 .owner = THIS_MODULE,
5656 .close = sctp_close,
5657 .connect = sctp_connect,
5658 .disconnect = sctp_disconnect,
5659 .accept = sctp_accept,
5660 .ioctl = sctp_ioctl,
5661 .init = sctp_init_sock,
5662 .destroy = sctp_destroy_sock,
5663 .shutdown = sctp_shutdown,
5664 .setsockopt = sctp_setsockopt,
5665 .getsockopt = sctp_getsockopt,
5666 .sendmsg = sctp_sendmsg,
5667 .recvmsg = sctp_recvmsg,
5668 .bind = sctp_bind,
5669 .backlog_rcv = sctp_backlog_rcv,
5670 .hash = sctp_hash,
5671 .unhash = sctp_unhash,
5672 .get_port = sctp_get_port,
5673 .obj_size = sizeof(struct sctp6_sock),
5674};
5675#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */