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