blob: 710df67a67857184c43060fce807f1809411db4e [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
Neil Horman4d93df02007-08-15 16:07:44 -0700110extern struct kmem_cache *sctp_bucket_cachep;
111extern int sysctl_sctp_mem[3];
112extern int sysctl_sctp_rmem[3];
113extern int sysctl_sctp_wmem[3];
114
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200115static int sctp_memory_pressure;
116static atomic_t sctp_memory_allocated;
117static atomic_t sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700118
119static void sctp_enter_memory_pressure(void)
120{
121 sctp_memory_pressure = 1;
122}
123
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Get the sndbuf space available at the time on the association. */
126static inline int sctp_wspace(struct sctp_association *asoc)
127{
Neil Horman4d93df02007-08-15 16:07:44 -0700128 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Neil Horman4d93df02007-08-15 16:07:44 -0700130 if (asoc->ep->sndbuf_policy)
131 amt = asoc->sndbuf_used;
132 else
133 amt = atomic_read(&asoc->base.sk->sk_wmem_alloc);
134
135 if (amt >= asoc->base.sk->sk_sndbuf) {
136 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
137 amt = 0;
138 else {
139 amt = sk_stream_wspace(asoc->base.sk);
140 if (amt < 0)
141 amt = 0;
142 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700143 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700144 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return amt;
147}
148
149/* Increment the used sndbuf space count of the corresponding association by
150 * the size of the outgoing data chunk.
151 * Also, set the skb destructor for sndbuf accounting later.
152 *
153 * Since it is always 1-1 between chunk and skb, and also a new skb is always
154 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
155 * destructor in the data chunk skb for the purpose of the sndbuf space
156 * tracking.
157 */
158static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
159{
160 struct sctp_association *asoc = chunk->asoc;
161 struct sock *sk = asoc->base.sk;
162
163 /* The sndbuf space is tracked per association. */
164 sctp_association_hold(asoc);
165
Neil Horman4eb701d2005-04-28 12:02:04 -0700166 skb_set_owner_w(chunk->skb, sk);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 chunk->skb->destructor = sctp_wfree;
169 /* Save the chunk pointer in skb for sctp_wfree to use later. */
170 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
171
Neil Horman4eb701d2005-04-28 12:02:04 -0700172 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
173 sizeof(struct sk_buff) +
174 sizeof(struct sctp_chunk);
175
Neil Horman4eb701d2005-04-28 12:02:04 -0700176 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800177 sk->sk_wmem_queued += chunk->skb->truesize;
178 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/* Verify that this is a valid address. */
182static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
183 int len)
184{
185 struct sctp_af *af;
186
187 /* Verify basic sockaddr. */
188 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
189 if (!af)
190 return -EINVAL;
191
192 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700193 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EINVAL;
195
196 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
197 return -EINVAL;
198
199 return 0;
200}
201
202/* Look up the association by its id. If this is not a UDP-style
203 * socket, the ID field is always ignored.
204 */
205struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
206{
207 struct sctp_association *asoc = NULL;
208
209 /* If this is not a UDP-style socket, assoc id should be ignored. */
210 if (!sctp_style(sk, UDP)) {
211 /* Return NULL if the socket state is not ESTABLISHED. It
212 * could be a TCP-style listening socket or a socket which
213 * hasn't yet called connect() to establish an association.
214 */
215 if (!sctp_sstate(sk, ESTABLISHED))
216 return NULL;
217
218 /* Get the first and the only association from the list. */
219 if (!list_empty(&sctp_sk(sk)->ep->asocs))
220 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
221 struct sctp_association, asocs);
222 return asoc;
223 }
224
225 /* Otherwise this is a UDP-style socket. */
226 if (!id || (id == (sctp_assoc_t)-1))
227 return NULL;
228
229 spin_lock_bh(&sctp_assocs_id_lock);
230 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
231 spin_unlock_bh(&sctp_assocs_id_lock);
232
233 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
234 return NULL;
235
236 return asoc;
237}
238
239/* Look up the transport from an address and an assoc id. If both address and
240 * id are specified, the associations matching the address and the id should be
241 * the same.
242 */
243static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
244 struct sockaddr_storage *addr,
245 sctp_assoc_t id)
246{
247 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
248 struct sctp_transport *transport;
249 union sctp_addr *laddr = (union sctp_addr *)addr;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800252 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!addr_asoc)
256 return NULL;
257
258 id_asoc = sctp_id2assoc(sk, id);
259 if (id_asoc && (id_asoc != addr_asoc))
260 return NULL;
261
262 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
263 (union sctp_addr *)addr);
264
265 return transport;
266}
267
268/* API 3.1.2 bind() - UDP Style Syntax
269 * The syntax of bind() is,
270 *
271 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
272 *
273 * sd - the socket descriptor returned by socket().
274 * addr - the address structure (struct sockaddr_in or struct
275 * sockaddr_in6 [RFC 2553]),
276 * addr_len - the size of the address structure.
277 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700278SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int retval = 0;
281
282 sctp_lock_sock(sk);
283
Frank Filz3f7a87d2005-06-20 13:14:57 -0700284 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
285 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* Disallow binding twice. */
288 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700289 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 addr_len);
291 else
292 retval = -EINVAL;
293
294 sctp_release_sock(sk);
295
296 return retval;
297}
298
299static long sctp_get_port_local(struct sock *, union sctp_addr *);
300
301/* Verify this is a valid sockaddr. */
302static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
303 union sctp_addr *addr, int len)
304{
305 struct sctp_af *af;
306
307 /* Check minimum size. */
308 if (len < sizeof (struct sockaddr))
309 return NULL;
310
311 /* Does this PF support this AF? */
312 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
313 return NULL;
314
315 /* If we get this far, af is valid. */
316 af = sctp_get_af_specific(addr->sa.sa_family);
317
318 if (len < af->sockaddr_len)
319 return NULL;
320
321 return af;
322}
323
324/* Bind a local address either to an endpoint or to an association. */
325SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
326{
327 struct sctp_sock *sp = sctp_sk(sk);
328 struct sctp_endpoint *ep = sp->ep;
329 struct sctp_bind_addr *bp = &ep->base.bind_addr;
330 struct sctp_af *af;
331 unsigned short snum;
332 int ret = 0;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* Common sockaddr verification. */
335 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700336 if (!af) {
337 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
338 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700340 }
341
342 snum = ntohs(addr->v4.sin_port);
343
344 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
345 ", port: %d, new port: %d, len: %d)\n",
346 sk,
347 addr,
348 bp->port, snum,
349 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* PF specific bind() address verification. */
352 if (!sp->pf->bind_verify(sp, addr))
353 return -EADDRNOTAVAIL;
354
Vlad Yasevich8b358052007-05-15 17:14:58 -0400355 /* We must either be unbound, or bind to the same port.
356 * It's OK to allow 0 ports if we are already bound.
357 * We'll just inhert an already bound port in this case
358 */
359 if (bp->port) {
360 if (!snum)
361 snum = bp->port;
362 else if (snum != bp->port) {
363 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 " New port %d does not match existing port "
365 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400366 return -EINVAL;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
370 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
371 return -EACCES;
372
373 /* Make sure we are allowed to bind here.
374 * The function sctp_get_port_local() does duplicate address
375 * detection.
376 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900377 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if ((ret = sctp_get_port_local(sk, addr))) {
379 if (ret == (long) sk) {
380 /* This endpoint has a conflicting address. */
381 return -EINVAL;
382 } else {
383 return -EADDRINUSE;
384 }
385 }
386
387 /* Refresh ephemeral port. */
388 if (!bp->port)
389 bp->port = inet_sk(sk)->num;
390
Vlad Yasevich559cf712007-09-16 16:03:28 -0700391 /* Add the address to the bind address list.
392 * Use GFP_ATOMIC since BHs will be disabled.
393 */
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800394 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Copy back into socket for getsockname() use. */
397 if (!ret) {
398 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
399 af->to_sk_saddr(addr, sk);
400 }
401
402 return ret;
403}
404
405 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
406 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900407 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900409 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900411 * subsequent ASCONF. Note this restriction binds each side, so at any
412 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * from each endpoint).
414 */
415static int sctp_send_asconf(struct sctp_association *asoc,
416 struct sctp_chunk *chunk)
417{
418 int retval = 0;
419
420 /* If there is an outstanding ASCONF chunk, queue it for later
421 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700424 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 /* Hold the chunk until an ASCONF_ACK is received. */
429 sctp_chunk_hold(chunk);
430 retval = sctp_primitive_ASCONF(asoc, chunk);
431 if (retval)
432 sctp_chunk_free(chunk);
433 else
434 asoc->addip_last_asconf = chunk;
435
436out:
437 return retval;
438}
439
440/* Add a list of addresses as bind addresses to local endpoint or
441 * association.
442 *
443 * Basically run through each address specified in the addrs/addrcnt
444 * array/length pair, determine if it is IPv6 or IPv4 and call
445 * sctp_do_bind() on it.
446 *
447 * If any of them fails, then the operation will be reversed and the
448 * ones that were added will be removed.
449 *
450 * Only sctp_setsockopt_bindx() is supposed to call this function.
451 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200452static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 int cnt;
455 int retval = 0;
456 void *addr_buf;
457 struct sockaddr *sa_addr;
458 struct sctp_af *af;
459
460 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
461 sk, addrs, addrcnt);
462
463 addr_buf = addrs;
464 for (cnt = 0; cnt < addrcnt; cnt++) {
465 /* The list may contain either IPv4 or IPv6 address;
466 * determine the address length for walking thru the list.
467 */
468 sa_addr = (struct sockaddr *)addr_buf;
469 af = sctp_get_af_specific(sa_addr->sa_family);
470 if (!af) {
471 retval = -EINVAL;
472 goto err_bindx_add;
473 }
474
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900475 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 af->sockaddr_len);
477
478 addr_buf += af->sockaddr_len;
479
480err_bindx_add:
481 if (retval < 0) {
482 /* Failed. Cleanup the ones that have been added */
483 if (cnt > 0)
484 sctp_bindx_rem(sk, addrs, cnt);
485 return retval;
486 }
487 }
488
489 return retval;
490}
491
492/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
493 * associations that are part of the endpoint indicating that a list of local
494 * addresses are added to the endpoint.
495 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900496 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * association, we do not send the chunk for that association. But it will not
498 * affect other associations.
499 *
500 * Only sctp_setsockopt_bindx() is supposed to call this function.
501 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900502static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct sockaddr *addrs,
504 int addrcnt)
505{
506 struct sctp_sock *sp;
507 struct sctp_endpoint *ep;
508 struct sctp_association *asoc;
509 struct sctp_bind_addr *bp;
510 struct sctp_chunk *chunk;
511 struct sctp_sockaddr_entry *laddr;
512 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700513 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 void *addr_buf;
515 struct sctp_af *af;
516 struct list_head *pos;
517 struct list_head *p;
518 int i;
519 int retval = 0;
520
521 if (!sctp_addip_enable)
522 return retval;
523
524 sp = sctp_sk(sk);
525 ep = sp->ep;
526
527 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
528 __FUNCTION__, sk, addrs, addrcnt);
529
530 list_for_each(pos, &ep->asocs) {
531 asoc = list_entry(pos, struct sctp_association, asocs);
532
533 if (!asoc->peer.asconf_capable)
534 continue;
535
536 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
537 continue;
538
539 if (!sctp_state(asoc, ESTABLISHED))
540 continue;
541
542 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900543 * in the bind address list of the association. If so,
544 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * other associations.
546 */
547 addr_buf = addrs;
548 for (i = 0; i < addrcnt; i++) {
549 addr = (union sctp_addr *)addr_buf;
550 af = sctp_get_af_specific(addr->v4.sin_family);
551 if (!af) {
552 retval = -EINVAL;
553 goto out;
554 }
555
556 if (sctp_assoc_lookup_laddr(asoc, addr))
557 break;
558
559 addr_buf += af->sockaddr_len;
560 }
561 if (i < addrcnt)
562 continue;
563
Vlad Yasevich559cf712007-09-16 16:03:28 -0700564 /* Use the first valid address in bind addr list of
565 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 bp = &asoc->base.bind_addr;
568 p = bp->address_list.next;
569 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800570 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 addrcnt, SCTP_PARAM_ADD_IP);
572 if (!chunk) {
573 retval = -ENOMEM;
574 goto out;
575 }
576
577 retval = sctp_send_asconf(asoc, chunk);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700578 if (retval)
579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700581 /* Add the new addresses to the bind address list with
582 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700584 addr_buf = addrs;
585 for (i = 0; i < addrcnt; i++) {
586 addr = (union sctp_addr *)addr_buf;
587 af = sctp_get_af_specific(addr->v4.sin_family);
588 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800589 retval = sctp_add_bind_addr(bp, &saveaddr,
590 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700591 addr_buf += af->sockaddr_len;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595out:
596 return retval;
597}
598
599/* Remove a list of addresses from bind addresses list. Do not remove the
600 * last address.
601 *
602 * Basically run through each address specified in the addrs/addrcnt
603 * array/length pair, determine if it is IPv6 or IPv4 and call
604 * sctp_del_bind() on it.
605 *
606 * If any of them fails, then the operation will be reversed and the
607 * ones that were removed will be added back.
608 *
609 * At least one address has to be left; if only one address is
610 * available, the operation will return -EBUSY.
611 *
612 * Only sctp_setsockopt_bindx() is supposed to call this function.
613 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200614static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct sctp_sock *sp = sctp_sk(sk);
617 struct sctp_endpoint *ep = sp->ep;
618 int cnt;
619 struct sctp_bind_addr *bp = &ep->base.bind_addr;
620 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800622 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct sctp_af *af;
624
625 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
626 sk, addrs, addrcnt);
627
628 addr_buf = addrs;
629 for (cnt = 0; cnt < addrcnt; cnt++) {
630 /* If the bind address list is empty or if there is only one
631 * bind address, there is nothing more to be removed (we need
632 * at least one address here).
633 */
634 if (list_empty(&bp->address_list) ||
635 (sctp_list_single_entry(&bp->address_list))) {
636 retval = -EBUSY;
637 goto err_bindx_rem;
638 }
639
Al Viroc9a08502006-11-20 17:07:48 -0800640 sa_addr = (union sctp_addr *)addr_buf;
641 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!af) {
643 retval = -EINVAL;
644 goto err_bindx_rem;
645 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700646
647 if (!af->addr_valid(sa_addr, sp, NULL)) {
648 retval = -EADDRNOTAVAIL;
649 goto err_bindx_rem;
650 }
651
Al Viroc9a08502006-11-20 17:07:48 -0800652 if (sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 retval = -EINVAL;
654 goto err_bindx_rem;
655 }
656
657 /* FIXME - There is probably a need to check if sk->sk_saddr and
658 * sk->sk_rcv_addr are currently set to one of the addresses to
659 * be removed. This is something which needs to be looked into
660 * when we are fixing the outstanding issues with multi-homing
661 * socket routing and failover schemes. Refer to comments in
662 * sctp_do_bind(). -daisy
663 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400664 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 addr_buf += af->sockaddr_len;
667err_bindx_rem:
668 if (retval < 0) {
669 /* Failed. Add the ones that has been removed back */
670 if (cnt > 0)
671 sctp_bindx_add(sk, addrs, cnt);
672 return retval;
673 }
674 }
675
676 return retval;
677}
678
679/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
680 * the associations that are part of the endpoint indicating that a list of
681 * local addresses are removed from the endpoint.
682 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900683 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * association, we do not send the chunk for that association. But it will not
685 * affect other associations.
686 *
687 * Only sctp_setsockopt_bindx() is supposed to call this function.
688 */
689static int sctp_send_asconf_del_ip(struct sock *sk,
690 struct sockaddr *addrs,
691 int addrcnt)
692{
693 struct sctp_sock *sp;
694 struct sctp_endpoint *ep;
695 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700696 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct sctp_bind_addr *bp;
698 struct sctp_chunk *chunk;
699 union sctp_addr *laddr;
700 void *addr_buf;
701 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700702 struct list_head *pos, *pos1;
703 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int i;
705 int retval = 0;
706
707 if (!sctp_addip_enable)
708 return retval;
709
710 sp = sctp_sk(sk);
711 ep = sp->ep;
712
713 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
714 __FUNCTION__, sk, addrs, addrcnt);
715
716 list_for_each(pos, &ep->asocs) {
717 asoc = list_entry(pos, struct sctp_association, asocs);
718
719 if (!asoc->peer.asconf_capable)
720 continue;
721
722 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
723 continue;
724
725 if (!sctp_state(asoc, ESTABLISHED))
726 continue;
727
728 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900729 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * If so, do not send the asconf chunk to its peer, but
731 * continue with other associations.
732 */
733 addr_buf = addrs;
734 for (i = 0; i < addrcnt; i++) {
735 laddr = (union sctp_addr *)addr_buf;
736 af = sctp_get_af_specific(laddr->v4.sin_family);
737 if (!af) {
738 retval = -EINVAL;
739 goto out;
740 }
741
742 if (!sctp_assoc_lookup_laddr(asoc, laddr))
743 break;
744
745 addr_buf += af->sockaddr_len;
746 }
747 if (i < addrcnt)
748 continue;
749
750 /* Find one address in the association's bind address list
751 * that is not in the packed array of addresses. This is to
752 * make sure that we do not delete all the addresses in the
753 * association.
754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 bp = &asoc->base.bind_addr;
756 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
757 addrcnt, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!laddr)
759 continue;
760
Vlad Yasevich559cf712007-09-16 16:03:28 -0700761 /* We do not need RCU protection throughout this loop
762 * because this is done under a socket lock from the
763 * setsockopt call.
764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
766 SCTP_PARAM_DEL_IP);
767 if (!chunk) {
768 retval = -ENOMEM;
769 goto out;
770 }
771
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700772 /* Reset use_as_src flag for the addresses in the bind address
773 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700775 addr_buf = addrs;
776 for (i = 0; i < addrcnt; i++) {
777 laddr = (union sctp_addr *)addr_buf;
778 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700779 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800780 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800781 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700782 }
783 addr_buf += af->sockaddr_len;
784 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700785
786 /* Update the route and saddr entries for all the transports
787 * as some of the addresses in the bind address list are
788 * about to be deleted and cannot be used as source addresses.
789 */
790 list_for_each(pos1, &asoc->peer.transport_addr_list) {
791 transport = list_entry(pos1, struct sctp_transport,
792 transports);
793 dst_release(transport->dst);
794 sctp_transport_route(transport, NULL,
795 sctp_sk(asoc->base.sk));
796 }
797
798 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800out:
801 return retval;
802}
803
804/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
805 *
806 * API 8.1
807 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
808 * int flags);
809 *
810 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
811 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
812 * or IPv6 addresses.
813 *
814 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
815 * Section 3.1.2 for this usage.
816 *
817 * addrs is a pointer to an array of one or more socket addresses. Each
818 * address is contained in its appropriate structure (i.e. struct
819 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700820 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * representation is termed a "packed array" of addresses). The caller
822 * specifies the number of addresses in the array with addrcnt.
823 *
824 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
825 * -1, and sets errno to the appropriate error code.
826 *
827 * For SCTP, the port given in each socket address must be the same, or
828 * sctp_bindx() will fail, setting errno to EINVAL.
829 *
830 * The flags parameter is formed from the bitwise OR of zero or more of
831 * the following currently defined flags:
832 *
833 * SCTP_BINDX_ADD_ADDR
834 *
835 * SCTP_BINDX_REM_ADDR
836 *
837 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
838 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
839 * addresses from the association. The two flags are mutually exclusive;
840 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
841 * not remove all addresses from an association; sctp_bindx() will
842 * reject such an attempt with EINVAL.
843 *
844 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
845 * additional addresses with an endpoint after calling bind(). Or use
846 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
847 * socket is associated with so that no new association accepted will be
848 * associated with those addresses. If the endpoint supports dynamic
849 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
850 * endpoint to send the appropriate message to the peer to change the
851 * peers address lists.
852 *
853 * Adding and removing addresses from a connected association is
854 * optional functionality. Implementations that do not support this
855 * functionality should return EOPNOTSUPP.
856 *
857 * Basically do nothing but copying the addresses from user to kernel
858 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700859 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
860 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 *
862 * We don't use copy_from_user() for optimization: we first do the
863 * sanity checks (buffer size -fast- and access check-healthy
864 * pointer); if all of those succeed, then we can alloc the memory
865 * (expensive operation) needed to copy the data to kernel. Then we do
866 * the copying without checking the user space area
867 * (__copy_from_user()).
868 *
869 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
870 * it.
871 *
872 * sk The sk of the socket
873 * addrs The pointer to the addresses in user land
874 * addrssize Size of the addrs buffer
875 * op Operation to perform (add or remove, see the flags of
876 * sctp_bindx)
877 *
878 * Returns 0 if ok, <0 errno code on error.
879 */
880SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
881 struct sockaddr __user *addrs,
882 int addrs_size, int op)
883{
884 struct sockaddr *kaddrs;
885 int err;
886 int addrcnt = 0;
887 int walk_size = 0;
888 struct sockaddr *sa_addr;
889 void *addr_buf;
890 struct sctp_af *af;
891
892 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
893 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
894
895 if (unlikely(addrs_size <= 0))
896 return -EINVAL;
897
898 /* Check the user passed a healthy pointer. */
899 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
900 return -EFAULT;
901
902 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800903 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (unlikely(!kaddrs))
905 return -ENOMEM;
906
907 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
908 kfree(kaddrs);
909 return -EFAULT;
910 }
911
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900912 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 addr_buf = kaddrs;
914 while (walk_size < addrs_size) {
915 sa_addr = (struct sockaddr *)addr_buf;
916 af = sctp_get_af_specific(sa_addr->sa_family);
917
918 /* If the address family is not supported or if this address
919 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
922 kfree(kaddrs);
923 return -EINVAL;
924 }
925 addrcnt++;
926 addr_buf += af->sockaddr_len;
927 walk_size += af->sockaddr_len;
928 }
929
930 /* Do the work. */
931 switch (op) {
932 case SCTP_BINDX_ADD_ADDR:
933 err = sctp_bindx_add(sk, kaddrs, addrcnt);
934 if (err)
935 goto out;
936 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
937 break;
938
939 case SCTP_BINDX_REM_ADDR:
940 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
941 if (err)
942 goto out;
943 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
944 break;
945
946 default:
947 err = -EINVAL;
948 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951out:
952 kfree(kaddrs);
953
954 return err;
955}
956
Frank Filz3f7a87d2005-06-20 13:14:57 -0700957/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
958 *
959 * Common routine for handling connect() and sctp_connectx().
960 * Connect will come in with just a single address.
961 */
962static int __sctp_connect(struct sock* sk,
963 struct sockaddr *kaddrs,
964 int addrs_size)
965{
966 struct sctp_sock *sp;
967 struct sctp_endpoint *ep;
968 struct sctp_association *asoc = NULL;
969 struct sctp_association *asoc2;
970 struct sctp_transport *transport;
971 union sctp_addr to;
972 struct sctp_af *af;
973 sctp_scope_t scope;
974 long timeo;
975 int err = 0;
976 int addrcnt = 0;
977 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -0400978 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700979 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -0700980 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -0400981 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700982
983 sp = sctp_sk(sk);
984 ep = sp->ep;
985
986 /* connect() cannot be done on a socket that is already in ESTABLISHED
987 * state - UDP-style peeled off socket or a TCP-style socket that
988 * is already connected.
989 * It cannot be done even on a TCP-style listening socket.
990 */
991 if (sctp_sstate(sk, ESTABLISHED) ||
992 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
993 err = -EISCONN;
994 goto out_free;
995 }
996
997 /* Walk through the addrs buffer and count the number of addresses. */
998 addr_buf = kaddrs;
999 while (walk_size < addrs_size) {
Al Viro4bdf4b52006-11-20 17:10:20 -08001000 sa_addr = (union sctp_addr *)addr_buf;
1001 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001002 port = ntohs(sa_addr->v4.sin_port);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001003
1004 /* If the address family is not supported or if this address
1005 * causes the address buffer to overflow return EINVAL.
1006 */
1007 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1008 err = -EINVAL;
1009 goto out_free;
1010 }
1011
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001012 /* Save current address so we can work with it */
1013 memcpy(&to, sa_addr, af->sockaddr_len);
1014
1015 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001016 if (err)
1017 goto out_free;
1018
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001019 /* Make sure the destination port is correctly set
1020 * in all addresses.
1021 */
1022 if (asoc && asoc->peer.port && asoc->peer.port != port)
1023 goto out_free;
1024
Frank Filz3f7a87d2005-06-20 13:14:57 -07001025
1026 /* Check if there already is a matching association on the
1027 * endpoint (other than the one created here).
1028 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001029 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001030 if (asoc2 && asoc2 != asoc) {
1031 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1032 err = -EISCONN;
1033 else
1034 err = -EALREADY;
1035 goto out_free;
1036 }
1037
1038 /* If we could not find a matching association on the endpoint,
1039 * make sure that there is no peeled-off association matching
1040 * the peer address even on another socket.
1041 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001042 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001043 err = -EADDRNOTAVAIL;
1044 goto out_free;
1045 }
1046
1047 if (!asoc) {
1048 /* If a bind() or sctp_bindx() is not called prior to
1049 * an sctp_connectx() call, the system picks an
1050 * ephemeral port and will choose an address set
1051 * equivalent to binding with a wildcard address.
1052 */
1053 if (!ep->base.bind_addr.port) {
1054 if (sctp_autobind(sk)) {
1055 err = -EAGAIN;
1056 goto out_free;
1057 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001058 } else {
1059 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001060 * If an unprivileged user inherits a 1-many
1061 * style socket with open associations on a
1062 * privileged port, it MAY be permitted to
1063 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001064 * be permitted to open new associations.
1065 */
1066 if (ep->base.bind_addr.port < PROT_SOCK &&
1067 !capable(CAP_NET_BIND_SERVICE)) {
1068 err = -EACCES;
1069 goto out_free;
1070 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001071 }
1072
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001073 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001074 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1075 if (!asoc) {
1076 err = -ENOMEM;
1077 goto out_free;
1078 }
1079 }
1080
1081 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001082 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001083 SCTP_UNKNOWN);
1084 if (!transport) {
1085 err = -ENOMEM;
1086 goto out_free;
1087 }
1088
1089 addrcnt++;
1090 addr_buf += af->sockaddr_len;
1091 walk_size += af->sockaddr_len;
1092 }
1093
1094 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1095 if (err < 0) {
1096 goto out_free;
1097 }
1098
1099 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1100 if (err < 0) {
1101 goto out_free;
1102 }
1103
1104 /* Initialize sk's dport and daddr for getpeername() */
1105 inet_sk(sk)->dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001106 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1107 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001108 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001109
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001110 /* in-kernel sockets don't generally have a file allocated to them
1111 * if all they do is call sock_create_kern().
1112 */
1113 if (sk->sk_socket->file)
1114 f_flags = sk->sk_socket->file->f_flags;
1115
1116 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1117
Frank Filz3f7a87d2005-06-20 13:14:57 -07001118 err = sctp_wait_for_connect(asoc, &timeo);
1119
1120 /* Don't free association on exit. */
1121 asoc = NULL;
1122
1123out_free:
1124
1125 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001126 " kaddrs: %p err: %d\n",
1127 asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001128 if (asoc)
1129 sctp_association_free(asoc);
1130 return err;
1131}
1132
1133/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1134 *
1135 * API 8.9
1136 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1137 *
1138 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1139 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1140 * or IPv6 addresses.
1141 *
1142 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1143 * Section 3.1.2 for this usage.
1144 *
1145 * addrs is a pointer to an array of one or more socket addresses. Each
1146 * address is contained in its appropriate structure (i.e. struct
1147 * sockaddr_in or struct sockaddr_in6) the family of the address type
1148 * must be used to distengish the address length (note that this
1149 * representation is termed a "packed array" of addresses). The caller
1150 * specifies the number of addresses in the array with addrcnt.
1151 *
1152 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1153 * -1, and sets errno to the appropriate error code.
1154 *
1155 * For SCTP, the port given in each socket address must be the same, or
1156 * sctp_connectx() will fail, setting errno to EINVAL.
1157 *
1158 * An application can use sctp_connectx to initiate an association with
1159 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1160 * allows a caller to specify multiple addresses at which a peer can be
1161 * reached. The way the SCTP stack uses the list of addresses to set up
1162 * the association is implementation dependant. This function only
1163 * specifies that the stack will try to make use of all the addresses in
1164 * the list when needed.
1165 *
1166 * Note that the list of addresses passed in is only used for setting up
1167 * the association. It does not necessarily equal the set of addresses
1168 * the peer uses for the resulting association. If the caller wants to
1169 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1170 * retrieve them after the association has been set up.
1171 *
1172 * Basically do nothing but copying the addresses from user to kernel
1173 * land and invoking either sctp_connectx(). This is used for tunneling
1174 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1175 *
1176 * We don't use copy_from_user() for optimization: we first do the
1177 * sanity checks (buffer size -fast- and access check-healthy
1178 * pointer); if all of those succeed, then we can alloc the memory
1179 * (expensive operation) needed to copy the data to kernel. Then we do
1180 * the copying without checking the user space area
1181 * (__copy_from_user()).
1182 *
1183 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1184 * it.
1185 *
1186 * sk The sk of the socket
1187 * addrs The pointer to the addresses in user land
1188 * addrssize Size of the addrs buffer
1189 *
1190 * Returns 0 if ok, <0 errno code on error.
1191 */
1192SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1193 struct sockaddr __user *addrs,
1194 int addrs_size)
1195{
1196 int err = 0;
1197 struct sockaddr *kaddrs;
1198
1199 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1200 __FUNCTION__, sk, addrs, addrs_size);
1201
1202 if (unlikely(addrs_size <= 0))
1203 return -EINVAL;
1204
1205 /* Check the user passed a healthy pointer. */
1206 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1207 return -EFAULT;
1208
1209 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001210 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001211 if (unlikely(!kaddrs))
1212 return -ENOMEM;
1213
1214 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1215 err = -EFAULT;
1216 } else {
1217 err = __sctp_connect(sk, kaddrs, addrs_size);
1218 }
1219
1220 kfree(kaddrs);
1221 return err;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/* API 3.1.4 close() - UDP Style Syntax
1225 * Applications use close() to perform graceful shutdown (as described in
1226 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1227 * by a UDP-style socket.
1228 *
1229 * The syntax is
1230 *
1231 * ret = close(int sd);
1232 *
1233 * sd - the socket descriptor of the associations to be closed.
1234 *
1235 * To gracefully shutdown a specific association represented by the
1236 * UDP-style socket, an application should use the sendmsg() call,
1237 * passing no user data, but including the appropriate flag in the
1238 * ancillary data (see Section xxxx).
1239 *
1240 * If sd in the close() call is a branched-off socket representing only
1241 * one association, the shutdown is performed on that association only.
1242 *
1243 * 4.1.6 close() - TCP Style Syntax
1244 *
1245 * Applications use close() to gracefully close down an association.
1246 *
1247 * The syntax is:
1248 *
1249 * int close(int sd);
1250 *
1251 * sd - the socket descriptor of the association to be closed.
1252 *
1253 * After an application calls close() on a socket descriptor, no further
1254 * socket operations will succeed on that descriptor.
1255 *
1256 * API 7.1.4 SO_LINGER
1257 *
1258 * An application using the TCP-style socket can use this option to
1259 * perform the SCTP ABORT primitive. The linger option structure is:
1260 *
1261 * struct linger {
1262 * int l_onoff; // option on/off
1263 * int l_linger; // linger time
1264 * };
1265 *
1266 * To enable the option, set l_onoff to 1. If the l_linger value is set
1267 * to 0, calling close() is the same as the ABORT primitive. If the
1268 * value is set to a negative value, the setsockopt() call will return
1269 * an error. If the value is set to a positive value linger_time, the
1270 * close() can be blocked for at most linger_time ms. If the graceful
1271 * shutdown phase does not finish during this period, close() will
1272 * return but the graceful shutdown phase continues in the system.
1273 */
1274SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1275{
1276 struct sctp_endpoint *ep;
1277 struct sctp_association *asoc;
1278 struct list_head *pos, *temp;
1279
1280 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1281
1282 sctp_lock_sock(sk);
1283 sk->sk_shutdown = SHUTDOWN_MASK;
1284
1285 ep = sctp_sk(sk)->ep;
1286
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001287 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 list_for_each_safe(pos, temp, &ep->asocs) {
1289 asoc = list_entry(pos, struct sctp_association, asocs);
1290
1291 if (sctp_style(sk, TCP)) {
1292 /* A closed association can still be in the list if
1293 * it belongs to a TCP-style listening socket that is
1294 * not yet accepted. If so, free it. If not, send an
1295 * ABORT or SHUTDOWN based on the linger options.
1296 */
1297 if (sctp_state(asoc, CLOSED)) {
1298 sctp_unhash_established(asoc);
1299 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001300 continue;
1301 }
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001304 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1305 struct sctp_chunk *chunk;
1306
1307 chunk = sctp_make_abort_user(asoc, NULL, 0);
1308 if (chunk)
1309 sctp_primitive_ABORT(asoc, chunk);
1310 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sctp_primitive_SHUTDOWN(asoc, NULL);
1312 }
1313
1314 /* Clean up any skbs sitting on the receive queue. */
1315 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1316 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1317
1318 /* On a TCP-style socket, block for at most linger_time if set. */
1319 if (sctp_style(sk, TCP) && timeout)
1320 sctp_wait_for_close(sk, timeout);
1321
1322 /* This will run the backlog queue. */
1323 sctp_release_sock(sk);
1324
1325 /* Supposedly, no process has access to the socket, but
1326 * the net layers still may.
1327 */
1328 sctp_local_bh_disable();
1329 sctp_bh_lock_sock(sk);
1330
1331 /* Hold the sock, since sk_common_release() will put sock_put()
1332 * and we have just a little more cleanup.
1333 */
1334 sock_hold(sk);
1335 sk_common_release(sk);
1336
1337 sctp_bh_unlock_sock(sk);
1338 sctp_local_bh_enable();
1339
1340 sock_put(sk);
1341
1342 SCTP_DBG_OBJCNT_DEC(sock);
1343}
1344
1345/* Handle EPIPE error. */
1346static int sctp_error(struct sock *sk, int flags, int err)
1347{
1348 if (err == -EPIPE)
1349 err = sock_error(sk) ? : -EPIPE;
1350 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1351 send_sig(SIGPIPE, current, 0);
1352 return err;
1353}
1354
1355/* API 3.1.3 sendmsg() - UDP Style Syntax
1356 *
1357 * An application uses sendmsg() and recvmsg() calls to transmit data to
1358 * and receive data from its peer.
1359 *
1360 * ssize_t sendmsg(int socket, const struct msghdr *message,
1361 * int flags);
1362 *
1363 * socket - the socket descriptor of the endpoint.
1364 * message - pointer to the msghdr structure which contains a single
1365 * user message and possibly some ancillary data.
1366 *
1367 * See Section 5 for complete description of the data
1368 * structures.
1369 *
1370 * flags - flags sent or received with the user message, see Section
1371 * 5 for complete description of the flags.
1372 *
1373 * Note: This function could use a rewrite especially when explicit
1374 * connect support comes in.
1375 */
1376/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1377
1378SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1379
1380SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1381 struct msghdr *msg, size_t msg_len)
1382{
1383 struct sctp_sock *sp;
1384 struct sctp_endpoint *ep;
1385 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1386 struct sctp_transport *transport, *chunk_tp;
1387 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001388 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct sockaddr *msg_name = NULL;
1390 struct sctp_sndrcvinfo default_sinfo = { 0 };
1391 struct sctp_sndrcvinfo *sinfo;
1392 struct sctp_initmsg *sinit;
1393 sctp_assoc_t associd = 0;
1394 sctp_cmsgs_t cmsgs = { NULL };
1395 int err;
1396 sctp_scope_t scope;
1397 long timeo;
1398 __u16 sinfo_flags = 0;
1399 struct sctp_datamsg *datamsg;
1400 struct list_head *pos;
1401 int msg_flags = msg->msg_flags;
1402
1403 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1404 sk, msg, msg_len);
1405
1406 err = 0;
1407 sp = sctp_sk(sk);
1408 ep = sp->ep;
1409
Frank Filz3f7a87d2005-06-20 13:14:57 -07001410 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* We cannot send a message over a TCP-style listening socket. */
1413 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1414 err = -EPIPE;
1415 goto out_nounlock;
1416 }
1417
1418 /* Parse out the SCTP CMSGs. */
1419 err = sctp_msghdr_parse(msg, &cmsgs);
1420
1421 if (err) {
1422 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1423 goto out_nounlock;
1424 }
1425
1426 /* Fetch the destination address for this packet. This
1427 * address only selects the association--it is not necessarily
1428 * the address we will send to.
1429 * For a peeled-off socket, msg_name is ignored.
1430 */
1431 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1432 int msg_namelen = msg->msg_namelen;
1433
1434 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1435 msg_namelen);
1436 if (err)
1437 return err;
1438
1439 if (msg_namelen > sizeof(to))
1440 msg_namelen = sizeof(to);
1441 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 msg_name = msg->msg_name;
1443 }
1444
1445 sinfo = cmsgs.info;
1446 sinit = cmsgs.init;
1447
1448 /* Did the user specify SNDRCVINFO? */
1449 if (sinfo) {
1450 sinfo_flags = sinfo->sinfo_flags;
1451 associd = sinfo->sinfo_assoc_id;
1452 }
1453
1454 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1455 msg_len, sinfo_flags);
1456
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001457 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1458 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 err = -EINVAL;
1460 goto out_nounlock;
1461 }
1462
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001463 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1464 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1465 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001467 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001468 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1469 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 err = -EINVAL;
1471 goto out_nounlock;
1472 }
1473
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001474 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * specified in msg_name.
1476 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001477 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 err = -EINVAL;
1479 goto out_nounlock;
1480 }
1481
1482 transport = NULL;
1483
1484 SCTP_DEBUG_PRINTK("About to look up association.\n");
1485
1486 sctp_lock_sock(sk);
1487
1488 /* If a msg_name has been specified, assume this is to be used. */
1489 if (msg_name) {
1490 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001491 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (!asoc) {
1493 /* If we could not find a matching association on the
1494 * endpoint, make sure that it is not a TCP-style
1495 * socket that already has an association or there is
1496 * no peeled-off association on another socket.
1497 */
1498 if ((sctp_style(sk, TCP) &&
1499 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001500 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 err = -EADDRNOTAVAIL;
1502 goto out_unlock;
1503 }
1504 }
1505 } else {
1506 asoc = sctp_id2assoc(sk, associd);
1507 if (!asoc) {
1508 err = -EPIPE;
1509 goto out_unlock;
1510 }
1511 }
1512
1513 if (asoc) {
1514 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1515
1516 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1517 * socket that has an association in CLOSED state. This can
1518 * happen when an accepted socket has an association that is
1519 * already CLOSED.
1520 */
1521 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1522 err = -EPIPE;
1523 goto out_unlock;
1524 }
1525
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001526 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1528 asoc);
1529 sctp_primitive_SHUTDOWN(asoc, NULL);
1530 err = 0;
1531 goto out_unlock;
1532 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001533 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001534
1535 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1536 if (!chunk) {
1537 err = -ENOMEM;
1538 goto out_unlock;
1539 }
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001542 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 err = 0;
1544 goto out_unlock;
1545 }
1546 }
1547
1548 /* Do we need to create the association? */
1549 if (!asoc) {
1550 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1551
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001552 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 err = -EINVAL;
1554 goto out_unlock;
1555 }
1556
1557 /* Check for invalid stream against the stream counts,
1558 * either the default or the user specified stream counts.
1559 */
1560 if (sinfo) {
1561 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1562 /* Check against the defaults. */
1563 if (sinfo->sinfo_stream >=
1564 sp->initmsg.sinit_num_ostreams) {
1565 err = -EINVAL;
1566 goto out_unlock;
1567 }
1568 } else {
1569 /* Check against the requested. */
1570 if (sinfo->sinfo_stream >=
1571 sinit->sinit_num_ostreams) {
1572 err = -EINVAL;
1573 goto out_unlock;
1574 }
1575 }
1576 }
1577
1578 /*
1579 * API 3.1.2 bind() - UDP Style Syntax
1580 * If a bind() or sctp_bindx() is not called prior to a
1581 * sendmsg() call that initiates a new association, the
1582 * system picks an ephemeral port and will choose an address
1583 * set equivalent to binding with a wildcard address.
1584 */
1585 if (!ep->base.bind_addr.port) {
1586 if (sctp_autobind(sk)) {
1587 err = -EAGAIN;
1588 goto out_unlock;
1589 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001590 } else {
1591 /*
1592 * If an unprivileged user inherits a one-to-many
1593 * style socket with open associations on a privileged
1594 * port, it MAY be permitted to accept new associations,
1595 * but it SHOULD NOT be permitted to open new
1596 * associations.
1597 */
1598 if (ep->base.bind_addr.port < PROT_SOCK &&
1599 !capable(CAP_NET_BIND_SERVICE)) {
1600 err = -EACCES;
1601 goto out_unlock;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
1605 scope = sctp_scope(&to);
1606 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1607 if (!new_asoc) {
1608 err = -ENOMEM;
1609 goto out_unlock;
1610 }
1611 asoc = new_asoc;
1612
1613 /* If the SCTP_INIT ancillary data is specified, set all
1614 * the association init values accordingly.
1615 */
1616 if (sinit) {
1617 if (sinit->sinit_num_ostreams) {
1618 asoc->c.sinit_num_ostreams =
1619 sinit->sinit_num_ostreams;
1620 }
1621 if (sinit->sinit_max_instreams) {
1622 asoc->c.sinit_max_instreams =
1623 sinit->sinit_max_instreams;
1624 }
1625 if (sinit->sinit_max_attempts) {
1626 asoc->max_init_attempts
1627 = sinit->sinit_max_attempts;
1628 }
1629 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001630 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1632 }
1633 }
1634
1635 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001636 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!transport) {
1638 err = -ENOMEM;
1639 goto out_free;
1640 }
1641 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1642 if (err < 0) {
1643 err = -ENOMEM;
1644 goto out_free;
1645 }
1646 }
1647
1648 /* ASSERT: we have a valid association at this point. */
1649 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1650
1651 if (!sinfo) {
1652 /* If the user didn't specify SNDRCVINFO, make up one with
1653 * some defaults.
1654 */
1655 default_sinfo.sinfo_stream = asoc->default_stream;
1656 default_sinfo.sinfo_flags = asoc->default_flags;
1657 default_sinfo.sinfo_ppid = asoc->default_ppid;
1658 default_sinfo.sinfo_context = asoc->default_context;
1659 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1660 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1661 sinfo = &default_sinfo;
1662 }
1663
1664 /* API 7.1.7, the sndbuf size per association bounds the
1665 * maximum size of data that can be sent in a single send call.
1666 */
1667 if (msg_len > sk->sk_sndbuf) {
1668 err = -EMSGSIZE;
1669 goto out_free;
1670 }
1671
Vlad Yasevich8a479492007-06-07 14:21:05 -04001672 if (asoc->pmtu_pending)
1673 sctp_assoc_pending_pmtu(asoc);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /* If fragmentation is disabled and the message length exceeds the
1676 * association fragmentation point, return EMSGSIZE. The I-D
1677 * does not specify what this error is, but this looks like
1678 * a great fit.
1679 */
1680 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1681 err = -EMSGSIZE;
1682 goto out_free;
1683 }
1684
1685 if (sinfo) {
1686 /* Check for invalid stream. */
1687 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1688 err = -EINVAL;
1689 goto out_free;
1690 }
1691 }
1692
1693 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1694 if (!sctp_wspace(asoc)) {
1695 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1696 if (err)
1697 goto out_free;
1698 }
1699
1700 /* If an address is passed with the sendto/sendmsg call, it is used
1701 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001702 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 */
1704 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001705 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001706 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (!chunk_tp) {
1708 err = -EINVAL;
1709 goto out_free;
1710 }
1711 } else
1712 chunk_tp = NULL;
1713
1714 /* Auto-connect, if we aren't connected already. */
1715 if (sctp_state(asoc, CLOSED)) {
1716 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1717 if (err < 0)
1718 goto out_free;
1719 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1720 }
1721
1722 /* Break the message into multiple chunks of maximum size. */
1723 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1724 if (!datamsg) {
1725 err = -ENOMEM;
1726 goto out_free;
1727 }
1728
1729 /* Now send the (possibly) fragmented message. */
1730 list_for_each(pos, &datamsg->chunks) {
1731 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1732 sctp_datamsg_track(chunk);
1733
1734 /* Do accounting for the write space. */
1735 sctp_set_owner_w(chunk);
1736
1737 chunk->transport = chunk_tp;
1738
1739 /* Send it to the lower layers. Note: all chunks
1740 * must either fail or succeed. The lower layer
1741 * works that way today. Keep it that way or this
1742 * breaks.
1743 */
1744 err = sctp_primitive_SEND(asoc, chunk);
1745 /* Did the lower layer accept the chunk? */
1746 if (err)
1747 sctp_chunk_free(chunk);
1748 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1749 }
1750
1751 sctp_datamsg_free(datamsg);
1752 if (err)
1753 goto out_free;
1754 else
1755 err = msg_len;
1756
1757 /* If we are already past ASSOCIATE, the lower
1758 * layers are responsible for association cleanup.
1759 */
1760 goto out_unlock;
1761
1762out_free:
1763 if (new_asoc)
1764 sctp_association_free(asoc);
1765out_unlock:
1766 sctp_release_sock(sk);
1767
1768out_nounlock:
1769 return sctp_error(sk, msg_flags, err);
1770
1771#if 0
1772do_sock_err:
1773 if (msg_len)
1774 err = msg_len;
1775 else
1776 err = sock_error(sk);
1777 goto out;
1778
1779do_interrupted:
1780 if (msg_len)
1781 err = msg_len;
1782 goto out;
1783#endif /* 0 */
1784}
1785
1786/* This is an extended version of skb_pull() that removes the data from the
1787 * start of a skb even when data is spread across the list of skb's in the
1788 * frag_list. len specifies the total amount of data that needs to be removed.
1789 * when 'len' bytes could be removed from the skb, it returns 0.
1790 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1791 * could not be removed.
1792 */
1793static int sctp_skb_pull(struct sk_buff *skb, int len)
1794{
1795 struct sk_buff *list;
1796 int skb_len = skb_headlen(skb);
1797 int rlen;
1798
1799 if (len <= skb_len) {
1800 __skb_pull(skb, len);
1801 return 0;
1802 }
1803 len -= skb_len;
1804 __skb_pull(skb, skb_len);
1805
1806 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1807 rlen = sctp_skb_pull(list, len);
1808 skb->len -= (len-rlen);
1809 skb->data_len -= (len-rlen);
1810
1811 if (!rlen)
1812 return 0;
1813
1814 len = rlen;
1815 }
1816
1817 return len;
1818}
1819
1820/* API 3.1.3 recvmsg() - UDP Style Syntax
1821 *
1822 * ssize_t recvmsg(int socket, struct msghdr *message,
1823 * int flags);
1824 *
1825 * socket - the socket descriptor of the endpoint.
1826 * message - pointer to the msghdr structure which contains a single
1827 * user message and possibly some ancillary data.
1828 *
1829 * See Section 5 for complete description of the data
1830 * structures.
1831 *
1832 * flags - flags sent or received with the user message, see Section
1833 * 5 for complete description of the flags.
1834 */
1835static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1836
1837SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1838 struct msghdr *msg, size_t len, int noblock,
1839 int flags, int *addr_len)
1840{
1841 struct sctp_ulpevent *event = NULL;
1842 struct sctp_sock *sp = sctp_sk(sk);
1843 struct sk_buff *skb;
1844 int copied;
1845 int err = 0;
1846 int skb_len;
1847
1848 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1849 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1850 "len", len, "knoblauch", noblock,
1851 "flags", flags, "addr_len", addr_len);
1852
1853 sctp_lock_sock(sk);
1854
1855 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1856 err = -ENOTCONN;
1857 goto out;
1858 }
1859
1860 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1861 if (!skb)
1862 goto out;
1863
1864 /* Get the total length of the skb including any skb's in the
1865 * frag_list.
1866 */
1867 skb_len = skb->len;
1868
1869 copied = skb_len;
1870 if (copied > len)
1871 copied = len;
1872
1873 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1874
1875 event = sctp_skb2event(skb);
1876
1877 if (err)
1878 goto out_free;
1879
1880 sock_recv_timestamp(msg, sk, skb);
1881 if (sctp_ulpevent_is_notification(event)) {
1882 msg->msg_flags |= MSG_NOTIFICATION;
1883 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1884 } else {
1885 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1886 }
1887
1888 /* Check if we allow SCTP_SNDRCVINFO. */
1889 if (sp->subscribe.sctp_data_io_event)
1890 sctp_ulpevent_read_sndrcvinfo(event, msg);
1891#if 0
1892 /* FIXME: we should be calling IP/IPv6 layers. */
1893 if (sk->sk_protinfo.af_inet.cmsg_flags)
1894 ip_cmsg_recv(msg, skb);
1895#endif
1896
1897 err = copied;
1898
1899 /* If skb's length exceeds the user's buffer, update the skb and
1900 * push it back to the receive_queue so that the next call to
1901 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1902 */
1903 if (skb_len > copied) {
1904 msg->msg_flags &= ~MSG_EOR;
1905 if (flags & MSG_PEEK)
1906 goto out_free;
1907 sctp_skb_pull(skb, copied);
1908 skb_queue_head(&sk->sk_receive_queue, skb);
1909
1910 /* When only partial message is copied to the user, increase
1911 * rwnd by that amount. If all the data in the skb is read,
1912 * rwnd is updated when the event is freed.
1913 */
1914 sctp_assoc_rwnd_increase(event->asoc, copied);
1915 goto out;
1916 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1917 (event->msg_flags & MSG_EOR))
1918 msg->msg_flags |= MSG_EOR;
1919 else
1920 msg->msg_flags &= ~MSG_EOR;
1921
1922out_free:
1923 if (flags & MSG_PEEK) {
1924 /* Release the skb reference acquired after peeking the skb in
1925 * sctp_skb_recv_datagram().
1926 */
1927 kfree_skb(skb);
1928 } else {
1929 /* Free the event which includes releasing the reference to
1930 * the owner of the skb, freeing the skb and updating the
1931 * rwnd.
1932 */
1933 sctp_ulpevent_free(event);
1934 }
1935out:
1936 sctp_release_sock(sk);
1937 return err;
1938}
1939
1940/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1941 *
1942 * This option is a on/off flag. If enabled no SCTP message
1943 * fragmentation will be performed. Instead if a message being sent
1944 * exceeds the current PMTU size, the message will NOT be sent and
1945 * instead a error will be indicated to the user.
1946 */
1947static int sctp_setsockopt_disable_fragments(struct sock *sk,
1948 char __user *optval, int optlen)
1949{
1950 int val;
1951
1952 if (optlen < sizeof(int))
1953 return -EINVAL;
1954
1955 if (get_user(val, (int __user *)optval))
1956 return -EFAULT;
1957
1958 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1959
1960 return 0;
1961}
1962
1963static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1964 int optlen)
1965{
1966 if (optlen != sizeof(struct sctp_event_subscribe))
1967 return -EINVAL;
1968 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1969 return -EFAULT;
1970 return 0;
1971}
1972
1973/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1974 *
1975 * This socket option is applicable to the UDP-style socket only. When
1976 * set it will cause associations that are idle for more than the
1977 * specified number of seconds to automatically close. An association
1978 * being idle is defined an association that has NOT sent or received
1979 * user data. The special value of '0' indicates that no automatic
1980 * close of any associations should be performed. The option expects an
1981 * integer defining the number of seconds of idle time before an
1982 * association is closed.
1983 */
1984static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1985 int optlen)
1986{
1987 struct sctp_sock *sp = sctp_sk(sk);
1988
1989 /* Applicable to UDP-style socket only */
1990 if (sctp_style(sk, TCP))
1991 return -EOPNOTSUPP;
1992 if (optlen != sizeof(int))
1993 return -EINVAL;
1994 if (copy_from_user(&sp->autoclose, optval, optlen))
1995 return -EFAULT;
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return 0;
1998}
1999
2000/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2001 *
2002 * Applications can enable or disable heartbeats for any peer address of
2003 * an association, modify an address's heartbeat interval, force a
2004 * heartbeat to be sent immediately, and adjust the address's maximum
2005 * number of retransmissions sent before an address is considered
2006 * unreachable. The following structure is used to access and modify an
2007 * address's parameters:
2008 *
2009 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002010 * sctp_assoc_t spp_assoc_id;
2011 * struct sockaddr_storage spp_address;
2012 * uint32_t spp_hbinterval;
2013 * uint16_t spp_pathmaxrxt;
2014 * uint32_t spp_pathmtu;
2015 * uint32_t spp_sackdelay;
2016 * uint32_t spp_flags;
2017 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002019 * spp_assoc_id - (one-to-many style socket) This is filled in the
2020 * application, and identifies the association for
2021 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 * spp_address - This specifies which address is of interest.
2023 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002024 * in milliseconds. If a value of zero
2025 * is present in this field then no changes are to
2026 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * spp_pathmaxrxt - This contains the maximum number of
2028 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002029 * considered unreachable. If a value of zero
2030 * is present in this field then no changes are to
2031 * be made to this parameter.
2032 * spp_pathmtu - When Path MTU discovery is disabled the value
2033 * specified here will be the "fixed" path mtu.
2034 * Note that if the spp_address field is empty
2035 * then all associations on this address will
2036 * have this fixed path mtu set upon them.
2037 *
2038 * spp_sackdelay - When delayed sack is enabled, this value specifies
2039 * the number of milliseconds that sacks will be delayed
2040 * for. This value will apply to all addresses of an
2041 * association if the spp_address field is empty. Note
2042 * also, that if delayed sack is enabled and this
2043 * value is set to 0, no change is made to the last
2044 * recorded delayed sack timer value.
2045 *
2046 * spp_flags - These flags are used to control various features
2047 * on an association. The flag field may contain
2048 * zero or more of the following options.
2049 *
2050 * SPP_HB_ENABLE - Enable heartbeats on the
2051 * specified address. Note that if the address
2052 * field is empty all addresses for the association
2053 * have heartbeats enabled upon them.
2054 *
2055 * SPP_HB_DISABLE - Disable heartbeats on the
2056 * speicifed address. Note that if the address
2057 * field is empty all addresses for the association
2058 * will have their heartbeats disabled. Note also
2059 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2060 * mutually exclusive, only one of these two should
2061 * be specified. Enabling both fields will have
2062 * undetermined results.
2063 *
2064 * SPP_HB_DEMAND - Request a user initiated heartbeat
2065 * to be made immediately.
2066 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002067 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2068 * heartbeat delayis to be set to the value of 0
2069 * milliseconds.
2070 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002071 * SPP_PMTUD_ENABLE - This field will enable PMTU
2072 * discovery upon the specified address. Note that
2073 * if the address feild is empty then all addresses
2074 * on the association are effected.
2075 *
2076 * SPP_PMTUD_DISABLE - This field will disable PMTU
2077 * discovery upon the specified address. Note that
2078 * if the address feild is empty then all addresses
2079 * on the association are effected. Not also that
2080 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2081 * exclusive. Enabling both will have undetermined
2082 * results.
2083 *
2084 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2085 * on delayed sack. The time specified in spp_sackdelay
2086 * is used to specify the sack delay for this address. Note
2087 * that if spp_address is empty then all addresses will
2088 * enable delayed sack and take on the sack delay
2089 * value specified in spp_sackdelay.
2090 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2091 * off delayed sack. If the spp_address field is blank then
2092 * delayed sack is disabled for the entire association. Note
2093 * also that this field is mutually exclusive to
2094 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2095 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 */
Adrian Bunk16164362006-09-18 00:40:38 -07002097static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2098 struct sctp_transport *trans,
2099 struct sctp_association *asoc,
2100 struct sctp_sock *sp,
2101 int hb_change,
2102 int pmtud_change,
2103 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int error;
2106
Frank Filz52ccb8e2005-12-22 11:36:46 -08002107 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2109 if (error)
2110 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002113 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2114 * this field is ignored. Note also that a value of zero indicates
2115 * the current setting should be left unchanged.
2116 */
2117 if (params->spp_flags & SPP_HB_ENABLE) {
2118
2119 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2120 * set. This lets us use 0 value when this flag
2121 * is set.
2122 */
2123 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2124 params->spp_hbinterval = 0;
2125
2126 if (params->spp_hbinterval ||
2127 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2128 if (trans) {
2129 trans->hbinterval =
2130 msecs_to_jiffies(params->spp_hbinterval);
2131 } else if (asoc) {
2132 asoc->hbinterval =
2133 msecs_to_jiffies(params->spp_hbinterval);
2134 } else {
2135 sp->hbinterval = params->spp_hbinterval;
2136 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002137 }
2138 }
2139
2140 if (hb_change) {
2141 if (trans) {
2142 trans->param_flags =
2143 (trans->param_flags & ~SPP_HB) | hb_change;
2144 } else if (asoc) {
2145 asoc->param_flags =
2146 (asoc->param_flags & ~SPP_HB) | hb_change;
2147 } else {
2148 sp->param_flags =
2149 (sp->param_flags & ~SPP_HB) | hb_change;
2150 }
2151 }
2152
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002153 /* When Path MTU discovery is disabled the value specified here will
2154 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2155 * include the flag SPP_PMTUD_DISABLE for this field to have any
2156 * effect).
2157 */
2158 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002159 if (trans) {
2160 trans->pathmtu = params->spp_pathmtu;
2161 sctp_assoc_sync_pmtu(asoc);
2162 } else if (asoc) {
2163 asoc->pathmtu = params->spp_pathmtu;
2164 sctp_frag_point(sp, params->spp_pathmtu);
2165 } else {
2166 sp->pathmtu = params->spp_pathmtu;
2167 }
2168 }
2169
2170 if (pmtud_change) {
2171 if (trans) {
2172 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2173 (params->spp_flags & SPP_PMTUD_ENABLE);
2174 trans->param_flags =
2175 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2176 if (update) {
2177 sctp_transport_pmtu(trans);
2178 sctp_assoc_sync_pmtu(asoc);
2179 }
2180 } else if (asoc) {
2181 asoc->param_flags =
2182 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2183 } else {
2184 sp->param_flags =
2185 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2186 }
2187 }
2188
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002189 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2190 * value of this field is ignored. Note also that a value of zero
2191 * indicates the current setting should be left unchanged.
2192 */
2193 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002194 if (trans) {
2195 trans->sackdelay =
2196 msecs_to_jiffies(params->spp_sackdelay);
2197 } else if (asoc) {
2198 asoc->sackdelay =
2199 msecs_to_jiffies(params->spp_sackdelay);
2200 } else {
2201 sp->sackdelay = params->spp_sackdelay;
2202 }
2203 }
2204
2205 if (sackdelay_change) {
2206 if (trans) {
2207 trans->param_flags =
2208 (trans->param_flags & ~SPP_SACKDELAY) |
2209 sackdelay_change;
2210 } else if (asoc) {
2211 asoc->param_flags =
2212 (asoc->param_flags & ~SPP_SACKDELAY) |
2213 sackdelay_change;
2214 } else {
2215 sp->param_flags =
2216 (sp->param_flags & ~SPP_SACKDELAY) |
2217 sackdelay_change;
2218 }
2219 }
2220
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002221 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2222 * of this field is ignored. Note also that a value of zero
2223 * indicates the current setting should be left unchanged.
2224 */
2225 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002226 if (trans) {
2227 trans->pathmaxrxt = params->spp_pathmaxrxt;
2228 } else if (asoc) {
2229 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2230 } else {
2231 sp->pathmaxrxt = params->spp_pathmaxrxt;
2232 }
2233 }
2234
2235 return 0;
2236}
2237
2238static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2239 char __user *optval, int optlen)
2240{
2241 struct sctp_paddrparams params;
2242 struct sctp_transport *trans = NULL;
2243 struct sctp_association *asoc = NULL;
2244 struct sctp_sock *sp = sctp_sk(sk);
2245 int error;
2246 int hb_change, pmtud_change, sackdelay_change;
2247
2248 if (optlen != sizeof(struct sctp_paddrparams))
2249 return - EINVAL;
2250
2251 if (copy_from_user(&params, optval, optlen))
2252 return -EFAULT;
2253
2254 /* Validate flags and value parameters. */
2255 hb_change = params.spp_flags & SPP_HB;
2256 pmtud_change = params.spp_flags & SPP_PMTUD;
2257 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2258
2259 if (hb_change == SPP_HB ||
2260 pmtud_change == SPP_PMTUD ||
2261 sackdelay_change == SPP_SACKDELAY ||
2262 params.spp_sackdelay > 500 ||
2263 (params.spp_pathmtu
2264 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2265 return -EINVAL;
2266
2267 /* If an address other than INADDR_ANY is specified, and
2268 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002270 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2271 trans = sctp_addr_id2transport(sk, &params.spp_address,
2272 params.spp_assoc_id);
2273 if (!trans)
2274 return -EINVAL;
2275 }
2276
2277 /* Get association, if assoc_id != 0 and the socket is a one
2278 * to many style socket, and an association was not found, then
2279 * the id was invalid.
2280 */
2281 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2282 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2283 return -EINVAL;
2284
2285 /* Heartbeat demand can only be sent on a transport or
2286 * association, but not a socket.
2287 */
2288 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2289 return -EINVAL;
2290
2291 /* Process parameters. */
2292 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2293 hb_change, pmtud_change,
2294 sackdelay_change);
2295
2296 if (error)
2297 return error;
2298
2299 /* If changes are for association, also apply parameters to each
2300 * transport.
2301 */
2302 if (!trans && asoc) {
2303 struct list_head *pos;
2304
2305 list_for_each(pos, &asoc->peer.transport_addr_list) {
2306 trans = list_entry(pos, struct sctp_transport,
2307 transports);
2308 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2309 hb_change, pmtud_change,
2310 sackdelay_change);
2311 }
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 return 0;
2315}
2316
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002317/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08002318 *
2319 * This options will get or set the delayed ack timer. The time is set
2320 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2321 * endpoints default delayed ack timer value. If the assoc_id field is
2322 * non-zero, then the set or get effects the specified association.
2323 *
2324 * struct sctp_assoc_value {
2325 * sctp_assoc_t assoc_id;
2326 * uint32_t assoc_value;
2327 * };
2328 *
2329 * assoc_id - This parameter, indicates which association the
2330 * user is preforming an action upon. Note that if
2331 * this field's value is zero then the endpoints
2332 * default value is changed (effecting future
2333 * associations only).
2334 *
2335 * assoc_value - This parameter contains the number of milliseconds
2336 * that the user is requesting the delayed ACK timer
2337 * be set to. Note that this value is defined in
2338 * the standard to be between 200 and 500 milliseconds.
2339 *
2340 * Note: a value of zero will leave the value alone,
2341 * but disable SACK delay. A non-zero value will also
2342 * enable SACK delay.
2343 */
2344
2345static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2346 char __user *optval, int optlen)
2347{
2348 struct sctp_assoc_value params;
2349 struct sctp_transport *trans = NULL;
2350 struct sctp_association *asoc = NULL;
2351 struct sctp_sock *sp = sctp_sk(sk);
2352
2353 if (optlen != sizeof(struct sctp_assoc_value))
2354 return - EINVAL;
2355
2356 if (copy_from_user(&params, optval, optlen))
2357 return -EFAULT;
2358
2359 /* Validate value parameter. */
2360 if (params.assoc_value > 500)
2361 return -EINVAL;
2362
2363 /* Get association, if assoc_id != 0 and the socket is a one
2364 * to many style socket, and an association was not found, then
2365 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002366 */
Frank Filz77086102005-12-22 11:37:30 -08002367 asoc = sctp_id2assoc(sk, params.assoc_id);
2368 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2369 return -EINVAL;
2370
2371 if (params.assoc_value) {
2372 if (asoc) {
2373 asoc->sackdelay =
2374 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002375 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002376 (asoc->param_flags & ~SPP_SACKDELAY) |
2377 SPP_SACKDELAY_ENABLE;
2378 } else {
2379 sp->sackdelay = params.assoc_value;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002380 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002381 (sp->param_flags & ~SPP_SACKDELAY) |
2382 SPP_SACKDELAY_ENABLE;
2383 }
2384 } else {
2385 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002386 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002387 (asoc->param_flags & ~SPP_SACKDELAY) |
2388 SPP_SACKDELAY_DISABLE;
2389 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002390 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002391 (sp->param_flags & ~SPP_SACKDELAY) |
2392 SPP_SACKDELAY_DISABLE;
2393 }
2394 }
2395
2396 /* If change is for association, also apply to each transport. */
2397 if (asoc) {
2398 struct list_head *pos;
2399
2400 list_for_each(pos, &asoc->peer.transport_addr_list) {
2401 trans = list_entry(pos, struct sctp_transport,
2402 transports);
2403 if (params.assoc_value) {
2404 trans->sackdelay =
2405 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002406 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002407 (trans->param_flags & ~SPP_SACKDELAY) |
2408 SPP_SACKDELAY_ENABLE;
2409 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002410 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002411 (trans->param_flags & ~SPP_SACKDELAY) |
2412 SPP_SACKDELAY_DISABLE;
2413 }
2414 }
2415 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002416
Frank Filz77086102005-12-22 11:37:30 -08002417 return 0;
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2421 *
2422 * Applications can specify protocol parameters for the default association
2423 * initialization. The option name argument to setsockopt() and getsockopt()
2424 * is SCTP_INITMSG.
2425 *
2426 * Setting initialization parameters is effective only on an unconnected
2427 * socket (for UDP-style sockets only future associations are effected
2428 * by the change). With TCP-style sockets, this option is inherited by
2429 * sockets derived from a listener socket.
2430 */
2431static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2432{
2433 struct sctp_initmsg sinit;
2434 struct sctp_sock *sp = sctp_sk(sk);
2435
2436 if (optlen != sizeof(struct sctp_initmsg))
2437 return -EINVAL;
2438 if (copy_from_user(&sinit, optval, optlen))
2439 return -EFAULT;
2440
2441 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002442 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002444 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002446 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002448 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 return 0;
2451}
2452
2453/*
2454 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2455 *
2456 * Applications that wish to use the sendto() system call may wish to
2457 * specify a default set of parameters that would normally be supplied
2458 * through the inclusion of ancillary data. This socket option allows
2459 * such an application to set the default sctp_sndrcvinfo structure.
2460 * The application that wishes to use this socket option simply passes
2461 * in to this call the sctp_sndrcvinfo structure defined in Section
2462 * 5.2.2) The input parameters accepted by this call include
2463 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2464 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2465 * to this call if the caller is using the UDP model.
2466 */
2467static int sctp_setsockopt_default_send_param(struct sock *sk,
2468 char __user *optval, int optlen)
2469{
2470 struct sctp_sndrcvinfo info;
2471 struct sctp_association *asoc;
2472 struct sctp_sock *sp = sctp_sk(sk);
2473
2474 if (optlen != sizeof(struct sctp_sndrcvinfo))
2475 return -EINVAL;
2476 if (copy_from_user(&info, optval, optlen))
2477 return -EFAULT;
2478
2479 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2480 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2481 return -EINVAL;
2482
2483 if (asoc) {
2484 asoc->default_stream = info.sinfo_stream;
2485 asoc->default_flags = info.sinfo_flags;
2486 asoc->default_ppid = info.sinfo_ppid;
2487 asoc->default_context = info.sinfo_context;
2488 asoc->default_timetolive = info.sinfo_timetolive;
2489 } else {
2490 sp->default_stream = info.sinfo_stream;
2491 sp->default_flags = info.sinfo_flags;
2492 sp->default_ppid = info.sinfo_ppid;
2493 sp->default_context = info.sinfo_context;
2494 sp->default_timetolive = info.sinfo_timetolive;
2495 }
2496
2497 return 0;
2498}
2499
2500/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2501 *
2502 * Requests that the local SCTP stack use the enclosed peer address as
2503 * the association primary. The enclosed address must be one of the
2504 * association peer's addresses.
2505 */
2506static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2507 int optlen)
2508{
2509 struct sctp_prim prim;
2510 struct sctp_transport *trans;
2511
2512 if (optlen != sizeof(struct sctp_prim))
2513 return -EINVAL;
2514
2515 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2516 return -EFAULT;
2517
2518 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2519 if (!trans)
2520 return -EINVAL;
2521
2522 sctp_assoc_set_primary(trans->asoc, trans);
2523
2524 return 0;
2525}
2526
2527/*
2528 * 7.1.5 SCTP_NODELAY
2529 *
2530 * Turn on/off any Nagle-like algorithm. This means that packets are
2531 * generally sent as soon as possible and no unnecessary delays are
2532 * introduced, at the cost of more packets in the network. Expects an
2533 * integer boolean flag.
2534 */
2535static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2536 int optlen)
2537{
2538 int val;
2539
2540 if (optlen < sizeof(int))
2541 return -EINVAL;
2542 if (get_user(val, (int __user *)optval))
2543 return -EFAULT;
2544
2545 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2546 return 0;
2547}
2548
2549/*
2550 *
2551 * 7.1.1 SCTP_RTOINFO
2552 *
2553 * The protocol parameters used to initialize and bound retransmission
2554 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2555 * and modify these parameters.
2556 * All parameters are time values, in milliseconds. A value of 0, when
2557 * modifying the parameters, indicates that the current value should not
2558 * be changed.
2559 *
2560 */
2561static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2562 struct sctp_rtoinfo rtoinfo;
2563 struct sctp_association *asoc;
2564
2565 if (optlen != sizeof (struct sctp_rtoinfo))
2566 return -EINVAL;
2567
2568 if (copy_from_user(&rtoinfo, optval, optlen))
2569 return -EFAULT;
2570
2571 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2572
2573 /* Set the values to the specific association */
2574 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2575 return -EINVAL;
2576
2577 if (asoc) {
2578 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002579 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 msecs_to_jiffies(rtoinfo.srto_initial);
2581 if (rtoinfo.srto_max != 0)
2582 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2583 if (rtoinfo.srto_min != 0)
2584 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2585 } else {
2586 /* If there is no association or the association-id = 0
2587 * set the values to the endpoint.
2588 */
2589 struct sctp_sock *sp = sctp_sk(sk);
2590
2591 if (rtoinfo.srto_initial != 0)
2592 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2593 if (rtoinfo.srto_max != 0)
2594 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2595 if (rtoinfo.srto_min != 0)
2596 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2597 }
2598
2599 return 0;
2600}
2601
2602/*
2603 *
2604 * 7.1.2 SCTP_ASSOCINFO
2605 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002606 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 * of the association.
2608 * Returns an error if the new association retransmission value is
2609 * greater than the sum of the retransmission value of the peer.
2610 * See [SCTP] for more information.
2611 *
2612 */
2613static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2614{
2615
2616 struct sctp_assocparams assocparams;
2617 struct sctp_association *asoc;
2618
2619 if (optlen != sizeof(struct sctp_assocparams))
2620 return -EINVAL;
2621 if (copy_from_user(&assocparams, optval, optlen))
2622 return -EFAULT;
2623
2624 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2625
2626 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2627 return -EINVAL;
2628
2629 /* Set the values to the specific association */
2630 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002631 if (assocparams.sasoc_asocmaxrxt != 0) {
2632 __u32 path_sum = 0;
2633 int paths = 0;
2634 struct list_head *pos;
2635 struct sctp_transport *peer_addr;
2636
2637 list_for_each(pos, &asoc->peer.transport_addr_list) {
2638 peer_addr = list_entry(pos,
2639 struct sctp_transport,
2640 transports);
2641 path_sum += peer_addr->pathmaxrxt;
2642 paths++;
2643 }
2644
2645 /* Only validate asocmaxrxt if we have more then
2646 * one path/transport. We do this because path
2647 * retransmissions are only counted when we have more
2648 * then one path.
2649 */
2650 if (paths > 1 &&
2651 assocparams.sasoc_asocmaxrxt > path_sum)
2652 return -EINVAL;
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002655 }
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (assocparams.sasoc_cookie_life != 0) {
2658 asoc->cookie_life.tv_sec =
2659 assocparams.sasoc_cookie_life / 1000;
2660 asoc->cookie_life.tv_usec =
2661 (assocparams.sasoc_cookie_life % 1000)
2662 * 1000;
2663 }
2664 } else {
2665 /* Set the values to the endpoint */
2666 struct sctp_sock *sp = sctp_sk(sk);
2667
2668 if (assocparams.sasoc_asocmaxrxt != 0)
2669 sp->assocparams.sasoc_asocmaxrxt =
2670 assocparams.sasoc_asocmaxrxt;
2671 if (assocparams.sasoc_cookie_life != 0)
2672 sp->assocparams.sasoc_cookie_life =
2673 assocparams.sasoc_cookie_life;
2674 }
2675 return 0;
2676}
2677
2678/*
2679 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2680 *
2681 * This socket option is a boolean flag which turns on or off mapped V4
2682 * addresses. If this option is turned on and the socket is type
2683 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2684 * If this option is turned off, then no mapping will be done of V4
2685 * addresses and a user will receive both PF_INET6 and PF_INET type
2686 * addresses on the socket.
2687 */
2688static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2689{
2690 int val;
2691 struct sctp_sock *sp = sctp_sk(sk);
2692
2693 if (optlen < sizeof(int))
2694 return -EINVAL;
2695 if (get_user(val, (int __user *)optval))
2696 return -EFAULT;
2697 if (val)
2698 sp->v4mapped = 1;
2699 else
2700 sp->v4mapped = 0;
2701
2702 return 0;
2703}
2704
2705/*
2706 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2707 *
2708 * This socket option specifies the maximum size to put in any outgoing
2709 * SCTP chunk. If a message is larger than this size it will be
2710 * fragmented by SCTP into the specified size. Note that the underlying
2711 * SCTP implementation may fragment into smaller sized chunks when the
2712 * PMTU of the underlying association is smaller than the value set by
2713 * the user.
2714 */
2715static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2716{
2717 struct sctp_association *asoc;
2718 struct list_head *pos;
2719 struct sctp_sock *sp = sctp_sk(sk);
2720 int val;
2721
2722 if (optlen < sizeof(int))
2723 return -EINVAL;
2724 if (get_user(val, (int __user *)optval))
2725 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002726 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -EINVAL;
2728 sp->user_frag = val;
2729
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002730 /* Update the frag_point of the existing associations. */
2731 list_for_each(pos, &(sp->ep->asocs)) {
2732 asoc = list_entry(pos, struct sctp_association, asocs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002733 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
2735
2736 return 0;
2737}
2738
2739
2740/*
2741 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2742 *
2743 * Requests that the peer mark the enclosed address as the association
2744 * primary. The enclosed address must be one of the association's
2745 * locally bound addresses. The following structure is used to make a
2746 * set primary request:
2747 */
2748static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2749 int optlen)
2750{
2751 struct sctp_sock *sp;
2752 struct sctp_endpoint *ep;
2753 struct sctp_association *asoc = NULL;
2754 struct sctp_setpeerprim prim;
2755 struct sctp_chunk *chunk;
2756 int err;
2757
2758 sp = sctp_sk(sk);
2759 ep = sp->ep;
2760
2761 if (!sctp_addip_enable)
2762 return -EPERM;
2763
2764 if (optlen != sizeof(struct sctp_setpeerprim))
2765 return -EINVAL;
2766
2767 if (copy_from_user(&prim, optval, optlen))
2768 return -EFAULT;
2769
2770 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002771 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return -EINVAL;
2773
2774 if (!asoc->peer.asconf_capable)
2775 return -EPERM;
2776
2777 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2778 return -EPERM;
2779
2780 if (!sctp_state(asoc, ESTABLISHED))
2781 return -ENOTCONN;
2782
2783 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2784 return -EADDRNOTAVAIL;
2785
2786 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2787 chunk = sctp_make_asconf_set_prim(asoc,
2788 (union sctp_addr *)&prim.sspp_addr);
2789 if (!chunk)
2790 return -ENOMEM;
2791
2792 err = sctp_send_asconf(asoc, chunk);
2793
2794 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2795
2796 return err;
2797}
2798
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002799static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 int optlen)
2801{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002802 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002804 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002806 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return -EFAULT;
2808
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002809 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 return 0;
2812}
2813
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08002814/*
2815 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2816 *
2817 * The context field in the sctp_sndrcvinfo structure is normally only
2818 * used when a failed message is retrieved holding the value that was
2819 * sent down on the actual send call. This option allows the setting of
2820 * a default context on an association basis that will be received on
2821 * reading messages from the peer. This is especially helpful in the
2822 * one-2-many model for an application to keep some reference to an
2823 * internal state machine that is processing messages on the
2824 * association. Note that the setting of this value only effects
2825 * received messages from the peer and does not effect the value that is
2826 * saved with outbound messages.
2827 */
2828static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2829 int optlen)
2830{
2831 struct sctp_assoc_value params;
2832 struct sctp_sock *sp;
2833 struct sctp_association *asoc;
2834
2835 if (optlen != sizeof(struct sctp_assoc_value))
2836 return -EINVAL;
2837 if (copy_from_user(&params, optval, optlen))
2838 return -EFAULT;
2839
2840 sp = sctp_sk(sk);
2841
2842 if (params.assoc_id != 0) {
2843 asoc = sctp_id2assoc(sk, params.assoc_id);
2844 if (!asoc)
2845 return -EINVAL;
2846 asoc->default_rcv_context = params.assoc_value;
2847 } else {
2848 sp->default_rcv_context = params.assoc_value;
2849 }
2850
2851 return 0;
2852}
2853
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002854/*
2855 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2856 *
2857 * This options will at a minimum specify if the implementation is doing
2858 * fragmented interleave. Fragmented interleave, for a one to many
2859 * socket, is when subsequent calls to receive a message may return
2860 * parts of messages from different associations. Some implementations
2861 * may allow you to turn this value on or off. If so, when turned off,
2862 * no fragment interleave will occur (which will cause a head of line
2863 * blocking amongst multiple associations sharing the same one to many
2864 * socket). When this option is turned on, then each receive call may
2865 * come from a different association (thus the user must receive data
2866 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2867 * association each receive belongs to.
2868 *
2869 * This option takes a boolean value. A non-zero value indicates that
2870 * fragmented interleave is on. A value of zero indicates that
2871 * fragmented interleave is off.
2872 *
2873 * Note that it is important that an implementation that allows this
2874 * option to be turned on, have it off by default. Otherwise an unaware
2875 * application using the one to many model may become confused and act
2876 * incorrectly.
2877 */
2878static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2879 char __user *optval,
2880 int optlen)
2881{
2882 int val;
2883
2884 if (optlen != sizeof(int))
2885 return -EINVAL;
2886 if (get_user(val, (int __user *)optval))
2887 return -EFAULT;
2888
2889 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2890
2891 return 0;
2892}
2893
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07002894/*
2895 * 7.1.25. Set or Get the sctp partial delivery point
2896 * (SCTP_PARTIAL_DELIVERY_POINT)
2897 * This option will set or get the SCTP partial delivery point. This
2898 * point is the size of a message where the partial delivery API will be
2899 * invoked to help free up rwnd space for the peer. Setting this to a
2900 * lower value will cause partial delivery's to happen more often. The
2901 * calls argument is an integer that sets or gets the partial delivery
2902 * point.
2903 */
2904static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2905 char __user *optval,
2906 int optlen)
2907{
2908 u32 val;
2909
2910 if (optlen != sizeof(u32))
2911 return -EINVAL;
2912 if (get_user(val, (int __user *)optval))
2913 return -EFAULT;
2914
2915 sctp_sk(sk)->pd_point = val;
2916
2917 return 0; /* is this the right error code? */
2918}
2919
Vlad Yasevich70331572007-03-23 11:34:36 -07002920/*
2921 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2922 *
2923 * This option will allow a user to change the maximum burst of packets
2924 * that can be emitted by this association. Note that the default value
2925 * is 4, and some implementations may restrict this setting so that it
2926 * can only be lowered.
2927 *
2928 * NOTE: This text doesn't seem right. Do this on a socket basis with
2929 * future associations inheriting the socket value.
2930 */
2931static int sctp_setsockopt_maxburst(struct sock *sk,
2932 char __user *optval,
2933 int optlen)
2934{
2935 int val;
2936
2937 if (optlen != sizeof(int))
2938 return -EINVAL;
2939 if (get_user(val, (int __user *)optval))
2940 return -EFAULT;
2941
2942 if (val < 0)
2943 return -EINVAL;
2944
2945 sctp_sk(sk)->max_burst = val;
2946
2947 return 0;
2948}
2949
Vlad Yasevich65b07e52007-09-16 19:34:00 -07002950/*
2951 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
2952 *
2953 * This set option adds a chunk type that the user is requesting to be
2954 * received only in an authenticated way. Changes to the list of chunks
2955 * will only effect future associations on the socket.
2956 */
2957static int sctp_setsockopt_auth_chunk(struct sock *sk,
2958 char __user *optval,
2959 int optlen)
2960{
2961 struct sctp_authchunk val;
2962
2963 if (optlen != sizeof(struct sctp_authchunk))
2964 return -EINVAL;
2965 if (copy_from_user(&val, optval, optlen))
2966 return -EFAULT;
2967
2968 switch (val.sauth_chunk) {
2969 case SCTP_CID_INIT:
2970 case SCTP_CID_INIT_ACK:
2971 case SCTP_CID_SHUTDOWN_COMPLETE:
2972 case SCTP_CID_AUTH:
2973 return -EINVAL;
2974 }
2975
2976 /* add this chunk id to the endpoint */
2977 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
2978}
2979
2980/*
2981 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
2982 *
2983 * This option gets or sets the list of HMAC algorithms that the local
2984 * endpoint requires the peer to use.
2985 */
2986static int sctp_setsockopt_hmac_ident(struct sock *sk,
2987 char __user *optval,
2988 int optlen)
2989{
2990 struct sctp_hmacalgo *hmacs;
2991 int err;
2992
2993 if (optlen < sizeof(struct sctp_hmacalgo))
2994 return -EINVAL;
2995
2996 hmacs = kmalloc(optlen, GFP_KERNEL);
2997 if (!hmacs)
2998 return -ENOMEM;
2999
3000 if (copy_from_user(hmacs, optval, optlen)) {
3001 err = -EFAULT;
3002 goto out;
3003 }
3004
3005 if (hmacs->shmac_num_idents == 0 ||
3006 hmacs->shmac_num_idents > SCTP_AUTH_NUM_HMACS) {
3007 err = -EINVAL;
3008 goto out;
3009 }
3010
3011 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3012out:
3013 kfree(hmacs);
3014 return err;
3015}
3016
3017/*
3018 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3019 *
3020 * This option will set a shared secret key which is used to build an
3021 * association shared key.
3022 */
3023static int sctp_setsockopt_auth_key(struct sock *sk,
3024 char __user *optval,
3025 int optlen)
3026{
3027 struct sctp_authkey *authkey;
3028 struct sctp_association *asoc;
3029 int ret;
3030
3031 if (optlen <= sizeof(struct sctp_authkey))
3032 return -EINVAL;
3033
3034 authkey = kmalloc(optlen, GFP_KERNEL);
3035 if (!authkey)
3036 return -ENOMEM;
3037
3038 if (copy_from_user(authkey, optval, optlen)) {
3039 ret = -EFAULT;
3040 goto out;
3041 }
3042
3043 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3044 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3045 ret = -EINVAL;
3046 goto out;
3047 }
3048
3049 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3050out:
3051 kfree(authkey);
3052 return ret;
3053}
3054
3055/*
3056 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3057 *
3058 * This option will get or set the active shared key to be used to build
3059 * the association shared key.
3060 */
3061static int sctp_setsockopt_active_key(struct sock *sk,
3062 char __user *optval,
3063 int optlen)
3064{
3065 struct sctp_authkeyid val;
3066 struct sctp_association *asoc;
3067
3068 if (optlen != sizeof(struct sctp_authkeyid))
3069 return -EINVAL;
3070 if (copy_from_user(&val, optval, optlen))
3071 return -EFAULT;
3072
3073 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3074 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3075 return -EINVAL;
3076
3077 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3078 val.scact_keynumber);
3079}
3080
3081/*
3082 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3083 *
3084 * This set option will delete a shared secret key from use.
3085 */
3086static int sctp_setsockopt_del_key(struct sock *sk,
3087 char __user *optval,
3088 int optlen)
3089{
3090 struct sctp_authkeyid val;
3091 struct sctp_association *asoc;
3092
3093 if (optlen != sizeof(struct sctp_authkeyid))
3094 return -EINVAL;
3095 if (copy_from_user(&val, optval, optlen))
3096 return -EFAULT;
3097
3098 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3099 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3100 return -EINVAL;
3101
3102 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3103 val.scact_keynumber);
3104
3105}
3106
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/* API 6.2 setsockopt(), getsockopt()
3109 *
3110 * Applications use setsockopt() and getsockopt() to set or retrieve
3111 * socket options. Socket options are used to change the default
3112 * behavior of sockets calls. They are described in Section 7.
3113 *
3114 * The syntax is:
3115 *
3116 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3117 * int __user *optlen);
3118 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3119 * int optlen);
3120 *
3121 * sd - the socket descript.
3122 * level - set to IPPROTO_SCTP for all SCTP options.
3123 * optname - the option name.
3124 * optval - the buffer to store the value of the option.
3125 * optlen - the size of the buffer.
3126 */
3127SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
3128 char __user *optval, int optlen)
3129{
3130 int retval = 0;
3131
3132 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3133 sk, optname);
3134
3135 /* I can hardly begin to describe how wrong this is. This is
3136 * so broken as to be worse than useless. The API draft
3137 * REALLY is NOT helpful here... I am not convinced that the
3138 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3139 * are at all well-founded.
3140 */
3141 if (level != SOL_SCTP) {
3142 struct sctp_af *af = sctp_sk(sk)->pf->af;
3143 retval = af->setsockopt(sk, level, optname, optval, optlen);
3144 goto out_nounlock;
3145 }
3146
3147 sctp_lock_sock(sk);
3148
3149 switch (optname) {
3150 case SCTP_SOCKOPT_BINDX_ADD:
3151 /* 'optlen' is the size of the addresses buffer. */
3152 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3153 optlen, SCTP_BINDX_ADD_ADDR);
3154 break;
3155
3156 case SCTP_SOCKOPT_BINDX_REM:
3157 /* 'optlen' is the size of the addresses buffer. */
3158 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3159 optlen, SCTP_BINDX_REM_ADDR);
3160 break;
3161
Frank Filz3f7a87d2005-06-20 13:14:57 -07003162 case SCTP_SOCKOPT_CONNECTX:
3163 /* 'optlen' is the size of the addresses buffer. */
3164 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3165 optlen);
3166 break;
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 case SCTP_DISABLE_FRAGMENTS:
3169 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3170 break;
3171
3172 case SCTP_EVENTS:
3173 retval = sctp_setsockopt_events(sk, optval, optlen);
3174 break;
3175
3176 case SCTP_AUTOCLOSE:
3177 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3178 break;
3179
3180 case SCTP_PEER_ADDR_PARAMS:
3181 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3182 break;
3183
Frank Filz77086102005-12-22 11:37:30 -08003184 case SCTP_DELAYED_ACK_TIME:
3185 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3186 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003187 case SCTP_PARTIAL_DELIVERY_POINT:
3188 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3189 break;
Frank Filz77086102005-12-22 11:37:30 -08003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 case SCTP_INITMSG:
3192 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3193 break;
3194 case SCTP_DEFAULT_SEND_PARAM:
3195 retval = sctp_setsockopt_default_send_param(sk, optval,
3196 optlen);
3197 break;
3198 case SCTP_PRIMARY_ADDR:
3199 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3200 break;
3201 case SCTP_SET_PEER_PRIMARY_ADDR:
3202 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3203 break;
3204 case SCTP_NODELAY:
3205 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3206 break;
3207 case SCTP_RTOINFO:
3208 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3209 break;
3210 case SCTP_ASSOCINFO:
3211 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3212 break;
3213 case SCTP_I_WANT_MAPPED_V4_ADDR:
3214 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3215 break;
3216 case SCTP_MAXSEG:
3217 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3218 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003219 case SCTP_ADAPTATION_LAYER:
3220 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003222 case SCTP_CONTEXT:
3223 retval = sctp_setsockopt_context(sk, optval, optlen);
3224 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003225 case SCTP_FRAGMENT_INTERLEAVE:
3226 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3227 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003228 case SCTP_MAX_BURST:
3229 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3230 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003231 case SCTP_AUTH_CHUNK:
3232 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3233 break;
3234 case SCTP_HMAC_IDENT:
3235 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3236 break;
3237 case SCTP_AUTH_KEY:
3238 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3239 break;
3240 case SCTP_AUTH_ACTIVE_KEY:
3241 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3242 break;
3243 case SCTP_AUTH_DELETE_KEY:
3244 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 default:
3247 retval = -ENOPROTOOPT;
3248 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 sctp_release_sock(sk);
3252
3253out_nounlock:
3254 return retval;
3255}
3256
3257/* API 3.1.6 connect() - UDP Style Syntax
3258 *
3259 * An application may use the connect() call in the UDP model to initiate an
3260 * association without sending data.
3261 *
3262 * The syntax is:
3263 *
3264 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3265 *
3266 * sd: the socket descriptor to have a new association added to.
3267 *
3268 * nam: the address structure (either struct sockaddr_in or struct
3269 * sockaddr_in6 defined in RFC2553 [7]).
3270 *
3271 * len: the size of the address.
3272 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003273SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 int addr_len)
3275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003277 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 sctp_lock_sock(sk);
3280
Frank Filz3f7a87d2005-06-20 13:14:57 -07003281 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3282 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Frank Filz3f7a87d2005-06-20 13:14:57 -07003284 /* Validate addr_len before calling common connect/connectx routine. */
3285 af = sctp_get_af_specific(addr->sa_family);
3286 if (!af || addr_len < af->sockaddr_len) {
3287 err = -EINVAL;
3288 } else {
3289 /* Pass correct addr len to common routine (so it knows there
3290 * is only one address being passed.
3291 */
3292 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return err;
3297}
3298
3299/* FIXME: Write comments. */
3300SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3301{
3302 return -EOPNOTSUPP; /* STUB */
3303}
3304
3305/* 4.1.4 accept() - TCP Style Syntax
3306 *
3307 * Applications use accept() call to remove an established SCTP
3308 * association from the accept queue of the endpoint. A new socket
3309 * descriptor will be returned from accept() to represent the newly
3310 * formed association.
3311 */
3312SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3313{
3314 struct sctp_sock *sp;
3315 struct sctp_endpoint *ep;
3316 struct sock *newsk = NULL;
3317 struct sctp_association *asoc;
3318 long timeo;
3319 int error = 0;
3320
3321 sctp_lock_sock(sk);
3322
3323 sp = sctp_sk(sk);
3324 ep = sp->ep;
3325
3326 if (!sctp_style(sk, TCP)) {
3327 error = -EOPNOTSUPP;
3328 goto out;
3329 }
3330
3331 if (!sctp_sstate(sk, LISTENING)) {
3332 error = -EINVAL;
3333 goto out;
3334 }
3335
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003336 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 error = sctp_wait_for_accept(sk, timeo);
3339 if (error)
3340 goto out;
3341
3342 /* We treat the list of associations on the endpoint as the accept
3343 * queue and pick the first association on the list.
3344 */
3345 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3346
3347 newsk = sp->pf->create_accept_sk(sk, asoc);
3348 if (!newsk) {
3349 error = -ENOMEM;
3350 goto out;
3351 }
3352
3353 /* Populate the fields of the newsk from the oldsk and migrate the
3354 * asoc to the newsk.
3355 */
3356 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3357
3358out:
3359 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003360 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return newsk;
3362}
3363
3364/* The SCTP ioctl handler. */
3365SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3366{
3367 return -ENOIOCTLCMD;
3368}
3369
3370/* This is the function which gets called during socket creation to
3371 * initialized the SCTP-specific portion of the sock.
3372 * The sock structure should already be zero-filled memory.
3373 */
3374SCTP_STATIC int sctp_init_sock(struct sock *sk)
3375{
3376 struct sctp_endpoint *ep;
3377 struct sctp_sock *sp;
3378
3379 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3380
3381 sp = sctp_sk(sk);
3382
3383 /* Initialize the SCTP per socket area. */
3384 switch (sk->sk_type) {
3385 case SOCK_SEQPACKET:
3386 sp->type = SCTP_SOCKET_UDP;
3387 break;
3388 case SOCK_STREAM:
3389 sp->type = SCTP_SOCKET_TCP;
3390 break;
3391 default:
3392 return -ESOCKTNOSUPPORT;
3393 }
3394
3395 /* Initialize default send parameters. These parameters can be
3396 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3397 */
3398 sp->default_stream = 0;
3399 sp->default_ppid = 0;
3400 sp->default_flags = 0;
3401 sp->default_context = 0;
3402 sp->default_timetolive = 0;
3403
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003404 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003405 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /* Initialize default setup parameters. These parameters
3408 * can be modified with the SCTP_INITMSG socket option or
3409 * overridden by the SCTP_INIT CMSG.
3410 */
3411 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3412 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3413 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003414 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 /* Initialize default RTO related parameters. These parameters can
3417 * be modified for with the SCTP_RTOINFO socket option.
3418 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003419 sp->rtoinfo.srto_initial = sctp_rto_initial;
3420 sp->rtoinfo.srto_max = sctp_rto_max;
3421 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 /* Initialize default association related parameters. These parameters
3424 * can be modified with the SCTP_ASSOCINFO socket option.
3425 */
3426 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3427 sp->assocparams.sasoc_number_peer_destinations = 0;
3428 sp->assocparams.sasoc_peer_rwnd = 0;
3429 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003430 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003433 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 */
3435 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3436
3437 /* Default Peer Address Parameters. These defaults can
3438 * be modified via SCTP_PEER_ADDR_PARAMS
3439 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003440 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003441 sp->pathmaxrxt = sctp_max_retrans_path;
3442 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003443 sp->sackdelay = sctp_sack_timeout;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003444 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003445 SPP_PMTUD_ENABLE |
3446 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 /* If enabled no SCTP message fragmentation will be performed.
3449 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3450 */
3451 sp->disable_fragments = 0;
3452
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003453 /* Enable Nagle algorithm by default. */
3454 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456 /* Enable by default. */
3457 sp->v4mapped = 1;
3458
3459 /* Auto-close idle associations after the configured
3460 * number of seconds. A value of 0 disables this
3461 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3462 * for UDP-style sockets only.
3463 */
3464 sp->autoclose = 0;
3465
3466 /* User specified fragmentation limit. */
3467 sp->user_frag = 0;
3468
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003469 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 sp->pf = sctp_get_pf_specific(sk->sk_family);
3472
3473 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003474 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003476 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 /* Create a per socket endpoint structure. Even if we
3479 * change the data structure relationships, this may still
3480 * be useful for storing pre-connect address information.
3481 */
3482 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3483 if (!ep)
3484 return -ENOMEM;
3485
3486 sp->ep = ep;
3487 sp->hmac = NULL;
3488
3489 SCTP_DBG_OBJCNT_INC(sock);
Neil Horman4d93df02007-08-15 16:07:44 -07003490 atomic_inc(&sctp_sockets_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return 0;
3492}
3493
3494/* Cleanup any SCTP per socket resources. */
3495SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3496{
3497 struct sctp_endpoint *ep;
3498
3499 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3500
3501 /* Release our hold on the endpoint. */
3502 ep = sctp_sk(sk)->ep;
3503 sctp_endpoint_free(ep);
Neil Horman4d93df02007-08-15 16:07:44 -07003504 atomic_dec(&sctp_sockets_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return 0;
3506}
3507
3508/* API 4.1.7 shutdown() - TCP Style Syntax
3509 * int shutdown(int socket, int how);
3510 *
3511 * sd - the socket descriptor of the association to be closed.
3512 * how - Specifies the type of shutdown. The values are
3513 * as follows:
3514 * SHUT_RD
3515 * Disables further receive operations. No SCTP
3516 * protocol action is taken.
3517 * SHUT_WR
3518 * Disables further send operations, and initiates
3519 * the SCTP shutdown sequence.
3520 * SHUT_RDWR
3521 * Disables further send and receive operations
3522 * and initiates the SCTP shutdown sequence.
3523 */
3524SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3525{
3526 struct sctp_endpoint *ep;
3527 struct sctp_association *asoc;
3528
3529 if (!sctp_style(sk, TCP))
3530 return;
3531
3532 if (how & SEND_SHUTDOWN) {
3533 ep = sctp_sk(sk)->ep;
3534 if (!list_empty(&ep->asocs)) {
3535 asoc = list_entry(ep->asocs.next,
3536 struct sctp_association, asocs);
3537 sctp_primitive_SHUTDOWN(asoc, NULL);
3538 }
3539 }
3540}
3541
3542/* 7.2.1 Association Status (SCTP_STATUS)
3543
3544 * Applications can retrieve current status information about an
3545 * association, including association state, peer receiver window size,
3546 * number of unacked data chunks, and number of data chunks pending
3547 * receipt. This information is read-only.
3548 */
3549static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3550 char __user *optval,
3551 int __user *optlen)
3552{
3553 struct sctp_status status;
3554 struct sctp_association *asoc = NULL;
3555 struct sctp_transport *transport;
3556 sctp_assoc_t associd;
3557 int retval = 0;
3558
Neil Horman408f22e2007-06-16 14:03:45 -04003559 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 retval = -EINVAL;
3561 goto out;
3562 }
3563
Neil Horman408f22e2007-06-16 14:03:45 -04003564 len = sizeof(status);
3565 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 retval = -EFAULT;
3567 goto out;
3568 }
3569
3570 associd = status.sstat_assoc_id;
3571 asoc = sctp_id2assoc(sk, associd);
3572 if (!asoc) {
3573 retval = -EINVAL;
3574 goto out;
3575 }
3576
3577 transport = asoc->peer.primary_path;
3578
3579 status.sstat_assoc_id = sctp_assoc2id(asoc);
3580 status.sstat_state = asoc->state;
3581 status.sstat_rwnd = asoc->peer.rwnd;
3582 status.sstat_unackdata = asoc->unack_data;
3583
3584 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3585 status.sstat_instrms = asoc->c.sinit_max_instreams;
3586 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3587 status.sstat_fragmentation_point = asoc->frag_point;
3588 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08003589 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3590 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 /* Map ipv4 address into v4-mapped-on-v6 address. */
3592 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3593 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003594 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3596 status.sstat_primary.spinfo_srtt = transport->srtt;
3597 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003598 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Frank Filz3f7a87d2005-06-20 13:14:57 -07003600 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3601 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (put_user(len, optlen)) {
3604 retval = -EFAULT;
3605 goto out;
3606 }
3607
3608 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3609 len, status.sstat_state, status.sstat_rwnd,
3610 status.sstat_assoc_id);
3611
3612 if (copy_to_user(optval, &status, len)) {
3613 retval = -EFAULT;
3614 goto out;
3615 }
3616
3617out:
3618 return (retval);
3619}
3620
3621
3622/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3623 *
3624 * Applications can retrieve information about a specific peer address
3625 * of an association, including its reachability state, congestion
3626 * window, and retransmission timer values. This information is
3627 * read-only.
3628 */
3629static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3630 char __user *optval,
3631 int __user *optlen)
3632{
3633 struct sctp_paddrinfo pinfo;
3634 struct sctp_transport *transport;
3635 int retval = 0;
3636
Neil Horman408f22e2007-06-16 14:03:45 -04003637 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 retval = -EINVAL;
3639 goto out;
3640 }
3641
Neil Horman408f22e2007-06-16 14:03:45 -04003642 len = sizeof(pinfo);
3643 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 retval = -EFAULT;
3645 goto out;
3646 }
3647
3648 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3649 pinfo.spinfo_assoc_id);
3650 if (!transport)
3651 return -EINVAL;
3652
3653 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003654 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 pinfo.spinfo_cwnd = transport->cwnd;
3656 pinfo.spinfo_srtt = transport->srtt;
3657 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003658 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Frank Filz3f7a87d2005-06-20 13:14:57 -07003660 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3661 pinfo.spinfo_state = SCTP_ACTIVE;
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 if (put_user(len, optlen)) {
3664 retval = -EFAULT;
3665 goto out;
3666 }
3667
3668 if (copy_to_user(optval, &pinfo, len)) {
3669 retval = -EFAULT;
3670 goto out;
3671 }
3672
3673out:
3674 return (retval);
3675}
3676
3677/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3678 *
3679 * This option is a on/off flag. If enabled no SCTP message
3680 * fragmentation will be performed. Instead if a message being sent
3681 * exceeds the current PMTU size, the message will NOT be sent and
3682 * instead a error will be indicated to the user.
3683 */
3684static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3685 char __user *optval, int __user *optlen)
3686{
3687 int val;
3688
3689 if (len < sizeof(int))
3690 return -EINVAL;
3691
3692 len = sizeof(int);
3693 val = (sctp_sk(sk)->disable_fragments == 1);
3694 if (put_user(len, optlen))
3695 return -EFAULT;
3696 if (copy_to_user(optval, &val, len))
3697 return -EFAULT;
3698 return 0;
3699}
3700
3701/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3702 *
3703 * This socket option is used to specify various notifications and
3704 * ancillary data the user wishes to receive.
3705 */
3706static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3707 int __user *optlen)
3708{
Neil Horman408f22e2007-06-16 14:03:45 -04003709 if (len < sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003711 len = sizeof(struct sctp_event_subscribe);
3712 if (put_user(len, optlen))
3713 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3715 return -EFAULT;
3716 return 0;
3717}
3718
3719/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3720 *
3721 * This socket option is applicable to the UDP-style socket only. When
3722 * set it will cause associations that are idle for more than the
3723 * specified number of seconds to automatically close. An association
3724 * being idle is defined an association that has NOT sent or received
3725 * user data. The special value of '0' indicates that no automatic
3726 * close of any associations should be performed. The option expects an
3727 * integer defining the number of seconds of idle time before an
3728 * association is closed.
3729 */
3730static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3731{
3732 /* Applicable to UDP-style socket only */
3733 if (sctp_style(sk, TCP))
3734 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04003735 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003737 len = sizeof(int);
3738 if (put_user(len, optlen))
3739 return -EFAULT;
3740 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return -EFAULT;
3742 return 0;
3743}
3744
3745/* Helper routine to branch off an association to a new socket. */
3746SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3747 struct socket **sockp)
3748{
3749 struct sock *sk = asoc->base.sk;
3750 struct socket *sock;
Vlad Yasevich4f444302006-10-30 18:54:32 -08003751 struct inet_sock *inetsk;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003752 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 int err = 0;
3754
3755 /* An association cannot be branched off from an already peeled-off
3756 * socket, nor is this supported for tcp style sockets.
3757 */
3758 if (!sctp_style(sk, UDP))
3759 return -EINVAL;
3760
3761 /* Create a new socket. */
3762 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3763 if (err < 0)
3764 return err;
3765
3766 /* Populate the fields of the newsk from the oldsk and migrate the
3767 * asoc to the newsk.
3768 */
3769 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003770
3771 /* Make peeled-off sockets more like 1-1 accepted sockets.
3772 * Set the daddr and initialize id to something more random
3773 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003774 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3775 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003776 inetsk = inet_sk(sock->sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003777 inetsk->id = asoc->next_tsn ^ jiffies;
3778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 *sockp = sock;
3780
3781 return err;
3782}
3783
3784static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3785{
3786 sctp_peeloff_arg_t peeloff;
3787 struct socket *newsock;
3788 int retval = 0;
3789 struct sctp_association *asoc;
3790
Neil Horman408f22e2007-06-16 14:03:45 -04003791 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003793 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 if (copy_from_user(&peeloff, optval, len))
3795 return -EFAULT;
3796
3797 asoc = sctp_id2assoc(sk, peeloff.associd);
3798 if (!asoc) {
3799 retval = -EINVAL;
3800 goto out;
3801 }
3802
3803 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3804
3805 retval = sctp_do_peeloff(asoc, &newsock);
3806 if (retval < 0)
3807 goto out;
3808
3809 /* Map the socket to an unused fd that can be returned to the user. */
3810 retval = sock_map_fd(newsock);
3811 if (retval < 0) {
3812 sock_release(newsock);
3813 goto out;
3814 }
3815
3816 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3817 __FUNCTION__, sk, asoc, newsock->sk, retval);
3818
3819 /* Return the fd mapped to the new socket. */
3820 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04003821 if (put_user(len, optlen))
3822 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 if (copy_to_user(optval, &peeloff, len))
3824 retval = -EFAULT;
3825
3826out:
3827 return retval;
3828}
3829
3830/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3831 *
3832 * Applications can enable or disable heartbeats for any peer address of
3833 * an association, modify an address's heartbeat interval, force a
3834 * heartbeat to be sent immediately, and adjust the address's maximum
3835 * number of retransmissions sent before an address is considered
3836 * unreachable. The following structure is used to access and modify an
3837 * address's parameters:
3838 *
3839 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003840 * sctp_assoc_t spp_assoc_id;
3841 * struct sockaddr_storage spp_address;
3842 * uint32_t spp_hbinterval;
3843 * uint16_t spp_pathmaxrxt;
3844 * uint32_t spp_pathmtu;
3845 * uint32_t spp_sackdelay;
3846 * uint32_t spp_flags;
3847 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003849 * spp_assoc_id - (one-to-many style socket) This is filled in the
3850 * application, and identifies the association for
3851 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 * spp_address - This specifies which address is of interest.
3853 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003854 * in milliseconds. If a value of zero
3855 * is present in this field then no changes are to
3856 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 * spp_pathmaxrxt - This contains the maximum number of
3858 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003859 * considered unreachable. If a value of zero
3860 * is present in this field then no changes are to
3861 * be made to this parameter.
3862 * spp_pathmtu - When Path MTU discovery is disabled the value
3863 * specified here will be the "fixed" path mtu.
3864 * Note that if the spp_address field is empty
3865 * then all associations on this address will
3866 * have this fixed path mtu set upon them.
3867 *
3868 * spp_sackdelay - When delayed sack is enabled, this value specifies
3869 * the number of milliseconds that sacks will be delayed
3870 * for. This value will apply to all addresses of an
3871 * association if the spp_address field is empty. Note
3872 * also, that if delayed sack is enabled and this
3873 * value is set to 0, no change is made to the last
3874 * recorded delayed sack timer value.
3875 *
3876 * spp_flags - These flags are used to control various features
3877 * on an association. The flag field may contain
3878 * zero or more of the following options.
3879 *
3880 * SPP_HB_ENABLE - Enable heartbeats on the
3881 * specified address. Note that if the address
3882 * field is empty all addresses for the association
3883 * have heartbeats enabled upon them.
3884 *
3885 * SPP_HB_DISABLE - Disable heartbeats on the
3886 * speicifed address. Note that if the address
3887 * field is empty all addresses for the association
3888 * will have their heartbeats disabled. Note also
3889 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3890 * mutually exclusive, only one of these two should
3891 * be specified. Enabling both fields will have
3892 * undetermined results.
3893 *
3894 * SPP_HB_DEMAND - Request a user initiated heartbeat
3895 * to be made immediately.
3896 *
3897 * SPP_PMTUD_ENABLE - This field will enable PMTU
3898 * discovery upon the specified address. Note that
3899 * if the address feild is empty then all addresses
3900 * on the association are effected.
3901 *
3902 * SPP_PMTUD_DISABLE - This field will disable PMTU
3903 * discovery upon the specified address. Note that
3904 * if the address feild is empty then all addresses
3905 * on the association are effected. Not also that
3906 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3907 * exclusive. Enabling both will have undetermined
3908 * results.
3909 *
3910 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3911 * on delayed sack. The time specified in spp_sackdelay
3912 * is used to specify the sack delay for this address. Note
3913 * that if spp_address is empty then all addresses will
3914 * enable delayed sack and take on the sack delay
3915 * value specified in spp_sackdelay.
3916 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3917 * off delayed sack. If the spp_address field is blank then
3918 * delayed sack is disabled for the entire association. Note
3919 * also that this field is mutually exclusive to
3920 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3921 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 */
3923static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003924 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003926 struct sctp_paddrparams params;
3927 struct sctp_transport *trans = NULL;
3928 struct sctp_association *asoc = NULL;
3929 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Neil Horman408f22e2007-06-16 14:03:45 -04003931 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003933 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if (copy_from_user(&params, optval, len))
3935 return -EFAULT;
3936
Frank Filz52ccb8e2005-12-22 11:36:46 -08003937 /* If an address other than INADDR_ANY is specified, and
3938 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003940 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3941 trans = sctp_addr_id2transport(sk, &params.spp_address,
3942 params.spp_assoc_id);
3943 if (!trans) {
3944 SCTP_DEBUG_PRINTK("Failed no transport\n");
3945 return -EINVAL;
3946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
3948
Frank Filz52ccb8e2005-12-22 11:36:46 -08003949 /* Get association, if assoc_id != 0 and the socket is a one
3950 * to many style socket, and an association was not found, then
3951 * the id was invalid.
3952 */
3953 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3954 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3955 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Frank Filz52ccb8e2005-12-22 11:36:46 -08003959 if (trans) {
3960 /* Fetch transport values. */
3961 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3962 params.spp_pathmtu = trans->pathmtu;
3963 params.spp_pathmaxrxt = trans->pathmaxrxt;
3964 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Frank Filz52ccb8e2005-12-22 11:36:46 -08003966 /*draft-11 doesn't say what to return in spp_flags*/
3967 params.spp_flags = trans->param_flags;
3968 } else if (asoc) {
3969 /* Fetch association values. */
3970 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3971 params.spp_pathmtu = asoc->pathmtu;
3972 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3973 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Frank Filz52ccb8e2005-12-22 11:36:46 -08003975 /*draft-11 doesn't say what to return in spp_flags*/
3976 params.spp_flags = asoc->param_flags;
3977 } else {
3978 /* Fetch socket values. */
3979 params.spp_hbinterval = sp->hbinterval;
3980 params.spp_pathmtu = sp->pathmtu;
3981 params.spp_sackdelay = sp->sackdelay;
3982 params.spp_pathmaxrxt = sp->pathmaxrxt;
3983
3984 /*draft-11 doesn't say what to return in spp_flags*/
3985 params.spp_flags = sp->param_flags;
3986 }
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 if (copy_to_user(optval, &params, len))
3989 return -EFAULT;
3990
3991 if (put_user(len, optlen))
3992 return -EFAULT;
3993
3994 return 0;
3995}
3996
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003997/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08003998 *
3999 * This options will get or set the delayed ack timer. The time is set
4000 * in milliseconds. If the assoc_id is 0, then this sets or gets the
4001 * endpoints default delayed ack timer value. If the assoc_id field is
4002 * non-zero, then the set or get effects the specified association.
4003 *
4004 * struct sctp_assoc_value {
4005 * sctp_assoc_t assoc_id;
4006 * uint32_t assoc_value;
4007 * };
4008 *
4009 * assoc_id - This parameter, indicates which association the
4010 * user is preforming an action upon. Note that if
4011 * this field's value is zero then the endpoints
4012 * default value is changed (effecting future
4013 * associations only).
4014 *
4015 * assoc_value - This parameter contains the number of milliseconds
4016 * that the user is requesting the delayed ACK timer
4017 * be set to. Note that this value is defined in
4018 * the standard to be between 200 and 500 milliseconds.
4019 *
4020 * Note: a value of zero will leave the value alone,
4021 * but disable SACK delay. A non-zero value will also
4022 * enable SACK delay.
4023 */
4024static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
4025 char __user *optval,
4026 int __user *optlen)
4027{
4028 struct sctp_assoc_value params;
4029 struct sctp_association *asoc = NULL;
4030 struct sctp_sock *sp = sctp_sk(sk);
4031
Neil Horman408f22e2007-06-16 14:03:45 -04004032 if (len < sizeof(struct sctp_assoc_value))
Frank Filz77086102005-12-22 11:37:30 -08004033 return - EINVAL;
4034
Neil Horman408f22e2007-06-16 14:03:45 -04004035 len = sizeof(struct sctp_assoc_value);
4036
Frank Filz77086102005-12-22 11:37:30 -08004037 if (copy_from_user(&params, optval, len))
4038 return -EFAULT;
4039
4040 /* Get association, if assoc_id != 0 and the socket is a one
4041 * to many style socket, and an association was not found, then
4042 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004043 */
Frank Filz77086102005-12-22 11:37:30 -08004044 asoc = sctp_id2assoc(sk, params.assoc_id);
4045 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
4046 return -EINVAL;
4047
4048 if (asoc) {
4049 /* Fetch association values. */
4050 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
4051 params.assoc_value = jiffies_to_msecs(
4052 asoc->sackdelay);
4053 else
4054 params.assoc_value = 0;
4055 } else {
4056 /* Fetch socket values. */
4057 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
4058 params.assoc_value = sp->sackdelay;
4059 else
4060 params.assoc_value = 0;
4061 }
4062
4063 if (copy_to_user(optval, &params, len))
4064 return -EFAULT;
4065
4066 if (put_user(len, optlen))
4067 return -EFAULT;
4068
4069 return 0;
4070}
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4073 *
4074 * Applications can specify protocol parameters for the default association
4075 * initialization. The option name argument to setsockopt() and getsockopt()
4076 * is SCTP_INITMSG.
4077 *
4078 * Setting initialization parameters is effective only on an unconnected
4079 * socket (for UDP-style sockets only future associations are effected
4080 * by the change). With TCP-style sockets, this option is inherited by
4081 * sockets derived from a listener socket.
4082 */
4083static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4084{
Neil Horman408f22e2007-06-16 14:03:45 -04004085 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004087 len = sizeof(struct sctp_initmsg);
4088 if (put_user(len, optlen))
4089 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4091 return -EFAULT;
4092 return 0;
4093}
4094
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004095static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
4096 char __user *optval,
4097 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
4099 sctp_assoc_t id;
4100 struct sctp_association *asoc;
4101 struct list_head *pos;
4102 int cnt = 0;
4103
Neil Horman408f22e2007-06-16 14:03:45 -04004104 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return -EINVAL;
4106
4107 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4108 return -EFAULT;
4109
4110 /* For UDP-style sockets, id specifies the association to query. */
4111 asoc = sctp_id2assoc(sk, id);
4112 if (!asoc)
4113 return -EINVAL;
4114
4115 list_for_each(pos, &asoc->peer.transport_addr_list) {
4116 cnt ++;
4117 }
4118
4119 return cnt;
4120}
4121
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004122/*
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004123 * Old API for getting list of peer addresses. Does not work for 32-bit
4124 * programs running on a 64-bit kernel
4125 */
4126static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
4127 char __user *optval,
4128 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
4130 struct sctp_association *asoc;
4131 struct list_head *pos;
4132 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004133 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 struct sctp_transport *from;
4135 void __user *to;
4136 union sctp_addr temp;
4137 struct sctp_sock *sp = sctp_sk(sk);
4138 int addrlen;
4139
Neil Horman408f22e2007-06-16 14:03:45 -04004140 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return -EINVAL;
4142
Neil Horman408f22e2007-06-16 14:03:45 -04004143 len = sizeof(struct sctp_getaddrs_old);
4144
4145 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return -EFAULT;
4147
4148 if (getaddrs.addr_num <= 0) return -EINVAL;
4149
4150 /* For UDP-style sockets, id specifies the association to query. */
4151 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4152 if (!asoc)
4153 return -EINVAL;
4154
4155 to = (void __user *)getaddrs.addrs;
4156 list_for_each(pos, &asoc->peer.transport_addr_list) {
4157 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004158 memcpy(&temp, &from->ipaddr, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4160 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (copy_to_user(to, &temp, addrlen))
4162 return -EFAULT;
4163 to += addrlen ;
4164 cnt ++;
4165 if (cnt >= getaddrs.addr_num) break;
4166 }
4167 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004168 if (put_user(len, optlen))
4169 return -EFAULT;
4170 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return -EFAULT;
4172
4173 return 0;
4174}
4175
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004176static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4177 char __user *optval, int __user *optlen)
4178{
4179 struct sctp_association *asoc;
4180 struct list_head *pos;
4181 int cnt = 0;
4182 struct sctp_getaddrs getaddrs;
4183 struct sctp_transport *from;
4184 void __user *to;
4185 union sctp_addr temp;
4186 struct sctp_sock *sp = sctp_sk(sk);
4187 int addrlen;
4188 size_t space_left;
4189 int bytes_copied;
4190
4191 if (len < sizeof(struct sctp_getaddrs))
4192 return -EINVAL;
4193
4194 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4195 return -EFAULT;
4196
4197 /* For UDP-style sockets, id specifies the association to query. */
4198 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4199 if (!asoc)
4200 return -EINVAL;
4201
4202 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004203 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004204
4205 list_for_each(pos, &asoc->peer.transport_addr_list) {
4206 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004207 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004208 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4209 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004210 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004211 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004212 if (copy_to_user(to, &temp, addrlen))
4213 return -EFAULT;
4214 to += addrlen;
4215 cnt++;
4216 space_left -= addrlen;
4217 }
4218
4219 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4220 return -EFAULT;
4221 bytes_copied = ((char __user *)to) - optval;
4222 if (put_user(bytes_copied, optlen))
4223 return -EFAULT;
4224
4225 return 0;
4226}
4227
4228static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4229 char __user *optval,
4230 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 sctp_assoc_t id;
4233 struct sctp_bind_addr *bp;
4234 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int cnt = 0;
4237
Neil Horman408f22e2007-06-16 14:03:45 -04004238 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return -EINVAL;
4240
4241 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4242 return -EFAULT;
4243
4244 /*
4245 * For UDP-style sockets, id specifies the association to query.
4246 * If the id field is set to the value '0' then the locally bound
4247 * addresses are returned without regard to any particular
4248 * association.
4249 */
4250 if (0 == id) {
4251 bp = &sctp_sk(sk)->ep->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 } else {
4253 asoc = sctp_id2assoc(sk, id);
4254 if (!asoc)
4255 return -EINVAL;
4256 bp = &asoc->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 /* If the endpoint is bound to 0.0.0.0 or ::0, count 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 Yasevich29303542007-09-16 16:02:12 -07004266 rcu_read_lock();
4267 list_for_each_entry_rcu(addr,
4268 &sctp_local_addr_list, list) {
4269 if (!addr->valid)
4270 continue;
4271
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004272 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004273 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 continue;
Vlad Yasevich29303542007-09-16 16:02:12 -07004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 cnt++;
4277 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004278 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 } else {
4280 cnt = 1;
4281 }
4282 goto done;
4283 }
4284
Vlad Yasevich559cf712007-09-16 16:03:28 -07004285 /* Protection on the bound address list is not needed,
4286 * since in the socket option context we hold the socket lock,
4287 * so there is no way that the bound address list can change.
4288 */
4289 list_for_each_entry(addr, &bp->address_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 cnt ++;
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return cnt;
4294}
4295
4296/* Helper function that copies local addresses to user and returns the number
4297 * of addresses copied.
4298 */
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004299static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4300 int max_addrs, void *to,
4301 int *bytes_copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 union sctp_addr temp;
4305 int cnt = 0;
4306 int addrlen;
4307
Vlad Yasevich29303542007-09-16 16:02:12 -07004308 rcu_read_lock();
4309 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4310 if (!addr->valid)
4311 continue;
4312
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004313 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004314 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 continue;
Al Viro6244be42006-11-20 17:21:44 -08004316 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4318 &temp);
4319 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004320 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004323 *bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 cnt ++;
4325 if (cnt >= max_addrs) break;
4326 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004327 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 return cnt;
4330}
4331
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004332static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4333 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004334{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004335 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004336 union sctp_addr temp;
4337 int cnt = 0;
4338 int addrlen;
4339
Vlad Yasevich29303542007-09-16 16:02:12 -07004340 rcu_read_lock();
4341 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4342 if (!addr->valid)
4343 continue;
4344
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004345 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004346 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004347 continue;
Al Viro6244be42006-11-20 17:21:44 -08004348 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004349 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4350 &temp);
4351 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004352 if (space_left < addrlen) {
4353 cnt = -ENOMEM;
4354 break;
4355 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004356 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004357
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004358 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004359 cnt ++;
4360 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004361 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004362 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004363 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004364
4365 return cnt;
4366}
4367
4368/* Old API for getting list of local addresses. Does not work for 32-bit
4369 * programs running on a 64-bit kernel
4370 */
4371static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4372 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
4374 struct sctp_bind_addr *bp;
4375 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004377 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 struct sctp_sockaddr_entry *addr;
4379 void __user *to;
4380 union sctp_addr temp;
4381 struct sctp_sock *sp = sctp_sk(sk);
4382 int addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 int err = 0;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004384 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004385 void *buf;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004386 int bytes_copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Neil Horman408f22e2007-06-16 14:03:45 -04004388 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return -EINVAL;
4390
Neil Horman408f22e2007-06-16 14:03:45 -04004391 len = sizeof(struct sctp_getaddrs_old);
4392 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return -EFAULT;
4394
4395 if (getaddrs.addr_num <= 0) return -EINVAL;
4396 /*
4397 * For UDP-style sockets, id specifies the association to query.
4398 * If the id field is set to the value '0' then the locally bound
4399 * addresses are returned without regard to any particular
4400 * association.
4401 */
4402 if (0 == getaddrs.assoc_id) {
4403 bp = &sctp_sk(sk)->ep->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 } else {
4405 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4406 if (!asoc)
4407 return -EINVAL;
4408 bp = &asoc->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
4411 to = getaddrs.addrs;
4412
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004413 /* Allocate space for a local instance of packed array to hold all
4414 * the data. We store addresses here first and then put write them
4415 * to the user in one shot.
4416 */
4417 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4418 GFP_KERNEL);
4419 if (!addrs)
4420 return -ENOMEM;
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4423 * addresses from the global local address list.
4424 */
4425 if (sctp_list_single_entry(&bp->address_list)) {
4426 addr = list_entry(bp->address_list.next,
4427 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004428 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004429 cnt = sctp_copy_laddrs_old(sk, bp->port,
4430 getaddrs.addr_num,
4431 addrs, &bytes_copied);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004432 goto copy_getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434 }
4435
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004436 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004437 /* Protection on the bound address list is not needed since
4438 * in the socket option context we hold a socket lock and
4439 * thus the bound address list can't change.
4440 */
4441 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004442 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4444 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004445 memcpy(buf, &temp, addrlen);
4446 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004447 bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 cnt ++;
4449 if (cnt >= getaddrs.addr_num) break;
4450 }
4451
4452copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004453 /* copy the entire address list into the user provided space */
4454 if (copy_to_user(to, addrs, bytes_copied)) {
4455 err = -EFAULT;
4456 goto error;
4457 }
4458
4459 /* copy the leading structure back to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004461 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 err = -EFAULT;
4463
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004464error:
4465 kfree(addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return err;
4467}
4468
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004469static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4470 char __user *optval, int __user *optlen)
4471{
4472 struct sctp_bind_addr *bp;
4473 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004474 int cnt = 0;
4475 struct sctp_getaddrs getaddrs;
4476 struct sctp_sockaddr_entry *addr;
4477 void __user *to;
4478 union sctp_addr temp;
4479 struct sctp_sock *sp = sctp_sk(sk);
4480 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004481 int err = 0;
4482 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004483 int bytes_copied = 0;
4484 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004485 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004486
Neil Horman408f22e2007-06-16 14:03:45 -04004487 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004488 return -EINVAL;
4489
4490 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4491 return -EFAULT;
4492
4493 /*
4494 * For UDP-style sockets, id specifies the association to query.
4495 * If the id field is set to the value '0' then the locally bound
4496 * addresses are returned without regard to any particular
4497 * association.
4498 */
4499 if (0 == getaddrs.assoc_id) {
4500 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004501 } else {
4502 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4503 if (!asoc)
4504 return -EINVAL;
4505 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004506 }
4507
4508 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004509 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4510
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004511 addrs = kmalloc(space_left, GFP_KERNEL);
4512 if (!addrs)
4513 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004514
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004515 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4516 * addresses from the global local address list.
4517 */
4518 if (sctp_list_single_entry(&bp->address_list)) {
4519 addr = list_entry(bp->address_list.next,
4520 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004521 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004522 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4523 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004524 if (cnt < 0) {
4525 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004526 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004527 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004528 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004529 }
4530 }
4531
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004532 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004533 /* Protection on the bound address list is not needed since
4534 * in the socket option context we hold a socket lock and
4535 * thus the bound address list can't change.
4536 */
4537 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004538 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004539 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4540 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004541 if (space_left < addrlen) {
4542 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004543 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004544 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004545 memcpy(buf, &temp, addrlen);
4546 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004547 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004548 cnt ++;
4549 space_left -= addrlen;
4550 }
4551
4552copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004553 if (copy_to_user(to, addrs, bytes_copied)) {
4554 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004555 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004556 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004557 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4558 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004559 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004560 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004561 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004562 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004563out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004564 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004565 return err;
4566}
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4569 *
4570 * Requests that the local SCTP stack use the enclosed peer address as
4571 * the association primary. The enclosed address must be one of the
4572 * association peer's addresses.
4573 */
4574static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4575 char __user *optval, int __user *optlen)
4576{
4577 struct sctp_prim prim;
4578 struct sctp_association *asoc;
4579 struct sctp_sock *sp = sctp_sk(sk);
4580
Neil Horman408f22e2007-06-16 14:03:45 -04004581 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return -EINVAL;
4583
Neil Horman408f22e2007-06-16 14:03:45 -04004584 len = sizeof(struct sctp_prim);
4585
4586 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return -EFAULT;
4588
4589 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4590 if (!asoc)
4591 return -EINVAL;
4592
4593 if (!asoc->peer.primary_path)
4594 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004595
Al Viro8cec6b82006-11-20 17:23:01 -08004596 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4597 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
4599 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4600 (union sctp_addr *)&prim.ssp_addr);
4601
Neil Horman408f22e2007-06-16 14:03:45 -04004602 if (put_user(len, optlen))
4603 return -EFAULT;
4604 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return -EFAULT;
4606
4607 return 0;
4608}
4609
4610/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004611 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004613 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 * Indication parameter for all future INIT and INIT-ACK exchanges.
4615 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004616static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 char __user *optval, int __user *optlen)
4618{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004619 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Neil Horman408f22e2007-06-16 14:03:45 -04004621 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return -EINVAL;
4623
Neil Horman408f22e2007-06-16 14:03:45 -04004624 len = sizeof(struct sctp_setadaptation);
4625
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004626 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004627
4628 if (put_user(len, optlen))
4629 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004630 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return 0;
4634}
4635
4636/*
4637 *
4638 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4639 *
4640 * Applications that wish to use the sendto() system call may wish to
4641 * specify a default set of parameters that would normally be supplied
4642 * through the inclusion of ancillary data. This socket option allows
4643 * such an application to set the default sctp_sndrcvinfo structure.
4644
4645
4646 * The application that wishes to use this socket option simply passes
4647 * in to this call the sctp_sndrcvinfo structure defined in Section
4648 * 5.2.2) The input parameters accepted by this call include
4649 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4650 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4651 * to this call if the caller is using the UDP model.
4652 *
4653 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4654 */
4655static int sctp_getsockopt_default_send_param(struct sock *sk,
4656 int len, char __user *optval,
4657 int __user *optlen)
4658{
4659 struct sctp_sndrcvinfo info;
4660 struct sctp_association *asoc;
4661 struct sctp_sock *sp = sctp_sk(sk);
4662
Neil Horman408f22e2007-06-16 14:03:45 -04004663 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004665
4666 len = sizeof(struct sctp_sndrcvinfo);
4667
4668 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return -EFAULT;
4670
4671 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4672 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4673 return -EINVAL;
4674
4675 if (asoc) {
4676 info.sinfo_stream = asoc->default_stream;
4677 info.sinfo_flags = asoc->default_flags;
4678 info.sinfo_ppid = asoc->default_ppid;
4679 info.sinfo_context = asoc->default_context;
4680 info.sinfo_timetolive = asoc->default_timetolive;
4681 } else {
4682 info.sinfo_stream = sp->default_stream;
4683 info.sinfo_flags = sp->default_flags;
4684 info.sinfo_ppid = sp->default_ppid;
4685 info.sinfo_context = sp->default_context;
4686 info.sinfo_timetolive = sp->default_timetolive;
4687 }
4688
Neil Horman408f22e2007-06-16 14:03:45 -04004689 if (put_user(len, optlen))
4690 return -EFAULT;
4691 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return -EFAULT;
4693
4694 return 0;
4695}
4696
4697/*
4698 *
4699 * 7.1.5 SCTP_NODELAY
4700 *
4701 * Turn on/off any Nagle-like algorithm. This means that packets are
4702 * generally sent as soon as possible and no unnecessary delays are
4703 * introduced, at the cost of more packets in the network. Expects an
4704 * integer boolean flag.
4705 */
4706
4707static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4708 char __user *optval, int __user *optlen)
4709{
4710 int val;
4711
4712 if (len < sizeof(int))
4713 return -EINVAL;
4714
4715 len = sizeof(int);
4716 val = (sctp_sk(sk)->nodelay == 1);
4717 if (put_user(len, optlen))
4718 return -EFAULT;
4719 if (copy_to_user(optval, &val, len))
4720 return -EFAULT;
4721 return 0;
4722}
4723
4724/*
4725 *
4726 * 7.1.1 SCTP_RTOINFO
4727 *
4728 * The protocol parameters used to initialize and bound retransmission
4729 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4730 * and modify these parameters.
4731 * All parameters are time values, in milliseconds. A value of 0, when
4732 * modifying the parameters, indicates that the current value should not
4733 * be changed.
4734 *
4735 */
4736static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4737 char __user *optval,
4738 int __user *optlen) {
4739 struct sctp_rtoinfo rtoinfo;
4740 struct sctp_association *asoc;
4741
Neil Horman408f22e2007-06-16 14:03:45 -04004742 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 return -EINVAL;
4744
Neil Horman408f22e2007-06-16 14:03:45 -04004745 len = sizeof(struct sctp_rtoinfo);
4746
4747 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 return -EFAULT;
4749
4750 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4751
4752 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4753 return -EINVAL;
4754
4755 /* Values corresponding to the specific association. */
4756 if (asoc) {
4757 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4758 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4759 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4760 } else {
4761 /* Values corresponding to the endpoint. */
4762 struct sctp_sock *sp = sctp_sk(sk);
4763
4764 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4765 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4766 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4767 }
4768
4769 if (put_user(len, optlen))
4770 return -EFAULT;
4771
4772 if (copy_to_user(optval, &rtoinfo, len))
4773 return -EFAULT;
4774
4775 return 0;
4776}
4777
4778/*
4779 *
4780 * 7.1.2 SCTP_ASSOCINFO
4781 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004782 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 * of the association.
4784 * Returns an error if the new association retransmission value is
4785 * greater than the sum of the retransmission value of the peer.
4786 * See [SCTP] for more information.
4787 *
4788 */
4789static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4790 char __user *optval,
4791 int __user *optlen)
4792{
4793
4794 struct sctp_assocparams assocparams;
4795 struct sctp_association *asoc;
4796 struct list_head *pos;
4797 int cnt = 0;
4798
Neil Horman408f22e2007-06-16 14:03:45 -04004799 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return -EINVAL;
4801
Neil Horman408f22e2007-06-16 14:03:45 -04004802 len = sizeof(struct sctp_assocparams);
4803
4804 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 return -EFAULT;
4806
4807 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4808
4809 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4810 return -EINVAL;
4811
4812 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004813 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4815 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4816 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4817 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4818 * 1000) +
4819 (asoc->cookie_life.tv_usec
4820 / 1000);
4821
4822 list_for_each(pos, &asoc->peer.transport_addr_list) {
4823 cnt ++;
4824 }
4825
4826 assocparams.sasoc_number_peer_destinations = cnt;
4827 } else {
4828 /* Values corresponding to the endpoint */
4829 struct sctp_sock *sp = sctp_sk(sk);
4830
4831 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4832 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4833 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4834 assocparams.sasoc_cookie_life =
4835 sp->assocparams.sasoc_cookie_life;
4836 assocparams.sasoc_number_peer_destinations =
4837 sp->assocparams.
4838 sasoc_number_peer_destinations;
4839 }
4840
4841 if (put_user(len, optlen))
4842 return -EFAULT;
4843
4844 if (copy_to_user(optval, &assocparams, len))
4845 return -EFAULT;
4846
4847 return 0;
4848}
4849
4850/*
4851 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4852 *
4853 * This socket option is a boolean flag which turns on or off mapped V4
4854 * addresses. If this option is turned on and the socket is type
4855 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4856 * If this option is turned off, then no mapping will be done of V4
4857 * addresses and a user will receive both PF_INET6 and PF_INET type
4858 * addresses on the socket.
4859 */
4860static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4861 char __user *optval, int __user *optlen)
4862{
4863 int val;
4864 struct sctp_sock *sp = sctp_sk(sk);
4865
4866 if (len < sizeof(int))
4867 return -EINVAL;
4868
4869 len = sizeof(int);
4870 val = sp->v4mapped;
4871 if (put_user(len, optlen))
4872 return -EFAULT;
4873 if (copy_to_user(optval, &val, len))
4874 return -EFAULT;
4875
4876 return 0;
4877}
4878
4879/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004880 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4881 * (chapter and verse is quoted at sctp_setsockopt_context())
4882 */
4883static int sctp_getsockopt_context(struct sock *sk, int len,
4884 char __user *optval, int __user *optlen)
4885{
4886 struct sctp_assoc_value params;
4887 struct sctp_sock *sp;
4888 struct sctp_association *asoc;
4889
Neil Horman408f22e2007-06-16 14:03:45 -04004890 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004891 return -EINVAL;
4892
Neil Horman408f22e2007-06-16 14:03:45 -04004893 len = sizeof(struct sctp_assoc_value);
4894
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004895 if (copy_from_user(&params, optval, len))
4896 return -EFAULT;
4897
4898 sp = sctp_sk(sk);
4899
4900 if (params.assoc_id != 0) {
4901 asoc = sctp_id2assoc(sk, params.assoc_id);
4902 if (!asoc)
4903 return -EINVAL;
4904 params.assoc_value = asoc->default_rcv_context;
4905 } else {
4906 params.assoc_value = sp->default_rcv_context;
4907 }
4908
4909 if (put_user(len, optlen))
4910 return -EFAULT;
4911 if (copy_to_user(optval, &params, len))
4912 return -EFAULT;
4913
4914 return 0;
4915}
4916
4917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4919 *
4920 * This socket option specifies the maximum size to put in any outgoing
4921 * SCTP chunk. If a message is larger than this size it will be
4922 * fragmented by SCTP into the specified size. Note that the underlying
4923 * SCTP implementation may fragment into smaller sized chunks when the
4924 * PMTU of the underlying association is smaller than the value set by
4925 * the user.
4926 */
4927static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4928 char __user *optval, int __user *optlen)
4929{
4930 int val;
4931
4932 if (len < sizeof(int))
4933 return -EINVAL;
4934
4935 len = sizeof(int);
4936
4937 val = sctp_sk(sk)->user_frag;
4938 if (put_user(len, optlen))
4939 return -EFAULT;
4940 if (copy_to_user(optval, &val, len))
4941 return -EFAULT;
4942
4943 return 0;
4944}
4945
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004946/*
4947 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4948 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4949 */
4950static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4951 char __user *optval, int __user *optlen)
4952{
4953 int val;
4954
4955 if (len < sizeof(int))
4956 return -EINVAL;
4957
4958 len = sizeof(int);
4959
4960 val = sctp_sk(sk)->frag_interleave;
4961 if (put_user(len, optlen))
4962 return -EFAULT;
4963 if (copy_to_user(optval, &val, len))
4964 return -EFAULT;
4965
4966 return 0;
4967}
4968
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004969/*
4970 * 7.1.25. Set or Get the sctp partial delivery point
4971 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4972 */
4973static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4974 char __user *optval,
4975 int __user *optlen)
4976{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004977 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004978
4979 if (len < sizeof(u32))
4980 return -EINVAL;
4981
4982 len = sizeof(u32);
4983
4984 val = sctp_sk(sk)->pd_point;
4985 if (put_user(len, optlen))
4986 return -EFAULT;
4987 if (copy_to_user(optval, &val, len))
4988 return -EFAULT;
4989
4990 return -ENOTSUPP;
4991}
4992
Vlad Yasevich70331572007-03-23 11:34:36 -07004993/*
4994 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4995 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4996 */
4997static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4998 char __user *optval,
4999 int __user *optlen)
5000{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005001 int val;
Vlad Yasevich70331572007-03-23 11:34:36 -07005002
5003 if (len < sizeof(int))
5004 return -EINVAL;
5005
5006 len = sizeof(int);
5007
5008 val = sctp_sk(sk)->max_burst;
5009 if (put_user(len, optlen))
5010 return -EFAULT;
5011 if (copy_to_user(optval, &val, len))
5012 return -EFAULT;
5013
5014 return -ENOTSUPP;
5015}
5016
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005017static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5018 char __user *optval, int __user *optlen)
5019{
5020 struct sctp_hmac_algo_param *hmacs;
5021 __u16 param_len;
5022
5023 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5024 param_len = ntohs(hmacs->param_hdr.length);
5025
5026 if (len < param_len)
5027 return -EINVAL;
5028 if (put_user(len, optlen))
5029 return -EFAULT;
5030 if (copy_to_user(optval, hmacs->hmac_ids, len))
5031 return -EFAULT;
5032
5033 return 0;
5034}
5035
5036static int sctp_getsockopt_active_key(struct sock *sk, int len,
5037 char __user *optval, int __user *optlen)
5038{
5039 struct sctp_authkeyid val;
5040 struct sctp_association *asoc;
5041
5042 if (len < sizeof(struct sctp_authkeyid))
5043 return -EINVAL;
5044 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5045 return -EFAULT;
5046
5047 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5048 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5049 return -EINVAL;
5050
5051 if (asoc)
5052 val.scact_keynumber = asoc->active_key_id;
5053 else
5054 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5055
5056 return 0;
5057}
5058
5059static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5060 char __user *optval, int __user *optlen)
5061{
Al Viro411223c2007-10-14 19:21:20 +01005062 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005063 struct sctp_authchunks val;
5064 struct sctp_association *asoc;
5065 struct sctp_chunks_param *ch;
5066 char __user *to;
5067
5068 if (len <= sizeof(struct sctp_authchunks))
5069 return -EINVAL;
5070
Al Viro411223c2007-10-14 19:21:20 +01005071 if (copy_from_user(&val, p, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005072 return -EFAULT;
5073
Al Viro411223c2007-10-14 19:21:20 +01005074 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005075 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5076 if (!asoc)
5077 return -EINVAL;
5078
5079 ch = asoc->peer.peer_chunks;
5080
5081 /* See if the user provided enough room for all the data */
5082 if (len < ntohs(ch->param_hdr.length))
5083 return -EINVAL;
5084
5085 len = ntohs(ch->param_hdr.length);
5086 if (put_user(len, optlen))
5087 return -EFAULT;
5088 if (copy_to_user(to, ch->chunks, len))
5089 return -EFAULT;
5090
5091 return 0;
5092}
5093
5094static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5095 char __user *optval, int __user *optlen)
5096{
Al Viro411223c2007-10-14 19:21:20 +01005097 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005098 struct sctp_authchunks val;
5099 struct sctp_association *asoc;
5100 struct sctp_chunks_param *ch;
5101 char __user *to;
5102
5103 if (len <= sizeof(struct sctp_authchunks))
5104 return -EINVAL;
5105
Al Viro411223c2007-10-14 19:21:20 +01005106 if (copy_from_user(&val, p, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005107 return -EFAULT;
5108
Al Viro411223c2007-10-14 19:21:20 +01005109 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005110 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5111 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5112 return -EINVAL;
5113
5114 if (asoc)
5115 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5116 else
5117 ch = sctp_sk(sk)->ep->auth_chunk_list;
5118
5119 if (len < ntohs(ch->param_hdr.length))
5120 return -EINVAL;
5121
5122 len = ntohs(ch->param_hdr.length);
5123 if (put_user(len, optlen))
5124 return -EFAULT;
5125 if (copy_to_user(to, ch->chunks, len))
5126 return -EFAULT;
5127
5128 return 0;
5129}
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5132 char __user *optval, int __user *optlen)
5133{
5134 int retval = 0;
5135 int len;
5136
Frank Filz3f7a87d2005-06-20 13:14:57 -07005137 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5138 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
5140 /* I can hardly begin to describe how wrong this is. This is
5141 * so broken as to be worse than useless. The API draft
5142 * REALLY is NOT helpful here... I am not convinced that the
5143 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5144 * are at all well-founded.
5145 */
5146 if (level != SOL_SCTP) {
5147 struct sctp_af *af = sctp_sk(sk)->pf->af;
5148
5149 retval = af->getsockopt(sk, level, optname, optval, optlen);
5150 return retval;
5151 }
5152
5153 if (get_user(len, optlen))
5154 return -EFAULT;
5155
5156 sctp_lock_sock(sk);
5157
5158 switch (optname) {
5159 case SCTP_STATUS:
5160 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5161 break;
5162 case SCTP_DISABLE_FRAGMENTS:
5163 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5164 optlen);
5165 break;
5166 case SCTP_EVENTS:
5167 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5168 break;
5169 case SCTP_AUTOCLOSE:
5170 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5171 break;
5172 case SCTP_SOCKOPT_PEELOFF:
5173 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5174 break;
5175 case SCTP_PEER_ADDR_PARAMS:
5176 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5177 optlen);
5178 break;
Frank Filz77086102005-12-22 11:37:30 -08005179 case SCTP_DELAYED_ACK_TIME:
5180 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
5181 optlen);
5182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 case SCTP_INITMSG:
5184 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5185 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005186 case SCTP_GET_PEER_ADDRS_NUM_OLD:
5187 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
5188 optlen);
5189 break;
5190 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
5191 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
5192 optlen);
5193 break;
5194 case SCTP_GET_PEER_ADDRS_OLD:
5195 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 optlen);
5197 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005198 case SCTP_GET_LOCAL_ADDRS_OLD:
5199 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 optlen);
5201 break;
5202 case SCTP_GET_PEER_ADDRS:
5203 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5204 optlen);
5205 break;
5206 case SCTP_GET_LOCAL_ADDRS:
5207 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5208 optlen);
5209 break;
5210 case SCTP_DEFAULT_SEND_PARAM:
5211 retval = sctp_getsockopt_default_send_param(sk, len,
5212 optval, optlen);
5213 break;
5214 case SCTP_PRIMARY_ADDR:
5215 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5216 break;
5217 case SCTP_NODELAY:
5218 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5219 break;
5220 case SCTP_RTOINFO:
5221 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5222 break;
5223 case SCTP_ASSOCINFO:
5224 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5225 break;
5226 case SCTP_I_WANT_MAPPED_V4_ADDR:
5227 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5228 break;
5229 case SCTP_MAXSEG:
5230 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5231 break;
5232 case SCTP_GET_PEER_ADDR_INFO:
5233 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5234 optlen);
5235 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005236 case SCTP_ADAPTATION_LAYER:
5237 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 optlen);
5239 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005240 case SCTP_CONTEXT:
5241 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5242 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005243 case SCTP_FRAGMENT_INTERLEAVE:
5244 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5245 optlen);
5246 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005247 case SCTP_PARTIAL_DELIVERY_POINT:
5248 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5249 optlen);
5250 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005251 case SCTP_MAX_BURST:
5252 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5253 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005254 case SCTP_AUTH_KEY:
5255 case SCTP_AUTH_CHUNK:
5256 case SCTP_AUTH_DELETE_KEY:
5257 retval = -EOPNOTSUPP;
5258 break;
5259 case SCTP_HMAC_IDENT:
5260 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5261 break;
5262 case SCTP_AUTH_ACTIVE_KEY:
5263 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5264 break;
5265 case SCTP_PEER_AUTH_CHUNKS:
5266 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5267 optlen);
5268 break;
5269 case SCTP_LOCAL_AUTH_CHUNKS:
5270 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5271 optlen);
5272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 default:
5274 retval = -ENOPROTOOPT;
5275 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
5278 sctp_release_sock(sk);
5279 return retval;
5280}
5281
5282static void sctp_hash(struct sock *sk)
5283{
5284 /* STUB */
5285}
5286
5287static void sctp_unhash(struct sock *sk)
5288{
5289 /* STUB */
5290}
5291
5292/* Check if port is acceptable. Possibly find first available port.
5293 *
5294 * The port hash table (contained in the 'global' SCTP protocol storage
5295 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5296 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5297 * list (the list number is the port number hashed out, so as you
5298 * would expect from a hash function, all the ports in a given list have
5299 * such a number that hashes out to the same list number; you were
5300 * expecting that, right?); so each list has a set of ports, with a
5301 * link to the socket (struct sock) that uses it, the port number and
5302 * a fastreuse flag (FIXME: NPI ipg).
5303 */
5304static struct sctp_bind_bucket *sctp_bucket_create(
5305 struct sctp_bind_hashbucket *head, unsigned short snum);
5306
5307static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5308{
5309 struct sctp_bind_hashbucket *head; /* hash list */
5310 struct sctp_bind_bucket *pp; /* hash list port iterator */
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005311 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 unsigned short snum;
5313 int ret;
5314
Al Viro04afd8b2006-11-20 17:02:01 -08005315 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5318 sctp_local_bh_disable();
5319
5320 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07005321 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07005322 int low, high, remaining, index;
5323 unsigned int rover;
5324
5325 inet_get_local_port_range(&low, &high);
5326 remaining = (high - low) + 1;
5327 rover = net_random() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 do {
5330 rover++;
5331 if ((rover < low) || (rover > high))
5332 rover = low;
5333 index = sctp_phashfn(rover);
5334 head = &sctp_port_hashtable[index];
5335 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005336 sctp_for_each_hentry(pp, node, &head->chain)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 if (pp->port == rover)
5338 goto next;
5339 break;
5340 next:
5341 sctp_spin_unlock(&head->lock);
5342 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 /* Exhausted local port range during search? */
5345 ret = 1;
5346 if (remaining <= 0)
5347 goto fail;
5348
5349 /* OK, here is the one we will use. HEAD (the port
5350 * hash table list entry) is non-NULL and we hold it's
5351 * mutex.
5352 */
5353 snum = rover;
5354 } else {
5355 /* We are given an specific port number; we verify
5356 * that it is not being used. If it is used, we will
5357 * exahust the search in the hash list corresponding
5358 * to the port number (snum) - we detect that with the
5359 * port iterator, pp being NULL.
5360 */
5361 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5362 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005363 sctp_for_each_hentry(pp, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (pp->port == snum)
5365 goto pp_found;
5366 }
5367 }
5368 pp = NULL;
5369 goto pp_not_found;
5370pp_found:
5371 if (!hlist_empty(&pp->owner)) {
5372 /* We had a port hash table hit - there is an
5373 * available port (pp != NULL) and it is being
5374 * used by other socket (pp->owner not empty); that other
5375 * socket is going to be sk2.
5376 */
5377 int reuse = sk->sk_reuse;
5378 struct sock *sk2;
5379 struct hlist_node *node;
5380
5381 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005382 if (pp->fastreuse && sk->sk_reuse &&
5383 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 goto success;
5385
5386 /* Run through the list of sockets bound to the port
5387 * (pp->port) [via the pointers bind_next and
5388 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5389 * we get the endpoint they describe and run through
5390 * the endpoint's list of IP (v4 or v6) addresses,
5391 * comparing each of the addresses with the address of
5392 * the socket sk. If we find a match, then that means
5393 * that this port/socket (sk) combination are already
5394 * in an endpoint.
5395 */
5396 sk_for_each_bound(sk2, node, &pp->owner) {
5397 struct sctp_endpoint *ep2;
5398 ep2 = sctp_sk(sk2)->ep;
5399
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005400 if (reuse && sk2->sk_reuse &&
5401 sk2->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 continue;
5403
Al Viro7e1e4a22006-11-20 17:05:43 -08005404 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 sctp_sk(sk))) {
5406 ret = (long)sk2;
5407 goto fail_unlock;
5408 }
5409 }
5410 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5411 }
5412pp_not_found:
5413 /* If there was a hash table miss, create a new port. */
5414 ret = 1;
5415 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5416 goto fail_unlock;
5417
5418 /* In either case (hit or miss), make sure fastreuse is 1 only
5419 * if sk->sk_reuse is too (that is, if the caller requested
5420 * SO_REUSEADDR on this socket -sk-).
5421 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005422 if (hlist_empty(&pp->owner)) {
5423 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5424 pp->fastreuse = 1;
5425 else
5426 pp->fastreuse = 0;
5427 } else if (pp->fastreuse &&
5428 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 pp->fastreuse = 0;
5430
5431 /* We are set, so fill up all the data in the hash table
5432 * entry, tie the socket list information with the rest of the
5433 * sockets FIXME: Blurry, NPI (ipg).
5434 */
5435success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (!sctp_sk(sk)->bind_hash) {
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005437 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 sk_add_bind_node(sk, &pp->owner);
5439 sctp_sk(sk)->bind_hash = pp;
5440 }
5441 ret = 0;
5442
5443fail_unlock:
5444 sctp_spin_unlock(&head->lock);
5445
5446fail:
5447 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 return ret;
5449}
5450
5451/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5452 * port is requested.
5453 */
5454static int sctp_get_port(struct sock *sk, unsigned short snum)
5455{
5456 long ret;
5457 union sctp_addr addr;
5458 struct sctp_af *af = sctp_sk(sk)->pf->af;
5459
5460 /* Set up a dummy address struct from the sk. */
5461 af->from_sk(&addr, sk);
5462 addr.v4.sin_port = htons(snum);
5463
5464 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5465 ret = sctp_get_port_local(sk, &addr);
5466
5467 return (ret ? 1 : 0);
5468}
5469
5470/*
5471 * 3.1.3 listen() - UDP Style Syntax
5472 *
5473 * By default, new associations are not accepted for UDP style sockets.
5474 * An application uses listen() to mark a socket as being able to
5475 * accept new associations.
5476 */
5477SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5478{
5479 struct sctp_sock *sp = sctp_sk(sk);
5480 struct sctp_endpoint *ep = sp->ep;
5481
5482 /* Only UDP style sockets that are not peeled off are allowed to
5483 * listen().
5484 */
5485 if (!sctp_style(sk, UDP))
5486 return -EINVAL;
5487
5488 /* If backlog is zero, disable listening. */
5489 if (!backlog) {
5490 if (sctp_sstate(sk, CLOSED))
5491 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 sctp_unhash_endpoint(ep);
5494 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
5497
5498 /* Return if we are already listening. */
5499 if (sctp_sstate(sk, LISTENING))
5500 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 /*
5503 * If a bind() or sctp_bindx() is not called prior to a listen()
5504 * call that allows new associations to be accepted, the system
5505 * picks an ephemeral port and will choose an address set equivalent
5506 * to binding with a wildcard address.
5507 *
5508 * This is not currently spelled out in the SCTP sockets
5509 * extensions draft, but follows the practice as seen in TCP
5510 * sockets.
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005511 *
5512 * Additionally, turn off fastreuse flag since we are not listening
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005514 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (!ep->base.bind_addr.port) {
5516 if (sctp_autobind(sk))
5517 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005518 } else
5519 sctp_sk(sk)->bind_hash->fastreuse = 0;
5520
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 sctp_hash_endpoint(ep);
5522 return 0;
5523}
5524
5525/*
5526 * 4.1.3 listen() - TCP Style Syntax
5527 *
5528 * Applications uses listen() to ready the SCTP endpoint for accepting
5529 * inbound associations.
5530 */
5531SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5532{
5533 struct sctp_sock *sp = sctp_sk(sk);
5534 struct sctp_endpoint *ep = sp->ep;
5535
5536 /* If backlog is zero, disable listening. */
5537 if (!backlog) {
5538 if (sctp_sstate(sk, CLOSED))
5539 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 sctp_unhash_endpoint(ep);
5542 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 }
5545
5546 if (sctp_sstate(sk, LISTENING))
5547 return 0;
5548
5549 /*
5550 * If a bind() or sctp_bindx() is not called prior to a listen()
5551 * call that allows new associations to be accepted, the system
5552 * picks an ephemeral port and will choose an address set equivalent
5553 * to binding with a wildcard address.
5554 *
5555 * This is not currently spelled out in the SCTP sockets
5556 * extensions draft, but follows the practice as seen in TCP
5557 * sockets.
5558 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005559 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 if (!ep->base.bind_addr.port) {
5561 if (sctp_autobind(sk))
5562 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005563 } else
5564 sctp_sk(sk)->bind_hash->fastreuse = 0;
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 sk->sk_max_ack_backlog = backlog;
5567 sctp_hash_endpoint(ep);
5568 return 0;
5569}
5570
5571/*
5572 * Move a socket to LISTENING state.
5573 */
5574int sctp_inet_listen(struct socket *sock, int backlog)
5575{
5576 struct sock *sk = sock->sk;
Herbert Xu1b489e12006-08-20 15:07:14 +10005577 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 int err = -EINVAL;
5579
5580 if (unlikely(backlog < 0))
5581 goto out;
5582
5583 sctp_lock_sock(sk);
5584
5585 if (sock->state != SS_UNCONNECTED)
5586 goto out;
5587
5588 /* Allocate HMAC for generating cookie. */
5589 if (sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005590 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005591 if (IS_ERR(tfm)) {
5592 if (net_ratelimit()) {
5593 printk(KERN_INFO
5594 "SCTP: failed to load transform for %s: %ld\n",
5595 sctp_hmac_alg, PTR_ERR(tfm));
5596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 err = -ENOSYS;
5598 goto out;
5599 }
5600 }
5601
5602 switch (sock->type) {
5603 case SOCK_SEQPACKET:
5604 err = sctp_seqpacket_listen(sk, backlog);
5605 break;
5606 case SOCK_STREAM:
5607 err = sctp_stream_listen(sk, backlog);
5608 break;
5609 default:
5610 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005611 }
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 if (err)
5614 goto cleanup;
5615
5616 /* Store away the transform reference. */
5617 sctp_sk(sk)->hmac = tfm;
5618out:
5619 sctp_release_sock(sk);
5620 return err;
5621cleanup:
Herbert Xu1b489e12006-08-20 15:07:14 +10005622 crypto_free_hash(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 goto out;
5624}
5625
5626/*
5627 * This function is done by modeling the current datagram_poll() and the
5628 * tcp_poll(). Note that, based on these implementations, we don't
5629 * lock the socket in this function, even though it seems that,
5630 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005631 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 * in this place. We assume that we don't need locks either until proven
5633 * otherwise.
5634 *
5635 * Another thing to note is that we include the Async I/O support
5636 * here, again, by modeling the current TCP/UDP code. We don't have
5637 * a good way to test with it yet.
5638 */
5639unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5640{
5641 struct sock *sk = sock->sk;
5642 struct sctp_sock *sp = sctp_sk(sk);
5643 unsigned int mask;
5644
5645 poll_wait(file, sk->sk_sleep, wait);
5646
5647 /* A TCP-style listening socket becomes readable when the accept queue
5648 * is not empty.
5649 */
5650 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5651 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005652 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
5654 mask = 0;
5655
5656 /* Is there any exceptional events? */
5657 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5658 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08005659 if (sk->sk_shutdown & RCV_SHUTDOWN)
5660 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 if (sk->sk_shutdown == SHUTDOWN_MASK)
5662 mask |= POLLHUP;
5663
5664 /* Is it readable? Reconsider this code with TCP-style support. */
5665 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5666 (sk->sk_shutdown & RCV_SHUTDOWN))
5667 mask |= POLLIN | POLLRDNORM;
5668
5669 /* The association is either gone or not ready. */
5670 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5671 return mask;
5672
5673 /* Is it writable? */
5674 if (sctp_writeable(sk)) {
5675 mask |= POLLOUT | POLLWRNORM;
5676 } else {
5677 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5678 /*
5679 * Since the socket is not locked, the buffer
5680 * might be made available after the writeable check and
5681 * before the bit is set. This could cause a lost I/O
5682 * signal. tcp_poll() has a race breaker for this race
5683 * condition. Based on their implementation, we put
5684 * in the following code to cover it as well.
5685 */
5686 if (sctp_writeable(sk))
5687 mask |= POLLOUT | POLLWRNORM;
5688 }
5689 return mask;
5690}
5691
5692/********************************************************************
5693 * 2nd Level Abstractions
5694 ********************************************************************/
5695
5696static struct sctp_bind_bucket *sctp_bucket_create(
5697 struct sctp_bind_hashbucket *head, unsigned short snum)
5698{
5699 struct sctp_bind_bucket *pp;
5700
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08005701 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 SCTP_DBG_OBJCNT_INC(bind_bucket);
5703 if (pp) {
5704 pp->port = snum;
5705 pp->fastreuse = 0;
5706 INIT_HLIST_HEAD(&pp->owner);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005707 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 }
5709 return pp;
5710}
5711
5712/* Caller must hold hashbucket lock for this tb with local BH disabled */
5713static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5714{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005715 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005716 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 kmem_cache_free(sctp_bucket_cachep, pp);
5718 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5719 }
5720}
5721
5722/* Release this socket's reference to a local port. */
5723static inline void __sctp_put_port(struct sock *sk)
5724{
5725 struct sctp_bind_hashbucket *head =
5726 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5727 struct sctp_bind_bucket *pp;
5728
5729 sctp_spin_lock(&head->lock);
5730 pp = sctp_sk(sk)->bind_hash;
5731 __sk_del_bind_node(sk);
5732 sctp_sk(sk)->bind_hash = NULL;
5733 inet_sk(sk)->num = 0;
5734 sctp_bucket_destroy(pp);
5735 sctp_spin_unlock(&head->lock);
5736}
5737
5738void sctp_put_port(struct sock *sk)
5739{
5740 sctp_local_bh_disable();
5741 __sctp_put_port(sk);
5742 sctp_local_bh_enable();
5743}
5744
5745/*
5746 * The system picks an ephemeral port and choose an address set equivalent
5747 * to binding with a wildcard address.
5748 * One of those addresses will be the primary address for the association.
5749 * This automatically enables the multihoming capability of SCTP.
5750 */
5751static int sctp_autobind(struct sock *sk)
5752{
5753 union sctp_addr autoaddr;
5754 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08005755 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 /* Initialize a local sockaddr structure to INADDR_ANY. */
5758 af = sctp_sk(sk)->pf->af;
5759
5760 port = htons(inet_sk(sk)->num);
5761 af->inaddr_any(&autoaddr, port);
5762
5763 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5764}
5765
5766/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5767 *
5768 * From RFC 2292
5769 * 4.2 The cmsghdr Structure *
5770 *
5771 * When ancillary data is sent or received, any number of ancillary data
5772 * objects can be specified by the msg_control and msg_controllen members of
5773 * the msghdr structure, because each object is preceded by
5774 * a cmsghdr structure defining the object's length (the cmsg_len member).
5775 * Historically Berkeley-derived implementations have passed only one object
5776 * at a time, but this API allows multiple objects to be
5777 * passed in a single call to sendmsg() or recvmsg(). The following example
5778 * shows two ancillary data objects in a control buffer.
5779 *
5780 * |<--------------------------- msg_controllen -------------------------->|
5781 * | |
5782 *
5783 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5784 *
5785 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5786 * | | |
5787 *
5788 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5789 *
5790 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5791 * | | | | |
5792 *
5793 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5794 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5795 *
5796 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5797 *
5798 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5799 * ^
5800 * |
5801 *
5802 * msg_control
5803 * points here
5804 */
5805SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5806 sctp_cmsgs_t *cmsgs)
5807{
5808 struct cmsghdr *cmsg;
5809
5810 for (cmsg = CMSG_FIRSTHDR(msg);
5811 cmsg != NULL;
5812 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5813 if (!CMSG_OK(msg, cmsg))
5814 return -EINVAL;
5815
5816 /* Should we parse this header or ignore? */
5817 if (cmsg->cmsg_level != IPPROTO_SCTP)
5818 continue;
5819
5820 /* Strictly check lengths following example in SCM code. */
5821 switch (cmsg->cmsg_type) {
5822 case SCTP_INIT:
5823 /* SCTP Socket API Extension
5824 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5825 *
5826 * This cmsghdr structure provides information for
5827 * initializing new SCTP associations with sendmsg().
5828 * The SCTP_INITMSG socket option uses this same data
5829 * structure. This structure is not used for
5830 * recvmsg().
5831 *
5832 * cmsg_level cmsg_type cmsg_data[]
5833 * ------------ ------------ ----------------------
5834 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5835 */
5836 if (cmsg->cmsg_len !=
5837 CMSG_LEN(sizeof(struct sctp_initmsg)))
5838 return -EINVAL;
5839 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5840 break;
5841
5842 case SCTP_SNDRCV:
5843 /* SCTP Socket API Extension
5844 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5845 *
5846 * This cmsghdr structure specifies SCTP options for
5847 * sendmsg() and describes SCTP header information
5848 * about a received message through recvmsg().
5849 *
5850 * cmsg_level cmsg_type cmsg_data[]
5851 * ------------ ------------ ----------------------
5852 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5853 */
5854 if (cmsg->cmsg_len !=
5855 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5856 return -EINVAL;
5857
5858 cmsgs->info =
5859 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5860
5861 /* Minimally, validate the sinfo_flags. */
5862 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005863 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5864 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 return -EINVAL;
5866 break;
5867
5868 default:
5869 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 }
5872 return 0;
5873}
5874
5875/*
5876 * Wait for a packet..
5877 * Note: This function is the same function as in core/datagram.c
5878 * with a few modifications to make lksctp work.
5879 */
5880static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5881{
5882 int error;
5883 DEFINE_WAIT(wait);
5884
5885 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5886
5887 /* Socket errors? */
5888 error = sock_error(sk);
5889 if (error)
5890 goto out;
5891
5892 if (!skb_queue_empty(&sk->sk_receive_queue))
5893 goto ready;
5894
5895 /* Socket shut down? */
5896 if (sk->sk_shutdown & RCV_SHUTDOWN)
5897 goto out;
5898
5899 /* Sequenced packets can come disconnected. If so we report the
5900 * problem.
5901 */
5902 error = -ENOTCONN;
5903
5904 /* Is there a good reason to think that we may receive some data? */
5905 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5906 goto out;
5907
5908 /* Handle signals. */
5909 if (signal_pending(current))
5910 goto interrupted;
5911
5912 /* Let another process have a go. Since we are going to sleep
5913 * anyway. Note: This may cause odd behaviors if the message
5914 * does not fit in the user's buffer, but this seems to be the
5915 * only way to honor MSG_DONTWAIT realistically.
5916 */
5917 sctp_release_sock(sk);
5918 *timeo_p = schedule_timeout(*timeo_p);
5919 sctp_lock_sock(sk);
5920
5921ready:
5922 finish_wait(sk->sk_sleep, &wait);
5923 return 0;
5924
5925interrupted:
5926 error = sock_intr_errno(*timeo_p);
5927
5928out:
5929 finish_wait(sk->sk_sleep, &wait);
5930 *err = error;
5931 return error;
5932}
5933
5934/* Receive a datagram.
5935 * Note: This is pretty much the same routine as in core/datagram.c
5936 * with a few changes to make lksctp work.
5937 */
5938static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5939 int noblock, int *err)
5940{
5941 int error;
5942 struct sk_buff *skb;
5943 long timeo;
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 timeo = sock_rcvtimeo(sk, noblock);
5946
5947 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5948 timeo, MAX_SCHEDULE_TIMEOUT);
5949
5950 do {
5951 /* Again only user level code calls this function,
5952 * so nothing interrupt level
5953 * will suddenly eat the receive_queue.
5954 *
5955 * Look at current nfs client by the way...
5956 * However, this function was corrent in any case. 8)
5957 */
5958 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005959 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 skb = skb_peek(&sk->sk_receive_queue);
5961 if (skb)
5962 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005963 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 } else {
5965 skb = skb_dequeue(&sk->sk_receive_queue);
5966 }
5967
5968 if (skb)
5969 return skb;
5970
Neil Horman6736dc32005-12-02 20:30:06 -08005971 /* Caller is allowed not to check sk->sk_err before calling. */
5972 error = sock_error(sk);
5973 if (error)
5974 goto no_packet;
5975
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 if (sk->sk_shutdown & RCV_SHUTDOWN)
5977 break;
5978
5979 /* User doesn't want to wait. */
5980 error = -EAGAIN;
5981 if (!timeo)
5982 goto no_packet;
5983 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5984
5985 return NULL;
5986
5987no_packet:
5988 *err = error;
5989 return NULL;
5990}
5991
5992/* If sndbuf has changed, wake up per association sndbuf waiters. */
5993static void __sctp_write_space(struct sctp_association *asoc)
5994{
5995 struct sock *sk = asoc->base.sk;
5996 struct socket *sock = sk->sk_socket;
5997
5998 if ((sctp_wspace(asoc) > 0) && sock) {
5999 if (waitqueue_active(&asoc->wait))
6000 wake_up_interruptible(&asoc->wait);
6001
6002 if (sctp_writeable(sk)) {
6003 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
6004 wake_up_interruptible(sk->sk_sleep);
6005
6006 /* Note that we try to include the Async I/O support
6007 * here by modeling from the current TCP/UDP code.
6008 * We have not tested with it yet.
6009 */
6010 if (sock->fasync_list &&
6011 !(sk->sk_shutdown & SEND_SHUTDOWN))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08006012 sock_wake_async(sock,
6013 SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 }
6015 }
6016}
6017
6018/* Do accounting for the sndbuf space.
6019 * Decrement the used sndbuf space of the corresponding association by the
6020 * data size which was just transmitted(freed).
6021 */
6022static void sctp_wfree(struct sk_buff *skb)
6023{
6024 struct sctp_association *asoc;
6025 struct sctp_chunk *chunk;
6026 struct sock *sk;
6027
6028 /* Get the saved chunk pointer. */
6029 chunk = *((struct sctp_chunk **)(skb->cb));
6030 asoc = chunk->asoc;
6031 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006032 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6033 sizeof(struct sk_buff) +
6034 sizeof(struct sctp_chunk);
6035
Neil Horman4eb701d2005-04-28 12:02:04 -07006036 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6037
Neil Horman4d93df02007-08-15 16:07:44 -07006038 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006039 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07006040 */
6041 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006042 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07006043
Neil Horman4eb701d2005-04-28 12:02:04 -07006044 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 __sctp_write_space(asoc);
6046
6047 sctp_association_put(asoc);
6048}
6049
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006050/* Do accounting for the receive space on the socket.
6051 * Accounting for the association is done in ulpevent.c
6052 * We set this as a destructor for the cloned data skbs so that
6053 * accounting is done at the correct time.
6054 */
6055void sctp_sock_rfree(struct sk_buff *skb)
6056{
6057 struct sock *sk = skb->sk;
6058 struct sctp_ulpevent *event = sctp_skb2event(skb);
6059
6060 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006061
6062 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006063 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07006064 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006065 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006066}
6067
6068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069/* Helper function to wait for space in the sndbuf. */
6070static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6071 size_t msg_len)
6072{
6073 struct sock *sk = asoc->base.sk;
6074 int err = 0;
6075 long current_timeo = *timeo_p;
6076 DEFINE_WAIT(wait);
6077
6078 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006079 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 /* Increment the association's refcnt. */
6082 sctp_association_hold(asoc);
6083
6084 /* Wait on the association specific sndbuf space. */
6085 for (;;) {
6086 prepare_to_wait_exclusive(&asoc->wait, &wait,
6087 TASK_INTERRUPTIBLE);
6088 if (!*timeo_p)
6089 goto do_nonblock;
6090 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6091 asoc->base.dead)
6092 goto do_error;
6093 if (signal_pending(current))
6094 goto do_interrupted;
6095 if (msg_len <= sctp_wspace(asoc))
6096 break;
6097
6098 /* Let another process have a go. Since we are going
6099 * to sleep anyway.
6100 */
6101 sctp_release_sock(sk);
6102 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006103 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 sctp_lock_sock(sk);
6105
6106 *timeo_p = current_timeo;
6107 }
6108
6109out:
6110 finish_wait(&asoc->wait, &wait);
6111
6112 /* Release the association's refcnt. */
6113 sctp_association_put(asoc);
6114
6115 return err;
6116
6117do_error:
6118 err = -EPIPE;
6119 goto out;
6120
6121do_interrupted:
6122 err = sock_intr_errno(*timeo_p);
6123 goto out;
6124
6125do_nonblock:
6126 err = -EAGAIN;
6127 goto out;
6128}
6129
6130/* If socket sndbuf has changed, wake up all per association waiters. */
6131void sctp_write_space(struct sock *sk)
6132{
6133 struct sctp_association *asoc;
6134 struct list_head *pos;
6135
6136 /* Wake up the tasks in each wait queue. */
6137 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
6138 asoc = list_entry(pos, struct sctp_association, asocs);
6139 __sctp_write_space(asoc);
6140 }
6141}
6142
6143/* Is there any sndbuf space available on the socket?
6144 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006145 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 * associations on the same socket. For a UDP-style socket with
6147 * multiple associations, it is possible for it to be "unwriteable"
6148 * prematurely. I assume that this is acceptable because
6149 * a premature "unwriteable" is better than an accidental "writeable" which
6150 * would cause an unwanted block under certain circumstances. For the 1-1
6151 * UDP-style sockets or TCP-style sockets, this code should work.
6152 * - Daisy
6153 */
6154static int sctp_writeable(struct sock *sk)
6155{
6156 int amt = 0;
6157
Neil Horman9bffc4a2005-12-19 14:24:40 -08006158 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 if (amt < 0)
6160 amt = 0;
6161 return amt;
6162}
6163
6164/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6165 * returns immediately with EINPROGRESS.
6166 */
6167static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6168{
6169 struct sock *sk = asoc->base.sk;
6170 int err = 0;
6171 long current_timeo = *timeo_p;
6172 DEFINE_WAIT(wait);
6173
6174 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
6175 (long)(*timeo_p));
6176
6177 /* Increment the association's refcnt. */
6178 sctp_association_hold(asoc);
6179
6180 for (;;) {
6181 prepare_to_wait_exclusive(&asoc->wait, &wait,
6182 TASK_INTERRUPTIBLE);
6183 if (!*timeo_p)
6184 goto do_nonblock;
6185 if (sk->sk_shutdown & RCV_SHUTDOWN)
6186 break;
6187 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6188 asoc->base.dead)
6189 goto do_error;
6190 if (signal_pending(current))
6191 goto do_interrupted;
6192
6193 if (sctp_state(asoc, ESTABLISHED))
6194 break;
6195
6196 /* Let another process have a go. Since we are going
6197 * to sleep anyway.
6198 */
6199 sctp_release_sock(sk);
6200 current_timeo = schedule_timeout(current_timeo);
6201 sctp_lock_sock(sk);
6202
6203 *timeo_p = current_timeo;
6204 }
6205
6206out:
6207 finish_wait(&asoc->wait, &wait);
6208
6209 /* Release the association's refcnt. */
6210 sctp_association_put(asoc);
6211
6212 return err;
6213
6214do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006215 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 err = -ETIMEDOUT;
6217 else
6218 err = -ECONNREFUSED;
6219 goto out;
6220
6221do_interrupted:
6222 err = sock_intr_errno(*timeo_p);
6223 goto out;
6224
6225do_nonblock:
6226 err = -EINPROGRESS;
6227 goto out;
6228}
6229
6230static int sctp_wait_for_accept(struct sock *sk, long timeo)
6231{
6232 struct sctp_endpoint *ep;
6233 int err = 0;
6234 DEFINE_WAIT(wait);
6235
6236 ep = sctp_sk(sk)->ep;
6237
6238
6239 for (;;) {
6240 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
6241 TASK_INTERRUPTIBLE);
6242
6243 if (list_empty(&ep->asocs)) {
6244 sctp_release_sock(sk);
6245 timeo = schedule_timeout(timeo);
6246 sctp_lock_sock(sk);
6247 }
6248
6249 err = -EINVAL;
6250 if (!sctp_sstate(sk, LISTENING))
6251 break;
6252
6253 err = 0;
6254 if (!list_empty(&ep->asocs))
6255 break;
6256
6257 err = sock_intr_errno(timeo);
6258 if (signal_pending(current))
6259 break;
6260
6261 err = -EAGAIN;
6262 if (!timeo)
6263 break;
6264 }
6265
6266 finish_wait(sk->sk_sleep, &wait);
6267
6268 return err;
6269}
6270
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006271static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272{
6273 DEFINE_WAIT(wait);
6274
6275 do {
6276 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
6277 if (list_empty(&sctp_sk(sk)->ep->asocs))
6278 break;
6279 sctp_release_sock(sk);
6280 timeout = schedule_timeout(timeout);
6281 sctp_lock_sock(sk);
6282 } while (!signal_pending(current) && timeout);
6283
6284 finish_wait(sk->sk_sleep, &wait);
6285}
6286
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006287static void sctp_sock_rfree_frag(struct sk_buff *skb)
6288{
6289 struct sk_buff *frag;
6290
6291 if (!skb->data_len)
6292 goto done;
6293
6294 /* Don't forget the fragments. */
6295 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6296 sctp_sock_rfree_frag(frag);
6297
6298done:
6299 sctp_sock_rfree(skb);
6300}
6301
6302static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6303{
6304 struct sk_buff *frag;
6305
6306 if (!skb->data_len)
6307 goto done;
6308
6309 /* Don't forget the fragments. */
6310 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6311 sctp_skb_set_owner_r_frag(frag, sk);
6312
6313done:
6314 sctp_skb_set_owner_r(skb, sk);
6315}
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317/* Populate the fields of the newsk from the oldsk and migrate the assoc
6318 * and its messages to the newsk.
6319 */
6320static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6321 struct sctp_association *assoc,
6322 sctp_socket_type_t type)
6323{
6324 struct sctp_sock *oldsp = sctp_sk(oldsk);
6325 struct sctp_sock *newsp = sctp_sk(newsk);
6326 struct sctp_bind_bucket *pp; /* hash list port iterator */
6327 struct sctp_endpoint *newep = newsp->ep;
6328 struct sk_buff *skb, *tmp;
6329 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006330 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
6332 /* Migrate socket buffer sizes and all the socket level options to the
6333 * new socket.
6334 */
6335 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6336 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6337 /* Brute force copy old sctp opt. */
6338 inet_sk_copy_descendant(newsk, oldsk);
6339
6340 /* Restore the ep value that was overwritten with the above structure
6341 * copy.
6342 */
6343 newsp->ep = newep;
6344 newsp->hmac = NULL;
6345
6346 /* Hook this new socket in to the bind_hash list. */
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006347 head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->num)];
6348 sctp_local_bh_disable();
6349 sctp_spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 pp = sctp_sk(oldsk)->bind_hash;
6351 sk_add_bind_node(newsk, &pp->owner);
6352 sctp_sk(newsk)->bind_hash = pp;
6353 inet_sk(newsk)->num = inet_sk(oldsk)->num;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006354 sctp_spin_unlock(&head->lock);
6355 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006357 /* Copy the bind_addr list from the original endpoint to the new
6358 * endpoint so that we can handle restarts properly
6359 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08006360 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6361 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006362
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 /* Move any messages in the old socket's receive queue that are for the
6364 * peeled off association to the new socket's receive queue.
6365 */
6366 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6367 event = sctp_skb2event(skb);
6368 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006369 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006370 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006372 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 }
6374 }
6375
6376 /* Clean up any messages pending delivery due to partial
6377 * delivery. Three cases:
6378 * 1) No partial deliver; no work.
6379 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6380 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6381 */
6382 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006383 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006385 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 struct sk_buff_head *queue;
6387
6388 /* Decide which queue to move pd_lobby skbs to. */
6389 if (assoc->ulpq.pd_mode) {
6390 queue = &newsp->pd_lobby;
6391 } else
6392 queue = &newsk->sk_receive_queue;
6393
6394 /* Walk through the pd_lobby, looking for skbs that
6395 * need moved to the new socket.
6396 */
6397 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6398 event = sctp_skb2event(skb);
6399 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006400 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006401 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006403 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 }
6405 }
6406
6407 /* Clear up any skbs waiting for the partial
6408 * delivery to finish.
6409 */
6410 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006411 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
6413 }
6414
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006415 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6416 sctp_sock_rfree_frag(skb);
6417 sctp_skb_set_owner_r_frag(skb, newsk);
6418 }
6419
6420 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6421 sctp_sock_rfree_frag(skb);
6422 sctp_skb_set_owner_r_frag(skb, newsk);
6423 }
6424
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 /* Set the type of socket to indicate that it is peeled off from the
6426 * original UDP-style socket or created with the accept() call on a
6427 * TCP-style socket..
6428 */
6429 newsp->type = type;
6430
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006431 /* Mark the new socket "in-use" by the user so that any packets
6432 * that may arrive on the association after we've moved it are
6433 * queued to the backlog. This prevents a potential race between
6434 * backlog processing on the old socket and new-packet processing
6435 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006436 *
6437 * The caller has just allocated newsk so we can guarantee that other
6438 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006439 */
Zach Brown5131a182007-06-22 15:14:46 -07006440 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
6443 /* If the association on the newsk is already closed before accept()
6444 * is called, set RCV_SHUTDOWN flag.
6445 */
6446 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6447 newsk->sk_shutdown |= RCV_SHUTDOWN;
6448
6449 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006450 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451}
6452
Neil Horman4d93df02007-08-15 16:07:44 -07006453
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006454DEFINE_PROTO_INUSE(sctp)
6455
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456/* This proto struct describes the ULP interface for SCTP. */
6457struct proto sctp_prot = {
6458 .name = "SCTP",
6459 .owner = THIS_MODULE,
6460 .close = sctp_close,
6461 .connect = sctp_connect,
6462 .disconnect = sctp_disconnect,
6463 .accept = sctp_accept,
6464 .ioctl = sctp_ioctl,
6465 .init = sctp_init_sock,
6466 .destroy = sctp_destroy_sock,
6467 .shutdown = sctp_shutdown,
6468 .setsockopt = sctp_setsockopt,
6469 .getsockopt = sctp_getsockopt,
6470 .sendmsg = sctp_sendmsg,
6471 .recvmsg = sctp_recvmsg,
6472 .bind = sctp_bind,
6473 .backlog_rcv = sctp_backlog_rcv,
6474 .hash = sctp_hash,
6475 .unhash = sctp_unhash,
6476 .get_port = sctp_get_port,
6477 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006478 .sysctl_mem = sysctl_sctp_mem,
6479 .sysctl_rmem = sysctl_sctp_rmem,
6480 .sysctl_wmem = sysctl_sctp_wmem,
6481 .memory_pressure = &sctp_memory_pressure,
6482 .enter_memory_pressure = sctp_enter_memory_pressure,
6483 .memory_allocated = &sctp_memory_allocated,
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006484 REF_PROTO_INUSE(sctp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485};
6486
6487#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006488DEFINE_PROTO_INUSE(sctpv6)
6489
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490struct proto sctpv6_prot = {
6491 .name = "SCTPv6",
6492 .owner = THIS_MODULE,
6493 .close = sctp_close,
6494 .connect = sctp_connect,
6495 .disconnect = sctp_disconnect,
6496 .accept = sctp_accept,
6497 .ioctl = sctp_ioctl,
6498 .init = sctp_init_sock,
6499 .destroy = sctp_destroy_sock,
6500 .shutdown = sctp_shutdown,
6501 .setsockopt = sctp_setsockopt,
6502 .getsockopt = sctp_getsockopt,
6503 .sendmsg = sctp_sendmsg,
6504 .recvmsg = sctp_recvmsg,
6505 .bind = sctp_bind,
6506 .backlog_rcv = sctp_backlog_rcv,
6507 .hash = sctp_hash,
6508 .unhash = sctp_unhash,
6509 .get_port = sctp_get_port,
6510 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006511 .sysctl_mem = sysctl_sctp_mem,
6512 .sysctl_rmem = sysctl_sctp_rmem,
6513 .sysctl_wmem = sysctl_sctp_wmem,
6514 .memory_pressure = &sctp_memory_pressure,
6515 .enter_memory_pressure = sctp_enter_memory_pressure,
6516 .memory_allocated = &sctp_memory_allocated,
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006517 REF_PROTO_INUSE(sctpv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518};
6519#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */