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