blob: a3acf78d06bab491604c2ea7096fdf3a5eb72748 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Get the sndbuf space available at the time on the association. */
111static inline int sctp_wspace(struct sctp_association *asoc)
112{
113 struct sock *sk = asoc->base.sk;
114 int amt = 0;
115
Neil Horman4eb701d2005-04-28 12:02:04 -0700116 if (asoc->ep->sndbuf_policy) {
117 /* make sure that no association uses more than sk_sndbuf */
118 amt = sk->sk_sndbuf - asoc->sndbuf_used;
119 } else {
120 /* do socket level accounting */
121 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (amt < 0)
125 amt = 0;
Neil Horman4eb701d2005-04-28 12:02:04 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return amt;
128}
129
130/* Increment the used sndbuf space count of the corresponding association by
131 * the size of the outgoing data chunk.
132 * Also, set the skb destructor for sndbuf accounting later.
133 *
134 * Since it is always 1-1 between chunk and skb, and also a new skb is always
135 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
136 * destructor in the data chunk skb for the purpose of the sndbuf space
137 * tracking.
138 */
139static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
140{
141 struct sctp_association *asoc = chunk->asoc;
142 struct sock *sk = asoc->base.sk;
143
144 /* The sndbuf space is tracked per association. */
145 sctp_association_hold(asoc);
146
Neil Horman4eb701d2005-04-28 12:02:04 -0700147 skb_set_owner_w(chunk->skb, sk);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 chunk->skb->destructor = sctp_wfree;
150 /* Save the chunk pointer in skb for sctp_wfree to use later. */
151 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
152
Neil Horman4eb701d2005-04-28 12:02:04 -0700153 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
154 sizeof(struct sk_buff) +
155 sizeof(struct sctp_chunk);
156
Neil Horman4eb701d2005-04-28 12:02:04 -0700157 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/* Verify that this is a valid address. */
161static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
162 int len)
163{
164 struct sctp_af *af;
165
166 /* Verify basic sockaddr. */
167 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
168 if (!af)
169 return -EINVAL;
170
171 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700172 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EINVAL;
174
175 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
176 return -EINVAL;
177
178 return 0;
179}
180
181/* Look up the association by its id. If this is not a UDP-style
182 * socket, the ID field is always ignored.
183 */
184struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
185{
186 struct sctp_association *asoc = NULL;
187
188 /* If this is not a UDP-style socket, assoc id should be ignored. */
189 if (!sctp_style(sk, UDP)) {
190 /* Return NULL if the socket state is not ESTABLISHED. It
191 * could be a TCP-style listening socket or a socket which
192 * hasn't yet called connect() to establish an association.
193 */
194 if (!sctp_sstate(sk, ESTABLISHED))
195 return NULL;
196
197 /* Get the first and the only association from the list. */
198 if (!list_empty(&sctp_sk(sk)->ep->asocs))
199 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
200 struct sctp_association, asocs);
201 return asoc;
202 }
203
204 /* Otherwise this is a UDP-style socket. */
205 if (!id || (id == (sctp_assoc_t)-1))
206 return NULL;
207
208 spin_lock_bh(&sctp_assocs_id_lock);
209 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
210 spin_unlock_bh(&sctp_assocs_id_lock);
211
212 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
213 return NULL;
214
215 return asoc;
216}
217
218/* Look up the transport from an address and an assoc id. If both address and
219 * id are specified, the associations matching the address and the id should be
220 * the same.
221 */
222static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
223 struct sockaddr_storage *addr,
224 sctp_assoc_t id)
225{
226 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
227 struct sctp_transport *transport;
228 union sctp_addr *laddr = (union sctp_addr *)addr;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800231 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!addr_asoc)
235 return NULL;
236
237 id_asoc = sctp_id2assoc(sk, id);
238 if (id_asoc && (id_asoc != addr_asoc))
239 return NULL;
240
241 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
242 (union sctp_addr *)addr);
243
244 return transport;
245}
246
247/* API 3.1.2 bind() - UDP Style Syntax
248 * The syntax of bind() is,
249 *
250 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
251 *
252 * sd - the socket descriptor returned by socket().
253 * addr - the address structure (struct sockaddr_in or struct
254 * sockaddr_in6 [RFC 2553]),
255 * addr_len - the size of the address structure.
256 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700257SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 int retval = 0;
260
261 sctp_lock_sock(sk);
262
Frank Filz3f7a87d2005-06-20 13:14:57 -0700263 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
264 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Disallow binding twice. */
267 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700268 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 addr_len);
270 else
271 retval = -EINVAL;
272
273 sctp_release_sock(sk);
274
275 return retval;
276}
277
278static long sctp_get_port_local(struct sock *, union sctp_addr *);
279
280/* Verify this is a valid sockaddr. */
281static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
282 union sctp_addr *addr, int len)
283{
284 struct sctp_af *af;
285
286 /* Check minimum size. */
287 if (len < sizeof (struct sockaddr))
288 return NULL;
289
290 /* Does this PF support this AF? */
291 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
292 return NULL;
293
294 /* If we get this far, af is valid. */
295 af = sctp_get_af_specific(addr->sa.sa_family);
296
297 if (len < af->sockaddr_len)
298 return NULL;
299
300 return af;
301}
302
303/* Bind a local address either to an endpoint or to an association. */
304SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
305{
306 struct sctp_sock *sp = sctp_sk(sk);
307 struct sctp_endpoint *ep = sp->ep;
308 struct sctp_bind_addr *bp = &ep->base.bind_addr;
309 struct sctp_af *af;
310 unsigned short snum;
311 int ret = 0;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Common sockaddr verification. */
314 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700315 if (!af) {
316 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
317 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700319 }
320
321 snum = ntohs(addr->v4.sin_port);
322
323 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
324 ", port: %d, new port: %d, len: %d)\n",
325 sk,
326 addr,
327 bp->port, snum,
328 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* PF specific bind() address verification. */
331 if (!sp->pf->bind_verify(sp, addr))
332 return -EADDRNOTAVAIL;
333
Vlad Yasevich8b358052007-05-15 17:14:58 -0400334 /* We must either be unbound, or bind to the same port.
335 * It's OK to allow 0 ports if we are already bound.
336 * We'll just inhert an already bound port in this case
337 */
338 if (bp->port) {
339 if (!snum)
340 snum = bp->port;
341 else if (snum != bp->port) {
342 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 " New port %d does not match existing port "
344 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400345 return -EINVAL;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
350 return -EACCES;
351
352 /* Make sure we are allowed to bind here.
353 * The function sctp_get_port_local() does duplicate address
354 * detection.
355 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900356 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if ((ret = sctp_get_port_local(sk, addr))) {
358 if (ret == (long) sk) {
359 /* This endpoint has a conflicting address. */
360 return -EINVAL;
361 } else {
362 return -EADDRINUSE;
363 }
364 }
365
366 /* Refresh ephemeral port. */
367 if (!bp->port)
368 bp->port = inet_sk(sk)->num;
369
370 /* Add the address to the bind address list. */
371 sctp_local_bh_disable();
372 sctp_write_lock(&ep->base.addr_lock);
373
374 /* Use GFP_ATOMIC since BHs are disabled. */
Al Viro5ab7b852006-11-20 17:10:38 -0800375 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sctp_write_unlock(&ep->base.addr_lock);
377 sctp_local_bh_enable();
378
379 /* Copy back into socket for getsockname() use. */
380 if (!ret) {
381 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
382 af->to_sk_saddr(addr, sk);
383 }
384
385 return ret;
386}
387
388 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
389 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900390 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900392 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900394 * subsequent ASCONF. Note this restriction binds each side, so at any
395 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * from each endpoint).
397 */
398static int sctp_send_asconf(struct sctp_association *asoc,
399 struct sctp_chunk *chunk)
400{
401 int retval = 0;
402
403 /* If there is an outstanding ASCONF chunk, queue it for later
404 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700407 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 /* Hold the chunk until an ASCONF_ACK is received. */
412 sctp_chunk_hold(chunk);
413 retval = sctp_primitive_ASCONF(asoc, chunk);
414 if (retval)
415 sctp_chunk_free(chunk);
416 else
417 asoc->addip_last_asconf = chunk;
418
419out:
420 return retval;
421}
422
423/* Add a list of addresses as bind addresses to local endpoint or
424 * association.
425 *
426 * Basically run through each address specified in the addrs/addrcnt
427 * array/length pair, determine if it is IPv6 or IPv4 and call
428 * sctp_do_bind() on it.
429 *
430 * If any of them fails, then the operation will be reversed and the
431 * ones that were added will be removed.
432 *
433 * Only sctp_setsockopt_bindx() is supposed to call this function.
434 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200435static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 int cnt;
438 int retval = 0;
439 void *addr_buf;
440 struct sockaddr *sa_addr;
441 struct sctp_af *af;
442
443 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
444 sk, addrs, addrcnt);
445
446 addr_buf = addrs;
447 for (cnt = 0; cnt < addrcnt; cnt++) {
448 /* The list may contain either IPv4 or IPv6 address;
449 * determine the address length for walking thru the list.
450 */
451 sa_addr = (struct sockaddr *)addr_buf;
452 af = sctp_get_af_specific(sa_addr->sa_family);
453 if (!af) {
454 retval = -EINVAL;
455 goto err_bindx_add;
456 }
457
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 af->sockaddr_len);
460
461 addr_buf += af->sockaddr_len;
462
463err_bindx_add:
464 if (retval < 0) {
465 /* Failed. Cleanup the ones that have been added */
466 if (cnt > 0)
467 sctp_bindx_rem(sk, addrs, cnt);
468 return retval;
469 }
470 }
471
472 return retval;
473}
474
475/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
476 * associations that are part of the endpoint indicating that a list of local
477 * addresses are added to the endpoint.
478 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900479 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * association, we do not send the chunk for that association. But it will not
481 * affect other associations.
482 *
483 * Only sctp_setsockopt_bindx() is supposed to call this function.
484 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900485static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct sockaddr *addrs,
487 int addrcnt)
488{
489 struct sctp_sock *sp;
490 struct sctp_endpoint *ep;
491 struct sctp_association *asoc;
492 struct sctp_bind_addr *bp;
493 struct sctp_chunk *chunk;
494 struct sctp_sockaddr_entry *laddr;
495 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700496 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 void *addr_buf;
498 struct sctp_af *af;
499 struct list_head *pos;
500 struct list_head *p;
501 int i;
502 int retval = 0;
503
504 if (!sctp_addip_enable)
505 return retval;
506
507 sp = sctp_sk(sk);
508 ep = sp->ep;
509
510 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
511 __FUNCTION__, sk, addrs, addrcnt);
512
513 list_for_each(pos, &ep->asocs) {
514 asoc = list_entry(pos, struct sctp_association, asocs);
515
516 if (!asoc->peer.asconf_capable)
517 continue;
518
519 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
520 continue;
521
522 if (!sctp_state(asoc, ESTABLISHED))
523 continue;
524
525 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900526 * in the bind address list of the association. If so,
527 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * other associations.
529 */
530 addr_buf = addrs;
531 for (i = 0; i < addrcnt; i++) {
532 addr = (union sctp_addr *)addr_buf;
533 af = sctp_get_af_specific(addr->v4.sin_family);
534 if (!af) {
535 retval = -EINVAL;
536 goto out;
537 }
538
539 if (sctp_assoc_lookup_laddr(asoc, addr))
540 break;
541
542 addr_buf += af->sockaddr_len;
543 }
544 if (i < addrcnt)
545 continue;
546
547 /* Use the first address in bind addr list of association as
548 * Address Parameter of ASCONF CHUNK.
549 */
550 sctp_read_lock(&asoc->base.addr_lock);
551 bp = &asoc->base.bind_addr;
552 p = bp->address_list.next;
553 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
554 sctp_read_unlock(&asoc->base.addr_lock);
555
Al Viro5ae955c2006-11-20 17:22:08 -0800556 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 addrcnt, SCTP_PARAM_ADD_IP);
558 if (!chunk) {
559 retval = -ENOMEM;
560 goto out;
561 }
562
563 retval = sctp_send_asconf(asoc, chunk);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700564 if (retval)
565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700567 /* Add the new addresses to the bind address list with
568 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700570 sctp_local_bh_disable();
571 sctp_write_lock(&asoc->base.addr_lock);
572 addr_buf = addrs;
573 for (i = 0; i < addrcnt; i++) {
574 addr = (union sctp_addr *)addr_buf;
575 af = sctp_get_af_specific(addr->v4.sin_family);
576 memcpy(&saveaddr, addr, af->sockaddr_len);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700577 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
578 GFP_ATOMIC);
579 addr_buf += af->sockaddr_len;
580 }
581 sctp_write_unlock(&asoc->base.addr_lock);
582 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585out:
586 return retval;
587}
588
589/* Remove a list of addresses from bind addresses list. Do not remove the
590 * last address.
591 *
592 * Basically run through each address specified in the addrs/addrcnt
593 * array/length pair, determine if it is IPv6 or IPv4 and call
594 * sctp_del_bind() on it.
595 *
596 * If any of them fails, then the operation will be reversed and the
597 * ones that were removed will be added back.
598 *
599 * At least one address has to be left; if only one address is
600 * available, the operation will return -EBUSY.
601 *
602 * Only sctp_setsockopt_bindx() is supposed to call this function.
603 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200604static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct sctp_sock *sp = sctp_sk(sk);
607 struct sctp_endpoint *ep = sp->ep;
608 int cnt;
609 struct sctp_bind_addr *bp = &ep->base.bind_addr;
610 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800612 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct sctp_af *af;
614
615 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
616 sk, addrs, addrcnt);
617
618 addr_buf = addrs;
619 for (cnt = 0; cnt < addrcnt; cnt++) {
620 /* If the bind address list is empty or if there is only one
621 * bind address, there is nothing more to be removed (we need
622 * at least one address here).
623 */
624 if (list_empty(&bp->address_list) ||
625 (sctp_list_single_entry(&bp->address_list))) {
626 retval = -EBUSY;
627 goto err_bindx_rem;
628 }
629
Al Viroc9a08502006-11-20 17:07:48 -0800630 sa_addr = (union sctp_addr *)addr_buf;
631 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!af) {
633 retval = -EINVAL;
634 goto err_bindx_rem;
635 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700636
637 if (!af->addr_valid(sa_addr, sp, NULL)) {
638 retval = -EADDRNOTAVAIL;
639 goto err_bindx_rem;
640 }
641
Al Viroc9a08502006-11-20 17:07:48 -0800642 if (sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 retval = -EINVAL;
644 goto err_bindx_rem;
645 }
646
647 /* FIXME - There is probably a need to check if sk->sk_saddr and
648 * sk->sk_rcv_addr are currently set to one of the addresses to
649 * be removed. This is something which needs to be looked into
650 * when we are fixing the outstanding issues with multi-homing
651 * socket routing and failover schemes. Refer to comments in
652 * sctp_do_bind(). -daisy
653 */
654 sctp_local_bh_disable();
655 sctp_write_lock(&ep->base.addr_lock);
656
Al Viroc9a08502006-11-20 17:07:48 -0800657 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 sctp_write_unlock(&ep->base.addr_lock);
660 sctp_local_bh_enable();
661
662 addr_buf += af->sockaddr_len;
663err_bindx_rem:
664 if (retval < 0) {
665 /* Failed. Add the ones that has been removed back */
666 if (cnt > 0)
667 sctp_bindx_add(sk, addrs, cnt);
668 return retval;
669 }
670 }
671
672 return retval;
673}
674
675/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
676 * the associations that are part of the endpoint indicating that a list of
677 * local addresses are removed from the endpoint.
678 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900679 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * association, we do not send the chunk for that association. But it will not
681 * affect other associations.
682 *
683 * Only sctp_setsockopt_bindx() is supposed to call this function.
684 */
685static int sctp_send_asconf_del_ip(struct sock *sk,
686 struct sockaddr *addrs,
687 int addrcnt)
688{
689 struct sctp_sock *sp;
690 struct sctp_endpoint *ep;
691 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700692 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct sctp_bind_addr *bp;
694 struct sctp_chunk *chunk;
695 union sctp_addr *laddr;
696 void *addr_buf;
697 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700698 struct list_head *pos, *pos1;
699 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int i;
701 int retval = 0;
702
703 if (!sctp_addip_enable)
704 return retval;
705
706 sp = sctp_sk(sk);
707 ep = sp->ep;
708
709 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
710 __FUNCTION__, sk, addrs, addrcnt);
711
712 list_for_each(pos, &ep->asocs) {
713 asoc = list_entry(pos, struct sctp_association, asocs);
714
715 if (!asoc->peer.asconf_capable)
716 continue;
717
718 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
719 continue;
720
721 if (!sctp_state(asoc, ESTABLISHED))
722 continue;
723
724 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900725 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * If so, do not send the asconf chunk to its peer, but
727 * continue with other associations.
728 */
729 addr_buf = addrs;
730 for (i = 0; i < addrcnt; i++) {
731 laddr = (union sctp_addr *)addr_buf;
732 af = sctp_get_af_specific(laddr->v4.sin_family);
733 if (!af) {
734 retval = -EINVAL;
735 goto out;
736 }
737
738 if (!sctp_assoc_lookup_laddr(asoc, laddr))
739 break;
740
741 addr_buf += af->sockaddr_len;
742 }
743 if (i < addrcnt)
744 continue;
745
746 /* Find one address in the association's bind address list
747 * that is not in the packed array of addresses. This is to
748 * make sure that we do not delete all the addresses in the
749 * association.
750 */
751 sctp_read_lock(&asoc->base.addr_lock);
752 bp = &asoc->base.bind_addr;
753 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
754 addrcnt, sp);
755 sctp_read_unlock(&asoc->base.addr_lock);
756 if (!laddr)
757 continue;
758
759 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
760 SCTP_PARAM_DEL_IP);
761 if (!chunk) {
762 retval = -ENOMEM;
763 goto out;
764 }
765
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700766 /* Reset use_as_src flag for the addresses in the bind address
767 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700769 sctp_local_bh_disable();
770 sctp_write_lock(&asoc->base.addr_lock);
771 addr_buf = addrs;
772 for (i = 0; i < addrcnt; i++) {
773 laddr = (union sctp_addr *)addr_buf;
774 af = sctp_get_af_specific(laddr->v4.sin_family);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700775 list_for_each(pos1, &bp->address_list) {
776 saddr = list_entry(pos1,
777 struct sctp_sockaddr_entry,
778 list);
Al Viro5f242a12006-11-20 17:05:23 -0800779 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700780 saddr->use_as_src = 0;
781 }
782 addr_buf += af->sockaddr_len;
783 }
784 sctp_write_unlock(&asoc->base.addr_lock);
785 sctp_local_bh_enable();
786
787 /* Update the route and saddr entries for all the transports
788 * as some of the addresses in the bind address list are
789 * about to be deleted and cannot be used as source addresses.
790 */
791 list_for_each(pos1, &asoc->peer.transport_addr_list) {
792 transport = list_entry(pos1, struct sctp_transport,
793 transports);
794 dst_release(transport->dst);
795 sctp_transport_route(transport, NULL,
796 sctp_sk(asoc->base.sk));
797 }
798
799 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801out:
802 return retval;
803}
804
805/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
806 *
807 * API 8.1
808 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
809 * int flags);
810 *
811 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
812 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
813 * or IPv6 addresses.
814 *
815 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
816 * Section 3.1.2 for this usage.
817 *
818 * addrs is a pointer to an array of one or more socket addresses. Each
819 * address is contained in its appropriate structure (i.e. struct
820 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700821 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * representation is termed a "packed array" of addresses). The caller
823 * specifies the number of addresses in the array with addrcnt.
824 *
825 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
826 * -1, and sets errno to the appropriate error code.
827 *
828 * For SCTP, the port given in each socket address must be the same, or
829 * sctp_bindx() will fail, setting errno to EINVAL.
830 *
831 * The flags parameter is formed from the bitwise OR of zero or more of
832 * the following currently defined flags:
833 *
834 * SCTP_BINDX_ADD_ADDR
835 *
836 * SCTP_BINDX_REM_ADDR
837 *
838 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
839 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
840 * addresses from the association. The two flags are mutually exclusive;
841 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
842 * not remove all addresses from an association; sctp_bindx() will
843 * reject such an attempt with EINVAL.
844 *
845 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
846 * additional addresses with an endpoint after calling bind(). Or use
847 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
848 * socket is associated with so that no new association accepted will be
849 * associated with those addresses. If the endpoint supports dynamic
850 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
851 * endpoint to send the appropriate message to the peer to change the
852 * peers address lists.
853 *
854 * Adding and removing addresses from a connected association is
855 * optional functionality. Implementations that do not support this
856 * functionality should return EOPNOTSUPP.
857 *
858 * Basically do nothing but copying the addresses from user to kernel
859 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700860 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
861 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 *
863 * We don't use copy_from_user() for optimization: we first do the
864 * sanity checks (buffer size -fast- and access check-healthy
865 * pointer); if all of those succeed, then we can alloc the memory
866 * (expensive operation) needed to copy the data to kernel. Then we do
867 * the copying without checking the user space area
868 * (__copy_from_user()).
869 *
870 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
871 * it.
872 *
873 * sk The sk of the socket
874 * addrs The pointer to the addresses in user land
875 * addrssize Size of the addrs buffer
876 * op Operation to perform (add or remove, see the flags of
877 * sctp_bindx)
878 *
879 * Returns 0 if ok, <0 errno code on error.
880 */
881SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
882 struct sockaddr __user *addrs,
883 int addrs_size, int op)
884{
885 struct sockaddr *kaddrs;
886 int err;
887 int addrcnt = 0;
888 int walk_size = 0;
889 struct sockaddr *sa_addr;
890 void *addr_buf;
891 struct sctp_af *af;
892
893 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
894 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
895
896 if (unlikely(addrs_size <= 0))
897 return -EINVAL;
898
899 /* Check the user passed a healthy pointer. */
900 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
901 return -EFAULT;
902
903 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800904 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (unlikely(!kaddrs))
906 return -ENOMEM;
907
908 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
909 kfree(kaddrs);
910 return -EFAULT;
911 }
912
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900913 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 addr_buf = kaddrs;
915 while (walk_size < addrs_size) {
916 sa_addr = (struct sockaddr *)addr_buf;
917 af = sctp_get_af_specific(sa_addr->sa_family);
918
919 /* If the address family is not supported or if this address
920 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
923 kfree(kaddrs);
924 return -EINVAL;
925 }
926 addrcnt++;
927 addr_buf += af->sockaddr_len;
928 walk_size += af->sockaddr_len;
929 }
930
931 /* Do the work. */
932 switch (op) {
933 case SCTP_BINDX_ADD_ADDR:
934 err = sctp_bindx_add(sk, kaddrs, addrcnt);
935 if (err)
936 goto out;
937 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
938 break;
939
940 case SCTP_BINDX_REM_ADDR:
941 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
942 if (err)
943 goto out;
944 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
945 break;
946
947 default:
948 err = -EINVAL;
949 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952out:
953 kfree(kaddrs);
954
955 return err;
956}
957
Frank Filz3f7a87d2005-06-20 13:14:57 -0700958/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
959 *
960 * Common routine for handling connect() and sctp_connectx().
961 * Connect will come in with just a single address.
962 */
963static int __sctp_connect(struct sock* sk,
964 struct sockaddr *kaddrs,
965 int addrs_size)
966{
967 struct sctp_sock *sp;
968 struct sctp_endpoint *ep;
969 struct sctp_association *asoc = NULL;
970 struct sctp_association *asoc2;
971 struct sctp_transport *transport;
972 union sctp_addr to;
973 struct sctp_af *af;
974 sctp_scope_t scope;
975 long timeo;
976 int err = 0;
977 int addrcnt = 0;
978 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -0400979 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700980 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -0700981 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -0400982 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700983
984 sp = sctp_sk(sk);
985 ep = sp->ep;
986
987 /* connect() cannot be done on a socket that is already in ESTABLISHED
988 * state - UDP-style peeled off socket or a TCP-style socket that
989 * is already connected.
990 * It cannot be done even on a TCP-style listening socket.
991 */
992 if (sctp_sstate(sk, ESTABLISHED) ||
993 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
994 err = -EISCONN;
995 goto out_free;
996 }
997
998 /* Walk through the addrs buffer and count the number of addresses. */
999 addr_buf = kaddrs;
1000 while (walk_size < addrs_size) {
Al Viro4bdf4b52006-11-20 17:10:20 -08001001 sa_addr = (union sctp_addr *)addr_buf;
1002 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001003 port = ntohs(sa_addr->v4.sin_port);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
1007 */
1008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 err = -EINVAL;
1010 goto out_free;
1011 }
1012
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001013 /* Save current address so we can work with it */
1014 memcpy(&to, sa_addr, af->sockaddr_len);
1015
1016 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001017 if (err)
1018 goto out_free;
1019
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001020 /* Make sure the destination port is correctly set
1021 * in all addresses.
1022 */
1023 if (asoc && asoc->peer.port && asoc->peer.port != port)
1024 goto out_free;
1025
Frank Filz3f7a87d2005-06-20 13:14:57 -07001026
1027 /* Check if there already is a matching association on the
1028 * endpoint (other than the one created here).
1029 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001030 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001031 if (asoc2 && asoc2 != asoc) {
1032 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1033 err = -EISCONN;
1034 else
1035 err = -EALREADY;
1036 goto out_free;
1037 }
1038
1039 /* If we could not find a matching association on the endpoint,
1040 * make sure that there is no peeled-off association matching
1041 * the peer address even on another socket.
1042 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001043 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001044 err = -EADDRNOTAVAIL;
1045 goto out_free;
1046 }
1047
1048 if (!asoc) {
1049 /* If a bind() or sctp_bindx() is not called prior to
1050 * an sctp_connectx() call, the system picks an
1051 * ephemeral port and will choose an address set
1052 * equivalent to binding with a wildcard address.
1053 */
1054 if (!ep->base.bind_addr.port) {
1055 if (sctp_autobind(sk)) {
1056 err = -EAGAIN;
1057 goto out_free;
1058 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001059 } else {
1060 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001061 * If an unprivileged user inherits a 1-many
1062 * style socket with open associations on a
1063 * privileged port, it MAY be permitted to
1064 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001065 * be permitted to open new associations.
1066 */
1067 if (ep->base.bind_addr.port < PROT_SOCK &&
1068 !capable(CAP_NET_BIND_SERVICE)) {
1069 err = -EACCES;
1070 goto out_free;
1071 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001072 }
1073
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001074 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001075 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1076 if (!asoc) {
1077 err = -ENOMEM;
1078 goto out_free;
1079 }
1080 }
1081
1082 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001083 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001084 SCTP_UNKNOWN);
1085 if (!transport) {
1086 err = -ENOMEM;
1087 goto out_free;
1088 }
1089
1090 addrcnt++;
1091 addr_buf += af->sockaddr_len;
1092 walk_size += af->sockaddr_len;
1093 }
1094
1095 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1096 if (err < 0) {
1097 goto out_free;
1098 }
1099
1100 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1101 if (err < 0) {
1102 goto out_free;
1103 }
1104
1105 /* Initialize sk's dport and daddr for getpeername() */
1106 inet_sk(sk)->dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001107 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1108 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001109 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001110
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001111 /* in-kernel sockets don't generally have a file allocated to them
1112 * if all they do is call sock_create_kern().
1113 */
1114 if (sk->sk_socket->file)
1115 f_flags = sk->sk_socket->file->f_flags;
1116
1117 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1118
Frank Filz3f7a87d2005-06-20 13:14:57 -07001119 err = sctp_wait_for_connect(asoc, &timeo);
1120
1121 /* Don't free association on exit. */
1122 asoc = NULL;
1123
1124out_free:
1125
1126 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001127 " kaddrs: %p err: %d\n",
1128 asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001129 if (asoc)
1130 sctp_association_free(asoc);
1131 return err;
1132}
1133
1134/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1135 *
1136 * API 8.9
1137 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1138 *
1139 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1140 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1141 * or IPv6 addresses.
1142 *
1143 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1144 * Section 3.1.2 for this usage.
1145 *
1146 * addrs is a pointer to an array of one or more socket addresses. Each
1147 * address is contained in its appropriate structure (i.e. struct
1148 * sockaddr_in or struct sockaddr_in6) the family of the address type
1149 * must be used to distengish the address length (note that this
1150 * representation is termed a "packed array" of addresses). The caller
1151 * specifies the number of addresses in the array with addrcnt.
1152 *
1153 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1154 * -1, and sets errno to the appropriate error code.
1155 *
1156 * For SCTP, the port given in each socket address must be the same, or
1157 * sctp_connectx() will fail, setting errno to EINVAL.
1158 *
1159 * An application can use sctp_connectx to initiate an association with
1160 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1161 * allows a caller to specify multiple addresses at which a peer can be
1162 * reached. The way the SCTP stack uses the list of addresses to set up
1163 * the association is implementation dependant. This function only
1164 * specifies that the stack will try to make use of all the addresses in
1165 * the list when needed.
1166 *
1167 * Note that the list of addresses passed in is only used for setting up
1168 * the association. It does not necessarily equal the set of addresses
1169 * the peer uses for the resulting association. If the caller wants to
1170 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1171 * retrieve them after the association has been set up.
1172 *
1173 * Basically do nothing but copying the addresses from user to kernel
1174 * land and invoking either sctp_connectx(). This is used for tunneling
1175 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1176 *
1177 * We don't use copy_from_user() for optimization: we first do the
1178 * sanity checks (buffer size -fast- and access check-healthy
1179 * pointer); if all of those succeed, then we can alloc the memory
1180 * (expensive operation) needed to copy the data to kernel. Then we do
1181 * the copying without checking the user space area
1182 * (__copy_from_user()).
1183 *
1184 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1185 * it.
1186 *
1187 * sk The sk of the socket
1188 * addrs The pointer to the addresses in user land
1189 * addrssize Size of the addrs buffer
1190 *
1191 * Returns 0 if ok, <0 errno code on error.
1192 */
1193SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1194 struct sockaddr __user *addrs,
1195 int addrs_size)
1196{
1197 int err = 0;
1198 struct sockaddr *kaddrs;
1199
1200 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1201 __FUNCTION__, sk, addrs, addrs_size);
1202
1203 if (unlikely(addrs_size <= 0))
1204 return -EINVAL;
1205
1206 /* Check the user passed a healthy pointer. */
1207 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1208 return -EFAULT;
1209
1210 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001211 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001212 if (unlikely(!kaddrs))
1213 return -ENOMEM;
1214
1215 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1216 err = -EFAULT;
1217 } else {
1218 err = __sctp_connect(sk, kaddrs, addrs_size);
1219 }
1220
1221 kfree(kaddrs);
1222 return err;
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/* API 3.1.4 close() - UDP Style Syntax
1226 * Applications use close() to perform graceful shutdown (as described in
1227 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1228 * by a UDP-style socket.
1229 *
1230 * The syntax is
1231 *
1232 * ret = close(int sd);
1233 *
1234 * sd - the socket descriptor of the associations to be closed.
1235 *
1236 * To gracefully shutdown a specific association represented by the
1237 * UDP-style socket, an application should use the sendmsg() call,
1238 * passing no user data, but including the appropriate flag in the
1239 * ancillary data (see Section xxxx).
1240 *
1241 * If sd in the close() call is a branched-off socket representing only
1242 * one association, the shutdown is performed on that association only.
1243 *
1244 * 4.1.6 close() - TCP Style Syntax
1245 *
1246 * Applications use close() to gracefully close down an association.
1247 *
1248 * The syntax is:
1249 *
1250 * int close(int sd);
1251 *
1252 * sd - the socket descriptor of the association to be closed.
1253 *
1254 * After an application calls close() on a socket descriptor, no further
1255 * socket operations will succeed on that descriptor.
1256 *
1257 * API 7.1.4 SO_LINGER
1258 *
1259 * An application using the TCP-style socket can use this option to
1260 * perform the SCTP ABORT primitive. The linger option structure is:
1261 *
1262 * struct linger {
1263 * int l_onoff; // option on/off
1264 * int l_linger; // linger time
1265 * };
1266 *
1267 * To enable the option, set l_onoff to 1. If the l_linger value is set
1268 * to 0, calling close() is the same as the ABORT primitive. If the
1269 * value is set to a negative value, the setsockopt() call will return
1270 * an error. If the value is set to a positive value linger_time, the
1271 * close() can be blocked for at most linger_time ms. If the graceful
1272 * shutdown phase does not finish during this period, close() will
1273 * return but the graceful shutdown phase continues in the system.
1274 */
1275SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1276{
1277 struct sctp_endpoint *ep;
1278 struct sctp_association *asoc;
1279 struct list_head *pos, *temp;
1280
1281 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1282
1283 sctp_lock_sock(sk);
1284 sk->sk_shutdown = SHUTDOWN_MASK;
1285
1286 ep = sctp_sk(sk)->ep;
1287
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001288 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 list_for_each_safe(pos, temp, &ep->asocs) {
1290 asoc = list_entry(pos, struct sctp_association, asocs);
1291
1292 if (sctp_style(sk, TCP)) {
1293 /* A closed association can still be in the list if
1294 * it belongs to a TCP-style listening socket that is
1295 * not yet accepted. If so, free it. If not, send an
1296 * ABORT or SHUTDOWN based on the linger options.
1297 */
1298 if (sctp_state(asoc, CLOSED)) {
1299 sctp_unhash_established(asoc);
1300 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001301 continue;
1302 }
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001305 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1306 struct sctp_chunk *chunk;
1307
1308 chunk = sctp_make_abort_user(asoc, NULL, 0);
1309 if (chunk)
1310 sctp_primitive_ABORT(asoc, chunk);
1311 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 sctp_primitive_SHUTDOWN(asoc, NULL);
1313 }
1314
1315 /* Clean up any skbs sitting on the receive queue. */
1316 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1317 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1318
1319 /* On a TCP-style socket, block for at most linger_time if set. */
1320 if (sctp_style(sk, TCP) && timeout)
1321 sctp_wait_for_close(sk, timeout);
1322
1323 /* This will run the backlog queue. */
1324 sctp_release_sock(sk);
1325
1326 /* Supposedly, no process has access to the socket, but
1327 * the net layers still may.
1328 */
1329 sctp_local_bh_disable();
1330 sctp_bh_lock_sock(sk);
1331
1332 /* Hold the sock, since sk_common_release() will put sock_put()
1333 * and we have just a little more cleanup.
1334 */
1335 sock_hold(sk);
1336 sk_common_release(sk);
1337
1338 sctp_bh_unlock_sock(sk);
1339 sctp_local_bh_enable();
1340
1341 sock_put(sk);
1342
1343 SCTP_DBG_OBJCNT_DEC(sock);
1344}
1345
1346/* Handle EPIPE error. */
1347static int sctp_error(struct sock *sk, int flags, int err)
1348{
1349 if (err == -EPIPE)
1350 err = sock_error(sk) ? : -EPIPE;
1351 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1352 send_sig(SIGPIPE, current, 0);
1353 return err;
1354}
1355
1356/* API 3.1.3 sendmsg() - UDP Style Syntax
1357 *
1358 * An application uses sendmsg() and recvmsg() calls to transmit data to
1359 * and receive data from its peer.
1360 *
1361 * ssize_t sendmsg(int socket, const struct msghdr *message,
1362 * int flags);
1363 *
1364 * socket - the socket descriptor of the endpoint.
1365 * message - pointer to the msghdr structure which contains a single
1366 * user message and possibly some ancillary data.
1367 *
1368 * See Section 5 for complete description of the data
1369 * structures.
1370 *
1371 * flags - flags sent or received with the user message, see Section
1372 * 5 for complete description of the flags.
1373 *
1374 * Note: This function could use a rewrite especially when explicit
1375 * connect support comes in.
1376 */
1377/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1378
1379SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1380
1381SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1382 struct msghdr *msg, size_t msg_len)
1383{
1384 struct sctp_sock *sp;
1385 struct sctp_endpoint *ep;
1386 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1387 struct sctp_transport *transport, *chunk_tp;
1388 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001389 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct sockaddr *msg_name = NULL;
1391 struct sctp_sndrcvinfo default_sinfo = { 0 };
1392 struct sctp_sndrcvinfo *sinfo;
1393 struct sctp_initmsg *sinit;
1394 sctp_assoc_t associd = 0;
1395 sctp_cmsgs_t cmsgs = { NULL };
1396 int err;
1397 sctp_scope_t scope;
1398 long timeo;
1399 __u16 sinfo_flags = 0;
1400 struct sctp_datamsg *datamsg;
1401 struct list_head *pos;
1402 int msg_flags = msg->msg_flags;
1403
1404 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1405 sk, msg, msg_len);
1406
1407 err = 0;
1408 sp = sctp_sk(sk);
1409 ep = sp->ep;
1410
Frank Filz3f7a87d2005-06-20 13:14:57 -07001411 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /* We cannot send a message over a TCP-style listening socket. */
1414 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1415 err = -EPIPE;
1416 goto out_nounlock;
1417 }
1418
1419 /* Parse out the SCTP CMSGs. */
1420 err = sctp_msghdr_parse(msg, &cmsgs);
1421
1422 if (err) {
1423 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1424 goto out_nounlock;
1425 }
1426
1427 /* Fetch the destination address for this packet. This
1428 * address only selects the association--it is not necessarily
1429 * the address we will send to.
1430 * For a peeled-off socket, msg_name is ignored.
1431 */
1432 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1433 int msg_namelen = msg->msg_namelen;
1434
1435 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1436 msg_namelen);
1437 if (err)
1438 return err;
1439
1440 if (msg_namelen > sizeof(to))
1441 msg_namelen = sizeof(to);
1442 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 msg_name = msg->msg_name;
1444 }
1445
1446 sinfo = cmsgs.info;
1447 sinit = cmsgs.init;
1448
1449 /* Did the user specify SNDRCVINFO? */
1450 if (sinfo) {
1451 sinfo_flags = sinfo->sinfo_flags;
1452 associd = sinfo->sinfo_assoc_id;
1453 }
1454
1455 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1456 msg_len, sinfo_flags);
1457
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001458 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1459 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 err = -EINVAL;
1461 goto out_nounlock;
1462 }
1463
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001464 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1465 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1466 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001468 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001469 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1470 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 err = -EINVAL;
1472 goto out_nounlock;
1473 }
1474
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001475 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * specified in msg_name.
1477 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001478 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 err = -EINVAL;
1480 goto out_nounlock;
1481 }
1482
1483 transport = NULL;
1484
1485 SCTP_DEBUG_PRINTK("About to look up association.\n");
1486
1487 sctp_lock_sock(sk);
1488
1489 /* If a msg_name has been specified, assume this is to be used. */
1490 if (msg_name) {
1491 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001492 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!asoc) {
1494 /* If we could not find a matching association on the
1495 * endpoint, make sure that it is not a TCP-style
1496 * socket that already has an association or there is
1497 * no peeled-off association on another socket.
1498 */
1499 if ((sctp_style(sk, TCP) &&
1500 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001501 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 err = -EADDRNOTAVAIL;
1503 goto out_unlock;
1504 }
1505 }
1506 } else {
1507 asoc = sctp_id2assoc(sk, associd);
1508 if (!asoc) {
1509 err = -EPIPE;
1510 goto out_unlock;
1511 }
1512 }
1513
1514 if (asoc) {
1515 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1516
1517 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1518 * socket that has an association in CLOSED state. This can
1519 * happen when an accepted socket has an association that is
1520 * already CLOSED.
1521 */
1522 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1523 err = -EPIPE;
1524 goto out_unlock;
1525 }
1526
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001527 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1529 asoc);
1530 sctp_primitive_SHUTDOWN(asoc, NULL);
1531 err = 0;
1532 goto out_unlock;
1533 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001534 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001535
1536 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1537 if (!chunk) {
1538 err = -ENOMEM;
1539 goto out_unlock;
1540 }
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001543 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 err = 0;
1545 goto out_unlock;
1546 }
1547 }
1548
1549 /* Do we need to create the association? */
1550 if (!asoc) {
1551 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1552
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001553 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 err = -EINVAL;
1555 goto out_unlock;
1556 }
1557
1558 /* Check for invalid stream against the stream counts,
1559 * either the default or the user specified stream counts.
1560 */
1561 if (sinfo) {
1562 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1563 /* Check against the defaults. */
1564 if (sinfo->sinfo_stream >=
1565 sp->initmsg.sinit_num_ostreams) {
1566 err = -EINVAL;
1567 goto out_unlock;
1568 }
1569 } else {
1570 /* Check against the requested. */
1571 if (sinfo->sinfo_stream >=
1572 sinit->sinit_num_ostreams) {
1573 err = -EINVAL;
1574 goto out_unlock;
1575 }
1576 }
1577 }
1578
1579 /*
1580 * API 3.1.2 bind() - UDP Style Syntax
1581 * If a bind() or sctp_bindx() is not called prior to a
1582 * sendmsg() call that initiates a new association, the
1583 * system picks an ephemeral port and will choose an address
1584 * set equivalent to binding with a wildcard address.
1585 */
1586 if (!ep->base.bind_addr.port) {
1587 if (sctp_autobind(sk)) {
1588 err = -EAGAIN;
1589 goto out_unlock;
1590 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001591 } else {
1592 /*
1593 * If an unprivileged user inherits a one-to-many
1594 * style socket with open associations on a privileged
1595 * port, it MAY be permitted to accept new associations,
1596 * but it SHOULD NOT be permitted to open new
1597 * associations.
1598 */
1599 if (ep->base.bind_addr.port < PROT_SOCK &&
1600 !capable(CAP_NET_BIND_SERVICE)) {
1601 err = -EACCES;
1602 goto out_unlock;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
1606 scope = sctp_scope(&to);
1607 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1608 if (!new_asoc) {
1609 err = -ENOMEM;
1610 goto out_unlock;
1611 }
1612 asoc = new_asoc;
1613
1614 /* If the SCTP_INIT ancillary data is specified, set all
1615 * the association init values accordingly.
1616 */
1617 if (sinit) {
1618 if (sinit->sinit_num_ostreams) {
1619 asoc->c.sinit_num_ostreams =
1620 sinit->sinit_num_ostreams;
1621 }
1622 if (sinit->sinit_max_instreams) {
1623 asoc->c.sinit_max_instreams =
1624 sinit->sinit_max_instreams;
1625 }
1626 if (sinit->sinit_max_attempts) {
1627 asoc->max_init_attempts
1628 = sinit->sinit_max_attempts;
1629 }
1630 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001631 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1633 }
1634 }
1635
1636 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001637 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!transport) {
1639 err = -ENOMEM;
1640 goto out_free;
1641 }
1642 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1643 if (err < 0) {
1644 err = -ENOMEM;
1645 goto out_free;
1646 }
1647 }
1648
1649 /* ASSERT: we have a valid association at this point. */
1650 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1651
1652 if (!sinfo) {
1653 /* If the user didn't specify SNDRCVINFO, make up one with
1654 * some defaults.
1655 */
1656 default_sinfo.sinfo_stream = asoc->default_stream;
1657 default_sinfo.sinfo_flags = asoc->default_flags;
1658 default_sinfo.sinfo_ppid = asoc->default_ppid;
1659 default_sinfo.sinfo_context = asoc->default_context;
1660 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1661 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1662 sinfo = &default_sinfo;
1663 }
1664
1665 /* API 7.1.7, the sndbuf size per association bounds the
1666 * maximum size of data that can be sent in a single send call.
1667 */
1668 if (msg_len > sk->sk_sndbuf) {
1669 err = -EMSGSIZE;
1670 goto out_free;
1671 }
1672
Vlad Yasevich8a479492007-06-07 14:21:05 -04001673 if (asoc->pmtu_pending)
1674 sctp_assoc_pending_pmtu(asoc);
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* If fragmentation is disabled and the message length exceeds the
1677 * association fragmentation point, return EMSGSIZE. The I-D
1678 * does not specify what this error is, but this looks like
1679 * a great fit.
1680 */
1681 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1682 err = -EMSGSIZE;
1683 goto out_free;
1684 }
1685
1686 if (sinfo) {
1687 /* Check for invalid stream. */
1688 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1689 err = -EINVAL;
1690 goto out_free;
1691 }
1692 }
1693
1694 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1695 if (!sctp_wspace(asoc)) {
1696 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1697 if (err)
1698 goto out_free;
1699 }
1700
1701 /* If an address is passed with the sendto/sendmsg call, it is used
1702 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001703 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001706 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001707 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!chunk_tp) {
1709 err = -EINVAL;
1710 goto out_free;
1711 }
1712 } else
1713 chunk_tp = NULL;
1714
1715 /* Auto-connect, if we aren't connected already. */
1716 if (sctp_state(asoc, CLOSED)) {
1717 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1718 if (err < 0)
1719 goto out_free;
1720 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1721 }
1722
1723 /* Break the message into multiple chunks of maximum size. */
1724 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1725 if (!datamsg) {
1726 err = -ENOMEM;
1727 goto out_free;
1728 }
1729
1730 /* Now send the (possibly) fragmented message. */
1731 list_for_each(pos, &datamsg->chunks) {
1732 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1733 sctp_datamsg_track(chunk);
1734
1735 /* Do accounting for the write space. */
1736 sctp_set_owner_w(chunk);
1737
1738 chunk->transport = chunk_tp;
1739
1740 /* Send it to the lower layers. Note: all chunks
1741 * must either fail or succeed. The lower layer
1742 * works that way today. Keep it that way or this
1743 * breaks.
1744 */
1745 err = sctp_primitive_SEND(asoc, chunk);
1746 /* Did the lower layer accept the chunk? */
1747 if (err)
1748 sctp_chunk_free(chunk);
1749 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1750 }
1751
1752 sctp_datamsg_free(datamsg);
1753 if (err)
1754 goto out_free;
1755 else
1756 err = msg_len;
1757
1758 /* If we are already past ASSOCIATE, the lower
1759 * layers are responsible for association cleanup.
1760 */
1761 goto out_unlock;
1762
1763out_free:
1764 if (new_asoc)
1765 sctp_association_free(asoc);
1766out_unlock:
1767 sctp_release_sock(sk);
1768
1769out_nounlock:
1770 return sctp_error(sk, msg_flags, err);
1771
1772#if 0
1773do_sock_err:
1774 if (msg_len)
1775 err = msg_len;
1776 else
1777 err = sock_error(sk);
1778 goto out;
1779
1780do_interrupted:
1781 if (msg_len)
1782 err = msg_len;
1783 goto out;
1784#endif /* 0 */
1785}
1786
1787/* This is an extended version of skb_pull() that removes the data from the
1788 * start of a skb even when data is spread across the list of skb's in the
1789 * frag_list. len specifies the total amount of data that needs to be removed.
1790 * when 'len' bytes could be removed from the skb, it returns 0.
1791 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1792 * could not be removed.
1793 */
1794static int sctp_skb_pull(struct sk_buff *skb, int len)
1795{
1796 struct sk_buff *list;
1797 int skb_len = skb_headlen(skb);
1798 int rlen;
1799
1800 if (len <= skb_len) {
1801 __skb_pull(skb, len);
1802 return 0;
1803 }
1804 len -= skb_len;
1805 __skb_pull(skb, skb_len);
1806
1807 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1808 rlen = sctp_skb_pull(list, len);
1809 skb->len -= (len-rlen);
1810 skb->data_len -= (len-rlen);
1811
1812 if (!rlen)
1813 return 0;
1814
1815 len = rlen;
1816 }
1817
1818 return len;
1819}
1820
1821/* API 3.1.3 recvmsg() - UDP Style Syntax
1822 *
1823 * ssize_t recvmsg(int socket, struct msghdr *message,
1824 * int flags);
1825 *
1826 * socket - the socket descriptor of the endpoint.
1827 * message - pointer to the msghdr structure which contains a single
1828 * user message and possibly some ancillary data.
1829 *
1830 * See Section 5 for complete description of the data
1831 * structures.
1832 *
1833 * flags - flags sent or received with the user message, see Section
1834 * 5 for complete description of the flags.
1835 */
1836static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1837
1838SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1839 struct msghdr *msg, size_t len, int noblock,
1840 int flags, int *addr_len)
1841{
1842 struct sctp_ulpevent *event = NULL;
1843 struct sctp_sock *sp = sctp_sk(sk);
1844 struct sk_buff *skb;
1845 int copied;
1846 int err = 0;
1847 int skb_len;
1848
1849 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1850 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1851 "len", len, "knoblauch", noblock,
1852 "flags", flags, "addr_len", addr_len);
1853
1854 sctp_lock_sock(sk);
1855
1856 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1857 err = -ENOTCONN;
1858 goto out;
1859 }
1860
1861 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1862 if (!skb)
1863 goto out;
1864
1865 /* Get the total length of the skb including any skb's in the
1866 * frag_list.
1867 */
1868 skb_len = skb->len;
1869
1870 copied = skb_len;
1871 if (copied > len)
1872 copied = len;
1873
1874 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1875
1876 event = sctp_skb2event(skb);
1877
1878 if (err)
1879 goto out_free;
1880
1881 sock_recv_timestamp(msg, sk, skb);
1882 if (sctp_ulpevent_is_notification(event)) {
1883 msg->msg_flags |= MSG_NOTIFICATION;
1884 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1885 } else {
1886 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1887 }
1888
1889 /* Check if we allow SCTP_SNDRCVINFO. */
1890 if (sp->subscribe.sctp_data_io_event)
1891 sctp_ulpevent_read_sndrcvinfo(event, msg);
1892#if 0
1893 /* FIXME: we should be calling IP/IPv6 layers. */
1894 if (sk->sk_protinfo.af_inet.cmsg_flags)
1895 ip_cmsg_recv(msg, skb);
1896#endif
1897
1898 err = copied;
1899
1900 /* If skb's length exceeds the user's buffer, update the skb and
1901 * push it back to the receive_queue so that the next call to
1902 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1903 */
1904 if (skb_len > copied) {
1905 msg->msg_flags &= ~MSG_EOR;
1906 if (flags & MSG_PEEK)
1907 goto out_free;
1908 sctp_skb_pull(skb, copied);
1909 skb_queue_head(&sk->sk_receive_queue, skb);
1910
1911 /* When only partial message is copied to the user, increase
1912 * rwnd by that amount. If all the data in the skb is read,
1913 * rwnd is updated when the event is freed.
1914 */
1915 sctp_assoc_rwnd_increase(event->asoc, copied);
1916 goto out;
1917 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1918 (event->msg_flags & MSG_EOR))
1919 msg->msg_flags |= MSG_EOR;
1920 else
1921 msg->msg_flags &= ~MSG_EOR;
1922
1923out_free:
1924 if (flags & MSG_PEEK) {
1925 /* Release the skb reference acquired after peeking the skb in
1926 * sctp_skb_recv_datagram().
1927 */
1928 kfree_skb(skb);
1929 } else {
1930 /* Free the event which includes releasing the reference to
1931 * the owner of the skb, freeing the skb and updating the
1932 * rwnd.
1933 */
1934 sctp_ulpevent_free(event);
1935 }
1936out:
1937 sctp_release_sock(sk);
1938 return err;
1939}
1940
1941/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1942 *
1943 * This option is a on/off flag. If enabled no SCTP message
1944 * fragmentation will be performed. Instead if a message being sent
1945 * exceeds the current PMTU size, the message will NOT be sent and
1946 * instead a error will be indicated to the user.
1947 */
1948static int sctp_setsockopt_disable_fragments(struct sock *sk,
1949 char __user *optval, int optlen)
1950{
1951 int val;
1952
1953 if (optlen < sizeof(int))
1954 return -EINVAL;
1955
1956 if (get_user(val, (int __user *)optval))
1957 return -EFAULT;
1958
1959 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1960
1961 return 0;
1962}
1963
1964static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1965 int optlen)
1966{
1967 if (optlen != sizeof(struct sctp_event_subscribe))
1968 return -EINVAL;
1969 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1970 return -EFAULT;
1971 return 0;
1972}
1973
1974/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1975 *
1976 * This socket option is applicable to the UDP-style socket only. When
1977 * set it will cause associations that are idle for more than the
1978 * specified number of seconds to automatically close. An association
1979 * being idle is defined an association that has NOT sent or received
1980 * user data. The special value of '0' indicates that no automatic
1981 * close of any associations should be performed. The option expects an
1982 * integer defining the number of seconds of idle time before an
1983 * association is closed.
1984 */
1985static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1986 int optlen)
1987{
1988 struct sctp_sock *sp = sctp_sk(sk);
1989
1990 /* Applicable to UDP-style socket only */
1991 if (sctp_style(sk, TCP))
1992 return -EOPNOTSUPP;
1993 if (optlen != sizeof(int))
1994 return -EINVAL;
1995 if (copy_from_user(&sp->autoclose, optval, optlen))
1996 return -EFAULT;
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
2000
2001/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2002 *
2003 * Applications can enable or disable heartbeats for any peer address of
2004 * an association, modify an address's heartbeat interval, force a
2005 * heartbeat to be sent immediately, and adjust the address's maximum
2006 * number of retransmissions sent before an address is considered
2007 * unreachable. The following structure is used to access and modify an
2008 * address's parameters:
2009 *
2010 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002011 * sctp_assoc_t spp_assoc_id;
2012 * struct sockaddr_storage spp_address;
2013 * uint32_t spp_hbinterval;
2014 * uint16_t spp_pathmaxrxt;
2015 * uint32_t spp_pathmtu;
2016 * uint32_t spp_sackdelay;
2017 * uint32_t spp_flags;
2018 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002020 * spp_assoc_id - (one-to-many style socket) This is filled in the
2021 * application, and identifies the association for
2022 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * spp_address - This specifies which address is of interest.
2024 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002025 * in milliseconds. If a value of zero
2026 * is present in this field then no changes are to
2027 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * spp_pathmaxrxt - This contains the maximum number of
2029 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002030 * considered unreachable. If a value of zero
2031 * is present in this field then no changes are to
2032 * be made to this parameter.
2033 * spp_pathmtu - When Path MTU discovery is disabled the value
2034 * specified here will be the "fixed" path mtu.
2035 * Note that if the spp_address field is empty
2036 * then all associations on this address will
2037 * have this fixed path mtu set upon them.
2038 *
2039 * spp_sackdelay - When delayed sack is enabled, this value specifies
2040 * the number of milliseconds that sacks will be delayed
2041 * for. This value will apply to all addresses of an
2042 * association if the spp_address field is empty. Note
2043 * also, that if delayed sack is enabled and this
2044 * value is set to 0, no change is made to the last
2045 * recorded delayed sack timer value.
2046 *
2047 * spp_flags - These flags are used to control various features
2048 * on an association. The flag field may contain
2049 * zero or more of the following options.
2050 *
2051 * SPP_HB_ENABLE - Enable heartbeats on the
2052 * specified address. Note that if the address
2053 * field is empty all addresses for the association
2054 * have heartbeats enabled upon them.
2055 *
2056 * SPP_HB_DISABLE - Disable heartbeats on the
2057 * speicifed address. Note that if the address
2058 * field is empty all addresses for the association
2059 * will have their heartbeats disabled. Note also
2060 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2061 * mutually exclusive, only one of these two should
2062 * be specified. Enabling both fields will have
2063 * undetermined results.
2064 *
2065 * SPP_HB_DEMAND - Request a user initiated heartbeat
2066 * to be made immediately.
2067 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002068 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2069 * heartbeat delayis to be set to the value of 0
2070 * milliseconds.
2071 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002072 * SPP_PMTUD_ENABLE - This field will enable PMTU
2073 * discovery upon the specified address. Note that
2074 * if the address feild is empty then all addresses
2075 * on the association are effected.
2076 *
2077 * SPP_PMTUD_DISABLE - This field will disable PMTU
2078 * discovery upon the specified address. Note that
2079 * if the address feild is empty then all addresses
2080 * on the association are effected. Not also that
2081 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2082 * exclusive. Enabling both will have undetermined
2083 * results.
2084 *
2085 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2086 * on delayed sack. The time specified in spp_sackdelay
2087 * is used to specify the sack delay for this address. Note
2088 * that if spp_address is empty then all addresses will
2089 * enable delayed sack and take on the sack delay
2090 * value specified in spp_sackdelay.
2091 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2092 * off delayed sack. If the spp_address field is blank then
2093 * delayed sack is disabled for the entire association. Note
2094 * also that this field is mutually exclusive to
2095 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2096 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
Adrian Bunk16164362006-09-18 00:40:38 -07002098static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2099 struct sctp_transport *trans,
2100 struct sctp_association *asoc,
2101 struct sctp_sock *sp,
2102 int hb_change,
2103 int pmtud_change,
2104 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int error;
2107
Frank Filz52ccb8e2005-12-22 11:36:46 -08002108 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2110 if (error)
2111 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002114 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2115 * this field is ignored. Note also that a value of zero indicates
2116 * the current setting should be left unchanged.
2117 */
2118 if (params->spp_flags & SPP_HB_ENABLE) {
2119
2120 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2121 * set. This lets us use 0 value when this flag
2122 * is set.
2123 */
2124 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2125 params->spp_hbinterval = 0;
2126
2127 if (params->spp_hbinterval ||
2128 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2129 if (trans) {
2130 trans->hbinterval =
2131 msecs_to_jiffies(params->spp_hbinterval);
2132 } else if (asoc) {
2133 asoc->hbinterval =
2134 msecs_to_jiffies(params->spp_hbinterval);
2135 } else {
2136 sp->hbinterval = params->spp_hbinterval;
2137 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002138 }
2139 }
2140
2141 if (hb_change) {
2142 if (trans) {
2143 trans->param_flags =
2144 (trans->param_flags & ~SPP_HB) | hb_change;
2145 } else if (asoc) {
2146 asoc->param_flags =
2147 (asoc->param_flags & ~SPP_HB) | hb_change;
2148 } else {
2149 sp->param_flags =
2150 (sp->param_flags & ~SPP_HB) | hb_change;
2151 }
2152 }
2153
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002154 /* When Path MTU discovery is disabled the value specified here will
2155 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2156 * include the flag SPP_PMTUD_DISABLE for this field to have any
2157 * effect).
2158 */
2159 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002160 if (trans) {
2161 trans->pathmtu = params->spp_pathmtu;
2162 sctp_assoc_sync_pmtu(asoc);
2163 } else if (asoc) {
2164 asoc->pathmtu = params->spp_pathmtu;
2165 sctp_frag_point(sp, params->spp_pathmtu);
2166 } else {
2167 sp->pathmtu = params->spp_pathmtu;
2168 }
2169 }
2170
2171 if (pmtud_change) {
2172 if (trans) {
2173 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2174 (params->spp_flags & SPP_PMTUD_ENABLE);
2175 trans->param_flags =
2176 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2177 if (update) {
2178 sctp_transport_pmtu(trans);
2179 sctp_assoc_sync_pmtu(asoc);
2180 }
2181 } else if (asoc) {
2182 asoc->param_flags =
2183 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2184 } else {
2185 sp->param_flags =
2186 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2187 }
2188 }
2189
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002190 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2191 * value of this field is ignored. Note also that a value of zero
2192 * indicates the current setting should be left unchanged.
2193 */
2194 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002195 if (trans) {
2196 trans->sackdelay =
2197 msecs_to_jiffies(params->spp_sackdelay);
2198 } else if (asoc) {
2199 asoc->sackdelay =
2200 msecs_to_jiffies(params->spp_sackdelay);
2201 } else {
2202 sp->sackdelay = params->spp_sackdelay;
2203 }
2204 }
2205
2206 if (sackdelay_change) {
2207 if (trans) {
2208 trans->param_flags =
2209 (trans->param_flags & ~SPP_SACKDELAY) |
2210 sackdelay_change;
2211 } else if (asoc) {
2212 asoc->param_flags =
2213 (asoc->param_flags & ~SPP_SACKDELAY) |
2214 sackdelay_change;
2215 } else {
2216 sp->param_flags =
2217 (sp->param_flags & ~SPP_SACKDELAY) |
2218 sackdelay_change;
2219 }
2220 }
2221
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002222 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2223 * of this field is ignored. Note also that a value of zero
2224 * indicates the current setting should be left unchanged.
2225 */
2226 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002227 if (trans) {
2228 trans->pathmaxrxt = params->spp_pathmaxrxt;
2229 } else if (asoc) {
2230 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2231 } else {
2232 sp->pathmaxrxt = params->spp_pathmaxrxt;
2233 }
2234 }
2235
2236 return 0;
2237}
2238
2239static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2240 char __user *optval, int optlen)
2241{
2242 struct sctp_paddrparams params;
2243 struct sctp_transport *trans = NULL;
2244 struct sctp_association *asoc = NULL;
2245 struct sctp_sock *sp = sctp_sk(sk);
2246 int error;
2247 int hb_change, pmtud_change, sackdelay_change;
2248
2249 if (optlen != sizeof(struct sctp_paddrparams))
2250 return - EINVAL;
2251
2252 if (copy_from_user(&params, optval, optlen))
2253 return -EFAULT;
2254
2255 /* Validate flags and value parameters. */
2256 hb_change = params.spp_flags & SPP_HB;
2257 pmtud_change = params.spp_flags & SPP_PMTUD;
2258 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2259
2260 if (hb_change == SPP_HB ||
2261 pmtud_change == SPP_PMTUD ||
2262 sackdelay_change == SPP_SACKDELAY ||
2263 params.spp_sackdelay > 500 ||
2264 (params.spp_pathmtu
2265 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2266 return -EINVAL;
2267
2268 /* If an address other than INADDR_ANY is specified, and
2269 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002271 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2272 trans = sctp_addr_id2transport(sk, &params.spp_address,
2273 params.spp_assoc_id);
2274 if (!trans)
2275 return -EINVAL;
2276 }
2277
2278 /* Get association, if assoc_id != 0 and the socket is a one
2279 * to many style socket, and an association was not found, then
2280 * the id was invalid.
2281 */
2282 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2283 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2284 return -EINVAL;
2285
2286 /* Heartbeat demand can only be sent on a transport or
2287 * association, but not a socket.
2288 */
2289 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2290 return -EINVAL;
2291
2292 /* Process parameters. */
2293 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2294 hb_change, pmtud_change,
2295 sackdelay_change);
2296
2297 if (error)
2298 return error;
2299
2300 /* If changes are for association, also apply parameters to each
2301 * transport.
2302 */
2303 if (!trans && asoc) {
2304 struct list_head *pos;
2305
2306 list_for_each(pos, &asoc->peer.transport_addr_list) {
2307 trans = list_entry(pos, struct sctp_transport,
2308 transports);
2309 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2310 hb_change, pmtud_change,
2311 sackdelay_change);
2312 }
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 return 0;
2316}
2317
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002318/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08002319 *
2320 * This options will get or set the delayed ack timer. The time is set
2321 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2322 * endpoints default delayed ack timer value. If the assoc_id field is
2323 * non-zero, then the set or get effects the specified association.
2324 *
2325 * struct sctp_assoc_value {
2326 * sctp_assoc_t assoc_id;
2327 * uint32_t assoc_value;
2328 * };
2329 *
2330 * assoc_id - This parameter, indicates which association the
2331 * user is preforming an action upon. Note that if
2332 * this field's value is zero then the endpoints
2333 * default value is changed (effecting future
2334 * associations only).
2335 *
2336 * assoc_value - This parameter contains the number of milliseconds
2337 * that the user is requesting the delayed ACK timer
2338 * be set to. Note that this value is defined in
2339 * the standard to be between 200 and 500 milliseconds.
2340 *
2341 * Note: a value of zero will leave the value alone,
2342 * but disable SACK delay. A non-zero value will also
2343 * enable SACK delay.
2344 */
2345
2346static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2347 char __user *optval, int optlen)
2348{
2349 struct sctp_assoc_value params;
2350 struct sctp_transport *trans = NULL;
2351 struct sctp_association *asoc = NULL;
2352 struct sctp_sock *sp = sctp_sk(sk);
2353
2354 if (optlen != sizeof(struct sctp_assoc_value))
2355 return - EINVAL;
2356
2357 if (copy_from_user(&params, optval, optlen))
2358 return -EFAULT;
2359
2360 /* Validate value parameter. */
2361 if (params.assoc_value > 500)
2362 return -EINVAL;
2363
2364 /* Get association, if assoc_id != 0 and the socket is a one
2365 * to many style socket, and an association was not found, then
2366 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002367 */
Frank Filz77086102005-12-22 11:37:30 -08002368 asoc = sctp_id2assoc(sk, params.assoc_id);
2369 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2370 return -EINVAL;
2371
2372 if (params.assoc_value) {
2373 if (asoc) {
2374 asoc->sackdelay =
2375 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002376 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002377 (asoc->param_flags & ~SPP_SACKDELAY) |
2378 SPP_SACKDELAY_ENABLE;
2379 } else {
2380 sp->sackdelay = params.assoc_value;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002381 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002382 (sp->param_flags & ~SPP_SACKDELAY) |
2383 SPP_SACKDELAY_ENABLE;
2384 }
2385 } else {
2386 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002387 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002388 (asoc->param_flags & ~SPP_SACKDELAY) |
2389 SPP_SACKDELAY_DISABLE;
2390 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002391 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002392 (sp->param_flags & ~SPP_SACKDELAY) |
2393 SPP_SACKDELAY_DISABLE;
2394 }
2395 }
2396
2397 /* If change is for association, also apply to each transport. */
2398 if (asoc) {
2399 struct list_head *pos;
2400
2401 list_for_each(pos, &asoc->peer.transport_addr_list) {
2402 trans = list_entry(pos, struct sctp_transport,
2403 transports);
2404 if (params.assoc_value) {
2405 trans->sackdelay =
2406 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002407 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002408 (trans->param_flags & ~SPP_SACKDELAY) |
2409 SPP_SACKDELAY_ENABLE;
2410 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002411 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002412 (trans->param_flags & ~SPP_SACKDELAY) |
2413 SPP_SACKDELAY_DISABLE;
2414 }
2415 }
2416 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002417
Frank Filz77086102005-12-22 11:37:30 -08002418 return 0;
2419}
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2422 *
2423 * Applications can specify protocol parameters for the default association
2424 * initialization. The option name argument to setsockopt() and getsockopt()
2425 * is SCTP_INITMSG.
2426 *
2427 * Setting initialization parameters is effective only on an unconnected
2428 * socket (for UDP-style sockets only future associations are effected
2429 * by the change). With TCP-style sockets, this option is inherited by
2430 * sockets derived from a listener socket.
2431 */
2432static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2433{
2434 struct sctp_initmsg sinit;
2435 struct sctp_sock *sp = sctp_sk(sk);
2436
2437 if (optlen != sizeof(struct sctp_initmsg))
2438 return -EINVAL;
2439 if (copy_from_user(&sinit, optval, optlen))
2440 return -EFAULT;
2441
2442 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002443 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002445 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002447 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002449 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 return 0;
2452}
2453
2454/*
2455 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2456 *
2457 * Applications that wish to use the sendto() system call may wish to
2458 * specify a default set of parameters that would normally be supplied
2459 * through the inclusion of ancillary data. This socket option allows
2460 * such an application to set the default sctp_sndrcvinfo structure.
2461 * The application that wishes to use this socket option simply passes
2462 * in to this call the sctp_sndrcvinfo structure defined in Section
2463 * 5.2.2) The input parameters accepted by this call include
2464 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2465 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2466 * to this call if the caller is using the UDP model.
2467 */
2468static int sctp_setsockopt_default_send_param(struct sock *sk,
2469 char __user *optval, int optlen)
2470{
2471 struct sctp_sndrcvinfo info;
2472 struct sctp_association *asoc;
2473 struct sctp_sock *sp = sctp_sk(sk);
2474
2475 if (optlen != sizeof(struct sctp_sndrcvinfo))
2476 return -EINVAL;
2477 if (copy_from_user(&info, optval, optlen))
2478 return -EFAULT;
2479
2480 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2481 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2482 return -EINVAL;
2483
2484 if (asoc) {
2485 asoc->default_stream = info.sinfo_stream;
2486 asoc->default_flags = info.sinfo_flags;
2487 asoc->default_ppid = info.sinfo_ppid;
2488 asoc->default_context = info.sinfo_context;
2489 asoc->default_timetolive = info.sinfo_timetolive;
2490 } else {
2491 sp->default_stream = info.sinfo_stream;
2492 sp->default_flags = info.sinfo_flags;
2493 sp->default_ppid = info.sinfo_ppid;
2494 sp->default_context = info.sinfo_context;
2495 sp->default_timetolive = info.sinfo_timetolive;
2496 }
2497
2498 return 0;
2499}
2500
2501/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2502 *
2503 * Requests that the local SCTP stack use the enclosed peer address as
2504 * the association primary. The enclosed address must be one of the
2505 * association peer's addresses.
2506 */
2507static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2508 int optlen)
2509{
2510 struct sctp_prim prim;
2511 struct sctp_transport *trans;
2512
2513 if (optlen != sizeof(struct sctp_prim))
2514 return -EINVAL;
2515
2516 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2517 return -EFAULT;
2518
2519 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2520 if (!trans)
2521 return -EINVAL;
2522
2523 sctp_assoc_set_primary(trans->asoc, trans);
2524
2525 return 0;
2526}
2527
2528/*
2529 * 7.1.5 SCTP_NODELAY
2530 *
2531 * Turn on/off any Nagle-like algorithm. This means that packets are
2532 * generally sent as soon as possible and no unnecessary delays are
2533 * introduced, at the cost of more packets in the network. Expects an
2534 * integer boolean flag.
2535 */
2536static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2537 int optlen)
2538{
2539 int val;
2540
2541 if (optlen < sizeof(int))
2542 return -EINVAL;
2543 if (get_user(val, (int __user *)optval))
2544 return -EFAULT;
2545
2546 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2547 return 0;
2548}
2549
2550/*
2551 *
2552 * 7.1.1 SCTP_RTOINFO
2553 *
2554 * The protocol parameters used to initialize and bound retransmission
2555 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2556 * and modify these parameters.
2557 * All parameters are time values, in milliseconds. A value of 0, when
2558 * modifying the parameters, indicates that the current value should not
2559 * be changed.
2560 *
2561 */
2562static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2563 struct sctp_rtoinfo rtoinfo;
2564 struct sctp_association *asoc;
2565
2566 if (optlen != sizeof (struct sctp_rtoinfo))
2567 return -EINVAL;
2568
2569 if (copy_from_user(&rtoinfo, optval, optlen))
2570 return -EFAULT;
2571
2572 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2573
2574 /* Set the values to the specific association */
2575 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2576 return -EINVAL;
2577
2578 if (asoc) {
2579 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002580 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 msecs_to_jiffies(rtoinfo.srto_initial);
2582 if (rtoinfo.srto_max != 0)
2583 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2584 if (rtoinfo.srto_min != 0)
2585 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2586 } else {
2587 /* If there is no association or the association-id = 0
2588 * set the values to the endpoint.
2589 */
2590 struct sctp_sock *sp = sctp_sk(sk);
2591
2592 if (rtoinfo.srto_initial != 0)
2593 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2594 if (rtoinfo.srto_max != 0)
2595 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2596 if (rtoinfo.srto_min != 0)
2597 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2598 }
2599
2600 return 0;
2601}
2602
2603/*
2604 *
2605 * 7.1.2 SCTP_ASSOCINFO
2606 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002607 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 * of the association.
2609 * Returns an error if the new association retransmission value is
2610 * greater than the sum of the retransmission value of the peer.
2611 * See [SCTP] for more information.
2612 *
2613 */
2614static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2615{
2616
2617 struct sctp_assocparams assocparams;
2618 struct sctp_association *asoc;
2619
2620 if (optlen != sizeof(struct sctp_assocparams))
2621 return -EINVAL;
2622 if (copy_from_user(&assocparams, optval, optlen))
2623 return -EFAULT;
2624
2625 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2626
2627 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2628 return -EINVAL;
2629
2630 /* Set the values to the specific association */
2631 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002632 if (assocparams.sasoc_asocmaxrxt != 0) {
2633 __u32 path_sum = 0;
2634 int paths = 0;
2635 struct list_head *pos;
2636 struct sctp_transport *peer_addr;
2637
2638 list_for_each(pos, &asoc->peer.transport_addr_list) {
2639 peer_addr = list_entry(pos,
2640 struct sctp_transport,
2641 transports);
2642 path_sum += peer_addr->pathmaxrxt;
2643 paths++;
2644 }
2645
2646 /* Only validate asocmaxrxt if we have more then
2647 * one path/transport. We do this because path
2648 * retransmissions are only counted when we have more
2649 * then one path.
2650 */
2651 if (paths > 1 &&
2652 assocparams.sasoc_asocmaxrxt > path_sum)
2653 return -EINVAL;
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002656 }
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (assocparams.sasoc_cookie_life != 0) {
2659 asoc->cookie_life.tv_sec =
2660 assocparams.sasoc_cookie_life / 1000;
2661 asoc->cookie_life.tv_usec =
2662 (assocparams.sasoc_cookie_life % 1000)
2663 * 1000;
2664 }
2665 } else {
2666 /* Set the values to the endpoint */
2667 struct sctp_sock *sp = sctp_sk(sk);
2668
2669 if (assocparams.sasoc_asocmaxrxt != 0)
2670 sp->assocparams.sasoc_asocmaxrxt =
2671 assocparams.sasoc_asocmaxrxt;
2672 if (assocparams.sasoc_cookie_life != 0)
2673 sp->assocparams.sasoc_cookie_life =
2674 assocparams.sasoc_cookie_life;
2675 }
2676 return 0;
2677}
2678
2679/*
2680 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2681 *
2682 * This socket option is a boolean flag which turns on or off mapped V4
2683 * addresses. If this option is turned on and the socket is type
2684 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2685 * If this option is turned off, then no mapping will be done of V4
2686 * addresses and a user will receive both PF_INET6 and PF_INET type
2687 * addresses on the socket.
2688 */
2689static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2690{
2691 int val;
2692 struct sctp_sock *sp = sctp_sk(sk);
2693
2694 if (optlen < sizeof(int))
2695 return -EINVAL;
2696 if (get_user(val, (int __user *)optval))
2697 return -EFAULT;
2698 if (val)
2699 sp->v4mapped = 1;
2700 else
2701 sp->v4mapped = 0;
2702
2703 return 0;
2704}
2705
2706/*
2707 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2708 *
2709 * This socket option specifies the maximum size to put in any outgoing
2710 * SCTP chunk. If a message is larger than this size it will be
2711 * fragmented by SCTP into the specified size. Note that the underlying
2712 * SCTP implementation may fragment into smaller sized chunks when the
2713 * PMTU of the underlying association is smaller than the value set by
2714 * the user.
2715 */
2716static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2717{
2718 struct sctp_association *asoc;
2719 struct list_head *pos;
2720 struct sctp_sock *sp = sctp_sk(sk);
2721 int val;
2722
2723 if (optlen < sizeof(int))
2724 return -EINVAL;
2725 if (get_user(val, (int __user *)optval))
2726 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002727 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return -EINVAL;
2729 sp->user_frag = val;
2730
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002731 /* Update the frag_point of the existing associations. */
2732 list_for_each(pos, &(sp->ep->asocs)) {
2733 asoc = list_entry(pos, struct sctp_association, asocs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002734 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736
2737 return 0;
2738}
2739
2740
2741/*
2742 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2743 *
2744 * Requests that the peer mark the enclosed address as the association
2745 * primary. The enclosed address must be one of the association's
2746 * locally bound addresses. The following structure is used to make a
2747 * set primary request:
2748 */
2749static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2750 int optlen)
2751{
2752 struct sctp_sock *sp;
2753 struct sctp_endpoint *ep;
2754 struct sctp_association *asoc = NULL;
2755 struct sctp_setpeerprim prim;
2756 struct sctp_chunk *chunk;
2757 int err;
2758
2759 sp = sctp_sk(sk);
2760 ep = sp->ep;
2761
2762 if (!sctp_addip_enable)
2763 return -EPERM;
2764
2765 if (optlen != sizeof(struct sctp_setpeerprim))
2766 return -EINVAL;
2767
2768 if (copy_from_user(&prim, optval, optlen))
2769 return -EFAULT;
2770
2771 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002772 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -EINVAL;
2774
2775 if (!asoc->peer.asconf_capable)
2776 return -EPERM;
2777
2778 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2779 return -EPERM;
2780
2781 if (!sctp_state(asoc, ESTABLISHED))
2782 return -ENOTCONN;
2783
2784 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2785 return -EADDRNOTAVAIL;
2786
2787 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2788 chunk = sctp_make_asconf_set_prim(asoc,
2789 (union sctp_addr *)&prim.sspp_addr);
2790 if (!chunk)
2791 return -ENOMEM;
2792
2793 err = sctp_send_asconf(asoc, chunk);
2794
2795 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2796
2797 return err;
2798}
2799
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002800static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 int optlen)
2802{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002803 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002805 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002807 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return -EFAULT;
2809
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002810 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 return 0;
2813}
2814
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08002815/*
2816 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2817 *
2818 * The context field in the sctp_sndrcvinfo structure is normally only
2819 * used when a failed message is retrieved holding the value that was
2820 * sent down on the actual send call. This option allows the setting of
2821 * a default context on an association basis that will be received on
2822 * reading messages from the peer. This is especially helpful in the
2823 * one-2-many model for an application to keep some reference to an
2824 * internal state machine that is processing messages on the
2825 * association. Note that the setting of this value only effects
2826 * received messages from the peer and does not effect the value that is
2827 * saved with outbound messages.
2828 */
2829static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2830 int optlen)
2831{
2832 struct sctp_assoc_value params;
2833 struct sctp_sock *sp;
2834 struct sctp_association *asoc;
2835
2836 if (optlen != sizeof(struct sctp_assoc_value))
2837 return -EINVAL;
2838 if (copy_from_user(&params, optval, optlen))
2839 return -EFAULT;
2840
2841 sp = sctp_sk(sk);
2842
2843 if (params.assoc_id != 0) {
2844 asoc = sctp_id2assoc(sk, params.assoc_id);
2845 if (!asoc)
2846 return -EINVAL;
2847 asoc->default_rcv_context = params.assoc_value;
2848 } else {
2849 sp->default_rcv_context = params.assoc_value;
2850 }
2851
2852 return 0;
2853}
2854
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002855/*
2856 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2857 *
2858 * This options will at a minimum specify if the implementation is doing
2859 * fragmented interleave. Fragmented interleave, for a one to many
2860 * socket, is when subsequent calls to receive a message may return
2861 * parts of messages from different associations. Some implementations
2862 * may allow you to turn this value on or off. If so, when turned off,
2863 * no fragment interleave will occur (which will cause a head of line
2864 * blocking amongst multiple associations sharing the same one to many
2865 * socket). When this option is turned on, then each receive call may
2866 * come from a different association (thus the user must receive data
2867 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2868 * association each receive belongs to.
2869 *
2870 * This option takes a boolean value. A non-zero value indicates that
2871 * fragmented interleave is on. A value of zero indicates that
2872 * fragmented interleave is off.
2873 *
2874 * Note that it is important that an implementation that allows this
2875 * option to be turned on, have it off by default. Otherwise an unaware
2876 * application using the one to many model may become confused and act
2877 * incorrectly.
2878 */
2879static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2880 char __user *optval,
2881 int optlen)
2882{
2883 int val;
2884
2885 if (optlen != sizeof(int))
2886 return -EINVAL;
2887 if (get_user(val, (int __user *)optval))
2888 return -EFAULT;
2889
2890 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2891
2892 return 0;
2893}
2894
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07002895/*
2896 * 7.1.25. Set or Get the sctp partial delivery point
2897 * (SCTP_PARTIAL_DELIVERY_POINT)
2898 * This option will set or get the SCTP partial delivery point. This
2899 * point is the size of a message where the partial delivery API will be
2900 * invoked to help free up rwnd space for the peer. Setting this to a
2901 * lower value will cause partial delivery's to happen more often. The
2902 * calls argument is an integer that sets or gets the partial delivery
2903 * point.
2904 */
2905static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2906 char __user *optval,
2907 int optlen)
2908{
2909 u32 val;
2910
2911 if (optlen != sizeof(u32))
2912 return -EINVAL;
2913 if (get_user(val, (int __user *)optval))
2914 return -EFAULT;
2915
2916 sctp_sk(sk)->pd_point = val;
2917
2918 return 0; /* is this the right error code? */
2919}
2920
Vlad Yasevich70331572007-03-23 11:34:36 -07002921/*
2922 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2923 *
2924 * This option will allow a user to change the maximum burst of packets
2925 * that can be emitted by this association. Note that the default value
2926 * is 4, and some implementations may restrict this setting so that it
2927 * can only be lowered.
2928 *
2929 * NOTE: This text doesn't seem right. Do this on a socket basis with
2930 * future associations inheriting the socket value.
2931 */
2932static int sctp_setsockopt_maxburst(struct sock *sk,
2933 char __user *optval,
2934 int optlen)
2935{
2936 int val;
2937
2938 if (optlen != sizeof(int))
2939 return -EINVAL;
2940 if (get_user(val, (int __user *)optval))
2941 return -EFAULT;
2942
2943 if (val < 0)
2944 return -EINVAL;
2945
2946 sctp_sk(sk)->max_burst = val;
2947
2948 return 0;
2949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951/* API 6.2 setsockopt(), getsockopt()
2952 *
2953 * Applications use setsockopt() and getsockopt() to set or retrieve
2954 * socket options. Socket options are used to change the default
2955 * behavior of sockets calls. They are described in Section 7.
2956 *
2957 * The syntax is:
2958 *
2959 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2960 * int __user *optlen);
2961 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2962 * int optlen);
2963 *
2964 * sd - the socket descript.
2965 * level - set to IPPROTO_SCTP for all SCTP options.
2966 * optname - the option name.
2967 * optval - the buffer to store the value of the option.
2968 * optlen - the size of the buffer.
2969 */
2970SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2971 char __user *optval, int optlen)
2972{
2973 int retval = 0;
2974
2975 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2976 sk, optname);
2977
2978 /* I can hardly begin to describe how wrong this is. This is
2979 * so broken as to be worse than useless. The API draft
2980 * REALLY is NOT helpful here... I am not convinced that the
2981 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2982 * are at all well-founded.
2983 */
2984 if (level != SOL_SCTP) {
2985 struct sctp_af *af = sctp_sk(sk)->pf->af;
2986 retval = af->setsockopt(sk, level, optname, optval, optlen);
2987 goto out_nounlock;
2988 }
2989
2990 sctp_lock_sock(sk);
2991
2992 switch (optname) {
2993 case SCTP_SOCKOPT_BINDX_ADD:
2994 /* 'optlen' is the size of the addresses buffer. */
2995 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2996 optlen, SCTP_BINDX_ADD_ADDR);
2997 break;
2998
2999 case SCTP_SOCKOPT_BINDX_REM:
3000 /* 'optlen' is the size of the addresses buffer. */
3001 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3002 optlen, SCTP_BINDX_REM_ADDR);
3003 break;
3004
Frank Filz3f7a87d2005-06-20 13:14:57 -07003005 case SCTP_SOCKOPT_CONNECTX:
3006 /* 'optlen' is the size of the addresses buffer. */
3007 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3008 optlen);
3009 break;
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 case SCTP_DISABLE_FRAGMENTS:
3012 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3013 break;
3014
3015 case SCTP_EVENTS:
3016 retval = sctp_setsockopt_events(sk, optval, optlen);
3017 break;
3018
3019 case SCTP_AUTOCLOSE:
3020 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3021 break;
3022
3023 case SCTP_PEER_ADDR_PARAMS:
3024 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3025 break;
3026
Frank Filz77086102005-12-22 11:37:30 -08003027 case SCTP_DELAYED_ACK_TIME:
3028 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3029 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003030 case SCTP_PARTIAL_DELIVERY_POINT:
3031 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3032 break;
Frank Filz77086102005-12-22 11:37:30 -08003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 case SCTP_INITMSG:
3035 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3036 break;
3037 case SCTP_DEFAULT_SEND_PARAM:
3038 retval = sctp_setsockopt_default_send_param(sk, optval,
3039 optlen);
3040 break;
3041 case SCTP_PRIMARY_ADDR:
3042 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3043 break;
3044 case SCTP_SET_PEER_PRIMARY_ADDR:
3045 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3046 break;
3047 case SCTP_NODELAY:
3048 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3049 break;
3050 case SCTP_RTOINFO:
3051 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3052 break;
3053 case SCTP_ASSOCINFO:
3054 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3055 break;
3056 case SCTP_I_WANT_MAPPED_V4_ADDR:
3057 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3058 break;
3059 case SCTP_MAXSEG:
3060 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3061 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003062 case SCTP_ADAPTATION_LAYER:
3063 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003065 case SCTP_CONTEXT:
3066 retval = sctp_setsockopt_context(sk, optval, optlen);
3067 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003068 case SCTP_FRAGMENT_INTERLEAVE:
3069 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3070 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003071 case SCTP_MAX_BURST:
3072 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 default:
3075 retval = -ENOPROTOOPT;
3076 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 sctp_release_sock(sk);
3080
3081out_nounlock:
3082 return retval;
3083}
3084
3085/* API 3.1.6 connect() - UDP Style Syntax
3086 *
3087 * An application may use the connect() call in the UDP model to initiate an
3088 * association without sending data.
3089 *
3090 * The syntax is:
3091 *
3092 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3093 *
3094 * sd: the socket descriptor to have a new association added to.
3095 *
3096 * nam: the address structure (either struct sockaddr_in or struct
3097 * sockaddr_in6 defined in RFC2553 [7]).
3098 *
3099 * len: the size of the address.
3100 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003101SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 int addr_len)
3103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003105 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 sctp_lock_sock(sk);
3108
Frank Filz3f7a87d2005-06-20 13:14:57 -07003109 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3110 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Frank Filz3f7a87d2005-06-20 13:14:57 -07003112 /* Validate addr_len before calling common connect/connectx routine. */
3113 af = sctp_get_af_specific(addr->sa_family);
3114 if (!af || addr_len < af->sockaddr_len) {
3115 err = -EINVAL;
3116 } else {
3117 /* Pass correct addr len to common routine (so it knows there
3118 * is only one address being passed.
3119 */
3120 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return err;
3125}
3126
3127/* FIXME: Write comments. */
3128SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3129{
3130 return -EOPNOTSUPP; /* STUB */
3131}
3132
3133/* 4.1.4 accept() - TCP Style Syntax
3134 *
3135 * Applications use accept() call to remove an established SCTP
3136 * association from the accept queue of the endpoint. A new socket
3137 * descriptor will be returned from accept() to represent the newly
3138 * formed association.
3139 */
3140SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3141{
3142 struct sctp_sock *sp;
3143 struct sctp_endpoint *ep;
3144 struct sock *newsk = NULL;
3145 struct sctp_association *asoc;
3146 long timeo;
3147 int error = 0;
3148
3149 sctp_lock_sock(sk);
3150
3151 sp = sctp_sk(sk);
3152 ep = sp->ep;
3153
3154 if (!sctp_style(sk, TCP)) {
3155 error = -EOPNOTSUPP;
3156 goto out;
3157 }
3158
3159 if (!sctp_sstate(sk, LISTENING)) {
3160 error = -EINVAL;
3161 goto out;
3162 }
3163
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003164 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 error = sctp_wait_for_accept(sk, timeo);
3167 if (error)
3168 goto out;
3169
3170 /* We treat the list of associations on the endpoint as the accept
3171 * queue and pick the first association on the list.
3172 */
3173 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3174
3175 newsk = sp->pf->create_accept_sk(sk, asoc);
3176 if (!newsk) {
3177 error = -ENOMEM;
3178 goto out;
3179 }
3180
3181 /* Populate the fields of the newsk from the oldsk and migrate the
3182 * asoc to the newsk.
3183 */
3184 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3185
3186out:
3187 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003188 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return newsk;
3190}
3191
3192/* The SCTP ioctl handler. */
3193SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3194{
3195 return -ENOIOCTLCMD;
3196}
3197
3198/* This is the function which gets called during socket creation to
3199 * initialized the SCTP-specific portion of the sock.
3200 * The sock structure should already be zero-filled memory.
3201 */
3202SCTP_STATIC int sctp_init_sock(struct sock *sk)
3203{
3204 struct sctp_endpoint *ep;
3205 struct sctp_sock *sp;
3206
3207 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3208
3209 sp = sctp_sk(sk);
3210
3211 /* Initialize the SCTP per socket area. */
3212 switch (sk->sk_type) {
3213 case SOCK_SEQPACKET:
3214 sp->type = SCTP_SOCKET_UDP;
3215 break;
3216 case SOCK_STREAM:
3217 sp->type = SCTP_SOCKET_TCP;
3218 break;
3219 default:
3220 return -ESOCKTNOSUPPORT;
3221 }
3222
3223 /* Initialize default send parameters. These parameters can be
3224 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3225 */
3226 sp->default_stream = 0;
3227 sp->default_ppid = 0;
3228 sp->default_flags = 0;
3229 sp->default_context = 0;
3230 sp->default_timetolive = 0;
3231
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003232 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003233 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 /* Initialize default setup parameters. These parameters
3236 * can be modified with the SCTP_INITMSG socket option or
3237 * overridden by the SCTP_INIT CMSG.
3238 */
3239 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3240 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3241 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003242 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244 /* Initialize default RTO related parameters. These parameters can
3245 * be modified for with the SCTP_RTOINFO socket option.
3246 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003247 sp->rtoinfo.srto_initial = sctp_rto_initial;
3248 sp->rtoinfo.srto_max = sctp_rto_max;
3249 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 /* Initialize default association related parameters. These parameters
3252 * can be modified with the SCTP_ASSOCINFO socket option.
3253 */
3254 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3255 sp->assocparams.sasoc_number_peer_destinations = 0;
3256 sp->assocparams.sasoc_peer_rwnd = 0;
3257 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003258 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003261 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 */
3263 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3264
3265 /* Default Peer Address Parameters. These defaults can
3266 * be modified via SCTP_PEER_ADDR_PARAMS
3267 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003268 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003269 sp->pathmaxrxt = sctp_max_retrans_path;
3270 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003271 sp->sackdelay = sctp_sack_timeout;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003272 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003273 SPP_PMTUD_ENABLE |
3274 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 /* If enabled no SCTP message fragmentation will be performed.
3277 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3278 */
3279 sp->disable_fragments = 0;
3280
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003281 /* Enable Nagle algorithm by default. */
3282 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 /* Enable by default. */
3285 sp->v4mapped = 1;
3286
3287 /* Auto-close idle associations after the configured
3288 * number of seconds. A value of 0 disables this
3289 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3290 * for UDP-style sockets only.
3291 */
3292 sp->autoclose = 0;
3293
3294 /* User specified fragmentation limit. */
3295 sp->user_frag = 0;
3296
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003297 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 sp->pf = sctp_get_pf_specific(sk->sk_family);
3300
3301 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003302 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003304 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
3306 /* Create a per socket endpoint structure. Even if we
3307 * change the data structure relationships, this may still
3308 * be useful for storing pre-connect address information.
3309 */
3310 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3311 if (!ep)
3312 return -ENOMEM;
3313
3314 sp->ep = ep;
3315 sp->hmac = NULL;
3316
3317 SCTP_DBG_OBJCNT_INC(sock);
3318 return 0;
3319}
3320
3321/* Cleanup any SCTP per socket resources. */
3322SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3323{
3324 struct sctp_endpoint *ep;
3325
3326 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3327
3328 /* Release our hold on the endpoint. */
3329 ep = sctp_sk(sk)->ep;
3330 sctp_endpoint_free(ep);
3331
3332 return 0;
3333}
3334
3335/* API 4.1.7 shutdown() - TCP Style Syntax
3336 * int shutdown(int socket, int how);
3337 *
3338 * sd - the socket descriptor of the association to be closed.
3339 * how - Specifies the type of shutdown. The values are
3340 * as follows:
3341 * SHUT_RD
3342 * Disables further receive operations. No SCTP
3343 * protocol action is taken.
3344 * SHUT_WR
3345 * Disables further send operations, and initiates
3346 * the SCTP shutdown sequence.
3347 * SHUT_RDWR
3348 * Disables further send and receive operations
3349 * and initiates the SCTP shutdown sequence.
3350 */
3351SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3352{
3353 struct sctp_endpoint *ep;
3354 struct sctp_association *asoc;
3355
3356 if (!sctp_style(sk, TCP))
3357 return;
3358
3359 if (how & SEND_SHUTDOWN) {
3360 ep = sctp_sk(sk)->ep;
3361 if (!list_empty(&ep->asocs)) {
3362 asoc = list_entry(ep->asocs.next,
3363 struct sctp_association, asocs);
3364 sctp_primitive_SHUTDOWN(asoc, NULL);
3365 }
3366 }
3367}
3368
3369/* 7.2.1 Association Status (SCTP_STATUS)
3370
3371 * Applications can retrieve current status information about an
3372 * association, including association state, peer receiver window size,
3373 * number of unacked data chunks, and number of data chunks pending
3374 * receipt. This information is read-only.
3375 */
3376static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3377 char __user *optval,
3378 int __user *optlen)
3379{
3380 struct sctp_status status;
3381 struct sctp_association *asoc = NULL;
3382 struct sctp_transport *transport;
3383 sctp_assoc_t associd;
3384 int retval = 0;
3385
Neil Horman408f22e2007-06-16 14:03:45 -04003386 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 retval = -EINVAL;
3388 goto out;
3389 }
3390
Neil Horman408f22e2007-06-16 14:03:45 -04003391 len = sizeof(status);
3392 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 retval = -EFAULT;
3394 goto out;
3395 }
3396
3397 associd = status.sstat_assoc_id;
3398 asoc = sctp_id2assoc(sk, associd);
3399 if (!asoc) {
3400 retval = -EINVAL;
3401 goto out;
3402 }
3403
3404 transport = asoc->peer.primary_path;
3405
3406 status.sstat_assoc_id = sctp_assoc2id(asoc);
3407 status.sstat_state = asoc->state;
3408 status.sstat_rwnd = asoc->peer.rwnd;
3409 status.sstat_unackdata = asoc->unack_data;
3410
3411 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3412 status.sstat_instrms = asoc->c.sinit_max_instreams;
3413 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3414 status.sstat_fragmentation_point = asoc->frag_point;
3415 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08003416 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3417 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 /* Map ipv4 address into v4-mapped-on-v6 address. */
3419 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3420 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003421 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3423 status.sstat_primary.spinfo_srtt = transport->srtt;
3424 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003425 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Frank Filz3f7a87d2005-06-20 13:14:57 -07003427 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3428 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 if (put_user(len, optlen)) {
3431 retval = -EFAULT;
3432 goto out;
3433 }
3434
3435 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3436 len, status.sstat_state, status.sstat_rwnd,
3437 status.sstat_assoc_id);
3438
3439 if (copy_to_user(optval, &status, len)) {
3440 retval = -EFAULT;
3441 goto out;
3442 }
3443
3444out:
3445 return (retval);
3446}
3447
3448
3449/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3450 *
3451 * Applications can retrieve information about a specific peer address
3452 * of an association, including its reachability state, congestion
3453 * window, and retransmission timer values. This information is
3454 * read-only.
3455 */
3456static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3457 char __user *optval,
3458 int __user *optlen)
3459{
3460 struct sctp_paddrinfo pinfo;
3461 struct sctp_transport *transport;
3462 int retval = 0;
3463
Neil Horman408f22e2007-06-16 14:03:45 -04003464 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 retval = -EINVAL;
3466 goto out;
3467 }
3468
Neil Horman408f22e2007-06-16 14:03:45 -04003469 len = sizeof(pinfo);
3470 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 retval = -EFAULT;
3472 goto out;
3473 }
3474
3475 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3476 pinfo.spinfo_assoc_id);
3477 if (!transport)
3478 return -EINVAL;
3479
3480 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003481 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 pinfo.spinfo_cwnd = transport->cwnd;
3483 pinfo.spinfo_srtt = transport->srtt;
3484 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003485 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Frank Filz3f7a87d2005-06-20 13:14:57 -07003487 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3488 pinfo.spinfo_state = SCTP_ACTIVE;
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (put_user(len, optlen)) {
3491 retval = -EFAULT;
3492 goto out;
3493 }
3494
3495 if (copy_to_user(optval, &pinfo, len)) {
3496 retval = -EFAULT;
3497 goto out;
3498 }
3499
3500out:
3501 return (retval);
3502}
3503
3504/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3505 *
3506 * This option is a on/off flag. If enabled no SCTP message
3507 * fragmentation will be performed. Instead if a message being sent
3508 * exceeds the current PMTU size, the message will NOT be sent and
3509 * instead a error will be indicated to the user.
3510 */
3511static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3512 char __user *optval, int __user *optlen)
3513{
3514 int val;
3515
3516 if (len < sizeof(int))
3517 return -EINVAL;
3518
3519 len = sizeof(int);
3520 val = (sctp_sk(sk)->disable_fragments == 1);
3521 if (put_user(len, optlen))
3522 return -EFAULT;
3523 if (copy_to_user(optval, &val, len))
3524 return -EFAULT;
3525 return 0;
3526}
3527
3528/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3529 *
3530 * This socket option is used to specify various notifications and
3531 * ancillary data the user wishes to receive.
3532 */
3533static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3534 int __user *optlen)
3535{
Neil Horman408f22e2007-06-16 14:03:45 -04003536 if (len < sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003538 len = sizeof(struct sctp_event_subscribe);
3539 if (put_user(len, optlen))
3540 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3542 return -EFAULT;
3543 return 0;
3544}
3545
3546/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3547 *
3548 * This socket option is applicable to the UDP-style socket only. When
3549 * set it will cause associations that are idle for more than the
3550 * specified number of seconds to automatically close. An association
3551 * being idle is defined an association that has NOT sent or received
3552 * user data. The special value of '0' indicates that no automatic
3553 * close of any associations should be performed. The option expects an
3554 * integer defining the number of seconds of idle time before an
3555 * association is closed.
3556 */
3557static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3558{
3559 /* Applicable to UDP-style socket only */
3560 if (sctp_style(sk, TCP))
3561 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04003562 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003564 len = sizeof(int);
3565 if (put_user(len, optlen))
3566 return -EFAULT;
3567 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 return -EFAULT;
3569 return 0;
3570}
3571
3572/* Helper routine to branch off an association to a new socket. */
3573SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3574 struct socket **sockp)
3575{
3576 struct sock *sk = asoc->base.sk;
3577 struct socket *sock;
Vlad Yasevich4f444302006-10-30 18:54:32 -08003578 struct inet_sock *inetsk;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003579 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 int err = 0;
3581
3582 /* An association cannot be branched off from an already peeled-off
3583 * socket, nor is this supported for tcp style sockets.
3584 */
3585 if (!sctp_style(sk, UDP))
3586 return -EINVAL;
3587
3588 /* Create a new socket. */
3589 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3590 if (err < 0)
3591 return err;
3592
3593 /* Populate the fields of the newsk from the oldsk and migrate the
3594 * asoc to the newsk.
3595 */
3596 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003597
3598 /* Make peeled-off sockets more like 1-1 accepted sockets.
3599 * Set the daddr and initialize id to something more random
3600 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003601 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3602 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003603 inetsk = inet_sk(sock->sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003604 inetsk->id = asoc->next_tsn ^ jiffies;
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 *sockp = sock;
3607
3608 return err;
3609}
3610
3611static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3612{
3613 sctp_peeloff_arg_t peeloff;
3614 struct socket *newsock;
3615 int retval = 0;
3616 struct sctp_association *asoc;
3617
Neil Horman408f22e2007-06-16 14:03:45 -04003618 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003620 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (copy_from_user(&peeloff, optval, len))
3622 return -EFAULT;
3623
3624 asoc = sctp_id2assoc(sk, peeloff.associd);
3625 if (!asoc) {
3626 retval = -EINVAL;
3627 goto out;
3628 }
3629
3630 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3631
3632 retval = sctp_do_peeloff(asoc, &newsock);
3633 if (retval < 0)
3634 goto out;
3635
3636 /* Map the socket to an unused fd that can be returned to the user. */
3637 retval = sock_map_fd(newsock);
3638 if (retval < 0) {
3639 sock_release(newsock);
3640 goto out;
3641 }
3642
3643 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3644 __FUNCTION__, sk, asoc, newsock->sk, retval);
3645
3646 /* Return the fd mapped to the new socket. */
3647 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04003648 if (put_user(len, optlen))
3649 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (copy_to_user(optval, &peeloff, len))
3651 retval = -EFAULT;
3652
3653out:
3654 return retval;
3655}
3656
3657/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3658 *
3659 * Applications can enable or disable heartbeats for any peer address of
3660 * an association, modify an address's heartbeat interval, force a
3661 * heartbeat to be sent immediately, and adjust the address's maximum
3662 * number of retransmissions sent before an address is considered
3663 * unreachable. The following structure is used to access and modify an
3664 * address's parameters:
3665 *
3666 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003667 * sctp_assoc_t spp_assoc_id;
3668 * struct sockaddr_storage spp_address;
3669 * uint32_t spp_hbinterval;
3670 * uint16_t spp_pathmaxrxt;
3671 * uint32_t spp_pathmtu;
3672 * uint32_t spp_sackdelay;
3673 * uint32_t spp_flags;
3674 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003676 * spp_assoc_id - (one-to-many style socket) This is filled in the
3677 * application, and identifies the association for
3678 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 * spp_address - This specifies which address is of interest.
3680 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003681 * in milliseconds. If a value of zero
3682 * is present in this field then no changes are to
3683 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * spp_pathmaxrxt - This contains the maximum number of
3685 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003686 * considered unreachable. If a value of zero
3687 * is present in this field then no changes are to
3688 * be made to this parameter.
3689 * spp_pathmtu - When Path MTU discovery is disabled the value
3690 * specified here will be the "fixed" path mtu.
3691 * Note that if the spp_address field is empty
3692 * then all associations on this address will
3693 * have this fixed path mtu set upon them.
3694 *
3695 * spp_sackdelay - When delayed sack is enabled, this value specifies
3696 * the number of milliseconds that sacks will be delayed
3697 * for. This value will apply to all addresses of an
3698 * association if the spp_address field is empty. Note
3699 * also, that if delayed sack is enabled and this
3700 * value is set to 0, no change is made to the last
3701 * recorded delayed sack timer value.
3702 *
3703 * spp_flags - These flags are used to control various features
3704 * on an association. The flag field may contain
3705 * zero or more of the following options.
3706 *
3707 * SPP_HB_ENABLE - Enable heartbeats on the
3708 * specified address. Note that if the address
3709 * field is empty all addresses for the association
3710 * have heartbeats enabled upon them.
3711 *
3712 * SPP_HB_DISABLE - Disable heartbeats on the
3713 * speicifed address. Note that if the address
3714 * field is empty all addresses for the association
3715 * will have their heartbeats disabled. Note also
3716 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3717 * mutually exclusive, only one of these two should
3718 * be specified. Enabling both fields will have
3719 * undetermined results.
3720 *
3721 * SPP_HB_DEMAND - Request a user initiated heartbeat
3722 * to be made immediately.
3723 *
3724 * SPP_PMTUD_ENABLE - This field will enable PMTU
3725 * discovery upon the specified address. Note that
3726 * if the address feild is empty then all addresses
3727 * on the association are effected.
3728 *
3729 * SPP_PMTUD_DISABLE - This field will disable PMTU
3730 * discovery upon the specified address. Note that
3731 * if the address feild is empty then all addresses
3732 * on the association are effected. Not also that
3733 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3734 * exclusive. Enabling both will have undetermined
3735 * results.
3736 *
3737 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3738 * on delayed sack. The time specified in spp_sackdelay
3739 * is used to specify the sack delay for this address. Note
3740 * that if spp_address is empty then all addresses will
3741 * enable delayed sack and take on the sack delay
3742 * value specified in spp_sackdelay.
3743 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3744 * off delayed sack. If the spp_address field is blank then
3745 * delayed sack is disabled for the entire association. Note
3746 * also that this field is mutually exclusive to
3747 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3748 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 */
3750static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003751 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003753 struct sctp_paddrparams params;
3754 struct sctp_transport *trans = NULL;
3755 struct sctp_association *asoc = NULL;
3756 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Neil Horman408f22e2007-06-16 14:03:45 -04003758 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003760 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (copy_from_user(&params, optval, len))
3762 return -EFAULT;
3763
Frank Filz52ccb8e2005-12-22 11:36:46 -08003764 /* If an address other than INADDR_ANY is specified, and
3765 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003767 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3768 trans = sctp_addr_id2transport(sk, &params.spp_address,
3769 params.spp_assoc_id);
3770 if (!trans) {
3771 SCTP_DEBUG_PRINTK("Failed no transport\n");
3772 return -EINVAL;
3773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
3775
Frank Filz52ccb8e2005-12-22 11:36:46 -08003776 /* Get association, if assoc_id != 0 and the socket is a one
3777 * to many style socket, and an association was not found, then
3778 * the id was invalid.
3779 */
3780 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3781 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3782 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Frank Filz52ccb8e2005-12-22 11:36:46 -08003786 if (trans) {
3787 /* Fetch transport values. */
3788 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3789 params.spp_pathmtu = trans->pathmtu;
3790 params.spp_pathmaxrxt = trans->pathmaxrxt;
3791 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Frank Filz52ccb8e2005-12-22 11:36:46 -08003793 /*draft-11 doesn't say what to return in spp_flags*/
3794 params.spp_flags = trans->param_flags;
3795 } else if (asoc) {
3796 /* Fetch association values. */
3797 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3798 params.spp_pathmtu = asoc->pathmtu;
3799 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3800 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Frank Filz52ccb8e2005-12-22 11:36:46 -08003802 /*draft-11 doesn't say what to return in spp_flags*/
3803 params.spp_flags = asoc->param_flags;
3804 } else {
3805 /* Fetch socket values. */
3806 params.spp_hbinterval = sp->hbinterval;
3807 params.spp_pathmtu = sp->pathmtu;
3808 params.spp_sackdelay = sp->sackdelay;
3809 params.spp_pathmaxrxt = sp->pathmaxrxt;
3810
3811 /*draft-11 doesn't say what to return in spp_flags*/
3812 params.spp_flags = sp->param_flags;
3813 }
3814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if (copy_to_user(optval, &params, len))
3816 return -EFAULT;
3817
3818 if (put_user(len, optlen))
3819 return -EFAULT;
3820
3821 return 0;
3822}
3823
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003824/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08003825 *
3826 * This options will get or set the delayed ack timer. The time is set
3827 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3828 * endpoints default delayed ack timer value. If the assoc_id field is
3829 * non-zero, then the set or get effects the specified association.
3830 *
3831 * struct sctp_assoc_value {
3832 * sctp_assoc_t assoc_id;
3833 * uint32_t assoc_value;
3834 * };
3835 *
3836 * assoc_id - This parameter, indicates which association the
3837 * user is preforming an action upon. Note that if
3838 * this field's value is zero then the endpoints
3839 * default value is changed (effecting future
3840 * associations only).
3841 *
3842 * assoc_value - This parameter contains the number of milliseconds
3843 * that the user is requesting the delayed ACK timer
3844 * be set to. Note that this value is defined in
3845 * the standard to be between 200 and 500 milliseconds.
3846 *
3847 * Note: a value of zero will leave the value alone,
3848 * but disable SACK delay. A non-zero value will also
3849 * enable SACK delay.
3850 */
3851static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3852 char __user *optval,
3853 int __user *optlen)
3854{
3855 struct sctp_assoc_value params;
3856 struct sctp_association *asoc = NULL;
3857 struct sctp_sock *sp = sctp_sk(sk);
3858
Neil Horman408f22e2007-06-16 14:03:45 -04003859 if (len < sizeof(struct sctp_assoc_value))
Frank Filz77086102005-12-22 11:37:30 -08003860 return - EINVAL;
3861
Neil Horman408f22e2007-06-16 14:03:45 -04003862 len = sizeof(struct sctp_assoc_value);
3863
Frank Filz77086102005-12-22 11:37:30 -08003864 if (copy_from_user(&params, optval, len))
3865 return -EFAULT;
3866
3867 /* Get association, if assoc_id != 0 and the socket is a one
3868 * to many style socket, and an association was not found, then
3869 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003870 */
Frank Filz77086102005-12-22 11:37:30 -08003871 asoc = sctp_id2assoc(sk, params.assoc_id);
3872 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3873 return -EINVAL;
3874
3875 if (asoc) {
3876 /* Fetch association values. */
3877 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3878 params.assoc_value = jiffies_to_msecs(
3879 asoc->sackdelay);
3880 else
3881 params.assoc_value = 0;
3882 } else {
3883 /* Fetch socket values. */
3884 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3885 params.assoc_value = sp->sackdelay;
3886 else
3887 params.assoc_value = 0;
3888 }
3889
3890 if (copy_to_user(optval, &params, len))
3891 return -EFAULT;
3892
3893 if (put_user(len, optlen))
3894 return -EFAULT;
3895
3896 return 0;
3897}
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3900 *
3901 * Applications can specify protocol parameters for the default association
3902 * initialization. The option name argument to setsockopt() and getsockopt()
3903 * is SCTP_INITMSG.
3904 *
3905 * Setting initialization parameters is effective only on an unconnected
3906 * socket (for UDP-style sockets only future associations are effected
3907 * by the change). With TCP-style sockets, this option is inherited by
3908 * sockets derived from a listener socket.
3909 */
3910static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3911{
Neil Horman408f22e2007-06-16 14:03:45 -04003912 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003914 len = sizeof(struct sctp_initmsg);
3915 if (put_user(len, optlen))
3916 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3918 return -EFAULT;
3919 return 0;
3920}
3921
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003922static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3923 char __user *optval,
3924 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
3926 sctp_assoc_t id;
3927 struct sctp_association *asoc;
3928 struct list_head *pos;
3929 int cnt = 0;
3930
Neil Horman408f22e2007-06-16 14:03:45 -04003931 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return -EINVAL;
3933
3934 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3935 return -EFAULT;
3936
3937 /* For UDP-style sockets, id specifies the association to query. */
3938 asoc = sctp_id2assoc(sk, id);
3939 if (!asoc)
3940 return -EINVAL;
3941
3942 list_for_each(pos, &asoc->peer.transport_addr_list) {
3943 cnt ++;
3944 }
3945
3946 return cnt;
3947}
3948
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003949/*
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003950 * Old API for getting list of peer addresses. Does not work for 32-bit
3951 * programs running on a 64-bit kernel
3952 */
3953static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3954 char __user *optval,
3955 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 struct sctp_association *asoc;
3958 struct list_head *pos;
3959 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003960 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 struct sctp_transport *from;
3962 void __user *to;
3963 union sctp_addr temp;
3964 struct sctp_sock *sp = sctp_sk(sk);
3965 int addrlen;
3966
Neil Horman408f22e2007-06-16 14:03:45 -04003967 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 return -EINVAL;
3969
Neil Horman408f22e2007-06-16 14:03:45 -04003970 len = sizeof(struct sctp_getaddrs_old);
3971
3972 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return -EFAULT;
3974
3975 if (getaddrs.addr_num <= 0) return -EINVAL;
3976
3977 /* For UDP-style sockets, id specifies the association to query. */
3978 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3979 if (!asoc)
3980 return -EINVAL;
3981
3982 to = (void __user *)getaddrs.addrs;
3983 list_for_each(pos, &asoc->peer.transport_addr_list) {
3984 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08003985 memcpy(&temp, &from->ipaddr, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3987 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 if (copy_to_user(to, &temp, addrlen))
3989 return -EFAULT;
3990 to += addrlen ;
3991 cnt ++;
3992 if (cnt >= getaddrs.addr_num) break;
3993 }
3994 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04003995 if (put_user(len, optlen))
3996 return -EFAULT;
3997 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return -EFAULT;
3999
4000 return 0;
4001}
4002
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004003static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4004 char __user *optval, int __user *optlen)
4005{
4006 struct sctp_association *asoc;
4007 struct list_head *pos;
4008 int cnt = 0;
4009 struct sctp_getaddrs getaddrs;
4010 struct sctp_transport *from;
4011 void __user *to;
4012 union sctp_addr temp;
4013 struct sctp_sock *sp = sctp_sk(sk);
4014 int addrlen;
4015 size_t space_left;
4016 int bytes_copied;
4017
4018 if (len < sizeof(struct sctp_getaddrs))
4019 return -EINVAL;
4020
4021 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4022 return -EFAULT;
4023
4024 /* For UDP-style sockets, id specifies the association to query. */
4025 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4026 if (!asoc)
4027 return -EINVAL;
4028
4029 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004030 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004031
4032 list_for_each(pos, &asoc->peer.transport_addr_list) {
4033 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004034 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004035 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4036 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004037 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004038 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004039 if (copy_to_user(to, &temp, addrlen))
4040 return -EFAULT;
4041 to += addrlen;
4042 cnt++;
4043 space_left -= addrlen;
4044 }
4045
4046 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4047 return -EFAULT;
4048 bytes_copied = ((char __user *)to) - optval;
4049 if (put_user(bytes_copied, optlen))
4050 return -EFAULT;
4051
4052 return 0;
4053}
4054
4055static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4056 char __user *optval,
4057 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
4059 sctp_assoc_t id;
Vlad Yasevich29303542007-09-16 16:02:12 -07004060 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 struct sctp_bind_addr *bp;
4062 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 struct sctp_sockaddr_entry *addr;
4064 rwlock_t *addr_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 int cnt = 0;
4066
Neil Horman408f22e2007-06-16 14:03:45 -04004067 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 return -EINVAL;
4069
4070 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4071 return -EFAULT;
4072
4073 /*
4074 * For UDP-style sockets, id specifies the association to query.
4075 * If the id field is set to the value '0' then the locally bound
4076 * addresses are returned without regard to any particular
4077 * association.
4078 */
4079 if (0 == id) {
4080 bp = &sctp_sk(sk)->ep->base.bind_addr;
4081 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4082 } else {
4083 asoc = sctp_id2assoc(sk, id);
4084 if (!asoc)
4085 return -EINVAL;
4086 bp = &asoc->base.bind_addr;
4087 addr_lock = &asoc->base.addr_lock;
4088 }
4089
4090 sctp_read_lock(addr_lock);
4091
4092 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4093 * addresses from the global local address list.
4094 */
4095 if (sctp_list_single_entry(&bp->address_list)) {
4096 addr = list_entry(bp->address_list.next,
4097 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004098 if (sctp_is_any(&addr->a)) {
Vlad Yasevich29303542007-09-16 16:02:12 -07004099 rcu_read_lock();
4100 list_for_each_entry_rcu(addr,
4101 &sctp_local_addr_list, list) {
4102 if (!addr->valid)
4103 continue;
4104
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004105 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004106 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 continue;
Vlad Yasevich29303542007-09-16 16:02:12 -07004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 cnt++;
4110 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004111 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 } else {
4113 cnt = 1;
4114 }
4115 goto done;
4116 }
4117
4118 list_for_each(pos, &bp->address_list) {
4119 cnt ++;
4120 }
4121
4122done:
4123 sctp_read_unlock(addr_lock);
4124 return cnt;
4125}
4126
4127/* Helper function that copies local addresses to user and returns the number
4128 * of addresses copied.
4129 */
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004130static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4131 int max_addrs, void *to,
4132 int *bytes_copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 union sctp_addr temp;
4136 int cnt = 0;
4137 int addrlen;
4138
Vlad Yasevich29303542007-09-16 16:02:12 -07004139 rcu_read_lock();
4140 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4141 if (!addr->valid)
4142 continue;
4143
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004144 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004145 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 continue;
Al Viro6244be42006-11-20 17:21:44 -08004147 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4149 &temp);
4150 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004151 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004154 *bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 cnt ++;
4156 if (cnt >= max_addrs) break;
4157 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 return cnt;
4161}
4162
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004163static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4164 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004165{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004166 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004167 union sctp_addr temp;
4168 int cnt = 0;
4169 int addrlen;
4170
Vlad Yasevich29303542007-09-16 16:02:12 -07004171 rcu_read_lock();
4172 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4173 if (!addr->valid)
4174 continue;
4175
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004176 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004177 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004178 continue;
Al Viro6244be42006-11-20 17:21:44 -08004179 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004180 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4181 &temp);
4182 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004183 if (space_left < addrlen) {
4184 cnt = -ENOMEM;
4185 break;
4186 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004187 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004188
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004189 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004190 cnt ++;
4191 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004192 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004193 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004194 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004195
4196 return cnt;
4197}
4198
4199/* Old API for getting list of local addresses. Does not work for 32-bit
4200 * programs running on a 64-bit kernel
4201 */
4202static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4203 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 struct sctp_bind_addr *bp;
4206 struct sctp_association *asoc;
4207 struct list_head *pos;
4208 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004209 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 struct sctp_sockaddr_entry *addr;
4211 void __user *to;
4212 union sctp_addr temp;
4213 struct sctp_sock *sp = sctp_sk(sk);
4214 int addrlen;
4215 rwlock_t *addr_lock;
4216 int err = 0;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004217 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004218 void *buf;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004219 int bytes_copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Neil Horman408f22e2007-06-16 14:03:45 -04004221 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return -EINVAL;
4223
Neil Horman408f22e2007-06-16 14:03:45 -04004224 len = sizeof(struct sctp_getaddrs_old);
4225 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return -EFAULT;
4227
4228 if (getaddrs.addr_num <= 0) return -EINVAL;
4229 /*
4230 * For UDP-style sockets, id specifies the association to query.
4231 * If the id field is set to the value '0' then the locally bound
4232 * addresses are returned without regard to any particular
4233 * association.
4234 */
4235 if (0 == getaddrs.assoc_id) {
4236 bp = &sctp_sk(sk)->ep->base.bind_addr;
4237 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4238 } else {
4239 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4240 if (!asoc)
4241 return -EINVAL;
4242 bp = &asoc->base.bind_addr;
4243 addr_lock = &asoc->base.addr_lock;
4244 }
4245
4246 to = getaddrs.addrs;
4247
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004248 /* Allocate space for a local instance of packed array to hold all
4249 * the data. We store addresses here first and then put write them
4250 * to the user in one shot.
4251 */
4252 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4253 GFP_KERNEL);
4254 if (!addrs)
4255 return -ENOMEM;
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 sctp_read_lock(addr_lock);
4258
4259 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4260 * addresses from the global local address list.
4261 */
4262 if (sctp_list_single_entry(&bp->address_list)) {
4263 addr = list_entry(bp->address_list.next,
4264 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004265 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004266 cnt = sctp_copy_laddrs_old(sk, bp->port,
4267 getaddrs.addr_num,
4268 addrs, &bytes_copied);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004269 goto copy_getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 }
4271 }
4272
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004273 buf = addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 list_for_each(pos, &bp->address_list) {
4275 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004276 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4278 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004279 memcpy(buf, &temp, addrlen);
4280 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004281 bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 cnt ++;
4283 if (cnt >= getaddrs.addr_num) break;
4284 }
4285
4286copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004287 sctp_read_unlock(addr_lock);
4288
4289 /* copy the entire address list into the user provided space */
4290 if (copy_to_user(to, addrs, bytes_copied)) {
4291 err = -EFAULT;
4292 goto error;
4293 }
4294
4295 /* copy the leading structure back to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004297 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 err = -EFAULT;
4299
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004300error:
4301 kfree(addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return err;
4303}
4304
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004305static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4306 char __user *optval, int __user *optlen)
4307{
4308 struct sctp_bind_addr *bp;
4309 struct sctp_association *asoc;
4310 struct list_head *pos;
4311 int cnt = 0;
4312 struct sctp_getaddrs getaddrs;
4313 struct sctp_sockaddr_entry *addr;
4314 void __user *to;
4315 union sctp_addr temp;
4316 struct sctp_sock *sp = sctp_sk(sk);
4317 int addrlen;
4318 rwlock_t *addr_lock;
4319 int err = 0;
4320 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004321 int bytes_copied = 0;
4322 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004323 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004324
Neil Horman408f22e2007-06-16 14:03:45 -04004325 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004326 return -EINVAL;
4327
4328 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4329 return -EFAULT;
4330
4331 /*
4332 * For UDP-style sockets, id specifies the association to query.
4333 * If the id field is set to the value '0' then the locally bound
4334 * addresses are returned without regard to any particular
4335 * association.
4336 */
4337 if (0 == getaddrs.assoc_id) {
4338 bp = &sctp_sk(sk)->ep->base.bind_addr;
4339 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4340 } else {
4341 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4342 if (!asoc)
4343 return -EINVAL;
4344 bp = &asoc->base.bind_addr;
4345 addr_lock = &asoc->base.addr_lock;
4346 }
4347
4348 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004349 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4350
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004351 addrs = kmalloc(space_left, GFP_KERNEL);
4352 if (!addrs)
4353 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004354
4355 sctp_read_lock(addr_lock);
4356
4357 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4358 * addresses from the global local address list.
4359 */
4360 if (sctp_list_single_entry(&bp->address_list)) {
4361 addr = list_entry(bp->address_list.next,
4362 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004363 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004364 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4365 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004366 if (cnt < 0) {
4367 err = cnt;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004368 goto error_lock;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004369 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004370 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004371 }
4372 }
4373
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004374 buf = addrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004375 list_for_each(pos, &bp->address_list) {
4376 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004377 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004378 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4379 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004380 if (space_left < addrlen) {
4381 err = -ENOMEM; /*fixme: right error?*/
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004382 goto error_lock;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004383 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004384 memcpy(buf, &temp, addrlen);
4385 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004386 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004387 cnt ++;
4388 space_left -= addrlen;
4389 }
4390
4391copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004392 sctp_read_unlock(addr_lock);
4393
4394 if (copy_to_user(to, addrs, bytes_copied)) {
4395 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004396 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004397 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004398 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4399 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004400 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004401 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004402 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004403 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004404
4405 goto out;
4406
4407error_lock:
4408 sctp_read_unlock(addr_lock);
4409
4410out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004411 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004412 return err;
4413}
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4416 *
4417 * Requests that the local SCTP stack use the enclosed peer address as
4418 * the association primary. The enclosed address must be one of the
4419 * association peer's addresses.
4420 */
4421static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4422 char __user *optval, int __user *optlen)
4423{
4424 struct sctp_prim prim;
4425 struct sctp_association *asoc;
4426 struct sctp_sock *sp = sctp_sk(sk);
4427
Neil Horman408f22e2007-06-16 14:03:45 -04004428 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return -EINVAL;
4430
Neil Horman408f22e2007-06-16 14:03:45 -04004431 len = sizeof(struct sctp_prim);
4432
4433 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return -EFAULT;
4435
4436 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4437 if (!asoc)
4438 return -EINVAL;
4439
4440 if (!asoc->peer.primary_path)
4441 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004442
Al Viro8cec6b82006-11-20 17:23:01 -08004443 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4444 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4447 (union sctp_addr *)&prim.ssp_addr);
4448
Neil Horman408f22e2007-06-16 14:03:45 -04004449 if (put_user(len, optlen))
4450 return -EFAULT;
4451 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return -EFAULT;
4453
4454 return 0;
4455}
4456
4457/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004458 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004460 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 * Indication parameter for all future INIT and INIT-ACK exchanges.
4462 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004463static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 char __user *optval, int __user *optlen)
4465{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004466 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Neil Horman408f22e2007-06-16 14:03:45 -04004468 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470
Neil Horman408f22e2007-06-16 14:03:45 -04004471 len = sizeof(struct sctp_setadaptation);
4472
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004473 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004474
4475 if (put_user(len, optlen))
4476 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004477 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return 0;
4481}
4482
4483/*
4484 *
4485 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4486 *
4487 * Applications that wish to use the sendto() system call may wish to
4488 * specify a default set of parameters that would normally be supplied
4489 * through the inclusion of ancillary data. This socket option allows
4490 * such an application to set the default sctp_sndrcvinfo structure.
4491
4492
4493 * The application that wishes to use this socket option simply passes
4494 * in to this call the sctp_sndrcvinfo structure defined in Section
4495 * 5.2.2) The input parameters accepted by this call include
4496 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4497 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4498 * to this call if the caller is using the UDP model.
4499 *
4500 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4501 */
4502static int sctp_getsockopt_default_send_param(struct sock *sk,
4503 int len, char __user *optval,
4504 int __user *optlen)
4505{
4506 struct sctp_sndrcvinfo info;
4507 struct sctp_association *asoc;
4508 struct sctp_sock *sp = sctp_sk(sk);
4509
Neil Horman408f22e2007-06-16 14:03:45 -04004510 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004512
4513 len = sizeof(struct sctp_sndrcvinfo);
4514
4515 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return -EFAULT;
4517
4518 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4519 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4520 return -EINVAL;
4521
4522 if (asoc) {
4523 info.sinfo_stream = asoc->default_stream;
4524 info.sinfo_flags = asoc->default_flags;
4525 info.sinfo_ppid = asoc->default_ppid;
4526 info.sinfo_context = asoc->default_context;
4527 info.sinfo_timetolive = asoc->default_timetolive;
4528 } else {
4529 info.sinfo_stream = sp->default_stream;
4530 info.sinfo_flags = sp->default_flags;
4531 info.sinfo_ppid = sp->default_ppid;
4532 info.sinfo_context = sp->default_context;
4533 info.sinfo_timetolive = sp->default_timetolive;
4534 }
4535
Neil Horman408f22e2007-06-16 14:03:45 -04004536 if (put_user(len, optlen))
4537 return -EFAULT;
4538 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return -EFAULT;
4540
4541 return 0;
4542}
4543
4544/*
4545 *
4546 * 7.1.5 SCTP_NODELAY
4547 *
4548 * Turn on/off any Nagle-like algorithm. This means that packets are
4549 * generally sent as soon as possible and no unnecessary delays are
4550 * introduced, at the cost of more packets in the network. Expects an
4551 * integer boolean flag.
4552 */
4553
4554static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4555 char __user *optval, int __user *optlen)
4556{
4557 int val;
4558
4559 if (len < sizeof(int))
4560 return -EINVAL;
4561
4562 len = sizeof(int);
4563 val = (sctp_sk(sk)->nodelay == 1);
4564 if (put_user(len, optlen))
4565 return -EFAULT;
4566 if (copy_to_user(optval, &val, len))
4567 return -EFAULT;
4568 return 0;
4569}
4570
4571/*
4572 *
4573 * 7.1.1 SCTP_RTOINFO
4574 *
4575 * The protocol parameters used to initialize and bound retransmission
4576 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4577 * and modify these parameters.
4578 * All parameters are time values, in milliseconds. A value of 0, when
4579 * modifying the parameters, indicates that the current value should not
4580 * be changed.
4581 *
4582 */
4583static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4584 char __user *optval,
4585 int __user *optlen) {
4586 struct sctp_rtoinfo rtoinfo;
4587 struct sctp_association *asoc;
4588
Neil Horman408f22e2007-06-16 14:03:45 -04004589 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return -EINVAL;
4591
Neil Horman408f22e2007-06-16 14:03:45 -04004592 len = sizeof(struct sctp_rtoinfo);
4593
4594 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return -EFAULT;
4596
4597 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4598
4599 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4600 return -EINVAL;
4601
4602 /* Values corresponding to the specific association. */
4603 if (asoc) {
4604 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4605 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4606 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4607 } else {
4608 /* Values corresponding to the endpoint. */
4609 struct sctp_sock *sp = sctp_sk(sk);
4610
4611 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4612 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4613 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4614 }
4615
4616 if (put_user(len, optlen))
4617 return -EFAULT;
4618
4619 if (copy_to_user(optval, &rtoinfo, len))
4620 return -EFAULT;
4621
4622 return 0;
4623}
4624
4625/*
4626 *
4627 * 7.1.2 SCTP_ASSOCINFO
4628 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004629 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 * of the association.
4631 * Returns an error if the new association retransmission value is
4632 * greater than the sum of the retransmission value of the peer.
4633 * See [SCTP] for more information.
4634 *
4635 */
4636static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4637 char __user *optval,
4638 int __user *optlen)
4639{
4640
4641 struct sctp_assocparams assocparams;
4642 struct sctp_association *asoc;
4643 struct list_head *pos;
4644 int cnt = 0;
4645
Neil Horman408f22e2007-06-16 14:03:45 -04004646 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return -EINVAL;
4648
Neil Horman408f22e2007-06-16 14:03:45 -04004649 len = sizeof(struct sctp_assocparams);
4650
4651 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 return -EFAULT;
4653
4654 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4655
4656 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4657 return -EINVAL;
4658
4659 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004660 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4662 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4663 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4664 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4665 * 1000) +
4666 (asoc->cookie_life.tv_usec
4667 / 1000);
4668
4669 list_for_each(pos, &asoc->peer.transport_addr_list) {
4670 cnt ++;
4671 }
4672
4673 assocparams.sasoc_number_peer_destinations = cnt;
4674 } else {
4675 /* Values corresponding to the endpoint */
4676 struct sctp_sock *sp = sctp_sk(sk);
4677
4678 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4679 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4680 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4681 assocparams.sasoc_cookie_life =
4682 sp->assocparams.sasoc_cookie_life;
4683 assocparams.sasoc_number_peer_destinations =
4684 sp->assocparams.
4685 sasoc_number_peer_destinations;
4686 }
4687
4688 if (put_user(len, optlen))
4689 return -EFAULT;
4690
4691 if (copy_to_user(optval, &assocparams, len))
4692 return -EFAULT;
4693
4694 return 0;
4695}
4696
4697/*
4698 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4699 *
4700 * This socket option is a boolean flag which turns on or off mapped V4
4701 * addresses. If this option is turned on and the socket is type
4702 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4703 * If this option is turned off, then no mapping will be done of V4
4704 * addresses and a user will receive both PF_INET6 and PF_INET type
4705 * addresses on the socket.
4706 */
4707static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4708 char __user *optval, int __user *optlen)
4709{
4710 int val;
4711 struct sctp_sock *sp = sctp_sk(sk);
4712
4713 if (len < sizeof(int))
4714 return -EINVAL;
4715
4716 len = sizeof(int);
4717 val = sp->v4mapped;
4718 if (put_user(len, optlen))
4719 return -EFAULT;
4720 if (copy_to_user(optval, &val, len))
4721 return -EFAULT;
4722
4723 return 0;
4724}
4725
4726/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004727 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4728 * (chapter and verse is quoted at sctp_setsockopt_context())
4729 */
4730static int sctp_getsockopt_context(struct sock *sk, int len,
4731 char __user *optval, int __user *optlen)
4732{
4733 struct sctp_assoc_value params;
4734 struct sctp_sock *sp;
4735 struct sctp_association *asoc;
4736
Neil Horman408f22e2007-06-16 14:03:45 -04004737 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004738 return -EINVAL;
4739
Neil Horman408f22e2007-06-16 14:03:45 -04004740 len = sizeof(struct sctp_assoc_value);
4741
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004742 if (copy_from_user(&params, optval, len))
4743 return -EFAULT;
4744
4745 sp = sctp_sk(sk);
4746
4747 if (params.assoc_id != 0) {
4748 asoc = sctp_id2assoc(sk, params.assoc_id);
4749 if (!asoc)
4750 return -EINVAL;
4751 params.assoc_value = asoc->default_rcv_context;
4752 } else {
4753 params.assoc_value = sp->default_rcv_context;
4754 }
4755
4756 if (put_user(len, optlen))
4757 return -EFAULT;
4758 if (copy_to_user(optval, &params, len))
4759 return -EFAULT;
4760
4761 return 0;
4762}
4763
4764/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4766 *
4767 * This socket option specifies the maximum size to put in any outgoing
4768 * SCTP chunk. If a message is larger than this size it will be
4769 * fragmented by SCTP into the specified size. Note that the underlying
4770 * SCTP implementation may fragment into smaller sized chunks when the
4771 * PMTU of the underlying association is smaller than the value set by
4772 * the user.
4773 */
4774static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4775 char __user *optval, int __user *optlen)
4776{
4777 int val;
4778
4779 if (len < sizeof(int))
4780 return -EINVAL;
4781
4782 len = sizeof(int);
4783
4784 val = sctp_sk(sk)->user_frag;
4785 if (put_user(len, optlen))
4786 return -EFAULT;
4787 if (copy_to_user(optval, &val, len))
4788 return -EFAULT;
4789
4790 return 0;
4791}
4792
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004793/*
4794 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4795 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4796 */
4797static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4798 char __user *optval, int __user *optlen)
4799{
4800 int val;
4801
4802 if (len < sizeof(int))
4803 return -EINVAL;
4804
4805 len = sizeof(int);
4806
4807 val = sctp_sk(sk)->frag_interleave;
4808 if (put_user(len, optlen))
4809 return -EFAULT;
4810 if (copy_to_user(optval, &val, len))
4811 return -EFAULT;
4812
4813 return 0;
4814}
4815
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004816/*
4817 * 7.1.25. Set or Get the sctp partial delivery point
4818 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4819 */
4820static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4821 char __user *optval,
4822 int __user *optlen)
4823{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004824 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004825
4826 if (len < sizeof(u32))
4827 return -EINVAL;
4828
4829 len = sizeof(u32);
4830
4831 val = sctp_sk(sk)->pd_point;
4832 if (put_user(len, optlen))
4833 return -EFAULT;
4834 if (copy_to_user(optval, &val, len))
4835 return -EFAULT;
4836
4837 return -ENOTSUPP;
4838}
4839
Vlad Yasevich70331572007-03-23 11:34:36 -07004840/*
4841 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4842 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4843 */
4844static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4845 char __user *optval,
4846 int __user *optlen)
4847{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004848 int val;
Vlad Yasevich70331572007-03-23 11:34:36 -07004849
4850 if (len < sizeof(int))
4851 return -EINVAL;
4852
4853 len = sizeof(int);
4854
4855 val = sctp_sk(sk)->max_burst;
4856 if (put_user(len, optlen))
4857 return -EFAULT;
4858 if (copy_to_user(optval, &val, len))
4859 return -EFAULT;
4860
4861 return -ENOTSUPP;
4862}
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4865 char __user *optval, int __user *optlen)
4866{
4867 int retval = 0;
4868 int len;
4869
Frank Filz3f7a87d2005-06-20 13:14:57 -07004870 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4871 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 /* I can hardly begin to describe how wrong this is. This is
4874 * so broken as to be worse than useless. The API draft
4875 * REALLY is NOT helpful here... I am not convinced that the
4876 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4877 * are at all well-founded.
4878 */
4879 if (level != SOL_SCTP) {
4880 struct sctp_af *af = sctp_sk(sk)->pf->af;
4881
4882 retval = af->getsockopt(sk, level, optname, optval, optlen);
4883 return retval;
4884 }
4885
4886 if (get_user(len, optlen))
4887 return -EFAULT;
4888
4889 sctp_lock_sock(sk);
4890
4891 switch (optname) {
4892 case SCTP_STATUS:
4893 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4894 break;
4895 case SCTP_DISABLE_FRAGMENTS:
4896 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4897 optlen);
4898 break;
4899 case SCTP_EVENTS:
4900 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4901 break;
4902 case SCTP_AUTOCLOSE:
4903 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4904 break;
4905 case SCTP_SOCKOPT_PEELOFF:
4906 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4907 break;
4908 case SCTP_PEER_ADDR_PARAMS:
4909 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4910 optlen);
4911 break;
Frank Filz77086102005-12-22 11:37:30 -08004912 case SCTP_DELAYED_ACK_TIME:
4913 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4914 optlen);
4915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 case SCTP_INITMSG:
4917 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4918 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004919 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4920 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4921 optlen);
4922 break;
4923 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4924 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4925 optlen);
4926 break;
4927 case SCTP_GET_PEER_ADDRS_OLD:
4928 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 optlen);
4930 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004931 case SCTP_GET_LOCAL_ADDRS_OLD:
4932 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 optlen);
4934 break;
4935 case SCTP_GET_PEER_ADDRS:
4936 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4937 optlen);
4938 break;
4939 case SCTP_GET_LOCAL_ADDRS:
4940 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4941 optlen);
4942 break;
4943 case SCTP_DEFAULT_SEND_PARAM:
4944 retval = sctp_getsockopt_default_send_param(sk, len,
4945 optval, optlen);
4946 break;
4947 case SCTP_PRIMARY_ADDR:
4948 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4949 break;
4950 case SCTP_NODELAY:
4951 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4952 break;
4953 case SCTP_RTOINFO:
4954 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4955 break;
4956 case SCTP_ASSOCINFO:
4957 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4958 break;
4959 case SCTP_I_WANT_MAPPED_V4_ADDR:
4960 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4961 break;
4962 case SCTP_MAXSEG:
4963 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4964 break;
4965 case SCTP_GET_PEER_ADDR_INFO:
4966 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4967 optlen);
4968 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004969 case SCTP_ADAPTATION_LAYER:
4970 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 optlen);
4972 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004973 case SCTP_CONTEXT:
4974 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4975 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004976 case SCTP_FRAGMENT_INTERLEAVE:
4977 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4978 optlen);
4979 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004980 case SCTP_PARTIAL_DELIVERY_POINT:
4981 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4982 optlen);
4983 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004984 case SCTP_MAX_BURST:
4985 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 default:
4988 retval = -ENOPROTOOPT;
4989 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 sctp_release_sock(sk);
4993 return retval;
4994}
4995
4996static void sctp_hash(struct sock *sk)
4997{
4998 /* STUB */
4999}
5000
5001static void sctp_unhash(struct sock *sk)
5002{
5003 /* STUB */
5004}
5005
5006/* Check if port is acceptable. Possibly find first available port.
5007 *
5008 * The port hash table (contained in the 'global' SCTP protocol storage
5009 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5010 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5011 * list (the list number is the port number hashed out, so as you
5012 * would expect from a hash function, all the ports in a given list have
5013 * such a number that hashes out to the same list number; you were
5014 * expecting that, right?); so each list has a set of ports, with a
5015 * link to the socket (struct sock) that uses it, the port number and
5016 * a fastreuse flag (FIXME: NPI ipg).
5017 */
5018static struct sctp_bind_bucket *sctp_bucket_create(
5019 struct sctp_bind_hashbucket *head, unsigned short snum);
5020
5021static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5022{
5023 struct sctp_bind_hashbucket *head; /* hash list */
5024 struct sctp_bind_bucket *pp; /* hash list port iterator */
5025 unsigned short snum;
5026 int ret;
5027
Al Viro04afd8b2006-11-20 17:02:01 -08005028 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5031 sctp_local_bh_disable();
5032
5033 if (snum == 0) {
5034 /* Search for an available port.
5035 *
5036 * 'sctp_port_rover' was the last port assigned, so
5037 * we start to search from 'sctp_port_rover +
5038 * 1'. What we do is first check if port 'rover' is
5039 * already in the hash table; if not, we use that; if
5040 * it is, we try next.
5041 */
5042 int low = sysctl_local_port_range[0];
5043 int high = sysctl_local_port_range[1];
5044 int remaining = (high - low) + 1;
5045 int rover;
5046 int index;
5047
5048 sctp_spin_lock(&sctp_port_alloc_lock);
5049 rover = sctp_port_rover;
5050 do {
5051 rover++;
5052 if ((rover < low) || (rover > high))
5053 rover = low;
5054 index = sctp_phashfn(rover);
5055 head = &sctp_port_hashtable[index];
5056 sctp_spin_lock(&head->lock);
5057 for (pp = head->chain; pp; pp = pp->next)
5058 if (pp->port == rover)
5059 goto next;
5060 break;
5061 next:
5062 sctp_spin_unlock(&head->lock);
5063 } while (--remaining > 0);
5064 sctp_port_rover = rover;
5065 sctp_spin_unlock(&sctp_port_alloc_lock);
5066
5067 /* Exhausted local port range during search? */
5068 ret = 1;
5069 if (remaining <= 0)
5070 goto fail;
5071
5072 /* OK, here is the one we will use. HEAD (the port
5073 * hash table list entry) is non-NULL and we hold it's
5074 * mutex.
5075 */
5076 snum = rover;
5077 } else {
5078 /* We are given an specific port number; we verify
5079 * that it is not being used. If it is used, we will
5080 * exahust the search in the hash list corresponding
5081 * to the port number (snum) - we detect that with the
5082 * port iterator, pp being NULL.
5083 */
5084 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5085 sctp_spin_lock(&head->lock);
5086 for (pp = head->chain; pp; pp = pp->next) {
5087 if (pp->port == snum)
5088 goto pp_found;
5089 }
5090 }
5091 pp = NULL;
5092 goto pp_not_found;
5093pp_found:
5094 if (!hlist_empty(&pp->owner)) {
5095 /* We had a port hash table hit - there is an
5096 * available port (pp != NULL) and it is being
5097 * used by other socket (pp->owner not empty); that other
5098 * socket is going to be sk2.
5099 */
5100 int reuse = sk->sk_reuse;
5101 struct sock *sk2;
5102 struct hlist_node *node;
5103
5104 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005105 if (pp->fastreuse && sk->sk_reuse &&
5106 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 goto success;
5108
5109 /* Run through the list of sockets bound to the port
5110 * (pp->port) [via the pointers bind_next and
5111 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5112 * we get the endpoint they describe and run through
5113 * the endpoint's list of IP (v4 or v6) addresses,
5114 * comparing each of the addresses with the address of
5115 * the socket sk. If we find a match, then that means
5116 * that this port/socket (sk) combination are already
5117 * in an endpoint.
5118 */
5119 sk_for_each_bound(sk2, node, &pp->owner) {
5120 struct sctp_endpoint *ep2;
5121 ep2 = sctp_sk(sk2)->ep;
5122
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005123 if (reuse && sk2->sk_reuse &&
5124 sk2->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 continue;
5126
Al Viro7e1e4a22006-11-20 17:05:43 -08005127 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 sctp_sk(sk))) {
5129 ret = (long)sk2;
5130 goto fail_unlock;
5131 }
5132 }
5133 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5134 }
5135pp_not_found:
5136 /* If there was a hash table miss, create a new port. */
5137 ret = 1;
5138 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5139 goto fail_unlock;
5140
5141 /* In either case (hit or miss), make sure fastreuse is 1 only
5142 * if sk->sk_reuse is too (that is, if the caller requested
5143 * SO_REUSEADDR on this socket -sk-).
5144 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005145 if (hlist_empty(&pp->owner)) {
5146 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5147 pp->fastreuse = 1;
5148 else
5149 pp->fastreuse = 0;
5150 } else if (pp->fastreuse &&
5151 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 pp->fastreuse = 0;
5153
5154 /* We are set, so fill up all the data in the hash table
5155 * entry, tie the socket list information with the rest of the
5156 * sockets FIXME: Blurry, NPI (ipg).
5157 */
5158success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 if (!sctp_sk(sk)->bind_hash) {
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005160 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 sk_add_bind_node(sk, &pp->owner);
5162 sctp_sk(sk)->bind_hash = pp;
5163 }
5164 ret = 0;
5165
5166fail_unlock:
5167 sctp_spin_unlock(&head->lock);
5168
5169fail:
5170 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 return ret;
5172}
5173
5174/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5175 * port is requested.
5176 */
5177static int sctp_get_port(struct sock *sk, unsigned short snum)
5178{
5179 long ret;
5180 union sctp_addr addr;
5181 struct sctp_af *af = sctp_sk(sk)->pf->af;
5182
5183 /* Set up a dummy address struct from the sk. */
5184 af->from_sk(&addr, sk);
5185 addr.v4.sin_port = htons(snum);
5186
5187 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5188 ret = sctp_get_port_local(sk, &addr);
5189
5190 return (ret ? 1 : 0);
5191}
5192
5193/*
5194 * 3.1.3 listen() - UDP Style Syntax
5195 *
5196 * By default, new associations are not accepted for UDP style sockets.
5197 * An application uses listen() to mark a socket as being able to
5198 * accept new associations.
5199 */
5200SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5201{
5202 struct sctp_sock *sp = sctp_sk(sk);
5203 struct sctp_endpoint *ep = sp->ep;
5204
5205 /* Only UDP style sockets that are not peeled off are allowed to
5206 * listen().
5207 */
5208 if (!sctp_style(sk, UDP))
5209 return -EINVAL;
5210
5211 /* If backlog is zero, disable listening. */
5212 if (!backlog) {
5213 if (sctp_sstate(sk, CLOSED))
5214 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 sctp_unhash_endpoint(ep);
5217 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 }
5220
5221 /* Return if we are already listening. */
5222 if (sctp_sstate(sk, LISTENING))
5223 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 /*
5226 * If a bind() or sctp_bindx() is not called prior to a listen()
5227 * call that allows new associations to be accepted, the system
5228 * picks an ephemeral port and will choose an address set equivalent
5229 * to binding with a wildcard address.
5230 *
5231 * This is not currently spelled out in the SCTP sockets
5232 * extensions draft, but follows the practice as seen in TCP
5233 * sockets.
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005234 *
5235 * Additionally, turn off fastreuse flag since we are not listening
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005237 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (!ep->base.bind_addr.port) {
5239 if (sctp_autobind(sk))
5240 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005241 } else
5242 sctp_sk(sk)->bind_hash->fastreuse = 0;
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 sctp_hash_endpoint(ep);
5245 return 0;
5246}
5247
5248/*
5249 * 4.1.3 listen() - TCP Style Syntax
5250 *
5251 * Applications uses listen() to ready the SCTP endpoint for accepting
5252 * inbound associations.
5253 */
5254SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5255{
5256 struct sctp_sock *sp = sctp_sk(sk);
5257 struct sctp_endpoint *ep = sp->ep;
5258
5259 /* If backlog is zero, disable listening. */
5260 if (!backlog) {
5261 if (sctp_sstate(sk, CLOSED))
5262 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 sctp_unhash_endpoint(ep);
5265 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 }
5268
5269 if (sctp_sstate(sk, LISTENING))
5270 return 0;
5271
5272 /*
5273 * If a bind() or sctp_bindx() is not called prior to a listen()
5274 * call that allows new associations to be accepted, the system
5275 * picks an ephemeral port and will choose an address set equivalent
5276 * to binding with a wildcard address.
5277 *
5278 * This is not currently spelled out in the SCTP sockets
5279 * extensions draft, but follows the practice as seen in TCP
5280 * sockets.
5281 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005282 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 if (!ep->base.bind_addr.port) {
5284 if (sctp_autobind(sk))
5285 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005286 } else
5287 sctp_sk(sk)->bind_hash->fastreuse = 0;
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 sk->sk_max_ack_backlog = backlog;
5290 sctp_hash_endpoint(ep);
5291 return 0;
5292}
5293
5294/*
5295 * Move a socket to LISTENING state.
5296 */
5297int sctp_inet_listen(struct socket *sock, int backlog)
5298{
5299 struct sock *sk = sock->sk;
Herbert Xu1b489e12006-08-20 15:07:14 +10005300 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 int err = -EINVAL;
5302
5303 if (unlikely(backlog < 0))
5304 goto out;
5305
5306 sctp_lock_sock(sk);
5307
5308 if (sock->state != SS_UNCONNECTED)
5309 goto out;
5310
5311 /* Allocate HMAC for generating cookie. */
5312 if (sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005313 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005314 if (IS_ERR(tfm)) {
5315 if (net_ratelimit()) {
5316 printk(KERN_INFO
5317 "SCTP: failed to load transform for %s: %ld\n",
5318 sctp_hmac_alg, PTR_ERR(tfm));
5319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 err = -ENOSYS;
5321 goto out;
5322 }
5323 }
5324
5325 switch (sock->type) {
5326 case SOCK_SEQPACKET:
5327 err = sctp_seqpacket_listen(sk, backlog);
5328 break;
5329 case SOCK_STREAM:
5330 err = sctp_stream_listen(sk, backlog);
5331 break;
5332 default:
5333 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005334 }
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 if (err)
5337 goto cleanup;
5338
5339 /* Store away the transform reference. */
5340 sctp_sk(sk)->hmac = tfm;
5341out:
5342 sctp_release_sock(sk);
5343 return err;
5344cleanup:
Herbert Xu1b489e12006-08-20 15:07:14 +10005345 crypto_free_hash(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 goto out;
5347}
5348
5349/*
5350 * This function is done by modeling the current datagram_poll() and the
5351 * tcp_poll(). Note that, based on these implementations, we don't
5352 * lock the socket in this function, even though it seems that,
5353 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005354 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 * in this place. We assume that we don't need locks either until proven
5356 * otherwise.
5357 *
5358 * Another thing to note is that we include the Async I/O support
5359 * here, again, by modeling the current TCP/UDP code. We don't have
5360 * a good way to test with it yet.
5361 */
5362unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5363{
5364 struct sock *sk = sock->sk;
5365 struct sctp_sock *sp = sctp_sk(sk);
5366 unsigned int mask;
5367
5368 poll_wait(file, sk->sk_sleep, wait);
5369
5370 /* A TCP-style listening socket becomes readable when the accept queue
5371 * is not empty.
5372 */
5373 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5374 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005375 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 mask = 0;
5378
5379 /* Is there any exceptional events? */
5380 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5381 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08005382 if (sk->sk_shutdown & RCV_SHUTDOWN)
5383 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (sk->sk_shutdown == SHUTDOWN_MASK)
5385 mask |= POLLHUP;
5386
5387 /* Is it readable? Reconsider this code with TCP-style support. */
5388 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5389 (sk->sk_shutdown & RCV_SHUTDOWN))
5390 mask |= POLLIN | POLLRDNORM;
5391
5392 /* The association is either gone or not ready. */
5393 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5394 return mask;
5395
5396 /* Is it writable? */
5397 if (sctp_writeable(sk)) {
5398 mask |= POLLOUT | POLLWRNORM;
5399 } else {
5400 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5401 /*
5402 * Since the socket is not locked, the buffer
5403 * might be made available after the writeable check and
5404 * before the bit is set. This could cause a lost I/O
5405 * signal. tcp_poll() has a race breaker for this race
5406 * condition. Based on their implementation, we put
5407 * in the following code to cover it as well.
5408 */
5409 if (sctp_writeable(sk))
5410 mask |= POLLOUT | POLLWRNORM;
5411 }
5412 return mask;
5413}
5414
5415/********************************************************************
5416 * 2nd Level Abstractions
5417 ********************************************************************/
5418
5419static struct sctp_bind_bucket *sctp_bucket_create(
5420 struct sctp_bind_hashbucket *head, unsigned short snum)
5421{
5422 struct sctp_bind_bucket *pp;
5423
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08005424 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 SCTP_DBG_OBJCNT_INC(bind_bucket);
5426 if (pp) {
5427 pp->port = snum;
5428 pp->fastreuse = 0;
5429 INIT_HLIST_HEAD(&pp->owner);
5430 if ((pp->next = head->chain) != NULL)
5431 pp->next->pprev = &pp->next;
5432 head->chain = pp;
5433 pp->pprev = &head->chain;
5434 }
5435 return pp;
5436}
5437
5438/* Caller must hold hashbucket lock for this tb with local BH disabled */
5439static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5440{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005441 if (pp && hlist_empty(&pp->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 if (pp->next)
5443 pp->next->pprev = pp->pprev;
5444 *(pp->pprev) = pp->next;
5445 kmem_cache_free(sctp_bucket_cachep, pp);
5446 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5447 }
5448}
5449
5450/* Release this socket's reference to a local port. */
5451static inline void __sctp_put_port(struct sock *sk)
5452{
5453 struct sctp_bind_hashbucket *head =
5454 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5455 struct sctp_bind_bucket *pp;
5456
5457 sctp_spin_lock(&head->lock);
5458 pp = sctp_sk(sk)->bind_hash;
5459 __sk_del_bind_node(sk);
5460 sctp_sk(sk)->bind_hash = NULL;
5461 inet_sk(sk)->num = 0;
5462 sctp_bucket_destroy(pp);
5463 sctp_spin_unlock(&head->lock);
5464}
5465
5466void sctp_put_port(struct sock *sk)
5467{
5468 sctp_local_bh_disable();
5469 __sctp_put_port(sk);
5470 sctp_local_bh_enable();
5471}
5472
5473/*
5474 * The system picks an ephemeral port and choose an address set equivalent
5475 * to binding with a wildcard address.
5476 * One of those addresses will be the primary address for the association.
5477 * This automatically enables the multihoming capability of SCTP.
5478 */
5479static int sctp_autobind(struct sock *sk)
5480{
5481 union sctp_addr autoaddr;
5482 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08005483 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
5485 /* Initialize a local sockaddr structure to INADDR_ANY. */
5486 af = sctp_sk(sk)->pf->af;
5487
5488 port = htons(inet_sk(sk)->num);
5489 af->inaddr_any(&autoaddr, port);
5490
5491 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5492}
5493
5494/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5495 *
5496 * From RFC 2292
5497 * 4.2 The cmsghdr Structure *
5498 *
5499 * When ancillary data is sent or received, any number of ancillary data
5500 * objects can be specified by the msg_control and msg_controllen members of
5501 * the msghdr structure, because each object is preceded by
5502 * a cmsghdr structure defining the object's length (the cmsg_len member).
5503 * Historically Berkeley-derived implementations have passed only one object
5504 * at a time, but this API allows multiple objects to be
5505 * passed in a single call to sendmsg() or recvmsg(). The following example
5506 * shows two ancillary data objects in a control buffer.
5507 *
5508 * |<--------------------------- msg_controllen -------------------------->|
5509 * | |
5510 *
5511 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5512 *
5513 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5514 * | | |
5515 *
5516 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5517 *
5518 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5519 * | | | | |
5520 *
5521 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5522 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5523 *
5524 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5525 *
5526 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5527 * ^
5528 * |
5529 *
5530 * msg_control
5531 * points here
5532 */
5533SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5534 sctp_cmsgs_t *cmsgs)
5535{
5536 struct cmsghdr *cmsg;
5537
5538 for (cmsg = CMSG_FIRSTHDR(msg);
5539 cmsg != NULL;
5540 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5541 if (!CMSG_OK(msg, cmsg))
5542 return -EINVAL;
5543
5544 /* Should we parse this header or ignore? */
5545 if (cmsg->cmsg_level != IPPROTO_SCTP)
5546 continue;
5547
5548 /* Strictly check lengths following example in SCM code. */
5549 switch (cmsg->cmsg_type) {
5550 case SCTP_INIT:
5551 /* SCTP Socket API Extension
5552 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5553 *
5554 * This cmsghdr structure provides information for
5555 * initializing new SCTP associations with sendmsg().
5556 * The SCTP_INITMSG socket option uses this same data
5557 * structure. This structure is not used for
5558 * recvmsg().
5559 *
5560 * cmsg_level cmsg_type cmsg_data[]
5561 * ------------ ------------ ----------------------
5562 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5563 */
5564 if (cmsg->cmsg_len !=
5565 CMSG_LEN(sizeof(struct sctp_initmsg)))
5566 return -EINVAL;
5567 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5568 break;
5569
5570 case SCTP_SNDRCV:
5571 /* SCTP Socket API Extension
5572 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5573 *
5574 * This cmsghdr structure specifies SCTP options for
5575 * sendmsg() and describes SCTP header information
5576 * about a received message through recvmsg().
5577 *
5578 * cmsg_level cmsg_type cmsg_data[]
5579 * ------------ ------------ ----------------------
5580 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5581 */
5582 if (cmsg->cmsg_len !=
5583 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5584 return -EINVAL;
5585
5586 cmsgs->info =
5587 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5588
5589 /* Minimally, validate the sinfo_flags. */
5590 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005591 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5592 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 return -EINVAL;
5594 break;
5595
5596 default:
5597 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 }
5600 return 0;
5601}
5602
5603/*
5604 * Wait for a packet..
5605 * Note: This function is the same function as in core/datagram.c
5606 * with a few modifications to make lksctp work.
5607 */
5608static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5609{
5610 int error;
5611 DEFINE_WAIT(wait);
5612
5613 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5614
5615 /* Socket errors? */
5616 error = sock_error(sk);
5617 if (error)
5618 goto out;
5619
5620 if (!skb_queue_empty(&sk->sk_receive_queue))
5621 goto ready;
5622
5623 /* Socket shut down? */
5624 if (sk->sk_shutdown & RCV_SHUTDOWN)
5625 goto out;
5626
5627 /* Sequenced packets can come disconnected. If so we report the
5628 * problem.
5629 */
5630 error = -ENOTCONN;
5631
5632 /* Is there a good reason to think that we may receive some data? */
5633 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5634 goto out;
5635
5636 /* Handle signals. */
5637 if (signal_pending(current))
5638 goto interrupted;
5639
5640 /* Let another process have a go. Since we are going to sleep
5641 * anyway. Note: This may cause odd behaviors if the message
5642 * does not fit in the user's buffer, but this seems to be the
5643 * only way to honor MSG_DONTWAIT realistically.
5644 */
5645 sctp_release_sock(sk);
5646 *timeo_p = schedule_timeout(*timeo_p);
5647 sctp_lock_sock(sk);
5648
5649ready:
5650 finish_wait(sk->sk_sleep, &wait);
5651 return 0;
5652
5653interrupted:
5654 error = sock_intr_errno(*timeo_p);
5655
5656out:
5657 finish_wait(sk->sk_sleep, &wait);
5658 *err = error;
5659 return error;
5660}
5661
5662/* Receive a datagram.
5663 * Note: This is pretty much the same routine as in core/datagram.c
5664 * with a few changes to make lksctp work.
5665 */
5666static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5667 int noblock, int *err)
5668{
5669 int error;
5670 struct sk_buff *skb;
5671 long timeo;
5672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 timeo = sock_rcvtimeo(sk, noblock);
5674
5675 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5676 timeo, MAX_SCHEDULE_TIMEOUT);
5677
5678 do {
5679 /* Again only user level code calls this function,
5680 * so nothing interrupt level
5681 * will suddenly eat the receive_queue.
5682 *
5683 * Look at current nfs client by the way...
5684 * However, this function was corrent in any case. 8)
5685 */
5686 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005687 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 skb = skb_peek(&sk->sk_receive_queue);
5689 if (skb)
5690 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005691 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 } else {
5693 skb = skb_dequeue(&sk->sk_receive_queue);
5694 }
5695
5696 if (skb)
5697 return skb;
5698
Neil Horman6736dc32005-12-02 20:30:06 -08005699 /* Caller is allowed not to check sk->sk_err before calling. */
5700 error = sock_error(sk);
5701 if (error)
5702 goto no_packet;
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (sk->sk_shutdown & RCV_SHUTDOWN)
5705 break;
5706
5707 /* User doesn't want to wait. */
5708 error = -EAGAIN;
5709 if (!timeo)
5710 goto no_packet;
5711 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5712
5713 return NULL;
5714
5715no_packet:
5716 *err = error;
5717 return NULL;
5718}
5719
5720/* If sndbuf has changed, wake up per association sndbuf waiters. */
5721static void __sctp_write_space(struct sctp_association *asoc)
5722{
5723 struct sock *sk = asoc->base.sk;
5724 struct socket *sock = sk->sk_socket;
5725
5726 if ((sctp_wspace(asoc) > 0) && sock) {
5727 if (waitqueue_active(&asoc->wait))
5728 wake_up_interruptible(&asoc->wait);
5729
5730 if (sctp_writeable(sk)) {
5731 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5732 wake_up_interruptible(sk->sk_sleep);
5733
5734 /* Note that we try to include the Async I/O support
5735 * here by modeling from the current TCP/UDP code.
5736 * We have not tested with it yet.
5737 */
5738 if (sock->fasync_list &&
5739 !(sk->sk_shutdown & SEND_SHUTDOWN))
5740 sock_wake_async(sock, 2, POLL_OUT);
5741 }
5742 }
5743}
5744
5745/* Do accounting for the sndbuf space.
5746 * Decrement the used sndbuf space of the corresponding association by the
5747 * data size which was just transmitted(freed).
5748 */
5749static void sctp_wfree(struct sk_buff *skb)
5750{
5751 struct sctp_association *asoc;
5752 struct sctp_chunk *chunk;
5753 struct sock *sk;
5754
5755 /* Get the saved chunk pointer. */
5756 chunk = *((struct sctp_chunk **)(skb->cb));
5757 asoc = chunk->asoc;
5758 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07005759 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5760 sizeof(struct sk_buff) +
5761 sizeof(struct sctp_chunk);
5762
Neil Horman4eb701d2005-04-28 12:02:04 -07005763 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5764
5765 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 __sctp_write_space(asoc);
5767
5768 sctp_association_put(asoc);
5769}
5770
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07005771/* Do accounting for the receive space on the socket.
5772 * Accounting for the association is done in ulpevent.c
5773 * We set this as a destructor for the cloned data skbs so that
5774 * accounting is done at the correct time.
5775 */
5776void sctp_sock_rfree(struct sk_buff *skb)
5777{
5778 struct sock *sk = skb->sk;
5779 struct sctp_ulpevent *event = sctp_skb2event(skb);
5780
5781 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5782}
5783
5784
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785/* Helper function to wait for space in the sndbuf. */
5786static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5787 size_t msg_len)
5788{
5789 struct sock *sk = asoc->base.sk;
5790 int err = 0;
5791 long current_timeo = *timeo_p;
5792 DEFINE_WAIT(wait);
5793
5794 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005795 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 /* Increment the association's refcnt. */
5798 sctp_association_hold(asoc);
5799
5800 /* Wait on the association specific sndbuf space. */
5801 for (;;) {
5802 prepare_to_wait_exclusive(&asoc->wait, &wait,
5803 TASK_INTERRUPTIBLE);
5804 if (!*timeo_p)
5805 goto do_nonblock;
5806 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5807 asoc->base.dead)
5808 goto do_error;
5809 if (signal_pending(current))
5810 goto do_interrupted;
5811 if (msg_len <= sctp_wspace(asoc))
5812 break;
5813
5814 /* Let another process have a go. Since we are going
5815 * to sleep anyway.
5816 */
5817 sctp_release_sock(sk);
5818 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005819 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 sctp_lock_sock(sk);
5821
5822 *timeo_p = current_timeo;
5823 }
5824
5825out:
5826 finish_wait(&asoc->wait, &wait);
5827
5828 /* Release the association's refcnt. */
5829 sctp_association_put(asoc);
5830
5831 return err;
5832
5833do_error:
5834 err = -EPIPE;
5835 goto out;
5836
5837do_interrupted:
5838 err = sock_intr_errno(*timeo_p);
5839 goto out;
5840
5841do_nonblock:
5842 err = -EAGAIN;
5843 goto out;
5844}
5845
5846/* If socket sndbuf has changed, wake up all per association waiters. */
5847void sctp_write_space(struct sock *sk)
5848{
5849 struct sctp_association *asoc;
5850 struct list_head *pos;
5851
5852 /* Wake up the tasks in each wait queue. */
5853 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5854 asoc = list_entry(pos, struct sctp_association, asocs);
5855 __sctp_write_space(asoc);
5856 }
5857}
5858
5859/* Is there any sndbuf space available on the socket?
5860 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08005861 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 * associations on the same socket. For a UDP-style socket with
5863 * multiple associations, it is possible for it to be "unwriteable"
5864 * prematurely. I assume that this is acceptable because
5865 * a premature "unwriteable" is better than an accidental "writeable" which
5866 * would cause an unwanted block under certain circumstances. For the 1-1
5867 * UDP-style sockets or TCP-style sockets, this code should work.
5868 * - Daisy
5869 */
5870static int sctp_writeable(struct sock *sk)
5871{
5872 int amt = 0;
5873
Neil Horman9bffc4a2005-12-19 14:24:40 -08005874 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 if (amt < 0)
5876 amt = 0;
5877 return amt;
5878}
5879
5880/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5881 * returns immediately with EINPROGRESS.
5882 */
5883static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5884{
5885 struct sock *sk = asoc->base.sk;
5886 int err = 0;
5887 long current_timeo = *timeo_p;
5888 DEFINE_WAIT(wait);
5889
5890 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5891 (long)(*timeo_p));
5892
5893 /* Increment the association's refcnt. */
5894 sctp_association_hold(asoc);
5895
5896 for (;;) {
5897 prepare_to_wait_exclusive(&asoc->wait, &wait,
5898 TASK_INTERRUPTIBLE);
5899 if (!*timeo_p)
5900 goto do_nonblock;
5901 if (sk->sk_shutdown & RCV_SHUTDOWN)
5902 break;
5903 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5904 asoc->base.dead)
5905 goto do_error;
5906 if (signal_pending(current))
5907 goto do_interrupted;
5908
5909 if (sctp_state(asoc, ESTABLISHED))
5910 break;
5911
5912 /* Let another process have a go. Since we are going
5913 * to sleep anyway.
5914 */
5915 sctp_release_sock(sk);
5916 current_timeo = schedule_timeout(current_timeo);
5917 sctp_lock_sock(sk);
5918
5919 *timeo_p = current_timeo;
5920 }
5921
5922out:
5923 finish_wait(&asoc->wait, &wait);
5924
5925 /* Release the association's refcnt. */
5926 sctp_association_put(asoc);
5927
5928 return err;
5929
5930do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08005931 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 err = -ETIMEDOUT;
5933 else
5934 err = -ECONNREFUSED;
5935 goto out;
5936
5937do_interrupted:
5938 err = sock_intr_errno(*timeo_p);
5939 goto out;
5940
5941do_nonblock:
5942 err = -EINPROGRESS;
5943 goto out;
5944}
5945
5946static int sctp_wait_for_accept(struct sock *sk, long timeo)
5947{
5948 struct sctp_endpoint *ep;
5949 int err = 0;
5950 DEFINE_WAIT(wait);
5951
5952 ep = sctp_sk(sk)->ep;
5953
5954
5955 for (;;) {
5956 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5957 TASK_INTERRUPTIBLE);
5958
5959 if (list_empty(&ep->asocs)) {
5960 sctp_release_sock(sk);
5961 timeo = schedule_timeout(timeo);
5962 sctp_lock_sock(sk);
5963 }
5964
5965 err = -EINVAL;
5966 if (!sctp_sstate(sk, LISTENING))
5967 break;
5968
5969 err = 0;
5970 if (!list_empty(&ep->asocs))
5971 break;
5972
5973 err = sock_intr_errno(timeo);
5974 if (signal_pending(current))
5975 break;
5976
5977 err = -EAGAIN;
5978 if (!timeo)
5979 break;
5980 }
5981
5982 finish_wait(sk->sk_sleep, &wait);
5983
5984 return err;
5985}
5986
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02005987static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988{
5989 DEFINE_WAIT(wait);
5990
5991 do {
5992 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5993 if (list_empty(&sctp_sk(sk)->ep->asocs))
5994 break;
5995 sctp_release_sock(sk);
5996 timeout = schedule_timeout(timeout);
5997 sctp_lock_sock(sk);
5998 } while (!signal_pending(current) && timeout);
5999
6000 finish_wait(sk->sk_sleep, &wait);
6001}
6002
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006003static void sctp_sock_rfree_frag(struct sk_buff *skb)
6004{
6005 struct sk_buff *frag;
6006
6007 if (!skb->data_len)
6008 goto done;
6009
6010 /* Don't forget the fragments. */
6011 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6012 sctp_sock_rfree_frag(frag);
6013
6014done:
6015 sctp_sock_rfree(skb);
6016}
6017
6018static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6019{
6020 struct sk_buff *frag;
6021
6022 if (!skb->data_len)
6023 goto done;
6024
6025 /* Don't forget the fragments. */
6026 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6027 sctp_skb_set_owner_r_frag(frag, sk);
6028
6029done:
6030 sctp_skb_set_owner_r(skb, sk);
6031}
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033/* Populate the fields of the newsk from the oldsk and migrate the assoc
6034 * and its messages to the newsk.
6035 */
6036static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6037 struct sctp_association *assoc,
6038 sctp_socket_type_t type)
6039{
6040 struct sctp_sock *oldsp = sctp_sk(oldsk);
6041 struct sctp_sock *newsp = sctp_sk(newsk);
6042 struct sctp_bind_bucket *pp; /* hash list port iterator */
6043 struct sctp_endpoint *newep = newsp->ep;
6044 struct sk_buff *skb, *tmp;
6045 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006046 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 /* Migrate socket buffer sizes and all the socket level options to the
6049 * new socket.
6050 */
6051 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6052 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6053 /* Brute force copy old sctp opt. */
6054 inet_sk_copy_descendant(newsk, oldsk);
6055
6056 /* Restore the ep value that was overwritten with the above structure
6057 * copy.
6058 */
6059 newsp->ep = newep;
6060 newsp->hmac = NULL;
6061
6062 /* Hook this new socket in to the bind_hash list. */
6063 pp = sctp_sk(oldsk)->bind_hash;
6064 sk_add_bind_node(newsk, &pp->owner);
6065 sctp_sk(newsk)->bind_hash = pp;
6066 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6067
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006068 /* Copy the bind_addr list from the original endpoint to the new
6069 * endpoint so that we can handle restarts properly
6070 */
Vladislav Yasevicheb5fa392006-08-22 00:23:13 -07006071 if (PF_INET6 == assoc->base.sk->sk_family)
6072 flags = SCTP_ADDR6_ALLOWED;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006073 if (assoc->peer.ipv4_address)
6074 flags |= SCTP_ADDR4_PEERSUPP;
6075 if (assoc->peer.ipv6_address)
6076 flags |= SCTP_ADDR6_PEERSUPP;
6077 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6078 &oldsp->ep->base.bind_addr,
6079 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6080
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 /* Move any messages in the old socket's receive queue that are for the
6082 * peeled off association to the new socket's receive queue.
6083 */
6084 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6085 event = sctp_skb2event(skb);
6086 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006087 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006088 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006090 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 }
6092 }
6093
6094 /* Clean up any messages pending delivery due to partial
6095 * delivery. Three cases:
6096 * 1) No partial deliver; no work.
6097 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6098 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6099 */
6100 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006101 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006103 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 struct sk_buff_head *queue;
6105
6106 /* Decide which queue to move pd_lobby skbs to. */
6107 if (assoc->ulpq.pd_mode) {
6108 queue = &newsp->pd_lobby;
6109 } else
6110 queue = &newsk->sk_receive_queue;
6111
6112 /* Walk through the pd_lobby, looking for skbs that
6113 * need moved to the new socket.
6114 */
6115 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6116 event = sctp_skb2event(skb);
6117 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006118 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006119 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006121 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 }
6123 }
6124
6125 /* Clear up any skbs waiting for the partial
6126 * delivery to finish.
6127 */
6128 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006129 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
6131 }
6132
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006133 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6134 sctp_sock_rfree_frag(skb);
6135 sctp_skb_set_owner_r_frag(skb, newsk);
6136 }
6137
6138 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6139 sctp_sock_rfree_frag(skb);
6140 sctp_skb_set_owner_r_frag(skb, newsk);
6141 }
6142
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 /* Set the type of socket to indicate that it is peeled off from the
6144 * original UDP-style socket or created with the accept() call on a
6145 * TCP-style socket..
6146 */
6147 newsp->type = type;
6148
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006149 /* Mark the new socket "in-use" by the user so that any packets
6150 * that may arrive on the association after we've moved it are
6151 * queued to the backlog. This prevents a potential race between
6152 * backlog processing on the old socket and new-packet processing
6153 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006154 *
6155 * The caller has just allocated newsk so we can guarantee that other
6156 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006157 */
Zach Brown5131a182007-06-22 15:14:46 -07006158 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 /* If the association on the newsk is already closed before accept()
6162 * is called, set RCV_SHUTDOWN flag.
6163 */
6164 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6165 newsk->sk_shutdown |= RCV_SHUTDOWN;
6166
6167 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006168 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169}
6170
6171/* This proto struct describes the ULP interface for SCTP. */
6172struct proto sctp_prot = {
6173 .name = "SCTP",
6174 .owner = THIS_MODULE,
6175 .close = sctp_close,
6176 .connect = sctp_connect,
6177 .disconnect = sctp_disconnect,
6178 .accept = sctp_accept,
6179 .ioctl = sctp_ioctl,
6180 .init = sctp_init_sock,
6181 .destroy = sctp_destroy_sock,
6182 .shutdown = sctp_shutdown,
6183 .setsockopt = sctp_setsockopt,
6184 .getsockopt = sctp_getsockopt,
6185 .sendmsg = sctp_sendmsg,
6186 .recvmsg = sctp_recvmsg,
6187 .bind = sctp_bind,
6188 .backlog_rcv = sctp_backlog_rcv,
6189 .hash = sctp_hash,
6190 .unhash = sctp_unhash,
6191 .get_port = sctp_get_port,
6192 .obj_size = sizeof(struct sctp_sock),
6193};
6194
6195#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6196struct proto sctpv6_prot = {
6197 .name = "SCTPv6",
6198 .owner = THIS_MODULE,
6199 .close = sctp_close,
6200 .connect = sctp_connect,
6201 .disconnect = sctp_disconnect,
6202 .accept = sctp_accept,
6203 .ioctl = sctp_ioctl,
6204 .init = sctp_init_sock,
6205 .destroy = sctp_destroy_sock,
6206 .shutdown = sctp_shutdown,
6207 .setsockopt = sctp_setsockopt,
6208 .getsockopt = sctp_getsockopt,
6209 .sendmsg = sctp_sendmsg,
6210 .recvmsg = sctp_recvmsg,
6211 .bind = sctp_bind,
6212 .backlog_rcv = sctp_backlog_rcv,
6213 .hash = sctp_hash,
6214 .unhash = sctp_unhash,
6215 .get_port = sctp_get_port,
6216 .obj_size = sizeof(struct sctp6_sock),
6217};
6218#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */