blob: bc7b5de49725cca7153bd64f81efa935de0d8ef1 [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>
Daniel Borkmann19e48382014-02-17 12:11:11 +010073#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#include <net/ip.h>
76#include <net/icmp.h>
77#include <net/route.h>
78#include <net/ipv6.h>
79#include <net/inet_common.h>
80
81#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040082#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <net/sock.h>
84#include <net/sctp/sctp.h>
85#include <net/sctp/sm.h>
86
87/* WARNING: Please do not remove the SCTP_STATIC attribute to
88 * any of the functions below as they are used to export functions
89 * used by a project regression testsuite.
90 */
91
92/* Forward declarations for internal helper functions. */
93static int sctp_writeable(struct sock *sk);
94static void sctp_wfree(struct sk_buff *skb);
95static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
96 size_t msg_len);
97static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
98static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
99static int sctp_wait_for_accept(struct sock *sk, long timeo);
100static void sctp_wait_for_close(struct sock *sk, long timeo);
101static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
102 union sctp_addr *addr, int len);
103static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
104static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
105static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
106static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
107static int sctp_send_asconf(struct sctp_association *asoc,
108 struct sctp_chunk *chunk);
109static int sctp_do_bind(struct sock *, union sctp_addr *, int);
110static int sctp_autobind(struct sock *sk);
111static void sctp_sock_migrate(struct sock *, struct sock *,
112 struct sctp_association *, sctp_socket_type_t);
113static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
114
Neil Horman4d93df02007-08-15 16:07:44 -0700115extern struct kmem_cache *sctp_bucket_cachep;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000116extern long sysctl_sctp_mem[3];
Neil Horman4d93df02007-08-15 16:07:44 -0700117extern int sysctl_sctp_rmem[3];
118extern int sysctl_sctp_wmem[3];
119
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200120static int sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000121static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800122struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700123
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700124static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700125{
126 sctp_memory_pressure = 1;
127}
128
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* Get the sndbuf space available at the time on the association. */
131static inline int sctp_wspace(struct sctp_association *asoc)
132{
Neil Horman4d93df02007-08-15 16:07:44 -0700133 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Neil Horman4d93df02007-08-15 16:07:44 -0700135 if (asoc->ep->sndbuf_policy)
136 amt = asoc->sndbuf_used;
137 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700138 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700139
140 if (amt >= asoc->base.sk->sk_sndbuf) {
141 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
142 amt = 0;
143 else {
144 amt = sk_stream_wspace(asoc->base.sk);
145 if (amt < 0)
146 amt = 0;
147 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700148 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700149 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return amt;
152}
153
154/* Increment the used sndbuf space count of the corresponding association by
155 * the size of the outgoing data chunk.
156 * Also, set the skb destructor for sndbuf accounting later.
157 *
158 * Since it is always 1-1 between chunk and skb, and also a new skb is always
159 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
160 * destructor in the data chunk skb for the purpose of the sndbuf space
161 * tracking.
162 */
163static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
164{
165 struct sctp_association *asoc = chunk->asoc;
166 struct sock *sk = asoc->base.sk;
167
168 /* The sndbuf space is tracked per association. */
169 sctp_association_hold(asoc);
170
Neil Horman4eb701d2005-04-28 12:02:04 -0700171 skb_set_owner_w(chunk->skb, sk);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 chunk->skb->destructor = sctp_wfree;
174 /* Save the chunk pointer in skb for sctp_wfree to use later. */
175 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
176
Neil Horman4eb701d2005-04-28 12:02:04 -0700177 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
178 sizeof(struct sk_buff) +
179 sizeof(struct sctp_chunk);
180
Neil Horman4eb701d2005-04-28 12:02:04 -0700181 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800182 sk->sk_wmem_queued += chunk->skb->truesize;
183 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/* Verify that this is a valid address. */
187static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
188 int len)
189{
190 struct sctp_af *af;
191
192 /* Verify basic sockaddr. */
193 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
194 if (!af)
195 return -EINVAL;
196
197 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700198 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
200
201 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
202 return -EINVAL;
203
204 return 0;
205}
206
207/* Look up the association by its id. If this is not a UDP-style
208 * socket, the ID field is always ignored.
209 */
210struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
211{
212 struct sctp_association *asoc = NULL;
213
214 /* If this is not a UDP-style socket, assoc id should be ignored. */
215 if (!sctp_style(sk, UDP)) {
216 /* Return NULL if the socket state is not ESTABLISHED. It
217 * could be a TCP-style listening socket or a socket which
218 * hasn't yet called connect() to establish an association.
219 */
220 if (!sctp_sstate(sk, ESTABLISHED))
221 return NULL;
222
223 /* Get the first and the only association from the list. */
224 if (!list_empty(&sctp_sk(sk)->ep->asocs))
225 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
226 struct sctp_association, asocs);
227 return asoc;
228 }
229
230 /* Otherwise this is a UDP-style socket. */
231 if (!id || (id == (sctp_assoc_t)-1))
232 return NULL;
233
234 spin_lock_bh(&sctp_assocs_id_lock);
235 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
236 spin_unlock_bh(&sctp_assocs_id_lock);
237
238 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
239 return NULL;
240
241 return asoc;
242}
243
244/* Look up the transport from an address and an assoc id. If both address and
245 * id are specified, the associations matching the address and the id should be
246 * the same.
247 */
248static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
249 struct sockaddr_storage *addr,
250 sctp_assoc_t id)
251{
252 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
253 struct sctp_transport *transport;
254 union sctp_addr *laddr = (union sctp_addr *)addr;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800257 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (!addr_asoc)
261 return NULL;
262
263 id_asoc = sctp_id2assoc(sk, id);
264 if (id_asoc && (id_asoc != addr_asoc))
265 return NULL;
266
267 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
268 (union sctp_addr *)addr);
269
270 return transport;
271}
272
273/* API 3.1.2 bind() - UDP Style Syntax
274 * The syntax of bind() is,
275 *
276 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
277 *
278 * sd - the socket descriptor returned by socket().
279 * addr - the address structure (struct sockaddr_in or struct
280 * sockaddr_in6 [RFC 2553]),
281 * addr_len - the size of the address structure.
282 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700283SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int retval = 0;
286
287 sctp_lock_sock(sk);
288
Frank Filz3f7a87d2005-06-20 13:14:57 -0700289 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
290 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Disallow binding twice. */
293 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700294 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 addr_len);
296 else
297 retval = -EINVAL;
298
299 sctp_release_sock(sk);
300
301 return retval;
302}
303
304static long sctp_get_port_local(struct sock *, union sctp_addr *);
305
306/* Verify this is a valid sockaddr. */
307static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
308 union sctp_addr *addr, int len)
309{
310 struct sctp_af *af;
311
312 /* Check minimum size. */
313 if (len < sizeof (struct sockaddr))
314 return NULL;
315
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700316 /* V4 mapped address are really of AF_INET family */
317 if (addr->sa.sa_family == AF_INET6 &&
318 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
319 if (!opt->pf->af_supported(AF_INET, opt))
320 return NULL;
321 } else {
322 /* Does this PF support this AF? */
323 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
324 return NULL;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* If we get this far, af is valid. */
328 af = sctp_get_af_specific(addr->sa.sa_family);
329
330 if (len < af->sockaddr_len)
331 return NULL;
332
333 return af;
334}
335
336/* Bind a local address either to an endpoint or to an association. */
337SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
338{
339 struct sctp_sock *sp = sctp_sk(sk);
340 struct sctp_endpoint *ep = sp->ep;
341 struct sctp_bind_addr *bp = &ep->base.bind_addr;
342 struct sctp_af *af;
343 unsigned short snum;
344 int ret = 0;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Common sockaddr verification. */
347 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700348 if (!af) {
349 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
350 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700352 }
353
354 snum = ntohs(addr->v4.sin_port);
355
356 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
357 ", port: %d, new port: %d, len: %d)\n",
358 sk,
359 addr,
360 bp->port, snum,
361 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* PF specific bind() address verification. */
364 if (!sp->pf->bind_verify(sp, addr))
365 return -EADDRNOTAVAIL;
366
Vlad Yasevich8b358052007-05-15 17:14:58 -0400367 /* We must either be unbound, or bind to the same port.
368 * It's OK to allow 0 ports if we are already bound.
369 * We'll just inhert an already bound port in this case
370 */
371 if (bp->port) {
372 if (!snum)
373 snum = bp->port;
374 else if (snum != bp->port) {
375 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 " New port %d does not match existing port "
377 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400378 return -EINVAL;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
383 return -EACCES;
384
Vlad Yasevich4e540642008-07-18 23:06:32 -0700385 /* See if the address matches any of the addresses we may have
386 * already bound before checking against other endpoints.
387 */
388 if (sctp_bind_addr_match(bp, addr, sp))
389 return -EINVAL;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Make sure we are allowed to bind here.
392 * The function sctp_get_port_local() does duplicate address
393 * detection.
394 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900395 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700397 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 /* Refresh ephemeral port. */
401 if (!bp->port)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000402 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Vlad Yasevich559cf712007-09-16 16:03:28 -0700404 /* Add the address to the bind address list.
405 * Use GFP_ATOMIC since BHs will be disabled.
406 */
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800407 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Copy back into socket for getsockname() use. */
410 if (!ret) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000411 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 af->to_sk_saddr(addr, sk);
413 }
414
415 return ret;
416}
417
418 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
419 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900420 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900422 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900424 * subsequent ASCONF. Note this restriction binds each side, so at any
425 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * from each endpoint).
427 */
428static int sctp_send_asconf(struct sctp_association *asoc,
429 struct sctp_chunk *chunk)
430{
431 int retval = 0;
432
433 /* If there is an outstanding ASCONF chunk, queue it for later
434 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700437 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
441 /* Hold the chunk until an ASCONF_ACK is received. */
442 sctp_chunk_hold(chunk);
443 retval = sctp_primitive_ASCONF(asoc, chunk);
444 if (retval)
445 sctp_chunk_free(chunk);
446 else
447 asoc->addip_last_asconf = chunk;
448
449out:
450 return retval;
451}
452
453/* Add a list of addresses as bind addresses to local endpoint or
454 * association.
455 *
456 * Basically run through each address specified in the addrs/addrcnt
457 * array/length pair, determine if it is IPv6 or IPv4 and call
458 * sctp_do_bind() on it.
459 *
460 * If any of them fails, then the operation will be reversed and the
461 * ones that were added will be removed.
462 *
463 * Only sctp_setsockopt_bindx() is supposed to call this function.
464 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200465static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int cnt;
468 int retval = 0;
469 void *addr_buf;
470 struct sockaddr *sa_addr;
471 struct sctp_af *af;
472
473 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
474 sk, addrs, addrcnt);
475
476 addr_buf = addrs;
477 for (cnt = 0; cnt < addrcnt; cnt++) {
478 /* The list may contain either IPv4 or IPv6 address;
479 * determine the address length for walking thru the list.
480 */
Joe Perchesea110732011-06-13 16:21:26 +0000481 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 af = sctp_get_af_specific(sa_addr->sa_family);
483 if (!af) {
484 retval = -EINVAL;
485 goto err_bindx_add;
486 }
487
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900488 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 af->sockaddr_len);
490
491 addr_buf += af->sockaddr_len;
492
493err_bindx_add:
494 if (retval < 0) {
495 /* Failed. Cleanup the ones that have been added */
496 if (cnt > 0)
497 sctp_bindx_rem(sk, addrs, cnt);
498 return retval;
499 }
500 }
501
502 return retval;
503}
504
505/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
506 * associations that are part of the endpoint indicating that a list of local
507 * addresses are added to the endpoint.
508 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900509 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * association, we do not send the chunk for that association. But it will not
511 * affect other associations.
512 *
513 * Only sctp_setsockopt_bindx() is supposed to call this function.
514 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900515static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sockaddr *addrs,
517 int addrcnt)
518{
519 struct sctp_sock *sp;
520 struct sctp_endpoint *ep;
521 struct sctp_association *asoc;
522 struct sctp_bind_addr *bp;
523 struct sctp_chunk *chunk;
524 struct sctp_sockaddr_entry *laddr;
525 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700526 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 void *addr_buf;
528 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct list_head *p;
530 int i;
531 int retval = 0;
532
533 if (!sctp_addip_enable)
534 return retval;
535
536 sp = sctp_sk(sk);
537 ep = sp->ep;
538
539 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800540 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700542 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (!asoc->peer.asconf_capable)
545 continue;
546
547 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
548 continue;
549
550 if (!sctp_state(asoc, ESTABLISHED))
551 continue;
552
553 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900554 * in the bind address list of the association. If so,
555 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * other associations.
557 */
558 addr_buf = addrs;
559 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000560 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 af = sctp_get_af_specific(addr->v4.sin_family);
562 if (!af) {
563 retval = -EINVAL;
564 goto out;
565 }
566
567 if (sctp_assoc_lookup_laddr(asoc, addr))
568 break;
569
570 addr_buf += af->sockaddr_len;
571 }
572 if (i < addrcnt)
573 continue;
574
Vlad Yasevich559cf712007-09-16 16:03:28 -0700575 /* Use the first valid address in bind addr list of
576 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 bp = &asoc->base.bind_addr;
579 p = bp->address_list.next;
580 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800581 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 addrcnt, SCTP_PARAM_ADD_IP);
583 if (!chunk) {
584 retval = -ENOMEM;
585 goto out;
586 }
587
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700591 addr_buf = addrs;
592 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000593 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700594 af = sctp_get_af_specific(addr->v4.sin_family);
595 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800596 retval = sctp_add_bind_addr(bp, &saveaddr,
597 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700598 addr_buf += af->sockaddr_len;
599 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900600 if (asoc->src_out_of_asoc_ok) {
601 struct sctp_transport *trans;
602
603 list_for_each_entry(trans,
604 &asoc->peer.transport_addr_list, transports) {
605 /* Clear the source and route cache */
606 dst_release(trans->dst);
607 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
608 2*asoc->pathmtu, 4380));
609 trans->ssthresh = asoc->peer.i.a_rwnd;
610 trans->rto = asoc->rto_initial;
611 trans->rtt = trans->srtt = trans->rttvar = 0;
612 sctp_transport_route(trans, NULL,
613 sctp_sk(asoc->base.sk));
614 }
615 }
616 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619out:
620 return retval;
621}
622
623/* Remove a list of addresses from bind addresses list. Do not remove the
624 * last address.
625 *
626 * Basically run through each address specified in the addrs/addrcnt
627 * array/length pair, determine if it is IPv6 or IPv4 and call
628 * sctp_del_bind() on it.
629 *
630 * If any of them fails, then the operation will be reversed and the
631 * ones that were removed will be added back.
632 *
633 * At least one address has to be left; if only one address is
634 * available, the operation will return -EBUSY.
635 *
636 * Only sctp_setsockopt_bindx() is supposed to call this function.
637 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200638static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct sctp_sock *sp = sctp_sk(sk);
641 struct sctp_endpoint *ep = sp->ep;
642 int cnt;
643 struct sctp_bind_addr *bp = &ep->base.bind_addr;
644 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800646 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct sctp_af *af;
648
649 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
650 sk, addrs, addrcnt);
651
652 addr_buf = addrs;
653 for (cnt = 0; cnt < addrcnt; cnt++) {
654 /* If the bind address list is empty or if there is only one
655 * bind address, there is nothing more to be removed (we need
656 * at least one address here).
657 */
658 if (list_empty(&bp->address_list) ||
659 (sctp_list_single_entry(&bp->address_list))) {
660 retval = -EBUSY;
661 goto err_bindx_rem;
662 }
663
Joe Perchesea110732011-06-13 16:21:26 +0000664 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800665 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!af) {
667 retval = -EINVAL;
668 goto err_bindx_rem;
669 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700670
671 if (!af->addr_valid(sa_addr, sp, NULL)) {
672 retval = -EADDRNOTAVAIL;
673 goto err_bindx_rem;
674 }
675
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000676 if (sa_addr->v4.sin_port &&
677 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retval = -EINVAL;
679 goto err_bindx_rem;
680 }
681
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000682 if (!sa_addr->v4.sin_port)
683 sa_addr->v4.sin_port = htons(bp->port);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* FIXME - There is probably a need to check if sk->sk_saddr and
686 * sk->sk_rcv_addr are currently set to one of the addresses to
687 * be removed. This is something which needs to be looked into
688 * when we are fixing the outstanding issues with multi-homing
689 * socket routing and failover schemes. Refer to comments in
690 * sctp_do_bind(). -daisy
691 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400692 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 addr_buf += af->sockaddr_len;
695err_bindx_rem:
696 if (retval < 0) {
697 /* Failed. Add the ones that has been removed back */
698 if (cnt > 0)
699 sctp_bindx_add(sk, addrs, cnt);
700 return retval;
701 }
702 }
703
704 return retval;
705}
706
707/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
708 * the associations that are part of the endpoint indicating that a list of
709 * local addresses are removed from the endpoint.
710 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900711 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * association, we do not send the chunk for that association. But it will not
713 * affect other associations.
714 *
715 * Only sctp_setsockopt_bindx() is supposed to call this function.
716 */
717static int sctp_send_asconf_del_ip(struct sock *sk,
718 struct sockaddr *addrs,
719 int addrcnt)
720{
721 struct sctp_sock *sp;
722 struct sctp_endpoint *ep;
723 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700724 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct sctp_bind_addr *bp;
726 struct sctp_chunk *chunk;
727 union sctp_addr *laddr;
728 void *addr_buf;
729 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700730 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int i;
732 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900733 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Michio Honda8a07eb02011-04-26 20:19:36 +0900735 chunk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!sctp_addip_enable)
737 return retval;
738
739 sp = sctp_sk(sk);
740 ep = sp->ep;
741
742 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800743 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700745 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (!asoc->peer.asconf_capable)
748 continue;
749
750 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
751 continue;
752
753 if (!sctp_state(asoc, ESTABLISHED))
754 continue;
755
756 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900757 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * If so, do not send the asconf chunk to its peer, but
759 * continue with other associations.
760 */
761 addr_buf = addrs;
762 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000763 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 af = sctp_get_af_specific(laddr->v4.sin_family);
765 if (!af) {
766 retval = -EINVAL;
767 goto out;
768 }
769
770 if (!sctp_assoc_lookup_laddr(asoc, laddr))
771 break;
772
773 addr_buf += af->sockaddr_len;
774 }
775 if (i < addrcnt)
776 continue;
777
778 /* Find one address in the association's bind address list
779 * that is not in the packed array of addresses. This is to
780 * make sure that we do not delete all the addresses in the
781 * association.
782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 bp = &asoc->base.bind_addr;
784 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
785 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900786 if ((laddr == NULL) && (addrcnt == 1)) {
787 if (asoc->asconf_addr_del_pending)
788 continue;
789 asoc->asconf_addr_del_pending =
790 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900791 if (asoc->asconf_addr_del_pending == NULL) {
792 retval = -ENOMEM;
793 goto out;
794 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900795 asoc->asconf_addr_del_pending->sa.sa_family =
796 addrs->sa_family;
797 asoc->asconf_addr_del_pending->v4.sin_port =
798 htons(bp->port);
799 if (addrs->sa_family == AF_INET) {
800 struct sockaddr_in *sin;
801
802 sin = (struct sockaddr_in *)addrs;
803 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
804 } else if (addrs->sa_family == AF_INET6) {
805 struct sockaddr_in6 *sin6;
806
807 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000808 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900809 }
810 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
811 " at %p\n", asoc, asoc->asconf_addr_del_pending,
812 asoc->asconf_addr_del_pending);
813 asoc->src_out_of_asoc_ok = 1;
814 stored = 1;
815 goto skip_mkasconf;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Daniel Borkmann835e0aa2013-09-07 20:51:21 +0200818 if (laddr == NULL)
819 return -EINVAL;
820
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
977 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
978 " 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{
1057 struct sctp_sock *sp;
1058 struct sctp_endpoint *ep;
1059 struct sctp_association *asoc = NULL;
1060 struct sctp_association *asoc2;
1061 struct sctp_transport *transport;
1062 union sctp_addr to;
1063 struct sctp_af *af;
1064 sctp_scope_t scope;
1065 long timeo;
1066 int err = 0;
1067 int addrcnt = 0;
1068 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001069 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001071 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001072 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001073
1074 sp = sctp_sk(sk);
1075 ep = sp->ep;
1076
1077 /* connect() cannot be done on a socket that is already in ESTABLISHED
1078 * state - UDP-style peeled off socket or a TCP-style socket that
1079 * is already connected.
1080 * It cannot be done even on a TCP-style listening socket.
1081 */
1082 if (sctp_sstate(sk, ESTABLISHED) ||
1083 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1084 err = -EISCONN;
1085 goto out_free;
1086 }
1087
1088 /* Walk through the addrs buffer and count the number of addresses. */
1089 addr_buf = kaddrs;
1090 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001091 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1092 err = -EINVAL;
1093 goto out_free;
1094 }
1095
Joe Perchesea110732011-06-13 16:21:26 +00001096 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001097 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001098
1099 /* If the address family is not supported or if this address
1100 * causes the address buffer to overflow return EINVAL.
1101 */
1102 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1103 err = -EINVAL;
1104 goto out_free;
1105 }
1106
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001107 port = ntohs(sa_addr->v4.sin_port);
1108
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001109 /* Save current address so we can work with it */
1110 memcpy(&to, sa_addr, af->sockaddr_len);
1111
1112 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001113 if (err)
1114 goto out_free;
1115
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001116 /* Make sure the destination port is correctly set
1117 * in all addresses.
1118 */
1119 if (asoc && asoc->peer.port && asoc->peer.port != port)
1120 goto out_free;
1121
Frank Filz3f7a87d2005-06-20 13:14:57 -07001122
1123 /* Check if there already is a matching association on the
1124 * endpoint (other than the one created here).
1125 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001126 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001127 if (asoc2 && asoc2 != asoc) {
1128 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1129 err = -EISCONN;
1130 else
1131 err = -EALREADY;
1132 goto out_free;
1133 }
1134
1135 /* If we could not find a matching association on the endpoint,
1136 * make sure that there is no peeled-off association matching
1137 * the peer address even on another socket.
1138 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001139 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001140 err = -EADDRNOTAVAIL;
1141 goto out_free;
1142 }
1143
1144 if (!asoc) {
1145 /* If a bind() or sctp_bindx() is not called prior to
1146 * an sctp_connectx() call, the system picks an
1147 * ephemeral port and will choose an address set
1148 * equivalent to binding with a wildcard address.
1149 */
1150 if (!ep->base.bind_addr.port) {
1151 if (sctp_autobind(sk)) {
1152 err = -EAGAIN;
1153 goto out_free;
1154 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001155 } else {
1156 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001157 * If an unprivileged user inherits a 1-many
1158 * style socket with open associations on a
1159 * privileged port, it MAY be permitted to
1160 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001161 * be permitted to open new associations.
1162 */
1163 if (ep->base.bind_addr.port < PROT_SOCK &&
1164 !capable(CAP_NET_BIND_SERVICE)) {
1165 err = -EACCES;
1166 goto out_free;
1167 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001168 }
1169
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001170 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001171 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1172 if (!asoc) {
1173 err = -ENOMEM;
1174 goto out_free;
1175 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001176
1177 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1178 GFP_KERNEL);
1179 if (err < 0) {
1180 goto out_free;
1181 }
1182
Frank Filz3f7a87d2005-06-20 13:14:57 -07001183 }
1184
1185 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001186 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001187 SCTP_UNKNOWN);
1188 if (!transport) {
1189 err = -ENOMEM;
1190 goto out_free;
1191 }
1192
1193 addrcnt++;
1194 addr_buf += af->sockaddr_len;
1195 walk_size += af->sockaddr_len;
1196 }
1197
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001198 /* In case the user of sctp_connectx() wants an association
1199 * id back, assign one now.
1200 */
1201 if (assoc_id) {
1202 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1203 if (err < 0)
1204 goto out_free;
1205 }
1206
Frank Filz3f7a87d2005-06-20 13:14:57 -07001207 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1208 if (err < 0) {
1209 goto out_free;
1210 }
1211
1212 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001213 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001214 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1215 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001216 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001217
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001218 /* in-kernel sockets don't generally have a file allocated to them
1219 * if all they do is call sock_create_kern().
1220 */
1221 if (sk->sk_socket->file)
1222 f_flags = sk->sk_socket->file->f_flags;
1223
1224 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1225
Frank Filz3f7a87d2005-06-20 13:14:57 -07001226 err = sctp_wait_for_connect(asoc, &timeo);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001227 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001228 *assoc_id = asoc->assoc_id;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001229
1230 /* Don't free association on exit. */
1231 asoc = NULL;
1232
1233out_free:
1234
1235 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001236 " kaddrs: %p err: %d\n",
1237 asoc, kaddrs, err);
Neil Horman2936d352012-07-16 09:13:51 +00001238 if (asoc) {
1239 /* sctp_primitive_ASSOCIATE may have added this association
1240 * To the hash table, try to unhash it, just in case, its a noop
1241 * if it wasn't hashed so we're safe
1242 */
1243 sctp_unhash_established(asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001244 sctp_association_free(asoc);
Neil Horman2936d352012-07-16 09:13:51 +00001245 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001246 return err;
1247}
1248
1249/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1250 *
1251 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001252 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1253 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001254 *
1255 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1256 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1257 * or IPv6 addresses.
1258 *
1259 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1260 * Section 3.1.2 for this usage.
1261 *
1262 * addrs is a pointer to an array of one or more socket addresses. Each
1263 * address is contained in its appropriate structure (i.e. struct
1264 * sockaddr_in or struct sockaddr_in6) the family of the address type
1265 * must be used to distengish the address length (note that this
1266 * representation is termed a "packed array" of addresses). The caller
1267 * specifies the number of addresses in the array with addrcnt.
1268 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001269 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1270 * the association id of the new association. On failure, sctp_connectx()
1271 * returns -1, and sets errno to the appropriate error code. The assoc_id
1272 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001273 *
1274 * For SCTP, the port given in each socket address must be the same, or
1275 * sctp_connectx() will fail, setting errno to EINVAL.
1276 *
1277 * An application can use sctp_connectx to initiate an association with
1278 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1279 * allows a caller to specify multiple addresses at which a peer can be
1280 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001281 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001282 * specifies that the stack will try to make use of all the addresses in
1283 * the list when needed.
1284 *
1285 * Note that the list of addresses passed in is only used for setting up
1286 * the association. It does not necessarily equal the set of addresses
1287 * the peer uses for the resulting association. If the caller wants to
1288 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1289 * retrieve them after the association has been set up.
1290 *
1291 * Basically do nothing but copying the addresses from user to kernel
1292 * land and invoking either sctp_connectx(). This is used for tunneling
1293 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1294 *
1295 * We don't use copy_from_user() for optimization: we first do the
1296 * sanity checks (buffer size -fast- and access check-healthy
1297 * pointer); if all of those succeed, then we can alloc the memory
1298 * (expensive operation) needed to copy the data to kernel. Then we do
1299 * the copying without checking the user space area
1300 * (__copy_from_user()).
1301 *
1302 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1303 * it.
1304 *
1305 * sk The sk of the socket
1306 * addrs The pointer to the addresses in user land
1307 * addrssize Size of the addrs buffer
1308 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001309 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001311SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001312 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001313 int addrs_size,
1314 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001315{
1316 int err = 0;
1317 struct sockaddr *kaddrs;
1318
1319 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001320 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001321
1322 if (unlikely(addrs_size <= 0))
1323 return -EINVAL;
1324
1325 /* Check the user passed a healthy pointer. */
1326 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1327 return -EFAULT;
1328
1329 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001330 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001331 if (unlikely(!kaddrs))
1332 return -ENOMEM;
1333
1334 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1335 err = -EFAULT;
1336 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001337 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001338 }
1339
1340 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001341
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342 return err;
1343}
1344
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001345/*
1346 * This is an older interface. It's kept for backward compatibility
1347 * to the option that doesn't provide association id.
1348 */
1349SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1350 struct sockaddr __user *addrs,
1351 int addrs_size)
1352{
1353 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1354}
1355
1356/*
1357 * New interface for the API. The since the API is done with a socket
1358 * option, to make it simple we feed back the association id is as a return
1359 * indication to the call. Error is always negative and association id is
1360 * always positive.
1361 */
1362SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1363 struct sockaddr __user *addrs,
1364 int addrs_size)
1365{
1366 sctp_assoc_t assoc_id = 0;
1367 int err = 0;
1368
1369 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1370
1371 if (err)
1372 return err;
1373 else
1374 return assoc_id;
1375}
1376
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001377/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001378 * New (hopefully final) interface for the API.
1379 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmann19e48382014-02-17 12:11:11 +01001380 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001381 * is that we store the actual length of the address buffer into the
Daniel Borkmann19e48382014-02-17 12:11:11 +01001382 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001383 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001384 */
Daniel Borkmann19e48382014-02-17 12:11:11 +01001385#ifdef CONFIG_COMPAT
1386struct compat_sctp_getaddrs_old {
1387 sctp_assoc_t assoc_id;
1388 s32 addr_num;
1389 compat_uptr_t addrs; /* struct sockaddr * */
1390};
1391#endif
1392
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001393SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1394 char __user *optval,
1395 int __user *optlen)
1396{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001397 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001398 sctp_assoc_t assoc_id = 0;
1399 int err = 0;
1400
Daniel Borkmann19e48382014-02-17 12:11:11 +01001401#ifdef CONFIG_COMPAT
1402 if (is_compat_task()) {
1403 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001404
Daniel Borkmann19e48382014-02-17 12:11:11 +01001405 if (len < sizeof(param32))
1406 return -EINVAL;
1407 if (copy_from_user(&param32, optval, sizeof(param32)))
1408 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001409
Daniel Borkmann19e48382014-02-17 12:11:11 +01001410 param.assoc_id = param32.assoc_id;
1411 param.addr_num = param32.addr_num;
1412 param.addrs = compat_ptr(param32.addrs);
1413 } else
1414#endif
1415 {
1416 if (len < sizeof(param))
1417 return -EINVAL;
1418 if (copy_from_user(&param, optval, sizeof(param)))
1419 return -EFAULT;
1420 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001421
Daniel Borkmann19e48382014-02-17 12:11:11 +01001422 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1423 param.addrs, param.addr_num,
1424 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001425 if (err == 0 || err == -EINPROGRESS) {
1426 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1427 return -EFAULT;
1428 if (put_user(sizeof(assoc_id), optlen))
1429 return -EFAULT;
1430 }
1431
1432 return err;
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/* API 3.1.4 close() - UDP Style Syntax
1436 * Applications use close() to perform graceful shutdown (as described in
1437 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1438 * by a UDP-style socket.
1439 *
1440 * The syntax is
1441 *
1442 * ret = close(int sd);
1443 *
1444 * sd - the socket descriptor of the associations to be closed.
1445 *
1446 * To gracefully shutdown a specific association represented by the
1447 * UDP-style socket, an application should use the sendmsg() call,
1448 * passing no user data, but including the appropriate flag in the
1449 * ancillary data (see Section xxxx).
1450 *
1451 * If sd in the close() call is a branched-off socket representing only
1452 * one association, the shutdown is performed on that association only.
1453 *
1454 * 4.1.6 close() - TCP Style Syntax
1455 *
1456 * Applications use close() to gracefully close down an association.
1457 *
1458 * The syntax is:
1459 *
1460 * int close(int sd);
1461 *
1462 * sd - the socket descriptor of the association to be closed.
1463 *
1464 * After an application calls close() on a socket descriptor, no further
1465 * socket operations will succeed on that descriptor.
1466 *
1467 * API 7.1.4 SO_LINGER
1468 *
1469 * An application using the TCP-style socket can use this option to
1470 * perform the SCTP ABORT primitive. The linger option structure is:
1471 *
1472 * struct linger {
1473 * int l_onoff; // option on/off
1474 * int l_linger; // linger time
1475 * };
1476 *
1477 * To enable the option, set l_onoff to 1. If the l_linger value is set
1478 * to 0, calling close() is the same as the ABORT primitive. If the
1479 * value is set to a negative value, the setsockopt() call will return
1480 * an error. If the value is set to a positive value linger_time, the
1481 * close() can be blocked for at most linger_time ms. If the graceful
1482 * shutdown phase does not finish during this period, close() will
1483 * return but the graceful shutdown phase continues in the system.
1484 */
1485SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1486{
1487 struct sctp_endpoint *ep;
1488 struct sctp_association *asoc;
1489 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001490 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1493
1494 sctp_lock_sock(sk);
1495 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001496 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 ep = sctp_sk(sk)->ep;
1499
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001500 /* Clean up any skbs sitting on the receive queue. */
1501 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1502 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1503
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001504 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 list_for_each_safe(pos, temp, &ep->asocs) {
1506 asoc = list_entry(pos, struct sctp_association, asocs);
1507
1508 if (sctp_style(sk, TCP)) {
1509 /* A closed association can still be in the list if
1510 * it belongs to a TCP-style listening socket that is
1511 * not yet accepted. If so, free it. If not, send an
1512 * ABORT or SHUTDOWN based on the linger options.
1513 */
1514 if (sctp_state(asoc, CLOSED)) {
1515 sctp_unhash_established(asoc);
1516 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001517 continue;
1518 }
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001521 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1522 !skb_queue_empty(&asoc->ulpq.reasm) ||
1523 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001524 struct sctp_chunk *chunk;
1525
1526 chunk = sctp_make_abort_user(asoc, NULL, 0);
1527 if (chunk)
1528 sctp_primitive_ABORT(asoc, chunk);
1529 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 sctp_primitive_SHUTDOWN(asoc, NULL);
1531 }
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* On a TCP-style socket, block for at most linger_time if set. */
1534 if (sctp_style(sk, TCP) && timeout)
1535 sctp_wait_for_close(sk, timeout);
1536
1537 /* This will run the backlog queue. */
1538 sctp_release_sock(sk);
1539
1540 /* Supposedly, no process has access to the socket, but
1541 * the net layers still may.
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03001542 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1543 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03001545 spin_lock_bh(&sctp_globals.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sctp_bh_lock_sock(sk);
1547
1548 /* Hold the sock, since sk_common_release() will put sock_put()
1549 * and we have just a little more cleanup.
1550 */
1551 sock_hold(sk);
1552 sk_common_release(sk);
1553
1554 sctp_bh_unlock_sock(sk);
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03001555 spin_unlock_bh(&sctp_globals.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 sock_put(sk);
1558
1559 SCTP_DBG_OBJCNT_DEC(sock);
1560}
1561
1562/* Handle EPIPE error. */
1563static int sctp_error(struct sock *sk, int flags, int err)
1564{
1565 if (err == -EPIPE)
1566 err = sock_error(sk) ? : -EPIPE;
1567 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1568 send_sig(SIGPIPE, current, 0);
1569 return err;
1570}
1571
1572/* API 3.1.3 sendmsg() - UDP Style Syntax
1573 *
1574 * An application uses sendmsg() and recvmsg() calls to transmit data to
1575 * and receive data from its peer.
1576 *
1577 * ssize_t sendmsg(int socket, const struct msghdr *message,
1578 * int flags);
1579 *
1580 * socket - the socket descriptor of the endpoint.
1581 * message - pointer to the msghdr structure which contains a single
1582 * user message and possibly some ancillary data.
1583 *
1584 * See Section 5 for complete description of the data
1585 * structures.
1586 *
1587 * flags - flags sent or received with the user message, see Section
1588 * 5 for complete description of the flags.
1589 *
1590 * Note: This function could use a rewrite especially when explicit
1591 * connect support comes in.
1592 */
1593/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1594
1595SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1596
1597SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1598 struct msghdr *msg, size_t msg_len)
1599{
1600 struct sctp_sock *sp;
1601 struct sctp_endpoint *ep;
1602 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1603 struct sctp_transport *transport, *chunk_tp;
1604 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001605 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001607 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct sctp_sndrcvinfo *sinfo;
1609 struct sctp_initmsg *sinit;
1610 sctp_assoc_t associd = 0;
1611 sctp_cmsgs_t cmsgs = { NULL };
1612 int err;
1613 sctp_scope_t scope;
1614 long timeo;
1615 __u16 sinfo_flags = 0;
1616 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int msg_flags = msg->msg_flags;
1618
1619 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1620 sk, msg, msg_len);
1621
1622 err = 0;
1623 sp = sctp_sk(sk);
1624 ep = sp->ep;
1625
Frank Filz3f7a87d2005-06-20 13:14:57 -07001626 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* We cannot send a message over a TCP-style listening socket. */
1629 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1630 err = -EPIPE;
1631 goto out_nounlock;
1632 }
1633
1634 /* Parse out the SCTP CMSGs. */
1635 err = sctp_msghdr_parse(msg, &cmsgs);
1636
1637 if (err) {
1638 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1639 goto out_nounlock;
1640 }
1641
1642 /* Fetch the destination address for this packet. This
1643 * address only selects the association--it is not necessarily
1644 * the address we will send to.
1645 * For a peeled-off socket, msg_name is ignored.
1646 */
1647 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1648 int msg_namelen = msg->msg_namelen;
1649
1650 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1651 msg_namelen);
1652 if (err)
1653 return err;
1654
1655 if (msg_namelen > sizeof(to))
1656 msg_namelen = sizeof(to);
1657 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 msg_name = msg->msg_name;
1659 }
1660
1661 sinfo = cmsgs.info;
1662 sinit = cmsgs.init;
1663
1664 /* Did the user specify SNDRCVINFO? */
1665 if (sinfo) {
1666 sinfo_flags = sinfo->sinfo_flags;
1667 associd = sinfo->sinfo_assoc_id;
1668 }
1669
1670 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1671 msg_len, sinfo_flags);
1672
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001673 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1674 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 err = -EINVAL;
1676 goto out_nounlock;
1677 }
1678
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001679 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1680 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1681 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001683 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001684 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1685 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 err = -EINVAL;
1687 goto out_nounlock;
1688 }
1689
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001690 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * specified in msg_name.
1692 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001693 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 err = -EINVAL;
1695 goto out_nounlock;
1696 }
1697
1698 transport = NULL;
1699
1700 SCTP_DEBUG_PRINTK("About to look up association.\n");
1701
1702 sctp_lock_sock(sk);
1703
1704 /* If a msg_name has been specified, assume this is to be used. */
1705 if (msg_name) {
1706 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001707 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!asoc) {
1709 /* If we could not find a matching association on the
1710 * endpoint, make sure that it is not a TCP-style
1711 * socket that already has an association or there is
1712 * no peeled-off association on another socket.
1713 */
1714 if ((sctp_style(sk, TCP) &&
1715 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001716 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 err = -EADDRNOTAVAIL;
1718 goto out_unlock;
1719 }
1720 }
1721 } else {
1722 asoc = sctp_id2assoc(sk, associd);
1723 if (!asoc) {
1724 err = -EPIPE;
1725 goto out_unlock;
1726 }
1727 }
1728
1729 if (asoc) {
1730 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1731
1732 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1733 * socket that has an association in CLOSED state. This can
1734 * happen when an accepted socket has an association that is
1735 * already CLOSED.
1736 */
1737 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1738 err = -EPIPE;
1739 goto out_unlock;
1740 }
1741
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001742 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1744 asoc);
1745 sctp_primitive_SHUTDOWN(asoc, NULL);
1746 err = 0;
1747 goto out_unlock;
1748 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001749 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001750
1751 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1752 if (!chunk) {
1753 err = -ENOMEM;
1754 goto out_unlock;
1755 }
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001758 sctp_primitive_ABORT(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 err = 0;
1760 goto out_unlock;
1761 }
1762 }
1763
1764 /* Do we need to create the association? */
1765 if (!asoc) {
1766 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1767
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001768 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 err = -EINVAL;
1770 goto out_unlock;
1771 }
1772
1773 /* Check for invalid stream against the stream counts,
1774 * either the default or the user specified stream counts.
1775 */
1776 if (sinfo) {
1777 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1778 /* Check against the defaults. */
1779 if (sinfo->sinfo_stream >=
1780 sp->initmsg.sinit_num_ostreams) {
1781 err = -EINVAL;
1782 goto out_unlock;
1783 }
1784 } else {
1785 /* Check against the requested. */
1786 if (sinfo->sinfo_stream >=
1787 sinit->sinit_num_ostreams) {
1788 err = -EINVAL;
1789 goto out_unlock;
1790 }
1791 }
1792 }
1793
1794 /*
1795 * API 3.1.2 bind() - UDP Style Syntax
1796 * If a bind() or sctp_bindx() is not called prior to a
1797 * sendmsg() call that initiates a new association, the
1798 * system picks an ephemeral port and will choose an address
1799 * set equivalent to binding with a wildcard address.
1800 */
1801 if (!ep->base.bind_addr.port) {
1802 if (sctp_autobind(sk)) {
1803 err = -EAGAIN;
1804 goto out_unlock;
1805 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001806 } else {
1807 /*
1808 * If an unprivileged user inherits a one-to-many
1809 * style socket with open associations on a privileged
1810 * port, it MAY be permitted to accept new associations,
1811 * but it SHOULD NOT be permitted to open new
1812 * associations.
1813 */
1814 if (ep->base.bind_addr.port < PROT_SOCK &&
1815 !capable(CAP_NET_BIND_SERVICE)) {
1816 err = -EACCES;
1817 goto out_unlock;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
1821 scope = sctp_scope(&to);
1822 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1823 if (!new_asoc) {
1824 err = -ENOMEM;
1825 goto out_unlock;
1826 }
1827 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001828 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1829 if (err < 0) {
1830 err = -ENOMEM;
1831 goto out_free;
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 /* If the SCTP_INIT ancillary data is specified, set all
1835 * the association init values accordingly.
1836 */
1837 if (sinit) {
1838 if (sinit->sinit_num_ostreams) {
1839 asoc->c.sinit_num_ostreams =
1840 sinit->sinit_num_ostreams;
1841 }
1842 if (sinit->sinit_max_instreams) {
1843 asoc->c.sinit_max_instreams =
1844 sinit->sinit_max_instreams;
1845 }
1846 if (sinit->sinit_max_attempts) {
1847 asoc->max_init_attempts
1848 = sinit->sinit_max_attempts;
1849 }
1850 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001851 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1853 }
1854 }
1855
1856 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001857 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!transport) {
1859 err = -ENOMEM;
1860 goto out_free;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
1864 /* ASSERT: we have a valid association at this point. */
1865 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1866
1867 if (!sinfo) {
1868 /* If the user didn't specify SNDRCVINFO, make up one with
1869 * some defaults.
1870 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001871 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 default_sinfo.sinfo_stream = asoc->default_stream;
1873 default_sinfo.sinfo_flags = asoc->default_flags;
1874 default_sinfo.sinfo_ppid = asoc->default_ppid;
1875 default_sinfo.sinfo_context = asoc->default_context;
1876 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1877 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1878 sinfo = &default_sinfo;
1879 }
1880
1881 /* API 7.1.7, the sndbuf size per association bounds the
1882 * maximum size of data that can be sent in a single send call.
1883 */
1884 if (msg_len > sk->sk_sndbuf) {
1885 err = -EMSGSIZE;
1886 goto out_free;
1887 }
1888
Vlad Yasevich8a479492007-06-07 14:21:05 -04001889 if (asoc->pmtu_pending)
1890 sctp_assoc_pending_pmtu(asoc);
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* If fragmentation is disabled and the message length exceeds the
1893 * association fragmentation point, return EMSGSIZE. The I-D
1894 * does not specify what this error is, but this looks like
1895 * a great fit.
1896 */
1897 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1898 err = -EMSGSIZE;
1899 goto out_free;
1900 }
1901
Joe Perchesafd76142011-05-12 09:19:10 +00001902 /* Check for invalid stream. */
1903 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1904 err = -EINVAL;
1905 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
1908 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1909 if (!sctp_wspace(asoc)) {
1910 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1911 if (err)
1912 goto out_free;
1913 }
1914
1915 /* If an address is passed with the sendto/sendmsg call, it is used
1916 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001917 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
1919 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001920 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001921 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!chunk_tp) {
1923 err = -EINVAL;
1924 goto out_free;
1925 }
1926 } else
1927 chunk_tp = NULL;
1928
1929 /* Auto-connect, if we aren't connected already. */
1930 if (sctp_state(asoc, CLOSED)) {
1931 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1932 if (err < 0)
1933 goto out_free;
1934 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1935 }
1936
1937 /* Break the message into multiple chunks of maximum size. */
1938 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
Tommi Rantaladff343c2012-11-22 03:23:16 +00001939 if (IS_ERR(datamsg)) {
1940 err = PTR_ERR(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto out_free;
1942 }
1943
1944 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001945 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Florian Westphal80445cf2008-03-23 22:47:08 -07001946 sctp_chunk_hold(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /* Do accounting for the write space. */
1949 sctp_set_owner_w(chunk);
1950
1951 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001954 /* Send it to the lower layers. Note: all chunks
1955 * must either fail or succeed. The lower layer
1956 * works that way today. Keep it that way or this
1957 * breaks.
1958 */
1959 err = sctp_primitive_SEND(asoc, datamsg);
1960 /* Did the lower layer accept the chunk? */
1961 if (err)
1962 sctp_datamsg_free(datamsg);
1963 else
1964 sctp_datamsg_put(datamsg);
1965
1966 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (err)
1969 goto out_free;
1970 else
1971 err = msg_len;
1972
1973 /* If we are already past ASSOCIATE, the lower
1974 * layers are responsible for association cleanup.
1975 */
1976 goto out_unlock;
1977
1978out_free:
Neil Horman2936d352012-07-16 09:13:51 +00001979 if (new_asoc) {
1980 sctp_unhash_established(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 sctp_association_free(asoc);
Neil Horman2936d352012-07-16 09:13:51 +00001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983out_unlock:
1984 sctp_release_sock(sk);
1985
1986out_nounlock:
1987 return sctp_error(sk, msg_flags, err);
1988
1989#if 0
1990do_sock_err:
1991 if (msg_len)
1992 err = msg_len;
1993 else
1994 err = sock_error(sk);
1995 goto out;
1996
1997do_interrupted:
1998 if (msg_len)
1999 err = msg_len;
2000 goto out;
2001#endif /* 0 */
2002}
2003
2004/* This is an extended version of skb_pull() that removes the data from the
2005 * start of a skb even when data is spread across the list of skb's in the
2006 * frag_list. len specifies the total amount of data that needs to be removed.
2007 * when 'len' bytes could be removed from the skb, it returns 0.
2008 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2009 * could not be removed.
2010 */
2011static int sctp_skb_pull(struct sk_buff *skb, int len)
2012{
2013 struct sk_buff *list;
2014 int skb_len = skb_headlen(skb);
2015 int rlen;
2016
2017 if (len <= skb_len) {
2018 __skb_pull(skb, len);
2019 return 0;
2020 }
2021 len -= skb_len;
2022 __skb_pull(skb, skb_len);
2023
David S. Miller1b003be2009-06-09 00:22:35 -07002024 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 rlen = sctp_skb_pull(list, len);
2026 skb->len -= (len-rlen);
2027 skb->data_len -= (len-rlen);
2028
2029 if (!rlen)
2030 return 0;
2031
2032 len = rlen;
2033 }
2034
2035 return len;
2036}
2037
2038/* API 3.1.3 recvmsg() - UDP Style Syntax
2039 *
2040 * ssize_t recvmsg(int socket, struct msghdr *message,
2041 * int flags);
2042 *
2043 * socket - the socket descriptor of the endpoint.
2044 * message - pointer to the msghdr structure which contains a single
2045 * user message and possibly some ancillary data.
2046 *
2047 * See Section 5 for complete description of the data
2048 * structures.
2049 *
2050 * flags - flags sent or received with the user message, see Section
2051 * 5 for complete description of the flags.
2052 */
2053static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2054
2055SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2056 struct msghdr *msg, size_t len, int noblock,
2057 int flags, int *addr_len)
2058{
2059 struct sctp_ulpevent *event = NULL;
2060 struct sctp_sock *sp = sctp_sk(sk);
2061 struct sk_buff *skb;
2062 int copied;
2063 int err = 0;
2064 int skb_len;
2065
2066 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2067 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2068 "len", len, "knoblauch", noblock,
2069 "flags", flags, "addr_len", addr_len);
2070
2071 sctp_lock_sock(sk);
2072
2073 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2074 err = -ENOTCONN;
2075 goto out;
2076 }
2077
2078 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2079 if (!skb)
2080 goto out;
2081
2082 /* Get the total length of the skb including any skb's in the
2083 * frag_list.
2084 */
2085 skb_len = skb->len;
2086
2087 copied = skb_len;
2088 if (copied > len)
2089 copied = len;
2090
2091 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2092
2093 event = sctp_skb2event(skb);
2094
2095 if (err)
2096 goto out_free;
2097
Neil Horman3b885782009-10-12 13:26:31 -07002098 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (sctp_ulpevent_is_notification(event)) {
2100 msg->msg_flags |= MSG_NOTIFICATION;
2101 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2102 } else {
2103 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2104 }
2105
2106 /* Check if we allow SCTP_SNDRCVINFO. */
2107 if (sp->subscribe.sctp_data_io_event)
2108 sctp_ulpevent_read_sndrcvinfo(event, msg);
2109#if 0
2110 /* FIXME: we should be calling IP/IPv6 layers. */
2111 if (sk->sk_protinfo.af_inet.cmsg_flags)
2112 ip_cmsg_recv(msg, skb);
2113#endif
2114
2115 err = copied;
2116
2117 /* If skb's length exceeds the user's buffer, update the skb and
2118 * push it back to the receive_queue so that the next call to
2119 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2120 */
2121 if (skb_len > copied) {
2122 msg->msg_flags &= ~MSG_EOR;
2123 if (flags & MSG_PEEK)
2124 goto out_free;
2125 sctp_skb_pull(skb, copied);
2126 skb_queue_head(&sk->sk_receive_queue, skb);
2127
2128 /* When only partial message is copied to the user, increase
2129 * rwnd by that amount. If all the data in the skb is read,
2130 * rwnd is updated when the event is freed.
2131 */
Vlad Yasevich0eca8fe2008-01-11 10:12:56 -05002132 if (!sctp_ulpevent_is_notification(event))
2133 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto out;
2135 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2136 (event->msg_flags & MSG_EOR))
2137 msg->msg_flags |= MSG_EOR;
2138 else
2139 msg->msg_flags &= ~MSG_EOR;
2140
2141out_free:
2142 if (flags & MSG_PEEK) {
2143 /* Release the skb reference acquired after peeking the skb in
2144 * sctp_skb_recv_datagram().
2145 */
2146 kfree_skb(skb);
2147 } else {
2148 /* Free the event which includes releasing the reference to
2149 * the owner of the skb, freeing the skb and updating the
2150 * rwnd.
2151 */
2152 sctp_ulpevent_free(event);
2153 }
2154out:
2155 sctp_release_sock(sk);
2156 return err;
2157}
2158
2159/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2160 *
2161 * This option is a on/off flag. If enabled no SCTP message
2162 * fragmentation will be performed. Instead if a message being sent
2163 * exceeds the current PMTU size, the message will NOT be sent and
2164 * instead a error will be indicated to the user.
2165 */
2166static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002167 char __user *optval,
2168 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 int val;
2171
2172 if (optlen < sizeof(int))
2173 return -EINVAL;
2174
2175 if (get_user(val, (int __user *)optval))
2176 return -EFAULT;
2177
2178 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2179
2180 return 0;
2181}
2182
2183static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002184 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Wei Yongjun94912302011-07-02 09:28:04 +00002186 struct sctp_association *asoc;
2187 struct sctp_ulpevent *event;
2188
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002189 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -EINVAL;
2191 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2192 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002193
2194 /*
2195 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2196 * if there is no data to be sent or retransmit, the stack will
2197 * immediately send up this notification.
2198 */
2199 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2200 &sctp_sk(sk)->subscribe)) {
2201 asoc = sctp_id2assoc(sk, 0);
2202
2203 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2204 event = sctp_ulpevent_make_sender_dry_event(asoc,
2205 GFP_ATOMIC);
2206 if (!event)
2207 return -ENOMEM;
2208
2209 sctp_ulpq_tail_event(&asoc->ulpq, event);
2210 }
2211 }
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
2215
2216/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2217 *
2218 * This socket option is applicable to the UDP-style socket only. When
2219 * set it will cause associations that are idle for more than the
2220 * specified number of seconds to automatically close. An association
2221 * being idle is defined an association that has NOT sent or received
2222 * user data. The special value of '0' indicates that no automatic
2223 * close of any associations should be performed. The option expects an
2224 * integer defining the number of seconds of idle time before an
2225 * association is closed.
2226 */
2227static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002228 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 struct sctp_sock *sp = sctp_sk(sk);
2231
2232 /* Applicable to UDP-style socket only */
2233 if (sctp_style(sk, TCP))
2234 return -EOPNOTSUPP;
2235 if (optlen != sizeof(int))
2236 return -EINVAL;
2237 if (copy_from_user(&sp->autoclose, optval, optlen))
2238 return -EFAULT;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
2241}
2242
2243/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2244 *
2245 * Applications can enable or disable heartbeats for any peer address of
2246 * an association, modify an address's heartbeat interval, force a
2247 * heartbeat to be sent immediately, and adjust the address's maximum
2248 * number of retransmissions sent before an address is considered
2249 * unreachable. The following structure is used to access and modify an
2250 * address's parameters:
2251 *
2252 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002253 * sctp_assoc_t spp_assoc_id;
2254 * struct sockaddr_storage spp_address;
2255 * uint32_t spp_hbinterval;
2256 * uint16_t spp_pathmaxrxt;
2257 * uint32_t spp_pathmtu;
2258 * uint32_t spp_sackdelay;
2259 * uint32_t spp_flags;
2260 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002262 * spp_assoc_id - (one-to-many style socket) This is filled in the
2263 * application, and identifies the association for
2264 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * spp_address - This specifies which address is of interest.
2266 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002267 * in milliseconds. If a value of zero
2268 * is present in this field then no changes are to
2269 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * spp_pathmaxrxt - This contains the maximum number of
2271 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002272 * considered unreachable. If a value of zero
2273 * is present in this field then no changes are to
2274 * be made to this parameter.
2275 * spp_pathmtu - When Path MTU discovery is disabled the value
2276 * specified here will be the "fixed" path mtu.
2277 * Note that if the spp_address field is empty
2278 * then all associations on this address will
2279 * have this fixed path mtu set upon them.
2280 *
2281 * spp_sackdelay - When delayed sack is enabled, this value specifies
2282 * the number of milliseconds that sacks will be delayed
2283 * for. This value will apply to all addresses of an
2284 * association if the spp_address field is empty. Note
2285 * also, that if delayed sack is enabled and this
2286 * value is set to 0, no change is made to the last
2287 * recorded delayed sack timer value.
2288 *
2289 * spp_flags - These flags are used to control various features
2290 * on an association. The flag field may contain
2291 * zero or more of the following options.
2292 *
2293 * SPP_HB_ENABLE - Enable heartbeats on the
2294 * specified address. Note that if the address
2295 * field is empty all addresses for the association
2296 * have heartbeats enabled upon them.
2297 *
2298 * SPP_HB_DISABLE - Disable heartbeats on the
2299 * speicifed address. Note that if the address
2300 * field is empty all addresses for the association
2301 * will have their heartbeats disabled. Note also
2302 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2303 * mutually exclusive, only one of these two should
2304 * be specified. Enabling both fields will have
2305 * undetermined results.
2306 *
2307 * SPP_HB_DEMAND - Request a user initiated heartbeat
2308 * to be made immediately.
2309 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002310 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2311 * heartbeat delayis to be set to the value of 0
2312 * milliseconds.
2313 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002314 * SPP_PMTUD_ENABLE - This field will enable PMTU
2315 * discovery upon the specified address. Note that
2316 * if the address feild is empty then all addresses
2317 * on the association are effected.
2318 *
2319 * SPP_PMTUD_DISABLE - This field will disable PMTU
2320 * discovery upon the specified address. Note that
2321 * if the address feild is empty then all addresses
2322 * on the association are effected. Not also that
2323 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2324 * exclusive. Enabling both will have undetermined
2325 * results.
2326 *
2327 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2328 * on delayed sack. The time specified in spp_sackdelay
2329 * is used to specify the sack delay for this address. Note
2330 * that if spp_address is empty then all addresses will
2331 * enable delayed sack and take on the sack delay
2332 * value specified in spp_sackdelay.
2333 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2334 * off delayed sack. If the spp_address field is blank then
2335 * delayed sack is disabled for the entire association. Note
2336 * also that this field is mutually exclusive to
2337 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2338 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 */
Adrian Bunk16164362006-09-18 00:40:38 -07002340static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2341 struct sctp_transport *trans,
2342 struct sctp_association *asoc,
2343 struct sctp_sock *sp,
2344 int hb_change,
2345 int pmtud_change,
2346 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int error;
2349
Frank Filz52ccb8e2005-12-22 11:36:46 -08002350 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2352 if (error)
2353 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002356 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2357 * this field is ignored. Note also that a value of zero indicates
2358 * the current setting should be left unchanged.
2359 */
2360 if (params->spp_flags & SPP_HB_ENABLE) {
2361
2362 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2363 * set. This lets us use 0 value when this flag
2364 * is set.
2365 */
2366 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2367 params->spp_hbinterval = 0;
2368
2369 if (params->spp_hbinterval ||
2370 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2371 if (trans) {
2372 trans->hbinterval =
2373 msecs_to_jiffies(params->spp_hbinterval);
2374 } else if (asoc) {
2375 asoc->hbinterval =
2376 msecs_to_jiffies(params->spp_hbinterval);
2377 } else {
2378 sp->hbinterval = params->spp_hbinterval;
2379 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002380 }
2381 }
2382
2383 if (hb_change) {
2384 if (trans) {
2385 trans->param_flags =
2386 (trans->param_flags & ~SPP_HB) | hb_change;
2387 } else if (asoc) {
2388 asoc->param_flags =
2389 (asoc->param_flags & ~SPP_HB) | hb_change;
2390 } else {
2391 sp->param_flags =
2392 (sp->param_flags & ~SPP_HB) | hb_change;
2393 }
2394 }
2395
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002396 /* When Path MTU discovery is disabled the value specified here will
2397 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2398 * include the flag SPP_PMTUD_DISABLE for this field to have any
2399 * effect).
2400 */
2401 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002402 if (trans) {
2403 trans->pathmtu = params->spp_pathmtu;
2404 sctp_assoc_sync_pmtu(asoc);
2405 } else if (asoc) {
2406 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002407 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002408 } else {
2409 sp->pathmtu = params->spp_pathmtu;
2410 }
2411 }
2412
2413 if (pmtud_change) {
2414 if (trans) {
2415 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2416 (params->spp_flags & SPP_PMTUD_ENABLE);
2417 trans->param_flags =
2418 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2419 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002420 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Frank Filz52ccb8e2005-12-22 11:36:46 -08002421 sctp_assoc_sync_pmtu(asoc);
2422 }
2423 } else if (asoc) {
2424 asoc->param_flags =
2425 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2426 } else {
2427 sp->param_flags =
2428 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2429 }
2430 }
2431
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002432 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2433 * value of this field is ignored. Note also that a value of zero
2434 * indicates the current setting should be left unchanged.
2435 */
2436 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002437 if (trans) {
2438 trans->sackdelay =
2439 msecs_to_jiffies(params->spp_sackdelay);
2440 } else if (asoc) {
2441 asoc->sackdelay =
2442 msecs_to_jiffies(params->spp_sackdelay);
2443 } else {
2444 sp->sackdelay = params->spp_sackdelay;
2445 }
2446 }
2447
2448 if (sackdelay_change) {
2449 if (trans) {
2450 trans->param_flags =
2451 (trans->param_flags & ~SPP_SACKDELAY) |
2452 sackdelay_change;
2453 } else if (asoc) {
2454 asoc->param_flags =
2455 (asoc->param_flags & ~SPP_SACKDELAY) |
2456 sackdelay_change;
2457 } else {
2458 sp->param_flags =
2459 (sp->param_flags & ~SPP_SACKDELAY) |
2460 sackdelay_change;
2461 }
2462 }
2463
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002464 /* Note that a value of zero indicates the current setting should be
2465 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002466 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002467 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002468 if (trans) {
2469 trans->pathmaxrxt = params->spp_pathmaxrxt;
2470 } else if (asoc) {
2471 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2472 } else {
2473 sp->pathmaxrxt = params->spp_pathmaxrxt;
2474 }
2475 }
2476
2477 return 0;
2478}
2479
2480static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002481 char __user *optval,
2482 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002483{
2484 struct sctp_paddrparams params;
2485 struct sctp_transport *trans = NULL;
2486 struct sctp_association *asoc = NULL;
2487 struct sctp_sock *sp = sctp_sk(sk);
2488 int error;
2489 int hb_change, pmtud_change, sackdelay_change;
2490
2491 if (optlen != sizeof(struct sctp_paddrparams))
2492 return - EINVAL;
2493
2494 if (copy_from_user(&params, optval, optlen))
2495 return -EFAULT;
2496
2497 /* Validate flags and value parameters. */
2498 hb_change = params.spp_flags & SPP_HB;
2499 pmtud_change = params.spp_flags & SPP_PMTUD;
2500 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2501
2502 if (hb_change == SPP_HB ||
2503 pmtud_change == SPP_PMTUD ||
2504 sackdelay_change == SPP_SACKDELAY ||
2505 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002506 (params.spp_pathmtu &&
2507 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002508 return -EINVAL;
2509
2510 /* If an address other than INADDR_ANY is specified, and
2511 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04002513 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002514 trans = sctp_addr_id2transport(sk, &params.spp_address,
2515 params.spp_assoc_id);
2516 if (!trans)
2517 return -EINVAL;
2518 }
2519
2520 /* Get association, if assoc_id != 0 and the socket is a one
2521 * to many style socket, and an association was not found, then
2522 * the id was invalid.
2523 */
2524 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2525 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2526 return -EINVAL;
2527
2528 /* Heartbeat demand can only be sent on a transport or
2529 * association, but not a socket.
2530 */
2531 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2532 return -EINVAL;
2533
2534 /* Process parameters. */
2535 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2536 hb_change, pmtud_change,
2537 sackdelay_change);
2538
2539 if (error)
2540 return error;
2541
2542 /* If changes are for association, also apply parameters to each
2543 * transport.
2544 */
2545 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002546 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2547 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002548 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2549 hb_change, pmtud_change,
2550 sackdelay_change);
2551 }
2552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 return 0;
2555}
2556
Wei Yongjund364d922008-05-09 15:13:26 -07002557/*
2558 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002559 *
Wei Yongjund364d922008-05-09 15:13:26 -07002560 * This option will effect the way delayed acks are performed. This
2561 * option allows you to get or set the delayed ack time, in
2562 * milliseconds. It also allows changing the delayed ack frequency.
2563 * Changing the frequency to 1 disables the delayed sack algorithm. If
2564 * the assoc_id is 0, then this sets or gets the endpoints default
2565 * values. If the assoc_id field is non-zero, then the set or get
2566 * effects the specified association for the one to many model (the
2567 * assoc_id field is ignored by the one to one model). Note that if
2568 * sack_delay or sack_freq are 0 when setting this option, then the
2569 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002570 *
Wei Yongjund364d922008-05-09 15:13:26 -07002571 * struct sctp_sack_info {
2572 * sctp_assoc_t sack_assoc_id;
2573 * uint32_t sack_delay;
2574 * uint32_t sack_freq;
2575 * };
Frank Filz77086102005-12-22 11:37:30 -08002576 *
Wei Yongjund364d922008-05-09 15:13:26 -07002577 * sack_assoc_id - This parameter, indicates which association the user
2578 * is performing an action upon. Note that if this field's value is
2579 * zero then the endpoints default value is changed (effecting future
2580 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002581 *
Wei Yongjund364d922008-05-09 15:13:26 -07002582 * sack_delay - This parameter contains the number of milliseconds that
2583 * the user is requesting the delayed ACK timer be set to. Note that
2584 * this value is defined in the standard to be between 200 and 500
2585 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002586 *
Wei Yongjund364d922008-05-09 15:13:26 -07002587 * sack_freq - This parameter contains the number of packets that must
2588 * be received before a sack is sent without waiting for the delay
2589 * timer to expire. The default value for this is 2, setting this
2590 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002591 */
2592
Wei Yongjund364d922008-05-09 15:13:26 -07002593static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002594 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002595{
Wei Yongjund364d922008-05-09 15:13:26 -07002596 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002597 struct sctp_transport *trans = NULL;
2598 struct sctp_association *asoc = NULL;
2599 struct sctp_sock *sp = sctp_sk(sk);
2600
Wei Yongjund364d922008-05-09 15:13:26 -07002601 if (optlen == sizeof(struct sctp_sack_info)) {
2602 if (copy_from_user(&params, optval, optlen))
2603 return -EFAULT;
2604
2605 if (params.sack_delay == 0 && params.sack_freq == 0)
2606 return 0;
2607 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00002608 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2609 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07002610 if (copy_from_user(&params, optval, optlen))
2611 return -EFAULT;
2612
2613 if (params.sack_delay == 0)
2614 params.sack_freq = 1;
2615 else
2616 params.sack_freq = 0;
2617 } else
Frank Filz77086102005-12-22 11:37:30 -08002618 return - EINVAL;
2619
Frank Filz77086102005-12-22 11:37:30 -08002620 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002621 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002622 return -EINVAL;
2623
Wei Yongjund364d922008-05-09 15:13:26 -07002624 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002625 * to many style socket, and an association was not found, then
2626 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002627 */
Wei Yongjund364d922008-05-09 15:13:26 -07002628 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2629 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002630 return -EINVAL;
2631
Wei Yongjund364d922008-05-09 15:13:26 -07002632 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002633 if (asoc) {
2634 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002635 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002636 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002637 (asoc->param_flags & ~SPP_SACKDELAY) |
2638 SPP_SACKDELAY_ENABLE;
2639 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002640 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002641 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002642 (sp->param_flags & ~SPP_SACKDELAY) |
2643 SPP_SACKDELAY_ENABLE;
2644 }
Wei Yongjund364d922008-05-09 15:13:26 -07002645 }
2646
2647 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002648 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002649 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002650 (asoc->param_flags & ~SPP_SACKDELAY) |
2651 SPP_SACKDELAY_DISABLE;
2652 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002653 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002654 (sp->param_flags & ~SPP_SACKDELAY) |
2655 SPP_SACKDELAY_DISABLE;
2656 }
Wei Yongjund364d922008-05-09 15:13:26 -07002657 } else if (params.sack_freq > 1) {
2658 if (asoc) {
2659 asoc->sackfreq = params.sack_freq;
2660 asoc->param_flags =
2661 (asoc->param_flags & ~SPP_SACKDELAY) |
2662 SPP_SACKDELAY_ENABLE;
2663 } else {
2664 sp->sackfreq = params.sack_freq;
2665 sp->param_flags =
2666 (sp->param_flags & ~SPP_SACKDELAY) |
2667 SPP_SACKDELAY_ENABLE;
2668 }
Frank Filz77086102005-12-22 11:37:30 -08002669 }
2670
2671 /* If change is for association, also apply to each transport. */
2672 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002673 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2674 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002675 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002676 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002677 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002678 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002679 (trans->param_flags & ~SPP_SACKDELAY) |
2680 SPP_SACKDELAY_ENABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002681 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002682 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002683 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002684 (trans->param_flags & ~SPP_SACKDELAY) |
2685 SPP_SACKDELAY_DISABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002686 } else if (params.sack_freq > 1) {
2687 trans->sackfreq = params.sack_freq;
2688 trans->param_flags =
2689 (trans->param_flags & ~SPP_SACKDELAY) |
2690 SPP_SACKDELAY_ENABLE;
Frank Filz77086102005-12-22 11:37:30 -08002691 }
2692 }
2693 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002694
Frank Filz77086102005-12-22 11:37:30 -08002695 return 0;
2696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2699 *
2700 * Applications can specify protocol parameters for the default association
2701 * initialization. The option name argument to setsockopt() and getsockopt()
2702 * is SCTP_INITMSG.
2703 *
2704 * Setting initialization parameters is effective only on an unconnected
2705 * socket (for UDP-style sockets only future associations are effected
2706 * by the change). With TCP-style sockets, this option is inherited by
2707 * sockets derived from a listener socket.
2708 */
David S. Millerb7058842009-09-30 16:12:20 -07002709static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct sctp_initmsg sinit;
2712 struct sctp_sock *sp = sctp_sk(sk);
2713
2714 if (optlen != sizeof(struct sctp_initmsg))
2715 return -EINVAL;
2716 if (copy_from_user(&sinit, optval, optlen))
2717 return -EFAULT;
2718
2719 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002720 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002722 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002724 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002726 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 return 0;
2729}
2730
2731/*
2732 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2733 *
2734 * Applications that wish to use the sendto() system call may wish to
2735 * specify a default set of parameters that would normally be supplied
2736 * through the inclusion of ancillary data. This socket option allows
2737 * such an application to set the default sctp_sndrcvinfo structure.
2738 * The application that wishes to use this socket option simply passes
2739 * in to this call the sctp_sndrcvinfo structure defined in Section
2740 * 5.2.2) The input parameters accepted by this call include
2741 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2742 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2743 * to this call if the caller is using the UDP model.
2744 */
2745static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002746 char __user *optval,
2747 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct sctp_sndrcvinfo info;
2750 struct sctp_association *asoc;
2751 struct sctp_sock *sp = sctp_sk(sk);
2752
2753 if (optlen != sizeof(struct sctp_sndrcvinfo))
2754 return -EINVAL;
2755 if (copy_from_user(&info, optval, optlen))
2756 return -EFAULT;
2757
2758 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2759 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2760 return -EINVAL;
2761
2762 if (asoc) {
2763 asoc->default_stream = info.sinfo_stream;
2764 asoc->default_flags = info.sinfo_flags;
2765 asoc->default_ppid = info.sinfo_ppid;
2766 asoc->default_context = info.sinfo_context;
2767 asoc->default_timetolive = info.sinfo_timetolive;
2768 } else {
2769 sp->default_stream = info.sinfo_stream;
2770 sp->default_flags = info.sinfo_flags;
2771 sp->default_ppid = info.sinfo_ppid;
2772 sp->default_context = info.sinfo_context;
2773 sp->default_timetolive = info.sinfo_timetolive;
2774 }
2775
2776 return 0;
2777}
2778
2779/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2780 *
2781 * Requests that the local SCTP stack use the enclosed peer address as
2782 * the association primary. The enclosed address must be one of the
2783 * association peer's addresses.
2784 */
2785static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002786 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct sctp_prim prim;
2789 struct sctp_transport *trans;
2790
2791 if (optlen != sizeof(struct sctp_prim))
2792 return -EINVAL;
2793
2794 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2795 return -EFAULT;
2796
2797 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2798 if (!trans)
2799 return -EINVAL;
2800
2801 sctp_assoc_set_primary(trans->asoc, trans);
2802
2803 return 0;
2804}
2805
2806/*
2807 * 7.1.5 SCTP_NODELAY
2808 *
2809 * Turn on/off any Nagle-like algorithm. This means that packets are
2810 * generally sent as soon as possible and no unnecessary delays are
2811 * introduced, at the cost of more packets in the network. Expects an
2812 * integer boolean flag.
2813 */
2814static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002815 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 int val;
2818
2819 if (optlen < sizeof(int))
2820 return -EINVAL;
2821 if (get_user(val, (int __user *)optval))
2822 return -EFAULT;
2823
2824 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2825 return 0;
2826}
2827
2828/*
2829 *
2830 * 7.1.1 SCTP_RTOINFO
2831 *
2832 * The protocol parameters used to initialize and bound retransmission
2833 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2834 * and modify these parameters.
2835 * All parameters are time values, in milliseconds. A value of 0, when
2836 * modifying the parameters, indicates that the current value should not
2837 * be changed.
2838 *
2839 */
David S. Millerb7058842009-09-30 16:12:20 -07002840static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 struct sctp_rtoinfo rtoinfo;
2843 struct sctp_association *asoc;
2844
2845 if (optlen != sizeof (struct sctp_rtoinfo))
2846 return -EINVAL;
2847
2848 if (copy_from_user(&rtoinfo, optval, optlen))
2849 return -EFAULT;
2850
2851 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2852
2853 /* Set the values to the specific association */
2854 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2855 return -EINVAL;
2856
2857 if (asoc) {
2858 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002859 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 msecs_to_jiffies(rtoinfo.srto_initial);
2861 if (rtoinfo.srto_max != 0)
2862 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2863 if (rtoinfo.srto_min != 0)
2864 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2865 } else {
2866 /* If there is no association or the association-id = 0
2867 * set the values to the endpoint.
2868 */
2869 struct sctp_sock *sp = sctp_sk(sk);
2870
2871 if (rtoinfo.srto_initial != 0)
2872 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2873 if (rtoinfo.srto_max != 0)
2874 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2875 if (rtoinfo.srto_min != 0)
2876 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2877 }
2878
2879 return 0;
2880}
2881
2882/*
2883 *
2884 * 7.1.2 SCTP_ASSOCINFO
2885 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002886 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 * of the association.
2888 * Returns an error if the new association retransmission value is
2889 * greater than the sum of the retransmission value of the peer.
2890 * See [SCTP] for more information.
2891 *
2892 */
David S. Millerb7058842009-09-30 16:12:20 -07002893static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
2895
2896 struct sctp_assocparams assocparams;
2897 struct sctp_association *asoc;
2898
2899 if (optlen != sizeof(struct sctp_assocparams))
2900 return -EINVAL;
2901 if (copy_from_user(&assocparams, optval, optlen))
2902 return -EFAULT;
2903
2904 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2905
2906 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2907 return -EINVAL;
2908
2909 /* Set the values to the specific association */
2910 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002911 if (assocparams.sasoc_asocmaxrxt != 0) {
2912 __u32 path_sum = 0;
2913 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002914 struct sctp_transport *peer_addr;
2915
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002916 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2917 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002918 path_sum += peer_addr->pathmaxrxt;
2919 paths++;
2920 }
2921
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002922 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002923 * one path/transport. We do this because path
2924 * retransmissions are only counted when we have more
2925 * then one path.
2926 */
2927 if (paths > 1 &&
2928 assocparams.sasoc_asocmaxrxt > path_sum)
2929 return -EINVAL;
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002932 }
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (assocparams.sasoc_cookie_life != 0) {
2935 asoc->cookie_life.tv_sec =
2936 assocparams.sasoc_cookie_life / 1000;
2937 asoc->cookie_life.tv_usec =
2938 (assocparams.sasoc_cookie_life % 1000)
2939 * 1000;
2940 }
2941 } else {
2942 /* Set the values to the endpoint */
2943 struct sctp_sock *sp = sctp_sk(sk);
2944
2945 if (assocparams.sasoc_asocmaxrxt != 0)
2946 sp->assocparams.sasoc_asocmaxrxt =
2947 assocparams.sasoc_asocmaxrxt;
2948 if (assocparams.sasoc_cookie_life != 0)
2949 sp->assocparams.sasoc_cookie_life =
2950 assocparams.sasoc_cookie_life;
2951 }
2952 return 0;
2953}
2954
2955/*
2956 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2957 *
2958 * This socket option is a boolean flag which turns on or off mapped V4
2959 * addresses. If this option is turned on and the socket is type
2960 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2961 * If this option is turned off, then no mapping will be done of V4
2962 * addresses and a user will receive both PF_INET6 and PF_INET type
2963 * addresses on the socket.
2964 */
David S. Millerb7058842009-09-30 16:12:20 -07002965static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
2967 int val;
2968 struct sctp_sock *sp = sctp_sk(sk);
2969
2970 if (optlen < sizeof(int))
2971 return -EINVAL;
2972 if (get_user(val, (int __user *)optval))
2973 return -EFAULT;
2974 if (val)
2975 sp->v4mapped = 1;
2976 else
2977 sp->v4mapped = 0;
2978
2979 return 0;
2980}
2981
2982/*
Wei Yongjune89c2092008-12-25 16:54:58 -08002983 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2984 * This option will get or set the maximum size to put in any outgoing
2985 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 * fragmented by SCTP into the specified size. Note that the underlying
2987 * SCTP implementation may fragment into smaller sized chunks when the
2988 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08002989 * the user. The default value for this option is '0' which indicates
2990 * the user is NOT limiting fragmentation and only the PMTU will effect
2991 * SCTP's choice of DATA chunk size. Note also that values set larger
2992 * than the maximum size of an IP datagram will effectively let SCTP
2993 * control fragmentation (i.e. the same as setting this option to 0).
2994 *
2995 * The following structure is used to access and modify this parameter:
2996 *
2997 * struct sctp_assoc_value {
2998 * sctp_assoc_t assoc_id;
2999 * uint32_t assoc_value;
3000 * };
3001 *
3002 * assoc_id: This parameter is ignored for one-to-one style sockets.
3003 * For one-to-many style sockets this parameter indicates which
3004 * association the user is performing an action upon. Note that if
3005 * this field's value is zero then the endpoints default value is
3006 * changed (effecting future associations only).
3007 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
David S. Millerb7058842009-09-30 16:12:20 -07003009static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Wei Yongjune89c2092008-12-25 16:54:58 -08003011 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct sctp_sock *sp = sctp_sk(sk);
3014 int val;
3015
Wei Yongjune89c2092008-12-25 16:54:58 -08003016 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00003017 pr_warn("Use of int in maxseg socket option deprecated\n");
3018 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08003019 if (copy_from_user(&val, optval, optlen))
3020 return -EFAULT;
3021 params.assoc_id = 0;
3022 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3023 if (copy_from_user(&params, optval, optlen))
3024 return -EFAULT;
3025 val = params.assoc_value;
3026 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003028
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07003029 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Wei Yongjune89c2092008-12-25 16:54:58 -08003032 asoc = sctp_id2assoc(sk, params.assoc_id);
3033 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3034 return -EINVAL;
3035
3036 if (asoc) {
3037 if (val == 0) {
3038 val = asoc->pathmtu;
3039 val -= sp->pf->af->net_header_len;
3040 val -= sizeof(struct sctphdr) +
3041 sizeof(struct sctp_data_chunk);
3042 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003043 asoc->user_frag = val;
3044 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003045 } else {
3046 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048
3049 return 0;
3050}
3051
3052
3053/*
3054 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3055 *
3056 * Requests that the peer mark the enclosed address as the association
3057 * primary. The enclosed address must be one of the association's
3058 * locally bound addresses. The following structure is used to make a
3059 * set primary request:
3060 */
3061static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003062 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct sctp_association *asoc = NULL;
3066 struct sctp_setpeerprim prim;
3067 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003068 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 int err;
3070
3071 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 if (!sctp_addip_enable)
3074 return -EPERM;
3075
3076 if (optlen != sizeof(struct sctp_setpeerprim))
3077 return -EINVAL;
3078
3079 if (copy_from_user(&prim, optval, optlen))
3080 return -EFAULT;
3081
3082 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003083 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return -EINVAL;
3085
3086 if (!asoc->peer.asconf_capable)
3087 return -EPERM;
3088
3089 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3090 return -EPERM;
3091
3092 if (!sctp_state(asoc, ESTABLISHED))
3093 return -ENOTCONN;
3094
Wei Yongjun40a01032010-12-07 17:11:09 +00003095 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3096 if (!af)
3097 return -EINVAL;
3098
3099 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3100 return -EADDRNOTAVAIL;
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3103 return -EADDRNOTAVAIL;
3104
3105 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3106 chunk = sctp_make_asconf_set_prim(asoc,
3107 (union sctp_addr *)&prim.sspp_addr);
3108 if (!chunk)
3109 return -ENOMEM;
3110
3111 err = sctp_send_asconf(asoc, chunk);
3112
3113 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3114
3115 return err;
3116}
3117
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003118static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003119 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003121 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003123 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003125 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return -EFAULT;
3127
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003128 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 return 0;
3131}
3132
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003133/*
3134 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3135 *
3136 * The context field in the sctp_sndrcvinfo structure is normally only
3137 * used when a failed message is retrieved holding the value that was
3138 * sent down on the actual send call. This option allows the setting of
3139 * a default context on an association basis that will be received on
3140 * reading messages from the peer. This is especially helpful in the
3141 * one-2-many model for an application to keep some reference to an
3142 * internal state machine that is processing messages on the
3143 * association. Note that the setting of this value only effects
3144 * received messages from the peer and does not effect the value that is
3145 * saved with outbound messages.
3146 */
3147static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003148 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003149{
3150 struct sctp_assoc_value params;
3151 struct sctp_sock *sp;
3152 struct sctp_association *asoc;
3153
3154 if (optlen != sizeof(struct sctp_assoc_value))
3155 return -EINVAL;
3156 if (copy_from_user(&params, optval, optlen))
3157 return -EFAULT;
3158
3159 sp = sctp_sk(sk);
3160
3161 if (params.assoc_id != 0) {
3162 asoc = sctp_id2assoc(sk, params.assoc_id);
3163 if (!asoc)
3164 return -EINVAL;
3165 asoc->default_rcv_context = params.assoc_value;
3166 } else {
3167 sp->default_rcv_context = params.assoc_value;
3168 }
3169
3170 return 0;
3171}
3172
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003173/*
3174 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3175 *
3176 * This options will at a minimum specify if the implementation is doing
3177 * fragmented interleave. Fragmented interleave, for a one to many
3178 * socket, is when subsequent calls to receive a message may return
3179 * parts of messages from different associations. Some implementations
3180 * may allow you to turn this value on or off. If so, when turned off,
3181 * no fragment interleave will occur (which will cause a head of line
3182 * blocking amongst multiple associations sharing the same one to many
3183 * socket). When this option is turned on, then each receive call may
3184 * come from a different association (thus the user must receive data
3185 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3186 * association each receive belongs to.
3187 *
3188 * This option takes a boolean value. A non-zero value indicates that
3189 * fragmented interleave is on. A value of zero indicates that
3190 * fragmented interleave is off.
3191 *
3192 * Note that it is important that an implementation that allows this
3193 * option to be turned on, have it off by default. Otherwise an unaware
3194 * application using the one to many model may become confused and act
3195 * incorrectly.
3196 */
3197static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3198 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003199 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003200{
3201 int val;
3202
3203 if (optlen != sizeof(int))
3204 return -EINVAL;
3205 if (get_user(val, (int __user *)optval))
3206 return -EFAULT;
3207
3208 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3209
3210 return 0;
3211}
3212
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003213/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003214 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003215 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003216 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003217 * This option will set or get the SCTP partial delivery point. This
3218 * point is the size of a message where the partial delivery API will be
3219 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003220 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003221 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003222 * point. Note also that the call will fail if the user attempts to set
3223 * this value larger than the socket receive buffer size.
3224 *
3225 * Note that any single message having a length smaller than or equal to
3226 * the SCTP partial delivery point will be delivered in one single read
3227 * call as long as the user provided buffer is large enough to hold the
3228 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003229 */
3230static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3231 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003232 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003233{
3234 u32 val;
3235
3236 if (optlen != sizeof(u32))
3237 return -EINVAL;
3238 if (get_user(val, (int __user *)optval))
3239 return -EFAULT;
3240
Wei Yongjun8510b932008-12-25 16:59:03 -08003241 /* Note: We double the receive buffer from what the user sets
3242 * it to be, also initial rwnd is based on rcvbuf/2.
3243 */
3244 if (val > (sk->sk_rcvbuf >> 1))
3245 return -EINVAL;
3246
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003247 sctp_sk(sk)->pd_point = val;
3248
3249 return 0; /* is this the right error code? */
3250}
3251
Vlad Yasevich70331572007-03-23 11:34:36 -07003252/*
3253 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3254 *
3255 * This option will allow a user to change the maximum burst of packets
3256 * that can be emitted by this association. Note that the default value
3257 * is 4, and some implementations may restrict this setting so that it
3258 * can only be lowered.
3259 *
3260 * NOTE: This text doesn't seem right. Do this on a socket basis with
3261 * future associations inheriting the socket value.
3262 */
3263static int sctp_setsockopt_maxburst(struct sock *sk,
3264 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003265 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003266{
Neil Horman219b99a2008-03-05 13:44:46 -08003267 struct sctp_assoc_value params;
3268 struct sctp_sock *sp;
3269 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003270 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003271 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003272
Neil Horman219b99a2008-03-05 13:44:46 -08003273 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00003274 pr_warn("Use of int in max_burst socket option deprecated\n");
3275 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08003276 if (copy_from_user(&val, optval, optlen))
3277 return -EFAULT;
3278 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3279 if (copy_from_user(&params, optval, optlen))
3280 return -EFAULT;
3281 val = params.assoc_value;
3282 assoc_id = params.assoc_id;
3283 } else
3284 return -EINVAL;
3285
3286 sp = sctp_sk(sk);
3287
3288 if (assoc_id != 0) {
3289 asoc = sctp_id2assoc(sk, assoc_id);
3290 if (!asoc)
3291 return -EINVAL;
3292 asoc->max_burst = val;
3293 } else
3294 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003295
3296 return 0;
3297}
3298
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003299/*
3300 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3301 *
3302 * This set option adds a chunk type that the user is requesting to be
3303 * received only in an authenticated way. Changes to the list of chunks
3304 * will only effect future associations on the socket.
3305 */
3306static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003307 char __user *optval,
3308 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003309{
3310 struct sctp_authchunk val;
3311
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003312 if (!sctp_auth_enable)
3313 return -EACCES;
3314
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003315 if (optlen != sizeof(struct sctp_authchunk))
3316 return -EINVAL;
3317 if (copy_from_user(&val, optval, optlen))
3318 return -EFAULT;
3319
3320 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003321 case SCTP_CID_INIT:
3322 case SCTP_CID_INIT_ACK:
3323 case SCTP_CID_SHUTDOWN_COMPLETE:
3324 case SCTP_CID_AUTH:
3325 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003326 }
3327
3328 /* add this chunk id to the endpoint */
3329 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3330}
3331
3332/*
3333 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3334 *
3335 * This option gets or sets the list of HMAC algorithms that the local
3336 * endpoint requires the peer to use.
3337 */
3338static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003339 char __user *optval,
3340 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003341{
3342 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003343 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003344 int err;
3345
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003346 if (!sctp_auth_enable)
3347 return -EACCES;
3348
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003349 if (optlen < sizeof(struct sctp_hmacalgo))
3350 return -EINVAL;
3351
Shan Wei934253a2011-04-18 19:13:18 +00003352 hmacs= memdup_user(optval, optlen);
3353 if (IS_ERR(hmacs))
3354 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003355
Vlad Yasevichd9724052008-08-27 16:09:49 -07003356 idents = hmacs->shmac_num_idents;
3357 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3358 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003359 err = -EINVAL;
3360 goto out;
3361 }
3362
3363 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3364out:
3365 kfree(hmacs);
3366 return err;
3367}
3368
3369/*
3370 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3371 *
3372 * This option will set a shared secret key which is used to build an
3373 * association shared key.
3374 */
3375static int sctp_setsockopt_auth_key(struct sock *sk,
3376 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003377 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003378{
3379 struct sctp_authkey *authkey;
3380 struct sctp_association *asoc;
3381 int ret;
3382
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003383 if (!sctp_auth_enable)
3384 return -EACCES;
3385
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003386 if (optlen <= sizeof(struct sctp_authkey))
3387 return -EINVAL;
3388
Shan Wei934253a2011-04-18 19:13:18 +00003389 authkey= memdup_user(optval, optlen);
3390 if (IS_ERR(authkey))
3391 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003392
Vlad Yasevich328fc472008-08-27 16:08:54 -07003393 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003394 ret = -EINVAL;
3395 goto out;
3396 }
3397
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003398 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3399 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3400 ret = -EINVAL;
3401 goto out;
3402 }
3403
3404 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3405out:
Daniel Borkmann8a501d82013-02-08 03:04:34 +00003406 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003407 return ret;
3408}
3409
3410/*
3411 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3412 *
3413 * This option will get or set the active shared key to be used to build
3414 * the association shared key.
3415 */
3416static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003417 char __user *optval,
3418 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003419{
3420 struct sctp_authkeyid val;
3421 struct sctp_association *asoc;
3422
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003423 if (!sctp_auth_enable)
3424 return -EACCES;
3425
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003426 if (optlen != sizeof(struct sctp_authkeyid))
3427 return -EINVAL;
3428 if (copy_from_user(&val, optval, optlen))
3429 return -EFAULT;
3430
3431 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3432 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3433 return -EINVAL;
3434
3435 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3436 val.scact_keynumber);
3437}
3438
3439/*
3440 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3441 *
3442 * This set option will delete a shared secret key from use.
3443 */
3444static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003445 char __user *optval,
3446 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003447{
3448 struct sctp_authkeyid val;
3449 struct sctp_association *asoc;
3450
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003451 if (!sctp_auth_enable)
3452 return -EACCES;
3453
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003454 if (optlen != sizeof(struct sctp_authkeyid))
3455 return -EINVAL;
3456 if (copy_from_user(&val, optval, optlen))
3457 return -EFAULT;
3458
3459 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3460 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3461 return -EINVAL;
3462
3463 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3464 val.scact_keynumber);
3465
3466}
3467
Michio Honda7dc04d72011-04-26 20:16:31 +09003468/*
3469 * 8.1.23 SCTP_AUTO_ASCONF
3470 *
3471 * This option will enable or disable the use of the automatic generation of
3472 * ASCONF chunks to add and delete addresses to an existing association. Note
3473 * that this option has two caveats namely: a) it only affects sockets that
3474 * are bound to all addresses available to the SCTP stack, and b) the system
3475 * administrator may have an overriding control that turns the ASCONF feature
3476 * off no matter what setting the socket option may have.
3477 * This option expects an integer boolean flag, where a non-zero value turns on
3478 * the option, and a zero value turns off the option.
3479 * Note. In this implementation, socket operation overrides default parameter
3480 * being set by sysctl as well as FreeBSD implementation
3481 */
3482static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3483 unsigned int optlen)
3484{
3485 int val;
3486 struct sctp_sock *sp = sctp_sk(sk);
3487
3488 if (optlen < sizeof(int))
3489 return -EINVAL;
3490 if (get_user(val, (int __user *)optval))
3491 return -EFAULT;
3492 if (!sctp_is_ep_boundall(sk) && val)
3493 return -EINVAL;
3494 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3495 return 0;
3496
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003497 spin_lock_bh(&sctp_globals.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003498 if (val == 0 && sp->do_auto_asconf) {
3499 list_del(&sp->auto_asconf_list);
3500 sp->do_auto_asconf = 0;
3501 } else if (val && !sp->do_auto_asconf) {
3502 list_add_tail(&sp->auto_asconf_list,
3503 &sctp_auto_asconf_splist);
3504 sp->do_auto_asconf = 1;
3505 }
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003506 spin_unlock_bh(&sctp_globals.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003507 return 0;
3508}
3509
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511/* API 6.2 setsockopt(), getsockopt()
3512 *
3513 * Applications use setsockopt() and getsockopt() to set or retrieve
3514 * socket options. Socket options are used to change the default
3515 * behavior of sockets calls. They are described in Section 7.
3516 *
3517 * The syntax is:
3518 *
3519 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3520 * int __user *optlen);
3521 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3522 * int optlen);
3523 *
3524 * sd - the socket descript.
3525 * level - set to IPPROTO_SCTP for all SCTP options.
3526 * optname - the option name.
3527 * optval - the buffer to store the value of the option.
3528 * optlen - the size of the buffer.
3529 */
3530SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003531 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 int retval = 0;
3534
3535 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3536 sk, optname);
3537
3538 /* I can hardly begin to describe how wrong this is. This is
3539 * so broken as to be worse than useless. The API draft
3540 * REALLY is NOT helpful here... I am not convinced that the
3541 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3542 * are at all well-founded.
3543 */
3544 if (level != SOL_SCTP) {
3545 struct sctp_af *af = sctp_sk(sk)->pf->af;
3546 retval = af->setsockopt(sk, level, optname, optval, optlen);
3547 goto out_nounlock;
3548 }
3549
3550 sctp_lock_sock(sk);
3551
3552 switch (optname) {
3553 case SCTP_SOCKOPT_BINDX_ADD:
3554 /* 'optlen' is the size of the addresses buffer. */
3555 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3556 optlen, SCTP_BINDX_ADD_ADDR);
3557 break;
3558
3559 case SCTP_SOCKOPT_BINDX_REM:
3560 /* 'optlen' is the size of the addresses buffer. */
3561 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3562 optlen, SCTP_BINDX_REM_ADDR);
3563 break;
3564
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003565 case SCTP_SOCKOPT_CONNECTX_OLD:
3566 /* 'optlen' is the size of the addresses buffer. */
3567 retval = sctp_setsockopt_connectx_old(sk,
3568 (struct sockaddr __user *)optval,
3569 optlen);
3570 break;
3571
Frank Filz3f7a87d2005-06-20 13:14:57 -07003572 case SCTP_SOCKOPT_CONNECTX:
3573 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003574 retval = sctp_setsockopt_connectx(sk,
3575 (struct sockaddr __user *)optval,
3576 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003577 break;
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 case SCTP_DISABLE_FRAGMENTS:
3580 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3581 break;
3582
3583 case SCTP_EVENTS:
3584 retval = sctp_setsockopt_events(sk, optval, optlen);
3585 break;
3586
3587 case SCTP_AUTOCLOSE:
3588 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3589 break;
3590
3591 case SCTP_PEER_ADDR_PARAMS:
3592 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3593 break;
3594
Shan Wei4580ccc2011-01-18 22:39:00 +00003595 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003596 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003597 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003598 case SCTP_PARTIAL_DELIVERY_POINT:
3599 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3600 break;
Frank Filz77086102005-12-22 11:37:30 -08003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 case SCTP_INITMSG:
3603 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3604 break;
3605 case SCTP_DEFAULT_SEND_PARAM:
3606 retval = sctp_setsockopt_default_send_param(sk, optval,
3607 optlen);
3608 break;
3609 case SCTP_PRIMARY_ADDR:
3610 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3611 break;
3612 case SCTP_SET_PEER_PRIMARY_ADDR:
3613 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3614 break;
3615 case SCTP_NODELAY:
3616 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3617 break;
3618 case SCTP_RTOINFO:
3619 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3620 break;
3621 case SCTP_ASSOCINFO:
3622 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3623 break;
3624 case SCTP_I_WANT_MAPPED_V4_ADDR:
3625 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3626 break;
3627 case SCTP_MAXSEG:
3628 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3629 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003630 case SCTP_ADAPTATION_LAYER:
3631 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003633 case SCTP_CONTEXT:
3634 retval = sctp_setsockopt_context(sk, optval, optlen);
3635 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003636 case SCTP_FRAGMENT_INTERLEAVE:
3637 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3638 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003639 case SCTP_MAX_BURST:
3640 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3641 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003642 case SCTP_AUTH_CHUNK:
3643 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3644 break;
3645 case SCTP_HMAC_IDENT:
3646 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3647 break;
3648 case SCTP_AUTH_KEY:
3649 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3650 break;
3651 case SCTP_AUTH_ACTIVE_KEY:
3652 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3653 break;
3654 case SCTP_AUTH_DELETE_KEY:
3655 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3656 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003657 case SCTP_AUTO_ASCONF:
3658 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 default:
3661 retval = -ENOPROTOOPT;
3662 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 sctp_release_sock(sk);
3666
3667out_nounlock:
3668 return retval;
3669}
3670
3671/* API 3.1.6 connect() - UDP Style Syntax
3672 *
3673 * An application may use the connect() call in the UDP model to initiate an
3674 * association without sending data.
3675 *
3676 * The syntax is:
3677 *
3678 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3679 *
3680 * sd: the socket descriptor to have a new association added to.
3681 *
3682 * nam: the address structure (either struct sockaddr_in or struct
3683 * sockaddr_in6 defined in RFC2553 [7]).
3684 *
3685 * len: the size of the address.
3686 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003687SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 int addr_len)
3689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003691 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 sctp_lock_sock(sk);
3694
Frank Filz3f7a87d2005-06-20 13:14:57 -07003695 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08003696 __func__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Frank Filz3f7a87d2005-06-20 13:14:57 -07003698 /* Validate addr_len before calling common connect/connectx routine. */
3699 af = sctp_get_af_specific(addr->sa_family);
3700 if (!af || addr_len < af->sockaddr_len) {
3701 err = -EINVAL;
3702 } else {
3703 /* Pass correct addr len to common routine (so it knows there
3704 * is only one address being passed.
3705 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003706 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return err;
3711}
3712
3713/* FIXME: Write comments. */
3714SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3715{
3716 return -EOPNOTSUPP; /* STUB */
3717}
3718
3719/* 4.1.4 accept() - TCP Style Syntax
3720 *
3721 * Applications use accept() call to remove an established SCTP
3722 * association from the accept queue of the endpoint. A new socket
3723 * descriptor will be returned from accept() to represent the newly
3724 * formed association.
3725 */
3726SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3727{
3728 struct sctp_sock *sp;
3729 struct sctp_endpoint *ep;
3730 struct sock *newsk = NULL;
3731 struct sctp_association *asoc;
3732 long timeo;
3733 int error = 0;
3734
3735 sctp_lock_sock(sk);
3736
3737 sp = sctp_sk(sk);
3738 ep = sp->ep;
3739
3740 if (!sctp_style(sk, TCP)) {
3741 error = -EOPNOTSUPP;
3742 goto out;
3743 }
3744
3745 if (!sctp_sstate(sk, LISTENING)) {
3746 error = -EINVAL;
3747 goto out;
3748 }
3749
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003750 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 error = sctp_wait_for_accept(sk, timeo);
3753 if (error)
3754 goto out;
3755
3756 /* We treat the list of associations on the endpoint as the accept
3757 * queue and pick the first association on the list.
3758 */
3759 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3760
3761 newsk = sp->pf->create_accept_sk(sk, asoc);
3762 if (!newsk) {
3763 error = -ENOMEM;
3764 goto out;
3765 }
3766
3767 /* Populate the fields of the newsk from the oldsk and migrate the
3768 * asoc to the newsk.
3769 */
3770 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3771
3772out:
3773 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003774 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return newsk;
3776}
3777
3778/* The SCTP ioctl handler. */
3779SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3780{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003781 int rc = -ENOTCONN;
3782
3783 sctp_lock_sock(sk);
3784
3785 /*
3786 * SEQPACKET-style sockets in LISTENING state are valid, for
3787 * SCTP, so only discard TCP-style sockets in LISTENING state.
3788 */
3789 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3790 goto out;
3791
3792 switch (cmd) {
3793 case SIOCINQ: {
3794 struct sk_buff *skb;
3795 unsigned int amount = 0;
3796
3797 skb = skb_peek(&sk->sk_receive_queue);
3798 if (skb != NULL) {
3799 /*
3800 * We will only return the amount of this packet since
3801 * that is all that will be read.
3802 */
3803 amount = skb->len;
3804 }
3805 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003806 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07003807 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003808 default:
3809 rc = -ENOIOCTLCMD;
3810 break;
3811 }
3812out:
3813 sctp_release_sock(sk);
3814 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
3817/* This is the function which gets called during socket creation to
3818 * initialized the SCTP-specific portion of the sock.
3819 * The sock structure should already be zero-filled memory.
3820 */
3821SCTP_STATIC int sctp_init_sock(struct sock *sk)
3822{
3823 struct sctp_endpoint *ep;
3824 struct sctp_sock *sp;
3825
3826 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3827
3828 sp = sctp_sk(sk);
3829
3830 /* Initialize the SCTP per socket area. */
3831 switch (sk->sk_type) {
3832 case SOCK_SEQPACKET:
3833 sp->type = SCTP_SOCKET_UDP;
3834 break;
3835 case SOCK_STREAM:
3836 sp->type = SCTP_SOCKET_TCP;
3837 break;
3838 default:
3839 return -ESOCKTNOSUPPORT;
3840 }
3841
3842 /* Initialize default send parameters. These parameters can be
3843 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3844 */
3845 sp->default_stream = 0;
3846 sp->default_ppid = 0;
3847 sp->default_flags = 0;
3848 sp->default_context = 0;
3849 sp->default_timetolive = 0;
3850
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003851 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003852 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 /* Initialize default setup parameters. These parameters
3855 * can be modified with the SCTP_INITMSG socket option or
3856 * overridden by the SCTP_INIT CMSG.
3857 */
3858 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3859 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3860 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003861 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
3863 /* Initialize default RTO related parameters. These parameters can
3864 * be modified for with the SCTP_RTOINFO socket option.
3865 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003866 sp->rtoinfo.srto_initial = sctp_rto_initial;
3867 sp->rtoinfo.srto_max = sctp_rto_max;
3868 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 /* Initialize default association related parameters. These parameters
3871 * can be modified with the SCTP_ASSOCINFO socket option.
3872 */
3873 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3874 sp->assocparams.sasoc_number_peer_destinations = 0;
3875 sp->assocparams.sasoc_peer_rwnd = 0;
3876 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003877 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003880 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 */
3882 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3883
3884 /* Default Peer Address Parameters. These defaults can
3885 * be modified via SCTP_PEER_ADDR_PARAMS
3886 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003887 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003888 sp->pathmaxrxt = sctp_max_retrans_path;
3889 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003890 sp->sackdelay = sctp_sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07003891 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003892 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003893 SPP_PMTUD_ENABLE |
3894 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 /* If enabled no SCTP message fragmentation will be performed.
3897 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3898 */
3899 sp->disable_fragments = 0;
3900
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003901 /* Enable Nagle algorithm by default. */
3902 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 /* Enable by default. */
3905 sp->v4mapped = 1;
3906
3907 /* Auto-close idle associations after the configured
3908 * number of seconds. A value of 0 disables this
3909 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3910 * for UDP-style sockets only.
3911 */
3912 sp->autoclose = 0;
3913
3914 /* User specified fragmentation limit. */
3915 sp->user_frag = 0;
3916
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003917 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
3919 sp->pf = sctp_get_pf_specific(sk->sk_family);
3920
3921 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003922 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003924 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926 /* Create a per socket endpoint structure. Even if we
3927 * change the data structure relationships, this may still
3928 * be useful for storing pre-connect address information.
3929 */
3930 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3931 if (!ep)
3932 return -ENOMEM;
3933
3934 sp->ep = ep;
3935 sp->hmac = NULL;
3936
3937 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08003938
3939 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003940 percpu_counter_inc(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003941 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003942
3943 /* Nothing can fail after this block, otherwise
3944 * sctp_destroy_sock() will be called without addr_wq_lock held
3945 */
Michio Honda9f7d6532011-04-26 19:32:51 +09003946 if (sctp_default_auto_asconf) {
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003947 spin_lock(&sctp_globals.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09003948 list_add_tail(&sp->auto_asconf_list,
3949 &sctp_auto_asconf_splist);
3950 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003951 spin_unlock(&sctp_globals.addr_wq_lock);
3952 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09003953 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003954 }
3955
David S. Miller6f756a82008-11-23 17:34:03 -08003956 local_bh_enable();
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return 0;
3959}
3960
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03003961/* Cleanup any SCTP per socket resources. Must be called with
3962 * sctp_globals.addr_wq_lock held if sp->do_auto_asconf is true
3963 */
Brian Haley7d06b2e2008-06-14 17:04:49 -07003964SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Michio Honda9f7d6532011-04-26 19:32:51 +09003966 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3969
3970 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09003971 sp = sctp_sk(sk);
Daniel Borkmann93f75342013-06-06 15:53:47 +02003972 /* This could happen during socket init, thus we bail out
3973 * early, since the rest of the below is not setup either.
3974 */
3975 if (sp->ep == NULL)
3976 return;
3977
Michio Honda9f7d6532011-04-26 19:32:51 +09003978 if (sp->do_auto_asconf) {
3979 sp->do_auto_asconf = 0;
3980 list_del(&sp->auto_asconf_list);
3981 }
3982 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003983 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003984 percpu_counter_dec(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003985 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003986 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987}
3988
3989/* API 4.1.7 shutdown() - TCP Style Syntax
3990 * int shutdown(int socket, int how);
3991 *
3992 * sd - the socket descriptor of the association to be closed.
3993 * how - Specifies the type of shutdown. The values are
3994 * as follows:
3995 * SHUT_RD
3996 * Disables further receive operations. No SCTP
3997 * protocol action is taken.
3998 * SHUT_WR
3999 * Disables further send operations, and initiates
4000 * the SCTP shutdown sequence.
4001 * SHUT_RDWR
4002 * Disables further send and receive operations
4003 * and initiates the SCTP shutdown sequence.
4004 */
4005SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4006{
4007 struct sctp_endpoint *ep;
4008 struct sctp_association *asoc;
4009
4010 if (!sctp_style(sk, TCP))
4011 return;
4012
4013 if (how & SEND_SHUTDOWN) {
4014 ep = sctp_sk(sk)->ep;
4015 if (!list_empty(&ep->asocs)) {
4016 asoc = list_entry(ep->asocs.next,
4017 struct sctp_association, asocs);
4018 sctp_primitive_SHUTDOWN(asoc, NULL);
4019 }
4020 }
4021}
4022
4023/* 7.2.1 Association Status (SCTP_STATUS)
4024
4025 * Applications can retrieve current status information about an
4026 * association, including association state, peer receiver window size,
4027 * number of unacked data chunks, and number of data chunks pending
4028 * receipt. This information is read-only.
4029 */
4030static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4031 char __user *optval,
4032 int __user *optlen)
4033{
4034 struct sctp_status status;
4035 struct sctp_association *asoc = NULL;
4036 struct sctp_transport *transport;
4037 sctp_assoc_t associd;
4038 int retval = 0;
4039
Neil Horman408f22e2007-06-16 14:03:45 -04004040 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 retval = -EINVAL;
4042 goto out;
4043 }
4044
Neil Horman408f22e2007-06-16 14:03:45 -04004045 len = sizeof(status);
4046 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 retval = -EFAULT;
4048 goto out;
4049 }
4050
4051 associd = status.sstat_assoc_id;
4052 asoc = sctp_id2assoc(sk, associd);
4053 if (!asoc) {
4054 retval = -EINVAL;
4055 goto out;
4056 }
4057
4058 transport = asoc->peer.primary_path;
4059
4060 status.sstat_assoc_id = sctp_assoc2id(asoc);
4061 status.sstat_state = asoc->state;
4062 status.sstat_rwnd = asoc->peer.rwnd;
4063 status.sstat_unackdata = asoc->unack_data;
4064
4065 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4066 status.sstat_instrms = asoc->c.sinit_max_instreams;
4067 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4068 status.sstat_fragmentation_point = asoc->frag_point;
4069 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004070 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4071 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 /* Map ipv4 address into v4-mapped-on-v6 address. */
4073 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4074 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004075 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4077 status.sstat_primary.spinfo_srtt = transport->srtt;
4078 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004079 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Frank Filz3f7a87d2005-06-20 13:14:57 -07004081 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4082 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4083
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (put_user(len, optlen)) {
4085 retval = -EFAULT;
4086 goto out;
4087 }
4088
4089 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4090 len, status.sstat_state, status.sstat_rwnd,
4091 status.sstat_assoc_id);
4092
4093 if (copy_to_user(optval, &status, len)) {
4094 retval = -EFAULT;
4095 goto out;
4096 }
4097
4098out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004099 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
4102
4103/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4104 *
4105 * Applications can retrieve information about a specific peer address
4106 * of an association, including its reachability state, congestion
4107 * window, and retransmission timer values. This information is
4108 * read-only.
4109 */
4110static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4111 char __user *optval,
4112 int __user *optlen)
4113{
4114 struct sctp_paddrinfo pinfo;
4115 struct sctp_transport *transport;
4116 int retval = 0;
4117
Neil Horman408f22e2007-06-16 14:03:45 -04004118 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 retval = -EINVAL;
4120 goto out;
4121 }
4122
Neil Horman408f22e2007-06-16 14:03:45 -04004123 len = sizeof(pinfo);
4124 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 retval = -EFAULT;
4126 goto out;
4127 }
4128
4129 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4130 pinfo.spinfo_assoc_id);
4131 if (!transport)
4132 return -EINVAL;
4133
4134 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004135 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 pinfo.spinfo_cwnd = transport->cwnd;
4137 pinfo.spinfo_srtt = transport->srtt;
4138 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004139 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Frank Filz3f7a87d2005-06-20 13:14:57 -07004141 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4142 pinfo.spinfo_state = SCTP_ACTIVE;
4143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (put_user(len, optlen)) {
4145 retval = -EFAULT;
4146 goto out;
4147 }
4148
4149 if (copy_to_user(optval, &pinfo, len)) {
4150 retval = -EFAULT;
4151 goto out;
4152 }
4153
4154out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004155 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
4158/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4159 *
4160 * This option is a on/off flag. If enabled no SCTP message
4161 * fragmentation will be performed. Instead if a message being sent
4162 * exceeds the current PMTU size, the message will NOT be sent and
4163 * instead a error will be indicated to the user.
4164 */
4165static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4166 char __user *optval, int __user *optlen)
4167{
4168 int val;
4169
4170 if (len < sizeof(int))
4171 return -EINVAL;
4172
4173 len = sizeof(int);
4174 val = (sctp_sk(sk)->disable_fragments == 1);
4175 if (put_user(len, optlen))
4176 return -EFAULT;
4177 if (copy_to_user(optval, &val, len))
4178 return -EFAULT;
4179 return 0;
4180}
4181
4182/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4183 *
4184 * This socket option is used to specify various notifications and
4185 * ancillary data the user wishes to receive.
4186 */
4187static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4188 int __user *optlen)
4189{
Thomas Grafacdd5982012-04-03 22:17:53 +00004190 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004192 if (len > sizeof(struct sctp_event_subscribe))
4193 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004194 if (put_user(len, optlen))
4195 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4197 return -EFAULT;
4198 return 0;
4199}
4200
4201/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4202 *
4203 * This socket option is applicable to the UDP-style socket only. When
4204 * set it will cause associations that are idle for more than the
4205 * specified number of seconds to automatically close. An association
4206 * being idle is defined an association that has NOT sent or received
4207 * user data. The special value of '0' indicates that no automatic
4208 * close of any associations should be performed. The option expects an
4209 * integer defining the number of seconds of idle time before an
4210 * association is closed.
4211 */
4212static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4213{
4214 /* Applicable to UDP-style socket only */
4215 if (sctp_style(sk, TCP))
4216 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004217 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004219 len = sizeof(int);
4220 if (put_user(len, optlen))
4221 return -EFAULT;
4222 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return -EFAULT;
4224 return 0;
4225}
4226
4227/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004228int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004230 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 struct socket *sock;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004232 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 int err = 0;
4234
Benjamin Poirier0343c552012-03-08 05:55:58 +00004235 if (!asoc)
4236 return -EINVAL;
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 /* An association cannot be branched off from an already peeled-off
4239 * socket, nor is this supported for tcp style sockets.
4240 */
4241 if (!sctp_style(sk, UDP))
4242 return -EINVAL;
4243
4244 /* Create a new socket. */
4245 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4246 if (err < 0)
4247 return err;
4248
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004249 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004250
4251 /* Make peeled-off sockets more like 1-1 accepted sockets.
4252 * Set the daddr and initialize id to something more random
4253 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004254 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4255 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004256
4257 /* Populate the fields of the newsk from the oldsk and migrate the
4258 * asoc to the newsk.
4259 */
4260 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 *sockp = sock;
4263
4264 return err;
4265}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004266EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4269{
4270 sctp_peeloff_arg_t peeloff;
4271 struct socket *newsock;
4272 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Neil Horman408f22e2007-06-16 14:03:45 -04004274 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004276 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (copy_from_user(&peeloff, optval, len))
4278 return -EFAULT;
4279
Benjamin Poirier0343c552012-03-08 05:55:58 +00004280 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (retval < 0)
4282 goto out;
4283
4284 /* Map the socket to an unused fd that can be returned to the user. */
Ulrich Dreppera677a032008-07-23 21:29:17 -07004285 retval = sock_map_fd(newsock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (retval < 0) {
4287 sock_release(newsock);
4288 goto out;
4289 }
4290
Benjamin Poirier0343c552012-03-08 05:55:58 +00004291 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4292 __func__, sk, newsock->sk, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 /* Return the fd mapped to the new socket. */
4295 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04004296 if (put_user(len, optlen))
4297 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (copy_to_user(optval, &peeloff, len))
4299 retval = -EFAULT;
4300
4301out:
4302 return retval;
4303}
4304
4305/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4306 *
4307 * Applications can enable or disable heartbeats for any peer address of
4308 * an association, modify an address's heartbeat interval, force a
4309 * heartbeat to be sent immediately, and adjust the address's maximum
4310 * number of retransmissions sent before an address is considered
4311 * unreachable. The following structure is used to access and modify an
4312 * address's parameters:
4313 *
4314 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004315 * sctp_assoc_t spp_assoc_id;
4316 * struct sockaddr_storage spp_address;
4317 * uint32_t spp_hbinterval;
4318 * uint16_t spp_pathmaxrxt;
4319 * uint32_t spp_pathmtu;
4320 * uint32_t spp_sackdelay;
4321 * uint32_t spp_flags;
4322 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004324 * spp_assoc_id - (one-to-many style socket) This is filled in the
4325 * application, and identifies the association for
4326 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * spp_address - This specifies which address is of interest.
4328 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004329 * in milliseconds. If a value of zero
4330 * is present in this field then no changes are to
4331 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 * spp_pathmaxrxt - This contains the maximum number of
4333 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004334 * considered unreachable. If a value of zero
4335 * is present in this field then no changes are to
4336 * be made to this parameter.
4337 * spp_pathmtu - When Path MTU discovery is disabled the value
4338 * specified here will be the "fixed" path mtu.
4339 * Note that if the spp_address field is empty
4340 * then all associations on this address will
4341 * have this fixed path mtu set upon them.
4342 *
4343 * spp_sackdelay - When delayed sack is enabled, this value specifies
4344 * the number of milliseconds that sacks will be delayed
4345 * for. This value will apply to all addresses of an
4346 * association if the spp_address field is empty. Note
4347 * also, that if delayed sack is enabled and this
4348 * value is set to 0, no change is made to the last
4349 * recorded delayed sack timer value.
4350 *
4351 * spp_flags - These flags are used to control various features
4352 * on an association. The flag field may contain
4353 * zero or more of the following options.
4354 *
4355 * SPP_HB_ENABLE - Enable heartbeats on the
4356 * specified address. Note that if the address
4357 * field is empty all addresses for the association
4358 * have heartbeats enabled upon them.
4359 *
4360 * SPP_HB_DISABLE - Disable heartbeats on the
4361 * speicifed address. Note that if the address
4362 * field is empty all addresses for the association
4363 * will have their heartbeats disabled. Note also
4364 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4365 * mutually exclusive, only one of these two should
4366 * be specified. Enabling both fields will have
4367 * undetermined results.
4368 *
4369 * SPP_HB_DEMAND - Request a user initiated heartbeat
4370 * to be made immediately.
4371 *
4372 * SPP_PMTUD_ENABLE - This field will enable PMTU
4373 * discovery upon the specified address. Note that
4374 * if the address feild is empty then all addresses
4375 * on the association are effected.
4376 *
4377 * SPP_PMTUD_DISABLE - This field will disable PMTU
4378 * discovery upon the specified address. Note that
4379 * if the address feild is empty then all addresses
4380 * on the association are effected. Not also that
4381 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4382 * exclusive. Enabling both will have undetermined
4383 * results.
4384 *
4385 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4386 * on delayed sack. The time specified in spp_sackdelay
4387 * is used to specify the sack delay for this address. Note
4388 * that if spp_address is empty then all addresses will
4389 * enable delayed sack and take on the sack delay
4390 * value specified in spp_sackdelay.
4391 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4392 * off delayed sack. If the spp_address field is blank then
4393 * delayed sack is disabled for the entire association. Note
4394 * also that this field is mutually exclusive to
4395 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4396 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 */
4398static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004399 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004401 struct sctp_paddrparams params;
4402 struct sctp_transport *trans = NULL;
4403 struct sctp_association *asoc = NULL;
4404 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Neil Horman408f22e2007-06-16 14:03:45 -04004406 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004408 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 if (copy_from_user(&params, optval, len))
4410 return -EFAULT;
4411
Frank Filz52ccb8e2005-12-22 11:36:46 -08004412 /* If an address other than INADDR_ANY is specified, and
4413 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004415 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004416 trans = sctp_addr_id2transport(sk, &params.spp_address,
4417 params.spp_assoc_id);
4418 if (!trans) {
4419 SCTP_DEBUG_PRINTK("Failed no transport\n");
4420 return -EINVAL;
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
4423
Frank Filz52ccb8e2005-12-22 11:36:46 -08004424 /* Get association, if assoc_id != 0 and the socket is a one
4425 * to many style socket, and an association was not found, then
4426 * the id was invalid.
4427 */
4428 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4429 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4430 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Frank Filz52ccb8e2005-12-22 11:36:46 -08004434 if (trans) {
4435 /* Fetch transport values. */
4436 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4437 params.spp_pathmtu = trans->pathmtu;
4438 params.spp_pathmaxrxt = trans->pathmaxrxt;
4439 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Frank Filz52ccb8e2005-12-22 11:36:46 -08004441 /*draft-11 doesn't say what to return in spp_flags*/
4442 params.spp_flags = trans->param_flags;
4443 } else if (asoc) {
4444 /* Fetch association values. */
4445 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4446 params.spp_pathmtu = asoc->pathmtu;
4447 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4448 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Frank Filz52ccb8e2005-12-22 11:36:46 -08004450 /*draft-11 doesn't say what to return in spp_flags*/
4451 params.spp_flags = asoc->param_flags;
4452 } else {
4453 /* Fetch socket values. */
4454 params.spp_hbinterval = sp->hbinterval;
4455 params.spp_pathmtu = sp->pathmtu;
4456 params.spp_sackdelay = sp->sackdelay;
4457 params.spp_pathmaxrxt = sp->pathmaxrxt;
4458
4459 /*draft-11 doesn't say what to return in spp_flags*/
4460 params.spp_flags = sp->param_flags;
4461 }
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (copy_to_user(optval, &params, len))
4464 return -EFAULT;
4465
4466 if (put_user(len, optlen))
4467 return -EFAULT;
4468
4469 return 0;
4470}
4471
Wei Yongjund364d922008-05-09 15:13:26 -07004472/*
4473 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08004474 *
Wei Yongjund364d922008-05-09 15:13:26 -07004475 * This option will effect the way delayed acks are performed. This
4476 * option allows you to get or set the delayed ack time, in
4477 * milliseconds. It also allows changing the delayed ack frequency.
4478 * Changing the frequency to 1 disables the delayed sack algorithm. If
4479 * the assoc_id is 0, then this sets or gets the endpoints default
4480 * values. If the assoc_id field is non-zero, then the set or get
4481 * effects the specified association for the one to many model (the
4482 * assoc_id field is ignored by the one to one model). Note that if
4483 * sack_delay or sack_freq are 0 when setting this option, then the
4484 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08004485 *
Wei Yongjund364d922008-05-09 15:13:26 -07004486 * struct sctp_sack_info {
4487 * sctp_assoc_t sack_assoc_id;
4488 * uint32_t sack_delay;
4489 * uint32_t sack_freq;
4490 * };
Frank Filz77086102005-12-22 11:37:30 -08004491 *
Wei Yongjund364d922008-05-09 15:13:26 -07004492 * sack_assoc_id - This parameter, indicates which association the user
4493 * is performing an action upon. Note that if this field's value is
4494 * zero then the endpoints default value is changed (effecting future
4495 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08004496 *
Wei Yongjund364d922008-05-09 15:13:26 -07004497 * sack_delay - This parameter contains the number of milliseconds that
4498 * the user is requesting the delayed ACK timer be set to. Note that
4499 * this value is defined in the standard to be between 200 and 500
4500 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08004501 *
Wei Yongjund364d922008-05-09 15:13:26 -07004502 * sack_freq - This parameter contains the number of packets that must
4503 * be received before a sack is sent without waiting for the delay
4504 * timer to expire. The default value for this is 2, setting this
4505 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08004506 */
Wei Yongjund364d922008-05-09 15:13:26 -07004507static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08004508 char __user *optval,
4509 int __user *optlen)
4510{
Wei Yongjund364d922008-05-09 15:13:26 -07004511 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08004512 struct sctp_association *asoc = NULL;
4513 struct sctp_sock *sp = sctp_sk(sk);
4514
Wei Yongjund364d922008-05-09 15:13:26 -07004515 if (len >= sizeof(struct sctp_sack_info)) {
4516 len = sizeof(struct sctp_sack_info);
4517
4518 if (copy_from_user(&params, optval, len))
4519 return -EFAULT;
4520 } else if (len == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00004521 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4522 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07004523 if (copy_from_user(&params, optval, len))
4524 return -EFAULT;
4525 } else
Frank Filz77086102005-12-22 11:37:30 -08004526 return - EINVAL;
4527
Wei Yongjund364d922008-05-09 15:13:26 -07004528 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08004529 * to many style socket, and an association was not found, then
4530 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004531 */
Wei Yongjund364d922008-05-09 15:13:26 -07004532 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4533 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08004534 return -EINVAL;
4535
4536 if (asoc) {
4537 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004538 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4539 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08004540 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07004541 params.sack_freq = asoc->sackfreq;
4542
4543 } else {
4544 params.sack_delay = 0;
4545 params.sack_freq = 1;
4546 }
Frank Filz77086102005-12-22 11:37:30 -08004547 } else {
4548 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004549 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4550 params.sack_delay = sp->sackdelay;
4551 params.sack_freq = sp->sackfreq;
4552 } else {
4553 params.sack_delay = 0;
4554 params.sack_freq = 1;
4555 }
Frank Filz77086102005-12-22 11:37:30 -08004556 }
4557
4558 if (copy_to_user(optval, &params, len))
4559 return -EFAULT;
4560
4561 if (put_user(len, optlen))
4562 return -EFAULT;
4563
4564 return 0;
4565}
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4568 *
4569 * Applications can specify protocol parameters for the default association
4570 * initialization. The option name argument to setsockopt() and getsockopt()
4571 * is SCTP_INITMSG.
4572 *
4573 * Setting initialization parameters is effective only on an unconnected
4574 * socket (for UDP-style sockets only future associations are effected
4575 * by the change). With TCP-style sockets, this option is inherited by
4576 * sockets derived from a listener socket.
4577 */
4578static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4579{
Neil Horman408f22e2007-06-16 14:03:45 -04004580 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004582 len = sizeof(struct sctp_initmsg);
4583 if (put_user(len, optlen))
4584 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4586 return -EFAULT;
4587 return 0;
4588}
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004591static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4592 char __user *optval, int __user *optlen)
4593{
4594 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004595 int cnt = 0;
4596 struct sctp_getaddrs getaddrs;
4597 struct sctp_transport *from;
4598 void __user *to;
4599 union sctp_addr temp;
4600 struct sctp_sock *sp = sctp_sk(sk);
4601 int addrlen;
4602 size_t space_left;
4603 int bytes_copied;
4604
4605 if (len < sizeof(struct sctp_getaddrs))
4606 return -EINVAL;
4607
4608 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4609 return -EFAULT;
4610
4611 /* For UDP-style sockets, id specifies the association to query. */
4612 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4613 if (!asoc)
4614 return -EINVAL;
4615
4616 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004617 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004618
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07004619 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4620 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08004621 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004622 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
Vlad Yasevicha5f4cea2010-04-30 21:42:42 -04004623 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004624 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004625 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004626 if (copy_to_user(to, &temp, addrlen))
4627 return -EFAULT;
4628 to += addrlen;
4629 cnt++;
4630 space_left -= addrlen;
4631 }
4632
4633 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4634 return -EFAULT;
4635 bytes_copied = ((char __user *)to) - optval;
4636 if (put_user(bytes_copied, optlen))
4637 return -EFAULT;
4638
4639 return 0;
4640}
4641
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004642static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4643 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004644{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004645 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004646 union sctp_addr temp;
4647 int cnt = 0;
4648 int addrlen;
4649
Vlad Yasevich29303542007-09-16 16:02:12 -07004650 rcu_read_lock();
4651 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
4652 if (!addr->valid)
4653 continue;
4654
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004655 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004656 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004657 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07004658 if ((PF_INET6 == sk->sk_family) &&
4659 inet_v6_ipv6only(sk) &&
4660 (AF_INET == addr->a.sa.sa_family))
4661 continue;
Al Viro6244be42006-11-20 17:21:44 -08004662 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05004663 if (!temp.v4.sin_port)
4664 temp.v4.sin_port = htons(port);
4665
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004666 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4667 &temp);
4668 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004669 if (space_left < addrlen) {
4670 cnt = -ENOMEM;
4671 break;
4672 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004673 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004674
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004675 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004676 cnt ++;
4677 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004678 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004679 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004680 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004681
4682 return cnt;
4683}
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004686static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4687 char __user *optval, int __user *optlen)
4688{
4689 struct sctp_bind_addr *bp;
4690 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004691 int cnt = 0;
4692 struct sctp_getaddrs getaddrs;
4693 struct sctp_sockaddr_entry *addr;
4694 void __user *to;
4695 union sctp_addr temp;
4696 struct sctp_sock *sp = sctp_sk(sk);
4697 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004698 int err = 0;
4699 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004700 int bytes_copied = 0;
4701 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004702 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004703
Neil Horman408f22e2007-06-16 14:03:45 -04004704 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004705 return -EINVAL;
4706
4707 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4708 return -EFAULT;
4709
4710 /*
4711 * For UDP-style sockets, id specifies the association to query.
4712 * If the id field is set to the value '0' then the locally bound
4713 * addresses are returned without regard to any particular
4714 * association.
4715 */
4716 if (0 == getaddrs.assoc_id) {
4717 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004718 } else {
4719 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4720 if (!asoc)
4721 return -EINVAL;
4722 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004723 }
4724
4725 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004726 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4727
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004728 addrs = kmalloc(space_left, GFP_KERNEL);
4729 if (!addrs)
4730 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004731
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004732 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4733 * addresses from the global local address list.
4734 */
4735 if (sctp_list_single_entry(&bp->address_list)) {
4736 addr = list_entry(bp->address_list.next,
4737 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004738 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004739 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4740 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004741 if (cnt < 0) {
4742 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004743 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004744 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004745 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004746 }
4747 }
4748
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004749 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004750 /* Protection on the bound address list is not needed since
4751 * in the socket option context we hold a socket lock and
4752 * thus the bound address list can't change.
4753 */
4754 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004755 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004756 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4757 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004758 if (space_left < addrlen) {
4759 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004760 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004761 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004762 memcpy(buf, &temp, addrlen);
4763 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004764 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004765 cnt ++;
4766 space_left -= addrlen;
4767 }
4768
4769copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004770 if (copy_to_user(to, addrs, bytes_copied)) {
4771 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004772 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004773 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004774 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4775 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004776 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004777 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004778 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004779 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004780out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004781 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004782 return err;
4783}
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4786 *
4787 * Requests that the local SCTP stack use the enclosed peer address as
4788 * the association primary. The enclosed address must be one of the
4789 * association peer's addresses.
4790 */
4791static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4792 char __user *optval, int __user *optlen)
4793{
4794 struct sctp_prim prim;
4795 struct sctp_association *asoc;
4796 struct sctp_sock *sp = sctp_sk(sk);
4797
Neil Horman408f22e2007-06-16 14:03:45 -04004798 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return -EINVAL;
4800
Neil Horman408f22e2007-06-16 14:03:45 -04004801 len = sizeof(struct sctp_prim);
4802
4803 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return -EFAULT;
4805
4806 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4807 if (!asoc)
4808 return -EINVAL;
4809
4810 if (!asoc->peer.primary_path)
4811 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004812
Al Viro8cec6b82006-11-20 17:23:01 -08004813 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4814 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4817 (union sctp_addr *)&prim.ssp_addr);
4818
Neil Horman408f22e2007-06-16 14:03:45 -04004819 if (put_user(len, optlen))
4820 return -EFAULT;
4821 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return -EFAULT;
4823
4824 return 0;
4825}
4826
4827/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004828 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004830 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 * Indication parameter for all future INIT and INIT-ACK exchanges.
4832 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004833static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 char __user *optval, int __user *optlen)
4835{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004836 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Neil Horman408f22e2007-06-16 14:03:45 -04004838 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return -EINVAL;
4840
Neil Horman408f22e2007-06-16 14:03:45 -04004841 len = sizeof(struct sctp_setadaptation);
4842
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004843 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004844
4845 if (put_user(len, optlen))
4846 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004847 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 return 0;
4851}
4852
4853/*
4854 *
4855 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4856 *
4857 * Applications that wish to use the sendto() system call may wish to
4858 * specify a default set of parameters that would normally be supplied
4859 * through the inclusion of ancillary data. This socket option allows
4860 * such an application to set the default sctp_sndrcvinfo structure.
4861
4862
4863 * The application that wishes to use this socket option simply passes
4864 * in to this call the sctp_sndrcvinfo structure defined in Section
4865 * 5.2.2) The input parameters accepted by this call include
4866 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4867 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4868 * to this call if the caller is using the UDP model.
4869 *
4870 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4871 */
4872static int sctp_getsockopt_default_send_param(struct sock *sk,
4873 int len, char __user *optval,
4874 int __user *optlen)
4875{
4876 struct sctp_sndrcvinfo info;
4877 struct sctp_association *asoc;
4878 struct sctp_sock *sp = sctp_sk(sk);
4879
Neil Horman408f22e2007-06-16 14:03:45 -04004880 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004882
4883 len = sizeof(struct sctp_sndrcvinfo);
4884
4885 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return -EFAULT;
4887
4888 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4889 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4890 return -EINVAL;
4891
4892 if (asoc) {
4893 info.sinfo_stream = asoc->default_stream;
4894 info.sinfo_flags = asoc->default_flags;
4895 info.sinfo_ppid = asoc->default_ppid;
4896 info.sinfo_context = asoc->default_context;
4897 info.sinfo_timetolive = asoc->default_timetolive;
4898 } else {
4899 info.sinfo_stream = sp->default_stream;
4900 info.sinfo_flags = sp->default_flags;
4901 info.sinfo_ppid = sp->default_ppid;
4902 info.sinfo_context = sp->default_context;
4903 info.sinfo_timetolive = sp->default_timetolive;
4904 }
4905
Neil Horman408f22e2007-06-16 14:03:45 -04004906 if (put_user(len, optlen))
4907 return -EFAULT;
4908 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return -EFAULT;
4910
4911 return 0;
4912}
4913
4914/*
4915 *
4916 * 7.1.5 SCTP_NODELAY
4917 *
4918 * Turn on/off any Nagle-like algorithm. This means that packets are
4919 * generally sent as soon as possible and no unnecessary delays are
4920 * introduced, at the cost of more packets in the network. Expects an
4921 * integer boolean flag.
4922 */
4923
4924static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4925 char __user *optval, int __user *optlen)
4926{
4927 int val;
4928
4929 if (len < sizeof(int))
4930 return -EINVAL;
4931
4932 len = sizeof(int);
4933 val = (sctp_sk(sk)->nodelay == 1);
4934 if (put_user(len, optlen))
4935 return -EFAULT;
4936 if (copy_to_user(optval, &val, len))
4937 return -EFAULT;
4938 return 0;
4939}
4940
4941/*
4942 *
4943 * 7.1.1 SCTP_RTOINFO
4944 *
4945 * The protocol parameters used to initialize and bound retransmission
4946 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4947 * and modify these parameters.
4948 * All parameters are time values, in milliseconds. A value of 0, when
4949 * modifying the parameters, indicates that the current value should not
4950 * be changed.
4951 *
4952 */
4953static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4954 char __user *optval,
4955 int __user *optlen) {
4956 struct sctp_rtoinfo rtoinfo;
4957 struct sctp_association *asoc;
4958
Neil Horman408f22e2007-06-16 14:03:45 -04004959 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 return -EINVAL;
4961
Neil Horman408f22e2007-06-16 14:03:45 -04004962 len = sizeof(struct sctp_rtoinfo);
4963
4964 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return -EFAULT;
4966
4967 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4968
4969 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4970 return -EINVAL;
4971
4972 /* Values corresponding to the specific association. */
4973 if (asoc) {
4974 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4975 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4976 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4977 } else {
4978 /* Values corresponding to the endpoint. */
4979 struct sctp_sock *sp = sctp_sk(sk);
4980
4981 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4982 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4983 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4984 }
4985
4986 if (put_user(len, optlen))
4987 return -EFAULT;
4988
4989 if (copy_to_user(optval, &rtoinfo, len))
4990 return -EFAULT;
4991
4992 return 0;
4993}
4994
4995/*
4996 *
4997 * 7.1.2 SCTP_ASSOCINFO
4998 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02004999 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 * of the association.
5001 * Returns an error if the new association retransmission value is
5002 * greater than the sum of the retransmission value of the peer.
5003 * See [SCTP] for more information.
5004 *
5005 */
5006static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5007 char __user *optval,
5008 int __user *optlen)
5009{
5010
5011 struct sctp_assocparams assocparams;
5012 struct sctp_association *asoc;
5013 struct list_head *pos;
5014 int cnt = 0;
5015
Neil Horman408f22e2007-06-16 14:03:45 -04005016 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 return -EINVAL;
5018
Neil Horman408f22e2007-06-16 14:03:45 -04005019 len = sizeof(struct sctp_assocparams);
5020
5021 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return -EFAULT;
5023
5024 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5025
5026 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5027 return -EINVAL;
5028
5029 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005030 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5032 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5033 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5034 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5035 * 1000) +
5036 (asoc->cookie_life.tv_usec
5037 / 1000);
5038
5039 list_for_each(pos, &asoc->peer.transport_addr_list) {
5040 cnt ++;
5041 }
5042
5043 assocparams.sasoc_number_peer_destinations = cnt;
5044 } else {
5045 /* Values corresponding to the endpoint */
5046 struct sctp_sock *sp = sctp_sk(sk);
5047
5048 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5049 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5050 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5051 assocparams.sasoc_cookie_life =
5052 sp->assocparams.sasoc_cookie_life;
5053 assocparams.sasoc_number_peer_destinations =
5054 sp->assocparams.
5055 sasoc_number_peer_destinations;
5056 }
5057
5058 if (put_user(len, optlen))
5059 return -EFAULT;
5060
5061 if (copy_to_user(optval, &assocparams, len))
5062 return -EFAULT;
5063
5064 return 0;
5065}
5066
5067/*
5068 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5069 *
5070 * This socket option is a boolean flag which turns on or off mapped V4
5071 * addresses. If this option is turned on and the socket is type
5072 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5073 * If this option is turned off, then no mapping will be done of V4
5074 * addresses and a user will receive both PF_INET6 and PF_INET type
5075 * addresses on the socket.
5076 */
5077static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5078 char __user *optval, int __user *optlen)
5079{
5080 int val;
5081 struct sctp_sock *sp = sctp_sk(sk);
5082
5083 if (len < sizeof(int))
5084 return -EINVAL;
5085
5086 len = sizeof(int);
5087 val = sp->v4mapped;
5088 if (put_user(len, optlen))
5089 return -EFAULT;
5090 if (copy_to_user(optval, &val, len))
5091 return -EFAULT;
5092
5093 return 0;
5094}
5095
5096/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005097 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5098 * (chapter and verse is quoted at sctp_setsockopt_context())
5099 */
5100static int sctp_getsockopt_context(struct sock *sk, int len,
5101 char __user *optval, int __user *optlen)
5102{
5103 struct sctp_assoc_value params;
5104 struct sctp_sock *sp;
5105 struct sctp_association *asoc;
5106
Neil Horman408f22e2007-06-16 14:03:45 -04005107 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005108 return -EINVAL;
5109
Neil Horman408f22e2007-06-16 14:03:45 -04005110 len = sizeof(struct sctp_assoc_value);
5111
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005112 if (copy_from_user(&params, optval, len))
5113 return -EFAULT;
5114
5115 sp = sctp_sk(sk);
5116
5117 if (params.assoc_id != 0) {
5118 asoc = sctp_id2assoc(sk, params.assoc_id);
5119 if (!asoc)
5120 return -EINVAL;
5121 params.assoc_value = asoc->default_rcv_context;
5122 } else {
5123 params.assoc_value = sp->default_rcv_context;
5124 }
5125
5126 if (put_user(len, optlen))
5127 return -EFAULT;
5128 if (copy_to_user(optval, &params, len))
5129 return -EFAULT;
5130
5131 return 0;
5132}
5133
5134/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005135 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5136 * This option will get or set the maximum size to put in any outgoing
5137 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 * fragmented by SCTP into the specified size. Note that the underlying
5139 * SCTP implementation may fragment into smaller sized chunks when the
5140 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005141 * the user. The default value for this option is '0' which indicates
5142 * the user is NOT limiting fragmentation and only the PMTU will effect
5143 * SCTP's choice of DATA chunk size. Note also that values set larger
5144 * than the maximum size of an IP datagram will effectively let SCTP
5145 * control fragmentation (i.e. the same as setting this option to 0).
5146 *
5147 * The following structure is used to access and modify this parameter:
5148 *
5149 * struct sctp_assoc_value {
5150 * sctp_assoc_t assoc_id;
5151 * uint32_t assoc_value;
5152 * };
5153 *
5154 * assoc_id: This parameter is ignored for one-to-one style sockets.
5155 * For one-to-many style sockets this parameter indicates which
5156 * association the user is performing an action upon. Note that if
5157 * this field's value is zero then the endpoints default value is
5158 * changed (effecting future associations only).
5159 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 */
5161static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5162 char __user *optval, int __user *optlen)
5163{
Wei Yongjune89c2092008-12-25 16:54:58 -08005164 struct sctp_assoc_value params;
5165 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Wei Yongjune89c2092008-12-25 16:54:58 -08005167 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005168 pr_warn("Use of int in maxseg socket option deprecated\n");
5169 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08005170 params.assoc_id = 0;
5171 } else if (len >= sizeof(struct sctp_assoc_value)) {
5172 len = sizeof(struct sctp_assoc_value);
5173 if (copy_from_user(&params, optval, sizeof(params)))
5174 return -EFAULT;
5175 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return -EINVAL;
5177
Wei Yongjune89c2092008-12-25 16:54:58 -08005178 asoc = sctp_id2assoc(sk, params.assoc_id);
5179 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Wei Yongjune89c2092008-12-25 16:54:58 -08005182 if (asoc)
5183 params.assoc_value = asoc->frag_point;
5184 else
5185 params.assoc_value = sctp_sk(sk)->user_frag;
5186
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 if (put_user(len, optlen))
5188 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005189 if (len == sizeof(int)) {
5190 if (copy_to_user(optval, &params.assoc_value, len))
5191 return -EFAULT;
5192 } else {
5193 if (copy_to_user(optval, &params, len))
5194 return -EFAULT;
5195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
5197 return 0;
5198}
5199
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005200/*
5201 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5202 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5203 */
5204static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5205 char __user *optval, int __user *optlen)
5206{
5207 int val;
5208
5209 if (len < sizeof(int))
5210 return -EINVAL;
5211
5212 len = sizeof(int);
5213
5214 val = sctp_sk(sk)->frag_interleave;
5215 if (put_user(len, optlen))
5216 return -EFAULT;
5217 if (copy_to_user(optval, &val, len))
5218 return -EFAULT;
5219
5220 return 0;
5221}
5222
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005223/*
5224 * 7.1.25. Set or Get the sctp partial delivery point
5225 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5226 */
5227static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5228 char __user *optval,
5229 int __user *optlen)
5230{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005231 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005232
5233 if (len < sizeof(u32))
5234 return -EINVAL;
5235
5236 len = sizeof(u32);
5237
5238 val = sctp_sk(sk)->pd_point;
5239 if (put_user(len, optlen))
5240 return -EFAULT;
5241 if (copy_to_user(optval, &val, len))
5242 return -EFAULT;
5243
Wei Yongjun7d743b72010-12-14 16:10:41 +00005244 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005245}
5246
Vlad Yasevich70331572007-03-23 11:34:36 -07005247/*
5248 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5249 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5250 */
5251static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5252 char __user *optval,
5253 int __user *optlen)
5254{
Neil Horman219b99a2008-03-05 13:44:46 -08005255 struct sctp_assoc_value params;
5256 struct sctp_sock *sp;
5257 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005258
Neil Horman219b99a2008-03-05 13:44:46 -08005259 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005260 pr_warn("Use of int in max_burst socket option deprecated\n");
5261 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08005262 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005263 } else if (len >= sizeof(struct sctp_assoc_value)) {
5264 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005265 if (copy_from_user(&params, optval, len))
5266 return -EFAULT;
5267 } else
5268 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005269
Neil Horman219b99a2008-03-05 13:44:46 -08005270 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005271
Neil Horman219b99a2008-03-05 13:44:46 -08005272 if (params.assoc_id != 0) {
5273 asoc = sctp_id2assoc(sk, params.assoc_id);
5274 if (!asoc)
5275 return -EINVAL;
5276 params.assoc_value = asoc->max_burst;
5277 } else
5278 params.assoc_value = sp->max_burst;
5279
5280 if (len == sizeof(int)) {
5281 if (copy_to_user(optval, &params.assoc_value, len))
5282 return -EFAULT;
5283 } else {
5284 if (copy_to_user(optval, &params, len))
5285 return -EFAULT;
5286 }
5287
5288 return 0;
5289
Vlad Yasevich70331572007-03-23 11:34:36 -07005290}
5291
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005292static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5293 char __user *optval, int __user *optlen)
5294{
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005295 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005296 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005297 __u16 data_len = 0;
5298 u32 num_idents;
5299
5300 if (!sctp_auth_enable)
5301 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005302
5303 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005304 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005305
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005306 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005307 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005308
5309 len = sizeof(struct sctp_hmacalgo) + data_len;
5310 num_idents = data_len / sizeof(u16);
5311
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005312 if (put_user(len, optlen))
5313 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005314 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005315 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005316 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5317 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005318 return 0;
5319}
5320
5321static int sctp_getsockopt_active_key(struct sock *sk, int len,
5322 char __user *optval, int __user *optlen)
5323{
5324 struct sctp_authkeyid val;
5325 struct sctp_association *asoc;
5326
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005327 if (!sctp_auth_enable)
5328 return -EACCES;
5329
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005330 if (len < sizeof(struct sctp_authkeyid))
5331 return -EINVAL;
5332 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5333 return -EFAULT;
5334
5335 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5336 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5337 return -EINVAL;
5338
5339 if (asoc)
5340 val.scact_keynumber = asoc->active_key_id;
5341 else
5342 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5343
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005344 len = sizeof(struct sctp_authkeyid);
5345 if (put_user(len, optlen))
5346 return -EFAULT;
5347 if (copy_to_user(optval, &val, len))
5348 return -EFAULT;
5349
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005350 return 0;
5351}
5352
5353static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5354 char __user *optval, int __user *optlen)
5355{
Al Viro411223c2007-10-14 19:21:20 +01005356 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005357 struct sctp_authchunks val;
5358 struct sctp_association *asoc;
5359 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005360 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005361 char __user *to;
5362
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005363 if (!sctp_auth_enable)
5364 return -EACCES;
5365
5366 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005367 return -EINVAL;
5368
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005369 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005370 return -EFAULT;
5371
Al Viro411223c2007-10-14 19:21:20 +01005372 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005373 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5374 if (!asoc)
5375 return -EINVAL;
5376
5377 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005378 if (!ch)
5379 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005380
5381 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05005382 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5383 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005384 return -EINVAL;
5385
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005386 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005387 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005388num:
5389 len = sizeof(struct sctp_authchunks) + num_chunks;
5390 if (put_user(len, optlen)) return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005391 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5392 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005393 return 0;
5394}
5395
5396static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5397 char __user *optval, int __user *optlen)
5398{
Al Viro411223c2007-10-14 19:21:20 +01005399 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005400 struct sctp_authchunks val;
5401 struct sctp_association *asoc;
5402 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005403 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005404 char __user *to;
5405
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005406 if (!sctp_auth_enable)
5407 return -EACCES;
5408
5409 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005410 return -EINVAL;
5411
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005412 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005413 return -EFAULT;
5414
Al Viro411223c2007-10-14 19:21:20 +01005415 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005416 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5417 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5418 return -EINVAL;
5419
5420 if (asoc)
5421 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5422 else
5423 ch = sctp_sk(sk)->ep->auth_chunk_list;
5424
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005425 if (!ch)
5426 goto num;
5427
Vlad Yasevichb40db682008-02-27 14:40:37 -05005428 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005429 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005430 return -EINVAL;
5431
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005432 if (copy_to_user(to, ch->chunks, num_chunks))
5433 return -EFAULT;
5434num:
5435 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005436 if (put_user(len, optlen))
5437 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005438 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5439 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005440
5441 return 0;
5442}
5443
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005444/*
5445 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5446 * This option gets the current number of associations that are attached
5447 * to a one-to-many style socket. The option value is an uint32_t.
5448 */
5449static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5450 char __user *optval, int __user *optlen)
5451{
5452 struct sctp_sock *sp = sctp_sk(sk);
5453 struct sctp_association *asoc;
5454 u32 val = 0;
5455
5456 if (sctp_style(sk, TCP))
5457 return -EOPNOTSUPP;
5458
5459 if (len < sizeof(u32))
5460 return -EINVAL;
5461
5462 len = sizeof(u32);
5463
5464 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5465 val++;
5466 }
5467
5468 if (put_user(len, optlen))
5469 return -EFAULT;
5470 if (copy_to_user(optval, &val, len))
5471 return -EFAULT;
5472
5473 return 0;
5474}
5475
Wei Yongjun209ba422011-04-17 17:27:08 +00005476/*
Michio Honda7dc04d72011-04-26 20:16:31 +09005477 * 8.1.23 SCTP_AUTO_ASCONF
5478 * See the corresponding setsockopt entry as description
5479 */
5480static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
5481 char __user *optval, int __user *optlen)
5482{
5483 int val = 0;
5484
5485 if (len < sizeof(int))
5486 return -EINVAL;
5487
5488 len = sizeof(int);
5489 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
5490 val = 1;
5491 if (put_user(len, optlen))
5492 return -EFAULT;
5493 if (copy_to_user(optval, &val, len))
5494 return -EFAULT;
5495 return 0;
5496}
5497
5498/*
Wei Yongjun209ba422011-04-17 17:27:08 +00005499 * 8.2.6. Get the Current Identifiers of Associations
5500 * (SCTP_GET_ASSOC_ID_LIST)
5501 *
5502 * This option gets the current list of SCTP association identifiers of
5503 * the SCTP associations handled by a one-to-many style socket.
5504 */
5505static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
5506 char __user *optval, int __user *optlen)
5507{
5508 struct sctp_sock *sp = sctp_sk(sk);
5509 struct sctp_association *asoc;
5510 struct sctp_assoc_ids *ids;
5511 u32 num = 0;
5512
5513 if (sctp_style(sk, TCP))
5514 return -EOPNOTSUPP;
5515
5516 if (len < sizeof(struct sctp_assoc_ids))
5517 return -EINVAL;
5518
5519 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5520 num++;
5521 }
5522
5523 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
5524 return -EINVAL;
5525
5526 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
5527
5528 ids = kmalloc(len, GFP_KERNEL);
5529 if (unlikely(!ids))
5530 return -ENOMEM;
5531
5532 ids->gaids_number_of_ids = num;
5533 num = 0;
5534 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5535 ids->gaids_assoc_id[num++] = asoc->assoc_id;
5536 }
5537
5538 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
5539 kfree(ids);
5540 return -EFAULT;
5541 }
5542
5543 kfree(ids);
5544 return 0;
5545}
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5548 char __user *optval, int __user *optlen)
5549{
5550 int retval = 0;
5551 int len;
5552
Frank Filz3f7a87d2005-06-20 13:14:57 -07005553 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5554 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 /* I can hardly begin to describe how wrong this is. This is
5557 * so broken as to be worse than useless. The API draft
5558 * REALLY is NOT helpful here... I am not convinced that the
5559 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5560 * are at all well-founded.
5561 */
5562 if (level != SOL_SCTP) {
5563 struct sctp_af *af = sctp_sk(sk)->pf->af;
5564
5565 retval = af->getsockopt(sk, level, optname, optval, optlen);
5566 return retval;
5567 }
5568
5569 if (get_user(len, optlen))
5570 return -EFAULT;
5571
5572 sctp_lock_sock(sk);
5573
5574 switch (optname) {
5575 case SCTP_STATUS:
5576 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5577 break;
5578 case SCTP_DISABLE_FRAGMENTS:
5579 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5580 optlen);
5581 break;
5582 case SCTP_EVENTS:
5583 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5584 break;
5585 case SCTP_AUTOCLOSE:
5586 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5587 break;
5588 case SCTP_SOCKOPT_PEELOFF:
5589 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5590 break;
5591 case SCTP_PEER_ADDR_PARAMS:
5592 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5593 optlen);
5594 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00005595 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07005596 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08005597 optlen);
5598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 case SCTP_INITMSG:
5600 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 case SCTP_GET_PEER_ADDRS:
5603 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5604 optlen);
5605 break;
5606 case SCTP_GET_LOCAL_ADDRS:
5607 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5608 optlen);
5609 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04005610 case SCTP_SOCKOPT_CONNECTX3:
5611 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 case SCTP_DEFAULT_SEND_PARAM:
5614 retval = sctp_getsockopt_default_send_param(sk, len,
5615 optval, optlen);
5616 break;
5617 case SCTP_PRIMARY_ADDR:
5618 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5619 break;
5620 case SCTP_NODELAY:
5621 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5622 break;
5623 case SCTP_RTOINFO:
5624 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5625 break;
5626 case SCTP_ASSOCINFO:
5627 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5628 break;
5629 case SCTP_I_WANT_MAPPED_V4_ADDR:
5630 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5631 break;
5632 case SCTP_MAXSEG:
5633 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5634 break;
5635 case SCTP_GET_PEER_ADDR_INFO:
5636 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5637 optlen);
5638 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005639 case SCTP_ADAPTATION_LAYER:
5640 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 optlen);
5642 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005643 case SCTP_CONTEXT:
5644 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5645 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005646 case SCTP_FRAGMENT_INTERLEAVE:
5647 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5648 optlen);
5649 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005650 case SCTP_PARTIAL_DELIVERY_POINT:
5651 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5652 optlen);
5653 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005654 case SCTP_MAX_BURST:
5655 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5656 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005657 case SCTP_AUTH_KEY:
5658 case SCTP_AUTH_CHUNK:
5659 case SCTP_AUTH_DELETE_KEY:
5660 retval = -EOPNOTSUPP;
5661 break;
5662 case SCTP_HMAC_IDENT:
5663 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5664 break;
5665 case SCTP_AUTH_ACTIVE_KEY:
5666 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5667 break;
5668 case SCTP_PEER_AUTH_CHUNKS:
5669 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5670 optlen);
5671 break;
5672 case SCTP_LOCAL_AUTH_CHUNKS:
5673 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5674 optlen);
5675 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005676 case SCTP_GET_ASSOC_NUMBER:
5677 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5678 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00005679 case SCTP_GET_ASSOC_ID_LIST:
5680 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5681 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09005682 case SCTP_AUTO_ASCONF:
5683 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 default:
5686 retval = -ENOPROTOOPT;
5687 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 sctp_release_sock(sk);
5691 return retval;
5692}
5693
5694static void sctp_hash(struct sock *sk)
5695{
5696 /* STUB */
5697}
5698
5699static void sctp_unhash(struct sock *sk)
5700{
5701 /* STUB */
5702}
5703
5704/* Check if port is acceptable. Possibly find first available port.
5705 *
5706 * The port hash table (contained in the 'global' SCTP protocol storage
5707 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5708 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5709 * list (the list number is the port number hashed out, so as you
5710 * would expect from a hash function, all the ports in a given list have
5711 * such a number that hashes out to the same list number; you were
5712 * expecting that, right?); so each list has a set of ports, with a
5713 * link to the socket (struct sock) that uses it, the port number and
5714 * a fastreuse flag (FIXME: NPI ipg).
5715 */
5716static struct sctp_bind_bucket *sctp_bucket_create(
5717 struct sctp_bind_hashbucket *head, unsigned short snum);
5718
5719static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5720{
5721 struct sctp_bind_hashbucket *head; /* hash list */
5722 struct sctp_bind_bucket *pp; /* hash list port iterator */
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005723 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 unsigned short snum;
5725 int ret;
5726
Al Viro04afd8b2006-11-20 17:02:01 -08005727 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5730 sctp_local_bh_disable();
5731
5732 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07005733 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07005734 int low, high, remaining, index;
5735 unsigned int rover;
5736
5737 inet_get_local_port_range(&low, &high);
5738 remaining = (high - low) + 1;
5739 rover = net_random() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 do {
5742 rover++;
5743 if ((rover < low) || (rover > high))
5744 rover = low;
Amerigo Wange3826f12010-05-05 00:27:06 +00005745 if (inet_is_reserved_local_port(rover))
5746 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 index = sctp_phashfn(rover);
5748 head = &sctp_port_hashtable[index];
5749 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005750 sctp_for_each_hentry(pp, node, &head->chain)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (pp->port == rover)
5752 goto next;
5753 break;
5754 next:
5755 sctp_spin_unlock(&head->lock);
5756 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 /* Exhausted local port range during search? */
5759 ret = 1;
5760 if (remaining <= 0)
5761 goto fail;
5762
5763 /* OK, here is the one we will use. HEAD (the port
5764 * hash table list entry) is non-NULL and we hold it's
5765 * mutex.
5766 */
5767 snum = rover;
5768 } else {
5769 /* We are given an specific port number; we verify
5770 * that it is not being used. If it is used, we will
5771 * exahust the search in the hash list corresponding
5772 * to the port number (snum) - we detect that with the
5773 * port iterator, pp being NULL.
5774 */
5775 head = &sctp_port_hashtable[sctp_phashfn(snum)];
5776 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005777 sctp_for_each_hentry(pp, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 if (pp->port == snum)
5779 goto pp_found;
5780 }
5781 }
5782 pp = NULL;
5783 goto pp_not_found;
5784pp_found:
5785 if (!hlist_empty(&pp->owner)) {
5786 /* We had a port hash table hit - there is an
5787 * available port (pp != NULL) and it is being
5788 * used by other socket (pp->owner not empty); that other
5789 * socket is going to be sk2.
5790 */
5791 int reuse = sk->sk_reuse;
5792 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005795 if (pp->fastreuse && sk->sk_reuse &&
5796 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 goto success;
5798
5799 /* Run through the list of sockets bound to the port
5800 * (pp->port) [via the pointers bind_next and
5801 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5802 * we get the endpoint they describe and run through
5803 * the endpoint's list of IP (v4 or v6) addresses,
5804 * comparing each of the addresses with the address of
5805 * the socket sk. If we find a match, then that means
5806 * that this port/socket (sk) combination are already
5807 * in an endpoint.
5808 */
5809 sk_for_each_bound(sk2, node, &pp->owner) {
5810 struct sctp_endpoint *ep2;
5811 ep2 = sctp_sk(sk2)->ep;
5812
Vlad Yasevich4e540642008-07-18 23:06:32 -07005813 if (sk == sk2 ||
5814 (reuse && sk2->sk_reuse &&
5815 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 continue;
5817
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005818 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5819 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 ret = (long)sk2;
5821 goto fail_unlock;
5822 }
5823 }
5824 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5825 }
5826pp_not_found:
5827 /* If there was a hash table miss, create a new port. */
5828 ret = 1;
5829 if (!pp && !(pp = sctp_bucket_create(head, snum)))
5830 goto fail_unlock;
5831
5832 /* In either case (hit or miss), make sure fastreuse is 1 only
5833 * if sk->sk_reuse is too (that is, if the caller requested
5834 * SO_REUSEADDR on this socket -sk-).
5835 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005836 if (hlist_empty(&pp->owner)) {
5837 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5838 pp->fastreuse = 1;
5839 else
5840 pp->fastreuse = 0;
5841 } else if (pp->fastreuse &&
5842 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 pp->fastreuse = 0;
5844
5845 /* We are set, so fill up all the data in the hash table
5846 * entry, tie the socket list information with the rest of the
5847 * sockets FIXME: Blurry, NPI (ipg).
5848 */
5849success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005851 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 sk_add_bind_node(sk, &pp->owner);
5853 sctp_sk(sk)->bind_hash = pp;
5854 }
5855 ret = 0;
5856
5857fail_unlock:
5858 sctp_spin_unlock(&head->lock);
5859
5860fail:
5861 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 return ret;
5863}
5864
5865/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5866 * port is requested.
5867 */
5868static int sctp_get_port(struct sock *sk, unsigned short snum)
5869{
5870 long ret;
5871 union sctp_addr addr;
5872 struct sctp_af *af = sctp_sk(sk)->pf->af;
5873
5874 /* Set up a dummy address struct from the sk. */
5875 af->from_sk(&addr, sk);
5876 addr.v4.sin_port = htons(snum);
5877
5878 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5879 ret = sctp_get_port_local(sk, &addr);
5880
Eric Dumazeta02cec22010-09-22 20:43:57 +00005881 return ret ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882}
5883
5884/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 * Move a socket to LISTENING state.
5886 */
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005887SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005889 struct sctp_sock *sp = sctp_sk(sk);
5890 struct sctp_endpoint *ep = sp->ep;
Herbert Xu1b489e12006-08-20 15:07:14 +10005891 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893 /* Allocate HMAC for generating cookie. */
Vlad Yasevich23b29ed2008-07-18 23:06:07 -07005894 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005895 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005896 if (IS_ERR(tfm)) {
5897 if (net_ratelimit()) {
Joe Perches145ce502010-08-24 13:21:08 +00005898 pr_info("failed to load transform for %s: %ld\n",
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005899 sctp_hmac_alg, PTR_ERR(tfm));
5900 }
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005901 return -ENOSYS;
5902 }
5903 sctp_sk(sk)->hmac = tfm;
5904 }
5905
5906 /*
5907 * If a bind() or sctp_bindx() is not called prior to a listen()
5908 * call that allows new associations to be accepted, the system
5909 * picks an ephemeral port and will choose an address set equivalent
5910 * to binding with a wildcard address.
5911 *
5912 * This is not currently spelled out in the SCTP sockets
5913 * extensions draft, but follows the practice as seen in TCP
5914 * sockets.
5915 *
5916 */
5917 sk->sk_state = SCTP_SS_LISTENING;
5918 if (!ep->base.bind_addr.port) {
5919 if (sctp_autobind(sk))
5920 return -EAGAIN;
5921 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005922 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005923 sk->sk_state = SCTP_SS_CLOSED;
5924 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 }
5926 }
5927
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005928 sk->sk_max_ack_backlog = backlog;
5929 sctp_hash_endpoint(ep);
5930 return 0;
5931}
5932
5933/*
5934 * 4.1.3 / 5.1.3 listen()
5935 *
5936 * By default, new associations are not accepted for UDP style sockets.
5937 * An application uses listen() to mark a socket as being able to
5938 * accept new associations.
5939 *
5940 * On TCP style sockets, applications use listen() to ready the SCTP
5941 * endpoint for accepting inbound associations.
5942 *
5943 * On both types of endpoints a backlog of '0' disables listening.
5944 *
5945 * Move a socket to LISTENING state.
5946 */
5947int sctp_inet_listen(struct socket *sock, int backlog)
5948{
5949 struct sock *sk = sock->sk;
5950 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
5951 int err = -EINVAL;
5952
5953 if (unlikely(backlog < 0))
5954 return err;
5955
5956 sctp_lock_sock(sk);
5957
5958 /* Peeled-off sockets are not allowed to listen(). */
5959 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
5960 goto out;
5961
5962 if (sock->state != SS_UNCONNECTED)
5963 goto out;
5964
5965 /* If backlog is zero, disable listening. */
5966 if (!backlog) {
5967 if (sctp_sstate(sk, CLOSED))
5968 goto out;
5969
5970 err = 0;
5971 sctp_unhash_endpoint(ep);
5972 sk->sk_state = SCTP_SS_CLOSED;
5973 if (sk->sk_reuse)
5974 sctp_sk(sk)->bind_hash->fastreuse = 1;
5975 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005976 }
5977
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005978 /* If we are already listening, just update the backlog */
5979 if (sctp_sstate(sk, LISTENING))
5980 sk->sk_max_ack_backlog = backlog;
5981 else {
5982 err = sctp_listen_start(sk, backlog);
5983 if (err)
5984 goto out;
5985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005987 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988out:
5989 sctp_release_sock(sk);
5990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991}
5992
5993/*
5994 * This function is done by modeling the current datagram_poll() and the
5995 * tcp_poll(). Note that, based on these implementations, we don't
5996 * lock the socket in this function, even though it seems that,
5997 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08005998 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 * in this place. We assume that we don't need locks either until proven
6000 * otherwise.
6001 *
6002 * Another thing to note is that we include the Async I/O support
6003 * here, again, by modeling the current TCP/UDP code. We don't have
6004 * a good way to test with it yet.
6005 */
6006unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6007{
6008 struct sock *sk = sock->sk;
6009 struct sctp_sock *sp = sctp_sk(sk);
6010 unsigned int mask;
6011
Eric Dumazetaa395142010-04-20 13:03:51 +00006012 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
6014 /* A TCP-style listening socket becomes readable when the accept queue
6015 * is not empty.
6016 */
6017 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6018 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006019 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 mask = 0;
6022
6023 /* Is there any exceptional events? */
6024 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
6025 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08006026 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006027 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 if (sk->sk_shutdown == SHUTDOWN_MASK)
6029 mask |= POLLHUP;
6030
6031 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006032 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 mask |= POLLIN | POLLRDNORM;
6034
6035 /* The association is either gone or not ready. */
6036 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6037 return mask;
6038
6039 /* Is it writable? */
6040 if (sctp_writeable(sk)) {
6041 mask |= POLLOUT | POLLWRNORM;
6042 } else {
6043 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
6044 /*
6045 * Since the socket is not locked, the buffer
6046 * might be made available after the writeable check and
6047 * before the bit is set. This could cause a lost I/O
6048 * signal. tcp_poll() has a race breaker for this race
6049 * condition. Based on their implementation, we put
6050 * in the following code to cover it as well.
6051 */
6052 if (sctp_writeable(sk))
6053 mask |= POLLOUT | POLLWRNORM;
6054 }
6055 return mask;
6056}
6057
6058/********************************************************************
6059 * 2nd Level Abstractions
6060 ********************************************************************/
6061
6062static struct sctp_bind_bucket *sctp_bucket_create(
6063 struct sctp_bind_hashbucket *head, unsigned short snum)
6064{
6065 struct sctp_bind_bucket *pp;
6066
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08006067 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07006069 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 pp->port = snum;
6071 pp->fastreuse = 0;
6072 INIT_HLIST_HEAD(&pp->owner);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006073 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 }
6075 return pp;
6076}
6077
6078/* Caller must hold hashbucket lock for this tb with local BH disabled */
6079static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
6080{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07006081 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006082 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 kmem_cache_free(sctp_bucket_cachep, pp);
6084 SCTP_DBG_OBJCNT_DEC(bind_bucket);
6085 }
6086}
6087
6088/* Release this socket's reference to a local port. */
6089static inline void __sctp_put_port(struct sock *sk)
6090{
6091 struct sctp_bind_hashbucket *head =
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006092 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 struct sctp_bind_bucket *pp;
6094
6095 sctp_spin_lock(&head->lock);
6096 pp = sctp_sk(sk)->bind_hash;
6097 __sk_del_bind_node(sk);
6098 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006099 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 sctp_bucket_destroy(pp);
6101 sctp_spin_unlock(&head->lock);
6102}
6103
6104void sctp_put_port(struct sock *sk)
6105{
6106 sctp_local_bh_disable();
6107 __sctp_put_port(sk);
6108 sctp_local_bh_enable();
6109}
6110
6111/*
6112 * The system picks an ephemeral port and choose an address set equivalent
6113 * to binding with a wildcard address.
6114 * One of those addresses will be the primary address for the association.
6115 * This automatically enables the multihoming capability of SCTP.
6116 */
6117static int sctp_autobind(struct sock *sk)
6118{
6119 union sctp_addr autoaddr;
6120 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08006121 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
6123 /* Initialize a local sockaddr structure to INADDR_ANY. */
6124 af = sctp_sk(sk)->pf->af;
6125
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006126 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 af->inaddr_any(&autoaddr, port);
6128
6129 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
6130}
6131
6132/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
6133 *
6134 * From RFC 2292
6135 * 4.2 The cmsghdr Structure *
6136 *
6137 * When ancillary data is sent or received, any number of ancillary data
6138 * objects can be specified by the msg_control and msg_controllen members of
6139 * the msghdr structure, because each object is preceded by
6140 * a cmsghdr structure defining the object's length (the cmsg_len member).
6141 * Historically Berkeley-derived implementations have passed only one object
6142 * at a time, but this API allows multiple objects to be
6143 * passed in a single call to sendmsg() or recvmsg(). The following example
6144 * shows two ancillary data objects in a control buffer.
6145 *
6146 * |<--------------------------- msg_controllen -------------------------->|
6147 * | |
6148 *
6149 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
6150 *
6151 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
6152 * | | |
6153 *
6154 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
6155 *
6156 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
6157 * | | | | |
6158 *
6159 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6160 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
6161 *
6162 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
6163 *
6164 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6165 * ^
6166 * |
6167 *
6168 * msg_control
6169 * points here
6170 */
6171SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
6172 sctp_cmsgs_t *cmsgs)
6173{
6174 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006175 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 for (cmsg = CMSG_FIRSTHDR(msg);
6178 cmsg != NULL;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006179 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
6180 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 return -EINVAL;
6182
6183 /* Should we parse this header or ignore? */
6184 if (cmsg->cmsg_level != IPPROTO_SCTP)
6185 continue;
6186
6187 /* Strictly check lengths following example in SCM code. */
6188 switch (cmsg->cmsg_type) {
6189 case SCTP_INIT:
6190 /* SCTP Socket API Extension
6191 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
6192 *
6193 * This cmsghdr structure provides information for
6194 * initializing new SCTP associations with sendmsg().
6195 * The SCTP_INITMSG socket option uses this same data
6196 * structure. This structure is not used for
6197 * recvmsg().
6198 *
6199 * cmsg_level cmsg_type cmsg_data[]
6200 * ------------ ------------ ----------------------
6201 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
6202 */
6203 if (cmsg->cmsg_len !=
6204 CMSG_LEN(sizeof(struct sctp_initmsg)))
6205 return -EINVAL;
6206 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
6207 break;
6208
6209 case SCTP_SNDRCV:
6210 /* SCTP Socket API Extension
6211 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
6212 *
6213 * This cmsghdr structure specifies SCTP options for
6214 * sendmsg() and describes SCTP header information
6215 * about a received message through recvmsg().
6216 *
6217 * cmsg_level cmsg_type cmsg_data[]
6218 * ------------ ------------ ----------------------
6219 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
6220 */
6221 if (cmsg->cmsg_len !=
6222 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
6223 return -EINVAL;
6224
6225 cmsgs->info =
6226 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
6227
6228 /* Minimally, validate the sinfo_flags. */
6229 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07006230 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6231 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 return -EINVAL;
6233 break;
6234
6235 default:
6236 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 }
6239 return 0;
6240}
6241
6242/*
6243 * Wait for a packet..
6244 * Note: This function is the same function as in core/datagram.c
6245 * with a few modifications to make lksctp work.
6246 */
6247static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6248{
6249 int error;
6250 DEFINE_WAIT(wait);
6251
Eric Dumazetaa395142010-04-20 13:03:51 +00006252 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
6254 /* Socket errors? */
6255 error = sock_error(sk);
6256 if (error)
6257 goto out;
6258
6259 if (!skb_queue_empty(&sk->sk_receive_queue))
6260 goto ready;
6261
6262 /* Socket shut down? */
6263 if (sk->sk_shutdown & RCV_SHUTDOWN)
6264 goto out;
6265
6266 /* Sequenced packets can come disconnected. If so we report the
6267 * problem.
6268 */
6269 error = -ENOTCONN;
6270
6271 /* Is there a good reason to think that we may receive some data? */
6272 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6273 goto out;
6274
6275 /* Handle signals. */
6276 if (signal_pending(current))
6277 goto interrupted;
6278
6279 /* Let another process have a go. Since we are going to sleep
6280 * anyway. Note: This may cause odd behaviors if the message
6281 * does not fit in the user's buffer, but this seems to be the
6282 * only way to honor MSG_DONTWAIT realistically.
6283 */
6284 sctp_release_sock(sk);
6285 *timeo_p = schedule_timeout(*timeo_p);
6286 sctp_lock_sock(sk);
6287
6288ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00006289 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 return 0;
6291
6292interrupted:
6293 error = sock_intr_errno(*timeo_p);
6294
6295out:
Eric Dumazetaa395142010-04-20 13:03:51 +00006296 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 *err = error;
6298 return error;
6299}
6300
6301/* Receive a datagram.
6302 * Note: This is pretty much the same routine as in core/datagram.c
6303 * with a few changes to make lksctp work.
6304 */
6305static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6306 int noblock, int *err)
6307{
6308 int error;
6309 struct sk_buff *skb;
6310 long timeo;
6311
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 timeo = sock_rcvtimeo(sk, noblock);
6313
6314 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6315 timeo, MAX_SCHEDULE_TIMEOUT);
6316
6317 do {
6318 /* Again only user level code calls this function,
6319 * so nothing interrupt level
6320 * will suddenly eat the receive_queue.
6321 *
6322 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00006323 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 */
6325 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07006326 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 skb = skb_peek(&sk->sk_receive_queue);
6328 if (skb)
6329 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07006330 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 } else {
6332 skb = skb_dequeue(&sk->sk_receive_queue);
6333 }
6334
6335 if (skb)
6336 return skb;
6337
Neil Horman6736dc32005-12-02 20:30:06 -08006338 /* Caller is allowed not to check sk->sk_err before calling. */
6339 error = sock_error(sk);
6340 if (error)
6341 goto no_packet;
6342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 if (sk->sk_shutdown & RCV_SHUTDOWN)
6344 break;
6345
6346 /* User doesn't want to wait. */
6347 error = -EAGAIN;
6348 if (!timeo)
6349 goto no_packet;
6350 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6351
6352 return NULL;
6353
6354no_packet:
6355 *err = error;
6356 return NULL;
6357}
6358
6359/* If sndbuf has changed, wake up per association sndbuf waiters. */
6360static void __sctp_write_space(struct sctp_association *asoc)
6361{
6362 struct sock *sk = asoc->base.sk;
6363 struct socket *sock = sk->sk_socket;
6364
6365 if ((sctp_wspace(asoc) > 0) && sock) {
6366 if (waitqueue_active(&asoc->wait))
6367 wake_up_interruptible(&asoc->wait);
6368
6369 if (sctp_writeable(sk)) {
Eric Dumazeteaefd112011-02-18 03:26:36 +00006370 wait_queue_head_t *wq = sk_sleep(sk);
6371
6372 if (wq && waitqueue_active(wq))
6373 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 /* Note that we try to include the Async I/O support
6376 * here by modeling from the current TCP/UDP code.
6377 * We have not tested with it yet.
6378 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00006379 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08006380 sock_wake_async(sock,
6381 SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 }
6383 }
6384}
6385
Daniel Borkmann0fc175d2014-04-08 17:26:13 +02006386static void sctp_wake_up_waiters(struct sock *sk,
6387 struct sctp_association *asoc)
6388{
6389 struct sctp_association *tmp = asoc;
6390
6391 /* We do accounting for the sndbuf space per association,
6392 * so we only need to wake our own association.
6393 */
6394 if (asoc->ep->sndbuf_policy)
6395 return __sctp_write_space(asoc);
6396
Daniel Borkmann7658e7d2014-04-09 16:10:20 +02006397 /* If association goes down and is just flushing its
6398 * outq, then just normally notify others.
6399 */
6400 if (asoc->base.dead)
6401 return sctp_write_space(sk);
6402
Daniel Borkmann0fc175d2014-04-08 17:26:13 +02006403 /* Accounting for the sndbuf space is per socket, so we
6404 * need to wake up others, try to be fair and in case of
6405 * other associations, let them have a go first instead
6406 * of just doing a sctp_write_space() call.
6407 *
6408 * Note that we reach sctp_wake_up_waiters() only when
6409 * associations free up queued chunks, thus we are under
6410 * lock and the list of associations on a socket is
6411 * guaranteed not to change.
6412 */
6413 for (tmp = list_next_entry(tmp, asocs); 1;
6414 tmp = list_next_entry(tmp, asocs)) {
6415 /* Manually skip the head element. */
6416 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
6417 continue;
6418 /* Wake up association. */
6419 __sctp_write_space(tmp);
6420 /* We've reached the end. */
6421 if (tmp == asoc)
6422 break;
6423 }
6424}
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426/* Do accounting for the sndbuf space.
6427 * Decrement the used sndbuf space of the corresponding association by the
6428 * data size which was just transmitted(freed).
6429 */
6430static void sctp_wfree(struct sk_buff *skb)
6431{
6432 struct sctp_association *asoc;
6433 struct sctp_chunk *chunk;
6434 struct sock *sk;
6435
6436 /* Get the saved chunk pointer. */
6437 chunk = *((struct sctp_chunk **)(skb->cb));
6438 asoc = chunk->asoc;
6439 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006440 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6441 sizeof(struct sk_buff) +
6442 sizeof(struct sctp_chunk);
6443
Neil Horman4eb701d2005-04-28 12:02:04 -07006444 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6445
Neil Horman4d93df02007-08-15 16:07:44 -07006446 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006447 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07006448 */
6449 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006450 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07006451
Neil Horman4eb701d2005-04-28 12:02:04 -07006452 sock_wfree(skb);
Daniel Borkmann0fc175d2014-04-08 17:26:13 +02006453 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 sctp_association_put(asoc);
6456}
6457
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006458/* Do accounting for the receive space on the socket.
6459 * Accounting for the association is done in ulpevent.c
6460 * We set this as a destructor for the cloned data skbs so that
6461 * accounting is done at the correct time.
6462 */
6463void sctp_sock_rfree(struct sk_buff *skb)
6464{
6465 struct sock *sk = skb->sk;
6466 struct sctp_ulpevent *event = sctp_skb2event(skb);
6467
6468 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006469
6470 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006471 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07006472 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006473 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006474}
6475
6476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477/* Helper function to wait for space in the sndbuf. */
6478static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6479 size_t msg_len)
6480{
6481 struct sock *sk = asoc->base.sk;
6482 int err = 0;
6483 long current_timeo = *timeo_p;
6484 DEFINE_WAIT(wait);
6485
6486 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006487 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
6489 /* Increment the association's refcnt. */
6490 sctp_association_hold(asoc);
6491
6492 /* Wait on the association specific sndbuf space. */
6493 for (;;) {
6494 prepare_to_wait_exclusive(&asoc->wait, &wait,
6495 TASK_INTERRUPTIBLE);
6496 if (!*timeo_p)
6497 goto do_nonblock;
6498 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6499 asoc->base.dead)
6500 goto do_error;
6501 if (signal_pending(current))
6502 goto do_interrupted;
6503 if (msg_len <= sctp_wspace(asoc))
6504 break;
6505
6506 /* Let another process have a go. Since we are going
6507 * to sleep anyway.
6508 */
6509 sctp_release_sock(sk);
6510 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006511 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 sctp_lock_sock(sk);
6513
6514 *timeo_p = current_timeo;
6515 }
6516
6517out:
6518 finish_wait(&asoc->wait, &wait);
6519
6520 /* Release the association's refcnt. */
6521 sctp_association_put(asoc);
6522
6523 return err;
6524
6525do_error:
6526 err = -EPIPE;
6527 goto out;
6528
6529do_interrupted:
6530 err = sock_intr_errno(*timeo_p);
6531 goto out;
6532
6533do_nonblock:
6534 err = -EAGAIN;
6535 goto out;
6536}
6537
Wei Yongjun561b1732010-04-28 08:47:18 +00006538void sctp_data_ready(struct sock *sk, int len)
6539{
David S. Miller7ef52732010-05-02 21:43:40 -07006540 struct socket_wq *wq;
6541
6542 rcu_read_lock();
6543 wq = rcu_dereference(sk->sk_wq);
6544 if (wq_has_sleeper(wq))
6545 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00006546 POLLRDNORM | POLLRDBAND);
6547 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07006548 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00006549}
6550
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551/* If socket sndbuf has changed, wake up all per association waiters. */
6552void sctp_write_space(struct sock *sk)
6553{
6554 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07006557 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 __sctp_write_space(asoc);
6559 }
6560}
6561
6562/* Is there any sndbuf space available on the socket?
6563 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006564 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 * associations on the same socket. For a UDP-style socket with
6566 * multiple associations, it is possible for it to be "unwriteable"
6567 * prematurely. I assume that this is acceptable because
6568 * a premature "unwriteable" is better than an accidental "writeable" which
6569 * would cause an unwanted block under certain circumstances. For the 1-1
6570 * UDP-style sockets or TCP-style sockets, this code should work.
6571 * - Daisy
6572 */
6573static int sctp_writeable(struct sock *sk)
6574{
6575 int amt = 0;
6576
Eric Dumazet31e6d362009-06-17 19:05:41 -07006577 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 if (amt < 0)
6579 amt = 0;
6580 return amt;
6581}
6582
6583/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6584 * returns immediately with EINPROGRESS.
6585 */
6586static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6587{
6588 struct sock *sk = asoc->base.sk;
6589 int err = 0;
6590 long current_timeo = *timeo_p;
6591 DEFINE_WAIT(wait);
6592
Harvey Harrison0dc47872008-03-05 20:47:47 -08006593 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 (long)(*timeo_p));
6595
6596 /* Increment the association's refcnt. */
6597 sctp_association_hold(asoc);
6598
6599 for (;;) {
6600 prepare_to_wait_exclusive(&asoc->wait, &wait,
6601 TASK_INTERRUPTIBLE);
6602 if (!*timeo_p)
6603 goto do_nonblock;
6604 if (sk->sk_shutdown & RCV_SHUTDOWN)
6605 break;
6606 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6607 asoc->base.dead)
6608 goto do_error;
6609 if (signal_pending(current))
6610 goto do_interrupted;
6611
6612 if (sctp_state(asoc, ESTABLISHED))
6613 break;
6614
6615 /* Let another process have a go. Since we are going
6616 * to sleep anyway.
6617 */
6618 sctp_release_sock(sk);
6619 current_timeo = schedule_timeout(current_timeo);
6620 sctp_lock_sock(sk);
6621
6622 *timeo_p = current_timeo;
6623 }
6624
6625out:
6626 finish_wait(&asoc->wait, &wait);
6627
6628 /* Release the association's refcnt. */
6629 sctp_association_put(asoc);
6630
6631 return err;
6632
6633do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006634 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 err = -ETIMEDOUT;
6636 else
6637 err = -ECONNREFUSED;
6638 goto out;
6639
6640do_interrupted:
6641 err = sock_intr_errno(*timeo_p);
6642 goto out;
6643
6644do_nonblock:
6645 err = -EINPROGRESS;
6646 goto out;
6647}
6648
6649static int sctp_wait_for_accept(struct sock *sk, long timeo)
6650{
6651 struct sctp_endpoint *ep;
6652 int err = 0;
6653 DEFINE_WAIT(wait);
6654
6655 ep = sctp_sk(sk)->ep;
6656
6657
6658 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00006659 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 TASK_INTERRUPTIBLE);
6661
6662 if (list_empty(&ep->asocs)) {
6663 sctp_release_sock(sk);
6664 timeo = schedule_timeout(timeo);
6665 sctp_lock_sock(sk);
6666 }
6667
6668 err = -EINVAL;
6669 if (!sctp_sstate(sk, LISTENING))
6670 break;
6671
6672 err = 0;
6673 if (!list_empty(&ep->asocs))
6674 break;
6675
6676 err = sock_intr_errno(timeo);
6677 if (signal_pending(current))
6678 break;
6679
6680 err = -EAGAIN;
6681 if (!timeo)
6682 break;
6683 }
6684
Eric Dumazetaa395142010-04-20 13:03:51 +00006685 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
6687 return err;
6688}
6689
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006690static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691{
6692 DEFINE_WAIT(wait);
6693
6694 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00006695 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 if (list_empty(&sctp_sk(sk)->ep->asocs))
6697 break;
6698 sctp_release_sock(sk);
6699 timeout = schedule_timeout(timeout);
6700 sctp_lock_sock(sk);
6701 } while (!signal_pending(current) && timeout);
6702
Eric Dumazetaa395142010-04-20 13:03:51 +00006703 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704}
6705
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006706static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6707{
6708 struct sk_buff *frag;
6709
6710 if (!skb->data_len)
6711 goto done;
6712
6713 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07006714 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006715 sctp_skb_set_owner_r_frag(frag, sk);
6716
6717done:
6718 sctp_skb_set_owner_r(skb, sk);
6719}
6720
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006721void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6722 struct sctp_association *asoc)
6723{
6724 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08006725 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006726
6727 newsk->sk_type = sk->sk_type;
6728 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6729 newsk->sk_flags = sk->sk_flags;
6730 newsk->sk_no_check = sk->sk_no_check;
6731 newsk->sk_reuse = sk->sk_reuse;
6732
6733 newsk->sk_shutdown = sk->sk_shutdown;
6734 newsk->sk_destruct = inet_sock_destruct;
6735 newsk->sk_family = sk->sk_family;
6736 newsk->sk_protocol = IPPROTO_SCTP;
6737 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6738 newsk->sk_sndbuf = sk->sk_sndbuf;
6739 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6740 newsk->sk_lingertime = sk->sk_lingertime;
6741 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6742 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6743
6744 newinet = inet_sk(newsk);
6745
6746 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6747 * getsockname() and getpeername()
6748 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006749 newinet->inet_sport = inet->inet_sport;
6750 newinet->inet_saddr = inet->inet_saddr;
6751 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6752 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006753 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006754 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006755
6756 newinet->uc_ttl = inet->uc_ttl;
6757 newinet->mc_loop = 1;
6758 newinet->mc_ttl = 1;
6759 newinet->mc_index = 0;
6760 newinet->mc_list = NULL;
6761}
6762
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03006763static inline void sctp_copy_descendant(struct sock *sk_to,
6764 const struct sock *sk_from)
6765{
6766 int ancestor_size = sizeof(struct inet_sock) +
6767 sizeof(struct sctp_sock) -
6768 offsetof(struct sctp_sock, auto_asconf_list);
6769
6770 if (sk_from->sk_family == PF_INET6)
6771 ancestor_size += sizeof(struct ipv6_pinfo);
6772
6773 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
6774}
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776/* Populate the fields of the newsk from the oldsk and migrate the assoc
6777 * and its messages to the newsk.
6778 */
6779static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6780 struct sctp_association *assoc,
6781 sctp_socket_type_t type)
6782{
6783 struct sctp_sock *oldsp = sctp_sk(oldsk);
6784 struct sctp_sock *newsp = sctp_sk(newsk);
6785 struct sctp_bind_bucket *pp; /* hash list port iterator */
6786 struct sctp_endpoint *newep = newsp->ep;
6787 struct sk_buff *skb, *tmp;
6788 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006789 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 /* Migrate socket buffer sizes and all the socket level options to the
6792 * new socket.
6793 */
6794 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6795 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6796 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner493d6a22015-06-12 10:16:41 -03006797 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799 /* Restore the ep value that was overwritten with the above structure
6800 * copy.
6801 */
6802 newsp->ep = newep;
6803 newsp->hmac = NULL;
6804
6805 /* Hook this new socket in to the bind_hash list. */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006806 head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)];
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006807 sctp_local_bh_disable();
6808 sctp_spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 pp = sctp_sk(oldsk)->bind_hash;
6810 sk_add_bind_node(newsk, &pp->owner);
6811 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006812 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006813 sctp_spin_unlock(&head->lock);
6814 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006816 /* Copy the bind_addr list from the original endpoint to the new
6817 * endpoint so that we can handle restarts properly
6818 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08006819 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6820 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006821
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 /* Move any messages in the old socket's receive queue that are for the
6823 * peeled off association to the new socket's receive queue.
6824 */
6825 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6826 event = sctp_skb2event(skb);
6827 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006828 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006830 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
6832 }
6833
6834 /* Clean up any messages pending delivery due to partial
6835 * delivery. Three cases:
6836 * 1) No partial deliver; no work.
6837 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6838 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6839 */
6840 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006841 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006843 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 struct sk_buff_head *queue;
6845
6846 /* Decide which queue to move pd_lobby skbs to. */
6847 if (assoc->ulpq.pd_mode) {
6848 queue = &newsp->pd_lobby;
6849 } else
6850 queue = &newsk->sk_receive_queue;
6851
6852 /* Walk through the pd_lobby, looking for skbs that
6853 * need moved to the new socket.
6854 */
6855 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6856 event = sctp_skb2event(skb);
6857 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006858 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006860 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 }
6862 }
6863
6864 /* Clear up any skbs waiting for the partial
6865 * delivery to finish.
6866 */
6867 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006868 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
6870 }
6871
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006872 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006873 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006874
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006875 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006876 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 /* Set the type of socket to indicate that it is peeled off from the
6879 * original UDP-style socket or created with the accept() call on a
6880 * TCP-style socket..
6881 */
6882 newsp->type = type;
6883
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006884 /* Mark the new socket "in-use" by the user so that any packets
6885 * that may arrive on the association after we've moved it are
6886 * queued to the backlog. This prevents a potential race between
6887 * backlog processing on the old socket and new-packet processing
6888 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006889 *
6890 * The caller has just allocated newsk so we can guarantee that other
6891 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006892 */
Zach Brown5131a182007-06-22 15:14:46 -07006893 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
6896 /* If the association on the newsk is already closed before accept()
6897 * is called, set RCV_SHUTDOWN flag.
6898 */
6899 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6900 newsk->sk_shutdown |= RCV_SHUTDOWN;
6901
6902 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006903 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
Neil Horman4d93df02007-08-15 16:07:44 -07006906
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907/* This proto struct describes the ULP interface for SCTP. */
6908struct proto sctp_prot = {
6909 .name = "SCTP",
6910 .owner = THIS_MODULE,
6911 .close = sctp_close,
6912 .connect = sctp_connect,
6913 .disconnect = sctp_disconnect,
6914 .accept = sctp_accept,
6915 .ioctl = sctp_ioctl,
6916 .init = sctp_init_sock,
6917 .destroy = sctp_destroy_sock,
6918 .shutdown = sctp_shutdown,
6919 .setsockopt = sctp_setsockopt,
6920 .getsockopt = sctp_getsockopt,
6921 .sendmsg = sctp_sendmsg,
6922 .recvmsg = sctp_recvmsg,
6923 .bind = sctp_bind,
6924 .backlog_rcv = sctp_backlog_rcv,
6925 .hash = sctp_hash,
6926 .unhash = sctp_unhash,
6927 .get_port = sctp_get_port,
6928 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006929 .sysctl_mem = sysctl_sctp_mem,
6930 .sysctl_rmem = sysctl_sctp_rmem,
6931 .sysctl_wmem = sysctl_sctp_wmem,
6932 .memory_pressure = &sctp_memory_pressure,
6933 .enter_memory_pressure = sctp_enter_memory_pressure,
6934 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006935 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936};
6937
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006938#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006939
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940struct proto sctpv6_prot = {
6941 .name = "SCTPv6",
6942 .owner = THIS_MODULE,
6943 .close = sctp_close,
6944 .connect = sctp_connect,
6945 .disconnect = sctp_disconnect,
6946 .accept = sctp_accept,
6947 .ioctl = sctp_ioctl,
6948 .init = sctp_init_sock,
6949 .destroy = sctp_destroy_sock,
6950 .shutdown = sctp_shutdown,
6951 .setsockopt = sctp_setsockopt,
6952 .getsockopt = sctp_getsockopt,
6953 .sendmsg = sctp_sendmsg,
6954 .recvmsg = sctp_recvmsg,
6955 .bind = sctp_bind,
6956 .backlog_rcv = sctp_backlog_rcv,
6957 .hash = sctp_hash,
6958 .unhash = sctp_unhash,
6959 .get_port = sctp_get_port,
6960 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006961 .sysctl_mem = sysctl_sctp_mem,
6962 .sysctl_rmem = sysctl_sctp_rmem,
6963 .sysctl_wmem = sysctl_sctp_wmem,
6964 .memory_pressure = &sctp_memory_pressure,
6965 .enter_memory_pressure = sctp_enter_memory_pressure,
6966 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006967 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006969#endif /* IS_ENABLED(CONFIG_IPV6) */