blob: 2fc0a92caa78696284c1db90cdb1ac7b4dc45913 [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
Christoph Lametere18b8902006-12-06 20:33:20 -0800110extern struct kmem_cache *sctp_bucket_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
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;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800233 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!addr_asoc)
237 return NULL;
238
239 id_asoc = sctp_id2assoc(sk, id);
240 if (id_asoc && (id_asoc != addr_asoc))
241 return NULL;
242
243 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
244 (union sctp_addr *)addr);
245
246 return transport;
247}
248
249/* API 3.1.2 bind() - UDP Style Syntax
250 * The syntax of bind() is,
251 *
252 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
253 *
254 * sd - the socket descriptor returned by socket().
255 * addr - the address structure (struct sockaddr_in or struct
256 * sockaddr_in6 [RFC 2553]),
257 * addr_len - the size of the address structure.
258 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700259SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int retval = 0;
262
263 sctp_lock_sock(sk);
264
Frank Filz3f7a87d2005-06-20 13:14:57 -0700265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
266 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* Disallow binding twice. */
269 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700270 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 addr_len);
272 else
273 retval = -EINVAL;
274
275 sctp_release_sock(sk);
276
277 return retval;
278}
279
280static long sctp_get_port_local(struct sock *, union sctp_addr *);
281
282/* Verify this is a valid sockaddr. */
283static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
284 union sctp_addr *addr, int len)
285{
286 struct sctp_af *af;
287
288 /* Check minimum size. */
289 if (len < sizeof (struct sockaddr))
290 return NULL;
291
292 /* Does this PF support this AF? */
293 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
294 return NULL;
295
296 /* If we get this far, af is valid. */
297 af = sctp_get_af_specific(addr->sa.sa_family);
298
299 if (len < af->sockaddr_len)
300 return NULL;
301
302 return af;
303}
304
305/* Bind a local address either to an endpoint or to an association. */
306SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
307{
308 struct sctp_sock *sp = sctp_sk(sk);
309 struct sctp_endpoint *ep = sp->ep;
310 struct sctp_bind_addr *bp = &ep->base.bind_addr;
311 struct sctp_af *af;
312 unsigned short snum;
313 int ret = 0;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Common sockaddr verification. */
316 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700317 if (!af) {
318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
319 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700321 }
322
323 snum = ntohs(addr->v4.sin_port);
324
325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
326 ", port: %d, new port: %d, len: %d)\n",
327 sk,
328 addr,
329 bp->port, snum,
330 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* PF specific bind() address verification. */
333 if (!sp->pf->bind_verify(sp, addr))
334 return -EADDRNOTAVAIL;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* We must either be unbound, or bind to the same port. */
337 if (bp->port && (snum != bp->port)) {
338 SCTP_DEBUG_PRINTK("sctp_do_bind:"
339 " New port %d does not match existing port "
340 "%d.\n", snum, bp->port);
341 return -EINVAL;
342 }
343
344 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
345 return -EACCES;
346
347 /* Make sure we are allowed to bind here.
348 * The function sctp_get_port_local() does duplicate address
349 * detection.
350 */
351 if ((ret = sctp_get_port_local(sk, addr))) {
352 if (ret == (long) sk) {
353 /* This endpoint has a conflicting address. */
354 return -EINVAL;
355 } else {
356 return -EADDRINUSE;
357 }
358 }
359
360 /* Refresh ephemeral port. */
361 if (!bp->port)
362 bp->port = inet_sk(sk)->num;
363
364 /* Add the address to the bind address list. */
365 sctp_local_bh_disable();
366 sctp_write_lock(&ep->base.addr_lock);
367
368 /* Use GFP_ATOMIC since BHs are disabled. */
Al Viro5ab7b852006-11-20 17:10:38 -0800369 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 sctp_write_unlock(&ep->base.addr_lock);
371 sctp_local_bh_enable();
372
373 /* Copy back into socket for getsockname() use. */
374 if (!ret) {
375 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
376 af->to_sk_saddr(addr, sk);
377 }
378
379 return ret;
380}
381
382 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
383 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900384 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900386 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900388 * subsequent ASCONF. Note this restriction binds each side, so at any
389 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * from each endpoint).
391 */
392static int sctp_send_asconf(struct sctp_association *asoc,
393 struct sctp_chunk *chunk)
394{
395 int retval = 0;
396
397 /* If there is an outstanding ASCONF chunk, queue it for later
398 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700401 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900402 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 /* Hold the chunk until an ASCONF_ACK is received. */
406 sctp_chunk_hold(chunk);
407 retval = sctp_primitive_ASCONF(asoc, chunk);
408 if (retval)
409 sctp_chunk_free(chunk);
410 else
411 asoc->addip_last_asconf = chunk;
412
413out:
414 return retval;
415}
416
417/* Add a list of addresses as bind addresses to local endpoint or
418 * association.
419 *
420 * Basically run through each address specified in the addrs/addrcnt
421 * array/length pair, determine if it is IPv6 or IPv4 and call
422 * sctp_do_bind() on it.
423 *
424 * If any of them fails, then the operation will be reversed and the
425 * ones that were added will be removed.
426 *
427 * Only sctp_setsockopt_bindx() is supposed to call this function.
428 */
429int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
430{
431 int cnt;
432 int retval = 0;
433 void *addr_buf;
434 struct sockaddr *sa_addr;
435 struct sctp_af *af;
436
437 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
438 sk, addrs, addrcnt);
439
440 addr_buf = addrs;
441 for (cnt = 0; cnt < addrcnt; cnt++) {
442 /* The list may contain either IPv4 or IPv6 address;
443 * determine the address length for walking thru the list.
444 */
445 sa_addr = (struct sockaddr *)addr_buf;
446 af = sctp_get_af_specific(sa_addr->sa_family);
447 if (!af) {
448 retval = -EINVAL;
449 goto err_bindx_add;
450 }
451
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900452 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 af->sockaddr_len);
454
455 addr_buf += af->sockaddr_len;
456
457err_bindx_add:
458 if (retval < 0) {
459 /* Failed. Cleanup the ones that have been added */
460 if (cnt > 0)
461 sctp_bindx_rem(sk, addrs, cnt);
462 return retval;
463 }
464 }
465
466 return retval;
467}
468
469/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
470 * associations that are part of the endpoint indicating that a list of local
471 * addresses are added to the endpoint.
472 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900473 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * association, we do not send the chunk for that association. But it will not
475 * affect other associations.
476 *
477 * Only sctp_setsockopt_bindx() is supposed to call this function.
478 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900479static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct sockaddr *addrs,
481 int addrcnt)
482{
483 struct sctp_sock *sp;
484 struct sctp_endpoint *ep;
485 struct sctp_association *asoc;
486 struct sctp_bind_addr *bp;
487 struct sctp_chunk *chunk;
488 struct sctp_sockaddr_entry *laddr;
489 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700490 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 void *addr_buf;
492 struct sctp_af *af;
493 struct list_head *pos;
494 struct list_head *p;
495 int i;
496 int retval = 0;
497
498 if (!sctp_addip_enable)
499 return retval;
500
501 sp = sctp_sk(sk);
502 ep = sp->ep;
503
504 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
505 __FUNCTION__, sk, addrs, addrcnt);
506
507 list_for_each(pos, &ep->asocs) {
508 asoc = list_entry(pos, struct sctp_association, asocs);
509
510 if (!asoc->peer.asconf_capable)
511 continue;
512
513 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
514 continue;
515
516 if (!sctp_state(asoc, ESTABLISHED))
517 continue;
518
519 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900520 * in the bind address list of the association. If so,
521 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * other associations.
523 */
524 addr_buf = addrs;
525 for (i = 0; i < addrcnt; i++) {
526 addr = (union sctp_addr *)addr_buf;
527 af = sctp_get_af_specific(addr->v4.sin_family);
528 if (!af) {
529 retval = -EINVAL;
530 goto out;
531 }
532
533 if (sctp_assoc_lookup_laddr(asoc, addr))
534 break;
535
536 addr_buf += af->sockaddr_len;
537 }
538 if (i < addrcnt)
539 continue;
540
541 /* Use the first address in bind addr list of association as
542 * Address Parameter of ASCONF CHUNK.
543 */
544 sctp_read_lock(&asoc->base.addr_lock);
545 bp = &asoc->base.bind_addr;
546 p = bp->address_list.next;
547 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
548 sctp_read_unlock(&asoc->base.addr_lock);
549
Al Viro5ae955c2006-11-20 17:22:08 -0800550 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 addrcnt, SCTP_PARAM_ADD_IP);
552 if (!chunk) {
553 retval = -ENOMEM;
554 goto out;
555 }
556
557 retval = sctp_send_asconf(asoc, chunk);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700558 if (retval)
559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700561 /* Add the new addresses to the bind address list with
562 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700564 sctp_local_bh_disable();
565 sctp_write_lock(&asoc->base.addr_lock);
566 addr_buf = addrs;
567 for (i = 0; i < addrcnt; i++) {
568 addr = (union sctp_addr *)addr_buf;
569 af = sctp_get_af_specific(addr->v4.sin_family);
570 memcpy(&saveaddr, addr, af->sockaddr_len);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700571 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
572 GFP_ATOMIC);
573 addr_buf += af->sockaddr_len;
574 }
575 sctp_write_unlock(&asoc->base.addr_lock);
576 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579out:
580 return retval;
581}
582
583/* Remove a list of addresses from bind addresses list. Do not remove the
584 * last address.
585 *
586 * Basically run through each address specified in the addrs/addrcnt
587 * array/length pair, determine if it is IPv6 or IPv4 and call
588 * sctp_del_bind() on it.
589 *
590 * If any of them fails, then the operation will be reversed and the
591 * ones that were removed will be added back.
592 *
593 * At least one address has to be left; if only one address is
594 * available, the operation will return -EBUSY.
595 *
596 * Only sctp_setsockopt_bindx() is supposed to call this function.
597 */
598int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
599{
600 struct sctp_sock *sp = sctp_sk(sk);
601 struct sctp_endpoint *ep = sp->ep;
602 int cnt;
603 struct sctp_bind_addr *bp = &ep->base.bind_addr;
604 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800606 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct sctp_af *af;
608
609 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
610 sk, addrs, addrcnt);
611
612 addr_buf = addrs;
613 for (cnt = 0; cnt < addrcnt; cnt++) {
614 /* If the bind address list is empty or if there is only one
615 * bind address, there is nothing more to be removed (we need
616 * at least one address here).
617 */
618 if (list_empty(&bp->address_list) ||
619 (sctp_list_single_entry(&bp->address_list))) {
620 retval = -EBUSY;
621 goto err_bindx_rem;
622 }
623
Al Viroc9a08502006-11-20 17:07:48 -0800624 sa_addr = (union sctp_addr *)addr_buf;
625 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!af) {
627 retval = -EINVAL;
628 goto err_bindx_rem;
629 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700630
631 if (!af->addr_valid(sa_addr, sp, NULL)) {
632 retval = -EADDRNOTAVAIL;
633 goto err_bindx_rem;
634 }
635
Al Viroc9a08502006-11-20 17:07:48 -0800636 if (sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 retval = -EINVAL;
638 goto err_bindx_rem;
639 }
640
641 /* FIXME - There is probably a need to check if sk->sk_saddr and
642 * sk->sk_rcv_addr are currently set to one of the addresses to
643 * be removed. This is something which needs to be looked into
644 * when we are fixing the outstanding issues with multi-homing
645 * socket routing and failover schemes. Refer to comments in
646 * sctp_do_bind(). -daisy
647 */
648 sctp_local_bh_disable();
649 sctp_write_lock(&ep->base.addr_lock);
650
Al Viroc9a08502006-11-20 17:07:48 -0800651 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 sctp_write_unlock(&ep->base.addr_lock);
654 sctp_local_bh_enable();
655
656 addr_buf += af->sockaddr_len;
657err_bindx_rem:
658 if (retval < 0) {
659 /* Failed. Add the ones that has been removed back */
660 if (cnt > 0)
661 sctp_bindx_add(sk, addrs, cnt);
662 return retval;
663 }
664 }
665
666 return retval;
667}
668
669/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
670 * the associations that are part of the endpoint indicating that a list of
671 * local addresses are removed from the endpoint.
672 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900673 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * association, we do not send the chunk for that association. But it will not
675 * affect other associations.
676 *
677 * Only sctp_setsockopt_bindx() is supposed to call this function.
678 */
679static int sctp_send_asconf_del_ip(struct sock *sk,
680 struct sockaddr *addrs,
681 int addrcnt)
682{
683 struct sctp_sock *sp;
684 struct sctp_endpoint *ep;
685 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700686 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct sctp_bind_addr *bp;
688 struct sctp_chunk *chunk;
689 union sctp_addr *laddr;
690 void *addr_buf;
691 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700692 struct list_head *pos, *pos1;
693 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int i;
695 int retval = 0;
696
697 if (!sctp_addip_enable)
698 return retval;
699
700 sp = sctp_sk(sk);
701 ep = sp->ep;
702
703 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
704 __FUNCTION__, sk, addrs, addrcnt);
705
706 list_for_each(pos, &ep->asocs) {
707 asoc = list_entry(pos, struct sctp_association, asocs);
708
709 if (!asoc->peer.asconf_capable)
710 continue;
711
712 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
713 continue;
714
715 if (!sctp_state(asoc, ESTABLISHED))
716 continue;
717
718 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900719 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * If so, do not send the asconf chunk to its peer, but
721 * continue with other associations.
722 */
723 addr_buf = addrs;
724 for (i = 0; i < addrcnt; i++) {
725 laddr = (union sctp_addr *)addr_buf;
726 af = sctp_get_af_specific(laddr->v4.sin_family);
727 if (!af) {
728 retval = -EINVAL;
729 goto out;
730 }
731
732 if (!sctp_assoc_lookup_laddr(asoc, laddr))
733 break;
734
735 addr_buf += af->sockaddr_len;
736 }
737 if (i < addrcnt)
738 continue;
739
740 /* Find one address in the association's bind address list
741 * that is not in the packed array of addresses. This is to
742 * make sure that we do not delete all the addresses in the
743 * association.
744 */
745 sctp_read_lock(&asoc->base.addr_lock);
746 bp = &asoc->base.bind_addr;
747 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
748 addrcnt, sp);
749 sctp_read_unlock(&asoc->base.addr_lock);
750 if (!laddr)
751 continue;
752
753 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
754 SCTP_PARAM_DEL_IP);
755 if (!chunk) {
756 retval = -ENOMEM;
757 goto out;
758 }
759
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700760 /* Reset use_as_src flag for the addresses in the bind address
761 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700763 sctp_local_bh_disable();
764 sctp_write_lock(&asoc->base.addr_lock);
765 addr_buf = addrs;
766 for (i = 0; i < addrcnt; i++) {
767 laddr = (union sctp_addr *)addr_buf;
768 af = sctp_get_af_specific(laddr->v4.sin_family);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700769 list_for_each(pos1, &bp->address_list) {
770 saddr = list_entry(pos1,
771 struct sctp_sockaddr_entry,
772 list);
Al Viro5f242a12006-11-20 17:05:23 -0800773 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700774 saddr->use_as_src = 0;
775 }
776 addr_buf += af->sockaddr_len;
777 }
778 sctp_write_unlock(&asoc->base.addr_lock);
779 sctp_local_bh_enable();
780
781 /* Update the route and saddr entries for all the transports
782 * as some of the addresses in the bind address list are
783 * about to be deleted and cannot be used as source addresses.
784 */
785 list_for_each(pos1, &asoc->peer.transport_addr_list) {
786 transport = list_entry(pos1, struct sctp_transport,
787 transports);
788 dst_release(transport->dst);
789 sctp_transport_route(transport, NULL,
790 sctp_sk(asoc->base.sk));
791 }
792
793 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795out:
796 return retval;
797}
798
799/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
800 *
801 * API 8.1
802 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
803 * int flags);
804 *
805 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
806 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
807 * or IPv6 addresses.
808 *
809 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
810 * Section 3.1.2 for this usage.
811 *
812 * addrs is a pointer to an array of one or more socket addresses. Each
813 * address is contained in its appropriate structure (i.e. struct
814 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700815 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * representation is termed a "packed array" of addresses). The caller
817 * specifies the number of addresses in the array with addrcnt.
818 *
819 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
820 * -1, and sets errno to the appropriate error code.
821 *
822 * For SCTP, the port given in each socket address must be the same, or
823 * sctp_bindx() will fail, setting errno to EINVAL.
824 *
825 * The flags parameter is formed from the bitwise OR of zero or more of
826 * the following currently defined flags:
827 *
828 * SCTP_BINDX_ADD_ADDR
829 *
830 * SCTP_BINDX_REM_ADDR
831 *
832 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
833 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
834 * addresses from the association. The two flags are mutually exclusive;
835 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
836 * not remove all addresses from an association; sctp_bindx() will
837 * reject such an attempt with EINVAL.
838 *
839 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
840 * additional addresses with an endpoint after calling bind(). Or use
841 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
842 * socket is associated with so that no new association accepted will be
843 * associated with those addresses. If the endpoint supports dynamic
844 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
845 * endpoint to send the appropriate message to the peer to change the
846 * peers address lists.
847 *
848 * Adding and removing addresses from a connected association is
849 * optional functionality. Implementations that do not support this
850 * functionality should return EOPNOTSUPP.
851 *
852 * Basically do nothing but copying the addresses from user to kernel
853 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700854 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
855 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *
857 * We don't use copy_from_user() for optimization: we first do the
858 * sanity checks (buffer size -fast- and access check-healthy
859 * pointer); if all of those succeed, then we can alloc the memory
860 * (expensive operation) needed to copy the data to kernel. Then we do
861 * the copying without checking the user space area
862 * (__copy_from_user()).
863 *
864 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
865 * it.
866 *
867 * sk The sk of the socket
868 * addrs The pointer to the addresses in user land
869 * addrssize Size of the addrs buffer
870 * op Operation to perform (add or remove, see the flags of
871 * sctp_bindx)
872 *
873 * Returns 0 if ok, <0 errno code on error.
874 */
875SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
876 struct sockaddr __user *addrs,
877 int addrs_size, int op)
878{
879 struct sockaddr *kaddrs;
880 int err;
881 int addrcnt = 0;
882 int walk_size = 0;
883 struct sockaddr *sa_addr;
884 void *addr_buf;
885 struct sctp_af *af;
886
887 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
888 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
889
890 if (unlikely(addrs_size <= 0))
891 return -EINVAL;
892
893 /* Check the user passed a healthy pointer. */
894 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
895 return -EFAULT;
896
897 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800898 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (unlikely(!kaddrs))
900 return -ENOMEM;
901
902 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
903 kfree(kaddrs);
904 return -EFAULT;
905 }
906
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900907 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 addr_buf = kaddrs;
909 while (walk_size < addrs_size) {
910 sa_addr = (struct sockaddr *)addr_buf;
911 af = sctp_get_af_specific(sa_addr->sa_family);
912
913 /* If the address family is not supported or if this address
914 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
917 kfree(kaddrs);
918 return -EINVAL;
919 }
920 addrcnt++;
921 addr_buf += af->sockaddr_len;
922 walk_size += af->sockaddr_len;
923 }
924
925 /* Do the work. */
926 switch (op) {
927 case SCTP_BINDX_ADD_ADDR:
928 err = sctp_bindx_add(sk, kaddrs, addrcnt);
929 if (err)
930 goto out;
931 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
932 break;
933
934 case SCTP_BINDX_REM_ADDR:
935 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
936 if (err)
937 goto out;
938 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
939 break;
940
941 default:
942 err = -EINVAL;
943 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946out:
947 kfree(kaddrs);
948
949 return err;
950}
951
Frank Filz3f7a87d2005-06-20 13:14:57 -0700952/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
953 *
954 * Common routine for handling connect() and sctp_connectx().
955 * Connect will come in with just a single address.
956 */
957static int __sctp_connect(struct sock* sk,
958 struct sockaddr *kaddrs,
959 int addrs_size)
960{
961 struct sctp_sock *sp;
962 struct sctp_endpoint *ep;
963 struct sctp_association *asoc = NULL;
964 struct sctp_association *asoc2;
965 struct sctp_transport *transport;
966 union sctp_addr to;
967 struct sctp_af *af;
968 sctp_scope_t scope;
969 long timeo;
970 int err = 0;
971 int addrcnt = 0;
972 int walk_size = 0;
Al Viro4bdf4b52006-11-20 17:10:20 -0800973 union sctp_addr *sa_addr;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700974 void *addr_buf;
975
976 sp = sctp_sk(sk);
977 ep = sp->ep;
978
979 /* connect() cannot be done on a socket that is already in ESTABLISHED
980 * state - UDP-style peeled off socket or a TCP-style socket that
981 * is already connected.
982 * It cannot be done even on a TCP-style listening socket.
983 */
984 if (sctp_sstate(sk, ESTABLISHED) ||
985 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
986 err = -EISCONN;
987 goto out_free;
988 }
989
990 /* Walk through the addrs buffer and count the number of addresses. */
991 addr_buf = kaddrs;
992 while (walk_size < addrs_size) {
Al Viro4bdf4b52006-11-20 17:10:20 -0800993 sa_addr = (union sctp_addr *)addr_buf;
994 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700995
996 /* If the address family is not supported or if this address
997 * causes the address buffer to overflow return EINVAL.
998 */
999 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1000 err = -EINVAL;
1001 goto out_free;
1002 }
1003
Al Viro4bdf4b52006-11-20 17:10:20 -08001004 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001005 if (err)
1006 goto out_free;
1007
1008 memcpy(&to, sa_addr, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001009
1010 /* Check if there already is a matching association on the
1011 * endpoint (other than the one created here).
1012 */
Al Virocd4ff032006-11-20 17:11:33 -08001013 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001014 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 */
Al Viro6c7be552006-11-20 17:11:50 -08001026 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001027 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 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001044 * 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
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001048 * 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
Al Virodce116a2006-11-20 17:25:15 -08001057 scope = sctp_scope(sa_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001058 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"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001103 " kaddrs: %p err: %d\n",
1104 asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001105 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 Virodce116a2006-11-20 17:25:15 -08001365 union sctp_addr to;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 msg_name = msg->msg_name;
1420 }
1421
1422 sinfo = cmsgs.info;
1423 sinit = cmsgs.init;
1424
1425 /* Did the user specify SNDRCVINFO? */
1426 if (sinfo) {
1427 sinfo_flags = sinfo->sinfo_flags;
1428 associd = sinfo->sinfo_assoc_id;
1429 }
1430
1431 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1432 msg_len, sinfo_flags);
1433
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001434 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1435 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 err = -EINVAL;
1437 goto out_nounlock;
1438 }
1439
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001440 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1441 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1442 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001444 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001445 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1446 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 err = -EINVAL;
1448 goto out_nounlock;
1449 }
1450
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001451 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * specified in msg_name.
1453 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001454 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 err = -EINVAL;
1456 goto out_nounlock;
1457 }
1458
1459 transport = NULL;
1460
1461 SCTP_DEBUG_PRINTK("About to look up association.\n");
1462
1463 sctp_lock_sock(sk);
1464
1465 /* If a msg_name has been specified, assume this is to be used. */
1466 if (msg_name) {
1467 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001468 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (!asoc) {
1470 /* If we could not find a matching association on the
1471 * endpoint, make sure that it is not a TCP-style
1472 * socket that already has an association or there is
1473 * no peeled-off association on another socket.
1474 */
1475 if ((sctp_style(sk, TCP) &&
1476 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001477 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 err = -EADDRNOTAVAIL;
1479 goto out_unlock;
1480 }
1481 }
1482 } else {
1483 asoc = sctp_id2assoc(sk, associd);
1484 if (!asoc) {
1485 err = -EPIPE;
1486 goto out_unlock;
1487 }
1488 }
1489
1490 if (asoc) {
1491 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1492
1493 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1494 * socket that has an association in CLOSED state. This can
1495 * happen when an accepted socket has an association that is
1496 * already CLOSED.
1497 */
1498 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1499 err = -EPIPE;
1500 goto out_unlock;
1501 }
1502
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001503 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1505 asoc);
1506 sctp_primitive_SHUTDOWN(asoc, NULL);
1507 err = 0;
1508 goto out_unlock;
1509 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001510 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001511 struct sctp_chunk *chunk;
1512
1513 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1514 if (!chunk) {
1515 err = -ENOMEM;
1516 goto out_unlock;
1517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001520 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 err = 0;
1522 goto out_unlock;
1523 }
1524 }
1525
1526 /* Do we need to create the association? */
1527 if (!asoc) {
1528 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1529
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001530 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 err = -EINVAL;
1532 goto out_unlock;
1533 }
1534
1535 /* Check for invalid stream against the stream counts,
1536 * either the default or the user specified stream counts.
1537 */
1538 if (sinfo) {
1539 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1540 /* Check against the defaults. */
1541 if (sinfo->sinfo_stream >=
1542 sp->initmsg.sinit_num_ostreams) {
1543 err = -EINVAL;
1544 goto out_unlock;
1545 }
1546 } else {
1547 /* Check against the requested. */
1548 if (sinfo->sinfo_stream >=
1549 sinit->sinit_num_ostreams) {
1550 err = -EINVAL;
1551 goto out_unlock;
1552 }
1553 }
1554 }
1555
1556 /*
1557 * API 3.1.2 bind() - UDP Style Syntax
1558 * If a bind() or sctp_bindx() is not called prior to a
1559 * sendmsg() call that initiates a new association, the
1560 * system picks an ephemeral port and will choose an address
1561 * set equivalent to binding with a wildcard address.
1562 */
1563 if (!ep->base.bind_addr.port) {
1564 if (sctp_autobind(sk)) {
1565 err = -EAGAIN;
1566 goto out_unlock;
1567 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001568 } else {
1569 /*
1570 * If an unprivileged user inherits a one-to-many
1571 * style socket with open associations on a privileged
1572 * port, it MAY be permitted to accept new associations,
1573 * but it SHOULD NOT be permitted to open new
1574 * associations.
1575 */
1576 if (ep->base.bind_addr.port < PROT_SOCK &&
1577 !capable(CAP_NET_BIND_SERVICE)) {
1578 err = -EACCES;
1579 goto out_unlock;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
1583 scope = sctp_scope(&to);
1584 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1585 if (!new_asoc) {
1586 err = -ENOMEM;
1587 goto out_unlock;
1588 }
1589 asoc = new_asoc;
1590
1591 /* If the SCTP_INIT ancillary data is specified, set all
1592 * the association init values accordingly.
1593 */
1594 if (sinit) {
1595 if (sinit->sinit_num_ostreams) {
1596 asoc->c.sinit_num_ostreams =
1597 sinit->sinit_num_ostreams;
1598 }
1599 if (sinit->sinit_max_instreams) {
1600 asoc->c.sinit_max_instreams =
1601 sinit->sinit_max_instreams;
1602 }
1603 if (sinit->sinit_max_attempts) {
1604 asoc->max_init_attempts
1605 = sinit->sinit_max_attempts;
1606 }
1607 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001608 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1610 }
1611 }
1612
1613 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001614 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!transport) {
1616 err = -ENOMEM;
1617 goto out_free;
1618 }
1619 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1620 if (err < 0) {
1621 err = -ENOMEM;
1622 goto out_free;
1623 }
1624 }
1625
1626 /* ASSERT: we have a valid association at this point. */
1627 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1628
1629 if (!sinfo) {
1630 /* If the user didn't specify SNDRCVINFO, make up one with
1631 * some defaults.
1632 */
1633 default_sinfo.sinfo_stream = asoc->default_stream;
1634 default_sinfo.sinfo_flags = asoc->default_flags;
1635 default_sinfo.sinfo_ppid = asoc->default_ppid;
1636 default_sinfo.sinfo_context = asoc->default_context;
1637 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1638 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1639 sinfo = &default_sinfo;
1640 }
1641
1642 /* API 7.1.7, the sndbuf size per association bounds the
1643 * maximum size of data that can be sent in a single send call.
1644 */
1645 if (msg_len > sk->sk_sndbuf) {
1646 err = -EMSGSIZE;
1647 goto out_free;
1648 }
1649
1650 /* If fragmentation is disabled and the message length exceeds the
1651 * association fragmentation point, return EMSGSIZE. The I-D
1652 * does not specify what this error is, but this looks like
1653 * a great fit.
1654 */
1655 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1656 err = -EMSGSIZE;
1657 goto out_free;
1658 }
1659
1660 if (sinfo) {
1661 /* Check for invalid stream. */
1662 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1663 err = -EINVAL;
1664 goto out_free;
1665 }
1666 }
1667
1668 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1669 if (!sctp_wspace(asoc)) {
1670 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1671 if (err)
1672 goto out_free;
1673 }
1674
1675 /* If an address is passed with the sendto/sendmsg call, it is used
1676 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001677 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
1679 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001680 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001681 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (!chunk_tp) {
1683 err = -EINVAL;
1684 goto out_free;
1685 }
1686 } else
1687 chunk_tp = NULL;
1688
1689 /* Auto-connect, if we aren't connected already. */
1690 if (sctp_state(asoc, CLOSED)) {
1691 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1692 if (err < 0)
1693 goto out_free;
1694 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1695 }
1696
1697 /* Break the message into multiple chunks of maximum size. */
1698 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1699 if (!datamsg) {
1700 err = -ENOMEM;
1701 goto out_free;
1702 }
1703
1704 /* Now send the (possibly) fragmented message. */
1705 list_for_each(pos, &datamsg->chunks) {
1706 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1707 sctp_datamsg_track(chunk);
1708
1709 /* Do accounting for the write space. */
1710 sctp_set_owner_w(chunk);
1711
1712 chunk->transport = chunk_tp;
1713
1714 /* Send it to the lower layers. Note: all chunks
1715 * must either fail or succeed. The lower layer
1716 * works that way today. Keep it that way or this
1717 * breaks.
1718 */
1719 err = sctp_primitive_SEND(asoc, chunk);
1720 /* Did the lower layer accept the chunk? */
1721 if (err)
1722 sctp_chunk_free(chunk);
1723 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1724 }
1725
1726 sctp_datamsg_free(datamsg);
1727 if (err)
1728 goto out_free;
1729 else
1730 err = msg_len;
1731
1732 /* If we are already past ASSOCIATE, the lower
1733 * layers are responsible for association cleanup.
1734 */
1735 goto out_unlock;
1736
1737out_free:
1738 if (new_asoc)
1739 sctp_association_free(asoc);
1740out_unlock:
1741 sctp_release_sock(sk);
1742
1743out_nounlock:
1744 return sctp_error(sk, msg_flags, err);
1745
1746#if 0
1747do_sock_err:
1748 if (msg_len)
1749 err = msg_len;
1750 else
1751 err = sock_error(sk);
1752 goto out;
1753
1754do_interrupted:
1755 if (msg_len)
1756 err = msg_len;
1757 goto out;
1758#endif /* 0 */
1759}
1760
1761/* This is an extended version of skb_pull() that removes the data from the
1762 * start of a skb even when data is spread across the list of skb's in the
1763 * frag_list. len specifies the total amount of data that needs to be removed.
1764 * when 'len' bytes could be removed from the skb, it returns 0.
1765 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1766 * could not be removed.
1767 */
1768static int sctp_skb_pull(struct sk_buff *skb, int len)
1769{
1770 struct sk_buff *list;
1771 int skb_len = skb_headlen(skb);
1772 int rlen;
1773
1774 if (len <= skb_len) {
1775 __skb_pull(skb, len);
1776 return 0;
1777 }
1778 len -= skb_len;
1779 __skb_pull(skb, skb_len);
1780
1781 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1782 rlen = sctp_skb_pull(list, len);
1783 skb->len -= (len-rlen);
1784 skb->data_len -= (len-rlen);
1785
1786 if (!rlen)
1787 return 0;
1788
1789 len = rlen;
1790 }
1791
1792 return len;
1793}
1794
1795/* API 3.1.3 recvmsg() - UDP Style Syntax
1796 *
1797 * ssize_t recvmsg(int socket, struct msghdr *message,
1798 * int flags);
1799 *
1800 * socket - the socket descriptor of the endpoint.
1801 * message - pointer to the msghdr structure which contains a single
1802 * user message and possibly some ancillary data.
1803 *
1804 * See Section 5 for complete description of the data
1805 * structures.
1806 *
1807 * flags - flags sent or received with the user message, see Section
1808 * 5 for complete description of the flags.
1809 */
1810static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1811
1812SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1813 struct msghdr *msg, size_t len, int noblock,
1814 int flags, int *addr_len)
1815{
1816 struct sctp_ulpevent *event = NULL;
1817 struct sctp_sock *sp = sctp_sk(sk);
1818 struct sk_buff *skb;
1819 int copied;
1820 int err = 0;
1821 int skb_len;
1822
1823 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1824 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1825 "len", len, "knoblauch", noblock,
1826 "flags", flags, "addr_len", addr_len);
1827
1828 sctp_lock_sock(sk);
1829
1830 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1831 err = -ENOTCONN;
1832 goto out;
1833 }
1834
1835 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1836 if (!skb)
1837 goto out;
1838
1839 /* Get the total length of the skb including any skb's in the
1840 * frag_list.
1841 */
1842 skb_len = skb->len;
1843
1844 copied = skb_len;
1845 if (copied > len)
1846 copied = len;
1847
1848 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1849
1850 event = sctp_skb2event(skb);
1851
1852 if (err)
1853 goto out_free;
1854
1855 sock_recv_timestamp(msg, sk, skb);
1856 if (sctp_ulpevent_is_notification(event)) {
1857 msg->msg_flags |= MSG_NOTIFICATION;
1858 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1859 } else {
1860 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1861 }
1862
1863 /* Check if we allow SCTP_SNDRCVINFO. */
1864 if (sp->subscribe.sctp_data_io_event)
1865 sctp_ulpevent_read_sndrcvinfo(event, msg);
1866#if 0
1867 /* FIXME: we should be calling IP/IPv6 layers. */
1868 if (sk->sk_protinfo.af_inet.cmsg_flags)
1869 ip_cmsg_recv(msg, skb);
1870#endif
1871
1872 err = copied;
1873
1874 /* If skb's length exceeds the user's buffer, update the skb and
1875 * push it back to the receive_queue so that the next call to
1876 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1877 */
1878 if (skb_len > copied) {
1879 msg->msg_flags &= ~MSG_EOR;
1880 if (flags & MSG_PEEK)
1881 goto out_free;
1882 sctp_skb_pull(skb, copied);
1883 skb_queue_head(&sk->sk_receive_queue, skb);
1884
1885 /* When only partial message is copied to the user, increase
1886 * rwnd by that amount. If all the data in the skb is read,
1887 * rwnd is updated when the event is freed.
1888 */
1889 sctp_assoc_rwnd_increase(event->asoc, copied);
1890 goto out;
1891 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1892 (event->msg_flags & MSG_EOR))
1893 msg->msg_flags |= MSG_EOR;
1894 else
1895 msg->msg_flags &= ~MSG_EOR;
1896
1897out_free:
1898 if (flags & MSG_PEEK) {
1899 /* Release the skb reference acquired after peeking the skb in
1900 * sctp_skb_recv_datagram().
1901 */
1902 kfree_skb(skb);
1903 } else {
1904 /* Free the event which includes releasing the reference to
1905 * the owner of the skb, freeing the skb and updating the
1906 * rwnd.
1907 */
1908 sctp_ulpevent_free(event);
1909 }
1910out:
1911 sctp_release_sock(sk);
1912 return err;
1913}
1914
1915/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1916 *
1917 * This option is a on/off flag. If enabled no SCTP message
1918 * fragmentation will be performed. Instead if a message being sent
1919 * exceeds the current PMTU size, the message will NOT be sent and
1920 * instead a error will be indicated to the user.
1921 */
1922static int sctp_setsockopt_disable_fragments(struct sock *sk,
1923 char __user *optval, int optlen)
1924{
1925 int val;
1926
1927 if (optlen < sizeof(int))
1928 return -EINVAL;
1929
1930 if (get_user(val, (int __user *)optval))
1931 return -EFAULT;
1932
1933 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1934
1935 return 0;
1936}
1937
1938static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1939 int optlen)
1940{
1941 if (optlen != sizeof(struct sctp_event_subscribe))
1942 return -EINVAL;
1943 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1944 return -EFAULT;
1945 return 0;
1946}
1947
1948/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1949 *
1950 * This socket option is applicable to the UDP-style socket only. When
1951 * set it will cause associations that are idle for more than the
1952 * specified number of seconds to automatically close. An association
1953 * being idle is defined an association that has NOT sent or received
1954 * user data. The special value of '0' indicates that no automatic
1955 * close of any associations should be performed. The option expects an
1956 * integer defining the number of seconds of idle time before an
1957 * association is closed.
1958 */
1959static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1960 int optlen)
1961{
1962 struct sctp_sock *sp = sctp_sk(sk);
1963
1964 /* Applicable to UDP-style socket only */
1965 if (sctp_style(sk, TCP))
1966 return -EOPNOTSUPP;
1967 if (optlen != sizeof(int))
1968 return -EINVAL;
1969 if (copy_from_user(&sp->autoclose, optval, optlen))
1970 return -EFAULT;
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return 0;
1973}
1974
1975/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1976 *
1977 * Applications can enable or disable heartbeats for any peer address of
1978 * an association, modify an address's heartbeat interval, force a
1979 * heartbeat to be sent immediately, and adjust the address's maximum
1980 * number of retransmissions sent before an address is considered
1981 * unreachable. The following structure is used to access and modify an
1982 * address's parameters:
1983 *
1984 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08001985 * sctp_assoc_t spp_assoc_id;
1986 * struct sockaddr_storage spp_address;
1987 * uint32_t spp_hbinterval;
1988 * uint16_t spp_pathmaxrxt;
1989 * uint32_t spp_pathmtu;
1990 * uint32_t spp_sackdelay;
1991 * uint32_t spp_flags;
1992 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08001994 * spp_assoc_id - (one-to-many style socket) This is filled in the
1995 * application, and identifies the association for
1996 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 * spp_address - This specifies which address is of interest.
1998 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08001999 * in milliseconds. If a value of zero
2000 * is present in this field then no changes are to
2001 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 * spp_pathmaxrxt - This contains the maximum number of
2003 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002004 * considered unreachable. If a value of zero
2005 * is present in this field then no changes are to
2006 * be made to this parameter.
2007 * spp_pathmtu - When Path MTU discovery is disabled the value
2008 * specified here will be the "fixed" path mtu.
2009 * Note that if the spp_address field is empty
2010 * then all associations on this address will
2011 * have this fixed path mtu set upon them.
2012 *
2013 * spp_sackdelay - When delayed sack is enabled, this value specifies
2014 * the number of milliseconds that sacks will be delayed
2015 * for. This value will apply to all addresses of an
2016 * association if the spp_address field is empty. Note
2017 * also, that if delayed sack is enabled and this
2018 * value is set to 0, no change is made to the last
2019 * recorded delayed sack timer value.
2020 *
2021 * spp_flags - These flags are used to control various features
2022 * on an association. The flag field may contain
2023 * zero or more of the following options.
2024 *
2025 * SPP_HB_ENABLE - Enable heartbeats on the
2026 * specified address. Note that if the address
2027 * field is empty all addresses for the association
2028 * have heartbeats enabled upon them.
2029 *
2030 * SPP_HB_DISABLE - Disable heartbeats on the
2031 * speicifed address. Note that if the address
2032 * field is empty all addresses for the association
2033 * will have their heartbeats disabled. Note also
2034 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2035 * mutually exclusive, only one of these two should
2036 * be specified. Enabling both fields will have
2037 * undetermined results.
2038 *
2039 * SPP_HB_DEMAND - Request a user initiated heartbeat
2040 * to be made immediately.
2041 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002042 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2043 * heartbeat delayis to be set to the value of 0
2044 * milliseconds.
2045 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002046 * SPP_PMTUD_ENABLE - This field will enable PMTU
2047 * discovery upon the specified address. Note that
2048 * if the address feild is empty then all addresses
2049 * on the association are effected.
2050 *
2051 * SPP_PMTUD_DISABLE - This field will disable PMTU
2052 * discovery upon the specified address. Note that
2053 * if the address feild is empty then all addresses
2054 * on the association are effected. Not also that
2055 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2056 * exclusive. Enabling both will have undetermined
2057 * results.
2058 *
2059 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2060 * on delayed sack. The time specified in spp_sackdelay
2061 * is used to specify the sack delay for this address. Note
2062 * that if spp_address is empty then all addresses will
2063 * enable delayed sack and take on the sack delay
2064 * value specified in spp_sackdelay.
2065 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2066 * off delayed sack. If the spp_address field is blank then
2067 * delayed sack is disabled for the entire association. Note
2068 * also that this field is mutually exclusive to
2069 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2070 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Adrian Bunk16164362006-09-18 00:40:38 -07002072static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2073 struct sctp_transport *trans,
2074 struct sctp_association *asoc,
2075 struct sctp_sock *sp,
2076 int hb_change,
2077 int pmtud_change,
2078 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int error;
2081
Frank Filz52ccb8e2005-12-22 11:36:46 -08002082 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2084 if (error)
2085 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002088 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2089 * this field is ignored. Note also that a value of zero indicates
2090 * the current setting should be left unchanged.
2091 */
2092 if (params->spp_flags & SPP_HB_ENABLE) {
2093
2094 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2095 * set. This lets us use 0 value when this flag
2096 * is set.
2097 */
2098 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2099 params->spp_hbinterval = 0;
2100
2101 if (params->spp_hbinterval ||
2102 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2103 if (trans) {
2104 trans->hbinterval =
2105 msecs_to_jiffies(params->spp_hbinterval);
2106 } else if (asoc) {
2107 asoc->hbinterval =
2108 msecs_to_jiffies(params->spp_hbinterval);
2109 } else {
2110 sp->hbinterval = params->spp_hbinterval;
2111 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002112 }
2113 }
2114
2115 if (hb_change) {
2116 if (trans) {
2117 trans->param_flags =
2118 (trans->param_flags & ~SPP_HB) | hb_change;
2119 } else if (asoc) {
2120 asoc->param_flags =
2121 (asoc->param_flags & ~SPP_HB) | hb_change;
2122 } else {
2123 sp->param_flags =
2124 (sp->param_flags & ~SPP_HB) | hb_change;
2125 }
2126 }
2127
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002128 /* When Path MTU discovery is disabled the value specified here will
2129 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2130 * include the flag SPP_PMTUD_DISABLE for this field to have any
2131 * effect).
2132 */
2133 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002134 if (trans) {
2135 trans->pathmtu = params->spp_pathmtu;
2136 sctp_assoc_sync_pmtu(asoc);
2137 } else if (asoc) {
2138 asoc->pathmtu = params->spp_pathmtu;
2139 sctp_frag_point(sp, params->spp_pathmtu);
2140 } else {
2141 sp->pathmtu = params->spp_pathmtu;
2142 }
2143 }
2144
2145 if (pmtud_change) {
2146 if (trans) {
2147 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2148 (params->spp_flags & SPP_PMTUD_ENABLE);
2149 trans->param_flags =
2150 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2151 if (update) {
2152 sctp_transport_pmtu(trans);
2153 sctp_assoc_sync_pmtu(asoc);
2154 }
2155 } else if (asoc) {
2156 asoc->param_flags =
2157 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2158 } else {
2159 sp->param_flags =
2160 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2161 }
2162 }
2163
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002164 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2165 * value of this field is ignored. Note also that a value of zero
2166 * indicates the current setting should be left unchanged.
2167 */
2168 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002169 if (trans) {
2170 trans->sackdelay =
2171 msecs_to_jiffies(params->spp_sackdelay);
2172 } else if (asoc) {
2173 asoc->sackdelay =
2174 msecs_to_jiffies(params->spp_sackdelay);
2175 } else {
2176 sp->sackdelay = params->spp_sackdelay;
2177 }
2178 }
2179
2180 if (sackdelay_change) {
2181 if (trans) {
2182 trans->param_flags =
2183 (trans->param_flags & ~SPP_SACKDELAY) |
2184 sackdelay_change;
2185 } else if (asoc) {
2186 asoc->param_flags =
2187 (asoc->param_flags & ~SPP_SACKDELAY) |
2188 sackdelay_change;
2189 } else {
2190 sp->param_flags =
2191 (sp->param_flags & ~SPP_SACKDELAY) |
2192 sackdelay_change;
2193 }
2194 }
2195
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002196 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2197 * of this field is ignored. Note also that a value of zero
2198 * indicates the current setting should be left unchanged.
2199 */
2200 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002201 if (trans) {
2202 trans->pathmaxrxt = params->spp_pathmaxrxt;
2203 } else if (asoc) {
2204 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2205 } else {
2206 sp->pathmaxrxt = params->spp_pathmaxrxt;
2207 }
2208 }
2209
2210 return 0;
2211}
2212
2213static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2214 char __user *optval, int optlen)
2215{
2216 struct sctp_paddrparams params;
2217 struct sctp_transport *trans = NULL;
2218 struct sctp_association *asoc = NULL;
2219 struct sctp_sock *sp = sctp_sk(sk);
2220 int error;
2221 int hb_change, pmtud_change, sackdelay_change;
2222
2223 if (optlen != sizeof(struct sctp_paddrparams))
2224 return - EINVAL;
2225
2226 if (copy_from_user(&params, optval, optlen))
2227 return -EFAULT;
2228
2229 /* Validate flags and value parameters. */
2230 hb_change = params.spp_flags & SPP_HB;
2231 pmtud_change = params.spp_flags & SPP_PMTUD;
2232 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2233
2234 if (hb_change == SPP_HB ||
2235 pmtud_change == SPP_PMTUD ||
2236 sackdelay_change == SPP_SACKDELAY ||
2237 params.spp_sackdelay > 500 ||
2238 (params.spp_pathmtu
2239 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2240 return -EINVAL;
2241
2242 /* If an address other than INADDR_ANY is specified, and
2243 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002245 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2246 trans = sctp_addr_id2transport(sk, &params.spp_address,
2247 params.spp_assoc_id);
2248 if (!trans)
2249 return -EINVAL;
2250 }
2251
2252 /* Get association, if assoc_id != 0 and the socket is a one
2253 * to many style socket, and an association was not found, then
2254 * the id was invalid.
2255 */
2256 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2257 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2258 return -EINVAL;
2259
2260 /* Heartbeat demand can only be sent on a transport or
2261 * association, but not a socket.
2262 */
2263 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2264 return -EINVAL;
2265
2266 /* Process parameters. */
2267 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2268 hb_change, pmtud_change,
2269 sackdelay_change);
2270
2271 if (error)
2272 return error;
2273
2274 /* If changes are for association, also apply parameters to each
2275 * transport.
2276 */
2277 if (!trans && asoc) {
2278 struct list_head *pos;
2279
2280 list_for_each(pos, &asoc->peer.transport_addr_list) {
2281 trans = list_entry(pos, struct sctp_transport,
2282 transports);
2283 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2284 hb_change, pmtud_change,
2285 sackdelay_change);
2286 }
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 return 0;
2290}
2291
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002292/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08002293 *
2294 * This options will get or set the delayed ack timer. The time is set
2295 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2296 * endpoints default delayed ack timer value. If the assoc_id field is
2297 * non-zero, then the set or get effects the specified association.
2298 *
2299 * struct sctp_assoc_value {
2300 * sctp_assoc_t assoc_id;
2301 * uint32_t assoc_value;
2302 * };
2303 *
2304 * assoc_id - This parameter, indicates which association the
2305 * user is preforming an action upon. Note that if
2306 * this field's value is zero then the endpoints
2307 * default value is changed (effecting future
2308 * associations only).
2309 *
2310 * assoc_value - This parameter contains the number of milliseconds
2311 * that the user is requesting the delayed ACK timer
2312 * be set to. Note that this value is defined in
2313 * the standard to be between 200 and 500 milliseconds.
2314 *
2315 * Note: a value of zero will leave the value alone,
2316 * but disable SACK delay. A non-zero value will also
2317 * enable SACK delay.
2318 */
2319
2320static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2321 char __user *optval, int optlen)
2322{
2323 struct sctp_assoc_value params;
2324 struct sctp_transport *trans = NULL;
2325 struct sctp_association *asoc = NULL;
2326 struct sctp_sock *sp = sctp_sk(sk);
2327
2328 if (optlen != sizeof(struct sctp_assoc_value))
2329 return - EINVAL;
2330
2331 if (copy_from_user(&params, optval, optlen))
2332 return -EFAULT;
2333
2334 /* Validate value parameter. */
2335 if (params.assoc_value > 500)
2336 return -EINVAL;
2337
2338 /* Get association, if assoc_id != 0 and the socket is a one
2339 * to many style socket, and an association was not found, then
2340 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002341 */
Frank Filz77086102005-12-22 11:37:30 -08002342 asoc = sctp_id2assoc(sk, params.assoc_id);
2343 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2344 return -EINVAL;
2345
2346 if (params.assoc_value) {
2347 if (asoc) {
2348 asoc->sackdelay =
2349 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002350 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002351 (asoc->param_flags & ~SPP_SACKDELAY) |
2352 SPP_SACKDELAY_ENABLE;
2353 } else {
2354 sp->sackdelay = params.assoc_value;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002355 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002356 (sp->param_flags & ~SPP_SACKDELAY) |
2357 SPP_SACKDELAY_ENABLE;
2358 }
2359 } else {
2360 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002361 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002362 (asoc->param_flags & ~SPP_SACKDELAY) |
2363 SPP_SACKDELAY_DISABLE;
2364 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002365 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002366 (sp->param_flags & ~SPP_SACKDELAY) |
2367 SPP_SACKDELAY_DISABLE;
2368 }
2369 }
2370
2371 /* If change is for association, also apply to each transport. */
2372 if (asoc) {
2373 struct list_head *pos;
2374
2375 list_for_each(pos, &asoc->peer.transport_addr_list) {
2376 trans = list_entry(pos, struct sctp_transport,
2377 transports);
2378 if (params.assoc_value) {
2379 trans->sackdelay =
2380 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002381 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002382 (trans->param_flags & ~SPP_SACKDELAY) |
2383 SPP_SACKDELAY_ENABLE;
2384 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002385 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002386 (trans->param_flags & ~SPP_SACKDELAY) |
2387 SPP_SACKDELAY_DISABLE;
2388 }
2389 }
2390 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002391
Frank Filz77086102005-12-22 11:37:30 -08002392 return 0;
2393}
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2396 *
2397 * Applications can specify protocol parameters for the default association
2398 * initialization. The option name argument to setsockopt() and getsockopt()
2399 * is SCTP_INITMSG.
2400 *
2401 * Setting initialization parameters is effective only on an unconnected
2402 * socket (for UDP-style sockets only future associations are effected
2403 * by the change). With TCP-style sockets, this option is inherited by
2404 * sockets derived from a listener socket.
2405 */
2406static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2407{
2408 struct sctp_initmsg sinit;
2409 struct sctp_sock *sp = sctp_sk(sk);
2410
2411 if (optlen != sizeof(struct sctp_initmsg))
2412 return -EINVAL;
2413 if (copy_from_user(&sinit, optval, optlen))
2414 return -EFAULT;
2415
2416 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002417 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002419 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002421 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002423 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 return 0;
2426}
2427
2428/*
2429 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2430 *
2431 * Applications that wish to use the sendto() system call may wish to
2432 * specify a default set of parameters that would normally be supplied
2433 * through the inclusion of ancillary data. This socket option allows
2434 * such an application to set the default sctp_sndrcvinfo structure.
2435 * The application that wishes to use this socket option simply passes
2436 * in to this call the sctp_sndrcvinfo structure defined in Section
2437 * 5.2.2) The input parameters accepted by this call include
2438 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2439 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2440 * to this call if the caller is using the UDP model.
2441 */
2442static int sctp_setsockopt_default_send_param(struct sock *sk,
2443 char __user *optval, int optlen)
2444{
2445 struct sctp_sndrcvinfo info;
2446 struct sctp_association *asoc;
2447 struct sctp_sock *sp = sctp_sk(sk);
2448
2449 if (optlen != sizeof(struct sctp_sndrcvinfo))
2450 return -EINVAL;
2451 if (copy_from_user(&info, optval, optlen))
2452 return -EFAULT;
2453
2454 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2455 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2456 return -EINVAL;
2457
2458 if (asoc) {
2459 asoc->default_stream = info.sinfo_stream;
2460 asoc->default_flags = info.sinfo_flags;
2461 asoc->default_ppid = info.sinfo_ppid;
2462 asoc->default_context = info.sinfo_context;
2463 asoc->default_timetolive = info.sinfo_timetolive;
2464 } else {
2465 sp->default_stream = info.sinfo_stream;
2466 sp->default_flags = info.sinfo_flags;
2467 sp->default_ppid = info.sinfo_ppid;
2468 sp->default_context = info.sinfo_context;
2469 sp->default_timetolive = info.sinfo_timetolive;
2470 }
2471
2472 return 0;
2473}
2474
2475/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2476 *
2477 * Requests that the local SCTP stack use the enclosed peer address as
2478 * the association primary. The enclosed address must be one of the
2479 * association peer's addresses.
2480 */
2481static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2482 int optlen)
2483{
2484 struct sctp_prim prim;
2485 struct sctp_transport *trans;
2486
2487 if (optlen != sizeof(struct sctp_prim))
2488 return -EINVAL;
2489
2490 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2491 return -EFAULT;
2492
2493 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2494 if (!trans)
2495 return -EINVAL;
2496
2497 sctp_assoc_set_primary(trans->asoc, trans);
2498
2499 return 0;
2500}
2501
2502/*
2503 * 7.1.5 SCTP_NODELAY
2504 *
2505 * Turn on/off any Nagle-like algorithm. This means that packets are
2506 * generally sent as soon as possible and no unnecessary delays are
2507 * introduced, at the cost of more packets in the network. Expects an
2508 * integer boolean flag.
2509 */
2510static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2511 int optlen)
2512{
2513 int val;
2514
2515 if (optlen < sizeof(int))
2516 return -EINVAL;
2517 if (get_user(val, (int __user *)optval))
2518 return -EFAULT;
2519
2520 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2521 return 0;
2522}
2523
2524/*
2525 *
2526 * 7.1.1 SCTP_RTOINFO
2527 *
2528 * The protocol parameters used to initialize and bound retransmission
2529 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2530 * and modify these parameters.
2531 * All parameters are time values, in milliseconds. A value of 0, when
2532 * modifying the parameters, indicates that the current value should not
2533 * be changed.
2534 *
2535 */
2536static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2537 struct sctp_rtoinfo rtoinfo;
2538 struct sctp_association *asoc;
2539
2540 if (optlen != sizeof (struct sctp_rtoinfo))
2541 return -EINVAL;
2542
2543 if (copy_from_user(&rtoinfo, optval, optlen))
2544 return -EFAULT;
2545
2546 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2547
2548 /* Set the values to the specific association */
2549 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2550 return -EINVAL;
2551
2552 if (asoc) {
2553 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002554 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 msecs_to_jiffies(rtoinfo.srto_initial);
2556 if (rtoinfo.srto_max != 0)
2557 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2558 if (rtoinfo.srto_min != 0)
2559 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2560 } else {
2561 /* If there is no association or the association-id = 0
2562 * set the values to the endpoint.
2563 */
2564 struct sctp_sock *sp = sctp_sk(sk);
2565
2566 if (rtoinfo.srto_initial != 0)
2567 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2568 if (rtoinfo.srto_max != 0)
2569 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2570 if (rtoinfo.srto_min != 0)
2571 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2572 }
2573
2574 return 0;
2575}
2576
2577/*
2578 *
2579 * 7.1.2 SCTP_ASSOCINFO
2580 *
2581 * This option is used to tune the the maximum retransmission attempts
2582 * of the association.
2583 * Returns an error if the new association retransmission value is
2584 * greater than the sum of the retransmission value of the peer.
2585 * See [SCTP] for more information.
2586 *
2587 */
2588static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2589{
2590
2591 struct sctp_assocparams assocparams;
2592 struct sctp_association *asoc;
2593
2594 if (optlen != sizeof(struct sctp_assocparams))
2595 return -EINVAL;
2596 if (copy_from_user(&assocparams, optval, optlen))
2597 return -EFAULT;
2598
2599 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2600
2601 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2602 return -EINVAL;
2603
2604 /* Set the values to the specific association */
2605 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002606 if (assocparams.sasoc_asocmaxrxt != 0) {
2607 __u32 path_sum = 0;
2608 int paths = 0;
2609 struct list_head *pos;
2610 struct sctp_transport *peer_addr;
2611
2612 list_for_each(pos, &asoc->peer.transport_addr_list) {
2613 peer_addr = list_entry(pos,
2614 struct sctp_transport,
2615 transports);
2616 path_sum += peer_addr->pathmaxrxt;
2617 paths++;
2618 }
2619
2620 /* Only validate asocmaxrxt if we have more then
2621 * one path/transport. We do this because path
2622 * retransmissions are only counted when we have more
2623 * then one path.
2624 */
2625 if (paths > 1 &&
2626 assocparams.sasoc_asocmaxrxt > path_sum)
2627 return -EINVAL;
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002630 }
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (assocparams.sasoc_cookie_life != 0) {
2633 asoc->cookie_life.tv_sec =
2634 assocparams.sasoc_cookie_life / 1000;
2635 asoc->cookie_life.tv_usec =
2636 (assocparams.sasoc_cookie_life % 1000)
2637 * 1000;
2638 }
2639 } else {
2640 /* Set the values to the endpoint */
2641 struct sctp_sock *sp = sctp_sk(sk);
2642
2643 if (assocparams.sasoc_asocmaxrxt != 0)
2644 sp->assocparams.sasoc_asocmaxrxt =
2645 assocparams.sasoc_asocmaxrxt;
2646 if (assocparams.sasoc_cookie_life != 0)
2647 sp->assocparams.sasoc_cookie_life =
2648 assocparams.sasoc_cookie_life;
2649 }
2650 return 0;
2651}
2652
2653/*
2654 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2655 *
2656 * This socket option is a boolean flag which turns on or off mapped V4
2657 * addresses. If this option is turned on and the socket is type
2658 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2659 * If this option is turned off, then no mapping will be done of V4
2660 * addresses and a user will receive both PF_INET6 and PF_INET type
2661 * addresses on the socket.
2662 */
2663static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2664{
2665 int val;
2666 struct sctp_sock *sp = sctp_sk(sk);
2667
2668 if (optlen < sizeof(int))
2669 return -EINVAL;
2670 if (get_user(val, (int __user *)optval))
2671 return -EFAULT;
2672 if (val)
2673 sp->v4mapped = 1;
2674 else
2675 sp->v4mapped = 0;
2676
2677 return 0;
2678}
2679
2680/*
2681 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2682 *
2683 * This socket option specifies the maximum size to put in any outgoing
2684 * SCTP chunk. If a message is larger than this size it will be
2685 * fragmented by SCTP into the specified size. Note that the underlying
2686 * SCTP implementation may fragment into smaller sized chunks when the
2687 * PMTU of the underlying association is smaller than the value set by
2688 * the user.
2689 */
2690static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2691{
2692 struct sctp_association *asoc;
2693 struct list_head *pos;
2694 struct sctp_sock *sp = sctp_sk(sk);
2695 int val;
2696
2697 if (optlen < sizeof(int))
2698 return -EINVAL;
2699 if (get_user(val, (int __user *)optval))
2700 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002701 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return -EINVAL;
2703 sp->user_frag = val;
2704
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002705 /* Update the frag_point of the existing associations. */
2706 list_for_each(pos, &(sp->ep->asocs)) {
2707 asoc = list_entry(pos, struct sctp_association, asocs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002708 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710
2711 return 0;
2712}
2713
2714
2715/*
2716 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2717 *
2718 * Requests that the peer mark the enclosed address as the association
2719 * primary. The enclosed address must be one of the association's
2720 * locally bound addresses. The following structure is used to make a
2721 * set primary request:
2722 */
2723static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2724 int optlen)
2725{
2726 struct sctp_sock *sp;
2727 struct sctp_endpoint *ep;
2728 struct sctp_association *asoc = NULL;
2729 struct sctp_setpeerprim prim;
2730 struct sctp_chunk *chunk;
2731 int err;
2732
2733 sp = sctp_sk(sk);
2734 ep = sp->ep;
2735
2736 if (!sctp_addip_enable)
2737 return -EPERM;
2738
2739 if (optlen != sizeof(struct sctp_setpeerprim))
2740 return -EINVAL;
2741
2742 if (copy_from_user(&prim, optval, optlen))
2743 return -EFAULT;
2744
2745 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002746 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return -EINVAL;
2748
2749 if (!asoc->peer.asconf_capable)
2750 return -EPERM;
2751
2752 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2753 return -EPERM;
2754
2755 if (!sctp_state(asoc, ESTABLISHED))
2756 return -ENOTCONN;
2757
2758 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2759 return -EADDRNOTAVAIL;
2760
2761 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2762 chunk = sctp_make_asconf_set_prim(asoc,
2763 (union sctp_addr *)&prim.sspp_addr);
2764 if (!chunk)
2765 return -ENOMEM;
2766
2767 err = sctp_send_asconf(asoc, chunk);
2768
2769 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2770
2771 return err;
2772}
2773
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002774static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 int optlen)
2776{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002777 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002779 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002781 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return -EFAULT;
2783
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002784 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 return 0;
2787}
2788
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08002789/*
2790 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2791 *
2792 * The context field in the sctp_sndrcvinfo structure is normally only
2793 * used when a failed message is retrieved holding the value that was
2794 * sent down on the actual send call. This option allows the setting of
2795 * a default context on an association basis that will be received on
2796 * reading messages from the peer. This is especially helpful in the
2797 * one-2-many model for an application to keep some reference to an
2798 * internal state machine that is processing messages on the
2799 * association. Note that the setting of this value only effects
2800 * received messages from the peer and does not effect the value that is
2801 * saved with outbound messages.
2802 */
2803static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2804 int optlen)
2805{
2806 struct sctp_assoc_value params;
2807 struct sctp_sock *sp;
2808 struct sctp_association *asoc;
2809
2810 if (optlen != sizeof(struct sctp_assoc_value))
2811 return -EINVAL;
2812 if (copy_from_user(&params, optval, optlen))
2813 return -EFAULT;
2814
2815 sp = sctp_sk(sk);
2816
2817 if (params.assoc_id != 0) {
2818 asoc = sctp_id2assoc(sk, params.assoc_id);
2819 if (!asoc)
2820 return -EINVAL;
2821 asoc->default_rcv_context = params.assoc_value;
2822 } else {
2823 sp->default_rcv_context = params.assoc_value;
2824 }
2825
2826 return 0;
2827}
2828
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002829/*
2830 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2831 *
2832 * This options will at a minimum specify if the implementation is doing
2833 * fragmented interleave. Fragmented interleave, for a one to many
2834 * socket, is when subsequent calls to receive a message may return
2835 * parts of messages from different associations. Some implementations
2836 * may allow you to turn this value on or off. If so, when turned off,
2837 * no fragment interleave will occur (which will cause a head of line
2838 * blocking amongst multiple associations sharing the same one to many
2839 * socket). When this option is turned on, then each receive call may
2840 * come from a different association (thus the user must receive data
2841 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2842 * association each receive belongs to.
2843 *
2844 * This option takes a boolean value. A non-zero value indicates that
2845 * fragmented interleave is on. A value of zero indicates that
2846 * fragmented interleave is off.
2847 *
2848 * Note that it is important that an implementation that allows this
2849 * option to be turned on, have it off by default. Otherwise an unaware
2850 * application using the one to many model may become confused and act
2851 * incorrectly.
2852 */
2853static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2854 char __user *optval,
2855 int optlen)
2856{
2857 int val;
2858
2859 if (optlen != sizeof(int))
2860 return -EINVAL;
2861 if (get_user(val, (int __user *)optval))
2862 return -EFAULT;
2863
2864 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2865
2866 return 0;
2867}
2868
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07002869/*
2870 * 7.1.25. Set or Get the sctp partial delivery point
2871 * (SCTP_PARTIAL_DELIVERY_POINT)
2872 * This option will set or get the SCTP partial delivery point. This
2873 * point is the size of a message where the partial delivery API will be
2874 * invoked to help free up rwnd space for the peer. Setting this to a
2875 * lower value will cause partial delivery's to happen more often. The
2876 * calls argument is an integer that sets or gets the partial delivery
2877 * point.
2878 */
2879static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2880 char __user *optval,
2881 int optlen)
2882{
2883 u32 val;
2884
2885 if (optlen != sizeof(u32))
2886 return -EINVAL;
2887 if (get_user(val, (int __user *)optval))
2888 return -EFAULT;
2889
2890 sctp_sk(sk)->pd_point = val;
2891
2892 return 0; /* is this the right error code? */
2893}
2894
Vlad Yasevich70331572007-03-23 11:34:36 -07002895/*
2896 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2897 *
2898 * This option will allow a user to change the maximum burst of packets
2899 * that can be emitted by this association. Note that the default value
2900 * is 4, and some implementations may restrict this setting so that it
2901 * can only be lowered.
2902 *
2903 * NOTE: This text doesn't seem right. Do this on a socket basis with
2904 * future associations inheriting the socket value.
2905 */
2906static int sctp_setsockopt_maxburst(struct sock *sk,
2907 char __user *optval,
2908 int optlen)
2909{
2910 int val;
2911
2912 if (optlen != sizeof(int))
2913 return -EINVAL;
2914 if (get_user(val, (int __user *)optval))
2915 return -EFAULT;
2916
2917 if (val < 0)
2918 return -EINVAL;
2919
2920 sctp_sk(sk)->max_burst = val;
2921
2922 return 0;
2923}
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925/* API 6.2 setsockopt(), getsockopt()
2926 *
2927 * Applications use setsockopt() and getsockopt() to set or retrieve
2928 * socket options. Socket options are used to change the default
2929 * behavior of sockets calls. They are described in Section 7.
2930 *
2931 * The syntax is:
2932 *
2933 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
2934 * int __user *optlen);
2935 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2936 * int optlen);
2937 *
2938 * sd - the socket descript.
2939 * level - set to IPPROTO_SCTP for all SCTP options.
2940 * optname - the option name.
2941 * optval - the buffer to store the value of the option.
2942 * optlen - the size of the buffer.
2943 */
2944SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2945 char __user *optval, int optlen)
2946{
2947 int retval = 0;
2948
2949 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2950 sk, optname);
2951
2952 /* I can hardly begin to describe how wrong this is. This is
2953 * so broken as to be worse than useless. The API draft
2954 * REALLY is NOT helpful here... I am not convinced that the
2955 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2956 * are at all well-founded.
2957 */
2958 if (level != SOL_SCTP) {
2959 struct sctp_af *af = sctp_sk(sk)->pf->af;
2960 retval = af->setsockopt(sk, level, optname, optval, optlen);
2961 goto out_nounlock;
2962 }
2963
2964 sctp_lock_sock(sk);
2965
2966 switch (optname) {
2967 case SCTP_SOCKOPT_BINDX_ADD:
2968 /* 'optlen' is the size of the addresses buffer. */
2969 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2970 optlen, SCTP_BINDX_ADD_ADDR);
2971 break;
2972
2973 case SCTP_SOCKOPT_BINDX_REM:
2974 /* 'optlen' is the size of the addresses buffer. */
2975 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2976 optlen, SCTP_BINDX_REM_ADDR);
2977 break;
2978
Frank Filz3f7a87d2005-06-20 13:14:57 -07002979 case SCTP_SOCKOPT_CONNECTX:
2980 /* 'optlen' is the size of the addresses buffer. */
2981 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2982 optlen);
2983 break;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 case SCTP_DISABLE_FRAGMENTS:
2986 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2987 break;
2988
2989 case SCTP_EVENTS:
2990 retval = sctp_setsockopt_events(sk, optval, optlen);
2991 break;
2992
2993 case SCTP_AUTOCLOSE:
2994 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2995 break;
2996
2997 case SCTP_PEER_ADDR_PARAMS:
2998 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2999 break;
3000
Frank Filz77086102005-12-22 11:37:30 -08003001 case SCTP_DELAYED_ACK_TIME:
3002 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3003 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003004 case SCTP_PARTIAL_DELIVERY_POINT:
3005 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3006 break;
Frank Filz77086102005-12-22 11:37:30 -08003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 case SCTP_INITMSG:
3009 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3010 break;
3011 case SCTP_DEFAULT_SEND_PARAM:
3012 retval = sctp_setsockopt_default_send_param(sk, optval,
3013 optlen);
3014 break;
3015 case SCTP_PRIMARY_ADDR:
3016 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3017 break;
3018 case SCTP_SET_PEER_PRIMARY_ADDR:
3019 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3020 break;
3021 case SCTP_NODELAY:
3022 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3023 break;
3024 case SCTP_RTOINFO:
3025 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3026 break;
3027 case SCTP_ASSOCINFO:
3028 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3029 break;
3030 case SCTP_I_WANT_MAPPED_V4_ADDR:
3031 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3032 break;
3033 case SCTP_MAXSEG:
3034 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3035 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003036 case SCTP_ADAPTATION_LAYER:
3037 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003039 case SCTP_CONTEXT:
3040 retval = sctp_setsockopt_context(sk, optval, optlen);
3041 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003042 case SCTP_FRAGMENT_INTERLEAVE:
3043 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3044 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003045 case SCTP_MAX_BURST:
3046 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 default:
3049 retval = -ENOPROTOOPT;
3050 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 sctp_release_sock(sk);
3054
3055out_nounlock:
3056 return retval;
3057}
3058
3059/* API 3.1.6 connect() - UDP Style Syntax
3060 *
3061 * An application may use the connect() call in the UDP model to initiate an
3062 * association without sending data.
3063 *
3064 * The syntax is:
3065 *
3066 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3067 *
3068 * sd: the socket descriptor to have a new association added to.
3069 *
3070 * nam: the address structure (either struct sockaddr_in or struct
3071 * sockaddr_in6 defined in RFC2553 [7]).
3072 *
3073 * len: the size of the address.
3074 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003075SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 int addr_len)
3077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003079 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081 sctp_lock_sock(sk);
3082
Frank Filz3f7a87d2005-06-20 13:14:57 -07003083 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3084 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Frank Filz3f7a87d2005-06-20 13:14:57 -07003086 /* Validate addr_len before calling common connect/connectx routine. */
3087 af = sctp_get_af_specific(addr->sa_family);
3088 if (!af || addr_len < af->sockaddr_len) {
3089 err = -EINVAL;
3090 } else {
3091 /* Pass correct addr len to common routine (so it knows there
3092 * is only one address being passed.
3093 */
3094 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return err;
3099}
3100
3101/* FIXME: Write comments. */
3102SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3103{
3104 return -EOPNOTSUPP; /* STUB */
3105}
3106
3107/* 4.1.4 accept() - TCP Style Syntax
3108 *
3109 * Applications use accept() call to remove an established SCTP
3110 * association from the accept queue of the endpoint. A new socket
3111 * descriptor will be returned from accept() to represent the newly
3112 * formed association.
3113 */
3114SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3115{
3116 struct sctp_sock *sp;
3117 struct sctp_endpoint *ep;
3118 struct sock *newsk = NULL;
3119 struct sctp_association *asoc;
3120 long timeo;
3121 int error = 0;
3122
3123 sctp_lock_sock(sk);
3124
3125 sp = sctp_sk(sk);
3126 ep = sp->ep;
3127
3128 if (!sctp_style(sk, TCP)) {
3129 error = -EOPNOTSUPP;
3130 goto out;
3131 }
3132
3133 if (!sctp_sstate(sk, LISTENING)) {
3134 error = -EINVAL;
3135 goto out;
3136 }
3137
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003138 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 error = sctp_wait_for_accept(sk, timeo);
3141 if (error)
3142 goto out;
3143
3144 /* We treat the list of associations on the endpoint as the accept
3145 * queue and pick the first association on the list.
3146 */
3147 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3148
3149 newsk = sp->pf->create_accept_sk(sk, asoc);
3150 if (!newsk) {
3151 error = -ENOMEM;
3152 goto out;
3153 }
3154
3155 /* Populate the fields of the newsk from the oldsk and migrate the
3156 * asoc to the newsk.
3157 */
3158 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3159
3160out:
3161 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003162 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return newsk;
3164}
3165
3166/* The SCTP ioctl handler. */
3167SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3168{
3169 return -ENOIOCTLCMD;
3170}
3171
3172/* This is the function which gets called during socket creation to
3173 * initialized the SCTP-specific portion of the sock.
3174 * The sock structure should already be zero-filled memory.
3175 */
3176SCTP_STATIC int sctp_init_sock(struct sock *sk)
3177{
3178 struct sctp_endpoint *ep;
3179 struct sctp_sock *sp;
3180
3181 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3182
3183 sp = sctp_sk(sk);
3184
3185 /* Initialize the SCTP per socket area. */
3186 switch (sk->sk_type) {
3187 case SOCK_SEQPACKET:
3188 sp->type = SCTP_SOCKET_UDP;
3189 break;
3190 case SOCK_STREAM:
3191 sp->type = SCTP_SOCKET_TCP;
3192 break;
3193 default:
3194 return -ESOCKTNOSUPPORT;
3195 }
3196
3197 /* Initialize default send parameters. These parameters can be
3198 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3199 */
3200 sp->default_stream = 0;
3201 sp->default_ppid = 0;
3202 sp->default_flags = 0;
3203 sp->default_context = 0;
3204 sp->default_timetolive = 0;
3205
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003206 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003207 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 /* Initialize default setup parameters. These parameters
3210 * can be modified with the SCTP_INITMSG socket option or
3211 * overridden by the SCTP_INIT CMSG.
3212 */
3213 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3214 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3215 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003216 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 /* Initialize default RTO related parameters. These parameters can
3219 * be modified for with the SCTP_RTOINFO socket option.
3220 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003221 sp->rtoinfo.srto_initial = sctp_rto_initial;
3222 sp->rtoinfo.srto_max = sctp_rto_max;
3223 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 /* Initialize default association related parameters. These parameters
3226 * can be modified with the SCTP_ASSOCINFO socket option.
3227 */
3228 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3229 sp->assocparams.sasoc_number_peer_destinations = 0;
3230 sp->assocparams.sasoc_peer_rwnd = 0;
3231 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003232 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003235 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 */
3237 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3238
3239 /* Default Peer Address Parameters. These defaults can
3240 * be modified via SCTP_PEER_ADDR_PARAMS
3241 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003242 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003243 sp->pathmaxrxt = sctp_max_retrans_path;
3244 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003245 sp->sackdelay = sctp_sack_timeout;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003246 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003247 SPP_PMTUD_ENABLE |
3248 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /* If enabled no SCTP message fragmentation will be performed.
3251 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3252 */
3253 sp->disable_fragments = 0;
3254
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003255 /* Enable Nagle algorithm by default. */
3256 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 /* Enable by default. */
3259 sp->v4mapped = 1;
3260
3261 /* Auto-close idle associations after the configured
3262 * number of seconds. A value of 0 disables this
3263 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3264 * for UDP-style sockets only.
3265 */
3266 sp->autoclose = 0;
3267
3268 /* User specified fragmentation limit. */
3269 sp->user_frag = 0;
3270
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003271 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 sp->pf = sctp_get_pf_specific(sk->sk_family);
3274
3275 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003276 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003278 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 /* Create a per socket endpoint structure. Even if we
3281 * change the data structure relationships, this may still
3282 * be useful for storing pre-connect address information.
3283 */
3284 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3285 if (!ep)
3286 return -ENOMEM;
3287
3288 sp->ep = ep;
3289 sp->hmac = NULL;
3290
3291 SCTP_DBG_OBJCNT_INC(sock);
3292 return 0;
3293}
3294
3295/* Cleanup any SCTP per socket resources. */
3296SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3297{
3298 struct sctp_endpoint *ep;
3299
3300 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3301
3302 /* Release our hold on the endpoint. */
3303 ep = sctp_sk(sk)->ep;
3304 sctp_endpoint_free(ep);
3305
3306 return 0;
3307}
3308
3309/* API 4.1.7 shutdown() - TCP Style Syntax
3310 * int shutdown(int socket, int how);
3311 *
3312 * sd - the socket descriptor of the association to be closed.
3313 * how - Specifies the type of shutdown. The values are
3314 * as follows:
3315 * SHUT_RD
3316 * Disables further receive operations. No SCTP
3317 * protocol action is taken.
3318 * SHUT_WR
3319 * Disables further send operations, and initiates
3320 * the SCTP shutdown sequence.
3321 * SHUT_RDWR
3322 * Disables further send and receive operations
3323 * and initiates the SCTP shutdown sequence.
3324 */
3325SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3326{
3327 struct sctp_endpoint *ep;
3328 struct sctp_association *asoc;
3329
3330 if (!sctp_style(sk, TCP))
3331 return;
3332
3333 if (how & SEND_SHUTDOWN) {
3334 ep = sctp_sk(sk)->ep;
3335 if (!list_empty(&ep->asocs)) {
3336 asoc = list_entry(ep->asocs.next,
3337 struct sctp_association, asocs);
3338 sctp_primitive_SHUTDOWN(asoc, NULL);
3339 }
3340 }
3341}
3342
3343/* 7.2.1 Association Status (SCTP_STATUS)
3344
3345 * Applications can retrieve current status information about an
3346 * association, including association state, peer receiver window size,
3347 * number of unacked data chunks, and number of data chunks pending
3348 * receipt. This information is read-only.
3349 */
3350static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3351 char __user *optval,
3352 int __user *optlen)
3353{
3354 struct sctp_status status;
3355 struct sctp_association *asoc = NULL;
3356 struct sctp_transport *transport;
3357 sctp_assoc_t associd;
3358 int retval = 0;
3359
3360 if (len != sizeof(status)) {
3361 retval = -EINVAL;
3362 goto out;
3363 }
3364
3365 if (copy_from_user(&status, optval, sizeof(status))) {
3366 retval = -EFAULT;
3367 goto out;
3368 }
3369
3370 associd = status.sstat_assoc_id;
3371 asoc = sctp_id2assoc(sk, associd);
3372 if (!asoc) {
3373 retval = -EINVAL;
3374 goto out;
3375 }
3376
3377 transport = asoc->peer.primary_path;
3378
3379 status.sstat_assoc_id = sctp_assoc2id(asoc);
3380 status.sstat_state = asoc->state;
3381 status.sstat_rwnd = asoc->peer.rwnd;
3382 status.sstat_unackdata = asoc->unack_data;
3383
3384 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3385 status.sstat_instrms = asoc->c.sinit_max_instreams;
3386 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3387 status.sstat_fragmentation_point = asoc->frag_point;
3388 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08003389 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3390 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 /* Map ipv4 address into v4-mapped-on-v6 address. */
3392 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3393 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003394 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3396 status.sstat_primary.spinfo_srtt = transport->srtt;
3397 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003398 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Frank Filz3f7a87d2005-06-20 13:14:57 -07003400 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3401 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (put_user(len, optlen)) {
3404 retval = -EFAULT;
3405 goto out;
3406 }
3407
3408 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3409 len, status.sstat_state, status.sstat_rwnd,
3410 status.sstat_assoc_id);
3411
3412 if (copy_to_user(optval, &status, len)) {
3413 retval = -EFAULT;
3414 goto out;
3415 }
3416
3417out:
3418 return (retval);
3419}
3420
3421
3422/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3423 *
3424 * Applications can retrieve information about a specific peer address
3425 * of an association, including its reachability state, congestion
3426 * window, and retransmission timer values. This information is
3427 * read-only.
3428 */
3429static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3430 char __user *optval,
3431 int __user *optlen)
3432{
3433 struct sctp_paddrinfo pinfo;
3434 struct sctp_transport *transport;
3435 int retval = 0;
3436
3437 if (len != sizeof(pinfo)) {
3438 retval = -EINVAL;
3439 goto out;
3440 }
3441
3442 if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3443 retval = -EFAULT;
3444 goto out;
3445 }
3446
3447 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3448 pinfo.spinfo_assoc_id);
3449 if (!transport)
3450 return -EINVAL;
3451
3452 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003453 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 pinfo.spinfo_cwnd = transport->cwnd;
3455 pinfo.spinfo_srtt = transport->srtt;
3456 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003457 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Frank Filz3f7a87d2005-06-20 13:14:57 -07003459 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3460 pinfo.spinfo_state = SCTP_ACTIVE;
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (put_user(len, optlen)) {
3463 retval = -EFAULT;
3464 goto out;
3465 }
3466
3467 if (copy_to_user(optval, &pinfo, len)) {
3468 retval = -EFAULT;
3469 goto out;
3470 }
3471
3472out:
3473 return (retval);
3474}
3475
3476/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3477 *
3478 * This option is a on/off flag. If enabled no SCTP message
3479 * fragmentation will be performed. Instead if a message being sent
3480 * exceeds the current PMTU size, the message will NOT be sent and
3481 * instead a error will be indicated to the user.
3482 */
3483static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3484 char __user *optval, int __user *optlen)
3485{
3486 int val;
3487
3488 if (len < sizeof(int))
3489 return -EINVAL;
3490
3491 len = sizeof(int);
3492 val = (sctp_sk(sk)->disable_fragments == 1);
3493 if (put_user(len, optlen))
3494 return -EFAULT;
3495 if (copy_to_user(optval, &val, len))
3496 return -EFAULT;
3497 return 0;
3498}
3499
3500/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3501 *
3502 * This socket option is used to specify various notifications and
3503 * ancillary data the user wishes to receive.
3504 */
3505static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3506 int __user *optlen)
3507{
3508 if (len != sizeof(struct sctp_event_subscribe))
3509 return -EINVAL;
3510 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3511 return -EFAULT;
3512 return 0;
3513}
3514
3515/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3516 *
3517 * This socket option is applicable to the UDP-style socket only. When
3518 * set it will cause associations that are idle for more than the
3519 * specified number of seconds to automatically close. An association
3520 * being idle is defined an association that has NOT sent or received
3521 * user data. The special value of '0' indicates that no automatic
3522 * close of any associations should be performed. The option expects an
3523 * integer defining the number of seconds of idle time before an
3524 * association is closed.
3525 */
3526static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3527{
3528 /* Applicable to UDP-style socket only */
3529 if (sctp_style(sk, TCP))
3530 return -EOPNOTSUPP;
3531 if (len != sizeof(int))
3532 return -EINVAL;
3533 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3534 return -EFAULT;
3535 return 0;
3536}
3537
3538/* Helper routine to branch off an association to a new socket. */
3539SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3540 struct socket **sockp)
3541{
3542 struct sock *sk = asoc->base.sk;
3543 struct socket *sock;
Vlad Yasevich4f444302006-10-30 18:54:32 -08003544 struct inet_sock *inetsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int err = 0;
3546
3547 /* An association cannot be branched off from an already peeled-off
3548 * socket, nor is this supported for tcp style sockets.
3549 */
3550 if (!sctp_style(sk, UDP))
3551 return -EINVAL;
3552
3553 /* Create a new socket. */
3554 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3555 if (err < 0)
3556 return err;
3557
3558 /* Populate the fields of the newsk from the oldsk and migrate the
3559 * asoc to the newsk.
3560 */
3561 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003562
3563 /* Make peeled-off sockets more like 1-1 accepted sockets.
3564 * Set the daddr and initialize id to something more random
3565 */
3566 inetsk = inet_sk(sock->sk);
3567 inetsk->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
3568 inetsk->id = asoc->next_tsn ^ jiffies;
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 *sockp = sock;
3571
3572 return err;
3573}
3574
3575static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3576{
3577 sctp_peeloff_arg_t peeloff;
3578 struct socket *newsock;
3579 int retval = 0;
3580 struct sctp_association *asoc;
3581
3582 if (len != sizeof(sctp_peeloff_arg_t))
3583 return -EINVAL;
3584 if (copy_from_user(&peeloff, optval, len))
3585 return -EFAULT;
3586
3587 asoc = sctp_id2assoc(sk, peeloff.associd);
3588 if (!asoc) {
3589 retval = -EINVAL;
3590 goto out;
3591 }
3592
3593 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3594
3595 retval = sctp_do_peeloff(asoc, &newsock);
3596 if (retval < 0)
3597 goto out;
3598
3599 /* Map the socket to an unused fd that can be returned to the user. */
3600 retval = sock_map_fd(newsock);
3601 if (retval < 0) {
3602 sock_release(newsock);
3603 goto out;
3604 }
3605
3606 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3607 __FUNCTION__, sk, asoc, newsock->sk, retval);
3608
3609 /* Return the fd mapped to the new socket. */
3610 peeloff.sd = retval;
3611 if (copy_to_user(optval, &peeloff, len))
3612 retval = -EFAULT;
3613
3614out:
3615 return retval;
3616}
3617
3618/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3619 *
3620 * Applications can enable or disable heartbeats for any peer address of
3621 * an association, modify an address's heartbeat interval, force a
3622 * heartbeat to be sent immediately, and adjust the address's maximum
3623 * number of retransmissions sent before an address is considered
3624 * unreachable. The following structure is used to access and modify an
3625 * address's parameters:
3626 *
3627 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003628 * sctp_assoc_t spp_assoc_id;
3629 * struct sockaddr_storage spp_address;
3630 * uint32_t spp_hbinterval;
3631 * uint16_t spp_pathmaxrxt;
3632 * uint32_t spp_pathmtu;
3633 * uint32_t spp_sackdelay;
3634 * uint32_t spp_flags;
3635 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003637 * spp_assoc_id - (one-to-many style socket) This is filled in the
3638 * application, and identifies the association for
3639 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 * spp_address - This specifies which address is of interest.
3641 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003642 * in milliseconds. If a value of zero
3643 * is present in this field then no changes are to
3644 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 * spp_pathmaxrxt - This contains the maximum number of
3646 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003647 * considered unreachable. If a value of zero
3648 * is present in this field then no changes are to
3649 * be made to this parameter.
3650 * spp_pathmtu - When Path MTU discovery is disabled the value
3651 * specified here will be the "fixed" path mtu.
3652 * Note that if the spp_address field is empty
3653 * then all associations on this address will
3654 * have this fixed path mtu set upon them.
3655 *
3656 * spp_sackdelay - When delayed sack is enabled, this value specifies
3657 * the number of milliseconds that sacks will be delayed
3658 * for. This value will apply to all addresses of an
3659 * association if the spp_address field is empty. Note
3660 * also, that if delayed sack is enabled and this
3661 * value is set to 0, no change is made to the last
3662 * recorded delayed sack timer value.
3663 *
3664 * spp_flags - These flags are used to control various features
3665 * on an association. The flag field may contain
3666 * zero or more of the following options.
3667 *
3668 * SPP_HB_ENABLE - Enable heartbeats on the
3669 * specified address. Note that if the address
3670 * field is empty all addresses for the association
3671 * have heartbeats enabled upon them.
3672 *
3673 * SPP_HB_DISABLE - Disable heartbeats on the
3674 * speicifed address. Note that if the address
3675 * field is empty all addresses for the association
3676 * will have their heartbeats disabled. Note also
3677 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3678 * mutually exclusive, only one of these two should
3679 * be specified. Enabling both fields will have
3680 * undetermined results.
3681 *
3682 * SPP_HB_DEMAND - Request a user initiated heartbeat
3683 * to be made immediately.
3684 *
3685 * SPP_PMTUD_ENABLE - This field will enable PMTU
3686 * discovery upon the specified address. Note that
3687 * if the address feild is empty then all addresses
3688 * on the association are effected.
3689 *
3690 * SPP_PMTUD_DISABLE - This field will disable PMTU
3691 * discovery upon the specified address. Note that
3692 * if the address feild is empty then all addresses
3693 * on the association are effected. Not also that
3694 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3695 * exclusive. Enabling both will have undetermined
3696 * results.
3697 *
3698 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3699 * on delayed sack. The time specified in spp_sackdelay
3700 * is used to specify the sack delay for this address. Note
3701 * that if spp_address is empty then all addresses will
3702 * enable delayed sack and take on the sack delay
3703 * value specified in spp_sackdelay.
3704 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3705 * off delayed sack. If the spp_address field is blank then
3706 * delayed sack is disabled for the entire association. Note
3707 * also that this field is mutually exclusive to
3708 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3709 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 */
3711static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003712 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003714 struct sctp_paddrparams params;
3715 struct sctp_transport *trans = NULL;
3716 struct sctp_association *asoc = NULL;
3717 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 if (len != sizeof(struct sctp_paddrparams))
3720 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 if (copy_from_user(&params, optval, len))
3723 return -EFAULT;
3724
Frank Filz52ccb8e2005-12-22 11:36:46 -08003725 /* If an address other than INADDR_ANY is specified, and
3726 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003728 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3729 trans = sctp_addr_id2transport(sk, &params.spp_address,
3730 params.spp_assoc_id);
3731 if (!trans) {
3732 SCTP_DEBUG_PRINTK("Failed no transport\n");
3733 return -EINVAL;
3734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736
Frank Filz52ccb8e2005-12-22 11:36:46 -08003737 /* Get association, if assoc_id != 0 and the socket is a one
3738 * to many style socket, and an association was not found, then
3739 * the id was invalid.
3740 */
3741 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3742 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3743 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Frank Filz52ccb8e2005-12-22 11:36:46 -08003747 if (trans) {
3748 /* Fetch transport values. */
3749 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3750 params.spp_pathmtu = trans->pathmtu;
3751 params.spp_pathmaxrxt = trans->pathmaxrxt;
3752 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Frank Filz52ccb8e2005-12-22 11:36:46 -08003754 /*draft-11 doesn't say what to return in spp_flags*/
3755 params.spp_flags = trans->param_flags;
3756 } else if (asoc) {
3757 /* Fetch association values. */
3758 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3759 params.spp_pathmtu = asoc->pathmtu;
3760 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3761 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Frank Filz52ccb8e2005-12-22 11:36:46 -08003763 /*draft-11 doesn't say what to return in spp_flags*/
3764 params.spp_flags = asoc->param_flags;
3765 } else {
3766 /* Fetch socket values. */
3767 params.spp_hbinterval = sp->hbinterval;
3768 params.spp_pathmtu = sp->pathmtu;
3769 params.spp_sackdelay = sp->sackdelay;
3770 params.spp_pathmaxrxt = sp->pathmaxrxt;
3771
3772 /*draft-11 doesn't say what to return in spp_flags*/
3773 params.spp_flags = sp->param_flags;
3774 }
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 if (copy_to_user(optval, &params, len))
3777 return -EFAULT;
3778
3779 if (put_user(len, optlen))
3780 return -EFAULT;
3781
3782 return 0;
3783}
3784
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003785/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08003786 *
3787 * This options will get or set the delayed ack timer. The time is set
3788 * in milliseconds. If the assoc_id is 0, then this sets or gets the
3789 * endpoints default delayed ack timer value. If the assoc_id field is
3790 * non-zero, then the set or get effects the specified association.
3791 *
3792 * struct sctp_assoc_value {
3793 * sctp_assoc_t assoc_id;
3794 * uint32_t assoc_value;
3795 * };
3796 *
3797 * assoc_id - This parameter, indicates which association the
3798 * user is preforming an action upon. Note that if
3799 * this field's value is zero then the endpoints
3800 * default value is changed (effecting future
3801 * associations only).
3802 *
3803 * assoc_value - This parameter contains the number of milliseconds
3804 * that the user is requesting the delayed ACK timer
3805 * be set to. Note that this value is defined in
3806 * the standard to be between 200 and 500 milliseconds.
3807 *
3808 * Note: a value of zero will leave the value alone,
3809 * but disable SACK delay. A non-zero value will also
3810 * enable SACK delay.
3811 */
3812static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3813 char __user *optval,
3814 int __user *optlen)
3815{
3816 struct sctp_assoc_value params;
3817 struct sctp_association *asoc = NULL;
3818 struct sctp_sock *sp = sctp_sk(sk);
3819
3820 if (len != sizeof(struct sctp_assoc_value))
3821 return - EINVAL;
3822
3823 if (copy_from_user(&params, optval, len))
3824 return -EFAULT;
3825
3826 /* Get association, if assoc_id != 0 and the socket is a one
3827 * to many style socket, and an association was not found, then
3828 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003829 */
Frank Filz77086102005-12-22 11:37:30 -08003830 asoc = sctp_id2assoc(sk, params.assoc_id);
3831 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3832 return -EINVAL;
3833
3834 if (asoc) {
3835 /* Fetch association values. */
3836 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3837 params.assoc_value = jiffies_to_msecs(
3838 asoc->sackdelay);
3839 else
3840 params.assoc_value = 0;
3841 } else {
3842 /* Fetch socket values. */
3843 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3844 params.assoc_value = sp->sackdelay;
3845 else
3846 params.assoc_value = 0;
3847 }
3848
3849 if (copy_to_user(optval, &params, len))
3850 return -EFAULT;
3851
3852 if (put_user(len, optlen))
3853 return -EFAULT;
3854
3855 return 0;
3856}
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3859 *
3860 * Applications can specify protocol parameters for the default association
3861 * initialization. The option name argument to setsockopt() and getsockopt()
3862 * is SCTP_INITMSG.
3863 *
3864 * Setting initialization parameters is effective only on an unconnected
3865 * socket (for UDP-style sockets only future associations are effected
3866 * by the change). With TCP-style sockets, this option is inherited by
3867 * sockets derived from a listener socket.
3868 */
3869static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3870{
3871 if (len != sizeof(struct sctp_initmsg))
3872 return -EINVAL;
3873 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3874 return -EFAULT;
3875 return 0;
3876}
3877
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003878static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3879 char __user *optval,
3880 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 sctp_assoc_t id;
3883 struct sctp_association *asoc;
3884 struct list_head *pos;
3885 int cnt = 0;
3886
3887 if (len != sizeof(sctp_assoc_t))
3888 return -EINVAL;
3889
3890 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3891 return -EFAULT;
3892
3893 /* For UDP-style sockets, id specifies the association to query. */
3894 asoc = sctp_id2assoc(sk, id);
3895 if (!asoc)
3896 return -EINVAL;
3897
3898 list_for_each(pos, &asoc->peer.transport_addr_list) {
3899 cnt ++;
3900 }
3901
3902 return cnt;
3903}
3904
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003905/*
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003906 * Old API for getting list of peer addresses. Does not work for 32-bit
3907 * programs running on a 64-bit kernel
3908 */
3909static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3910 char __user *optval,
3911 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
3913 struct sctp_association *asoc;
3914 struct list_head *pos;
3915 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003916 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 struct sctp_transport *from;
3918 void __user *to;
3919 union sctp_addr temp;
3920 struct sctp_sock *sp = sctp_sk(sk);
3921 int addrlen;
3922
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003923 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return -EINVAL;
3925
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003926 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return -EFAULT;
3928
3929 if (getaddrs.addr_num <= 0) return -EINVAL;
3930
3931 /* For UDP-style sockets, id specifies the association to query. */
3932 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3933 if (!asoc)
3934 return -EINVAL;
3935
3936 to = (void __user *)getaddrs.addrs;
3937 list_for_each(pos, &asoc->peer.transport_addr_list) {
3938 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08003939 memcpy(&temp, &from->ipaddr, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3941 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (copy_to_user(to, &temp, addrlen))
3943 return -EFAULT;
3944 to += addrlen ;
3945 cnt ++;
3946 if (cnt >= getaddrs.addr_num) break;
3947 }
3948 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003949 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return -EFAULT;
3951
3952 return 0;
3953}
3954
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003955static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3956 char __user *optval, int __user *optlen)
3957{
3958 struct sctp_association *asoc;
3959 struct list_head *pos;
3960 int cnt = 0;
3961 struct sctp_getaddrs getaddrs;
3962 struct sctp_transport *from;
3963 void __user *to;
3964 union sctp_addr temp;
3965 struct sctp_sock *sp = sctp_sk(sk);
3966 int addrlen;
3967 size_t space_left;
3968 int bytes_copied;
3969
3970 if (len < sizeof(struct sctp_getaddrs))
3971 return -EINVAL;
3972
3973 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3974 return -EFAULT;
3975
3976 /* For UDP-style sockets, id specifies the association to query. */
3977 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3978 if (!asoc)
3979 return -EINVAL;
3980
3981 to = optval + offsetof(struct sctp_getaddrs,addrs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003982 space_left = len - sizeof(struct sctp_getaddrs) -
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003983 offsetof(struct sctp_getaddrs,addrs);
3984
3985 list_for_each(pos, &asoc->peer.transport_addr_list) {
3986 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08003987 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003988 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3989 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07003990 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003991 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07003992 if (copy_to_user(to, &temp, addrlen))
3993 return -EFAULT;
3994 to += addrlen;
3995 cnt++;
3996 space_left -= addrlen;
3997 }
3998
3999 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4000 return -EFAULT;
4001 bytes_copied = ((char __user *)to) - optval;
4002 if (put_user(bytes_copied, optlen))
4003 return -EFAULT;
4004
4005 return 0;
4006}
4007
4008static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4009 char __user *optval,
4010 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
4012 sctp_assoc_t id;
4013 struct sctp_bind_addr *bp;
4014 struct sctp_association *asoc;
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004015 struct list_head *pos, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 struct sctp_sockaddr_entry *addr;
4017 rwlock_t *addr_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 int cnt = 0;
4019
4020 if (len != sizeof(sctp_assoc_t))
4021 return -EINVAL;
4022
4023 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4024 return -EFAULT;
4025
4026 /*
4027 * For UDP-style sockets, id specifies the association to query.
4028 * If the id field is set to the value '0' then the locally bound
4029 * addresses are returned without regard to any particular
4030 * association.
4031 */
4032 if (0 == id) {
4033 bp = &sctp_sk(sk)->ep->base.bind_addr;
4034 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4035 } else {
4036 asoc = sctp_id2assoc(sk, id);
4037 if (!asoc)
4038 return -EINVAL;
4039 bp = &asoc->base.bind_addr;
4040 addr_lock = &asoc->base.addr_lock;
4041 }
4042
4043 sctp_read_lock(addr_lock);
4044
4045 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4046 * addresses from the global local address list.
4047 */
4048 if (sctp_list_single_entry(&bp->address_list)) {
4049 addr = list_entry(bp->address_list.next,
4050 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004051 if (sctp_is_any(&addr->a)) {
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004052 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 addr = list_entry(pos,
4054 struct sctp_sockaddr_entry,
4055 list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004056 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004057 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 continue;
4059 cnt++;
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 } else {
4062 cnt = 1;
4063 }
4064 goto done;
4065 }
4066
4067 list_for_each(pos, &bp->address_list) {
4068 cnt ++;
4069 }
4070
4071done:
4072 sctp_read_unlock(addr_lock);
4073 return cnt;
4074}
4075
4076/* Helper function that copies local addresses to user and returns the number
4077 * of addresses copied.
4078 */
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004079static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4080 int max_addrs, void *to,
4081 int *bytes_copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004083 struct list_head *pos, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 union sctp_addr temp;
4086 int cnt = 0;
4087 int addrlen;
4088
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004089 list_for_each_safe(pos, next, &sctp_local_addr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004091 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004092 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 continue;
Al Viro6244be42006-11-20 17:21:44 -08004094 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4096 &temp);
4097 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004098 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004101 *bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 cnt ++;
4103 if (cnt >= max_addrs) break;
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 return cnt;
4107}
4108
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004109static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4110 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004111{
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004112 struct list_head *pos, *next;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004113 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004114 union sctp_addr temp;
4115 int cnt = 0;
4116 int addrlen;
4117
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004118 list_for_each_safe(pos, next, &sctp_local_addr_list) {
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004119 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004120 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004121 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004122 continue;
Al Viro6244be42006-11-20 17:21:44 -08004123 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004124 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4125 &temp);
4126 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004127 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004128 return -ENOMEM;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004129 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004130
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004131 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004132 cnt ++;
4133 space_left -= addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004134 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004135 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004136
4137 return cnt;
4138}
4139
4140/* Old API for getting list of local addresses. Does not work for 32-bit
4141 * programs running on a 64-bit kernel
4142 */
4143static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4144 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
4146 struct sctp_bind_addr *bp;
4147 struct sctp_association *asoc;
4148 struct list_head *pos;
4149 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004150 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 struct sctp_sockaddr_entry *addr;
4152 void __user *to;
4153 union sctp_addr temp;
4154 struct sctp_sock *sp = sctp_sk(sk);
4155 int addrlen;
4156 rwlock_t *addr_lock;
4157 int err = 0;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004158 void *addrs;
4159 int bytes_copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004161 if (len != sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 return -EINVAL;
4163
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004164 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return -EFAULT;
4166
4167 if (getaddrs.addr_num <= 0) return -EINVAL;
4168 /*
4169 * For UDP-style sockets, id specifies the association to query.
4170 * If the id field is set to the value '0' then the locally bound
4171 * addresses are returned without regard to any particular
4172 * association.
4173 */
4174 if (0 == getaddrs.assoc_id) {
4175 bp = &sctp_sk(sk)->ep->base.bind_addr;
4176 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4177 } else {
4178 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4179 if (!asoc)
4180 return -EINVAL;
4181 bp = &asoc->base.bind_addr;
4182 addr_lock = &asoc->base.addr_lock;
4183 }
4184
4185 to = getaddrs.addrs;
4186
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004187 /* Allocate space for a local instance of packed array to hold all
4188 * the data. We store addresses here first and then put write them
4189 * to the user in one shot.
4190 */
4191 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4192 GFP_KERNEL);
4193 if (!addrs)
4194 return -ENOMEM;
4195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 sctp_read_lock(addr_lock);
4197
4198 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4199 * addresses from the global local address list.
4200 */
4201 if (sctp_list_single_entry(&bp->address_list)) {
4202 addr = list_entry(bp->address_list.next,
4203 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004204 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004205 cnt = sctp_copy_laddrs_old(sk, bp->port,
4206 getaddrs.addr_num,
4207 addrs, &bytes_copied);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004208 goto copy_getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
4210 }
4211
4212 list_for_each(pos, &bp->address_list) {
4213 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004214 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4216 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004217 memcpy(addrs, &temp, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004219 bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 cnt ++;
4221 if (cnt >= getaddrs.addr_num) break;
4222 }
4223
4224copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004225 sctp_read_unlock(addr_lock);
4226
4227 /* copy the entire address list into the user provided space */
4228 if (copy_to_user(to, addrs, bytes_copied)) {
4229 err = -EFAULT;
4230 goto error;
4231 }
4232
4233 /* copy the leading structure back to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 getaddrs.addr_num = cnt;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004235 if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 err = -EFAULT;
4237
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004238error:
4239 kfree(addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return err;
4241}
4242
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004243static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4244 char __user *optval, int __user *optlen)
4245{
4246 struct sctp_bind_addr *bp;
4247 struct sctp_association *asoc;
4248 struct list_head *pos;
4249 int cnt = 0;
4250 struct sctp_getaddrs getaddrs;
4251 struct sctp_sockaddr_entry *addr;
4252 void __user *to;
4253 union sctp_addr temp;
4254 struct sctp_sock *sp = sctp_sk(sk);
4255 int addrlen;
4256 rwlock_t *addr_lock;
4257 int err = 0;
4258 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004259 int bytes_copied = 0;
4260 void *addrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004261
4262 if (len <= sizeof(struct sctp_getaddrs))
4263 return -EINVAL;
4264
4265 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4266 return -EFAULT;
4267
4268 /*
4269 * For UDP-style sockets, id specifies the association to query.
4270 * If the id field is set to the value '0' then the locally bound
4271 * addresses are returned without regard to any particular
4272 * association.
4273 */
4274 if (0 == getaddrs.assoc_id) {
4275 bp = &sctp_sk(sk)->ep->base.bind_addr;
4276 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4277 } else {
4278 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4279 if (!asoc)
4280 return -EINVAL;
4281 bp = &asoc->base.bind_addr;
4282 addr_lock = &asoc->base.addr_lock;
4283 }
4284
4285 to = optval + offsetof(struct sctp_getaddrs,addrs);
4286 space_left = len - sizeof(struct sctp_getaddrs) -
4287 offsetof(struct sctp_getaddrs,addrs);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004288 addrs = kmalloc(space_left, GFP_KERNEL);
4289 if (!addrs)
4290 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004291
4292 sctp_read_lock(addr_lock);
4293
4294 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4295 * addresses from the global local address list.
4296 */
4297 if (sctp_list_single_entry(&bp->address_list)) {
4298 addr = list_entry(bp->address_list.next,
4299 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004300 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004301 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4302 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004303 if (cnt < 0) {
4304 err = cnt;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004305 goto error;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004306 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004307 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004308 }
4309 }
4310
4311 list_for_each(pos, &bp->address_list) {
4312 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004313 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004314 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4315 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004316 if (space_left < addrlen) {
4317 err = -ENOMEM; /*fixme: right error?*/
4318 goto error;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004319 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004320 memcpy(addrs, &temp, addrlen);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004321 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004322 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004323 cnt ++;
4324 space_left -= addrlen;
4325 }
4326
4327copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004328 sctp_read_unlock(addr_lock);
4329
4330 if (copy_to_user(to, addrs, bytes_copied)) {
4331 err = -EFAULT;
4332 goto error;
4333 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004334 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4335 return -EFAULT;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004336 if (put_user(bytes_copied, optlen))
4337 return -EFAULT;
4338
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004339error:
4340 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004341 return err;
4342}
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4345 *
4346 * Requests that the local SCTP stack use the enclosed peer address as
4347 * the association primary. The enclosed address must be one of the
4348 * association peer's addresses.
4349 */
4350static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4351 char __user *optval, int __user *optlen)
4352{
4353 struct sctp_prim prim;
4354 struct sctp_association *asoc;
4355 struct sctp_sock *sp = sctp_sk(sk);
4356
4357 if (len != sizeof(struct sctp_prim))
4358 return -EINVAL;
4359
4360 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4361 return -EFAULT;
4362
4363 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4364 if (!asoc)
4365 return -EINVAL;
4366
4367 if (!asoc->peer.primary_path)
4368 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004369
Al Viro8cec6b82006-11-20 17:23:01 -08004370 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4371 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4374 (union sctp_addr *)&prim.ssp_addr);
4375
4376 if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4377 return -EFAULT;
4378
4379 return 0;
4380}
4381
4382/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004383 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004385 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 * Indication parameter for all future INIT and INIT-ACK exchanges.
4387 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004388static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 char __user *optval, int __user *optlen)
4390{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004391 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004393 if (len != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return -EINVAL;
4395
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004396 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4397 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return 0;
4401}
4402
4403/*
4404 *
4405 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4406 *
4407 * Applications that wish to use the sendto() system call may wish to
4408 * specify a default set of parameters that would normally be supplied
4409 * through the inclusion of ancillary data. This socket option allows
4410 * such an application to set the default sctp_sndrcvinfo structure.
4411
4412
4413 * The application that wishes to use this socket option simply passes
4414 * in to this call the sctp_sndrcvinfo structure defined in Section
4415 * 5.2.2) The input parameters accepted by this call include
4416 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4417 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4418 * to this call if the caller is using the UDP model.
4419 *
4420 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4421 */
4422static int sctp_getsockopt_default_send_param(struct sock *sk,
4423 int len, char __user *optval,
4424 int __user *optlen)
4425{
4426 struct sctp_sndrcvinfo info;
4427 struct sctp_association *asoc;
4428 struct sctp_sock *sp = sctp_sk(sk);
4429
4430 if (len != sizeof(struct sctp_sndrcvinfo))
4431 return -EINVAL;
4432 if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4433 return -EFAULT;
4434
4435 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4436 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4437 return -EINVAL;
4438
4439 if (asoc) {
4440 info.sinfo_stream = asoc->default_stream;
4441 info.sinfo_flags = asoc->default_flags;
4442 info.sinfo_ppid = asoc->default_ppid;
4443 info.sinfo_context = asoc->default_context;
4444 info.sinfo_timetolive = asoc->default_timetolive;
4445 } else {
4446 info.sinfo_stream = sp->default_stream;
4447 info.sinfo_flags = sp->default_flags;
4448 info.sinfo_ppid = sp->default_ppid;
4449 info.sinfo_context = sp->default_context;
4450 info.sinfo_timetolive = sp->default_timetolive;
4451 }
4452
4453 if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4454 return -EFAULT;
4455
4456 return 0;
4457}
4458
4459/*
4460 *
4461 * 7.1.5 SCTP_NODELAY
4462 *
4463 * Turn on/off any Nagle-like algorithm. This means that packets are
4464 * generally sent as soon as possible and no unnecessary delays are
4465 * introduced, at the cost of more packets in the network. Expects an
4466 * integer boolean flag.
4467 */
4468
4469static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4470 char __user *optval, int __user *optlen)
4471{
4472 int val;
4473
4474 if (len < sizeof(int))
4475 return -EINVAL;
4476
4477 len = sizeof(int);
4478 val = (sctp_sk(sk)->nodelay == 1);
4479 if (put_user(len, optlen))
4480 return -EFAULT;
4481 if (copy_to_user(optval, &val, len))
4482 return -EFAULT;
4483 return 0;
4484}
4485
4486/*
4487 *
4488 * 7.1.1 SCTP_RTOINFO
4489 *
4490 * The protocol parameters used to initialize and bound retransmission
4491 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4492 * and modify these parameters.
4493 * All parameters are time values, in milliseconds. A value of 0, when
4494 * modifying the parameters, indicates that the current value should not
4495 * be changed.
4496 *
4497 */
4498static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4499 char __user *optval,
4500 int __user *optlen) {
4501 struct sctp_rtoinfo rtoinfo;
4502 struct sctp_association *asoc;
4503
4504 if (len != sizeof (struct sctp_rtoinfo))
4505 return -EINVAL;
4506
4507 if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4508 return -EFAULT;
4509
4510 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4511
4512 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4513 return -EINVAL;
4514
4515 /* Values corresponding to the specific association. */
4516 if (asoc) {
4517 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4518 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4519 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4520 } else {
4521 /* Values corresponding to the endpoint. */
4522 struct sctp_sock *sp = sctp_sk(sk);
4523
4524 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4525 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4526 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4527 }
4528
4529 if (put_user(len, optlen))
4530 return -EFAULT;
4531
4532 if (copy_to_user(optval, &rtoinfo, len))
4533 return -EFAULT;
4534
4535 return 0;
4536}
4537
4538/*
4539 *
4540 * 7.1.2 SCTP_ASSOCINFO
4541 *
4542 * This option is used to tune the the maximum retransmission attempts
4543 * of the association.
4544 * Returns an error if the new association retransmission value is
4545 * greater than the sum of the retransmission value of the peer.
4546 * See [SCTP] for more information.
4547 *
4548 */
4549static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4550 char __user *optval,
4551 int __user *optlen)
4552{
4553
4554 struct sctp_assocparams assocparams;
4555 struct sctp_association *asoc;
4556 struct list_head *pos;
4557 int cnt = 0;
4558
4559 if (len != sizeof (struct sctp_assocparams))
4560 return -EINVAL;
4561
4562 if (copy_from_user(&assocparams, optval,
4563 sizeof (struct sctp_assocparams)))
4564 return -EFAULT;
4565
4566 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4567
4568 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4569 return -EINVAL;
4570
4571 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004572 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4574 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4575 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4576 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4577 * 1000) +
4578 (asoc->cookie_life.tv_usec
4579 / 1000);
4580
4581 list_for_each(pos, &asoc->peer.transport_addr_list) {
4582 cnt ++;
4583 }
4584
4585 assocparams.sasoc_number_peer_destinations = cnt;
4586 } else {
4587 /* Values corresponding to the endpoint */
4588 struct sctp_sock *sp = sctp_sk(sk);
4589
4590 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4591 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4592 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4593 assocparams.sasoc_cookie_life =
4594 sp->assocparams.sasoc_cookie_life;
4595 assocparams.sasoc_number_peer_destinations =
4596 sp->assocparams.
4597 sasoc_number_peer_destinations;
4598 }
4599
4600 if (put_user(len, optlen))
4601 return -EFAULT;
4602
4603 if (copy_to_user(optval, &assocparams, len))
4604 return -EFAULT;
4605
4606 return 0;
4607}
4608
4609/*
4610 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4611 *
4612 * This socket option is a boolean flag which turns on or off mapped V4
4613 * addresses. If this option is turned on and the socket is type
4614 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4615 * If this option is turned off, then no mapping will be done of V4
4616 * addresses and a user will receive both PF_INET6 and PF_INET type
4617 * addresses on the socket.
4618 */
4619static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4620 char __user *optval, int __user *optlen)
4621{
4622 int val;
4623 struct sctp_sock *sp = sctp_sk(sk);
4624
4625 if (len < sizeof(int))
4626 return -EINVAL;
4627
4628 len = sizeof(int);
4629 val = sp->v4mapped;
4630 if (put_user(len, optlen))
4631 return -EFAULT;
4632 if (copy_to_user(optval, &val, len))
4633 return -EFAULT;
4634
4635 return 0;
4636}
4637
4638/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004639 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4640 * (chapter and verse is quoted at sctp_setsockopt_context())
4641 */
4642static int sctp_getsockopt_context(struct sock *sk, int len,
4643 char __user *optval, int __user *optlen)
4644{
4645 struct sctp_assoc_value params;
4646 struct sctp_sock *sp;
4647 struct sctp_association *asoc;
4648
4649 if (len != sizeof(struct sctp_assoc_value))
4650 return -EINVAL;
4651
4652 if (copy_from_user(&params, optval, len))
4653 return -EFAULT;
4654
4655 sp = sctp_sk(sk);
4656
4657 if (params.assoc_id != 0) {
4658 asoc = sctp_id2assoc(sk, params.assoc_id);
4659 if (!asoc)
4660 return -EINVAL;
4661 params.assoc_value = asoc->default_rcv_context;
4662 } else {
4663 params.assoc_value = sp->default_rcv_context;
4664 }
4665
4666 if (put_user(len, optlen))
4667 return -EFAULT;
4668 if (copy_to_user(optval, &params, len))
4669 return -EFAULT;
4670
4671 return 0;
4672}
4673
4674/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4676 *
4677 * This socket option specifies the maximum size to put in any outgoing
4678 * SCTP chunk. If a message is larger than this size it will be
4679 * fragmented by SCTP into the specified size. Note that the underlying
4680 * SCTP implementation may fragment into smaller sized chunks when the
4681 * PMTU of the underlying association is smaller than the value set by
4682 * the user.
4683 */
4684static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4685 char __user *optval, int __user *optlen)
4686{
4687 int val;
4688
4689 if (len < sizeof(int))
4690 return -EINVAL;
4691
4692 len = sizeof(int);
4693
4694 val = sctp_sk(sk)->user_frag;
4695 if (put_user(len, optlen))
4696 return -EFAULT;
4697 if (copy_to_user(optval, &val, len))
4698 return -EFAULT;
4699
4700 return 0;
4701}
4702
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004703/*
4704 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4705 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4706 */
4707static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4708 char __user *optval, int __user *optlen)
4709{
4710 int val;
4711
4712 if (len < sizeof(int))
4713 return -EINVAL;
4714
4715 len = sizeof(int);
4716
4717 val = sctp_sk(sk)->frag_interleave;
4718 if (put_user(len, optlen))
4719 return -EFAULT;
4720 if (copy_to_user(optval, &val, len))
4721 return -EFAULT;
4722
4723 return 0;
4724}
4725
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004726/*
4727 * 7.1.25. Set or Get the sctp partial delivery point
4728 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4729 */
4730static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4731 char __user *optval,
4732 int __user *optlen)
4733{
4734 u32 val;
4735
4736 if (len < sizeof(u32))
4737 return -EINVAL;
4738
4739 len = sizeof(u32);
4740
4741 val = sctp_sk(sk)->pd_point;
4742 if (put_user(len, optlen))
4743 return -EFAULT;
4744 if (copy_to_user(optval, &val, len))
4745 return -EFAULT;
4746
4747 return -ENOTSUPP;
4748}
4749
Vlad Yasevich70331572007-03-23 11:34:36 -07004750/*
4751 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4752 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4753 */
4754static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4755 char __user *optval,
4756 int __user *optlen)
4757{
4758 int val;
4759
4760 if (len < sizeof(int))
4761 return -EINVAL;
4762
4763 len = sizeof(int);
4764
4765 val = sctp_sk(sk)->max_burst;
4766 if (put_user(len, optlen))
4767 return -EFAULT;
4768 if (copy_to_user(optval, &val, len))
4769 return -EFAULT;
4770
4771 return -ENOTSUPP;
4772}
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4775 char __user *optval, int __user *optlen)
4776{
4777 int retval = 0;
4778 int len;
4779
Frank Filz3f7a87d2005-06-20 13:14:57 -07004780 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4781 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
4783 /* I can hardly begin to describe how wrong this is. This is
4784 * so broken as to be worse than useless. The API draft
4785 * REALLY is NOT helpful here... I am not convinced that the
4786 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4787 * are at all well-founded.
4788 */
4789 if (level != SOL_SCTP) {
4790 struct sctp_af *af = sctp_sk(sk)->pf->af;
4791
4792 retval = af->getsockopt(sk, level, optname, optval, optlen);
4793 return retval;
4794 }
4795
4796 if (get_user(len, optlen))
4797 return -EFAULT;
4798
4799 sctp_lock_sock(sk);
4800
4801 switch (optname) {
4802 case SCTP_STATUS:
4803 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4804 break;
4805 case SCTP_DISABLE_FRAGMENTS:
4806 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4807 optlen);
4808 break;
4809 case SCTP_EVENTS:
4810 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4811 break;
4812 case SCTP_AUTOCLOSE:
4813 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4814 break;
4815 case SCTP_SOCKOPT_PEELOFF:
4816 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4817 break;
4818 case SCTP_PEER_ADDR_PARAMS:
4819 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4820 optlen);
4821 break;
Frank Filz77086102005-12-22 11:37:30 -08004822 case SCTP_DELAYED_ACK_TIME:
4823 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4824 optlen);
4825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 case SCTP_INITMSG:
4827 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4828 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004829 case SCTP_GET_PEER_ADDRS_NUM_OLD:
4830 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4831 optlen);
4832 break;
4833 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4834 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4835 optlen);
4836 break;
4837 case SCTP_GET_PEER_ADDRS_OLD:
4838 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 optlen);
4840 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004841 case SCTP_GET_LOCAL_ADDRS_OLD:
4842 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 optlen);
4844 break;
4845 case SCTP_GET_PEER_ADDRS:
4846 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4847 optlen);
4848 break;
4849 case SCTP_GET_LOCAL_ADDRS:
4850 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4851 optlen);
4852 break;
4853 case SCTP_DEFAULT_SEND_PARAM:
4854 retval = sctp_getsockopt_default_send_param(sk, len,
4855 optval, optlen);
4856 break;
4857 case SCTP_PRIMARY_ADDR:
4858 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4859 break;
4860 case SCTP_NODELAY:
4861 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4862 break;
4863 case SCTP_RTOINFO:
4864 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4865 break;
4866 case SCTP_ASSOCINFO:
4867 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4868 break;
4869 case SCTP_I_WANT_MAPPED_V4_ADDR:
4870 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4871 break;
4872 case SCTP_MAXSEG:
4873 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4874 break;
4875 case SCTP_GET_PEER_ADDR_INFO:
4876 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4877 optlen);
4878 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004879 case SCTP_ADAPTATION_LAYER:
4880 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 optlen);
4882 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004883 case SCTP_CONTEXT:
4884 retval = sctp_getsockopt_context(sk, len, optval, optlen);
4885 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004886 case SCTP_FRAGMENT_INTERLEAVE:
4887 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
4888 optlen);
4889 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004890 case SCTP_PARTIAL_DELIVERY_POINT:
4891 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
4892 optlen);
4893 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004894 case SCTP_MAX_BURST:
4895 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
4896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 default:
4898 retval = -ENOPROTOOPT;
4899 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 sctp_release_sock(sk);
4903 return retval;
4904}
4905
4906static void sctp_hash(struct sock *sk)
4907{
4908 /* STUB */
4909}
4910
4911static void sctp_unhash(struct sock *sk)
4912{
4913 /* STUB */
4914}
4915
4916/* Check if port is acceptable. Possibly find first available port.
4917 *
4918 * The port hash table (contained in the 'global' SCTP protocol storage
4919 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4920 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4921 * list (the list number is the port number hashed out, so as you
4922 * would expect from a hash function, all the ports in a given list have
4923 * such a number that hashes out to the same list number; you were
4924 * expecting that, right?); so each list has a set of ports, with a
4925 * link to the socket (struct sock) that uses it, the port number and
4926 * a fastreuse flag (FIXME: NPI ipg).
4927 */
4928static struct sctp_bind_bucket *sctp_bucket_create(
4929 struct sctp_bind_hashbucket *head, unsigned short snum);
4930
4931static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4932{
4933 struct sctp_bind_hashbucket *head; /* hash list */
4934 struct sctp_bind_bucket *pp; /* hash list port iterator */
4935 unsigned short snum;
4936 int ret;
4937
Al Viro04afd8b2006-11-20 17:02:01 -08004938 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4941 sctp_local_bh_disable();
4942
4943 if (snum == 0) {
4944 /* Search for an available port.
4945 *
4946 * 'sctp_port_rover' was the last port assigned, so
4947 * we start to search from 'sctp_port_rover +
4948 * 1'. What we do is first check if port 'rover' is
4949 * already in the hash table; if not, we use that; if
4950 * it is, we try next.
4951 */
4952 int low = sysctl_local_port_range[0];
4953 int high = sysctl_local_port_range[1];
4954 int remaining = (high - low) + 1;
4955 int rover;
4956 int index;
4957
4958 sctp_spin_lock(&sctp_port_alloc_lock);
4959 rover = sctp_port_rover;
4960 do {
4961 rover++;
4962 if ((rover < low) || (rover > high))
4963 rover = low;
4964 index = sctp_phashfn(rover);
4965 head = &sctp_port_hashtable[index];
4966 sctp_spin_lock(&head->lock);
4967 for (pp = head->chain; pp; pp = pp->next)
4968 if (pp->port == rover)
4969 goto next;
4970 break;
4971 next:
4972 sctp_spin_unlock(&head->lock);
4973 } while (--remaining > 0);
4974 sctp_port_rover = rover;
4975 sctp_spin_unlock(&sctp_port_alloc_lock);
4976
4977 /* Exhausted local port range during search? */
4978 ret = 1;
4979 if (remaining <= 0)
4980 goto fail;
4981
4982 /* OK, here is the one we will use. HEAD (the port
4983 * hash table list entry) is non-NULL and we hold it's
4984 * mutex.
4985 */
4986 snum = rover;
4987 } else {
4988 /* We are given an specific port number; we verify
4989 * that it is not being used. If it is used, we will
4990 * exahust the search in the hash list corresponding
4991 * to the port number (snum) - we detect that with the
4992 * port iterator, pp being NULL.
4993 */
4994 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4995 sctp_spin_lock(&head->lock);
4996 for (pp = head->chain; pp; pp = pp->next) {
4997 if (pp->port == snum)
4998 goto pp_found;
4999 }
5000 }
5001 pp = NULL;
5002 goto pp_not_found;
5003pp_found:
5004 if (!hlist_empty(&pp->owner)) {
5005 /* We had a port hash table hit - there is an
5006 * available port (pp != NULL) and it is being
5007 * used by other socket (pp->owner not empty); that other
5008 * socket is going to be sk2.
5009 */
5010 int reuse = sk->sk_reuse;
5011 struct sock *sk2;
5012 struct hlist_node *node;
5013
5014 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
5015 if (pp->fastreuse && sk->sk_reuse)
5016 goto success;
5017
5018 /* Run through the list of sockets bound to the port
5019 * (pp->port) [via the pointers bind_next and
5020 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5021 * we get the endpoint they describe and run through
5022 * the endpoint's list of IP (v4 or v6) addresses,
5023 * comparing each of the addresses with the address of
5024 * the socket sk. If we find a match, then that means
5025 * that this port/socket (sk) combination are already
5026 * in an endpoint.
5027 */
5028 sk_for_each_bound(sk2, node, &pp->owner) {
5029 struct sctp_endpoint *ep2;
5030 ep2 = sctp_sk(sk2)->ep;
5031
5032 if (reuse && sk2->sk_reuse)
5033 continue;
5034
Al Viro7e1e4a22006-11-20 17:05:43 -08005035 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 sctp_sk(sk))) {
5037 ret = (long)sk2;
5038 goto fail_unlock;
5039 }
5040 }
5041 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5042 }
5043pp_not_found:
5044 /* If there was a hash table miss, create a new port. */
5045 ret = 1;
5046 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5047 goto fail_unlock;
5048
5049 /* In either case (hit or miss), make sure fastreuse is 1 only
5050 * if sk->sk_reuse is too (that is, if the caller requested
5051 * SO_REUSEADDR on this socket -sk-).
5052 */
5053 if (hlist_empty(&pp->owner))
5054 pp->fastreuse = sk->sk_reuse ? 1 : 0;
5055 else if (pp->fastreuse && !sk->sk_reuse)
5056 pp->fastreuse = 0;
5057
5058 /* We are set, so fill up all the data in the hash table
5059 * entry, tie the socket list information with the rest of the
5060 * sockets FIXME: Blurry, NPI (ipg).
5061 */
5062success:
5063 inet_sk(sk)->num = snum;
5064 if (!sctp_sk(sk)->bind_hash) {
5065 sk_add_bind_node(sk, &pp->owner);
5066 sctp_sk(sk)->bind_hash = pp;
5067 }
5068 ret = 0;
5069
5070fail_unlock:
5071 sctp_spin_unlock(&head->lock);
5072
5073fail:
5074 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 return ret;
5076}
5077
5078/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5079 * port is requested.
5080 */
5081static int sctp_get_port(struct sock *sk, unsigned short snum)
5082{
5083 long ret;
5084 union sctp_addr addr;
5085 struct sctp_af *af = sctp_sk(sk)->pf->af;
5086
5087 /* Set up a dummy address struct from the sk. */
5088 af->from_sk(&addr, sk);
5089 addr.v4.sin_port = htons(snum);
5090
5091 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5092 ret = sctp_get_port_local(sk, &addr);
5093
5094 return (ret ? 1 : 0);
5095}
5096
5097/*
5098 * 3.1.3 listen() - UDP Style Syntax
5099 *
5100 * By default, new associations are not accepted for UDP style sockets.
5101 * An application uses listen() to mark a socket as being able to
5102 * accept new associations.
5103 */
5104SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5105{
5106 struct sctp_sock *sp = sctp_sk(sk);
5107 struct sctp_endpoint *ep = sp->ep;
5108
5109 /* Only UDP style sockets that are not peeled off are allowed to
5110 * listen().
5111 */
5112 if (!sctp_style(sk, UDP))
5113 return -EINVAL;
5114
5115 /* If backlog is zero, disable listening. */
5116 if (!backlog) {
5117 if (sctp_sstate(sk, CLOSED))
5118 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 sctp_unhash_endpoint(ep);
5121 sk->sk_state = SCTP_SS_CLOSED;
5122 }
5123
5124 /* Return if we are already listening. */
5125 if (sctp_sstate(sk, LISTENING))
5126 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 /*
5129 * If a bind() or sctp_bindx() is not called prior to a listen()
5130 * call that allows new associations to be accepted, the system
5131 * picks an ephemeral port and will choose an address set equivalent
5132 * to binding with a wildcard address.
5133 *
5134 * This is not currently spelled out in the SCTP sockets
5135 * extensions draft, but follows the practice as seen in TCP
5136 * sockets.
5137 */
5138 if (!ep->base.bind_addr.port) {
5139 if (sctp_autobind(sk))
5140 return -EAGAIN;
5141 }
5142 sk->sk_state = SCTP_SS_LISTENING;
5143 sctp_hash_endpoint(ep);
5144 return 0;
5145}
5146
5147/*
5148 * 4.1.3 listen() - TCP Style Syntax
5149 *
5150 * Applications uses listen() to ready the SCTP endpoint for accepting
5151 * inbound associations.
5152 */
5153SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5154{
5155 struct sctp_sock *sp = sctp_sk(sk);
5156 struct sctp_endpoint *ep = sp->ep;
5157
5158 /* If backlog is zero, disable listening. */
5159 if (!backlog) {
5160 if (sctp_sstate(sk, CLOSED))
5161 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 sctp_unhash_endpoint(ep);
5164 sk->sk_state = SCTP_SS_CLOSED;
5165 }
5166
5167 if (sctp_sstate(sk, LISTENING))
5168 return 0;
5169
5170 /*
5171 * If a bind() or sctp_bindx() is not called prior to a listen()
5172 * call that allows new associations to be accepted, the system
5173 * picks an ephemeral port and will choose an address set equivalent
5174 * to binding with a wildcard address.
5175 *
5176 * This is not currently spelled out in the SCTP sockets
5177 * extensions draft, but follows the practice as seen in TCP
5178 * sockets.
5179 */
5180 if (!ep->base.bind_addr.port) {
5181 if (sctp_autobind(sk))
5182 return -EAGAIN;
5183 }
5184 sk->sk_state = SCTP_SS_LISTENING;
5185 sk->sk_max_ack_backlog = backlog;
5186 sctp_hash_endpoint(ep);
5187 return 0;
5188}
5189
5190/*
5191 * Move a socket to LISTENING state.
5192 */
5193int sctp_inet_listen(struct socket *sock, int backlog)
5194{
5195 struct sock *sk = sock->sk;
Herbert Xu1b489e12006-08-20 15:07:14 +10005196 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 int err = -EINVAL;
5198
5199 if (unlikely(backlog < 0))
5200 goto out;
5201
5202 sctp_lock_sock(sk);
5203
5204 if (sock->state != SS_UNCONNECTED)
5205 goto out;
5206
5207 /* Allocate HMAC for generating cookie. */
5208 if (sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005209 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 if (!tfm) {
5211 err = -ENOSYS;
5212 goto out;
5213 }
5214 }
5215
5216 switch (sock->type) {
5217 case SOCK_SEQPACKET:
5218 err = sctp_seqpacket_listen(sk, backlog);
5219 break;
5220 case SOCK_STREAM:
5221 err = sctp_stream_listen(sk, backlog);
5222 break;
5223 default:
5224 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005225 }
5226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (err)
5228 goto cleanup;
5229
5230 /* Store away the transform reference. */
5231 sctp_sk(sk)->hmac = tfm;
5232out:
5233 sctp_release_sock(sk);
5234 return err;
5235cleanup:
Herbert Xu1b489e12006-08-20 15:07:14 +10005236 crypto_free_hash(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 goto out;
5238}
5239
5240/*
5241 * This function is done by modeling the current datagram_poll() and the
5242 * tcp_poll(). Note that, based on these implementations, we don't
5243 * lock the socket in this function, even though it seems that,
5244 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005245 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 * in this place. We assume that we don't need locks either until proven
5247 * otherwise.
5248 *
5249 * Another thing to note is that we include the Async I/O support
5250 * here, again, by modeling the current TCP/UDP code. We don't have
5251 * a good way to test with it yet.
5252 */
5253unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5254{
5255 struct sock *sk = sock->sk;
5256 struct sctp_sock *sp = sctp_sk(sk);
5257 unsigned int mask;
5258
5259 poll_wait(file, sk->sk_sleep, wait);
5260
5261 /* A TCP-style listening socket becomes readable when the accept queue
5262 * is not empty.
5263 */
5264 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5265 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005266 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 mask = 0;
5269
5270 /* Is there any exceptional events? */
5271 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5272 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08005273 if (sk->sk_shutdown & RCV_SHUTDOWN)
5274 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (sk->sk_shutdown == SHUTDOWN_MASK)
5276 mask |= POLLHUP;
5277
5278 /* Is it readable? Reconsider this code with TCP-style support. */
5279 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5280 (sk->sk_shutdown & RCV_SHUTDOWN))
5281 mask |= POLLIN | POLLRDNORM;
5282
5283 /* The association is either gone or not ready. */
5284 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5285 return mask;
5286
5287 /* Is it writable? */
5288 if (sctp_writeable(sk)) {
5289 mask |= POLLOUT | POLLWRNORM;
5290 } else {
5291 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5292 /*
5293 * Since the socket is not locked, the buffer
5294 * might be made available after the writeable check and
5295 * before the bit is set. This could cause a lost I/O
5296 * signal. tcp_poll() has a race breaker for this race
5297 * condition. Based on their implementation, we put
5298 * in the following code to cover it as well.
5299 */
5300 if (sctp_writeable(sk))
5301 mask |= POLLOUT | POLLWRNORM;
5302 }
5303 return mask;
5304}
5305
5306/********************************************************************
5307 * 2nd Level Abstractions
5308 ********************************************************************/
5309
5310static struct sctp_bind_bucket *sctp_bucket_create(
5311 struct sctp_bind_hashbucket *head, unsigned short snum)
5312{
5313 struct sctp_bind_bucket *pp;
5314
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08005315 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 SCTP_DBG_OBJCNT_INC(bind_bucket);
5317 if (pp) {
5318 pp->port = snum;
5319 pp->fastreuse = 0;
5320 INIT_HLIST_HEAD(&pp->owner);
5321 if ((pp->next = head->chain) != NULL)
5322 pp->next->pprev = &pp->next;
5323 head->chain = pp;
5324 pp->pprev = &head->chain;
5325 }
5326 return pp;
5327}
5328
5329/* Caller must hold hashbucket lock for this tb with local BH disabled */
5330static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5331{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005332 if (pp && hlist_empty(&pp->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 if (pp->next)
5334 pp->next->pprev = pp->pprev;
5335 *(pp->pprev) = pp->next;
5336 kmem_cache_free(sctp_bucket_cachep, pp);
5337 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5338 }
5339}
5340
5341/* Release this socket's reference to a local port. */
5342static inline void __sctp_put_port(struct sock *sk)
5343{
5344 struct sctp_bind_hashbucket *head =
5345 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5346 struct sctp_bind_bucket *pp;
5347
5348 sctp_spin_lock(&head->lock);
5349 pp = sctp_sk(sk)->bind_hash;
5350 __sk_del_bind_node(sk);
5351 sctp_sk(sk)->bind_hash = NULL;
5352 inet_sk(sk)->num = 0;
5353 sctp_bucket_destroy(pp);
5354 sctp_spin_unlock(&head->lock);
5355}
5356
5357void sctp_put_port(struct sock *sk)
5358{
5359 sctp_local_bh_disable();
5360 __sctp_put_port(sk);
5361 sctp_local_bh_enable();
5362}
5363
5364/*
5365 * The system picks an ephemeral port and choose an address set equivalent
5366 * to binding with a wildcard address.
5367 * One of those addresses will be the primary address for the association.
5368 * This automatically enables the multihoming capability of SCTP.
5369 */
5370static int sctp_autobind(struct sock *sk)
5371{
5372 union sctp_addr autoaddr;
5373 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08005374 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
5376 /* Initialize a local sockaddr structure to INADDR_ANY. */
5377 af = sctp_sk(sk)->pf->af;
5378
5379 port = htons(inet_sk(sk)->num);
5380 af->inaddr_any(&autoaddr, port);
5381
5382 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5383}
5384
5385/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5386 *
5387 * From RFC 2292
5388 * 4.2 The cmsghdr Structure *
5389 *
5390 * When ancillary data is sent or received, any number of ancillary data
5391 * objects can be specified by the msg_control and msg_controllen members of
5392 * the msghdr structure, because each object is preceded by
5393 * a cmsghdr structure defining the object's length (the cmsg_len member).
5394 * Historically Berkeley-derived implementations have passed only one object
5395 * at a time, but this API allows multiple objects to be
5396 * passed in a single call to sendmsg() or recvmsg(). The following example
5397 * shows two ancillary data objects in a control buffer.
5398 *
5399 * |<--------------------------- msg_controllen -------------------------->|
5400 * | |
5401 *
5402 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5403 *
5404 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5405 * | | |
5406 *
5407 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5408 *
5409 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5410 * | | | | |
5411 *
5412 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5413 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5414 *
5415 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5416 *
5417 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5418 * ^
5419 * |
5420 *
5421 * msg_control
5422 * points here
5423 */
5424SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5425 sctp_cmsgs_t *cmsgs)
5426{
5427 struct cmsghdr *cmsg;
5428
5429 for (cmsg = CMSG_FIRSTHDR(msg);
5430 cmsg != NULL;
5431 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5432 if (!CMSG_OK(msg, cmsg))
5433 return -EINVAL;
5434
5435 /* Should we parse this header or ignore? */
5436 if (cmsg->cmsg_level != IPPROTO_SCTP)
5437 continue;
5438
5439 /* Strictly check lengths following example in SCM code. */
5440 switch (cmsg->cmsg_type) {
5441 case SCTP_INIT:
5442 /* SCTP Socket API Extension
5443 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5444 *
5445 * This cmsghdr structure provides information for
5446 * initializing new SCTP associations with sendmsg().
5447 * The SCTP_INITMSG socket option uses this same data
5448 * structure. This structure is not used for
5449 * recvmsg().
5450 *
5451 * cmsg_level cmsg_type cmsg_data[]
5452 * ------------ ------------ ----------------------
5453 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5454 */
5455 if (cmsg->cmsg_len !=
5456 CMSG_LEN(sizeof(struct sctp_initmsg)))
5457 return -EINVAL;
5458 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5459 break;
5460
5461 case SCTP_SNDRCV:
5462 /* SCTP Socket API Extension
5463 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5464 *
5465 * This cmsghdr structure specifies SCTP options for
5466 * sendmsg() and describes SCTP header information
5467 * about a received message through recvmsg().
5468 *
5469 * cmsg_level cmsg_type cmsg_data[]
5470 * ------------ ------------ ----------------------
5471 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5472 */
5473 if (cmsg->cmsg_len !=
5474 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5475 return -EINVAL;
5476
5477 cmsgs->info =
5478 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5479
5480 /* Minimally, validate the sinfo_flags. */
5481 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005482 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5483 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 return -EINVAL;
5485 break;
5486
5487 default:
5488 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 }
5491 return 0;
5492}
5493
5494/*
5495 * Wait for a packet..
5496 * Note: This function is the same function as in core/datagram.c
5497 * with a few modifications to make lksctp work.
5498 */
5499static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5500{
5501 int error;
5502 DEFINE_WAIT(wait);
5503
5504 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5505
5506 /* Socket errors? */
5507 error = sock_error(sk);
5508 if (error)
5509 goto out;
5510
5511 if (!skb_queue_empty(&sk->sk_receive_queue))
5512 goto ready;
5513
5514 /* Socket shut down? */
5515 if (sk->sk_shutdown & RCV_SHUTDOWN)
5516 goto out;
5517
5518 /* Sequenced packets can come disconnected. If so we report the
5519 * problem.
5520 */
5521 error = -ENOTCONN;
5522
5523 /* Is there a good reason to think that we may receive some data? */
5524 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5525 goto out;
5526
5527 /* Handle signals. */
5528 if (signal_pending(current))
5529 goto interrupted;
5530
5531 /* Let another process have a go. Since we are going to sleep
5532 * anyway. Note: This may cause odd behaviors if the message
5533 * does not fit in the user's buffer, but this seems to be the
5534 * only way to honor MSG_DONTWAIT realistically.
5535 */
5536 sctp_release_sock(sk);
5537 *timeo_p = schedule_timeout(*timeo_p);
5538 sctp_lock_sock(sk);
5539
5540ready:
5541 finish_wait(sk->sk_sleep, &wait);
5542 return 0;
5543
5544interrupted:
5545 error = sock_intr_errno(*timeo_p);
5546
5547out:
5548 finish_wait(sk->sk_sleep, &wait);
5549 *err = error;
5550 return error;
5551}
5552
5553/* Receive a datagram.
5554 * Note: This is pretty much the same routine as in core/datagram.c
5555 * with a few changes to make lksctp work.
5556 */
5557static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5558 int noblock, int *err)
5559{
5560 int error;
5561 struct sk_buff *skb;
5562 long timeo;
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 timeo = sock_rcvtimeo(sk, noblock);
5565
5566 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5567 timeo, MAX_SCHEDULE_TIMEOUT);
5568
5569 do {
5570 /* Again only user level code calls this function,
5571 * so nothing interrupt level
5572 * will suddenly eat the receive_queue.
5573 *
5574 * Look at current nfs client by the way...
5575 * However, this function was corrent in any case. 8)
5576 */
5577 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005578 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 skb = skb_peek(&sk->sk_receive_queue);
5580 if (skb)
5581 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005582 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 } else {
5584 skb = skb_dequeue(&sk->sk_receive_queue);
5585 }
5586
5587 if (skb)
5588 return skb;
5589
Neil Horman6736dc32005-12-02 20:30:06 -08005590 /* Caller is allowed not to check sk->sk_err before calling. */
5591 error = sock_error(sk);
5592 if (error)
5593 goto no_packet;
5594
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (sk->sk_shutdown & RCV_SHUTDOWN)
5596 break;
5597
5598 /* User doesn't want to wait. */
5599 error = -EAGAIN;
5600 if (!timeo)
5601 goto no_packet;
5602 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5603
5604 return NULL;
5605
5606no_packet:
5607 *err = error;
5608 return NULL;
5609}
5610
5611/* If sndbuf has changed, wake up per association sndbuf waiters. */
5612static void __sctp_write_space(struct sctp_association *asoc)
5613{
5614 struct sock *sk = asoc->base.sk;
5615 struct socket *sock = sk->sk_socket;
5616
5617 if ((sctp_wspace(asoc) > 0) && sock) {
5618 if (waitqueue_active(&asoc->wait))
5619 wake_up_interruptible(&asoc->wait);
5620
5621 if (sctp_writeable(sk)) {
5622 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5623 wake_up_interruptible(sk->sk_sleep);
5624
5625 /* Note that we try to include the Async I/O support
5626 * here by modeling from the current TCP/UDP code.
5627 * We have not tested with it yet.
5628 */
5629 if (sock->fasync_list &&
5630 !(sk->sk_shutdown & SEND_SHUTDOWN))
5631 sock_wake_async(sock, 2, POLL_OUT);
5632 }
5633 }
5634}
5635
5636/* Do accounting for the sndbuf space.
5637 * Decrement the used sndbuf space of the corresponding association by the
5638 * data size which was just transmitted(freed).
5639 */
5640static void sctp_wfree(struct sk_buff *skb)
5641{
5642 struct sctp_association *asoc;
5643 struct sctp_chunk *chunk;
5644 struct sock *sk;
5645
5646 /* Get the saved chunk pointer. */
5647 chunk = *((struct sctp_chunk **)(skb->cb));
5648 asoc = chunk->asoc;
5649 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07005650 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5651 sizeof(struct sk_buff) +
5652 sizeof(struct sctp_chunk);
5653
Neil Horman4eb701d2005-04-28 12:02:04 -07005654 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5655
5656 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 __sctp_write_space(asoc);
5658
5659 sctp_association_put(asoc);
5660}
5661
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07005662/* Do accounting for the receive space on the socket.
5663 * Accounting for the association is done in ulpevent.c
5664 * We set this as a destructor for the cloned data skbs so that
5665 * accounting is done at the correct time.
5666 */
5667void sctp_sock_rfree(struct sk_buff *skb)
5668{
5669 struct sock *sk = skb->sk;
5670 struct sctp_ulpevent *event = sctp_skb2event(skb);
5671
5672 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
5673}
5674
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676/* Helper function to wait for space in the sndbuf. */
5677static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5678 size_t msg_len)
5679{
5680 struct sock *sk = asoc->base.sk;
5681 int err = 0;
5682 long current_timeo = *timeo_p;
5683 DEFINE_WAIT(wait);
5684
5685 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005686 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 /* Increment the association's refcnt. */
5689 sctp_association_hold(asoc);
5690
5691 /* Wait on the association specific sndbuf space. */
5692 for (;;) {
5693 prepare_to_wait_exclusive(&asoc->wait, &wait,
5694 TASK_INTERRUPTIBLE);
5695 if (!*timeo_p)
5696 goto do_nonblock;
5697 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5698 asoc->base.dead)
5699 goto do_error;
5700 if (signal_pending(current))
5701 goto do_interrupted;
5702 if (msg_len <= sctp_wspace(asoc))
5703 break;
5704
5705 /* Let another process have a go. Since we are going
5706 * to sleep anyway.
5707 */
5708 sctp_release_sock(sk);
5709 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07005710 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 sctp_lock_sock(sk);
5712
5713 *timeo_p = current_timeo;
5714 }
5715
5716out:
5717 finish_wait(&asoc->wait, &wait);
5718
5719 /* Release the association's refcnt. */
5720 sctp_association_put(asoc);
5721
5722 return err;
5723
5724do_error:
5725 err = -EPIPE;
5726 goto out;
5727
5728do_interrupted:
5729 err = sock_intr_errno(*timeo_p);
5730 goto out;
5731
5732do_nonblock:
5733 err = -EAGAIN;
5734 goto out;
5735}
5736
5737/* If socket sndbuf has changed, wake up all per association waiters. */
5738void sctp_write_space(struct sock *sk)
5739{
5740 struct sctp_association *asoc;
5741 struct list_head *pos;
5742
5743 /* Wake up the tasks in each wait queue. */
5744 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5745 asoc = list_entry(pos, struct sctp_association, asocs);
5746 __sctp_write_space(asoc);
5747 }
5748}
5749
5750/* Is there any sndbuf space available on the socket?
5751 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08005752 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 * associations on the same socket. For a UDP-style socket with
5754 * multiple associations, it is possible for it to be "unwriteable"
5755 * prematurely. I assume that this is acceptable because
5756 * a premature "unwriteable" is better than an accidental "writeable" which
5757 * would cause an unwanted block under certain circumstances. For the 1-1
5758 * UDP-style sockets or TCP-style sockets, this code should work.
5759 * - Daisy
5760 */
5761static int sctp_writeable(struct sock *sk)
5762{
5763 int amt = 0;
5764
Neil Horman9bffc4a2005-12-19 14:24:40 -08005765 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 if (amt < 0)
5767 amt = 0;
5768 return amt;
5769}
5770
5771/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5772 * returns immediately with EINPROGRESS.
5773 */
5774static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5775{
5776 struct sock *sk = asoc->base.sk;
5777 int err = 0;
5778 long current_timeo = *timeo_p;
5779 DEFINE_WAIT(wait);
5780
5781 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5782 (long)(*timeo_p));
5783
5784 /* Increment the association's refcnt. */
5785 sctp_association_hold(asoc);
5786
5787 for (;;) {
5788 prepare_to_wait_exclusive(&asoc->wait, &wait,
5789 TASK_INTERRUPTIBLE);
5790 if (!*timeo_p)
5791 goto do_nonblock;
5792 if (sk->sk_shutdown & RCV_SHUTDOWN)
5793 break;
5794 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5795 asoc->base.dead)
5796 goto do_error;
5797 if (signal_pending(current))
5798 goto do_interrupted;
5799
5800 if (sctp_state(asoc, ESTABLISHED))
5801 break;
5802
5803 /* Let another process have a go. Since we are going
5804 * to sleep anyway.
5805 */
5806 sctp_release_sock(sk);
5807 current_timeo = schedule_timeout(current_timeo);
5808 sctp_lock_sock(sk);
5809
5810 *timeo_p = current_timeo;
5811 }
5812
5813out:
5814 finish_wait(&asoc->wait, &wait);
5815
5816 /* Release the association's refcnt. */
5817 sctp_association_put(asoc);
5818
5819 return err;
5820
5821do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08005822 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 err = -ETIMEDOUT;
5824 else
5825 err = -ECONNREFUSED;
5826 goto out;
5827
5828do_interrupted:
5829 err = sock_intr_errno(*timeo_p);
5830 goto out;
5831
5832do_nonblock:
5833 err = -EINPROGRESS;
5834 goto out;
5835}
5836
5837static int sctp_wait_for_accept(struct sock *sk, long timeo)
5838{
5839 struct sctp_endpoint *ep;
5840 int err = 0;
5841 DEFINE_WAIT(wait);
5842
5843 ep = sctp_sk(sk)->ep;
5844
5845
5846 for (;;) {
5847 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5848 TASK_INTERRUPTIBLE);
5849
5850 if (list_empty(&ep->asocs)) {
5851 sctp_release_sock(sk);
5852 timeo = schedule_timeout(timeo);
5853 sctp_lock_sock(sk);
5854 }
5855
5856 err = -EINVAL;
5857 if (!sctp_sstate(sk, LISTENING))
5858 break;
5859
5860 err = 0;
5861 if (!list_empty(&ep->asocs))
5862 break;
5863
5864 err = sock_intr_errno(timeo);
5865 if (signal_pending(current))
5866 break;
5867
5868 err = -EAGAIN;
5869 if (!timeo)
5870 break;
5871 }
5872
5873 finish_wait(sk->sk_sleep, &wait);
5874
5875 return err;
5876}
5877
5878void sctp_wait_for_close(struct sock *sk, long timeout)
5879{
5880 DEFINE_WAIT(wait);
5881
5882 do {
5883 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5884 if (list_empty(&sctp_sk(sk)->ep->asocs))
5885 break;
5886 sctp_release_sock(sk);
5887 timeout = schedule_timeout(timeout);
5888 sctp_lock_sock(sk);
5889 } while (!signal_pending(current) && timeout);
5890
5891 finish_wait(sk->sk_sleep, &wait);
5892}
5893
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07005894static void sctp_sock_rfree_frag(struct sk_buff *skb)
5895{
5896 struct sk_buff *frag;
5897
5898 if (!skb->data_len)
5899 goto done;
5900
5901 /* Don't forget the fragments. */
5902 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5903 sctp_sock_rfree_frag(frag);
5904
5905done:
5906 sctp_sock_rfree(skb);
5907}
5908
5909static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
5910{
5911 struct sk_buff *frag;
5912
5913 if (!skb->data_len)
5914 goto done;
5915
5916 /* Don't forget the fragments. */
5917 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
5918 sctp_skb_set_owner_r_frag(frag, sk);
5919
5920done:
5921 sctp_skb_set_owner_r(skb, sk);
5922}
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924/* Populate the fields of the newsk from the oldsk and migrate the assoc
5925 * and its messages to the newsk.
5926 */
5927static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5928 struct sctp_association *assoc,
5929 sctp_socket_type_t type)
5930{
5931 struct sctp_sock *oldsp = sctp_sk(oldsk);
5932 struct sctp_sock *newsp = sctp_sk(newsk);
5933 struct sctp_bind_bucket *pp; /* hash list port iterator */
5934 struct sctp_endpoint *newep = newsp->ep;
5935 struct sk_buff *skb, *tmp;
5936 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005937 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 /* Migrate socket buffer sizes and all the socket level options to the
5940 * new socket.
5941 */
5942 newsk->sk_sndbuf = oldsk->sk_sndbuf;
5943 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5944 /* Brute force copy old sctp opt. */
5945 inet_sk_copy_descendant(newsk, oldsk);
5946
5947 /* Restore the ep value that was overwritten with the above structure
5948 * copy.
5949 */
5950 newsp->ep = newep;
5951 newsp->hmac = NULL;
5952
5953 /* Hook this new socket in to the bind_hash list. */
5954 pp = sctp_sk(oldsk)->bind_hash;
5955 sk_add_bind_node(newsk, &pp->owner);
5956 sctp_sk(newsk)->bind_hash = pp;
5957 inet_sk(newsk)->num = inet_sk(oldsk)->num;
5958
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005959 /* Copy the bind_addr list from the original endpoint to the new
5960 * endpoint so that we can handle restarts properly
5961 */
Vladislav Yasevicheb5fa392006-08-22 00:23:13 -07005962 if (PF_INET6 == assoc->base.sk->sk_family)
5963 flags = SCTP_ADDR6_ALLOWED;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07005964 if (assoc->peer.ipv4_address)
5965 flags |= SCTP_ADDR4_PEERSUPP;
5966 if (assoc->peer.ipv6_address)
5967 flags |= SCTP_ADDR6_PEERSUPP;
5968 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5969 &oldsp->ep->base.bind_addr,
5970 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 /* Move any messages in the old socket's receive queue that are for the
5973 * peeled off association to the new socket's receive queue.
5974 */
5975 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5976 event = sctp_skb2event(skb);
5977 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07005978 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07005979 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07005981 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 }
5983 }
5984
5985 /* Clean up any messages pending delivery due to partial
5986 * delivery. Three cases:
5987 * 1) No partial deliver; no work.
5988 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5989 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5990 */
5991 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005992 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005994 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 struct sk_buff_head *queue;
5996
5997 /* Decide which queue to move pd_lobby skbs to. */
5998 if (assoc->ulpq.pd_mode) {
5999 queue = &newsp->pd_lobby;
6000 } else
6001 queue = &newsk->sk_receive_queue;
6002
6003 /* Walk through the pd_lobby, looking for skbs that
6004 * need moved to the new socket.
6005 */
6006 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6007 event = sctp_skb2event(skb);
6008 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006009 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006010 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006012 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 }
6014 }
6015
6016 /* Clear up any skbs waiting for the partial
6017 * delivery to finish.
6018 */
6019 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006020 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
6022 }
6023
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006024 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6025 sctp_sock_rfree_frag(skb);
6026 sctp_skb_set_owner_r_frag(skb, newsk);
6027 }
6028
6029 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6030 sctp_sock_rfree_frag(skb);
6031 sctp_skb_set_owner_r_frag(skb, newsk);
6032 }
6033
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 /* Set the type of socket to indicate that it is peeled off from the
6035 * original UDP-style socket or created with the accept() call on a
6036 * TCP-style socket..
6037 */
6038 newsp->type = type;
6039
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006040 /* Mark the new socket "in-use" by the user so that any packets
6041 * that may arrive on the association after we've moved it are
6042 * queued to the backlog. This prevents a potential race between
6043 * backlog processing on the old socket and new-packet processing
6044 * on the new socket.
6045 */
6046 sctp_lock_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 /* If the association on the newsk is already closed before accept()
6050 * is called, set RCV_SHUTDOWN flag.
6051 */
6052 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6053 newsk->sk_shutdown |= RCV_SHUTDOWN;
6054
6055 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006056 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057}
6058
6059/* This proto struct describes the ULP interface for SCTP. */
6060struct proto sctp_prot = {
6061 .name = "SCTP",
6062 .owner = THIS_MODULE,
6063 .close = sctp_close,
6064 .connect = sctp_connect,
6065 .disconnect = sctp_disconnect,
6066 .accept = sctp_accept,
6067 .ioctl = sctp_ioctl,
6068 .init = sctp_init_sock,
6069 .destroy = sctp_destroy_sock,
6070 .shutdown = sctp_shutdown,
6071 .setsockopt = sctp_setsockopt,
6072 .getsockopt = sctp_getsockopt,
6073 .sendmsg = sctp_sendmsg,
6074 .recvmsg = sctp_recvmsg,
6075 .bind = sctp_bind,
6076 .backlog_rcv = sctp_backlog_rcv,
6077 .hash = sctp_hash,
6078 .unhash = sctp_unhash,
6079 .get_port = sctp_get_port,
6080 .obj_size = sizeof(struct sctp_sock),
6081};
6082
6083#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6084struct proto sctpv6_prot = {
6085 .name = "SCTPv6",
6086 .owner = THIS_MODULE,
6087 .close = sctp_close,
6088 .connect = sctp_connect,
6089 .disconnect = sctp_disconnect,
6090 .accept = sctp_accept,
6091 .ioctl = sctp_ioctl,
6092 .init = sctp_init_sock,
6093 .destroy = sctp_destroy_sock,
6094 .shutdown = sctp_shutdown,
6095 .setsockopt = sctp_setsockopt,
6096 .getsockopt = sctp_getsockopt,
6097 .sendmsg = sctp_sendmsg,
6098 .recvmsg = sctp_recvmsg,
6099 .bind = sctp_bind,
6100 .backlog_rcv = sctp_backlog_rcv,
6101 .hash = sctp_hash,
6102 .unhash = sctp_unhash,
6103 .get_port = sctp_get_port,
6104 .obj_size = sizeof(struct sctp6_sock),
6105};
6106#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */