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