blob: f3e1a9c811ad342d64e86fa93ec643322982e235 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
18 * The SCTP reference implementation is free software;
19 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
24 * The SCTP reference implementation is distributed in the hope that it
25 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with GNU CC; see the file COPYING. If not, write to
32 * the Free Software Foundation, 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
34 *
35 * Please send any bug reports or fixes you make to the
36 * email address(es):
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 *
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
41 *
42 * Written or modified by:
43 * La Monte H.P. Yarroll <piggy@acm.org>
44 * Narasimha Budihal <narsi@refcode.org>
45 * Karl Knutson <karl@athena.chicago.il.us>
46 * Jon Grimm <jgrimm@us.ibm.com>
47 * Xingang Guo <xingang.guo@intel.com>
48 * Daisy Chang <daisyc@us.ibm.com>
49 * Sridhar Samudrala <samudrala@us.ibm.com>
50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
51 * Ardelle Fan <ardelle.fan@intel.com>
52 * Ryan Layer <rmlayer@us.ibm.com>
53 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
54 * Kevin Gao <kevin.gao@intel.com>
55 *
56 * Any bugs reported given to us we will try to fix... any fixes shared will
57 * be incorporated into the next SCTP release.
58 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/types.h>
61#include <linux/kernel.h>
62#include <linux/wait.h>
63#include <linux/time.h>
64#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080065#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/fcntl.h>
67#include <linux/poll.h>
68#include <linux/init.h>
69#include <linux/crypto.h>
70
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
76
77#include <linux/socket.h> /* for sa_family_t */
78#include <net/sock.h>
79#include <net/sctp/sctp.h>
80#include <net/sctp/sm.h>
81
82/* WARNING: Please do not remove the SCTP_STATIC attribute to
83 * any of the functions below as they are used to export functions
84 * used by a project regression testsuite.
85 */
86
87/* Forward declarations for internal helper functions. */
88static int sctp_writeable(struct sock *sk);
89static void sctp_wfree(struct sk_buff *skb);
90static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
91 size_t msg_len);
92static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
93static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
94static int sctp_wait_for_accept(struct sock *sk, long timeo);
95static void sctp_wait_for_close(struct sock *sk, long timeo);
96static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
97 union sctp_addr *addr, int len);
98static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
99static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102static int sctp_send_asconf(struct sctp_association *asoc,
103 struct sctp_chunk *chunk);
104static int sctp_do_bind(struct sock *, union sctp_addr *, int);
105static int sctp_autobind(struct sock *sk);
106static void sctp_sock_migrate(struct sock *, struct sock *,
107 struct sctp_association *, sctp_socket_type_t);
108static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
109
Neil Horman4d93df02007-08-15 16:07:44 -0700110extern struct kmem_cache *sctp_bucket_cachep;
111extern int sysctl_sctp_mem[3];
112extern int sysctl_sctp_rmem[3];
113extern int sysctl_sctp_wmem[3];
114
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200115static int sctp_memory_pressure;
116static atomic_t sctp_memory_allocated;
117static atomic_t sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700118
119static void sctp_enter_memory_pressure(void)
120{
121 sctp_memory_pressure = 1;
122}
123
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Get the sndbuf space available at the time on the association. */
126static inline int sctp_wspace(struct sctp_association *asoc)
127{
Neil Horman4d93df02007-08-15 16:07:44 -0700128 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Neil Horman4d93df02007-08-15 16:07:44 -0700130 if (asoc->ep->sndbuf_policy)
131 amt = asoc->sndbuf_used;
132 else
133 amt = atomic_read(&asoc->base.sk->sk_wmem_alloc);
134
135 if (amt >= asoc->base.sk->sk_sndbuf) {
136 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
137 amt = 0;
138 else {
139 amt = sk_stream_wspace(asoc->base.sk);
140 if (amt < 0)
141 amt = 0;
142 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700143 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700144 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return amt;
147}
148
149/* Increment the used sndbuf space count of the corresponding association by
150 * the size of the outgoing data chunk.
151 * Also, set the skb destructor for sndbuf accounting later.
152 *
153 * Since it is always 1-1 between chunk and skb, and also a new skb is always
154 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
155 * destructor in the data chunk skb for the purpose of the sndbuf space
156 * tracking.
157 */
158static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
159{
160 struct sctp_association *asoc = chunk->asoc;
161 struct sock *sk = asoc->base.sk;
162
163 /* The sndbuf space is tracked per association. */
164 sctp_association_hold(asoc);
165
Neil Horman4eb701d2005-04-28 12:02:04 -0700166 skb_set_owner_w(chunk->skb, sk);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 chunk->skb->destructor = sctp_wfree;
169 /* Save the chunk pointer in skb for sctp_wfree to use later. */
170 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
171
Neil Horman4eb701d2005-04-28 12:02:04 -0700172 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
173 sizeof(struct sk_buff) +
174 sizeof(struct sctp_chunk);
175
Neil Horman4eb701d2005-04-28 12:02:04 -0700176 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -0700177 sk_charge_skb(sk, chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/* Verify that this is a valid address. */
181static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
182 int len)
183{
184 struct sctp_af *af;
185
186 /* Verify basic sockaddr. */
187 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
188 if (!af)
189 return -EINVAL;
190
191 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700192 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EINVAL;
194
195 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
196 return -EINVAL;
197
198 return 0;
199}
200
201/* Look up the association by its id. If this is not a UDP-style
202 * socket, the ID field is always ignored.
203 */
204struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
205{
206 struct sctp_association *asoc = NULL;
207
208 /* If this is not a UDP-style socket, assoc id should be ignored. */
209 if (!sctp_style(sk, UDP)) {
210 /* Return NULL if the socket state is not ESTABLISHED. It
211 * could be a TCP-style listening socket or a socket which
212 * hasn't yet called connect() to establish an association.
213 */
214 if (!sctp_sstate(sk, ESTABLISHED))
215 return NULL;
216
217 /* Get the first and the only association from the list. */
218 if (!list_empty(&sctp_sk(sk)->ep->asocs))
219 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
220 struct sctp_association, asocs);
221 return asoc;
222 }
223
224 /* Otherwise this is a UDP-style socket. */
225 if (!id || (id == (sctp_assoc_t)-1))
226 return NULL;
227
228 spin_lock_bh(&sctp_assocs_id_lock);
229 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
230 spin_unlock_bh(&sctp_assocs_id_lock);
231
232 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
233 return NULL;
234
235 return asoc;
236}
237
238/* Look up the transport from an address and an assoc id. If both address and
239 * id are specified, the associations matching the address and the id should be
240 * the same.
241 */
242static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
243 struct sockaddr_storage *addr,
244 sctp_assoc_t id)
245{
246 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
247 struct sctp_transport *transport;
248 union sctp_addr *laddr = (union sctp_addr *)addr;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800251 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!addr_asoc)
255 return NULL;
256
257 id_asoc = sctp_id2assoc(sk, id);
258 if (id_asoc && (id_asoc != addr_asoc))
259 return NULL;
260
261 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
262 (union sctp_addr *)addr);
263
264 return transport;
265}
266
267/* API 3.1.2 bind() - UDP Style Syntax
268 * The syntax of bind() is,
269 *
270 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
271 *
272 * sd - the socket descriptor returned by socket().
273 * addr - the address structure (struct sockaddr_in or struct
274 * sockaddr_in6 [RFC 2553]),
275 * addr_len - the size of the address structure.
276 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700277SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 int retval = 0;
280
281 sctp_lock_sock(sk);
282
Frank Filz3f7a87d2005-06-20 13:14:57 -0700283 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
284 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Disallow binding twice. */
287 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700288 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 addr_len);
290 else
291 retval = -EINVAL;
292
293 sctp_release_sock(sk);
294
295 return retval;
296}
297
298static long sctp_get_port_local(struct sock *, union sctp_addr *);
299
300/* Verify this is a valid sockaddr. */
301static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
302 union sctp_addr *addr, int len)
303{
304 struct sctp_af *af;
305
306 /* Check minimum size. */
307 if (len < sizeof (struct sockaddr))
308 return NULL;
309
310 /* Does this PF support this AF? */
311 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
312 return NULL;
313
314 /* If we get this far, af is valid. */
315 af = sctp_get_af_specific(addr->sa.sa_family);
316
317 if (len < af->sockaddr_len)
318 return NULL;
319
320 return af;
321}
322
323/* Bind a local address either to an endpoint or to an association. */
324SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
325{
326 struct sctp_sock *sp = sctp_sk(sk);
327 struct sctp_endpoint *ep = sp->ep;
328 struct sctp_bind_addr *bp = &ep->base.bind_addr;
329 struct sctp_af *af;
330 unsigned short snum;
331 int ret = 0;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Common sockaddr verification. */
334 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700335 if (!af) {
336 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
337 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700339 }
340
341 snum = ntohs(addr->v4.sin_port);
342
343 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
344 ", port: %d, new port: %d, len: %d)\n",
345 sk,
346 addr,
347 bp->port, snum,
348 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* PF specific bind() address verification. */
351 if (!sp->pf->bind_verify(sp, addr))
352 return -EADDRNOTAVAIL;
353
Vlad Yasevich8b358052007-05-15 17:14:58 -0400354 /* We must either be unbound, or bind to the same port.
355 * It's OK to allow 0 ports if we are already bound.
356 * We'll just inhert an already bound port in this case
357 */
358 if (bp->port) {
359 if (!snum)
360 snum = bp->port;
361 else if (snum != bp->port) {
362 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 " New port %d does not match existing port "
364 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400365 return -EINVAL;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
370 return -EACCES;
371
372 /* Make sure we are allowed to bind here.
373 * The function sctp_get_port_local() does duplicate address
374 * detection.
375 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900376 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if ((ret = sctp_get_port_local(sk, addr))) {
378 if (ret == (long) sk) {
379 /* This endpoint has a conflicting address. */
380 return -EINVAL;
381 } else {
382 return -EADDRINUSE;
383 }
384 }
385
386 /* Refresh ephemeral port. */
387 if (!bp->port)
388 bp->port = inet_sk(sk)->num;
389
Vlad Yasevich559cf712007-09-16 16:03:28 -0700390 /* Add the address to the bind address list.
391 * Use GFP_ATOMIC since BHs will be disabled.
392 */
Al Viro5ab7b852006-11-20 17:10:38 -0800393 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* Copy back into socket for getsockname() use. */
396 if (!ret) {
397 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
398 af->to_sk_saddr(addr, sk);
399 }
400
401 return ret;
402}
403
404 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
405 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900406 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900408 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900410 * subsequent ASCONF. Note this restriction binds each side, so at any
411 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * from each endpoint).
413 */
414static int sctp_send_asconf(struct sctp_association *asoc,
415 struct sctp_chunk *chunk)
416{
417 int retval = 0;
418
419 /* If there is an outstanding ASCONF chunk, queue it for later
420 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700423 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
427 /* Hold the chunk until an ASCONF_ACK is received. */
428 sctp_chunk_hold(chunk);
429 retval = sctp_primitive_ASCONF(asoc, chunk);
430 if (retval)
431 sctp_chunk_free(chunk);
432 else
433 asoc->addip_last_asconf = chunk;
434
435out:
436 return retval;
437}
438
439/* Add a list of addresses as bind addresses to local endpoint or
440 * association.
441 *
442 * Basically run through each address specified in the addrs/addrcnt
443 * array/length pair, determine if it is IPv6 or IPv4 and call
444 * sctp_do_bind() on it.
445 *
446 * If any of them fails, then the operation will be reversed and the
447 * ones that were added will be removed.
448 *
449 * Only sctp_setsockopt_bindx() is supposed to call this function.
450 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200451static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int cnt;
454 int retval = 0;
455 void *addr_buf;
456 struct sockaddr *sa_addr;
457 struct sctp_af *af;
458
459 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
460 sk, addrs, addrcnt);
461
462 addr_buf = addrs;
463 for (cnt = 0; cnt < addrcnt; cnt++) {
464 /* The list may contain either IPv4 or IPv6 address;
465 * determine the address length for walking thru the list.
466 */
467 sa_addr = (struct sockaddr *)addr_buf;
468 af = sctp_get_af_specific(sa_addr->sa_family);
469 if (!af) {
470 retval = -EINVAL;
471 goto err_bindx_add;
472 }
473
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900474 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 af->sockaddr_len);
476
477 addr_buf += af->sockaddr_len;
478
479err_bindx_add:
480 if (retval < 0) {
481 /* Failed. Cleanup the ones that have been added */
482 if (cnt > 0)
483 sctp_bindx_rem(sk, addrs, cnt);
484 return retval;
485 }
486 }
487
488 return retval;
489}
490
491/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
492 * associations that are part of the endpoint indicating that a list of local
493 * addresses are added to the endpoint.
494 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900495 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * association, we do not send the chunk for that association. But it will not
497 * affect other associations.
498 *
499 * Only sctp_setsockopt_bindx() is supposed to call this function.
500 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900501static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct sockaddr *addrs,
503 int addrcnt)
504{
505 struct sctp_sock *sp;
506 struct sctp_endpoint *ep;
507 struct sctp_association *asoc;
508 struct sctp_bind_addr *bp;
509 struct sctp_chunk *chunk;
510 struct sctp_sockaddr_entry *laddr;
511 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700512 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 void *addr_buf;
514 struct sctp_af *af;
515 struct list_head *pos;
516 struct list_head *p;
517 int i;
518 int retval = 0;
519
520 if (!sctp_addip_enable)
521 return retval;
522
523 sp = sctp_sk(sk);
524 ep = sp->ep;
525
526 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
527 __FUNCTION__, sk, addrs, addrcnt);
528
529 list_for_each(pos, &ep->asocs) {
530 asoc = list_entry(pos, struct sctp_association, asocs);
531
532 if (!asoc->peer.asconf_capable)
533 continue;
534
535 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
536 continue;
537
538 if (!sctp_state(asoc, ESTABLISHED))
539 continue;
540
541 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900542 * in the bind address list of the association. If so,
543 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * other associations.
545 */
546 addr_buf = addrs;
547 for (i = 0; i < addrcnt; i++) {
548 addr = (union sctp_addr *)addr_buf;
549 af = sctp_get_af_specific(addr->v4.sin_family);
550 if (!af) {
551 retval = -EINVAL;
552 goto out;
553 }
554
555 if (sctp_assoc_lookup_laddr(asoc, addr))
556 break;
557
558 addr_buf += af->sockaddr_len;
559 }
560 if (i < addrcnt)
561 continue;
562
Vlad Yasevich559cf712007-09-16 16:03:28 -0700563 /* Use the first valid address in bind addr list of
564 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 bp = &asoc->base.bind_addr;
567 p = bp->address_list.next;
568 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800569 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 addrcnt, SCTP_PARAM_ADD_IP);
571 if (!chunk) {
572 retval = -ENOMEM;
573 goto out;
574 }
575
576 retval = sctp_send_asconf(asoc, chunk);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700577 if (retval)
578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700580 /* Add the new addresses to the bind address list with
581 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700583 addr_buf = addrs;
584 for (i = 0; i < addrcnt; i++) {
585 addr = (union sctp_addr *)addr_buf;
586 af = sctp_get_af_specific(addr->v4.sin_family);
587 memcpy(&saveaddr, addr, af->sockaddr_len);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700588 retval = sctp_add_bind_addr(bp, &saveaddr, 0,
589 GFP_ATOMIC);
590 addr_buf += af->sockaddr_len;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594out:
595 return retval;
596}
597
598/* Remove a list of addresses from bind addresses list. Do not remove the
599 * last address.
600 *
601 * Basically run through each address specified in the addrs/addrcnt
602 * array/length pair, determine if it is IPv6 or IPv4 and call
603 * sctp_del_bind() on it.
604 *
605 * If any of them fails, then the operation will be reversed and the
606 * ones that were removed will be added back.
607 *
608 * At least one address has to be left; if only one address is
609 * available, the operation will return -EBUSY.
610 *
611 * Only sctp_setsockopt_bindx() is supposed to call this function.
612 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200613static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct sctp_sock *sp = sctp_sk(sk);
616 struct sctp_endpoint *ep = sp->ep;
617 int cnt;
618 struct sctp_bind_addr *bp = &ep->base.bind_addr;
619 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800621 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct sctp_af *af;
623
624 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
625 sk, addrs, addrcnt);
626
627 addr_buf = addrs;
628 for (cnt = 0; cnt < addrcnt; cnt++) {
629 /* If the bind address list is empty or if there is only one
630 * bind address, there is nothing more to be removed (we need
631 * at least one address here).
632 */
633 if (list_empty(&bp->address_list) ||
634 (sctp_list_single_entry(&bp->address_list))) {
635 retval = -EBUSY;
636 goto err_bindx_rem;
637 }
638
Al Viroc9a08502006-11-20 17:07:48 -0800639 sa_addr = (union sctp_addr *)addr_buf;
640 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!af) {
642 retval = -EINVAL;
643 goto err_bindx_rem;
644 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700645
646 if (!af->addr_valid(sa_addr, sp, NULL)) {
647 retval = -EADDRNOTAVAIL;
648 goto err_bindx_rem;
649 }
650
Al Viroc9a08502006-11-20 17:07:48 -0800651 if (sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 retval = -EINVAL;
653 goto err_bindx_rem;
654 }
655
656 /* FIXME - There is probably a need to check if sk->sk_saddr and
657 * sk->sk_rcv_addr are currently set to one of the addresses to
658 * be removed. This is something which needs to be looked into
659 * when we are fixing the outstanding issues with multi-homing
660 * socket routing and failover schemes. Refer to comments in
661 * sctp_do_bind(). -daisy
662 */
Vlad Yasevich559cf712007-09-16 16:03:28 -0700663 retval = sctp_del_bind_addr(bp, sa_addr, call_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 addr_buf += af->sockaddr_len;
666err_bindx_rem:
667 if (retval < 0) {
668 /* Failed. Add the ones that has been removed back */
669 if (cnt > 0)
670 sctp_bindx_add(sk, addrs, cnt);
671 return retval;
672 }
673 }
674
675 return retval;
676}
677
678/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
679 * the associations that are part of the endpoint indicating that a list of
680 * local addresses are removed from the endpoint.
681 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900682 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * association, we do not send the chunk for that association. But it will not
684 * affect other associations.
685 *
686 * Only sctp_setsockopt_bindx() is supposed to call this function.
687 */
688static int sctp_send_asconf_del_ip(struct sock *sk,
689 struct sockaddr *addrs,
690 int addrcnt)
691{
692 struct sctp_sock *sp;
693 struct sctp_endpoint *ep;
694 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700695 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct sctp_bind_addr *bp;
697 struct sctp_chunk *chunk;
698 union sctp_addr *laddr;
699 void *addr_buf;
700 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700701 struct list_head *pos, *pos1;
702 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int i;
704 int retval = 0;
705
706 if (!sctp_addip_enable)
707 return retval;
708
709 sp = sctp_sk(sk);
710 ep = sp->ep;
711
712 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
713 __FUNCTION__, sk, addrs, addrcnt);
714
715 list_for_each(pos, &ep->asocs) {
716 asoc = list_entry(pos, struct sctp_association, asocs);
717
718 if (!asoc->peer.asconf_capable)
719 continue;
720
721 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
722 continue;
723
724 if (!sctp_state(asoc, ESTABLISHED))
725 continue;
726
727 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900728 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * If so, do not send the asconf chunk to its peer, but
730 * continue with other associations.
731 */
732 addr_buf = addrs;
733 for (i = 0; i < addrcnt; i++) {
734 laddr = (union sctp_addr *)addr_buf;
735 af = sctp_get_af_specific(laddr->v4.sin_family);
736 if (!af) {
737 retval = -EINVAL;
738 goto out;
739 }
740
741 if (!sctp_assoc_lookup_laddr(asoc, laddr))
742 break;
743
744 addr_buf += af->sockaddr_len;
745 }
746 if (i < addrcnt)
747 continue;
748
749 /* Find one address in the association's bind address list
750 * that is not in the packed array of addresses. This is to
751 * make sure that we do not delete all the addresses in the
752 * association.
753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 bp = &asoc->base.bind_addr;
755 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
756 addrcnt, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!laddr)
758 continue;
759
Vlad Yasevich559cf712007-09-16 16:03:28 -0700760 /* We do not need RCU protection throughout this loop
761 * because this is done under a socket lock from the
762 * setsockopt call.
763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
765 SCTP_PARAM_DEL_IP);
766 if (!chunk) {
767 retval = -ENOMEM;
768 goto out;
769 }
770
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700771 /* Reset use_as_src flag for the addresses in the bind address
772 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700774 addr_buf = addrs;
775 for (i = 0; i < addrcnt; i++) {
776 laddr = (union sctp_addr *)addr_buf;
777 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700778 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800779 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700780 saddr->use_as_src = 0;
781 }
782 addr_buf += af->sockaddr_len;
783 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700784
785 /* Update the route and saddr entries for all the transports
786 * as some of the addresses in the bind address list are
787 * about to be deleted and cannot be used as source addresses.
788 */
789 list_for_each(pos1, &asoc->peer.transport_addr_list) {
790 transport = list_entry(pos1, struct sctp_transport,
791 transports);
792 dst_release(transport->dst);
793 sctp_transport_route(transport, NULL,
794 sctp_sk(asoc->base.sk));
795 }
796
797 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799out:
800 return retval;
801}
802
803/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
804 *
805 * API 8.1
806 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
807 * int flags);
808 *
809 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
810 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
811 * or IPv6 addresses.
812 *
813 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
814 * Section 3.1.2 for this usage.
815 *
816 * addrs is a pointer to an array of one or more socket addresses. Each
817 * address is contained in its appropriate structure (i.e. struct
818 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700819 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * representation is termed a "packed array" of addresses). The caller
821 * specifies the number of addresses in the array with addrcnt.
822 *
823 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
824 * -1, and sets errno to the appropriate error code.
825 *
826 * For SCTP, the port given in each socket address must be the same, or
827 * sctp_bindx() will fail, setting errno to EINVAL.
828 *
829 * The flags parameter is formed from the bitwise OR of zero or more of
830 * the following currently defined flags:
831 *
832 * SCTP_BINDX_ADD_ADDR
833 *
834 * SCTP_BINDX_REM_ADDR
835 *
836 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
837 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
838 * addresses from the association. The two flags are mutually exclusive;
839 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
840 * not remove all addresses from an association; sctp_bindx() will
841 * reject such an attempt with EINVAL.
842 *
843 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
844 * additional addresses with an endpoint after calling bind(). Or use
845 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
846 * socket is associated with so that no new association accepted will be
847 * associated with those addresses. If the endpoint supports dynamic
848 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
849 * endpoint to send the appropriate message to the peer to change the
850 * peers address lists.
851 *
852 * Adding and removing addresses from a connected association is
853 * optional functionality. Implementations that do not support this
854 * functionality should return EOPNOTSUPP.
855 *
856 * Basically do nothing but copying the addresses from user to kernel
857 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700858 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
859 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
861 * We don't use copy_from_user() for optimization: we first do the
862 * sanity checks (buffer size -fast- and access check-healthy
863 * pointer); if all of those succeed, then we can alloc the memory
864 * (expensive operation) needed to copy the data to kernel. Then we do
865 * the copying without checking the user space area
866 * (__copy_from_user()).
867 *
868 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
869 * it.
870 *
871 * sk The sk of the socket
872 * addrs The pointer to the addresses in user land
873 * addrssize Size of the addrs buffer
874 * op Operation to perform (add or remove, see the flags of
875 * sctp_bindx)
876 *
877 * Returns 0 if ok, <0 errno code on error.
878 */
879SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
880 struct sockaddr __user *addrs,
881 int addrs_size, int op)
882{
883 struct sockaddr *kaddrs;
884 int err;
885 int addrcnt = 0;
886 int walk_size = 0;
887 struct sockaddr *sa_addr;
888 void *addr_buf;
889 struct sctp_af *af;
890
891 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
892 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
893
894 if (unlikely(addrs_size <= 0))
895 return -EINVAL;
896
897 /* Check the user passed a healthy pointer. */
898 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
899 return -EFAULT;
900
901 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800902 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (unlikely(!kaddrs))
904 return -ENOMEM;
905
906 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
907 kfree(kaddrs);
908 return -EFAULT;
909 }
910
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900911 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 addr_buf = kaddrs;
913 while (walk_size < addrs_size) {
914 sa_addr = (struct sockaddr *)addr_buf;
915 af = sctp_get_af_specific(sa_addr->sa_family);
916
917 /* If the address family is not supported or if this address
918 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
921 kfree(kaddrs);
922 return -EINVAL;
923 }
924 addrcnt++;
925 addr_buf += af->sockaddr_len;
926 walk_size += af->sockaddr_len;
927 }
928
929 /* Do the work. */
930 switch (op) {
931 case SCTP_BINDX_ADD_ADDR:
932 err = sctp_bindx_add(sk, kaddrs, addrcnt);
933 if (err)
934 goto out;
935 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
936 break;
937
938 case SCTP_BINDX_REM_ADDR:
939 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
940 if (err)
941 goto out;
942 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
943 break;
944
945 default:
946 err = -EINVAL;
947 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950out:
951 kfree(kaddrs);
952
953 return err;
954}
955
Frank Filz3f7a87d2005-06-20 13:14:57 -0700956/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
957 *
958 * Common routine for handling connect() and sctp_connectx().
959 * Connect will come in with just a single address.
960 */
961static int __sctp_connect(struct sock* sk,
962 struct sockaddr *kaddrs,
963 int addrs_size)
964{
965 struct sctp_sock *sp;
966 struct sctp_endpoint *ep;
967 struct sctp_association *asoc = NULL;
968 struct sctp_association *asoc2;
969 struct sctp_transport *transport;
970 union sctp_addr to;
971 struct sctp_af *af;
972 sctp_scope_t scope;
973 long timeo;
974 int err = 0;
975 int addrcnt = 0;
976 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -0400977 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700978 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -0700979 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -0400980 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700981
982 sp = sctp_sk(sk);
983 ep = sp->ep;
984
985 /* connect() cannot be done on a socket that is already in ESTABLISHED
986 * state - UDP-style peeled off socket or a TCP-style socket that
987 * is already connected.
988 * It cannot be done even on a TCP-style listening socket.
989 */
990 if (sctp_sstate(sk, ESTABLISHED) ||
991 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
992 err = -EISCONN;
993 goto out_free;
994 }
995
996 /* Walk through the addrs buffer and count the number of addresses. */
997 addr_buf = kaddrs;
998 while (walk_size < addrs_size) {
Al Viro4bdf4b52006-11-20 17:10:20 -0800999 sa_addr = (union sctp_addr *)addr_buf;
1000 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001001 port = ntohs(sa_addr->v4.sin_port);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001002
1003 /* If the address family is not supported or if this address
1004 * causes the address buffer to overflow return EINVAL.
1005 */
1006 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1007 err = -EINVAL;
1008 goto out_free;
1009 }
1010
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001011 /* Save current address so we can work with it */
1012 memcpy(&to, sa_addr, af->sockaddr_len);
1013
1014 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001015 if (err)
1016 goto out_free;
1017
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001018 /* Make sure the destination port is correctly set
1019 * in all addresses.
1020 */
1021 if (asoc && asoc->peer.port && asoc->peer.port != port)
1022 goto out_free;
1023
Frank Filz3f7a87d2005-06-20 13:14:57 -07001024
1025 /* Check if there already is a matching association on the
1026 * endpoint (other than the one created here).
1027 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001028 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001029 if (asoc2 && asoc2 != asoc) {
1030 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1031 err = -EISCONN;
1032 else
1033 err = -EALREADY;
1034 goto out_free;
1035 }
1036
1037 /* If we could not find a matching association on the endpoint,
1038 * make sure that there is no peeled-off association matching
1039 * the peer address even on another socket.
1040 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001041 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001042 err = -EADDRNOTAVAIL;
1043 goto out_free;
1044 }
1045
1046 if (!asoc) {
1047 /* If a bind() or sctp_bindx() is not called prior to
1048 * an sctp_connectx() call, the system picks an
1049 * ephemeral port and will choose an address set
1050 * equivalent to binding with a wildcard address.
1051 */
1052 if (!ep->base.bind_addr.port) {
1053 if (sctp_autobind(sk)) {
1054 err = -EAGAIN;
1055 goto out_free;
1056 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001057 } else {
1058 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001059 * If an unprivileged user inherits a 1-many
1060 * style socket with open associations on a
1061 * privileged port, it MAY be permitted to
1062 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001063 * be permitted to open new associations.
1064 */
1065 if (ep->base.bind_addr.port < PROT_SOCK &&
1066 !capable(CAP_NET_BIND_SERVICE)) {
1067 err = -EACCES;
1068 goto out_free;
1069 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070 }
1071
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001072 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001073 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1074 if (!asoc) {
1075 err = -ENOMEM;
1076 goto out_free;
1077 }
1078 }
1079
1080 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001081 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001082 SCTP_UNKNOWN);
1083 if (!transport) {
1084 err = -ENOMEM;
1085 goto out_free;
1086 }
1087
1088 addrcnt++;
1089 addr_buf += af->sockaddr_len;
1090 walk_size += af->sockaddr_len;
1091 }
1092
1093 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1094 if (err < 0) {
1095 goto out_free;
1096 }
1097
1098 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1099 if (err < 0) {
1100 goto out_free;
1101 }
1102
1103 /* Initialize sk's dport and daddr for getpeername() */
1104 inet_sk(sk)->dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001105 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1106 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001107 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001108
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001109 /* in-kernel sockets don't generally have a file allocated to them
1110 * if all they do is call sock_create_kern().
1111 */
1112 if (sk->sk_socket->file)
1113 f_flags = sk->sk_socket->file->f_flags;
1114
1115 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1116
Frank Filz3f7a87d2005-06-20 13:14:57 -07001117 err = sctp_wait_for_connect(asoc, &timeo);
1118
1119 /* Don't free association on exit. */
1120 asoc = NULL;
1121
1122out_free:
1123
1124 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001125 " kaddrs: %p err: %d\n",
1126 asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001127 if (asoc)
1128 sctp_association_free(asoc);
1129 return err;
1130}
1131
1132/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1133 *
1134 * API 8.9
1135 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1136 *
1137 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1138 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1139 * or IPv6 addresses.
1140 *
1141 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1142 * Section 3.1.2 for this usage.
1143 *
1144 * addrs is a pointer to an array of one or more socket addresses. Each
1145 * address is contained in its appropriate structure (i.e. struct
1146 * sockaddr_in or struct sockaddr_in6) the family of the address type
1147 * must be used to distengish the address length (note that this
1148 * representation is termed a "packed array" of addresses). The caller
1149 * specifies the number of addresses in the array with addrcnt.
1150 *
1151 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1152 * -1, and sets errno to the appropriate error code.
1153 *
1154 * For SCTP, the port given in each socket address must be the same, or
1155 * sctp_connectx() will fail, setting errno to EINVAL.
1156 *
1157 * An application can use sctp_connectx to initiate an association with
1158 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1159 * allows a caller to specify multiple addresses at which a peer can be
1160 * reached. The way the SCTP stack uses the list of addresses to set up
1161 * the association is implementation dependant. This function only
1162 * specifies that the stack will try to make use of all the addresses in
1163 * the list when needed.
1164 *
1165 * Note that the list of addresses passed in is only used for setting up
1166 * the association. It does not necessarily equal the set of addresses
1167 * the peer uses for the resulting association. If the caller wants to
1168 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1169 * retrieve them after the association has been set up.
1170 *
1171 * Basically do nothing but copying the addresses from user to kernel
1172 * land and invoking either sctp_connectx(). This is used for tunneling
1173 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1174 *
1175 * We don't use copy_from_user() for optimization: we first do the
1176 * sanity checks (buffer size -fast- and access check-healthy
1177 * pointer); if all of those succeed, then we can alloc the memory
1178 * (expensive operation) needed to copy the data to kernel. Then we do
1179 * the copying without checking the user space area
1180 * (__copy_from_user()).
1181 *
1182 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1183 * it.
1184 *
1185 * sk The sk of the socket
1186 * addrs The pointer to the addresses in user land
1187 * addrssize Size of the addrs buffer
1188 *
1189 * Returns 0 if ok, <0 errno code on error.
1190 */
1191SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1192 struct sockaddr __user *addrs,
1193 int addrs_size)
1194{
1195 int err = 0;
1196 struct sockaddr *kaddrs;
1197
1198 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1199 __FUNCTION__, sk, addrs, addrs_size);
1200
1201 if (unlikely(addrs_size <= 0))
1202 return -EINVAL;
1203
1204 /* Check the user passed a healthy pointer. */
1205 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1206 return -EFAULT;
1207
1208 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001209 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001210 if (unlikely(!kaddrs))
1211 return -ENOMEM;
1212
1213 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1214 err = -EFAULT;
1215 } else {
1216 err = __sctp_connect(sk, kaddrs, addrs_size);
1217 }
1218
1219 kfree(kaddrs);
1220 return err;
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/* API 3.1.4 close() - UDP Style Syntax
1224 * Applications use close() to perform graceful shutdown (as described in
1225 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1226 * by a UDP-style socket.
1227 *
1228 * The syntax is
1229 *
1230 * ret = close(int sd);
1231 *
1232 * sd - the socket descriptor of the associations to be closed.
1233 *
1234 * To gracefully shutdown a specific association represented by the
1235 * UDP-style socket, an application should use the sendmsg() call,
1236 * passing no user data, but including the appropriate flag in the
1237 * ancillary data (see Section xxxx).
1238 *
1239 * If sd in the close() call is a branched-off socket representing only
1240 * one association, the shutdown is performed on that association only.
1241 *
1242 * 4.1.6 close() - TCP Style Syntax
1243 *
1244 * Applications use close() to gracefully close down an association.
1245 *
1246 * The syntax is:
1247 *
1248 * int close(int sd);
1249 *
1250 * sd - the socket descriptor of the association to be closed.
1251 *
1252 * After an application calls close() on a socket descriptor, no further
1253 * socket operations will succeed on that descriptor.
1254 *
1255 * API 7.1.4 SO_LINGER
1256 *
1257 * An application using the TCP-style socket can use this option to
1258 * perform the SCTP ABORT primitive. The linger option structure is:
1259 *
1260 * struct linger {
1261 * int l_onoff; // option on/off
1262 * int l_linger; // linger time
1263 * };
1264 *
1265 * To enable the option, set l_onoff to 1. If the l_linger value is set
1266 * to 0, calling close() is the same as the ABORT primitive. If the
1267 * value is set to a negative value, the setsockopt() call will return
1268 * an error. If the value is set to a positive value linger_time, the
1269 * close() can be blocked for at most linger_time ms. If the graceful
1270 * shutdown phase does not finish during this period, close() will
1271 * return but the graceful shutdown phase continues in the system.
1272 */
1273SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1274{
1275 struct sctp_endpoint *ep;
1276 struct sctp_association *asoc;
1277 struct list_head *pos, *temp;
1278
1279 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1280
1281 sctp_lock_sock(sk);
1282 sk->sk_shutdown = SHUTDOWN_MASK;
1283
1284 ep = sctp_sk(sk)->ep;
1285
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001286 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 list_for_each_safe(pos, temp, &ep->asocs) {
1288 asoc = list_entry(pos, struct sctp_association, asocs);
1289
1290 if (sctp_style(sk, TCP)) {
1291 /* A closed association can still be in the list if
1292 * it belongs to a TCP-style listening socket that is
1293 * not yet accepted. If so, free it. If not, send an
1294 * ABORT or SHUTDOWN based on the linger options.
1295 */
1296 if (sctp_state(asoc, CLOSED)) {
1297 sctp_unhash_established(asoc);
1298 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001299 continue;
1300 }
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001303 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1304 struct sctp_chunk *chunk;
1305
1306 chunk = sctp_make_abort_user(asoc, NULL, 0);
1307 if (chunk)
1308 sctp_primitive_ABORT(asoc, chunk);
1309 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sctp_primitive_SHUTDOWN(asoc, NULL);
1311 }
1312
1313 /* Clean up any skbs sitting on the receive queue. */
1314 sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1315 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1316
1317 /* On a TCP-style socket, block for at most linger_time if set. */
1318 if (sctp_style(sk, TCP) && timeout)
1319 sctp_wait_for_close(sk, timeout);
1320
1321 /* This will run the backlog queue. */
1322 sctp_release_sock(sk);
1323
1324 /* Supposedly, no process has access to the socket, but
1325 * the net layers still may.
1326 */
1327 sctp_local_bh_disable();
1328 sctp_bh_lock_sock(sk);
1329
1330 /* Hold the sock, since sk_common_release() will put sock_put()
1331 * and we have just a little more cleanup.
1332 */
1333 sock_hold(sk);
1334 sk_common_release(sk);
1335
1336 sctp_bh_unlock_sock(sk);
1337 sctp_local_bh_enable();
1338
1339 sock_put(sk);
1340
1341 SCTP_DBG_OBJCNT_DEC(sock);
1342}
1343
1344/* Handle EPIPE error. */
1345static int sctp_error(struct sock *sk, int flags, int err)
1346{
1347 if (err == -EPIPE)
1348 err = sock_error(sk) ? : -EPIPE;
1349 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1350 send_sig(SIGPIPE, current, 0);
1351 return err;
1352}
1353
1354/* API 3.1.3 sendmsg() - UDP Style Syntax
1355 *
1356 * An application uses sendmsg() and recvmsg() calls to transmit data to
1357 * and receive data from its peer.
1358 *
1359 * ssize_t sendmsg(int socket, const struct msghdr *message,
1360 * int flags);
1361 *
1362 * socket - the socket descriptor of the endpoint.
1363 * message - pointer to the msghdr structure which contains a single
1364 * user message and possibly some ancillary data.
1365 *
1366 * See Section 5 for complete description of the data
1367 * structures.
1368 *
1369 * flags - flags sent or received with the user message, see Section
1370 * 5 for complete description of the flags.
1371 *
1372 * Note: This function could use a rewrite especially when explicit
1373 * connect support comes in.
1374 */
1375/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1376
1377SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1378
1379SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1380 struct msghdr *msg, size_t msg_len)
1381{
1382 struct sctp_sock *sp;
1383 struct sctp_endpoint *ep;
1384 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1385 struct sctp_transport *transport, *chunk_tp;
1386 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001387 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct sockaddr *msg_name = NULL;
1389 struct sctp_sndrcvinfo default_sinfo = { 0 };
1390 struct sctp_sndrcvinfo *sinfo;
1391 struct sctp_initmsg *sinit;
1392 sctp_assoc_t associd = 0;
1393 sctp_cmsgs_t cmsgs = { NULL };
1394 int err;
1395 sctp_scope_t scope;
1396 long timeo;
1397 __u16 sinfo_flags = 0;
1398 struct sctp_datamsg *datamsg;
1399 struct list_head *pos;
1400 int msg_flags = msg->msg_flags;
1401
1402 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1403 sk, msg, msg_len);
1404
1405 err = 0;
1406 sp = sctp_sk(sk);
1407 ep = sp->ep;
1408
Frank Filz3f7a87d2005-06-20 13:14:57 -07001409 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* We cannot send a message over a TCP-style listening socket. */
1412 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1413 err = -EPIPE;
1414 goto out_nounlock;
1415 }
1416
1417 /* Parse out the SCTP CMSGs. */
1418 err = sctp_msghdr_parse(msg, &cmsgs);
1419
1420 if (err) {
1421 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1422 goto out_nounlock;
1423 }
1424
1425 /* Fetch the destination address for this packet. This
1426 * address only selects the association--it is not necessarily
1427 * the address we will send to.
1428 * For a peeled-off socket, msg_name is ignored.
1429 */
1430 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1431 int msg_namelen = msg->msg_namelen;
1432
1433 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1434 msg_namelen);
1435 if (err)
1436 return err;
1437
1438 if (msg_namelen > sizeof(to))
1439 msg_namelen = sizeof(to);
1440 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 msg_name = msg->msg_name;
1442 }
1443
1444 sinfo = cmsgs.info;
1445 sinit = cmsgs.init;
1446
1447 /* Did the user specify SNDRCVINFO? */
1448 if (sinfo) {
1449 sinfo_flags = sinfo->sinfo_flags;
1450 associd = sinfo->sinfo_assoc_id;
1451 }
1452
1453 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1454 msg_len, sinfo_flags);
1455
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001456 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1457 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 err = -EINVAL;
1459 goto out_nounlock;
1460 }
1461
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001462 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1463 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1464 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001466 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001467 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1468 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 err = -EINVAL;
1470 goto out_nounlock;
1471 }
1472
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001473 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * specified in msg_name.
1475 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001476 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 err = -EINVAL;
1478 goto out_nounlock;
1479 }
1480
1481 transport = NULL;
1482
1483 SCTP_DEBUG_PRINTK("About to look up association.\n");
1484
1485 sctp_lock_sock(sk);
1486
1487 /* If a msg_name has been specified, assume this is to be used. */
1488 if (msg_name) {
1489 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001490 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (!asoc) {
1492 /* If we could not find a matching association on the
1493 * endpoint, make sure that it is not a TCP-style
1494 * socket that already has an association or there is
1495 * no peeled-off association on another socket.
1496 */
1497 if ((sctp_style(sk, TCP) &&
1498 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001499 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 err = -EADDRNOTAVAIL;
1501 goto out_unlock;
1502 }
1503 }
1504 } else {
1505 asoc = sctp_id2assoc(sk, associd);
1506 if (!asoc) {
1507 err = -EPIPE;
1508 goto out_unlock;
1509 }
1510 }
1511
1512 if (asoc) {
1513 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1514
1515 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1516 * socket that has an association in CLOSED state. This can
1517 * happen when an accepted socket has an association that is
1518 * already CLOSED.
1519 */
1520 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1521 err = -EPIPE;
1522 goto out_unlock;
1523 }
1524
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001525 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1527 asoc);
1528 sctp_primitive_SHUTDOWN(asoc, NULL);
1529 err = 0;
1530 goto out_unlock;
1531 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001532 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001533
1534 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1535 if (!chunk) {
1536 err = -ENOMEM;
1537 goto out_unlock;
1538 }
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001541 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 err = 0;
1543 goto out_unlock;
1544 }
1545 }
1546
1547 /* Do we need to create the association? */
1548 if (!asoc) {
1549 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1550
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001551 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 err = -EINVAL;
1553 goto out_unlock;
1554 }
1555
1556 /* Check for invalid stream against the stream counts,
1557 * either the default or the user specified stream counts.
1558 */
1559 if (sinfo) {
1560 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1561 /* Check against the defaults. */
1562 if (sinfo->sinfo_stream >=
1563 sp->initmsg.sinit_num_ostreams) {
1564 err = -EINVAL;
1565 goto out_unlock;
1566 }
1567 } else {
1568 /* Check against the requested. */
1569 if (sinfo->sinfo_stream >=
1570 sinit->sinit_num_ostreams) {
1571 err = -EINVAL;
1572 goto out_unlock;
1573 }
1574 }
1575 }
1576
1577 /*
1578 * API 3.1.2 bind() - UDP Style Syntax
1579 * If a bind() or sctp_bindx() is not called prior to a
1580 * sendmsg() call that initiates a new association, the
1581 * system picks an ephemeral port and will choose an address
1582 * set equivalent to binding with a wildcard address.
1583 */
1584 if (!ep->base.bind_addr.port) {
1585 if (sctp_autobind(sk)) {
1586 err = -EAGAIN;
1587 goto out_unlock;
1588 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001589 } else {
1590 /*
1591 * If an unprivileged user inherits a one-to-many
1592 * style socket with open associations on a privileged
1593 * port, it MAY be permitted to accept new associations,
1594 * but it SHOULD NOT be permitted to open new
1595 * associations.
1596 */
1597 if (ep->base.bind_addr.port < PROT_SOCK &&
1598 !capable(CAP_NET_BIND_SERVICE)) {
1599 err = -EACCES;
1600 goto out_unlock;
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
1604 scope = sctp_scope(&to);
1605 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1606 if (!new_asoc) {
1607 err = -ENOMEM;
1608 goto out_unlock;
1609 }
1610 asoc = new_asoc;
1611
1612 /* If the SCTP_INIT ancillary data is specified, set all
1613 * the association init values accordingly.
1614 */
1615 if (sinit) {
1616 if (sinit->sinit_num_ostreams) {
1617 asoc->c.sinit_num_ostreams =
1618 sinit->sinit_num_ostreams;
1619 }
1620 if (sinit->sinit_max_instreams) {
1621 asoc->c.sinit_max_instreams =
1622 sinit->sinit_max_instreams;
1623 }
1624 if (sinit->sinit_max_attempts) {
1625 asoc->max_init_attempts
1626 = sinit->sinit_max_attempts;
1627 }
1628 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001629 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1631 }
1632 }
1633
1634 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001635 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (!transport) {
1637 err = -ENOMEM;
1638 goto out_free;
1639 }
1640 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1641 if (err < 0) {
1642 err = -ENOMEM;
1643 goto out_free;
1644 }
1645 }
1646
1647 /* ASSERT: we have a valid association at this point. */
1648 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1649
1650 if (!sinfo) {
1651 /* If the user didn't specify SNDRCVINFO, make up one with
1652 * some defaults.
1653 */
1654 default_sinfo.sinfo_stream = asoc->default_stream;
1655 default_sinfo.sinfo_flags = asoc->default_flags;
1656 default_sinfo.sinfo_ppid = asoc->default_ppid;
1657 default_sinfo.sinfo_context = asoc->default_context;
1658 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1659 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1660 sinfo = &default_sinfo;
1661 }
1662
1663 /* API 7.1.7, the sndbuf size per association bounds the
1664 * maximum size of data that can be sent in a single send call.
1665 */
1666 if (msg_len > sk->sk_sndbuf) {
1667 err = -EMSGSIZE;
1668 goto out_free;
1669 }
1670
Vlad Yasevich8a479492007-06-07 14:21:05 -04001671 if (asoc->pmtu_pending)
1672 sctp_assoc_pending_pmtu(asoc);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* If fragmentation is disabled and the message length exceeds the
1675 * association fragmentation point, return EMSGSIZE. The I-D
1676 * does not specify what this error is, but this looks like
1677 * a great fit.
1678 */
1679 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1680 err = -EMSGSIZE;
1681 goto out_free;
1682 }
1683
1684 if (sinfo) {
1685 /* Check for invalid stream. */
1686 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1687 err = -EINVAL;
1688 goto out_free;
1689 }
1690 }
1691
1692 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1693 if (!sctp_wspace(asoc)) {
1694 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1695 if (err)
1696 goto out_free;
1697 }
1698
1699 /* If an address is passed with the sendto/sendmsg call, it is used
1700 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001701 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 */
1703 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001704 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001705 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!chunk_tp) {
1707 err = -EINVAL;
1708 goto out_free;
1709 }
1710 } else
1711 chunk_tp = NULL;
1712
1713 /* Auto-connect, if we aren't connected already. */
1714 if (sctp_state(asoc, CLOSED)) {
1715 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1716 if (err < 0)
1717 goto out_free;
1718 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1719 }
1720
1721 /* Break the message into multiple chunks of maximum size. */
1722 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1723 if (!datamsg) {
1724 err = -ENOMEM;
1725 goto out_free;
1726 }
1727
1728 /* Now send the (possibly) fragmented message. */
1729 list_for_each(pos, &datamsg->chunks) {
1730 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1731 sctp_datamsg_track(chunk);
1732
1733 /* Do accounting for the write space. */
1734 sctp_set_owner_w(chunk);
1735
1736 chunk->transport = chunk_tp;
1737
1738 /* Send it to the lower layers. Note: all chunks
1739 * must either fail or succeed. The lower layer
1740 * works that way today. Keep it that way or this
1741 * breaks.
1742 */
1743 err = sctp_primitive_SEND(asoc, chunk);
1744 /* Did the lower layer accept the chunk? */
1745 if (err)
1746 sctp_chunk_free(chunk);
1747 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1748 }
1749
1750 sctp_datamsg_free(datamsg);
1751 if (err)
1752 goto out_free;
1753 else
1754 err = msg_len;
1755
1756 /* If we are already past ASSOCIATE, the lower
1757 * layers are responsible for association cleanup.
1758 */
1759 goto out_unlock;
1760
1761out_free:
1762 if (new_asoc)
1763 sctp_association_free(asoc);
1764out_unlock:
1765 sctp_release_sock(sk);
1766
1767out_nounlock:
1768 return sctp_error(sk, msg_flags, err);
1769
1770#if 0
1771do_sock_err:
1772 if (msg_len)
1773 err = msg_len;
1774 else
1775 err = sock_error(sk);
1776 goto out;
1777
1778do_interrupted:
1779 if (msg_len)
1780 err = msg_len;
1781 goto out;
1782#endif /* 0 */
1783}
1784
1785/* This is an extended version of skb_pull() that removes the data from the
1786 * start of a skb even when data is spread across the list of skb's in the
1787 * frag_list. len specifies the total amount of data that needs to be removed.
1788 * when 'len' bytes could be removed from the skb, it returns 0.
1789 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1790 * could not be removed.
1791 */
1792static int sctp_skb_pull(struct sk_buff *skb, int len)
1793{
1794 struct sk_buff *list;
1795 int skb_len = skb_headlen(skb);
1796 int rlen;
1797
1798 if (len <= skb_len) {
1799 __skb_pull(skb, len);
1800 return 0;
1801 }
1802 len -= skb_len;
1803 __skb_pull(skb, skb_len);
1804
1805 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1806 rlen = sctp_skb_pull(list, len);
1807 skb->len -= (len-rlen);
1808 skb->data_len -= (len-rlen);
1809
1810 if (!rlen)
1811 return 0;
1812
1813 len = rlen;
1814 }
1815
1816 return len;
1817}
1818
1819/* API 3.1.3 recvmsg() - UDP Style Syntax
1820 *
1821 * ssize_t recvmsg(int socket, struct msghdr *message,
1822 * int flags);
1823 *
1824 * socket - the socket descriptor of the endpoint.
1825 * message - pointer to the msghdr structure which contains a single
1826 * user message and possibly some ancillary data.
1827 *
1828 * See Section 5 for complete description of the data
1829 * structures.
1830 *
1831 * flags - flags sent or received with the user message, see Section
1832 * 5 for complete description of the flags.
1833 */
1834static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1835
1836SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1837 struct msghdr *msg, size_t len, int noblock,
1838 int flags, int *addr_len)
1839{
1840 struct sctp_ulpevent *event = NULL;
1841 struct sctp_sock *sp = sctp_sk(sk);
1842 struct sk_buff *skb;
1843 int copied;
1844 int err = 0;
1845 int skb_len;
1846
1847 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1848 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1849 "len", len, "knoblauch", noblock,
1850 "flags", flags, "addr_len", addr_len);
1851
1852 sctp_lock_sock(sk);
1853
1854 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1855 err = -ENOTCONN;
1856 goto out;
1857 }
1858
1859 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1860 if (!skb)
1861 goto out;
1862
1863 /* Get the total length of the skb including any skb's in the
1864 * frag_list.
1865 */
1866 skb_len = skb->len;
1867
1868 copied = skb_len;
1869 if (copied > len)
1870 copied = len;
1871
1872 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1873
1874 event = sctp_skb2event(skb);
1875
1876 if (err)
1877 goto out_free;
1878
1879 sock_recv_timestamp(msg, sk, skb);
1880 if (sctp_ulpevent_is_notification(event)) {
1881 msg->msg_flags |= MSG_NOTIFICATION;
1882 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1883 } else {
1884 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1885 }
1886
1887 /* Check if we allow SCTP_SNDRCVINFO. */
1888 if (sp->subscribe.sctp_data_io_event)
1889 sctp_ulpevent_read_sndrcvinfo(event, msg);
1890#if 0
1891 /* FIXME: we should be calling IP/IPv6 layers. */
1892 if (sk->sk_protinfo.af_inet.cmsg_flags)
1893 ip_cmsg_recv(msg, skb);
1894#endif
1895
1896 err = copied;
1897
1898 /* If skb's length exceeds the user's buffer, update the skb and
1899 * push it back to the receive_queue so that the next call to
1900 * recvmsg() will return the remaining data. Don't set MSG_EOR.
1901 */
1902 if (skb_len > copied) {
1903 msg->msg_flags &= ~MSG_EOR;
1904 if (flags & MSG_PEEK)
1905 goto out_free;
1906 sctp_skb_pull(skb, copied);
1907 skb_queue_head(&sk->sk_receive_queue, skb);
1908
1909 /* When only partial message is copied to the user, increase
1910 * rwnd by that amount. If all the data in the skb is read,
1911 * rwnd is updated when the event is freed.
1912 */
1913 sctp_assoc_rwnd_increase(event->asoc, copied);
1914 goto out;
1915 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1916 (event->msg_flags & MSG_EOR))
1917 msg->msg_flags |= MSG_EOR;
1918 else
1919 msg->msg_flags &= ~MSG_EOR;
1920
1921out_free:
1922 if (flags & MSG_PEEK) {
1923 /* Release the skb reference acquired after peeking the skb in
1924 * sctp_skb_recv_datagram().
1925 */
1926 kfree_skb(skb);
1927 } else {
1928 /* Free the event which includes releasing the reference to
1929 * the owner of the skb, freeing the skb and updating the
1930 * rwnd.
1931 */
1932 sctp_ulpevent_free(event);
1933 }
1934out:
1935 sctp_release_sock(sk);
1936 return err;
1937}
1938
1939/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1940 *
1941 * This option is a on/off flag. If enabled no SCTP message
1942 * fragmentation will be performed. Instead if a message being sent
1943 * exceeds the current PMTU size, the message will NOT be sent and
1944 * instead a error will be indicated to the user.
1945 */
1946static int sctp_setsockopt_disable_fragments(struct sock *sk,
1947 char __user *optval, int optlen)
1948{
1949 int val;
1950
1951 if (optlen < sizeof(int))
1952 return -EINVAL;
1953
1954 if (get_user(val, (int __user *)optval))
1955 return -EFAULT;
1956
1957 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1958
1959 return 0;
1960}
1961
1962static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1963 int optlen)
1964{
1965 if (optlen != sizeof(struct sctp_event_subscribe))
1966 return -EINVAL;
1967 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1968 return -EFAULT;
1969 return 0;
1970}
1971
1972/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1973 *
1974 * This socket option is applicable to the UDP-style socket only. When
1975 * set it will cause associations that are idle for more than the
1976 * specified number of seconds to automatically close. An association
1977 * being idle is defined an association that has NOT sent or received
1978 * user data. The special value of '0' indicates that no automatic
1979 * close of any associations should be performed. The option expects an
1980 * integer defining the number of seconds of idle time before an
1981 * association is closed.
1982 */
1983static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1984 int optlen)
1985{
1986 struct sctp_sock *sp = sctp_sk(sk);
1987
1988 /* Applicable to UDP-style socket only */
1989 if (sctp_style(sk, TCP))
1990 return -EOPNOTSUPP;
1991 if (optlen != sizeof(int))
1992 return -EINVAL;
1993 if (copy_from_user(&sp->autoclose, optval, optlen))
1994 return -EFAULT;
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
1997}
1998
1999/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2000 *
2001 * Applications can enable or disable heartbeats for any peer address of
2002 * an association, modify an address's heartbeat interval, force a
2003 * heartbeat to be sent immediately, and adjust the address's maximum
2004 * number of retransmissions sent before an address is considered
2005 * unreachable. The following structure is used to access and modify an
2006 * address's parameters:
2007 *
2008 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002009 * sctp_assoc_t spp_assoc_id;
2010 * struct sockaddr_storage spp_address;
2011 * uint32_t spp_hbinterval;
2012 * uint16_t spp_pathmaxrxt;
2013 * uint32_t spp_pathmtu;
2014 * uint32_t spp_sackdelay;
2015 * uint32_t spp_flags;
2016 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002018 * spp_assoc_id - (one-to-many style socket) This is filled in the
2019 * application, and identifies the association for
2020 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * spp_address - This specifies which address is of interest.
2022 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002023 * in milliseconds. If a value of zero
2024 * is present in this field then no changes are to
2025 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 * spp_pathmaxrxt - This contains the maximum number of
2027 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002028 * considered unreachable. If a value of zero
2029 * is present in this field then no changes are to
2030 * be made to this parameter.
2031 * spp_pathmtu - When Path MTU discovery is disabled the value
2032 * specified here will be the "fixed" path mtu.
2033 * Note that if the spp_address field is empty
2034 * then all associations on this address will
2035 * have this fixed path mtu set upon them.
2036 *
2037 * spp_sackdelay - When delayed sack is enabled, this value specifies
2038 * the number of milliseconds that sacks will be delayed
2039 * for. This value will apply to all addresses of an
2040 * association if the spp_address field is empty. Note
2041 * also, that if delayed sack is enabled and this
2042 * value is set to 0, no change is made to the last
2043 * recorded delayed sack timer value.
2044 *
2045 * spp_flags - These flags are used to control various features
2046 * on an association. The flag field may contain
2047 * zero or more of the following options.
2048 *
2049 * SPP_HB_ENABLE - Enable heartbeats on the
2050 * specified address. Note that if the address
2051 * field is empty all addresses for the association
2052 * have heartbeats enabled upon them.
2053 *
2054 * SPP_HB_DISABLE - Disable heartbeats on the
2055 * speicifed address. Note that if the address
2056 * field is empty all addresses for the association
2057 * will have their heartbeats disabled. Note also
2058 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2059 * mutually exclusive, only one of these two should
2060 * be specified. Enabling both fields will have
2061 * undetermined results.
2062 *
2063 * SPP_HB_DEMAND - Request a user initiated heartbeat
2064 * to be made immediately.
2065 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002066 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2067 * heartbeat delayis to be set to the value of 0
2068 * milliseconds.
2069 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002070 * SPP_PMTUD_ENABLE - This field will enable PMTU
2071 * discovery upon the specified address. Note that
2072 * if the address feild is empty then all addresses
2073 * on the association are effected.
2074 *
2075 * SPP_PMTUD_DISABLE - This field will disable PMTU
2076 * discovery upon the specified address. Note that
2077 * if the address feild is empty then all addresses
2078 * on the association are effected. Not also that
2079 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2080 * exclusive. Enabling both will have undetermined
2081 * results.
2082 *
2083 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2084 * on delayed sack. The time specified in spp_sackdelay
2085 * is used to specify the sack delay for this address. Note
2086 * that if spp_address is empty then all addresses will
2087 * enable delayed sack and take on the sack delay
2088 * value specified in spp_sackdelay.
2089 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2090 * off delayed sack. If the spp_address field is blank then
2091 * delayed sack is disabled for the entire association. Note
2092 * also that this field is mutually exclusive to
2093 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2094 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Adrian Bunk16164362006-09-18 00:40:38 -07002096static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2097 struct sctp_transport *trans,
2098 struct sctp_association *asoc,
2099 struct sctp_sock *sp,
2100 int hb_change,
2101 int pmtud_change,
2102 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int error;
2105
Frank Filz52ccb8e2005-12-22 11:36:46 -08002106 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2108 if (error)
2109 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002112 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2113 * this field is ignored. Note also that a value of zero indicates
2114 * the current setting should be left unchanged.
2115 */
2116 if (params->spp_flags & SPP_HB_ENABLE) {
2117
2118 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2119 * set. This lets us use 0 value when this flag
2120 * is set.
2121 */
2122 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2123 params->spp_hbinterval = 0;
2124
2125 if (params->spp_hbinterval ||
2126 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2127 if (trans) {
2128 trans->hbinterval =
2129 msecs_to_jiffies(params->spp_hbinterval);
2130 } else if (asoc) {
2131 asoc->hbinterval =
2132 msecs_to_jiffies(params->spp_hbinterval);
2133 } else {
2134 sp->hbinterval = params->spp_hbinterval;
2135 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002136 }
2137 }
2138
2139 if (hb_change) {
2140 if (trans) {
2141 trans->param_flags =
2142 (trans->param_flags & ~SPP_HB) | hb_change;
2143 } else if (asoc) {
2144 asoc->param_flags =
2145 (asoc->param_flags & ~SPP_HB) | hb_change;
2146 } else {
2147 sp->param_flags =
2148 (sp->param_flags & ~SPP_HB) | hb_change;
2149 }
2150 }
2151
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002152 /* When Path MTU discovery is disabled the value specified here will
2153 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2154 * include the flag SPP_PMTUD_DISABLE for this field to have any
2155 * effect).
2156 */
2157 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002158 if (trans) {
2159 trans->pathmtu = params->spp_pathmtu;
2160 sctp_assoc_sync_pmtu(asoc);
2161 } else if (asoc) {
2162 asoc->pathmtu = params->spp_pathmtu;
2163 sctp_frag_point(sp, params->spp_pathmtu);
2164 } else {
2165 sp->pathmtu = params->spp_pathmtu;
2166 }
2167 }
2168
2169 if (pmtud_change) {
2170 if (trans) {
2171 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2172 (params->spp_flags & SPP_PMTUD_ENABLE);
2173 trans->param_flags =
2174 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2175 if (update) {
2176 sctp_transport_pmtu(trans);
2177 sctp_assoc_sync_pmtu(asoc);
2178 }
2179 } else if (asoc) {
2180 asoc->param_flags =
2181 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2182 } else {
2183 sp->param_flags =
2184 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2185 }
2186 }
2187
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002188 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2189 * value of this field is ignored. Note also that a value of zero
2190 * indicates the current setting should be left unchanged.
2191 */
2192 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002193 if (trans) {
2194 trans->sackdelay =
2195 msecs_to_jiffies(params->spp_sackdelay);
2196 } else if (asoc) {
2197 asoc->sackdelay =
2198 msecs_to_jiffies(params->spp_sackdelay);
2199 } else {
2200 sp->sackdelay = params->spp_sackdelay;
2201 }
2202 }
2203
2204 if (sackdelay_change) {
2205 if (trans) {
2206 trans->param_flags =
2207 (trans->param_flags & ~SPP_SACKDELAY) |
2208 sackdelay_change;
2209 } else if (asoc) {
2210 asoc->param_flags =
2211 (asoc->param_flags & ~SPP_SACKDELAY) |
2212 sackdelay_change;
2213 } else {
2214 sp->param_flags =
2215 (sp->param_flags & ~SPP_SACKDELAY) |
2216 sackdelay_change;
2217 }
2218 }
2219
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002220 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
2221 * of this field is ignored. Note also that a value of zero
2222 * indicates the current setting should be left unchanged.
2223 */
2224 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002225 if (trans) {
2226 trans->pathmaxrxt = params->spp_pathmaxrxt;
2227 } else if (asoc) {
2228 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2229 } else {
2230 sp->pathmaxrxt = params->spp_pathmaxrxt;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
2237static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2238 char __user *optval, int optlen)
2239{
2240 struct sctp_paddrparams params;
2241 struct sctp_transport *trans = NULL;
2242 struct sctp_association *asoc = NULL;
2243 struct sctp_sock *sp = sctp_sk(sk);
2244 int error;
2245 int hb_change, pmtud_change, sackdelay_change;
2246
2247 if (optlen != sizeof(struct sctp_paddrparams))
2248 return - EINVAL;
2249
2250 if (copy_from_user(&params, optval, optlen))
2251 return -EFAULT;
2252
2253 /* Validate flags and value parameters. */
2254 hb_change = params.spp_flags & SPP_HB;
2255 pmtud_change = params.spp_flags & SPP_PMTUD;
2256 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2257
2258 if (hb_change == SPP_HB ||
2259 pmtud_change == SPP_PMTUD ||
2260 sackdelay_change == SPP_SACKDELAY ||
2261 params.spp_sackdelay > 500 ||
2262 (params.spp_pathmtu
2263 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2264 return -EINVAL;
2265
2266 /* If an address other than INADDR_ANY is specified, and
2267 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002269 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2270 trans = sctp_addr_id2transport(sk, &params.spp_address,
2271 params.spp_assoc_id);
2272 if (!trans)
2273 return -EINVAL;
2274 }
2275
2276 /* Get association, if assoc_id != 0 and the socket is a one
2277 * to many style socket, and an association was not found, then
2278 * the id was invalid.
2279 */
2280 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2281 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2282 return -EINVAL;
2283
2284 /* Heartbeat demand can only be sent on a transport or
2285 * association, but not a socket.
2286 */
2287 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2288 return -EINVAL;
2289
2290 /* Process parameters. */
2291 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2292 hb_change, pmtud_change,
2293 sackdelay_change);
2294
2295 if (error)
2296 return error;
2297
2298 /* If changes are for association, also apply parameters to each
2299 * transport.
2300 */
2301 if (!trans && asoc) {
2302 struct list_head *pos;
2303
2304 list_for_each(pos, &asoc->peer.transport_addr_list) {
2305 trans = list_entry(pos, struct sctp_transport,
2306 transports);
2307 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2308 hb_change, pmtud_change,
2309 sackdelay_change);
2310 }
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 return 0;
2314}
2315
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002316/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08002317 *
2318 * This options will get or set the delayed ack timer. The time is set
2319 * in milliseconds. If the assoc_id is 0, then this sets or gets the
2320 * endpoints default delayed ack timer value. If the assoc_id field is
2321 * non-zero, then the set or get effects the specified association.
2322 *
2323 * struct sctp_assoc_value {
2324 * sctp_assoc_t assoc_id;
2325 * uint32_t assoc_value;
2326 * };
2327 *
2328 * assoc_id - This parameter, indicates which association the
2329 * user is preforming an action upon. Note that if
2330 * this field's value is zero then the endpoints
2331 * default value is changed (effecting future
2332 * associations only).
2333 *
2334 * assoc_value - This parameter contains the number of milliseconds
2335 * that the user is requesting the delayed ACK timer
2336 * be set to. Note that this value is defined in
2337 * the standard to be between 200 and 500 milliseconds.
2338 *
2339 * Note: a value of zero will leave the value alone,
2340 * but disable SACK delay. A non-zero value will also
2341 * enable SACK delay.
2342 */
2343
2344static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2345 char __user *optval, int optlen)
2346{
2347 struct sctp_assoc_value params;
2348 struct sctp_transport *trans = NULL;
2349 struct sctp_association *asoc = NULL;
2350 struct sctp_sock *sp = sctp_sk(sk);
2351
2352 if (optlen != sizeof(struct sctp_assoc_value))
2353 return - EINVAL;
2354
2355 if (copy_from_user(&params, optval, optlen))
2356 return -EFAULT;
2357
2358 /* Validate value parameter. */
2359 if (params.assoc_value > 500)
2360 return -EINVAL;
2361
2362 /* Get association, if assoc_id != 0 and the socket is a one
2363 * to many style socket, and an association was not found, then
2364 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002365 */
Frank Filz77086102005-12-22 11:37:30 -08002366 asoc = sctp_id2assoc(sk, params.assoc_id);
2367 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2368 return -EINVAL;
2369
2370 if (params.assoc_value) {
2371 if (asoc) {
2372 asoc->sackdelay =
2373 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002374 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002375 (asoc->param_flags & ~SPP_SACKDELAY) |
2376 SPP_SACKDELAY_ENABLE;
2377 } else {
2378 sp->sackdelay = params.assoc_value;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002379 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002380 (sp->param_flags & ~SPP_SACKDELAY) |
2381 SPP_SACKDELAY_ENABLE;
2382 }
2383 } else {
2384 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002385 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002386 (asoc->param_flags & ~SPP_SACKDELAY) |
2387 SPP_SACKDELAY_DISABLE;
2388 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002389 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002390 (sp->param_flags & ~SPP_SACKDELAY) |
2391 SPP_SACKDELAY_DISABLE;
2392 }
2393 }
2394
2395 /* If change is for association, also apply to each transport. */
2396 if (asoc) {
2397 struct list_head *pos;
2398
2399 list_for_each(pos, &asoc->peer.transport_addr_list) {
2400 trans = list_entry(pos, struct sctp_transport,
2401 transports);
2402 if (params.assoc_value) {
2403 trans->sackdelay =
2404 msecs_to_jiffies(params.assoc_value);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002405 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002406 (trans->param_flags & ~SPP_SACKDELAY) |
2407 SPP_SACKDELAY_ENABLE;
2408 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002409 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002410 (trans->param_flags & ~SPP_SACKDELAY) |
2411 SPP_SACKDELAY_DISABLE;
2412 }
2413 }
2414 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002415
Frank Filz77086102005-12-22 11:37:30 -08002416 return 0;
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2420 *
2421 * Applications can specify protocol parameters for the default association
2422 * initialization. The option name argument to setsockopt() and getsockopt()
2423 * is SCTP_INITMSG.
2424 *
2425 * Setting initialization parameters is effective only on an unconnected
2426 * socket (for UDP-style sockets only future associations are effected
2427 * by the change). With TCP-style sockets, this option is inherited by
2428 * sockets derived from a listener socket.
2429 */
2430static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2431{
2432 struct sctp_initmsg sinit;
2433 struct sctp_sock *sp = sctp_sk(sk);
2434
2435 if (optlen != sizeof(struct sctp_initmsg))
2436 return -EINVAL;
2437 if (copy_from_user(&sinit, optval, optlen))
2438 return -EFAULT;
2439
2440 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002441 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002443 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002445 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002447 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 return 0;
2450}
2451
2452/*
2453 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2454 *
2455 * Applications that wish to use the sendto() system call may wish to
2456 * specify a default set of parameters that would normally be supplied
2457 * through the inclusion of ancillary data. This socket option allows
2458 * such an application to set the default sctp_sndrcvinfo structure.
2459 * The application that wishes to use this socket option simply passes
2460 * in to this call the sctp_sndrcvinfo structure defined in Section
2461 * 5.2.2) The input parameters accepted by this call include
2462 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2463 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2464 * to this call if the caller is using the UDP model.
2465 */
2466static int sctp_setsockopt_default_send_param(struct sock *sk,
2467 char __user *optval, int optlen)
2468{
2469 struct sctp_sndrcvinfo info;
2470 struct sctp_association *asoc;
2471 struct sctp_sock *sp = sctp_sk(sk);
2472
2473 if (optlen != sizeof(struct sctp_sndrcvinfo))
2474 return -EINVAL;
2475 if (copy_from_user(&info, optval, optlen))
2476 return -EFAULT;
2477
2478 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2479 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2480 return -EINVAL;
2481
2482 if (asoc) {
2483 asoc->default_stream = info.sinfo_stream;
2484 asoc->default_flags = info.sinfo_flags;
2485 asoc->default_ppid = info.sinfo_ppid;
2486 asoc->default_context = info.sinfo_context;
2487 asoc->default_timetolive = info.sinfo_timetolive;
2488 } else {
2489 sp->default_stream = info.sinfo_stream;
2490 sp->default_flags = info.sinfo_flags;
2491 sp->default_ppid = info.sinfo_ppid;
2492 sp->default_context = info.sinfo_context;
2493 sp->default_timetolive = info.sinfo_timetolive;
2494 }
2495
2496 return 0;
2497}
2498
2499/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2500 *
2501 * Requests that the local SCTP stack use the enclosed peer address as
2502 * the association primary. The enclosed address must be one of the
2503 * association peer's addresses.
2504 */
2505static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2506 int optlen)
2507{
2508 struct sctp_prim prim;
2509 struct sctp_transport *trans;
2510
2511 if (optlen != sizeof(struct sctp_prim))
2512 return -EINVAL;
2513
2514 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2515 return -EFAULT;
2516
2517 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2518 if (!trans)
2519 return -EINVAL;
2520
2521 sctp_assoc_set_primary(trans->asoc, trans);
2522
2523 return 0;
2524}
2525
2526/*
2527 * 7.1.5 SCTP_NODELAY
2528 *
2529 * Turn on/off any Nagle-like algorithm. This means that packets are
2530 * generally sent as soon as possible and no unnecessary delays are
2531 * introduced, at the cost of more packets in the network. Expects an
2532 * integer boolean flag.
2533 */
2534static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2535 int optlen)
2536{
2537 int val;
2538
2539 if (optlen < sizeof(int))
2540 return -EINVAL;
2541 if (get_user(val, (int __user *)optval))
2542 return -EFAULT;
2543
2544 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2545 return 0;
2546}
2547
2548/*
2549 *
2550 * 7.1.1 SCTP_RTOINFO
2551 *
2552 * The protocol parameters used to initialize and bound retransmission
2553 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2554 * and modify these parameters.
2555 * All parameters are time values, in milliseconds. A value of 0, when
2556 * modifying the parameters, indicates that the current value should not
2557 * be changed.
2558 *
2559 */
2560static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2561 struct sctp_rtoinfo rtoinfo;
2562 struct sctp_association *asoc;
2563
2564 if (optlen != sizeof (struct sctp_rtoinfo))
2565 return -EINVAL;
2566
2567 if (copy_from_user(&rtoinfo, optval, optlen))
2568 return -EFAULT;
2569
2570 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2571
2572 /* Set the values to the specific association */
2573 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2574 return -EINVAL;
2575
2576 if (asoc) {
2577 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002578 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 msecs_to_jiffies(rtoinfo.srto_initial);
2580 if (rtoinfo.srto_max != 0)
2581 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2582 if (rtoinfo.srto_min != 0)
2583 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2584 } else {
2585 /* If there is no association or the association-id = 0
2586 * set the values to the endpoint.
2587 */
2588 struct sctp_sock *sp = sctp_sk(sk);
2589
2590 if (rtoinfo.srto_initial != 0)
2591 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2592 if (rtoinfo.srto_max != 0)
2593 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2594 if (rtoinfo.srto_min != 0)
2595 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2596 }
2597
2598 return 0;
2599}
2600
2601/*
2602 *
2603 * 7.1.2 SCTP_ASSOCINFO
2604 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002605 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 * of the association.
2607 * Returns an error if the new association retransmission value is
2608 * greater than the sum of the retransmission value of the peer.
2609 * See [SCTP] for more information.
2610 *
2611 */
2612static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2613{
2614
2615 struct sctp_assocparams assocparams;
2616 struct sctp_association *asoc;
2617
2618 if (optlen != sizeof(struct sctp_assocparams))
2619 return -EINVAL;
2620 if (copy_from_user(&assocparams, optval, optlen))
2621 return -EFAULT;
2622
2623 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2624
2625 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2626 return -EINVAL;
2627
2628 /* Set the values to the specific association */
2629 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002630 if (assocparams.sasoc_asocmaxrxt != 0) {
2631 __u32 path_sum = 0;
2632 int paths = 0;
2633 struct list_head *pos;
2634 struct sctp_transport *peer_addr;
2635
2636 list_for_each(pos, &asoc->peer.transport_addr_list) {
2637 peer_addr = list_entry(pos,
2638 struct sctp_transport,
2639 transports);
2640 path_sum += peer_addr->pathmaxrxt;
2641 paths++;
2642 }
2643
2644 /* Only validate asocmaxrxt if we have more then
2645 * one path/transport. We do this because path
2646 * retransmissions are only counted when we have more
2647 * then one path.
2648 */
2649 if (paths > 1 &&
2650 assocparams.sasoc_asocmaxrxt > path_sum)
2651 return -EINVAL;
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002654 }
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (assocparams.sasoc_cookie_life != 0) {
2657 asoc->cookie_life.tv_sec =
2658 assocparams.sasoc_cookie_life / 1000;
2659 asoc->cookie_life.tv_usec =
2660 (assocparams.sasoc_cookie_life % 1000)
2661 * 1000;
2662 }
2663 } else {
2664 /* Set the values to the endpoint */
2665 struct sctp_sock *sp = sctp_sk(sk);
2666
2667 if (assocparams.sasoc_asocmaxrxt != 0)
2668 sp->assocparams.sasoc_asocmaxrxt =
2669 assocparams.sasoc_asocmaxrxt;
2670 if (assocparams.sasoc_cookie_life != 0)
2671 sp->assocparams.sasoc_cookie_life =
2672 assocparams.sasoc_cookie_life;
2673 }
2674 return 0;
2675}
2676
2677/*
2678 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2679 *
2680 * This socket option is a boolean flag which turns on or off mapped V4
2681 * addresses. If this option is turned on and the socket is type
2682 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2683 * If this option is turned off, then no mapping will be done of V4
2684 * addresses and a user will receive both PF_INET6 and PF_INET type
2685 * addresses on the socket.
2686 */
2687static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2688{
2689 int val;
2690 struct sctp_sock *sp = sctp_sk(sk);
2691
2692 if (optlen < sizeof(int))
2693 return -EINVAL;
2694 if (get_user(val, (int __user *)optval))
2695 return -EFAULT;
2696 if (val)
2697 sp->v4mapped = 1;
2698 else
2699 sp->v4mapped = 0;
2700
2701 return 0;
2702}
2703
2704/*
2705 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2706 *
2707 * This socket option specifies the maximum size to put in any outgoing
2708 * SCTP chunk. If a message is larger than this size it will be
2709 * fragmented by SCTP into the specified size. Note that the underlying
2710 * SCTP implementation may fragment into smaller sized chunks when the
2711 * PMTU of the underlying association is smaller than the value set by
2712 * the user.
2713 */
2714static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2715{
2716 struct sctp_association *asoc;
2717 struct list_head *pos;
2718 struct sctp_sock *sp = sctp_sk(sk);
2719 int val;
2720
2721 if (optlen < sizeof(int))
2722 return -EINVAL;
2723 if (get_user(val, (int __user *)optval))
2724 return -EFAULT;
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002725 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return -EINVAL;
2727 sp->user_frag = val;
2728
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07002729 /* Update the frag_point of the existing associations. */
2730 list_for_each(pos, &(sp->ep->asocs)) {
2731 asoc = list_entry(pos, struct sctp_association, asocs);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002732 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734
2735 return 0;
2736}
2737
2738
2739/*
2740 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2741 *
2742 * Requests that the peer mark the enclosed address as the association
2743 * primary. The enclosed address must be one of the association's
2744 * locally bound addresses. The following structure is used to make a
2745 * set primary request:
2746 */
2747static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2748 int optlen)
2749{
2750 struct sctp_sock *sp;
2751 struct sctp_endpoint *ep;
2752 struct sctp_association *asoc = NULL;
2753 struct sctp_setpeerprim prim;
2754 struct sctp_chunk *chunk;
2755 int err;
2756
2757 sp = sctp_sk(sk);
2758 ep = sp->ep;
2759
2760 if (!sctp_addip_enable)
2761 return -EPERM;
2762
2763 if (optlen != sizeof(struct sctp_setpeerprim))
2764 return -EINVAL;
2765
2766 if (copy_from_user(&prim, optval, optlen))
2767 return -EFAULT;
2768
2769 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002770 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return -EINVAL;
2772
2773 if (!asoc->peer.asconf_capable)
2774 return -EPERM;
2775
2776 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2777 return -EPERM;
2778
2779 if (!sctp_state(asoc, ESTABLISHED))
2780 return -ENOTCONN;
2781
2782 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2783 return -EADDRNOTAVAIL;
2784
2785 /* Create an ASCONF chunk with SET_PRIMARY parameter */
2786 chunk = sctp_make_asconf_set_prim(asoc,
2787 (union sctp_addr *)&prim.sspp_addr);
2788 if (!chunk)
2789 return -ENOMEM;
2790
2791 err = sctp_send_asconf(asoc, chunk);
2792
2793 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2794
2795 return err;
2796}
2797
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002798static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 int optlen)
2800{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002801 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002803 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002805 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -EFAULT;
2807
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002808 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 return 0;
2811}
2812
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08002813/*
2814 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
2815 *
2816 * The context field in the sctp_sndrcvinfo structure is normally only
2817 * used when a failed message is retrieved holding the value that was
2818 * sent down on the actual send call. This option allows the setting of
2819 * a default context on an association basis that will be received on
2820 * reading messages from the peer. This is especially helpful in the
2821 * one-2-many model for an application to keep some reference to an
2822 * internal state machine that is processing messages on the
2823 * association. Note that the setting of this value only effects
2824 * received messages from the peer and does not effect the value that is
2825 * saved with outbound messages.
2826 */
2827static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
2828 int optlen)
2829{
2830 struct sctp_assoc_value params;
2831 struct sctp_sock *sp;
2832 struct sctp_association *asoc;
2833
2834 if (optlen != sizeof(struct sctp_assoc_value))
2835 return -EINVAL;
2836 if (copy_from_user(&params, optval, optlen))
2837 return -EFAULT;
2838
2839 sp = sctp_sk(sk);
2840
2841 if (params.assoc_id != 0) {
2842 asoc = sctp_id2assoc(sk, params.assoc_id);
2843 if (!asoc)
2844 return -EINVAL;
2845 asoc->default_rcv_context = params.assoc_value;
2846 } else {
2847 sp->default_rcv_context = params.assoc_value;
2848 }
2849
2850 return 0;
2851}
2852
Vlad Yasevichb6e13312007-04-20 12:23:15 -07002853/*
2854 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
2855 *
2856 * This options will at a minimum specify if the implementation is doing
2857 * fragmented interleave. Fragmented interleave, for a one to many
2858 * socket, is when subsequent calls to receive a message may return
2859 * parts of messages from different associations. Some implementations
2860 * may allow you to turn this value on or off. If so, when turned off,
2861 * no fragment interleave will occur (which will cause a head of line
2862 * blocking amongst multiple associations sharing the same one to many
2863 * socket). When this option is turned on, then each receive call may
2864 * come from a different association (thus the user must receive data
2865 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
2866 * association each receive belongs to.
2867 *
2868 * This option takes a boolean value. A non-zero value indicates that
2869 * fragmented interleave is on. A value of zero indicates that
2870 * fragmented interleave is off.
2871 *
2872 * Note that it is important that an implementation that allows this
2873 * option to be turned on, have it off by default. Otherwise an unaware
2874 * application using the one to many model may become confused and act
2875 * incorrectly.
2876 */
2877static int sctp_setsockopt_fragment_interleave(struct sock *sk,
2878 char __user *optval,
2879 int optlen)
2880{
2881 int val;
2882
2883 if (optlen != sizeof(int))
2884 return -EINVAL;
2885 if (get_user(val, (int __user *)optval))
2886 return -EFAULT;
2887
2888 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
2889
2890 return 0;
2891}
2892
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07002893/*
2894 * 7.1.25. Set or Get the sctp partial delivery point
2895 * (SCTP_PARTIAL_DELIVERY_POINT)
2896 * This option will set or get the SCTP partial delivery point. This
2897 * point is the size of a message where the partial delivery API will be
2898 * invoked to help free up rwnd space for the peer. Setting this to a
2899 * lower value will cause partial delivery's to happen more often. The
2900 * calls argument is an integer that sets or gets the partial delivery
2901 * point.
2902 */
2903static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
2904 char __user *optval,
2905 int optlen)
2906{
2907 u32 val;
2908
2909 if (optlen != sizeof(u32))
2910 return -EINVAL;
2911 if (get_user(val, (int __user *)optval))
2912 return -EFAULT;
2913
2914 sctp_sk(sk)->pd_point = val;
2915
2916 return 0; /* is this the right error code? */
2917}
2918
Vlad Yasevich70331572007-03-23 11:34:36 -07002919/*
2920 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
2921 *
2922 * This option will allow a user to change the maximum burst of packets
2923 * that can be emitted by this association. Note that the default value
2924 * is 4, and some implementations may restrict this setting so that it
2925 * can only be lowered.
2926 *
2927 * NOTE: This text doesn't seem right. Do this on a socket basis with
2928 * future associations inheriting the socket value.
2929 */
2930static int sctp_setsockopt_maxburst(struct sock *sk,
2931 char __user *optval,
2932 int optlen)
2933{
2934 int val;
2935
2936 if (optlen != sizeof(int))
2937 return -EINVAL;
2938 if (get_user(val, (int __user *)optval))
2939 return -EFAULT;
2940
2941 if (val < 0)
2942 return -EINVAL;
2943
2944 sctp_sk(sk)->max_burst = val;
2945
2946 return 0;
2947}
2948
Vlad Yasevich65b07e52007-09-16 19:34:00 -07002949/*
2950 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
2951 *
2952 * This set option adds a chunk type that the user is requesting to be
2953 * received only in an authenticated way. Changes to the list of chunks
2954 * will only effect future associations on the socket.
2955 */
2956static int sctp_setsockopt_auth_chunk(struct sock *sk,
2957 char __user *optval,
2958 int optlen)
2959{
2960 struct sctp_authchunk val;
2961
2962 if (optlen != sizeof(struct sctp_authchunk))
2963 return -EINVAL;
2964 if (copy_from_user(&val, optval, optlen))
2965 return -EFAULT;
2966
2967 switch (val.sauth_chunk) {
2968 case SCTP_CID_INIT:
2969 case SCTP_CID_INIT_ACK:
2970 case SCTP_CID_SHUTDOWN_COMPLETE:
2971 case SCTP_CID_AUTH:
2972 return -EINVAL;
2973 }
2974
2975 /* add this chunk id to the endpoint */
2976 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
2977}
2978
2979/*
2980 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
2981 *
2982 * This option gets or sets the list of HMAC algorithms that the local
2983 * endpoint requires the peer to use.
2984 */
2985static int sctp_setsockopt_hmac_ident(struct sock *sk,
2986 char __user *optval,
2987 int optlen)
2988{
2989 struct sctp_hmacalgo *hmacs;
2990 int err;
2991
2992 if (optlen < sizeof(struct sctp_hmacalgo))
2993 return -EINVAL;
2994
2995 hmacs = kmalloc(optlen, GFP_KERNEL);
2996 if (!hmacs)
2997 return -ENOMEM;
2998
2999 if (copy_from_user(hmacs, optval, optlen)) {
3000 err = -EFAULT;
3001 goto out;
3002 }
3003
3004 if (hmacs->shmac_num_idents == 0 ||
3005 hmacs->shmac_num_idents > SCTP_AUTH_NUM_HMACS) {
3006 err = -EINVAL;
3007 goto out;
3008 }
3009
3010 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3011out:
3012 kfree(hmacs);
3013 return err;
3014}
3015
3016/*
3017 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3018 *
3019 * This option will set a shared secret key which is used to build an
3020 * association shared key.
3021 */
3022static int sctp_setsockopt_auth_key(struct sock *sk,
3023 char __user *optval,
3024 int optlen)
3025{
3026 struct sctp_authkey *authkey;
3027 struct sctp_association *asoc;
3028 int ret;
3029
3030 if (optlen <= sizeof(struct sctp_authkey))
3031 return -EINVAL;
3032
3033 authkey = kmalloc(optlen, GFP_KERNEL);
3034 if (!authkey)
3035 return -ENOMEM;
3036
3037 if (copy_from_user(authkey, optval, optlen)) {
3038 ret = -EFAULT;
3039 goto out;
3040 }
3041
3042 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3043 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3044 ret = -EINVAL;
3045 goto out;
3046 }
3047
3048 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3049out:
3050 kfree(authkey);
3051 return ret;
3052}
3053
3054/*
3055 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3056 *
3057 * This option will get or set the active shared key to be used to build
3058 * the association shared key.
3059 */
3060static int sctp_setsockopt_active_key(struct sock *sk,
3061 char __user *optval,
3062 int optlen)
3063{
3064 struct sctp_authkeyid val;
3065 struct sctp_association *asoc;
3066
3067 if (optlen != sizeof(struct sctp_authkeyid))
3068 return -EINVAL;
3069 if (copy_from_user(&val, optval, optlen))
3070 return -EFAULT;
3071
3072 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3073 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3074 return -EINVAL;
3075
3076 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3077 val.scact_keynumber);
3078}
3079
3080/*
3081 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3082 *
3083 * This set option will delete a shared secret key from use.
3084 */
3085static int sctp_setsockopt_del_key(struct sock *sk,
3086 char __user *optval,
3087 int optlen)
3088{
3089 struct sctp_authkeyid val;
3090 struct sctp_association *asoc;
3091
3092 if (optlen != sizeof(struct sctp_authkeyid))
3093 return -EINVAL;
3094 if (copy_from_user(&val, optval, optlen))
3095 return -EFAULT;
3096
3097 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3098 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3099 return -EINVAL;
3100
3101 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3102 val.scact_keynumber);
3103
3104}
3105
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107/* API 6.2 setsockopt(), getsockopt()
3108 *
3109 * Applications use setsockopt() and getsockopt() to set or retrieve
3110 * socket options. Socket options are used to change the default
3111 * behavior of sockets calls. They are described in Section 7.
3112 *
3113 * The syntax is:
3114 *
3115 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3116 * int __user *optlen);
3117 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3118 * int optlen);
3119 *
3120 * sd - the socket descript.
3121 * level - set to IPPROTO_SCTP for all SCTP options.
3122 * optname - the option name.
3123 * optval - the buffer to store the value of the option.
3124 * optlen - the size of the buffer.
3125 */
3126SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
3127 char __user *optval, int optlen)
3128{
3129 int retval = 0;
3130
3131 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3132 sk, optname);
3133
3134 /* I can hardly begin to describe how wrong this is. This is
3135 * so broken as to be worse than useless. The API draft
3136 * REALLY is NOT helpful here... I am not convinced that the
3137 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3138 * are at all well-founded.
3139 */
3140 if (level != SOL_SCTP) {
3141 struct sctp_af *af = sctp_sk(sk)->pf->af;
3142 retval = af->setsockopt(sk, level, optname, optval, optlen);
3143 goto out_nounlock;
3144 }
3145
3146 sctp_lock_sock(sk);
3147
3148 switch (optname) {
3149 case SCTP_SOCKOPT_BINDX_ADD:
3150 /* 'optlen' is the size of the addresses buffer. */
3151 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3152 optlen, SCTP_BINDX_ADD_ADDR);
3153 break;
3154
3155 case SCTP_SOCKOPT_BINDX_REM:
3156 /* 'optlen' is the size of the addresses buffer. */
3157 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3158 optlen, SCTP_BINDX_REM_ADDR);
3159 break;
3160
Frank Filz3f7a87d2005-06-20 13:14:57 -07003161 case SCTP_SOCKOPT_CONNECTX:
3162 /* 'optlen' is the size of the addresses buffer. */
3163 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
3164 optlen);
3165 break;
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 case SCTP_DISABLE_FRAGMENTS:
3168 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3169 break;
3170
3171 case SCTP_EVENTS:
3172 retval = sctp_setsockopt_events(sk, optval, optlen);
3173 break;
3174
3175 case SCTP_AUTOCLOSE:
3176 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3177 break;
3178
3179 case SCTP_PEER_ADDR_PARAMS:
3180 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3181 break;
3182
Frank Filz77086102005-12-22 11:37:30 -08003183 case SCTP_DELAYED_ACK_TIME:
3184 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
3185 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003186 case SCTP_PARTIAL_DELIVERY_POINT:
3187 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3188 break;
Frank Filz77086102005-12-22 11:37:30 -08003189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 case SCTP_INITMSG:
3191 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3192 break;
3193 case SCTP_DEFAULT_SEND_PARAM:
3194 retval = sctp_setsockopt_default_send_param(sk, optval,
3195 optlen);
3196 break;
3197 case SCTP_PRIMARY_ADDR:
3198 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3199 break;
3200 case SCTP_SET_PEER_PRIMARY_ADDR:
3201 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3202 break;
3203 case SCTP_NODELAY:
3204 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3205 break;
3206 case SCTP_RTOINFO:
3207 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3208 break;
3209 case SCTP_ASSOCINFO:
3210 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3211 break;
3212 case SCTP_I_WANT_MAPPED_V4_ADDR:
3213 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3214 break;
3215 case SCTP_MAXSEG:
3216 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3217 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003218 case SCTP_ADAPTATION_LAYER:
3219 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003221 case SCTP_CONTEXT:
3222 retval = sctp_setsockopt_context(sk, optval, optlen);
3223 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003224 case SCTP_FRAGMENT_INTERLEAVE:
3225 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3226 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003227 case SCTP_MAX_BURST:
3228 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3229 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003230 case SCTP_AUTH_CHUNK:
3231 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3232 break;
3233 case SCTP_HMAC_IDENT:
3234 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3235 break;
3236 case SCTP_AUTH_KEY:
3237 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3238 break;
3239 case SCTP_AUTH_ACTIVE_KEY:
3240 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3241 break;
3242 case SCTP_AUTH_DELETE_KEY:
3243 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 default:
3246 retval = -ENOPROTOOPT;
3247 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 sctp_release_sock(sk);
3251
3252out_nounlock:
3253 return retval;
3254}
3255
3256/* API 3.1.6 connect() - UDP Style Syntax
3257 *
3258 * An application may use the connect() call in the UDP model to initiate an
3259 * association without sending data.
3260 *
3261 * The syntax is:
3262 *
3263 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3264 *
3265 * sd: the socket descriptor to have a new association added to.
3266 *
3267 * nam: the address structure (either struct sockaddr_in or struct
3268 * sockaddr_in6 defined in RFC2553 [7]).
3269 *
3270 * len: the size of the address.
3271 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003272SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 int addr_len)
3274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003276 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 sctp_lock_sock(sk);
3279
Frank Filz3f7a87d2005-06-20 13:14:57 -07003280 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
3281 __FUNCTION__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Frank Filz3f7a87d2005-06-20 13:14:57 -07003283 /* Validate addr_len before calling common connect/connectx routine. */
3284 af = sctp_get_af_specific(addr->sa_family);
3285 if (!af || addr_len < af->sockaddr_len) {
3286 err = -EINVAL;
3287 } else {
3288 /* Pass correct addr len to common routine (so it knows there
3289 * is only one address being passed.
3290 */
3291 err = __sctp_connect(sk, addr, af->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return err;
3296}
3297
3298/* FIXME: Write comments. */
3299SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3300{
3301 return -EOPNOTSUPP; /* STUB */
3302}
3303
3304/* 4.1.4 accept() - TCP Style Syntax
3305 *
3306 * Applications use accept() call to remove an established SCTP
3307 * association from the accept queue of the endpoint. A new socket
3308 * descriptor will be returned from accept() to represent the newly
3309 * formed association.
3310 */
3311SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3312{
3313 struct sctp_sock *sp;
3314 struct sctp_endpoint *ep;
3315 struct sock *newsk = NULL;
3316 struct sctp_association *asoc;
3317 long timeo;
3318 int error = 0;
3319
3320 sctp_lock_sock(sk);
3321
3322 sp = sctp_sk(sk);
3323 ep = sp->ep;
3324
3325 if (!sctp_style(sk, TCP)) {
3326 error = -EOPNOTSUPP;
3327 goto out;
3328 }
3329
3330 if (!sctp_sstate(sk, LISTENING)) {
3331 error = -EINVAL;
3332 goto out;
3333 }
3334
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003335 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 error = sctp_wait_for_accept(sk, timeo);
3338 if (error)
3339 goto out;
3340
3341 /* We treat the list of associations on the endpoint as the accept
3342 * queue and pick the first association on the list.
3343 */
3344 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3345
3346 newsk = sp->pf->create_accept_sk(sk, asoc);
3347 if (!newsk) {
3348 error = -ENOMEM;
3349 goto out;
3350 }
3351
3352 /* Populate the fields of the newsk from the oldsk and migrate the
3353 * asoc to the newsk.
3354 */
3355 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3356
3357out:
3358 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003359 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return newsk;
3361}
3362
3363/* The SCTP ioctl handler. */
3364SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3365{
3366 return -ENOIOCTLCMD;
3367}
3368
3369/* This is the function which gets called during socket creation to
3370 * initialized the SCTP-specific portion of the sock.
3371 * The sock structure should already be zero-filled memory.
3372 */
3373SCTP_STATIC int sctp_init_sock(struct sock *sk)
3374{
3375 struct sctp_endpoint *ep;
3376 struct sctp_sock *sp;
3377
3378 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3379
3380 sp = sctp_sk(sk);
3381
3382 /* Initialize the SCTP per socket area. */
3383 switch (sk->sk_type) {
3384 case SOCK_SEQPACKET:
3385 sp->type = SCTP_SOCKET_UDP;
3386 break;
3387 case SOCK_STREAM:
3388 sp->type = SCTP_SOCKET_TCP;
3389 break;
3390 default:
3391 return -ESOCKTNOSUPPORT;
3392 }
3393
3394 /* Initialize default send parameters. These parameters can be
3395 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3396 */
3397 sp->default_stream = 0;
3398 sp->default_ppid = 0;
3399 sp->default_flags = 0;
3400 sp->default_context = 0;
3401 sp->default_timetolive = 0;
3402
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003403 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003404 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 /* Initialize default setup parameters. These parameters
3407 * can be modified with the SCTP_INITMSG socket option or
3408 * overridden by the SCTP_INIT CMSG.
3409 */
3410 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3411 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3412 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003413 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 /* Initialize default RTO related parameters. These parameters can
3416 * be modified for with the SCTP_RTOINFO socket option.
3417 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003418 sp->rtoinfo.srto_initial = sctp_rto_initial;
3419 sp->rtoinfo.srto_max = sctp_rto_max;
3420 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
3422 /* Initialize default association related parameters. These parameters
3423 * can be modified with the SCTP_ASSOCINFO socket option.
3424 */
3425 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3426 sp->assocparams.sasoc_number_peer_destinations = 0;
3427 sp->assocparams.sasoc_peer_rwnd = 0;
3428 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003429 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003432 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 */
3434 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3435
3436 /* Default Peer Address Parameters. These defaults can
3437 * be modified via SCTP_PEER_ADDR_PARAMS
3438 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003439 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003440 sp->pathmaxrxt = sctp_max_retrans_path;
3441 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003442 sp->sackdelay = sctp_sack_timeout;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003443 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003444 SPP_PMTUD_ENABLE |
3445 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 /* If enabled no SCTP message fragmentation will be performed.
3448 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3449 */
3450 sp->disable_fragments = 0;
3451
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003452 /* Enable Nagle algorithm by default. */
3453 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 /* Enable by default. */
3456 sp->v4mapped = 1;
3457
3458 /* Auto-close idle associations after the configured
3459 * number of seconds. A value of 0 disables this
3460 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3461 * for UDP-style sockets only.
3462 */
3463 sp->autoclose = 0;
3464
3465 /* User specified fragmentation limit. */
3466 sp->user_frag = 0;
3467
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003468 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 sp->pf = sctp_get_pf_specific(sk->sk_family);
3471
3472 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003473 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003475 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* Create a per socket endpoint structure. Even if we
3478 * change the data structure relationships, this may still
3479 * be useful for storing pre-connect address information.
3480 */
3481 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3482 if (!ep)
3483 return -ENOMEM;
3484
3485 sp->ep = ep;
3486 sp->hmac = NULL;
3487
3488 SCTP_DBG_OBJCNT_INC(sock);
Neil Horman4d93df02007-08-15 16:07:44 -07003489 atomic_inc(&sctp_sockets_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return 0;
3491}
3492
3493/* Cleanup any SCTP per socket resources. */
3494SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3495{
3496 struct sctp_endpoint *ep;
3497
3498 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3499
3500 /* Release our hold on the endpoint. */
3501 ep = sctp_sk(sk)->ep;
3502 sctp_endpoint_free(ep);
Neil Horman4d93df02007-08-15 16:07:44 -07003503 atomic_dec(&sctp_sockets_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return 0;
3505}
3506
3507/* API 4.1.7 shutdown() - TCP Style Syntax
3508 * int shutdown(int socket, int how);
3509 *
3510 * sd - the socket descriptor of the association to be closed.
3511 * how - Specifies the type of shutdown. The values are
3512 * as follows:
3513 * SHUT_RD
3514 * Disables further receive operations. No SCTP
3515 * protocol action is taken.
3516 * SHUT_WR
3517 * Disables further send operations, and initiates
3518 * the SCTP shutdown sequence.
3519 * SHUT_RDWR
3520 * Disables further send and receive operations
3521 * and initiates the SCTP shutdown sequence.
3522 */
3523SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3524{
3525 struct sctp_endpoint *ep;
3526 struct sctp_association *asoc;
3527
3528 if (!sctp_style(sk, TCP))
3529 return;
3530
3531 if (how & SEND_SHUTDOWN) {
3532 ep = sctp_sk(sk)->ep;
3533 if (!list_empty(&ep->asocs)) {
3534 asoc = list_entry(ep->asocs.next,
3535 struct sctp_association, asocs);
3536 sctp_primitive_SHUTDOWN(asoc, NULL);
3537 }
3538 }
3539}
3540
3541/* 7.2.1 Association Status (SCTP_STATUS)
3542
3543 * Applications can retrieve current status information about an
3544 * association, including association state, peer receiver window size,
3545 * number of unacked data chunks, and number of data chunks pending
3546 * receipt. This information is read-only.
3547 */
3548static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3549 char __user *optval,
3550 int __user *optlen)
3551{
3552 struct sctp_status status;
3553 struct sctp_association *asoc = NULL;
3554 struct sctp_transport *transport;
3555 sctp_assoc_t associd;
3556 int retval = 0;
3557
Neil Horman408f22e2007-06-16 14:03:45 -04003558 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 retval = -EINVAL;
3560 goto out;
3561 }
3562
Neil Horman408f22e2007-06-16 14:03:45 -04003563 len = sizeof(status);
3564 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 retval = -EFAULT;
3566 goto out;
3567 }
3568
3569 associd = status.sstat_assoc_id;
3570 asoc = sctp_id2assoc(sk, associd);
3571 if (!asoc) {
3572 retval = -EINVAL;
3573 goto out;
3574 }
3575
3576 transport = asoc->peer.primary_path;
3577
3578 status.sstat_assoc_id = sctp_assoc2id(asoc);
3579 status.sstat_state = asoc->state;
3580 status.sstat_rwnd = asoc->peer.rwnd;
3581 status.sstat_unackdata = asoc->unack_data;
3582
3583 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3584 status.sstat_instrms = asoc->c.sinit_max_instreams;
3585 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3586 status.sstat_fragmentation_point = asoc->frag_point;
3587 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08003588 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
3589 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 /* Map ipv4 address into v4-mapped-on-v6 address. */
3591 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3592 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003593 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 status.sstat_primary.spinfo_cwnd = transport->cwnd;
3595 status.sstat_primary.spinfo_srtt = transport->srtt;
3596 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003597 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Frank Filz3f7a87d2005-06-20 13:14:57 -07003599 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3600 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (put_user(len, optlen)) {
3603 retval = -EFAULT;
3604 goto out;
3605 }
3606
3607 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3608 len, status.sstat_state, status.sstat_rwnd,
3609 status.sstat_assoc_id);
3610
3611 if (copy_to_user(optval, &status, len)) {
3612 retval = -EFAULT;
3613 goto out;
3614 }
3615
3616out:
3617 return (retval);
3618}
3619
3620
3621/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3622 *
3623 * Applications can retrieve information about a specific peer address
3624 * of an association, including its reachability state, congestion
3625 * window, and retransmission timer values. This information is
3626 * read-only.
3627 */
3628static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3629 char __user *optval,
3630 int __user *optlen)
3631{
3632 struct sctp_paddrinfo pinfo;
3633 struct sctp_transport *transport;
3634 int retval = 0;
3635
Neil Horman408f22e2007-06-16 14:03:45 -04003636 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 retval = -EINVAL;
3638 goto out;
3639 }
3640
Neil Horman408f22e2007-06-16 14:03:45 -04003641 len = sizeof(pinfo);
3642 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 retval = -EFAULT;
3644 goto out;
3645 }
3646
3647 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3648 pinfo.spinfo_assoc_id);
3649 if (!transport)
3650 return -EINVAL;
3651
3652 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003653 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 pinfo.spinfo_cwnd = transport->cwnd;
3655 pinfo.spinfo_srtt = transport->srtt;
3656 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08003657 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Frank Filz3f7a87d2005-06-20 13:14:57 -07003659 if (pinfo.spinfo_state == SCTP_UNKNOWN)
3660 pinfo.spinfo_state = SCTP_ACTIVE;
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (put_user(len, optlen)) {
3663 retval = -EFAULT;
3664 goto out;
3665 }
3666
3667 if (copy_to_user(optval, &pinfo, len)) {
3668 retval = -EFAULT;
3669 goto out;
3670 }
3671
3672out:
3673 return (retval);
3674}
3675
3676/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3677 *
3678 * This option is a on/off flag. If enabled no SCTP message
3679 * fragmentation will be performed. Instead if a message being sent
3680 * exceeds the current PMTU size, the message will NOT be sent and
3681 * instead a error will be indicated to the user.
3682 */
3683static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3684 char __user *optval, int __user *optlen)
3685{
3686 int val;
3687
3688 if (len < sizeof(int))
3689 return -EINVAL;
3690
3691 len = sizeof(int);
3692 val = (sctp_sk(sk)->disable_fragments == 1);
3693 if (put_user(len, optlen))
3694 return -EFAULT;
3695 if (copy_to_user(optval, &val, len))
3696 return -EFAULT;
3697 return 0;
3698}
3699
3700/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3701 *
3702 * This socket option is used to specify various notifications and
3703 * ancillary data the user wishes to receive.
3704 */
3705static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3706 int __user *optlen)
3707{
Neil Horman408f22e2007-06-16 14:03:45 -04003708 if (len < sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003710 len = sizeof(struct sctp_event_subscribe);
3711 if (put_user(len, optlen))
3712 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3714 return -EFAULT;
3715 return 0;
3716}
3717
3718/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3719 *
3720 * This socket option is applicable to the UDP-style socket only. When
3721 * set it will cause associations that are idle for more than the
3722 * specified number of seconds to automatically close. An association
3723 * being idle is defined an association that has NOT sent or received
3724 * user data. The special value of '0' indicates that no automatic
3725 * close of any associations should be performed. The option expects an
3726 * integer defining the number of seconds of idle time before an
3727 * association is closed.
3728 */
3729static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3730{
3731 /* Applicable to UDP-style socket only */
3732 if (sctp_style(sk, TCP))
3733 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04003734 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003736 len = sizeof(int);
3737 if (put_user(len, optlen))
3738 return -EFAULT;
3739 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return -EFAULT;
3741 return 0;
3742}
3743
3744/* Helper routine to branch off an association to a new socket. */
3745SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3746 struct socket **sockp)
3747{
3748 struct sock *sk = asoc->base.sk;
3749 struct socket *sock;
Vlad Yasevich4f444302006-10-30 18:54:32 -08003750 struct inet_sock *inetsk;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003751 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 int err = 0;
3753
3754 /* An association cannot be branched off from an already peeled-off
3755 * socket, nor is this supported for tcp style sockets.
3756 */
3757 if (!sctp_style(sk, UDP))
3758 return -EINVAL;
3759
3760 /* Create a new socket. */
3761 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3762 if (err < 0)
3763 return err;
3764
3765 /* Populate the fields of the newsk from the oldsk and migrate the
3766 * asoc to the newsk.
3767 */
3768 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003769
3770 /* Make peeled-off sockets more like 1-1 accepted sockets.
3771 * Set the daddr and initialize id to something more random
3772 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04003773 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
3774 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003775 inetsk = inet_sk(sock->sk);
Vlad Yasevich4f444302006-10-30 18:54:32 -08003776 inetsk->id = asoc->next_tsn ^ jiffies;
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 *sockp = sock;
3779
3780 return err;
3781}
3782
3783static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3784{
3785 sctp_peeloff_arg_t peeloff;
3786 struct socket *newsock;
3787 int retval = 0;
3788 struct sctp_association *asoc;
3789
Neil Horman408f22e2007-06-16 14:03:45 -04003790 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003792 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 if (copy_from_user(&peeloff, optval, len))
3794 return -EFAULT;
3795
3796 asoc = sctp_id2assoc(sk, peeloff.associd);
3797 if (!asoc) {
3798 retval = -EINVAL;
3799 goto out;
3800 }
3801
3802 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3803
3804 retval = sctp_do_peeloff(asoc, &newsock);
3805 if (retval < 0)
3806 goto out;
3807
3808 /* Map the socket to an unused fd that can be returned to the user. */
3809 retval = sock_map_fd(newsock);
3810 if (retval < 0) {
3811 sock_release(newsock);
3812 goto out;
3813 }
3814
3815 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3816 __FUNCTION__, sk, asoc, newsock->sk, retval);
3817
3818 /* Return the fd mapped to the new socket. */
3819 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04003820 if (put_user(len, optlen))
3821 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 if (copy_to_user(optval, &peeloff, len))
3823 retval = -EFAULT;
3824
3825out:
3826 return retval;
3827}
3828
3829/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3830 *
3831 * Applications can enable or disable heartbeats for any peer address of
3832 * an association, modify an address's heartbeat interval, force a
3833 * heartbeat to be sent immediately, and adjust the address's maximum
3834 * number of retransmissions sent before an address is considered
3835 * unreachable. The following structure is used to access and modify an
3836 * address's parameters:
3837 *
3838 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08003839 * sctp_assoc_t spp_assoc_id;
3840 * struct sockaddr_storage spp_address;
3841 * uint32_t spp_hbinterval;
3842 * uint16_t spp_pathmaxrxt;
3843 * uint32_t spp_pathmtu;
3844 * uint32_t spp_sackdelay;
3845 * uint32_t spp_flags;
3846 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08003848 * spp_assoc_id - (one-to-many style socket) This is filled in the
3849 * application, and identifies the association for
3850 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 * spp_address - This specifies which address is of interest.
3852 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003853 * in milliseconds. If a value of zero
3854 * is present in this field then no changes are to
3855 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 * spp_pathmaxrxt - This contains the maximum number of
3857 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08003858 * considered unreachable. If a value of zero
3859 * is present in this field then no changes are to
3860 * be made to this parameter.
3861 * spp_pathmtu - When Path MTU discovery is disabled the value
3862 * specified here will be the "fixed" path mtu.
3863 * Note that if the spp_address field is empty
3864 * then all associations on this address will
3865 * have this fixed path mtu set upon them.
3866 *
3867 * spp_sackdelay - When delayed sack is enabled, this value specifies
3868 * the number of milliseconds that sacks will be delayed
3869 * for. This value will apply to all addresses of an
3870 * association if the spp_address field is empty. Note
3871 * also, that if delayed sack is enabled and this
3872 * value is set to 0, no change is made to the last
3873 * recorded delayed sack timer value.
3874 *
3875 * spp_flags - These flags are used to control various features
3876 * on an association. The flag field may contain
3877 * zero or more of the following options.
3878 *
3879 * SPP_HB_ENABLE - Enable heartbeats on the
3880 * specified address. Note that if the address
3881 * field is empty all addresses for the association
3882 * have heartbeats enabled upon them.
3883 *
3884 * SPP_HB_DISABLE - Disable heartbeats on the
3885 * speicifed address. Note that if the address
3886 * field is empty all addresses for the association
3887 * will have their heartbeats disabled. Note also
3888 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
3889 * mutually exclusive, only one of these two should
3890 * be specified. Enabling both fields will have
3891 * undetermined results.
3892 *
3893 * SPP_HB_DEMAND - Request a user initiated heartbeat
3894 * to be made immediately.
3895 *
3896 * SPP_PMTUD_ENABLE - This field will enable PMTU
3897 * discovery upon the specified address. Note that
3898 * if the address feild is empty then all addresses
3899 * on the association are effected.
3900 *
3901 * SPP_PMTUD_DISABLE - This field will disable PMTU
3902 * discovery upon the specified address. Note that
3903 * if the address feild is empty then all addresses
3904 * on the association are effected. Not also that
3905 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3906 * exclusive. Enabling both will have undetermined
3907 * results.
3908 *
3909 * SPP_SACKDELAY_ENABLE - Setting this flag turns
3910 * on delayed sack. The time specified in spp_sackdelay
3911 * is used to specify the sack delay for this address. Note
3912 * that if spp_address is empty then all addresses will
3913 * enable delayed sack and take on the sack delay
3914 * value specified in spp_sackdelay.
3915 * SPP_SACKDELAY_DISABLE - Setting this flag turns
3916 * off delayed sack. If the spp_address field is blank then
3917 * delayed sack is disabled for the entire association. Note
3918 * also that this field is mutually exclusive to
3919 * SPP_SACKDELAY_ENABLE, setting both will have undefined
3920 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 */
3922static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08003923 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Frank Filz52ccb8e2005-12-22 11:36:46 -08003925 struct sctp_paddrparams params;
3926 struct sctp_transport *trans = NULL;
3927 struct sctp_association *asoc = NULL;
3928 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Neil Horman408f22e2007-06-16 14:03:45 -04003930 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04003932 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (copy_from_user(&params, optval, len))
3934 return -EFAULT;
3935
Frank Filz52ccb8e2005-12-22 11:36:46 -08003936 /* If an address other than INADDR_ANY is specified, and
3937 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003939 if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3940 trans = sctp_addr_id2transport(sk, &params.spp_address,
3941 params.spp_assoc_id);
3942 if (!trans) {
3943 SCTP_DEBUG_PRINTK("Failed no transport\n");
3944 return -EINVAL;
3945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947
Frank Filz52ccb8e2005-12-22 11:36:46 -08003948 /* Get association, if assoc_id != 0 and the socket is a one
3949 * to many style socket, and an association was not found, then
3950 * the id was invalid.
3951 */
3952 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3953 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3954 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Frank Filz52ccb8e2005-12-22 11:36:46 -08003958 if (trans) {
3959 /* Fetch transport values. */
3960 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3961 params.spp_pathmtu = trans->pathmtu;
3962 params.spp_pathmaxrxt = trans->pathmaxrxt;
3963 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
Frank Filz52ccb8e2005-12-22 11:36:46 -08003965 /*draft-11 doesn't say what to return in spp_flags*/
3966 params.spp_flags = trans->param_flags;
3967 } else if (asoc) {
3968 /* Fetch association values. */
3969 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3970 params.spp_pathmtu = asoc->pathmtu;
3971 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3972 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Frank Filz52ccb8e2005-12-22 11:36:46 -08003974 /*draft-11 doesn't say what to return in spp_flags*/
3975 params.spp_flags = asoc->param_flags;
3976 } else {
3977 /* Fetch socket values. */
3978 params.spp_hbinterval = sp->hbinterval;
3979 params.spp_pathmtu = sp->pathmtu;
3980 params.spp_sackdelay = sp->sackdelay;
3981 params.spp_pathmaxrxt = sp->pathmaxrxt;
3982
3983 /*draft-11 doesn't say what to return in spp_flags*/
3984 params.spp_flags = sp->param_flags;
3985 }
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 if (copy_to_user(optval, &params, len))
3988 return -EFAULT;
3989
3990 if (put_user(len, optlen))
3991 return -EFAULT;
3992
3993 return 0;
3994}
3995
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003996/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
Frank Filz77086102005-12-22 11:37:30 -08003997 *
3998 * This options will get or set the delayed ack timer. The time is set
3999 * in milliseconds. If the assoc_id is 0, then this sets or gets the
4000 * endpoints default delayed ack timer value. If the assoc_id field is
4001 * non-zero, then the set or get effects the specified association.
4002 *
4003 * struct sctp_assoc_value {
4004 * sctp_assoc_t assoc_id;
4005 * uint32_t assoc_value;
4006 * };
4007 *
4008 * assoc_id - This parameter, indicates which association the
4009 * user is preforming an action upon. Note that if
4010 * this field's value is zero then the endpoints
4011 * default value is changed (effecting future
4012 * associations only).
4013 *
4014 * assoc_value - This parameter contains the number of milliseconds
4015 * that the user is requesting the delayed ACK timer
4016 * be set to. Note that this value is defined in
4017 * the standard to be between 200 and 500 milliseconds.
4018 *
4019 * Note: a value of zero will leave the value alone,
4020 * but disable SACK delay. A non-zero value will also
4021 * enable SACK delay.
4022 */
4023static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
4024 char __user *optval,
4025 int __user *optlen)
4026{
4027 struct sctp_assoc_value params;
4028 struct sctp_association *asoc = NULL;
4029 struct sctp_sock *sp = sctp_sk(sk);
4030
Neil Horman408f22e2007-06-16 14:03:45 -04004031 if (len < sizeof(struct sctp_assoc_value))
Frank Filz77086102005-12-22 11:37:30 -08004032 return - EINVAL;
4033
Neil Horman408f22e2007-06-16 14:03:45 -04004034 len = sizeof(struct sctp_assoc_value);
4035
Frank Filz77086102005-12-22 11:37:30 -08004036 if (copy_from_user(&params, optval, len))
4037 return -EFAULT;
4038
4039 /* Get association, if assoc_id != 0 and the socket is a one
4040 * to many style socket, and an association was not found, then
4041 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004042 */
Frank Filz77086102005-12-22 11:37:30 -08004043 asoc = sctp_id2assoc(sk, params.assoc_id);
4044 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
4045 return -EINVAL;
4046
4047 if (asoc) {
4048 /* Fetch association values. */
4049 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
4050 params.assoc_value = jiffies_to_msecs(
4051 asoc->sackdelay);
4052 else
4053 params.assoc_value = 0;
4054 } else {
4055 /* Fetch socket values. */
4056 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
4057 params.assoc_value = sp->sackdelay;
4058 else
4059 params.assoc_value = 0;
4060 }
4061
4062 if (copy_to_user(optval, &params, len))
4063 return -EFAULT;
4064
4065 if (put_user(len, optlen))
4066 return -EFAULT;
4067
4068 return 0;
4069}
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4072 *
4073 * Applications can specify protocol parameters for the default association
4074 * initialization. The option name argument to setsockopt() and getsockopt()
4075 * is SCTP_INITMSG.
4076 *
4077 * Setting initialization parameters is effective only on an unconnected
4078 * socket (for UDP-style sockets only future associations are effected
4079 * by the change). With TCP-style sockets, this option is inherited by
4080 * sockets derived from a listener socket.
4081 */
4082static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4083{
Neil Horman408f22e2007-06-16 14:03:45 -04004084 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004086 len = sizeof(struct sctp_initmsg);
4087 if (put_user(len, optlen))
4088 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4090 return -EFAULT;
4091 return 0;
4092}
4093
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004094static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
4095 char __user *optval,
4096 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
4098 sctp_assoc_t id;
4099 struct sctp_association *asoc;
4100 struct list_head *pos;
4101 int cnt = 0;
4102
Neil Horman408f22e2007-06-16 14:03:45 -04004103 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 return -EINVAL;
4105
4106 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4107 return -EFAULT;
4108
4109 /* For UDP-style sockets, id specifies the association to query. */
4110 asoc = sctp_id2assoc(sk, id);
4111 if (!asoc)
4112 return -EINVAL;
4113
4114 list_for_each(pos, &asoc->peer.transport_addr_list) {
4115 cnt ++;
4116 }
4117
4118 return cnt;
4119}
4120
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004121/*
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004122 * Old API for getting list of peer addresses. Does not work for 32-bit
4123 * programs running on a 64-bit kernel
4124 */
4125static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
4126 char __user *optval,
4127 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 struct sctp_association *asoc;
4130 struct list_head *pos;
4131 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004132 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 struct sctp_transport *from;
4134 void __user *to;
4135 union sctp_addr temp;
4136 struct sctp_sock *sp = sctp_sk(sk);
4137 int addrlen;
4138
Neil Horman408f22e2007-06-16 14:03:45 -04004139 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return -EINVAL;
4141
Neil Horman408f22e2007-06-16 14:03:45 -04004142 len = sizeof(struct sctp_getaddrs_old);
4143
4144 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return -EFAULT;
4146
4147 if (getaddrs.addr_num <= 0) return -EINVAL;
4148
4149 /* For UDP-style sockets, id specifies the association to query. */
4150 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4151 if (!asoc)
4152 return -EINVAL;
4153
4154 to = (void __user *)getaddrs.addrs;
4155 list_for_each(pos, &asoc->peer.transport_addr_list) {
4156 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004157 memcpy(&temp, &from->ipaddr, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4159 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (copy_to_user(to, &temp, addrlen))
4161 return -EFAULT;
4162 to += addrlen ;
4163 cnt ++;
4164 if (cnt >= getaddrs.addr_num) break;
4165 }
4166 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004167 if (put_user(len, optlen))
4168 return -EFAULT;
4169 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return -EFAULT;
4171
4172 return 0;
4173}
4174
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004175static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4176 char __user *optval, int __user *optlen)
4177{
4178 struct sctp_association *asoc;
4179 struct list_head *pos;
4180 int cnt = 0;
4181 struct sctp_getaddrs getaddrs;
4182 struct sctp_transport *from;
4183 void __user *to;
4184 union sctp_addr temp;
4185 struct sctp_sock *sp = sctp_sk(sk);
4186 int addrlen;
4187 size_t space_left;
4188 int bytes_copied;
4189
4190 if (len < sizeof(struct sctp_getaddrs))
4191 return -EINVAL;
4192
4193 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4194 return -EFAULT;
4195
4196 /* For UDP-style sockets, id specifies the association to query. */
4197 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4198 if (!asoc)
4199 return -EINVAL;
4200
4201 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004202 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004203
4204 list_for_each(pos, &asoc->peer.transport_addr_list) {
4205 from = list_entry(pos, struct sctp_transport, transports);
Al Virob3f5b3b2006-11-20 17:22:43 -08004206 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004207 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4208 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004209 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004210 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004211 if (copy_to_user(to, &temp, addrlen))
4212 return -EFAULT;
4213 to += addrlen;
4214 cnt++;
4215 space_left -= addrlen;
4216 }
4217
4218 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4219 return -EFAULT;
4220 bytes_copied = ((char __user *)to) - optval;
4221 if (put_user(bytes_copied, optlen))
4222 return -EFAULT;
4223
4224 return 0;
4225}
4226
4227static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
4228 char __user *optval,
4229 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
4231 sctp_assoc_t id;
4232 struct sctp_bind_addr *bp;
4233 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 int cnt = 0;
4236
Neil Horman408f22e2007-06-16 14:03:45 -04004237 if (len < sizeof(sctp_assoc_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return -EINVAL;
4239
4240 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
4241 return -EFAULT;
4242
4243 /*
4244 * For UDP-style sockets, id specifies the association to query.
4245 * If the id field is set to the value '0' then the locally bound
4246 * addresses are returned without regard to any particular
4247 * association.
4248 */
4249 if (0 == id) {
4250 bp = &sctp_sk(sk)->ep->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 } else {
4252 asoc = sctp_id2assoc(sk, id);
4253 if (!asoc)
4254 return -EINVAL;
4255 bp = &asoc->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 }
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
4259 * addresses from the global local address list.
4260 */
4261 if (sctp_list_single_entry(&bp->address_list)) {
4262 addr = list_entry(bp->address_list.next,
4263 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004264 if (sctp_is_any(&addr->a)) {
Vlad Yasevich29303542007-09-16 16:02:12 -07004265 rcu_read_lock();
4266 list_for_each_entry_rcu(addr,
4267 &sctp_local_addr_list, list) {
4268 if (!addr->valid)
4269 continue;
4270
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004271 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004272 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 continue;
Vlad Yasevich29303542007-09-16 16:02:12 -07004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 cnt++;
4276 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004277 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 } else {
4279 cnt = 1;
4280 }
4281 goto done;
4282 }
4283
Vlad Yasevich559cf712007-09-16 16:03:28 -07004284 /* Protection on the bound address list is not needed,
4285 * since in the socket option context we hold the socket lock,
4286 * so there is no way that the bound address list can change.
4287 */
4288 list_for_each_entry(addr, &bp->address_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 cnt ++;
4290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return cnt;
4293}
4294
4295/* Helper function that copies local addresses to user and returns the number
4296 * of addresses copied.
4297 */
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004298static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
4299 int max_addrs, void *to,
4300 int *bytes_copied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 struct sctp_sockaddr_entry *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 union sctp_addr temp;
4304 int cnt = 0;
4305 int addrlen;
4306
Vlad Yasevich29303542007-09-16 16:02:12 -07004307 rcu_read_lock();
4308 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4309 if (!addr->valid)
4310 continue;
4311
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004312 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004313 (AF_INET6 == addr->a.sa.sa_family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 continue;
Al Viro6244be42006-11-20 17:21:44 -08004315 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4317 &temp);
4318 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004319 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 to += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004322 *bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 cnt ++;
4324 if (cnt >= max_addrs) break;
4325 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 return cnt;
4329}
4330
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004331static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4332 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004333{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004334 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004335 union sctp_addr temp;
4336 int cnt = 0;
4337 int addrlen;
4338
Vlad Yasevich29303542007-09-16 16:02:12 -07004339 rcu_read_lock();
4340 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4341 if (!addr->valid)
4342 continue;
4343
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004344 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004345 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004346 continue;
Al Viro6244be42006-11-20 17:21:44 -08004347 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004348 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4349 &temp);
4350 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004351 if (space_left < addrlen) {
4352 cnt = -ENOMEM;
4353 break;
4354 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004355 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004356
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004357 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004358 cnt ++;
4359 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004360 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004361 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004362 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004363
4364 return cnt;
4365}
4366
4367/* Old API for getting list of local addresses. Does not work for 32-bit
4368 * programs running on a 64-bit kernel
4369 */
4370static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
4371 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 struct sctp_bind_addr *bp;
4374 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 int cnt = 0;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004376 struct sctp_getaddrs_old getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 struct sctp_sockaddr_entry *addr;
4378 void __user *to;
4379 union sctp_addr temp;
4380 struct sctp_sock *sp = sctp_sk(sk);
4381 int addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 int err = 0;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004383 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004384 void *buf;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004385 int bytes_copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Neil Horman408f22e2007-06-16 14:03:45 -04004387 if (len < sizeof(struct sctp_getaddrs_old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return -EINVAL;
4389
Neil Horman408f22e2007-06-16 14:03:45 -04004390 len = sizeof(struct sctp_getaddrs_old);
4391 if (copy_from_user(&getaddrs, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return -EFAULT;
4393
4394 if (getaddrs.addr_num <= 0) return -EINVAL;
4395 /*
4396 * For UDP-style sockets, id specifies the association to query.
4397 * If the id field is set to the value '0' then the locally bound
4398 * addresses are returned without regard to any particular
4399 * association.
4400 */
4401 if (0 == getaddrs.assoc_id) {
4402 bp = &sctp_sk(sk)->ep->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 } else {
4404 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4405 if (!asoc)
4406 return -EINVAL;
4407 bp = &asoc->base.bind_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409
4410 to = getaddrs.addrs;
4411
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004412 /* Allocate space for a local instance of packed array to hold all
4413 * the data. We store addresses here first and then put write them
4414 * to the user in one shot.
4415 */
4416 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
4417 GFP_KERNEL);
4418 if (!addrs)
4419 return -ENOMEM;
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4422 * addresses from the global local address list.
4423 */
4424 if (sctp_list_single_entry(&bp->address_list)) {
4425 addr = list_entry(bp->address_list.next,
4426 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004427 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004428 cnt = sctp_copy_laddrs_old(sk, bp->port,
4429 getaddrs.addr_num,
4430 addrs, &bytes_copied);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004431 goto copy_getaddrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 }
4433 }
4434
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004435 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004436 /* Protection on the bound address list is not needed since
4437 * in the socket option context we hold a socket lock and
4438 * thus the bound address list can't change.
4439 */
4440 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004441 memcpy(&temp, &addr->a, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4443 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004444 memcpy(buf, &temp, addrlen);
4445 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004446 bytes_copied += addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 cnt ++;
4448 if (cnt >= getaddrs.addr_num) break;
4449 }
4450
4451copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004452 /* copy the entire address list into the user provided space */
4453 if (copy_to_user(to, addrs, bytes_copied)) {
4454 err = -EFAULT;
4455 goto error;
4456 }
4457
4458 /* copy the leading structure back to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 getaddrs.addr_num = cnt;
Neil Horman408f22e2007-06-16 14:03:45 -04004460 if (copy_to_user(optval, &getaddrs, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 err = -EFAULT;
4462
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004463error:
4464 kfree(addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return err;
4466}
4467
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004468static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4469 char __user *optval, int __user *optlen)
4470{
4471 struct sctp_bind_addr *bp;
4472 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004473 int cnt = 0;
4474 struct sctp_getaddrs getaddrs;
4475 struct sctp_sockaddr_entry *addr;
4476 void __user *to;
4477 union sctp_addr temp;
4478 struct sctp_sock *sp = sctp_sk(sk);
4479 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004480 int err = 0;
4481 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004482 int bytes_copied = 0;
4483 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004484 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004485
Neil Horman408f22e2007-06-16 14:03:45 -04004486 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004487 return -EINVAL;
4488
4489 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4490 return -EFAULT;
4491
4492 /*
4493 * For UDP-style sockets, id specifies the association to query.
4494 * If the id field is set to the value '0' then the locally bound
4495 * addresses are returned without regard to any particular
4496 * association.
4497 */
4498 if (0 == getaddrs.assoc_id) {
4499 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004500 } else {
4501 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4502 if (!asoc)
4503 return -EINVAL;
4504 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004505 }
4506
4507 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004508 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4509
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004510 addrs = kmalloc(space_left, GFP_KERNEL);
4511 if (!addrs)
4512 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004513
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004514 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4515 * addresses from the global local address list.
4516 */
4517 if (sctp_list_single_entry(&bp->address_list)) {
4518 addr = list_entry(bp->address_list.next,
4519 struct sctp_sockaddr_entry, list);
Al Viro6244be42006-11-20 17:21:44 -08004520 if (sctp_is_any(&addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004521 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4522 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004523 if (cnt < 0) {
4524 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004525 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004526 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004527 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004528 }
4529 }
4530
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004531 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004532 /* Protection on the bound address list is not needed since
4533 * in the socket option context we hold a socket lock and
4534 * thus the bound address list can't change.
4535 */
4536 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004537 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004538 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4539 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004540 if (space_left < addrlen) {
4541 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004542 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004543 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004544 memcpy(buf, &temp, addrlen);
4545 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004546 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004547 cnt ++;
4548 space_left -= addrlen;
4549 }
4550
4551copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004552 if (copy_to_user(to, addrs, bytes_copied)) {
4553 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004554 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004555 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004556 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4557 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004558 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004559 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004560 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004561 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004562out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004563 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004564 return err;
4565}
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4568 *
4569 * Requests that the local SCTP stack use the enclosed peer address as
4570 * the association primary. The enclosed address must be one of the
4571 * association peer's addresses.
4572 */
4573static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4574 char __user *optval, int __user *optlen)
4575{
4576 struct sctp_prim prim;
4577 struct sctp_association *asoc;
4578 struct sctp_sock *sp = sctp_sk(sk);
4579
Neil Horman408f22e2007-06-16 14:03:45 -04004580 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return -EINVAL;
4582
Neil Horman408f22e2007-06-16 14:03:45 -04004583 len = sizeof(struct sctp_prim);
4584
4585 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 return -EFAULT;
4587
4588 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4589 if (!asoc)
4590 return -EINVAL;
4591
4592 if (!asoc->peer.primary_path)
4593 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004594
Al Viro8cec6b82006-11-20 17:23:01 -08004595 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4596 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4599 (union sctp_addr *)&prim.ssp_addr);
4600
Neil Horman408f22e2007-06-16 14:03:45 -04004601 if (put_user(len, optlen))
4602 return -EFAULT;
4603 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return -EFAULT;
4605
4606 return 0;
4607}
4608
4609/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004610 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004612 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 * Indication parameter for all future INIT and INIT-ACK exchanges.
4614 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004615static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 char __user *optval, int __user *optlen)
4617{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004618 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Neil Horman408f22e2007-06-16 14:03:45 -04004620 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return -EINVAL;
4622
Neil Horman408f22e2007-06-16 14:03:45 -04004623 len = sizeof(struct sctp_setadaptation);
4624
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004625 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004626
4627 if (put_user(len, optlen))
4628 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004629 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 return 0;
4633}
4634
4635/*
4636 *
4637 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4638 *
4639 * Applications that wish to use the sendto() system call may wish to
4640 * specify a default set of parameters that would normally be supplied
4641 * through the inclusion of ancillary data. This socket option allows
4642 * such an application to set the default sctp_sndrcvinfo structure.
4643
4644
4645 * The application that wishes to use this socket option simply passes
4646 * in to this call the sctp_sndrcvinfo structure defined in Section
4647 * 5.2.2) The input parameters accepted by this call include
4648 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4649 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4650 * to this call if the caller is using the UDP model.
4651 *
4652 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4653 */
4654static int sctp_getsockopt_default_send_param(struct sock *sk,
4655 int len, char __user *optval,
4656 int __user *optlen)
4657{
4658 struct sctp_sndrcvinfo info;
4659 struct sctp_association *asoc;
4660 struct sctp_sock *sp = sctp_sk(sk);
4661
Neil Horman408f22e2007-06-16 14:03:45 -04004662 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004664
4665 len = sizeof(struct sctp_sndrcvinfo);
4666
4667 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return -EFAULT;
4669
4670 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4671 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4672 return -EINVAL;
4673
4674 if (asoc) {
4675 info.sinfo_stream = asoc->default_stream;
4676 info.sinfo_flags = asoc->default_flags;
4677 info.sinfo_ppid = asoc->default_ppid;
4678 info.sinfo_context = asoc->default_context;
4679 info.sinfo_timetolive = asoc->default_timetolive;
4680 } else {
4681 info.sinfo_stream = sp->default_stream;
4682 info.sinfo_flags = sp->default_flags;
4683 info.sinfo_ppid = sp->default_ppid;
4684 info.sinfo_context = sp->default_context;
4685 info.sinfo_timetolive = sp->default_timetolive;
4686 }
4687
Neil Horman408f22e2007-06-16 14:03:45 -04004688 if (put_user(len, optlen))
4689 return -EFAULT;
4690 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return -EFAULT;
4692
4693 return 0;
4694}
4695
4696/*
4697 *
4698 * 7.1.5 SCTP_NODELAY
4699 *
4700 * Turn on/off any Nagle-like algorithm. This means that packets are
4701 * generally sent as soon as possible and no unnecessary delays are
4702 * introduced, at the cost of more packets in the network. Expects an
4703 * integer boolean flag.
4704 */
4705
4706static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4707 char __user *optval, int __user *optlen)
4708{
4709 int val;
4710
4711 if (len < sizeof(int))
4712 return -EINVAL;
4713
4714 len = sizeof(int);
4715 val = (sctp_sk(sk)->nodelay == 1);
4716 if (put_user(len, optlen))
4717 return -EFAULT;
4718 if (copy_to_user(optval, &val, len))
4719 return -EFAULT;
4720 return 0;
4721}
4722
4723/*
4724 *
4725 * 7.1.1 SCTP_RTOINFO
4726 *
4727 * The protocol parameters used to initialize and bound retransmission
4728 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4729 * and modify these parameters.
4730 * All parameters are time values, in milliseconds. A value of 0, when
4731 * modifying the parameters, indicates that the current value should not
4732 * be changed.
4733 *
4734 */
4735static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4736 char __user *optval,
4737 int __user *optlen) {
4738 struct sctp_rtoinfo rtoinfo;
4739 struct sctp_association *asoc;
4740
Neil Horman408f22e2007-06-16 14:03:45 -04004741 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 return -EINVAL;
4743
Neil Horman408f22e2007-06-16 14:03:45 -04004744 len = sizeof(struct sctp_rtoinfo);
4745
4746 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 return -EFAULT;
4748
4749 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4750
4751 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4752 return -EINVAL;
4753
4754 /* Values corresponding to the specific association. */
4755 if (asoc) {
4756 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4757 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4758 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4759 } else {
4760 /* Values corresponding to the endpoint. */
4761 struct sctp_sock *sp = sctp_sk(sk);
4762
4763 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4764 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4765 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4766 }
4767
4768 if (put_user(len, optlen))
4769 return -EFAULT;
4770
4771 if (copy_to_user(optval, &rtoinfo, len))
4772 return -EFAULT;
4773
4774 return 0;
4775}
4776
4777/*
4778 *
4779 * 7.1.2 SCTP_ASSOCINFO
4780 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004781 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 * of the association.
4783 * Returns an error if the new association retransmission value is
4784 * greater than the sum of the retransmission value of the peer.
4785 * See [SCTP] for more information.
4786 *
4787 */
4788static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4789 char __user *optval,
4790 int __user *optlen)
4791{
4792
4793 struct sctp_assocparams assocparams;
4794 struct sctp_association *asoc;
4795 struct list_head *pos;
4796 int cnt = 0;
4797
Neil Horman408f22e2007-06-16 14:03:45 -04004798 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return -EINVAL;
4800
Neil Horman408f22e2007-06-16 14:03:45 -04004801 len = sizeof(struct sctp_assocparams);
4802
4803 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return -EFAULT;
4805
4806 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4807
4808 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4809 return -EINVAL;
4810
4811 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07004812 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4814 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4815 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4816 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4817 * 1000) +
4818 (asoc->cookie_life.tv_usec
4819 / 1000);
4820
4821 list_for_each(pos, &asoc->peer.transport_addr_list) {
4822 cnt ++;
4823 }
4824
4825 assocparams.sasoc_number_peer_destinations = cnt;
4826 } else {
4827 /* Values corresponding to the endpoint */
4828 struct sctp_sock *sp = sctp_sk(sk);
4829
4830 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4831 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4832 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4833 assocparams.sasoc_cookie_life =
4834 sp->assocparams.sasoc_cookie_life;
4835 assocparams.sasoc_number_peer_destinations =
4836 sp->assocparams.
4837 sasoc_number_peer_destinations;
4838 }
4839
4840 if (put_user(len, optlen))
4841 return -EFAULT;
4842
4843 if (copy_to_user(optval, &assocparams, len))
4844 return -EFAULT;
4845
4846 return 0;
4847}
4848
4849/*
4850 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4851 *
4852 * This socket option is a boolean flag which turns on or off mapped V4
4853 * addresses. If this option is turned on and the socket is type
4854 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4855 * If this option is turned off, then no mapping will be done of V4
4856 * addresses and a user will receive both PF_INET6 and PF_INET type
4857 * addresses on the socket.
4858 */
4859static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4860 char __user *optval, int __user *optlen)
4861{
4862 int val;
4863 struct sctp_sock *sp = sctp_sk(sk);
4864
4865 if (len < sizeof(int))
4866 return -EINVAL;
4867
4868 len = sizeof(int);
4869 val = sp->v4mapped;
4870 if (put_user(len, optlen))
4871 return -EFAULT;
4872 if (copy_to_user(optval, &val, len))
4873 return -EFAULT;
4874
4875 return 0;
4876}
4877
4878/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004879 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
4880 * (chapter and verse is quoted at sctp_setsockopt_context())
4881 */
4882static int sctp_getsockopt_context(struct sock *sk, int len,
4883 char __user *optval, int __user *optlen)
4884{
4885 struct sctp_assoc_value params;
4886 struct sctp_sock *sp;
4887 struct sctp_association *asoc;
4888
Neil Horman408f22e2007-06-16 14:03:45 -04004889 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004890 return -EINVAL;
4891
Neil Horman408f22e2007-06-16 14:03:45 -04004892 len = sizeof(struct sctp_assoc_value);
4893
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004894 if (copy_from_user(&params, optval, len))
4895 return -EFAULT;
4896
4897 sp = sctp_sk(sk);
4898
4899 if (params.assoc_id != 0) {
4900 asoc = sctp_id2assoc(sk, params.assoc_id);
4901 if (!asoc)
4902 return -EINVAL;
4903 params.assoc_value = asoc->default_rcv_context;
4904 } else {
4905 params.assoc_value = sp->default_rcv_context;
4906 }
4907
4908 if (put_user(len, optlen))
4909 return -EFAULT;
4910 if (copy_to_user(optval, &params, len))
4911 return -EFAULT;
4912
4913 return 0;
4914}
4915
4916/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4918 *
4919 * This socket option specifies the maximum size to put in any outgoing
4920 * SCTP chunk. If a message is larger than this size it will be
4921 * fragmented by SCTP into the specified size. Note that the underlying
4922 * SCTP implementation may fragment into smaller sized chunks when the
4923 * PMTU of the underlying association is smaller than the value set by
4924 * the user.
4925 */
4926static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4927 char __user *optval, int __user *optlen)
4928{
4929 int val;
4930
4931 if (len < sizeof(int))
4932 return -EINVAL;
4933
4934 len = sizeof(int);
4935
4936 val = sctp_sk(sk)->user_frag;
4937 if (put_user(len, optlen))
4938 return -EFAULT;
4939 if (copy_to_user(optval, &val, len))
4940 return -EFAULT;
4941
4942 return 0;
4943}
4944
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004945/*
4946 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
4947 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
4948 */
4949static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
4950 char __user *optval, int __user *optlen)
4951{
4952 int val;
4953
4954 if (len < sizeof(int))
4955 return -EINVAL;
4956
4957 len = sizeof(int);
4958
4959 val = sctp_sk(sk)->frag_interleave;
4960 if (put_user(len, optlen))
4961 return -EFAULT;
4962 if (copy_to_user(optval, &val, len))
4963 return -EFAULT;
4964
4965 return 0;
4966}
4967
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004968/*
4969 * 7.1.25. Set or Get the sctp partial delivery point
4970 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
4971 */
4972static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
4973 char __user *optval,
4974 int __user *optlen)
4975{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09004976 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004977
4978 if (len < sizeof(u32))
4979 return -EINVAL;
4980
4981 len = sizeof(u32);
4982
4983 val = sctp_sk(sk)->pd_point;
4984 if (put_user(len, optlen))
4985 return -EFAULT;
4986 if (copy_to_user(optval, &val, len))
4987 return -EFAULT;
4988
4989 return -ENOTSUPP;
4990}
4991
Vlad Yasevich70331572007-03-23 11:34:36 -07004992/*
4993 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
4994 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
4995 */
4996static int sctp_getsockopt_maxburst(struct sock *sk, int len,
4997 char __user *optval,
4998 int __user *optlen)
4999{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005000 int val;
Vlad Yasevich70331572007-03-23 11:34:36 -07005001
5002 if (len < sizeof(int))
5003 return -EINVAL;
5004
5005 len = sizeof(int);
5006
5007 val = sctp_sk(sk)->max_burst;
5008 if (put_user(len, optlen))
5009 return -EFAULT;
5010 if (copy_to_user(optval, &val, len))
5011 return -EFAULT;
5012
5013 return -ENOTSUPP;
5014}
5015
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005016static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5017 char __user *optval, int __user *optlen)
5018{
5019 struct sctp_hmac_algo_param *hmacs;
5020 __u16 param_len;
5021
5022 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5023 param_len = ntohs(hmacs->param_hdr.length);
5024
5025 if (len < param_len)
5026 return -EINVAL;
5027 if (put_user(len, optlen))
5028 return -EFAULT;
5029 if (copy_to_user(optval, hmacs->hmac_ids, len))
5030 return -EFAULT;
5031
5032 return 0;
5033}
5034
5035static int sctp_getsockopt_active_key(struct sock *sk, int len,
5036 char __user *optval, int __user *optlen)
5037{
5038 struct sctp_authkeyid val;
5039 struct sctp_association *asoc;
5040
5041 if (len < sizeof(struct sctp_authkeyid))
5042 return -EINVAL;
5043 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5044 return -EFAULT;
5045
5046 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5047 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5048 return -EINVAL;
5049
5050 if (asoc)
5051 val.scact_keynumber = asoc->active_key_id;
5052 else
5053 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5054
5055 return 0;
5056}
5057
5058static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5059 char __user *optval, int __user *optlen)
5060{
5061 struct sctp_authchunks val;
5062 struct sctp_association *asoc;
5063 struct sctp_chunks_param *ch;
5064 char __user *to;
5065
5066 if (len <= sizeof(struct sctp_authchunks))
5067 return -EINVAL;
5068
5069 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5070 return -EFAULT;
5071
5072 to = val.gauth_chunks;
5073 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5074 if (!asoc)
5075 return -EINVAL;
5076
5077 ch = asoc->peer.peer_chunks;
5078
5079 /* See if the user provided enough room for all the data */
5080 if (len < ntohs(ch->param_hdr.length))
5081 return -EINVAL;
5082
5083 len = ntohs(ch->param_hdr.length);
5084 if (put_user(len, optlen))
5085 return -EFAULT;
5086 if (copy_to_user(to, ch->chunks, len))
5087 return -EFAULT;
5088
5089 return 0;
5090}
5091
5092static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5093 char __user *optval, int __user *optlen)
5094{
5095 struct sctp_authchunks val;
5096 struct sctp_association *asoc;
5097 struct sctp_chunks_param *ch;
5098 char __user *to;
5099
5100 if (len <= sizeof(struct sctp_authchunks))
5101 return -EINVAL;
5102
5103 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
5104 return -EFAULT;
5105
5106 to = val.gauth_chunks;
5107 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5108 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5109 return -EINVAL;
5110
5111 if (asoc)
5112 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5113 else
5114 ch = sctp_sk(sk)->ep->auth_chunk_list;
5115
5116 if (len < ntohs(ch->param_hdr.length))
5117 return -EINVAL;
5118
5119 len = ntohs(ch->param_hdr.length);
5120 if (put_user(len, optlen))
5121 return -EFAULT;
5122 if (copy_to_user(to, ch->chunks, len))
5123 return -EFAULT;
5124
5125 return 0;
5126}
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5129 char __user *optval, int __user *optlen)
5130{
5131 int retval = 0;
5132 int len;
5133
Frank Filz3f7a87d2005-06-20 13:14:57 -07005134 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5135 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 /* I can hardly begin to describe how wrong this is. This is
5138 * so broken as to be worse than useless. The API draft
5139 * REALLY is NOT helpful here... I am not convinced that the
5140 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5141 * are at all well-founded.
5142 */
5143 if (level != SOL_SCTP) {
5144 struct sctp_af *af = sctp_sk(sk)->pf->af;
5145
5146 retval = af->getsockopt(sk, level, optname, optval, optlen);
5147 return retval;
5148 }
5149
5150 if (get_user(len, optlen))
5151 return -EFAULT;
5152
5153 sctp_lock_sock(sk);
5154
5155 switch (optname) {
5156 case SCTP_STATUS:
5157 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5158 break;
5159 case SCTP_DISABLE_FRAGMENTS:
5160 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5161 optlen);
5162 break;
5163 case SCTP_EVENTS:
5164 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5165 break;
5166 case SCTP_AUTOCLOSE:
5167 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5168 break;
5169 case SCTP_SOCKOPT_PEELOFF:
5170 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5171 break;
5172 case SCTP_PEER_ADDR_PARAMS:
5173 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5174 optlen);
5175 break;
Frank Filz77086102005-12-22 11:37:30 -08005176 case SCTP_DELAYED_ACK_TIME:
5177 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
5178 optlen);
5179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 case SCTP_INITMSG:
5181 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5182 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005183 case SCTP_GET_PEER_ADDRS_NUM_OLD:
5184 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
5185 optlen);
5186 break;
5187 case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
5188 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
5189 optlen);
5190 break;
5191 case SCTP_GET_PEER_ADDRS_OLD:
5192 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 optlen);
5194 break;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005195 case SCTP_GET_LOCAL_ADDRS_OLD:
5196 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 optlen);
5198 break;
5199 case SCTP_GET_PEER_ADDRS:
5200 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5201 optlen);
5202 break;
5203 case SCTP_GET_LOCAL_ADDRS:
5204 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5205 optlen);
5206 break;
5207 case SCTP_DEFAULT_SEND_PARAM:
5208 retval = sctp_getsockopt_default_send_param(sk, len,
5209 optval, optlen);
5210 break;
5211 case SCTP_PRIMARY_ADDR:
5212 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5213 break;
5214 case SCTP_NODELAY:
5215 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5216 break;
5217 case SCTP_RTOINFO:
5218 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5219 break;
5220 case SCTP_ASSOCINFO:
5221 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5222 break;
5223 case SCTP_I_WANT_MAPPED_V4_ADDR:
5224 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5225 break;
5226 case SCTP_MAXSEG:
5227 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5228 break;
5229 case SCTP_GET_PEER_ADDR_INFO:
5230 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5231 optlen);
5232 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005233 case SCTP_ADAPTATION_LAYER:
5234 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 optlen);
5236 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005237 case SCTP_CONTEXT:
5238 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5239 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005240 case SCTP_FRAGMENT_INTERLEAVE:
5241 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5242 optlen);
5243 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005244 case SCTP_PARTIAL_DELIVERY_POINT:
5245 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5246 optlen);
5247 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005248 case SCTP_MAX_BURST:
5249 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5250 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005251 case SCTP_AUTH_KEY:
5252 case SCTP_AUTH_CHUNK:
5253 case SCTP_AUTH_DELETE_KEY:
5254 retval = -EOPNOTSUPP;
5255 break;
5256 case SCTP_HMAC_IDENT:
5257 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5258 break;
5259 case SCTP_AUTH_ACTIVE_KEY:
5260 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5261 break;
5262 case SCTP_PEER_AUTH_CHUNKS:
5263 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5264 optlen);
5265 break;
5266 case SCTP_LOCAL_AUTH_CHUNKS:
5267 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5268 optlen);
5269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 default:
5271 retval = -ENOPROTOOPT;
5272 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 sctp_release_sock(sk);
5276 return retval;
5277}
5278
5279static void sctp_hash(struct sock *sk)
5280{
5281 /* STUB */
5282}
5283
5284static void sctp_unhash(struct sock *sk)
5285{
5286 /* STUB */
5287}
5288
5289/* Check if port is acceptable. Possibly find first available port.
5290 *
5291 * The port hash table (contained in the 'global' SCTP protocol storage
5292 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5293 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5294 * list (the list number is the port number hashed out, so as you
5295 * would expect from a hash function, all the ports in a given list have
5296 * such a number that hashes out to the same list number; you were
5297 * expecting that, right?); so each list has a set of ports, with a
5298 * link to the socket (struct sock) that uses it, the port number and
5299 * a fastreuse flag (FIXME: NPI ipg).
5300 */
5301static struct sctp_bind_bucket *sctp_bucket_create(
5302 struct sctp_bind_hashbucket *head, unsigned short snum);
5303
5304static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5305{
5306 struct sctp_bind_hashbucket *head; /* hash list */
5307 struct sctp_bind_bucket *pp; /* hash list port iterator */
5308 unsigned short snum;
5309 int ret;
5310
Al Viro04afd8b2006-11-20 17:02:01 -08005311 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5314 sctp_local_bh_disable();
5315
5316 if (snum == 0) {
5317 /* Search for an available port.
5318 *
5319 * 'sctp_port_rover' was the last port assigned, so
5320 * we start to search from 'sctp_port_rover +
5321 * 1'. What we do is first check if port 'rover' is
5322 * already in the hash table; if not, we use that; if
5323 * it is, we try next.
5324 */
5325 int low = sysctl_local_port_range[0];
5326 int high = sysctl_local_port_range[1];
5327 int remaining = (high - low) + 1;
5328 int rover;
5329 int index;
5330
5331 sctp_spin_lock(&sctp_port_alloc_lock);
5332 rover = sctp_port_rover;
5333 do {
5334 rover++;
5335 if ((rover < low) || (rover > high))
5336 rover = low;
5337 index = sctp_phashfn(rover);
5338 head = &sctp_port_hashtable[index];
5339 sctp_spin_lock(&head->lock);
5340 for (pp = head->chain; pp; pp = pp->next)
5341 if (pp->port == rover)
5342 goto next;
5343 break;
5344 next:
5345 sctp_spin_unlock(&head->lock);
5346 } while (--remaining > 0);
5347 sctp_port_rover = rover;
5348 sctp_spin_unlock(&sctp_port_alloc_lock);
5349
5350 /* Exhausted local port range during search? */
5351 ret = 1;
5352 if (remaining <= 0)
5353 goto fail;
5354
5355 /* OK, here is the one we will use. HEAD (the port
5356 * hash table list entry) is non-NULL and we hold it's
5357 * mutex.
5358 */
5359 snum = rover;
5360 } else {
5361 /* We are given an specific port number; we verify
5362 * that it is not being used. If it is used, we will
5363 * exahust the search in the hash list corresponding
5364 * to the port number (snum) - we detect that with the
5365 * port iterator, pp being NULL.
5366 */
5367 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5368 sctp_spin_lock(&head->lock);
5369 for (pp = head->chain; pp; pp = pp->next) {
5370 if (pp->port == snum)
5371 goto pp_found;
5372 }
5373 }
5374 pp = NULL;
5375 goto pp_not_found;
5376pp_found:
5377 if (!hlist_empty(&pp->owner)) {
5378 /* We had a port hash table hit - there is an
5379 * available port (pp != NULL) and it is being
5380 * used by other socket (pp->owner not empty); that other
5381 * socket is going to be sk2.
5382 */
5383 int reuse = sk->sk_reuse;
5384 struct sock *sk2;
5385 struct hlist_node *node;
5386
5387 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005388 if (pp->fastreuse && sk->sk_reuse &&
5389 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 goto success;
5391
5392 /* Run through the list of sockets bound to the port
5393 * (pp->port) [via the pointers bind_next and
5394 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5395 * we get the endpoint they describe and run through
5396 * the endpoint's list of IP (v4 or v6) addresses,
5397 * comparing each of the addresses with the address of
5398 * the socket sk. If we find a match, then that means
5399 * that this port/socket (sk) combination are already
5400 * in an endpoint.
5401 */
5402 sk_for_each_bound(sk2, node, &pp->owner) {
5403 struct sctp_endpoint *ep2;
5404 ep2 = sctp_sk(sk2)->ep;
5405
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005406 if (reuse && sk2->sk_reuse &&
5407 sk2->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 continue;
5409
Al Viro7e1e4a22006-11-20 17:05:43 -08005410 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 sctp_sk(sk))) {
5412 ret = (long)sk2;
5413 goto fail_unlock;
5414 }
5415 }
5416 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5417 }
5418pp_not_found:
5419 /* If there was a hash table miss, create a new port. */
5420 ret = 1;
5421 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5422 goto fail_unlock;
5423
5424 /* In either case (hit or miss), make sure fastreuse is 1 only
5425 * if sk->sk_reuse is too (that is, if the caller requested
5426 * SO_REUSEADDR on this socket -sk-).
5427 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005428 if (hlist_empty(&pp->owner)) {
5429 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5430 pp->fastreuse = 1;
5431 else
5432 pp->fastreuse = 0;
5433 } else if (pp->fastreuse &&
5434 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 pp->fastreuse = 0;
5436
5437 /* We are set, so fill up all the data in the hash table
5438 * entry, tie the socket list information with the rest of the
5439 * sockets FIXME: Blurry, NPI (ipg).
5440 */
5441success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 if (!sctp_sk(sk)->bind_hash) {
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005443 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 sk_add_bind_node(sk, &pp->owner);
5445 sctp_sk(sk)->bind_hash = pp;
5446 }
5447 ret = 0;
5448
5449fail_unlock:
5450 sctp_spin_unlock(&head->lock);
5451
5452fail:
5453 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 return ret;
5455}
5456
5457/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5458 * port is requested.
5459 */
5460static int sctp_get_port(struct sock *sk, unsigned short snum)
5461{
5462 long ret;
5463 union sctp_addr addr;
5464 struct sctp_af *af = sctp_sk(sk)->pf->af;
5465
5466 /* Set up a dummy address struct from the sk. */
5467 af->from_sk(&addr, sk);
5468 addr.v4.sin_port = htons(snum);
5469
5470 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5471 ret = sctp_get_port_local(sk, &addr);
5472
5473 return (ret ? 1 : 0);
5474}
5475
5476/*
5477 * 3.1.3 listen() - UDP Style Syntax
5478 *
5479 * By default, new associations are not accepted for UDP style sockets.
5480 * An application uses listen() to mark a socket as being able to
5481 * accept new associations.
5482 */
5483SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
5484{
5485 struct sctp_sock *sp = sctp_sk(sk);
5486 struct sctp_endpoint *ep = sp->ep;
5487
5488 /* Only UDP style sockets that are not peeled off are allowed to
5489 * listen().
5490 */
5491 if (!sctp_style(sk, UDP))
5492 return -EINVAL;
5493
5494 /* If backlog is zero, disable listening. */
5495 if (!backlog) {
5496 if (sctp_sstate(sk, CLOSED))
5497 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 sctp_unhash_endpoint(ep);
5500 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503
5504 /* Return if we are already listening. */
5505 if (sctp_sstate(sk, LISTENING))
5506 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 /*
5509 * If a bind() or sctp_bindx() is not called prior to a listen()
5510 * call that allows new associations to be accepted, the system
5511 * picks an ephemeral port and will choose an address set equivalent
5512 * to binding with a wildcard address.
5513 *
5514 * This is not currently spelled out in the SCTP sockets
5515 * extensions draft, but follows the practice as seen in TCP
5516 * sockets.
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005517 *
5518 * Additionally, turn off fastreuse flag since we are not listening
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005520 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (!ep->base.bind_addr.port) {
5522 if (sctp_autobind(sk))
5523 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005524 } else
5525 sctp_sk(sk)->bind_hash->fastreuse = 0;
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 sctp_hash_endpoint(ep);
5528 return 0;
5529}
5530
5531/*
5532 * 4.1.3 listen() - TCP Style Syntax
5533 *
5534 * Applications uses listen() to ready the SCTP endpoint for accepting
5535 * inbound associations.
5536 */
5537SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
5538{
5539 struct sctp_sock *sp = sctp_sk(sk);
5540 struct sctp_endpoint *ep = sp->ep;
5541
5542 /* If backlog is zero, disable listening. */
5543 if (!backlog) {
5544 if (sctp_sstate(sk, CLOSED))
5545 return 0;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 sctp_unhash_endpoint(ep);
5548 sk->sk_state = SCTP_SS_CLOSED;
Vlad Yasevich498d6302007-08-30 14:03:58 -04005549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551
5552 if (sctp_sstate(sk, LISTENING))
5553 return 0;
5554
5555 /*
5556 * If a bind() or sctp_bindx() is not called prior to a listen()
5557 * call that allows new associations to be accepted, the system
5558 * picks an ephemeral port and will choose an address set equivalent
5559 * to binding with a wildcard address.
5560 *
5561 * This is not currently spelled out in the SCTP sockets
5562 * extensions draft, but follows the practice as seen in TCP
5563 * sockets.
5564 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005565 sk->sk_state = SCTP_SS_LISTENING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (!ep->base.bind_addr.port) {
5567 if (sctp_autobind(sk))
5568 return -EAGAIN;
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005569 } else
5570 sctp_sk(sk)->bind_hash->fastreuse = 0;
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 sk->sk_max_ack_backlog = backlog;
5573 sctp_hash_endpoint(ep);
5574 return 0;
5575}
5576
5577/*
5578 * Move a socket to LISTENING state.
5579 */
5580int sctp_inet_listen(struct socket *sock, int backlog)
5581{
5582 struct sock *sk = sock->sk;
Herbert Xu1b489e12006-08-20 15:07:14 +10005583 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 int err = -EINVAL;
5585
5586 if (unlikely(backlog < 0))
5587 goto out;
5588
5589 sctp_lock_sock(sk);
5590
5591 if (sock->state != SS_UNCONNECTED)
5592 goto out;
5593
5594 /* Allocate HMAC for generating cookie. */
5595 if (sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005596 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005597 if (IS_ERR(tfm)) {
5598 if (net_ratelimit()) {
5599 printk(KERN_INFO
5600 "SCTP: failed to load transform for %s: %ld\n",
5601 sctp_hmac_alg, PTR_ERR(tfm));
5602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 err = -ENOSYS;
5604 goto out;
5605 }
5606 }
5607
5608 switch (sock->type) {
5609 case SOCK_SEQPACKET:
5610 err = sctp_seqpacket_listen(sk, backlog);
5611 break;
5612 case SOCK_STREAM:
5613 err = sctp_stream_listen(sk, backlog);
5614 break;
5615 default:
5616 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005617 }
5618
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (err)
5620 goto cleanup;
5621
5622 /* Store away the transform reference. */
5623 sctp_sk(sk)->hmac = tfm;
5624out:
5625 sctp_release_sock(sk);
5626 return err;
5627cleanup:
Herbert Xu1b489e12006-08-20 15:07:14 +10005628 crypto_free_hash(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 goto out;
5630}
5631
5632/*
5633 * This function is done by modeling the current datagram_poll() and the
5634 * tcp_poll(). Note that, based on these implementations, we don't
5635 * lock the socket in this function, even though it seems that,
5636 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005637 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 * in this place. We assume that we don't need locks either until proven
5639 * otherwise.
5640 *
5641 * Another thing to note is that we include the Async I/O support
5642 * here, again, by modeling the current TCP/UDP code. We don't have
5643 * a good way to test with it yet.
5644 */
5645unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
5646{
5647 struct sock *sk = sock->sk;
5648 struct sctp_sock *sp = sctp_sk(sk);
5649 unsigned int mask;
5650
5651 poll_wait(file, sk->sk_sleep, wait);
5652
5653 /* A TCP-style listening socket becomes readable when the accept queue
5654 * is not empty.
5655 */
5656 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
5657 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005658 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 mask = 0;
5661
5662 /* Is there any exceptional events? */
5663 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
5664 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08005665 if (sk->sk_shutdown & RCV_SHUTDOWN)
5666 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (sk->sk_shutdown == SHUTDOWN_MASK)
5668 mask |= POLLHUP;
5669
5670 /* Is it readable? Reconsider this code with TCP-style support. */
5671 if (!skb_queue_empty(&sk->sk_receive_queue) ||
5672 (sk->sk_shutdown & RCV_SHUTDOWN))
5673 mask |= POLLIN | POLLRDNORM;
5674
5675 /* The association is either gone or not ready. */
5676 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
5677 return mask;
5678
5679 /* Is it writable? */
5680 if (sctp_writeable(sk)) {
5681 mask |= POLLOUT | POLLWRNORM;
5682 } else {
5683 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
5684 /*
5685 * Since the socket is not locked, the buffer
5686 * might be made available after the writeable check and
5687 * before the bit is set. This could cause a lost I/O
5688 * signal. tcp_poll() has a race breaker for this race
5689 * condition. Based on their implementation, we put
5690 * in the following code to cover it as well.
5691 */
5692 if (sctp_writeable(sk))
5693 mask |= POLLOUT | POLLWRNORM;
5694 }
5695 return mask;
5696}
5697
5698/********************************************************************
5699 * 2nd Level Abstractions
5700 ********************************************************************/
5701
5702static struct sctp_bind_bucket *sctp_bucket_create(
5703 struct sctp_bind_hashbucket *head, unsigned short snum)
5704{
5705 struct sctp_bind_bucket *pp;
5706
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08005707 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 SCTP_DBG_OBJCNT_INC(bind_bucket);
5709 if (pp) {
5710 pp->port = snum;
5711 pp->fastreuse = 0;
5712 INIT_HLIST_HEAD(&pp->owner);
5713 if ((pp->next = head->chain) != NULL)
5714 pp->next->pprev = &pp->next;
5715 head->chain = pp;
5716 pp->pprev = &head->chain;
5717 }
5718 return pp;
5719}
5720
5721/* Caller must hold hashbucket lock for this tb with local BH disabled */
5722static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
5723{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07005724 if (pp && hlist_empty(&pp->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 if (pp->next)
5726 pp->next->pprev = pp->pprev;
5727 *(pp->pprev) = pp->next;
5728 kmem_cache_free(sctp_bucket_cachep, pp);
5729 SCTP_DBG_OBJCNT_DEC(bind_bucket);
5730 }
5731}
5732
5733/* Release this socket's reference to a local port. */
5734static inline void __sctp_put_port(struct sock *sk)
5735{
5736 struct sctp_bind_hashbucket *head =
5737 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5738 struct sctp_bind_bucket *pp;
5739
5740 sctp_spin_lock(&head->lock);
5741 pp = sctp_sk(sk)->bind_hash;
5742 __sk_del_bind_node(sk);
5743 sctp_sk(sk)->bind_hash = NULL;
5744 inet_sk(sk)->num = 0;
5745 sctp_bucket_destroy(pp);
5746 sctp_spin_unlock(&head->lock);
5747}
5748
5749void sctp_put_port(struct sock *sk)
5750{
5751 sctp_local_bh_disable();
5752 __sctp_put_port(sk);
5753 sctp_local_bh_enable();
5754}
5755
5756/*
5757 * The system picks an ephemeral port and choose an address set equivalent
5758 * to binding with a wildcard address.
5759 * One of those addresses will be the primary address for the association.
5760 * This automatically enables the multihoming capability of SCTP.
5761 */
5762static int sctp_autobind(struct sock *sk)
5763{
5764 union sctp_addr autoaddr;
5765 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08005766 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
5768 /* Initialize a local sockaddr structure to INADDR_ANY. */
5769 af = sctp_sk(sk)->pf->af;
5770
5771 port = htons(inet_sk(sk)->num);
5772 af->inaddr_any(&autoaddr, port);
5773
5774 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5775}
5776
5777/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
5778 *
5779 * From RFC 2292
5780 * 4.2 The cmsghdr Structure *
5781 *
5782 * When ancillary data is sent or received, any number of ancillary data
5783 * objects can be specified by the msg_control and msg_controllen members of
5784 * the msghdr structure, because each object is preceded by
5785 * a cmsghdr structure defining the object's length (the cmsg_len member).
5786 * Historically Berkeley-derived implementations have passed only one object
5787 * at a time, but this API allows multiple objects to be
5788 * passed in a single call to sendmsg() or recvmsg(). The following example
5789 * shows two ancillary data objects in a control buffer.
5790 *
5791 * |<--------------------------- msg_controllen -------------------------->|
5792 * | |
5793 *
5794 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
5795 *
5796 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5797 * | | |
5798 *
5799 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
5800 *
5801 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
5802 * | | | | |
5803 *
5804 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5805 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
5806 *
5807 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
5808 *
5809 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5810 * ^
5811 * |
5812 *
5813 * msg_control
5814 * points here
5815 */
5816SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5817 sctp_cmsgs_t *cmsgs)
5818{
5819 struct cmsghdr *cmsg;
5820
5821 for (cmsg = CMSG_FIRSTHDR(msg);
5822 cmsg != NULL;
5823 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5824 if (!CMSG_OK(msg, cmsg))
5825 return -EINVAL;
5826
5827 /* Should we parse this header or ignore? */
5828 if (cmsg->cmsg_level != IPPROTO_SCTP)
5829 continue;
5830
5831 /* Strictly check lengths following example in SCM code. */
5832 switch (cmsg->cmsg_type) {
5833 case SCTP_INIT:
5834 /* SCTP Socket API Extension
5835 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5836 *
5837 * This cmsghdr structure provides information for
5838 * initializing new SCTP associations with sendmsg().
5839 * The SCTP_INITMSG socket option uses this same data
5840 * structure. This structure is not used for
5841 * recvmsg().
5842 *
5843 * cmsg_level cmsg_type cmsg_data[]
5844 * ------------ ------------ ----------------------
5845 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
5846 */
5847 if (cmsg->cmsg_len !=
5848 CMSG_LEN(sizeof(struct sctp_initmsg)))
5849 return -EINVAL;
5850 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5851 break;
5852
5853 case SCTP_SNDRCV:
5854 /* SCTP Socket API Extension
5855 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5856 *
5857 * This cmsghdr structure specifies SCTP options for
5858 * sendmsg() and describes SCTP header information
5859 * about a received message through recvmsg().
5860 *
5861 * cmsg_level cmsg_type cmsg_data[]
5862 * ------------ ------------ ----------------------
5863 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
5864 */
5865 if (cmsg->cmsg_len !=
5866 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5867 return -EINVAL;
5868
5869 cmsgs->info =
5870 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5871
5872 /* Minimally, validate the sinfo_flags. */
5873 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07005874 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5875 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return -EINVAL;
5877 break;
5878
5879 default:
5880 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
5883 return 0;
5884}
5885
5886/*
5887 * Wait for a packet..
5888 * Note: This function is the same function as in core/datagram.c
5889 * with a few modifications to make lksctp work.
5890 */
5891static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5892{
5893 int error;
5894 DEFINE_WAIT(wait);
5895
5896 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5897
5898 /* Socket errors? */
5899 error = sock_error(sk);
5900 if (error)
5901 goto out;
5902
5903 if (!skb_queue_empty(&sk->sk_receive_queue))
5904 goto ready;
5905
5906 /* Socket shut down? */
5907 if (sk->sk_shutdown & RCV_SHUTDOWN)
5908 goto out;
5909
5910 /* Sequenced packets can come disconnected. If so we report the
5911 * problem.
5912 */
5913 error = -ENOTCONN;
5914
5915 /* Is there a good reason to think that we may receive some data? */
5916 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5917 goto out;
5918
5919 /* Handle signals. */
5920 if (signal_pending(current))
5921 goto interrupted;
5922
5923 /* Let another process have a go. Since we are going to sleep
5924 * anyway. Note: This may cause odd behaviors if the message
5925 * does not fit in the user's buffer, but this seems to be the
5926 * only way to honor MSG_DONTWAIT realistically.
5927 */
5928 sctp_release_sock(sk);
5929 *timeo_p = schedule_timeout(*timeo_p);
5930 sctp_lock_sock(sk);
5931
5932ready:
5933 finish_wait(sk->sk_sleep, &wait);
5934 return 0;
5935
5936interrupted:
5937 error = sock_intr_errno(*timeo_p);
5938
5939out:
5940 finish_wait(sk->sk_sleep, &wait);
5941 *err = error;
5942 return error;
5943}
5944
5945/* Receive a datagram.
5946 * Note: This is pretty much the same routine as in core/datagram.c
5947 * with a few changes to make lksctp work.
5948 */
5949static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5950 int noblock, int *err)
5951{
5952 int error;
5953 struct sk_buff *skb;
5954 long timeo;
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 timeo = sock_rcvtimeo(sk, noblock);
5957
5958 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5959 timeo, MAX_SCHEDULE_TIMEOUT);
5960
5961 do {
5962 /* Again only user level code calls this function,
5963 * so nothing interrupt level
5964 * will suddenly eat the receive_queue.
5965 *
5966 * Look at current nfs client by the way...
5967 * However, this function was corrent in any case. 8)
5968 */
5969 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07005970 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 skb = skb_peek(&sk->sk_receive_queue);
5972 if (skb)
5973 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07005974 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 } else {
5976 skb = skb_dequeue(&sk->sk_receive_queue);
5977 }
5978
5979 if (skb)
5980 return skb;
5981
Neil Horman6736dc32005-12-02 20:30:06 -08005982 /* Caller is allowed not to check sk->sk_err before calling. */
5983 error = sock_error(sk);
5984 if (error)
5985 goto no_packet;
5986
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 if (sk->sk_shutdown & RCV_SHUTDOWN)
5988 break;
5989
5990 /* User doesn't want to wait. */
5991 error = -EAGAIN;
5992 if (!timeo)
5993 goto no_packet;
5994 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5995
5996 return NULL;
5997
5998no_packet:
5999 *err = error;
6000 return NULL;
6001}
6002
6003/* If sndbuf has changed, wake up per association sndbuf waiters. */
6004static void __sctp_write_space(struct sctp_association *asoc)
6005{
6006 struct sock *sk = asoc->base.sk;
6007 struct socket *sock = sk->sk_socket;
6008
6009 if ((sctp_wspace(asoc) > 0) && sock) {
6010 if (waitqueue_active(&asoc->wait))
6011 wake_up_interruptible(&asoc->wait);
6012
6013 if (sctp_writeable(sk)) {
6014 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
6015 wake_up_interruptible(sk->sk_sleep);
6016
6017 /* Note that we try to include the Async I/O support
6018 * here by modeling from the current TCP/UDP code.
6019 * We have not tested with it yet.
6020 */
6021 if (sock->fasync_list &&
6022 !(sk->sk_shutdown & SEND_SHUTDOWN))
6023 sock_wake_async(sock, 2, POLL_OUT);
6024 }
6025 }
6026}
6027
6028/* Do accounting for the sndbuf space.
6029 * Decrement the used sndbuf space of the corresponding association by the
6030 * data size which was just transmitted(freed).
6031 */
6032static void sctp_wfree(struct sk_buff *skb)
6033{
6034 struct sctp_association *asoc;
6035 struct sctp_chunk *chunk;
6036 struct sock *sk;
6037
6038 /* Get the saved chunk pointer. */
6039 chunk = *((struct sctp_chunk **)(skb->cb));
6040 asoc = chunk->asoc;
6041 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006042 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6043 sizeof(struct sk_buff) +
6044 sizeof(struct sctp_chunk);
6045
Neil Horman4eb701d2005-04-28 12:02:04 -07006046 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6047
Neil Horman4d93df02007-08-15 16:07:44 -07006048 /*
6049 * This undoes what is done via sk_charge_skb
6050 */
6051 sk->sk_wmem_queued -= skb->truesize;
6052 sk->sk_forward_alloc += skb->truesize;
6053
Neil Horman4eb701d2005-04-28 12:02:04 -07006054 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 __sctp_write_space(asoc);
6056
6057 sctp_association_put(asoc);
6058}
6059
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006060/* Do accounting for the receive space on the socket.
6061 * Accounting for the association is done in ulpevent.c
6062 * We set this as a destructor for the cloned data skbs so that
6063 * accounting is done at the correct time.
6064 */
6065void sctp_sock_rfree(struct sk_buff *skb)
6066{
6067 struct sock *sk = skb->sk;
6068 struct sctp_ulpevent *event = sctp_skb2event(skb);
6069
6070 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006071
6072 /*
6073 * Mimic the behavior of sk_stream_rfree
6074 */
6075 sk->sk_forward_alloc += event->rmem_len;
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006076}
6077
6078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079/* Helper function to wait for space in the sndbuf. */
6080static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6081 size_t msg_len)
6082{
6083 struct sock *sk = asoc->base.sk;
6084 int err = 0;
6085 long current_timeo = *timeo_p;
6086 DEFINE_WAIT(wait);
6087
6088 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006089 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
6091 /* Increment the association's refcnt. */
6092 sctp_association_hold(asoc);
6093
6094 /* Wait on the association specific sndbuf space. */
6095 for (;;) {
6096 prepare_to_wait_exclusive(&asoc->wait, &wait,
6097 TASK_INTERRUPTIBLE);
6098 if (!*timeo_p)
6099 goto do_nonblock;
6100 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6101 asoc->base.dead)
6102 goto do_error;
6103 if (signal_pending(current))
6104 goto do_interrupted;
6105 if (msg_len <= sctp_wspace(asoc))
6106 break;
6107
6108 /* Let another process have a go. Since we are going
6109 * to sleep anyway.
6110 */
6111 sctp_release_sock(sk);
6112 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006113 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 sctp_lock_sock(sk);
6115
6116 *timeo_p = current_timeo;
6117 }
6118
6119out:
6120 finish_wait(&asoc->wait, &wait);
6121
6122 /* Release the association's refcnt. */
6123 sctp_association_put(asoc);
6124
6125 return err;
6126
6127do_error:
6128 err = -EPIPE;
6129 goto out;
6130
6131do_interrupted:
6132 err = sock_intr_errno(*timeo_p);
6133 goto out;
6134
6135do_nonblock:
6136 err = -EAGAIN;
6137 goto out;
6138}
6139
6140/* If socket sndbuf has changed, wake up all per association waiters. */
6141void sctp_write_space(struct sock *sk)
6142{
6143 struct sctp_association *asoc;
6144 struct list_head *pos;
6145
6146 /* Wake up the tasks in each wait queue. */
6147 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
6148 asoc = list_entry(pos, struct sctp_association, asocs);
6149 __sctp_write_space(asoc);
6150 }
6151}
6152
6153/* Is there any sndbuf space available on the socket?
6154 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006155 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 * associations on the same socket. For a UDP-style socket with
6157 * multiple associations, it is possible for it to be "unwriteable"
6158 * prematurely. I assume that this is acceptable because
6159 * a premature "unwriteable" is better than an accidental "writeable" which
6160 * would cause an unwanted block under certain circumstances. For the 1-1
6161 * UDP-style sockets or TCP-style sockets, this code should work.
6162 * - Daisy
6163 */
6164static int sctp_writeable(struct sock *sk)
6165{
6166 int amt = 0;
6167
Neil Horman9bffc4a2005-12-19 14:24:40 -08006168 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 if (amt < 0)
6170 amt = 0;
6171 return amt;
6172}
6173
6174/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6175 * returns immediately with EINPROGRESS.
6176 */
6177static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6178{
6179 struct sock *sk = asoc->base.sk;
6180 int err = 0;
6181 long current_timeo = *timeo_p;
6182 DEFINE_WAIT(wait);
6183
6184 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
6185 (long)(*timeo_p));
6186
6187 /* Increment the association's refcnt. */
6188 sctp_association_hold(asoc);
6189
6190 for (;;) {
6191 prepare_to_wait_exclusive(&asoc->wait, &wait,
6192 TASK_INTERRUPTIBLE);
6193 if (!*timeo_p)
6194 goto do_nonblock;
6195 if (sk->sk_shutdown & RCV_SHUTDOWN)
6196 break;
6197 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6198 asoc->base.dead)
6199 goto do_error;
6200 if (signal_pending(current))
6201 goto do_interrupted;
6202
6203 if (sctp_state(asoc, ESTABLISHED))
6204 break;
6205
6206 /* Let another process have a go. Since we are going
6207 * to sleep anyway.
6208 */
6209 sctp_release_sock(sk);
6210 current_timeo = schedule_timeout(current_timeo);
6211 sctp_lock_sock(sk);
6212
6213 *timeo_p = current_timeo;
6214 }
6215
6216out:
6217 finish_wait(&asoc->wait, &wait);
6218
6219 /* Release the association's refcnt. */
6220 sctp_association_put(asoc);
6221
6222 return err;
6223
6224do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006225 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 err = -ETIMEDOUT;
6227 else
6228 err = -ECONNREFUSED;
6229 goto out;
6230
6231do_interrupted:
6232 err = sock_intr_errno(*timeo_p);
6233 goto out;
6234
6235do_nonblock:
6236 err = -EINPROGRESS;
6237 goto out;
6238}
6239
6240static int sctp_wait_for_accept(struct sock *sk, long timeo)
6241{
6242 struct sctp_endpoint *ep;
6243 int err = 0;
6244 DEFINE_WAIT(wait);
6245
6246 ep = sctp_sk(sk)->ep;
6247
6248
6249 for (;;) {
6250 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
6251 TASK_INTERRUPTIBLE);
6252
6253 if (list_empty(&ep->asocs)) {
6254 sctp_release_sock(sk);
6255 timeo = schedule_timeout(timeo);
6256 sctp_lock_sock(sk);
6257 }
6258
6259 err = -EINVAL;
6260 if (!sctp_sstate(sk, LISTENING))
6261 break;
6262
6263 err = 0;
6264 if (!list_empty(&ep->asocs))
6265 break;
6266
6267 err = sock_intr_errno(timeo);
6268 if (signal_pending(current))
6269 break;
6270
6271 err = -EAGAIN;
6272 if (!timeo)
6273 break;
6274 }
6275
6276 finish_wait(sk->sk_sleep, &wait);
6277
6278 return err;
6279}
6280
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006281static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282{
6283 DEFINE_WAIT(wait);
6284
6285 do {
6286 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
6287 if (list_empty(&sctp_sk(sk)->ep->asocs))
6288 break;
6289 sctp_release_sock(sk);
6290 timeout = schedule_timeout(timeout);
6291 sctp_lock_sock(sk);
6292 } while (!signal_pending(current) && timeout);
6293
6294 finish_wait(sk->sk_sleep, &wait);
6295}
6296
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006297static void sctp_sock_rfree_frag(struct sk_buff *skb)
6298{
6299 struct sk_buff *frag;
6300
6301 if (!skb->data_len)
6302 goto done;
6303
6304 /* Don't forget the fragments. */
6305 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6306 sctp_sock_rfree_frag(frag);
6307
6308done:
6309 sctp_sock_rfree(skb);
6310}
6311
6312static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6313{
6314 struct sk_buff *frag;
6315
6316 if (!skb->data_len)
6317 goto done;
6318
6319 /* Don't forget the fragments. */
6320 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
6321 sctp_skb_set_owner_r_frag(frag, sk);
6322
6323done:
6324 sctp_skb_set_owner_r(skb, sk);
6325}
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327/* Populate the fields of the newsk from the oldsk and migrate the assoc
6328 * and its messages to the newsk.
6329 */
6330static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6331 struct sctp_association *assoc,
6332 sctp_socket_type_t type)
6333{
6334 struct sctp_sock *oldsp = sctp_sk(oldsk);
6335 struct sctp_sock *newsp = sctp_sk(newsk);
6336 struct sctp_bind_bucket *pp; /* hash list port iterator */
6337 struct sctp_endpoint *newep = newsp->ep;
6338 struct sk_buff *skb, *tmp;
6339 struct sctp_ulpevent *event;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006340 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342 /* Migrate socket buffer sizes and all the socket level options to the
6343 * new socket.
6344 */
6345 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6346 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6347 /* Brute force copy old sctp opt. */
6348 inet_sk_copy_descendant(newsk, oldsk);
6349
6350 /* Restore the ep value that was overwritten with the above structure
6351 * copy.
6352 */
6353 newsp->ep = newep;
6354 newsp->hmac = NULL;
6355
6356 /* Hook this new socket in to the bind_hash list. */
6357 pp = sctp_sk(oldsk)->bind_hash;
6358 sk_add_bind_node(newsk, &pp->owner);
6359 sctp_sk(newsk)->bind_hash = pp;
6360 inet_sk(newsk)->num = inet_sk(oldsk)->num;
6361
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006362 /* Copy the bind_addr list from the original endpoint to the new
6363 * endpoint so that we can handle restarts properly
6364 */
Vladislav Yasevicheb5fa392006-08-22 00:23:13 -07006365 if (PF_INET6 == assoc->base.sk->sk_family)
6366 flags = SCTP_ADDR6_ALLOWED;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006367 if (assoc->peer.ipv4_address)
6368 flags |= SCTP_ADDR4_PEERSUPP;
6369 if (assoc->peer.ipv6_address)
6370 flags |= SCTP_ADDR6_PEERSUPP;
6371 sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
6372 &oldsp->ep->base.bind_addr,
6373 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 /* Move any messages in the old socket's receive queue that are for the
6376 * peeled off association to the new socket's receive queue.
6377 */
6378 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6379 event = sctp_skb2event(skb);
6380 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006381 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006382 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006384 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 }
6386 }
6387
6388 /* Clean up any messages pending delivery due to partial
6389 * delivery. Three cases:
6390 * 1) No partial deliver; no work.
6391 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6392 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6393 */
6394 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006395 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006397 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 struct sk_buff_head *queue;
6399
6400 /* Decide which queue to move pd_lobby skbs to. */
6401 if (assoc->ulpq.pd_mode) {
6402 queue = &newsp->pd_lobby;
6403 } else
6404 queue = &newsk->sk_receive_queue;
6405
6406 /* Walk through the pd_lobby, looking for skbs that
6407 * need moved to the new socket.
6408 */
6409 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6410 event = sctp_skb2event(skb);
6411 if (event->asoc == assoc) {
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006412 sctp_sock_rfree_frag(skb);
David S. Miller8728b832005-08-09 19:25:21 -07006413 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006415 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 }
6417 }
6418
6419 /* Clear up any skbs waiting for the partial
6420 * delivery to finish.
6421 */
6422 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006423 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
6425 }
6426
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006427 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
6428 sctp_sock_rfree_frag(skb);
6429 sctp_skb_set_owner_r_frag(skb, newsk);
6430 }
6431
6432 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
6433 sctp_sock_rfree_frag(skb);
6434 sctp_skb_set_owner_r_frag(skb, newsk);
6435 }
6436
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 /* Set the type of socket to indicate that it is peeled off from the
6438 * original UDP-style socket or created with the accept() call on a
6439 * TCP-style socket..
6440 */
6441 newsp->type = type;
6442
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006443 /* Mark the new socket "in-use" by the user so that any packets
6444 * that may arrive on the association after we've moved it are
6445 * queued to the backlog. This prevents a potential race between
6446 * backlog processing on the old socket and new-packet processing
6447 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006448 *
6449 * The caller has just allocated newsk so we can guarantee that other
6450 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006451 */
Zach Brown5131a182007-06-22 15:14:46 -07006452 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 /* If the association on the newsk is already closed before accept()
6456 * is called, set RCV_SHUTDOWN flag.
6457 */
6458 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6459 newsk->sk_shutdown |= RCV_SHUTDOWN;
6460
6461 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006462 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463}
6464
Neil Horman4d93df02007-08-15 16:07:44 -07006465
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466/* This proto struct describes the ULP interface for SCTP. */
6467struct proto sctp_prot = {
6468 .name = "SCTP",
6469 .owner = THIS_MODULE,
6470 .close = sctp_close,
6471 .connect = sctp_connect,
6472 .disconnect = sctp_disconnect,
6473 .accept = sctp_accept,
6474 .ioctl = sctp_ioctl,
6475 .init = sctp_init_sock,
6476 .destroy = sctp_destroy_sock,
6477 .shutdown = sctp_shutdown,
6478 .setsockopt = sctp_setsockopt,
6479 .getsockopt = sctp_getsockopt,
6480 .sendmsg = sctp_sendmsg,
6481 .recvmsg = sctp_recvmsg,
6482 .bind = sctp_bind,
6483 .backlog_rcv = sctp_backlog_rcv,
6484 .hash = sctp_hash,
6485 .unhash = sctp_unhash,
6486 .get_port = sctp_get_port,
6487 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006488 .sysctl_mem = sysctl_sctp_mem,
6489 .sysctl_rmem = sysctl_sctp_rmem,
6490 .sysctl_wmem = sysctl_sctp_wmem,
6491 .memory_pressure = &sctp_memory_pressure,
6492 .enter_memory_pressure = sctp_enter_memory_pressure,
6493 .memory_allocated = &sctp_memory_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494};
6495
6496#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6497struct proto sctpv6_prot = {
6498 .name = "SCTPv6",
6499 .owner = THIS_MODULE,
6500 .close = sctp_close,
6501 .connect = sctp_connect,
6502 .disconnect = sctp_disconnect,
6503 .accept = sctp_accept,
6504 .ioctl = sctp_ioctl,
6505 .init = sctp_init_sock,
6506 .destroy = sctp_destroy_sock,
6507 .shutdown = sctp_shutdown,
6508 .setsockopt = sctp_setsockopt,
6509 .getsockopt = sctp_getsockopt,
6510 .sendmsg = sctp_sendmsg,
6511 .recvmsg = sctp_recvmsg,
6512 .bind = sctp_bind,
6513 .backlog_rcv = sctp_backlog_rcv,
6514 .hash = sctp_hash,
6515 .unhash = sctp_unhash,
6516 .get_port = sctp_get_port,
6517 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006518 .sysctl_mem = sysctl_sctp_mem,
6519 .sysctl_rmem = sysctl_sctp_rmem,
6520 .sysctl_wmem = sysctl_sctp_wmem,
6521 .memory_pressure = &sctp_memory_pressure,
6522 .enter_memory_pressure = sctp_enter_memory_pressure,
6523 .memory_allocated = &sctp_memory_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524};
6525#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */