blob: b31be0994572828a094b416b05d4e0792b252d9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
34 *
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 *
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
41 *
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
55 *
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
58 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/types.h>
61#include <linux/kernel.h>
62#include <linux/wait.h>
63#include <linux/time.h>
64#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080065#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/fcntl.h>
67#include <linux/poll.h>
68#include <linux/init.h>
69#include <linux/crypto.h>
70
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
76
77#include <linux/socket.h> /* for sa_family_t */
78#include <net/sock.h>
79#include <net/sctp/sctp.h>
80#include <net/sctp/sm.h>
81
82/* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
85 */
86
87/* Forward declarations for internal helper functions. */
88static int sctp_writeable(struct sock *sk);
89static void sctp_wfree(struct sk_buff *skb);
90static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94static int sctp_wait_for_accept(struct sock *sk, long timeo);
95static void sctp_wait_for_close(struct sock *sk, long timeo);
96static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105static int sctp_autobind(struct sock *sk);
106static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Get the sndbuf space available at the time on the association. */
111static inline int sctp_wspace(struct sctp_association *asoc)
112{
113 struct sock *sk = asoc->base.sk;
114 int amt = 0;
115
Neil Horman4eb701d2005-04-28 12:02:04 -0700116 if (asoc->ep->sndbuf_policy) {
117 /* make sure that no association uses more than sk_sndbuf */
118 amt = sk->sk_sndbuf - asoc->sndbuf_used;
119 } else {
120 /* do socket level accounting */
121 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (amt < 0)
125 amt = 0;
Neil Horman4eb701d2005-04-28 12:02:04 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return amt;
128}
129
130/* Increment the used sndbuf space count of the corresponding association by
131 * the size of the outgoing data chunk.
132 * Also, set the skb destructor for sndbuf accounting later.
133 *
134 * Since it is always 1-1 between chunk and skb, and also a new skb is always
135 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
136 * destructor in the data chunk skb for the purpose of the sndbuf space
137 * tracking.
138 */
139static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
140{
141 struct sctp_association *asoc = chunk->asoc;
142 struct sock *sk = asoc->base.sk;
143
144 /* The sndbuf space is tracked per association. */
145 sctp_association_hold(asoc);
146
Neil Horman4eb701d2005-04-28 12:02:04 -0700147 skb_set_owner_w(chunk->skb, sk);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 chunk->skb->destructor = sctp_wfree;
150 /* Save the chunk pointer in skb for sctp_wfree to use later. */
151 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
152
Neil Horman4eb701d2005-04-28 12:02:04 -0700153 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
154 sizeof(struct sk_buff) +
155 sizeof(struct sctp_chunk);
156
Neil Horman4eb701d2005-04-28 12:02:04 -0700157 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/* Verify that this is a valid address. */
161static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
162 int len)
163{
164 struct sctp_af *af;
165
166 /* Verify basic sockaddr. */
167 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
168 if (!af)
169 return -EINVAL;
170
171 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700172 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EINVAL;
174
175 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
176 return -EINVAL;
177
178 return 0;
179}
180
181/* Look up the association by its id. If this is not a UDP-style
182 * socket, the ID field is always ignored.
183 */
184struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
185{
186 struct sctp_association *asoc = NULL;
187
188 /* If this is not a UDP-style socket, assoc id should be ignored. */
189 if (!sctp_style(sk, UDP)) {
190 /* Return NULL if the socket state is not ESTABLISHED. It
191 * could be a TCP-style listening socket or a socket which
192 * hasn't yet called connect() to establish an association.
193 */
194 if (!sctp_sstate(sk, ESTABLISHED))
195 return NULL;
196
197 /* Get the first and the only association from the list. */
198 if (!list_empty(&sctp_sk(sk)->ep->asocs))
199 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
200 struct sctp_association, asocs);
201 return asoc;
202 }
203
204 /* Otherwise this is a UDP-style socket. */
205 if (!id || (id == (sctp_assoc_t)-1))
206 return NULL;
207
208 spin_lock_bh(&sctp_assocs_id_lock);
209 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
210 spin_unlock_bh(&sctp_assocs_id_lock);
211
212 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
213 return NULL;
214
215 return asoc;
216}
217
218/* Look up the transport from an address and an assoc id. If both address and
219 * id are specified, the associations matching the address and the id should be
220 * the same.
221 */
222static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
223 struct sockaddr_storage *addr,
224 sctp_assoc_t id)
225{
226 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
227 struct sctp_transport *transport;
228 union sctp_addr *laddr = (union sctp_addr *)addr;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800231 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!addr_asoc)
235 return NULL;
236
237 id_asoc = sctp_id2assoc(sk, id);
238 if (id_asoc && (id_asoc != addr_asoc))
239 return NULL;
240
241 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
242 (union sctp_addr *)addr);
243
244 return transport;
245}
246
247/* API 3.1.2 bind() - UDP Style Syntax
248 * The syntax of bind() is,
249 *
250 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
251 *
252 * sd - the socket descriptor returned by socket().
253 * addr - the address structure (struct sockaddr_in or struct
254 * sockaddr_in6 [RFC 2553]),
255 * addr_len - the size of the address structure.
256 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700257SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 int retval = 0;
260
261 sctp_lock_sock(sk);
262
Frank Filz3f7a87d2005-06-20 13:14:57 -0700263 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
264 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Disallow binding twice. */
267 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700268 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 addr_len);
270 else
271 retval = -EINVAL;
272
273 sctp_release_sock(sk);
274
275 return retval;
276}
277
278static long sctp_get_port_local(struct sock *, union sctp_addr *);
279
280/* Verify this is a valid sockaddr. */
281static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
282 union sctp_addr *addr, int len)
283{
284 struct sctp_af *af;
285
286 /* Check minimum size. */
287 if (len < sizeof (struct sockaddr))
288 return NULL;
289
290 /* Does this PF support this AF? */
291 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
292 return NULL;
293
294 /* If we get this far, af is valid. */
295 af = sctp_get_af_specific(addr->sa.sa_family);
296
297 if (len < af->sockaddr_len)
298 return NULL;
299
300 return af;
301}
302
303/* Bind a local address either to an endpoint or to an association. */
304SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
305{
306 struct sctp_sock *sp = sctp_sk(sk);
307 struct sctp_endpoint *ep = sp->ep;
308 struct sctp_bind_addr *bp = &ep->base.bind_addr;
309 struct sctp_af *af;
310 unsigned short snum;
311 int ret = 0;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Common sockaddr verification. */
314 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700315 if (!af) {
316 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
317 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700319 }
320
321 snum = ntohs(addr->v4.sin_port);
322
323 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
324 ", port: %d, new port: %d, len: %d)\n",
325 sk,
326 addr,
327 bp->port, snum,
328 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* PF specific bind() address verification. */
331 if (!sp->pf->bind_verify(sp, addr))
332 return -EADDRNOTAVAIL;
333
Vlad Yasevich8b358052007-05-15 17:14:58 -0400334 /* We must either be unbound, or bind to the same port.
335 * It's OK to allow 0 ports if we are already bound.
336 * We'll just inhert an already bound port in this case
337 */
338 if (bp->port) {
339 if (!snum)
340 snum = bp->port;
341 else if (snum != bp->port) {
342 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 " New port %d does not match existing port "
344 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400345 return -EINVAL;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
350 return -EACCES;
351
352 /* Make sure we are allowed to bind here.
353 * The function sctp_get_port_local() does duplicate address
354 * detection.
355 */
356 if ((ret = sctp_get_port_local(sk, addr))) {
357 if (ret == (long) sk) {
358 /* This endpoint has a conflicting address. */
359 return -EINVAL;
360 } else {
361 return -EADDRINUSE;
362 }
363 }
364
365 /* Refresh ephemeral port. */
366 if (!bp->port)
367 bp->port = inet_sk(sk)->num;
368
369 /* Add the address to the bind address list. */
370 sctp_local_bh_disable();
371 sctp_write_lock(&ep->base.addr_lock);
372
373 /* Use GFP_ATOMIC since BHs are disabled. */
Al Viro5ab7b852006-11-20 17:10:38 -0800374 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sctp_write_unlock(&ep->base.addr_lock);
376 sctp_local_bh_enable();
377
378 /* Copy back into socket for getsockname() use. */
379 if (!ret) {
380 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
381 af->to_sk_saddr(addr, sk);
382 }
383
384 return ret;
385}
386
387 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900389 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900391 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900393 * subsequent ASCONF. Note this restriction binds each side, so at any
394 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * from each endpoint).
396 */
397static int sctp_send_asconf(struct sctp_association *asoc,
398 struct sctp_chunk *chunk)
399{
400 int retval = 0;
401
402 /* If there is an outstanding ASCONF chunk, queue it for later
403 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700406 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 /* Hold the chunk until an ASCONF_ACK is received. */
411 sctp_chunk_hold(chunk);
412 retval = sctp_primitive_ASCONF(asoc, chunk);
413 if (retval)
414 sctp_chunk_free(chunk);
415 else
416 asoc->addip_last_asconf = chunk;
417
418out:
419 return retval;
420}
421
422/* Add a list of addresses as bind addresses to local endpoint or
423 * association.
424 *
425 * Basically run through each address specified in the addrs/addrcnt
426 * array/length pair, determine if it is IPv6 or IPv4 and call
427 * sctp_do_bind() on it.
428 *
429 * If any of them fails, then the operation will be reversed and the
430 * ones that were added will be removed.
431 *
432 * Only sctp_setsockopt_bindx() is supposed to call this function.
433 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200434static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int cnt;
437 int retval = 0;
438 void *addr_buf;
439 struct sockaddr *sa_addr;
440 struct sctp_af *af;
441
442 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
443 sk, addrs, addrcnt);
444
445 addr_buf = addrs;
446 for (cnt = 0; cnt < addrcnt; cnt++) {
447 /* The list may contain either IPv4 or IPv6 address;
448 * determine the address length for walking thru the list.
449 */
450 sa_addr = (struct sockaddr *)addr_buf;
451 af = sctp_get_af_specific(sa_addr->sa_family);
452 if (!af) {
453 retval = -EINVAL;
454 goto err_bindx_add;
455 }
456
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900457 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 af->sockaddr_len);
459
460 addr_buf += af->sockaddr_len;
461
462err_bindx_add:
463 if (retval < 0) {
464 /* Failed. Cleanup the ones that have been added */
465 if (cnt > 0)
466 sctp_bindx_rem(sk, addrs, cnt);
467 return retval;
468 }
469 }
470
471 return retval;
472}
473
474/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
475 * associations that are part of the endpoint indicating that a list of local
476 * addresses are added to the endpoint.
477 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900478 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * association, we do not send the chunk for that association. But it will not
480 * affect other associations.
481 *
482 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900484static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct sockaddr *addrs,
486 int addrcnt)
487{
488 struct sctp_sock *sp;
489 struct sctp_endpoint *ep;
490 struct sctp_association *asoc;
491 struct sctp_bind_addr *bp;
492 struct sctp_chunk *chunk;
493 struct sctp_sockaddr_entry *laddr;
494 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700495 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 void *addr_buf;
497 struct sctp_af *af;
498 struct list_head *pos;
499 struct list_head *p;
500 int i;
501 int retval = 0;
502
503 if (!sctp_addip_enable)
504 return retval;
505
506 sp = sctp_sk(sk);
507 ep = sp->ep;
508
509 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
510 __FUNCTION__, sk, addrs, addrcnt);
511
512 list_for_each(pos, &ep->asocs) {
513 asoc = list_entry(pos, struct sctp_association, asocs);
514
515 if (!asoc->peer.asconf_capable)
516 continue;
517
518 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
519 continue;
520
521 if (!sctp_state(asoc, ESTABLISHED))
522 continue;
523
524 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900525 * in the bind address list of the association. If so,
526 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * other associations.
528 */
529 addr_buf = addrs;
530 for (i = 0; i < addrcnt; i++) {
531 addr = (union sctp_addr *)addr_buf;
532 af = sctp_get_af_specific(addr->v4.sin_family);
533 if (!af) {
534 retval = -EINVAL;
535 goto out;
536 }
537
538 if (sctp_assoc_lookup_laddr(asoc, addr))
539 break;
540
541 addr_buf += af->sockaddr_len;
542 }
543 if (i < addrcnt)
544 continue;
545
546 /* Use the first address in bind addr list of association as
547 * Address Parameter of ASCONF CHUNK.
548 */
549 sctp_read_lock(&asoc->base.addr_lock);
550 bp = &asoc->base.bind_addr;
551 p = bp->address_list.next;
552 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
553 sctp_read_unlock(&asoc->base.addr_lock);
554
Al Viro5ae955c2006-11-20 17:22:08 -0800555 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 addrcnt, SCTP_PARAM_ADD_IP);
557 if (!chunk) {
558 retval = -ENOMEM;
559 goto out;
560 }
561
562 retval = sctp_send_asconf(asoc, chunk);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700563 if (retval)
564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700566 /* Add the new addresses to the bind address list with
567 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700569 sctp_local_bh_disable();
570 sctp_write_lock(&asoc->base.addr_lock);
571 addr_buf = addrs;
572 for (i = 0; i < addrcnt; i++) {
573 addr = (union sctp_addr *)addr_buf;
574 af = sctp_get_af_specific(addr->v4.sin_family);
575 memcpy(&saveaddr, addr, af->sockaddr_len);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700576 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
577 GFP_ATOMIC);
578 addr_buf += af->sockaddr_len;
579 }
580 sctp_write_unlock(&asoc->base.addr_lock);
581 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584out:
585 return retval;
586}
587
588/* Remove a list of addresses from bind addresses list. Do not remove the
589 * last address.
590 *
591 * Basically run through each address specified in the addrs/addrcnt
592 * array/length pair, determine if it is IPv6 or IPv4 and call
593 * sctp_del_bind() on it.
594 *
595 * If any of them fails, then the operation will be reversed and the
596 * ones that were removed will be added back.
597 *
598 * At least one address has to be left; if only one address is
599 * available, the operation will return -EBUSY.
600 *
601 * Only sctp_setsockopt_bindx() is supposed to call this function.
602 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200603static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct sctp_sock *sp = sctp_sk(sk);
606 struct sctp_endpoint *ep = sp->ep;
607 int cnt;
608 struct sctp_bind_addr *bp = &ep->base.bind_addr;
609 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800611 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct sctp_af *af;
613
614 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
615 sk, addrs, addrcnt);
616
617 addr_buf = addrs;
618 for (cnt = 0; cnt < addrcnt; cnt++) {
619 /* If the bind address list is empty or if there is only one
620 * bind address, there is nothing more to be removed (we need
621 * at least one address here).
622 */
623 if (list_empty(&bp->address_list) ||
624 (sctp_list_single_entry(&bp->address_list))) {
625 retval = -EBUSY;
626 goto err_bindx_rem;
627 }
628
Al Viroc9a08502006-11-20 17:07:48 -0800629 sa_addr = (union sctp_addr *)addr_buf;
630 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!af) {
632 retval = -EINVAL;
633 goto err_bindx_rem;
634 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700635
636 if (!af->addr_valid(sa_addr, sp, NULL)) {
637 retval = -EADDRNOTAVAIL;
638 goto err_bindx_rem;
639 }
640
Al Viroc9a08502006-11-20 17:07:48 -0800641 if (sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 retval = -EINVAL;
643 goto err_bindx_rem;
644 }
645
646 /* FIXME - There is probably a need to check if sk->sk_saddr and
647 * sk->sk_rcv_addr are currently set to one of the addresses to
648 * be removed. This is something which needs to be looked into
649 * when we are fixing the outstanding issues with multi-homing
650 * socket routing and failover schemes. Refer to comments in
651 * sctp_do_bind(). -daisy
652 */
653 sctp_local_bh_disable();
654 sctp_write_lock(&ep->base.addr_lock);
655
Al Viroc9a08502006-11-20 17:07:48 -0800656 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 sctp_write_unlock(&ep->base.addr_lock);
659 sctp_local_bh_enable();
660
661 addr_buf += af->sockaddr_len;
662err_bindx_rem:
663 if (retval < 0) {
664 /* Failed. Add the ones that has been removed back */
665 if (cnt > 0)
666 sctp_bindx_add(sk, addrs, cnt);
667 return retval;
668 }
669 }
670
671 return retval;
672}
673
674/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
675 * the associations that are part of the endpoint indicating that a list of
676 * local addresses are removed from the endpoint.
677 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900678 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * association, we do not send the chunk for that association. But it will not
680 * affect other associations.
681 *
682 * Only sctp_setsockopt_bindx() is supposed to call this function.
683 */
684static int sctp_send_asconf_del_ip(struct sock *sk,
685 struct sockaddr *addrs,
686 int addrcnt)
687{
688 struct sctp_sock *sp;
689 struct sctp_endpoint *ep;
690 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700691 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct sctp_bind_addr *bp;
693 struct sctp_chunk *chunk;
694 union sctp_addr *laddr;
695 void *addr_buf;
696 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700697 struct list_head *pos, *pos1;
698 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int i;
700 int retval = 0;
701
702 if (!sctp_addip_enable)
703 return retval;
704
705 sp = sctp_sk(sk);
706 ep = sp->ep;
707
708 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
709 __FUNCTION__, sk, addrs, addrcnt);
710
711 list_for_each(pos, &ep->asocs) {
712 asoc = list_entry(pos, struct sctp_association, asocs);
713
714 if (!asoc->peer.asconf_capable)
715 continue;
716
717 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
718 continue;
719
720 if (!sctp_state(asoc, ESTABLISHED))
721 continue;
722
723 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900724 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * If so, do not send the asconf chunk to its peer, but
726 * continue with other associations.
727 */
728 addr_buf = addrs;
729 for (i = 0; i < addrcnt; i++) {
730 laddr = (union sctp_addr *)addr_buf;
731 af = sctp_get_af_specific(laddr->v4.sin_family);
732 if (!af) {
733 retval = -EINVAL;
734 goto out;
735 }
736
737 if (!sctp_assoc_lookup_laddr(asoc, laddr))
738 break;
739
740 addr_buf += af->sockaddr_len;
741 }
742 if (i < addrcnt)
743 continue;
744
745 /* Find one address in the association's bind address list
746 * that is not in the packed array of addresses. This is to
747 * make sure that we do not delete all the addresses in the
748 * association.
749 */
750 sctp_read_lock(&asoc->base.addr_lock);
751 bp = &asoc->base.bind_addr;
752 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
753 addrcnt, sp);
754 sctp_read_unlock(&asoc->base.addr_lock);
755 if (!laddr)
756 continue;
757
758 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
759 SCTP_PARAM_DEL_IP);
760 if (!chunk) {
761 retval = -ENOMEM;
762 goto out;
763 }
764
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700765 /* Reset use_as_src flag for the addresses in the bind address
766 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700768 sctp_local_bh_disable();
769 sctp_write_lock(&asoc->base.addr_lock);
770 addr_buf = addrs;
771 for (i = 0; i < addrcnt; i++) {
772 laddr = (union sctp_addr *)addr_buf;
773 af = sctp_get_af_specific(laddr->v4.sin_family);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700774 list_for_each(pos1, &bp->address_list) {
775 saddr = list_entry(pos1,
776 struct sctp_sockaddr_entry,
777 list);
Al Viro5f242a12006-11-20 17:05:23 -0800778 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700779 saddr->use_as_src = 0;
780 }
781 addr_buf += af->sockaddr_len;
782 }
783 sctp_write_unlock(&asoc->base.addr_lock);
784 sctp_local_bh_enable();
785
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;
Al Viro4bdf4b52006-11-20 17:10:20 -0800978 union sctp_addr *sa_addr;
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
Al Viro4bdf4b52006-11-20 17:10:20 -08001012 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001013 if (err)
1014 goto out_free;
1015
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001016 /* Make sure the destination port is correctly set
1017 * in all addresses.
1018 */
1019 if (asoc && asoc->peer.port && asoc->peer.port != port)
1020 goto out_free;
1021
Frank Filz3f7a87d2005-06-20 13:14:57 -07001022 memcpy(&to, sa_addr, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001023
1024 /* Check if there already is a matching association on the
1025 * endpoint (other than the one created here).
1026 */
Al Virocd4ff032006-11-20 17:11:33 -08001027 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001028 if (asoc2 && asoc2 != asoc) {
1029 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1030 err = -EISCONN;
1031 else
1032 err = -EALREADY;
1033 goto out_free;
1034 }
1035
1036 /* If we could not find a matching association on the endpoint,
1037 * make sure that there is no peeled-off association matching
1038 * the peer address even on another socket.
1039 */
Al Viro6c7be552006-11-20 17:11:50 -08001040 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001041 err = -EADDRNOTAVAIL;
1042 goto out_free;
1043 }
1044
1045 if (!asoc) {
1046 /* If a bind() or sctp_bindx() is not called prior to
1047 * an sctp_connectx() call, the system picks an
1048 * ephemeral port and will choose an address set
1049 * equivalent to binding with a wildcard address.
1050 */
1051 if (!ep->base.bind_addr.port) {
1052 if (sctp_autobind(sk)) {
1053 err = -EAGAIN;
1054 goto out_free;
1055 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001056 } else {
1057 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001058 * If an unprivileged user inherits a 1-many
1059 * style socket with open associations on a
1060 * privileged port, it MAY be permitted to
1061 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001062 * be permitted to open new associations.
1063 */
1064 if (ep->base.bind_addr.port < PROT_SOCK &&
1065 !capable(CAP_NET_BIND_SERVICE)) {
1066 err = -EACCES;
1067 goto out_free;
1068 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001069 }
1070
Al Virodce116a2006-11-20 17:25:15 -08001071 scope = sctp_scope(sa_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001072 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1073 if (!asoc) {
1074 err = -ENOMEM;
1075 goto out_free;
1076 }
1077 }
1078
1079 /* Prime the peer's transport structures. */
Al Viro4bdf4b52006-11-20 17:10:20 -08001080 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081 SCTP_UNKNOWN);
1082 if (!transport) {
1083 err = -ENOMEM;
1084 goto out_free;
1085 }
1086
1087 addrcnt++;
1088 addr_buf += af->sockaddr_len;
1089 walk_size += af->sockaddr_len;
1090 }
1091
1092 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1093 if (err < 0) {
1094 goto out_free;
1095 }
1096
1097 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1098 if (err < 0) {
1099 goto out_free;
1100 }
1101
1102 /* Initialize sk's dport and daddr for getpeername() */
1103 inet_sk(sk)->dport = htons(asoc->peer.port);
1104 af = sctp_get_af_specific(to.sa.sa_family);
1105 af->to_sk_daddr(&to, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001106 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001107
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001108 /* in-kernel sockets don't generally have a file allocated to them
1109 * if all they do is call sock_create_kern().
1110 */
1111 if (sk->sk_socket->file)
1112 f_flags = sk->sk_socket->file->f_flags;
1113
1114 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1115
Frank Filz3f7a87d2005-06-20 13:14:57 -07001116 err = sctp_wait_for_connect(asoc, &timeo);
1117
1118 /* Don't free association on exit. */
1119 asoc = NULL;
1120
1121out_free:
1122
1123 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001124 " kaddrs: %p err: %d\n",
1125 asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001126 if (asoc)
1127 sctp_association_free(asoc);
1128 return err;
1129}
1130
1131/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1132 *
1133 * API 8.9
1134 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1135 *
1136 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1137 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1138 * or IPv6 addresses.
1139 *
1140 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1141 * Section 3.1.2 for this usage.
1142 *
1143 * addrs is a pointer to an array of one or more socket addresses. Each
1144 * address is contained in its appropriate structure (i.e. struct
1145 * sockaddr_in or struct sockaddr_in6) the family of the address type
1146 * must be used to distengish the address length (note that this
1147 * representation is termed a "packed array" of addresses). The caller
1148 * specifies the number of addresses in the array with addrcnt.
1149 *
1150 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1151 * -1, and sets errno to the appropriate error code.
1152 *
1153 * For SCTP, the port given in each socket address must be the same, or
1154 * sctp_connectx() will fail, setting errno to EINVAL.
1155 *
1156 * An application can use sctp_connectx to initiate an association with
1157 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1158 * allows a caller to specify multiple addresses at which a peer can be
1159 * reached. The way the SCTP stack uses the list of addresses to set up
1160 * the association is implementation dependant. This function only
1161 * specifies that the stack will try to make use of all the addresses in
1162 * the list when needed.
1163 *
1164 * Note that the list of addresses passed in is only used for setting up
1165 * the association. It does not necessarily equal the set of addresses
1166 * the peer uses for the resulting association. If the caller wants to
1167 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1168 * retrieve them after the association has been set up.
1169 *
1170 * Basically do nothing but copying the addresses from user to kernel
1171 * land and invoking either sctp_connectx(). This is used for tunneling
1172 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1173 *
1174 * We don't use copy_from_user() for optimization: we first do the
1175 * sanity checks (buffer size -fast- and access check-healthy
1176 * pointer); if all of those succeed, then we can alloc the memory
1177 * (expensive operation) needed to copy the data to kernel. Then we do
1178 * the copying without checking the user space area
1179 * (__copy_from_user()).
1180 *
1181 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1182 * it.
1183 *
1184 * sk The sk of the socket
1185 * addrs The pointer to the addresses in user land
1186 * addrssize Size of the addrs buffer
1187 *
1188 * Returns 0 if ok, <0 errno code on error.
1189 */
1190SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1191 struct sockaddr __user *addrs,
1192 int addrs_size)
1193{
1194 int err = 0;
1195 struct sockaddr *kaddrs;
1196
1197 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1198 __FUNCTION__, sk, addrs, addrs_size);
1199
1200 if (unlikely(addrs_size <= 0))
1201 return -EINVAL;
1202
1203 /* Check the user passed a healthy pointer. */
1204 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1205 return -EFAULT;
1206
1207 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001208 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001209 if (unlikely(!kaddrs))
1210 return -ENOMEM;
1211
1212 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1213 err = -EFAULT;
1214 } else {
1215 err = __sctp_connect(sk, kaddrs, addrs_size);
1216 }
1217
1218 kfree(kaddrs);
1219 return err;
1220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/* API 3.1.4 close() - UDP Style Syntax
1223 * Applications use close() to perform graceful shutdown (as described in
1224 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1225 * by a UDP-style socket.
1226 *
1227 * The syntax is
1228 *
1229 * ret = close(int sd);
1230 *
1231 * sd - the socket descriptor of the associations to be closed.
1232 *
1233 * To gracefully shutdown a specific association represented by the
1234 * UDP-style socket, an application should use the sendmsg() call,
1235 * passing no user data, but including the appropriate flag in the
1236 * ancillary data (see Section xxxx).
1237 *
1238 * If sd in the close() call is a branched-off socket representing only
1239 * one association, the shutdown is performed on that association only.
1240 *
1241 * 4.1.6 close() - TCP Style Syntax
1242 *
1243 * Applications use close() to gracefully close down an association.
1244 *
1245 * The syntax is:
1246 *
1247 * int close(int sd);
1248 *
1249 * sd - the socket descriptor of the association to be closed.
1250 *
1251 * After an application calls close() on a socket descriptor, no further
1252 * socket operations will succeed on that descriptor.
1253 *
1254 * API 7.1.4 SO_LINGER
1255 *
1256 * An application using the TCP-style socket can use this option to
1257 * perform the SCTP ABORT primitive. The linger option structure is:
1258 *
1259 * struct linger {
1260 * int l_onoff; // option on/off
1261 * int l_linger; // linger time
1262 * };
1263 *
1264 * To enable the option, set l_onoff to 1. If the l_linger value is set
1265 * to 0, calling close() is the same as the ABORT primitive. If the
1266 * value is set to a negative value, the setsockopt() call will return
1267 * an error. If the value is set to a positive value linger_time, the
1268 * close() can be blocked for at most linger_time ms. If the graceful
1269 * shutdown phase does not finish during this period, close() will
1270 * return but the graceful shutdown phase continues in the system.
1271 */
1272SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1273{
1274 struct sctp_endpoint *ep;
1275 struct sctp_association *asoc;
1276 struct list_head *pos, *temp;
1277
1278 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1279
1280 sctp_lock_sock(sk);
1281 sk->sk_shutdown = SHUTDOWN_MASK;
1282
1283 ep = sctp_sk(sk)->ep;
1284
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001285 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_for_each_safe(pos, temp, &ep->asocs) {
1287 asoc = list_entry(pos, struct sctp_association, asocs);
1288
1289 if (sctp_style(sk, TCP)) {
1290 /* A closed association can still be in the list if
1291 * it belongs to a TCP-style listening socket that is
1292 * not yet accepted. If so, free it. If not, send an
1293 * ABORT or SHUTDOWN based on the linger options.
1294 */
1295 if (sctp_state(asoc, CLOSED)) {
1296 sctp_unhash_established(asoc);
1297 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001298 continue;
1299 }
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001302 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1303 struct sctp_chunk *chunk;
1304
1305 chunk = sctp_make_abort_user(asoc, NULL, 0);
1306 if (chunk)
1307 sctp_primitive_ABORT(asoc, chunk);
1308 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 sctp_primitive_SHUTDOWN(asoc, NULL);
1310 }
1311
1312 /* Clean up any skbs sitting on the receive queue. */
1313 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1314 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1315
1316 /* On a TCP-style socket, block for at most linger_time if set. */
1317 if (sctp_style(sk, TCP) && timeout)
1318 sctp_wait_for_close(sk, timeout);
1319
1320 /* This will run the backlog queue. */
1321 sctp_release_sock(sk);
1322
1323 /* Supposedly, no process has access to the socket, but
1324 * the net layers still may.
1325 */
1326 sctp_local_bh_disable();
1327 sctp_bh_lock_sock(sk);
1328
1329 /* Hold the sock, since sk_common_release() will put sock_put()
1330 * and we have just a little more cleanup.
1331 */
1332 sock_hold(sk);
1333 sk_common_release(sk);
1334
1335 sctp_bh_unlock_sock(sk);
1336 sctp_local_bh_enable();
1337
1338 sock_put(sk);
1339
1340 SCTP_DBG_OBJCNT_DEC(sock);
1341}
1342
1343/* Handle EPIPE error. */
1344static int sctp_error(struct sock *sk, int flags, int err)
1345{
1346 if (err == -EPIPE)
1347 err = sock_error(sk) ? : -EPIPE;
1348 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1349 send_sig(SIGPIPE, current, 0);
1350 return err;
1351}
1352
1353/* API 3.1.3 sendmsg() - UDP Style Syntax
1354 *
1355 * An application uses sendmsg() and recvmsg() calls to transmit data to
1356 * and receive data from its peer.
1357 *
1358 * ssize_t sendmsg(int socket, const struct msghdr *message,
1359 * int flags);
1360 *
1361 * socket - the socket descriptor of the endpoint.
1362 * message - pointer to the msghdr structure which contains a single
1363 * user message and possibly some ancillary data.
1364 *
1365 * See Section 5 for complete description of the data
1366 * structures.
1367 *
1368 * flags - flags sent or received with the user message, see Section
1369 * 5 for complete description of the flags.
1370 *
1371 * Note: This function could use a rewrite especially when explicit
1372 * connect support comes in.
1373 */
1374/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1375
1376SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1377
1378SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1379 struct msghdr *msg, size_t msg_len)
1380{
1381 struct sctp_sock *sp;
1382 struct sctp_endpoint *ep;
1383 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1384 struct sctp_transport *transport, *chunk_tp;
1385 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001386 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct sockaddr *msg_name = NULL;
1388 struct sctp_sndrcvinfo default_sinfo = { 0 };
1389 struct sctp_sndrcvinfo *sinfo;
1390 struct sctp_initmsg *sinit;
1391 sctp_assoc_t associd = 0;
1392 sctp_cmsgs_t cmsgs = { NULL };
1393 int err;
1394 sctp_scope_t scope;
1395 long timeo;
1396 __u16 sinfo_flags = 0;
1397 struct sctp_datamsg *datamsg;
1398 struct list_head *pos;
1399 int msg_flags = msg->msg_flags;
1400
1401 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1402 sk, msg, msg_len);
1403
1404 err = 0;
1405 sp = sctp_sk(sk);
1406 ep = sp->ep;
1407
Frank Filz3f7a87d2005-06-20 13:14:57 -07001408 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* We cannot send a message over a TCP-style listening socket. */
1411 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1412 err = -EPIPE;
1413 goto out_nounlock;
1414 }
1415
1416 /* Parse out the SCTP CMSGs. */
1417 err = sctp_msghdr_parse(msg, &cmsgs);
1418
1419 if (err) {
1420 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1421 goto out_nounlock;
1422 }
1423
1424 /* Fetch the destination address for this packet. This
1425 * address only selects the association--it is not necessarily
1426 * the address we will send to.
1427 * For a peeled-off socket, msg_name is ignored.
1428 */
1429 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1430 int msg_namelen = msg->msg_namelen;
1431
1432 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1433 msg_namelen);
1434 if (err)
1435 return err;
1436
1437 if (msg_namelen > sizeof(to))
1438 msg_namelen = sizeof(to);
1439 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 msg_name = msg->msg_name;
1441 }
1442
1443 sinfo = cmsgs.info;
1444 sinit = cmsgs.init;
1445
1446 /* Did the user specify SNDRCVINFO? */
1447 if (sinfo) {
1448 sinfo_flags = sinfo->sinfo_flags;
1449 associd = sinfo->sinfo_assoc_id;
1450 }
1451
1452 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1453 msg_len, sinfo_flags);
1454
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001455 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1456 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 err = -EINVAL;
1458 goto out_nounlock;
1459 }
1460
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001461 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1462 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1463 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001465 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001466 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1467 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 err = -EINVAL;
1469 goto out_nounlock;
1470 }
1471
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001472 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * specified in msg_name.
1474 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001475 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 err = -EINVAL;
1477 goto out_nounlock;
1478 }
1479
1480 transport = NULL;
1481
1482 SCTP_DEBUG_PRINTK("About to look up association.\n");
1483
1484 sctp_lock_sock(sk);
1485
1486 /* If a msg_name has been specified, assume this is to be used. */
1487 if (msg_name) {
1488 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001489 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (!asoc) {
1491 /* If we could not find a matching association on the
1492 * endpoint, make sure that it is not a TCP-style
1493 * socket that already has an association or there is
1494 * no peeled-off association on another socket.
1495 */
1496 if ((sctp_style(sk, TCP) &&
1497 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001498 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 err = -EADDRNOTAVAIL;
1500 goto out_unlock;
1501 }
1502 }
1503 } else {
1504 asoc = sctp_id2assoc(sk, associd);
1505 if (!asoc) {
1506 err = -EPIPE;
1507 goto out_unlock;
1508 }
1509 }
1510
1511 if (asoc) {
1512 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1513
1514 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1515 * socket that has an association in CLOSED state. This can
1516 * happen when an accepted socket has an association that is
1517 * already CLOSED.
1518 */
1519 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1520 err = -EPIPE;
1521 goto out_unlock;
1522 }
1523
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001524 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1526 asoc);
1527 sctp_primitive_SHUTDOWN(asoc, NULL);
1528 err = 0;
1529 goto out_unlock;
1530 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001531 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001532
1533 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1534 if (!chunk) {
1535 err = -ENOMEM;
1536 goto out_unlock;
1537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001540 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 err = 0;
1542 goto out_unlock;
1543 }
1544 }
1545
1546 /* Do we need to create the association? */
1547 if (!asoc) {
1548 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1549
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001550 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 err = -EINVAL;
1552 goto out_unlock;
1553 }
1554
1555 /* Check for invalid stream against the stream counts,
1556 * either the default or the user specified stream counts.
1557 */
1558 if (sinfo) {
1559 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1560 /* Check against the defaults. */
1561 if (sinfo->sinfo_stream >=
1562 sp->initmsg.sinit_num_ostreams) {
1563 err = -EINVAL;
1564 goto out_unlock;
1565 }
1566 } else {
1567 /* Check against the requested. */
1568 if (sinfo->sinfo_stream >=
1569 sinit->sinit_num_ostreams) {
1570 err = -EINVAL;
1571 goto out_unlock;
1572 }
1573 }
1574 }
1575
1576 /*
1577 * API 3.1.2 bind() - UDP Style Syntax
1578 * If a bind() or sctp_bindx() is not called prior to a
1579 * sendmsg() call that initiates a new association, the
1580 * system picks an ephemeral port and will choose an address
1581 * set equivalent to binding with a wildcard address.
1582 */
1583 if (!ep->base.bind_addr.port) {
1584 if (sctp_autobind(sk)) {
1585 err = -EAGAIN;
1586 goto out_unlock;
1587 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001588 } else {
1589 /*
1590 * If an unprivileged user inherits a one-to-many
1591 * style socket with open associations on a privileged
1592 * port, it MAY be permitted to accept new associations,
1593 * but it SHOULD NOT be permitted to open new
1594 * associations.
1595 */
1596 if (ep->base.bind_addr.port < PROT_SOCK &&
1597 !capable(CAP_NET_BIND_SERVICE)) {
1598 err = -EACCES;
1599 goto out_unlock;
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
1603 scope = sctp_scope(&to);
1604 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1605 if (!new_asoc) {
1606 err = -ENOMEM;
1607 goto out_unlock;
1608 }
1609 asoc = new_asoc;
1610
1611 /* If the SCTP_INIT ancillary data is specified, set all
1612 * the association init values accordingly.
1613 */
1614 if (sinit) {
1615 if (sinit->sinit_num_ostreams) {
1616 asoc->c.sinit_num_ostreams =
1617 sinit->sinit_num_ostreams;
1618 }
1619 if (sinit->sinit_max_instreams) {
1620 asoc->c.sinit_max_instreams =
1621 sinit->sinit_max_instreams;
1622 }
1623 if (sinit->sinit_max_attempts) {
1624 asoc->max_init_attempts
1625 = sinit->sinit_max_attempts;
1626 }
1627 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001628 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1630 }
1631 }
1632
1633 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001634 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (!transport) {
1636 err = -ENOMEM;
1637 goto out_free;
1638 }
1639 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1640 if (err < 0) {
1641 err = -ENOMEM;
1642 goto out_free;
1643 }
1644 }
1645
1646 /* ASSERT: we have a valid association at this point. */
1647 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1648
1649 if (!sinfo) {
1650 /* If the user didn't specify SNDRCVINFO, make up one with
1651 * some defaults.
1652 */
1653 default_sinfo.sinfo_stream = asoc->default_stream;
1654 default_sinfo.sinfo_flags = asoc->default_flags;
1655 default_sinfo.sinfo_ppid = asoc->default_ppid;
1656 default_sinfo.sinfo_context = asoc->default_context;
1657 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1658 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1659 sinfo = &default_sinfo;
1660 }
1661
1662 /* API 7.1.7, the sndbuf size per association bounds the
1663 * maximum size of data that can be sent in a single send call.
1664 */
1665 if (msg_len > sk->sk_sndbuf) {
1666 err = -EMSGSIZE;
1667 goto out_free;
1668 }
1669
Vlad Yasevich8a479492007-06-07 14:21:05 -04001670 if (asoc->pmtu_pending)
1671 sctp_assoc_pending_pmtu(asoc);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* If fragmentation is disabled and the message length exceeds the
1674 * association fragmentation point, return EMSGSIZE. The I-D
1675 * does not specify what this error is, but this looks like
1676 * a great fit.
1677 */
1678 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1679 err = -EMSGSIZE;
1680 goto out_free;
1681 }
1682
1683 if (sinfo) {
1684 /* Check for invalid stream. */
1685 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1686 err = -EINVAL;
1687 goto out_free;
1688 }
1689 }
1690
1691 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1692 if (!sctp_wspace(asoc)) {
1693 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1694 if (err)
1695 goto out_free;
1696 }
1697
1698 /* If an address is passed with the sendto/sendmsg call, it is used
1699 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001700 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
1702 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001703 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001704 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (!chunk_tp) {
1706 err = -EINVAL;
1707 goto out_free;
1708 }
1709 } else
1710 chunk_tp = NULL;
1711
1712 /* Auto-connect, if we aren't connected already. */
1713 if (sctp_state(asoc, CLOSED)) {
1714 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1715 if (err < 0)
1716 goto out_free;
1717 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1718 }
1719
1720 /* Break the message into multiple chunks of maximum size. */
1721 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1722 if (!datamsg) {
1723 err = -ENOMEM;
1724 goto out_free;
1725 }
1726
1727 /* Now send the (possibly) fragmented message. */
1728 list_for_each(pos, &datamsg->chunks) {
1729 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1730 sctp_datamsg_track(chunk);
1731
1732 /* Do accounting for the write space. */
1733 sctp_set_owner_w(chunk);
1734
1735 chunk->transport = chunk_tp;
1736
1737 /* Send it to the lower layers. Note: all chunks
1738 * must either fail or succeed. The lower layer
1739 * works that way today. Keep it that way or this
1740 * breaks.
1741 */
1742 err = sctp_primitive_SEND(asoc, chunk);
1743 /* Did the lower layer accept the chunk? */
1744 if (err)
1745 sctp_chunk_free(chunk);
1746 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1747 }
1748
1749 sctp_datamsg_free(datamsg);
1750 if (err)
1751 goto out_free;
1752 else
1753 err = msg_len;
1754
1755 /* If we are already past ASSOCIATE, the lower
1756 * layers are responsible for association cleanup.
1757 */
1758 goto out_unlock;
1759
1760out_free:
1761 if (new_asoc)
1762 sctp_association_free(asoc);
1763out_unlock:
1764 sctp_release_sock(sk);
1765
1766out_nounlock:
1767 return sctp_error(sk, msg_flags, err);
1768
1769#if 0
1770do_sock_err:
1771 if (msg_len)
1772 err = msg_len;
1773 else
1774 err = sock_error(sk);
1775 goto out;
1776
1777do_interrupted:
1778 if (msg_len)
1779 err = msg_len;
1780 goto out;
1781#endif /* 0 */
1782}
1783
1784/* This is an extended version of skb_pull() that removes the data from the
1785 * start of a skb even when data is spread across the list of skb's in the
1786 * frag_list. len specifies the total amount of data that needs to be removed.
1787 * when 'len' bytes could be removed from the skb, it returns 0.
1788 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1789 * could not be removed.
1790 */
1791static int sctp_skb_pull(struct sk_buff *skb, int len)
1792{
1793 struct sk_buff *list;
1794 int skb_len = skb_headlen(skb);
1795 int rlen;
1796
1797 if (len <= skb_len) {
1798 __skb_pull(skb, len);
1799 return 0;
1800 }
1801 len -= skb_len;
1802 __skb_pull(skb, skb_len);
1803
1804 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1805 rlen = sctp_skb_pull(list, len);
1806 skb->len -= (len-rlen);
1807 skb->data_len -= (len-rlen);
1808
1809 if (!rlen)
1810 return 0;
1811
1812 len = rlen;
1813 }
1814
1815 return len;
1816}
1817
1818/* API 3.1.3 recvmsg() - UDP Style Syntax
1819 *
1820 * ssize_t recvmsg(int socket, struct msghdr *message,
1821 * int flags);
1822 *
1823 * socket - the socket descriptor of the endpoint.
1824 * message - pointer to the msghdr structure which contains a single
1825 * user message and possibly some ancillary data.
1826 *
1827 * See Section 5 for complete description of the data
1828 * structures.
1829 *
1830 * flags - flags sent or received with the user message, see Section
1831 * 5 for complete description of the flags.
1832 */
1833static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1834
1835SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1836 struct msghdr *msg, size_t len, int noblock,
1837 int flags, int *addr_len)
1838{
1839 struct sctp_ulpevent *event = NULL;
1840 struct sctp_sock *sp = sctp_sk(sk);
1841 struct sk_buff *skb;
1842 int copied;
1843 int err = 0;
1844 int skb_len;
1845
1846 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1847 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1848 "len", len, "knoblauch", noblock,
1849 "flags", flags, "addr_len", addr_len);
1850
1851 sctp_lock_sock(sk);
1852
1853 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1854 err = -ENOTCONN;
1855 goto out;
1856 }
1857
1858 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1859 if (!skb)
1860 goto out;
1861
1862 /* Get the total length of the skb including any skb's in the
1863 * frag_list.
1864 */
1865 skb_len = skb->len;
1866
1867 copied = skb_len;
1868 if (copied > len)
1869 copied = len;
1870
1871 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1872
1873 event = sctp_skb2event(skb);
1874
1875 if (err)
1876 goto out_free;
1877
1878 sock_recv_timestamp(msg, sk, skb);
1879 if (sctp_ulpevent_is_notification(event)) {
1880 msg->msg_flags |= MSG_NOTIFICATION;
1881 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1882 } else {
1883 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1884 }
1885
1886 /* Check if we allow SCTP_SNDRCVINFO. */
1887 if (sp->subscribe.sctp_data_io_event)
1888 sctp_ulpevent_read_sndrcvinfo(event, msg);
1889#if 0
1890 /* FIXME: we should be calling IP/IPv6 layers. */
1891 if (sk->sk_protinfo.af_inet.cmsg_flags)
1892 ip_cmsg_recv(msg, skb);
1893#endif
1894
1895 err = copied;
1896
1897 /* If skb's length exceeds the user's buffer, update the skb and
1898 * push it back to the receive_queue so that the next call to
1899 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1900 */
1901 if (skb_len > copied) {
1902 msg->msg_flags &= ~MSG_EOR;
1903 if (flags & MSG_PEEK)
1904 goto out_free;
1905 sctp_skb_pull(skb, copied);
1906 skb_queue_head(&sk->sk_receive_queue, skb);
1907
1908 /* When only partial message is copied to the user, increase
1909 * rwnd by that amount. If all the data in the skb is read,
1910 * rwnd is updated when the event is freed.
1911 */
1912 sctp_assoc_rwnd_increase(event->asoc, copied);
1913 goto out;
1914 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1915 (event->msg_flags & MSG_EOR))
1916 msg->msg_flags |= MSG_EOR;
1917 else
1918 msg->msg_flags &= ~MSG_EOR;
1919
1920out_free:
1921 if (flags & MSG_PEEK) {
1922 /* Release the skb reference acquired after peeking the skb in
1923 * sctp_skb_recv_datagram().
1924 */
1925 kfree_skb(skb);
1926 } else {
1927 /* Free the event which includes releasing the reference to
1928 * the owner of the skb, freeing the skb and updating the
1929 * rwnd.
1930 */
1931 sctp_ulpevent_free(event);
1932 }
1933out:
1934 sctp_release_sock(sk);
1935 return err;
1936}
1937
1938/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1939 *
1940 * This option is a on/off flag. If enabled no SCTP message
1941 * fragmentation will be performed. Instead if a message being sent
1942 * exceeds the current PMTU size, the message will NOT be sent and
1943 * instead a error will be indicated to the user.
1944 */
1945static int sctp_setsockopt_disable_fragments(struct sock *sk,
1946 char __user *optval, int optlen)
1947{
1948 int val;
1949
1950 if (optlen < sizeof(int))
1951 return -EINVAL;
1952
1953 if (get_user(val, (int __user *)optval))
1954 return -EFAULT;
1955
1956 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1957
1958 return 0;
1959}
1960
1961static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1962 int optlen)
1963{
1964 if (optlen != sizeof(struct sctp_event_subscribe))
1965 return -EINVAL;
1966 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1967 return -EFAULT;
1968 return 0;
1969}
1970
1971/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1972 *
1973 * This socket option is applicable to the UDP-style socket only. When
1974 * set it will cause associations that are idle for more than the
1975 * specified number of seconds to automatically close. An association
1976 * being idle is defined an association that has NOT sent or received
1977 * user data. The special value of '0' indicates that no automatic
1978 * close of any associations should be performed. The option expects an
1979 * integer defining the number of seconds of idle time before an
1980 * association is closed.
1981 */
1982static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1983 int optlen)
1984{
1985 struct sctp_sock *sp = sctp_sk(sk);
1986
1987 /* Applicable to UDP-style socket only */
1988 if (sctp_style(sk, TCP))
1989 return -EOPNOTSUPP;
1990 if (optlen != sizeof(int))
1991 return -EINVAL;
1992 if (copy_from_user(&sp->autoclose, optval, optlen))
1993 return -EFAULT;
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
1997
1998/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1999 *
2000 * Applications can enable or disable heartbeats for any peer address of
2001 * an association, modify an address's heartbeat interval, force a
2002 * heartbeat to be sent immediately, and adjust the address's maximum
2003 * number of retransmissions sent before an address is considered
2004 * unreachable. The following structure is used to access and modify an
2005 * address's parameters:
2006 *
2007 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002008 * sctp_assoc_t spp_assoc_id;
2009 * struct sockaddr_storage spp_address;
2010 * uint32_t spp_hbinterval;
2011 * uint16_t spp_pathmaxrxt;
2012 * uint32_t spp_pathmtu;
2013 * uint32_t spp_sackdelay;
2014 * uint32_t spp_flags;
2015 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002017 * spp_assoc_id - (one-to-many style socket) This is filled in the
2018 * application, and identifies the association for
2019 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * spp_address - This specifies which address is of interest.
2021 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002022 * in milliseconds. If a value of zero
2023 * is present in this field then no changes are to
2024 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * spp_pathmaxrxt - This contains the maximum number of
2026 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002027 * considered unreachable. If a value of zero
2028 * is present in this field then no changes are to
2029 * be made to this parameter.
2030 * spp_pathmtu - When Path MTU discovery is disabled the value
2031 * specified here will be the "fixed" path mtu.
2032 * Note that if the spp_address field is empty
2033 * then all associations on this address will
2034 * have this fixed path mtu set upon them.
2035 *
2036 * spp_sackdelay - When delayed sack is enabled, this value specifies
2037 * the number of milliseconds that sacks will be delayed
2038 * for. This value will apply to all addresses of an
2039 * association if the spp_address field is empty. Note
2040 * also, that if delayed sack is enabled and this
2041 * value is set to 0, no change is made to the last
2042 * recorded delayed sack timer value.
2043 *
2044 * spp_flags - These flags are used to control various features
2045 * on an association. The flag field may contain
2046 * zero or more of the following options.
2047 *
2048 * SPP_HB_ENABLE - Enable heartbeats on the
2049 * specified address. Note that if the address
2050 * field is empty all addresses for the association
2051 * have heartbeats enabled upon them.
2052 *
2053 * SPP_HB_DISABLE - Disable heartbeats on the
2054 * speicifed address. Note that if the address
2055 * field is empty all addresses for the association
2056 * will have their heartbeats disabled. Note also
2057 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2058 * mutually exclusive, only one of these two should
2059 * be specified. Enabling both fields will have
2060 * undetermined results.
2061 *
2062 * SPP_HB_DEMAND - Request a user initiated heartbeat
2063 * to be made immediately.
2064 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002065 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2066 * heartbeat delayis to be set to the value of 0
2067 * milliseconds.
2068 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002069 * SPP_PMTUD_ENABLE - This field will enable PMTU
2070 * discovery upon the specified address. Note that
2071 * if the address feild is empty then all addresses
2072 * on the association are effected.
2073 *
2074 * SPP_PMTUD_DISABLE - This field will disable PMTU
2075 * discovery upon the specified address. Note that
2076 * if the address feild is empty then all addresses
2077 * on the association are effected. Not also that
2078 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2079 * exclusive. Enabling both will have undetermined
2080 * results.
2081 *
2082 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2083 * on delayed sack. The time specified in spp_sackdelay
2084 * is used to specify the sack delay for this address. Note
2085 * that if spp_address is empty then all addresses will
2086 * enable delayed sack and take on the sack delay
2087 * value specified in spp_sackdelay.
2088 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2089 * off delayed sack. If the spp_address field is blank then
2090 * delayed sack is disabled for the entire association. Note
2091 * also that this field is mutually exclusive to
2092 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2093 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 */
Adrian Bunk16164362006-09-18 00:40:38 -07002095static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2096 struct sctp_transport *trans,
2097 struct sctp_association *asoc,
2098 struct sctp_sock *sp,
2099 int hb_change,
2100 int pmtud_change,
2101 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 int error;
2104
Frank Filz52ccb8e2005-12-22 11:36:46 -08002105 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2107 if (error)
2108 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002111 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2112 * this field is ignored. Note also that a value of zero indicates
2113 * the current setting should be left unchanged.
2114 */
2115 if (params->spp_flags & SPP_HB_ENABLE) {
2116
2117 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2118 * set. This lets us use 0 value when this flag
2119 * is set.
2120 */
2121 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2122 params->spp_hbinterval = 0;
2123
2124 if (params->spp_hbinterval ||
2125 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2126 if (trans) {
2127 trans->hbinterval =
2128 msecs_to_jiffies(params->spp_hbinterval);
2129 } else if (asoc) {
2130 asoc->hbinterval =
2131 msecs_to_jiffies(params->spp_hbinterval);
2132 } else {
2133 sp->hbinterval = params->spp_hbinterval;
2134 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002135 }
2136 }
2137
2138 if (hb_change) {
2139 if (trans) {
2140 trans->param_flags =
2141 (trans->param_flags & ~SPP_HB) | hb_change;
2142 } else if (asoc) {
2143 asoc->param_flags =
2144 (asoc->param_flags & ~SPP_HB) | hb_change;
2145 } else {
2146 sp->param_flags =
2147 (sp->param_flags & ~SPP_HB) | hb_change;
2148 }
2149 }
2150
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002151 /* When Path MTU discovery is disabled the value specified here will
2152 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2153 * include the flag SPP_PMTUD_DISABLE for this field to have any
2154 * effect).
2155 */
2156 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002157 if (trans) {
2158 trans->pathmtu = params->spp_pathmtu;
2159 sctp_assoc_sync_pmtu(asoc);
2160 } else if (asoc) {
2161 asoc->pathmtu = params->spp_pathmtu;
2162 sctp_frag_point(sp, params->spp_pathmtu);
2163 } else {
2164 sp->pathmtu = params->spp_pathmtu;
2165 }
2166 }
2167
2168 if (pmtud_change) {
2169 if (trans) {
2170 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2171 (params->spp_flags & SPP_PMTUD_ENABLE);
2172 trans->param_flags =
2173 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2174 if (update) {
2175 sctp_transport_pmtu(trans);
2176 sctp_assoc_sync_pmtu(asoc);
2177 }
2178 } else if (asoc) {
2179 asoc->param_flags =
2180 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2181 } else {
2182 sp->param_flags =
2183 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2184 }
2185 }
2186
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002187 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2188 * value of this field is ignored. Note also that a value of zero
2189 * indicates the current setting should be left unchanged.
2190 */
2191 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002192 if (trans) {
2193 trans->sackdelay =
2194 msecs_to_jiffies(params->spp_sackdelay);
2195 } else if (asoc) {
2196 asoc->sackdelay =
2197 msecs_to_jiffies(params->spp_sackdelay);
2198 } else {
2199 sp->sackdelay = params->spp_sackdelay;
2200 }
2201 }
2202
2203 if (sackdelay_change) {
2204 if (trans) {
2205 trans->param_flags =
2206 (trans->param_flags & ~SPP_SACKDELAY) |
2207 sackdelay_change;
2208 } else if (asoc) {
2209 asoc->param_flags =
2210 (asoc->param_flags & ~SPP_SACKDELAY) |
2211 sackdelay_change;
2212 } else {
2213 sp->param_flags =
2214 (sp->param_flags & ~SPP_SACKDELAY) |
2215 sackdelay_change;
2216 }
2217 }
2218
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002219 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2220 * of this field is ignored. Note also that a value of zero
2221 * indicates the current setting should be left unchanged.
2222 */
2223 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002224 if (trans) {
2225 trans->pathmaxrxt = params->spp_pathmaxrxt;
2226 } else if (asoc) {
2227 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2228 } else {
2229 sp->pathmaxrxt = params->spp_pathmaxrxt;
2230 }
2231 }
2232
2233 return 0;
2234}
2235
2236static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2237 char __user *optval, int optlen)
2238{
2239 struct sctp_paddrparams params;
2240 struct sctp_transport *trans = NULL;
2241 struct sctp_association *asoc = NULL;
2242 struct sctp_sock *sp = sctp_sk(sk);
2243 int error;
2244 int hb_change, pmtud_change, sackdelay_change;
2245
2246 if (optlen != sizeof(struct sctp_paddrparams))
2247 return - EINVAL;
2248
2249 if (copy_from_user(&params, optval, optlen))
2250 return -EFAULT;
2251
2252 /* Validate flags and value parameters. */
2253 hb_change = params.spp_flags & SPP_HB;
2254 pmtud_change = params.spp_flags & SPP_PMTUD;
2255 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2256
2257 if (hb_change == SPP_HB ||
2258 pmtud_change == SPP_PMTUD ||
2259 sackdelay_change == SPP_SACKDELAY ||
2260 params.spp_sackdelay > 500 ||
2261 (params.spp_pathmtu
2262 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2263 return -EINVAL;
2264
2265 /* If an address other than INADDR_ANY is specified, and
2266 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002268 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2269 trans = sctp_addr_id2transport(sk, &params.spp_address,
2270 params.spp_assoc_id);
2271 if (!trans)
2272 return -EINVAL;
2273 }
2274
2275 /* Get association, if assoc_id != 0 and the socket is a one
2276 * to many style socket, and an association was not found, then
2277 * the id was invalid.
2278 */
2279 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2280 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2281 return -EINVAL;
2282
2283 /* Heartbeat demand can only be sent on a transport or
2284 * association, but not a socket.
2285 */
2286 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2287 return -EINVAL;
2288
2289 /* Process parameters. */
2290 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2291 hb_change, pmtud_change,
2292 sackdelay_change);
2293
2294 if (error)
2295 return error;
2296
2297 /* If changes are for association, also apply parameters to each
2298 * transport.
2299 */
2300 if (!trans && asoc) {
2301 struct list_head *pos;
2302
2303 list_for_each(pos, &asoc->peer.transport_addr_list) {
2304 trans = list_entry(pos, struct sctp_transport,
2305 transports);
2306 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2307 hb_change, pmtud_change,
2308 sackdelay_change);
2309 }
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 return 0;
2313}
2314
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002315/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08002316 *
2317 * This options will get or set the delayed ack timer. The time is set
2318 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2319 * endpoints default delayed ack timer value. If the assoc_id field is
2320 * non-zero, then the set or get effects the specified association.
2321 *
2322 * struct sctp_assoc_value {
2323 * sctp_assoc_t assoc_id;
2324 * uint32_t assoc_value;
2325 * };
2326 *
2327 * assoc_id - This parameter, indicates which association the
2328 * user is preforming an action upon. Note that if
2329 * this field's value is zero then the endpoints
2330 * default value is changed (effecting future
2331 * associations only).
2332 *
2333 * assoc_value - This parameter contains the number of milliseconds
2334 * that the user is requesting the delayed ACK timer
2335 * be set to. Note that this value is defined in
2336 * the standard to be between 200 and 500 milliseconds.
2337 *
2338 * Note: a value of zero will leave the value alone,
2339 * but disable SACK delay. A non-zero value will also
2340 * enable SACK delay.
2341 */
2342
2343static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2344 char __user *optval, int optlen)
2345{
2346 struct sctp_assoc_value params;
2347 struct sctp_transport *trans = NULL;
2348 struct sctp_association *asoc = NULL;
2349 struct sctp_sock *sp = sctp_sk(sk);
2350
2351 if (optlen != sizeof(struct sctp_assoc_value))
2352 return - EINVAL;
2353
2354 if (copy_from_user(&params, optval, optlen))
2355 return -EFAULT;
2356
2357 /* Validate value parameter. */
2358 if (params.assoc_value > 500)
2359 return -EINVAL;
2360
2361 /* Get association, if assoc_id != 0 and the socket is a one
2362 * to many style socket, and an association was not found, then
2363 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002364 */
Frank Filz77086102005-12-22 11:37:30 -08002365 asoc = sctp_id2assoc(sk, params.assoc_id);
2366 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2367 return -EINVAL;
2368
2369 if (params.assoc_value) {
2370 if (asoc) {
2371 asoc->sackdelay =
2372 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002373 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002374 (asoc->param_flags & ~SPP_SACKDELAY) |
2375 SPP_SACKDELAY_ENABLE;
2376 } else {
2377 sp->sackdelay = params.assoc_value;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002378 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002379 (sp->param_flags & ~SPP_SACKDELAY) |
2380 SPP_SACKDELAY_ENABLE;
2381 }
2382 } else {
2383 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002384 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002385 (asoc->param_flags & ~SPP_SACKDELAY) |
2386 SPP_SACKDELAY_DISABLE;
2387 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002388 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002389 (sp->param_flags & ~SPP_SACKDELAY) |
2390 SPP_SACKDELAY_DISABLE;
2391 }
2392 }
2393
2394 /* If change is for association, also apply to each transport. */
2395 if (asoc) {
2396 struct list_head *pos;
2397
2398 list_for_each(pos, &asoc->peer.transport_addr_list) {
2399 trans = list_entry(pos, struct sctp_transport,
2400 transports);
2401 if (params.assoc_value) {
2402 trans->sackdelay =
2403 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002404 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002405 (trans->param_flags & ~SPP_SACKDELAY) |
2406 SPP_SACKDELAY_ENABLE;
2407 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002408 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002409 (trans->param_flags & ~SPP_SACKDELAY) |
2410 SPP_SACKDELAY_DISABLE;
2411 }
2412 }
2413 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002414
Frank Filz77086102005-12-22 11:37:30 -08002415 return 0;
2416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2419 *
2420 * Applications can specify protocol parameters for the default association
2421 * initialization. The option name argument to setsockopt() and getsockopt()
2422 * is SCTP_INITMSG.
2423 *
2424 * Setting initialization parameters is effective only on an unconnected
2425 * socket (for UDP-style sockets only future associations are effected
2426 * by the change). With TCP-style sockets, this option is inherited by
2427 * sockets derived from a listener socket.
2428 */
2429static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2430{
2431 struct sctp_initmsg sinit;
2432 struct sctp_sock *sp = sctp_sk(sk);
2433
2434 if (optlen != sizeof(struct sctp_initmsg))
2435 return -EINVAL;
2436 if (copy_from_user(&sinit, optval, optlen))
2437 return -EFAULT;
2438
2439 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002440 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002442 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002444 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002446 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 return 0;
2449}
2450
2451/*
2452 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2453 *
2454 * Applications that wish to use the sendto() system call may wish to
2455 * specify a default set of parameters that would normally be supplied
2456 * through the inclusion of ancillary data. This socket option allows
2457 * such an application to set the default sctp_sndrcvinfo structure.
2458 * The application that wishes to use this socket option simply passes
2459 * in to this call the sctp_sndrcvinfo structure defined in Section
2460 * 5.2.2) The input parameters accepted by this call include
2461 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2462 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2463 * to this call if the caller is using the UDP model.
2464 */
2465static int sctp_setsockopt_default_send_param(struct sock *sk,
2466 char __user *optval, int optlen)
2467{
2468 struct sctp_sndrcvinfo info;
2469 struct sctp_association *asoc;
2470 struct sctp_sock *sp = sctp_sk(sk);
2471
2472 if (optlen != sizeof(struct sctp_sndrcvinfo))
2473 return -EINVAL;
2474 if (copy_from_user(&info, optval, optlen))
2475 return -EFAULT;
2476
2477 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2478 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2479 return -EINVAL;
2480
2481 if (asoc) {
2482 asoc->default_stream = info.sinfo_stream;
2483 asoc->default_flags = info.sinfo_flags;
2484 asoc->default_ppid = info.sinfo_ppid;
2485 asoc->default_context = info.sinfo_context;
2486 asoc->default_timetolive = info.sinfo_timetolive;
2487 } else {
2488 sp->default_stream = info.sinfo_stream;
2489 sp->default_flags = info.sinfo_flags;
2490 sp->default_ppid = info.sinfo_ppid;
2491 sp->default_context = info.sinfo_context;
2492 sp->default_timetolive = info.sinfo_timetolive;
2493 }
2494
2495 return 0;
2496}
2497
2498/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2499 *
2500 * Requests that the local SCTP stack use the enclosed peer address as
2501 * the association primary. The enclosed address must be one of the
2502 * association peer's addresses.
2503 */
2504static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2505 int optlen)
2506{
2507 struct sctp_prim prim;
2508 struct sctp_transport *trans;
2509
2510 if (optlen != sizeof(struct sctp_prim))
2511 return -EINVAL;
2512
2513 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2514 return -EFAULT;
2515
2516 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2517 if (!trans)
2518 return -EINVAL;
2519
2520 sctp_assoc_set_primary(trans->asoc, trans);
2521
2522 return 0;
2523}
2524
2525/*
2526 * 7.1.5 SCTP_NODELAY
2527 *
2528 * Turn on/off any Nagle-like algorithm. This means that packets are
2529 * generally sent as soon as possible and no unnecessary delays are
2530 * introduced, at the cost of more packets in the network. Expects an
2531 * integer boolean flag.
2532 */
2533static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2534 int optlen)
2535{
2536 int val;
2537
2538 if (optlen < sizeof(int))
2539 return -EINVAL;
2540 if (get_user(val, (int __user *)optval))
2541 return -EFAULT;
2542
2543 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2544 return 0;
2545}
2546
2547/*
2548 *
2549 * 7.1.1 SCTP_RTOINFO
2550 *
2551 * The protocol parameters used to initialize and bound retransmission
2552 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2553 * and modify these parameters.
2554 * All parameters are time values, in milliseconds. A value of 0, when
2555 * modifying the parameters, indicates that the current value should not
2556 * be changed.
2557 *
2558 */
2559static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2560 struct sctp_rtoinfo rtoinfo;
2561 struct sctp_association *asoc;
2562
2563 if (optlen != sizeof (struct sctp_rtoinfo))
2564 return -EINVAL;
2565
2566 if (copy_from_user(&rtoinfo, optval, optlen))
2567 return -EFAULT;
2568
2569 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2570
2571 /* Set the values to the specific association */
2572 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2573 return -EINVAL;
2574
2575 if (asoc) {
2576 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002577 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 msecs_to_jiffies(rtoinfo.srto_initial);
2579 if (rtoinfo.srto_max != 0)
2580 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2581 if (rtoinfo.srto_min != 0)
2582 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2583 } else {
2584 /* If there is no association or the association-id = 0
2585 * set the values to the endpoint.
2586 */
2587 struct sctp_sock *sp = sctp_sk(sk);
2588
2589 if (rtoinfo.srto_initial != 0)
2590 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2591 if (rtoinfo.srto_max != 0)
2592 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2593 if (rtoinfo.srto_min != 0)
2594 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2595 }
2596
2597 return 0;
2598}
2599
2600/*
2601 *
2602 * 7.1.2 SCTP_ASSOCINFO
2603 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002604 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 * of the association.
2606 * Returns an error if the new association retransmission value is
2607 * greater than the sum of the retransmission value of the peer.
2608 * See [SCTP] for more information.
2609 *
2610 */
2611static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2612{
2613
2614 struct sctp_assocparams assocparams;
2615 struct sctp_association *asoc;
2616
2617 if (optlen != sizeof(struct sctp_assocparams))
2618 return -EINVAL;
2619 if (copy_from_user(&assocparams, optval, optlen))
2620 return -EFAULT;
2621
2622 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2623
2624 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2625 return -EINVAL;
2626
2627 /* Set the values to the specific association */
2628 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002629 if (assocparams.sasoc_asocmaxrxt != 0) {
2630 __u32 path_sum = 0;
2631 int paths = 0;
2632 struct list_head *pos;
2633 struct sctp_transport *peer_addr;
2634
2635 list_for_each(pos, &asoc->peer.transport_addr_list) {
2636 peer_addr = list_entry(pos,
2637 struct sctp_transport,
2638 transports);
2639 path_sum += peer_addr->pathmaxrxt;
2640 paths++;
2641 }
2642
2643 /* Only validate asocmaxrxt if we have more then
2644 * one path/transport. We do this because path
2645 * retransmissions are only counted when we have more
2646 * then one path.
2647 */
2648 if (paths > 1 &&
2649 assocparams.sasoc_asocmaxrxt > path_sum)
2650 return -EINVAL;
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002653 }
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (assocparams.sasoc_cookie_life != 0) {
2656 asoc->cookie_life.tv_sec =
2657 assocparams.sasoc_cookie_life / 1000;
2658 asoc->cookie_life.tv_usec =
2659 (assocparams.sasoc_cookie_life % 1000)
2660 * 1000;
2661 }
2662 } else {
2663 /* Set the values to the endpoint */
2664 struct sctp_sock *sp = sctp_sk(sk);
2665
2666 if (assocparams.sasoc_asocmaxrxt != 0)
2667 sp->assocparams.sasoc_asocmaxrxt =
2668 assocparams.sasoc_asocmaxrxt;
2669 if (assocparams.sasoc_cookie_life != 0)
2670 sp->assocparams.sasoc_cookie_life =
2671 assocparams.sasoc_cookie_life;
2672 }
2673 return 0;
2674}
2675
2676/*
2677 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2678 *
2679 * This socket option is a boolean flag which turns on or off mapped V4
2680 * addresses. If this option is turned on and the socket is type
2681 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2682 * If this option is turned off, then no mapping will be done of V4
2683 * addresses and a user will receive both PF_INET6 and PF_INET type
2684 * addresses on the socket.
2685 */
2686static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2687{
2688 int val;
2689 struct sctp_sock *sp = sctp_sk(sk);
2690
2691 if (optlen < sizeof(int))
2692 return -EINVAL;
2693 if (get_user(val, (int __user *)optval))
2694 return -EFAULT;
2695 if (val)
2696 sp->v4mapped = 1;
2697 else
2698 sp->v4mapped = 0;
2699
2700 return 0;
2701}
2702
2703/*
2704 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2705 *
2706 * This socket option specifies the maximum size to put in any outgoing
2707 * SCTP chunk. If a message is larger than this size it will be
2708 * fragmented by SCTP into the specified size. Note that the underlying
2709 * SCTP implementation may fragment into smaller sized chunks when the
2710 * PMTU of the underlying association is smaller than the value set by
2711 * the user.
2712 */
2713static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2714{
2715 struct sctp_association *asoc;
2716 struct list_head *pos;
2717 struct sctp_sock *sp = sctp_sk(sk);
2718 int val;
2719
2720 if (optlen < sizeof(int))
2721 return -EINVAL;
2722 if (get_user(val, (int __user *)optval))
2723 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002724 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -EINVAL;
2726 sp->user_frag = val;
2727
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002728 /* Update the frag_point of the existing associations. */
2729 list_for_each(pos, &(sp->ep->asocs)) {
2730 asoc = list_entry(pos, struct sctp_association, asocs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002731 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
2733
2734 return 0;
2735}
2736
2737
2738/*
2739 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2740 *
2741 * Requests that the peer mark the enclosed address as the association
2742 * primary. The enclosed address must be one of the association's
2743 * locally bound addresses. The following structure is used to make a
2744 * set primary request:
2745 */
2746static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2747 int optlen)
2748{
2749 struct sctp_sock *sp;
2750 struct sctp_endpoint *ep;
2751 struct sctp_association *asoc = NULL;
2752 struct sctp_setpeerprim prim;
2753 struct sctp_chunk *chunk;
2754 int err;
2755
2756 sp = sctp_sk(sk);
2757 ep = sp->ep;
2758
2759 if (!sctp_addip_enable)
2760 return -EPERM;
2761
2762 if (optlen != sizeof(struct sctp_setpeerprim))
2763 return -EINVAL;
2764
2765 if (copy_from_user(&prim, optval, optlen))
2766 return -EFAULT;
2767
2768 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002769 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -EINVAL;
2771
2772 if (!asoc->peer.asconf_capable)
2773 return -EPERM;
2774
2775 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2776 return -EPERM;
2777
2778 if (!sctp_state(asoc, ESTABLISHED))
2779 return -ENOTCONN;
2780
2781 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2782 return -EADDRNOTAVAIL;
2783
2784 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2785 chunk = sctp_make_asconf_set_prim(asoc,
2786 (union sctp_addr *)&prim.sspp_addr);
2787 if (!chunk)
2788 return -ENOMEM;
2789
2790 err = sctp_send_asconf(asoc, chunk);
2791
2792 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2793
2794 return err;
2795}
2796
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002797static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 int optlen)
2799{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002800 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002802 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002804 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return -EFAULT;
2806
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002807 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 return 0;
2810}
2811
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08002812/*
2813 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2814 *
2815 * The context field in the sctp_sndrcvinfo structure is normally only
2816 * used when a failed message is retrieved holding the value that was
2817 * sent down on the actual send call. This option allows the setting of
2818 * a default context on an association basis that will be received on
2819 * reading messages from the peer. This is especially helpful in the
2820 * one-2-many model for an application to keep some reference to an
2821 * internal state machine that is processing messages on the
2822 * association. Note that the setting of this value only effects
2823 * received messages from the peer and does not effect the value that is
2824 * saved with outbound messages.
2825 */
2826static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2827 int optlen)
2828{
2829 struct sctp_assoc_value params;
2830 struct sctp_sock *sp;
2831 struct sctp_association *asoc;
2832
2833 if (optlen != sizeof(struct sctp_assoc_value))
2834 return -EINVAL;
2835 if (copy_from_user(&params, optval, optlen))
2836 return -EFAULT;
2837
2838 sp = sctp_sk(sk);
2839
2840 if (params.assoc_id != 0) {
2841 asoc = sctp_id2assoc(sk, params.assoc_id);
2842 if (!asoc)
2843 return -EINVAL;
2844 asoc->default_rcv_context = params.assoc_value;
2845 } else {
2846 sp->default_rcv_context = params.assoc_value;
2847 }
2848
2849 return 0;
2850}
2851
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002852/*
2853 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2854 *
2855 * This options will at a minimum specify if the implementation is doing
2856 * fragmented interleave. Fragmented interleave, for a one to many
2857 * socket, is when subsequent calls to receive a message may return
2858 * parts of messages from different associations. Some implementations
2859 * may allow you to turn this value on or off. If so, when turned off,
2860 * no fragment interleave will occur (which will cause a head of line
2861 * blocking amongst multiple associations sharing the same one to many
2862 * socket). When this option is turned on, then each receive call may
2863 * come from a different association (thus the user must receive data
2864 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2865 * association each receive belongs to.
2866 *
2867 * This option takes a boolean value. A non-zero value indicates that
2868 * fragmented interleave is on. A value of zero indicates that
2869 * fragmented interleave is off.
2870 *
2871 * Note that it is important that an implementation that allows this
2872 * option to be turned on, have it off by default. Otherwise an unaware
2873 * application using the one to many model may become confused and act
2874 * incorrectly.
2875 */
2876static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2877 char __user *optval,
2878 int optlen)
2879{
2880 int val;
2881
2882 if (optlen != sizeof(int))
2883 return -EINVAL;
2884 if (get_user(val, (int __user *)optval))
2885 return -EFAULT;
2886
2887 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2888
2889 return 0;
2890}
2891
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07002892/*
2893 * 7.1.25. Set or Get the sctp partial delivery point
2894 * (SCTP_PARTIAL_DELIVERY_POINT)
2895 * This option will set or get the SCTP partial delivery point. This
2896 * point is the size of a message where the partial delivery API will be
2897 * invoked to help free up rwnd space for the peer. Setting this to a
2898 * lower value will cause partial delivery's to happen more often. The
2899 * calls argument is an integer that sets or gets the partial delivery
2900 * point.
2901 */
2902static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2903 char __user *optval,
2904 int optlen)
2905{
2906 u32 val;
2907
2908 if (optlen != sizeof(u32))
2909 return -EINVAL;
2910 if (get_user(val, (int __user *)optval))
2911 return -EFAULT;
2912
2913 sctp_sk(sk)->pd_point = val;
2914
2915 return 0; /* is this the right error code? */
2916}
2917
Vlad Yasevich70331572007-03-23 11:34:36 -07002918/*
2919 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2920 *
2921 * This option will allow a user to change the maximum burst of packets
2922 * that can be emitted by this association. Note that the default value
2923 * is 4, and some implementations may restrict this setting so that it
2924 * can only be lowered.
2925 *
2926 * NOTE: This text doesn't seem right. Do this on a socket basis with
2927 * future associations inheriting the socket value.
2928 */
2929static int sctp_setsockopt_maxburst(struct sock *sk,
2930 char __user *optval,
2931 int optlen)
2932{
2933 int val;
2934
2935 if (optlen != sizeof(int))
2936 return -EINVAL;
2937 if (get_user(val, (int __user *)optval))
2938 return -EFAULT;
2939
2940 if (val < 0)
2941 return -EINVAL;
2942
2943 sctp_sk(sk)->max_burst = val;
2944
2945 return 0;
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948/* API 6.2 setsockopt(), getsockopt()
2949 *
2950 * Applications use setsockopt() and getsockopt() to set or retrieve
2951 * socket options. Socket options are used to change the default
2952 * behavior of sockets calls. They are described in Section 7.
2953 *
2954 * The syntax is:
2955 *
2956 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2957 * int __user *optlen);
2958 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2959 * int optlen);
2960 *
2961 * sd - the socket descript.
2962 * level - set to IPPROTO_SCTP for all SCTP options.
2963 * optname - the option name.
2964 * optval - the buffer to store the value of the option.
2965 * optlen - the size of the buffer.
2966 */
2967SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2968 char __user *optval, int optlen)
2969{
2970 int retval = 0;
2971
2972 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2973 sk, optname);
2974
2975 /* I can hardly begin to describe how wrong this is. This is
2976 * so broken as to be worse than useless. The API draft
2977 * REALLY is NOT helpful here... I am not convinced that the
2978 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2979 * are at all well-founded.
2980 */
2981 if (level != SOL_SCTP) {
2982 struct sctp_af *af = sctp_sk(sk)->pf->af;
2983 retval = af->setsockopt(sk, level, optname, optval, optlen);
2984 goto out_nounlock;
2985 }
2986
2987 sctp_lock_sock(sk);
2988
2989 switch (optname) {
2990 case SCTP_SOCKOPT_BINDX_ADD:
2991 /* 'optlen' is the size of the addresses buffer. */
2992 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2993 optlen, SCTP_BINDX_ADD_ADDR);
2994 break;
2995
2996 case SCTP_SOCKOPT_BINDX_REM:
2997 /* 'optlen' is the size of the addresses buffer. */
2998 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2999 optlen, SCTP_BINDX_REM_ADDR);
3000 break;
3001
Frank Filz3f7a87d2005-06-20 13:14:57 -07003002 case SCTP_SOCKOPT_CONNECTX:
3003 /* 'optlen' is the size of the addresses buffer. */
3004 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3005 optlen);
3006 break;
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 case SCTP_DISABLE_FRAGMENTS:
3009 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3010 break;
3011
3012 case SCTP_EVENTS:
3013 retval = sctp_setsockopt_events(sk, optval, optlen);
3014 break;
3015
3016 case SCTP_AUTOCLOSE:
3017 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3018 break;
3019
3020 case SCTP_PEER_ADDR_PARAMS:
3021 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3022 break;
3023
Frank Filz77086102005-12-22 11:37:30 -08003024 case SCTP_DELAYED_ACK_TIME:
3025 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3026 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003027 case SCTP_PARTIAL_DELIVERY_POINT:
3028 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3029 break;
Frank Filz77086102005-12-22 11:37:30 -08003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 case SCTP_INITMSG:
3032 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3033 break;
3034 case SCTP_DEFAULT_SEND_PARAM:
3035 retval = sctp_setsockopt_default_send_param(sk, optval,
3036 optlen);
3037 break;
3038 case SCTP_PRIMARY_ADDR:
3039 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3040 break;
3041 case SCTP_SET_PEER_PRIMARY_ADDR:
3042 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3043 break;
3044 case SCTP_NODELAY:
3045 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3046 break;
3047 case SCTP_RTOINFO:
3048 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3049 break;
3050 case SCTP_ASSOCINFO:
3051 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3052 break;
3053 case SCTP_I_WANT_MAPPED_V4_ADDR:
3054 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3055 break;
3056 case SCTP_MAXSEG:
3057 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3058 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003059 case SCTP_ADAPTATION_LAYER:
3060 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003062 case SCTP_CONTEXT:
3063 retval = sctp_setsockopt_context(sk, optval, optlen);
3064 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003065 case SCTP_FRAGMENT_INTERLEAVE:
3066 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3067 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003068 case SCTP_MAX_BURST:
3069 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 default:
3072 retval = -ENOPROTOOPT;
3073 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 sctp_release_sock(sk);
3077
3078out_nounlock:
3079 return retval;
3080}
3081
3082/* API 3.1.6 connect() - UDP Style Syntax
3083 *
3084 * An application may use the connect() call in the UDP model to initiate an
3085 * association without sending data.
3086 *
3087 * The syntax is:
3088 *
3089 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3090 *
3091 * sd: the socket descriptor to have a new association added to.
3092 *
3093 * nam: the address structure (either struct sockaddr_in or struct
3094 * sockaddr_in6 defined in RFC2553 [7]).
3095 *
3096 * len: the size of the address.
3097 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003098SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 int addr_len)
3100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003102 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 sctp_lock_sock(sk);
3105
Frank Filz3f7a87d2005-06-20 13:14:57 -07003106 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3107 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Frank Filz3f7a87d2005-06-20 13:14:57 -07003109 /* Validate addr_len before calling common connect/connectx routine. */
3110 af = sctp_get_af_specific(addr->sa_family);
3111 if (!af || addr_len < af->sockaddr_len) {
3112 err = -EINVAL;
3113 } else {
3114 /* Pass correct addr len to common routine (so it knows there
3115 * is only one address being passed.
3116 */
3117 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return err;
3122}
3123
3124/* FIXME: Write comments. */
3125SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3126{
3127 return -EOPNOTSUPP; /* STUB */
3128}
3129
3130/* 4.1.4 accept() - TCP Style Syntax
3131 *
3132 * Applications use accept() call to remove an established SCTP
3133 * association from the accept queue of the endpoint. A new socket
3134 * descriptor will be returned from accept() to represent the newly
3135 * formed association.
3136 */
3137SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3138{
3139 struct sctp_sock *sp;
3140 struct sctp_endpoint *ep;
3141 struct sock *newsk = NULL;
3142 struct sctp_association *asoc;
3143 long timeo;
3144 int error = 0;
3145
3146 sctp_lock_sock(sk);
3147
3148 sp = sctp_sk(sk);
3149 ep = sp->ep;
3150
3151 if (!sctp_style(sk, TCP)) {
3152 error = -EOPNOTSUPP;
3153 goto out;
3154 }
3155
3156 if (!sctp_sstate(sk, LISTENING)) {
3157 error = -EINVAL;
3158 goto out;
3159 }
3160
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003161 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 error = sctp_wait_for_accept(sk, timeo);
3164 if (error)
3165 goto out;
3166
3167 /* We treat the list of associations on the endpoint as the accept
3168 * queue and pick the first association on the list.
3169 */
3170 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3171
3172 newsk = sp->pf->create_accept_sk(sk, asoc);
3173 if (!newsk) {
3174 error = -ENOMEM;
3175 goto out;
3176 }
3177
3178 /* Populate the fields of the newsk from the oldsk and migrate the
3179 * asoc to the newsk.
3180 */
3181 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3182
3183out:
3184 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003185 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return newsk;
3187}
3188
3189/* The SCTP ioctl handler. */
3190SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3191{
3192 return -ENOIOCTLCMD;
3193}
3194
3195/* This is the function which gets called during socket creation to
3196 * initialized the SCTP-specific portion of the sock.
3197 * The sock structure should already be zero-filled memory.
3198 */
3199SCTP_STATIC int sctp_init_sock(struct sock *sk)
3200{
3201 struct sctp_endpoint *ep;
3202 struct sctp_sock *sp;
3203
3204 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3205
3206 sp = sctp_sk(sk);
3207
3208 /* Initialize the SCTP per socket area. */
3209 switch (sk->sk_type) {
3210 case SOCK_SEQPACKET:
3211 sp->type = SCTP_SOCKET_UDP;
3212 break;
3213 case SOCK_STREAM:
3214 sp->type = SCTP_SOCKET_TCP;
3215 break;
3216 default:
3217 return -ESOCKTNOSUPPORT;
3218 }
3219
3220 /* Initialize default send parameters. These parameters can be
3221 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3222 */
3223 sp->default_stream = 0;
3224 sp->default_ppid = 0;
3225 sp->default_flags = 0;
3226 sp->default_context = 0;
3227 sp->default_timetolive = 0;
3228
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003229 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003230 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 /* Initialize default setup parameters. These parameters
3233 * can be modified with the SCTP_INITMSG socket option or
3234 * overridden by the SCTP_INIT CMSG.
3235 */
3236 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3237 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3238 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003239 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241 /* Initialize default RTO related parameters. These parameters can
3242 * be modified for with the SCTP_RTOINFO socket option.
3243 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003244 sp->rtoinfo.srto_initial = sctp_rto_initial;
3245 sp->rtoinfo.srto_max = sctp_rto_max;
3246 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 /* Initialize default association related parameters. These parameters
3249 * can be modified with the SCTP_ASSOCINFO socket option.
3250 */
3251 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3252 sp->assocparams.sasoc_number_peer_destinations = 0;
3253 sp->assocparams.sasoc_peer_rwnd = 0;
3254 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003255 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003258 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 */
3260 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3261
3262 /* Default Peer Address Parameters. These defaults can
3263 * be modified via SCTP_PEER_ADDR_PARAMS
3264 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003265 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003266 sp->pathmaxrxt = sctp_max_retrans_path;
3267 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003268 sp->sackdelay = sctp_sack_timeout;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003269 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003270 SPP_PMTUD_ENABLE |
3271 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* If enabled no SCTP message fragmentation will be performed.
3274 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3275 */
3276 sp->disable_fragments = 0;
3277
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003278 /* Enable Nagle algorithm by default. */
3279 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 /* Enable by default. */
3282 sp->v4mapped = 1;
3283
3284 /* Auto-close idle associations after the configured
3285 * number of seconds. A value of 0 disables this
3286 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3287 * for UDP-style sockets only.
3288 */
3289 sp->autoclose = 0;
3290
3291 /* User specified fragmentation limit. */
3292 sp->user_frag = 0;
3293
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003294 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 sp->pf = sctp_get_pf_specific(sk->sk_family);
3297
3298 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003299 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003301 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 /* Create a per socket endpoint structure. Even if we
3304 * change the data structure relationships, this may still
3305 * be useful for storing pre-connect address information.
3306 */
3307 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3308 if (!ep)
3309 return -ENOMEM;
3310
3311 sp->ep = ep;
3312 sp->hmac = NULL;
3313
3314 SCTP_DBG_OBJCNT_INC(sock);
3315 return 0;
3316}
3317
3318/* Cleanup any SCTP per socket resources. */
3319SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3320{
3321 struct sctp_endpoint *ep;
3322
3323 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3324
3325 /* Release our hold on the endpoint. */
3326 ep = sctp_sk(sk)->ep;
3327 sctp_endpoint_free(ep);
3328
3329 return 0;
3330}
3331
3332/* API 4.1.7 shutdown() - TCP Style Syntax
3333 * int shutdown(int socket, int how);
3334 *
3335 * sd - the socket descriptor of the association to be closed.
3336 * how - Specifies the type of shutdown. The values are
3337 * as follows:
3338 * SHUT_RD
3339 * Disables further receive operations. No SCTP
3340 * protocol action is taken.
3341 * SHUT_WR
3342 * Disables further send operations, and initiates
3343 * the SCTP shutdown sequence.
3344 * SHUT_RDWR
3345 * Disables further send and receive operations
3346 * and initiates the SCTP shutdown sequence.
3347 */
3348SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3349{
3350 struct sctp_endpoint *ep;
3351 struct sctp_association *asoc;
3352
3353 if (!sctp_style(sk, TCP))
3354 return;
3355
3356 if (how & SEND_SHUTDOWN) {
3357 ep = sctp_sk(sk)->ep;
3358 if (!list_empty(&ep->asocs)) {
3359 asoc = list_entry(ep->asocs.next,
3360 struct sctp_association, asocs);
3361 sctp_primitive_SHUTDOWN(asoc, NULL);
3362 }
3363 }
3364}
3365
3366/* 7.2.1 Association Status (SCTP_STATUS)
3367
3368 * Applications can retrieve current status information about an
3369 * association, including association state, peer receiver window size,
3370 * number of unacked data chunks, and number of data chunks pending
3371 * receipt. This information is read-only.
3372 */
3373static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3374 char __user *optval,
3375 int __user *optlen)
3376{
3377 struct sctp_status status;
3378 struct sctp_association *asoc = NULL;
3379 struct sctp_transport *transport;
3380 sctp_assoc_t associd;
3381 int retval = 0;
3382
Neil Horman408f22e2007-06-16 14:03:45 -04003383 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 retval = -EINVAL;
3385 goto out;
3386 }
3387
Neil Horman408f22e2007-06-16 14:03:45 -04003388 len = sizeof(status);
3389 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 retval = -EFAULT;
3391 goto out;
3392 }
3393
3394 associd = status.sstat_assoc_id;
3395 asoc = sctp_id2assoc(sk, associd);
3396 if (!asoc) {
3397 retval = -EINVAL;
3398 goto out;
3399 }
3400
3401 transport = asoc->peer.primary_path;
3402
3403 status.sstat_assoc_id = sctp_assoc2id(asoc);
3404 status.sstat_state = asoc->state;
3405 status.sstat_rwnd = asoc->peer.rwnd;
3406 status.sstat_unackdata = asoc->unack_data;
3407
3408 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3409 status.sstat_instrms = asoc->c.sinit_max_instreams;
3410 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3411 status.sstat_fragmentation_point = asoc->frag_point;
3412 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08003413 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3414 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /* Map ipv4 address into v4-mapped-on-v6 address. */
3416 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3417 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003418 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3420 status.sstat_primary.spinfo_srtt = transport->srtt;
3421 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003422 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Frank Filz3f7a87d2005-06-20 13:14:57 -07003424 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3425 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (put_user(len, optlen)) {
3428 retval = -EFAULT;
3429 goto out;
3430 }
3431
3432 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3433 len, status.sstat_state, status.sstat_rwnd,
3434 status.sstat_assoc_id);
3435
3436 if (copy_to_user(optval, &status, len)) {
3437 retval = -EFAULT;
3438 goto out;
3439 }
3440
3441out:
3442 return (retval);
3443}
3444
3445
3446/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3447 *
3448 * Applications can retrieve information about a specific peer address
3449 * of an association, including its reachability state, congestion
3450 * window, and retransmission timer values. This information is
3451 * read-only.
3452 */
3453static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3454 char __user *optval,
3455 int __user *optlen)
3456{
3457 struct sctp_paddrinfo pinfo;
3458 struct sctp_transport *transport;
3459 int retval = 0;
3460
Neil Horman408f22e2007-06-16 14:03:45 -04003461 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 retval = -EINVAL;
3463 goto out;
3464 }
3465
Neil Horman408f22e2007-06-16 14:03:45 -04003466 len = sizeof(pinfo);
3467 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 retval = -EFAULT;
3469 goto out;
3470 }
3471
3472 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3473 pinfo.spinfo_assoc_id);
3474 if (!transport)
3475 return -EINVAL;
3476
3477 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003478 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 pinfo.spinfo_cwnd = transport->cwnd;
3480 pinfo.spinfo_srtt = transport->srtt;
3481 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003482 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Frank Filz3f7a87d2005-06-20 13:14:57 -07003484 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3485 pinfo.spinfo_state = SCTP_ACTIVE;
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (put_user(len, optlen)) {
3488 retval = -EFAULT;
3489 goto out;
3490 }
3491
3492 if (copy_to_user(optval, &pinfo, len)) {
3493 retval = -EFAULT;
3494 goto out;
3495 }
3496
3497out:
3498 return (retval);
3499}
3500
3501/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3502 *
3503 * This option is a on/off flag. If enabled no SCTP message
3504 * fragmentation will be performed. Instead if a message being sent
3505 * exceeds the current PMTU size, the message will NOT be sent and
3506 * instead a error will be indicated to the user.
3507 */
3508static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3509 char __user *optval, int __user *optlen)
3510{
3511 int val;
3512
3513 if (len < sizeof(int))
3514 return -EINVAL;
3515
3516 len = sizeof(int);
3517 val = (sctp_sk(sk)->disable_fragments == 1);
3518 if (put_user(len, optlen))
3519 return -EFAULT;
3520 if (copy_to_user(optval, &val, len))
3521 return -EFAULT;
3522 return 0;
3523}
3524
3525/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3526 *
3527 * This socket option is used to specify various notifications and
3528 * ancillary data the user wishes to receive.
3529 */
3530static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3531 int __user *optlen)
3532{
Neil Horman408f22e2007-06-16 14:03:45 -04003533 if (len < sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003535 len = sizeof(struct sctp_event_subscribe);
3536 if (put_user(len, optlen))
3537 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3539 return -EFAULT;
3540 return 0;
3541}
3542
3543/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3544 *
3545 * This socket option is applicable to the UDP-style socket only. When
3546 * set it will cause associations that are idle for more than the
3547 * specified number of seconds to automatically close. An association
3548 * being idle is defined an association that has NOT sent or received
3549 * user data. The special value of '0' indicates that no automatic
3550 * close of any associations should be performed. The option expects an
3551 * integer defining the number of seconds of idle time before an
3552 * association is closed.
3553 */
3554static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3555{
3556 /* Applicable to UDP-style socket only */
3557 if (sctp_style(sk, TCP))
3558 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04003559 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003561 len = sizeof(int);
3562 if (put_user(len, optlen))
3563 return -EFAULT;
3564 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return -EFAULT;
3566 return 0;
3567}
3568
3569/* Helper routine to branch off an association to a new socket. */
3570SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3571 struct socket **sockp)
3572{
3573 struct sock *sk = asoc->base.sk;
3574 struct socket *sock;
Vlad Yasevich4f444302006-10-30 18:54:32 -08003575 struct inet_sock *inetsk;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003576 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 int err = 0;
3578
3579 /* An association cannot be branched off from an already peeled-off
3580 * socket, nor is this supported for tcp style sockets.
3581 */
3582 if (!sctp_style(sk, UDP))
3583 return -EINVAL;
3584
3585 /* Create a new socket. */
3586 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3587 if (err < 0)
3588 return err;
3589
3590 /* Populate the fields of the newsk from the oldsk and migrate the
3591 * asoc to the newsk.
3592 */
3593 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003594
3595 /* Make peeled-off sockets more like 1-1 accepted sockets.
3596 * Set the daddr and initialize id to something more random
3597 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003598 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3599 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003600 inetsk = inet_sk(sock->sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003601 inetsk->id = asoc->next_tsn ^ jiffies;
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 *sockp = sock;
3604
3605 return err;
3606}
3607
3608static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3609{
3610 sctp_peeloff_arg_t peeloff;
3611 struct socket *newsock;
3612 int retval = 0;
3613 struct sctp_association *asoc;
3614
Neil Horman408f22e2007-06-16 14:03:45 -04003615 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003617 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 if (copy_from_user(&peeloff, optval, len))
3619 return -EFAULT;
3620
3621 asoc = sctp_id2assoc(sk, peeloff.associd);
3622 if (!asoc) {
3623 retval = -EINVAL;
3624 goto out;
3625 }
3626
3627 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3628
3629 retval = sctp_do_peeloff(asoc, &newsock);
3630 if (retval < 0)
3631 goto out;
3632
3633 /* Map the socket to an unused fd that can be returned to the user. */
3634 retval = sock_map_fd(newsock);
3635 if (retval < 0) {
3636 sock_release(newsock);
3637 goto out;
3638 }
3639
3640 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3641 __FUNCTION__, sk, asoc, newsock->sk, retval);
3642
3643 /* Return the fd mapped to the new socket. */
3644 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04003645 if (put_user(len, optlen))
3646 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 if (copy_to_user(optval, &peeloff, len))
3648 retval = -EFAULT;
3649
3650out:
3651 return retval;
3652}
3653
3654/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3655 *
3656 * Applications can enable or disable heartbeats for any peer address of
3657 * an association, modify an address's heartbeat interval, force a
3658 * heartbeat to be sent immediately, and adjust the address's maximum
3659 * number of retransmissions sent before an address is considered
3660 * unreachable. The following structure is used to access and modify an
3661 * address's parameters:
3662 *
3663 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003664 * sctp_assoc_t spp_assoc_id;
3665 * struct sockaddr_storage spp_address;
3666 * uint32_t spp_hbinterval;
3667 * uint16_t spp_pathmaxrxt;
3668 * uint32_t spp_pathmtu;
3669 * uint32_t spp_sackdelay;
3670 * uint32_t spp_flags;
3671 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003673 * spp_assoc_id - (one-to-many style socket) This is filled in the
3674 * application, and identifies the association for
3675 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * spp_address - This specifies which address is of interest.
3677 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003678 * in milliseconds. If a value of zero
3679 * is present in this field then no changes are to
3680 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * spp_pathmaxrxt - This contains the maximum number of
3682 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003683 * considered unreachable. If a value of zero
3684 * is present in this field then no changes are to
3685 * be made to this parameter.
3686 * spp_pathmtu - When Path MTU discovery is disabled the value
3687 * specified here will be the "fixed" path mtu.
3688 * Note that if the spp_address field is empty
3689 * then all associations on this address will
3690 * have this fixed path mtu set upon them.
3691 *
3692 * spp_sackdelay - When delayed sack is enabled, this value specifies
3693 * the number of milliseconds that sacks will be delayed
3694 * for. This value will apply to all addresses of an
3695 * association if the spp_address field is empty. Note
3696 * also, that if delayed sack is enabled and this
3697 * value is set to 0, no change is made to the last
3698 * recorded delayed sack timer value.
3699 *
3700 * spp_flags - These flags are used to control various features
3701 * on an association. The flag field may contain
3702 * zero or more of the following options.
3703 *
3704 * SPP_HB_ENABLE - Enable heartbeats on the
3705 * specified address. Note that if the address
3706 * field is empty all addresses for the association
3707 * have heartbeats enabled upon them.
3708 *
3709 * SPP_HB_DISABLE - Disable heartbeats on the
3710 * speicifed address. Note that if the address
3711 * field is empty all addresses for the association
3712 * will have their heartbeats disabled. Note also
3713 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3714 * mutually exclusive, only one of these two should
3715 * be specified. Enabling both fields will have
3716 * undetermined results.
3717 *
3718 * SPP_HB_DEMAND - Request a user initiated heartbeat
3719 * to be made immediately.
3720 *
3721 * SPP_PMTUD_ENABLE - This field will enable PMTU
3722 * discovery upon the specified address. Note that
3723 * if the address feild is empty then all addresses
3724 * on the association are effected.
3725 *
3726 * SPP_PMTUD_DISABLE - This field will disable PMTU
3727 * discovery upon the specified address. Note that
3728 * if the address feild is empty then all addresses
3729 * on the association are effected. Not also that
3730 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3731 * exclusive. Enabling both will have undetermined
3732 * results.
3733 *
3734 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3735 * on delayed sack. The time specified in spp_sackdelay
3736 * is used to specify the sack delay for this address. Note
3737 * that if spp_address is empty then all addresses will
3738 * enable delayed sack and take on the sack delay
3739 * value specified in spp_sackdelay.
3740 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3741 * off delayed sack. If the spp_address field is blank then
3742 * delayed sack is disabled for the entire association. Note
3743 * also that this field is mutually exclusive to
3744 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3745 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 */
3747static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003748 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003750 struct sctp_paddrparams params;
3751 struct sctp_transport *trans = NULL;
3752 struct sctp_association *asoc = NULL;
3753 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Neil Horman408f22e2007-06-16 14:03:45 -04003755 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003757 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (copy_from_user(&params, optval, len))
3759 return -EFAULT;
3760
Frank Filz52ccb8e2005-12-22 11:36:46 -08003761 /* If an address other than INADDR_ANY is specified, and
3762 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003764 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3765 trans = sctp_addr_id2transport(sk, &params.spp_address,
3766 params.spp_assoc_id);
3767 if (!trans) {
3768 SCTP_DEBUG_PRINTK("Failed no transport\n");
3769 return -EINVAL;
3770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
3772
Frank Filz52ccb8e2005-12-22 11:36:46 -08003773 /* Get association, if assoc_id != 0 and the socket is a one
3774 * to many style socket, and an association was not found, then
3775 * the id was invalid.
3776 */
3777 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3778 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3779 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Frank Filz52ccb8e2005-12-22 11:36:46 -08003783 if (trans) {
3784 /* Fetch transport values. */
3785 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3786 params.spp_pathmtu = trans->pathmtu;
3787 params.spp_pathmaxrxt = trans->pathmaxrxt;
3788 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Frank Filz52ccb8e2005-12-22 11:36:46 -08003790 /*draft-11 doesn't say what to return in spp_flags*/
3791 params.spp_flags = trans->param_flags;
3792 } else if (asoc) {
3793 /* Fetch association values. */
3794 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3795 params.spp_pathmtu = asoc->pathmtu;
3796 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3797 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Frank Filz52ccb8e2005-12-22 11:36:46 -08003799 /*draft-11 doesn't say what to return in spp_flags*/
3800 params.spp_flags = asoc->param_flags;
3801 } else {
3802 /* Fetch socket values. */
3803 params.spp_hbinterval = sp->hbinterval;
3804 params.spp_pathmtu = sp->pathmtu;
3805 params.spp_sackdelay = sp->sackdelay;
3806 params.spp_pathmaxrxt = sp->pathmaxrxt;
3807
3808 /*draft-11 doesn't say what to return in spp_flags*/
3809 params.spp_flags = sp->param_flags;
3810 }
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 if (copy_to_user(optval, &params, len))
3813 return -EFAULT;
3814
3815 if (put_user(len, optlen))
3816 return -EFAULT;
3817
3818 return 0;
3819}
3820
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003821/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08003822 *
3823 * This options will get or set the delayed ack timer. The time is set
3824 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3825 * endpoints default delayed ack timer value. If the assoc_id field is
3826 * non-zero, then the set or get effects the specified association.
3827 *
3828 * struct sctp_assoc_value {
3829 * sctp_assoc_t assoc_id;
3830 * uint32_t assoc_value;
3831 * };
3832 *
3833 * assoc_id - This parameter, indicates which association the
3834 * user is preforming an action upon. Note that if
3835 * this field's value is zero then the endpoints
3836 * default value is changed (effecting future
3837 * associations only).
3838 *
3839 * assoc_value - This parameter contains the number of milliseconds
3840 * that the user is requesting the delayed ACK timer
3841 * be set to. Note that this value is defined in
3842 * the standard to be between 200 and 500 milliseconds.
3843 *
3844 * Note: a value of zero will leave the value alone,
3845 * but disable SACK delay. A non-zero value will also
3846 * enable SACK delay.
3847 */
3848static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3849 char __user *optval,
3850 int __user *optlen)
3851{
3852 struct sctp_assoc_value params;
3853 struct sctp_association *asoc = NULL;
3854 struct sctp_sock *sp = sctp_sk(sk);
3855
Neil Horman408f22e2007-06-16 14:03:45 -04003856 if (len < sizeof(struct sctp_assoc_value))
Frank Filz77086102005-12-22 11:37:30 -08003857 return - EINVAL;
3858
Neil Horman408f22e2007-06-16 14:03:45 -04003859 len = sizeof(struct sctp_assoc_value);
3860
Frank Filz77086102005-12-22 11:37:30 -08003861 if (copy_from_user(&params, optval, len))
3862 return -EFAULT;
3863
3864 /* Get association, if assoc_id != 0 and the socket is a one
3865 * to many style socket, and an association was not found, then
3866 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003867 */
Frank Filz77086102005-12-22 11:37:30 -08003868 asoc = sctp_id2assoc(sk, params.assoc_id);
3869 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3870 return -EINVAL;
3871
3872 if (asoc) {
3873 /* Fetch association values. */
3874 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3875 params.assoc_value = jiffies_to_msecs(
3876 asoc->sackdelay);
3877 else
3878 params.assoc_value = 0;
3879 } else {
3880 /* Fetch socket values. */
3881 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3882 params.assoc_value = sp->sackdelay;
3883 else
3884 params.assoc_value = 0;
3885 }
3886
3887 if (copy_to_user(optval, &params, len))
3888 return -EFAULT;
3889
3890 if (put_user(len, optlen))
3891 return -EFAULT;
3892
3893 return 0;
3894}
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3897 *
3898 * Applications can specify protocol parameters for the default association
3899 * initialization. The option name argument to setsockopt() and getsockopt()
3900 * is SCTP_INITMSG.
3901 *
3902 * Setting initialization parameters is effective only on an unconnected
3903 * socket (for UDP-style sockets only future associations are effected
3904 * by the change). With TCP-style sockets, this option is inherited by
3905 * sockets derived from a listener socket.
3906 */
3907static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3908{
Neil Horman408f22e2007-06-16 14:03:45 -04003909 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003911 len = sizeof(struct sctp_initmsg);
3912 if (put_user(len, optlen))
3913 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3915 return -EFAULT;
3916 return 0;
3917}
3918
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003919static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3920 char __user *optval,
3921 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
3923 sctp_assoc_t id;
3924 struct sctp_association *asoc;
3925 struct list_head *pos;
3926 int cnt = 0;
3927
Neil Horman408f22e2007-06-16 14:03:45 -04003928 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return -EINVAL;
3930
3931 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3932 return -EFAULT;
3933
3934 /* For UDP-style sockets, id specifies the association to query. */
3935 asoc = sctp_id2assoc(sk, id);
3936 if (!asoc)
3937 return -EINVAL;
3938
3939 list_for_each(pos, &asoc->peer.transport_addr_list) {
3940 cnt ++;
3941 }
3942
3943 return cnt;
3944}
3945
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003946/*
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003947 * Old API for getting list of peer addresses. Does not work for 32-bit
3948 * programs running on a 64-bit kernel
3949 */
3950static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3951 char __user *optval,
3952 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
3954 struct sctp_association *asoc;
3955 struct list_head *pos;
3956 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003957 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 struct sctp_transport *from;
3959 void __user *to;
3960 union sctp_addr temp;
3961 struct sctp_sock *sp = sctp_sk(sk);
3962 int addrlen;
3963
Neil Horman408f22e2007-06-16 14:03:45 -04003964 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return -EINVAL;
3966
Neil Horman408f22e2007-06-16 14:03:45 -04003967 len = sizeof(struct sctp_getaddrs_old);
3968
3969 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return -EFAULT;
3971
3972 if (getaddrs.addr_num <= 0) return -EINVAL;
3973
3974 /* For UDP-style sockets, id specifies the association to query. */
3975 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3976 if (!asoc)
3977 return -EINVAL;
3978
3979 to = (void __user *)getaddrs.addrs;
3980 list_for_each(pos, &asoc->peer.transport_addr_list) {
3981 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08003982 memcpy(&temp, &from->ipaddr, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3984 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 if (copy_to_user(to, &temp, addrlen))
3986 return -EFAULT;
3987 to += addrlen ;
3988 cnt ++;
3989 if (cnt >= getaddrs.addr_num) break;
3990 }
3991 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04003992 if (put_user(len, optlen))
3993 return -EFAULT;
3994 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 return -EFAULT;
3996
3997 return 0;
3998}
3999
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004000static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4001 char __user *optval, int __user *optlen)
4002{
4003 struct sctp_association *asoc;
4004 struct list_head *pos;
4005 int cnt = 0;
4006 struct sctp_getaddrs getaddrs;
4007 struct sctp_transport *from;
4008 void __user *to;
4009 union sctp_addr temp;
4010 struct sctp_sock *sp = sctp_sk(sk);
4011 int addrlen;
4012 size_t space_left;
4013 int bytes_copied;
4014
4015 if (len < sizeof(struct sctp_getaddrs))
4016 return -EINVAL;
4017
4018 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4019 return -EFAULT;
4020
4021 /* For UDP-style sockets, id specifies the association to query. */
4022 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4023 if (!asoc)
4024 return -EINVAL;
4025
4026 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004027 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004028
4029 list_for_each(pos, &asoc->peer.transport_addr_list) {
4030 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004031 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004032 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4033 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004034 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004035 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004036 if (copy_to_user(to, &temp, addrlen))
4037 return -EFAULT;
4038 to += addrlen;
4039 cnt++;
4040 space_left -= addrlen;
4041 }
4042
4043 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4044 return -EFAULT;
4045 bytes_copied = ((char __user *)to) - optval;
4046 if (put_user(bytes_copied, optlen))
4047 return -EFAULT;
4048
4049 return 0;
4050}
4051
4052static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4053 char __user *optval,
4054 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 sctp_assoc_t id;
4057 struct sctp_bind_addr *bp;
4058 struct sctp_association *asoc;
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004059 struct list_head *pos, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 struct sctp_sockaddr_entry *addr;
4061 rwlock_t *addr_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 int cnt = 0;
4063
Neil Horman408f22e2007-06-16 14:03:45 -04004064 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return -EINVAL;
4066
4067 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4068 return -EFAULT;
4069
4070 /*
4071 * For UDP-style sockets, id specifies the association to query.
4072 * If the id field is set to the value '0' then the locally bound
4073 * addresses are returned without regard to any particular
4074 * association.
4075 */
4076 if (0 == id) {
4077 bp = &sctp_sk(sk)->ep->base.bind_addr;
4078 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4079 } else {
4080 asoc = sctp_id2assoc(sk, id);
4081 if (!asoc)
4082 return -EINVAL;
4083 bp = &asoc->base.bind_addr;
4084 addr_lock = &asoc->base.addr_lock;
4085 }
4086
4087 sctp_read_lock(addr_lock);
4088
4089 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4090 * addresses from the global local address list.
4091 */
4092 if (sctp_list_single_entry(&bp->address_list)) {
4093 addr = list_entry(bp->address_list.next,
4094 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004095 if (sctp_is_any(&addr->a)) {
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004096 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 addr = list_entry(pos,
4098 struct sctp_sockaddr_entry,
4099 list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004100 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004101 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 continue;
4103 cnt++;
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 } else {
4106 cnt = 1;
4107 }
4108 goto done;
4109 }
4110
4111 list_for_each(pos, &bp->address_list) {
4112 cnt ++;
4113 }
4114
4115done:
4116 sctp_read_unlock(addr_lock);
4117 return cnt;
4118}
4119
4120/* Helper function that copies local addresses to user and returns the number
4121 * of addresses copied.
4122 */
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004123static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4124 int max_addrs, void *to,
4125 int *bytes_copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004127 struct list_head *pos, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 union sctp_addr temp;
4130 int cnt = 0;
4131 int addrlen;
4132
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004133 list_for_each_safe(pos, next, &sctp_local_addr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004135 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004136 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 continue;
Al Viro6244be42006-11-20 17:21:44 -08004138 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4140 &temp);
4141 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004142 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004145 *bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 cnt ++;
4147 if (cnt >= max_addrs) break;
4148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150 return cnt;
4151}
4152
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004153static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4154 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004155{
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004156 struct list_head *pos, *next;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004157 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004158 union sctp_addr temp;
4159 int cnt = 0;
4160 int addrlen;
4161
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004162 list_for_each_safe(pos, next, &sctp_local_addr_list) {
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004163 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004164 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004165 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004166 continue;
Al Viro6244be42006-11-20 17:21:44 -08004167 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004168 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4169 &temp);
4170 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004171 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004172 return -ENOMEM;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004173 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004174
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004175 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004176 cnt ++;
4177 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004178 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004179 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004180
4181 return cnt;
4182}
4183
4184/* Old API for getting list of local addresses. Does not work for 32-bit
4185 * programs running on a 64-bit kernel
4186 */
4187static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4188 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
4190 struct sctp_bind_addr *bp;
4191 struct sctp_association *asoc;
4192 struct list_head *pos;
4193 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004194 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct sctp_sockaddr_entry *addr;
4196 void __user *to;
4197 union sctp_addr temp;
4198 struct sctp_sock *sp = sctp_sk(sk);
4199 int addrlen;
4200 rwlock_t *addr_lock;
4201 int err = 0;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004202 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004203 void *buf;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004204 int bytes_copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Neil Horman408f22e2007-06-16 14:03:45 -04004206 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 return -EINVAL;
4208
Neil Horman408f22e2007-06-16 14:03:45 -04004209 len = sizeof(struct sctp_getaddrs_old);
4210 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return -EFAULT;
4212
4213 if (getaddrs.addr_num <= 0) return -EINVAL;
4214 /*
4215 * For UDP-style sockets, id specifies the association to query.
4216 * If the id field is set to the value '0' then the locally bound
4217 * addresses are returned without regard to any particular
4218 * association.
4219 */
4220 if (0 == getaddrs.assoc_id) {
4221 bp = &sctp_sk(sk)->ep->base.bind_addr;
4222 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4223 } else {
4224 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4225 if (!asoc)
4226 return -EINVAL;
4227 bp = &asoc->base.bind_addr;
4228 addr_lock = &asoc->base.addr_lock;
4229 }
4230
4231 to = getaddrs.addrs;
4232
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004233 /* Allocate space for a local instance of packed array to hold all
4234 * the data. We store addresses here first and then put write them
4235 * to the user in one shot.
4236 */
4237 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4238 GFP_KERNEL);
4239 if (!addrs)
4240 return -ENOMEM;
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 sctp_read_lock(addr_lock);
4243
4244 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4245 * addresses from the global local address list.
4246 */
4247 if (sctp_list_single_entry(&bp->address_list)) {
4248 addr = list_entry(bp->address_list.next,
4249 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004250 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004251 cnt = sctp_copy_laddrs_old(sk, bp->port,
4252 getaddrs.addr_num,
4253 addrs, &bytes_copied);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004254 goto copy_getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
4256 }
4257
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004258 buf = addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 list_for_each(pos, &bp->address_list) {
4260 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004261 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4263 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004264 memcpy(buf, &temp, addrlen);
4265 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004266 bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 cnt ++;
4268 if (cnt >= getaddrs.addr_num) break;
4269 }
4270
4271copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004272 sctp_read_unlock(addr_lock);
4273
4274 /* copy the entire address list into the user provided space */
4275 if (copy_to_user(to, addrs, bytes_copied)) {
4276 err = -EFAULT;
4277 goto error;
4278 }
4279
4280 /* copy the leading structure back to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004282 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 err = -EFAULT;
4284
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004285error:
4286 kfree(addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return err;
4288}
4289
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004290static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4291 char __user *optval, int __user *optlen)
4292{
4293 struct sctp_bind_addr *bp;
4294 struct sctp_association *asoc;
4295 struct list_head *pos;
4296 int cnt = 0;
4297 struct sctp_getaddrs getaddrs;
4298 struct sctp_sockaddr_entry *addr;
4299 void __user *to;
4300 union sctp_addr temp;
4301 struct sctp_sock *sp = sctp_sk(sk);
4302 int addrlen;
4303 rwlock_t *addr_lock;
4304 int err = 0;
4305 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004306 int bytes_copied = 0;
4307 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004308 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004309
Neil Horman408f22e2007-06-16 14:03:45 -04004310 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004311 return -EINVAL;
4312
4313 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4314 return -EFAULT;
4315
4316 /*
4317 * For UDP-style sockets, id specifies the association to query.
4318 * If the id field is set to the value '0' then the locally bound
4319 * addresses are returned without regard to any particular
4320 * association.
4321 */
4322 if (0 == getaddrs.assoc_id) {
4323 bp = &sctp_sk(sk)->ep->base.bind_addr;
4324 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4325 } else {
4326 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4327 if (!asoc)
4328 return -EINVAL;
4329 bp = &asoc->base.bind_addr;
4330 addr_lock = &asoc->base.addr_lock;
4331 }
4332
4333 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004334 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4335
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004336 addrs = kmalloc(space_left, GFP_KERNEL);
4337 if (!addrs)
4338 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004339
4340 sctp_read_lock(addr_lock);
4341
4342 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4343 * addresses from the global local address list.
4344 */
4345 if (sctp_list_single_entry(&bp->address_list)) {
4346 addr = list_entry(bp->address_list.next,
4347 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004348 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004349 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4350 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004351 if (cnt < 0) {
4352 err = cnt;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004353 goto error;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004354 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004355 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004356 }
4357 }
4358
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004359 buf = addrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004360 list_for_each(pos, &bp->address_list) {
4361 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004362 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004363 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4364 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004365 if (space_left < addrlen) {
4366 err = -ENOMEM; /*fixme: right error?*/
4367 goto error;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004368 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004369 memcpy(buf, &temp, addrlen);
4370 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004371 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004372 cnt ++;
4373 space_left -= addrlen;
4374 }
4375
4376copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004377 sctp_read_unlock(addr_lock);
4378
4379 if (copy_to_user(to, addrs, bytes_copied)) {
4380 err = -EFAULT;
4381 goto error;
4382 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004383 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4384 err = -EFAULT;
4385 goto error;
4386 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004387 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004388 err = -EFAULT;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004389error:
4390 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004391 return err;
4392}
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4395 *
4396 * Requests that the local SCTP stack use the enclosed peer address as
4397 * the association primary. The enclosed address must be one of the
4398 * association peer's addresses.
4399 */
4400static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4401 char __user *optval, int __user *optlen)
4402{
4403 struct sctp_prim prim;
4404 struct sctp_association *asoc;
4405 struct sctp_sock *sp = sctp_sk(sk);
4406
Neil Horman408f22e2007-06-16 14:03:45 -04004407 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 return -EINVAL;
4409
Neil Horman408f22e2007-06-16 14:03:45 -04004410 len = sizeof(struct sctp_prim);
4411
4412 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return -EFAULT;
4414
4415 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4416 if (!asoc)
4417 return -EINVAL;
4418
4419 if (!asoc->peer.primary_path)
4420 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004421
Al Viro8cec6b82006-11-20 17:23:01 -08004422 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4423 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4426 (union sctp_addr *)&prim.ssp_addr);
4427
Neil Horman408f22e2007-06-16 14:03:45 -04004428 if (put_user(len, optlen))
4429 return -EFAULT;
4430 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return -EFAULT;
4432
4433 return 0;
4434}
4435
4436/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004437 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004439 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 * Indication parameter for all future INIT and INIT-ACK exchanges.
4441 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004442static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 char __user *optval, int __user *optlen)
4444{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004445 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Neil Horman408f22e2007-06-16 14:03:45 -04004447 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return -EINVAL;
4449
Neil Horman408f22e2007-06-16 14:03:45 -04004450 len = sizeof(struct sctp_setadaptation);
4451
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004452 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004453
4454 if (put_user(len, optlen))
4455 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004456 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return 0;
4460}
4461
4462/*
4463 *
4464 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4465 *
4466 * Applications that wish to use the sendto() system call may wish to
4467 * specify a default set of parameters that would normally be supplied
4468 * through the inclusion of ancillary data. This socket option allows
4469 * such an application to set the default sctp_sndrcvinfo structure.
4470
4471
4472 * The application that wishes to use this socket option simply passes
4473 * in to this call the sctp_sndrcvinfo structure defined in Section
4474 * 5.2.2) The input parameters accepted by this call include
4475 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4476 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4477 * to this call if the caller is using the UDP model.
4478 *
4479 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4480 */
4481static int sctp_getsockopt_default_send_param(struct sock *sk,
4482 int len, char __user *optval,
4483 int __user *optlen)
4484{
4485 struct sctp_sndrcvinfo info;
4486 struct sctp_association *asoc;
4487 struct sctp_sock *sp = sctp_sk(sk);
4488
Neil Horman408f22e2007-06-16 14:03:45 -04004489 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004491
4492 len = sizeof(struct sctp_sndrcvinfo);
4493
4494 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return -EFAULT;
4496
4497 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4498 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4499 return -EINVAL;
4500
4501 if (asoc) {
4502 info.sinfo_stream = asoc->default_stream;
4503 info.sinfo_flags = asoc->default_flags;
4504 info.sinfo_ppid = asoc->default_ppid;
4505 info.sinfo_context = asoc->default_context;
4506 info.sinfo_timetolive = asoc->default_timetolive;
4507 } else {
4508 info.sinfo_stream = sp->default_stream;
4509 info.sinfo_flags = sp->default_flags;
4510 info.sinfo_ppid = sp->default_ppid;
4511 info.sinfo_context = sp->default_context;
4512 info.sinfo_timetolive = sp->default_timetolive;
4513 }
4514
Neil Horman408f22e2007-06-16 14:03:45 -04004515 if (put_user(len, optlen))
4516 return -EFAULT;
4517 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 return -EFAULT;
4519
4520 return 0;
4521}
4522
4523/*
4524 *
4525 * 7.1.5 SCTP_NODELAY
4526 *
4527 * Turn on/off any Nagle-like algorithm. This means that packets are
4528 * generally sent as soon as possible and no unnecessary delays are
4529 * introduced, at the cost of more packets in the network. Expects an
4530 * integer boolean flag.
4531 */
4532
4533static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4534 char __user *optval, int __user *optlen)
4535{
4536 int val;
4537
4538 if (len < sizeof(int))
4539 return -EINVAL;
4540
4541 len = sizeof(int);
4542 val = (sctp_sk(sk)->nodelay == 1);
4543 if (put_user(len, optlen))
4544 return -EFAULT;
4545 if (copy_to_user(optval, &val, len))
4546 return -EFAULT;
4547 return 0;
4548}
4549
4550/*
4551 *
4552 * 7.1.1 SCTP_RTOINFO
4553 *
4554 * The protocol parameters used to initialize and bound retransmission
4555 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4556 * and modify these parameters.
4557 * All parameters are time values, in milliseconds. A value of 0, when
4558 * modifying the parameters, indicates that the current value should not
4559 * be changed.
4560 *
4561 */
4562static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4563 char __user *optval,
4564 int __user *optlen) {
4565 struct sctp_rtoinfo rtoinfo;
4566 struct sctp_association *asoc;
4567
Neil Horman408f22e2007-06-16 14:03:45 -04004568 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return -EINVAL;
4570
Neil Horman408f22e2007-06-16 14:03:45 -04004571 len = sizeof(struct sctp_rtoinfo);
4572
4573 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return -EFAULT;
4575
4576 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4577
4578 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4579 return -EINVAL;
4580
4581 /* Values corresponding to the specific association. */
4582 if (asoc) {
4583 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4584 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4585 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4586 } else {
4587 /* Values corresponding to the endpoint. */
4588 struct sctp_sock *sp = sctp_sk(sk);
4589
4590 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4591 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4592 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4593 }
4594
4595 if (put_user(len, optlen))
4596 return -EFAULT;
4597
4598 if (copy_to_user(optval, &rtoinfo, len))
4599 return -EFAULT;
4600
4601 return 0;
4602}
4603
4604/*
4605 *
4606 * 7.1.2 SCTP_ASSOCINFO
4607 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004608 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 * of the association.
4610 * Returns an error if the new association retransmission value is
4611 * greater than the sum of the retransmission value of the peer.
4612 * See [SCTP] for more information.
4613 *
4614 */
4615static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4616 char __user *optval,
4617 int __user *optlen)
4618{
4619
4620 struct sctp_assocparams assocparams;
4621 struct sctp_association *asoc;
4622 struct list_head *pos;
4623 int cnt = 0;
4624
Neil Horman408f22e2007-06-16 14:03:45 -04004625 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return -EINVAL;
4627
Neil Horman408f22e2007-06-16 14:03:45 -04004628 len = sizeof(struct sctp_assocparams);
4629
4630 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return -EFAULT;
4632
4633 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4634
4635 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4636 return -EINVAL;
4637
4638 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004639 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4641 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4642 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4643 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4644 * 1000) +
4645 (asoc->cookie_life.tv_usec
4646 / 1000);
4647
4648 list_for_each(pos, &asoc->peer.transport_addr_list) {
4649 cnt ++;
4650 }
4651
4652 assocparams.sasoc_number_peer_destinations = cnt;
4653 } else {
4654 /* Values corresponding to the endpoint */
4655 struct sctp_sock *sp = sctp_sk(sk);
4656
4657 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4658 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4659 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4660 assocparams.sasoc_cookie_life =
4661 sp->assocparams.sasoc_cookie_life;
4662 assocparams.sasoc_number_peer_destinations =
4663 sp->assocparams.
4664 sasoc_number_peer_destinations;
4665 }
4666
4667 if (put_user(len, optlen))
4668 return -EFAULT;
4669
4670 if (copy_to_user(optval, &assocparams, len))
4671 return -EFAULT;
4672
4673 return 0;
4674}
4675
4676/*
4677 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4678 *
4679 * This socket option is a boolean flag which turns on or off mapped V4
4680 * addresses. If this option is turned on and the socket is type
4681 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4682 * If this option is turned off, then no mapping will be done of V4
4683 * addresses and a user will receive both PF_INET6 and PF_INET type
4684 * addresses on the socket.
4685 */
4686static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4687 char __user *optval, int __user *optlen)
4688{
4689 int val;
4690 struct sctp_sock *sp = sctp_sk(sk);
4691
4692 if (len < sizeof(int))
4693 return -EINVAL;
4694
4695 len = sizeof(int);
4696 val = sp->v4mapped;
4697 if (put_user(len, optlen))
4698 return -EFAULT;
4699 if (copy_to_user(optval, &val, len))
4700 return -EFAULT;
4701
4702 return 0;
4703}
4704
4705/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004706 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4707 * (chapter and verse is quoted at sctp_setsockopt_context())
4708 */
4709static int sctp_getsockopt_context(struct sock *sk, int len,
4710 char __user *optval, int __user *optlen)
4711{
4712 struct sctp_assoc_value params;
4713 struct sctp_sock *sp;
4714 struct sctp_association *asoc;
4715
Neil Horman408f22e2007-06-16 14:03:45 -04004716 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004717 return -EINVAL;
4718
Neil Horman408f22e2007-06-16 14:03:45 -04004719 len = sizeof(struct sctp_assoc_value);
4720
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004721 if (copy_from_user(&params, optval, len))
4722 return -EFAULT;
4723
4724 sp = sctp_sk(sk);
4725
4726 if (params.assoc_id != 0) {
4727 asoc = sctp_id2assoc(sk, params.assoc_id);
4728 if (!asoc)
4729 return -EINVAL;
4730 params.assoc_value = asoc->default_rcv_context;
4731 } else {
4732 params.assoc_value = sp->default_rcv_context;
4733 }
4734
4735 if (put_user(len, optlen))
4736 return -EFAULT;
4737 if (copy_to_user(optval, &params, len))
4738 return -EFAULT;
4739
4740 return 0;
4741}
4742
4743/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4745 *
4746 * This socket option specifies the maximum size to put in any outgoing
4747 * SCTP chunk. If a message is larger than this size it will be
4748 * fragmented by SCTP into the specified size. Note that the underlying
4749 * SCTP implementation may fragment into smaller sized chunks when the
4750 * PMTU of the underlying association is smaller than the value set by
4751 * the user.
4752 */
4753static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4754 char __user *optval, int __user *optlen)
4755{
4756 int val;
4757
4758 if (len < sizeof(int))
4759 return -EINVAL;
4760
4761 len = sizeof(int);
4762
4763 val = sctp_sk(sk)->user_frag;
4764 if (put_user(len, optlen))
4765 return -EFAULT;
4766 if (copy_to_user(optval, &val, len))
4767 return -EFAULT;
4768
4769 return 0;
4770}
4771
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004772/*
4773 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4774 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4775 */
4776static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4777 char __user *optval, int __user *optlen)
4778{
4779 int val;
4780
4781 if (len < sizeof(int))
4782 return -EINVAL;
4783
4784 len = sizeof(int);
4785
4786 val = sctp_sk(sk)->frag_interleave;
4787 if (put_user(len, optlen))
4788 return -EFAULT;
4789 if (copy_to_user(optval, &val, len))
4790 return -EFAULT;
4791
4792 return 0;
4793}
4794
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004795/*
4796 * 7.1.25. Set or Get the sctp partial delivery point
4797 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4798 */
4799static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4800 char __user *optval,
4801 int __user *optlen)
4802{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004803 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004804
4805 if (len < sizeof(u32))
4806 return -EINVAL;
4807
4808 len = sizeof(u32);
4809
4810 val = sctp_sk(sk)->pd_point;
4811 if (put_user(len, optlen))
4812 return -EFAULT;
4813 if (copy_to_user(optval, &val, len))
4814 return -EFAULT;
4815
4816 return -ENOTSUPP;
4817}
4818
Vlad Yasevich70331572007-03-23 11:34:36 -07004819/*
4820 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4821 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4822 */
4823static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4824 char __user *optval,
4825 int __user *optlen)
4826{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004827 int val;
Vlad Yasevich70331572007-03-23 11:34:36 -07004828
4829 if (len < sizeof(int))
4830 return -EINVAL;
4831
4832 len = sizeof(int);
4833
4834 val = sctp_sk(sk)->max_burst;
4835 if (put_user(len, optlen))
4836 return -EFAULT;
4837 if (copy_to_user(optval, &val, len))
4838 return -EFAULT;
4839
4840 return -ENOTSUPP;
4841}
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4844 char __user *optval, int __user *optlen)
4845{
4846 int retval = 0;
4847 int len;
4848
Frank Filz3f7a87d2005-06-20 13:14:57 -07004849 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4850 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
4852 /* I can hardly begin to describe how wrong this is. This is
4853 * so broken as to be worse than useless. The API draft
4854 * REALLY is NOT helpful here... I am not convinced that the
4855 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4856 * are at all well-founded.
4857 */
4858 if (level != SOL_SCTP) {
4859 struct sctp_af *af = sctp_sk(sk)->pf->af;
4860
4861 retval = af->getsockopt(sk, level, optname, optval, optlen);
4862 return retval;
4863 }
4864
4865 if (get_user(len, optlen))
4866 return -EFAULT;
4867
4868 sctp_lock_sock(sk);
4869
4870 switch (optname) {
4871 case SCTP_STATUS:
4872 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4873 break;
4874 case SCTP_DISABLE_FRAGMENTS:
4875 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4876 optlen);
4877 break;
4878 case SCTP_EVENTS:
4879 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4880 break;
4881 case SCTP_AUTOCLOSE:
4882 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4883 break;
4884 case SCTP_SOCKOPT_PEELOFF:
4885 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4886 break;
4887 case SCTP_PEER_ADDR_PARAMS:
4888 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4889 optlen);
4890 break;
Frank Filz77086102005-12-22 11:37:30 -08004891 case SCTP_DELAYED_ACK_TIME:
4892 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4893 optlen);
4894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 case SCTP_INITMSG:
4896 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4897 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004898 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4899 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4900 optlen);
4901 break;
4902 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4903 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4904 optlen);
4905 break;
4906 case SCTP_GET_PEER_ADDRS_OLD:
4907 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 optlen);
4909 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004910 case SCTP_GET_LOCAL_ADDRS_OLD:
4911 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 optlen);
4913 break;
4914 case SCTP_GET_PEER_ADDRS:
4915 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4916 optlen);
4917 break;
4918 case SCTP_GET_LOCAL_ADDRS:
4919 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4920 optlen);
4921 break;
4922 case SCTP_DEFAULT_SEND_PARAM:
4923 retval = sctp_getsockopt_default_send_param(sk, len,
4924 optval, optlen);
4925 break;
4926 case SCTP_PRIMARY_ADDR:
4927 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4928 break;
4929 case SCTP_NODELAY:
4930 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4931 break;
4932 case SCTP_RTOINFO:
4933 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4934 break;
4935 case SCTP_ASSOCINFO:
4936 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4937 break;
4938 case SCTP_I_WANT_MAPPED_V4_ADDR:
4939 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4940 break;
4941 case SCTP_MAXSEG:
4942 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4943 break;
4944 case SCTP_GET_PEER_ADDR_INFO:
4945 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4946 optlen);
4947 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004948 case SCTP_ADAPTATION_LAYER:
4949 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 optlen);
4951 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004952 case SCTP_CONTEXT:
4953 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4954 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004955 case SCTP_FRAGMENT_INTERLEAVE:
4956 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4957 optlen);
4958 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004959 case SCTP_PARTIAL_DELIVERY_POINT:
4960 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4961 optlen);
4962 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004963 case SCTP_MAX_BURST:
4964 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 default:
4967 retval = -ENOPROTOOPT;
4968 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 sctp_release_sock(sk);
4972 return retval;
4973}
4974
4975static void sctp_hash(struct sock *sk)
4976{
4977 /* STUB */
4978}
4979
4980static void sctp_unhash(struct sock *sk)
4981{
4982 /* STUB */
4983}
4984
4985/* Check if port is acceptable. Possibly find first available port.
4986 *
4987 * The port hash table (contained in the 'global' SCTP protocol storage
4988 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4989 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4990 * list (the list number is the port number hashed out, so as you
4991 * would expect from a hash function, all the ports in a given list have
4992 * such a number that hashes out to the same list number; you were
4993 * expecting that, right?); so each list has a set of ports, with a
4994 * link to the socket (struct sock) that uses it, the port number and
4995 * a fastreuse flag (FIXME: NPI ipg).
4996 */
4997static struct sctp_bind_bucket *sctp_bucket_create(
4998 struct sctp_bind_hashbucket *head, unsigned short snum);
4999
5000static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5001{
5002 struct sctp_bind_hashbucket *head; /* hash list */
5003 struct sctp_bind_bucket *pp; /* hash list port iterator */
5004 unsigned short snum;
5005 int ret;
5006
Al Viro04afd8b2006-11-20 17:02:01 -08005007 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5010 sctp_local_bh_disable();
5011
5012 if (snum == 0) {
5013 /* Search for an available port.
5014 *
5015 * 'sctp_port_rover' was the last port assigned, so
5016 * we start to search from 'sctp_port_rover +
5017 * 1'. What we do is first check if port 'rover' is
5018 * already in the hash table; if not, we use that; if
5019 * it is, we try next.
5020 */
5021 int low = sysctl_local_port_range[0];
5022 int high = sysctl_local_port_range[1];
5023 int remaining = (high - low) + 1;
5024 int rover;
5025 int index;
5026
5027 sctp_spin_lock(&sctp_port_alloc_lock);
5028 rover = sctp_port_rover;
5029 do {
5030 rover++;
5031 if ((rover < low) || (rover > high))
5032 rover = low;
5033 index = sctp_phashfn(rover);
5034 head = &sctp_port_hashtable[index];
5035 sctp_spin_lock(&head->lock);
5036 for (pp = head->chain; pp; pp = pp->next)
5037 if (pp->port == rover)
5038 goto next;
5039 break;
5040 next:
5041 sctp_spin_unlock(&head->lock);
5042 } while (--remaining > 0);
5043 sctp_port_rover = rover;
5044 sctp_spin_unlock(&sctp_port_alloc_lock);
5045
5046 /* Exhausted local port range during search? */
5047 ret = 1;
5048 if (remaining <= 0)
5049 goto fail;
5050
5051 /* OK, here is the one we will use. HEAD (the port
5052 * hash table list entry) is non-NULL and we hold it's
5053 * mutex.
5054 */
5055 snum = rover;
5056 } else {
5057 /* We are given an specific port number; we verify
5058 * that it is not being used. If it is used, we will
5059 * exahust the search in the hash list corresponding
5060 * to the port number (snum) - we detect that with the
5061 * port iterator, pp being NULL.
5062 */
5063 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5064 sctp_spin_lock(&head->lock);
5065 for (pp = head->chain; pp; pp = pp->next) {
5066 if (pp->port == snum)
5067 goto pp_found;
5068 }
5069 }
5070 pp = NULL;
5071 goto pp_not_found;
5072pp_found:
5073 if (!hlist_empty(&pp->owner)) {
5074 /* We had a port hash table hit - there is an
5075 * available port (pp != NULL) and it is being
5076 * used by other socket (pp->owner not empty); that other
5077 * socket is going to be sk2.
5078 */
5079 int reuse = sk->sk_reuse;
5080 struct sock *sk2;
5081 struct hlist_node *node;
5082
5083 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005084 if (pp->fastreuse && sk->sk_reuse &&
5085 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 goto success;
5087
5088 /* Run through the list of sockets bound to the port
5089 * (pp->port) [via the pointers bind_next and
5090 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5091 * we get the endpoint they describe and run through
5092 * the endpoint's list of IP (v4 or v6) addresses,
5093 * comparing each of the addresses with the address of
5094 * the socket sk. If we find a match, then that means
5095 * that this port/socket (sk) combination are already
5096 * in an endpoint.
5097 */
5098 sk_for_each_bound(sk2, node, &pp->owner) {
5099 struct sctp_endpoint *ep2;
5100 ep2 = sctp_sk(sk2)->ep;
5101
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005102 if (reuse && sk2->sk_reuse &&
5103 sk2->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 continue;
5105
Al Viro7e1e4a2b2006-11-20 17:05:43 -08005106 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 sctp_sk(sk))) {
5108 ret = (long)sk2;
5109 goto fail_unlock;
5110 }
5111 }
5112 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5113 }
5114pp_not_found:
5115 /* If there was a hash table miss, create a new port. */
5116 ret = 1;
5117 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5118 goto fail_unlock;
5119
5120 /* In either case (hit or miss), make sure fastreuse is 1 only
5121 * if sk->sk_reuse is too (that is, if the caller requested
5122 * SO_REUSEADDR on this socket -sk-).
5123 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005124 if (hlist_empty(&pp->owner)) {
5125 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5126 pp->fastreuse = 1;
5127 else
5128 pp->fastreuse = 0;
5129 } else if (pp->fastreuse &&
5130 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 pp->fastreuse = 0;
5132
5133 /* We are set, so fill up all the data in the hash table
5134 * entry, tie the socket list information with the rest of the
5135 * sockets FIXME: Blurry, NPI (ipg).
5136 */
5137success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 if (!sctp_sk(sk)->bind_hash) {
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005139 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 sk_add_bind_node(sk, &pp->owner);
5141 sctp_sk(sk)->bind_hash = pp;
5142 }
5143 ret = 0;
5144
5145fail_unlock:
5146 sctp_spin_unlock(&head->lock);
5147
5148fail:
5149 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 return ret;
5151}
5152
5153/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5154 * port is requested.
5155 */
5156static int sctp_get_port(struct sock *sk, unsigned short snum)
5157{
5158 long ret;
5159 union sctp_addr addr;
5160 struct sctp_af *af = sctp_sk(sk)->pf->af;
5161
5162 /* Set up a dummy address struct from the sk. */
5163 af->from_sk(&addr, sk);
5164 addr.v4.sin_port = htons(snum);
5165
5166 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5167 ret = sctp_get_port_local(sk, &addr);
5168
5169 return (ret ? 1 : 0);
5170}
5171
5172/*
5173 * 3.1.3 listen() - UDP Style Syntax
5174 *
5175 * By default, new associations are not accepted for UDP style sockets.
5176 * An application uses listen() to mark a socket as being able to
5177 * accept new associations.
5178 */
5179SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5180{
5181 struct sctp_sock *sp = sctp_sk(sk);
5182 struct sctp_endpoint *ep = sp->ep;
5183
5184 /* Only UDP style sockets that are not peeled off are allowed to
5185 * listen().
5186 */
5187 if (!sctp_style(sk, UDP))
5188 return -EINVAL;
5189
5190 /* If backlog is zero, disable listening. */
5191 if (!backlog) {
5192 if (sctp_sstate(sk, CLOSED))
5193 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 sctp_unhash_endpoint(ep);
5196 sk->sk_state = SCTP_SS_CLOSED;
5197 }
5198
5199 /* Return if we are already listening. */
5200 if (sctp_sstate(sk, LISTENING))
5201 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 /*
5204 * If a bind() or sctp_bindx() is not called prior to a listen()
5205 * call that allows new associations to be accepted, the system
5206 * picks an ephemeral port and will choose an address set equivalent
5207 * to binding with a wildcard address.
5208 *
5209 * This is not currently spelled out in the SCTP sockets
5210 * extensions draft, but follows the practice as seen in TCP
5211 * sockets.
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005212 *
5213 * Additionally, turn off fastreuse flag since we are not listening
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005215 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 if (!ep->base.bind_addr.port) {
5217 if (sctp_autobind(sk))
5218 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005219 } else
5220 sctp_sk(sk)->bind_hash->fastreuse = 0;
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 sctp_hash_endpoint(ep);
5223 return 0;
5224}
5225
5226/*
5227 * 4.1.3 listen() - TCP Style Syntax
5228 *
5229 * Applications uses listen() to ready the SCTP endpoint for accepting
5230 * inbound associations.
5231 */
5232SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5233{
5234 struct sctp_sock *sp = sctp_sk(sk);
5235 struct sctp_endpoint *ep = sp->ep;
5236
5237 /* If backlog is zero, disable listening. */
5238 if (!backlog) {
5239 if (sctp_sstate(sk, CLOSED))
5240 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005241
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 sctp_unhash_endpoint(ep);
5243 sk->sk_state = SCTP_SS_CLOSED;
5244 }
5245
5246 if (sctp_sstate(sk, LISTENING))
5247 return 0;
5248
5249 /*
5250 * If a bind() or sctp_bindx() is not called prior to a listen()
5251 * call that allows new associations to be accepted, the system
5252 * picks an ephemeral port and will choose an address set equivalent
5253 * to binding with a wildcard address.
5254 *
5255 * This is not currently spelled out in the SCTP sockets
5256 * extensions draft, but follows the practice as seen in TCP
5257 * sockets.
5258 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005259 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 if (!ep->base.bind_addr.port) {
5261 if (sctp_autobind(sk))
5262 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005263 } else
5264 sctp_sk(sk)->bind_hash->fastreuse = 0;
5265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 sk->sk_max_ack_backlog = backlog;
5267 sctp_hash_endpoint(ep);
5268 return 0;
5269}
5270
5271/*
5272 * Move a socket to LISTENING state.
5273 */
5274int sctp_inet_listen(struct socket *sock, int backlog)
5275{
5276 struct sock *sk = sock->sk;
Herbert Xu1b489e12006-08-20 15:07:14 +10005277 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 int err = -EINVAL;
5279
5280 if (unlikely(backlog < 0))
5281 goto out;
5282
5283 sctp_lock_sock(sk);
5284
5285 if (sock->state != SS_UNCONNECTED)
5286 goto out;
5287
5288 /* Allocate HMAC for generating cookie. */
5289 if (sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005290 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005291 if (IS_ERR(tfm)) {
5292 if (net_ratelimit()) {
5293 printk(KERN_INFO
5294 "SCTP: failed to load transform for %s: %ld\n",
5295 sctp_hmac_alg, PTR_ERR(tfm));
5296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 err = -ENOSYS;
5298 goto out;
5299 }
5300 }
5301
5302 switch (sock->type) {
5303 case SOCK_SEQPACKET:
5304 err = sctp_seqpacket_listen(sk, backlog);
5305 break;
5306 case SOCK_STREAM:
5307 err = sctp_stream_listen(sk, backlog);
5308 break;
5309 default:
5310 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005311 }
5312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 if (err)
5314 goto cleanup;
5315
5316 /* Store away the transform reference. */
5317 sctp_sk(sk)->hmac = tfm;
5318out:
5319 sctp_release_sock(sk);
5320 return err;
5321cleanup:
Herbert Xu1b489e12006-08-20 15:07:14 +10005322 crypto_free_hash(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 goto out;
5324}
5325
5326/*
5327 * This function is done by modeling the current datagram_poll() and the
5328 * tcp_poll(). Note that, based on these implementations, we don't
5329 * lock the socket in this function, even though it seems that,
5330 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005331 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 * in this place. We assume that we don't need locks either until proven
5333 * otherwise.
5334 *
5335 * Another thing to note is that we include the Async I/O support
5336 * here, again, by modeling the current TCP/UDP code. We don't have
5337 * a good way to test with it yet.
5338 */
5339unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5340{
5341 struct sock *sk = sock->sk;
5342 struct sctp_sock *sp = sctp_sk(sk);
5343 unsigned int mask;
5344
5345 poll_wait(file, sk->sk_sleep, wait);
5346
5347 /* A TCP-style listening socket becomes readable when the accept queue
5348 * is not empty.
5349 */
5350 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5351 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005352 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
5354 mask = 0;
5355
5356 /* Is there any exceptional events? */
5357 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5358 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08005359 if (sk->sk_shutdown & RCV_SHUTDOWN)
5360 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (sk->sk_shutdown == SHUTDOWN_MASK)
5362 mask |= POLLHUP;
5363
5364 /* Is it readable? Reconsider this code with TCP-style support. */
5365 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5366 (sk->sk_shutdown & RCV_SHUTDOWN))
5367 mask |= POLLIN | POLLRDNORM;
5368
5369 /* The association is either gone or not ready. */
5370 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5371 return mask;
5372
5373 /* Is it writable? */
5374 if (sctp_writeable(sk)) {
5375 mask |= POLLOUT | POLLWRNORM;
5376 } else {
5377 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5378 /*
5379 * Since the socket is not locked, the buffer
5380 * might be made available after the writeable check and
5381 * before the bit is set. This could cause a lost I/O
5382 * signal. tcp_poll() has a race breaker for this race
5383 * condition. Based on their implementation, we put
5384 * in the following code to cover it as well.
5385 */
5386 if (sctp_writeable(sk))
5387 mask |= POLLOUT | POLLWRNORM;
5388 }
5389 return mask;
5390}
5391
5392/********************************************************************
5393 * 2nd Level Abstractions
5394 ********************************************************************/
5395
5396static struct sctp_bind_bucket *sctp_bucket_create(
5397 struct sctp_bind_hashbucket *head, unsigned short snum)
5398{
5399 struct sctp_bind_bucket *pp;
5400
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08005401 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 SCTP_DBG_OBJCNT_INC(bind_bucket);
5403 if (pp) {
5404 pp->port = snum;
5405 pp->fastreuse = 0;
5406 INIT_HLIST_HEAD(&pp->owner);
5407 if ((pp->next = head->chain) != NULL)
5408 pp->next->pprev = &pp->next;
5409 head->chain = pp;
5410 pp->pprev = &head->chain;
5411 }
5412 return pp;
5413}
5414
5415/* Caller must hold hashbucket lock for this tb with local BH disabled */
5416static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5417{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005418 if (pp && hlist_empty(&pp->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 if (pp->next)
5420 pp->next->pprev = pp->pprev;
5421 *(pp->pprev) = pp->next;
5422 kmem_cache_free(sctp_bucket_cachep, pp);
5423 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5424 }
5425}
5426
5427/* Release this socket's reference to a local port. */
5428static inline void __sctp_put_port(struct sock *sk)
5429{
5430 struct sctp_bind_hashbucket *head =
5431 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5432 struct sctp_bind_bucket *pp;
5433
5434 sctp_spin_lock(&head->lock);
5435 pp = sctp_sk(sk)->bind_hash;
5436 __sk_del_bind_node(sk);
5437 sctp_sk(sk)->bind_hash = NULL;
5438 inet_sk(sk)->num = 0;
5439 sctp_bucket_destroy(pp);
5440 sctp_spin_unlock(&head->lock);
5441}
5442
5443void sctp_put_port(struct sock *sk)
5444{
5445 sctp_local_bh_disable();
5446 __sctp_put_port(sk);
5447 sctp_local_bh_enable();
5448}
5449
5450/*
5451 * The system picks an ephemeral port and choose an address set equivalent
5452 * to binding with a wildcard address.
5453 * One of those addresses will be the primary address for the association.
5454 * This automatically enables the multihoming capability of SCTP.
5455 */
5456static int sctp_autobind(struct sock *sk)
5457{
5458 union sctp_addr autoaddr;
5459 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08005460 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
5462 /* Initialize a local sockaddr structure to INADDR_ANY. */
5463 af = sctp_sk(sk)->pf->af;
5464
5465 port = htons(inet_sk(sk)->num);
5466 af->inaddr_any(&autoaddr, port);
5467
5468 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5469}
5470
5471/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5472 *
5473 * From RFC 2292
5474 * 4.2 The cmsghdr Structure *
5475 *
5476 * When ancillary data is sent or received, any number of ancillary data
5477 * objects can be specified by the msg_control and msg_controllen members of
5478 * the msghdr structure, because each object is preceded by
5479 * a cmsghdr structure defining the object's length (the cmsg_len member).
5480 * Historically Berkeley-derived implementations have passed only one object
5481 * at a time, but this API allows multiple objects to be
5482 * passed in a single call to sendmsg() or recvmsg(). The following example
5483 * shows two ancillary data objects in a control buffer.
5484 *
5485 * |<--------------------------- msg_controllen -------------------------->|
5486 * | |
5487 *
5488 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5489 *
5490 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5491 * | | |
5492 *
5493 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5494 *
5495 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5496 * | | | | |
5497 *
5498 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5499 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5500 *
5501 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5502 *
5503 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5504 * ^
5505 * |
5506 *
5507 * msg_control
5508 * points here
5509 */
5510SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5511 sctp_cmsgs_t *cmsgs)
5512{
5513 struct cmsghdr *cmsg;
5514
5515 for (cmsg = CMSG_FIRSTHDR(msg);
5516 cmsg != NULL;
5517 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5518 if (!CMSG_OK(msg, cmsg))
5519 return -EINVAL;
5520
5521 /* Should we parse this header or ignore? */
5522 if (cmsg->cmsg_level != IPPROTO_SCTP)
5523 continue;
5524
5525 /* Strictly check lengths following example in SCM code. */
5526 switch (cmsg->cmsg_type) {
5527 case SCTP_INIT:
5528 /* SCTP Socket API Extension
5529 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5530 *
5531 * This cmsghdr structure provides information for
5532 * initializing new SCTP associations with sendmsg().
5533 * The SCTP_INITMSG socket option uses this same data
5534 * structure. This structure is not used for
5535 * recvmsg().
5536 *
5537 * cmsg_level cmsg_type cmsg_data[]
5538 * ------------ ------------ ----------------------
5539 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5540 */
5541 if (cmsg->cmsg_len !=
5542 CMSG_LEN(sizeof(struct sctp_initmsg)))
5543 return -EINVAL;
5544 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5545 break;
5546
5547 case SCTP_SNDRCV:
5548 /* SCTP Socket API Extension
5549 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5550 *
5551 * This cmsghdr structure specifies SCTP options for
5552 * sendmsg() and describes SCTP header information
5553 * about a received message through recvmsg().
5554 *
5555 * cmsg_level cmsg_type cmsg_data[]
5556 * ------------ ------------ ----------------------
5557 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5558 */
5559 if (cmsg->cmsg_len !=
5560 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5561 return -EINVAL;
5562
5563 cmsgs->info =
5564 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5565
5566 /* Minimally, validate the sinfo_flags. */
5567 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005568 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5569 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return -EINVAL;
5571 break;
5572
5573 default:
5574 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 }
5577 return 0;
5578}
5579
5580/*
5581 * Wait for a packet..
5582 * Note: This function is the same function as in core/datagram.c
5583 * with a few modifications to make lksctp work.
5584 */
5585static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5586{
5587 int error;
5588 DEFINE_WAIT(wait);
5589
5590 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5591
5592 /* Socket errors? */
5593 error = sock_error(sk);
5594 if (error)
5595 goto out;
5596
5597 if (!skb_queue_empty(&sk->sk_receive_queue))
5598 goto ready;
5599
5600 /* Socket shut down? */
5601 if (sk->sk_shutdown & RCV_SHUTDOWN)
5602 goto out;
5603
5604 /* Sequenced packets can come disconnected. If so we report the
5605 * problem.
5606 */
5607 error = -ENOTCONN;
5608
5609 /* Is there a good reason to think that we may receive some data? */
5610 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5611 goto out;
5612
5613 /* Handle signals. */
5614 if (signal_pending(current))
5615 goto interrupted;
5616
5617 /* Let another process have a go. Since we are going to sleep
5618 * anyway. Note: This may cause odd behaviors if the message
5619 * does not fit in the user's buffer, but this seems to be the
5620 * only way to honor MSG_DONTWAIT realistically.
5621 */
5622 sctp_release_sock(sk);
5623 *timeo_p = schedule_timeout(*timeo_p);
5624 sctp_lock_sock(sk);
5625
5626ready:
5627 finish_wait(sk->sk_sleep, &wait);
5628 return 0;
5629
5630interrupted:
5631 error = sock_intr_errno(*timeo_p);
5632
5633out:
5634 finish_wait(sk->sk_sleep, &wait);
5635 *err = error;
5636 return error;
5637}
5638
5639/* Receive a datagram.
5640 * Note: This is pretty much the same routine as in core/datagram.c
5641 * with a few changes to make lksctp work.
5642 */
5643static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5644 int noblock, int *err)
5645{
5646 int error;
5647 struct sk_buff *skb;
5648 long timeo;
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 timeo = sock_rcvtimeo(sk, noblock);
5651
5652 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5653 timeo, MAX_SCHEDULE_TIMEOUT);
5654
5655 do {
5656 /* Again only user level code calls this function,
5657 * so nothing interrupt level
5658 * will suddenly eat the receive_queue.
5659 *
5660 * Look at current nfs client by the way...
5661 * However, this function was corrent in any case. 8)
5662 */
5663 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005664 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 skb = skb_peek(&sk->sk_receive_queue);
5666 if (skb)
5667 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005668 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 } else {
5670 skb = skb_dequeue(&sk->sk_receive_queue);
5671 }
5672
5673 if (skb)
5674 return skb;
5675
Neil Horman6736dc32005-12-02 20:30:06 -08005676 /* Caller is allowed not to check sk->sk_err before calling. */
5677 error = sock_error(sk);
5678 if (error)
5679 goto no_packet;
5680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 if (sk->sk_shutdown & RCV_SHUTDOWN)
5682 break;
5683
5684 /* User doesn't want to wait. */
5685 error = -EAGAIN;
5686 if (!timeo)
5687 goto no_packet;
5688 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5689
5690 return NULL;
5691
5692no_packet:
5693 *err = error;
5694 return NULL;
5695}
5696
5697/* If sndbuf has changed, wake up per association sndbuf waiters. */
5698static void __sctp_write_space(struct sctp_association *asoc)
5699{
5700 struct sock *sk = asoc->base.sk;
5701 struct socket *sock = sk->sk_socket;
5702
5703 if ((sctp_wspace(asoc) > 0) && sock) {
5704 if (waitqueue_active(&asoc->wait))
5705 wake_up_interruptible(&asoc->wait);
5706
5707 if (sctp_writeable(sk)) {
5708 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5709 wake_up_interruptible(sk->sk_sleep);
5710
5711 /* Note that we try to include the Async I/O support
5712 * here by modeling from the current TCP/UDP code.
5713 * We have not tested with it yet.
5714 */
5715 if (sock->fasync_list &&
5716 !(sk->sk_shutdown & SEND_SHUTDOWN))
5717 sock_wake_async(sock, 2, POLL_OUT);
5718 }
5719 }
5720}
5721
5722/* Do accounting for the sndbuf space.
5723 * Decrement the used sndbuf space of the corresponding association by the
5724 * data size which was just transmitted(freed).
5725 */
5726static void sctp_wfree(struct sk_buff *skb)
5727{
5728 struct sctp_association *asoc;
5729 struct sctp_chunk *chunk;
5730 struct sock *sk;
5731
5732 /* Get the saved chunk pointer. */
5733 chunk = *((struct sctp_chunk **)(skb->cb));
5734 asoc = chunk->asoc;
5735 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07005736 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5737 sizeof(struct sk_buff) +
5738 sizeof(struct sctp_chunk);
5739
Neil Horman4eb701d2005-04-28 12:02:04 -07005740 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5741
5742 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 __sctp_write_space(asoc);
5744
5745 sctp_association_put(asoc);
5746}
5747
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07005748/* Do accounting for the receive space on the socket.
5749 * Accounting for the association is done in ulpevent.c
5750 * We set this as a destructor for the cloned data skbs so that
5751 * accounting is done at the correct time.
5752 */
5753void sctp_sock_rfree(struct sk_buff *skb)
5754{
5755 struct sock *sk = skb->sk;
5756 struct sctp_ulpevent *event = sctp_skb2event(skb);
5757
5758 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5759}
5760
5761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762/* Helper function to wait for space in the sndbuf. */
5763static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5764 size_t msg_len)
5765{
5766 struct sock *sk = asoc->base.sk;
5767 int err = 0;
5768 long current_timeo = *timeo_p;
5769 DEFINE_WAIT(wait);
5770
5771 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005772 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 /* Increment the association's refcnt. */
5775 sctp_association_hold(asoc);
5776
5777 /* Wait on the association specific sndbuf space. */
5778 for (;;) {
5779 prepare_to_wait_exclusive(&asoc->wait, &wait,
5780 TASK_INTERRUPTIBLE);
5781 if (!*timeo_p)
5782 goto do_nonblock;
5783 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5784 asoc->base.dead)
5785 goto do_error;
5786 if (signal_pending(current))
5787 goto do_interrupted;
5788 if (msg_len <= sctp_wspace(asoc))
5789 break;
5790
5791 /* Let another process have a go. Since we are going
5792 * to sleep anyway.
5793 */
5794 sctp_release_sock(sk);
5795 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005796 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 sctp_lock_sock(sk);
5798
5799 *timeo_p = current_timeo;
5800 }
5801
5802out:
5803 finish_wait(&asoc->wait, &wait);
5804
5805 /* Release the association's refcnt. */
5806 sctp_association_put(asoc);
5807
5808 return err;
5809
5810do_error:
5811 err = -EPIPE;
5812 goto out;
5813
5814do_interrupted:
5815 err = sock_intr_errno(*timeo_p);
5816 goto out;
5817
5818do_nonblock:
5819 err = -EAGAIN;
5820 goto out;
5821}
5822
5823/* If socket sndbuf has changed, wake up all per association waiters. */
5824void sctp_write_space(struct sock *sk)
5825{
5826 struct sctp_association *asoc;
5827 struct list_head *pos;
5828
5829 /* Wake up the tasks in each wait queue. */
5830 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5831 asoc = list_entry(pos, struct sctp_association, asocs);
5832 __sctp_write_space(asoc);
5833 }
5834}
5835
5836/* Is there any sndbuf space available on the socket?
5837 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08005838 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 * associations on the same socket. For a UDP-style socket with
5840 * multiple associations, it is possible for it to be "unwriteable"
5841 * prematurely. I assume that this is acceptable because
5842 * a premature "unwriteable" is better than an accidental "writeable" which
5843 * would cause an unwanted block under certain circumstances. For the 1-1
5844 * UDP-style sockets or TCP-style sockets, this code should work.
5845 * - Daisy
5846 */
5847static int sctp_writeable(struct sock *sk)
5848{
5849 int amt = 0;
5850
Neil Horman9bffc4a2005-12-19 14:24:40 -08005851 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 if (amt < 0)
5853 amt = 0;
5854 return amt;
5855}
5856
5857/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5858 * returns immediately with EINPROGRESS.
5859 */
5860static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5861{
5862 struct sock *sk = asoc->base.sk;
5863 int err = 0;
5864 long current_timeo = *timeo_p;
5865 DEFINE_WAIT(wait);
5866
5867 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5868 (long)(*timeo_p));
5869
5870 /* Increment the association's refcnt. */
5871 sctp_association_hold(asoc);
5872
5873 for (;;) {
5874 prepare_to_wait_exclusive(&asoc->wait, &wait,
5875 TASK_INTERRUPTIBLE);
5876 if (!*timeo_p)
5877 goto do_nonblock;
5878 if (sk->sk_shutdown & RCV_SHUTDOWN)
5879 break;
5880 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5881 asoc->base.dead)
5882 goto do_error;
5883 if (signal_pending(current))
5884 goto do_interrupted;
5885
5886 if (sctp_state(asoc, ESTABLISHED))
5887 break;
5888
5889 /* Let another process have a go. Since we are going
5890 * to sleep anyway.
5891 */
5892 sctp_release_sock(sk);
5893 current_timeo = schedule_timeout(current_timeo);
5894 sctp_lock_sock(sk);
5895
5896 *timeo_p = current_timeo;
5897 }
5898
5899out:
5900 finish_wait(&asoc->wait, &wait);
5901
5902 /* Release the association's refcnt. */
5903 sctp_association_put(asoc);
5904
5905 return err;
5906
5907do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08005908 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 err = -ETIMEDOUT;
5910 else
5911 err = -ECONNREFUSED;
5912 goto out;
5913
5914do_interrupted:
5915 err = sock_intr_errno(*timeo_p);
5916 goto out;
5917
5918do_nonblock:
5919 err = -EINPROGRESS;
5920 goto out;
5921}
5922
5923static int sctp_wait_for_accept(struct sock *sk, long timeo)
5924{
5925 struct sctp_endpoint *ep;
5926 int err = 0;
5927 DEFINE_WAIT(wait);
5928
5929 ep = sctp_sk(sk)->ep;
5930
5931
5932 for (;;) {
5933 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5934 TASK_INTERRUPTIBLE);
5935
5936 if (list_empty(&ep->asocs)) {
5937 sctp_release_sock(sk);
5938 timeo = schedule_timeout(timeo);
5939 sctp_lock_sock(sk);
5940 }
5941
5942 err = -EINVAL;
5943 if (!sctp_sstate(sk, LISTENING))
5944 break;
5945
5946 err = 0;
5947 if (!list_empty(&ep->asocs))
5948 break;
5949
5950 err = sock_intr_errno(timeo);
5951 if (signal_pending(current))
5952 break;
5953
5954 err = -EAGAIN;
5955 if (!timeo)
5956 break;
5957 }
5958
5959 finish_wait(sk->sk_sleep, &wait);
5960
5961 return err;
5962}
5963
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02005964static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965{
5966 DEFINE_WAIT(wait);
5967
5968 do {
5969 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5970 if (list_empty(&sctp_sk(sk)->ep->asocs))
5971 break;
5972 sctp_release_sock(sk);
5973 timeout = schedule_timeout(timeout);
5974 sctp_lock_sock(sk);
5975 } while (!signal_pending(current) && timeout);
5976
5977 finish_wait(sk->sk_sleep, &wait);
5978}
5979
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07005980static void sctp_sock_rfree_frag(struct sk_buff *skb)
5981{
5982 struct sk_buff *frag;
5983
5984 if (!skb->data_len)
5985 goto done;
5986
5987 /* Don't forget the fragments. */
5988 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5989 sctp_sock_rfree_frag(frag);
5990
5991done:
5992 sctp_sock_rfree(skb);
5993}
5994
5995static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5996{
5997 struct sk_buff *frag;
5998
5999 if (!skb->data_len)
6000 goto done;
6001
6002 /* Don't forget the fragments. */
6003 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6004 sctp_skb_set_owner_r_frag(frag, sk);
6005
6006done:
6007 sctp_skb_set_owner_r(skb, sk);
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010/* Populate the fields of the newsk from the oldsk and migrate the assoc
6011 * and its messages to the newsk.
6012 */
6013static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6014 struct sctp_association *assoc,
6015 sctp_socket_type_t type)
6016{
6017 struct sctp_sock *oldsp = sctp_sk(oldsk);
6018 struct sctp_sock *newsp = sctp_sk(newsk);
6019 struct sctp_bind_bucket *pp; /* hash list port iterator */
6020 struct sctp_endpoint *newep = newsp->ep;
6021 struct sk_buff *skb, *tmp;
6022 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006023 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 /* Migrate socket buffer sizes and all the socket level options to the
6026 * new socket.
6027 */
6028 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6029 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6030 /* Brute force copy old sctp opt. */
6031 inet_sk_copy_descendant(newsk, oldsk);
6032
6033 /* Restore the ep value that was overwritten with the above structure
6034 * copy.
6035 */
6036 newsp->ep = newep;
6037 newsp->hmac = NULL;
6038
6039 /* Hook this new socket in to the bind_hash list. */
6040 pp = sctp_sk(oldsk)->bind_hash;
6041 sk_add_bind_node(newsk, &pp->owner);
6042 sctp_sk(newsk)->bind_hash = pp;
6043 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6044
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006045 /* Copy the bind_addr list from the original endpoint to the new
6046 * endpoint so that we can handle restarts properly
6047 */
Vladislav Yasevicheb5fa392006-08-22 00:23:13 -07006048 if (PF_INET6 == assoc->base.sk->sk_family)
6049 flags = SCTP_ADDR6_ALLOWED;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006050 if (assoc->peer.ipv4_address)
6051 flags |= SCTP_ADDR4_PEERSUPP;
6052 if (assoc->peer.ipv6_address)
6053 flags |= SCTP_ADDR6_PEERSUPP;
6054 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6055 &oldsp->ep->base.bind_addr,
6056 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 /* Move any messages in the old socket's receive queue that are for the
6059 * peeled off association to the new socket's receive queue.
6060 */
6061 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6062 event = sctp_skb2event(skb);
6063 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006064 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006065 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006067 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 }
6069 }
6070
6071 /* Clean up any messages pending delivery due to partial
6072 * delivery. Three cases:
6073 * 1) No partial deliver; no work.
6074 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6075 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6076 */
6077 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006078 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006080 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 struct sk_buff_head *queue;
6082
6083 /* Decide which queue to move pd_lobby skbs to. */
6084 if (assoc->ulpq.pd_mode) {
6085 queue = &newsp->pd_lobby;
6086 } else
6087 queue = &newsk->sk_receive_queue;
6088
6089 /* Walk through the pd_lobby, looking for skbs that
6090 * need moved to the new socket.
6091 */
6092 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6093 event = sctp_skb2event(skb);
6094 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006095 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006096 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006098 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 }
6100 }
6101
6102 /* Clear up any skbs waiting for the partial
6103 * delivery to finish.
6104 */
6105 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006106 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108 }
6109
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006110 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6111 sctp_sock_rfree_frag(skb);
6112 sctp_skb_set_owner_r_frag(skb, newsk);
6113 }
6114
6115 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6116 sctp_sock_rfree_frag(skb);
6117 sctp_skb_set_owner_r_frag(skb, newsk);
6118 }
6119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 /* Set the type of socket to indicate that it is peeled off from the
6121 * original UDP-style socket or created with the accept() call on a
6122 * TCP-style socket..
6123 */
6124 newsp->type = type;
6125
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006126 /* Mark the new socket "in-use" by the user so that any packets
6127 * that may arrive on the association after we've moved it are
6128 * queued to the backlog. This prevents a potential race between
6129 * backlog processing on the old socket and new-packet processing
6130 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006131 *
6132 * The caller has just allocated newsk so we can guarantee that other
6133 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006134 */
Zach Brown5131a182007-06-22 15:14:46 -07006135 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
6138 /* If the association on the newsk is already closed before accept()
6139 * is called, set RCV_SHUTDOWN flag.
6140 */
6141 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6142 newsk->sk_shutdown |= RCV_SHUTDOWN;
6143
6144 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006145 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146}
6147
6148/* This proto struct describes the ULP interface for SCTP. */
6149struct proto sctp_prot = {
6150 .name = "SCTP",
6151 .owner = THIS_MODULE,
6152 .close = sctp_close,
6153 .connect = sctp_connect,
6154 .disconnect = sctp_disconnect,
6155 .accept = sctp_accept,
6156 .ioctl = sctp_ioctl,
6157 .init = sctp_init_sock,
6158 .destroy = sctp_destroy_sock,
6159 .shutdown = sctp_shutdown,
6160 .setsockopt = sctp_setsockopt,
6161 .getsockopt = sctp_getsockopt,
6162 .sendmsg = sctp_sendmsg,
6163 .recvmsg = sctp_recvmsg,
6164 .bind = sctp_bind,
6165 .backlog_rcv = sctp_backlog_rcv,
6166 .hash = sctp_hash,
6167 .unhash = sctp_unhash,
6168 .get_port = sctp_get_port,
6169 .obj_size = sizeof(struct sctp_sock),
6170};
6171
6172#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6173struct proto sctpv6_prot = {
6174 .name = "SCTPv6",
6175 .owner = THIS_MODULE,
6176 .close = sctp_close,
6177 .connect = sctp_connect,
6178 .disconnect = sctp_disconnect,
6179 .accept = sctp_accept,
6180 .ioctl = sctp_ioctl,
6181 .init = sctp_init_sock,
6182 .destroy = sctp_destroy_sock,
6183 .shutdown = sctp_shutdown,
6184 .setsockopt = sctp_setsockopt,
6185 .getsockopt = sctp_getsockopt,
6186 .sendmsg = sctp_sendmsg,
6187 .recvmsg = sctp_recvmsg,
6188 .bind = sctp_bind,
6189 .backlog_rcv = sctp_backlog_rcv,
6190 .hash = sctp_hash,
6191 .unhash = sctp_unhash,
6192 .get_port = sctp_get_port,
6193 .obj_size = sizeof(struct sctp6_sock),
6194};
6195#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */