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