blob: fb5931ca50d0fc4aad0ac49d60b7591d981ea30f [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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
Joe Perches145ce502010-08-24 13:21:08 +000060#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/types.h>
63#include <linux/kernel.h>
64#include <linux/wait.h>
65#include <linux/time.h>
66#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080067#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/fcntl.h>
69#include <linux/poll.h>
70#include <linux/init.h>
71#include <linux/crypto.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090072#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040073#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#include <net/ip.h>
76#include <net/icmp.h>
77#include <net/route.h>
78#include <net/ipv6.h>
79#include <net/inet_common.h>
80
81#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040082#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <net/sock.h>
84#include <net/sctp/sctp.h>
85#include <net/sctp/sm.h>
86
87/* WARNING: Please do not remove the SCTP_STATIC attribute to
88 * any of the functions below as they are used to export functions
89 * used by a project regression testsuite.
90 */
91
92/* Forward declarations for internal helper functions. */
93static int sctp_writeable(struct sock *sk);
94static void sctp_wfree(struct sk_buff *skb);
95static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
96 size_t msg_len);
97static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
98static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
99static int sctp_wait_for_accept(struct sock *sk, long timeo);
100static void sctp_wait_for_close(struct sock *sk, long timeo);
101static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
102 union sctp_addr *addr, int len);
103static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
104static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
105static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
106static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
107static int sctp_send_asconf(struct sctp_association *asoc,
108 struct sctp_chunk *chunk);
109static int sctp_do_bind(struct sock *, union sctp_addr *, int);
110static int sctp_autobind(struct sock *sk);
111static void sctp_sock_migrate(struct sock *, struct sock *,
112 struct sctp_association *, sctp_socket_type_t);
113static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
114
Neil Horman4d93df02007-08-15 16:07:44 -0700115extern struct kmem_cache *sctp_bucket_cachep;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000116extern long sysctl_sctp_mem[3];
Neil Horman4d93df02007-08-15 16:07:44 -0700117extern int sysctl_sctp_rmem[3];
118extern int sysctl_sctp_wmem[3];
119
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200120static int sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000121static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800122struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700123
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700124static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700125{
126 sctp_memory_pressure = 1;
127}
128
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* Get the sndbuf space available at the time on the association. */
131static inline int sctp_wspace(struct sctp_association *asoc)
132{
Neil Horman4d93df02007-08-15 16:07:44 -0700133 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Neil Horman4d93df02007-08-15 16:07:44 -0700135 if (asoc->ep->sndbuf_policy)
136 amt = asoc->sndbuf_used;
137 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700138 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700139
140 if (amt >= asoc->base.sk->sk_sndbuf) {
141 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
142 amt = 0;
143 else {
144 amt = sk_stream_wspace(asoc->base.sk);
145 if (amt < 0)
146 amt = 0;
147 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700148 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700149 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return amt;
152}
153
154/* Increment the used sndbuf space count of the corresponding association by
155 * the size of the outgoing data chunk.
156 * Also, set the skb destructor for sndbuf accounting later.
157 *
158 * Since it is always 1-1 between chunk and skb, and also a new skb is always
159 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
160 * destructor in the data chunk skb for the purpose of the sndbuf space
161 * tracking.
162 */
163static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
164{
165 struct sctp_association *asoc = chunk->asoc;
166 struct sock *sk = asoc->base.sk;
167
168 /* The sndbuf space is tracked per association. */
169 sctp_association_hold(asoc);
170
Neil Horman4eb701d2005-04-28 12:02:04 -0700171 skb_set_owner_w(chunk->skb, sk);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 chunk->skb->destructor = sctp_wfree;
174 /* Save the chunk pointer in skb for sctp_wfree to use later. */
175 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
176
Neil Horman4eb701d2005-04-28 12:02:04 -0700177 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
178 sizeof(struct sk_buff) +
179 sizeof(struct sctp_chunk);
180
Neil Horman4eb701d2005-04-28 12:02:04 -0700181 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800182 sk->sk_wmem_queued += chunk->skb->truesize;
183 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/* Verify that this is a valid address. */
187static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
188 int len)
189{
190 struct sctp_af *af;
191
192 /* Verify basic sockaddr. */
193 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
194 if (!af)
195 return -EINVAL;
196
197 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700198 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
200
201 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
202 return -EINVAL;
203
204 return 0;
205}
206
207/* Look up the association by its id. If this is not a UDP-style
208 * socket, the ID field is always ignored.
209 */
210struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
211{
212 struct sctp_association *asoc = NULL;
213
214 /* If this is not a UDP-style socket, assoc id should be ignored. */
215 if (!sctp_style(sk, UDP)) {
216 /* Return NULL if the socket state is not ESTABLISHED. It
217 * could be a TCP-style listening socket or a socket which
218 * hasn't yet called connect() to establish an association.
219 */
220 if (!sctp_sstate(sk, ESTABLISHED))
221 return NULL;
222
223 /* Get the first and the only association from the list. */
224 if (!list_empty(&sctp_sk(sk)->ep->asocs))
225 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
226 struct sctp_association, asocs);
227 return asoc;
228 }
229
230 /* Otherwise this is a UDP-style socket. */
231 if (!id || (id == (sctp_assoc_t)-1))
232 return NULL;
233
234 spin_lock_bh(&sctp_assocs_id_lock);
235 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
236 spin_unlock_bh(&sctp_assocs_id_lock);
237
238 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
239 return NULL;
240
241 return asoc;
242}
243
244/* Look up the transport from an address and an assoc id. If both address and
245 * id are specified, the associations matching the address and the id should be
246 * the same.
247 */
248static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
249 struct sockaddr_storage *addr,
250 sctp_assoc_t id)
251{
252 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
253 struct sctp_transport *transport;
254 union sctp_addr *laddr = (union sctp_addr *)addr;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800257 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (!addr_asoc)
261 return NULL;
262
263 id_asoc = sctp_id2assoc(sk, id);
264 if (id_asoc && (id_asoc != addr_asoc))
265 return NULL;
266
267 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
268 (union sctp_addr *)addr);
269
270 return transport;
271}
272
273/* API 3.1.2 bind() - UDP Style Syntax
274 * The syntax of bind() is,
275 *
276 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
277 *
278 * sd - the socket descriptor returned by socket().
279 * addr - the address structure (struct sockaddr_in or struct
280 * sockaddr_in6 [RFC 2553]),
281 * addr_len - the size of the address structure.
282 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700283SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int retval = 0;
286
287 sctp_lock_sock(sk);
288
Frank Filz3f7a87d2005-06-20 13:14:57 -0700289 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
290 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Disallow binding twice. */
293 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700294 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 addr_len);
296 else
297 retval = -EINVAL;
298
299 sctp_release_sock(sk);
300
301 return retval;
302}
303
304static long sctp_get_port_local(struct sock *, union sctp_addr *);
305
306/* Verify this is a valid sockaddr. */
307static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
308 union sctp_addr *addr, int len)
309{
310 struct sctp_af *af;
311
312 /* Check minimum size. */
313 if (len < sizeof (struct sockaddr))
314 return NULL;
315
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700316 /* V4 mapped address are really of AF_INET family */
317 if (addr->sa.sa_family == AF_INET6 &&
318 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
319 if (!opt->pf->af_supported(AF_INET, opt))
320 return NULL;
321 } else {
322 /* Does this PF support this AF? */
323 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
324 return NULL;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* If we get this far, af is valid. */
328 af = sctp_get_af_specific(addr->sa.sa_family);
329
330 if (len < af->sockaddr_len)
331 return NULL;
332
333 return af;
334}
335
336/* Bind a local address either to an endpoint or to an association. */
337SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
338{
339 struct sctp_sock *sp = sctp_sk(sk);
340 struct sctp_endpoint *ep = sp->ep;
341 struct sctp_bind_addr *bp = &ep->base.bind_addr;
342 struct sctp_af *af;
343 unsigned short snum;
344 int ret = 0;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Common sockaddr verification. */
347 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700348 if (!af) {
349 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
350 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700352 }
353
354 snum = ntohs(addr->v4.sin_port);
355
356 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
357 ", port: %d, new port: %d, len: %d)\n",
358 sk,
359 addr,
360 bp->port, snum,
361 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* PF specific bind() address verification. */
364 if (!sp->pf->bind_verify(sp, addr))
365 return -EADDRNOTAVAIL;
366
Vlad Yasevich8b358052007-05-15 17:14:58 -0400367 /* We must either be unbound, or bind to the same port.
368 * It's OK to allow 0 ports if we are already bound.
369 * We'll just inhert an already bound port in this case
370 */
371 if (bp->port) {
372 if (!snum)
373 snum = bp->port;
374 else if (snum != bp->port) {
375 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 " New port %d does not match existing port "
377 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400378 return -EINVAL;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
383 return -EACCES;
384
Vlad Yasevich4e540642008-07-18 23:06:32 -0700385 /* See if the address matches any of the addresses we may have
386 * already bound before checking against other endpoints.
387 */
388 if (sctp_bind_addr_match(bp, addr, sp))
389 return -EINVAL;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Make sure we are allowed to bind here.
392 * The function sctp_get_port_local() does duplicate address
393 * detection.
394 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900395 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700397 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 /* Refresh ephemeral port. */
401 if (!bp->port)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000402 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Vlad Yasevich559cf712007-09-16 16:03:28 -0700404 /* Add the address to the bind address list.
405 * Use GFP_ATOMIC since BHs will be disabled.
406 */
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800407 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Copy back into socket for getsockname() use. */
410 if (!ret) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000411 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 af->to_sk_saddr(addr, sk);
413 }
414
415 return ret;
416}
417
418 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
419 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900420 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900422 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900424 * subsequent ASCONF. Note this restriction binds each side, so at any
425 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * from each endpoint).
427 */
428static int sctp_send_asconf(struct sctp_association *asoc,
429 struct sctp_chunk *chunk)
430{
431 int retval = 0;
432
433 /* If there is an outstanding ASCONF chunk, queue it for later
434 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700437 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 /* Hold the chunk until an ASCONF_ACK is received. */
442 sctp_chunk_hold(chunk);
443 retval = sctp_primitive_ASCONF(asoc, chunk);
444 if (retval)
445 sctp_chunk_free(chunk);
446 else
447 asoc->addip_last_asconf = chunk;
448
449out:
450 return retval;
451}
452
453/* Add a list of addresses as bind addresses to local endpoint or
454 * association.
455 *
456 * Basically run through each address specified in the addrs/addrcnt
457 * array/length pair, determine if it is IPv6 or IPv4 and call
458 * sctp_do_bind() on it.
459 *
460 * If any of them fails, then the operation will be reversed and the
461 * ones that were added will be removed.
462 *
463 * Only sctp_setsockopt_bindx() is supposed to call this function.
464 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200465static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int cnt;
468 int retval = 0;
469 void *addr_buf;
470 struct sockaddr *sa_addr;
471 struct sctp_af *af;
472
473 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
474 sk, addrs, addrcnt);
475
476 addr_buf = addrs;
477 for (cnt = 0; cnt < addrcnt; cnt++) {
478 /* The list may contain either IPv4 or IPv6 address;
479 * determine the address length for walking thru the list.
480 */
Joe Perchesea110732011-06-13 16:21:26 +0000481 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 af = sctp_get_af_specific(sa_addr->sa_family);
483 if (!af) {
484 retval = -EINVAL;
485 goto err_bindx_add;
486 }
487
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900488 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 af->sockaddr_len);
490
491 addr_buf += af->sockaddr_len;
492
493err_bindx_add:
494 if (retval < 0) {
495 /* Failed. Cleanup the ones that have been added */
496 if (cnt > 0)
497 sctp_bindx_rem(sk, addrs, cnt);
498 return retval;
499 }
500 }
501
502 return retval;
503}
504
505/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
506 * associations that are part of the endpoint indicating that a list of local
507 * addresses are added to the endpoint.
508 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900509 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * association, we do not send the chunk for that association. But it will not
511 * affect other associations.
512 *
513 * Only sctp_setsockopt_bindx() is supposed to call this function.
514 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900515static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sockaddr *addrs,
517 int addrcnt)
518{
519 struct sctp_sock *sp;
520 struct sctp_endpoint *ep;
521 struct sctp_association *asoc;
522 struct sctp_bind_addr *bp;
523 struct sctp_chunk *chunk;
524 struct sctp_sockaddr_entry *laddr;
525 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700526 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 void *addr_buf;
528 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct list_head *p;
530 int i;
531 int retval = 0;
532
533 if (!sctp_addip_enable)
534 return retval;
535
536 sp = sctp_sk(sk);
537 ep = sp->ep;
538
539 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800540 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700542 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (!asoc->peer.asconf_capable)
545 continue;
546
547 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
548 continue;
549
550 if (!sctp_state(asoc, ESTABLISHED))
551 continue;
552
553 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900554 * in the bind address list of the association. If so,
555 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * other associations.
557 */
558 addr_buf = addrs;
559 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000560 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 af = sctp_get_af_specific(addr->v4.sin_family);
562 if (!af) {
563 retval = -EINVAL;
564 goto out;
565 }
566
567 if (sctp_assoc_lookup_laddr(asoc, addr))
568 break;
569
570 addr_buf += af->sockaddr_len;
571 }
572 if (i < addrcnt)
573 continue;
574
Vlad Yasevich559cf712007-09-16 16:03:28 -0700575 /* Use the first valid address in bind addr list of
576 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 bp = &asoc->base.bind_addr;
579 p = bp->address_list.next;
580 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800581 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 addrcnt, SCTP_PARAM_ADD_IP);
583 if (!chunk) {
584 retval = -ENOMEM;
585 goto out;
586 }
587
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700591 addr_buf = addrs;
592 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000593 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700594 af = sctp_get_af_specific(addr->v4.sin_family);
595 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800596 retval = sctp_add_bind_addr(bp, &saveaddr,
597 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700598 addr_buf += af->sockaddr_len;
599 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900600 if (asoc->src_out_of_asoc_ok) {
601 struct sctp_transport *trans;
602
603 list_for_each_entry(trans,
604 &asoc->peer.transport_addr_list, transports) {
605 /* Clear the source and route cache */
606 dst_release(trans->dst);
607 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
608 2*asoc->pathmtu, 4380));
609 trans->ssthresh = asoc->peer.i.a_rwnd;
610 trans->rto = asoc->rto_initial;
611 trans->rtt = trans->srtt = trans->rttvar = 0;
612 sctp_transport_route(trans, NULL,
613 sctp_sk(asoc->base.sk));
614 }
615 }
616 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619out:
620 return retval;
621}
622
623/* Remove a list of addresses from bind addresses list. Do not remove the
624 * last address.
625 *
626 * Basically run through each address specified in the addrs/addrcnt
627 * array/length pair, determine if it is IPv6 or IPv4 and call
628 * sctp_del_bind() on it.
629 *
630 * If any of them fails, then the operation will be reversed and the
631 * ones that were removed will be added back.
632 *
633 * At least one address has to be left; if only one address is
634 * available, the operation will return -EBUSY.
635 *
636 * Only sctp_setsockopt_bindx() is supposed to call this function.
637 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200638static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct sctp_sock *sp = sctp_sk(sk);
641 struct sctp_endpoint *ep = sp->ep;
642 int cnt;
643 struct sctp_bind_addr *bp = &ep->base.bind_addr;
644 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800646 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct sctp_af *af;
648
649 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
650 sk, addrs, addrcnt);
651
652 addr_buf = addrs;
653 for (cnt = 0; cnt < addrcnt; cnt++) {
654 /* If the bind address list is empty or if there is only one
655 * bind address, there is nothing more to be removed (we need
656 * at least one address here).
657 */
658 if (list_empty(&bp->address_list) ||
659 (sctp_list_single_entry(&bp->address_list))) {
660 retval = -EBUSY;
661 goto err_bindx_rem;
662 }
663
Joe Perchesea110732011-06-13 16:21:26 +0000664 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800665 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!af) {
667 retval = -EINVAL;
668 goto err_bindx_rem;
669 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700670
671 if (!af->addr_valid(sa_addr, sp, NULL)) {
672 retval = -EADDRNOTAVAIL;
673 goto err_bindx_rem;
674 }
675
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000676 if (sa_addr->v4.sin_port &&
677 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retval = -EINVAL;
679 goto err_bindx_rem;
680 }
681
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000682 if (!sa_addr->v4.sin_port)
683 sa_addr->v4.sin_port = htons(bp->port);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* FIXME - There is probably a need to check if sk->sk_saddr and
686 * sk->sk_rcv_addr are currently set to one of the addresses to
687 * be removed. This is something which needs to be looked into
688 * when we are fixing the outstanding issues with multi-homing
689 * socket routing and failover schemes. Refer to comments in
690 * sctp_do_bind(). -daisy
691 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400692 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 addr_buf += af->sockaddr_len;
695err_bindx_rem:
696 if (retval < 0) {
697 /* Failed. Add the ones that has been removed back */
698 if (cnt > 0)
699 sctp_bindx_add(sk, addrs, cnt);
700 return retval;
701 }
702 }
703
704 return retval;
705}
706
707/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
708 * the associations that are part of the endpoint indicating that a list of
709 * local addresses are removed from the endpoint.
710 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900711 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * association, we do not send the chunk for that association. But it will not
713 * affect other associations.
714 *
715 * Only sctp_setsockopt_bindx() is supposed to call this function.
716 */
717static int sctp_send_asconf_del_ip(struct sock *sk,
718 struct sockaddr *addrs,
719 int addrcnt)
720{
721 struct sctp_sock *sp;
722 struct sctp_endpoint *ep;
723 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700724 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct sctp_bind_addr *bp;
726 struct sctp_chunk *chunk;
727 union sctp_addr *laddr;
728 void *addr_buf;
729 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700730 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int i;
732 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900733 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Michio Honda8a07eb02011-04-26 20:19:36 +0900735 chunk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!sctp_addip_enable)
737 return retval;
738
739 sp = sctp_sk(sk);
740 ep = sp->ep;
741
742 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800743 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700745 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (!asoc->peer.asconf_capable)
748 continue;
749
750 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
751 continue;
752
753 if (!sctp_state(asoc, ESTABLISHED))
754 continue;
755
756 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900757 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * If so, do not send the asconf chunk to its peer, but
759 * continue with other associations.
760 */
761 addr_buf = addrs;
762 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000763 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 af = sctp_get_af_specific(laddr->v4.sin_family);
765 if (!af) {
766 retval = -EINVAL;
767 goto out;
768 }
769
770 if (!sctp_assoc_lookup_laddr(asoc, laddr))
771 break;
772
773 addr_buf += af->sockaddr_len;
774 }
775 if (i < addrcnt)
776 continue;
777
778 /* Find one address in the association's bind address list
779 * that is not in the packed array of addresses. This is to
780 * make sure that we do not delete all the addresses in the
781 * association.
782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 bp = &asoc->base.bind_addr;
784 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
785 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900786 if ((laddr == NULL) && (addrcnt == 1)) {
787 if (asoc->asconf_addr_del_pending)
788 continue;
789 asoc->asconf_addr_del_pending =
790 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900791 if (asoc->asconf_addr_del_pending == NULL) {
792 retval = -ENOMEM;
793 goto out;
794 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900795 asoc->asconf_addr_del_pending->sa.sa_family =
796 addrs->sa_family;
797 asoc->asconf_addr_del_pending->v4.sin_port =
798 htons(bp->port);
799 if (addrs->sa_family == AF_INET) {
800 struct sockaddr_in *sin;
801
802 sin = (struct sockaddr_in *)addrs;
803 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
804 } else if (addrs->sa_family == AF_INET6) {
805 struct sockaddr_in6 *sin6;
806
807 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000808 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900809 }
810 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
811 " at %p\n", asoc, asoc->asconf_addr_del_pending,
812 asoc->asconf_addr_del_pending);
813 asoc->src_out_of_asoc_ok = 1;
814 stored = 1;
815 goto skip_mkasconf;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Vlad Yasevich559cf712007-09-16 16:03:28 -0700818 /* We do not need RCU protection throughout this loop
819 * because this is done under a socket lock from the
820 * setsockopt call.
821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
823 SCTP_PARAM_DEL_IP);
824 if (!chunk) {
825 retval = -ENOMEM;
826 goto out;
827 }
828
Michio Honda8a07eb02011-04-26 20:19:36 +0900829skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700830 /* Reset use_as_src flag for the addresses in the bind address
831 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700833 addr_buf = addrs;
834 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000835 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700836 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700837 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800838 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800839 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700840 }
841 addr_buf += af->sockaddr_len;
842 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700843
844 /* Update the route and saddr entries for all the transports
845 * as some of the addresses in the bind address list are
846 * about to be deleted and cannot be used as source addresses.
847 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700848 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
849 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700850 dst_release(transport->dst);
851 sctp_transport_route(transport, NULL,
852 sctp_sk(asoc->base.sk));
853 }
854
Michio Honda8a07eb02011-04-26 20:19:36 +0900855 if (stored)
856 /* We don't need to transmit ASCONF */
857 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700858 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860out:
861 return retval;
862}
863
Michio Honda9f7d6532011-04-26 19:32:51 +0900864/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
865int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
866{
867 struct sock *sk = sctp_opt2sk(sp);
868 union sctp_addr *addr;
869 struct sctp_af *af;
870
871 /* It is safe to write port space in caller. */
872 addr = &addrw->a;
873 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
874 af = sctp_get_af_specific(addr->sa.sa_family);
875 if (!af)
876 return -EINVAL;
877 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
878 return -EINVAL;
879
880 if (addrw->state == SCTP_ADDR_NEW)
881 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
882 else
883 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
887 *
888 * API 8.1
889 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
890 * int flags);
891 *
892 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
893 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
894 * or IPv6 addresses.
895 *
896 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
897 * Section 3.1.2 for this usage.
898 *
899 * addrs is a pointer to an array of one or more socket addresses. Each
900 * address is contained in its appropriate structure (i.e. struct
901 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700902 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * representation is termed a "packed array" of addresses). The caller
904 * specifies the number of addresses in the array with addrcnt.
905 *
906 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
907 * -1, and sets errno to the appropriate error code.
908 *
909 * For SCTP, the port given in each socket address must be the same, or
910 * sctp_bindx() will fail, setting errno to EINVAL.
911 *
912 * The flags parameter is formed from the bitwise OR of zero or more of
913 * the following currently defined flags:
914 *
915 * SCTP_BINDX_ADD_ADDR
916 *
917 * SCTP_BINDX_REM_ADDR
918 *
919 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
920 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
921 * addresses from the association. The two flags are mutually exclusive;
922 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
923 * not remove all addresses from an association; sctp_bindx() will
924 * reject such an attempt with EINVAL.
925 *
926 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
927 * additional addresses with an endpoint after calling bind(). Or use
928 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
929 * socket is associated with so that no new association accepted will be
930 * associated with those addresses. If the endpoint supports dynamic
931 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
932 * endpoint to send the appropriate message to the peer to change the
933 * peers address lists.
934 *
935 * Adding and removing addresses from a connected association is
936 * optional functionality. Implementations that do not support this
937 * functionality should return EOPNOTSUPP.
938 *
939 * Basically do nothing but copying the addresses from user to kernel
940 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700941 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
942 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
944 * We don't use copy_from_user() for optimization: we first do the
945 * sanity checks (buffer size -fast- and access check-healthy
946 * pointer); if all of those succeed, then we can alloc the memory
947 * (expensive operation) needed to copy the data to kernel. Then we do
948 * the copying without checking the user space area
949 * (__copy_from_user()).
950 *
951 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
952 * it.
953 *
954 * sk The sk of the socket
955 * addrs The pointer to the addresses in user land
956 * addrssize Size of the addrs buffer
957 * op Operation to perform (add or remove, see the flags of
958 * sctp_bindx)
959 *
960 * Returns 0 if ok, <0 errno code on error.
961 */
962SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
963 struct sockaddr __user *addrs,
964 int addrs_size, int op)
965{
966 struct sockaddr *kaddrs;
967 int err;
968 int addrcnt = 0;
969 int walk_size = 0;
970 struct sockaddr *sa_addr;
971 void *addr_buf;
972 struct sctp_af *af;
973
974 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
975 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
976
977 if (unlikely(addrs_size <= 0))
978 return -EINVAL;
979
980 /* Check the user passed a healthy pointer. */
981 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
982 return -EFAULT;
983
984 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800985 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (unlikely(!kaddrs))
987 return -ENOMEM;
988
989 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
990 kfree(kaddrs);
991 return -EFAULT;
992 }
993
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900994 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 addr_buf = kaddrs;
996 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +0000997 if (walk_size + sizeof(sa_family_t) > addrs_size) {
998 kfree(kaddrs);
999 return -EINVAL;
1000 }
1001
Joe Perchesea110732011-06-13 16:21:26 +00001002 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 af = sctp_get_af_specific(sa_addr->sa_family);
1004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 kfree(kaddrs);
1010 return -EINVAL;
1011 }
1012 addrcnt++;
1013 addr_buf += af->sockaddr_len;
1014 walk_size += af->sockaddr_len;
1015 }
1016
1017 /* Do the work. */
1018 switch (op) {
1019 case SCTP_BINDX_ADD_ADDR:
1020 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1021 if (err)
1022 goto out;
1023 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1024 break;
1025
1026 case SCTP_BINDX_REM_ADDR:
1027 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1028 if (err)
1029 goto out;
1030 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1031 break;
1032
1033 default:
1034 err = -EINVAL;
1035 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038out:
1039 kfree(kaddrs);
1040
1041 return err;
1042}
1043
Frank Filz3f7a87d2005-06-20 13:14:57 -07001044/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1045 *
1046 * Common routine for handling connect() and sctp_connectx().
1047 * Connect will come in with just a single address.
1048 */
1049static int __sctp_connect(struct sock* sk,
1050 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001051 int addrs_size,
1052 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001053{
1054 struct sctp_sock *sp;
1055 struct sctp_endpoint *ep;
1056 struct sctp_association *asoc = NULL;
1057 struct sctp_association *asoc2;
1058 struct sctp_transport *transport;
1059 union sctp_addr to;
1060 struct sctp_af *af;
1061 sctp_scope_t scope;
1062 long timeo;
1063 int err = 0;
1064 int addrcnt = 0;
1065 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001066 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001067 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001068 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001069 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070
1071 sp = sctp_sk(sk);
1072 ep = sp->ep;
1073
1074 /* connect() cannot be done on a socket that is already in ESTABLISHED
1075 * state - UDP-style peeled off socket or a TCP-style socket that
1076 * is already connected.
1077 * It cannot be done even on a TCP-style listening socket.
1078 */
1079 if (sctp_sstate(sk, ESTABLISHED) ||
1080 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1081 err = -EISCONN;
1082 goto out_free;
1083 }
1084
1085 /* Walk through the addrs buffer and count the number of addresses. */
1086 addr_buf = kaddrs;
1087 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001088 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1089 err = -EINVAL;
1090 goto out_free;
1091 }
1092
Joe Perchesea110732011-06-13 16:21:26 +00001093 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001094 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001095
1096 /* If the address family is not supported or if this address
1097 * causes the address buffer to overflow return EINVAL.
1098 */
1099 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1100 err = -EINVAL;
1101 goto out_free;
1102 }
1103
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001104 port = ntohs(sa_addr->v4.sin_port);
1105
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001106 /* Save current address so we can work with it */
1107 memcpy(&to, sa_addr, af->sockaddr_len);
1108
1109 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001110 if (err)
1111 goto out_free;
1112
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001113 /* Make sure the destination port is correctly set
1114 * in all addresses.
1115 */
1116 if (asoc && asoc->peer.port && asoc->peer.port != port)
1117 goto out_free;
1118
Frank Filz3f7a87d2005-06-20 13:14:57 -07001119
1120 /* Check if there already is a matching association on the
1121 * endpoint (other than the one created here).
1122 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001123 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001124 if (asoc2 && asoc2 != asoc) {
1125 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1126 err = -EISCONN;
1127 else
1128 err = -EALREADY;
1129 goto out_free;
1130 }
1131
1132 /* If we could not find a matching association on the endpoint,
1133 * make sure that there is no peeled-off association matching
1134 * the peer address even on another socket.
1135 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001136 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001137 err = -EADDRNOTAVAIL;
1138 goto out_free;
1139 }
1140
1141 if (!asoc) {
1142 /* If a bind() or sctp_bindx() is not called prior to
1143 * an sctp_connectx() call, the system picks an
1144 * ephemeral port and will choose an address set
1145 * equivalent to binding with a wildcard address.
1146 */
1147 if (!ep->base.bind_addr.port) {
1148 if (sctp_autobind(sk)) {
1149 err = -EAGAIN;
1150 goto out_free;
1151 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001152 } else {
1153 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001154 * If an unprivileged user inherits a 1-many
1155 * style socket with open associations on a
1156 * privileged port, it MAY be permitted to
1157 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001158 * be permitted to open new associations.
1159 */
1160 if (ep->base.bind_addr.port < PROT_SOCK &&
1161 !capable(CAP_NET_BIND_SERVICE)) {
1162 err = -EACCES;
1163 goto out_free;
1164 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001165 }
1166
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001167 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001168 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1169 if (!asoc) {
1170 err = -ENOMEM;
1171 goto out_free;
1172 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001173
1174 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1175 GFP_KERNEL);
1176 if (err < 0) {
1177 goto out_free;
1178 }
1179
Frank Filz3f7a87d2005-06-20 13:14:57 -07001180 }
1181
1182 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001183 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001184 SCTP_UNKNOWN);
1185 if (!transport) {
1186 err = -ENOMEM;
1187 goto out_free;
1188 }
1189
1190 addrcnt++;
1191 addr_buf += af->sockaddr_len;
1192 walk_size += af->sockaddr_len;
1193 }
1194
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001195 /* In case the user of sctp_connectx() wants an association
1196 * id back, assign one now.
1197 */
1198 if (assoc_id) {
1199 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1200 if (err < 0)
1201 goto out_free;
1202 }
1203
Frank Filz3f7a87d2005-06-20 13:14:57 -07001204 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1205 if (err < 0) {
1206 goto out_free;
1207 }
1208
1209 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001210 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001211 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1212 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001213 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001214
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001215 /* in-kernel sockets don't generally have a file allocated to them
1216 * if all they do is call sock_create_kern().
1217 */
1218 if (sk->sk_socket->file)
1219 f_flags = sk->sk_socket->file->f_flags;
1220
1221 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1222
Frank Filz3f7a87d2005-06-20 13:14:57 -07001223 err = sctp_wait_for_connect(asoc, &timeo);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001224 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001225 *assoc_id = asoc->assoc_id;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001226
1227 /* Don't free association on exit. */
1228 asoc = NULL;
1229
1230out_free:
1231
1232 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001233 " kaddrs: %p err: %d\n",
1234 asoc, kaddrs, err);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001235 if (asoc) {
1236 /* sctp_primitive_ASSOCIATE may have added this association
1237 * To the hash table, try to unhash it, just in case, its a noop
1238 * if it wasn't hashed so we're safe
1239 */
1240 sctp_unhash_established(asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001241 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001242 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001243 return err;
1244}
1245
1246/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1247 *
1248 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001249 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1250 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001251 *
1252 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1253 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1254 * or IPv6 addresses.
1255 *
1256 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1257 * Section 3.1.2 for this usage.
1258 *
1259 * addrs is a pointer to an array of one or more socket addresses. Each
1260 * address is contained in its appropriate structure (i.e. struct
1261 * sockaddr_in or struct sockaddr_in6) the family of the address type
1262 * must be used to distengish the address length (note that this
1263 * representation is termed a "packed array" of addresses). The caller
1264 * specifies the number of addresses in the array with addrcnt.
1265 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001266 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1267 * the association id of the new association. On failure, sctp_connectx()
1268 * returns -1, and sets errno to the appropriate error code. The assoc_id
1269 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001270 *
1271 * For SCTP, the port given in each socket address must be the same, or
1272 * sctp_connectx() will fail, setting errno to EINVAL.
1273 *
1274 * An application can use sctp_connectx to initiate an association with
1275 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1276 * allows a caller to specify multiple addresses at which a peer can be
1277 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001278 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001279 * specifies that the stack will try to make use of all the addresses in
1280 * the list when needed.
1281 *
1282 * Note that the list of addresses passed in is only used for setting up
1283 * the association. It does not necessarily equal the set of addresses
1284 * the peer uses for the resulting association. If the caller wants to
1285 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1286 * retrieve them after the association has been set up.
1287 *
1288 * Basically do nothing but copying the addresses from user to kernel
1289 * land and invoking either sctp_connectx(). This is used for tunneling
1290 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1291 *
1292 * We don't use copy_from_user() for optimization: we first do the
1293 * sanity checks (buffer size -fast- and access check-healthy
1294 * pointer); if all of those succeed, then we can alloc the memory
1295 * (expensive operation) needed to copy the data to kernel. Then we do
1296 * the copying without checking the user space area
1297 * (__copy_from_user()).
1298 *
1299 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1300 * it.
1301 *
1302 * sk The sk of the socket
1303 * addrs The pointer to the addresses in user land
1304 * addrssize Size of the addrs buffer
1305 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001306 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001307 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001308SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001309 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001310 int addrs_size,
1311 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001312{
1313 int err = 0;
1314 struct sockaddr *kaddrs;
1315
1316 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001317 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001318
1319 if (unlikely(addrs_size <= 0))
1320 return -EINVAL;
1321
1322 /* Check the user passed a healthy pointer. */
1323 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1324 return -EFAULT;
1325
1326 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001327 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001328 if (unlikely(!kaddrs))
1329 return -ENOMEM;
1330
1331 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1332 err = -EFAULT;
1333 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001334 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001335 }
1336
1337 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001338
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339 return err;
1340}
1341
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001342/*
1343 * This is an older interface. It's kept for backward compatibility
1344 * to the option that doesn't provide association id.
1345 */
1346SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1347 struct sockaddr __user *addrs,
1348 int addrs_size)
1349{
1350 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1351}
1352
1353/*
1354 * New interface for the API. The since the API is done with a socket
1355 * option, to make it simple we feed back the association id is as a return
1356 * indication to the call. Error is always negative and association id is
1357 * always positive.
1358 */
1359SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1360 struct sockaddr __user *addrs,
1361 int addrs_size)
1362{
1363 sctp_assoc_t assoc_id = 0;
1364 int err = 0;
1365
1366 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1367
1368 if (err)
1369 return err;
1370 else
1371 return assoc_id;
1372}
1373
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001374/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001375 * New (hopefully final) interface for the API.
1376 * We use the sctp_getaddrs_old structure so that use-space library
1377 * can avoid any unnecessary allocations. The only defferent part
1378 * is that we store the actual length of the address buffer into the
1379 * addrs_num structure member. That way we can re-use the existing
1380 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001381 */
1382SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1383 char __user *optval,
1384 int __user *optlen)
1385{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001386 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001387 sctp_assoc_t assoc_id = 0;
1388 int err = 0;
1389
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001390 if (len < sizeof(param))
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001391 return -EINVAL;
1392
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001393 if (copy_from_user(&param, optval, sizeof(param)))
1394 return -EFAULT;
1395
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001396 err = __sctp_setsockopt_connectx(sk,
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001397 (struct sockaddr __user *)param.addrs,
1398 param.addr_num, &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001399
1400 if (err == 0 || err == -EINPROGRESS) {
1401 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1402 return -EFAULT;
1403 if (put_user(sizeof(assoc_id), optlen))
1404 return -EFAULT;
1405 }
1406
1407 return err;
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/* API 3.1.4 close() - UDP Style Syntax
1411 * Applications use close() to perform graceful shutdown (as described in
1412 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1413 * by a UDP-style socket.
1414 *
1415 * The syntax is
1416 *
1417 * ret = close(int sd);
1418 *
1419 * sd - the socket descriptor of the associations to be closed.
1420 *
1421 * To gracefully shutdown a specific association represented by the
1422 * UDP-style socket, an application should use the sendmsg() call,
1423 * passing no user data, but including the appropriate flag in the
1424 * ancillary data (see Section xxxx).
1425 *
1426 * If sd in the close() call is a branched-off socket representing only
1427 * one association, the shutdown is performed on that association only.
1428 *
1429 * 4.1.6 close() - TCP Style Syntax
1430 *
1431 * Applications use close() to gracefully close down an association.
1432 *
1433 * The syntax is:
1434 *
1435 * int close(int sd);
1436 *
1437 * sd - the socket descriptor of the association to be closed.
1438 *
1439 * After an application calls close() on a socket descriptor, no further
1440 * socket operations will succeed on that descriptor.
1441 *
1442 * API 7.1.4 SO_LINGER
1443 *
1444 * An application using the TCP-style socket can use this option to
1445 * perform the SCTP ABORT primitive. The linger option structure is:
1446 *
1447 * struct linger {
1448 * int l_onoff; // option on/off
1449 * int l_linger; // linger time
1450 * };
1451 *
1452 * To enable the option, set l_onoff to 1. If the l_linger value is set
1453 * to 0, calling close() is the same as the ABORT primitive. If the
1454 * value is set to a negative value, the setsockopt() call will return
1455 * an error. If the value is set to a positive value linger_time, the
1456 * close() can be blocked for at most linger_time ms. If the graceful
1457 * shutdown phase does not finish during this period, close() will
1458 * return but the graceful shutdown phase continues in the system.
1459 */
1460SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1461{
1462 struct sctp_endpoint *ep;
1463 struct sctp_association *asoc;
1464 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001465 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1468
1469 sctp_lock_sock(sk);
1470 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001471 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 ep = sctp_sk(sk)->ep;
1474
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001475 /* Clean up any skbs sitting on the receive queue. */
1476 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1477 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1478
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001479 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 list_for_each_safe(pos, temp, &ep->asocs) {
1481 asoc = list_entry(pos, struct sctp_association, asocs);
1482
1483 if (sctp_style(sk, TCP)) {
1484 /* A closed association can still be in the list if
1485 * it belongs to a TCP-style listening socket that is
1486 * not yet accepted. If so, free it. If not, send an
1487 * ABORT or SHUTDOWN based on the linger options.
1488 */
1489 if (sctp_state(asoc, CLOSED)) {
1490 sctp_unhash_established(asoc);
1491 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001492 continue;
1493 }
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001496 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1497 !skb_queue_empty(&asoc->ulpq.reasm) ||
1498 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001499 struct sctp_chunk *chunk;
1500
1501 chunk = sctp_make_abort_user(asoc, NULL, 0);
1502 if (chunk)
1503 sctp_primitive_ABORT(asoc, chunk);
1504 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 sctp_primitive_SHUTDOWN(asoc, NULL);
1506 }
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* On a TCP-style socket, block for at most linger_time if set. */
1509 if (sctp_style(sk, TCP) && timeout)
1510 sctp_wait_for_close(sk, timeout);
1511
1512 /* This will run the backlog queue. */
1513 sctp_release_sock(sk);
1514
1515 /* Supposedly, no process has access to the socket, but
1516 * the net layers still may.
1517 */
1518 sctp_local_bh_disable();
1519 sctp_bh_lock_sock(sk);
1520
1521 /* Hold the sock, since sk_common_release() will put sock_put()
1522 * and we have just a little more cleanup.
1523 */
1524 sock_hold(sk);
1525 sk_common_release(sk);
1526
1527 sctp_bh_unlock_sock(sk);
1528 sctp_local_bh_enable();
1529
1530 sock_put(sk);
1531
1532 SCTP_DBG_OBJCNT_DEC(sock);
1533}
1534
1535/* Handle EPIPE error. */
1536static int sctp_error(struct sock *sk, int flags, int err)
1537{
1538 if (err == -EPIPE)
1539 err = sock_error(sk) ? : -EPIPE;
1540 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1541 send_sig(SIGPIPE, current, 0);
1542 return err;
1543}
1544
1545/* API 3.1.3 sendmsg() - UDP Style Syntax
1546 *
1547 * An application uses sendmsg() and recvmsg() calls to transmit data to
1548 * and receive data from its peer.
1549 *
1550 * ssize_t sendmsg(int socket, const struct msghdr *message,
1551 * int flags);
1552 *
1553 * socket - the socket descriptor of the endpoint.
1554 * message - pointer to the msghdr structure which contains a single
1555 * user message and possibly some ancillary data.
1556 *
1557 * See Section 5 for complete description of the data
1558 * structures.
1559 *
1560 * flags - flags sent or received with the user message, see Section
1561 * 5 for complete description of the flags.
1562 *
1563 * Note: This function could use a rewrite especially when explicit
1564 * connect support comes in.
1565 */
1566/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1567
1568SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1569
1570SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1571 struct msghdr *msg, size_t msg_len)
1572{
1573 struct sctp_sock *sp;
1574 struct sctp_endpoint *ep;
1575 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1576 struct sctp_transport *transport, *chunk_tp;
1577 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001578 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001580 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct sctp_sndrcvinfo *sinfo;
1582 struct sctp_initmsg *sinit;
1583 sctp_assoc_t associd = 0;
1584 sctp_cmsgs_t cmsgs = { NULL };
1585 int err;
1586 sctp_scope_t scope;
1587 long timeo;
1588 __u16 sinfo_flags = 0;
1589 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int msg_flags = msg->msg_flags;
1591
1592 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1593 sk, msg, msg_len);
1594
1595 err = 0;
1596 sp = sctp_sk(sk);
1597 ep = sp->ep;
1598
Frank Filz3f7a87d2005-06-20 13:14:57 -07001599 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* We cannot send a message over a TCP-style listening socket. */
1602 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1603 err = -EPIPE;
1604 goto out_nounlock;
1605 }
1606
1607 /* Parse out the SCTP CMSGs. */
1608 err = sctp_msghdr_parse(msg, &cmsgs);
1609
1610 if (err) {
1611 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1612 goto out_nounlock;
1613 }
1614
1615 /* Fetch the destination address for this packet. This
1616 * address only selects the association--it is not necessarily
1617 * the address we will send to.
1618 * For a peeled-off socket, msg_name is ignored.
1619 */
1620 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1621 int msg_namelen = msg->msg_namelen;
1622
1623 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1624 msg_namelen);
1625 if (err)
1626 return err;
1627
1628 if (msg_namelen > sizeof(to))
1629 msg_namelen = sizeof(to);
1630 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 msg_name = msg->msg_name;
1632 }
1633
1634 sinfo = cmsgs.info;
1635 sinit = cmsgs.init;
1636
1637 /* Did the user specify SNDRCVINFO? */
1638 if (sinfo) {
1639 sinfo_flags = sinfo->sinfo_flags;
1640 associd = sinfo->sinfo_assoc_id;
1641 }
1642
1643 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1644 msg_len, sinfo_flags);
1645
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001646 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1647 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 err = -EINVAL;
1649 goto out_nounlock;
1650 }
1651
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001652 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1653 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1654 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001656 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001657 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1658 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 err = -EINVAL;
1660 goto out_nounlock;
1661 }
1662
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001663 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * specified in msg_name.
1665 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001666 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 err = -EINVAL;
1668 goto out_nounlock;
1669 }
1670
1671 transport = NULL;
1672
1673 SCTP_DEBUG_PRINTK("About to look up association.\n");
1674
1675 sctp_lock_sock(sk);
1676
1677 /* If a msg_name has been specified, assume this is to be used. */
1678 if (msg_name) {
1679 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001680 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!asoc) {
1682 /* If we could not find a matching association on the
1683 * endpoint, make sure that it is not a TCP-style
1684 * socket that already has an association or there is
1685 * no peeled-off association on another socket.
1686 */
1687 if ((sctp_style(sk, TCP) &&
1688 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001689 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 err = -EADDRNOTAVAIL;
1691 goto out_unlock;
1692 }
1693 }
1694 } else {
1695 asoc = sctp_id2assoc(sk, associd);
1696 if (!asoc) {
1697 err = -EPIPE;
1698 goto out_unlock;
1699 }
1700 }
1701
1702 if (asoc) {
1703 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1704
1705 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1706 * socket that has an association in CLOSED state. This can
1707 * happen when an accepted socket has an association that is
1708 * already CLOSED.
1709 */
1710 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1711 err = -EPIPE;
1712 goto out_unlock;
1713 }
1714
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001715 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1717 asoc);
1718 sctp_primitive_SHUTDOWN(asoc, NULL);
1719 err = 0;
1720 goto out_unlock;
1721 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001722 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001723
1724 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1725 if (!chunk) {
1726 err = -ENOMEM;
1727 goto out_unlock;
1728 }
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001731 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 err = 0;
1733 goto out_unlock;
1734 }
1735 }
1736
1737 /* Do we need to create the association? */
1738 if (!asoc) {
1739 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1740
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001741 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 err = -EINVAL;
1743 goto out_unlock;
1744 }
1745
1746 /* Check for invalid stream against the stream counts,
1747 * either the default or the user specified stream counts.
1748 */
1749 if (sinfo) {
1750 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1751 /* Check against the defaults. */
1752 if (sinfo->sinfo_stream >=
1753 sp->initmsg.sinit_num_ostreams) {
1754 err = -EINVAL;
1755 goto out_unlock;
1756 }
1757 } else {
1758 /* Check against the requested. */
1759 if (sinfo->sinfo_stream >=
1760 sinit->sinit_num_ostreams) {
1761 err = -EINVAL;
1762 goto out_unlock;
1763 }
1764 }
1765 }
1766
1767 /*
1768 * API 3.1.2 bind() - UDP Style Syntax
1769 * If a bind() or sctp_bindx() is not called prior to a
1770 * sendmsg() call that initiates a new association, the
1771 * system picks an ephemeral port and will choose an address
1772 * set equivalent to binding with a wildcard address.
1773 */
1774 if (!ep->base.bind_addr.port) {
1775 if (sctp_autobind(sk)) {
1776 err = -EAGAIN;
1777 goto out_unlock;
1778 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001779 } else {
1780 /*
1781 * If an unprivileged user inherits a one-to-many
1782 * style socket with open associations on a privileged
1783 * port, it MAY be permitted to accept new associations,
1784 * but it SHOULD NOT be permitted to open new
1785 * associations.
1786 */
1787 if (ep->base.bind_addr.port < PROT_SOCK &&
1788 !capable(CAP_NET_BIND_SERVICE)) {
1789 err = -EACCES;
1790 goto out_unlock;
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
1794 scope = sctp_scope(&to);
1795 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1796 if (!new_asoc) {
1797 err = -ENOMEM;
1798 goto out_unlock;
1799 }
1800 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001801 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1802 if (err < 0) {
1803 err = -ENOMEM;
1804 goto out_free;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 /* If the SCTP_INIT ancillary data is specified, set all
1808 * the association init values accordingly.
1809 */
1810 if (sinit) {
1811 if (sinit->sinit_num_ostreams) {
1812 asoc->c.sinit_num_ostreams =
1813 sinit->sinit_num_ostreams;
1814 }
1815 if (sinit->sinit_max_instreams) {
1816 asoc->c.sinit_max_instreams =
1817 sinit->sinit_max_instreams;
1818 }
1819 if (sinit->sinit_max_attempts) {
1820 asoc->max_init_attempts
1821 = sinit->sinit_max_attempts;
1822 }
1823 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001824 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1826 }
1827 }
1828
1829 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001830 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (!transport) {
1832 err = -ENOMEM;
1833 goto out_free;
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
1837 /* ASSERT: we have a valid association at this point. */
1838 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1839
1840 if (!sinfo) {
1841 /* If the user didn't specify SNDRCVINFO, make up one with
1842 * some defaults.
1843 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001844 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 default_sinfo.sinfo_stream = asoc->default_stream;
1846 default_sinfo.sinfo_flags = asoc->default_flags;
1847 default_sinfo.sinfo_ppid = asoc->default_ppid;
1848 default_sinfo.sinfo_context = asoc->default_context;
1849 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1850 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1851 sinfo = &default_sinfo;
1852 }
1853
1854 /* API 7.1.7, the sndbuf size per association bounds the
1855 * maximum size of data that can be sent in a single send call.
1856 */
1857 if (msg_len > sk->sk_sndbuf) {
1858 err = -EMSGSIZE;
1859 goto out_free;
1860 }
1861
Vlad Yasevich8a479492007-06-07 14:21:05 -04001862 if (asoc->pmtu_pending)
David S. Miller02f3d4c2012-07-16 03:57:14 -07001863 sctp_assoc_pending_pmtu(sk, asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /* If fragmentation is disabled and the message length exceeds the
1866 * association fragmentation point, return EMSGSIZE. The I-D
1867 * does not specify what this error is, but this looks like
1868 * a great fit.
1869 */
1870 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1871 err = -EMSGSIZE;
1872 goto out_free;
1873 }
1874
Joe Perchesafd76142011-05-12 09:19:10 +00001875 /* Check for invalid stream. */
1876 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1877 err = -EINVAL;
1878 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
1881 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1882 if (!sctp_wspace(asoc)) {
1883 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1884 if (err)
1885 goto out_free;
1886 }
1887
1888 /* If an address is passed with the sendto/sendmsg call, it is used
1889 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001890 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
1892 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001893 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001894 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!chunk_tp) {
1896 err = -EINVAL;
1897 goto out_free;
1898 }
1899 } else
1900 chunk_tp = NULL;
1901
1902 /* Auto-connect, if we aren't connected already. */
1903 if (sctp_state(asoc, CLOSED)) {
1904 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1905 if (err < 0)
1906 goto out_free;
1907 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1908 }
1909
1910 /* Break the message into multiple chunks of maximum size. */
1911 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1912 if (!datamsg) {
1913 err = -ENOMEM;
1914 goto out_free;
1915 }
1916
1917 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001918 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Florian Westphal80445cf2008-03-23 22:47:08 -07001919 sctp_chunk_hold(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* Do accounting for the write space. */
1922 sctp_set_owner_w(chunk);
1923
1924 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001927 /* Send it to the lower layers. Note: all chunks
1928 * must either fail or succeed. The lower layer
1929 * works that way today. Keep it that way or this
1930 * breaks.
1931 */
1932 err = sctp_primitive_SEND(asoc, datamsg);
1933 /* Did the lower layer accept the chunk? */
1934 if (err)
1935 sctp_datamsg_free(datamsg);
1936 else
1937 sctp_datamsg_put(datamsg);
1938
1939 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (err)
1942 goto out_free;
1943 else
1944 err = msg_len;
1945
1946 /* If we are already past ASSOCIATE, the lower
1947 * layers are responsible for association cleanup.
1948 */
1949 goto out_unlock;
1950
1951out_free:
Neil Horman2eebc1e2012-07-16 09:13:51 +00001952 if (new_asoc) {
1953 sctp_unhash_established(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956out_unlock:
1957 sctp_release_sock(sk);
1958
1959out_nounlock:
1960 return sctp_error(sk, msg_flags, err);
1961
1962#if 0
1963do_sock_err:
1964 if (msg_len)
1965 err = msg_len;
1966 else
1967 err = sock_error(sk);
1968 goto out;
1969
1970do_interrupted:
1971 if (msg_len)
1972 err = msg_len;
1973 goto out;
1974#endif /* 0 */
1975}
1976
1977/* This is an extended version of skb_pull() that removes the data from the
1978 * start of a skb even when data is spread across the list of skb's in the
1979 * frag_list. len specifies the total amount of data that needs to be removed.
1980 * when 'len' bytes could be removed from the skb, it returns 0.
1981 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1982 * could not be removed.
1983 */
1984static int sctp_skb_pull(struct sk_buff *skb, int len)
1985{
1986 struct sk_buff *list;
1987 int skb_len = skb_headlen(skb);
1988 int rlen;
1989
1990 if (len <= skb_len) {
1991 __skb_pull(skb, len);
1992 return 0;
1993 }
1994 len -= skb_len;
1995 __skb_pull(skb, skb_len);
1996
David S. Miller1b003be2009-06-09 00:22:35 -07001997 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 rlen = sctp_skb_pull(list, len);
1999 skb->len -= (len-rlen);
2000 skb->data_len -= (len-rlen);
2001
2002 if (!rlen)
2003 return 0;
2004
2005 len = rlen;
2006 }
2007
2008 return len;
2009}
2010
2011/* API 3.1.3 recvmsg() - UDP Style Syntax
2012 *
2013 * ssize_t recvmsg(int socket, struct msghdr *message,
2014 * int flags);
2015 *
2016 * socket - the socket descriptor of the endpoint.
2017 * message - pointer to the msghdr structure which contains a single
2018 * user message and possibly some ancillary data.
2019 *
2020 * See Section 5 for complete description of the data
2021 * structures.
2022 *
2023 * flags - flags sent or received with the user message, see Section
2024 * 5 for complete description of the flags.
2025 */
2026static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2027
2028SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2029 struct msghdr *msg, size_t len, int noblock,
2030 int flags, int *addr_len)
2031{
2032 struct sctp_ulpevent *event = NULL;
2033 struct sctp_sock *sp = sctp_sk(sk);
2034 struct sk_buff *skb;
2035 int copied;
2036 int err = 0;
2037 int skb_len;
2038
2039 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2040 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2041 "len", len, "knoblauch", noblock,
2042 "flags", flags, "addr_len", addr_len);
2043
2044 sctp_lock_sock(sk);
2045
2046 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2047 err = -ENOTCONN;
2048 goto out;
2049 }
2050
2051 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2052 if (!skb)
2053 goto out;
2054
2055 /* Get the total length of the skb including any skb's in the
2056 * frag_list.
2057 */
2058 skb_len = skb->len;
2059
2060 copied = skb_len;
2061 if (copied > len)
2062 copied = len;
2063
2064 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2065
2066 event = sctp_skb2event(skb);
2067
2068 if (err)
2069 goto out_free;
2070
Neil Horman3b885782009-10-12 13:26:31 -07002071 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (sctp_ulpevent_is_notification(event)) {
2073 msg->msg_flags |= MSG_NOTIFICATION;
2074 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2075 } else {
2076 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2077 }
2078
2079 /* Check if we allow SCTP_SNDRCVINFO. */
2080 if (sp->subscribe.sctp_data_io_event)
2081 sctp_ulpevent_read_sndrcvinfo(event, msg);
2082#if 0
2083 /* FIXME: we should be calling IP/IPv6 layers. */
2084 if (sk->sk_protinfo.af_inet.cmsg_flags)
2085 ip_cmsg_recv(msg, skb);
2086#endif
2087
2088 err = copied;
2089
2090 /* If skb's length exceeds the user's buffer, update the skb and
2091 * push it back to the receive_queue so that the next call to
2092 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2093 */
2094 if (skb_len > copied) {
2095 msg->msg_flags &= ~MSG_EOR;
2096 if (flags & MSG_PEEK)
2097 goto out_free;
2098 sctp_skb_pull(skb, copied);
2099 skb_queue_head(&sk->sk_receive_queue, skb);
2100
2101 /* When only partial message is copied to the user, increase
2102 * rwnd by that amount. If all the data in the skb is read,
2103 * rwnd is updated when the event is freed.
2104 */
Vlad Yasevich0eca8fe2008-01-11 10:12:56 -05002105 if (!sctp_ulpevent_is_notification(event))
2106 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto out;
2108 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2109 (event->msg_flags & MSG_EOR))
2110 msg->msg_flags |= MSG_EOR;
2111 else
2112 msg->msg_flags &= ~MSG_EOR;
2113
2114out_free:
2115 if (flags & MSG_PEEK) {
2116 /* Release the skb reference acquired after peeking the skb in
2117 * sctp_skb_recv_datagram().
2118 */
2119 kfree_skb(skb);
2120 } else {
2121 /* Free the event which includes releasing the reference to
2122 * the owner of the skb, freeing the skb and updating the
2123 * rwnd.
2124 */
2125 sctp_ulpevent_free(event);
2126 }
2127out:
2128 sctp_release_sock(sk);
2129 return err;
2130}
2131
2132/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2133 *
2134 * This option is a on/off flag. If enabled no SCTP message
2135 * fragmentation will be performed. Instead if a message being sent
2136 * exceeds the current PMTU size, the message will NOT be sent and
2137 * instead a error will be indicated to the user.
2138 */
2139static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002140 char __user *optval,
2141 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int val;
2144
2145 if (optlen < sizeof(int))
2146 return -EINVAL;
2147
2148 if (get_user(val, (int __user *)optval))
2149 return -EFAULT;
2150
2151 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2152
2153 return 0;
2154}
2155
2156static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002157 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Wei Yongjun94912302011-07-02 09:28:04 +00002159 struct sctp_association *asoc;
2160 struct sctp_ulpevent *event;
2161
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002162 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -EINVAL;
2164 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2165 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002166
2167 /*
2168 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2169 * if there is no data to be sent or retransmit, the stack will
2170 * immediately send up this notification.
2171 */
2172 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2173 &sctp_sk(sk)->subscribe)) {
2174 asoc = sctp_id2assoc(sk, 0);
2175
2176 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2177 event = sctp_ulpevent_make_sender_dry_event(asoc,
2178 GFP_ATOMIC);
2179 if (!event)
2180 return -ENOMEM;
2181
2182 sctp_ulpq_tail_event(&asoc->ulpq, event);
2183 }
2184 }
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
2187}
2188
2189/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2190 *
2191 * This socket option is applicable to the UDP-style socket only. When
2192 * set it will cause associations that are idle for more than the
2193 * specified number of seconds to automatically close. An association
2194 * being idle is defined an association that has NOT sent or received
2195 * user data. The special value of '0' indicates that no automatic
2196 * close of any associations should be performed. The option expects an
2197 * integer defining the number of seconds of idle time before an
2198 * association is closed.
2199 */
2200static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002201 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct sctp_sock *sp = sctp_sk(sk);
2204
2205 /* Applicable to UDP-style socket only */
2206 if (sctp_style(sk, TCP))
2207 return -EOPNOTSUPP;
2208 if (optlen != sizeof(int))
2209 return -EINVAL;
2210 if (copy_from_user(&sp->autoclose, optval, optlen))
2211 return -EFAULT;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
2215
2216/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2217 *
2218 * Applications can enable or disable heartbeats for any peer address of
2219 * an association, modify an address's heartbeat interval, force a
2220 * heartbeat to be sent immediately, and adjust the address's maximum
2221 * number of retransmissions sent before an address is considered
2222 * unreachable. The following structure is used to access and modify an
2223 * address's parameters:
2224 *
2225 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002226 * sctp_assoc_t spp_assoc_id;
2227 * struct sockaddr_storage spp_address;
2228 * uint32_t spp_hbinterval;
2229 * uint16_t spp_pathmaxrxt;
2230 * uint32_t spp_pathmtu;
2231 * uint32_t spp_sackdelay;
2232 * uint32_t spp_flags;
2233 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002235 * spp_assoc_id - (one-to-many style socket) This is filled in the
2236 * application, and identifies the association for
2237 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * spp_address - This specifies which address is of interest.
2239 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002240 * in milliseconds. If a value of zero
2241 * is present in this field then no changes are to
2242 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * spp_pathmaxrxt - This contains the maximum number of
2244 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002245 * considered unreachable. If a value of zero
2246 * is present in this field then no changes are to
2247 * be made to this parameter.
2248 * spp_pathmtu - When Path MTU discovery is disabled the value
2249 * specified here will be the "fixed" path mtu.
2250 * Note that if the spp_address field is empty
2251 * then all associations on this address will
2252 * have this fixed path mtu set upon them.
2253 *
2254 * spp_sackdelay - When delayed sack is enabled, this value specifies
2255 * the number of milliseconds that sacks will be delayed
2256 * for. This value will apply to all addresses of an
2257 * association if the spp_address field is empty. Note
2258 * also, that if delayed sack is enabled and this
2259 * value is set to 0, no change is made to the last
2260 * recorded delayed sack timer value.
2261 *
2262 * spp_flags - These flags are used to control various features
2263 * on an association. The flag field may contain
2264 * zero or more of the following options.
2265 *
2266 * SPP_HB_ENABLE - Enable heartbeats on the
2267 * specified address. Note that if the address
2268 * field is empty all addresses for the association
2269 * have heartbeats enabled upon them.
2270 *
2271 * SPP_HB_DISABLE - Disable heartbeats on the
2272 * speicifed address. Note that if the address
2273 * field is empty all addresses for the association
2274 * will have their heartbeats disabled. Note also
2275 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2276 * mutually exclusive, only one of these two should
2277 * be specified. Enabling both fields will have
2278 * undetermined results.
2279 *
2280 * SPP_HB_DEMAND - Request a user initiated heartbeat
2281 * to be made immediately.
2282 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002283 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2284 * heartbeat delayis to be set to the value of 0
2285 * milliseconds.
2286 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002287 * SPP_PMTUD_ENABLE - This field will enable PMTU
2288 * discovery upon the specified address. Note that
2289 * if the address feild is empty then all addresses
2290 * on the association are effected.
2291 *
2292 * SPP_PMTUD_DISABLE - This field will disable PMTU
2293 * discovery upon the specified address. Note that
2294 * if the address feild is empty then all addresses
2295 * on the association are effected. Not also that
2296 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2297 * exclusive. Enabling both will have undetermined
2298 * results.
2299 *
2300 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2301 * on delayed sack. The time specified in spp_sackdelay
2302 * is used to specify the sack delay for this address. Note
2303 * that if spp_address is empty then all addresses will
2304 * enable delayed sack and take on the sack delay
2305 * value specified in spp_sackdelay.
2306 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2307 * off delayed sack. If the spp_address field is blank then
2308 * delayed sack is disabled for the entire association. Note
2309 * also that this field is mutually exclusive to
2310 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2311 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 */
Adrian Bunk16164362006-09-18 00:40:38 -07002313static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2314 struct sctp_transport *trans,
2315 struct sctp_association *asoc,
2316 struct sctp_sock *sp,
2317 int hb_change,
2318 int pmtud_change,
2319 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 int error;
2322
Frank Filz52ccb8e2005-12-22 11:36:46 -08002323 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2325 if (error)
2326 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002329 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2330 * this field is ignored. Note also that a value of zero indicates
2331 * the current setting should be left unchanged.
2332 */
2333 if (params->spp_flags & SPP_HB_ENABLE) {
2334
2335 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2336 * set. This lets us use 0 value when this flag
2337 * is set.
2338 */
2339 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2340 params->spp_hbinterval = 0;
2341
2342 if (params->spp_hbinterval ||
2343 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2344 if (trans) {
2345 trans->hbinterval =
2346 msecs_to_jiffies(params->spp_hbinterval);
2347 } else if (asoc) {
2348 asoc->hbinterval =
2349 msecs_to_jiffies(params->spp_hbinterval);
2350 } else {
2351 sp->hbinterval = params->spp_hbinterval;
2352 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002353 }
2354 }
2355
2356 if (hb_change) {
2357 if (trans) {
2358 trans->param_flags =
2359 (trans->param_flags & ~SPP_HB) | hb_change;
2360 } else if (asoc) {
2361 asoc->param_flags =
2362 (asoc->param_flags & ~SPP_HB) | hb_change;
2363 } else {
2364 sp->param_flags =
2365 (sp->param_flags & ~SPP_HB) | hb_change;
2366 }
2367 }
2368
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002369 /* When Path MTU discovery is disabled the value specified here will
2370 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2371 * include the flag SPP_PMTUD_DISABLE for this field to have any
2372 * effect).
2373 */
2374 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002375 if (trans) {
2376 trans->pathmtu = params->spp_pathmtu;
David S. Miller02f3d4c2012-07-16 03:57:14 -07002377 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002378 } else if (asoc) {
2379 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002380 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002381 } else {
2382 sp->pathmtu = params->spp_pathmtu;
2383 }
2384 }
2385
2386 if (pmtud_change) {
2387 if (trans) {
2388 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2389 (params->spp_flags & SPP_PMTUD_ENABLE);
2390 trans->param_flags =
2391 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2392 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002393 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
David S. Miller02f3d4c2012-07-16 03:57:14 -07002394 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002395 }
2396 } else if (asoc) {
2397 asoc->param_flags =
2398 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2399 } else {
2400 sp->param_flags =
2401 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2402 }
2403 }
2404
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002405 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2406 * value of this field is ignored. Note also that a value of zero
2407 * indicates the current setting should be left unchanged.
2408 */
2409 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002410 if (trans) {
2411 trans->sackdelay =
2412 msecs_to_jiffies(params->spp_sackdelay);
2413 } else if (asoc) {
2414 asoc->sackdelay =
2415 msecs_to_jiffies(params->spp_sackdelay);
2416 } else {
2417 sp->sackdelay = params->spp_sackdelay;
2418 }
2419 }
2420
2421 if (sackdelay_change) {
2422 if (trans) {
2423 trans->param_flags =
2424 (trans->param_flags & ~SPP_SACKDELAY) |
2425 sackdelay_change;
2426 } else if (asoc) {
2427 asoc->param_flags =
2428 (asoc->param_flags & ~SPP_SACKDELAY) |
2429 sackdelay_change;
2430 } else {
2431 sp->param_flags =
2432 (sp->param_flags & ~SPP_SACKDELAY) |
2433 sackdelay_change;
2434 }
2435 }
2436
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002437 /* Note that a value of zero indicates the current setting should be
2438 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002439 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002440 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002441 if (trans) {
2442 trans->pathmaxrxt = params->spp_pathmaxrxt;
2443 } else if (asoc) {
2444 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2445 } else {
2446 sp->pathmaxrxt = params->spp_pathmaxrxt;
2447 }
2448 }
2449
2450 return 0;
2451}
2452
2453static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002454 char __user *optval,
2455 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002456{
2457 struct sctp_paddrparams params;
2458 struct sctp_transport *trans = NULL;
2459 struct sctp_association *asoc = NULL;
2460 struct sctp_sock *sp = sctp_sk(sk);
2461 int error;
2462 int hb_change, pmtud_change, sackdelay_change;
2463
2464 if (optlen != sizeof(struct sctp_paddrparams))
2465 return - EINVAL;
2466
2467 if (copy_from_user(&params, optval, optlen))
2468 return -EFAULT;
2469
2470 /* Validate flags and value parameters. */
2471 hb_change = params.spp_flags & SPP_HB;
2472 pmtud_change = params.spp_flags & SPP_PMTUD;
2473 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2474
2475 if (hb_change == SPP_HB ||
2476 pmtud_change == SPP_PMTUD ||
2477 sackdelay_change == SPP_SACKDELAY ||
2478 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002479 (params.spp_pathmtu &&
2480 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002481 return -EINVAL;
2482
2483 /* If an address other than INADDR_ANY is specified, and
2484 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04002486 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002487 trans = sctp_addr_id2transport(sk, &params.spp_address,
2488 params.spp_assoc_id);
2489 if (!trans)
2490 return -EINVAL;
2491 }
2492
2493 /* Get association, if assoc_id != 0 and the socket is a one
2494 * to many style socket, and an association was not found, then
2495 * the id was invalid.
2496 */
2497 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2498 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2499 return -EINVAL;
2500
2501 /* Heartbeat demand can only be sent on a transport or
2502 * association, but not a socket.
2503 */
2504 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2505 return -EINVAL;
2506
2507 /* Process parameters. */
2508 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2509 hb_change, pmtud_change,
2510 sackdelay_change);
2511
2512 if (error)
2513 return error;
2514
2515 /* If changes are for association, also apply parameters to each
2516 * transport.
2517 */
2518 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002519 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2520 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002521 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2522 hb_change, pmtud_change,
2523 sackdelay_change);
2524 }
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 return 0;
2528}
2529
Wei Yongjund364d922008-05-09 15:13:26 -07002530/*
2531 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002532 *
Wei Yongjund364d922008-05-09 15:13:26 -07002533 * This option will effect the way delayed acks are performed. This
2534 * option allows you to get or set the delayed ack time, in
2535 * milliseconds. It also allows changing the delayed ack frequency.
2536 * Changing the frequency to 1 disables the delayed sack algorithm. If
2537 * the assoc_id is 0, then this sets or gets the endpoints default
2538 * values. If the assoc_id field is non-zero, then the set or get
2539 * effects the specified association for the one to many model (the
2540 * assoc_id field is ignored by the one to one model). Note that if
2541 * sack_delay or sack_freq are 0 when setting this option, then the
2542 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002543 *
Wei Yongjund364d922008-05-09 15:13:26 -07002544 * struct sctp_sack_info {
2545 * sctp_assoc_t sack_assoc_id;
2546 * uint32_t sack_delay;
2547 * uint32_t sack_freq;
2548 * };
Frank Filz77086102005-12-22 11:37:30 -08002549 *
Wei Yongjund364d922008-05-09 15:13:26 -07002550 * sack_assoc_id - This parameter, indicates which association the user
2551 * is performing an action upon. Note that if this field's value is
2552 * zero then the endpoints default value is changed (effecting future
2553 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002554 *
Wei Yongjund364d922008-05-09 15:13:26 -07002555 * sack_delay - This parameter contains the number of milliseconds that
2556 * the user is requesting the delayed ACK timer be set to. Note that
2557 * this value is defined in the standard to be between 200 and 500
2558 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002559 *
Wei Yongjund364d922008-05-09 15:13:26 -07002560 * sack_freq - This parameter contains the number of packets that must
2561 * be received before a sack is sent without waiting for the delay
2562 * timer to expire. The default value for this is 2, setting this
2563 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002564 */
2565
Wei Yongjund364d922008-05-09 15:13:26 -07002566static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002567 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002568{
Wei Yongjund364d922008-05-09 15:13:26 -07002569 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002570 struct sctp_transport *trans = NULL;
2571 struct sctp_association *asoc = NULL;
2572 struct sctp_sock *sp = sctp_sk(sk);
2573
Wei Yongjund364d922008-05-09 15:13:26 -07002574 if (optlen == sizeof(struct sctp_sack_info)) {
2575 if (copy_from_user(&params, optval, optlen))
2576 return -EFAULT;
2577
2578 if (params.sack_delay == 0 && params.sack_freq == 0)
2579 return 0;
2580 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00002581 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2582 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07002583 if (copy_from_user(&params, optval, optlen))
2584 return -EFAULT;
2585
2586 if (params.sack_delay == 0)
2587 params.sack_freq = 1;
2588 else
2589 params.sack_freq = 0;
2590 } else
Frank Filz77086102005-12-22 11:37:30 -08002591 return - EINVAL;
2592
Frank Filz77086102005-12-22 11:37:30 -08002593 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002594 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002595 return -EINVAL;
2596
Wei Yongjund364d922008-05-09 15:13:26 -07002597 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002598 * to many style socket, and an association was not found, then
2599 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002600 */
Wei Yongjund364d922008-05-09 15:13:26 -07002601 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2602 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002603 return -EINVAL;
2604
Wei Yongjund364d922008-05-09 15:13:26 -07002605 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002606 if (asoc) {
2607 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002608 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002609 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002610 (asoc->param_flags & ~SPP_SACKDELAY) |
2611 SPP_SACKDELAY_ENABLE;
2612 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002613 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002614 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002615 (sp->param_flags & ~SPP_SACKDELAY) |
2616 SPP_SACKDELAY_ENABLE;
2617 }
Wei Yongjund364d922008-05-09 15:13:26 -07002618 }
2619
2620 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002621 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002622 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002623 (asoc->param_flags & ~SPP_SACKDELAY) |
2624 SPP_SACKDELAY_DISABLE;
2625 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002626 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002627 (sp->param_flags & ~SPP_SACKDELAY) |
2628 SPP_SACKDELAY_DISABLE;
2629 }
Wei Yongjund364d922008-05-09 15:13:26 -07002630 } else if (params.sack_freq > 1) {
2631 if (asoc) {
2632 asoc->sackfreq = params.sack_freq;
2633 asoc->param_flags =
2634 (asoc->param_flags & ~SPP_SACKDELAY) |
2635 SPP_SACKDELAY_ENABLE;
2636 } else {
2637 sp->sackfreq = params.sack_freq;
2638 sp->param_flags =
2639 (sp->param_flags & ~SPP_SACKDELAY) |
2640 SPP_SACKDELAY_ENABLE;
2641 }
Frank Filz77086102005-12-22 11:37:30 -08002642 }
2643
2644 /* If change is for association, also apply to each transport. */
2645 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002646 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2647 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002648 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002649 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002650 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002651 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002652 (trans->param_flags & ~SPP_SACKDELAY) |
2653 SPP_SACKDELAY_ENABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002654 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002655 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002656 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002657 (trans->param_flags & ~SPP_SACKDELAY) |
2658 SPP_SACKDELAY_DISABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002659 } else if (params.sack_freq > 1) {
2660 trans->sackfreq = params.sack_freq;
2661 trans->param_flags =
2662 (trans->param_flags & ~SPP_SACKDELAY) |
2663 SPP_SACKDELAY_ENABLE;
Frank Filz77086102005-12-22 11:37:30 -08002664 }
2665 }
2666 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002667
Frank Filz77086102005-12-22 11:37:30 -08002668 return 0;
2669}
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2672 *
2673 * Applications can specify protocol parameters for the default association
2674 * initialization. The option name argument to setsockopt() and getsockopt()
2675 * is SCTP_INITMSG.
2676 *
2677 * Setting initialization parameters is effective only on an unconnected
2678 * socket (for UDP-style sockets only future associations are effected
2679 * by the change). With TCP-style sockets, this option is inherited by
2680 * sockets derived from a listener socket.
2681 */
David S. Millerb7058842009-09-30 16:12:20 -07002682static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 struct sctp_initmsg sinit;
2685 struct sctp_sock *sp = sctp_sk(sk);
2686
2687 if (optlen != sizeof(struct sctp_initmsg))
2688 return -EINVAL;
2689 if (copy_from_user(&sinit, optval, optlen))
2690 return -EFAULT;
2691
2692 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002693 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002695 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002697 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002699 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 return 0;
2702}
2703
2704/*
2705 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2706 *
2707 * Applications that wish to use the sendto() system call may wish to
2708 * specify a default set of parameters that would normally be supplied
2709 * through the inclusion of ancillary data. This socket option allows
2710 * such an application to set the default sctp_sndrcvinfo structure.
2711 * The application that wishes to use this socket option simply passes
2712 * in to this call the sctp_sndrcvinfo structure defined in Section
2713 * 5.2.2) The input parameters accepted by this call include
2714 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2715 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2716 * to this call if the caller is using the UDP model.
2717 */
2718static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002719 char __user *optval,
2720 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 struct sctp_sndrcvinfo info;
2723 struct sctp_association *asoc;
2724 struct sctp_sock *sp = sctp_sk(sk);
2725
2726 if (optlen != sizeof(struct sctp_sndrcvinfo))
2727 return -EINVAL;
2728 if (copy_from_user(&info, optval, optlen))
2729 return -EFAULT;
2730
2731 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2732 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2733 return -EINVAL;
2734
2735 if (asoc) {
2736 asoc->default_stream = info.sinfo_stream;
2737 asoc->default_flags = info.sinfo_flags;
2738 asoc->default_ppid = info.sinfo_ppid;
2739 asoc->default_context = info.sinfo_context;
2740 asoc->default_timetolive = info.sinfo_timetolive;
2741 } else {
2742 sp->default_stream = info.sinfo_stream;
2743 sp->default_flags = info.sinfo_flags;
2744 sp->default_ppid = info.sinfo_ppid;
2745 sp->default_context = info.sinfo_context;
2746 sp->default_timetolive = info.sinfo_timetolive;
2747 }
2748
2749 return 0;
2750}
2751
2752/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2753 *
2754 * Requests that the local SCTP stack use the enclosed peer address as
2755 * the association primary. The enclosed address must be one of the
2756 * association peer's addresses.
2757 */
2758static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002759 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct sctp_prim prim;
2762 struct sctp_transport *trans;
2763
2764 if (optlen != sizeof(struct sctp_prim))
2765 return -EINVAL;
2766
2767 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2768 return -EFAULT;
2769
2770 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2771 if (!trans)
2772 return -EINVAL;
2773
2774 sctp_assoc_set_primary(trans->asoc, trans);
2775
2776 return 0;
2777}
2778
2779/*
2780 * 7.1.5 SCTP_NODELAY
2781 *
2782 * Turn on/off any Nagle-like algorithm. This means that packets are
2783 * generally sent as soon as possible and no unnecessary delays are
2784 * introduced, at the cost of more packets in the network. Expects an
2785 * integer boolean flag.
2786 */
2787static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002788 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 int val;
2791
2792 if (optlen < sizeof(int))
2793 return -EINVAL;
2794 if (get_user(val, (int __user *)optval))
2795 return -EFAULT;
2796
2797 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2798 return 0;
2799}
2800
2801/*
2802 *
2803 * 7.1.1 SCTP_RTOINFO
2804 *
2805 * The protocol parameters used to initialize and bound retransmission
2806 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2807 * and modify these parameters.
2808 * All parameters are time values, in milliseconds. A value of 0, when
2809 * modifying the parameters, indicates that the current value should not
2810 * be changed.
2811 *
2812 */
David S. Millerb7058842009-09-30 16:12:20 -07002813static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct sctp_rtoinfo rtoinfo;
2816 struct sctp_association *asoc;
2817
2818 if (optlen != sizeof (struct sctp_rtoinfo))
2819 return -EINVAL;
2820
2821 if (copy_from_user(&rtoinfo, optval, optlen))
2822 return -EFAULT;
2823
2824 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2825
2826 /* Set the values to the specific association */
2827 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2828 return -EINVAL;
2829
2830 if (asoc) {
2831 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002832 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 msecs_to_jiffies(rtoinfo.srto_initial);
2834 if (rtoinfo.srto_max != 0)
2835 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2836 if (rtoinfo.srto_min != 0)
2837 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2838 } else {
2839 /* If there is no association or the association-id = 0
2840 * set the values to the endpoint.
2841 */
2842 struct sctp_sock *sp = sctp_sk(sk);
2843
2844 if (rtoinfo.srto_initial != 0)
2845 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2846 if (rtoinfo.srto_max != 0)
2847 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2848 if (rtoinfo.srto_min != 0)
2849 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2850 }
2851
2852 return 0;
2853}
2854
2855/*
2856 *
2857 * 7.1.2 SCTP_ASSOCINFO
2858 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002859 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * of the association.
2861 * Returns an error if the new association retransmission value is
2862 * greater than the sum of the retransmission value of the peer.
2863 * See [SCTP] for more information.
2864 *
2865 */
David S. Millerb7058842009-09-30 16:12:20 -07002866static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868
2869 struct sctp_assocparams assocparams;
2870 struct sctp_association *asoc;
2871
2872 if (optlen != sizeof(struct sctp_assocparams))
2873 return -EINVAL;
2874 if (copy_from_user(&assocparams, optval, optlen))
2875 return -EFAULT;
2876
2877 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2878
2879 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2880 return -EINVAL;
2881
2882 /* Set the values to the specific association */
2883 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002884 if (assocparams.sasoc_asocmaxrxt != 0) {
2885 __u32 path_sum = 0;
2886 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002887 struct sctp_transport *peer_addr;
2888
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002889 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2890 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002891 path_sum += peer_addr->pathmaxrxt;
2892 paths++;
2893 }
2894
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002895 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002896 * one path/transport. We do this because path
2897 * retransmissions are only counted when we have more
2898 * then one path.
2899 */
2900 if (paths > 1 &&
2901 assocparams.sasoc_asocmaxrxt > path_sum)
2902 return -EINVAL;
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002905 }
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (assocparams.sasoc_cookie_life != 0) {
2908 asoc->cookie_life.tv_sec =
2909 assocparams.sasoc_cookie_life / 1000;
2910 asoc->cookie_life.tv_usec =
2911 (assocparams.sasoc_cookie_life % 1000)
2912 * 1000;
2913 }
2914 } else {
2915 /* Set the values to the endpoint */
2916 struct sctp_sock *sp = sctp_sk(sk);
2917
2918 if (assocparams.sasoc_asocmaxrxt != 0)
2919 sp->assocparams.sasoc_asocmaxrxt =
2920 assocparams.sasoc_asocmaxrxt;
2921 if (assocparams.sasoc_cookie_life != 0)
2922 sp->assocparams.sasoc_cookie_life =
2923 assocparams.sasoc_cookie_life;
2924 }
2925 return 0;
2926}
2927
2928/*
2929 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2930 *
2931 * This socket option is a boolean flag which turns on or off mapped V4
2932 * addresses. If this option is turned on and the socket is type
2933 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2934 * If this option is turned off, then no mapping will be done of V4
2935 * addresses and a user will receive both PF_INET6 and PF_INET type
2936 * addresses on the socket.
2937 */
David S. Millerb7058842009-09-30 16:12:20 -07002938static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 int val;
2941 struct sctp_sock *sp = sctp_sk(sk);
2942
2943 if (optlen < sizeof(int))
2944 return -EINVAL;
2945 if (get_user(val, (int __user *)optval))
2946 return -EFAULT;
2947 if (val)
2948 sp->v4mapped = 1;
2949 else
2950 sp->v4mapped = 0;
2951
2952 return 0;
2953}
2954
2955/*
Wei Yongjune89c2092008-12-25 16:54:58 -08002956 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2957 * This option will get or set the maximum size to put in any outgoing
2958 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 * fragmented by SCTP into the specified size. Note that the underlying
2960 * SCTP implementation may fragment into smaller sized chunks when the
2961 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08002962 * the user. The default value for this option is '0' which indicates
2963 * the user is NOT limiting fragmentation and only the PMTU will effect
2964 * SCTP's choice of DATA chunk size. Note also that values set larger
2965 * than the maximum size of an IP datagram will effectively let SCTP
2966 * control fragmentation (i.e. the same as setting this option to 0).
2967 *
2968 * The following structure is used to access and modify this parameter:
2969 *
2970 * struct sctp_assoc_value {
2971 * sctp_assoc_t assoc_id;
2972 * uint32_t assoc_value;
2973 * };
2974 *
2975 * assoc_id: This parameter is ignored for one-to-one style sockets.
2976 * For one-to-many style sockets this parameter indicates which
2977 * association the user is performing an action upon. Note that if
2978 * this field's value is zero then the endpoints default value is
2979 * changed (effecting future associations only).
2980 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 */
David S. Millerb7058842009-09-30 16:12:20 -07002982static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Wei Yongjune89c2092008-12-25 16:54:58 -08002984 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct sctp_sock *sp = sctp_sk(sk);
2987 int val;
2988
Wei Yongjune89c2092008-12-25 16:54:58 -08002989 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00002990 pr_warn("Use of int in maxseg socket option deprecated\n");
2991 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08002992 if (copy_from_user(&val, optval, optlen))
2993 return -EFAULT;
2994 params.assoc_id = 0;
2995 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2996 if (copy_from_user(&params, optval, optlen))
2997 return -EFAULT;
2998 val = params.assoc_value;
2999 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003001
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07003002 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Wei Yongjune89c2092008-12-25 16:54:58 -08003005 asoc = sctp_id2assoc(sk, params.assoc_id);
3006 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3007 return -EINVAL;
3008
3009 if (asoc) {
3010 if (val == 0) {
3011 val = asoc->pathmtu;
3012 val -= sp->pf->af->net_header_len;
3013 val -= sizeof(struct sctphdr) +
3014 sizeof(struct sctp_data_chunk);
3015 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003016 asoc->user_frag = val;
3017 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003018 } else {
3019 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
3022 return 0;
3023}
3024
3025
3026/*
3027 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3028 *
3029 * Requests that the peer mark the enclosed address as the association
3030 * primary. The enclosed address must be one of the association's
3031 * locally bound addresses. The following structure is used to make a
3032 * set primary request:
3033 */
3034static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003035 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct sctp_association *asoc = NULL;
3039 struct sctp_setpeerprim prim;
3040 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003041 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 int err;
3043
3044 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 if (!sctp_addip_enable)
3047 return -EPERM;
3048
3049 if (optlen != sizeof(struct sctp_setpeerprim))
3050 return -EINVAL;
3051
3052 if (copy_from_user(&prim, optval, optlen))
3053 return -EFAULT;
3054
3055 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003056 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return -EINVAL;
3058
3059 if (!asoc->peer.asconf_capable)
3060 return -EPERM;
3061
3062 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3063 return -EPERM;
3064
3065 if (!sctp_state(asoc, ESTABLISHED))
3066 return -ENOTCONN;
3067
Wei Yongjun40a01032010-12-07 17:11:09 +00003068 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3069 if (!af)
3070 return -EINVAL;
3071
3072 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3073 return -EADDRNOTAVAIL;
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3076 return -EADDRNOTAVAIL;
3077
3078 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3079 chunk = sctp_make_asconf_set_prim(asoc,
3080 (union sctp_addr *)&prim.sspp_addr);
3081 if (!chunk)
3082 return -ENOMEM;
3083
3084 err = sctp_send_asconf(asoc, chunk);
3085
3086 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3087
3088 return err;
3089}
3090
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003091static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003092 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003094 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003096 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003098 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return -EFAULT;
3100
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003101 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 return 0;
3104}
3105
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003106/*
3107 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3108 *
3109 * The context field in the sctp_sndrcvinfo structure is normally only
3110 * used when a failed message is retrieved holding the value that was
3111 * sent down on the actual send call. This option allows the setting of
3112 * a default context on an association basis that will be received on
3113 * reading messages from the peer. This is especially helpful in the
3114 * one-2-many model for an application to keep some reference to an
3115 * internal state machine that is processing messages on the
3116 * association. Note that the setting of this value only effects
3117 * received messages from the peer and does not effect the value that is
3118 * saved with outbound messages.
3119 */
3120static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003121 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003122{
3123 struct sctp_assoc_value params;
3124 struct sctp_sock *sp;
3125 struct sctp_association *asoc;
3126
3127 if (optlen != sizeof(struct sctp_assoc_value))
3128 return -EINVAL;
3129 if (copy_from_user(&params, optval, optlen))
3130 return -EFAULT;
3131
3132 sp = sctp_sk(sk);
3133
3134 if (params.assoc_id != 0) {
3135 asoc = sctp_id2assoc(sk, params.assoc_id);
3136 if (!asoc)
3137 return -EINVAL;
3138 asoc->default_rcv_context = params.assoc_value;
3139 } else {
3140 sp->default_rcv_context = params.assoc_value;
3141 }
3142
3143 return 0;
3144}
3145
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003146/*
3147 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3148 *
3149 * This options will at a minimum specify if the implementation is doing
3150 * fragmented interleave. Fragmented interleave, for a one to many
3151 * socket, is when subsequent calls to receive a message may return
3152 * parts of messages from different associations. Some implementations
3153 * may allow you to turn this value on or off. If so, when turned off,
3154 * no fragment interleave will occur (which will cause a head of line
3155 * blocking amongst multiple associations sharing the same one to many
3156 * socket). When this option is turned on, then each receive call may
3157 * come from a different association (thus the user must receive data
3158 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3159 * association each receive belongs to.
3160 *
3161 * This option takes a boolean value. A non-zero value indicates that
3162 * fragmented interleave is on. A value of zero indicates that
3163 * fragmented interleave is off.
3164 *
3165 * Note that it is important that an implementation that allows this
3166 * option to be turned on, have it off by default. Otherwise an unaware
3167 * application using the one to many model may become confused and act
3168 * incorrectly.
3169 */
3170static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3171 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003172 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003173{
3174 int val;
3175
3176 if (optlen != sizeof(int))
3177 return -EINVAL;
3178 if (get_user(val, (int __user *)optval))
3179 return -EFAULT;
3180
3181 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3182
3183 return 0;
3184}
3185
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003186/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003187 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003188 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003189 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003190 * This option will set or get the SCTP partial delivery point. This
3191 * point is the size of a message where the partial delivery API will be
3192 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003193 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003194 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003195 * point. Note also that the call will fail if the user attempts to set
3196 * this value larger than the socket receive buffer size.
3197 *
3198 * Note that any single message having a length smaller than or equal to
3199 * the SCTP partial delivery point will be delivered in one single read
3200 * call as long as the user provided buffer is large enough to hold the
3201 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003202 */
3203static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3204 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003205 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003206{
3207 u32 val;
3208
3209 if (optlen != sizeof(u32))
3210 return -EINVAL;
3211 if (get_user(val, (int __user *)optval))
3212 return -EFAULT;
3213
Wei Yongjun8510b932008-12-25 16:59:03 -08003214 /* Note: We double the receive buffer from what the user sets
3215 * it to be, also initial rwnd is based on rcvbuf/2.
3216 */
3217 if (val > (sk->sk_rcvbuf >> 1))
3218 return -EINVAL;
3219
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003220 sctp_sk(sk)->pd_point = val;
3221
3222 return 0; /* is this the right error code? */
3223}
3224
Vlad Yasevich70331572007-03-23 11:34:36 -07003225/*
3226 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3227 *
3228 * This option will allow a user to change the maximum burst of packets
3229 * that can be emitted by this association. Note that the default value
3230 * is 4, and some implementations may restrict this setting so that it
3231 * can only be lowered.
3232 *
3233 * NOTE: This text doesn't seem right. Do this on a socket basis with
3234 * future associations inheriting the socket value.
3235 */
3236static int sctp_setsockopt_maxburst(struct sock *sk,
3237 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003238 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003239{
Neil Horman219b99a2008-03-05 13:44:46 -08003240 struct sctp_assoc_value params;
3241 struct sctp_sock *sp;
3242 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003243 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003244 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003245
Neil Horman219b99a2008-03-05 13:44:46 -08003246 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00003247 pr_warn("Use of int in max_burst socket option deprecated\n");
3248 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08003249 if (copy_from_user(&val, optval, optlen))
3250 return -EFAULT;
3251 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3252 if (copy_from_user(&params, optval, optlen))
3253 return -EFAULT;
3254 val = params.assoc_value;
3255 assoc_id = params.assoc_id;
3256 } else
3257 return -EINVAL;
3258
3259 sp = sctp_sk(sk);
3260
3261 if (assoc_id != 0) {
3262 asoc = sctp_id2assoc(sk, assoc_id);
3263 if (!asoc)
3264 return -EINVAL;
3265 asoc->max_burst = val;
3266 } else
3267 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003268
3269 return 0;
3270}
3271
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003272/*
3273 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3274 *
3275 * This set option adds a chunk type that the user is requesting to be
3276 * received only in an authenticated way. Changes to the list of chunks
3277 * will only effect future associations on the socket.
3278 */
3279static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003280 char __user *optval,
3281 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003282{
3283 struct sctp_authchunk val;
3284
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003285 if (!sctp_auth_enable)
3286 return -EACCES;
3287
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003288 if (optlen != sizeof(struct sctp_authchunk))
3289 return -EINVAL;
3290 if (copy_from_user(&val, optval, optlen))
3291 return -EFAULT;
3292
3293 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003294 case SCTP_CID_INIT:
3295 case SCTP_CID_INIT_ACK:
3296 case SCTP_CID_SHUTDOWN_COMPLETE:
3297 case SCTP_CID_AUTH:
3298 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003299 }
3300
3301 /* add this chunk id to the endpoint */
3302 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3303}
3304
3305/*
3306 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3307 *
3308 * This option gets or sets the list of HMAC algorithms that the local
3309 * endpoint requires the peer to use.
3310 */
3311static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003312 char __user *optval,
3313 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003314{
3315 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003316 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003317 int err;
3318
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003319 if (!sctp_auth_enable)
3320 return -EACCES;
3321
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003322 if (optlen < sizeof(struct sctp_hmacalgo))
3323 return -EINVAL;
3324
Shan Wei934253a2011-04-18 19:13:18 +00003325 hmacs= memdup_user(optval, optlen);
3326 if (IS_ERR(hmacs))
3327 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003328
Vlad Yasevichd9724052008-08-27 16:09:49 -07003329 idents = hmacs->shmac_num_idents;
3330 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3331 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003332 err = -EINVAL;
3333 goto out;
3334 }
3335
3336 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3337out:
3338 kfree(hmacs);
3339 return err;
3340}
3341
3342/*
3343 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3344 *
3345 * This option will set a shared secret key which is used to build an
3346 * association shared key.
3347 */
3348static int sctp_setsockopt_auth_key(struct sock *sk,
3349 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003350 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003351{
3352 struct sctp_authkey *authkey;
3353 struct sctp_association *asoc;
3354 int ret;
3355
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003356 if (!sctp_auth_enable)
3357 return -EACCES;
3358
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003359 if (optlen <= sizeof(struct sctp_authkey))
3360 return -EINVAL;
3361
Shan Wei934253a2011-04-18 19:13:18 +00003362 authkey= memdup_user(optval, optlen);
3363 if (IS_ERR(authkey))
3364 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003365
Vlad Yasevich328fc472008-08-27 16:08:54 -07003366 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003367 ret = -EINVAL;
3368 goto out;
3369 }
3370
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003371 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3372 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3373 ret = -EINVAL;
3374 goto out;
3375 }
3376
3377 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3378out:
3379 kfree(authkey);
3380 return ret;
3381}
3382
3383/*
3384 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3385 *
3386 * This option will get or set the active shared key to be used to build
3387 * the association shared key.
3388 */
3389static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003390 char __user *optval,
3391 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003392{
3393 struct sctp_authkeyid val;
3394 struct sctp_association *asoc;
3395
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003396 if (!sctp_auth_enable)
3397 return -EACCES;
3398
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003399 if (optlen != sizeof(struct sctp_authkeyid))
3400 return -EINVAL;
3401 if (copy_from_user(&val, optval, optlen))
3402 return -EFAULT;
3403
3404 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3405 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3406 return -EINVAL;
3407
3408 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3409 val.scact_keynumber);
3410}
3411
3412/*
3413 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3414 *
3415 * This set option will delete a shared secret key from use.
3416 */
3417static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003418 char __user *optval,
3419 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003420{
3421 struct sctp_authkeyid val;
3422 struct sctp_association *asoc;
3423
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003424 if (!sctp_auth_enable)
3425 return -EACCES;
3426
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003427 if (optlen != sizeof(struct sctp_authkeyid))
3428 return -EINVAL;
3429 if (copy_from_user(&val, optval, optlen))
3430 return -EFAULT;
3431
3432 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3433 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3434 return -EINVAL;
3435
3436 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3437 val.scact_keynumber);
3438
3439}
3440
Michio Honda7dc04d72011-04-26 20:16:31 +09003441/*
3442 * 8.1.23 SCTP_AUTO_ASCONF
3443 *
3444 * This option will enable or disable the use of the automatic generation of
3445 * ASCONF chunks to add and delete addresses to an existing association. Note
3446 * that this option has two caveats namely: a) it only affects sockets that
3447 * are bound to all addresses available to the SCTP stack, and b) the system
3448 * administrator may have an overriding control that turns the ASCONF feature
3449 * off no matter what setting the socket option may have.
3450 * This option expects an integer boolean flag, where a non-zero value turns on
3451 * the option, and a zero value turns off the option.
3452 * Note. In this implementation, socket operation overrides default parameter
3453 * being set by sysctl as well as FreeBSD implementation
3454 */
3455static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3456 unsigned int optlen)
3457{
3458 int val;
3459 struct sctp_sock *sp = sctp_sk(sk);
3460
3461 if (optlen < sizeof(int))
3462 return -EINVAL;
3463 if (get_user(val, (int __user *)optval))
3464 return -EFAULT;
3465 if (!sctp_is_ep_boundall(sk) && val)
3466 return -EINVAL;
3467 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3468 return 0;
3469
3470 if (val == 0 && sp->do_auto_asconf) {
3471 list_del(&sp->auto_asconf_list);
3472 sp->do_auto_asconf = 0;
3473 } else if (val && !sp->do_auto_asconf) {
3474 list_add_tail(&sp->auto_asconf_list,
3475 &sctp_auto_asconf_splist);
3476 sp->do_auto_asconf = 1;
3477 }
3478 return 0;
3479}
3480
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003481
Neil Horman5aa93bc2012-07-21 07:56:07 +00003482/*
3483 * SCTP_PEER_ADDR_THLDS
3484 *
3485 * This option allows us to alter the partially failed threshold for one or all
3486 * transports in an association. See Section 6.1 of:
3487 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3488 */
3489static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3490 char __user *optval,
3491 unsigned int optlen)
3492{
3493 struct sctp_paddrthlds val;
3494 struct sctp_transport *trans;
3495 struct sctp_association *asoc;
3496
3497 if (optlen < sizeof(struct sctp_paddrthlds))
3498 return -EINVAL;
3499 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3500 sizeof(struct sctp_paddrthlds)))
3501 return -EFAULT;
3502
3503
3504 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3505 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3506 if (!asoc)
3507 return -ENOENT;
3508 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3509 transports) {
3510 if (val.spt_pathmaxrxt)
3511 trans->pathmaxrxt = val.spt_pathmaxrxt;
3512 trans->pf_retrans = val.spt_pathpfthld;
3513 }
3514
3515 if (val.spt_pathmaxrxt)
3516 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3517 asoc->pf_retrans = val.spt_pathpfthld;
3518 } else {
3519 trans = sctp_addr_id2transport(sk, &val.spt_address,
3520 val.spt_assoc_id);
3521 if (!trans)
3522 return -ENOENT;
3523
3524 if (val.spt_pathmaxrxt)
3525 trans->pathmaxrxt = val.spt_pathmaxrxt;
3526 trans->pf_retrans = val.spt_pathpfthld;
3527 }
3528
3529 return 0;
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/* API 6.2 setsockopt(), getsockopt()
3533 *
3534 * Applications use setsockopt() and getsockopt() to set or retrieve
3535 * socket options. Socket options are used to change the default
3536 * behavior of sockets calls. They are described in Section 7.
3537 *
3538 * The syntax is:
3539 *
3540 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3541 * int __user *optlen);
3542 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3543 * int optlen);
3544 *
3545 * sd - the socket descript.
3546 * level - set to IPPROTO_SCTP for all SCTP options.
3547 * optname - the option name.
3548 * optval - the buffer to store the value of the option.
3549 * optlen - the size of the buffer.
3550 */
3551SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003552 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 int retval = 0;
3555
3556 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3557 sk, optname);
3558
3559 /* I can hardly begin to describe how wrong this is. This is
3560 * so broken as to be worse than useless. The API draft
3561 * REALLY is NOT helpful here... I am not convinced that the
3562 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3563 * are at all well-founded.
3564 */
3565 if (level != SOL_SCTP) {
3566 struct sctp_af *af = sctp_sk(sk)->pf->af;
3567 retval = af->setsockopt(sk, level, optname, optval, optlen);
3568 goto out_nounlock;
3569 }
3570
3571 sctp_lock_sock(sk);
3572
3573 switch (optname) {
3574 case SCTP_SOCKOPT_BINDX_ADD:
3575 /* 'optlen' is the size of the addresses buffer. */
3576 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3577 optlen, SCTP_BINDX_ADD_ADDR);
3578 break;
3579
3580 case SCTP_SOCKOPT_BINDX_REM:
3581 /* 'optlen' is the size of the addresses buffer. */
3582 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3583 optlen, SCTP_BINDX_REM_ADDR);
3584 break;
3585
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003586 case SCTP_SOCKOPT_CONNECTX_OLD:
3587 /* 'optlen' is the size of the addresses buffer. */
3588 retval = sctp_setsockopt_connectx_old(sk,
3589 (struct sockaddr __user *)optval,
3590 optlen);
3591 break;
3592
Frank Filz3f7a87d2005-06-20 13:14:57 -07003593 case SCTP_SOCKOPT_CONNECTX:
3594 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003595 retval = sctp_setsockopt_connectx(sk,
3596 (struct sockaddr __user *)optval,
3597 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003598 break;
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 case SCTP_DISABLE_FRAGMENTS:
3601 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3602 break;
3603
3604 case SCTP_EVENTS:
3605 retval = sctp_setsockopt_events(sk, optval, optlen);
3606 break;
3607
3608 case SCTP_AUTOCLOSE:
3609 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3610 break;
3611
3612 case SCTP_PEER_ADDR_PARAMS:
3613 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3614 break;
3615
Shan Wei4580ccc2011-01-18 22:39:00 +00003616 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003617 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003618 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003619 case SCTP_PARTIAL_DELIVERY_POINT:
3620 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3621 break;
Frank Filz77086102005-12-22 11:37:30 -08003622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 case SCTP_INITMSG:
3624 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3625 break;
3626 case SCTP_DEFAULT_SEND_PARAM:
3627 retval = sctp_setsockopt_default_send_param(sk, optval,
3628 optlen);
3629 break;
3630 case SCTP_PRIMARY_ADDR:
3631 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3632 break;
3633 case SCTP_SET_PEER_PRIMARY_ADDR:
3634 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3635 break;
3636 case SCTP_NODELAY:
3637 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3638 break;
3639 case SCTP_RTOINFO:
3640 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3641 break;
3642 case SCTP_ASSOCINFO:
3643 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3644 break;
3645 case SCTP_I_WANT_MAPPED_V4_ADDR:
3646 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3647 break;
3648 case SCTP_MAXSEG:
3649 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3650 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003651 case SCTP_ADAPTATION_LAYER:
3652 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003654 case SCTP_CONTEXT:
3655 retval = sctp_setsockopt_context(sk, optval, optlen);
3656 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003657 case SCTP_FRAGMENT_INTERLEAVE:
3658 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3659 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003660 case SCTP_MAX_BURST:
3661 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3662 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003663 case SCTP_AUTH_CHUNK:
3664 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3665 break;
3666 case SCTP_HMAC_IDENT:
3667 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3668 break;
3669 case SCTP_AUTH_KEY:
3670 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3671 break;
3672 case SCTP_AUTH_ACTIVE_KEY:
3673 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3674 break;
3675 case SCTP_AUTH_DELETE_KEY:
3676 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3677 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003678 case SCTP_AUTO_ASCONF:
3679 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3680 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00003681 case SCTP_PEER_ADDR_THLDS:
3682 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 default:
3685 retval = -ENOPROTOOPT;
3686 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 sctp_release_sock(sk);
3690
3691out_nounlock:
3692 return retval;
3693}
3694
3695/* API 3.1.6 connect() - UDP Style Syntax
3696 *
3697 * An application may use the connect() call in the UDP model to initiate an
3698 * association without sending data.
3699 *
3700 * The syntax is:
3701 *
3702 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3703 *
3704 * sd: the socket descriptor to have a new association added to.
3705 *
3706 * nam: the address structure (either struct sockaddr_in or struct
3707 * sockaddr_in6 defined in RFC2553 [7]).
3708 *
3709 * len: the size of the address.
3710 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003711SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 int addr_len)
3713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003715 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 sctp_lock_sock(sk);
3718
Frank Filz3f7a87d2005-06-20 13:14:57 -07003719 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08003720 __func__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Frank Filz3f7a87d2005-06-20 13:14:57 -07003722 /* Validate addr_len before calling common connect/connectx routine. */
3723 af = sctp_get_af_specific(addr->sa_family);
3724 if (!af || addr_len < af->sockaddr_len) {
3725 err = -EINVAL;
3726 } else {
3727 /* Pass correct addr len to common routine (so it knows there
3728 * is only one address being passed.
3729 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003730 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 }
3732
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 return err;
3735}
3736
3737/* FIXME: Write comments. */
3738SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3739{
3740 return -EOPNOTSUPP; /* STUB */
3741}
3742
3743/* 4.1.4 accept() - TCP Style Syntax
3744 *
3745 * Applications use accept() call to remove an established SCTP
3746 * association from the accept queue of the endpoint. A new socket
3747 * descriptor will be returned from accept() to represent the newly
3748 * formed association.
3749 */
3750SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3751{
3752 struct sctp_sock *sp;
3753 struct sctp_endpoint *ep;
3754 struct sock *newsk = NULL;
3755 struct sctp_association *asoc;
3756 long timeo;
3757 int error = 0;
3758
3759 sctp_lock_sock(sk);
3760
3761 sp = sctp_sk(sk);
3762 ep = sp->ep;
3763
3764 if (!sctp_style(sk, TCP)) {
3765 error = -EOPNOTSUPP;
3766 goto out;
3767 }
3768
3769 if (!sctp_sstate(sk, LISTENING)) {
3770 error = -EINVAL;
3771 goto out;
3772 }
3773
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003774 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 error = sctp_wait_for_accept(sk, timeo);
3777 if (error)
3778 goto out;
3779
3780 /* We treat the list of associations on the endpoint as the accept
3781 * queue and pick the first association on the list.
3782 */
3783 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3784
3785 newsk = sp->pf->create_accept_sk(sk, asoc);
3786 if (!newsk) {
3787 error = -ENOMEM;
3788 goto out;
3789 }
3790
3791 /* Populate the fields of the newsk from the oldsk and migrate the
3792 * asoc to the newsk.
3793 */
3794 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3795
3796out:
3797 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003798 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return newsk;
3800}
3801
3802/* The SCTP ioctl handler. */
3803SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3804{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003805 int rc = -ENOTCONN;
3806
3807 sctp_lock_sock(sk);
3808
3809 /*
3810 * SEQPACKET-style sockets in LISTENING state are valid, for
3811 * SCTP, so only discard TCP-style sockets in LISTENING state.
3812 */
3813 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3814 goto out;
3815
3816 switch (cmd) {
3817 case SIOCINQ: {
3818 struct sk_buff *skb;
3819 unsigned int amount = 0;
3820
3821 skb = skb_peek(&sk->sk_receive_queue);
3822 if (skb != NULL) {
3823 /*
3824 * We will only return the amount of this packet since
3825 * that is all that will be read.
3826 */
3827 amount = skb->len;
3828 }
3829 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003830 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07003831 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003832 default:
3833 rc = -ENOIOCTLCMD;
3834 break;
3835 }
3836out:
3837 sctp_release_sock(sk);
3838 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
3841/* This is the function which gets called during socket creation to
3842 * initialized the SCTP-specific portion of the sock.
3843 * The sock structure should already be zero-filled memory.
3844 */
3845SCTP_STATIC int sctp_init_sock(struct sock *sk)
3846{
3847 struct sctp_endpoint *ep;
3848 struct sctp_sock *sp;
3849
3850 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3851
3852 sp = sctp_sk(sk);
3853
3854 /* Initialize the SCTP per socket area. */
3855 switch (sk->sk_type) {
3856 case SOCK_SEQPACKET:
3857 sp->type = SCTP_SOCKET_UDP;
3858 break;
3859 case SOCK_STREAM:
3860 sp->type = SCTP_SOCKET_TCP;
3861 break;
3862 default:
3863 return -ESOCKTNOSUPPORT;
3864 }
3865
3866 /* Initialize default send parameters. These parameters can be
3867 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3868 */
3869 sp->default_stream = 0;
3870 sp->default_ppid = 0;
3871 sp->default_flags = 0;
3872 sp->default_context = 0;
3873 sp->default_timetolive = 0;
3874
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003875 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003876 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* Initialize default setup parameters. These parameters
3879 * can be modified with the SCTP_INITMSG socket option or
3880 * overridden by the SCTP_INIT CMSG.
3881 */
3882 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3883 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3884 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003885 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 /* Initialize default RTO related parameters. These parameters can
3888 * be modified for with the SCTP_RTOINFO socket option.
3889 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003890 sp->rtoinfo.srto_initial = sctp_rto_initial;
3891 sp->rtoinfo.srto_max = sctp_rto_max;
3892 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 /* Initialize default association related parameters. These parameters
3895 * can be modified with the SCTP_ASSOCINFO socket option.
3896 */
3897 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3898 sp->assocparams.sasoc_number_peer_destinations = 0;
3899 sp->assocparams.sasoc_peer_rwnd = 0;
3900 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003901 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003904 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 */
3906 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3907
3908 /* Default Peer Address Parameters. These defaults can
3909 * be modified via SCTP_PEER_ADDR_PARAMS
3910 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003911 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003912 sp->pathmaxrxt = sctp_max_retrans_path;
3913 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003914 sp->sackdelay = sctp_sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07003915 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003916 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003917 SPP_PMTUD_ENABLE |
3918 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
3920 /* If enabled no SCTP message fragmentation will be performed.
3921 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3922 */
3923 sp->disable_fragments = 0;
3924
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003925 /* Enable Nagle algorithm by default. */
3926 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 /* Enable by default. */
3929 sp->v4mapped = 1;
3930
3931 /* Auto-close idle associations after the configured
3932 * number of seconds. A value of 0 disables this
3933 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3934 * for UDP-style sockets only.
3935 */
3936 sp->autoclose = 0;
3937
3938 /* User specified fragmentation limit. */
3939 sp->user_frag = 0;
3940
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003941 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 sp->pf = sctp_get_pf_specific(sk->sk_family);
3944
3945 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003946 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003948 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950 /* Create a per socket endpoint structure. Even if we
3951 * change the data structure relationships, this may still
3952 * be useful for storing pre-connect address information.
3953 */
3954 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3955 if (!ep)
3956 return -ENOMEM;
3957
3958 sp->ep = ep;
3959 sp->hmac = NULL;
3960
3961 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08003962
3963 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003964 percpu_counter_inc(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003965 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Michio Honda9f7d6532011-04-26 19:32:51 +09003966 if (sctp_default_auto_asconf) {
3967 list_add_tail(&sp->auto_asconf_list,
3968 &sctp_auto_asconf_splist);
3969 sp->do_auto_asconf = 1;
3970 } else
3971 sp->do_auto_asconf = 0;
David S. Miller6f756a82008-11-23 17:34:03 -08003972 local_bh_enable();
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 return 0;
3975}
3976
3977/* Cleanup any SCTP per socket resources. */
Brian Haley7d06b2e2008-06-14 17:04:49 -07003978SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Michio Honda9f7d6532011-04-26 19:32:51 +09003980 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3983
3984 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09003985 sp = sctp_sk(sk);
3986 if (sp->do_auto_asconf) {
3987 sp->do_auto_asconf = 0;
3988 list_del(&sp->auto_asconf_list);
3989 }
3990 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003991 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003992 percpu_counter_dec(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003993 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003994 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
3997/* API 4.1.7 shutdown() - TCP Style Syntax
3998 * int shutdown(int socket, int how);
3999 *
4000 * sd - the socket descriptor of the association to be closed.
4001 * how - Specifies the type of shutdown. The values are
4002 * as follows:
4003 * SHUT_RD
4004 * Disables further receive operations. No SCTP
4005 * protocol action is taken.
4006 * SHUT_WR
4007 * Disables further send operations, and initiates
4008 * the SCTP shutdown sequence.
4009 * SHUT_RDWR
4010 * Disables further send and receive operations
4011 * and initiates the SCTP shutdown sequence.
4012 */
4013SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4014{
4015 struct sctp_endpoint *ep;
4016 struct sctp_association *asoc;
4017
4018 if (!sctp_style(sk, TCP))
4019 return;
4020
4021 if (how & SEND_SHUTDOWN) {
4022 ep = sctp_sk(sk)->ep;
4023 if (!list_empty(&ep->asocs)) {
4024 asoc = list_entry(ep->asocs.next,
4025 struct sctp_association, asocs);
4026 sctp_primitive_SHUTDOWN(asoc, NULL);
4027 }
4028 }
4029}
4030
4031/* 7.2.1 Association Status (SCTP_STATUS)
4032
4033 * Applications can retrieve current status information about an
4034 * association, including association state, peer receiver window size,
4035 * number of unacked data chunks, and number of data chunks pending
4036 * receipt. This information is read-only.
4037 */
4038static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4039 char __user *optval,
4040 int __user *optlen)
4041{
4042 struct sctp_status status;
4043 struct sctp_association *asoc = NULL;
4044 struct sctp_transport *transport;
4045 sctp_assoc_t associd;
4046 int retval = 0;
4047
Neil Horman408f22e2007-06-16 14:03:45 -04004048 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 retval = -EINVAL;
4050 goto out;
4051 }
4052
Neil Horman408f22e2007-06-16 14:03:45 -04004053 len = sizeof(status);
4054 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 retval = -EFAULT;
4056 goto out;
4057 }
4058
4059 associd = status.sstat_assoc_id;
4060 asoc = sctp_id2assoc(sk, associd);
4061 if (!asoc) {
4062 retval = -EINVAL;
4063 goto out;
4064 }
4065
4066 transport = asoc->peer.primary_path;
4067
4068 status.sstat_assoc_id = sctp_assoc2id(asoc);
4069 status.sstat_state = asoc->state;
4070 status.sstat_rwnd = asoc->peer.rwnd;
4071 status.sstat_unackdata = asoc->unack_data;
4072
4073 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4074 status.sstat_instrms = asoc->c.sinit_max_instreams;
4075 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4076 status.sstat_fragmentation_point = asoc->frag_point;
4077 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004078 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4079 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 /* Map ipv4 address into v4-mapped-on-v6 address. */
4081 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4082 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004083 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4085 status.sstat_primary.spinfo_srtt = transport->srtt;
4086 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004087 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Frank Filz3f7a87d2005-06-20 13:14:57 -07004089 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4090 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (put_user(len, optlen)) {
4093 retval = -EFAULT;
4094 goto out;
4095 }
4096
4097 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4098 len, status.sstat_state, status.sstat_rwnd,
4099 status.sstat_assoc_id);
4100
4101 if (copy_to_user(optval, &status, len)) {
4102 retval = -EFAULT;
4103 goto out;
4104 }
4105
4106out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004107 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
4110
4111/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4112 *
4113 * Applications can retrieve information about a specific peer address
4114 * of an association, including its reachability state, congestion
4115 * window, and retransmission timer values. This information is
4116 * read-only.
4117 */
4118static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4119 char __user *optval,
4120 int __user *optlen)
4121{
4122 struct sctp_paddrinfo pinfo;
4123 struct sctp_transport *transport;
4124 int retval = 0;
4125
Neil Horman408f22e2007-06-16 14:03:45 -04004126 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 retval = -EINVAL;
4128 goto out;
4129 }
4130
Neil Horman408f22e2007-06-16 14:03:45 -04004131 len = sizeof(pinfo);
4132 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 retval = -EFAULT;
4134 goto out;
4135 }
4136
4137 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4138 pinfo.spinfo_assoc_id);
4139 if (!transport)
4140 return -EINVAL;
4141
4142 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004143 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 pinfo.spinfo_cwnd = transport->cwnd;
4145 pinfo.spinfo_srtt = transport->srtt;
4146 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004147 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Frank Filz3f7a87d2005-06-20 13:14:57 -07004149 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4150 pinfo.spinfo_state = SCTP_ACTIVE;
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (put_user(len, optlen)) {
4153 retval = -EFAULT;
4154 goto out;
4155 }
4156
4157 if (copy_to_user(optval, &pinfo, len)) {
4158 retval = -EFAULT;
4159 goto out;
4160 }
4161
4162out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004163 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4167 *
4168 * This option is a on/off flag. If enabled no SCTP message
4169 * fragmentation will be performed. Instead if a message being sent
4170 * exceeds the current PMTU size, the message will NOT be sent and
4171 * instead a error will be indicated to the user.
4172 */
4173static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4174 char __user *optval, int __user *optlen)
4175{
4176 int val;
4177
4178 if (len < sizeof(int))
4179 return -EINVAL;
4180
4181 len = sizeof(int);
4182 val = (sctp_sk(sk)->disable_fragments == 1);
4183 if (put_user(len, optlen))
4184 return -EFAULT;
4185 if (copy_to_user(optval, &val, len))
4186 return -EFAULT;
4187 return 0;
4188}
4189
4190/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4191 *
4192 * This socket option is used to specify various notifications and
4193 * ancillary data the user wishes to receive.
4194 */
4195static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4196 int __user *optlen)
4197{
Thomas Grafacdd5982012-04-03 22:17:53 +00004198 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004200 if (len > sizeof(struct sctp_event_subscribe))
4201 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004202 if (put_user(len, optlen))
4203 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4205 return -EFAULT;
4206 return 0;
4207}
4208
4209/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4210 *
4211 * This socket option is applicable to the UDP-style socket only. When
4212 * set it will cause associations that are idle for more than the
4213 * specified number of seconds to automatically close. An association
4214 * being idle is defined an association that has NOT sent or received
4215 * user data. The special value of '0' indicates that no automatic
4216 * close of any associations should be performed. The option expects an
4217 * integer defining the number of seconds of idle time before an
4218 * association is closed.
4219 */
4220static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4221{
4222 /* Applicable to UDP-style socket only */
4223 if (sctp_style(sk, TCP))
4224 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004225 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004227 len = sizeof(int);
4228 if (put_user(len, optlen))
4229 return -EFAULT;
4230 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return -EFAULT;
4232 return 0;
4233}
4234
4235/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004236int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004238 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 struct socket *sock;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004240 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 int err = 0;
4242
Benjamin Poirier0343c552012-03-08 05:55:58 +00004243 if (!asoc)
4244 return -EINVAL;
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 /* An association cannot be branched off from an already peeled-off
4247 * socket, nor is this supported for tcp style sockets.
4248 */
4249 if (!sctp_style(sk, UDP))
4250 return -EINVAL;
4251
4252 /* Create a new socket. */
4253 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4254 if (err < 0)
4255 return err;
4256
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004257 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004258
4259 /* Make peeled-off sockets more like 1-1 accepted sockets.
4260 * Set the daddr and initialize id to something more random
4261 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004262 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4263 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004264
4265 /* Populate the fields of the newsk from the oldsk and migrate the
4266 * asoc to the newsk.
4267 */
4268 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 *sockp = sock;
4271
4272 return err;
4273}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004274EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4277{
4278 sctp_peeloff_arg_t peeloff;
4279 struct socket *newsock;
Al Viro56b31d12012-08-18 00:25:51 -04004280 struct file *newfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Neil Horman408f22e2007-06-16 14:03:45 -04004283 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004285 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (copy_from_user(&peeloff, optval, len))
4287 return -EFAULT;
4288
Benjamin Poirier0343c552012-03-08 05:55:58 +00004289 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (retval < 0)
4291 goto out;
4292
4293 /* Map the socket to an unused fd that can be returned to the user. */
Al Viro56b31d12012-08-18 00:25:51 -04004294 retval = get_unused_fd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (retval < 0) {
4296 sock_release(newsock);
4297 goto out;
4298 }
4299
Al Viro56b31d12012-08-18 00:25:51 -04004300 newfile = sock_alloc_file(newsock, 0);
4301 if (unlikely(IS_ERR(newfile))) {
4302 put_unused_fd(retval);
4303 sock_release(newsock);
4304 return PTR_ERR(newfile);
4305 }
4306
Benjamin Poirier0343c552012-03-08 05:55:58 +00004307 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4308 __func__, sk, newsock->sk, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04004311 if (put_user(len, optlen)) {
4312 fput(newfile);
4313 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04004314 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04004315 }
4316 peeloff.sd = retval;
4317 if (copy_to_user(optval, &peeloff, len)) {
4318 fput(newfile);
4319 put_unused_fd(retval);
4320 return -EFAULT;
4321 }
4322 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323out:
4324 return retval;
4325}
4326
4327/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4328 *
4329 * Applications can enable or disable heartbeats for any peer address of
4330 * an association, modify an address's heartbeat interval, force a
4331 * heartbeat to be sent immediately, and adjust the address's maximum
4332 * number of retransmissions sent before an address is considered
4333 * unreachable. The following structure is used to access and modify an
4334 * address's parameters:
4335 *
4336 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004337 * sctp_assoc_t spp_assoc_id;
4338 * struct sockaddr_storage spp_address;
4339 * uint32_t spp_hbinterval;
4340 * uint16_t spp_pathmaxrxt;
4341 * uint32_t spp_pathmtu;
4342 * uint32_t spp_sackdelay;
4343 * uint32_t spp_flags;
4344 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004346 * spp_assoc_id - (one-to-many style socket) This is filled in the
4347 * application, and identifies the association for
4348 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 * spp_address - This specifies which address is of interest.
4350 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004351 * in milliseconds. If a value of zero
4352 * is present in this field then no changes are to
4353 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 * spp_pathmaxrxt - This contains the maximum number of
4355 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004356 * considered unreachable. If a value of zero
4357 * is present in this field then no changes are to
4358 * be made to this parameter.
4359 * spp_pathmtu - When Path MTU discovery is disabled the value
4360 * specified here will be the "fixed" path mtu.
4361 * Note that if the spp_address field is empty
4362 * then all associations on this address will
4363 * have this fixed path mtu set upon them.
4364 *
4365 * spp_sackdelay - When delayed sack is enabled, this value specifies
4366 * the number of milliseconds that sacks will be delayed
4367 * for. This value will apply to all addresses of an
4368 * association if the spp_address field is empty. Note
4369 * also, that if delayed sack is enabled and this
4370 * value is set to 0, no change is made to the last
4371 * recorded delayed sack timer value.
4372 *
4373 * spp_flags - These flags are used to control various features
4374 * on an association. The flag field may contain
4375 * zero or more of the following options.
4376 *
4377 * SPP_HB_ENABLE - Enable heartbeats on the
4378 * specified address. Note that if the address
4379 * field is empty all addresses for the association
4380 * have heartbeats enabled upon them.
4381 *
4382 * SPP_HB_DISABLE - Disable heartbeats on the
4383 * speicifed address. Note that if the address
4384 * field is empty all addresses for the association
4385 * will have their heartbeats disabled. Note also
4386 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4387 * mutually exclusive, only one of these two should
4388 * be specified. Enabling both fields will have
4389 * undetermined results.
4390 *
4391 * SPP_HB_DEMAND - Request a user initiated heartbeat
4392 * to be made immediately.
4393 *
4394 * SPP_PMTUD_ENABLE - This field will enable PMTU
4395 * discovery upon the specified address. Note that
4396 * if the address feild is empty then all addresses
4397 * on the association are effected.
4398 *
4399 * SPP_PMTUD_DISABLE - This field will disable PMTU
4400 * discovery upon the specified address. Note that
4401 * if the address feild is empty then all addresses
4402 * on the association are effected. Not also that
4403 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4404 * exclusive. Enabling both will have undetermined
4405 * results.
4406 *
4407 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4408 * on delayed sack. The time specified in spp_sackdelay
4409 * is used to specify the sack delay for this address. Note
4410 * that if spp_address is empty then all addresses will
4411 * enable delayed sack and take on the sack delay
4412 * value specified in spp_sackdelay.
4413 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4414 * off delayed sack. If the spp_address field is blank then
4415 * delayed sack is disabled for the entire association. Note
4416 * also that this field is mutually exclusive to
4417 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4418 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 */
4420static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004421 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004423 struct sctp_paddrparams params;
4424 struct sctp_transport *trans = NULL;
4425 struct sctp_association *asoc = NULL;
4426 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Neil Horman408f22e2007-06-16 14:03:45 -04004428 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004430 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 if (copy_from_user(&params, optval, len))
4432 return -EFAULT;
4433
Frank Filz52ccb8e2005-12-22 11:36:46 -08004434 /* If an address other than INADDR_ANY is specified, and
4435 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004437 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004438 trans = sctp_addr_id2transport(sk, &params.spp_address,
4439 params.spp_assoc_id);
4440 if (!trans) {
4441 SCTP_DEBUG_PRINTK("Failed no transport\n");
4442 return -EINVAL;
4443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
4445
Frank Filz52ccb8e2005-12-22 11:36:46 -08004446 /* Get association, if assoc_id != 0 and the socket is a one
4447 * to many style socket, and an association was not found, then
4448 * the id was invalid.
4449 */
4450 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4451 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4452 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Frank Filz52ccb8e2005-12-22 11:36:46 -08004456 if (trans) {
4457 /* Fetch transport values. */
4458 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4459 params.spp_pathmtu = trans->pathmtu;
4460 params.spp_pathmaxrxt = trans->pathmaxrxt;
4461 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Frank Filz52ccb8e2005-12-22 11:36:46 -08004463 /*draft-11 doesn't say what to return in spp_flags*/
4464 params.spp_flags = trans->param_flags;
4465 } else if (asoc) {
4466 /* Fetch association values. */
4467 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4468 params.spp_pathmtu = asoc->pathmtu;
4469 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4470 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Frank Filz52ccb8e2005-12-22 11:36:46 -08004472 /*draft-11 doesn't say what to return in spp_flags*/
4473 params.spp_flags = asoc->param_flags;
4474 } else {
4475 /* Fetch socket values. */
4476 params.spp_hbinterval = sp->hbinterval;
4477 params.spp_pathmtu = sp->pathmtu;
4478 params.spp_sackdelay = sp->sackdelay;
4479 params.spp_pathmaxrxt = sp->pathmaxrxt;
4480
4481 /*draft-11 doesn't say what to return in spp_flags*/
4482 params.spp_flags = sp->param_flags;
4483 }
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 if (copy_to_user(optval, &params, len))
4486 return -EFAULT;
4487
4488 if (put_user(len, optlen))
4489 return -EFAULT;
4490
4491 return 0;
4492}
4493
Wei Yongjund364d922008-05-09 15:13:26 -07004494/*
4495 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08004496 *
Wei Yongjund364d922008-05-09 15:13:26 -07004497 * This option will effect the way delayed acks are performed. This
4498 * option allows you to get or set the delayed ack time, in
4499 * milliseconds. It also allows changing the delayed ack frequency.
4500 * Changing the frequency to 1 disables the delayed sack algorithm. If
4501 * the assoc_id is 0, then this sets or gets the endpoints default
4502 * values. If the assoc_id field is non-zero, then the set or get
4503 * effects the specified association for the one to many model (the
4504 * assoc_id field is ignored by the one to one model). Note that if
4505 * sack_delay or sack_freq are 0 when setting this option, then the
4506 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08004507 *
Wei Yongjund364d922008-05-09 15:13:26 -07004508 * struct sctp_sack_info {
4509 * sctp_assoc_t sack_assoc_id;
4510 * uint32_t sack_delay;
4511 * uint32_t sack_freq;
4512 * };
Frank Filz77086102005-12-22 11:37:30 -08004513 *
Wei Yongjund364d922008-05-09 15:13:26 -07004514 * sack_assoc_id - This parameter, indicates which association the user
4515 * is performing an action upon. Note that if this field's value is
4516 * zero then the endpoints default value is changed (effecting future
4517 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08004518 *
Wei Yongjund364d922008-05-09 15:13:26 -07004519 * sack_delay - This parameter contains the number of milliseconds that
4520 * the user is requesting the delayed ACK timer be set to. Note that
4521 * this value is defined in the standard to be between 200 and 500
4522 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08004523 *
Wei Yongjund364d922008-05-09 15:13:26 -07004524 * sack_freq - This parameter contains the number of packets that must
4525 * be received before a sack is sent without waiting for the delay
4526 * timer to expire. The default value for this is 2, setting this
4527 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08004528 */
Wei Yongjund364d922008-05-09 15:13:26 -07004529static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08004530 char __user *optval,
4531 int __user *optlen)
4532{
Wei Yongjund364d922008-05-09 15:13:26 -07004533 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08004534 struct sctp_association *asoc = NULL;
4535 struct sctp_sock *sp = sctp_sk(sk);
4536
Wei Yongjund364d922008-05-09 15:13:26 -07004537 if (len >= sizeof(struct sctp_sack_info)) {
4538 len = sizeof(struct sctp_sack_info);
4539
4540 if (copy_from_user(&params, optval, len))
4541 return -EFAULT;
4542 } else if (len == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00004543 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4544 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07004545 if (copy_from_user(&params, optval, len))
4546 return -EFAULT;
4547 } else
Frank Filz77086102005-12-22 11:37:30 -08004548 return - EINVAL;
4549
Wei Yongjund364d922008-05-09 15:13:26 -07004550 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08004551 * to many style socket, and an association was not found, then
4552 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004553 */
Wei Yongjund364d922008-05-09 15:13:26 -07004554 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4555 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08004556 return -EINVAL;
4557
4558 if (asoc) {
4559 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004560 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4561 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08004562 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07004563 params.sack_freq = asoc->sackfreq;
4564
4565 } else {
4566 params.sack_delay = 0;
4567 params.sack_freq = 1;
4568 }
Frank Filz77086102005-12-22 11:37:30 -08004569 } else {
4570 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004571 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4572 params.sack_delay = sp->sackdelay;
4573 params.sack_freq = sp->sackfreq;
4574 } else {
4575 params.sack_delay = 0;
4576 params.sack_freq = 1;
4577 }
Frank Filz77086102005-12-22 11:37:30 -08004578 }
4579
4580 if (copy_to_user(optval, &params, len))
4581 return -EFAULT;
4582
4583 if (put_user(len, optlen))
4584 return -EFAULT;
4585
4586 return 0;
4587}
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4590 *
4591 * Applications can specify protocol parameters for the default association
4592 * initialization. The option name argument to setsockopt() and getsockopt()
4593 * is SCTP_INITMSG.
4594 *
4595 * Setting initialization parameters is effective only on an unconnected
4596 * socket (for UDP-style sockets only future associations are effected
4597 * by the change). With TCP-style sockets, this option is inherited by
4598 * sockets derived from a listener socket.
4599 */
4600static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4601{
Neil Horman408f22e2007-06-16 14:03:45 -04004602 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004604 len = sizeof(struct sctp_initmsg);
4605 if (put_user(len, optlen))
4606 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4608 return -EFAULT;
4609 return 0;
4610}
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004613static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4614 char __user *optval, int __user *optlen)
4615{
4616 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004617 int cnt = 0;
4618 struct sctp_getaddrs getaddrs;
4619 struct sctp_transport *from;
4620 void __user *to;
4621 union sctp_addr temp;
4622 struct sctp_sock *sp = sctp_sk(sk);
4623 int addrlen;
4624 size_t space_left;
4625 int bytes_copied;
4626
4627 if (len < sizeof(struct sctp_getaddrs))
4628 return -EINVAL;
4629
4630 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4631 return -EFAULT;
4632
4633 /* For UDP-style sockets, id specifies the association to query. */
4634 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4635 if (!asoc)
4636 return -EINVAL;
4637
4638 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004639 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004640
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07004641 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4642 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08004643 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004644 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
Vlad Yasevicha5f4cea2010-04-30 21:42:42 -04004645 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004646 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004647 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004648 if (copy_to_user(to, &temp, addrlen))
4649 return -EFAULT;
4650 to += addrlen;
4651 cnt++;
4652 space_left -= addrlen;
4653 }
4654
4655 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4656 return -EFAULT;
4657 bytes_copied = ((char __user *)to) - optval;
4658 if (put_user(bytes_copied, optlen))
4659 return -EFAULT;
4660
4661 return 0;
4662}
4663
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004664static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4665 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004666{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004667 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004668 union sctp_addr temp;
4669 int cnt = 0;
4670 int addrlen;
4671
Vlad Yasevich29303542007-09-16 16:02:12 -07004672 rcu_read_lock();
4673 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4674 if (!addr->valid)
4675 continue;
4676
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004677 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004678 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004679 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07004680 if ((PF_INET6 == sk->sk_family) &&
4681 inet_v6_ipv6only(sk) &&
4682 (AF_INET == addr->a.sa.sa_family))
4683 continue;
Al Viro6244be42006-11-20 17:21:44 -08004684 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05004685 if (!temp.v4.sin_port)
4686 temp.v4.sin_port = htons(port);
4687
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004688 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4689 &temp);
4690 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004691 if (space_left < addrlen) {
4692 cnt = -ENOMEM;
4693 break;
4694 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004695 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004696
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004697 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004698 cnt ++;
4699 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004700 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004701 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004702 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004703
4704 return cnt;
4705}
4706
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004708static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4709 char __user *optval, int __user *optlen)
4710{
4711 struct sctp_bind_addr *bp;
4712 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004713 int cnt = 0;
4714 struct sctp_getaddrs getaddrs;
4715 struct sctp_sockaddr_entry *addr;
4716 void __user *to;
4717 union sctp_addr temp;
4718 struct sctp_sock *sp = sctp_sk(sk);
4719 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004720 int err = 0;
4721 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004722 int bytes_copied = 0;
4723 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004724 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004725
Neil Horman408f22e2007-06-16 14:03:45 -04004726 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004727 return -EINVAL;
4728
4729 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4730 return -EFAULT;
4731
4732 /*
4733 * For UDP-style sockets, id specifies the association to query.
4734 * If the id field is set to the value '0' then the locally bound
4735 * addresses are returned without regard to any particular
4736 * association.
4737 */
4738 if (0 == getaddrs.assoc_id) {
4739 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004740 } else {
4741 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4742 if (!asoc)
4743 return -EINVAL;
4744 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004745 }
4746
4747 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004748 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4749
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004750 addrs = kmalloc(space_left, GFP_KERNEL);
4751 if (!addrs)
4752 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004753
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004754 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4755 * addresses from the global local address list.
4756 */
4757 if (sctp_list_single_entry(&bp->address_list)) {
4758 addr = list_entry(bp->address_list.next,
4759 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004760 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004761 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4762 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004763 if (cnt < 0) {
4764 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004765 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004766 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004767 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004768 }
4769 }
4770
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004771 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004772 /* Protection on the bound address list is not needed since
4773 * in the socket option context we hold a socket lock and
4774 * thus the bound address list can't change.
4775 */
4776 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004777 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004778 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4779 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004780 if (space_left < addrlen) {
4781 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004782 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004783 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004784 memcpy(buf, &temp, addrlen);
4785 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004786 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004787 cnt ++;
4788 space_left -= addrlen;
4789 }
4790
4791copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004792 if (copy_to_user(to, addrs, bytes_copied)) {
4793 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004794 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004795 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004796 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4797 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004798 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004799 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004800 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004801 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004802out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004803 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004804 return err;
4805}
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4808 *
4809 * Requests that the local SCTP stack use the enclosed peer address as
4810 * the association primary. The enclosed address must be one of the
4811 * association peer's addresses.
4812 */
4813static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4814 char __user *optval, int __user *optlen)
4815{
4816 struct sctp_prim prim;
4817 struct sctp_association *asoc;
4818 struct sctp_sock *sp = sctp_sk(sk);
4819
Neil Horman408f22e2007-06-16 14:03:45 -04004820 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 return -EINVAL;
4822
Neil Horman408f22e2007-06-16 14:03:45 -04004823 len = sizeof(struct sctp_prim);
4824
4825 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 return -EFAULT;
4827
4828 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4829 if (!asoc)
4830 return -EINVAL;
4831
4832 if (!asoc->peer.primary_path)
4833 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004834
Al Viro8cec6b82006-11-20 17:23:01 -08004835 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4836 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4839 (union sctp_addr *)&prim.ssp_addr);
4840
Neil Horman408f22e2007-06-16 14:03:45 -04004841 if (put_user(len, optlen))
4842 return -EFAULT;
4843 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return -EFAULT;
4845
4846 return 0;
4847}
4848
4849/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004850 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004852 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 * Indication parameter for all future INIT and INIT-ACK exchanges.
4854 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004855static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 char __user *optval, int __user *optlen)
4857{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004858 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Neil Horman408f22e2007-06-16 14:03:45 -04004860 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return -EINVAL;
4862
Neil Horman408f22e2007-06-16 14:03:45 -04004863 len = sizeof(struct sctp_setadaptation);
4864
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004865 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004866
4867 if (put_user(len, optlen))
4868 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004869 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 return 0;
4873}
4874
4875/*
4876 *
4877 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4878 *
4879 * Applications that wish to use the sendto() system call may wish to
4880 * specify a default set of parameters that would normally be supplied
4881 * through the inclusion of ancillary data. This socket option allows
4882 * such an application to set the default sctp_sndrcvinfo structure.
4883
4884
4885 * The application that wishes to use this socket option simply passes
4886 * in to this call the sctp_sndrcvinfo structure defined in Section
4887 * 5.2.2) The input parameters accepted by this call include
4888 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4889 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4890 * to this call if the caller is using the UDP model.
4891 *
4892 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4893 */
4894static int sctp_getsockopt_default_send_param(struct sock *sk,
4895 int len, char __user *optval,
4896 int __user *optlen)
4897{
4898 struct sctp_sndrcvinfo info;
4899 struct sctp_association *asoc;
4900 struct sctp_sock *sp = sctp_sk(sk);
4901
Neil Horman408f22e2007-06-16 14:03:45 -04004902 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004904
4905 len = sizeof(struct sctp_sndrcvinfo);
4906
4907 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return -EFAULT;
4909
4910 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4911 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4912 return -EINVAL;
4913
4914 if (asoc) {
4915 info.sinfo_stream = asoc->default_stream;
4916 info.sinfo_flags = asoc->default_flags;
4917 info.sinfo_ppid = asoc->default_ppid;
4918 info.sinfo_context = asoc->default_context;
4919 info.sinfo_timetolive = asoc->default_timetolive;
4920 } else {
4921 info.sinfo_stream = sp->default_stream;
4922 info.sinfo_flags = sp->default_flags;
4923 info.sinfo_ppid = sp->default_ppid;
4924 info.sinfo_context = sp->default_context;
4925 info.sinfo_timetolive = sp->default_timetolive;
4926 }
4927
Neil Horman408f22e2007-06-16 14:03:45 -04004928 if (put_user(len, optlen))
4929 return -EFAULT;
4930 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 return -EFAULT;
4932
4933 return 0;
4934}
4935
4936/*
4937 *
4938 * 7.1.5 SCTP_NODELAY
4939 *
4940 * Turn on/off any Nagle-like algorithm. This means that packets are
4941 * generally sent as soon as possible and no unnecessary delays are
4942 * introduced, at the cost of more packets in the network. Expects an
4943 * integer boolean flag.
4944 */
4945
4946static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4947 char __user *optval, int __user *optlen)
4948{
4949 int val;
4950
4951 if (len < sizeof(int))
4952 return -EINVAL;
4953
4954 len = sizeof(int);
4955 val = (sctp_sk(sk)->nodelay == 1);
4956 if (put_user(len, optlen))
4957 return -EFAULT;
4958 if (copy_to_user(optval, &val, len))
4959 return -EFAULT;
4960 return 0;
4961}
4962
4963/*
4964 *
4965 * 7.1.1 SCTP_RTOINFO
4966 *
4967 * The protocol parameters used to initialize and bound retransmission
4968 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4969 * and modify these parameters.
4970 * All parameters are time values, in milliseconds. A value of 0, when
4971 * modifying the parameters, indicates that the current value should not
4972 * be changed.
4973 *
4974 */
4975static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4976 char __user *optval,
4977 int __user *optlen) {
4978 struct sctp_rtoinfo rtoinfo;
4979 struct sctp_association *asoc;
4980
Neil Horman408f22e2007-06-16 14:03:45 -04004981 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 return -EINVAL;
4983
Neil Horman408f22e2007-06-16 14:03:45 -04004984 len = sizeof(struct sctp_rtoinfo);
4985
4986 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 return -EFAULT;
4988
4989 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4990
4991 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4992 return -EINVAL;
4993
4994 /* Values corresponding to the specific association. */
4995 if (asoc) {
4996 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4997 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4998 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4999 } else {
5000 /* Values corresponding to the endpoint. */
5001 struct sctp_sock *sp = sctp_sk(sk);
5002
5003 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5004 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5005 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5006 }
5007
5008 if (put_user(len, optlen))
5009 return -EFAULT;
5010
5011 if (copy_to_user(optval, &rtoinfo, len))
5012 return -EFAULT;
5013
5014 return 0;
5015}
5016
5017/*
5018 *
5019 * 7.1.2 SCTP_ASSOCINFO
5020 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005021 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 * of the association.
5023 * Returns an error if the new association retransmission value is
5024 * greater than the sum of the retransmission value of the peer.
5025 * See [SCTP] for more information.
5026 *
5027 */
5028static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5029 char __user *optval,
5030 int __user *optlen)
5031{
5032
5033 struct sctp_assocparams assocparams;
5034 struct sctp_association *asoc;
5035 struct list_head *pos;
5036 int cnt = 0;
5037
Neil Horman408f22e2007-06-16 14:03:45 -04005038 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 return -EINVAL;
5040
Neil Horman408f22e2007-06-16 14:03:45 -04005041 len = sizeof(struct sctp_assocparams);
5042
5043 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 return -EFAULT;
5045
5046 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5047
5048 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5049 return -EINVAL;
5050
5051 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005052 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5054 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5055 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5056 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5057 * 1000) +
5058 (asoc->cookie_life.tv_usec
5059 / 1000);
5060
5061 list_for_each(pos, &asoc->peer.transport_addr_list) {
5062 cnt ++;
5063 }
5064
5065 assocparams.sasoc_number_peer_destinations = cnt;
5066 } else {
5067 /* Values corresponding to the endpoint */
5068 struct sctp_sock *sp = sctp_sk(sk);
5069
5070 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5071 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5072 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5073 assocparams.sasoc_cookie_life =
5074 sp->assocparams.sasoc_cookie_life;
5075 assocparams.sasoc_number_peer_destinations =
5076 sp->assocparams.
5077 sasoc_number_peer_destinations;
5078 }
5079
5080 if (put_user(len, optlen))
5081 return -EFAULT;
5082
5083 if (copy_to_user(optval, &assocparams, len))
5084 return -EFAULT;
5085
5086 return 0;
5087}
5088
5089/*
5090 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5091 *
5092 * This socket option is a boolean flag which turns on or off mapped V4
5093 * addresses. If this option is turned on and the socket is type
5094 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5095 * If this option is turned off, then no mapping will be done of V4
5096 * addresses and a user will receive both PF_INET6 and PF_INET type
5097 * addresses on the socket.
5098 */
5099static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5100 char __user *optval, int __user *optlen)
5101{
5102 int val;
5103 struct sctp_sock *sp = sctp_sk(sk);
5104
5105 if (len < sizeof(int))
5106 return -EINVAL;
5107
5108 len = sizeof(int);
5109 val = sp->v4mapped;
5110 if (put_user(len, optlen))
5111 return -EFAULT;
5112 if (copy_to_user(optval, &val, len))
5113 return -EFAULT;
5114
5115 return 0;
5116}
5117
5118/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005119 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5120 * (chapter and verse is quoted at sctp_setsockopt_context())
5121 */
5122static int sctp_getsockopt_context(struct sock *sk, int len,
5123 char __user *optval, int __user *optlen)
5124{
5125 struct sctp_assoc_value params;
5126 struct sctp_sock *sp;
5127 struct sctp_association *asoc;
5128
Neil Horman408f22e2007-06-16 14:03:45 -04005129 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005130 return -EINVAL;
5131
Neil Horman408f22e2007-06-16 14:03:45 -04005132 len = sizeof(struct sctp_assoc_value);
5133
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005134 if (copy_from_user(&params, optval, len))
5135 return -EFAULT;
5136
5137 sp = sctp_sk(sk);
5138
5139 if (params.assoc_id != 0) {
5140 asoc = sctp_id2assoc(sk, params.assoc_id);
5141 if (!asoc)
5142 return -EINVAL;
5143 params.assoc_value = asoc->default_rcv_context;
5144 } else {
5145 params.assoc_value = sp->default_rcv_context;
5146 }
5147
5148 if (put_user(len, optlen))
5149 return -EFAULT;
5150 if (copy_to_user(optval, &params, len))
5151 return -EFAULT;
5152
5153 return 0;
5154}
5155
5156/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005157 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5158 * This option will get or set the maximum size to put in any outgoing
5159 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 * fragmented by SCTP into the specified size. Note that the underlying
5161 * SCTP implementation may fragment into smaller sized chunks when the
5162 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005163 * the user. The default value for this option is '0' which indicates
5164 * the user is NOT limiting fragmentation and only the PMTU will effect
5165 * SCTP's choice of DATA chunk size. Note also that values set larger
5166 * than the maximum size of an IP datagram will effectively let SCTP
5167 * control fragmentation (i.e. the same as setting this option to 0).
5168 *
5169 * The following structure is used to access and modify this parameter:
5170 *
5171 * struct sctp_assoc_value {
5172 * sctp_assoc_t assoc_id;
5173 * uint32_t assoc_value;
5174 * };
5175 *
5176 * assoc_id: This parameter is ignored for one-to-one style sockets.
5177 * For one-to-many style sockets this parameter indicates which
5178 * association the user is performing an action upon. Note that if
5179 * this field's value is zero then the endpoints default value is
5180 * changed (effecting future associations only).
5181 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 */
5183static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5184 char __user *optval, int __user *optlen)
5185{
Wei Yongjune89c2092008-12-25 16:54:58 -08005186 struct sctp_assoc_value params;
5187 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Wei Yongjune89c2092008-12-25 16:54:58 -08005189 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005190 pr_warn("Use of int in maxseg socket option deprecated\n");
5191 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08005192 params.assoc_id = 0;
5193 } else if (len >= sizeof(struct sctp_assoc_value)) {
5194 len = sizeof(struct sctp_assoc_value);
5195 if (copy_from_user(&params, optval, sizeof(params)))
5196 return -EFAULT;
5197 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return -EINVAL;
5199
Wei Yongjune89c2092008-12-25 16:54:58 -08005200 asoc = sctp_id2assoc(sk, params.assoc_id);
5201 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Wei Yongjune89c2092008-12-25 16:54:58 -08005204 if (asoc)
5205 params.assoc_value = asoc->frag_point;
5206 else
5207 params.assoc_value = sctp_sk(sk)->user_frag;
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 if (put_user(len, optlen))
5210 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005211 if (len == sizeof(int)) {
5212 if (copy_to_user(optval, &params.assoc_value, len))
5213 return -EFAULT;
5214 } else {
5215 if (copy_to_user(optval, &params, len))
5216 return -EFAULT;
5217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 return 0;
5220}
5221
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005222/*
5223 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5224 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5225 */
5226static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5227 char __user *optval, int __user *optlen)
5228{
5229 int val;
5230
5231 if (len < sizeof(int))
5232 return -EINVAL;
5233
5234 len = sizeof(int);
5235
5236 val = sctp_sk(sk)->frag_interleave;
5237 if (put_user(len, optlen))
5238 return -EFAULT;
5239 if (copy_to_user(optval, &val, len))
5240 return -EFAULT;
5241
5242 return 0;
5243}
5244
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005245/*
5246 * 7.1.25. Set or Get the sctp partial delivery point
5247 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5248 */
5249static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5250 char __user *optval,
5251 int __user *optlen)
5252{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005253 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005254
5255 if (len < sizeof(u32))
5256 return -EINVAL;
5257
5258 len = sizeof(u32);
5259
5260 val = sctp_sk(sk)->pd_point;
5261 if (put_user(len, optlen))
5262 return -EFAULT;
5263 if (copy_to_user(optval, &val, len))
5264 return -EFAULT;
5265
Wei Yongjun7d743b72010-12-14 16:10:41 +00005266 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005267}
5268
Vlad Yasevich70331572007-03-23 11:34:36 -07005269/*
5270 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5271 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5272 */
5273static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5274 char __user *optval,
5275 int __user *optlen)
5276{
Neil Horman219b99a2008-03-05 13:44:46 -08005277 struct sctp_assoc_value params;
5278 struct sctp_sock *sp;
5279 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005280
Neil Horman219b99a2008-03-05 13:44:46 -08005281 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005282 pr_warn("Use of int in max_burst socket option deprecated\n");
5283 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08005284 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005285 } else if (len >= sizeof(struct sctp_assoc_value)) {
5286 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005287 if (copy_from_user(&params, optval, len))
5288 return -EFAULT;
5289 } else
5290 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005291
Neil Horman219b99a2008-03-05 13:44:46 -08005292 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005293
Neil Horman219b99a2008-03-05 13:44:46 -08005294 if (params.assoc_id != 0) {
5295 asoc = sctp_id2assoc(sk, params.assoc_id);
5296 if (!asoc)
5297 return -EINVAL;
5298 params.assoc_value = asoc->max_burst;
5299 } else
5300 params.assoc_value = sp->max_burst;
5301
5302 if (len == sizeof(int)) {
5303 if (copy_to_user(optval, &params.assoc_value, len))
5304 return -EFAULT;
5305 } else {
5306 if (copy_to_user(optval, &params, len))
5307 return -EFAULT;
5308 }
5309
5310 return 0;
5311
Vlad Yasevich70331572007-03-23 11:34:36 -07005312}
5313
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005314static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5315 char __user *optval, int __user *optlen)
5316{
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005317 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005318 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005319 __u16 data_len = 0;
5320 u32 num_idents;
5321
5322 if (!sctp_auth_enable)
5323 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005324
5325 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005326 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005327
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005328 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005329 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005330
5331 len = sizeof(struct sctp_hmacalgo) + data_len;
5332 num_idents = data_len / sizeof(u16);
5333
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005334 if (put_user(len, optlen))
5335 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005336 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005337 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005338 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5339 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005340 return 0;
5341}
5342
5343static int sctp_getsockopt_active_key(struct sock *sk, int len,
5344 char __user *optval, int __user *optlen)
5345{
5346 struct sctp_authkeyid val;
5347 struct sctp_association *asoc;
5348
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005349 if (!sctp_auth_enable)
5350 return -EACCES;
5351
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005352 if (len < sizeof(struct sctp_authkeyid))
5353 return -EINVAL;
5354 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5355 return -EFAULT;
5356
5357 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5358 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5359 return -EINVAL;
5360
5361 if (asoc)
5362 val.scact_keynumber = asoc->active_key_id;
5363 else
5364 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5365
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005366 len = sizeof(struct sctp_authkeyid);
5367 if (put_user(len, optlen))
5368 return -EFAULT;
5369 if (copy_to_user(optval, &val, len))
5370 return -EFAULT;
5371
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005372 return 0;
5373}
5374
5375static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5376 char __user *optval, int __user *optlen)
5377{
Al Viro411223c2007-10-14 19:21:20 +01005378 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005379 struct sctp_authchunks val;
5380 struct sctp_association *asoc;
5381 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005382 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005383 char __user *to;
5384
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005385 if (!sctp_auth_enable)
5386 return -EACCES;
5387
5388 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005389 return -EINVAL;
5390
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005391 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005392 return -EFAULT;
5393
Al Viro411223c2007-10-14 19:21:20 +01005394 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005395 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5396 if (!asoc)
5397 return -EINVAL;
5398
5399 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005400 if (!ch)
5401 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005402
5403 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05005404 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5405 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005406 return -EINVAL;
5407
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005408 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005409 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005410num:
5411 len = sizeof(struct sctp_authchunks) + num_chunks;
5412 if (put_user(len, optlen)) return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005413 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5414 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005415 return 0;
5416}
5417
5418static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5419 char __user *optval, int __user *optlen)
5420{
Al Viro411223c2007-10-14 19:21:20 +01005421 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005422 struct sctp_authchunks val;
5423 struct sctp_association *asoc;
5424 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005425 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005426 char __user *to;
5427
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005428 if (!sctp_auth_enable)
5429 return -EACCES;
5430
5431 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005432 return -EINVAL;
5433
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005434 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005435 return -EFAULT;
5436
Al Viro411223c2007-10-14 19:21:20 +01005437 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005438 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5439 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5440 return -EINVAL;
5441
5442 if (asoc)
5443 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5444 else
5445 ch = sctp_sk(sk)->ep->auth_chunk_list;
5446
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005447 if (!ch)
5448 goto num;
5449
Vlad Yasevichb40db682008-02-27 14:40:37 -05005450 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005451 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005452 return -EINVAL;
5453
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005454 if (copy_to_user(to, ch->chunks, num_chunks))
5455 return -EFAULT;
5456num:
5457 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005458 if (put_user(len, optlen))
5459 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005460 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5461 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005462
5463 return 0;
5464}
5465
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005466/*
5467 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5468 * This option gets the current number of associations that are attached
5469 * to a one-to-many style socket. The option value is an uint32_t.
5470 */
5471static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5472 char __user *optval, int __user *optlen)
5473{
5474 struct sctp_sock *sp = sctp_sk(sk);
5475 struct sctp_association *asoc;
5476 u32 val = 0;
5477
5478 if (sctp_style(sk, TCP))
5479 return -EOPNOTSUPP;
5480
5481 if (len < sizeof(u32))
5482 return -EINVAL;
5483
5484 len = sizeof(u32);
5485
5486 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5487 val++;
5488 }
5489
5490 if (put_user(len, optlen))
5491 return -EFAULT;
5492 if (copy_to_user(optval, &val, len))
5493 return -EFAULT;
5494
5495 return 0;
5496}
5497
Wei Yongjun209ba422011-04-17 17:27:08 +00005498/*
Michio Honda7dc04d72011-04-26 20:16:31 +09005499 * 8.1.23 SCTP_AUTO_ASCONF
5500 * See the corresponding setsockopt entry as description
5501 */
5502static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
5503 char __user *optval, int __user *optlen)
5504{
5505 int val = 0;
5506
5507 if (len < sizeof(int))
5508 return -EINVAL;
5509
5510 len = sizeof(int);
5511 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
5512 val = 1;
5513 if (put_user(len, optlen))
5514 return -EFAULT;
5515 if (copy_to_user(optval, &val, len))
5516 return -EFAULT;
5517 return 0;
5518}
5519
5520/*
Wei Yongjun209ba422011-04-17 17:27:08 +00005521 * 8.2.6. Get the Current Identifiers of Associations
5522 * (SCTP_GET_ASSOC_ID_LIST)
5523 *
5524 * This option gets the current list of SCTP association identifiers of
5525 * the SCTP associations handled by a one-to-many style socket.
5526 */
5527static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
5528 char __user *optval, int __user *optlen)
5529{
5530 struct sctp_sock *sp = sctp_sk(sk);
5531 struct sctp_association *asoc;
5532 struct sctp_assoc_ids *ids;
5533 u32 num = 0;
5534
5535 if (sctp_style(sk, TCP))
5536 return -EOPNOTSUPP;
5537
5538 if (len < sizeof(struct sctp_assoc_ids))
5539 return -EINVAL;
5540
5541 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5542 num++;
5543 }
5544
5545 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
5546 return -EINVAL;
5547
5548 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
5549
5550 ids = kmalloc(len, GFP_KERNEL);
5551 if (unlikely(!ids))
5552 return -ENOMEM;
5553
5554 ids->gaids_number_of_ids = num;
5555 num = 0;
5556 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5557 ids->gaids_assoc_id[num++] = asoc->assoc_id;
5558 }
5559
5560 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
5561 kfree(ids);
5562 return -EFAULT;
5563 }
5564
5565 kfree(ids);
5566 return 0;
5567}
5568
Neil Horman5aa93bc2012-07-21 07:56:07 +00005569/*
5570 * SCTP_PEER_ADDR_THLDS
5571 *
5572 * This option allows us to fetch the partially failed threshold for one or all
5573 * transports in an association. See Section 6.1 of:
5574 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
5575 */
5576static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
5577 char __user *optval,
5578 int len,
5579 int __user *optlen)
5580{
5581 struct sctp_paddrthlds val;
5582 struct sctp_transport *trans;
5583 struct sctp_association *asoc;
5584
5585 if (len < sizeof(struct sctp_paddrthlds))
5586 return -EINVAL;
5587 len = sizeof(struct sctp_paddrthlds);
5588 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
5589 return -EFAULT;
5590
5591 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
5592 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
5593 if (!asoc)
5594 return -ENOENT;
5595
5596 val.spt_pathpfthld = asoc->pf_retrans;
5597 val.spt_pathmaxrxt = asoc->pathmaxrxt;
5598 } else {
5599 trans = sctp_addr_id2transport(sk, &val.spt_address,
5600 val.spt_assoc_id);
5601 if (!trans)
5602 return -ENOENT;
5603
5604 val.spt_pathmaxrxt = trans->pathmaxrxt;
5605 val.spt_pathpfthld = trans->pf_retrans;
5606 }
5607
5608 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
5609 return -EFAULT;
5610
5611 return 0;
5612}
5613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5615 char __user *optval, int __user *optlen)
5616{
5617 int retval = 0;
5618 int len;
5619
Frank Filz3f7a87d2005-06-20 13:14:57 -07005620 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5621 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 /* I can hardly begin to describe how wrong this is. This is
5624 * so broken as to be worse than useless. The API draft
5625 * REALLY is NOT helpful here... I am not convinced that the
5626 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5627 * are at all well-founded.
5628 */
5629 if (level != SOL_SCTP) {
5630 struct sctp_af *af = sctp_sk(sk)->pf->af;
5631
5632 retval = af->getsockopt(sk, level, optname, optval, optlen);
5633 return retval;
5634 }
5635
5636 if (get_user(len, optlen))
5637 return -EFAULT;
5638
5639 sctp_lock_sock(sk);
5640
5641 switch (optname) {
5642 case SCTP_STATUS:
5643 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5644 break;
5645 case SCTP_DISABLE_FRAGMENTS:
5646 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5647 optlen);
5648 break;
5649 case SCTP_EVENTS:
5650 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5651 break;
5652 case SCTP_AUTOCLOSE:
5653 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5654 break;
5655 case SCTP_SOCKOPT_PEELOFF:
5656 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5657 break;
5658 case SCTP_PEER_ADDR_PARAMS:
5659 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5660 optlen);
5661 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00005662 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07005663 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08005664 optlen);
5665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 case SCTP_INITMSG:
5667 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 case SCTP_GET_PEER_ADDRS:
5670 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5671 optlen);
5672 break;
5673 case SCTP_GET_LOCAL_ADDRS:
5674 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5675 optlen);
5676 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04005677 case SCTP_SOCKOPT_CONNECTX3:
5678 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 case SCTP_DEFAULT_SEND_PARAM:
5681 retval = sctp_getsockopt_default_send_param(sk, len,
5682 optval, optlen);
5683 break;
5684 case SCTP_PRIMARY_ADDR:
5685 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5686 break;
5687 case SCTP_NODELAY:
5688 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5689 break;
5690 case SCTP_RTOINFO:
5691 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5692 break;
5693 case SCTP_ASSOCINFO:
5694 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5695 break;
5696 case SCTP_I_WANT_MAPPED_V4_ADDR:
5697 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5698 break;
5699 case SCTP_MAXSEG:
5700 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5701 break;
5702 case SCTP_GET_PEER_ADDR_INFO:
5703 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5704 optlen);
5705 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005706 case SCTP_ADAPTATION_LAYER:
5707 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 optlen);
5709 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005710 case SCTP_CONTEXT:
5711 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5712 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005713 case SCTP_FRAGMENT_INTERLEAVE:
5714 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5715 optlen);
5716 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005717 case SCTP_PARTIAL_DELIVERY_POINT:
5718 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5719 optlen);
5720 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005721 case SCTP_MAX_BURST:
5722 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5723 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005724 case SCTP_AUTH_KEY:
5725 case SCTP_AUTH_CHUNK:
5726 case SCTP_AUTH_DELETE_KEY:
5727 retval = -EOPNOTSUPP;
5728 break;
5729 case SCTP_HMAC_IDENT:
5730 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5731 break;
5732 case SCTP_AUTH_ACTIVE_KEY:
5733 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5734 break;
5735 case SCTP_PEER_AUTH_CHUNKS:
5736 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5737 optlen);
5738 break;
5739 case SCTP_LOCAL_AUTH_CHUNKS:
5740 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5741 optlen);
5742 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005743 case SCTP_GET_ASSOC_NUMBER:
5744 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5745 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00005746 case SCTP_GET_ASSOC_ID_LIST:
5747 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5748 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09005749 case SCTP_AUTO_ASCONF:
5750 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5751 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00005752 case SCTP_PEER_ADDR_THLDS:
5753 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
5754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 default:
5756 retval = -ENOPROTOOPT;
5757 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
5760 sctp_release_sock(sk);
5761 return retval;
5762}
5763
5764static void sctp_hash(struct sock *sk)
5765{
5766 /* STUB */
5767}
5768
5769static void sctp_unhash(struct sock *sk)
5770{
5771 /* STUB */
5772}
5773
5774/* Check if port is acceptable. Possibly find first available port.
5775 *
5776 * The port hash table (contained in the 'global' SCTP protocol storage
5777 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5778 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5779 * list (the list number is the port number hashed out, so as you
5780 * would expect from a hash function, all the ports in a given list have
5781 * such a number that hashes out to the same list number; you were
5782 * expecting that, right?); so each list has a set of ports, with a
5783 * link to the socket (struct sock) that uses it, the port number and
5784 * a fastreuse flag (FIXME: NPI ipg).
5785 */
5786static struct sctp_bind_bucket *sctp_bucket_create(
5787 struct sctp_bind_hashbucket *head, unsigned short snum);
5788
5789static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5790{
5791 struct sctp_bind_hashbucket *head; /* hash list */
5792 struct sctp_bind_bucket *pp; /* hash list port iterator */
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005793 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 unsigned short snum;
5795 int ret;
5796
Al Viro04afd8b2006-11-20 17:02:01 -08005797 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5800 sctp_local_bh_disable();
5801
5802 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07005803 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07005804 int low, high, remaining, index;
5805 unsigned int rover;
5806
5807 inet_get_local_port_range(&low, &high);
5808 remaining = (high - low) + 1;
5809 rover = net_random() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 do {
5812 rover++;
5813 if ((rover < low) || (rover > high))
5814 rover = low;
Amerigo Wange3826f12010-05-05 00:27:06 +00005815 if (inet_is_reserved_local_port(rover))
5816 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 index = sctp_phashfn(rover);
5818 head = &sctp_port_hashtable[index];
5819 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005820 sctp_for_each_hentry(pp, node, &head->chain)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 if (pp->port == rover)
5822 goto next;
5823 break;
5824 next:
5825 sctp_spin_unlock(&head->lock);
5826 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 /* Exhausted local port range during search? */
5829 ret = 1;
5830 if (remaining <= 0)
5831 goto fail;
5832
5833 /* OK, here is the one we will use. HEAD (the port
5834 * hash table list entry) is non-NULL and we hold it's
5835 * mutex.
5836 */
5837 snum = rover;
5838 } else {
5839 /* We are given an specific port number; we verify
5840 * that it is not being used. If it is used, we will
5841 * exahust the search in the hash list corresponding
5842 * to the port number (snum) - we detect that with the
5843 * port iterator, pp being NULL.
5844 */
5845 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5846 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005847 sctp_for_each_hentry(pp, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 if (pp->port == snum)
5849 goto pp_found;
5850 }
5851 }
5852 pp = NULL;
5853 goto pp_not_found;
5854pp_found:
5855 if (!hlist_empty(&pp->owner)) {
5856 /* We had a port hash table hit - there is an
5857 * available port (pp != NULL) and it is being
5858 * used by other socket (pp->owner not empty); that other
5859 * socket is going to be sk2.
5860 */
5861 int reuse = sk->sk_reuse;
5862 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
5864 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005865 if (pp->fastreuse && sk->sk_reuse &&
5866 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 goto success;
5868
5869 /* Run through the list of sockets bound to the port
5870 * (pp->port) [via the pointers bind_next and
5871 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5872 * we get the endpoint they describe and run through
5873 * the endpoint's list of IP (v4 or v6) addresses,
5874 * comparing each of the addresses with the address of
5875 * the socket sk. If we find a match, then that means
5876 * that this port/socket (sk) combination are already
5877 * in an endpoint.
5878 */
5879 sk_for_each_bound(sk2, node, &pp->owner) {
5880 struct sctp_endpoint *ep2;
5881 ep2 = sctp_sk(sk2)->ep;
5882
Vlad Yasevich4e540642008-07-18 23:06:32 -07005883 if (sk == sk2 ||
5884 (reuse && sk2->sk_reuse &&
5885 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 continue;
5887
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005888 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5889 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 ret = (long)sk2;
5891 goto fail_unlock;
5892 }
5893 }
5894 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5895 }
5896pp_not_found:
5897 /* If there was a hash table miss, create a new port. */
5898 ret = 1;
5899 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5900 goto fail_unlock;
5901
5902 /* In either case (hit or miss), make sure fastreuse is 1 only
5903 * if sk->sk_reuse is too (that is, if the caller requested
5904 * SO_REUSEADDR on this socket -sk-).
5905 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005906 if (hlist_empty(&pp->owner)) {
5907 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5908 pp->fastreuse = 1;
5909 else
5910 pp->fastreuse = 0;
5911 } else if (pp->fastreuse &&
5912 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 pp->fastreuse = 0;
5914
5915 /* We are set, so fill up all the data in the hash table
5916 * entry, tie the socket list information with the rest of the
5917 * sockets FIXME: Blurry, NPI (ipg).
5918 */
5919success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005921 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 sk_add_bind_node(sk, &pp->owner);
5923 sctp_sk(sk)->bind_hash = pp;
5924 }
5925 ret = 0;
5926
5927fail_unlock:
5928 sctp_spin_unlock(&head->lock);
5929
5930fail:
5931 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 return ret;
5933}
5934
5935/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5936 * port is requested.
5937 */
5938static int sctp_get_port(struct sock *sk, unsigned short snum)
5939{
5940 long ret;
5941 union sctp_addr addr;
5942 struct sctp_af *af = sctp_sk(sk)->pf->af;
5943
5944 /* Set up a dummy address struct from the sk. */
5945 af->from_sk(&addr, sk);
5946 addr.v4.sin_port = htons(snum);
5947
5948 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5949 ret = sctp_get_port_local(sk, &addr);
5950
Eric Dumazeta02cec22010-09-22 20:43:57 +00005951 return ret ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952}
5953
5954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 * Move a socket to LISTENING state.
5956 */
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005957SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005959 struct sctp_sock *sp = sctp_sk(sk);
5960 struct sctp_endpoint *ep = sp->ep;
Herbert Xu1b489e12006-08-20 15:07:14 +10005961 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 /* Allocate HMAC for generating cookie. */
Vlad Yasevich23b29ed2008-07-18 23:06:07 -07005964 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005965 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005966 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00005967 net_info_ratelimited("failed to load transform for %s: %ld\n",
5968 sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005969 return -ENOSYS;
5970 }
5971 sctp_sk(sk)->hmac = tfm;
5972 }
5973
5974 /*
5975 * If a bind() or sctp_bindx() is not called prior to a listen()
5976 * call that allows new associations to be accepted, the system
5977 * picks an ephemeral port and will choose an address set equivalent
5978 * to binding with a wildcard address.
5979 *
5980 * This is not currently spelled out in the SCTP sockets
5981 * extensions draft, but follows the practice as seen in TCP
5982 * sockets.
5983 *
5984 */
5985 sk->sk_state = SCTP_SS_LISTENING;
5986 if (!ep->base.bind_addr.port) {
5987 if (sctp_autobind(sk))
5988 return -EAGAIN;
5989 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005990 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005991 sk->sk_state = SCTP_SS_CLOSED;
5992 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 }
5994 }
5995
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005996 sk->sk_max_ack_backlog = backlog;
5997 sctp_hash_endpoint(ep);
5998 return 0;
5999}
6000
6001/*
6002 * 4.1.3 / 5.1.3 listen()
6003 *
6004 * By default, new associations are not accepted for UDP style sockets.
6005 * An application uses listen() to mark a socket as being able to
6006 * accept new associations.
6007 *
6008 * On TCP style sockets, applications use listen() to ready the SCTP
6009 * endpoint for accepting inbound associations.
6010 *
6011 * On both types of endpoints a backlog of '0' disables listening.
6012 *
6013 * Move a socket to LISTENING state.
6014 */
6015int sctp_inet_listen(struct socket *sock, int backlog)
6016{
6017 struct sock *sk = sock->sk;
6018 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6019 int err = -EINVAL;
6020
6021 if (unlikely(backlog < 0))
6022 return err;
6023
6024 sctp_lock_sock(sk);
6025
6026 /* Peeled-off sockets are not allowed to listen(). */
6027 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6028 goto out;
6029
6030 if (sock->state != SS_UNCONNECTED)
6031 goto out;
6032
6033 /* If backlog is zero, disable listening. */
6034 if (!backlog) {
6035 if (sctp_sstate(sk, CLOSED))
6036 goto out;
6037
6038 err = 0;
6039 sctp_unhash_endpoint(ep);
6040 sk->sk_state = SCTP_SS_CLOSED;
6041 if (sk->sk_reuse)
6042 sctp_sk(sk)->bind_hash->fastreuse = 1;
6043 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006044 }
6045
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006046 /* If we are already listening, just update the backlog */
6047 if (sctp_sstate(sk, LISTENING))
6048 sk->sk_max_ack_backlog = backlog;
6049 else {
6050 err = sctp_listen_start(sk, backlog);
6051 if (err)
6052 goto out;
6053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006055 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056out:
6057 sctp_release_sock(sk);
6058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059}
6060
6061/*
6062 * This function is done by modeling the current datagram_poll() and the
6063 * tcp_poll(). Note that, based on these implementations, we don't
6064 * lock the socket in this function, even though it seems that,
6065 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08006066 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 * in this place. We assume that we don't need locks either until proven
6068 * otherwise.
6069 *
6070 * Another thing to note is that we include the Async I/O support
6071 * here, again, by modeling the current TCP/UDP code. We don't have
6072 * a good way to test with it yet.
6073 */
6074unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6075{
6076 struct sock *sk = sock->sk;
6077 struct sctp_sock *sp = sctp_sk(sk);
6078 unsigned int mask;
6079
Eric Dumazetaa395142010-04-20 13:03:51 +00006080 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082 /* A TCP-style listening socket becomes readable when the accept queue
6083 * is not empty.
6084 */
6085 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6086 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006087 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
6089 mask = 0;
6090
6091 /* Is there any exceptional events? */
6092 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
6093 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08006094 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006095 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 if (sk->sk_shutdown == SHUTDOWN_MASK)
6097 mask |= POLLHUP;
6098
6099 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006100 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 mask |= POLLIN | POLLRDNORM;
6102
6103 /* The association is either gone or not ready. */
6104 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6105 return mask;
6106
6107 /* Is it writable? */
6108 if (sctp_writeable(sk)) {
6109 mask |= POLLOUT | POLLWRNORM;
6110 } else {
6111 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
6112 /*
6113 * Since the socket is not locked, the buffer
6114 * might be made available after the writeable check and
6115 * before the bit is set. This could cause a lost I/O
6116 * signal. tcp_poll() has a race breaker for this race
6117 * condition. Based on their implementation, we put
6118 * in the following code to cover it as well.
6119 */
6120 if (sctp_writeable(sk))
6121 mask |= POLLOUT | POLLWRNORM;
6122 }
6123 return mask;
6124}
6125
6126/********************************************************************
6127 * 2nd Level Abstractions
6128 ********************************************************************/
6129
6130static struct sctp_bind_bucket *sctp_bucket_create(
6131 struct sctp_bind_hashbucket *head, unsigned short snum)
6132{
6133 struct sctp_bind_bucket *pp;
6134
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08006135 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07006137 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 pp->port = snum;
6139 pp->fastreuse = 0;
6140 INIT_HLIST_HEAD(&pp->owner);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006141 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
6143 return pp;
6144}
6145
6146/* Caller must hold hashbucket lock for this tb with local BH disabled */
6147static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
6148{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07006149 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006150 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 kmem_cache_free(sctp_bucket_cachep, pp);
6152 SCTP_DBG_OBJCNT_DEC(bind_bucket);
6153 }
6154}
6155
6156/* Release this socket's reference to a local port. */
6157static inline void __sctp_put_port(struct sock *sk)
6158{
6159 struct sctp_bind_hashbucket *head =
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006160 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 struct sctp_bind_bucket *pp;
6162
6163 sctp_spin_lock(&head->lock);
6164 pp = sctp_sk(sk)->bind_hash;
6165 __sk_del_bind_node(sk);
6166 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006167 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 sctp_bucket_destroy(pp);
6169 sctp_spin_unlock(&head->lock);
6170}
6171
6172void sctp_put_port(struct sock *sk)
6173{
6174 sctp_local_bh_disable();
6175 __sctp_put_port(sk);
6176 sctp_local_bh_enable();
6177}
6178
6179/*
6180 * The system picks an ephemeral port and choose an address set equivalent
6181 * to binding with a wildcard address.
6182 * One of those addresses will be the primary address for the association.
6183 * This automatically enables the multihoming capability of SCTP.
6184 */
6185static int sctp_autobind(struct sock *sk)
6186{
6187 union sctp_addr autoaddr;
6188 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08006189 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 /* Initialize a local sockaddr structure to INADDR_ANY. */
6192 af = sctp_sk(sk)->pf->af;
6193
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006194 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 af->inaddr_any(&autoaddr, port);
6196
6197 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
6198}
6199
6200/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
6201 *
6202 * From RFC 2292
6203 * 4.2 The cmsghdr Structure *
6204 *
6205 * When ancillary data is sent or received, any number of ancillary data
6206 * objects can be specified by the msg_control and msg_controllen members of
6207 * the msghdr structure, because each object is preceded by
6208 * a cmsghdr structure defining the object's length (the cmsg_len member).
6209 * Historically Berkeley-derived implementations have passed only one object
6210 * at a time, but this API allows multiple objects to be
6211 * passed in a single call to sendmsg() or recvmsg(). The following example
6212 * shows two ancillary data objects in a control buffer.
6213 *
6214 * |<--------------------------- msg_controllen -------------------------->|
6215 * | |
6216 *
6217 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
6218 *
6219 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
6220 * | | |
6221 *
6222 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
6223 *
6224 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
6225 * | | | | |
6226 *
6227 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6228 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
6229 *
6230 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
6231 *
6232 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6233 * ^
6234 * |
6235 *
6236 * msg_control
6237 * points here
6238 */
6239SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
6240 sctp_cmsgs_t *cmsgs)
6241{
6242 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006243 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 for (cmsg = CMSG_FIRSTHDR(msg);
6246 cmsg != NULL;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006247 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
6248 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 return -EINVAL;
6250
6251 /* Should we parse this header or ignore? */
6252 if (cmsg->cmsg_level != IPPROTO_SCTP)
6253 continue;
6254
6255 /* Strictly check lengths following example in SCM code. */
6256 switch (cmsg->cmsg_type) {
6257 case SCTP_INIT:
6258 /* SCTP Socket API Extension
6259 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
6260 *
6261 * This cmsghdr structure provides information for
6262 * initializing new SCTP associations with sendmsg().
6263 * The SCTP_INITMSG socket option uses this same data
6264 * structure. This structure is not used for
6265 * recvmsg().
6266 *
6267 * cmsg_level cmsg_type cmsg_data[]
6268 * ------------ ------------ ----------------------
6269 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
6270 */
6271 if (cmsg->cmsg_len !=
6272 CMSG_LEN(sizeof(struct sctp_initmsg)))
6273 return -EINVAL;
6274 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
6275 break;
6276
6277 case SCTP_SNDRCV:
6278 /* SCTP Socket API Extension
6279 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
6280 *
6281 * This cmsghdr structure specifies SCTP options for
6282 * sendmsg() and describes SCTP header information
6283 * about a received message through recvmsg().
6284 *
6285 * cmsg_level cmsg_type cmsg_data[]
6286 * ------------ ------------ ----------------------
6287 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
6288 */
6289 if (cmsg->cmsg_len !=
6290 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
6291 return -EINVAL;
6292
6293 cmsgs->info =
6294 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
6295
6296 /* Minimally, validate the sinfo_flags. */
6297 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07006298 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6299 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 return -EINVAL;
6301 break;
6302
6303 default:
6304 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 }
6307 return 0;
6308}
6309
6310/*
6311 * Wait for a packet..
6312 * Note: This function is the same function as in core/datagram.c
6313 * with a few modifications to make lksctp work.
6314 */
6315static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6316{
6317 int error;
6318 DEFINE_WAIT(wait);
6319
Eric Dumazetaa395142010-04-20 13:03:51 +00006320 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 /* Socket errors? */
6323 error = sock_error(sk);
6324 if (error)
6325 goto out;
6326
6327 if (!skb_queue_empty(&sk->sk_receive_queue))
6328 goto ready;
6329
6330 /* Socket shut down? */
6331 if (sk->sk_shutdown & RCV_SHUTDOWN)
6332 goto out;
6333
6334 /* Sequenced packets can come disconnected. If so we report the
6335 * problem.
6336 */
6337 error = -ENOTCONN;
6338
6339 /* Is there a good reason to think that we may receive some data? */
6340 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6341 goto out;
6342
6343 /* Handle signals. */
6344 if (signal_pending(current))
6345 goto interrupted;
6346
6347 /* Let another process have a go. Since we are going to sleep
6348 * anyway. Note: This may cause odd behaviors if the message
6349 * does not fit in the user's buffer, but this seems to be the
6350 * only way to honor MSG_DONTWAIT realistically.
6351 */
6352 sctp_release_sock(sk);
6353 *timeo_p = schedule_timeout(*timeo_p);
6354 sctp_lock_sock(sk);
6355
6356ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00006357 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 return 0;
6359
6360interrupted:
6361 error = sock_intr_errno(*timeo_p);
6362
6363out:
Eric Dumazetaa395142010-04-20 13:03:51 +00006364 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 *err = error;
6366 return error;
6367}
6368
6369/* Receive a datagram.
6370 * Note: This is pretty much the same routine as in core/datagram.c
6371 * with a few changes to make lksctp work.
6372 */
6373static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6374 int noblock, int *err)
6375{
6376 int error;
6377 struct sk_buff *skb;
6378 long timeo;
6379
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 timeo = sock_rcvtimeo(sk, noblock);
6381
6382 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6383 timeo, MAX_SCHEDULE_TIMEOUT);
6384
6385 do {
6386 /* Again only user level code calls this function,
6387 * so nothing interrupt level
6388 * will suddenly eat the receive_queue.
6389 *
6390 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00006391 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 */
6393 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07006394 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 skb = skb_peek(&sk->sk_receive_queue);
6396 if (skb)
6397 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07006398 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 } else {
6400 skb = skb_dequeue(&sk->sk_receive_queue);
6401 }
6402
6403 if (skb)
6404 return skb;
6405
Neil Horman6736dc32005-12-02 20:30:06 -08006406 /* Caller is allowed not to check sk->sk_err before calling. */
6407 error = sock_error(sk);
6408 if (error)
6409 goto no_packet;
6410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 if (sk->sk_shutdown & RCV_SHUTDOWN)
6412 break;
6413
6414 /* User doesn't want to wait. */
6415 error = -EAGAIN;
6416 if (!timeo)
6417 goto no_packet;
6418 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6419
6420 return NULL;
6421
6422no_packet:
6423 *err = error;
6424 return NULL;
6425}
6426
6427/* If sndbuf has changed, wake up per association sndbuf waiters. */
6428static void __sctp_write_space(struct sctp_association *asoc)
6429{
6430 struct sock *sk = asoc->base.sk;
6431 struct socket *sock = sk->sk_socket;
6432
6433 if ((sctp_wspace(asoc) > 0) && sock) {
6434 if (waitqueue_active(&asoc->wait))
6435 wake_up_interruptible(&asoc->wait);
6436
6437 if (sctp_writeable(sk)) {
Eric Dumazeteaefd112011-02-18 03:26:36 +00006438 wait_queue_head_t *wq = sk_sleep(sk);
6439
6440 if (wq && waitqueue_active(wq))
6441 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
6443 /* Note that we try to include the Async I/O support
6444 * here by modeling from the current TCP/UDP code.
6445 * We have not tested with it yet.
6446 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00006447 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08006448 sock_wake_async(sock,
6449 SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
6451 }
6452}
6453
6454/* Do accounting for the sndbuf space.
6455 * Decrement the used sndbuf space of the corresponding association by the
6456 * data size which was just transmitted(freed).
6457 */
6458static void sctp_wfree(struct sk_buff *skb)
6459{
6460 struct sctp_association *asoc;
6461 struct sctp_chunk *chunk;
6462 struct sock *sk;
6463
6464 /* Get the saved chunk pointer. */
6465 chunk = *((struct sctp_chunk **)(skb->cb));
6466 asoc = chunk->asoc;
6467 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006468 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6469 sizeof(struct sk_buff) +
6470 sizeof(struct sctp_chunk);
6471
Neil Horman4eb701d2005-04-28 12:02:04 -07006472 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6473
Neil Horman4d93df02007-08-15 16:07:44 -07006474 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006475 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07006476 */
6477 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006478 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07006479
Neil Horman4eb701d2005-04-28 12:02:04 -07006480 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 __sctp_write_space(asoc);
6482
6483 sctp_association_put(asoc);
6484}
6485
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006486/* Do accounting for the receive space on the socket.
6487 * Accounting for the association is done in ulpevent.c
6488 * We set this as a destructor for the cloned data skbs so that
6489 * accounting is done at the correct time.
6490 */
6491void sctp_sock_rfree(struct sk_buff *skb)
6492{
6493 struct sock *sk = skb->sk;
6494 struct sctp_ulpevent *event = sctp_skb2event(skb);
6495
6496 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006497
6498 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006499 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07006500 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006501 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006502}
6503
6504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505/* Helper function to wait for space in the sndbuf. */
6506static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6507 size_t msg_len)
6508{
6509 struct sock *sk = asoc->base.sk;
6510 int err = 0;
6511 long current_timeo = *timeo_p;
6512 DEFINE_WAIT(wait);
6513
6514 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006515 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
6517 /* Increment the association's refcnt. */
6518 sctp_association_hold(asoc);
6519
6520 /* Wait on the association specific sndbuf space. */
6521 for (;;) {
6522 prepare_to_wait_exclusive(&asoc->wait, &wait,
6523 TASK_INTERRUPTIBLE);
6524 if (!*timeo_p)
6525 goto do_nonblock;
6526 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6527 asoc->base.dead)
6528 goto do_error;
6529 if (signal_pending(current))
6530 goto do_interrupted;
6531 if (msg_len <= sctp_wspace(asoc))
6532 break;
6533
6534 /* Let another process have a go. Since we are going
6535 * to sleep anyway.
6536 */
6537 sctp_release_sock(sk);
6538 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006539 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 sctp_lock_sock(sk);
6541
6542 *timeo_p = current_timeo;
6543 }
6544
6545out:
6546 finish_wait(&asoc->wait, &wait);
6547
6548 /* Release the association's refcnt. */
6549 sctp_association_put(asoc);
6550
6551 return err;
6552
6553do_error:
6554 err = -EPIPE;
6555 goto out;
6556
6557do_interrupted:
6558 err = sock_intr_errno(*timeo_p);
6559 goto out;
6560
6561do_nonblock:
6562 err = -EAGAIN;
6563 goto out;
6564}
6565
Wei Yongjun561b1732010-04-28 08:47:18 +00006566void sctp_data_ready(struct sock *sk, int len)
6567{
David S. Miller7ef52732010-05-02 21:43:40 -07006568 struct socket_wq *wq;
6569
6570 rcu_read_lock();
6571 wq = rcu_dereference(sk->sk_wq);
6572 if (wq_has_sleeper(wq))
6573 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00006574 POLLRDNORM | POLLRDBAND);
6575 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07006576 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00006577}
6578
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579/* If socket sndbuf has changed, wake up all per association waiters. */
6580void sctp_write_space(struct sock *sk)
6581{
6582 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
6584 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07006585 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 __sctp_write_space(asoc);
6587 }
6588}
6589
6590/* Is there any sndbuf space available on the socket?
6591 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006592 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 * associations on the same socket. For a UDP-style socket with
6594 * multiple associations, it is possible for it to be "unwriteable"
6595 * prematurely. I assume that this is acceptable because
6596 * a premature "unwriteable" is better than an accidental "writeable" which
6597 * would cause an unwanted block under certain circumstances. For the 1-1
6598 * UDP-style sockets or TCP-style sockets, this code should work.
6599 * - Daisy
6600 */
6601static int sctp_writeable(struct sock *sk)
6602{
6603 int amt = 0;
6604
Eric Dumazet31e6d362009-06-17 19:05:41 -07006605 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 if (amt < 0)
6607 amt = 0;
6608 return amt;
6609}
6610
6611/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6612 * returns immediately with EINPROGRESS.
6613 */
6614static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6615{
6616 struct sock *sk = asoc->base.sk;
6617 int err = 0;
6618 long current_timeo = *timeo_p;
6619 DEFINE_WAIT(wait);
6620
Harvey Harrison0dc47872008-03-05 20:47:47 -08006621 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 (long)(*timeo_p));
6623
6624 /* Increment the association's refcnt. */
6625 sctp_association_hold(asoc);
6626
6627 for (;;) {
6628 prepare_to_wait_exclusive(&asoc->wait, &wait,
6629 TASK_INTERRUPTIBLE);
6630 if (!*timeo_p)
6631 goto do_nonblock;
6632 if (sk->sk_shutdown & RCV_SHUTDOWN)
6633 break;
6634 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6635 asoc->base.dead)
6636 goto do_error;
6637 if (signal_pending(current))
6638 goto do_interrupted;
6639
6640 if (sctp_state(asoc, ESTABLISHED))
6641 break;
6642
6643 /* Let another process have a go. Since we are going
6644 * to sleep anyway.
6645 */
6646 sctp_release_sock(sk);
6647 current_timeo = schedule_timeout(current_timeo);
6648 sctp_lock_sock(sk);
6649
6650 *timeo_p = current_timeo;
6651 }
6652
6653out:
6654 finish_wait(&asoc->wait, &wait);
6655
6656 /* Release the association's refcnt. */
6657 sctp_association_put(asoc);
6658
6659 return err;
6660
6661do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006662 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 err = -ETIMEDOUT;
6664 else
6665 err = -ECONNREFUSED;
6666 goto out;
6667
6668do_interrupted:
6669 err = sock_intr_errno(*timeo_p);
6670 goto out;
6671
6672do_nonblock:
6673 err = -EINPROGRESS;
6674 goto out;
6675}
6676
6677static int sctp_wait_for_accept(struct sock *sk, long timeo)
6678{
6679 struct sctp_endpoint *ep;
6680 int err = 0;
6681 DEFINE_WAIT(wait);
6682
6683 ep = sctp_sk(sk)->ep;
6684
6685
6686 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00006687 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 TASK_INTERRUPTIBLE);
6689
6690 if (list_empty(&ep->asocs)) {
6691 sctp_release_sock(sk);
6692 timeo = schedule_timeout(timeo);
6693 sctp_lock_sock(sk);
6694 }
6695
6696 err = -EINVAL;
6697 if (!sctp_sstate(sk, LISTENING))
6698 break;
6699
6700 err = 0;
6701 if (!list_empty(&ep->asocs))
6702 break;
6703
6704 err = sock_intr_errno(timeo);
6705 if (signal_pending(current))
6706 break;
6707
6708 err = -EAGAIN;
6709 if (!timeo)
6710 break;
6711 }
6712
Eric Dumazetaa395142010-04-20 13:03:51 +00006713 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
6715 return err;
6716}
6717
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006718static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719{
6720 DEFINE_WAIT(wait);
6721
6722 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00006723 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 if (list_empty(&sctp_sk(sk)->ep->asocs))
6725 break;
6726 sctp_release_sock(sk);
6727 timeout = schedule_timeout(timeout);
6728 sctp_lock_sock(sk);
6729 } while (!signal_pending(current) && timeout);
6730
Eric Dumazetaa395142010-04-20 13:03:51 +00006731 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732}
6733
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006734static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6735{
6736 struct sk_buff *frag;
6737
6738 if (!skb->data_len)
6739 goto done;
6740
6741 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07006742 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006743 sctp_skb_set_owner_r_frag(frag, sk);
6744
6745done:
6746 sctp_skb_set_owner_r(skb, sk);
6747}
6748
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006749void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6750 struct sctp_association *asoc)
6751{
6752 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08006753 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006754
6755 newsk->sk_type = sk->sk_type;
6756 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6757 newsk->sk_flags = sk->sk_flags;
6758 newsk->sk_no_check = sk->sk_no_check;
6759 newsk->sk_reuse = sk->sk_reuse;
6760
6761 newsk->sk_shutdown = sk->sk_shutdown;
6762 newsk->sk_destruct = inet_sock_destruct;
6763 newsk->sk_family = sk->sk_family;
6764 newsk->sk_protocol = IPPROTO_SCTP;
6765 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6766 newsk->sk_sndbuf = sk->sk_sndbuf;
6767 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6768 newsk->sk_lingertime = sk->sk_lingertime;
6769 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6770 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6771
6772 newinet = inet_sk(newsk);
6773
6774 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6775 * getsockname() and getpeername()
6776 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006777 newinet->inet_sport = inet->inet_sport;
6778 newinet->inet_saddr = inet->inet_saddr;
6779 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6780 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006781 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006782 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006783
6784 newinet->uc_ttl = inet->uc_ttl;
6785 newinet->mc_loop = 1;
6786 newinet->mc_ttl = 1;
6787 newinet->mc_index = 0;
6788 newinet->mc_list = NULL;
6789}
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791/* Populate the fields of the newsk from the oldsk and migrate the assoc
6792 * and its messages to the newsk.
6793 */
6794static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6795 struct sctp_association *assoc,
6796 sctp_socket_type_t type)
6797{
6798 struct sctp_sock *oldsp = sctp_sk(oldsk);
6799 struct sctp_sock *newsp = sctp_sk(newsk);
6800 struct sctp_bind_bucket *pp; /* hash list port iterator */
6801 struct sctp_endpoint *newep = newsp->ep;
6802 struct sk_buff *skb, *tmp;
6803 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006804 struct sctp_bind_hashbucket *head;
Michio Honda9f7d6532011-04-26 19:32:51 +09006805 struct list_head tmplist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
6807 /* Migrate socket buffer sizes and all the socket level options to the
6808 * new socket.
6809 */
6810 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6811 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6812 /* Brute force copy old sctp opt. */
Michio Honda9f7d6532011-04-26 19:32:51 +09006813 if (oldsp->do_auto_asconf) {
6814 memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist));
6815 inet_sk_copy_descendant(newsk, oldsk);
6816 memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist));
6817 } else
6818 inet_sk_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
6820 /* Restore the ep value that was overwritten with the above structure
6821 * copy.
6822 */
6823 newsp->ep = newep;
6824 newsp->hmac = NULL;
6825
6826 /* Hook this new socket in to the bind_hash list. */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006827 head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)];
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006828 sctp_local_bh_disable();
6829 sctp_spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 pp = sctp_sk(oldsk)->bind_hash;
6831 sk_add_bind_node(newsk, &pp->owner);
6832 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006833 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006834 sctp_spin_unlock(&head->lock);
6835 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006837 /* Copy the bind_addr list from the original endpoint to the new
6838 * endpoint so that we can handle restarts properly
6839 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08006840 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6841 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006842
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 /* Move any messages in the old socket's receive queue that are for the
6844 * peeled off association to the new socket's receive queue.
6845 */
6846 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6847 event = sctp_skb2event(skb);
6848 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006849 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006851 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
6853 }
6854
6855 /* Clean up any messages pending delivery due to partial
6856 * delivery. Three cases:
6857 * 1) No partial deliver; no work.
6858 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6859 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6860 */
6861 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006862 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006864 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 struct sk_buff_head *queue;
6866
6867 /* Decide which queue to move pd_lobby skbs to. */
6868 if (assoc->ulpq.pd_mode) {
6869 queue = &newsp->pd_lobby;
6870 } else
6871 queue = &newsk->sk_receive_queue;
6872
6873 /* Walk through the pd_lobby, looking for skbs that
6874 * need moved to the new socket.
6875 */
6876 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6877 event = sctp_skb2event(skb);
6878 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006879 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006881 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 }
6883 }
6884
6885 /* Clear up any skbs waiting for the partial
6886 * delivery to finish.
6887 */
6888 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006889 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
6891 }
6892
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006893 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006894 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006895
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006896 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006897 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006898
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 /* Set the type of socket to indicate that it is peeled off from the
6900 * original UDP-style socket or created with the accept() call on a
6901 * TCP-style socket..
6902 */
6903 newsp->type = type;
6904
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006905 /* Mark the new socket "in-use" by the user so that any packets
6906 * that may arrive on the association after we've moved it are
6907 * queued to the backlog. This prevents a potential race between
6908 * backlog processing on the old socket and new-packet processing
6909 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006910 *
6911 * The caller has just allocated newsk so we can guarantee that other
6912 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006913 */
Zach Brown5131a182007-06-22 15:14:46 -07006914 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
6917 /* If the association on the newsk is already closed before accept()
6918 * is called, set RCV_SHUTDOWN flag.
6919 */
6920 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6921 newsk->sk_shutdown |= RCV_SHUTDOWN;
6922
6923 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006924 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925}
6926
Neil Horman4d93df02007-08-15 16:07:44 -07006927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928/* This proto struct describes the ULP interface for SCTP. */
6929struct proto sctp_prot = {
6930 .name = "SCTP",
6931 .owner = THIS_MODULE,
6932 .close = sctp_close,
6933 .connect = sctp_connect,
6934 .disconnect = sctp_disconnect,
6935 .accept = sctp_accept,
6936 .ioctl = sctp_ioctl,
6937 .init = sctp_init_sock,
6938 .destroy = sctp_destroy_sock,
6939 .shutdown = sctp_shutdown,
6940 .setsockopt = sctp_setsockopt,
6941 .getsockopt = sctp_getsockopt,
6942 .sendmsg = sctp_sendmsg,
6943 .recvmsg = sctp_recvmsg,
6944 .bind = sctp_bind,
6945 .backlog_rcv = sctp_backlog_rcv,
6946 .hash = sctp_hash,
6947 .unhash = sctp_unhash,
6948 .get_port = sctp_get_port,
6949 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006950 .sysctl_mem = sysctl_sctp_mem,
6951 .sysctl_rmem = sysctl_sctp_rmem,
6952 .sysctl_wmem = sysctl_sctp_wmem,
6953 .memory_pressure = &sctp_memory_pressure,
6954 .enter_memory_pressure = sctp_enter_memory_pressure,
6955 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006956 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957};
6958
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006959#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961struct proto sctpv6_prot = {
6962 .name = "SCTPv6",
6963 .owner = THIS_MODULE,
6964 .close = sctp_close,
6965 .connect = sctp_connect,
6966 .disconnect = sctp_disconnect,
6967 .accept = sctp_accept,
6968 .ioctl = sctp_ioctl,
6969 .init = sctp_init_sock,
6970 .destroy = sctp_destroy_sock,
6971 .shutdown = sctp_shutdown,
6972 .setsockopt = sctp_setsockopt,
6973 .getsockopt = sctp_getsockopt,
6974 .sendmsg = sctp_sendmsg,
6975 .recvmsg = sctp_recvmsg,
6976 .bind = sctp_bind,
6977 .backlog_rcv = sctp_backlog_rcv,
6978 .hash = sctp_hash,
6979 .unhash = sctp_unhash,
6980 .get_port = sctp_get_port,
6981 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006982 .sysctl_mem = sysctl_sctp_mem,
6983 .sysctl_rmem = sysctl_sctp_rmem,
6984 .sysctl_wmem = sysctl_sctp_wmem,
6985 .memory_pressure = &sctp_memory_pressure,
6986 .enter_memory_pressure = sctp_enter_memory_pressure,
6987 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006988 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006990#endif /* IS_ENABLED(CONFIG_IPV6) */