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