blob: 54722e622e6dbf68d44be8f82cdb01f72ab7b558 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/types.h>
61#include <linux/kernel.h>
62#include <linux/wait.h>
63#include <linux/time.h>
64#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080065#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/fcntl.h>
67#include <linux/poll.h>
68#include <linux/init.h>
69#include <linux/crypto.h>
70
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
76
77#include <linux/socket.h> /* for sa_family_t */
78#include <net/sock.h>
79#include <net/sctp/sctp.h>
80#include <net/sctp/sm.h>
81
82/* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
85 */
86
87/* Forward declarations for internal helper functions. */
88static int sctp_writeable(struct sock *sk);
89static void sctp_wfree(struct sk_buff *skb);
90static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94static int sctp_wait_for_accept(struct sock *sk, long timeo);
95static void sctp_wait_for_close(struct sock *sk, long timeo);
96static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105static int sctp_autobind(struct sock *sk);
106static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
109
110extern kmem_cache_t *sctp_bucket_cachep;
111
112/* Get the sndbuf space available at the time on the association. */
113static inline int sctp_wspace(struct sctp_association *asoc)
114{
115 struct sock *sk = asoc->base.sk;
116 int amt = 0;
117
Neil Horman4eb701d2005-04-28 12:02:04 -0700118 if (asoc->ep->sndbuf_policy) {
119 /* make sure that no association uses more than sk_sndbuf */
120 amt = sk->sk_sndbuf - asoc->sndbuf_used;
121 } else {
122 /* do socket level accounting */
123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
124 }
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (amt < 0)
127 amt = 0;
Neil Horman4eb701d2005-04-28 12:02:04 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return amt;
130}
131
132/* Increment the used sndbuf space count of the corresponding association by
133 * the size of the outgoing data chunk.
134 * Also, set the skb destructor for sndbuf accounting later.
135 *
136 * Since it is always 1-1 between chunk and skb, and also a new skb is always
137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
138 * destructor in the data chunk skb for the purpose of the sndbuf space
139 * tracking.
140 */
141static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
142{
143 struct sctp_association *asoc = chunk->asoc;
144 struct sock *sk = asoc->base.sk;
145
146 /* The sndbuf space is tracked per association. */
147 sctp_association_hold(asoc);
148
Neil Horman4eb701d2005-04-28 12:02:04 -0700149 skb_set_owner_w(chunk->skb, sk);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 chunk->skb->destructor = sctp_wfree;
152 /* Save the chunk pointer in skb for sctp_wfree to use later. */
153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
154
Neil Horman4eb701d2005-04-28 12:02:04 -0700155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
156 sizeof(struct sk_buff) +
157 sizeof(struct sctp_chunk);
158
Neil Horman4eb701d2005-04-28 12:02:04 -0700159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/* Verify that this is a valid address. */
163static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
164 int len)
165{
166 struct sctp_af *af;
167
168 /* Verify basic sockaddr. */
169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
170 if (!af)
171 return -EINVAL;
172
173 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700174 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -EINVAL;
176
177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
178 return -EINVAL;
179
180 return 0;
181}
182
183/* Look up the association by its id. If this is not a UDP-style
184 * socket, the ID field is always ignored.
185 */
186struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
187{
188 struct sctp_association *asoc = NULL;
189
190 /* If this is not a UDP-style socket, assoc id should be ignored. */
191 if (!sctp_style(sk, UDP)) {
192 /* Return NULL if the socket state is not ESTABLISHED. It
193 * could be a TCP-style listening socket or a socket which
194 * hasn't yet called connect() to establish an association.
195 */
196 if (!sctp_sstate(sk, ESTABLISHED))
197 return NULL;
198
199 /* Get the first and the only association from the list. */
200 if (!list_empty(&sctp_sk(sk)->ep->asocs))
201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
202 struct sctp_association, asocs);
203 return asoc;
204 }
205
206 /* Otherwise this is a UDP-style socket. */
207 if (!id || (id == (sctp_assoc_t)-1))
208 return NULL;
209
210 spin_lock_bh(&sctp_assocs_id_lock);
211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
212 spin_unlock_bh(&sctp_assocs_id_lock);
213
214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
215 return NULL;
216
217 return asoc;
218}
219
220/* Look up the transport from an address and an assoc id. If both address and
221 * id are specified, the associations matching the address and the id should be
222 * the same.
223 */
224static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
225 struct sockaddr_storage *addr,
226 sctp_assoc_t id)
227{
228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
229 struct sctp_transport *transport;
230 union sctp_addr *laddr = (union sctp_addr *)addr;
231
232 laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
233 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
234 (union sctp_addr *)addr,
235 &transport);
236 laddr->v4.sin_port = htons(laddr->v4.sin_port);
237
238 if (!addr_asoc)
239 return NULL;
240
241 id_asoc = sctp_id2assoc(sk, id);
242 if (id_asoc && (id_asoc != addr_asoc))
243 return NULL;
244
245 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
246 (union sctp_addr *)addr);
247
248 return transport;
249}
250
251/* API 3.1.2 bind() - UDP Style Syntax
252 * The syntax of bind() is,
253 *
254 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
255 *
256 * sd - the socket descriptor returned by socket().
257 * addr - the address structure (struct sockaddr_in or struct
258 * sockaddr_in6 [RFC 2553]),
259 * addr_len - the size of the address structure.
260 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700261SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int retval = 0;
264
265 sctp_lock_sock(sk);
266
Frank Filz3f7a87d2005-06-20 13:14:57 -0700267 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
268 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* Disallow binding twice. */
271 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700272 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 addr_len);
274 else
275 retval = -EINVAL;
276
277 sctp_release_sock(sk);
278
279 return retval;
280}
281
282static long sctp_get_port_local(struct sock *, union sctp_addr *);
283
284/* Verify this is a valid sockaddr. */
285static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
286 union sctp_addr *addr, int len)
287{
288 struct sctp_af *af;
289
290 /* Check minimum size. */
291 if (len < sizeof (struct sockaddr))
292 return NULL;
293
294 /* Does this PF support this AF? */
295 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
296 return NULL;
297
298 /* If we get this far, af is valid. */
299 af = sctp_get_af_specific(addr->sa.sa_family);
300
301 if (len < af->sockaddr_len)
302 return NULL;
303
304 return af;
305}
306
307/* Bind a local address either to an endpoint or to an association. */
308SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
309{
310 struct sctp_sock *sp = sctp_sk(sk);
311 struct sctp_endpoint *ep = sp->ep;
312 struct sctp_bind_addr *bp = &ep->base.bind_addr;
313 struct sctp_af *af;
314 unsigned short snum;
315 int ret = 0;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Common sockaddr verification. */
318 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700319 if (!af) {
320 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
321 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700323 }
324
325 snum = ntohs(addr->v4.sin_port);
326
327 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
328 ", port: %d, new port: %d, len: %d)\n",
329 sk,
330 addr,
331 bp->port, snum,
332 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* PF specific bind() address verification. */
335 if (!sp->pf->bind_verify(sp, addr))
336 return -EADDRNOTAVAIL;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* We must either be unbound, or bind to the same port. */
339 if (bp->port && (snum != bp->port)) {
340 SCTP_DEBUG_PRINTK("sctp_do_bind:"
341 " New port %d does not match existing port "
342 "%d.\n", snum, bp->port);
343 return -EINVAL;
344 }
345
346 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
347 return -EACCES;
348
349 /* Make sure we are allowed to bind here.
350 * The function sctp_get_port_local() does duplicate address
351 * detection.
352 */
353 if ((ret = sctp_get_port_local(sk, addr))) {
354 if (ret == (long) sk) {
355 /* This endpoint has a conflicting address. */
356 return -EINVAL;
357 } else {
358 return -EADDRINUSE;
359 }
360 }
361
362 /* Refresh ephemeral port. */
363 if (!bp->port)
364 bp->port = inet_sk(sk)->num;
365
366 /* Add the address to the bind address list. */
367 sctp_local_bh_disable();
368 sctp_write_lock(&ep->base.addr_lock);
369
370 /* Use GFP_ATOMIC since BHs are disabled. */
371 addr->v4.sin_port = ntohs(addr->v4.sin_port);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700372 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 addr->v4.sin_port = htons(addr->v4.sin_port);
374 sctp_write_unlock(&ep->base.addr_lock);
375 sctp_local_bh_enable();
376
377 /* Copy back into socket for getsockname() use. */
378 if (!ret) {
379 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
380 af->to_sk_saddr(addr, sk);
381 }
382
383 return ret;
384}
385
386 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
387 *
388 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
389 * at any one time. If a sender, after sending an ASCONF chunk, decides
390 * it needs to transfer another ASCONF Chunk, it MUST wait until the
391 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
392 * subsequent ASCONF. Note this restriction binds each side, so at any
393 * time two ASCONF may be in-transit on any given association (one sent
394 * from each endpoint).
395 */
396static int sctp_send_asconf(struct sctp_association *asoc,
397 struct sctp_chunk *chunk)
398{
399 int retval = 0;
400
401 /* If there is an outstanding ASCONF chunk, queue it for later
402 * transmission.
403 */
404 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700405 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto out;
407 }
408
409 /* Hold the chunk until an ASCONF_ACK is received. */
410 sctp_chunk_hold(chunk);
411 retval = sctp_primitive_ASCONF(asoc, chunk);
412 if (retval)
413 sctp_chunk_free(chunk);
414 else
415 asoc->addip_last_asconf = chunk;
416
417out:
418 return retval;
419}
420
421/* Add a list of addresses as bind addresses to local endpoint or
422 * association.
423 *
424 * Basically run through each address specified in the addrs/addrcnt
425 * array/length pair, determine if it is IPv6 or IPv4 and call
426 * sctp_do_bind() on it.
427 *
428 * If any of them fails, then the operation will be reversed and the
429 * ones that were added will be removed.
430 *
431 * Only sctp_setsockopt_bindx() is supposed to call this function.
432 */
433int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
434{
435 int cnt;
436 int retval = 0;
437 void *addr_buf;
438 struct sockaddr *sa_addr;
439 struct sctp_af *af;
440
441 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
442 sk, addrs, addrcnt);
443
444 addr_buf = addrs;
445 for (cnt = 0; cnt < addrcnt; cnt++) {
446 /* The list may contain either IPv4 or IPv6 address;
447 * determine the address length for walking thru the list.
448 */
449 sa_addr = (struct sockaddr *)addr_buf;
450 af = sctp_get_af_specific(sa_addr->sa_family);
451 if (!af) {
452 retval = -EINVAL;
453 goto err_bindx_add;
454 }
455
456 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
457 af->sockaddr_len);
458
459 addr_buf += af->sockaddr_len;
460
461err_bindx_add:
462 if (retval < 0) {
463 /* Failed. Cleanup the ones that have been added */
464 if (cnt > 0)
465 sctp_bindx_rem(sk, addrs, cnt);
466 return retval;
467 }
468 }
469
470 return retval;
471}
472
473/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
474 * associations that are part of the endpoint indicating that a list of local
475 * addresses are added to the endpoint.
476 *
477 * If any of the addresses is already in the bind address list of the
478 * association, we do not send the chunk for that association. But it will not
479 * affect other associations.
480 *
481 * Only sctp_setsockopt_bindx() is supposed to call this function.
482 */
483static int sctp_send_asconf_add_ip(struct sock *sk,
484 struct sockaddr *addrs,
485 int addrcnt)
486{
487 struct sctp_sock *sp;
488 struct sctp_endpoint *ep;
489 struct sctp_association *asoc;
490 struct sctp_bind_addr *bp;
491 struct sctp_chunk *chunk;
492 struct sctp_sockaddr_entry *laddr;
493 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700494 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 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);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700562 if (retval)
563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700565 /* Add the new addresses to the bind address list with
566 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700568 sctp_local_bh_disable();
569 sctp_write_lock(&asoc->base.addr_lock);
570 addr_buf = addrs;
571 for (i = 0; i < addrcnt; i++) {
572 addr = (union sctp_addr *)addr_buf;
573 af = sctp_get_af_specific(addr->v4.sin_family);
574 memcpy(&saveaddr, addr, af->sockaddr_len);
575 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
576 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
577 GFP_ATOMIC);
578 addr_buf += af->sockaddr_len;
579 }
580 sctp_write_unlock(&asoc->base.addr_lock);
581 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584out:
585 return retval;
586}
587
588/* Remove a list of addresses from bind addresses list. Do not remove the
589 * last address.
590 *
591 * Basically run through each address specified in the addrs/addrcnt
592 * array/length pair, determine if it is IPv6 or IPv4 and call
593 * sctp_del_bind() on it.
594 *
595 * If any of them fails, then the operation will be reversed and the
596 * ones that were removed will be added back.
597 *
598 * At least one address has to be left; if only one address is
599 * available, the operation will return -EBUSY.
600 *
601 * Only sctp_setsockopt_bindx() is supposed to call this function.
602 */
603int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
604{
605 struct sctp_sock *sp = sctp_sk(sk);
606 struct sctp_endpoint *ep = sp->ep;
607 int cnt;
608 struct sctp_bind_addr *bp = &ep->base.bind_addr;
609 int retval = 0;
610 union sctp_addr saveaddr;
611 void *addr_buf;
612 struct sockaddr *sa_addr;
613 struct sctp_af *af;
614
615 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
616 sk, addrs, addrcnt);
617
618 addr_buf = addrs;
619 for (cnt = 0; cnt < addrcnt; cnt++) {
620 /* If the bind address list is empty or if there is only one
621 * bind address, there is nothing more to be removed (we need
622 * at least one address here).
623 */
624 if (list_empty(&bp->address_list) ||
625 (sctp_list_single_entry(&bp->address_list))) {
626 retval = -EBUSY;
627 goto err_bindx_rem;
628 }
629
630 /* The list may contain either IPv4 or IPv6 address;
631 * determine the address length to copy the address to
632 * saveaddr.
633 */
634 sa_addr = (struct sockaddr *)addr_buf;
635 af = sctp_get_af_specific(sa_addr->sa_family);
636 if (!af) {
637 retval = -EINVAL;
638 goto err_bindx_rem;
639 }
640 memcpy(&saveaddr, sa_addr, af->sockaddr_len);
641 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
642 if (saveaddr.v4.sin_port != bp->port) {
643 retval = -EINVAL;
644 goto err_bindx_rem;
645 }
646
647 /* FIXME - There is probably a need to check if sk->sk_saddr and
648 * sk->sk_rcv_addr are currently set to one of the addresses to
649 * be removed. This is something which needs to be looked into
650 * when we are fixing the outstanding issues with multi-homing
651 * socket routing and failover schemes. Refer to comments in
652 * sctp_do_bind(). -daisy
653 */
654 sctp_local_bh_disable();
655 sctp_write_lock(&ep->base.addr_lock);
656
657 retval = sctp_del_bind_addr(bp, &saveaddr);
658
659 sctp_write_unlock(&ep->base.addr_lock);
660 sctp_local_bh_enable();
661
662 addr_buf += af->sockaddr_len;
663err_bindx_rem:
664 if (retval < 0) {
665 /* Failed. Add the ones that has been removed back */
666 if (cnt > 0)
667 sctp_bindx_add(sk, addrs, cnt);
668 return retval;
669 }
670 }
671
672 return retval;
673}
674
675/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
676 * the associations that are part of the endpoint indicating that a list of
677 * local addresses are removed from the endpoint.
678 *
679 * If any of the addresses is already in the bind address list of the
680 * association, we do not send the chunk for that association. But it will not
681 * affect other associations.
682 *
683 * Only sctp_setsockopt_bindx() is supposed to call this function.
684 */
685static int sctp_send_asconf_del_ip(struct sock *sk,
686 struct sockaddr *addrs,
687 int addrcnt)
688{
689 struct sctp_sock *sp;
690 struct sctp_endpoint *ep;
691 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700692 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct sctp_bind_addr *bp;
694 struct sctp_chunk *chunk;
695 union sctp_addr *laddr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700696 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 void *addr_buf;
698 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700699 struct list_head *pos, *pos1;
700 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int i;
702 int retval = 0;
703
704 if (!sctp_addip_enable)
705 return retval;
706
707 sp = sctp_sk(sk);
708 ep = sp->ep;
709
710 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
711 __FUNCTION__, sk, addrs, addrcnt);
712
713 list_for_each(pos, &ep->asocs) {
714 asoc = list_entry(pos, struct sctp_association, asocs);
715
716 if (!asoc->peer.asconf_capable)
717 continue;
718
719 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
720 continue;
721
722 if (!sctp_state(asoc, ESTABLISHED))
723 continue;
724
725 /* Check if any address in the packed array of addresses is
726 * not present in the bind address list of the association.
727 * If so, do not send the asconf chunk to its peer, but
728 * continue with other associations.
729 */
730 addr_buf = addrs;
731 for (i = 0; i < addrcnt; i++) {
732 laddr = (union sctp_addr *)addr_buf;
733 af = sctp_get_af_specific(laddr->v4.sin_family);
734 if (!af) {
735 retval = -EINVAL;
736 goto out;
737 }
738
739 if (!sctp_assoc_lookup_laddr(asoc, laddr))
740 break;
741
742 addr_buf += af->sockaddr_len;
743 }
744 if (i < addrcnt)
745 continue;
746
747 /* Find one address in the association's bind address list
748 * that is not in the packed array of addresses. This is to
749 * make sure that we do not delete all the addresses in the
750 * association.
751 */
752 sctp_read_lock(&asoc->base.addr_lock);
753 bp = &asoc->base.bind_addr;
754 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
755 addrcnt, sp);
756 sctp_read_unlock(&asoc->base.addr_lock);
757 if (!laddr)
758 continue;
759
760 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
761 SCTP_PARAM_DEL_IP);
762 if (!chunk) {
763 retval = -ENOMEM;
764 goto out;
765 }
766
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700767 /* Reset use_as_src flag for the addresses in the bind address
768 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700770 sctp_local_bh_disable();
771 sctp_write_lock(&asoc->base.addr_lock);
772 addr_buf = addrs;
773 for (i = 0; i < addrcnt; i++) {
774 laddr = (union sctp_addr *)addr_buf;
775 af = sctp_get_af_specific(laddr->v4.sin_family);
776 memcpy(&saveaddr, laddr, af->sockaddr_len);
777 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
778 list_for_each(pos1, &bp->address_list) {
779 saddr = list_entry(pos1,
780 struct sctp_sockaddr_entry,
781 list);
782 if (sctp_cmp_addr_exact(&saddr->a, &saveaddr))
783 saddr->use_as_src = 0;
784 }
785 addr_buf += af->sockaddr_len;
786 }
787 sctp_write_unlock(&asoc->base.addr_lock);
788 sctp_local_bh_enable();
789
790 /* Update the route and saddr entries for all the transports
791 * as some of the addresses in the bind address list are
792 * about to be deleted and cannot be used as source addresses.
793 */
794 list_for_each(pos1, &asoc->peer.transport_addr_list) {
795 transport = list_entry(pos1, struct sctp_transport,
796 transports);
797 dst_release(transport->dst);
798 sctp_transport_route(transport, NULL,
799 sctp_sk(asoc->base.sk));
800 }
801
802 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804out:
805 return retval;
806}
807
808/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
809 *
810 * API 8.1
811 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
812 * int flags);
813 *
814 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
815 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
816 * or IPv6 addresses.
817 *
818 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
819 * Section 3.1.2 for this usage.
820 *
821 * addrs is a pointer to an array of one or more socket addresses. Each
822 * address is contained in its appropriate structure (i.e. struct
823 * sockaddr_in or struct sockaddr_in6) the family of the address type
824 * must be used to distengish the address length (note that this
825 * representation is termed a "packed array" of addresses). The caller
826 * specifies the number of addresses in the array with addrcnt.
827 *
828 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
829 * -1, and sets errno to the appropriate error code.
830 *
831 * For SCTP, the port given in each socket address must be the same, or
832 * sctp_bindx() will fail, setting errno to EINVAL.
833 *
834 * The flags parameter is formed from the bitwise OR of zero or more of
835 * the following currently defined flags:
836 *
837 * SCTP_BINDX_ADD_ADDR
838 *
839 * SCTP_BINDX_REM_ADDR
840 *
841 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
842 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
843 * addresses from the association. The two flags are mutually exclusive;
844 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
845 * not remove all addresses from an association; sctp_bindx() will
846 * reject such an attempt with EINVAL.
847 *
848 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
849 * additional addresses with an endpoint after calling bind(). Or use
850 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
851 * socket is associated with so that no new association accepted will be
852 * associated with those addresses. If the endpoint supports dynamic
853 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
854 * endpoint to send the appropriate message to the peer to change the
855 * peers address lists.
856 *
857 * Adding and removing addresses from a connected association is
858 * optional functionality. Implementations that do not support this
859 * functionality should return EOPNOTSUPP.
860 *
861 * Basically do nothing but copying the addresses from user to kernel
862 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700863 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
864 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * We don't use copy_from_user() for optimization: we first do the
867 * sanity checks (buffer size -fast- and access check-healthy
868 * pointer); if all of those succeed, then we can alloc the memory
869 * (expensive operation) needed to copy the data to kernel. Then we do
870 * the copying without checking the user space area
871 * (__copy_from_user()).
872 *
873 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
874 * it.
875 *
876 * sk The sk of the socket
877 * addrs The pointer to the addresses in user land
878 * addrssize Size of the addrs buffer
879 * op Operation to perform (add or remove, see the flags of
880 * sctp_bindx)
881 *
882 * Returns 0 if ok, <0 errno code on error.
883 */
884SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
885 struct sockaddr __user *addrs,
886 int addrs_size, int op)
887{
888 struct sockaddr *kaddrs;
889 int err;
890 int addrcnt = 0;
891 int walk_size = 0;
892 struct sockaddr *sa_addr;
893 void *addr_buf;
894 struct sctp_af *af;
895
896 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
897 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
898
899 if (unlikely(addrs_size <= 0))
900 return -EINVAL;
901
902 /* Check the user passed a healthy pointer. */
903 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
904 return -EFAULT;
905
906 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800907 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (unlikely(!kaddrs))
909 return -ENOMEM;
910
911 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
912 kfree(kaddrs);
913 return -EFAULT;
914 }
915
916 /* Walk through the addrs buffer and count the number of addresses. */
917 addr_buf = kaddrs;
918 while (walk_size < addrs_size) {
919 sa_addr = (struct sockaddr *)addr_buf;
920 af = sctp_get_af_specific(sa_addr->sa_family);
921
922 /* If the address family is not supported or if this address
923 * causes the address buffer to overflow return EINVAL.
924 */
925 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
926 kfree(kaddrs);
927 return -EINVAL;
928 }
929 addrcnt++;
930 addr_buf += af->sockaddr_len;
931 walk_size += af->sockaddr_len;
932 }
933
934 /* Do the work. */
935 switch (op) {
936 case SCTP_BINDX_ADD_ADDR:
937 err = sctp_bindx_add(sk, kaddrs, addrcnt);
938 if (err)
939 goto out;
940 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
941 break;
942
943 case SCTP_BINDX_REM_ADDR:
944 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
945 if (err)
946 goto out;
947 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
948 break;
949
950 default:
951 err = -EINVAL;
952 break;
953 };
954
955out:
956 kfree(kaddrs);
957
958 return err;
959}
960
Frank Filz3f7a87d2005-06-20 13:14:57 -0700961/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
962 *
963 * Common routine for handling connect() and sctp_connectx().
964 * Connect will come in with just a single address.
965 */
966static int __sctp_connect(struct sock* sk,
967 struct sockaddr *kaddrs,
968 int addrs_size)
969{
970 struct sctp_sock *sp;
971 struct sctp_endpoint *ep;
972 struct sctp_association *asoc = NULL;
973 struct sctp_association *asoc2;
974 struct sctp_transport *transport;
975 union sctp_addr to;
976 struct sctp_af *af;
977 sctp_scope_t scope;
978 long timeo;
979 int err = 0;
980 int addrcnt = 0;
981 int walk_size = 0;
982 struct sockaddr *sa_addr;
983 void *addr_buf;
984
985 sp = sctp_sk(sk);
986 ep = sp->ep;
987
988 /* connect() cannot be done on a socket that is already in ESTABLISHED
989 * state - UDP-style peeled off socket or a TCP-style socket that
990 * is already connected.
991 * It cannot be done even on a TCP-style listening socket.
992 */
993 if (sctp_sstate(sk, ESTABLISHED) ||
994 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
995 err = -EISCONN;
996 goto out_free;
997 }
998
999 /* Walk through the addrs buffer and count the number of addresses. */
1000 addr_buf = kaddrs;
1001 while (walk_size < addrs_size) {
1002 sa_addr = (struct sockaddr *)addr_buf;
1003 af = sctp_get_af_specific(sa_addr->sa_family);
1004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
1007 */
1008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 err = -EINVAL;
1010 goto out_free;
1011 }
1012
1013 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
1014 af->sockaddr_len);
1015 if (err)
1016 goto out_free;
1017
1018 memcpy(&to, sa_addr, af->sockaddr_len);
1019 to.v4.sin_port = ntohs(to.v4.sin_port);
1020
1021 /* Check if there already is a matching association on the
1022 * endpoint (other than the one created here).
1023 */
1024 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1025 if (asoc2 && asoc2 != asoc) {
1026 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1027 err = -EISCONN;
1028 else
1029 err = -EALREADY;
1030 goto out_free;
1031 }
1032
1033 /* If we could not find a matching association on the endpoint,
1034 * make sure that there is no peeled-off association matching
1035 * the peer address even on another socket.
1036 */
1037 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1038 err = -EADDRNOTAVAIL;
1039 goto out_free;
1040 }
1041
1042 if (!asoc) {
1043 /* If a bind() or sctp_bindx() is not called prior to
1044 * an sctp_connectx() call, the system picks an
1045 * ephemeral port and will choose an address set
1046 * equivalent to binding with a wildcard address.
1047 */
1048 if (!ep->base.bind_addr.port) {
1049 if (sctp_autobind(sk)) {
1050 err = -EAGAIN;
1051 goto out_free;
1052 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001053 } else {
1054 /*
1055 * If an unprivileged user inherits a 1-many
1056 * style socket with open associations on a
1057 * privileged port, it MAY be permitted to
1058 * accept new associations, but it SHOULD NOT
1059 * be permitted to open new associations.
1060 */
1061 if (ep->base.bind_addr.port < PROT_SOCK &&
1062 !capable(CAP_NET_BIND_SERVICE)) {
1063 err = -EACCES;
1064 goto out_free;
1065 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001066 }
1067
1068 scope = sctp_scope(&to);
1069 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1070 if (!asoc) {
1071 err = -ENOMEM;
1072 goto out_free;
1073 }
1074 }
1075
1076 /* Prime the peer's transport structures. */
1077 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1078 SCTP_UNKNOWN);
1079 if (!transport) {
1080 err = -ENOMEM;
1081 goto out_free;
1082 }
1083
1084 addrcnt++;
1085 addr_buf += af->sockaddr_len;
1086 walk_size += af->sockaddr_len;
1087 }
1088
1089 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1090 if (err < 0) {
1091 goto out_free;
1092 }
1093
1094 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1095 if (err < 0) {
1096 goto out_free;
1097 }
1098
1099 /* Initialize sk's dport and daddr for getpeername() */
1100 inet_sk(sk)->dport = htons(asoc->peer.port);
1101 af = sctp_get_af_specific(to.sa.sa_family);
1102 af->to_sk_daddr(&to, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001103 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001104
1105 timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1106 err = sctp_wait_for_connect(asoc, &timeo);
1107
1108 /* Don't free association on exit. */
1109 asoc = NULL;
1110
1111out_free:
1112
1113 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1114 " kaddrs: %p err: %d\n",
1115 asoc, kaddrs, err);
1116 if (asoc)
1117 sctp_association_free(asoc);
1118 return err;
1119}
1120
1121/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1122 *
1123 * API 8.9
1124 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1125 *
1126 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1127 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1128 * or IPv6 addresses.
1129 *
1130 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1131 * Section 3.1.2 for this usage.
1132 *
1133 * addrs is a pointer to an array of one or more socket addresses. Each
1134 * address is contained in its appropriate structure (i.e. struct
1135 * sockaddr_in or struct sockaddr_in6) the family of the address type
1136 * must be used to distengish the address length (note that this
1137 * representation is termed a "packed array" of addresses). The caller
1138 * specifies the number of addresses in the array with addrcnt.
1139 *
1140 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1141 * -1, and sets errno to the appropriate error code.
1142 *
1143 * For SCTP, the port given in each socket address must be the same, or
1144 * sctp_connectx() will fail, setting errno to EINVAL.
1145 *
1146 * An application can use sctp_connectx to initiate an association with
1147 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1148 * allows a caller to specify multiple addresses at which a peer can be
1149 * reached. The way the SCTP stack uses the list of addresses to set up
1150 * the association is implementation dependant. This function only
1151 * specifies that the stack will try to make use of all the addresses in
1152 * the list when needed.
1153 *
1154 * Note that the list of addresses passed in is only used for setting up
1155 * the association. It does not necessarily equal the set of addresses
1156 * the peer uses for the resulting association. If the caller wants to
1157 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1158 * retrieve them after the association has been set up.
1159 *
1160 * Basically do nothing but copying the addresses from user to kernel
1161 * land and invoking either sctp_connectx(). This is used for tunneling
1162 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1163 *
1164 * We don't use copy_from_user() for optimization: we first do the
1165 * sanity checks (buffer size -fast- and access check-healthy
1166 * pointer); if all of those succeed, then we can alloc the memory
1167 * (expensive operation) needed to copy the data to kernel. Then we do
1168 * the copying without checking the user space area
1169 * (__copy_from_user()).
1170 *
1171 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1172 * it.
1173 *
1174 * sk The sk of the socket
1175 * addrs The pointer to the addresses in user land
1176 * addrssize Size of the addrs buffer
1177 *
1178 * Returns 0 if ok, <0 errno code on error.
1179 */
1180SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1181 struct sockaddr __user *addrs,
1182 int addrs_size)
1183{
1184 int err = 0;
1185 struct sockaddr *kaddrs;
1186
1187 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1188 __FUNCTION__, sk, addrs, addrs_size);
1189
1190 if (unlikely(addrs_size <= 0))
1191 return -EINVAL;
1192
1193 /* Check the user passed a healthy pointer. */
1194 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1195 return -EFAULT;
1196
1197 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001198 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001199 if (unlikely(!kaddrs))
1200 return -ENOMEM;
1201
1202 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1203 err = -EFAULT;
1204 } else {
1205 err = __sctp_connect(sk, kaddrs, addrs_size);
1206 }
1207
1208 kfree(kaddrs);
1209 return err;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/* API 3.1.4 close() - UDP Style Syntax
1213 * Applications use close() to perform graceful shutdown (as described in
1214 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1215 * by a UDP-style socket.
1216 *
1217 * The syntax is
1218 *
1219 * ret = close(int sd);
1220 *
1221 * sd - the socket descriptor of the associations to be closed.
1222 *
1223 * To gracefully shutdown a specific association represented by the
1224 * UDP-style socket, an application should use the sendmsg() call,
1225 * passing no user data, but including the appropriate flag in the
1226 * ancillary data (see Section xxxx).
1227 *
1228 * If sd in the close() call is a branched-off socket representing only
1229 * one association, the shutdown is performed on that association only.
1230 *
1231 * 4.1.6 close() - TCP Style Syntax
1232 *
1233 * Applications use close() to gracefully close down an association.
1234 *
1235 * The syntax is:
1236 *
1237 * int close(int sd);
1238 *
1239 * sd - the socket descriptor of the association to be closed.
1240 *
1241 * After an application calls close() on a socket descriptor, no further
1242 * socket operations will succeed on that descriptor.
1243 *
1244 * API 7.1.4 SO_LINGER
1245 *
1246 * An application using the TCP-style socket can use this option to
1247 * perform the SCTP ABORT primitive. The linger option structure is:
1248 *
1249 * struct linger {
1250 * int l_onoff; // option on/off
1251 * int l_linger; // linger time
1252 * };
1253 *
1254 * To enable the option, set l_onoff to 1. If the l_linger value is set
1255 * to 0, calling close() is the same as the ABORT primitive. If the
1256 * value is set to a negative value, the setsockopt() call will return
1257 * an error. If the value is set to a positive value linger_time, the
1258 * close() can be blocked for at most linger_time ms. If the graceful
1259 * shutdown phase does not finish during this period, close() will
1260 * return but the graceful shutdown phase continues in the system.
1261 */
1262SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1263{
1264 struct sctp_endpoint *ep;
1265 struct sctp_association *asoc;
1266 struct list_head *pos, *temp;
1267
1268 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1269
1270 sctp_lock_sock(sk);
1271 sk->sk_shutdown = SHUTDOWN_MASK;
1272
1273 ep = sctp_sk(sk)->ep;
1274
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001275 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 list_for_each_safe(pos, temp, &ep->asocs) {
1277 asoc = list_entry(pos, struct sctp_association, asocs);
1278
1279 if (sctp_style(sk, TCP)) {
1280 /* A closed association can still be in the list if
1281 * it belongs to a TCP-style listening socket that is
1282 * not yet accepted. If so, free it. If not, send an
1283 * ABORT or SHUTDOWN based on the linger options.
1284 */
1285 if (sctp_state(asoc, CLOSED)) {
1286 sctp_unhash_established(asoc);
1287 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001288 continue;
1289 }
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001292 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)
1293 sctp_primitive_ABORT(asoc, NULL);
1294 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 sctp_primitive_SHUTDOWN(asoc, NULL);
1296 }
1297
1298 /* Clean up any skbs sitting on the receive queue. */
1299 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1300 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1301
1302 /* On a TCP-style socket, block for at most linger_time if set. */
1303 if (sctp_style(sk, TCP) && timeout)
1304 sctp_wait_for_close(sk, timeout);
1305
1306 /* This will run the backlog queue. */
1307 sctp_release_sock(sk);
1308
1309 /* Supposedly, no process has access to the socket, but
1310 * the net layers still may.
1311 */
1312 sctp_local_bh_disable();
1313 sctp_bh_lock_sock(sk);
1314
1315 /* Hold the sock, since sk_common_release() will put sock_put()
1316 * and we have just a little more cleanup.
1317 */
1318 sock_hold(sk);
1319 sk_common_release(sk);
1320
1321 sctp_bh_unlock_sock(sk);
1322 sctp_local_bh_enable();
1323
1324 sock_put(sk);
1325
1326 SCTP_DBG_OBJCNT_DEC(sock);
1327}
1328
1329/* Handle EPIPE error. */
1330static int sctp_error(struct sock *sk, int flags, int err)
1331{
1332 if (err == -EPIPE)
1333 err = sock_error(sk) ? : -EPIPE;
1334 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1335 send_sig(SIGPIPE, current, 0);
1336 return err;
1337}
1338
1339/* API 3.1.3 sendmsg() - UDP Style Syntax
1340 *
1341 * An application uses sendmsg() and recvmsg() calls to transmit data to
1342 * and receive data from its peer.
1343 *
1344 * ssize_t sendmsg(int socket, const struct msghdr *message,
1345 * int flags);
1346 *
1347 * socket - the socket descriptor of the endpoint.
1348 * message - pointer to the msghdr structure which contains a single
1349 * user message and possibly some ancillary data.
1350 *
1351 * See Section 5 for complete description of the data
1352 * structures.
1353 *
1354 * flags - flags sent or received with the user message, see Section
1355 * 5 for complete description of the flags.
1356 *
1357 * Note: This function could use a rewrite especially when explicit
1358 * connect support comes in.
1359 */
1360/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1361
1362SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1363
1364SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1365 struct msghdr *msg, size_t msg_len)
1366{
1367 struct sctp_sock *sp;
1368 struct sctp_endpoint *ep;
1369 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1370 struct sctp_transport *transport, *chunk_tp;
1371 struct sctp_chunk *chunk;
1372 union sctp_addr to;
1373 struct sockaddr *msg_name = NULL;
1374 struct sctp_sndrcvinfo default_sinfo = { 0 };
1375 struct sctp_sndrcvinfo *sinfo;
1376 struct sctp_initmsg *sinit;
1377 sctp_assoc_t associd = 0;
1378 sctp_cmsgs_t cmsgs = { NULL };
1379 int err;
1380 sctp_scope_t scope;
1381 long timeo;
1382 __u16 sinfo_flags = 0;
1383 struct sctp_datamsg *datamsg;
1384 struct list_head *pos;
1385 int msg_flags = msg->msg_flags;
1386
1387 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1388 sk, msg, msg_len);
1389
1390 err = 0;
1391 sp = sctp_sk(sk);
1392 ep = sp->ep;
1393
Frank Filz3f7a87d2005-06-20 13:14:57 -07001394 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* We cannot send a message over a TCP-style listening socket. */
1397 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1398 err = -EPIPE;
1399 goto out_nounlock;
1400 }
1401
1402 /* Parse out the SCTP CMSGs. */
1403 err = sctp_msghdr_parse(msg, &cmsgs);
1404
1405 if (err) {
1406 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1407 goto out_nounlock;
1408 }
1409
1410 /* Fetch the destination address for this packet. This
1411 * address only selects the association--it is not necessarily
1412 * the address we will send to.
1413 * For a peeled-off socket, msg_name is ignored.
1414 */
1415 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1416 int msg_namelen = msg->msg_namelen;
1417
1418 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1419 msg_namelen);
1420 if (err)
1421 return err;
1422
1423 if (msg_namelen > sizeof(to))
1424 msg_namelen = sizeof(to);
1425 memcpy(&to, msg->msg_name, msg_namelen);
1426 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1427 "0x%x:%u.\n",
1428 to.v4.sin_addr.s_addr, to.v4.sin_port);
1429
1430 to.v4.sin_port = ntohs(to.v4.sin_port);
1431 msg_name = msg->msg_name;
1432 }
1433
1434 sinfo = cmsgs.info;
1435 sinit = cmsgs.init;
1436
1437 /* Did the user specify SNDRCVINFO? */
1438 if (sinfo) {
1439 sinfo_flags = sinfo->sinfo_flags;
1440 associd = sinfo->sinfo_assoc_id;
1441 }
1442
1443 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1444 msg_len, sinfo_flags);
1445
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001446 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1447 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 err = -EINVAL;
1449 goto out_nounlock;
1450 }
1451
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001452 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1453 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1454 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * the msg_iov set to the user abort reason.
1456 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001457 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1458 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 err = -EINVAL;
1460 goto out_nounlock;
1461 }
1462
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001463 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * specified in msg_name.
1465 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001466 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 err = -EINVAL;
1468 goto out_nounlock;
1469 }
1470
1471 transport = NULL;
1472
1473 SCTP_DEBUG_PRINTK("About to look up association.\n");
1474
1475 sctp_lock_sock(sk);
1476
1477 /* If a msg_name has been specified, assume this is to be used. */
1478 if (msg_name) {
1479 /* Look for a matching association on the endpoint. */
1480 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1481 if (!asoc) {
1482 /* If we could not find a matching association on the
1483 * endpoint, make sure that it is not a TCP-style
1484 * socket that already has an association or there is
1485 * no peeled-off association on another socket.
1486 */
1487 if ((sctp_style(sk, TCP) &&
1488 sctp_sstate(sk, ESTABLISHED)) ||
1489 sctp_endpoint_is_peeled_off(ep, &to)) {
1490 err = -EADDRNOTAVAIL;
1491 goto out_unlock;
1492 }
1493 }
1494 } else {
1495 asoc = sctp_id2assoc(sk, associd);
1496 if (!asoc) {
1497 err = -EPIPE;
1498 goto out_unlock;
1499 }
1500 }
1501
1502 if (asoc) {
1503 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1504
1505 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1506 * socket that has an association in CLOSED state. This can
1507 * happen when an accepted socket has an association that is
1508 * already CLOSED.
1509 */
1510 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1511 err = -EPIPE;
1512 goto out_unlock;
1513 }
1514
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001515 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1517 asoc);
1518 sctp_primitive_SHUTDOWN(asoc, NULL);
1519 err = 0;
1520 goto out_unlock;
1521 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001522 if (sinfo_flags & SCTP_ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1524 sctp_primitive_ABORT(asoc, msg);
1525 err = 0;
1526 goto out_unlock;
1527 }
1528 }
1529
1530 /* Do we need to create the association? */
1531 if (!asoc) {
1532 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1533
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001534 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 err = -EINVAL;
1536 goto out_unlock;
1537 }
1538
1539 /* Check for invalid stream against the stream counts,
1540 * either the default or the user specified stream counts.
1541 */
1542 if (sinfo) {
1543 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1544 /* Check against the defaults. */
1545 if (sinfo->sinfo_stream >=
1546 sp->initmsg.sinit_num_ostreams) {
1547 err = -EINVAL;
1548 goto out_unlock;
1549 }
1550 } else {
1551 /* Check against the requested. */
1552 if (sinfo->sinfo_stream >=
1553 sinit->sinit_num_ostreams) {
1554 err = -EINVAL;
1555 goto out_unlock;
1556 }
1557 }
1558 }
1559
1560 /*
1561 * API 3.1.2 bind() - UDP Style Syntax
1562 * If a bind() or sctp_bindx() is not called prior to a
1563 * sendmsg() call that initiates a new association, the
1564 * system picks an ephemeral port and will choose an address
1565 * set equivalent to binding with a wildcard address.
1566 */
1567 if (!ep->base.bind_addr.port) {
1568 if (sctp_autobind(sk)) {
1569 err = -EAGAIN;
1570 goto out_unlock;
1571 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001572 } else {
1573 /*
1574 * If an unprivileged user inherits a one-to-many
1575 * style socket with open associations on a privileged
1576 * port, it MAY be permitted to accept new associations,
1577 * but it SHOULD NOT be permitted to open new
1578 * associations.
1579 */
1580 if (ep->base.bind_addr.port < PROT_SOCK &&
1581 !capable(CAP_NET_BIND_SERVICE)) {
1582 err = -EACCES;
1583 goto out_unlock;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
1587 scope = sctp_scope(&to);
1588 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1589 if (!new_asoc) {
1590 err = -ENOMEM;
1591 goto out_unlock;
1592 }
1593 asoc = new_asoc;
1594
1595 /* If the SCTP_INIT ancillary data is specified, set all
1596 * the association init values accordingly.
1597 */
1598 if (sinit) {
1599 if (sinit->sinit_num_ostreams) {
1600 asoc->c.sinit_num_ostreams =
1601 sinit->sinit_num_ostreams;
1602 }
1603 if (sinit->sinit_max_instreams) {
1604 asoc->c.sinit_max_instreams =
1605 sinit->sinit_max_instreams;
1606 }
1607 if (sinit->sinit_max_attempts) {
1608 asoc->max_init_attempts
1609 = sinit->sinit_max_attempts;
1610 }
1611 if (sinit->sinit_max_init_timeo) {
1612 asoc->max_init_timeo =
1613 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1614 }
1615 }
1616
1617 /* Prime the peer's transport structures. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001618 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!transport) {
1620 err = -ENOMEM;
1621 goto out_free;
1622 }
1623 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1624 if (err < 0) {
1625 err = -ENOMEM;
1626 goto out_free;
1627 }
1628 }
1629
1630 /* ASSERT: we have a valid association at this point. */
1631 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1632
1633 if (!sinfo) {
1634 /* If the user didn't specify SNDRCVINFO, make up one with
1635 * some defaults.
1636 */
1637 default_sinfo.sinfo_stream = asoc->default_stream;
1638 default_sinfo.sinfo_flags = asoc->default_flags;
1639 default_sinfo.sinfo_ppid = asoc->default_ppid;
1640 default_sinfo.sinfo_context = asoc->default_context;
1641 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1642 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1643 sinfo = &default_sinfo;
1644 }
1645
1646 /* API 7.1.7, the sndbuf size per association bounds the
1647 * maximum size of data that can be sent in a single send call.
1648 */
1649 if (msg_len > sk->sk_sndbuf) {
1650 err = -EMSGSIZE;
1651 goto out_free;
1652 }
1653
1654 /* If fragmentation is disabled and the message length exceeds the
1655 * association fragmentation point, return EMSGSIZE. The I-D
1656 * does not specify what this error is, but this looks like
1657 * a great fit.
1658 */
1659 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1660 err = -EMSGSIZE;
1661 goto out_free;
1662 }
1663
1664 if (sinfo) {
1665 /* Check for invalid stream. */
1666 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1667 err = -EINVAL;
1668 goto out_free;
1669 }
1670 }
1671
1672 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1673 if (!sctp_wspace(asoc)) {
1674 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1675 if (err)
1676 goto out_free;
1677 }
1678
1679 /* If an address is passed with the sendto/sendmsg call, it is used
1680 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001681 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 */
1683 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001684 (sinfo_flags & SCTP_ADDR_OVER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1686 if (!chunk_tp) {
1687 err = -EINVAL;
1688 goto out_free;
1689 }
1690 } else
1691 chunk_tp = NULL;
1692
1693 /* Auto-connect, if we aren't connected already. */
1694 if (sctp_state(asoc, CLOSED)) {
1695 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1696 if (err < 0)
1697 goto out_free;
1698 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1699 }
1700
1701 /* Break the message into multiple chunks of maximum size. */
1702 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1703 if (!datamsg) {
1704 err = -ENOMEM;
1705 goto out_free;
1706 }
1707
1708 /* Now send the (possibly) fragmented message. */
1709 list_for_each(pos, &datamsg->chunks) {
1710 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1711 sctp_datamsg_track(chunk);
1712
1713 /* Do accounting for the write space. */
1714 sctp_set_owner_w(chunk);
1715
1716 chunk->transport = chunk_tp;
1717
1718 /* Send it to the lower layers. Note: all chunks
1719 * must either fail or succeed. The lower layer
1720 * works that way today. Keep it that way or this
1721 * breaks.
1722 */
1723 err = sctp_primitive_SEND(asoc, chunk);
1724 /* Did the lower layer accept the chunk? */
1725 if (err)
1726 sctp_chunk_free(chunk);
1727 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1728 }
1729
1730 sctp_datamsg_free(datamsg);
1731 if (err)
1732 goto out_free;
1733 else
1734 err = msg_len;
1735
1736 /* If we are already past ASSOCIATE, the lower
1737 * layers are responsible for association cleanup.
1738 */
1739 goto out_unlock;
1740
1741out_free:
1742 if (new_asoc)
1743 sctp_association_free(asoc);
1744out_unlock:
1745 sctp_release_sock(sk);
1746
1747out_nounlock:
1748 return sctp_error(sk, msg_flags, err);
1749
1750#if 0
1751do_sock_err:
1752 if (msg_len)
1753 err = msg_len;
1754 else
1755 err = sock_error(sk);
1756 goto out;
1757
1758do_interrupted:
1759 if (msg_len)
1760 err = msg_len;
1761 goto out;
1762#endif /* 0 */
1763}
1764
1765/* This is an extended version of skb_pull() that removes the data from the
1766 * start of a skb even when data is spread across the list of skb's in the
1767 * frag_list. len specifies the total amount of data that needs to be removed.
1768 * when 'len' bytes could be removed from the skb, it returns 0.
1769 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1770 * could not be removed.
1771 */
1772static int sctp_skb_pull(struct sk_buff *skb, int len)
1773{
1774 struct sk_buff *list;
1775 int skb_len = skb_headlen(skb);
1776 int rlen;
1777
1778 if (len <= skb_len) {
1779 __skb_pull(skb, len);
1780 return 0;
1781 }
1782 len -= skb_len;
1783 __skb_pull(skb, skb_len);
1784
1785 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1786 rlen = sctp_skb_pull(list, len);
1787 skb->len -= (len-rlen);
1788 skb->data_len -= (len-rlen);
1789
1790 if (!rlen)
1791 return 0;
1792
1793 len = rlen;
1794 }
1795
1796 return len;
1797}
1798
1799/* API 3.1.3 recvmsg() - UDP Style Syntax
1800 *
1801 * ssize_t recvmsg(int socket, struct msghdr *message,
1802 * int flags);
1803 *
1804 * socket - the socket descriptor of the endpoint.
1805 * message - pointer to the msghdr structure which contains a single
1806 * user message and possibly some ancillary data.
1807 *
1808 * See Section 5 for complete description of the data
1809 * structures.
1810 *
1811 * flags - flags sent or received with the user message, see Section
1812 * 5 for complete description of the flags.
1813 */
1814static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1815
1816SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1817 struct msghdr *msg, size_t len, int noblock,
1818 int flags, int *addr_len)
1819{
1820 struct sctp_ulpevent *event = NULL;
1821 struct sctp_sock *sp = sctp_sk(sk);
1822 struct sk_buff *skb;
1823 int copied;
1824 int err = 0;
1825 int skb_len;
1826
1827 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1828 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1829 "len", len, "knoblauch", noblock,
1830 "flags", flags, "addr_len", addr_len);
1831
1832 sctp_lock_sock(sk);
1833
1834 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1835 err = -ENOTCONN;
1836 goto out;
1837 }
1838
1839 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1840 if (!skb)
1841 goto out;
1842
1843 /* Get the total length of the skb including any skb's in the
1844 * frag_list.
1845 */
1846 skb_len = skb->len;
1847
1848 copied = skb_len;
1849 if (copied > len)
1850 copied = len;
1851
1852 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1853
1854 event = sctp_skb2event(skb);
1855
1856 if (err)
1857 goto out_free;
1858
1859 sock_recv_timestamp(msg, sk, skb);
1860 if (sctp_ulpevent_is_notification(event)) {
1861 msg->msg_flags |= MSG_NOTIFICATION;
1862 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1863 } else {
1864 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1865 }
1866
1867 /* Check if we allow SCTP_SNDRCVINFO. */
1868 if (sp->subscribe.sctp_data_io_event)
1869 sctp_ulpevent_read_sndrcvinfo(event, msg);
1870#if 0
1871 /* FIXME: we should be calling IP/IPv6 layers. */
1872 if (sk->sk_protinfo.af_inet.cmsg_flags)
1873 ip_cmsg_recv(msg, skb);
1874#endif
1875
1876 err = copied;
1877
1878 /* If skb's length exceeds the user's buffer, update the skb and
1879 * push it back to the receive_queue so that the next call to
1880 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1881 */
1882 if (skb_len > copied) {
1883 msg->msg_flags &= ~MSG_EOR;
1884 if (flags & MSG_PEEK)
1885 goto out_free;
1886 sctp_skb_pull(skb, copied);
1887 skb_queue_head(&sk->sk_receive_queue, skb);
1888
1889 /* When only partial message is copied to the user, increase
1890 * rwnd by that amount. If all the data in the skb is read,
1891 * rwnd is updated when the event is freed.
1892 */
1893 sctp_assoc_rwnd_increase(event->asoc, copied);
1894 goto out;
1895 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1896 (event->msg_flags & MSG_EOR))
1897 msg->msg_flags |= MSG_EOR;
1898 else
1899 msg->msg_flags &= ~MSG_EOR;
1900
1901out_free:
1902 if (flags & MSG_PEEK) {
1903 /* Release the skb reference acquired after peeking the skb in
1904 * sctp_skb_recv_datagram().
1905 */
1906 kfree_skb(skb);
1907 } else {
1908 /* Free the event which includes releasing the reference to
1909 * the owner of the skb, freeing the skb and updating the
1910 * rwnd.
1911 */
1912 sctp_ulpevent_free(event);
1913 }
1914out:
1915 sctp_release_sock(sk);
1916 return err;
1917}
1918
1919/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1920 *
1921 * This option is a on/off flag. If enabled no SCTP message
1922 * fragmentation will be performed. Instead if a message being sent
1923 * exceeds the current PMTU size, the message will NOT be sent and
1924 * instead a error will be indicated to the user.
1925 */
1926static int sctp_setsockopt_disable_fragments(struct sock *sk,
1927 char __user *optval, int optlen)
1928{
1929 int val;
1930
1931 if (optlen < sizeof(int))
1932 return -EINVAL;
1933
1934 if (get_user(val, (int __user *)optval))
1935 return -EFAULT;
1936
1937 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1938
1939 return 0;
1940}
1941
1942static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1943 int optlen)
1944{
1945 if (optlen != sizeof(struct sctp_event_subscribe))
1946 return -EINVAL;
1947 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1948 return -EFAULT;
1949 return 0;
1950}
1951
1952/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1953 *
1954 * This socket option is applicable to the UDP-style socket only. When
1955 * set it will cause associations that are idle for more than the
1956 * specified number of seconds to automatically close. An association
1957 * being idle is defined an association that has NOT sent or received
1958 * user data. The special value of '0' indicates that no automatic
1959 * close of any associations should be performed. The option expects an
1960 * integer defining the number of seconds of idle time before an
1961 * association is closed.
1962 */
1963static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1964 int optlen)
1965{
1966 struct sctp_sock *sp = sctp_sk(sk);
1967
1968 /* Applicable to UDP-style socket only */
1969 if (sctp_style(sk, TCP))
1970 return -EOPNOTSUPP;
1971 if (optlen != sizeof(int))
1972 return -EINVAL;
1973 if (copy_from_user(&sp->autoclose, optval, optlen))
1974 return -EFAULT;
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
1978
1979/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1980 *
1981 * Applications can enable or disable heartbeats for any peer address of
1982 * an association, modify an address's heartbeat interval, force a
1983 * heartbeat to be sent immediately, and adjust the address's maximum
1984 * number of retransmissions sent before an address is considered
1985 * unreachable. The following structure is used to access and modify an
1986 * address's parameters:
1987 *
1988 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08001989 * sctp_assoc_t spp_assoc_id;
1990 * struct sockaddr_storage spp_address;
1991 * uint32_t spp_hbinterval;
1992 * uint16_t spp_pathmaxrxt;
1993 * uint32_t spp_pathmtu;
1994 * uint32_t spp_sackdelay;
1995 * uint32_t spp_flags;
1996 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08001998 * spp_assoc_id - (one-to-many style socket) This is filled in the
1999 * application, and identifies the association for
2000 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * spp_address - This specifies which address is of interest.
2002 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002003 * in milliseconds. If a value of zero
2004 * is present in this field then no changes are to
2005 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 * spp_pathmaxrxt - This contains the maximum number of
2007 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002008 * considered unreachable. If a value of zero
2009 * is present in this field then no changes are to
2010 * be made to this parameter.
2011 * spp_pathmtu - When Path MTU discovery is disabled the value
2012 * specified here will be the "fixed" path mtu.
2013 * Note that if the spp_address field is empty
2014 * then all associations on this address will
2015 * have this fixed path mtu set upon them.
2016 *
2017 * spp_sackdelay - When delayed sack is enabled, this value specifies
2018 * the number of milliseconds that sacks will be delayed
2019 * for. This value will apply to all addresses of an
2020 * association if the spp_address field is empty. Note
2021 * also, that if delayed sack is enabled and this
2022 * value is set to 0, no change is made to the last
2023 * recorded delayed sack timer value.
2024 *
2025 * spp_flags - These flags are used to control various features
2026 * on an association. The flag field may contain
2027 * zero or more of the following options.
2028 *
2029 * SPP_HB_ENABLE - Enable heartbeats on the
2030 * specified address. Note that if the address
2031 * field is empty all addresses for the association
2032 * have heartbeats enabled upon them.
2033 *
2034 * SPP_HB_DISABLE - Disable heartbeats on the
2035 * speicifed address. Note that if the address
2036 * field is empty all addresses for the association
2037 * will have their heartbeats disabled. Note also
2038 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2039 * mutually exclusive, only one of these two should
2040 * be specified. Enabling both fields will have
2041 * undetermined results.
2042 *
2043 * SPP_HB_DEMAND - Request a user initiated heartbeat
2044 * to be made immediately.
2045 *
2046 * SPP_PMTUD_ENABLE - This field will enable PMTU
2047 * discovery upon the specified address. Note that
2048 * if the address feild is empty then all addresses
2049 * on the association are effected.
2050 *
2051 * SPP_PMTUD_DISABLE - This field will disable PMTU
2052 * discovery upon the specified address. Note that
2053 * if the address feild is empty then all addresses
2054 * on the association are effected. Not also that
2055 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2056 * exclusive. Enabling both will have undetermined
2057 * results.
2058 *
2059 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2060 * on delayed sack. The time specified in spp_sackdelay
2061 * is used to specify the sack delay for this address. Note
2062 * that if spp_address is empty then all addresses will
2063 * enable delayed sack and take on the sack delay
2064 * value specified in spp_sackdelay.
2065 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2066 * off delayed sack. If the spp_address field is blank then
2067 * delayed sack is disabled for the entire association. Note
2068 * also that this field is mutually exclusive to
2069 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2070 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002072int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2073 struct sctp_transport *trans,
2074 struct sctp_association *asoc,
2075 struct sctp_sock *sp,
2076 int hb_change,
2077 int pmtud_change,
2078 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int error;
2081
Frank Filz52ccb8e2005-12-22 11:36:46 -08002082 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2084 if (error)
2085 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Frank Filz52ccb8e2005-12-22 11:36:46 -08002088 if (params->spp_hbinterval) {
2089 if (trans) {
2090 trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2091 } else if (asoc) {
2092 asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2093 } else {
2094 sp->hbinterval = params->spp_hbinterval;
2095 }
2096 }
2097
2098 if (hb_change) {
2099 if (trans) {
2100 trans->param_flags =
2101 (trans->param_flags & ~SPP_HB) | hb_change;
2102 } else if (asoc) {
2103 asoc->param_flags =
2104 (asoc->param_flags & ~SPP_HB) | hb_change;
2105 } else {
2106 sp->param_flags =
2107 (sp->param_flags & ~SPP_HB) | hb_change;
2108 }
2109 }
2110
2111 if (params->spp_pathmtu) {
2112 if (trans) {
2113 trans->pathmtu = params->spp_pathmtu;
2114 sctp_assoc_sync_pmtu(asoc);
2115 } else if (asoc) {
2116 asoc->pathmtu = params->spp_pathmtu;
2117 sctp_frag_point(sp, params->spp_pathmtu);
2118 } else {
2119 sp->pathmtu = params->spp_pathmtu;
2120 }
2121 }
2122
2123 if (pmtud_change) {
2124 if (trans) {
2125 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2126 (params->spp_flags & SPP_PMTUD_ENABLE);
2127 trans->param_flags =
2128 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2129 if (update) {
2130 sctp_transport_pmtu(trans);
2131 sctp_assoc_sync_pmtu(asoc);
2132 }
2133 } else if (asoc) {
2134 asoc->param_flags =
2135 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2136 } else {
2137 sp->param_flags =
2138 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2139 }
2140 }
2141
2142 if (params->spp_sackdelay) {
2143 if (trans) {
2144 trans->sackdelay =
2145 msecs_to_jiffies(params->spp_sackdelay);
2146 } else if (asoc) {
2147 asoc->sackdelay =
2148 msecs_to_jiffies(params->spp_sackdelay);
2149 } else {
2150 sp->sackdelay = params->spp_sackdelay;
2151 }
2152 }
2153
2154 if (sackdelay_change) {
2155 if (trans) {
2156 trans->param_flags =
2157 (trans->param_flags & ~SPP_SACKDELAY) |
2158 sackdelay_change;
2159 } else if (asoc) {
2160 asoc->param_flags =
2161 (asoc->param_flags & ~SPP_SACKDELAY) |
2162 sackdelay_change;
2163 } else {
2164 sp->param_flags =
2165 (sp->param_flags & ~SPP_SACKDELAY) |
2166 sackdelay_change;
2167 }
2168 }
2169
2170 if (params->spp_pathmaxrxt) {
2171 if (trans) {
2172 trans->pathmaxrxt = params->spp_pathmaxrxt;
2173 } else if (asoc) {
2174 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2175 } else {
2176 sp->pathmaxrxt = params->spp_pathmaxrxt;
2177 }
2178 }
2179
2180 return 0;
2181}
2182
2183static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2184 char __user *optval, int optlen)
2185{
2186 struct sctp_paddrparams params;
2187 struct sctp_transport *trans = NULL;
2188 struct sctp_association *asoc = NULL;
2189 struct sctp_sock *sp = sctp_sk(sk);
2190 int error;
2191 int hb_change, pmtud_change, sackdelay_change;
2192
2193 if (optlen != sizeof(struct sctp_paddrparams))
2194 return - EINVAL;
2195
2196 if (copy_from_user(&params, optval, optlen))
2197 return -EFAULT;
2198
2199 /* Validate flags and value parameters. */
2200 hb_change = params.spp_flags & SPP_HB;
2201 pmtud_change = params.spp_flags & SPP_PMTUD;
2202 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2203
2204 if (hb_change == SPP_HB ||
2205 pmtud_change == SPP_PMTUD ||
2206 sackdelay_change == SPP_SACKDELAY ||
2207 params.spp_sackdelay > 500 ||
2208 (params.spp_pathmtu
2209 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2210 return -EINVAL;
2211
2212 /* If an address other than INADDR_ANY is specified, and
2213 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002215 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2216 trans = sctp_addr_id2transport(sk, &params.spp_address,
2217 params.spp_assoc_id);
2218 if (!trans)
2219 return -EINVAL;
2220 }
2221
2222 /* Get association, if assoc_id != 0 and the socket is a one
2223 * to many style socket, and an association was not found, then
2224 * the id was invalid.
2225 */
2226 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2227 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2228 return -EINVAL;
2229
2230 /* Heartbeat demand can only be sent on a transport or
2231 * association, but not a socket.
2232 */
2233 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2234 return -EINVAL;
2235
2236 /* Process parameters. */
2237 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2238 hb_change, pmtud_change,
2239 sackdelay_change);
2240
2241 if (error)
2242 return error;
2243
2244 /* If changes are for association, also apply parameters to each
2245 * transport.
2246 */
2247 if (!trans && asoc) {
2248 struct list_head *pos;
2249
2250 list_for_each(pos, &asoc->peer.transport_addr_list) {
2251 trans = list_entry(pos, struct sctp_transport,
2252 transports);
2253 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2254 hb_change, pmtud_change,
2255 sackdelay_change);
2256 }
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 return 0;
2260}
2261
Frank Filz77086102005-12-22 11:37:30 -08002262/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2263 *
2264 * This options will get or set the delayed ack timer. The time is set
2265 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2266 * endpoints default delayed ack timer value. If the assoc_id field is
2267 * non-zero, then the set or get effects the specified association.
2268 *
2269 * struct sctp_assoc_value {
2270 * sctp_assoc_t assoc_id;
2271 * uint32_t assoc_value;
2272 * };
2273 *
2274 * assoc_id - This parameter, indicates which association the
2275 * user is preforming an action upon. Note that if
2276 * this field's value is zero then the endpoints
2277 * default value is changed (effecting future
2278 * associations only).
2279 *
2280 * assoc_value - This parameter contains the number of milliseconds
2281 * that the user is requesting the delayed ACK timer
2282 * be set to. Note that this value is defined in
2283 * the standard to be between 200 and 500 milliseconds.
2284 *
2285 * Note: a value of zero will leave the value alone,
2286 * but disable SACK delay. A non-zero value will also
2287 * enable SACK delay.
2288 */
2289
2290static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2291 char __user *optval, int optlen)
2292{
2293 struct sctp_assoc_value params;
2294 struct sctp_transport *trans = NULL;
2295 struct sctp_association *asoc = NULL;
2296 struct sctp_sock *sp = sctp_sk(sk);
2297
2298 if (optlen != sizeof(struct sctp_assoc_value))
2299 return - EINVAL;
2300
2301 if (copy_from_user(&params, optval, optlen))
2302 return -EFAULT;
2303
2304 /* Validate value parameter. */
2305 if (params.assoc_value > 500)
2306 return -EINVAL;
2307
2308 /* Get association, if assoc_id != 0 and the socket is a one
2309 * to many style socket, and an association was not found, then
2310 * the id was invalid.
2311 */
2312 asoc = sctp_id2assoc(sk, params.assoc_id);
2313 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2314 return -EINVAL;
2315
2316 if (params.assoc_value) {
2317 if (asoc) {
2318 asoc->sackdelay =
2319 msecs_to_jiffies(params.assoc_value);
2320 asoc->param_flags =
2321 (asoc->param_flags & ~SPP_SACKDELAY) |
2322 SPP_SACKDELAY_ENABLE;
2323 } else {
2324 sp->sackdelay = params.assoc_value;
2325 sp->param_flags =
2326 (sp->param_flags & ~SPP_SACKDELAY) |
2327 SPP_SACKDELAY_ENABLE;
2328 }
2329 } else {
2330 if (asoc) {
2331 asoc->param_flags =
2332 (asoc->param_flags & ~SPP_SACKDELAY) |
2333 SPP_SACKDELAY_DISABLE;
2334 } else {
2335 sp->param_flags =
2336 (sp->param_flags & ~SPP_SACKDELAY) |
2337 SPP_SACKDELAY_DISABLE;
2338 }
2339 }
2340
2341 /* If change is for association, also apply to each transport. */
2342 if (asoc) {
2343 struct list_head *pos;
2344
2345 list_for_each(pos, &asoc->peer.transport_addr_list) {
2346 trans = list_entry(pos, struct sctp_transport,
2347 transports);
2348 if (params.assoc_value) {
2349 trans->sackdelay =
2350 msecs_to_jiffies(params.assoc_value);
2351 trans->param_flags =
2352 (trans->param_flags & ~SPP_SACKDELAY) |
2353 SPP_SACKDELAY_ENABLE;
2354 } else {
2355 trans->param_flags =
2356 (trans->param_flags & ~SPP_SACKDELAY) |
2357 SPP_SACKDELAY_DISABLE;
2358 }
2359 }
2360 }
2361
2362 return 0;
2363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2366 *
2367 * Applications can specify protocol parameters for the default association
2368 * initialization. The option name argument to setsockopt() and getsockopt()
2369 * is SCTP_INITMSG.
2370 *
2371 * Setting initialization parameters is effective only on an unconnected
2372 * socket (for UDP-style sockets only future associations are effected
2373 * by the change). With TCP-style sockets, this option is inherited by
2374 * sockets derived from a listener socket.
2375 */
2376static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2377{
2378 struct sctp_initmsg sinit;
2379 struct sctp_sock *sp = sctp_sk(sk);
2380
2381 if (optlen != sizeof(struct sctp_initmsg))
2382 return -EINVAL;
2383 if (copy_from_user(&sinit, optval, optlen))
2384 return -EFAULT;
2385
2386 if (sinit.sinit_num_ostreams)
2387 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2388 if (sinit.sinit_max_instreams)
2389 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2390 if (sinit.sinit_max_attempts)
2391 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2392 if (sinit.sinit_max_init_timeo)
2393 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2394
2395 return 0;
2396}
2397
2398/*
2399 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2400 *
2401 * Applications that wish to use the sendto() system call may wish to
2402 * specify a default set of parameters that would normally be supplied
2403 * through the inclusion of ancillary data. This socket option allows
2404 * such an application to set the default sctp_sndrcvinfo structure.
2405 * The application that wishes to use this socket option simply passes
2406 * in to this call the sctp_sndrcvinfo structure defined in Section
2407 * 5.2.2) The input parameters accepted by this call include
2408 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2409 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2410 * to this call if the caller is using the UDP model.
2411 */
2412static int sctp_setsockopt_default_send_param(struct sock *sk,
2413 char __user *optval, int optlen)
2414{
2415 struct sctp_sndrcvinfo info;
2416 struct sctp_association *asoc;
2417 struct sctp_sock *sp = sctp_sk(sk);
2418
2419 if (optlen != sizeof(struct sctp_sndrcvinfo))
2420 return -EINVAL;
2421 if (copy_from_user(&info, optval, optlen))
2422 return -EFAULT;
2423
2424 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2425 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2426 return -EINVAL;
2427
2428 if (asoc) {
2429 asoc->default_stream = info.sinfo_stream;
2430 asoc->default_flags = info.sinfo_flags;
2431 asoc->default_ppid = info.sinfo_ppid;
2432 asoc->default_context = info.sinfo_context;
2433 asoc->default_timetolive = info.sinfo_timetolive;
2434 } else {
2435 sp->default_stream = info.sinfo_stream;
2436 sp->default_flags = info.sinfo_flags;
2437 sp->default_ppid = info.sinfo_ppid;
2438 sp->default_context = info.sinfo_context;
2439 sp->default_timetolive = info.sinfo_timetolive;
2440 }
2441
2442 return 0;
2443}
2444
2445/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2446 *
2447 * Requests that the local SCTP stack use the enclosed peer address as
2448 * the association primary. The enclosed address must be one of the
2449 * association peer's addresses.
2450 */
2451static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2452 int optlen)
2453{
2454 struct sctp_prim prim;
2455 struct sctp_transport *trans;
2456
2457 if (optlen != sizeof(struct sctp_prim))
2458 return -EINVAL;
2459
2460 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2461 return -EFAULT;
2462
2463 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2464 if (!trans)
2465 return -EINVAL;
2466
2467 sctp_assoc_set_primary(trans->asoc, trans);
2468
2469 return 0;
2470}
2471
2472/*
2473 * 7.1.5 SCTP_NODELAY
2474 *
2475 * Turn on/off any Nagle-like algorithm. This means that packets are
2476 * generally sent as soon as possible and no unnecessary delays are
2477 * introduced, at the cost of more packets in the network. Expects an
2478 * integer boolean flag.
2479 */
2480static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2481 int optlen)
2482{
2483 int val;
2484
2485 if (optlen < sizeof(int))
2486 return -EINVAL;
2487 if (get_user(val, (int __user *)optval))
2488 return -EFAULT;
2489
2490 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2491 return 0;
2492}
2493
2494/*
2495 *
2496 * 7.1.1 SCTP_RTOINFO
2497 *
2498 * The protocol parameters used to initialize and bound retransmission
2499 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2500 * and modify these parameters.
2501 * All parameters are time values, in milliseconds. A value of 0, when
2502 * modifying the parameters, indicates that the current value should not
2503 * be changed.
2504 *
2505 */
2506static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2507 struct sctp_rtoinfo rtoinfo;
2508 struct sctp_association *asoc;
2509
2510 if (optlen != sizeof (struct sctp_rtoinfo))
2511 return -EINVAL;
2512
2513 if (copy_from_user(&rtoinfo, optval, optlen))
2514 return -EFAULT;
2515
2516 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2517
2518 /* Set the values to the specific association */
2519 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2520 return -EINVAL;
2521
2522 if (asoc) {
2523 if (rtoinfo.srto_initial != 0)
2524 asoc->rto_initial =
2525 msecs_to_jiffies(rtoinfo.srto_initial);
2526 if (rtoinfo.srto_max != 0)
2527 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2528 if (rtoinfo.srto_min != 0)
2529 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2530 } else {
2531 /* If there is no association or the association-id = 0
2532 * set the values to the endpoint.
2533 */
2534 struct sctp_sock *sp = sctp_sk(sk);
2535
2536 if (rtoinfo.srto_initial != 0)
2537 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2538 if (rtoinfo.srto_max != 0)
2539 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2540 if (rtoinfo.srto_min != 0)
2541 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2542 }
2543
2544 return 0;
2545}
2546
2547/*
2548 *
2549 * 7.1.2 SCTP_ASSOCINFO
2550 *
2551 * This option is used to tune the the maximum retransmission attempts
2552 * of the association.
2553 * Returns an error if the new association retransmission value is
2554 * greater than the sum of the retransmission value of the peer.
2555 * See [SCTP] for more information.
2556 *
2557 */
2558static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2559{
2560
2561 struct sctp_assocparams assocparams;
2562 struct sctp_association *asoc;
2563
2564 if (optlen != sizeof(struct sctp_assocparams))
2565 return -EINVAL;
2566 if (copy_from_user(&assocparams, optval, optlen))
2567 return -EFAULT;
2568
2569 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2570
2571 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2572 return -EINVAL;
2573
2574 /* Set the values to the specific association */
2575 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002576 if (assocparams.sasoc_asocmaxrxt != 0) {
2577 __u32 path_sum = 0;
2578 int paths = 0;
2579 struct list_head *pos;
2580 struct sctp_transport *peer_addr;
2581
2582 list_for_each(pos, &asoc->peer.transport_addr_list) {
2583 peer_addr = list_entry(pos,
2584 struct sctp_transport,
2585 transports);
2586 path_sum += peer_addr->pathmaxrxt;
2587 paths++;
2588 }
2589
2590 /* Only validate asocmaxrxt if we have more then
2591 * one path/transport. We do this because path
2592 * retransmissions are only counted when we have more
2593 * then one path.
2594 */
2595 if (paths > 1 &&
2596 assocparams.sasoc_asocmaxrxt > path_sum)
2597 return -EINVAL;
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002600 }
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (assocparams.sasoc_cookie_life != 0) {
2603 asoc->cookie_life.tv_sec =
2604 assocparams.sasoc_cookie_life / 1000;
2605 asoc->cookie_life.tv_usec =
2606 (assocparams.sasoc_cookie_life % 1000)
2607 * 1000;
2608 }
2609 } else {
2610 /* Set the values to the endpoint */
2611 struct sctp_sock *sp = sctp_sk(sk);
2612
2613 if (assocparams.sasoc_asocmaxrxt != 0)
2614 sp->assocparams.sasoc_asocmaxrxt =
2615 assocparams.sasoc_asocmaxrxt;
2616 if (assocparams.sasoc_cookie_life != 0)
2617 sp->assocparams.sasoc_cookie_life =
2618 assocparams.sasoc_cookie_life;
2619 }
2620 return 0;
2621}
2622
2623/*
2624 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2625 *
2626 * This socket option is a boolean flag which turns on or off mapped V4
2627 * addresses. If this option is turned on and the socket is type
2628 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2629 * If this option is turned off, then no mapping will be done of V4
2630 * addresses and a user will receive both PF_INET6 and PF_INET type
2631 * addresses on the socket.
2632 */
2633static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2634{
2635 int val;
2636 struct sctp_sock *sp = sctp_sk(sk);
2637
2638 if (optlen < sizeof(int))
2639 return -EINVAL;
2640 if (get_user(val, (int __user *)optval))
2641 return -EFAULT;
2642 if (val)
2643 sp->v4mapped = 1;
2644 else
2645 sp->v4mapped = 0;
2646
2647 return 0;
2648}
2649
2650/*
2651 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2652 *
2653 * This socket option specifies the maximum size to put in any outgoing
2654 * SCTP chunk. If a message is larger than this size it will be
2655 * fragmented by SCTP into the specified size. Note that the underlying
2656 * SCTP implementation may fragment into smaller sized chunks when the
2657 * PMTU of the underlying association is smaller than the value set by
2658 * the user.
2659 */
2660static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2661{
2662 struct sctp_association *asoc;
2663 struct list_head *pos;
2664 struct sctp_sock *sp = sctp_sk(sk);
2665 int val;
2666
2667 if (optlen < sizeof(int))
2668 return -EINVAL;
2669 if (get_user(val, (int __user *)optval))
2670 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002671 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return -EINVAL;
2673 sp->user_frag = val;
2674
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002675 /* Update the frag_point of the existing associations. */
2676 list_for_each(pos, &(sp->ep->asocs)) {
2677 asoc = list_entry(pos, struct sctp_association, asocs);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002678 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680
2681 return 0;
2682}
2683
2684
2685/*
2686 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2687 *
2688 * Requests that the peer mark the enclosed address as the association
2689 * primary. The enclosed address must be one of the association's
2690 * locally bound addresses. The following structure is used to make a
2691 * set primary request:
2692 */
2693static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2694 int optlen)
2695{
2696 struct sctp_sock *sp;
2697 struct sctp_endpoint *ep;
2698 struct sctp_association *asoc = NULL;
2699 struct sctp_setpeerprim prim;
2700 struct sctp_chunk *chunk;
2701 int err;
2702
2703 sp = sctp_sk(sk);
2704 ep = sp->ep;
2705
2706 if (!sctp_addip_enable)
2707 return -EPERM;
2708
2709 if (optlen != sizeof(struct sctp_setpeerprim))
2710 return -EINVAL;
2711
2712 if (copy_from_user(&prim, optval, optlen))
2713 return -EFAULT;
2714
2715 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2716 if (!asoc)
2717 return -EINVAL;
2718
2719 if (!asoc->peer.asconf_capable)
2720 return -EPERM;
2721
2722 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2723 return -EPERM;
2724
2725 if (!sctp_state(asoc, ESTABLISHED))
2726 return -ENOTCONN;
2727
2728 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2729 return -EADDRNOTAVAIL;
2730
2731 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2732 chunk = sctp_make_asconf_set_prim(asoc,
2733 (union sctp_addr *)&prim.sspp_addr);
2734 if (!chunk)
2735 return -ENOMEM;
2736
2737 err = sctp_send_asconf(asoc, chunk);
2738
2739 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2740
2741 return err;
2742}
2743
2744static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2745 int optlen)
2746{
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002747 struct sctp_setadaption adaption;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002749 if (optlen != sizeof(struct sctp_setadaption))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return -EINVAL;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002751 if (copy_from_user(&adaption, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return -EFAULT;
2753
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07002754 sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 return 0;
2757}
2758
2759/* API 6.2 setsockopt(), getsockopt()
2760 *
2761 * Applications use setsockopt() and getsockopt() to set or retrieve
2762 * socket options. Socket options are used to change the default
2763 * behavior of sockets calls. They are described in Section 7.
2764 *
2765 * The syntax is:
2766 *
2767 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2768 * int __user *optlen);
2769 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2770 * int optlen);
2771 *
2772 * sd - the socket descript.
2773 * level - set to IPPROTO_SCTP for all SCTP options.
2774 * optname - the option name.
2775 * optval - the buffer to store the value of the option.
2776 * optlen - the size of the buffer.
2777 */
2778SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2779 char __user *optval, int optlen)
2780{
2781 int retval = 0;
2782
2783 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2784 sk, optname);
2785
2786 /* I can hardly begin to describe how wrong this is. This is
2787 * so broken as to be worse than useless. The API draft
2788 * REALLY is NOT helpful here... I am not convinced that the
2789 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2790 * are at all well-founded.
2791 */
2792 if (level != SOL_SCTP) {
2793 struct sctp_af *af = sctp_sk(sk)->pf->af;
2794 retval = af->setsockopt(sk, level, optname, optval, optlen);
2795 goto out_nounlock;
2796 }
2797
2798 sctp_lock_sock(sk);
2799
2800 switch (optname) {
2801 case SCTP_SOCKOPT_BINDX_ADD:
2802 /* 'optlen' is the size of the addresses buffer. */
2803 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2804 optlen, SCTP_BINDX_ADD_ADDR);
2805 break;
2806
2807 case SCTP_SOCKOPT_BINDX_REM:
2808 /* 'optlen' is the size of the addresses buffer. */
2809 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2810 optlen, SCTP_BINDX_REM_ADDR);
2811 break;
2812
Frank Filz3f7a87d2005-06-20 13:14:57 -07002813 case SCTP_SOCKOPT_CONNECTX:
2814 /* 'optlen' is the size of the addresses buffer. */
2815 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2816 optlen);
2817 break;
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 case SCTP_DISABLE_FRAGMENTS:
2820 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2821 break;
2822
2823 case SCTP_EVENTS:
2824 retval = sctp_setsockopt_events(sk, optval, optlen);
2825 break;
2826
2827 case SCTP_AUTOCLOSE:
2828 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2829 break;
2830
2831 case SCTP_PEER_ADDR_PARAMS:
2832 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2833 break;
2834
Frank Filz77086102005-12-22 11:37:30 -08002835 case SCTP_DELAYED_ACK_TIME:
2836 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2837 break;
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 case SCTP_INITMSG:
2840 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2841 break;
2842 case SCTP_DEFAULT_SEND_PARAM:
2843 retval = sctp_setsockopt_default_send_param(sk, optval,
2844 optlen);
2845 break;
2846 case SCTP_PRIMARY_ADDR:
2847 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2848 break;
2849 case SCTP_SET_PEER_PRIMARY_ADDR:
2850 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2851 break;
2852 case SCTP_NODELAY:
2853 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2854 break;
2855 case SCTP_RTOINFO:
2856 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2857 break;
2858 case SCTP_ASSOCINFO:
2859 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2860 break;
2861 case SCTP_I_WANT_MAPPED_V4_ADDR:
2862 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2863 break;
2864 case SCTP_MAXSEG:
2865 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2866 break;
2867 case SCTP_ADAPTION_LAYER:
2868 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2869 break;
2870
2871 default:
2872 retval = -ENOPROTOOPT;
2873 break;
2874 };
2875
2876 sctp_release_sock(sk);
2877
2878out_nounlock:
2879 return retval;
2880}
2881
2882/* API 3.1.6 connect() - UDP Style Syntax
2883 *
2884 * An application may use the connect() call in the UDP model to initiate an
2885 * association without sending data.
2886 *
2887 * The syntax is:
2888 *
2889 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2890 *
2891 * sd: the socket descriptor to have a new association added to.
2892 *
2893 * nam: the address structure (either struct sockaddr_in or struct
2894 * sockaddr_in6 defined in RFC2553 [7]).
2895 *
2896 * len: the size of the address.
2897 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002898SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 int addr_len)
2900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002902 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 sctp_lock_sock(sk);
2905
Frank Filz3f7a87d2005-06-20 13:14:57 -07002906 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2907 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Frank Filz3f7a87d2005-06-20 13:14:57 -07002909 /* Validate addr_len before calling common connect/connectx routine. */
2910 af = sctp_get_af_specific(addr->sa_family);
2911 if (!af || addr_len < af->sockaddr_len) {
2912 err = -EINVAL;
2913 } else {
2914 /* Pass correct addr len to common routine (so it knows there
2915 * is only one address being passed.
2916 */
2917 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return err;
2922}
2923
2924/* FIXME: Write comments. */
2925SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2926{
2927 return -EOPNOTSUPP; /* STUB */
2928}
2929
2930/* 4.1.4 accept() - TCP Style Syntax
2931 *
2932 * Applications use accept() call to remove an established SCTP
2933 * association from the accept queue of the endpoint. A new socket
2934 * descriptor will be returned from accept() to represent the newly
2935 * formed association.
2936 */
2937SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2938{
2939 struct sctp_sock *sp;
2940 struct sctp_endpoint *ep;
2941 struct sock *newsk = NULL;
2942 struct sctp_association *asoc;
2943 long timeo;
2944 int error = 0;
2945
2946 sctp_lock_sock(sk);
2947
2948 sp = sctp_sk(sk);
2949 ep = sp->ep;
2950
2951 if (!sctp_style(sk, TCP)) {
2952 error = -EOPNOTSUPP;
2953 goto out;
2954 }
2955
2956 if (!sctp_sstate(sk, LISTENING)) {
2957 error = -EINVAL;
2958 goto out;
2959 }
2960
2961 timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2962
2963 error = sctp_wait_for_accept(sk, timeo);
2964 if (error)
2965 goto out;
2966
2967 /* We treat the list of associations on the endpoint as the accept
2968 * queue and pick the first association on the list.
2969 */
2970 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2971
2972 newsk = sp->pf->create_accept_sk(sk, asoc);
2973 if (!newsk) {
2974 error = -ENOMEM;
2975 goto out;
2976 }
2977
2978 /* Populate the fields of the newsk from the oldsk and migrate the
2979 * asoc to the newsk.
2980 */
2981 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2982
2983out:
2984 sctp_release_sock(sk);
2985 *err = error;
2986 return newsk;
2987}
2988
2989/* The SCTP ioctl handler. */
2990SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2991{
2992 return -ENOIOCTLCMD;
2993}
2994
2995/* This is the function which gets called during socket creation to
2996 * initialized the SCTP-specific portion of the sock.
2997 * The sock structure should already be zero-filled memory.
2998 */
2999SCTP_STATIC int sctp_init_sock(struct sock *sk)
3000{
3001 struct sctp_endpoint *ep;
3002 struct sctp_sock *sp;
3003
3004 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3005
3006 sp = sctp_sk(sk);
3007
3008 /* Initialize the SCTP per socket area. */
3009 switch (sk->sk_type) {
3010 case SOCK_SEQPACKET:
3011 sp->type = SCTP_SOCKET_UDP;
3012 break;
3013 case SOCK_STREAM:
3014 sp->type = SCTP_SOCKET_TCP;
3015 break;
3016 default:
3017 return -ESOCKTNOSUPPORT;
3018 }
3019
3020 /* Initialize default send parameters. These parameters can be
3021 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3022 */
3023 sp->default_stream = 0;
3024 sp->default_ppid = 0;
3025 sp->default_flags = 0;
3026 sp->default_context = 0;
3027 sp->default_timetolive = 0;
3028
3029 /* Initialize default setup parameters. These parameters
3030 * can be modified with the SCTP_INITMSG socket option or
3031 * overridden by the SCTP_INIT CMSG.
3032 */
3033 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3034 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3035 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
3036 sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
3037
3038 /* Initialize default RTO related parameters. These parameters can
3039 * be modified for with the SCTP_RTOINFO socket option.
3040 */
3041 sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
3042 sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
3043 sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
3044
3045 /* Initialize default association related parameters. These parameters
3046 * can be modified with the SCTP_ASSOCINFO socket option.
3047 */
3048 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3049 sp->assocparams.sasoc_number_peer_destinations = 0;
3050 sp->assocparams.sasoc_peer_rwnd = 0;
3051 sp->assocparams.sasoc_local_rwnd = 0;
3052 sp->assocparams.sasoc_cookie_life =
3053 jiffies_to_msecs(sctp_valid_cookie_life);
3054
3055 /* Initialize default event subscriptions. By default, all the
3056 * options are off.
3057 */
3058 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3059
3060 /* Default Peer Address Parameters. These defaults can
3061 * be modified via SCTP_PEER_ADDR_PARAMS
3062 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003063 sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
3064 sp->pathmaxrxt = sctp_max_retrans_path;
3065 sp->pathmtu = 0; // allow default discovery
Vlad Yasevich8116ffa2006-01-17 11:55:17 -08003066 sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003067 sp->param_flags = SPP_HB_ENABLE |
3068 SPP_PMTUD_ENABLE |
3069 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 /* If enabled no SCTP message fragmentation will be performed.
3072 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3073 */
3074 sp->disable_fragments = 0;
3075
3076 /* Turn on/off any Nagle-like algorithm. */
3077 sp->nodelay = 1;
3078
3079 /* Enable by default. */
3080 sp->v4mapped = 1;
3081
3082 /* Auto-close idle associations after the configured
3083 * number of seconds. A value of 0 disables this
3084 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3085 * for UDP-style sockets only.
3086 */
3087 sp->autoclose = 0;
3088
3089 /* User specified fragmentation limit. */
3090 sp->user_frag = 0;
3091
3092 sp->adaption_ind = 0;
3093
3094 sp->pf = sctp_get_pf_specific(sk->sk_family);
3095
3096 /* Control variables for partial data delivery. */
3097 sp->pd_mode = 0;
3098 skb_queue_head_init(&sp->pd_lobby);
3099
3100 /* Create a per socket endpoint structure. Even if we
3101 * change the data structure relationships, this may still
3102 * be useful for storing pre-connect address information.
3103 */
3104 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3105 if (!ep)
3106 return -ENOMEM;
3107
3108 sp->ep = ep;
3109 sp->hmac = NULL;
3110
3111 SCTP_DBG_OBJCNT_INC(sock);
3112 return 0;
3113}
3114
3115/* Cleanup any SCTP per socket resources. */
3116SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3117{
3118 struct sctp_endpoint *ep;
3119
3120 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3121
3122 /* Release our hold on the endpoint. */
3123 ep = sctp_sk(sk)->ep;
3124 sctp_endpoint_free(ep);
3125
3126 return 0;
3127}
3128
3129/* API 4.1.7 shutdown() - TCP Style Syntax
3130 * int shutdown(int socket, int how);
3131 *
3132 * sd - the socket descriptor of the association to be closed.
3133 * how - Specifies the type of shutdown. The values are
3134 * as follows:
3135 * SHUT_RD
3136 * Disables further receive operations. No SCTP
3137 * protocol action is taken.
3138 * SHUT_WR
3139 * Disables further send operations, and initiates
3140 * the SCTP shutdown sequence.
3141 * SHUT_RDWR
3142 * Disables further send and receive operations
3143 * and initiates the SCTP shutdown sequence.
3144 */
3145SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3146{
3147 struct sctp_endpoint *ep;
3148 struct sctp_association *asoc;
3149
3150 if (!sctp_style(sk, TCP))
3151 return;
3152
3153 if (how & SEND_SHUTDOWN) {
3154 ep = sctp_sk(sk)->ep;
3155 if (!list_empty(&ep->asocs)) {
3156 asoc = list_entry(ep->asocs.next,
3157 struct sctp_association, asocs);
3158 sctp_primitive_SHUTDOWN(asoc, NULL);
3159 }
3160 }
3161}
3162
3163/* 7.2.1 Association Status (SCTP_STATUS)
3164
3165 * Applications can retrieve current status information about an
3166 * association, including association state, peer receiver window size,
3167 * number of unacked data chunks, and number of data chunks pending
3168 * receipt. This information is read-only.
3169 */
3170static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3171 char __user *optval,
3172 int __user *optlen)
3173{
3174 struct sctp_status status;
3175 struct sctp_association *asoc = NULL;
3176 struct sctp_transport *transport;
3177 sctp_assoc_t associd;
3178 int retval = 0;
3179
3180 if (len != sizeof(status)) {
3181 retval = -EINVAL;
3182 goto out;
3183 }
3184
3185 if (copy_from_user(&status, optval, sizeof(status))) {
3186 retval = -EFAULT;
3187 goto out;
3188 }
3189
3190 associd = status.sstat_assoc_id;
3191 asoc = sctp_id2assoc(sk, associd);
3192 if (!asoc) {
3193 retval = -EINVAL;
3194 goto out;
3195 }
3196
3197 transport = asoc->peer.primary_path;
3198
3199 status.sstat_assoc_id = sctp_assoc2id(asoc);
3200 status.sstat_state = asoc->state;
3201 status.sstat_rwnd = asoc->peer.rwnd;
3202 status.sstat_unackdata = asoc->unack_data;
3203
3204 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3205 status.sstat_instrms = asoc->c.sinit_max_instreams;
3206 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3207 status.sstat_fragmentation_point = asoc->frag_point;
3208 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3209 memcpy(&status.sstat_primary.spinfo_address,
3210 &(transport->ipaddr), sizeof(union sctp_addr));
3211 /* Map ipv4 address into v4-mapped-on-v6 address. */
3212 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3213 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003214 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3216 status.sstat_primary.spinfo_srtt = transport->srtt;
3217 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003218 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Frank Filz3f7a87d2005-06-20 13:14:57 -07003220 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3221 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (put_user(len, optlen)) {
3224 retval = -EFAULT;
3225 goto out;
3226 }
3227
3228 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3229 len, status.sstat_state, status.sstat_rwnd,
3230 status.sstat_assoc_id);
3231
3232 if (copy_to_user(optval, &status, len)) {
3233 retval = -EFAULT;
3234 goto out;
3235 }
3236
3237out:
3238 return (retval);
3239}
3240
3241
3242/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3243 *
3244 * Applications can retrieve information about a specific peer address
3245 * of an association, including its reachability state, congestion
3246 * window, and retransmission timer values. This information is
3247 * read-only.
3248 */
3249static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3250 char __user *optval,
3251 int __user *optlen)
3252{
3253 struct sctp_paddrinfo pinfo;
3254 struct sctp_transport *transport;
3255 int retval = 0;
3256
3257 if (len != sizeof(pinfo)) {
3258 retval = -EINVAL;
3259 goto out;
3260 }
3261
3262 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3263 retval = -EFAULT;
3264 goto out;
3265 }
3266
3267 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3268 pinfo.spinfo_assoc_id);
3269 if (!transport)
3270 return -EINVAL;
3271
3272 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003273 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 pinfo.spinfo_cwnd = transport->cwnd;
3275 pinfo.spinfo_srtt = transport->srtt;
3276 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003277 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Frank Filz3f7a87d2005-06-20 13:14:57 -07003279 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3280 pinfo.spinfo_state = SCTP_ACTIVE;
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (put_user(len, optlen)) {
3283 retval = -EFAULT;
3284 goto out;
3285 }
3286
3287 if (copy_to_user(optval, &pinfo, len)) {
3288 retval = -EFAULT;
3289 goto out;
3290 }
3291
3292out:
3293 return (retval);
3294}
3295
3296/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3297 *
3298 * This option is a on/off flag. If enabled no SCTP message
3299 * fragmentation will be performed. Instead if a message being sent
3300 * exceeds the current PMTU size, the message will NOT be sent and
3301 * instead a error will be indicated to the user.
3302 */
3303static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3304 char __user *optval, int __user *optlen)
3305{
3306 int val;
3307
3308 if (len < sizeof(int))
3309 return -EINVAL;
3310
3311 len = sizeof(int);
3312 val = (sctp_sk(sk)->disable_fragments == 1);
3313 if (put_user(len, optlen))
3314 return -EFAULT;
3315 if (copy_to_user(optval, &val, len))
3316 return -EFAULT;
3317 return 0;
3318}
3319
3320/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3321 *
3322 * This socket option is used to specify various notifications and
3323 * ancillary data the user wishes to receive.
3324 */
3325static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3326 int __user *optlen)
3327{
3328 if (len != sizeof(struct sctp_event_subscribe))
3329 return -EINVAL;
3330 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3331 return -EFAULT;
3332 return 0;
3333}
3334
3335/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3336 *
3337 * This socket option is applicable to the UDP-style socket only. When
3338 * set it will cause associations that are idle for more than the
3339 * specified number of seconds to automatically close. An association
3340 * being idle is defined an association that has NOT sent or received
3341 * user data. The special value of '0' indicates that no automatic
3342 * close of any associations should be performed. The option expects an
3343 * integer defining the number of seconds of idle time before an
3344 * association is closed.
3345 */
3346static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3347{
3348 /* Applicable to UDP-style socket only */
3349 if (sctp_style(sk, TCP))
3350 return -EOPNOTSUPP;
3351 if (len != sizeof(int))
3352 return -EINVAL;
3353 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3354 return -EFAULT;
3355 return 0;
3356}
3357
3358/* Helper routine to branch off an association to a new socket. */
3359SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3360 struct socket **sockp)
3361{
3362 struct sock *sk = asoc->base.sk;
3363 struct socket *sock;
3364 int err = 0;
3365
3366 /* An association cannot be branched off from an already peeled-off
3367 * socket, nor is this supported for tcp style sockets.
3368 */
3369 if (!sctp_style(sk, UDP))
3370 return -EINVAL;
3371
3372 /* Create a new socket. */
3373 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3374 if (err < 0)
3375 return err;
3376
3377 /* Populate the fields of the newsk from the oldsk and migrate the
3378 * asoc to the newsk.
3379 */
3380 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3381 *sockp = sock;
3382
3383 return err;
3384}
3385
3386static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3387{
3388 sctp_peeloff_arg_t peeloff;
3389 struct socket *newsock;
3390 int retval = 0;
3391 struct sctp_association *asoc;
3392
3393 if (len != sizeof(sctp_peeloff_arg_t))
3394 return -EINVAL;
3395 if (copy_from_user(&peeloff, optval, len))
3396 return -EFAULT;
3397
3398 asoc = sctp_id2assoc(sk, peeloff.associd);
3399 if (!asoc) {
3400 retval = -EINVAL;
3401 goto out;
3402 }
3403
3404 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3405
3406 retval = sctp_do_peeloff(asoc, &newsock);
3407 if (retval < 0)
3408 goto out;
3409
3410 /* Map the socket to an unused fd that can be returned to the user. */
3411 retval = sock_map_fd(newsock);
3412 if (retval < 0) {
3413 sock_release(newsock);
3414 goto out;
3415 }
3416
3417 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3418 __FUNCTION__, sk, asoc, newsock->sk, retval);
3419
3420 /* Return the fd mapped to the new socket. */
3421 peeloff.sd = retval;
3422 if (copy_to_user(optval, &peeloff, len))
3423 retval = -EFAULT;
3424
3425out:
3426 return retval;
3427}
3428
3429/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3430 *
3431 * Applications can enable or disable heartbeats for any peer address of
3432 * an association, modify an address's heartbeat interval, force a
3433 * heartbeat to be sent immediately, and adjust the address's maximum
3434 * number of retransmissions sent before an address is considered
3435 * unreachable. The following structure is used to access and modify an
3436 * address's parameters:
3437 *
3438 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003439 * sctp_assoc_t spp_assoc_id;
3440 * struct sockaddr_storage spp_address;
3441 * uint32_t spp_hbinterval;
3442 * uint16_t spp_pathmaxrxt;
3443 * uint32_t spp_pathmtu;
3444 * uint32_t spp_sackdelay;
3445 * uint32_t spp_flags;
3446 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003448 * spp_assoc_id - (one-to-many style socket) This is filled in the
3449 * application, and identifies the association for
3450 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 * spp_address - This specifies which address is of interest.
3452 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003453 * in milliseconds. If a value of zero
3454 * is present in this field then no changes are to
3455 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 * spp_pathmaxrxt - This contains the maximum number of
3457 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003458 * considered unreachable. If a value of zero
3459 * is present in this field then no changes are to
3460 * be made to this parameter.
3461 * spp_pathmtu - When Path MTU discovery is disabled the value
3462 * specified here will be the "fixed" path mtu.
3463 * Note that if the spp_address field is empty
3464 * then all associations on this address will
3465 * have this fixed path mtu set upon them.
3466 *
3467 * spp_sackdelay - When delayed sack is enabled, this value specifies
3468 * the number of milliseconds that sacks will be delayed
3469 * for. This value will apply to all addresses of an
3470 * association if the spp_address field is empty. Note
3471 * also, that if delayed sack is enabled and this
3472 * value is set to 0, no change is made to the last
3473 * recorded delayed sack timer value.
3474 *
3475 * spp_flags - These flags are used to control various features
3476 * on an association. The flag field may contain
3477 * zero or more of the following options.
3478 *
3479 * SPP_HB_ENABLE - Enable heartbeats on the
3480 * specified address. Note that if the address
3481 * field is empty all addresses for the association
3482 * have heartbeats enabled upon them.
3483 *
3484 * SPP_HB_DISABLE - Disable heartbeats on the
3485 * speicifed address. Note that if the address
3486 * field is empty all addresses for the association
3487 * will have their heartbeats disabled. Note also
3488 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3489 * mutually exclusive, only one of these two should
3490 * be specified. Enabling both fields will have
3491 * undetermined results.
3492 *
3493 * SPP_HB_DEMAND - Request a user initiated heartbeat
3494 * to be made immediately.
3495 *
3496 * SPP_PMTUD_ENABLE - This field will enable PMTU
3497 * discovery upon the specified address. Note that
3498 * if the address feild is empty then all addresses
3499 * on the association are effected.
3500 *
3501 * SPP_PMTUD_DISABLE - This field will disable PMTU
3502 * discovery upon the specified address. Note that
3503 * if the address feild is empty then all addresses
3504 * on the association are effected. Not also that
3505 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3506 * exclusive. Enabling both will have undetermined
3507 * results.
3508 *
3509 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3510 * on delayed sack. The time specified in spp_sackdelay
3511 * is used to specify the sack delay for this address. Note
3512 * that if spp_address is empty then all addresses will
3513 * enable delayed sack and take on the sack delay
3514 * value specified in spp_sackdelay.
3515 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3516 * off delayed sack. If the spp_address field is blank then
3517 * delayed sack is disabled for the entire association. Note
3518 * also that this field is mutually exclusive to
3519 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3520 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 */
3522static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003523 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003525 struct sctp_paddrparams params;
3526 struct sctp_transport *trans = NULL;
3527 struct sctp_association *asoc = NULL;
3528 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 if (len != sizeof(struct sctp_paddrparams))
3531 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (copy_from_user(&params, optval, len))
3534 return -EFAULT;
3535
Frank Filz52ccb8e2005-12-22 11:36:46 -08003536 /* If an address other than INADDR_ANY is specified, and
3537 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003539 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3540 trans = sctp_addr_id2transport(sk, &params.spp_address,
3541 params.spp_assoc_id);
3542 if (!trans) {
3543 SCTP_DEBUG_PRINTK("Failed no transport\n");
3544 return -EINVAL;
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
3547
Frank Filz52ccb8e2005-12-22 11:36:46 -08003548 /* Get association, if assoc_id != 0 and the socket is a one
3549 * to many style socket, and an association was not found, then
3550 * the id was invalid.
3551 */
3552 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3553 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3554 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Frank Filz52ccb8e2005-12-22 11:36:46 -08003558 if (trans) {
3559 /* Fetch transport values. */
3560 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3561 params.spp_pathmtu = trans->pathmtu;
3562 params.spp_pathmaxrxt = trans->pathmaxrxt;
3563 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Frank Filz52ccb8e2005-12-22 11:36:46 -08003565 /*draft-11 doesn't say what to return in spp_flags*/
3566 params.spp_flags = trans->param_flags;
3567 } else if (asoc) {
3568 /* Fetch association values. */
3569 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3570 params.spp_pathmtu = asoc->pathmtu;
3571 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3572 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Frank Filz52ccb8e2005-12-22 11:36:46 -08003574 /*draft-11 doesn't say what to return in spp_flags*/
3575 params.spp_flags = asoc->param_flags;
3576 } else {
3577 /* Fetch socket values. */
3578 params.spp_hbinterval = sp->hbinterval;
3579 params.spp_pathmtu = sp->pathmtu;
3580 params.spp_sackdelay = sp->sackdelay;
3581 params.spp_pathmaxrxt = sp->pathmaxrxt;
3582
3583 /*draft-11 doesn't say what to return in spp_flags*/
3584 params.spp_flags = sp->param_flags;
3585 }
3586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (copy_to_user(optval, &params, len))
3588 return -EFAULT;
3589
3590 if (put_user(len, optlen))
3591 return -EFAULT;
3592
3593 return 0;
3594}
3595
Frank Filz77086102005-12-22 11:37:30 -08003596/* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3597 *
3598 * This options will get or set the delayed ack timer. The time is set
3599 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3600 * endpoints default delayed ack timer value. If the assoc_id field is
3601 * non-zero, then the set or get effects the specified association.
3602 *
3603 * struct sctp_assoc_value {
3604 * sctp_assoc_t assoc_id;
3605 * uint32_t assoc_value;
3606 * };
3607 *
3608 * assoc_id - This parameter, indicates which association the
3609 * user is preforming an action upon. Note that if
3610 * this field's value is zero then the endpoints
3611 * default value is changed (effecting future
3612 * associations only).
3613 *
3614 * assoc_value - This parameter contains the number of milliseconds
3615 * that the user is requesting the delayed ACK timer
3616 * be set to. Note that this value is defined in
3617 * the standard to be between 200 and 500 milliseconds.
3618 *
3619 * Note: a value of zero will leave the value alone,
3620 * but disable SACK delay. A non-zero value will also
3621 * enable SACK delay.
3622 */
3623static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3624 char __user *optval,
3625 int __user *optlen)
3626{
3627 struct sctp_assoc_value params;
3628 struct sctp_association *asoc = NULL;
3629 struct sctp_sock *sp = sctp_sk(sk);
3630
3631 if (len != sizeof(struct sctp_assoc_value))
3632 return - EINVAL;
3633
3634 if (copy_from_user(&params, optval, len))
3635 return -EFAULT;
3636
3637 /* Get association, if assoc_id != 0 and the socket is a one
3638 * to many style socket, and an association was not found, then
3639 * the id was invalid.
3640 */
3641 asoc = sctp_id2assoc(sk, params.assoc_id);
3642 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3643 return -EINVAL;
3644
3645 if (asoc) {
3646 /* Fetch association values. */
3647 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3648 params.assoc_value = jiffies_to_msecs(
3649 asoc->sackdelay);
3650 else
3651 params.assoc_value = 0;
3652 } else {
3653 /* Fetch socket values. */
3654 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3655 params.assoc_value = sp->sackdelay;
3656 else
3657 params.assoc_value = 0;
3658 }
3659
3660 if (copy_to_user(optval, &params, len))
3661 return -EFAULT;
3662
3663 if (put_user(len, optlen))
3664 return -EFAULT;
3665
3666 return 0;
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3670 *
3671 * Applications can specify protocol parameters for the default association
3672 * initialization. The option name argument to setsockopt() and getsockopt()
3673 * is SCTP_INITMSG.
3674 *
3675 * Setting initialization parameters is effective only on an unconnected
3676 * socket (for UDP-style sockets only future associations are effected
3677 * by the change). With TCP-style sockets, this option is inherited by
3678 * sockets derived from a listener socket.
3679 */
3680static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3681{
3682 if (len != sizeof(struct sctp_initmsg))
3683 return -EINVAL;
3684 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3685 return -EFAULT;
3686 return 0;
3687}
3688
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003689static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3690 char __user *optval,
3691 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 sctp_assoc_t id;
3694 struct sctp_association *asoc;
3695 struct list_head *pos;
3696 int cnt = 0;
3697
3698 if (len != sizeof(sctp_assoc_t))
3699 return -EINVAL;
3700
3701 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3702 return -EFAULT;
3703
3704 /* For UDP-style sockets, id specifies the association to query. */
3705 asoc = sctp_id2assoc(sk, id);
3706 if (!asoc)
3707 return -EINVAL;
3708
3709 list_for_each(pos, &asoc->peer.transport_addr_list) {
3710 cnt ++;
3711 }
3712
3713 return cnt;
3714}
3715
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003716/*
3717 * Old API for getting list of peer addresses. Does not work for 32-bit
3718 * programs running on a 64-bit kernel
3719 */
3720static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3721 char __user *optval,
3722 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 struct sctp_association *asoc;
3725 struct list_head *pos;
3726 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003727 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 struct sctp_transport *from;
3729 void __user *to;
3730 union sctp_addr temp;
3731 struct sctp_sock *sp = sctp_sk(sk);
3732 int addrlen;
3733
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003734 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return -EINVAL;
3736
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003737 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 return -EFAULT;
3739
3740 if (getaddrs.addr_num <= 0) return -EINVAL;
3741
3742 /* For UDP-style sockets, id specifies the association to query. */
3743 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3744 if (!asoc)
3745 return -EINVAL;
3746
3747 to = (void __user *)getaddrs.addrs;
3748 list_for_each(pos, &asoc->peer.transport_addr_list) {
3749 from = list_entry(pos, struct sctp_transport, transports);
3750 memcpy(&temp, &from->ipaddr, sizeof(temp));
3751 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3752 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3753 temp.v4.sin_port = htons(temp.v4.sin_port);
3754 if (copy_to_user(to, &temp, addrlen))
3755 return -EFAULT;
3756 to += addrlen ;
3757 cnt ++;
3758 if (cnt >= getaddrs.addr_num) break;
3759 }
3760 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003761 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return -EFAULT;
3763
3764 return 0;
3765}
3766
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003767static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3768 char __user *optval, int __user *optlen)
3769{
3770 struct sctp_association *asoc;
3771 struct list_head *pos;
3772 int cnt = 0;
3773 struct sctp_getaddrs getaddrs;
3774 struct sctp_transport *from;
3775 void __user *to;
3776 union sctp_addr temp;
3777 struct sctp_sock *sp = sctp_sk(sk);
3778 int addrlen;
3779 size_t space_left;
3780 int bytes_copied;
3781
3782 if (len < sizeof(struct sctp_getaddrs))
3783 return -EINVAL;
3784
3785 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3786 return -EFAULT;
3787
3788 /* For UDP-style sockets, id specifies the association to query. */
3789 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3790 if (!asoc)
3791 return -EINVAL;
3792
3793 to = optval + offsetof(struct sctp_getaddrs,addrs);
3794 space_left = len - sizeof(struct sctp_getaddrs) -
3795 offsetof(struct sctp_getaddrs,addrs);
3796
3797 list_for_each(pos, &asoc->peer.transport_addr_list) {
3798 from = list_entry(pos, struct sctp_transport, transports);
3799 memcpy(&temp, &from->ipaddr, sizeof(temp));
3800 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3801 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3802 if(space_left < addrlen)
3803 return -ENOMEM;
3804 temp.v4.sin_port = htons(temp.v4.sin_port);
3805 if (copy_to_user(to, &temp, addrlen))
3806 return -EFAULT;
3807 to += addrlen;
3808 cnt++;
3809 space_left -= addrlen;
3810 }
3811
3812 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3813 return -EFAULT;
3814 bytes_copied = ((char __user *)to) - optval;
3815 if (put_user(bytes_copied, optlen))
3816 return -EFAULT;
3817
3818 return 0;
3819}
3820
3821static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3822 char __user *optval,
3823 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 sctp_assoc_t id;
3826 struct sctp_bind_addr *bp;
3827 struct sctp_association *asoc;
3828 struct list_head *pos;
3829 struct sctp_sockaddr_entry *addr;
3830 rwlock_t *addr_lock;
3831 unsigned long flags;
3832 int cnt = 0;
3833
3834 if (len != sizeof(sctp_assoc_t))
3835 return -EINVAL;
3836
3837 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3838 return -EFAULT;
3839
3840 /*
3841 * For UDP-style sockets, id specifies the association to query.
3842 * If the id field is set to the value '0' then the locally bound
3843 * addresses are returned without regard to any particular
3844 * association.
3845 */
3846 if (0 == id) {
3847 bp = &sctp_sk(sk)->ep->base.bind_addr;
3848 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3849 } else {
3850 asoc = sctp_id2assoc(sk, id);
3851 if (!asoc)
3852 return -EINVAL;
3853 bp = &asoc->base.bind_addr;
3854 addr_lock = &asoc->base.addr_lock;
3855 }
3856
3857 sctp_read_lock(addr_lock);
3858
3859 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3860 * addresses from the global local address list.
3861 */
3862 if (sctp_list_single_entry(&bp->address_list)) {
3863 addr = list_entry(bp->address_list.next,
3864 struct sctp_sockaddr_entry, list);
3865 if (sctp_is_any(&addr->a)) {
3866 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3867 list_for_each(pos, &sctp_local_addr_list) {
3868 addr = list_entry(pos,
3869 struct sctp_sockaddr_entry,
3870 list);
3871 if ((PF_INET == sk->sk_family) &&
3872 (AF_INET6 == addr->a.sa.sa_family))
3873 continue;
3874 cnt++;
3875 }
3876 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3877 flags);
3878 } else {
3879 cnt = 1;
3880 }
3881 goto done;
3882 }
3883
3884 list_for_each(pos, &bp->address_list) {
3885 cnt ++;
3886 }
3887
3888done:
3889 sctp_read_unlock(addr_lock);
3890 return cnt;
3891}
3892
3893/* Helper function that copies local addresses to user and returns the number
3894 * of addresses copied.
3895 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003896static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3897 void __user *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
3899 struct list_head *pos;
3900 struct sctp_sockaddr_entry *addr;
3901 unsigned long flags;
3902 union sctp_addr temp;
3903 int cnt = 0;
3904 int addrlen;
3905
3906 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3907 list_for_each(pos, &sctp_local_addr_list) {
3908 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3909 if ((PF_INET == sk->sk_family) &&
3910 (AF_INET6 == addr->a.sa.sa_family))
3911 continue;
3912 memcpy(&temp, &addr->a, sizeof(temp));
3913 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3914 &temp);
3915 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3916 temp.v4.sin_port = htons(port);
3917 if (copy_to_user(to, &temp, addrlen)) {
3918 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3919 flags);
3920 return -EFAULT;
3921 }
3922 to += addrlen;
3923 cnt ++;
3924 if (cnt >= max_addrs) break;
3925 }
3926 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3927
3928 return cnt;
3929}
3930
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003931static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
Al Virod3a880e2005-12-15 09:18:30 +00003932 void __user **to, size_t space_left)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003933{
3934 struct list_head *pos;
3935 struct sctp_sockaddr_entry *addr;
3936 unsigned long flags;
3937 union sctp_addr temp;
3938 int cnt = 0;
3939 int addrlen;
3940
3941 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3942 list_for_each(pos, &sctp_local_addr_list) {
3943 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3944 if ((PF_INET == sk->sk_family) &&
3945 (AF_INET6 == addr->a.sa.sa_family))
3946 continue;
3947 memcpy(&temp, &addr->a, sizeof(temp));
3948 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3949 &temp);
3950 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3951 if(space_left<addrlen)
3952 return -ENOMEM;
3953 temp.v4.sin_port = htons(port);
3954 if (copy_to_user(*to, &temp, addrlen)) {
3955 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3956 flags);
3957 return -EFAULT;
3958 }
3959 *to += addrlen;
3960 cnt ++;
3961 space_left -= addrlen;
3962 }
3963 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3964
3965 return cnt;
3966}
3967
3968/* Old API for getting list of local addresses. Does not work for 32-bit
3969 * programs running on a 64-bit kernel
3970 */
3971static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3972 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
3974 struct sctp_bind_addr *bp;
3975 struct sctp_association *asoc;
3976 struct list_head *pos;
3977 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003978 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct sctp_sockaddr_entry *addr;
3980 void __user *to;
3981 union sctp_addr temp;
3982 struct sctp_sock *sp = sctp_sk(sk);
3983 int addrlen;
3984 rwlock_t *addr_lock;
3985 int err = 0;
3986
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003987 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return -EINVAL;
3989
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003990 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return -EFAULT;
3992
3993 if (getaddrs.addr_num <= 0) return -EINVAL;
3994 /*
3995 * For UDP-style sockets, id specifies the association to query.
3996 * If the id field is set to the value '0' then the locally bound
3997 * addresses are returned without regard to any particular
3998 * association.
3999 */
4000 if (0 == getaddrs.assoc_id) {
4001 bp = &sctp_sk(sk)->ep->base.bind_addr;
4002 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4003 } else {
4004 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4005 if (!asoc)
4006 return -EINVAL;
4007 bp = &asoc->base.bind_addr;
4008 addr_lock = &asoc->base.addr_lock;
4009 }
4010
4011 to = getaddrs.addrs;
4012
4013 sctp_read_lock(addr_lock);
4014
4015 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4016 * addresses from the global local address list.
4017 */
4018 if (sctp_list_single_entry(&bp->address_list)) {
4019 addr = list_entry(bp->address_list.next,
4020 struct sctp_sockaddr_entry, list);
4021 if (sctp_is_any(&addr->a)) {
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004022 cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
4023 getaddrs.addr_num,
4024 to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (cnt < 0) {
4026 err = cnt;
4027 goto unlock;
4028 }
4029 goto copy_getaddrs;
4030 }
4031 }
4032
4033 list_for_each(pos, &bp->address_list) {
4034 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4035 memcpy(&temp, &addr->a, sizeof(temp));
4036 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4037 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4038 temp.v4.sin_port = htons(temp.v4.sin_port);
4039 if (copy_to_user(to, &temp, addrlen)) {
4040 err = -EFAULT;
4041 goto unlock;
4042 }
4043 to += addrlen;
4044 cnt ++;
4045 if (cnt >= getaddrs.addr_num) break;
4046 }
4047
4048copy_getaddrs:
4049 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004050 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 err = -EFAULT;
4052
4053unlock:
4054 sctp_read_unlock(addr_lock);
4055 return err;
4056}
4057
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004058static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4059 char __user *optval, int __user *optlen)
4060{
4061 struct sctp_bind_addr *bp;
4062 struct sctp_association *asoc;
4063 struct list_head *pos;
4064 int cnt = 0;
4065 struct sctp_getaddrs getaddrs;
4066 struct sctp_sockaddr_entry *addr;
4067 void __user *to;
4068 union sctp_addr temp;
4069 struct sctp_sock *sp = sctp_sk(sk);
4070 int addrlen;
4071 rwlock_t *addr_lock;
4072 int err = 0;
4073 size_t space_left;
4074 int bytes_copied;
4075
4076 if (len <= sizeof(struct sctp_getaddrs))
4077 return -EINVAL;
4078
4079 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4080 return -EFAULT;
4081
4082 /*
4083 * For UDP-style sockets, id specifies the association to query.
4084 * If the id field is set to the value '0' then the locally bound
4085 * addresses are returned without regard to any particular
4086 * association.
4087 */
4088 if (0 == getaddrs.assoc_id) {
4089 bp = &sctp_sk(sk)->ep->base.bind_addr;
4090 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4091 } else {
4092 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4093 if (!asoc)
4094 return -EINVAL;
4095 bp = &asoc->base.bind_addr;
4096 addr_lock = &asoc->base.addr_lock;
4097 }
4098
4099 to = optval + offsetof(struct sctp_getaddrs,addrs);
4100 space_left = len - sizeof(struct sctp_getaddrs) -
4101 offsetof(struct sctp_getaddrs,addrs);
4102
4103 sctp_read_lock(addr_lock);
4104
4105 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4106 * addresses from the global local address list.
4107 */
4108 if (sctp_list_single_entry(&bp->address_list)) {
4109 addr = list_entry(bp->address_list.next,
4110 struct sctp_sockaddr_entry, list);
4111 if (sctp_is_any(&addr->a)) {
4112 cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4113 &to, space_left);
4114 if (cnt < 0) {
4115 err = cnt;
4116 goto unlock;
4117 }
4118 goto copy_getaddrs;
4119 }
4120 }
4121
4122 list_for_each(pos, &bp->address_list) {
4123 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4124 memcpy(&temp, &addr->a, sizeof(temp));
4125 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4126 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4127 if(space_left < addrlen)
4128 return -ENOMEM; /*fixme: right error?*/
4129 temp.v4.sin_port = htons(temp.v4.sin_port);
4130 if (copy_to_user(to, &temp, addrlen)) {
4131 err = -EFAULT;
4132 goto unlock;
4133 }
4134 to += addrlen;
4135 cnt ++;
4136 space_left -= addrlen;
4137 }
4138
4139copy_getaddrs:
4140 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4141 return -EFAULT;
4142 bytes_copied = ((char __user *)to) - optval;
4143 if (put_user(bytes_copied, optlen))
4144 return -EFAULT;
4145
4146unlock:
4147 sctp_read_unlock(addr_lock);
4148 return err;
4149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4152 *
4153 * Requests that the local SCTP stack use the enclosed peer address as
4154 * the association primary. The enclosed address must be one of the
4155 * association peer's addresses.
4156 */
4157static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4158 char __user *optval, int __user *optlen)
4159{
4160 struct sctp_prim prim;
4161 struct sctp_association *asoc;
4162 struct sctp_sock *sp = sctp_sk(sk);
4163
4164 if (len != sizeof(struct sctp_prim))
4165 return -EINVAL;
4166
4167 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4168 return -EFAULT;
4169
4170 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4171 if (!asoc)
4172 return -EINVAL;
4173
4174 if (!asoc->peer.primary_path)
4175 return -ENOTCONN;
4176
4177 asoc->peer.primary_path->ipaddr.v4.sin_port =
4178 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4179 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4180 sizeof(union sctp_addr));
4181 asoc->peer.primary_path->ipaddr.v4.sin_port =
4182 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4183
4184 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4185 (union sctp_addr *)&prim.ssp_addr);
4186
4187 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4188 return -EFAULT;
4189
4190 return 0;
4191}
4192
4193/*
4194 * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4195 *
4196 * Requests that the local endpoint set the specified Adaption Layer
4197 * Indication parameter for all future INIT and INIT-ACK exchanges.
4198 */
4199static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4200 char __user *optval, int __user *optlen)
4201{
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004202 struct sctp_setadaption adaption;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004204 if (len != sizeof(struct sctp_setadaption))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return -EINVAL;
4206
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004207 adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4208 if (copy_to_user(optval, &adaption, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return 0;
4212}
4213
4214/*
4215 *
4216 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4217 *
4218 * Applications that wish to use the sendto() system call may wish to
4219 * specify a default set of parameters that would normally be supplied
4220 * through the inclusion of ancillary data. This socket option allows
4221 * such an application to set the default sctp_sndrcvinfo structure.
4222
4223
4224 * The application that wishes to use this socket option simply passes
4225 * in to this call the sctp_sndrcvinfo structure defined in Section
4226 * 5.2.2) The input parameters accepted by this call include
4227 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4228 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4229 * to this call if the caller is using the UDP model.
4230 *
4231 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4232 */
4233static int sctp_getsockopt_default_send_param(struct sock *sk,
4234 int len, char __user *optval,
4235 int __user *optlen)
4236{
4237 struct sctp_sndrcvinfo info;
4238 struct sctp_association *asoc;
4239 struct sctp_sock *sp = sctp_sk(sk);
4240
4241 if (len != sizeof(struct sctp_sndrcvinfo))
4242 return -EINVAL;
4243 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4244 return -EFAULT;
4245
4246 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4247 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4248 return -EINVAL;
4249
4250 if (asoc) {
4251 info.sinfo_stream = asoc->default_stream;
4252 info.sinfo_flags = asoc->default_flags;
4253 info.sinfo_ppid = asoc->default_ppid;
4254 info.sinfo_context = asoc->default_context;
4255 info.sinfo_timetolive = asoc->default_timetolive;
4256 } else {
4257 info.sinfo_stream = sp->default_stream;
4258 info.sinfo_flags = sp->default_flags;
4259 info.sinfo_ppid = sp->default_ppid;
4260 info.sinfo_context = sp->default_context;
4261 info.sinfo_timetolive = sp->default_timetolive;
4262 }
4263
4264 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4265 return -EFAULT;
4266
4267 return 0;
4268}
4269
4270/*
4271 *
4272 * 7.1.5 SCTP_NODELAY
4273 *
4274 * Turn on/off any Nagle-like algorithm. This means that packets are
4275 * generally sent as soon as possible and no unnecessary delays are
4276 * introduced, at the cost of more packets in the network. Expects an
4277 * integer boolean flag.
4278 */
4279
4280static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4281 char __user *optval, int __user *optlen)
4282{
4283 int val;
4284
4285 if (len < sizeof(int))
4286 return -EINVAL;
4287
4288 len = sizeof(int);
4289 val = (sctp_sk(sk)->nodelay == 1);
4290 if (put_user(len, optlen))
4291 return -EFAULT;
4292 if (copy_to_user(optval, &val, len))
4293 return -EFAULT;
4294 return 0;
4295}
4296
4297/*
4298 *
4299 * 7.1.1 SCTP_RTOINFO
4300 *
4301 * The protocol parameters used to initialize and bound retransmission
4302 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4303 * and modify these parameters.
4304 * All parameters are time values, in milliseconds. A value of 0, when
4305 * modifying the parameters, indicates that the current value should not
4306 * be changed.
4307 *
4308 */
4309static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4310 char __user *optval,
4311 int __user *optlen) {
4312 struct sctp_rtoinfo rtoinfo;
4313 struct sctp_association *asoc;
4314
4315 if (len != sizeof (struct sctp_rtoinfo))
4316 return -EINVAL;
4317
4318 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4319 return -EFAULT;
4320
4321 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4322
4323 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4324 return -EINVAL;
4325
4326 /* Values corresponding to the specific association. */
4327 if (asoc) {
4328 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4329 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4330 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4331 } else {
4332 /* Values corresponding to the endpoint. */
4333 struct sctp_sock *sp = sctp_sk(sk);
4334
4335 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4336 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4337 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4338 }
4339
4340 if (put_user(len, optlen))
4341 return -EFAULT;
4342
4343 if (copy_to_user(optval, &rtoinfo, len))
4344 return -EFAULT;
4345
4346 return 0;
4347}
4348
4349/*
4350 *
4351 * 7.1.2 SCTP_ASSOCINFO
4352 *
4353 * This option is used to tune the the maximum retransmission attempts
4354 * of the association.
4355 * Returns an error if the new association retransmission value is
4356 * greater than the sum of the retransmission value of the peer.
4357 * See [SCTP] for more information.
4358 *
4359 */
4360static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4361 char __user *optval,
4362 int __user *optlen)
4363{
4364
4365 struct sctp_assocparams assocparams;
4366 struct sctp_association *asoc;
4367 struct list_head *pos;
4368 int cnt = 0;
4369
4370 if (len != sizeof (struct sctp_assocparams))
4371 return -EINVAL;
4372
4373 if (copy_from_user(&assocparams, optval,
4374 sizeof (struct sctp_assocparams)))
4375 return -EFAULT;
4376
4377 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4378
4379 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4380 return -EINVAL;
4381
4382 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004383 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4385 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4386 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4387 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4388 * 1000) +
4389 (asoc->cookie_life.tv_usec
4390 / 1000);
4391
4392 list_for_each(pos, &asoc->peer.transport_addr_list) {
4393 cnt ++;
4394 }
4395
4396 assocparams.sasoc_number_peer_destinations = cnt;
4397 } else {
4398 /* Values corresponding to the endpoint */
4399 struct sctp_sock *sp = sctp_sk(sk);
4400
4401 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4402 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4403 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4404 assocparams.sasoc_cookie_life =
4405 sp->assocparams.sasoc_cookie_life;
4406 assocparams.sasoc_number_peer_destinations =
4407 sp->assocparams.
4408 sasoc_number_peer_destinations;
4409 }
4410
4411 if (put_user(len, optlen))
4412 return -EFAULT;
4413
4414 if (copy_to_user(optval, &assocparams, len))
4415 return -EFAULT;
4416
4417 return 0;
4418}
4419
4420/*
4421 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4422 *
4423 * This socket option is a boolean flag which turns on or off mapped V4
4424 * addresses. If this option is turned on and the socket is type
4425 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4426 * If this option is turned off, then no mapping will be done of V4
4427 * addresses and a user will receive both PF_INET6 and PF_INET type
4428 * addresses on the socket.
4429 */
4430static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4431 char __user *optval, int __user *optlen)
4432{
4433 int val;
4434 struct sctp_sock *sp = sctp_sk(sk);
4435
4436 if (len < sizeof(int))
4437 return -EINVAL;
4438
4439 len = sizeof(int);
4440 val = sp->v4mapped;
4441 if (put_user(len, optlen))
4442 return -EFAULT;
4443 if (copy_to_user(optval, &val, len))
4444 return -EFAULT;
4445
4446 return 0;
4447}
4448
4449/*
4450 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4451 *
4452 * This socket option specifies the maximum size to put in any outgoing
4453 * SCTP chunk. If a message is larger than this size it will be
4454 * fragmented by SCTP into the specified size. Note that the underlying
4455 * SCTP implementation may fragment into smaller sized chunks when the
4456 * PMTU of the underlying association is smaller than the value set by
4457 * the user.
4458 */
4459static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4460 char __user *optval, int __user *optlen)
4461{
4462 int val;
4463
4464 if (len < sizeof(int))
4465 return -EINVAL;
4466
4467 len = sizeof(int);
4468
4469 val = sctp_sk(sk)->user_frag;
4470 if (put_user(len, optlen))
4471 return -EFAULT;
4472 if (copy_to_user(optval, &val, len))
4473 return -EFAULT;
4474
4475 return 0;
4476}
4477
4478SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4479 char __user *optval, int __user *optlen)
4480{
4481 int retval = 0;
4482 int len;
4483
Frank Filz3f7a87d2005-06-20 13:14:57 -07004484 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4485 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 /* I can hardly begin to describe how wrong this is. This is
4488 * so broken as to be worse than useless. The API draft
4489 * REALLY is NOT helpful here... I am not convinced that the
4490 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4491 * are at all well-founded.
4492 */
4493 if (level != SOL_SCTP) {
4494 struct sctp_af *af = sctp_sk(sk)->pf->af;
4495
4496 retval = af->getsockopt(sk, level, optname, optval, optlen);
4497 return retval;
4498 }
4499
4500 if (get_user(len, optlen))
4501 return -EFAULT;
4502
4503 sctp_lock_sock(sk);
4504
4505 switch (optname) {
4506 case SCTP_STATUS:
4507 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4508 break;
4509 case SCTP_DISABLE_FRAGMENTS:
4510 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4511 optlen);
4512 break;
4513 case SCTP_EVENTS:
4514 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4515 break;
4516 case SCTP_AUTOCLOSE:
4517 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4518 break;
4519 case SCTP_SOCKOPT_PEELOFF:
4520 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4521 break;
4522 case SCTP_PEER_ADDR_PARAMS:
4523 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4524 optlen);
4525 break;
Frank Filz77086102005-12-22 11:37:30 -08004526 case SCTP_DELAYED_ACK_TIME:
4527 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4528 optlen);
4529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 case SCTP_INITMSG:
4531 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4532 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004533 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4534 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4535 optlen);
4536 break;
4537 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4538 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4539 optlen);
4540 break;
4541 case SCTP_GET_PEER_ADDRS_OLD:
4542 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 optlen);
4544 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004545 case SCTP_GET_LOCAL_ADDRS_OLD:
4546 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 optlen);
4548 break;
4549 case SCTP_GET_PEER_ADDRS:
4550 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4551 optlen);
4552 break;
4553 case SCTP_GET_LOCAL_ADDRS:
4554 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4555 optlen);
4556 break;
4557 case SCTP_DEFAULT_SEND_PARAM:
4558 retval = sctp_getsockopt_default_send_param(sk, len,
4559 optval, optlen);
4560 break;
4561 case SCTP_PRIMARY_ADDR:
4562 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4563 break;
4564 case SCTP_NODELAY:
4565 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4566 break;
4567 case SCTP_RTOINFO:
4568 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4569 break;
4570 case SCTP_ASSOCINFO:
4571 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4572 break;
4573 case SCTP_I_WANT_MAPPED_V4_ADDR:
4574 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4575 break;
4576 case SCTP_MAXSEG:
4577 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4578 break;
4579 case SCTP_GET_PEER_ADDR_INFO:
4580 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4581 optlen);
4582 break;
4583 case SCTP_ADAPTION_LAYER:
4584 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4585 optlen);
4586 break;
4587 default:
4588 retval = -ENOPROTOOPT;
4589 break;
4590 };
4591
4592 sctp_release_sock(sk);
4593 return retval;
4594}
4595
4596static void sctp_hash(struct sock *sk)
4597{
4598 /* STUB */
4599}
4600
4601static void sctp_unhash(struct sock *sk)
4602{
4603 /* STUB */
4604}
4605
4606/* Check if port is acceptable. Possibly find first available port.
4607 *
4608 * The port hash table (contained in the 'global' SCTP protocol storage
4609 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4610 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4611 * list (the list number is the port number hashed out, so as you
4612 * would expect from a hash function, all the ports in a given list have
4613 * such a number that hashes out to the same list number; you were
4614 * expecting that, right?); so each list has a set of ports, with a
4615 * link to the socket (struct sock) that uses it, the port number and
4616 * a fastreuse flag (FIXME: NPI ipg).
4617 */
4618static struct sctp_bind_bucket *sctp_bucket_create(
4619 struct sctp_bind_hashbucket *head, unsigned short snum);
4620
4621static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4622{
4623 struct sctp_bind_hashbucket *head; /* hash list */
4624 struct sctp_bind_bucket *pp; /* hash list port iterator */
4625 unsigned short snum;
4626 int ret;
4627
4628 /* NOTE: Remember to put this back to net order. */
4629 addr->v4.sin_port = ntohs(addr->v4.sin_port);
4630 snum = addr->v4.sin_port;
4631
4632 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4633 sctp_local_bh_disable();
4634
4635 if (snum == 0) {
4636 /* Search for an available port.
4637 *
4638 * 'sctp_port_rover' was the last port assigned, so
4639 * we start to search from 'sctp_port_rover +
4640 * 1'. What we do is first check if port 'rover' is
4641 * already in the hash table; if not, we use that; if
4642 * it is, we try next.
4643 */
4644 int low = sysctl_local_port_range[0];
4645 int high = sysctl_local_port_range[1];
4646 int remaining = (high - low) + 1;
4647 int rover;
4648 int index;
4649
4650 sctp_spin_lock(&sctp_port_alloc_lock);
4651 rover = sctp_port_rover;
4652 do {
4653 rover++;
4654 if ((rover < low) || (rover > high))
4655 rover = low;
4656 index = sctp_phashfn(rover);
4657 head = &sctp_port_hashtable[index];
4658 sctp_spin_lock(&head->lock);
4659 for (pp = head->chain; pp; pp = pp->next)
4660 if (pp->port == rover)
4661 goto next;
4662 break;
4663 next:
4664 sctp_spin_unlock(&head->lock);
4665 } while (--remaining > 0);
4666 sctp_port_rover = rover;
4667 sctp_spin_unlock(&sctp_port_alloc_lock);
4668
4669 /* Exhausted local port range during search? */
4670 ret = 1;
4671 if (remaining <= 0)
4672 goto fail;
4673
4674 /* OK, here is the one we will use. HEAD (the port
4675 * hash table list entry) is non-NULL and we hold it's
4676 * mutex.
4677 */
4678 snum = rover;
4679 } else {
4680 /* We are given an specific port number; we verify
4681 * that it is not being used. If it is used, we will
4682 * exahust the search in the hash list corresponding
4683 * to the port number (snum) - we detect that with the
4684 * port iterator, pp being NULL.
4685 */
4686 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4687 sctp_spin_lock(&head->lock);
4688 for (pp = head->chain; pp; pp = pp->next) {
4689 if (pp->port == snum)
4690 goto pp_found;
4691 }
4692 }
4693 pp = NULL;
4694 goto pp_not_found;
4695pp_found:
4696 if (!hlist_empty(&pp->owner)) {
4697 /* We had a port hash table hit - there is an
4698 * available port (pp != NULL) and it is being
4699 * used by other socket (pp->owner not empty); that other
4700 * socket is going to be sk2.
4701 */
4702 int reuse = sk->sk_reuse;
4703 struct sock *sk2;
4704 struct hlist_node *node;
4705
4706 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4707 if (pp->fastreuse && sk->sk_reuse)
4708 goto success;
4709
4710 /* Run through the list of sockets bound to the port
4711 * (pp->port) [via the pointers bind_next and
4712 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4713 * we get the endpoint they describe and run through
4714 * the endpoint's list of IP (v4 or v6) addresses,
4715 * comparing each of the addresses with the address of
4716 * the socket sk. If we find a match, then that means
4717 * that this port/socket (sk) combination are already
4718 * in an endpoint.
4719 */
4720 sk_for_each_bound(sk2, node, &pp->owner) {
4721 struct sctp_endpoint *ep2;
4722 ep2 = sctp_sk(sk2)->ep;
4723
4724 if (reuse && sk2->sk_reuse)
4725 continue;
4726
4727 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4728 sctp_sk(sk))) {
4729 ret = (long)sk2;
4730 goto fail_unlock;
4731 }
4732 }
4733 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4734 }
4735pp_not_found:
4736 /* If there was a hash table miss, create a new port. */
4737 ret = 1;
4738 if (!pp && !(pp = sctp_bucket_create(head, snum)))
4739 goto fail_unlock;
4740
4741 /* In either case (hit or miss), make sure fastreuse is 1 only
4742 * if sk->sk_reuse is too (that is, if the caller requested
4743 * SO_REUSEADDR on this socket -sk-).
4744 */
4745 if (hlist_empty(&pp->owner))
4746 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4747 else if (pp->fastreuse && !sk->sk_reuse)
4748 pp->fastreuse = 0;
4749
4750 /* We are set, so fill up all the data in the hash table
4751 * entry, tie the socket list information with the rest of the
4752 * sockets FIXME: Blurry, NPI (ipg).
4753 */
4754success:
4755 inet_sk(sk)->num = snum;
4756 if (!sctp_sk(sk)->bind_hash) {
4757 sk_add_bind_node(sk, &pp->owner);
4758 sctp_sk(sk)->bind_hash = pp;
4759 }
4760 ret = 0;
4761
4762fail_unlock:
4763 sctp_spin_unlock(&head->lock);
4764
4765fail:
4766 sctp_local_bh_enable();
4767 addr->v4.sin_port = htons(addr->v4.sin_port);
4768 return ret;
4769}
4770
4771/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
4772 * port is requested.
4773 */
4774static int sctp_get_port(struct sock *sk, unsigned short snum)
4775{
4776 long ret;
4777 union sctp_addr addr;
4778 struct sctp_af *af = sctp_sk(sk)->pf->af;
4779
4780 /* Set up a dummy address struct from the sk. */
4781 af->from_sk(&addr, sk);
4782 addr.v4.sin_port = htons(snum);
4783
4784 /* Note: sk->sk_num gets filled in if ephemeral port request. */
4785 ret = sctp_get_port_local(sk, &addr);
4786
4787 return (ret ? 1 : 0);
4788}
4789
4790/*
4791 * 3.1.3 listen() - UDP Style Syntax
4792 *
4793 * By default, new associations are not accepted for UDP style sockets.
4794 * An application uses listen() to mark a socket as being able to
4795 * accept new associations.
4796 */
4797SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4798{
4799 struct sctp_sock *sp = sctp_sk(sk);
4800 struct sctp_endpoint *ep = sp->ep;
4801
4802 /* Only UDP style sockets that are not peeled off are allowed to
4803 * listen().
4804 */
4805 if (!sctp_style(sk, UDP))
4806 return -EINVAL;
4807
4808 /* If backlog is zero, disable listening. */
4809 if (!backlog) {
4810 if (sctp_sstate(sk, CLOSED))
4811 return 0;
4812
4813 sctp_unhash_endpoint(ep);
4814 sk->sk_state = SCTP_SS_CLOSED;
4815 }
4816
4817 /* Return if we are already listening. */
4818 if (sctp_sstate(sk, LISTENING))
4819 return 0;
4820
4821 /*
4822 * If a bind() or sctp_bindx() is not called prior to a listen()
4823 * call that allows new associations to be accepted, the system
4824 * picks an ephemeral port and will choose an address set equivalent
4825 * to binding with a wildcard address.
4826 *
4827 * This is not currently spelled out in the SCTP sockets
4828 * extensions draft, but follows the practice as seen in TCP
4829 * sockets.
4830 */
4831 if (!ep->base.bind_addr.port) {
4832 if (sctp_autobind(sk))
4833 return -EAGAIN;
4834 }
4835 sk->sk_state = SCTP_SS_LISTENING;
4836 sctp_hash_endpoint(ep);
4837 return 0;
4838}
4839
4840/*
4841 * 4.1.3 listen() - TCP Style Syntax
4842 *
4843 * Applications uses listen() to ready the SCTP endpoint for accepting
4844 * inbound associations.
4845 */
4846SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4847{
4848 struct sctp_sock *sp = sctp_sk(sk);
4849 struct sctp_endpoint *ep = sp->ep;
4850
4851 /* If backlog is zero, disable listening. */
4852 if (!backlog) {
4853 if (sctp_sstate(sk, CLOSED))
4854 return 0;
4855
4856 sctp_unhash_endpoint(ep);
4857 sk->sk_state = SCTP_SS_CLOSED;
4858 }
4859
4860 if (sctp_sstate(sk, LISTENING))
4861 return 0;
4862
4863 /*
4864 * If a bind() or sctp_bindx() is not called prior to a listen()
4865 * call that allows new associations to be accepted, the system
4866 * picks an ephemeral port and will choose an address set equivalent
4867 * to binding with a wildcard address.
4868 *
4869 * This is not currently spelled out in the SCTP sockets
4870 * extensions draft, but follows the practice as seen in TCP
4871 * sockets.
4872 */
4873 if (!ep->base.bind_addr.port) {
4874 if (sctp_autobind(sk))
4875 return -EAGAIN;
4876 }
4877 sk->sk_state = SCTP_SS_LISTENING;
4878 sk->sk_max_ack_backlog = backlog;
4879 sctp_hash_endpoint(ep);
4880 return 0;
4881}
4882
4883/*
4884 * Move a socket to LISTENING state.
4885 */
4886int sctp_inet_listen(struct socket *sock, int backlog)
4887{
4888 struct sock *sk = sock->sk;
4889 struct crypto_tfm *tfm=NULL;
4890 int err = -EINVAL;
4891
4892 if (unlikely(backlog < 0))
4893 goto out;
4894
4895 sctp_lock_sock(sk);
4896
4897 if (sock->state != SS_UNCONNECTED)
4898 goto out;
4899
4900 /* Allocate HMAC for generating cookie. */
4901 if (sctp_hmac_alg) {
4902 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4903 if (!tfm) {
4904 err = -ENOSYS;
4905 goto out;
4906 }
4907 }
4908
4909 switch (sock->type) {
4910 case SOCK_SEQPACKET:
4911 err = sctp_seqpacket_listen(sk, backlog);
4912 break;
4913 case SOCK_STREAM:
4914 err = sctp_stream_listen(sk, backlog);
4915 break;
4916 default:
4917 break;
4918 };
4919 if (err)
4920 goto cleanup;
4921
4922 /* Store away the transform reference. */
4923 sctp_sk(sk)->hmac = tfm;
4924out:
4925 sctp_release_sock(sk);
4926 return err;
4927cleanup:
Jesper Juhl573dbd92005-09-01 17:44:29 -07004928 sctp_crypto_free_tfm(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 goto out;
4930}
4931
4932/*
4933 * This function is done by modeling the current datagram_poll() and the
4934 * tcp_poll(). Note that, based on these implementations, we don't
4935 * lock the socket in this function, even though it seems that,
4936 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08004937 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 * in this place. We assume that we don't need locks either until proven
4939 * otherwise.
4940 *
4941 * Another thing to note is that we include the Async I/O support
4942 * here, again, by modeling the current TCP/UDP code. We don't have
4943 * a good way to test with it yet.
4944 */
4945unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4946{
4947 struct sock *sk = sock->sk;
4948 struct sctp_sock *sp = sctp_sk(sk);
4949 unsigned int mask;
4950
4951 poll_wait(file, sk->sk_sleep, wait);
4952
4953 /* A TCP-style listening socket becomes readable when the accept queue
4954 * is not empty.
4955 */
4956 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4957 return (!list_empty(&sp->ep->asocs)) ?
4958 (POLLIN | POLLRDNORM) : 0;
4959
4960 mask = 0;
4961
4962 /* Is there any exceptional events? */
4963 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4964 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08004965 if (sk->sk_shutdown & RCV_SHUTDOWN)
4966 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 if (sk->sk_shutdown == SHUTDOWN_MASK)
4968 mask |= POLLHUP;
4969
4970 /* Is it readable? Reconsider this code with TCP-style support. */
4971 if (!skb_queue_empty(&sk->sk_receive_queue) ||
4972 (sk->sk_shutdown & RCV_SHUTDOWN))
4973 mask |= POLLIN | POLLRDNORM;
4974
4975 /* The association is either gone or not ready. */
4976 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4977 return mask;
4978
4979 /* Is it writable? */
4980 if (sctp_writeable(sk)) {
4981 mask |= POLLOUT | POLLWRNORM;
4982 } else {
4983 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4984 /*
4985 * Since the socket is not locked, the buffer
4986 * might be made available after the writeable check and
4987 * before the bit is set. This could cause a lost I/O
4988 * signal. tcp_poll() has a race breaker for this race
4989 * condition. Based on their implementation, we put
4990 * in the following code to cover it as well.
4991 */
4992 if (sctp_writeable(sk))
4993 mask |= POLLOUT | POLLWRNORM;
4994 }
4995 return mask;
4996}
4997
4998/********************************************************************
4999 * 2nd Level Abstractions
5000 ********************************************************************/
5001
5002static struct sctp_bind_bucket *sctp_bucket_create(
5003 struct sctp_bind_hashbucket *head, unsigned short snum)
5004{
5005 struct sctp_bind_bucket *pp;
5006
5007 pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
5008 SCTP_DBG_OBJCNT_INC(bind_bucket);
5009 if (pp) {
5010 pp->port = snum;
5011 pp->fastreuse = 0;
5012 INIT_HLIST_HEAD(&pp->owner);
5013 if ((pp->next = head->chain) != NULL)
5014 pp->next->pprev = &pp->next;
5015 head->chain = pp;
5016 pp->pprev = &head->chain;
5017 }
5018 return pp;
5019}
5020
5021/* Caller must hold hashbucket lock for this tb with local BH disabled */
5022static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5023{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005024 if (pp && hlist_empty(&pp->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 if (pp->next)
5026 pp->next->pprev = pp->pprev;
5027 *(pp->pprev) = pp->next;
5028 kmem_cache_free(sctp_bucket_cachep, pp);
5029 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5030 }
5031}
5032
5033/* Release this socket's reference to a local port. */
5034static inline void __sctp_put_port(struct sock *sk)
5035{
5036 struct sctp_bind_hashbucket *head =
5037 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5038 struct sctp_bind_bucket *pp;
5039
5040 sctp_spin_lock(&head->lock);
5041 pp = sctp_sk(sk)->bind_hash;
5042 __sk_del_bind_node(sk);
5043 sctp_sk(sk)->bind_hash = NULL;
5044 inet_sk(sk)->num = 0;
5045 sctp_bucket_destroy(pp);
5046 sctp_spin_unlock(&head->lock);
5047}
5048
5049void sctp_put_port(struct sock *sk)
5050{
5051 sctp_local_bh_disable();
5052 __sctp_put_port(sk);
5053 sctp_local_bh_enable();
5054}
5055
5056/*
5057 * The system picks an ephemeral port and choose an address set equivalent
5058 * to binding with a wildcard address.
5059 * One of those addresses will be the primary address for the association.
5060 * This automatically enables the multihoming capability of SCTP.
5061 */
5062static int sctp_autobind(struct sock *sk)
5063{
5064 union sctp_addr autoaddr;
5065 struct sctp_af *af;
5066 unsigned short port;
5067
5068 /* Initialize a local sockaddr structure to INADDR_ANY. */
5069 af = sctp_sk(sk)->pf->af;
5070
5071 port = htons(inet_sk(sk)->num);
5072 af->inaddr_any(&autoaddr, port);
5073
5074 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5075}
5076
5077/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5078 *
5079 * From RFC 2292
5080 * 4.2 The cmsghdr Structure *
5081 *
5082 * When ancillary data is sent or received, any number of ancillary data
5083 * objects can be specified by the msg_control and msg_controllen members of
5084 * the msghdr structure, because each object is preceded by
5085 * a cmsghdr structure defining the object's length (the cmsg_len member).
5086 * Historically Berkeley-derived implementations have passed only one object
5087 * at a time, but this API allows multiple objects to be
5088 * passed in a single call to sendmsg() or recvmsg(). The following example
5089 * shows two ancillary data objects in a control buffer.
5090 *
5091 * |<--------------------------- msg_controllen -------------------------->|
5092 * | |
5093 *
5094 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5095 *
5096 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5097 * | | |
5098 *
5099 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5100 *
5101 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5102 * | | | | |
5103 *
5104 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5105 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5106 *
5107 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5108 *
5109 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5110 * ^
5111 * |
5112 *
5113 * msg_control
5114 * points here
5115 */
5116SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5117 sctp_cmsgs_t *cmsgs)
5118{
5119 struct cmsghdr *cmsg;
5120
5121 for (cmsg = CMSG_FIRSTHDR(msg);
5122 cmsg != NULL;
5123 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5124 if (!CMSG_OK(msg, cmsg))
5125 return -EINVAL;
5126
5127 /* Should we parse this header or ignore? */
5128 if (cmsg->cmsg_level != IPPROTO_SCTP)
5129 continue;
5130
5131 /* Strictly check lengths following example in SCM code. */
5132 switch (cmsg->cmsg_type) {
5133 case SCTP_INIT:
5134 /* SCTP Socket API Extension
5135 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5136 *
5137 * This cmsghdr structure provides information for
5138 * initializing new SCTP associations with sendmsg().
5139 * The SCTP_INITMSG socket option uses this same data
5140 * structure. This structure is not used for
5141 * recvmsg().
5142 *
5143 * cmsg_level cmsg_type cmsg_data[]
5144 * ------------ ------------ ----------------------
5145 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5146 */
5147 if (cmsg->cmsg_len !=
5148 CMSG_LEN(sizeof(struct sctp_initmsg)))
5149 return -EINVAL;
5150 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5151 break;
5152
5153 case SCTP_SNDRCV:
5154 /* SCTP Socket API Extension
5155 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5156 *
5157 * This cmsghdr structure specifies SCTP options for
5158 * sendmsg() and describes SCTP header information
5159 * about a received message through recvmsg().
5160 *
5161 * cmsg_level cmsg_type cmsg_data[]
5162 * ------------ ------------ ----------------------
5163 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5164 */
5165 if (cmsg->cmsg_len !=
5166 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5167 return -EINVAL;
5168
5169 cmsgs->info =
5170 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5171
5172 /* Minimally, validate the sinfo_flags. */
5173 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005174 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5175 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return -EINVAL;
5177 break;
5178
5179 default:
5180 return -EINVAL;
5181 };
5182 }
5183 return 0;
5184}
5185
5186/*
5187 * Wait for a packet..
5188 * Note: This function is the same function as in core/datagram.c
5189 * with a few modifications to make lksctp work.
5190 */
5191static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5192{
5193 int error;
5194 DEFINE_WAIT(wait);
5195
5196 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5197
5198 /* Socket errors? */
5199 error = sock_error(sk);
5200 if (error)
5201 goto out;
5202
5203 if (!skb_queue_empty(&sk->sk_receive_queue))
5204 goto ready;
5205
5206 /* Socket shut down? */
5207 if (sk->sk_shutdown & RCV_SHUTDOWN)
5208 goto out;
5209
5210 /* Sequenced packets can come disconnected. If so we report the
5211 * problem.
5212 */
5213 error = -ENOTCONN;
5214
5215 /* Is there a good reason to think that we may receive some data? */
5216 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5217 goto out;
5218
5219 /* Handle signals. */
5220 if (signal_pending(current))
5221 goto interrupted;
5222
5223 /* Let another process have a go. Since we are going to sleep
5224 * anyway. Note: This may cause odd behaviors if the message
5225 * does not fit in the user's buffer, but this seems to be the
5226 * only way to honor MSG_DONTWAIT realistically.
5227 */
5228 sctp_release_sock(sk);
5229 *timeo_p = schedule_timeout(*timeo_p);
5230 sctp_lock_sock(sk);
5231
5232ready:
5233 finish_wait(sk->sk_sleep, &wait);
5234 return 0;
5235
5236interrupted:
5237 error = sock_intr_errno(*timeo_p);
5238
5239out:
5240 finish_wait(sk->sk_sleep, &wait);
5241 *err = error;
5242 return error;
5243}
5244
5245/* Receive a datagram.
5246 * Note: This is pretty much the same routine as in core/datagram.c
5247 * with a few changes to make lksctp work.
5248 */
5249static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5250 int noblock, int *err)
5251{
5252 int error;
5253 struct sk_buff *skb;
5254 long timeo;
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 timeo = sock_rcvtimeo(sk, noblock);
5257
5258 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5259 timeo, MAX_SCHEDULE_TIMEOUT);
5260
5261 do {
5262 /* Again only user level code calls this function,
5263 * so nothing interrupt level
5264 * will suddenly eat the receive_queue.
5265 *
5266 * Look at current nfs client by the way...
5267 * However, this function was corrent in any case. 8)
5268 */
5269 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005270 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 skb = skb_peek(&sk->sk_receive_queue);
5272 if (skb)
5273 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005274 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 } else {
5276 skb = skb_dequeue(&sk->sk_receive_queue);
5277 }
5278
5279 if (skb)
5280 return skb;
5281
Neil Horman6736dc32005-12-02 20:30:06 -08005282 /* Caller is allowed not to check sk->sk_err before calling. */
5283 error = sock_error(sk);
5284 if (error)
5285 goto no_packet;
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 if (sk->sk_shutdown & RCV_SHUTDOWN)
5288 break;
5289
5290 /* User doesn't want to wait. */
5291 error = -EAGAIN;
5292 if (!timeo)
5293 goto no_packet;
5294 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5295
5296 return NULL;
5297
5298no_packet:
5299 *err = error;
5300 return NULL;
5301}
5302
5303/* If sndbuf has changed, wake up per association sndbuf waiters. */
5304static void __sctp_write_space(struct sctp_association *asoc)
5305{
5306 struct sock *sk = asoc->base.sk;
5307 struct socket *sock = sk->sk_socket;
5308
5309 if ((sctp_wspace(asoc) > 0) && sock) {
5310 if (waitqueue_active(&asoc->wait))
5311 wake_up_interruptible(&asoc->wait);
5312
5313 if (sctp_writeable(sk)) {
5314 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5315 wake_up_interruptible(sk->sk_sleep);
5316
5317 /* Note that we try to include the Async I/O support
5318 * here by modeling from the current TCP/UDP code.
5319 * We have not tested with it yet.
5320 */
5321 if (sock->fasync_list &&
5322 !(sk->sk_shutdown & SEND_SHUTDOWN))
5323 sock_wake_async(sock, 2, POLL_OUT);
5324 }
5325 }
5326}
5327
5328/* Do accounting for the sndbuf space.
5329 * Decrement the used sndbuf space of the corresponding association by the
5330 * data size which was just transmitted(freed).
5331 */
5332static void sctp_wfree(struct sk_buff *skb)
5333{
5334 struct sctp_association *asoc;
5335 struct sctp_chunk *chunk;
5336 struct sock *sk;
5337
5338 /* Get the saved chunk pointer. */
5339 chunk = *((struct sctp_chunk **)(skb->cb));
5340 asoc = chunk->asoc;
5341 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07005342 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5343 sizeof(struct sk_buff) +
5344 sizeof(struct sctp_chunk);
5345
Neil Horman4eb701d2005-04-28 12:02:04 -07005346 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5347
5348 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 __sctp_write_space(asoc);
5350
5351 sctp_association_put(asoc);
5352}
5353
5354/* Helper function to wait for space in the sndbuf. */
5355static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5356 size_t msg_len)
5357{
5358 struct sock *sk = asoc->base.sk;
5359 int err = 0;
5360 long current_timeo = *timeo_p;
5361 DEFINE_WAIT(wait);
5362
5363 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5364 asoc, (long)(*timeo_p), msg_len);
5365
5366 /* Increment the association's refcnt. */
5367 sctp_association_hold(asoc);
5368
5369 /* Wait on the association specific sndbuf space. */
5370 for (;;) {
5371 prepare_to_wait_exclusive(&asoc->wait, &wait,
5372 TASK_INTERRUPTIBLE);
5373 if (!*timeo_p)
5374 goto do_nonblock;
5375 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5376 asoc->base.dead)
5377 goto do_error;
5378 if (signal_pending(current))
5379 goto do_interrupted;
5380 if (msg_len <= sctp_wspace(asoc))
5381 break;
5382
5383 /* Let another process have a go. Since we are going
5384 * to sleep anyway.
5385 */
5386 sctp_release_sock(sk);
5387 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005388 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 sctp_lock_sock(sk);
5390
5391 *timeo_p = current_timeo;
5392 }
5393
5394out:
5395 finish_wait(&asoc->wait, &wait);
5396
5397 /* Release the association's refcnt. */
5398 sctp_association_put(asoc);
5399
5400 return err;
5401
5402do_error:
5403 err = -EPIPE;
5404 goto out;
5405
5406do_interrupted:
5407 err = sock_intr_errno(*timeo_p);
5408 goto out;
5409
5410do_nonblock:
5411 err = -EAGAIN;
5412 goto out;
5413}
5414
5415/* If socket sndbuf has changed, wake up all per association waiters. */
5416void sctp_write_space(struct sock *sk)
5417{
5418 struct sctp_association *asoc;
5419 struct list_head *pos;
5420
5421 /* Wake up the tasks in each wait queue. */
5422 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5423 asoc = list_entry(pos, struct sctp_association, asocs);
5424 __sctp_write_space(asoc);
5425 }
5426}
5427
5428/* Is there any sndbuf space available on the socket?
5429 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08005430 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 * associations on the same socket. For a UDP-style socket with
5432 * multiple associations, it is possible for it to be "unwriteable"
5433 * prematurely. I assume that this is acceptable because
5434 * a premature "unwriteable" is better than an accidental "writeable" which
5435 * would cause an unwanted block under certain circumstances. For the 1-1
5436 * UDP-style sockets or TCP-style sockets, this code should work.
5437 * - Daisy
5438 */
5439static int sctp_writeable(struct sock *sk)
5440{
5441 int amt = 0;
5442
Neil Horman9bffc4a2005-12-19 14:24:40 -08005443 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 if (amt < 0)
5445 amt = 0;
5446 return amt;
5447}
5448
5449/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5450 * returns immediately with EINPROGRESS.
5451 */
5452static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5453{
5454 struct sock *sk = asoc->base.sk;
5455 int err = 0;
5456 long current_timeo = *timeo_p;
5457 DEFINE_WAIT(wait);
5458
5459 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5460 (long)(*timeo_p));
5461
5462 /* Increment the association's refcnt. */
5463 sctp_association_hold(asoc);
5464
5465 for (;;) {
5466 prepare_to_wait_exclusive(&asoc->wait, &wait,
5467 TASK_INTERRUPTIBLE);
5468 if (!*timeo_p)
5469 goto do_nonblock;
5470 if (sk->sk_shutdown & RCV_SHUTDOWN)
5471 break;
5472 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5473 asoc->base.dead)
5474 goto do_error;
5475 if (signal_pending(current))
5476 goto do_interrupted;
5477
5478 if (sctp_state(asoc, ESTABLISHED))
5479 break;
5480
5481 /* Let another process have a go. Since we are going
5482 * to sleep anyway.
5483 */
5484 sctp_release_sock(sk);
5485 current_timeo = schedule_timeout(current_timeo);
5486 sctp_lock_sock(sk);
5487
5488 *timeo_p = current_timeo;
5489 }
5490
5491out:
5492 finish_wait(&asoc->wait, &wait);
5493
5494 /* Release the association's refcnt. */
5495 sctp_association_put(asoc);
5496
5497 return err;
5498
5499do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08005500 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 err = -ETIMEDOUT;
5502 else
5503 err = -ECONNREFUSED;
5504 goto out;
5505
5506do_interrupted:
5507 err = sock_intr_errno(*timeo_p);
5508 goto out;
5509
5510do_nonblock:
5511 err = -EINPROGRESS;
5512 goto out;
5513}
5514
5515static int sctp_wait_for_accept(struct sock *sk, long timeo)
5516{
5517 struct sctp_endpoint *ep;
5518 int err = 0;
5519 DEFINE_WAIT(wait);
5520
5521 ep = sctp_sk(sk)->ep;
5522
5523
5524 for (;;) {
5525 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5526 TASK_INTERRUPTIBLE);
5527
5528 if (list_empty(&ep->asocs)) {
5529 sctp_release_sock(sk);
5530 timeo = schedule_timeout(timeo);
5531 sctp_lock_sock(sk);
5532 }
5533
5534 err = -EINVAL;
5535 if (!sctp_sstate(sk, LISTENING))
5536 break;
5537
5538 err = 0;
5539 if (!list_empty(&ep->asocs))
5540 break;
5541
5542 err = sock_intr_errno(timeo);
5543 if (signal_pending(current))
5544 break;
5545
5546 err = -EAGAIN;
5547 if (!timeo)
5548 break;
5549 }
5550
5551 finish_wait(sk->sk_sleep, &wait);
5552
5553 return err;
5554}
5555
5556void sctp_wait_for_close(struct sock *sk, long timeout)
5557{
5558 DEFINE_WAIT(wait);
5559
5560 do {
5561 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5562 if (list_empty(&sctp_sk(sk)->ep->asocs))
5563 break;
5564 sctp_release_sock(sk);
5565 timeout = schedule_timeout(timeout);
5566 sctp_lock_sock(sk);
5567 } while (!signal_pending(current) && timeout);
5568
5569 finish_wait(sk->sk_sleep, &wait);
5570}
5571
5572/* Populate the fields of the newsk from the oldsk and migrate the assoc
5573 * and its messages to the newsk.
5574 */
5575static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5576 struct sctp_association *assoc,
5577 sctp_socket_type_t type)
5578{
5579 struct sctp_sock *oldsp = sctp_sk(oldsk);
5580 struct sctp_sock *newsp = sctp_sk(newsk);
5581 struct sctp_bind_bucket *pp; /* hash list port iterator */
5582 struct sctp_endpoint *newep = newsp->ep;
5583 struct sk_buff *skb, *tmp;
5584 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005585 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 /* Migrate socket buffer sizes and all the socket level options to the
5588 * new socket.
5589 */
5590 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5591 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5592 /* Brute force copy old sctp opt. */
5593 inet_sk_copy_descendant(newsk, oldsk);
5594
5595 /* Restore the ep value that was overwritten with the above structure
5596 * copy.
5597 */
5598 newsp->ep = newep;
5599 newsp->hmac = NULL;
5600
5601 /* Hook this new socket in to the bind_hash list. */
5602 pp = sctp_sk(oldsk)->bind_hash;
5603 sk_add_bind_node(newsk, &pp->owner);
5604 sctp_sk(newsk)->bind_hash = pp;
5605 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5606
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005607 /* Copy the bind_addr list from the original endpoint to the new
5608 * endpoint so that we can handle restarts properly
5609 */
5610 if (assoc->peer.ipv4_address)
5611 flags |= SCTP_ADDR4_PEERSUPP;
5612 if (assoc->peer.ipv6_address)
5613 flags |= SCTP_ADDR6_PEERSUPP;
5614 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5615 &oldsp->ep->base.bind_addr,
5616 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 /* Move any messages in the old socket's receive queue that are for the
5619 * peeled off association to the new socket's receive queue.
5620 */
5621 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5622 event = sctp_skb2event(skb);
5623 if (event->asoc == assoc) {
Neil Horman049b3ff2005-11-11 16:08:24 -08005624 sock_rfree(skb);
David S. Miller8728b832005-08-09 19:25:21 -07005625 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Neil Horman049b3ff2005-11-11 16:08:24 -08005627 skb_set_owner_r(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629 }
5630
5631 /* Clean up any messages pending delivery due to partial
5632 * delivery. Three cases:
5633 * 1) No partial deliver; no work.
5634 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5635 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5636 */
5637 skb_queue_head_init(&newsp->pd_lobby);
5638 sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5639
5640 if (sctp_sk(oldsk)->pd_mode) {
5641 struct sk_buff_head *queue;
5642
5643 /* Decide which queue to move pd_lobby skbs to. */
5644 if (assoc->ulpq.pd_mode) {
5645 queue = &newsp->pd_lobby;
5646 } else
5647 queue = &newsk->sk_receive_queue;
5648
5649 /* Walk through the pd_lobby, looking for skbs that
5650 * need moved to the new socket.
5651 */
5652 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5653 event = sctp_skb2event(skb);
5654 if (event->asoc == assoc) {
Neil Horman049b3ff2005-11-11 16:08:24 -08005655 sock_rfree(skb);
David S. Miller8728b832005-08-09 19:25:21 -07005656 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 __skb_queue_tail(queue, skb);
Neil Horman049b3ff2005-11-11 16:08:24 -08005658 skb_set_owner_r(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660 }
5661
5662 /* Clear up any skbs waiting for the partial
5663 * delivery to finish.
5664 */
5665 if (assoc->ulpq.pd_mode)
5666 sctp_clear_pd(oldsk);
5667
5668 }
5669
5670 /* Set the type of socket to indicate that it is peeled off from the
5671 * original UDP-style socket or created with the accept() call on a
5672 * TCP-style socket..
5673 */
5674 newsp->type = type;
5675
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005676 /* Mark the new socket "in-use" by the user so that any packets
5677 * that may arrive on the association after we've moved it are
5678 * queued to the backlog. This prevents a potential race between
5679 * backlog processing on the old socket and new-packet processing
5680 * on the new socket.
5681 */
5682 sctp_lock_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
5685 /* If the association on the newsk is already closed before accept()
5686 * is called, set RCV_SHUTDOWN flag.
5687 */
5688 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5689 newsk->sk_shutdown |= RCV_SHUTDOWN;
5690
5691 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005692 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693}
5694
5695/* This proto struct describes the ULP interface for SCTP. */
5696struct proto sctp_prot = {
5697 .name = "SCTP",
5698 .owner = THIS_MODULE,
5699 .close = sctp_close,
5700 .connect = sctp_connect,
5701 .disconnect = sctp_disconnect,
5702 .accept = sctp_accept,
5703 .ioctl = sctp_ioctl,
5704 .init = sctp_init_sock,
5705 .destroy = sctp_destroy_sock,
5706 .shutdown = sctp_shutdown,
5707 .setsockopt = sctp_setsockopt,
5708 .getsockopt = sctp_getsockopt,
5709 .sendmsg = sctp_sendmsg,
5710 .recvmsg = sctp_recvmsg,
5711 .bind = sctp_bind,
5712 .backlog_rcv = sctp_backlog_rcv,
5713 .hash = sctp_hash,
5714 .unhash = sctp_unhash,
5715 .get_port = sctp_get_port,
5716 .obj_size = sizeof(struct sctp_sock),
5717};
5718
5719#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5720struct proto sctpv6_prot = {
5721 .name = "SCTPv6",
5722 .owner = THIS_MODULE,
5723 .close = sctp_close,
5724 .connect = sctp_connect,
5725 .disconnect = sctp_disconnect,
5726 .accept = sctp_accept,
5727 .ioctl = sctp_ioctl,
5728 .init = sctp_init_sock,
5729 .destroy = sctp_destroy_sock,
5730 .shutdown = sctp_shutdown,
5731 .setsockopt = sctp_setsockopt,
5732 .getsockopt = sctp_getsockopt,
5733 .sendmsg = sctp_sendmsg,
5734 .recvmsg = sctp_recvmsg,
5735 .bind = sctp_bind,
5736 .backlog_rcv = sctp_backlog_rcv,
5737 .hash = sctp_hash,
5738 .unhash = sctp_unhash,
5739 .get_port = sctp_get_port,
5740 .obj_size = sizeof(struct sctp6_sock),
5741};
5742#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */