blob: a60d1f8b41c5e2330e837265e175202aba322fcb [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{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000431 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int retval = 0;
433
434 /* If there is an outstanding ASCONF chunk, queue it for later
435 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700438 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 /* Hold the chunk until an ASCONF_ACK is received. */
443 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000444 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (retval)
446 sctp_chunk_free(chunk);
447 else
448 asoc->addip_last_asconf = chunk;
449
450out:
451 return retval;
452}
453
454/* Add a list of addresses as bind addresses to local endpoint or
455 * association.
456 *
457 * Basically run through each address specified in the addrs/addrcnt
458 * array/length pair, determine if it is IPv6 or IPv4 and call
459 * sctp_do_bind() on it.
460 *
461 * If any of them fails, then the operation will be reversed and the
462 * ones that were added will be removed.
463 *
464 * Only sctp_setsockopt_bindx() is supposed to call this function.
465 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200466static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int cnt;
469 int retval = 0;
470 void *addr_buf;
471 struct sockaddr *sa_addr;
472 struct sctp_af *af;
473
474 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
475 sk, addrs, addrcnt);
476
477 addr_buf = addrs;
478 for (cnt = 0; cnt < addrcnt; cnt++) {
479 /* The list may contain either IPv4 or IPv6 address;
480 * determine the address length for walking thru the list.
481 */
Joe Perchesea110732011-06-13 16:21:26 +0000482 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 af = sctp_get_af_specific(sa_addr->sa_family);
484 if (!af) {
485 retval = -EINVAL;
486 goto err_bindx_add;
487 }
488
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900489 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 af->sockaddr_len);
491
492 addr_buf += af->sockaddr_len;
493
494err_bindx_add:
495 if (retval < 0) {
496 /* Failed. Cleanup the ones that have been added */
497 if (cnt > 0)
498 sctp_bindx_rem(sk, addrs, cnt);
499 return retval;
500 }
501 }
502
503 return retval;
504}
505
506/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
507 * associations that are part of the endpoint indicating that a list of local
508 * addresses are added to the endpoint.
509 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900510 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * association, we do not send the chunk for that association. But it will not
512 * affect other associations.
513 *
514 * Only sctp_setsockopt_bindx() is supposed to call this function.
515 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900516static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct sockaddr *addrs,
518 int addrcnt)
519{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000520 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct sctp_sock *sp;
522 struct sctp_endpoint *ep;
523 struct sctp_association *asoc;
524 struct sctp_bind_addr *bp;
525 struct sctp_chunk *chunk;
526 struct sctp_sockaddr_entry *laddr;
527 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700528 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 void *addr_buf;
530 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct list_head *p;
532 int i;
533 int retval = 0;
534
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000535 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return retval;
537
538 sp = sctp_sk(sk);
539 ep = sp->ep;
540
541 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800542 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700544 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (!asoc->peer.asconf_capable)
547 continue;
548
549 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
550 continue;
551
552 if (!sctp_state(asoc, ESTABLISHED))
553 continue;
554
555 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900556 * in the bind address list of the association. If so,
557 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * other associations.
559 */
560 addr_buf = addrs;
561 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000562 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 af = sctp_get_af_specific(addr->v4.sin_family);
564 if (!af) {
565 retval = -EINVAL;
566 goto out;
567 }
568
569 if (sctp_assoc_lookup_laddr(asoc, addr))
570 break;
571
572 addr_buf += af->sockaddr_len;
573 }
574 if (i < addrcnt)
575 continue;
576
Vlad Yasevich559cf712007-09-16 16:03:28 -0700577 /* Use the first valid address in bind addr list of
578 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 bp = &asoc->base.bind_addr;
581 p = bp->address_list.next;
582 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800583 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 addrcnt, SCTP_PARAM_ADD_IP);
585 if (!chunk) {
586 retval = -ENOMEM;
587 goto out;
588 }
589
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700590 /* Add the new addresses to the bind address list with
591 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700593 addr_buf = addrs;
594 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000595 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700596 af = sctp_get_af_specific(addr->v4.sin_family);
597 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800598 retval = sctp_add_bind_addr(bp, &saveaddr,
599 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700600 addr_buf += af->sockaddr_len;
601 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900602 if (asoc->src_out_of_asoc_ok) {
603 struct sctp_transport *trans;
604
605 list_for_each_entry(trans,
606 &asoc->peer.transport_addr_list, transports) {
607 /* Clear the source and route cache */
608 dst_release(trans->dst);
609 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
610 2*asoc->pathmtu, 4380));
611 trans->ssthresh = asoc->peer.i.a_rwnd;
612 trans->rto = asoc->rto_initial;
613 trans->rtt = trans->srtt = trans->rttvar = 0;
614 sctp_transport_route(trans, NULL,
615 sctp_sk(asoc->base.sk));
616 }
617 }
618 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621out:
622 return retval;
623}
624
625/* Remove a list of addresses from bind addresses list. Do not remove the
626 * last address.
627 *
628 * Basically run through each address specified in the addrs/addrcnt
629 * array/length pair, determine if it is IPv6 or IPv4 and call
630 * sctp_del_bind() on it.
631 *
632 * If any of them fails, then the operation will be reversed and the
633 * ones that were removed will be added back.
634 *
635 * At least one address has to be left; if only one address is
636 * available, the operation will return -EBUSY.
637 *
638 * Only sctp_setsockopt_bindx() is supposed to call this function.
639 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200640static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct sctp_sock *sp = sctp_sk(sk);
643 struct sctp_endpoint *ep = sp->ep;
644 int cnt;
645 struct sctp_bind_addr *bp = &ep->base.bind_addr;
646 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800648 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct sctp_af *af;
650
651 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
652 sk, addrs, addrcnt);
653
654 addr_buf = addrs;
655 for (cnt = 0; cnt < addrcnt; cnt++) {
656 /* If the bind address list is empty or if there is only one
657 * bind address, there is nothing more to be removed (we need
658 * at least one address here).
659 */
660 if (list_empty(&bp->address_list) ||
661 (sctp_list_single_entry(&bp->address_list))) {
662 retval = -EBUSY;
663 goto err_bindx_rem;
664 }
665
Joe Perchesea110732011-06-13 16:21:26 +0000666 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800667 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!af) {
669 retval = -EINVAL;
670 goto err_bindx_rem;
671 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700672
673 if (!af->addr_valid(sa_addr, sp, NULL)) {
674 retval = -EADDRNOTAVAIL;
675 goto err_bindx_rem;
676 }
677
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000678 if (sa_addr->v4.sin_port &&
679 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 retval = -EINVAL;
681 goto err_bindx_rem;
682 }
683
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000684 if (!sa_addr->v4.sin_port)
685 sa_addr->v4.sin_port = htons(bp->port);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* FIXME - There is probably a need to check if sk->sk_saddr and
688 * sk->sk_rcv_addr are currently set to one of the addresses to
689 * be removed. This is something which needs to be looked into
690 * when we are fixing the outstanding issues with multi-homing
691 * socket routing and failover schemes. Refer to comments in
692 * sctp_do_bind(). -daisy
693 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400694 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 addr_buf += af->sockaddr_len;
697err_bindx_rem:
698 if (retval < 0) {
699 /* Failed. Add the ones that has been removed back */
700 if (cnt > 0)
701 sctp_bindx_add(sk, addrs, cnt);
702 return retval;
703 }
704 }
705
706 return retval;
707}
708
709/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
710 * the associations that are part of the endpoint indicating that a list of
711 * local addresses are removed from the endpoint.
712 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900713 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * association, we do not send the chunk for that association. But it will not
715 * affect other associations.
716 *
717 * Only sctp_setsockopt_bindx() is supposed to call this function.
718 */
719static int sctp_send_asconf_del_ip(struct sock *sk,
720 struct sockaddr *addrs,
721 int addrcnt)
722{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000723 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct sctp_sock *sp;
725 struct sctp_endpoint *ep;
726 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700727 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct sctp_bind_addr *bp;
729 struct sctp_chunk *chunk;
730 union sctp_addr *laddr;
731 void *addr_buf;
732 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700733 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int i;
735 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900736 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Michio Honda8a07eb02011-04-26 20:19:36 +0900738 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000739 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return retval;
741
742 sp = sctp_sk(sk);
743 ep = sp->ep;
744
745 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800746 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700748 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (!asoc->peer.asconf_capable)
751 continue;
752
753 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
754 continue;
755
756 if (!sctp_state(asoc, ESTABLISHED))
757 continue;
758
759 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900760 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * If so, do not send the asconf chunk to its peer, but
762 * continue with other associations.
763 */
764 addr_buf = addrs;
765 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000766 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 af = sctp_get_af_specific(laddr->v4.sin_family);
768 if (!af) {
769 retval = -EINVAL;
770 goto out;
771 }
772
773 if (!sctp_assoc_lookup_laddr(asoc, laddr))
774 break;
775
776 addr_buf += af->sockaddr_len;
777 }
778 if (i < addrcnt)
779 continue;
780
781 /* Find one address in the association's bind address list
782 * that is not in the packed array of addresses. This is to
783 * make sure that we do not delete all the addresses in the
784 * association.
785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 bp = &asoc->base.bind_addr;
787 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
788 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900789 if ((laddr == NULL) && (addrcnt == 1)) {
790 if (asoc->asconf_addr_del_pending)
791 continue;
792 asoc->asconf_addr_del_pending =
793 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900794 if (asoc->asconf_addr_del_pending == NULL) {
795 retval = -ENOMEM;
796 goto out;
797 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900798 asoc->asconf_addr_del_pending->sa.sa_family =
799 addrs->sa_family;
800 asoc->asconf_addr_del_pending->v4.sin_port =
801 htons(bp->port);
802 if (addrs->sa_family == AF_INET) {
803 struct sockaddr_in *sin;
804
805 sin = (struct sockaddr_in *)addrs;
806 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
807 } else if (addrs->sa_family == AF_INET6) {
808 struct sockaddr_in6 *sin6;
809
810 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000811 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900812 }
813 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
814 " at %p\n", asoc, asoc->asconf_addr_del_pending,
815 asoc->asconf_addr_del_pending);
816 asoc->src_out_of_asoc_ok = 1;
817 stored = 1;
818 goto skip_mkasconf;
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Vlad Yasevich559cf712007-09-16 16:03:28 -0700821 /* We do not need RCU protection throughout this loop
822 * because this is done under a socket lock from the
823 * setsockopt call.
824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
826 SCTP_PARAM_DEL_IP);
827 if (!chunk) {
828 retval = -ENOMEM;
829 goto out;
830 }
831
Michio Honda8a07eb02011-04-26 20:19:36 +0900832skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700833 /* Reset use_as_src flag for the addresses in the bind address
834 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700836 addr_buf = addrs;
837 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000838 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700839 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700840 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800841 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800842 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700843 }
844 addr_buf += af->sockaddr_len;
845 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700846
847 /* Update the route and saddr entries for all the transports
848 * as some of the addresses in the bind address list are
849 * about to be deleted and cannot be used as source addresses.
850 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700851 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
852 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700853 dst_release(transport->dst);
854 sctp_transport_route(transport, NULL,
855 sctp_sk(asoc->base.sk));
856 }
857
Michio Honda8a07eb02011-04-26 20:19:36 +0900858 if (stored)
859 /* We don't need to transmit ASCONF */
860 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700861 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863out:
864 return retval;
865}
866
Michio Honda9f7d6532011-04-26 19:32:51 +0900867/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
868int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
869{
870 struct sock *sk = sctp_opt2sk(sp);
871 union sctp_addr *addr;
872 struct sctp_af *af;
873
874 /* It is safe to write port space in caller. */
875 addr = &addrw->a;
876 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
877 af = sctp_get_af_specific(addr->sa.sa_family);
878 if (!af)
879 return -EINVAL;
880 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
881 return -EINVAL;
882
883 if (addrw->state == SCTP_ADDR_NEW)
884 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
885 else
886 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
890 *
891 * API 8.1
892 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
893 * int flags);
894 *
895 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
896 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
897 * or IPv6 addresses.
898 *
899 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
900 * Section 3.1.2 for this usage.
901 *
902 * addrs is a pointer to an array of one or more socket addresses. Each
903 * address is contained in its appropriate structure (i.e. struct
904 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700905 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * representation is termed a "packed array" of addresses). The caller
907 * specifies the number of addresses in the array with addrcnt.
908 *
909 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
910 * -1, and sets errno to the appropriate error code.
911 *
912 * For SCTP, the port given in each socket address must be the same, or
913 * sctp_bindx() will fail, setting errno to EINVAL.
914 *
915 * The flags parameter is formed from the bitwise OR of zero or more of
916 * the following currently defined flags:
917 *
918 * SCTP_BINDX_ADD_ADDR
919 *
920 * SCTP_BINDX_REM_ADDR
921 *
922 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
923 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
924 * addresses from the association. The two flags are mutually exclusive;
925 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
926 * not remove all addresses from an association; sctp_bindx() will
927 * reject such an attempt with EINVAL.
928 *
929 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
930 * additional addresses with an endpoint after calling bind(). Or use
931 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
932 * socket is associated with so that no new association accepted will be
933 * associated with those addresses. If the endpoint supports dynamic
934 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
935 * endpoint to send the appropriate message to the peer to change the
936 * peers address lists.
937 *
938 * Adding and removing addresses from a connected association is
939 * optional functionality. Implementations that do not support this
940 * functionality should return EOPNOTSUPP.
941 *
942 * Basically do nothing but copying the addresses from user to kernel
943 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700944 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
945 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
947 * We don't use copy_from_user() for optimization: we first do the
948 * sanity checks (buffer size -fast- and access check-healthy
949 * pointer); if all of those succeed, then we can alloc the memory
950 * (expensive operation) needed to copy the data to kernel. Then we do
951 * the copying without checking the user space area
952 * (__copy_from_user()).
953 *
954 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
955 * it.
956 *
957 * sk The sk of the socket
958 * addrs The pointer to the addresses in user land
959 * addrssize Size of the addrs buffer
960 * op Operation to perform (add or remove, see the flags of
961 * sctp_bindx)
962 *
963 * Returns 0 if ok, <0 errno code on error.
964 */
965SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
966 struct sockaddr __user *addrs,
967 int addrs_size, int op)
968{
969 struct sockaddr *kaddrs;
970 int err;
971 int addrcnt = 0;
972 int walk_size = 0;
973 struct sockaddr *sa_addr;
974 void *addr_buf;
975 struct sctp_af *af;
976
Masanari Iidad3e9a1d2012-10-31 05:48:19 +0000977 SCTP_DEBUG_PRINTK("sctp_setsockopt_bindx: sk %p addrs %p"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
979
980 if (unlikely(addrs_size <= 0))
981 return -EINVAL;
982
983 /* Check the user passed a healthy pointer. */
984 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
985 return -EFAULT;
986
987 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800988 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (unlikely(!kaddrs))
990 return -ENOMEM;
991
992 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
993 kfree(kaddrs);
994 return -EFAULT;
995 }
996
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900997 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 addr_buf = kaddrs;
999 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001000 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1001 kfree(kaddrs);
1002 return -EINVAL;
1003 }
1004
Joe Perchesea110732011-06-13 16:21:26 +00001005 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 af = sctp_get_af_specific(sa_addr->sa_family);
1007
1008 /* If the address family is not supported or if this address
1009 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1012 kfree(kaddrs);
1013 return -EINVAL;
1014 }
1015 addrcnt++;
1016 addr_buf += af->sockaddr_len;
1017 walk_size += af->sockaddr_len;
1018 }
1019
1020 /* Do the work. */
1021 switch (op) {
1022 case SCTP_BINDX_ADD_ADDR:
1023 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1024 if (err)
1025 goto out;
1026 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1027 break;
1028
1029 case SCTP_BINDX_REM_ADDR:
1030 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1031 if (err)
1032 goto out;
1033 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1034 break;
1035
1036 default:
1037 err = -EINVAL;
1038 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041out:
1042 kfree(kaddrs);
1043
1044 return err;
1045}
1046
Frank Filz3f7a87d2005-06-20 13:14:57 -07001047/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1048 *
1049 * Common routine for handling connect() and sctp_connectx().
1050 * Connect will come in with just a single address.
1051 */
1052static int __sctp_connect(struct sock* sk,
1053 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001054 int addrs_size,
1055 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001056{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001057 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001058 struct sctp_sock *sp;
1059 struct sctp_endpoint *ep;
1060 struct sctp_association *asoc = NULL;
1061 struct sctp_association *asoc2;
1062 struct sctp_transport *transport;
1063 union sctp_addr to;
1064 struct sctp_af *af;
1065 sctp_scope_t scope;
1066 long timeo;
1067 int err = 0;
1068 int addrcnt = 0;
1069 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001070 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001071 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001072 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001073 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001074
1075 sp = sctp_sk(sk);
1076 ep = sp->ep;
1077
1078 /* connect() cannot be done on a socket that is already in ESTABLISHED
1079 * state - UDP-style peeled off socket or a TCP-style socket that
1080 * is already connected.
1081 * It cannot be done even on a TCP-style listening socket.
1082 */
1083 if (sctp_sstate(sk, ESTABLISHED) ||
1084 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1085 err = -EISCONN;
1086 goto out_free;
1087 }
1088
1089 /* Walk through the addrs buffer and count the number of addresses. */
1090 addr_buf = kaddrs;
1091 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001092 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1093 err = -EINVAL;
1094 goto out_free;
1095 }
1096
Joe Perchesea110732011-06-13 16:21:26 +00001097 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001098 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001099
1100 /* If the address family is not supported or if this address
1101 * causes the address buffer to overflow return EINVAL.
1102 */
1103 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1104 err = -EINVAL;
1105 goto out_free;
1106 }
1107
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001108 port = ntohs(sa_addr->v4.sin_port);
1109
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001110 /* Save current address so we can work with it */
1111 memcpy(&to, sa_addr, af->sockaddr_len);
1112
1113 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001114 if (err)
1115 goto out_free;
1116
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001117 /* Make sure the destination port is correctly set
1118 * in all addresses.
1119 */
1120 if (asoc && asoc->peer.port && asoc->peer.port != port)
1121 goto out_free;
1122
Frank Filz3f7a87d2005-06-20 13:14:57 -07001123
1124 /* Check if there already is a matching association on the
1125 * endpoint (other than the one created here).
1126 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001127 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001128 if (asoc2 && asoc2 != asoc) {
1129 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1130 err = -EISCONN;
1131 else
1132 err = -EALREADY;
1133 goto out_free;
1134 }
1135
1136 /* If we could not find a matching association on the endpoint,
1137 * make sure that there is no peeled-off association matching
1138 * the peer address even on another socket.
1139 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001140 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001141 err = -EADDRNOTAVAIL;
1142 goto out_free;
1143 }
1144
1145 if (!asoc) {
1146 /* If a bind() or sctp_bindx() is not called prior to
1147 * an sctp_connectx() call, the system picks an
1148 * ephemeral port and will choose an address set
1149 * equivalent to binding with a wildcard address.
1150 */
1151 if (!ep->base.bind_addr.port) {
1152 if (sctp_autobind(sk)) {
1153 err = -EAGAIN;
1154 goto out_free;
1155 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001156 } else {
1157 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001158 * If an unprivileged user inherits a 1-many
1159 * style socket with open associations on a
1160 * privileged port, it MAY be permitted to
1161 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001162 * be permitted to open new associations.
1163 */
1164 if (ep->base.bind_addr.port < PROT_SOCK &&
1165 !capable(CAP_NET_BIND_SERVICE)) {
1166 err = -EACCES;
1167 goto out_free;
1168 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 }
1170
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001171 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001172 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1173 if (!asoc) {
1174 err = -ENOMEM;
1175 goto out_free;
1176 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001177
1178 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1179 GFP_KERNEL);
1180 if (err < 0) {
1181 goto out_free;
1182 }
1183
Frank Filz3f7a87d2005-06-20 13:14:57 -07001184 }
1185
1186 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001187 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001188 SCTP_UNKNOWN);
1189 if (!transport) {
1190 err = -ENOMEM;
1191 goto out_free;
1192 }
1193
1194 addrcnt++;
1195 addr_buf += af->sockaddr_len;
1196 walk_size += af->sockaddr_len;
1197 }
1198
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001199 /* In case the user of sctp_connectx() wants an association
1200 * id back, assign one now.
1201 */
1202 if (assoc_id) {
1203 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1204 if (err < 0)
1205 goto out_free;
1206 }
1207
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001208 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001209 if (err < 0) {
1210 goto out_free;
1211 }
1212
1213 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001214 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001215 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1216 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001217 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001218
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001219 /* in-kernel sockets don't generally have a file allocated to them
1220 * if all they do is call sock_create_kern().
1221 */
1222 if (sk->sk_socket->file)
1223 f_flags = sk->sk_socket->file->f_flags;
1224
1225 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1226
Frank Filz3f7a87d2005-06-20 13:14:57 -07001227 err = sctp_wait_for_connect(asoc, &timeo);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001228 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001229 *assoc_id = asoc->assoc_id;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001230
1231 /* Don't free association on exit. */
1232 asoc = NULL;
1233
1234out_free:
1235
1236 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001237 " kaddrs: %p err: %d\n",
1238 asoc, kaddrs, err);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001239 if (asoc) {
1240 /* sctp_primitive_ASSOCIATE may have added this association
1241 * To the hash table, try to unhash it, just in case, its a noop
1242 * if it wasn't hashed so we're safe
1243 */
1244 sctp_unhash_established(asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001245 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001246 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001247 return err;
1248}
1249
1250/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1251 *
1252 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001253 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1254 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001255 *
1256 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1257 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1258 * or IPv6 addresses.
1259 *
1260 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1261 * Section 3.1.2 for this usage.
1262 *
1263 * addrs is a pointer to an array of one or more socket addresses. Each
1264 * address is contained in its appropriate structure (i.e. struct
1265 * sockaddr_in or struct sockaddr_in6) the family of the address type
1266 * must be used to distengish the address length (note that this
1267 * representation is termed a "packed array" of addresses). The caller
1268 * specifies the number of addresses in the array with addrcnt.
1269 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001270 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1271 * the association id of the new association. On failure, sctp_connectx()
1272 * returns -1, and sets errno to the appropriate error code. The assoc_id
1273 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001274 *
1275 * For SCTP, the port given in each socket address must be the same, or
1276 * sctp_connectx() will fail, setting errno to EINVAL.
1277 *
1278 * An application can use sctp_connectx to initiate an association with
1279 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1280 * allows a caller to specify multiple addresses at which a peer can be
1281 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001282 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001283 * specifies that the stack will try to make use of all the addresses in
1284 * the list when needed.
1285 *
1286 * Note that the list of addresses passed in is only used for setting up
1287 * the association. It does not necessarily equal the set of addresses
1288 * the peer uses for the resulting association. If the caller wants to
1289 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1290 * retrieve them after the association has been set up.
1291 *
1292 * Basically do nothing but copying the addresses from user to kernel
1293 * land and invoking either sctp_connectx(). This is used for tunneling
1294 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1295 *
1296 * We don't use copy_from_user() for optimization: we first do the
1297 * sanity checks (buffer size -fast- and access check-healthy
1298 * pointer); if all of those succeed, then we can alloc the memory
1299 * (expensive operation) needed to copy the data to kernel. Then we do
1300 * the copying without checking the user space area
1301 * (__copy_from_user()).
1302 *
1303 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1304 * it.
1305 *
1306 * sk The sk of the socket
1307 * addrs The pointer to the addresses in user land
1308 * addrssize Size of the addrs buffer
1309 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001310 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001311 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001312SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001313 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001314 int addrs_size,
1315 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001316{
1317 int err = 0;
1318 struct sockaddr *kaddrs;
1319
1320 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001321 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001322
1323 if (unlikely(addrs_size <= 0))
1324 return -EINVAL;
1325
1326 /* Check the user passed a healthy pointer. */
1327 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1328 return -EFAULT;
1329
1330 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001331 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001332 if (unlikely(!kaddrs))
1333 return -ENOMEM;
1334
1335 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1336 err = -EFAULT;
1337 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001338 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339 }
1340
1341 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001342
Frank Filz3f7a87d2005-06-20 13:14:57 -07001343 return err;
1344}
1345
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001346/*
1347 * This is an older interface. It's kept for backward compatibility
1348 * to the option that doesn't provide association id.
1349 */
1350SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1351 struct sockaddr __user *addrs,
1352 int addrs_size)
1353{
1354 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1355}
1356
1357/*
1358 * New interface for the API. The since the API is done with a socket
1359 * option, to make it simple we feed back the association id is as a return
1360 * indication to the call. Error is always negative and association id is
1361 * always positive.
1362 */
1363SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1364 struct sockaddr __user *addrs,
1365 int addrs_size)
1366{
1367 sctp_assoc_t assoc_id = 0;
1368 int err = 0;
1369
1370 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1371
1372 if (err)
1373 return err;
1374 else
1375 return assoc_id;
1376}
1377
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001378/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001379 * New (hopefully final) interface for the API.
1380 * We use the sctp_getaddrs_old structure so that use-space library
1381 * can avoid any unnecessary allocations. The only defferent part
1382 * is that we store the actual length of the address buffer into the
1383 * addrs_num structure member. That way we can re-use the existing
1384 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001385 */
1386SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1387 char __user *optval,
1388 int __user *optlen)
1389{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001390 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001391 sctp_assoc_t assoc_id = 0;
1392 int err = 0;
1393
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 if (len < sizeof(param))
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001395 return -EINVAL;
1396
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001397 if (copy_from_user(&param, optval, sizeof(param)))
1398 return -EFAULT;
1399
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001400 err = __sctp_setsockopt_connectx(sk,
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001401 (struct sockaddr __user *)param.addrs,
1402 param.addr_num, &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001403
1404 if (err == 0 || err == -EINPROGRESS) {
1405 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1406 return -EFAULT;
1407 if (put_user(sizeof(assoc_id), optlen))
1408 return -EFAULT;
1409 }
1410
1411 return err;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/* API 3.1.4 close() - UDP Style Syntax
1415 * Applications use close() to perform graceful shutdown (as described in
1416 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1417 * by a UDP-style socket.
1418 *
1419 * The syntax is
1420 *
1421 * ret = close(int sd);
1422 *
1423 * sd - the socket descriptor of the associations to be closed.
1424 *
1425 * To gracefully shutdown a specific association represented by the
1426 * UDP-style socket, an application should use the sendmsg() call,
1427 * passing no user data, but including the appropriate flag in the
1428 * ancillary data (see Section xxxx).
1429 *
1430 * If sd in the close() call is a branched-off socket representing only
1431 * one association, the shutdown is performed on that association only.
1432 *
1433 * 4.1.6 close() - TCP Style Syntax
1434 *
1435 * Applications use close() to gracefully close down an association.
1436 *
1437 * The syntax is:
1438 *
1439 * int close(int sd);
1440 *
1441 * sd - the socket descriptor of the association to be closed.
1442 *
1443 * After an application calls close() on a socket descriptor, no further
1444 * socket operations will succeed on that descriptor.
1445 *
1446 * API 7.1.4 SO_LINGER
1447 *
1448 * An application using the TCP-style socket can use this option to
1449 * perform the SCTP ABORT primitive. The linger option structure is:
1450 *
1451 * struct linger {
1452 * int l_onoff; // option on/off
1453 * int l_linger; // linger time
1454 * };
1455 *
1456 * To enable the option, set l_onoff to 1. If the l_linger value is set
1457 * to 0, calling close() is the same as the ABORT primitive. If the
1458 * value is set to a negative value, the setsockopt() call will return
1459 * an error. If the value is set to a positive value linger_time, the
1460 * close() can be blocked for at most linger_time ms. If the graceful
1461 * shutdown phase does not finish during this period, close() will
1462 * return but the graceful shutdown phase continues in the system.
1463 */
1464SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1465{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001466 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct sctp_endpoint *ep;
1468 struct sctp_association *asoc;
1469 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001470 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1473
1474 sctp_lock_sock(sk);
1475 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001476 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 ep = sctp_sk(sk)->ep;
1479
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001480 /* Clean up any skbs sitting on the receive queue. */
1481 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1482 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1483
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001484 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 list_for_each_safe(pos, temp, &ep->asocs) {
1486 asoc = list_entry(pos, struct sctp_association, asocs);
1487
1488 if (sctp_style(sk, TCP)) {
1489 /* A closed association can still be in the list if
1490 * it belongs to a TCP-style listening socket that is
1491 * not yet accepted. If so, free it. If not, send an
1492 * ABORT or SHUTDOWN based on the linger options.
1493 */
1494 if (sctp_state(asoc, CLOSED)) {
1495 sctp_unhash_established(asoc);
1496 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001497 continue;
1498 }
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001501 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1502 !skb_queue_empty(&asoc->ulpq.reasm) ||
1503 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001504 struct sctp_chunk *chunk;
1505
1506 chunk = sctp_make_abort_user(asoc, NULL, 0);
1507 if (chunk)
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001508 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001509 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001510 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* On a TCP-style socket, block for at most linger_time if set. */
1514 if (sctp_style(sk, TCP) && timeout)
1515 sctp_wait_for_close(sk, timeout);
1516
1517 /* This will run the backlog queue. */
1518 sctp_release_sock(sk);
1519
1520 /* Supposedly, no process has access to the socket, but
1521 * the net layers still may.
1522 */
1523 sctp_local_bh_disable();
1524 sctp_bh_lock_sock(sk);
1525
1526 /* Hold the sock, since sk_common_release() will put sock_put()
1527 * and we have just a little more cleanup.
1528 */
1529 sock_hold(sk);
1530 sk_common_release(sk);
1531
1532 sctp_bh_unlock_sock(sk);
1533 sctp_local_bh_enable();
1534
1535 sock_put(sk);
1536
1537 SCTP_DBG_OBJCNT_DEC(sock);
1538}
1539
1540/* Handle EPIPE error. */
1541static int sctp_error(struct sock *sk, int flags, int err)
1542{
1543 if (err == -EPIPE)
1544 err = sock_error(sk) ? : -EPIPE;
1545 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1546 send_sig(SIGPIPE, current, 0);
1547 return err;
1548}
1549
1550/* API 3.1.3 sendmsg() - UDP Style Syntax
1551 *
1552 * An application uses sendmsg() and recvmsg() calls to transmit data to
1553 * and receive data from its peer.
1554 *
1555 * ssize_t sendmsg(int socket, const struct msghdr *message,
1556 * int flags);
1557 *
1558 * socket - the socket descriptor of the endpoint.
1559 * message - pointer to the msghdr structure which contains a single
1560 * user message and possibly some ancillary data.
1561 *
1562 * See Section 5 for complete description of the data
1563 * structures.
1564 *
1565 * flags - flags sent or received with the user message, see Section
1566 * 5 for complete description of the flags.
1567 *
1568 * Note: This function could use a rewrite especially when explicit
1569 * connect support comes in.
1570 */
1571/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1572
1573SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1574
1575SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1576 struct msghdr *msg, size_t msg_len)
1577{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001578 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct sctp_sock *sp;
1580 struct sctp_endpoint *ep;
1581 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1582 struct sctp_transport *transport, *chunk_tp;
1583 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001584 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001586 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct sctp_sndrcvinfo *sinfo;
1588 struct sctp_initmsg *sinit;
1589 sctp_assoc_t associd = 0;
1590 sctp_cmsgs_t cmsgs = { NULL };
1591 int err;
1592 sctp_scope_t scope;
1593 long timeo;
1594 __u16 sinfo_flags = 0;
1595 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int msg_flags = msg->msg_flags;
1597
1598 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1599 sk, msg, msg_len);
1600
1601 err = 0;
1602 sp = sctp_sk(sk);
1603 ep = sp->ep;
1604
Frank Filz3f7a87d2005-06-20 13:14:57 -07001605 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /* We cannot send a message over a TCP-style listening socket. */
1608 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1609 err = -EPIPE;
1610 goto out_nounlock;
1611 }
1612
1613 /* Parse out the SCTP CMSGs. */
1614 err = sctp_msghdr_parse(msg, &cmsgs);
1615
1616 if (err) {
1617 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1618 goto out_nounlock;
1619 }
1620
1621 /* Fetch the destination address for this packet. This
1622 * address only selects the association--it is not necessarily
1623 * the address we will send to.
1624 * For a peeled-off socket, msg_name is ignored.
1625 */
1626 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1627 int msg_namelen = msg->msg_namelen;
1628
1629 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1630 msg_namelen);
1631 if (err)
1632 return err;
1633
1634 if (msg_namelen > sizeof(to))
1635 msg_namelen = sizeof(to);
1636 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 msg_name = msg->msg_name;
1638 }
1639
1640 sinfo = cmsgs.info;
1641 sinit = cmsgs.init;
1642
1643 /* Did the user specify SNDRCVINFO? */
1644 if (sinfo) {
1645 sinfo_flags = sinfo->sinfo_flags;
1646 associd = sinfo->sinfo_assoc_id;
1647 }
1648
1649 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1650 msg_len, sinfo_flags);
1651
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001652 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1653 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 err = -EINVAL;
1655 goto out_nounlock;
1656 }
1657
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001658 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1659 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1660 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001662 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001663 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1664 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 err = -EINVAL;
1666 goto out_nounlock;
1667 }
1668
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001669 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * specified in msg_name.
1671 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001672 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 err = -EINVAL;
1674 goto out_nounlock;
1675 }
1676
1677 transport = NULL;
1678
1679 SCTP_DEBUG_PRINTK("About to look up association.\n");
1680
1681 sctp_lock_sock(sk);
1682
1683 /* If a msg_name has been specified, assume this is to be used. */
1684 if (msg_name) {
1685 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001686 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!asoc) {
1688 /* If we could not find a matching association on the
1689 * endpoint, make sure that it is not a TCP-style
1690 * socket that already has an association or there is
1691 * no peeled-off association on another socket.
1692 */
1693 if ((sctp_style(sk, TCP) &&
1694 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001695 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 err = -EADDRNOTAVAIL;
1697 goto out_unlock;
1698 }
1699 }
1700 } else {
1701 asoc = sctp_id2assoc(sk, associd);
1702 if (!asoc) {
1703 err = -EPIPE;
1704 goto out_unlock;
1705 }
1706 }
1707
1708 if (asoc) {
1709 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1710
1711 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1712 * socket that has an association in CLOSED state. This can
1713 * happen when an accepted socket has an association that is
1714 * already CLOSED.
1715 */
1716 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1717 err = -EPIPE;
1718 goto out_unlock;
1719 }
1720
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001721 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1723 asoc);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001724 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 err = 0;
1726 goto out_unlock;
1727 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001728 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001729
1730 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1731 if (!chunk) {
1732 err = -ENOMEM;
1733 goto out_unlock;
1734 }
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001737 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 err = 0;
1739 goto out_unlock;
1740 }
1741 }
1742
1743 /* Do we need to create the association? */
1744 if (!asoc) {
1745 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1746
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001747 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 err = -EINVAL;
1749 goto out_unlock;
1750 }
1751
1752 /* Check for invalid stream against the stream counts,
1753 * either the default or the user specified stream counts.
1754 */
1755 if (sinfo) {
1756 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1757 /* Check against the defaults. */
1758 if (sinfo->sinfo_stream >=
1759 sp->initmsg.sinit_num_ostreams) {
1760 err = -EINVAL;
1761 goto out_unlock;
1762 }
1763 } else {
1764 /* Check against the requested. */
1765 if (sinfo->sinfo_stream >=
1766 sinit->sinit_num_ostreams) {
1767 err = -EINVAL;
1768 goto out_unlock;
1769 }
1770 }
1771 }
1772
1773 /*
1774 * API 3.1.2 bind() - UDP Style Syntax
1775 * If a bind() or sctp_bindx() is not called prior to a
1776 * sendmsg() call that initiates a new association, the
1777 * system picks an ephemeral port and will choose an address
1778 * set equivalent to binding with a wildcard address.
1779 */
1780 if (!ep->base.bind_addr.port) {
1781 if (sctp_autobind(sk)) {
1782 err = -EAGAIN;
1783 goto out_unlock;
1784 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001785 } else {
1786 /*
1787 * If an unprivileged user inherits a one-to-many
1788 * style socket with open associations on a privileged
1789 * port, it MAY be permitted to accept new associations,
1790 * but it SHOULD NOT be permitted to open new
1791 * associations.
1792 */
1793 if (ep->base.bind_addr.port < PROT_SOCK &&
1794 !capable(CAP_NET_BIND_SERVICE)) {
1795 err = -EACCES;
1796 goto out_unlock;
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
1800 scope = sctp_scope(&to);
1801 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1802 if (!new_asoc) {
1803 err = -ENOMEM;
1804 goto out_unlock;
1805 }
1806 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001807 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1808 if (err < 0) {
1809 err = -ENOMEM;
1810 goto out_free;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* If the SCTP_INIT ancillary data is specified, set all
1814 * the association init values accordingly.
1815 */
1816 if (sinit) {
1817 if (sinit->sinit_num_ostreams) {
1818 asoc->c.sinit_num_ostreams =
1819 sinit->sinit_num_ostreams;
1820 }
1821 if (sinit->sinit_max_instreams) {
1822 asoc->c.sinit_max_instreams =
1823 sinit->sinit_max_instreams;
1824 }
1825 if (sinit->sinit_max_attempts) {
1826 asoc->max_init_attempts
1827 = sinit->sinit_max_attempts;
1828 }
1829 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001830 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1832 }
1833 }
1834
1835 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001836 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (!transport) {
1838 err = -ENOMEM;
1839 goto out_free;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
1843 /* ASSERT: we have a valid association at this point. */
1844 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1845
1846 if (!sinfo) {
1847 /* If the user didn't specify SNDRCVINFO, make up one with
1848 * some defaults.
1849 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001850 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 default_sinfo.sinfo_stream = asoc->default_stream;
1852 default_sinfo.sinfo_flags = asoc->default_flags;
1853 default_sinfo.sinfo_ppid = asoc->default_ppid;
1854 default_sinfo.sinfo_context = asoc->default_context;
1855 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1856 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1857 sinfo = &default_sinfo;
1858 }
1859
1860 /* API 7.1.7, the sndbuf size per association bounds the
1861 * maximum size of data that can be sent in a single send call.
1862 */
1863 if (msg_len > sk->sk_sndbuf) {
1864 err = -EMSGSIZE;
1865 goto out_free;
1866 }
1867
Vlad Yasevich8a479492007-06-07 14:21:05 -04001868 if (asoc->pmtu_pending)
David S. Miller02f3d4c2012-07-16 03:57:14 -07001869 sctp_assoc_pending_pmtu(sk, asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /* If fragmentation is disabled and the message length exceeds the
1872 * association fragmentation point, return EMSGSIZE. The I-D
1873 * does not specify what this error is, but this looks like
1874 * a great fit.
1875 */
1876 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1877 err = -EMSGSIZE;
1878 goto out_free;
1879 }
1880
Joe Perchesafd76142011-05-12 09:19:10 +00001881 /* Check for invalid stream. */
1882 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1883 err = -EINVAL;
1884 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
1887 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1888 if (!sctp_wspace(asoc)) {
1889 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1890 if (err)
1891 goto out_free;
1892 }
1893
1894 /* If an address is passed with the sendto/sendmsg call, it is used
1895 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001896 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
1898 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001899 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001900 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!chunk_tp) {
1902 err = -EINVAL;
1903 goto out_free;
1904 }
1905 } else
1906 chunk_tp = NULL;
1907
1908 /* Auto-connect, if we aren't connected already. */
1909 if (sctp_state(asoc, CLOSED)) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001910 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (err < 0)
1912 goto out_free;
1913 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1914 }
1915
1916 /* Break the message into multiple chunks of maximum size. */
1917 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1918 if (!datamsg) {
1919 err = -ENOMEM;
1920 goto out_free;
1921 }
1922
1923 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001924 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Florian Westphal80445cf2008-03-23 22:47:08 -07001925 sctp_chunk_hold(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /* Do accounting for the write space. */
1928 sctp_set_owner_w(chunk);
1929
1930 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001933 /* Send it to the lower layers. Note: all chunks
1934 * must either fail or succeed. The lower layer
1935 * works that way today. Keep it that way or this
1936 * breaks.
1937 */
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001938 err = sctp_primitive_SEND(net, asoc, datamsg);
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001939 /* Did the lower layer accept the chunk? */
1940 if (err)
1941 sctp_datamsg_free(datamsg);
1942 else
1943 sctp_datamsg_put(datamsg);
1944
1945 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (err)
1948 goto out_free;
1949 else
1950 err = msg_len;
1951
1952 /* If we are already past ASSOCIATE, the lower
1953 * layers are responsible for association cleanup.
1954 */
1955 goto out_unlock;
1956
1957out_free:
Neil Horman2eebc1e2012-07-16 09:13:51 +00001958 if (new_asoc) {
1959 sctp_unhash_established(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962out_unlock:
1963 sctp_release_sock(sk);
1964
1965out_nounlock:
1966 return sctp_error(sk, msg_flags, err);
1967
1968#if 0
1969do_sock_err:
1970 if (msg_len)
1971 err = msg_len;
1972 else
1973 err = sock_error(sk);
1974 goto out;
1975
1976do_interrupted:
1977 if (msg_len)
1978 err = msg_len;
1979 goto out;
1980#endif /* 0 */
1981}
1982
1983/* This is an extended version of skb_pull() that removes the data from the
1984 * start of a skb even when data is spread across the list of skb's in the
1985 * frag_list. len specifies the total amount of data that needs to be removed.
1986 * when 'len' bytes could be removed from the skb, it returns 0.
1987 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1988 * could not be removed.
1989 */
1990static int sctp_skb_pull(struct sk_buff *skb, int len)
1991{
1992 struct sk_buff *list;
1993 int skb_len = skb_headlen(skb);
1994 int rlen;
1995
1996 if (len <= skb_len) {
1997 __skb_pull(skb, len);
1998 return 0;
1999 }
2000 len -= skb_len;
2001 __skb_pull(skb, skb_len);
2002
David S. Miller1b003be2009-06-09 00:22:35 -07002003 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 rlen = sctp_skb_pull(list, len);
2005 skb->len -= (len-rlen);
2006 skb->data_len -= (len-rlen);
2007
2008 if (!rlen)
2009 return 0;
2010
2011 len = rlen;
2012 }
2013
2014 return len;
2015}
2016
2017/* API 3.1.3 recvmsg() - UDP Style Syntax
2018 *
2019 * ssize_t recvmsg(int socket, struct msghdr *message,
2020 * int flags);
2021 *
2022 * socket - the socket descriptor of the endpoint.
2023 * message - pointer to the msghdr structure which contains a single
2024 * user message and possibly some ancillary data.
2025 *
2026 * See Section 5 for complete description of the data
2027 * structures.
2028 *
2029 * flags - flags sent or received with the user message, see Section
2030 * 5 for complete description of the flags.
2031 */
2032static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2033
2034SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2035 struct msghdr *msg, size_t len, int noblock,
2036 int flags, int *addr_len)
2037{
2038 struct sctp_ulpevent *event = NULL;
2039 struct sctp_sock *sp = sctp_sk(sk);
2040 struct sk_buff *skb;
2041 int copied;
2042 int err = 0;
2043 int skb_len;
2044
2045 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2046 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2047 "len", len, "knoblauch", noblock,
2048 "flags", flags, "addr_len", addr_len);
2049
2050 sctp_lock_sock(sk);
2051
2052 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2053 err = -ENOTCONN;
2054 goto out;
2055 }
2056
2057 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2058 if (!skb)
2059 goto out;
2060
2061 /* Get the total length of the skb including any skb's in the
2062 * frag_list.
2063 */
2064 skb_len = skb->len;
2065
2066 copied = skb_len;
2067 if (copied > len)
2068 copied = len;
2069
2070 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2071
2072 event = sctp_skb2event(skb);
2073
2074 if (err)
2075 goto out_free;
2076
Neil Horman3b885782009-10-12 13:26:31 -07002077 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (sctp_ulpevent_is_notification(event)) {
2079 msg->msg_flags |= MSG_NOTIFICATION;
2080 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2081 } else {
2082 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2083 }
2084
2085 /* Check if we allow SCTP_SNDRCVINFO. */
2086 if (sp->subscribe.sctp_data_io_event)
2087 sctp_ulpevent_read_sndrcvinfo(event, msg);
2088#if 0
2089 /* FIXME: we should be calling IP/IPv6 layers. */
2090 if (sk->sk_protinfo.af_inet.cmsg_flags)
2091 ip_cmsg_recv(msg, skb);
2092#endif
2093
2094 err = copied;
2095
2096 /* If skb's length exceeds the user's buffer, update the skb and
2097 * push it back to the receive_queue so that the next call to
2098 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2099 */
2100 if (skb_len > copied) {
2101 msg->msg_flags &= ~MSG_EOR;
2102 if (flags & MSG_PEEK)
2103 goto out_free;
2104 sctp_skb_pull(skb, copied);
2105 skb_queue_head(&sk->sk_receive_queue, skb);
2106
2107 /* When only partial message is copied to the user, increase
2108 * rwnd by that amount. If all the data in the skb is read,
2109 * rwnd is updated when the event is freed.
2110 */
Vlad Yasevich0eca8fe2008-01-11 10:12:56 -05002111 if (!sctp_ulpevent_is_notification(event))
2112 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto out;
2114 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2115 (event->msg_flags & MSG_EOR))
2116 msg->msg_flags |= MSG_EOR;
2117 else
2118 msg->msg_flags &= ~MSG_EOR;
2119
2120out_free:
2121 if (flags & MSG_PEEK) {
2122 /* Release the skb reference acquired after peeking the skb in
2123 * sctp_skb_recv_datagram().
2124 */
2125 kfree_skb(skb);
2126 } else {
2127 /* Free the event which includes releasing the reference to
2128 * the owner of the skb, freeing the skb and updating the
2129 * rwnd.
2130 */
2131 sctp_ulpevent_free(event);
2132 }
2133out:
2134 sctp_release_sock(sk);
2135 return err;
2136}
2137
2138/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2139 *
2140 * This option is a on/off flag. If enabled no SCTP message
2141 * fragmentation will be performed. Instead if a message being sent
2142 * exceeds the current PMTU size, the message will NOT be sent and
2143 * instead a error will be indicated to the user.
2144 */
2145static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002146 char __user *optval,
2147 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 int val;
2150
2151 if (optlen < sizeof(int))
2152 return -EINVAL;
2153
2154 if (get_user(val, (int __user *)optval))
2155 return -EFAULT;
2156
2157 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2158
2159 return 0;
2160}
2161
2162static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002163 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Wei Yongjun94912302011-07-02 09:28:04 +00002165 struct sctp_association *asoc;
2166 struct sctp_ulpevent *event;
2167
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002168 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -EINVAL;
2170 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2171 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002172
2173 /*
2174 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2175 * if there is no data to be sent or retransmit, the stack will
2176 * immediately send up this notification.
2177 */
2178 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2179 &sctp_sk(sk)->subscribe)) {
2180 asoc = sctp_id2assoc(sk, 0);
2181
2182 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2183 event = sctp_ulpevent_make_sender_dry_event(asoc,
2184 GFP_ATOMIC);
2185 if (!event)
2186 return -ENOMEM;
2187
2188 sctp_ulpq_tail_event(&asoc->ulpq, event);
2189 }
2190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
2194
2195/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2196 *
2197 * This socket option is applicable to the UDP-style socket only. When
2198 * set it will cause associations that are idle for more than the
2199 * specified number of seconds to automatically close. An association
2200 * being idle is defined an association that has NOT sent or received
2201 * user data. The special value of '0' indicates that no automatic
2202 * close of any associations should be performed. The option expects an
2203 * integer defining the number of seconds of idle time before an
2204 * association is closed.
2205 */
2206static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002207 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 struct sctp_sock *sp = sctp_sk(sk);
2210
2211 /* Applicable to UDP-style socket only */
2212 if (sctp_style(sk, TCP))
2213 return -EOPNOTSUPP;
2214 if (optlen != sizeof(int))
2215 return -EINVAL;
2216 if (copy_from_user(&sp->autoclose, optval, optlen))
2217 return -EFAULT;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return 0;
2220}
2221
2222/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2223 *
2224 * Applications can enable or disable heartbeats for any peer address of
2225 * an association, modify an address's heartbeat interval, force a
2226 * heartbeat to be sent immediately, and adjust the address's maximum
2227 * number of retransmissions sent before an address is considered
2228 * unreachable. The following structure is used to access and modify an
2229 * address's parameters:
2230 *
2231 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002232 * sctp_assoc_t spp_assoc_id;
2233 * struct sockaddr_storage spp_address;
2234 * uint32_t spp_hbinterval;
2235 * uint16_t spp_pathmaxrxt;
2236 * uint32_t spp_pathmtu;
2237 * uint32_t spp_sackdelay;
2238 * uint32_t spp_flags;
2239 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002241 * spp_assoc_id - (one-to-many style socket) This is filled in the
2242 * application, and identifies the association for
2243 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * spp_address - This specifies which address is of interest.
2245 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002246 * in milliseconds. If a value of zero
2247 * is present in this field then no changes are to
2248 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 * spp_pathmaxrxt - This contains the maximum number of
2250 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002251 * considered unreachable. If a value of zero
2252 * is present in this field then no changes are to
2253 * be made to this parameter.
2254 * spp_pathmtu - When Path MTU discovery is disabled the value
2255 * specified here will be the "fixed" path mtu.
2256 * Note that if the spp_address field is empty
2257 * then all associations on this address will
2258 * have this fixed path mtu set upon them.
2259 *
2260 * spp_sackdelay - When delayed sack is enabled, this value specifies
2261 * the number of milliseconds that sacks will be delayed
2262 * for. This value will apply to all addresses of an
2263 * association if the spp_address field is empty. Note
2264 * also, that if delayed sack is enabled and this
2265 * value is set to 0, no change is made to the last
2266 * recorded delayed sack timer value.
2267 *
2268 * spp_flags - These flags are used to control various features
2269 * on an association. The flag field may contain
2270 * zero or more of the following options.
2271 *
2272 * SPP_HB_ENABLE - Enable heartbeats on the
2273 * specified address. Note that if the address
2274 * field is empty all addresses for the association
2275 * have heartbeats enabled upon them.
2276 *
2277 * SPP_HB_DISABLE - Disable heartbeats on the
2278 * speicifed address. Note that if the address
2279 * field is empty all addresses for the association
2280 * will have their heartbeats disabled. Note also
2281 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2282 * mutually exclusive, only one of these two should
2283 * be specified. Enabling both fields will have
2284 * undetermined results.
2285 *
2286 * SPP_HB_DEMAND - Request a user initiated heartbeat
2287 * to be made immediately.
2288 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002289 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2290 * heartbeat delayis to be set to the value of 0
2291 * milliseconds.
2292 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002293 * SPP_PMTUD_ENABLE - This field will enable PMTU
2294 * discovery upon the specified address. Note that
2295 * if the address feild is empty then all addresses
2296 * on the association are effected.
2297 *
2298 * SPP_PMTUD_DISABLE - This field will disable PMTU
2299 * discovery upon the specified address. Note that
2300 * if the address feild is empty then all addresses
2301 * on the association are effected. Not also that
2302 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2303 * exclusive. Enabling both will have undetermined
2304 * results.
2305 *
2306 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2307 * on delayed sack. The time specified in spp_sackdelay
2308 * is used to specify the sack delay for this address. Note
2309 * that if spp_address is empty then all addresses will
2310 * enable delayed sack and take on the sack delay
2311 * value specified in spp_sackdelay.
2312 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2313 * off delayed sack. If the spp_address field is blank then
2314 * delayed sack is disabled for the entire association. Note
2315 * also that this field is mutually exclusive to
2316 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2317 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
Adrian Bunk16164362006-09-18 00:40:38 -07002319static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2320 struct sctp_transport *trans,
2321 struct sctp_association *asoc,
2322 struct sctp_sock *sp,
2323 int hb_change,
2324 int pmtud_change,
2325 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 int error;
2328
Frank Filz52ccb8e2005-12-22 11:36:46 -08002329 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002330 struct net *net = sock_net(trans->asoc->base.sk);
2331
2332 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (error)
2334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002337 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2338 * this field is ignored. Note also that a value of zero indicates
2339 * the current setting should be left unchanged.
2340 */
2341 if (params->spp_flags & SPP_HB_ENABLE) {
2342
2343 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2344 * set. This lets us use 0 value when this flag
2345 * is set.
2346 */
2347 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2348 params->spp_hbinterval = 0;
2349
2350 if (params->spp_hbinterval ||
2351 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2352 if (trans) {
2353 trans->hbinterval =
2354 msecs_to_jiffies(params->spp_hbinterval);
2355 } else if (asoc) {
2356 asoc->hbinterval =
2357 msecs_to_jiffies(params->spp_hbinterval);
2358 } else {
2359 sp->hbinterval = params->spp_hbinterval;
2360 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002361 }
2362 }
2363
2364 if (hb_change) {
2365 if (trans) {
2366 trans->param_flags =
2367 (trans->param_flags & ~SPP_HB) | hb_change;
2368 } else if (asoc) {
2369 asoc->param_flags =
2370 (asoc->param_flags & ~SPP_HB) | hb_change;
2371 } else {
2372 sp->param_flags =
2373 (sp->param_flags & ~SPP_HB) | hb_change;
2374 }
2375 }
2376
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002377 /* When Path MTU discovery is disabled the value specified here will
2378 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2379 * include the flag SPP_PMTUD_DISABLE for this field to have any
2380 * effect).
2381 */
2382 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002383 if (trans) {
2384 trans->pathmtu = params->spp_pathmtu;
David S. Miller02f3d4c2012-07-16 03:57:14 -07002385 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002386 } else if (asoc) {
2387 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002388 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002389 } else {
2390 sp->pathmtu = params->spp_pathmtu;
2391 }
2392 }
2393
2394 if (pmtud_change) {
2395 if (trans) {
2396 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2397 (params->spp_flags & SPP_PMTUD_ENABLE);
2398 trans->param_flags =
2399 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2400 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002401 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
David S. Miller02f3d4c2012-07-16 03:57:14 -07002402 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002403 }
2404 } else if (asoc) {
2405 asoc->param_flags =
2406 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2407 } else {
2408 sp->param_flags =
2409 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2410 }
2411 }
2412
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002413 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2414 * value of this field is ignored. Note also that a value of zero
2415 * indicates the current setting should be left unchanged.
2416 */
2417 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002418 if (trans) {
2419 trans->sackdelay =
2420 msecs_to_jiffies(params->spp_sackdelay);
2421 } else if (asoc) {
2422 asoc->sackdelay =
2423 msecs_to_jiffies(params->spp_sackdelay);
2424 } else {
2425 sp->sackdelay = params->spp_sackdelay;
2426 }
2427 }
2428
2429 if (sackdelay_change) {
2430 if (trans) {
2431 trans->param_flags =
2432 (trans->param_flags & ~SPP_SACKDELAY) |
2433 sackdelay_change;
2434 } else if (asoc) {
2435 asoc->param_flags =
2436 (asoc->param_flags & ~SPP_SACKDELAY) |
2437 sackdelay_change;
2438 } else {
2439 sp->param_flags =
2440 (sp->param_flags & ~SPP_SACKDELAY) |
2441 sackdelay_change;
2442 }
2443 }
2444
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002445 /* Note that a value of zero indicates the current setting should be
2446 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002447 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002448 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002449 if (trans) {
2450 trans->pathmaxrxt = params->spp_pathmaxrxt;
2451 } else if (asoc) {
2452 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2453 } else {
2454 sp->pathmaxrxt = params->spp_pathmaxrxt;
2455 }
2456 }
2457
2458 return 0;
2459}
2460
2461static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002462 char __user *optval,
2463 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002464{
2465 struct sctp_paddrparams params;
2466 struct sctp_transport *trans = NULL;
2467 struct sctp_association *asoc = NULL;
2468 struct sctp_sock *sp = sctp_sk(sk);
2469 int error;
2470 int hb_change, pmtud_change, sackdelay_change;
2471
2472 if (optlen != sizeof(struct sctp_paddrparams))
2473 return - EINVAL;
2474
2475 if (copy_from_user(&params, optval, optlen))
2476 return -EFAULT;
2477
2478 /* Validate flags and value parameters. */
2479 hb_change = params.spp_flags & SPP_HB;
2480 pmtud_change = params.spp_flags & SPP_PMTUD;
2481 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2482
2483 if (hb_change == SPP_HB ||
2484 pmtud_change == SPP_PMTUD ||
2485 sackdelay_change == SPP_SACKDELAY ||
2486 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002487 (params.spp_pathmtu &&
2488 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002489 return -EINVAL;
2490
2491 /* If an address other than INADDR_ANY is specified, and
2492 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04002494 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002495 trans = sctp_addr_id2transport(sk, &params.spp_address,
2496 params.spp_assoc_id);
2497 if (!trans)
2498 return -EINVAL;
2499 }
2500
2501 /* Get association, if assoc_id != 0 and the socket is a one
2502 * to many style socket, and an association was not found, then
2503 * the id was invalid.
2504 */
2505 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2506 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2507 return -EINVAL;
2508
2509 /* Heartbeat demand can only be sent on a transport or
2510 * association, but not a socket.
2511 */
2512 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2513 return -EINVAL;
2514
2515 /* Process parameters. */
2516 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2517 hb_change, pmtud_change,
2518 sackdelay_change);
2519
2520 if (error)
2521 return error;
2522
2523 /* If changes are for association, also apply parameters to each
2524 * transport.
2525 */
2526 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002527 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2528 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002529 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2530 hb_change, pmtud_change,
2531 sackdelay_change);
2532 }
2533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 return 0;
2536}
2537
Wei Yongjund364d922008-05-09 15:13:26 -07002538/*
2539 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002540 *
Wei Yongjund364d922008-05-09 15:13:26 -07002541 * This option will effect the way delayed acks are performed. This
2542 * option allows you to get or set the delayed ack time, in
2543 * milliseconds. It also allows changing the delayed ack frequency.
2544 * Changing the frequency to 1 disables the delayed sack algorithm. If
2545 * the assoc_id is 0, then this sets or gets the endpoints default
2546 * values. If the assoc_id field is non-zero, then the set or get
2547 * effects the specified association for the one to many model (the
2548 * assoc_id field is ignored by the one to one model). Note that if
2549 * sack_delay or sack_freq are 0 when setting this option, then the
2550 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002551 *
Wei Yongjund364d922008-05-09 15:13:26 -07002552 * struct sctp_sack_info {
2553 * sctp_assoc_t sack_assoc_id;
2554 * uint32_t sack_delay;
2555 * uint32_t sack_freq;
2556 * };
Frank Filz77086102005-12-22 11:37:30 -08002557 *
Wei Yongjund364d922008-05-09 15:13:26 -07002558 * sack_assoc_id - This parameter, indicates which association the user
2559 * is performing an action upon. Note that if this field's value is
2560 * zero then the endpoints default value is changed (effecting future
2561 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002562 *
Wei Yongjund364d922008-05-09 15:13:26 -07002563 * sack_delay - This parameter contains the number of milliseconds that
2564 * the user is requesting the delayed ACK timer be set to. Note that
2565 * this value is defined in the standard to be between 200 and 500
2566 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002567 *
Wei Yongjund364d922008-05-09 15:13:26 -07002568 * sack_freq - This parameter contains the number of packets that must
2569 * be received before a sack is sent without waiting for the delay
2570 * timer to expire. The default value for this is 2, setting this
2571 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002572 */
2573
Wei Yongjund364d922008-05-09 15:13:26 -07002574static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002575 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002576{
Wei Yongjund364d922008-05-09 15:13:26 -07002577 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002578 struct sctp_transport *trans = NULL;
2579 struct sctp_association *asoc = NULL;
2580 struct sctp_sock *sp = sctp_sk(sk);
2581
Wei Yongjund364d922008-05-09 15:13:26 -07002582 if (optlen == sizeof(struct sctp_sack_info)) {
2583 if (copy_from_user(&params, optval, optlen))
2584 return -EFAULT;
2585
2586 if (params.sack_delay == 0 && params.sack_freq == 0)
2587 return 0;
2588 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00002589 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2590 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07002591 if (copy_from_user(&params, optval, optlen))
2592 return -EFAULT;
2593
2594 if (params.sack_delay == 0)
2595 params.sack_freq = 1;
2596 else
2597 params.sack_freq = 0;
2598 } else
Frank Filz77086102005-12-22 11:37:30 -08002599 return - EINVAL;
2600
Frank Filz77086102005-12-22 11:37:30 -08002601 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002602 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002603 return -EINVAL;
2604
Wei Yongjund364d922008-05-09 15:13:26 -07002605 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002606 * to many style socket, and an association was not found, then
2607 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002608 */
Wei Yongjund364d922008-05-09 15:13:26 -07002609 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2610 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002611 return -EINVAL;
2612
Wei Yongjund364d922008-05-09 15:13:26 -07002613 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002614 if (asoc) {
2615 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002616 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002617 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002618 (asoc->param_flags & ~SPP_SACKDELAY) |
2619 SPP_SACKDELAY_ENABLE;
2620 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002621 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002622 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002623 (sp->param_flags & ~SPP_SACKDELAY) |
2624 SPP_SACKDELAY_ENABLE;
2625 }
Wei Yongjund364d922008-05-09 15:13:26 -07002626 }
2627
2628 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002629 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002630 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002631 (asoc->param_flags & ~SPP_SACKDELAY) |
2632 SPP_SACKDELAY_DISABLE;
2633 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002634 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002635 (sp->param_flags & ~SPP_SACKDELAY) |
2636 SPP_SACKDELAY_DISABLE;
2637 }
Wei Yongjund364d922008-05-09 15:13:26 -07002638 } else if (params.sack_freq > 1) {
2639 if (asoc) {
2640 asoc->sackfreq = params.sack_freq;
2641 asoc->param_flags =
2642 (asoc->param_flags & ~SPP_SACKDELAY) |
2643 SPP_SACKDELAY_ENABLE;
2644 } else {
2645 sp->sackfreq = params.sack_freq;
2646 sp->param_flags =
2647 (sp->param_flags & ~SPP_SACKDELAY) |
2648 SPP_SACKDELAY_ENABLE;
2649 }
Frank Filz77086102005-12-22 11:37:30 -08002650 }
2651
2652 /* If change is for association, also apply to each transport. */
2653 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002654 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2655 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002656 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002657 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002658 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002659 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002660 (trans->param_flags & ~SPP_SACKDELAY) |
2661 SPP_SACKDELAY_ENABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002662 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002663 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002664 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002665 (trans->param_flags & ~SPP_SACKDELAY) |
2666 SPP_SACKDELAY_DISABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002667 } else if (params.sack_freq > 1) {
2668 trans->sackfreq = params.sack_freq;
2669 trans->param_flags =
2670 (trans->param_flags & ~SPP_SACKDELAY) |
2671 SPP_SACKDELAY_ENABLE;
Frank Filz77086102005-12-22 11:37:30 -08002672 }
2673 }
2674 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002675
Frank Filz77086102005-12-22 11:37:30 -08002676 return 0;
2677}
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2680 *
2681 * Applications can specify protocol parameters for the default association
2682 * initialization. The option name argument to setsockopt() and getsockopt()
2683 * is SCTP_INITMSG.
2684 *
2685 * Setting initialization parameters is effective only on an unconnected
2686 * socket (for UDP-style sockets only future associations are effected
2687 * by the change). With TCP-style sockets, this option is inherited by
2688 * sockets derived from a listener socket.
2689 */
David S. Millerb7058842009-09-30 16:12:20 -07002690static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 struct sctp_initmsg sinit;
2693 struct sctp_sock *sp = sctp_sk(sk);
2694
2695 if (optlen != sizeof(struct sctp_initmsg))
2696 return -EINVAL;
2697 if (copy_from_user(&sinit, optval, optlen))
2698 return -EFAULT;
2699
2700 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002701 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002703 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002705 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002707 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 return 0;
2710}
2711
2712/*
2713 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2714 *
2715 * Applications that wish to use the sendto() system call may wish to
2716 * specify a default set of parameters that would normally be supplied
2717 * through the inclusion of ancillary data. This socket option allows
2718 * such an application to set the default sctp_sndrcvinfo structure.
2719 * The application that wishes to use this socket option simply passes
2720 * in to this call the sctp_sndrcvinfo structure defined in Section
2721 * 5.2.2) The input parameters accepted by this call include
2722 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2723 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2724 * to this call if the caller is using the UDP model.
2725 */
2726static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002727 char __user *optval,
2728 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 struct sctp_sndrcvinfo info;
2731 struct sctp_association *asoc;
2732 struct sctp_sock *sp = sctp_sk(sk);
2733
2734 if (optlen != sizeof(struct sctp_sndrcvinfo))
2735 return -EINVAL;
2736 if (copy_from_user(&info, optval, optlen))
2737 return -EFAULT;
2738
2739 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2740 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2741 return -EINVAL;
2742
2743 if (asoc) {
2744 asoc->default_stream = info.sinfo_stream;
2745 asoc->default_flags = info.sinfo_flags;
2746 asoc->default_ppid = info.sinfo_ppid;
2747 asoc->default_context = info.sinfo_context;
2748 asoc->default_timetolive = info.sinfo_timetolive;
2749 } else {
2750 sp->default_stream = info.sinfo_stream;
2751 sp->default_flags = info.sinfo_flags;
2752 sp->default_ppid = info.sinfo_ppid;
2753 sp->default_context = info.sinfo_context;
2754 sp->default_timetolive = info.sinfo_timetolive;
2755 }
2756
2757 return 0;
2758}
2759
2760/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2761 *
2762 * Requests that the local SCTP stack use the enclosed peer address as
2763 * the association primary. The enclosed address must be one of the
2764 * association peer's addresses.
2765 */
2766static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002767 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct sctp_prim prim;
2770 struct sctp_transport *trans;
2771
2772 if (optlen != sizeof(struct sctp_prim))
2773 return -EINVAL;
2774
2775 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2776 return -EFAULT;
2777
2778 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2779 if (!trans)
2780 return -EINVAL;
2781
2782 sctp_assoc_set_primary(trans->asoc, trans);
2783
2784 return 0;
2785}
2786
2787/*
2788 * 7.1.5 SCTP_NODELAY
2789 *
2790 * Turn on/off any Nagle-like algorithm. This means that packets are
2791 * generally sent as soon as possible and no unnecessary delays are
2792 * introduced, at the cost of more packets in the network. Expects an
2793 * integer boolean flag.
2794 */
2795static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002796 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 int val;
2799
2800 if (optlen < sizeof(int))
2801 return -EINVAL;
2802 if (get_user(val, (int __user *)optval))
2803 return -EFAULT;
2804
2805 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2806 return 0;
2807}
2808
2809/*
2810 *
2811 * 7.1.1 SCTP_RTOINFO
2812 *
2813 * The protocol parameters used to initialize and bound retransmission
2814 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2815 * and modify these parameters.
2816 * All parameters are time values, in milliseconds. A value of 0, when
2817 * modifying the parameters, indicates that the current value should not
2818 * be changed.
2819 *
2820 */
David S. Millerb7058842009-09-30 16:12:20 -07002821static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct sctp_rtoinfo rtoinfo;
2824 struct sctp_association *asoc;
2825
2826 if (optlen != sizeof (struct sctp_rtoinfo))
2827 return -EINVAL;
2828
2829 if (copy_from_user(&rtoinfo, optval, optlen))
2830 return -EFAULT;
2831
2832 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2833
2834 /* Set the values to the specific association */
2835 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2836 return -EINVAL;
2837
2838 if (asoc) {
2839 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002840 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 msecs_to_jiffies(rtoinfo.srto_initial);
2842 if (rtoinfo.srto_max != 0)
2843 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2844 if (rtoinfo.srto_min != 0)
2845 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2846 } else {
2847 /* If there is no association or the association-id = 0
2848 * set the values to the endpoint.
2849 */
2850 struct sctp_sock *sp = sctp_sk(sk);
2851
2852 if (rtoinfo.srto_initial != 0)
2853 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2854 if (rtoinfo.srto_max != 0)
2855 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2856 if (rtoinfo.srto_min != 0)
2857 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2858 }
2859
2860 return 0;
2861}
2862
2863/*
2864 *
2865 * 7.1.2 SCTP_ASSOCINFO
2866 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002867 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 * of the association.
2869 * Returns an error if the new association retransmission value is
2870 * greater than the sum of the retransmission value of the peer.
2871 * See [SCTP] for more information.
2872 *
2873 */
David S. Millerb7058842009-09-30 16:12:20 -07002874static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876
2877 struct sctp_assocparams assocparams;
2878 struct sctp_association *asoc;
2879
2880 if (optlen != sizeof(struct sctp_assocparams))
2881 return -EINVAL;
2882 if (copy_from_user(&assocparams, optval, optlen))
2883 return -EFAULT;
2884
2885 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2886
2887 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2888 return -EINVAL;
2889
2890 /* Set the values to the specific association */
2891 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002892 if (assocparams.sasoc_asocmaxrxt != 0) {
2893 __u32 path_sum = 0;
2894 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002895 struct sctp_transport *peer_addr;
2896
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002897 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2898 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002899 path_sum += peer_addr->pathmaxrxt;
2900 paths++;
2901 }
2902
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002903 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002904 * one path/transport. We do this because path
2905 * retransmissions are only counted when we have more
2906 * then one path.
2907 */
2908 if (paths > 1 &&
2909 assocparams.sasoc_asocmaxrxt > path_sum)
2910 return -EINVAL;
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002913 }
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (assocparams.sasoc_cookie_life != 0) {
2916 asoc->cookie_life.tv_sec =
2917 assocparams.sasoc_cookie_life / 1000;
2918 asoc->cookie_life.tv_usec =
2919 (assocparams.sasoc_cookie_life % 1000)
2920 * 1000;
2921 }
2922 } else {
2923 /* Set the values to the endpoint */
2924 struct sctp_sock *sp = sctp_sk(sk);
2925
2926 if (assocparams.sasoc_asocmaxrxt != 0)
2927 sp->assocparams.sasoc_asocmaxrxt =
2928 assocparams.sasoc_asocmaxrxt;
2929 if (assocparams.sasoc_cookie_life != 0)
2930 sp->assocparams.sasoc_cookie_life =
2931 assocparams.sasoc_cookie_life;
2932 }
2933 return 0;
2934}
2935
2936/*
2937 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2938 *
2939 * This socket option is a boolean flag which turns on or off mapped V4
2940 * addresses. If this option is turned on and the socket is type
2941 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2942 * If this option is turned off, then no mapping will be done of V4
2943 * addresses and a user will receive both PF_INET6 and PF_INET type
2944 * addresses on the socket.
2945 */
David S. Millerb7058842009-09-30 16:12:20 -07002946static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 int val;
2949 struct sctp_sock *sp = sctp_sk(sk);
2950
2951 if (optlen < sizeof(int))
2952 return -EINVAL;
2953 if (get_user(val, (int __user *)optval))
2954 return -EFAULT;
2955 if (val)
2956 sp->v4mapped = 1;
2957 else
2958 sp->v4mapped = 0;
2959
2960 return 0;
2961}
2962
2963/*
Wei Yongjune89c2092008-12-25 16:54:58 -08002964 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2965 * This option will get or set the maximum size to put in any outgoing
2966 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * fragmented by SCTP into the specified size. Note that the underlying
2968 * SCTP implementation may fragment into smaller sized chunks when the
2969 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08002970 * the user. The default value for this option is '0' which indicates
2971 * the user is NOT limiting fragmentation and only the PMTU will effect
2972 * SCTP's choice of DATA chunk size. Note also that values set larger
2973 * than the maximum size of an IP datagram will effectively let SCTP
2974 * control fragmentation (i.e. the same as setting this option to 0).
2975 *
2976 * The following structure is used to access and modify this parameter:
2977 *
2978 * struct sctp_assoc_value {
2979 * sctp_assoc_t assoc_id;
2980 * uint32_t assoc_value;
2981 * };
2982 *
2983 * assoc_id: This parameter is ignored for one-to-one style sockets.
2984 * For one-to-many style sockets this parameter indicates which
2985 * association the user is performing an action upon. Note that if
2986 * this field's value is zero then the endpoints default value is
2987 * changed (effecting future associations only).
2988 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 */
David S. Millerb7058842009-09-30 16:12:20 -07002990static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Wei Yongjune89c2092008-12-25 16:54:58 -08002992 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct sctp_sock *sp = sctp_sk(sk);
2995 int val;
2996
Wei Yongjune89c2092008-12-25 16:54:58 -08002997 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00002998 pr_warn("Use of int in maxseg socket option deprecated\n");
2999 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08003000 if (copy_from_user(&val, optval, optlen))
3001 return -EFAULT;
3002 params.assoc_id = 0;
3003 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3004 if (copy_from_user(&params, optval, optlen))
3005 return -EFAULT;
3006 val = params.assoc_value;
3007 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003009
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07003010 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Wei Yongjune89c2092008-12-25 16:54:58 -08003013 asoc = sctp_id2assoc(sk, params.assoc_id);
3014 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3015 return -EINVAL;
3016
3017 if (asoc) {
3018 if (val == 0) {
3019 val = asoc->pathmtu;
3020 val -= sp->pf->af->net_header_len;
3021 val -= sizeof(struct sctphdr) +
3022 sizeof(struct sctp_data_chunk);
3023 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003024 asoc->user_frag = val;
3025 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003026 } else {
3027 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029
3030 return 0;
3031}
3032
3033
3034/*
3035 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3036 *
3037 * Requests that the peer mark the enclosed address as the association
3038 * primary. The enclosed address must be one of the association's
3039 * locally bound addresses. The following structure is used to make a
3040 * set primary request:
3041 */
3042static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003043 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003045 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 struct sctp_association *asoc = NULL;
3048 struct sctp_setpeerprim prim;
3049 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003050 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 int err;
3052
3053 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003055 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return -EPERM;
3057
3058 if (optlen != sizeof(struct sctp_setpeerprim))
3059 return -EINVAL;
3060
3061 if (copy_from_user(&prim, optval, optlen))
3062 return -EFAULT;
3063
3064 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003065 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return -EINVAL;
3067
3068 if (!asoc->peer.asconf_capable)
3069 return -EPERM;
3070
3071 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3072 return -EPERM;
3073
3074 if (!sctp_state(asoc, ESTABLISHED))
3075 return -ENOTCONN;
3076
Wei Yongjun40a01032010-12-07 17:11:09 +00003077 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3078 if (!af)
3079 return -EINVAL;
3080
3081 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3082 return -EADDRNOTAVAIL;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3085 return -EADDRNOTAVAIL;
3086
3087 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3088 chunk = sctp_make_asconf_set_prim(asoc,
3089 (union sctp_addr *)&prim.sspp_addr);
3090 if (!chunk)
3091 return -ENOMEM;
3092
3093 err = sctp_send_asconf(asoc, chunk);
3094
3095 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3096
3097 return err;
3098}
3099
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003100static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003101 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003103 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003105 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003107 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return -EFAULT;
3109
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003110 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 return 0;
3113}
3114
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003115/*
3116 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3117 *
3118 * The context field in the sctp_sndrcvinfo structure is normally only
3119 * used when a failed message is retrieved holding the value that was
3120 * sent down on the actual send call. This option allows the setting of
3121 * a default context on an association basis that will be received on
3122 * reading messages from the peer. This is especially helpful in the
3123 * one-2-many model for an application to keep some reference to an
3124 * internal state machine that is processing messages on the
3125 * association. Note that the setting of this value only effects
3126 * received messages from the peer and does not effect the value that is
3127 * saved with outbound messages.
3128 */
3129static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003130 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003131{
3132 struct sctp_assoc_value params;
3133 struct sctp_sock *sp;
3134 struct sctp_association *asoc;
3135
3136 if (optlen != sizeof(struct sctp_assoc_value))
3137 return -EINVAL;
3138 if (copy_from_user(&params, optval, optlen))
3139 return -EFAULT;
3140
3141 sp = sctp_sk(sk);
3142
3143 if (params.assoc_id != 0) {
3144 asoc = sctp_id2assoc(sk, params.assoc_id);
3145 if (!asoc)
3146 return -EINVAL;
3147 asoc->default_rcv_context = params.assoc_value;
3148 } else {
3149 sp->default_rcv_context = params.assoc_value;
3150 }
3151
3152 return 0;
3153}
3154
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003155/*
3156 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3157 *
3158 * This options will at a minimum specify if the implementation is doing
3159 * fragmented interleave. Fragmented interleave, for a one to many
3160 * socket, is when subsequent calls to receive a message may return
3161 * parts of messages from different associations. Some implementations
3162 * may allow you to turn this value on or off. If so, when turned off,
3163 * no fragment interleave will occur (which will cause a head of line
3164 * blocking amongst multiple associations sharing the same one to many
3165 * socket). When this option is turned on, then each receive call may
3166 * come from a different association (thus the user must receive data
3167 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3168 * association each receive belongs to.
3169 *
3170 * This option takes a boolean value. A non-zero value indicates that
3171 * fragmented interleave is on. A value of zero indicates that
3172 * fragmented interleave is off.
3173 *
3174 * Note that it is important that an implementation that allows this
3175 * option to be turned on, have it off by default. Otherwise an unaware
3176 * application using the one to many model may become confused and act
3177 * incorrectly.
3178 */
3179static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3180 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003181 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003182{
3183 int val;
3184
3185 if (optlen != sizeof(int))
3186 return -EINVAL;
3187 if (get_user(val, (int __user *)optval))
3188 return -EFAULT;
3189
3190 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3191
3192 return 0;
3193}
3194
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003195/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003196 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003197 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003198 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003199 * This option will set or get the SCTP partial delivery point. This
3200 * point is the size of a message where the partial delivery API will be
3201 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003202 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003203 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003204 * point. Note also that the call will fail if the user attempts to set
3205 * this value larger than the socket receive buffer size.
3206 *
3207 * Note that any single message having a length smaller than or equal to
3208 * the SCTP partial delivery point will be delivered in one single read
3209 * call as long as the user provided buffer is large enough to hold the
3210 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003211 */
3212static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3213 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003214 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003215{
3216 u32 val;
3217
3218 if (optlen != sizeof(u32))
3219 return -EINVAL;
3220 if (get_user(val, (int __user *)optval))
3221 return -EFAULT;
3222
Wei Yongjun8510b932008-12-25 16:59:03 -08003223 /* Note: We double the receive buffer from what the user sets
3224 * it to be, also initial rwnd is based on rcvbuf/2.
3225 */
3226 if (val > (sk->sk_rcvbuf >> 1))
3227 return -EINVAL;
3228
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003229 sctp_sk(sk)->pd_point = val;
3230
3231 return 0; /* is this the right error code? */
3232}
3233
Vlad Yasevich70331572007-03-23 11:34:36 -07003234/*
3235 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3236 *
3237 * This option will allow a user to change the maximum burst of packets
3238 * that can be emitted by this association. Note that the default value
3239 * is 4, and some implementations may restrict this setting so that it
3240 * can only be lowered.
3241 *
3242 * NOTE: This text doesn't seem right. Do this on a socket basis with
3243 * future associations inheriting the socket value.
3244 */
3245static int sctp_setsockopt_maxburst(struct sock *sk,
3246 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003247 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003248{
Neil Horman219b99a2008-03-05 13:44:46 -08003249 struct sctp_assoc_value params;
3250 struct sctp_sock *sp;
3251 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003252 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003253 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003254
Neil Horman219b99a2008-03-05 13:44:46 -08003255 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00003256 pr_warn("Use of int in max_burst socket option deprecated\n");
3257 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08003258 if (copy_from_user(&val, optval, optlen))
3259 return -EFAULT;
3260 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3261 if (copy_from_user(&params, optval, optlen))
3262 return -EFAULT;
3263 val = params.assoc_value;
3264 assoc_id = params.assoc_id;
3265 } else
3266 return -EINVAL;
3267
3268 sp = sctp_sk(sk);
3269
3270 if (assoc_id != 0) {
3271 asoc = sctp_id2assoc(sk, assoc_id);
3272 if (!asoc)
3273 return -EINVAL;
3274 asoc->max_burst = val;
3275 } else
3276 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003277
3278 return 0;
3279}
3280
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003281/*
3282 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3283 *
3284 * This set option adds a chunk type that the user is requesting to be
3285 * received only in an authenticated way. Changes to the list of chunks
3286 * will only effect future associations on the socket.
3287 */
3288static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003289 char __user *optval,
3290 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003291{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003292 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003293 struct sctp_authchunk val;
3294
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003295 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003296 return -EACCES;
3297
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003298 if (optlen != sizeof(struct sctp_authchunk))
3299 return -EINVAL;
3300 if (copy_from_user(&val, optval, optlen))
3301 return -EFAULT;
3302
3303 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003304 case SCTP_CID_INIT:
3305 case SCTP_CID_INIT_ACK:
3306 case SCTP_CID_SHUTDOWN_COMPLETE:
3307 case SCTP_CID_AUTH:
3308 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003309 }
3310
3311 /* add this chunk id to the endpoint */
3312 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3313}
3314
3315/*
3316 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3317 *
3318 * This option gets or sets the list of HMAC algorithms that the local
3319 * endpoint requires the peer to use.
3320 */
3321static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003322 char __user *optval,
3323 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003324{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003325 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003326 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003327 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003328 int err;
3329
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003330 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003331 return -EACCES;
3332
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003333 if (optlen < sizeof(struct sctp_hmacalgo))
3334 return -EINVAL;
3335
Shan Wei934253a2011-04-18 19:13:18 +00003336 hmacs= memdup_user(optval, optlen);
3337 if (IS_ERR(hmacs))
3338 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003339
Vlad Yasevichd9724052008-08-27 16:09:49 -07003340 idents = hmacs->shmac_num_idents;
3341 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3342 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003343 err = -EINVAL;
3344 goto out;
3345 }
3346
3347 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3348out:
3349 kfree(hmacs);
3350 return err;
3351}
3352
3353/*
3354 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3355 *
3356 * This option will set a shared secret key which is used to build an
3357 * association shared key.
3358 */
3359static int sctp_setsockopt_auth_key(struct sock *sk,
3360 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003361 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003362{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003363 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003364 struct sctp_authkey *authkey;
3365 struct sctp_association *asoc;
3366 int ret;
3367
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003368 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003369 return -EACCES;
3370
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003371 if (optlen <= sizeof(struct sctp_authkey))
3372 return -EINVAL;
3373
Shan Wei934253a2011-04-18 19:13:18 +00003374 authkey= memdup_user(optval, optlen);
3375 if (IS_ERR(authkey))
3376 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003377
Vlad Yasevich328fc472008-08-27 16:08:54 -07003378 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003379 ret = -EINVAL;
3380 goto out;
3381 }
3382
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003383 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3384 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3385 ret = -EINVAL;
3386 goto out;
3387 }
3388
3389 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3390out:
3391 kfree(authkey);
3392 return ret;
3393}
3394
3395/*
3396 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3397 *
3398 * This option will get or set the active shared key to be used to build
3399 * the association shared key.
3400 */
3401static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003402 char __user *optval,
3403 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003404{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003405 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003406 struct sctp_authkeyid val;
3407 struct sctp_association *asoc;
3408
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003409 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003410 return -EACCES;
3411
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003412 if (optlen != sizeof(struct sctp_authkeyid))
3413 return -EINVAL;
3414 if (copy_from_user(&val, optval, optlen))
3415 return -EFAULT;
3416
3417 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3418 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3419 return -EINVAL;
3420
3421 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3422 val.scact_keynumber);
3423}
3424
3425/*
3426 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3427 *
3428 * This set option will delete a shared secret key from use.
3429 */
3430static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003431 char __user *optval,
3432 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003433{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003434 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003435 struct sctp_authkeyid val;
3436 struct sctp_association *asoc;
3437
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003438 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003439 return -EACCES;
3440
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003441 if (optlen != sizeof(struct sctp_authkeyid))
3442 return -EINVAL;
3443 if (copy_from_user(&val, optval, optlen))
3444 return -EFAULT;
3445
3446 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3447 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3448 return -EINVAL;
3449
3450 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3451 val.scact_keynumber);
3452
3453}
3454
Michio Honda7dc04d72011-04-26 20:16:31 +09003455/*
3456 * 8.1.23 SCTP_AUTO_ASCONF
3457 *
3458 * This option will enable or disable the use of the automatic generation of
3459 * ASCONF chunks to add and delete addresses to an existing association. Note
3460 * that this option has two caveats namely: a) it only affects sockets that
3461 * are bound to all addresses available to the SCTP stack, and b) the system
3462 * administrator may have an overriding control that turns the ASCONF feature
3463 * off no matter what setting the socket option may have.
3464 * This option expects an integer boolean flag, where a non-zero value turns on
3465 * the option, and a zero value turns off the option.
3466 * Note. In this implementation, socket operation overrides default parameter
3467 * being set by sysctl as well as FreeBSD implementation
3468 */
3469static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3470 unsigned int optlen)
3471{
3472 int val;
3473 struct sctp_sock *sp = sctp_sk(sk);
3474
3475 if (optlen < sizeof(int))
3476 return -EINVAL;
3477 if (get_user(val, (int __user *)optval))
3478 return -EFAULT;
3479 if (!sctp_is_ep_boundall(sk) && val)
3480 return -EINVAL;
3481 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3482 return 0;
3483
3484 if (val == 0 && sp->do_auto_asconf) {
3485 list_del(&sp->auto_asconf_list);
3486 sp->do_auto_asconf = 0;
3487 } else if (val && !sp->do_auto_asconf) {
3488 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003489 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003490 sp->do_auto_asconf = 1;
3491 }
3492 return 0;
3493}
3494
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003495
Neil Horman5aa93bc2012-07-21 07:56:07 +00003496/*
3497 * SCTP_PEER_ADDR_THLDS
3498 *
3499 * This option allows us to alter the partially failed threshold for one or all
3500 * transports in an association. See Section 6.1 of:
3501 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3502 */
3503static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3504 char __user *optval,
3505 unsigned int optlen)
3506{
3507 struct sctp_paddrthlds val;
3508 struct sctp_transport *trans;
3509 struct sctp_association *asoc;
3510
3511 if (optlen < sizeof(struct sctp_paddrthlds))
3512 return -EINVAL;
3513 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3514 sizeof(struct sctp_paddrthlds)))
3515 return -EFAULT;
3516
3517
3518 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3519 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3520 if (!asoc)
3521 return -ENOENT;
3522 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3523 transports) {
3524 if (val.spt_pathmaxrxt)
3525 trans->pathmaxrxt = val.spt_pathmaxrxt;
3526 trans->pf_retrans = val.spt_pathpfthld;
3527 }
3528
3529 if (val.spt_pathmaxrxt)
3530 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3531 asoc->pf_retrans = val.spt_pathpfthld;
3532 } else {
3533 trans = sctp_addr_id2transport(sk, &val.spt_address,
3534 val.spt_assoc_id);
3535 if (!trans)
3536 return -ENOENT;
3537
3538 if (val.spt_pathmaxrxt)
3539 trans->pathmaxrxt = val.spt_pathmaxrxt;
3540 trans->pf_retrans = val.spt_pathpfthld;
3541 }
3542
3543 return 0;
3544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546/* API 6.2 setsockopt(), getsockopt()
3547 *
3548 * Applications use setsockopt() and getsockopt() to set or retrieve
3549 * socket options. Socket options are used to change the default
3550 * behavior of sockets calls. They are described in Section 7.
3551 *
3552 * The syntax is:
3553 *
3554 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3555 * int __user *optlen);
3556 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3557 * int optlen);
3558 *
3559 * sd - the socket descript.
3560 * level - set to IPPROTO_SCTP for all SCTP options.
3561 * optname - the option name.
3562 * optval - the buffer to store the value of the option.
3563 * optlen - the size of the buffer.
3564 */
3565SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003566 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int retval = 0;
3569
3570 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3571 sk, optname);
3572
3573 /* I can hardly begin to describe how wrong this is. This is
3574 * so broken as to be worse than useless. The API draft
3575 * REALLY is NOT helpful here... I am not convinced that the
3576 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3577 * are at all well-founded.
3578 */
3579 if (level != SOL_SCTP) {
3580 struct sctp_af *af = sctp_sk(sk)->pf->af;
3581 retval = af->setsockopt(sk, level, optname, optval, optlen);
3582 goto out_nounlock;
3583 }
3584
3585 sctp_lock_sock(sk);
3586
3587 switch (optname) {
3588 case SCTP_SOCKOPT_BINDX_ADD:
3589 /* 'optlen' is the size of the addresses buffer. */
3590 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3591 optlen, SCTP_BINDX_ADD_ADDR);
3592 break;
3593
3594 case SCTP_SOCKOPT_BINDX_REM:
3595 /* 'optlen' is the size of the addresses buffer. */
3596 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3597 optlen, SCTP_BINDX_REM_ADDR);
3598 break;
3599
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003600 case SCTP_SOCKOPT_CONNECTX_OLD:
3601 /* 'optlen' is the size of the addresses buffer. */
3602 retval = sctp_setsockopt_connectx_old(sk,
3603 (struct sockaddr __user *)optval,
3604 optlen);
3605 break;
3606
Frank Filz3f7a87d2005-06-20 13:14:57 -07003607 case SCTP_SOCKOPT_CONNECTX:
3608 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003609 retval = sctp_setsockopt_connectx(sk,
3610 (struct sockaddr __user *)optval,
3611 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003612 break;
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 case SCTP_DISABLE_FRAGMENTS:
3615 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3616 break;
3617
3618 case SCTP_EVENTS:
3619 retval = sctp_setsockopt_events(sk, optval, optlen);
3620 break;
3621
3622 case SCTP_AUTOCLOSE:
3623 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3624 break;
3625
3626 case SCTP_PEER_ADDR_PARAMS:
3627 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3628 break;
3629
Shan Wei4580ccc2011-01-18 22:39:00 +00003630 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003631 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003632 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003633 case SCTP_PARTIAL_DELIVERY_POINT:
3634 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3635 break;
Frank Filz77086102005-12-22 11:37:30 -08003636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 case SCTP_INITMSG:
3638 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3639 break;
3640 case SCTP_DEFAULT_SEND_PARAM:
3641 retval = sctp_setsockopt_default_send_param(sk, optval,
3642 optlen);
3643 break;
3644 case SCTP_PRIMARY_ADDR:
3645 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3646 break;
3647 case SCTP_SET_PEER_PRIMARY_ADDR:
3648 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3649 break;
3650 case SCTP_NODELAY:
3651 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3652 break;
3653 case SCTP_RTOINFO:
3654 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3655 break;
3656 case SCTP_ASSOCINFO:
3657 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3658 break;
3659 case SCTP_I_WANT_MAPPED_V4_ADDR:
3660 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3661 break;
3662 case SCTP_MAXSEG:
3663 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3664 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003665 case SCTP_ADAPTATION_LAYER:
3666 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003668 case SCTP_CONTEXT:
3669 retval = sctp_setsockopt_context(sk, optval, optlen);
3670 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003671 case SCTP_FRAGMENT_INTERLEAVE:
3672 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3673 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003674 case SCTP_MAX_BURST:
3675 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3676 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003677 case SCTP_AUTH_CHUNK:
3678 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3679 break;
3680 case SCTP_HMAC_IDENT:
3681 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3682 break;
3683 case SCTP_AUTH_KEY:
3684 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3685 break;
3686 case SCTP_AUTH_ACTIVE_KEY:
3687 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3688 break;
3689 case SCTP_AUTH_DELETE_KEY:
3690 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3691 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003692 case SCTP_AUTO_ASCONF:
3693 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3694 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00003695 case SCTP_PEER_ADDR_THLDS:
3696 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 default:
3699 retval = -ENOPROTOOPT;
3700 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 sctp_release_sock(sk);
3704
3705out_nounlock:
3706 return retval;
3707}
3708
3709/* API 3.1.6 connect() - UDP Style Syntax
3710 *
3711 * An application may use the connect() call in the UDP model to initiate an
3712 * association without sending data.
3713 *
3714 * The syntax is:
3715 *
3716 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3717 *
3718 * sd: the socket descriptor to have a new association added to.
3719 *
3720 * nam: the address structure (either struct sockaddr_in or struct
3721 * sockaddr_in6 defined in RFC2553 [7]).
3722 *
3723 * len: the size of the address.
3724 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003725SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 int addr_len)
3727{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003729 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
3731 sctp_lock_sock(sk);
3732
Frank Filz3f7a87d2005-06-20 13:14:57 -07003733 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08003734 __func__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Frank Filz3f7a87d2005-06-20 13:14:57 -07003736 /* Validate addr_len before calling common connect/connectx routine. */
3737 af = sctp_get_af_specific(addr->sa_family);
3738 if (!af || addr_len < af->sockaddr_len) {
3739 err = -EINVAL;
3740 } else {
3741 /* Pass correct addr len to common routine (so it knows there
3742 * is only one address being passed.
3743 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003744 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return err;
3749}
3750
3751/* FIXME: Write comments. */
3752SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3753{
3754 return -EOPNOTSUPP; /* STUB */
3755}
3756
3757/* 4.1.4 accept() - TCP Style Syntax
3758 *
3759 * Applications use accept() call to remove an established SCTP
3760 * association from the accept queue of the endpoint. A new socket
3761 * descriptor will be returned from accept() to represent the newly
3762 * formed association.
3763 */
3764SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3765{
3766 struct sctp_sock *sp;
3767 struct sctp_endpoint *ep;
3768 struct sock *newsk = NULL;
3769 struct sctp_association *asoc;
3770 long timeo;
3771 int error = 0;
3772
3773 sctp_lock_sock(sk);
3774
3775 sp = sctp_sk(sk);
3776 ep = sp->ep;
3777
3778 if (!sctp_style(sk, TCP)) {
3779 error = -EOPNOTSUPP;
3780 goto out;
3781 }
3782
3783 if (!sctp_sstate(sk, LISTENING)) {
3784 error = -EINVAL;
3785 goto out;
3786 }
3787
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003788 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 error = sctp_wait_for_accept(sk, timeo);
3791 if (error)
3792 goto out;
3793
3794 /* We treat the list of associations on the endpoint as the accept
3795 * queue and pick the first association on the list.
3796 */
3797 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3798
3799 newsk = sp->pf->create_accept_sk(sk, asoc);
3800 if (!newsk) {
3801 error = -ENOMEM;
3802 goto out;
3803 }
3804
3805 /* Populate the fields of the newsk from the oldsk and migrate the
3806 * asoc to the newsk.
3807 */
3808 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3809
3810out:
3811 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003812 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return newsk;
3814}
3815
3816/* The SCTP ioctl handler. */
3817SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3818{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003819 int rc = -ENOTCONN;
3820
3821 sctp_lock_sock(sk);
3822
3823 /*
3824 * SEQPACKET-style sockets in LISTENING state are valid, for
3825 * SCTP, so only discard TCP-style sockets in LISTENING state.
3826 */
3827 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3828 goto out;
3829
3830 switch (cmd) {
3831 case SIOCINQ: {
3832 struct sk_buff *skb;
3833 unsigned int amount = 0;
3834
3835 skb = skb_peek(&sk->sk_receive_queue);
3836 if (skb != NULL) {
3837 /*
3838 * We will only return the amount of this packet since
3839 * that is all that will be read.
3840 */
3841 amount = skb->len;
3842 }
3843 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003844 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07003845 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003846 default:
3847 rc = -ENOIOCTLCMD;
3848 break;
3849 }
3850out:
3851 sctp_release_sock(sk);
3852 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
3855/* This is the function which gets called during socket creation to
3856 * initialized the SCTP-specific portion of the sock.
3857 * The sock structure should already be zero-filled memory.
3858 */
3859SCTP_STATIC int sctp_init_sock(struct sock *sk)
3860{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003861 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 struct sctp_endpoint *ep;
3863 struct sctp_sock *sp;
3864
3865 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3866
3867 sp = sctp_sk(sk);
3868
3869 /* Initialize the SCTP per socket area. */
3870 switch (sk->sk_type) {
3871 case SOCK_SEQPACKET:
3872 sp->type = SCTP_SOCKET_UDP;
3873 break;
3874 case SOCK_STREAM:
3875 sp->type = SCTP_SOCKET_TCP;
3876 break;
3877 default:
3878 return -ESOCKTNOSUPPORT;
3879 }
3880
3881 /* Initialize default send parameters. These parameters can be
3882 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3883 */
3884 sp->default_stream = 0;
3885 sp->default_ppid = 0;
3886 sp->default_flags = 0;
3887 sp->default_context = 0;
3888 sp->default_timetolive = 0;
3889
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003890 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003891 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 /* Initialize default setup parameters. These parameters
3894 * can be modified with the SCTP_INITMSG socket option or
3895 * overridden by the SCTP_INIT CMSG.
3896 */
3897 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3898 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003899 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
3900 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 /* Initialize default RTO related parameters. These parameters can
3903 * be modified for with the SCTP_RTOINFO socket option.
3904 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003905 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
3906 sp->rtoinfo.srto_max = net->sctp.rto_max;
3907 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 /* Initialize default association related parameters. These parameters
3910 * can be modified with the SCTP_ASSOCINFO socket option.
3911 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003912 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 sp->assocparams.sasoc_number_peer_destinations = 0;
3914 sp->assocparams.sasoc_peer_rwnd = 0;
3915 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003916 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003919 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 */
3921 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3922
3923 /* Default Peer Address Parameters. These defaults can
3924 * be modified via SCTP_PEER_ADDR_PARAMS
3925 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003926 sp->hbinterval = net->sctp.hb_interval;
3927 sp->pathmaxrxt = net->sctp.max_retrans_path;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003928 sp->pathmtu = 0; // allow default discovery
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003929 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07003930 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003931 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003932 SPP_PMTUD_ENABLE |
3933 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 /* If enabled no SCTP message fragmentation will be performed.
3936 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3937 */
3938 sp->disable_fragments = 0;
3939
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003940 /* Enable Nagle algorithm by default. */
3941 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 /* Enable by default. */
3944 sp->v4mapped = 1;
3945
3946 /* Auto-close idle associations after the configured
3947 * number of seconds. A value of 0 disables this
3948 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3949 * for UDP-style sockets only.
3950 */
3951 sp->autoclose = 0;
3952
3953 /* User specified fragmentation limit. */
3954 sp->user_frag = 0;
3955
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003956 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 sp->pf = sctp_get_pf_specific(sk->sk_family);
3959
3960 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003961 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003963 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
3965 /* Create a per socket endpoint structure. Even if we
3966 * change the data structure relationships, this may still
3967 * be useful for storing pre-connect address information.
3968 */
3969 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3970 if (!ep)
3971 return -ENOMEM;
3972
3973 sp->ep = ep;
3974 sp->hmac = NULL;
3975
3976 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08003977
3978 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003979 percpu_counter_inc(&sctp_sockets_allocated);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003980 sock_prot_inuse_add(net, sk->sk_prot, 1);
3981 if (net->sctp.default_auto_asconf) {
Michio Honda9f7d6532011-04-26 19:32:51 +09003982 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003983 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09003984 sp->do_auto_asconf = 1;
3985 } else
3986 sp->do_auto_asconf = 0;
David S. Miller6f756a82008-11-23 17:34:03 -08003987 local_bh_enable();
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return 0;
3990}
3991
3992/* Cleanup any SCTP per socket resources. */
Brian Haley7d06b2e2008-06-14 17:04:49 -07003993SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Michio Honda9f7d6532011-04-26 19:32:51 +09003995 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3998
3999 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004000 sp = sctp_sk(sk);
4001 if (sp->do_auto_asconf) {
4002 sp->do_auto_asconf = 0;
4003 list_del(&sp->auto_asconf_list);
4004 }
4005 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004006 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00004007 percpu_counter_dec(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004008 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004009 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012/* API 4.1.7 shutdown() - TCP Style Syntax
4013 * int shutdown(int socket, int how);
4014 *
4015 * sd - the socket descriptor of the association to be closed.
4016 * how - Specifies the type of shutdown. The values are
4017 * as follows:
4018 * SHUT_RD
4019 * Disables further receive operations. No SCTP
4020 * protocol action is taken.
4021 * SHUT_WR
4022 * Disables further send operations, and initiates
4023 * the SCTP shutdown sequence.
4024 * SHUT_RDWR
4025 * Disables further send and receive operations
4026 * and initiates the SCTP shutdown sequence.
4027 */
4028SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4029{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004030 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct sctp_endpoint *ep;
4032 struct sctp_association *asoc;
4033
4034 if (!sctp_style(sk, TCP))
4035 return;
4036
4037 if (how & SEND_SHUTDOWN) {
4038 ep = sctp_sk(sk)->ep;
4039 if (!list_empty(&ep->asocs)) {
4040 asoc = list_entry(ep->asocs.next,
4041 struct sctp_association, asocs);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004042 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
4044 }
4045}
4046
4047/* 7.2.1 Association Status (SCTP_STATUS)
4048
4049 * Applications can retrieve current status information about an
4050 * association, including association state, peer receiver window size,
4051 * number of unacked data chunks, and number of data chunks pending
4052 * receipt. This information is read-only.
4053 */
4054static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4055 char __user *optval,
4056 int __user *optlen)
4057{
4058 struct sctp_status status;
4059 struct sctp_association *asoc = NULL;
4060 struct sctp_transport *transport;
4061 sctp_assoc_t associd;
4062 int retval = 0;
4063
Neil Horman408f22e2007-06-16 14:03:45 -04004064 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 retval = -EINVAL;
4066 goto out;
4067 }
4068
Neil Horman408f22e2007-06-16 14:03:45 -04004069 len = sizeof(status);
4070 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 retval = -EFAULT;
4072 goto out;
4073 }
4074
4075 associd = status.sstat_assoc_id;
4076 asoc = sctp_id2assoc(sk, associd);
4077 if (!asoc) {
4078 retval = -EINVAL;
4079 goto out;
4080 }
4081
4082 transport = asoc->peer.primary_path;
4083
4084 status.sstat_assoc_id = sctp_assoc2id(asoc);
4085 status.sstat_state = asoc->state;
4086 status.sstat_rwnd = asoc->peer.rwnd;
4087 status.sstat_unackdata = asoc->unack_data;
4088
4089 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4090 status.sstat_instrms = asoc->c.sinit_max_instreams;
4091 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4092 status.sstat_fragmentation_point = asoc->frag_point;
4093 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004094 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4095 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 /* Map ipv4 address into v4-mapped-on-v6 address. */
4097 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4098 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004099 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4101 status.sstat_primary.spinfo_srtt = transport->srtt;
4102 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004103 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Frank Filz3f7a87d2005-06-20 13:14:57 -07004105 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4106 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 if (put_user(len, optlen)) {
4109 retval = -EFAULT;
4110 goto out;
4111 }
4112
4113 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4114 len, status.sstat_state, status.sstat_rwnd,
4115 status.sstat_assoc_id);
4116
4117 if (copy_to_user(optval, &status, len)) {
4118 retval = -EFAULT;
4119 goto out;
4120 }
4121
4122out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004123 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
4126
4127/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4128 *
4129 * Applications can retrieve information about a specific peer address
4130 * of an association, including its reachability state, congestion
4131 * window, and retransmission timer values. This information is
4132 * read-only.
4133 */
4134static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4135 char __user *optval,
4136 int __user *optlen)
4137{
4138 struct sctp_paddrinfo pinfo;
4139 struct sctp_transport *transport;
4140 int retval = 0;
4141
Neil Horman408f22e2007-06-16 14:03:45 -04004142 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 retval = -EINVAL;
4144 goto out;
4145 }
4146
Neil Horman408f22e2007-06-16 14:03:45 -04004147 len = sizeof(pinfo);
4148 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 retval = -EFAULT;
4150 goto out;
4151 }
4152
4153 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4154 pinfo.spinfo_assoc_id);
4155 if (!transport)
4156 return -EINVAL;
4157
4158 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004159 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 pinfo.spinfo_cwnd = transport->cwnd;
4161 pinfo.spinfo_srtt = transport->srtt;
4162 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004163 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Frank Filz3f7a87d2005-06-20 13:14:57 -07004165 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4166 pinfo.spinfo_state = SCTP_ACTIVE;
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (put_user(len, optlen)) {
4169 retval = -EFAULT;
4170 goto out;
4171 }
4172
4173 if (copy_to_user(optval, &pinfo, len)) {
4174 retval = -EFAULT;
4175 goto out;
4176 }
4177
4178out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004179 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
4182/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4183 *
4184 * This option is a on/off flag. If enabled no SCTP message
4185 * fragmentation will be performed. Instead if a message being sent
4186 * exceeds the current PMTU size, the message will NOT be sent and
4187 * instead a error will be indicated to the user.
4188 */
4189static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4190 char __user *optval, int __user *optlen)
4191{
4192 int val;
4193
4194 if (len < sizeof(int))
4195 return -EINVAL;
4196
4197 len = sizeof(int);
4198 val = (sctp_sk(sk)->disable_fragments == 1);
4199 if (put_user(len, optlen))
4200 return -EFAULT;
4201 if (copy_to_user(optval, &val, len))
4202 return -EFAULT;
4203 return 0;
4204}
4205
4206/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4207 *
4208 * This socket option is used to specify various notifications and
4209 * ancillary data the user wishes to receive.
4210 */
4211static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4212 int __user *optlen)
4213{
Thomas Grafacdd5982012-04-03 22:17:53 +00004214 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004216 if (len > sizeof(struct sctp_event_subscribe))
4217 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004218 if (put_user(len, optlen))
4219 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4221 return -EFAULT;
4222 return 0;
4223}
4224
4225/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4226 *
4227 * This socket option is applicable to the UDP-style socket only. When
4228 * set it will cause associations that are idle for more than the
4229 * specified number of seconds to automatically close. An association
4230 * being idle is defined an association that has NOT sent or received
4231 * user data. The special value of '0' indicates that no automatic
4232 * close of any associations should be performed. The option expects an
4233 * integer defining the number of seconds of idle time before an
4234 * association is closed.
4235 */
4236static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4237{
4238 /* Applicable to UDP-style socket only */
4239 if (sctp_style(sk, TCP))
4240 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004241 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004243 len = sizeof(int);
4244 if (put_user(len, optlen))
4245 return -EFAULT;
4246 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 return -EFAULT;
4248 return 0;
4249}
4250
4251/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004252int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004254 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 struct socket *sock;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004256 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 int err = 0;
4258
Benjamin Poirier0343c552012-03-08 05:55:58 +00004259 if (!asoc)
4260 return -EINVAL;
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 /* An association cannot be branched off from an already peeled-off
4263 * socket, nor is this supported for tcp style sockets.
4264 */
4265 if (!sctp_style(sk, UDP))
4266 return -EINVAL;
4267
4268 /* Create a new socket. */
4269 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4270 if (err < 0)
4271 return err;
4272
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004273 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004274
4275 /* Make peeled-off sockets more like 1-1 accepted sockets.
4276 * Set the daddr and initialize id to something more random
4277 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004278 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4279 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004280
4281 /* Populate the fields of the newsk from the oldsk and migrate the
4282 * asoc to the newsk.
4283 */
4284 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 *sockp = sock;
4287
4288 return err;
4289}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004290EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4293{
4294 sctp_peeloff_arg_t peeloff;
4295 struct socket *newsock;
Al Viro56b31d12012-08-18 00:25:51 -04004296 struct file *newfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Neil Horman408f22e2007-06-16 14:03:45 -04004299 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004301 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (copy_from_user(&peeloff, optval, len))
4303 return -EFAULT;
4304
Benjamin Poirier0343c552012-03-08 05:55:58 +00004305 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 if (retval < 0)
4307 goto out;
4308
4309 /* Map the socket to an unused fd that can be returned to the user. */
Al Viro56b31d12012-08-18 00:25:51 -04004310 retval = get_unused_fd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (retval < 0) {
4312 sock_release(newsock);
4313 goto out;
4314 }
4315
Linus Torvaldsaab174f2012-10-02 20:25:04 -07004316 newfile = sock_alloc_file(newsock, 0, NULL);
Al Viro56b31d12012-08-18 00:25:51 -04004317 if (unlikely(IS_ERR(newfile))) {
4318 put_unused_fd(retval);
4319 sock_release(newsock);
4320 return PTR_ERR(newfile);
4321 }
4322
Benjamin Poirier0343c552012-03-08 05:55:58 +00004323 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4324 __func__, sk, newsock->sk, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04004327 if (put_user(len, optlen)) {
4328 fput(newfile);
4329 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04004330 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04004331 }
4332 peeloff.sd = retval;
4333 if (copy_to_user(optval, &peeloff, len)) {
4334 fput(newfile);
4335 put_unused_fd(retval);
4336 return -EFAULT;
4337 }
4338 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339out:
4340 return retval;
4341}
4342
4343/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4344 *
4345 * Applications can enable or disable heartbeats for any peer address of
4346 * an association, modify an address's heartbeat interval, force a
4347 * heartbeat to be sent immediately, and adjust the address's maximum
4348 * number of retransmissions sent before an address is considered
4349 * unreachable. The following structure is used to access and modify an
4350 * address's parameters:
4351 *
4352 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004353 * sctp_assoc_t spp_assoc_id;
4354 * struct sockaddr_storage spp_address;
4355 * uint32_t spp_hbinterval;
4356 * uint16_t spp_pathmaxrxt;
4357 * uint32_t spp_pathmtu;
4358 * uint32_t spp_sackdelay;
4359 * uint32_t spp_flags;
4360 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004362 * spp_assoc_id - (one-to-many style socket) This is filled in the
4363 * application, and identifies the association for
4364 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 * spp_address - This specifies which address is of interest.
4366 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004367 * in milliseconds. If a value of zero
4368 * is present in this field then no changes are to
4369 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 * spp_pathmaxrxt - This contains the maximum number of
4371 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004372 * considered unreachable. If a value of zero
4373 * is present in this field then no changes are to
4374 * be made to this parameter.
4375 * spp_pathmtu - When Path MTU discovery is disabled the value
4376 * specified here will be the "fixed" path mtu.
4377 * Note that if the spp_address field is empty
4378 * then all associations on this address will
4379 * have this fixed path mtu set upon them.
4380 *
4381 * spp_sackdelay - When delayed sack is enabled, this value specifies
4382 * the number of milliseconds that sacks will be delayed
4383 * for. This value will apply to all addresses of an
4384 * association if the spp_address field is empty. Note
4385 * also, that if delayed sack is enabled and this
4386 * value is set to 0, no change is made to the last
4387 * recorded delayed sack timer value.
4388 *
4389 * spp_flags - These flags are used to control various features
4390 * on an association. The flag field may contain
4391 * zero or more of the following options.
4392 *
4393 * SPP_HB_ENABLE - Enable heartbeats on the
4394 * specified address. Note that if the address
4395 * field is empty all addresses for the association
4396 * have heartbeats enabled upon them.
4397 *
4398 * SPP_HB_DISABLE - Disable heartbeats on the
4399 * speicifed address. Note that if the address
4400 * field is empty all addresses for the association
4401 * will have their heartbeats disabled. Note also
4402 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4403 * mutually exclusive, only one of these two should
4404 * be specified. Enabling both fields will have
4405 * undetermined results.
4406 *
4407 * SPP_HB_DEMAND - Request a user initiated heartbeat
4408 * to be made immediately.
4409 *
4410 * SPP_PMTUD_ENABLE - This field will enable PMTU
4411 * discovery upon the specified address. Note that
4412 * if the address feild is empty then all addresses
4413 * on the association are effected.
4414 *
4415 * SPP_PMTUD_DISABLE - This field will disable PMTU
4416 * discovery upon the specified address. Note that
4417 * if the address feild is empty then all addresses
4418 * on the association are effected. Not also that
4419 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4420 * exclusive. Enabling both will have undetermined
4421 * results.
4422 *
4423 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4424 * on delayed sack. The time specified in spp_sackdelay
4425 * is used to specify the sack delay for this address. Note
4426 * that if spp_address is empty then all addresses will
4427 * enable delayed sack and take on the sack delay
4428 * value specified in spp_sackdelay.
4429 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4430 * off delayed sack. If the spp_address field is blank then
4431 * delayed sack is disabled for the entire association. Note
4432 * also that this field is mutually exclusive to
4433 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4434 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 */
4436static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004437 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004439 struct sctp_paddrparams params;
4440 struct sctp_transport *trans = NULL;
4441 struct sctp_association *asoc = NULL;
4442 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Neil Horman408f22e2007-06-16 14:03:45 -04004444 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004446 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 if (copy_from_user(&params, optval, len))
4448 return -EFAULT;
4449
Frank Filz52ccb8e2005-12-22 11:36:46 -08004450 /* If an address other than INADDR_ANY is specified, and
4451 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004453 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004454 trans = sctp_addr_id2transport(sk, &params.spp_address,
4455 params.spp_assoc_id);
4456 if (!trans) {
4457 SCTP_DEBUG_PRINTK("Failed no transport\n");
4458 return -EINVAL;
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 }
4461
Frank Filz52ccb8e2005-12-22 11:36:46 -08004462 /* Get association, if assoc_id != 0 and the socket is a one
4463 * to many style socket, and an association was not found, then
4464 * the id was invalid.
4465 */
4466 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4467 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4468 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Frank Filz52ccb8e2005-12-22 11:36:46 -08004472 if (trans) {
4473 /* Fetch transport values. */
4474 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4475 params.spp_pathmtu = trans->pathmtu;
4476 params.spp_pathmaxrxt = trans->pathmaxrxt;
4477 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Frank Filz52ccb8e2005-12-22 11:36:46 -08004479 /*draft-11 doesn't say what to return in spp_flags*/
4480 params.spp_flags = trans->param_flags;
4481 } else if (asoc) {
4482 /* Fetch association values. */
4483 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4484 params.spp_pathmtu = asoc->pathmtu;
4485 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4486 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Frank Filz52ccb8e2005-12-22 11:36:46 -08004488 /*draft-11 doesn't say what to return in spp_flags*/
4489 params.spp_flags = asoc->param_flags;
4490 } else {
4491 /* Fetch socket values. */
4492 params.spp_hbinterval = sp->hbinterval;
4493 params.spp_pathmtu = sp->pathmtu;
4494 params.spp_sackdelay = sp->sackdelay;
4495 params.spp_pathmaxrxt = sp->pathmaxrxt;
4496
4497 /*draft-11 doesn't say what to return in spp_flags*/
4498 params.spp_flags = sp->param_flags;
4499 }
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 if (copy_to_user(optval, &params, len))
4502 return -EFAULT;
4503
4504 if (put_user(len, optlen))
4505 return -EFAULT;
4506
4507 return 0;
4508}
4509
Wei Yongjund364d922008-05-09 15:13:26 -07004510/*
4511 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08004512 *
Wei Yongjund364d922008-05-09 15:13:26 -07004513 * This option will effect the way delayed acks are performed. This
4514 * option allows you to get or set the delayed ack time, in
4515 * milliseconds. It also allows changing the delayed ack frequency.
4516 * Changing the frequency to 1 disables the delayed sack algorithm. If
4517 * the assoc_id is 0, then this sets or gets the endpoints default
4518 * values. If the assoc_id field is non-zero, then the set or get
4519 * effects the specified association for the one to many model (the
4520 * assoc_id field is ignored by the one to one model). Note that if
4521 * sack_delay or sack_freq are 0 when setting this option, then the
4522 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08004523 *
Wei Yongjund364d922008-05-09 15:13:26 -07004524 * struct sctp_sack_info {
4525 * sctp_assoc_t sack_assoc_id;
4526 * uint32_t sack_delay;
4527 * uint32_t sack_freq;
4528 * };
Frank Filz77086102005-12-22 11:37:30 -08004529 *
Wei Yongjund364d922008-05-09 15:13:26 -07004530 * sack_assoc_id - This parameter, indicates which association the user
4531 * is performing an action upon. Note that if this field's value is
4532 * zero then the endpoints default value is changed (effecting future
4533 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08004534 *
Wei Yongjund364d922008-05-09 15:13:26 -07004535 * sack_delay - This parameter contains the number of milliseconds that
4536 * the user is requesting the delayed ACK timer be set to. Note that
4537 * this value is defined in the standard to be between 200 and 500
4538 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08004539 *
Wei Yongjund364d922008-05-09 15:13:26 -07004540 * sack_freq - This parameter contains the number of packets that must
4541 * be received before a sack is sent without waiting for the delay
4542 * timer to expire. The default value for this is 2, setting this
4543 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08004544 */
Wei Yongjund364d922008-05-09 15:13:26 -07004545static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08004546 char __user *optval,
4547 int __user *optlen)
4548{
Wei Yongjund364d922008-05-09 15:13:26 -07004549 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08004550 struct sctp_association *asoc = NULL;
4551 struct sctp_sock *sp = sctp_sk(sk);
4552
Wei Yongjund364d922008-05-09 15:13:26 -07004553 if (len >= sizeof(struct sctp_sack_info)) {
4554 len = sizeof(struct sctp_sack_info);
4555
4556 if (copy_from_user(&params, optval, len))
4557 return -EFAULT;
4558 } else if (len == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00004559 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4560 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07004561 if (copy_from_user(&params, optval, len))
4562 return -EFAULT;
4563 } else
Frank Filz77086102005-12-22 11:37:30 -08004564 return - EINVAL;
4565
Wei Yongjund364d922008-05-09 15:13:26 -07004566 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08004567 * to many style socket, and an association was not found, then
4568 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004569 */
Wei Yongjund364d922008-05-09 15:13:26 -07004570 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4571 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08004572 return -EINVAL;
4573
4574 if (asoc) {
4575 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004576 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4577 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08004578 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07004579 params.sack_freq = asoc->sackfreq;
4580
4581 } else {
4582 params.sack_delay = 0;
4583 params.sack_freq = 1;
4584 }
Frank Filz77086102005-12-22 11:37:30 -08004585 } else {
4586 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004587 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4588 params.sack_delay = sp->sackdelay;
4589 params.sack_freq = sp->sackfreq;
4590 } else {
4591 params.sack_delay = 0;
4592 params.sack_freq = 1;
4593 }
Frank Filz77086102005-12-22 11:37:30 -08004594 }
4595
4596 if (copy_to_user(optval, &params, len))
4597 return -EFAULT;
4598
4599 if (put_user(len, optlen))
4600 return -EFAULT;
4601
4602 return 0;
4603}
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4606 *
4607 * Applications can specify protocol parameters for the default association
4608 * initialization. The option name argument to setsockopt() and getsockopt()
4609 * is SCTP_INITMSG.
4610 *
4611 * Setting initialization parameters is effective only on an unconnected
4612 * socket (for UDP-style sockets only future associations are effected
4613 * by the change). With TCP-style sockets, this option is inherited by
4614 * sockets derived from a listener socket.
4615 */
4616static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4617{
Neil Horman408f22e2007-06-16 14:03:45 -04004618 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004620 len = sizeof(struct sctp_initmsg);
4621 if (put_user(len, optlen))
4622 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4624 return -EFAULT;
4625 return 0;
4626}
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004629static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4630 char __user *optval, int __user *optlen)
4631{
4632 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004633 int cnt = 0;
4634 struct sctp_getaddrs getaddrs;
4635 struct sctp_transport *from;
4636 void __user *to;
4637 union sctp_addr temp;
4638 struct sctp_sock *sp = sctp_sk(sk);
4639 int addrlen;
4640 size_t space_left;
4641 int bytes_copied;
4642
4643 if (len < sizeof(struct sctp_getaddrs))
4644 return -EINVAL;
4645
4646 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4647 return -EFAULT;
4648
4649 /* For UDP-style sockets, id specifies the association to query. */
4650 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4651 if (!asoc)
4652 return -EINVAL;
4653
4654 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004655 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004656
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07004657 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4658 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08004659 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004660 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
Vlad Yasevicha5f4cea2010-04-30 21:42:42 -04004661 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004662 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004663 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004664 if (copy_to_user(to, &temp, addrlen))
4665 return -EFAULT;
4666 to += addrlen;
4667 cnt++;
4668 space_left -= addrlen;
4669 }
4670
4671 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4672 return -EFAULT;
4673 bytes_copied = ((char __user *)to) - optval;
4674 if (put_user(bytes_copied, optlen))
4675 return -EFAULT;
4676
4677 return 0;
4678}
4679
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004680static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4681 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004682{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004683 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004684 union sctp_addr temp;
4685 int cnt = 0;
4686 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004687 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004688
Vlad Yasevich29303542007-09-16 16:02:12 -07004689 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004690 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07004691 if (!addr->valid)
4692 continue;
4693
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004694 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004695 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004696 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07004697 if ((PF_INET6 == sk->sk_family) &&
4698 inet_v6_ipv6only(sk) &&
4699 (AF_INET == addr->a.sa.sa_family))
4700 continue;
Al Viro6244be42006-11-20 17:21:44 -08004701 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05004702 if (!temp.v4.sin_port)
4703 temp.v4.sin_port = htons(port);
4704
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004705 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4706 &temp);
4707 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004708 if (space_left < addrlen) {
4709 cnt = -ENOMEM;
4710 break;
4711 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004712 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004713
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004714 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004715 cnt ++;
4716 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004717 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004718 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004719 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004720
4721 return cnt;
4722}
4723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004725static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4726 char __user *optval, int __user *optlen)
4727{
4728 struct sctp_bind_addr *bp;
4729 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004730 int cnt = 0;
4731 struct sctp_getaddrs getaddrs;
4732 struct sctp_sockaddr_entry *addr;
4733 void __user *to;
4734 union sctp_addr temp;
4735 struct sctp_sock *sp = sctp_sk(sk);
4736 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004737 int err = 0;
4738 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004739 int bytes_copied = 0;
4740 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004741 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004742
Neil Horman408f22e2007-06-16 14:03:45 -04004743 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004744 return -EINVAL;
4745
4746 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4747 return -EFAULT;
4748
4749 /*
4750 * For UDP-style sockets, id specifies the association to query.
4751 * If the id field is set to the value '0' then the locally bound
4752 * addresses are returned without regard to any particular
4753 * association.
4754 */
4755 if (0 == getaddrs.assoc_id) {
4756 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004757 } else {
4758 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4759 if (!asoc)
4760 return -EINVAL;
4761 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004762 }
4763
4764 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004765 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4766
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004767 addrs = kmalloc(space_left, GFP_KERNEL);
4768 if (!addrs)
4769 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004770
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004771 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4772 * addresses from the global local address list.
4773 */
4774 if (sctp_list_single_entry(&bp->address_list)) {
4775 addr = list_entry(bp->address_list.next,
4776 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004777 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004778 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4779 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004780 if (cnt < 0) {
4781 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004782 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004783 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004784 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004785 }
4786 }
4787
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004788 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004789 /* Protection on the bound address list is not needed since
4790 * in the socket option context we hold a socket lock and
4791 * thus the bound address list can't change.
4792 */
4793 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004794 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004795 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4796 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004797 if (space_left < addrlen) {
4798 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004799 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004800 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004801 memcpy(buf, &temp, addrlen);
4802 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004803 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004804 cnt ++;
4805 space_left -= addrlen;
4806 }
4807
4808copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004809 if (copy_to_user(to, addrs, bytes_copied)) {
4810 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004811 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004812 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004813 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4814 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004815 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004816 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004817 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004818 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004819out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004820 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004821 return err;
4822}
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4825 *
4826 * Requests that the local SCTP stack use the enclosed peer address as
4827 * the association primary. The enclosed address must be one of the
4828 * association peer's addresses.
4829 */
4830static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4831 char __user *optval, int __user *optlen)
4832{
4833 struct sctp_prim prim;
4834 struct sctp_association *asoc;
4835 struct sctp_sock *sp = sctp_sk(sk);
4836
Neil Horman408f22e2007-06-16 14:03:45 -04004837 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 return -EINVAL;
4839
Neil Horman408f22e2007-06-16 14:03:45 -04004840 len = sizeof(struct sctp_prim);
4841
4842 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return -EFAULT;
4844
4845 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4846 if (!asoc)
4847 return -EINVAL;
4848
4849 if (!asoc->peer.primary_path)
4850 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004851
Al Viro8cec6b82006-11-20 17:23:01 -08004852 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4853 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4856 (union sctp_addr *)&prim.ssp_addr);
4857
Neil Horman408f22e2007-06-16 14:03:45 -04004858 if (put_user(len, optlen))
4859 return -EFAULT;
4860 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return -EFAULT;
4862
4863 return 0;
4864}
4865
4866/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004867 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004869 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 * Indication parameter for all future INIT and INIT-ACK exchanges.
4871 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004872static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 char __user *optval, int __user *optlen)
4874{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004875 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Neil Horman408f22e2007-06-16 14:03:45 -04004877 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 return -EINVAL;
4879
Neil Horman408f22e2007-06-16 14:03:45 -04004880 len = sizeof(struct sctp_setadaptation);
4881
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004882 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004883
4884 if (put_user(len, optlen))
4885 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004886 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 return 0;
4890}
4891
4892/*
4893 *
4894 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4895 *
4896 * Applications that wish to use the sendto() system call may wish to
4897 * specify a default set of parameters that would normally be supplied
4898 * through the inclusion of ancillary data. This socket option allows
4899 * such an application to set the default sctp_sndrcvinfo structure.
4900
4901
4902 * The application that wishes to use this socket option simply passes
4903 * in to this call the sctp_sndrcvinfo structure defined in Section
4904 * 5.2.2) The input parameters accepted by this call include
4905 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4906 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4907 * to this call if the caller is using the UDP model.
4908 *
4909 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4910 */
4911static int sctp_getsockopt_default_send_param(struct sock *sk,
4912 int len, char __user *optval,
4913 int __user *optlen)
4914{
4915 struct sctp_sndrcvinfo info;
4916 struct sctp_association *asoc;
4917 struct sctp_sock *sp = sctp_sk(sk);
4918
Neil Horman408f22e2007-06-16 14:03:45 -04004919 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004921
4922 len = sizeof(struct sctp_sndrcvinfo);
4923
4924 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return -EFAULT;
4926
4927 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4928 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4929 return -EINVAL;
4930
4931 if (asoc) {
4932 info.sinfo_stream = asoc->default_stream;
4933 info.sinfo_flags = asoc->default_flags;
4934 info.sinfo_ppid = asoc->default_ppid;
4935 info.sinfo_context = asoc->default_context;
4936 info.sinfo_timetolive = asoc->default_timetolive;
4937 } else {
4938 info.sinfo_stream = sp->default_stream;
4939 info.sinfo_flags = sp->default_flags;
4940 info.sinfo_ppid = sp->default_ppid;
4941 info.sinfo_context = sp->default_context;
4942 info.sinfo_timetolive = sp->default_timetolive;
4943 }
4944
Neil Horman408f22e2007-06-16 14:03:45 -04004945 if (put_user(len, optlen))
4946 return -EFAULT;
4947 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return -EFAULT;
4949
4950 return 0;
4951}
4952
4953/*
4954 *
4955 * 7.1.5 SCTP_NODELAY
4956 *
4957 * Turn on/off any Nagle-like algorithm. This means that packets are
4958 * generally sent as soon as possible and no unnecessary delays are
4959 * introduced, at the cost of more packets in the network. Expects an
4960 * integer boolean flag.
4961 */
4962
4963static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4964 char __user *optval, int __user *optlen)
4965{
4966 int val;
4967
4968 if (len < sizeof(int))
4969 return -EINVAL;
4970
4971 len = sizeof(int);
4972 val = (sctp_sk(sk)->nodelay == 1);
4973 if (put_user(len, optlen))
4974 return -EFAULT;
4975 if (copy_to_user(optval, &val, len))
4976 return -EFAULT;
4977 return 0;
4978}
4979
4980/*
4981 *
4982 * 7.1.1 SCTP_RTOINFO
4983 *
4984 * The protocol parameters used to initialize and bound retransmission
4985 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4986 * and modify these parameters.
4987 * All parameters are time values, in milliseconds. A value of 0, when
4988 * modifying the parameters, indicates that the current value should not
4989 * be changed.
4990 *
4991 */
4992static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4993 char __user *optval,
4994 int __user *optlen) {
4995 struct sctp_rtoinfo rtoinfo;
4996 struct sctp_association *asoc;
4997
Neil Horman408f22e2007-06-16 14:03:45 -04004998 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 return -EINVAL;
5000
Neil Horman408f22e2007-06-16 14:03:45 -04005001 len = sizeof(struct sctp_rtoinfo);
5002
5003 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return -EFAULT;
5005
5006 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5007
5008 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5009 return -EINVAL;
5010
5011 /* Values corresponding to the specific association. */
5012 if (asoc) {
5013 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5014 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5015 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5016 } else {
5017 /* Values corresponding to the endpoint. */
5018 struct sctp_sock *sp = sctp_sk(sk);
5019
5020 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5021 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5022 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5023 }
5024
5025 if (put_user(len, optlen))
5026 return -EFAULT;
5027
5028 if (copy_to_user(optval, &rtoinfo, len))
5029 return -EFAULT;
5030
5031 return 0;
5032}
5033
5034/*
5035 *
5036 * 7.1.2 SCTP_ASSOCINFO
5037 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005038 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 * of the association.
5040 * Returns an error if the new association retransmission value is
5041 * greater than the sum of the retransmission value of the peer.
5042 * See [SCTP] for more information.
5043 *
5044 */
5045static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5046 char __user *optval,
5047 int __user *optlen)
5048{
5049
5050 struct sctp_assocparams assocparams;
5051 struct sctp_association *asoc;
5052 struct list_head *pos;
5053 int cnt = 0;
5054
Neil Horman408f22e2007-06-16 14:03:45 -04005055 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 return -EINVAL;
5057
Neil Horman408f22e2007-06-16 14:03:45 -04005058 len = sizeof(struct sctp_assocparams);
5059
5060 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return -EFAULT;
5062
5063 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5064
5065 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5066 return -EINVAL;
5067
5068 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005069 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5071 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5072 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5073 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5074 * 1000) +
5075 (asoc->cookie_life.tv_usec
5076 / 1000);
5077
5078 list_for_each(pos, &asoc->peer.transport_addr_list) {
5079 cnt ++;
5080 }
5081
5082 assocparams.sasoc_number_peer_destinations = cnt;
5083 } else {
5084 /* Values corresponding to the endpoint */
5085 struct sctp_sock *sp = sctp_sk(sk);
5086
5087 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5088 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5089 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5090 assocparams.sasoc_cookie_life =
5091 sp->assocparams.sasoc_cookie_life;
5092 assocparams.sasoc_number_peer_destinations =
5093 sp->assocparams.
5094 sasoc_number_peer_destinations;
5095 }
5096
5097 if (put_user(len, optlen))
5098 return -EFAULT;
5099
5100 if (copy_to_user(optval, &assocparams, len))
5101 return -EFAULT;
5102
5103 return 0;
5104}
5105
5106/*
5107 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5108 *
5109 * This socket option is a boolean flag which turns on or off mapped V4
5110 * addresses. If this option is turned on and the socket is type
5111 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5112 * If this option is turned off, then no mapping will be done of V4
5113 * addresses and a user will receive both PF_INET6 and PF_INET type
5114 * addresses on the socket.
5115 */
5116static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5117 char __user *optval, int __user *optlen)
5118{
5119 int val;
5120 struct sctp_sock *sp = sctp_sk(sk);
5121
5122 if (len < sizeof(int))
5123 return -EINVAL;
5124
5125 len = sizeof(int);
5126 val = sp->v4mapped;
5127 if (put_user(len, optlen))
5128 return -EFAULT;
5129 if (copy_to_user(optval, &val, len))
5130 return -EFAULT;
5131
5132 return 0;
5133}
5134
5135/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005136 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5137 * (chapter and verse is quoted at sctp_setsockopt_context())
5138 */
5139static int sctp_getsockopt_context(struct sock *sk, int len,
5140 char __user *optval, int __user *optlen)
5141{
5142 struct sctp_assoc_value params;
5143 struct sctp_sock *sp;
5144 struct sctp_association *asoc;
5145
Neil Horman408f22e2007-06-16 14:03:45 -04005146 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005147 return -EINVAL;
5148
Neil Horman408f22e2007-06-16 14:03:45 -04005149 len = sizeof(struct sctp_assoc_value);
5150
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005151 if (copy_from_user(&params, optval, len))
5152 return -EFAULT;
5153
5154 sp = sctp_sk(sk);
5155
5156 if (params.assoc_id != 0) {
5157 asoc = sctp_id2assoc(sk, params.assoc_id);
5158 if (!asoc)
5159 return -EINVAL;
5160 params.assoc_value = asoc->default_rcv_context;
5161 } else {
5162 params.assoc_value = sp->default_rcv_context;
5163 }
5164
5165 if (put_user(len, optlen))
5166 return -EFAULT;
5167 if (copy_to_user(optval, &params, len))
5168 return -EFAULT;
5169
5170 return 0;
5171}
5172
5173/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005174 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5175 * This option will get or set the maximum size to put in any outgoing
5176 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 * fragmented by SCTP into the specified size. Note that the underlying
5178 * SCTP implementation may fragment into smaller sized chunks when the
5179 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005180 * the user. The default value for this option is '0' which indicates
5181 * the user is NOT limiting fragmentation and only the PMTU will effect
5182 * SCTP's choice of DATA chunk size. Note also that values set larger
5183 * than the maximum size of an IP datagram will effectively let SCTP
5184 * control fragmentation (i.e. the same as setting this option to 0).
5185 *
5186 * The following structure is used to access and modify this parameter:
5187 *
5188 * struct sctp_assoc_value {
5189 * sctp_assoc_t assoc_id;
5190 * uint32_t assoc_value;
5191 * };
5192 *
5193 * assoc_id: This parameter is ignored for one-to-one style sockets.
5194 * For one-to-many style sockets this parameter indicates which
5195 * association the user is performing an action upon. Note that if
5196 * this field's value is zero then the endpoints default value is
5197 * changed (effecting future associations only).
5198 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 */
5200static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5201 char __user *optval, int __user *optlen)
5202{
Wei Yongjune89c2092008-12-25 16:54:58 -08005203 struct sctp_assoc_value params;
5204 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Wei Yongjune89c2092008-12-25 16:54:58 -08005206 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005207 pr_warn("Use of int in maxseg socket option deprecated\n");
5208 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08005209 params.assoc_id = 0;
5210 } else if (len >= sizeof(struct sctp_assoc_value)) {
5211 len = sizeof(struct sctp_assoc_value);
5212 if (copy_from_user(&params, optval, sizeof(params)))
5213 return -EFAULT;
5214 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return -EINVAL;
5216
Wei Yongjune89c2092008-12-25 16:54:58 -08005217 asoc = sctp_id2assoc(sk, params.assoc_id);
5218 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Wei Yongjune89c2092008-12-25 16:54:58 -08005221 if (asoc)
5222 params.assoc_value = asoc->frag_point;
5223 else
5224 params.assoc_value = sctp_sk(sk)->user_frag;
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (put_user(len, optlen))
5227 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005228 if (len == sizeof(int)) {
5229 if (copy_to_user(optval, &params.assoc_value, len))
5230 return -EFAULT;
5231 } else {
5232 if (copy_to_user(optval, &params, len))
5233 return -EFAULT;
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236 return 0;
5237}
5238
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005239/*
5240 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5241 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5242 */
5243static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5244 char __user *optval, int __user *optlen)
5245{
5246 int val;
5247
5248 if (len < sizeof(int))
5249 return -EINVAL;
5250
5251 len = sizeof(int);
5252
5253 val = sctp_sk(sk)->frag_interleave;
5254 if (put_user(len, optlen))
5255 return -EFAULT;
5256 if (copy_to_user(optval, &val, len))
5257 return -EFAULT;
5258
5259 return 0;
5260}
5261
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005262/*
5263 * 7.1.25. Set or Get the sctp partial delivery point
5264 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5265 */
5266static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5267 char __user *optval,
5268 int __user *optlen)
5269{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005270 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005271
5272 if (len < sizeof(u32))
5273 return -EINVAL;
5274
5275 len = sizeof(u32);
5276
5277 val = sctp_sk(sk)->pd_point;
5278 if (put_user(len, optlen))
5279 return -EFAULT;
5280 if (copy_to_user(optval, &val, len))
5281 return -EFAULT;
5282
Wei Yongjun7d743b72010-12-14 16:10:41 +00005283 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005284}
5285
Vlad Yasevich70331572007-03-23 11:34:36 -07005286/*
5287 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5288 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5289 */
5290static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5291 char __user *optval,
5292 int __user *optlen)
5293{
Neil Horman219b99a2008-03-05 13:44:46 -08005294 struct sctp_assoc_value params;
5295 struct sctp_sock *sp;
5296 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005297
Neil Horman219b99a2008-03-05 13:44:46 -08005298 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005299 pr_warn("Use of int in max_burst socket option deprecated\n");
5300 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08005301 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005302 } else if (len >= sizeof(struct sctp_assoc_value)) {
5303 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005304 if (copy_from_user(&params, optval, len))
5305 return -EFAULT;
5306 } else
5307 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005308
Neil Horman219b99a2008-03-05 13:44:46 -08005309 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005310
Neil Horman219b99a2008-03-05 13:44:46 -08005311 if (params.assoc_id != 0) {
5312 asoc = sctp_id2assoc(sk, params.assoc_id);
5313 if (!asoc)
5314 return -EINVAL;
5315 params.assoc_value = asoc->max_burst;
5316 } else
5317 params.assoc_value = sp->max_burst;
5318
5319 if (len == sizeof(int)) {
5320 if (copy_to_user(optval, &params.assoc_value, len))
5321 return -EFAULT;
5322 } else {
5323 if (copy_to_user(optval, &params, len))
5324 return -EFAULT;
5325 }
5326
5327 return 0;
5328
Vlad Yasevich70331572007-03-23 11:34:36 -07005329}
5330
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005331static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5332 char __user *optval, int __user *optlen)
5333{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005334 struct net *net = sock_net(sk);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005335 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005336 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005337 __u16 data_len = 0;
5338 u32 num_idents;
5339
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005340 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005341 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005342
5343 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005344 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005345
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005346 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005347 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005348
5349 len = sizeof(struct sctp_hmacalgo) + data_len;
5350 num_idents = data_len / sizeof(u16);
5351
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005352 if (put_user(len, optlen))
5353 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005354 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005355 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005356 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5357 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005358 return 0;
5359}
5360
5361static int sctp_getsockopt_active_key(struct sock *sk, int len,
5362 char __user *optval, int __user *optlen)
5363{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005364 struct net *net = sock_net(sk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005365 struct sctp_authkeyid val;
5366 struct sctp_association *asoc;
5367
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005368 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005369 return -EACCES;
5370
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005371 if (len < sizeof(struct sctp_authkeyid))
5372 return -EINVAL;
5373 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5374 return -EFAULT;
5375
5376 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5377 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5378 return -EINVAL;
5379
5380 if (asoc)
5381 val.scact_keynumber = asoc->active_key_id;
5382 else
5383 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5384
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005385 len = sizeof(struct sctp_authkeyid);
5386 if (put_user(len, optlen))
5387 return -EFAULT;
5388 if (copy_to_user(optval, &val, len))
5389 return -EFAULT;
5390
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005391 return 0;
5392}
5393
5394static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5395 char __user *optval, int __user *optlen)
5396{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005397 struct net *net = sock_net(sk);
Al Viro411223c2007-10-14 19:21:20 +01005398 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005399 struct sctp_authchunks val;
5400 struct sctp_association *asoc;
5401 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005402 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005403 char __user *to;
5404
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005405 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005406 return -EACCES;
5407
5408 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005409 return -EINVAL;
5410
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005411 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005412 return -EFAULT;
5413
Al Viro411223c2007-10-14 19:21:20 +01005414 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005415 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5416 if (!asoc)
5417 return -EINVAL;
5418
5419 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005420 if (!ch)
5421 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005422
5423 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05005424 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5425 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005426 return -EINVAL;
5427
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005428 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005429 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005430num:
5431 len = sizeof(struct sctp_authchunks) + num_chunks;
5432 if (put_user(len, optlen)) return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005433 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5434 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005435 return 0;
5436}
5437
5438static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5439 char __user *optval, int __user *optlen)
5440{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005441 struct net *net = sock_net(sk);
Al Viro411223c2007-10-14 19:21:20 +01005442 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005443 struct sctp_authchunks val;
5444 struct sctp_association *asoc;
5445 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005446 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005447 char __user *to;
5448
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005449 if (!net->sctp.auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005450 return -EACCES;
5451
5452 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005453 return -EINVAL;
5454
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005455 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005456 return -EFAULT;
5457
Al Viro411223c2007-10-14 19:21:20 +01005458 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005459 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5460 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5461 return -EINVAL;
5462
5463 if (asoc)
5464 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5465 else
5466 ch = sctp_sk(sk)->ep->auth_chunk_list;
5467
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005468 if (!ch)
5469 goto num;
5470
Vlad Yasevichb40db682008-02-27 14:40:37 -05005471 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005472 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005473 return -EINVAL;
5474
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005475 if (copy_to_user(to, ch->chunks, num_chunks))
5476 return -EFAULT;
5477num:
5478 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005479 if (put_user(len, optlen))
5480 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005481 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5482 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005483
5484 return 0;
5485}
5486
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005487/*
5488 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5489 * This option gets the current number of associations that are attached
5490 * to a one-to-many style socket. The option value is an uint32_t.
5491 */
5492static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5493 char __user *optval, int __user *optlen)
5494{
5495 struct sctp_sock *sp = sctp_sk(sk);
5496 struct sctp_association *asoc;
5497 u32 val = 0;
5498
5499 if (sctp_style(sk, TCP))
5500 return -EOPNOTSUPP;
5501
5502 if (len < sizeof(u32))
5503 return -EINVAL;
5504
5505 len = sizeof(u32);
5506
5507 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5508 val++;
5509 }
5510
5511 if (put_user(len, optlen))
5512 return -EFAULT;
5513 if (copy_to_user(optval, &val, len))
5514 return -EFAULT;
5515
5516 return 0;
5517}
5518
Wei Yongjun209ba422011-04-17 17:27:08 +00005519/*
Michio Honda7dc04d72011-04-26 20:16:31 +09005520 * 8.1.23 SCTP_AUTO_ASCONF
5521 * See the corresponding setsockopt entry as description
5522 */
5523static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
5524 char __user *optval, int __user *optlen)
5525{
5526 int val = 0;
5527
5528 if (len < sizeof(int))
5529 return -EINVAL;
5530
5531 len = sizeof(int);
5532 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
5533 val = 1;
5534 if (put_user(len, optlen))
5535 return -EFAULT;
5536 if (copy_to_user(optval, &val, len))
5537 return -EFAULT;
5538 return 0;
5539}
5540
5541/*
Wei Yongjun209ba422011-04-17 17:27:08 +00005542 * 8.2.6. Get the Current Identifiers of Associations
5543 * (SCTP_GET_ASSOC_ID_LIST)
5544 *
5545 * This option gets the current list of SCTP association identifiers of
5546 * the SCTP associations handled by a one-to-many style socket.
5547 */
5548static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
5549 char __user *optval, int __user *optlen)
5550{
5551 struct sctp_sock *sp = sctp_sk(sk);
5552 struct sctp_association *asoc;
5553 struct sctp_assoc_ids *ids;
5554 u32 num = 0;
5555
5556 if (sctp_style(sk, TCP))
5557 return -EOPNOTSUPP;
5558
5559 if (len < sizeof(struct sctp_assoc_ids))
5560 return -EINVAL;
5561
5562 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5563 num++;
5564 }
5565
5566 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
5567 return -EINVAL;
5568
5569 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
5570
5571 ids = kmalloc(len, GFP_KERNEL);
5572 if (unlikely(!ids))
5573 return -ENOMEM;
5574
5575 ids->gaids_number_of_ids = num;
5576 num = 0;
5577 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5578 ids->gaids_assoc_id[num++] = asoc->assoc_id;
5579 }
5580
5581 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
5582 kfree(ids);
5583 return -EFAULT;
5584 }
5585
5586 kfree(ids);
5587 return 0;
5588}
5589
Neil Horman5aa93bc2012-07-21 07:56:07 +00005590/*
5591 * SCTP_PEER_ADDR_THLDS
5592 *
5593 * This option allows us to fetch the partially failed threshold for one or all
5594 * transports in an association. See Section 6.1 of:
5595 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
5596 */
5597static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
5598 char __user *optval,
5599 int len,
5600 int __user *optlen)
5601{
5602 struct sctp_paddrthlds val;
5603 struct sctp_transport *trans;
5604 struct sctp_association *asoc;
5605
5606 if (len < sizeof(struct sctp_paddrthlds))
5607 return -EINVAL;
5608 len = sizeof(struct sctp_paddrthlds);
5609 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
5610 return -EFAULT;
5611
5612 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
5613 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
5614 if (!asoc)
5615 return -ENOENT;
5616
5617 val.spt_pathpfthld = asoc->pf_retrans;
5618 val.spt_pathmaxrxt = asoc->pathmaxrxt;
5619 } else {
5620 trans = sctp_addr_id2transport(sk, &val.spt_address,
5621 val.spt_assoc_id);
5622 if (!trans)
5623 return -ENOENT;
5624
5625 val.spt_pathmaxrxt = trans->pathmaxrxt;
5626 val.spt_pathpfthld = trans->pf_retrans;
5627 }
5628
5629 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
5630 return -EFAULT;
5631
5632 return 0;
5633}
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5636 char __user *optval, int __user *optlen)
5637{
5638 int retval = 0;
5639 int len;
5640
Frank Filz3f7a87d2005-06-20 13:14:57 -07005641 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5642 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 /* I can hardly begin to describe how wrong this is. This is
5645 * so broken as to be worse than useless. The API draft
5646 * REALLY is NOT helpful here... I am not convinced that the
5647 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5648 * are at all well-founded.
5649 */
5650 if (level != SOL_SCTP) {
5651 struct sctp_af *af = sctp_sk(sk)->pf->af;
5652
5653 retval = af->getsockopt(sk, level, optname, optval, optlen);
5654 return retval;
5655 }
5656
5657 if (get_user(len, optlen))
5658 return -EFAULT;
5659
5660 sctp_lock_sock(sk);
5661
5662 switch (optname) {
5663 case SCTP_STATUS:
5664 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5665 break;
5666 case SCTP_DISABLE_FRAGMENTS:
5667 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5668 optlen);
5669 break;
5670 case SCTP_EVENTS:
5671 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5672 break;
5673 case SCTP_AUTOCLOSE:
5674 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5675 break;
5676 case SCTP_SOCKOPT_PEELOFF:
5677 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5678 break;
5679 case SCTP_PEER_ADDR_PARAMS:
5680 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5681 optlen);
5682 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00005683 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07005684 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08005685 optlen);
5686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 case SCTP_INITMSG:
5688 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 case SCTP_GET_PEER_ADDRS:
5691 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5692 optlen);
5693 break;
5694 case SCTP_GET_LOCAL_ADDRS:
5695 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5696 optlen);
5697 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04005698 case SCTP_SOCKOPT_CONNECTX3:
5699 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 case SCTP_DEFAULT_SEND_PARAM:
5702 retval = sctp_getsockopt_default_send_param(sk, len,
5703 optval, optlen);
5704 break;
5705 case SCTP_PRIMARY_ADDR:
5706 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5707 break;
5708 case SCTP_NODELAY:
5709 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5710 break;
5711 case SCTP_RTOINFO:
5712 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5713 break;
5714 case SCTP_ASSOCINFO:
5715 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5716 break;
5717 case SCTP_I_WANT_MAPPED_V4_ADDR:
5718 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5719 break;
5720 case SCTP_MAXSEG:
5721 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5722 break;
5723 case SCTP_GET_PEER_ADDR_INFO:
5724 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5725 optlen);
5726 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005727 case SCTP_ADAPTATION_LAYER:
5728 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 optlen);
5730 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005731 case SCTP_CONTEXT:
5732 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5733 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005734 case SCTP_FRAGMENT_INTERLEAVE:
5735 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5736 optlen);
5737 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005738 case SCTP_PARTIAL_DELIVERY_POINT:
5739 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5740 optlen);
5741 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005742 case SCTP_MAX_BURST:
5743 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5744 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005745 case SCTP_AUTH_KEY:
5746 case SCTP_AUTH_CHUNK:
5747 case SCTP_AUTH_DELETE_KEY:
5748 retval = -EOPNOTSUPP;
5749 break;
5750 case SCTP_HMAC_IDENT:
5751 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5752 break;
5753 case SCTP_AUTH_ACTIVE_KEY:
5754 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5755 break;
5756 case SCTP_PEER_AUTH_CHUNKS:
5757 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5758 optlen);
5759 break;
5760 case SCTP_LOCAL_AUTH_CHUNKS:
5761 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5762 optlen);
5763 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005764 case SCTP_GET_ASSOC_NUMBER:
5765 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5766 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00005767 case SCTP_GET_ASSOC_ID_LIST:
5768 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5769 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09005770 case SCTP_AUTO_ASCONF:
5771 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5772 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00005773 case SCTP_PEER_ADDR_THLDS:
5774 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
5775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 default:
5777 retval = -ENOPROTOOPT;
5778 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
5781 sctp_release_sock(sk);
5782 return retval;
5783}
5784
5785static void sctp_hash(struct sock *sk)
5786{
5787 /* STUB */
5788}
5789
5790static void sctp_unhash(struct sock *sk)
5791{
5792 /* STUB */
5793}
5794
5795/* Check if port is acceptable. Possibly find first available port.
5796 *
5797 * The port hash table (contained in the 'global' SCTP protocol storage
5798 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5799 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5800 * list (the list number is the port number hashed out, so as you
5801 * would expect from a hash function, all the ports in a given list have
5802 * such a number that hashes out to the same list number; you were
5803 * expecting that, right?); so each list has a set of ports, with a
5804 * link to the socket (struct sock) that uses it, the port number and
5805 * a fastreuse flag (FIXME: NPI ipg).
5806 */
5807static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005808 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
5810static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5811{
5812 struct sctp_bind_hashbucket *head; /* hash list */
5813 struct sctp_bind_bucket *pp; /* hash list port iterator */
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005814 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 unsigned short snum;
5816 int ret;
5817
Al Viro04afd8b2006-11-20 17:02:01 -08005818 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5821 sctp_local_bh_disable();
5822
5823 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07005824 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07005825 int low, high, remaining, index;
5826 unsigned int rover;
5827
5828 inet_get_local_port_range(&low, &high);
5829 remaining = (high - low) + 1;
5830 rover = net_random() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 do {
5833 rover++;
5834 if ((rover < low) || (rover > high))
5835 rover = low;
Amerigo Wange3826f12010-05-05 00:27:06 +00005836 if (inet_is_reserved_local_port(rover))
5837 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005838 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 head = &sctp_port_hashtable[index];
5840 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005841 sctp_for_each_hentry(pp, node, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005842 if ((pp->port == rover) &&
5843 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 goto next;
5845 break;
5846 next:
5847 sctp_spin_unlock(&head->lock);
5848 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850 /* Exhausted local port range during search? */
5851 ret = 1;
5852 if (remaining <= 0)
5853 goto fail;
5854
5855 /* OK, here is the one we will use. HEAD (the port
5856 * hash table list entry) is non-NULL and we hold it's
5857 * mutex.
5858 */
5859 snum = rover;
5860 } else {
5861 /* We are given an specific port number; we verify
5862 * that it is not being used. If it is used, we will
5863 * exahust the search in the hash list corresponding
5864 * to the port number (snum) - we detect that with the
5865 * port iterator, pp being NULL.
5866 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005867 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005869 sctp_for_each_hentry(pp, node, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005870 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 goto pp_found;
5872 }
5873 }
5874 pp = NULL;
5875 goto pp_not_found;
5876pp_found:
5877 if (!hlist_empty(&pp->owner)) {
5878 /* We had a port hash table hit - there is an
5879 * available port (pp != NULL) and it is being
5880 * used by other socket (pp->owner not empty); that other
5881 * socket is going to be sk2.
5882 */
5883 int reuse = sk->sk_reuse;
5884 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
5886 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005887 if (pp->fastreuse && sk->sk_reuse &&
5888 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 goto success;
5890
5891 /* Run through the list of sockets bound to the port
5892 * (pp->port) [via the pointers bind_next and
5893 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5894 * we get the endpoint they describe and run through
5895 * the endpoint's list of IP (v4 or v6) addresses,
5896 * comparing each of the addresses with the address of
5897 * the socket sk. If we find a match, then that means
5898 * that this port/socket (sk) combination are already
5899 * in an endpoint.
5900 */
5901 sk_for_each_bound(sk2, node, &pp->owner) {
5902 struct sctp_endpoint *ep2;
5903 ep2 = sctp_sk(sk2)->ep;
5904
Vlad Yasevich4e540642008-07-18 23:06:32 -07005905 if (sk == sk2 ||
5906 (reuse && sk2->sk_reuse &&
5907 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 continue;
5909
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005910 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5911 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 ret = (long)sk2;
5913 goto fail_unlock;
5914 }
5915 }
5916 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5917 }
5918pp_not_found:
5919 /* If there was a hash table miss, create a new port. */
5920 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005921 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 goto fail_unlock;
5923
5924 /* In either case (hit or miss), make sure fastreuse is 1 only
5925 * if sk->sk_reuse is too (that is, if the caller requested
5926 * SO_REUSEADDR on this socket -sk-).
5927 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005928 if (hlist_empty(&pp->owner)) {
5929 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5930 pp->fastreuse = 1;
5931 else
5932 pp->fastreuse = 0;
5933 } else if (pp->fastreuse &&
5934 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 pp->fastreuse = 0;
5936
5937 /* We are set, so fill up all the data in the hash table
5938 * entry, tie the socket list information with the rest of the
5939 * sockets FIXME: Blurry, NPI (ipg).
5940 */
5941success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005943 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 sk_add_bind_node(sk, &pp->owner);
5945 sctp_sk(sk)->bind_hash = pp;
5946 }
5947 ret = 0;
5948
5949fail_unlock:
5950 sctp_spin_unlock(&head->lock);
5951
5952fail:
5953 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 return ret;
5955}
5956
5957/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5958 * port is requested.
5959 */
5960static int sctp_get_port(struct sock *sk, unsigned short snum)
5961{
5962 long ret;
5963 union sctp_addr addr;
5964 struct sctp_af *af = sctp_sk(sk)->pf->af;
5965
5966 /* Set up a dummy address struct from the sk. */
5967 af->from_sk(&addr, sk);
5968 addr.v4.sin_port = htons(snum);
5969
5970 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5971 ret = sctp_get_port_local(sk, &addr);
5972
Eric Dumazeta02cec22010-09-22 20:43:57 +00005973 return ret ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974}
5975
5976/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 * Move a socket to LISTENING state.
5978 */
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005979SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005981 struct sctp_sock *sp = sctp_sk(sk);
5982 struct sctp_endpoint *ep = sp->ep;
Herbert Xu1b489e12006-08-20 15:07:14 +10005983 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
5985 /* Allocate HMAC for generating cookie. */
Vlad Yasevich23b29ed2008-07-18 23:06:07 -07005986 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005987 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005988 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00005989 net_info_ratelimited("failed to load transform for %s: %ld\n",
5990 sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005991 return -ENOSYS;
5992 }
5993 sctp_sk(sk)->hmac = tfm;
5994 }
5995
5996 /*
5997 * If a bind() or sctp_bindx() is not called prior to a listen()
5998 * call that allows new associations to be accepted, the system
5999 * picks an ephemeral port and will choose an address set equivalent
6000 * to binding with a wildcard address.
6001 *
6002 * This is not currently spelled out in the SCTP sockets
6003 * extensions draft, but follows the practice as seen in TCP
6004 * sockets.
6005 *
6006 */
6007 sk->sk_state = SCTP_SS_LISTENING;
6008 if (!ep->base.bind_addr.port) {
6009 if (sctp_autobind(sk))
6010 return -EAGAIN;
6011 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006012 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006013 sk->sk_state = SCTP_SS_CLOSED;
6014 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016 }
6017
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006018 sk->sk_max_ack_backlog = backlog;
6019 sctp_hash_endpoint(ep);
6020 return 0;
6021}
6022
6023/*
6024 * 4.1.3 / 5.1.3 listen()
6025 *
6026 * By default, new associations are not accepted for UDP style sockets.
6027 * An application uses listen() to mark a socket as being able to
6028 * accept new associations.
6029 *
6030 * On TCP style sockets, applications use listen() to ready the SCTP
6031 * endpoint for accepting inbound associations.
6032 *
6033 * On both types of endpoints a backlog of '0' disables listening.
6034 *
6035 * Move a socket to LISTENING state.
6036 */
6037int sctp_inet_listen(struct socket *sock, int backlog)
6038{
6039 struct sock *sk = sock->sk;
6040 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6041 int err = -EINVAL;
6042
6043 if (unlikely(backlog < 0))
6044 return err;
6045
6046 sctp_lock_sock(sk);
6047
6048 /* Peeled-off sockets are not allowed to listen(). */
6049 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6050 goto out;
6051
6052 if (sock->state != SS_UNCONNECTED)
6053 goto out;
6054
6055 /* If backlog is zero, disable listening. */
6056 if (!backlog) {
6057 if (sctp_sstate(sk, CLOSED))
6058 goto out;
6059
6060 err = 0;
6061 sctp_unhash_endpoint(ep);
6062 sk->sk_state = SCTP_SS_CLOSED;
6063 if (sk->sk_reuse)
6064 sctp_sk(sk)->bind_hash->fastreuse = 1;
6065 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006066 }
6067
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006068 /* If we are already listening, just update the backlog */
6069 if (sctp_sstate(sk, LISTENING))
6070 sk->sk_max_ack_backlog = backlog;
6071 else {
6072 err = sctp_listen_start(sk, backlog);
6073 if (err)
6074 goto out;
6075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006077 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078out:
6079 sctp_release_sock(sk);
6080 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081}
6082
6083/*
6084 * This function is done by modeling the current datagram_poll() and the
6085 * tcp_poll(). Note that, based on these implementations, we don't
6086 * lock the socket in this function, even though it seems that,
6087 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08006088 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 * in this place. We assume that we don't need locks either until proven
6090 * otherwise.
6091 *
6092 * Another thing to note is that we include the Async I/O support
6093 * here, again, by modeling the current TCP/UDP code. We don't have
6094 * a good way to test with it yet.
6095 */
6096unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6097{
6098 struct sock *sk = sock->sk;
6099 struct sctp_sock *sp = sctp_sk(sk);
6100 unsigned int mask;
6101
Eric Dumazetaa395142010-04-20 13:03:51 +00006102 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
6104 /* A TCP-style listening socket becomes readable when the accept queue
6105 * is not empty.
6106 */
6107 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6108 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006109 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 mask = 0;
6112
6113 /* Is there any exceptional events? */
6114 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
6115 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08006116 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006117 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 if (sk->sk_shutdown == SHUTDOWN_MASK)
6119 mask |= POLLHUP;
6120
6121 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006122 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 mask |= POLLIN | POLLRDNORM;
6124
6125 /* The association is either gone or not ready. */
6126 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6127 return mask;
6128
6129 /* Is it writable? */
6130 if (sctp_writeable(sk)) {
6131 mask |= POLLOUT | POLLWRNORM;
6132 } else {
6133 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
6134 /*
6135 * Since the socket is not locked, the buffer
6136 * might be made available after the writeable check and
6137 * before the bit is set. This could cause a lost I/O
6138 * signal. tcp_poll() has a race breaker for this race
6139 * condition. Based on their implementation, we put
6140 * in the following code to cover it as well.
6141 */
6142 if (sctp_writeable(sk))
6143 mask |= POLLOUT | POLLWRNORM;
6144 }
6145 return mask;
6146}
6147
6148/********************************************************************
6149 * 2nd Level Abstractions
6150 ********************************************************************/
6151
6152static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006153 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154{
6155 struct sctp_bind_bucket *pp;
6156
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08006157 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07006159 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 pp->port = snum;
6161 pp->fastreuse = 0;
6162 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006163 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006164 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 }
6166 return pp;
6167}
6168
6169/* Caller must hold hashbucket lock for this tb with local BH disabled */
6170static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
6171{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07006172 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006173 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 kmem_cache_free(sctp_bucket_cachep, pp);
6175 SCTP_DBG_OBJCNT_DEC(bind_bucket);
6176 }
6177}
6178
6179/* Release this socket's reference to a local port. */
6180static inline void __sctp_put_port(struct sock *sk)
6181{
6182 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006183 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
6184 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 struct sctp_bind_bucket *pp;
6186
6187 sctp_spin_lock(&head->lock);
6188 pp = sctp_sk(sk)->bind_hash;
6189 __sk_del_bind_node(sk);
6190 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006191 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 sctp_bucket_destroy(pp);
6193 sctp_spin_unlock(&head->lock);
6194}
6195
6196void sctp_put_port(struct sock *sk)
6197{
6198 sctp_local_bh_disable();
6199 __sctp_put_port(sk);
6200 sctp_local_bh_enable();
6201}
6202
6203/*
6204 * The system picks an ephemeral port and choose an address set equivalent
6205 * to binding with a wildcard address.
6206 * One of those addresses will be the primary address for the association.
6207 * This automatically enables the multihoming capability of SCTP.
6208 */
6209static int sctp_autobind(struct sock *sk)
6210{
6211 union sctp_addr autoaddr;
6212 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08006213 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 /* Initialize a local sockaddr structure to INADDR_ANY. */
6216 af = sctp_sk(sk)->pf->af;
6217
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006218 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 af->inaddr_any(&autoaddr, port);
6220
6221 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
6222}
6223
6224/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
6225 *
6226 * From RFC 2292
6227 * 4.2 The cmsghdr Structure *
6228 *
6229 * When ancillary data is sent or received, any number of ancillary data
6230 * objects can be specified by the msg_control and msg_controllen members of
6231 * the msghdr structure, because each object is preceded by
6232 * a cmsghdr structure defining the object's length (the cmsg_len member).
6233 * Historically Berkeley-derived implementations have passed only one object
6234 * at a time, but this API allows multiple objects to be
6235 * passed in a single call to sendmsg() or recvmsg(). The following example
6236 * shows two ancillary data objects in a control buffer.
6237 *
6238 * |<--------------------------- msg_controllen -------------------------->|
6239 * | |
6240 *
6241 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
6242 *
6243 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
6244 * | | |
6245 *
6246 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
6247 *
6248 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
6249 * | | | | |
6250 *
6251 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6252 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
6253 *
6254 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
6255 *
6256 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6257 * ^
6258 * |
6259 *
6260 * msg_control
6261 * points here
6262 */
6263SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
6264 sctp_cmsgs_t *cmsgs)
6265{
6266 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006267 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
6269 for (cmsg = CMSG_FIRSTHDR(msg);
6270 cmsg != NULL;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006271 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
6272 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 return -EINVAL;
6274
6275 /* Should we parse this header or ignore? */
6276 if (cmsg->cmsg_level != IPPROTO_SCTP)
6277 continue;
6278
6279 /* Strictly check lengths following example in SCM code. */
6280 switch (cmsg->cmsg_type) {
6281 case SCTP_INIT:
6282 /* SCTP Socket API Extension
6283 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
6284 *
6285 * This cmsghdr structure provides information for
6286 * initializing new SCTP associations with sendmsg().
6287 * The SCTP_INITMSG socket option uses this same data
6288 * structure. This structure is not used for
6289 * recvmsg().
6290 *
6291 * cmsg_level cmsg_type cmsg_data[]
6292 * ------------ ------------ ----------------------
6293 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
6294 */
6295 if (cmsg->cmsg_len !=
6296 CMSG_LEN(sizeof(struct sctp_initmsg)))
6297 return -EINVAL;
6298 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
6299 break;
6300
6301 case SCTP_SNDRCV:
6302 /* SCTP Socket API Extension
6303 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
6304 *
6305 * This cmsghdr structure specifies SCTP options for
6306 * sendmsg() and describes SCTP header information
6307 * about a received message through recvmsg().
6308 *
6309 * cmsg_level cmsg_type cmsg_data[]
6310 * ------------ ------------ ----------------------
6311 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
6312 */
6313 if (cmsg->cmsg_len !=
6314 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
6315 return -EINVAL;
6316
6317 cmsgs->info =
6318 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
6319
6320 /* Minimally, validate the sinfo_flags. */
6321 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07006322 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6323 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 return -EINVAL;
6325 break;
6326
6327 default:
6328 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 }
6331 return 0;
6332}
6333
6334/*
6335 * Wait for a packet..
6336 * Note: This function is the same function as in core/datagram.c
6337 * with a few modifications to make lksctp work.
6338 */
6339static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6340{
6341 int error;
6342 DEFINE_WAIT(wait);
6343
Eric Dumazetaa395142010-04-20 13:03:51 +00006344 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346 /* Socket errors? */
6347 error = sock_error(sk);
6348 if (error)
6349 goto out;
6350
6351 if (!skb_queue_empty(&sk->sk_receive_queue))
6352 goto ready;
6353
6354 /* Socket shut down? */
6355 if (sk->sk_shutdown & RCV_SHUTDOWN)
6356 goto out;
6357
6358 /* Sequenced packets can come disconnected. If so we report the
6359 * problem.
6360 */
6361 error = -ENOTCONN;
6362
6363 /* Is there a good reason to think that we may receive some data? */
6364 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6365 goto out;
6366
6367 /* Handle signals. */
6368 if (signal_pending(current))
6369 goto interrupted;
6370
6371 /* Let another process have a go. Since we are going to sleep
6372 * anyway. Note: This may cause odd behaviors if the message
6373 * does not fit in the user's buffer, but this seems to be the
6374 * only way to honor MSG_DONTWAIT realistically.
6375 */
6376 sctp_release_sock(sk);
6377 *timeo_p = schedule_timeout(*timeo_p);
6378 sctp_lock_sock(sk);
6379
6380ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00006381 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 return 0;
6383
6384interrupted:
6385 error = sock_intr_errno(*timeo_p);
6386
6387out:
Eric Dumazetaa395142010-04-20 13:03:51 +00006388 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 *err = error;
6390 return error;
6391}
6392
6393/* Receive a datagram.
6394 * Note: This is pretty much the same routine as in core/datagram.c
6395 * with a few changes to make lksctp work.
6396 */
6397static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6398 int noblock, int *err)
6399{
6400 int error;
6401 struct sk_buff *skb;
6402 long timeo;
6403
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 timeo = sock_rcvtimeo(sk, noblock);
6405
6406 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6407 timeo, MAX_SCHEDULE_TIMEOUT);
6408
6409 do {
6410 /* Again only user level code calls this function,
6411 * so nothing interrupt level
6412 * will suddenly eat the receive_queue.
6413 *
6414 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00006415 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 */
6417 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07006418 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 skb = skb_peek(&sk->sk_receive_queue);
6420 if (skb)
6421 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07006422 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 } else {
6424 skb = skb_dequeue(&sk->sk_receive_queue);
6425 }
6426
6427 if (skb)
6428 return skb;
6429
Neil Horman6736dc32005-12-02 20:30:06 -08006430 /* Caller is allowed not to check sk->sk_err before calling. */
6431 error = sock_error(sk);
6432 if (error)
6433 goto no_packet;
6434
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 if (sk->sk_shutdown & RCV_SHUTDOWN)
6436 break;
6437
6438 /* User doesn't want to wait. */
6439 error = -EAGAIN;
6440 if (!timeo)
6441 goto no_packet;
6442 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6443
6444 return NULL;
6445
6446no_packet:
6447 *err = error;
6448 return NULL;
6449}
6450
6451/* If sndbuf has changed, wake up per association sndbuf waiters. */
6452static void __sctp_write_space(struct sctp_association *asoc)
6453{
6454 struct sock *sk = asoc->base.sk;
6455 struct socket *sock = sk->sk_socket;
6456
6457 if ((sctp_wspace(asoc) > 0) && sock) {
6458 if (waitqueue_active(&asoc->wait))
6459 wake_up_interruptible(&asoc->wait);
6460
6461 if (sctp_writeable(sk)) {
Eric Dumazeteaefd112011-02-18 03:26:36 +00006462 wait_queue_head_t *wq = sk_sleep(sk);
6463
6464 if (wq && waitqueue_active(wq))
6465 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
6467 /* Note that we try to include the Async I/O support
6468 * here by modeling from the current TCP/UDP code.
6469 * We have not tested with it yet.
6470 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00006471 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08006472 sock_wake_async(sock,
6473 SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
6475 }
6476}
6477
6478/* Do accounting for the sndbuf space.
6479 * Decrement the used sndbuf space of the corresponding association by the
6480 * data size which was just transmitted(freed).
6481 */
6482static void sctp_wfree(struct sk_buff *skb)
6483{
6484 struct sctp_association *asoc;
6485 struct sctp_chunk *chunk;
6486 struct sock *sk;
6487
6488 /* Get the saved chunk pointer. */
6489 chunk = *((struct sctp_chunk **)(skb->cb));
6490 asoc = chunk->asoc;
6491 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006492 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6493 sizeof(struct sk_buff) +
6494 sizeof(struct sctp_chunk);
6495
Neil Horman4eb701d2005-04-28 12:02:04 -07006496 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6497
Neil Horman4d93df02007-08-15 16:07:44 -07006498 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006499 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07006500 */
6501 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006502 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07006503
Neil Horman4eb701d2005-04-28 12:02:04 -07006504 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 __sctp_write_space(asoc);
6506
6507 sctp_association_put(asoc);
6508}
6509
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006510/* Do accounting for the receive space on the socket.
6511 * Accounting for the association is done in ulpevent.c
6512 * We set this as a destructor for the cloned data skbs so that
6513 * accounting is done at the correct time.
6514 */
6515void sctp_sock_rfree(struct sk_buff *skb)
6516{
6517 struct sock *sk = skb->sk;
6518 struct sctp_ulpevent *event = sctp_skb2event(skb);
6519
6520 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006521
6522 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006523 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07006524 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006525 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006526}
6527
6528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529/* Helper function to wait for space in the sndbuf. */
6530static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6531 size_t msg_len)
6532{
6533 struct sock *sk = asoc->base.sk;
6534 int err = 0;
6535 long current_timeo = *timeo_p;
6536 DEFINE_WAIT(wait);
6537
6538 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006539 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
6541 /* Increment the association's refcnt. */
6542 sctp_association_hold(asoc);
6543
6544 /* Wait on the association specific sndbuf space. */
6545 for (;;) {
6546 prepare_to_wait_exclusive(&asoc->wait, &wait,
6547 TASK_INTERRUPTIBLE);
6548 if (!*timeo_p)
6549 goto do_nonblock;
6550 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6551 asoc->base.dead)
6552 goto do_error;
6553 if (signal_pending(current))
6554 goto do_interrupted;
6555 if (msg_len <= sctp_wspace(asoc))
6556 break;
6557
6558 /* Let another process have a go. Since we are going
6559 * to sleep anyway.
6560 */
6561 sctp_release_sock(sk);
6562 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006563 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 sctp_lock_sock(sk);
6565
6566 *timeo_p = current_timeo;
6567 }
6568
6569out:
6570 finish_wait(&asoc->wait, &wait);
6571
6572 /* Release the association's refcnt. */
6573 sctp_association_put(asoc);
6574
6575 return err;
6576
6577do_error:
6578 err = -EPIPE;
6579 goto out;
6580
6581do_interrupted:
6582 err = sock_intr_errno(*timeo_p);
6583 goto out;
6584
6585do_nonblock:
6586 err = -EAGAIN;
6587 goto out;
6588}
6589
Wei Yongjun561b1732010-04-28 08:47:18 +00006590void sctp_data_ready(struct sock *sk, int len)
6591{
David S. Miller7ef52732010-05-02 21:43:40 -07006592 struct socket_wq *wq;
6593
6594 rcu_read_lock();
6595 wq = rcu_dereference(sk->sk_wq);
6596 if (wq_has_sleeper(wq))
6597 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00006598 POLLRDNORM | POLLRDBAND);
6599 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07006600 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00006601}
6602
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603/* If socket sndbuf has changed, wake up all per association waiters. */
6604void sctp_write_space(struct sock *sk)
6605{
6606 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
6608 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07006609 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 __sctp_write_space(asoc);
6611 }
6612}
6613
6614/* Is there any sndbuf space available on the socket?
6615 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006616 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 * associations on the same socket. For a UDP-style socket with
6618 * multiple associations, it is possible for it to be "unwriteable"
6619 * prematurely. I assume that this is acceptable because
6620 * a premature "unwriteable" is better than an accidental "writeable" which
6621 * would cause an unwanted block under certain circumstances. For the 1-1
6622 * UDP-style sockets or TCP-style sockets, this code should work.
6623 * - Daisy
6624 */
6625static int sctp_writeable(struct sock *sk)
6626{
6627 int amt = 0;
6628
Eric Dumazet31e6d362009-06-17 19:05:41 -07006629 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 if (amt < 0)
6631 amt = 0;
6632 return amt;
6633}
6634
6635/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6636 * returns immediately with EINPROGRESS.
6637 */
6638static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6639{
6640 struct sock *sk = asoc->base.sk;
6641 int err = 0;
6642 long current_timeo = *timeo_p;
6643 DEFINE_WAIT(wait);
6644
Harvey Harrison0dc47872008-03-05 20:47:47 -08006645 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 (long)(*timeo_p));
6647
6648 /* Increment the association's refcnt. */
6649 sctp_association_hold(asoc);
6650
6651 for (;;) {
6652 prepare_to_wait_exclusive(&asoc->wait, &wait,
6653 TASK_INTERRUPTIBLE);
6654 if (!*timeo_p)
6655 goto do_nonblock;
6656 if (sk->sk_shutdown & RCV_SHUTDOWN)
6657 break;
6658 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6659 asoc->base.dead)
6660 goto do_error;
6661 if (signal_pending(current))
6662 goto do_interrupted;
6663
6664 if (sctp_state(asoc, ESTABLISHED))
6665 break;
6666
6667 /* Let another process have a go. Since we are going
6668 * to sleep anyway.
6669 */
6670 sctp_release_sock(sk);
6671 current_timeo = schedule_timeout(current_timeo);
6672 sctp_lock_sock(sk);
6673
6674 *timeo_p = current_timeo;
6675 }
6676
6677out:
6678 finish_wait(&asoc->wait, &wait);
6679
6680 /* Release the association's refcnt. */
6681 sctp_association_put(asoc);
6682
6683 return err;
6684
6685do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006686 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 err = -ETIMEDOUT;
6688 else
6689 err = -ECONNREFUSED;
6690 goto out;
6691
6692do_interrupted:
6693 err = sock_intr_errno(*timeo_p);
6694 goto out;
6695
6696do_nonblock:
6697 err = -EINPROGRESS;
6698 goto out;
6699}
6700
6701static int sctp_wait_for_accept(struct sock *sk, long timeo)
6702{
6703 struct sctp_endpoint *ep;
6704 int err = 0;
6705 DEFINE_WAIT(wait);
6706
6707 ep = sctp_sk(sk)->ep;
6708
6709
6710 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00006711 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 TASK_INTERRUPTIBLE);
6713
6714 if (list_empty(&ep->asocs)) {
6715 sctp_release_sock(sk);
6716 timeo = schedule_timeout(timeo);
6717 sctp_lock_sock(sk);
6718 }
6719
6720 err = -EINVAL;
6721 if (!sctp_sstate(sk, LISTENING))
6722 break;
6723
6724 err = 0;
6725 if (!list_empty(&ep->asocs))
6726 break;
6727
6728 err = sock_intr_errno(timeo);
6729 if (signal_pending(current))
6730 break;
6731
6732 err = -EAGAIN;
6733 if (!timeo)
6734 break;
6735 }
6736
Eric Dumazetaa395142010-04-20 13:03:51 +00006737 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 return err;
6740}
6741
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006742static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743{
6744 DEFINE_WAIT(wait);
6745
6746 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00006747 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 if (list_empty(&sctp_sk(sk)->ep->asocs))
6749 break;
6750 sctp_release_sock(sk);
6751 timeout = schedule_timeout(timeout);
6752 sctp_lock_sock(sk);
6753 } while (!signal_pending(current) && timeout);
6754
Eric Dumazetaa395142010-04-20 13:03:51 +00006755 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756}
6757
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006758static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6759{
6760 struct sk_buff *frag;
6761
6762 if (!skb->data_len)
6763 goto done;
6764
6765 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07006766 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006767 sctp_skb_set_owner_r_frag(frag, sk);
6768
6769done:
6770 sctp_skb_set_owner_r(skb, sk);
6771}
6772
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006773void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6774 struct sctp_association *asoc)
6775{
6776 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08006777 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006778
6779 newsk->sk_type = sk->sk_type;
6780 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6781 newsk->sk_flags = sk->sk_flags;
6782 newsk->sk_no_check = sk->sk_no_check;
6783 newsk->sk_reuse = sk->sk_reuse;
6784
6785 newsk->sk_shutdown = sk->sk_shutdown;
6786 newsk->sk_destruct = inet_sock_destruct;
6787 newsk->sk_family = sk->sk_family;
6788 newsk->sk_protocol = IPPROTO_SCTP;
6789 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6790 newsk->sk_sndbuf = sk->sk_sndbuf;
6791 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6792 newsk->sk_lingertime = sk->sk_lingertime;
6793 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6794 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6795
6796 newinet = inet_sk(newsk);
6797
6798 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6799 * getsockname() and getpeername()
6800 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006801 newinet->inet_sport = inet->inet_sport;
6802 newinet->inet_saddr = inet->inet_saddr;
6803 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6804 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006805 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006806 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006807
6808 newinet->uc_ttl = inet->uc_ttl;
6809 newinet->mc_loop = 1;
6810 newinet->mc_ttl = 1;
6811 newinet->mc_index = 0;
6812 newinet->mc_list = NULL;
6813}
6814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815/* Populate the fields of the newsk from the oldsk and migrate the assoc
6816 * and its messages to the newsk.
6817 */
6818static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6819 struct sctp_association *assoc,
6820 sctp_socket_type_t type)
6821{
6822 struct sctp_sock *oldsp = sctp_sk(oldsk);
6823 struct sctp_sock *newsp = sctp_sk(newsk);
6824 struct sctp_bind_bucket *pp; /* hash list port iterator */
6825 struct sctp_endpoint *newep = newsp->ep;
6826 struct sk_buff *skb, *tmp;
6827 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006828 struct sctp_bind_hashbucket *head;
Michio Honda9f7d6532011-04-26 19:32:51 +09006829 struct list_head tmplist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
6831 /* Migrate socket buffer sizes and all the socket level options to the
6832 * new socket.
6833 */
6834 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6835 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6836 /* Brute force copy old sctp opt. */
Michio Honda9f7d6532011-04-26 19:32:51 +09006837 if (oldsp->do_auto_asconf) {
6838 memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist));
6839 inet_sk_copy_descendant(newsk, oldsk);
6840 memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist));
6841 } else
6842 inet_sk_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
6844 /* Restore the ep value that was overwritten with the above structure
6845 * copy.
6846 */
6847 newsp->ep = newep;
6848 newsp->hmac = NULL;
6849
6850 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006851 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
6852 inet_sk(oldsk)->inet_num)];
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006853 sctp_local_bh_disable();
6854 sctp_spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 pp = sctp_sk(oldsk)->bind_hash;
6856 sk_add_bind_node(newsk, &pp->owner);
6857 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006858 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006859 sctp_spin_unlock(&head->lock);
6860 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006862 /* Copy the bind_addr list from the original endpoint to the new
6863 * endpoint so that we can handle restarts properly
6864 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08006865 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6866 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006867
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 /* Move any messages in the old socket's receive queue that are for the
6869 * peeled off association to the new socket's receive queue.
6870 */
6871 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6872 event = sctp_skb2event(skb);
6873 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006874 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006876 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 }
6878 }
6879
6880 /* Clean up any messages pending delivery due to partial
6881 * delivery. Three cases:
6882 * 1) No partial deliver; no work.
6883 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6884 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6885 */
6886 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006887 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006889 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 struct sk_buff_head *queue;
6891
6892 /* Decide which queue to move pd_lobby skbs to. */
6893 if (assoc->ulpq.pd_mode) {
6894 queue = &newsp->pd_lobby;
6895 } else
6896 queue = &newsk->sk_receive_queue;
6897
6898 /* Walk through the pd_lobby, looking for skbs that
6899 * need moved to the new socket.
6900 */
6901 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6902 event = sctp_skb2event(skb);
6903 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006904 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006906 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 }
6908 }
6909
6910 /* Clear up any skbs waiting for the partial
6911 * delivery to finish.
6912 */
6913 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006914 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916 }
6917
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006918 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006919 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006920
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006921 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006922 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006923
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 /* Set the type of socket to indicate that it is peeled off from the
6925 * original UDP-style socket or created with the accept() call on a
6926 * TCP-style socket..
6927 */
6928 newsp->type = type;
6929
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006930 /* Mark the new socket "in-use" by the user so that any packets
6931 * that may arrive on the association after we've moved it are
6932 * queued to the backlog. This prevents a potential race between
6933 * backlog processing on the old socket and new-packet processing
6934 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006935 *
6936 * The caller has just allocated newsk so we can guarantee that other
6937 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006938 */
Zach Brown5131a182007-06-22 15:14:46 -07006939 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 /* If the association on the newsk is already closed before accept()
6943 * is called, set RCV_SHUTDOWN flag.
6944 */
6945 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6946 newsk->sk_shutdown |= RCV_SHUTDOWN;
6947
6948 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006949 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950}
6951
Neil Horman4d93df02007-08-15 16:07:44 -07006952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953/* This proto struct describes the ULP interface for SCTP. */
6954struct proto sctp_prot = {
6955 .name = "SCTP",
6956 .owner = THIS_MODULE,
6957 .close = sctp_close,
6958 .connect = sctp_connect,
6959 .disconnect = sctp_disconnect,
6960 .accept = sctp_accept,
6961 .ioctl = sctp_ioctl,
6962 .init = sctp_init_sock,
6963 .destroy = sctp_destroy_sock,
6964 .shutdown = sctp_shutdown,
6965 .setsockopt = sctp_setsockopt,
6966 .getsockopt = sctp_getsockopt,
6967 .sendmsg = sctp_sendmsg,
6968 .recvmsg = sctp_recvmsg,
6969 .bind = sctp_bind,
6970 .backlog_rcv = sctp_backlog_rcv,
6971 .hash = sctp_hash,
6972 .unhash = sctp_unhash,
6973 .get_port = sctp_get_port,
6974 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006975 .sysctl_mem = sysctl_sctp_mem,
6976 .sysctl_rmem = sysctl_sctp_rmem,
6977 .sysctl_wmem = sysctl_sctp_wmem,
6978 .memory_pressure = &sctp_memory_pressure,
6979 .enter_memory_pressure = sctp_enter_memory_pressure,
6980 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006981 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982};
6983
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006984#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986struct proto sctpv6_prot = {
6987 .name = "SCTPv6",
6988 .owner = THIS_MODULE,
6989 .close = sctp_close,
6990 .connect = sctp_connect,
6991 .disconnect = sctp_disconnect,
6992 .accept = sctp_accept,
6993 .ioctl = sctp_ioctl,
6994 .init = sctp_init_sock,
6995 .destroy = sctp_destroy_sock,
6996 .shutdown = sctp_shutdown,
6997 .setsockopt = sctp_setsockopt,
6998 .getsockopt = sctp_getsockopt,
6999 .sendmsg = sctp_sendmsg,
7000 .recvmsg = sctp_recvmsg,
7001 .bind = sctp_bind,
7002 .backlog_rcv = sctp_backlog_rcv,
7003 .hash = sctp_hash,
7004 .unhash = sctp_unhash,
7005 .get_port = sctp_get_port,
7006 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07007007 .sysctl_mem = sysctl_sctp_mem,
7008 .sysctl_rmem = sysctl_sctp_rmem,
7009 .sysctl_wmem = sysctl_sctp_wmem,
7010 .memory_pressure = &sctp_memory_pressure,
7011 .enter_memory_pressure = sctp_enter_memory_pressure,
7012 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08007013 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00007015#endif /* IS_ENABLED(CONFIG_IPV6) */